1 | /* $Id: HWVMXR0.cpp 10463 2008-07-10 09:42:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HWACCM VMX - Host Context Ring 0.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_HWACCM
|
---|
27 | #include <VBox/hwaccm.h>
|
---|
28 | #include "HWACCMInternal.h"
|
---|
29 | #include <VBox/vm.h>
|
---|
30 | #include <VBox/x86.h>
|
---|
31 | #include <VBox/pgm.h>
|
---|
32 | #include <VBox/pdm.h>
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 | #include <VBox/selm.h>
|
---|
36 | #include <VBox/iom.h>
|
---|
37 | #include <iprt/param.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include "HWVMXR0.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /* IO operation lookup arrays. */
|
---|
45 | static uint32_t aIOSize[4] = {1, 2, 0, 4};
|
---|
46 | static uint32_t aIOOpAnd[4] = {0xff, 0xffff, 0, 0xffffffff};
|
---|
47 |
|
---|
48 |
|
---|
49 | static void VMXR0CheckError(PVM pVM, int rc)
|
---|
50 | {
|
---|
51 | if (rc == VERR_VMX_GENERIC)
|
---|
52 | {
|
---|
53 | RTCCUINTREG instrError;
|
---|
54 |
|
---|
55 | VMXReadVMCS(VMX_VMCS_RO_VM_INSTR_ERROR, &instrError);
|
---|
56 | pVM->hwaccm.s.vmx.ulLastInstrError = instrError;
|
---|
57 | }
|
---|
58 | pVM->hwaccm.s.lLastError = rc;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Sets up and activates VT-x on the current CPU
|
---|
63 | *
|
---|
64 | * @returns VBox status code.
|
---|
65 | * @param pCpu CPU info struct
|
---|
66 | * @param pVM The VM to operate on.
|
---|
67 | * @param pvPageCpu Pointer to the global cpu page
|
---|
68 | * @param pPageCpuPhys Physical address of the global cpu page
|
---|
69 | */
|
---|
70 | HWACCMR0DECL(int) VMXR0EnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
|
---|
71 | {
|
---|
72 | AssertReturn(pPageCpuPhys, VERR_INVALID_PARAMETER);
|
---|
73 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
74 | AssertReturn(pvPageCpu, VERR_INVALID_PARAMETER);
|
---|
75 |
|
---|
76 | /* Setup Intel VMX. */
|
---|
77 | Assert(pVM->hwaccm.s.vmx.fSupported);
|
---|
78 |
|
---|
79 | #ifdef LOG_ENABLED
|
---|
80 | SUPR0Printf("VMXR0EnableCpu cpu %d page (%x) %x\n", pCpu->idCpu, pvPageCpu, (uint32_t)pPageCpuPhys);
|
---|
81 | #endif
|
---|
82 | /* Set revision dword at the beginning of the VMXON structure. */
|
---|
83 | *(uint32_t *)pvPageCpu = MSR_IA32_VMX_BASIC_INFO_VMCS_ID(pVM->hwaccm.s.vmx.msr.vmx_basic_info);
|
---|
84 |
|
---|
85 | /* @todo we should unmap the two pages from the virtual address space in order to prevent accidental corruption.
|
---|
86 | * (which can have very bad consequences!!!)
|
---|
87 | */
|
---|
88 |
|
---|
89 | /* Make sure the VMX instructions don't cause #UD faults. */
|
---|
90 | ASMSetCR4(ASMGetCR4() | X86_CR4_VMXE);
|
---|
91 |
|
---|
92 | /* Enter VMX Root Mode */
|
---|
93 | int rc = VMXEnable(pPageCpuPhys);
|
---|
94 | if (VBOX_FAILURE(rc))
|
---|
95 | {
|
---|
96 | VMXR0CheckError(pVM, rc);
|
---|
97 | ASMSetCR4(ASMGetCR4() & ~X86_CR4_VMXE);
|
---|
98 | return VERR_VMX_VMXON_FAILED;
|
---|
99 | }
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Deactivates VT-x on the current CPU
|
---|
105 | *
|
---|
106 | * @returns VBox status code.
|
---|
107 | * @param pCpu CPU info struct
|
---|
108 | * @param pvPageCpu Pointer to the global cpu page
|
---|
109 | * @param pPageCpuPhys Physical address of the global cpu page
|
---|
110 | */
|
---|
111 | HWACCMR0DECL(int) VMXR0DisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
|
---|
112 | {
|
---|
113 | AssertReturn(pPageCpuPhys, VERR_INVALID_PARAMETER);
|
---|
114 | AssertReturn(pvPageCpu, VERR_INVALID_PARAMETER);
|
---|
115 |
|
---|
116 | /* Leave VMX Root Mode. */
|
---|
117 | VMXDisable();
|
---|
118 |
|
---|
119 | /* And clear the X86_CR4_VMXE bit */
|
---|
120 | ASMSetCR4(ASMGetCR4() & ~X86_CR4_VMXE);
|
---|
121 |
|
---|
122 | #ifdef LOG_ENABLED
|
---|
123 | SUPR0Printf("VMXR0DisableCpu cpu %d\n", pCpu->idCpu);
|
---|
124 | #endif
|
---|
125 | return VINF_SUCCESS;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Does Ring-0 per VM VT-x init.
|
---|
130 | *
|
---|
131 | * @returns VBox status code.
|
---|
132 | * @param pVM The VM to operate on.
|
---|
133 | */
|
---|
134 | HWACCMR0DECL(int) VMXR0InitVM(PVM pVM)
|
---|
135 | {
|
---|
136 | int rc;
|
---|
137 |
|
---|
138 | #ifdef LOG_ENABLED
|
---|
139 | SUPR0Printf("VMXR0InitVM %x\n", pVM);
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | /* Allocate one page for the VM control structure (VMCS). */
|
---|
143 | rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.vmx.pMemObjVMCS, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
|
---|
144 | AssertRC(rc);
|
---|
145 | if (RT_FAILURE(rc))
|
---|
146 | return rc;
|
---|
147 |
|
---|
148 | pVM->hwaccm.s.vmx.pVMCS = RTR0MemObjAddress(pVM->hwaccm.s.vmx.pMemObjVMCS);
|
---|
149 | pVM->hwaccm.s.vmx.pVMCSPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.vmx.pMemObjVMCS, 0);
|
---|
150 | ASMMemZero32(pVM->hwaccm.s.vmx.pVMCS, PAGE_SIZE);
|
---|
151 |
|
---|
152 | /* Allocate one page for the TSS we need for real mode emulation. */
|
---|
153 | rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.vmx.pMemObjRealModeTSS, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
|
---|
154 | AssertRC(rc);
|
---|
155 | if (RT_FAILURE(rc))
|
---|
156 | return rc;
|
---|
157 |
|
---|
158 | pVM->hwaccm.s.vmx.pRealModeTSS = (PVBOXTSS)RTR0MemObjAddress(pVM->hwaccm.s.vmx.pMemObjRealModeTSS);
|
---|
159 | pVM->hwaccm.s.vmx.pRealModeTSSPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.vmx.pMemObjRealModeTSS, 0);
|
---|
160 |
|
---|
161 | /* The I/O bitmap starts right after the virtual interrupt redirection bitmap. Outside the TSS on purpose; the CPU will not check it
|
---|
162 | * for I/O operations. */
|
---|
163 | ASMMemZero32(pVM->hwaccm.s.vmx.pRealModeTSS, PAGE_SIZE);
|
---|
164 | pVM->hwaccm.s.vmx.pRealModeTSS->offIoBitmap = sizeof(*pVM->hwaccm.s.vmx.pRealModeTSS);
|
---|
165 | /* Bit set to 0 means redirection enabled. */
|
---|
166 | memset(pVM->hwaccm.s.vmx.pRealModeTSS->IntRedirBitmap, 0x0, sizeof(pVM->hwaccm.s.vmx.pRealModeTSS->IntRedirBitmap));
|
---|
167 |
|
---|
168 | if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW)
|
---|
169 | {
|
---|
170 | /* Allocate one page for the virtual APIC mmio cache. */
|
---|
171 | rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.vmx.pMemObjAPIC, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
|
---|
172 | AssertRC(rc);
|
---|
173 | if (RT_FAILURE(rc))
|
---|
174 | return rc;
|
---|
175 |
|
---|
176 | pVM->hwaccm.s.vmx.pAPIC = (uint8_t *)RTR0MemObjAddress(pVM->hwaccm.s.vmx.pMemObjAPIC);
|
---|
177 | pVM->hwaccm.s.vmx.pAPICPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.vmx.pMemObjAPIC, 0);
|
---|
178 | ASMMemZero32(pVM->hwaccm.s.vmx.pAPIC, PAGE_SIZE);
|
---|
179 | }
|
---|
180 | else
|
---|
181 | {
|
---|
182 | pVM->hwaccm.s.vmx.pMemObjAPIC = 0;
|
---|
183 | pVM->hwaccm.s.vmx.pAPIC = 0;
|
---|
184 | pVM->hwaccm.s.vmx.pAPICPhys = 0;
|
---|
185 | }
|
---|
186 |
|
---|
187 | #ifdef LOG_ENABLED
|
---|
188 | SUPR0Printf("VMXR0InitVM %x VMCS=%x (%x) RealModeTSS=%x (%x)\n", pVM, pVM->hwaccm.s.vmx.pVMCS, (uint32_t)pVM->hwaccm.s.vmx.pVMCSPhys, pVM->hwaccm.s.vmx.pRealModeTSS, (uint32_t)pVM->hwaccm.s.vmx.pRealModeTSSPhys);
|
---|
189 | #endif
|
---|
190 | return VINF_SUCCESS;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Does Ring-0 per VM VT-x termination.
|
---|
195 | *
|
---|
196 | * @returns VBox status code.
|
---|
197 | * @param pVM The VM to operate on.
|
---|
198 | */
|
---|
199 | HWACCMR0DECL(int) VMXR0TermVM(PVM pVM)
|
---|
200 | {
|
---|
201 | if (pVM->hwaccm.s.vmx.pMemObjVMCS)
|
---|
202 | {
|
---|
203 | RTR0MemObjFree(pVM->hwaccm.s.vmx.pMemObjVMCS, false);
|
---|
204 | pVM->hwaccm.s.vmx.pMemObjVMCS = 0;
|
---|
205 | pVM->hwaccm.s.vmx.pVMCS = 0;
|
---|
206 | pVM->hwaccm.s.vmx.pVMCSPhys = 0;
|
---|
207 | }
|
---|
208 | if (pVM->hwaccm.s.vmx.pMemObjRealModeTSS)
|
---|
209 | {
|
---|
210 | RTR0MemObjFree(pVM->hwaccm.s.vmx.pMemObjRealModeTSS, false);
|
---|
211 | pVM->hwaccm.s.vmx.pMemObjRealModeTSS = 0;
|
---|
212 | pVM->hwaccm.s.vmx.pRealModeTSS = 0;
|
---|
213 | pVM->hwaccm.s.vmx.pRealModeTSSPhys = 0;
|
---|
214 | }
|
---|
215 | if (pVM->hwaccm.s.vmx.pMemObjAPIC)
|
---|
216 | {
|
---|
217 | RTR0MemObjFree(pVM->hwaccm.s.vmx.pMemObjAPIC, false);
|
---|
218 | pVM->hwaccm.s.vmx.pMemObjAPIC = 0;
|
---|
219 | pVM->hwaccm.s.vmx.pAPIC = 0;
|
---|
220 | pVM->hwaccm.s.vmx.pAPICPhys = 0;
|
---|
221 | }
|
---|
222 | return VINF_SUCCESS;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Sets up VT-x for the specified VM
|
---|
227 | *
|
---|
228 | * @returns VBox status code.
|
---|
229 | * @param pVM The VM to operate on.
|
---|
230 | */
|
---|
231 | HWACCMR0DECL(int) VMXR0SetupVM(PVM pVM)
|
---|
232 | {
|
---|
233 | int rc = VINF_SUCCESS;
|
---|
234 | uint32_t val;
|
---|
235 |
|
---|
236 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
237 | Assert(pVM->hwaccm.s.vmx.pVMCS);
|
---|
238 |
|
---|
239 | /* Set revision dword at the beginning of the VMCS structure. */
|
---|
240 | *(uint32_t *)pVM->hwaccm.s.vmx.pVMCS = MSR_IA32_VMX_BASIC_INFO_VMCS_ID(pVM->hwaccm.s.vmx.msr.vmx_basic_info);
|
---|
241 |
|
---|
242 | /* Clear VM Control Structure. */
|
---|
243 | Log(("pVMCSPhys = %VHp\n", pVM->hwaccm.s.vmx.pVMCSPhys));
|
---|
244 | rc = VMXClearVMCS(pVM->hwaccm.s.vmx.pVMCSPhys);
|
---|
245 | if (VBOX_FAILURE(rc))
|
---|
246 | goto vmx_end;
|
---|
247 |
|
---|
248 | /* Activate the VM Control Structure. */
|
---|
249 | rc = VMXActivateVMCS(pVM->hwaccm.s.vmx.pVMCSPhys);
|
---|
250 | if (VBOX_FAILURE(rc))
|
---|
251 | goto vmx_end;
|
---|
252 |
|
---|
253 | /* VMX_VMCS_CTRL_PIN_EXEC_CONTROLS
|
---|
254 | * Set required bits to one and zero according to the MSR capabilities.
|
---|
255 | */
|
---|
256 | val = (pVM->hwaccm.s.vmx.msr.vmx_pin_ctls & 0xFFFFFFFF);
|
---|
257 | /* External and non-maskable interrupts cause VM-exits. */
|
---|
258 | val = val | VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_EXT_INT_EXIT | VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_NMI_EXIT;
|
---|
259 | val &= (pVM->hwaccm.s.vmx.msr.vmx_pin_ctls >> 32ULL);
|
---|
260 |
|
---|
261 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_PIN_EXEC_CONTROLS, val);
|
---|
262 | AssertRC(rc);
|
---|
263 |
|
---|
264 | /* VMX_VMCS_CTRL_PROC_EXEC_CONTROLS
|
---|
265 | * Set required bits to one and zero according to the MSR capabilities.
|
---|
266 | */
|
---|
267 | val = (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls & 0xFFFFFFFF);
|
---|
268 | /* Program which event cause VM-exits and which features we want to use. */
|
---|
269 | val = val | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_HLT_EXIT
|
---|
270 | | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_TSC_OFFSET
|
---|
271 | | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT
|
---|
272 | | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT
|
---|
273 | | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_UNCOND_IO_EXIT
|
---|
274 | | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MWAIT_EXIT; /* don't execute mwait or else we'll idle inside the guest (host thinks the cpu load is high) */
|
---|
275 |
|
---|
276 | /** @note VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MWAIT_EXIT might cause a vmlaunch failure with an invalid control fields error. (combined with some other exit reasons) */
|
---|
277 |
|
---|
278 | #if HC_ARCH_BITS == 64
|
---|
279 | if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW)
|
---|
280 | {
|
---|
281 | /* CR8 reads from the APIC shadow page; writes cause an exit is they lower the TPR below the threshold */
|
---|
282 | val |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW;
|
---|
283 | }
|
---|
284 | else
|
---|
285 | /* Exit on CR8 reads & writes in case the TPR shadow feature isn't present. */
|
---|
286 | val |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_STORE_EXIT | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_LOAD_EXIT;
|
---|
287 | #endif
|
---|
288 | /* Mask away the bits that the CPU doesn't support */
|
---|
289 | /** @todo make sure they don't conflict with the above requirements. */
|
---|
290 | val &= (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls >> 32ULL);
|
---|
291 | pVM->hwaccm.s.vmx.proc_ctls = val;
|
---|
292 |
|
---|
293 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, val);
|
---|
294 | AssertRC(rc);
|
---|
295 |
|
---|
296 | /* VMX_VMCS_CTRL_CR3_TARGET_COUNT
|
---|
297 | * Set required bits to one and zero according to the MSR capabilities.
|
---|
298 | */
|
---|
299 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_CR3_TARGET_COUNT, 0);
|
---|
300 | AssertRC(rc);
|
---|
301 |
|
---|
302 | /* VMX_VMCS_CTRL_EXIT_CONTROLS
|
---|
303 | * Set required bits to one and zero according to the MSR capabilities.
|
---|
304 | */
|
---|
305 | val = (pVM->hwaccm.s.vmx.msr.vmx_exit & 0xFFFFFFFF);
|
---|
306 | #if HC_ARCH_BITS == 64
|
---|
307 | val |= VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_AMD64;
|
---|
308 | #else
|
---|
309 | /* else Must be zero when AMD64 is not available. */
|
---|
310 | #endif
|
---|
311 | val &= (pVM->hwaccm.s.vmx.msr.vmx_exit >> 32ULL);
|
---|
312 | /* Don't acknowledge external interrupts on VM-exit. */
|
---|
313 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_EXIT_CONTROLS, val);
|
---|
314 | AssertRC(rc);
|
---|
315 |
|
---|
316 | /* Forward all exception except #NM & #PF to the guest.
|
---|
317 | * We always need to check pagefaults since our shadow page table can be out of sync.
|
---|
318 | * And we always lazily sync the FPU & XMM state.
|
---|
319 | */
|
---|
320 |
|
---|
321 | /*
|
---|
322 | * @todo Possible optimization:
|
---|
323 | * Keep the FPU and XMM state current in the EM thread. That way there's no need to
|
---|
324 | * lazily sync anything, but the downside is that we can't use the FPU stack or XMM
|
---|
325 | * registers ourselves of course.
|
---|
326 | *
|
---|
327 | * @note only possible if the current state is actually ours (X86_CR0_TS flag)
|
---|
328 | */
|
---|
329 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_EXCEPTION_BITMAP, HWACCM_VMX_TRAP_MASK);
|
---|
330 | AssertRC(rc);
|
---|
331 |
|
---|
332 | /* Don't filter page faults; all of them should cause a switch. */
|
---|
333 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_PAGEFAULT_ERROR_MASK, 0);
|
---|
334 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_PAGEFAULT_ERROR_MATCH, 0);
|
---|
335 | AssertRC(rc);
|
---|
336 |
|
---|
337 | /* Init TSC offset to zero. */
|
---|
338 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_TSC_OFFSET_FULL, 0);
|
---|
339 | #if HC_ARCH_BITS == 32
|
---|
340 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_TSC_OFFSET_HIGH, 0);
|
---|
341 | #endif
|
---|
342 | AssertRC(rc);
|
---|
343 |
|
---|
344 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_IO_BITMAP_A_FULL, 0);
|
---|
345 | #if HC_ARCH_BITS == 32
|
---|
346 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_IO_BITMAP_A_HIGH, 0);
|
---|
347 | #endif
|
---|
348 | AssertRC(rc);
|
---|
349 |
|
---|
350 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_IO_BITMAP_B_FULL, 0);
|
---|
351 | #if HC_ARCH_BITS == 32
|
---|
352 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_IO_BITMAP_B_HIGH, 0);
|
---|
353 | #endif
|
---|
354 | AssertRC(rc);
|
---|
355 |
|
---|
356 | /* Clear MSR controls. */
|
---|
357 | if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS)
|
---|
358 | {
|
---|
359 | /* Optional */
|
---|
360 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_MSR_BITMAP_FULL, 0);
|
---|
361 | #if HC_ARCH_BITS == 32
|
---|
362 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_MSR_BITMAP_HIGH, 0);
|
---|
363 | #endif
|
---|
364 | AssertRC(rc);
|
---|
365 | }
|
---|
366 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_VMEXIT_MSR_STORE_FULL, 0);
|
---|
367 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_VMEXIT_MSR_LOAD_FULL, 0);
|
---|
368 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_VMENTRY_MSR_LOAD_FULL, 0);
|
---|
369 | #if HC_ARCH_BITS == 32
|
---|
370 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_VMEXIT_MSR_STORE_HIGH, 0);
|
---|
371 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_VMEXIT_MSR_LOAD_HIGH, 0);
|
---|
372 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_VMEXIT_MSR_LOAD_HIGH, 0);
|
---|
373 | #endif
|
---|
374 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_EXIT_MSR_STORE_COUNT, 0);
|
---|
375 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_EXIT_MSR_LOAD_COUNT, 0);
|
---|
376 | AssertRC(rc);
|
---|
377 |
|
---|
378 | if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW)
|
---|
379 | {
|
---|
380 | Assert(pVM->hwaccm.s.vmx.pMemObjAPIC);
|
---|
381 | /* Optional */
|
---|
382 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_TPR_THRESHOLD, 0);
|
---|
383 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_VAPIC_PAGEADDR_FULL, pVM->hwaccm.s.vmx.pAPICPhys);
|
---|
384 | #if HC_ARCH_BITS == 32
|
---|
385 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_VAPIC_PAGEADDR_HIGH, pVM->hwaccm.s.vmx.pAPICPhys >> 32);
|
---|
386 | #endif
|
---|
387 | AssertRC(rc);
|
---|
388 | }
|
---|
389 |
|
---|
390 | /* Set link pointer to -1. Not currently used. */
|
---|
391 | #if HC_ARCH_BITS == 32
|
---|
392 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_LINK_PTR_FULL, 0xFFFFFFFF);
|
---|
393 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_LINK_PTR_HIGH, 0xFFFFFFFF);
|
---|
394 | #else
|
---|
395 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_LINK_PTR_FULL, 0xFFFFFFFFFFFFFFFF);
|
---|
396 | #endif
|
---|
397 | AssertRC(rc);
|
---|
398 |
|
---|
399 | /* Clear VM Control Structure. Marking it inactive, clearing implementation specific data and writing back VMCS data to memory. */
|
---|
400 | rc = VMXClearVMCS(pVM->hwaccm.s.vmx.pVMCSPhys);
|
---|
401 | AssertRC(rc);
|
---|
402 |
|
---|
403 | vmx_end:
|
---|
404 | VMXR0CheckError(pVM, rc);
|
---|
405 | return rc;
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Injects an event (trap or external interrupt)
|
---|
411 | *
|
---|
412 | * @returns VBox status code.
|
---|
413 | * @param pVM The VM to operate on.
|
---|
414 | * @param pCtx CPU Context
|
---|
415 | * @param intInfo VMX interrupt info
|
---|
416 | * @param cbInstr Opcode length of faulting instruction
|
---|
417 | * @param errCode Error code (optional)
|
---|
418 | */
|
---|
419 | static int VMXR0InjectEvent(PVM pVM, CPUMCTX *pCtx, uint32_t intInfo, uint32_t cbInstr, uint32_t errCode)
|
---|
420 | {
|
---|
421 | int rc;
|
---|
422 |
|
---|
423 | #ifdef VBOX_STRICT
|
---|
424 | uint32_t iGate = VMX_EXIT_INTERRUPTION_INFO_VECTOR(intInfo);
|
---|
425 | if (iGate == 0xE)
|
---|
426 | Log2(("VMXR0InjectEvent: Injecting interrupt %d at %VGv error code=%08x CR2=%08x intInfo=%08x\n", iGate, pCtx->rip, errCode, pCtx->cr2, intInfo));
|
---|
427 | else
|
---|
428 | if (iGate < 0x20)
|
---|
429 | Log2(("VMXR0InjectEvent: Injecting interrupt %d at %VGv error code=%08x\n", iGate, pCtx->rip, errCode));
|
---|
430 | else
|
---|
431 | {
|
---|
432 | Log2(("INJ-EI: %x at %VGv\n", iGate, pCtx->rip));
|
---|
433 | Assert(!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS));
|
---|
434 | Assert(pCtx->eflags.u32 & X86_EFL_IF);
|
---|
435 | }
|
---|
436 | #endif
|
---|
437 |
|
---|
438 | /* Set event injection state. */
|
---|
439 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_ENTRY_IRQ_INFO,
|
---|
440 | intInfo | (1 << VMX_EXIT_INTERRUPTION_INFO_VALID_SHIFT)
|
---|
441 | );
|
---|
442 |
|
---|
443 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_ENTRY_INSTR_LENGTH, cbInstr);
|
---|
444 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_ENTRY_EXCEPTION_ERRCODE, errCode);
|
---|
445 |
|
---|
446 | AssertRC(rc);
|
---|
447 | return rc;
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Checks for pending guest interrupts and injects them
|
---|
453 | *
|
---|
454 | * @returns VBox status code.
|
---|
455 | * @param pVM The VM to operate on.
|
---|
456 | * @param pCtx CPU Context
|
---|
457 | */
|
---|
458 | static int VMXR0CheckPendingInterrupt(PVM pVM, CPUMCTX *pCtx)
|
---|
459 | {
|
---|
460 | int rc;
|
---|
461 |
|
---|
462 | /* Dispatch any pending interrupts. (injected before, but a VM exit occurred prematurely) */
|
---|
463 | if (pVM->hwaccm.s.Event.fPending)
|
---|
464 | {
|
---|
465 | Log(("Reinjecting event %VX64 %08x at %VGv\n", pVM->hwaccm.s.Event.intInfo, pVM->hwaccm.s.Event.errCode, pCtx->rip));
|
---|
466 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatIntReinject);
|
---|
467 | rc = VMXR0InjectEvent(pVM, pCtx, pVM->hwaccm.s.Event.intInfo, 0, pVM->hwaccm.s.Event.errCode);
|
---|
468 | AssertRC(rc);
|
---|
469 |
|
---|
470 | pVM->hwaccm.s.Event.fPending = false;
|
---|
471 | return VINF_SUCCESS;
|
---|
472 | }
|
---|
473 |
|
---|
474 | /* When external interrupts are pending, we should exit the VM when IF is set. */
|
---|
475 | if ( !TRPMHasTrap(pVM)
|
---|
476 | && VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)))
|
---|
477 | {
|
---|
478 | if (!(pCtx->eflags.u32 & X86_EFL_IF))
|
---|
479 | {
|
---|
480 | Log2(("Enable irq window exit!\n"));
|
---|
481 | pVM->hwaccm.s.vmx.proc_ctls |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT;
|
---|
482 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVM->hwaccm.s.vmx.proc_ctls);
|
---|
483 | AssertRC(rc);
|
---|
484 | }
|
---|
485 | else
|
---|
486 | if (!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
|
---|
487 | {
|
---|
488 | uint8_t u8Interrupt;
|
---|
489 |
|
---|
490 | rc = PDMGetInterrupt(pVM, &u8Interrupt);
|
---|
491 | Log(("Dispatch interrupt: u8Interrupt=%x (%d) rc=%Vrc\n", u8Interrupt, u8Interrupt, rc));
|
---|
492 | if (VBOX_SUCCESS(rc))
|
---|
493 | {
|
---|
494 | rc = TRPMAssertTrap(pVM, u8Interrupt, TRPM_HARDWARE_INT);
|
---|
495 | AssertRC(rc);
|
---|
496 | }
|
---|
497 | else
|
---|
498 | {
|
---|
499 | /* Can only happen in rare cases where a pending interrupt is cleared behind our back */
|
---|
500 | Assert(!VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)));
|
---|
501 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatSwitchGuestIrq);
|
---|
502 | /* Just continue */
|
---|
503 | }
|
---|
504 | }
|
---|
505 | else
|
---|
506 | Log(("Pending interrupt blocked at %VGv by VM_FF_INHIBIT_INTERRUPTS!!\n", pCtx->rip));
|
---|
507 | }
|
---|
508 |
|
---|
509 | #ifdef VBOX_STRICT
|
---|
510 | if (TRPMHasTrap(pVM))
|
---|
511 | {
|
---|
512 | uint8_t u8Vector;
|
---|
513 | rc = TRPMQueryTrapAll(pVM, &u8Vector, 0, 0, 0);
|
---|
514 | AssertRC(rc);
|
---|
515 | }
|
---|
516 | #endif
|
---|
517 |
|
---|
518 | if ( pCtx->eflags.u32 & X86_EFL_IF
|
---|
519 | && (!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
|
---|
520 | && TRPMHasTrap(pVM)
|
---|
521 | )
|
---|
522 | {
|
---|
523 | uint8_t u8Vector;
|
---|
524 | int rc;
|
---|
525 | TRPMEVENT enmType;
|
---|
526 | RTGCUINTPTR intInfo;
|
---|
527 | RTGCUINT errCode;
|
---|
528 |
|
---|
529 | /* If a new event is pending, then dispatch it now. */
|
---|
530 | rc = TRPMQueryTrapAll(pVM, &u8Vector, &enmType, &errCode, 0);
|
---|
531 | AssertRC(rc);
|
---|
532 | Assert(pCtx->eflags.Bits.u1IF == 1 || enmType == TRPM_TRAP);
|
---|
533 | Assert(enmType != TRPM_SOFTWARE_INT);
|
---|
534 |
|
---|
535 | /* Clear the pending trap. */
|
---|
536 | rc = TRPMResetTrap(pVM);
|
---|
537 | AssertRC(rc);
|
---|
538 |
|
---|
539 | intInfo = u8Vector;
|
---|
540 | intInfo |= (1 << VMX_EXIT_INTERRUPTION_INFO_VALID_SHIFT);
|
---|
541 |
|
---|
542 | if (enmType == TRPM_TRAP)
|
---|
543 | {
|
---|
544 | switch (u8Vector) {
|
---|
545 | case 8:
|
---|
546 | case 10:
|
---|
547 | case 11:
|
---|
548 | case 12:
|
---|
549 | case 13:
|
---|
550 | case 14:
|
---|
551 | case 17:
|
---|
552 | /* Valid error codes. */
|
---|
553 | intInfo |= VMX_EXIT_INTERRUPTION_INFO_ERROR_CODE_VALID;
|
---|
554 | break;
|
---|
555 | default:
|
---|
556 | break;
|
---|
557 | }
|
---|
558 | if (u8Vector == X86_XCPT_BP || u8Vector == X86_XCPT_OF)
|
---|
559 | intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_SWEXCPT << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
|
---|
560 | else
|
---|
561 | intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_HWEXCPT << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
|
---|
562 | }
|
---|
563 | else
|
---|
564 | intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_EXT << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
|
---|
565 |
|
---|
566 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatIntInject);
|
---|
567 | rc = VMXR0InjectEvent(pVM, pCtx, intInfo, 0, errCode);
|
---|
568 | AssertRC(rc);
|
---|
569 | } /* if (interrupts can be dispatched) */
|
---|
570 |
|
---|
571 | return VINF_SUCCESS;
|
---|
572 | }
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Save the host state
|
---|
576 | *
|
---|
577 | * @returns VBox status code.
|
---|
578 | * @param pVM The VM to operate on.
|
---|
579 | */
|
---|
580 | HWACCMR0DECL(int) VMXR0SaveHostState(PVM pVM)
|
---|
581 | {
|
---|
582 | int rc = VINF_SUCCESS;
|
---|
583 |
|
---|
584 | /*
|
---|
585 | * Host CPU Context
|
---|
586 | */
|
---|
587 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_HOST_CONTEXT)
|
---|
588 | {
|
---|
589 | RTIDTR idtr;
|
---|
590 | RTGDTR gdtr;
|
---|
591 | RTSEL SelTR;
|
---|
592 | PX86DESCHC pDesc;
|
---|
593 | uintptr_t trBase;
|
---|
594 |
|
---|
595 | /* Control registers */
|
---|
596 | rc = VMXWriteVMCS(VMX_VMCS_HOST_CR0, ASMGetCR0());
|
---|
597 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_CR3, ASMGetCR3());
|
---|
598 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_CR4, ASMGetCR4());
|
---|
599 | AssertRC(rc);
|
---|
600 | Log2(("VMX_VMCS_HOST_CR0 %08x\n", ASMGetCR0()));
|
---|
601 | Log2(("VMX_VMCS_HOST_CR3 %VHp\n", ASMGetCR3()));
|
---|
602 | Log2(("VMX_VMCS_HOST_CR4 %08x\n", ASMGetCR4()));
|
---|
603 |
|
---|
604 | /* Selector registers. */
|
---|
605 | rc = VMXWriteVMCS(VMX_VMCS_HOST_FIELD_CS, ASMGetCS());
|
---|
606 | /** @note VMX is (again) very picky about the RPL of the selectors here; we'll restore them manually. */
|
---|
607 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_FIELD_DS, 0);
|
---|
608 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_FIELD_ES, 0);
|
---|
609 | #if HC_ARCH_BITS == 32
|
---|
610 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_FIELD_FS, 0);
|
---|
611 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_FIELD_GS, 0);
|
---|
612 | #endif
|
---|
613 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_FIELD_SS, ASMGetSS());
|
---|
614 | SelTR = ASMGetTR();
|
---|
615 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_FIELD_TR, SelTR);
|
---|
616 | AssertRC(rc);
|
---|
617 | Log2(("VMX_VMCS_HOST_FIELD_CS %08x\n", ASMGetCS()));
|
---|
618 | Log2(("VMX_VMCS_HOST_FIELD_DS %08x\n", ASMGetDS()));
|
---|
619 | Log2(("VMX_VMCS_HOST_FIELD_ES %08x\n", ASMGetES()));
|
---|
620 | Log2(("VMX_VMCS_HOST_FIELD_FS %08x\n", ASMGetFS()));
|
---|
621 | Log2(("VMX_VMCS_HOST_FIELD_GS %08x\n", ASMGetGS()));
|
---|
622 | Log2(("VMX_VMCS_HOST_FIELD_SS %08x\n", ASMGetSS()));
|
---|
623 | Log2(("VMX_VMCS_HOST_FIELD_TR %08x\n", ASMGetTR()));
|
---|
624 |
|
---|
625 | /* GDTR & IDTR */
|
---|
626 | ASMGetGDTR(&gdtr);
|
---|
627 | rc = VMXWriteVMCS(VMX_VMCS_HOST_GDTR_BASE, gdtr.pGdt);
|
---|
628 | ASMGetIDTR(&idtr);
|
---|
629 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_IDTR_BASE, idtr.pIdt);
|
---|
630 | AssertRC(rc);
|
---|
631 | Log2(("VMX_VMCS_HOST_GDTR_BASE %VHv\n", gdtr.pGdt));
|
---|
632 | Log2(("VMX_VMCS_HOST_IDTR_BASE %VHv\n", idtr.pIdt));
|
---|
633 |
|
---|
634 | /* Save the base address of the TR selector. */
|
---|
635 | if (SelTR > gdtr.cbGdt)
|
---|
636 | {
|
---|
637 | AssertMsgFailed(("Invalid TR selector %x. GDTR.cbGdt=%x\n", SelTR, gdtr.cbGdt));
|
---|
638 | return VERR_VMX_INVALID_HOST_STATE;
|
---|
639 | }
|
---|
640 |
|
---|
641 | pDesc = &((PX86DESCHC)gdtr.pGdt)[SelTR >> X86_SEL_SHIFT_HC];
|
---|
642 | #if HC_ARCH_BITS == 64
|
---|
643 | trBase = X86DESC64_BASE(*pDesc);
|
---|
644 | #else
|
---|
645 | trBase = X86DESC_BASE(*pDesc);
|
---|
646 | #endif
|
---|
647 | rc = VMXWriteVMCS(VMX_VMCS_HOST_TR_BASE, trBase);
|
---|
648 | AssertRC(rc);
|
---|
649 | Log2(("VMX_VMCS_HOST_TR_BASE %VHv\n", trBase));
|
---|
650 |
|
---|
651 | /* FS and GS base. */
|
---|
652 | #if HC_ARCH_BITS == 64
|
---|
653 | Log2(("MSR_K8_FS_BASE = %VHv\n", ASMRdMsr(MSR_K8_FS_BASE)));
|
---|
654 | Log2(("MSR_K8_GS_BASE = %VHv\n", ASMRdMsr(MSR_K8_GS_BASE)));
|
---|
655 | rc = VMXWriteVMCS64(VMX_VMCS_HOST_FS_BASE, ASMRdMsr(MSR_K8_FS_BASE));
|
---|
656 | rc |= VMXWriteVMCS64(VMX_VMCS_HOST_GS_BASE, ASMRdMsr(MSR_K8_GS_BASE));
|
---|
657 | #endif
|
---|
658 | AssertRC(rc);
|
---|
659 |
|
---|
660 | /* Sysenter MSRs. */
|
---|
661 | /** @todo expensive!! */
|
---|
662 | rc = VMXWriteVMCS(VMX_VMCS_HOST_SYSENTER_CS, ASMRdMsr_Low(MSR_IA32_SYSENTER_CS));
|
---|
663 | Log2(("VMX_VMCS_HOST_SYSENTER_CS %08x\n", ASMRdMsr_Low(MSR_IA32_SYSENTER_CS)));
|
---|
664 | #if HC_ARCH_BITS == 32
|
---|
665 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_SYSENTER_ESP, ASMRdMsr_Low(MSR_IA32_SYSENTER_ESP));
|
---|
666 | rc |= VMXWriteVMCS(VMX_VMCS_HOST_SYSENTER_EIP, ASMRdMsr_Low(MSR_IA32_SYSENTER_EIP));
|
---|
667 | Log2(("VMX_VMCS_HOST_SYSENTER_EIP %VHv\n", ASMRdMsr_Low(MSR_IA32_SYSENTER_EIP)));
|
---|
668 | Log2(("VMX_VMCS_HOST_SYSENTER_ESP %VHv\n", ASMRdMsr_Low(MSR_IA32_SYSENTER_ESP)));
|
---|
669 | #else
|
---|
670 | Log2(("VMX_VMCS_HOST_SYSENTER_EIP %VHv\n", ASMRdMsr(MSR_IA32_SYSENTER_EIP)));
|
---|
671 | Log2(("VMX_VMCS_HOST_SYSENTER_ESP %VHv\n", ASMRdMsr(MSR_IA32_SYSENTER_ESP)));
|
---|
672 | rc |= VMXWriteVMCS64(VMX_VMCS_HOST_SYSENTER_ESP, ASMRdMsr(MSR_IA32_SYSENTER_ESP));
|
---|
673 | rc |= VMXWriteVMCS64(VMX_VMCS_HOST_SYSENTER_EIP, ASMRdMsr(MSR_IA32_SYSENTER_EIP));
|
---|
674 | #endif
|
---|
675 | AssertRC(rc);
|
---|
676 |
|
---|
677 | pVM->hwaccm.s.fContextUseFlags &= ~HWACCM_CHANGED_HOST_CONTEXT;
|
---|
678 | }
|
---|
679 | return rc;
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Loads the guest state
|
---|
685 | *
|
---|
686 | * NOTE: Don't do anything here that can cause a jump back to ring 3!!!!!
|
---|
687 | *
|
---|
688 | * @returns VBox status code.
|
---|
689 | * @param pVM The VM to operate on.
|
---|
690 | * @param pCtx Guest context
|
---|
691 | */
|
---|
692 | HWACCMR0DECL(int) VMXR0LoadGuestState(PVM pVM, CPUMCTX *pCtx)
|
---|
693 | {
|
---|
694 | int rc = VINF_SUCCESS;
|
---|
695 | RTGCUINTPTR val;
|
---|
696 | X86EFLAGS eflags;
|
---|
697 |
|
---|
698 | /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
|
---|
699 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_SEGMENT_REGS)
|
---|
700 | {
|
---|
701 | VMX_WRITE_SELREG(ES, es);
|
---|
702 | AssertRC(rc);
|
---|
703 |
|
---|
704 | VMX_WRITE_SELREG(CS, cs);
|
---|
705 | AssertRC(rc);
|
---|
706 |
|
---|
707 | VMX_WRITE_SELREG(SS, ss);
|
---|
708 | AssertRC(rc);
|
---|
709 |
|
---|
710 | VMX_WRITE_SELREG(DS, ds);
|
---|
711 | AssertRC(rc);
|
---|
712 |
|
---|
713 | /* The base values in the hidden fs & gs registers are not in sync with the msrs; they are cut to 32 bits. */
|
---|
714 | VMX_WRITE_SELREG(FS, fs);
|
---|
715 | AssertRC(rc);
|
---|
716 |
|
---|
717 | VMX_WRITE_SELREG(GS, gs);
|
---|
718 | AssertRC(rc);
|
---|
719 | }
|
---|
720 |
|
---|
721 | /* Guest CPU context: LDTR. */
|
---|
722 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_LDTR)
|
---|
723 | {
|
---|
724 | if (pCtx->ldtr == 0)
|
---|
725 | {
|
---|
726 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_FIELD_LDTR, 0);
|
---|
727 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_LDTR_LIMIT, 0);
|
---|
728 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_LDTR_BASE, 0);
|
---|
729 | /** @note vmlaunch will fail with 0 or just 0x02. No idea why. */
|
---|
730 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_LDTR_ACCESS_RIGHTS, 0x82 /* present, LDT */);
|
---|
731 | }
|
---|
732 | else
|
---|
733 | {
|
---|
734 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_FIELD_LDTR, pCtx->ldtr);
|
---|
735 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_LDTR_LIMIT, pCtx->ldtrHid.u32Limit);
|
---|
736 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_LDTR_BASE, pCtx->ldtrHid.u64Base);
|
---|
737 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_LDTR_ACCESS_RIGHTS, pCtx->ldtrHid.Attr.u);
|
---|
738 | }
|
---|
739 | AssertRC(rc);
|
---|
740 | }
|
---|
741 | /* Guest CPU context: TR. */
|
---|
742 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_TR)
|
---|
743 | {
|
---|
744 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_FIELD_TR, pCtx->tr);
|
---|
745 |
|
---|
746 | /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
|
---|
747 | if (!(pCtx->cr0 & X86_CR0_PROTECTION_ENABLE))
|
---|
748 | {
|
---|
749 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_TR_LIMIT, sizeof(*pVM->hwaccm.s.vmx.pRealModeTSS));
|
---|
750 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_TR_BASE, 0);
|
---|
751 | }
|
---|
752 | else
|
---|
753 | {
|
---|
754 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_TR_LIMIT, pCtx->trHid.u32Limit);
|
---|
755 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_TR_BASE, pCtx->trHid.u64Base);
|
---|
756 | }
|
---|
757 | val = pCtx->trHid.Attr.u;
|
---|
758 |
|
---|
759 | /* The TSS selector must be busy. */
|
---|
760 | if ((val & 0xF) == X86_SEL_TYPE_SYS_286_TSS_AVAIL)
|
---|
761 | val = (val & ~0xF) | X86_SEL_TYPE_SYS_286_TSS_BUSY;
|
---|
762 | else
|
---|
763 | /* Default even if no TR selector has been set (otherwise vmlaunch will fail!) */
|
---|
764 | val = (val & ~0xF) | X86_SEL_TYPE_SYS_386_TSS_BUSY;
|
---|
765 |
|
---|
766 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_TR_ACCESS_RIGHTS, val);
|
---|
767 | AssertRC(rc);
|
---|
768 | }
|
---|
769 | /* Guest CPU context: GDTR. */
|
---|
770 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_GDTR)
|
---|
771 | {
|
---|
772 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_GDTR_LIMIT, pCtx->gdtr.cbGdt);
|
---|
773 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_GDTR_BASE, pCtx->gdtr.pGdt);
|
---|
774 | AssertRC(rc);
|
---|
775 | }
|
---|
776 | /* Guest CPU context: IDTR. */
|
---|
777 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_IDTR)
|
---|
778 | {
|
---|
779 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_IDTR_LIMIT, pCtx->idtr.cbIdt);
|
---|
780 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_IDTR_BASE, pCtx->idtr.pIdt);
|
---|
781 | AssertRC(rc);
|
---|
782 | }
|
---|
783 |
|
---|
784 | /*
|
---|
785 | * Sysenter MSRs (unconditional)
|
---|
786 | */
|
---|
787 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_SYSENTER_CS, pCtx->SysEnter.cs);
|
---|
788 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_SYSENTER_EIP, pCtx->SysEnter.eip);
|
---|
789 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_SYSENTER_ESP, pCtx->SysEnter.esp);
|
---|
790 | AssertRC(rc);
|
---|
791 |
|
---|
792 | /* Control registers */
|
---|
793 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR0)
|
---|
794 | {
|
---|
795 | val = pCtx->cr0;
|
---|
796 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_CR0_READ_SHADOW, val);
|
---|
797 | Log2(("Guest CR0-shadow %08x\n", val));
|
---|
798 | if (CPUMIsGuestFPUStateActive(pVM) == false)
|
---|
799 | {
|
---|
800 | /* Always use #NM exceptions to load the FPU/XMM state on demand. */
|
---|
801 | val |= X86_CR0_TS | X86_CR0_ET | X86_CR0_NE | X86_CR0_MP;
|
---|
802 | }
|
---|
803 | else
|
---|
804 | {
|
---|
805 | /** @todo check if we support the old style mess correctly. */
|
---|
806 | if (!(val & X86_CR0_NE))
|
---|
807 | {
|
---|
808 | Log(("Forcing X86_CR0_NE!!!\n"));
|
---|
809 |
|
---|
810 | /* Also catch floating point exceptions as we need to report them to the guest in a different way. */
|
---|
811 | if (!pVM->hwaccm.s.fFPUOldStyleOverride)
|
---|
812 | {
|
---|
813 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_EXCEPTION_BITMAP, HWACCM_VMX_TRAP_MASK | RT_BIT(X86_XCPT_MF));
|
---|
814 | AssertRC(rc);
|
---|
815 | pVM->hwaccm.s.fFPUOldStyleOverride = true;
|
---|
816 | }
|
---|
817 | }
|
---|
818 |
|
---|
819 | val |= X86_CR0_NE; /* always turn on the native mechanism to report FPU errors (old style uses interrupts) */
|
---|
820 | }
|
---|
821 | /* Note: protected mode & paging are always enabled; we use them for emulating real and protected mode without paging too. */
|
---|
822 | val |= X86_CR0_PE | X86_CR0_PG;
|
---|
823 | /* Note: We must also set this as we rely on protecting various pages for which supervisor writes must be caught. */
|
---|
824 | val |= X86_CR0_WP;
|
---|
825 |
|
---|
826 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_CR0, val);
|
---|
827 | Log2(("Guest CR0 %08x\n", val));
|
---|
828 | /* CR0 flags owned by the host; if the guests attempts to change them, then
|
---|
829 | * the VM will exit.
|
---|
830 | */
|
---|
831 | val = X86_CR0_PE /* Must monitor this bit (assumptions are made for real mode emulation) */
|
---|
832 | | X86_CR0_WP /* Must monitor this bit (it must always be enabled). */
|
---|
833 | | X86_CR0_PG /* Must monitor this bit (assumptions are made for real mode & protected mode without paging emulation) */
|
---|
834 | | X86_CR0_TS
|
---|
835 | | X86_CR0_ET
|
---|
836 | | X86_CR0_NE
|
---|
837 | | X86_CR0_MP;
|
---|
838 | pVM->hwaccm.s.vmx.cr0_mask = val;
|
---|
839 |
|
---|
840 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_CR0_MASK, val);
|
---|
841 | Log2(("Guest CR0-mask %08x\n", val));
|
---|
842 | AssertRC(rc);
|
---|
843 | }
|
---|
844 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR4)
|
---|
845 | {
|
---|
846 | /* CR4 */
|
---|
847 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_CR4_READ_SHADOW, pCtx->cr4);
|
---|
848 | Log2(("Guest CR4-shadow %08x\n", pCtx->cr4));
|
---|
849 | /* Set the required bits in cr4 too (currently X86_CR4_VMXE). */
|
---|
850 | val = pCtx->cr4 | (uint32_t)pVM->hwaccm.s.vmx.msr.vmx_cr4_fixed0;
|
---|
851 | switch(pVM->hwaccm.s.enmShadowMode)
|
---|
852 | {
|
---|
853 | case PGMMODE_REAL: /* Real mode -> emulated using v86 mode */
|
---|
854 | case PGMMODE_PROTECTED: /* Protected mode, no paging -> emulated using identity mapping. */
|
---|
855 | case PGMMODE_32_BIT: /* 32-bit paging. */
|
---|
856 | break;
|
---|
857 |
|
---|
858 | case PGMMODE_PAE: /* PAE paging. */
|
---|
859 | case PGMMODE_PAE_NX: /* PAE paging with NX enabled. */
|
---|
860 | /** @todo use normal 32 bits paging */
|
---|
861 | val |= X86_CR4_PAE;
|
---|
862 | break;
|
---|
863 |
|
---|
864 | case PGMMODE_AMD64: /* 64-bit AMD paging (long mode). */
|
---|
865 | case PGMMODE_AMD64_NX: /* 64-bit AMD paging (long mode) with NX enabled. */
|
---|
866 | #ifdef VBOX_ENABLE_64_BITS_GUESTS
|
---|
867 | break;
|
---|
868 | #else
|
---|
869 | AssertFailed();
|
---|
870 | return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
|
---|
871 | #endif
|
---|
872 | default: /* shut up gcc */
|
---|
873 | AssertFailed();
|
---|
874 | return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
|
---|
875 | }
|
---|
876 | /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
|
---|
877 | if (!(pCtx->cr0 & X86_CR0_PROTECTION_ENABLE))
|
---|
878 | val |= X86_CR4_VME;
|
---|
879 |
|
---|
880 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_CR4, val);
|
---|
881 | Log2(("Guest CR4 %08x\n", val));
|
---|
882 | /* CR4 flags owned by the host; if the guests attempts to change them, then
|
---|
883 | * the VM will exit.
|
---|
884 | */
|
---|
885 | val = X86_CR4_PAE
|
---|
886 | | X86_CR4_PGE
|
---|
887 | | X86_CR4_PSE
|
---|
888 | | X86_CR4_VMXE;
|
---|
889 | pVM->hwaccm.s.vmx.cr4_mask = val;
|
---|
890 |
|
---|
891 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_CR4_MASK, val);
|
---|
892 | Log2(("Guest CR4-mask %08x\n", val));
|
---|
893 | AssertRC(rc);
|
---|
894 | }
|
---|
895 |
|
---|
896 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR3)
|
---|
897 | {
|
---|
898 | /* Save our shadow CR3 register. */
|
---|
899 | val = PGMGetHyperCR3(pVM);
|
---|
900 | Assert(val);
|
---|
901 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_CR3, val);
|
---|
902 | AssertRC(rc);
|
---|
903 | }
|
---|
904 |
|
---|
905 | /* Debug registers. */
|
---|
906 | if (pVM->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_DEBUG)
|
---|
907 | {
|
---|
908 | /** @todo DR0-6 */
|
---|
909 | val = pCtx->dr7;
|
---|
910 | val &= ~(RT_BIT(11) | RT_BIT(12) | RT_BIT(14) | RT_BIT(15)); /* must be zero */
|
---|
911 | val |= 0x400; /* must be one */
|
---|
912 | #ifdef VBOX_STRICT
|
---|
913 | val = 0x400;
|
---|
914 | #endif
|
---|
915 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_DR7, val);
|
---|
916 | AssertRC(rc);
|
---|
917 |
|
---|
918 | /* IA32_DEBUGCTL MSR. */
|
---|
919 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_DEBUGCTL_FULL, 0);
|
---|
920 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_DEBUGCTL_HIGH, 0);
|
---|
921 | AssertRC(rc);
|
---|
922 |
|
---|
923 | /** @todo */
|
---|
924 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_DEBUG_EXCEPTIONS, 0);
|
---|
925 | AssertRC(rc);
|
---|
926 | }
|
---|
927 |
|
---|
928 | /* EIP, ESP and EFLAGS */
|
---|
929 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_RIP, pCtx->rip);
|
---|
930 | rc |= VMXWriteVMCS(VMX_VMCS_GUEST_RSP, pCtx->rsp);
|
---|
931 | AssertRC(rc);
|
---|
932 |
|
---|
933 | /* Bits 22-31, 15, 5 & 3 must be zero. Bit 1 must be 1. */
|
---|
934 | eflags = pCtx->eflags;
|
---|
935 | eflags.u32 &= VMX_EFLAGS_RESERVED_0;
|
---|
936 | eflags.u32 |= VMX_EFLAGS_RESERVED_1;
|
---|
937 |
|
---|
938 | /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
|
---|
939 | if (!(pCtx->cr0 & X86_CR0_PROTECTION_ENABLE))
|
---|
940 | {
|
---|
941 | eflags.Bits.u1VM = 1;
|
---|
942 | eflags.Bits.u1VIF = pCtx->eflags.Bits.u1IF;
|
---|
943 | eflags.Bits.u2IOPL = 3;
|
---|
944 | }
|
---|
945 |
|
---|
946 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_RFLAGS, eflags.u32);
|
---|
947 | AssertRC(rc);
|
---|
948 |
|
---|
949 | /** TSC offset. */
|
---|
950 | uint64_t u64TSCOffset;
|
---|
951 |
|
---|
952 | if (TMCpuTickCanUseRealTSC(pVM, &u64TSCOffset))
|
---|
953 | {
|
---|
954 | /* Note: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT takes precedence over TSC_OFFSET */
|
---|
955 | #if HC_ARCH_BITS == 64
|
---|
956 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_TSC_OFFSET_FULL, u64TSCOffset);
|
---|
957 | #else
|
---|
958 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_TSC_OFFSET_FULL, (uint32_t)u64TSCOffset);
|
---|
959 | rc |= VMXWriteVMCS(VMX_VMCS_CTRL_TSC_OFFSET_HIGH, (uint32_t)(u64TSCOffset >> 32ULL));
|
---|
960 | #endif
|
---|
961 | AssertRC(rc);
|
---|
962 |
|
---|
963 | pVM->hwaccm.s.vmx.proc_ctls &= ~VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT;
|
---|
964 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVM->hwaccm.s.vmx.proc_ctls);
|
---|
965 | AssertRC(rc);
|
---|
966 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatTSCOffset);
|
---|
967 | }
|
---|
968 | else
|
---|
969 | {
|
---|
970 | pVM->hwaccm.s.vmx.proc_ctls |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT;
|
---|
971 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVM->hwaccm.s.vmx.proc_ctls);
|
---|
972 | AssertRC(rc);
|
---|
973 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatTSCIntercept);
|
---|
974 | }
|
---|
975 |
|
---|
976 | /* VMX_VMCS_CTRL_ENTRY_CONTROLS
|
---|
977 | * Set required bits to one and zero according to the MSR capabilities.
|
---|
978 | */
|
---|
979 | val = (pVM->hwaccm.s.vmx.msr.vmx_entry & 0xFFFFFFFF);
|
---|
980 | /* 64 bits guest mode? */
|
---|
981 | if (pCtx->msrEFER & MSR_K6_EFER_LMA)
|
---|
982 | val |= VMX_VMCS_CTRL_ENTRY_CONTROLS_IA64_MODE;
|
---|
983 | /* else Must be zero when AMD64 is not available. */
|
---|
984 |
|
---|
985 | /* Mask away the bits that the CPU doesn't support */
|
---|
986 | val &= (pVM->hwaccm.s.vmx.msr.vmx_entry >> 32ULL);
|
---|
987 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_ENTRY_CONTROLS, val);
|
---|
988 | AssertRC(rc);
|
---|
989 |
|
---|
990 | /* 64 bits guest mode? */
|
---|
991 | if (pCtx->msrEFER & MSR_K6_EFER_LMA)
|
---|
992 | {
|
---|
993 | #if !defined(VBOX_WITH_64_BITS_GUESTS) || HC_ARCH_BITS != 64
|
---|
994 | return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
|
---|
995 | #else
|
---|
996 | pVM->hwaccm.s.vmx.pfnStartVM = VMXR0StartVM64;
|
---|
997 | #endif
|
---|
998 | /* Unconditionally update these as wrmsr might have changed them. */
|
---|
999 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_FS_BASE, pCtx->fsHid.u64Base);
|
---|
1000 | AssertRC(rc);
|
---|
1001 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_GS_BASE, pCtx->gsHid.u64Base);
|
---|
1002 | AssertRC(rc);
|
---|
1003 | }
|
---|
1004 | else
|
---|
1005 | {
|
---|
1006 | pVM->hwaccm.s.vmx.pfnStartVM = VMXR0StartVM32;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | /* Done. */
|
---|
1010 | pVM->hwaccm.s.fContextUseFlags &= ~HWACCM_CHANGED_ALL_GUEST;
|
---|
1011 |
|
---|
1012 | return rc;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | /**
|
---|
1016 | * Runs guest code in a VT-x VM.
|
---|
1017 | *
|
---|
1018 | * @note NEVER EVER turn on interrupts here. Due to our illegal entry into the kernel, it might mess things up. (XP kernel traps have been frequently observed)
|
---|
1019 | *
|
---|
1020 | * @returns VBox status code.
|
---|
1021 | * @param pVM The VM to operate on.
|
---|
1022 | * @param pCtx Guest context
|
---|
1023 | * @param pCpu CPU info struct
|
---|
1024 | */
|
---|
1025 | HWACCMR0DECL(int) VMXR0RunGuestCode(PVM pVM, CPUMCTX *pCtx, PHWACCM_CPUINFO pCpu)
|
---|
1026 | {
|
---|
1027 | int rc = VINF_SUCCESS;
|
---|
1028 | RTCCUINTREG val, valShadow;
|
---|
1029 | RTCCUINTREG exitReason, instrError, cbInstr;
|
---|
1030 | RTGCUINTPTR exitQualification;
|
---|
1031 | RTGCUINTPTR intInfo = 0; /* shut up buggy gcc 4 */
|
---|
1032 | RTGCUINTPTR errCode, instrInfo, uInterruptState;
|
---|
1033 | bool fGuestStateSynced = false;
|
---|
1034 | unsigned cResume = 0;
|
---|
1035 |
|
---|
1036 | Log2(("\nE"));
|
---|
1037 |
|
---|
1038 | AssertReturn(pCpu->fConfigured, VERR_EM_INTERNAL_ERROR);
|
---|
1039 |
|
---|
1040 | STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatEntry, x);
|
---|
1041 |
|
---|
1042 | #ifdef VBOX_STRICT
|
---|
1043 | rc = VMXReadVMCS(VMX_VMCS_CTRL_PIN_EXEC_CONTROLS, &val);
|
---|
1044 | AssertRC(rc);
|
---|
1045 | Log2(("VMX_VMCS_CTRL_PIN_EXEC_CONTROLS = %08x\n", val));
|
---|
1046 |
|
---|
1047 | /* allowed zero */
|
---|
1048 | if ((val & (pVM->hwaccm.s.vmx.msr.vmx_pin_ctls & 0xFFFFFFFF)) != (pVM->hwaccm.s.vmx.msr.vmx_pin_ctls & 0xFFFFFFFF))
|
---|
1049 | Log(("Invalid VMX_VMCS_CTRL_PIN_EXEC_CONTROLS: zero\n"));
|
---|
1050 |
|
---|
1051 | /* allowed one */
|
---|
1052 | if ((val & ~(pVM->hwaccm.s.vmx.msr.vmx_pin_ctls >> 32ULL)) != 0)
|
---|
1053 | Log(("Invalid VMX_VMCS_CTRL_PIN_EXEC_CONTROLS: one\n"));
|
---|
1054 |
|
---|
1055 | rc = VMXReadVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, &val);
|
---|
1056 | AssertRC(rc);
|
---|
1057 | Log2(("VMX_VMCS_CTRL_PROC_EXEC_CONTROLS = %08x\n", val));
|
---|
1058 |
|
---|
1059 | /* allowed zero */
|
---|
1060 | if ((val & (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls & 0xFFFFFFFF)) != (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls & 0xFFFFFFFF))
|
---|
1061 | Log(("Invalid VMX_VMCS_CTRL_PROC_EXEC_CONTROLS: zero\n"));
|
---|
1062 |
|
---|
1063 | /* allowed one */
|
---|
1064 | if ((val & ~(pVM->hwaccm.s.vmx.msr.vmx_proc_ctls >> 32ULL)) != 0)
|
---|
1065 | Log(("Invalid VMX_VMCS_CTRL_PROC_EXEC_CONTROLS: one\n"));
|
---|
1066 |
|
---|
1067 | rc = VMXReadVMCS(VMX_VMCS_CTRL_ENTRY_CONTROLS, &val);
|
---|
1068 | AssertRC(rc);
|
---|
1069 | Log2(("VMX_VMCS_CTRL_ENTRY_CONTROLS = %08x\n", val));
|
---|
1070 |
|
---|
1071 | /* allowed zero */
|
---|
1072 | if ((val & (pVM->hwaccm.s.vmx.msr.vmx_entry & 0xFFFFFFFF)) != (pVM->hwaccm.s.vmx.msr.vmx_entry & 0xFFFFFFFF))
|
---|
1073 | Log(("Invalid VMX_VMCS_CTRL_ENTRY_CONTROLS: zero\n"));
|
---|
1074 |
|
---|
1075 | /* allowed one */
|
---|
1076 | if ((val & ~(pVM->hwaccm.s.vmx.msr.vmx_entry >> 32ULL)) != 0)
|
---|
1077 | Log(("Invalid VMX_VMCS_CTRL_ENTRY_CONTROLS: one\n"));
|
---|
1078 |
|
---|
1079 | rc = VMXReadVMCS(VMX_VMCS_CTRL_EXIT_CONTROLS, &val);
|
---|
1080 | AssertRC(rc);
|
---|
1081 | Log2(("VMX_VMCS_CTRL_EXIT_CONTROLS = %08x\n", val));
|
---|
1082 |
|
---|
1083 | /* allowed zero */
|
---|
1084 | if ((val & (pVM->hwaccm.s.vmx.msr.vmx_exit & 0xFFFFFFFF)) != (pVM->hwaccm.s.vmx.msr.vmx_exit & 0xFFFFFFFF))
|
---|
1085 | Log(("Invalid VMX_VMCS_CTRL_EXIT_CONTROLS: zero\n"));
|
---|
1086 |
|
---|
1087 | /* allowed one */
|
---|
1088 | if ((val & ~(pVM->hwaccm.s.vmx.msr.vmx_exit >> 32ULL)) != 0)
|
---|
1089 | Log(("Invalid VMX_VMCS_CTRL_EXIT_CONTROLS: one\n"));
|
---|
1090 | #endif
|
---|
1091 |
|
---|
1092 | #if 0
|
---|
1093 | /*
|
---|
1094 | * Check if debug registers are armed.
|
---|
1095 | */
|
---|
1096 | uint32_t u32DR7 = ASMGetDR7();
|
---|
1097 | if (u32DR7 & X86_DR7_ENABLED_MASK)
|
---|
1098 | {
|
---|
1099 | pVM->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS_HOST;
|
---|
1100 | }
|
---|
1101 | else
|
---|
1102 | pVM->cpum.s.fUseFlags &= ~CPUM_USE_DEBUG_REGS_HOST;
|
---|
1103 | #endif
|
---|
1104 |
|
---|
1105 | /* We can jump to this point to resume execution after determining that a VM-exit is innocent.
|
---|
1106 | */
|
---|
1107 | ResumeExecution:
|
---|
1108 | /* Safety precaution; looping for too long here can have a very bad effect on the host */
|
---|
1109 | if (++cResume > HWACCM_MAX_RESUME_LOOPS)
|
---|
1110 | {
|
---|
1111 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitMaxResume);
|
---|
1112 | rc = VINF_EM_RAW_INTERRUPT;
|
---|
1113 | goto end;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | /* Check for irq inhibition due to instruction fusing (sti, mov ss). */
|
---|
1117 | if (VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
|
---|
1118 | {
|
---|
1119 | Log(("VM_FF_INHIBIT_INTERRUPTS at %VGv successor %VGv\n", pCtx->rip, EMGetInhibitInterruptsPC(pVM)));
|
---|
1120 | if (pCtx->rip != EMGetInhibitInterruptsPC(pVM))
|
---|
1121 | {
|
---|
1122 | /** @note we intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here.
|
---|
1123 | * Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
|
---|
1124 | * force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
|
---|
1125 | * break the guest. Sounds very unlikely, but such timing sensitive problem are not as rare as you might think.
|
---|
1126 | */
|
---|
1127 | VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
|
---|
1128 | /* Irq inhibition is no longer active; clear the corresponding VMX state. */
|
---|
1129 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_INTERRUPTIBILITY_STATE, 0);
|
---|
1130 | AssertRC(rc);
|
---|
1131 | }
|
---|
1132 | }
|
---|
1133 | else
|
---|
1134 | {
|
---|
1135 | /* Irq inhibition is no longer active; clear the corresponding VMX state. */
|
---|
1136 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_INTERRUPTIBILITY_STATE, 0);
|
---|
1137 | AssertRC(rc);
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /* Check for pending actions that force us to go back to ring 3. */
|
---|
1141 | if (VM_FF_ISPENDING(pVM, VM_FF_TO_R3 | VM_FF_TIMER))
|
---|
1142 | {
|
---|
1143 | VM_FF_CLEAR(pVM, VM_FF_TO_R3);
|
---|
1144 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatSwitchToR3);
|
---|
1145 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
|
---|
1146 | rc = VINF_EM_RAW_TO_R3;
|
---|
1147 | goto end;
|
---|
1148 | }
|
---|
1149 | /* Pending request packets might contain actions that need immediate attention, such as pending hardware interrupts. */
|
---|
1150 | if (VM_FF_ISPENDING(pVM, VM_FF_REQUEST))
|
---|
1151 | {
|
---|
1152 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
|
---|
1153 | rc = VINF_EM_PENDING_REQUEST;
|
---|
1154 | goto end;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | /* When external interrupts are pending, we should exit the VM when IF is set. */
|
---|
1158 | /** @note *after* VM_FF_INHIBIT_INTERRUPTS check!!! */
|
---|
1159 | rc = VMXR0CheckPendingInterrupt(pVM, pCtx);
|
---|
1160 | if (VBOX_FAILURE(rc))
|
---|
1161 | {
|
---|
1162 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
|
---|
1163 | goto end;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | /** @todo check timers?? */
|
---|
1167 |
|
---|
1168 | /* Save the host state first. */
|
---|
1169 | rc = VMXR0SaveHostState(pVM);
|
---|
1170 | if (rc != VINF_SUCCESS)
|
---|
1171 | {
|
---|
1172 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
|
---|
1173 | goto end;
|
---|
1174 | }
|
---|
1175 | /* Load the guest state */
|
---|
1176 | rc = VMXR0LoadGuestState(pVM, pCtx);
|
---|
1177 | if (rc != VINF_SUCCESS)
|
---|
1178 | {
|
---|
1179 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
|
---|
1180 | goto end;
|
---|
1181 | }
|
---|
1182 | fGuestStateSynced = true;
|
---|
1183 |
|
---|
1184 | /* TPR caching using CR8 is only available in 64 bits mode */
|
---|
1185 | /* Note the 32 bits exception for AMD (X86_CPUID_AMD_FEATURE_ECX_CR8L), but that appears missing in Intel CPUs */
|
---|
1186 | /* Note: we can't do this in LoadGuestState as PDMApicGetTPR can jump back to ring 3 (lock). */
|
---|
1187 | if ( pCtx->msrEFER & MSR_K6_EFER_LMA
|
---|
1188 | && pVM->hwaccm.s.vmx.pAPIC)
|
---|
1189 | {
|
---|
1190 | /* TPR caching in CR8 */
|
---|
1191 | uint8_t u8TPR;
|
---|
1192 | int rc = PDMApicGetTPR(pVM, &u8TPR);
|
---|
1193 | AssertRC(rc);
|
---|
1194 | /* The TPR can be found at offset 0x80 in the APIC mmio page. */
|
---|
1195 | pVM->hwaccm.s.vmx.pAPIC[0x80] = u8TPR << 4; /* bits 7-4 contain the task priority */
|
---|
1196 |
|
---|
1197 | /* CR8 updates that lower the TPR value to below the current value should cause an exit. */
|
---|
1198 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_TPR_THRESHOLD, u8TPR);
|
---|
1199 | AssertRC(rc);
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | /* Non-register state Guest Context */
|
---|
1203 | /** @todo change me according to cpu state */
|
---|
1204 | rc = VMXWriteVMCS(VMX_VMCS_GUEST_ACTIVITY_STATE, VMX_CMS_GUEST_ACTIVITY_ACTIVE);
|
---|
1205 | AssertRC(rc);
|
---|
1206 |
|
---|
1207 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatEntry, x);
|
---|
1208 |
|
---|
1209 | /* Manual save and restore:
|
---|
1210 | * - General purpose registers except RIP, RSP
|
---|
1211 | *
|
---|
1212 | * Trashed:
|
---|
1213 | * - CR2 (we don't care)
|
---|
1214 | * - LDTR (reset to 0)
|
---|
1215 | * - DRx (presumably not changed at all)
|
---|
1216 | * - DR7 (reset to 0x400)
|
---|
1217 | * - EFLAGS (reset to RT_BIT(1); not relevant)
|
---|
1218 | *
|
---|
1219 | */
|
---|
1220 |
|
---|
1221 | /* All done! Let's start VM execution. */
|
---|
1222 | STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatInGC, x);
|
---|
1223 | rc = pVM->hwaccm.s.vmx.pfnStartVM(pVM->hwaccm.s.vmx.fResumeVM, pCtx);
|
---|
1224 |
|
---|
1225 | /* In case we execute a goto ResumeExecution later on. */
|
---|
1226 | pVM->hwaccm.s.vmx.fResumeVM = true;
|
---|
1227 |
|
---|
1228 | /**
|
---|
1229 | * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
1230 | * IMPORTANT: WE CAN'T DO ANY LOGGING OR OPERATIONS THAT CAN DO A LONGJMP BACK TO RING 3 *BEFORE* WE'VE SYNCED BACK (MOST OF) THE GUEST STATE
|
---|
1231 | * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
1232 | */
|
---|
1233 |
|
---|
1234 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatInGC, x);
|
---|
1235 | STAM_PROFILE_ADV_START(&pVM->hwaccm.s.StatExit, x);
|
---|
1236 |
|
---|
1237 | switch (rc)
|
---|
1238 | {
|
---|
1239 | case VINF_SUCCESS:
|
---|
1240 | break;
|
---|
1241 |
|
---|
1242 | case VERR_VMX_INVALID_VMXON_PTR:
|
---|
1243 | AssertFailed();
|
---|
1244 | goto end;
|
---|
1245 |
|
---|
1246 | case VERR_VMX_UNABLE_TO_START_VM:
|
---|
1247 | case VERR_VMX_UNABLE_TO_RESUME_VM:
|
---|
1248 | {
|
---|
1249 | #ifdef VBOX_STRICT
|
---|
1250 | int rc1;
|
---|
1251 |
|
---|
1252 | rc1 = VMXReadVMCS(VMX_VMCS_RO_EXIT_REASON, &exitReason);
|
---|
1253 | rc1 |= VMXReadVMCS(VMX_VMCS_RO_VM_INSTR_ERROR, &instrError);
|
---|
1254 | AssertRC(rc1);
|
---|
1255 | if (rc1 == VINF_SUCCESS)
|
---|
1256 | {
|
---|
1257 | RTGDTR gdtr;
|
---|
1258 | PX86DESCHC pDesc;
|
---|
1259 |
|
---|
1260 | ASMGetGDTR(&gdtr);
|
---|
1261 |
|
---|
1262 | Log(("Unable to start/resume VM for reason: %x. Instruction error %x\n", (uint32_t)exitReason, (uint32_t)instrError));
|
---|
1263 | Log(("Current stack %08x\n", &rc1));
|
---|
1264 |
|
---|
1265 |
|
---|
1266 | VMXReadVMCS(VMX_VMCS_GUEST_RIP, &val);
|
---|
1267 | Log(("Old eip %VGv new %VGv\n", pCtx->rip, (RTGCPTR)val));
|
---|
1268 | VMXReadVMCS(VMX_VMCS_CTRL_PIN_EXEC_CONTROLS, &val);
|
---|
1269 | Log(("VMX_VMCS_CTRL_PIN_EXEC_CONTROLS %08x\n", val));
|
---|
1270 | VMXReadVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, &val);
|
---|
1271 | Log(("VMX_VMCS_CTRL_PROC_EXEC_CONTROLS %08x\n", val));
|
---|
1272 | VMXReadVMCS(VMX_VMCS_CTRL_ENTRY_CONTROLS, &val);
|
---|
1273 | Log(("VMX_VMCS_CTRL_ENTRY_CONTROLS %08x\n", val));
|
---|
1274 | VMXReadVMCS(VMX_VMCS_CTRL_EXIT_CONTROLS, &val);
|
---|
1275 | Log(("VMX_VMCS_CTRL_EXIT_CONTROLS %08x\n", val));
|
---|
1276 |
|
---|
1277 | VMXReadVMCS(VMX_VMCS_HOST_CR0, &val);
|
---|
1278 | Log(("VMX_VMCS_HOST_CR0 %08x\n", val));
|
---|
1279 |
|
---|
1280 | VMXReadVMCS(VMX_VMCS_HOST_CR3, &val);
|
---|
1281 | Log(("VMX_VMCS_HOST_CR3 %VHp\n", val));
|
---|
1282 |
|
---|
1283 | VMXReadVMCS(VMX_VMCS_HOST_CR4, &val);
|
---|
1284 | Log(("VMX_VMCS_HOST_CR4 %08x\n", val));
|
---|
1285 |
|
---|
1286 | VMXReadVMCS(VMX_VMCS_HOST_FIELD_CS, &val);
|
---|
1287 | Log(("VMX_VMCS_HOST_FIELD_CS %08x\n", val));
|
---|
1288 | if (val < gdtr.cbGdt)
|
---|
1289 | {
|
---|
1290 | pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
|
---|
1291 | HWACCMR0DumpDescriptor(pDesc, val, "CS: ");
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | VMXReadVMCS(VMX_VMCS_HOST_FIELD_DS, &val);
|
---|
1295 | Log(("VMX_VMCS_HOST_FIELD_DS %08x\n", val));
|
---|
1296 | if (val < gdtr.cbGdt)
|
---|
1297 | {
|
---|
1298 | pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
|
---|
1299 | HWACCMR0DumpDescriptor(pDesc, val, "DS: ");
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | VMXReadVMCS(VMX_VMCS_HOST_FIELD_ES, &val);
|
---|
1303 | Log(("VMX_VMCS_HOST_FIELD_ES %08x\n", val));
|
---|
1304 | if (val < gdtr.cbGdt)
|
---|
1305 | {
|
---|
1306 | pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
|
---|
1307 | HWACCMR0DumpDescriptor(pDesc, val, "ES: ");
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | VMXReadVMCS(VMX_VMCS_HOST_FIELD_FS, &val);
|
---|
1311 | Log(("VMX_VMCS_HOST_FIELD_FS %08x\n", val));
|
---|
1312 | if (val < gdtr.cbGdt)
|
---|
1313 | {
|
---|
1314 | pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
|
---|
1315 | HWACCMR0DumpDescriptor(pDesc, val, "FS: ");
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | VMXReadVMCS(VMX_VMCS_HOST_FIELD_GS, &val);
|
---|
1319 | Log(("VMX_VMCS_HOST_FIELD_GS %08x\n", val));
|
---|
1320 | if (val < gdtr.cbGdt)
|
---|
1321 | {
|
---|
1322 | pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
|
---|
1323 | HWACCMR0DumpDescriptor(pDesc, val, "GS: ");
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | VMXReadVMCS(VMX_VMCS_HOST_FIELD_SS, &val);
|
---|
1327 | Log(("VMX_VMCS_HOST_FIELD_SS %08x\n", val));
|
---|
1328 | if (val < gdtr.cbGdt)
|
---|
1329 | {
|
---|
1330 | pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
|
---|
1331 | HWACCMR0DumpDescriptor(pDesc, val, "SS: ");
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | VMXReadVMCS(VMX_VMCS_HOST_FIELD_TR, &val);
|
---|
1335 | Log(("VMX_VMCS_HOST_FIELD_TR %08x\n", val));
|
---|
1336 | if (val < gdtr.cbGdt)
|
---|
1337 | {
|
---|
1338 | pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
|
---|
1339 | HWACCMR0DumpDescriptor(pDesc, val, "TR: ");
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | VMXReadVMCS(VMX_VMCS_HOST_TR_BASE, &val);
|
---|
1343 | Log(("VMX_VMCS_HOST_TR_BASE %VHv\n", val));
|
---|
1344 |
|
---|
1345 | VMXReadVMCS(VMX_VMCS_HOST_GDTR_BASE, &val);
|
---|
1346 | Log(("VMX_VMCS_HOST_GDTR_BASE %VHv\n", val));
|
---|
1347 | VMXReadVMCS(VMX_VMCS_HOST_IDTR_BASE, &val);
|
---|
1348 | Log(("VMX_VMCS_HOST_IDTR_BASE %VHv\n", val));
|
---|
1349 |
|
---|
1350 | VMXReadVMCS(VMX_VMCS_HOST_SYSENTER_CS, &val);
|
---|
1351 | Log(("VMX_VMCS_HOST_SYSENTER_CS %08x\n", val));
|
---|
1352 |
|
---|
1353 | VMXReadVMCS(VMX_VMCS_HOST_SYSENTER_EIP, &val);
|
---|
1354 | Log(("VMX_VMCS_HOST_SYSENTER_EIP %VHv\n", val));
|
---|
1355 |
|
---|
1356 | VMXReadVMCS(VMX_VMCS_HOST_SYSENTER_ESP, &val);
|
---|
1357 | Log(("VMX_VMCS_HOST_SYSENTER_ESP %VHv\n", val));
|
---|
1358 |
|
---|
1359 | VMXReadVMCS(VMX_VMCS_HOST_RSP, &val);
|
---|
1360 | Log(("VMX_VMCS_HOST_RSP %VHv\n", val));
|
---|
1361 | VMXReadVMCS(VMX_VMCS_HOST_RIP, &val);
|
---|
1362 | Log(("VMX_VMCS_HOST_RIP %VHv\n", val));
|
---|
1363 |
|
---|
1364 | #if HC_ARCH_BITS == 64
|
---|
1365 | Log(("MSR_K6_EFER = %VX64\n", ASMRdMsr(MSR_K6_EFER)));
|
---|
1366 | Log(("MSR_K6_STAR = %VX64\n", ASMRdMsr(MSR_K6_STAR)));
|
---|
1367 | Log(("MSR_K8_LSTAR = %VX64\n", ASMRdMsr(MSR_K8_LSTAR)));
|
---|
1368 | Log(("MSR_K8_CSTAR = %VX64\n", ASMRdMsr(MSR_K8_CSTAR)));
|
---|
1369 | Log(("MSR_K8_SF_MASK = %VX64\n", ASMRdMsr(MSR_K8_SF_MASK)));
|
---|
1370 | #endif
|
---|
1371 | }
|
---|
1372 | #endif /* VBOX_STRICT */
|
---|
1373 | goto end;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | default:
|
---|
1377 | /* impossible */
|
---|
1378 | AssertFailed();
|
---|
1379 | goto end;
|
---|
1380 | }
|
---|
1381 | /* Success. Query the guest state and figure out what has happened. */
|
---|
1382 |
|
---|
1383 | /* Investigate why there was a VM-exit. */
|
---|
1384 | rc = VMXReadVMCS(VMX_VMCS_RO_EXIT_REASON, &exitReason);
|
---|
1385 | STAM_COUNTER_INC(&pVM->hwaccm.s.pStatExitReasonR0[exitReason & MASK_EXITREASON_STAT]);
|
---|
1386 |
|
---|
1387 | exitReason &= 0xffff; /* bit 0-15 contain the exit code. */
|
---|
1388 | rc |= VMXReadVMCS(VMX_VMCS_RO_VM_INSTR_ERROR, &instrError);
|
---|
1389 | rc |= VMXReadVMCS(VMX_VMCS_RO_EXIT_INSTR_LENGTH, &cbInstr);
|
---|
1390 | rc |= VMXReadVMCS(VMX_VMCS_RO_EXIT_INTERRUPTION_INFO, &val);
|
---|
1391 | intInfo = val;
|
---|
1392 | rc |= VMXReadVMCS(VMX_VMCS_RO_EXIT_INTERRUPTION_ERRCODE, &val);
|
---|
1393 | errCode = val; /* might not be valid; depends on VMX_EXIT_INTERRUPTION_INFO_ERROR_CODE_IS_VALID. */
|
---|
1394 | rc |= VMXReadVMCS(VMX_VMCS_RO_EXIT_INSTR_INFO, &val);
|
---|
1395 | instrInfo = val;
|
---|
1396 | rc |= VMXReadVMCS(VMX_VMCS_RO_EXIT_QUALIFICATION, &val);
|
---|
1397 | exitQualification = val;
|
---|
1398 | AssertRC(rc);
|
---|
1399 |
|
---|
1400 | /* Let's first sync back eip, esp, and eflags. */
|
---|
1401 | rc = VMXReadVMCS(VMX_VMCS_GUEST_RIP, &val);
|
---|
1402 | AssertRC(rc);
|
---|
1403 | pCtx->rip = val;
|
---|
1404 | rc = VMXReadVMCS(VMX_VMCS_GUEST_RSP, &val);
|
---|
1405 | AssertRC(rc);
|
---|
1406 | pCtx->rsp = val;
|
---|
1407 | rc = VMXReadVMCS(VMX_VMCS_GUEST_RFLAGS, &val);
|
---|
1408 | AssertRC(rc);
|
---|
1409 | pCtx->eflags.u32 = val;
|
---|
1410 |
|
---|
1411 | /* Take care of instruction fusing (sti, mov ss) */
|
---|
1412 | rc |= VMXReadVMCS(VMX_VMCS_GUEST_INTERRUPTIBILITY_STATE, &val);
|
---|
1413 | uInterruptState = val;
|
---|
1414 | if (uInterruptState != 0)
|
---|
1415 | {
|
---|
1416 | Assert(uInterruptState <= 2); /* only sti & mov ss */
|
---|
1417 | Log(("uInterruptState %x eip=%VGv\n", uInterruptState, pCtx->rip));
|
---|
1418 | EMSetInhibitInterruptsPC(pVM, pCtx->rip);
|
---|
1419 | }
|
---|
1420 | else
|
---|
1421 | VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
|
---|
1422 |
|
---|
1423 | /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
|
---|
1424 | if (!(pCtx->cr0 & X86_CR0_PROTECTION_ENABLE))
|
---|
1425 | {
|
---|
1426 | /* Hide our emulation flags */
|
---|
1427 | pCtx->eflags.Bits.u1VM = 0;
|
---|
1428 | pCtx->eflags.Bits.u1IF = pCtx->eflags.Bits.u1VIF;
|
---|
1429 | pCtx->eflags.Bits.u1VIF = 0;
|
---|
1430 | pCtx->eflags.Bits.u2IOPL = 0;
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | /* Control registers. */
|
---|
1434 | VMXReadVMCS(VMX_VMCS_CTRL_CR0_READ_SHADOW, &valShadow);
|
---|
1435 | VMXReadVMCS(VMX_VMCS_GUEST_CR0, &val);
|
---|
1436 | val = (valShadow & pVM->hwaccm.s.vmx.cr0_mask) | (val & ~pVM->hwaccm.s.vmx.cr0_mask);
|
---|
1437 | CPUMSetGuestCR0(pVM, val);
|
---|
1438 |
|
---|
1439 | VMXReadVMCS(VMX_VMCS_CTRL_CR4_READ_SHADOW, &valShadow);
|
---|
1440 | VMXReadVMCS(VMX_VMCS_GUEST_CR4, &val);
|
---|
1441 | val = (valShadow & pVM->hwaccm.s.vmx.cr4_mask) | (val & ~pVM->hwaccm.s.vmx.cr4_mask);
|
---|
1442 | CPUMSetGuestCR4(pVM, val);
|
---|
1443 |
|
---|
1444 | CPUMSetGuestCR2(pVM, ASMGetCR2());
|
---|
1445 |
|
---|
1446 | VMXReadVMCS(VMX_VMCS_GUEST_DR7, &val);
|
---|
1447 | CPUMSetGuestDR7(pVM, val);
|
---|
1448 |
|
---|
1449 | /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
|
---|
1450 | VMX_READ_SELREG(ES, es);
|
---|
1451 | VMX_READ_SELREG(SS, ss);
|
---|
1452 | VMX_READ_SELREG(CS, cs);
|
---|
1453 | VMX_READ_SELREG(DS, ds);
|
---|
1454 | VMX_READ_SELREG(FS, fs);
|
---|
1455 | VMX_READ_SELREG(GS, gs);
|
---|
1456 |
|
---|
1457 | /** @note NOW IT'S SAFE FOR LOGGING! */
|
---|
1458 | Log2(("Raw exit reason %08x\n", exitReason));
|
---|
1459 |
|
---|
1460 | /* Check if an injected event was interrupted prematurely. */
|
---|
1461 | rc = VMXReadVMCS(VMX_VMCS_RO_IDT_INFO, &val);
|
---|
1462 | AssertRC(rc);
|
---|
1463 | pVM->hwaccm.s.Event.intInfo = VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(val);
|
---|
1464 | if ( VMX_EXIT_INTERRUPTION_INFO_VALID(pVM->hwaccm.s.Event.intInfo)
|
---|
1465 | && VMX_EXIT_INTERRUPTION_INFO_TYPE(pVM->hwaccm.s.Event.intInfo) != VMX_EXIT_INTERRUPTION_INFO_TYPE_SW)
|
---|
1466 | {
|
---|
1467 | pVM->hwaccm.s.Event.fPending = true;
|
---|
1468 | /* Error code present? */
|
---|
1469 | if (VMX_EXIT_INTERRUPTION_INFO_ERROR_CODE_IS_VALID(pVM->hwaccm.s.Event.intInfo))
|
---|
1470 | {
|
---|
1471 | rc = VMXReadVMCS(VMX_VMCS_RO_IDT_ERRCODE, &val);
|
---|
1472 | AssertRC(rc);
|
---|
1473 | pVM->hwaccm.s.Event.errCode = val;
|
---|
1474 | Log(("Pending inject %VX64 at %VGv exit=%08x intInfo=%08x exitQualification=%08x pending error=%RX64\n", pVM->hwaccm.s.Event.intInfo, pCtx->rip, exitReason, intInfo, exitQualification, val));
|
---|
1475 | }
|
---|
1476 | else
|
---|
1477 | {
|
---|
1478 | Log(("Pending inject %VX64 at %VGv exit=%08x intInfo=%08x exitQualification=%08x\n", pVM->hwaccm.s.Event.intInfo, pCtx->rip, exitReason, intInfo, exitQualification));
|
---|
1479 | pVM->hwaccm.s.Event.errCode = 0;
|
---|
1480 | }
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | #ifdef VBOX_STRICT
|
---|
1484 | if (exitReason == VMX_EXIT_ERR_INVALID_GUEST_STATE)
|
---|
1485 | HWACCMDumpRegs(pCtx);
|
---|
1486 | #endif
|
---|
1487 |
|
---|
1488 | Log2(("E%d", exitReason));
|
---|
1489 | Log2(("Exit reason %d, exitQualification %08x\n", exitReason, exitQualification));
|
---|
1490 | Log2(("instrInfo=%d instrError=%d instr length=%d\n", instrInfo, instrError, cbInstr));
|
---|
1491 | Log2(("Interruption error code %d\n", errCode));
|
---|
1492 | Log2(("IntInfo = %08x\n", intInfo));
|
---|
1493 | Log2(("New EIP=%VGv\n", pCtx->rip));
|
---|
1494 |
|
---|
1495 | /* Some cases don't need a complete resync of the guest CPU state; handle them here. */
|
---|
1496 | switch (exitReason)
|
---|
1497 | {
|
---|
1498 | case VMX_EXIT_EXCEPTION: /* 0 Exception or non-maskable interrupt (NMI). */
|
---|
1499 | case VMX_EXIT_EXTERNAL_IRQ: /* 1 External interrupt. */
|
---|
1500 | {
|
---|
1501 | uint32_t vector = VMX_EXIT_INTERRUPTION_INFO_VECTOR(intInfo);
|
---|
1502 |
|
---|
1503 | if (!VMX_EXIT_INTERRUPTION_INFO_VALID(intInfo))
|
---|
1504 | {
|
---|
1505 | Assert(exitReason == VMX_EXIT_EXTERNAL_IRQ);
|
---|
1506 | /* External interrupt; leave to allow it to be dispatched again. */
|
---|
1507 | rc = VINF_EM_RAW_INTERRUPT;
|
---|
1508 | break;
|
---|
1509 | }
|
---|
1510 | switch (VMX_EXIT_INTERRUPTION_INFO_TYPE(intInfo))
|
---|
1511 | {
|
---|
1512 | case VMX_EXIT_INTERRUPTION_INFO_TYPE_NMI: /* Non-maskable interrupt. */
|
---|
1513 | /* External interrupt; leave to allow it to be dispatched again. */
|
---|
1514 | rc = VINF_EM_RAW_INTERRUPT;
|
---|
1515 | break;
|
---|
1516 |
|
---|
1517 | case VMX_EXIT_INTERRUPTION_INFO_TYPE_EXT: /* External hardware interrupt. */
|
---|
1518 | AssertFailed(); /* can't come here; fails the first check. */
|
---|
1519 | break;
|
---|
1520 |
|
---|
1521 | case VMX_EXIT_INTERRUPTION_INFO_TYPE_SWEXCPT: /* Software exception. (#BP or #OF) */
|
---|
1522 | Assert(vector == 3 || vector == 4);
|
---|
1523 | /* no break */
|
---|
1524 | case VMX_EXIT_INTERRUPTION_INFO_TYPE_HWEXCPT: /* Hardware exception. */
|
---|
1525 | Log2(("Hardware/software interrupt %d\n", vector));
|
---|
1526 | switch (vector)
|
---|
1527 | {
|
---|
1528 | case X86_XCPT_NM:
|
---|
1529 | {
|
---|
1530 | uint32_t oldCR0;
|
---|
1531 |
|
---|
1532 | Log(("#NM fault at %VGv error code %x\n", pCtx->rip, errCode));
|
---|
1533 |
|
---|
1534 | /** @todo don't intercept #NM exceptions anymore when we've activated the guest FPU state. */
|
---|
1535 | oldCR0 = ASMGetCR0();
|
---|
1536 | /* If we sync the FPU/XMM state on-demand, then we can continue execution as if nothing has happened. */
|
---|
1537 | rc = CPUMHandleLazyFPU(pVM);
|
---|
1538 | if (rc == VINF_SUCCESS)
|
---|
1539 | {
|
---|
1540 | Assert(CPUMIsGuestFPUStateActive(pVM));
|
---|
1541 |
|
---|
1542 | /* CPUMHandleLazyFPU could have changed CR0; restore it. */
|
---|
1543 | ASMSetCR0(oldCR0);
|
---|
1544 |
|
---|
1545 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitShadowNM);
|
---|
1546 |
|
---|
1547 | /* Continue execution. */
|
---|
1548 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1549 | pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
|
---|
1550 |
|
---|
1551 | goto ResumeExecution;
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | Log(("Forward #NM fault to the guest\n"));
|
---|
1555 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestNM);
|
---|
1556 | rc = VMXR0InjectEvent(pVM, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, 0);
|
---|
1557 | AssertRC(rc);
|
---|
1558 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1559 | goto ResumeExecution;
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | case X86_XCPT_PF: /* Page fault */
|
---|
1563 | {
|
---|
1564 | Log2(("Page fault at %VGv error code %x\n", exitQualification ,errCode));
|
---|
1565 | /* Exit qualification contains the linear address of the page fault. */
|
---|
1566 | TRPMAssertTrap(pVM, X86_XCPT_PF, TRPM_TRAP);
|
---|
1567 | TRPMSetErrorCode(pVM, errCode);
|
---|
1568 | TRPMSetFaultAddress(pVM, exitQualification);
|
---|
1569 |
|
---|
1570 | /* Forward it to our trap handler first, in case our shadow pages are out of sync. */
|
---|
1571 | rc = PGMTrap0eHandler(pVM, errCode, CPUMCTX2CORE(pCtx), (RTGCPTR)exitQualification);
|
---|
1572 | Log2(("PGMTrap0eHandler %VGv returned %Vrc\n", pCtx->rip, rc));
|
---|
1573 | if (rc == VINF_SUCCESS)
|
---|
1574 | { /* We've successfully synced our shadow pages, so let's just continue execution. */
|
---|
1575 | Log2(("Shadow page fault at %VGv cr2=%VGv error code %x\n", pCtx->rip, exitQualification ,errCode));
|
---|
1576 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitShadowPF);
|
---|
1577 |
|
---|
1578 | TRPMResetTrap(pVM);
|
---|
1579 |
|
---|
1580 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1581 | goto ResumeExecution;
|
---|
1582 | }
|
---|
1583 | else
|
---|
1584 | if (rc == VINF_EM_RAW_GUEST_TRAP)
|
---|
1585 | { /* A genuine pagefault.
|
---|
1586 | * Forward the trap to the guest by injecting the exception and resuming execution.
|
---|
1587 | */
|
---|
1588 | Log2(("Forward page fault to the guest\n"));
|
---|
1589 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestPF);
|
---|
1590 | /* The error code might have been changed. */
|
---|
1591 | errCode = TRPMGetErrorCode(pVM);
|
---|
1592 |
|
---|
1593 | TRPMResetTrap(pVM);
|
---|
1594 |
|
---|
1595 | /* Now we must update CR2. */
|
---|
1596 | pCtx->cr2 = exitQualification;
|
---|
1597 | rc = VMXR0InjectEvent(pVM, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
|
---|
1598 | AssertRC(rc);
|
---|
1599 |
|
---|
1600 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1601 | goto ResumeExecution;
|
---|
1602 | }
|
---|
1603 | #ifdef VBOX_STRICT
|
---|
1604 | if (rc != VINF_EM_RAW_EMULATE_INSTR)
|
---|
1605 | Log2(("PGMTrap0eHandler failed with %d\n", rc));
|
---|
1606 | #endif
|
---|
1607 | /* Need to go back to the recompiler to emulate the instruction. */
|
---|
1608 | TRPMResetTrap(pVM);
|
---|
1609 | break;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | case X86_XCPT_MF: /* Floating point exception. */
|
---|
1613 | {
|
---|
1614 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestMF);
|
---|
1615 | if (!(pCtx->cr0 & X86_CR0_NE))
|
---|
1616 | {
|
---|
1617 | /* old style FPU error reporting needs some extra work. */
|
---|
1618 | /** @todo don't fall back to the recompiler, but do it manually. */
|
---|
1619 | rc = VINF_EM_RAW_EMULATE_INSTR;
|
---|
1620 | break;
|
---|
1621 | }
|
---|
1622 | Log(("Trap %x at %VGv\n", vector, pCtx->rip));
|
---|
1623 | rc = VMXR0InjectEvent(pVM, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
|
---|
1624 | AssertRC(rc);
|
---|
1625 |
|
---|
1626 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1627 | goto ResumeExecution;
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | #ifdef VBOX_STRICT
|
---|
1631 | case X86_XCPT_GP: /* General protection failure exception.*/
|
---|
1632 | case X86_XCPT_UD: /* Unknown opcode exception. */
|
---|
1633 | case X86_XCPT_DE: /* Debug exception. */
|
---|
1634 | case X86_XCPT_SS: /* Stack segment exception. */
|
---|
1635 | case X86_XCPT_NP: /* Segment not present exception. */
|
---|
1636 | {
|
---|
1637 | switch(vector)
|
---|
1638 | {
|
---|
1639 | case X86_XCPT_DE:
|
---|
1640 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestDE);
|
---|
1641 | break;
|
---|
1642 | case X86_XCPT_UD:
|
---|
1643 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestUD);
|
---|
1644 | break;
|
---|
1645 | case X86_XCPT_SS:
|
---|
1646 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestSS);
|
---|
1647 | break;
|
---|
1648 | case X86_XCPT_NP:
|
---|
1649 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestNP);
|
---|
1650 | break;
|
---|
1651 | case X86_XCPT_GP:
|
---|
1652 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitGuestGP);
|
---|
1653 | break;
|
---|
1654 | }
|
---|
1655 |
|
---|
1656 | Log(("Trap %x at %VGv\n", vector, pCtx->rip));
|
---|
1657 | rc = VMXR0InjectEvent(pVM, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
|
---|
1658 | AssertRC(rc);
|
---|
1659 |
|
---|
1660 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1661 | goto ResumeExecution;
|
---|
1662 | }
|
---|
1663 | #endif
|
---|
1664 | default:
|
---|
1665 | AssertMsgFailed(("Unexpected vm-exit caused by exception %x\n", vector));
|
---|
1666 | rc = VERR_EM_INTERNAL_ERROR;
|
---|
1667 | break;
|
---|
1668 | } /* switch (vector) */
|
---|
1669 |
|
---|
1670 | break;
|
---|
1671 |
|
---|
1672 | default:
|
---|
1673 | rc = VERR_EM_INTERNAL_ERROR;
|
---|
1674 | AssertFailed();
|
---|
1675 | break;
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | break;
|
---|
1679 | }
|
---|
1680 |
|
---|
1681 | case VMX_EXIT_IRQ_WINDOW: /* 7 Interrupt window. */
|
---|
1682 | /* Clear VM-exit on IF=1 change. */
|
---|
1683 | Log2(("VMX_EXIT_IRQ_WINDOW %VGv\n", pCtx->rip));
|
---|
1684 | pVM->hwaccm.s.vmx.proc_ctls &= ~VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT;
|
---|
1685 | rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVM->hwaccm.s.vmx.proc_ctls);
|
---|
1686 | AssertRC(rc);
|
---|
1687 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIrqWindow);
|
---|
1688 | goto ResumeExecution; /* we check for pending guest interrupts there */
|
---|
1689 |
|
---|
1690 | case VMX_EXIT_INVD: /* 13 Guest software attempted to execute INVD. */
|
---|
1691 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitInvd);
|
---|
1692 | /* Skip instruction and continue directly. */
|
---|
1693 | pCtx->rip += cbInstr;
|
---|
1694 | /* Continue execution.*/
|
---|
1695 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1696 | goto ResumeExecution;
|
---|
1697 |
|
---|
1698 | case VMX_EXIT_CPUID: /* 10 Guest software attempted to execute CPUID. */
|
---|
1699 | {
|
---|
1700 | Log2(("VMX: Cpuid %x\n", pCtx->eax));
|
---|
1701 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCpuid);
|
---|
1702 | rc = EMInterpretCpuId(pVM, CPUMCTX2CORE(pCtx));
|
---|
1703 | if (rc == VINF_SUCCESS)
|
---|
1704 | {
|
---|
1705 | /* Update EIP and continue execution. */
|
---|
1706 | Assert(cbInstr == 2);
|
---|
1707 | pCtx->rip += cbInstr;
|
---|
1708 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1709 | goto ResumeExecution;
|
---|
1710 | }
|
---|
1711 | AssertMsgFailed(("EMU: cpuid failed with %Vrc\n", rc));
|
---|
1712 | rc = VINF_EM_RAW_EMULATE_INSTR;
|
---|
1713 | break;
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 | case VMX_EXIT_RDTSC: /* 16 Guest software attempted to execute RDTSC. */
|
---|
1717 | {
|
---|
1718 | Log2(("VMX: Rdtsc\n"));
|
---|
1719 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitRdtsc);
|
---|
1720 | rc = EMInterpretRdtsc(pVM, CPUMCTX2CORE(pCtx));
|
---|
1721 | if (rc == VINF_SUCCESS)
|
---|
1722 | {
|
---|
1723 | /* Update EIP and continue execution. */
|
---|
1724 | Assert(cbInstr == 2);
|
---|
1725 | pCtx->rip += cbInstr;
|
---|
1726 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1727 | goto ResumeExecution;
|
---|
1728 | }
|
---|
1729 | AssertMsgFailed(("EMU: rdtsc failed with %Vrc\n", rc));
|
---|
1730 | rc = VINF_EM_RAW_EMULATE_INSTR;
|
---|
1731 | break;
|
---|
1732 | }
|
---|
1733 |
|
---|
1734 | case VMX_EXIT_INVPG: /* 14 Guest software attempted to execute INVPG. */
|
---|
1735 | {
|
---|
1736 | Log2(("VMX: invlpg\n"));
|
---|
1737 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitInvpg);
|
---|
1738 | rc = EMInterpretInvlpg(pVM, CPUMCTX2CORE(pCtx), exitQualification);
|
---|
1739 | if (rc == VINF_SUCCESS)
|
---|
1740 | {
|
---|
1741 | /* Update EIP and continue execution. */
|
---|
1742 | pCtx->rip += cbInstr;
|
---|
1743 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1744 | goto ResumeExecution;
|
---|
1745 | }
|
---|
1746 | AssertMsg(rc == VERR_EM_INTERPRETER, ("EMU: invlpg %VGv failed with %Vrc\n", exitQualification, rc));
|
---|
1747 | break;
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | case VMX_EXIT_RDMSR: /* 31 RDMSR. Guest software attempted to execute RDMSR. */
|
---|
1751 | case VMX_EXIT_WRMSR: /* 32 WRMSR. Guest software attempted to execute WRMSR. */
|
---|
1752 | {
|
---|
1753 | uint32_t cbSize;
|
---|
1754 |
|
---|
1755 | /* Note: the intel manual claims there's a REX version of RDMSR that's slightly different, so we play safe by completely disassembling the instruction. */
|
---|
1756 | Log2(("VMX: %s\n", (exitReason == VMX_EXIT_RDMSR) ? "rdmsr" : "wrmsr"));
|
---|
1757 | rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
|
---|
1758 | if (rc == VINF_SUCCESS)
|
---|
1759 | {
|
---|
1760 | /* EIP has been updated already. */
|
---|
1761 |
|
---|
1762 | /* Only resume if successful. */
|
---|
1763 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1764 | goto ResumeExecution;
|
---|
1765 | }
|
---|
1766 | AssertMsg(rc == VERR_EM_INTERPRETER, ("EMU: %s failed with %Vrc\n", (exitReason == VMX_EXIT_RDMSR) ? "rdmsr" : "wrmsr", rc));
|
---|
1767 | break;
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | case VMX_EXIT_CRX_MOVE: /* 28 Control-register accesses. */
|
---|
1771 | {
|
---|
1772 | switch (VMX_EXIT_QUALIFICATION_CRX_ACCESS(exitQualification))
|
---|
1773 | {
|
---|
1774 | case VMX_EXIT_QUALIFICATION_CRX_ACCESS_WRITE:
|
---|
1775 | Log2(("VMX: %VGv mov cr%d, x\n", pCtx->rip, VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification)));
|
---|
1776 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCRxWrite);
|
---|
1777 | rc = EMInterpretCRxWrite(pVM, CPUMCTX2CORE(pCtx),
|
---|
1778 | VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification),
|
---|
1779 | VMX_EXIT_QUALIFICATION_CRX_GENREG(exitQualification));
|
---|
1780 |
|
---|
1781 | switch (VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification))
|
---|
1782 | {
|
---|
1783 | case 0:
|
---|
1784 | pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
|
---|
1785 | break;
|
---|
1786 | case 2:
|
---|
1787 | break;
|
---|
1788 | case 3:
|
---|
1789 | pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR3;
|
---|
1790 | break;
|
---|
1791 | case 4:
|
---|
1792 | pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR4;
|
---|
1793 | break;
|
---|
1794 | case 8:
|
---|
1795 | /* CR8 contains the APIC TPR */
|
---|
1796 | break;
|
---|
1797 |
|
---|
1798 | default:
|
---|
1799 | AssertFailed();
|
---|
1800 | break;
|
---|
1801 | }
|
---|
1802 | /* Check if a sync operation is pending. */
|
---|
1803 | if ( rc == VINF_SUCCESS /* don't bother if we are going to ring 3 anyway */
|
---|
1804 | && VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL))
|
---|
1805 | {
|
---|
1806 | rc = PGMSyncCR3(pVM, CPUMGetGuestCR0(pVM), CPUMGetGuestCR3(pVM), CPUMGetGuestCR4(pVM), VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
|
---|
1807 | AssertRC(rc);
|
---|
1808 | }
|
---|
1809 | break;
|
---|
1810 |
|
---|
1811 | case VMX_EXIT_QUALIFICATION_CRX_ACCESS_READ:
|
---|
1812 | Log2(("VMX: mov x, crx\n"));
|
---|
1813 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCRxRead);
|
---|
1814 |
|
---|
1815 | /* CR8 reads only cause an exit when the TPR shadow feature isn't present. */
|
---|
1816 | Assert(VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification) != 8 || !(pVM->hwaccm.s.vmx.msr.vmx_proc_ctls & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW));
|
---|
1817 |
|
---|
1818 | rc = EMInterpretCRxRead(pVM, CPUMCTX2CORE(pCtx),
|
---|
1819 | VMX_EXIT_QUALIFICATION_CRX_GENREG(exitQualification),
|
---|
1820 | VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification));
|
---|
1821 | break;
|
---|
1822 |
|
---|
1823 | case VMX_EXIT_QUALIFICATION_CRX_ACCESS_CLTS:
|
---|
1824 | Log2(("VMX: clts\n"));
|
---|
1825 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitCLTS);
|
---|
1826 | rc = EMInterpretCLTS(pVM);
|
---|
1827 | pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
|
---|
1828 | break;
|
---|
1829 |
|
---|
1830 | case VMX_EXIT_QUALIFICATION_CRX_ACCESS_LMSW:
|
---|
1831 | Log2(("VMX: lmsw %x\n", VMX_EXIT_QUALIFICATION_CRX_LMSW_DATA(exitQualification)));
|
---|
1832 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitLMSW);
|
---|
1833 | rc = EMInterpretLMSW(pVM, VMX_EXIT_QUALIFICATION_CRX_LMSW_DATA(exitQualification));
|
---|
1834 | pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
|
---|
1835 | break;
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | /* Update EIP if no error occurred. */
|
---|
1839 | if (VBOX_SUCCESS(rc))
|
---|
1840 | pCtx->rip += cbInstr;
|
---|
1841 |
|
---|
1842 | if (rc == VINF_SUCCESS)
|
---|
1843 | {
|
---|
1844 | /* Only resume if successful. */
|
---|
1845 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1846 | goto ResumeExecution;
|
---|
1847 | }
|
---|
1848 | Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
|
---|
1849 | break;
|
---|
1850 | }
|
---|
1851 |
|
---|
1852 | case VMX_EXIT_DRX_MOVE: /* 29 Debug-register accesses. */
|
---|
1853 | {
|
---|
1854 | /** @todo clear VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT after the first time and restore drx registers afterwards */
|
---|
1855 | if (VMX_EXIT_QUALIFICATION_DRX_DIRECTION(exitQualification) == VMX_EXIT_QUALIFICATION_DRX_DIRECTION_WRITE)
|
---|
1856 | {
|
---|
1857 | Log2(("VMX: mov drx%d, genreg%d\n", VMX_EXIT_QUALIFICATION_DRX_REGISTER(exitQualification), VMX_EXIT_QUALIFICATION_DRX_GENREG(exitQualification)));
|
---|
1858 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitDRxWrite);
|
---|
1859 | rc = EMInterpretDRxWrite(pVM, CPUMCTX2CORE(pCtx),
|
---|
1860 | VMX_EXIT_QUALIFICATION_DRX_REGISTER(exitQualification),
|
---|
1861 | VMX_EXIT_QUALIFICATION_DRX_GENREG(exitQualification));
|
---|
1862 | Log2(("DR7=%08x\n", pCtx->dr7));
|
---|
1863 | }
|
---|
1864 | else
|
---|
1865 | {
|
---|
1866 | Log2(("VMX: mov x, drx\n"));
|
---|
1867 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitDRxRead);
|
---|
1868 | rc = EMInterpretDRxRead(pVM, CPUMCTX2CORE(pCtx),
|
---|
1869 | VMX_EXIT_QUALIFICATION_DRX_GENREG(exitQualification),
|
---|
1870 | VMX_EXIT_QUALIFICATION_DRX_REGISTER(exitQualification));
|
---|
1871 | }
|
---|
1872 | /* Update EIP if no error occurred. */
|
---|
1873 | if (VBOX_SUCCESS(rc))
|
---|
1874 | pCtx->rip += cbInstr;
|
---|
1875 |
|
---|
1876 | if (rc == VINF_SUCCESS)
|
---|
1877 | {
|
---|
1878 | /* Only resume if successful. */
|
---|
1879 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1880 | goto ResumeExecution;
|
---|
1881 | }
|
---|
1882 | Assert(rc == VERR_EM_INTERPRETER);
|
---|
1883 | break;
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 | /** @note We'll get a #GP if the IO instruction isn't allowed (IOPL or TSS bitmap); no need to double check. */
|
---|
1887 | case VMX_EXIT_PORT_IO: /* 30 I/O instruction. */
|
---|
1888 | {
|
---|
1889 | uint32_t uIOWidth = VMX_EXIT_QUALIFICATION_IO_WIDTH(exitQualification);
|
---|
1890 | uint32_t uPort;
|
---|
1891 | bool fIOWrite = (VMX_EXIT_QUALIFICATION_IO_DIRECTION(exitQualification) == VMX_EXIT_QUALIFICATION_IO_DIRECTION_OUT);
|
---|
1892 |
|
---|
1893 | /** @todo necessary to make the distinction? */
|
---|
1894 | if (VMX_EXIT_QUALIFICATION_IO_ENCODING(exitQualification) == VMX_EXIT_QUALIFICATION_IO_ENCODING_DX)
|
---|
1895 | {
|
---|
1896 | uPort = pCtx->edx & 0xffff;
|
---|
1897 | }
|
---|
1898 | else
|
---|
1899 | uPort = VMX_EXIT_QUALIFICATION_IO_PORT(exitQualification); /* Immediate encoding. */
|
---|
1900 |
|
---|
1901 | /* paranoia */
|
---|
1902 | if (RT_UNLIKELY(uIOWidth == 2 || uIOWidth >= 4))
|
---|
1903 | {
|
---|
1904 | rc = fIOWrite ? VINF_IOM_HC_IOPORT_WRITE : VINF_IOM_HC_IOPORT_READ;
|
---|
1905 | break;
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | uint32_t cbSize = aIOSize[uIOWidth];
|
---|
1909 |
|
---|
1910 | if (VMX_EXIT_QUALIFICATION_IO_STRING(exitQualification))
|
---|
1911 | {
|
---|
1912 | /* ins/outs */
|
---|
1913 | uint32_t prefix = 0;
|
---|
1914 | if (VMX_EXIT_QUALIFICATION_IO_REP(exitQualification))
|
---|
1915 | prefix |= PREFIX_REP;
|
---|
1916 |
|
---|
1917 | if (fIOWrite)
|
---|
1918 | {
|
---|
1919 | Log2(("IOMInterpretOUTSEx %VGv %x size=%d\n", pCtx->rip, uPort, cbSize));
|
---|
1920 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOStringWrite);
|
---|
1921 | rc = IOMInterpretOUTSEx(pVM, CPUMCTX2CORE(pCtx), uPort, prefix, cbSize);
|
---|
1922 | }
|
---|
1923 | else
|
---|
1924 | {
|
---|
1925 | Log2(("IOMInterpretINSEx %VGv %x size=%d\n", pCtx->rip, uPort, cbSize));
|
---|
1926 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOStringRead);
|
---|
1927 | rc = IOMInterpretINSEx(pVM, CPUMCTX2CORE(pCtx), uPort, prefix, cbSize);
|
---|
1928 | }
|
---|
1929 | }
|
---|
1930 | else
|
---|
1931 | {
|
---|
1932 | /* normal in/out */
|
---|
1933 | uint32_t uAndVal = aIOOpAnd[uIOWidth];
|
---|
1934 |
|
---|
1935 | Assert(!VMX_EXIT_QUALIFICATION_IO_REP(exitQualification));
|
---|
1936 |
|
---|
1937 | if (fIOWrite)
|
---|
1938 | {
|
---|
1939 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIOWrite);
|
---|
1940 | rc = IOMIOPortWrite(pVM, uPort, pCtx->eax & uAndVal, cbSize);
|
---|
1941 | }
|
---|
1942 | else
|
---|
1943 | {
|
---|
1944 | uint32_t u32Val = 0;
|
---|
1945 |
|
---|
1946 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatExitIORead);
|
---|
1947 | rc = IOMIOPortRead(pVM, uPort, &u32Val, cbSize);
|
---|
1948 | if (IOM_SUCCESS(rc))
|
---|
1949 | {
|
---|
1950 | /* Write back to the EAX register. */
|
---|
1951 | pCtx->eax = (pCtx->eax & ~uAndVal) | (u32Val & uAndVal);
|
---|
1952 | }
|
---|
1953 | }
|
---|
1954 | }
|
---|
1955 | /*
|
---|
1956 | * Handled the I/O return codes.
|
---|
1957 | * (The unhandled cases end up with rc == VINF_EM_RAW_EMULATE_INSTR.)
|
---|
1958 | */
|
---|
1959 | if (IOM_SUCCESS(rc))
|
---|
1960 | {
|
---|
1961 | /* Update EIP and continue execution. */
|
---|
1962 | pCtx->rip += cbInstr;
|
---|
1963 | if (RT_LIKELY(rc == VINF_SUCCESS))
|
---|
1964 | {
|
---|
1965 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
1966 | goto ResumeExecution;
|
---|
1967 | }
|
---|
1968 | break;
|
---|
1969 | }
|
---|
1970 |
|
---|
1971 | #ifdef VBOX_STRICT
|
---|
1972 | if (rc == VINF_IOM_HC_IOPORT_READ)
|
---|
1973 | Assert(!fIOWrite);
|
---|
1974 | else if (rc == VINF_IOM_HC_IOPORT_WRITE)
|
---|
1975 | Assert(fIOWrite);
|
---|
1976 | else
|
---|
1977 | AssertMsg(VBOX_FAILURE(rc) || rc == VINF_EM_RAW_EMULATE_INSTR || rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED, ("%Vrc\n", rc));
|
---|
1978 | #endif
|
---|
1979 | break;
|
---|
1980 | }
|
---|
1981 |
|
---|
1982 | case VMX_EXIT_TPR: /* 43 TPR below threshold. Guest software executed MOV to CR8. */
|
---|
1983 | LogFlow(("VMX_EXIT_TPR\n"));
|
---|
1984 | /* RIP is already set to the next instruction and the TPR has been synced back. Just resume. */
|
---|
1985 | goto ResumeExecution;
|
---|
1986 |
|
---|
1987 | default:
|
---|
1988 | /* The rest is handled after syncing the entire CPU state. */
|
---|
1989 | break;
|
---|
1990 | }
|
---|
1991 |
|
---|
1992 | /* Note: the guest state isn't entirely synced back at this stage. */
|
---|
1993 |
|
---|
1994 | /* Investigate why there was a VM-exit. (part 2) */
|
---|
1995 | switch (exitReason)
|
---|
1996 | {
|
---|
1997 | case VMX_EXIT_EXCEPTION: /* 0 Exception or non-maskable interrupt (NMI). */
|
---|
1998 | case VMX_EXIT_EXTERNAL_IRQ: /* 1 External interrupt. */
|
---|
1999 | /* Already handled above. */
|
---|
2000 | break;
|
---|
2001 |
|
---|
2002 | case VMX_EXIT_TRIPLE_FAULT: /* 2 Triple fault. */
|
---|
2003 | rc = VINF_EM_RESET; /* Triple fault equals a reset. */
|
---|
2004 | break;
|
---|
2005 |
|
---|
2006 | case VMX_EXIT_INIT_SIGNAL: /* 3 INIT signal. */
|
---|
2007 | case VMX_EXIT_SIPI: /* 4 Start-up IPI (SIPI). */
|
---|
2008 | rc = VINF_EM_RAW_INTERRUPT;
|
---|
2009 | AssertFailed(); /* Can't happen. Yet. */
|
---|
2010 | break;
|
---|
2011 |
|
---|
2012 | case VMX_EXIT_IO_SMI_IRQ: /* 5 I/O system-management interrupt (SMI). */
|
---|
2013 | case VMX_EXIT_SMI_IRQ: /* 6 Other SMI. */
|
---|
2014 | rc = VINF_EM_RAW_INTERRUPT;
|
---|
2015 | AssertFailed(); /* Can't happen afaik. */
|
---|
2016 | break;
|
---|
2017 |
|
---|
2018 | case VMX_EXIT_TASK_SWITCH: /* 9 Task switch. */
|
---|
2019 | rc = VERR_EM_INTERPRETER;
|
---|
2020 | break;
|
---|
2021 |
|
---|
2022 | case VMX_EXIT_HLT: /* 12 Guest software attempted to execute HLT. */
|
---|
2023 | /** Check if external interrupts are pending; if so, don't switch back. */
|
---|
2024 | if ( pCtx->eflags.Bits.u1IF
|
---|
2025 | && VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)))
|
---|
2026 | {
|
---|
2027 | pCtx->rip++; /* skip hlt */
|
---|
2028 | goto ResumeExecution;
|
---|
2029 | }
|
---|
2030 |
|
---|
2031 | rc = VINF_EM_RAW_EMULATE_INSTR_HLT;
|
---|
2032 | break;
|
---|
2033 |
|
---|
2034 | case VMX_EXIT_RSM: /* 17 Guest software attempted to execute RSM in SMM. */
|
---|
2035 | AssertFailed(); /* can't happen. */
|
---|
2036 | rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
|
---|
2037 | break;
|
---|
2038 |
|
---|
2039 | case VMX_EXIT_VMCALL: /* 18 Guest software executed VMCALL. */
|
---|
2040 | case VMX_EXIT_VMCLEAR: /* 19 Guest software executed VMCLEAR. */
|
---|
2041 | case VMX_EXIT_VMLAUNCH: /* 20 Guest software executed VMLAUNCH. */
|
---|
2042 | case VMX_EXIT_VMPTRLD: /* 21 Guest software executed VMPTRLD. */
|
---|
2043 | case VMX_EXIT_VMPTRST: /* 22 Guest software executed VMPTRST. */
|
---|
2044 | case VMX_EXIT_VMREAD: /* 23 Guest software executed VMREAD. */
|
---|
2045 | case VMX_EXIT_VMRESUME: /* 24 Guest software executed VMRESUME. */
|
---|
2046 | case VMX_EXIT_VMWRITE: /* 25 Guest software executed VMWRITE. */
|
---|
2047 | case VMX_EXIT_VMXOFF: /* 26 Guest software executed VMXOFF. */
|
---|
2048 | case VMX_EXIT_VMXON: /* 27 Guest software executed VMXON. */
|
---|
2049 | /** @todo inject #UD immediately */
|
---|
2050 | rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
|
---|
2051 | break;
|
---|
2052 |
|
---|
2053 | case VMX_EXIT_CPUID: /* 10 Guest software attempted to execute CPUID. */
|
---|
2054 | case VMX_EXIT_RDTSC: /* 16 Guest software attempted to execute RDTSC. */
|
---|
2055 | case VMX_EXIT_INVPG: /* 14 Guest software attempted to execute INVPG. */
|
---|
2056 | case VMX_EXIT_CRX_MOVE: /* 28 Control-register accesses. */
|
---|
2057 | case VMX_EXIT_DRX_MOVE: /* 29 Debug-register accesses. */
|
---|
2058 | case VMX_EXIT_PORT_IO: /* 30 I/O instruction. */
|
---|
2059 | /* already handled above */
|
---|
2060 | AssertMsg( rc == VINF_PGM_CHANGE_MODE
|
---|
2061 | || rc == VINF_EM_RAW_INTERRUPT
|
---|
2062 | || rc == VERR_EM_INTERPRETER
|
---|
2063 | || rc == VINF_EM_RAW_EMULATE_INSTR
|
---|
2064 | || rc == VINF_PGM_SYNC_CR3
|
---|
2065 | || rc == VINF_IOM_HC_IOPORT_READ
|
---|
2066 | || rc == VINF_IOM_HC_IOPORT_WRITE
|
---|
2067 | || rc == VINF_EM_RAW_GUEST_TRAP
|
---|
2068 | || rc == VINF_TRPM_XCPT_DISPATCHED
|
---|
2069 | || rc == VINF_EM_RESCHEDULE_REM,
|
---|
2070 | ("rc = %d\n", rc));
|
---|
2071 | break;
|
---|
2072 |
|
---|
2073 | case VMX_EXIT_TPR: /* 43 TPR below threshold. Guest software executed MOV to CR8. */
|
---|
2074 | case VMX_EXIT_RDMSR: /* 31 RDMSR. Guest software attempted to execute RDMSR. */
|
---|
2075 | case VMX_EXIT_WRMSR: /* 32 WRMSR. Guest software attempted to execute WRMSR. */
|
---|
2076 | /* Note: If we decide to emulate them here, then we must sync the MSRs that could have been changed (sysenter, fs/gs base)!!! */
|
---|
2077 | rc = VERR_EM_INTERPRETER;
|
---|
2078 | break;
|
---|
2079 |
|
---|
2080 | case VMX_EXIT_RDPMC: /* 15 Guest software attempted to execute RDPMC. */
|
---|
2081 | case VMX_EXIT_MWAIT: /* 36 Guest software executed MWAIT. */
|
---|
2082 | case VMX_EXIT_MONITOR: /* 39 Guest software attempted to execute MONITOR. */
|
---|
2083 | case VMX_EXIT_PAUSE: /* 40 Guest software attempted to execute PAUSE. */
|
---|
2084 | rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
|
---|
2085 | break;
|
---|
2086 |
|
---|
2087 | case VMX_EXIT_IRQ_WINDOW: /* 7 Interrupt window. */
|
---|
2088 | Assert(rc == VINF_EM_RAW_INTERRUPT);
|
---|
2089 | break;
|
---|
2090 |
|
---|
2091 | case VMX_EXIT_ERR_INVALID_GUEST_STATE: /* 33 VM-entry failure due to invalid guest state. */
|
---|
2092 | {
|
---|
2093 | #ifdef VBOX_STRICT
|
---|
2094 | Log(("VMX_EXIT_ERR_INVALID_GUEST_STATE\n"));
|
---|
2095 |
|
---|
2096 | VMXReadVMCS(VMX_VMCS_GUEST_RIP, &val);
|
---|
2097 | Log(("Old eip %VGv new %VGv\n", pCtx->rip, (RTGCPTR)val));
|
---|
2098 |
|
---|
2099 | VMXReadVMCS(VMX_VMCS_GUEST_CR0, &val);
|
---|
2100 | Log(("VMX_VMCS_GUEST_CR0 %RX64\n", val));
|
---|
2101 |
|
---|
2102 | VMXReadVMCS(VMX_VMCS_GUEST_CR3, &val);
|
---|
2103 | Log(("VMX_VMCS_HOST_CR3 %VGp\n", val));
|
---|
2104 |
|
---|
2105 | VMXReadVMCS(VMX_VMCS_GUEST_CR4, &val);
|
---|
2106 | Log(("VMX_VMCS_GUEST_CR4 %RX64\n", val));
|
---|
2107 |
|
---|
2108 | VMX_LOG_SELREG(CS, "CS");
|
---|
2109 | VMX_LOG_SELREG(DS, "DS");
|
---|
2110 | VMX_LOG_SELREG(ES, "ES");
|
---|
2111 | VMX_LOG_SELREG(FS, "FS");
|
---|
2112 | VMX_LOG_SELREG(GS, "GS");
|
---|
2113 | VMX_LOG_SELREG(SS, "SS");
|
---|
2114 | VMX_LOG_SELREG(TR, "TR");
|
---|
2115 | VMX_LOG_SELREG(LDTR, "LDTR");
|
---|
2116 |
|
---|
2117 | VMXReadVMCS(VMX_VMCS_GUEST_GDTR_BASE, &val);
|
---|
2118 | Log(("VMX_VMCS_GUEST_GDTR_BASE %VGv\n", val));
|
---|
2119 | VMXReadVMCS(VMX_VMCS_GUEST_IDTR_BASE, &val);
|
---|
2120 | Log(("VMX_VMCS_GUEST_IDTR_BASE %VGv\n", val));
|
---|
2121 | #endif /* VBOX_STRICT */
|
---|
2122 | rc = VERR_EM_INTERNAL_ERROR;
|
---|
2123 | break;
|
---|
2124 | }
|
---|
2125 |
|
---|
2126 | case VMX_EXIT_ERR_MSR_LOAD: /* 34 VM-entry failure due to MSR loading. */
|
---|
2127 | case VMX_EXIT_ERR_MACHINE_CHECK: /* 41 VM-entry failure due to machine-check. */
|
---|
2128 | default:
|
---|
2129 | rc = VERR_EM_INTERNAL_ERROR;
|
---|
2130 | AssertMsgFailed(("Unexpected exit code %d\n", exitReason)); /* Can't happen. */
|
---|
2131 | break;
|
---|
2132 |
|
---|
2133 | }
|
---|
2134 | end:
|
---|
2135 | if (fGuestStateSynced)
|
---|
2136 | {
|
---|
2137 | /* Remaining guest CPU context: TR, IDTR, GDTR, LDTR. */
|
---|
2138 | VMX_READ_SELREG(LDTR, ldtr);
|
---|
2139 | VMX_READ_SELREG(TR, tr);
|
---|
2140 |
|
---|
2141 | VMXReadVMCS(VMX_VMCS_GUEST_GDTR_LIMIT, &val);
|
---|
2142 | pCtx->gdtr.cbGdt = val;
|
---|
2143 | VMXReadVMCS(VMX_VMCS_GUEST_GDTR_BASE, &val);
|
---|
2144 | pCtx->gdtr.pGdt = val;
|
---|
2145 |
|
---|
2146 | VMXReadVMCS(VMX_VMCS_GUEST_IDTR_LIMIT, &val);
|
---|
2147 | pCtx->idtr.cbIdt = val;
|
---|
2148 | VMXReadVMCS(VMX_VMCS_GUEST_IDTR_BASE, &val);
|
---|
2149 | pCtx->idtr.pIdt = val;
|
---|
2150 |
|
---|
2151 | /*
|
---|
2152 | * System MSRs
|
---|
2153 | */
|
---|
2154 | VMXReadVMCS(VMX_VMCS_GUEST_SYSENTER_CS, &val);
|
---|
2155 | pCtx->SysEnter.cs = val;
|
---|
2156 | VMXReadVMCS(VMX_VMCS_GUEST_SYSENTER_EIP, &val);
|
---|
2157 | pCtx->SysEnter.eip = val;
|
---|
2158 | VMXReadVMCS(VMX_VMCS_GUEST_SYSENTER_ESP, &val);
|
---|
2159 | pCtx->SysEnter.esp = val;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | /* Signal changes for the recompiler. */
|
---|
2163 | CPUMSetChangedFlags(pVM, CPUM_CHANGED_SYSENTER_MSR | CPUM_CHANGED_LDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_IDTR | CPUM_CHANGED_TR | CPUM_CHANGED_HIDDEN_SEL_REGS);
|
---|
2164 |
|
---|
2165 | /* If we executed vmlaunch/vmresume and an external irq was pending, then we don't have to do a full sync the next time. */
|
---|
2166 | if ( exitReason == VMX_EXIT_EXTERNAL_IRQ
|
---|
2167 | && !VMX_EXIT_INTERRUPTION_INFO_VALID(intInfo))
|
---|
2168 | {
|
---|
2169 | STAM_COUNTER_INC(&pVM->hwaccm.s.StatPendingHostIrq);
|
---|
2170 | /* On the next entry we'll only sync the host context. */
|
---|
2171 | pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_HOST_CONTEXT;
|
---|
2172 | }
|
---|
2173 | else
|
---|
2174 | {
|
---|
2175 | /* On the next entry we'll sync everything. */
|
---|
2176 | /** @todo we can do better than this */
|
---|
2177 | pVM->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL;
|
---|
2178 | }
|
---|
2179 |
|
---|
2180 | /* translate into a less severe return code */
|
---|
2181 | if (rc == VERR_EM_INTERPRETER)
|
---|
2182 | rc = VINF_EM_RAW_EMULATE_INSTR;
|
---|
2183 |
|
---|
2184 | STAM_PROFILE_ADV_STOP(&pVM->hwaccm.s.StatExit, x);
|
---|
2185 | Log2(("X"));
|
---|
2186 | return rc;
|
---|
2187 | }
|
---|
2188 |
|
---|
2189 |
|
---|
2190 | /**
|
---|
2191 | * Enters the VT-x session
|
---|
2192 | *
|
---|
2193 | * @returns VBox status code.
|
---|
2194 | * @param pVM The VM to operate on.
|
---|
2195 | * @param pCpu CPU info struct
|
---|
2196 | */
|
---|
2197 | HWACCMR0DECL(int) VMXR0Enter(PVM pVM, PHWACCM_CPUINFO pCpu)
|
---|
2198 | {
|
---|
2199 | Assert(pVM->hwaccm.s.vmx.fSupported);
|
---|
2200 |
|
---|
2201 | unsigned cr4 = ASMGetCR4();
|
---|
2202 | if (!(cr4 & X86_CR4_VMXE))
|
---|
2203 | {
|
---|
2204 | AssertMsgFailed(("X86_CR4_VMXE should be set!\n"));
|
---|
2205 | return VERR_VMX_X86_CR4_VMXE_CLEARED;
|
---|
2206 | }
|
---|
2207 |
|
---|
2208 | /* Activate the VM Control Structure. */
|
---|
2209 | int rc = VMXActivateVMCS(pVM->hwaccm.s.vmx.pVMCSPhys);
|
---|
2210 | if (VBOX_FAILURE(rc))
|
---|
2211 | return rc;
|
---|
2212 |
|
---|
2213 | pVM->hwaccm.s.vmx.fResumeVM = false;
|
---|
2214 | return VINF_SUCCESS;
|
---|
2215 | }
|
---|
2216 |
|
---|
2217 |
|
---|
2218 | /**
|
---|
2219 | * Leaves the VT-x session
|
---|
2220 | *
|
---|
2221 | * @returns VBox status code.
|
---|
2222 | * @param pVM The VM to operate on.
|
---|
2223 | */
|
---|
2224 | HWACCMR0DECL(int) VMXR0Leave(PVM pVM)
|
---|
2225 | {
|
---|
2226 | Assert(pVM->hwaccm.s.vmx.fSupported);
|
---|
2227 |
|
---|
2228 | /* Clear VM Control Structure. Marking it inactive, clearing implementation specific data and writing back VMCS data to memory. */
|
---|
2229 | int rc = VMXClearVMCS(pVM->hwaccm.s.vmx.pVMCSPhys);
|
---|
2230 | AssertRC(rc);
|
---|
2231 |
|
---|
2232 | return VINF_SUCCESS;
|
---|
2233 | }
|
---|
2234 |
|
---|