VirtualBox

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

Last change on this file since 95123 was 95123, checked in by vboxsync, 3 years ago

VMM: bugref:10209 Call SUPR0FpuEnd after restoring the FPU state.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 30.1 KB
Line 
1/* $Id: CPUMR0.cpp 95123 2022-05-26 14:59:46Z vboxsync $ */
2/** @file
3 * CPUM - Host Context Ring 0.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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#define CPUM_WITH_NONCONST_HOST_FEATURES
24#include <VBox/vmm/cpum.h>
25#include <VBox/vmm/hm.h>
26#include "CPUMInternal.h"
27#include <VBox/vmm/vmcc.h>
28#include <VBox/vmm/gvm.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31#include <VBox/vmm/hm.h>
32#include <iprt/assert.h>
33#include <iprt/asm-amd64-x86.h>
34#include <iprt/mem.h>
35#include <iprt/x86.h>
36
37
38/*********************************************************************************************************************************
39* Global Variables *
40*********************************************************************************************************************************/
41/** Host CPU features. */
42DECL_HIDDEN_DATA(CPUHOSTFEATURES) g_CpumHostFeatures;
43/** Static storage for host MSRs. */
44static CPUMMSRS g_CpumHostMsrs;
45
46/**
47 * CPUID bits to unify among all cores.
48 */
49static struct
50{
51 uint32_t uLeaf; /**< Leaf to check. */
52 uint32_t uEcx; /**< which bits in ecx to unify between CPUs. */
53 uint32_t uEdx; /**< which bits in edx to unify between CPUs. */
54}
55const g_aCpuidUnifyBits[] =
56{
57 {
58 0x00000001,
59 X86_CPUID_FEATURE_ECX_CX16 | X86_CPUID_FEATURE_ECX_MONITOR,
60 X86_CPUID_FEATURE_EDX_CX8
61 }
62};
63
64
65
66/*********************************************************************************************************************************
67* Internal Functions *
68*********************************************************************************************************************************/
69static int cpumR0SaveHostDebugState(PVMCPUCC pVCpu);
70
71
72/**
73 * Check the CPUID features of this particular CPU and disable relevant features
74 * for the guest which do not exist on this CPU.
75 *
76 * We have seen systems where the X86_CPUID_FEATURE_ECX_MONITOR feature flag is
77 * only set on some host CPUs, see @bugref{5436}.
78 *
79 * @note This function might be called simultaneously on more than one CPU!
80 *
81 * @param idCpu The identifier for the CPU the function is called on.
82 * @param pvUser1 Leaf array.
83 * @param pvUser2 Number of leaves.
84 */
85static DECLCALLBACK(void) cpumR0CheckCpuid(RTCPUID idCpu, void *pvUser1, void *pvUser2)
86{
87 PCPUMCPUIDLEAF const paLeaves = (PCPUMCPUIDLEAF)pvUser1;
88 uint32_t const cLeaves = (uint32_t)(uintptr_t)pvUser2;
89 RT_NOREF(idCpu);
90
91 for (uint32_t i = 0; i < RT_ELEMENTS(g_aCpuidUnifyBits); i++)
92 {
93 PCPUMCPUIDLEAF pLeaf = cpumCpuIdGetLeafInt(paLeaves, cLeaves, g_aCpuidUnifyBits[i].uLeaf, 0);
94 if (pLeaf)
95 {
96 uint32_t uEax, uEbx, uEcx, uEdx;
97 ASMCpuIdExSlow(g_aCpuidUnifyBits[i].uLeaf, 0, 0, 0, &uEax, &uEbx, &uEcx, &uEdx);
98
99 ASMAtomicAndU32(&pLeaf->uEcx, uEcx | ~g_aCpuidUnifyBits[i].uEcx);
100 ASMAtomicAndU32(&pLeaf->uEdx, uEdx | ~g_aCpuidUnifyBits[i].uEdx);
101 }
102 }
103}
104
105
106/**
107 * Does the Ring-0 CPU initialization once during module load.
108 * XXX Host-CPU hot-plugging?
109 */
110VMMR0_INT_DECL(int) CPUMR0ModuleInit(void)
111{
112 /*
113 * Query the hardware virtualization capabilities of the host CPU first.
114 */
115 uint32_t fHwCaps = 0;
116 int rc = SUPR0GetVTSupport(&fHwCaps);
117 AssertLogRelMsg(RT_SUCCESS(rc) || rc == VERR_UNSUPPORTED_CPU || rc == VERR_SVM_NO_SVM || rc == VERR_VMX_NO_VMX,
118 ("SUPR0GetHwvirtMsrs -> %Rrc\n", rc));
119 if (RT_SUCCESS(rc))
120 {
121 SUPHWVIRTMSRS HwvirtMsrs;
122 rc = SUPR0GetHwvirtMsrs(&HwvirtMsrs, fHwCaps, false /*fIgnored*/);
123 AssertLogRelRC(rc);
124 if (RT_SUCCESS(rc))
125 {
126 if (fHwCaps & SUPVTCAPS_VT_X)
127 HMGetVmxMsrsFromHwvirtMsrs(&HwvirtMsrs, &g_CpumHostMsrs.hwvirt.vmx);
128 else
129 HMGetSvmMsrsFromHwvirtMsrs(&HwvirtMsrs, &g_CpumHostMsrs.hwvirt.svm);
130 }
131 }
132
133 /*
134 * Collect CPUID leaves.
135 */
136 PCPUMCPUIDLEAF paLeaves;
137 uint32_t cLeaves;
138 rc = CPUMCpuIdCollectLeavesX86(&paLeaves, &cLeaves);
139 AssertLogRelRCReturn(rc, rc);
140
141 /*
142 * Unify/cross check some CPUID feature bits on all available CPU cores
143 * and threads. We've seen CPUs where the monitor support differed.
144 */
145 RTMpOnAll(cpumR0CheckCpuid, paLeaves, (void *)(uintptr_t)cLeaves);
146
147 /*
148 * Populate the host CPU feature global variable.
149 */
150 rc = cpumCpuIdExplodeFeaturesX86(paLeaves, cLeaves, &g_CpumHostMsrs, &g_CpumHostFeatures.s);
151 RTMemFree(paLeaves);
152 AssertLogRelRCReturn(rc, rc);
153
154 /*
155 * Get MSR_IA32_ARCH_CAPABILITIES and expand it into the host feature structure.
156 */
157 if (ASMHasCpuId())
158 {
159 /** @todo Should add this MSR to CPUMMSRS and expose it via SUPDrv... */
160 g_CpumHostFeatures.s.fArchRdclNo = 0;
161 g_CpumHostFeatures.s.fArchIbrsAll = 0;
162 g_CpumHostFeatures.s.fArchRsbOverride = 0;
163 g_CpumHostFeatures.s.fArchVmmNeedNotFlushL1d = 0;
164 g_CpumHostFeatures.s.fArchMdsNo = 0;
165 uint32_t const cStdRange = ASMCpuId_EAX(0);
166 if ( RTX86IsValidStdRange(cStdRange)
167 && cStdRange >= 7)
168 {
169 uint32_t const fStdFeaturesEdx = ASMCpuId_EDX(1);
170 uint32_t fStdExtFeaturesEdx;
171 ASMCpuIdExSlow(7, 0, 0, 0, NULL, NULL, NULL, &fStdExtFeaturesEdx);
172 if ( (fStdExtFeaturesEdx & X86_CPUID_STEXT_FEATURE_EDX_ARCHCAP)
173 && (fStdFeaturesEdx & X86_CPUID_FEATURE_EDX_MSR))
174 {
175 uint64_t fArchVal = ASMRdMsr(MSR_IA32_ARCH_CAPABILITIES);
176 g_CpumHostFeatures.s.fArchRdclNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RDCL_NO);
177 g_CpumHostFeatures.s.fArchIbrsAll = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_IBRS_ALL);
178 g_CpumHostFeatures.s.fArchRsbOverride = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RSBO);
179 g_CpumHostFeatures.s.fArchVmmNeedNotFlushL1d = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_VMM_NEED_NOT_FLUSH_L1D);
180 g_CpumHostFeatures.s.fArchMdsNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_MDS_NO);
181 }
182 else
183 g_CpumHostFeatures.s.fArchCap = 0;
184 }
185 }
186
187 return VINF_SUCCESS;
188}
189
190
191/**
192 * Terminate the module.
193 */
194VMMR0_INT_DECL(int) CPUMR0ModuleTerm(void)
195{
196 return VINF_SUCCESS;
197}
198
199
200/**
201 * Initializes the CPUM data in the VM structure.
202 *
203 * @param pGVM The global VM structure.
204 */
205VMMR0_INT_DECL(void) CPUMR0InitPerVMData(PGVM pGVM)
206{
207 /* Copy the ring-0 host feature set to the shared part so ring-3 can pick it up. */
208 pGVM->cpum.s.HostFeatures = g_CpumHostFeatures.s;
209}
210
211
212/**
213 * Check the CPUID features of this particular CPU and disable relevant features
214 * for the guest which do not exist on this CPU. We have seen systems where the
215 * X86_CPUID_FEATURE_ECX_MONITOR feature flag is only set on some host CPUs, see
216 * @bugref{5436}.
217 *
218 * @note This function might be called simultaneously on more than one CPU!
219 *
220 * @param idCpu The identifier for the CPU the function is called on.
221 * @param pvUser1 Pointer to the VM structure.
222 * @param pvUser2 Ignored.
223 */
224static DECLCALLBACK(void) cpumR0CheckCpuidLegacy(RTCPUID idCpu, void *pvUser1, void *pvUser2)
225{
226 PVMCC pVM = (PVMCC)pvUser1;
227
228 NOREF(idCpu); NOREF(pvUser2);
229 for (uint32_t i = 0; i < RT_ELEMENTS(g_aCpuidUnifyBits); i++)
230 {
231 /* Note! Cannot use cpumCpuIdGetLeaf from here because we're not
232 necessarily in the VM process context. So, we using the
233 legacy arrays as temporary storage. */
234
235 uint32_t uLeaf = g_aCpuidUnifyBits[i].uLeaf;
236 PCPUMCPUID pLegacyLeaf;
237 if (uLeaf < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmStd))
238 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmStd[uLeaf];
239 else if (uLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmExt))
240 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmExt[uLeaf - UINT32_C(0x80000000)];
241 else if (uLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmCentaur))
242 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmCentaur[uLeaf - UINT32_C(0xc0000000)];
243 else
244 continue;
245
246 uint32_t eax, ebx, ecx, edx;
247 ASMCpuIdExSlow(uLeaf, 0, 0, 0, &eax, &ebx, &ecx, &edx);
248
249 ASMAtomicAndU32(&pLegacyLeaf->uEcx, ecx | ~g_aCpuidUnifyBits[i].uEcx);
250 ASMAtomicAndU32(&pLegacyLeaf->uEdx, edx | ~g_aCpuidUnifyBits[i].uEdx);
251 }
252}
253
254
255/**
256 * Does Ring-0 CPUM initialization.
257 *
258 * This is mainly to check that the Host CPU mode is compatible
259 * with VBox.
260 *
261 * @returns VBox status code.
262 * @param pVM The cross context VM structure.
263 */
264VMMR0_INT_DECL(int) CPUMR0InitVM(PVMCC pVM)
265{
266 LogFlow(("CPUMR0Init: %p\n", pVM));
267 AssertCompile(sizeof(pVM->aCpus[0].cpum.s.Host.abXState) >= sizeof(pVM->aCpus[0].cpum.s.Guest.abXState));
268
269 /*
270 * Check CR0 & CR4 flags.
271 */
272 uint32_t u32CR0 = ASMGetCR0();
273 if ((u32CR0 & (X86_CR0_PE | X86_CR0_PG)) != (X86_CR0_PE | X86_CR0_PG)) /* a bit paranoid perhaps.. */
274 {
275 Log(("CPUMR0Init: PE or PG not set. cr0=%#x\n", u32CR0));
276 return VERR_UNSUPPORTED_CPU_MODE;
277 }
278
279 /*
280 * Check for sysenter and syscall usage.
281 */
282 if (ASMHasCpuId())
283 {
284 /*
285 * SYSENTER/SYSEXIT
286 *
287 * Intel docs claim you should test both the flag and family, model &
288 * stepping because some Pentium Pro CPUs have the SEP cpuid flag set,
289 * but don't support it. AMD CPUs may support this feature in legacy
290 * mode, they've banned it from long mode. Since we switch to 32-bit
291 * mode when entering raw-mode context the feature would become
292 * accessible again on AMD CPUs, so we have to check regardless of
293 * host bitness.
294 */
295 uint32_t u32CpuVersion;
296 uint32_t u32Dummy;
297 uint32_t fFeatures; /* (Used further down to check for MSRs, so don't clobber.) */
298 ASMCpuId(1, &u32CpuVersion, &u32Dummy, &u32Dummy, &fFeatures);
299 uint32_t const u32Family = u32CpuVersion >> 8;
300 uint32_t const u32Model = (u32CpuVersion >> 4) & 0xF;
301 uint32_t const u32Stepping = u32CpuVersion & 0xF;
302 if ( (fFeatures & X86_CPUID_FEATURE_EDX_SEP)
303 && ( u32Family != 6 /* (> pentium pro) */
304 || u32Model >= 3
305 || u32Stepping >= 3
306 || !ASMIsIntelCpu())
307 )
308 {
309 /*
310 * Read the MSR and see if it's in use or not.
311 */
312 uint32_t u32 = ASMRdMsr_Low(MSR_IA32_SYSENTER_CS);
313 if (u32)
314 {
315 pVM->cpum.s.fHostUseFlags |= CPUM_USE_SYSENTER;
316 Log(("CPUMR0Init: host uses sysenter cs=%08x%08x\n", ASMRdMsr_High(MSR_IA32_SYSENTER_CS), u32));
317 }
318 }
319
320 /*
321 * SYSCALL/SYSRET
322 *
323 * This feature is indicated by the SEP bit returned in EDX by CPUID
324 * function 0x80000001. Intel CPUs only supports this feature in
325 * long mode. Since we're not running 64-bit guests in raw-mode there
326 * are no issues with 32-bit intel hosts.
327 */
328 uint32_t cExt = 0;
329 ASMCpuId(0x80000000, &cExt, &u32Dummy, &u32Dummy, &u32Dummy);
330 if (RTX86IsValidExtRange(cExt))
331 {
332 uint32_t fExtFeaturesEDX = ASMCpuId_EDX(0x80000001);
333 if (fExtFeaturesEDX & X86_CPUID_EXT_FEATURE_EDX_SYSCALL)
334 {
335#ifdef RT_ARCH_X86
336 if (!ASMIsIntelCpu())
337#endif
338 {
339 uint64_t fEfer = ASMRdMsr(MSR_K6_EFER);
340 if (fEfer & MSR_K6_EFER_SCE)
341 {
342 pVM->cpum.s.fHostUseFlags |= CPUM_USE_SYSCALL;
343 Log(("CPUMR0Init: host uses syscall\n"));
344 }
345 }
346 }
347 }
348
349 /*
350 * Copy MSR_IA32_ARCH_CAPABILITIES bits over into the host and guest feature
351 * structure and as well as the guest MSR.
352 * Note! we assume this happens after the CPUMR3Init is done, so CPUID bits are settled.
353 */
354 pVM->cpum.s.HostFeatures.fArchRdclNo = 0;
355 pVM->cpum.s.HostFeatures.fArchIbrsAll = 0;
356 pVM->cpum.s.HostFeatures.fArchRsbOverride = 0;
357 pVM->cpum.s.HostFeatures.fArchVmmNeedNotFlushL1d = 0;
358 pVM->cpum.s.HostFeatures.fArchMdsNo = 0;
359 uint32_t const cStdRange = ASMCpuId_EAX(0);
360 if ( RTX86IsValidStdRange(cStdRange)
361 && cStdRange >= 7)
362 {
363 uint32_t fEdxFeatures = ASMCpuId_EDX(7);
364 if ( (fEdxFeatures & X86_CPUID_STEXT_FEATURE_EDX_ARCHCAP)
365 && (fFeatures & X86_CPUID_FEATURE_EDX_MSR))
366 {
367 /* Host: */
368 uint64_t fArchVal = ASMRdMsr(MSR_IA32_ARCH_CAPABILITIES);
369 pVM->cpum.s.HostFeatures.fArchRdclNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RDCL_NO);
370 pVM->cpum.s.HostFeatures.fArchIbrsAll = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_IBRS_ALL);
371 pVM->cpum.s.HostFeatures.fArchRsbOverride = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RSBO);
372 pVM->cpum.s.HostFeatures.fArchVmmNeedNotFlushL1d = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_VMM_NEED_NOT_FLUSH_L1D);
373 pVM->cpum.s.HostFeatures.fArchMdsNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_MDS_NO);
374
375 /* guest: */
376 if (!pVM->cpum.s.GuestFeatures.fArchCap)
377 fArchVal = 0;
378 else if (!pVM->cpum.s.GuestFeatures.fIbrs)
379 fArchVal &= ~MSR_IA32_ARCH_CAP_F_IBRS_ALL;
380 VMCC_FOR_EACH_VMCPU_STMT(pVM, pVCpu->cpum.s.GuestMsrs.msr.ArchCaps = fArchVal);
381 pVM->cpum.s.GuestFeatures.fArchRdclNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RDCL_NO);
382 pVM->cpum.s.GuestFeatures.fArchIbrsAll = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_IBRS_ALL);
383 pVM->cpum.s.GuestFeatures.fArchRsbOverride = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_RSBO);
384 pVM->cpum.s.GuestFeatures.fArchVmmNeedNotFlushL1d = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_VMM_NEED_NOT_FLUSH_L1D);
385 pVM->cpum.s.GuestFeatures.fArchMdsNo = RT_BOOL(fArchVal & MSR_IA32_ARCH_CAP_F_MDS_NO);
386 }
387 else
388 pVM->cpum.s.HostFeatures.fArchCap = 0;
389 }
390
391 /*
392 * Unify/cross check some CPUID feature bits on all available CPU cores
393 * and threads. We've seen CPUs where the monitor support differed.
394 *
395 * Because the hyper heap isn't always mapped into ring-0, we cannot
396 * access it from a RTMpOnAll callback. We use the legacy CPUID arrays
397 * as temp ring-0 accessible memory instead, ASSUMING that they're all
398 * up to date when we get here.
399 */
400 RTMpOnAll(cpumR0CheckCpuidLegacy, pVM, NULL);
401
402 for (uint32_t i = 0; i < RT_ELEMENTS(g_aCpuidUnifyBits); i++)
403 {
404 bool fIgnored;
405 uint32_t uLeaf = g_aCpuidUnifyBits[i].uLeaf;
406 PCPUMCPUIDLEAF pLeaf = cpumCpuIdGetLeafEx(pVM, uLeaf, 0, &fIgnored);
407 if (pLeaf)
408 {
409 PCPUMCPUID pLegacyLeaf;
410 if (uLeaf < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmStd))
411 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmStd[uLeaf];
412 else if (uLeaf - UINT32_C(0x80000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmExt))
413 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmExt[uLeaf - UINT32_C(0x80000000)];
414 else if (uLeaf - UINT32_C(0xc0000000) < RT_ELEMENTS(pVM->cpum.s.aGuestCpuIdPatmCentaur))
415 pLegacyLeaf = &pVM->cpum.s.aGuestCpuIdPatmCentaur[uLeaf - UINT32_C(0xc0000000)];
416 else
417 continue;
418
419 pLeaf->uEcx = pLegacyLeaf->uEcx;
420 pLeaf->uEdx = pLegacyLeaf->uEdx;
421 }
422 }
423
424 }
425
426
427 /*
428 * Check if debug registers are armed.
429 * This ASSUMES that DR7.GD is not set, or that it's handled transparently!
430 */
431 uint32_t u32DR7 = ASMGetDR7();
432 if (u32DR7 & X86_DR7_ENABLED_MASK)
433 {
434 VMCC_FOR_EACH_VMCPU_STMT(pVM, pVCpu->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS_HOST);
435 Log(("CPUMR0Init: host uses debug registers (dr7=%x)\n", u32DR7));
436 }
437
438 return VINF_SUCCESS;
439}
440
441
442/**
443 * Trap handler for device-not-available fault (\#NM).
444 * Device not available, FP or (F)WAIT instruction.
445 *
446 * @returns VBox status code.
447 * @retval VINF_SUCCESS if the guest FPU state is loaded.
448 * @retval VINF_EM_RAW_GUEST_TRAP if it is a guest trap.
449 * @retval VINF_CPUM_HOST_CR0_MODIFIED if we modified the host CR0.
450 *
451 * @param pVM The cross context VM structure.
452 * @param pVCpu The cross context virtual CPU structure.
453 */
454VMMR0_INT_DECL(int) CPUMR0Trap07Handler(PVMCC pVM, PVMCPUCC pVCpu)
455{
456 Assert(pVM->cpum.s.HostFeatures.fFxSaveRstor);
457 Assert(ASMGetCR4() & X86_CR4_OSFXSR);
458
459 /* If the FPU state has already been loaded, then it's a guest trap. */
460 if (CPUMIsGuestFPUStateActive(pVCpu))
461 {
462 Assert( ((pVCpu->cpum.s.Guest.cr0 & (X86_CR0_MP | X86_CR0_EM | X86_CR0_TS)) == (X86_CR0_MP | X86_CR0_TS))
463 || ((pVCpu->cpum.s.Guest.cr0 & (X86_CR0_MP | X86_CR0_EM | X86_CR0_TS)) == (X86_CR0_MP | X86_CR0_TS | X86_CR0_EM)));
464 return VINF_EM_RAW_GUEST_TRAP;
465 }
466
467 /*
468 * There are two basic actions:
469 * 1. Save host fpu and restore guest fpu.
470 * 2. Generate guest trap.
471 *
472 * When entering the hypervisor we'll always enable MP (for proper wait
473 * trapping) and TS (for intercepting all fpu/mmx/sse stuff). The EM flag
474 * is taken from the guest OS in order to get proper SSE handling.
475 *
476 *
477 * Actions taken depending on the guest CR0 flags:
478 *
479 * 3 2 1
480 * TS | EM | MP | FPUInstr | WAIT :: VMM Action
481 * ------------------------------------------------------------------------
482 * 0 | 0 | 0 | Exec | Exec :: Clear TS & MP, Save HC, Load GC.
483 * 0 | 0 | 1 | Exec | Exec :: Clear TS, Save HC, Load GC.
484 * 0 | 1 | 0 | #NM | Exec :: Clear TS & MP, Save HC, Load GC.
485 * 0 | 1 | 1 | #NM | Exec :: Clear TS, Save HC, Load GC.
486 * 1 | 0 | 0 | #NM | Exec :: Clear MP, Save HC, Load GC. (EM is already cleared.)
487 * 1 | 0 | 1 | #NM | #NM :: Go to guest taking trap there.
488 * 1 | 1 | 0 | #NM | Exec :: Clear MP, Save HC, Load GC. (EM is already set.)
489 * 1 | 1 | 1 | #NM | #NM :: Go to guest taking trap there.
490 */
491
492 switch (pVCpu->cpum.s.Guest.cr0 & (X86_CR0_MP | X86_CR0_EM | X86_CR0_TS))
493 {
494 case X86_CR0_MP | X86_CR0_TS:
495 case X86_CR0_MP | X86_CR0_TS | X86_CR0_EM:
496 return VINF_EM_RAW_GUEST_TRAP;
497 default:
498 break;
499 }
500
501 return CPUMR0LoadGuestFPU(pVM, pVCpu);
502}
503
504
505/**
506 * Saves the host-FPU/XMM state (if necessary) and (always) loads the guest-FPU
507 * state into the CPU.
508 *
509 * @returns VINF_SUCCESS on success, host CR0 unmodified.
510 * @returns VINF_CPUM_HOST_CR0_MODIFIED on success when the host CR0 was
511 * modified and VT-x needs to update the value in the VMCS.
512 *
513 * @param pVM The cross context VM structure.
514 * @param pVCpu The cross context virtual CPU structure.
515 */
516VMMR0_INT_DECL(int) CPUMR0LoadGuestFPU(PVMCC pVM, PVMCPUCC pVCpu)
517{
518 int rc;
519 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
520 Assert(!(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST));
521
522 /* Notify the support driver prior to loading the guest-FPU register state. */
523 SUPR0FpuBegin(false /* unused */);
524
525 if (!pVM->cpum.s.HostFeatures.fLeakyFxSR)
526 {
527 Assert(!(pVCpu->cpum.s.fUseFlags & CPUM_USED_MANUAL_XMM_RESTORE));
528 rc = cpumR0SaveHostRestoreGuestFPUState(&pVCpu->cpum.s);
529 }
530 else
531 {
532 Assert(!(pVCpu->cpum.s.fUseFlags & CPUM_USED_MANUAL_XMM_RESTORE) || (pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_HOST));
533 /** @todo r=ramshankar: Can't we used a cached value here
534 * instead of reading the MSR? host EFER doesn't usually
535 * change. */
536 uint64_t uHostEfer = ASMRdMsr(MSR_K6_EFER);
537 if (!(uHostEfer & MSR_K6_EFER_FFXSR))
538 rc = cpumR0SaveHostRestoreGuestFPUState(&pVCpu->cpum.s);
539 else
540 {
541 RTCCUINTREG const uSavedFlags = ASMIntDisableFlags();
542 pVCpu->cpum.s.fUseFlags |= CPUM_USED_MANUAL_XMM_RESTORE;
543 ASMWrMsr(MSR_K6_EFER, uHostEfer & ~MSR_K6_EFER_FFXSR);
544 rc = cpumR0SaveHostRestoreGuestFPUState(&pVCpu->cpum.s);
545 ASMWrMsr(MSR_K6_EFER, uHostEfer | MSR_K6_EFER_FFXSR);
546 ASMSetFlags(uSavedFlags);
547 }
548 }
549 Assert( (pVCpu->cpum.s.fUseFlags & (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST | CPUM_USED_FPU_SINCE_REM))
550 == (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST | CPUM_USED_FPU_SINCE_REM));
551 Assert(pVCpu->cpum.s.Guest.fUsedFpuGuest);
552 return rc;
553}
554
555
556/**
557 * Saves the guest FPU/XMM state if needed, restores the host FPU/XMM state as
558 * needed.
559 *
560 * @returns true if we saved the guest state.
561 * @param pVCpu The cross context virtual CPU structure.
562 */
563VMMR0_INT_DECL(bool) CPUMR0FpuStateMaybeSaveGuestAndRestoreHost(PVMCPUCC pVCpu)
564{
565 bool fSavedGuest;
566 Assert(pVCpu->CTX_SUFF(pVM)->cpum.s.HostFeatures.fFxSaveRstor);
567 Assert(ASMGetCR4() & X86_CR4_OSFXSR);
568 if (pVCpu->cpum.s.fUseFlags & (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST))
569 {
570 fSavedGuest = RT_BOOL(pVCpu->cpum.s.fUseFlags & CPUM_USED_FPU_GUEST);
571 Assert(fSavedGuest == pVCpu->cpum.s.Guest.fUsedFpuGuest);
572 if (!(pVCpu->cpum.s.fUseFlags & CPUM_USED_MANUAL_XMM_RESTORE))
573 cpumR0SaveGuestRestoreHostFPUState(&pVCpu->cpum.s);
574 else
575 {
576 /* Temporarily clear MSR_K6_EFER_FFXSR or else we'll be unable to
577 save/restore the XMM state with fxsave/fxrstor. */
578 uint64_t uHostEfer = ASMRdMsr(MSR_K6_EFER);
579 if (uHostEfer & MSR_K6_EFER_FFXSR)
580 {
581 RTCCUINTREG const uSavedFlags = ASMIntDisableFlags();
582 ASMWrMsr(MSR_K6_EFER, uHostEfer & ~MSR_K6_EFER_FFXSR);
583 cpumR0SaveGuestRestoreHostFPUState(&pVCpu->cpum.s);
584 ASMWrMsr(MSR_K6_EFER, uHostEfer | MSR_K6_EFER_FFXSR);
585 ASMSetFlags(uSavedFlags);
586 }
587 else
588 cpumR0SaveGuestRestoreHostFPUState(&pVCpu->cpum.s);
589 pVCpu->cpum.s.fUseFlags &= ~CPUM_USED_MANUAL_XMM_RESTORE;
590 }
591
592 /* Notify the support driver after loading the host-FPU register state. */
593 SUPR0FpuEnd(false /* unused */);
594 }
595 else
596 fSavedGuest = false;
597 Assert(!( pVCpu->cpum.s.fUseFlags
598 & (CPUM_USED_FPU_GUEST | CPUM_USED_FPU_HOST | CPUM_USED_MANUAL_XMM_RESTORE)));
599 Assert(!pVCpu->cpum.s.Guest.fUsedFpuGuest);
600 return fSavedGuest;
601}
602
603
604/**
605 * Saves the host debug state, setting CPUM_USED_HOST_DEBUG_STATE and loading
606 * DR7 with safe values.
607 *
608 * @returns VBox status code.
609 * @param pVCpu The cross context virtual CPU structure.
610 */
611static int cpumR0SaveHostDebugState(PVMCPUCC pVCpu)
612{
613 /*
614 * Save the host state.
615 */
616 pVCpu->cpum.s.Host.dr0 = ASMGetDR0();
617 pVCpu->cpum.s.Host.dr1 = ASMGetDR1();
618 pVCpu->cpum.s.Host.dr2 = ASMGetDR2();
619 pVCpu->cpum.s.Host.dr3 = ASMGetDR3();
620 pVCpu->cpum.s.Host.dr6 = ASMGetDR6();
621 /** @todo dr7 might already have been changed to 0x400; don't care right now as it's harmless. */
622 pVCpu->cpum.s.Host.dr7 = ASMGetDR7();
623
624 /* Preemption paranoia. */
625 ASMAtomicOrU32(&pVCpu->cpum.s.fUseFlags, CPUM_USED_DEBUG_REGS_HOST);
626
627 /*
628 * Make sure DR7 is harmless or else we could trigger breakpoints when
629 * load guest or hypervisor DRx values later.
630 */
631 if (pVCpu->cpum.s.Host.dr7 != X86_DR7_INIT_VAL)
632 ASMSetDR7(X86_DR7_INIT_VAL);
633
634 return VINF_SUCCESS;
635}
636
637
638/**
639 * Saves the guest DRx state residing in host registers and restore the host
640 * register values.
641 *
642 * The guest DRx state is only saved if CPUMR0LoadGuestDebugState was called,
643 * since it's assumed that we're shadowing the guest DRx register values
644 * accurately when using the combined hypervisor debug register values
645 * (CPUMR0LoadHyperDebugState).
646 *
647 * @returns true if either guest or hypervisor debug registers were loaded.
648 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
649 * @param fDr6 Whether to include DR6 or not.
650 * @thread EMT(pVCpu)
651 */
652VMMR0_INT_DECL(bool) CPUMR0DebugStateMaybeSaveGuestAndRestoreHost(PVMCPUCC pVCpu, bool fDr6)
653{
654 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
655 bool const fDrXLoaded = RT_BOOL(pVCpu->cpum.s.fUseFlags & (CPUM_USED_DEBUG_REGS_GUEST | CPUM_USED_DEBUG_REGS_HYPER));
656
657 /*
658 * Do we need to save the guest DRx registered loaded into host registers?
659 * (DR7 and DR6 (if fDr6 is true) are left to the caller.)
660 */
661 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_GUEST)
662 {
663 pVCpu->cpum.s.Guest.dr[0] = ASMGetDR0();
664 pVCpu->cpum.s.Guest.dr[1] = ASMGetDR1();
665 pVCpu->cpum.s.Guest.dr[2] = ASMGetDR2();
666 pVCpu->cpum.s.Guest.dr[3] = ASMGetDR3();
667 if (fDr6)
668 pVCpu->cpum.s.Guest.dr[6] = ASMGetDR6();
669 }
670 ASMAtomicAndU32(&pVCpu->cpum.s.fUseFlags, ~(CPUM_USED_DEBUG_REGS_GUEST | CPUM_USED_DEBUG_REGS_HYPER));
671
672 /*
673 * Restore the host's debug state. DR0-3, DR6 and only then DR7!
674 */
675 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_HOST)
676 {
677 /* A bit of paranoia first... */
678 uint64_t uCurDR7 = ASMGetDR7();
679 if (uCurDR7 != X86_DR7_INIT_VAL)
680 ASMSetDR7(X86_DR7_INIT_VAL);
681
682 ASMSetDR0(pVCpu->cpum.s.Host.dr0);
683 ASMSetDR1(pVCpu->cpum.s.Host.dr1);
684 ASMSetDR2(pVCpu->cpum.s.Host.dr2);
685 ASMSetDR3(pVCpu->cpum.s.Host.dr3);
686 /** @todo consider only updating if they differ, esp. DR6. Need to figure how
687 * expensive DRx reads are over DRx writes. */
688 ASMSetDR6(pVCpu->cpum.s.Host.dr6);
689 ASMSetDR7(pVCpu->cpum.s.Host.dr7);
690
691 ASMAtomicAndU32(&pVCpu->cpum.s.fUseFlags, ~CPUM_USED_DEBUG_REGS_HOST);
692 }
693
694 return fDrXLoaded;
695}
696
697
698/**
699 * Saves the guest DRx state if it resides host registers.
700 *
701 * This does NOT clear any use flags, so the host registers remains loaded with
702 * the guest DRx state upon return. The purpose is only to make sure the values
703 * in the CPU context structure is up to date.
704 *
705 * @returns true if the host registers contains guest values, false if not.
706 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
707 * @param fDr6 Whether to include DR6 or not.
708 * @thread EMT(pVCpu)
709 */
710VMMR0_INT_DECL(bool) CPUMR0DebugStateMaybeSaveGuest(PVMCPUCC pVCpu, bool fDr6)
711{
712 /*
713 * Do we need to save the guest DRx registered loaded into host registers?
714 * (DR7 and DR6 (if fDr6 is true) are left to the caller.)
715 */
716 if (pVCpu->cpum.s.fUseFlags & CPUM_USED_DEBUG_REGS_GUEST)
717 {
718 pVCpu->cpum.s.Guest.dr[0] = ASMGetDR0();
719 pVCpu->cpum.s.Guest.dr[1] = ASMGetDR1();
720 pVCpu->cpum.s.Guest.dr[2] = ASMGetDR2();
721 pVCpu->cpum.s.Guest.dr[3] = ASMGetDR3();
722 if (fDr6)
723 pVCpu->cpum.s.Guest.dr[6] = ASMGetDR6();
724 return true;
725 }
726 return false;
727}
728
729
730/**
731 * Lazily sync in the debug state.
732 *
733 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
734 * @param fDr6 Whether to include DR6 or not.
735 * @thread EMT(pVCpu)
736 */
737VMMR0_INT_DECL(void) CPUMR0LoadGuestDebugState(PVMCPUCC pVCpu, bool fDr6)
738{
739 /*
740 * Save the host state and disarm all host BPs.
741 */
742 cpumR0SaveHostDebugState(pVCpu);
743 Assert(ASMGetDR7() == X86_DR7_INIT_VAL);
744
745 /*
746 * Activate the guest state DR0-3.
747 * DR7 and DR6 (if fDr6 is true) are left to the caller.
748 */
749 ASMSetDR0(pVCpu->cpum.s.Guest.dr[0]);
750 ASMSetDR1(pVCpu->cpum.s.Guest.dr[1]);
751 ASMSetDR2(pVCpu->cpum.s.Guest.dr[2]);
752 ASMSetDR3(pVCpu->cpum.s.Guest.dr[3]);
753 if (fDr6)
754 ASMSetDR6(pVCpu->cpum.s.Guest.dr[6]);
755
756 ASMAtomicOrU32(&pVCpu->cpum.s.fUseFlags, CPUM_USED_DEBUG_REGS_GUEST);
757}
758
759
760/**
761 * Lazily sync in the hypervisor debug state
762 *
763 * @returns VBox status code.
764 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
765 * @param fDr6 Whether to include DR6 or not.
766 * @thread EMT(pVCpu)
767 */
768VMMR0_INT_DECL(void) CPUMR0LoadHyperDebugState(PVMCPUCC pVCpu, bool fDr6)
769{
770 /*
771 * Save the host state and disarm all host BPs.
772 */
773 cpumR0SaveHostDebugState(pVCpu);
774 Assert(ASMGetDR7() == X86_DR7_INIT_VAL);
775
776 /*
777 * Make sure the hypervisor values are up to date.
778 */
779 CPUMRecalcHyperDRx(pVCpu, UINT8_MAX /* no loading, please */);
780
781 /*
782 * Activate the guest state DR0-3.
783 * DR7 and DR6 (if fDr6 is true) are left to the caller.
784 */
785 ASMSetDR0(pVCpu->cpum.s.Hyper.dr[0]);
786 ASMSetDR1(pVCpu->cpum.s.Hyper.dr[1]);
787 ASMSetDR2(pVCpu->cpum.s.Hyper.dr[2]);
788 ASMSetDR3(pVCpu->cpum.s.Hyper.dr[3]);
789 if (fDr6)
790 ASMSetDR6(X86_DR6_INIT_VAL);
791
792 ASMAtomicOrU32(&pVCpu->cpum.s.fUseFlags, CPUM_USED_DEBUG_REGS_HYPER);
793}
794
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