VMEmt.cpp revision a40c05ca69e15a5efdd0796ef52e26ec0b1bc4d2
/* $Id$ */
/** @file
* VM - Virtual Machine, The Emulation Thread.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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/semaphore.h>
/**
* The emulation thread.
*
* @returns Thread exit code.
* @param ThreadSelf The handle to the executing thread.
* @param pvArgs Pointer to a VMEMULATIONTHREADARGS structure.
*/
{
/*
* Init the native thread member.
*/
/*
* The request loop.
*/
int rc;
Log(("vmR3EmulationThread: Emulation thread starting the days work... Thread=%#x pVM=%p\n", ThreadSelf, pVM));
for (;;)
{
/* Requested to exit the EMT thread out of sync? (currently only VMR3WaitForResume) */
{
rc = VINF_SUCCESS;
break;
}
/*
* Pending requests which needs servicing?
*
* We check for state changes in addition to status codes when
* servicing requests. (Look after the ifs.)
*/
{
break;
}
{
/*
* Service execute in EMT request.
*/
}
{
/*
* Service the debugger request.
*/
}
{
/*
* Service a delay reset request.
*/
}
else
{
/*
* Nothing important is pending, so wait for something.
*/
if (VBOX_FAILURE(rc))
break;
}
/*
* Check for termination requests, these are 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 ( VBOX_SUCCESS(rc)
{
}
} /* forever */
/*
* Exiting.
*/
Log(("vmR3EmulationThread: Terminating emulation thread! Thread=%#x pVM=%p rc=%Vrc enmBefore=%d enmVMState=%d\n",
{
Log(("vmR3EmulationThread: executing delayed Destroy\n"));
Log(("vmR3EmulationThread: EMT is terminated.\n"));
}
else
{
/* we don't reset ThreadEMT here because it's used in waiting. */
}
return rc;
}
/**
* Wait for VM to be resumed. Handle events like vmR3EmulationThread does.
* In case the VM is stopped, clean up and long jump to the main EMT loop.
*
* @returns VINF_SUCCESS or doesn't return
* @param pVM VM handle.
*/
{
/*
* The request loop.
*/
int rc;
for (;;)
{
/*
* Pending requests which needs servicing?
*
* We check for state changes in addition to status codes when
* servicing requests. (Look after the ifs.)
*/
{
break;
}
{
/*
* Service execute in EMT request.
*/
}
{
/*
* Service the debugger request.
*/
}
{
/*
* Service a delay reset request.
*/
}
else
{
/*
* Nothing important is pending, so wait for something.
*/
if (VBOX_FAILURE(rc))
break;
}
/*
* Check for termination requests, these are 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 ( VBOX_SUCCESS(rc)
{
/* Only valid exit reason. */
return VINF_SUCCESS;
}
} /* forever */
/* Return to the main loop in vmR3EmulationThread, which will clean up for us. */
}
/**
* Gets the name of a halt method.
*
* @returns Pointer to a read only string.
* @param enmMethod The method.
*/
{
switch (enmMethod)
{
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";
}
}
/**
* The old halt loop.
*/
{
/*
* 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=%Vrc in %RU64 ns / %RI64 ns delta\n", rc, u64Slept, u64NanoTS - u64Slept);
}
if (rc == VERR_TIMEOUT)
rc = VINF_SUCCESS;
else if (VBOX_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 pVM The VM handle.
*/
{
return vmR3HaltMethod12ReadConfig(pVM);
}
/**
* 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 - pVM->vm.s.Halt.Method12.u64StartSpinTS) / 1000000);
}
}
else
{
if (fSpinning)
}
}
{
//RTLogRelPrintf("Stopped spinning (%u ms)\n", (u64Now - pVM->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 (VBOX_FAILURE(rc))
{
break;
}
/*
* Calc the statistics.
* Update averages every 16th time, and flush parts of the history every 64th time.
*/
{
pVM->vm.s.Halt.Method12.cNSBlockedTooLongAvg = pVM->vm.s.Halt.Method12.cNSBlockedTooLong / pVM->vm.s.Halt.Method12.cBlocks;
{
}
}
//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 pVM The VM handle.
*/
{
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 (VBOX_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))
{
}
}
//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 pVM The VM handle.
*/
{
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 = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GVMM_SCHED_HALT, RTTimeNanoTS() + 1000000000 /* +1s */, NULL);
if (rc == VERR_INTERRUPTED)
rc = VINF_SUCCESS;
else if (VBOX_FAILURE(rc))
{
break;
}
}
return rc;
}
/**
* The global 1 halt method - VMR3NotifyFF() worker.
*
* @param pVM The VM handle.
* @param fNotifiedREM Se VMR3NotifyFF().
*/
{
{
}
else if (!fNotifiedREM)
}
/**
* Default VMR3Wait() worker.
*
* @returns VBox status code.
* @param pVM The VM handle.
*/
{
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 (VBOX_FAILURE(rc))
{
break;
}
}
return rc;
}
/**
* Default VMR3NotifyFF() worker.
*
* @param pVM The VM handle.
* @param fNotifiedREM Se VMR3NotifyFF().
*/
{
{
}
else if (!fNotifiedREM)
}
/**
* 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 halt function. */
/** The wait function. */
/** The notifyFF function. */
} g_aHaltMethods[] =
{
{ VMHALTMETHOD_1, vmR3HaltMethod1Init, NULL, vmR3HaltMethod1Halt, vmR3DefaultWait, vmR3DefaultNotifyFF },
//{ VMHALTMETHOD_2, vmR3HaltMethod2Init, vmR3HaltMethod2Term, vmR3HaltMethod2DoHalt, vmR3HaltMethod2Wait, vmR3HaltMethod2NotifyFF },
{ VMHALTMETHOD_GLOBAL_1,vmR3HaltGlobal1Init, NULL, vmR3HaltGlobal1Halt, vmR3HaltGlobal1Wait, vmR3HaltGlobal1NotifyFF },
};
/**
* 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 pVM VM handle.
* @param fNotifiedREM Set if REM have already been notified. If clear the
* generic REMR3NotifyFF() method is called.
*/
{
LogFlow(("VMR3NotifyFF:\n"));
}
/**
* Halted VM Wait.
* Any external event will unblock the thread.
*
* @returns VINF_SUCCESS unless a fatal error occured. In the latter
* case an appropriate status code is returned.
* @param pVM VM handle.
* @param fIgnoreInterrupts If set the VM_FF_INTERRUPT flags is ignored.
* @thread The emulation thread.
*/
{
/*
* Check Relevant FFs.
*/
{
return VINF_SUCCESS;
}
/*
* The yielder is suspended while we're halting.
*/
/*
* Record halt averages for the last second.
*/
if (off > 1000000000)
{
{
}
else
{
}
}
/*
* Do the halt.
*/
/*
* Resume the yielder and tell the world we're not blocking.
*/
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 occured. In the latter
* case an appropriate status code is returned.
* @param pVM VM handle.
* @thread The emulation thread.
*/
{
LogFlow(("VMR3Wait:\n"));
/*
* Check Relevant FFs.
*/
{
return VINF_SUCCESS;
}
/*
* Do waiting according to the halt method (so VMR3NotifyFF
* doesn't have to special case anything).
*/
return rc;
}
/**
* Changes the halt method.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @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 (VBOX_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++;
/*
* Terminate the old one.
*/
{
}
/*
* Init the new one.
*/
if (g_aHaltMethods[i].pfnInit)
{
}
return VINF_SUCCESS;
}