VirtualBox

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

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

Safety precaution

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