Skip to main content

Module kvm

Module kvm 

Source
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
StopContext backed 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; see KvmTracer::disable.

Functions§

absorb_stale_single_step
Handles a KVM_EXIT_DEBUG that arrives while a vCPU is not being traced. Returns true if it was a stale single-step trap, now absorbed: re-enter the vCPU. false means 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_DEBUG control 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_run shared page, where KVM copied it on the last exit (KVM_CAP_SYNC_REGS). No system call.