VMEmt.cpp revision 7785166d7603b71b07349bb0fe57e2df14caa3c6
/* $Id$ */
/** @file
* VM - Virtual Machine, The Emulation Thread.
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* 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) */
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. */
}
/**
* 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"));
{
}
else if (!fNotifiedREM)
}
/**
* 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 CPU TSC is running while halted,
* and the yielder is suspended.
*/
// TMCpuTickResume(pVM);
/*
* Halt loop.
*/
int rc = VINF_SUCCESS;
//unsigned cLoops = 0;
for (;;)
{
#ifdef VBOX_HIGH_RES_TIMERS_HACK
/*
* 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
{
{
//RTLogPrintf("u64NanoTS=%RI64 cLoops=%d yield\n", u64NanoTS, cLoops++);
RTThreadYield(); /* this is the best we can do here */
}
else if (u64NanoTS < 2000000)
{
//RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep 1ms\n", u64NanoTS, cLoops++);
}
else
{
//RTLogPrintf("u64NanoTS=%RI64 cLoops=%d sleep %dms\n", u64NanoTS, cLoops++, (uint32_t)RT_MIN(u64NanoTS / 1000000, 15));
}
}
#else
/*
* We have to check if we can exit, run timers, and then recheck.
*/
/** @todo
* The other thing we have to check is how long it is till the next timer
* can be serviced and not wait any longer than that.
*/
break;
break;
/* hacking */
break;
/*
* Wait for a while. Someone will wake us up or interrupt the call if
* anything needs our attention.
*/
#endif
if (rc == VERR_TIMEOUT)
rc = VINF_SUCCESS;
else if (VBOX_FAILURE(rc))
{
break;
}
}
/*
*
* Pause the TSC, it's restarted when we start executing,
* and resume the yielder.
*/
// TMCpuTickPause(pVM);
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;
}
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;
}