4 #include <geekos/ktypes.h>
7 #define VMCB_CTRL_AREA_OFFSET 0x0
8 #define VMCB_STATE_SAVE_AREA_OFFSET 0x400
11 #define GET_VMCB_CTRL_AREA(page) (page + VMCB_CTRL_AREA_OFFSET)
12 #define GET_VMCB_SAVE_STATE_AREA(page) (page + VMCB_STATE_SAVE_AREA_OFFSET)
18 #define PACKED __attribute__((packed))
25 union Ctrl_Registers {
26 ushort_t bitmap PACKED;
28 uint_t cr0 : 1 PACKED;
29 uint_t cr1 : 1 PACKED;
30 uint_t cr2 : 1 PACKED;
31 uint_t cr3 : 1 PACKED;
32 uint_t cr4 : 1 PACKED;
33 uint_t cr5 : 1 PACKED;
34 uint_t cr6 : 1 PACKED;
35 uint_t cr7 : 1 PACKED;
36 uint_t cr8 : 1 PACKED;
37 uint_t cr9 : 1 PACKED;
38 uint_t cr10 : 1 PACKED;
39 uint_t cr11 : 1 PACKED;
40 uint_t cr12 : 1 PACKED;
41 uint_t cr13 : 1 PACKED;
42 uint_t cr14 : 1 PACKED;
43 uint_t cr15 : 1 PACKED;
48 union Debug_Registers {
49 ushort_t bitmap PACKED;
51 uint_t dr0 : 1 PACKED;
52 uint_t dr1 : 1 PACKED;
53 uint_t dr2 : 1 PACKED;
54 uint_t dr3 : 1 PACKED;
55 uint_t dr4 : 1 PACKED;
56 uint_t dr5 : 1 PACKED;
57 uint_t dr6 : 1 PACKED;
58 uint_t dr7 : 1 PACKED;
59 uint_t dr8 : 1 PACKED;
60 uint_t dr9 : 1 PACKED;
61 uint_t dr10 : 1 PACKED;
62 uint_t dr11 : 1 PACKED;
63 uint_t dr12 : 1 PACKED;
64 uint_t dr13 : 1 PACKED;
65 uint_t dr14 : 1 PACKED;
66 uint_t dr15 : 1 PACKED;
71 union Exception_Vectors {
74 uint_t ex0 : 1 PACKED;
75 uint_t ex1 : 1 PACKED;
76 uint_t ex2 : 1 PACKED;
77 uint_t ex3 : 1 PACKED;
78 uint_t ex4 : 1 PACKED;
79 uint_t ex5 : 1 PACKED;
80 uint_t ex6 : 1 PACKED;
81 uint_t ex7 : 1 PACKED;
82 uint_t ex8 : 1 PACKED;
83 uint_t ex9 : 1 PACKED;
84 uint_t ex10 : 1 PACKED;
85 uint_t ex11 : 1 PACKED;
86 uint_t ex12 : 1 PACKED;
87 uint_t ex13 : 1 PACKED;
88 uint_t ex14 : 1 PACKED;
89 uint_t ex15 : 1 PACKED;
90 uint_t ex16 : 1 PACKED;
91 uint_t ex17 : 1 PACKED;
92 uint_t ex18 : 1 PACKED;
93 uint_t ex19 : 1 PACKED;
94 uint_t ex20 : 1 PACKED;
95 uint_t ex21 : 1 PACKED;
96 uint_t ex22 : 1 PACKED;
97 uint_t ex23 : 1 PACKED;
98 uint_t ex24 : 1 PACKED;
99 uint_t ex25 : 1 PACKED;
100 uint_t ex26 : 1 PACKED;
101 uint_t ex27 : 1 PACKED;
102 uint_t ex28 : 1 PACKED;
103 uint_t ex29 : 1 PACKED;
104 uint_t ex30 : 1 PACKED;
105 uint_t ex31 : 1 PACKED;
108 uint_t de : 1 PACKED; // divide by zero
109 uint_t db : 1 PACKED; // Debug
110 uint_t nmi : 1 PACKED; // Non-maskable interrupt
111 uint_t bp : 1 PACKED; // Breakpoint
112 uint_t of : 1 PACKED; // Overflow
113 uint_t br : 1 PACKED; // Bound-Range
114 uint_t ud : 1 PACKED; // Invalid-Opcode
115 uint_t nm : 1 PACKED; // Device-not-available
116 uint_t df : 1 PACKED; // Double Fault
117 uint_t ex9 : 1 PACKED;
118 uint_t ts : 1 PACKED; // Invalid TSS
119 uint_t np : 1 PACKED; // Segment-not-present
120 uint_t ss : 1 PACKED; // Stack
121 uint_t gp : 1 PACKED; // General Protection Fault
122 uint_t pf : 1 PACKED; // Page fault
123 uint_t ex15 : 1 PACKED;
124 uint_t mf : 1 PACKED; // Floating point exception
125 uint_t ac : 1 PACKED; // Alignment-check
126 uint_t mc : 1 PACKED; // Machine Check
127 uint_t xf : 1 PACKED; // SIMD floating-point
128 uint_t ex20 : 1 PACKED;
129 uint_t ex21 : 1 PACKED;
130 uint_t ex22 : 1 PACKED;
131 uint_t ex23 : 1 PACKED;
132 uint_t ex24 : 1 PACKED;
133 uint_t ex25 : 1 PACKED;
134 uint_t ex26 : 1 PACKED;
135 uint_t ex27 : 1 PACKED;
136 uint_t ex28 : 1 PACKED;
137 uint_t ex29 : 1 PACKED;
138 uint_t sx : 1 PACKED; // Security Exception
139 uint_t ex31 : 1 PACKED;
144 union Instr_Intercepts {
145 uint_t bitmap PACKED;
147 uint_t INTR : 1 PACKED;
148 uint_t NMI : 1 PACKED;
149 uint_t SMI : 1 PACKED;
150 uint_t INIT : 1 PACKED;
151 uint_t VINTR : 1 PACKED;
152 uint_t CR0 : 1 PACKED;
153 uint_t RD_IDTR : 1 PACKED;
154 uint_t RD_GDTR : 1 PACKED;
155 uint_t RD_LDTR : 1 PACKED;
156 uint_t RD_TR : 1 PACKED;
157 uint_t WR_IDTR : 1 PACKED;
158 uint_t WR_GDTR : 1 PACKED;
159 uint_t WR_LDTR : 1 PACKED;
160 uint_t WR_TR : 1 PACKED;
161 uint_t RDTSC : 1 PACKED;
162 uint_t RDPMC : 1 PACKED;
163 uint_t PUSHF : 1 PACKED;
164 uint_t POPF : 1 PACKED;
165 uint_t CPUID : 1 PACKED;
166 uint_t RSM : 1 PACKED;
167 uint_t IRET : 1 PACKED;
168 uint_t INTn : 1 PACKED;
169 uint_t INVD : 1 PACKED;
170 uint_t PAUSE : 1 PACKED;
171 uint_t HLT : 1 PACKED;
172 uint_t INVPLG : 1 PACKED;
173 uint_t INVPLGA : 1 PACKED;
174 uint_t IOIO_PROT : 1 PACKED;
175 uint_t MSR_PROT : 1 PACKED;
176 uint_t task_switch : 1 PACKED;
177 uint_t FERR_FREEZE : 1 PACKED;
178 uint_t shutdown_evts: 1 PACKED;
182 union SVM_Instr_Intercepts {
183 uint_t bitmap PACKED;
185 uint_t VMRUN : 1 PACKED;
186 uint_t VMMCALL : 1 PACKED;
187 uint_t VMLOAD : 1 PACKED;
188 uint_t VMSAVE : 1 PACKED;
189 uint_t STGI : 1 PACKED;
190 uint_t CLGI : 1 PACKED;
191 uint_t SKINIT : 1 PACKED;
192 uint_t RDTSCP : 1 PACKED;
193 uint_t ICEBP : 1 PACKED;
194 uint_t WBINVD : 1 PACKED;
195 uint_t MONITOR : 1 PACKED;
196 uint_t MWAIT_always : 1 PACKED;
197 uint_t MWAIT_if_armed : 1 PACKED;
198 uint_t reserved : 19 PACKED; // Should be 0
203 union Guest_Control {
204 uint_t bitmap PACKED;
206 uchar_t V_TPR PACKED;
207 uint_t V_IRQ : 1 PACKED;
208 uint_t rsvd1 : 7 PACKED; // Should be 0
209 uint_t V_INTR_PRIO : 4 PACKED;
210 uint_t V_IGN_TPR : 1 PACKED;
211 uint_t rsvd2 : 3 PACKED; // Should be 0
212 uint_t V_INTR_MASKING : 1 PACKED;
213 uint_t rsvd3 : 7 PACKED; // Should be 0
214 uchar_t V_INTR_VECTOR PACKED;
215 uint_t rsvd4 : 24 PACKED; // Should be 0
221 typedef struct VMCB_Control_Area {
223 union Ctrl_Registers cr_reads PACKED;
224 union Ctrl_Registers cr_writes PACKED;
225 union Debug_Registers dr_reads PACKED;
226 union Debug_Registers dr_writes PACKED;
227 union Exception_Vectors exceptions PACKED;
228 union Instr_Intercepts instrs PACKED;
229 union SVM_Instr_Intercepts svm_instrs PACKED;
231 uchar_t rsvd1[44] PACKED; // Should be 0
234 ullong_t IOPM_BASE_PA PACKED;
235 ullong_t MSRPM_BASE_PA PACKED;
236 ullong_t TSC_OFFSET PACKED;
238 uint_t guest_ASID PACKED;
239 uchar_t TLB_CONTROL PACKED;
241 uchar_t rsvd2[3] PACKED; // Should be 0
243 union Guest_Control guest_ctrl PACKED;
245 uint_t interrupt_shadow : 1 PACKED;
246 uint_t rsvd3 : 31 PACKED; // Should be 0
247 uint_t rsvd4 PACKED; // Should be 0
249 ullong_t exit_code PACKED;
250 ullong_t exit_info1 PACKED;
251 ullong_t exit_info2 PACKED;
253 /* This could be a typo in the manual....
254 * It doesn't actually say that there is a reserved bit
255 * But it does say that the EXITINTINFO field is in bits 63-1
256 * ALL other occurances mention a 1 bit reserved field
258 uint_t rsvd5 : 1 PACKED;
259 ullong_t exit_int_info : 63 PACKED;
262 // uint_t NP_ENABLE : 1 PACKED;
263 //ullong_t rsvd6 : 63 PACKED; // Should be 0
264 ullong_t NP_ENABLE PACKED;
266 uchar_t rsvd7[16] PACKED; // Should be 0
269 ullong_t EVENTINJ PACKED;
272 /* This could be a typo in the manual....
273 * It doesn't actually say that there is a reserved bit
274 * But it does say that the EXITINTINFO field is in bits 63-1
275 * ALL other occurances mention a 1 bit reserved field
277 // uint_t rsvd8 : 1 PACKED;
278 //ullong_t N_CR3 : 63 PACKED;
279 ullong_t N_CR3 PACKED;
283 uint_t LBR_VIRTUALIZATION_ENABLE : 1 PACKED;
284 ullong_t rsvd9 : 63 PACKED; // Should be 0
293 struct vmcb_selector {
294 ushort_t selector PACKED;
296 /* These attributes are basically a direct map of the attribute fields of a segment desc.
297 * The segment limit in the middle is removed and the fields are pused together
298 * There IS empty space at the end... See AMD Arch vol3, sect. 4.7.1, pg 78
303 uint_t type : 4 PACKED; // segment type, [see Intel vol. 3b, sect. 3.4.5.1 (because I have the books)]
304 uint_t S : 1 PACKED; // System=0, code/data=1
305 uint_t dpl : 2 PACKED; // priviledge level, corresonds to protection ring
306 uint_t P : 1 PACKED; // present flag
307 uint_t avl : 1 PACKED; // available for use by system software
308 uint_t L : 1 PACKED; // long mode (64 bit?)
309 uint_t db : 1 PACKED; // default op size (0=16 bit seg, 1=32 bit seg)
310 uint_t G : 1 PACKED; // Granularity, (0=bytes, 1=4k)
314 ullong_t base PACKED;
318 typedef struct VMCB_State_Save_Area {
319 struct vmcb_selector es PACKED; // only lower 32 bits of base are implemented
320 struct vmcb_selector cs PACKED; // only lower 32 bits of base are implemented
321 struct vmcb_selector ss PACKED; // only lower 32 bits of base are implemented
322 struct vmcb_selector ds PACKED; // only lower 32 bits of base are implemented
323 struct vmcb_selector fs PACKED;
324 struct vmcb_selector gs PACKED;
326 struct vmcb_selector gdtr PACKED; // selector+attrib are reserved, only lower 16 bits of limit are implemented
327 struct vmcb_selector ldtr PACKED;
328 struct vmcb_selector idtr PACKED; // selector+attrib are reserved, only lower 16 bits of limit are implemented
329 struct vmcb_selector tr PACKED;
331 uchar_t rsvd1[43] PACKED;
334 uchar_t cpl PACKED; // if the guest is real-mode then the CPL is forced to 0
335 // if the guest is virtual-mode then the CPL is forced to 3
340 ullong_t efer PACKED;
342 uchar_t rsvd3[112] PACKED;
350 ullong_t rflags PACKED;
353 uchar_t rsvd4[88] PACKED;
358 uchar_t rsvd5[24] PACKED;
362 ullong_t star PACKED;
363 ullong_t lstar PACKED;
364 ullong_t cstar PACKED;
365 ullong_t sfmask PACKED;
366 ullong_t KernelGsBase PACKED;
367 ullong_t sysenter_cs PACKED;
368 ullong_t sysenter_esp PACKED;
369 ullong_t sysenter_eip PACKED;
373 uchar_t rsvd6[32] PACKED;
376 ullong_t g_pat PACKED; // Guest PAT
377 // -- only used if nested paging is enabled
378 ullong_t dbgctl PACKED; // Guest DBGCTL MSR
379 // -- only used if the LBR registers are virtualized
380 ullong_t br_from PACKED; // Guest LastBranchFromIP MSR
381 // -- only used if the LBR registers are virtualized
382 ullong_t br_to PACKED; // Guest LastBranchToIP MSR
383 // -- only used if the LBR registers are virtualized
384 ullong_t lastexcpfrom PACKED; // Guest LastExceptionFromIP MSR
385 // -- only used if the LBR registers are virtualized
386 ullong_t lastexcpto PACKED; // Guest LastExceptionToIP MSR
387 // -- only used if the LBR registers are virtualized
389 } vmcb_saved_state_t;
391 void PrintDebugVMCB(vmcb_t * vmcb);