KVM(3)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’… 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 via mem(4) and kmem(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:

  1. print errors to stderr automatically, or
  2. 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 with kvm_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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms