VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/timer-r0drv-linux.c@ 44824

Last change on this file since 44824 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 54.9 KB
Line 
1/* $Id: timer-r0drv-linux.c 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * IPRT - Timers, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "the-linux-kernel.h"
32#include "internal/iprt.h"
33
34#include <iprt/timer.h>
35#include <iprt/time.h>
36#include <iprt/mp.h>
37#include <iprt/cpuset.h>
38#include <iprt/spinlock.h>
39#include <iprt/err.h>
40#include <iprt/asm.h>
41#include <iprt/assert.h>
42#include <iprt/alloc.h>
43
44#include "internal/magics.h"
45
46/** @def RTTIMER_LINUX_WITH_HRTIMER
47 * Whether to use high resolution timers. */
48#if !defined(RTTIMER_LINUX_WITH_HRTIMER) \
49 && defined(IPRT_LINUX_HAS_HRTIMER)
50# define RTTIMER_LINUX_WITH_HRTIMER
51#endif
52
53#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)
54# define mod_timer_pinned mod_timer
55# define HRTIMER_MODE_ABS_PINNED HRTIMER_MODE_ABS
56#endif
57
58
59/*******************************************************************************
60* Structures and Typedefs *
61*******************************************************************************/
62/**
63 * Timer state machine.
64 *
65 * This is used to try handle the issues with MP events and
66 * timers that runs on all CPUs. It's relatively nasty :-/
67 */
68typedef enum RTTIMERLNXSTATE
69{
70 /** Stopped. */
71 RTTIMERLNXSTATE_STOPPED = 0,
72 /** Transient state; next ACTIVE. */
73 RTTIMERLNXSTATE_STARTING,
74 /** Transient state; next ACTIVE. (not really necessary) */
75 RTTIMERLNXSTATE_MP_STARTING,
76 /** Active. */
77 RTTIMERLNXSTATE_ACTIVE,
78 /** Active and in callback; next ACTIVE, STOPPED or CALLBACK_DESTROYING. */
79 RTTIMERLNXSTATE_CALLBACK,
80 /** Stopped while in the callback; next STOPPED. */
81 RTTIMERLNXSTATE_CB_STOPPING,
82 /** Restarted while in the callback; next ACTIVE, STOPPED, DESTROYING. */
83 RTTIMERLNXSTATE_CB_RESTARTING,
84 /** The callback shall destroy the timer; next STOPPED. */
85 RTTIMERLNXSTATE_CB_DESTROYING,
86 /** Transient state; next STOPPED. */
87 RTTIMERLNXSTATE_STOPPING,
88 /** Transient state; next STOPPED. */
89 RTTIMERLNXSTATE_MP_STOPPING,
90 /** The usual 32-bit hack. */
91 RTTIMERLNXSTATE_32BIT_HACK = 0x7fffffff
92} RTTIMERLNXSTATE;
93
94
95/**
96 * A Linux sub-timer.
97 */
98typedef struct RTTIMERLNXSUBTIMER
99{
100 /** Timer specific data. */
101 union
102 {
103#if defined(RTTIMER_LINUX_WITH_HRTIMER)
104 /** High resolution timer. */
105 struct
106 {
107 /** The linux timer structure. */
108 struct hrtimer LnxTimer;
109 } Hr;
110#endif
111 /** Standard timer. */
112 struct
113 {
114 /** The linux timer structure. */
115 struct timer_list LnxTimer;
116 /** The start of the current run (ns).
117 * This is used to calculate when the timer ought to fire the next time. */
118 uint64_t u64NextTS;
119 /** The u64NextTS in jiffies. */
120 unsigned long ulNextJiffies;
121 /** Set when starting or changing the timer so that u64StartTs
122 * and u64NextTS gets reinitialized (eliminating some jitter). */
123 bool volatile fFirstAfterChg;
124 } Std;
125 } u;
126 /** The current tick number. */
127 uint64_t iTick;
128 /** Restart the single shot timer at this specific time.
129 * Used when a single shot timer is restarted from the callback. */
130 uint64_t volatile uNsRestartAt;
131 /** Pointer to the parent timer. */
132 PRTTIMER pParent;
133 /** The current sub-timer state. */
134 RTTIMERLNXSTATE volatile enmState;
135} RTTIMERLNXSUBTIMER;
136/** Pointer to a linux sub-timer. */
137typedef RTTIMERLNXSUBTIMER *PRTTIMERLNXSUBTIMER;
138
139
140/**
141 * The internal representation of an Linux timer handle.
142 */
143typedef struct RTTIMER
144{
145 /** Magic.
146 * This is RTTIMER_MAGIC, but changes to something else before the timer
147 * is destroyed to indicate clearly that thread should exit. */
148 uint32_t volatile u32Magic;
149 /** Spinlock synchronizing the fSuspended and MP event handling.
150 * This is NIL_RTSPINLOCK if cCpus == 1. */
151 RTSPINLOCK hSpinlock;
152 /** Flag indicating that the timer is suspended. */
153 bool volatile fSuspended;
154 /** Whether the timer must run on one specific CPU or not. */
155 bool fSpecificCpu;
156#ifdef CONFIG_SMP
157 /** Whether the timer must run on all CPUs or not. */
158 bool fAllCpus;
159#endif /* else: All -> specific on non-SMP kernels */
160 /** Whether it is a high resolution timer or a standard one. */
161 bool fHighRes;
162 /** The id of the CPU it must run on if fSpecificCpu is set. */
163 RTCPUID idCpu;
164 /** The number of CPUs this timer should run on. */
165 RTCPUID cCpus;
166 /** Callback. */
167 PFNRTTIMER pfnTimer;
168 /** User argument. */
169 void *pvUser;
170 /** The timer interval. 0 if one-shot. */
171 uint64_t volatile u64NanoInterval;
172 /** This is set to the number of jiffies between ticks if the interval is
173 * an exact number of jiffies. (Standard timers only.) */
174 unsigned long volatile cJiffies;
175 /** The change interval spinlock for standard timers only. */
176 spinlock_t ChgIntLock;
177 /** Workqueue item for delayed destruction. */
178 RTR0LNXWORKQUEUEITEM DtorWorkqueueItem;
179 /** Sub-timers.
180 * Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
181 * an entry for all possible cpus. In that case the index will be the same as
182 * for the RTCpuSet. */
183 RTTIMERLNXSUBTIMER aSubTimers[1];
184} RTTIMER;
185
186
187/**
188 * A rtTimerLinuxStartOnCpu and rtTimerLinuxStartOnCpu argument package.
189 */
190typedef struct RTTIMERLINUXSTARTONCPUARGS
191{
192 /** The current time (RTTimeSystemNanoTS). */
193 uint64_t u64Now;
194 /** When to start firing (delta). */
195 uint64_t u64First;
196} RTTIMERLINUXSTARTONCPUARGS;
197/** Pointer to a rtTimerLinuxStartOnCpu argument package. */
198typedef RTTIMERLINUXSTARTONCPUARGS *PRTTIMERLINUXSTARTONCPUARGS;
199
200
201/*******************************************************************************
202* Internal Functions *
203*******************************************************************************/
204#ifdef CONFIG_SMP
205static DECLCALLBACK(void) rtTimerLinuxMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser);
206#endif
207
208#if 0
209#define DEBUG_HACKING
210#include <iprt/string.h>
211#include <iprt/asm-amd64-x86.h>
212static void myLogBackdoorPrintf(const char *pszFormat, ...)
213{
214 char szTmp[256];
215 va_list args;
216 size_t cb;
217
218 cb = RTStrPrintf(szTmp, sizeof(szTmp) - 10, "%d: ", RTMpCpuId());
219 va_start(args, pszFormat);
220 cb += RTStrPrintfV(&szTmp[cb], sizeof(szTmp) - cb, pszFormat, args);
221 va_end(args);
222
223 ASMOutStrU8(0x504, (uint8_t *)&szTmp[0], cb);
224}
225# define RTAssertMsg1Weak(pszExpr, uLine, pszFile, pszFunction) \
226 myLogBackdoorPrintf("\n!!Guest Assertion failed!!\n%s(%d) %s\n%s\n", uLine, pszFile, pszFunction, (pszExpr))
227# define RTAssertMsg2Weak myLogBackdoorPrintf
228# define RTTIMERLNX_LOG(a) myLogBackdoorPrintf a
229#else
230# define RTTIMERLNX_LOG(a) do { } while (0)
231#endif
232
233/**
234 * Sets the state.
235 */
236DECLINLINE(void) rtTimerLnxSetState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState)
237{
238#ifdef DEBUG_HACKING
239 RTTIMERLNX_LOG(("set %d -> %d\n", *penmState, enmNewState));
240#endif
241 ASMAtomicWriteU32((uint32_t volatile *)penmState, enmNewState);
242}
243
244
245/**
246 * Sets the state if it has a certain value.
247 *
248 * @return true if xchg was done.
249 * @return false if xchg wasn't done.
250 */
251#ifdef DEBUG_HACKING
252#define rtTimerLnxCmpXchgState(penmState, enmNewState, enmCurState) rtTimerLnxCmpXchgStateDebug(penmState, enmNewState, enmCurState, __LINE__)
253static bool rtTimerLnxCmpXchgStateDebug(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState,
254 RTTIMERLNXSTATE enmCurState, uint32_t uLine)
255{
256 RTTIMERLNXSTATE enmOldState = enmCurState;
257 bool fRc = ASMAtomicCmpXchgExU32((uint32_t volatile *)penmState, enmNewState, enmCurState, (uint32_t *)&enmOldState);
258 RTTIMERLNX_LOG(("cxg %d -> %d - %d at %u\n", enmOldState, enmNewState, fRc, uLine));
259 return fRc;
260}
261#else
262DECLINLINE(bool) rtTimerLnxCmpXchgState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState,
263 RTTIMERLNXSTATE enmCurState)
264{
265 return ASMAtomicCmpXchgU32((uint32_t volatile *)penmState, enmNewState, enmCurState);
266}
267#endif
268
269
270/**
271 * Gets the state.
272 */
273DECLINLINE(RTTIMERLNXSTATE) rtTimerLnxGetState(RTTIMERLNXSTATE volatile *penmState)
274{
275 return (RTTIMERLNXSTATE)ASMAtomicUoReadU32((uint32_t volatile *)penmState);
276}
277
278#ifdef RTTIMER_LINUX_WITH_HRTIMER
279
280/**
281 * Converts a nano second time stamp to ktime_t.
282 *
283 * ASSUMES RTTimeSystemNanoTS() is implemented using ktime_get_ts().
284 *
285 * @returns ktime_t.
286 * @param cNanoSecs Nanoseconds.
287 */
288DECLINLINE(ktime_t) rtTimerLnxNanoToKt(uint64_t cNanoSecs)
289{
290 /* With some luck the compiler optimizes the division out of this... (Bet it doesn't.) */
291 return ktime_set(cNanoSecs / 1000000000, cNanoSecs % 1000000000);
292}
293
294/**
295 * Converts ktime_t to a nano second time stamp.
296 *
297 * ASSUMES RTTimeSystemNanoTS() is implemented using ktime_get_ts().
298 *
299 * @returns nano second time stamp.
300 * @param Kt ktime_t.
301 */
302DECLINLINE(uint64_t) rtTimerLnxKtToNano(ktime_t Kt)
303{
304 return ktime_to_ns(Kt);
305}
306
307#endif /* RTTIMER_LINUX_WITH_HRTIMER */
308
309/**
310 * Converts a nano second interval to jiffies.
311 *
312 * @returns Jiffies.
313 * @param cNanoSecs Nanoseconds.
314 */
315DECLINLINE(unsigned long) rtTimerLnxNanoToJiffies(uint64_t cNanoSecs)
316{
317 /* this can be made even better... */
318 if (cNanoSecs > (uint64_t)TICK_NSEC * MAX_JIFFY_OFFSET)
319 return MAX_JIFFY_OFFSET;
320# if ARCH_BITS == 32
321 if (RT_LIKELY(cNanoSecs <= UINT32_MAX))
322 return ((uint32_t)cNanoSecs + (TICK_NSEC-1)) / TICK_NSEC;
323# endif
324 return (cNanoSecs + (TICK_NSEC-1)) / TICK_NSEC;
325}
326
327
328/**
329 * Starts a sub-timer (RTTimerStart).
330 *
331 * @param pSubTimer The sub-timer to start.
332 * @param u64Now The current timestamp (RTTimeSystemNanoTS()).
333 * @param u64First The interval from u64Now to the first time the timer should fire.
334 * @param fPinned true = timer pinned to a specific CPU,
335 * false = timer can migrate between CPUs
336 * @param fHighRes Whether the user requested a high resolution timer or not.
337 * @param enmOldState The old timer state.
338 */
339static void rtTimerLnxStartSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, uint64_t u64Now, uint64_t u64First,
340 bool fPinned, bool fHighRes)
341{
342 /*
343 * Calc when it should start firing.
344 */
345 uint64_t u64NextTS = u64Now + u64First;
346 if (!fHighRes)
347 pSubTimer->u.Std.u64NextTS = u64NextTS;
348 RTTIMERLNX_LOG(("startsubtimer %p\n", pSubTimer->pParent));
349
350 pSubTimer->iTick = 0;
351
352#ifdef RTTIMER_LINUX_WITH_HRTIMER
353 if (fHighRes)
354 hrtimer_start(&pSubTimer->u.Hr.LnxTimer, rtTimerLnxNanoToKt(u64NextTS),
355 fPinned ? HRTIMER_MODE_ABS_PINNED : HRTIMER_MODE_ABS);
356 else
357#endif
358 {
359 unsigned long cJiffies = !u64First ? 0 : rtTimerLnxNanoToJiffies(u64First);
360 pSubTimer->u.Std.ulNextJiffies = jiffies + cJiffies;
361 pSubTimer->u.Std.fFirstAfterChg = true;
362#ifdef CONFIG_SMP
363 if (fPinned)
364 mod_timer_pinned(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
365 else
366#endif
367 mod_timer(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
368 }
369
370 /* Be a bit careful here since we could be racing the callback. */
371 if (!rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_STARTING))
372 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_MP_STARTING);
373}
374
375
376/**
377 * Stops a sub-timer (RTTimerStart and rtTimerLinuxMpEvent()).
378 *
379 * The caller has already changed the state, so we will not be in a callback
380 * situation wrt to the calling thread.
381 *
382 * @param pSubTimer The sub-timer.
383 * @param fHighRes Whether the user requested a high resolution timer or not.
384 */
385static void rtTimerLnxStopSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, bool fHighRes)
386{
387 RTTIMERLNX_LOG(("stopsubtimer %p %d\n", pSubTimer->pParent, fHighRes));
388#ifdef RTTIMER_LINUX_WITH_HRTIMER
389 if (fHighRes)
390 {
391 /* There is no equivalent to del_timer in the hrtimer API,
392 hrtimer_cancel() == del_timer_sync(). Just like the WARN_ON in
393 del_timer_sync() asserts, waiting for a timer callback to complete
394 is deadlock prone, so don't do it. */
395 int rc = hrtimer_try_to_cancel(&pSubTimer->u.Hr.LnxTimer);
396 if (rc < 0)
397 {
398 hrtimer_start(&pSubTimer->u.Hr.LnxTimer, ktime_set(KTIME_SEC_MAX, 0), HRTIMER_MODE_ABS);
399 hrtimer_try_to_cancel(&pSubTimer->u.Hr.LnxTimer);
400 }
401 }
402 else
403#endif
404 del_timer(&pSubTimer->u.Std.LnxTimer);
405
406 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED);
407}
408
409
410/**
411 * Used by RTTimerDestroy and rtTimerLnxCallbackDestroy to do the actual work.
412 *
413 * @param pTimer The timer in question.
414 */
415static void rtTimerLnxDestroyIt(PRTTIMER pTimer)
416{
417 RTSPINLOCK hSpinlock = pTimer->hSpinlock;
418 RTCPUID iCpu;
419 Assert(pTimer->fSuspended);
420 RTTIMERLNX_LOG(("destroyit %p\n", pTimer));
421
422 /*
423 * Remove the MP notifications first because it'll reduce the risk of
424 * us overtaking any MP event that might theoretically be racing us here.
425 */
426#ifdef CONFIG_SMP
427 if ( pTimer->cCpus > 1
428 && hSpinlock != NIL_RTSPINLOCK)
429 {
430 int rc = RTMpNotificationDeregister(rtTimerLinuxMpEvent, pTimer);
431 AssertRC(rc);
432 }
433#endif /* CONFIG_SMP */
434
435 /*
436 * Invalidate the handle.
437 */
438 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
439
440 /*
441 * Make sure all timers have stopped executing since we're stopping them in
442 * an asynchronous manner up in rtTimerLnxStopSubTimer.
443 */
444 iCpu = pTimer->cCpus;
445 while (iCpu-- > 0)
446 {
447#ifdef RTTIMER_LINUX_WITH_HRTIMER
448 if (pTimer->fHighRes)
449 hrtimer_cancel(&pTimer->aSubTimers[iCpu].u.Hr.LnxTimer);
450 else
451#endif
452 del_timer_sync(&pTimer->aSubTimers[iCpu].u.Std.LnxTimer);
453 }
454
455 /*
456 * Finally, free the resources.
457 */
458 RTMemFreeEx(pTimer, RT_OFFSETOF(RTTIMER, aSubTimers[pTimer->cCpus]));
459 if (hSpinlock != NIL_RTSPINLOCK)
460 RTSpinlockDestroy(hSpinlock);
461}
462
463
464/**
465 * Workqueue callback (no DECLCALLBACK!) for deferred destruction.
466 *
467 * @param pWork Pointer to the DtorWorkqueueItem member of our timer
468 * structure.
469 */
470static void rtTimerLnxDestroyDeferred(RTR0LNXWORKQUEUEITEM *pWork)
471{
472 PRTTIMER pTimer = RT_FROM_MEMBER(pWork, RTTIMER, DtorWorkqueueItem);
473 rtTimerLnxDestroyIt(pTimer);
474}
475
476
477/**
478 * Called when the timer was destroyed by the callback function.
479 *
480 * @param pTimer The timer.
481 * @param pSubTimer The sub-timer which we're handling, the state of this
482 * will be RTTIMERLNXSTATE_CALLBACK_DESTROYING.
483 */
484static void rtTimerLnxCallbackDestroy(PRTTIMER pTimer, PRTTIMERLNXSUBTIMER pSubTimer)
485{
486 /*
487 * If it's an omni timer, the last dude does the destroying.
488 */
489 if (pTimer->cCpus > 1)
490 {
491 uint32_t iCpu = pTimer->cCpus;
492 RTSpinlockAcquire(pTimer->hSpinlock);
493
494 Assert(pSubTimer->enmState == RTTIMERLNXSTATE_CB_DESTROYING);
495 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED);
496
497 while (iCpu-- > 0)
498 if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) != RTTIMERLNXSTATE_STOPPED)
499 {
500 RTSpinlockRelease(pTimer->hSpinlock);
501 return;
502 }
503
504 RTSpinlockRelease(pTimer->hSpinlock);
505 }
506
507 /*
508 * Destroying a timer from the callback is unsafe since the callout code
509 * might be touching the timer structure upon return (hrtimer does!). So,
510 * we have to defer the actual destruction to the IRPT workqueue.
511 */
512 rtR0LnxWorkqueuePush(&pTimer->DtorWorkqueueItem, rtTimerLnxDestroyDeferred);
513}
514
515
516#ifdef CONFIG_SMP
517/**
518 * Deal with a sub-timer that has migrated.
519 *
520 * @param pTimer The timer.
521 * @param pSubTimer The sub-timer.
522 */
523static void rtTimerLnxCallbackHandleMigration(PRTTIMER pTimer, PRTTIMERLNXSUBTIMER pSubTimer)
524{
525 RTTIMERLNXSTATE enmState;
526 if (pTimer->cCpus > 1)
527 RTSpinlockAcquire(pTimer->hSpinlock);
528
529 do
530 {
531 enmState = rtTimerLnxGetState(&pSubTimer->enmState);
532 switch (enmState)
533 {
534 case RTTIMERLNXSTATE_STOPPING:
535 case RTTIMERLNXSTATE_MP_STOPPING:
536 enmState = RTTIMERLNXSTATE_STOPPED;
537 case RTTIMERLNXSTATE_STOPPED:
538 break;
539
540 default:
541 AssertMsgFailed(("%d\n", enmState));
542 case RTTIMERLNXSTATE_STARTING:
543 case RTTIMERLNXSTATE_MP_STARTING:
544 case RTTIMERLNXSTATE_ACTIVE:
545 case RTTIMERLNXSTATE_CALLBACK:
546 case RTTIMERLNXSTATE_CB_STOPPING:
547 case RTTIMERLNXSTATE_CB_RESTARTING:
548 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, enmState))
549 enmState = RTTIMERLNXSTATE_STOPPED;
550 break;
551
552 case RTTIMERLNXSTATE_CB_DESTROYING:
553 {
554 if (pTimer->cCpus > 1)
555 RTSpinlockRelease(pTimer->hSpinlock);
556
557 rtTimerLnxCallbackDestroy(pTimer, pSubTimer);
558 return;
559 }
560 }
561 } while (enmState != RTTIMERLNXSTATE_STOPPED);
562
563 if (pTimer->cCpus > 1)
564 RTSpinlockRelease(pTimer->hSpinlock);
565}
566#endif /* CONFIG_SMP */
567
568
569/**
570 * The slow path of rtTimerLnxChangeToCallbackState.
571 *
572 * @returns true if changed successfully, false if not.
573 * @param pSubTimer The sub-timer.
574 */
575static bool rtTimerLnxChangeToCallbackStateSlow(PRTTIMERLNXSUBTIMER pSubTimer)
576{
577 for (;;)
578 {
579 RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pSubTimer->enmState);
580 switch (enmState)
581 {
582 case RTTIMERLNXSTATE_ACTIVE:
583 case RTTIMERLNXSTATE_STARTING:
584 case RTTIMERLNXSTATE_MP_STARTING:
585 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_CALLBACK, enmState))
586 return true;
587 break;
588
589 case RTTIMERLNXSTATE_CALLBACK:
590 case RTTIMERLNXSTATE_CB_STOPPING:
591 case RTTIMERLNXSTATE_CB_RESTARTING:
592 case RTTIMERLNXSTATE_CB_DESTROYING:
593 AssertMsgFailed(("%d\n", enmState));
594 default:
595 return false;
596 }
597 ASMNopPause();
598 }
599}
600
601
602/**
603 * Tries to change the sub-timer state to 'callback'.
604 *
605 * @returns true if changed successfully, false if not.
606 * @param pSubTimer The sub-timer.
607 */
608DECLINLINE(bool) rtTimerLnxChangeToCallbackState(PRTTIMERLNXSUBTIMER pSubTimer)
609{
610 if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_CALLBACK, RTTIMERLNXSTATE_ACTIVE)))
611 return true;
612 return rtTimerLnxChangeToCallbackStateSlow(pSubTimer);
613}
614
615
616#ifdef RTTIMER_LINUX_WITH_HRTIMER
617/**
618 * Timer callback function for high resolution timers.
619 *
620 * @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a
621 * one-shot or interval timer.
622 * @param pHrTimer Pointer to the sub-timer structure.
623 */
624static enum hrtimer_restart rtTimerLinuxHrCallback(struct hrtimer *pHrTimer)
625{
626 PRTTIMERLNXSUBTIMER pSubTimer = RT_FROM_MEMBER(pHrTimer, RTTIMERLNXSUBTIMER, u.Hr.LnxTimer);
627 PRTTIMER pTimer = pSubTimer->pParent;
628
629
630 RTTIMERLNX_LOG(("hrcallback %p\n", pTimer));
631 if (RT_UNLIKELY(!rtTimerLnxChangeToCallbackState(pSubTimer)))
632 return HRTIMER_NORESTART;
633
634#ifdef CONFIG_SMP
635 /*
636 * Check for unwanted migration.
637 */
638 if (pTimer->fAllCpus || pTimer->fSpecificCpu)
639 {
640 RTCPUID idCpu = RTMpCpuId();
641 if (RT_UNLIKELY( pTimer->fAllCpus
642 ? (RTCPUID)(pSubTimer - &pTimer->aSubTimers[0]) != idCpu
643 : pTimer->idCpu != idCpu))
644 {
645 rtTimerLnxCallbackHandleMigration(pTimer, pSubTimer);
646 return HRTIMER_NORESTART;
647 }
648 }
649#endif
650
651 if (pTimer->u64NanoInterval)
652 {
653 /*
654 * Periodic timer, run it and update the native timer afterwards so
655 * we can handle RTTimerStop and RTTimerChangeInterval from the
656 * callback as well as a racing control thread.
657 */
658 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
659 hrtimer_add_expires_ns(&pSubTimer->u.Hr.LnxTimer, ASMAtomicReadU64(&pTimer->u64NanoInterval));
660 if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CALLBACK)))
661 return HRTIMER_RESTART;
662 }
663 else
664 {
665 /*
666 * One shot timer (no omni), stop it before dispatching it.
667 * Allow RTTimerStart as well as RTTimerDestroy to be called from
668 * the callback.
669 */
670 ASMAtomicWriteBool(&pTimer->fSuspended, true);
671 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
672 if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CALLBACK)))
673 return HRTIMER_NORESTART;
674 }
675
676 /*
677 * Some state change occurred while we were in the callback routine.
678 */
679 for (;;)
680 {
681 RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pSubTimer->enmState);
682 switch (enmState)
683 {
684 case RTTIMERLNXSTATE_CB_DESTROYING:
685 rtTimerLnxCallbackDestroy(pTimer, pSubTimer);
686 return HRTIMER_NORESTART;
687
688 case RTTIMERLNXSTATE_CB_STOPPING:
689 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CB_STOPPING))
690 return HRTIMER_NORESTART;
691 break;
692
693 case RTTIMERLNXSTATE_CB_RESTARTING:
694 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CB_RESTARTING))
695 {
696 pSubTimer->iTick = 0;
697 hrtimer_set_expires(&pSubTimer->u.Hr.LnxTimer, rtTimerLnxNanoToKt(pSubTimer->uNsRestartAt));
698 return HRTIMER_RESTART;
699 }
700 break;
701
702 default:
703 AssertMsgFailed(("%d\n", enmState));
704 return HRTIMER_NORESTART;
705 }
706 ASMNopPause();
707 }
708}
709#endif /* RTTIMER_LINUX_WITH_HRTIMER */
710
711
712/**
713 * Timer callback function for standard timers.
714 *
715 * @param ulUser Address of the sub-timer structure.
716 */
717static void rtTimerLinuxStdCallback(unsigned long ulUser)
718{
719 PRTTIMERLNXSUBTIMER pSubTimer = (PRTTIMERLNXSUBTIMER)ulUser;
720 PRTTIMER pTimer = pSubTimer->pParent;
721
722 RTTIMERLNX_LOG(("stdcallback %p\n", pTimer));
723 if (RT_UNLIKELY(!rtTimerLnxChangeToCallbackState(pSubTimer)))
724 return;
725
726#ifdef CONFIG_SMP
727 /*
728 * Check for unwanted migration.
729 */
730 if (pTimer->fAllCpus || pTimer->fSpecificCpu)
731 {
732 RTCPUID idCpu = RTMpCpuId();
733 if (RT_UNLIKELY( pTimer->fAllCpus
734 ? (RTCPUID)(pSubTimer - &pTimer->aSubTimers[0]) != idCpu
735 : pTimer->idCpu != idCpu))
736 {
737 rtTimerLnxCallbackHandleMigration(pTimer, pSubTimer);
738 return;
739 }
740 }
741#endif
742
743 if (pTimer->u64NanoInterval)
744 {
745 /*
746 * Interval timer, calculate the next timeout.
747 *
748 * The first time around, we'll re-adjust the u.Std.u64NextTS to
749 * try prevent some jittering if we were started at a bad time.
750 */
751 const uint64_t iTick = ++pSubTimer->iTick;
752 uint64_t u64NanoInterval;
753 unsigned long cJiffies;
754 unsigned long flFlags;
755
756 spin_lock_irqsave(&pTimer->ChgIntLock, flFlags);
757 u64NanoInterval = pTimer->u64NanoInterval;
758 cJiffies = pTimer->cJiffies;
759 if (RT_UNLIKELY(pSubTimer->u.Std.fFirstAfterChg))
760 {
761 pSubTimer->u.Std.fFirstAfterChg = false;
762 pSubTimer->u.Std.u64NextTS = RTTimeSystemNanoTS();
763 pSubTimer->u.Std.ulNextJiffies = jiffies;
764 }
765 spin_unlock_irqrestore(&pTimer->ChgIntLock, flFlags);
766
767 pSubTimer->u.Std.u64NextTS += u64NanoInterval;
768 if (cJiffies)
769 {
770 pSubTimer->u.Std.ulNextJiffies += cJiffies;
771 /* Prevent overflows when the jiffies counter wraps around.
772 * Special thanks to Ken Preslan for helping debugging! */
773 while (time_before(pSubTimer->u.Std.ulNextJiffies, jiffies))
774 {
775 pSubTimer->u.Std.ulNextJiffies += cJiffies;
776 pSubTimer->u.Std.u64NextTS += u64NanoInterval;
777 }
778 }
779 else
780 {
781 const uint64_t u64NanoTS = RTTimeSystemNanoTS();
782 while (pSubTimer->u.Std.u64NextTS < u64NanoTS)
783 pSubTimer->u.Std.u64NextTS += u64NanoInterval;
784 pSubTimer->u.Std.ulNextJiffies = jiffies + rtTimerLnxNanoToJiffies(pSubTimer->u.Std.u64NextTS - u64NanoTS);
785 }
786
787 /*
788 * Run the timer and re-arm it unless the state changed .
789 * .
790 * We must re-arm it afterwards as we're not in a position to undo this .
791 * operation if for instance someone stopped or destroyed us while we .
792 * were in the callback. (Linux takes care of any races here.)
793 */
794 pTimer->pfnTimer(pTimer, pTimer->pvUser, iTick);
795 if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CALLBACK)))
796 {
797#ifdef CONFIG_SMP
798 if (pTimer->fSpecificCpu || pTimer->fAllCpus)
799 mod_timer_pinned(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
800 else
801#endif
802 mod_timer(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
803 return;
804 }
805 }
806 else
807 {
808 /*
809 * One shot timer, stop it before dispatching it.
810 * Allow RTTimerStart as well as RTTimerDestroy to be called from
811 * the callback.
812 */
813 ASMAtomicWriteBool(&pTimer->fSuspended, true);
814 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
815 if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CALLBACK)))
816 return;
817 }
818
819 /*
820 * Some state change occurred while we were in the callback routine.
821 */
822 for (;;)
823 {
824 RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pSubTimer->enmState);
825 switch (enmState)
826 {
827 case RTTIMERLNXSTATE_CB_DESTROYING:
828 rtTimerLnxCallbackDestroy(pTimer, pSubTimer);
829 return;
830
831 case RTTIMERLNXSTATE_CB_STOPPING:
832 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CB_STOPPING))
833 return;
834 break;
835
836 case RTTIMERLNXSTATE_CB_RESTARTING:
837 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CB_RESTARTING))
838 {
839 uint64_t u64NanoTS;
840 uint64_t u64NextTS;
841 unsigned long flFlags;
842
843 spin_lock_irqsave(&pTimer->ChgIntLock, flFlags);
844 u64NextTS = pSubTimer->uNsRestartAt;
845 u64NanoTS = RTTimeSystemNanoTS();
846 pSubTimer->iTick = 0;
847 pSubTimer->u.Std.u64NextTS = u64NextTS;
848 pSubTimer->u.Std.fFirstAfterChg = true;
849 pSubTimer->u.Std.ulNextJiffies = u64NextTS > u64NanoTS
850 ? jiffies + rtTimerLnxNanoToJiffies(u64NextTS - u64NanoTS)
851 : jiffies;
852 spin_unlock_irqrestore(&pTimer->ChgIntLock, flFlags);
853
854#ifdef CONFIG_SMP
855 if (pTimer->fSpecificCpu || pTimer->fAllCpus)
856 mod_timer_pinned(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
857 else
858#endif
859 mod_timer(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
860 return;
861 }
862 break;
863
864 default:
865 AssertMsgFailed(("%d\n", enmState));
866 return;
867 }
868 ASMNopPause();
869 }
870}
871
872
873#ifdef CONFIG_SMP
874
875/**
876 * Per-cpu callback function (RTMpOnAll/RTMpOnSpecific).
877 *
878 * @param idCpu The current CPU.
879 * @param pvUser1 Pointer to the timer.
880 * @param pvUser2 Pointer to the argument structure.
881 */
882static DECLCALLBACK(void) rtTimerLnxStartAllOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
883{
884 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
885 PRTTIMER pTimer = (PRTTIMER)pvUser1;
886 Assert(idCpu < pTimer->cCpus);
887 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[idCpu], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
888}
889
890
891/**
892 * Worker for RTTimerStart() that takes care of the ugly bits.
893 *
894 * @returns RTTimerStart() return value.
895 * @param pTimer The timer.
896 * @param pArgs The argument structure.
897 */
898static int rtTimerLnxOmniStart(PRTTIMER pTimer, PRTTIMERLINUXSTARTONCPUARGS pArgs)
899{
900 RTCPUID iCpu;
901 RTCPUSET OnlineSet;
902 RTCPUSET OnlineSet2;
903 int rc2;
904
905 /*
906 * Prepare all the sub-timers for the startup and then flag the timer
907 * as a whole as non-suspended, make sure we get them all before
908 * clearing fSuspended as the MP handler will be waiting on this
909 * should something happen while we're looping.
910 */
911 RTSpinlockAcquire(pTimer->hSpinlock);
912
913 /* Just make it a omni timer restriction that no stop/start races are allowed. */
914 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
915 if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) != RTTIMERLNXSTATE_STOPPED)
916 {
917 RTSpinlockRelease(pTimer->hSpinlock);
918 return VERR_TIMER_BUSY;
919 }
920
921 do
922 {
923 RTMpGetOnlineSet(&OnlineSet);
924 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
925 {
926 Assert(pTimer->aSubTimers[iCpu].enmState != RTTIMERLNXSTATE_MP_STOPPING);
927 rtTimerLnxSetState(&pTimer->aSubTimers[iCpu].enmState,
928 RTCpuSetIsMember(&OnlineSet, iCpu)
929 ? RTTIMERLNXSTATE_STARTING
930 : RTTIMERLNXSTATE_STOPPED);
931 }
932 } while (!RTCpuSetIsEqual(&OnlineSet, RTMpGetOnlineSet(&OnlineSet2)));
933
934 ASMAtomicWriteBool(&pTimer->fSuspended, false);
935
936 RTSpinlockRelease(pTimer->hSpinlock);
937
938 /*
939 * Start them (can't find any exported function that allows me to
940 * do this without the cross calls).
941 */
942 pArgs->u64Now = RTTimeSystemNanoTS();
943 rc2 = RTMpOnAll(rtTimerLnxStartAllOnCpu, pTimer, pArgs);
944 AssertRC(rc2); /* screw this if it fails. */
945
946 /*
947 * Reset the sub-timers who didn't start up (ALL CPUs case).
948 */
949 RTSpinlockAcquire(pTimer->hSpinlock);
950
951 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
952 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_STARTING))
953 {
954 /** @todo very odd case for a rainy day. Cpus that temporarily went offline while
955 * we were between calls needs to nudged as the MP handler will ignore events for
956 * them because of the STARTING state. This is an extremely unlikely case - not that
957 * that means anything in my experience... ;-) */
958 RTTIMERLNX_LOG(("what!? iCpu=%u -> didn't start\n", iCpu));
959 }
960
961 RTSpinlockRelease(pTimer->hSpinlock);
962
963 return VINF_SUCCESS;
964}
965
966
967/**
968 * Worker for RTTimerStop() that takes care of the ugly SMP bits.
969 *
970 * @returns true if there was any active callbacks, false if not.
971 * @param pTimer The timer (valid).
972 * @param fForDestroy Whether this is for RTTimerDestroy or not.
973 */
974static bool rtTimerLnxOmniStop(PRTTIMER pTimer, bool fForDestroy)
975{
976 bool fActiveCallbacks = false;
977 RTCPUID iCpu;
978 RTTIMERLNXSTATE enmState;
979
980
981 /*
982 * Mark the timer as suspended and flag all timers as stopping, except
983 * for those being stopped by an MP event.
984 */
985 RTSpinlockAcquire(pTimer->hSpinlock);
986
987 ASMAtomicWriteBool(&pTimer->fSuspended, true);
988 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
989 {
990 for (;;)
991 {
992 enmState = rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState);
993 if ( enmState == RTTIMERLNXSTATE_STOPPED
994 || enmState == RTTIMERLNXSTATE_MP_STOPPING)
995 break;
996 if ( enmState == RTTIMERLNXSTATE_CALLBACK
997 || enmState == RTTIMERLNXSTATE_CB_STOPPING
998 || enmState == RTTIMERLNXSTATE_CB_RESTARTING)
999 {
1000 Assert(enmState != RTTIMERLNXSTATE_CB_STOPPING || fForDestroy);
1001 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState,
1002 !fForDestroy ? RTTIMERLNXSTATE_CB_STOPPING : RTTIMERLNXSTATE_CB_DESTROYING,
1003 enmState))
1004 {
1005 fActiveCallbacks = true;
1006 break;
1007 }
1008 }
1009 else
1010 {
1011 Assert(enmState == RTTIMERLNXSTATE_ACTIVE);
1012 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPING, enmState))
1013 break;
1014 }
1015 ASMNopPause();
1016 }
1017 }
1018
1019 RTSpinlockRelease(pTimer->hSpinlock);
1020
1021 /*
1022 * Do the actual stopping. Fortunately, this doesn't require any IPIs.
1023 * Unfortunately it cannot be done synchronously.
1024 */
1025 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
1026 if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) == RTTIMERLNXSTATE_STOPPING)
1027 rtTimerLnxStopSubTimer(&pTimer->aSubTimers[iCpu], pTimer->fHighRes);
1028
1029 return fActiveCallbacks;
1030}
1031
1032
1033/**
1034 * Per-cpu callback function (RTMpOnSpecific) used by rtTimerLinuxMpEvent()
1035 * to start a sub-timer on a cpu that just have come online.
1036 *
1037 * @param idCpu The current CPU.
1038 * @param pvUser1 Pointer to the timer.
1039 * @param pvUser2 Pointer to the argument structure.
1040 */
1041static DECLCALLBACK(void) rtTimerLinuxMpStartOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
1042{
1043 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
1044 PRTTIMER pTimer = (PRTTIMER)pvUser1;
1045 RTSPINLOCK hSpinlock;
1046 Assert(idCpu < pTimer->cCpus);
1047
1048 /*
1049 * We have to be kind of careful here as we might be racing RTTimerStop
1050 * (and/or RTTimerDestroy, thus the paranoia.
1051 */
1052 hSpinlock = pTimer->hSpinlock;
1053 if ( hSpinlock != NIL_RTSPINLOCK
1054 && pTimer->u32Magic == RTTIMER_MAGIC)
1055 {
1056 RTSpinlockAcquire(hSpinlock);
1057
1058 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
1059 && pTimer->u32Magic == RTTIMER_MAGIC)
1060 {
1061 /* We're sane and the timer is not suspended yet. */
1062 PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
1063 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
1064 rtTimerLnxStartSubTimer(pSubTimer, pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
1065 }
1066
1067 RTSpinlockRelease(hSpinlock);
1068 }
1069}
1070
1071
1072/**
1073 * MP event notification callback.
1074 *
1075 * @param enmEvent The event.
1076 * @param idCpu The cpu it applies to.
1077 * @param pvUser The timer.
1078 */
1079static DECLCALLBACK(void) rtTimerLinuxMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser)
1080{
1081 PRTTIMER pTimer = (PRTTIMER)pvUser;
1082 PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
1083 RTSPINLOCK hSpinlock;
1084
1085 Assert(idCpu < pTimer->cCpus);
1086
1087 /*
1088 * Some initial paranoia.
1089 */
1090 if (pTimer->u32Magic != RTTIMER_MAGIC)
1091 return;
1092 hSpinlock = pTimer->hSpinlock;
1093 if (hSpinlock == NIL_RTSPINLOCK)
1094 return;
1095
1096 RTSpinlockAcquire(hSpinlock);
1097
1098 /* Is it active? */
1099 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
1100 && pTimer->u32Magic == RTTIMER_MAGIC)
1101 {
1102 switch (enmEvent)
1103 {
1104 /*
1105 * Try do it without leaving the spin lock, but if we have to, retake it
1106 * when we're on the right cpu.
1107 */
1108 case RTMPEVENT_ONLINE:
1109 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
1110 {
1111 RTTIMERLINUXSTARTONCPUARGS Args;
1112 Args.u64Now = RTTimeSystemNanoTS();
1113 Args.u64First = 0;
1114
1115 if (RTMpCpuId() == idCpu)
1116 rtTimerLnxStartSubTimer(pSubTimer, Args.u64Now, Args.u64First, true /*fPinned*/, pTimer->fHighRes);
1117 else
1118 {
1119 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED); /* we'll recheck it. */
1120 RTSpinlockRelease(hSpinlock);
1121
1122 RTMpOnSpecific(idCpu, rtTimerLinuxMpStartOnCpu, pTimer, &Args);
1123 return; /* we've left the spinlock */
1124 }
1125 }
1126 break;
1127
1128 /*
1129 * The CPU is (going) offline, make sure the sub-timer is stopped.
1130 *
1131 * Linux will migrate it to a different CPU, but we don't want this. The
1132 * timer function is checking for this.
1133 */
1134 case RTMPEVENT_OFFLINE:
1135 {
1136 RTTIMERLNXSTATE enmState;
1137 while ( (enmState = rtTimerLnxGetState(&pSubTimer->enmState)) == RTTIMERLNXSTATE_ACTIVE
1138 || enmState == RTTIMERLNXSTATE_CALLBACK
1139 || enmState == RTTIMERLNXSTATE_CB_RESTARTING)
1140 {
1141 if (enmState == RTTIMERLNXSTATE_ACTIVE)
1142 {
1143 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STOPPING, RTTIMERLNXSTATE_ACTIVE))
1144 {
1145 RTSpinlockRelease(hSpinlock);
1146
1147 rtTimerLnxStopSubTimer(pSubTimer, pTimer->fHighRes);
1148 return; /* we've left the spinlock */
1149 }
1150 }
1151 else if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_CB_STOPPING, enmState))
1152 break;
1153
1154 /* State not stable, try again. */
1155 ASMNopPause();
1156 }
1157 break;
1158 }
1159 }
1160 }
1161
1162 RTSpinlockRelease(hSpinlock);
1163}
1164
1165#endif /* CONFIG_SMP */
1166
1167
1168/**
1169 * Callback function use by RTTimerStart via RTMpOnSpecific to start a timer
1170 * running on a specific CPU.
1171 *
1172 * @param idCpu The current CPU.
1173 * @param pvUser1 Pointer to the timer.
1174 * @param pvUser2 Pointer to the argument structure.
1175 */
1176static DECLCALLBACK(void) rtTimerLnxStartOnSpecificCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
1177{
1178 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
1179 PRTTIMER pTimer = (PRTTIMER)pvUser1;
1180 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
1181}
1182
1183
1184RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
1185{
1186 RTTIMERLINUXSTARTONCPUARGS Args;
1187 int rc2;
1188
1189 /*
1190 * Validate.
1191 */
1192 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
1193 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
1194
1195 if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
1196 return VERR_TIMER_ACTIVE;
1197 RTTIMERLNX_LOG(("start %p cCpus=%d\n", pTimer, pTimer->cCpus));
1198
1199 Args.u64First = u64First;
1200#ifdef CONFIG_SMP
1201 /*
1202 * Omni timer?
1203 */
1204 if (pTimer->fAllCpus)
1205 return rtTimerLnxOmniStart(pTimer, &Args);
1206#endif
1207
1208 /*
1209 * Simple timer - Pretty straight forward if it wasn't for restarting.
1210 */
1211 Args.u64Now = RTTimeSystemNanoTS();
1212 ASMAtomicWriteU64(&pTimer->aSubTimers[0].uNsRestartAt, Args.u64Now + u64First);
1213 for (;;)
1214 {
1215 RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pTimer->aSubTimers[0].enmState);
1216 switch (enmState)
1217 {
1218 case RTTIMERLNXSTATE_STOPPED:
1219 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STARTING, RTTIMERLNXSTATE_STOPPED))
1220 {
1221 ASMAtomicWriteBool(&pTimer->fSuspended, false);
1222 if (!pTimer->fSpecificCpu)
1223 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], Args.u64Now, Args.u64First,
1224 false /*fPinned*/, pTimer->fHighRes);
1225 else
1226 {
1227 rc2 = RTMpOnSpecific(pTimer->idCpu, rtTimerLnxStartOnSpecificCpu, pTimer, &Args);
1228 if (RT_FAILURE(rc2))
1229 {
1230 /* Suspend it, the cpu id is probably invalid or offline. */
1231 ASMAtomicWriteBool(&pTimer->fSuspended, true);
1232 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPED);
1233 return rc2;
1234 }
1235 }
1236 return VINF_SUCCESS;
1237 }
1238 break;
1239
1240 case RTTIMERLNXSTATE_CALLBACK:
1241 case RTTIMERLNXSTATE_CB_STOPPING:
1242 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_CB_RESTARTING, enmState))
1243 {
1244 ASMAtomicWriteBool(&pTimer->fSuspended, false);
1245 return VINF_SUCCESS;
1246 }
1247 break;
1248
1249 default:
1250 AssertMsgFailed(("%d\n", enmState));
1251 return VERR_INTERNAL_ERROR_4;
1252 }
1253 ASMNopPause();
1254 }
1255}
1256RT_EXPORT_SYMBOL(RTTimerStart);
1257
1258
1259/**
1260 * Common worker for RTTimerStop and RTTimerDestroy.
1261 *
1262 * @returns true if there was any active callbacks, false if not.
1263 * @param pTimer The timer to stop.
1264 * @param fForDestroy Whether it's RTTimerDestroy calling or not.
1265 */
1266static bool rtTimerLnxStop(PRTTIMER pTimer, bool fForDestroy)
1267{
1268 RTTIMERLNX_LOG(("lnxstop %p %d\n", pTimer, fForDestroy));
1269#ifdef CONFIG_SMP
1270 /*
1271 * Omni timer?
1272 */
1273 if (pTimer->fAllCpus)
1274 return rtTimerLnxOmniStop(pTimer, fForDestroy);
1275#endif
1276
1277 /*
1278 * Simple timer.
1279 */
1280 ASMAtomicWriteBool(&pTimer->fSuspended, true);
1281 for (;;)
1282 {
1283 RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pTimer->aSubTimers[0].enmState);
1284 switch (enmState)
1285 {
1286 case RTTIMERLNXSTATE_ACTIVE:
1287 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPING, RTTIMERLNXSTATE_ACTIVE))
1288 {
1289 rtTimerLnxStopSubTimer(&pTimer->aSubTimers[0], pTimer->fHighRes);
1290 return false;
1291 }
1292 break;
1293
1294 case RTTIMERLNXSTATE_CALLBACK:
1295 case RTTIMERLNXSTATE_CB_RESTARTING:
1296 case RTTIMERLNXSTATE_CB_STOPPING:
1297 Assert(enmState != RTTIMERLNXSTATE_CB_STOPPING || fForDestroy);
1298 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState,
1299 !fForDestroy ? RTTIMERLNXSTATE_CB_STOPPING : RTTIMERLNXSTATE_CB_DESTROYING,
1300 enmState))
1301 return true;
1302 break;
1303
1304 case RTTIMERLNXSTATE_STOPPED:
1305 return VINF_SUCCESS;
1306
1307 case RTTIMERLNXSTATE_CB_DESTROYING:
1308 AssertMsgFailed(("enmState=%d pTimer=%p\n", enmState, pTimer));
1309 return true;
1310
1311 default:
1312 case RTTIMERLNXSTATE_STARTING:
1313 case RTTIMERLNXSTATE_MP_STARTING:
1314 case RTTIMERLNXSTATE_STOPPING:
1315 case RTTIMERLNXSTATE_MP_STOPPING:
1316 AssertMsgFailed(("enmState=%d pTimer=%p\n", enmState, pTimer));
1317 return false;
1318 }
1319
1320 /* State not stable, try again. */
1321 ASMNopPause();
1322 }
1323}
1324
1325
1326RTDECL(int) RTTimerStop(PRTTIMER pTimer)
1327{
1328 /*
1329 * Validate.
1330 */
1331 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
1332 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
1333 RTTIMERLNX_LOG(("stop %p\n", pTimer));
1334
1335 if (ASMAtomicUoReadBool(&pTimer->fSuspended))
1336 return VERR_TIMER_SUSPENDED;
1337
1338 rtTimerLnxStop(pTimer, false /*fForDestroy*/);
1339 return VINF_SUCCESS;
1340}
1341RT_EXPORT_SYMBOL(RTTimerStop);
1342
1343
1344RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
1345{
1346 unsigned long cJiffies;
1347 unsigned long flFlags;
1348
1349 /*
1350 * Validate.
1351 */
1352 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
1353 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
1354 AssertReturn(u64NanoInterval, VERR_INVALID_PARAMETER);
1355 RTTIMERLNX_LOG(("change %p %llu\n", pTimer, u64NanoInterval));
1356
1357#ifdef RTTIMER_LINUX_WITH_HRTIMER
1358 /*
1359 * For the high resolution timers it is easy since we don't care so much
1360 * about when it is applied to the sub-timers.
1361 */
1362 if (pTimer->fHighRes)
1363 {
1364 ASMAtomicWriteU64(&pTimer->u64NanoInterval, u64NanoInterval);
1365 return VINF_SUCCESS;
1366 }
1367#endif
1368
1369 /*
1370 * Standard timers have a bit more complicated way of calculating
1371 * their interval and such. So, forget omni timers for now.
1372 */
1373 if (pTimer->cCpus > 1)
1374 return VERR_NOT_SUPPORTED;
1375
1376 cJiffies = u64NanoInterval / RTTimerGetSystemGranularity();
1377 if (cJiffies * RTTimerGetSystemGranularity() != u64NanoInterval)
1378 cJiffies = 0;
1379
1380 spin_lock_irqsave(&pTimer->ChgIntLock, flFlags);
1381 pTimer->aSubTimers[0].u.Std.fFirstAfterChg = true;
1382 pTimer->cJiffies = cJiffies;
1383 ASMAtomicWriteU64(&pTimer->u64NanoInterval, u64NanoInterval);
1384 spin_unlock_irqrestore(&pTimer->ChgIntLock, flFlags);
1385 return VINF_SUCCESS;
1386}
1387RT_EXPORT_SYMBOL(RTTimerChangeInterval);
1388
1389
1390RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
1391{
1392 bool fCanDestroy;
1393
1394 /*
1395 * Validate. It's ok to pass NULL pointer.
1396 */
1397 if (pTimer == /*NIL_RTTIMER*/ NULL)
1398 return VINF_SUCCESS;
1399 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
1400 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
1401 RTTIMERLNX_LOG(("destroy %p\n", pTimer));
1402/** @todo We should invalidate the magic here! */
1403
1404 /*
1405 * Stop the timer if it's still active, then destroy it if we can.
1406 */
1407 if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
1408 fCanDestroy = rtTimerLnxStop(pTimer, true /*fForDestroy*/);
1409 else
1410 {
1411 uint32_t iCpu = pTimer->cCpus;
1412 if (pTimer->cCpus > 1)
1413 RTSpinlockAcquire(pTimer->hSpinlock);
1414
1415 fCanDestroy = true;
1416 while (iCpu-- > 0)
1417 {
1418 for (;;)
1419 {
1420 RTTIMERLNXSTATE enmState = rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState);
1421 switch (enmState)
1422 {
1423 case RTTIMERLNXSTATE_CALLBACK:
1424 case RTTIMERLNXSTATE_CB_RESTARTING:
1425 case RTTIMERLNXSTATE_CB_STOPPING:
1426 if (!rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_CB_DESTROYING, enmState))
1427 continue;
1428 fCanDestroy = false;
1429 break;
1430
1431 case RTTIMERLNXSTATE_CB_DESTROYING:
1432 AssertMsgFailed(("%d\n", enmState));
1433 fCanDestroy = false;
1434 break;
1435 default:
1436 break;
1437 }
1438 break;
1439 }
1440 }
1441
1442 if (pTimer->cCpus > 1)
1443 RTSpinlockReleaseNoInts(pTimer->hSpinlock);
1444 }
1445
1446 if (fCanDestroy)
1447 {
1448 /* For paranoid reasons, defer actually destroying the semaphore when
1449 in atomic or interrupt context. */
1450#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 32)
1451 if (in_atomic() || in_interrupt())
1452#else
1453 if (in_interrupt())
1454#endif
1455 rtR0LnxWorkqueuePush(&pTimer->DtorWorkqueueItem, rtTimerLnxDestroyDeferred);
1456 else
1457 rtTimerLnxDestroyIt(pTimer);
1458 }
1459
1460 return VINF_SUCCESS;
1461}
1462RT_EXPORT_SYMBOL(RTTimerDestroy);
1463
1464
1465RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
1466{
1467 PRTTIMER pTimer;
1468 RTCPUID iCpu;
1469 unsigned cCpus;
1470 int rc;
1471
1472 rtR0LnxWorkqueueFlush(); /* for 2.4 */
1473 *ppTimer = NULL;
1474
1475 /*
1476 * Validate flags.
1477 */
1478 if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
1479 return VERR_INVALID_PARAMETER;
1480 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
1481 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
1482 && !RTMpIsCpuPossible(RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK)))
1483 return VERR_CPU_NOT_FOUND;
1484
1485 /*
1486 * Allocate the timer handler.
1487 */
1488 cCpus = 1;
1489#ifdef CONFIG_SMP
1490 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
1491 {
1492 cCpus = RTMpGetMaxCpuId() + 1;
1493 Assert(cCpus <= RTCPUSET_MAX_CPUS); /* On linux we have a 1:1 relationship between cpuid and set index. */
1494 AssertReturn(u64NanoInterval, VERR_NOT_IMPLEMENTED); /* We don't implement single shot on all cpus, sorry. */
1495 }
1496#endif
1497
1498 rc = RTMemAllocEx(RT_OFFSETOF(RTTIMER, aSubTimers[cCpus]), 0,
1499 RTMEMALLOCEX_FLAGS_ZEROED | RTMEMALLOCEX_FLAGS_ANY_CTX_FREE, (void **)&pTimer);
1500 if (RT_FAILURE(rc))
1501 return rc;
1502
1503 /*
1504 * Initialize it.
1505 */
1506 pTimer->u32Magic = RTTIMER_MAGIC;
1507 pTimer->hSpinlock = NIL_RTSPINLOCK;
1508 pTimer->fSuspended = true;
1509 pTimer->fHighRes = !!(fFlags & RTTIMER_FLAGS_HIGH_RES);
1510#ifdef CONFIG_SMP
1511 pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
1512 pTimer->fAllCpus = (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL;
1513 pTimer->idCpu = pTimer->fSpecificCpu
1514 ? RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK)
1515 : NIL_RTCPUID;
1516#else
1517 pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
1518 pTimer->idCpu = RTMpCpuId();
1519#endif
1520 pTimer->cCpus = cCpus;
1521 pTimer->pfnTimer = pfnTimer;
1522 pTimer->pvUser = pvUser;
1523 pTimer->u64NanoInterval = u64NanoInterval;
1524 pTimer->cJiffies = u64NanoInterval / RTTimerGetSystemGranularity();
1525 if (pTimer->cJiffies * RTTimerGetSystemGranularity() != u64NanoInterval)
1526 pTimer->cJiffies = 0;
1527 spin_lock_init(&pTimer->ChgIntLock);
1528
1529 for (iCpu = 0; iCpu < cCpus; iCpu++)
1530 {
1531#ifdef RTTIMER_LINUX_WITH_HRTIMER
1532 if (pTimer->fHighRes)
1533 {
1534 hrtimer_init(&pTimer->aSubTimers[iCpu].u.Hr.LnxTimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1535 pTimer->aSubTimers[iCpu].u.Hr.LnxTimer.function = rtTimerLinuxHrCallback;
1536 }
1537 else
1538#endif
1539 {
1540 init_timer(&pTimer->aSubTimers[iCpu].u.Std.LnxTimer);
1541 pTimer->aSubTimers[iCpu].u.Std.LnxTimer.data = (unsigned long)&pTimer->aSubTimers[iCpu];
1542 pTimer->aSubTimers[iCpu].u.Std.LnxTimer.function = rtTimerLinuxStdCallback;
1543 pTimer->aSubTimers[iCpu].u.Std.LnxTimer.expires = jiffies;
1544 pTimer->aSubTimers[iCpu].u.Std.u64NextTS = 0;
1545 }
1546 pTimer->aSubTimers[iCpu].iTick = 0;
1547 pTimer->aSubTimers[iCpu].pParent = pTimer;
1548 pTimer->aSubTimers[iCpu].enmState = RTTIMERLNXSTATE_STOPPED;
1549 }
1550
1551#ifdef CONFIG_SMP
1552 /*
1553 * If this is running on ALL cpus, we'll have to register a callback
1554 * for MP events (so timers can be started/stopped on cpus going
1555 * online/offline). We also create the spinlock for synchronizing
1556 * stop/start/mp-event.
1557 */
1558 if (cCpus > 1)
1559 {
1560 int rc = RTSpinlockCreate(&pTimer->hSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "RTTimerLnx");
1561 if (RT_SUCCESS(rc))
1562 rc = RTMpNotificationRegister(rtTimerLinuxMpEvent, pTimer);
1563 else
1564 pTimer->hSpinlock = NIL_RTSPINLOCK;
1565 if (RT_FAILURE(rc))
1566 {
1567 RTTimerDestroy(pTimer);
1568 return rc;
1569 }
1570 }
1571#endif /* CONFIG_SMP */
1572
1573 RTTIMERLNX_LOG(("create %p hires=%d fFlags=%#x cCpus=%u\n", pTimer, pTimer->fHighRes, fFlags, cCpus));
1574 *ppTimer = pTimer;
1575 return VINF_SUCCESS;
1576}
1577RT_EXPORT_SYMBOL(RTTimerCreateEx);
1578
1579
1580RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
1581{
1582#if 0 /** @todo Not sure if this is what we want or not... Add new API for
1583 * querying the resolution of the high res timers? */
1584 struct timespec Ts;
1585 int rc = hrtimer_get_res(CLOCK_MONOTONIC, &Ts);
1586 if (!rc)
1587 {
1588 Assert(!Ts.tv_sec);
1589 return Ts.tv_nsec;
1590 }
1591#endif
1592 return 1000000000 / HZ; /* ns */
1593}
1594RT_EXPORT_SYMBOL(RTTimerGetSystemGranularity);
1595
1596
1597RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
1598{
1599 return VERR_NOT_SUPPORTED;
1600}
1601RT_EXPORT_SYMBOL(RTTimerRequestSystemGranularity);
1602
1603
1604RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
1605{
1606 return VERR_NOT_SUPPORTED;
1607}
1608RT_EXPORT_SYMBOL(RTTimerReleaseSystemGranularity);
1609
1610
1611RTDECL(bool) RTTimerCanDoHighResolution(void)
1612{
1613#ifdef RTTIMER_LINUX_WITH_HRTIMER
1614 return true;
1615#else
1616 return false;
1617#endif
1618}
1619RT_EXPORT_SYMBOL(RTTimerCanDoHighResolution);
1620
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette