VirtualBox

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

Last change on this file since 32619 was 32572, checked in by vboxsync, 15 years ago

VMM,SUPDrv,IPRT: More changes for related to the priodic preemption timer. (still disabled)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.3 KB
Line 
1/* $Id: timer-r0drv-linux.c 32572 2010-09-16 16:18:12Z vboxsync $ */
2/** @file
3 * IPRT - Timers, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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_HAVE_HRTIMER
47 * Whether the kernel support high resolution timers (Linux kernel versions
48 * 2.6.28 and later (hrtimer_add_expires_ns()). */
49#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
50# define RTTIMER_LINUX_HAVE_HRTIMER
51#endif
52
53/** @def RTTIMER_LINUX_ONLY_HRTIMER
54 * Whether to use high resolution timers for everything or not. Implies
55 * RTTIMER_LINUX_WITH_HRTIMER. */
56#if !defined(RTTIMER_LINUX_ONLY_HRTIMER) \
57 && defined(RTTIMER_LINUX_HAVE_HRTIMER) \
58 && 0 /* currently disabled */
59# define RTTIMER_LINUX_ONLY_HRTIMER
60#endif
61
62/* This check must match the ktime usage in rtTimeGetSystemNanoTS() / time-r0drv-linux.c. */
63#if defined(RTTIMER_LINUX_ONLY_HRTIMER) \
64 && !defined(RTTIMER_LINUX_HAVE_HRTIMER)
65# error "RTTIMER_LINUX_ONLY_HRTIMER requires 2.6.28 or later, sorry."
66#endif
67
68/** @def RTTIMER_LINUX_WITH_HRTIMER
69 * Whether to use high resolution timers. */
70#if !defined(RTTIMER_LINUX_WITH_HRTIMER) \
71 && defined(RTTIMER_LINUX_HAVE_HRTIMER)
72# define RTTIMER_LINUX_WITH_HRTIMER
73#endif
74
75#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)
76# define mod_timer_pinned mod_timer
77# define HRTIMER_MODE_ABS_PINNED HRTIMER_MODE_ABS
78#endif
79
80
81/*******************************************************************************
82* Structures and Typedefs *
83*******************************************************************************/
84/**
85 * Timer state machine.
86 *
87 * This is used to try handle the issues with MP events and
88 * timers that runs on all CPUs. It's relatively nasty :-/
89 */
90typedef enum RTTIMERLNXSTATE
91{
92 /** Stopped. */
93 RTTIMERLNXSTATE_STOPPED = 0,
94 /** Transient state; next ACTIVE. */
95 RTTIMERLNXSTATE_STARTING,
96 /** Transient state; next ACTIVE. (not really necessary) */
97 RTTIMERLNXSTATE_MP_STARTING,
98 /** Active. */
99 RTTIMERLNXSTATE_ACTIVE,
100 /** Transient state; next STOPPED. */
101 RTTIMERLNXSTATE_STOPPING,
102 /** Transient state; next STOPPED. */
103 RTTIMERLNXSTATE_MP_STOPPING,
104 /** The usual 32-bit hack. */
105 RTTIMERLNXSTATE_32BIT_HACK = 0x7fffffff
106} RTTIMERLNXSTATE;
107
108
109/**
110 * A Linux sub-timer.
111 */
112typedef struct RTTIMERLNXSUBTIMER
113{
114 /** Timer specific data. */
115 union
116 {
117#if defined(RTTIMER_LINUX_WITH_HRTIMER)
118 /** High resolution timer. */
119 struct
120 {
121 /** The linux timer structure. */
122 struct hrtimer LnxTimer;
123 } Hr;
124#endif
125#ifndef RTTIMER_LINUX_ONLY_HRTIMER
126 /** Standard timer. */
127 struct
128 {
129 /** The linux timer structure. */
130 struct timer_list LnxTimer;
131 /** The start of the current run (ns).
132 * This is used to calculate when the timer ought to fire the next time. */
133 uint64_t u64StartTS;
134 /** The start of the current run (ns).
135 * This is used to calculate when the timer ought to fire the next time. */
136 uint64_t u64NextTS;
137 /** The u64NextTS in jiffies. */
138 unsigned long ulNextJiffies;
139 } Std;
140#endif
141 } u;
142 /** The current tick number (since u.Std.u64StartTS). */
143 uint64_t iTick;
144 /** Pointer to the parent timer. */
145 PRTTIMER pParent;
146 /** The current sub-timer state. */
147 RTTIMERLNXSTATE volatile enmState;
148} RTTIMERLNXSUBTIMER;
149/** Pointer to a linux sub-timer. */
150typedef RTTIMERLNXSUBTIMER *PRTTIMERLNXSUBTIMER;
151
152
153/**
154 * The internal representation of an Linux timer handle.
155 */
156typedef struct RTTIMER
157{
158 /** Magic.
159 * This is RTTIMER_MAGIC, but changes to something else before the timer
160 * is destroyed to indicate clearly that thread should exit. */
161 uint32_t volatile u32Magic;
162 /** Spinlock synchronizing the fSuspended and MP event handling.
163 * This is NIL_RTSPINLOCK if cCpus == 1. */
164 RTSPINLOCK hSpinlock;
165 /** Flag indicating that the timer is suspended. */
166 bool volatile fSuspended;
167 /** Whether the timer must run on one specific CPU or not. */
168 bool fSpecificCpu;
169#ifdef CONFIG_SMP
170 /** Whether the timer must run on all CPUs or not. */
171 bool fAllCpus;
172#endif /* else: All -> specific on non-SMP kernels */
173 /** Whether it is a high resolution timer or a standard one. */
174 bool fHighRes;
175 /** The id of the CPU it must run on if fSpecificCpu is set. */
176 RTCPUID idCpu;
177 /** The number of CPUs this timer should run on. */
178 RTCPUID cCpus;
179 /** Callback. */
180 PFNRTTIMER pfnTimer;
181 /** User argument. */
182 void *pvUser;
183 /** The timer interval. 0 if one-shot. */
184 uint64_t volatile u64NanoInterval;
185#ifndef RTTIMER_LINUX_ONLY_HRTIMER
186 /** This is set to the number of jiffies between ticks if the interval is
187 * an exact number of jiffies. */
188 unsigned long cJiffies;
189#endif
190 /** Sub-timers.
191 * Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
192 * an entry for all possible cpus. In that case the index will be the same as
193 * for the RTCpuSet. */
194 RTTIMERLNXSUBTIMER aSubTimers[1];
195} RTTIMER;
196
197
198/**
199 * A rtTimerLinuxStartOnCpu and rtTimerLinuxStartOnCpu argument package.
200 */
201typedef struct RTTIMERLINUXSTARTONCPUARGS
202{
203 /** The current time (RTTimeNanoTS). */
204 uint64_t u64Now;
205 /** When to start firing (delta). */
206 uint64_t u64First;
207} RTTIMERLINUXSTARTONCPUARGS;
208/** Pointer to a rtTimerLinuxStartOnCpu argument package. */
209typedef RTTIMERLINUXSTARTONCPUARGS *PRTTIMERLINUXSTARTONCPUARGS;
210
211
212/**
213 * Sets the state.
214 */
215DECLINLINE(void) rtTimerLnxSetState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState)
216{
217 ASMAtomicWriteU32((uint32_t volatile *)penmState, enmNewState);
218}
219
220
221/**
222 * Sets the state if it has a certain value.
223 *
224 * @return true if xchg was done.
225 * @return false if xchg wasn't done.
226 */
227DECLINLINE(bool) rtTimerLnxCmpXchgState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState, RTTIMERLNXSTATE enmCurState)
228{
229 return ASMAtomicCmpXchgU32((uint32_t volatile *)penmState, enmNewState, enmCurState);
230}
231
232
233/**
234 * Gets the state.
235 */
236DECLINLINE(RTTIMERLNXSTATE) rtTimerLnxGetState(RTTIMERLNXSTATE volatile *penmState)
237{
238 return (RTTIMERLNXSTATE)ASMAtomicUoReadU32((uint32_t volatile *)penmState);
239}
240
241#ifdef RTTIMER_LINUX_WITH_HRTIMER
242
243/**
244 * Converts a nano second time stamp to ktime_t.
245 *
246 * ASSUMES RTTimeNanoTS() is implemented using ktime_get_ts().
247 *
248 * @returns ktime_t.
249 * @param cNanoSecs Nanoseconds.
250 */
251DECLINLINE(ktime_t) rtTimerLnxNanoToKt(uint64_t cNanoSecs)
252{
253 /* With some luck the compiler optimizes the division out of this... (Bet it doesn't.) */
254 return ktime_set(cNanoSecs / 1000000000, cNanoSecs % 1000000000);
255}
256
257/**
258 * Converts ktime_t to a nano second time stamp.
259 *
260 * ASSUMES RTTimeNanoTS() is implemented using ktime_get_ts().
261 *
262 * @returns nano second time stamp.
263 * @param Kt ktime_t.
264 */
265DECLINLINE(uint64_t) rtTimerLnxKtToNano(ktime_t Kt)
266{
267 return ktime_to_ns(Kt);
268}
269
270#endif /* RTTIMER_LINUX_WITH_HRTIMER */
271
272#ifndef RTTIMER_LINUX_ONLY_HRTIMER
273/**
274 * Converts a nano second interval to jiffies.
275 *
276 * @returns Jiffies.
277 * @param cNanoSecs Nanoseconds.
278 */
279DECLINLINE(unsigned long) rtTimerLnxNanoToJiffies(uint64_t cNanoSecs)
280{
281 /* this can be made even better... */
282 if (cNanoSecs > (uint64_t)TICK_NSEC * MAX_JIFFY_OFFSET)
283 return MAX_JIFFY_OFFSET;
284# if ARCH_BITS == 32
285 if (RT_LIKELY(cNanoSecs <= UINT32_MAX))
286 return ((uint32_t)cNanoSecs + (TICK_NSEC-1)) / TICK_NSEC;
287# endif
288 return (cNanoSecs + (TICK_NSEC-1)) / TICK_NSEC;
289}
290#endif /* !RTTIMER_LINUX_ONLY_HRTIMER */
291
292
293/**
294 * Starts a sub-timer (RTTimerStart).
295 *
296 * @param pSubTimer The sub-timer to start.
297 * @param u64Now The current timestamp (RTTimeNanoTS()).
298 * @param u64First The interval from u64Now to the first time the timer should fire.
299 * @param fPinned true = timer pinned to a specific CPU,
300 * false = timer can migrate between CPUs
301 * @param fHighRes Whether the user requested a high resolution timer or not.
302 */
303static void rtTimerLnxStartSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, uint64_t u64Now, uint64_t u64First,
304 bool fPinned, bool fHighRes)
305{
306 /*
307 * Calc when it should start firing.
308 */
309 uint64_t u64NextTS = u64Now + u64First;
310#ifndef RTTIMER_LINUX_ONLY_HRTIMER
311 if (fHighRes)
312 {
313 pSubTimer->u.Std.u64StartTS = u64NextTS;
314 pSubTimer->u.Std.u64NextTS = u64NextTS;
315 }
316#endif
317
318 pSubTimer->iTick = 0;
319
320#ifdef RTTIMER_LINUX_WITH_HRTIMER
321# ifndef RTTIMER_LINUX_ONLY_HRTIMER
322 if (fHighRes)
323# endif
324 hrtimer_start(&pSubTimer->u.Hr.LnxTimer, rtTimerLnxNanoToKt(u64NextTS),
325 fPinned ? HRTIMER_MODE_ABS_PINNED : HRTIMER_MODE_ABS);
326# ifndef RTTIMER_LINUX_ONLY_HRTIMER
327 else
328# endif
329#endif
330#ifndef RTTIMER_LINUX_ONLY_HRTIMER
331 {
332 unsigned long cJiffies = !u64First ? 0 : rtTimerLnxNanoToJiffies(u64First);
333 pSubTimer->u.Std.ulNextJiffies = jiffies + cJiffies;
334# ifdef CONFIG_SMP
335 if (fPinned)
336 mod_timer_pinned(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
337 else
338# endif
339 mod_timer(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
340 }
341#endif
342
343 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE);
344}
345
346
347/**
348 * Stops a sub-timer (RTTimerStart and rtTimerLinuxMpEvent()).
349 *
350 * @param pSubTimer The sub-timer.
351 */
352static void rtTimerLnxStopSubTimer(PRTTIMERLNXSUBTIMER pSubTimer)
353{
354#ifdef RTTIMER_LINUX_WITH_HRTIMER
355# ifndef RTTIMER_LINUX_ONLY_HRTIMER
356 if (pSubTimer->pParent->fHighRes)
357# endif
358 hrtimer_cancel(&pSubTimer->u.Hr.LnxTimer);
359# ifndef RTTIMER_LINUX_ONLY_HRTIMER
360 else
361# endif
362#endif
363# ifndef RTTIMER_LINUX_ONLY_HRTIMER
364 {
365 if (timer_pending(&pSubTimer->u.Std.LnxTimer))
366 del_timer_sync(&pSubTimer->u.Std.LnxTimer);
367 }
368#endif
369
370 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED);
371}
372
373
374#ifdef RTTIMER_LINUX_WITH_HRTIMER
375/**
376 * Timer callback function for high resolution timers.
377 *
378 * @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a
379 * one-shot or interval timer.
380 * @param pHrTimer Pointer to the sub-timer structure.
381 */
382static enum hrtimer_restart rtTimerLinuxHrCallback(struct hrtimer *pHrTimer)
383{
384 PRTTIMERLNXSUBTIMER pSubTimer = RT_FROM_MEMBER(pHrTimer, RTTIMERLNXSUBTIMER, u.Hr.LnxTimer);
385 PRTTIMER pTimer = pSubTimer->pParent;
386 enum hrtimer_restart rc;
387
388 /*
389 * Don't call the handler if the timer has been suspended.
390 * Also, when running on all CPUS, make sure we don't call out twice
391 * on a CPU because of timer migration.
392 *
393 * For the specific cpu case, we're just ignoring timer migration for now... (bad)
394 */
395 if ( ASMAtomicUoReadBool(&pTimer->fSuspended)
396#ifdef CONFIG_SMP
397 || ( pTimer->fAllCpus
398 && (RTCPUID)(pSubTimer - &pTimer->aSubTimers[0]) != RTMpCpuId())
399#endif
400 )
401 {
402 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_ACTIVE);
403 rc = HRTIMER_NORESTART;
404 }
405 else if (!pTimer->u64NanoInterval)
406 {
407 /*
408 * One shot timer, stop it before dispatching it.
409 */
410 if (pTimer->cCpus == 1)
411 ASMAtomicWriteBool(&pTimer->fSuspended, true);
412 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_ACTIVE);
413 rc = HRTIMER_NORESTART;
414 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
415 }
416 else
417 {
418 /*
419 * Run the timer. Update the timer after calling the method.
420 */
421 const uint64_t iTick = ++pSubTimer->iTick;
422 pTimer->pfnTimer(pTimer, pTimer->pvUser, iTick);
423
424 /** @todo Check reference counting wrt. RTTimerDestroy from the
425 * callback? */
426 if ( pSubTimer->enmState == RTTIMERLNXSTATE_ACTIVE
427 || pSubTimer->enmState == RTTIMERLNXSTATE_MP_STARTING)
428 {
429 hrtimer_add_expires_ns(&pSubTimer->u.Hr.LnxTimer, ASMAtomicReadU64(&pTimer->u64NanoInterval));
430 rc = HRTIMER_RESTART;
431 }
432 else
433 rc = HRTIMER_NORESTART;
434 }
435
436 return rc;
437}
438#endif /* RTTIMER_LINUX_ONLY_HRTIMER */
439
440
441#ifndef RTTIMER_LINUX_ONLY_HRTIMER
442/**
443 * Timer callback function for standard timers.
444 *
445 * @param ulUser Address of the sub-timer structure.
446 */
447static void rtTimerLinuxStdCallback(unsigned long ulUser)
448{
449 PRTTIMERLNXSUBTIMER pSubTimer = (PRTTIMERLNXSUBTIMER)ulUser;
450 PRTTIMER pTimer = pSubTimer->pParent;
451
452 /*
453 * Don't call the handler if the timer has been suspended.
454 * Also, when running on all CPUS, make sure we don't call out twice
455 * on a CPU because of timer migration.
456 *
457 * For the specific cpu case, we're just ignoring timer migration for now... (bad)
458 */
459 if ( ASMAtomicUoReadBool(&pTimer->fSuspended)
460#ifdef CONFIG_SMP
461 || ( pTimer->fAllCpus
462 && (RTCPUID)(pSubTimer - &pTimer->aSubTimers[0]) != RTMpCpuId())
463#endif
464 )
465 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_ACTIVE);
466 else if (!pTimer->u64NanoInterval)
467 {
468 /*
469 * One shot timer, stop it before dispatching it.
470 */
471 if (pTimer->cCpus == 1)
472 ASMAtomicWriteBool(&pTimer->fSuspended, true);
473 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_ACTIVE);
474 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
475 }
476 else
477 {
478 /*
479 * Interval timer, calculate the next timeout and re-arm it.
480 *
481 * The first time around, we'll re-adjust the u.Std.u64StartTS to
482 * try prevent some jittering if we were started at a bad time.
483 * This may of course backfire with highres timers...
484 *
485 * Note! u64NanoInterval won't change for standard timers.
486 */
487 const uint64_t iTick = ++pSubTimer->iTick;
488 const uint64_t u64NanoInterval = pTimer->u64NanoInterval;
489 const uint64_t u64NanoTS = RTTimeNanoTS();
490
491 if (RT_UNLIKELY(iTick == 1))
492 {
493 pSubTimer->u.Std.u64StartTS = u64NanoTS;
494 pSubTimer->u.Std.u64NextTS = u64NanoTS;
495 pSubTimer->u.Std.ulNextJiffies = jiffies;
496 }
497
498 pSubTimer->u.Std.u64NextTS += u64NanoInterval;
499 if (pTimer->cJiffies)
500 {
501 pSubTimer->u.Std.ulNextJiffies += pTimer->cJiffies;
502 /* Prevent overflows when the jiffies counter wraps around.
503 * Special thanks to Ken Preslan for helping debugging! */
504 while (time_before(pSubTimer->u.Std.ulNextJiffies, jiffies))
505 {
506 pSubTimer->u.Std.ulNextJiffies += pTimer->cJiffies;
507 pSubTimer->u.Std.u64NextTS += u64NanoInterval;
508 }
509 }
510 else
511 {
512 while (pSubTimer->u.Std.u64NextTS < u64NanoTS)
513 pSubTimer->u.Std.u64NextTS += u64NanoInterval;
514 pSubTimer->u.Std.ulNextJiffies = jiffies + rtTimerLnxNanoToJiffies(pSubTimer->u.Std.u64NextTS - u64NanoTS);
515 }
516
517# ifdef CONFIG_SMP
518 if (pTimer->fSpecificCpu || pTimer->fAllCpus)
519 mod_timer_pinned(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
520 else
521# endif
522 mod_timer(&pSubTimer->u.Std.LnxTimer, pSubTimer->u.Std.ulNextJiffies);
523
524 /*
525 * Run the timer.
526 */
527 pTimer->pfnTimer(pTimer, pTimer->pvUser, iTick);
528 }
529}
530#endif /* !RTTIMER_LINUX_ONLY_HRTIMER */
531
532
533
534#ifdef CONFIG_SMP
535
536/**
537 * Per-cpu callback function (RTMpOnAll/RTMpOnSpecific).
538 *
539 * @param idCpu The current CPU.
540 * @param pvUser1 Pointer to the timer.
541 * @param pvUser2 Pointer to the argument structure.
542 */
543static DECLCALLBACK(void) rtTimerLnxStartAllOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
544{
545 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
546 PRTTIMER pTimer = (PRTTIMER)pvUser1;
547 Assert(idCpu < pTimer->cCpus);
548 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[idCpu], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
549}
550
551
552/**
553 * Worker for RTTimerStart() that takes care of the ugly bits.
554 *
555 * @returns RTTimerStart() return value.
556 * @param pTimer The timer.
557 * @param pArgs The argument structure.
558 */
559static int rtTimerLnxStartAll(PRTTIMER pTimer, PRTTIMERLINUXSTARTONCPUARGS pArgs)
560{
561 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
562 RTCPUID iCpu;
563 RTCPUSET OnlineSet;
564 RTCPUSET OnlineSet2;
565 int rc2;
566
567 /*
568 * Prepare all the sub-timers for the startup and then flag the timer
569 * as a whole as non-suspended, make sure we get them all before
570 * clearing fSuspended as the MP handler will be waiting on this
571 * should something happen while we're looping.
572 */
573 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
574
575 do
576 {
577 RTMpGetOnlineSet(&OnlineSet);
578 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
579 {
580 Assert(pTimer->aSubTimers[iCpu].enmState != RTTIMERLNXSTATE_MP_STOPPING);
581 rtTimerLnxSetState(&pTimer->aSubTimers[iCpu].enmState,
582 RTCpuSetIsMember(&OnlineSet, iCpu)
583 ? RTTIMERLNXSTATE_STARTING
584 : RTTIMERLNXSTATE_STOPPED);
585 }
586 } while (!RTCpuSetIsEqual(&OnlineSet, RTMpGetOnlineSet(&OnlineSet2)));
587
588 ASMAtomicWriteBool(&pTimer->fSuspended, false);
589
590 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
591
592 /*
593 * Start them (can't find any exported function that allows me to
594 * do this without the cross calls).
595 */
596 pArgs->u64Now = RTTimeNanoTS();
597 rc2 = RTMpOnAll(rtTimerLnxStartAllOnCpu, pTimer, pArgs);
598 AssertRC(rc2); /* screw this if it fails. */
599
600 /*
601 * Reset the sub-timers who didn't start up (ALL CPUs case).
602 * CPUs that comes online between the
603 */
604 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
605
606 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
607 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_STARTING))
608 {
609 /** @todo very odd case for a rainy day. Cpus that temporarily went offline while
610 * we were between calls needs to nudged as the MP handler will ignore events for
611 * them because of the STARTING state. This is an extremely unlikely case - not that
612 * that means anything in my experience... ;-) */
613 }
614
615 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
616
617 return VINF_SUCCESS;
618}
619
620
621/**
622 * Worker for RTTimerStop() that takes care of the ugly SMP bits.
623 *
624 * @returns RTTimerStop() return value.
625 * @param pTimer The timer (valid).
626 */
627static int rtTimerLnxStopAll(PRTTIMER pTimer)
628{
629 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
630 RTCPUID iCpu;
631
632
633 /*
634 * Mark the timer as suspended and flag all timers as stopping, except
635 * for those being stopped by an MP event.
636 */
637 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
638
639 ASMAtomicWriteBool(&pTimer->fSuspended, true);
640 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
641 {
642 RTTIMERLNXSTATE enmState;
643 do
644 {
645 enmState = rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState);
646 if ( enmState == RTTIMERLNXSTATE_STOPPED
647 || enmState == RTTIMERLNXSTATE_MP_STOPPING)
648 break;
649 Assert(enmState == RTTIMERLNXSTATE_ACTIVE);
650 } while (!rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPING, enmState));
651 }
652
653 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
654
655 /*
656 * Do the actual stopping. Fortunately, this doesn't require any IPIs.
657 * Unfortunately it cannot be done synchronously from within the spinlock,
658 * because we might end up in an active waiting for a handler to complete.
659 */
660 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
661 if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) == RTTIMERLNXSTATE_STOPPING)
662 rtTimerLnxStopSubTimer(&pTimer->aSubTimers[iCpu]);
663
664 return VINF_SUCCESS;
665}
666
667
668/**
669 * Per-cpu callback function (RTMpOnSpecific) used by rtTimerLinuxMpEvent()
670 * to start a sub-timer on a cpu that just have come online.
671 *
672 * @param idCpu The current CPU.
673 * @param pvUser1 Pointer to the timer.
674 * @param pvUser2 Pointer to the argument structure.
675 */
676static DECLCALLBACK(void) rtTimerLinuxMpStartOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
677{
678 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
679 PRTTIMER pTimer = (PRTTIMER)pvUser1;
680 RTSPINLOCK hSpinlock;
681 Assert(idCpu < pTimer->cCpus);
682
683 /*
684 * We have to be kind of careful here as we might be racing RTTimerStop
685 * (and/or RTTimerDestroy, thus the paranoia.
686 */
687 hSpinlock = pTimer->hSpinlock;
688 if ( hSpinlock != NIL_RTSPINLOCK
689 && pTimer->u32Magic == RTTIMER_MAGIC)
690 {
691 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
692 RTSpinlockAcquire(hSpinlock, &Tmp);
693
694 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
695 && pTimer->u32Magic == RTTIMER_MAGIC)
696 {
697 /* We're sane and the timer is not suspended yet. */
698 PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
699 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
700 rtTimerLnxStartSubTimer(pSubTimer, pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
701 }
702
703 RTSpinlockRelease(hSpinlock, &Tmp);
704 }
705}
706
707
708/**
709 * MP event notification callback.
710 *
711 * @param enmEvent The event.
712 * @param idCpu The cpu it applies to.
713 * @param pvUser The timer.
714 */
715static DECLCALLBACK(void) rtTimerLinuxMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser)
716{
717 PRTTIMER pTimer = (PRTTIMER)pvUser;
718 PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
719 RTSPINLOCK hSpinlock;
720 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
721
722 Assert(idCpu < pTimer->cCpus);
723
724 /*
725 * Some initial paranoia.
726 */
727 if (pTimer->u32Magic != RTTIMER_MAGIC)
728 return;
729 hSpinlock = pTimer->hSpinlock;
730 if (hSpinlock == NIL_RTSPINLOCK)
731 return;
732
733 RTSpinlockAcquire(hSpinlock, &Tmp);
734
735 /* Is it active? */
736 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
737 && pTimer->u32Magic == RTTIMER_MAGIC)
738 {
739 switch (enmEvent)
740 {
741 /*
742 * Try do it without leaving the spin lock, but if we have to, retake it
743 * when we're on the right cpu.
744 */
745 case RTMPEVENT_ONLINE:
746 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
747 {
748 RTTIMERLINUXSTARTONCPUARGS Args;
749 Args.u64Now = RTTimeNanoTS();
750 Args.u64First = 0;
751
752 if (RTMpCpuId() == idCpu)
753 rtTimerLnxStartSubTimer(pSubTimer, Args.u64Now, Args.u64First, true /*fPinned*/, pTimer->fHighRes);
754 else
755 {
756 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED); /* we'll recheck it. */
757 RTSpinlockRelease(hSpinlock, &Tmp);
758
759 RTMpOnSpecific(idCpu, rtTimerLinuxMpStartOnCpu, pTimer, &Args);
760 return; /* we've left the spinlock */
761 }
762 }
763 break;
764
765 /*
766 * The CPU is (going) offline, make sure the sub-timer is stopped.
767 *
768 * Linux will migrate it to a different CPU, but we don't want this. The
769 * timer function is checking for this.
770 */
771 case RTMPEVENT_OFFLINE:
772 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STOPPING, RTTIMERLNXSTATE_ACTIVE))
773 {
774 RTSpinlockRelease(hSpinlock, &Tmp);
775
776 rtTimerLnxStopSubTimer(pSubTimer);
777 return; /* we've left the spinlock */
778 }
779 break;
780 }
781 }
782
783 RTSpinlockRelease(hSpinlock, &Tmp);
784}
785
786#endif /* CONFIG_SMP */
787
788
789/**
790 * Callback function use by RTTimerStart via RTMpOnSpecific to start
791 * a timer running on a specific CPU.
792 *
793 * @param idCpu The current CPU.
794 * @param pvUser1 Pointer to the timer.
795 * @param pvUser2 Pointer to the argument structure.
796 */
797static DECLCALLBACK(void) rtTimerLnxStartOnSpecificCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
798{
799 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
800 PRTTIMER pTimer = (PRTTIMER)pvUser1;
801 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
802}
803
804
805RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
806{
807 RTTIMERLINUXSTARTONCPUARGS Args;
808 int rc2;
809
810 /*
811 * Validate.
812 */
813 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
814 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
815
816 if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
817 return VERR_TIMER_ACTIVE;
818
819 Args.u64First = u64First;
820#ifdef CONFIG_SMP
821 /*
822 * Omnit timer?
823 */
824 if (pTimer->fAllCpus)
825 return rtTimerLnxStartAll(pTimer, &Args);
826#endif
827
828 /*
829 * Simple timer - Pretty straight forward.
830 */
831 Args.u64Now = RTTimeNanoTS();
832 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STARTING);
833 ASMAtomicWriteBool(&pTimer->fSuspended, false);
834 if (!pTimer->fSpecificCpu)
835 {
836 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], Args.u64Now, Args.u64First, false /*fPinned*/, pTimer->fHighRes);
837 }
838 else
839 {
840 rc2 = RTMpOnSpecific(pTimer->idCpu, rtTimerLnxStartOnSpecificCpu, pTimer, &Args);
841 if (RT_FAILURE(rc2))
842 {
843 /* Suspend it, the cpu id is probably invalid or offline. */
844 ASMAtomicWriteBool(&pTimer->fSuspended, true);
845 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPED);
846 return rc2;
847 }
848 }
849
850 return VINF_SUCCESS;
851}
852RT_EXPORT_SYMBOL(RTTimerStart);
853
854
855RTDECL(int) RTTimerStop(PRTTIMER pTimer)
856{
857
858 /*
859 * Validate.
860 */
861 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
862 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
863
864 if (ASMAtomicUoReadBool(&pTimer->fSuspended))
865 return VERR_TIMER_SUSPENDED;
866
867#ifdef CONFIG_SMP
868 /*
869 * Omni timer?
870 */
871 if (pTimer->fAllCpus)
872 return rtTimerLnxStopAll(pTimer);
873#endif
874
875 /*
876 * Simple timer.
877 */
878 ASMAtomicWriteBool(&pTimer->fSuspended, true);
879 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPING);
880 rtTimerLnxStopSubTimer(&pTimer->aSubTimers[0]);
881
882 return VINF_SUCCESS;
883}
884RT_EXPORT_SYMBOL(RTTimerStop);
885
886
887RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
888{
889 int rc = VINF_SUCCESS;
890
891 /*
892 * Validate.
893 */
894 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
895 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
896 AssertReturn(u64NanoInterval, VERR_INVALID_PARAMETER);
897
898 /*
899 * Make the change.
900 */
901#ifdef RTTIMER_LINUX_WITH_HRTIMER
902# ifndef RTTIMER_LINUX_ONLY_HRTIMER
903 if (pTimer->fHighRes)
904# endif
905 ASMAtomicWriteU64(&pTimer->u64NanoInterval, u64NanoInterval);
906# ifndef RTTIMER_LINUX_ONLY_HRTIMER
907 else
908# endif
909#endif
910#ifndef RTTIMER_LINUX_ONLY_HRTIMER
911 rc = VERR_NOT_SUPPORTED; /** @todo Implement this if needed. */
912#endif
913
914 return rc;
915}
916RT_EXPORT_SYMBOL(RTTimerChangeInterval);
917
918
919RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
920{
921 RTSPINLOCK hSpinlock;
922
923 /* It's ok to pass NULL pointer. */
924 if (pTimer == /*NIL_RTTIMER*/ NULL)
925 return VINF_SUCCESS;
926 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
927 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
928
929 /*
930 * Remove the MP notifications first because it'll reduce the risk of
931 * us overtaking any MP event that might theoretically be racing us here.
932 */
933 hSpinlock = pTimer->hSpinlock;
934#ifdef CONFIG_SMP
935 if ( pTimer->cCpus > 1
936 && hSpinlock != NIL_RTSPINLOCK)
937 {
938 int rc = RTMpNotificationDeregister(rtTimerLinuxMpEvent, pTimer);
939 AssertRC(rc);
940 }
941#endif /* CONFIG_SMP */
942
943 /*
944 * Stop the timer if it's running.
945 */
946 if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
947 RTTimerStop(pTimer);
948
949 /*
950 * Uninitialize the structure and free the associated resources.
951 * The spinlock goes last.
952 */
953 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
954 RTMemFree(pTimer);
955 if (hSpinlock != NIL_RTSPINLOCK)
956 RTSpinlockDestroy(hSpinlock);
957
958 return VINF_SUCCESS;
959}
960RT_EXPORT_SYMBOL(RTTimerDestroy);
961
962
963RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
964{
965 PRTTIMER pTimer;
966 RTCPUID iCpu;
967 unsigned cCpus;
968
969 *ppTimer = NULL;
970
971 /*
972 * Validate flags.
973 */
974 if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
975 return VERR_INVALID_PARAMETER;
976 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
977 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
978 && !RTMpIsCpuPossible(RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK)))
979 return VERR_CPU_NOT_FOUND;
980
981 /*
982 * Allocate the timer handler.
983 */
984 cCpus = 1;
985#ifdef CONFIG_SMP
986 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
987 {
988 cCpus = RTMpGetMaxCpuId() + 1;
989 Assert(cCpus <= RTCPUSET_MAX_CPUS); /* On linux we have a 1:1 relationship between cpuid and set index. */
990 AssertReturn(u64NanoInterval, VERR_NOT_IMPLEMENTED); /* We don't implement single shot on all cpus, sorry. */
991 }
992#endif
993
994 pTimer = (PRTTIMER)RTMemAllocZ(RT_OFFSETOF(RTTIMER, aSubTimers[cCpus]));
995 if (!pTimer)
996 return VERR_NO_MEMORY;
997
998 /*
999 * Initialize it.
1000 */
1001 pTimer->u32Magic = RTTIMER_MAGIC;
1002 pTimer->hSpinlock = NIL_RTSPINLOCK;
1003 pTimer->fSuspended = true;
1004 pTimer->fHighRes = !!(fFlags & RTTIMER_FLAGS_HIGH_RES);
1005#ifdef CONFIG_SMP
1006 pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
1007 pTimer->fAllCpus = (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL;
1008 pTimer->idCpu = pTimer->fSpecificCpu ? RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK) : NIL_RTCPUID;
1009#else
1010 pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
1011 pTimer->idCpu = RTMpCpuId();
1012#endif
1013 pTimer->cCpus = cCpus;
1014 pTimer->pfnTimer = pfnTimer;
1015 pTimer->pvUser = pvUser;
1016 pTimer->u64NanoInterval = u64NanoInterval;
1017#ifndef RTTIMER_LINUX_ONLY_HRTIMER
1018 pTimer->cJiffies = u64NanoInterval / RTTimerGetSystemGranularity();
1019 if (pTimer->cJiffies * RTTimerGetSystemGranularity() != u64NanoInterval)
1020 pTimer->cJiffies = 0;
1021#endif
1022
1023 for (iCpu = 0; iCpu < cCpus; iCpu++)
1024 {
1025#ifdef RTTIMER_LINUX_WITH_HRTIMER
1026# ifndef RTTIMER_LINUX_ONLY_HRTIMER
1027 if (pTimer->fHighRes)
1028# endif
1029 {
1030 hrtimer_init(&pTimer->aSubTimers[iCpu].u.Hr.LnxTimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1031 pTimer->aSubTimers[iCpu].u.Hr.LnxTimer.function = rtTimerLinuxHrCallback;
1032 }
1033# ifndef RTTIMER_LINUX_ONLY_HRTIMER
1034 else
1035# endif
1036#endif
1037#ifndef RTTIMER_LINUX_ONLY_HRTIMER
1038 {
1039 init_timer(&pTimer->aSubTimers[iCpu].u.Std.LnxTimer);
1040 pTimer->aSubTimers[iCpu].u.Std.LnxTimer.data = (unsigned long)&pTimer->aSubTimers[iCpu];
1041 pTimer->aSubTimers[iCpu].u.Std.LnxTimer.function = rtTimerLinuxStdCallback;
1042 pTimer->aSubTimers[iCpu].u.Std.LnxTimer.expires = jiffies;
1043 pTimer->aSubTimers[iCpu].u.Std.u64StartTS = 0;
1044 pTimer->aSubTimers[iCpu].u.Std.u64NextTS = 0;
1045 }
1046#endif
1047 pTimer->aSubTimers[iCpu].iTick = 0;
1048 pTimer->aSubTimers[iCpu].pParent = pTimer;
1049 pTimer->aSubTimers[iCpu].enmState = RTTIMERLNXSTATE_STOPPED;
1050 }
1051
1052#ifdef CONFIG_SMP
1053 /*
1054 * If this is running on ALL cpus, we'll have to register a callback
1055 * for MP events (so timers can be started/stopped on cpus going
1056 * online/offline). We also create the spinlock for syncrhonizing
1057 * stop/start/mp-event.
1058 */
1059 if (cCpus > 1)
1060 {
1061 int rc = RTSpinlockCreate(&pTimer->hSpinlock);
1062 if (RT_SUCCESS(rc))
1063 rc = RTMpNotificationRegister(rtTimerLinuxMpEvent, pTimer);
1064 else
1065 pTimer->hSpinlock = NIL_RTSPINLOCK;
1066 if (RT_FAILURE(rc))
1067 {
1068 RTTimerDestroy(pTimer);
1069 return rc;
1070 }
1071 }
1072#endif /* CONFIG_SMP */
1073
1074 *ppTimer = pTimer;
1075 return VINF_SUCCESS;
1076}
1077RT_EXPORT_SYMBOL(RTTimerCreateEx);
1078
1079
1080RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
1081{
1082#ifdef RTTIMER_LINUX_ONLY_HRTIMER
1083 /** @todo Not sure if this is what we want or not... Add new API for
1084 * querying the resolution of the high res timers? */
1085 struct timespec Ts;
1086 int rc = hrtimer_get_res(CLOCK_MONOTONIC, &Ts);
1087 if (!rc)
1088 {
1089 Assert(!Ts.tv_sec);
1090 return Ts.tv_nsec;
1091 }
1092#endif
1093 return 1000000000 / HZ; /* ns */
1094}
1095RT_EXPORT_SYMBOL(RTTimerGetSystemGranularity);
1096
1097
1098RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
1099{
1100 return VERR_NOT_SUPPORTED;
1101}
1102RT_EXPORT_SYMBOL(RTTimerRequestSystemGranularity);
1103
1104
1105RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
1106{
1107 return VERR_NOT_SUPPORTED;
1108}
1109RT_EXPORT_SYMBOL(RTTimerReleaseSystemGranularity);
1110
1111
1112RTDECL(bool) RTTimerCanDoHighResolution(void)
1113{
1114#ifdef RTTIMER_LINUX_WITH_HRTIMER
1115 return true;
1116#else
1117 return false;
1118#endif
1119}
1120RT_EXPORT_SYMBOL(RTTimerCanDoHighResolution);
1121
Note: See TracBrowser for help on using the repository browser.

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