timer-r0drv-linux.c revision ad27e1d5e48ca41245120c331cc88b50464813ce
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/* $Id$ */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/** @file
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * IPRT - Timers, Ring-0 Driver, Linux.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/*
b72d3233df38e3122eda39b39a27b35c27209615vboxsync * Copyright (C) 2006-2010 Oracle Corporation
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync *
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * available from http://www.virtualbox.org. This file is free software;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * you can redistribute it and/or modify it under the terms of the GNU
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * General Public License (GPL) as published by the Free Software
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync *
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * The contents of this file may alternatively be used under the terms
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * of the Common Development and Distribution License Version 1.0
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * VirtualBox OSE distribution, in which case the provisions of the
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * CDDL are applicable instead of those of the GPL.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync *
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * You may elect to license modified versions of this file under the
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * terms and conditions of either the GPL or the CDDL or both.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/*******************************************************************************
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync* Header Files *
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync*******************************************************************************/
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include "the-linux-kernel.h"
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include "internal/iprt.h"
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <iprt/timer.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <iprt/time.h>
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#include <iprt/mp.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <iprt/cpuset.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include <iprt/spinlock.h>
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#include <iprt/err.h>
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#include <iprt/asm.h>
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#include <iprt/assert.h>
b72d3233df38e3122eda39b39a27b35c27209615vboxsync#include <iprt/alloc.h>
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#include "internal/magics.h"
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync/** @def RTTIMER_LINUX_WITH_HRTIMER
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync * Whether to use high resolution timers. */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#if !defined(RTTIMER_LINUX_WITH_HRTIMER) \
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync && defined(IPRT_LINUX_HAS_HRTIMER)
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync# define RTTIMER_LINUX_WITH_HRTIMER
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#endif
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync# define mod_timer_pinned mod_timer
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync# define HRTIMER_MODE_ABS_PINNED HRTIMER_MODE_ABS
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#endif
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync/*******************************************************************************
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync* Structures and Typedefs *
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync*******************************************************************************/
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync/**
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync * Timer state machine.
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync *
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync * This is used to try handle the issues with MP events and
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync * timers that runs on all CPUs. It's relatively nasty :-/
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsynctypedef enum RTTIMERLNXSTATE
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync{
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Stopped. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync RTTIMERLNXSTATE_STOPPED = 0,
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /** Transient state; next ACTIVE. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTTIMERLNXSTATE_STARTING,
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /** Transient state; next ACTIVE. (not really necessary) */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTTIMERLNXSTATE_MP_STARTING,
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /** Active. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTTIMERLNXSTATE_ACTIVE,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Active and in callback; next ACTIVE, STOPPED or CALLBACK_DESTROYING. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync RTTIMERLNXSTATE_CALLBACK,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Stopped while in the callback; next STOPPED. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync RTTIMERLNXSTATE_CB_STOPPING,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Restarted while in the callback; next ACTIVE, STOPPED, DESTROYING. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync RTTIMERLNXSTATE_CB_RESTARTING,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** The callback shall destroy the timer; next STOPPED. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync RTTIMERLNXSTATE_CB_DESTROYING,
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Transient state; next STOPPED. */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync RTTIMERLNXSTATE_STOPPING,
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync /** Transient state; next STOPPED. */
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync RTTIMERLNXSTATE_MP_STOPPING,
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync /** The usual 32-bit hack. */
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync RTTIMERLNXSTATE_32BIT_HACK = 0x7fffffff
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync} RTTIMERLNXSTATE;
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/**
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * A Linux sub-timer.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsynctypedef struct RTTIMERLNXSUBTIMER
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync{
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Timer specific data. */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync union
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync {
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#if defined(RTTIMER_LINUX_WITH_HRTIMER)
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** High resolution timer. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync struct
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync {
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** The linux timer structure. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync struct hrtimer LnxTimer;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync } Hr;
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync#endif
0368e9c310393e82ef37c480b6acbd0f107cf0edvboxsync /** Standard timer. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync struct
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync {
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** The linux timer structure. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync struct timer_list LnxTimer;
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync /** The start of the current run (ns).
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * This is used to calculate when the timer ought to fire the next time. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync uint64_t u64NextTS;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** The u64NextTS in jiffies. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync unsigned long ulNextJiffies;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** Set when starting or changing the timer so that u64StartTs
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * and u64NextTS gets reinitialized (eliminating some jitter). */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync bool volatile fFirstAfterChg;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync } Std;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync } u;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** The current tick number. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync uint64_t iTick;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** Restart the single shot timer at this specific time.
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * Used when a single shot timer is restarted from the callback. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync uint64_t volatile uNsRestartAt;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** Pointer to the parent timer. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync PRTTIMER pParent;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** The current sub-timer state. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync RTTIMERLNXSTATE volatile enmState;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync} RTTIMERLNXSUBTIMER;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/** Pointer to a linux sub-timer. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsynctypedef RTTIMERLNXSUBTIMER *PRTTIMERLNXSUBTIMER;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync/**
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * The internal representation of an Linux timer handle.
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsynctypedef struct RTTIMER
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync{
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Magic.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * This is RTTIMER_MAGIC, but changes to something else before the timer
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * is destroyed to indicate clearly that thread should exit. */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync uint32_t volatile u32Magic;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Spinlock synchronizing the fSuspended and MP event handling.
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * This is NIL_RTSPINLOCK if cCpus == 1. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync RTSPINLOCK hSpinlock;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** Flag indicating that the timer is suspended. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync bool volatile fSuspended;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** Whether the timer must run on one specific CPU or not. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync bool fSpecificCpu;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#ifdef CONFIG_SMP
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Whether the timer must run on all CPUs or not. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync bool fAllCpus;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#endif /* else: All -> specific on non-SMP kernels */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** Whether it is a high resolution timer or a standard one. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync bool fHighRes;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** The id of the CPU it must run on if fSpecificCpu is set. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync RTCPUID idCpu;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** The number of CPUs this timer should run on. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync RTCPUID cCpus;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** Callback. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync PFNRTTIMER pfnTimer;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** User argument. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync void *pvUser;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** The timer interval. 0 if one-shot. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync uint64_t volatile u64NanoInterval;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** This is set to the number of jiffies between ticks if the interval is
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * an exact number of jiffies. (Standard timers only.) */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync unsigned long volatile cJiffies;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync /** The change interval spinlock for standard timers only. */
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync spinlock_t ChgIntLock;
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync /** Sub-timers.
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * an entry for all possible cpus. In that case the index will be the same as
d1c36fd86d36726777e3d6f9d040573e0aaf30devboxsync * for the RTCpuSet. */
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync RTTIMERLNXSUBTIMER aSubTimers[1];
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync} RTTIMER;
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync/**
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync * A rtTimerLinuxStartOnCpu and rtTimerLinuxStartOnCpu argument package.
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync */
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsynctypedef struct RTTIMERLINUXSTARTONCPUARGS
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync{
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync /** The current time (RTTimeSystemNanoTS). */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync uint64_t u64Now;
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync /** When to start firing (delta). */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync uint64_t u64First;
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync} RTTIMERLINUXSTARTONCPUARGS;
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync/** Pointer to a rtTimerLinuxStartOnCpu argument package. */
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsynctypedef RTTIMERLINUXSTARTONCPUARGS *PRTTIMERLINUXSTARTONCPUARGS;
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync/*******************************************************************************
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync* Internal Functions *
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync*******************************************************************************/
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync#ifdef CONFIG_SMP
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsyncstatic DECLCALLBACK(void) rtTimerLinuxMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser);
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync#endif
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync#if 0
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync#define DEBUG_HACKING
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync#include <iprt/string.h>
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync#include <iprt/asm-amd64-x86.h>
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsyncstatic void myLogBackdoorPrintf(const char *pszFormat, ...)
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync{
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync char szTmp[256];
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync va_list args;
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync size_t cb;
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync cb = RTStrPrintf(szTmp, sizeof(szTmp) - 10, "%d: ", RTMpCpuId());
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync va_start(args, pszFormat);
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync cb += RTStrPrintfV(&szTmp[cb], sizeof(szTmp) - cb, pszFormat, args);
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync va_end(args);
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync ASMOutStrU8(0x504, (uint8_t *)&szTmp[0], cb);
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync}
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync# define RTAssertMsg1Weak(pszExpr, uLine, pszFile, pszFunction) \
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync myLogBackdoorPrintf("\n!!Guest Assertion failed!!\n%s(%d) %s\n%s\n", uLine, pszFile, pszFunction, (pszExpr))
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync# define RTAssertMsg2Weak myLogBackdoorPrintf
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync# define RTTIMERLNX_LOG(a) myLogBackdoorPrintf a
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync#else
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync# define RTTIMERLNX_LOG(a) do { } while (0)
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync#endif
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync/**
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync * Sets the state.
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync */
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsyncDECLINLINE(void) rtTimerLnxSetState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState)
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync{
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#ifdef DEBUG_HACKING
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync RTTIMERLNX_LOG(("set %d -> %d\n", *penmState, enmNewState));
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#endif
b72d3233df38e3122eda39b39a27b35c27209615vboxsync ASMAtomicWriteU32((uint32_t volatile *)penmState, enmNewState);
b72d3233df38e3122eda39b39a27b35c27209615vboxsync}
b72d3233df38e3122eda39b39a27b35c27209615vboxsync
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync/**
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync * Sets the state if it has a certain value.
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync *
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync * @return true if xchg was done.
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync * @return false if xchg wasn't done.
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync */
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#ifdef DEBUG_HACKING
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync#define rtTimerLnxCmpXchgState(penmState, enmNewState, enmCurState) rtTimerLnxCmpXchgStateDebug(penmState, enmNewState, enmCurState, __LINE__)
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsyncstatic bool rtTimerLnxCmpXchgStateDebug(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState,
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync RTTIMERLNXSTATE enmCurState, uint32_t uLine)
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync{
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync RTTIMERLNXSTATE enmOldState = enmCurState;
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync bool fRc = ASMAtomicCmpXchgExU32((uint32_t volatile *)penmState, enmNewState, enmCurState, (uint32_t *)&enmOldState);
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync RTTIMERLNX_LOG(("cxg %d -> %d - %d at %u\n", enmOldState, enmNewState, fRc, uLine));
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync return fRc;
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync}
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync#else
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsyncDECLINLINE(bool) rtTimerLnxCmpXchgState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState,
39c2eccedfdb7455c52225543c355e33a65f0c81vboxsync RTTIMERLNXSTATE enmCurState)
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync{
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync return ASMAtomicCmpXchgU32((uint32_t volatile *)penmState, enmNewState, enmCurState);
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync}
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync#endif
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync
b35e3948f1287430503b6b432945b8cf4bfd3a23vboxsync/**
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * Gets the state.
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync */
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsyncDECLINLINE(RTTIMERLNXSTATE) rtTimerLnxGetState(RTTIMERLNXSTATE volatile *penmState)
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync{
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync return (RTTIMERLNXSTATE)ASMAtomicUoReadU32((uint32_t volatile *)penmState);
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync}
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync#ifdef RTTIMER_LINUX_WITH_HRTIMER
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync/**
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * Converts a nano second time stamp to ktime_t.
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync *
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * ASSUMES RTTimeSystemNanoTS() is implemented using ktime_get_ts().
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync *
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * @returns ktime_t.
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync * @param cNanoSecs Nanoseconds.
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync */
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsyncDECLINLINE(ktime_t) rtTimerLnxNanoToKt(uint64_t cNanoSecs)
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync{
d8dee9a7ef33d4c705d5fd087d5af9c7cb071f85vboxsync /* With some luck the compiler optimizes the division out of this... (Bet it doesn't.) */
0612e2adbcc146b9eb7748983c720e35e38d0dc9vboxsync return ktime_set(cNanoSecs / 1000000000, cNanoSecs % 1000000000);
e2a73964f463b9e91f6f096f9e15974a3edcc416vboxsync}
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Converts ktime_t to a nano second time stamp.
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * ASSUMES RTTimeSystemNanoTS() is implemented using ktime_get_ts().
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @returns nano second time stamp.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param Kt ktime_t.
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncDECLINLINE(uint64_t) rtTimerLnxKtToNano(ktime_t Kt)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return ktime_to_ns(Kt);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#endif /* RTTIMER_LINUX_WITH_HRTIMER */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Converts a nano second interval to jiffies.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @returns Jiffies.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param cNanoSecs Nanoseconds.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncDECLINLINE(unsigned long) rtTimerLnxNanoToJiffies(uint64_t cNanoSecs)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /* this can be made even better... */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (cNanoSecs > (uint64_t)TICK_NSEC * MAX_JIFFY_OFFSET)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return MAX_JIFFY_OFFSET;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync# if ARCH_BITS == 32
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (RT_LIKELY(cNanoSecs <= UINT32_MAX))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return ((uint32_t)cNanoSecs + (TICK_NSEC-1)) / TICK_NSEC;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync# endif
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return (cNanoSecs + (TICK_NSEC-1)) / TICK_NSEC;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Starts a sub-timer (RTTimerStart).
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pSubTimer The sub-timer to start.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param u64Now The current timestamp (RTTimeSystemNanoTS()).
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync * @param u64First The interval from u64Now to the first time the timer should fire.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param fPinned true = timer pinned to a specific CPU,
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * false = timer can migrate between CPUs
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync * @param fHighRes Whether the user requested a high resolution timer or not.
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync * @param enmOldState The old timer state.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic void rtTimerLnxStartSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, uint64_t u64Now, uint64_t u64First,
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync bool fPinned, bool fHighRes)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /*
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Calc when it should start firing.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync uint64_t u64NextTS = u64Now + u64First;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (fHighRes)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync pSubTimer->u.Std.u64NextTS = u64NextTS;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTTIMERLNX_LOG(("startsubtimer %p\n", pSubTimer->pParent));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync pSubTimer->iTick = 0;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#ifdef RTTIMER_LINUX_WITH_HRTIMER
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (fHighRes)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync hrtimer_start(&pSubTimer->u.Hr.LnxTimer, rtTimerLnxNanoToKt(u64NextTS),
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync fPinned ? HRTIMER_MODE_ABS_PINNED : HRTIMER_MODE_ABS);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync else
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#endif
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync unsigned long cJiffies = !u64First ? 0 : rtTimerLnxNanoToJiffies(u64First);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync pSubTimer->u.Std.ulNextJiffies = jiffies + cJiffies;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync pSubTimer->u.Std.fFirstAfterChg = true;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#ifdef CONFIG_SMP
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (fPinned)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync mod_timer_pinned(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync else
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#endif
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync mod_timer(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /* Be a bit careful here since we could be racing the callback. */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (!rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_STARTING))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_MP_STARTING);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Stops a sub-timer (RTTimerStart and rtTimerLinuxMpEvent()).
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * The caller has already changed the state, so we will not be in a callback
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync * situation wrt to the calling thread.
683eff3070b1b86fe71b71af7fda82766ea19d17vboxsync *
b72d3233df38e3122eda39b39a27b35c27209615vboxsync * @param pSubTimer The sub-timer.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param fHighRes Whether the user requested a high resolution timer or not.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic void rtTimerLnxStopSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, bool fHighRes)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
b72d3233df38e3122eda39b39a27b35c27209615vboxsync RTTIMERLNX_LOG(("stopsubtimer %p %d\n", pSubTimer->pParent, fHighRes));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#ifdef RTTIMER_LINUX_WITH_HRTIMER
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (fHighRes)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync hrtimer_cancel(&pSubTimer->u.Hr.LnxTimer);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync else
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#endif
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (timer_pending(&pSubTimer->u.Std.LnxTimer))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync del_timer_sync(&pSubTimer->u.Std.LnxTimer);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Used by RTTimerDestroy and rtTimerLnxCallbackDestroy to do the actual work.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pTimer The timer in question.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic void rtTimerLnxDestroyIt(PRTTIMER pTimer)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSPINLOCK hSpinlock = pTimer->hSpinlock;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync Assert(pTimer->fSuspended);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTTIMERLNX_LOG(("destroyit %p\n", pTimer));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /*
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Remove the MP notifications first because it'll reduce the risk of
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * us overtaking any MP event that might theoretically be racing us here.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#ifdef CONFIG_SMP
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if ( pTimer->cCpus > 1
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync && hSpinlock != NIL_RTSPINLOCK)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync int rc = RTMpNotificationDeregister(rtTimerLinuxMpEvent, pTimer);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertRC(rc);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#endif /* CONFIG_SMP */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /*
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Uninitialize the structure and free the associated resources.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * The spinlock goes last.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTMemFreeEx(pTimer, RT_OFFSETOF(RTTIMER, aSubTimers[pTimer->cCpus]));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (hSpinlock != NIL_RTSPINLOCK)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSpinlockDestroy(hSpinlock);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Called when the timer was destroyed by the callback function.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pTimer The timer.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pSubTimer The sub-timer which we're handling, the state of this
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * will be RTTIMERLNXSTATE_CALLBACK_DESTROYING.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic void rtTimerLnxCallbackDestroy(PRTTIMER pTimer, PRTTIMERLNXSUBTIMER pSubTimer)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync /*
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * If it's an omni timer, the last dude does the destroying.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (pTimer->cCpus > 1)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync uint32_t iCpu = pTimer->cCpus;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync Assert(pSubTimer->enmState == RTTIMERLNXSTATE_CB_DESTROYING);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync while (iCpu-- > 0)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) != RTTIMERLNXSTATE_STOPPED)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync rtTimerLnxDestroyIt(pTimer);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync#ifdef CONFIG_SMP
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Deal with a sub-timer that has migrated.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pTimer The timer.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pSubTimer The sub-timer.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic void rtTimerLnxCallbackHandleMigration(PRTTIMER pTimer, PRTTIMERLNXSUBTIMER pSubTimer)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTTIMERLNXSTATE enmState;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (pTimer->cCpus > 1)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync do
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync enmState = rtTimerLnxGetState(&pSubTimer->enmState);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync switch (enmState)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_STOPPING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_MP_STOPPING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync enmState = RTTIMERLNXSTATE_STOPPED;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_STOPPED:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync default:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertMsgFailed(("%d\n", enmState));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_STARTING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_MP_STARTING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_ACTIVE:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_CALLBACK:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_CB_STOPPING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_CB_RESTARTING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, enmState))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync enmState = RTTIMERLNXSTATE_STOPPED;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_CB_DESTROYING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (pTimer->cCpus > 1)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync rtTimerLnxCallbackDestroy(pTimer, pSubTimer);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync } while (enmState != RTTIMERLNXSTATE_STOPPED);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (pTimer->cCpus > 1)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
b72d3233df38e3122eda39b39a27b35c27209615vboxsync#endif /* CONFIG_SMP */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * The slow path of rtTimerLnxChangeToCallbackState.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
b72d3233df38e3122eda39b39a27b35c27209615vboxsync * @returns true if changed successfully, false if not.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pSubTimer The sub-timer.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsyncstatic bool rtTimerLnxChangeToCallbackStateSlow(PRTTIMERLNXSUBTIMER pSubTimer)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync{
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync for (;;)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pSubTimer->enmState);
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync switch (enmState)
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync {
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_ACTIVE:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_STARTING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_MP_STARTING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_CALLBACK, enmState))
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return true;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync break;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_CALLBACK:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_CB_STOPPING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_CB_RESTARTING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync case RTTIMERLNXSTATE_CB_DESTROYING:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync AssertMsgFailed(("%d\n", enmState));
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync default:
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync return false;
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync ASMNopPause();
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync }
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync}
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync/**
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * Tries to change the sub-timer state to 'callback'.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync *
b72d3233df38e3122eda39b39a27b35c27209615vboxsync * @returns true if changed successfully, false if not.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync * @param pSubTimer The sub-timer.
9b456547aefb8a2e8f5600eba9ec377dbc9b4475vboxsync */
b72d3233df38e3122eda39b39a27b35c27209615vboxsyncDECLINLINE(bool) rtTimerLnxChangeToCallbackState(PRTTIMERLNXSUBTIMER pSubTimer)
b72d3233df38e3122eda39b39a27b35c27209615vboxsync{
b72d3233df38e3122eda39b39a27b35c27209615vboxsync if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_CALLBACK, RTTIMERLNXSTATE_ACTIVE)))
b72d3233df38e3122eda39b39a27b35c27209615vboxsync return true;
b72d3233df38e3122eda39b39a27b35c27209615vboxsync return rtTimerLnxChangeToCallbackStateSlow(pSubTimer);
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync}
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync#ifdef RTTIMER_LINUX_WITH_HRTIMER
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync/**
84f746c9015f34e9ab096b87e063d0d6ab7fc7aevboxsync * Timer callback function for high resolution timers.
*
* @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a
* one-shot or interval timer.
* @param pHrTimer Pointer to the sub-timer structure.
*/
static enum hrtimer_restart rtTimerLinuxHrCallback(struct hrtimer *pHrTimer)
{
PRTTIMERLNXSUBTIMER pSubTimer = RT_FROM_MEMBER(pHrTimer, RTTIMERLNXSUBTIMER, u.Hr.LnxTimer);
PRTTIMER pTimer = pSubTimer->pParent;
RTTIMERLNX_LOG(("hrcallback %p\n", pTimer));
if (RT_UNLIKELY(!rtTimerLnxChangeToCallbackState(pSubTimer)))
return HRTIMER_NORESTART;
#ifdef CONFIG_SMP
/*
* Check for unwanted migration.
*/
if (pTimer->fAllCpus || pTimer->fSpecificCpu)
{
RTCPUID idCpu = RTMpCpuId();
if (RT_UNLIKELY( pTimer->fAllCpus
? (RTCPUID)(pSubTimer - &pTimer->aSubTimers[0]) != idCpu
: pTimer->idCpu != idCpu))
{
rtTimerLnxCallbackHandleMigration(pTimer, pSubTimer);
return HRTIMER_NORESTART;
}
}
#endif
if (pTimer->u64NanoInterval)
{
/*
* Periodic timer, run it and update the native timer afterwards so
* we can handle RTTimerStop and RTTimerChangeInterval from the
* callback as well as a racing control thread.
*/
pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
hrtimer_add_expires_ns(&pSubTimer->u.Hr.LnxTimer, ASMAtomicReadU64(&pTimer->u64NanoInterval));
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CALLBACK)))
return HRTIMER_RESTART;
}
else
{
/*
* One shot timer (no omni), stop it before dispatching it.
* Allow RTTimerStart as well as RTTimerDestroy to be called from
* the callback.
*/
ASMAtomicWriteBool(&pTimer->fSuspended, true);
pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CALLBACK)))
return HRTIMER_NORESTART;
}
/*
* Some state change occurred while we were in the callback routine.
*/
for (;;)
{
RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pSubTimer->enmState);
switch (enmState)
{
case RTTIMERLNXSTATE_CB_DESTROYING:
rtTimerLnxCallbackDestroy(pTimer, pSubTimer);
return HRTIMER_NORESTART;
case RTTIMERLNXSTATE_CB_STOPPING:
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CB_STOPPING))
return HRTIMER_NORESTART;
break;
case RTTIMERLNXSTATE_CB_RESTARTING:
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CB_RESTARTING))
{
pSubTimer->iTick = 0;
hrtimer_set_expires(&pSubTimer->u.Hr.LnxTimer, rtTimerLnxNanoToKt(pSubTimer->uNsRestartAt));
return HRTIMER_RESTART;
}
break;
default:
AssertMsgFailed(("%d\n", enmState));
return HRTIMER_NORESTART;
}
ASMNopPause();
}
}
#endif /* RTTIMER_LINUX_WITH_HRTIMER */
/**
* Timer callback function for standard timers.
*
* @param ulUser Address of the sub-timer structure.
*/
static void rtTimerLinuxStdCallback(unsigned long ulUser)
{
PRTTIMERLNXSUBTIMER pSubTimer = (PRTTIMERLNXSUBTIMER)ulUser;
PRTTIMER pTimer = pSubTimer->pParent;
RTTIMERLNX_LOG(("stdcallback %p\n", pTimer));
if (RT_UNLIKELY(!rtTimerLnxChangeToCallbackState(pSubTimer)))
return;
#ifdef CONFIG_SMP
/*
* Check for unwanted migration.
*/
if (pTimer->fAllCpus || pTimer->fSpecificCpu)
{
RTCPUID idCpu = RTMpCpuId();
if (RT_UNLIKELY( pTimer->fAllCpus
? (RTCPUID)(pSubTimer - &pTimer->aSubTimers[0]) != idCpu
: pTimer->idCpu != idCpu))
{
rtTimerLnxCallbackHandleMigration(pTimer, pSubTimer);
return;
}
}
#endif
if (pTimer->u64NanoInterval)
{
/*
* Interval timer, calculate the next timeout.
*
* The first time around, we'll re-adjust the u.Std.u64NextTS to
* try prevent some jittering if we were started at a bad time.
*/
const uint64_t iTick = ++pSubTimer->iTick;
uint64_t u64NanoInterval;
unsigned long cJiffies;
unsigned long flFlags;
spin_lock_irqsave(&pTimer->ChgIntLock, flFlags);
u64NanoInterval = pTimer->u64NanoInterval;
cJiffies = pTimer->cJiffies;
if (RT_UNLIKELY(pSubTimer->u.Std.fFirstAfterChg))
{
pSubTimer->u.Std.fFirstAfterChg = false;
pSubTimer->u.Std.u64NextTS = RTTimeSystemNanoTS();
pSubTimer->u.Std.ulNextJiffies = jiffies;
}
spin_unlock_irqrestore(&pTimer->ChgIntLock, flFlags);
pSubTimer->u.Std.u64NextTS += u64NanoInterval;
if (cJiffies)
{
pSubTimer->u.Std.ulNextJiffies += cJiffies;
/* Prevent overflows when the jiffies counter wraps around.
* Special thanks to Ken Preslan for helping debugging! */
while (time_before(pSubTimer->u.Std.ulNextJiffies, jiffies))
{
pSubTimer->u.Std.ulNextJiffies += cJiffies;
pSubTimer->u.Std.u64NextTS += u64NanoInterval;
}
}
else
{
const uint64_t u64NanoTS = RTTimeSystemNanoTS();
while (pSubTimer->u.Std.u64NextTS < u64NanoTS)
pSubTimer->u.Std.u64NextTS += u64NanoInterval;
pSubTimer->u.Std.ulNextJiffies = jiffies + rtTimerLnxNanoToJiffies(pSubTimer->u.Std.u64NextTS - u64NanoTS);
}
/*
* Run the timer and re-arm it unless the state changed .
* .
* We must re-arm it afterwards as we're not in a position to undo this .
* operation if for instance someone stopped or destroyed us while we .
* were in the callback. (Linux takes care of any races here.)
*/
pTimer->pfnTimer(pTimer, pTimer->pvUser, iTick);
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CALLBACK)))
{
#ifdef CONFIG_SMP
if (pTimer->fSpecificCpu || pTimer->fAllCpus)
mod_timer_pinned(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
else
#endif
mod_timer(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
return;
}
}
else
{
/*
* One shot timer, stop it before dispatching it.
* Allow RTTimerStart as well as RTTimerDestroy to be called from
* the callback.
*/
ASMAtomicWriteBool(&pTimer->fSuspended, true);
pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CALLBACK)))
return;
}
/*
* Some state change occurred while we were in the callback routine.
*/
for (;;)
{
RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pSubTimer->enmState);
switch (enmState)
{
case RTTIMERLNXSTATE_CB_DESTROYING:
rtTimerLnxCallbackDestroy(pTimer, pSubTimer);
return;
case RTTIMERLNXSTATE_CB_STOPPING:
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CB_STOPPING))
return;
break;
case RTTIMERLNXSTATE_CB_RESTARTING:
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CB_RESTARTING))
{
uint64_t u64NanoTS;
uint64_t u64NextTS;
unsigned long flFlags;
spin_lock_irqsave(&pTimer->ChgIntLock, flFlags);
u64NextTS = pSubTimer->uNsRestartAt;
u64NanoTS = RTTimeSystemNanoTS();
pSubTimer->iTick = 0;
pSubTimer->u.Std.u64NextTS = u64NextTS;
pSubTimer->u.Std.fFirstAfterChg = true;
pSubTimer->u.Std.ulNextJiffies = u64NextTS > u64NanoTS
? jiffies + rtTimerLnxNanoToJiffies(u64NextTS - u64NanoTS)
: jiffies;
spin_unlock_irqrestore(&pTimer->ChgIntLock, flFlags);
#ifdef CONFIG_SMP
if (pTimer->fSpecificCpu || pTimer->fAllCpus)
mod_timer_pinned(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
else
#endif
mod_timer(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
return;
}
break;
default:
AssertMsgFailed(("%d\n", enmState));
return;
}
ASMNopPause();
}
}
#ifdef CONFIG_SMP
/**
* Per-cpu callback function (RTMpOnAll/RTMpOnSpecific).
*
* @param idCpu The current CPU.
* @param pvUser1 Pointer to the timer.
* @param pvUser2 Pointer to the argument structure.
*/
static DECLCALLBACK(void) rtTimerLnxStartAllOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
{
PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
PRTTIMER pTimer = (PRTTIMER)pvUser1;
Assert(idCpu < pTimer->cCpus);
rtTimerLnxStartSubTimer(&pTimer->aSubTimers[idCpu], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
}
/**
* Worker for RTTimerStart() that takes care of the ugly bits.
*
* @returns RTTimerStart() return value.
* @param pTimer The timer.
* @param pArgs The argument structure.
*/
static int rtTimerLnxOmniStart(PRTTIMER pTimer, PRTTIMERLINUXSTARTONCPUARGS pArgs)
{
RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
RTCPUID iCpu;
RTCPUSET OnlineSet;
RTCPUSET OnlineSet2;
int rc2;
/*
* Prepare all the sub-timers for the startup and then flag the timer
* as a whole as non-suspended, make sure we get them all before
* clearing fSuspended as the MP handler will be waiting on this
* should something happen while we're looping.
*/
RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
/* Just make it a omni timer restriction that no stop/start races are allowed. */
for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) != RTTIMERLNXSTATE_STOPPED)
{
RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
return VERR_TIMER_BUSY;
}
do
{
RTMpGetOnlineSet(&OnlineSet);
for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
{
Assert(pTimer->aSubTimers[iCpu].enmState != RTTIMERLNXSTATE_MP_STOPPING);
rtTimerLnxSetState(&pTimer->aSubTimers[iCpu].enmState,
RTCpuSetIsMember(&OnlineSet, iCpu)
? RTTIMERLNXSTATE_STARTING
: RTTIMERLNXSTATE_STOPPED);
}
} while (!RTCpuSetIsEqual(&OnlineSet, RTMpGetOnlineSet(&OnlineSet2)));
ASMAtomicWriteBool(&pTimer->fSuspended, false);
RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
/*
* Start them (can't find any exported function that allows me to
* do this without the cross calls).
*/
pArgs->u64Now = RTTimeSystemNanoTS();
rc2 = RTMpOnAll(rtTimerLnxStartAllOnCpu, pTimer, pArgs);
AssertRC(rc2); /* screw this if it fails. */
/*
* Reset the sub-timers who didn't start up (ALL CPUs case).
*/
RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_STARTING))
{
/** @todo very odd case for a rainy day. Cpus that temporarily went offline while
* we were between calls needs to nudged as the MP handler will ignore events for
* them because of the STARTING state. This is an extremely unlikely case - not that
* that means anything in my experience... ;-) */
RTTIMERLNX_LOG(("what!? iCpu=%u -> didn't start\n", iCpu));
}
RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
return VINF_SUCCESS;
}
/**
* Worker for RTTimerStop() that takes care of the ugly SMP bits.
*
* @returns true if there was any active callbacks, false if not.
* @param pTimer The timer (valid).
* @param fForDestroy Whether this is for RTTimerDestroy or not.
*/
static bool rtTimerLnxOmniStop(PRTTIMER pTimer, bool fForDestroy)
{
bool fActiveCallbacks = false;
RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
RTCPUID iCpu;
/*
* Mark the timer as suspended and flag all timers as stopping, except
* for those being stopped by an MP event.
*/
RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
ASMAtomicWriteBool(&pTimer->fSuspended, true);
for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
{
RTTIMERLNXSTATE enmState;
for (;;)
{
enmState = rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState);
if ( enmState == RTTIMERLNXSTATE_STOPPED
|| enmState == RTTIMERLNXSTATE_MP_STOPPING)
break;
if ( enmState == RTTIMERLNXSTATE_CALLBACK
|| enmState == RTTIMERLNXSTATE_CB_STOPPING
|| enmState == RTTIMERLNXSTATE_CB_RESTARTING)
{
Assert(enmState != RTTIMERLNXSTATE_CB_STOPPING || fForDestroy);
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState,
!fForDestroy ? RTTIMERLNXSTATE_CB_STOPPING : RTTIMERLNXSTATE_CB_DESTROYING,
enmState))
{
fActiveCallbacks = true;
break;
}
}
else
{
Assert(enmState == RTTIMERLNXSTATE_ACTIVE);
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPING, enmState))
break;
}
ASMNopPause();
}
}
RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
/*
* Do the actual stopping. Fortunately, this doesn't require any IPIs.
* Unfortunately it cannot be done synchronously from within the spinlock,
* because we might end up in an active waiting for a handler to complete.
*/
for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) == RTTIMERLNXSTATE_STOPPING)
rtTimerLnxStopSubTimer(&pTimer->aSubTimers[iCpu], pTimer->fHighRes);
return fActiveCallbacks;
}
/**
* Per-cpu callback function (RTMpOnSpecific) used by rtTimerLinuxMpEvent()
* to start a sub-timer on a cpu that just have come online.
*
* @param idCpu The current CPU.
* @param pvUser1 Pointer to the timer.
* @param pvUser2 Pointer to the argument structure.
*/
static DECLCALLBACK(void) rtTimerLinuxMpStartOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
{
PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
PRTTIMER pTimer = (PRTTIMER)pvUser1;
RTSPINLOCK hSpinlock;
Assert(idCpu < pTimer->cCpus);
/*
* We have to be kind of careful here as we might be racing RTTimerStop
* (and/or RTTimerDestroy, thus the paranoia.
*/
hSpinlock = pTimer->hSpinlock;
if ( hSpinlock != NIL_RTSPINLOCK
&& pTimer->u32Magic == RTTIMER_MAGIC)
{
RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
RTSpinlockAcquire(hSpinlock, &Tmp);
if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
&& pTimer->u32Magic == RTTIMER_MAGIC)
{
/* We're sane and the timer is not suspended yet. */
PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
rtTimerLnxStartSubTimer(pSubTimer, pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
}
RTSpinlockRelease(hSpinlock, &Tmp);
}
}
/**
* MP event notification callback.
*
* @param enmEvent The event.
* @param idCpu The cpu it applies to.
* @param pvUser The timer.
*/
static DECLCALLBACK(void) rtTimerLinuxMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser)
{
PRTTIMER pTimer = (PRTTIMER)pvUser;
PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
RTSPINLOCK hSpinlock;
Assert(idCpu < pTimer->cCpus);
/*
* Some initial paranoia.
*/
if (pTimer->u32Magic != RTTIMER_MAGIC)
return;
hSpinlock = pTimer->hSpinlock;
if (hSpinlock == NIL_RTSPINLOCK)
return;
RTSpinlockAcquire(hSpinlock, &Tmp);
/* Is it active? */
if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
&& pTimer->u32Magic == RTTIMER_MAGIC)
{
switch (enmEvent)
{
/*
* Try do it without leaving the spin lock, but if we have to, retake it
* when we're on the right cpu.
*/
case RTMPEVENT_ONLINE:
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
{
RTTIMERLINUXSTARTONCPUARGS Args;
Args.u64Now = RTTimeSystemNanoTS();
Args.u64First = 0;
if (RTMpCpuId() == idCpu)
rtTimerLnxStartSubTimer(pSubTimer, Args.u64Now, Args.u64First, true /*fPinned*/, pTimer->fHighRes);
else
{
rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED); /* we'll recheck it. */
RTSpinlockRelease(hSpinlock, &Tmp);
RTMpOnSpecific(idCpu, rtTimerLinuxMpStartOnCpu, pTimer, &Args);
return; /* we've left the spinlock */
}
}
break;
/*
* The CPU is (going) offline, make sure the sub-timer is stopped.
*
* Linux will migrate it to a different CPU, but we don't want this. The
* timer function is checking for this.
*/
case RTMPEVENT_OFFLINE:
{
RTTIMERLNXSTATE enmState;
while ( (enmState = rtTimerLnxGetState(&pSubTimer->enmState)) == RTTIMERLNXSTATE_ACTIVE
|| enmState == RTTIMERLNXSTATE_CALLBACK
|| enmState == RTTIMERLNXSTATE_CB_RESTARTING)
{
if (enmState == RTTIMERLNXSTATE_ACTIVE)
{
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STOPPING, RTTIMERLNXSTATE_ACTIVE))
{
RTSpinlockRelease(hSpinlock, &Tmp);
rtTimerLnxStopSubTimer(pSubTimer, pTimer->fHighRes);
return; /* we've left the spinlock */
}
}
else if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_CB_STOPPING, enmState))
break;
/* State not stable, try again. */
ASMNopPause();
}
break;
}
}
}
RTSpinlockRelease(hSpinlock, &Tmp);
}
#endif /* CONFIG_SMP */
/**
* Callback function use by RTTimerStart via RTMpOnSpecific to start a timer
* running on a specific CPU.
*
* @param idCpu The current CPU.
* @param pvUser1 Pointer to the timer.
* @param pvUser2 Pointer to the argument structure.
*/
static DECLCALLBACK(void) rtTimerLnxStartOnSpecificCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
{
PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
PRTTIMER pTimer = (PRTTIMER)pvUser1;
rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
}
RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
{
RTTIMERLINUXSTARTONCPUARGS Args;
int rc2;
/*
* Validate.
*/
AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
return VERR_TIMER_ACTIVE;
RTTIMERLNX_LOG(("start %p cCpus=%d\n", pTimer, pTimer->cCpus));
Args.u64First = u64First;
#ifdef CONFIG_SMP
/*
* Omni timer?
*/
if (pTimer->fAllCpus)
return rtTimerLnxOmniStart(pTimer, &Args);
#endif
/*
* Simple timer - Pretty straight forward if it wasn't for restarting.
*/
Args.u64Now = RTTimeSystemNanoTS();
ASMAtomicWriteU64(&pTimer->aSubTimers[0].uNsRestartAt, Args.u64Now + u64First);
for (;;)
{
RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pTimer->aSubTimers[0].enmState);
switch (enmState)
{
case RTTIMERLNXSTATE_STOPPED:
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STARTING, RTTIMERLNXSTATE_STOPPED))
{
ASMAtomicWriteBool(&pTimer->fSuspended, false);
if (!pTimer->fSpecificCpu)
rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], Args.u64Now, Args.u64First,
false /*fPinned*/, pTimer->fHighRes);
else
{
rc2 = RTMpOnSpecific(pTimer->idCpu, rtTimerLnxStartOnSpecificCpu, pTimer, &Args);
if (RT_FAILURE(rc2))
{
/* Suspend it, the cpu id is probably invalid or offline. */
ASMAtomicWriteBool(&pTimer->fSuspended, true);
rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPED);
return rc2;
}
}
return VINF_SUCCESS;
}
break;
case RTTIMERLNXSTATE_CALLBACK:
case RTTIMERLNXSTATE_CB_STOPPING:
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_CB_RESTARTING, enmState))
{
ASMAtomicWriteBool(&pTimer->fSuspended, false);
return VINF_SUCCESS;
}
break;
default:
AssertMsgFailed(("%d\n", enmState));
return VERR_INTERNAL_ERROR_4;
}
ASMNopPause();
}
}
RT_EXPORT_SYMBOL(RTTimerStart);
/**
* Common worker for RTTimerStop and RTTimerDestroy.
*
* @returns true if there was any active callbacks, false if not.
* @param pTimer The timer to stop.
* @param fForDestroy Whether it's RTTimerDestroy calling or not.
*/
static bool rtTimerLnxStop(PRTTIMER pTimer, bool fForDestroy)
{
RTTIMERLNX_LOG(("lnxstop %p %d\n", pTimer, fForDestroy));
#ifdef CONFIG_SMP
/*
* Omni timer?
*/
if (pTimer->fAllCpus)
return rtTimerLnxOmniStop(pTimer, fForDestroy);
#endif
/*
* Simple timer.
*/
ASMAtomicWriteBool(&pTimer->fSuspended, true);
for (;;)
{
RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pTimer->aSubTimers[0].enmState);
switch (enmState)
{
case RTTIMERLNXSTATE_ACTIVE:
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPING, RTTIMERLNXSTATE_ACTIVE))
{
rtTimerLnxStopSubTimer(&pTimer->aSubTimers[0], pTimer->fHighRes);
return false;
}
break;
case RTTIMERLNXSTATE_CALLBACK:
case RTTIMERLNXSTATE_CB_RESTARTING:
case RTTIMERLNXSTATE_CB_STOPPING:
Assert(enmState != RTTIMERLNXSTATE_CB_STOPPING || fForDestroy);
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState,
!fForDestroy ? RTTIMERLNXSTATE_CB_STOPPING : RTTIMERLNXSTATE_CB_DESTROYING,
enmState))
return true;
break;
case RTTIMERLNXSTATE_STOPPED:
return VINF_SUCCESS;
case RTTIMERLNXSTATE_CB_DESTROYING:
AssertMsgFailed(("enmState=%d pTimer=%p\n", enmState, pTimer));
return true;
default:
case RTTIMERLNXSTATE_STARTING:
case RTTIMERLNXSTATE_MP_STARTING:
case RTTIMERLNXSTATE_STOPPING:
case RTTIMERLNXSTATE_MP_STOPPING:
AssertMsgFailed(("enmState=%d pTimer=%p\n", enmState, pTimer));
return false;
}
/* State not stable, try again. */
ASMNopPause();
}
}
RTDECL(int) RTTimerStop(PRTTIMER pTimer)
{
/*
* Validate.
*/
AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
RTTIMERLNX_LOG(("stop %p\n", pTimer));
if (ASMAtomicUoReadBool(&pTimer->fSuspended))
return VERR_TIMER_SUSPENDED;
rtTimerLnxStop(pTimer, false /*fForDestroy*/);
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTTimerStop);
RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
{
unsigned long cJiffies;
unsigned long flFlags;
/*
* Validate.
*/
AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(u64NanoInterval, VERR_INVALID_PARAMETER);
RTTIMERLNX_LOG(("change %p %llu\n", pTimer, u64NanoInterval));
#ifdef RTTIMER_LINUX_WITH_HRTIMER
/*
* For the high resolution timers it is easy since we don't care so much
* about when it is applied to the sub-timers.
*/
if (pTimer->fHighRes)
{
ASMAtomicWriteU64(&pTimer->u64NanoInterval, u64NanoInterval);
return VINF_SUCCESS;
}
#endif
/*
* Standard timers have a bit more complicated way of calculating
* their interval and such. So, forget omni timers for now.
*/
if (pTimer->cCpus > 1)
return VERR_NOT_SUPPORTED;
cJiffies = u64NanoInterval / RTTimerGetSystemGranularity();
if (cJiffies * RTTimerGetSystemGranularity() != u64NanoInterval)
cJiffies = 0;
spin_lock_irqsave(&pTimer->ChgIntLock, flFlags);
pTimer->aSubTimers[0].u.Std.fFirstAfterChg = true;
pTimer->cJiffies = cJiffies;
ASMAtomicWriteU64(&pTimer->u64NanoInterval, u64NanoInterval);
spin_unlock_irqrestore(&pTimer->ChgIntLock, flFlags);
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTTimerChangeInterval);
RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
{
bool fCanDestroy;
/*
* Validate. It's ok to pass NULL pointer.
*/
if (pTimer == /*NIL_RTTIMER*/ NULL)
return VINF_SUCCESS;
AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
RTTIMERLNX_LOG(("destroy %p\n", pTimer));
/*
* Stop the timer if it's still active, then destroy it if we can.
*/
if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
fCanDestroy = rtTimerLnxStop(pTimer, true /*fForDestroy*/);
else
{
uint32_t iCpu = pTimer->cCpus;
RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
if (pTimer->cCpus > 1)
RTSpinlockAcquireNoInts(pTimer->hSpinlock, &Tmp);
fCanDestroy = true;
while (iCpu-- > 0)
{
for (;;)
{
RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState);
switch (enmState)
{
case RTTIMERLNXSTATE_CALLBACK:
case RTTIMERLNXSTATE_CB_RESTARTING:
case RTTIMERLNXSTATE_CB_STOPPING:
if (!rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_CB_DESTROYING, enmState))
continue;
fCanDestroy = false;
break;
case RTTIMERLNXSTATE_CB_DESTROYING:
AssertMsgFailed(("%d\n", enmState));
fCanDestroy = false;
break;
default:
break;
}
break;
}
}
if (pTimer->cCpus > 1)
RTSpinlockReleaseNoInts(pTimer->hSpinlock, &Tmp);
}
if (fCanDestroy)
rtTimerLnxDestroyIt(pTimer);
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTTimerDestroy);
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
{
PRTTIMER pTimer;
RTCPUID iCpu;
unsigned cCpus;
int rc;
*ppTimer = NULL;
/*
* Validate flags.
*/
if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
return VERR_INVALID_PARAMETER;
if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
&& (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
&& !RTMpIsCpuPossible(RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK)))
return VERR_CPU_NOT_FOUND;
/*
* Allocate the timer handler.
*/
cCpus = 1;
#ifdef CONFIG_SMP
if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
{
cCpus = RTMpGetMaxCpuId() + 1;
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. */
}
#endif
rc = RTMemAllocEx(RT_OFFSETOF(RTTIMER, aSubTimers[cCpus]), 0,
RTMEMALLOCEX_FLAGS_ZEROED | RTMEMALLOCEX_FLAGS_ANY_CTX_FREE, (void **)&pTimer);
if (RT_FAILURE(rc))
return rc;
/*
* Initialize it.
*/
pTimer->u32Magic = RTTIMER_MAGIC;
pTimer->hSpinlock = NIL_RTSPINLOCK;
pTimer->fSuspended = true;
pTimer->fHighRes = !!(fFlags & RTTIMER_FLAGS_HIGH_RES);
#ifdef CONFIG_SMP
pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
pTimer->fAllCpus = (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL;
pTimer->idCpu = pTimer->fSpecificCpu
? RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK)
: NIL_RTCPUID;
#else
pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
pTimer->idCpu = RTMpCpuId();
#endif
pTimer->cCpus = cCpus;
pTimer->pfnTimer = pfnTimer;
pTimer->pvUser = pvUser;
pTimer->u64NanoInterval = u64NanoInterval;
pTimer->cJiffies = u64NanoInterval / RTTimerGetSystemGranularity();
if (pTimer->cJiffies * RTTimerGetSystemGranularity() != u64NanoInterval)
pTimer->cJiffies = 0;
spin_lock_init(&pTimer->ChgIntLock);
for (iCpu = 0; iCpu < cCpus; iCpu++)
{
#ifdef RTTIMER_LINUX_WITH_HRTIMER
if (pTimer->fHighRes)
{
hrtimer_init(&pTimer->aSubTimers[iCpu].u.Hr.LnxTimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
pTimer->aSubTimers[iCpu].u.Hr.LnxTimer.function = rtTimerLinuxHrCallback;
}
else
#endif
{
init_timer(&pTimer->aSubTimers[iCpu].u.Std.LnxTimer);
pTimer->aSubTimers[iCpu].u.Std.LnxTimer.data = (unsigned long)&pTimer->aSubTimers[iCpu];
pTimer->aSubTimers[iCpu].u.Std.LnxTimer.function = rtTimerLinuxStdCallback;
pTimer->aSubTimers[iCpu].u.Std.LnxTimer.expires = jiffies;
pTimer->aSubTimers[iCpu].u.Std.u64NextTS = 0;
}
pTimer->aSubTimers[iCpu].iTick = 0;
pTimer->aSubTimers[iCpu].pParent = pTimer;
pTimer->aSubTimers[iCpu].enmState = RTTIMERLNXSTATE_STOPPED;
}
#ifdef CONFIG_SMP
/*
* If this is running on ALL cpus, we'll have to register a callback
* for MP events (so timers can be started/stopped on cpus going
* online/offline). We also create the spinlock for synchronizing
* stop/start/mp-event.
*/
if (cCpus > 1)
{
int rc = RTSpinlockCreate(&pTimer->hSpinlock);
if (RT_SUCCESS(rc))
rc = RTMpNotificationRegister(rtTimerLinuxMpEvent, pTimer);
else
pTimer->hSpinlock = NIL_RTSPINLOCK;
if (RT_FAILURE(rc))
{
RTTimerDestroy(pTimer);
return rc;
}
}
#endif /* CONFIG_SMP */
RTTIMERLNX_LOG(("create %p hires=%d fFlags=%#x cCpus=%u\n", pTimer, pTimer->fHighRes, fFlags, cCpus));
*ppTimer = pTimer;
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTTimerCreateEx);
RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
{
#if 0 /** @todo Not sure if this is what we want or not... Add new API for
* querying the resolution of the high res timers? */
struct timespec Ts;
int rc = hrtimer_get_res(CLOCK_MONOTONIC, &Ts);
if (!rc)
{
Assert(!Ts.tv_sec);
return Ts.tv_nsec;
}
#endif
return 1000000000 / HZ; /* ns */
}
RT_EXPORT_SYMBOL(RTTimerGetSystemGranularity);
RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
{
return VERR_NOT_SUPPORTED;
}
RT_EXPORT_SYMBOL(RTTimerRequestSystemGranularity);
RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
{
return VERR_NOT_SUPPORTED;
}
RT_EXPORT_SYMBOL(RTTimerReleaseSystemGranularity);
RTDECL(bool) RTTimerCanDoHighResolution(void)
{
#ifdef RTTIMER_LINUX_WITH_HRTIMER
return true;
#else
return false;
#endif
}
RT_EXPORT_SYMBOL(RTTimerCanDoHighResolution);