KVM(3)
π KVM(3) β kernel memory interface
okay bestie, so this is the kvm page from the Library Functions Manual, and honestly? it's giving "backstage pass to the kernel" β¨
π«Ά LIBRARY
Kernel Data Access Library β that's libkvm, and you link it with -lkvm. π
πΈ DESCRIPTION
The kvm library hands you one uniform, consistent interface for peeking at kernel virtual memory images β and that includes both live systems and crash dumps. no cap, it does both the same way π
So like, how do you actually get in?
- live systems β via
sysctl(3)for some functions, and viamem(4)andkmem(4)for other functions - crash dumps β you can examine those via the core file that
savecore(8)generates
and the whole point is that the interface behaves similarly in both cases. genuinely iconic design choice.
What can you actually do with it? π
- read memory β
- write memory β
- look up kernel symbol addresses efficiently β
- gather information about user processes β
β tiny but important detail: you call kvm_open() first, bestie. that gives you the descriptor you need for literally every subsequent call. no descriptor, no vibes.
π COMPATIBILITY
storytime π§ the kvm interface was first introduced in SunOS. and because a considerable number of programs have been written against it, backward compatibility is highkey highly desirable. the way that people depend on this stuffβ¦ π
In most respects, the Sun kvm interface is consistent and clean β ate, no notes. So the generic portion of the interface got incorporated into the BSD interface, and that generic portion is:
kvm_open()kvm_close()kvm_read()kvm_write()kvm_nlist()
and honestly? many kvm applications (think debuggers and statistical monitors) use only this subset. so if you learn these five, you're already kind of the main character π
The process interface was not kept. and before you come for it β this is not a portability issue. any code that manipulates processes is inherently machine dependent. that's just facts. π
Finally, the Sun kvm error reporting semantics are poorly defined π and the original setup had exactly two moods:
- print errors to
stderrautomatically, or - print no error messages at all β in which case the nature of the error cannot be determined π
to overcome this (because leaving you with mystery errors is not it), the BSD interface includes a routine, kvm_geterr(3), that returns β not prints out β the error message corresponding to the most recent error condition on the given descriptor. obsessed. π«Ά
π¦ CROSS DEBUGGING
okay this part is genuinely cool: the kvm library supports inspection of crash dumps from non-native kernels. π
π caution bestie: only a limited subset of the kvm interface is supported for these dumps. don't come in expecting the whole menu.
To inspect a crash dump of a non-native kernel, the caller must provide a resolver function when opening a descriptor via kvm_open2(). that's not optional, that's the entry fee.
Also, the interface defines:
- an integer type β
kvaddr_tβ that's large enough to hold all valid addresses of all supported architectures β¨ - a new namelist structure type β
struct kvm_nlistβ for use withkvm_nlist2()
And to avoid address truncation issues (we don't do truncation in this house), the caller should use kvm_nlist2() and kvm_read2() in place of kvm_nlist() and kvm_read(), respectively. π
Finally, for non-native crash dumps only this limited subset of operations is supported:
kvm_close()kvm_geterr()kvm_kerndisp()kvm_open2()kvm_native()kvm_nlist2()kvm_read2()
that's the whole list. periodt. π
π SEE ALSO
go read these, bestie, the whole friend group is here π«Ά
kvm_close(3), kvm_getargv(3), kvm_getenvv(3), kvm_geterr(3), kvm_getloadavg(3), kvm_getprocs(3), kvm_getswapinfo(3), kvm_kerndisp(3), kvm_native(3), kvm_nlist(3), kvm_nlist2(3), kvm_open(3), kvm_open2(3), kvm_openfiles(3), kvm_read(3), kvm_read2(3), kvm_write(3), sysctl(3), kmem(4), mem(4)
π HISTORY
The kvm_native(), kvm_nlist2(), kvm_open2(), and kvm_read2() functions first appeared in FreeBSD 11.0. β
and that's the whole page, bestie π this lil writeup is from the Debian edition of the manual, dated February 5, 2020, and it's the KVM(3) page β go forth and inspect some kernel memory πΈπ¦β¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms