Skip to main content

Module ch10_limits_and_future_work

Module ch10_limits_and_future_work 

Source
Expand description

§Limits and Future Work

Every tracing method sees some things and misses others. This chapter lists what kvmtrace does not do, or does only approximately, so you can judge whether that matters for your experiment. Each item says how it shows up in a trace, if it does.

§Speed: exhaustive tracing under KVM is slower than TCG

This is the most important limit, and it runs against intuition. KVM runs a guest at native speed, but an exhaustive trace makes the CPU stop after every instruction. Each stop is a VM exit, a return to user space, and the tracer’s own work. Measured on the same guest and window (chapter 9):

MethodInstructions/s while tracing (median of 5, range)
kvmtrace, one vCPU70 000 (53 000–89 000)
kvmtrace, two vCPUs, serialized19 000 in total
kvmtrace, two vCPUs, concurrent178 000 in total
QEMU TCG with a plugin logging every instruction and access3 520 000 (3 460 000–3 550 000)

So if all you need is every instruction of a window, and the guest can run under TCG, a TCG plugin is about 50× faster. (The laptop was shared with other work while measuring; kvmtrace’s per-step cost makes it far more sensitive to that than TCG, hence its wide range.) What KVM buys instead:

  • Fast-forward. Outside the trace window the guest runs at native speed under KVM. Booting Linux and reaching a region of interest takes well under a second, where TCG takes several seconds to minutes.
  • The real CPU. Instructions execute on the host’s silicon, with its exact semantics (-cpu host), not an emulation of them.
  • KVM-only setups. VMMs other than QEMU, or guests that depend on KVM features, can be traced.

Where the time goes per step: the KVM_RUN round trip including the debug exit, re-arming (KVM_SET_GUEST_DEBUG) after some instructions, reading state (mostly avoided with KVM_CAP_SYNC_REGS), decoding and planning, the software page walk, and compression. A kernel-side tracer (recording in KVM’s #DB intercept, no user-space round trip) would remove the largest part. That needs a kernel patch and is future work.

§Time runs differently for a traced guest

While traced, the guest executes thousands of times slower than normal, but its clocks and timers follow host time. Consequences:

  • Interrupt pacing. Delivering timer interrupts as they fall due would livelock the guest, so interrupts are held and let in every interrupt_pacing instructions (50 000 by default with QEMU). Interrupt timing in the trace is therefore not the guest’s natural timing. Every interrupt that is taken is traced.
  • Watchdogs. Linux may report RCU stalls or soft lockups during or just after a long trace window. These are messages, not failures, but they are visible in the guest.
  • Timing-dependent code. Code that measures elapsed time (spin-waits with timeouts, adaptive locks) behaves as on a very slow machine.

§Paravirtual shared memory

KVM updates some guest memory itself, on each vCPU entry. Steal time is one example: the guest’s read loop can never complete under single-step, so the QEMU interposer hides the feature (chapter 7). If a guest or another paravirtual feature uses the same pattern, the symptom is a guest stuck in a short read loop; kvmtrace dump shows it plainly.

§What is computed, not observed

The tracer observes the instruction stream (each RIP and the bytes there), the register state before and after each instruction, and every event. Memory accesses are computed: from the decoded operands and the register values before the instruction, by the same rules the CPU follows. That is exact for architectural accesses, and the TCG comparison in chapter 9 checks it byte for byte on user code. It does not include accesses the architecture does not define per instruction:

  • page-table walks by the MMU, and their Accessed/Dirty-bit updates;
  • speculative accesses and hardware prefetches;
  • device DMA and accesses by the hypervisor itself (e.g. KVM writing the steal-time or clock pages).

§Breakpoints are bytes in guest memory

Entry breakpoints are real 0xCC bytes. The tracer hides them from its own decoder and keeps them correct when traced code writes there, but:

  • A guest that reads its own handler code as data (an integrity check, or the kernel examining a patch site) reads 0xCC at the first byte of each handler. Linux’s normal operation does not do this on IDT entry points.
  • DMA reading guest kernel text would also see them.
  • Several vCPUs. In serialized mode the vCPU that lifts a breakpoint keeps the token until it is back, so no other vCPU can slip past. In concurrent mode the other vCPUs keep running. One that enters the same handler during that one step (microseconds) misses its entry, and that handler runs untraced. Use serialized mode when exhaustiveness matters.

§After a window: the trap-flag guard

Copies of RFLAGS that the guest saved while being stepped carry the trap flag. The guard that absorbs the resulting stale traps (chapter 7) has two effects inside the guest for as long as it is on: the guest’s own hardware breakpoints and watchpoints (DR0–DR7) do not fire, and the single-step traps of a debugger running inside the guest are absorbed too. A stale copy can in principle live indefinitely (a task that sleeps for an hour after being preempted during a window), so the guard stays on until the next window or the end of the run. Without it (KVMTRACE_TF_GUARD=0) such a task gets SIGTRAP when it resumes.

§Closing a window takes a moment on several vCPUs

A window closes when every vCPU has reached the stop rendezvous. With one vCPU that is immediate; on two vCPUs, 1-second timed windows took 1.0, 2.4 and 4.3 seconds from open to written in our runs. The trace is complete either way: vCPUs keep being traced until they stop. Only the window is longer than asked. Use KVMTRACE_MAX when you need an exact size.

§Things not traced at all

  • System Management Mode: SMM code runs outside KVM’s view.
  • Nested guests: instructions of an L2 guest run by a hypervisor inside the traced guest.
  • Anything between windows: by design. Use KVMTRACE_START=now to trace from the first instruction, and no DURATION to trace to the end.

§The MacSim format

Covered in chapter 8. The .raw record holds at most two loads and one store and no privilege level, CR3 or physical address. The sideband .side file holds all of them, but MacSim needs a small change in its reader to use them.

§Future work

  1. A kernel-side recorder in KVM’s #DB intercept, for 10× or more speed.
  2. A MacSim reader patch that loads the sideband, so MacSim’s MMU can key its page mapping by CR3 and use the guest’s own physical addresses.
  3. A guest-time mode that keeps guest time proportional to instructions executed (adjusting the TSC offset), instead of pacing interrupts.
  4. Intel PT as a fast path on Intel hosts, using single-step only to recover memory addresses for the instructions that need them.

§Glossary

TermMeaning
TFthe x86 trap flag: when set, the CPU raises #DB after each instruction
#DB, #BPdebug and breakpoint exceptions (vectors 1 and 3)
entry breakpointan int3 the tracer places on an IDT handler or SYSCALL/SYSENTER entry
liftput a breakpoint’s original byte back for one step
CPLcurrent privilege level: 0 kernel, 3 user. Equal to CS bits 1:0 in a stable state
CR3the control register holding the page-table root of the running process
linear / physical addressthe address after segmentation / after paging
gapa step whose result the tracer cannot explain: instructions may have run untraced
SS shadowafter MOV SS/POP SS, interrupts and debug traps wait one instruction
sidebandtrace_<v>.side: the per-record facts MacSim’s format has no room for
tokenthe lock that serializes vCPUs in Ordering::Serialized
TCGQEMU’s dynamic binary translator, the non-KVM way to run a guest