aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/kvm_sys.rs
blob: d21da47878a4b17f143fbcb7cbc54292fd390ba7 (plain) (blame)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Definitions of the system header [`<linux/kvm.h>`][kvm-h].
//!
//! [kvm-h]: https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/kvm.h

#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(dead_code)]

// Generated by `build.rs`.
include!(concat!(env!("OUT_DIR"), "/kvm_constants.rs"));

#[repr(C)]
#[derive(Default, Debug)]
pub struct kvm_regs {
    pub rax: u64,
    pub rbx: u64,
    pub rcx: u64,
    pub rdx: u64,
    pub rsi: u64,
    pub rdi: u64,
    pub rsp: u64,
    pub rbp: u64,
    pub r8: u64,
    pub r9: u64,
    pub r10: u64,
    pub r11: u64,
    pub r12: u64,
    pub r13: u64,
    pub r14: u64,
    pub r15: u64,
    pub rip: u64,
    pub rflags: u64,
}

#[repr(C)]
#[derive(Default, Debug)]
pub struct kvm_segment {
    pub base: u64,
    pub limit: u32,
    pub selector: u16,
    pub type_: u8,
    pub present: u8,
    pub dpl: u8,
    pub db: u8,
    pub s: u8,
    pub l: u8,
    pub g: u8,
    pub avl: u8,
    unusable: u8,
    _padding: u8,
}

#[repr(C)]
#[derive(Default, Debug)]
pub struct kvm_dtable {
    pub base: u64,
    pub limit: u16,
    _padding: [u16; 3],
}

#[repr(C)]
#[derive(Default, Debug)]
pub struct kvm_sregs {
    pub cs: kvm_segment,
    pub ds: kvm_segment,
    pub es: kvm_segment,
    pub fs: kvm_segment,
    pub gs: kvm_segment,
    pub ss: kvm_segment,
    pub tr: kvm_segment,
    pub ldt: kvm_segment,
    pub gdt: kvm_dtable,
    pub idt: kvm_dtable,
    pub cr0: u64,
    pub cr2: u64,
    pub cr3: u64,
    pub cr4: u64,
    pub cr8: u64,
    pub efer: u64,
    pub apic_base: u64,
    pub interrupt_bitmap: [u64; 4],
}

#[repr(C)]
#[derive(Default, Debug)]
pub(crate) struct kvm_userspace_memory_region {
    pub slot: u32,
    pub flags: u32,
    pub guest_phys_addr: u64,
    pub memory_size: u64,
    pub userspace_addr: u64,
}

#[repr(C)]
pub(crate) struct kvm_run {
    request_interrupt_window: u8,
    immediate_exit: u8,
    padding1: [u8; 6],
    pub exit_reason: u32,
    ready_for_interrupt_injection: u8,
    if_flag: u8,
    flags: u16,
    cr8: u64,
    apic_base: u64,
    pub inner: kvm_run_union,
    kvm_valid_regs: u64,
    kvm_dirty_regs: u64,
    s: kvm_run_union_s,
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub(crate) struct kvm_run_io {
    pub direction: u8,
    pub size: u8,
    pub port: u16,
    pub count: u32,
    pub data_offset: u64,
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub(crate) struct kvm_run_mmio {
    pub phys_addr: u64,
    pub data: [u8; 8],
    pub len: u32,
    pub is_write: u8,
}

// 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,
    padding: [u8; 256],
}

// Only add the union fields used here.
#[repr(C)]
union kvm_run_union_s {
    padding: [u8; 2048],
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::mem;

    #[test]
    fn check_kvm_regs() {
        assert_eq!(mem::size_of::<kvm_regs>(), TEST_KVM_REGS_SIZE);
        assert_eq!(mem::align_of::<kvm_regs>(), TEST_KVM_REGS_ALIGN);
    }

    #[test]
    fn check_kvm_segment() {
        assert_eq!(mem::size_of::<kvm_segment>(), TEST_KVM_SEGMENT_SIZE);
        assert_eq!(mem::align_of::<kvm_segment>(), TEST_KVM_SEGMENT_ALIGN);
    }

    #[test]
    fn check_kvm_dtable() {
        assert_eq!(mem::size_of::<kvm_dtable>(), TEST_KVM_DTABLE_SIZE);
        assert_eq!(mem::align_of::<kvm_dtable>(), TEST_KVM_DTABLE_ALIGN);
    }

    #[test]
    fn check_kvm_sregs() {
        assert_eq!(mem::size_of::<kvm_sregs>(), TEST_KVM_SREGS_SIZE);
        assert_eq!(mem::align_of::<kvm_sregs>(), TEST_KVM_SREGS_ALIGN);
        assert_eq!(
            mem::size_of_val(&kvm_sregs::default().interrupt_bitmap),
            TEST_KVM_SREGS_INTERRTUP_BITMAP_SIZE
        );
    }

    #[test]
    fn check_kvm_userspace_memory_region() {
        assert_eq!(
            mem::size_of::<kvm_userspace_memory_region>(),
            TEST_KVM_USERSPACE_MEMORY_REGION_SIZE
        );
        assert_eq!(
            mem::align_of::<kvm_userspace_memory_region>(),
            TEST_KVM_USERSPACE_MEMORY_REGION_ALIGN
        );
    }

    #[test]
    fn check_kvm_run() {
        assert_eq!(mem::size_of::<kvm_run>(), TEST_KVM_RUN_SIZE);
        assert_eq!(mem::align_of::<kvm_run>(), TEST_KVM_RUN_ALIGN);
        assert_eq!(mem::size_of::<kvm_run_io>(), TEST_KVM_RUN_IO_SIZE);
        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);
    }
}