Skip to main content

Module ch09_how_we_know_it_works

Module ch09_how_we_know_it_works 

Source
Expand description

§How We Know It Works

A tracer that is wrong is worse than none: its output looks exactly as plausible as a correct trace. This chapter lists every check kvmtrace has passed, what each one proves, and what none of them proves. Every number here comes from qemu-tests/measure.sh, which you can run yourself; the results file of the run quoted here is RESULTS.txt in the repository’s qemu-tests/results/.

§In numbers

CheckResult
user-mode instructions vs. an independent TCG reference (PC, length, every byte read, every byte written)101 757 of 101 757 identical, including 16 faulted-then-retried loads
gaps (unexplained control transfers) on Linux guests, 1 and 2 vCPUs0
global order vs. hardware memory order, 2 vCPUs × 4096 lock xadd8192 of 8192 in exact order (serialized mode)
MacSim sideband entries vs. .raw records336 952 = 336 952, 0 index or address mismatches, CS.RPL = CPL on every record
MacSim record layout vs. MacSim’s C structidentical offsets (C compiler)
page walker vs. KVM’s own KVM_TRANSLATEidentical on 6 000+ addresses
timed windows (3 × 1 s, 2 vCPUs, no guest markers)guest ran to completion; each window’s sideband consistent; 1 stale trap absorbed
unit and integration testscargo test: all pass

§1. Against an independent reference

Problem. The tracer’s own consistency checks (does each RIP follow from the last?) cannot catch everything. Chapter 5 showed that untraced handlers return exactly where the check expects. And the memory accesses are computed from operands, so a decoder or planner bug would produce wrong addresses that look perfectly reasonable.

Idea. Run the same deterministic workload under QEMU’s TCG, where QEMU executes every instruction itself, and log every instruction and every memory access from a TCG plugin (qemu-tests/tcg-plugin/kvmtrace_ref.c). TCG’s memory callbacks fire for accesses that QEMU actually performed; they share no code with kvmtrace. Then compare instruction by instruction (kvmtrace compare).

The kernel part of the two runs legitimately differs: timer interrupts arrive at different points under TCG and KVM, and the CPU models differ. So the comparison covers the user-mode part, which is deterministic. The workload (qemu-tests/guest/workload.c) is a 24×24 matrix product, 16 first-touch page faults, and a system call, inside a marked window.

Result.

kvmtrace: 101 757 user-mode records (235 195 kernel records not compared)
TCG ref : 101 757 user-mode instructions
agree   : 101 757 instructions (PC, length, bytes read, bytes written); 16 faulted-then-retried
          221272 bytes read and 80 bytes written, all identical
RESULT  : MATCH

Getting there found three real bugs, which is what a reference is for:

  • The first comparison failed on the very first instruction. The user-mode out that opens the window reads 4 bytes of the TSS I/O-permission bitmap, an implicit access the CPU makes when CPL > IOPL, and kvmtrace did not report it. It does now (decode::io_permission_accesses).
  • Kernel ASLR moved the TSS between runs; the comparison now boots both with nokaslr.
  • When kvmtrace started recording the closing STOP out too, the comparison failed on that last instruction: the reference plugin closed its window before QEMU reported the out’s own TSS reads. The plugin now closes one instruction later. A reference has bugs too; it earns trust by being independent, not by being perfect.

§2b. Timed windows

Trace windows opened and closed on a timer (chapter 7) stop the guest at arbitrary points, including inside interrupt handlers. That exposed the stale trap-flag problem (a guest program killed by SIGTRAP after the window). With the guard, a 2-vCPU guest running a 14-second compute loop through three 1-second windows ran to completion, and each window’s files passed kvmtrace side --check.

§2. No gaps on a real kernel

Problem. A Linux kernel does everything a toy VM does not: page faults, timer and inter-processor interrupts, NMIs, system calls, stack switches.

Result. On Linux 6.18 guests, with handlers followed, every step was explained. That includes page faults (#14), local timer interrupts (236), IPIs (251, 253), an NMI (2), and the mov ss stack switches whose following instruction runs in the SS shadow (chapter 4). Before the SS shadow was handled, those were the only gaps we saw: 4 to 44 per run. After, 0.

§3. Global order equals memory order

Covered in chapter 6: two threads pinned to two vCPUs race on one counter with lock xadd. The values they get back are the hardware’s verdict on the order. Sorting the traced xadds by global sequence number reproduces it exactly for all 8192, while the threads were genuinely interleaved (thread 0 had 3147 of the first 4096 xadds in global order).

Concurrent mode passed the same check in our run too, but by luck, not by design: its global numbers are only the order in which steps were recorded.

§4. The MacSim files

  • Layout. A test compiles MacSim’s trace_info_cpu_s with the system C compiler and checks each field’s offset against our serializer. Another compiles macsim/kvmtrace_side.h and checks the sideband the same way.
  • Content. kvmtrace side --check reads each .raw file alongside its sideband: one entry per record, and the accesses the sideband marks “in MacSim” are exactly the record’s ld_vaddr1/2 and st_vaddr.

§5. Unit and integration tests (cargo test)

TestProves
every_handler_is_tracedentry breakpoints catch a #PF handler, an int 0x80 handler and an injected IRQ; faults are retried; REP iterations; event frames
without_entry_breakpoints_handlers_are_missedthe failure mode of plain TF, and that the successor check cannot see it
accesses_match_hand_computed_addressesSIB, RIP-relative, FS base, push/pop/call/ret, RMW, lock cmpxchg, cmov, rep stos with count 0, AVX2 gather lanes
walker_agrees_with_kvm_translatethe page walk
serialized_global_order_is_the_execution_orderthe xadd check on MiniVm
instruction_after_mov_ss_is_reconstructedthe SS shadow
macsim_record_layout_matches_the_c_struct, sideband_c_header_matches_the_writer, files_on_disk_round_tripthe file formats

§What is not proven

  • Kernel-mode memory accesses are checked against hand-computed values and by the absence of gaps, but not against an independent reference instruction by instruction: TCG and KVM kernels do not execute the same path.
  • Instruction coverage. The decoder is iced-x86, whose coverage is broad and tested, but our access planner has been checked against a reference only on what the workloads execute: general-purpose integer code, string operations, and the kernel’s own code. AVX-512 scatter, xsave-family and other exotic instructions rely on iced’s operand descriptions without a reference comparison.
  • MacSim itself has not consumed these traces yet in our tests. The layout and content are checked; running MacSim on them is the obvious next step.