1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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({}) T({}) 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_tf(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,
        )
    }
}