VirtualBox

source: vbox/trunk/include/VBox/vm.h@ 19812

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

Implemented HWACCMFlushAllTLBs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.2 KB
Line 
1/** @file
2 * VM - The Virtual Machine, data.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___VBox_vm_h
31#define ___VBox_vm_h
32
33#include <VBox/cdefs.h>
34#include <VBox/types.h>
35#include <VBox/cpum.h>
36#include <VBox/stam.h>
37#include <VBox/vmapi.h>
38#include <VBox/sup.h>
39#include <VBox/vmm.h>
40
41
42/** @defgroup grp_vm The Virtual Machine
43 * @{
44 */
45
46/**
47 * The state of a Virtual CPU.
48 *
49 * The basic state indicated here is whether the CPU has been started or not. In
50 * addition, there are sub-states when started for assisting scheduling (GVMM
51 * mostly).
52 *
53 * The transision out of the STOPPED state is done by a vmR3PowerOn.
54 * The transision back to the STOPPED state is done by vmR3PowerOff.
55 *
56 * (Alternatively we could let vmR3PowerOn start CPU 0 only and let the SPIP
57 * handling switch on the other CPUs. Then vmR3Reset would stop all but CPU 0.)
58 */
59typedef enum VMCPUSTATE
60{
61 /** The customary invalid zero. */
62 VMCPUSTATE_INVALID = 0,
63
64 /** Virtual CPU has not yet been started. */
65 VMCPUSTATE_STOPPED,
66
67 /** CPU started. */
68 VMCPUSTATE_STARTED,
69 /** Executing guest code and can be poked. */
70 VMCPUSTATE_STARTED_EXEC,
71 /** Executing guest code in the recompiler. */
72 VMCPUSTATE_STARTED_EXEC_REM,
73 /** Halted. */
74 VMCPUSTATE_STARTED_HALTED,
75
76 /** The end of valid virtual CPU states. */
77 VMCPUSTATE_END,
78
79 /** Ensure 32-bit type. */
80 VMCPUSTATE_32BIT_HACK = 0x7fffffff
81} VMCPUSTATE;
82
83
84/**
85 * Per virtual CPU data.
86 */
87typedef struct VMCPU
88{
89 /** Per CPU forced action.
90 * See the VMCPU_FF_* \#defines. Updated atomically. */
91 uint32_t volatile fLocalForcedActions;
92 /** The CPU state. */
93 VMCPUSTATE volatile enmState;
94
95 /** Pointer to the ring-3 UVMCPU structure. */
96 PUVMCPU pUVCpu;
97 /** Ring-3 Host Context VM Pointer. */
98 PVMR3 pVMR3;
99 /** Ring-0 Host Context VM Pointer. */
100 PVMR0 pVMR0;
101 /** Raw-mode Context VM Pointer. */
102 PVMRC pVMRC;
103 /** The CPU ID.
104 * This is the index into the VM::aCpu array. */
105 VMCPUID idCpu;
106 /** The native thread handle. */
107 RTNATIVETHREAD hNativeThread;
108 /** Which host CPU ID is this EMT running on.
109 * Only valid when in RC or HWACCMR0 with scheduling disabled. */
110 RTCPUID volatile idHostCpu;
111
112 /** Align the next bit on a 64-byte boundary.
113 *
114 * @remarks The aligments of the members that are larger than 48 bytes should be
115 * 64-byte for cache line reasons. structs containing small amounts of
116 * data could be lumped together at the end with a < 64 byte padding
117 * following it (to grow into and align the struct size).
118 * */
119 uint32_t au32Alignment[HC_ARCH_BITS == 32 ? 7 : 3];
120
121 /** CPUM part. */
122 union
123 {
124#ifdef ___CPUMInternal_h
125 struct CPUMCPU s;
126#endif
127 char padding[4096]; /* multiple of 64 */
128 } cpum;
129
130 /** PGM part. */
131 union
132 {
133#ifdef ___PGMInternal_h
134 struct PGMCPU s;
135#endif
136 char padding[32*1024]; /* multiple of 64 */
137 } pgm;
138
139 /** HWACCM part. */
140 union
141 {
142#ifdef ___HWACCMInternal_h
143 struct HWACCMCPU s;
144#endif
145 char padding[5120]; /* multiple of 64 */
146 } hwaccm;
147
148 /** EM part. */
149 union
150 {
151#ifdef ___EMInternal_h
152 struct EMCPU s;
153#endif
154 char padding[2048]; /* multiple of 64 */
155 } em;
156
157 /** TRPM part. */
158 union
159 {
160#ifdef ___TRPMInternal_h
161 struct TRPMCPU s;
162#endif
163 char padding[128]; /* multiple of 64 */
164 } trpm;
165
166 /** TM part. */
167 union
168 {
169#ifdef ___TMInternal_h
170 struct TMCPU s;
171#endif
172 char padding[64]; /* multiple of 64 */
173 } tm;
174
175 /** VMM part. */
176 union
177 {
178#ifdef ___VMMInternal_h
179 struct VMMCPU s;
180#endif
181 char padding[256]; /* multiple of 64 */
182 } vmm;
183
184 /** PDM part. */
185 union
186 {
187#ifdef ___PDMInternal_h
188 struct PDMCPU s;
189#endif
190 char padding[128]; /* multiple of 64 */
191 } pdm;
192
193 /** DBGF part.
194 * @todo Combine this with other tiny structures. */
195 union
196 {
197#ifdef ___DBGFInternal_h
198 struct DBGFCPU s;
199#endif
200 uint8_t padding[64]; /* multiple of 64 */
201 } dbgf;
202
203} VMCPU;
204
205
206/** @name Operations on VMCPU::enmState
207 * @{ */
208/** Gets the VMCPU state. */
209#define VMCPU_GET_STATE(pVCpu) ( (pVCpu)->enmState )
210/** Sets the VMCPU state. */
211#define VMCPU_SET_STATE(pVCpu, enmNewState) \
212 ASMAtomicWriteU32((uint32_t volatile *)&(pVCpu)->enmState, (enmNewState))
213/** Cmpares and sets the VMCPU state. */
214#define VMCPU_CMPXCHG_STATE(pVCpu, enmNewState, enmOldState) \
215 ASMAtomicCmpXchgU32((uint32_t volatile *)&(pVCpu)->enmState, (enmNewState), (enmOldState))
216/** Checks the VMCPU state. */
217#define VMCPU_ASSERT_STATE(pVCpu, enmExpectedState) \
218 do { \
219 VMCPUSTATE enmState = VMCPU_GET_STATE(pVCpu); \
220 AssertMsg(enmState == (enmExpectedState), \
221 ("enmState=%d enmExpectedState=%d idCpu=%u\n", \
222 enmState, enmExpectedState, (pVCpu)->idCpu)); \
223 } while (0)
224/** Tests if the state means that the CPU is started. */
225#define VMCPUSTATE_IS_STARTED(enmState) ( (enmState) > VMCPUSTATE_STOPPED )
226/** Tests if the state means that the CPU is stopped. */
227#define VMCPUSTATE_IS_STOPPED(enmState) ( (enmState) == VMCPUSTATE_STOPPED )
228/** @} */
229
230
231/** The name of the Guest Context VMM Core module. */
232#define VMMGC_MAIN_MODULE_NAME "VMMGC.gc"
233/** The name of the Ring 0 Context VMM Core module. */
234#define VMMR0_MAIN_MODULE_NAME "VMMR0.r0"
235
236/** VM Forced Action Flags.
237 *
238 * Use the VM_FF_SET() and VM_FF_CLEAR() macros to change the force
239 * action mask of a VM.
240 *
241 * @{
242 */
243/** The virtual sync clock has been stopped, go to TM until it has been
244 * restarted... */
245#define VM_FF_TM_VIRTUAL_SYNC RT_BIT_32(2)
246/** PDM Queues are pending. */
247#define VM_FF_PDM_QUEUES_BIT 3
248#define VM_FF_PDM_QUEUES RT_BIT_32(VM_FF_PDM_QUEUES_BIT)
249/** PDM DMA transfers are pending. */
250#define VM_FF_PDM_DMA_BIT 4
251#define VM_FF_PDM_DMA RT_BIT_32(VM_FF_PDM_DMA_BIT)
252/** This action forces the VM to call DBGF so DBGF can service debugger
253 * requests in the emulation thread.
254 * This action flag stays asserted till DBGF clears it.*/
255#define VM_FF_DBGF_BIT 8
256#define VM_FF_DBGF RT_BIT_32(VM_FF_DBGF_BIT)
257/** This action forces the VM to service pending requests from other
258 * thread or requests which must be executed in another context. */
259#define VM_FF_REQUEST RT_BIT_32(9)
260/** Terminate the VM immediately. */
261#define VM_FF_TERMINATE RT_BIT_32(10)
262/** Reset the VM. (postponed) */
263#define VM_FF_RESET_BIT 11
264#define VM_FF_RESET RT_BIT_32(VM_FF_RESET_BIT)
265/** PGM needs to allocate handy pages. */
266#define VM_FF_PGM_NEED_HANDY_PAGES RT_BIT_32(18)
267/** PGM is out of memory.
268 * Abandon all loops and code paths which can be resumed and get up to the EM
269 * loops. */
270#define VM_FF_PGM_NO_MEMORY RT_BIT_32(19)
271/** REM needs to be informed about handler changes. */
272#define VM_FF_REM_HANDLER_NOTIFY RT_BIT_32(29)
273/** Suspend the VM - debug only. */
274#define VM_FF_DEBUG_SUSPEND RT_BIT_32(31)
275
276
277/** This action forces the VM to service check and pending interrups on the APIC. */
278#define VMCPU_FF_INTERRUPT_APIC RT_BIT_32(0)
279/** This action forces the VM to service check and pending interrups on the PIC. */
280#define VMCPU_FF_INTERRUPT_PIC RT_BIT_32(1)
281/** This action forces the VM to schedule and run pending timer (TM).
282 * @remarks Don't move - PATM compatability. */
283#define VMCPU_FF_TIMER RT_BIT_32(2)
284/** PDM critical section unlocking is pending, process promptly upon return to R3. */
285#define VMCPU_FF_PDM_CRITSECT RT_BIT_32(5)
286/** This action forces the VM to service pending requests from other
287 * thread or requests which must be executed in another context. */
288#define VMCPU_FF_REQUEST RT_BIT_32(9)
289/** This action forces the VM to resync the page tables before going
290 * back to execute guest code. (GLOBAL FLUSH) */
291#define VMCPU_FF_PGM_SYNC_CR3 RT_BIT_32(16)
292/** Same as VM_FF_PGM_SYNC_CR3 except that global pages can be skipped.
293 * (NON-GLOBAL FLUSH) */
294#define VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL RT_BIT_32(17)
295/** Check for pending TLB shootdown actions. */
296#define VMCPU_FF_TLB_SHOOTDOWN RT_BIT_32(18)
297/** Check for pending TLB flush action. */
298#define VMCPU_FF_TLB_FLUSH_BIT 19
299#define VMCPU_FF_TLB_FLUSH RT_BIT_32(VMCPU_FF_TLB_FLUSH_BIT)
300/** Check the interupt and trap gates */
301#define VMCPU_FF_TRPM_SYNC_IDT RT_BIT_32(20)
302/** Check Guest's TSS ring 0 stack */
303#define VMCPU_FF_SELM_SYNC_TSS RT_BIT_32(21)
304/** Check Guest's GDT table */
305#define VMCPU_FF_SELM_SYNC_GDT RT_BIT_32(22)
306/** Check Guest's LDT table */
307#define VMCPU_FF_SELM_SYNC_LDT RT_BIT_32(23)
308/** Inhibit interrupts pending. See EMGetInhibitInterruptsPC(). */
309#define VMCPU_FF_INHIBIT_INTERRUPTS RT_BIT_32(24)
310/** CSAM needs to scan the page that's being executed */
311#define VMCPU_FF_CSAM_SCAN_PAGE RT_BIT_32(26)
312/** CSAM needs to do some homework. */
313#define VMCPU_FF_CSAM_PENDING_ACTION RT_BIT_32(27)
314/** Force return to Ring-3. */
315#define VMCPU_FF_TO_R3 RT_BIT_32(28)
316
317/** Externally VM forced actions. Used to quit the idle/wait loop. */
318#define VM_FF_EXTERNAL_SUSPENDED_MASK (VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_REQUEST)
319/** Externally VMCPU forced actions. Used to quit the idle/wait loop. */
320#define VMCPU_FF_EXTERNAL_SUSPENDED_MASK (VMCPU_FF_REQUEST)
321
322/** Externally forced VM actions. Used to quit the idle/wait loop. */
323#define VM_FF_EXTERNAL_HALTED_MASK (VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA)
324/** Externally forced VMCPU actions. Used to quit the idle/wait loop. */
325#define VMCPU_FF_EXTERNAL_HALTED_MASK (VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_REQUEST | VMCPU_FF_TIMER)
326
327/** High priority VM pre-execution actions. */
328#define VM_FF_HIGH_PRIORITY_PRE_MASK ( VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_TM_VIRTUAL_SYNC | VM_FF_DEBUG_SUSPEND \
329 | VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY)
330/** High priority VMCPU pre-execution actions. */
331#define VMCPU_FF_HIGH_PRIORITY_PRE_MASK ( VMCPU_FF_TIMER | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_PGM_SYNC_CR3 \
332 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_TRPM_SYNC_IDT \
333 | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_INHIBIT_INTERRUPTS)
334
335/** High priority VM pre raw-mode execution mask. */
336#define VM_FF_HIGH_PRIORITY_PRE_RAW_MASK (VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY)
337/** High priority VMCPU pre raw-mode execution mask. */
338#define VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK ( VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_TRPM_SYNC_IDT \
339 | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_INHIBIT_INTERRUPTS)
340
341/** High priority post-execution actions. */
342#define VM_FF_HIGH_PRIORITY_POST_MASK (VM_FF_PGM_NO_MEMORY)
343/** High priority post-execution actions. */
344#define VMCPU_FF_HIGH_PRIORITY_POST_MASK (VMCPU_FF_PDM_CRITSECT|VMCPU_FF_CSAM_PENDING_ACTION)
345
346/** Normal priority VM post-execution actions. */
347#define VM_FF_NORMAL_PRIORITY_POST_MASK (VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_RESET | VM_FF_PGM_NO_MEMORY)
348/** Normal priority VMCPU post-execution actions. */
349#define VMCPU_FF_NORMAL_PRIORITY_POST_MASK (VMCPU_FF_CSAM_SCAN_PAGE)
350
351/** Normal priority VM actions. */
352#define VM_FF_NORMAL_PRIORITY_MASK (VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA | VM_FF_REM_HANDLER_NOTIFY)
353/** Normal priority VMCPU actions. */
354#define VMCPU_FF_NORMAL_PRIORITY_MASK (VMCPU_FF_REQUEST)
355
356/** Flags to clear before resuming guest execution. */
357#define VMCPU_FF_RESUME_GUEST_MASK (VMCPU_FF_TO_R3)
358
359/** VM Flags that cause the HWACCM loops to go back to ring-3. */
360#define VM_FF_HWACCM_TO_R3_MASK (VM_FF_TM_VIRTUAL_SYNC | VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY)
361/** VMCPU Flags that cause the HWACCM loops to go back to ring-3. */
362#define VMCPU_FF_HWACCM_TO_R3_MASK (VMCPU_FF_TO_R3 | VMCPU_FF_TIMER)
363
364/** All the forced VM flags. */
365#define VM_FF_ALL_MASK (~0U)
366/** All the forced VMCPU flags. */
367#define VMCPU_FF_ALL_MASK (~0U)
368
369/** All the forced VM flags. */
370#define VM_FF_ALL_BUT_RAW_MASK (~(VM_FF_HIGH_PRIORITY_PRE_RAW_MASK) | VM_FF_PGM_NO_MEMORY)
371/** All the forced VMCPU flags. */
372#define VMCPU_FF_ALL_BUT_RAW_MASK (~(VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK | VMCPU_FF_CSAM_PENDING_ACTION | VMCPU_FF_PDM_CRITSECT))
373
374/** @} */
375
376/** @def VM_FF_SET
377 * Sets a force action flag.
378 *
379 * @param pVM VM Handle.
380 * @param fFlag The flag to set.
381 */
382#if 1
383# define VM_FF_SET(pVM, fFlag) ASMAtomicOrU32(&(pVM)->fGlobalForcedActions, (fFlag))
384#else
385# define VM_FF_SET(pVM, fFlag) \
386 do { ASMAtomicOrU32(&(pVM)->fGlobalForcedActions, (fFlag)); \
387 RTLogPrintf("VM_FF_SET : %08x %s - %s(%d) %s\n", (pVM)->fGlobalForcedActions, #fFlag, __FILE__, __LINE__, __FUNCTION__); \
388 } while (0)
389#endif
390
391/** @def VMCPU_FF_SET
392 * Sets a force action flag for the given VCPU.
393 *
394 * @param pVCpu VMCPU Handle.
395 * @param fFlag The flag to set.
396 */
397#define VMCPU_FF_SET(pVCpu, fFlag) ASMAtomicOrU32(&(pVCpu)->fLocalForcedActions, (fFlag))
398
399/** @def VM_FF_CLEAR
400 * Clears a force action flag.
401 *
402 * @param pVM VM Handle.
403 * @param fFlag The flag to clear.
404 */
405#if 1
406# define VM_FF_CLEAR(pVM, fFlag) ASMAtomicAndU32(&(pVM)->fGlobalForcedActions, ~(fFlag))
407#else
408# define VM_FF_CLEAR(pVM, fFlag) \
409 do { ASMAtomicAndU32(&(pVM)->fGlobalForcedActions, ~(fFlag)); \
410 RTLogPrintf("VM_FF_CLEAR: %08x %s - %s(%d) %s\n", (pVM)->fGlobalForcedActions, #fFlag, __FILE__, __LINE__, __FUNCTION__); \
411 } while (0)
412#endif
413
414/** @def VMCPU_FF_CLEAR
415 * Clears a force action flag for the given VCPU.
416 *
417 * @param pVCpu VMCPU Handle.
418 * @param fFlag The flag to clear.
419 */
420#define VMCPU_FF_CLEAR(pVCpu, fFlag) ASMAtomicAndU32(&(pVCpu)->fLocalForcedActions, ~(fFlag))
421
422/** @def VM_FF_ISSET
423 * Checks if a force action flag is set.
424 *
425 * @param pVM VM Handle.
426 * @param fFlag The flag to check.
427 */
428#define VM_FF_ISSET(pVM, fFlag) (((pVM)->fGlobalForcedActions & (fFlag)) == (fFlag))
429
430/** @def VMCPU_FF_ISSET
431 * Checks if a force action flag is set for the given VCPU.
432 *
433 * @param pVCpu VMCPU Handle.
434 * @param fFlag The flag to check.
435 */
436#define VMCPU_FF_ISSET(pVCpu, fFlag) (((pVCpu)->fLocalForcedActions & (fFlag)) == (fFlag))
437
438/** @def VM_FF_ISPENDING
439 * Checks if one or more force action in the specified set is pending.
440 *
441 * @param pVM VM Handle.
442 * @param fFlags The flags to check for.
443 */
444#define VM_FF_ISPENDING(pVM, fFlags) ((pVM)->fGlobalForcedActions & (fFlags))
445
446/** @def VM_FF_TESTANDCLEAR
447 * Checks if one (!) force action in the specified set is pending and clears it atomically
448 *
449 * @returns true if the bit was set.
450 * @returns false if the bit was clear.
451 * @param pVM VM Handle.
452 * @param iBit Bit position to check and clear
453 */
454#define VM_FF_TESTANDCLEAR(pVM, iBit) (ASMBitTestAndClear(&(pVM)->fGlobalForcedActions, iBit))
455
456/** @def VMCPU_FF_TESTANDCLEAR
457 * Checks if one (!) force action in the specified set is pending and clears it atomically
458 *
459 * @returns true if the bit was set.
460 * @returns false if the bit was clear.
461 * @param pVCpu VMCPU Handle.
462 * @param iBit Bit position to check and clear
463 */
464#define VMCPU_FF_TESTANDCLEAR(pVCpu, iBit) (ASMBitTestAndClear(&(pVCpu)->fLocalForcedActions, iBit))
465
466/** @def VMCPU_FF_ISPENDING
467 * Checks if one or more force action in the specified set is pending for the given VCPU.
468 *
469 * @param pVCpu VMCPU Handle.
470 * @param fFlags The flags to check for.
471 */
472#define VMCPU_FF_ISPENDING(pVCpu, fFlags) ((pVCpu)->fLocalForcedActions & (fFlags))
473
474/** @def VM_FF_ISPENDING
475 * Checks if one or more force action in the specified set is pending while one
476 * or more other ones are not.
477 *
478 * @param pVM VM Handle.
479 * @param fFlags The flags to check for.
480 * @param fExcpt The flags that should not be set.
481 */
482#define VM_FF_IS_PENDING_EXCEPT(pVM, fFlags, fExcpt) ( ((pVM)->fGlobalForcedActions & (fFlags)) && !((pVM)->fGlobalForcedActions & (fExcpt)) )
483
484/** @def VMCPU_FF_IS_PENDING_EXCEPT
485 * Checks if one or more force action in the specified set is pending for the given
486 * VCPU while one or more other ones are not.
487 *
488 * @param pVCpu VMCPU Handle.
489 * @param fFlags The flags to check for.
490 * @param fExcpt The flags that should not be set.
491 */
492#define VMCPU_FF_IS_PENDING_EXCEPT(pVCpu, fFlags, fExcpt) ( ((pVCpu)->fLocalForcedActions & (fFlags)) && !((pVCpu)->fLocalForcedActions & (fExcpt)) )
493
494/** @def VM_IS_EMT
495 * Checks if the current thread is the emulation thread (EMT).
496 *
497 * @remark The ring-0 variation will need attention if we expand the ring-0
498 * code to let threads other than EMT mess around with the VM.
499 */
500#ifdef IN_RC
501# define VM_IS_EMT(pVM) true
502#else
503# define VM_IS_EMT(pVM) (VMMGetCpu(pVM) != NULL)
504#endif
505
506/** @def VMCPU_IS_EMT
507 * Checks if the current thread is the emulation thread (EMT) for the specified
508 * virtual CPU.
509 */
510#ifdef IN_RC
511# define VMCPU_IS_EMT(pVCpu) true
512#else
513# define VMCPU_IS_EMT(pVCpu) ((pVCpu) && ((pVCpu) == VMMGetCpu((pVCpu)->CTX_SUFF(pVM))))
514#endif
515
516/** @def VM_ASSERT_EMT
517 * Asserts that the current thread IS the emulation thread (EMT).
518 */
519#ifdef IN_RC
520# define VM_ASSERT_EMT(pVM) Assert(VM_IS_EMT(pVM))
521#elif defined(IN_RING0)
522# define VM_ASSERT_EMT(pVM) Assert(VM_IS_EMT(pVM))
523#else
524# define VM_ASSERT_EMT(pVM) \
525 AssertMsg(VM_IS_EMT(pVM), \
526 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd\n", RTThreadNativeSelf(), VMR3GetVMCPUNativeThread(pVM)))
527#endif
528
529/** @def VMCPU_ASSERT_EMT
530 * Asserts that the current thread IS the emulation thread (EMT) of the
531 * specified virtual CPU.
532 */
533#ifdef IN_RC
534# define VMCPU_ASSERT_EMT(pVCpu) Assert(VMCPU_IS_EMT(pVCpu))
535#elif defined(IN_RING0)
536# define VMCPU_ASSERT_EMT(pVCpu) Assert(VMCPU_IS_EMT(pVCpu))
537#else
538# define VMCPU_ASSERT_EMT(pVCpu) \
539 AssertMsg(VMCPU_IS_EMT(pVCpu), \
540 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%#x\n", \
541 RTThreadNativeSelf(), (pVCpu)->hNativeThread, (pVCpu)->idCpu))
542#endif
543
544/** @def VM_ASSERT_EMT_RETURN
545 * Asserts that the current thread IS the emulation thread (EMT) and returns if it isn't.
546 */
547#ifdef IN_RC
548# define VM_ASSERT_EMT_RETURN(pVM, rc) AssertReturn(VM_IS_EMT(pVM), (rc))
549#elif defined(IN_RING0)
550# define VM_ASSERT_EMT_RETURN(pVM, rc) AssertReturn(VM_IS_EMT(pVM), (rc))
551#else
552# define VM_ASSERT_EMT_RETURN(pVM, rc) \
553 AssertMsgReturn(VM_IS_EMT(pVM), \
554 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd\n", RTThreadNativeSelf(), VMR3GetVMCPUNativeThread(pVM)), \
555 (rc))
556#endif
557
558/** @def VMCPU_ASSERT_EMT_RETURN
559 * Asserts that the current thread IS the emulation thread (EMT) and returns if it isn't.
560 */
561#ifdef IN_RC
562# define VMCPU_ASSERT_EMT_RETURN(pVCpu, rc) AssertReturn(VMCPU_IS_EMT(pVCpu), (rc))
563#elif defined(IN_RING0)
564# define VMCPU_ASSERT_EMT_RETURN(pVCpu, rc) AssertReturn(VMCPU_IS_EMT(pVCpu), (rc))
565#else
566# define VMCPU_ASSERT_EMT_RETURN(pVCpu, rc) \
567 AssertMsg(VMCPU_IS_EMT(pVCpu), \
568 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%#x\n", \
569 RTThreadNativeSelf(), (pVCpu)->hNativeThread, (pVCpu)->idCpu), \
570 (rc))
571#endif
572
573
574/**
575 * Asserts that the current thread is NOT the emulation thread.
576 */
577#define VM_ASSERT_OTHER_THREAD(pVM) \
578 AssertMsg(!VM_IS_EMT(pVM), ("Not other thread!!\n"))
579
580
581/** @def VM_ASSERT_STATE_RETURN
582 * Asserts a certain VM state.
583 */
584#define VM_ASSERT_STATE(pVM, _enmState) \
585 AssertMsg((pVM)->enmVMState == (_enmState), \
586 ("state %s, expected %s\n", VMGetStateName((pVM)->enmVMState), VMGetStateName(_enmState)))
587
588/** @def VM_ASSERT_STATE_RETURN
589 * Asserts a certain VM state and returns if it doesn't match.
590 */
591#define VM_ASSERT_STATE_RETURN(pVM, _enmState, rc) \
592 AssertMsgReturn((pVM)->enmVMState == (_enmState), \
593 ("state %s, expected %s\n", VMGetStateName((pVM)->enmVMState), VMGetStateName(_enmState)), \
594 (rc))
595
596/** @def VM_ASSERT_VALID_EXT_RETURN
597 * Asserts a the VM handle is valid for external access, i.e. not being
598 * destroy or terminated.
599 */
600#define VM_ASSERT_VALID_EXT_RETURN(pVM, rc) \
601 AssertMsgReturn( RT_VALID_ALIGNED_PTR(pVM, PAGE_SIZE) \
602 && (unsigned)(pVM)->enmVMState < (unsigned)VMSTATE_DESTROYING, \
603 ("pVM=%p state %s\n", (pVM), RT_VALID_ALIGNED_PTR(pVM, PAGE_SIZE) \
604 ? VMGetStateName(pVM->enmVMState) : ""), \
605 (rc))
606
607/** @def VMCPU_ASSERT_VALID_EXT_RETURN
608 * Asserts a the VMCPU handle is valid for external access, i.e. not being
609 * destroy or terminated.
610 */
611#define VMCPU_ASSERT_VALID_EXT_RETURN(pVCpu, rc) \
612 AssertMsgReturn( RT_VALID_ALIGNED_PTR(pVCpu, 64) \
613 && RT_VALID_ALIGNED_PTR((pVCpu)->CTX_SUFF(pVM), PAGE_SIZE) \
614 && (unsigned)(pVCpu)->CTX_SUFF(pVM)->enmVMState < (unsigned)VMSTATE_DESTROYING, \
615 ("pVCpu=%p pVM=%p state %s\n", (pVCpu), RT_VALID_ALIGNED_PTR(pVCpu, 64) ? (pVCpu)->CTX_SUFF(pVM) : NULL, \
616 RT_VALID_ALIGNED_PTR(pVCpu, 64) && RT_VALID_ALIGNED_PTR((pVCpu)->CTX_SUFF(pVM), PAGE_SIZE) \
617 ? VMGetStateName((pVCpu)->pVMR3->enmVMState) : ""), \
618 (rc))
619
620
621/** This is the VM structure.
622 *
623 * It contains (nearly?) all the VM data which have to be available in all
624 * contexts. Even if it contains all the data the idea is to use APIs not
625 * to modify all the members all around the place. Therefore we make use of
626 * unions to hide everything which isn't local to the current source module.
627 * This means we'll have to pay a little bit of attention when adding new
628 * members to structures in the unions and make sure to keep the padding sizes
629 * up to date.
630 *
631 * Run tstVMStructSize after update!
632 */
633typedef struct VM
634{
635 /** The state of the VM.
636 * This field is read only to everyone except the VM and EM. */
637 VMSTATE enmVMState;
638 /** Forced action flags.
639 * See the VM_FF_* \#defines. Updated atomically.
640 */
641 volatile uint32_t fGlobalForcedActions;
642 /** Pointer to the array of page descriptors for the VM structure allocation. */
643 R3PTRTYPE(PSUPPAGE) paVMPagesR3;
644 /** Session handle. For use when calling SUPR0 APIs. */
645 PSUPDRVSESSION pSession;
646 /** Pointer to the ring-3 VM structure. */
647 PUVM pUVM;
648 /** Ring-3 Host Context VM Pointer. */
649 R3PTRTYPE(struct VM *) pVMR3;
650 /** Ring-0 Host Context VM Pointer. */
651 R0PTRTYPE(struct VM *) pVMR0;
652 /** Raw-mode Context VM Pointer. */
653 RCPTRTYPE(struct VM *) pVMRC;
654
655 /** The GVM VM handle. Only the GVM should modify this field. */
656 uint32_t hSelf;
657 /** Number of virtual CPUs. */
658 uint32_t cCPUs;
659
660 /** Size of the VM structure including the VMCPU array. */
661 uint32_t cbSelf;
662
663 /** Offset to the VMCPU array starting from beginning of this structure. */
664 uint32_t offVMCPU;
665
666 /** Reserved; alignment. */
667 uint32_t u32Reserved[6];
668
669 /** @name Public VMM Switcher APIs
670 * @{ */
671 /**
672 * Assembly switch entry point for returning to host context.
673 * This function will clean up the stack frame.
674 *
675 * @param eax The return code, register.
676 * @param Ctx The guest core context.
677 * @remark Assume interrupts disabled.
678 */
679 RTRCPTR pfnVMMGCGuestToHostAsmGuestCtx/*(int32_t eax, CPUMCTXCORE Ctx)*/;
680
681 /**
682 * Assembly switch entry point for returning to host context.
683 *
684 * This is an alternative entry point which we'll be using when the we have the
685 * hypervisor context and need to save that before going to the host.
686 *
687 * This is typically useful when abandoning the hypervisor because of a trap
688 * and want the trap state to be saved.
689 *
690 * @param eax The return code, register.
691 * @param ecx Pointer to the hypervisor core context, register.
692 * @remark Assume interrupts disabled.
693 */
694 RTRCPTR pfnVMMGCGuestToHostAsmHyperCtx/*(int32_t eax, PCPUMCTXCORE ecx)*/;
695
696 /**
697 * Assembly switch entry point for returning to host context.
698 *
699 * This is an alternative to the two *Ctx APIs and implies that the context has already
700 * been saved, or that it's just a brief return to HC and that the caller intends to resume
701 * whatever it is doing upon 'return' from this call.
702 *
703 * @param eax The return code, register.
704 * @remark Assume interrupts disabled.
705 */
706 RTRCPTR pfnVMMGCGuestToHostAsm/*(int32_t eax)*/;
707 /** @} */
708
709
710 /** @name Various VM data owned by VM.
711 * @{ */
712 RTTHREAD uPadding1;
713 /** The native handle of ThreadEMT. Getting the native handle
714 * is generally faster than getting the IPRT one (except on OS/2 :-). */
715 RTNATIVETHREAD uPadding2;
716 /** @} */
717
718
719 /** @name Various items that are frequently accessed.
720 * @{ */
721 /** Raw ring-3 indicator. */
722 bool fRawR3Enabled;
723 /** Raw ring-0 indicator. */
724 bool fRawR0Enabled;
725 /** PATM enabled flag.
726 * This is placed here for performance reasons. */
727 bool fPATMEnabled;
728 /** CSAM enabled flag.
729 * This is placed here for performance reasons. */
730 bool fCSAMEnabled;
731 /** Hardware VM support is available and enabled.
732 * This is placed here for performance reasons. */
733 bool fHWACCMEnabled;
734 /** Hardware VM support is required and non-optional.
735 * This is initialized together with the rest of the VM structure. */
736 bool fHwVirtExtForced;
737 /** PARAV enabled flag. */
738 bool fPARAVEnabled;
739 /** @} */
740
741
742 /* padding to make gnuc put the StatQemuToGC where msc does. */
743#if HC_ARCH_BITS == 32
744 uint32_t padding0;
745#endif
746
747 /** Profiling the total time from Qemu to GC. */
748 STAMPROFILEADV StatTotalQemuToGC;
749 /** Profiling the total time from GC to Qemu. */
750 STAMPROFILEADV StatTotalGCToQemu;
751 /** Profiling the total time spent in GC. */
752 STAMPROFILEADV StatTotalInGC;
753 /** Profiling the total time spent not in Qemu. */
754 STAMPROFILEADV StatTotalInQemu;
755 /** Profiling the VMMSwitcher code for going to GC. */
756 STAMPROFILEADV StatSwitcherToGC;
757 /** Profiling the VMMSwitcher code for going to HC. */
758 STAMPROFILEADV StatSwitcherToHC;
759 STAMPROFILEADV StatSwitcherSaveRegs;
760 STAMPROFILEADV StatSwitcherSysEnter;
761 STAMPROFILEADV StatSwitcherDebug;
762 STAMPROFILEADV StatSwitcherCR0;
763 STAMPROFILEADV StatSwitcherCR4;
764 STAMPROFILEADV StatSwitcherJmpCR3;
765 STAMPROFILEADV StatSwitcherRstrRegs;
766 STAMPROFILEADV StatSwitcherLgdt;
767 STAMPROFILEADV StatSwitcherLidt;
768 STAMPROFILEADV StatSwitcherLldt;
769 STAMPROFILEADV StatSwitcherTSS;
770
771/** @todo Realign everything on 64 byte boundaries to better match the
772 * cache-line size. */
773 /* padding - the unions must be aligned on 32 bytes boundraries. */
774 uint32_t padding[HC_ARCH_BITS == 32 ? 4+8 : 6];
775
776 /** CPUM part. */
777 union
778 {
779#ifdef ___CPUMInternal_h
780 struct CPUM s;
781#endif
782 char padding[2048]; /* multiple of 32 */
783 } cpum;
784
785 /** VMM part. */
786 union
787 {
788#ifdef ___VMMInternal_h
789 struct VMM s;
790#endif
791 char padding[1600]; /* multiple of 32 */
792 } vmm;
793
794 /** PGM part. */
795 union
796 {
797#ifdef ___PGMInternal_h
798 struct PGM s;
799#endif
800 char padding[16*1024]; /* multiple of 32 */
801 } pgm;
802
803 /** HWACCM part. */
804 union
805 {
806#ifdef ___HWACCMInternal_h
807 struct HWACCM s;
808#endif
809 char padding[512]; /* multiple of 32 */
810 } hwaccm;
811
812 /** TRPM part. */
813 union
814 {
815#ifdef ___TRPMInternal_h
816 struct TRPM s;
817#endif
818 char padding[5344]; /* multiple of 32 */
819 } trpm;
820
821 /** SELM part. */
822 union
823 {
824#ifdef ___SELMInternal_h
825 struct SELM s;
826#endif
827 char padding[544]; /* multiple of 32 */
828 } selm;
829
830 /** MM part. */
831 union
832 {
833#ifdef ___MMInternal_h
834 struct MM s;
835#endif
836 char padding[192]; /* multiple of 32 */
837 } mm;
838
839 /** CFGM part. */
840 union
841 {
842#ifdef ___CFGMInternal_h
843 struct CFGM s;
844#endif
845 char padding[32]; /* multiple of 32 */
846 } cfgm;
847
848 /** PDM part. */
849 union
850 {
851#ifdef ___PDMInternal_h
852 struct PDM s;
853#endif
854 char padding[1824]; /* multiple of 32 */
855 } pdm;
856
857 /** IOM part. */
858 union
859 {
860#ifdef ___IOMInternal_h
861 struct IOM s;
862#endif
863 char padding[4544]; /* multiple of 32 */
864 } iom;
865
866 /** PATM part. */
867 union
868 {
869#ifdef ___PATMInternal_h
870 struct PATM s;
871#endif
872 char padding[768]; /* multiple of 32 */
873 } patm;
874
875 /** CSAM part. */
876 union
877 {
878#ifdef ___CSAMInternal_h
879 struct CSAM s;
880#endif
881 char padding[3328]; /* multiple of 32 */
882 } csam;
883
884 /** PARAV part. */
885 union
886 {
887#ifdef ___PARAVInternal_h
888 struct PARAV s;
889#endif
890 char padding[128];
891 } parav;
892
893 /** EM part. */
894 union
895 {
896#ifdef ___EMInternal_h
897 struct EM s;
898#endif
899 char padding[256]; /* multiple of 32 */
900 } em;
901
902 /** TM part. */
903 union
904 {
905#ifdef ___TMInternal_h
906 struct TM s;
907#endif
908 char padding[1920]; /* multiple of 32 */
909 } tm;
910
911 /** DBGF part. */
912 union
913 {
914#ifdef ___DBGFInternal_h
915 struct DBGF s;
916#endif
917 char padding[2368]; /* multiple of 32 */
918 } dbgf;
919
920 /** SSM part. */
921 union
922 {
923#ifdef ___SSMInternal_h
924 struct SSM s;
925#endif
926 char padding[32]; /* multiple of 32 */
927 } ssm;
928
929 /** VM part. */
930 union
931 {
932#ifdef ___VMInternal_h
933 struct VMINT s;
934#endif
935 char padding[768]; /* multiple of 32 */
936 } vm;
937
938 /** REM part. */
939 union
940 {
941#ifdef ___REMInternal_h
942 struct REM s;
943#endif
944
945/** @def VM_REM_SIZE
946 * Must be multiple of 32 and coherent with REM_ENV_SIZE from REMInternal.h. */
947#if GC_ARCH_BITS == 32
948# define VM_REM_SIZE (HC_ARCH_BITS == 32 ? 0x10800 : 0x10800)
949#else
950# define VM_REM_SIZE (HC_ARCH_BITS == 32 ? 0x10900 : 0x10900)
951#endif
952 char padding[VM_REM_SIZE]; /* multiple of 32 */
953 } rem;
954
955 /** Padding for aligning the cpu array on a 64 byte boundrary. */
956 uint32_t u32Reserved2[8];
957
958 /** VMCPU array for the configured number of virtual CPUs.
959 * Must be aligned on a 64-byte boundrary. */
960 VMCPU aCpus[1];
961} VM;
962
963
964#ifdef IN_RC
965__BEGIN_DECLS
966
967/** The VM structure.
968 * This is imported from the VMMGCBuiltin module, i.e. it's a one
969 * of those magic globals which we should avoid using.
970 */
971extern DECLIMPORT(VM) g_VM;
972
973__END_DECLS
974#endif
975
976/** @} */
977
978#endif
979
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