VirtualBox

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

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

VBox/vm.h: added VMCPU_CMPXCHG_STATE.

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