VirtualBox

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

Last change on this file since 2297 was 2297, checked in by vboxsync, 18 years ago

svn:eol-style native

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