Expand description
Driving a KVM vCPU: the glue between a kvm_ioctls::VcpuFd and the
tracing engine.
If your VMM is written in Rust on top of kvm-ioctls, this is the
whole integration: create a KvmTracer per vCPU, call
KvmTracer::enable once, and hand every VcpuExit::Debug to
KvmTracer::on_debug_exit. Everything else your run loop already does
(port I/O, MMIO, HLT) stays exactly as it was.
let session = Session::new(TraceOptions::default(), mem, Some("out".as_ref()))?;
let mut tracer = KvmTracer::new(&session, 0)?;
tracer.enable(vcpu)?;
let mut held = None; // the token, kept while an entry breakpoint is lifted
loop {
let token = held.take().or_else(|| tracer.needs_token(vcpu).then(|| session.token()).flatten());
let exit = vcpu.run().map_err(|e| kvmtrace::Error::Kvm { what: "KVM_RUN", source: e })?;
match exit {
VcpuExit::Debug(d) => {
tracer.on_debug_exit(vcpu, &d, token.is_none())?;
}
VcpuExit::Hlt => {
tracer.flush(vcpu)?; // the HLT itself: no further step will report it
break;
}
_ => { /* your VMM's usual exit handling */ }
}
if tracer.holds_lifted_breakpoint() {
held = token;
}
}
tracer.finish()?;
session.finish()?;Structs§
- KvmContext
StopContextbacked by KVM ioctls on a vCPU fd.- KvmTracer
- Traces one KVM vCPU.
Constants§
- GUARD_
CONTROL - Debug state of a vCPU between trace windows, with the guard on: every
#DB exits to the VMM (
USE_HW_BP, with no hardware breakpoint set), so a stale single-step trap can be caught; seeKvmTracer::disable.
Functions§
- absorb_
stale_ single_ step - Handles a
KVM_EXIT_DEBUGthat arrives while a vCPU is not being traced. Returnstrueif it was a stale single-step trap, now absorbed: re-enter the vCPU.falsemeans it is the guest’s own (or a VMM debugger’s) #DB: hand the exit to the VMM as usual (QEMU passes such exceptions back to the guest). - debug_
control - The
KVM_SET_GUEST_DEBUGcontrol word for tracing. - read_
state - Reads the vCPU state with
KVM_GET_REGS+KVM_GET_SREGS. - read_
state_ synced - Reads the vCPU state from the
kvm_runshared page, where KVM copied it on the last exit (KVM_CAP_SYNC_REGS). No system call.