VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/HWACCMR0.cpp@ 26112

最後變更 在這個檔案從26112是 26103,由 vboxsync 提交於 15 年 前

Do not enable NXE if the host hasn't activated it (32 bits VT-x only)

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 65.5 KB
 
1/* $Id: HWACCMR0.cpp 26103 2010-01-29 15:14:22Z vboxsync $ */
2/** @file
3 * HWACCM - 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.alldomusa.eu.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/hwacc_vmx.h>
32#include <VBox/hwacc_svm.h>
33#include <VBox/pgm.h>
34#include <VBox/pdm.h>
35#include <VBox/err.h>
36#include <VBox/log.h>
37#include <VBox/selm.h>
38#include <VBox/iom.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/cpuset.h>
42#include <iprt/memobj.h>
43#include <iprt/param.h>
44#include <iprt/power.h>
45#include <iprt/string.h>
46#include <iprt/thread.h>
47#include "HWVMXR0.h"
48#include "HWSVMR0.h"
49
50/*******************************************************************************
51* Internal Functions *
52*******************************************************************************/
53static DECLCALLBACK(void) hwaccmR0EnableCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2);
54static DECLCALLBACK(void) hwaccmR0DisableCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2);
55static DECLCALLBACK(void) HWACCMR0InitCPU(RTCPUID idCpu, void *pvUser1, void *pvUser2);
56static int hwaccmR0CheckCpuRcArray(int *paRc, unsigned cErrorCodes, RTCPUID *pidCpu);
57static DECLCALLBACK(void) hwaccmR0PowerCallback(RTPOWEREVENT enmEvent, void *pvUser);
58
59/*******************************************************************************
60* Global Variables *
61*******************************************************************************/
62
63static struct
64{
65 HWACCM_CPUINFO aCpuInfo[RTCPUSET_MAX_CPUS];
66
67 /** Ring 0 handlers for VT-x and AMD-V. */
68 DECLR0CALLBACKMEMBER(int, pfnEnterSession,(PVM pVM, PVMCPU pVCpu, PHWACCM_CPUINFO pCpu));
69 DECLR0CALLBACKMEMBER(int, pfnLeaveSession,(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx));
70 DECLR0CALLBACKMEMBER(int, pfnSaveHostState,(PVM pVM, PVMCPU pVCpu));
71 DECLR0CALLBACKMEMBER(int, pfnLoadGuestState,(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx));
72 DECLR0CALLBACKMEMBER(int, pfnRunGuestCode,(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx));
73 DECLR0CALLBACKMEMBER(int, pfnEnableCpu, (PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys));
74 DECLR0CALLBACKMEMBER(int, pfnDisableCpu, (PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys));
75 DECLR0CALLBACKMEMBER(int, pfnInitVM, (PVM pVM));
76 DECLR0CALLBACKMEMBER(int, pfnTermVM, (PVM pVM));
77 DECLR0CALLBACKMEMBER(int, pfnSetupVM, (PVM pVM));
78
79 /** Maximum ASID allowed. */
80 uint32_t uMaxASID;
81
82 struct
83 {
84 /** Set by the ring-0 driver to indicate VMX is supported by the CPU. */
85 bool fSupported;
86 /** Whether we're using SUPR0EnableVTx or not. */
87 bool fUsingSUPR0EnableVTx;
88
89 /** Host CR4 value (set by ring-0 VMX init) */
90 uint64_t hostCR4;
91
92 /** Host EFER value (set by ring-0 VMX init) */
93 uint64_t hostEFER;
94
95 /** VMX MSR values */
96 struct
97 {
98 uint64_t feature_ctrl;
99 uint64_t vmx_basic_info;
100 VMX_CAPABILITY vmx_pin_ctls;
101 VMX_CAPABILITY vmx_proc_ctls;
102 VMX_CAPABILITY vmx_proc_ctls2;
103 VMX_CAPABILITY vmx_exit;
104 VMX_CAPABILITY vmx_entry;
105 uint64_t vmx_misc;
106 uint64_t vmx_cr0_fixed0;
107 uint64_t vmx_cr0_fixed1;
108 uint64_t vmx_cr4_fixed0;
109 uint64_t vmx_cr4_fixed1;
110 uint64_t vmx_vmcs_enum;
111 uint64_t vmx_eptcaps;
112 } msr;
113 /* Last instruction error */
114 uint32_t ulLastInstrError;
115 } vmx;
116 struct
117 {
118 /* HWCR msr (for diagnostics) */
119 uint64_t msrHWCR;
120
121 /** SVM revision. */
122 uint32_t u32Rev;
123
124 /** SVM feature bits from cpuid 0x8000000a */
125 uint32_t u32Features;
126
127 /** Set by the ring-0 driver to indicate SVM is supported by the CPU. */
128 bool fSupported;
129 } svm;
130 /** Saved error from detection */
131 int32_t lLastError;
132
133 struct
134 {
135 uint32_t u32AMDFeatureECX;
136 uint32_t u32AMDFeatureEDX;
137 } cpuid;
138
139 HWACCMSTATE enmHwAccmState;
140
141 bool fGlobalInit;
142 volatile bool fSuspended;
143} HWACCMR0Globals;
144
145
146
147/**
148 * Does global Ring-0 HWACCM initialization.
149 *
150 * @returns VBox status code.
151 */
152VMMR0DECL(int) HWACCMR0Init(void)
153{
154 int rc;
155 bool fAMDVPresent = false;
156
157 memset(&HWACCMR0Globals, 0, sizeof(HWACCMR0Globals));
158 HWACCMR0Globals.enmHwAccmState = HWACCMSTATE_UNINITIALIZED;
159 for (unsigned i = 0; i < RT_ELEMENTS(HWACCMR0Globals.aCpuInfo); i++)
160 HWACCMR0Globals.aCpuInfo[i].pMemObj = NIL_RTR0MEMOBJ;
161
162 /* Fill in all callbacks with placeholders. */
163 HWACCMR0Globals.pfnEnterSession = HWACCMR0DummyEnter;
164 HWACCMR0Globals.pfnLeaveSession = HWACCMR0DummyLeave;
165 HWACCMR0Globals.pfnSaveHostState = HWACCMR0DummySaveHostState;
166 HWACCMR0Globals.pfnLoadGuestState = HWACCMR0DummyLoadGuestState;
167 HWACCMR0Globals.pfnRunGuestCode = HWACCMR0DummyRunGuestCode;
168 HWACCMR0Globals.pfnEnableCpu = HWACCMR0DummyEnableCpu;
169 HWACCMR0Globals.pfnDisableCpu = HWACCMR0DummyDisableCpu;
170 HWACCMR0Globals.pfnInitVM = HWACCMR0DummyInitVM;
171 HWACCMR0Globals.pfnTermVM = HWACCMR0DummyTermVM;
172 HWACCMR0Globals.pfnSetupVM = HWACCMR0DummySetupVM;
173
174 /* Default is global VT-x/AMD-V init */
175 HWACCMR0Globals.fGlobalInit = true;
176
177 /*
178 * Check for VT-x and AMD-V capabilities
179 */
180 if (ASMHasCpuId())
181 {
182 uint32_t u32FeaturesECX;
183 uint32_t u32Dummy;
184 uint32_t u32FeaturesEDX;
185 uint32_t u32VendorEBX, u32VendorECX, u32VendorEDX;
186
187 ASMCpuId(0, &u32Dummy, &u32VendorEBX, &u32VendorECX, &u32VendorEDX);
188 ASMCpuId(1, &u32Dummy, &u32Dummy, &u32FeaturesECX, &u32FeaturesEDX);
189 /* Query AMD features. */
190 ASMCpuId(0x80000001, &u32Dummy, &u32Dummy, &HWACCMR0Globals.cpuid.u32AMDFeatureECX, &HWACCMR0Globals.cpuid.u32AMDFeatureEDX);
191
192 if ( u32VendorEBX == X86_CPUID_VENDOR_INTEL_EBX
193 && u32VendorECX == X86_CPUID_VENDOR_INTEL_ECX
194 && u32VendorEDX == X86_CPUID_VENDOR_INTEL_EDX
195 )
196 {
197 /*
198 * Read all VMX MSRs if VMX is available. (same goes for RDMSR/WRMSR)
199 * We also assume all VMX-enabled CPUs support fxsave/fxrstor.
200 */
201 if ( (u32FeaturesECX & X86_CPUID_FEATURE_ECX_VMX)
202 && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_MSR)
203 && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_FXSR)
204 )
205 {
206 int aRc[RTCPUSET_MAX_CPUS];
207 RTCPUID idCpu = 0;
208
209 HWACCMR0Globals.vmx.msr.feature_ctrl = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
210
211 /*
212 * First try use native kernel API for controlling VT-x.
213 * (This is only supported by some Mac OS X kernels atm.)
214 */
215 HWACCMR0Globals.lLastError = rc = SUPR0EnableVTx(true /* fEnable */);
216 if (rc != VERR_NOT_SUPPORTED)
217 {
218 AssertMsg(rc == VINF_SUCCESS || rc == VERR_VMX_IN_VMX_ROOT_MODE || rc == VERR_VMX_NO_VMX, ("%Rrc\n", rc));
219 HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx = true;
220 if (RT_SUCCESS(rc))
221 {
222 HWACCMR0Globals.vmx.fSupported = true;
223 rc = SUPR0EnableVTx(false /* fEnable */);
224 AssertRC(rc);
225 }
226 }
227 else
228 {
229 HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx = false;
230
231 /* We need to check if VT-x has been properly initialized on all CPUs. Some BIOSes do a lousy job. */
232 memset(aRc, 0, sizeof(aRc));
233 HWACCMR0Globals.lLastError = RTMpOnAll(HWACCMR0InitCPU, (void *)u32VendorEBX, aRc);
234
235 /* Check the return code of all invocations. */
236 if (RT_SUCCESS(HWACCMR0Globals.lLastError))
237 HWACCMR0Globals.lLastError = hwaccmR0CheckCpuRcArray(aRc, RT_ELEMENTS(aRc), &idCpu);
238 }
239 if (RT_SUCCESS(HWACCMR0Globals.lLastError))
240 {
241 /* Reread in case we've changed it. */
242 HWACCMR0Globals.vmx.msr.feature_ctrl = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
243
244 if ( (HWACCMR0Globals.vmx.msr.feature_ctrl & (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK))
245 == (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK))
246 {
247 RTR0MEMOBJ pScatchMemObj;
248 void *pvScatchPage;
249 RTHCPHYS pScatchPagePhys;
250
251 HWACCMR0Globals.vmx.msr.vmx_basic_info = ASMRdMsr(MSR_IA32_VMX_BASIC_INFO);
252 HWACCMR0Globals.vmx.msr.vmx_pin_ctls.u = ASMRdMsr(MSR_IA32_VMX_PINBASED_CTLS);
253 HWACCMR0Globals.vmx.msr.vmx_proc_ctls.u = ASMRdMsr(MSR_IA32_VMX_PROCBASED_CTLS);
254 HWACCMR0Globals.vmx.msr.vmx_exit.u = ASMRdMsr(MSR_IA32_VMX_EXIT_CTLS);
255 HWACCMR0Globals.vmx.msr.vmx_entry.u = ASMRdMsr(MSR_IA32_VMX_ENTRY_CTLS);
256 HWACCMR0Globals.vmx.msr.vmx_misc = ASMRdMsr(MSR_IA32_VMX_MISC);
257 HWACCMR0Globals.vmx.msr.vmx_cr0_fixed0 = ASMRdMsr(MSR_IA32_VMX_CR0_FIXED0);
258 HWACCMR0Globals.vmx.msr.vmx_cr0_fixed1 = ASMRdMsr(MSR_IA32_VMX_CR0_FIXED1);
259 HWACCMR0Globals.vmx.msr.vmx_cr4_fixed0 = ASMRdMsr(MSR_IA32_VMX_CR4_FIXED0);
260 HWACCMR0Globals.vmx.msr.vmx_cr4_fixed1 = ASMRdMsr(MSR_IA32_VMX_CR4_FIXED1);
261 HWACCMR0Globals.vmx.msr.vmx_vmcs_enum = ASMRdMsr(MSR_IA32_VMX_VMCS_ENUM);
262 /* VPID 16 bits ASID. */
263 HWACCMR0Globals.uMaxASID = 0x10000; /* exclusive */
264
265 if (HWACCMR0Globals.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
266 {
267 HWACCMR0Globals.vmx.msr.vmx_proc_ctls2.u = ASMRdMsr(MSR_IA32_VMX_PROCBASED_CTLS2);
268 if (HWACCMR0Globals.vmx.msr.vmx_proc_ctls2.n.allowed1 & (VMX_VMCS_CTRL_PROC_EXEC2_EPT|VMX_VMCS_CTRL_PROC_EXEC2_VPID))
269 HWACCMR0Globals.vmx.msr.vmx_eptcaps = ASMRdMsr(MSR_IA32_VMX_EPT_CAPS);
270 }
271
272 if (!HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx)
273 {
274 HWACCMR0Globals.vmx.hostCR4 = ASMGetCR4();
275 HWACCMR0Globals.vmx.hostEFER = ASMRdMsr(MSR_K6_EFER);
276
277 rc = RTR0MemObjAllocCont(&pScatchMemObj, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
278 if (RT_FAILURE(rc))
279 return rc;
280
281 pvScatchPage = RTR0MemObjAddress(pScatchMemObj);
282 pScatchPagePhys = RTR0MemObjGetPagePhysAddr(pScatchMemObj, 0);
283 memset(pvScatchPage, 0, PAGE_SIZE);
284
285 /* Set revision dword at the beginning of the structure. */
286 *(uint32_t *)pvScatchPage = MSR_IA32_VMX_BASIC_INFO_VMCS_ID(HWACCMR0Globals.vmx.msr.vmx_basic_info);
287
288 /* Make sure we don't get rescheduled to another cpu during this probe. */
289 RTCCUINTREG fFlags = ASMIntDisableFlags();
290
291 /*
292 * Check CR4.VMXE
293 */
294 if (!(HWACCMR0Globals.vmx.hostCR4 & X86_CR4_VMXE))
295 {
296 /* In theory this bit could be cleared behind our back. Which would cause #UD faults when we
297 * try to execute the VMX instructions...
298 */
299 ASMSetCR4(HWACCMR0Globals.vmx.hostCR4 | X86_CR4_VMXE);
300 }
301
302 /* Enter VMX Root Mode */
303 rc = VMXEnable(pScatchPagePhys);
304 if (RT_FAILURE(rc))
305 {
306 /* KVM leaves the CPU in VMX root mode. Not only is this not allowed, it will crash the host when we enter raw mode, because
307 * (a) clearing X86_CR4_VMXE in CR4 causes a #GP (we no longer modify this bit)
308 * (b) turning off paging causes a #GP (unavoidable when switching from long to 32 bits mode or 32 bits to PAE)
309 *
310 * They should fix their code, but until they do we simply refuse to run.
311 */
312 HWACCMR0Globals.lLastError = VERR_VMX_IN_VMX_ROOT_MODE;
313 }
314 else
315 {
316 HWACCMR0Globals.vmx.fSupported = true;
317 VMXDisable();
318 }
319
320 /* Restore CR4 again; don't leave the X86_CR4_VMXE flag set if it wasn't so before (some software could incorrectly think it's in VMX mode) */
321 ASMSetCR4(HWACCMR0Globals.vmx.hostCR4);
322 ASMSetFlags(fFlags);
323
324 RTR0MemObjFree(pScatchMemObj, false);
325 if (RT_FAILURE(HWACCMR0Globals.lLastError))
326 return HWACCMR0Globals.lLastError;
327 }
328 }
329 else
330 {
331 AssertFailed(); /* can't hit this case anymore */
332 HWACCMR0Globals.lLastError = VERR_VMX_ILLEGAL_FEATURE_CONTROL_MSR;
333 }
334 }
335#ifdef LOG_ENABLED
336 else
337 SUPR0Printf("HWACCMR0InitCPU failed with rc=%d\n", HWACCMR0Globals.lLastError);
338#endif
339 }
340 else
341 HWACCMR0Globals.lLastError = VERR_VMX_NO_VMX;
342 }
343 else
344 if ( u32VendorEBX == X86_CPUID_VENDOR_AMD_EBX
345 && u32VendorECX == X86_CPUID_VENDOR_AMD_ECX
346 && u32VendorEDX == X86_CPUID_VENDOR_AMD_EDX
347 )
348 {
349 /*
350 * Read all SVM MSRs if SVM is available. (same goes for RDMSR/WRMSR)
351 * We also assume all SVM-enabled CPUs support fxsave/fxrstor.
352 */
353 if ( (HWACCMR0Globals.cpuid.u32AMDFeatureECX & X86_CPUID_AMD_FEATURE_ECX_SVM)
354 && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_MSR)
355 && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_FXSR)
356 )
357 {
358 int aRc[RTCPUSET_MAX_CPUS];
359 RTCPUID idCpu = 0;
360
361 fAMDVPresent = true;
362
363 /* Query AMD features. */
364 ASMCpuId(0x8000000A, &HWACCMR0Globals.svm.u32Rev, &HWACCMR0Globals.uMaxASID, &u32Dummy, &HWACCMR0Globals.svm.u32Features);
365
366 /* We need to check if AMD-V has been properly initialized on all CPUs. Some BIOSes might do a poor job. */
367 memset(aRc, 0, sizeof(aRc));
368 rc = RTMpOnAll(HWACCMR0InitCPU, (void *)u32VendorEBX, aRc);
369 AssertRC(rc);
370
371 /* Check the return code of all invocations. */
372 if (RT_SUCCESS(rc))
373 rc = hwaccmR0CheckCpuRcArray(aRc, RT_ELEMENTS(aRc), &idCpu);
374
375#ifndef DEBUG_bird
376 AssertMsg(rc == VINF_SUCCESS || rc == VERR_SVM_IN_USE, ("HWACCMR0InitCPU failed for cpu %d with rc=%d\n", idCpu, rc));
377#endif
378 if (RT_SUCCESS(rc))
379 {
380 /* Read the HWCR msr for diagnostics. */
381 HWACCMR0Globals.svm.msrHWCR = ASMRdMsr(MSR_K8_HWCR);
382 HWACCMR0Globals.svm.fSupported = true;
383 }
384 else
385 HWACCMR0Globals.lLastError = rc;
386 }
387 else
388 HWACCMR0Globals.lLastError = VERR_SVM_NO_SVM;
389 }
390 else
391 HWACCMR0Globals.lLastError = VERR_HWACCM_UNKNOWN_CPU;
392 }
393 else
394 HWACCMR0Globals.lLastError = VERR_HWACCM_NO_CPUID;
395
396 if (HWACCMR0Globals.vmx.fSupported)
397 {
398 HWACCMR0Globals.pfnEnterSession = VMXR0Enter;
399 HWACCMR0Globals.pfnLeaveSession = VMXR0Leave;
400 HWACCMR0Globals.pfnSaveHostState = VMXR0SaveHostState;
401 HWACCMR0Globals.pfnLoadGuestState = VMXR0LoadGuestState;
402 HWACCMR0Globals.pfnRunGuestCode = VMXR0RunGuestCode;
403 HWACCMR0Globals.pfnEnableCpu = VMXR0EnableCpu;
404 HWACCMR0Globals.pfnDisableCpu = VMXR0DisableCpu;
405 HWACCMR0Globals.pfnInitVM = VMXR0InitVM;
406 HWACCMR0Globals.pfnTermVM = VMXR0TermVM;
407 HWACCMR0Globals.pfnSetupVM = VMXR0SetupVM;
408 }
409 else
410 if (fAMDVPresent)
411 {
412 HWACCMR0Globals.pfnEnterSession = SVMR0Enter;
413 HWACCMR0Globals.pfnLeaveSession = SVMR0Leave;
414 HWACCMR0Globals.pfnSaveHostState = SVMR0SaveHostState;
415 HWACCMR0Globals.pfnLoadGuestState = SVMR0LoadGuestState;
416 HWACCMR0Globals.pfnRunGuestCode = SVMR0RunGuestCode;
417 HWACCMR0Globals.pfnEnableCpu = SVMR0EnableCpu;
418 HWACCMR0Globals.pfnDisableCpu = SVMR0DisableCpu;
419 HWACCMR0Globals.pfnInitVM = SVMR0InitVM;
420 HWACCMR0Globals.pfnTermVM = SVMR0TermVM;
421 HWACCMR0Globals.pfnSetupVM = SVMR0SetupVM;
422 }
423
424 if (!HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx)
425 {
426 rc = RTPowerNotificationRegister(hwaccmR0PowerCallback, 0);
427 AssertRC(rc);
428 }
429
430 return VINF_SUCCESS;
431}
432
433
434/**
435 * Checks the error code array filled in for each cpu in the system.
436 *
437 * @returns VBox status code.
438 * @param paRc Error code array
439 * @param cErrorCodes Array size
440 * @param pidCpu Value of the first cpu that set an error (out)
441 */
442static int hwaccmR0CheckCpuRcArray(int *paRc, unsigned cErrorCodes, RTCPUID *pidCpu)
443{
444 int rc = VINF_SUCCESS;
445
446 Assert(cErrorCodes == RTCPUSET_MAX_CPUS);
447
448 for (unsigned i=0;i<cErrorCodes;i++)
449 {
450 if (RTMpIsCpuOnline(i))
451 {
452 if (RT_FAILURE(paRc[i]))
453 {
454 rc = paRc[i];
455 *pidCpu = i;
456 break;
457 }
458 }
459 }
460 return rc;
461}
462
463/**
464 * Does global Ring-0 HWACCM termination.
465 *
466 * @returns VBox status code.
467 */
468VMMR0DECL(int) HWACCMR0Term(void)
469{
470 int rc;
471 if ( HWACCMR0Globals.vmx.fSupported
472 && HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx)
473 {
474 Assert(HWACCMR0Globals.fGlobalInit);
475 rc = SUPR0EnableVTx(false /* fEnable */);
476 for (unsigned iCpu = 0; iCpu < RT_ELEMENTS(HWACCMR0Globals.aCpuInfo); iCpu++)
477 {
478 HWACCMR0Globals.aCpuInfo[iCpu].fConfigured = false;
479 Assert(HWACCMR0Globals.aCpuInfo[iCpu].pMemObj == NIL_RTR0MEMOBJ);
480 }
481 }
482 else
483 {
484 Assert(!HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx);
485 if (!HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx)
486 {
487 rc = RTPowerNotificationDeregister(hwaccmR0PowerCallback, 0);
488 AssertRC(rc);
489 }
490 else
491 rc = VINF_SUCCESS;
492
493 /* Only disable VT-x/AMD-V on all CPUs if we enabled it before. */
494 if (HWACCMR0Globals.fGlobalInit)
495 {
496 int aRc[RTCPUSET_MAX_CPUS];
497
498 memset(aRc, 0, sizeof(aRc));
499 rc = RTMpOnAll(hwaccmR0DisableCpuCallback, aRc, NULL);
500 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
501#ifdef VBOX_STRICT
502 for (unsigned i=0;i<RT_ELEMENTS(HWACCMR0Globals.aCpuInfo);i++)
503 AssertMsgRC(aRc[i], ("hwaccmR0DisableCpuCallback failed for cpu %d with rc=%d\n", i, aRc[i]));
504#endif
505 }
506
507 /* Free the per-cpu pages used for VT-x and AMD-V */
508 for (unsigned i=0;i<RT_ELEMENTS(HWACCMR0Globals.aCpuInfo);i++)
509 {
510 if (HWACCMR0Globals.aCpuInfo[i].pMemObj != NIL_RTR0MEMOBJ)
511 {
512 RTR0MemObjFree(HWACCMR0Globals.aCpuInfo[i].pMemObj, false);
513 HWACCMR0Globals.aCpuInfo[i].pMemObj = NIL_RTR0MEMOBJ;
514 }
515 }
516 }
517 return rc;
518}
519
520
521/**
522 * Worker function passed to RTMpOnAll, RTMpOnOthers and RTMpOnSpecific that
523 * is to be called on the target cpus.
524 *
525 * @param idCpu The identifier for the CPU the function is called on.
526 * @param pvUser1 The 1st user argument.
527 * @param pvUser2 The 2nd user argument.
528 */
529static DECLCALLBACK(void) HWACCMR0InitCPU(RTCPUID idCpu, void *pvUser1, void *pvUser2)
530{
531 unsigned u32VendorEBX = (uintptr_t)pvUser1;
532 int *paRc = (int *)pvUser2;
533 uint64_t val;
534
535#if defined(LOG_ENABLED) && !defined(DEBUG_bird)
536 SUPR0Printf("HWACCMR0InitCPU cpu %d\n", idCpu);
537#endif
538 Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /// @todo fix idCpu == index assumption (rainy day)
539
540 if (u32VendorEBX == X86_CPUID_VENDOR_INTEL_EBX)
541 {
542 val = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
543
544 /*
545 * Both the LOCK and VMXON bit must be set; otherwise VMXON will generate a #GP.
546 * Once the lock bit is set, this MSR can no longer be modified.
547 */
548 if ( !(val & (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK))
549 || ((val & (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK)) == MSR_IA32_FEATURE_CONTROL_VMXON) /* Some BIOSes forget to set the locked bit. */
550 )
551 {
552 /* MSR is not yet locked; we can change it ourselves here */
553 ASMWrMsr(MSR_IA32_FEATURE_CONTROL, HWACCMR0Globals.vmx.msr.feature_ctrl | MSR_IA32_FEATURE_CONTROL_VMXON | MSR_IA32_FEATURE_CONTROL_LOCK);
554 val = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
555 }
556 if ( (val & (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK))
557 == (MSR_IA32_FEATURE_CONTROL_VMXON|MSR_IA32_FEATURE_CONTROL_LOCK))
558 paRc[idCpu] = VINF_SUCCESS;
559 else
560 paRc[idCpu] = VERR_VMX_MSR_LOCKED_OR_DISABLED;
561 }
562 else
563 if (u32VendorEBX == X86_CPUID_VENDOR_AMD_EBX)
564 {
565 /* Check if SVM is disabled */
566 val = ASMRdMsr(MSR_K8_VM_CR);
567 if (!(val & MSR_K8_VM_CR_SVM_DISABLE))
568 {
569 /* Turn on SVM in the EFER MSR. */
570 val = ASMRdMsr(MSR_K6_EFER);
571 if (val & MSR_K6_EFER_SVME)
572 {
573 paRc[idCpu] = VERR_SVM_IN_USE;
574 }
575 else
576 {
577 ASMWrMsr(MSR_K6_EFER, val | MSR_K6_EFER_SVME);
578
579 /* Paranoia. */
580 val = ASMRdMsr(MSR_K6_EFER);
581 if (val & MSR_K6_EFER_SVME)
582 {
583 /* Restore previous value. */
584 ASMWrMsr(MSR_K6_EFER, val & ~MSR_K6_EFER_SVME);
585 paRc[idCpu] = VINF_SUCCESS;
586 }
587 else
588 paRc[idCpu] = VERR_SVM_ILLEGAL_EFER_MSR;
589 }
590 }
591 else
592 paRc[idCpu] = VERR_SVM_DISABLED;
593 }
594 else
595 AssertFailed(); /* can't happen */
596 return;
597}
598
599
600/**
601 * Sets up HWACCM on all cpus.
602 *
603 * @returns VBox status code.
604 * @param pVM The VM to operate on.
605 *
606 */
607VMMR0DECL(int) HWACCMR0EnableAllCpus(PVM pVM)
608{
609 AssertCompile(sizeof(HWACCMR0Globals.enmHwAccmState) == sizeof(uint32_t));
610
611 /* Make sure we don't touch hwaccm after we've disabled hwaccm in preparation of a suspend. */
612 if (ASMAtomicReadBool(&HWACCMR0Globals.fSuspended))
613 return VERR_HWACCM_SUSPEND_PENDING;
614
615 if (ASMAtomicCmpXchgU32((volatile uint32_t *)&HWACCMR0Globals.enmHwAccmState, HWACCMSTATE_ENABLED, HWACCMSTATE_UNINITIALIZED))
616 {
617 int rc;
618
619 HWACCMR0Globals.fGlobalInit = pVM->hwaccm.s.fGlobalInit;
620
621 if ( HWACCMR0Globals.vmx.fSupported
622 && HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx)
623 {
624 rc = SUPR0EnableVTx(true /* fEnable */);
625 if (RT_SUCCESS(rc))
626 {
627 for (unsigned iCpu = 0; iCpu < RT_ELEMENTS(HWACCMR0Globals.aCpuInfo); iCpu++)
628 {
629 HWACCMR0Globals.aCpuInfo[iCpu].fConfigured = true;
630 Assert(HWACCMR0Globals.aCpuInfo[iCpu].pMemObj == NIL_RTR0MEMOBJ);
631 }
632 /* If the host provides a VT-x init API, then we'll rely on that for global init. */
633 HWACCMR0Globals.fGlobalInit = pVM->hwaccm.s.fGlobalInit = true;
634 }
635 else
636 AssertMsgFailed(("HWACCMR0EnableAllCpus/SUPR0EnableVTx: rc=%Rrc\n", rc));
637 }
638 else
639 {
640 int aRc[RTCPUSET_MAX_CPUS];
641 RTCPUID idCpu = 0;
642
643 memset(aRc, 0, sizeof(aRc));
644
645 /* Allocate one page per cpu for the global vt-x and amd-v pages */
646 for (unsigned i=0;i<RT_ELEMENTS(HWACCMR0Globals.aCpuInfo);i++)
647 {
648 Assert(!HWACCMR0Globals.aCpuInfo[i].pMemObj);
649
650 /** @todo this is rather dangerous if cpus can be taken offline; we don't care for now */
651 if (RTMpIsCpuOnline(i))
652 {
653 rc = RTR0MemObjAllocCont(&HWACCMR0Globals.aCpuInfo[i].pMemObj, 1 << PAGE_SHIFT, true /* executable R0 mapping */);
654 AssertRC(rc);
655 if (RT_FAILURE(rc))
656 return rc;
657
658 void *pvR0 = RTR0MemObjAddress(HWACCMR0Globals.aCpuInfo[i].pMemObj);
659 Assert(pvR0);
660 ASMMemZeroPage(pvR0);
661
662#if defined(LOG_ENABLED) && !defined(DEBUG_bird)
663 SUPR0Printf("address %x phys %x\n", pvR0, (uint32_t)RTR0MemObjGetPagePhysAddr(HWACCMR0Globals.aCpuInfo[i].pMemObj, 0));
664#endif
665 }
666 }
667 if (HWACCMR0Globals.fGlobalInit)
668 {
669 /* First time, so initialize each cpu/core */
670 rc = RTMpOnAll(hwaccmR0EnableCpuCallback, (void *)pVM, aRc);
671
672 /* Check the return code of all invocations. */
673 if (RT_SUCCESS(rc))
674 rc = hwaccmR0CheckCpuRcArray(aRc, RT_ELEMENTS(aRc), &idCpu);
675 AssertMsgRC(rc, ("HWACCMR0EnableAllCpus failed for cpu %d with rc=%d\n", idCpu, rc));
676 }
677 else
678 rc = VINF_SUCCESS;
679 }
680
681 return rc;
682 }
683 return VINF_SUCCESS;
684}
685
686/**
687 * Disable VT-x or AMD-V on the current CPU
688 *
689 * @returns VBox status code.
690 * @param pVM VM handle (can be 0!)
691 * @param idCpu The identifier for the CPU the function is called on.
692 */
693static int hwaccmR0EnableCpu(PVM pVM, RTCPUID idCpu)
694{
695 void *pvPageCpu;
696 RTHCPHYS pPageCpuPhys;
697 PHWACCM_CPUINFO pCpu = &HWACCMR0Globals.aCpuInfo[idCpu];
698
699 Assert(!HWACCMR0Globals.vmx.fSupported || !HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx);
700 Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /// @todo fix idCpu == index assumption (rainy day)
701 Assert(idCpu < RT_ELEMENTS(HWACCMR0Globals.aCpuInfo));
702 Assert(!pCpu->fConfigured);
703 Assert(!HWACCMR0Globals.fGlobalInit || ASMAtomicReadBool(&pCpu->fInUse) == false);
704
705 pCpu->idCpu = idCpu;
706
707 /* Make sure we start with a clean TLB. */
708 pCpu->fFlushTLB = true;
709
710 pCpu->uCurrentASID = 0; /* we'll aways increment this the first time (host uses ASID 0) */
711 pCpu->cTLBFlushes = 0;
712
713 /* Should never happen */
714 if (!pCpu->pMemObj)
715 {
716 AssertFailed();
717 return VERR_INTERNAL_ERROR;
718 }
719
720 pvPageCpu = RTR0MemObjAddress(pCpu->pMemObj);
721 pPageCpuPhys = RTR0MemObjGetPagePhysAddr(pCpu->pMemObj, 0);
722
723 int rc = HWACCMR0Globals.pfnEnableCpu(pCpu, pVM, pvPageCpu, pPageCpuPhys);
724 AssertRC(rc);
725 if (RT_SUCCESS(rc))
726 pCpu->fConfigured = true;
727
728 return rc;
729}
730
731
732/**
733 * Worker function passed to RTMpOnAll, RTMpOnOthers and RTMpOnSpecific that
734 * is to be called on the target cpus.
735 *
736 * @param idCpu The identifier for the CPU the function is called on.
737 * @param pvUser1 The 1st user argument.
738 * @param pvUser2 The 2nd user argument.
739 */
740static DECLCALLBACK(void) hwaccmR0EnableCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
741{
742 PVM pVM = (PVM)pvUser1; /* can be NULL! */
743 int *paRc = (int *)pvUser2;
744
745 if (!HWACCMR0Globals.fGlobalInit)
746 {
747 paRc[idCpu] = VINF_SUCCESS;
748 AssertFailed();
749 return;
750 }
751
752 paRc[idCpu] = hwaccmR0EnableCpu(pVM, idCpu);
753}
754
755
756/**
757 * Disable VT-x or AMD-V on the current CPU
758 *
759 * @returns VBox status code.
760 * @param idCpu The identifier for the CPU the function is called on.
761 */
762static int hwaccmR0DisableCpu(RTCPUID idCpu)
763{
764 void *pvPageCpu;
765 RTHCPHYS pPageCpuPhys;
766 int rc;
767 PHWACCM_CPUINFO pCpu = &HWACCMR0Globals.aCpuInfo[idCpu];
768
769 Assert(!HWACCMR0Globals.vmx.fSupported || !HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx);
770 Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /// @todo fix idCpu == index assumption (rainy day)
771 Assert(idCpu < RT_ELEMENTS(HWACCMR0Globals.aCpuInfo));
772 Assert(!HWACCMR0Globals.fGlobalInit || ASMAtomicReadBool(&pCpu->fInUse) == false);
773 Assert(!pCpu->fConfigured || pCpu->pMemObj);
774
775 if (!pCpu->pMemObj)
776 return (pCpu->fConfigured) ? VERR_NO_MEMORY : VINF_SUCCESS /* not initialized. */;
777
778 pvPageCpu = RTR0MemObjAddress(pCpu->pMemObj);
779 pPageCpuPhys = RTR0MemObjGetPagePhysAddr(pCpu->pMemObj, 0);
780
781 if (pCpu->fConfigured)
782 {
783 rc = HWACCMR0Globals.pfnDisableCpu(pCpu, pvPageCpu, pPageCpuPhys);
784 AssertRC(rc);
785 pCpu->fConfigured = false;
786 }
787 else
788 rc = VINF_SUCCESS; /* nothing to do */
789
790 pCpu->uCurrentASID = 0;
791 return rc;
792}
793
794/**
795 * Worker function passed to RTMpOnAll, RTMpOnOthers and RTMpOnSpecific that
796 * is to be called on the target cpus.
797 *
798 * @param idCpu The identifier for the CPU the function is called on.
799 * @param pvUser1 The 1st user argument.
800 * @param pvUser2 The 2nd user argument.
801 */
802static DECLCALLBACK(void) hwaccmR0DisableCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
803{
804 int *paRc = (int *)pvUser1;
805
806 if (!HWACCMR0Globals.fGlobalInit)
807 {
808 paRc[idCpu] = VINF_SUCCESS;
809 AssertFailed();
810 return;
811 }
812
813 paRc[idCpu] = hwaccmR0DisableCpu(idCpu);
814}
815
816/**
817 * Called whenever a system power state change occurs.
818 *
819 * @param enmEvent Power event
820 * @param pvUser User argument
821 */
822static DECLCALLBACK(void) hwaccmR0PowerCallback(RTPOWEREVENT enmEvent, void *pvUser)
823{
824 NOREF(pvUser);
825 Assert(!HWACCMR0Globals.vmx.fSupported || !HWACCMR0Globals.vmx.fUsingSUPR0EnableVTx);
826
827#ifdef LOG_ENABLED
828 if (enmEvent == RTPOWEREVENT_SUSPEND)
829 SUPR0Printf("hwaccmR0PowerCallback RTPOWEREVENT_SUSPEND\n");
830 else
831 SUPR0Printf("hwaccmR0PowerCallback RTPOWEREVENT_RESUME\n");
832#endif
833
834 if (enmEvent == RTPOWEREVENT_SUSPEND)
835 ASMAtomicWriteBool(&HWACCMR0Globals.fSuspended, true);
836
837 if (HWACCMR0Globals.enmHwAccmState == HWACCMSTATE_ENABLED)
838 {
839 int aRc[RTCPUSET_MAX_CPUS];
840 int rc;
841 RTCPUID idCpu;
842
843 memset(aRc, 0, sizeof(aRc));
844 if (enmEvent == RTPOWEREVENT_SUSPEND)
845 {
846 if (HWACCMR0Globals.fGlobalInit)
847 {
848 /* Turn off VT-x or AMD-V on all CPUs. */
849 rc = RTMpOnAll(hwaccmR0DisableCpuCallback, aRc, NULL);
850 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
851 }
852 /* else nothing to do here for the local init case */
853 }
854 else
855 {
856 /* Reinit the CPUs from scratch as the suspend state might have messed with the MSRs. (lousy BIOSes as usual) */
857 rc = RTMpOnAll(HWACCMR0InitCPU, (void *)((HWACCMR0Globals.vmx.fSupported) ? X86_CPUID_VENDOR_INTEL_EBX : X86_CPUID_VENDOR_AMD_EBX), aRc);
858 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
859
860 if (RT_SUCCESS(rc))
861 rc = hwaccmR0CheckCpuRcArray(aRc, RT_ELEMENTS(aRc), &idCpu);
862#ifdef LOG_ENABLED
863 if (RT_FAILURE(rc))
864 SUPR0Printf("hwaccmR0PowerCallback HWACCMR0InitCPU failed with %d\n", rc);
865#endif
866
867 if (HWACCMR0Globals.fGlobalInit)
868 {
869 /* Turn VT-x or AMD-V back on on all CPUs. */
870 rc = RTMpOnAll(hwaccmR0EnableCpuCallback, NULL, aRc);
871 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
872 }
873 /* else nothing to do here for the local init case */
874 }
875 }
876 if (enmEvent == RTPOWEREVENT_RESUME)
877 ASMAtomicWriteBool(&HWACCMR0Globals.fSuspended, false);
878}
879
880
881/**
882 * Does Ring-0 per VM HWACCM initialization.
883 *
884 * This is mainly to check that the Host CPU mode is compatible
885 * with VMX.
886 *
887 * @returns VBox status code.
888 * @param pVM The VM to operate on.
889 */
890VMMR0DECL(int) HWACCMR0InitVM(PVM pVM)
891{
892 int rc;
893
894 AssertReturn(pVM, VERR_INVALID_PARAMETER);
895
896#ifdef LOG_ENABLED
897 SUPR0Printf("HWACCMR0InitVM: %p\n", pVM);
898#endif
899
900 /* Make sure we don't touch hwaccm after we've disabled hwaccm in preparation of a suspend. */
901 if (ASMAtomicReadBool(&HWACCMR0Globals.fSuspended))
902 return VERR_HWACCM_SUSPEND_PENDING;
903
904 pVM->hwaccm.s.vmx.fSupported = HWACCMR0Globals.vmx.fSupported;
905 pVM->hwaccm.s.svm.fSupported = HWACCMR0Globals.svm.fSupported;
906
907 pVM->hwaccm.s.vmx.msr.feature_ctrl = HWACCMR0Globals.vmx.msr.feature_ctrl;
908 pVM->hwaccm.s.vmx.hostCR4 = HWACCMR0Globals.vmx.hostCR4;
909 pVM->hwaccm.s.vmx.hostEFER = HWACCMR0Globals.vmx.hostEFER;
910 pVM->hwaccm.s.vmx.msr.vmx_basic_info = HWACCMR0Globals.vmx.msr.vmx_basic_info;
911 pVM->hwaccm.s.vmx.msr.vmx_pin_ctls = HWACCMR0Globals.vmx.msr.vmx_pin_ctls;
912 pVM->hwaccm.s.vmx.msr.vmx_proc_ctls = HWACCMR0Globals.vmx.msr.vmx_proc_ctls;
913 pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2 = HWACCMR0Globals.vmx.msr.vmx_proc_ctls2;
914 pVM->hwaccm.s.vmx.msr.vmx_exit = HWACCMR0Globals.vmx.msr.vmx_exit;
915 pVM->hwaccm.s.vmx.msr.vmx_entry = HWACCMR0Globals.vmx.msr.vmx_entry;
916 pVM->hwaccm.s.vmx.msr.vmx_misc = HWACCMR0Globals.vmx.msr.vmx_misc;
917 pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed0 = HWACCMR0Globals.vmx.msr.vmx_cr0_fixed0;
918 pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed1 = HWACCMR0Globals.vmx.msr.vmx_cr0_fixed1;
919 pVM->hwaccm.s.vmx.msr.vmx_cr4_fixed0 = HWACCMR0Globals.vmx.msr.vmx_cr4_fixed0;
920 pVM->hwaccm.s.vmx.msr.vmx_cr4_fixed1 = HWACCMR0Globals.vmx.msr.vmx_cr4_fixed1;
921 pVM->hwaccm.s.vmx.msr.vmx_vmcs_enum = HWACCMR0Globals.vmx.msr.vmx_vmcs_enum;
922 pVM->hwaccm.s.vmx.msr.vmx_eptcaps = HWACCMR0Globals.vmx.msr.vmx_eptcaps;
923 pVM->hwaccm.s.svm.msrHWCR = HWACCMR0Globals.svm.msrHWCR;
924 pVM->hwaccm.s.svm.u32Rev = HWACCMR0Globals.svm.u32Rev;
925 pVM->hwaccm.s.svm.u32Features = HWACCMR0Globals.svm.u32Features;
926 pVM->hwaccm.s.cpuid.u32AMDFeatureECX = HWACCMR0Globals.cpuid.u32AMDFeatureECX;
927 pVM->hwaccm.s.cpuid.u32AMDFeatureEDX = HWACCMR0Globals.cpuid.u32AMDFeatureEDX;
928 pVM->hwaccm.s.lLastError = HWACCMR0Globals.lLastError;
929
930 pVM->hwaccm.s.uMaxASID = HWACCMR0Globals.uMaxASID;
931
932
933 if (!pVM->hwaccm.s.cMaxResumeLoops) /* allow ring-3 overrides */
934 {
935 pVM->hwaccm.s.cMaxResumeLoops = 1024;
936#ifdef VBOX_WITH_VMMR0_DISABLE_PREEMPTION
937 if (RTThreadPreemptIsPendingTrusty())
938 pVM->hwaccm.s.cMaxResumeLoops = 8192;
939#endif
940 }
941
942 for (VMCPUID i = 0; i < pVM->cCpus; i++)
943 {
944 PVMCPU pVCpu = &pVM->aCpus[i];
945
946 pVCpu->hwaccm.s.idEnteredCpu = NIL_RTCPUID;
947
948 /* Invalidate the last cpu we were running on. */
949 pVCpu->hwaccm.s.idLastCpu = NIL_RTCPUID;
950
951 /* we'll aways increment this the first time (host uses ASID 0) */
952 pVCpu->hwaccm.s.uCurrentASID = 0;
953 }
954
955 RTCCUINTREG fFlags = ASMIntDisableFlags();
956 PHWACCM_CPUINFO pCpu = HWACCMR0GetCurrentCpu();
957
958 /* Note: Not correct as we can be rescheduled to a different cpu, but the fInUse case is mostly for debugging. */
959 ASMAtomicWriteBool(&pCpu->fInUse, true);
960 ASMSetFlags(fFlags);
961
962 /* Init a VT-x or AMD-V VM. */
963 rc = HWACCMR0Globals.pfnInitVM(pVM);
964
965 ASMAtomicWriteBool(&pCpu->fInUse, false);
966 return rc;
967}
968
969
970/**
971 * Does Ring-0 per VM HWACCM termination.
972 *
973 * @returns VBox status code.
974 * @param pVM The VM to operate on.
975 */
976VMMR0DECL(int) HWACCMR0TermVM(PVM pVM)
977{
978 int rc;
979
980 AssertReturn(pVM, VERR_INVALID_PARAMETER);
981
982#ifdef LOG_ENABLED
983 SUPR0Printf("HWACCMR0TermVM: %p\n", pVM);
984#endif
985
986 /* Make sure we don't touch hwaccm after we've disabled hwaccm in preparation of a suspend. */
987 AssertReturn(!ASMAtomicReadBool(&HWACCMR0Globals.fSuspended), VERR_HWACCM_SUSPEND_PENDING);
988
989 /* @note Not correct as we can be rescheduled to a different cpu, but the fInUse case is mostly for debugging. */
990 RTCCUINTREG fFlags = ASMIntDisableFlags();
991 PHWACCM_CPUINFO pCpu = HWACCMR0GetCurrentCpu();
992
993 ASMAtomicWriteBool(&pCpu->fInUse, true);
994 ASMSetFlags(fFlags);
995
996 /* Terminate a VT-x or AMD-V VM. */
997 rc = HWACCMR0Globals.pfnTermVM(pVM);
998
999 ASMAtomicWriteBool(&pCpu->fInUse, false);
1000 return rc;
1001}
1002
1003
1004/**
1005 * Sets up a VT-x or AMD-V session
1006 *
1007 * @returns VBox status code.
1008 * @param pVM The VM to operate on.
1009 */
1010VMMR0DECL(int) HWACCMR0SetupVM(PVM pVM)
1011{
1012 int rc;
1013 RTCPUID idCpu = RTMpCpuId();
1014 PHWACCM_CPUINFO pCpu = &HWACCMR0Globals.aCpuInfo[idCpu];
1015
1016 AssertReturn(pVM, VERR_INVALID_PARAMETER);
1017
1018 /* Make sure we don't touch hwaccm after we've disabled hwaccm in preparation of a suspend. */
1019 AssertReturn(!ASMAtomicReadBool(&HWACCMR0Globals.fSuspended), VERR_HWACCM_SUSPEND_PENDING);
1020
1021#ifdef LOG_ENABLED
1022 SUPR0Printf("HWACCMR0SetupVM: %p\n", pVM);
1023#endif
1024
1025 ASMAtomicWriteBool(&pCpu->fInUse, true);
1026
1027 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1028 {
1029 /* On first entry we'll sync everything. */
1030 pVM->aCpus[i].hwaccm.s.fContextUseFlags = HWACCM_CHANGED_ALL;
1031 }
1032
1033 /* Enable VT-x or AMD-V if local init is required. */
1034 if (!HWACCMR0Globals.fGlobalInit)
1035 {
1036 rc = hwaccmR0EnableCpu(pVM, idCpu);
1037 AssertRCReturn(rc, rc);
1038 }
1039
1040 /* Setup VT-x or AMD-V. */
1041 rc = HWACCMR0Globals.pfnSetupVM(pVM);
1042
1043 /* Disable VT-x or AMD-V if local init was done before. */
1044 if (!HWACCMR0Globals.fGlobalInit)
1045 {
1046 rc = hwaccmR0DisableCpu(idCpu);
1047 AssertRC(rc);
1048 }
1049
1050 ASMAtomicWriteBool(&pCpu->fInUse, false);
1051
1052 return rc;
1053}
1054
1055
1056/**
1057 * Enters the VT-x or AMD-V session
1058 *
1059 * @returns VBox status code.
1060 * @param pVM The VM to operate on.
1061 * @param pVCpu VMCPUD id.
1062 */
1063VMMR0DECL(int) HWACCMR0Enter(PVM pVM, PVMCPU pVCpu)
1064{
1065 PCPUMCTX pCtx;
1066 int rc;
1067 RTCPUID idCpu = RTMpCpuId();
1068 PHWACCM_CPUINFO pCpu = &HWACCMR0Globals.aCpuInfo[idCpu];
1069
1070 /* Make sure we can't enter a session after we've disabled hwaccm in preparation of a suspend. */
1071 AssertReturn(!ASMAtomicReadBool(&HWACCMR0Globals.fSuspended), VERR_HWACCM_SUSPEND_PENDING);
1072 ASMAtomicWriteBool(&pCpu->fInUse, true);
1073
1074 AssertMsg(pVCpu->hwaccm.s.idEnteredCpu == NIL_RTCPUID, ("%d", (int)pVCpu->hwaccm.s.idEnteredCpu));
1075 pVCpu->hwaccm.s.idEnteredCpu = idCpu;
1076
1077 pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1078
1079 /* Always load the guest's FPU/XMM state on-demand. */
1080 CPUMDeactivateGuestFPUState(pVCpu);
1081
1082 /* Always load the guest's debug state on-demand. */
1083 CPUMDeactivateGuestDebugState(pVCpu);
1084
1085 /* Always reload the host context and the guest's CR0 register. (!!!!) */
1086 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0 | HWACCM_CHANGED_HOST_CONTEXT;
1087
1088 /* Setup the register and mask according to the current execution mode. */
1089 if (pCtx->msrEFER & MSR_K6_EFER_LMA)
1090 pVM->hwaccm.s.u64RegisterMask = UINT64_C(0xFFFFFFFFFFFFFFFF);
1091 else
1092 pVM->hwaccm.s.u64RegisterMask = UINT64_C(0xFFFFFFFF);
1093
1094 /* Enable VT-x or AMD-V if local init is required. */
1095 if (!HWACCMR0Globals.fGlobalInit)
1096 {
1097 rc = hwaccmR0EnableCpu(pVM, idCpu);
1098 AssertRCReturn(rc, rc);
1099 }
1100
1101 rc = HWACCMR0Globals.pfnEnterSession(pVM, pVCpu, pCpu);
1102 AssertRC(rc);
1103 /* We must save the host context here (VT-x) as we might be rescheduled on a different cpu after a long jump back to ring 3. */
1104 rc |= HWACCMR0Globals.pfnSaveHostState(pVM, pVCpu);
1105 AssertRC(rc);
1106 rc |= HWACCMR0Globals.pfnLoadGuestState(pVM, pVCpu, pCtx);
1107 AssertRC(rc);
1108
1109 /* keep track of the CPU owning the VMCS for debugging scheduling weirdness and ring-3 calls. */
1110 if (RT_SUCCESS(rc))
1111 {
1112#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1113 PGMDynMapMigrateAutoSet(pVCpu);
1114#endif
1115 }
1116 else
1117 pVCpu->hwaccm.s.idEnteredCpu = NIL_RTCPUID;
1118 return rc;
1119}
1120
1121
1122/**
1123 * Leaves the VT-x or AMD-V session
1124 *
1125 * @returns VBox status code.
1126 * @param pVM The VM to operate on.
1127 * @param pVCpu VMCPUD id.
1128 */
1129VMMR0DECL(int) HWACCMR0Leave(PVM pVM, PVMCPU pVCpu)
1130{
1131 PCPUMCTX pCtx;
1132 int rc;
1133 RTCPUID idCpu = RTMpCpuId();
1134 PHWACCM_CPUINFO pCpu = &HWACCMR0Globals.aCpuInfo[idCpu];
1135
1136 AssertReturn(!ASMAtomicReadBool(&HWACCMR0Globals.fSuspended), VERR_HWACCM_SUSPEND_PENDING);
1137
1138 pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1139
1140 /* Note: It's rather tricky with longjmps done by e.g. Log statements or the page fault handler.
1141 * We must restore the host FPU here to make absolutely sure we don't leave the guest FPU state active
1142 * or trash somebody else's FPU state.
1143 */
1144 /* Save the guest FPU and XMM state if necessary. */
1145 if (CPUMIsGuestFPUStateActive(pVCpu))
1146 {
1147 Log2(("CPUMR0SaveGuestFPU\n"));
1148 CPUMR0SaveGuestFPU(pVM, pVCpu, pCtx);
1149
1150 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_GUEST_CR0;
1151 Assert(!CPUMIsGuestFPUStateActive(pVCpu));
1152 }
1153
1154 rc = HWACCMR0Globals.pfnLeaveSession(pVM, pVCpu, pCtx);
1155
1156 /* keep track of the CPU owning the VMCS for debugging scheduling weirdness and ring-3 calls. */
1157#ifdef RT_STRICT
1158 if (RT_UNLIKELY( pVCpu->hwaccm.s.idEnteredCpu != idCpu
1159 && RT_FAILURE(rc)))
1160 {
1161 AssertMsgFailed(("Owner is %d, I'm %d", (int)pVCpu->hwaccm.s.idEnteredCpu, (int)idCpu));
1162 rc = VERR_INTERNAL_ERROR;
1163 }
1164#endif
1165 pVCpu->hwaccm.s.idEnteredCpu = NIL_RTCPUID;
1166
1167 /* Disable VT-x or AMD-V if local init was done before. */
1168 if (!HWACCMR0Globals.fGlobalInit)
1169 {
1170 rc = hwaccmR0DisableCpu(idCpu);
1171 AssertRC(rc);
1172
1173 /* Reset these to force a TLB flush for the next entry. (-> EXPENSIVE) */
1174 pVCpu->hwaccm.s.idLastCpu = NIL_RTCPUID;
1175 pVCpu->hwaccm.s.uCurrentASID = 0;
1176 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
1177 }
1178
1179 ASMAtomicWriteBool(&pCpu->fInUse, false);
1180 return rc;
1181}
1182
1183/**
1184 * Runs guest code in a hardware accelerated VM.
1185 *
1186 * @returns VBox status code.
1187 * @param pVM The VM to operate on.
1188 * @param pVCpu VMCPUD id.
1189 */
1190VMMR0DECL(int) HWACCMR0RunGuestCode(PVM pVM, PVMCPU pVCpu)
1191{
1192 CPUMCTX *pCtx;
1193 int rc;
1194#ifdef VBOX_STRICT
1195 RTCPUID idCpu = RTMpCpuId(); NOREF(idCpu);
1196 PHWACCM_CPUINFO pCpu = &HWACCMR0Globals.aCpuInfo[idCpu];
1197#endif
1198
1199 Assert(!VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL));
1200 Assert(HWACCMR0Globals.aCpuInfo[idCpu].fConfigured);
1201 AssertReturn(!ASMAtomicReadBool(&HWACCMR0Globals.fSuspended), VERR_HWACCM_SUSPEND_PENDING);
1202 Assert(ASMAtomicReadBool(&pCpu->fInUse) == true);
1203
1204#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1205 PGMDynMapStartAutoSet(pVCpu);
1206#endif
1207
1208 pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1209
1210 rc = HWACCMR0Globals.pfnRunGuestCode(pVM, pVCpu, pCtx);
1211
1212#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1213 PGMDynMapReleaseAutoSet(pVCpu);
1214#endif
1215 return rc;
1216}
1217
1218
1219#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1220/**
1221 * Save guest FPU/XMM state (64 bits guest mode & 32 bits host only)
1222 *
1223 * @returns VBox status code.
1224 * @param pVM VM handle.
1225 * @param pVCpu VMCPU handle.
1226 * @param pCtx CPU context
1227 */
1228VMMR0DECL(int) HWACCMR0SaveFPUState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1229{
1230 if (pVM->hwaccm.s.vmx.fSupported)
1231 return VMXR0Execute64BitsHandler(pVM, pVCpu, pCtx, pVM->hwaccm.s.pfnSaveGuestFPU64, 0, NULL);
1232
1233 return SVMR0Execute64BitsHandler(pVM, pVCpu, pCtx, pVM->hwaccm.s.pfnSaveGuestFPU64, 0, NULL);
1234}
1235
1236/**
1237 * Save guest debug state (64 bits guest mode & 32 bits host only)
1238 *
1239 * @returns VBox status code.
1240 * @param pVM VM handle.
1241 * @param pVCpu VMCPU handle.
1242 * @param pCtx CPU context
1243 */
1244VMMR0DECL(int) HWACCMR0SaveDebugState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1245{
1246 if (pVM->hwaccm.s.vmx.fSupported)
1247 return VMXR0Execute64BitsHandler(pVM, pVCpu, pCtx, pVM->hwaccm.s.pfnSaveGuestDebug64, 0, NULL);
1248
1249 return SVMR0Execute64BitsHandler(pVM, pVCpu, pCtx, pVM->hwaccm.s.pfnSaveGuestDebug64, 0, NULL);
1250}
1251
1252/**
1253 * Test the 32->64 bits switcher
1254 *
1255 * @returns VBox status code.
1256 * @param pVM VM handle.
1257 */
1258VMMR0DECL(int) HWACCMR0TestSwitcher3264(PVM pVM)
1259{
1260 PVMCPU pVCpu = &pVM->aCpus[0];
1261 CPUMCTX *pCtx;
1262 uint32_t aParam[5] = {0, 1, 2, 3, 4};
1263 int rc;
1264
1265 pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1266
1267 STAM_PROFILE_ADV_START(&pVCpu->hwaccm.s.StatWorldSwitch3264, z);
1268 if (pVM->hwaccm.s.vmx.fSupported)
1269 rc = VMXR0Execute64BitsHandler(pVM, pVCpu, pCtx, pVM->hwaccm.s.pfnTest64, 5, &aParam[0]);
1270 else
1271 rc = SVMR0Execute64BitsHandler(pVM, pVCpu, pCtx, pVM->hwaccm.s.pfnTest64, 5, &aParam[0]);
1272 STAM_PROFILE_ADV_STOP(&pVCpu->hwaccm.s.StatWorldSwitch3264, z);
1273 return rc;
1274}
1275
1276#endif /* HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL) */
1277
1278/**
1279 * Returns suspend status of the host
1280 *
1281 * @returns Suspend pending or not
1282 */
1283VMMR0DECL(bool) HWACCMR0SuspendPending()
1284{
1285 return ASMAtomicReadBool(&HWACCMR0Globals.fSuspended);
1286}
1287
1288/**
1289 * Returns the cpu structure for the current cpu.
1290 * Keep in mind that there is no guarantee it will stay the same (long jumps to ring 3!!!).
1291 *
1292 * @returns cpu structure pointer
1293 */
1294VMMR0DECL(PHWACCM_CPUINFO) HWACCMR0GetCurrentCpu()
1295{
1296 RTCPUID idCpu = RTMpCpuId();
1297
1298 return &HWACCMR0Globals.aCpuInfo[idCpu];
1299}
1300
1301/**
1302 * Returns the cpu structure for the current cpu.
1303 * Keep in mind that there is no guarantee it will stay the same (long jumps to ring 3!!!).
1304 *
1305 * @returns cpu structure pointer
1306 * @param idCpu id of the VCPU
1307 */
1308VMMR0DECL(PHWACCM_CPUINFO) HWACCMR0GetCurrentCpuEx(RTCPUID idCpu)
1309{
1310 return &HWACCMR0Globals.aCpuInfo[idCpu];
1311}
1312
1313/**
1314 * Returns the VMCPU of the current EMT thread.
1315 *
1316 * @param pVM The VM to operate on.
1317 */
1318VMMR0DECL(PVMCPU) HWACCMR0GetVMCPU(PVM pVM)
1319{
1320 /* RTMpCpuId had better be cheap. */
1321 RTCPUID idHostCpu = RTMpCpuId();
1322
1323 /** @todo optimize for large number of VCPUs when that becomes more common. */
1324 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1325 {
1326 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1327
1328 if (pVCpu->hwaccm.s.idEnteredCpu == idHostCpu)
1329 return pVCpu;
1330 }
1331 return NULL;
1332}
1333
1334/**
1335 * Returns the VMCPU id of the current EMT thread.
1336 *
1337 * @param pVM The VM to operate on.
1338 */
1339VMMR0DECL(VMCPUID) HWACCMR0GetVMCPUId(PVM pVM)
1340{
1341 PVMCPU pVCpu = HWACCMR0GetVMCPU(pVM);
1342 if (pVCpu)
1343 return pVCpu->idCpu;
1344
1345 return 0;
1346}
1347
1348/**
1349 * Save a pending IO read.
1350 *
1351 * @param pVCpu The VMCPU to operate on.
1352 * @param GCPtrRip Address of IO instruction
1353 * @param GCPtrRipNext Address of the next instruction
1354 * @param uPort Port address
1355 * @param uAndVal And mask for saving the result in eax
1356 * @param cbSize Read size
1357 */
1358VMMR0DECL(void) HWACCMR0SavePendingIOPortRead(PVMCPU pVCpu, RTGCPTR GCPtrRip, RTGCPTR GCPtrRipNext, unsigned uPort, unsigned uAndVal, unsigned cbSize)
1359{
1360 pVCpu->hwaccm.s.PendingIO.enmType = HWACCMPENDINGIO_PORT_READ;
1361 pVCpu->hwaccm.s.PendingIO.GCPtrRip = GCPtrRip;
1362 pVCpu->hwaccm.s.PendingIO.GCPtrRipNext = GCPtrRipNext;
1363 pVCpu->hwaccm.s.PendingIO.s.Port.uPort = uPort;
1364 pVCpu->hwaccm.s.PendingIO.s.Port.uAndVal = uAndVal;
1365 pVCpu->hwaccm.s.PendingIO.s.Port.cbSize = cbSize;
1366 return;
1367}
1368
1369/**
1370 * Save a pending IO write.
1371 *
1372 * @param pVCpu The VMCPU to operate on.
1373 * @param GCPtrRIP Address of IO instruction
1374 * @param uPort Port address
1375 * @param uAndVal And mask for fetching the result from eax
1376 * @param cbSize Read size
1377 */
1378VMMR0DECL(void) HWACCMR0SavePendingIOPortWrite(PVMCPU pVCpu, RTGCPTR GCPtrRip, RTGCPTR GCPtrRipNext, unsigned uPort, unsigned uAndVal, unsigned cbSize)
1379{
1380 pVCpu->hwaccm.s.PendingIO.enmType = HWACCMPENDINGIO_PORT_WRITE;
1381 pVCpu->hwaccm.s.PendingIO.GCPtrRip = GCPtrRip;
1382 pVCpu->hwaccm.s.PendingIO.GCPtrRipNext = GCPtrRipNext;
1383 pVCpu->hwaccm.s.PendingIO.s.Port.uPort = uPort;
1384 pVCpu->hwaccm.s.PendingIO.s.Port.uAndVal = uAndVal;
1385 pVCpu->hwaccm.s.PendingIO.s.Port.cbSize = cbSize;
1386 return;
1387}
1388
1389/**
1390 * Disable VT-x if it's active *and* the current switcher turns off paging
1391 *
1392 * @returns VBox status code.
1393 * @param pVM VM handle.
1394 * @param pfVTxDisabled VT-x was disabled or not (out)
1395 */
1396VMMR0DECL(int) HWACCMR0EnterSwitcher(PVM pVM, bool *pfVTxDisabled)
1397{
1398 Assert(!(ASMGetFlags() & X86_EFL_IF));
1399
1400 *pfVTxDisabled = false;
1401
1402 if ( HWACCMR0Globals.enmHwAccmState != HWACCMSTATE_ENABLED
1403 || !HWACCMR0Globals.vmx.fSupported /* no such issues with AMD-V */
1404 || !HWACCMR0Globals.fGlobalInit /* Local init implies the CPU is currently not in VMX root mode. */)
1405 return VINF_SUCCESS; /* nothing to do */
1406
1407 switch(VMMGetSwitcher(pVM))
1408 {
1409 case VMMSWITCHER_32_TO_32:
1410 case VMMSWITCHER_PAE_TO_PAE:
1411 return VINF_SUCCESS; /* safe switchers as they don't turn off paging */
1412
1413 case VMMSWITCHER_32_TO_PAE:
1414 case VMMSWITCHER_PAE_TO_32: /* is this one actually used?? */
1415 case VMMSWITCHER_AMD64_TO_32:
1416 case VMMSWITCHER_AMD64_TO_PAE:
1417 break; /* unsafe switchers */
1418
1419 default:
1420 AssertFailed();
1421 return VERR_INTERNAL_ERROR;
1422 }
1423
1424 PHWACCM_CPUINFO pCpu = HWACCMR0GetCurrentCpu();
1425 void *pvPageCpu;
1426 RTHCPHYS pPageCpuPhys;
1427
1428 AssertReturn(pCpu && pCpu->pMemObj, VERR_INTERNAL_ERROR);
1429 pvPageCpu = RTR0MemObjAddress(pCpu->pMemObj);
1430 pPageCpuPhys = RTR0MemObjGetPagePhysAddr(pCpu->pMemObj, 0);
1431
1432 *pfVTxDisabled = true;
1433 return VMXR0DisableCpu(pCpu, pvPageCpu, pPageCpuPhys);
1434}
1435
1436/**
1437 * Reeable VT-x if was active *and* the current switcher turned off paging
1438 *
1439 * @returns VBox status code.
1440 * @param pVM VM handle.
1441 * @param fVTxDisabled VT-x was disabled or not
1442 */
1443VMMR0DECL(int) HWACCMR0LeaveSwitcher(PVM pVM, bool fVTxDisabled)
1444{
1445 Assert(!(ASMGetFlags() & X86_EFL_IF));
1446
1447 if (!fVTxDisabled)
1448 return VINF_SUCCESS; /* nothing to do */
1449
1450 Assert( HWACCMR0Globals.enmHwAccmState == HWACCMSTATE_ENABLED
1451 && HWACCMR0Globals.vmx.fSupported
1452 && HWACCMR0Globals.fGlobalInit);
1453
1454 PHWACCM_CPUINFO pCpu = HWACCMR0GetCurrentCpu();
1455 void *pvPageCpu;
1456 RTHCPHYS pPageCpuPhys;
1457
1458 AssertReturn(pCpu && pCpu->pMemObj, VERR_INTERNAL_ERROR);
1459 pvPageCpu = RTR0MemObjAddress(pCpu->pMemObj);
1460 pPageCpuPhys = RTR0MemObjGetPagePhysAddr(pCpu->pMemObj, 0);
1461
1462 return VMXR0EnableCpu(pCpu, pVM, pvPageCpu, pPageCpuPhys);
1463}
1464
1465#ifdef VBOX_STRICT
1466/**
1467 * Dumps a descriptor.
1468 *
1469 * @param pDesc Descriptor to dump.
1470 * @param Sel Selector number.
1471 * @param pszMsg Message to prepend the log entry with.
1472 */
1473VMMR0DECL(void) HWACCMR0DumpDescriptor(PCX86DESCHC pDesc, RTSEL Sel, const char *pszMsg)
1474{
1475 /*
1476 * Make variable description string.
1477 */
1478 static struct
1479 {
1480 unsigned cch;
1481 const char *psz;
1482 } const s_aTypes[32] =
1483 {
1484# define STRENTRY(str) { sizeof(str) - 1, str }
1485
1486 /* system */
1487# if HC_ARCH_BITS == 64
1488 STRENTRY("Reserved0 "), /* 0x00 */
1489 STRENTRY("Reserved1 "), /* 0x01 */
1490 STRENTRY("LDT "), /* 0x02 */
1491 STRENTRY("Reserved3 "), /* 0x03 */
1492 STRENTRY("Reserved4 "), /* 0x04 */
1493 STRENTRY("Reserved5 "), /* 0x05 */
1494 STRENTRY("Reserved6 "), /* 0x06 */
1495 STRENTRY("Reserved7 "), /* 0x07 */
1496 STRENTRY("Reserved8 "), /* 0x08 */
1497 STRENTRY("TSS64Avail "), /* 0x09 */
1498 STRENTRY("ReservedA "), /* 0x0a */
1499 STRENTRY("TSS64Busy "), /* 0x0b */
1500 STRENTRY("Call64 "), /* 0x0c */
1501 STRENTRY("ReservedD "), /* 0x0d */
1502 STRENTRY("Int64 "), /* 0x0e */
1503 STRENTRY("Trap64 "), /* 0x0f */
1504# else
1505 STRENTRY("Reserved0 "), /* 0x00 */
1506 STRENTRY("TSS16Avail "), /* 0x01 */
1507 STRENTRY("LDT "), /* 0x02 */
1508 STRENTRY("TSS16Busy "), /* 0x03 */
1509 STRENTRY("Call16 "), /* 0x04 */
1510 STRENTRY("Task "), /* 0x05 */
1511 STRENTRY("Int16 "), /* 0x06 */
1512 STRENTRY("Trap16 "), /* 0x07 */
1513 STRENTRY("Reserved8 "), /* 0x08 */
1514 STRENTRY("TSS32Avail "), /* 0x09 */
1515 STRENTRY("ReservedA "), /* 0x0a */
1516 STRENTRY("TSS32Busy "), /* 0x0b */
1517 STRENTRY("Call32 "), /* 0x0c */
1518 STRENTRY("ReservedD "), /* 0x0d */
1519 STRENTRY("Int32 "), /* 0x0e */
1520 STRENTRY("Trap32 "), /* 0x0f */
1521# endif
1522 /* non system */
1523 STRENTRY("DataRO "), /* 0x10 */
1524 STRENTRY("DataRO Accessed "), /* 0x11 */
1525 STRENTRY("DataRW "), /* 0x12 */
1526 STRENTRY("DataRW Accessed "), /* 0x13 */
1527 STRENTRY("DataDownRO "), /* 0x14 */
1528 STRENTRY("DataDownRO Accessed "), /* 0x15 */
1529 STRENTRY("DataDownRW "), /* 0x16 */
1530 STRENTRY("DataDownRW Accessed "), /* 0x17 */
1531 STRENTRY("CodeEO "), /* 0x18 */
1532 STRENTRY("CodeEO Accessed "), /* 0x19 */
1533 STRENTRY("CodeER "), /* 0x1a */
1534 STRENTRY("CodeER Accessed "), /* 0x1b */
1535 STRENTRY("CodeConfEO "), /* 0x1c */
1536 STRENTRY("CodeConfEO Accessed "), /* 0x1d */
1537 STRENTRY("CodeConfER "), /* 0x1e */
1538 STRENTRY("CodeConfER Accessed ") /* 0x1f */
1539# undef SYSENTRY
1540 };
1541# define ADD_STR(psz, pszAdd) do { strcpy(psz, pszAdd); psz += strlen(pszAdd); } while (0)
1542 char szMsg[128];
1543 char *psz = &szMsg[0];
1544 unsigned i = pDesc->Gen.u1DescType << 4 | pDesc->Gen.u4Type;
1545 memcpy(psz, s_aTypes[i].psz, s_aTypes[i].cch);
1546 psz += s_aTypes[i].cch;
1547
1548 if (pDesc->Gen.u1Present)
1549 ADD_STR(psz, "Present ");
1550 else
1551 ADD_STR(psz, "Not-Present ");
1552# if HC_ARCH_BITS == 64
1553 if (pDesc->Gen.u1Long)
1554 ADD_STR(psz, "64-bit ");
1555 else
1556 ADD_STR(psz, "Comp ");
1557# else
1558 if (pDesc->Gen.u1Granularity)
1559 ADD_STR(psz, "Page ");
1560 if (pDesc->Gen.u1DefBig)
1561 ADD_STR(psz, "32-bit ");
1562 else
1563 ADD_STR(psz, "16-bit ");
1564# endif
1565# undef ADD_STR
1566 *psz = '\0';
1567
1568 /*
1569 * Limit and Base and format the output.
1570 */
1571 uint32_t u32Limit = X86DESC_LIMIT(*pDesc);
1572 if (pDesc->Gen.u1Granularity)
1573 u32Limit = u32Limit << PAGE_SHIFT | PAGE_OFFSET_MASK;
1574
1575# if HC_ARCH_BITS == 64
1576 uint64_t u32Base = X86DESC64_BASE(*pDesc);
1577
1578 Log(("%s %04x - %RX64 %RX64 - base=%RX64 limit=%08x dpl=%d %s\n", pszMsg,
1579 Sel, pDesc->au64[0], pDesc->au64[1], u32Base, u32Limit, pDesc->Gen.u2Dpl, szMsg));
1580# else
1581 uint32_t u32Base = X86DESC_BASE(*pDesc);
1582
1583 Log(("%s %04x - %08x %08x - base=%08x limit=%08x dpl=%d %s\n", pszMsg,
1584 Sel, pDesc->au32[0], pDesc->au32[1], u32Base, u32Limit, pDesc->Gen.u2Dpl, szMsg));
1585# endif
1586}
1587
1588/**
1589 * Formats a full register dump.
1590 *
1591 * @param pVM The VM to operate on.
1592 * @param pVCpu The VMCPU to operate on.
1593 * @param pCtx The context to format.
1594 */
1595VMMR0DECL(void) HWACCMDumpRegs(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1596{
1597 /*
1598 * Format the flags.
1599 */
1600 static struct
1601 {
1602 const char *pszSet; const char *pszClear; uint32_t fFlag;
1603 } aFlags[] =
1604 {
1605 { "vip",NULL, X86_EFL_VIP },
1606 { "vif",NULL, X86_EFL_VIF },
1607 { "ac", NULL, X86_EFL_AC },
1608 { "vm", NULL, X86_EFL_VM },
1609 { "rf", NULL, X86_EFL_RF },
1610 { "nt", NULL, X86_EFL_NT },
1611 { "ov", "nv", X86_EFL_OF },
1612 { "dn", "up", X86_EFL_DF },
1613 { "ei", "di", X86_EFL_IF },
1614 { "tf", NULL, X86_EFL_TF },
1615 { "nt", "pl", X86_EFL_SF },
1616 { "nz", "zr", X86_EFL_ZF },
1617 { "ac", "na", X86_EFL_AF },
1618 { "po", "pe", X86_EFL_PF },
1619 { "cy", "nc", X86_EFL_CF },
1620 };
1621 char szEFlags[80];
1622 char *psz = szEFlags;
1623 uint32_t efl = pCtx->eflags.u32;
1624 for (unsigned i = 0; i < RT_ELEMENTS(aFlags); i++)
1625 {
1626 const char *pszAdd = aFlags[i].fFlag & efl ? aFlags[i].pszSet : aFlags[i].pszClear;
1627 if (pszAdd)
1628 {
1629 strcpy(psz, pszAdd);
1630 psz += strlen(pszAdd);
1631 *psz++ = ' ';
1632 }
1633 }
1634 psz[-1] = '\0';
1635
1636
1637 /*
1638 * Format the registers.
1639 */
1640 if (CPUMIsGuestIn64BitCode(pVCpu, CPUMCTX2CORE(pCtx)))
1641 {
1642 Log(("rax=%016RX64 rbx=%016RX64 rcx=%016RX64 rdx=%016RX64\n"
1643 "rsi=%016RX64 rdi=%016RX64 r8 =%016RX64 r9 =%016RX64\n"
1644 "r10=%016RX64 r11=%016RX64 r12=%016RX64 r13=%016RX64\n"
1645 "r14=%016RX64 r15=%016RX64\n"
1646 "rip=%016RX64 rsp=%016RX64 rbp=%016RX64 iopl=%d %*s\n"
1647 "cs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1648 "ds={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1649 "es={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1650 "fs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1651 "gs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1652 "ss={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1653 "cr0=%016RX64 cr2=%016RX64 cr3=%016RX64 cr4=%016RX64\n"
1654 "dr0=%016RX64 dr1=%016RX64 dr2=%016RX64 dr3=%016RX64\n"
1655 "dr4=%016RX64 dr5=%016RX64 dr6=%016RX64 dr7=%016RX64\n"
1656 "gdtr=%016RX64:%04x idtr=%016RX64:%04x eflags=%08x\n"
1657 "ldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1658 "tr ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1659 "SysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
1660 ,
1661 pCtx->rax, pCtx->rbx, pCtx->rcx, pCtx->rdx, pCtx->rsi, pCtx->rdi,
1662 pCtx->r8, pCtx->r9, pCtx->r10, pCtx->r11, pCtx->r12, pCtx->r13,
1663 pCtx->r14, pCtx->r15,
1664 pCtx->rip, pCtx->rsp, pCtx->rbp, X86_EFL_GET_IOPL(efl), 31, szEFlags,
1665 (RTSEL)pCtx->cs, pCtx->csHid.u64Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u,
1666 (RTSEL)pCtx->ds, pCtx->dsHid.u64Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u,
1667 (RTSEL)pCtx->es, pCtx->esHid.u64Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u,
1668 (RTSEL)pCtx->fs, pCtx->fsHid.u64Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u,
1669 (RTSEL)pCtx->gs, pCtx->gsHid.u64Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u,
1670 (RTSEL)pCtx->ss, pCtx->ssHid.u64Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u,
1671 pCtx->cr0, pCtx->cr2, pCtx->cr3, pCtx->cr4,
1672 pCtx->dr[0], pCtx->dr[1], pCtx->dr[2], pCtx->dr[3],
1673 pCtx->dr[4], pCtx->dr[5], pCtx->dr[6], pCtx->dr[7],
1674 pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, efl,
1675 (RTSEL)pCtx->ldtr, pCtx->ldtrHid.u64Base, pCtx->ldtrHid.u32Limit, pCtx->ldtrHid.Attr.u,
1676 (RTSEL)pCtx->tr, pCtx->trHid.u64Base, pCtx->trHid.u32Limit, pCtx->trHid.Attr.u,
1677 pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp));
1678 }
1679 else
1680 Log(("eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
1681 "eip=%08x esp=%08x ebp=%08x iopl=%d %*s\n"
1682 "cs={%04x base=%016RX64 limit=%08x flags=%08x} dr0=%08RX64 dr1=%08RX64\n"
1683 "ds={%04x base=%016RX64 limit=%08x flags=%08x} dr2=%08RX64 dr3=%08RX64\n"
1684 "es={%04x base=%016RX64 limit=%08x flags=%08x} dr4=%08RX64 dr5=%08RX64\n"
1685 "fs={%04x base=%016RX64 limit=%08x flags=%08x} dr6=%08RX64 dr7=%08RX64\n"
1686 "gs={%04x base=%016RX64 limit=%08x flags=%08x} cr0=%08RX64 cr2=%08RX64\n"
1687 "ss={%04x base=%016RX64 limit=%08x flags=%08x} cr3=%08RX64 cr4=%08RX64\n"
1688 "gdtr=%016RX64:%04x idtr=%016RX64:%04x eflags=%08x\n"
1689 "ldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1690 "tr ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1691 "SysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
1692 ,
1693 pCtx->eax, pCtx->ebx, pCtx->ecx, pCtx->edx, pCtx->esi, pCtx->edi,
1694 pCtx->eip, pCtx->esp, pCtx->ebp, X86_EFL_GET_IOPL(efl), 31, szEFlags,
1695 (RTSEL)pCtx->cs, pCtx->csHid.u64Base, pCtx->csHid.u32Limit, pCtx->csHid.Attr.u, pCtx->dr[0], pCtx->dr[1],
1696 (RTSEL)pCtx->ds, pCtx->dsHid.u64Base, pCtx->dsHid.u32Limit, pCtx->dsHid.Attr.u, pCtx->dr[2], pCtx->dr[3],
1697 (RTSEL)pCtx->es, pCtx->esHid.u64Base, pCtx->esHid.u32Limit, pCtx->esHid.Attr.u, pCtx->dr[4], pCtx->dr[5],
1698 (RTSEL)pCtx->fs, pCtx->fsHid.u64Base, pCtx->fsHid.u32Limit, pCtx->fsHid.Attr.u, pCtx->dr[6], pCtx->dr[7],
1699 (RTSEL)pCtx->gs, pCtx->gsHid.u64Base, pCtx->gsHid.u32Limit, pCtx->gsHid.Attr.u, pCtx->cr0, pCtx->cr2,
1700 (RTSEL)pCtx->ss, pCtx->ssHid.u64Base, pCtx->ssHid.u32Limit, pCtx->ssHid.Attr.u, pCtx->cr3, pCtx->cr4,
1701 pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, efl,
1702 (RTSEL)pCtx->ldtr, pCtx->ldtrHid.u64Base, pCtx->ldtrHid.u32Limit, pCtx->ldtrHid.Attr.u,
1703 (RTSEL)pCtx->tr, pCtx->trHid.u64Base, pCtx->trHid.u32Limit, pCtx->trHid.Attr.u,
1704 pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp));
1705
1706 Log(("FPU:\n"
1707 "FCW=%04x FSW=%04x FTW=%02x\n"
1708 "FOP=%04x FPUIP=%08x CS=%04x Rsvrd1=%04x\n"
1709 "FPUDP=%04x DS=%04x Rsvrd2=%04x MXCSR=%08x MXCSR_MASK=%08x\n"
1710 ,
1711 pCtx->fpu.FCW, pCtx->fpu.FSW, pCtx->fpu.FTW,
1712 pCtx->fpu.FOP, pCtx->fpu.FPUIP, pCtx->fpu.CS, pCtx->fpu.Rsvrd1,
1713 pCtx->fpu.FPUDP, pCtx->fpu.DS, pCtx->fpu.Rsrvd2,
1714 pCtx->fpu.MXCSR, pCtx->fpu.MXCSR_MASK));
1715
1716
1717 Log(("MSR:\n"
1718 "EFER =%016RX64\n"
1719 "PAT =%016RX64\n"
1720 "STAR =%016RX64\n"
1721 "CSTAR =%016RX64\n"
1722 "LSTAR =%016RX64\n"
1723 "SFMASK =%016RX64\n"
1724 "KERNELGSBASE =%016RX64\n",
1725 pCtx->msrEFER,
1726 pCtx->msrPAT,
1727 pCtx->msrSTAR,
1728 pCtx->msrCSTAR,
1729 pCtx->msrLSTAR,
1730 pCtx->msrSFMASK,
1731 pCtx->msrKERNELGSBASE));
1732
1733}
1734#endif /* VBOX_STRICT */
1735
1736/* Dummy callback handlers. */
1737VMMR0DECL(int) HWACCMR0DummyEnter(PVM pVM, PVMCPU pVCpu, PHWACCM_CPUINFO pCpu)
1738{
1739 return VINF_SUCCESS;
1740}
1741
1742VMMR0DECL(int) HWACCMR0DummyLeave(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1743{
1744 return VINF_SUCCESS;
1745}
1746
1747VMMR0DECL(int) HWACCMR0DummyEnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
1748{
1749 return VINF_SUCCESS;
1750}
1751
1752VMMR0DECL(int) HWACCMR0DummyDisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys)
1753{
1754 return VINF_SUCCESS;
1755}
1756
1757VMMR0DECL(int) HWACCMR0DummyInitVM(PVM pVM)
1758{
1759 return VINF_SUCCESS;
1760}
1761
1762VMMR0DECL(int) HWACCMR0DummyTermVM(PVM pVM)
1763{
1764 return VINF_SUCCESS;
1765}
1766
1767VMMR0DECL(int) HWACCMR0DummySetupVM(PVM pVM)
1768{
1769 return VINF_SUCCESS;
1770}
1771
1772VMMR0DECL(int) HWACCMR0DummyRunGuestCode(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1773{
1774 return VINF_SUCCESS;
1775}
1776
1777VMMR0DECL(int) HWACCMR0DummySaveHostState(PVM pVM, PVMCPU pVCpu)
1778{
1779 return VINF_SUCCESS;
1780}
1781
1782VMMR0DECL(int) HWACCMR0DummyLoadGuestState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1783{
1784 return VINF_SUCCESS;
1785}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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