VMEmt.cpp revision 9ecc480e5268a3685ec1b05c0dad9b59adf19575
/* $Id$ */
/** @file
* VM - Virtual Machine, The Emulation Thread.
*/
/*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_VM
#include "VMInternal.h"
#include <iprt/asm-math.h>
#include <iprt/semaphore.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* The emulation thread main function.
*
* @returns Thread exit code.
* @param ThreadSelf The handle to the executing thread.
* @param pvArgs Pointer to the user mode per-VCpu structure (UVMPCU).
*/
{
}
/**
* The emulation thread main function, with Virtual CPU ID for debugging.
*
* @returns Thread exit code.
* @param ThreadSelf The handle to the executing thread.
* @param pUVCpu Pointer to the user mode per-VCpu structure.
* @param idCpu The virtual CPU ID, for backtrace purposes.
*/
{
int rc;
("Invalid arguments to the emulation thread!\n"));
/*
* The request loop.
*/
rc = VINF_SUCCESS;
Log(("vmR3EmulationThread: Emulation thread starting the days work... Thread=%#x pUVM=%p\n", ThreadSelf, pUVM));
for (;;)
{
/*
* During early init there is no pVM, so make a special path
* for that to keep things clearly separate.
*/
{
/*
* Check for termination first.
*/
{
break;
}
/*
* Only the first VCPU may initialize the VM during early init
* and must therefore service all VMCPUID_ANY requests.
* See also VMR3Create
*/
{
/*
* Service execute in any EMT request.
*/
Log(("vmR3EmulationThread: Req rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), pUVM->pVM ? VMR3GetStateName(pUVM->pVM->enmVMState) : "CREATING"));
}
{
/*
* Service execute in specific EMT request.
*/
Log(("vmR3EmulationThread: Req (cpu=%u) rc=%Rrc, VM state %s -> %s\n", pUVCpu->idCpu, rc, VMR3GetStateName(enmBefore), pUVM->pVM ? VMR3GetStateName(pUVM->pVM->enmVMState) : "CREATING"));
}
else
{
/*
* Nothing important is pending, so wait for something.
*/
if (RT_FAILURE(rc))
{
break;
}
}
}
else
{
/*
* Pending requests which needs servicing?
*
* We check for state changes in addition to status codes when
* servicing requests. (Look after the ifs.)
*/
{
break;
}
{
Log(("vmR3EmulationThread: Rendezvous rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
}
{
/*
* Service execute in any EMT request.
*/
Log(("vmR3EmulationThread: Req rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
}
{
/*
* Service execute in specific EMT request.
*/
Log(("vmR3EmulationThread: Req (cpu=%u) rc=%Rrc, VM state %s -> %s\n", pUVCpu->idCpu, rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
}
{
/*
* Service the debugger request.
*/
Log(("vmR3EmulationThread: Dbg rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
}
{
/*
* Service a delayed reset request.
*/
Log(("vmR3EmulationThread: Reset rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
}
else
{
/*
* Nothing important is pending, so wait for something.
*/
if (RT_FAILURE(rc))
{
break;
}
}
/*
* Check for termination requests, these have extremely high priority.
*/
if ( rc == VINF_EM_TERMINATE
break;
}
/*
* Some requests (both VMR3Req* and the DBGF) can potentially resume
* or start the VM, in that case we'll get a change in VM status
* indicating that we're now running.
*/
if ( RT_SUCCESS(rc)
{
{
}
}
} /* forever */
/*
* Cleanup and exit.
*/
Log(("vmR3EmulationThread: Terminating emulation thread! Thread=%#x pUVM=%p rc=%Rrc enmBefore=%d enmVMState=%d\n",
if ( idCpu == 0
{
/** @todo SMP: This isn't 100% safe. We should wait for the other
* threads to finish before destroy the VM. */
}
Log(("vmR3EmulationThread: EMT is terminated.\n"));
return rc;
}
/**
* Gets the name of a halt method.
*
* @returns Pointer to a read only string.
* @param enmMethod The method.
*/
{
switch (enmMethod)
{
case VMHALTMETHOD_BOOTSTRAP: return "bootstrap";
case VMHALTMETHOD_DEFAULT: return "default";
case VMHALTMETHOD_OLD: return "old";
case VMHALTMETHOD_1: return "method1";
//case VMHALTMETHOD_2: return "method2";
case VMHALTMETHOD_GLOBAL_1: return "global1";
default: return "unknown";
}
}
/**
* Signal a fatal wait error.
*
* @returns Fatal error code to be propagated up the call stack.
* @param pUVCpu The user mode per CPU structure of the calling
* EMT.
* @param pszFmt The error format with a single %Rrc in it.
* @param rcFmt The status code to format.
*/
{
/** @todo This is wrong ... raise a fatal error / guru meditation
* instead. */
return VERR_INTERNAL_ERROR;
}
/**
* The old halt loop.
*/
static DECLCALLBACK(int) vmR3HaltOldDoHalt(PUVMCPU pUVCpu, const uint32_t fMask, uint64_t /* u64Now*/)
{
/*
* Halt loop.
*/
int rc = VINF_SUCCESS;
//unsigned cLoops = 0;
for (;;)
{
/*
* Work the timers and check if we can exit.
* The poll call gives us the ticks left to the next event in
* addition to perhaps set an FF.
*/
break;
break;
/*
* Wait for a while. Someone will wake us up or interrupt the call if
* anything needs our attention.
*/
if (u64NanoTS < 50000)
{
//RTLogPrintf("u64NanoTS=%RI64 cLoops=%d spin\n", u64NanoTS, cLoops++);
/* spin */;
}
else
{
//uint64_t u64Start = RTTimeNanoTS();
{
//RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield", u64NanoTS, cLoops++);
RTThreadYield(); /* this is the best we can do here */
}
else if (u64NanoTS < 2000000)
{
//RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms", u64NanoTS, cLoops++);
}
else
{
//RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms", u64NanoTS, cLoops++, (uint32_t)RT_MIN((u64NanoTS - 500000) / 1000000, 15));
}
//uint64_t u64Slept = RTTimeNanoTS() - u64Start;
//RTLogPrintf(" -> rc=%Rrc in %RU64 ns / %RI64 ns delta\n", rc, u64Slept, u64NanoTS - u64Slept);
}
if (rc == VERR_TIMEOUT)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc))
{
break;
}
}
return rc;
}
/**
* Initialize the configuration of halt method 1 & 2.
*
* @return VBox status code. Failure on invalid CFGM data.
* @param pVM The VM handle.
*/
{
/*
* The defaults.
*/
#if 1 /* DEBUGGING STUFF - REMOVE LATER */
#else
#endif
/*
* Query overrides.
*
* I don't have time to bother with niceities such as invalid value checks
* here right now. sorry.
*/
if (pCfg)
{
LogRel(("HaltedMethod1 config: %d/%d/%d/%d/%d\n",
}
return VINF_SUCCESS;
}
/**
* Initialize halt method 1.
*
* @return VBox status code.
* @param pUVM Pointer to the user mode VM structure.
*/
{
return vmR3HaltMethod12ReadConfigU(pUVM);
}
/**
* Method 1 - Block whenever possible, and when lagging behind
* switch to spinning for 10-30ms with occational blocking until
* the lag has been eliminated.
*/
{
/*
* To simplify things, we decide up-front whether we should switch to spinning or
* and that it will generate interrupts or other events that will cause us to exit
* the halt loop.
*/
bool fBlockOnce = false;
bool fSpinning = false;
if (u32CatchUpPct /* non-zero if catching up */)
{
{
if (fSpinning)
{
}
else
{
//RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
}
}
else
{
if (fSpinning)
}
}
{
//RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pUVCpu->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
}
/*
* Halt loop.
*/
int rc = VINF_SUCCESS;
unsigned cLoops = 0;
for (;; cLoops++)
{
/*
* Work the timers and check if we can exit.
*/
break;
/*
* Estimate time left to the next event.
*/
break;
/*
* Block if we're not spinning and the interval isn't all that small.
*/
if ( ( !fSpinning
|| fBlockOnce)
#if 1 /* DEBUGGING STUFF - REMOVE LATER */
#else
#endif
{
cMilliSecs = 1;
else
//RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
if (rc == VERR_TIMEOUT)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc))
{
break;
}
/*
* Calc the statistics.
* Update averages every 16th time, and flush parts of the history every 64th time.
*/
{
pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong / pUVCpu->vm.s.Halt.Method12.cBlocks;
{
pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLong = pUVCpu->vm.s.Halt.Method12.cNSBlockedTooLongAvg * 0x40;
}
}
//RTLogRelPrintf(" -> %7RU64 ns / %7RI64 ns delta%s\n", Elapsed, Elapsed - u64NanoTS, fBlockOnce ? " (block once)" : "");
/*
* Clear the block once flag if we actually blocked.
*/
if ( fBlockOnce
fBlockOnce = false;
}
}
//if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
return rc;
}
/**
* Initialize the global 1 halt method.
*
* @return VBox status code.
* @param pUVM Pointer to the user mode VM structure.
*/
{
return VINF_SUCCESS;
}
/**
* The global 1 halt method - Block in GMM (ring-0) and let it
* try take care of the global scheduling of EMT threads.
*/
{
/*
* Halt loop.
*/
int rc = VINF_SUCCESS;
unsigned cLoops = 0;
for (;; cLoops++)
{
/*
* Work the timers and check if we can exit.
*/
break;
/*
* Estimate time left to the next event.
*/
break;
/*
* Block if we're not spinning and the interval isn't all that small.
*/
{
break;
//RTLogRelPrintf("u64NanoTS=%RI64 cLoops=%3d sleep %02dms (%7RU64) ", u64NanoTS, cLoops, cMilliSecs, u64NanoTS);
if (rc == VERR_INTERRUPTED)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc))
{
break;
}
}
/*
* When spinning call upon the GVMM and do some wakups once
* in a while, it's not like we're actually busy or anything.
*/
else if (!(cLoops & 0x1fff))
{
rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_POLL, false /* don't yield */, NULL);
}
}
//if (fSpinning) RTLogRelPrintf("spun for %RU64 ns %u loops; lag=%RU64 pct=%d\n", RTTimeNanoTS() - u64Now, cLoops, TMVirtualSyncGetLag(pVM), u32CatchUpPct);
return rc;
}
/**
* The global 1 halt method - VMR3Wait() worker.
*
* @returns VBox status code.
* @param pUVCpu Pointer to the user mode VMCPU structure.
*/
{
int rc = VINF_SUCCESS;
for (;;)
{
/*
* Check Relevant FFs.
*/
break;
/*
* Wait for a while. Someone will wake us up or interrupt the call if
* anything needs our attention.
*/
rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, VMMR0_DO_GVMM_SCHED_HALT, RTTimeNanoTS() + 1000000000 /* +1s */, NULL);
if (rc == VERR_INTERRUPTED)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc))
{
break;
}
}
return rc;
}
/**
* The global 1 halt method - VMR3NotifyFF() worker.
*
* @param pUVCpu Pointer to the user mode VMCPU structure.
* @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
*/
{
{
}
else if ( ( (fFlags & VMNOTIFYFF_FLAGS_POKE)
|| !(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
{
if (enmState == VMCPUSTATE_STARTED_EXEC)
{
if (fFlags & VMNOTIFYFF_FLAGS_POKE)
{
}
}
else if (enmState == VMCPUSTATE_STARTED_EXEC_REM)
{
if (!(fFlags & VMNOTIFYFF_FLAGS_DONE_REM))
}
}
}
/**
* Bootstrap VMR3Wait() worker.
*
* @returns VBox status code.
* @param pUVMCPU Pointer to the user mode VMCPU structure.
*/
{
int rc = VINF_SUCCESS;
for (;;)
{
/*
* Check Relevant FFs.
*/
break;
break;
)
)
break;
break;
/*
* Wait for a while. Someone will wake us up or interrupt the call if
* anything needs our attention.
*/
if (rc == VERR_TIMEOUT)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc))
{
break;
}
}
return rc;
}
/**
* Bootstrap VMR3NotifyFF() worker.
*
* @param pUVCpu Pointer to the user mode VMCPU structure.
* @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
*/
{
{
}
}
/**
* Default VMR3Wait() worker.
*
* @returns VBox status code.
* @param pUVMCPU Pointer to the user mode VMCPU structure.
*/
{
int rc = VINF_SUCCESS;
for (;;)
{
/*
* Check Relevant FFs.
*/
break;
/*
* Wait for a while. Someone will wake us up or interrupt the call if
* anything needs our attention.
*/
if (rc == VERR_TIMEOUT)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc))
{
break;
}
}
return rc;
}
/**
* Default VMR3NotifyFF() worker.
*
* @param pUVCpu Pointer to the user mode VMCPU structure.
* @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
*/
{
{
}
else if ( !(fFlags & VMNOTIFYFF_FLAGS_DONE_REM)
}
/**
* Array with halt method descriptors.
* VMINT::iHaltMethod contains an index into this array.
*/
static const struct VMHALTMETHODDESC
{
/** The halt method id. */
/** The init function for loading config and initialize variables. */
/** The term function. */
/** The VMR3WaitHaltedU function. */
/** The VMR3WaitU function. */
/** The VMR3NotifyCpuFFU function. */
/** The VMR3NotifyGlobalFFU function. */
} g_aHaltMethods[] =
{
{ VMHALTMETHOD_1, vmR3HaltMethod1Init, NULL, vmR3HaltMethod1Halt, vmR3DefaultWait, vmR3DefaultNotifyCpuFF, NULL },
{ VMHALTMETHOD_GLOBAL_1, vmR3HaltGlobal1Init, NULL, vmR3HaltGlobal1Halt, vmR3HaltGlobal1Wait, vmR3HaltGlobal1NotifyCpuFF, NULL },
};
/**
* Notify the emulation thread (EMT) about pending Forced Action (FF).
*
* This function is called by thread other than EMT to make
* sure EMT wakes up and promptly service an FF request.
*
* @param pUVM Pointer to the user mode VM structure.
* @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
*/
{
LogFlow(("VMR3NotifyGlobalFFU:\n"));
else
}
/**
* Notify the emulation thread (EMT) about pending Forced Action (FF).
*
* This function is called by thread other than EMT to make
* sure EMT wakes up and promptly service an FF request.
*
* @param pUVM Pointer to the user mode VM structure.
* @param fFlags Notification flags, VMNOTIFYFF_FLAGS_*.
*/
{
LogFlow(("VMR3NotifyCpuFFU:\n"));
}
/**
* Halted VM Wait.
* Any external event will unblock the thread.
*
* @returns VINF_SUCCESS unless a fatal error occurred. In the latter
* case an appropriate status code is returned.
* @param pVM VM handle.
* @param pVCpu VMCPU handle.
* @param fIgnoreInterrupts If set the VM_FF_INTERRUPT flags is ignored.
* @thread The emulation thread.
*/
{
/*
* Check Relevant FFs.
*/
{
LogFlow(("VMR3WaitHalted: returns VINF_SUCCESS (FF %#x FFCPU %#x)\n", pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions));
return VINF_SUCCESS;
}
/*
* The yielder is suspended while we're halting, while TM might have clock(s) running
* only at certain times and need to be notified..
*/
/*
* Record halt averages for the last second.
*/
if (off > 1000000000)
{
{
}
else
{
pUVCpu->vm.s.HaltFrequency = ASMMultU64ByU32DivByU32(pUVCpu->vm.s.cHalts, 1000000000, (uint32_t)off);
}
}
/*
* Do the halt.
*/
/*
* Notify TM and resume the yielder
*/
return rc;
}
/**
* Suspended VM Wait.
* Only a handful of forced actions will cause the function to
* return to the caller.
*
* @returns VINF_SUCCESS unless a fatal error occurred. In the latter
* case an appropriate status code is returned.
* @param pUVCpu Pointer to the user mode VMCPU structure.
* @thread The emulation thread.
*/
{
LogFlow(("VMR3WaitU:\n"));
/*
* Check Relevant FFs.
*/
if ( pVM
)
)
{
return VINF_SUCCESS;
}
/*
* Do waiting according to the halt method (so VMR3NotifyFF
* doesn't have to special case anything).
*/
return rc;
}
/**
* Interface that PDMR3Suspend, PDMR3PowerOff and PDMR3Reset uses when they wait
* for the handling of asynchronous notifications to complete.
*
* @returns VINF_SUCCESS unless a fatal error occurred. In the latter
* case an appropriate status code is returned.
* @param pUVCpu Pointer to the user mode VMCPU structure.
* @thread The emulation thread.
*/
{
LogFlow(("VMR3AsyncPdmNotificationWaitU:\n"));
}
/**
* Interface that PDM the helper asynchronous notification completed methods
* uses for EMT0 when it is waiting inside VMR3AsyncPdmNotificationWaitU().
*
* @param pUVM Pointer to the user mode VM structure.
*/
{
LogFlow(("VMR3AsyncPdmNotificationWakeupU:\n"));
}
/**
* Rendezvous callback that will be called once.
*
* @returns VBox strict status code.
* @param pVM VM handle.
* @param pVCpu The VMCPU handle for the calling EMT.
* @param pvUser The new g_aHaltMethods index.
*/
{
/*
* Terminate the old one.
*/
{
}
/* Assert that the failure fallback is where we expect. */
/*
* Init the new one.
*/
int rc = VINF_SUCCESS;
if (g_aHaltMethods[i].pfnInit)
{
if (RT_FAILURE(rc))
{
/* Fall back on the bootstrap method. This requires no
i = 0;
}
}
/*
* Commit it.
*/
return rc;
}
/**
* Changes the halt method.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
* @param enmHaltMethod The new halt method.
* @thread EMT.
*/
{
AssertReturn(enmHaltMethod > VMHALTMETHOD_INVALID && enmHaltMethod < VMHALTMETHOD_END, VERR_INVALID_PARAMETER);
/*
* Resolve default (can be overridden in the configuration).
*/
if (enmHaltMethod == VMHALTMETHOD_DEFAULT)
{
if (RT_SUCCESS(rc))
{
return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Invalid VM/HaltMethod value %d"), enmHaltMethod);
}
else
//enmHaltMethod = VMHALTMETHOD_1;
//enmHaltMethod = VMHALTMETHOD_OLD;
}
/*
* Find the descriptor.
*/
unsigned i = 0;
while ( i < RT_ELEMENTS(g_aHaltMethods)
i++;
/*
* This needs to be done while the other EMTs are not sleeping or otherwise messing around.
*/
return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, vmR3SetHaltMethodCallback, (void *)(uintptr_t)i);
}