vm.h revision 9ecc480e5268a3685ec1b05c0dad9b59adf19575
/** @file
* VM - The Virtual Machine, data. (VMM)
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
#ifndef ___VBox_vm_h
#define ___VBox_vm_h
/** @defgroup grp_vm The Virtual Machine
* @{
*/
/**
* The state of a Virtual CPU.
*
* The basic state indicated here is whether the CPU has been started or not. In
* addition, there are sub-states when started for assisting scheduling (GVMM
* mostly).
*
* The transision out of the STOPPED state is done by a vmR3PowerOn.
* The transision back to the STOPPED state is done by vmR3PowerOff.
*
* (Alternatively we could let vmR3PowerOn start CPU 0 only and let the SPIP
* handling switch on the other CPUs. Then vmR3Reset would stop all but CPU 0.)
*/
typedef enum VMCPUSTATE
{
/** The customary invalid zero. */
VMCPUSTATE_INVALID = 0,
/** Virtual CPU has not yet been started. */
/** CPU started. */
/** Executing guest code and can be poked. */
/** Executing guest code in the recompiler. */
/** Halted. */
/** The end of valid virtual CPU states. */
/** Ensure 32-bit type. */
VMCPUSTATE_32BIT_HACK = 0x7fffffff
} VMCPUSTATE;
/**
* Per virtual CPU data.
*/
typedef struct VMCPU
{
/** Per CPU forced action.
* See the VMCPU_FF_* \#defines. Updated atomically. */
uint32_t volatile fLocalForcedActions;
/** The CPU state. */
VMCPUSTATE volatile enmState;
/** Pointer to the ring-3 UVMCPU structure. */
/** Ring-3 Host Context VM Pointer. */
/** Ring-0 Host Context VM Pointer. */
/** Raw-mode Context VM Pointer. */
/** The CPU ID.
* This is the index into the VM::aCpu array. */
/** The native thread handle. */
/** Which host CPU ID is this EMT running on.
* Only valid when in RC or HWACCMR0 with scheduling disabled. */
/** Align the next bit on a 64-byte boundary and make sure it starts at the same
* offset in both 64-bit and 32-bit builds.
*
* @remarks The aligments of the members that are larger than 48 bytes should be
* 64-byte for cache line reasons. structs containing small amounts of
* data could be lumped together at the end with a < 64 byte padding
* following it (to grow into and align the struct size).
* */
/** CPUM part. */
union
{
#ifdef ___CPUMInternal_h
struct CPUMCPU s;
#endif
} cpum;
/** HWACCM part. */
union
{
#ifdef ___HWACCMInternal_h
struct HWACCMCPU s;
#endif
} hwaccm;
/** EM part. */
union
{
#ifdef ___EMInternal_h
struct EMCPU s;
#endif
} em;
/** TRPM part. */
union
{
#ifdef ___TRPMInternal_h
struct TRPMCPU s;
#endif
} trpm;
/** TM part. */
union
{
#ifdef ___TMInternal_h
struct TMCPU s;
#endif
} tm;
/** VMM part. */
union
{
#ifdef ___VMMInternal_h
struct VMMCPU s;
#endif
} vmm;
/** PDM part. */
union
{
#ifdef ___PDMInternal_h
struct PDMCPU s;
#endif
} pdm;
/** IOM part. */
union
{
#ifdef ___IOMInternal_h
struct IOMCPU s;
#endif
} iom;
/** DBGF part.
* @todo Combine this with other tiny structures. */
union
{
#ifdef ___DBGFInternal_h
struct DBGFCPU s;
#endif
} dbgf;
/** Align the following members on page boundrary. */
/** PGM part. */
union
{
#ifdef ___PGMInternal_h
struct PGMCPU s;
#endif
} pgm;
} VMCPU;
/** @name Operations on VMCPU::enmState
* @{ */
/** Gets the VMCPU state. */
/** Sets the VMCPU state. */
/** Cmpares and sets the VMCPU state. */
/** Checks the VMCPU state. */
do { \
("enmState=%d enmExpectedState=%d idCpu=%u\n", \
} while (0)
/** Tests if the state means that the CPU is started. */
/** Tests if the state means that the CPU is stopped. */
/** @} */
/** The name of the Guest Context VMM Core module. */
#define VMMGC_MAIN_MODULE_NAME "VMMGC.gc"
/** The name of the Ring 0 Context VMM Core module. */
#define VMMR0_MAIN_MODULE_NAME "VMMR0.r0"
/** VM Forced Action Flags.
*
* Use the VM_FF_SET() and VM_FF_CLEAR() macros to change the force
* action mask of a VM.
*
* @{
*/
/** The virtual sync clock has been stopped, go to TM until it has been
* restarted... */
/** PDM Queues are pending. */
/** The bit number for VM_FF_PDM_QUEUES. */
#define VM_FF_PDM_QUEUES_BIT 3
/** PDM DMA transfers are pending. */
/** The bit number for VM_FF_PDM_DMA. */
#define VM_FF_PDM_DMA_BIT 4
/** This action forces the VM to call DBGF so DBGF can service debugger
* requests in the emulation thread.
* This action flag stays asserted till DBGF clears it.*/
/** The bit number for VM_FF_DBGF. */
#define VM_FF_DBGF_BIT 8
/** This action forces the VM to service pending requests from other
* thread or requests which must be executed in another context. */
/** Check for VM state changes and take appropriate action. */
/** The bit number for VM_FF_CHECK_VM_STATE. */
#define VM_FF_CHECK_VM_STATE_BIT 10
/** Reset the VM. (postponed) */
/** The bit number for VM_FF_RESET. */
#define VM_FF_RESET_BIT 11
/** EMT rendezvous in VMM. */
/** The bit number for VM_FF_EMT_RENDEZVOUS. */
#define VM_FF_EMT_RENDEZVOUS_BIT 12
/** PGM needs to allocate handy pages. */
/** PGM is out of memory.
* Abandon all loops and code paths which can be resumed and get up to the EM
* loops. */
/** PGM is about to perform a lightweight pool flush
* Guest SMP: all EMT threads should return to ring 3
*/
/** REM needs to be informed about handler changes. */
/** The bit number for VM_FF_REM_HANDLER_NOTIFY. */
#define VM_FF_REM_HANDLER_NOTIFY_BIT 29
/** Suspend the VM - debug only. */
/** This action forces the VM to check any pending interrups on the APIC. */
#define VMCPU_FF_INTERRUPT_APIC RT_BIT_32(0)
/** This action forces the VM to check any pending interrups on the PIC. */
/** This action forces the VM to schedule and run pending timer (TM).
* @remarks Don't move - PATM compatability. */
/** This action forces the VM to check any pending NMIs. */
#define VMCPU_FF_INTERRUPT_NMI_BIT 3
/** This action forces the VM to check any pending SMIs. */
#define VMCPU_FF_INTERRUPT_SMI_BIT 4
/** PDM critical section unlocking is pending, process promptly upon return to R3. */
/** This action forces the VM to service pending requests from other
* thread or requests which must be executed in another context. */
/** This action forces the VM to resync the page tables before going
* back to execute guest code. (GLOBAL FLUSH) */
/** Same as VM_FF_PGM_SYNC_CR3 except that global pages can be skipped.
* (NON-GLOBAL FLUSH) */
/** Check for pending TLB shootdown actions. */
/** Check for pending TLB flush action. */
/** The bit number for VMCPU_FF_TLB_FLUSH. */
#define VMCPU_FF_TLB_FLUSH_BIT 19
/** Check the interupt and trap gates */
/** Check Guest's TSS ring 0 stack */
/** Check Guest's GDT table */
/** Check Guest's LDT table */
/** Inhibit interrupts pending. See EMGetInhibitInterruptsPC(). */
/** CSAM needs to scan the page that's being executed */
/** CSAM needs to do some homework. */
/** Force return to Ring-3. */
#define VM_FF_EXTERNAL_SUSPENDED_MASK (VM_FF_CHECK_VM_STATE | VM_FF_DBGF | VM_FF_REQUEST | VM_FF_EMT_RENDEZVOUS)
#define VMCPU_FF_EXTERNAL_HALTED_MASK (VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_REQUEST | VMCPU_FF_TIMER)
/** High priority VM pre-execution actions. */
/** High priority VMCPU pre-execution actions. */
#define VMCPU_FF_HIGH_PRIORITY_PRE_MASK ( VMCPU_FF_TIMER | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_PGM_SYNC_CR3 \
/** High priority VM pre raw-mode execution mask. */
/** High priority VMCPU pre raw-mode execution mask. */
#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 \
/** High priority post-execution actions. */
/** High priority post-execution actions. */
/** Normal priority VM post-execution actions. */
/** Normal priority VMCPU post-execution actions. */
/** Normal priority VM actions. */
#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)
/** Normal priority VMCPU actions. */
/** Flags to clear before resuming guest execution. */
#define VMCPU_FF_RESUME_GUEST_MASK (VMCPU_FF_TO_R3)
/** VM Flags that cause the HWACCM loops to go back to ring-3. */
#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 | VM_FF_EMT_RENDEZVOUS)
/** VMCPU Flags that cause the HWACCM loops to go back to ring-3. */
/** All the forced VM flags. */
#define VM_FF_ALL_MASK (~0U)
/** All the forced VMCPU flags. */
#define VMCPU_FF_ALL_MASK (~0U)
/** All the forced VM flags. */
/** All the forced VMCPU flags. */
#define VMCPU_FF_ALL_BUT_RAW_MASK (~(VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK | VMCPU_FF_CSAM_PENDING_ACTION | VMCPU_FF_PDM_CRITSECT))
/** @} */
/** @def VM_FF_SET
* Sets a force action flag.
*
* @param pVM VM Handle.
* @param fFlag The flag to set.
*/
#if 1
#else
RTLogPrintf("VM_FF_SET : %08x %s - %s(%d) %s\n", (pVM)->fGlobalForcedActions, #fFlag, __FILE__, __LINE__, __FUNCTION__); \
} while (0)
#endif
/** @def VMCPU_FF_SET
* Sets a force action flag for the given VCPU.
*
* @param pVCpu VMCPU Handle.
* @param fFlag The flag to set.
*/
/** @def VM_FF_CLEAR
* Clears a force action flag.
*
* @param pVM VM Handle.
* @param fFlag The flag to clear.
*/
#if 1
#else
RTLogPrintf("VM_FF_CLEAR: %08x %s - %s(%d) %s\n", (pVM)->fGlobalForcedActions, #fFlag, __FILE__, __LINE__, __FUNCTION__); \
} while (0)
#endif
/** @def VMCPU_FF_CLEAR
* Clears a force action flag for the given VCPU.
*
* @param pVCpu VMCPU Handle.
* @param fFlag The flag to clear.
*/
/** @def VM_FF_ISSET
* Checks if a force action flag is set.
*
* @param pVM VM Handle.
* @param fFlag The flag to check.
*/
/** @def VMCPU_FF_ISSET
* Checks if a force action flag is set for the given VCPU.
*
* @param pVCpu VMCPU Handle.
* @param fFlag The flag to check.
*/
/** @def VM_FF_ISPENDING
* Checks if one or more force action in the specified set is pending.
*
* @param pVM VM Handle.
* @param fFlags The flags to check for.
*/
/** @def VM_FF_TESTANDCLEAR
* Checks if one (!) force action in the specified set is pending and clears it atomically
*
* @returns true if the bit was set.
* @returns false if the bit was clear.
* @param pVM VM Handle.
* @param iBit Bit position to check and clear
*/
#define VM_FF_TESTANDCLEAR(pVM, iBit) (ASMAtomicBitTestAndClear(&(pVM)->fGlobalForcedActions, iBit##_BIT))
/** @def VMCPU_FF_TESTANDCLEAR
* Checks if one (!) force action in the specified set is pending and clears it atomically
*
* @returns true if the bit was set.
* @returns false if the bit was clear.
* @param pVCpu VMCPU Handle.
* @param iBit Bit position to check and clear
*/
#define VMCPU_FF_TESTANDCLEAR(pVCpu, iBit) (ASMAtomicBitTestAndClear(&(pVCpu)->fLocalForcedActions, iBit##_BIT))
/** @def VMCPU_FF_ISPENDING
* Checks if one or more force action in the specified set is pending for the given VCPU.
*
* @param pVCpu VMCPU Handle.
* @param fFlags The flags to check for.
*/
/** @def VM_FF_ISPENDING
* Checks if one or more force action in the specified set is pending while one
* or more other ones are not.
*
* @param pVM VM Handle.
* @param fFlags The flags to check for.
* @param fExcpt The flags that should not be set.
*/
#define VM_FF_IS_PENDING_EXCEPT(pVM, fFlags, fExcpt) ( ((pVM)->fGlobalForcedActions & (fFlags)) && !((pVM)->fGlobalForcedActions & (fExcpt)) )
/** @def VMCPU_FF_IS_PENDING_EXCEPT
* Checks if one or more force action in the specified set is pending for the given
* VCPU while one or more other ones are not.
*
* @param pVCpu VMCPU Handle.
* @param fFlags The flags to check for.
* @param fExcpt The flags that should not be set.
*/
#define VMCPU_FF_IS_PENDING_EXCEPT(pVCpu, fFlags, fExcpt) ( ((pVCpu)->fLocalForcedActions & (fFlags)) && !((pVCpu)->fLocalForcedActions & (fExcpt)) )
/** @def VM_IS_EMT
* Checks if the current thread is the emulation thread (EMT).
*
* @remark The ring-0 variation will need attention if we expand the ring-0
* code to let threads other than EMT mess around with the VM.
*/
#ifdef IN_RC
#else
#endif
/** @def VMCPU_IS_EMT
* Checks if the current thread is the emulation thread (EMT) for the specified
* virtual CPU.
*/
#ifdef IN_RC
# define VMCPU_IS_EMT(pVCpu) true
#else
#endif
/** @def VM_ASSERT_EMT
* Asserts that the current thread IS the emulation thread (EMT).
*/
#ifdef IN_RC
#else
# define VM_ASSERT_EMT(pVM) \
("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd\n", RTThreadNativeSelf(), VMR3GetVMCPUNativeThread(pVM)))
#endif
/** @def VMCPU_ASSERT_EMT
* Asserts that the current thread IS the emulation thread (EMT) of the
* specified virtual CPU.
*/
#ifdef IN_RC
#else
# define VMCPU_ASSERT_EMT(pVCpu) \
("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%#x\n", \
#endif
/** @def VM_ASSERT_EMT_RETURN
* Asserts that the current thread IS the emulation thread (EMT) and returns if it isn't.
*/
#ifdef IN_RC
#else
("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd\n", RTThreadNativeSelf(), VMR3GetVMCPUNativeThread(pVM)), \
(rc))
#endif
/** @def VMCPU_ASSERT_EMT_RETURN
* Asserts that the current thread IS the emulation thread (EMT) and returns if it isn't.
*/
#ifdef IN_RC
#else
("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd idCpu=%#x\n", \
(rc))
#endif
/** @def VM_ASSERT_EMT0
* Asserts that the current thread IS emulation thread \#0 (EMT0).
*/
/** @def VM_ASSERT_EMT0_RETURN
* Asserts that the current thread IS emulation thread \#0 (EMT0) and returns if
* it isn't.
*/
/**
* Asserts that the current thread is NOT the emulation thread.
*/
#define VM_ASSERT_OTHER_THREAD(pVM) \
/** @def VM_ASSERT_STATE_RETURN
* Asserts a certain VM state.
*/
/** @def VM_ASSERT_STATE_RETURN
* Asserts a certain VM state and returns if it doesn't match.
*/
(rc))
/** @def VM_ASSERT_VALID_EXT_RETURN
* Asserts a the VM handle is valid for external access, i.e. not being
* destroy or terminated.
*/
(rc))
/** @def VMCPU_ASSERT_VALID_EXT_RETURN
* Asserts a the VMCPU handle is valid for external access, i.e. not being
* destroy or terminated.
*/
("pVCpu=%p pVM=%p state %s\n", (pVCpu), RT_VALID_ALIGNED_PTR(pVCpu, 64) ? (pVCpu)->CTX_SUFF(pVM) : NULL, \
(rc))
/** This is the VM structure.
*
* It contains (nearly?) all the VM data which have to be available in all
* contexts. Even if it contains all the data the idea is to use APIs not
* to modify all the members all around the place. Therefore we make use of
* unions to hide everything which isn't local to the current source module.
* This means we'll have to pay a little bit of attention when adding new
* members to structures in the unions and make sure to keep the padding sizes
* up to date.
*
* Run tstVMStructSize after update!
*/
typedef struct VM
{
/** The state of the VM.
* This field is read only to everyone except the VM and EM. */
VMSTATE volatile enmVMState;
/** Forced action flags.
* See the VM_FF_* \#defines. Updated atomically.
*/
volatile uint32_t fGlobalForcedActions;
/** Pointer to the array of page descriptors for the VM structure allocation. */
/** Session handle. For use when calling SUPR0 APIs. */
/** Pointer to the ring-3 VM structure. */
/** Ring-3 Host Context VM Pointer. */
/** Ring-0 Host Context VM Pointer. */
/** Raw-mode Context VM Pointer. */
/** The GVM VM handle. Only the GVM should modify this field. */
/** Number of virtual CPUs. */
/** Size of the VM structure including the VMCPU array. */
/** Offset to the VMCPU array starting from beginning of this structure. */
/** Reserved; alignment. */
/** @name Public VMM Switcher APIs
* @{ */
/**
* Assembly switch entry point for returning to host context.
* This function will clean up the stack frame.
*
* @param eax The return code, register.
* @param Ctx The guest core context.
* @remark Assume interrupts disabled.
*/
/**
* Assembly switch entry point for returning to host context.
*
* This is an alternative entry point which we'll be using when the we have the
* hypervisor context and need to save that before going to the host.
*
* This is typically useful when abandoning the hypervisor because of a trap
* and want the trap state to be saved.
*
* @param eax The return code, register.
* @param ecx Pointer to the hypervisor core context, register.
* @remark Assume interrupts disabled.
*/
/**
* Assembly switch entry point for returning to host context.
*
* This is an alternative to the two *Ctx APIs and implies that the context has already
* been saved, or that it's just a brief return to HC and that the caller intends to resume
* whatever it is doing upon 'return' from this call.
*
* @param eax The return code, register.
* @remark Assume interrupts disabled.
*/
/** @} */
/** @name Various VM data owned by VM.
* @{ */
/** The native handle of ThreadEMT. Getting the native handle
* is generally faster than getting the IPRT one (except on OS/2 :-). */
/** @} */
/** @name Various items that are frequently accessed.
* @{ */
/** Raw ring-3 indicator. */
bool fRawR3Enabled;
/** Raw ring-0 indicator. */
bool fRawR0Enabled;
/** PATM enabled flag.
* This is placed here for performance reasons. */
bool fPATMEnabled;
/** CSAM enabled flag.
* This is placed here for performance reasons. */
bool fCSAMEnabled;
/** Hardware VM support is available and enabled.
* This is placed here for performance reasons. */
bool fHWACCMEnabled;
/** Hardware VM support is required and non-optional.
* This is initialized together with the rest of the VM structure. */
bool fHwVirtExtForced;
/** PARAV enabled flag. */
bool fPARAVEnabled;
/** Large page enabled flag. */
bool fUseLargePages;
/** @} */
/* padding to make gnuc put the StatQemuToGC where msc does. */
#if HC_ARCH_BITS == 32
#endif
/** Profiling the total time from Qemu to GC. */
/** Profiling the total time from GC to Qemu. */
/** Profiling the total time spent in GC. */
/** Profiling the total time spent not in Qemu. */
/** Profiling the VMMSwitcher code for going to GC. */
/** Profiling the VMMSwitcher code for going to HC. */
/** Padding - the unions must be aligned on a 64 bytes boundrary and the unions
* must start at the same offset on both 64-bit and 32-bit hosts. */
/** CPUM part. */
union
{
#ifdef ___CPUMInternal_h
struct CPUM s;
#endif
} cpum;
/** VMM part. */
union
{
#ifdef ___VMMInternal_h
struct VMM s;
#endif
} vmm;
/** PGM part. */
union
{
#ifdef ___PGMInternal_h
struct PGM s;
#endif
} pgm;
/** HWACCM part. */
union
{
#ifdef ___HWACCMInternal_h
struct HWACCM s;
#endif
} hwaccm;
/** TRPM part. */
union
{
#ifdef ___TRPMInternal_h
struct TRPM s;
#endif
} trpm;
/** SELM part. */
union
{
#ifdef ___SELMInternal_h
struct SELM s;
#endif
} selm;
/** MM part. */
union
{
#ifdef ___MMInternal_h
struct MM s;
#endif
} mm;
/** PDM part. */
union
{
#ifdef ___PDMInternal_h
struct PDM s;
#endif
} pdm;
/** IOM part. */
union
{
#ifdef ___IOMInternal_h
struct IOM s;
#endif
} iom;
/** PATM part. */
union
{
#ifdef ___PATMInternal_h
struct PATM s;
#endif
} patm;
/** CSAM part. */
union
{
#ifdef ___CSAMInternal_h
struct CSAM s;
#endif
} csam;
/** EM part. */
union
{
#ifdef ___EMInternal_h
struct EM s;
#endif
} em;
/** TM part. */
union
{
#ifdef ___TMInternal_h
struct TM s;
#endif
} tm;
/** DBGF part. */
union
{
#ifdef ___DBGFInternal_h
struct DBGF s;
#endif
} dbgf;
/** SSM part. */
union
{
#ifdef ___SSMInternal_h
struct SSM s;
#endif
} ssm;
/** REM part. */
union
{
#ifdef ___REMInternal_h
struct REM s;
#endif
} rem;
/* ---- begin small stuff ---- */
/** VM part. */
union
{
#ifdef ___VMInternal_h
struct VMINT s;
#endif
} vm;
/** CFGM part. */
union
{
#ifdef ___CFGMInternal_h
struct CFGM s;
#endif
} cfgm;
/** PARAV part. */
union
{
#ifdef ___PARAVInternal_h
struct PARAV s;
#endif
} parav;
/** Padding for aligning the cpu array on a page boundrary. */
/* ---- end small stuff ---- */
/** VMCPU array for the configured number of virtual CPUs.
* Must be aligned on a page boundrary for TLB hit reasons as well as
* alignment of VMCPU members. */
} VM;
#ifdef IN_RC
/** The VM structure.
* This is imported from the VMMGCBuiltin module, i.e. it's a one
* of those magic globals which we should avoid using.
*/
#endif
/** @} */
#endif