VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/HWVMXR0.cpp@ 16084

Last change on this file since 16084 was 16084, checked in by vboxsync, 16 years ago

Flush the recompiler code cache when switch from real to protected mode. */

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 154.6 KB
Line 
1/* $Id: HWVMXR0.cpp 16084 2009-01-20 14:20:58Z 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 <VBox/rem.h>
38#include <iprt/param.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/string.h>
42#include "HWVMXR0.h"
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47#if defined(RT_ARCH_AMD64)
48# define VMX_IS_64BIT_HOST_MODE() (true)
49#elif defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
50# define VMX_IS_64BIT_HOST_MODE() (g_fVMXIs64bitHost != 0)
51#else
52# define VMX_IS_64BIT_HOST_MODE() (false)
53#endif
54
55/*******************************************************************************
56* Global Variables *
57*******************************************************************************/
58/* IO operation lookup arrays. */
59static uint32_t const g_aIOSize[4] = {1, 2, 0, 4};
60static uint32_t const g_aIOOpAnd[4] = {0xff, 0xffff, 0, 0xffffffff};
61
62#ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
63/** See HWACCMR0A.asm. */
64extern "C" uint32_t g_fVMXIs64bitHost;
65#endif
66
67/*******************************************************************************
68* Local Functions *
69*******************************************************************************/
70static void VMXR0ReportWorldSwitchError(PVM pVM, PVMCPU pVCpu, int rc, PCPUMCTX pCtx);
71static void vmxR0SetupTLBEPT(PVM pVM, PVMCPU pVCpu);
72static void vmxR0SetupTLBVPID(PVM pVM, PVMCPU pVCpu);
73static void vmxR0SetupTLBDummy(PVM pVM, PVMCPU pVCpu);
74static void vmxR0FlushEPT(PVM pVM, PVMCPU pVCpu, VMX_FLUSH enmFlush, RTGCPHYS GCPhys);
75static void vmxR0FlushVPID(PVM pVM, PVMCPU pVCpu, VMX_FLUSH enmFlush, RTGCPTR GCPtr);
76static void vmxR0UpdateExceptionBitmap(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
77#ifdef VBOX_STRICT
78static bool vmxR0IsValidReadField(uint32_t idxField);
79static bool vmxR0IsValidWriteField(uint32_t idxField);
80#endif
81
82static void VMXR0CheckError(PVM pVM, PVMCPU pVCpu, int rc)
83{
84 if (rc == VERR_VMX_GENERIC)
85 {
86 RTCCUINTREG instrError;
87
88 VMXReadVMCS(VMX_VMCS32_RO_VM_INSTR_ERROR, &instrError);
89 pVCpu->hwaccm.s.vmx.lasterror.ulInstrError = instrError;
90 }
91 pVM->hwaccm.s.lLastError = rc;
92}
93
94/**
95 * Sets up and activates VT-x on the current CPU
96 *
97 * @returns VBox status code.
98 * @param pCpu CPU info struct
99 * @param pVM The VM to operate on. (can be NULL after a resume!!)
100 * @param pvPageCpu Pointer to the global cpu page
101 * @param pPageCpuPhys Physical address of the global cpu page
102 */
103VMMR0DECL(int) VMXR0EnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
104{
105 AssertReturn(pPageCpuPhys, VERR_INVALID_PARAMETER);
106 AssertReturn(pvPageCpu, VERR_INVALID_PARAMETER);
107
108#ifdef LOG_ENABLED
109 SUPR0Printf("VMXR0EnableCpu cpu %d page (%x) %x\n", pCpu->idCpu, pvPageCpu, (uint32_t)pPageCpuPhys);
110#endif
111 if (pVM)
112 {
113 /* Set revision dword at the beginning of the VMXON structure. */
114 *(uint32_t *)pvPageCpu = MSR_IA32_VMX_BASIC_INFO_VMCS_ID(pVM->hwaccm.s.vmx.msr.vmx_basic_info);
115 }
116
117 /** @todo we should unmap the two pages from the virtual address space in order to prevent accidental corruption.
118 * (which can have very bad consequences!!!)
119 */
120
121 /* Make sure the VMX instructions don't cause #UD faults. */
122 ASMSetCR4(ASMGetCR4() | X86_CR4_VMXE);
123
124 /* Enter VMX Root Mode */
125 int rc = VMXEnable(pPageCpuPhys);
126 if (RT_FAILURE(rc))
127 {
128 if (pVM)
129 VMXR0CheckError(pVM, &pVM->aCpus[0], rc);
130 ASMSetCR4(ASMGetCR4() & ~X86_CR4_VMXE);
131 return VERR_VMX_VMXON_FAILED;
132 }
133 return VINF_SUCCESS;
134}
135
136/**
137 * Deactivates VT-x on the current CPU
138 *
139 * @returns VBox status code.
140 * @param pCpu CPU info struct
141 * @param pvPageCpu Pointer to the global cpu page
142 * @param pPageCpuPhys Physical address of the global cpu page
143 */
144VMMR0DECL(int) VMXR0DisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
145{
146 AssertReturn(pPageCpuPhys, VERR_INVALID_PARAMETER);
147 AssertReturn(pvPageCpu, VERR_INVALID_PARAMETER);
148
149 /* Leave VMX Root Mode. */
150 VMXDisable();
151
152 /* And clear the X86_CR4_VMXE bit */
153 ASMSetCR4(ASMGetCR4() & ~X86_CR4_VMXE);
154
155#ifdef LOG_ENABLED
156 SUPR0Printf("VMXR0DisableCpu cpu %d\n", pCpu->idCpu);
157#endif
158 return VINF_SUCCESS;
159}
160
161/**
162 * Does Ring-0 per VM VT-x init.
163 *
164 * @returns VBox status code.
165 * @param pVM The VM to operate on.
166 */
167VMMR0DECL(int) VMXR0InitVM(PVM pVM)
168{
169 int rc;
170
171#ifdef LOG_ENABLED
172 SUPR0Printf("VMXR0InitVM %x\n", pVM);
173#endif
174
175 pVM->hwaccm.s.vmx.pMemObjAPIC = NIL_RTR0MEMOBJ;
176
177 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW)
178 {
179 /* Allocate one page for the virtual APIC mmio cache. */
180 rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.vmx.pMemObjAPIC, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
181 AssertRC(rc);
182 if (RT_FAILURE(rc))
183 return rc;
184
185 pVM->hwaccm.s.vmx.pAPIC = (uint8_t *)RTR0MemObjAddress(pVM->hwaccm.s.vmx.pMemObjAPIC);
186 pVM->hwaccm.s.vmx.pAPICPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.vmx.pMemObjAPIC, 0);
187 ASMMemZero32(pVM->hwaccm.s.vmx.pAPIC, PAGE_SIZE);
188 }
189 else
190 {
191 pVM->hwaccm.s.vmx.pMemObjAPIC = 0;
192 pVM->hwaccm.s.vmx.pAPIC = 0;
193 pVM->hwaccm.s.vmx.pAPICPhys = 0;
194 }
195
196 /* Allocate the MSR bitmap if this feature is supported. */
197 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS)
198 {
199 rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.vmx.pMemObjMSRBitmap, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
200 AssertRC(rc);
201 if (RT_FAILURE(rc))
202 return rc;
203
204 pVM->hwaccm.s.vmx.pMSRBitmap = (uint8_t *)RTR0MemObjAddress(pVM->hwaccm.s.vmx.pMemObjMSRBitmap);
205 pVM->hwaccm.s.vmx.pMSRBitmapPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.vmx.pMemObjMSRBitmap, 0);
206 memset(pVM->hwaccm.s.vmx.pMSRBitmap, 0xff, PAGE_SIZE);
207 }
208
209#ifdef VBOX_WITH_CRASHDUMP_MAGIC
210 {
211 rc = RTR0MemObjAllocCont(&pVM->hwaccm.s.vmx.pMemObjScratch, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
212 AssertRC(rc);
213 if (RT_FAILURE(rc))
214 return rc;
215
216 pVM->hwaccm.s.vmx.pScratch = (uint8_t *)RTR0MemObjAddress(pVM->hwaccm.s.vmx.pMemObjScratch);
217 pVM->hwaccm.s.vmx.pScratchPhys = RTR0MemObjGetPagePhysAddr(pVM->hwaccm.s.vmx.pMemObjScratch, 0);
218
219 ASMMemZero32(pVM->hwaccm.s.vmx.pScratch, PAGE_SIZE);
220 strcpy((char *)pVM->hwaccm.s.vmx.pScratch, "SCRATCH Magic");
221 *(uint64_t *)(pVM->hwaccm.s.vmx.pScratch + 16) = UINT64_C(0xDEADBEEFDEADBEEF);
222 }
223#endif
224
225 /* Allocate VMCBs for all guest CPUs. */
226 for (unsigned i=0;i<pVM->cCPUs;i++)
227 {
228 PVMCPU pVCpu = &pVM->aCpus[i];
229
230 pVCpu->hwaccm.s.vmx.pMemObjVMCS = NIL_RTR0MEMOBJ;
231
232 /* Allocate one page for the VM control structure (VMCS). */
233 rc = RTR0MemObjAllocCont(&pVCpu->hwaccm.s.vmx.pMemObjVMCS, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
234 AssertRC(rc);
235 if (RT_FAILURE(rc))
236 return rc;
237
238 pVCpu->hwaccm.s.vmx.pVMCS = RTR0MemObjAddress(pVCpu->hwaccm.s.vmx.pMemObjVMCS);
239 pVCpu->hwaccm.s.vmx.pVMCSPhys = RTR0MemObjGetPagePhysAddr(pVCpu->hwaccm.s.vmx.pMemObjVMCS, 0);
240 ASMMemZero32(pVCpu->hwaccm.s.vmx.pVMCS, PAGE_SIZE);
241
242 pVCpu->hwaccm.s.vmx.cr0_mask = 0;
243 pVCpu->hwaccm.s.vmx.cr4_mask = 0;
244
245 /* Current guest paging mode. */
246 pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode = PGMMODE_REAL;
247
248#ifdef LOG_ENABLED
249 SUPR0Printf("VMXR0InitVM %x VMCS=%x (%x)\n", pVM, pVCpu->hwaccm.s.vmx.pVMCS, (uint32_t)pVCpu->hwaccm.s.vmx.pVMCSPhys);
250#endif
251 }
252
253 return VINF_SUCCESS;
254}
255
256/**
257 * Does Ring-0 per VM VT-x termination.
258 *
259 * @returns VBox status code.
260 * @param pVM The VM to operate on.
261 */
262VMMR0DECL(int) VMXR0TermVM(PVM pVM)
263{
264 for (unsigned i=0;i<pVM->cCPUs;i++)
265 {
266 if (pVM->aCpus[i].hwaccm.s.vmx.pMemObjVMCS != NIL_RTR0MEMOBJ)
267 {
268 RTR0MemObjFree(pVM->aCpus[i].hwaccm.s.vmx.pMemObjVMCS, false);
269 pVM->aCpus[i].hwaccm.s.vmx.pMemObjVMCS = NIL_RTR0MEMOBJ;
270 pVM->aCpus[i].hwaccm.s.vmx.pVMCS = 0;
271 pVM->aCpus[i].hwaccm.s.vmx.pVMCSPhys = 0;
272 }
273 }
274 if (pVM->hwaccm.s.vmx.pMemObjAPIC != NIL_RTR0MEMOBJ)
275 {
276 RTR0MemObjFree(pVM->hwaccm.s.vmx.pMemObjAPIC, false);
277 pVM->hwaccm.s.vmx.pMemObjAPIC = NIL_RTR0MEMOBJ;
278 pVM->hwaccm.s.vmx.pAPIC = 0;
279 pVM->hwaccm.s.vmx.pAPICPhys = 0;
280 }
281 if (pVM->hwaccm.s.vmx.pMemObjMSRBitmap != NIL_RTR0MEMOBJ)
282 {
283 RTR0MemObjFree(pVM->hwaccm.s.vmx.pMemObjMSRBitmap, false);
284 pVM->hwaccm.s.vmx.pMemObjMSRBitmap = NIL_RTR0MEMOBJ;
285 pVM->hwaccm.s.vmx.pMSRBitmap = 0;
286 pVM->hwaccm.s.vmx.pMSRBitmapPhys = 0;
287 }
288#ifdef VBOX_WITH_CRASHDUMP_MAGIC
289 if (pVM->hwaccm.s.vmx.pMemObjScratch != NIL_RTR0MEMOBJ)
290 {
291 ASMMemZero32(pVM->hwaccm.s.vmx.pScratch, PAGE_SIZE);
292 RTR0MemObjFree(pVM->hwaccm.s.vmx.pMemObjScratch, false);
293 pVM->hwaccm.s.vmx.pMemObjScratch = NIL_RTR0MEMOBJ;
294 pVM->hwaccm.s.vmx.pScratch = 0;
295 pVM->hwaccm.s.vmx.pScratchPhys = 0;
296 }
297#endif
298 return VINF_SUCCESS;
299}
300
301/**
302 * Sets up VT-x for the specified VM
303 *
304 * @returns VBox status code.
305 * @param pVM The VM to operate on.
306 */
307VMMR0DECL(int) VMXR0SetupVM(PVM pVM)
308{
309 int rc = VINF_SUCCESS;
310 uint32_t val;
311
312 AssertReturn(pVM, VERR_INVALID_PARAMETER);
313
314 for (unsigned i=0;i<pVM->cCPUs;i++)
315 {
316 PVMCPU pVCpu = &pVM->aCpus[i];
317
318 Assert(pVCpu->hwaccm.s.vmx.pVMCS);
319
320 /* Set revision dword at the beginning of the VMCS structure. */
321 *(uint32_t *)pVCpu->hwaccm.s.vmx.pVMCS = MSR_IA32_VMX_BASIC_INFO_VMCS_ID(pVM->hwaccm.s.vmx.msr.vmx_basic_info);
322
323 /* Clear VM Control Structure. */
324 Log(("pVMCSPhys = %RHp\n", pVCpu->hwaccm.s.vmx.pVMCSPhys));
325 rc = VMXClearVMCS(pVCpu->hwaccm.s.vmx.pVMCSPhys);
326 if (RT_FAILURE(rc))
327 goto vmx_end;
328
329 /* Activate the VM Control Structure. */
330 rc = VMXActivateVMCS(pVCpu->hwaccm.s.vmx.pVMCSPhys);
331 if (RT_FAILURE(rc))
332 goto vmx_end;
333
334 /* VMX_VMCS_CTRL_PIN_EXEC_CONTROLS
335 * Set required bits to one and zero according to the MSR capabilities.
336 */
337 val = pVM->hwaccm.s.vmx.msr.vmx_pin_ctls.n.disallowed0;
338 /* External and non-maskable interrupts cause VM-exits. */
339 val = val | VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_EXT_INT_EXIT | VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_NMI_EXIT;
340 val &= pVM->hwaccm.s.vmx.msr.vmx_pin_ctls.n.allowed1;
341
342 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PIN_EXEC_CONTROLS, val);
343 AssertRC(rc);
344
345 /* VMX_VMCS_CTRL_PROC_EXEC_CONTROLS
346 * Set required bits to one and zero according to the MSR capabilities.
347 */
348 val = pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.disallowed0;
349 /* Program which event cause VM-exits and which features we want to use. */
350 val = val | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_HLT_EXIT
351 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_TSC_OFFSET
352 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT
353 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_UNCOND_IO_EXIT
354 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDPMC_EXIT
355 | 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) */
356
357 /* Without nested paging we should intercept invlpg and cr3 mov instructions. */
358 if (!pVM->hwaccm.s.fNestedPaging)
359 val |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT
360 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT
361 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT;
362
363 /* 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) */
364 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW)
365 {
366 /* CR8 reads from the APIC shadow page; writes cause an exit is they lower the TPR below the threshold */
367 val |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW;
368 Assert(pVM->hwaccm.s.vmx.pAPIC);
369 }
370 else
371 /* Exit on CR8 reads & writes in case the TPR shadow feature isn't present. */
372 val |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_STORE_EXIT | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_LOAD_EXIT;
373
374#ifdef VBOX_WITH_VTX_MSR_BITMAPS
375 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS)
376 {
377 Assert(pVM->hwaccm.s.vmx.pMSRBitmapPhys);
378 val |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS;
379 }
380#endif
381
382 /* We will use the secondary control if it's present. */
383 val |= VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL;
384
385 /* Mask away the bits that the CPU doesn't support */
386 /** @todo make sure they don't conflict with the above requirements. */
387 val &= pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1;
388 pVCpu->hwaccm.s.vmx.proc_ctls = val;
389
390 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, val);
391 AssertRC(rc);
392
393 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
394 {
395 /* VMX_VMCS_CTRL_PROC_EXEC_CONTROLS2
396 * Set required bits to one and zero according to the MSR capabilities.
397 */
398 val = pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.n.disallowed0;
399 val |= VMX_VMCS_CTRL_PROC_EXEC2_WBINVD_EXIT;
400
401#ifdef HWACCM_VTX_WITH_EPT
402 if (pVM->hwaccm.s.fNestedPaging)
403 val |= VMX_VMCS_CTRL_PROC_EXEC2_EPT;
404#endif /* HWACCM_VTX_WITH_EPT */
405#ifdef HWACCM_VTX_WITH_VPID
406 else
407 if (pVM->hwaccm.s.vmx.fVPID)
408 val |= VMX_VMCS_CTRL_PROC_EXEC2_VPID;
409#endif /* HWACCM_VTX_WITH_VPID */
410
411 /* Mask away the bits that the CPU doesn't support */
412 /** @todo make sure they don't conflict with the above requirements. */
413 val &= pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.n.allowed1;
414
415 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS2, val);
416 AssertRC(rc);
417 }
418
419 /* VMX_VMCS_CTRL_CR3_TARGET_COUNT
420 * Set required bits to one and zero according to the MSR capabilities.
421 */
422 rc = VMXWriteVMCS(VMX_VMCS_CTRL_CR3_TARGET_COUNT, 0);
423 AssertRC(rc);
424
425 /* Forward all exception except #NM & #PF to the guest.
426 * We always need to check pagefaults since our shadow page table can be out of sync.
427 * And we always lazily sync the FPU & XMM state.
428 */
429
430 /** @todo Possible optimization:
431 * Keep the FPU and XMM state current in the EM thread. That way there's no need to
432 * lazily sync anything, but the downside is that we can't use the FPU stack or XMM
433 * registers ourselves of course.
434 *
435 * Note: only possible if the current state is actually ours (X86_CR0_TS flag)
436 */
437
438 /* Don't filter page faults; all of them should cause a switch. */
439 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PAGEFAULT_ERROR_MASK, 0);
440 rc |= VMXWriteVMCS(VMX_VMCS_CTRL_PAGEFAULT_ERROR_MATCH, 0);
441 AssertRC(rc);
442
443 /* Init TSC offset to zero. */
444 rc = VMXWriteVMCS64(VMX_VMCS_CTRL_TSC_OFFSET_FULL, 0);
445 AssertRC(rc);
446
447 rc = VMXWriteVMCS64(VMX_VMCS_CTRL_IO_BITMAP_A_FULL, 0);
448 AssertRC(rc);
449
450 rc = VMXWriteVMCS64(VMX_VMCS_CTRL_IO_BITMAP_B_FULL, 0);
451 AssertRC(rc);
452
453 /* Set the MSR bitmap address. */
454 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS)
455 {
456 /* Optional */
457 rc = VMXWriteVMCS64(VMX_VMCS_CTRL_MSR_BITMAP_FULL, pVM->hwaccm.s.vmx.pMSRBitmapPhys);
458 AssertRC(rc);
459 }
460
461 /* Clear MSR controls. */
462 rc = VMXWriteVMCS64(VMX_VMCS_CTRL_VMEXIT_MSR_STORE_FULL, 0);
463 rc |= VMXWriteVMCS64(VMX_VMCS_CTRL_VMEXIT_MSR_LOAD_FULL, 0);
464 rc |= VMXWriteVMCS64(VMX_VMCS_CTRL_VMENTRY_MSR_LOAD_FULL, 0);
465 rc |= VMXWriteVMCS(VMX_VMCS_CTRL_EXIT_MSR_STORE_COUNT, 0);
466 rc |= VMXWriteVMCS(VMX_VMCS_CTRL_EXIT_MSR_LOAD_COUNT, 0);
467 AssertRC(rc);
468
469 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW)
470 {
471 Assert(pVM->hwaccm.s.vmx.pMemObjAPIC);
472 /* Optional */
473 rc = VMXWriteVMCS(VMX_VMCS_CTRL_TPR_THRESHOLD, 0);
474 rc |= VMXWriteVMCS64(VMX_VMCS_CTRL_VAPIC_PAGEADDR_FULL, pVM->hwaccm.s.vmx.pAPICPhys);
475 AssertRC(rc);
476 }
477
478 /* Set link pointer to -1. Not currently used. */
479 rc = VMXWriteVMCS64(VMX_VMCS_GUEST_LINK_PTR_FULL, 0xFFFFFFFFFFFFFFFFULL);
480 AssertRC(rc);
481
482 /* Clear VM Control Structure. Marking it inactive, clearing implementation specific data and writing back VMCS data to memory. */
483 rc = VMXClearVMCS(pVCpu->hwaccm.s.vmx.pVMCSPhys);
484 AssertRC(rc);
485
486 /* Configure the VMCS read cache. */
487 PVMCSCACHE pCache = &pVCpu->hwaccm.s.vmx.VMCSCache;
488
489 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_RIP);
490 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_RSP);
491 VMXSetupCachedReadVMCS(pCache, VMX_VMCS_GUEST_RFLAGS);
492 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_GUEST_INTERRUPTIBILITY_STATE);
493 VMXSetupCachedReadVMCS(pCache, VMX_VMCS_CTRL_CR0_READ_SHADOW);
494 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_CR0);
495 VMXSetupCachedReadVMCS(pCache, VMX_VMCS_CTRL_CR4_READ_SHADOW);
496 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_CR4);
497 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_DR7);
498 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_GUEST_SYSENTER_CS);
499 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_SYSENTER_EIP);
500 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_SYSENTER_ESP);
501 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_GUEST_GDTR_LIMIT);
502 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_GDTR_BASE);
503 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_GUEST_IDTR_LIMIT);
504 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_IDTR_BASE);
505
506 VMX_SETUP_SELREG(ES, pCache);
507 VMX_SETUP_SELREG(SS, pCache);
508 VMX_SETUP_SELREG(CS, pCache);
509 VMX_SETUP_SELREG(DS, pCache);
510 VMX_SETUP_SELREG(FS, pCache);
511 VMX_SETUP_SELREG(GS, pCache);
512 VMX_SETUP_SELREG(LDTR, pCache);
513 VMX_SETUP_SELREG(TR, pCache);
514
515 /* Status code VMCS reads. */
516 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_RO_EXIT_REASON);
517 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_RO_VM_INSTR_ERROR);
518 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_RO_EXIT_INSTR_LENGTH);
519 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_RO_EXIT_INTERRUPTION_ERRCODE);
520 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_RO_EXIT_INTERRUPTION_INFO);
521 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_RO_EXIT_INSTR_INFO);
522 VMXSetupCachedReadVMCS(pCache, VMX_VMCS_RO_EXIT_QUALIFICATION);
523 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_RO_IDT_INFO);
524 VMXSetupCachedReadVMCS(pCache, VMX_VMCS32_RO_IDT_ERRCODE);
525
526 if (pVM->hwaccm.s.fNestedPaging)
527 {
528 VMXSetupCachedReadVMCS(pCache, VMX_VMCS64_GUEST_CR3);
529 VMXSetupCachedReadVMCS(pCache, VMX_VMCS_EXIT_PHYS_ADDR_FULL);
530 pCache->Read.cValidEntries = VMX_VMCS_MAX_NESTED_PAGING_CACHE_IDX;
531 }
532 else
533 pCache->Read.cValidEntries = VMX_VMCS_MAX_CACHE_IDX;
534 } /* for each VMCPU */
535
536 /* Choose the right TLB setup function. */
537 if (pVM->hwaccm.s.fNestedPaging)
538 {
539 pVM->hwaccm.s.vmx.pfnSetupTaggedTLB = vmxR0SetupTLBEPT;
540
541 /* Default values for flushing. */
542 pVM->hwaccm.s.vmx.enmFlushPage = VMX_FLUSH_ALL_CONTEXTS;
543 pVM->hwaccm.s.vmx.enmFlushContext = VMX_FLUSH_ALL_CONTEXTS;
544
545 /* If the capabilities specify we can do more, then make use of it. */
546 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_INDIV)
547 pVM->hwaccm.s.vmx.enmFlushPage = VMX_FLUSH_PAGE;
548 else
549 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_CONTEXT)
550 pVM->hwaccm.s.vmx.enmFlushPage = VMX_FLUSH_SINGLE_CONTEXT;
551
552 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_CONTEXT)
553 pVM->hwaccm.s.vmx.enmFlushContext = VMX_FLUSH_SINGLE_CONTEXT;
554 }
555#ifdef HWACCM_VTX_WITH_VPID
556 else
557 if (pVM->hwaccm.s.vmx.fVPID)
558 {
559 pVM->hwaccm.s.vmx.pfnSetupTaggedTLB = vmxR0SetupTLBVPID;
560
561 /* Default values for flushing. */
562 pVM->hwaccm.s.vmx.enmFlushPage = VMX_FLUSH_ALL_CONTEXTS;
563 pVM->hwaccm.s.vmx.enmFlushContext = VMX_FLUSH_ALL_CONTEXTS;
564
565 /* If the capabilities specify we can do more, then make use of it. */
566 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_INDIV)
567 pVM->hwaccm.s.vmx.enmFlushPage = VMX_FLUSH_PAGE;
568 else
569 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_CONTEXT)
570 pVM->hwaccm.s.vmx.enmFlushPage = VMX_FLUSH_SINGLE_CONTEXT;
571
572 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_CONTEXT)
573 pVM->hwaccm.s.vmx.enmFlushContext = VMX_FLUSH_SINGLE_CONTEXT;
574 }
575#endif /* HWACCM_VTX_WITH_VPID */
576 else
577 pVM->hwaccm.s.vmx.pfnSetupTaggedTLB = vmxR0SetupTLBDummy;
578
579vmx_end:
580 VMXR0CheckError(pVM, &pVM->aCpus[0], rc);
581 return rc;
582}
583
584
585/**
586 * Injects an event (trap or external interrupt)
587 *
588 * @returns VBox status code.
589 * @param pVM The VM to operate on.
590 * @param pVCpu The VMCPU to operate on.
591 * @param pCtx CPU Context
592 * @param intInfo VMX interrupt info
593 * @param cbInstr Opcode length of faulting instruction
594 * @param errCode Error code (optional)
595 */
596static int VMXR0InjectEvent(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t intInfo, uint32_t cbInstr, uint32_t errCode)
597{
598 int rc;
599 uint32_t iGate = VMX_EXIT_INTERRUPTION_INFO_VECTOR(intInfo);
600
601#ifdef VBOX_STRICT
602 if (iGate == 0xE)
603 LogFlow(("VMXR0InjectEvent: Injecting interrupt %d at %RGv error code=%08x CR2=%08x intInfo=%08x\n", iGate, (RTGCPTR)pCtx->rip, errCode, pCtx->cr2, intInfo));
604 else
605 if (iGate < 0x20)
606 LogFlow(("VMXR0InjectEvent: Injecting interrupt %d at %RGv error code=%08x\n", iGate, (RTGCPTR)pCtx->rip, errCode));
607 else
608 {
609 LogFlow(("INJ-EI: %x at %RGv\n", iGate, (RTGCPTR)pCtx->rip));
610 Assert(!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS));
611 Assert(pCtx->eflags.u32 & X86_EFL_IF);
612 }
613#endif
614
615#ifdef HWACCM_VMX_EMULATE_REALMODE
616 if (CPUMIsGuestInRealModeEx(pCtx))
617 {
618 RTGCPHYS GCPhysHandler;
619 uint16_t offset, ip;
620 RTSEL sel;
621
622 /* Injecting events doesn't work right with real mode emulation.
623 * (#GP if we try to inject external hardware interrupts)
624 * Inject the interrupt or trap directly instead.
625 */
626 Log(("Manual interrupt/trap '%x' inject (real mode)\n", iGate));
627
628 /* Check if the interrupt handler is present. */
629 if (iGate * 4 + 3 > pCtx->idtr.cbIdt)
630 {
631 Log(("IDT cbIdt violation\n"));
632 if (iGate != X86_XCPT_DF)
633 {
634 RTGCUINTPTR intInfo;
635
636 intInfo = (iGate == X86_XCPT_GP) ? (uint32_t)X86_XCPT_DF : iGate;
637 intInfo |= (1 << VMX_EXIT_INTERRUPTION_INFO_VALID_SHIFT);
638 intInfo |= VMX_EXIT_INTERRUPTION_INFO_ERROR_CODE_VALID;
639 intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_HWEXCPT << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
640
641 return VMXR0InjectEvent(pVM, pVCpu, pCtx, intInfo, 0, 0 /* no error code according to the Intel docs */);
642 }
643 Log(("Triple fault -> reset the VM!\n"));
644 return VINF_EM_RESET;
645 }
646 if ( VMX_EXIT_INTERRUPTION_INFO_TYPE(intInfo) == VMX_EXIT_INTERRUPTION_INFO_TYPE_SW
647 || iGate == 3 /* Both #BP and #OF point to the instruction after. */
648 || iGate == 4)
649 {
650 ip = pCtx->ip + cbInstr;
651 }
652 else
653 ip = pCtx->ip;
654
655 /* Read the selector:offset pair of the interrupt handler. */
656 GCPhysHandler = (RTGCPHYS)pCtx->idtr.pIdt + iGate * 4;
657 PGMPhysRead(pVM, GCPhysHandler, &offset, sizeof(offset));
658 PGMPhysRead(pVM, GCPhysHandler + 2, &sel, sizeof(sel));
659
660 LogFlow(("IDT handler %04X:%04X\n", sel, offset));
661
662 /* Construct the stack frame. */
663 /** @todo should check stack limit. */
664 pCtx->sp -= 2;
665 LogFlow(("ss:sp %04X:%04X eflags=%x\n", pCtx->ss, pCtx->sp, pCtx->eflags.u));
666 PGMPhysWrite(pVM, pCtx->ssHid.u64Base + pCtx->sp, &pCtx->eflags, sizeof(uint16_t));
667 pCtx->sp -= 2;
668 LogFlow(("ss:sp %04X:%04X cs=%x\n", pCtx->ss, pCtx->sp, pCtx->cs));
669 PGMPhysWrite(pVM, pCtx->ssHid.u64Base + pCtx->sp, &pCtx->cs, sizeof(uint16_t));
670 pCtx->sp -= 2;
671 LogFlow(("ss:sp %04X:%04X ip=%x\n", pCtx->ss, pCtx->sp, ip));
672 PGMPhysWrite(pVM, pCtx->ssHid.u64Base + pCtx->sp, &ip, sizeof(ip));
673
674 /* Update the CPU state for executing the handler. */
675 pCtx->rip = offset;
676 pCtx->cs = sel;
677 pCtx->csHid.u64Base = sel << 4;
678 pCtx->eflags.u &= ~(X86_EFL_IF|X86_EFL_TF|X86_EFL_RF|X86_EFL_AC);
679
680 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_SEGMENT_REGS;
681 return VINF_SUCCESS;
682 }
683#endif /* HWACCM_VMX_EMULATE_REALMODE */
684
685 /* Set event injection state. */
686 rc = VMXWriteVMCS(VMX_VMCS_CTRL_ENTRY_IRQ_INFO, intInfo | (1 << VMX_EXIT_INTERRUPTION_INFO_VALID_SHIFT));
687
688 rc |= VMXWriteVMCS(VMX_VMCS_CTRL_ENTRY_INSTR_LENGTH, cbInstr);
689 rc |= VMXWriteVMCS(VMX_VMCS_CTRL_ENTRY_EXCEPTION_ERRCODE, errCode);
690
691 AssertRC(rc);
692 return rc;
693}
694
695
696/**
697 * Checks for pending guest interrupts and injects them
698 *
699 * @returns VBox status code.
700 * @param pVM The VM to operate on.
701 * @param pVCpu The VMCPU to operate on.
702 * @param pCtx CPU Context
703 */
704static int VMXR0CheckPendingInterrupt(PVM pVM, PVMCPU pVCpu, CPUMCTX *pCtx)
705{
706 int rc;
707
708 /* Dispatch any pending interrupts. (injected before, but a VM exit occurred prematurely) */
709 if (pVCpu->hwaccm.s.Event.fPending)
710 {
711 Log(("Reinjecting event %RX64 %08x at %RGv cr2=%RX64\n", pVCpu->hwaccm.s.Event.intInfo, pVCpu->hwaccm.s.Event.errCode, (RTGCPTR)pCtx->rip, pCtx->cr2));
712 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatIntReinject);
713 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, pVCpu->hwaccm.s.Event.intInfo, 0, pVCpu->hwaccm.s.Event.errCode);
714 AssertRC(rc);
715
716 pVCpu->hwaccm.s.Event.fPending = false;
717 return VINF_SUCCESS;
718 }
719
720 if (pVM->hwaccm.s.fInjectNMI)
721 {
722 RTGCUINTPTR intInfo;
723
724 intInfo = X86_XCPT_NMI;
725 intInfo |= (1 << VMX_EXIT_INTERRUPTION_INFO_VALID_SHIFT);
726 intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_NMI << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
727
728 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, intInfo, 0, 0);
729 AssertRC(rc);
730
731 pVM->hwaccm.s.fInjectNMI = false;
732 return VINF_SUCCESS;
733 }
734
735 /* When external interrupts are pending, we should exit the VM when IF is set. */
736 if ( !TRPMHasTrap(pVM)
737 && VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)))
738 {
739 if (!(pCtx->eflags.u32 & X86_EFL_IF))
740 {
741 if (!(pVCpu->hwaccm.s.vmx.proc_ctls & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT))
742 {
743 LogFlow(("Enable irq window exit!\n"));
744 pVCpu->hwaccm.s.vmx.proc_ctls |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT;
745 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVCpu->hwaccm.s.vmx.proc_ctls);
746 AssertRC(rc);
747 }
748 /* else nothing to do but wait */
749 }
750 else
751 if (!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
752 {
753 uint8_t u8Interrupt;
754
755 rc = PDMGetInterrupt(pVM, &u8Interrupt);
756 Log(("Dispatch interrupt: u8Interrupt=%x (%d) rc=%Rrc cs:rip=%04X:%RGv\n", u8Interrupt, u8Interrupt, rc, pCtx->cs, (RTGCPTR)pCtx->rip));
757 if (RT_SUCCESS(rc))
758 {
759 rc = TRPMAssertTrap(pVM, u8Interrupt, TRPM_HARDWARE_INT);
760 AssertRC(rc);
761 }
762 else
763 {
764 /* Can only happen in rare cases where a pending interrupt is cleared behind our back */
765 Assert(!VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)));
766 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatSwitchGuestIrq);
767 /* Just continue */
768 }
769 }
770 else
771 Log(("Pending interrupt blocked at %RGv by VM_FF_INHIBIT_INTERRUPTS!!\n", (RTGCPTR)pCtx->rip));
772 }
773
774#ifdef VBOX_STRICT
775 if (TRPMHasTrap(pVM))
776 {
777 uint8_t u8Vector;
778 rc = TRPMQueryTrapAll(pVM, &u8Vector, 0, 0, 0);
779 AssertRC(rc);
780 }
781#endif
782
783 if ( pCtx->eflags.u32 & X86_EFL_IF
784 && (!VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
785 && TRPMHasTrap(pVM)
786 )
787 {
788 uint8_t u8Vector;
789 int rc;
790 TRPMEVENT enmType;
791 RTGCUINTPTR intInfo;
792 RTGCUINT errCode;
793
794 /* If a new event is pending, then dispatch it now. */
795 rc = TRPMQueryTrapAll(pVM, &u8Vector, &enmType, &errCode, 0);
796 AssertRC(rc);
797 Assert(pCtx->eflags.Bits.u1IF == 1 || enmType == TRPM_TRAP);
798 Assert(enmType != TRPM_SOFTWARE_INT);
799
800 /* Clear the pending trap. */
801 rc = TRPMResetTrap(pVM);
802 AssertRC(rc);
803
804 intInfo = u8Vector;
805 intInfo |= (1 << VMX_EXIT_INTERRUPTION_INFO_VALID_SHIFT);
806
807 if (enmType == TRPM_TRAP)
808 {
809 switch (u8Vector) {
810 case 8:
811 case 10:
812 case 11:
813 case 12:
814 case 13:
815 case 14:
816 case 17:
817 /* Valid error codes. */
818 intInfo |= VMX_EXIT_INTERRUPTION_INFO_ERROR_CODE_VALID;
819 break;
820 default:
821 break;
822 }
823 if (u8Vector == X86_XCPT_BP || u8Vector == X86_XCPT_OF)
824 intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_SWEXCPT << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
825 else
826 intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_HWEXCPT << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
827 }
828 else
829 intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_EXT << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
830
831 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatIntInject);
832 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, intInfo, 0, errCode);
833 AssertRC(rc);
834 } /* if (interrupts can be dispatched) */
835
836 return VINF_SUCCESS;
837}
838
839/**
840 * Save the host state
841 *
842 * @returns VBox status code.
843 * @param pVM The VM to operate on.
844 * @param pVCpu The VMCPU to operate on.
845 */
846VMMR0DECL(int) VMXR0SaveHostState(PVM pVM, PVMCPU pVCpu)
847{
848 int rc = VINF_SUCCESS;
849
850 /*
851 * Host CPU Context
852 */
853 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_HOST_CONTEXT)
854 {
855 RTIDTR idtr;
856 RTGDTR gdtr;
857 RTSEL SelTR;
858 PX86DESCHC pDesc;
859 uintptr_t trBase;
860 RTSEL cs;
861 RTSEL ss;
862 uint64_t cr3;
863
864 /* Control registers */
865 rc = VMXWriteVMCS(VMX_VMCS_HOST_CR0, ASMGetCR0());
866#ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
867 if (VMX_IS_64BIT_HOST_MODE())
868 {
869 cr3 = hwaccmR0Get64bitCR3();
870 rc |= VMXWriteVMCS64(VMX_VMCS_HOST_CR3, cr3);
871 }
872 else
873#endif
874 {
875 cr3 = ASMGetCR3();
876 rc |= VMXWriteVMCS(VMX_VMCS_HOST_CR3, cr3);
877 }
878 rc |= VMXWriteVMCS(VMX_VMCS_HOST_CR4, ASMGetCR4());
879 AssertRC(rc);
880 Log2(("VMX_VMCS_HOST_CR0 %08x\n", ASMGetCR0()));
881 Log2(("VMX_VMCS_HOST_CR3 %08RX64\n", cr3));
882 Log2(("VMX_VMCS_HOST_CR4 %08x\n", ASMGetCR4()));
883
884 /* Selector registers. */
885#ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
886 if (VMX_IS_64BIT_HOST_MODE())
887 {
888 cs = (RTSEL)(uintptr_t)&SUPR0Abs64bitKernelCS;
889 ss = (RTSEL)(uintptr_t)&SUPR0Abs64bitKernelSS;
890 }
891 else
892 {
893 /* sysenter loads LDT cs & ss, VMX doesn't like this. Load the GDT ones (safe). */
894 cs = (RTSEL)(uintptr_t)&SUPR0AbsKernelCS;
895 ss = (RTSEL)(uintptr_t)&SUPR0AbsKernelSS;
896 }
897#else
898 cs = ASMGetCS();
899 ss = ASMGetSS();
900#endif
901 Assert(!(cs & X86_SEL_LDT)); Assert((cs & X86_SEL_RPL) == 0);
902 Assert(!(ss & X86_SEL_LDT)); Assert((ss & X86_SEL_RPL) == 0);
903 rc = VMXWriteVMCS(VMX_VMCS16_HOST_FIELD_CS, cs);
904 /* Note: VMX is (again) very picky about the RPL of the selectors here; we'll restore them manually. */
905 rc |= VMXWriteVMCS(VMX_VMCS16_HOST_FIELD_DS, 0);
906 rc |= VMXWriteVMCS(VMX_VMCS16_HOST_FIELD_ES, 0);
907#if HC_ARCH_BITS == 32
908 if (!VMX_IS_64BIT_HOST_MODE())
909 {
910 rc |= VMXWriteVMCS(VMX_VMCS16_HOST_FIELD_FS, 0);
911 rc |= VMXWriteVMCS(VMX_VMCS16_HOST_FIELD_GS, 0);
912 }
913#endif
914 rc |= VMXWriteVMCS(VMX_VMCS16_HOST_FIELD_SS, ss);
915 SelTR = ASMGetTR();
916 rc |= VMXWriteVMCS(VMX_VMCS16_HOST_FIELD_TR, SelTR);
917 AssertRC(rc);
918 Log2(("VMX_VMCS_HOST_FIELD_CS %08x (%08x)\n", cs, ASMGetSS()));
919 Log2(("VMX_VMCS_HOST_FIELD_DS 00000000 (%08x)\n", ASMGetDS()));
920 Log2(("VMX_VMCS_HOST_FIELD_ES 00000000 (%08x)\n", ASMGetES()));
921 Log2(("VMX_VMCS_HOST_FIELD_FS 00000000 (%08x)\n", ASMGetFS()));
922 Log2(("VMX_VMCS_HOST_FIELD_GS 00000000 (%08x)\n", ASMGetGS()));
923 Log2(("VMX_VMCS_HOST_FIELD_SS %08x (%08x)\n", ss, ASMGetSS()));
924 Log2(("VMX_VMCS_HOST_FIELD_TR %08x\n", ASMGetTR()));
925
926 /* GDTR & IDTR */
927#ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
928 if (VMX_IS_64BIT_HOST_MODE())
929 {
930 X86XDTR64 gdtr64, idtr64;
931 hwaccmR0Get64bitGDTRandIDTR(&gdtr64, &idtr64);
932 rc = VMXWriteVMCS64(VMX_VMCS_HOST_GDTR_BASE, gdtr64.uAddr);
933 rc |= VMXWriteVMCS64(VMX_VMCS_HOST_IDTR_BASE, gdtr64.uAddr);
934 AssertRC(rc);
935 Log2(("VMX_VMCS_HOST_GDTR_BASE %RX64\n", gdtr64.uAddr));
936 Log2(("VMX_VMCS_HOST_IDTR_BASE %RX64\n", idtr64.uAddr));
937 gdtr.cbGdt = gdtr64.cb;
938 gdtr.pGdt = (uintptr_t)gdtr64.uAddr;
939 }
940 else
941#endif
942 {
943 ASMGetGDTR(&gdtr);
944 rc = VMXWriteVMCS(VMX_VMCS_HOST_GDTR_BASE, gdtr.pGdt);
945 ASMGetIDTR(&idtr);
946 rc |= VMXWriteVMCS(VMX_VMCS_HOST_IDTR_BASE, idtr.pIdt);
947 AssertRC(rc);
948 Log2(("VMX_VMCS_HOST_GDTR_BASE %RHv\n", gdtr.pGdt));
949 Log2(("VMX_VMCS_HOST_IDTR_BASE %RHv\n", idtr.pIdt));
950 }
951
952
953 /* Save the base address of the TR selector. */
954 if (SelTR > gdtr.cbGdt)
955 {
956 AssertMsgFailed(("Invalid TR selector %x. GDTR.cbGdt=%x\n", SelTR, gdtr.cbGdt));
957 return VERR_VMX_INVALID_HOST_STATE;
958 }
959
960#ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
961 if (VMX_IS_64BIT_HOST_MODE())
962 {
963 pDesc = &((PX86DESCHC)gdtr.pGdt)[SelTR >> X86_SEL_SHIFT_HC]; /// ????
964 uint64_t trBase64 = X86DESC64_BASE(*(PX86DESC64)pDesc);
965 rc = VMXWriteVMCS64(VMX_VMCS_HOST_TR_BASE, trBase64);
966 Log2(("VMX_VMCS_HOST_TR_BASE %RX64\n", trBase64));
967 AssertRC(rc);
968 }
969 else
970#endif
971 {
972 pDesc = &((PX86DESCHC)gdtr.pGdt)[SelTR >> X86_SEL_SHIFT_HC];
973#if HC_ARCH_BITS == 64
974 trBase = X86DESC64_BASE(*pDesc);
975#else
976 trBase = X86DESC_BASE(*pDesc);
977#endif
978 rc = VMXWriteVMCS(VMX_VMCS_HOST_TR_BASE, trBase);
979 AssertRC(rc);
980 Log2(("VMX_VMCS_HOST_TR_BASE %RHv\n", trBase));
981 }
982
983 /* FS and GS base. */
984#if HC_ARCH_BITS == 64 || defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
985 if (VMX_IS_64BIT_HOST_MODE())
986 {
987 Log2(("MSR_K8_FS_BASE = %RX64\n", ASMRdMsr(MSR_K8_FS_BASE)));
988 Log2(("MSR_K8_GS_BASE = %RX64\n", ASMRdMsr(MSR_K8_GS_BASE)));
989 rc = VMXWriteVMCS64(VMX_VMCS_HOST_FS_BASE, ASMRdMsr(MSR_K8_FS_BASE));
990 rc |= VMXWriteVMCS64(VMX_VMCS_HOST_GS_BASE, ASMRdMsr(MSR_K8_GS_BASE));
991 }
992#endif
993 AssertRC(rc);
994
995 /* Sysenter MSRs. */
996 /** @todo expensive!! */
997 rc = VMXWriteVMCS(VMX_VMCS32_HOST_SYSENTER_CS, ASMRdMsr_Low(MSR_IA32_SYSENTER_CS));
998 Log2(("VMX_VMCS_HOST_SYSENTER_CS %08x\n", ASMRdMsr_Low(MSR_IA32_SYSENTER_CS)));
999#ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
1000 if (VMX_IS_64BIT_HOST_MODE())
1001 {
1002 Log2(("VMX_VMCS_HOST_SYSENTER_EIP %RX64\n", ASMRdMsr(MSR_IA32_SYSENTER_EIP)));
1003 Log2(("VMX_VMCS_HOST_SYSENTER_ESP %RX64\n", ASMRdMsr(MSR_IA32_SYSENTER_ESP)));
1004 rc |= VMXWriteVMCS64(VMX_VMCS_HOST_SYSENTER_ESP, ASMRdMsr(MSR_IA32_SYSENTER_ESP));
1005 rc |= VMXWriteVMCS64(VMX_VMCS_HOST_SYSENTER_EIP, ASMRdMsr(MSR_IA32_SYSENTER_EIP));
1006 }
1007 else
1008 {
1009 rc |= VMXWriteVMCS(VMX_VMCS_HOST_SYSENTER_ESP, ASMRdMsr_Low(MSR_IA32_SYSENTER_ESP));
1010 rc |= VMXWriteVMCS(VMX_VMCS_HOST_SYSENTER_EIP, ASMRdMsr_Low(MSR_IA32_SYSENTER_EIP));
1011 Log2(("VMX_VMCS_HOST_SYSENTER_EIP %RX32\n", ASMRdMsr_Low(MSR_IA32_SYSENTER_EIP)));
1012 Log2(("VMX_VMCS_HOST_SYSENTER_ESP %RX32\n", ASMRdMsr_Low(MSR_IA32_SYSENTER_ESP)));
1013 }
1014#elif HC_ARCH_BITS == 32
1015 rc |= VMXWriteVMCS(VMX_VMCS_HOST_SYSENTER_ESP, ASMRdMsr_Low(MSR_IA32_SYSENTER_ESP));
1016 rc |= VMXWriteVMCS(VMX_VMCS_HOST_SYSENTER_EIP, ASMRdMsr_Low(MSR_IA32_SYSENTER_EIP));
1017 Log2(("VMX_VMCS_HOST_SYSENTER_EIP %RX32\n", ASMRdMsr_Low(MSR_IA32_SYSENTER_EIP)));
1018 Log2(("VMX_VMCS_HOST_SYSENTER_ESP %RX32\n", ASMRdMsr_Low(MSR_IA32_SYSENTER_ESP)));
1019#else
1020 Log2(("VMX_VMCS_HOST_SYSENTER_EIP %RX64\n", ASMRdMsr(MSR_IA32_SYSENTER_EIP)));
1021 Log2(("VMX_VMCS_HOST_SYSENTER_ESP %RX64\n", ASMRdMsr(MSR_IA32_SYSENTER_ESP)));
1022 rc |= VMXWriteVMCS64(VMX_VMCS_HOST_SYSENTER_ESP, ASMRdMsr(MSR_IA32_SYSENTER_ESP));
1023 rc |= VMXWriteVMCS64(VMX_VMCS_HOST_SYSENTER_EIP, ASMRdMsr(MSR_IA32_SYSENTER_EIP));
1024#endif
1025 AssertRC(rc);
1026
1027#if 0 /* @todo deal with 32/64 */
1028 /* Restore the host EFER - on CPUs that support it. */
1029 if (pVM->hwaccm.s.vmx.msr.vmx_exit.n.allowed1 & VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_EFER_MSR)
1030 {
1031 uint64_t msrEFER = ASMRdMsr(MSR_IA32_EFER);
1032 rc = VMXWriteVMCS64(VMX_VMCS_HOST_FIELD_EFER_FULL, msrEFER);
1033 AssertRC(rc);
1034 }
1035#endif
1036 pVCpu->hwaccm.s.fContextUseFlags &= ~HWACCM_CHANGED_HOST_CONTEXT;
1037 }
1038 return rc;
1039}
1040
1041/**
1042 * Prefetch the 4 PDPT pointers (PAE and nested paging only)
1043 *
1044 * @param pVM The VM to operate on.
1045 * @param pVCpu The VMCPU to operate on.
1046 * @param pCtx Guest context
1047 */
1048static void vmxR0PrefetchPAEPdptrs(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1049{
1050 if (CPUMIsGuestInPAEModeEx(pCtx))
1051 {
1052 X86PDPE Pdpe;
1053
1054 for (unsigned i=0;i<4;i++)
1055 {
1056 Pdpe = PGMGstGetPaePDPtr(pVM, i);
1057 int rc = VMXWriteVMCS64(VMX_VMCS_GUEST_PDPTR0_FULL + i*2, Pdpe.u);
1058 AssertRC(rc);
1059 }
1060 }
1061}
1062
1063/**
1064 * Update the exception bitmap according to the current CPU state
1065 *
1066 * @param pVM The VM to operate on.
1067 * @param pVCpu The VMCPU to operate on.
1068 * @param pCtx Guest context
1069 */
1070static void vmxR0UpdateExceptionBitmap(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1071{
1072 uint32_t u32TrapMask;
1073 Assert(pCtx);
1074
1075 u32TrapMask = HWACCM_VMX_TRAP_MASK;
1076#ifndef DEBUG
1077 if (pVM->hwaccm.s.fNestedPaging)
1078 u32TrapMask &= ~RT_BIT(X86_XCPT_PF); /* no longer need to intercept #PF. */
1079#endif
1080
1081 /* Also catch floating point exceptions as we need to report them to the guest in a different way. */
1082 if ( CPUMIsGuestFPUStateActive(pVCpu) == true
1083 && !(pCtx->cr0 & X86_CR0_NE)
1084 && !pVCpu->hwaccm.s.fFPUOldStyleOverride)
1085 {
1086 u32TrapMask |= RT_BIT(X86_XCPT_MF);
1087 pVCpu->hwaccm.s.fFPUOldStyleOverride = true;
1088 }
1089
1090#ifdef DEBUG
1091 /* Intercept X86_XCPT_DB if stepping is enabled */
1092 if (DBGFIsStepping(pVM))
1093 u32TrapMask |= RT_BIT(X86_XCPT_DB);
1094#endif
1095
1096#ifdef VBOX_STRICT
1097 Assert(u32TrapMask & RT_BIT(X86_XCPT_GP));
1098#endif
1099
1100# ifdef HWACCM_VMX_EMULATE_REALMODE
1101 /* Intercept all exceptions in real mode as none of them can be injected directly (#GP otherwise). */
1102 if (CPUMIsGuestInRealModeEx(pCtx) && pVM->hwaccm.s.vmx.pRealModeTSS)
1103 u32TrapMask |= HWACCM_VMX_TRAP_MASK_REALMODE;
1104# endif /* HWACCM_VMX_EMULATE_REALMODE */
1105
1106 int rc = VMXWriteVMCS(VMX_VMCS_CTRL_EXCEPTION_BITMAP, u32TrapMask);
1107 AssertRC(rc);
1108}
1109
1110/**
1111 * Loads the guest state
1112 *
1113 * NOTE: Don't do anything here that can cause a jump back to ring 3!!!!!
1114 *
1115 * @returns VBox status code.
1116 * @param pVM The VM to operate on.
1117 * @param pVCpu The VMCPU to operate on.
1118 * @param pCtx Guest context
1119 */
1120VMMR0DECL(int) VMXR0LoadGuestState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1121{
1122 int rc = VINF_SUCCESS;
1123 RTGCUINTPTR val;
1124 X86EFLAGS eflags;
1125
1126 /* VMX_VMCS_CTRL_ENTRY_CONTROLS
1127 * Set required bits to one and zero according to the MSR capabilities.
1128 */
1129 val = pVM->hwaccm.s.vmx.msr.vmx_entry.n.disallowed0;
1130 /* Load guest debug controls (dr7 & IA32_DEBUGCTL_MSR) (forced to 1 on the 'first' VT-x capable CPUs; this actually includes the newest Nehalem CPUs) */
1131 val |= VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_DEBUG;
1132#if 0 /* @todo deal with 32/64 */
1133 /* Required for the EFER write below, not supported on all CPUs. */
1134 val |= VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_EFER_MSR;
1135#endif
1136 /* 64 bits guest mode? */
1137 if (pCtx->msrEFER & MSR_K6_EFER_LMA)
1138 val |= VMX_VMCS_CTRL_ENTRY_CONTROLS_IA64_MODE;
1139 /* else Must be zero when AMD64 is not available. */
1140
1141 /* Mask away the bits that the CPU doesn't support */
1142 val &= pVM->hwaccm.s.vmx.msr.vmx_entry.n.allowed1;
1143 rc = VMXWriteVMCS(VMX_VMCS_CTRL_ENTRY_CONTROLS, val);
1144 AssertRC(rc);
1145
1146 /* VMX_VMCS_CTRL_EXIT_CONTROLS
1147 * Set required bits to one and zero according to the MSR capabilities.
1148 */
1149 val = pVM->hwaccm.s.vmx.msr.vmx_exit.n.disallowed0;
1150
1151 /* Save debug controls (dr7 & IA32_DEBUGCTL_MSR) (forced to 1 on the 'first' VT-x capable CPUs; this actually includes the newest Nehalem CPUs) */
1152#if 0 /* @todo deal with 32/64 */
1153 val |= VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_DEBUG | VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_EFER_MSR;
1154#else
1155 val |= VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_DEBUG;
1156#endif
1157
1158#if HC_ARCH_BITS == 64 || defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1159 if (VMX_IS_64BIT_HOST_MODE())
1160 val |= VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_AMD64;
1161 /* else: Must be zero when AMD64 is not available. */
1162#elif HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS)
1163 if (pCtx->msrEFER & MSR_K6_EFER_LMA)
1164 val |= VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_AMD64; /* our switcher goes to long mode */
1165 else
1166 Assert(!(val & VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_AMD64));
1167#endif
1168 val &= pVM->hwaccm.s.vmx.msr.vmx_exit.n.allowed1;
1169 /* Don't acknowledge external interrupts on VM-exit. */
1170 rc = VMXWriteVMCS(VMX_VMCS_CTRL_EXIT_CONTROLS, val);
1171 AssertRC(rc);
1172
1173 /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
1174 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_SEGMENT_REGS)
1175 {
1176#ifdef HWACCM_VMX_EMULATE_REALMODE
1177 if (pVM->hwaccm.s.vmx.pRealModeTSS)
1178 {
1179 PGMMODE enmGuestMode = PGMGetGuestMode(pVM);
1180 if (pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode != enmGuestMode)
1181 {
1182 /* Correct weird requirements for switching to protected mode. */
1183 if ( pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
1184 && enmGuestMode >= PGMMODE_PROTECTED)
1185 {
1186 /* Flush the recompiler code cache as it's not unlikely
1187 * the guest will rewrite code it will later execute in real
1188 * mode (OpenBSD 4.0 is one such example)
1189 */
1190 REMFlushTBs(pVM);
1191
1192 /* DPL of all hidden selector registers must match the current CPL (0). */
1193 pCtx->csHid.Attr.n.u2Dpl = 0;
1194 pCtx->csHid.Attr.n.u4Type = X86_SEL_TYPE_CODE | X86_SEL_TYPE_RW_ACC;
1195
1196 pCtx->dsHid.Attr.n.u2Dpl = 0;
1197 pCtx->esHid.Attr.n.u2Dpl = 0;
1198 pCtx->fsHid.Attr.n.u2Dpl = 0;
1199 pCtx->gsHid.Attr.n.u2Dpl = 0;
1200 pCtx->ssHid.Attr.n.u2Dpl = 0;
1201
1202 /* The limit must correspond to the granularity bit. */
1203 if (!pCtx->csHid.Attr.n.u1Granularity)
1204 pCtx->csHid.u32Limit &= 0xffff;
1205 if (!pCtx->dsHid.Attr.n.u1Granularity)
1206 pCtx->dsHid.u32Limit &= 0xffff;
1207 if (!pCtx->esHid.Attr.n.u1Granularity)
1208 pCtx->esHid.u32Limit &= 0xffff;
1209 if (!pCtx->fsHid.Attr.n.u1Granularity)
1210 pCtx->fsHid.u32Limit &= 0xffff;
1211 if (!pCtx->gsHid.Attr.n.u1Granularity)
1212 pCtx->gsHid.u32Limit &= 0xffff;
1213 if (!pCtx->ssHid.Attr.n.u1Granularity)
1214 pCtx->ssHid.u32Limit &= 0xffff;
1215 }
1216 else
1217 /* Switching from protected mode to real mode. */
1218 if ( pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode >= PGMMODE_PROTECTED
1219 && enmGuestMode == PGMMODE_REAL)
1220 {
1221 /* The limit must also be set to 0xffff. */
1222 pCtx->csHid.u32Limit = 0xffff;
1223 pCtx->dsHid.u32Limit = 0xffff;
1224 pCtx->esHid.u32Limit = 0xffff;
1225 pCtx->fsHid.u32Limit = 0xffff;
1226 pCtx->gsHid.u32Limit = 0xffff;
1227 pCtx->ssHid.u32Limit = 0xffff;
1228
1229 Assert(pCtx->csHid.u64Base <= 0xfffff);
1230 Assert(pCtx->dsHid.u64Base <= 0xfffff);
1231 Assert(pCtx->esHid.u64Base <= 0xfffff);
1232 Assert(pCtx->fsHid.u64Base <= 0xfffff);
1233 Assert(pCtx->gsHid.u64Base <= 0xfffff);
1234 }
1235 pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode = enmGuestMode;
1236 }
1237 else
1238 /* VT-x will fail with a guest invalid state otherwise... (CPU state after a reset) */
1239 if ( CPUMIsGuestInRealModeEx(pCtx)
1240 && pCtx->csHid.u64Base == 0xffff0000)
1241 {
1242 pCtx->csHid.u64Base = 0xf0000;
1243 pCtx->cs = 0xf000;
1244 }
1245 }
1246#endif /* HWACCM_VMX_EMULATE_REALMODE */
1247
1248 VMX_WRITE_SELREG(ES, es);
1249 AssertRC(rc);
1250
1251 VMX_WRITE_SELREG(CS, cs);
1252 AssertRC(rc);
1253
1254 VMX_WRITE_SELREG(SS, ss);
1255 AssertRC(rc);
1256
1257 VMX_WRITE_SELREG(DS, ds);
1258 AssertRC(rc);
1259
1260 /* The base values in the hidden fs & gs registers are not in sync with the msrs; they are cut to 32 bits. */
1261 VMX_WRITE_SELREG(FS, fs);
1262 AssertRC(rc);
1263
1264 VMX_WRITE_SELREG(GS, gs);
1265 AssertRC(rc);
1266 }
1267
1268 /* Guest CPU context: LDTR. */
1269 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_LDTR)
1270 {
1271 if (pCtx->ldtr == 0)
1272 {
1273 rc = VMXWriteVMCS(VMX_VMCS16_GUEST_FIELD_LDTR, 0);
1274 rc |= VMXWriteVMCS(VMX_VMCS32_GUEST_LDTR_LIMIT, 0);
1275 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_LDTR_BASE, 0);
1276 /* Note: vmlaunch will fail with 0 or just 0x02. No idea why. */
1277 rc |= VMXWriteVMCS(VMX_VMCS32_GUEST_LDTR_ACCESS_RIGHTS, 0x82 /* present, LDT */);
1278 }
1279 else
1280 {
1281 rc = VMXWriteVMCS(VMX_VMCS16_GUEST_FIELD_LDTR, pCtx->ldtr);
1282 rc |= VMXWriteVMCS(VMX_VMCS32_GUEST_LDTR_LIMIT, pCtx->ldtrHid.u32Limit);
1283 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_LDTR_BASE, pCtx->ldtrHid.u64Base);
1284 rc |= VMXWriteVMCS(VMX_VMCS32_GUEST_LDTR_ACCESS_RIGHTS, pCtx->ldtrHid.Attr.u);
1285 }
1286 AssertRC(rc);
1287 }
1288 /* Guest CPU context: TR. */
1289 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_TR)
1290 {
1291#ifdef HWACCM_VMX_EMULATE_REALMODE
1292 /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
1293 if (CPUMIsGuestInRealModeEx(pCtx))
1294 {
1295 RTGCPHYS GCPhys;
1296
1297 /* We convert it here every time as pci regions could be reconfigured. */
1298 rc = PDMVMMDevHeapR3ToGCPhys(pVM, pVM->hwaccm.s.vmx.pRealModeTSS, &GCPhys);
1299 AssertRC(rc);
1300
1301 rc = VMXWriteVMCS(VMX_VMCS16_GUEST_FIELD_TR, 0);
1302 rc |= VMXWriteVMCS(VMX_VMCS32_GUEST_TR_LIMIT, HWACCM_VTX_TSS_SIZE);
1303 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_TR_BASE, GCPhys /* phys = virt in this mode */);
1304
1305 X86DESCATTR attr;
1306
1307 attr.u = 0;
1308 attr.n.u1Present = 1;
1309 attr.n.u4Type = X86_SEL_TYPE_SYS_386_TSS_BUSY;
1310 val = attr.u;
1311 }
1312 else
1313#endif /* HWACCM_VMX_EMULATE_REALMODE */
1314 {
1315 rc = VMXWriteVMCS(VMX_VMCS16_GUEST_FIELD_TR, pCtx->tr);
1316 rc |= VMXWriteVMCS(VMX_VMCS32_GUEST_TR_LIMIT, pCtx->trHid.u32Limit);
1317 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_TR_BASE, pCtx->trHid.u64Base);
1318
1319 val = pCtx->trHid.Attr.u;
1320
1321 /* The TSS selector must be busy. */
1322 if ((val & 0xF) == X86_SEL_TYPE_SYS_286_TSS_AVAIL)
1323 val = (val & ~0xF) | X86_SEL_TYPE_SYS_286_TSS_BUSY;
1324 else
1325 /* Default even if no TR selector has been set (otherwise vmlaunch will fail!) */
1326 val = (val & ~0xF) | X86_SEL_TYPE_SYS_386_TSS_BUSY;
1327
1328 }
1329 rc |= VMXWriteVMCS(VMX_VMCS32_GUEST_TR_ACCESS_RIGHTS, val);
1330 AssertRC(rc);
1331 }
1332 /* Guest CPU context: GDTR. */
1333 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_GDTR)
1334 {
1335 rc = VMXWriteVMCS(VMX_VMCS32_GUEST_GDTR_LIMIT, pCtx->gdtr.cbGdt);
1336 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_GDTR_BASE, pCtx->gdtr.pGdt);
1337 AssertRC(rc);
1338 }
1339 /* Guest CPU context: IDTR. */
1340 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_IDTR)
1341 {
1342 rc = VMXWriteVMCS(VMX_VMCS32_GUEST_IDTR_LIMIT, pCtx->idtr.cbIdt);
1343 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_IDTR_BASE, pCtx->idtr.pIdt);
1344 AssertRC(rc);
1345 }
1346
1347 /*
1348 * Sysenter MSRs (unconditional)
1349 */
1350 rc = VMXWriteVMCS(VMX_VMCS32_GUEST_SYSENTER_CS, pCtx->SysEnter.cs);
1351 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_SYSENTER_EIP, pCtx->SysEnter.eip);
1352 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_SYSENTER_ESP, pCtx->SysEnter.esp);
1353 AssertRC(rc);
1354
1355 /* Control registers */
1356 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR0)
1357 {
1358 val = pCtx->cr0;
1359 rc = VMXWriteVMCS(VMX_VMCS_CTRL_CR0_READ_SHADOW, val);
1360 Log2(("Guest CR0-shadow %08x\n", val));
1361 if (CPUMIsGuestFPUStateActive(pVCpu) == false)
1362 {
1363 /* Always use #NM exceptions to load the FPU/XMM state on demand. */
1364 val |= X86_CR0_TS | X86_CR0_ET | X86_CR0_NE | X86_CR0_MP;
1365 }
1366 else
1367 {
1368 /** @todo check if we support the old style mess correctly. */
1369 if (!(val & X86_CR0_NE))
1370 Log(("Forcing X86_CR0_NE!!!\n"));
1371
1372 val |= X86_CR0_NE; /* always turn on the native mechanism to report FPU errors (old style uses interrupts) */
1373 }
1374 /* Note: protected mode & paging are always enabled; we use them for emulating real and protected mode without paging too. */
1375 val |= X86_CR0_PE | X86_CR0_PG;
1376 if (pVM->hwaccm.s.fNestedPaging)
1377 {
1378 if (CPUMIsGuestInPagedProtectedModeEx(pCtx))
1379 {
1380 /* Disable cr3 read/write monitoring as we don't need it for EPT. */
1381 pVCpu->hwaccm.s.vmx.proc_ctls &= ~( VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT
1382 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT);
1383 }
1384 else
1385 {
1386 /* Reenable cr3 read/write monitoring as our identity mapped page table is active. */
1387 pVCpu->hwaccm.s.vmx.proc_ctls |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT
1388 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT;
1389 }
1390 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVCpu->hwaccm.s.vmx.proc_ctls);
1391 AssertRC(rc);
1392 }
1393 else
1394 {
1395 /* Note: We must also set this as we rely on protecting various pages for which supervisor writes must be caught. */
1396 val |= X86_CR0_WP;
1397 }
1398
1399 /* Always enable caching. */
1400 val &= ~(X86_CR0_CD|X86_CR0_NW);
1401
1402 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_CR0, val);
1403 Log2(("Guest CR0 %08x\n", val));
1404 /* CR0 flags owned by the host; if the guests attempts to change them, then
1405 * the VM will exit.
1406 */
1407 val = X86_CR0_PE /* Must monitor this bit (assumptions are made for real mode emulation) */
1408 | X86_CR0_WP /* Must monitor this bit (it must always be enabled). */
1409 | X86_CR0_PG /* Must monitor this bit (assumptions are made for real mode & protected mode without paging emulation) */
1410 | X86_CR0_TS
1411 | X86_CR0_ET /* Bit not restored during VM-exit! */
1412 | X86_CR0_CD /* Bit not restored during VM-exit! */
1413 | X86_CR0_NW /* Bit not restored during VM-exit! */
1414 | X86_CR0_NE
1415 | X86_CR0_MP;
1416 pVCpu->hwaccm.s.vmx.cr0_mask = val;
1417
1418 rc |= VMXWriteVMCS(VMX_VMCS_CTRL_CR0_MASK, val);
1419 Log2(("Guest CR0-mask %08x\n", val));
1420 AssertRC(rc);
1421 }
1422 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR4)
1423 {
1424 /* CR4 */
1425 rc = VMXWriteVMCS(VMX_VMCS_CTRL_CR4_READ_SHADOW, pCtx->cr4);
1426 Log2(("Guest CR4-shadow %08x\n", pCtx->cr4));
1427 /* Set the required bits in cr4 too (currently X86_CR4_VMXE). */
1428 val = pCtx->cr4 | (uint32_t)pVM->hwaccm.s.vmx.msr.vmx_cr4_fixed0;
1429
1430 if (!pVM->hwaccm.s.fNestedPaging)
1431 {
1432 switch(pVCpu->hwaccm.s.enmShadowMode)
1433 {
1434 case PGMMODE_REAL: /* Real mode -> emulated using v86 mode */
1435 case PGMMODE_PROTECTED: /* Protected mode, no paging -> emulated using identity mapping. */
1436 case PGMMODE_32_BIT: /* 32-bit paging. */
1437 val &= ~X86_CR4_PAE;
1438 break;
1439
1440 case PGMMODE_PAE: /* PAE paging. */
1441 case PGMMODE_PAE_NX: /* PAE paging with NX enabled. */
1442 /** @todo use normal 32 bits paging */
1443 val |= X86_CR4_PAE;
1444 break;
1445
1446 case PGMMODE_AMD64: /* 64-bit AMD paging (long mode). */
1447 case PGMMODE_AMD64_NX: /* 64-bit AMD paging (long mode) with NX enabled. */
1448#ifdef VBOX_ENABLE_64_BITS_GUESTS
1449 break;
1450#else
1451 AssertFailed();
1452 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
1453#endif
1454 default: /* shut up gcc */
1455 AssertFailed();
1456 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
1457 }
1458 }
1459 else
1460 if (!CPUMIsGuestInPagedProtectedModeEx(pCtx))
1461 {
1462 /* We use 4 MB pages in our identity mapping page table for real and protected mode without paging. */
1463 val |= X86_CR4_PSE;
1464 /* Our identity mapping is a 32 bits page directory. */
1465 val &= ~X86_CR4_PAE;
1466 }
1467
1468#ifdef HWACCM_VMX_EMULATE_REALMODE
1469 /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
1470 if (CPUMIsGuestInRealModeEx(pCtx))
1471 val |= X86_CR4_VME;
1472#endif /* HWACCM_VMX_EMULATE_REALMODE */
1473
1474 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_CR4, val);
1475 Log2(("Guest CR4 %08x\n", val));
1476 /* CR4 flags owned by the host; if the guests attempts to change them, then
1477 * the VM will exit.
1478 */
1479 val = 0
1480#ifdef HWACCM_VMX_EMULATE_REALMODE
1481 | (pVM->hwaccm.s.vmx.pRealModeTSS ? X86_CR4_VME : 0)
1482#endif
1483 | X86_CR4_PAE
1484 | X86_CR4_PGE
1485 | X86_CR4_PSE
1486 | X86_CR4_VMXE;
1487 pVCpu->hwaccm.s.vmx.cr4_mask = val;
1488
1489 rc |= VMXWriteVMCS(VMX_VMCS_CTRL_CR4_MASK, val);
1490 Log2(("Guest CR4-mask %08x\n", val));
1491 AssertRC(rc);
1492 }
1493
1494 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_CR3)
1495 {
1496 if (pVM->hwaccm.s.fNestedPaging)
1497 {
1498 AssertMsg( PGMGetEPTCR3(pVM) == PGMGetHyperCR3(pVM)
1499 || VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL),
1500 ("%RHp vs %RHp\n", PGMGetEPTCR3(pVM), PGMGetHyperCR3(pVM)));
1501 pVCpu->hwaccm.s.vmx.GCPhysEPTP = PGMGetEPTCR3(pVM);
1502
1503 Assert(!(pVCpu->hwaccm.s.vmx.GCPhysEPTP & 0xfff));
1504 /** @todo Check the IA32_VMX_EPT_VPID_CAP MSR for other supported memory types. */
1505 pVCpu->hwaccm.s.vmx.GCPhysEPTP |= VMX_EPT_MEMTYPE_WB
1506 | (VMX_EPT_PAGE_WALK_LENGTH_DEFAULT << VMX_EPT_PAGE_WALK_LENGTH_SHIFT);
1507
1508 rc = VMXWriteVMCS64(VMX_VMCS_CTRL_EPTP_FULL, pVCpu->hwaccm.s.vmx.GCPhysEPTP);
1509 AssertRC(rc);
1510
1511 if (!CPUMIsGuestInPagedProtectedModeEx(pCtx))
1512 {
1513 RTGCPHYS GCPhys;
1514
1515 /* We convert it here every time as pci regions could be reconfigured. */
1516 rc = PDMVMMDevHeapR3ToGCPhys(pVM, pVM->hwaccm.s.vmx.pNonPagingModeEPTPageTable, &GCPhys);
1517 AssertRC(rc);
1518
1519 /* We use our identity mapping page table here as we need to map guest virtual to guest physical addresses; EPT will
1520 * take care of the translation to host physical addresses.
1521 */
1522 val = GCPhys;
1523 }
1524 else
1525 {
1526 /* Save the real guest CR3 in VMX_VMCS_GUEST_CR3 */
1527 val = pCtx->cr3;
1528 /* Prefetch the four PDPT entries in PAE mode. */
1529 vmxR0PrefetchPAEPdptrs(pVM, pVCpu, pCtx);
1530 }
1531 }
1532 else
1533 {
1534 val = PGMGetHyperCR3(pVM);
1535 Assert(val || VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL));
1536 }
1537
1538 /* Save our shadow CR3 register. */
1539 rc = VMXWriteVMCS64(VMX_VMCS64_GUEST_CR3, val);
1540 AssertRC(rc);
1541 }
1542
1543 /* Debug registers. */
1544 if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_DEBUG)
1545 {
1546 pCtx->dr[6] |= X86_DR6_INIT_VAL; /* set all reserved bits to 1. */
1547 pCtx->dr[6] &= ~RT_BIT(12); /* must be zero. */
1548
1549 pCtx->dr[7] &= 0xffffffff; /* upper 32 bits reserved */
1550 pCtx->dr[7] &= ~(RT_BIT(11) | RT_BIT(12) | RT_BIT(14) | RT_BIT(15)); /* must be zero */
1551 pCtx->dr[7] |= 0x400; /* must be one */
1552
1553 /* Resync DR7 */
1554 rc = VMXWriteVMCS64(VMX_VMCS64_GUEST_DR7, pCtx->dr[7]);
1555 AssertRC(rc);
1556
1557 /* Sync the debug state now if any breakpoint is armed. */
1558 if ( (pCtx->dr[7] & (X86_DR7_ENABLED_MASK|X86_DR7_GD))
1559 && !CPUMIsGuestDebugStateActive(pVM)
1560 && !DBGFIsStepping(pVM))
1561 {
1562 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatDRxArmed);
1563
1564 /* Disable drx move intercepts. */
1565 pVCpu->hwaccm.s.vmx.proc_ctls &= ~VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT;
1566 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVCpu->hwaccm.s.vmx.proc_ctls);
1567 AssertRC(rc);
1568
1569 /* Save the host and load the guest debug state. */
1570 rc = CPUMR0LoadGuestDebugState(pVM, pVCpu, pCtx, true /* include DR6 */);
1571 AssertRC(rc);
1572 }
1573
1574 /* IA32_DEBUGCTL MSR. */
1575 rc = VMXWriteVMCS64(VMX_VMCS_GUEST_DEBUGCTL_FULL, 0);
1576 AssertRC(rc);
1577
1578 /** @todo do we really ever need this? */
1579 rc |= VMXWriteVMCS(VMX_VMCS_GUEST_DEBUG_EXCEPTIONS, 0);
1580 AssertRC(rc);
1581 }
1582
1583 /* EIP, ESP and EFLAGS */
1584 rc = VMXWriteVMCS64(VMX_VMCS64_GUEST_RIP, pCtx->rip);
1585 rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_RSP, pCtx->rsp);
1586 AssertRC(rc);
1587
1588 /* Bits 22-31, 15, 5 & 3 must be zero. Bit 1 must be 1. */
1589 eflags = pCtx->eflags;
1590 eflags.u32 &= VMX_EFLAGS_RESERVED_0;
1591 eflags.u32 |= VMX_EFLAGS_RESERVED_1;
1592
1593#ifdef HWACCM_VMX_EMULATE_REALMODE
1594 /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
1595 if (CPUMIsGuestInRealModeEx(pCtx))
1596 {
1597 pVCpu->hwaccm.s.vmx.RealMode.eflags = eflags;
1598
1599 eflags.Bits.u1VM = 1;
1600 eflags.Bits.u2IOPL = 3;
1601 }
1602#endif /* HWACCM_VMX_EMULATE_REALMODE */
1603 rc = VMXWriteVMCS(VMX_VMCS_GUEST_RFLAGS, eflags.u32);
1604 AssertRC(rc);
1605
1606 /* TSC offset. */
1607 uint64_t u64TSCOffset;
1608
1609 if (TMCpuTickCanUseRealTSC(pVM, &u64TSCOffset))
1610 {
1611 /* Note: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT takes precedence over TSC_OFFSET */
1612 rc = VMXWriteVMCS64(VMX_VMCS_CTRL_TSC_OFFSET_FULL, u64TSCOffset);
1613 AssertRC(rc);
1614
1615 pVCpu->hwaccm.s.vmx.proc_ctls &= ~VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT;
1616 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVCpu->hwaccm.s.vmx.proc_ctls);
1617 AssertRC(rc);
1618 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatTSCOffset);
1619 }
1620 else
1621 {
1622 pVCpu->hwaccm.s.vmx.proc_ctls |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT;
1623 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVCpu->hwaccm.s.vmx.proc_ctls);
1624 AssertRC(rc);
1625 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatTSCIntercept);
1626 }
1627
1628 /* 64 bits guest mode? */
1629 if (pCtx->msrEFER & MSR_K6_EFER_LMA)
1630 {
1631#if !defined(VBOX_ENABLE_64_BITS_GUESTS)
1632 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
1633#elif HC_ARCH_BITS == 32 && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1634 pVCpu->hwaccm.s.vmx.pfnStartVM = VMXR0SwitcherStartVM64;
1635#else
1636# ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
1637 if (!pVM->hwaccm.s.fAllow64BitGuests)
1638 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
1639# endif
1640 pVCpu->hwaccm.s.vmx.pfnStartVM = VMXR0StartVM64;
1641#endif
1642 /* Unconditionally update these as wrmsr might have changed them. */
1643 rc = VMXWriteVMCS64(VMX_VMCS64_GUEST_FS_BASE, pCtx->fsHid.u64Base);
1644 AssertRC(rc);
1645 rc = VMXWriteVMCS64(VMX_VMCS64_GUEST_GS_BASE, pCtx->gsHid.u64Base);
1646 AssertRC(rc);
1647 }
1648 else
1649 {
1650 pVCpu->hwaccm.s.vmx.pfnStartVM = VMXR0StartVM32;
1651 }
1652
1653#if 0 /* @todo deal with 32/64 */
1654 /* Unconditionally update the guest EFER - on CPUs that supports it. */
1655 if (pVM->hwaccm.s.vmx.msr.vmx_entry.n.allowed1 & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_EFER_MSR)
1656 {
1657 rc = VMXWriteVMCS64(VMX_VMCS_GUEST_EFER_FULL, pCtx->msrEFER);
1658 AssertRC(rc);
1659 }
1660#endif
1661
1662 vmxR0UpdateExceptionBitmap(pVM, pVCpu, pCtx);
1663
1664 /* Done. */
1665 pVCpu->hwaccm.s.fContextUseFlags &= ~HWACCM_CHANGED_ALL_GUEST;
1666
1667 return rc;
1668}
1669
1670/**
1671 * Syncs back the guest state
1672 *
1673 * @returns VBox status code.
1674 * @param pVM The VM to operate on.
1675 * @param pVCpu The VMCPU to operate on.
1676 * @param pCtx Guest context
1677 */
1678DECLINLINE(int) VMXR0SaveGuestState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1679{
1680 RTGCUINTREG val, valShadow;
1681 RTGCUINTPTR uInterruptState;
1682 int rc;
1683
1684 /* Let's first sync back eip, esp, and eflags. */
1685 rc = VMXReadCachedVMCS(VMX_VMCS64_GUEST_RIP, &val);
1686 AssertRC(rc);
1687 pCtx->rip = val;
1688 rc = VMXReadCachedVMCS(VMX_VMCS64_GUEST_RSP, &val);
1689 AssertRC(rc);
1690 pCtx->rsp = val;
1691 rc = VMXReadCachedVMCS(VMX_VMCS_GUEST_RFLAGS, &val);
1692 AssertRC(rc);
1693 pCtx->eflags.u32 = val;
1694
1695 /* Take care of instruction fusing (sti, mov ss) */
1696 rc |= VMXReadCachedVMCS(VMX_VMCS32_GUEST_INTERRUPTIBILITY_STATE, &val);
1697 uInterruptState = val;
1698 if (uInterruptState != 0)
1699 {
1700 Assert(uInterruptState <= 2); /* only sti & mov ss */
1701 Log(("uInterruptState %x eip=%RGv\n", uInterruptState, pCtx->rip));
1702 EMSetInhibitInterruptsPC(pVM, pCtx->rip);
1703 }
1704 else
1705 VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
1706
1707 /* Control registers. */
1708 VMXReadCachedVMCS(VMX_VMCS_CTRL_CR0_READ_SHADOW, &valShadow);
1709 VMXReadCachedVMCS(VMX_VMCS64_GUEST_CR0, &val);
1710 val = (valShadow & pVCpu->hwaccm.s.vmx.cr0_mask) | (val & ~pVCpu->hwaccm.s.vmx.cr0_mask);
1711 CPUMSetGuestCR0(pVM, val);
1712
1713 VMXReadCachedVMCS(VMX_VMCS_CTRL_CR4_READ_SHADOW, &valShadow);
1714 VMXReadCachedVMCS(VMX_VMCS64_GUEST_CR4, &val);
1715 val = (valShadow & pVCpu->hwaccm.s.vmx.cr4_mask) | (val & ~pVCpu->hwaccm.s.vmx.cr4_mask);
1716 CPUMSetGuestCR4(pVM, val);
1717
1718 /* Note: no reason to sync back the CRx registers. They can't be changed by the guest. */
1719 /* Note: only in the nested paging case can CR3 & CR4 be changed by the guest. */
1720 if ( pVM->hwaccm.s.fNestedPaging
1721 && CPUMIsGuestInPagedProtectedModeEx(pCtx))
1722 {
1723 PVMCSCACHE pCache = &pVCpu->hwaccm.s.vmx.VMCSCache;
1724
1725 /* Can be updated behind our back in the nested paging case. */
1726 CPUMSetGuestCR2(pVM, pCache->cr2);
1727
1728 VMXReadCachedVMCS(VMX_VMCS64_GUEST_CR3, &val);
1729
1730 if (val != pCtx->cr3)
1731 {
1732 CPUMSetGuestCR3(pVM, val);
1733 PGMUpdateCR3(pVM, val);
1734 }
1735 /* Prefetch the four PDPT entries in PAE mode. */
1736 vmxR0PrefetchPAEPdptrs(pVM, pVCpu, pCtx);
1737 }
1738
1739 /* Sync back DR7 here. */
1740 VMXReadCachedVMCS(VMX_VMCS64_GUEST_DR7, &val);
1741 pCtx->dr[7] = val;
1742
1743 /* Guest CPU context: ES, CS, SS, DS, FS, GS. */
1744 VMX_READ_SELREG(ES, es);
1745 VMX_READ_SELREG(SS, ss);
1746 VMX_READ_SELREG(CS, cs);
1747 VMX_READ_SELREG(DS, ds);
1748 VMX_READ_SELREG(FS, fs);
1749 VMX_READ_SELREG(GS, gs);
1750
1751 /*
1752 * System MSRs
1753 */
1754 VMXReadCachedVMCS(VMX_VMCS32_GUEST_SYSENTER_CS, &val);
1755 pCtx->SysEnter.cs = val;
1756 VMXReadCachedVMCS(VMX_VMCS64_GUEST_SYSENTER_EIP, &val);
1757 pCtx->SysEnter.eip = val;
1758 VMXReadCachedVMCS(VMX_VMCS64_GUEST_SYSENTER_ESP, &val);
1759 pCtx->SysEnter.esp = val;
1760
1761 /* Misc. registers; must sync everything otherwise we can get out of sync when jumping to ring 3. */
1762 VMX_READ_SELREG(LDTR, ldtr);
1763
1764 VMXReadCachedVMCS(VMX_VMCS32_GUEST_GDTR_LIMIT, &val);
1765 pCtx->gdtr.cbGdt = val;
1766 VMXReadCachedVMCS(VMX_VMCS64_GUEST_GDTR_BASE, &val);
1767 pCtx->gdtr.pGdt = val;
1768
1769 VMXReadCachedVMCS(VMX_VMCS32_GUEST_IDTR_LIMIT, &val);
1770 pCtx->idtr.cbIdt = val;
1771 VMXReadCachedVMCS(VMX_VMCS64_GUEST_IDTR_BASE, &val);
1772 pCtx->idtr.pIdt = val;
1773
1774#ifdef HWACCM_VMX_EMULATE_REALMODE
1775 /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
1776 if (CPUMIsGuestInRealModeEx(pCtx))
1777 {
1778 /* Hide our emulation flags */
1779 pCtx->eflags.Bits.u1VM = 0;
1780 pCtx->eflags.Bits.u2IOPL = pVCpu->hwaccm.s.vmx.RealMode.eflags.Bits.u2IOPL;
1781
1782 /* Force a TR resync every time in case we switch modes. */
1783 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_TR;
1784 }
1785 else
1786#endif /* HWACCM_VMX_EMULATE_REALMODE */
1787 {
1788 /* In real mode we have a fake TSS, so only sync it back when it's supposed to be valid. */
1789 VMX_READ_SELREG(TR, tr);
1790 }
1791 return VINF_SUCCESS;
1792}
1793
1794/**
1795 * Dummy placeholder
1796 *
1797 * @param pVM The VM to operate on.
1798 * @param pVCpu The VMCPU to operate on.
1799 */
1800static void vmxR0SetupTLBDummy(PVM pVM, PVMCPU pVCpu)
1801{
1802 NOREF(pVM);
1803 NOREF(pVCpu);
1804 return;
1805}
1806
1807/**
1808 * Setup the tagged TLB for EPT
1809 *
1810 * @returns VBox status code.
1811 * @param pVM The VM to operate on.
1812 * @param pVCpu The VMCPU to operate on.
1813 */
1814static void vmxR0SetupTLBEPT(PVM pVM, PVMCPU pVCpu)
1815{
1816 PHWACCM_CPUINFO pCpu;
1817
1818 Assert(pVM->hwaccm.s.fNestedPaging);
1819 Assert(!pVM->hwaccm.s.vmx.fVPID);
1820
1821 /* Deal with tagged TLBs if VPID or EPT is supported. */
1822 pCpu = HWACCMR0GetCurrentCpu();
1823 /* Force a TLB flush for the first world switch if the current cpu differs from the one we ran on last. */
1824 /* Note that this can happen both for start and resume due to long jumps back to ring 3. */
1825 if ( pVCpu->hwaccm.s.idLastCpu != pCpu->idCpu
1826 /* if the tlb flush count has changed, another VM has flushed the TLB of this cpu, so we can't use our current ASID anymore. */
1827 || pVCpu->hwaccm.s.cTLBFlushes != pCpu->cTLBFlushes)
1828 {
1829 /* Force a TLB flush on VM entry. */
1830 pVCpu->hwaccm.s.fForceTLBFlush = true;
1831 }
1832 else
1833 Assert(!pCpu->fFlushTLB);
1834
1835 pVCpu->hwaccm.s.idLastCpu = pCpu->idCpu;
1836 pCpu->fFlushTLB = false;
1837
1838 if (pVCpu->hwaccm.s.fForceTLBFlush)
1839 vmxR0FlushEPT(pVM, pVCpu, pVM->hwaccm.s.vmx.enmFlushContext, 0);
1840
1841#ifdef VBOX_WITH_STATISTICS
1842 if (pVCpu->hwaccm.s.fForceTLBFlush)
1843 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatFlushTLBWorldSwitch);
1844 else
1845 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatNoFlushTLBWorldSwitch);
1846#endif
1847}
1848
1849#ifdef HWACCM_VTX_WITH_VPID
1850/**
1851 * Setup the tagged TLB for VPID
1852 *
1853 * @returns VBox status code.
1854 * @param pVM The VM to operate on.
1855 * @param pVCpu The VMCPU to operate on.
1856 */
1857static void vmxR0SetupTLBVPID(PVM pVM, PVMCPU pVCpu)
1858{
1859 PHWACCM_CPUINFO pCpu;
1860
1861 Assert(pVM->hwaccm.s.vmx.fVPID);
1862 Assert(!pVM->hwaccm.s.fNestedPaging);
1863
1864 /* Deal with tagged TLBs if VPID or EPT is supported. */
1865 pCpu = HWACCMR0GetCurrentCpu();
1866 /* Force a TLB flush for the first world switch if the current cpu differs from the one we ran on last. */
1867 /* Note that this can happen both for start and resume due to long jumps back to ring 3. */
1868 if ( pVCpu->hwaccm.s.idLastCpu != pCpu->idCpu
1869 /* if the tlb flush count has changed, another VM has flushed the TLB of this cpu, so we can't use our current ASID anymore. */
1870 || pVCpu->hwaccm.s.cTLBFlushes != pCpu->cTLBFlushes)
1871 {
1872 /* Force a TLB flush on VM entry. */
1873 pVCpu->hwaccm.s.fForceTLBFlush = true;
1874 }
1875 else
1876 Assert(!pCpu->fFlushTLB);
1877
1878 pVCpu->hwaccm.s.idLastCpu = pCpu->idCpu;
1879
1880 /* Make sure we flush the TLB when required. Switch ASID to achieve the same thing, but without actually flushing the whole TLB (which is expensive). */
1881 if (pVCpu->hwaccm.s.fForceTLBFlush)
1882 {
1883 if ( ++pCpu->uCurrentASID >= pVM->hwaccm.s.uMaxASID
1884 || pCpu->fFlushTLB)
1885 {
1886 pCpu->fFlushTLB = false;
1887 pCpu->uCurrentASID = 1; /* start at 1; host uses 0 */
1888 pCpu->cTLBFlushes++;
1889 }
1890 else
1891 {
1892 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatFlushASID);
1893 pVCpu->hwaccm.s.fForceTLBFlush = false;
1894 }
1895
1896 pVCpu->hwaccm.s.cTLBFlushes = pCpu->cTLBFlushes;
1897 pVCpu->hwaccm.s.uCurrentASID = pCpu->uCurrentASID;
1898 }
1899 else
1900 {
1901 Assert(!pCpu->fFlushTLB);
1902
1903 if (!pCpu->uCurrentASID || !pVCpu->hwaccm.s.uCurrentASID)
1904 pVCpu->hwaccm.s.uCurrentASID = pCpu->uCurrentASID = 1;
1905 }
1906 AssertMsg(pVCpu->hwaccm.s.cTLBFlushes == pCpu->cTLBFlushes, ("Flush count mismatch for cpu %d (%x vs %x)\n", pCpu->idCpu, pVCpu->hwaccm.s.cTLBFlushes, pCpu->cTLBFlushes));
1907 AssertMsg(pCpu->uCurrentASID >= 1 && pCpu->uCurrentASID < pVM->hwaccm.s.uMaxASID, ("cpu%d uCurrentASID = %x\n", pCpu->idCpu, pCpu->uCurrentASID));
1908 AssertMsg(pVCpu->hwaccm.s.uCurrentASID >= 1 && pVCpu->hwaccm.s.uCurrentASID < pVM->hwaccm.s.uMaxASID, ("cpu%d VM uCurrentASID = %x\n", pCpu->idCpu, pVCpu->hwaccm.s.uCurrentASID));
1909
1910 int rc = VMXWriteVMCS(VMX_VMCS16_GUEST_FIELD_VPID, pVCpu->hwaccm.s.uCurrentASID);
1911 AssertRC(rc);
1912
1913 if (pVCpu->hwaccm.s.fForceTLBFlush)
1914 vmxR0FlushVPID(pVM, pVCpu, pVM->hwaccm.s.vmx.enmFlushContext, 0);
1915
1916#ifdef VBOX_WITH_STATISTICS
1917 if (pVCpu->hwaccm.s.fForceTLBFlush)
1918 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatFlushTLBWorldSwitch);
1919 else
1920 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatNoFlushTLBWorldSwitch);
1921#endif
1922}
1923#endif /* HWACCM_VTX_WITH_VPID */
1924
1925/**
1926 * Runs guest code in a VT-x VM.
1927 *
1928 * @returns VBox status code.
1929 * @param pVM The VM to operate on.
1930 * @param pVCpu The VMCPU to operate on.
1931 * @param pCtx Guest context
1932 */
1933VMMR0DECL(int) VMXR0RunGuestCode(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1934{
1935 int rc = VINF_SUCCESS;
1936 RTGCUINTREG val;
1937 RTGCUINTREG exitReason, instrError, cbInstr;
1938 RTGCUINTPTR exitQualification;
1939 RTGCUINTPTR intInfo = 0; /* shut up buggy gcc 4 */
1940 RTGCUINTPTR errCode, instrInfo;
1941 bool fSyncTPR = false;
1942 PHWACCM_CPUINFO pCpu = 0;
1943 unsigned cResume = 0;
1944#ifdef VBOX_STRICT
1945 RTCPUID idCpuCheck;
1946#endif
1947#ifdef VBOX_WITH_STATISTICS
1948 bool fStatEntryStarted = true;
1949 bool fStatExit2Started = false;
1950#endif
1951
1952 Log2(("\nE"));
1953
1954 STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatEntry, x);
1955
1956#ifdef VBOX_STRICT
1957 {
1958 RTCCUINTREG val;
1959
1960 rc = VMXReadVMCS(VMX_VMCS_CTRL_PIN_EXEC_CONTROLS, &val);
1961 AssertRC(rc);
1962 Log2(("VMX_VMCS_CTRL_PIN_EXEC_CONTROLS = %08x\n", val));
1963
1964 /* allowed zero */
1965 if ((val & pVM->hwaccm.s.vmx.msr.vmx_pin_ctls.n.disallowed0) != pVM->hwaccm.s.vmx.msr.vmx_pin_ctls.n.disallowed0)
1966 Log(("Invalid VMX_VMCS_CTRL_PIN_EXEC_CONTROLS: zero\n"));
1967
1968 /* allowed one */
1969 if ((val & ~pVM->hwaccm.s.vmx.msr.vmx_pin_ctls.n.allowed1) != 0)
1970 Log(("Invalid VMX_VMCS_CTRL_PIN_EXEC_CONTROLS: one\n"));
1971
1972 rc = VMXReadVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, &val);
1973 AssertRC(rc);
1974 Log2(("VMX_VMCS_CTRL_PROC_EXEC_CONTROLS = %08x\n", val));
1975
1976 /* Must be set according to the MSR, but can be cleared in case of EPT. */
1977 if (pVM->hwaccm.s.fNestedPaging)
1978 val |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT
1979 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT
1980 | VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT;
1981
1982 /* allowed zero */
1983 if ((val & pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.disallowed0) != pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.disallowed0)
1984 Log(("Invalid VMX_VMCS_CTRL_PROC_EXEC_CONTROLS: zero\n"));
1985
1986 /* allowed one */
1987 if ((val & ~pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1) != 0)
1988 Log(("Invalid VMX_VMCS_CTRL_PROC_EXEC_CONTROLS: one\n"));
1989
1990 rc = VMXReadVMCS(VMX_VMCS_CTRL_ENTRY_CONTROLS, &val);
1991 AssertRC(rc);
1992 Log2(("VMX_VMCS_CTRL_ENTRY_CONTROLS = %08x\n", val));
1993
1994 /* allowed zero */
1995 if ((val & pVM->hwaccm.s.vmx.msr.vmx_entry.n.disallowed0) != pVM->hwaccm.s.vmx.msr.vmx_entry.n.disallowed0)
1996 Log(("Invalid VMX_VMCS_CTRL_ENTRY_CONTROLS: zero\n"));
1997
1998 /* allowed one */
1999 if ((val & ~pVM->hwaccm.s.vmx.msr.vmx_entry.n.allowed1) != 0)
2000 Log(("Invalid VMX_VMCS_CTRL_ENTRY_CONTROLS: one\n"));
2001
2002 rc = VMXReadVMCS(VMX_VMCS_CTRL_EXIT_CONTROLS, &val);
2003 AssertRC(rc);
2004 Log2(("VMX_VMCS_CTRL_EXIT_CONTROLS = %08x\n", val));
2005
2006 /* allowed zero */
2007 if ((val & pVM->hwaccm.s.vmx.msr.vmx_exit.n.disallowed0) != pVM->hwaccm.s.vmx.msr.vmx_exit.n.disallowed0)
2008 Log(("Invalid VMX_VMCS_CTRL_EXIT_CONTROLS: zero\n"));
2009
2010 /* allowed one */
2011 if ((val & ~pVM->hwaccm.s.vmx.msr.vmx_exit.n.allowed1) != 0)
2012 Log(("Invalid VMX_VMCS_CTRL_EXIT_CONTROLS: one\n"));
2013 }
2014#endif
2015
2016#ifdef VBOX_WITH_CRASHDUMP_MAGIC
2017 pVCpu->hwaccm.s.vmx.VMCSCache.u64TimeEntry = RTTimeNanoTS();
2018#endif
2019
2020 /* We can jump to this point to resume execution after determining that a VM-exit is innocent.
2021 */
2022ResumeExecution:
2023 STAM_STATS({
2024 if (fStatExit2Started) { STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2, y); fStatExit2Started = false; }
2025 if (!fStatEntryStarted) { STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatEntry, x); fStatEntryStarted = true; }
2026 });
2027 AssertMsg(pVCpu->hwaccm.s.idEnteredCpu == RTMpCpuId(),
2028 ("Expected %d, I'm %d; cResume=%d exitReason=%RGv exitQualification=%RGv\n",
2029 (int)pVCpu->hwaccm.s.idEnteredCpu, (int)RTMpCpuId(), cResume, exitReason, exitQualification));
2030 Assert(!HWACCMR0SuspendPending());
2031
2032 /* Safety precaution; looping for too long here can have a very bad effect on the host */
2033 if (++cResume > HWACCM_MAX_RESUME_LOOPS)
2034 {
2035 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitMaxResume);
2036 rc = VINF_EM_RAW_INTERRUPT;
2037 goto end;
2038 }
2039
2040 /* Check for irq inhibition due to instruction fusing (sti, mov ss). */
2041 if (VM_FF_ISSET(pVM, VM_FF_INHIBIT_INTERRUPTS))
2042 {
2043 Log(("VM_FF_INHIBIT_INTERRUPTS at %RGv successor %RGv\n", (RTGCPTR)pCtx->rip, EMGetInhibitInterruptsPC(pVM)));
2044 if (pCtx->rip != EMGetInhibitInterruptsPC(pVM))
2045 {
2046 /* Note: we intentionally don't clear VM_FF_INHIBIT_INTERRUPTS here.
2047 * Before we are able to execute this instruction in raw mode (iret to guest code) an external interrupt might
2048 * force a world switch again. Possibly allowing a guest interrupt to be dispatched in the process. This could
2049 * break the guest. Sounds very unlikely, but such timing sensitive problems are not as rare as you might think.
2050 */
2051 VM_FF_CLEAR(pVM, VM_FF_INHIBIT_INTERRUPTS);
2052 /* Irq inhibition is no longer active; clear the corresponding VMX state. */
2053 rc = VMXWriteVMCS(VMX_VMCS32_GUEST_INTERRUPTIBILITY_STATE, 0);
2054 AssertRC(rc);
2055 }
2056 }
2057 else
2058 {
2059 /* Irq inhibition is no longer active; clear the corresponding VMX state. */
2060 rc = VMXWriteVMCS(VMX_VMCS32_GUEST_INTERRUPTIBILITY_STATE, 0);
2061 AssertRC(rc);
2062 }
2063
2064 /* Check for pending actions that force us to go back to ring 3. */
2065 if (VM_FF_ISPENDING(pVM, VM_FF_TO_R3 | VM_FF_TIMER))
2066 {
2067 VM_FF_CLEAR(pVM, VM_FF_TO_R3);
2068 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatSwitchToR3);
2069 rc = VINF_EM_RAW_TO_R3;
2070 goto end;
2071 }
2072 /* Pending request packets might contain actions that need immediate attention, such as pending hardware interrupts. */
2073 if (VM_FF_ISPENDING(pVM, VM_FF_REQUEST))
2074 {
2075 rc = VINF_EM_PENDING_REQUEST;
2076 goto end;
2077 }
2078
2079 /* When external interrupts are pending, we should exit the VM when IF is set. */
2080 /* Note! *After* VM_FF_INHIBIT_INTERRUPTS check!!! */
2081 rc = VMXR0CheckPendingInterrupt(pVM, pVCpu, pCtx);
2082 if (RT_FAILURE(rc))
2083 goto end;
2084
2085 /** @todo check timers?? */
2086
2087 /* TPR caching using CR8 is only available in 64 bits mode */
2088 /* Note the 32 bits exception for AMD (X86_CPUID_AMD_FEATURE_ECX_CR8L), but that appears missing in Intel CPUs */
2089 /* Note: we can't do this in LoadGuestState as PDMApicGetTPR can jump back to ring 3 (lock)!!!!! */
2090 /**
2091 * @todo reduce overhead
2092 */
2093 if ( (pCtx->msrEFER & MSR_K6_EFER_LMA)
2094 && pVM->hwaccm.s.vmx.pAPIC)
2095 {
2096 /* TPR caching in CR8 */
2097 uint8_t u8TPR;
2098 bool fPending;
2099
2100 int rc = PDMApicGetTPR(pVM, &u8TPR, &fPending);
2101 AssertRC(rc);
2102 /* The TPR can be found at offset 0x80 in the APIC mmio page. */
2103 pVM->hwaccm.s.vmx.pAPIC[0x80] = u8TPR << 4; /* bits 7-4 contain the task priority */
2104
2105 /* Two options here:
2106 * - external interrupt pending, but masked by the TPR value.
2107 * -> a CR8 update that lower the current TPR value should cause an exit
2108 * - no pending interrupts
2109 * -> We don't need to be explicitely notified. There are enough world switches for detecting pending interrupts.
2110 */
2111 rc = VMXWriteVMCS(VMX_VMCS_CTRL_TPR_THRESHOLD, (fPending) ? u8TPR : 0);
2112 AssertRC(rc);
2113
2114 /* Always sync back the TPR; we should optimize this though */ /** @todo optimize TPR sync. */
2115 fSyncTPR = true;
2116 }
2117
2118#if defined(HWACCM_VTX_WITH_EPT) && defined(LOG_ENABLED)
2119 if ( pVM->hwaccm.s.fNestedPaging
2120# ifdef HWACCM_VTX_WITH_VPID
2121 || pVM->hwaccm.s.vmx.fVPID
2122# endif /* HWACCM_VTX_WITH_VPID */
2123 )
2124 {
2125 pCpu = HWACCMR0GetCurrentCpu();
2126 if ( pVCpu->hwaccm.s.idLastCpu != pCpu->idCpu
2127 || pVCpu->hwaccm.s.cTLBFlushes != pCpu->cTLBFlushes)
2128 {
2129 if (pVCpu->hwaccm.s.idLastCpu != pCpu->idCpu)
2130 Log(("Force TLB flush due to rescheduling to a different cpu (%d vs %d)\n", pVCpu->hwaccm.s.idLastCpu, pCpu->idCpu));
2131 else
2132 Log(("Force TLB flush due to changed TLB flush count (%x vs %x)\n", pVCpu->hwaccm.s.cTLBFlushes, pCpu->cTLBFlushes));
2133 }
2134 if (pCpu->fFlushTLB)
2135 Log(("Force TLB flush: first time cpu %d is used -> flush\n", pCpu->idCpu));
2136 else
2137 if (pVCpu->hwaccm.s.fForceTLBFlush)
2138 LogFlow(("Manual TLB flush\n"));
2139 }
2140#endif
2141#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2142 PGMDynMapFlushAutoSet(pVCpu);
2143#endif
2144
2145 /*
2146 * NOTE: DO NOT DO ANYTHING AFTER THIS POINT THAT MIGHT JUMP BACK TO RING 3!
2147 * (until the actual world switch)
2148 */
2149#ifdef VBOX_STRICT
2150 idCpuCheck = RTMpCpuId();
2151#endif
2152#ifdef LOG_LOGGING
2153 VMMR0LogFlushDisable(pVCpu);
2154#endif
2155 /* Save the host state first. */
2156 rc = VMXR0SaveHostState(pVM, pVCpu);
2157 if (rc != VINF_SUCCESS)
2158 goto end;
2159 /* Load the guest state */
2160 rc = VMXR0LoadGuestState(pVM, pVCpu, pCtx);
2161 if (rc != VINF_SUCCESS)
2162 goto end;
2163
2164 /* Deal with tagged TLB setup and invalidation. */
2165 pVM->hwaccm.s.vmx.pfnSetupTaggedTLB(pVM, pVCpu);
2166
2167 /* Non-register state Guest Context */
2168 /** @todo change me according to cpu state */
2169 rc = VMXWriteVMCS(VMX_VMCS32_GUEST_ACTIVITY_STATE, VMX_CMS_GUEST_ACTIVITY_ACTIVE);
2170 AssertRC(rc);
2171
2172 STAM_STATS({ STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatEntry, x); fStatEntryStarted = false; });
2173
2174 /* Manual save and restore:
2175 * - General purpose registers except RIP, RSP
2176 *
2177 * Trashed:
2178 * - CR2 (we don't care)
2179 * - LDTR (reset to 0)
2180 * - DRx (presumably not changed at all)
2181 * - DR7 (reset to 0x400)
2182 * - EFLAGS (reset to RT_BIT(1); not relevant)
2183 *
2184 */
2185
2186
2187 /* All done! Let's start VM execution. */
2188 STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatInGC, z);
2189#ifdef VBOX_STRICT
2190 Assert(idCpuCheck == RTMpCpuId());
2191#endif
2192
2193#ifdef VBOX_WITH_CRASHDUMP_MAGIC
2194 pVCpu->hwaccm.s.vmx.VMCSCache.cResume = cResume;
2195 pVCpu->hwaccm.s.vmx.VMCSCache.u64TimeSwitch = RTTimeNanoTS();
2196#endif
2197
2198 TMNotifyStartOfExecution(pVM);
2199 rc = pVCpu->hwaccm.s.vmx.pfnStartVM(pVCpu->hwaccm.s.fResumeVM, pCtx, &pVCpu->hwaccm.s.vmx.VMCSCache, pVM, pVCpu);
2200 TMNotifyEndOfExecution(pVM);
2201
2202 AssertMsg(!pVCpu->hwaccm.s.vmx.VMCSCache.Write.cValidEntries, ("pVCpu->hwaccm.s.vmx.VMCSCache.Write.cValidEntries=%d\n", pVCpu->hwaccm.s.vmx.VMCSCache.Write.cValidEntries));
2203
2204 /* In case we execute a goto ResumeExecution later on. */
2205 pVCpu->hwaccm.s.fResumeVM = true;
2206 pVCpu->hwaccm.s.fForceTLBFlush = false;
2207
2208 /*
2209 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2210 * 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
2211 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2212 */
2213 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatInGC, z);
2214 STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatExit1, v);
2215
2216 if (rc != VINF_SUCCESS)
2217 {
2218 VMXR0ReportWorldSwitchError(pVM, pVCpu, rc, pCtx);
2219 goto end;
2220 }
2221
2222 /* Success. Query the guest state and figure out what has happened. */
2223
2224 /* Investigate why there was a VM-exit. */
2225 rc = VMXReadCachedVMCS(VMX_VMCS32_RO_EXIT_REASON, &exitReason);
2226 STAM_COUNTER_INC(&pVCpu->hwaccm.s.paStatExitReasonR0[exitReason & MASK_EXITREASON_STAT]);
2227
2228 exitReason &= 0xffff; /* bit 0-15 contain the exit code. */
2229 rc |= VMXReadCachedVMCS(VMX_VMCS32_RO_VM_INSTR_ERROR, &instrError);
2230 rc |= VMXReadCachedVMCS(VMX_VMCS32_RO_EXIT_INSTR_LENGTH, &cbInstr);
2231 rc |= VMXReadCachedVMCS(VMX_VMCS32_RO_EXIT_INTERRUPTION_INFO, &intInfo);
2232 /* might not be valid; depends on VMX_EXIT_INTERRUPTION_INFO_ERROR_CODE_IS_VALID. */
2233 rc |= VMXReadCachedVMCS(VMX_VMCS32_RO_EXIT_INTERRUPTION_ERRCODE, &errCode);
2234 rc |= VMXReadCachedVMCS(VMX_VMCS32_RO_EXIT_INSTR_INFO, &instrInfo);
2235 rc |= VMXReadCachedVMCS(VMX_VMCS_RO_EXIT_QUALIFICATION, &exitQualification);
2236 AssertRC(rc);
2237
2238 /* Sync back the guest state */
2239 rc = VMXR0SaveGuestState(pVM, pVCpu, pCtx);
2240 AssertRC(rc);
2241
2242 /* Note! NOW IT'S SAFE FOR LOGGING! */
2243#ifdef LOG_LOGGING
2244 VMMR0LogFlushEnable(pVCpu);
2245#endif
2246 Log2(("Raw exit reason %08x\n", exitReason));
2247
2248 /* Check if an injected event was interrupted prematurely. */
2249 rc = VMXReadCachedVMCS(VMX_VMCS32_RO_IDT_INFO, &val);
2250 AssertRC(rc);
2251 pVCpu->hwaccm.s.Event.intInfo = VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(val);
2252 if ( VMX_EXIT_INTERRUPTION_INFO_VALID(pVCpu->hwaccm.s.Event.intInfo)
2253 /* Ignore 'int xx' as they'll be restarted anyway. */
2254 && VMX_EXIT_INTERRUPTION_INFO_TYPE(pVCpu->hwaccm.s.Event.intInfo) != VMX_EXIT_INTERRUPTION_INFO_TYPE_SW
2255 /* Ignore software exceptions (such as int3) as they're reoccur when we restart the instruction anyway. */
2256 && VMX_EXIT_INTERRUPTION_INFO_TYPE(pVCpu->hwaccm.s.Event.intInfo) != VMX_EXIT_INTERRUPTION_INFO_TYPE_SWEXCPT)
2257 {
2258 pVCpu->hwaccm.s.Event.fPending = true;
2259 /* Error code present? */
2260 if (VMX_EXIT_INTERRUPTION_INFO_ERROR_CODE_IS_VALID(pVCpu->hwaccm.s.Event.intInfo))
2261 {
2262 rc = VMXReadCachedVMCS(VMX_VMCS32_RO_IDT_ERRCODE, &val);
2263 AssertRC(rc);
2264 pVCpu->hwaccm.s.Event.errCode = val;
2265 Log(("Pending inject %RX64 at %RGv exit=%08x intInfo=%08x exitQualification=%RGv pending error=%RX64\n", pVCpu->hwaccm.s.Event.intInfo, (RTGCPTR)pCtx->rip, exitReason, intInfo, exitQualification, val));
2266 }
2267 else
2268 {
2269 Log(("Pending inject %RX64 at %RGv exit=%08x intInfo=%08x exitQualification=%RGv\n", pVCpu->hwaccm.s.Event.intInfo, (RTGCPTR)pCtx->rip, exitReason, intInfo, exitQualification));
2270 pVCpu->hwaccm.s.Event.errCode = 0;
2271 }
2272 }
2273#ifdef VBOX_STRICT
2274 else
2275 if ( VMX_EXIT_INTERRUPTION_INFO_VALID(pVCpu->hwaccm.s.Event.intInfo)
2276 /* Ignore software exceptions (such as int3) as they're reoccur when we restart the instruction anyway. */
2277 && VMX_EXIT_INTERRUPTION_INFO_TYPE(pVCpu->hwaccm.s.Event.intInfo) == VMX_EXIT_INTERRUPTION_INFO_TYPE_SWEXCPT)
2278 {
2279 Log(("Ignore pending inject %RX64 at %RGv exit=%08x intInfo=%08x exitQualification=%RGv\n", pVCpu->hwaccm.s.Event.intInfo, (RTGCPTR)pCtx->rip, exitReason, intInfo, exitQualification));
2280 }
2281
2282 if (exitReason == VMX_EXIT_ERR_INVALID_GUEST_STATE)
2283 HWACCMDumpRegs(pVM, pCtx);
2284#endif
2285
2286 Log2(("E%d: New EIP=%RGv\n", exitReason, (RTGCPTR)pCtx->rip));
2287 Log2(("Exit reason %d, exitQualification %RGv\n", (uint32_t)exitReason, exitQualification));
2288 Log2(("instrInfo=%d instrError=%d instr length=%d\n", (uint32_t)instrInfo, (uint32_t)instrError, (uint32_t)cbInstr));
2289 Log2(("Interruption error code %d\n", (uint32_t)errCode));
2290 Log2(("IntInfo = %08x\n", (uint32_t)intInfo));
2291
2292 if (fSyncTPR)
2293 {
2294 rc = PDMApicSetTPR(pVM, pVM->hwaccm.s.vmx.pAPIC[0x80] >> 4);
2295 AssertRC(rc);
2296 }
2297
2298 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit1, v);
2299 STAM_STATS({ STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatExit2, y); fStatExit2Started = true; });
2300
2301 /* Some cases don't need a complete resync of the guest CPU state; handle them here. */
2302 switch (exitReason)
2303 {
2304 case VMX_EXIT_EXCEPTION: /* 0 Exception or non-maskable interrupt (NMI). */
2305 case VMX_EXIT_EXTERNAL_IRQ: /* 1 External interrupt. */
2306 {
2307 uint32_t vector = VMX_EXIT_INTERRUPTION_INFO_VECTOR(intInfo);
2308
2309 if (!VMX_EXIT_INTERRUPTION_INFO_VALID(intInfo))
2310 {
2311 Assert(exitReason == VMX_EXIT_EXTERNAL_IRQ);
2312 /* External interrupt; leave to allow it to be dispatched again. */
2313 rc = VINF_EM_RAW_INTERRUPT;
2314 break;
2315 }
2316 STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2317 switch (VMX_EXIT_INTERRUPTION_INFO_TYPE(intInfo))
2318 {
2319 case VMX_EXIT_INTERRUPTION_INFO_TYPE_NMI: /* Non-maskable interrupt. */
2320 /* External interrupt; leave to allow it to be dispatched again. */
2321 rc = VINF_EM_RAW_INTERRUPT;
2322 break;
2323
2324 case VMX_EXIT_INTERRUPTION_INFO_TYPE_EXT: /* External hardware interrupt. */
2325 AssertFailed(); /* can't come here; fails the first check. */
2326 break;
2327
2328 case VMX_EXIT_INTERRUPTION_INFO_TYPE_DBEXCPT: /* Unknown why we get this type for #DB */
2329 case VMX_EXIT_INTERRUPTION_INFO_TYPE_SWEXCPT: /* Software exception. (#BP or #OF) */
2330 Assert(vector == 1 || vector == 3 || vector == 4);
2331 /* no break */
2332 case VMX_EXIT_INTERRUPTION_INFO_TYPE_HWEXCPT: /* Hardware exception. */
2333 Log2(("Hardware/software interrupt %d\n", vector));
2334 switch (vector)
2335 {
2336 case X86_XCPT_NM:
2337 {
2338 Log(("#NM fault at %RGv error code %x\n", (RTGCPTR)pCtx->rip, errCode));
2339
2340 /** @todo don't intercept #NM exceptions anymore when we've activated the guest FPU state. */
2341 /* If we sync the FPU/XMM state on-demand, then we can continue execution as if nothing has happened. */
2342 rc = CPUMR0LoadGuestFPU(pVM, pVCpu, pCtx);
2343 if (rc == VINF_SUCCESS)
2344 {
2345 Assert(CPUMIsGuestFPUStateActive(pVCpu));
2346
2347 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitShadowNM);
2348
2349 /* Continue execution. */
2350 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
2351
2352 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2353 goto ResumeExecution;
2354 }
2355
2356 Log(("Forward #NM fault to the guest\n"));
2357 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestNM);
2358 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, 0);
2359 AssertRC(rc);
2360 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2361 goto ResumeExecution;
2362 }
2363
2364 case X86_XCPT_PF: /* Page fault */
2365 {
2366#ifdef DEBUG
2367 if (pVM->hwaccm.s.fNestedPaging)
2368 { /* A genuine pagefault.
2369 * Forward the trap to the guest by injecting the exception and resuming execution.
2370 */
2371 Log(("Guest page fault at %RGv cr2=%RGv error code %x rsp=%RGv\n", (RTGCPTR)pCtx->rip, exitQualification, errCode, (RTGCPTR)pCtx->rsp));
2372
2373 Assert(CPUMIsGuestInPagedProtectedModeEx(pCtx));
2374
2375 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestPF);
2376
2377 /* Now we must update CR2. */
2378 pCtx->cr2 = exitQualification;
2379 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
2380 AssertRC(rc);
2381
2382 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2383 goto ResumeExecution;
2384 }
2385#endif
2386 Assert(!pVM->hwaccm.s.fNestedPaging);
2387
2388 Log2(("Page fault at %RGv error code %x\n", exitQualification, errCode));
2389 /* Exit qualification contains the linear address of the page fault. */
2390 TRPMAssertTrap(pVM, X86_XCPT_PF, TRPM_TRAP);
2391 TRPMSetErrorCode(pVM, errCode);
2392 TRPMSetFaultAddress(pVM, exitQualification);
2393
2394 /* Forward it to our trap handler first, in case our shadow pages are out of sync. */
2395 rc = PGMTrap0eHandler(pVM, errCode, CPUMCTX2CORE(pCtx), (RTGCPTR)exitQualification);
2396 Log2(("PGMTrap0eHandler %RGv returned %Rrc\n", (RTGCPTR)pCtx->rip, rc));
2397 if (rc == VINF_SUCCESS)
2398 { /* We've successfully synced our shadow pages, so let's just continue execution. */
2399 Log2(("Shadow page fault at %RGv cr2=%RGv error code %x\n", (RTGCPTR)pCtx->rip, exitQualification ,errCode));
2400 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitShadowPF);
2401
2402 TRPMResetTrap(pVM);
2403
2404 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2405 goto ResumeExecution;
2406 }
2407 else
2408 if (rc == VINF_EM_RAW_GUEST_TRAP)
2409 { /* A genuine pagefault.
2410 * Forward the trap to the guest by injecting the exception and resuming execution.
2411 */
2412 Log2(("Forward page fault to the guest\n"));
2413
2414 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestPF);
2415 /* The error code might have been changed. */
2416 errCode = TRPMGetErrorCode(pVM);
2417
2418 TRPMResetTrap(pVM);
2419
2420 /* Now we must update CR2. */
2421 pCtx->cr2 = exitQualification;
2422 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
2423 AssertRC(rc);
2424
2425 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2426 goto ResumeExecution;
2427 }
2428#ifdef VBOX_STRICT
2429 if (rc != VINF_EM_RAW_EMULATE_INSTR)
2430 Log2(("PGMTrap0eHandler failed with %d\n", rc));
2431#endif
2432 /* Need to go back to the recompiler to emulate the instruction. */
2433 TRPMResetTrap(pVM);
2434 break;
2435 }
2436
2437 case X86_XCPT_MF: /* Floating point exception. */
2438 {
2439 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestMF);
2440 if (!(pCtx->cr0 & X86_CR0_NE))
2441 {
2442 /* old style FPU error reporting needs some extra work. */
2443 /** @todo don't fall back to the recompiler, but do it manually. */
2444 rc = VINF_EM_RAW_EMULATE_INSTR;
2445 break;
2446 }
2447 Log(("Trap %x at %04X:%RGv\n", vector, pCtx->cs, (RTGCPTR)pCtx->rip));
2448 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
2449 AssertRC(rc);
2450
2451 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2452 goto ResumeExecution;
2453 }
2454
2455 case X86_XCPT_DB: /* Debug exception. */
2456 {
2457 uint64_t uDR6;
2458
2459 /* DR6, DR7.GD and IA32_DEBUGCTL.LBR are not updated yet.
2460 *
2461 * Exit qualification bits:
2462 * 3:0 B0-B3 which breakpoint condition was met
2463 * 12:4 Reserved (0)
2464 * 13 BD - debug register access detected
2465 * 14 BS - single step execution or branch taken
2466 * 63:15 Reserved (0)
2467 */
2468 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestDB);
2469
2470 /* Note that we don't support guest and host-initiated debugging at the same time. */
2471 Assert(DBGFIsStepping(pVM) || CPUMIsGuestInRealModeEx(pCtx));
2472
2473 uDR6 = X86_DR6_INIT_VAL;
2474 uDR6 |= (exitQualification & (X86_DR6_B0|X86_DR6_B1|X86_DR6_B2|X86_DR6_B3|X86_DR6_BD|X86_DR6_BS));
2475 rc = DBGFR0Trap01Handler(pVM, CPUMCTX2CORE(pCtx), uDR6);
2476 if (rc == VINF_EM_RAW_GUEST_TRAP)
2477 {
2478 /** @todo this isn't working, but we'll never get here normally. */
2479
2480 /* Update DR6 here. */
2481 pCtx->dr[6] = uDR6;
2482
2483 /* X86_DR7_GD will be cleared if drx accesses should be trapped inside the guest. */
2484 pCtx->dr[7] &= ~X86_DR7_GD;
2485
2486 /* Paranoia. */
2487 pCtx->dr[7] &= 0xffffffff; /* upper 32 bits reserved */
2488 pCtx->dr[7] &= ~(RT_BIT(11) | RT_BIT(12) | RT_BIT(14) | RT_BIT(15)); /* must be zero */
2489 pCtx->dr[7] |= 0x400; /* must be one */
2490
2491 /* Resync DR7 */
2492 rc = VMXWriteVMCS64(VMX_VMCS64_GUEST_DR7, pCtx->dr[7]);
2493 AssertRC(rc);
2494
2495 Log(("Trap %x (debug) at %RGv exit qualification %RX64\n", vector, (RTGCPTR)pCtx->rip, exitQualification));
2496 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
2497 AssertRC(rc);
2498
2499 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2500 goto ResumeExecution;
2501 }
2502 /* Return to ring 3 to deal with the debug exit code. */
2503 break;
2504 }
2505
2506 case X86_XCPT_GP: /* General protection failure exception.*/
2507 {
2508 uint32_t cbSize;
2509
2510 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestGP);
2511#ifdef VBOX_STRICT
2512 if (!CPUMIsGuestInRealModeEx(pCtx))
2513 {
2514 Log(("Trap %x at %04X:%RGv errorCode=%x\n", vector, pCtx->cs, (RTGCPTR)pCtx->rip, errCode));
2515 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
2516 AssertRC(rc);
2517 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2518 goto ResumeExecution;
2519 }
2520#endif
2521 Assert(CPUMIsGuestInRealModeEx(pCtx));
2522
2523 LogFlow(("Real mode X86_XCPT_GP instruction emulation at %RGv\n", (RTGCPTR)pCtx->rip));
2524 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
2525 if (rc == VINF_SUCCESS)
2526 {
2527 /* EIP has been updated already. */
2528
2529 /* lidt, lgdt can end up here. In the future crx changes as well. Just reload the whole context to be done with it. */
2530 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL;
2531
2532 /* Only resume if successful. */
2533 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2534 goto ResumeExecution;
2535 }
2536 AssertMsg(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_EM_HALT, ("Unexpected rc=%Rrc\n", rc));
2537 break;
2538 }
2539
2540#ifdef VBOX_STRICT
2541 case X86_XCPT_DE: /* Divide error. */
2542 case X86_XCPT_UD: /* Unknown opcode exception. */
2543 case X86_XCPT_SS: /* Stack segment exception. */
2544 case X86_XCPT_NP: /* Segment not present exception. */
2545 {
2546 switch(vector)
2547 {
2548 case X86_XCPT_DE:
2549 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestDE);
2550 break;
2551 case X86_XCPT_UD:
2552 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestUD);
2553 break;
2554 case X86_XCPT_SS:
2555 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestSS);
2556 break;
2557 case X86_XCPT_NP:
2558 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitGuestNP);
2559 break;
2560 }
2561
2562 Log(("Trap %x at %04X:%RGv\n", vector, pCtx->cs, (RTGCPTR)pCtx->rip));
2563 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
2564 AssertRC(rc);
2565
2566 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2567 goto ResumeExecution;
2568 }
2569#endif
2570 default:
2571#ifdef HWACCM_VMX_EMULATE_REALMODE
2572 if (CPUMIsGuestInRealModeEx(pCtx))
2573 {
2574 Log(("Real Mode Trap %x at %04x:%04X error code %x\n", vector, pCtx->cs, pCtx->eip, errCode));
2575 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), cbInstr, errCode);
2576 AssertRC(rc);
2577
2578 /* Go back to ring 3 in case of a triple fault. */
2579 if ( vector == X86_XCPT_DF
2580 && rc == VINF_EM_RESET)
2581 break;
2582
2583 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2584 goto ResumeExecution;
2585 }
2586#endif
2587 AssertMsgFailed(("Unexpected vm-exit caused by exception %x\n", vector));
2588 rc = VERR_VMX_UNEXPECTED_EXCEPTION;
2589 break;
2590 } /* switch (vector) */
2591
2592 break;
2593
2594 default:
2595 rc = VERR_VMX_UNEXPECTED_INTERRUPTION_EXIT_CODE;
2596 AssertMsgFailed(("Unexpected interuption code %x\n", intInfo));
2597 break;
2598 }
2599
2600 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub3, y3);
2601 break;
2602 }
2603
2604 case VMX_EXIT_EPT_VIOLATION: /* 48 EPT violation. An attempt to access memory with a guest-physical address was disallowed by the configuration of the EPT paging structures. */
2605 {
2606 RTGCPHYS GCPhys;
2607
2608 Assert(pVM->hwaccm.s.fNestedPaging);
2609
2610 rc = VMXReadVMCS64(VMX_VMCS_EXIT_PHYS_ADDR_FULL, &GCPhys);
2611 AssertRC(rc);
2612 Assert(((exitQualification >> 7) & 3) != 2);
2613
2614 /* Determine the kind of violation. */
2615 errCode = 0;
2616 if (exitQualification & VMX_EXIT_QUALIFICATION_EPT_INSTR_FETCH)
2617 errCode |= X86_TRAP_PF_ID;
2618
2619 if (exitQualification & VMX_EXIT_QUALIFICATION_EPT_DATA_WRITE)
2620 errCode |= X86_TRAP_PF_RW;
2621
2622 /* If the page is present, then it's a page level protection fault. */
2623 if (exitQualification & VMX_EXIT_QUALIFICATION_EPT_ENTRY_PRESENT)
2624 errCode |= X86_TRAP_PF_P;
2625
2626 Log(("EPT Page fault %x at %RGp error code %x\n", (uint32_t)exitQualification, GCPhys, errCode));
2627
2628 /* GCPhys contains the guest physical address of the page fault. */
2629 TRPMAssertTrap(pVM, X86_XCPT_PF, TRPM_TRAP);
2630 TRPMSetErrorCode(pVM, errCode);
2631 TRPMSetFaultAddress(pVM, GCPhys);
2632
2633 /* Handle the pagefault trap for the nested shadow table. */
2634 rc = PGMR0Trap0eHandlerNestedPaging(pVM, PGMMODE_EPT, errCode, CPUMCTX2CORE(pCtx), GCPhys);
2635 Log2(("PGMR0Trap0eHandlerNestedPaging %RGv returned %Rrc\n", (RTGCPTR)pCtx->rip, rc));
2636 if (rc == VINF_SUCCESS)
2637 { /* We've successfully synced our shadow pages, so let's just continue execution. */
2638 Log2(("Shadow page fault at %RGv cr2=%RGp error code %x\n", (RTGCPTR)pCtx->rip, exitQualification , errCode));
2639 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitReasonNPF);
2640
2641 TRPMResetTrap(pVM);
2642
2643 goto ResumeExecution;
2644 }
2645
2646#ifdef VBOX_STRICT
2647 if (rc != VINF_EM_RAW_EMULATE_INSTR)
2648 LogFlow(("PGMTrap0eHandlerNestedPaging failed with %d\n", rc));
2649#endif
2650 /* Need to go back to the recompiler to emulate the instruction. */
2651 TRPMResetTrap(pVM);
2652 break;
2653 }
2654
2655 case VMX_EXIT_EPT_MISCONFIG:
2656 {
2657 RTGCPHYS GCPhys;
2658
2659 Assert(pVM->hwaccm.s.fNestedPaging);
2660
2661 rc = VMXReadVMCS64(VMX_VMCS_EXIT_PHYS_ADDR_FULL, &GCPhys);
2662 AssertRC(rc);
2663
2664 Log(("VMX_EXIT_EPT_MISCONFIG for %VGp\n", GCPhys));
2665 break;
2666 }
2667
2668 case VMX_EXIT_IRQ_WINDOW: /* 7 Interrupt window. */
2669 /* Clear VM-exit on IF=1 change. */
2670 LogFlow(("VMX_EXIT_IRQ_WINDOW %RGv pending=%d IF=%d\n", (RTGCPTR)pCtx->rip, VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)), pCtx->eflags.Bits.u1IF));
2671 pVCpu->hwaccm.s.vmx.proc_ctls &= ~VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT;
2672 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVCpu->hwaccm.s.vmx.proc_ctls);
2673 AssertRC(rc);
2674 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitIrqWindow);
2675 goto ResumeExecution; /* we check for pending guest interrupts there */
2676
2677 case VMX_EXIT_WBINVD: /* 54 Guest software attempted to execute WBINVD. (conditional) */
2678 case VMX_EXIT_INVD: /* 13 Guest software attempted to execute INVD. (unconditional) */
2679 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitInvd);
2680 /* Skip instruction and continue directly. */
2681 pCtx->rip += cbInstr;
2682 /* Continue execution.*/
2683 goto ResumeExecution;
2684
2685 case VMX_EXIT_CPUID: /* 10 Guest software attempted to execute CPUID. */
2686 {
2687 Log2(("VMX: Cpuid %x\n", pCtx->eax));
2688 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitCpuid);
2689 rc = EMInterpretCpuId(pVM, CPUMCTX2CORE(pCtx));
2690 if (rc == VINF_SUCCESS)
2691 {
2692 /* Update EIP and continue execution. */
2693 Assert(cbInstr == 2);
2694 pCtx->rip += cbInstr;
2695 goto ResumeExecution;
2696 }
2697 AssertMsgFailed(("EMU: cpuid failed with %Rrc\n", rc));
2698 rc = VINF_EM_RAW_EMULATE_INSTR;
2699 break;
2700 }
2701
2702 case VMX_EXIT_RDTSC: /* 16 Guest software attempted to execute RDTSC. */
2703 {
2704 Log2(("VMX: Rdtsc\n"));
2705 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitRdtsc);
2706 rc = EMInterpretRdtsc(pVM, CPUMCTX2CORE(pCtx));
2707 if (rc == VINF_SUCCESS)
2708 {
2709 /* Update EIP and continue execution. */
2710 Assert(cbInstr == 2);
2711 pCtx->rip += cbInstr;
2712 goto ResumeExecution;
2713 }
2714 AssertMsgFailed(("EMU: rdtsc failed with %Rrc\n", rc));
2715 rc = VINF_EM_RAW_EMULATE_INSTR;
2716 break;
2717 }
2718
2719 case VMX_EXIT_INVPG: /* 14 Guest software attempted to execute INVPG. */
2720 {
2721 Log2(("VMX: invlpg\n"));
2722 Assert(!pVM->hwaccm.s.fNestedPaging);
2723
2724 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitInvpg);
2725 rc = EMInterpretInvlpg(pVM, CPUMCTX2CORE(pCtx), exitQualification);
2726 if (rc == VINF_SUCCESS)
2727 {
2728 /* Update EIP and continue execution. */
2729 pCtx->rip += cbInstr;
2730 goto ResumeExecution;
2731 }
2732 AssertMsg(rc == VERR_EM_INTERPRETER, ("EMU: invlpg %RGv failed with %Rrc\n", exitQualification, rc));
2733 break;
2734 }
2735
2736 case VMX_EXIT_RDMSR: /* 31 RDMSR. Guest software attempted to execute RDMSR. */
2737 case VMX_EXIT_WRMSR: /* 32 WRMSR. Guest software attempted to execute WRMSR. */
2738 {
2739 uint32_t cbSize;
2740
2741 /* 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. */
2742 Log2(("VMX: %s\n", (exitReason == VMX_EXIT_RDMSR) ? "rdmsr" : "wrmsr"));
2743 rc = EMInterpretInstruction(pVM, CPUMCTX2CORE(pCtx), 0, &cbSize);
2744 if (rc == VINF_SUCCESS)
2745 {
2746 /* EIP has been updated already. */
2747
2748 /* Only resume if successful. */
2749 goto ResumeExecution;
2750 }
2751 AssertMsg(rc == VERR_EM_INTERPRETER, ("EMU: %s failed with %Rrc\n", (exitReason == VMX_EXIT_RDMSR) ? "rdmsr" : "wrmsr", rc));
2752 break;
2753 }
2754
2755 case VMX_EXIT_CRX_MOVE: /* 28 Control-register accesses. */
2756 {
2757 STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatExit2Sub2, y2);
2758
2759 switch (VMX_EXIT_QUALIFICATION_CRX_ACCESS(exitQualification))
2760 {
2761 case VMX_EXIT_QUALIFICATION_CRX_ACCESS_WRITE:
2762 Log2(("VMX: %RGv mov cr%d, x\n", (RTGCPTR)pCtx->rip, VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification)));
2763 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitCRxWrite[VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification)]);
2764 rc = EMInterpretCRxWrite(pVM, CPUMCTX2CORE(pCtx),
2765 VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification),
2766 VMX_EXIT_QUALIFICATION_CRX_GENREG(exitQualification));
2767
2768 switch (VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification))
2769 {
2770 case 0:
2771 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0 | HWACCM_CHANGED_GUEST_CR3;
2772 break;
2773 case 2:
2774 break;
2775 case 3:
2776 Assert(!pVM->hwaccm.s.fNestedPaging || !CPUMIsGuestInPagedProtectedModeEx(pCtx));
2777 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR3;
2778 break;
2779 case 4:
2780 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR4;
2781 break;
2782 case 8:
2783 /* CR8 contains the APIC TPR */
2784 Assert(!(pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW));
2785 break;
2786
2787 default:
2788 AssertFailed();
2789 break;
2790 }
2791 /* Check if a sync operation is pending. */
2792 if ( rc == VINF_SUCCESS /* don't bother if we are going to ring 3 anyway */
2793 && VM_FF_ISPENDING(pVM, VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL))
2794 {
2795 rc = PGMSyncCR3(pVM, CPUMGetGuestCR0(pVM), CPUMGetGuestCR3(pVM), CPUMGetGuestCR4(pVM), VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
2796 AssertRC(rc);
2797 }
2798 break;
2799
2800 case VMX_EXIT_QUALIFICATION_CRX_ACCESS_READ:
2801 Log2(("VMX: mov x, crx\n"));
2802 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitCRxRead[VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification)]);
2803
2804 Assert(!pVM->hwaccm.s.fNestedPaging || !CPUMIsGuestInPagedProtectedModeEx(pCtx) || VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification) != USE_REG_CR3);
2805
2806 /* CR8 reads only cause an exit when the TPR shadow feature isn't present. */
2807 Assert(VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification) != 8 || !(pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW));
2808
2809 rc = EMInterpretCRxRead(pVM, CPUMCTX2CORE(pCtx),
2810 VMX_EXIT_QUALIFICATION_CRX_GENREG(exitQualification),
2811 VMX_EXIT_QUALIFICATION_CRX_REGISTER(exitQualification));
2812 break;
2813
2814 case VMX_EXIT_QUALIFICATION_CRX_ACCESS_CLTS:
2815 Log2(("VMX: clts\n"));
2816 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitCLTS);
2817 rc = EMInterpretCLTS(pVM);
2818 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
2819 break;
2820
2821 case VMX_EXIT_QUALIFICATION_CRX_ACCESS_LMSW:
2822 Log2(("VMX: lmsw %x\n", VMX_EXIT_QUALIFICATION_CRX_LMSW_DATA(exitQualification)));
2823 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitLMSW);
2824 rc = EMInterpretLMSW(pVM, CPUMCTX2CORE(pCtx), VMX_EXIT_QUALIFICATION_CRX_LMSW_DATA(exitQualification));
2825 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
2826 break;
2827 }
2828
2829 /* Update EIP if no error occurred. */
2830 if (RT_SUCCESS(rc))
2831 pCtx->rip += cbInstr;
2832
2833 if (rc == VINF_SUCCESS)
2834 {
2835 /* Only resume if successful. */
2836 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub2, y2);
2837 goto ResumeExecution;
2838 }
2839 Assert(rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3);
2840 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub2, y2);
2841 break;
2842 }
2843
2844 case VMX_EXIT_DRX_MOVE: /* 29 Debug-register accesses. */
2845 {
2846 if (!DBGFIsStepping(pVM))
2847 {
2848 /* Disable drx move intercepts. */
2849 pVCpu->hwaccm.s.vmx.proc_ctls &= ~VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT;
2850 rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVCpu->hwaccm.s.vmx.proc_ctls);
2851 AssertRC(rc);
2852
2853 /* Save the host and load the guest debug state. */
2854 rc = CPUMR0LoadGuestDebugState(pVM, pVCpu, pCtx, true /* include DR6 */);
2855 AssertRC(rc);
2856
2857#ifdef VBOX_WITH_STATISTICS
2858 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatDRxContextSwitch);
2859 if (VMX_EXIT_QUALIFICATION_DRX_DIRECTION(exitQualification) == VMX_EXIT_QUALIFICATION_DRX_DIRECTION_WRITE)
2860 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitDRxWrite);
2861 else
2862 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitDRxRead);
2863#endif
2864
2865 goto ResumeExecution;
2866 }
2867
2868 /** @todo clear VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT after the first time and restore drx registers afterwards */
2869 if (VMX_EXIT_QUALIFICATION_DRX_DIRECTION(exitQualification) == VMX_EXIT_QUALIFICATION_DRX_DIRECTION_WRITE)
2870 {
2871 Log2(("VMX: mov drx%d, genreg%d\n", VMX_EXIT_QUALIFICATION_DRX_REGISTER(exitQualification), VMX_EXIT_QUALIFICATION_DRX_GENREG(exitQualification)));
2872 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitDRxWrite);
2873 rc = EMInterpretDRxWrite(pVM, CPUMCTX2CORE(pCtx),
2874 VMX_EXIT_QUALIFICATION_DRX_REGISTER(exitQualification),
2875 VMX_EXIT_QUALIFICATION_DRX_GENREG(exitQualification));
2876 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_DEBUG;
2877 Log2(("DR7=%08x\n", pCtx->dr[7]));
2878 }
2879 else
2880 {
2881 Log2(("VMX: mov x, drx\n"));
2882 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitDRxRead);
2883 rc = EMInterpretDRxRead(pVM, CPUMCTX2CORE(pCtx),
2884 VMX_EXIT_QUALIFICATION_DRX_GENREG(exitQualification),
2885 VMX_EXIT_QUALIFICATION_DRX_REGISTER(exitQualification));
2886 }
2887 /* Update EIP if no error occurred. */
2888 if (RT_SUCCESS(rc))
2889 pCtx->rip += cbInstr;
2890
2891 if (rc == VINF_SUCCESS)
2892 {
2893 /* Only resume if successful. */
2894 goto ResumeExecution;
2895 }
2896 Assert(rc == VERR_EM_INTERPRETER);
2897 break;
2898 }
2899
2900 /* Note: We'll get a #GP if the IO instruction isn't allowed (IOPL or TSS bitmap); no need to double check. */
2901 case VMX_EXIT_PORT_IO: /* 30 I/O instruction. */
2902 {
2903 STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatExit2Sub1, y1);
2904 uint32_t uIOWidth = VMX_EXIT_QUALIFICATION_IO_WIDTH(exitQualification);
2905 uint32_t uPort;
2906 bool fIOWrite = (VMX_EXIT_QUALIFICATION_IO_DIRECTION(exitQualification) == VMX_EXIT_QUALIFICATION_IO_DIRECTION_OUT);
2907
2908 /** @todo necessary to make the distinction? */
2909 if (VMX_EXIT_QUALIFICATION_IO_ENCODING(exitQualification) == VMX_EXIT_QUALIFICATION_IO_ENCODING_DX)
2910 {
2911 uPort = pCtx->edx & 0xffff;
2912 }
2913 else
2914 uPort = VMX_EXIT_QUALIFICATION_IO_PORT(exitQualification); /* Immediate encoding. */
2915
2916 /* paranoia */
2917 if (RT_UNLIKELY(uIOWidth == 2 || uIOWidth >= 4))
2918 {
2919 rc = fIOWrite ? VINF_IOM_HC_IOPORT_WRITE : VINF_IOM_HC_IOPORT_READ;
2920 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub1, y1);
2921 break;
2922 }
2923
2924 uint32_t cbSize = g_aIOSize[uIOWidth];
2925
2926 if (VMX_EXIT_QUALIFICATION_IO_STRING(exitQualification))
2927 {
2928 /* ins/outs */
2929 DISCPUSTATE Cpu;
2930
2931 /* Disassemble manually to deal with segment prefixes. */
2932 /** @todo VMX_VMCS_EXIT_GUEST_LINEAR_ADDR contains the flat pointer operand of the instruction. */
2933 /** @todo VMX_VMCS32_RO_EXIT_INSTR_INFO also contains segment prefix info. */
2934 rc = EMInterpretDisasOne(pVM, CPUMCTX2CORE(pCtx), &Cpu, NULL);
2935 if (rc == VINF_SUCCESS)
2936 {
2937 if (fIOWrite)
2938 {
2939 Log2(("IOMInterpretOUTSEx %RGv %x size=%d\n", (RTGCPTR)pCtx->rip, uPort, cbSize));
2940 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitIOStringWrite);
2941 rc = IOMInterpretOUTSEx(pVM, CPUMCTX2CORE(pCtx), uPort, Cpu.prefix, cbSize);
2942 }
2943 else
2944 {
2945 Log2(("IOMInterpretINSEx %RGv %x size=%d\n", (RTGCPTR)pCtx->rip, uPort, cbSize));
2946 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitIOStringRead);
2947 rc = IOMInterpretINSEx(pVM, CPUMCTX2CORE(pCtx), uPort, Cpu.prefix, cbSize);
2948 }
2949 }
2950 else
2951 rc = VINF_EM_RAW_EMULATE_INSTR;
2952 }
2953 else
2954 {
2955 /* normal in/out */
2956 uint32_t uAndVal = g_aIOOpAnd[uIOWidth];
2957
2958 Assert(!VMX_EXIT_QUALIFICATION_IO_REP(exitQualification));
2959
2960 if (fIOWrite)
2961 {
2962 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitIOWrite);
2963 rc = IOMIOPortWrite(pVM, uPort, pCtx->eax & uAndVal, cbSize);
2964 }
2965 else
2966 {
2967 uint32_t u32Val = 0;
2968
2969 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatExitIORead);
2970 rc = IOMIOPortRead(pVM, uPort, &u32Val, cbSize);
2971 if (IOM_SUCCESS(rc))
2972 {
2973 /* Write back to the EAX register. */
2974 pCtx->eax = (pCtx->eax & ~uAndVal) | (u32Val & uAndVal);
2975 }
2976 }
2977 }
2978 /*
2979 * Handled the I/O return codes.
2980 * (The unhandled cases end up with rc == VINF_EM_RAW_EMULATE_INSTR.)
2981 */
2982 if (IOM_SUCCESS(rc))
2983 {
2984 /* Update EIP and continue execution. */
2985 pCtx->rip += cbInstr;
2986 if (RT_LIKELY(rc == VINF_SUCCESS))
2987 {
2988 /* If any IO breakpoints are armed, then we should check if a debug trap needs to be generated. */
2989 if (pCtx->dr[7] & X86_DR7_ENABLED_MASK)
2990 {
2991 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatDRxIOCheck);
2992 for (unsigned i=0;i<4;i++)
2993 {
2994 unsigned uBPLen = g_aIOSize[X86_DR7_GET_LEN(pCtx->dr[7], i)];
2995
2996 if ( (uPort >= pCtx->dr[i] && uPort < pCtx->dr[i] + uBPLen)
2997 && (pCtx->dr[7] & (X86_DR7_L(i) | X86_DR7_G(i)))
2998 && (pCtx->dr[7] & X86_DR7_RW(i, X86_DR7_RW_IO)) == X86_DR7_RW(i, X86_DR7_RW_IO))
2999 {
3000 uint64_t uDR6;
3001
3002 Assert(CPUMIsGuestDebugStateActive(pVM));
3003
3004 uDR6 = ASMGetDR6();
3005
3006 /* Clear all breakpoint status flags and set the one we just hit. */
3007 uDR6 &= ~(X86_DR6_B0|X86_DR6_B1|X86_DR6_B2|X86_DR6_B3);
3008 uDR6 |= (uint64_t)RT_BIT(i);
3009
3010 /* Note: AMD64 Architecture Programmer's Manual 13.1:
3011 * Bits 15:13 of the DR6 register is never cleared by the processor and must be cleared by software after
3012 * the contents have been read.
3013 */
3014 ASMSetDR6(uDR6);
3015
3016 /* X86_DR7_GD will be cleared if drx accesses should be trapped inside the guest. */
3017 pCtx->dr[7] &= ~X86_DR7_GD;
3018
3019 /* Paranoia. */
3020 pCtx->dr[7] &= 0xffffffff; /* upper 32 bits reserved */
3021 pCtx->dr[7] &= ~(RT_BIT(11) | RT_BIT(12) | RT_BIT(14) | RT_BIT(15)); /* must be zero */
3022 pCtx->dr[7] |= 0x400; /* must be one */
3023
3024 /* Resync DR7 */
3025 rc = VMXWriteVMCS64(VMX_VMCS64_GUEST_DR7, pCtx->dr[7]);
3026 AssertRC(rc);
3027
3028 /* Construct inject info. */
3029 intInfo = X86_XCPT_DB;
3030 intInfo |= (1 << VMX_EXIT_INTERRUPTION_INFO_VALID_SHIFT);
3031 intInfo |= (VMX_EXIT_INTERRUPTION_INFO_TYPE_HWEXCPT << VMX_EXIT_INTERRUPTION_INFO_TYPE_SHIFT);
3032
3033 Log(("Inject IO debug trap at %RGv\n", (RTGCPTR)pCtx->rip));
3034 rc = VMXR0InjectEvent(pVM, pVCpu, pCtx, VMX_VMCS_CTRL_ENTRY_IRQ_INFO_FROM_EXIT_INT_INFO(intInfo), 0, 0);
3035 AssertRC(rc);
3036
3037 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub1, y1);
3038 goto ResumeExecution;
3039 }
3040 }
3041 }
3042
3043 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub1, y1);
3044 goto ResumeExecution;
3045 }
3046 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub1, y1);
3047 break;
3048 }
3049
3050#ifdef VBOX_STRICT
3051 if (rc == VINF_IOM_HC_IOPORT_READ)
3052 Assert(!fIOWrite);
3053 else if (rc == VINF_IOM_HC_IOPORT_WRITE)
3054 Assert(fIOWrite);
3055 else
3056 AssertMsg(RT_FAILURE(rc) || rc == VINF_EM_RAW_EMULATE_INSTR || rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_TRPM_XCPT_DISPATCHED, ("%Rrc\n", rc));
3057#endif
3058 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2Sub1, y1);
3059 break;
3060 }
3061
3062 case VMX_EXIT_TPR: /* 43 TPR below threshold. Guest software executed MOV to CR8. */
3063 LogFlow(("VMX_EXIT_TPR\n"));
3064 /* RIP is already set to the next instruction and the TPR has been synced back. Just resume. */
3065 goto ResumeExecution;
3066
3067 case VMX_EXIT_PREEMPTION_TIMER: /* 52 VMX-preemption timer expired. The preemption timer counted down to zero. */
3068 goto ResumeExecution;
3069
3070 default:
3071 /* The rest is handled after syncing the entire CPU state. */
3072 break;
3073 }
3074
3075 /* Note: the guest state isn't entirely synced back at this stage. */
3076
3077 /* Investigate why there was a VM-exit. (part 2) */
3078 switch (exitReason)
3079 {
3080 case VMX_EXIT_EXCEPTION: /* 0 Exception or non-maskable interrupt (NMI). */
3081 case VMX_EXIT_EXTERNAL_IRQ: /* 1 External interrupt. */
3082 case VMX_EXIT_EPT_VIOLATION:
3083 case VMX_EXIT_PREEMPTION_TIMER: /* 52 VMX-preemption timer expired. The preemption timer counted down to zero. */
3084 /* Already handled above. */
3085 break;
3086
3087 case VMX_EXIT_TRIPLE_FAULT: /* 2 Triple fault. */
3088 rc = VINF_EM_RESET; /* Triple fault equals a reset. */
3089 break;
3090
3091 case VMX_EXIT_INIT_SIGNAL: /* 3 INIT signal. */
3092 case VMX_EXIT_SIPI: /* 4 Start-up IPI (SIPI). */
3093 rc = VINF_EM_RAW_INTERRUPT;
3094 AssertFailed(); /* Can't happen. Yet. */
3095 break;
3096
3097 case VMX_EXIT_IO_SMI_IRQ: /* 5 I/O system-management interrupt (SMI). */
3098 case VMX_EXIT_SMI_IRQ: /* 6 Other SMI. */
3099 rc = VINF_EM_RAW_INTERRUPT;
3100 AssertFailed(); /* Can't happen afaik. */
3101 break;
3102
3103 case VMX_EXIT_TASK_SWITCH: /* 9 Task switch. */
3104 rc = VERR_EM_INTERPRETER;
3105 break;
3106
3107 case VMX_EXIT_HLT: /* 12 Guest software attempted to execute HLT. */
3108 /** Check if external interrupts are pending; if so, don't switch back. */
3109 pCtx->rip++; /* skip hlt */
3110 if ( pCtx->eflags.Bits.u1IF
3111 && VM_FF_ISPENDING(pVM, (VM_FF_INTERRUPT_APIC|VM_FF_INTERRUPT_PIC)))
3112 goto ResumeExecution;
3113
3114 rc = VINF_EM_HALT;
3115 break;
3116
3117 case VMX_EXIT_RSM: /* 17 Guest software attempted to execute RSM in SMM. */
3118 AssertFailed(); /* can't happen. */
3119 rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
3120 break;
3121
3122 case VMX_EXIT_VMCALL: /* 18 Guest software executed VMCALL. */
3123 case VMX_EXIT_VMCLEAR: /* 19 Guest software executed VMCLEAR. */
3124 case VMX_EXIT_VMLAUNCH: /* 20 Guest software executed VMLAUNCH. */
3125 case VMX_EXIT_VMPTRLD: /* 21 Guest software executed VMPTRLD. */
3126 case VMX_EXIT_VMPTRST: /* 22 Guest software executed VMPTRST. */
3127 case VMX_EXIT_VMREAD: /* 23 Guest software executed VMREAD. */
3128 case VMX_EXIT_VMRESUME: /* 24 Guest software executed VMRESUME. */
3129 case VMX_EXIT_VMWRITE: /* 25 Guest software executed VMWRITE. */
3130 case VMX_EXIT_VMXOFF: /* 26 Guest software executed VMXOFF. */
3131 case VMX_EXIT_VMXON: /* 27 Guest software executed VMXON. */
3132 /** @todo inject #UD immediately */
3133 rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
3134 break;
3135
3136 case VMX_EXIT_CPUID: /* 10 Guest software attempted to execute CPUID. */
3137 case VMX_EXIT_RDTSC: /* 16 Guest software attempted to execute RDTSC. */
3138 case VMX_EXIT_INVPG: /* 14 Guest software attempted to execute INVPG. */
3139 case VMX_EXIT_CRX_MOVE: /* 28 Control-register accesses. */
3140 case VMX_EXIT_DRX_MOVE: /* 29 Debug-register accesses. */
3141 case VMX_EXIT_PORT_IO: /* 30 I/O instruction. */
3142 /* already handled above */
3143 AssertMsg( rc == VINF_PGM_CHANGE_MODE
3144 || rc == VINF_EM_RAW_INTERRUPT
3145 || rc == VERR_EM_INTERPRETER
3146 || rc == VINF_EM_RAW_EMULATE_INSTR
3147 || rc == VINF_PGM_SYNC_CR3
3148 || rc == VINF_IOM_HC_IOPORT_READ
3149 || rc == VINF_IOM_HC_IOPORT_WRITE
3150 || rc == VINF_EM_RAW_GUEST_TRAP
3151 || rc == VINF_TRPM_XCPT_DISPATCHED
3152 || rc == VINF_EM_RESCHEDULE_REM,
3153 ("rc = %d\n", rc));
3154 break;
3155
3156 case VMX_EXIT_TPR: /* 43 TPR below threshold. Guest software executed MOV to CR8. */
3157 case VMX_EXIT_RDMSR: /* 31 RDMSR. Guest software attempted to execute RDMSR. */
3158 case VMX_EXIT_WRMSR: /* 32 WRMSR. Guest software attempted to execute WRMSR. */
3159 /* Note: If we decide to emulate them here, then we must sync the MSRs that could have been changed (sysenter, fs/gs base)!!! */
3160 rc = VERR_EM_INTERPRETER;
3161 break;
3162
3163 case VMX_EXIT_RDPMC: /* 15 Guest software attempted to execute RDPMC. */
3164 case VMX_EXIT_MWAIT: /* 36 Guest software executed MWAIT. */
3165 case VMX_EXIT_MONITOR: /* 39 Guest software attempted to execute MONITOR. */
3166 case VMX_EXIT_PAUSE: /* 40 Guest software attempted to execute PAUSE. */
3167 rc = VINF_EM_RAW_EXCEPTION_PRIVILEGED;
3168 break;
3169
3170 case VMX_EXIT_IRQ_WINDOW: /* 7 Interrupt window. */
3171 Assert(rc == VINF_EM_RAW_INTERRUPT);
3172 break;
3173
3174 case VMX_EXIT_ERR_INVALID_GUEST_STATE: /* 33 VM-entry failure due to invalid guest state. */
3175 {
3176#ifdef VBOX_STRICT
3177 RTCCUINTREG val = 0;
3178
3179 Log(("VMX_EXIT_ERR_INVALID_GUEST_STATE\n"));
3180
3181 VMXReadVMCS(VMX_VMCS64_GUEST_RIP, &val);
3182 Log(("Old eip %RGv new %RGv\n", (RTGCPTR)pCtx->rip, (RTGCPTR)val));
3183
3184 VMXReadVMCS(VMX_VMCS64_GUEST_CR0, &val);
3185 Log(("VMX_VMCS_GUEST_CR0 %RX64\n", (uint64_t)val));
3186
3187 VMXReadVMCS(VMX_VMCS64_GUEST_CR3, &val);
3188 Log(("VMX_VMCS_GUEST_CR3 %RX64\n", (uint64_t)val));
3189
3190 VMXReadVMCS(VMX_VMCS64_GUEST_CR4, &val);
3191 Log(("VMX_VMCS_GUEST_CR4 %RX64\n", (uint64_t)val));
3192
3193 VMXReadVMCS(VMX_VMCS_GUEST_RFLAGS, &val);
3194 Log(("VMX_VMCS_GUEST_RFLAGS %08x\n", val));
3195
3196 VMX_LOG_SELREG(CS, "CS");
3197 VMX_LOG_SELREG(DS, "DS");
3198 VMX_LOG_SELREG(ES, "ES");
3199 VMX_LOG_SELREG(FS, "FS");
3200 VMX_LOG_SELREG(GS, "GS");
3201 VMX_LOG_SELREG(SS, "SS");
3202 VMX_LOG_SELREG(TR, "TR");
3203 VMX_LOG_SELREG(LDTR, "LDTR");
3204
3205 VMXReadVMCS(VMX_VMCS64_GUEST_GDTR_BASE, &val);
3206 Log(("VMX_VMCS_GUEST_GDTR_BASE %RX64\n", (uint64_t)val));
3207 VMXReadVMCS(VMX_VMCS64_GUEST_IDTR_BASE, &val);
3208 Log(("VMX_VMCS_GUEST_IDTR_BASE %RX64\n", (uint64_t)val));
3209#endif /* VBOX_STRICT */
3210 rc = VERR_VMX_INVALID_GUEST_STATE;
3211 break;
3212 }
3213
3214 case VMX_EXIT_ERR_MSR_LOAD: /* 34 VM-entry failure due to MSR loading. */
3215 case VMX_EXIT_ERR_MACHINE_CHECK: /* 41 VM-entry failure due to machine-check. */
3216 default:
3217 rc = VERR_VMX_UNEXPECTED_EXIT_CODE;
3218 AssertMsgFailed(("Unexpected exit code %d\n", exitReason)); /* Can't happen. */
3219 break;
3220
3221 }
3222end:
3223
3224 /* Signal changes for the recompiler. */
3225 CPUMSetChangedFlags(pVM, CPUM_CHANGED_SYSENTER_MSR | CPUM_CHANGED_LDTR | CPUM_CHANGED_GDTR | CPUM_CHANGED_IDTR | CPUM_CHANGED_TR | CPUM_CHANGED_HIDDEN_SEL_REGS);
3226
3227 /* If we executed vmlaunch/vmresume and an external irq was pending, then we don't have to do a full sync the next time. */
3228 if ( exitReason == VMX_EXIT_EXTERNAL_IRQ
3229 && !VMX_EXIT_INTERRUPTION_INFO_VALID(intInfo))
3230 {
3231 STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatPendingHostIrq);
3232 /* On the next entry we'll only sync the host context. */
3233 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_HOST_CONTEXT;
3234 }
3235 else
3236 {
3237 /* On the next entry we'll sync everything. */
3238 /** @todo we can do better than this */
3239 /* Not in the VINF_PGM_CHANGE_MODE though! */
3240 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL;
3241 }
3242
3243 /* translate into a less severe return code */
3244 if (rc == VERR_EM_INTERPRETER)
3245 rc = VINF_EM_RAW_EMULATE_INSTR;
3246 else
3247 /* Try to extract more information about what might have gone wrong here. */
3248 if (rc == VERR_VMX_INVALID_VMCS_PTR)
3249 {
3250 VMXGetActivateVMCS(&pVCpu->hwaccm.s.vmx.lasterror.u64VMCSPhys);
3251 pVCpu->hwaccm.s.vmx.lasterror.ulVMCSRevision = *(uint32_t *)pVCpu->hwaccm.s.vmx.pVMCS;
3252 pVCpu->hwaccm.s.vmx.lasterror.idEnteredCpu = pVCpu->hwaccm.s.idEnteredCpu;
3253 pVCpu->hwaccm.s.vmx.lasterror.idCurrentCpu = RTMpCpuId();
3254 }
3255
3256 STAM_STATS({
3257 if (fStatExit2Started) STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatExit2, y);
3258 else if (fStatEntryStarted) STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatEntry, x);
3259 });
3260 Log2(("X"));
3261 return rc;
3262}
3263
3264
3265/**
3266 * Enters the VT-x session
3267 *
3268 * @returns VBox status code.
3269 * @param pVM The VM to operate on.
3270 * @param pVCpu The VMCPU to operate on.
3271 * @param pCpu CPU info struct
3272 */
3273VMMR0DECL(int) VMXR0Enter(PVM pVM, PVMCPU pVCpu, PHWACCM_CPUINFO pCpu)
3274{
3275 Assert(pVM->hwaccm.s.vmx.fSupported);
3276
3277 unsigned cr4 = ASMGetCR4();
3278 if (!(cr4 & X86_CR4_VMXE))
3279 {
3280 AssertMsgFailed(("X86_CR4_VMXE should be set!\n"));
3281 return VERR_VMX_X86_CR4_VMXE_CLEARED;
3282 }
3283
3284 /* Activate the VM Control Structure. */
3285 int rc = VMXActivateVMCS(pVCpu->hwaccm.s.vmx.pVMCSPhys);
3286 if (RT_FAILURE(rc))
3287 return rc;
3288
3289 pVCpu->hwaccm.s.fResumeVM = false;
3290 return VINF_SUCCESS;
3291}
3292
3293
3294/**
3295 * Leaves the VT-x session
3296 *
3297 * @returns VBox status code.
3298 * @param pVM The VM to operate on.
3299 * @param pVCpu The VMCPU to operate on.
3300 * @param pCtx CPU context
3301 */
3302VMMR0DECL(int) VMXR0Leave(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
3303{
3304 Assert(pVM->hwaccm.s.vmx.fSupported);
3305
3306 /* Save the guest debug state if necessary. */
3307 if (CPUMIsGuestDebugStateActive(pVM))
3308 {
3309 CPUMR0SaveGuestDebugState(pVM, pVCpu, pCtx, true /* save DR6 */);
3310
3311 /* Enable drx move intercepts again. */
3312 pVCpu->hwaccm.s.vmx.proc_ctls |= VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT;
3313 int rc = VMXWriteVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, pVCpu->hwaccm.s.vmx.proc_ctls);
3314 AssertRC(rc);
3315
3316 /* Resync the debug registers the next time. */
3317 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_DEBUG;
3318 }
3319 else
3320 Assert(pVCpu->hwaccm.s.vmx.proc_ctls & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT);
3321
3322 /* Clear VM Control Structure. Marking it inactive, clearing implementation specific data and writing back VMCS data to memory. */
3323 int rc = VMXClearVMCS(pVCpu->hwaccm.s.vmx.pVMCSPhys);
3324 AssertRC(rc);
3325
3326 return VINF_SUCCESS;
3327}
3328
3329/**
3330 * Flush the TLB (EPT)
3331 *
3332 * @returns VBox status code.
3333 * @param pVM The VM to operate on.
3334 * @param pVCpu The VM CPU to operate on.
3335 * @param enmFlush Type of flush
3336 * @param GCPhys Physical address of the page to flush
3337 */
3338static void vmxR0FlushEPT(PVM pVM, PVMCPU pVCpu, VMX_FLUSH enmFlush, RTGCPHYS GCPhys)
3339{
3340 uint64_t descriptor[2];
3341
3342 LogFlow(("vmxR0FlushEPT %d %RGv\n", enmFlush, GCPhys));
3343 Assert(pVM->hwaccm.s.fNestedPaging);
3344 descriptor[0] = pVCpu->hwaccm.s.vmx.GCPhysEPTP;
3345 descriptor[1] = GCPhys;
3346 int rc = VMXR0InvEPT(enmFlush, &descriptor[0]);
3347 AssertRC(rc);
3348}
3349
3350#ifdef HWACCM_VTX_WITH_VPID
3351/**
3352 * Flush the TLB (EPT)
3353 *
3354 * @returns VBox status code.
3355 * @param pVM The VM to operate on.
3356 * @param pVCpu The VM CPU to operate on.
3357 * @param enmFlush Type of flush
3358 * @param GCPtr Virtual address of the page to flush
3359 */
3360static void vmxR0FlushVPID(PVM pVM, PVMCPU pVCpu, VMX_FLUSH enmFlush, RTGCPTR GCPtr)
3361{
3362#if HC_ARCH_BITS == 32
3363 /* If we get a flush in 64 bits guest mode, then force a full TLB flush. Invvpid probably takes only 32 bits addresses. (@todo) */
3364 if ( CPUMIsGuestInLongMode(pVM)
3365 && !VMX_IS_64BIT_HOST_MODE())
3366 {
3367 pVCpu->hwaccm.s.fForceTLBFlush = true;
3368 }
3369 else
3370#endif
3371 {
3372 uint64_t descriptor[2];
3373
3374 Assert(pVM->hwaccm.s.vmx.fVPID);
3375 descriptor[0] = pVCpu->hwaccm.s.uCurrentASID;
3376 descriptor[1] = GCPtr;
3377 int rc = VMXR0InvVPID(enmFlush, &descriptor[0]);
3378 AssertRC(rc);
3379 }
3380}
3381#endif /* HWACCM_VTX_WITH_VPID */
3382
3383/**
3384 * Invalidates a guest page
3385 *
3386 * @returns VBox status code.
3387 * @param pVM The VM to operate on.
3388 * @param pVCpu The VM CPU to operate on.
3389 * @param GCVirt Page to invalidate
3390 */
3391VMMR0DECL(int) VMXR0InvalidatePage(PVM pVM, PVMCPU pVCpu, RTGCPTR GCVirt)
3392{
3393 bool fFlushPending = pVCpu->hwaccm.s.fForceTLBFlush;
3394
3395 LogFlow(("VMXR0InvalidatePage %RGv\n", GCVirt));
3396
3397 /* Only relevant if we want to use VPID.
3398 * In the nested paging case we still see such calls, but
3399 * can safely ignore them. (e.g. after cr3 updates)
3400 */
3401#ifdef HWACCM_VTX_WITH_VPID
3402 /* Skip it if a TLB flush is already pending. */
3403 if ( !fFlushPending
3404 && pVM->hwaccm.s.vmx.fVPID)
3405 vmxR0FlushVPID(pVM, pVCpu, pVM->hwaccm.s.vmx.enmFlushPage, GCVirt);
3406#endif /* HWACCM_VTX_WITH_VPID */
3407
3408 return VINF_SUCCESS;
3409}
3410
3411/**
3412 * Invalidates a guest page by physical address
3413 *
3414 * NOTE: Assumes the current instruction references this physical page though a virtual address!!
3415 *
3416 * @returns VBox status code.
3417 * @param pVM The VM to operate on.
3418 * @param pVCpu The VM CPU to operate on.
3419 * @param GCPhys Page to invalidate
3420 */
3421VMMR0DECL(int) VMXR0InvalidatePhysPage(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys)
3422{
3423 bool fFlushPending = pVCpu->hwaccm.s.fForceTLBFlush;
3424
3425 Assert(pVM->hwaccm.s.fNestedPaging);
3426
3427 LogFlow(("VMXR0InvalidatePhysPage %RGp\n", GCPhys));
3428
3429 /* Skip it if a TLB flush is already pending. */
3430 if (!fFlushPending)
3431 vmxR0FlushEPT(pVM, pVCpu, pVM->hwaccm.s.vmx.enmFlushPage, GCPhys);
3432
3433 return VINF_SUCCESS;
3434}
3435
3436/**
3437 * Report world switch error and dump some useful debug info
3438 *
3439 * @param pVM The VM to operate on.
3440 * @param pVCpu The VMCPU to operate on.
3441 * @param rc Return code
3442 * @param pCtx Current CPU context (not updated)
3443 */
3444static void VMXR0ReportWorldSwitchError(PVM pVM, PVMCPU pVCpu, int rc, PCPUMCTX pCtx)
3445{
3446 switch (rc)
3447 {
3448 case VERR_VMX_INVALID_VMXON_PTR:
3449 AssertFailed();
3450 break;
3451
3452 case VERR_VMX_UNABLE_TO_START_VM:
3453 case VERR_VMX_UNABLE_TO_RESUME_VM:
3454 {
3455 int rc;
3456 RTCCUINTREG exitReason, instrError;
3457
3458 rc = VMXReadVMCS(VMX_VMCS32_RO_EXIT_REASON, &exitReason);
3459 rc |= VMXReadVMCS(VMX_VMCS32_RO_VM_INSTR_ERROR, &instrError);
3460 AssertRC(rc);
3461 if (rc == VINF_SUCCESS)
3462 {
3463 Log(("Unable to start/resume VM for reason: %x. Instruction error %x\n", (uint32_t)exitReason, (uint32_t)instrError));
3464 Log(("Current stack %08x\n", &rc));
3465
3466 pVCpu->hwaccm.s.vmx.lasterror.ulInstrError = instrError;
3467 pVCpu->hwaccm.s.vmx.lasterror.ulExitReason = exitReason;
3468
3469#ifdef VBOX_STRICT
3470 RTGDTR gdtr;
3471 PX86DESCHC pDesc;
3472 RTCCUINTREG val;
3473
3474 ASMGetGDTR(&gdtr);
3475
3476 VMXReadVMCS(VMX_VMCS64_GUEST_RIP, &val);
3477 Log(("Old eip %RGv new %RGv\n", (RTGCPTR)pCtx->rip, (RTGCPTR)val));
3478 VMXReadVMCS(VMX_VMCS_CTRL_PIN_EXEC_CONTROLS, &val);
3479 Log(("VMX_VMCS_CTRL_PIN_EXEC_CONTROLS %08x\n", val));
3480 VMXReadVMCS(VMX_VMCS_CTRL_PROC_EXEC_CONTROLS, &val);
3481 Log(("VMX_VMCS_CTRL_PROC_EXEC_CONTROLS %08x\n", val));
3482 VMXReadVMCS(VMX_VMCS_CTRL_ENTRY_CONTROLS, &val);
3483 Log(("VMX_VMCS_CTRL_ENTRY_CONTROLS %08x\n", val));
3484 VMXReadVMCS(VMX_VMCS_CTRL_EXIT_CONTROLS, &val);
3485 Log(("VMX_VMCS_CTRL_EXIT_CONTROLS %08x\n", val));
3486
3487 VMXReadVMCS(VMX_VMCS_HOST_CR0, &val);
3488 Log(("VMX_VMCS_HOST_CR0 %08x\n", val));
3489
3490 VMXReadVMCS(VMX_VMCS_HOST_CR3, &val);
3491 Log(("VMX_VMCS_HOST_CR3 %08x\n", val));
3492
3493 VMXReadVMCS(VMX_VMCS_HOST_CR4, &val);
3494 Log(("VMX_VMCS_HOST_CR4 %08x\n", val));
3495
3496 VMXReadVMCS(VMX_VMCS16_HOST_FIELD_CS, &val);
3497 Log(("VMX_VMCS_HOST_FIELD_CS %08x\n", val));
3498
3499 VMXReadVMCS(VMX_VMCS_GUEST_RFLAGS, &val);
3500 Log(("VMX_VMCS_GUEST_RFLAGS %08x\n", val));
3501
3502 if (val < gdtr.cbGdt)
3503 {
3504 pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
3505 HWACCMR0DumpDescriptor(pDesc, val, "CS: ");
3506 }
3507
3508 VMXReadVMCS(VMX_VMCS16_HOST_FIELD_DS, &val);
3509 Log(("VMX_VMCS_HOST_FIELD_DS %08x\n", val));
3510 if (val < gdtr.cbGdt)
3511 {
3512 pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
3513 HWACCMR0DumpDescriptor(pDesc, val, "DS: ");
3514 }
3515
3516 VMXReadVMCS(VMX_VMCS16_HOST_FIELD_ES, &val);
3517 Log(("VMX_VMCS_HOST_FIELD_ES %08x\n", val));
3518 if (val < gdtr.cbGdt)
3519 {
3520 pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
3521 HWACCMR0DumpDescriptor(pDesc, val, "ES: ");
3522 }
3523
3524 VMXReadVMCS(VMX_VMCS16_HOST_FIELD_FS, &val);
3525 Log(("VMX_VMCS16_HOST_FIELD_FS %08x\n", val));
3526 if (val < gdtr.cbGdt)
3527 {
3528 pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
3529 HWACCMR0DumpDescriptor(pDesc, val, "FS: ");
3530 }
3531
3532 VMXReadVMCS(VMX_VMCS16_HOST_FIELD_GS, &val);
3533 Log(("VMX_VMCS16_HOST_FIELD_GS %08x\n", val));
3534 if (val < gdtr.cbGdt)
3535 {
3536 pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
3537 HWACCMR0DumpDescriptor(pDesc, val, "GS: ");
3538 }
3539
3540 VMXReadVMCS(VMX_VMCS16_HOST_FIELD_SS, &val);
3541 Log(("VMX_VMCS16_HOST_FIELD_SS %08x\n", val));
3542 if (val < gdtr.cbGdt)
3543 {
3544 pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
3545 HWACCMR0DumpDescriptor(pDesc, val, "SS: ");
3546 }
3547
3548 VMXReadVMCS(VMX_VMCS16_HOST_FIELD_TR, &val);
3549 Log(("VMX_VMCS16_HOST_FIELD_TR %08x\n", val));
3550 if (val < gdtr.cbGdt)
3551 {
3552 pDesc = &((PX86DESCHC)gdtr.pGdt)[val >> X86_SEL_SHIFT_HC];
3553 HWACCMR0DumpDescriptor(pDesc, val, "TR: ");
3554 }
3555
3556 VMXReadVMCS(VMX_VMCS_HOST_TR_BASE, &val);
3557 Log(("VMX_VMCS_HOST_TR_BASE %RHv\n", val));
3558
3559 VMXReadVMCS(VMX_VMCS_HOST_GDTR_BASE, &val);
3560 Log(("VMX_VMCS_HOST_GDTR_BASE %RHv\n", val));
3561 VMXReadVMCS(VMX_VMCS_HOST_IDTR_BASE, &val);
3562 Log(("VMX_VMCS_HOST_IDTR_BASE %RHv\n", val));
3563
3564 VMXReadVMCS(VMX_VMCS32_HOST_SYSENTER_CS, &val);
3565 Log(("VMX_VMCS_HOST_SYSENTER_CS %08x\n", val));
3566
3567 VMXReadVMCS(VMX_VMCS_HOST_SYSENTER_EIP, &val);
3568 Log(("VMX_VMCS_HOST_SYSENTER_EIP %RHv\n", val));
3569
3570 VMXReadVMCS(VMX_VMCS_HOST_SYSENTER_ESP, &val);
3571 Log(("VMX_VMCS_HOST_SYSENTER_ESP %RHv\n", val));
3572
3573 VMXReadVMCS(VMX_VMCS_HOST_RSP, &val);
3574 Log(("VMX_VMCS_HOST_RSP %RHv\n", val));
3575 VMXReadVMCS(VMX_VMCS_HOST_RIP, &val);
3576 Log(("VMX_VMCS_HOST_RIP %RHv\n", val));
3577
3578# if HC_ARCH_BITS == 64 || defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
3579 if (VMX_IS_64BIT_HOST_MODE())
3580 {
3581 Log(("MSR_K6_EFER = %RX64\n", ASMRdMsr(MSR_K6_EFER)));
3582 Log(("MSR_K6_STAR = %RX64\n", ASMRdMsr(MSR_K6_STAR)));
3583 Log(("MSR_K8_LSTAR = %RX64\n", ASMRdMsr(MSR_K8_LSTAR)));
3584 Log(("MSR_K8_CSTAR = %RX64\n", ASMRdMsr(MSR_K8_CSTAR)));
3585 Log(("MSR_K8_SF_MASK = %RX64\n", ASMRdMsr(MSR_K8_SF_MASK)));
3586 }
3587# endif
3588#endif /* VBOX_STRICT */
3589 }
3590 break;
3591 }
3592
3593 default:
3594 /* impossible */
3595 AssertMsgFailed(("%Rrc (%#x)\n", rc, rc));
3596 break;
3597 }
3598}
3599
3600#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
3601/**
3602 * Prepares for and executes VMLAUNCH (64 bits guest mode)
3603 *
3604 * @returns VBox status code
3605 * @param fResume vmlauch/vmresume
3606 * @param pCtx Guest context
3607 * @param pCache VMCS cache
3608 * @param pVM The VM to operate on.
3609 * @param pVCpu The VMCPU to operate on.
3610 */
3611DECLASM(int) VMXR0SwitcherStartVM64(RTHCUINT fResume, PCPUMCTX pCtx, PVMCSCACHE pCache, PVM pVM, PVMCPU pVCpu)
3612{
3613 uint32_t aParam[6];
3614 PHWACCM_CPUINFO pCpu;
3615 RTHCPHYS pPageCpuPhys;
3616 int rc;
3617
3618 pCpu = HWACCMR0GetCurrentCpu();
3619 pPageCpuPhys = RTR0MemObjGetPagePhysAddr(pCpu->pMemObj, 0);
3620
3621#ifdef VBOX_WITH_CRASHDUMP_MAGIC
3622 pCache->uPos = 1;
3623 pCache->interPD = PGMGetInterPaeCR3(pVM);
3624 pCache->pSwitcher = (uint64_t)pVM->hwaccm.s.pfnHost32ToGuest64R0;
3625#endif
3626
3627#ifdef DEBUG
3628 pCache->TestIn.pPageCpuPhys = 0;
3629 pCache->TestIn.pVMCSPhys = 0;
3630 pCache->TestIn.pCache = 0;
3631 pCache->TestOut.pVMCSPhys = 0;
3632 pCache->TestOut.pCache = 0;
3633 pCache->TestOut.pCtx = 0;
3634 pCache->TestOut.eflags = 0;
3635#endif
3636
3637 aParam[0] = (uint32_t)(pPageCpuPhys); /* Param 1: VMXON physical address - Lo. */
3638 aParam[1] = (uint32_t)(pPageCpuPhys >> 32); /* Param 1: VMXON physical address - Hi. */
3639 aParam[2] = (uint32_t)(pVCpu->hwaccm.s.vmx.pVMCSPhys); /* Param 2: VMCS physical address - Lo. */
3640 aParam[3] = (uint32_t)(pVCpu->hwaccm.s.vmx.pVMCSPhys >> 32); /* Param 2: VMCS physical address - Hi. */
3641 aParam[4] = VM_RC_ADDR(pVM, &pVM->aCpus[pVCpu->idCpu].hwaccm.s.vmx.VMCSCache);
3642 aParam[5] = 0;
3643
3644#ifdef VBOX_WITH_CRASHDUMP_MAGIC
3645 pCtx->dr[4] = pVM->hwaccm.s.vmx.pScratchPhys + 16 + 8;
3646 *(uint32_t *)(pVM->hwaccm.s.vmx.pScratch + 16 + 8) = 1;
3647#endif
3648 rc = VMXR0Execute64BitsHandler(pVM, pVCpu, pCtx, pVM->hwaccm.s.pfnVMXGCStartVM64, 6, &aParam[0]);
3649
3650#ifdef VBOX_WITH_CRASHDUMP_MAGIC
3651 Assert(*(uint32_t *)(pVM->hwaccm.s.vmx.pScratch + 16 + 8) == 5);
3652 Assert(pCtx->dr[4] == 10);
3653 *(uint32_t *)(pVM->hwaccm.s.vmx.pScratch + 16 + 8) = 0xff;
3654#endif
3655
3656#ifdef DEBUG
3657 AssertMsg(pCache->TestIn.pPageCpuPhys == pPageCpuPhys, ("%RHp vs %RHp\n", pCache->TestIn.pPageCpuPhys, pPageCpuPhys));
3658 AssertMsg(pCache->TestIn.pVMCSPhys == pVCpu->hwaccm.s.vmx.pVMCSPhys, ("%RHp vs %RHp\n", pCache->TestIn.pVMCSPhys, pVCpu->hwaccm.s.vmx.pVMCSPhys));
3659 AssertMsg(pCache->TestIn.pVMCSPhys == pCache->TestOut.pVMCSPhys, ("%RHp vs %RHp\n", pCache->TestIn.pVMCSPhys, pCache->TestOut.pVMCSPhys));
3660 AssertMsg(pCache->TestIn.pCache == pCache->TestOut.pCache, ("%RGv vs %RGv\n", pCache->TestIn.pCache, pCache->TestOut.pCache));
3661 AssertMsg(pCache->TestIn.pCache == VM_RC_ADDR(pVM, &pVM->aCpus[pVCpu->idCpu].hwaccm.s.vmx.VMCSCache), ("%RGv vs %RGv\n", pCache->TestIn.pCache, VM_RC_ADDR(pVM, &pVM->aCpus[pVCpu->idCpu].hwaccm.s.vmx.VMCSCache)));
3662 AssertMsg(pCache->TestIn.pCtx == pCache->TestOut.pCtx, ("%RGv vs %RGv\n", pCache->TestIn.pCtx, pCache->TestOut.pCtx));
3663 Assert(!(pCache->TestOut.eflags & X86_EFL_IF));
3664#endif
3665 return rc;
3666}
3667
3668/**
3669 * Executes the specified handler in 64 mode
3670 *
3671 * @returns VBox status code.
3672 * @param pVM The VM to operate on.
3673 * @param pVCpu The VMCPU to operate on.
3674 * @param pCtx Guest context
3675 * @param pfnHandler RC handler
3676 * @param cbParam Number of parameters
3677 * @param paParam Array of 32 bits parameters
3678 */
3679VMMR0DECL(int) VMXR0Execute64BitsHandler(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, RTRCPTR pfnHandler, uint32_t cbParam, uint32_t *paParam)
3680{
3681 int rc, rc2;
3682 PHWACCM_CPUINFO pCpu;
3683 RTHCPHYS pPageCpuPhys;
3684
3685 /* @todo This code is not guest SMP safe (hyper context) */
3686 AssertReturn(pVM->cCPUs == 1, VERR_ACCESS_DENIED);
3687 AssertReturn(pVM->hwaccm.s.pfnHost32ToGuest64R0, VERR_INTERNAL_ERROR);
3688 Assert(pVCpu->hwaccm.s.vmx.VMCSCache.Write.cValidEntries <= RT_ELEMENTS(pVCpu->hwaccm.s.vmx.VMCSCache.Write.aField));
3689 Assert(pVCpu->hwaccm.s.vmx.VMCSCache.Read.cValidEntries <= RT_ELEMENTS(pVCpu->hwaccm.s.vmx.VMCSCache.Read.aField));
3690
3691#ifdef VBOX_STRICT
3692 for (unsigned i=0;i<pVCpu->hwaccm.s.vmx.VMCSCache.Write.cValidEntries;i++)
3693 Assert(vmxR0IsValidWriteField(pVCpu->hwaccm.s.vmx.VMCSCache.Write.aField[i]));
3694
3695 for (unsigned i=0;i<pVCpu->hwaccm.s.vmx.VMCSCache.Read.cValidEntries;i++)
3696 Assert(vmxR0IsValidReadField(pVCpu->hwaccm.s.vmx.VMCSCache.Read.aField[i]));
3697#endif
3698
3699 pCpu = HWACCMR0GetCurrentCpu();
3700 pPageCpuPhys = RTR0MemObjGetPagePhysAddr(pCpu->pMemObj, 0);
3701
3702 /* Clear VM Control Structure. Marking it inactive, clearing implementation specific data and writing back VMCS data to memory. */
3703 VMXClearVMCS(pVCpu->hwaccm.s.vmx.pVMCSPhys);
3704
3705 /* Leave VMX Root Mode. */
3706 VMXDisable();
3707
3708 ASMSetCR4(ASMGetCR4() & ~X86_CR4_VMXE);
3709
3710 CPUMSetHyperESP(pVM, VMMGetStackRC(pVM));
3711 CPUMSetHyperEIP(pVM, pfnHandler);
3712 for (int i=(int)cbParam-1;i>=0;i--)
3713 CPUMPushHyper(pVM, paParam[i]);
3714
3715 STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatWorldSwitch3264, z);
3716 /* Call switcher. */
3717 rc = pVM->hwaccm.s.pfnHost32ToGuest64R0(pVM);
3718 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatWorldSwitch3264, z);
3719
3720 /* Make sure the VMX instructions don't cause #UD faults. */
3721 ASMSetCR4(ASMGetCR4() | X86_CR4_VMXE);
3722
3723 /* Enter VMX Root Mode */
3724 rc2 = VMXEnable(pPageCpuPhys);
3725 if (RT_FAILURE(rc2))
3726 {
3727 if (pVM)
3728 VMXR0CheckError(pVM, pVCpu, rc2);
3729 ASMSetCR4(ASMGetCR4() & ~X86_CR4_VMXE);
3730 return VERR_VMX_VMXON_FAILED;
3731 }
3732
3733 rc2 = VMXActivateVMCS(pVCpu->hwaccm.s.vmx.pVMCSPhys);
3734 AssertRCReturn(rc2, rc2);
3735#ifdef RT_OS_WINDOWS
3736 Assert(ASMGetFlags() & X86_EFL_IF);
3737#else
3738 Assert(!(ASMGetFlags() & X86_EFL_IF));
3739#endif
3740 return rc;
3741}
3742
3743#endif /* HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL) */
3744
3745
3746#if HC_ARCH_BITS == 32 && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
3747/**
3748 * Executes VMWRITE
3749 *
3750 * @returns VBox status code
3751 * @param pVCpu The VMCPU to operate on.
3752 * @param idxField VMCS index
3753 * @param u64Val 16, 32 or 64 bits value
3754 */
3755VMMR0DECL(int) VMXWriteVMCS64Ex(PVMCPU pVCpu, uint32_t idxField, uint64_t u64Val)
3756{
3757 int rc;
3758
3759 switch (idxField)
3760 {
3761 case VMX_VMCS_CTRL_TSC_OFFSET_FULL:
3762 case VMX_VMCS_CTRL_IO_BITMAP_A_FULL:
3763 case VMX_VMCS_CTRL_IO_BITMAP_B_FULL:
3764 case VMX_VMCS_CTRL_MSR_BITMAP_FULL:
3765 case VMX_VMCS_CTRL_VMEXIT_MSR_STORE_FULL:
3766 case VMX_VMCS_CTRL_VMEXIT_MSR_LOAD_FULL:
3767 case VMX_VMCS_CTRL_VMENTRY_MSR_LOAD_FULL:
3768 case VMX_VMCS_CTRL_VAPIC_PAGEADDR_FULL:
3769 case VMX_VMCS_GUEST_LINK_PTR_FULL:
3770 case VMX_VMCS_GUEST_PDPTR0_FULL:
3771 case VMX_VMCS_GUEST_PDPTR1_FULL:
3772 case VMX_VMCS_GUEST_PDPTR2_FULL:
3773 case VMX_VMCS_GUEST_PDPTR3_FULL:
3774 case VMX_VMCS_GUEST_DEBUGCTL_FULL:
3775 case VMX_VMCS_GUEST_EFER_FULL:
3776 case VMX_VMCS_CTRL_EPTP_FULL:
3777 /* These fields consist of two parts, which are both writable in 32 bits mode. */
3778 rc = VMXWriteVMCS32(idxField, u64Val);
3779 rc |= VMXWriteVMCS32(idxField + 1, (uint32_t)(u64Val >> 32ULL));
3780 AssertRC(rc);
3781 return rc;
3782
3783 case VMX_VMCS64_GUEST_LDTR_BASE:
3784 case VMX_VMCS64_GUEST_TR_BASE:
3785 case VMX_VMCS64_GUEST_GDTR_BASE:
3786 case VMX_VMCS64_GUEST_IDTR_BASE:
3787 case VMX_VMCS64_GUEST_SYSENTER_EIP:
3788 case VMX_VMCS64_GUEST_SYSENTER_ESP:
3789 case VMX_VMCS64_GUEST_CR0:
3790 case VMX_VMCS64_GUEST_CR4:
3791 case VMX_VMCS64_GUEST_CR3:
3792 case VMX_VMCS64_GUEST_DR7:
3793 case VMX_VMCS64_GUEST_RIP:
3794 case VMX_VMCS64_GUEST_RSP:
3795 case VMX_VMCS64_GUEST_CS_BASE:
3796 case VMX_VMCS64_GUEST_DS_BASE:
3797 case VMX_VMCS64_GUEST_ES_BASE:
3798 case VMX_VMCS64_GUEST_FS_BASE:
3799 case VMX_VMCS64_GUEST_GS_BASE:
3800 case VMX_VMCS64_GUEST_SS_BASE:
3801 /* Queue a 64 bits value as we can't set it in 32 bits host mode. */
3802 if (u64Val >> 32ULL)
3803 rc = VMXWriteCachedVMCSEx(pVCpu, idxField, u64Val);
3804 else
3805 rc = VMXWriteVMCS32(idxField, (uint32_t)u64Val);
3806
3807 return rc;
3808
3809 default:
3810 AssertMsgFailed(("Unexpected field %x\n", idxField));
3811 return VERR_INVALID_PARAMETER;
3812 }
3813}
3814
3815/**
3816 * Cache VMCS writes for performance reasons (Darwin) and for running 64 bits guests on 32 bits hosts.
3817 *
3818 * @param pVCpu The VMCPU to operate on.
3819 * @param idxField VMCS field
3820 * @param u64Val Value
3821 */
3822VMMR0DECL(int) VMXWriteCachedVMCSEx(PVMCPU pVCpu, uint32_t idxField, uint64_t u64Val)
3823{
3824 PVMCSCACHE pCache = &pVCpu->hwaccm.s.vmx.VMCSCache;
3825
3826 AssertMsgReturn(pCache->Write.cValidEntries < VMCSCACHE_MAX_ENTRY - 1, ("entries=%x\n", pCache->Write.cValidEntries), VERR_ACCESS_DENIED);
3827
3828 /* Make sure there are no duplicates. */
3829 for (unsigned i=0;i<pCache->Write.cValidEntries;i++)
3830 {
3831 if (pCache->Write.aField[i] == idxField)
3832 {
3833 pCache->Write.aFieldVal[i] = u64Val;
3834 return VINF_SUCCESS;
3835 }
3836 }
3837
3838 pCache->Write.aField[pCache->Write.cValidEntries] = idxField;
3839 pCache->Write.aFieldVal[pCache->Write.cValidEntries] = u64Val;
3840 pCache->Write.cValidEntries++;
3841 return VINF_SUCCESS;
3842}
3843
3844#endif /* HC_ARCH_BITS == 32 && !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
3845
3846#ifdef VBOX_STRICT
3847static bool vmxR0IsValidReadField(uint32_t idxField)
3848{
3849 switch(idxField)
3850 {
3851 case VMX_VMCS64_GUEST_RIP:
3852 case VMX_VMCS64_GUEST_RSP:
3853 case VMX_VMCS_GUEST_RFLAGS:
3854 case VMX_VMCS32_GUEST_INTERRUPTIBILITY_STATE:
3855 case VMX_VMCS_CTRL_CR0_READ_SHADOW:
3856 case VMX_VMCS64_GUEST_CR0:
3857 case VMX_VMCS_CTRL_CR4_READ_SHADOW:
3858 case VMX_VMCS64_GUEST_CR4:
3859 case VMX_VMCS64_GUEST_DR7:
3860 case VMX_VMCS32_GUEST_SYSENTER_CS:
3861 case VMX_VMCS64_GUEST_SYSENTER_EIP:
3862 case VMX_VMCS64_GUEST_SYSENTER_ESP:
3863 case VMX_VMCS32_GUEST_GDTR_LIMIT:
3864 case VMX_VMCS64_GUEST_GDTR_BASE:
3865 case VMX_VMCS32_GUEST_IDTR_LIMIT:
3866 case VMX_VMCS64_GUEST_IDTR_BASE:
3867 case VMX_VMCS16_GUEST_FIELD_CS:
3868 case VMX_VMCS32_GUEST_CS_LIMIT:
3869 case VMX_VMCS64_GUEST_CS_BASE:
3870 case VMX_VMCS32_GUEST_CS_ACCESS_RIGHTS:
3871 case VMX_VMCS16_GUEST_FIELD_DS:
3872 case VMX_VMCS32_GUEST_DS_LIMIT:
3873 case VMX_VMCS64_GUEST_DS_BASE:
3874 case VMX_VMCS32_GUEST_DS_ACCESS_RIGHTS:
3875 case VMX_VMCS16_GUEST_FIELD_ES:
3876 case VMX_VMCS32_GUEST_ES_LIMIT:
3877 case VMX_VMCS64_GUEST_ES_BASE:
3878 case VMX_VMCS32_GUEST_ES_ACCESS_RIGHTS:
3879 case VMX_VMCS16_GUEST_FIELD_FS:
3880 case VMX_VMCS32_GUEST_FS_LIMIT:
3881 case VMX_VMCS64_GUEST_FS_BASE:
3882 case VMX_VMCS32_GUEST_FS_ACCESS_RIGHTS:
3883 case VMX_VMCS16_GUEST_FIELD_GS:
3884 case VMX_VMCS32_GUEST_GS_LIMIT:
3885 case VMX_VMCS64_GUEST_GS_BASE:
3886 case VMX_VMCS32_GUEST_GS_ACCESS_RIGHTS:
3887 case VMX_VMCS16_GUEST_FIELD_SS:
3888 case VMX_VMCS32_GUEST_SS_LIMIT:
3889 case VMX_VMCS64_GUEST_SS_BASE:
3890 case VMX_VMCS32_GUEST_SS_ACCESS_RIGHTS:
3891 case VMX_VMCS16_GUEST_FIELD_LDTR:
3892 case VMX_VMCS32_GUEST_LDTR_LIMIT:
3893 case VMX_VMCS64_GUEST_LDTR_BASE:
3894 case VMX_VMCS32_GUEST_LDTR_ACCESS_RIGHTS:
3895 case VMX_VMCS16_GUEST_FIELD_TR:
3896 case VMX_VMCS32_GUEST_TR_LIMIT:
3897 case VMX_VMCS64_GUEST_TR_BASE:
3898 case VMX_VMCS32_GUEST_TR_ACCESS_RIGHTS:
3899 case VMX_VMCS32_RO_EXIT_REASON:
3900 case VMX_VMCS32_RO_VM_INSTR_ERROR:
3901 case VMX_VMCS32_RO_EXIT_INSTR_LENGTH:
3902 case VMX_VMCS32_RO_EXIT_INTERRUPTION_ERRCODE:
3903 case VMX_VMCS32_RO_EXIT_INTERRUPTION_INFO:
3904 case VMX_VMCS32_RO_EXIT_INSTR_INFO:
3905 case VMX_VMCS_RO_EXIT_QUALIFICATION:
3906 case VMX_VMCS32_RO_IDT_INFO:
3907 case VMX_VMCS32_RO_IDT_ERRCODE:
3908 case VMX_VMCS64_GUEST_CR3:
3909 case VMX_VMCS_EXIT_PHYS_ADDR_FULL:
3910 return true;
3911 }
3912 return false;
3913}
3914
3915static bool vmxR0IsValidWriteField(uint32_t idxField)
3916{
3917 switch(idxField)
3918 {
3919 case VMX_VMCS64_GUEST_LDTR_BASE:
3920 case VMX_VMCS64_GUEST_TR_BASE:
3921 case VMX_VMCS64_GUEST_GDTR_BASE:
3922 case VMX_VMCS64_GUEST_IDTR_BASE:
3923 case VMX_VMCS64_GUEST_SYSENTER_EIP:
3924 case VMX_VMCS64_GUEST_SYSENTER_ESP:
3925 case VMX_VMCS64_GUEST_CR0:
3926 case VMX_VMCS64_GUEST_CR4:
3927 case VMX_VMCS64_GUEST_CR3:
3928 case VMX_VMCS64_GUEST_DR7:
3929 case VMX_VMCS64_GUEST_RIP:
3930 case VMX_VMCS64_GUEST_RSP:
3931 case VMX_VMCS64_GUEST_CS_BASE:
3932 case VMX_VMCS64_GUEST_DS_BASE:
3933 case VMX_VMCS64_GUEST_ES_BASE:
3934 case VMX_VMCS64_GUEST_FS_BASE:
3935 case VMX_VMCS64_GUEST_GS_BASE:
3936 case VMX_VMCS64_GUEST_SS_BASE:
3937 return true;
3938 }
3939 return false;
3940}
3941
3942#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette