VirtualBox

source: vbox/trunk/src/VBox/VMM/include/HMInternal.h@ 55830

Last change on this file since 55830 was 55756, checked in by vboxsync, 10 years ago

VMM/HM: Add some rudimentary logging for VERR_VMX_INVALID_VMXON_PTR errors.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 40.3 KB
Line 
1/* $Id: HMInternal.h 55756 2015-05-08 14:28:36Z vboxsync $ */
2/** @file
3 * HM - 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 ___HMInternal_h
19#define ___HMInternal_h
20
21#include <VBox/cdefs.h>
22#include <VBox/types.h>
23#include <VBox/vmm/em.h>
24#include <VBox/vmm/stam.h>
25#include <VBox/dis.h>
26#include <VBox/vmm/hm.h>
27#include <VBox/vmm/hm_vmx.h>
28#include <VBox/vmm/pgm.h>
29#include <VBox/vmm/cpum.h>
30#include <iprt/memobj.h>
31#include <iprt/cpuset.h>
32#include <iprt/mp.h>
33#include <iprt/avl.h>
34#include <iprt/string.h>
35
36#if HC_ARCH_BITS == 64 || defined(VBOX_WITH_HYBRID_32BIT_KERNEL) || defined (VBOX_WITH_64_BITS_GUESTS)
37/* Enable 64 bits guest support. */
38# define VBOX_ENABLE_64_BITS_GUESTS
39#endif
40
41#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
42# define VMX_USE_CACHED_VMCS_ACCESSES
43#endif
44
45/** @def HM_PROFILE_EXIT_DISPATCH
46 * Enables profiling of the VM exit handler dispatching. */
47#if 0
48# define HM_PROFILE_EXIT_DISPATCH
49#endif
50
51RT_C_DECLS_BEGIN
52
53
54/** @defgroup grp_hm_int Internal
55 * @ingroup grp_hm
56 * @internal
57 * @{
58 */
59
60/** @def HMCPU_CF_CLEAR
61 * Clears a HM-context flag.
62 *
63 * @param pVCpu Pointer to the VMCPU.
64 * @param fFlag The flag to clear.
65 */
66#define HMCPU_CF_CLEAR(pVCpu, fFlag) (ASMAtomicUoAndU32(&(pVCpu)->hm.s.fContextUseFlags, ~(fFlag)))
67
68/** @def HMCPU_CF_SET
69 * Sets a HM-context flag.
70 *
71 * @param pVCpu Pointer to the VMCPU.
72 * @param fFlag The flag to set.
73 */
74#define HMCPU_CF_SET(pVCpu, fFlag) (ASMAtomicUoOrU32(&(pVCpu)->hm.s.fContextUseFlags, (fFlag)))
75
76/** @def HMCPU_CF_IS_SET
77 * Checks if all the flags in the specified HM-context set is pending.
78 *
79 * @param pVCpu Pointer to the VMCPU.
80 * @param fFlag The flag to check.
81 */
82#define HMCPU_CF_IS_SET(pVCpu, fFlag) ((ASMAtomicUoReadU32(&(pVCpu)->hm.s.fContextUseFlags) & (fFlag)) == (fFlag))
83
84/** @def HMCPU_CF_IS_PENDING
85 * Checks if one or more of the flags in the specified HM-context set is
86 * pending.
87 *
88 * @param pVCpu Pointer to the VMCPU.
89 * @param fFlags The flags to check for.
90 */
91#define HMCPU_CF_IS_PENDING(pVCpu, fFlags) RT_BOOL(ASMAtomicUoReadU32(&(pVCpu)->hm.s.fContextUseFlags) & (fFlags))
92
93/** @def HMCPU_CF_IS_PENDING_ONLY
94 * Checks if -only- one or more of the specified HM-context flags is pending.
95 *
96 * @param pVCpu Pointer to the VMCPU.
97 * @param fFlags The flags to check for.
98 */
99#define HMCPU_CF_IS_PENDING_ONLY(pVCpu, fFlags) !RT_BOOL(ASMAtomicUoReadU32(&(pVCpu)->hm.s.fContextUseFlags) & ~(fFlags))
100
101/** @def HMCPU_CF_IS_SET_ONLY
102 * Checks if -only- all the flags in the specified HM-context set is pending.
103 *
104 * @param pVCpu Pointer to the VMCPU.
105 * @param fFlags The flags to check for.
106 */
107#define HMCPU_CF_IS_SET_ONLY(pVCpu, fFlags) (ASMAtomicUoReadU32(&(pVCpu)->hm.s.fContextUseFlags) == (fFlags))
108
109/** @def HMCPU_CF_RESET_TO
110 * Resets the HM-context flags to the specified value.
111 *
112 * @param pVCpu Pointer to the VMCPU.
113 * @param fFlags The new value.
114 */
115#define HMCPU_CF_RESET_TO(pVCpu, fFlags) (ASMAtomicUoWriteU32(&(pVCpu)->hm.s.fContextUseFlags, (fFlags)))
116
117/** @def HMCPU_CF_VALUE
118 * Returns the current HM-context flags value.
119 *
120 * @param pVCpu Pointer to the VMCPU.
121 */
122#define HMCPU_CF_VALUE(pVCpu) (ASMAtomicUoReadU32(&(pVCpu)->hm.s.fContextUseFlags))
123
124
125/** Resets/initializes the VM-exit/#VMEXIT history array. */
126#define HMCPU_EXIT_HISTORY_RESET(pVCpu) (memset(&(pVCpu)->hm.s.auExitHistory, 0xff, sizeof((pVCpu)->hm.s.auExitHistory)))
127
128/** Updates the VM-exit/#VMEXIT history array. */
129#define HMCPU_EXIT_HISTORY_ADD(pVCpu, a_ExitReason) \
130 do { \
131 AssertMsg((pVCpu)->hm.s.idxExitHistoryFree < RT_ELEMENTS((pVCpu)->hm.s.auExitHistory), ("%u\n", (pVCpu)->hm.s.idxExitHistoryFree)); \
132 (pVCpu)->hm.s.auExitHistory[(pVCpu)->hm.s.idxExitHistoryFree++] = (uint16_t)(a_ExitReason); \
133 if ((pVCpu)->hm.s.idxExitHistoryFree == RT_ELEMENTS((pVCpu)->hm.s.auExitHistory)) \
134 (pVCpu)->hm.s.idxExitHistoryFree = 0; \
135 (pVCpu)->hm.s.auExitHistory[(pVCpu)->hm.s.idxExitHistoryFree] = UINT16_MAX; \
136 } while (0)
137
138/** Maximum number of exit reason statistics counters. */
139#define MAX_EXITREASON_STAT 0x100
140#define MASK_EXITREASON_STAT 0xff
141#define MASK_INJECT_IRQ_STAT 0xff
142
143/** @name HM changed flags.
144 * These flags are used to keep track of which important registers that
145 * have been changed since last they were reset.
146 * @{
147 */
148#define HM_CHANGED_GUEST_CR0 RT_BIT(0) /* Shared */
149#define HM_CHANGED_GUEST_CR3 RT_BIT(1)
150#define HM_CHANGED_GUEST_CR4 RT_BIT(2)
151#define HM_CHANGED_GUEST_GDTR RT_BIT(3)
152#define HM_CHANGED_GUEST_IDTR RT_BIT(4)
153#define HM_CHANGED_GUEST_LDTR RT_BIT(5)
154#define HM_CHANGED_GUEST_TR RT_BIT(6)
155#define HM_CHANGED_GUEST_SEGMENT_REGS RT_BIT(7)
156#define HM_CHANGED_GUEST_DEBUG RT_BIT(8) /* Shared */
157#define HM_CHANGED_GUEST_RIP RT_BIT(9)
158#define HM_CHANGED_GUEST_RSP RT_BIT(10)
159#define HM_CHANGED_GUEST_RFLAGS RT_BIT(11)
160#define HM_CHANGED_GUEST_CR2 RT_BIT(12)
161#define HM_CHANGED_GUEST_SYSENTER_CS_MSR RT_BIT(13)
162#define HM_CHANGED_GUEST_SYSENTER_EIP_MSR RT_BIT(14)
163#define HM_CHANGED_GUEST_SYSENTER_ESP_MSR RT_BIT(15)
164#define HM_CHANGED_GUEST_EFER_MSR RT_BIT(16)
165#define HM_CHANGED_GUEST_LAZY_MSRS RT_BIT(17) /* Shared */
166#define HM_CHANGED_GUEST_XCPT_INTERCEPTS RT_BIT(18)
167/* VT-x specific state. */
168#define HM_CHANGED_VMX_GUEST_AUTO_MSRS RT_BIT(19)
169#define HM_CHANGED_VMX_GUEST_ACTIVITY_STATE RT_BIT(20)
170#define HM_CHANGED_VMX_GUEST_APIC_STATE RT_BIT(21)
171#define HM_CHANGED_VMX_ENTRY_CTLS RT_BIT(22)
172#define HM_CHANGED_VMX_EXIT_CTLS RT_BIT(23)
173/* AMD-V specific state. */
174#define HM_CHANGED_SVM_GUEST_APIC_STATE RT_BIT(19)
175#define HM_CHANGED_SVM_RESERVED1 RT_BIT(20)
176#define HM_CHANGED_SVM_RESERVED2 RT_BIT(21)
177#define HM_CHANGED_SVM_RESERVED3 RT_BIT(22)
178#define HM_CHANGED_SVM_RESERVED4 RT_BIT(23)
179
180#define HM_CHANGED_ALL_GUEST ( HM_CHANGED_GUEST_CR0 \
181 | HM_CHANGED_GUEST_CR3 \
182 | HM_CHANGED_GUEST_CR4 \
183 | HM_CHANGED_GUEST_GDTR \
184 | HM_CHANGED_GUEST_IDTR \
185 | HM_CHANGED_GUEST_LDTR \
186 | HM_CHANGED_GUEST_TR \
187 | HM_CHANGED_GUEST_SEGMENT_REGS \
188 | HM_CHANGED_GUEST_DEBUG \
189 | HM_CHANGED_GUEST_RIP \
190 | HM_CHANGED_GUEST_RSP \
191 | HM_CHANGED_GUEST_RFLAGS \
192 | HM_CHANGED_GUEST_CR2 \
193 | HM_CHANGED_GUEST_SYSENTER_CS_MSR \
194 | HM_CHANGED_GUEST_SYSENTER_EIP_MSR \
195 | HM_CHANGED_GUEST_SYSENTER_ESP_MSR \
196 | HM_CHANGED_GUEST_EFER_MSR \
197 | HM_CHANGED_GUEST_LAZY_MSRS \
198 | HM_CHANGED_GUEST_XCPT_INTERCEPTS \
199 | HM_CHANGED_VMX_GUEST_AUTO_MSRS \
200 | HM_CHANGED_VMX_GUEST_ACTIVITY_STATE \
201 | HM_CHANGED_VMX_GUEST_APIC_STATE \
202 | HM_CHANGED_VMX_ENTRY_CTLS \
203 | HM_CHANGED_VMX_EXIT_CTLS)
204
205#define HM_CHANGED_HOST_CONTEXT RT_BIT(24)
206
207/* Bits shared between host and guest. */
208#define HM_CHANGED_HOST_GUEST_SHARED_STATE ( HM_CHANGED_GUEST_CR0 \
209 | HM_CHANGED_GUEST_DEBUG \
210 | HM_CHANGED_GUEST_LAZY_MSRS)
211/** @} */
212
213/** Maximum number of page flushes we are willing to remember before considering a full TLB flush. */
214#define HM_MAX_TLB_SHOOTDOWN_PAGES 8
215
216/** Size for the EPT identity page table (1024 4 MB pages to cover the entire address space). */
217#define HM_EPT_IDENTITY_PG_TABLE_SIZE PAGE_SIZE
218/** Size of the TSS structure + 2 pages for the IO bitmap + end byte. */
219#define HM_VTX_TSS_SIZE (sizeof(VBOXTSS) + 2 * PAGE_SIZE + 1)
220/** Total guest mapped memory needed. */
221#define HM_VTX_TOTAL_DEVHEAP_MEM (HM_EPT_IDENTITY_PG_TABLE_SIZE + HM_VTX_TSS_SIZE)
222
223/** Enable for TPR guest patching. */
224#define VBOX_HM_WITH_GUEST_PATCHING
225
226/** HM SSM version
227 */
228#ifdef VBOX_HM_WITH_GUEST_PATCHING
229# define HM_SAVED_STATE_VERSION 5
230# define HM_SAVED_STATE_VERSION_NO_PATCHING 4
231#else
232# define HM_SAVED_STATE_VERSION 4
233# define HM_SAVED_STATE_VERSION_NO_PATCHING 4
234#endif
235#define HM_SAVED_STATE_VERSION_2_0_X 3
236
237/**
238 * Global per-cpu information. (host)
239 */
240typedef struct HMGLOBALCPUINFO
241{
242 /** The CPU ID. */
243 RTCPUID idCpu;
244 /** The VM_HSAVE_AREA (AMD-V) / VMXON region (Intel) memory backing. */
245 RTR0MEMOBJ hMemObj;
246 /** Current ASID (AMD-V) / VPID (Intel). */
247 uint32_t uCurrentAsid;
248 /** TLB flush count. */
249 uint32_t cTlbFlushes;
250 /** Whether to flush each new ASID/VPID before use. */
251 bool fFlushAsidBeforeUse;
252 /** Configured for VT-x or AMD-V. */
253 bool fConfigured;
254 /** Set if the VBOX_HWVIRTEX_IGNORE_SVM_IN_USE hack is active. */
255 bool fIgnoreAMDVInUseError;
256 /** In use by our code. (for power suspend) */
257 volatile bool fInUse;
258} HMGLOBALCPUINFO;
259/** Pointer to the per-cpu global information. */
260typedef HMGLOBALCPUINFO *PHMGLOBALCPUINFO;
261
262typedef enum
263{
264 HMPENDINGIO_INVALID = 0,
265 HMPENDINGIO_PORT_READ,
266 HMPENDINGIO_PORT_WRITE,
267 HMPENDINGIO_STRING_READ,
268 HMPENDINGIO_STRING_WRITE,
269 /** The usual 32-bit paranoia. */
270 HMPENDINGIO_32BIT_HACK = 0x7fffffff
271} HMPENDINGIO;
272
273
274typedef enum
275{
276 HMTPRINSTR_INVALID,
277 HMTPRINSTR_READ,
278 HMTPRINSTR_READ_SHR4,
279 HMTPRINSTR_WRITE_REG,
280 HMTPRINSTR_WRITE_IMM,
281 HMTPRINSTR_JUMP_REPLACEMENT,
282 /** The usual 32-bit paranoia. */
283 HMTPRINSTR_32BIT_HACK = 0x7fffffff
284} HMTPRINSTR;
285
286typedef struct
287{
288 /** The key is the address of patched instruction. (32 bits GC ptr) */
289 AVLOU32NODECORE Core;
290 /** Original opcode. */
291 uint8_t aOpcode[16];
292 /** Instruction size. */
293 uint32_t cbOp;
294 /** Replacement opcode. */
295 uint8_t aNewOpcode[16];
296 /** Replacement instruction size. */
297 uint32_t cbNewOp;
298 /** Instruction type. */
299 HMTPRINSTR enmType;
300 /** Source operand. */
301 uint32_t uSrcOperand;
302 /** Destination operand. */
303 uint32_t uDstOperand;
304 /** Number of times the instruction caused a fault. */
305 uint32_t cFaults;
306 /** Patch address of the jump replacement. */
307 RTGCPTR32 pJumpTarget;
308} HMTPRPATCH;
309/** Pointer to HMTPRPATCH. */
310typedef HMTPRPATCH *PHMTPRPATCH;
311
312/**
313 * Switcher function, HC to the special 64-bit RC.
314 *
315 * @param pVM Pointer to the VM.
316 * @param offCpumVCpu Offset from pVM->cpum to pVM->aCpus[idCpu].cpum.
317 * @returns Return code indicating the action to take.
318 */
319typedef DECLCALLBACK(int) FNHMSWITCHERHC(PVM pVM, uint32_t offCpumVCpu);
320/** Pointer to switcher function. */
321typedef FNHMSWITCHERHC *PFNHMSWITCHERHC;
322
323/**
324 * HM VM Instance data.
325 * Changes to this must checked against the padding of the hm union in VM!
326 */
327typedef struct HM
328{
329 /** Set when we've initialized VMX or SVM. */
330 bool fInitialized;
331 /** Set if nested paging is enabled. */
332 bool fNestedPaging;
333 /** Set if nested paging is allowed. */
334 bool fAllowNestedPaging;
335 /** Set if large pages are enabled (requires nested paging). */
336 bool fLargePages;
337 /** Set if we can support 64-bit guests or not. */
338 bool fAllow64BitGuests;
339 /** Set if an IO-APIC is configured for this VM. */
340 bool fHasIoApic;
341 /** Set when TPR patching is allowed. */
342 bool fTprPatchingAllowed;
343 /** Set when we initialize VT-x or AMD-V once for all CPUs. */
344 bool fGlobalInit;
345 /** Set when TPR patching is active. */
346 bool fTPRPatchingActive;
347 bool u8Alignment[3];
348
349 /** Host kernel flags that HM might need to know (SUPKERNELFEATURES_XXX). */
350 uint32_t uHostKernelFeatures;
351
352 /** Maximum ASID allowed. */
353 uint32_t uMaxAsid;
354 /** The maximum number of resumes loops allowed in ring-0 (safety precaution).
355 * This number is set much higher when RTThreadPreemptIsPending is reliable. */
356 uint32_t cMaxResumeLoops;
357
358 /** Guest allocated memory for patching purposes. */
359 RTGCPTR pGuestPatchMem;
360 /** Current free pointer inside the patch block. */
361 RTGCPTR pFreeGuestPatchMem;
362 /** Size of the guest patch memory block. */
363 uint32_t cbGuestPatchMem;
364 uint32_t u32Alignment0;
365
366#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
367 /** 32 to 64 bits switcher entrypoint. */
368 R0PTRTYPE(PFNHMSWITCHERHC) pfnHost32ToGuest64R0;
369 RTR0PTR pvR0Alignment0;
370#endif
371
372 struct
373 {
374 /** Set by the ring-0 side of HM to indicate VMX is supported by the
375 * CPU. */
376 bool fSupported;
377 /** Set when we've enabled VMX. */
378 bool fEnabled;
379 /** Set if VPID is supported. */
380 bool fVpid;
381 /** Set if VT-x VPID is allowed. */
382 bool fAllowVpid;
383 /** Set if unrestricted guest execution is in use (real and protected mode without paging). */
384 bool fUnrestrictedGuest;
385 /** Set if unrestricted guest execution is allowed to be used. */
386 bool fAllowUnrestricted;
387 /** Whether we're using the preemption timer or not. */
388 bool fUsePreemptTimer;
389 /** The shift mask employed by the VMX-Preemption timer. */
390 uint8_t cPreemptTimerShift;
391
392 /** Virtual address of the TSS page used for real mode emulation. */
393 R3PTRTYPE(PVBOXTSS) pRealModeTSS;
394 /** Virtual address of the identity page table used for real mode and protected mode without paging emulation in EPT mode. */
395 R3PTRTYPE(PX86PD) pNonPagingModeEPTPageTable;
396
397 /** Physical address of the APIC-access page. */
398 RTHCPHYS HCPhysApicAccess;
399 /** R0 memory object for the APIC-access page. */
400 RTR0MEMOBJ hMemObjApicAccess;
401 /** Virtual address of the APIC-access page. */
402 R0PTRTYPE(uint8_t *) pbApicAccess;
403
404#ifdef VBOX_WITH_CRASHDUMP_MAGIC
405 RTHCPHYS HCPhysScratch;
406 RTR0MEMOBJ hMemObjScratch;
407 R0PTRTYPE(uint8_t *) pbScratch;
408#endif
409
410 /** Internal Id of which flush-handler to use for tagged-TLB entries. */
411 uint32_t uFlushTaggedTlb;
412 uint32_t u32Alignment0;
413 /** Host CR4 value (set by ring-0 VMX init) */
414 uint64_t u64HostCr4;
415
416 /** Host EFER value (set by ring-0 VMX init) */
417 uint64_t u64HostEfer;
418 /** Whether the CPU supports VMCS fields for swapping EFER. */
419 bool fSupportsVmcsEfer;
420 uint8_t u8Alignment2[7];
421
422 /** VMX MSR values */
423 VMXMSRS Msrs;
424
425 /** Flush types for invept & invvpid; they depend on capabilities. */
426 VMXFLUSHEPT enmFlushEpt;
427 VMXFLUSHVPID enmFlushVpid;
428
429 /** Host-physical address for a failing VMXON instruction. */
430 RTHCPHYS HCPhysVmxEnableError;
431 } vmx;
432
433 struct
434 {
435 /** Set by the ring-0 side of HM to indicate SVM is supported by the
436 * CPU. */
437 bool fSupported;
438 /** Set when we've enabled SVM. */
439 bool fEnabled;
440 /** Set if erratum 170 affects the AMD cpu. */
441 bool fAlwaysFlushTLB;
442 /** Set when the hack to ignore VERR_SVM_IN_USE is active. */
443 bool fIgnoreInUseError;
444 uint8_t u8Alignment0[4];
445
446 /** Physical address of the IO bitmap (12kb). */
447 RTHCPHYS HCPhysIOBitmap;
448 /** R0 memory object for the IO bitmap (12kb). */
449 RTR0MEMOBJ hMemObjIOBitmap;
450 /** Virtual address of the IO bitmap. */
451 R0PTRTYPE(void *) pvIOBitmap;
452
453 /* HWCR MSR (for diagnostics) */
454 uint64_t u64MsrHwcr;
455
456 /** SVM revision. */
457 uint32_t u32Rev;
458 /** SVM feature bits from cpuid 0x8000000a */
459 uint32_t u32Features;
460 } svm;
461
462 /**
463 * AVL tree with all patches (active or disabled) sorted by guest instruction
464 * address.
465 */
466 AVLOU32TREE PatchTree;
467 uint32_t cPatches;
468 HMTPRPATCH aPatches[64];
469
470 struct
471 {
472 uint32_t u32AMDFeatureECX;
473 uint32_t u32AMDFeatureEDX;
474 } cpuid;
475
476 /** Saved error from detection */
477 int32_t lLastError;
478
479 /** HMR0Init was run */
480 bool fHMR0Init;
481 bool u8Alignment1[7];
482
483 STAMCOUNTER StatTprPatchSuccess;
484 STAMCOUNTER StatTprPatchFailure;
485 STAMCOUNTER StatTprReplaceSuccess;
486 STAMCOUNTER StatTprReplaceFailure;
487} HM;
488/** Pointer to HM VM instance data. */
489typedef HM *PHM;
490
491/* Maximum number of cached entries. */
492#define VMCSCACHE_MAX_ENTRY 128
493
494/**
495 * Structure for storing read and write VMCS actions.
496 */
497typedef struct VMCSCACHE
498{
499#ifdef VBOX_WITH_CRASHDUMP_MAGIC
500 /* Magic marker for searching in crash dumps. */
501 uint8_t aMagic[16];
502 uint64_t uMagic;
503 uint64_t u64TimeEntry;
504 uint64_t u64TimeSwitch;
505 uint64_t cResume;
506 uint64_t interPD;
507 uint64_t pSwitcher;
508 uint32_t uPos;
509 uint32_t idCpu;
510#endif
511 /* CR2 is saved here for EPT syncing. */
512 uint64_t cr2;
513 struct
514 {
515 uint32_t cValidEntries;
516 uint32_t uAlignment;
517 uint32_t aField[VMCSCACHE_MAX_ENTRY];
518 uint64_t aFieldVal[VMCSCACHE_MAX_ENTRY];
519 } Write;
520 struct
521 {
522 uint32_t cValidEntries;
523 uint32_t uAlignment;
524 uint32_t aField[VMCSCACHE_MAX_ENTRY];
525 uint64_t aFieldVal[VMCSCACHE_MAX_ENTRY];
526 } Read;
527#ifdef VBOX_STRICT
528 struct
529 {
530 RTHCPHYS HCPhysCpuPage;
531 RTHCPHYS HCPhysVmcs;
532 RTGCPTR pCache;
533 RTGCPTR pCtx;
534 } TestIn;
535 struct
536 {
537 RTHCPHYS HCPhysVmcs;
538 RTGCPTR pCache;
539 RTGCPTR pCtx;
540 uint64_t eflags;
541 uint64_t cr8;
542 } TestOut;
543 struct
544 {
545 uint64_t param1;
546 uint64_t param2;
547 uint64_t param3;
548 uint64_t param4;
549 } ScratchPad;
550#endif
551} VMCSCACHE;
552/** Pointer to VMCSCACHE. */
553typedef VMCSCACHE *PVMCSCACHE;
554AssertCompileSizeAlignment(VMCSCACHE, 8);
555
556/** VMX StartVM function. */
557typedef DECLCALLBACK(int) FNHMVMXSTARTVM(RTHCUINT fResume, PCPUMCTX pCtx, PVMCSCACHE pCache, PVM pVM, PVMCPU pVCpu);
558/** Pointer to a VMX StartVM function. */
559typedef R0PTRTYPE(FNHMVMXSTARTVM *) PFNHMVMXSTARTVM;
560
561/** SVM VMRun function. */
562typedef DECLCALLBACK(int) FNHMSVMVMRUN(RTHCPHYS pVmcbHostPhys, RTHCPHYS pVmcbPhys, PCPUMCTX pCtx, PVM pVM, PVMCPU pVCpu);
563/** Pointer to a SVM VMRun function. */
564typedef R0PTRTYPE(FNHMSVMVMRUN *) PFNHMSVMVMRUN;
565
566/**
567 * HM VMCPU Instance data.
568 *
569 * Note! If you change members of this struct, make sure to check if the
570 * assembly counterpart in HMInternal.mac needs to be updated as well.
571 */
572typedef struct HMCPU
573{
574 /** Set if we need to flush the TLB during the world switch. */
575 bool fForceTLBFlush;
576 /** Set when we're using VT-x or AMD-V at that moment. */
577 bool fActive;
578 /** Set when the TLB has been checked until we return from the world switch. */
579 volatile bool fCheckedTLBFlush;
580 /** Whether we're executing a single instruction. */
581 bool fSingleInstruction;
582 /** Set if we need to clear the trap flag because of single stepping. */
583 bool fClearTrapFlag;
584 /** Whether we've completed the inner HM leave function. */
585 bool fLeaveDone;
586 /** Whether we're using the hyper DR7 or guest DR7. */
587 bool fUsingHyperDR7;
588 /** Whether to preload the guest-FPU state to avoid #NM VM-exit overhead. */
589 bool fPreloadGuestFpu;
590 /** Set if XCR0 needs to be loaded and saved when entering and exiting guest
591 * code execution. */
592 bool fLoadSaveGuestXcr0;
593
594 /** Whether #UD needs to be intercepted (required by certain GIM providers). */
595 bool fGIMTrapXcptUD;
596 /** Whether paravirt. hypercalls are enabled. */
597 bool fHypercallsEnabled;
598 uint8_t u8Alignment0[5];
599
600 /** World switch exit counter. */
601 volatile uint32_t cWorldSwitchExits;
602 /** HM_CHANGED_* flags. */
603 volatile uint32_t fContextUseFlags;
604 /** Id of the last cpu we were executing code on (NIL_RTCPUID for the first
605 * time). */
606 RTCPUID idLastCpu;
607 /** TLB flush count. */
608 uint32_t cTlbFlushes;
609 /** Current ASID in use by the VM. */
610 uint32_t uCurrentAsid;
611 /** An additional error code used for some gurus. */
612 uint32_t u32HMError;
613 /** Host's TSC_AUX MSR (used when RDTSCP doesn't cause VM-exits). */
614 uint64_t u64HostTscAux;
615
616 struct
617 {
618 /** Ring 0 handlers for VT-x. */
619 PFNHMVMXSTARTVM pfnStartVM;
620#if HC_ARCH_BITS == 32
621 uint32_t u32Alignment0;
622#endif
623 /** Current VMX_VMCS32_CTRL_PIN_EXEC. */
624 uint32_t u32PinCtls;
625 /** Current VMX_VMCS32_CTRL_PROC_EXEC. */
626 uint32_t u32ProcCtls;
627 /** Current VMX_VMCS32_CTRL_PROC_EXEC2. */
628 uint32_t u32ProcCtls2;
629 /** Current VMX_VMCS32_CTRL_EXIT. */
630 uint32_t u32ExitCtls;
631 /** Current VMX_VMCS32_CTRL_ENTRY. */
632 uint32_t u32EntryCtls;
633
634 /** Current CR0 mask. */
635 uint32_t u32CR0Mask;
636 /** Current CR4 mask. */
637 uint32_t u32CR4Mask;
638 /** Current exception bitmap. */
639 uint32_t u32XcptBitmap;
640 /** The updated-guest-state mask. */
641 volatile uint32_t fUpdatedGuestState;
642 uint32_t u32Alignment1;
643
644 /** Physical address of the VM control structure (VMCS). */
645 RTHCPHYS HCPhysVmcs;
646 /** R0 memory object for the VM control structure (VMCS). */
647 RTR0MEMOBJ hMemObjVmcs;
648 /** Virtual address of the VM control structure (VMCS). */
649 R0PTRTYPE(void *) pvVmcs;
650
651 /** Physical address of the virtual APIC page for TPR caching. */
652 RTHCPHYS HCPhysVirtApic;
653 /** R0 memory object for the virtual APIC page for TPR caching. */
654 RTR0MEMOBJ hMemObjVirtApic;
655 /** Virtual address of the virtual APIC page for TPR caching. */
656 R0PTRTYPE(uint8_t *) pbVirtApic;
657
658 /** Physical address of the MSR bitmap. */
659 RTHCPHYS HCPhysMsrBitmap;
660 /** R0 memory object for the MSR bitmap. */
661 RTR0MEMOBJ hMemObjMsrBitmap;
662 /** Virtual address of the MSR bitmap. */
663 R0PTRTYPE(void *) pvMsrBitmap;
664
665 /** Physical address of the VM-entry MSR-load and VM-exit MSR-store area (used
666 * for guest MSRs). */
667 RTHCPHYS HCPhysGuestMsr;
668 /** R0 memory object of the VM-entry MSR-load and VM-exit MSR-store area
669 * (used for guest MSRs). */
670 RTR0MEMOBJ hMemObjGuestMsr;
671 /** Virtual address of the VM-entry MSR-load and VM-exit MSR-store area (used
672 * for guest MSRs). */
673 R0PTRTYPE(void *) pvGuestMsr;
674
675 /** Physical address of the VM-exit MSR-load area (used for host MSRs). */
676 RTHCPHYS HCPhysHostMsr;
677 /** R0 memory object for the VM-exit MSR-load area (used for host MSRs). */
678 RTR0MEMOBJ hMemObjHostMsr;
679 /** Virtual address of the VM-exit MSR-load area (used for host MSRs). */
680 R0PTRTYPE(void *) pvHostMsr;
681
682 /** Current EPTP. */
683 RTHCPHYS HCPhysEPTP;
684
685 /** Number of guest/host MSR pairs in the auto-load/store area. */
686 uint32_t cMsrs;
687 /** Whether the host MSR values are up-to-date in the auto-load/store area. */
688 bool fUpdatedHostMsrs;
689 uint8_t u8Alignment0[3];
690
691 /** Host LSTAR MSR value to restore lazily while leaving VT-x. */
692 uint64_t u64HostLStarMsr;
693 /** Host STAR MSR value to restore lazily while leaving VT-x. */
694 uint64_t u64HostStarMsr;
695 /** Host SF_MASK MSR value to restore lazily while leaving VT-x. */
696 uint64_t u64HostSFMaskMsr;
697 /** Host KernelGS-Base MSR value to restore lazily while leaving VT-x. */
698 uint64_t u64HostKernelGSBaseMsr;
699 /** A mask of which MSRs have been swapped and need restoration. */
700 uint32_t fLazyMsrs;
701 uint32_t u32Alignment2;
702
703 /** The cached APIC-base MSR used for identifying when to map the HC physical APIC-access page. */
704 uint64_t u64MsrApicBase;
705 /** Last use TSC offset value. (cached) */
706 uint64_t u64TSCOffset;
707
708 /** VMCS cache. */
709 VMCSCACHE VMCSCache;
710
711 /** Real-mode emulation state. */
712 struct
713 {
714 X86DESCATTR AttrCS;
715 X86DESCATTR AttrDS;
716 X86DESCATTR AttrES;
717 X86DESCATTR AttrFS;
718 X86DESCATTR AttrGS;
719 X86DESCATTR AttrSS;
720 X86EFLAGS Eflags;
721 uint32_t fRealOnV86Active;
722 } RealMode;
723
724 /** VT-x error-reporting (mainly for ring-3 propagation). */
725 struct
726 {
727 uint64_t u64VMCSPhys;
728 uint32_t u32VMCSRevision;
729 uint32_t u32InstrError;
730 uint32_t u32ExitReason;
731 RTCPUID idEnteredCpu;
732 RTCPUID idCurrentCpu;
733 uint32_t u32Alignment0;
734 } LastError;
735
736 /** Current state of the VMCS. */
737 uint32_t uVmcsState;
738 /** Which host-state bits to restore before being preempted. */
739 uint32_t fRestoreHostFlags;
740 /** The host-state restoration structure. */
741 VMXRESTOREHOST RestoreHost;
742
743 /** Set if guest was executing in real mode (extra checks). */
744 bool fWasInRealMode;
745 uint8_t u8Alignment1[7];
746 } vmx;
747
748 struct
749 {
750 /** Ring 0 handlers for VT-x. */
751 PFNHMSVMVMRUN pfnVMRun;
752#if HC_ARCH_BITS == 32
753 uint32_t u32Alignment0;
754#endif
755
756 /** Physical address of the host VMCB which holds additional host-state. */
757 RTHCPHYS HCPhysVmcbHost;
758 /** R0 memory object for the host VMCB which holds additional host-state. */
759 RTR0MEMOBJ hMemObjVmcbHost;
760 /** Virtual address of the host VMCB which holds additional host-state. */
761 R0PTRTYPE(void *) pvVmcbHost;
762
763 /** Physical address of the guest VMCB. */
764 RTHCPHYS HCPhysVmcb;
765 /** R0 memory object for the guest VMCB. */
766 RTR0MEMOBJ hMemObjVmcb;
767 /** Virtual address of the guest VMCB. */
768 R0PTRTYPE(void *) pvVmcb;
769
770 /** Physical address of the MSR bitmap (8 KB). */
771 RTHCPHYS HCPhysMsrBitmap;
772 /** R0 memory object for the MSR bitmap (8 KB). */
773 RTR0MEMOBJ hMemObjMsrBitmap;
774 /** Virtual address of the MSR bitmap. */
775 R0PTRTYPE(void *) pvMsrBitmap;
776
777 /** Whether VTPR with V_INTR_MASKING set is in effect, indicating
778 * we should check if the VTPR changed on every VM-exit. */
779 bool fSyncVTpr;
780 uint8_t u8Alignment0[7];
781 } svm;
782
783 /** Event injection state. */
784 struct
785 {
786 uint32_t fPending;
787 uint32_t u32ErrCode;
788 uint32_t cbInstr;
789 uint32_t u32Padding; /**< Explicit alignment padding. */
790 uint64_t u64IntInfo;
791 RTGCUINTPTR GCPtrFaultAddress;
792 } Event;
793
794 /** IO Block emulation state. */
795 struct
796 {
797 bool fEnabled;
798 uint8_t u8Align[7];
799
800 /** RIP at the start of the io code we wish to emulate in the recompiler. */
801 RTGCPTR GCPtrFunctionEip;
802
803 uint64_t cr0;
804 } EmulateIoBlock;
805
806 struct
807 {
808 /** Pending IO operation type. */
809 HMPENDINGIO enmType;
810 uint32_t u32Alignment0;
811 RTGCPTR GCPtrRip;
812 RTGCPTR GCPtrRipNext;
813 union
814 {
815 struct
816 {
817 uint32_t uPort;
818 uint32_t uAndVal;
819 uint32_t cbSize;
820 } Port;
821 uint64_t aRaw[2];
822 } s;
823 } PendingIO;
824
825 /** The PAE PDPEs used with Nested Paging (only valid when
826 * VMCPU_FF_HM_UPDATE_PAE_PDPES is set). */
827 X86PDPE aPdpes[4];
828
829 /** Current shadow paging mode. */
830 PGMMODE enmShadowMode;
831
832 /** The CPU ID of the CPU currently owning the VMCS. Set in
833 * HMR0Enter and cleared in HMR0Leave. */
834 RTCPUID idEnteredCpu;
835
836 /** To keep track of pending TLB shootdown pages. (SMP guest only) */
837 struct
838 {
839 RTGCPTR aPages[HM_MAX_TLB_SHOOTDOWN_PAGES];
840 uint32_t cPages;
841 uint32_t u32Alignment0; /**< Explicit alignment padding. */
842 } TlbShootdown;
843
844 /** VT-x/AMD-V VM-exit/#VMXEXIT history, circular array. */
845 uint16_t auExitHistory[31];
846 /** The index of the next free slot in the history array. */
847 uint16_t idxExitHistoryFree;
848
849 /** For saving stack space, the disassembler state is allocated here instead of
850 * on the stack. */
851 DISCPUSTATE DisState;
852
853 STAMPROFILEADV StatEntry;
854 STAMPROFILEADV StatExit1;
855 STAMPROFILEADV StatExit2;
856 STAMPROFILEADV StatExitIO;
857 STAMPROFILEADV StatExitMovCRx;
858 STAMPROFILEADV StatExitXcptNmi;
859 STAMPROFILEADV StatLoadGuestState;
860 STAMPROFILEADV StatInGC;
861
862#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
863 STAMPROFILEADV StatWorldSwitch3264;
864#endif
865 STAMPROFILEADV StatPoke;
866 STAMPROFILEADV StatSpinPoke;
867 STAMPROFILEADV StatSpinPokeFailed;
868
869 STAMCOUNTER StatInjectInterrupt;
870 STAMCOUNTER StatInjectXcpt;
871 STAMCOUNTER StatInjectPendingReflect;
872
873 STAMCOUNTER StatExitAll;
874 STAMCOUNTER StatExitShadowNM;
875 STAMCOUNTER StatExitGuestNM;
876 STAMCOUNTER StatExitShadowPF; /* Misleading, currently used for MMIO #PFs as well. */
877 STAMCOUNTER StatExitShadowPFEM;
878 STAMCOUNTER StatExitGuestPF;
879 STAMCOUNTER StatExitGuestUD;
880 STAMCOUNTER StatExitGuestSS;
881 STAMCOUNTER StatExitGuestNP;
882 STAMCOUNTER StatExitGuestTS;
883 STAMCOUNTER StatExitGuestGP;
884 STAMCOUNTER StatExitGuestDE;
885 STAMCOUNTER StatExitGuestDB;
886 STAMCOUNTER StatExitGuestMF;
887 STAMCOUNTER StatExitGuestBP;
888 STAMCOUNTER StatExitGuestXF;
889 STAMCOUNTER StatExitGuestXcpUnk;
890 STAMCOUNTER StatExitInvlpg;
891 STAMCOUNTER StatExitInvd;
892 STAMCOUNTER StatExitWbinvd;
893 STAMCOUNTER StatExitPause;
894 STAMCOUNTER StatExitCpuid;
895 STAMCOUNTER StatExitRdtsc;
896 STAMCOUNTER StatExitRdtscp;
897 STAMCOUNTER StatExitRdpmc;
898 STAMCOUNTER StatExitVmcall;
899 STAMCOUNTER StatExitRdrand;
900 STAMCOUNTER StatExitCli;
901 STAMCOUNTER StatExitSti;
902 STAMCOUNTER StatExitPushf;
903 STAMCOUNTER StatExitPopf;
904 STAMCOUNTER StatExitIret;
905 STAMCOUNTER StatExitInt;
906 STAMCOUNTER StatExitCRxWrite[16];
907 STAMCOUNTER StatExitCRxRead[16];
908 STAMCOUNTER StatExitDRxWrite;
909 STAMCOUNTER StatExitDRxRead;
910 STAMCOUNTER StatExitRdmsr;
911 STAMCOUNTER StatExitWrmsr;
912 STAMCOUNTER StatExitClts;
913 STAMCOUNTER StatExitXdtrAccess;
914 STAMCOUNTER StatExitHlt;
915 STAMCOUNTER StatExitMwait;
916 STAMCOUNTER StatExitMonitor;
917 STAMCOUNTER StatExitLmsw;
918 STAMCOUNTER StatExitIOWrite;
919 STAMCOUNTER StatExitIORead;
920 STAMCOUNTER StatExitIOStringWrite;
921 STAMCOUNTER StatExitIOStringRead;
922 STAMCOUNTER StatExitIntWindow;
923 STAMCOUNTER StatExitExtInt;
924 STAMCOUNTER StatExitHostNmiInGC;
925 STAMCOUNTER StatExitPreemptTimer;
926 STAMCOUNTER StatExitTprBelowThreshold;
927 STAMCOUNTER StatExitTaskSwitch;
928 STAMCOUNTER StatExitMtf;
929 STAMCOUNTER StatExitApicAccess;
930 STAMCOUNTER StatPendingHostIrq;
931
932 STAMCOUNTER StatPreemptPreempting;
933 STAMCOUNTER StatPreemptSaveHostState;
934
935 STAMCOUNTER StatFlushPage;
936 STAMCOUNTER StatFlushPageManual;
937 STAMCOUNTER StatFlushPhysPageManual;
938 STAMCOUNTER StatFlushTlb;
939 STAMCOUNTER StatFlushTlbManual;
940 STAMCOUNTER StatFlushTlbWorldSwitch;
941 STAMCOUNTER StatNoFlushTlbWorldSwitch;
942 STAMCOUNTER StatFlushEntire;
943 STAMCOUNTER StatFlushAsid;
944 STAMCOUNTER StatFlushNestedPaging;
945 STAMCOUNTER StatFlushTlbInvlpgVirt;
946 STAMCOUNTER StatFlushTlbInvlpgPhys;
947 STAMCOUNTER StatTlbShootdown;
948 STAMCOUNTER StatTlbShootdownFlush;
949
950 STAMCOUNTER StatSwitchGuestIrq;
951 STAMCOUNTER StatSwitchHmToR3FF;
952 STAMCOUNTER StatSwitchExitToR3;
953 STAMCOUNTER StatSwitchLongJmpToR3;
954 STAMCOUNTER StatSwitchMaxResumeLoops;
955 STAMCOUNTER StatSwitchHltToR3;
956 STAMCOUNTER StatSwitchApicAccessToR3;
957
958 STAMCOUNTER StatTscParavirt;
959 STAMCOUNTER StatTscOffset;
960 STAMCOUNTER StatTscIntercept;
961
962 STAMCOUNTER StatExitReasonNpf;
963 STAMCOUNTER StatDRxArmed;
964 STAMCOUNTER StatDRxContextSwitch;
965 STAMCOUNTER StatDRxIoCheck;
966
967 STAMCOUNTER StatLoadMinimal;
968 STAMCOUNTER StatLoadFull;
969
970 STAMCOUNTER StatVmxCheckBadRmSelBase;
971 STAMCOUNTER StatVmxCheckBadRmSelLimit;
972 STAMCOUNTER StatVmxCheckRmOk;
973
974 STAMCOUNTER StatVmxCheckBadSel;
975 STAMCOUNTER StatVmxCheckBadRpl;
976 STAMCOUNTER StatVmxCheckBadLdt;
977 STAMCOUNTER StatVmxCheckBadTr;
978 STAMCOUNTER StatVmxCheckPmOk;
979
980#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
981 STAMCOUNTER StatFpu64SwitchBack;
982 STAMCOUNTER StatDebug64SwitchBack;
983#endif
984
985#ifdef VBOX_WITH_STATISTICS
986 R3PTRTYPE(PSTAMCOUNTER) paStatExitReason;
987 R0PTRTYPE(PSTAMCOUNTER) paStatExitReasonR0;
988 R3PTRTYPE(PSTAMCOUNTER) paStatInjectedIrqs;
989 R0PTRTYPE(PSTAMCOUNTER) paStatInjectedIrqsR0;
990#endif
991#ifdef HM_PROFILE_EXIT_DISPATCH
992 STAMPROFILEADV StatExitDispatch;
993#endif
994} HMCPU;
995/** Pointer to HM VMCPU instance data. */
996typedef HMCPU *PHMCPU;
997AssertCompileMemberAlignment(HMCPU, vmx, 8);
998AssertCompileMemberAlignment(HMCPU, svm, 8);
999AssertCompileMemberAlignment(HMCPU, Event, 8);
1000
1001
1002#ifdef IN_RING0
1003VMMR0DECL(PHMGLOBALCPUINFO) HMR0GetCurrentCpu(void);
1004VMMR0DECL(PHMGLOBALCPUINFO) HMR0GetCurrentCpuEx(RTCPUID idCpu);
1005
1006
1007# ifdef VBOX_STRICT
1008VMMR0DECL(void) HMDumpRegs(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
1009VMMR0DECL(void) HMR0DumpDescriptor(PCX86DESCHC pDesc, RTSEL Sel, const char *pszMsg);
1010# else
1011# define HMDumpRegs(a, b ,c) do { } while (0)
1012# define HMR0DumpDescriptor(a, b, c) do { } while (0)
1013# endif /* VBOX_STRICT */
1014
1015# ifdef VBOX_WITH_KERNEL_USING_XMM
1016DECLASM(int) HMR0VMXStartVMWrapXMM(RTHCUINT fResume, PCPUMCTX pCtx, PVMCSCACHE pCache, PVM pVM, PVMCPU pVCpu, PFNHMVMXSTARTVM pfnStartVM);
1017DECLASM(int) HMR0SVMRunWrapXMM(RTHCPHYS pVmcbHostPhys, RTHCPHYS pVmcbPhys, PCPUMCTX pCtx, PVM pVM, PVMCPU pVCpu, PFNHMSVMVMRUN pfnVMRun);
1018# endif
1019
1020# ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
1021/**
1022 * Gets 64-bit GDTR and IDTR on darwin.
1023 * @param pGdtr Where to store the 64-bit GDTR.
1024 * @param pIdtr Where to store the 64-bit IDTR.
1025 */
1026DECLASM(void) HMR0Get64bitGdtrAndIdtr(PX86XDTR64 pGdtr, PX86XDTR64 pIdtr);
1027
1028/**
1029 * Gets 64-bit CR3 on darwin.
1030 * @returns CR3
1031 */
1032DECLASM(uint64_t) HMR0Get64bitCR3(void);
1033# endif /* VBOX_WITH_HYBRID_32BIT_KERNEL */
1034
1035#endif /* IN_RING0 */
1036
1037/** @} */
1038
1039RT_C_DECLS_END
1040
1041#endif
1042
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