TMAll.cpp revision c0eb823dba9ba8afc2aa44bc80e46c7212690d72
/* $Id$ */
/** @file
* TM - Timeout Manager, all contexts.
*/
/*
* 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_TM
#ifdef IN_RING3
#endif
#include "TMInternal.h"
#include <iprt/asm-math.h>
#ifdef IN_RING3
#endif
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @def TMTIMER_ASSERT_CRITSECT
* Checks that the caller owns the critical section if one is associated with
* the timer. */
#ifdef VBOX_STRICT
# define TMTIMER_ASSERT_CRITSECT(pTimer) \
do { \
{ \
PPDMCRITSECT pCritSect = (PPDMCRITSECT)MMHyperR3ToCC((pTimer)->CTX_SUFF(pVM), (pTimer)->pCritSect); \
} \
} while (0)
#else
# define TMTIMER_ASSERT_CRITSECT(pTimer) do { } while (0)
#endif
#ifndef tmTimerLock
/**
*
* @retval VINF_SUCCESS on success (always in ring-3).
* @retval VERR_SEM_BUSY in RC and R0 if the semaphore is busy.
*
* @param pVM The VM handle.
*
* @thread EMTs for the time being.
*/
{
return rc;
}
/**
* Try take the timer lock, no waiting.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_SEM_BUSY if busy.
*
* @param pVM The VM handle.
*/
{
return rc;
}
/**
*
* @param pVM The VM handle.
*/
{
}
/**
*
* @retval VINF_SUCCESS on success (always in ring-3).
* @retval VERR_SEM_BUSY in RC and R0 if the semaphore is busy.
*
* @param pVM The VM handle.
*/
{
return rc;
}
/**
* Try take the VirtualSync lock, no waiting.
*
* @retval VINF_SUCCESS on success.
* @retval VERR_SEM_BUSY if busy.
*
* @param pVM The VM handle.
*/
{
return rc;
}
/**
* Release the VirtualSync lock.
*
* @param pVM The VM handle.
*/
{
}
#endif /* ! macros */
/**
* Notification that execution is about to start.
*
* This call must always be paired with a TMNotifyEndOfExecution call.
*
* The function may, depending on the configuration, resume the TSC and future
* clocks that only ticks when we're executing guest code.
*
* @param pVCpu The VMCPU to operate on.
*/
{
#ifndef VBOX_WITHOUT_NS_ACCOUNTING
#endif
}
/**
* Notification that execution is about to start.
*
* This call must always be paired with a TMNotifyStartOfExecution call.
*
* The function may, depending on the configuration, suspend the TSC and future
* clocks that only ticks when we're executing guest code.
*
* @param pVCpu The VMCPU to operate on.
*/
{
#ifndef VBOX_WITHOUT_NS_ACCOUNTING
# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
if (cNsOtherNewDelta > 0)
STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsOther, cNsOtherNewDelta); /* (the period before execution) */
# endif
#endif
}
/**
* Notification that the cpu is entering the halt state
*
* This call must always be paired with a TMNotifyEndOfExecution call.
*
* The function may, depending on the configuration, resume the TSC and future
* clocks that only ticks when we're halted.
*
* @param pVCpu The VMCPU to operate on.
*/
{
#ifndef VBOX_WITHOUT_NS_ACCOUNTING
#endif
}
/**
* Notification that the cpu is leaving the halt state
*
* This call must always be paired with a TMNotifyStartOfHalt call.
*
* The function may, depending on the configuration, suspend the TSC and future
* clocks that only ticks when we're halted.
*
* @param pVCpu The VMCPU to operate on.
*/
{
#ifndef VBOX_WITHOUT_NS_ACCOUNTING
# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
if (cNsOtherNewDelta > 0)
STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsOther, cNsOtherNewDelta); /* (the period before halting) */
# endif
#endif
}
/**
* Raise the timer force action flag and notify the dedicated timer EMT.
*
* @param pVM The VM handle.
*/
{
{
#ifdef IN_RING3
#endif
}
}
/**
* Schedule the queue which was changed.
*/
{
{
Log3(("tmSchedule: tmTimerQueueSchedule\n"));
#ifdef VBOX_STRICT
#endif
}
else
{
}
}
/**
* Try change the state to enmStateNew from enmStateOld
* and link the timer into the scheduling queue.
*
* @returns Success indicator.
* @param pTimer Timer in question.
* @param enmStateNew The new timer state.
* @param enmStateOld The old timer state.
*/
{
/*
* Attempt state change.
*/
bool fRc;
return fRc;
}
/**
* Links the timer onto the scheduling queue.
*
* @param pQueue The timer queue the timer belongs to.
* @param pTimer The timer.
*
* @todo FIXME: Look into potential race with the thread running the queues
* and stuff.
*/
{
do
{
if (offHead)
else
pTimer->offScheduleNext = 0;
}
/**
* Try change the state to enmStateNew from enmStateOld
* and link the timer into the scheduling queue.
*
* @returns Success indicator.
* @param pTimer Timer in question.
* @param enmStateNew The new timer state.
* @param enmStateOld The old timer state.
*/
DECLINLINE(bool) tmTimerTryWithLink(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
{
{
return true;
}
return false;
}
#ifdef VBOX_HIGH_RES_TIMERS_HACK
/**
* Worker for tmTimerPollInternal that handles misses when the decidate timer
* EMT is polling.
*
* @returns See tmTimerPollInternal.
* @param pVM Pointer to the shared VM structure.
* @param u64Now Current virtual clock timestamp.
* @param u64Delta The delta to the next even in ticks of the
* virtual clock.
* @param pu64Delta Where to return the delta.
* @param pCounter The statistics counter to update.
*/
DECLINLINE(uint64_t) tmTimerPollReturnMiss(PVM pVM, uint64_t u64Now, uint64_t u64Delta, uint64_t *pu64Delta)
{
{
}
/*
* Warp drive adjustments - this is the reverse of what tmVirtualGetRaw is doing.
*/
if (u64GipTime >= u64Delta)
{
}
else
{
u64Delta -= u64GipTime;
u64Delta += u64GipTime;
}
u64GipTime += u64Start;
return u64GipTime;
}
/**
* Worker for tmTimerPollInternal dealing with returns on virtual CPUs other
* than the one dedicated to timer work.
*
* @returns See tmTimerPollInternal.
* @param pVM Pointer to the shared VM structure.
* @param u64Now Current virtual clock timestamp.
* @param pu64Delta Where to return the delta.
*/
DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnOtherCpu(PVM pVM, uint64_t u64Now, uint64_t *pu64Delta)
{
}
/**
* Worker for tmTimerPollInternal.
*
* @returns See tmTimerPollInternal.
* @param pVM Pointer to the shared VM structure.
* @param pVCpu Pointer to the shared VMCPU structure of the
* caller.
* @param pVCpuDst Pointer to the shared VMCPU structure of the
* dedicated timer EMT.
* @param u64Now Current virtual clock timestamp.
* @param pu64Delta Where to return the delta.
* @param pCounter The statistics counter to update.
*/
DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnHit(PVM pVM, PVMCPU pVCpu, PVMCPU pVCpuDst, uint64_t u64Now,
{
*pu64Delta = 0;
return 0;
}
/**
* Common worker for TMTimerPollGIP and TMTimerPoll.
*
* This function is called before FFs are checked in the inner execution EM loops.
*
* @returns The GIP timestamp of the next event.
* 0 if the next event has already expired.
*
* @param pVM Pointer to the shared VM structure.
* @param pVCpu Pointer to the shared VMCPU structure of the caller.
* @param pu64Delta Where to store the delta.
*
* @thread The emulation thread.
*
* @remarks GIP uses ns ticks.
*/
{
/*
* Return straight away if the timer FF is already set ...
*/
return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
/*
* ... or if timers are being run.
*/
{
}
/*
* Check for TMCLOCK_VIRTUAL expiration.
*/
const uint64_t u64Expire1 = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire);
if (i64Delta1 <= 0)
{
{
#ifdef IN_RING3
#endif
}
}
/*
* Check for TMCLOCK_VIRTUAL_SYNC expiration.
* This isn't quite as stright forward if in a catch-up, not only do
* we have to adjust the 'now' but when have to adjust the delta as well.
*/
/*
* Optimistic lockless approach.
*/
uint64_t u64Expire2 = ASMAtomicUoReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
{
{
&& u64Expire2 == ASMAtomicUoReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)))
{
if (i64Delta2 > 0)
{
}
{
#ifdef IN_RING3
#endif
}
return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
}
}
}
else
{
LogFlow(("TMTimerPoll: stopped\n"));
return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
}
/*
* Complicated lockless approach.
*/
bool fCatchUp;
int cOuterTries = 42;
for (;; cOuterTries--)
{
if (fCatchUp)
{
/* No changes allowed, try get a consistent set of parameters. */
&& u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)
|| cOuterTries <= 0)
{
{
else /* we've completely caught up. */
off = offGivenUp;
}
else
/* More than 4 seconds since last time (or negative), ignore it. */
/* Check that we're still running and in catch up. */
break;
}
}
&& u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)
break; /* Got an consistent offset */
/* Repeat the initial checks before iterating. */
return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
{
}
{
LogFlow(("TMTimerPoll: stopped\n"));
return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
}
if (cOuterTries <= 0)
break; /* that's enough */
}
if (cOuterTries <= 0)
/* Calc delta and see if we've got a virtual sync hit. */
if (i64Delta2 <= 0)
{
{
#ifdef IN_RING3
#endif
}
return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
}
/*
* Return the time left to the next event.
*/
{
if (fCatchUp)
}
}
/**
* Set FF if we've passed the next virtual event.
*
* This function is called before FFs are checked in the inner execution EM loops.
*
* @returns true if timers are pending, false if not.
*
* @param pVM Pointer to the shared VM structure.
* @param pVCpu Pointer to the shared VMCPU structure of the caller.
* @thread The emulation thread.
*/
{
return off == 0;
}
/**
* Set FF if we've passed the next virtual event.
*
* This function is called before FFs are checked in the inner execution EM loops.
*
* @param pVM Pointer to the shared VM structure.
* @param pVCpu Pointer to the shared VMCPU structure of the caller.
* @thread The emulation thread.
*/
{
}
/**
* Set FF if we've passed the next virtual event.
*
* This function is called before FFs are checked in the inner execution EM loops.
*
* @returns The GIP timestamp of the next event.
* 0 if the next event has already expired.
* @param pVM Pointer to the shared VM structure.
* @param pVCpu Pointer to the shared VMCPU structure of the caller.
* @param pu64Delta Where to store the delta.
* @thread The emulation thread.
*/
{
}
#endif /* VBOX_HIGH_RES_TIMERS_HACK */
/**
* Gets the host context ring-3 pointer of the timer.
*
* @returns HC R3 pointer.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
}
/**
* Gets the host context ring-0 pointer of the timer.
*
* @returns HC R0 pointer.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
}
/**
* Gets the RC pointer of the timer.
*
* @returns RC pointer.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
}
/**
* Links a timer into the active list of a timer queue.
*
* The caller must have taken the TM semaphore before calling this function.
*
* @param pQueue The queue.
* @param pTimer The timer.
* @param u64Expire The timer expiration time.
*/
DECL_FORCE_INLINE(void) tmTimerActiveLink(PTMTIMERQUEUE pQueue, PTMTIMER pTimer, uint64_t u64Expire)
{
if (pCur)
{
{
{
if (pPrev)
else
{
}
return;
}
{
return;
}
}
}
else
{
}
}
/**
* Optimized TMTimerSet code path for starting an inactive timer.
*
* @returns VBox status code.
*
* @param pVM The VM handle.
* @param pTimer The timer handle.
* @param u64Expire The new expire time.
*/
{
/*
* Calculate and set the expiration time.
*/
Log2(("tmTimerSetOptimizedStart: %p:{.pszDesc='%s', .u64Expire=%'RU64}\n", pTimer, R3STRING(pTimer->pszDesc), u64Expire));
/*
* Link the timer into the active list.
*/
return VINF_SUCCESS;
}
/**
* Arm a timer with a (new) expire time.
*
* @returns VBox status.
* @param pTimer Timer handle as returned by one of the create functions.
* @param u64Expire New expire time.
*/
{
#ifdef VBOX_WITH_STATISTICS
/* Gather optimization info. */
switch (enmOrgState)
{
case TMTIMERSTATE_PENDING_STOP_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendStopSched); break;
case TMTIMERSTATE_PENDING_RESCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendResched); break;
}
#endif
/*
* The most common case is setting the timer again during the callback.
* The second most common case is starting a timer at some other time.
*/
#if 1
|| ( enmState1 == TMTIMERSTATE_STOPPED
{
/* Try take the TM lock and check the state again. */
{
{
return VINF_SUCCESS;
}
}
}
#endif
/*
* Unoptimized code path.
*/
int cRetries = 1000;
do
{
/*
* Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
*/
Log2(("TMTimerSet: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d u64Expire=%'RU64\n",
switch (enmState)
{
case TMTIMERSTATE_STOPPED:
{
return VINF_SUCCESS;
}
break;
{
return VINF_SUCCESS;
}
break;
case TMTIMERSTATE_ACTIVE:
{
return VINF_SUCCESS;
}
break;
{
return VINF_SUCCESS;
}
break;
#ifdef IN_RING3
if (!RTThreadYield())
RTThreadSleep(1);
#else
/** @todo call host context and yield after a couple of iterations */
#endif
break;
/*
* Invalid states.
*/
case TMTIMERSTATE_DESTROY:
case TMTIMERSTATE_FREE:
return VERR_TM_INVALID_STATE;
default:
return VERR_TM_UNKNOWN_STATE;
}
} while (cRetries-- > 0);
AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
return VERR_INTERNAL_ERROR;
}
/**
* Return the current time for the specified clock, setting pu64Now if not NULL.
*
* @returns Current time.
* @param pVM The VM handle.
* @param enmClock The clock to query.
* @param pu64Now Optional pointer where to store the return time
*/
DECL_FORCE_INLINE(uint64_t) tmTimerSetRelativeNowWorker(PVM pVM, TMCLOCK enmClock, uint64_t *pu64Now)
{
switch (enmClock)
{
case TMCLOCK_VIRTUAL_SYNC:
break;
case TMCLOCK_VIRTUAL:
break;
case TMCLOCK_REAL:
break;
default:
}
if (pu64Now)
return u64Now;
}
/**
* Optimized TMTimerSetRelative code path.
*
* @returns VBox status code.
*
* @param pVM The VM handle.
* @param pTimer The timer handle.
* @param cTicksToNext Clock ticks until the next time expiration.
* @param pu64Now Where to return the current time stamp used.
* Optional.
*/
static int tmTimerSetRelativeOptimizedStart(PVM pVM, PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now)
{
/*
* Calculate and set the expiration time.
*/
Log2(("tmTimerSetRelativeOptimizedStart: %p:{.pszDesc='%s', .u64Expire=%'RU64} cTicksToNext=%'RU64\n", pTimer, R3STRING(pTimer->pszDesc), u64Expire, cTicksToNext));
/*
* Link the timer into the active list.
*/
return VINF_SUCCESS;
}
/**
* Arm a timer with a expire time relative to the current time.
*
* @returns VBox status.
* @param pTimer Timer handle as returned by one of the create functions.
* @param cTicksToNext Clock ticks until the next time expiration.
* @param pu64Now Where to return the current time stamp used.
* Optional.
*/
{
int rc;
#ifdef VBOX_WITH_STATISTICS
/* Gather optimization info. */
switch (enmOrgState)
{
case TMTIMERSTATE_EXPIRED_DELIVER: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStExpDeliver); break;
case TMTIMERSTATE_PENDING_STOP_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendStopSched); break;
case TMTIMERSTATE_PENDING_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendSched); break;
case TMTIMERSTATE_PENDING_RESCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendResched); break;
}
#endif
/*
* Try to take the TM lock and optimize the common cases.
*
* With the TM lock we can safely make optimizations like immediate
* scheduling and we can also be 100% sure that we're not racing the
* running of the timer queues. As an additional restraint we require the
* timer to have a critical section associated with to be 100% there aren't
* concurrent operations on the timer. (This latter isn't necessary any
* longer as this isn't supported for any timers, critsect or not.)
*
* Note! Lock ordering doesn't apply when we only tries to
* get the innermost locks.
*/
#if 1
if ( fOwnTMLock
{
|| enmState == TMTIMERSTATE_STOPPED)
{
return VINF_SUCCESS;
}
/* Optimize other states when it becomes necessary. */
}
#endif
/*
* Unoptimized path.
*/
bool fOwnVirtSyncLock;
&& enmClock == TMCLOCK_VIRTUAL_SYNC
{
/*
* Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
*/
switch (enmState)
{
case TMTIMERSTATE_STOPPED:
if (enmClock == TMCLOCK_VIRTUAL_SYNC)
{
/** @todo To fix assertion in tmR3TimerQueueRunVirtualSync:
* Figure a safe way of activating this timer while the queue is
* being run.
* (99.9% sure this that the assertion is caused by DevAPIC.cpp
* re-starting the timer in respons to a initial_count write.) */
}
/* fall thru */
{
Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [EXP/STOP]\n",
rc = VINF_SUCCESS;
break;
}
rc = VERR_TRY_AGAIN;
break;
{
Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [PEND_SCHED]\n",
rc = VINF_SUCCESS;
break;
}
rc = VERR_TRY_AGAIN;
break;
case TMTIMERSTATE_ACTIVE:
{
Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [ACTIVE]\n",
rc = VINF_SUCCESS;
break;
}
rc = VERR_TRY_AGAIN;
break;
{
Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [PEND_RESCH/STOP]\n",
rc = VINF_SUCCESS;
break;
}
rc = VERR_TRY_AGAIN;
break;
#ifdef IN_RING3
if (!RTThreadYield())
RTThreadSleep(1);
#else
/** @todo call host context and yield after a couple of iterations */
#endif
rc = VERR_TRY_AGAIN;
break;
/*
* Invalid states.
*/
case TMTIMERSTATE_DESTROY:
case TMTIMERSTATE_FREE:
break;
default:
break;
}
/* switch + loop is tedious to break out of. */
if (rc == VINF_SUCCESS)
break;
if (rc != VERR_TRY_AGAIN)
{
break;
}
if (cRetries <= 0)
{
AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
break;
}
/*
* Retry to gain locks.
*/
if (!fOwnTMLock)
{
if ( !fOwnTMLock
&& enmClock == TMCLOCK_VIRTUAL_SYNC
&& !fOwnVirtSyncLock)
}
} /* for (;;) */
/*
* Clean up and return.
*/
if (fOwnVirtSyncLock)
if (fOwnTMLock)
if ( !fOwnTMLock
&& !fOwnVirtSyncLock
&& enmClock == TMCLOCK_VIRTUAL_SYNC)
return rc;
}
/**
* Arm a timer with a (new) expire time relative to current time.
*
* @returns VBox status.
* @param pTimer Timer handle as returned by one of the create functions.
* @param cMilliesToNext Number of millieseconds to the next tick.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
case TMCLOCK_REAL:
default:
return VERR_INTERNAL_ERROR;
}
}
/**
* Arm a timer with a (new) expire time relative to current time.
*
* @returns VBox status.
* @param pTimer Timer handle as returned by one of the create functions.
* @param cMicrosToNext Number of microseconds to the next tick.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
case TMCLOCK_REAL:
default:
return VERR_INTERNAL_ERROR;
}
}
/**
* Arm a timer with a (new) expire time relative to current time.
*
* @returns VBox status.
* @param pTimer Timer handle as returned by one of the create functions.
* @param cNanosToNext Number of nanoseconds to the next tick.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
case TMCLOCK_REAL:
default:
return VERR_INTERNAL_ERROR;
}
}
/**
* Drops a hint about the frequency of the timer.
*
* This is used by TM and the VMM to calculate how often guest execution needs
* to be interrupted. The hint is automatically cleared by TMTimerStop.
*
* @returns VBox status code.
* @param pTimer Timer handle as returned by one of the create
* functions.
* @param uHzHint The frequency hint. Pass 0 to clear the hint.
*
* @remarks We're using an integer hertz value here since anything above 1 HZ
* is not going to be any trouble satisfying scheduling wise. The
* range where it makes sense is >= 100 HZ.
*/
{
return VINF_SUCCESS;
}
/**
* Stop the timer.
* Use TMR3TimerArm() to "un-stop" the timer.
*
* @returns VBox status.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
/* Reset the HZ hint. */
{
}
/** @todo see if this function needs optimizing. */
int cRetries = 1000;
do
{
/*
* Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
*/
Log2(("TMTimerStop: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
switch (enmState)
{
//AssertMsgFailed(("You don't stop an expired timer dude!\n"));
return VERR_INVALID_PARAMETER;
case TMTIMERSTATE_STOPPED:
return VINF_SUCCESS;
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
break;
case TMTIMERSTATE_ACTIVE:
{
return VINF_SUCCESS;
}
break;
#ifdef IN_RING3
if (!RTThreadYield())
RTThreadSleep(1);
#else
/**@todo call host and yield cpu after a while. */
#endif
break;
/*
* Invalid states.
*/
case TMTIMERSTATE_DESTROY:
case TMTIMERSTATE_FREE:
return VERR_TM_INVALID_STATE;
default:
return VERR_TM_UNKNOWN_STATE;
}
} while (cRetries-- > 0);
AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
return VERR_INTERNAL_ERROR;
}
/**
* Get the current clock time.
* Handy for calculating the new expire time.
*
* @returns Current clock time.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
{
case TMCLOCK_VIRTUAL:
break;
case TMCLOCK_VIRTUAL_SYNC:
break;
case TMCLOCK_REAL:
break;
default:
return ~(uint64_t)0;
}
//Log2(("TMTimerGet: returns %'RU64 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
// u64, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
return u64;
}
/**
* Get the freqency of the timer clock.
*
* @returns Clock frequency (as Hz of course).
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
return TMCLOCK_FREQ_VIRTUAL;
case TMCLOCK_REAL:
return TMCLOCK_FREQ_REAL;
default:
return 0;
}
}
/**
* Get the current clock time as nanoseconds.
*
* @returns The timer clock as nanoseconds.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
}
/**
* Get the current clock time as microseconds.
*
* @returns The timer clock as microseconds.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
}
/**
* Get the current clock time as milliseconds.
*
* @returns The timer clock as milliseconds.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
}
/**
* Converts the specified timer clock time to nanoseconds.
*
* @returns nanoseconds.
* @param pTimer Timer handle as returned by one of the create functions.
* @param u64Ticks The clock ticks.
* @remark There could be rounding errors here. We just do a simple integere divide
* without any adjustments.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
return u64Ticks;
case TMCLOCK_REAL:
return u64Ticks * 1000000;
default:
return 0;
}
}
/**
* Converts the specified timer clock time to microseconds.
*
* @returns microseconds.
* @param pTimer Timer handle as returned by one of the create functions.
* @param u64Ticks The clock ticks.
* @remark There could be rounding errors here. We just do a simple integere divide
* without any adjustments.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
return u64Ticks / 1000;
case TMCLOCK_REAL:
return u64Ticks * 1000;
default:
return 0;
}
}
/**
* Converts the specified timer clock time to milliseconds.
*
* @returns milliseconds.
* @param pTimer Timer handle as returned by one of the create functions.
* @param u64Ticks The clock ticks.
* @remark There could be rounding errors here. We just do a simple integere divide
* without any adjustments.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
return u64Ticks / 1000000;
case TMCLOCK_REAL:
return u64Ticks;
default:
return 0;
}
}
/**
* Converts the specified nanosecond timestamp to timer clock ticks.
*
* @returns timer clock ticks.
* @param pTimer Timer handle as returned by one of the create functions.
* @param u64NanoTS The nanosecond value ticks to convert.
* @remark There could be rounding and overflow errors here.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
return u64NanoTS;
case TMCLOCK_REAL:
return u64NanoTS / 1000000;
default:
return 0;
}
}
/**
* Converts the specified microsecond timestamp to timer clock ticks.
*
* @returns timer clock ticks.
* @param pTimer Timer handle as returned by one of the create functions.
* @param u64MicroTS The microsecond value ticks to convert.
* @remark There could be rounding and overflow errors here.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
return u64MicroTS * 1000;
case TMCLOCK_REAL:
return u64MicroTS / 1000;
default:
return 0;
}
}
/**
* Converts the specified millisecond timestamp to timer clock ticks.
*
* @returns timer clock ticks.
* @param pTimer Timer handle as returned by one of the create functions.
* @param u64MilliTS The millisecond value ticks to convert.
* @remark There could be rounding and overflow errors here.
*/
{
{
case TMCLOCK_VIRTUAL:
case TMCLOCK_VIRTUAL_SYNC:
return u64MilliTS * 1000000;
case TMCLOCK_REAL:
return u64MilliTS;
default:
return 0;
}
}
/**
* Get the expire time of the timer.
* Only valid for active timers.
*
* @returns Expire time of the timer.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
int cRetries = 1000;
do
{
switch (enmState)
{
case TMTIMERSTATE_STOPPED:
Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
return ~(uint64_t)0;
case TMTIMERSTATE_ACTIVE:
Log2(("TMTimerGetExpire: returns %'RU64 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
#ifdef IN_RING3
if (!RTThreadYield())
RTThreadSleep(1);
#endif
break;
/*
* Invalid states.
*/
case TMTIMERSTATE_DESTROY:
case TMTIMERSTATE_FREE:
Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
return ~(uint64_t)0;
default:
return ~(uint64_t)0;
}
} while (cRetries-- > 0);
AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
return ~(uint64_t)0;
}
/**
* Checks if a timer is active or not.
*
* @returns True if active.
* @returns False if not active.
* @param pTimer Timer handle as returned by one of the create functions.
*/
{
switch (enmState)
{
case TMTIMERSTATE_STOPPED:
Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
return false;
case TMTIMERSTATE_ACTIVE:
Log2(("TMTimerIsActive: returns true (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
return true;
/*
* Invalid states.
*/
case TMTIMERSTATE_DESTROY:
case TMTIMERSTATE_FREE:
AssertMsgFailed(("Invalid timer state %s (%s)\n", tmTimerState(enmState), R3STRING(pTimer->pszDesc)));
Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
return false;
default:
return false;
}
}
/**
* Convert state to string.
*
* @returns Readonly status name.
* @param enmState State.
*/
{
switch (enmState)
{
case TMTIMERSTATE_##state: \
default:
return "Invalid state!";
}
}
/**
* Schedules the given timer on the given queue.
*
* @param pQueue The timer queue.
* @param pTimer The timer that needs scheduling.
*
* @remarks Called while owning the lock.
*/
{
/*
* Processing.
*/
unsigned cRetries = 2;
do
{
switch (enmState)
{
/*
* Reschedule timer (in the active list).
*/
{
if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE, TMTIMERSTATE_PENDING_RESCHEDULE)))
break; /* retry */
if (pPrev)
else
{
}
if (pNext)
/* fall thru */
}
/*
* Schedule timer (insert into the active list).
*/
{
break; /* retry */
if (pCur)
{
{
{
if (pPrev)
else
{
}
return;
}
{
return;
}
}
}
else
{
}
return;
}
/*
* Stop the timer in active list.
*/
{
if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, TMTIMERSTATE_PENDING_STOP)))
break; /* retry */
if (pPrev)
else
{
}
if (pNext)
/* fall thru */
}
/*
* Stop the timer (not on the active list).
*/
break;
return;
/*
* The timer is pending destruction by TMR3TimerDestroy, our caller.
* Nothing to do here.
*/
case TMTIMERSTATE_DESTROY:
break;
/*
* Postpone these until they get into the right state.
*/
return;
/*
* None of these can be in the schedule.
*/
case TMTIMERSTATE_FREE:
case TMTIMERSTATE_STOPPED:
case TMTIMERSTATE_ACTIVE:
default:
AssertMsgFailed(("Timer (%p) in the scheduling list has an invalid state %s (%d)!",
return;
}
} while (cRetries-- > 0);
}
/**
* Schedules the specified timer queue.
*
* @param pVM The VM to run the timers for.
* @param pQueue The queue to schedule.
*
* @remarks Called while owning the lock.
*/
{
/*
* Dequeue the scheduling list and iterate it.
*/
Log2(("tmTimerQueueSchedule: pQueue=%p:{.enmClock=%d, offNext=%RI32, .u64Expired=%'RU64}\n", pQueue, pQueue->enmClock, offNext, pQueue->u64Expire));
if (!offNext)
return;
while (pNext)
{
/*
* Unlink the head timer and find the next one.
*/
pTimer->offScheduleNext = 0;
/*
* Do the scheduling.
*/
Log2(("tmTimerQueueSchedule: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, .pszDesc=%s}\n",
pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, R3STRING(pTimer->pszDesc)));
} /* foreach timer in current schedule batch. */
}
#ifdef VBOX_STRICT
/**
* Checks that the timer queues are sane.
*
* @param pVM VM handle.
*
* @remarks Called while owning the lock.
*/
{
/*
* Check the linking of the active lists.
*/
for (int i = 0; i < TMCLOCK_MAX; i++)
{
{
AssertMsg(TMTIMER_GET_PREV(pCur) == pPrev, ("%s: %p != %p\n", pszWhere, TMTIMER_GET_PREV(pCur), pPrev));
switch (enmState)
{
case TMTIMERSTATE_ACTIVE:
break;
break;
default:
AssertMsgFailed(("%s: Invalid state enmState=%d %s\n", pszWhere, enmState, tmTimerState(enmState)));
break;
}
}
}
# ifdef IN_RING3
/*
* Do the big list and check that active timers all are in the active lists.
*/
{
switch (enmState)
{
case TMTIMERSTATE_ACTIVE:
{
break;
}
case TMTIMERSTATE_STOPPED:
{
{
}
break;
}
/* ignore */
break;
/* shouldn't get here! */
case TMTIMERSTATE_DESTROY:
default:
break;
}
}
# endif /* IN_RING3 */
}
#endif /* !VBOX_STRICT */
/**
* Gets the current warp drive percent.
*
* @returns The warp drive percent.
* @param pVM The VM handle.
*/
{
}
/**
* Gets the highest frequency hint for all the important timers.
*
* @returns The highest frequency. 0 if no timers care.
* @param pVM The VM handle.
*/
{
/*
* Query the value, recalculate it if necessary.
*
* The "right" highest frequency value isn't so important that we'll block
* waiting on the timer semaphore.
*/
{
{
/*
* Loop over the timers associated with each clock.
*/
uMaxHzHint = 0;
for (int i = 0; i < TMCLOCK_MAX; i++)
{
{
if (uHzHint > uMaxHzHint)
{
{
case TMTIMERSTATE_ACTIVE:
break;
case TMTIMERSTATE_STOPPED:
case TMTIMERSTATE_DESTROY:
case TMTIMERSTATE_FREE:
break;
/* no default, want gcc warnings when adding more states. */
}
}
}
}
}
}
return uMaxHzHint;
}
/**
* Calculates a host timer frequency that would be suitable for the current
* timer load.
*
* This will take the highest timer frequency, adjust for catch-up and warp
* driver, and finally add a little fudge factor. The caller (VMM) will use
* the result to adjust the per-cpu preemption timer.
*
* @returns The highest frequency. 0 if no important timers around.
* @param pVM The VM handle.
* @param pVCpu The current CPU.
*/
{
/* Catch up. */
{
{
uHz /= 100;
}
}
/* Warp drive */
{
{
uHz /= 100;
}
}
/* Fudge factor. */
/** @todo make this configurable. */
uHz /= 100;
return uHz;
}