VirtualBox

source: vbox/trunk/include/VBox/vmm/vm.h@ 62075

Last change on this file since 62075 was 62075, checked in by vboxsync, 8 years ago

VMCPU.cpum: Added GstCtx member for execution engines like HM and IEM so they can save a register if they care.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 50.9 KB
Line 
1/** @file
2 * VM - The Virtual Machine, data.
3 */
4
5/*
6 * Copyright (C) 2006-2015 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_vmm_vm_h
27#define ___VBox_vmm_vm_h
28
29#ifndef VBOX_FOR_DTRACE_LIB
30# include <iprt/param.h>
31# include <VBox/types.h>
32# include <VBox/vmm/cpum.h>
33# include <VBox/vmm/stam.h>
34# include <VBox/vmm/vmapi.h>
35# include <VBox/vmm/vmm.h>
36# include <VBox/sup.h>
37#else
38# pragma D depends_on library vbox-types.d
39# pragma D depends_on library CPUMInternal.d
40# define ___CPUMInternal_h
41#endif
42
43
44
45/** @defgroup grp_vm The Virtual Machine
46 * @ingroup grp_vmm
47 * @{
48 */
49
50/**
51 * The state of a Virtual CPU.
52 *
53 * The basic state indicated here is whether the CPU has been started or not. In
54 * addition, there are sub-states when started for assisting scheduling (GVMM
55 * mostly).
56 *
57 * The transition out of the STOPPED state is done by a vmR3PowerOn.
58 * The transition back to the STOPPED state is done by vmR3PowerOff.
59 *
60 * (Alternatively we could let vmR3PowerOn start CPU 0 only and let the SPIP
61 * handling switch on the other CPUs. Then vmR3Reset would stop all but CPU 0.)
62 */
63typedef enum VMCPUSTATE
64{
65 /** The customary invalid zero. */
66 VMCPUSTATE_INVALID = 0,
67
68 /** Virtual CPU has not yet been started. */
69 VMCPUSTATE_STOPPED,
70
71 /** CPU started. */
72 VMCPUSTATE_STARTED,
73 /** CPU started in HM context. */
74 VMCPUSTATE_STARTED_HM,
75 /** Executing guest code and can be poked (RC or STI bits of HM). */
76 VMCPUSTATE_STARTED_EXEC,
77 /** Executing guest code in the recompiler. */
78 VMCPUSTATE_STARTED_EXEC_REM,
79 /** Halted. */
80 VMCPUSTATE_STARTED_HALTED,
81
82 /** The end of valid virtual CPU states. */
83 VMCPUSTATE_END,
84
85 /** Ensure 32-bit type. */
86 VMCPUSTATE_32BIT_HACK = 0x7fffffff
87} VMCPUSTATE;
88
89
90/**
91 * The cross context virtual CPU structure.
92 *
93 * Run 'kmk run-struct-tests' (from src/VBox/VMM if you like) after updating!
94 */
95typedef struct VMCPU
96{
97 /** Per CPU forced action.
98 * See the VMCPU_FF_* \#defines. Updated atomically. */
99 uint32_t volatile fLocalForcedActions; /* 0 */
100 /** The CPU state. */
101 VMCPUSTATE volatile enmState; /* 4 */
102
103 /** Pointer to the ring-3 UVMCPU structure. */
104 PUVMCPU pUVCpu; /* 8 */
105 /** Ring-3 Host Context VM Pointer. */
106 PVMR3 pVMR3; /* 16 / 12 */
107 /** Ring-0 Host Context VM Pointer. */
108 PVMR0 pVMR0; /* 24 / 16 */
109 /** Raw-mode Context VM Pointer. */
110 PVMRC pVMRC; /* 32 / 20 */
111 /** The CPU ID.
112 * This is the index into the VM::aCpu array. */
113 VMCPUID idCpu; /* 36 / 24 */
114 /** The native thread handle. */
115 RTNATIVETHREAD hNativeThread; /* 40 / 28 */
116 /** The native R0 thread handle. (different from the R3 handle!) */
117 RTNATIVETHREAD hNativeThreadR0; /* 48 / 32 */
118 /** Which host CPU ID is this EMT running on.
119 * Only valid when in RC or HMR0 with scheduling disabled. */
120 RTCPUID volatile idHostCpu; /* 56 / 36 */
121 /** The CPU set index corresponding to idHostCpu, UINT32_MAX if not valid.
122 * @remarks Best to make sure iHostCpuSet shares cache line with idHostCpu! */
123 uint32_t volatile iHostCpuSet; /* 60 / 40 */
124
125#if HC_ARCH_BITS == 32
126 /** Align the structures below bit on a 64-byte boundary and make sure it starts
127 * at the same offset in both 64-bit and 32-bit builds.
128 *
129 * @remarks The alignments of the members that are larger than 48 bytes should be
130 * 64-byte for cache line reasons. structs containing small amounts of
131 * data could be lumped together at the end with a < 64 byte padding
132 * following it (to grow into and align the struct size).
133 */
134 uint8_t abAlignment1[HC_ARCH_BITS == 64 ? 0 : 20];
135#endif
136
137 /** IEM part.
138 * @remarks This comes first as it allows the use of 8-bit immediates for the
139 * first 64 bytes of the structure, reducing code size a wee bit. */
140 union
141 {
142#ifdef ___IEMInternal_h
143 struct IEMCPU s;
144#endif
145 uint8_t padding[18432]; /* multiple of 64 */
146 } iem;
147
148 /** HM part. */
149 union
150 {
151#ifdef ___HMInternal_h
152 struct HMCPU s;
153#endif
154 uint8_t padding[5760]; /* multiple of 64 */
155 } hm;
156
157 /** EM part. */
158 union
159 {
160#ifdef ___EMInternal_h
161 struct EMCPU s;
162#endif
163 uint8_t padding[1408]; /* multiple of 64 */
164 } em;
165
166 /** TRPM part. */
167 union
168 {
169#ifdef ___TRPMInternal_h
170 struct TRPMCPU s;
171#endif
172 uint8_t padding[128]; /* multiple of 64 */
173 } trpm;
174
175 /** TM part. */
176 union
177 {
178#ifdef ___TMInternal_h
179 struct TMCPU s;
180#endif
181 uint8_t padding[384]; /* multiple of 64 */
182 } tm;
183
184 /** VMM part. */
185 union
186 {
187#ifdef ___VMMInternal_h
188 struct VMMCPU s;
189#endif
190 uint8_t padding[704]; /* multiple of 64 */
191 } vmm;
192
193 /** PDM part. */
194 union
195 {
196#ifdef ___PDMInternal_h
197 struct PDMCPU s;
198#endif
199 uint8_t padding[256]; /* multiple of 64 */
200 } pdm;
201
202 /** IOM part. */
203 union
204 {
205#ifdef ___IOMInternal_h
206 struct IOMCPU s;
207#endif
208 uint8_t padding[512]; /* multiple of 64 */
209 } iom;
210
211 /** DBGF part.
212 * @todo Combine this with other tiny structures. */
213 union
214 {
215#ifdef ___DBGFInternal_h
216 struct DBGFCPU s;
217#endif
218 uint8_t padding[256]; /* multiple of 64 */
219 } dbgf;
220
221 /** GIM part. */
222 union
223 {
224#ifdef ___GIMInternal_h
225 struct GIMCPU s;
226#endif
227 uint8_t padding[64]; /* multiple of 64 */
228 } gim;
229
230 /** APIC part. */
231 union
232 {
233#ifdef ___APICInternal_h
234 struct APICCPU s;
235#endif
236 uint8_t padding[768]; /* multiple of 64 */
237 } apic;
238
239 /*
240 * Some less frequently used global members that doesn't need to take up
241 * precious space at the head of the structure.
242 */
243
244 /** Trace groups enable flags. */
245 uint32_t fTraceGroups; /* 64 / 44 */
246 /** State data for use by ad hoc profiling. */
247 uint32_t uAdHoc;
248 /** Profiling samples for use by ad hoc profiling. */
249 STAMPROFILEADV aStatAdHoc[8]; /* size: 40*8 = 320 */
250
251 /** Align the following members on page boundary. */
252 uint8_t abAlignment2[3704];
253
254 /** PGM part. */
255 union
256 {
257#ifdef ___PGMInternal_h
258 struct PGMCPU s;
259#endif
260 uint8_t padding[4096]; /* multiple of 4096 */
261 } pgm;
262
263 /** CPUM part. */
264 union
265 {
266#ifdef ___CPUMInternal_h
267 struct CPUMCPU s;
268#endif
269#ifdef VMCPU_INCL_CPUM_GST_CTX
270 /** The guest CPUM context for direct use by execution engines.
271 * This is not for general consumption, but for HM, REM, IEM, and maybe a few
272 * others. The rest will use the function based CPUM API. */
273 CPUMCTX GstCtx;
274#endif
275 uint8_t padding[4096]; /* multiple of 4096 */
276 } cpum;
277} VMCPU;
278
279
280#ifndef VBOX_FOR_DTRACE_LIB
281
282/** @name Operations on VMCPU::enmState
283 * @{ */
284/** Gets the VMCPU state. */
285#define VMCPU_GET_STATE(pVCpu) ( (pVCpu)->enmState )
286/** Sets the VMCPU state. */
287#define VMCPU_SET_STATE(pVCpu, enmNewState) \
288 ASMAtomicWriteU32((uint32_t volatile *)&(pVCpu)->enmState, (enmNewState))
289/** Cmpares and sets the VMCPU state. */
290#define VMCPU_CMPXCHG_STATE(pVCpu, enmNewState, enmOldState) \
291 ASMAtomicCmpXchgU32((uint32_t volatile *)&(pVCpu)->enmState, (enmNewState), (enmOldState))
292/** Checks the VMCPU state. */
293#ifdef VBOX_STRICT
294# define VMCPU_ASSERT_STATE(pVCpu, enmExpectedState) \
295 do { \
296 VMCPUSTATE enmState = VMCPU_GET_STATE(pVCpu); \
297 AssertMsg(enmState == (enmExpectedState), \
298 ("enmState=%d enmExpectedState=%d idCpu=%u\n", \
299 enmState, enmExpectedState, (pVCpu)->idCpu)); \
300 } while (0)
301#else
302# define VMCPU_ASSERT_STATE(pVCpu, enmExpectedState) do { } while (0)
303#endif
304/** Tests if the state means that the CPU is started. */
305#define VMCPUSTATE_IS_STARTED(enmState) ( (enmState) > VMCPUSTATE_STOPPED )
306/** Tests if the state means that the CPU is stopped. */
307#define VMCPUSTATE_IS_STOPPED(enmState) ( (enmState) == VMCPUSTATE_STOPPED )
308/** @} */
309
310
311/** The name of the raw-mode context VMM Core module. */
312#define VMMRC_MAIN_MODULE_NAME "VMMRC.rc"
313/** The name of the ring-0 context VMM Core module. */
314#define VMMR0_MAIN_MODULE_NAME "VMMR0.r0"
315
316/**
317 * Wrapper macro for avoiding too much \#ifdef VBOX_WITH_RAW_MODE.
318 */
319#ifdef VBOX_WITH_RAW_MODE
320# define VM_WHEN_RAW_MODE(a_WithExpr, a_WithoutExpr) a_WithExpr
321#else
322# define VM_WHEN_RAW_MODE(a_WithExpr, a_WithoutExpr) a_WithoutExpr
323#endif
324
325
326/** VM Forced Action Flags.
327 *
328 * Use the VM_FF_SET() and VM_FF_CLEAR() macros to change the force
329 * action mask of a VM.
330 *
331 * Available VM bits:
332 * 0, 1, 5, 6, 7, 13, 14, 15, 16, 17, 21, 22, 23, 24, 25, 26, 27, 28, 30
333 *
334 *
335 * Available VMCPU bits:
336 * 11, 14, 15, 31
337 *
338 * @todo If we run low on VMCPU, we may consider merging the SELM bits
339 *
340 * @{
341 */
342/** The virtual sync clock has been stopped, go to TM until it has been
343 * restarted... */
344#define VM_FF_TM_VIRTUAL_SYNC RT_BIT_32(2)
345/** PDM Queues are pending. */
346#define VM_FF_PDM_QUEUES RT_BIT_32(VM_FF_PDM_QUEUES_BIT)
347/** The bit number for VM_FF_PDM_QUEUES. */
348#define VM_FF_PDM_QUEUES_BIT 3
349/** PDM DMA transfers are pending. */
350#define VM_FF_PDM_DMA RT_BIT_32(VM_FF_PDM_DMA_BIT)
351/** The bit number for VM_FF_PDM_DMA. */
352#define VM_FF_PDM_DMA_BIT 4
353/** This action forces the VM to call DBGF so DBGF can service debugger
354 * requests in the emulation thread.
355 * This action flag stays asserted till DBGF clears it.*/
356#define VM_FF_DBGF RT_BIT_32(VM_FF_DBGF_BIT)
357/** The bit number for VM_FF_DBGF. */
358#define VM_FF_DBGF_BIT 8
359/** This action forces the VM to service pending requests from other
360 * thread or requests which must be executed in another context. */
361#define VM_FF_REQUEST RT_BIT_32(9)
362/** Check for VM state changes and take appropriate action. */
363#define VM_FF_CHECK_VM_STATE RT_BIT_32(VM_FF_CHECK_VM_STATE_BIT)
364/** The bit number for VM_FF_CHECK_VM_STATE. */
365#define VM_FF_CHECK_VM_STATE_BIT 10
366/** Reset the VM. (postponed) */
367#define VM_FF_RESET RT_BIT_32(VM_FF_RESET_BIT)
368/** The bit number for VM_FF_RESET. */
369#define VM_FF_RESET_BIT 11
370/** EMT rendezvous in VMM. */
371#define VM_FF_EMT_RENDEZVOUS RT_BIT_32(VM_FF_EMT_RENDEZVOUS_BIT)
372/** The bit number for VM_FF_EMT_RENDEZVOUS. */
373#define VM_FF_EMT_RENDEZVOUS_BIT 12
374
375/** PGM needs to allocate handy pages. */
376#define VM_FF_PGM_NEED_HANDY_PAGES RT_BIT_32(18)
377/** PGM is out of memory.
378 * Abandon all loops and code paths which can be resumed and get up to the EM
379 * loops. */
380#define VM_FF_PGM_NO_MEMORY RT_BIT_32(19)
381 /** PGM is about to perform a lightweight pool flush
382 * Guest SMP: all EMT threads should return to ring 3
383 */
384#define VM_FF_PGM_POOL_FLUSH_PENDING RT_BIT_32(20)
385/** REM needs to be informed about handler changes. */
386#define VM_FF_REM_HANDLER_NOTIFY RT_BIT_32(VM_FF_REM_HANDLER_NOTIFY_BIT)
387/** The bit number for VM_FF_REM_HANDLER_NOTIFY. */
388#define VM_FF_REM_HANDLER_NOTIFY_BIT 29
389/** Suspend the VM - debug only. */
390#define VM_FF_DEBUG_SUSPEND RT_BIT_32(31)
391
392
393/** This action forces the VM to check any pending interrupts on the APIC. */
394#define VMCPU_FF_INTERRUPT_APIC RT_BIT_32(0)
395/** This action forces the VM to check any pending interrups on the PIC. */
396#define VMCPU_FF_INTERRUPT_PIC RT_BIT_32(1)
397/** This action forces the VM to schedule and run pending timer (TM).
398 * @remarks Don't move - PATM compatibility. */
399#define VMCPU_FF_TIMER RT_BIT_32(2)
400/** This action forces the VM to check any pending NMIs. */
401#define VMCPU_FF_INTERRUPT_NMI_BIT 3
402#define VMCPU_FF_INTERRUPT_NMI RT_BIT_32(VMCPU_FF_INTERRUPT_NMI_BIT)
403/** This action forces the VM to check any pending SMIs. */
404#define VMCPU_FF_INTERRUPT_SMI_BIT 4
405#define VMCPU_FF_INTERRUPT_SMI RT_BIT_32(VMCPU_FF_INTERRUPT_SMI_BIT)
406/** PDM critical section unlocking is pending, process promptly upon return to R3. */
407#define VMCPU_FF_PDM_CRITSECT RT_BIT_32(5)
408/** This action forces the VCPU out of the halted state. */
409#define VMCPU_FF_UNHALT RT_BIT_32(6)
410/** Pending IEM action (bit number). */
411#define VMCPU_FF_IEM_BIT 7
412/** Pending IEM action (mask). */
413#define VMCPU_FF_IEM RT_BIT_32(VMCPU_FF_IEM_BIT)
414/** Pending APIC action (bit number). */
415#define VMCPU_FF_UPDATE_APIC_BIT 8
416/** This action forces the VM to update APIC's asynchronously arrived
417 * interrupts as pending interrupts. */
418#define VMCPU_FF_UPDATE_APIC RT_BIT_32(VMCPU_FF_UPDATE_APIC_BIT)
419/** This action forces the VM to service pending requests from other
420 * thread or requests which must be executed in another context. */
421#define VMCPU_FF_REQUEST RT_BIT_32(9)
422/** Pending DBGF event (alternative to passing VINF_EM_DBG_EVENT around). */
423#define VMCPU_FF_DBGF RT_BIT_32(VMCPU_FF_DBGF_BIT)
424/** The bit number for VMCPU_FF_DBGF. */
425#define VMCPU_FF_DBGF_BIT 10
426/** This action forces the VM to service any pending updates to CR3 (used only
427 * by HM). */
428#define VMCPU_FF_HM_UPDATE_CR3 RT_BIT_32(12)
429/** This action forces the VM to service any pending updates to PAE PDPEs (used
430 * only by HM). */
431#define VMCPU_FF_HM_UPDATE_PAE_PDPES RT_BIT_32(13)
432/** This action forces the VM to resync the page tables before going
433 * back to execute guest code. (GLOBAL FLUSH) */
434#define VMCPU_FF_PGM_SYNC_CR3 RT_BIT_32(16)
435/** Same as VM_FF_PGM_SYNC_CR3 except that global pages can be skipped.
436 * (NON-GLOBAL FLUSH) */
437#define VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL RT_BIT_32(17)
438/** Check for pending TLB shootdown actions (deprecated)
439 * Reserved for furture HM re-use if necessary / safe.
440 * Consumer: HM */
441#define VMCPU_FF_TLB_SHOOTDOWN_UNUSED RT_BIT_32(18)
442/** Check for pending TLB flush action.
443 * Consumer: HM
444 * @todo rename to VMCPU_FF_HM_TLB_FLUSH */
445#define VMCPU_FF_TLB_FLUSH RT_BIT_32(VMCPU_FF_TLB_FLUSH_BIT)
446/** The bit number for VMCPU_FF_TLB_FLUSH. */
447#define VMCPU_FF_TLB_FLUSH_BIT 19
448#ifdef VBOX_WITH_RAW_MODE
449/** Check the interrupt and trap gates */
450# define VMCPU_FF_TRPM_SYNC_IDT RT_BIT_32(20)
451/** Check Guest's TSS ring 0 stack */
452# define VMCPU_FF_SELM_SYNC_TSS RT_BIT_32(21)
453/** Check Guest's GDT table */
454# define VMCPU_FF_SELM_SYNC_GDT RT_BIT_32(22)
455/** Check Guest's LDT table */
456# define VMCPU_FF_SELM_SYNC_LDT RT_BIT_32(23)
457#endif /* VBOX_WITH_RAW_MODE */
458/** Inhibit interrupts pending. See EMGetInhibitInterruptsPC(). */
459#define VMCPU_FF_INHIBIT_INTERRUPTS RT_BIT_32(24)
460/** Block injection of non-maskable interrupts to the guest. */
461#define VMCPU_FF_BLOCK_NMIS RT_BIT_32(25)
462#ifdef VBOX_WITH_RAW_MODE
463/** CSAM needs to scan the page that's being executed */
464# define VMCPU_FF_CSAM_SCAN_PAGE RT_BIT_32(26)
465/** CSAM needs to do some homework. */
466# define VMCPU_FF_CSAM_PENDING_ACTION RT_BIT_32(27)
467#endif /* VBOX_WITH_RAW_MODE */
468/** Force return to Ring-3. */
469#define VMCPU_FF_TO_R3 RT_BIT_32(28)
470/** Force return to ring-3 to service pending I/O or MMIO write.
471 * This is a backup for mechanism VINF_IOM_R3_IOPORT_COMMIT_WRITE and
472 * VINF_IOM_R3_MMIO_COMMIT_WRITE, allowing VINF_EM_DBG_BREAKPOINT and similar
473 * status codes to be propagated at the same time without loss. */
474#define VMCPU_FF_IOM RT_BIT_32(29)
475#ifdef VBOX_WITH_RAW_MODE
476/** CPUM need to adjust CR0.TS/EM before executing raw-mode code again. */
477# define VMCPU_FF_CPUM RT_BIT_32(VMCPU_FF_CPUM_BIT)
478/** The bit number for VMCPU_FF_CPUM. */
479# define VMCPU_FF_CPUM_BIT 30
480#endif /* VBOX_WITH_RAW_MODE */
481
482/** Externally VM forced actions. Used to quit the idle/wait loop. */
483#define VM_FF_EXTERNAL_SUSPENDED_MASK ( VM_FF_CHECK_VM_STATE | VM_FF_DBGF | VM_FF_REQUEST | VM_FF_EMT_RENDEZVOUS )
484/** Externally VMCPU forced actions. Used to quit the idle/wait loop. */
485#define VMCPU_FF_EXTERNAL_SUSPENDED_MASK ( VMCPU_FF_REQUEST | VMCPU_FF_DBGF )
486
487/** Externally forced VM actions. Used to quit the idle/wait loop. */
488#define VM_FF_EXTERNAL_HALTED_MASK ( VM_FF_CHECK_VM_STATE | VM_FF_DBGF | VM_FF_REQUEST \
489 | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA | VM_FF_EMT_RENDEZVOUS )
490/** Externally forced VMCPU actions. Used to quit the idle/wait loop. */
491#define VMCPU_FF_EXTERNAL_HALTED_MASK ( VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC \
492 | VMCPU_FF_REQUEST | VMCPU_FF_INTERRUPT_NMI | VMCPU_FF_INTERRUPT_SMI \
493 | VMCPU_FF_UNHALT | VMCPU_FF_TIMER | VMCPU_FF_DBGF )
494
495/** High priority VM pre-execution actions. */
496#define VM_FF_HIGH_PRIORITY_PRE_MASK ( VM_FF_CHECK_VM_STATE | VM_FF_DBGF | VM_FF_TM_VIRTUAL_SYNC \
497 | VM_FF_DEBUG_SUSPEND | VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY \
498 | VM_FF_EMT_RENDEZVOUS )
499/** High priority VMCPU pre-execution actions. */
500#define VMCPU_FF_HIGH_PRIORITY_PRE_MASK ( VMCPU_FF_TIMER | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC \
501 | VMCPU_FF_UPDATE_APIC | VMCPU_FF_INHIBIT_INTERRUPTS | VMCPU_FF_DBGF \
502 | VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL \
503 | VM_WHEN_RAW_MODE( VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_TRPM_SYNC_IDT \
504 | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT, 0 ) )
505
506/** High priority VM pre raw-mode execution mask. */
507#define VM_FF_HIGH_PRIORITY_PRE_RAW_MASK ( VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY )
508/** High priority VMCPU pre raw-mode execution mask. */
509#define VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK ( VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL \
510 | VMCPU_FF_INHIBIT_INTERRUPTS \
511 | VM_WHEN_RAW_MODE( VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_TRPM_SYNC_IDT \
512 | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT, 0) )
513
514/** High priority post-execution actions. */
515#define VM_FF_HIGH_PRIORITY_POST_MASK ( VM_FF_PGM_NO_MEMORY )
516/** High priority post-execution actions. */
517#define VMCPU_FF_HIGH_PRIORITY_POST_MASK ( VMCPU_FF_PDM_CRITSECT | VM_WHEN_RAW_MODE(VMCPU_FF_CSAM_PENDING_ACTION, 0) \
518 | VMCPU_FF_HM_UPDATE_CR3 | VMCPU_FF_HM_UPDATE_PAE_PDPES \
519 | VMCPU_FF_IEM | VMCPU_FF_IOM )
520
521/** Normal priority VM post-execution actions. */
522#define VM_FF_NORMAL_PRIORITY_POST_MASK ( VM_FF_CHECK_VM_STATE | VM_FF_DBGF | VM_FF_RESET \
523 | VM_FF_PGM_NO_MEMORY | VM_FF_EMT_RENDEZVOUS)
524/** Normal priority VMCPU post-execution actions. */
525#define VMCPU_FF_NORMAL_PRIORITY_POST_MASK ( VM_WHEN_RAW_MODE(VMCPU_FF_CSAM_SCAN_PAGE, 0) | VMCPU_FF_DBGF )
526
527/** Normal priority VM actions. */
528#define VM_FF_NORMAL_PRIORITY_MASK ( VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA \
529 | VM_FF_REM_HANDLER_NOTIFY | VM_FF_EMT_RENDEZVOUS)
530/** Normal priority VMCPU actions. */
531#define VMCPU_FF_NORMAL_PRIORITY_MASK ( VMCPU_FF_REQUEST | VMCPU_FF_UNHALT )
532
533/** Flags to clear before resuming guest execution. */
534#define VMCPU_FF_RESUME_GUEST_MASK ( VMCPU_FF_TO_R3 )
535
536
537/** VM flags that cause the REP[|NE|E] STRINS loops to yield immediately. */
538#define VM_FF_HIGH_PRIORITY_POST_REPSTR_MASK ( VM_FF_TM_VIRTUAL_SYNC | VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY \
539 | VM_FF_EMT_RENDEZVOUS | VM_FF_PGM_POOL_FLUSH_PENDING | VM_FF_RESET)
540/** VM flags that cause the REP[|NE|E] STRINS loops to yield. */
541#define VM_FF_YIELD_REPSTR_MASK ( VM_FF_HIGH_PRIORITY_POST_REPSTR_MASK \
542 | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA | VM_FF_DBGF | VM_FF_DEBUG_SUSPEND )
543/** VMCPU flags that cause the REP[|NE|E] STRINS loops to yield immediately. */
544#ifdef IN_RING3
545# define VMCPU_FF_HIGH_PRIORITY_POST_REPSTR_MASK ( VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_DBGF )
546#else
547# define VMCPU_FF_HIGH_PRIORITY_POST_REPSTR_MASK ( VMCPU_FF_TO_R3 | VMCPU_FF_IEM | VMCPU_FF_IOM | VMCPU_FF_PGM_SYNC_CR3 \
548 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_DBGF )
549#endif
550/** VMCPU flags that cause the REP[|NE|E] STRINS loops to yield, interrupts
551 * enabled. */
552#define VMCPU_FF_YIELD_REPSTR_MASK ( VMCPU_FF_HIGH_PRIORITY_POST_REPSTR_MASK \
553 | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_PIC \
554 | VMCPU_FF_INTERRUPT_NMI | VMCPU_FF_INTERRUPT_SMI | VMCPU_FF_PDM_CRITSECT \
555 | VMCPU_FF_TIMER | VMCPU_FF_REQUEST )
556/** VMCPU flags that cause the REP[|NE|E] STRINS loops to yield, interrupts
557 * disabled. */
558#define VMCPU_FF_YIELD_REPSTR_NOINT_MASK ( VMCPU_FF_YIELD_REPSTR_MASK \
559 & ~(VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_PIC) )
560
561/** VM Flags that cause the HM loops to go back to ring-3. */
562#define VM_FF_HM_TO_R3_MASK ( VM_FF_TM_VIRTUAL_SYNC | VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY \
563 | VM_FF_PDM_QUEUES | VM_FF_EMT_RENDEZVOUS)
564/** VMCPU Flags that cause the HM loops to go back to ring-3. */
565#define VMCPU_FF_HM_TO_R3_MASK ( VMCPU_FF_TO_R3 | VMCPU_FF_TIMER | VMCPU_FF_PDM_CRITSECT \
566 | VMCPU_FF_IEM | VMCPU_FF_IOM)
567
568/** High priority ring-0 VM pre HM-mode execution mask. */
569#define VM_FF_HP_R0_PRE_HM_MASK (VM_FF_HM_TO_R3_MASK | VM_FF_REQUEST | VM_FF_PGM_POOL_FLUSH_PENDING | VM_FF_PDM_DMA)
570/** High priority ring-0 VMCPU pre HM-mode execution mask. */
571#define VMCPU_FF_HP_R0_PRE_HM_MASK ( VMCPU_FF_HM_TO_R3_MASK | VMCPU_FF_PGM_SYNC_CR3 \
572 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_REQUEST)
573/** High priority ring-0 VM pre HM-mode execution mask, single stepping. */
574#define VM_FF_HP_R0_PRE_HM_STEP_MASK (VM_FF_HP_R0_PRE_HM_MASK & ~( VM_FF_TM_VIRTUAL_SYNC | VM_FF_PDM_QUEUES \
575 | VM_FF_EMT_RENDEZVOUS | VM_FF_REQUEST \
576 | VM_FF_PDM_DMA) )
577/** High priority ring-0 VMCPU pre HM-mode execution mask, single stepping. */
578#define VMCPU_FF_HP_R0_PRE_HM_STEP_MASK (VMCPU_FF_HP_R0_PRE_HM_MASK & ~( VMCPU_FF_TO_R3 | VMCPU_FF_TIMER \
579 | VMCPU_FF_PDM_CRITSECT | VMCPU_FF_REQUEST) )
580
581/** All the forced VM flags. */
582#define VM_FF_ALL_MASK (~0U)
583/** All the forced VMCPU flags. */
584#define VMCPU_FF_ALL_MASK (~0U)
585
586/** All the forced VM flags except those related to raw-mode and hardware
587 * assisted execution. */
588#define VM_FF_ALL_REM_MASK (~(VM_FF_HIGH_PRIORITY_PRE_RAW_MASK) | VM_FF_PGM_NO_MEMORY)
589/** All the forced VMCPU flags except those related to raw-mode and hardware
590 * assisted execution. */
591#define VMCPU_FF_ALL_REM_MASK (~( VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK | VMCPU_FF_PDM_CRITSECT \
592 | VMCPU_FF_TLB_FLUSH | VM_WHEN_RAW_MODE(VMCPU_FF_CSAM_PENDING_ACTION, 0) ))
593/** @} */
594
595/** @def VM_FF_SET
596 * Sets a force action flag.
597 *
598 * @param pVM The cross context VM structure.
599 * @param fFlag The flag to set.
600 */
601#if 1
602# define VM_FF_SET(pVM, fFlag) ASMAtomicOrU32(&(pVM)->fGlobalForcedActions, (fFlag))
603#else
604# define VM_FF_SET(pVM, fFlag) \
605 do { ASMAtomicOrU32(&(pVM)->fGlobalForcedActions, (fFlag)); \
606 RTLogPrintf("VM_FF_SET : %08x %s - %s(%d) %s\n", (pVM)->fGlobalForcedActions, #fFlag, __FILE__, __LINE__, __FUNCTION__); \
607 } while (0)
608#endif
609
610/** @def VMCPU_FF_SET
611 * Sets a force action flag for the given VCPU.
612 *
613 * @param pVCpu The cross context virtual CPU structure.
614 * @param fFlag The flag to set.
615 */
616#define VMCPU_FF_SET(pVCpu, fFlag) ASMAtomicOrU32(&(pVCpu)->fLocalForcedActions, (fFlag))
617
618/** @def VM_FF_CLEAR
619 * Clears a force action flag.
620 *
621 * @param pVM The cross context VM structure.
622 * @param fFlag The flag to clear.
623 */
624#if 1
625# define VM_FF_CLEAR(pVM, fFlag) ASMAtomicAndU32(&(pVM)->fGlobalForcedActions, ~(fFlag))
626#else
627# define VM_FF_CLEAR(pVM, fFlag) \
628 do { ASMAtomicAndU32(&(pVM)->fGlobalForcedActions, ~(fFlag)); \
629 RTLogPrintf("VM_FF_CLEAR: %08x %s - %s(%d) %s\n", (pVM)->fGlobalForcedActions, #fFlag, __FILE__, __LINE__, __FUNCTION__); \
630 } while (0)
631#endif
632
633/** @def VMCPU_FF_CLEAR
634 * Clears a force action flag for the given VCPU.
635 *
636 * @param pVCpu The cross context virtual CPU structure.
637 * @param fFlag The flag to clear.
638 */
639#define VMCPU_FF_CLEAR(pVCpu, fFlag) ASMAtomicAndU32(&(pVCpu)->fLocalForcedActions, ~(fFlag))
640
641/** @def VM_FF_IS_SET
642 * Checks if a force action flag is set.
643 *
644 * @param pVM The cross context VM structure.
645 * @param fFlag The flag to check.
646 */
647#define VM_FF_IS_SET(pVM, fFlag) (((pVM)->fGlobalForcedActions & (fFlag)) == (fFlag))
648
649/** @def VMCPU_FF_IS_SET
650 * Checks if a force action flag is set for the given VCPU.
651 *
652 * @param pVCpu The cross context virtual CPU structure.
653 * @param fFlag The flag to check.
654 */
655#define VMCPU_FF_IS_SET(pVCpu, fFlag) (((pVCpu)->fLocalForcedActions & (fFlag)) == (fFlag))
656
657/** @def VM_FF_IS_PENDING
658 * Checks if one or more force action in the specified set is pending.
659 *
660 * @param pVM The cross context VM structure.
661 * @param fFlags The flags to check for.
662 */
663#define VM_FF_IS_PENDING(pVM, fFlags) RT_BOOL((pVM)->fGlobalForcedActions & (fFlags))
664
665/** @def VM_FF_TEST_AND_CLEAR
666 * Checks if one (!) force action in the specified set is pending and clears it atomically
667 *
668 * @returns true if the bit was set.
669 * @returns false if the bit was clear.
670 * @param pVM The cross context VM structure.
671 * @param iBit Bit position to check and clear
672 */
673#define VM_FF_TEST_AND_CLEAR(pVM, iBit) (ASMAtomicBitTestAndClear(&(pVM)->fGlobalForcedActions, iBit##_BIT))
674
675/** @def VMCPU_FF_TEST_AND_CLEAR
676 * Checks if one (!) force action in the specified set is pending and clears it atomically
677 *
678 * @returns true if the bit was set.
679 * @returns false if the bit was clear.
680 * @param pVCpu The cross context virtual CPU structure.
681 * @param iBit Bit position to check and clear
682 */
683#define VMCPU_FF_TEST_AND_CLEAR(pVCpu, iBit) (ASMAtomicBitTestAndClear(&(pVCpu)->fLocalForcedActions, iBit##_BIT))
684
685/** @def VMCPU_FF_IS_PENDING
686 * Checks if one or more force action in the specified set is pending for the given VCPU.
687 *
688 * @param pVCpu The cross context virtual CPU structure.
689 * @param fFlags The flags to check for.
690 */
691#define VMCPU_FF_IS_PENDING(pVCpu, fFlags) RT_BOOL((pVCpu)->fLocalForcedActions & (fFlags))
692
693/** @def VM_FF_IS_PENDING_EXCEPT
694 * Checks if one or more force action in the specified set is pending while one
695 * or more other ones are not.
696 *
697 * @param pVM The cross context VM structure.
698 * @param fFlags The flags to check for.
699 * @param fExcpt The flags that should not be set.
700 */
701#define VM_FF_IS_PENDING_EXCEPT(pVM, fFlags, fExcpt) ( ((pVM)->fGlobalForcedActions & (fFlags)) && !((pVM)->fGlobalForcedActions & (fExcpt)) )
702
703/** @def VMCPU_FF_IS_PENDING_EXCEPT
704 * Checks if one or more force action in the specified set is pending for the given
705 * VCPU while one or more other ones are not.
706 *
707 * @param pVCpu The cross context virtual CPU structure.
708 * @param fFlags The flags to check for.
709 * @param fExcpt The flags that should not be set.
710 */
711#define VMCPU_FF_IS_PENDING_EXCEPT(pVCpu, fFlags, fExcpt) ( ((pVCpu)->fLocalForcedActions & (fFlags)) && !((pVCpu)->fLocalForcedActions & (fExcpt)) )
712
713/** @def VM_IS_EMT
714 * Checks if the current thread is the emulation thread (EMT).
715 *
716 * @remark The ring-0 variation will need attention if we expand the ring-0
717 * code to let threads other than EMT mess around with the VM.
718 */
719#ifdef IN_RC
720# define VM_IS_EMT(pVM) true
721#else
722# define VM_IS_EMT(pVM) (VMMGetCpu(pVM) != NULL)
723#endif
724
725/** @def VMCPU_IS_EMT
726 * Checks if the current thread is the emulation thread (EMT) for the specified
727 * virtual CPU.
728 */
729#ifdef IN_RC
730# define VMCPU_IS_EMT(pVCpu) true
731#else
732# define VMCPU_IS_EMT(pVCpu) ((pVCpu) && ((pVCpu) == VMMGetCpu((pVCpu)->CTX_SUFF(pVM))))
733#endif
734
735/** @def VM_ASSERT_EMT
736 * Asserts that the current thread IS the emulation thread (EMT).
737 */
738#ifdef IN_RC
739# define VM_ASSERT_EMT(pVM) Assert(VM_IS_EMT(pVM))
740#elif defined(IN_RING0)
741# define VM_ASSERT_EMT(pVM) Assert(VM_IS_EMT(pVM))
742#else
743# define VM_ASSERT_EMT(pVM) \
744 AssertMsg(VM_IS_EMT(pVM), \
745 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd\n", RTThreadNativeSelf(), VMR3GetVMCPUNativeThread(pVM)))
746#endif
747
748/** @def VMCPU_ASSERT_EMT
749 * Asserts that the current thread IS the emulation thread (EMT) of the
750 * specified virtual CPU.
751 */
752#ifdef IN_RC
753# define VMCPU_ASSERT_EMT(pVCpu) Assert(VMCPU_IS_EMT(pVCpu))
754#elif defined(IN_RING0)
755# define VMCPU_ASSERT_EMT(pVCpu) AssertMsg(VMCPU_IS_EMT(pVCpu), \
756 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%u\n", \
757 RTThreadNativeSelf(), (pVCpu) ? (pVCpu)->hNativeThreadR0 : 0, \
758 (pVCpu) ? (pVCpu)->idCpu : 0))
759#else
760# define VMCPU_ASSERT_EMT(pVCpu) AssertMsg(VMCPU_IS_EMT(pVCpu), \
761 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%#x\n", \
762 RTThreadNativeSelf(), (pVCpu)->hNativeThread, (pVCpu)->idCpu))
763#endif
764
765/** @def VM_ASSERT_EMT_RETURN
766 * Asserts that the current thread IS the emulation thread (EMT) and returns if it isn't.
767 */
768#ifdef IN_RC
769# define VM_ASSERT_EMT_RETURN(pVM, rc) AssertReturn(VM_IS_EMT(pVM), (rc))
770#elif defined(IN_RING0)
771# define VM_ASSERT_EMT_RETURN(pVM, rc) AssertReturn(VM_IS_EMT(pVM), (rc))
772#else
773# define VM_ASSERT_EMT_RETURN(pVM, rc) \
774 AssertMsgReturn(VM_IS_EMT(pVM), \
775 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd\n", RTThreadNativeSelf(), VMR3GetVMCPUNativeThread(pVM)), \
776 (rc))
777#endif
778
779/** @def VMCPU_ASSERT_EMT_RETURN
780 * Asserts that the current thread IS the emulation thread (EMT) and returns if it isn't.
781 */
782#ifdef IN_RC
783# define VMCPU_ASSERT_EMT_RETURN(pVCpu, rc) AssertReturn(VMCPU_IS_EMT(pVCpu), (rc))
784#elif defined(IN_RING0)
785# define VMCPU_ASSERT_EMT_RETURN(pVCpu, rc) AssertReturn(VMCPU_IS_EMT(pVCpu), (rc))
786#else
787# define VMCPU_ASSERT_EMT_RETURN(pVCpu, rc) \
788 AssertMsgReturn(VMCPU_IS_EMT(pVCpu), \
789 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%#x\n", \
790 RTThreadNativeSelf(), (pVCpu)->hNativeThread, (pVCpu)->idCpu), \
791 (rc))
792#endif
793
794/** @def VMCPU_ASSERT_EMT_OR_GURU
795 * Asserts that the current thread IS the emulation thread (EMT) of the
796 * specified virtual CPU.
797 */
798#if defined(IN_RC) || defined(IN_RING0)
799# define VMCPU_ASSERT_EMT_OR_GURU(pVCpu) Assert( VMCPU_IS_EMT(pVCpu) \
800 || pVCpu->CTX_SUFF(pVM)->enmVMState == VMSTATE_GURU_MEDITATION \
801 || pVCpu->CTX_SUFF(pVM)->enmVMState == VMSTATE_GURU_MEDITATION_LS )
802#else
803# define VMCPU_ASSERT_EMT_OR_GURU(pVCpu) \
804 AssertMsg( VMCPU_IS_EMT(pVCpu) \
805 || pVCpu->CTX_SUFF(pVM)->enmVMState == VMSTATE_GURU_MEDITATION \
806 || pVCpu->CTX_SUFF(pVM)->enmVMState == VMSTATE_GURU_MEDITATION_LS, \
807 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%#x\n", \
808 RTThreadNativeSelf(), (pVCpu)->hNativeThread, (pVCpu)->idCpu))
809#endif
810
811/** @def VMCPU_ASSERT_EMT_OR_NOT_RUNNING
812 * Asserts that the current thread IS the emulation thread (EMT) of the
813 * specified virtual CPU or the VM is not running.
814 */
815#if defined(IN_RC) || defined(IN_RING0)
816# define VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu) \
817 Assert( VMCPU_IS_EMT(pVCpu) \
818 || !VM_IS_RUNNING((pVCpu)->CTX_SUFF(pVM)) )
819#else
820# define VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu) \
821 AssertMsg( VMCPU_IS_EMT(pVCpu) \
822 || !VM_IS_RUNNING((pVCpu)->CTX_SUFF(pVM)), \
823 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%#x\n", \
824 RTThreadNativeSelf(), (pVCpu)->hNativeThread, (pVCpu)->idCpu))
825#endif
826
827/** @def VM_IS_RUNNING
828 * Checks if the the VM is running.
829 */
830#define VM_IS_RUNNING(pVM) ( (pVM)->enmVMState == VMSTATE_RUNNING \
831 || (pVM)->enmVMState == VMSTATE_RUNNING_LS \
832 || (pVM)->enmVMState == VMSTATE_RUNNING_FT)
833
834/** @def VM_ASSERT_IS_NOT_RUNNING
835 * Asserts that the VM is not running.
836 */
837#if defined(IN_RC) || defined(IN_RING0)
838#define VM_ASSERT_IS_NOT_RUNNING(pVM) Assert(!VM_IS_RUNNING(pVM))
839#else
840#define VM_ASSERT_IS_NOT_RUNNING(pVM) AssertMsg(!VM_IS_RUNNING(pVM), ("VM is running. enmVMState=%d\n", \
841 (pVM)->enmVMState))
842#endif
843
844/** @def VM_ASSERT_EMT0
845 * Asserts that the current thread IS emulation thread \#0 (EMT0).
846 */
847#define VM_ASSERT_EMT0(pVM) VMCPU_ASSERT_EMT(&(pVM)->aCpus[0])
848
849/** @def VM_ASSERT_EMT0_RETURN
850 * Asserts that the current thread IS emulation thread \#0 (EMT0) and returns if
851 * it isn't.
852 */
853#define VM_ASSERT_EMT0_RETURN(pVM, rc) VMCPU_ASSERT_EMT_RETURN(&(pVM)->aCpus[0], (rc))
854
855
856/**
857 * Asserts that the current thread is NOT the emulation thread.
858 */
859#define VM_ASSERT_OTHER_THREAD(pVM) \
860 AssertMsg(!VM_IS_EMT(pVM), ("Not other thread!!\n"))
861
862
863/** @def VM_ASSERT_STATE
864 * Asserts a certain VM state.
865 */
866#define VM_ASSERT_STATE(pVM, _enmState) \
867 AssertMsg((pVM)->enmVMState == (_enmState), \
868 ("state %s, expected %s\n", VMGetStateName((pVM)->enmVMState), VMGetStateName(_enmState)))
869
870/** @def VM_ASSERT_STATE_RETURN
871 * Asserts a certain VM state and returns if it doesn't match.
872 */
873#define VM_ASSERT_STATE_RETURN(pVM, _enmState, rc) \
874 AssertMsgReturn((pVM)->enmVMState == (_enmState), \
875 ("state %s, expected %s\n", VMGetStateName((pVM)->enmVMState), VMGetStateName(_enmState)), \
876 (rc))
877
878/** @def VM_IS_VALID_EXT
879 * Asserts a the VM handle is valid for external access, i.e. not being destroy
880 * or terminated. */
881#define VM_IS_VALID_EXT(pVM) \
882 ( RT_VALID_ALIGNED_PTR(pVM, PAGE_SIZE) \
883 && ( (unsigned)(pVM)->enmVMState < (unsigned)VMSTATE_DESTROYING \
884 || ( (unsigned)(pVM)->enmVMState == (unsigned)VMSTATE_DESTROYING \
885 && VM_IS_EMT(pVM))) )
886
887/** @def VM_ASSERT_VALID_EXT_RETURN
888 * Asserts a the VM handle is valid for external access, i.e. not being
889 * destroy or terminated.
890 */
891#define VM_ASSERT_VALID_EXT_RETURN(pVM, rc) \
892 AssertMsgReturn(VM_IS_VALID_EXT(pVM), \
893 ("pVM=%p state %s\n", (pVM), RT_VALID_ALIGNED_PTR(pVM, PAGE_SIZE) \
894 ? VMGetStateName(pVM->enmVMState) : ""), \
895 (rc))
896
897/** @def VMCPU_ASSERT_VALID_EXT_RETURN
898 * Asserts a the VMCPU handle is valid for external access, i.e. not being
899 * destroy or terminated.
900 */
901#define VMCPU_ASSERT_VALID_EXT_RETURN(pVCpu, rc) \
902 AssertMsgReturn( RT_VALID_ALIGNED_PTR(pVCpu, 64) \
903 && RT_VALID_ALIGNED_PTR((pVCpu)->CTX_SUFF(pVM), PAGE_SIZE) \
904 && (unsigned)(pVCpu)->CTX_SUFF(pVM)->enmVMState < (unsigned)VMSTATE_DESTROYING, \
905 ("pVCpu=%p pVM=%p state %s\n", (pVCpu), RT_VALID_ALIGNED_PTR(pVCpu, 64) ? (pVCpu)->CTX_SUFF(pVM) : NULL, \
906 RT_VALID_ALIGNED_PTR(pVCpu, 64) && RT_VALID_ALIGNED_PTR((pVCpu)->CTX_SUFF(pVM), PAGE_SIZE) \
907 ? VMGetStateName((pVCpu)->pVMR3->enmVMState) : ""), \
908 (rc))
909
910#endif /* !VBOX_FOR_DTRACE_LIB */
911
912
913
914/**
915 * The cross context VM structure.
916 *
917 * It contains all the VM data which have to be available in all contexts.
918 * Even if it contains all the data the idea is to use APIs not to modify all
919 * the members all around the place. Therefore we make use of unions to hide
920 * everything which isn't local to the current source module. This means we'll
921 * have to pay a little bit of attention when adding new members to structures
922 * in the unions and make sure to keep the padding sizes up to date.
923 *
924 * Run 'kmk run-struct-tests' (from src/VBox/VMM if you like) after updating!
925 */
926typedef struct VM
927{
928 /** The state of the VM.
929 * This field is read only to everyone except the VM and EM. */
930 VMSTATE volatile enmVMState;
931 /** Forced action flags.
932 * See the VM_FF_* \#defines. Updated atomically.
933 */
934 volatile uint32_t fGlobalForcedActions;
935 /** Pointer to the array of page descriptors for the VM structure allocation. */
936 R3PTRTYPE(PSUPPAGE) paVMPagesR3;
937 /** Session handle. For use when calling SUPR0 APIs. */
938 PSUPDRVSESSION pSession;
939 /** Pointer to the ring-3 VM structure. */
940 PUVM pUVM;
941 /** Ring-3 Host Context VM Pointer. */
942 R3PTRTYPE(struct VM *) pVMR3;
943 /** Ring-0 Host Context VM Pointer. */
944 R0PTRTYPE(struct VM *) pVMR0;
945 /** Raw-mode Context VM Pointer. */
946 RCPTRTYPE(struct VM *) pVMRC;
947
948 /** The GVM VM handle. Only the GVM should modify this field. */
949 uint32_t hSelf;
950 /** Number of virtual CPUs. */
951 uint32_t cCpus;
952 /** CPU excution cap (1-100) */
953 uint32_t uCpuExecutionCap;
954
955 /** Size of the VM structure including the VMCPU array. */
956 uint32_t cbSelf;
957
958 /** Offset to the VMCPU array starting from beginning of this structure. */
959 uint32_t offVMCPU;
960
961 /**
962 * VMMSwitcher assembly entry point returning to host context.
963 *
964 * Depending on how the host handles the rc status given in @a eax, this may
965 * return and let the caller resume whatever it was doing prior to the call.
966 *
967 *
968 * @param eax The return code, register.
969 * @remark Assume interrupts disabled.
970 * @remark This method pointer lives here because TRPM needs it.
971 */
972 RTRCPTR pfnVMMRCToHostAsm/*(int32_t eax)*/;
973
974 /**
975 * VMMSwitcher assembly entry point returning to host context without saving the
976 * raw-mode context (hyper) registers.
977 *
978 * Unlike pfnVMMRC2HCAsm, this will not return to the caller. Instead it
979 * expects the caller to save a RC context in CPUM where one might return if the
980 * return code indicate that this is possible.
981 *
982 * This method pointer lives here because TRPM needs it.
983 *
984 * @param eax The return code, register.
985 * @remark Assume interrupts disabled.
986 * @remark This method pointer lives here because TRPM needs it.
987 */
988 RTRCPTR pfnVMMRCToHostAsmNoReturn/*(int32_t eax)*/;
989
990 /** @name Various items that are frequently accessed.
991 * @{ */
992 /** Whether to recompile user mode code or run it raw/hm. */
993 bool fRecompileUser;
994 /** Whether to recompile supervisor mode code or run it raw/hm. */
995 bool fRecompileSupervisor;
996 /** Whether raw mode supports ring-1 code or not. */
997 bool fRawRing1Enabled;
998 /** PATM enabled flag.
999 * This is placed here for performance reasons. */
1000 bool fPATMEnabled;
1001 /** CSAM enabled flag.
1002 * This is placed here for performance reasons. */
1003 bool fCSAMEnabled;
1004 /** Hardware VM support is available and enabled.
1005 * Determined very early during init.
1006 * This is placed here for performance reasons. */
1007 bool fHMEnabled;
1008 /** For asserting on fHMEnable usage. */
1009 bool fHMEnabledFixed;
1010 /** Hardware VM support requires a minimal raw-mode context.
1011 * This is never set on 64-bit hosts, only 32-bit hosts requires it. */
1012 bool fHMNeedRawModeCtx;
1013 /** Set when this VM is the master FT node.
1014 * @todo This doesn't need to be here, FTM should store it in it's own
1015 * structures instead. */
1016 bool fFaultTolerantMaster;
1017 /** Large page enabled flag.
1018 * @todo This doesn't need to be here, PGM should store it in it's own
1019 * structures instead. */
1020 bool fUseLargePages;
1021 /** @} */
1022
1023 /** Alignment padding. */
1024 uint8_t uPadding1[2];
1025
1026 /** @name Debugging
1027 * @{ */
1028 /** Raw-mode Context VM Pointer. */
1029 RCPTRTYPE(RTTRACEBUF) hTraceBufRC;
1030 /** Ring-3 Host Context VM Pointer. */
1031 R3PTRTYPE(RTTRACEBUF) hTraceBufR3;
1032 /** Ring-0 Host Context VM Pointer. */
1033 R0PTRTYPE(RTTRACEBUF) hTraceBufR0;
1034 /** @} */
1035
1036#if HC_ARCH_BITS == 32
1037 /** Alignment padding. */
1038 uint32_t uPadding2;
1039#endif
1040
1041 /** @name Switcher statistics (remove)
1042 * @{ */
1043 /** Profiling the total time from Qemu to GC. */
1044 STAMPROFILEADV StatTotalQemuToGC;
1045 /** Profiling the total time from GC to Qemu. */
1046 STAMPROFILEADV StatTotalGCToQemu;
1047 /** Profiling the total time spent in GC. */
1048 STAMPROFILEADV StatTotalInGC;
1049 /** Profiling the total time spent not in Qemu. */
1050 STAMPROFILEADV StatTotalInQemu;
1051 /** Profiling the VMMSwitcher code for going to GC. */
1052 STAMPROFILEADV StatSwitcherToGC;
1053 /** Profiling the VMMSwitcher code for going to HC. */
1054 STAMPROFILEADV StatSwitcherToHC;
1055 STAMPROFILEADV StatSwitcherSaveRegs;
1056 STAMPROFILEADV StatSwitcherSysEnter;
1057 STAMPROFILEADV StatSwitcherDebug;
1058 STAMPROFILEADV StatSwitcherCR0;
1059 STAMPROFILEADV StatSwitcherCR4;
1060 STAMPROFILEADV StatSwitcherJmpCR3;
1061 STAMPROFILEADV StatSwitcherRstrRegs;
1062 STAMPROFILEADV StatSwitcherLgdt;
1063 STAMPROFILEADV StatSwitcherLidt;
1064 STAMPROFILEADV StatSwitcherLldt;
1065 STAMPROFILEADV StatSwitcherTSS;
1066 /** @} */
1067
1068 /** Padding - the unions must be aligned on a 64 bytes boundary and the unions
1069 * must start at the same offset on both 64-bit and 32-bit hosts. */
1070 uint8_t abAlignment3[(HC_ARCH_BITS == 32 ? 24 : 0) + 40];
1071
1072 /** CPUM part. */
1073 union
1074 {
1075#ifdef ___CPUMInternal_h
1076 struct CPUM s;
1077#endif
1078#ifdef ___VBox_vmm_cpum_h
1079 /** Read only info exposed about the host and guest CPUs. */
1080 struct
1081 {
1082 /** Padding for hidden fields. */
1083 uint8_t abHidden0[64];
1084 /** Host CPU feature information. */
1085 CPUMFEATURES HostFeatures;
1086 /** Guest CPU feature information. */
1087 CPUMFEATURES GuestFeatures;
1088 } const ro;
1089#endif
1090 uint8_t padding[1536]; /* multiple of 64 */
1091 } cpum;
1092
1093 /** VMM part. */
1094 union
1095 {
1096#ifdef ___VMMInternal_h
1097 struct VMM s;
1098#endif
1099 uint8_t padding[1600]; /* multiple of 64 */
1100 } vmm;
1101
1102 /** PGM part. */
1103 union
1104 {
1105#ifdef ___PGMInternal_h
1106 struct PGM s;
1107#endif
1108 uint8_t padding[4096*2+6080]; /* multiple of 64 */
1109 } pgm;
1110
1111 /** HM part. */
1112 union
1113 {
1114#ifdef ___HMInternal_h
1115 struct HM s;
1116#endif
1117 uint8_t padding[5440]; /* multiple of 64 */
1118 } hm;
1119
1120 /** TRPM part. */
1121 union
1122 {
1123#ifdef ___TRPMInternal_h
1124 struct TRPM s;
1125#endif
1126 uint8_t padding[5248]; /* multiple of 64 */
1127 } trpm;
1128
1129 /** SELM part. */
1130 union
1131 {
1132#ifdef ___SELMInternal_h
1133 struct SELM s;
1134#endif
1135 uint8_t padding[768]; /* multiple of 64 */
1136 } selm;
1137
1138 /** MM part. */
1139 union
1140 {
1141#ifdef ___MMInternal_h
1142 struct MM s;
1143#endif
1144 uint8_t padding[192]; /* multiple of 64 */
1145 } mm;
1146
1147 /** PDM part. */
1148 union
1149 {
1150#ifdef ___PDMInternal_h
1151 struct PDM s;
1152#endif
1153 uint8_t padding[1920]; /* multiple of 64 */
1154 } pdm;
1155
1156 /** IOM part. */
1157 union
1158 {
1159#ifdef ___IOMInternal_h
1160 struct IOM s;
1161#endif
1162 uint8_t padding[896]; /* multiple of 64 */
1163 } iom;
1164
1165 /** EM part. */
1166 union
1167 {
1168#ifdef ___EMInternal_h
1169 struct EM s;
1170#endif
1171 uint8_t padding[256]; /* multiple of 64 */
1172 } em;
1173
1174 /** TM part. */
1175 union
1176 {
1177#ifdef ___TMInternal_h
1178 struct TM s;
1179#endif
1180 uint8_t padding[2496]; /* multiple of 64 */
1181 } tm;
1182
1183 /** DBGF part. */
1184 union
1185 {
1186#ifdef ___DBGFInternal_h
1187 struct DBGF s;
1188#endif
1189#ifdef ___VBox_vmm_dbgf_h
1190 /** Read only info exposed about interrupt breakpoints and selected events. */
1191 struct
1192 {
1193 /** Bitmap of enabled hardware interrupt breakpoints. */
1194 uint32_t bmHardIntBreakpoints[256 / 32];
1195 /** Bitmap of enabled software interrupt breakpoints. */
1196 uint32_t bmSoftIntBreakpoints[256 / 32];
1197 /** Bitmap of selected events.
1198 * This includes non-selectable events too for simplicity, we maintain the
1199 * state for some of these, as it may come in handy. */
1200 uint64_t bmSelectedEvents[(DBGFEVENT_END + 63) / 64];
1201 /** Enabled hardware interrupt breakpoints. */
1202 uint32_t cHardIntBreakpoints;
1203 /** Enabled software interrupt breakpoints. */
1204 uint32_t cSoftIntBreakpoints;
1205 /** Number of selected events. */
1206 uint32_t cSelectedEvents;
1207 } const ro;
1208#endif
1209 uint8_t padding[2368]; /* multiple of 64 */
1210 } dbgf;
1211
1212 /** SSM part. */
1213 union
1214 {
1215#ifdef ___SSMInternal_h
1216 struct SSM s;
1217#endif
1218 uint8_t padding[128]; /* multiple of 64 */
1219 } ssm;
1220
1221 /** FTM part. */
1222 union
1223 {
1224#ifdef ___FTMInternal_h
1225 struct FTM s;
1226#endif
1227 uint8_t padding[512]; /* multiple of 64 */
1228 } ftm;
1229
1230#ifdef VBOX_WITH_RAW_MODE
1231 /** PATM part. */
1232 union
1233 {
1234# ifdef ___PATMInternal_h
1235 struct PATM s;
1236# endif
1237 uint8_t padding[768]; /* multiple of 64 */
1238 } patm;
1239
1240 /** CSAM part. */
1241 union
1242 {
1243# ifdef ___CSAMInternal_h
1244 struct CSAM s;
1245# endif
1246 uint8_t padding[1088]; /* multiple of 64 */
1247 } csam;
1248#endif
1249
1250#ifdef VBOX_WITH_REM
1251 /** REM part. */
1252 union
1253 {
1254# ifdef ___REMInternal_h
1255 struct REM s;
1256# endif
1257 uint8_t padding[0x11100]; /* multiple of 64 */
1258 } rem;
1259#endif
1260
1261 union
1262 {
1263#ifdef ___GIMInternal_h
1264 struct GIM s;
1265#endif
1266 uint8_t padding[448]; /* multiple of 64 */
1267 } gim;
1268
1269 union
1270 {
1271#ifdef ___APICInternal_h
1272 struct APIC s;
1273#endif
1274 uint8_t padding[128]; /* multiple of 8 */
1275 } apic;
1276
1277 /* ---- begin small stuff ---- */
1278
1279 /** VM part. */
1280 union
1281 {
1282#ifdef ___VMInternal_h
1283 struct VMINT s;
1284#endif
1285 uint8_t padding[24]; /* multiple of 8 */
1286 } vm;
1287
1288 /** CFGM part. */
1289 union
1290 {
1291#ifdef ___CFGMInternal_h
1292 struct CFGM s;
1293#endif
1294 uint8_t padding[8]; /* multiple of 8 */
1295 } cfgm;
1296
1297 /** Padding for aligning the cpu array on a page boundary. */
1298#if defined(VBOX_WITH_REM) && defined(VBOX_WITH_RAW_MODE)
1299 uint8_t abAlignment2[3870];
1300#elif defined(VBOX_WITH_REM) && !defined(VBOX_WITH_RAW_MODE)
1301 uint8_t abAlignment2[1630];
1302#elif !defined(VBOX_WITH_REM) && defined(VBOX_WITH_RAW_MODE)
1303 uint8_t abAlignment2[30];
1304#else
1305 uint8_t abAlignment2[1886];
1306#endif
1307
1308 /* ---- end small stuff ---- */
1309
1310 /** VMCPU array for the configured number of virtual CPUs.
1311 * Must be aligned on a page boundary for TLB hit reasons as well as
1312 * alignment of VMCPU members. */
1313 VMCPU aCpus[1];
1314} VM;
1315
1316
1317#ifdef IN_RC
1318RT_C_DECLS_BEGIN
1319
1320/** The VM structure.
1321 * This is imported from the VMMRCBuiltin module, i.e. it's a one of those magic
1322 * globals which we should avoid using.
1323 */
1324extern DECLIMPORT(VM) g_VM;
1325
1326RT_C_DECLS_END
1327#endif
1328
1329/** @} */
1330
1331#endif
1332
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