timer-r0drv-linux.c revision 0cde281779e97ead3181bbd3b628451fa2b1efe1
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Timers, Ring-0 Driver, Linux.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2010 Oracle Corporation
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * available from http://www.virtualbox.org. This file is free software;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync/*******************************************************************************
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync* Header Files *
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync*******************************************************************************/
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/** @def RTTIMER_LINUX_HAVE_HRTIMER
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Whether the kernel support high resolution timers (Linux kernel versions
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * 2.6.28 and later (hrtimer_add_expires_ns()). */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/** @def RTTIMER_LINUX_ONLY_HRTIMER
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Whether to use high resolution timers for everything or not. Implies
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * RTTIMER_LINUX_WITH_HRTIMER. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync && 0 /* currently disabled */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/* This check must match the ktime usage in rtTimeGetSystemNanoTS() / time-r0drv-linux.c. */
b34f0f200ed0778053a2a1d93381c2c6b60cb2d5vboxsync# error "RTTIMER_LINUX_ONLY_HRTIMER requires 2.6.28 or later, sorry."
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/** @def RTTIMER_LINUX_WITH_HRTIMER
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Whether to use high resolution timers. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/*******************************************************************************
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync* Structures and Typedefs *
249acff39649895e369c7d0c07d3d0adc4e10f38vboxsync*******************************************************************************/
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Timer state machine.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * This is used to try handle the issues with MP events and
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * timers that runs on all CPUs. It's relatively nasty :-/
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Stopped. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Transient state; next ACTIVE. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Transient state; next ACTIVE. (not really necessary) */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Active. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Transient state; next STOPPED. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Transient state; next STOPPED. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** The usual 32-bit hack. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * A Linux sub-timer.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Timer specific data. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** High resolution timer. */
b34f0f200ed0778053a2a1d93381c2c6b60cb2d5vboxsync /** The linux timer structure. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Standard timer. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** The linux timer structure. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** The start of the current run (ns).
8c99dcd207cf5b7bee01f95fbe19728a94076f94vboxsync * This is used to calculate when the timer ought to fire the next time. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** The start of the current run (ns).
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * This is used to calculate when the timer ought to fire the next time. */
8c99dcd207cf5b7bee01f95fbe19728a94076f94vboxsync /** The u64NextTS in jiffies. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync unsigned long ulNextJiffies;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** The current tick number (since u.Std.u64StartTS). */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Pointer to the parent timer. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** The current sub-timer state. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/** Pointer to a linux sub-timer. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * The internal representation of an Linux timer handle.
8c99dcd207cf5b7bee01f95fbe19728a94076f94vboxsynctypedef struct RTTIMER
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * This is RTTIMER_MAGIC, but changes to something else before the timer
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * is destroyed to indicate clearly that thread should exit. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Spinlock synchronizing the fSuspended and MP event handling.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * This is NIL_RTSPINLOCK if cCpus == 1. */
8c99dcd207cf5b7bee01f95fbe19728a94076f94vboxsync /** Flag indicating that the timer is suspended. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync bool volatile fSuspended;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Whether the timer must run on one specific CPU or not. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Whether the timer must run on all CPUs or not. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync#endif /* else: All -> specific on non-SMP kernels */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Whether it is a high resolution timer or a standard one. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** The id of the CPU it must run on if fSpecificCpu is set. */
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync /** The number of CPUs this timer should run on. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** Callback. */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** User argument. */
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync /** The timer interval. 0 if one-shot. */
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync /** This is set to the number of jiffies between ticks if the interval is
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync * an exact number of jiffies. */
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync unsigned long cJiffies;
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync /** Sub-timers.
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync * Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
727cf0d6c794dffee1023d2bbb6e94eda7f35ca9vboxsync * an entry for all possible cpus. In that case the index will be the same as
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync * for the RTCpuSet. */
56665b35e85d3cd7d4232957d09b488ab44d81cfvboxsync * A rtTimerLinuxStartOnCpu and rtTimerLinuxStartOnCpu argument package.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** The current time (RTTimeNanoTS). */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /** When to start firing (delta). */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/** Pointer to a rtTimerLinuxStartOnCpu argument package. */
b34f0f200ed0778053a2a1d93381c2c6b60cb2d5vboxsynctypedef RTTIMERLINUXSTARTONCPUARGS *PRTTIMERLINUXSTARTONCPUARGS;
b34f0f200ed0778053a2a1d93381c2c6b60cb2d5vboxsync * Sets the state.
b34f0f200ed0778053a2a1d93381c2c6b60cb2d5vboxsyncDECLINLINE(void) rtTimerLnxSetState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync ASMAtomicWriteU32((uint32_t volatile *)penmState, enmNewState);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Sets the state if it has a certain value.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * @return true if xchg was done.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * @return false if xchg wasn't done.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsyncDECLINLINE(bool) rtTimerLnxCmpXchgState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState, RTTIMERLNXSTATE enmCurState)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync return ASMAtomicCmpXchgU32((uint32_t volatile *)penmState, enmNewState, enmCurState);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Gets the state.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsyncDECLINLINE(RTTIMERLNXSTATE) rtTimerLnxGetState(RTTIMERLNXSTATE volatile *penmState)
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync return (RTTIMERLNXSTATE)ASMAtomicUoReadU32((uint32_t volatile *)penmState);
727cf0d6c794dffee1023d2bbb6e94eda7f35ca9vboxsync * Converts a nano second time stamp to ktime_t.
727cf0d6c794dffee1023d2bbb6e94eda7f35ca9vboxsync * ASSUMES RTTimeNanoTS() is implemented using ktime_get_ts().
727cf0d6c794dffee1023d2bbb6e94eda7f35ca9vboxsync * @returns ktime_t.
727cf0d6c794dffee1023d2bbb6e94eda7f35ca9vboxsync * @param cNanoSecs Nanoseconds.
727cf0d6c794dffee1023d2bbb6e94eda7f35ca9vboxsyncDECLINLINE(ktime_t) rtTimerLnxNanoToKt(uint64_t cNanoSecs)
#ifndef RTTIMER_LINUX_ONLY_HRTIMER
return MAX_JIFFY_OFFSET;
static void rtTimerLnxStartSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, uint64_t u64Now, uint64_t u64First,
#ifndef RTTIMER_LINUX_ONLY_HRTIMER
if (fHighRes)
#ifdef RTTIMER_LINUX_WITH_HRTIMER
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
if (fHighRes)
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
#ifndef RTTIMER_LINUX_ONLY_HRTIMER
# ifdef CONFIG_SMP
if (fPinned)
#ifdef RTTIMER_LINUX_WITH_HRTIMER
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
#ifdef RTTIMER_LINUX_WITH_HRTIMER
#ifdef CONFIG_SMP
return rc;
#ifndef RTTIMER_LINUX_ONLY_HRTIMER
#ifdef CONFIG_SMP
pSubTimer->u.Std.ulNextJiffies = jiffies + rtTimerLnxNanoToJiffies(pSubTimer->u.Std.u64NextTS - u64NanoTS);
# ifdef CONFIG_SMP
#ifdef CONFIG_SMP
* Per-cpu callback function (RTMpOnAll/RTMpOnSpecific).
rtTimerLnxStartSubTimer(&pTimer->aSubTimers[idCpu], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
int rc2;
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_STARTING))
return VINF_SUCCESS;
} while (!rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPING, enmState));
return VINF_SUCCESS;
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
rtTimerLnxStartSubTimer(pSubTimer, pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
switch (enmEvent)
case RTMPEVENT_ONLINE:
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
case RTMPEVENT_OFFLINE:
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STOPPING, RTTIMERLNXSTATE_ACTIVE))
rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
int rc2;
return VERR_TIMER_ACTIVE;
#ifdef CONFIG_SMP
rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], Args.u64Now, Args.u64First, false /*fPinned*/, pTimer->fHighRes);
return rc2;
return VINF_SUCCESS;
return VERR_TIMER_SUSPENDED;
#ifdef CONFIG_SMP
return VINF_SUCCESS;
#ifdef RTTIMER_LINUX_WITH_HRTIMER
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
#ifndef RTTIMER_LINUX_ONLY_HRTIMER
return rc;
return VINF_SUCCESS;
#ifdef CONFIG_SMP
return VINF_SUCCESS;
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
unsigned cCpus;
return VERR_INVALID_PARAMETER;
return VERR_CPU_NOT_FOUND;
#ifdef CONFIG_SMP
Assert(cCpus <= RTCPUSET_MAX_CPUS); /* On linux we have a 1:1 relationship between cpuid and set index. */
AssertReturn(u64NanoInterval, VERR_NOT_IMPLEMENTED); /* We don't implement single shot on all cpus, sorry. */
if (!pTimer)
return VERR_NO_MEMORY;
#ifdef CONFIG_SMP
pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
pTimer->idCpu = pTimer->fSpecificCpu ? RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK) : NIL_RTCPUID;
#ifndef RTTIMER_LINUX_ONLY_HRTIMER
#ifdef RTTIMER_LINUX_WITH_HRTIMER
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
# ifndef RTTIMER_LINUX_ONLY_HRTIMER
#ifndef RTTIMER_LINUX_ONLY_HRTIMER
#ifdef CONFIG_SMP
return rc;
return VINF_SUCCESS;
#ifdef RTTIMER_LINUX_ONLY_HRTIMER
if (!rc)
return VERR_NOT_SUPPORTED;
return VERR_NOT_SUPPORTED;
#ifdef RTTIMER_LINUX_WITH_HRTIMER