VirtualBox

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

Last change on this file since 23002 was 22893, checked in by vboxsync, 15 years ago

vm.h: page align VMCPU::pgm (it's 28KB).

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