Expand description
Interrupts, exceptions and system calls: noticing them, and making sure the tracer follows the CPU into their handlers.
§The problem
KVM single-steps a guest by setting RFLAGS.TF. But when the CPU
delivers an interrupt or exception through the IDT, it clears TF, and
SYSCALL clears it too (through IA32_FMASK, which Linux sets to include
TF). The handler therefore runs at full speed, untraced, and stepping
resumes only when iret/sysret restores the old flags. Measured on
this crate’s test VM, plain single-stepping misses every handler: an
int 0x80, a page fault, a SYSCALL and an external interrupt all looked
like one ordinary step.
§The fix
We put a software breakpoint (int3, intercepted by KVM with
KVM_GUESTDBG_USE_SW_BP) on the first byte of every IDT handler and of
the SYSCALL/SYSENTER entry points. Entering any handler now stops the
vCPU before the handler’s first instruction. The tracer puts the
original byte back, re-arms single-stepping, steps that one
instruction, and puts the breakpoint back.
The breakpoint table is shared by all vCPUs, because guest memory is.
Modules§
- msr
- MSR numbers of the fast-system-call entry points.
Structs§
- Breakpoints
- The shared table of entry-point breakpoints.
- Entry
Msrs - The guest’s fast-system-call entry points.
- Event
Match - An event delivery found by inspecting the interrupt frame.
- Gate
- One IDT gate.
Functions§
- has_
error_ code - Does exception
vectorpush an error code (in protected/long mode)? - is_
trap_ vector - Is
vectora trap-class exception (reported after the instruction)? - match_
event - Checks whether
postis the first instruction of a handler entered by an event while the vCPU was atpre, and if so which one. - read_
idt - Reads every present interrupt/trap gate of the vCPU’s IDT.