diff options
Diffstat (limited to 'src/vcpu.rs')
-rw-r--r-- | src/vcpu.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/vcpu.rs b/src/vcpu.rs index f0d311e..4b25736 100644 --- a/src/vcpu.rs +++ b/src/vcpu.rs @@ -18,6 +18,7 @@ pub enum KvmExit<'cpu> { IoOut(u16, &'cpu [u8]), MmioRead(u64, &'cpu mut [u8]), MmioWrite(u64, &'cpu [u8]), + Debug(u64), } /// Wrapper for VCPU ioctls. @@ -86,6 +87,65 @@ impl Vcpu { .map(|_| ()) } + /// Get the debug registers with the [`KVM_GET_DEBUGREGS`][kvm-get-debugregs] ioctl in form of + /// [`kvm_debugregs`](crate::kvm_sys::kvm_debugregs). + /// + /// [kvm-get-debugregs]: + /// https://www.kernel.org/doc/html/latest/virt/kvm/api.html#kvm-get-debugregs + #[cfg(target_arch = "x86_64")] + pub fn get_debugregs(&self) -> io::Result<kvm_sys::kvm_debugregs> { + let mut dregs = kvm_sys::kvm_debugregs::default(); + ioctl( + &self.vcpu, + kvm_sys::KVM_GET_DEBUGREGS, + &mut dregs as *mut _ as u64, + )?; + Ok(dregs) + } + + /// Set the debug registers with the [`KVM_SET_DEBUGREGS`][kvm-set-debugregs] ioctl in form of + /// [`kvm_debugregs`](crate::kvm_sys::kvm_debugregs). + /// + /// [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, + kvm_sys::KVM_SET_DEBUGREGS, + &dregs as *const _ as u64, + ) + .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). /// @@ -128,6 +188,12 @@ impl Vcpu { _ => unreachable!(), } } + kvm_sys::KVM_EXIT_DEBUG => { + // Safe to use union `debug` field, as Kernel instructed us to. + let debug = unsafe { kvm_run.inner.debug }; + + Ok(KvmExit::Debug(debug.pc)) + } r @ _ => { todo!("KVM_EXIT_... (exit_reason={}) not implemented!", r) } |