Expand description
§Following the CPU into Handlers
In the last chapter we saw that the trap flag is cleared whenever the CPU
delivers an interrupt or exception or executes SYSCALL, so the handler
runs unseen. This chapter shows how kvmtrace follows the CPU anyway, and
how it tells a fault from an interrupt from a trap, using nothing but the
state before and after a step.
§The idea: breakpoints on every door into the kernel
The CPU can only enter a handler through a small, known set of doors:
- the IDT: 256 gates, one per vector (exceptions, interrupts,
int n); SYSCALL: the address in theIA32_LSTARMSR (andIA32_CSTARfor 32-bit callers);SYSENTER: the address inIA32_SYSENTER_EIP.
So we put a software breakpoint (the one-byte int3, 0xCC) on the first
byte of every one of them, and tell KVM to send breakpoints to us rather
than to the guest (KVM_GUESTDBG_USE_SW_BP). TF is cleared on entry,
but the very first handler instruction is a breakpoint, so we stop anyway.
When a breakpoint is hit, KVM reports exception 3 with RIP at the breakpoint, before the instruction ran. The tracer then:
- records the event (more on that below);
- writes the original byte back (lifts the breakpoint);
- re-arms single-stepping, which re-sets TF;
- lets the vCPU execute the real first instruction;
- puts the breakpoint back.
From there TF does its usual job until the handler returns. This is
Breakpoints; its
sync reads the IDT (in long,
protected or real mode) and the three MSRs and plants what is missing.
§Keeping the breakpoints honest
The guest must never notice them:
- Reading code. When the tracer decodes guest instructions, it reads
through a filter that shows the original bytes
(
decode::fetchtakes anoriginal_bytecallback). - Writing code. If a traced instruction writes to a breakpointed byte
(the kernel patching itself with
text_poke, say), the new byte becomes the “original” and the breakpoint is re-planted (on_guest_write). - Moving the doors. A write to the IDT,
lidt, orwrmsrto one of the entry MSRs marks the set stale, and it is re-synced at the next stop. - The guest’s own
int3. A breakpoint that is not ours (a debugger in the guest, Linux’s own code patching) is handed back to the guest withKVM_GUESTDBG_INJECT_BP.
§Several vCPUs: never leave a door unguarded
There is one more rule, and breaking it crashes the guest. A vCPU running
without USE_SW_BP that reaches a planted breakpoint delivers #BP to the
guest, and since the #BP handler’s own first byte is a breakpoint too, the
guest recurses until it overflows its exception stack. (We found this the
hard way: a two-vCPU Linux guest died with BUG: #DF stack guard page was hit in asm_exc_int3.) So:
While any breakpoint is in guest memory, every vCPU that can run guest code must have
KVM_GUESTDBG_USE_SW_BPenabled.
KvmTracer::prearm turns USE_SW_BP on
without stepping. To start, call it on every vCPU and wait for all of them
before any calls enable, which is what
plants. To stop, stop stepping everywhere, call
Session::remove_breakpoints, and
only then disable. Listing 5-1 shows
the start in a threaded VMM; libkvmtrace_preload.so does the same for
QEMU (chapter 7).
Listing 5-1: Starting a multi-vCPU trace safely
let mut tracer = KvmTracer::new(session, id)?;
tracer.prearm(vcpu)?; // int3 now comes to us on this vCPU
all_armed.wait(); // ...and on every other vCPU
tracer.enable(vcpu)?; // safe to plant breakpoints and start stepping§Telling events apart
When a step ends somewhere unexpected, the tracer must decide what
happened. It looks at the stack of the handler it landed in: the CPU has
just pushed an interrupt frame there (RIP, CS, RFLAGS, RSP, SS, plus an
error code for vectors 8, 10–14, 17, 21, 29 and 30). The return RIP in
that frame, compared with the instruction we were stepping, tells the
story (events::match_event):
| Return RIP is… | and the vector is… | So the instruction… | Event kind |
|---|---|---|---|
| the instruction’s own RIP | an exception (< 32), not #DB/#BP/#OF, not NMI | faulted: it will be retried | Exception |
| the instruction’s own RIP | anything else | never started: an interrupt arrived first | Interrupt |
| one of its possible successors | an exception, or int n | completed, then trapped (int3, into, int n, syscall) | Software |
| one of its possible successors | ≥ 32 | completed, then an interrupt was taken | Interrupt |
For a REP instruction “the own RIP” is ambiguous (an interrupt between
iterations also returns there), so the tracer also compares RCX before
and after: if it changed, an iteration completed.
The EventRecord includes the event’s own memory
traffic, because a cache sees that too: the IDT descriptor read, and the
interrupt frame write (40 bytes in long mode, 48 with an error code) at
the handler’s stack.
§Where this leaves us
With handlers followed, the successor check becomes meaningful again:
every RIP change is explained either by the instruction or by an event
frame. On the Linux guest of chapter 9 the tracer recorded page faults,
timer and IPI interrupts and their handlers with zero gaps. When
handlers are not followed (TraceOptions::follow_handlers
= false), the test without_entry_breakpoints_handlers_are_missed shows
the cost: the handlers simply vanish, and also the successor check
cannot see it. That is why following handlers is the default, and why
chapter 9 validates against an independent reference rather than trusting
the tracer’s own consistency checks.