VirtualBox

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

Last change on this file since 12805 was 12805, checked in by vboxsync, 17 years ago

Reenabled real-mode emulation in VT-x again.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette