VirtualBox

source: vbox/trunk/src/VBox/VMM/include/CPUMInternal.h@ 61317

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

CPUM,HM: CPUM must tell VT-x that it modified the host CR0 because it caches the value in the VMCS and state corruption may ensue if it restores it because we'll take a #NM when saving the guest state, probably ending up with the FPU state of the EMT instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 18.7 KB
Line 
1/* $Id: CPUMInternal.h 61317 2016-05-31 04:55:10Z vboxsync $ */
2/** @file
3 * CPUM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ___CPUMInternal_h
19#define ___CPUMInternal_h
20
21#ifndef VBOX_FOR_DTRACE_LIB
22# include <VBox/cdefs.h>
23# include <VBox/types.h>
24# include <VBox/vmm/stam.h>
25# include <iprt/x86.h>
26#else
27# pragma D depends_on library x86.d
28# pragma D depends_on library cpumctx.d
29# pragma D depends_on library cpum.d
30
31/* Some fudging. */
32typedef uint64_t STAMCOUNTER;
33#endif
34
35
36
37
38/** @defgroup grp_cpum_int Internals
39 * @ingroup grp_cpum
40 * @internal
41 * @{
42 */
43
44/** Flags and types for CPUM fault handlers
45 * @{ */
46/** Type: Load DS */
47#define CPUM_HANDLER_DS 1
48/** Type: Load ES */
49#define CPUM_HANDLER_ES 2
50/** Type: Load FS */
51#define CPUM_HANDLER_FS 3
52/** Type: Load GS */
53#define CPUM_HANDLER_GS 4
54/** Type: IRET */
55#define CPUM_HANDLER_IRET 5
56/** Type mask. */
57#define CPUM_HANDLER_TYPEMASK 0xff
58/** If set EBP points to the CPUMCTXCORE that's being used. */
59#define CPUM_HANDLER_CTXCORE_IN_EBP RT_BIT(31)
60/** @} */
61
62
63/** Use flags (CPUM::fUseFlags).
64 * (Don't forget to sync this with CPUMInternal.mac !)
65 * @note Part of saved state.
66 * @{ */
67/** Indicates that we've saved the host FPU, SSE, whatever state and that it
68 * needs to be restored. */
69#define CPUM_USED_FPU_HOST RT_BIT(0)
70/** Indicates that we've loaded the guest FPU, SSE, whatever state and that it
71 * needs to be saved. */
72#define CPUM_USED_FPU_GUEST RT_BIT(10)
73/** Used the guest FPU, SSE or such stuff since last we were in REM.
74 * REM syncing is clearing this, lazy FPU is setting it. */
75#define CPUM_USED_FPU_SINCE_REM RT_BIT(1)
76/** The XMM state was manually restored. (AMD only) */
77#define CPUM_USED_MANUAL_XMM_RESTORE RT_BIT(2)
78
79/** Host OS is using SYSENTER and we must NULL the CS. */
80#define CPUM_USE_SYSENTER RT_BIT(3)
81/** Host OS is using SYSENTER and we must NULL the CS. */
82#define CPUM_USE_SYSCALL RT_BIT(4)
83
84/** Debug registers are used by host and that DR7 and DR6 must be saved and
85 * disabled when switching to raw-mode. */
86#define CPUM_USE_DEBUG_REGS_HOST RT_BIT(5)
87/** Records that we've saved the host DRx registers.
88 * In ring-0 this means all (DR0-7), while in raw-mode context this means DR0-3
89 * since DR6 and DR7 are covered by CPUM_USE_DEBUG_REGS_HOST. */
90#define CPUM_USED_DEBUG_REGS_HOST RT_BIT(6)
91/** Set to indicate that we should save host DR0-7 and load the hypervisor debug
92 * registers in the raw-mode world switchers. (See CPUMRecalcHyperDRx.) */
93#define CPUM_USE_DEBUG_REGS_HYPER RT_BIT(7)
94/** Used in ring-0 to indicate that we have loaded the hypervisor debug
95 * registers. */
96#define CPUM_USED_DEBUG_REGS_HYPER RT_BIT(8)
97/** Used in ring-0 to indicate that we have loaded the guest debug
98 * registers (DR0-3 and maybe DR6) for direct use by the guest.
99 * DR7 (and AMD-V DR6) are handled via the VMCB. */
100#define CPUM_USED_DEBUG_REGS_GUEST RT_BIT(9)
101
102/** Sync the FPU state on next entry (32->64 switcher only). */
103#define CPUM_SYNC_FPU_STATE RT_BIT(16)
104/** Sync the debug state on next entry (32->64 switcher only). */
105#define CPUM_SYNC_DEBUG_REGS_GUEST RT_BIT(17)
106/** Sync the debug state on next entry (32->64 switcher only).
107 * Almost the same as CPUM_USE_DEBUG_REGS_HYPER in the raw-mode switchers. */
108#define CPUM_SYNC_DEBUG_REGS_HYPER RT_BIT(18)
109/** Host CPU requires fxsave/fxrstor leaky bit handling. */
110#define CPUM_USE_FFXSR_LEAKY RT_BIT(19)
111/** Set if the VM supports long-mode. */
112#define CPUM_USE_SUPPORTS_LONGMODE RT_BIT(20)
113/** @} */
114
115
116/** @name CPUM Saved State Version.
117 * @{ */
118/** The current saved state version. */
119#define CPUM_SAVED_STATE_VERSION CPUM_SAVED_STATE_VERSION_XSAVE
120/** The saved state version including XSAVE state. */
121#define CPUM_SAVED_STATE_VERSION_XSAVE 17
122/** The saved state version with good CPUID leaf count. */
123#define CPUM_SAVED_STATE_VERSION_GOOD_CPUID_COUNT 16
124/** CPUID changes with explode forgetting to update the leaf count on
125 * restore, resulting in garbage being saved restoring+saving old states). */
126#define CPUM_SAVED_STATE_VERSION_BAD_CPUID_COUNT 15
127/** The saved state version before the CPUIDs changes. */
128#define CPUM_SAVED_STATE_VERSION_PUT_STRUCT 14
129/** The saved state version before using SSMR3PutStruct. */
130#define CPUM_SAVED_STATE_VERSION_MEM 13
131/** The saved state version before introducing the MSR size field. */
132#define CPUM_SAVED_STATE_VERSION_NO_MSR_SIZE 12
133/** The saved state version of 3.2, 3.1 and 3.3 trunk before the hidden
134 * selector register change (CPUM_CHANGED_HIDDEN_SEL_REGS_INVALID). */
135#define CPUM_SAVED_STATE_VERSION_VER3_2 11
136/** The saved state version of 3.0 and 3.1 trunk before the teleportation
137 * changes. */
138#define CPUM_SAVED_STATE_VERSION_VER3_0 10
139/** The saved state version for the 2.1 trunk before the MSR changes. */
140#define CPUM_SAVED_STATE_VERSION_VER2_1_NOMSR 9
141/** The saved state version of 2.0, used for backwards compatibility. */
142#define CPUM_SAVED_STATE_VERSION_VER2_0 8
143/** The saved state version of 1.6, used for backwards compatibility. */
144#define CPUM_SAVED_STATE_VERSION_VER1_6 6
145/** @} */
146
147
148/**
149 * CPU info
150 */
151typedef struct CPUMINFO
152{
153 /** The number of MSR ranges (CPUMMSRRANGE) in the array pointed to below. */
154 uint32_t cMsrRanges;
155 /** Mask applied to ECX before looking up the MSR for a RDMSR/WRMSR
156 * instruction. Older hardware has been observed to ignore higher bits. */
157 uint32_t fMsrMask;
158
159 /** The number of CPUID leaves (CPUMCPUIDLEAF) in the array pointed to below. */
160 uint32_t cCpuIdLeaves;
161 /** The index of the first extended CPUID leaf in the array.
162 * Set to cCpuIdLeaves if none present. */
163 uint32_t iFirstExtCpuIdLeaf;
164 /** Alignment padding. */
165 uint32_t uPadding;
166 /** How to handle unknown CPUID leaves. */
167 CPUMUNKNOWNCPUID enmUnknownCpuIdMethod;
168 /** For use with CPUMUNKNOWNCPUID_DEFAULTS (DB & VM),
169 * CPUMUNKNOWNCPUID_LAST_STD_LEAF (VM) and CPUMUNKNOWNCPUID_LAST_STD_LEAF_WITH_ECX (VM). */
170 CPUMCPUID DefCpuId;
171
172 /** Scalable bus frequency used for reporting other frequencies. */
173 uint64_t uScalableBusFreq;
174
175 /** Pointer to the MSR ranges (ring-0 pointer). */
176 R0PTRTYPE(PCPUMMSRRANGE) paMsrRangesR0;
177 /** Pointer to the CPUID leaves (ring-0 pointer). */
178 R0PTRTYPE(PCPUMCPUIDLEAF) paCpuIdLeavesR0;
179
180 /** Pointer to the MSR ranges (ring-3 pointer). */
181 R3PTRTYPE(PCPUMMSRRANGE) paMsrRangesR3;
182 /** Pointer to the CPUID leaves (ring-3 pointer). */
183 R3PTRTYPE(PCPUMCPUIDLEAF) paCpuIdLeavesR3;
184
185 /** Pointer to the MSR ranges (raw-mode context pointer). */
186 RCPTRTYPE(PCPUMMSRRANGE) paMsrRangesRC;
187 /** Pointer to the CPUID leaves (raw-mode context pointer). */
188 RCPTRTYPE(PCPUMCPUIDLEAF) paCpuIdLeavesRC;
189} CPUMINFO;
190/** Pointer to a CPU info structure. */
191typedef CPUMINFO *PCPUMINFO;
192/** Pointer to a const CPU info structure. */
193typedef CPUMINFO const *CPCPUMINFO;
194
195
196/**
197 * The saved host CPU state.
198 */
199typedef struct CPUMHOSTCTX
200{
201 /** General purpose register, selectors, flags and more
202 * @{ */
203#if HC_ARCH_BITS == 64
204 /** General purpose register ++
205 * { */
206 /*uint64_t rax; - scratch*/
207 uint64_t rbx;
208 /*uint64_t rcx; - scratch*/
209 /*uint64_t rdx; - scratch*/
210 uint64_t rdi;
211 uint64_t rsi;
212 uint64_t rbp;
213 uint64_t rsp;
214 /*uint64_t r8; - scratch*/
215 /*uint64_t r9; - scratch*/
216 uint64_t r10;
217 uint64_t r11;
218 uint64_t r12;
219 uint64_t r13;
220 uint64_t r14;
221 uint64_t r15;
222 /*uint64_t rip; - scratch*/
223 uint64_t rflags;
224#endif
225
226#if HC_ARCH_BITS == 32
227 /*uint32_t eax; - scratch*/
228 uint32_t ebx;
229 /*uint32_t ecx; - scratch*/
230 /*uint32_t edx; - scratch*/
231 uint32_t edi;
232 uint32_t esi;
233 uint32_t ebp;
234 X86EFLAGS eflags;
235 /*uint32_t eip; - scratch*/
236 /* lss pair! */
237 uint32_t esp;
238#endif
239 /** @} */
240
241 /** Selector registers
242 * @{ */
243 RTSEL ss;
244 RTSEL ssPadding;
245 RTSEL gs;
246 RTSEL gsPadding;
247 RTSEL fs;
248 RTSEL fsPadding;
249 RTSEL es;
250 RTSEL esPadding;
251 RTSEL ds;
252 RTSEL dsPadding;
253 RTSEL cs;
254 RTSEL csPadding;
255 /** @} */
256
257#if HC_ARCH_BITS == 32
258 /** Control registers.
259 * @{ */
260 uint32_t cr0;
261 /*uint32_t cr2; - scratch*/
262 uint32_t cr3;
263 uint32_t cr4;
264 /** The CR0 FPU state in HM mode. Can't use cr0 here because the
265 * 64-bit-on-32-bit-host world switches is using it. */
266 uint32_t cr0Fpu;
267 /** @} */
268
269 /** Debug registers.
270 * @{ */
271 uint32_t dr0;
272 uint32_t dr1;
273 uint32_t dr2;
274 uint32_t dr3;
275 uint32_t dr6;
276 uint32_t dr7;
277 /** @} */
278
279 /** Global Descriptor Table register. */
280 X86XDTR32 gdtr;
281 uint16_t gdtrPadding;
282 /** Interrupt Descriptor Table register. */
283 X86XDTR32 idtr;
284 uint16_t idtrPadding;
285 /** The task register. */
286 RTSEL ldtr;
287 RTSEL ldtrPadding;
288 /** The task register. */
289 RTSEL tr;
290 RTSEL trPadding;
291
292 /** The sysenter msr registers.
293 * This member is not used by the hypervisor context. */
294 CPUMSYSENTER SysEnter;
295
296 /** MSRs
297 * @{ */
298 uint64_t efer;
299 /** @} */
300
301 /* padding to get 64byte aligned size */
302 uint8_t auPadding[20];
303
304#elif HC_ARCH_BITS == 64
305
306 /** Control registers.
307 * @{ */
308 /** The CR0 FPU state in HM mode. */
309 uint64_t cr0;
310 /*uint64_t cr2; - scratch*/
311 uint64_t cr3;
312 uint64_t cr4;
313 uint64_t cr8;
314 /** @} */
315
316 /** Debug registers.
317 * @{ */
318 uint64_t dr0;
319 uint64_t dr1;
320 uint64_t dr2;
321 uint64_t dr3;
322 uint64_t dr6;
323 uint64_t dr7;
324 /** @} */
325
326 /** Global Descriptor Table register. */
327 X86XDTR64 gdtr;
328 uint16_t gdtrPadding;
329 /** Interrupt Descriptor Table register. */
330 X86XDTR64 idtr;
331 uint16_t idtrPadding;
332 /** The task register. */
333 RTSEL ldtr;
334 RTSEL ldtrPadding;
335 /** The task register. */
336 RTSEL tr;
337 RTSEL trPadding;
338
339 /** MSRs
340 * @{ */
341 CPUMSYSENTER SysEnter;
342 uint64_t FSbase;
343 uint64_t GSbase;
344 uint64_t efer;
345 /** @} */
346
347 /* padding to get 64byte aligned size */
348 uint8_t auPadding[4];
349
350#else
351# error HC_ARCH_BITS not defined or unsupported
352#endif
353
354 /** Pointer to the FPU/SSE/AVX/XXXX state raw-mode mapping. */
355 RCPTRTYPE(PX86XSAVEAREA) pXStateRC;
356 /** Pointer to the FPU/SSE/AVX/XXXX state ring-0 mapping. */
357 R0PTRTYPE(PX86XSAVEAREA) pXStateR0;
358 /** Pointer to the FPU/SSE/AVX/XXXX state ring-3 mapping. */
359 R3PTRTYPE(PX86XSAVEAREA) pXStateR3;
360 /** The XCR0 register. */
361 uint64_t xcr0;
362 /** The mask to pass to XSAVE/XRSTOR in EDX:EAX. If zero we use
363 * FXSAVE/FXRSTOR (since bit 0 will always be set, we only need to test it). */
364 uint64_t fXStateMask;
365} CPUMHOSTCTX;
366#ifndef VBOX_FOR_DTRACE_LIB
367AssertCompileSizeAlignment(CPUMHOSTCTX, 64);
368#endif
369/** Pointer to the saved host CPU state. */
370typedef CPUMHOSTCTX *PCPUMHOSTCTX;
371
372
373/**
374 * CPUM Data (part of VM)
375 */
376typedef struct CPUM
377{
378 /** Offset from CPUM to CPUMCPU for the first CPU. */
379 uint32_t offCPUMCPU0;
380
381 /** Use flags.
382 * These flags indicates which CPU features the host uses.
383 */
384 uint32_t fHostUseFlags;
385
386 /** CR4 mask */
387 struct
388 {
389 uint32_t AndMask; /**< @todo Move these to the per-CPU structure and fix the switchers. Saves a register! */
390 uint32_t OrMask;
391 } CR4;
392
393 /** The (more) portable CPUID level. */
394 uint8_t u8PortableCpuIdLevel;
395 /** Indicates that a state restore is pending.
396 * This is used to verify load order dependencies (PGM). */
397 bool fPendingRestore;
398 uint8_t abPadding0[6];
399
400 /** XSAVE/XRTOR components we can expose to the guest mask. */
401 uint64_t fXStateGuestMask;
402 /** XSAVE/XRSTOR host mask. Only state components in this mask can be exposed
403 * to the guest. This is 0 if no XSAVE/XRSTOR bits can be exposed. */
404 uint64_t fXStateHostMask;
405 uint8_t abPadding1[24];
406
407 /** Host CPU feature information.
408 * Externaly visible via the VM structure, aligned on 64-byte boundrary. */
409 CPUMFEATURES HostFeatures;
410 /** Guest CPU feature information.
411 * Externaly visible via that VM structure, aligned with HostFeatures. */
412 CPUMFEATURES GuestFeatures;
413 /** Guest CPU info. */
414 CPUMINFO GuestInfo;
415
416
417 /** The standard set of CpuId leaves. */
418 CPUMCPUID aGuestCpuIdPatmStd[6];
419 /** The extended set of CpuId leaves. */
420 CPUMCPUID aGuestCpuIdPatmExt[10];
421 /** The centaur set of CpuId leaves. */
422 CPUMCPUID aGuestCpuIdPatmCentaur[4];
423
424 /** @name MSR statistics.
425 * @{ */
426 STAMCOUNTER cMsrWrites;
427 STAMCOUNTER cMsrWritesToIgnoredBits;
428 STAMCOUNTER cMsrWritesRaiseGp;
429 STAMCOUNTER cMsrWritesUnknown;
430 STAMCOUNTER cMsrReads;
431 STAMCOUNTER cMsrReadsRaiseGp;
432 STAMCOUNTER cMsrReadsUnknown;
433 /** @} */
434} CPUM;
435#ifndef VBOX_FOR_DTRACE_LIB
436AssertCompileMemberOffset(CPUM, HostFeatures, 64);
437AssertCompileMemberOffset(CPUM, GuestFeatures, 96);
438#endif
439/** Pointer to the CPUM instance data residing in the shared VM structure. */
440typedef CPUM *PCPUM;
441
442/**
443 * CPUM Data (part of VMCPU)
444 */
445typedef struct CPUMCPU
446{
447 /**
448 * Guest context.
449 * Aligned on a 64-byte boundary.
450 */
451 CPUMCTX Guest;
452
453 /**
454 * Guest context - misc MSRs
455 * Aligned on a 64-byte boundary.
456 */
457 CPUMCTXMSRS GuestMsrs;
458
459 /** Use flags.
460 * These flags indicates both what is to be used and what has been used.
461 */
462 uint32_t fUseFlags;
463
464 /** Changed flags.
465 * These flags indicates to REM (and others) which important guest
466 * registers which has been changed since last time the flags were cleared.
467 * See the CPUM_CHANGED_* defines for what we keep track of.
468 */
469 uint32_t fChanged;
470
471 /** Offset from CPUM to CPUMCPU. */
472 uint32_t offCPUM;
473
474 /** Temporary storage for the return code of the function called in the
475 * 32-64 switcher. */
476 uint32_t u32RetCode;
477
478#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
479 /** The address of the APIC mapping, NULL if no APIC.
480 * Call CPUMR0SetLApic to update this before doing a world switch. */
481 RTHCPTR pvApicBase;
482 /** Used by the world switcher code to store which vectors needs restoring on
483 * the way back. */
484 uint32_t fApicDisVectors;
485 /** Set if the CPU has the X2APIC mode enabled.
486 * Call CPUMR0SetLApic to update this before doing a world switch. */
487 bool fX2Apic;
488#else
489 uint8_t abPadding3[(HC_ARCH_BITS == 64 ? 8 : 4) + 4 + 1];
490#endif
491
492 /** Have we entered raw-mode? */
493 bool fRawEntered;
494 /** Have we entered the recompiler? */
495 bool fRemEntered;
496
497 /** Align the next member on a 64-bit boundrary. */
498 uint8_t abPadding2[64 - 16 - (HC_ARCH_BITS == 64 ? 8 : 4) - 4 - 1 - 2];
499
500 /** Saved host context. Only valid while inside RC or HM contexts.
501 * Must be aligned on a 64-byte boundary. */
502 CPUMHOSTCTX Host;
503 /** Hypervisor context. Must be aligned on a 64-byte boundary. */
504 CPUMCTX Hyper;
505
506#ifdef VBOX_WITH_CRASHDUMP_MAGIC
507 uint8_t aMagic[56];
508 uint64_t uMagic;
509#endif
510} CPUMCPU;
511/** Pointer to the CPUMCPU instance data residing in the shared VMCPU structure. */
512typedef CPUMCPU *PCPUMCPU;
513
514#ifndef VBOX_FOR_DTRACE_LIB
515RT_C_DECLS_BEGIN
516
517PCPUMCPUIDLEAF cpumCpuIdGetLeaf(PVM pVM, uint32_t uLeaf);
518PCPUMCPUIDLEAF cpumCpuIdGetLeafEx(PVM pVM, uint32_t uLeaf, uint32_t uSubLeaf, bool *pfExactSubLeafHit);
519
520# ifdef IN_RING3
521int cpumR3DbgInit(PVM pVM);
522int cpumR3CpuIdExplodeFeatures(PCCPUMCPUIDLEAF paLeaves, uint32_t cLeaves, PCPUMFEATURES pFeatures);
523int cpumR3InitCpuIdAndMsrs(PVM pVM);
524void cpumR3SaveCpuId(PVM pVM, PSSMHANDLE pSSM);
525int cpumR3LoadCpuId(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion);
526int cpumR3LoadCpuIdPre32(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion);
527DECLCALLBACK(void) cpumR3CpuIdInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
528
529int cpumR3DbGetCpuInfo(const char *pszName, PCPUMINFO pInfo);
530int cpumR3MsrRangesInsert(PVM pVM, PCPUMMSRRANGE *ppaMsrRanges, uint32_t *pcMsrRanges, PCCPUMMSRRANGE pNewRange);
531int cpumR3MsrApplyFudge(PVM pVM);
532int cpumR3MsrRegStats(PVM pVM);
533int cpumR3MsrStrictInitChecks(void);
534PCPUMMSRRANGE cpumLookupMsrRange(PVM pVM, uint32_t idMsr);
535# endif
536
537# ifdef IN_RC
538DECLASM(int) cpumHandleLazyFPUAsm(PCPUMCPU pCPUM);
539# endif
540
541# ifdef IN_RING0
542DECLASM(int) cpumR0SaveHostRestoreGuestFPUState(PCPUMCPU pCPUM);
543DECLASM(void) cpumR0SaveGuestRestoreHostFPUState(PCPUMCPU pCPUM);
544# if ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS)
545DECLASM(void) cpumR0RestoreHostFPUState(PCPUMCPU pCPUM);
546# endif
547# endif
548
549# if defined(IN_RC) || defined(IN_RING0)
550DECLASM(int) cpumRZSaveHostFPUState(PCPUMCPU pCPUM);
551DECLASM(void) cpumRZSaveGuestFpuState(PCPUMCPU pCPUM, bool fLeaveFpuAccessible);
552DECLASM(void) cpumRZSaveGuestSseRegisters(PCPUMCPU pCPUM);
553# endif
554
555RT_C_DECLS_END
556#endif /* !VBOX_FOR_DTRACE_LIB */
557
558/** @} */
559
560#endif
561
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