VirtualBox

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

Last change on this file since 45947 was 45947, checked in by vboxsync, 12 years ago

VMM: HM cleanup for CTRL, CONTROLS in symbolic names.

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