VirtualBox

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

Last change on this file since 23080 was 23058, checked in by vboxsync, 15 years ago

Noisy logging

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

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