Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


code clean up
[palacios.git] / palacios / include / palacios / vmcb.h
1 #ifndef __VMCB_H
2 #define __VMCB_H
3
4 #ifdef __V3VEE__
5
6 #include <palacios/vmm_types.h>
7 #include <palacios/vm_guest.h>
8
9 #define VMCB_CTRL_AREA_OFFSET                   0x0
10 #define VMCB_STATE_SAVE_AREA_OFFSET             0x400
11
12
13 #define GET_VMCB_CTRL_AREA(page)         (page + VMCB_CTRL_AREA_OFFSET)
14 #define GET_VMCB_SAVE_STATE_AREA(page)   (page + VMCB_STATE_SAVE_AREA_OFFSET)
15
16
17 #if __TINYC__
18 #define PACKED
19 #else
20 #define PACKED __attribute__((packed))
21 #endif
22
23
24 typedef void vmcb_t;
25
26
27 struct Ctrl_Registers {
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;
44 };
45
46
47 struct Debug_Registers {
48     uint_t dr0        : 1        PACKED;
49     uint_t dr1        : 1        PACKED;
50     uint_t dr2        : 1        PACKED;
51     uint_t dr3        : 1        PACKED;
52     uint_t dr4        : 1        PACKED;
53     uint_t dr5        : 1        PACKED;
54     uint_t dr6        : 1        PACKED;
55     uint_t dr7        : 1        PACKED;
56     uint_t dr8        : 1        PACKED;
57     uint_t dr9        : 1        PACKED;
58     uint_t dr10       : 1        PACKED;
59     uint_t dr11       : 1        PACKED;
60     uint_t dr12       : 1        PACKED;
61     uint_t dr13       : 1        PACKED;
62     uint_t dr14       : 1        PACKED;
63     uint_t dr15       : 1        PACKED;
64 };
65
66
67 struct Exception_Vectors {
68     uint_t de          : 1        PACKED; // (0) divide by zero
69     uint_t db          : 1        PACKED; // (1) Debug
70     uint_t nmi         : 1        PACKED; // (2) Non-maskable interrupt
71     uint_t bp          : 1        PACKED; // (3) Breakpoint
72     uint_t of          : 1        PACKED; // (4) Overflow
73     uint_t br          : 1        PACKED; // (5) Bound-Range
74     uint_t ud          : 1        PACKED; // (6) Invalid-Opcode
75     uint_t nm          : 1        PACKED; // (7) Device-not-available
76     uint_t df          : 1        PACKED; // (8) Double Fault
77     uint_t ex9         : 1        PACKED; 
78     uint_t ts          : 1        PACKED; // (10) Invalid TSS
79     uint_t np          : 1        PACKED; // (11) Segment-not-present
80     uint_t ss          : 1        PACKED; // (12) Stack
81     uint_t gp          : 1        PACKED; // (13) General Protection Fault
82     uint_t pf          : 1        PACKED; // (14) Page fault
83     uint_t ex15        : 1        PACKED;
84     uint_t mf          : 1        PACKED; // (15) Floating point exception
85     uint_t ac          : 1        PACKED; // (16) Alignment-check
86     uint_t mc          : 1        PACKED; // (17) Machine Check
87     uint_t xf          : 1        PACKED; // (18) SIMD floating-point
88     uint_t ex20        : 1        PACKED;
89     uint_t ex21        : 1        PACKED;
90     uint_t ex22        : 1        PACKED;
91     uint_t ex23        : 1        PACKED;
92     uint_t ex24        : 1        PACKED;
93     uint_t ex25        : 1        PACKED;
94     uint_t ex26        : 1        PACKED;
95     uint_t ex27        : 1        PACKED;
96     uint_t ex28        : 1        PACKED;
97     uint_t ex29        : 1        PACKED;
98     uint_t sx          : 1        PACKED; // (30) Security Exception
99     uint_t ex31        : 1        PACKED;
100 };
101
102
103 struct Instr_Intercepts {
104     uint_t INTR        : 1        PACKED;
105     uint_t NMI         : 1        PACKED;
106     uint_t SMI         : 1        PACKED;
107     uint_t INIT        : 1        PACKED;
108     uint_t VINTR       : 1        PACKED;
109     uint_t CR0         : 1        PACKED;
110     uint_t RD_IDTR     : 1        PACKED;
111     uint_t RD_GDTR     : 1        PACKED;
112     uint_t RD_LDTR     : 1        PACKED;
113     uint_t RD_TR       : 1        PACKED;
114     uint_t WR_IDTR     : 1        PACKED;
115     uint_t WR_GDTR     : 1        PACKED;
116     uint_t WR_LDTR     : 1        PACKED;
117     uint_t WR_TR       : 1        PACKED;
118     uint_t RDTSC       : 1        PACKED;
119     uint_t RDPMC       : 1        PACKED;
120     uint_t PUSHF       : 1        PACKED;
121     uint_t POPF        : 1        PACKED;
122     uint_t CPUID       : 1        PACKED;
123     uint_t RSM         : 1        PACKED;
124     uint_t IRET        : 1        PACKED;
125     uint_t INTn        : 1        PACKED;
126     uint_t INVD        : 1        PACKED;
127     uint_t PAUSE       : 1        PACKED;
128     uint_t HLT         : 1        PACKED;
129     uint_t INVLPG      : 1        PACKED;
130     uint_t INVLPGA     : 1        PACKED;
131     uint_t IOIO_PROT   : 1        PACKED;
132     uint_t MSR_PROT    : 1        PACKED;
133     uint_t task_switch : 1        PACKED;
134     uint_t FERR_FREEZE : 1        PACKED;
135     uint_t shutdown_evts: 1       PACKED;
136 };
137
138 struct SVM_Instr_Intercepts { 
139   uint_t VMRUN      : 1         PACKED;
140   uint_t VMMCALL    : 1         PACKED;
141   uint_t VMLOAD     : 1         PACKED;
142   uint_t VMSAVE     : 1         PACKED;
143   uint_t STGI       : 1         PACKED;
144   uint_t CLGI       : 1         PACKED;
145   uint_t SKINIT     : 1         PACKED;
146   uint_t RDTSCP     : 1         PACKED;
147   uint_t ICEBP      : 1         PACKED;
148   uint_t WBINVD     : 1         PACKED;
149   uint_t MONITOR    : 1         PACKED;
150   uint_t MWAIT_always : 1       PACKED;
151   uint_t MWAIT_if_armed : 1     PACKED;
152   uint_t reserved  : 19         PACKED;  // Should be 0
153 };
154
155
156 struct Guest_Control {
157   uchar_t V_TPR                 PACKED;
158   uint_t V_IRQ      : 1         PACKED;
159   uint_t rsvd1      : 7         PACKED;  // Should be 0
160   uint_t V_INTR_PRIO : 4        PACKED;
161   uint_t V_IGN_TPR  : 1         PACKED;
162   uint_t rsvd2      : 3         PACKED;  // Should be 0
163   uint_t V_INTR_MASKING : 1     PACKED;
164   uint_t rsvd3      : 7         PACKED;  // Should be 0
165   uchar_t V_INTR_VECTOR         PACKED;
166   uint_t rsvd4      : 24        PACKED;  // Should be 0
167 };
168
169 #define SVM_INJECTION_EXTERNAL_INTR 0
170 #define SVM_INJECTION_VIRTUAL_INTR  0
171 #define SVM_INJECTION_NMI           2
172 #define SVM_INJECTION_EXCEPTION     3
173 #define SVM_INJECTION_SOFT_INTR     4
174
175 struct Interrupt_Info {
176   uint_t vector       : 8         PACKED;
177   uint_t type         : 3         PACKED;
178   uint_t ev           : 1         PACKED;
179   uint_t rsvd         : 19        PACKED;
180   uint_t valid        : 1         PACKED;
181   uint_t error_code   : 32        PACKED;
182 };
183
184
185 typedef struct VMCB_Control_Area {
186   // offset 0x0
187   struct Ctrl_Registers cr_reads         PACKED;
188   struct Ctrl_Registers cr_writes        PACKED;
189   struct Debug_Registers dr_reads        PACKED;
190   struct Debug_Registers dr_writes       PACKED;
191   struct Exception_Vectors exceptions    PACKED;
192   struct Instr_Intercepts instrs         PACKED;
193   struct SVM_Instr_Intercepts svm_instrs PACKED;
194
195   uchar_t rsvd1[44]                     PACKED;  // Should be 0
196
197   // offset 0x040
198   ullong_t IOPM_BASE_PA                 PACKED;
199   ullong_t MSRPM_BASE_PA                PACKED;
200   ullong_t TSC_OFFSET                   PACKED;
201
202   uint_t guest_ASID                     PACKED;
203   uchar_t TLB_CONTROL                   PACKED;
204
205   uchar_t rsvd2[3]                      PACKED;  // Should be 0
206
207   struct Guest_Control guest_ctrl        PACKED;
208   
209   uint_t interrupt_shadow  : 1          PACKED;
210   uint_t rsvd3             : 31         PACKED;  // Should be 0
211   uint_t rsvd4                          PACKED;  // Should be 0
212
213   ullong_t exit_code                    PACKED;
214   ullong_t exit_info1                   PACKED;
215   ullong_t exit_info2                   PACKED;
216
217   /* This could be a typo in the manual....
218    * It doesn't actually say that there is a reserved bit
219    * But it does say that the EXITINTINFO field is in bits 63-1
220    * ALL other occurances mention a 1 bit reserved field
221    */
222   //  uint_t rsvd5             : 1          PACKED;
223   //ullong_t exit_int_info   : 63         PACKED;
224   /* ** */
225
226   // AMD Manual 2, pg 391, sect: 15.19
227   struct Interrupt_Info exit_int_info  PACKED;
228
229   //  uint_t NP_ENABLE         : 1          PACKED;
230   //ullong_t rsvd6           : 63         PACKED;  // Should be 0 
231   ullong_t NP_ENABLE                    PACKED;
232
233   uchar_t rsvd7[16]                     PACKED;  // Should be 0
234
235   // Offset 0xA8
236   struct Interrupt_Info EVENTINJ                     PACKED;
237
238
239   /* This could be a typo in the manual....
240    * It doesn't actually say that there is a reserved bit
241    * But it does say that the EXITINTINFO field is in bits 63-1
242    * ALL other occurances mention a 1 bit reserved field
243    */
244   //  uint_t rsvd8              : 1         PACKED;
245   //ullong_t N_CR3            : 63        PACKED;
246   ullong_t N_CR3                        PACKED;
247   /* ** */
248
249
250   uint_t LBR_VIRTUALIZATION_ENABLE : 1  PACKED;
251   ullong_t rsvd9            : 63        PACKED;   // Should be 0
252
253 } vmcb_ctrl_t;
254
255
256
257
258
259
260 struct vmcb_selector {
261   ushort_t selector                   PACKED;
262
263   /* These attributes are basically a direct map of the attribute fields of a segment desc.
264    * The segment limit in the middle is removed and the fields are fused together
265    * There IS empty space at the end... See AMD Arch vol3, sect. 4.7.1,  pg 78
266    */
267   union {
268     ushort_t raw                      PACKED;
269     struct {
270       uint_t type              : 4    PACKED; // segment type, [see Intel vol. 3b, sect. 3.4.5.1 (because I have the books)]
271       uint_t S                 : 1    PACKED; // System=0, code/data=1
272       uint_t dpl               : 2    PACKED; // priviledge level, corresonds to protection ring
273       uint_t P                 : 1    PACKED; // present flag
274       uint_t avl               : 1    PACKED; // available for use by system software
275       uint_t L                 : 1    PACKED; // long mode (64 bit?)
276       uint_t db                : 1    PACKED; // default op size (0=16 bit seg, 1=32 bit seg)
277       uint_t G                 : 1    PACKED; // Granularity, (0=bytes, 1=4k)
278       uint_t rsvd              : 4    PACKED;
279     } fields;
280   } attrib;
281   uint_t  limit                       PACKED;
282   ullong_t base                       PACKED;
283 };
284
285
286 typedef struct VMCB_State_Save_Area {
287   struct vmcb_selector es            PACKED; // only lower 32 bits of base are implemented
288   struct vmcb_selector cs            PACKED; // only lower 32 bits of base are implemented
289   struct vmcb_selector ss            PACKED; // only lower 32 bits of base are implemented
290   struct vmcb_selector ds            PACKED; // only lower 32 bits of base are implemented
291   struct vmcb_selector fs            PACKED; 
292   struct vmcb_selector gs            PACKED; 
293
294   struct vmcb_selector gdtr          PACKED; // selector+attrib are reserved, only lower 16 bits of limit are implemented
295   struct vmcb_selector ldtr          PACKED; 
296   struct vmcb_selector idtr          PACKED; // selector+attrib are reserved, only lower 16 bits of limit are implemented
297   struct vmcb_selector tr            PACKED; 
298
299   uchar_t rsvd1[43]                  PACKED;
300
301   //offset 0x0cb
302   uchar_t cpl                        PACKED; // if the guest is real-mode then the CPL is forced to 0
303                                              // if the guest is virtual-mode then the CPL is forced to 3
304
305   uint_t rsvd2                       PACKED;
306
307   // offset 0x0d0
308   ullong_t efer                      PACKED;
309
310   uchar_t rsvd3[112]                 PACKED;
311   
312   //offset 0x148
313   ullong_t cr4                       PACKED;
314   ullong_t cr3                       PACKED;
315   ullong_t cr0                       PACKED;
316   ullong_t dr7                       PACKED;
317   ullong_t dr6                       PACKED;
318   ullong_t rflags                    PACKED;
319   ullong_t rip                       PACKED;
320
321   uchar_t rsvd4[88]                  PACKED;
322   
323   //offset 0x1d8
324   ullong_t rsp                       PACKED;
325
326   uchar_t rsvd5[24]                  PACKED;
327
328   //offset 0x1f8
329   ullong_t rax                       PACKED;
330   ullong_t star                      PACKED;
331   ullong_t lstar                     PACKED;
332   ullong_t cstar                     PACKED;
333   ullong_t sfmask                    PACKED;
334   ullong_t KernelGsBase              PACKED;
335   ullong_t sysenter_cs               PACKED;
336   ullong_t sysenter_esp              PACKED;
337   ullong_t sysenter_eip              PACKED;
338   ullong_t cr2                       PACKED;
339
340
341   uchar_t rsvd6[32]                  PACKED;
342
343   //offset 0x268
344   ullong_t g_pat                     PACKED; // Guest PAT                     
345                                              //   -- only used if nested paging is enabled
346   ullong_t dbgctl                    PACKED; // Guest DBGCTL MSR               
347                                              //   -- only used if the LBR registers are virtualized
348   ullong_t br_from                   PACKED; // Guest LastBranchFromIP MSR
349                                              //   -- only used if the LBR registers are virtualized
350   ullong_t br_to                     PACKED; // Guest LastBranchToIP MSR   
351                                              //   -- only used if the LBR registers are virtualized
352   ullong_t lastexcpfrom              PACKED; // Guest LastExceptionFromIP MSR
353                                              //   -- only used if the LBR registers are virtualized
354   ullong_t lastexcpto                PACKED; // Guest LastExceptionToIP MSR 
355                                              //   -- only used if the LBR registers are virtualized
356
357 } vmcb_saved_state_t;
358
359 void PrintDebugVMCB(vmcb_t * vmcb);
360
361
362 void set_vmcb_segments(vmcb_t * vmcb, struct v3_segments * segs);
363 void get_vmcb_segments(vmcb_t * vmcb, struct v3_segments * segs);
364
365 #endif // ! __V3VEE__
366
367 #endif