Skip to main content

Module events

Module events 

Source
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.
EntryMsrs
The guest’s fast-system-call entry points.
EventMatch
An event delivery found by inspecting the interrupt frame.
Gate
One IDT gate.

Functions§

has_error_code
Does exception vector push an error code (in protected/long mode)?
is_trap_vector
Is vector a trap-class exception (reported after the instruction)?
match_event
Checks whether post is the first instruction of a handler entered by an event while the vCPU was at pre, and if so which one.
read_idt
Reads every present interrupt/trap gate of the vCPU’s IDT.