VirtualBox

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

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

VMM: VMCPU::enmState.

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