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.


working instruction emulation
[palacios.releases.git] / palacios / src / palacios / vmcs.c
1 /* 
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmcs.h>
21
22
23
24 //extern char * exception_names;
25 //
26 // Ignores "HIGH" addresses - 32 bit only for now
27 //
28
29
30 #define CHK_VMCS_READ(tag, val) {if (VMCS_READ(tag, val) != 0) return -1;}
31 #define CHK_VMCS_WRITE(tag, val) {if (VMCS_WRITE(tag, val) != 0) return -1;}
32
33
34
35 int CopyOutVMCSGuestStateArea(struct VMCSGuestStateArea *p) {
36   CHK_VMCS_READ(GUEST_CR0, &(p->cr0));
37   CHK_VMCS_READ(GUEST_CR3, &(p->cr3));
38   CHK_VMCS_READ(GUEST_CR4, &(p->cr4));
39   CHK_VMCS_READ(GUEST_DR7, &(p->dr7));
40   CHK_VMCS_READ(GUEST_RSP, &(p->rsp));
41   CHK_VMCS_READ(GUEST_RIP, &(p->rip));
42   CHK_VMCS_READ(GUEST_RFLAGS, &(p->rflags));
43   CHK_VMCS_READ(VMCS_GUEST_CS_SELECTOR, &(p->cs.selector));
44   CHK_VMCS_READ(VMCS_GUEST_SS_SELECTOR, &(p->ss.selector));
45   CHK_VMCS_READ(VMCS_GUEST_DS_SELECTOR, &(p->ds.selector));
46   CHK_VMCS_READ(VMCS_GUEST_ES_SELECTOR, &(p->es.selector));
47   CHK_VMCS_READ(VMCS_GUEST_FS_SELECTOR, &(p->fs.selector));
48   CHK_VMCS_READ(VMCS_GUEST_GS_SELECTOR, &(p->gs.selector));
49   CHK_VMCS_READ(VMCS_GUEST_LDTR_SELECTOR, &(p->ldtr.selector));
50   CHK_VMCS_READ(VMCS_GUEST_TR_SELECTOR, &(p->tr.selector));
51   CHK_VMCS_READ(GUEST_CS_BASE, &(p->cs.baseAddr));
52   CHK_VMCS_READ(GUEST_SS_BASE, &(p->ss.baseAddr));
53   CHK_VMCS_READ(GUEST_DS_BASE, &(p->ds.baseAddr));
54   CHK_VMCS_READ(GUEST_ES_BASE, &(p->es.baseAddr));
55   CHK_VMCS_READ(GUEST_FS_BASE, &(p->fs.baseAddr));
56   CHK_VMCS_READ(GUEST_GS_BASE, &(p->gs.baseAddr));
57   CHK_VMCS_READ(GUEST_LDTR_BASE, &(p->ldtr.baseAddr));
58   CHK_VMCS_READ(GUEST_TR_BASE, &(p->tr.baseAddr));
59   CHK_VMCS_READ(GUEST_CS_LIMIT, &(p->cs.limit));
60   CHK_VMCS_READ(GUEST_SS_LIMIT, &(p->ss.limit));
61   CHK_VMCS_READ(GUEST_DS_LIMIT, &(p->ds.limit));
62   CHK_VMCS_READ(GUEST_ES_LIMIT, &(p->es.limit));
63   CHK_VMCS_READ(GUEST_FS_LIMIT, &(p->fs.limit));
64   CHK_VMCS_READ(GUEST_GS_LIMIT, &(p->gs.limit));
65   CHK_VMCS_READ(GUEST_LDTR_LIMIT, &(p->ldtr.limit));
66   CHK_VMCS_READ(GUEST_TR_LIMIT, &(p->tr.limit));
67   CHK_VMCS_READ(GUEST_CS_ACCESS, &(p->cs.access));
68   CHK_VMCS_READ(GUEST_SS_ACCESS, &(p->ss.access));
69   CHK_VMCS_READ(GUEST_DS_ACCESS, &(p->ds.access));
70   CHK_VMCS_READ(GUEST_ES_ACCESS, &(p->es.access));
71   CHK_VMCS_READ(GUEST_FS_ACCESS, &(p->fs.access));
72   CHK_VMCS_READ(GUEST_GS_ACCESS, &(p->gs.access));
73   CHK_VMCS_READ(GUEST_LDTR_ACCESS, &(p->ldtr.access));
74   CHK_VMCS_READ(GUEST_TR_ACCESS, &(p->tr.access));
75   CHK_VMCS_READ(GUEST_GDTR_BASE, &(p->gdtr.baseAddr));
76   CHK_VMCS_READ(GUEST_IDTR_BASE, &(p->idtr.baseAddr));
77   CHK_VMCS_READ(GUEST_GDTR_LIMIT, &(p->gdtr.limit));
78   CHK_VMCS_READ(GUEST_IDTR_LIMIT, &(p->idtr.limit));
79   CHK_VMCS_READ(GUEST_IA32_DEBUGCTL, &(p->dbg_ctrl));
80   CHK_VMCS_READ(GUEST_IA32_DEBUGCTL_HIGH, ((char *)&(p->dbg_ctrl)) + 4);
81   CHK_VMCS_READ(GUEST_IA32_SYSENTER_CS, &(p->sysenter_cs));
82   CHK_VMCS_READ(GUEST_IA32_SYSENTER_ESP, &(p->sysenter_esp));
83   CHK_VMCS_READ(GUEST_IA32_SYSENTER_EIP, &(p->sysenter_eip));
84   CHK_VMCS_READ(GUEST_SMBASE, &(p->smbase));
85
86   CHK_VMCS_READ(GUEST_ACTIVITY_STATE, &(p->activity));
87   CHK_VMCS_READ(GUEST_INT_STATE, &(p->interrupt_state));
88   CHK_VMCS_READ(GUEST_PENDING_DEBUG_EXCS, &(p->pending_dbg_exceptions));
89   CHK_VMCS_READ(VMCS_LINK_PTR, &(p->vmcs_link));
90   CHK_VMCS_READ(VMCS_LINK_PTR_HIGH, ((char *)&(p->vmcs_link)) + 4);
91   return 0;
92 }
93
94
95 int CopyInVMCSGuestStateArea(struct VMCSGuestStateArea *p) {
96   CHK_VMCS_WRITE(GUEST_CR0, &(p->cr0));
97   CHK_VMCS_WRITE(GUEST_CR3, &(p->cr3));
98   CHK_VMCS_WRITE(GUEST_CR4, &(p->cr4));
99   CHK_VMCS_WRITE(GUEST_DR7, &(p->dr7));
100   CHK_VMCS_WRITE(GUEST_RSP, &(p->rsp));
101   CHK_VMCS_WRITE(GUEST_RIP, &(p->rip));
102   CHK_VMCS_WRITE(GUEST_RFLAGS, &(p->rflags));
103   CHK_VMCS_WRITE(VMCS_GUEST_CS_SELECTOR, &(p->cs.selector));
104   CHK_VMCS_WRITE(VMCS_GUEST_SS_SELECTOR, &(p->ss.selector));
105   CHK_VMCS_WRITE(VMCS_GUEST_DS_SELECTOR, &(p->ds.selector));
106   CHK_VMCS_WRITE(VMCS_GUEST_ES_SELECTOR, &(p->es.selector));
107   CHK_VMCS_WRITE(VMCS_GUEST_FS_SELECTOR, &(p->fs.selector));
108   CHK_VMCS_WRITE(VMCS_GUEST_GS_SELECTOR, &(p->gs.selector));
109   CHK_VMCS_WRITE(VMCS_GUEST_LDTR_SELECTOR, &(p->ldtr.selector));
110   CHK_VMCS_WRITE(VMCS_GUEST_TR_SELECTOR, &(p->tr.selector));
111   CHK_VMCS_WRITE(GUEST_CS_BASE, &(p->cs.baseAddr));
112   CHK_VMCS_WRITE(GUEST_SS_BASE, &(p->ss.baseAddr));
113   CHK_VMCS_WRITE(GUEST_DS_BASE, &(p->ds.baseAddr));
114   CHK_VMCS_WRITE(GUEST_ES_BASE, &(p->es.baseAddr));
115   CHK_VMCS_WRITE(GUEST_FS_BASE, &(p->fs.baseAddr));
116   CHK_VMCS_WRITE(GUEST_GS_BASE, &(p->gs.baseAddr));
117   CHK_VMCS_WRITE(GUEST_LDTR_BASE, &(p->ldtr.baseAddr));
118   CHK_VMCS_WRITE(GUEST_TR_BASE, &(p->tr.baseAddr));
119   CHK_VMCS_WRITE(GUEST_CS_LIMIT, &(p->cs.limit));
120   CHK_VMCS_WRITE(GUEST_SS_LIMIT, &(p->ss.limit));
121   CHK_VMCS_WRITE(GUEST_DS_LIMIT, &(p->ds.limit));
122   CHK_VMCS_WRITE(GUEST_ES_LIMIT, &(p->es.limit));
123   CHK_VMCS_WRITE(GUEST_FS_LIMIT, &(p->fs.limit));
124   CHK_VMCS_WRITE(GUEST_GS_LIMIT, &(p->gs.limit));
125   CHK_VMCS_WRITE(GUEST_LDTR_LIMIT, &(p->ldtr.limit));
126   CHK_VMCS_WRITE(GUEST_TR_LIMIT, &(p->tr.limit));
127   CHK_VMCS_WRITE(GUEST_CS_ACCESS, &(p->cs.access));
128   CHK_VMCS_WRITE(GUEST_SS_ACCESS, &(p->ss.access));
129   CHK_VMCS_WRITE(GUEST_DS_ACCESS, &(p->ds.access));
130   CHK_VMCS_WRITE(GUEST_ES_ACCESS, &(p->es.access));
131   CHK_VMCS_WRITE(GUEST_FS_ACCESS, &(p->fs.access));
132   CHK_VMCS_WRITE(GUEST_GS_ACCESS, &(p->gs.access));
133   CHK_VMCS_WRITE(GUEST_LDTR_ACCESS, &(p->ldtr.access));
134   CHK_VMCS_WRITE(GUEST_TR_ACCESS, &(p->tr.access));
135   CHK_VMCS_WRITE(GUEST_GDTR_BASE, &(p->gdtr.baseAddr));
136   CHK_VMCS_WRITE(GUEST_IDTR_BASE, &(p->idtr.baseAddr));
137   CHK_VMCS_WRITE(GUEST_GDTR_LIMIT, &(p->gdtr.limit));
138   CHK_VMCS_WRITE(GUEST_IDTR_LIMIT, &(p->idtr.limit));
139   CHK_VMCS_WRITE(GUEST_IA32_DEBUGCTL, &(p->dbg_ctrl));
140   CHK_VMCS_WRITE(GUEST_IA32_DEBUGCTL_HIGH, ((char *)&(p->dbg_ctrl)) + 4);
141   CHK_VMCS_WRITE(GUEST_IA32_SYSENTER_CS, &(p->sysenter_cs));
142   CHK_VMCS_WRITE(GUEST_IA32_SYSENTER_ESP, &(p->sysenter_esp));
143   CHK_VMCS_WRITE(GUEST_IA32_SYSENTER_EIP, &(p->sysenter_eip));
144   CHK_VMCS_WRITE(GUEST_SMBASE, &(p->smbase));
145
146   CHK_VMCS_WRITE(GUEST_ACTIVITY_STATE, &(p->activity));
147   CHK_VMCS_WRITE(GUEST_INT_STATE, &(p->interrupt_state));
148   CHK_VMCS_WRITE(GUEST_PENDING_DEBUG_EXCS, &(p->pending_dbg_exceptions));
149   CHK_VMCS_WRITE(VMCS_LINK_PTR, &(p->vmcs_link));
150   CHK_VMCS_WRITE(VMCS_LINK_PTR_HIGH, ((char *)&(p->vmcs_link)) + 4);
151   return 0;
152 }
153
154
155
156 int CopyOutVMCSHostStateArea(struct VMCSHostStateArea *p) {
157   CHK_VMCS_READ(HOST_CR0, &(p->cr0));
158   CHK_VMCS_READ(HOST_CR3, &(p->cr3));
159   CHK_VMCS_READ(HOST_CR4, &(p->cr4));
160   CHK_VMCS_READ(HOST_RSP, &(p->rsp));
161   CHK_VMCS_READ(HOST_RIP, &(p->rip));
162   CHK_VMCS_READ(VMCS_HOST_CS_SELECTOR, &(p->csSelector));
163   CHK_VMCS_READ(VMCS_HOST_SS_SELECTOR, &(p->ssSelector));
164   CHK_VMCS_READ(VMCS_HOST_DS_SELECTOR, &(p->dsSelector));
165   CHK_VMCS_READ(VMCS_HOST_ES_SELECTOR, &(p->esSelector));
166   CHK_VMCS_READ(VMCS_HOST_FS_SELECTOR, &(p->fsSelector));
167   CHK_VMCS_READ(VMCS_HOST_GS_SELECTOR, &(p->gsSelector));
168   CHK_VMCS_READ(VMCS_HOST_TR_SELECTOR, &(p->trSelector));
169   CHK_VMCS_READ(HOST_FS_BASE, &(p->fsBaseAddr));
170   CHK_VMCS_READ(HOST_GS_BASE, &(p->gsBaseAddr));
171   CHK_VMCS_READ(HOST_TR_BASE, &(p->trBaseAddr));
172   CHK_VMCS_READ(HOST_GDTR_BASE, &(p->gdtrBaseAddr));
173   CHK_VMCS_READ(HOST_IDTR_BASE, &(p->idtrBaseAddr));
174   CHK_VMCS_READ(HOST_IA32_SYSENTER_CS, &(p->sysenter_cs));
175   CHK_VMCS_READ(HOST_IA32_SYSENTER_ESP, &(p->sysenter_esp));
176   CHK_VMCS_READ(HOST_IA32_SYSENTER_EIP, &(p->sysenter_eip));
177   return 0;
178 }
179
180
181
182 int CopyInVMCSHostStateArea(struct VMCSHostStateArea *p) {
183   CHK_VMCS_WRITE(HOST_CR0, &(p->cr0));
184   CHK_VMCS_WRITE(HOST_CR3, &(p->cr3));
185   CHK_VMCS_WRITE(HOST_CR4, &(p->cr4));
186   CHK_VMCS_WRITE(HOST_RSP, &(p->rsp));
187   CHK_VMCS_WRITE(HOST_RIP, &(p->rip));
188   CHK_VMCS_WRITE(VMCS_HOST_CS_SELECTOR, &(p->csSelector));
189   CHK_VMCS_WRITE(VMCS_HOST_SS_SELECTOR, &(p->ssSelector));
190   CHK_VMCS_WRITE(VMCS_HOST_DS_SELECTOR, &(p->dsSelector));
191   CHK_VMCS_WRITE(VMCS_HOST_ES_SELECTOR, &(p->esSelector));
192   CHK_VMCS_WRITE(VMCS_HOST_FS_SELECTOR, &(p->fsSelector));
193   CHK_VMCS_WRITE(VMCS_HOST_GS_SELECTOR, &(p->gsSelector));
194   CHK_VMCS_WRITE(VMCS_HOST_TR_SELECTOR, &(p->trSelector));
195   CHK_VMCS_WRITE(HOST_FS_BASE, &(p->fsBaseAddr));
196   CHK_VMCS_WRITE(HOST_GS_BASE, &(p->gsBaseAddr));
197   CHK_VMCS_WRITE(HOST_TR_BASE, &(p->trBaseAddr));
198   CHK_VMCS_WRITE(HOST_GDTR_BASE, &(p->gdtrBaseAddr));
199   CHK_VMCS_WRITE(HOST_IDTR_BASE, &(p->idtrBaseAddr));
200   CHK_VMCS_WRITE(HOST_IA32_SYSENTER_CS, &(p->sysenter_cs));
201   CHK_VMCS_WRITE(HOST_IA32_SYSENTER_ESP, &(p->sysenter_esp));
202   CHK_VMCS_WRITE(HOST_IA32_SYSENTER_EIP, &(p->sysenter_eip));
203   return 0;
204 }
205
206
207 int CopyOutVMCSExitCtrlFields(struct VMCSExitCtrlFields *p)
208 {
209   CHK_VMCS_READ(VM_EXIT_CTRLS,&(p->exitCtrls));
210   CHK_VMCS_READ(VM_EXIT_MSR_STORE_COUNT,&(p->msrStoreCount));
211   CHK_VMCS_READ(VM_EXIT_MSR_STORE_ADDR,&(p->msrStoreAddr));
212   CHK_VMCS_READ(VM_EXIT_MSR_LOAD_COUNT,&(p->msrLoadCount));
213   CHK_VMCS_READ(VM_EXIT_MSR_LOAD_ADDR,&(p->msrLoadAddr));
214   return 0;
215 }
216
217 int CopyInVMCSExitCtrlFields(struct VMCSExitCtrlFields *p)
218 {
219   CHK_VMCS_WRITE(VM_EXIT_CTRLS,&(p->exitCtrls));
220   CHK_VMCS_WRITE(VM_EXIT_MSR_STORE_COUNT,&(p->msrStoreCount));
221   CHK_VMCS_WRITE(VM_EXIT_MSR_STORE_ADDR,&(p->msrStoreAddr));
222   CHK_VMCS_WRITE(VM_EXIT_MSR_LOAD_COUNT,&(p->msrLoadCount));
223   CHK_VMCS_WRITE(VM_EXIT_MSR_LOAD_ADDR,&(p->msrLoadAddr));
224   return 0;
225 }
226
227
228 int CopyOutVMCSEntryCtrlFields(struct VMCSEntryCtrlFields *p)
229 {
230   CHK_VMCS_READ(VM_ENTRY_CTRLS,&(p->entryCtrls));
231   CHK_VMCS_READ(VM_ENTRY_MSR_LOAD_COUNT,&(p->msrLoadCount));
232   CHK_VMCS_READ(VM_ENTRY_MSR_LOAD_ADDR,&(p->msrLoadAddr));
233   CHK_VMCS_READ(VM_ENTRY_INT_INFO_FIELD,&(p->intInfo));
234   CHK_VMCS_READ(VM_ENTRY_EXCEPTION_ERROR,&(p->exceptionErrorCode));
235   CHK_VMCS_READ(VM_ENTRY_INSTR_LENGTH,&(p->instrLength));
236   return 0;
237 }
238
239 int CopyInVMCSEntryCtrlFields(struct VMCSEntryCtrlFields *p)
240 {
241   CHK_VMCS_WRITE(VM_ENTRY_CTRLS,&(p->entryCtrls));
242   CHK_VMCS_WRITE(VM_ENTRY_MSR_LOAD_COUNT,&(p->msrLoadCount));
243   CHK_VMCS_WRITE(VM_ENTRY_MSR_LOAD_ADDR,&(p->msrLoadAddr));
244   CHK_VMCS_WRITE(VM_ENTRY_INT_INFO_FIELD,&(p->intInfo));
245   CHK_VMCS_WRITE(VM_ENTRY_EXCEPTION_ERROR,&(p->exceptionErrorCode));
246   CHK_VMCS_WRITE(VM_ENTRY_INSTR_LENGTH,&(p->instrLength));
247   return 0;
248 }
249
250 int CopyOutVMCSExitInfoFields(struct VMCSExitInfoFields *p) {
251   CHK_VMCS_READ(EXIT_REASON,&(p->reason));
252   CHK_VMCS_READ(EXIT_QUALIFICATION,&(p->qualification));
253   CHK_VMCS_READ(VM_EXIT_INT_INFO,&(p->intInfo));
254   CHK_VMCS_READ(VM_EXIT_INT_ERROR,&(p->intErrorCode));
255   CHK_VMCS_READ(IDT_VECTOR_INFO,&(p->idtVectorInfo));
256   CHK_VMCS_READ(IDT_VECTOR_ERROR,&(p->idtVectorErrorCode));
257   CHK_VMCS_READ(VM_EXIT_INSTR_LENGTH,&(p->instrLength));
258   CHK_VMCS_READ(GUEST_LINEAR_ADDR,&(p->guestLinearAddr));
259   CHK_VMCS_READ(VMX_INSTR_INFO,&(p->instrInfo));
260   CHK_VMCS_READ(IO_RCX,&(p->ioRCX));
261   CHK_VMCS_READ(IO_RSI,&(p->ioRSI));
262   CHK_VMCS_READ(IO_RDI,&(p->ioRDI));
263   CHK_VMCS_READ(IO_RIP,&(p->ioRIP));
264   CHK_VMCS_READ(VM_INSTR_ERROR,&(p->instrErrorField));
265   return 0;
266 }
267
268
269 int CopyOutVMCSExecCtrlFields(struct VMCSExecCtrlFields *p)
270 {
271   CHK_VMCS_READ(PIN_VM_EXEC_CTRLS,&(p->pinCtrls));
272   CHK_VMCS_READ(PROC_VM_EXEC_CTRLS,&(p->procCtrls));
273   CHK_VMCS_READ(EXCEPTION_BITMAP,&(p->execBitmap));
274   CHK_VMCS_READ(PAGE_FAULT_ERROR_MASK,&(p->pageFaultErrorMask));
275   CHK_VMCS_READ(PAGE_FAULT_ERROR_MATCH,&(p->pageFaultErrorMatch));
276   CHK_VMCS_READ(IO_BITMAP_A_ADDR,&(p->ioBitmapA));
277   CHK_VMCS_READ(IO_BITMAP_B_ADDR,&(p->ioBitmapB));
278   CHK_VMCS_READ(TSC_OFFSET,&(p->tscOffset));
279   CHK_VMCS_READ(CR0_GUEST_HOST_MASK,&(p->cr0GuestHostMask));
280   CHK_VMCS_READ(CR0_READ_SHADOW,&(p->cr0ReadShadow));
281   CHK_VMCS_READ(CR4_GUEST_HOST_MASK,&(p->cr4GuestHostMask));
282   CHK_VMCS_READ(CR4_READ_SHADOW,&(p->cr4ReadShadow));
283   CHK_VMCS_READ(CR3_TARGET_COUNT, &(p->cr3TargetCount));
284   CHK_VMCS_READ(CR3_TARGET_VALUE_0, &(p->cr3TargetValue0));
285   CHK_VMCS_READ(CR3_TARGET_VALUE_1, &(p->cr3TargetValue1));
286   CHK_VMCS_READ(CR3_TARGET_VALUE_2, &(p->cr3TargetValue2));
287   CHK_VMCS_READ(CR3_TARGET_VALUE_3, &(p->cr3TargetValue3));
288   CHK_VMCS_READ(VIRT_APIC_PAGE_ADDR, &(p->virtApicPageAddr));
289   CHK_VMCS_READ(TPR_THRESHOLD, &(p->tprThreshold));
290   CHK_VMCS_READ(MSR_BITMAPS, &(p->MSRBitmapsBaseAddr));
291   CHK_VMCS_READ(VMCS_EXEC_PTR,&(p->vmcsExecPtr));
292   return 0;
293 }
294
295
296 int CopyInVMCSExecCtrlFields(struct VMCSExecCtrlFields *p)
297 {
298   CHK_VMCS_WRITE(PIN_VM_EXEC_CTRLS,&(p->pinCtrls));
299   CHK_VMCS_WRITE(PROC_VM_EXEC_CTRLS,&(p->procCtrls));
300   CHK_VMCS_WRITE(EXCEPTION_BITMAP,&(p->execBitmap));
301   CHK_VMCS_WRITE(PAGE_FAULT_ERROR_MASK,&(p->pageFaultErrorMask));
302   CHK_VMCS_WRITE(PAGE_FAULT_ERROR_MATCH,&(p->pageFaultErrorMatch));
303   CHK_VMCS_WRITE(IO_BITMAP_A_ADDR,&(p->ioBitmapA));
304   CHK_VMCS_WRITE(IO_BITMAP_B_ADDR,&(p->ioBitmapB));
305   CHK_VMCS_WRITE(TSC_OFFSET,&(p->tscOffset));
306   CHK_VMCS_WRITE(CR0_GUEST_HOST_MASK,&(p->cr0GuestHostMask));
307   CHK_VMCS_WRITE(CR0_READ_SHADOW,&(p->cr0ReadShadow));
308   CHK_VMCS_WRITE(CR4_GUEST_HOST_MASK,&(p->cr4GuestHostMask));
309   CHK_VMCS_WRITE(CR4_READ_SHADOW,&(p->cr4ReadShadow));
310   CHK_VMCS_WRITE(CR3_TARGET_COUNT, &(p->cr3TargetCount));
311   CHK_VMCS_WRITE(CR3_TARGET_VALUE_0, &(p->cr3TargetValue0));
312   CHK_VMCS_WRITE(CR3_TARGET_VALUE_1, &(p->cr3TargetValue1));
313   CHK_VMCS_WRITE(CR3_TARGET_VALUE_2, &(p->cr3TargetValue2));
314   CHK_VMCS_WRITE(CR3_TARGET_VALUE_3, &(p->cr3TargetValue3));
315   CHK_VMCS_WRITE(VIRT_APIC_PAGE_ADDR, &(p->virtApicPageAddr));
316   CHK_VMCS_WRITE(TPR_THRESHOLD, &(p->tprThreshold));
317   CHK_VMCS_WRITE(MSR_BITMAPS, &(p->MSRBitmapsBaseAddr));
318   CHK_VMCS_WRITE(VMCS_EXEC_PTR,&(p->vmcsExecPtr));
319   return 0;
320 }
321
322
323 int CopyOutVMCSData(struct VMCSData *p) {
324   if (CopyOutVMCSGuestStateArea(&(p->guestStateArea)) != 0) {
325     return -1;
326   }
327   if (CopyOutVMCSHostStateArea(&(p->hostStateArea)) != 0) {
328     return -1;
329   }
330   if (CopyOutVMCSExecCtrlFields(&(p->execCtrlFields)) != 0) {
331     return -1;
332   }
333   if (CopyOutVMCSExitCtrlFields(&(p->exitCtrlFields)) != 0) {
334     return -1;
335   }
336   if (CopyOutVMCSEntryCtrlFields(&(p->entryCtrlFields)) != 0) {
337     return -1;
338   }
339   if (CopyOutVMCSExitInfoFields(&(p->exitInfoFields)) != 0) {
340     return -1;
341   }
342   return 0;
343 }
344
345
346 int CopyInVMCSData(struct VMCSData *p) {
347   if (CopyInVMCSGuestStateArea(&(p->guestStateArea)) != 0) {
348     return -1;
349   }
350   if (CopyInVMCSHostStateArea(&(p->hostStateArea)) != 0) {
351     return -1;
352   }
353   if (CopyInVMCSExecCtrlFields(&(p->execCtrlFields)) != 0) {
354     return -1;
355   }
356   if (CopyInVMCSExitCtrlFields(&(p->exitCtrlFields)) != 0) {
357     return -1;
358   }
359   if (CopyInVMCSEntryCtrlFields(&(p->entryCtrlFields)) != 0) {
360     return -1;
361   }
362   return 0;
363 }
364
365
366 void PrintTrace_VMX_Regs(struct VMXRegs * regs) {
367   PrintTrace("==>VMX Register values:\n");
368   PrintTrace("EAX: %x\n", regs->eax);
369   PrintTrace("ECX: %x\n", regs->ecx);
370   PrintTrace("EDX: %x\n", regs->edx);
371   PrintTrace("EBX: %x\n", regs->ebx);
372   PrintTrace("ESP: %x\n", regs->esp);
373   PrintTrace("EBP: %x\n", regs->ebp);
374   PrintTrace("ESI: %x\n", regs->esi);
375   PrintTrace("EDI: %x\n", regs->edi);
376   PrintTrace("\n");
377 }
378
379
380 void PrintTrace_VMCSSegment(char * segname, struct VMCSSegment * seg, int abbr) {
381   PrintTrace("Segment: %s\n", segname);
382   if (abbr == 0) {
383     PrintTrace("\tSelector: %x\n", (uint_t)seg->selector);
384     PrintTrace("\tAccess: %x\n", *(uint_t*)&(seg->access));
385   }
386   PrintTrace("\tBase Addr: %x\n", (uint_t)seg->baseAddr);
387   PrintTrace("\tLimit: %x\n", (uint_t)seg->limit);
388
389 }
390
391
392 void PrintTrace_VMCSGuestStateArea(struct VMCSGuestStateArea * guestState) {
393   PrintTrace("==>Guest State Area\n");
394   PrintTrace("==>==> Guest Register State\n");
395   PrintTrace("GUEST_CR0: %x\n",(uint_t) guestState->cr0);
396   PrintTrace("GUEST_CR3: %x\n",(uint_t)guestState->cr3);
397   PrintTrace("GUEST_CR4: %x\n",(uint_t)guestState->cr4);
398   PrintTrace("GUEST_DR7: %x\n",(uint_t)guestState->dr7);
399   PrintTrace("GUEST_RSP: %x\n",(uint_t)guestState->rsp);
400   PrintTrace("GUEST_RIP: %x\n",(uint_t)guestState->rip);
401   PrintTrace("GUEST_RFLAGS: %x\n",(uint_t)guestState->rflags);
402
403   PrintTrace_VMCSSegment("Guest CS", &(guestState->cs), 0);
404   PrintTrace_VMCSSegment("Guest SS", &(guestState->ss), 0);
405   PrintTrace_VMCSSegment("Guest DS",&(guestState->ds), 0);
406   PrintTrace_VMCSSegment("Guest ES", &(guestState->es), 0);
407   PrintTrace_VMCSSegment("Guest FS", &(guestState->fs), 0);
408   PrintTrace_VMCSSegment("Guest GS", &(guestState->gs), 0);
409   PrintTrace_VMCSSegment("Guest LDTR", &(guestState->ldtr), 0);
410   PrintTrace_VMCSSegment("Guest TR", &(guestState->tr), 0);
411   PrintTrace_VMCSSegment("Guest GDTR", &(guestState->gdtr), 1);  
412   PrintTrace_VMCSSegment("Guest IDTR", &(guestState->idtr), 1);  
413
414
415   PrintTrace("GUEST_IA32_DEBUGCTL: %x\n",(uint_t)(guestState->dbg_ctrl & 0xffffffff));
416   PrintTrace("GUEST_IA32_DEBUGCTL_HIGH: %x\n",(uint_t)(guestState->dbg_ctrl >> 32) & 0xffffffff);
417   PrintTrace("GUEST_IA32_SYSENTER_CS: %x\n",guestState->sysenter_cs);
418   PrintTrace("GUEST_IA32_SYSENTER_ESP: %x\n",(uint_t)guestState->sysenter_esp);
419   PrintTrace("GUEST_IA32_SYSENTER_EIP: %x\n",(uint_t)guestState->sysenter_eip);
420   PrintTrace("GUEST_SMBASE: %x\n", (uint_t)guestState->smbase);
421
422   PrintTrace("==>==> Guest Non-Register State\n");
423   PrintTrace("GUEST_ACTIVITY_STATE: %x\n", (uint_t)guestState->activity);
424   PrintTrace("GUEST_INT_STATE: %x\n", (uint_t)guestState->interrupt_state);
425   PrintTrace("GUEST_PENDING_DEBUG_EXCS: %x\n", (uint_t)guestState->pending_dbg_exceptions);
426   PrintTrace("VMCS_LINK_PTR: %x\n", (uint_t)guestState->vmcs_link & 0xffffffff);
427   PrintTrace("VMCS_LINK_PTR_HIGH: %x\n", (uint_t)(guestState->vmcs_link >> 32) & 0xffffffff);
428 }
429
430
431 void PrintTrace_VMCSHostStateArea(struct VMCSHostStateArea * hostState) {
432   PrintTrace("\n==> Host State Area\n");
433   PrintTrace("HOST_CR0: %x\n", (uint_t)hostState->cr0);
434   PrintTrace("HOST_CR3: %x\n", (uint_t)hostState->cr3);
435   PrintTrace("HOST_CR4: %x\n", (uint_t)hostState->cr4);
436   PrintTrace("HOST_RSP: %x\n", (uint_t)hostState->rsp);
437   PrintTrace("HOST_RIP: %x\n", (uint_t)hostState->rip);
438   PrintTrace("VMCS_HOST_CS_SELECTOR: %x\n", (uint_t)hostState->csSelector);
439   PrintTrace("VMCS_HOST_SS_SELECTOR: %x\n", (uint_t)hostState->ssSelector);
440   PrintTrace("VMCS_HOST_DS_SELECTOR: %x\n", (uint_t)hostState->dsSelector);
441   PrintTrace("VMCS_HOST_ES_SELECTOR: %x\n", (uint_t)hostState->esSelector);
442   PrintTrace("VMCS_HOST_FS_SELECTOR: %x\n", (uint_t)hostState->fsSelector);
443   PrintTrace("VMCS_HOST_GS_SELECTOR: %x\n", (uint_t)hostState->gsSelector);
444   PrintTrace("VMCS_HOST_TR_SELECTOR: %x\n", (uint_t)hostState->trSelector);
445   PrintTrace("HOST_FS_BASE: %x\n", (uint_t)hostState->fsBaseAddr);
446   PrintTrace("HOST_GS_BASE: %x\n", (uint_t)hostState->gsBaseAddr);
447   PrintTrace("HOST_TR_BASE: %x\n", (uint_t)hostState->trBaseAddr);
448   PrintTrace("HOST_GDTR_BASE: %x\n", (uint_t)hostState->gdtrBaseAddr);
449   PrintTrace("HOST_IDTR_BASE: %x\n", (uint_t)hostState->idtrBaseAddr);
450   PrintTrace("HOST_IA32_SYSENTER_CS: %x\n", (uint_t)hostState->sysenter_cs);
451   PrintTrace("HOST_IA32_SYSENTER_ESP: %x\n", (uint_t)hostState->sysenter_esp);
452   PrintTrace("HOST_IA32_SYSENTER_EIP: %x\n", (uint_t)hostState->sysenter_eip);
453 }
454
455 void PrintTrace_VMCSExecCtrlFields(struct VMCSExecCtrlFields * execCtrls) {
456   PrintTrace("\n==> VM-Execution Controls:\n");
457   PrintTrace("PIN_VM_EXEC_CTRLS: %x\n", (uint_t) execCtrls->pinCtrls);
458   PrintTrace("PROC_VM_EXEC_CTRLS: %x\n", (uint_t) execCtrls->procCtrls);
459   PrintTrace("EXCEPTION_BITMAP: %x\n", (uint_t) execCtrls->execBitmap);
460   PrintTrace("PAGE_FAULT_ERROR_MASK: %x\n", (uint_t) execCtrls->pageFaultErrorMask);
461   PrintTrace("PAGE_FAULT_ERROR_MATCH: %x\n", (uint_t) execCtrls->pageFaultErrorMatch);
462   PrintTrace("IO_BITMAP_A_ADDR: %x\n", (uint_t) execCtrls->ioBitmapA);
463   //  PrintTrace("IO_BITMAP_A_ADDR_HIGH: %x\n", (uint_t) execCtrls->);
464   PrintTrace("IO_BITMAP_B_ADDR: %x\n", (uint_t) execCtrls->ioBitmapB);
465   // PrintTrace("IO_BITMAP_B_ADDR_HIGH: %x\n", (uint_t) execCtrls->);
466   PrintTrace("TSC_OFFSET: %x\n", (uint_t) execCtrls->tscOffset & 0xffffffff);
467   PrintTrace("TSC_OFFSET_HIGH: %x\n", (uint_t) (execCtrls->tscOffset >> 32) & 0xffffffff);
468   PrintTrace("CR0_GUEST_HOST_MASK: %x\n", (uint_t) execCtrls->cr0GuestHostMask);
469   PrintTrace("CR0_READ_SHADOW: %x\n", (uint_t) execCtrls->cr0ReadShadow);
470   PrintTrace("CR4_GUEST_HOST_MASK: %x\n", (uint_t) execCtrls->cr4GuestHostMask);
471   PrintTrace("CR4_READ_SHADOW: %x\n", (uint_t) execCtrls->cr4ReadShadow);
472   PrintTrace("CR3_TARGET_COUNT: %x\n", (uint_t) execCtrls->cr3TargetCount);
473   PrintTrace("CR3_TARGET_VALUE_0: %x\n", (uint_t) execCtrls->cr3TargetValue0);
474   PrintTrace("CR3_TARGET_VALUE_1: %x\n", (uint_t) execCtrls->cr3TargetValue1);
475   PrintTrace("CR3_TARGET_VALUE_2: %x\n", (uint_t) execCtrls->cr3TargetValue2);
476   PrintTrace("CR3_TARGET_VALUE_3: %x\n", (uint_t) execCtrls->cr3TargetValue3);
477   PrintTrace("VIRT_APIC_PAGE_ADDR: %x\n", (uint_t) execCtrls->virtApicPageAddr & 0xffffffff);
478   PrintTrace("VIRT_APIC_PAGE_ADDR_HIGH: %x\n", (uint_t) (execCtrls->virtApicPageAddr >> 32) & 0xffffffff);
479   PrintTrace("TPR_THRESHOLD: %x\n", (uint_t) execCtrls->tprThreshold);
480   PrintTrace("MSR_BITMAPS: %x\n", (uint_t) execCtrls->MSRBitmapsBaseAddr & 0xffffffff);
481   PrintTrace("MSR_BITMAPS_HIGH: %x\n", (uint_t) (execCtrls->MSRBitmapsBaseAddr >> 32) & 0xffffffff);
482   PrintTrace("VMCS_EXEC_PTR: %x\n", (uint_t) execCtrls->vmcsExecPtr & 0xffffffff);
483   PrintTrace("VMCS_EXEC_PTR_HIGH: %x\n", (uint_t) (execCtrls->vmcsExecPtr >> 32) & 0xffffffff);
484 }
485
486 void PrintTrace_VMCSExitCtrlFields(struct VMCSExitCtrlFields * exitCtrls) {
487   PrintTrace("\n==> VM Exit Controls\n");
488   PrintTrace("VM_EXIT_CTRLS: %x\n", (uint_t) exitCtrls->exitCtrls);
489   PrintTrace("VM_EXIT_MSR_STORE_COUNT: %x\n", (uint_t) exitCtrls->msrStoreCount);
490   PrintTrace("VM_EXIT_MSR_STORE_ADDR: %x\n", (uint_t) exitCtrls->msrStoreAddr & 0xffffffff);
491   PrintTrace("VM_EXIT_MSR_STORE_ADDR_HIGH: %x\n", (uint_t) (exitCtrls->msrStoreAddr >> 32) & 0xffffffff);
492   PrintTrace("VM_EXIT_MSR_LOAD_COUNT: %x\n", (uint_t) exitCtrls->msrLoadCount);
493   PrintTrace("VM_EXIT_MSR_LOAD_ADDR: %x\n", (uint_t) exitCtrls->msrLoadAddr & 0xffffffff);
494   PrintTrace("VM_EXIT_MSR_LOAD_ADDR_HIGH: %x\n", (uint_t) (exitCtrls->msrLoadAddr >> 32) & 0xffffffff);
495 }
496
497 void PrintTrace_VMCSEntryCtrlFields(struct VMCSEntryCtrlFields * entryCtrls) {
498   PrintTrace("\n==> VM Entry Controls\n");
499   PrintTrace("VM_ENTRY_CTRLS: %x\n", (uint_t) entryCtrls->entryCtrls);
500   PrintTrace("VM_ENTRY_MSR_LOAD_COUNT: %x\n", (uint_t) entryCtrls->msrLoadCount);
501   PrintTrace("VM_ENTRY_MSR_LOAD_ADDR: %x\n", (uint_t) entryCtrls->msrLoadAddr & 0xffffffff);
502   PrintTrace("VM_ENTRY_MSR_LOAD_ADDR_HIGH: %x\n", (uint_t) (entryCtrls->msrLoadAddr >> 32) & 0xffffffff);
503   PrintTrace("VM_ENTRY_INT_INFO_FIELD: %x\n", (uint_t) entryCtrls->intInfo);
504   PrintTrace("VM_ENTRY_EXCEPTION_ERROR: %x\n", (uint_t) entryCtrls->exceptionErrorCode);
505   PrintTrace("VM_ENTRY_INSTR_LENGTH: %x\n", (uint_t) entryCtrls->instrLength);
506 }
507
508 void PrintTrace_VMCSExitInfoFields(struct VMCSExitInfoFields * exitInfo) {
509   PrintTrace("\n==> VM Exit Info\n");
510   PrintTrace("EXIT_REASON: %x\n", (uint_t) exitInfo->reason);
511   PrintTrace("EXIT_QUALIFICATION: %x\n", (uint_t) exitInfo->qualification);
512   PrintTrace("VM_EXIT_INT_INFO: %x\n", (uint_t) exitInfo->intInfo);
513   PrintTrace("VM_EXIT_INT_ERROR: %x\n", (uint_t) exitInfo->intErrorCode);
514   PrintTrace("IDT_VECTOR_INFO: %x\n", (uint_t) exitInfo->idtVectorInfo);
515   PrintTrace("IDT_VECTOR_ERROR: %x\n", (uint_t) exitInfo->idtVectorErrorCode);
516   PrintTrace("VM_EXIT_INSTR_LENGTH: %x\n", (uint_t) exitInfo->instrLength);
517   PrintTrace("GUEST_LINEAR_ADDR: %x\n", (uint_t) exitInfo->guestLinearAddr);
518   PrintTrace("VMX_INSTR_INFO: %x\n", (uint_t) exitInfo->instrInfo);
519   PrintTrace("IO_RCX: %x\n", (uint_t) exitInfo->ioRCX);
520   PrintTrace("IO_RSI: %x\n", (uint_t) exitInfo->ioRSI);
521   PrintTrace("IO_RDI: %x\n", (uint_t) exitInfo->ioRDI);
522   PrintTrace("IO_RIP: %x\n", (uint_t) exitInfo->ioRIP);
523   PrintTrace("VM_INSTR_ERROR: %x\n", (uint_t) exitInfo->instrErrorField);
524 }
525
526
527 void PrintTrace_VMCSData(struct VMCSData * vmcs) {
528   PrintTrace("VMCSData Structure\n");
529
530   PrintTrace_VMCSGuestStateArea(&(vmcs->guestStateArea));
531   PrintTrace_VMCSHostStateArea(&(vmcs->hostStateArea));
532   PrintTrace_VMCSExecCtrlFields(&(vmcs->execCtrlFields));
533   PrintTrace_VMCSExitCtrlFields(&(vmcs->exitCtrlFields));
534   PrintTrace_VMCSEntryCtrlFields(&(vmcs->entryCtrlFields));
535   PrintTrace_VMCSExitInfoFields(&(vmcs->exitInfoFields));
536   PrintTrace("\n");
537 }