aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fmt.rs3
-rw-r--r--src/kvm_sys.rs73
-rw-r--r--src/vcpu.rs66
-rw-r--r--src/x86_64.rs5
4 files changed, 145 insertions, 2 deletions
diff --git a/src/fmt.rs b/src/fmt.rs
index 9ae8689..24971bf 100644
--- a/src/fmt.rs
+++ b/src/fmt.rs
@@ -12,7 +12,7 @@ impl fmt::Display for kvm_regs {
r8 : {:#018x} r9 : {:#018x} r10: {:#018x} r11: {:#018x}\n\
r12: {:#018x} r13: {:#018x} r14: {:#018x} r15: {:#018x}\n\
rsp: {:#018x} rbp: {:#018x}\n\
- rip: {:#018x} rfl: {:#018x} O({}) D({}) I({}) S({}) Z({}) P({}) C({})",
+ rip: {:#018x} rfl: {:#018x} O({}) D({}) I({}) T({}) S({}) Z({}) P({}) C({})",
self.rax,
self.rbx,
self.rcx,
@@ -34,6 +34,7 @@ impl fmt::Display for kvm_regs {
rflags_of(self.rflags),
rflags_df(self.rflags),
rflags_if(self.rflags),
+ rflags_tf(self.rflags),
rflags_sf(self.rflags),
rflags_zf(self.rflags),
rflags_pf(self.rflags),
diff --git a/src/kvm_sys.rs b/src/kvm_sys.rs
index d21da47..2ee97a1 100644
--- a/src/kvm_sys.rs
+++ b/src/kvm_sys.rs
@@ -91,6 +91,32 @@ pub(crate) struct kvm_userspace_memory_region {
pub userspace_addr: u64,
}
+#[cfg(target_arch = "x86_64")]
+#[repr(C)]
+#[derive(Default, Debug)]
+pub struct kvm_debugregs {
+ pub db: [u64; 4],
+ pub dr6: u64,
+ pub dr7: u64,
+ pub flags: u64,
+ pub reserved: [u64; 9],
+}
+
+#[repr(C)]
+#[derive(Default, Debug)]
+pub(crate) struct kvm_guest_debug {
+ pub control: u32,
+ pad: u32,
+ pub arch: kvm_guest_debug_arch,
+}
+
+#[cfg(target_arch = "x86_64")]
+#[repr(C)]
+#[derive(Default, Debug)]
+pub(crate) struct kvm_guest_debug_arch {
+ pub debugreg: [u64; 8],
+}
+
#[repr(C)]
pub(crate) struct kvm_run {
request_interrupt_window: u8,
@@ -127,11 +153,23 @@ pub(crate) struct kvm_run_mmio {
pub is_write: u8,
}
+#[cfg(target_arch = "x86_64")]
+#[repr(C)]
+#[derive(Copy, Clone, Debug)]
+pub(crate) struct kvm_run_debug {
+ pub exception: u32,
+ pad: u32,
+ pub pc: u64,
+ pub dr6: u64,
+ pub dr7: u64,
+}
+
// Only add the union fields used here.
#[repr(C)]
pub(crate) union kvm_run_union {
pub io: kvm_run_io,
pub mmio: kvm_run_mmio,
+ pub debug: kvm_run_debug,
padding: [u8; 256],
}
@@ -194,4 +232,39 @@ mod tests {
assert_eq!(mem::size_of::<kvm_run_mmio>(), TEST_KVM_RUN_MMIO_SIZE);
assert_eq!(mem::size_of::<kvm_run_union_s>(), TEST_KVM_RUN_UNION_S_SIZE);
}
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn check_kvm_run_x86() {
+ assert_eq!(mem::size_of::<kvm_run_debug>(), TEST_KVM_RUN_DEBUG_SIZE);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn check_kvm_debugregs() {
+ assert_eq!(mem::size_of::<kvm_debugregs>(), TEST_KVM_DEBUGREGS_SIZE);
+ assert_eq!(mem::align_of::<kvm_debugregs>(), TEST_KVM_DEBUGREGS_ALIGN);
+ }
+
+ #[test]
+ fn check_kvm_guest_dbg() {
+ assert_eq!(mem::size_of::<kvm_guest_debug>(), TEST_KVM_GUEST_DEBUG_SIZE);
+ assert_eq!(
+ mem::align_of::<kvm_guest_debug>(),
+ TEST_KVM_GUEST_DEBUG_ALIGN
+ );
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn check_kvm_guest_dbg_arch() {
+ assert_eq!(
+ mem::size_of::<kvm_guest_debug_arch>(),
+ TEST_KVM_GUEST_DEBUG_ARCH_SIZE
+ );
+ assert_eq!(
+ mem::align_of::<kvm_guest_debug_arch>(),
+ TEST_KVM_GUEST_DEBUG_ARCH_ALIGN
+ );
+ }
}
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)
}
diff --git a/src/x86_64.rs b/src/x86_64.rs
index ac153e9..d87319c 100644
--- a/src/x86_64.rs
+++ b/src/x86_64.rs
@@ -16,6 +16,8 @@ mod x86_64 {
pub const RFLAGS_ZF: u64 = 1 << 6;
/// Sign flag.
pub const RFLAGS_SF: u64 = 1 << 7;
+ /// Trap flag.
+ pub const RFLAGS_TF: u64 = 1 << 8;
/// Sign flag.
pub const RFLAGS_IF: u64 = 1 << 9;
/// Direction flag.
@@ -32,6 +34,7 @@ mod x86_64 {
pub const fn rflags_af(r: u64) -> u64 { (r & RFLAGS_AF) >> 4 }
pub const fn rflags_zf(r: u64) -> u64 { (r & RFLAGS_ZF) >> 6 }
pub const fn rflags_sf(r: u64) -> u64 { (r & RFLAGS_SF) >> 7 }
+ pub const fn rflags_tf(r: u64) -> u64 { (r & RFLAGS_TF) >> 8 }
pub const fn rflags_if(r: u64) -> u64 { (r & RFLAGS_IF) >> 9 }
pub const fn rflags_df(r: u64) -> u64 { (r & RFLAGS_DF) >> 10 }
pub const fn rflags_of(r: u64) -> u64 { (r & RFLAGS_OF) >> 11 }
@@ -155,5 +158,5 @@ mod x86_64 {
/// Page region read/write.
///
/// If set, region reference by paging entry is writeable.
- pub const PAGE_RENTRY_RW: u64 = 1 << 1;
+ pub const PAGE_ENTRY_RW: u64 = 1 << 1;
}