VirtualBox

source: vbox/trunk/src/VBox/VMM/HWACCMInternal.h@ 13533

Last change on this file since 13533 was 13533, checked in by vboxsync, 16 years ago

Extra assertions to check for unexpected use conditions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 20.3 KB
Line 
1/* $Id: HWACCMInternal.h 13533 2008-10-23 13:00:28Z vboxsync $ */
2/** @file
3 * HWACCM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#ifndef ___HWACCMInternal_h
23#define ___HWACCMInternal_h
24
25#include <VBox/cdefs.h>
26#include <VBox/types.h>
27#include <VBox/em.h>
28#include <VBox/stam.h>
29#include <VBox/dis.h>
30#include <VBox/hwaccm.h>
31#include <VBox/pgm.h>
32#include <VBox/cpum.h>
33#include <iprt/memobj.h>
34#include <iprt/cpuset.h>
35#include <iprt/mp.h>
36
37#if HC_ARCH_BITS == 64
38/* Enable 64 bits guest support. */
39# define VBOX_ENABLE_64_BITS_GUESTS
40#endif
41
42#define HWACCM_VMX_EMULATE_REALMODE
43#define HWACCM_VTX_WITH_EPT
44#define HWACCM_VTX_WITH_VPID
45
46__BEGIN_DECLS
47
48
49/** @defgroup grp_hwaccm_int Internal
50 * @ingroup grp_hwaccm
51 * @internal
52 * @{
53 */
54
55
56/**
57 * Converts a HWACCM pointer into a VM pointer.
58 * @returns Pointer to the VM structure the EM is part of.
59 * @param pHWACCM Pointer to HWACCM instance data.
60 */
61#define HWACCM2VM(pHWACCM) ( (PVM)((char*)pHWACCM - pHWACCM->offVM) )
62
63/** Maximum number of exit reason statistics counters. */
64#define MAX_EXITREASON_STAT 0x100
65#define MASK_EXITREASON_STAT 0xff
66
67/** @name Changed flags
68 * These flags are used to keep track of which important registers that
69 * have been changed since last they were reset.
70 * @{
71 */
72#define HWACCM_CHANGED_GUEST_FPU RT_BIT(0)
73#define HWACCM_CHANGED_GUEST_CR0 RT_BIT(1)
74#define HWACCM_CHANGED_GUEST_CR3 RT_BIT(2)
75#define HWACCM_CHANGED_GUEST_CR4 RT_BIT(3)
76#define HWACCM_CHANGED_GUEST_GDTR RT_BIT(4)
77#define HWACCM_CHANGED_GUEST_IDTR RT_BIT(5)
78#define HWACCM_CHANGED_GUEST_LDTR RT_BIT(6)
79#define HWACCM_CHANGED_GUEST_TR RT_BIT(7)
80#define HWACCM_CHANGED_GUEST_SYSENTER_MSR RT_BIT(8)
81#define HWACCM_CHANGED_GUEST_SEGMENT_REGS RT_BIT(9)
82#define HWACCM_CHANGED_GUEST_DEBUG RT_BIT(10)
83#define HWACCM_CHANGED_HOST_CONTEXT RT_BIT(11)
84
85#define HWACCM_CHANGED_ALL ( HWACCM_CHANGED_GUEST_SEGMENT_REGS \
86 | HWACCM_CHANGED_GUEST_CR0 \
87 | HWACCM_CHANGED_GUEST_CR3 \
88 | HWACCM_CHANGED_GUEST_CR4 \
89 | HWACCM_CHANGED_GUEST_GDTR \
90 | HWACCM_CHANGED_GUEST_IDTR \
91 | HWACCM_CHANGED_GUEST_LDTR \
92 | HWACCM_CHANGED_GUEST_TR \
93 | HWACCM_CHANGED_GUEST_SYSENTER_MSR \
94 | HWACCM_CHANGED_GUEST_FPU \
95 | HWACCM_CHANGED_GUEST_DEBUG \
96 | HWACCM_CHANGED_HOST_CONTEXT)
97
98#define HWACCM_CHANGED_ALL_GUEST ( HWACCM_CHANGED_GUEST_SEGMENT_REGS \
99 | HWACCM_CHANGED_GUEST_CR0 \
100 | HWACCM_CHANGED_GUEST_CR3 \
101 | HWACCM_CHANGED_GUEST_CR4 \
102 | HWACCM_CHANGED_GUEST_GDTR \
103 | HWACCM_CHANGED_GUEST_IDTR \
104 | HWACCM_CHANGED_GUEST_LDTR \
105 | HWACCM_CHANGED_GUEST_TR \
106 | HWACCM_CHANGED_GUEST_SYSENTER_MSR \
107 | HWACCM_CHANGED_GUEST_DEBUG \
108 | HWACCM_CHANGED_GUEST_FPU)
109
110/** @} */
111
112/** @name Intercepted traps
113 * Traps that need to be intercepted so we can correctly dispatch them to the guest if required.
114 * Currently #NM and #PF only
115 */
116#ifdef VBOX_STRICT
117#define HWACCM_VMX_TRAP_MASK RT_BIT(X86_XCPT_DE) | RT_BIT(X86_XCPT_NM) | RT_BIT(X86_XCPT_PF) | RT_BIT(X86_XCPT_UD) | RT_BIT(X86_XCPT_NP) | RT_BIT(X86_XCPT_SS) | RT_BIT(X86_XCPT_GP) | RT_BIT(X86_XCPT_MF)
118#define HWACCM_SVM_TRAP_MASK HWACCM_VMX_TRAP_MASK
119#else
120#define HWACCM_VMX_TRAP_MASK RT_BIT(X86_XCPT_NM) | RT_BIT(X86_XCPT_PF)
121#define HWACCM_SVM_TRAP_MASK RT_BIT(X86_XCPT_NM) | RT_BIT(X86_XCPT_PF)
122#endif
123/* All exceptions have to be intercept in emulated real-mode (minues NM & PF as they are always intercepted. */
124#define HWACCM_VMX_TRAP_MASK_REALMODE RT_BIT(X86_XCPT_DE) | RT_BIT(X86_XCPT_DB) | RT_BIT(X86_XCPT_NMI) | RT_BIT(X86_XCPT_BP) | RT_BIT(X86_XCPT_OF) | RT_BIT(X86_XCPT_BR) | RT_BIT(X86_XCPT_UD) | RT_BIT(X86_XCPT_DF) | RT_BIT(X86_XCPT_CO_SEG_OVERRUN) | RT_BIT(X86_XCPT_TS) | RT_BIT(X86_XCPT_NP) | RT_BIT(X86_XCPT_SS) | RT_BIT(X86_XCPT_GP) | RT_BIT(X86_XCPT_MF) | RT_BIT(X86_XCPT_AC) | RT_BIT(X86_XCPT_MC) | RT_BIT(X86_XCPT_XF)
125/** @} */
126
127
128/** Maxium resume loops allowed in ring 0 (safety precaution) */
129#define HWACCM_MAX_RESUME_LOOPS 1024
130
131/** Size for the EPT identity page table (1024 4 MB pages to cover the entire address space). */
132#define HWACCM_EPT_IDENTITY_PG_TABLE_SIZE PAGE_SIZE
133/** Size of the TSS structure + 2 pages for the IO bitmap + end byte. */
134#define HWACCM_VTX_TSS_SIZE (sizeof(VBOXTSS) + 2*PAGE_SIZE + 1)
135/** Total guest mapped memory needed. */
136#define HWACCM_VTX_TOTAL_DEVHEAP_MEM (HWACCM_EPT_IDENTITY_PG_TABLE_SIZE + HWACCM_VTX_TSS_SIZE)
137
138/** HWACCM SSM version
139 */
140#define HWACCM_SSM_VERSION 3
141
142/* Per-cpu information. */
143typedef struct
144{
145 RTCPUID idCpu;
146
147 RTR0MEMOBJ pMemObj;
148 /* Current ASID (AMD-V)/VPID (Intel) */
149 uint32_t uCurrentASID;
150 /* TLB flush count */
151 uint32_t cTLBFlushes;
152
153 /* Set the first time a cpu is used to make sure we start with a clean TLB. */
154 bool fFlushTLB;
155
156 /** Configured for VT-x or AMD-V. */
157 bool fConfigured;
158
159 /** In use by our code. (for power suspend) */
160 volatile bool fInUse;
161} HWACCM_CPUINFO;
162typedef HWACCM_CPUINFO *PHWACCM_CPUINFO;
163
164/* VT-x capability qword. */
165typedef union
166{
167 struct
168 {
169 uint32_t disallowed0;
170 uint32_t allowed1;
171 } n;
172 uint64_t u;
173} VMX_CAPABILITY;
174
175/**
176 * HWACCM VM Instance data.
177 * Changes to this must checked against the padding of the cfgm union in VM!
178 */
179typedef struct HWACCM
180{
181 /** Offset to the VM structure.
182 * See HWACCM2VM(). */
183 RTUINT offVM;
184
185 /** Set when we've initialized VMX or SVM. */
186 bool fInitialized;
187 /** Set when we're using VMX/SVN at that moment. */
188 bool fActive;
189
190 /** Set when hardware acceleration is allowed. */
191 bool fAllowed;
192
193 /** Set if nested paging is enabled. */
194 bool fNestedPaging;
195
196 /** Set if nested paging is allowed. */
197 bool fAllowNestedPaging;
198 /** Set if VT-x VPID is allowed. */
199 bool fAllowVPID;
200
201 /** Set if we need to flush the TLB during the world switch. */
202 bool fForceTLBFlush;
203
204 /** Old style FPU reporting trap mask override performed (optimization) */
205 bool fFPUOldStyleOverride;
206
207#if 0
208 /** Explicit alignment padding to make 32-bit gcc align u64RegisterMask
209 * naturally. */
210 bool padding[1];
211#endif
212
213 /** HWACCM_CHANGED_* flags. */
214 RTUINT fContextUseFlags;
215
216 /* Id of the last cpu we were executing code on (NIL_RTCPUID for the first time) */
217 RTCPUID idLastCpu;
218
219 /* TLB flush count */
220 RTUINT cTLBFlushes;
221
222 /* Current ASID in use by the VM */
223 RTUINT uCurrentASID;
224
225 /** Maximum ASID allowed. */
226 RTUINT uMaxASID;
227
228 /** And mask for copying register contents. */
229 uint64_t u64RegisterMask;
230 struct
231 {
232 /** Set by the ring-0 driver to indicate VMX is supported by the CPU. */
233 bool fSupported;
234
235 /** Set when we've enabled VMX. */
236 bool fEnabled;
237
238 /** Set if we can use VMXResume to execute guest code. */
239 bool fResumeVM;
240
241 /** Set if VPID is supported. */
242 bool fVPID;
243
244 /** R0 memory object for the VM control structure (VMCS). */
245 RTR0MEMOBJ pMemObjVMCS;
246 /** Physical address of the VM control structure (VMCS). */
247 RTHCPHYS pVMCSPhys;
248 /** Virtual address of the VM control structure (VMCS). */
249 R0PTRTYPE(void *) pVMCS;
250
251 /** Virtual address of the TSS page used for real mode emulation. */
252 R3PTRTYPE(PVBOXTSS) pRealModeTSS;
253
254 /** Virtual address of the identity page table used for real mode and protected mode without paging emulation in EPT mode. */
255 R3PTRTYPE(PX86PD) pNonPagingModeEPTPageTable;
256
257 /** R0 memory object for the virtual APIC mmio cache. */
258 RTR0MEMOBJ pMemObjAPIC;
259 /** Physical address of the virtual APIC mmio cache. */
260 RTHCPHYS pAPICPhys;
261 /** Virtual address of the virtual APIC mmio cache. */
262 R0PTRTYPE(uint8_t *) pAPIC;
263
264 /** R0 memory object for the MSR bitmap (1 page). */
265 RTR0MEMOBJ pMemObjMSRBitmap;
266 /** Physical address of the MSR bitmap (1 page). */
267 RTHCPHYS pMSRBitmapPhys;
268 /** Virtual address of the MSR bitmap (1 page). */
269 R0PTRTYPE(uint8_t *) pMSRBitmap;
270
271 /** R0 memory object for the MSR entry load page (guest MSRs). */
272 RTR0MEMOBJ pMemObjMSREntryLoad;
273 /** Physical address of the MSR entry load page (guest MSRs). */
274 RTHCPHYS pMSREntryLoadPhys;
275 /** Virtual address of the MSR entry load page (guest MSRs). */
276 R0PTRTYPE(uint8_t *) pMSREntryLoad;
277
278 /** R0 memory object for the MSR exit store page (guest MSRs). */
279 RTR0MEMOBJ pMemObjMSRExitStore;
280 /** Physical address of the MSR exit store page (guest MSRs). */
281 RTHCPHYS pMSRExitStorePhys;
282 /** Virtual address of the MSR exit store page (guest MSRs). */
283 R0PTRTYPE(uint8_t *) pMSRExitStore;
284
285 /** R0 memory object for the MSR exit load page (host MSRs). */
286 RTR0MEMOBJ pMemObjMSRExitLoad;
287 /** Physical address of the MSR exit load page (host MSRs). */
288 RTHCPHYS pMSRExitLoadPhys;
289 /** Virtual address of the MSR exit load page (host MSRs). */
290 R0PTRTYPE(uint8_t *) pMSRExitLoad;
291
292 /** Ring 0 handlers for VT-x. */
293 DECLR0CALLBACKMEMBER(int, pfnStartVM,(RTHCUINT fResume, PCPUMCTX pCtx));
294 DECLR0CALLBACKMEMBER(void, pfnSetupTaggedTLB, (PVM pVM));
295
296#if HC_ARCH_BITS == 32
297 uint32_t Alignment1;
298#endif
299
300 /** Host CR4 value (set by ring-0 VMX init) */
301 uint64_t hostCR4;
302
303 /** Current VMX_VMCS_CTRL_PROC_EXEC_CONTROLS. */
304 uint64_t proc_ctls;
305
306 /** Current CR0 mask. */
307 uint64_t cr0_mask;
308 /** Current CR4 mask. */
309 uint64_t cr4_mask;
310
311 /** Current EPTP. */
312 RTHCPHYS GCPhysEPTP;
313
314 /** VMX MSR values */
315 struct
316 {
317 uint64_t feature_ctrl;
318 uint64_t vmx_basic_info;
319 VMX_CAPABILITY vmx_pin_ctls;
320 VMX_CAPABILITY vmx_proc_ctls;
321 VMX_CAPABILITY vmx_proc_ctls2;
322 VMX_CAPABILITY vmx_exit;
323 VMX_CAPABILITY vmx_entry;
324 uint64_t vmx_misc;
325 uint64_t vmx_cr0_fixed0;
326 uint64_t vmx_cr0_fixed1;
327 uint64_t vmx_cr4_fixed0;
328 uint64_t vmx_cr4_fixed1;
329 uint64_t vmx_vmcs_enum;
330 uint64_t vmx_eptcaps;
331 } msr;
332
333 /* Last instruction error */
334 uint32_t ulLastInstrError;
335
336 /** The last known guest paging mode. */
337 PGMMODE enmCurrGuestMode;
338
339 /** Flush types for invept & invvpid; they depend on capabilities. */
340 VMX_FLUSH enmFlushPage;
341 VMX_FLUSH enmFlushContext;
342
343 /** Real-mode emulation state. */
344 struct
345 {
346 X86EFLAGS eflags;
347 uint32_t fValid;
348 } RealMode;
349
350 struct
351 {
352 uint64_t u64VMCSPhys;
353 uint32_t ulVMCSRevision;
354 uint32_t padding;
355 } lasterror;
356 } vmx;
357
358 struct
359 {
360 /** Set by the ring-0 driver to indicate SVM is supported by the CPU. */
361 bool fSupported;
362 /** Set when we've enabled SVM. */
363 bool fEnabled;
364 /** Set if we don't have to flush the TLB on VM entry. */
365 bool fResumeVM;
366 /** Set if erratum 170 affects the AMD cpu. */
367 bool fAlwaysFlushTLB;
368
369 /** R0 memory object for the VM control block (VMCB). */
370 RTR0MEMOBJ pMemObjVMCB;
371 /** Physical address of the VM control block (VMCB). */
372 RTHCPHYS pVMCBPhys;
373 /** Virtual address of the VM control block (VMCB). */
374 R0PTRTYPE(void *) pVMCB;
375
376 /** R0 memory object for the host VM control block (VMCB). */
377 RTR0MEMOBJ pMemObjVMCBHost;
378 /** Physical address of the host VM control block (VMCB). */
379 RTHCPHYS pVMCBHostPhys;
380 /** Virtual address of the host VM control block (VMCB). */
381 R0PTRTYPE(void *) pVMCBHost;
382
383 /** R0 memory object for the IO bitmap (12kb). */
384 RTR0MEMOBJ pMemObjIOBitmap;
385 /** Physical address of the IO bitmap (12kb). */
386 RTHCPHYS pIOBitmapPhys;
387 /** Virtual address of the IO bitmap. */
388 R0PTRTYPE(void *) pIOBitmap;
389
390 /** R0 memory object for the MSR bitmap (8kb). */
391 RTR0MEMOBJ pMemObjMSRBitmap;
392 /** Physical address of the MSR bitmap (8kb). */
393 RTHCPHYS pMSRBitmapPhys;
394 /** Virtual address of the MSR bitmap. */
395 R0PTRTYPE(void *) pMSRBitmap;
396
397 /** Ring 0 handlers for VT-x. */
398 DECLR0CALLBACKMEMBER(int, pfnVMRun,(RTHCPHYS pVMCBHostPhys, RTHCPHYS pVMCBPhys, PCPUMCTX pCtx));
399
400 /** SVM revision. */
401 uint32_t u32Rev;
402
403 /** SVM feature bits from cpuid 0x8000000a */
404 uint32_t u32Features;
405 } svm;
406
407 struct
408 {
409 uint32_t u32AMDFeatureECX;
410 uint32_t u32AMDFeatureEDX;
411 } cpuid;
412
413 /** Event injection state. */
414 struct
415 {
416 uint32_t fPending;
417 uint32_t errCode;
418 uint64_t intInfo;
419 } Event;
420
421 /** Saved error from detection */
422 int32_t lLastError;
423
424 /** HWACCMR0Init was run */
425 bool fHWACCMR0Init;
426
427 /** Currenty shadow paging mode. */
428 PGMMODE enmShadowMode;
429
430 /** Explicit alignment padding of StatEntry (32-bit g++ again). */
431 int32_t padding2;
432
433#ifdef VBOX_STRICT
434 /** The CPU ID of the CPU currently owning the VMCS. Set in
435 * HWACCMR0Enter and cleared in HWACCMR0Leave. */
436 RTCPUID idEnteredCpu;
437# if HC_ARCH_BITS == 32
438 RTCPUID Alignment0;
439# endif
440#endif
441
442 STAMPROFILEADV StatEntry;
443 STAMPROFILEADV StatExit;
444 STAMPROFILEADV StatInGC;
445
446 STAMCOUNTER StatIntInject;
447
448 STAMCOUNTER StatExitShadowNM;
449 STAMCOUNTER StatExitGuestNM;
450 STAMCOUNTER StatExitShadowPF;
451 STAMCOUNTER StatExitGuestPF;
452 STAMCOUNTER StatExitGuestUD;
453 STAMCOUNTER StatExitGuestSS;
454 STAMCOUNTER StatExitGuestNP;
455 STAMCOUNTER StatExitGuestGP;
456 STAMCOUNTER StatExitGuestDE;
457 STAMCOUNTER StatExitGuestDB;
458 STAMCOUNTER StatExitGuestMF;
459 STAMCOUNTER StatExitInvpg;
460 STAMCOUNTER StatExitInvd;
461 STAMCOUNTER StatExitCpuid;
462 STAMCOUNTER StatExitRdtsc;
463 STAMCOUNTER StatExitCRxWrite;
464 STAMCOUNTER StatExitCRxRead;
465 STAMCOUNTER StatExitDRxWrite;
466 STAMCOUNTER StatExitDRxRead;
467 STAMCOUNTER StatExitCLTS;
468 STAMCOUNTER StatExitLMSW;
469 STAMCOUNTER StatExitIOWrite;
470 STAMCOUNTER StatExitIORead;
471 STAMCOUNTER StatExitIOStringWrite;
472 STAMCOUNTER StatExitIOStringRead;
473 STAMCOUNTER StatExitIrqWindow;
474 STAMCOUNTER StatExitMaxResume;
475 STAMCOUNTER StatIntReinject;
476 STAMCOUNTER StatPendingHostIrq;
477
478 STAMCOUNTER StatFlushPageManual;
479 STAMCOUNTER StatFlushPhysPageManual;
480 STAMCOUNTER StatFlushTLBManual;
481 STAMCOUNTER StatFlushPageInvlpg;
482 STAMCOUNTER StatFlushTLBWorldSwitch;
483 STAMCOUNTER StatNoFlushTLBWorldSwitch;
484 STAMCOUNTER StatFlushTLBCRxChange;
485 STAMCOUNTER StatFlushASID;
486 STAMCOUNTER StatFlushTLBInvlpga;
487
488 STAMCOUNTER StatSwitchGuestIrq;
489 STAMCOUNTER StatSwitchToR3;
490
491 STAMCOUNTER StatTSCOffset;
492 STAMCOUNTER StatTSCIntercept;
493
494 STAMCOUNTER StatExitReasonNPF;
495 STAMCOUNTER StatDRxArmed;
496 STAMCOUNTER StatDRxContextSwitch;
497 STAMCOUNTER StatDRxIOCheck;
498
499
500 R3PTRTYPE(PSTAMCOUNTER) paStatExitReason;
501 R0PTRTYPE(PSTAMCOUNTER) paStatExitReasonR0;
502} HWACCM;
503/** Pointer to HWACCM VM instance data. */
504typedef HWACCM *PHWACCM;
505
506#ifdef IN_RING0
507
508VMMR0DECL(PHWACCM_CPUINFO) HWACCMR0GetCurrentCpu();
509
510#ifdef VBOX_STRICT
511VMMR0DECL(void) HWACCMDumpRegs(PVM pVM, PCPUMCTX pCtx);
512VMMR0DECL(void) HWACCMR0DumpDescriptor(PX86DESCHC Desc, RTSEL Sel, const char *pszMsg);
513#else
514#define HWACCMDumpRegs(a, b) do { } while (0)
515#define HWACCMR0DumpDescriptor(a, b, c) do { } while (0)
516#endif
517
518/* Dummy callback handlers. */
519VMMR0DECL(int) HWACCMR0DummyEnter(PVM pVM, PHWACCM_CPUINFO pCpu);
520VMMR0DECL(int) HWACCMR0DummyLeave(PVM pVM, CPUMCTX *pCtx);
521VMMR0DECL(int) HWACCMR0DummyEnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys);
522VMMR0DECL(int) HWACCMR0DummyDisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys);
523VMMR0DECL(int) HWACCMR0DummyInitVM(PVM pVM);
524VMMR0DECL(int) HWACCMR0DummyTermVM(PVM pVM);
525VMMR0DECL(int) HWACCMR0DummySetupVM(PVM pVM);
526VMMR0DECL(int) HWACCMR0DummyRunGuestCode(PVM pVM, CPUMCTX *pCtx);
527VMMR0DECL(int) HWACCMR0DummySaveHostState(PVM pVM);
528VMMR0DECL(int) HWACCMR0DummyLoadGuestState(PVM pVM, CPUMCTX *pCtx);
529
530#endif /* IN_RING0 */
531
532/** @} */
533
534__END_DECLS
535
536#endif
537
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