VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp@ 57006

Last change on this file since 57006 was 56706, checked in by vboxsync, 9 years ago

HMR0Term: must init rc.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 70.8 KB
Line 
1/* $Id: HMR0.cpp 56706 2015-06-30 15:32:28Z vboxsync $ */
2/** @file
3 * Hardware Assisted Virtualization Manager (HM) - Host Context Ring-0.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_HM
22#include <VBox/vmm/hm.h>
23#include <VBox/vmm/pgm.h>
24#include "HMInternal.h"
25#include <VBox/vmm/vm.h>
26#include <VBox/vmm/hm_vmx.h>
27#include <VBox/vmm/hm_svm.h>
28#include <VBox/vmm/gim.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31#include <iprt/assert.h>
32#include <iprt/asm.h>
33#include <iprt/asm-amd64-x86.h>
34#include <iprt/cpuset.h>
35#include <iprt/mem.h>
36#include <iprt/memobj.h>
37#include <iprt/once.h>
38#include <iprt/param.h>
39#include <iprt/power.h>
40#include <iprt/string.h>
41#include <iprt/thread.h>
42#include <iprt/x86.h>
43#include "HMVMXR0.h"
44#include "HMSVMR0.h"
45
46
47/*******************************************************************************
48* Internal Functions *
49*******************************************************************************/
50static DECLCALLBACK(void) hmR0EnableCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2);
51static DECLCALLBACK(void) hmR0DisableCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2);
52static DECLCALLBACK(void) hmR0InitIntelCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2);
53static DECLCALLBACK(void) hmR0InitAmdCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2);
54static DECLCALLBACK(void) hmR0PowerCallback(RTPOWEREVENT enmEvent, void *pvUser);
55static DECLCALLBACK(void) hmR0MpEventCallback(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvData);
56
57
58/*******************************************************************************
59* Structures and Typedefs *
60*******************************************************************************/
61/**
62 * This is used to manage the status code of a RTMpOnAll in HM.
63 */
64typedef struct HMR0FIRSTRC
65{
66 /** The status code. */
67 int32_t volatile rc;
68 /** The ID of the CPU reporting the first failure. */
69 RTCPUID volatile idCpu;
70} HMR0FIRSTRC;
71/** Pointer to a first return code structure. */
72typedef HMR0FIRSTRC *PHMR0FIRSTRC;
73
74
75/*******************************************************************************
76* Global Variables *
77*******************************************************************************/
78/**
79 * Global data.
80 */
81static struct
82{
83 /** Per CPU globals. */
84 HMGLOBALCPUINFO aCpuInfo[RTCPUSET_MAX_CPUS];
85
86 /** @name Ring-0 method table for AMD-V and VT-x specific operations.
87 * @{ */
88 DECLR0CALLBACKMEMBER(int, pfnEnterSession, (PVM pVM, PVMCPU pVCpu, PHMGLOBALCPUINFO pCpu));
89 DECLR0CALLBACKMEMBER(void, pfnThreadCtxCallback, (RTTHREADCTXEVENT enmEvent, PVMCPU pVCpu, bool fGlobalInit));
90 DECLR0CALLBACKMEMBER(int, pfnSaveHostState, (PVM pVM, PVMCPU pVCpu));
91 DECLR0CALLBACKMEMBER(int, pfnRunGuestCode, (PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx));
92 DECLR0CALLBACKMEMBER(int, pfnEnableCpu, (PHMGLOBALCPUINFO pCpu, PVM pVM, void *pvCpuPage, RTHCPHYS HCPhysCpuPage,
93 bool fEnabledByHost, void *pvArg));
94 DECLR0CALLBACKMEMBER(int, pfnDisableCpu, (PHMGLOBALCPUINFO pCpu, void *pvCpuPage, RTHCPHYS HCPhysCpuPage));
95 DECLR0CALLBACKMEMBER(int, pfnInitVM, (PVM pVM));
96 DECLR0CALLBACKMEMBER(int, pfnTermVM, (PVM pVM));
97 DECLR0CALLBACKMEMBER(int, pfnSetupVM ,(PVM pVM));
98 /** @} */
99
100 /** Maximum ASID allowed. */
101 uint32_t uMaxAsid;
102
103 /** VT-x data. */
104 struct
105 {
106 /** Set to by us to indicate VMX is supported by the CPU. */
107 bool fSupported;
108 /** Whether we're using SUPR0EnableVTx or not. */
109 bool fUsingSUPR0EnableVTx;
110 /** Whether we're using the preemption timer or not. */
111 bool fUsePreemptTimer;
112 /** The shift mask employed by the VMX-Preemption timer. */
113 uint8_t cPreemptTimerShift;
114
115 /** Host CR4 value (set by ring-0 VMX init) */
116 uint64_t u64HostCr4;
117
118 /** Host EFER value (set by ring-0 VMX init) */
119 uint64_t u64HostEfer;
120
121 /** VMX MSR values */
122 VMXMSRS Msrs;
123
124 /** Last instruction error. */
125 uint32_t ulLastInstrError;
126
127 /** Set if we've called SUPR0EnableVTx(true) and should disable it during
128 * module termination. */
129 bool fCalledSUPR0EnableVTx;
130 } vmx;
131
132 /** AMD-V information. */
133 struct
134 {
135 /* HWCR MSR (for diagnostics) */
136 uint64_t u64MsrHwcr;
137
138 /** SVM revision. */
139 uint32_t u32Rev;
140
141 /** SVM feature bits from cpuid 0x8000000a */
142 uint32_t u32Features;
143
144 /** Set by us to indicate SVM is supported by the CPU. */
145 bool fSupported;
146 } svm;
147 /** Saved error from detection */
148 int32_t lLastError;
149
150 /** CPUID 0x80000001 ecx:edx features */
151 struct
152 {
153 uint32_t u32AMDFeatureECX;
154 uint32_t u32AMDFeatureEDX;
155 } cpuid;
156
157 /** If set, VT-x/AMD-V is enabled globally at init time, otherwise it's
158 * enabled and disabled each time it's used to execute guest code. */
159 bool fGlobalInit;
160 /** Indicates whether the host is suspending or not. We'll refuse a few
161 * actions when the host is being suspended to speed up the suspending and
162 * avoid trouble. */
163 volatile bool fSuspended;
164
165 /** Whether we've already initialized all CPUs.
166 * @remarks We could check the EnableAllCpusOnce state, but this is
167 * simpler and hopefully easier to understand. */
168 bool fEnabled;
169 /** Serialize initialization in HMR0EnableAllCpus. */
170 RTONCE EnableAllCpusOnce;
171} g_HmR0;
172
173
174
175/**
176 * Initializes a first return code structure.
177 *
178 * @param pFirstRc The structure to init.
179 */
180static void hmR0FirstRcInit(PHMR0FIRSTRC pFirstRc)
181{
182 pFirstRc->rc = VINF_SUCCESS;
183 pFirstRc->idCpu = NIL_RTCPUID;
184}
185
186
187/**
188 * Try set the status code (success ignored).
189 *
190 * @param pFirstRc The first return code structure.
191 * @param rc The status code.
192 */
193static void hmR0FirstRcSetStatus(PHMR0FIRSTRC pFirstRc, int rc)
194{
195 if ( RT_FAILURE(rc)
196 && ASMAtomicCmpXchgS32(&pFirstRc->rc, rc, VINF_SUCCESS))
197 pFirstRc->idCpu = RTMpCpuId();
198}
199
200
201/**
202 * Get the status code of a first return code structure.
203 *
204 * @returns The status code; VINF_SUCCESS or error status, no informational or
205 * warning errors.
206 * @param pFirstRc The first return code structure.
207 */
208static int hmR0FirstRcGetStatus(PHMR0FIRSTRC pFirstRc)
209{
210 return pFirstRc->rc;
211}
212
213
214#ifdef VBOX_STRICT
215/**
216 * Get the CPU ID on which the failure status code was reported.
217 *
218 * @returns The CPU ID, NIL_RTCPUID if no failure was reported.
219 * @param pFirstRc The first return code structure.
220 */
221static RTCPUID hmR0FirstRcGetCpuId(PHMR0FIRSTRC pFirstRc)
222{
223 return pFirstRc->idCpu;
224}
225#endif /* VBOX_STRICT */
226
227
228/** @name Dummy callback handlers.
229 * @{ */
230
231static DECLCALLBACK(int) hmR0DummyEnter(PVM pVM, PVMCPU pVCpu, PHMGLOBALCPUINFO pCpu)
232{
233 NOREF(pVM); NOREF(pVCpu); NOREF(pCpu);
234 return VINF_SUCCESS;
235}
236
237static DECLCALLBACK(void) hmR0DummyThreadCtxCallback(RTTHREADCTXEVENT enmEvent, PVMCPU pVCpu, bool fGlobalInit)
238{
239 NOREF(enmEvent); NOREF(pVCpu); NOREF(fGlobalInit);
240}
241
242static DECLCALLBACK(int) hmR0DummyEnableCpu(PHMGLOBALCPUINFO pCpu, PVM pVM, void *pvCpuPage, RTHCPHYS HCPhysCpuPage,
243 bool fEnabledBySystem, void *pvArg)
244{
245 NOREF(pCpu); NOREF(pVM); NOREF(pvCpuPage); NOREF(HCPhysCpuPage); NOREF(fEnabledBySystem); NOREF(pvArg);
246 return VINF_SUCCESS;
247}
248
249static DECLCALLBACK(int) hmR0DummyDisableCpu(PHMGLOBALCPUINFO pCpu, void *pvCpuPage, RTHCPHYS HCPhysCpuPage)
250{
251 NOREF(pCpu); NOREF(pvCpuPage); NOREF(HCPhysCpuPage);
252 return VINF_SUCCESS;
253}
254
255static DECLCALLBACK(int) hmR0DummyInitVM(PVM pVM)
256{
257 NOREF(pVM);
258 return VINF_SUCCESS;
259}
260
261static DECLCALLBACK(int) hmR0DummyTermVM(PVM pVM)
262{
263 NOREF(pVM);
264 return VINF_SUCCESS;
265}
266
267static DECLCALLBACK(int) hmR0DummySetupVM(PVM pVM)
268{
269 NOREF(pVM);
270 return VINF_SUCCESS;
271}
272
273static DECLCALLBACK(int) hmR0DummyRunGuestCode(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
274{
275 NOREF(pVM); NOREF(pVCpu); NOREF(pCtx);
276 return VINF_SUCCESS;
277}
278
279static DECLCALLBACK(int) hmR0DummySaveHostState(PVM pVM, PVMCPU pVCpu)
280{
281 NOREF(pVM); NOREF(pVCpu);
282 return VINF_SUCCESS;
283}
284
285/** @} */
286
287
288/**
289 * Checks if the CPU is subject to the "VMX-Preemption Timer Does Not Count
290 * Down at the Rate Specified" erratum.
291 *
292 * Errata names and related steppings:
293 * - BA86 - D0.
294 * - AAX65 - C2.
295 * - AAU65 - C2, K0.
296 * - AAO95 - B1.
297 * - AAT59 - C2.
298 * - AAK139 - D0.
299 * - AAM126 - C0, C1, D0.
300 * - AAN92 - B1.
301 * - AAJ124 - C0, D0.
302 *
303 * - AAP86 - B1.
304 *
305 * Steppings: B1, C0, C1, C2, D0, K0.
306 *
307 * @returns true if subject to it, false if not.
308 */
309static bool hmR0InitIntelIsSubjectToVmxPreemptionTimerErratum(void)
310{
311 uint32_t u = ASMCpuId_EAX(1);
312 u &= ~(RT_BIT_32(14) | RT_BIT_32(15) | RT_BIT_32(28) | RT_BIT_32(29) | RT_BIT_32(30) | RT_BIT_32(31));
313 if ( u == UINT32_C(0x000206E6) /* 323344.pdf - BA86 - D0 - Intel Xeon Processor 7500 Series */
314 || u == UINT32_C(0x00020652) /* 323056.pdf - AAX65 - C2 - Intel Xeon Processor L3406 */
315 || u == UINT32_C(0x00020652) /* 322814.pdf - AAT59 - C2 - Intel CoreTM i7-600, i5-500, i5-400 and i3-300 Mobile Processor Series */
316 || u == UINT32_C(0x00020652) /* 322911.pdf - AAU65 - C2 - Intel CoreTM i5-600, i3-500 Desktop Processor Series and Intel Pentium Processor G6950 */
317 || u == UINT32_C(0x00020655) /* 322911.pdf - AAU65 - K0 - Intel CoreTM i5-600, i3-500 Desktop Processor Series and Intel Pentium Processor G6950 */
318 || u == UINT32_C(0x000106E5) /* 322373.pdf - AAO95 - B1 - Intel Xeon Processor 3400 Series */
319 || u == UINT32_C(0x000106E5) /* 322166.pdf - AAN92 - B1 - Intel CoreTM i7-800 and i5-700 Desktop Processor Series */
320 || u == UINT32_C(0x000106E5) /* 320767.pdf - AAP86 - B1 - Intel Core i7-900 Mobile Processor Extreme Edition Series, Intel Core i7-800 and i7-700 Mobile Processor Series */
321 || u == UINT32_C(0x000106A0) /* 321333.pdf - AAM126 - C0 - Intel Xeon Processor 3500 Series Specification */
322 || u == UINT32_C(0x000106A1) /* 321333.pdf - AAM126 - C1 - Intel Xeon Processor 3500 Series Specification */
323 || u == UINT32_C(0x000106A4) /* 320836.pdf - AAJ124 - C0 - Intel Core i7-900 Desktop Processor Extreme Edition Series and Intel Core i7-900 Desktop Processor Series */
324 || u == UINT32_C(0x000106A5) /* 321333.pdf - AAM126 - D0 - Intel Xeon Processor 3500 Series Specification */
325 || u == UINT32_C(0x000106A5) /* 321324.pdf - AAK139 - D0 - Intel Xeon Processor 5500 Series Specification */
326 || u == UINT32_C(0x000106A5) /* 320836.pdf - AAJ124 - D0 - Intel Core i7-900 Desktop Processor Extreme Edition Series and Intel Core i7-900 Desktop Processor Series */
327 )
328 return true;
329 return false;
330}
331
332
333/**
334 * Intel specific initialization code.
335 *
336 * @returns VBox status code (will only fail if out of memory).
337 */
338static int hmR0InitIntel(uint32_t u32FeaturesECX, uint32_t u32FeaturesEDX)
339{
340 /*
341 * Check that all the required VT-x features are present.
342 * We also assume all VT-x-enabled CPUs support fxsave/fxrstor.
343 */
344 if ( (u32FeaturesECX & X86_CPUID_FEATURE_ECX_VMX)
345 && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_MSR)
346 && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_FXSR)
347 )
348 {
349 /* Read this MSR now as it may be useful for error reporting when initializing VT-x fails. */
350 g_HmR0.vmx.Msrs.u64FeatureCtrl = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
351
352 /*
353 * First try use native kernel API for controlling VT-x.
354 * (This is only supported by some Mac OS X kernels atm.)
355 */
356 int rc = g_HmR0.lLastError = SUPR0EnableVTx(true /* fEnable */);
357 g_HmR0.vmx.fUsingSUPR0EnableVTx = rc != VERR_NOT_SUPPORTED;
358 if (g_HmR0.vmx.fUsingSUPR0EnableVTx)
359 {
360 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VERR_VMX_IN_VMX_ROOT_MODE || rc == VERR_VMX_NO_VMX, ("%Rrc\n", rc));
361 if (RT_SUCCESS(rc))
362 {
363 g_HmR0.vmx.fSupported = true;
364 rc = SUPR0EnableVTx(false /* fEnable */);
365 AssertLogRelRC(rc);
366 }
367 }
368 else
369 {
370 HMR0FIRSTRC FirstRc;
371 hmR0FirstRcInit(&FirstRc);
372 g_HmR0.lLastError = RTMpOnAll(hmR0InitIntelCpu, &FirstRc, NULL);
373 if (RT_SUCCESS(g_HmR0.lLastError))
374 g_HmR0.lLastError = hmR0FirstRcGetStatus(&FirstRc);
375 }
376 if (RT_SUCCESS(g_HmR0.lLastError))
377 {
378 /* Reread in case it was changed by SUPR0GetVmxUsability(). */
379 g_HmR0.vmx.Msrs.u64FeatureCtrl = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
380
381 /*
382 * Read all relevant registers and MSRs.
383 */
384 g_HmR0.vmx.u64HostCr4 = ASMGetCR4();
385 g_HmR0.vmx.u64HostEfer = ASMRdMsr(MSR_K6_EFER);
386 g_HmR0.vmx.Msrs.u64BasicInfo = ASMRdMsr(MSR_IA32_VMX_BASIC_INFO);
387 g_HmR0.vmx.Msrs.VmxPinCtls.u = ASMRdMsr(MSR_IA32_VMX_PINBASED_CTLS);
388 g_HmR0.vmx.Msrs.VmxProcCtls.u = ASMRdMsr(MSR_IA32_VMX_PROCBASED_CTLS);
389 g_HmR0.vmx.Msrs.VmxExit.u = ASMRdMsr(MSR_IA32_VMX_EXIT_CTLS);
390 g_HmR0.vmx.Msrs.VmxEntry.u = ASMRdMsr(MSR_IA32_VMX_ENTRY_CTLS);
391 g_HmR0.vmx.Msrs.u64Misc = ASMRdMsr(MSR_IA32_VMX_MISC);
392 g_HmR0.vmx.Msrs.u64Cr0Fixed0 = ASMRdMsr(MSR_IA32_VMX_CR0_FIXED0);
393 g_HmR0.vmx.Msrs.u64Cr0Fixed1 = ASMRdMsr(MSR_IA32_VMX_CR0_FIXED1);
394 g_HmR0.vmx.Msrs.u64Cr4Fixed0 = ASMRdMsr(MSR_IA32_VMX_CR4_FIXED0);
395 g_HmR0.vmx.Msrs.u64Cr4Fixed1 = ASMRdMsr(MSR_IA32_VMX_CR4_FIXED1);
396 g_HmR0.vmx.Msrs.u64VmcsEnum = ASMRdMsr(MSR_IA32_VMX_VMCS_ENUM);
397 /* VPID 16 bits ASID. */
398 g_HmR0.uMaxAsid = 0x10000; /* exclusive */
399
400 if (g_HmR0.vmx.Msrs.VmxProcCtls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
401 {
402 g_HmR0.vmx.Msrs.VmxProcCtls2.u = ASMRdMsr(MSR_IA32_VMX_PROCBASED_CTLS2);
403 if (g_HmR0.vmx.Msrs.VmxProcCtls2.n.allowed1 & (VMX_VMCS_CTRL_PROC_EXEC2_EPT | VMX_VMCS_CTRL_PROC_EXEC2_VPID))
404 g_HmR0.vmx.Msrs.u64EptVpidCaps = ASMRdMsr(MSR_IA32_VMX_EPT_VPID_CAP);
405
406 if (g_HmR0.vmx.Msrs.VmxProcCtls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_VMFUNC)
407 g_HmR0.vmx.Msrs.u64Vmfunc = ASMRdMsr(MSR_IA32_VMX_VMFUNC);
408 }
409
410 if (!g_HmR0.vmx.fUsingSUPR0EnableVTx)
411 {
412 /*
413 * Enter root mode
414 */
415 RTR0MEMOBJ hScatchMemObj;
416 rc = RTR0MemObjAllocCont(&hScatchMemObj, PAGE_SIZE, false /* fExecutable */);
417 if (RT_FAILURE(rc))
418 {
419 LogRel(("hmR0InitIntel: RTR0MemObjAllocCont(,PAGE_SIZE,false) -> %Rrc\n", rc));
420 return rc;
421 }
422
423 void *pvScatchPage = RTR0MemObjAddress(hScatchMemObj);
424 RTHCPHYS HCPhysScratchPage = RTR0MemObjGetPagePhysAddr(hScatchMemObj, 0);
425 ASMMemZeroPage(pvScatchPage);
426
427 /* Set revision dword at the beginning of the structure. */
428 *(uint32_t *)pvScatchPage = MSR_IA32_VMX_BASIC_INFO_VMCS_ID(g_HmR0.vmx.Msrs.u64BasicInfo);
429
430 /* Make sure we don't get rescheduled to another cpu during this probe. */
431 RTCCUINTREG fFlags = ASMIntDisableFlags();
432
433 /*
434 * Check CR4.VMXE
435 */
436 g_HmR0.vmx.u64HostCr4 = ASMGetCR4();
437 if (!(g_HmR0.vmx.u64HostCr4 & X86_CR4_VMXE))
438 {
439 /* In theory this bit could be cleared behind our back. Which would cause
440 #UD faults when we try to execute the VMX instructions... */
441 ASMSetCR4(g_HmR0.vmx.u64HostCr4 | X86_CR4_VMXE);
442 }
443
444 /*
445 * The only way of checking if we're in VMX root mode or not is to try and enter it.
446 * There is no instruction or control bit that tells us if we're in VMX root mode.
447 * Therefore, try and enter VMX root mode here.
448 */
449 rc = VMXEnable(HCPhysScratchPage);
450 if (RT_SUCCESS(rc))
451 {
452 g_HmR0.vmx.fSupported = true;
453 VMXDisable();
454 }
455 else
456 {
457 /*
458 * KVM leaves the CPU in VMX root mode. Not only is this not allowed,
459 * it will crash the host when we enter raw mode, because:
460 *
461 * (a) clearing X86_CR4_VMXE in CR4 causes a #GP (we no longer modify
462 * this bit), and
463 * (b) turning off paging causes a #GP (unavoidable when switching
464 * from long to 32 bits mode or 32 bits to PAE).
465 *
466 * They should fix their code, but until they do we simply refuse to run.
467 */
468 g_HmR0.lLastError = VERR_VMX_IN_VMX_ROOT_MODE;
469 Assert(g_HmR0.vmx.fSupported == false);
470 }
471
472 /* Restore CR4 again; don't leave the X86_CR4_VMXE flag set
473 if it wasn't so before (some software could incorrectly
474 think it's in VMX mode). */
475 ASMSetCR4(g_HmR0.vmx.u64HostCr4);
476 ASMSetFlags(fFlags);
477
478 RTR0MemObjFree(hScatchMemObj, false);
479 }
480
481 if (g_HmR0.vmx.fSupported)
482 {
483 rc = VMXR0GlobalInit();
484 if (RT_FAILURE(rc))
485 g_HmR0.lLastError = rc;
486
487 /*
488 * Install the VT-x methods.
489 */
490 g_HmR0.pfnEnterSession = VMXR0Enter;
491 g_HmR0.pfnThreadCtxCallback = VMXR0ThreadCtxCallback;
492 g_HmR0.pfnSaveHostState = VMXR0SaveHostState;
493 g_HmR0.pfnRunGuestCode = VMXR0RunGuestCode;
494 g_HmR0.pfnEnableCpu = VMXR0EnableCpu;
495 g_HmR0.pfnDisableCpu = VMXR0DisableCpu;
496 g_HmR0.pfnInitVM = VMXR0InitVM;
497 g_HmR0.pfnTermVM = VMXR0TermVM;
498 g_HmR0.pfnSetupVM = VMXR0SetupVM;
499
500 /*
501 * Check for the VMX-Preemption Timer and adjust for the "VMX-Preemption
502 * Timer Does Not Count Down at the Rate Specified" erratum.
503 */
504 if (g_HmR0.vmx.Msrs.VmxPinCtls.n.allowed1 & VMX_VMCS_CTRL_PIN_EXEC_PREEMPT_TIMER)
505 {
506 g_HmR0.vmx.fUsePreemptTimer = true;
507 g_HmR0.vmx.cPreemptTimerShift = MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT(g_HmR0.vmx.Msrs.u64Misc);
508 if (hmR0InitIntelIsSubjectToVmxPreemptionTimerErratum())
509 g_HmR0.vmx.cPreemptTimerShift = 0; /* This is about right most of the time here. */
510 }
511 }
512 }
513#ifdef LOG_ENABLED
514 else
515 SUPR0Printf("hmR0InitIntelCpu failed with rc=%d\n", g_HmR0.lLastError);
516#endif
517 }
518 else
519 g_HmR0.lLastError = VERR_VMX_NO_VMX;
520 return VINF_SUCCESS;
521}
522
523
524/**
525 * AMD-specific initialization code.
526 *
527 * @returns VBox status code.
528 */
529static int hmR0InitAmd(uint32_t u32FeaturesEDX, uint32_t uMaxExtLeaf)
530{
531 /*
532 * Read all SVM MSRs if SVM is available. (same goes for RDMSR/WRMSR)
533 * We also assume all SVM-enabled CPUs support fxsave/fxrstor.
534 */
535 int rc;
536 if ( (g_HmR0.cpuid.u32AMDFeatureECX & X86_CPUID_AMD_FEATURE_ECX_SVM)
537 && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_MSR)
538 && (u32FeaturesEDX & X86_CPUID_FEATURE_EDX_FXSR)
539 && ASMIsValidExtRange(uMaxExtLeaf)
540 && uMaxExtLeaf >= 0x8000000a
541 )
542 {
543 /* Call the global AMD-V initialization routine. */
544 rc = SVMR0GlobalInit();
545 if (RT_FAILURE(rc))
546 {
547 g_HmR0.lLastError = rc;
548 return rc;
549 }
550
551 /*
552 * Install the AMD-V methods.
553 */
554 g_HmR0.pfnEnterSession = SVMR0Enter;
555 g_HmR0.pfnThreadCtxCallback = SVMR0ThreadCtxCallback;
556 g_HmR0.pfnSaveHostState = SVMR0SaveHostState;
557 g_HmR0.pfnRunGuestCode = SVMR0RunGuestCode;
558 g_HmR0.pfnEnableCpu = SVMR0EnableCpu;
559 g_HmR0.pfnDisableCpu = SVMR0DisableCpu;
560 g_HmR0.pfnInitVM = SVMR0InitVM;
561 g_HmR0.pfnTermVM = SVMR0TermVM;
562 g_HmR0.pfnSetupVM = SVMR0SetupVM;
563
564 /* Query AMD features. */
565 uint32_t u32Dummy;
566 ASMCpuId(0x8000000a, &g_HmR0.svm.u32Rev, &g_HmR0.uMaxAsid, &u32Dummy, &g_HmR0.svm.u32Features);
567
568 /*
569 * We need to check if AMD-V has been properly initialized on all CPUs.
570 * Some BIOSes might do a poor job.
571 */
572 HMR0FIRSTRC FirstRc;
573 hmR0FirstRcInit(&FirstRc);
574 rc = RTMpOnAll(hmR0InitAmdCpu, &FirstRc, NULL);
575 AssertRC(rc);
576 if (RT_SUCCESS(rc))
577 rc = hmR0FirstRcGetStatus(&FirstRc);
578#ifndef DEBUG_bird
579 AssertMsg(rc == VINF_SUCCESS || rc == VERR_SVM_IN_USE,
580 ("hmR0InitAmdCpu failed for cpu %d with rc=%Rrc\n", hmR0FirstRcGetCpuId(&FirstRc), rc));
581#endif
582 if (RT_SUCCESS(rc))
583 {
584 /* Read the HWCR MSR for diagnostics. */
585 g_HmR0.svm.u64MsrHwcr = ASMRdMsr(MSR_K8_HWCR);
586 g_HmR0.svm.fSupported = true;
587 }
588 else
589 {
590 g_HmR0.lLastError = rc;
591 if (rc == VERR_SVM_DISABLED || rc == VERR_SVM_IN_USE)
592 rc = VINF_SUCCESS; /* Don't fail if AMD-V is disabled or in use. */
593 }
594 }
595 else
596 {
597 rc = VINF_SUCCESS; /* Don't fail if AMD-V is not supported. See @bugref{6785}. */
598 g_HmR0.lLastError = VERR_SVM_NO_SVM;
599 }
600 return rc;
601}
602
603
604/**
605 * Does global Ring-0 HM initialization (at module init).
606 *
607 * @returns VBox status code.
608 */
609VMMR0_INT_DECL(int) HMR0Init(void)
610{
611 /*
612 * Initialize the globals.
613 */
614 g_HmR0.fEnabled = false;
615 static RTONCE s_OnceInit = RTONCE_INITIALIZER;
616 g_HmR0.EnableAllCpusOnce = s_OnceInit;
617 for (unsigned i = 0; i < RT_ELEMENTS(g_HmR0.aCpuInfo); i++)
618 {
619 g_HmR0.aCpuInfo[i].hMemObj = NIL_RTR0MEMOBJ;
620 g_HmR0.aCpuInfo[i].idCpu = NIL_RTCPUID;
621 }
622
623 /* Fill in all callbacks with placeholders. */
624 g_HmR0.pfnEnterSession = hmR0DummyEnter;
625 g_HmR0.pfnThreadCtxCallback = hmR0DummyThreadCtxCallback;
626 g_HmR0.pfnSaveHostState = hmR0DummySaveHostState;
627 g_HmR0.pfnRunGuestCode = hmR0DummyRunGuestCode;
628 g_HmR0.pfnEnableCpu = hmR0DummyEnableCpu;
629 g_HmR0.pfnDisableCpu = hmR0DummyDisableCpu;
630 g_HmR0.pfnInitVM = hmR0DummyInitVM;
631 g_HmR0.pfnTermVM = hmR0DummyTermVM;
632 g_HmR0.pfnSetupVM = hmR0DummySetupVM;
633
634 /* Default is global VT-x/AMD-V init. */
635 g_HmR0.fGlobalInit = true;
636
637 /*
638 * Make sure aCpuInfo is big enough for all the CPUs on this system.
639 */
640 if (RTMpGetArraySize() > RT_ELEMENTS(g_HmR0.aCpuInfo))
641 {
642 LogRel(("HM: Too many real CPUs/cores/threads - %u, max %u\n", RTMpGetArraySize(), RT_ELEMENTS(g_HmR0.aCpuInfo)));
643 return VERR_TOO_MANY_CPUS;
644 }
645
646 /*
647 * Check for VT-x and AMD-V capabilities.
648 */
649 int rc;
650 if (ASMHasCpuId())
651 {
652 /* Standard features. */
653 uint32_t uMaxLeaf, u32VendorEBX, u32VendorECX, u32VendorEDX;
654 ASMCpuId(0, &uMaxLeaf, &u32VendorEBX, &u32VendorECX, &u32VendorEDX);
655 if (ASMIsValidStdRange(uMaxLeaf))
656 {
657 uint32_t u32FeaturesECX, u32FeaturesEDX, u32Dummy;
658 ASMCpuId(1, &u32Dummy, &u32Dummy, &u32FeaturesECX, &u32FeaturesEDX);
659
660 /* Query AMD features. */
661 uint32_t uMaxExtLeaf = ASMCpuId_EAX(0x80000000);
662 if (ASMIsValidExtRange(uMaxExtLeaf))
663 ASMCpuId(0x80000001, &u32Dummy, &u32Dummy,
664 &g_HmR0.cpuid.u32AMDFeatureECX,
665 &g_HmR0.cpuid.u32AMDFeatureEDX);
666 else
667 g_HmR0.cpuid.u32AMDFeatureECX = g_HmR0.cpuid.u32AMDFeatureEDX = 0;
668
669 /* Go to CPU specific initialization code. */
670 if ( ASMIsIntelCpuEx(u32VendorEBX, u32VendorECX, u32VendorEDX)
671 || ASMIsViaCentaurCpuEx(u32VendorEBX, u32VendorECX, u32VendorEDX))
672 {
673 rc = hmR0InitIntel(u32FeaturesECX, u32FeaturesEDX);
674 if (RT_FAILURE(rc))
675 return rc;
676 }
677 else if (ASMIsAmdCpuEx(u32VendorEBX, u32VendorECX, u32VendorEDX))
678 {
679 rc = hmR0InitAmd(u32FeaturesEDX, uMaxExtLeaf);
680 if (RT_FAILURE(rc))
681 return rc;
682 }
683 else
684 g_HmR0.lLastError = VERR_HM_UNKNOWN_CPU;
685 }
686 else
687 g_HmR0.lLastError = VERR_HM_UNKNOWN_CPU;
688 }
689 else
690 g_HmR0.lLastError = VERR_HM_NO_CPUID;
691
692 /*
693 * Register notification callbacks that we can use to disable/enable CPUs
694 * when brought offline/online or suspending/resuming.
695 */
696 if (!g_HmR0.vmx.fUsingSUPR0EnableVTx)
697 {
698 rc = RTMpNotificationRegister(hmR0MpEventCallback, NULL);
699 AssertRC(rc);
700
701 rc = RTPowerNotificationRegister(hmR0PowerCallback, NULL);
702 AssertRC(rc);
703 }
704
705 /* We return success here because module init shall not fail if HM
706 fails to initialize. */
707 return VINF_SUCCESS;
708}
709
710
711/**
712 * Does global Ring-0 HM termination (at module termination).
713 *
714 * @returns VBox status code.
715 */
716VMMR0_INT_DECL(int) HMR0Term(void)
717{
718 int rc;
719 if ( g_HmR0.vmx.fSupported
720 && g_HmR0.vmx.fUsingSUPR0EnableVTx)
721 {
722 /*
723 * Simple if the host OS manages VT-x.
724 */
725 Assert(g_HmR0.fGlobalInit);
726
727 if (g_HmR0.vmx.fCalledSUPR0EnableVTx)
728 {
729 rc = SUPR0EnableVTx(false /* fEnable */);
730 g_HmR0.vmx.fCalledSUPR0EnableVTx = false;
731 }
732 else
733 rc = VINF_SUCCESS;
734
735 for (unsigned iCpu = 0; iCpu < RT_ELEMENTS(g_HmR0.aCpuInfo); iCpu++)
736 {
737 g_HmR0.aCpuInfo[iCpu].fConfigured = false;
738 Assert(g_HmR0.aCpuInfo[iCpu].hMemObj == NIL_RTR0MEMOBJ);
739 }
740 }
741 else
742 {
743 Assert(!g_HmR0.vmx.fSupported || !g_HmR0.vmx.fUsingSUPR0EnableVTx);
744
745 /* Doesn't really matter if this fails. */
746 rc = RTMpNotificationDeregister(hmR0MpEventCallback, NULL); AssertRC(rc);
747 rc = RTPowerNotificationDeregister(hmR0PowerCallback, NULL); AssertRC(rc);
748
749 /*
750 * Disable VT-x/AMD-V on all CPUs if we enabled it before.
751 */
752 if (g_HmR0.fGlobalInit)
753 {
754 HMR0FIRSTRC FirstRc;
755 hmR0FirstRcInit(&FirstRc);
756 rc = RTMpOnAll(hmR0DisableCpuCallback, NULL /* pvUser 1 */, &FirstRc);
757 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
758 if (RT_SUCCESS(rc))
759 rc = hmR0FirstRcGetStatus(&FirstRc);
760 }
761
762 /*
763 * Free the per-cpu pages used for VT-x and AMD-V.
764 */
765 for (unsigned i = 0; i < RT_ELEMENTS(g_HmR0.aCpuInfo); i++)
766 {
767 if (g_HmR0.aCpuInfo[i].hMemObj != NIL_RTR0MEMOBJ)
768 {
769 RTR0MemObjFree(g_HmR0.aCpuInfo[i].hMemObj, false);
770 g_HmR0.aCpuInfo[i].hMemObj = NIL_RTR0MEMOBJ;
771 }
772 }
773 }
774
775 /** @todo This needs cleaning up. There's no matching
776 * hmR0TermIntel()/hmR0TermAmd() and all the VT-x/AMD-V specific bits
777 * should move into their respective modules. */
778 /* Finally, call global VT-x/AMD-V termination. */
779 if (g_HmR0.vmx.fSupported)
780 VMXR0GlobalTerm();
781 else if (g_HmR0.svm.fSupported)
782 SVMR0GlobalTerm();
783
784 return rc;
785}
786
787
788/**
789 * Worker function used by hmR0PowerCallback() and HMR0Init() to initalize VT-x
790 * on a CPU.
791 *
792 * @param idCpu The identifier for the CPU the function is called on.
793 * @param pvUser1 Pointer to the first RC structure.
794 * @param pvUser2 Ignored.
795 */
796static DECLCALLBACK(void) hmR0InitIntelCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
797{
798 PHMR0FIRSTRC pFirstRc = (PHMR0FIRSTRC)pvUser1;
799 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
800 Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /** @todo fix idCpu == index assumption (rainy day) */
801 NOREF(idCpu); NOREF(pvUser2);
802
803 int rc = SUPR0GetVmxUsability(NULL /* pfIsSmxModeAmbiguous */);
804 hmR0FirstRcSetStatus(pFirstRc, rc);
805}
806
807
808/**
809 * Worker function used by hmR0PowerCallback() and HMR0Init() to initalize AMD-V
810 * on a CPU.
811 *
812 * @param idCpu The identifier for the CPU the function is called on.
813 * @param pvUser1 Pointer to the first RC structure.
814 * @param pvUser2 Ignored.
815 */
816static DECLCALLBACK(void) hmR0InitAmdCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
817{
818 PHMR0FIRSTRC pFirstRc = (PHMR0FIRSTRC)pvUser1;
819 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
820 Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /** @todo fix idCpu == index assumption (rainy day) */
821 NOREF(idCpu); NOREF(pvUser2);
822
823 int rc = SUPR0GetSvmUsability(true /* fInitSvm */);
824 hmR0FirstRcSetStatus(pFirstRc, rc);
825}
826
827
828/**
829 * Enable VT-x or AMD-V on the current CPU
830 *
831 * @returns VBox status code.
832 * @param pVM Pointer to the VM (can be NULL).
833 * @param idCpu The identifier for the CPU the function is called on.
834 *
835 * @remarks Maybe called with interrupts disabled!
836 */
837static int hmR0EnableCpu(PVM pVM, RTCPUID idCpu)
838{
839 PHMGLOBALCPUINFO pCpu = &g_HmR0.aCpuInfo[idCpu];
840
841 Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /** @todo fix idCpu == index assumption (rainy day) */
842 Assert(idCpu < RT_ELEMENTS(g_HmR0.aCpuInfo));
843 Assert(!pCpu->fConfigured);
844 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
845
846 pCpu->idCpu = idCpu;
847 /* Do NOT reset cTlbFlushes here, see @bugref{6255}. */
848
849 int rc;
850 if (g_HmR0.vmx.fSupported && g_HmR0.vmx.fUsingSUPR0EnableVTx)
851 rc = g_HmR0.pfnEnableCpu(pCpu, pVM, NULL /* pvCpuPage */, NIL_RTHCPHYS, true, &g_HmR0.vmx.Msrs);
852 else
853 {
854 AssertLogRelMsgReturn(pCpu->hMemObj != NIL_RTR0MEMOBJ, ("hmR0EnableCpu failed idCpu=%u.\n", idCpu), VERR_HM_IPE_1);
855 void *pvCpuPage = RTR0MemObjAddress(pCpu->hMemObj);
856 RTHCPHYS HCPhysCpuPage = RTR0MemObjGetPagePhysAddr(pCpu->hMemObj, 0 /* iPage */);
857
858 if (g_HmR0.vmx.fSupported)
859 rc = g_HmR0.pfnEnableCpu(pCpu, pVM, pvCpuPage, HCPhysCpuPage, false, &g_HmR0.vmx.Msrs);
860 else
861 rc = g_HmR0.pfnEnableCpu(pCpu, pVM, pvCpuPage, HCPhysCpuPage, false, NULL /* pvArg */);
862 }
863 if (RT_SUCCESS(rc))
864 pCpu->fConfigured = true;
865
866 return rc;
867}
868
869
870/**
871 * Worker function passed to RTMpOnAll() that is to be called on all CPUs.
872 *
873 * @param idCpu The identifier for the CPU the function is called on.
874 * @param pvUser1 Opaque pointer to the VM (can be NULL!).
875 * @param pvUser2 The 2nd user argument.
876 */
877static DECLCALLBACK(void) hmR0EnableCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
878{
879 PVM pVM = (PVM)pvUser1; /* can be NULL! */
880 PHMR0FIRSTRC pFirstRc = (PHMR0FIRSTRC)pvUser2;
881 AssertReturnVoid(g_HmR0.fGlobalInit);
882 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
883 hmR0FirstRcSetStatus(pFirstRc, hmR0EnableCpu(pVM, idCpu));
884}
885
886
887/**
888 * RTOnce callback employed by HMR0EnableAllCpus.
889 *
890 * @returns VBox status code.
891 * @param pvUser Pointer to the VM.
892 * @param pvUserIgnore NULL, ignored.
893 */
894static DECLCALLBACK(int32_t) hmR0EnableAllCpuOnce(void *pvUser)
895{
896 PVM pVM = (PVM)pvUser;
897
898 /*
899 * Indicate that we've initialized.
900 *
901 * Note! There is a potential race between this function and the suspend
902 * notification. Kind of unlikely though, so ignored for now.
903 */
904 AssertReturn(!g_HmR0.fEnabled, VERR_HM_ALREADY_ENABLED_IPE);
905 ASMAtomicWriteBool(&g_HmR0.fEnabled, true);
906
907 /*
908 * The global init variable is set by the first VM.
909 */
910 g_HmR0.fGlobalInit = pVM->hm.s.fGlobalInit;
911
912#ifdef VBOX_STRICT
913 for (unsigned i = 0; i < RT_ELEMENTS(g_HmR0.aCpuInfo); i++)
914 {
915 Assert(g_HmR0.aCpuInfo[i].hMemObj == NIL_RTR0MEMOBJ);
916 Assert(!g_HmR0.aCpuInfo[i].fConfigured);
917 Assert(!g_HmR0.aCpuInfo[i].cTlbFlushes);
918 Assert(!g_HmR0.aCpuInfo[i].uCurrentAsid);
919 }
920#endif
921
922 int rc;
923 if ( g_HmR0.vmx.fSupported
924 && g_HmR0.vmx.fUsingSUPR0EnableVTx)
925 {
926 /*
927 * Global VT-x initialization API (only darwin for now).
928 */
929 rc = SUPR0EnableVTx(true /* fEnable */);
930 if (RT_SUCCESS(rc))
931 {
932 g_HmR0.vmx.fCalledSUPR0EnableVTx = true;
933 /* If the host provides a VT-x init API, then we'll rely on that for global init. */
934 g_HmR0.fGlobalInit = pVM->hm.s.fGlobalInit = true;
935 }
936 else
937 AssertMsgFailed(("hmR0EnableAllCpuOnce/SUPR0EnableVTx: rc=%Rrc\n", rc));
938 }
939 else
940 {
941 /*
942 * We're doing the job ourselves.
943 */
944 /* Allocate one page per cpu for the global VT-x and AMD-V pages */
945 for (unsigned i = 0; i < RT_ELEMENTS(g_HmR0.aCpuInfo); i++)
946 {
947 Assert(g_HmR0.aCpuInfo[i].hMemObj == NIL_RTR0MEMOBJ);
948
949 if (RTMpIsCpuPossible(RTMpCpuIdFromSetIndex(i)))
950 {
951 rc = RTR0MemObjAllocCont(&g_HmR0.aCpuInfo[i].hMemObj, PAGE_SIZE, false /* executable R0 mapping */);
952 AssertLogRelRCReturn(rc, rc);
953
954 void *pvR0 = RTR0MemObjAddress(g_HmR0.aCpuInfo[i].hMemObj); Assert(pvR0);
955 ASMMemZeroPage(pvR0);
956 }
957 }
958
959 rc = VINF_SUCCESS;
960 }
961
962 if ( RT_SUCCESS(rc)
963 && g_HmR0.fGlobalInit)
964 {
965 /* First time, so initialize each cpu/core. */
966 HMR0FIRSTRC FirstRc;
967 hmR0FirstRcInit(&FirstRc);
968 rc = RTMpOnAll(hmR0EnableCpuCallback, (void *)pVM, &FirstRc);
969 if (RT_SUCCESS(rc))
970 rc = hmR0FirstRcGetStatus(&FirstRc);
971 }
972
973 return rc;
974}
975
976
977/**
978 * Sets up HM on all cpus.
979 *
980 * @returns VBox status code.
981 * @param pVM Pointer to the VM.
982 */
983VMMR0_INT_DECL(int) HMR0EnableAllCpus(PVM pVM)
984{
985 /* Make sure we don't touch HM after we've disabled HM in preparation of a suspend. */
986 if (ASMAtomicReadBool(&g_HmR0.fSuspended))
987 return VERR_HM_SUSPEND_PENDING;
988
989 return RTOnce(&g_HmR0.EnableAllCpusOnce, hmR0EnableAllCpuOnce, pVM);
990}
991
992
993/**
994 * Disable VT-x or AMD-V on the current CPU.
995 *
996 * @returns VBox status code.
997 * @param idCpu The identifier for the CPU this function is called on.
998 *
999 * @remarks Must be called with preemption disabled.
1000 */
1001static int hmR0DisableCpu(RTCPUID idCpu)
1002{
1003 PHMGLOBALCPUINFO pCpu = &g_HmR0.aCpuInfo[idCpu];
1004
1005 Assert(!g_HmR0.vmx.fSupported || !g_HmR0.vmx.fUsingSUPR0EnableVTx);
1006 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
1007 Assert(idCpu == (RTCPUID)RTMpCpuIdToSetIndex(idCpu)); /** @todo fix idCpu == index assumption (rainy day) */
1008 Assert(idCpu < RT_ELEMENTS(g_HmR0.aCpuInfo));
1009 Assert(!pCpu->fConfigured || pCpu->hMemObj != NIL_RTR0MEMOBJ);
1010 AssertRelease(idCpu == RTMpCpuId());
1011
1012 if (pCpu->hMemObj == NIL_RTR0MEMOBJ)
1013 return pCpu->fConfigured ? VERR_NO_MEMORY : VINF_SUCCESS /* not initialized. */;
1014
1015 int rc;
1016 if (pCpu->fConfigured)
1017 {
1018 void *pvCpuPage = RTR0MemObjAddress(pCpu->hMemObj);
1019 RTHCPHYS HCPhysCpuPage = RTR0MemObjGetPagePhysAddr(pCpu->hMemObj, 0);
1020
1021 rc = g_HmR0.pfnDisableCpu(pCpu, pvCpuPage, HCPhysCpuPage);
1022 AssertRCReturn(rc, rc);
1023
1024 pCpu->fConfigured = false;
1025 pCpu->idCpu = NIL_RTCPUID;
1026 }
1027 else
1028 rc = VINF_SUCCESS; /* nothing to do */
1029 return rc;
1030}
1031
1032
1033/**
1034 * Worker function passed to RTMpOnAll() that is to be called on the target
1035 * CPUs.
1036 *
1037 * @param idCpu The identifier for the CPU the function is called on.
1038 * @param pvUser1 The 1st user argument.
1039 * @param pvUser2 Opaque pointer to the FirstRc.
1040 */
1041static DECLCALLBACK(void) hmR0DisableCpuCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
1042{
1043 PHMR0FIRSTRC pFirstRc = (PHMR0FIRSTRC)pvUser2; NOREF(pvUser1);
1044 AssertReturnVoid(g_HmR0.fGlobalInit);
1045 hmR0FirstRcSetStatus(pFirstRc, hmR0DisableCpu(idCpu));
1046}
1047
1048
1049/**
1050 * Worker function passed to RTMpOnSpecific() that is to be called on the target
1051 * CPU.
1052 *
1053 * @param idCpu The identifier for the CPU the function is called on.
1054 * @param pvUser1 Null, not used.
1055 * @param pvUser2 Null, not used.
1056 */
1057static DECLCALLBACK(void) hmR0DisableCpuOnSpecificCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
1058{
1059 NOREF(pvUser1);
1060 NOREF(pvUser2);
1061 hmR0DisableCpu(idCpu);
1062}
1063
1064
1065/**
1066 * Callback function invoked when a cpu goes online or offline.
1067 *
1068 * @param enmEvent The Mp event.
1069 * @param idCpu The identifier for the CPU the function is called on.
1070 * @param pvData Opaque data (PVM pointer).
1071 */
1072static DECLCALLBACK(void) hmR0MpEventCallback(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvData)
1073{
1074 NOREF(pvData);
1075 Assert(!g_HmR0.vmx.fSupported || !g_HmR0.vmx.fUsingSUPR0EnableVTx);
1076
1077 /*
1078 * We only care about uninitializing a CPU that is going offline. When a
1079 * CPU comes online, the initialization is done lazily in HMR0Enter().
1080 */
1081 switch (enmEvent)
1082 {
1083 case RTMPEVENT_OFFLINE:
1084 {
1085 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
1086 RTThreadPreemptDisable(&PreemptState);
1087 if (idCpu == RTMpCpuId())
1088 {
1089 int rc = hmR0DisableCpu(idCpu);
1090 AssertRC(rc);
1091 RTThreadPreemptRestore(&PreemptState);
1092 }
1093 else
1094 {
1095 RTThreadPreemptRestore(&PreemptState);
1096 RTMpOnSpecific(idCpu, hmR0DisableCpuOnSpecificCallback, NULL /* pvUser1 */, NULL /* pvUser2 */);
1097 }
1098 break;
1099 }
1100
1101 default:
1102 break;
1103 }
1104}
1105
1106
1107/**
1108 * Called whenever a system power state change occurs.
1109 *
1110 * @param enmEvent The Power event.
1111 * @param pvUser User argument.
1112 */
1113static DECLCALLBACK(void) hmR0PowerCallback(RTPOWEREVENT enmEvent, void *pvUser)
1114{
1115 NOREF(pvUser);
1116 Assert(!g_HmR0.vmx.fSupported || !g_HmR0.vmx.fUsingSUPR0EnableVTx);
1117
1118#ifdef LOG_ENABLED
1119 if (enmEvent == RTPOWEREVENT_SUSPEND)
1120 SUPR0Printf("hmR0PowerCallback RTPOWEREVENT_SUSPEND\n");
1121 else
1122 SUPR0Printf("hmR0PowerCallback RTPOWEREVENT_RESUME\n");
1123#endif
1124
1125 if (enmEvent == RTPOWEREVENT_SUSPEND)
1126 ASMAtomicWriteBool(&g_HmR0.fSuspended, true);
1127
1128 if (g_HmR0.fEnabled)
1129 {
1130 int rc;
1131 HMR0FIRSTRC FirstRc;
1132 hmR0FirstRcInit(&FirstRc);
1133
1134 if (enmEvent == RTPOWEREVENT_SUSPEND)
1135 {
1136 if (g_HmR0.fGlobalInit)
1137 {
1138 /* Turn off VT-x or AMD-V on all CPUs. */
1139 rc = RTMpOnAll(hmR0DisableCpuCallback, NULL /* pvUser 1 */, &FirstRc);
1140 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
1141 }
1142 /* else nothing to do here for the local init case */
1143 }
1144 else
1145 {
1146 /* Reinit the CPUs from scratch as the suspend state might have
1147 messed with the MSRs. (lousy BIOSes as usual) */
1148 if (g_HmR0.vmx.fSupported)
1149 rc = RTMpOnAll(hmR0InitIntelCpu, &FirstRc, NULL);
1150 else
1151 rc = RTMpOnAll(hmR0InitAmdCpu, &FirstRc, NULL);
1152 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
1153 if (RT_SUCCESS(rc))
1154 rc = hmR0FirstRcGetStatus(&FirstRc);
1155#ifdef LOG_ENABLED
1156 if (RT_FAILURE(rc))
1157 SUPR0Printf("hmR0PowerCallback hmR0InitXxxCpu failed with %Rc\n", rc);
1158#endif
1159 if (g_HmR0.fGlobalInit)
1160 {
1161 /* Turn VT-x or AMD-V back on on all CPUs. */
1162 rc = RTMpOnAll(hmR0EnableCpuCallback, NULL /* pVM */, &FirstRc /* output ignored */);
1163 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
1164 }
1165 /* else nothing to do here for the local init case */
1166 }
1167 }
1168
1169 if (enmEvent == RTPOWEREVENT_RESUME)
1170 ASMAtomicWriteBool(&g_HmR0.fSuspended, false);
1171}
1172
1173
1174/**
1175 * Does ring-0 per-VM HM initialization.
1176 *
1177 * This will copy HM global into the VM structure and call the CPU specific
1178 * init routine which will allocate resources for each virtual CPU and such.
1179 *
1180 * @returns VBox status code.
1181 * @param pVM Pointer to the VM.
1182 *
1183 * @remarks This is called after HMR3Init(), see vmR3CreateU() and
1184 * vmR3InitRing3().
1185 */
1186VMMR0_INT_DECL(int) HMR0InitVM(PVM pVM)
1187{
1188 AssertReturn(pVM, VERR_INVALID_PARAMETER);
1189
1190#ifdef LOG_ENABLED
1191 SUPR0Printf("HMR0InitVM: %p\n", pVM);
1192#endif
1193
1194 /* Make sure we don't touch HM after we've disabled HM in preparation of a suspend. */
1195 if (ASMAtomicReadBool(&g_HmR0.fSuspended))
1196 return VERR_HM_SUSPEND_PENDING;
1197
1198 /*
1199 * Copy globals to the VM structure.
1200 */
1201 pVM->hm.s.vmx.fSupported = g_HmR0.vmx.fSupported;
1202 pVM->hm.s.svm.fSupported = g_HmR0.svm.fSupported;
1203
1204 pVM->hm.s.vmx.fUsePreemptTimer &= g_HmR0.vmx.fUsePreemptTimer; /* Can be overridden by CFGM. See HMR3Init(). */
1205 pVM->hm.s.vmx.cPreemptTimerShift = g_HmR0.vmx.cPreemptTimerShift;
1206 pVM->hm.s.vmx.u64HostCr4 = g_HmR0.vmx.u64HostCr4;
1207 pVM->hm.s.vmx.u64HostEfer = g_HmR0.vmx.u64HostEfer;
1208 pVM->hm.s.vmx.Msrs = g_HmR0.vmx.Msrs;
1209 pVM->hm.s.svm.u64MsrHwcr = g_HmR0.svm.u64MsrHwcr;
1210 pVM->hm.s.svm.u32Rev = g_HmR0.svm.u32Rev;
1211 pVM->hm.s.svm.u32Features = g_HmR0.svm.u32Features;
1212 pVM->hm.s.cpuid.u32AMDFeatureECX = g_HmR0.cpuid.u32AMDFeatureECX;
1213 pVM->hm.s.cpuid.u32AMDFeatureEDX = g_HmR0.cpuid.u32AMDFeatureEDX;
1214 pVM->hm.s.lLastError = g_HmR0.lLastError;
1215 pVM->hm.s.uMaxAsid = g_HmR0.uMaxAsid;
1216
1217 if (!pVM->hm.s.cMaxResumeLoops) /* allow ring-3 overrides */
1218 {
1219 pVM->hm.s.cMaxResumeLoops = 1024;
1220 if (RTThreadPreemptIsPendingTrusty())
1221 pVM->hm.s.cMaxResumeLoops = 8192;
1222 }
1223
1224 /*
1225 * Initialize some per-VCPU fields.
1226 */
1227 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1228 {
1229 PVMCPU pVCpu = &pVM->aCpus[i];
1230 pVCpu->hm.s.idEnteredCpu = NIL_RTCPUID;
1231 pVCpu->hm.s.idLastCpu = NIL_RTCPUID;
1232 pVCpu->hm.s.fGIMTrapXcptUD = GIMShouldTrapXcptUD(pVCpu);
1233
1234 /* We'll aways increment this the first time (host uses ASID 0). */
1235 AssertReturn(!pVCpu->hm.s.uCurrentAsid, VERR_HM_IPE_3);
1236 }
1237
1238 pVM->hm.s.uHostKernelFeatures = SUPR0GetKernelFeatures();
1239
1240 /*
1241 * Call the hardware specific initialization method.
1242 */
1243 return g_HmR0.pfnInitVM(pVM);
1244}
1245
1246
1247/**
1248 * Does ring-0 per VM HM termination.
1249 *
1250 * @returns VBox status code.
1251 * @param pVM Pointer to the VM.
1252 */
1253VMMR0_INT_DECL(int) HMR0TermVM(PVM pVM)
1254{
1255 Log(("HMR0TermVM: %p\n", pVM));
1256 AssertReturn(pVM, VERR_INVALID_PARAMETER);
1257
1258 /*
1259 * Call the hardware specific method.
1260 *
1261 * Note! We might be preparing for a suspend, so the pfnTermVM() functions should probably not
1262 * mess with VT-x/AMD-V features on the CPU, currently all they do is free memory so this is safe.
1263 */
1264 return g_HmR0.pfnTermVM(pVM);
1265}
1266
1267
1268/**
1269 * Sets up a VT-x or AMD-V session.
1270 *
1271 * This is mostly about setting up the hardware VM state.
1272 *
1273 * @returns VBox status code.
1274 * @param pVM Pointer to the VM.
1275 */
1276VMMR0_INT_DECL(int) HMR0SetupVM(PVM pVM)
1277{
1278 Log(("HMR0SetupVM: %p\n", pVM));
1279 AssertReturn(pVM, VERR_INVALID_PARAMETER);
1280
1281 /* Make sure we don't touch HM after we've disabled HM in preparation of a suspend. */
1282 AssertReturn(!ASMAtomicReadBool(&g_HmR0.fSuspended), VERR_HM_SUSPEND_PENDING);
1283
1284 /* On first entry we'll sync everything. */
1285 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1286 HMCPU_CF_RESET_TO(&pVM->aCpus[i], HM_CHANGED_HOST_CONTEXT | HM_CHANGED_ALL_GUEST);
1287
1288 /*
1289 * Call the hardware specific setup VM method. This requires the CPU to be
1290 * enabled for AMD-V/VT-x and preemption to be prevented.
1291 */
1292 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
1293 RTThreadPreemptDisable(&PreemptState);
1294 RTCPUID idCpu = RTMpCpuId();
1295
1296 /* Enable VT-x or AMD-V if local init is required. */
1297 int rc;
1298 if (!g_HmR0.fGlobalInit)
1299 {
1300 Assert(!g_HmR0.vmx.fSupported || !g_HmR0.vmx.fUsingSUPR0EnableVTx);
1301 rc = hmR0EnableCpu(pVM, idCpu);
1302 if (RT_FAILURE(rc))
1303 {
1304 RTThreadPreemptRestore(&PreemptState);
1305 return rc;
1306 }
1307 }
1308
1309 /* Setup VT-x or AMD-V. */
1310 rc = g_HmR0.pfnSetupVM(pVM);
1311
1312 /* Disable VT-x or AMD-V if local init was done before. */
1313 if (!g_HmR0.fGlobalInit)
1314 {
1315 Assert(!g_HmR0.vmx.fSupported || !g_HmR0.vmx.fUsingSUPR0EnableVTx);
1316 int rc2 = hmR0DisableCpu(idCpu);
1317 AssertRC(rc2);
1318 }
1319
1320 RTThreadPreemptRestore(&PreemptState);
1321 return rc;
1322}
1323
1324
1325/**
1326 * Turns on HM on the CPU if necessary and initializes the bare minimum state
1327 * required for entering HM context.
1328 *
1329 * @returns VBox status code.
1330 * @param pvCpu Pointer to the VMCPU.
1331 *
1332 * @remarks No-long-jump zone!!!
1333 */
1334VMMR0_INT_DECL(int) HMR0EnterCpu(PVMCPU pVCpu)
1335{
1336 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
1337
1338 int rc = VINF_SUCCESS;
1339 RTCPUID idCpu = RTMpCpuId();
1340 PHMGLOBALCPUINFO pCpu = &g_HmR0.aCpuInfo[idCpu];
1341 AssertPtr(pCpu);
1342
1343 /* Enable VT-x or AMD-V if local init is required, or enable if it's a freshly onlined CPU. */
1344 if (!pCpu->fConfigured)
1345 rc = hmR0EnableCpu(pVCpu->CTX_SUFF(pVM), idCpu);
1346
1347 /* Reload host-state (back from ring-3/migrated CPUs) and shared guest/host bits. */
1348 HMCPU_CF_SET(pVCpu, HM_CHANGED_HOST_CONTEXT | HM_CHANGED_HOST_GUEST_SHARED_STATE);
1349
1350 Assert(pCpu->idCpu == idCpu && pCpu->idCpu != NIL_RTCPUID);
1351 pVCpu->hm.s.idEnteredCpu = idCpu;
1352 return rc;
1353}
1354
1355
1356/**
1357 * Enters the VT-x or AMD-V session.
1358 *
1359 * @returns VBox status code.
1360 * @param pVM Pointer to the VM.
1361 * @param pVCpu Pointer to the VMCPU.
1362 *
1363 * @remarks This is called with preemption disabled.
1364 */
1365VMMR0_INT_DECL(int) HMR0Enter(PVM pVM, PVMCPU pVCpu)
1366{
1367 /* Make sure we can't enter a session after we've disabled HM in preparation of a suspend. */
1368 AssertReturn(!ASMAtomicReadBool(&g_HmR0.fSuspended), VERR_HM_SUSPEND_PENDING);
1369 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
1370
1371 /* Load the bare minimum state required for entering HM. */
1372 int rc = HMR0EnterCpu(pVCpu);
1373 AssertRCReturn(rc, rc);
1374
1375#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1376 AssertReturn(!VMMR0ThreadCtxHookIsEnabled(pVCpu), VERR_HM_IPE_5);
1377 bool fStartedSet = PGMR0DynMapStartOrMigrateAutoSet(pVCpu);
1378#endif
1379
1380 RTCPUID idCpu = RTMpCpuId();
1381 PHMGLOBALCPUINFO pCpu = &g_HmR0.aCpuInfo[idCpu];
1382 Assert(pCpu);
1383 Assert(HMCPU_CF_IS_SET(pVCpu, HM_CHANGED_HOST_CONTEXT | HM_CHANGED_HOST_GUEST_SHARED_STATE));
1384
1385 rc = g_HmR0.pfnEnterSession(pVM, pVCpu, pCpu);
1386 AssertMsgRCReturn(rc, ("pfnEnterSession failed. rc=%Rrc pVCpu=%p HostCpuId=%u\n", rc, pVCpu, idCpu), rc);
1387
1388 /* Load the host-state as we may be resuming code after a longjmp and quite
1389 possibly now be scheduled on a different CPU. */
1390 rc = g_HmR0.pfnSaveHostState(pVM, pVCpu);
1391 AssertMsgRCReturn(rc, ("pfnSaveHostState failed. rc=%Rrc pVCpu=%p HostCpuId=%u\n", rc, pVCpu, idCpu), rc);
1392
1393#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1394 if (fStartedSet)
1395 PGMRZDynMapReleaseAutoSet(pVCpu);
1396#endif
1397
1398 /* Keep track of the CPU owning the VMCS for debugging scheduling weirdness and ring-3 calls. */
1399 if (RT_FAILURE(rc))
1400 pVCpu->hm.s.idEnteredCpu = NIL_RTCPUID;
1401 return rc;
1402}
1403
1404
1405/**
1406 * Deinitializes the bare minimum state used for HM context and if necessary
1407 * disable HM on the CPU.
1408 *
1409 * @returns VBox status code.
1410 * @param pVCpu Pointer to the VMCPU.
1411 *
1412 * @remarks No-long-jump zone!!!
1413 */
1414VMMR0_INT_DECL(int) HMR0LeaveCpu(PVMCPU pVCpu)
1415{
1416 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
1417 VMCPU_ASSERT_EMT_RETURN(pVCpu, VERR_HM_WRONG_CPU);
1418
1419 RTCPUID idCpu = RTMpCpuId();
1420 PHMGLOBALCPUINFO pCpu = &g_HmR0.aCpuInfo[idCpu];
1421
1422 if ( !g_HmR0.fGlobalInit
1423 && pCpu->fConfigured)
1424 {
1425 int rc = hmR0DisableCpu(idCpu);
1426 AssertRCReturn(rc, rc);
1427 Assert(!pCpu->fConfigured);
1428 Assert(pCpu->idCpu == NIL_RTCPUID);
1429
1430 /* For obtaining a non-zero ASID/VPID on next re-entry. */
1431 pVCpu->hm.s.idLastCpu = NIL_RTCPUID;
1432 }
1433
1434 /* Clear it while leaving HM context, hmPokeCpuForTlbFlush() relies on this. */
1435 pVCpu->hm.s.idEnteredCpu = NIL_RTCPUID;
1436
1437 return VINF_SUCCESS;
1438}
1439
1440
1441/**
1442 * Thread-context hook for HM.
1443 *
1444 * @param enmEvent The thread-context event.
1445 * @param pvUser Opaque pointer to the VMCPU.
1446 */
1447VMMR0_INT_DECL(void) HMR0ThreadCtxCallback(RTTHREADCTXEVENT enmEvent, void *pvUser)
1448{
1449 PVMCPU pVCpu = (PVMCPU)pvUser;
1450 Assert(pVCpu);
1451 Assert(g_HmR0.pfnThreadCtxCallback);
1452
1453 g_HmR0.pfnThreadCtxCallback(enmEvent, pVCpu, g_HmR0.fGlobalInit);
1454}
1455
1456
1457/**
1458 * Runs guest code in a hardware accelerated VM.
1459 *
1460 * @returns VBox status code.
1461 * @param pVM Pointer to the VM.
1462 * @param pVCpu Pointer to the VMCPU.
1463 *
1464 * @remarks Can be called with preemption enabled if thread-context hooks are
1465 * used!!!
1466 */
1467VMMR0_INT_DECL(int) HMR0RunGuestCode(PVM pVM, PVMCPU pVCpu)
1468{
1469#ifdef VBOX_STRICT
1470 /* With thread-context hooks we would be running this code with preemption enabled. */
1471 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
1472 {
1473 PHMGLOBALCPUINFO pCpu = &g_HmR0.aCpuInfo[RTMpCpuId()];
1474 Assert(!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL));
1475 Assert(pCpu->fConfigured);
1476 AssertReturn(!ASMAtomicReadBool(&g_HmR0.fSuspended), VERR_HM_SUSPEND_PENDING);
1477 }
1478#endif
1479
1480#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1481 AssertReturn(!VMMR0ThreadCtxHookIsEnabled(pVCpu), VERR_HM_IPE_4);
1482 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
1483 PGMRZDynMapStartAutoSet(pVCpu);
1484#endif
1485
1486 int rc = g_HmR0.pfnRunGuestCode(pVM, pVCpu, CPUMQueryGuestCtxPtr(pVCpu));
1487
1488#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1489 PGMRZDynMapReleaseAutoSet(pVCpu);
1490#endif
1491 return rc;
1492}
1493
1494#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1495
1496/**
1497 * Save guest FPU/XMM state (64 bits guest mode & 32 bits host only)
1498 *
1499 * @returns VBox status code.
1500 * @param pVM Pointer to the VM.
1501 * @param pVCpu Pointer to the VMCPU.
1502 * @param pCtx Pointer to the guest CPU context.
1503 */
1504VMMR0_INT_DECL(int) HMR0SaveFPUState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1505{
1506 STAM_COUNTER_INC(&pVCpu->hm.s.StatFpu64SwitchBack);
1507 if (pVM->hm.s.vmx.fSupported)
1508 return VMXR0Execute64BitsHandler(pVM, pVCpu, pCtx, HM64ON32OP_HMRCSaveGuestFPU64, 0, NULL);
1509 return SVMR0Execute64BitsHandler(pVM, pVCpu, pCtx, HM64ON32OP_HMRCSaveGuestFPU64, 0, NULL);
1510}
1511
1512
1513/**
1514 * Save guest debug state (64 bits guest mode & 32 bits host only)
1515 *
1516 * @returns VBox status code.
1517 * @param pVM Pointer to the VM.
1518 * @param pVCpu Pointer to the VMCPU.
1519 * @param pCtx Pointer to the guest CPU context.
1520 */
1521VMMR0_INT_DECL(int) HMR0SaveDebugState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1522{
1523 STAM_COUNTER_INC(&pVCpu->hm.s.StatDebug64SwitchBack);
1524 if (pVM->hm.s.vmx.fSupported)
1525 return VMXR0Execute64BitsHandler(pVM, pVCpu, pCtx, HM64ON32OP_HMRCSaveGuestDebug64, 0, NULL);
1526 return SVMR0Execute64BitsHandler(pVM, pVCpu, pCtx, HM64ON32OP_HMRCSaveGuestDebug64, 0, NULL);
1527}
1528
1529
1530/**
1531 * Test the 32->64 bits switcher.
1532 *
1533 * @returns VBox status code.
1534 * @param pVM Pointer to the VM.
1535 */
1536VMMR0_INT_DECL(int) HMR0TestSwitcher3264(PVM pVM)
1537{
1538 PVMCPU pVCpu = &pVM->aCpus[0];
1539 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1540 uint32_t aParam[5] = {0, 1, 2, 3, 4};
1541 int rc;
1542
1543 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatWorldSwitch3264, z);
1544 if (pVM->hm.s.vmx.fSupported)
1545 rc = VMXR0Execute64BitsHandler(pVM, pVCpu, pCtx, HM64ON32OP_HMRCTestSwitcher64, 5, &aParam[0]);
1546 else
1547 rc = SVMR0Execute64BitsHandler(pVM, pVCpu, pCtx, HM64ON32OP_HMRCTestSwitcher64, 5, &aParam[0]);
1548 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatWorldSwitch3264, z);
1549
1550 return rc;
1551}
1552
1553#endif /* HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL) */
1554
1555/**
1556 * Returns suspend status of the host.
1557 *
1558 * @returns Suspend pending or not.
1559 */
1560VMMR0_INT_DECL(bool) HMR0SuspendPending(void)
1561{
1562 return ASMAtomicReadBool(&g_HmR0.fSuspended);
1563}
1564
1565
1566/**
1567 * Returns the cpu structure for the current cpu.
1568 * Keep in mind that there is no guarantee it will stay the same (long jumps to ring 3!!!).
1569 *
1570 * @returns The cpu structure pointer.
1571 */
1572VMMR0DECL(PHMGLOBALCPUINFO) HMR0GetCurrentCpu(void)
1573{
1574 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
1575 RTCPUID idCpu = RTMpCpuId();
1576 Assert(idCpu < RT_ELEMENTS(g_HmR0.aCpuInfo));
1577 return &g_HmR0.aCpuInfo[idCpu];
1578}
1579
1580
1581/**
1582 * Returns the cpu structure for the current cpu.
1583 * Keep in mind that there is no guarantee it will stay the same (long jumps to ring 3!!!).
1584 *
1585 * @returns The cpu structure pointer.
1586 * @param idCpu id of the VCPU.
1587 */
1588VMMR0DECL(PHMGLOBALCPUINFO) HMR0GetCurrentCpuEx(RTCPUID idCpu)
1589{
1590 Assert(idCpu < RT_ELEMENTS(g_HmR0.aCpuInfo));
1591 return &g_HmR0.aCpuInfo[idCpu];
1592}
1593
1594
1595/**
1596 * Save a pending IO read.
1597 *
1598 * @param pVCpu Pointer to the VMCPU.
1599 * @param GCPtrRip Address of IO instruction.
1600 * @param GCPtrRipNext Address of the next instruction.
1601 * @param uPort Port address.
1602 * @param uAndVal AND mask for saving the result in eax.
1603 * @param cbSize Read size.
1604 */
1605VMMR0_INT_DECL(void) HMR0SavePendingIOPortRead(PVMCPU pVCpu, RTGCPTR GCPtrRip, RTGCPTR GCPtrRipNext,
1606 unsigned uPort, unsigned uAndVal, unsigned cbSize)
1607{
1608 pVCpu->hm.s.PendingIO.enmType = HMPENDINGIO_PORT_READ;
1609 pVCpu->hm.s.PendingIO.GCPtrRip = GCPtrRip;
1610 pVCpu->hm.s.PendingIO.GCPtrRipNext = GCPtrRipNext;
1611 pVCpu->hm.s.PendingIO.s.Port.uPort = uPort;
1612 pVCpu->hm.s.PendingIO.s.Port.uAndVal = uAndVal;
1613 pVCpu->hm.s.PendingIO.s.Port.cbSize = cbSize;
1614 return;
1615}
1616
1617
1618/**
1619 * Save a pending IO write.
1620 *
1621 * @param pVCpu Pointer to the VMCPU.
1622 * @param GCPtrRIP Address of IO instruction.
1623 * @param uPort Port address.
1624 * @param uAndVal AND mask for fetching the result from eax.
1625 * @param cbSize Read size.
1626 */
1627VMMR0_INT_DECL(void) HMR0SavePendingIOPortWrite(PVMCPU pVCpu, RTGCPTR GCPtrRip, RTGCPTR GCPtrRipNext,
1628 unsigned uPort, unsigned uAndVal, unsigned cbSize)
1629{
1630 pVCpu->hm.s.PendingIO.enmType = HMPENDINGIO_PORT_WRITE;
1631 pVCpu->hm.s.PendingIO.GCPtrRip = GCPtrRip;
1632 pVCpu->hm.s.PendingIO.GCPtrRipNext = GCPtrRipNext;
1633 pVCpu->hm.s.PendingIO.s.Port.uPort = uPort;
1634 pVCpu->hm.s.PendingIO.s.Port.uAndVal = uAndVal;
1635 pVCpu->hm.s.PendingIO.s.Port.cbSize = cbSize;
1636 return;
1637}
1638
1639#ifdef VBOX_WITH_RAW_MODE
1640
1641/**
1642 * Raw-mode switcher hook - disable VT-x if it's active *and* the current
1643 * switcher turns off paging.
1644 *
1645 * @returns VBox status code.
1646 * @param pVM Pointer to the VM.
1647 * @param enmSwitcher The switcher we're about to use.
1648 * @param pfVTxDisabled Where to store whether VT-x was disabled or not.
1649 */
1650VMMR0_INT_DECL(int) HMR0EnterSwitcher(PVM pVM, VMMSWITCHER enmSwitcher, bool *pfVTxDisabled)
1651{
1652 NOREF(pVM);
1653
1654 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
1655
1656 *pfVTxDisabled = false;
1657
1658 /* No such issues with AMD-V */
1659 if (!g_HmR0.vmx.fSupported)
1660 return VINF_SUCCESS;
1661
1662 /* Check if the switching we're up to is safe. */
1663 switch (enmSwitcher)
1664 {
1665 case VMMSWITCHER_32_TO_32:
1666 case VMMSWITCHER_PAE_TO_PAE:
1667 return VINF_SUCCESS; /* safe switchers as they don't turn off paging */
1668
1669 case VMMSWITCHER_32_TO_PAE:
1670 case VMMSWITCHER_PAE_TO_32: /* is this one actually used?? */
1671 case VMMSWITCHER_AMD64_TO_32:
1672 case VMMSWITCHER_AMD64_TO_PAE:
1673 break; /* unsafe switchers */
1674
1675 default:
1676 AssertFailedReturn(VERR_HM_WRONG_SWITCHER);
1677 }
1678
1679 /* When using SUPR0EnableVTx we must let the host suspend and resume VT-x,
1680 regardless of whether we're currently using VT-x or not. */
1681 if (g_HmR0.vmx.fUsingSUPR0EnableVTx)
1682 {
1683 *pfVTxDisabled = SUPR0SuspendVTxOnCpu();
1684 return VINF_SUCCESS;
1685 }
1686
1687 /** @todo Check if this code is presumptive wrt other VT-x users on the
1688 * system... */
1689
1690 /* Nothing to do if we haven't enabled VT-x. */
1691 if (!g_HmR0.fEnabled)
1692 return VINF_SUCCESS;
1693
1694 /* Local init implies the CPU is currently not in VMX root mode. */
1695 if (!g_HmR0.fGlobalInit)
1696 return VINF_SUCCESS;
1697
1698 /* Ok, disable VT-x. */
1699 PHMGLOBALCPUINFO pCpu = HMR0GetCurrentCpu();
1700 AssertReturn(pCpu && pCpu->hMemObj != NIL_RTR0MEMOBJ, VERR_HM_IPE_2);
1701
1702 *pfVTxDisabled = true;
1703 void *pvCpuPage = RTR0MemObjAddress(pCpu->hMemObj);
1704 RTHCPHYS HCPhysCpuPage = RTR0MemObjGetPagePhysAddr(pCpu->hMemObj, 0);
1705 return VMXR0DisableCpu(pCpu, pvCpuPage, HCPhysCpuPage);
1706}
1707
1708
1709/**
1710 * Raw-mode switcher hook - re-enable VT-x if was active *and* the current
1711 * switcher turned off paging.
1712 *
1713 * @param pVM Pointer to the VM.
1714 * @param fVTxDisabled Whether VT-x was disabled or not.
1715 */
1716VMMR0_INT_DECL(void) HMR0LeaveSwitcher(PVM pVM, bool fVTxDisabled)
1717{
1718 Assert(!ASMIntAreEnabled());
1719
1720 if (!fVTxDisabled)
1721 return; /* nothing to do */
1722
1723 Assert(g_HmR0.vmx.fSupported);
1724 if (g_HmR0.vmx.fUsingSUPR0EnableVTx)
1725 SUPR0ResumeVTxOnCpu(fVTxDisabled);
1726 else
1727 {
1728 Assert(g_HmR0.fEnabled);
1729 Assert(g_HmR0.fGlobalInit);
1730
1731 PHMGLOBALCPUINFO pCpu = HMR0GetCurrentCpu();
1732 AssertReturnVoid(pCpu && pCpu->hMemObj != NIL_RTR0MEMOBJ);
1733
1734 void *pvCpuPage = RTR0MemObjAddress(pCpu->hMemObj);
1735 RTHCPHYS HCPhysCpuPage = RTR0MemObjGetPagePhysAddr(pCpu->hMemObj, 0);
1736 VMXR0EnableCpu(pCpu, pVM, pvCpuPage, HCPhysCpuPage, false, &g_HmR0.vmx.Msrs);
1737 }
1738}
1739
1740#endif /* VBOX_WITH_RAW_MODE */
1741#ifdef VBOX_STRICT
1742
1743/**
1744 * Dumps a descriptor.
1745 *
1746 * @param pDesc Descriptor to dump.
1747 * @param Sel Selector number.
1748 * @param pszMsg Message to prepend the log entry with.
1749 */
1750VMMR0DECL(void) HMR0DumpDescriptor(PCX86DESCHC pDesc, RTSEL Sel, const char *pszMsg)
1751{
1752 /*
1753 * Make variable description string.
1754 */
1755 static struct
1756 {
1757 unsigned cch;
1758 const char *psz;
1759 } const s_aTypes[32] =
1760 {
1761# define STRENTRY(str) { sizeof(str) - 1, str }
1762
1763 /* system */
1764# if HC_ARCH_BITS == 64
1765 STRENTRY("Reserved0 "), /* 0x00 */
1766 STRENTRY("Reserved1 "), /* 0x01 */
1767 STRENTRY("LDT "), /* 0x02 */
1768 STRENTRY("Reserved3 "), /* 0x03 */
1769 STRENTRY("Reserved4 "), /* 0x04 */
1770 STRENTRY("Reserved5 "), /* 0x05 */
1771 STRENTRY("Reserved6 "), /* 0x06 */
1772 STRENTRY("Reserved7 "), /* 0x07 */
1773 STRENTRY("Reserved8 "), /* 0x08 */
1774 STRENTRY("TSS64Avail "), /* 0x09 */
1775 STRENTRY("ReservedA "), /* 0x0a */
1776 STRENTRY("TSS64Busy "), /* 0x0b */
1777 STRENTRY("Call64 "), /* 0x0c */
1778 STRENTRY("ReservedD "), /* 0x0d */
1779 STRENTRY("Int64 "), /* 0x0e */
1780 STRENTRY("Trap64 "), /* 0x0f */
1781# else
1782 STRENTRY("Reserved0 "), /* 0x00 */
1783 STRENTRY("TSS16Avail "), /* 0x01 */
1784 STRENTRY("LDT "), /* 0x02 */
1785 STRENTRY("TSS16Busy "), /* 0x03 */
1786 STRENTRY("Call16 "), /* 0x04 */
1787 STRENTRY("Task "), /* 0x05 */
1788 STRENTRY("Int16 "), /* 0x06 */
1789 STRENTRY("Trap16 "), /* 0x07 */
1790 STRENTRY("Reserved8 "), /* 0x08 */
1791 STRENTRY("TSS32Avail "), /* 0x09 */
1792 STRENTRY("ReservedA "), /* 0x0a */
1793 STRENTRY("TSS32Busy "), /* 0x0b */
1794 STRENTRY("Call32 "), /* 0x0c */
1795 STRENTRY("ReservedD "), /* 0x0d */
1796 STRENTRY("Int32 "), /* 0x0e */
1797 STRENTRY("Trap32 "), /* 0x0f */
1798# endif
1799 /* non system */
1800 STRENTRY("DataRO "), /* 0x10 */
1801 STRENTRY("DataRO Accessed "), /* 0x11 */
1802 STRENTRY("DataRW "), /* 0x12 */
1803 STRENTRY("DataRW Accessed "), /* 0x13 */
1804 STRENTRY("DataDownRO "), /* 0x14 */
1805 STRENTRY("DataDownRO Accessed "), /* 0x15 */
1806 STRENTRY("DataDownRW "), /* 0x16 */
1807 STRENTRY("DataDownRW Accessed "), /* 0x17 */
1808 STRENTRY("CodeEO "), /* 0x18 */
1809 STRENTRY("CodeEO Accessed "), /* 0x19 */
1810 STRENTRY("CodeER "), /* 0x1a */
1811 STRENTRY("CodeER Accessed "), /* 0x1b */
1812 STRENTRY("CodeConfEO "), /* 0x1c */
1813 STRENTRY("CodeConfEO Accessed "), /* 0x1d */
1814 STRENTRY("CodeConfER "), /* 0x1e */
1815 STRENTRY("CodeConfER Accessed ") /* 0x1f */
1816# undef SYSENTRY
1817 };
1818# define ADD_STR(psz, pszAdd) do { strcpy(psz, pszAdd); psz += strlen(pszAdd); } while (0)
1819 char szMsg[128];
1820 char *psz = &szMsg[0];
1821 unsigned i = pDesc->Gen.u1DescType << 4 | pDesc->Gen.u4Type;
1822 memcpy(psz, s_aTypes[i].psz, s_aTypes[i].cch);
1823 psz += s_aTypes[i].cch;
1824
1825 if (pDesc->Gen.u1Present)
1826 ADD_STR(psz, "Present ");
1827 else
1828 ADD_STR(psz, "Not-Present ");
1829# if HC_ARCH_BITS == 64
1830 if (pDesc->Gen.u1Long)
1831 ADD_STR(psz, "64-bit ");
1832 else
1833 ADD_STR(psz, "Comp ");
1834# else
1835 if (pDesc->Gen.u1Granularity)
1836 ADD_STR(psz, "Page ");
1837 if (pDesc->Gen.u1DefBig)
1838 ADD_STR(psz, "32-bit ");
1839 else
1840 ADD_STR(psz, "16-bit ");
1841# endif
1842# undef ADD_STR
1843 *psz = '\0';
1844
1845 /*
1846 * Limit and Base and format the output.
1847 */
1848 uint32_t u32Limit = X86DESC_LIMIT_G(pDesc);
1849
1850# if HC_ARCH_BITS == 64
1851 uint64_t u32Base = X86DESC64_BASE(pDesc);
1852 Log(("%s %04x - %RX64 %RX64 - base=%RX64 limit=%08x dpl=%d %s\n", pszMsg,
1853 Sel, pDesc->au64[0], pDesc->au64[1], u32Base, u32Limit, pDesc->Gen.u2Dpl, szMsg));
1854# else
1855 uint32_t u32Base = X86DESC_BASE(pDesc);
1856 Log(("%s %04x - %08x %08x - base=%08x limit=%08x dpl=%d %s\n", pszMsg,
1857 Sel, pDesc->au32[0], pDesc->au32[1], u32Base, u32Limit, pDesc->Gen.u2Dpl, szMsg));
1858# endif
1859}
1860
1861
1862/**
1863 * Formats a full register dump.
1864 *
1865 * @param pVM Pointer to the VM.
1866 * @param pVCpu Pointer to the VMCPU.
1867 * @param pCtx Pointer to the CPU context.
1868 */
1869VMMR0DECL(void) HMDumpRegs(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
1870{
1871 NOREF(pVM);
1872
1873 /*
1874 * Format the flags.
1875 */
1876 static struct
1877 {
1878 const char *pszSet; const char *pszClear; uint32_t fFlag;
1879 } const s_aFlags[] =
1880 {
1881 { "vip", NULL, X86_EFL_VIP },
1882 { "vif", NULL, X86_EFL_VIF },
1883 { "ac", NULL, X86_EFL_AC },
1884 { "vm", NULL, X86_EFL_VM },
1885 { "rf", NULL, X86_EFL_RF },
1886 { "nt", NULL, X86_EFL_NT },
1887 { "ov", "nv", X86_EFL_OF },
1888 { "dn", "up", X86_EFL_DF },
1889 { "ei", "di", X86_EFL_IF },
1890 { "tf", NULL, X86_EFL_TF },
1891 { "nt", "pl", X86_EFL_SF },
1892 { "nz", "zr", X86_EFL_ZF },
1893 { "ac", "na", X86_EFL_AF },
1894 { "po", "pe", X86_EFL_PF },
1895 { "cy", "nc", X86_EFL_CF },
1896 };
1897 char szEFlags[80];
1898 char *psz = szEFlags;
1899 uint32_t uEFlags = pCtx->eflags.u32;
1900 for (unsigned i = 0; i < RT_ELEMENTS(s_aFlags); i++)
1901 {
1902 const char *pszAdd = s_aFlags[i].fFlag & uEFlags ? s_aFlags[i].pszSet : s_aFlags[i].pszClear;
1903 if (pszAdd)
1904 {
1905 strcpy(psz, pszAdd);
1906 psz += strlen(pszAdd);
1907 *psz++ = ' ';
1908 }
1909 }
1910 psz[-1] = '\0';
1911
1912
1913 /*
1914 * Format the registers.
1915 */
1916 if (CPUMIsGuestIn64BitCode(pVCpu))
1917 {
1918 Log(("rax=%016RX64 rbx=%016RX64 rcx=%016RX64 rdx=%016RX64\n"
1919 "rsi=%016RX64 rdi=%016RX64 r8 =%016RX64 r9 =%016RX64\n"
1920 "r10=%016RX64 r11=%016RX64 r12=%016RX64 r13=%016RX64\n"
1921 "r14=%016RX64 r15=%016RX64\n"
1922 "rip=%016RX64 rsp=%016RX64 rbp=%016RX64 iopl=%d %*s\n"
1923 "cs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1924 "ds={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1925 "es={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1926 "fs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1927 "gs={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1928 "ss={%04x base=%016RX64 limit=%08x flags=%08x}\n"
1929 "cr0=%016RX64 cr2=%016RX64 cr3=%016RX64 cr4=%016RX64\n"
1930 "dr0=%016RX64 dr1=%016RX64 dr2=%016RX64 dr3=%016RX64\n"
1931 "dr4=%016RX64 dr5=%016RX64 dr6=%016RX64 dr7=%016RX64\n"
1932 "gdtr=%016RX64:%04x idtr=%016RX64:%04x eflags=%08x\n"
1933 "ldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1934 "tr ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1935 "SysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
1936 ,
1937 pCtx->rax, pCtx->rbx, pCtx->rcx, pCtx->rdx, pCtx->rsi, pCtx->rdi,
1938 pCtx->r8, pCtx->r9, pCtx->r10, pCtx->r11, pCtx->r12, pCtx->r13,
1939 pCtx->r14, pCtx->r15,
1940 pCtx->rip, pCtx->rsp, pCtx->rbp, X86_EFL_GET_IOPL(uEFlags), 31, szEFlags,
1941 pCtx->cs.Sel, pCtx->cs.u64Base, pCtx->cs.u32Limit, pCtx->cs.Attr.u,
1942 pCtx->ds.Sel, pCtx->ds.u64Base, pCtx->ds.u32Limit, pCtx->ds.Attr.u,
1943 pCtx->es.Sel, pCtx->es.u64Base, pCtx->es.u32Limit, pCtx->es.Attr.u,
1944 pCtx->fs.Sel, pCtx->fs.u64Base, pCtx->fs.u32Limit, pCtx->fs.Attr.u,
1945 pCtx->gs.Sel, pCtx->gs.u64Base, pCtx->gs.u32Limit, pCtx->gs.Attr.u,
1946 pCtx->ss.Sel, pCtx->ss.u64Base, pCtx->ss.u32Limit, pCtx->ss.Attr.u,
1947 pCtx->cr0, pCtx->cr2, pCtx->cr3, pCtx->cr4,
1948 pCtx->dr[0], pCtx->dr[1], pCtx->dr[2], pCtx->dr[3],
1949 pCtx->dr[4], pCtx->dr[5], pCtx->dr[6], pCtx->dr[7],
1950 pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, uEFlags,
1951 pCtx->ldtr.Sel, pCtx->ldtr.u64Base, pCtx->ldtr.u32Limit, pCtx->ldtr.Attr.u,
1952 pCtx->tr.Sel, pCtx->tr.u64Base, pCtx->tr.u32Limit, pCtx->tr.Attr.u,
1953 pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp));
1954 }
1955 else
1956 Log(("eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n"
1957 "eip=%08x esp=%08x ebp=%08x iopl=%d %*s\n"
1958 "cs={%04x base=%016RX64 limit=%08x flags=%08x} dr0=%08RX64 dr1=%08RX64\n"
1959 "ds={%04x base=%016RX64 limit=%08x flags=%08x} dr2=%08RX64 dr3=%08RX64\n"
1960 "es={%04x base=%016RX64 limit=%08x flags=%08x} dr4=%08RX64 dr5=%08RX64\n"
1961 "fs={%04x base=%016RX64 limit=%08x flags=%08x} dr6=%08RX64 dr7=%08RX64\n"
1962 "gs={%04x base=%016RX64 limit=%08x flags=%08x} cr0=%08RX64 cr2=%08RX64\n"
1963 "ss={%04x base=%016RX64 limit=%08x flags=%08x} cr3=%08RX64 cr4=%08RX64\n"
1964 "gdtr=%016RX64:%04x idtr=%016RX64:%04x eflags=%08x\n"
1965 "ldtr={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1966 "tr ={%04x base=%08RX64 limit=%08x flags=%08x}\n"
1967 "SysEnter={cs=%04llx eip=%08llx esp=%08llx}\n"
1968 ,
1969 pCtx->eax, pCtx->ebx, pCtx->ecx, pCtx->edx, pCtx->esi, pCtx->edi,
1970 pCtx->eip, pCtx->esp, pCtx->ebp, X86_EFL_GET_IOPL(uEFlags), 31, szEFlags,
1971 pCtx->cs.Sel, pCtx->cs.u64Base, pCtx->cs.u32Limit, pCtx->cs.Attr.u, pCtx->dr[0], pCtx->dr[1],
1972 pCtx->ds.Sel, pCtx->ds.u64Base, pCtx->ds.u32Limit, pCtx->ds.Attr.u, pCtx->dr[2], pCtx->dr[3],
1973 pCtx->es.Sel, pCtx->es.u64Base, pCtx->es.u32Limit, pCtx->es.Attr.u, pCtx->dr[4], pCtx->dr[5],
1974 pCtx->fs.Sel, pCtx->fs.u64Base, pCtx->fs.u32Limit, pCtx->fs.Attr.u, pCtx->dr[6], pCtx->dr[7],
1975 pCtx->gs.Sel, pCtx->gs.u64Base, pCtx->gs.u32Limit, pCtx->gs.Attr.u, pCtx->cr0, pCtx->cr2,
1976 pCtx->ss.Sel, pCtx->ss.u64Base, pCtx->ss.u32Limit, pCtx->ss.Attr.u, pCtx->cr3, pCtx->cr4,
1977 pCtx->gdtr.pGdt, pCtx->gdtr.cbGdt, pCtx->idtr.pIdt, pCtx->idtr.cbIdt, uEFlags,
1978 pCtx->ldtr.Sel, pCtx->ldtr.u64Base, pCtx->ldtr.u32Limit, pCtx->ldtr.Attr.u,
1979 pCtx->tr.Sel, pCtx->tr.u64Base, pCtx->tr.u32Limit, pCtx->tr.Attr.u,
1980 pCtx->SysEnter.cs, pCtx->SysEnter.eip, pCtx->SysEnter.esp));
1981
1982 PX86FXSTATE pFpuCtx = &pCtx->CTX_SUFF(pXState)->x87;
1983 Log(("FPU:\n"
1984 "FCW=%04x FSW=%04x FTW=%02x\n"
1985 "FOP=%04x FPUIP=%08x CS=%04x Rsrvd1=%04x\n"
1986 "FPUDP=%04x DS=%04x Rsvrd2=%04x MXCSR=%08x MXCSR_MASK=%08x\n"
1987 ,
1988 pFpuCtx->FCW, pFpuCtx->FSW, pFpuCtx->FTW,
1989 pFpuCtx->FOP, pFpuCtx->FPUIP, pFpuCtx->CS, pFpuCtx->Rsrvd1,
1990 pFpuCtx->FPUDP, pFpuCtx->DS, pFpuCtx->Rsrvd2,
1991 pFpuCtx->MXCSR, pFpuCtx->MXCSR_MASK));
1992
1993 Log(("MSR:\n"
1994 "EFER =%016RX64\n"
1995 "PAT =%016RX64\n"
1996 "STAR =%016RX64\n"
1997 "CSTAR =%016RX64\n"
1998 "LSTAR =%016RX64\n"
1999 "SFMASK =%016RX64\n"
2000 "KERNELGSBASE =%016RX64\n",
2001 pCtx->msrEFER,
2002 pCtx->msrPAT,
2003 pCtx->msrSTAR,
2004 pCtx->msrCSTAR,
2005 pCtx->msrLSTAR,
2006 pCtx->msrSFMASK,
2007 pCtx->msrKERNELGSBASE));
2008}
2009
2010#endif /* VBOX_STRICT */
2011
Note: See TracBrowser for help on using the repository browser.

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