Skip to main content

Crate kvmtrace

Crate kvmtrace 

Source
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:

WhatWhere it comes from
RIP, opcode bytes, lengththe guest’s own memory, decoded with iced-x86
vCPU idwhich vCPU stopped
linear and guest-physical address of every accessa software page walk of the guest’s own tables
read / write / read-modify-write, size, which instructionthe decoded operands, plus implicit accesses (stack, string, TSS, IDT, interrupt frames)
CPL (from SS/CS), CR3the vCPU’s registers at the stop
attempted vs. performeddid the instruction fault? on which address?
per-vCPU and global ordera 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

RequirementMacSim trace_<v>.rawMacSim sideband trace_<v>.side.kvt
x86-64 guest under QEMU with KVM✓ (libkvmtrace_preload.so, unmodified QEMU)
the instruction being executedm_opcode (category), m_size, m_instruction_addrexact bytes (opcode + operands), physical PC✓
which CPU executed itone file per vCPUvcpu✓
memory accesses: virtual addressm_ld_vaddr1/2, m_st_vaddr (first 2 loads, 1 store)every access✓
memory accesses: physical address—physical, physical_end✓
read or write, sizem_num_ld, m_has_st, sizeskind, size✓
which core, which instruction caused itthe record it is inthe entry it is in✓
CS (bits 1:0 = privilege level)—cs, cpl✓
CR3 (current page table)—cr3✓
page tables themselvesnot needed: MacSim translates——
attempted vs. performed accessesfaulted instructions includedoutcome per access✓
per-core and global orderper filelocal_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 reference101 757 of 101 757 identical (PC, length, every byte read and written)
gaps (instructions that ran untraced) on the Linux guest0
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 offunchanged (within measurement noise)
MacSim record layoutbyte-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.

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::VcpuFd and 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 .raw record 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 VcpuTracer per vCPU.