aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/vcpu.rs
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2021-12-11 22:06:22 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2021-12-11 22:06:22 +0100
commit3b9f34189259c94a8fbd26bc16cd28d167282e35 (patch)
treef4086f3efb98dc6059000782eed094a7e8bda2e7 /src/vcpu.rs
parent58be7ebbf86c4185276250867eb74a6dd19aaca6 (diff)
downloadmini-kvm-rs-3b9f34189259c94a8fbd26bc16cd28d167282e35.tar.gz
mini-kvm-rs-3b9f34189259c94a8fbd26bc16cd28d167282e35.zip
vcpu: allow to enable/disable guest single stepping (debug)
Diffstat (limited to 'src/vcpu.rs')
-rw-r--r--src/vcpu.rs29
1 files changed, 29 insertions, 0 deletions
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).
///