VirtualBox

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

Last change on this file since 26234 was 26103, checked in by vboxsync, 15 years ago

Do not enable NXE if the host hasn't activated it (32 bits VT-x only)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 33.5 KB
Line 
1/* $Id: HWACCMInternal.h 26103 2010-01-29 15:14:22Z 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#include <iprt/avl.h>
37
38#if HC_ARCH_BITS == 64 || defined(VBOX_WITH_HYBRID_32BIT_KERNEL) || defined (VBOX_WITH_64_BITS_GUESTS)
39/* Enable 64 bits guest support. */
40# define VBOX_ENABLE_64_BITS_GUESTS
41#endif
42
43#define VMX_USE_CACHED_VMCS_ACCESSES
44#define HWACCM_VMX_EMULATE_REALMODE
45#define HWACCM_VTX_WITH_EPT
46#define HWACCM_VTX_WITH_VPID
47
48
49#if 0
50/* Seeing somewhat random behaviour on my Nehalem system with auto-save of guest MSRs;
51 * for some strange reason the CPU doesn't save the MSRs during the VM-exit.
52 * Clearly visible with a dual VCPU configured OpenSolaris 200906 live cd VM.
53 *
54 * Note: change the assembly files when enabling this! (remove the manual auto load/save)
55 */
56#define VBOX_WITH_AUTO_MSR_LOAD_RESTORE
57#endif
58
59RT_C_DECLS_BEGIN
60
61
62/** @defgroup grp_hwaccm_int Internal
63 * @ingroup grp_hwaccm
64 * @internal
65 * @{
66 */
67
68
69/** Maximum number of exit reason statistics counters. */
70#define MAX_EXITREASON_STAT 0x100
71#define MASK_EXITREASON_STAT 0xff
72#define MASK_INJECT_IRQ_STAT 0xff
73
74/** @name Changed flags
75 * These flags are used to keep track of which important registers that
76 * have been changed since last they were reset.
77 * @{
78 */
79#define HWACCM_CHANGED_GUEST_FPU RT_BIT(0)
80#define HWACCM_CHANGED_GUEST_CR0 RT_BIT(1)
81#define HWACCM_CHANGED_GUEST_CR3 RT_BIT(2)
82#define HWACCM_CHANGED_GUEST_CR4 RT_BIT(3)
83#define HWACCM_CHANGED_GUEST_GDTR RT_BIT(4)
84#define HWACCM_CHANGED_GUEST_IDTR RT_BIT(5)
85#define HWACCM_CHANGED_GUEST_LDTR RT_BIT(6)
86#define HWACCM_CHANGED_GUEST_TR RT_BIT(7)
87#define HWACCM_CHANGED_GUEST_SYSENTER_MSR RT_BIT(8)
88#define HWACCM_CHANGED_GUEST_SEGMENT_REGS RT_BIT(9)
89#define HWACCM_CHANGED_GUEST_DEBUG RT_BIT(10)
90#define HWACCM_CHANGED_HOST_CONTEXT RT_BIT(11)
91
92#define HWACCM_CHANGED_ALL ( HWACCM_CHANGED_GUEST_SEGMENT_REGS \
93 | HWACCM_CHANGED_GUEST_CR0 \
94 | HWACCM_CHANGED_GUEST_CR3 \
95 | HWACCM_CHANGED_GUEST_CR4 \
96 | HWACCM_CHANGED_GUEST_GDTR \
97 | HWACCM_CHANGED_GUEST_IDTR \
98 | HWACCM_CHANGED_GUEST_LDTR \
99 | HWACCM_CHANGED_GUEST_TR \
100 | HWACCM_CHANGED_GUEST_SYSENTER_MSR \
101 | HWACCM_CHANGED_GUEST_FPU \
102 | HWACCM_CHANGED_GUEST_DEBUG \
103 | HWACCM_CHANGED_HOST_CONTEXT)
104
105#define HWACCM_CHANGED_ALL_GUEST ( HWACCM_CHANGED_GUEST_SEGMENT_REGS \
106 | HWACCM_CHANGED_GUEST_CR0 \
107 | HWACCM_CHANGED_GUEST_CR3 \
108 | HWACCM_CHANGED_GUEST_CR4 \
109 | HWACCM_CHANGED_GUEST_GDTR \
110 | HWACCM_CHANGED_GUEST_IDTR \
111 | HWACCM_CHANGED_GUEST_LDTR \
112 | HWACCM_CHANGED_GUEST_TR \
113 | HWACCM_CHANGED_GUEST_SYSENTER_MSR \
114 | HWACCM_CHANGED_GUEST_DEBUG \
115 | HWACCM_CHANGED_GUEST_FPU)
116
117/** @} */
118
119/** @name Intercepted traps
120 * Traps that need to be intercepted so we can correctly dispatch them to the guest if required.
121 * Currently #NM and #PF only
122 */
123#ifdef VBOX_STRICT
124#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)
125#define HWACCM_SVM_TRAP_MASK HWACCM_VMX_TRAP_MASK
126#else
127#define HWACCM_VMX_TRAP_MASK RT_BIT(X86_XCPT_NM) | RT_BIT(X86_XCPT_PF)
128#define HWACCM_SVM_TRAP_MASK RT_BIT(X86_XCPT_NM) | RT_BIT(X86_XCPT_PF)
129#endif
130/* All exceptions have to be intercept in emulated real-mode (minues NM & PF as they are always intercepted. */
131#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)
132/** @} */
133
134
135/** Maximum number of page flushes we are willing to remember before considering a full TLB flush. */
136#define HWACCM_MAX_TLB_SHOOTDOWN_PAGES 8
137
138/** Size for the EPT identity page table (1024 4 MB pages to cover the entire address space). */
139#define HWACCM_EPT_IDENTITY_PG_TABLE_SIZE PAGE_SIZE
140/** Size of the TSS structure + 2 pages for the IO bitmap + end byte. */
141#define HWACCM_VTX_TSS_SIZE (sizeof(VBOXTSS) + 2*PAGE_SIZE + 1)
142/** Total guest mapped memory needed. */
143#define HWACCM_VTX_TOTAL_DEVHEAP_MEM (HWACCM_EPT_IDENTITY_PG_TABLE_SIZE + HWACCM_VTX_TSS_SIZE)
144
145/* Enable for TPR guest patching. */
146#define VBOX_HWACCM_WITH_GUEST_PATCHING
147
148/** HWACCM SSM version
149 */
150#ifdef VBOX_HWACCM_WITH_GUEST_PATCHING
151#define HWACCM_SSM_VERSION 5
152#define HWACCM_SSM_VERSION_NO_PATCHING 4
153#else
154#define HWACCM_SSM_VERSION 4
155#define HWACCM_SSM_VERSION_NO_PATCHING 4
156#endif
157#define HWACCM_SSM_VERSION_2_0_X 3
158
159/* Per-cpu information. (host) */
160typedef struct
161{
162 RTCPUID idCpu;
163
164 RTR0MEMOBJ pMemObj;
165 /* Current ASID (AMD-V)/VPID (Intel) */
166 uint32_t uCurrentASID;
167 /* TLB flush count */
168 uint32_t cTLBFlushes;
169
170 /* Set the first time a cpu is used to make sure we start with a clean TLB. */
171 bool fFlushTLB;
172
173 /** Configured for VT-x or AMD-V. */
174 bool fConfigured;
175
176 /** Set if the VBOX_HWVIRTEX_IGNORE_SVM_IN_USE hack is active. */
177 bool fIgnoreAMDVInUseError;
178
179 /** In use by our code. (for power suspend) */
180 volatile bool fInUse;
181} HWACCM_CPUINFO;
182typedef HWACCM_CPUINFO *PHWACCM_CPUINFO;
183
184typedef enum
185{
186 HWACCMPENDINGIO_INVALID = 0,
187 HWACCMPENDINGIO_PORT_READ,
188 HWACCMPENDINGIO_PORT_WRITE,
189 HWACCMPENDINGIO_STRING_READ,
190 HWACCMPENDINGIO_STRING_WRITE,
191 /** The usual 32-bit paranoia. */
192 HWACCMPENDINGIO_32BIT_HACK = 0x7fffffff
193} HWACCMPENDINGIO;
194
195
196typedef enum
197{
198 HWACCMTPRINSTR_INVALID,
199 HWACCMTPRINSTR_READ,
200 HWACCMTPRINSTR_READ_SHR4,
201 HWACCMTPRINSTR_WRITE_REG,
202 HWACCMTPRINSTR_WRITE_IMM,
203 HWACCMTPRINSTR_JUMP_REPLACEMENT,
204 /** The usual 32-bit paranoia. */
205 HWACCMTPRINSTR_32BIT_HACK = 0x7fffffff
206} HWACCMTPRINSTR;
207
208typedef struct
209{
210 /** The key is the address of patched instruction. (32 bits GC ptr) */
211 AVLOU32NODECORE Core;
212 /** Original opcode. */
213 uint8_t aOpcode[16];
214 /** Instruction size. */
215 uint32_t cbOp;
216 /** Replacement opcode. */
217 uint8_t aNewOpcode[16];
218 /** Replacement instruction size. */
219 uint32_t cbNewOp;
220 /** Instruction type. */
221 HWACCMTPRINSTR enmType;
222 /** Source operand. */
223 uint32_t uSrcOperand;
224 /** Destination operand. */
225 uint32_t uDstOperand;
226 /** Number of times the instruction caused a fault. */
227 uint32_t cFaults;
228 /** Patch address of the jump replacement. */
229 RTGCPTR32 pJumpTarget;
230} HWACCMTPRPATCH;
231/** Pointer to HWACCMTPRPATCH. */
232typedef HWACCMTPRPATCH *PHWACCMTPRPATCH;
233
234/**
235 * Switcher function, HC to RC.
236 *
237 * @param pVM The VM handle.
238 * @returns Return code indicating the action to take.
239 */
240typedef DECLASMTYPE(int) FNHWACCMSWITCHERHC(PVM pVM);
241/** Pointer to switcher function. */
242typedef FNHWACCMSWITCHERHC *PFNHWACCMSWITCHERHC;
243
244/**
245 * HWACCM VM Instance data.
246 * Changes to this must checked against the padding of the cfgm union in VM!
247 */
248typedef struct HWACCM
249{
250 /** Set when we've initialized VMX or SVM. */
251 bool fInitialized;
252
253 /** Set when hardware acceleration is allowed. */
254 bool fAllowed;
255
256 /** Set if nested paging is enabled. */
257 bool fNestedPaging;
258
259 /** Set if nested paging is allowed. */
260 bool fAllowNestedPaging;
261
262 /** Set if we can support 64-bit guests or not. */
263 bool fAllow64BitGuests;
264
265 /** Set if an IO-APIC is configured for this VM. */
266 bool fHasIoApic;
267
268 /** Set when TPR patching is allowed. */
269 bool fTRPPatchingAllowed;
270
271 /** Set when we initialize VT-x or AMD-V once for all CPUs. */
272 bool fGlobalInit;
273
274 /** Set when TPR patching is active. */
275 bool fTPRPatchingActive;
276 bool u8Alignment[7];
277
278 /** And mask for copying register contents. */
279 uint64_t u64RegisterMask;
280
281 /** Maximum ASID allowed. */
282 RTUINT uMaxASID;
283
284 /** The maximum number of resumes loops allowed in ring-0 (safety precaution).
285 * This number is set much higher when RTThreadPreemptIsPending is reliable. */
286 uint32_t cMaxResumeLoops;
287
288 /** Guest allocated memory for patching purposes. */
289 RTGCPTR pGuestPatchMem;
290 /** Current free pointer inside the patch block. */
291 RTGCPTR pFreeGuestPatchMem;
292 /** Size of the guest patch memory block. */
293 uint32_t cbGuestPatchMem;
294 uint32_t uPadding1;
295
296#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
297 /** 32 to 64 bits switcher entrypoint. */
298 R0PTRTYPE(PFNHWACCMSWITCHERHC) pfnHost32ToGuest64R0;
299
300 /* AMD-V 64 bits vmrun handler */
301 RTRCPTR pfnSVMGCVMRun64;
302
303 /* VT-x 64 bits vmlaunch handler */
304 RTRCPTR pfnVMXGCStartVM64;
305
306 /* RC handler to setup the 64 bits FPU state. */
307 RTRCPTR pfnSaveGuestFPU64;
308
309 /* RC handler to setup the 64 bits debug state. */
310 RTRCPTR pfnSaveGuestDebug64;
311
312 /* Test handler */
313 RTRCPTR pfnTest64;
314
315 RTRCPTR uAlignment[2];
316/*#elif defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
317 uint32_t u32Alignment[1]; */
318#endif
319
320 struct
321 {
322 /** Set by the ring-0 driver to indicate VMX is supported by the CPU. */
323 bool fSupported;
324
325 /** Set when we've enabled VMX. */
326 bool fEnabled;
327
328 /** Set if VPID is supported. */
329 bool fVPID;
330
331 /** Set if VT-x VPID is allowed. */
332 bool fAllowVPID;
333
334 /** Set if unrestricted guest execution is allowed (real and protected mode without paging). */
335 bool fUnrestrictedGuest;
336 bool uAlignment[3];
337
338 /** Virtual address of the TSS page used for real mode emulation. */
339 R3PTRTYPE(PVBOXTSS) pRealModeTSS;
340
341 /** Virtual address of the identity page table used for real mode and protected mode without paging emulation in EPT mode. */
342 R3PTRTYPE(PX86PD) pNonPagingModeEPTPageTable;
343
344 /** R0 memory object for the APIC physical page (serves for filtering accesses). */
345 RTR0MEMOBJ pMemObjAPIC;
346 /** Physical address of the APIC physical page (serves for filtering accesses). */
347 RTHCPHYS pAPICPhys;
348 /** Virtual address of the APIC physical page (serves for filtering accesses). */
349 R0PTRTYPE(uint8_t *) pAPIC;
350
351 /** R0 memory object for the MSR entry load page (guest MSRs). */
352 RTR0MEMOBJ pMemObjMSREntryLoad;
353 /** Physical address of the MSR entry load page (guest MSRs). */
354 RTHCPHYS pMSREntryLoadPhys;
355 /** Virtual address of the MSR entry load page (guest MSRs). */
356 R0PTRTYPE(uint8_t *) pMSREntryLoad;
357
358#ifdef VBOX_WITH_CRASHDUMP_MAGIC
359 RTR0MEMOBJ pMemObjScratch;
360 RTHCPHYS pScratchPhys;
361 R0PTRTYPE(uint8_t *) pScratch;
362#endif
363 /** R0 memory object for the MSR exit store page (guest MSRs). */
364 RTR0MEMOBJ pMemObjMSRExitStore;
365 /** Physical address of the MSR exit store page (guest MSRs). */
366 RTHCPHYS pMSRExitStorePhys;
367 /** Virtual address of the MSR exit store page (guest MSRs). */
368 R0PTRTYPE(uint8_t *) pMSRExitStore;
369
370 /** R0 memory object for the MSR exit load page (host MSRs). */
371 RTR0MEMOBJ pMemObjMSRExitLoad;
372 /** Physical address of the MSR exit load page (host MSRs). */
373 RTHCPHYS pMSRExitLoadPhys;
374 /** Virtual address of the MSR exit load page (host MSRs). */
375 R0PTRTYPE(uint8_t *) pMSRExitLoad;
376
377 /** Ring 0 handlers for VT-x. */
378 DECLR0CALLBACKMEMBER(void, pfnSetupTaggedTLB, (PVM pVM, PVMCPU pVCpu));
379
380#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS)
381 uint32_t u32Alignment;
382#endif
383 /** Host CR4 value (set by ring-0 VMX init) */
384 uint64_t hostCR4;
385
386 /** Host EFER value (set by ring-0 VMX init) */
387 uint64_t hostEFER;
388
389 /** VMX MSR values */
390 struct
391 {
392 uint64_t feature_ctrl;
393 uint64_t vmx_basic_info;
394 VMX_CAPABILITY vmx_pin_ctls;
395 VMX_CAPABILITY vmx_proc_ctls;
396 VMX_CAPABILITY vmx_proc_ctls2;
397 VMX_CAPABILITY vmx_exit;
398 VMX_CAPABILITY vmx_entry;
399 uint64_t vmx_misc;
400 uint64_t vmx_cr0_fixed0;
401 uint64_t vmx_cr0_fixed1;
402 uint64_t vmx_cr4_fixed0;
403 uint64_t vmx_cr4_fixed1;
404 uint64_t vmx_vmcs_enum;
405 uint64_t vmx_eptcaps;
406 } msr;
407
408 /** Flush types for invept & invvpid; they depend on capabilities. */
409 VMX_FLUSH enmFlushPage;
410 VMX_FLUSH enmFlushContext;
411 } vmx;
412
413 struct
414 {
415 /** Set by the ring-0 driver to indicate SVM is supported by the CPU. */
416 bool fSupported;
417 /** Set when we've enabled SVM. */
418 bool fEnabled;
419 /** Set if erratum 170 affects the AMD cpu. */
420 bool fAlwaysFlushTLB;
421 /** Set when the hack to ignore VERR_SVM_IN_USE is active. */
422 bool fIgnoreInUseError;
423
424 /** R0 memory object for the IO bitmap (12kb). */
425 RTR0MEMOBJ pMemObjIOBitmap;
426 /** Physical address of the IO bitmap (12kb). */
427 RTHCPHYS pIOBitmapPhys;
428 /** Virtual address of the IO bitmap. */
429 R0PTRTYPE(void *) pIOBitmap;
430
431 /* HWCR msr (for diagnostics) */
432 uint64_t msrHWCR;
433
434 /** SVM revision. */
435 uint32_t u32Rev;
436
437 /** SVM feature bits from cpuid 0x8000000a */
438 uint32_t u32Features;
439 } svm;
440
441 /**
442 * AVL tree with all patches (active or disabled) sorted by guest instruction address
443 */
444 AVLOU32TREE PatchTree;
445 uint32_t cPatches;
446 HWACCMTPRPATCH aPatches[64];
447
448 struct
449 {
450 uint32_t u32AMDFeatureECX;
451 uint32_t u32AMDFeatureEDX;
452 } cpuid;
453
454 /** Saved error from detection */
455 int32_t lLastError;
456
457 /** HWACCMR0Init was run */
458 bool fHWACCMR0Init;
459 bool u8Alignment1[7];
460
461 STAMCOUNTER StatTPRPatchSuccess;
462 STAMCOUNTER StatTPRPatchFailure;
463 STAMCOUNTER StatTPRReplaceSuccess;
464 STAMCOUNTER StatTPRReplaceFailure;
465} HWACCM;
466/** Pointer to HWACCM VM instance data. */
467typedef HWACCM *PHWACCM;
468
469/* Maximum number of cached entries. */
470#define VMCSCACHE_MAX_ENTRY 128
471
472/* Structure for storing read and write VMCS actions. */
473typedef struct VMCSCACHE
474{
475#ifdef VBOX_WITH_CRASHDUMP_MAGIC
476 /* Magic marker for searching in crash dumps. */
477 uint8_t aMagic[16];
478 uint64_t uMagic;
479 uint64_t u64TimeEntry;
480 uint64_t u64TimeSwitch;
481 uint64_t cResume;
482 uint64_t interPD;
483 uint64_t pSwitcher;
484 uint32_t uPos;
485 uint32_t idCpu;
486#endif
487 /* CR2 is saved here for EPT syncing. */
488 uint64_t cr2;
489 struct
490 {
491 uint32_t cValidEntries;
492 uint32_t uAlignment;
493 uint32_t aField[VMCSCACHE_MAX_ENTRY];
494 uint64_t aFieldVal[VMCSCACHE_MAX_ENTRY];
495 } Write;
496 struct
497 {
498 uint32_t cValidEntries;
499 uint32_t uAlignment;
500 uint32_t aField[VMCSCACHE_MAX_ENTRY];
501 uint64_t aFieldVal[VMCSCACHE_MAX_ENTRY];
502 } Read;
503#ifdef DEBUG
504 struct
505 {
506 RTHCPHYS pPageCpuPhys;
507 RTHCPHYS pVMCSPhys;
508 RTGCPTR pCache;
509 RTGCPTR pCtx;
510 } TestIn;
511 struct
512 {
513 RTHCPHYS pVMCSPhys;
514 RTGCPTR pCache;
515 RTGCPTR pCtx;
516 uint64_t eflags;
517 uint64_t cr8;
518 } TestOut;
519 struct
520 {
521 uint64_t param1;
522 uint64_t param2;
523 uint64_t param3;
524 uint64_t param4;
525 } ScratchPad;
526#endif
527} VMCSCACHE;
528/** Pointer to VMCSCACHE. */
529typedef VMCSCACHE *PVMCSCACHE;
530
531/** VMX StartVM function. */
532typedef DECLCALLBACK(int) FNHWACCMVMXSTARTVM(RTHCUINT fResume, PCPUMCTX pCtx, PVMCSCACHE pCache, PVM pVM, PVMCPU pVCpu);
533/** Pointer to a VMX StartVM function. */
534typedef R0PTRTYPE(FNHWACCMVMXSTARTVM *) PFNHWACCMVMXSTARTVM;
535
536/** SVM VMRun function. */
537typedef DECLCALLBACK(int) FNHWACCMSVMVMRUN(RTHCPHYS pVMCBHostPhys, RTHCPHYS pVMCBPhys, PCPUMCTX pCtx, PVM pVM, PVMCPU pVCpu);
538/** Pointer to a SVM VMRun function. */
539typedef R0PTRTYPE(FNHWACCMSVMVMRUN *) PFNHWACCMSVMVMRUN;
540
541/**
542 * HWACCM VMCPU Instance data.
543 */
544typedef struct HWACCMCPU
545{
546 /** Old style FPU reporting trap mask override performed (optimization) */
547 bool fFPUOldStyleOverride;
548
549 /** Set if we don't have to flush the TLB on VM entry. */
550 bool fResumeVM;
551
552 /** Set if we need to flush the TLB during the world switch. */
553 bool fForceTLBFlush;
554
555 /** Set when we're using VT-x or AMD-V at that moment. */
556 bool fActive;
557
558 /** Set when the TLB has been checked until we return from the world switch. */
559 volatile uint8_t fCheckedTLBFlush;
560 uint8_t bAlignment[3];
561
562 /** HWACCM_CHANGED_* flags. */
563 RTUINT fContextUseFlags;
564
565 /** Id of the last cpu we were executing code on (NIL_RTCPUID for the first time) */
566 RTCPUID idLastCpu;
567
568 /** TLB flush count */
569 RTUINT cTLBFlushes;
570
571 /** Current ASID in use by the VM */
572 RTUINT uCurrentASID;
573
574 /** World switch exit counter. */
575 volatile uint32_t cWorldSwitchExit;
576 uint32_t u32Alignment;
577
578 struct
579 {
580 /** Physical address of the VM control structure (VMCS). */
581 RTHCPHYS pVMCSPhys;
582 /** R0 memory object for the VM control structure (VMCS). */
583 RTR0MEMOBJ pMemObjVMCS;
584 /** Virtual address of the VM control structure (VMCS). */
585 R0PTRTYPE(void *) pVMCS;
586
587 /** Ring 0 handlers for VT-x. */
588 PFNHWACCMVMXSTARTVM pfnStartVM;
589
590#if HC_ARCH_BITS == 32
591 uint32_t u32Alignment;
592#endif
593
594 /** Current VMX_VMCS_CTRL_PROC_EXEC_CONTROLS. */
595 uint64_t proc_ctls;
596
597 /** Current VMX_VMCS_CTRL_PROC_EXEC2_CONTROLS. */
598 uint64_t proc_ctls2;
599
600 /** Physical address of the virtual APIC page for TPR caching. */
601 RTHCPHYS pVAPICPhys;
602 /** R0 memory object for the virtual APIC page for TPR caching. */
603 RTR0MEMOBJ pMemObjVAPIC;
604 /** Virtual address of the virtual APIC page for TPR caching. */
605 R0PTRTYPE(uint8_t *) pVAPIC;
606
607 /** Current CR0 mask. */
608 uint64_t cr0_mask;
609 /** Current CR4 mask. */
610 uint64_t cr4_mask;
611
612 /** Current EPTP. */
613 RTHCPHYS GCPhysEPTP;
614
615 /** Physical address of the MSR bitmap (1 page). */
616 RTHCPHYS pMSRBitmapPhys;
617 /** R0 memory object for the MSR bitmap (1 page). */
618 RTR0MEMOBJ pMemObjMSRBitmap;
619 /** Virtual address of the MSR bitmap (1 page). */
620 R0PTRTYPE(uint8_t *) pMSRBitmap;
621
622#ifdef VBOX_WITH_AUTO_MSR_LOAD_RESTORE
623 /** Physical address of the guest MSR load area (1 page). */
624 RTHCPHYS pGuestMSRPhys;
625 /** R0 memory object for the guest MSR load area (1 page). */
626 RTR0MEMOBJ pMemObjGuestMSR;
627 /** Virtual address of the guest MSR load area (1 page). */
628 R0PTRTYPE(uint8_t *) pGuestMSR;
629
630 /** Physical address of the MSR load area (1 page). */
631 RTHCPHYS pHostMSRPhys;
632 /** R0 memory object for the MSR load area (1 page). */
633 RTR0MEMOBJ pMemObjHostMSR;
634 /** Virtual address of the MSR load area (1 page). */
635 R0PTRTYPE(uint8_t *) pHostMSR;
636#endif /* VBOX_WITH_AUTO_MSR_LOAD_RESTORE */
637
638 /* Number of automatically loaded/restored MSRs. */
639 uint32_t cCachedMSRs;
640 uint32_t uAlignement;
641
642 /* Last use TSC offset value. (cached) */
643 uint64_t u64TSCOffset;
644
645 /** VMCS cache. */
646 VMCSCACHE VMCSCache;
647
648 /** Real-mode emulation state. */
649 struct
650 {
651 X86EFLAGS eflags;
652 uint32_t fValid;
653 } RealMode;
654
655 struct
656 {
657 uint64_t u64VMCSPhys;
658 uint32_t ulVMCSRevision;
659 uint32_t ulInstrError;
660 uint32_t ulExitReason;
661 RTCPUID idEnteredCpu;
662 RTCPUID idCurrentCpu;
663 uint32_t padding;
664 } lasterror;
665
666 /** The last seen guest paging mode (by VT-x). */
667 PGMMODE enmLastSeenGuestMode;
668 /** Current guest paging mode (as seen by HWACCMR3PagingModeChanged). */
669 PGMMODE enmCurrGuestMode;
670 /** Previous guest paging mode (as seen by HWACCMR3PagingModeChanged). */
671 PGMMODE enmPrevGuestMode;
672 } vmx;
673
674 struct
675 {
676 /** R0 memory object for the host VM control block (VMCB). */
677 RTR0MEMOBJ pMemObjVMCBHost;
678 /** Physical address of the host VM control block (VMCB). */
679 RTHCPHYS pVMCBHostPhys;
680 /** Virtual address of the host VM control block (VMCB). */
681 R0PTRTYPE(void *) pVMCBHost;
682
683 /** R0 memory object for the VM control block (VMCB). */
684 RTR0MEMOBJ pMemObjVMCB;
685 /** Physical address of the VM control block (VMCB). */
686 RTHCPHYS pVMCBPhys;
687 /** Virtual address of the VM control block (VMCB). */
688 R0PTRTYPE(void *) pVMCB;
689
690 /** Ring 0 handlers for VT-x. */
691 PFNHWACCMSVMVMRUN pfnVMRun;
692
693 /** R0 memory object for the MSR bitmap (8kb). */
694 RTR0MEMOBJ pMemObjMSRBitmap;
695 /** Physical address of the MSR bitmap (8kb). */
696 RTHCPHYS pMSRBitmapPhys;
697 /** Virtual address of the MSR bitmap. */
698 R0PTRTYPE(void *) pMSRBitmap;
699 } svm;
700
701 /** Event injection state. */
702 struct
703 {
704 uint32_t fPending;
705 uint32_t errCode;
706 uint64_t intInfo;
707 } Event;
708
709 /** IO Block emulation state. */
710 struct
711 {
712 bool fEnabled;
713 uint8_t u8Align[7];
714
715 /** RIP at the start of the io code we wish to emulate in the recompiler. */
716 RTGCPTR GCPtrFunctionEip;
717
718 uint64_t cr0;
719 } EmulateIoBlock;
720
721 struct
722 {
723 /* Pending IO operation type. */
724 HWACCMPENDINGIO enmType;
725 uint32_t uPadding;
726 RTGCPTR GCPtrRip;
727 RTGCPTR GCPtrRipNext;
728 union
729 {
730 struct
731 {
732 unsigned uPort;
733 unsigned uAndVal;
734 unsigned cbSize;
735 } Port;
736 uint64_t aRaw[2];
737 } s;
738 } PendingIO;
739
740 /** Currenty shadow paging mode. */
741 PGMMODE enmShadowMode;
742
743 /** The CPU ID of the CPU currently owning the VMCS. Set in
744 * HWACCMR0Enter and cleared in HWACCMR0Leave. */
745 RTCPUID idEnteredCpu;
746
747 /** To keep track of pending TLB shootdown pages. (SMP guest only) */
748 struct
749 {
750 RTGCPTR aPages[HWACCM_MAX_TLB_SHOOTDOWN_PAGES];
751 unsigned cPages;
752 } TlbShootdown;
753
754 /** For saving stack space, the disassembler state is allocated here instead of
755 * on the stack.
756 * @note The DISCPUSTATE structure is not R3/R0/RZ clean! */
757 union
758 {
759 /** The disassembler scratch space. */
760 DISCPUSTATE DisState;
761 /** Padding. */
762 uint8_t abDisStatePadding[DISCPUSTATE_PADDING_SIZE];
763 };
764
765 RTUINT padding2[1];
766
767 STAMPROFILEADV StatEntry;
768 STAMPROFILEADV StatExit1;
769 STAMPROFILEADV StatExit2;
770#if 1 /* temporary for tracking down darwin issues. */
771 STAMPROFILEADV StatExit2Sub1;
772 STAMPROFILEADV StatExit2Sub2;
773 STAMPROFILEADV StatExit2Sub3;
774#endif
775 STAMPROFILEADV StatInGC;
776
777#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
778 STAMPROFILEADV StatWorldSwitch3264;
779#endif
780 STAMPROFILEADV StatPoke;
781 STAMPROFILEADV StatSpinPoke;
782 STAMPROFILEADV StatSpinPokeFailed;
783
784 STAMCOUNTER StatIntInject;
785
786 STAMCOUNTER StatExitShadowNM;
787 STAMCOUNTER StatExitGuestNM;
788 STAMCOUNTER StatExitShadowPF;
789 STAMCOUNTER StatExitGuestPF;
790 STAMCOUNTER StatExitGuestUD;
791 STAMCOUNTER StatExitGuestSS;
792 STAMCOUNTER StatExitGuestNP;
793 STAMCOUNTER StatExitGuestGP;
794 STAMCOUNTER StatExitGuestDE;
795 STAMCOUNTER StatExitGuestDB;
796 STAMCOUNTER StatExitGuestMF;
797 STAMCOUNTER StatExitInvpg;
798 STAMCOUNTER StatExitInvd;
799 STAMCOUNTER StatExitCpuid;
800 STAMCOUNTER StatExitRdtsc;
801 STAMCOUNTER StatExitRdpmc;
802 STAMCOUNTER StatExitCli;
803 STAMCOUNTER StatExitSti;
804 STAMCOUNTER StatExitPushf;
805 STAMCOUNTER StatExitPopf;
806 STAMCOUNTER StatExitIret;
807 STAMCOUNTER StatExitInt;
808 STAMCOUNTER StatExitCRxWrite[16];
809 STAMCOUNTER StatExitCRxRead[16];
810 STAMCOUNTER StatExitDRxWrite;
811 STAMCOUNTER StatExitDRxRead;
812 STAMCOUNTER StatExitRdmsr;
813 STAMCOUNTER StatExitWrmsr;
814 STAMCOUNTER StatExitCLTS;
815 STAMCOUNTER StatExitHlt;
816 STAMCOUNTER StatExitMwait;
817 STAMCOUNTER StatExitLMSW;
818 STAMCOUNTER StatExitIOWrite;
819 STAMCOUNTER StatExitIORead;
820 STAMCOUNTER StatExitIOStringWrite;
821 STAMCOUNTER StatExitIOStringRead;
822 STAMCOUNTER StatExitIrqWindow;
823 STAMCOUNTER StatExitMaxResume;
824 STAMCOUNTER StatExitPreemptPending;
825 STAMCOUNTER StatIntReinject;
826 STAMCOUNTER StatPendingHostIrq;
827
828 STAMCOUNTER StatFlushPage;
829 STAMCOUNTER StatFlushPageManual;
830 STAMCOUNTER StatFlushPhysPageManual;
831 STAMCOUNTER StatFlushTLB;
832 STAMCOUNTER StatFlushTLBManual;
833 STAMCOUNTER StatFlushPageInvlpg;
834 STAMCOUNTER StatFlushTLBWorldSwitch;
835 STAMCOUNTER StatNoFlushTLBWorldSwitch;
836 STAMCOUNTER StatFlushTLBCRxChange;
837 STAMCOUNTER StatFlushASID;
838 STAMCOUNTER StatFlushTLBInvlpga;
839 STAMCOUNTER StatTlbShootdown;
840 STAMCOUNTER StatTlbShootdownFlush;
841
842 STAMCOUNTER StatSwitchGuestIrq;
843 STAMCOUNTER StatSwitchToR3;
844
845 STAMCOUNTER StatTSCOffset;
846 STAMCOUNTER StatTSCIntercept;
847 STAMCOUNTER StatTSCInterceptOverFlow;
848
849 STAMCOUNTER StatExitReasonNPF;
850 STAMCOUNTER StatDRxArmed;
851 STAMCOUNTER StatDRxContextSwitch;
852 STAMCOUNTER StatDRxIOCheck;
853
854
855#ifdef VBOX_WITH_STATISTICS
856 R3PTRTYPE(PSTAMCOUNTER) paStatExitReason;
857 R0PTRTYPE(PSTAMCOUNTER) paStatExitReasonR0;
858 R3PTRTYPE(PSTAMCOUNTER) paStatInjectedIrqs;
859 R0PTRTYPE(PSTAMCOUNTER) paStatInjectedIrqsR0;
860#endif
861} HWACCMCPU;
862/** Pointer to HWACCM VM instance data. */
863typedef HWACCMCPU *PHWACCMCPU;
864
865
866#ifdef IN_RING0
867
868VMMR0DECL(PHWACCM_CPUINFO) HWACCMR0GetCurrentCpu();
869VMMR0DECL(PHWACCM_CPUINFO) HWACCMR0GetCurrentCpuEx(RTCPUID idCpu);
870
871
872#ifdef VBOX_STRICT
873VMMR0DECL(void) HWACCMDumpRegs(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
874VMMR0DECL(void) HWACCMR0DumpDescriptor(PCX86DESCHC pDesc, RTSEL Sel, const char *pszMsg);
875#else
876# define HWACCMDumpRegs(a, b ,c) do { } while (0)
877# define HWACCMR0DumpDescriptor(a, b, c) do { } while (0)
878#endif
879
880/* Dummy callback handlers. */
881VMMR0DECL(int) HWACCMR0DummyEnter(PVM pVM, PVMCPU pVCpu, PHWACCM_CPUINFO pCpu);
882VMMR0DECL(int) HWACCMR0DummyLeave(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
883VMMR0DECL(int) HWACCMR0DummyEnableCpu(PHWACCM_CPUINFO pCpu, PVM pVM, void *pvPageCpu, RTHCPHYS pPageCpuPhys);
884VMMR0DECL(int) HWACCMR0DummyDisableCpu(PHWACCM_CPUINFO pCpu, void *pvPageCpu, RTHCPHYS pPageCpuPhys);
885VMMR0DECL(int) HWACCMR0DummyInitVM(PVM pVM);
886VMMR0DECL(int) HWACCMR0DummyTermVM(PVM pVM);
887VMMR0DECL(int) HWACCMR0DummySetupVM(PVM pVM);
888VMMR0DECL(int) HWACCMR0DummyRunGuestCode(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
889VMMR0DECL(int) HWACCMR0DummySaveHostState(PVM pVM, PVMCPU pVCpu);
890VMMR0DECL(int) HWACCMR0DummyLoadGuestState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
891
892# ifdef VBOX_WITH_KERNEL_USING_XMM
893DECLASM(int) hwaccmR0VMXStartVMWrapXMM(RTHCUINT fResume, PCPUMCTX pCtx, PVMCSCACHE pCache, PVM pVM, PVMCPU pVCpu, PFNHWACCMVMXSTARTVM pfnStartVM);
894DECLASM(int) hwaccmR0SVMRunWrapXMM(RTHCPHYS pVMCBHostPhys, RTHCPHYS pVMCBPhys, PCPUMCTX pCtx, PVM pVM, PVMCPU pVCpu, PFNHWACCMSVMVMRUN pfnVMRun);
895# endif
896
897# ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
898/**
899 * Gets 64-bit GDTR and IDTR on darwin.
900 * @param pGdtr Where to store the 64-bit GDTR.
901 * @param pIdtr Where to store the 64-bit IDTR.
902 */
903DECLASM(void) hwaccmR0Get64bitGDTRandIDTR(PX86XDTR64 pGdtr, PX86XDTR64 pIdtr);
904
905/**
906 * Gets 64-bit CR3 on darwin.
907 * @returns CR3
908 */
909DECLASM(uint64_t) hwaccmR0Get64bitCR3(void);
910# endif
911
912#endif /* IN_RING0 */
913
914/** @} */
915
916RT_C_DECLS_END
917
918#endif
919
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