VirtualBox

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

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

Enabled VPID (VT-x tagged TLB); default off

  • Added machine & machine debugger property
  • Added -vtxvpid option to VBoxManage
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 20.3 KB
Line 
1/* $Id: HWACCMInternal.h 13221 2008-10-13 14:43:54Z 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/** @} */
124
125
126/** Maxium resume loops allowed in ring 0 (safety precaution) */
127#define HWACCM_MAX_RESUME_LOOPS 1024
128
129/** Size for the EPT identity page table (1024 4 MB pages to cover the entire address space). */
130#define HWACCM_EPT_IDENTITY_PG_TABLE_SIZE PAGE_SIZE
131/** Size of the TSS structure + 2 pages for the IO bitmap + end byte. */
132#define HWACCM_VTX_TSS_SIZE (sizeof(VBOXTSS) + 2*PAGE_SIZE + 1)
133/** Total guest mapped memory needed. */
134#define HWACCM_VTX_TOTAL_DEVHEAP_MEM (HWACCM_EPT_IDENTITY_PG_TABLE_SIZE + HWACCM_VTX_TSS_SIZE)
135
136/** HWACCM SSM version
137 */
138#define HWACCM_SSM_VERSION 3
139
140/* Per-cpu information. */
141typedef struct
142{
143 RTCPUID idCpu;
144
145 RTR0MEMOBJ pMemObj;
146 /* Current ASID (AMD-V)/VPID (Intel) */
147 uint32_t uCurrentASID;
148 /* TLB flush count */
149 uint32_t cTLBFlushes;
150
151 /* Set the first time a cpu is used to make sure we start with a clean TLB. */
152 bool fFlushTLB;
153
154 bool fConfigured;
155} HWACCM_CPUINFO;
156typedef HWACCM_CPUINFO *PHWACCM_CPUINFO;
157
158/* VT-x capability qword. */
159typedef union
160{
161 struct
162 {
163 uint32_t disallowed0;
164 uint32_t allowed1;
165 } n;
166 uint64_t u;
167} VMX_CAPABILITY;
168
169/**
170 * HWACCM VM Instance data.
171 * Changes to this must checked against the padding of the cfgm union in VM!
172 */
173typedef struct HWACCM
174{
175 /** Offset to the VM structure.
176 * See HWACCM2VM(). */
177 RTUINT offVM;
178
179 /** Set when we've initialized VMX or SVM. */
180 bool fInitialized;
181 /** Set when we're using VMX/SVN at that moment. */
182 bool fActive;
183
184 /** Set when hardware acceleration is allowed. */
185 bool fAllowed;
186
187 /** Set if nested paging is enabled. */
188 bool fNestedPaging;
189
190 /** Set if nested paging is allowed. */
191 bool fAllowNestedPaging;
192 /** Set if VT-x VPID is allowed. */
193 bool fAllowVPID;
194
195 /** Set if we need to flush the TLB during the world switch. */
196 bool fForceTLBFlush;
197
198 /** Old style FPU reporting trap mask override performed (optimization) */
199 bool fFPUOldStyleOverride;
200
201#if 0
202 /** Explicit alignment padding to make 32-bit gcc align u64RegisterMask
203 * naturally. */
204 bool padding[1];
205#endif
206
207 /** HWACCM_CHANGED_* flags. */
208 RTUINT fContextUseFlags;
209
210 /* Id of the last cpu we were executing code on (NIL_RTCPUID for the first time) */
211 RTCPUID idLastCpu;
212
213 /* TLB flush count */
214 RTUINT cTLBFlushes;
215
216 /* Current ASID in use by the VM */
217 RTUINT uCurrentASID;
218
219 /** Maximum ASID allowed. */
220 RTUINT uMaxASID;
221
222 /** And mask for copying register contents. */
223 uint64_t u64RegisterMask;
224 struct
225 {
226 /** Set by the ring-0 driver to indicate VMX is supported by the CPU. */
227 bool fSupported;
228
229 /** Set when we've enabled VMX. */
230 bool fEnabled;
231
232 /** Set if we can use VMXResume to execute guest code. */
233 bool fResumeVM;
234
235 /** Set if VPID is supported. */
236 bool fVPID;
237
238 /** R0 memory object for the VM control structure (VMCS). */
239 RTR0MEMOBJ pMemObjVMCS;
240 /** Physical address of the VM control structure (VMCS). */
241 RTHCPHYS pVMCSPhys;
242 /** Virtual address of the VM control structure (VMCS). */
243 R0PTRTYPE(void *) pVMCS;
244
245 /** Virtual address of the TSS page used for real mode emulation. */
246 R3PTRTYPE(PVBOXTSS) pRealModeTSS;
247
248 /** Virtual address of the identity page table used for real mode and protected mode without paging emulation in EPT mode. */
249 R3PTRTYPE(PX86PD) pNonPagingModeEPTPageTable;
250
251 /** R0 memory object for the virtual APIC mmio cache. */
252 RTR0MEMOBJ pMemObjAPIC;
253 /** Physical address of the virtual APIC mmio cache. */
254 RTHCPHYS pAPICPhys;
255 /** Virtual address of the virtual APIC mmio cache. */
256 R0PTRTYPE(uint8_t *) pAPIC;
257
258 /** R0 memory object for the MSR bitmap (1 page). */
259 RTR0MEMOBJ pMemObjMSRBitmap;
260 /** Physical address of the MSR bitmap (1 page). */
261 RTHCPHYS pMSRBitmapPhys;
262 /** Virtual address of the MSR bitmap (1 page). */
263 R0PTRTYPE(uint8_t *) pMSRBitmap;
264
265 /** R0 memory object for the MSR entry load page (guest MSRs). */
266 RTR0MEMOBJ pMemObjMSREntryLoad;
267 /** Physical address of the MSR entry load page (guest MSRs). */
268 RTHCPHYS pMSREntryLoadPhys;
269 /** Virtual address of the MSR entry load page (guest MSRs). */
270 R0PTRTYPE(uint8_t *) pMSREntryLoad;
271
272 /** R0 memory object for the MSR exit store page (guest MSRs). */
273 RTR0MEMOBJ pMemObjMSRExitStore;
274 /** Physical address of the MSR exit store page (guest MSRs). */
275 RTHCPHYS pMSRExitStorePhys;
276 /** Virtual address of the MSR exit store page (guest MSRs). */
277 R0PTRTYPE(uint8_t *) pMSRExitStore;
278
279 /** R0 memory object for the MSR exit load page (host MSRs). */
280 RTR0MEMOBJ pMemObjMSRExitLoad;
281 /** Physical address of the MSR exit load page (host MSRs). */
282 RTHCPHYS pMSRExitLoadPhys;
283 /** Virtual address of the MSR exit load page (host MSRs). */
284 R0PTRTYPE(uint8_t *) pMSRExitLoad;
285
286 /** Ring 0 handlers for VT-x. */
287 DECLR0CALLBACKMEMBER(int, pfnStartVM,(RTHCUINT fResume, PCPUMCTX pCtx));
288 DECLR0CALLBACKMEMBER(void, pfnSetupTaggedTLB, (PVM pVM));
289
290#if HC_ARCH_BITS == 32
291 uint32_t Alignment1;
292#endif
293
294 /** Host CR4 value (set by ring-0 VMX init) */
295 uint64_t hostCR4;
296
297 /** Current VMX_VMCS_CTRL_PROC_EXEC_CONTROLS. */
298 uint64_t proc_ctls;
299
300 /** Current CR0 mask. */
301 uint64_t cr0_mask;
302 /** Current CR4 mask. */
303 uint64_t cr4_mask;
304
305 /** Current EPTP. */
306 RTHCPHYS GCPhysEPTP;
307
308 /** VMX MSR values */
309 struct
310 {
311 uint64_t feature_ctrl;
312 uint64_t vmx_basic_info;
313 VMX_CAPABILITY vmx_pin_ctls;
314 VMX_CAPABILITY vmx_proc_ctls;
315 VMX_CAPABILITY vmx_proc_ctls2;
316 VMX_CAPABILITY vmx_exit;
317 VMX_CAPABILITY vmx_entry;
318 uint64_t vmx_misc;
319 uint64_t vmx_cr0_fixed0;
320 uint64_t vmx_cr0_fixed1;
321 uint64_t vmx_cr4_fixed0;
322 uint64_t vmx_cr4_fixed1;
323 uint64_t vmx_vmcs_enum;
324 uint64_t vmx_eptcaps;
325 } msr;
326
327 /* Last instruction error */
328 uint32_t ulLastInstrError;
329
330 /** Current trap mask. */
331 uint32_t u32TrapMask;
332
333 /** The last known guest paging mode. */
334 PGMMODE enmCurrGuestMode;
335
336 /** Flush types for invept & invvpid; they depend on capabilities. */
337 VMX_FLUSH enmFlushPage;
338 VMX_FLUSH enmFlushContext;
339
340 /** Real-mode emulation state. */
341 struct
342 {
343 struct
344 {
345 uint32_t fPending;
346 uint32_t padding4;
347 uint64_t intInfo;
348 } Event;
349
350 CPUMSELREGHID dsHid;
351 CPUMSELREGHID esHid;
352 CPUMSELREGHID fsHid;
353 CPUMSELREGHID gsHid;
354 CPUMSELREGHID ssHid;
355 RTSEL ds;
356 RTSEL es;
357 RTSEL fs;
358 RTSEL gs;
359 RTSEL ss;
360 RTSEL padding5[3];
361 uint32_t eip;
362 uint32_t fValid;
363 } RealMode;
364
365 struct
366 {
367 uint64_t u64VMCSPhys;
368 uint32_t ulVMCSRevision;
369 } lasterror;
370 } vmx;
371
372 struct
373 {
374 /** Set by the ring-0 driver to indicate SVM is supported by the CPU. */
375 bool fSupported;
376 /** Set when we've enabled SVM. */
377 bool fEnabled;
378 /** Set if we don't have to flush the TLB on VM entry. */
379 bool fResumeVM;
380 /** Set if erratum 170 affects the AMD cpu. */
381 bool fAlwaysFlushTLB;
382
383 /** R0 memory object for the VM control block (VMCB). */
384 RTR0MEMOBJ pMemObjVMCB;
385 /** Physical address of the VM control block (VMCB). */
386 RTHCPHYS pVMCBPhys;
387 /** Virtual address of the VM control block (VMCB). */
388 R0PTRTYPE(void *) pVMCB;
389
390 /** R0 memory object for the host VM control block (VMCB). */
391 RTR0MEMOBJ pMemObjVMCBHost;
392 /** Physical address of the host VM control block (VMCB). */
393 RTHCPHYS pVMCBHostPhys;
394 /** Virtual address of the host VM control block (VMCB). */
395 R0PTRTYPE(void *) pVMCBHost;
396
397 /** R0 memory object for the IO bitmap (12kb). */
398 RTR0MEMOBJ pMemObjIOBitmap;
399 /** Physical address of the IO bitmap (12kb). */
400 RTHCPHYS pIOBitmapPhys;
401 /** Virtual address of the IO bitmap. */
402 R0PTRTYPE(void *) pIOBitmap;
403
404 /** R0 memory object for the MSR bitmap (8kb). */
405 RTR0MEMOBJ pMemObjMSRBitmap;
406 /** Physical address of the MSR bitmap (8kb). */
407 RTHCPHYS pMSRBitmapPhys;
408 /** Virtual address of the MSR bitmap. */
409 R0PTRTYPE(void *) pMSRBitmap;
410
411 /** Ring 0 handlers for VT-x. */
412 DECLR0CALLBACKMEMBER(int, pfnVMRun,(RTHCPHYS pVMCBHostPhys, RTHCPHYS pVMCBPhys, PCPUMCTX pCtx));
413
414 /** SVM revision. */
415 uint32_t u32Rev;
416
417 /** SVM feature bits from cpuid 0x8000000a */
418 uint32_t u32Features;
419 } svm;
420
421 struct
422 {
423 uint32_t u32AMDFeatureECX;
424 uint32_t u32AMDFeatureEDX;
425 } cpuid;
426
427 /** Event injection state. */
428 struct
429 {
430 uint32_t fPending;
431 uint32_t errCode;
432 uint64_t intInfo;
433 } Event;
434
435 /** Saved error from detection */
436 int32_t lLastError;
437
438 /** HWACCMR0Init was run */
439 bool fHWACCMR0Init;
440
441 /** Currenty shadow paging mode. */
442 PGMMODE enmShadowMode;
443
444 /** Explicit alignment padding of StatEntry (32-bit g++ again). */
445 int32_t padding2;
446
447#ifdef VBOX_STRICT
448 /** The CPU ID of the CPU currently owning the VMCS. Set in
449 * HWACCMR0Enter and cleared in HWACCMR0Leave. */
450 RTCPUID idEnteredCpu;
451# if HC_ARCH_BITS == 32
452 RTCPUID Alignment0;
453# endif
454#endif
455
456 STAMPROFILEADV StatEntry;
457 STAMPROFILEADV StatExit;
458 STAMPROFILEADV StatInGC;
459
460 STAMCOUNTER StatIntInject;
461
462 STAMCOUNTER StatExitShadowNM;
463 STAMCOUNTER StatExitGuestNM;
464 STAMCOUNTER StatExitShadowPF;
465 STAMCOUNTER StatExitGuestPF;
466 STAMCOUNTER StatExitGuestUD;
467 STAMCOUNTER StatExitGuestSS;
468 STAMCOUNTER StatExitGuestNP;
469 STAMCOUNTER StatExitGuestGP;
470 STAMCOUNTER StatExitGuestDE;
471 STAMCOUNTER StatExitGuestDB;
472 STAMCOUNTER StatExitGuestMF;
473 STAMCOUNTER StatExitInvpg;
474 STAMCOUNTER StatExitInvd;
475 STAMCOUNTER StatExitCpuid;
476 STAMCOUNTER StatExitRdtsc;
477 STAMCOUNTER StatExitCRxWrite;
478 STAMCOUNTER StatExitCRxRead;
479 STAMCOUNTER StatExitDRxWrite;
480 STAMCOUNTER StatExitDRxRead;
481 STAMCOUNTER StatExitCLTS;
482 STAMCOUNTER StatExitLMSW;
483 STAMCOUNTER StatExitIOWrite;
484 STAMCOUNTER StatExitIORead;
485 STAMCOUNTER StatExitIOStringWrite;
486 STAMCOUNTER StatExitIOStringRead;
487 STAMCOUNTER StatExitIrqWindow;
488 STAMCOUNTER StatExitMaxResume;
489 STAMCOUNTER StatIntReinject;
490 STAMCOUNTER StatPendingHostIrq;
491
492 STAMCOUNTER StatFlushPageManual;
493 STAMCOUNTER StatFlushPhysPageManual;
494 STAMCOUNTER StatFlushTLBManual;
495 STAMCOUNTER StatFlushPageInvlpg;
496 STAMCOUNTER StatFlushTLBWorldSwitch;
497 STAMCOUNTER StatNoFlushTLBWorldSwitch;
498 STAMCOUNTER StatFlushTLBCRxChange;
499 STAMCOUNTER StatFlushASID;
500
501 STAMCOUNTER StatSwitchGuestIrq;
502 STAMCOUNTER StatSwitchToR3;
503
504 STAMCOUNTER StatTSCOffset;
505 STAMCOUNTER StatTSCIntercept;
506
507 STAMCOUNTER StatExitReasonNPF;
508 STAMCOUNTER StatDRxArmed;
509 STAMCOUNTER StatDRxContextSwitch;
510 STAMCOUNTER StatDRxIOCheck;
511
512
513 R3PTRTYPE(PSTAMCOUNTER) paStatExitReason;
514 R0PTRTYPE(PSTAMCOUNTER) paStatExitReasonR0;
515} HWACCM;
516/** Pointer to HWACCM VM instance data. */
517typedef HWACCM *PHWACCM;
518
519#ifdef IN_RING0
520
521VMMR0DECL(PHWACCM_CPUINFO) HWACCMR0GetCurrentCpu();
522
523#ifdef VBOX_STRICT
524VMMR0DECL(void) HWACCMDumpRegs(PVM pVM, PCPUMCTX pCtx);
525VMMR0DECL(void) HWACCMR0DumpDescriptor(PX86DESCHC Desc, RTSEL Sel, const char *pszMsg);
526#else
527#define HWACCMDumpRegs(a, b) do { } while (0)
528#define HWACCMR0DumpDescriptor(a, b, c) do { } while (0)
529#endif
530
531/* Dummy callback handlers. */
532VMMR0DECL(int) HWACCMR0DummyEnter(PVM pVM, PHWACCM_CPUINFO pCpu);
533VMMR0DECL(int) HWACCMR0DummyLeave(PVM pVM, CPUMCTX *pCtx);
534VMMR0DECL(int) HWACCMR0DummyEnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys);
535VMMR0DECL(int) HWACCMR0DummyDisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys);
536VMMR0DECL(int) HWACCMR0DummyInitVM(PVM pVM);
537VMMR0DECL(int) HWACCMR0DummyTermVM(PVM pVM);
538VMMR0DECL(int) HWACCMR0DummySetupVM(PVM pVM);
539VMMR0DECL(int) HWACCMR0DummyRunGuestCode(PVM pVM, CPUMCTX *pCtx);
540VMMR0DECL(int) HWACCMR0DummySaveHostState(PVM pVM);
541VMMR0DECL(int) HWACCMR0DummyLoadGuestState(PVM pVM, CPUMCTX *pCtx);
542
543#endif /* IN_RING0 */
544
545/** @} */
546
547__END_DECLS
548
549#endif
550
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