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