From 3b9f34189259c94a8fbd26bc16cd28d167282e35 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Sat, 11 Dec 2021 22:06:22 +0100 Subject: vcpu: allow to enable/disable guest single stepping (debug) --- src/vcpu.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/vcpu.rs') diff --git a/src/vcpu.rs b/src/vcpu.rs index 147f602..0a3de04 100644 --- a/src/vcpu.rs +++ b/src/vcpu.rs @@ -108,6 +108,7 @@ impl Vcpu { /// /// [kvm-set-debugregs]: /// https://www.kernel.org/doc/html/latest/virt/kvm/api.html#kvm-set-debugregs + #[cfg(target_arch = "x86_64")] pub fn set_debugregs(&self, dregs: kvm_sys::kvm_debugregs) -> io::Result<()> { ioctl( &self.vcpu, @@ -117,6 +118,34 @@ impl Vcpu { .map(|_| ()) } + /// Enable or disable guest single steppig (debug) with the + /// [`KVM_GUESTDBG_ENABLE`][kvm-guest-debug] ioctl. + /// + /// [kvm-guest-debug]: https://www.kernel.org/doc/html/latest/virt/kvm/api.html#kvm-set-guest-debug + #[cfg(target_arch = "x86_64")] + pub fn set_single_step(&self, enable: bool) -> io::Result<()> { + let mut dbg = kvm_sys::kvm_guest_debug::default(); + + if enable { + // Enable guest debugging and single stepping. + dbg.control = kvm_sys::KVM_GUESTDBG_ENABLE | kvm_sys::KVM_GUESTDBG_SINGLESTEP; + } + + // Initialize debug registers based on current VCPUs debug register values. + let dregs = self.get_debugregs()?; + dbg.arch.debugreg[0..4].copy_from_slice(&dregs.db); + // DR4-DR5 are reserved. + dbg.arch.debugreg[6] = dregs.dr6; + dbg.arch.debugreg[7] = dregs.dr7; + + ioctl( + &self.vcpu, + kvm_sys::KVM_SET_GUEST_DEBUG, + &dbg as *const _ as u64, + ) + .map(|_| ()) + } + /// Run the guest VCPU with the [`KVM_RUN`][kvm-run] ioctl until it exits with one of the exit /// reasons described in [`KvmExit`](crate::vcpu::KvmExit). /// -- cgit v1.2.3