tm.h revision e51fe4b097229c66ff098ddbfa63c5054e0fa176
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/** @file
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * TM - Time Manager. (VMM)
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/*
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * Copyright (C) 2006-2010 Oracle Corporation
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync *
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * available from http://www.virtualbox.org. This file is free software;
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * you can redistribute it and/or modify it under the terms of the GNU
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * General Public License (GPL) as published by the Free Software
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync *
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * The contents of this file may alternatively be used under the terms
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * of the Common Development and Distribution License Version 1.0
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * VirtualBox OSE distribution, in which case the provisions of the
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * CDDL are applicable instead of those of the GPL.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync *
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * You may elect to license modified versions of this file under the
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * terms and conditions of either the GPL or the CDDL or both.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#ifndef ___VBox_tm_h
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#define ___VBox_tm_h
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#include <VBox/cdefs.h>
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#include <VBox/types.h>
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#ifdef IN_RING3
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync# include <iprt/time.h>
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#endif
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncRT_C_DECLS_BEGIN
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync/** @defgroup grp_tm The Time Manager API
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * @{
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/** Enable a timer hack which improves the timer response/resolution a bit. */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#define VBOX_HIGH_RES_TIMERS_HACK
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/**
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * Clock type.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsynctypedef enum TMCLOCK
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync{
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync /** Real host time.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * This clock ticks all the time, so use with care. */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync TMCLOCK_REAL = 0,
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync /** Virtual guest time.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * This clock only ticks when the guest is running. It's implemented
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * as an offset to monotonic real time (GIP). */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync TMCLOCK_VIRTUAL,
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync /** Virtual guest synchronized timer time.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * This is a special clock and timer queue for synchronizing virtual timers
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * and virtual time sources. This clock is trying to keep up with
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * TMCLOCK_VIRTUAL, but will wait for timers to be executed. If it lags
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * too far behind TMCLOCK_VIRTUAL, it will try speed up to close the
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * distance.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * @remarks Do not use this unless you really *must*. */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync TMCLOCK_VIRTUAL_SYNC,
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync /** Virtual CPU timestamp.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * By default this is a function of TMCLOCK_VIRTUAL_SYNC and the virtual
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * CPU frequency. */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync TMCLOCK_TSC,
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync /** Number of clocks. */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync TMCLOCK_MAX
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync} TMCLOCK;
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/** @defgroup grp_tm_timer_flags Timer flags.
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * @{ */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/** Use the default critical section for the class of timers. */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#define TMTIMER_FLAGS_DEFAULT_CRIT_SECT 0
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/** No critical section needed or a custom one is set using
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * TMR3TimerSetCritSect(). */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#define TMTIMER_FLAGS_NO_CRIT_SECT RT_BIT_32(0)
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/** @} */
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMMDECL(void) TMNotifyStartOfExecution(PVMCPU pVCpu);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMMDECL(void) TMNotifyEndOfExecution(PVMCPU pVCpu);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMM_INT_DECL(void) TMNotifyStartOfHalt(PVMCPU pVCpu);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMM_INT_DECL(void) TMNotifyEndOfHalt(PVMCPU pVCpu);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#ifdef IN_RING3
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMMR3DECL(int) TMR3NotifySuspend(PVM pVM, PVMCPU pVCpu);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMMR3DECL(int) TMR3NotifyResume(PVM pVM, PVMCPU pVCpu);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMMR3DECL(int) TMR3SetWarpDrive(PVM pVM, uint32_t u32Percent);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync#endif
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMMDECL(uint32_t) TMGetWarpDrive(PVM pVM);
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsync#ifdef IN_RING3
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsyncVMMR3DECL(int) TMR3GetCpuLoadTimes(PVM pVM, VMCPUID idCpu, uint64_t *pcNsTotal, uint64_t *pcNsExecuting,
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsync uint64_t *pcNsHalted, uint64_t *pcNsOther);
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsync#endif
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsync
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsync
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsync/** @name Real Clock Methods
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsync * @{
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsync */
6e59cff536e8de93f15fa1fb6b46d0ee01e06829vboxsyncVMM_INT_DECL(uint64_t) TMRealGet(PVM pVM);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsyncVMM_INT_DECL(uint64_t) TMRealGetFreq(PVM pVM);
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/** @} */
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync/** @name Virtual Clock Methods
33b0e5fd502b10034575ffa7c8a30c6816222ce2vboxsync * @{
*/
VMM_INT_DECL(uint64_t) TMVirtualGet(PVM pVM);
VMM_INT_DECL(uint64_t) TMVirtualGetNoCheck(PVM pVM);
VMM_INT_DECL(uint64_t) TMVirtualSyncGetLag(PVM pVM);
VMM_INT_DECL(uint32_t) TMVirtualSyncGetCatchUpPct(PVM pVM);
VMM_INT_DECL(uint64_t) TMVirtualGetFreq(PVM pVM);
VMM_INT_DECL(uint64_t) TMVirtualSyncGet(PVM pVM);
VMM_INT_DECL(uint64_t) TMVirtualSyncGetNoCheck(PVM pVM);
VMM_INT_DECL(uint64_t) TMVirtualSyncGetEx(PVM pVM, bool fCheckTimers);
VMM_INT_DECL(uint64_t) TMVirtualSyncGetWithDeadlineNoCheck(PVM pVM, uint64_t *pcNsToDeadline);
VMM_INT_DECL(uint64_t) TMVirtualSyncGetNsToDeadline(PVM pVM);
VMM_INT_DECL(uint64_t) TMVirtualToNano(PVM pVM, uint64_t u64VirtualTicks);
VMM_INT_DECL(uint64_t) TMVirtualToMicro(PVM pVM, uint64_t u64VirtualTicks);
VMM_INT_DECL(uint64_t) TMVirtualToMilli(PVM pVM, uint64_t u64VirtualTicks);
VMM_INT_DECL(uint64_t) TMVirtualFromNano(PVM pVM, uint64_t u64NanoTS);
VMM_INT_DECL(uint64_t) TMVirtualFromMicro(PVM pVM, uint64_t u64MicroTS);
VMM_INT_DECL(uint64_t) TMVirtualFromMilli(PVM pVM, uint64_t u64MilliTS);
/** @} */
/** @name CPU Clock Methods
* @{
*/
VMMDECL(uint64_t) TMCpuTickGet(PVMCPU pVCpu);
VMM_INT_DECL(uint64_t) TMCpuTickGetNoCheck(PVMCPU pVCpu);
VMM_INT_DECL(bool) TMCpuTickCanUseRealTSC(PVMCPU pVCpu, uint64_t *poffRealTSC);
VMM_INT_DECL(uint64_t) TMCpuTickGetDeadlineAndTscOffset(PVMCPU pVCpu, bool *pfOffsettedTsc, uint64_t *poffRealTSC);
VMM_INT_DECL(int) TMCpuTickSet(PVM pVM, PVMCPU pVCpu, uint64_t u64Tick);
VMM_INT_DECL(int) TMCpuTickSetLastSeen(PVMCPU pVCpu, uint64_t u64LastSeenTick);
VMM_INT_DECL(uint64_t) TMCpuTickGetLastSeen(PVMCPU pVCpu);
VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM);
/** @} */
/** @name Timer Methods
* @{
*/
/**
* Device timer callback function.
*
* @param pDevIns Device instance of the device which registered the timer.
* @param pTimer The timer handle.
* @param pvUser User argument specified upon timer creation.
*/
typedef DECLCALLBACK(void) FNTMTIMERDEV(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser);
/** Pointer to a device timer callback function. */
typedef FNTMTIMERDEV *PFNTMTIMERDEV;
/**
* Device timer callback function.
*
* @param pUsbIns The USB device instance the timer is associated
* with.
* @param pTimer The timer handle.
* @param pvUser User argument specified upon timer creation.
*/
typedef DECLCALLBACK(void) FNTMTIMERUSB(PPDMUSBINS pUsbIns, PTMTIMER pTimer, void *pvUser);
/** Pointer to a timer callback for a USB device. */
typedef FNTMTIMERUSB *PFNTMTIMERUSB;
/**
* Driver timer callback function.
*
* @param pDrvIns Device instance of the device which registered the timer.
* @param pTimer The timer handle.
* @param pvUser User argument specified upon timer creation.
*/
typedef DECLCALLBACK(void) FNTMTIMERDRV(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser);
/** Pointer to a driver timer callback function. */
typedef FNTMTIMERDRV *PFNTMTIMERDRV;
/**
* Service timer callback function.
*
* @param pSrvIns Service instance of the device which registered the timer.
* @param pTimer The timer handle.
*/
typedef DECLCALLBACK(void) FNTMTIMERSRV(PPDMSRVINS pSrvIns, PTMTIMER pTimer);
/** Pointer to a service timer callback function. */
typedef FNTMTIMERSRV *PFNTMTIMERSRV;
/**
* Internal timer callback function.
*
* @param pVM The VM.
* @param pTimer The timer handle.
* @param pvUser User argument specified upon timer creation.
*/
typedef DECLCALLBACK(void) FNTMTIMERINT(PVM pVM, PTMTIMER pTimer, void *pvUser);
/** Pointer to internal timer callback function. */
typedef FNTMTIMERINT *PFNTMTIMERINT;
/**
* External timer callback function.
*
* @param pvUser User argument as specified when the timer was created.
*/
typedef DECLCALLBACK(void) FNTMTIMEREXT(void *pvUser);
/** Pointer to an external timer callback function. */
typedef FNTMTIMEREXT *PFNTMTIMEREXT;
VMMDECL(PTMTIMERR3) TMTimerR3Ptr(PTMTIMER pTimer);
VMMDECL(PTMTIMERR0) TMTimerR0Ptr(PTMTIMER pTimer);
VMMDECL(PTMTIMERRC) TMTimerRCPtr(PTMTIMER pTimer);
VMMDECL(int) TMTimerSet(PTMTIMER pTimer, uint64_t u64Expire);
VMMDECL(int) TMTimerSetRelative(PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now);
VMMDECL(int) TMTimerSetMillies(PTMTIMER pTimer, uint32_t cMilliesToNext);
VMMDECL(int) TMTimerSetMicro(PTMTIMER pTimer, uint64_t cMicrosToNext);
VMMDECL(int) TMTimerSetNano(PTMTIMER pTimer, uint64_t cNanosToNext);
VMMDECL(int) TMTimerSetFrequencyHint(PTMTIMER pTimer, uint32_t uHz);
VMMDECL(uint64_t) TMTimerGet(PTMTIMER pTimer);
VMMDECL(uint64_t) TMTimerGetNano(PTMTIMER pTimer);
VMMDECL(uint64_t) TMTimerGetMicro(PTMTIMER pTimer);
VMMDECL(uint64_t) TMTimerGetMilli(PTMTIMER pTimer);
VMMDECL(uint64_t) TMTimerGetFreq(PTMTIMER pTimer);
VMMDECL(uint64_t) TMTimerGetExpire(PTMTIMER pTimer);
VMMDECL(uint64_t) TMTimerToNano(PTMTIMER pTimer, uint64_t u64Ticks);
VMMDECL(uint64_t) TMTimerToMicro(PTMTIMER pTimer, uint64_t u64Ticks);
VMMDECL(uint64_t) TMTimerToMilli(PTMTIMER pTimer, uint64_t u64Ticks);
VMMDECL(uint64_t) TMTimerFromNano(PTMTIMER pTimer, uint64_t u64NanoTS);
VMMDECL(uint64_t) TMTimerFromMicro(PTMTIMER pTimer, uint64_t u64MicroTS);
VMMDECL(uint64_t) TMTimerFromMilli(PTMTIMER pTimer, uint64_t u64MilliTS);
VMMDECL(int) TMTimerStop(PTMTIMER pTimer);
VMMDECL(bool) TMTimerIsActive(PTMTIMER pTimer);
VMMDECL(bool) TMTimerPollBool(PVM pVM, PVMCPU pVCpu);
VMM_INT_DECL(void) TMTimerPollVoid(PVM pVM, PVMCPU pVCpu);
VMM_INT_DECL(uint64_t) TMTimerPollGIP(PVM pVM, PVMCPU pVCpu, uint64_t *pu64Delta);
/** @} */
#ifdef IN_RING3
/** @defgroup grp_tm_r3 The TM Host Context Ring-3 API
* @ingroup grp_tm
* @{
*/
VMM_INT_DECL(int) TMR3Init(PVM pVM);
VMM_INT_DECL(int) TMR3InitCPU(PVM pVM);
VMM_INT_DECL(int) TMR3InitFinalize(PVM pVM);
VMM_INT_DECL(void) TMR3Relocate(PVM pVM, RTGCINTPTR offDelta);
VMM_INT_DECL(int) TMR3Term(PVM pVM);
VMM_INT_DECL(int) TMR3TermCPU(PVM pVM);
VMM_INT_DECL(void) TMR3Reset(PVM pVM);
VMM_INT_DECL(int) TMR3GetImportRC(PVM pVM, const char *pszSymbol, PRTRCPTR pRCPtrValue);
VMM_INT_DECL(int) TMR3TimerCreateDevice(PVM pVM, PPDMDEVINS pDevIns, TMCLOCK enmClock, PFNTMTIMERDEV pfnCallback, void *pvUser, uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer);
VMM_INT_DECL(int) TMR3TimerCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, void *pvUser, uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer);
VMMR3DECL(int) TMR3TimerCreateInternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMERINT pfnCallback, void *pvUser, const char *pszDesc, PPTMTIMERR3 ppTimer);
VMMR3DECL(PTMTIMERR3) TMR3TimerCreateExternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMEREXT pfnCallback, void *pvUser, const char *pszDesc);
VMMR3DECL(int) TMR3TimerDestroy(PTMTIMER pTimer);
VMM_INT_DECL(int) TMR3TimerDestroyDevice(PVM pVM, PPDMDEVINS pDevIns);
VMM_INT_DECL(int) TMR3TimerDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns);
VMMR3DECL(int) TMR3TimerSave(PTMTIMERR3 pTimer, PSSMHANDLE pSSM);
VMMR3DECL(int) TMR3TimerLoad(PTMTIMERR3 pTimer, PSSMHANDLE pSSM);
VMMR3DECL(int) TMR3TimerSetCritSect(PTMTIMERR3 pTimer, PPDMCRITSECT pCritSect);
VMMR3DECL(void) TMR3TimerQueuesDo(PVM pVM);
VMM_INT_DECL(void) TMR3VirtualSyncFF(PVM pVM, PVMCPU pVCpu);
VMM_INT_DECL(PRTTIMESPEC) TMR3UtcNow(PVM pVM, PRTTIMESPEC pTime);
/** @} */
#endif /* IN_RING3 */
/** @} */
RT_C_DECLS_END
#endif