From 623f731b440e604e0d90fc764e9a228ada09f473 Mon Sep 17 00:00:00 2001 From: johannst Date: Tue, 1 Jun 2021 22:59:17 +0200 Subject: added fmt::Display impl for kvm register structs --- src/fmt.rs | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 130 insertions(+) create mode 100644 src/fmt.rs (limited to 'src') diff --git a/src/fmt.rs b/src/fmt.rs new file mode 100644 index 0000000..9ae8689 --- /dev/null +++ b/src/fmt.rs @@ -0,0 +1,129 @@ +use std::fmt; + +use crate::kvm_sys::{kvm_dtable, kvm_regs, kvm_segment, kvm_sregs}; +use crate::x86_64::*; + +impl fmt::Display for kvm_regs { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "rax: {:#018x} rbx: {:#018x} rcx: {:#018x} rdx: {:#018x}\n\ + rsi: {:#018x} rdi: {:#018x}\n\ + 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({})", + self.rax, + self.rbx, + self.rcx, + self.rdx, + self.rsi, + self.rdi, + self.r8, + self.r9, + self.r10, + self.r11, + self.r12, + self.r13, + self.r14, + self.r15, + self.rsp, + self.rbp, + self.rip, + self.rflags, + rflags_of(self.rflags), + rflags_df(self.rflags), + rflags_if(self.rflags), + rflags_sf(self.rflags), + rflags_zf(self.rflags), + rflags_pf(self.rflags), + rflags_cf(self.rflags), + ) + } +} + +impl fmt::Display for kvm_segment { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.present == 0 { + write!(f, "{:#04x} P(0)", self.selector) + } else { + write!( + f, + "{:#06x} T({}) RPL({}) BASE({:#010x}) LIMIT({:#07x}) P(1) S({}) DPL({}) DB({}) L({}) TYPE({})", + self.selector, + if seg_selector_ti(self.selector) == 0 { + "GDT" + } else { + "LDT" + }, + seg_selector_rpl(self.selector), + self.base, + self.limit, + self.s, + self.dpl, + self.db, + self.l, + match self.type_ { + 0 => "D:R---", + 1 => "D:R-A-", + 2 => "D:RW--", + 3 => "D:RWA-", + 4 => "D:R--E", + 5 => "D:R-AE", + 6 => "D:RW-E", + 7 => "D:RWAE", + 8 => "C:X---", + 9 => "C:X-A-", + 10 => "C:XR--", + 11 => "C:XRA-", + 12 => "C:X--C", + 13 => "C:X-AC", + 14 => "C:XR-C", + 15 => "C:XRAC", + _ => unreachable!(), + } + ) + } + } +} + +impl fmt::Display for kvm_dtable { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "BASE({:#018x}) LIMIT({:#07x})", self.base, self.limit) + } +} + +impl fmt::Display for kvm_sregs { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "cs : {}\n\ + ds : {}\n\ + es : {}\n\ + fs : {}\n\ + gs : {}\n\ + ss : {}\n\ + tr : {}\n\ + ldt : {}\n\ + gdt : {}\n\ + idt : {}\n\ + cr0 : {:#018x} cr2: {:#018x} cr3: {:#018x} cr4: {:#018x}\n\ + efer: {:#018x}", + self.cs, + self.ds, + self.es, + self.fs, + self.gs, + self.ss, + self.tr, + self.ldt, + self.gdt, + self.idt, + self.cr0, + self.cr2, + self.cr3, + self.cr4, + self.efer, + ) + } +} diff --git a/src/lib.rs b/src/lib.rs index d31d33e..69be3c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ use std::io; use std::ops; use std::os::unix::io::AsRawFd; +pub mod fmt; pub mod kvm; pub mod kvm_sys; pub mod vcpu; -- cgit v1.2.3