TM.cpp revision 056c7f3bf4322be2711c6e33d04a886da57a7d24
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * TM - Time Manager.
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * available from http://www.virtualbox.org. This file is free software;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * you can redistribute it and/or modify it under the terms of the GNU
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * General Public License (GPL) as published by the Free Software
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * additional information or have any questions.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/** @page pg_tm TM - The Time Manager
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The Time Manager abstracts the CPU clocks and manages timers used by the VMM,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * device and drivers.
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync * @see grp_tm
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync * @section sec_tm_clocks Clocks
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * There are currently 4 clocks:
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * - Virtual (guest).
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * - Synchronous virtual (guest).
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * - CPU Tick (TSC) (guest). Only current use is rdtsc emulation. Usually a
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * function of the virtual clock.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * - Real (host). This is only used for display updates atm.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The most important clocks are the three first ones and of these the second is
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * the most interesting.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The synchronous virtual clock is tied to the virtual clock except that it
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * will take into account timer delivery lag caused by host scheduling. It will
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * normally never advance beyond the head timer, and when lagging too far behind
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * it will gradually speed up to catch up with the virtual clock. All devices
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * implementing time sources accessible to and used by the guest is using this
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * clock (for timers and other things). This ensures consistency between the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * time sources.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The virtual clock is implemented as an offset to a monotonic, high
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * resolution, wall clock. The current time source is using the RTTimeNanoTS()
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * machinery based upon the Global Info Pages (GIP), that is, we're using TSC
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * deltas (usually 10 ms) to fill the gaps between GIP updates. The result is
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * a fairly high res clock that works in all contexts and on all hosts. The
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * virtual clock is paused when the VM isn't in the running state.
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * The CPU tick (TSC) is normally virtualized as a function of the synchronous
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * virtual clock, where the frequency defaults to the host cpu frequency (as we
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * measure it). In this mode it is possible to configure the frequency. Another
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * (non-default) option is to use the raw unmodified host TSC values. And yet
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * another, to tie it to time spent executing guest code. All these things are
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * configurable should non-default behavior be desirable.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The real clock is a monotonic clock (when available) with relatively low
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * resolution, though this a bit host specific. Note that we're currently not
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * servicing timers using the real clock when the VM is not running, this is
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * simply because it has not been needed yet therefore not implemented.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @subsection subsec_tm_timesync Guest Time Sync / UTC time
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Guest time syncing is primarily taken care of by the VMM device. The
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * principle is very simple, the guest additions periodically asks the VMM
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * device what the current UTC time is and makes adjustments accordingly.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * A complicating factor is that the synchronous virtual clock might be doing
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * catchups and the guest perception is currently a little bit behind the world
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * but it will (hopefully) be catching up soon as we're feeding timer interrupts
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * at a slightly higher rate. Adjusting the guest clock to the current wall
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * time in the real world would be a bad idea then because the guest will be
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * advancing too fast and run ahead of world time (if the catchup works out).
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * To solve this problem TM provides the VMM device with an UTC time source that
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * gets adjusted with the current lag, so that when the guest eventually catches
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * up the lag it will be showing correct real world time.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @section sec_tm_timers Timers
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The timers can use any of the TM clocks described in the previous section.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Each clock has its own scheduling facility, or timer queue if you like.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * There are a few factors which makes it a bit complex. First, there is the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * usual R0 vs R3 vs. RC thing. Then there is multiple threads, and then there
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync * is the timer thread that periodically checks whether any timers has expired
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync * without EMT noticing. On the API level, all but the create and save APIs
f5f42fa2ec9924006cad8e5adc646b34b35ba3c3vboxsync * must be mulithreaded. EMT will always run the timers.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The design is using a doubly linked list of active timers which is ordered
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * by expire date. This list is only modified by the EMT thread. Updates to
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * the list are batched in a singly linked list, which is then process by the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * EMT thread at the first opportunity (immediately, next time EMT modifies a
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * timer on that clock, or next timer timeout). Both lists are offset based and
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync * all the elements are therefore allocated from the hyper heap.
f5f42fa2ec9924006cad8e5adc646b34b35ba3c3vboxsync * For figuring out when there is need to schedule and run timers TM will:
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * - Poll whenever somebody queries the virtual clock.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * - Poll the virtual clocks from the EM and REM loops.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * - Poll the virtual clocks from trap exit path.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * - Poll the virtual clocks and calculate first timeout from the halt loop.
68fb2428898c55a7172e6a75a0a8d7ce259919bdvboxsync * - Employ a thread which periodically (100Hz) polls all the timer queues.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @section sec_tm_timer Logging
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Level 2: Logs a most of the timer state transitions and queue servicing.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Level 3: Logs a few oddments.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Level 4: Logs TMCLOCK_VIRTUAL_SYNC catch-up events.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync/*******************************************************************************
956a0e3c076406b83d635174a201fd8761ee5133vboxsync* Header Files *
956a0e3c076406b83d635174a201fd8761ee5133vboxsync*******************************************************************************/
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync/*******************************************************************************
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync* Defined Constants And Macros *
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync*******************************************************************************/
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync/** The current saved state version.*/
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync/*******************************************************************************
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync* Internal Functions *
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync*******************************************************************************/
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncstatic DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncstatic DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncstatic DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncstatic void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncstatic DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncstatic DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsyncstatic DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * Initializes the TM.
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * @returns VBox status code.
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * @param pVM The VM to operate on.
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * Assert alignment and sizes.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertRelease(sizeof(pVM->tm.s) <= sizeof(pVM->tm.padding));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Init the structure.
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync int rc = MMHyperAlloc(pVM, sizeof(pVM->tm.s.paTimerQueuesR3[0]) * TMCLOCK_MAX, 0, MM_TAG_TM, &pv);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync pVM->tm.s.paTimerQueuesR0 = MMHyperR3ToR0(pVM, pv);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesRC = MMHyperR3ToRC(pVM, pv);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].enmClock = TMCLOCK_VIRTUAL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].u64Expire = INT64_MAX;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].enmClock = TMCLOCK_VIRTUAL_SYNC;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].u64Expire = INT64_MAX;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].enmClock = TMCLOCK_REAL;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].u64Expire = INT64_MAX;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].enmClock = TMCLOCK_TSC;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].u64Expire = INT64_MAX;
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * We directly use the GIP to calculate the virtual time. We map the
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * the GIP into the guest context so we can do this calculation there
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * as well and save costly world switches.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertMsgReturn(pVM->tm.s.pvGIPR3, ("GIP support is now required!\n"), VERR_INTERNAL_ERROR);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertMsgRCReturn(rc, ("Failed to get GIP physical address!\n"), rc);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = MMR3HyperMapHCPhys(pVM, pVM->tm.s.pvGIPR3, NIL_RTR0PTR, HCPhysGIP, PAGE_SIZE, "GIP", &GCPtr);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertMsgFailed(("Failed to map GIP into GC, rc=%Rrc!\n", rc));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync LogFlow(("TMR3Init: HCPhysGIP=%RHp at %RRv\n", HCPhysGIP, pVM->tm.s.pvGIPRC));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* Check assumptions made in TMAllVirtual.cpp about the GIP update interval. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if ( g_pSUPGlobalInfoPage->u32Magic == SUPGLOBALINFOPAGE_MAGIC
956a0e3c076406b83d635174a201fd8761ee5133vboxsync && g_pSUPGlobalInfoPage->u32UpdateIntervalNS >= 250000000 /* 0.25s */)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync N_("The GIP update interval is too big. u32UpdateIntervalNS=%RU32 (u32UpdateHz=%RU32)"),
956a0e3c076406b83d635174a201fd8761ee5133vboxsync g_pSUPGlobalInfoPage->u32UpdateIntervalNS, g_pSUPGlobalInfoPage->u32UpdateHz);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Setup the VirtualGetRaw backend.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.VirtualGetRawDataR3.pu64Prev = &pVM->tm.s.u64VirtualRawPrev;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.VirtualGetRawDataR3.pfnBad = tmVirtualNanoTSBad;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.VirtualGetRawDataR3.pfnRediscover = tmVirtualNanoTSRediscover;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLFenceSync;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLFenceAsync;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if (g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLegacySync;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLegacyAsync;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.VirtualGetRawDataRC.pu64Prev = MMHyperR3ToRC(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.VirtualGetRawDataR0.pu64Prev = MMHyperR3ToR0(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertReturn(pVM->tm.s.VirtualGetRawDataR0.pu64Prev, VERR_INTERNAL_ERROR);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* The rest is done in TMR3InitFinalize since it's too early to call PDM. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Get our CFGM node, create it if necessary.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync PCFGMNODE pCfgHandle = CFGMR3GetChild(CFGMR3GetRoot(pVM), "TM");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "TM", &pCfgHandle);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Determin the TSC configuration and frequency.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{/TM/TSCVirtualized,bool,true}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Use a virtualize TSC, i.e. trap all TSC access. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3QueryBool(pCfgHandle, "TSCVirtualized", &pVM->tm.s.fTSCVirtualized);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync N_("Configuration error: Failed to querying bool value \"UseRealTSC\""));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* source */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{/TM/UseRealTSC,bool,false}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Use the real TSC as time source for the TSC instead of the synchronous
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * virtual clock (false, default). */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3QueryBool(pCfgHandle, "UseRealTSC", &pVM->tm.s.fTSCUseRealTSC);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.fTSCUseRealTSC = false; /* use virtual time */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync N_("Configuration error: Failed to querying bool value \"UseRealTSC\""));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /* TSC reliability */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{/TM/MaybeUseOffsettedHostTSC,bool,detect}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Whether the CPU has a fixed TSC rate and may be used in offsetted mode with
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * VT-x/AMD-V execution. This is autodetected in a very restrictive way by
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * default. */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync rc = CFGMR3QueryBool(pCfgHandle, "MaybeUseOffsettedHostTSC", &pVM->tm.s.fMaybeUseOffsettedHostTSC);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync pVM->tm.s.fMaybeUseOffsettedHostTSC = tmR3HasFixedTSC(pVM);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync /** @cfgm{TM/TSCTicksPerSecond, uint32_t, Current TSC frequency from GIP}
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * The number of TSC ticks per second (i.e. the TSC frequency). This will
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * override TSCUseRealTSC, TSCVirtualized and MaybeUseOffsettedHostTSC.
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync rc = CFGMR3QueryU64(pCfgHandle, "TSCTicksPerSecond", &pVM->tm.s.cTSCTicksPerSecond);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync pVM->tm.s.cTSCTicksPerSecond = tmR3CalibrateTSC(pVM);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync pVM->tm.s.cTSCTicksPerSecond = _4G - 1; /* (A limitation of our math code) */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync N_("Configuration error: Failed to querying uint64_t value \"TSCTicksPerSecond\""));
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync N_("Configuration error: \"TSCTicksPerSecond\" = %RI64 is not in the range 1MHz..4GHz-1"),
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync pVM->tm.s.fTSCUseRealTSC = pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync /** @cfgm{TM/TSCTiedToExecution, bool, false}
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * Whether the TSC should be tied to execution. This will exclude most of the
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * virtualization overhead, but will by default include the time spend in the
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * halt state (see TM/TSCNotTiedToHalt). This setting will override all other
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * TSC settings except for TSCTicksPerSecond and TSCNotTiedToHalt, which should
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * be used avoided or used with great care. Note that this will only work right
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * together with VT-x or AMD-V, and with a single virtual CPU. */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCTiedToExecution", &pVM->tm.s.fTSCTiedToExecution, false);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync N_("Configuration error: Failed to querying bool value \"TSCTiedToExecution\""));
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync /* tied to execution, override all other settings. */
f5f42fa2ec9924006cad8e5adc646b34b35ba3c3vboxsync /** @cfgm{TM/TSCNotTiedToHalt, bool, true}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * For overriding the default of TM/TSCTiedToExecution, i.e. set this to false
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * to make the TSC freeze during HLT. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCNotTiedToHalt", &pVM->tm.s.fTSCNotTiedToHalt, false);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync N_("Configuration error: Failed to querying bool value \"TSCNotTiedToHalt\""));
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync /* setup and report */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync CPUMR3SetCR4Feature(pVM, X86_CR4_TSD, ~X86_CR4_TSD);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool\n"
956a0e3c076406b83d635174a201fd8761ee5133vboxsync "TM: fMaybeUseOffsettedHostTSC=%RTbool TSCTiedToExecution=%RTbool TSCNotTiedToHalt=%RTbool\n",
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.fMaybeUseOffsettedHostTSC, pVM->tm.s.fTSCTiedToExecution, pVM->tm.s.fTSCNotTiedToHalt));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Configure the timer synchronous virtual time.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{TM/ScheduleSlack, uint32_t, ns, 0, UINT32_MAX, 100000}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Scheduling slack when processing timers. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3QueryU32(pCfgHandle, "ScheduleSlack", &pVM->tm.s.u32VirtualSyncScheduleSlack);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.u32VirtualSyncScheduleSlack = 100000; /* 0.100ms (ASSUMES virtual time is nanoseconds) */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync N_("Configuration error: Failed to querying 32-bit integer value \"ScheduleSlack\""));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{TM/CatchUpStopThreshold, uint64_t, ns, 0, UINT64_MAX, 500000}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * When to stop a catch-up, considering it successful. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStopThreshold", &pVM->tm.s.u64VirtualSyncCatchUpStopThreshold);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.u64VirtualSyncCatchUpStopThreshold = 500000; /* 0.5ms */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpStopThreshold\""));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{TM/CatchUpGiveUpThreshold, uint64_t, ns, 0, UINT64_MAX, 60000000000}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * When to give up a catch-up attempt. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3QueryU64(pCfgHandle, "CatchUpGiveUpThreshold", &pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold = UINT64_C(60000000000); /* 60 sec */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpGiveUpThreshold\""));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{TM/CatchUpPrecentage[0..9], uint32_t, %, 1, 2000, various}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The catch-up percent for a given period. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{TM/CatchUpStartThreshold[0..9], uint64_t, ns, 0, UINT64_MAX,
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The catch-up period threshold, or if you like, when a period starts. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStartThreshold" #iPeriod, &u64); \
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpThreshold" #iPeriod "\"")); \
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync if ( (iPeriod > 0 && u64 <= pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod - 1].u64Start) \
956a0e3c076406b83d635174a201fd8761ee5133vboxsync || u64 >= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold) \
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Configuration error: Invalid start of period #" #iPeriod ": %RU64"), u64); \
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u64Start = u64; \
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync rc = CFGMR3QueryU32(pCfgHandle, "CatchUpPrecentage" #iPeriod, &pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage); \
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage = (DefPct); \
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 32-bit integer value \"CatchUpPrecentage" #iPeriod "\"")); \
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync } while (0)
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync /* This needs more tuning. Not sure if we really need so many period and be so gentle. */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync TM_CFG_PERIOD(1, 1500000, 10); /* 1.50ms at 1.10x */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync TM_CFG_PERIOD(3, 30000000, 50); /* 30ms at 1.50x */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync TM_CFG_PERIOD(4, 75000000, 75); /* 75ms at 1.75x */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync TM_CFG_PERIOD(5, 175000000, 100); /* 175ms at 2x */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync TM_CFG_PERIOD(6, 500000000, 200); /* 500ms at 3x */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync AssertCompile(RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods) == 10);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Configure real world time (UTC).
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{TM/UTCOffset, int64_t, ns, INT64_MIN, INT64_MAX, 0}
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * The UTC offset. This is used to put the guest back or forwards in time. */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync rc = CFGMR3QueryS64(pCfgHandle, "UTCOffset", &pVM->tm.s.offUTC);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync N_("Configuration error: Failed to querying 64-bit integer value \"UTCOffset\""));
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * Setup the warp drive.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync /** @cfgm{TM/WarpDrivePercentage, uint32_t, %, 0, 20000, 100}
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * The warp drive percentage, 100% is normal speed. This is used to speed up
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * or slow down the virtual clock, which can be useful for fast forwarding
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * borring periods during tests. */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync rc = CFGMR3QueryU32(pCfgHandle, "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync rc = CFGMR3QueryU32(CFGMR3GetRoot(pVM), "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage); /* legacy */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync N_("Configuration error: Failed to querying uint32_t value \"WarpDrivePercent\""));
956a0e3c076406b83d635174a201fd8761ee5133vboxsync else if ( pVM->tm.s.u32VirtualWarpDrivePercentage < 2
956a0e3c076406b83d635174a201fd8761ee5133vboxsync || pVM->tm.s.u32VirtualWarpDrivePercentage > 20000)
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync N_("Configuration error: \"WarpDrivePercent\" = %RI32 is not in the range 2..20000"),
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync pVM->tm.s.fVirtualWarpDrive = pVM->tm.s.u32VirtualWarpDrivePercentage != 100;
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync LogRel(("TM: u32VirtualWarpDrivePercentage=%RI32\n", pVM->tm.s.u32VirtualWarpDrivePercentage));
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync * Start the timer (guard against REM not yielding).
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** @cfgm{TM/TimerMillies, uint32_t, ms, 1, 1000, 10}
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync * The watchdog timer interval. */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rc = CFGMR3QueryU32(pCfgHandle, "TimerMillies", &u32Millies);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync N_("Configuration error: Failed to query uint32_t value \"TimerMillies\""));
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rc = RTTimerCreate(&pVM->tm.s.pTimer, u32Millies, tmR3TimerCallback, pVM);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync AssertMsgFailed(("Failed to create timer, u32Millies=%d rc=%Rrc.\n", u32Millies, rc));
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync Log(("TM: Created timer %p firing every %d millieseconds\n", pVM->tm.s.pTimer, u32Millies));
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync * Register saved state.
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rc = SSMR3RegisterInternal(pVM, "tm", 1, TM_SAVED_STATE_VERSION, sizeof(uint64_t) * 8,
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * Register statistics.
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR3.c1nsSteps,STAMTYPE_U32, "/TM/R3/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR3.cBadPrev, STAMTYPE_U32, "/TM/R3/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR0.c1nsSteps,STAMTYPE_U32, "/TM/R0/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR0.cBadPrev, STAMTYPE_U32, "/TM/R0/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataRC.c1nsSteps,STAMTYPE_U32, "/TM/GC/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataRC.cBadPrev, STAMTYPE_U32, "/TM/GC/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REL_REG( pVM,(void*)&pVM->tm.s.offVirtualSync, STAMTYPE_U64, "/TM/VirtualSync/CurrentOffset", STAMUNIT_NS, "The current offset. (subtract GivenUp to get the lag)");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.offVirtualSyncGivenUp, STAMTYPE_U64, "/TM/VirtualSync/GivenUp", STAMUNIT_NS, "Nanoseconds of the 'CurrentOffset' that's been given up and won't ever be attemted caught up with.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR3.cExpired, STAMTYPE_U32, "/TM/R3/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR3.cUpdateRaces,STAMTYPE_U32, "/TM/R3/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR0.cExpired, STAMTYPE_U32, "/TM/R0/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR0.cUpdateRaces,STAMTYPE_U32, "/TM/R0/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataRC.cExpired, STAMTYPE_U32, "/TM/GC/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataRC.cUpdateRaces,STAMTYPE_U32, "/TM/GC/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatDoQueues, STAMTYPE_PROFILE, "/TM/DoQueues", STAMUNIT_TICKS_PER_CALL, "Profiling timer TMR3TimerQueuesDo.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG(pVM, &pVM->tm.s.StatDoQueuesSchedule, STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Schedule", STAMUNIT_TICKS_PER_CALL, "The scheduling part.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG(pVM, &pVM->tm.s.StatDoQueuesRun, STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Run", STAMUNIT_TICKS_PER_CALL, "The run part.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG(pVM, &pVM->tm.s.StatPollAlreadySet, STAMTYPE_COUNTER, "/TM/PollAlreadySet", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the FF was already set.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG(pVM, &pVM->tm.s.StatPollVirtual, STAMTYPE_COUNTER, "/TM/PollHitsVirtual", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL queue.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatPollVirtualSync, STAMTYPE_COUNTER, "/TM/PollHitsVirtualSync", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL_SYNC queue.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatPollMiss, STAMTYPE_COUNTER, "/TM/PollMiss", STAMUNIT_OCCURENCES, "TMTimerPoll calls where nothing had expired.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG(pVM, &pVM->tm.s.StatPostponedR3, STAMTYPE_COUNTER, "/TM/PostponedR3", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-3.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatPostponedRZ, STAMTYPE_COUNTER, "/TM/PostponedRZ", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-0 / RC.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatScheduleOneR3, STAMTYPE_PROFILE, "/TM/ScheduleOneR3", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync STAM_REG(pVM, &pVM->tm.s.StatScheduleOneRZ, STAMTYPE_PROFILE, "/TM/ScheduleOneRZ", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatScheduleSetFF, STAMTYPE_COUNTER, "/TM/ScheduleSetFF", STAMUNIT_OCCURENCES, "The number of times the timer FF was set instead of doing scheduling.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTimerSetR3, STAMTYPE_PROFILE, "/TM/TimerSetR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-3.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTimerSetRZ, STAMTYPE_PROFILE, "/TM/TimerSetRZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-0 / RC.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTimerStopR3, STAMTYPE_PROFILE, "/TM/TimerStopR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-3.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTimerStopRZ, STAMTYPE_PROFILE, "/TM/TimerStopRZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-0 / RC.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualGet, STAMTYPE_COUNTER, "/TM/VirtualGet", STAMUNIT_OCCURENCES, "The number of times TMTimerGet was called when the clock was running.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSetFF, STAMTYPE_COUNTER, "/TM/VirtualGetSetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling TMTimerGet.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSync, STAMTYPE_COUNTER, "/TM/VirtualGetSync", STAMUNIT_OCCURENCES, "The number of times TMTimerGetSync was called when the clock was running.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSyncSetFF, STAMTYPE_COUNTER, "/TM/VirtualGetSyncSetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling TMTimerGetSync.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualPause, STAMTYPE_COUNTER, "/TM/VirtualPause", STAMUNIT_OCCURENCES, "The number of times TMR3TimerPause was called.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualResume, STAMTYPE_COUNTER, "/TM/VirtualResume", STAMUNIT_OCCURENCES, "The number of times TMR3TimerResume was called.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTimerCallbackSetFF, STAMTYPE_COUNTER, "/TM/CallbackSetFF", STAMUNIT_OCCURENCES, "The number of times the timer callback set FF.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE010, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE010", STAMUNIT_OCCURENCES, "In catch-up mode, 10% or lower.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE025, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE025", STAMUNIT_OCCURENCES, "In catch-up mode, 25%-11%.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE100, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE100", STAMUNIT_OCCURENCES, "In catch-up mode, 100%-26%.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupOther, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupOther", STAMUNIT_OCCURENCES, "In catch-up mode, > 100%.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTSCNotFixed, STAMTYPE_COUNTER, "/TM/TSC/Intercept/NotFixed", STAMUNIT_OCCURENCES, "TSC is not fixed, it may run at variable speed.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTSCNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/NotTicking", STAMUNIT_OCCURENCES, "TSC is not ticking.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTSCSyncNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/SyncNotTicking", STAMUNIT_OCCURENCES, "VirtualSync isn't ticking.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatTSCWarp, STAMTYPE_COUNTER, "/TM/TSC/Intercept/Warp", STAMUNIT_OCCURENCES, "Warpdrive is active.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncCatchup, STAMTYPE_PROFILE_ADV, "/TM/VirtualSync/CatchUp", STAMUNIT_TICKS_PER_OCCURENCE, "Counting and measuring the times spent catching up.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, (void *)&pVM->tm.s.fVirtualSyncCatchUp, STAMTYPE_U8, "/TM/VirtualSync/CatchUpActive", STAMUNIT_NONE, "Catch-Up active indicator.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, (void *)&pVM->tm.s.u32VirtualSyncCatchUpPercentage, STAMTYPE_U32, "/TM/VirtualSync/CatchUpPercentage", STAMUNIT_PCT, "The catch-up percentage. (+100/100 to get clock multiplier)");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGiveUp, STAMTYPE_COUNTER, "/TM/VirtualSync/GiveUp", STAMUNIT_OCCURENCES, "Times the catch-up was abandoned.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGiveUpBeforeStarting, STAMTYPE_COUNTER, "/TM/VirtualSync/GiveUpBeforeStarting",STAMUNIT_OCCURENCES, "Times the catch-up was abandoned before even starting. (Typically debugging++.)");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRun, STAMTYPE_COUNTER, "/TM/VirtualSync/Run", STAMUNIT_OCCURENCES, "Times the virtual sync timer queue was considered.");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunRestart, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/Restarts", STAMUNIT_OCCURENCES, "Times the clock was restarted after a run.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunStop, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/Stop", STAMUNIT_OCCURENCES, "Times the clock was stopped when calculating the current time before examining the timers.");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunStoppedAlready, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/StoppedAlready", STAMUNIT_OCCURENCES, "Times the clock was already stopped elsewhere (TMVirtualSyncGet).");
956a0e3c076406b83d635174a201fd8761ee5133vboxsync STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunSlack, STAMTYPE_PROFILE, "/TM/VirtualSync/Run/Slack", STAMUNIT_NS_PER_OCCURENCE, "The scheduling slack. (Catch-up handed out when running timers.)");
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods); i++)
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAMR3RegisterF(pVM, &pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "The catch-up percentage.", "/TM/VirtualSync/Periods/%u", i);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupAdjust[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times adjusted to this period.", "/TM/VirtualSync/Periods/%u/Adjust", i);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupInitial[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times started in this period.", "/TM/VirtualSync/Periods/%u/Initial", i);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync STAMR3RegisterF(pVM, &pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u64Start, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Start of this period (lag).", "/TM/VirtualSync/Periods/%u/Start", i);
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync#endif /* VBOX_WITH_STATISTICS */
2a0a20dee7f474c26cc8f6f9d7aa12c345c2b73bvboxsync * Register info handlers.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync DBGFR3InfoRegisterInternalEx(pVM, "timers", "Dumps all timers. No arguments.", tmR3TimerInfo, DBGFINFO_FLAGS_RUN_ON_EMT);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync DBGFR3InfoRegisterInternalEx(pVM, "activetimers", "Dumps active all timers. No arguments.", tmR3TimerInfoActive, DBGFINFO_FLAGS_RUN_ON_EMT);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync DBGFR3InfoRegisterInternalEx(pVM, "clocks", "Display the time of the various clocks.", tmR3InfoClocks, DBGFINFO_FLAGS_RUN_ON_EMT);
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Initializes the per-VCPU TM.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @returns VBox status code.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @param pVM The VM to operate on.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * Checks if the host CPU has a fixed TSC frequency.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @returns true if it has, false if it hasn't.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * @remark This test doesn't bother with very old CPUs that don't do power
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * management or any other stuff that might influence the TSC rate.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * This isn't currently relevant.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * AuthenticAMD - Check for APM support and that TscInvariant is set.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * This test isn't correct with respect to fixed/non-fixed TSC and
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * older models, but this isn't relevant since the result is currently
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * only used for making a descision on AMD-V models.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync if ( (uEDX & X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR) /* TscInvariant */
956a0e3c076406b83d635174a201fd8761ee5133vboxsync && pGip->u32Mode == SUPGIPMODE_SYNC_TSC /* no fixed tsc if the gip timer is in async mode */)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync return true;
956a0e3c076406b83d635174a201fd8761ee5133vboxsync else if (CPUMGetCPUVendor(pVM) == CPUMCPUVENDOR_INTEL)
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * GenuineIntel - Check the model number.
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * This test is lacking in the same way and for the same reasons
956a0e3c076406b83d635174a201fd8761ee5133vboxsync * as the AMD test above.
if ( pGip
AssertReleaseMsgFailed(("iCpu=%d - the ApicId is too high. send VBox.log and hardware specs!\n", iCpu));
if ( pGip
return u64Hz;
RTTimeNanoTS();
unsigned cMillies;
unsigned iHigh = 0;
unsigned iLow = 0;
iLow = i;
iHigh = i;
return u64Hz;
int rc;
rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataRC.pfnBad);
rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataRC.pfnRediscover);
rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataR0.pfnBad);
rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataR0.pfnRediscover);
return VINF_SUCCESS;
int rc;
rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataRC.pfnBad);
rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataRC.pfnRediscover);
return VINF_SUCCESS;
LogRel(("TM: Aborting catch-up attempt on reset with a %RU64 ns lag on reset; new total: %RU64 ns\n", offNew - offOld, offNew));
for (int i = 0; i < TMCLOCK_MAX; i++)
#ifdef VBOX_STRICT
return VERR_SYMBOL_NOT_FOUND;
return VINF_SUCCESS;
return rc;
return VERR_SSM_VIRTUAL_CLOCK_HZ;
return rc;
return rc;
LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool (state load)\n",
pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC));
return VINF_SUCCESS;
if (!pTimer)
return rc;
#ifdef VBOX_STRICT
return VINF_SUCCESS;
VMMR3DECL(int) TMR3TimerCreateDevice(PVM pVM, PPDMDEVINS pDevIns, TMCLOCK enmClock, PFNTMTIMERDEV pfnCallback, const char *pszDesc, PPTMTIMERR3 ppTimer)
Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
return rc;
VMMR3DECL(int) TMR3TimerCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, const char *pszDesc, PPTMTIMERR3 ppTimer)
Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
return rc;
VMMR3DECL(int) TMR3TimerCreateInternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMERINT pfnCallback, void *pvUser, const char *pszDesc, PPTMTIMERR3 ppTimer)
Log(("TM: Created internal timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
return rc;
VMMR3DECL(PTMTIMERR3) TMR3TimerCreateExternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMEREXT pfnCallback, void *pvUser, const char *pszDesc)
Log(("TM: Created external timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
return pTimer;
return NULL;
if (!pDevIns)
return VERR_INVALID_PARAMETER;
while (pCur)
return VINF_SUCCESS;
if (!pDrvIns)
return VERR_INVALID_PARAMETER;
while (pCur)
return VINF_SUCCESS;
switch (enmClock)
return ~(uint64_t)0;
#ifdef VBOX_STRICT
if (!pNext)
Log2(("tmR3TimerQueueRun: pTimer=%p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
bool fRc;
if (fRc)
if (pPrev)
if (pNext)
case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->u.Internal.pvUser); break;
uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
Log4(("TM: %RU64/%RU64: sub %RU64 (run)\n", u64VirtualNow - off, off - pVM->tm.s.offVirtualSyncGivenUp, u64Sub));
Log4(("TM: %RU64/%RU64: exp tmr (run)\n", u64Now, u64VirtualNow - u64Now - pVM->tm.s.offVirtualSyncGivenUp));
#ifdef VBOX_STRICT
Log2(("tmR3TimerQueueRun: pTimer=%p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
bool fRc;
if (fRc)
if (pPrev)
if (pNext)
#ifdef VBOX_STRICT
AssertMsg(pTimer->u64Expire >= u64Prev, ("%RU64 < %RU64 %s\n", pTimer->u64Expire, u64Prev, pTimer->pszDesc));
case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->u.Internal.pvUser); break;
AssertMsg(pVM->tm.s.u64VirtualSync >= u64Now, ("%RU64 < %RU64\n", pVM->tm.s.u64VirtualSync, u64Now));
if (offSlack)
p->cPeriods++;
u64Elapsed = 0;
if (pVM->tm.s.u32VirtualSyncCatchUpPercentage < pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage)
ASMAtomicXchgU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
Log4(("TM: %RU64/%RU64: adj %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
Log4(("TM: %RU64/%RU64: give up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
LogRel(("TM: Giving up catch-up attempt at a %RU64 ns lag; new total: %RU64 ns\n", offLag, offNew));
ASMAtomicXchgU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
Log4(("TM: %RU64/%RU64: catch-up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
LogRel(("TM: Not bothering to attempt catching up a %RU64 ns lag; new total: %RU64\n", offLag, offNew));
LogFlow(("TMR3TimerSave: pTimer=%p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
case TMTIMERSTATE_STOPPED:
if (!RTThreadYield())
case TMTIMERSTATE_ACTIVE:
case TMTIMERSTATE_EXPIRED:
case TMTIMERSTATE_FREE:
AssertMsgFailed(("Invalid timer state %d %s (%s)\n", pTimer->enmState, tmTimerState(pTimer->enmState), pTimer->pszDesc));
LogFlow(("TMR3TimerLoad: pTimer=%p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
return rc;
return rc;
return rc;
return pTime;
pVM,
pVM,