Expand description
§kvmtrace
Exhaustive instruction and memory-access traces of x86-64 guests running under KVM, written in the MacSim trace format.
kvmtrace single-steps a KVM virtual CPU, one instruction at a time, and
for every instruction it records what a cycle-level simulator needs:
| What | Where it comes from |
|---|---|
| RIP, opcode bytes, length | the guest’s own memory, decoded with iced-x86 |
| vCPU id | which vCPU stopped |
| linear and guest-physical address of every access | a software page walk of the guest’s own tables |
| read / write / read-modify-write, size, which instruction | the decoded operands, plus implicit accesses (stack, string, TSS, IDT, interrupt frames) |
| CPL (from SS/CS), CR3 | the vCPU’s registers at the stop |
| attempted vs. performed | did the instruction fault? on which address? |
| per-vCPU and global order | a per-vCPU counter, and a global token or counter |
It traces a guest’s kernel as well as its user programs: interrupt, exception and system-call handlers are followed, not skipped.
§What you asked for, and where it is
| Requirement | MacSim trace_<v>.raw | MacSim sideband trace_<v>.side | .kvt |
|---|---|---|---|
| x86-64 guest under QEMU with KVM | ✓ (libkvmtrace_preload.so, unmodified QEMU) | ||
| the instruction being executed | m_opcode (category), m_size, m_instruction_addr | exact bytes (opcode + operands), physical PC | ✓ |
| which CPU executed it | one file per vCPU | vcpu | ✓ |
| memory accesses: virtual address | m_ld_vaddr1/2, m_st_vaddr (first 2 loads, 1 store) | every access | ✓ |
| memory accesses: physical address | — | physical, physical_end | ✓ |
| read or write, size | m_num_ld, m_has_st, sizes | kind, size | ✓ |
| which core, which instruction caused it | the record it is in | the entry it is in | ✓ |
| CS (bits 1:0 = privilege level) | — | cs, cpl | ✓ |
| CR3 (current page table) | — | cr3 | ✓ |
| page tables themselves | not needed: MacSim translates | — | — |
| attempted vs. performed accesses | faulted instructions included | outcome per access | ✓ |
| per-core and global order | per file | local_seq, global_seq | ✓ |
| instruction fetches, switchable | — | ✓ (TraceOptions::instruction_fetches) | ✓ |
§In numbers
Measured on an AMD Ryzen 7 PRO 4750U laptop, Linux 6.18 guest, QEMU 10.2.4
with -accel kvm, the stock QEMU binary with libkvmtrace_preload.so:
| user-mode instructions checked against an independent TCG reference | 101 757 of 101 757 identical (PC, length, every byte read and written) |
| gaps (instructions that ran untraced) on the Linux guest | 0 |
global order checked against hardware memory order (2 vCPUs, lock xadd) | 8192 of 8192 in exact order |
| CS, CPL, CR3, physical addresses per MacSim record (sideband) | 336 952 of 336 952 entries consistent; CS bits 1:0 = CPL on every one |
| tracing speed, while a window is open | ≈ 70 000 instructions/s per vCPU (a TCG plugin is ≈ 50× faster: chapter 10) |
| guest speed while tracing is off | unchanged (within measurement noise) |
| MacSim record layout | byte-for-byte equal to MacSim’s C struct (checked by a C compiler) |
§Where to start
Read the book. It starts with a twenty-line program that traces a tiny guest, and ends with the limits of the method, the things this crate cannot tell you.
- To trace a VMM you wrote with
kvm-ioctls:kvm::KvmTracer. - To trace QEMU:
libkvmtrace_preload.so, chapter 7. - To read traces:
kvt::KvtReader,macsim::MacsimReader, and thekvmtracecommand-line tool.
Re-exports§
pub use error::Error;pub use error::Result;pub use record::AccessClass;pub use record::AccessKind;pub use record::AccessOutcome;pub use record::EventKind;pub use record::EventRecord;pub use record::InstructionOutcome;pub use record::InstructionRecord;pub use record::MemoryAccess;pub use record::RecordFlags;pub use record::Sequence;pub use record::TraceItem;pub use state::CpuMode;pub use state::CpuState;pub use tracer::Ordering;pub use tracer::Session;pub use tracer::Stats;pub use tracer::StopAction;pub use tracer::StopReason;pub use tracer::Token;pub use tracer::TraceOptions;pub use tracer::VcpuTracer;
Modules§
- book
- The kvmtrace Book
- decode
- Turning a register snapshot into “this instruction, touching these addresses”.
- error
- The one error type the crate returns.
- events
- Interrupts, exceptions and system calls: noticing them, and making sure the tracer follows the CPU into their handlers.
- kvm
- Driving a KVM vCPU: the glue between a
kvm_ioctls::VcpuFdand the tracing engine. - kvt
- The extended trace format (
.kvt): everything the tracer knows. - macsim
- Writing (and reading) traces in MacSim’s x86 format.
- memory
- Reading and writing guest-physical memory.
- minivm
- A tiny 64-bit virtual machine, for tests and for learning.
- paging
- A software walker for x86 guest page tables.
- record
- The trace records: what one executed instruction (or one interrupt) looks like after the tracer has finished with it.
- side
- The MacSim sideband: everything a MacSim
.rawrecord has no room for, one entry per record, in the same order. - state
- A snapshot of the architectural state of one virtual CPU.
- tracer
- The tracing engine: a session shared by all vCPUs, and one
VcpuTracerper vCPU.