VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/CPUMR0.cpp@ 85704

Last change on this file since 85704 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 35.7 KB
Line 
1/* $Id: CPUMR0.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * CPUM - Host Context Ring 0.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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_CPUM
23#include <VBox/vmm/cpum.h>
24#include "CPUMInternal.h"
25#include <VBox/vmm/vmcc.h>
26#include <VBox/vmm/gvm.h>
27#include <VBox/err.h>
28#include <VBox/log.h>
29#include <VBox/vmm/hm.h>
30#include <iprt/assert.h>
31#include <iprt/asm-amd64-x86.h>
32#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
33# include <iprt/mem.h>
34# include <iprt/memobj.h>
35# include <VBox/apic.h>
36#endif
37#include <iprt/x86.h>
38
39
40/*********************************************************************************************************************************
41* Structures and Typedefs *
42*********************************************************************************************************************************/
43#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
44/**
45 * Local APIC mappings.
46 */
47typedef struct CPUMHOSTLAPIC
48{
49 /** Indicates that the entry is in use and have valid data. */
50 bool fEnabled;
51 /** Whether it's operating in X2APIC mode (EXTD). */
52 bool fX2Apic;
53 /** The APIC version number. */
54 uint32_t uVersion;
55 /** The physical address of the APIC registers. */
56 RTHCPHYS PhysBase;
57 /** The memory object entering the physical address. */
58 RTR0MEMOBJ hMemObj;
59 /** The mapping object for hMemObj. */
60 RTR0MEMOBJ hMapObj;
61 /** The mapping address APIC registers.
62 * @remarks Different CPUs may use the same physical address to map their
63 * APICs, so this pointer is only valid when on the CPU owning the
64 * APIC. */
65 void *pv;
66} CPUMHOSTLAPIC;
67#endif
68
69
70/*********************************************************************************************************************************
71* Global Variables *
72*********************************************************************************************************************************/
73#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
74static CPUMHOSTLAPIC g_aLApics[RTCPUSET_MAX_CPUS];
75#endif
76
77/**
78 * CPUID bits to unify among all cores.
79 */
80static struct
81{
82 uint32_t uLeaf; /**< Leaf to check. */
83 uint32_t uEcx; /**< which bits in ecx to unify between CPUs. */
84 uint32_t uEdx; /**< which bits in edx to unify between CPUs. */
85}
86const g_aCpuidUnifyBits[] =
87{
88 {
89 0x00000001,
90 X86_CPUID_FEATURE_ECX_CX16 | X86_CPUID_FEATURE_ECX_MONITOR,
91 X86_CPUID_FEATURE_EDX_CX8
92 }
93};
94
95
96
97/*********************************************************************************************************************************
98* Internal Functions *
99*********************************************************************************************************************************/
100#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
101static int cpumR0MapLocalApics(void);
102static void cpumR0UnmapLocalApics(void);
103#endif
104static int cpumR0SaveHostDebugState(PVMCPUCC pVCpu);
105
106
107/**
108 * Does the Ring-0 CPU initialization once during module load.
109 * XXX Host-CPU hot-plugging?
110 */
111VMMR0_INT_DECL(int) CPUMR0ModuleInit(void)
112{
113 int rc = VINF_SUCCESS;
114#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
115 rc = cpumR0MapLocalApics();
116#endif
117 return rc;
118}
119
120
121/**
122 * Terminate the module.
123 */
124VMMR0_INT_DECL(int) CPUMR0ModuleTerm(void)
125{
126#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
127 cpumR0UnmapLocalApics();
128#endif
129 return VINF_SUCCESS;
130}
131
132
133/**
134 * Check the CPUID features of this particular CPU and disable relevant features
135 * for the guest which do not exist on this CPU. We have seen systems where the
136 * X86_CPUID_FEATURE_ECX_MONITOR feature flag is only set on some host CPUs, see
137 * @bugref{5436}.
138 *
139 * @note This function might be called simultaneously on more than one CPU!
140 *
141 * @param idCpu The identifier for the CPU the function is called on.
142 * @param pvUser1 Pointer to the VM structure.
143 * @param pvUser2 Ignored.
144 */
145static DECLCALLBACK(void) cpumR0CheckCpuid(RTCPUID idCpu, void *pvUser1, void *pvUser2)
146{
147 PVMCC pVM = (PVMCC)pvUser1;
148
149 NOREF(idCpu); NOREF(pvUser2);
150 for (uint32_t i = 0; i < RT_ELEMENTS(g_aCpuidUnifyBits); i++)
151 {
152 /* Note! Cannot use cpumCpuIdGetLeaf from here because we're not
153 necessarily in the VM process context. So, we using the
154 legacy arrays as temporary storage. */
155
156 uint32_t uLeaf = g_aCpuidUnifyBits[i].uLeaf;
157 PCPUMCPUID pLegacyLeaf;
158 if (uLeaf < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmStd))
159 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmStd[uLeaf];
160 else if (uLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmExt))
161 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmExt[uLeaf - UINT32_C(0x80000000)];
162 else if (uLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmCentaur))
163 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmCentaur[uLeaf - UINT32_C(0xc0000000)];
164 else
165 continue;
166
167 uint32_t eax, ebx, ecx, edx;
168 ASMCpuIdExSlow(uLeaf, 0, 0, 0, &eax, &ebx, &ecx, &edx);
169
170 ASMAtomicAndU32(&pLegacyLeaf->uEcx, ecx | ~g_aCpuidUnifyBits[i].uEcx);
171 ASMAtomicAndU32(&pLegacyLeaf->uEdx, edx | ~g_aCpuidUnifyBits[i].uEdx);
172 }
173}
174
175
176/**
177 * Does Ring-0 CPUM initialization.
178 *
179 * This is mainly to check that the Host CPU mode is compatible
180 * with VBox.
181 *
182 * @returns VBox status code.
183 * @param pVM The cross context VM structure.
184 */
185VMMR0_INT_DECL(int) CPUMR0InitVM(PVMCC pVM)
186{
187 LogFlow(("CPUMR0Init: %p\n", pVM));
188
189 /*
190 * Check CR0 & CR4 flags.
191 */
192 uint32_t u32CR0 = ASMGetCR0();
193 if ((u32CR0 & (X86_CR0_PE | X86_CR0_PG)) != (X86_CR0_PE | X86_CR0_PG)) /* a bit paranoid perhaps.. */
194 {
195 Log(("CPUMR0Init: PE or PG not set. cr0=%#x\n", u32CR0));
196 return VERR_UNSUPPORTED_CPU_MODE;
197 }
198
199 /*
200 * Check for sysenter and syscall usage.
201 */
202 if (ASMHasCpuId())
203 {
204 /*
205 * SYSENTER/SYSEXIT
206 *
207 * Intel docs claim you should test both the flag and family, model &
208 * stepping because some Pentium Pro CPUs have the SEP cpuid flag set,
209 * but don't support it. AMD CPUs may support this feature in legacy
210 * mode, they've banned it from long mode. Since we switch to 32-bit
211 * mode when entering raw-mode context the feature would become
212 * accessible again on AMD CPUs, so we have to check regardless of
213 * host bitness.
214 */
215 uint32_t u32CpuVersion;
216 uint32_t u32Dummy;
217 uint32_t fFeatures; /* (Used further down to check for MSRs, so don't clobber.) */
218 ASMCpuId(1, &u32CpuVersion, &u32Dummy, &u32Dummy, &fFeatures);
219 uint32_t const u32Family = u32CpuVersion >> 8;
220 uint32_t const u32Model = (u32CpuVersion >> 4) & 0xF;
221 uint32_t const u32Stepping = u32CpuVersion & 0xF;
222 if ( (fFeatures & X86_CPUID_FEATURE_EDX_SEP)
223 && ( u32Family != 6 /* (> pentium pro) */
224 || u32Model >= 3
225 || u32Stepping >= 3
226 || !ASMIsIntelCpu())
227 )
228 {
229 /*
230 * Read the MSR and see if it's in use or not.
231 */
232 uint32_t u32 = ASMRdMsr_Low(MSR_IA32_SYSENTER_CS);
233 if (u32)
234 {
235 pVM->cpum.s.fHostUseFlags |= CPUM_USE_SYSENTER;
236 Log(("CPUMR0Init: host uses sysenter cs=%08x%08x\n", ASMRdMsr_High(MSR_IA32_SYSENTER_CS), u32));
237 }
238 }
239
240 /*
241 * SYSCALL/SYSRET
242 *
243 * This feature is indicated by the SEP bit returned in EDX by CPUID
244 * function 0x80000001. Intel CPUs only supports this feature in
245 * long mode. Since we're not running 64-bit guests in raw-mode there
246 * are no issues with 32-bit intel hosts.
247 */
248 uint32_t cExt = 0;
249 ASMCpuId(0x80000000, &cExt, &u32Dummy, &u32Dummy, &u32Dummy);
250 if (ASMIsValidExtRange(cExt))
251 {
252 uint32_t fExtFeaturesEDX = ASMCpuId_EDX(0x80000001);
253 if (fExtFeaturesEDX & X86_CPUID_EXT_FEATURE_EDX_SYSCALL)
254 {
255#ifdef RT_ARCH_X86
256 if (!ASMIsIntelCpu())
257#endif
258 {
259 uint64_t fEfer = ASMRdMsr(MSR_K6_EFER);
260 if (fEfer & MSR_K6_EFER_SCE)
261 {
262 pVM->cpum.s.fHostUseFlags |= CPUM_USE_SYSCALL;
263 Log(("CPUMR0Init: host uses syscall\n"));
264 }
265 }
266 }
267 }
268
269 /*
270 * Copy MSR_IA32_ARCH_CAPABILITIES bits over into the host and guest feature
271 * structure and as well as the guest MSR.
272 * Note! we assume this happens after the CPUMR3Init is done, so CPUID bits are settled.
273 */
274 pVM->cpum.s.HostFeatures.fArchRdclNo = 0;
275 pVM->cpum.s.HostFeatures.fArchIbrsAll = 0;
276 pVM->cpum.s.HostFeatures.fArchRsbOverride = 0;
277 pVM->cpum.s.HostFeatures.fArchVmmNeedNotFlushL1d = 0;
278 pVM->cpum.s.HostFeatures.fArchMdsNo = 0;
279 uint32_t const cStdRange = ASMCpuId_EAX(0);
280 if ( ASMIsValidStdRange(cStdRange)
281 && cStdRange >= 7)
282 {
283 uint32_t fEdxFeatures = ASMCpuId_EDX(7);
284 if ( (fEdxFeatures & X86_CPUID_STEXT_FEATURE_EDX_ARCHCAP)
285 && (fFeatures & X86_CPUID_FEATURE_EDX_MSR))
286 {
287 /* Host: */
288 uint64_t fArchVal = ASMRdMsr(MSR_IA32_ARCH_CAPABILITIES);
289 pVM->cpum.s.HostFeatures.fArchRdclNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RDCL_NO);
290 pVM->cpum.s.HostFeatures.fArchIbrsAll = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_IBRS_ALL);
291 pVM->cpum.s.HostFeatures.fArchRsbOverride = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RSBO);
292 pVM->cpum.s.HostFeatures.fArchVmmNeedNotFlushL1d = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_VMM_NEED_NOT_FLUSH_L1D);
293 pVM->cpum.s.HostFeatures.fArchMdsNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_MDS_NO);
294
295 /* guest: */
296 if (!pVM->cpum.s.GuestFeatures.fArchCap)
297 fArchVal = 0;
298 else if (!pVM->cpum.s.GuestFeatures.fIbrs)
299 fArchVal &= ~MSR_IA32_ARCH_CAP_F_IBRS_ALL;
300 VMCC_FOR_EACH_VMCPU_STMT(pVM, pVCpu->cpum.s.GuestMsrs.msr.ArchCaps = fArchVal);
301 pVM->cpum.s.GuestFeatures.fArchRdclNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RDCL_NO);
302 pVM->cpum.s.GuestFeatures.fArchIbrsAll = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_IBRS_ALL);
303 pVM->cpum.s.GuestFeatures.fArchRsbOverride = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RSBO);
304 pVM->cpum.s.GuestFeatures.fArchVmmNeedNotFlushL1d = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_VMM_NEED_NOT_FLUSH_L1D);
305 pVM->cpum.s.GuestFeatures.fArchMdsNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_MDS_NO);
306 }
307 else
308 pVM->cpum.s.HostFeatures.fArchCap = 0;
309 }
310
311 /*
312 * Unify/cross check some CPUID feature bits on all available CPU cores
313 * and threads. We've seen CPUs where the monitor support differed.
314 *
315 * Because the hyper heap isn't always mapped into ring-0, we cannot
316 * access it from a RTMpOnAll callback. We use the legacy CPUID arrays
317 * as temp ring-0 accessible memory instead, ASSUMING that they're all
318 * up to date when we get here.
319 */
320 RTMpOnAll(cpumR0CheckCpuid, pVM, NULL);
321
322 for (uint32_t i = 0; i < RT_ELEMENTS(g_aCpuidUnifyBits); i++)
323 {
324 bool fIgnored;
325 uint32_t uLeaf = g_aCpuidUnifyBits[i].uLeaf;
326 PCPUMCPUIDLEAF pLeaf = cpumCpuIdGetLeafEx(pVM, uLeaf, 0, &fIgnored);
327 if (pLeaf)
328 {
329 PCPUMCPUID pLegacyLeaf;
330 if (uLeaf < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmStd))
331 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmStd[uLeaf];
332 else if (uLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmExt))
333 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmExt[uLeaf - UINT32_C(0x80000000)];
334 else if (uLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmCentaur))
335 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmCentaur[uLeaf - UINT32_C(0xc0000000)];
336 else
337 continue;
338
339 pLeaf->uEcx = pLegacyLeaf->uEcx;
340 pLeaf->uEdx = pLegacyLeaf->uEdx;
341 }
342 }
343
344 }
345
346
347 /*
348 * Check if debug registers are armed.
349 * This ASSUMES that DR7.GD is not set, or that it's handled transparently!
350 */
351 uint32_t u32DR7 = ASMGetDR7();
352 if (u32DR7 & X86_DR7_ENABLED_MASK)
353 {
354 VMCC_FOR_EACH_VMCPU_STMT(pVM, pVCpu->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS_HOST);
355 Log(("CPUMR0Init: host uses debug registers (dr7=%x)\n", u32DR7));
356 }
357
358 return VINF_SUCCESS;
359}
360
361
362/**
363 * Trap handler for device-not-available fault (\#NM).
364 * Device not available, FP or (F)WAIT instruction.
365 *
366 * @returns VBox status code.
367 * @retval VINF_SUCCESS if the guest FPU state is loaded.
368 * @retval VINF_EM_RAW_GUEST_TRAP if it is a guest trap.
369 * @retval VINF_CPUM_HOST_CR0_MODIFIED if we modified the host CR0.
370 *
371 * @param pVM The cross context VM structure.
372 * @param pVCpu The cross context virtual CPU structure.
373 */
374VMMR0_INT_DECL(int) CPUMR0Trap07Handler(PVMCC pVM, PVMCPUCC pVCpu)
375{
376 Assert(pVM->cpum.s.HostFeatures.fFxSaveRstor);
377 Assert(ASMGetCR4() & X86_CR4_OSFXSR);
378
379 /* If the FPU state has already been loaded, then it's a guest trap. */
380 if (CPUMIsGuestFPUStateActive(pVCpu))
381 {
382 Assert( ((pVCpu->cpum.s.Guest.cr0 & (X86_CR0_MP | X86_CR0_EM | X86_CR0_TS)) == (X86_CR0_MP | X86_CR0_TS))
383 || ((pVCpu->cpum.s.Guest.cr0 & (X86_CR0_MP | X86_CR0_EM | X86_CR0_TS)) == (X86_CR0_MP | X86_CR0_TS | X86_CR0_EM)));
384 return VINF_EM_RAW_GUEST_TRAP;
385 }
386
387 /*
388 * There are two basic actions:
389 * 1. Save host fpu and restore guest fpu.
390 * 2. Generate guest trap.
391 *
392 * When entering the hypervisor we'll always enable MP (for proper wait
393 * trapping) and TS (for intercepting all fpu/mmx/sse stuff). The EM flag
394 * is taken from the guest OS in order to get proper SSE handling.
395 *
396 *
397 * Actions taken depending on the guest CR0 flags:
398 *
399 * 3 2 1
400 * TS | EM | MP | FPUInstr | WAIT :: VMM Action
401 * ------------------------------------------------------------------------
402 * 0 | 0 | 0 | Exec | Exec :: Clear TS & MP, Save HC, Load GC.
403 * 0 | 0 | 1 | Exec | Exec :: Clear TS, Save HC, Load GC.
404 * 0 | 1 | 0 | #NM | Exec :: Clear TS & MP, Save HC, Load GC.
405 * 0 | 1 | 1 | #NM | Exec :: Clear TS, Save HC, Load GC.
406 * 1 | 0 | 0 | #NM | Exec :: Clear MP, Save HC, Load GC. (EM is already cleared.)
407 * 1 | 0 | 1 | #NM | #NM :: Go to guest taking trap there.
408 * 1 | 1 | 0 | #NM | Exec :: Clear MP, Save HC, Load GC. (EM is already set.)
409 * 1 | 1 | 1 | #NM | #NM :: Go to guest taking trap there.
410 */
411
412 switch (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_MP | X86_CR0_EM | X86_CR0_TS))
413 {
414 case X86_CR0_MP | X86_CR0_TS:
415 case X86_CR0_MP | X86_CR0_TS | X86_CR0_EM:
416 return VINF_EM_RAW_GUEST_TRAP;
417 default:
418 break;
419 }
420
421 return CPUMR0LoadGuestFPU(pVM, pVCpu);
422}
423
424
425/**
426 * Saves the host-FPU/XMM state (if necessary) and (always) loads the guest-FPU
427 * state into the CPU.
428 *
429 * @returns VINF_SUCCESS on success, host CR0 unmodified.
430 * @returns VINF_CPUM_HOST_CR0_MODIFIED on success when the host CR0 was
431 * modified and VT-x needs to update the value in the VMCS.
432 *
433 * @param pVM The cross context VM structure.
434 * @param pVCpu The cross context virtual CPU structure.
435 */
436VMMR0_INT_DECL(int) CPUMR0LoadGuestFPU(PVMCC pVM, PVMCPUCC pVCpu)
437{
438 int rc;
439 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
440 Assert(!(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST));
441 Assert(!(pVCpu->cpum.s.fUseFlags & CPUM_SYNC_FPU_STATE));
442
443 if (!pVM->cpum.s.HostFeatures.fLeakyFxSR)
444 {
445 Assert(!(pVCpu->cpum.s.fUseFlags & CPUM_USED_MANUAL_XMM_RESTORE));
446 rc = cpumR0SaveHostRestoreGuestFPUState(&pVCpu->cpum.s);
447 }
448 else
449 {
450 Assert(!(pVCpu->cpum.s.fUseFlags & CPUM_USED_MANUAL_XMM_RESTORE) || (pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_HOST));
451 /** @todo r=ramshankar: Can't we used a cached value here
452 * instead of reading the MSR? host EFER doesn't usually
453 * change. */
454 uint64_t uHostEfer = ASMRdMsr(MSR_K6_EFER);
455 if (!(uHostEfer & MSR_K6_EFER_FFXSR))
456 rc = cpumR0SaveHostRestoreGuestFPUState(&pVCpu->cpum.s);
457 else
458 {
459 RTCCUINTREG const uSavedFlags = ASMIntDisableFlags();
460 pVCpu->cpum.s.fUseFlags |= CPUM_USED_MANUAL_XMM_RESTORE;
461 ASMWrMsr(MSR_K6_EFER, uHostEfer & ~MSR_K6_EFER_FFXSR);
462 rc = cpumR0SaveHostRestoreGuestFPUState(&pVCpu->cpum.s);
463 ASMWrMsr(MSR_K6_EFER, uHostEfer | MSR_K6_EFER_FFXSR);
464 ASMSetFlags(uSavedFlags);
465 }
466 }
467 Assert( (pVCpu->cpum.s.fUseFlags & (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST | CPUM_USED_FPU_SINCE_REM))
468 == (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST | CPUM_USED_FPU_SINCE_REM));
469 return rc;
470}
471
472
473/**
474 * Saves the guest FPU/XMM state if needed, restores the host FPU/XMM state as
475 * needed.
476 *
477 * @returns true if we saved the guest state.
478 * @param pVCpu The cross context virtual CPU structure.
479 */
480VMMR0_INT_DECL(bool) CPUMR0FpuStateMaybeSaveGuestAndRestoreHost(PVMCPUCC pVCpu)
481{
482 bool fSavedGuest;
483 Assert(pVCpu->CTX_SUFF(pVM)->cpum.s.HostFeatures.fFxSaveRstor);
484 Assert(ASMGetCR4() & X86_CR4_OSFXSR);
485 if (pVCpu->cpum.s.fUseFlags & (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST))
486 {
487 fSavedGuest = RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST);
488 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USED_MANUAL_XMM_RESTORE))
489 cpumR0SaveGuestRestoreHostFPUState(&pVCpu->cpum.s);
490 else
491 {
492 /* Temporarily clear MSR_K6_EFER_FFXSR or else we'll be unable to
493 save/restore the XMM state with fxsave/fxrstor. */
494 uint64_t uHostEfer = ASMRdMsr(MSR_K6_EFER);
495 if (uHostEfer & MSR_K6_EFER_FFXSR)
496 {
497 RTCCUINTREG const uSavedFlags = ASMIntDisableFlags();
498 ASMWrMsr(MSR_K6_EFER, uHostEfer & ~MSR_K6_EFER_FFXSR);
499 cpumR0SaveGuestRestoreHostFPUState(&pVCpu->cpum.s);
500 ASMWrMsr(MSR_K6_EFER, uHostEfer | MSR_K6_EFER_FFXSR);
501 ASMSetFlags(uSavedFlags);
502 }
503 else
504 cpumR0SaveGuestRestoreHostFPUState(&pVCpu->cpum.s);
505 pVCpu->cpum.s.fUseFlags &= ~CPUM_USED_MANUAL_XMM_RESTORE;
506 }
507 }
508 else
509 fSavedGuest = false;
510 Assert(!( pVCpu->cpum.s.fUseFlags
511 & (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST | CPUM_SYNC_FPU_STATE | CPUM_USED_MANUAL_XMM_RESTORE)));
512 return fSavedGuest;
513}
514
515
516/**
517 * Saves the host debug state, setting CPUM_USED_HOST_DEBUG_STATE and loading
518 * DR7 with safe values.
519 *
520 * @returns VBox status code.
521 * @param pVCpu The cross context virtual CPU structure.
522 */
523static int cpumR0SaveHostDebugState(PVMCPUCC pVCpu)
524{
525 /*
526 * Save the host state.
527 */
528 pVCpu->cpum.s.Host.dr0 = ASMGetDR0();
529 pVCpu->cpum.s.Host.dr1 = ASMGetDR1();
530 pVCpu->cpum.s.Host.dr2 = ASMGetDR2();
531 pVCpu->cpum.s.Host.dr3 = ASMGetDR3();
532 pVCpu->cpum.s.Host.dr6 = ASMGetDR6();
533 /** @todo dr7 might already have been changed to 0x400; don't care right now as it's harmless. */
534 pVCpu->cpum.s.Host.dr7 = ASMGetDR7();
535
536 /* Preemption paranoia. */
537 ASMAtomicOrU32(&pVCpu->cpum.s.fUseFlags, CPUM_USED_DEBUG_REGS_HOST);
538
539 /*
540 * Make sure DR7 is harmless or else we could trigger breakpoints when
541 * load guest or hypervisor DRx values later.
542 */
543 if (pVCpu->cpum.s.Host.dr7 != X86_DR7_INIT_VAL)
544 ASMSetDR7(X86_DR7_INIT_VAL);
545
546 return VINF_SUCCESS;
547}
548
549
550/**
551 * Saves the guest DRx state residing in host registers and restore the host
552 * register values.
553 *
554 * The guest DRx state is only saved if CPUMR0LoadGuestDebugState was called,
555 * since it's assumed that we're shadowing the guest DRx register values
556 * accurately when using the combined hypervisor debug register values
557 * (CPUMR0LoadHyperDebugState).
558 *
559 * @returns true if either guest or hypervisor debug registers were loaded.
560 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
561 * @param fDr6 Whether to include DR6 or not.
562 * @thread EMT(pVCpu)
563 */
564VMMR0_INT_DECL(bool) CPUMR0DebugStateMaybeSaveGuestAndRestoreHost(PVMCPUCC pVCpu, bool fDr6)
565{
566 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
567 bool const fDrXLoaded = RT_BOOL(pVCpu->cpum.s.fUseFlags & (CPUM_USED_DEBUG_REGS_GUEST | CPUM_USED_DEBUG_REGS_HYPER));
568
569 /*
570 * Do we need to save the guest DRx registered loaded into host registers?
571 * (DR7 and DR6 (if fDr6 is true) are left to the caller.)
572 */
573 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_GUEST)
574 {
575 pVCpu->cpum.s.Guest.dr[0] = ASMGetDR0();
576 pVCpu->cpum.s.Guest.dr[1] = ASMGetDR1();
577 pVCpu->cpum.s.Guest.dr[2] = ASMGetDR2();
578 pVCpu->cpum.s.Guest.dr[3] = ASMGetDR3();
579 if (fDr6)
580 pVCpu->cpum.s.Guest.dr[6] = ASMGetDR6();
581 }
582 ASMAtomicAndU32(&pVCpu->cpum.s.fUseFlags, ~( CPUM_USED_DEBUG_REGS_GUEST | CPUM_USED_DEBUG_REGS_HYPER
583 | CPUM_SYNC_DEBUG_REGS_GUEST | CPUM_SYNC_DEBUG_REGS_HYPER));
584
585 /*
586 * Restore the host's debug state. DR0-3, DR6 and only then DR7!
587 */
588 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HOST)
589 {
590 /* A bit of paranoia first... */
591 uint64_t uCurDR7 = ASMGetDR7();
592 if (uCurDR7 != X86_DR7_INIT_VAL)
593 ASMSetDR7(X86_DR7_INIT_VAL);
594
595 ASMSetDR0(pVCpu->cpum.s.Host.dr0);
596 ASMSetDR1(pVCpu->cpum.s.Host.dr1);
597 ASMSetDR2(pVCpu->cpum.s.Host.dr2);
598 ASMSetDR3(pVCpu->cpum.s.Host.dr3);
599 /** @todo consider only updating if they differ, esp. DR6. Need to figure how
600 * expensive DRx reads are over DRx writes. */
601 ASMSetDR6(pVCpu->cpum.s.Host.dr6);
602 ASMSetDR7(pVCpu->cpum.s.Host.dr7);
603
604 ASMAtomicAndU32(&pVCpu->cpum.s.fUseFlags, ~CPUM_USED_DEBUG_REGS_HOST);
605 }
606
607 return fDrXLoaded;
608}
609
610
611/**
612 * Saves the guest DRx state if it resides host registers.
613 *
614 * This does NOT clear any use flags, so the host registers remains loaded with
615 * the guest DRx state upon return. The purpose is only to make sure the values
616 * in the CPU context structure is up to date.
617 *
618 * @returns true if the host registers contains guest values, false if not.
619 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
620 * @param fDr6 Whether to include DR6 or not.
621 * @thread EMT(pVCpu)
622 */
623VMMR0_INT_DECL(bool) CPUMR0DebugStateMaybeSaveGuest(PVMCPUCC pVCpu, bool fDr6)
624{
625 /*
626 * Do we need to save the guest DRx registered loaded into host registers?
627 * (DR7 and DR6 (if fDr6 is true) are left to the caller.)
628 */
629 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_GUEST)
630 {
631 pVCpu->cpum.s.Guest.dr[0] = ASMGetDR0();
632 pVCpu->cpum.s.Guest.dr[1] = ASMGetDR1();
633 pVCpu->cpum.s.Guest.dr[2] = ASMGetDR2();
634 pVCpu->cpum.s.Guest.dr[3] = ASMGetDR3();
635 if (fDr6)
636 pVCpu->cpum.s.Guest.dr[6] = ASMGetDR6();
637 return true;
638 }
639 return false;
640}
641
642
643/**
644 * Lazily sync in the debug state.
645 *
646 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
647 * @param fDr6 Whether to include DR6 or not.
648 * @thread EMT(pVCpu)
649 */
650VMMR0_INT_DECL(void) CPUMR0LoadGuestDebugState(PVMCPUCC pVCpu, bool fDr6)
651{
652 /*
653 * Save the host state and disarm all host BPs.
654 */
655 cpumR0SaveHostDebugState(pVCpu);
656 Assert(ASMGetDR7() == X86_DR7_INIT_VAL);
657
658 /*
659 * Activate the guest state DR0-3.
660 * DR7 and DR6 (if fDr6 is true) are left to the caller.
661 */
662 ASMSetDR0(pVCpu->cpum.s.Guest.dr[0]);
663 ASMSetDR1(pVCpu->cpum.s.Guest.dr[1]);
664 ASMSetDR2(pVCpu->cpum.s.Guest.dr[2]);
665 ASMSetDR3(pVCpu->cpum.s.Guest.dr[3]);
666 if (fDr6)
667 ASMSetDR6(pVCpu->cpum.s.Guest.dr[6]);
668
669 ASMAtomicOrU32(&pVCpu->cpum.s.fUseFlags, CPUM_USED_DEBUG_REGS_GUEST);
670}
671
672
673/**
674 * Lazily sync in the hypervisor debug state
675 *
676 * @returns VBox status code.
677 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
678 * @param fDr6 Whether to include DR6 or not.
679 * @thread EMT(pVCpu)
680 */
681VMMR0_INT_DECL(void) CPUMR0LoadHyperDebugState(PVMCPUCC pVCpu, bool fDr6)
682{
683 /*
684 * Save the host state and disarm all host BPs.
685 */
686 cpumR0SaveHostDebugState(pVCpu);
687 Assert(ASMGetDR7() == X86_DR7_INIT_VAL);
688
689 /*
690 * Make sure the hypervisor values are up to date.
691 */
692 CPUMRecalcHyperDRx(pVCpu, UINT8_MAX /* no loading, please */, true);
693
694 /*
695 * Activate the guest state DR0-3.
696 * DR7 and DR6 (if fDr6 is true) are left to the caller.
697 */
698 ASMSetDR0(pVCpu->cpum.s.Hyper.dr[0]);
699 ASMSetDR1(pVCpu->cpum.s.Hyper.dr[1]);
700 ASMSetDR2(pVCpu->cpum.s.Hyper.dr[2]);
701 ASMSetDR3(pVCpu->cpum.s.Hyper.dr[3]);
702 if (fDr6)
703 ASMSetDR6(X86_DR6_INIT_VAL);
704
705 ASMAtomicOrU32(&pVCpu->cpum.s.fUseFlags, CPUM_USED_DEBUG_REGS_HYPER);
706}
707
708#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
709
710/**
711 * Per-CPU callback that probes the CPU for APIC support.
712 *
713 * @param idCpu The identifier for the CPU the function is called on.
714 * @param pvUser1 Ignored.
715 * @param pvUser2 Ignored.
716 */
717static DECLCALLBACK(void) cpumR0MapLocalApicCpuProber(RTCPUID idCpu, void *pvUser1, void *pvUser2)
718{
719 NOREF(pvUser1); NOREF(pvUser2);
720 int iCpu = RTMpCpuIdToSetIndex(idCpu);
721 AssertReturnVoid(iCpu >= 0 && (unsigned)iCpu < RT_ELEMENTS(g_aLApics));
722
723 /*
724 * Check for APIC support.
725 */
726 uint32_t uMaxLeaf, u32EBX, u32ECX, u32EDX;
727 ASMCpuId(0, &uMaxLeaf, &u32EBX, &u32ECX, &u32EDX);
728 if ( ( ASMIsIntelCpuEx(u32EBX, u32ECX, u32EDX)
729 || ASMIsAmdCpuEx(u32EBX, u32ECX, u32EDX)
730 || ASMIsViaCentaurCpuEx(u32EBX, u32ECX, u32EDX)
731 || ASMIsShanghaiCpuEx(u32EBX, u32ECX, u32EDX)
732 || ASMIsHygonCpuEx(u32EBX, u32ECX, u32EDX))
733 && ASMIsValidStdRange(uMaxLeaf))
734 {
735 uint32_t uDummy;
736 ASMCpuId(1, &uDummy, &u32EBX, &u32ECX, &u32EDX);
737 if ( (u32EDX & X86_CPUID_FEATURE_EDX_APIC)
738 && (u32EDX & X86_CPUID_FEATURE_EDX_MSR))
739 {
740 /*
741 * Safe to access the MSR. Read it and calc the BASE (a little complicated).
742 */
743 uint64_t u64ApicBase = ASMRdMsr(MSR_IA32_APICBASE);
744 uint64_t u64Mask = MSR_IA32_APICBASE_BASE_MIN;
745
746 /* see Intel Manual: Local APIC Status and Location: MAXPHYADDR default is bit 36 */
747 uint32_t uMaxExtLeaf;
748 ASMCpuId(0x80000000, &uMaxExtLeaf, &u32EBX, &u32ECX, &u32EDX);
749 if ( uMaxExtLeaf >= UINT32_C(0x80000008)
750 && ASMIsValidExtRange(uMaxExtLeaf))
751 {
752 uint32_t u32PhysBits;
753 ASMCpuId(0x80000008, &u32PhysBits, &u32EBX, &u32ECX, &u32EDX);
754 u32PhysBits &= 0xff;
755 u64Mask = ((UINT64_C(1) << u32PhysBits) - 1) & UINT64_C(0xfffffffffffff000);
756 }
757
758 AssertCompile(sizeof(g_aLApics[iCpu].PhysBase) == sizeof(u64ApicBase));
759 g_aLApics[iCpu].PhysBase = u64ApicBase & u64Mask;
760 g_aLApics[iCpu].fEnabled = RT_BOOL(u64ApicBase & MSR_IA32_APICBASE_EN);
761 g_aLApics[iCpu].fX2Apic = (u64ApicBase & (MSR_IA32_APICBASE_EXTD | MSR_IA32_APICBASE_EN))
762 == (MSR_IA32_APICBASE_EXTD | MSR_IA32_APICBASE_EN);
763 }
764 }
765}
766
767
768
769/**
770 * Per-CPU callback that verifies our APIC expectations.
771 *
772 * @param idCpu The identifier for the CPU the function is called on.
773 * @param pvUser1 Ignored.
774 * @param pvUser2 Ignored.
775 */
776static DECLCALLBACK(void) cpumR0MapLocalApicCpuChecker(RTCPUID idCpu, void *pvUser1, void *pvUser2)
777{
778 NOREF(pvUser1); NOREF(pvUser2);
779
780 int iCpu = RTMpCpuIdToSetIndex(idCpu);
781 AssertReturnVoid(iCpu >= 0 && (unsigned)iCpu < RT_ELEMENTS(g_aLApics));
782 if (!g_aLApics[iCpu].fEnabled)
783 return;
784
785 /*
786 * 0x0X 82489 external APIC
787 * 0x1X Local APIC
788 * 0x2X..0xFF reserved
789 */
790 uint32_t uApicVersion;
791 if (g_aLApics[iCpu].fX2Apic)
792 uApicVersion = ApicX2RegRead32(APIC_REG_VERSION);
793 else
794 uApicVersion = ApicRegRead(g_aLApics[iCpu].pv, APIC_REG_VERSION);
795 if ((APIC_REG_VERSION_GET_VER(uApicVersion) & 0xF0) == 0x10)
796 {
797 g_aLApics[iCpu].uVersion = uApicVersion;
798
799# if 0 /* enable if you need it. */
800 if (g_aLApics[iCpu].fX2Apic)
801 SUPR0Printf("CPUM: X2APIC %02u - ver %#010x, lint0=%#07x lint1=%#07x pc=%#07x thmr=%#07x cmci=%#07x\n",
802 iCpu, uApicVersion,
803 ApicX2RegRead32(APIC_REG_LVT_LINT0), ApicX2RegRead32(APIC_REG_LVT_LINT1),
804 ApicX2RegRead32(APIC_REG_LVT_PC), ApicX2RegRead32(APIC_REG_LVT_THMR),
805 ApicX2RegRead32(APIC_REG_LVT_CMCI));
806 else
807 {
808 SUPR0Printf("CPUM: APIC %02u at %RGp (mapped at %p) - ver %#010x, lint0=%#07x lint1=%#07x pc=%#07x thmr=%#07x cmci=%#07x\n",
809 iCpu, g_aLApics[iCpu].PhysBase, g_aLApics[iCpu].pv, uApicVersion,
810 ApicRegRead(g_aLApics[iCpu].pv, APIC_REG_LVT_LINT0), ApicRegRead(g_aLApics[iCpu].pv, APIC_REG_LVT_LINT1),
811 ApicRegRead(g_aLApics[iCpu].pv, APIC_REG_LVT_PC), ApicRegRead(g_aLApics[iCpu].pv, APIC_REG_LVT_THMR),
812 ApicRegRead(g_aLApics[iCpu].pv, APIC_REG_LVT_CMCI));
813 if (uApicVersion & 0x80000000)
814 {
815 uint32_t uExtFeatures = ApicRegRead(g_aLApics[iCpu].pv, 0x400);
816 uint32_t cEiLvt = (uExtFeatures >> 16) & 0xff;
817 SUPR0Printf("CPUM: APIC %02u: ExtSpace available. extfeat=%08x eilvt[0..3]=%08x %08x %08x %08x\n",
818 iCpu,
819 ApicRegRead(g_aLApics[iCpu].pv, 0x400),
820 cEiLvt >= 1 ? ApicRegRead(g_aLApics[iCpu].pv, 0x500) : 0,
821 cEiLvt >= 2 ? ApicRegRead(g_aLApics[iCpu].pv, 0x510) : 0,
822 cEiLvt >= 3 ? ApicRegRead(g_aLApics[iCpu].pv, 0x520) : 0,
823 cEiLvt >= 4 ? ApicRegRead(g_aLApics[iCpu].pv, 0x530) : 0);
824 }
825 }
826# endif
827 }
828 else
829 {
830 g_aLApics[iCpu].fEnabled = false;
831 g_aLApics[iCpu].fX2Apic = false;
832 SUPR0Printf("VBox/CPUM: Unsupported APIC version %#x (iCpu=%d)\n", uApicVersion, iCpu);
833 }
834}
835
836
837/**
838 * Map the MMIO page of each local APIC in the system.
839 */
840static int cpumR0MapLocalApics(void)
841{
842 /*
843 * Check that we'll always stay within the array bounds.
844 */
845 if (RTMpGetArraySize() > RT_ELEMENTS(g_aLApics))
846 {
847 LogRel(("CPUM: Too many real CPUs/cores/threads - %u, max %u\n", RTMpGetArraySize(), RT_ELEMENTS(g_aLApics)));
848 return VERR_TOO_MANY_CPUS;
849 }
850
851 /*
852 * Create mappings for all online CPUs we think have legacy APICs.
853 */
854 int rc = RTMpOnAll(cpumR0MapLocalApicCpuProber, NULL, NULL);
855
856 for (unsigned iCpu = 0; RT_SUCCESS(rc) && iCpu < RT_ELEMENTS(g_aLApics); iCpu++)
857 {
858 if (g_aLApics[iCpu].fEnabled && !g_aLApics[iCpu].fX2Apic)
859 {
860 rc = RTR0MemObjEnterPhys(&g_aLApics[iCpu].hMemObj, g_aLApics[iCpu].PhysBase,
861 PAGE_SIZE, RTMEM_CACHE_POLICY_MMIO);
862 if (RT_SUCCESS(rc))
863 {
864 rc = RTR0MemObjMapKernel(&g_aLApics[iCpu].hMapObj, g_aLApics[iCpu].hMemObj, (void *)-1,
865 PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
866 if (RT_SUCCESS(rc))
867 {
868 g_aLApics[iCpu].pv = RTR0MemObjAddress(g_aLApics[iCpu].hMapObj);
869 continue;
870 }
871 RTR0MemObjFree(g_aLApics[iCpu].hMemObj, true /* fFreeMappings */);
872 }
873 g_aLApics[iCpu].fEnabled = false;
874 }
875 g_aLApics[iCpu].pv = NULL;
876 }
877
878 /*
879 * Check the APICs.
880 */
881 if (RT_SUCCESS(rc))
882 rc = RTMpOnAll(cpumR0MapLocalApicCpuChecker, NULL, NULL);
883
884 if (RT_FAILURE(rc))
885 {
886 cpumR0UnmapLocalApics();
887 return rc;
888 }
889
890# ifdef LOG_ENABLED
891 /*
892 * Log the result (pretty useless, requires enabling CPUM in VBoxDrv
893 * and !VBOX_WITH_R0_LOGGING).
894 */
895 if (LogIsEnabled())
896 {
897 uint32_t cEnabled = 0;
898 uint32_t cX2Apics = 0;
899 for (unsigned iCpu = 0; iCpu < RT_ELEMENTS(g_aLApics); iCpu++)
900 if (g_aLApics[iCpu].fEnabled)
901 {
902 cEnabled++;
903 cX2Apics += g_aLApics[iCpu].fX2Apic;
904 }
905 Log(("CPUM: %u APICs, %u X2APICs\n", cEnabled, cX2Apics));
906 }
907# endif
908
909 return VINF_SUCCESS;
910}
911
912
913/**
914 * Unmap the Local APIC of all host CPUs.
915 */
916static void cpumR0UnmapLocalApics(void)
917{
918 for (unsigned iCpu = RT_ELEMENTS(g_aLApics); iCpu-- > 0;)
919 {
920 if (g_aLApics[iCpu].pv)
921 {
922 RTR0MemObjFree(g_aLApics[iCpu].hMapObj, true /* fFreeMappings */);
923 RTR0MemObjFree(g_aLApics[iCpu].hMemObj, true /* fFreeMappings */);
924 g_aLApics[iCpu].hMapObj = NIL_RTR0MEMOBJ;
925 g_aLApics[iCpu].hMemObj = NIL_RTR0MEMOBJ;
926 g_aLApics[iCpu].fEnabled = false;
927 g_aLApics[iCpu].fX2Apic = false;
928 g_aLApics[iCpu].pv = NULL;
929 }
930 }
931}
932
933
934/**
935 * Updates CPUMCPU::pvApicBase and CPUMCPU::fX2Apic prior to world switch.
936 *
937 * Writes the Local APIC mapping address of the current host CPU to CPUMCPU so
938 * the world switchers can access the APIC registers for the purpose of
939 * disabling and re-enabling the NMIs. Must be called with disabled preemption
940 * or disabled interrupts!
941 *
942 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
943 * @param iHostCpuSet The CPU set index of the current host CPU.
944 */
945VMMR0_INT_DECL(void) CPUMR0SetLApic(PVMCPUCC pVCpu, uint32_t iHostCpuSet)
946{
947 Assert(iHostCpuSet <= RT_ELEMENTS(g_aLApics));
948 pVCpu->cpum.s.pvApicBase = g_aLApics[iHostCpuSet].pv;
949 pVCpu->cpum.s.fX2Apic = g_aLApics[iHostCpuSet].fX2Apic;
950// Log6(("CPUMR0SetLApic: pvApicBase=%p fX2Apic=%d\n", g_aLApics[idxCpu].pv, g_aLApics[idxCpu].fX2Apic));
951}
952
953#endif /* VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI */
954
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