VirtualBox

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

Last change on this file since 9372 was 9372, checked in by vboxsync, 17 years ago

fixed assertion, wonder if that helps the rhel3 gcc SIGSEGV.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.5 KB
Line 
1/* $Id: timer-r0drv-linux.c 9372 2008-06-04 01:10:06Z vboxsync $ */
2/** @file
3 * IPRT - Timers, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include "the-linux-kernel.h"
35
36#include <iprt/timer.h>
37#include <iprt/time.h>
38#include <iprt/mp.h>
39#include <iprt/cpuset.h>
40#include <iprt/spinlock.h>
41#include <iprt/err.h>
42#include <iprt/asm.h>
43#include <iprt/assert.h>
44#include <iprt/alloc.h>
45
46#include "internal/magics.h"
47
48#if !defined(RT_USE_LINUX_HRTIMER) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) /* ?? */
49# define RT_USE_LINUX_HRTIMER
50#endif
51
52/* This check must match the ktime usage in rtTimeGetSystemNanoTS() / time-r0drv-linux.c. */
53#if defined(RT_USE_LINUX_HRTIMER) \
54 && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
55# error "RT_USE_LINUX_HRTIMER requires 2.6.16 or later, sorry."
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 /** Transient state; next STOPPED. */
79 RTTIMERLNXSTATE_STOPPING,
80 /** Transient state; next STOPPED. */
81 RTTIMERLNXSTATE_MP_STOPPING,
82 /** The usual 32-bit hack. */
83 RTTIMERLNXSTATE_32BIT_HACK = 0x7fffffff
84} RTTIMERLNXSTATE;
85
86
87/**
88 * A Linux sub-timer.
89 */
90typedef struct RTTIMERLNXSUBTIMER
91{
92 /** The linux timer structure. */
93#ifdef RT_USE_LINUX_HRTIMER
94 struct hrtimer LnxTimer;
95#else
96 struct timer_list LnxTimer;
97#endif
98 /** The start of the current run (ns).
99 * This is used to calculate when the timer ought to fire the next time. */
100 uint64_t u64StartTS;
101 /** The start of the current run (ns).
102 * This is used to calculate when the timer ought to fire the next time. */
103 uint64_t u64NextTS;
104 /** The current tick number (since u64StartTS). */
105 uint64_t iTick;
106 /** Pointer to the parent timer. */
107 PRTTIMER pParent;
108 /** The current sub-timer state. */
109 RTTIMERLNXSTATE volatile enmState;
110} RTTIMERLNXSUBTIMER;
111/** Pointer to a linux sub-timer. */
112typedef RTTIMERLNXSUBTIMER *PRTTIMERLNXSUBTIMER;
113AssertCompileMemberOffset(RTTIMERLNXSUBTIMER, LnxTimer, 0);
114
115
116/**
117 * The internal representation of an Linux timer handle.
118 */
119typedef struct RTTIMER
120{
121 /** Magic.
122 * This is RTTIMER_MAGIC, but changes to something else before the timer
123 * is destroyed to indicate clearly that thread should exit. */
124 uint32_t volatile u32Magic;
125 /** Spinlock synchronizing the fSuspended and MP event handling.
126 * This is NIL_RTSPINLOCK if cCpus == 1. */
127 RTSPINLOCK hSpinlock;
128 /** Flag indicating the the timer is suspended. */
129 bool volatile fSuspended;
130 /** Whether the timer must run on one specific CPU or not. */
131 bool fSpecificCpu;
132#ifdef CONFIG_SMP
133 /** Whether the timer must run on all CPUs or not. */
134 bool fAllCpus;
135#endif /* else: All -> specific on non-SMP kernels */
136 /** The CPU it must run on if fSpecificCpu is set. */
137 RTCPUID idCpu;
138 /** The number of CPUs this timer should run on. */
139 RTCPUID cCpus;
140 /** Callback. */
141 PFNRTTIMER pfnTimer;
142 /** User argument. */
143 void *pvUser;
144 /** The timer interval. 0 if one-shot. */
145 uint64_t u64NanoInterval;
146 /** Sub-timers.
147 * Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
148 * an entry for all possible cpus. In that case the index will be the same as
149 * for the RTCpuSet. */
150 RTTIMERLNXSUBTIMER aSubTimers[1];
151} RTTIMER;
152
153
154/**
155 * A rtTimerLinuxStartOnCpu and rtTimerLinuxStartOnCpu argument package.
156 */
157typedef struct RTTIMERLINUXSTARTONCPUARGS
158{
159 /** The current time (RTTimeNanoTS). */
160 uint64_t u64Now;
161 /** When to start firing (delta). */
162 uint64_t u64First;
163} RTTIMERLINUXSTARTONCPUARGS;
164/** Pointer to a rtTimerLinuxStartOnCpu argument package. */
165typedef RTTIMERLINUXSTARTONCPUARGS *PRTTIMERLINUXSTARTONCPUARGS;
166
167
168/**
169 * Sets the state.
170 */
171DECLINLINE(void) rtTimerLnxSetState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState)
172{
173 ASMAtomicWriteU32((uint32_t volatile *)penmState, enmNewState);
174}
175
176
177/**
178 * Sets the state if it has a certain value.
179 */
180DECLINLINE(bool) rtTimerLnxCmpXchgState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState, RTTIMERLNXSTATE enmCurState)
181{
182 return ASMAtomicCmpXchgU32((uint32_t volatile *)penmState, enmNewState, enmCurState);
183}
184
185
186/**
187 * Gets the state.
188 */
189DECLINLINE(RTTIMERLNXSTATE) rtTimerLnxGetState(RTTIMERLNXSTATE volatile *penmState)
190{
191 return (RTTIMERLNXSTATE)ASMAtomicUoReadU32((uint32_t volatile *)penmState);
192}
193
194
195/**
196 * Starts a sub-timer (RTTimerStart).
197 *
198 * @param pSubTimer The sub-timer to start.
199 * @param u64Now The current timestamp (RTTimeNanoTS()).
200 * @param u64First The interval from u64Now to the first time the timer should fire.
201 */
202static void rtTimerLnxStartSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, uint64_t u64Now, uint64_t u64First)
203{
204 /*
205 * Calc when it should start firing.
206 */
207 uint64_t u64NextTS = u64Now + u64First;
208 pSubTimer->u64StartTS = u64Now;
209 pSubTimer->u64NextTS = u64NextTS;
210
211#ifdef RT_USE_LINUX_HRTIMER
212 {
213 /* ASSUMES RTTimeNanoTS() is implemented using ktime_get_ts(). */
214 struct timespec Ts;
215 ktime_t Kt;
216 Ts.tv_sec = u64NextTS / 1000000000;
217 Ts.tv_nsec = u64NextTS % 1000000000;
218 Kt = timespec_to_ktime(Ts);
219 hrtimer_start(&pSubTimer->LnxTimer, Kt, HRTIMER_MODE_ABS);
220 }
221#else
222 {
223 uint64_t cJiffies = !u64First ? 0 : u64First / TICK_NSEC;
224 if (cJiffies > MAX_JIFFY_OFFSET)
225 cJiffies = MAX_JIFFY_OFFSET;
226 mod_timer(&pSubTimer->LnxTimer, jiffies + cJiffies);
227 }
228#endif
229
230 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE);
231}
232
233
234/**
235 * Stops a sub-timer (RTTimerStart and rtTimerLinuxMpEvent()).
236 *
237 * @param pSubTimer The sub-timer.
238 */
239static void rtTimerLnxStopSubTimer(PRTTIMERLNXSUBTIMER pSubTimer)
240{
241#ifdef RT_USE_LINUX_HRTIMER
242 hrtimer_cancel(&pSubTimer->LnxTimer);
243#else
244 if (timer_pending(&pSubTimer->LnxTimer))
245 del_timer_sync(&pSubTimer->LnxTimer);
246#endif
247
248 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED);
249}
250
251
252#ifdef RT_USE_LINUX_HRTIMER
253/**
254 * Timer callback function.
255 * @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a one-shot or interval timer.
256 * @param pHrTimer Pointer to the sub-timer structure.
257 */
258static enum hrtimer_restart rtTimerLinuxCallback(struct hrtimer *pHrTimer)
259#else
260/**
261 * Timer callback function.
262 * @param ulUser Address of the sub-timer structure.
263 */
264static void rtTimerLinuxCallback(unsigned long ulUser)
265#endif
266{
267#ifdef RT_USE_LINUX_HRTIMER
268 enum hrtimer_restart rc;
269 PRTTIMERLNXSUBTIMER pSubTimer = (PRTTIMERLNXSUBTIMER)pHrTimer;
270#else
271 PRTTIMERLNXSUBTIMER pSubTimer = (PRTTIMERLNXSUBTIMER)ulUser;
272#endif
273 PRTTIMER pTimer = pSubTimer->pParent;
274
275 /*
276 * Don't call the handler if the timer has been suspended.
277 * Also, when running on all CPUS, make sure we don't call out twice
278 * on a CPU because of timer migration.
279 *
280 * For the specific cpu case, we're just ignoring timer migration for now... (bad)
281 */
282 if ( pTimer->fSuspended
283#ifdef CONFIG_SMP
284 || ( pTimer->fAllCpus
285 && (pSubTimer - &pTimer->aSubTimers[0]) != RTMpCpuId())
286#endif
287 )
288 {
289 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_ACTIVE);
290# ifdef RT_USE_LINUX_HRTIMER
291 rc = HRTIMER_NORESTART;
292# endif
293 }
294 else if (!pTimer->u64NanoInterval)
295 {
296 /*
297 * One shot timer, stop it before dispatching it.
298 */
299 if (pTimer->cCpus == 1)
300 ASMAtomicWriteBool(&pTimer->fSuspended, true);
301 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_ACTIVE);
302#ifdef RT_USE_LINUX_HRTIMER
303 rc = HRTIMER_NORESTART;
304#else
305 /* detached before we're called, nothing to do for this case. */
306#endif
307
308 pTimer->pfnTimer(pTimer, pTimer->pvUser);
309 }
310 else
311 {
312 /*
313 * Interval timer, calculate the next timeout and re-arm it.
314 *
315 * The first time around, we'll re-adjust the u64StartTS to
316 * try prevent some jittering if we were started at a bad time.
317 * This may of course backfire with highres timers...
318 */
319 const uint64_t u64NanoTS = RTTimeNanoTS();
320#if 0 /* this needs to be tested before it's enabled. */
321 if ( pSubTimer->iTick == 0
322 && pTimer->u64NanoInterval < u64NanoTS)
323 pSubTimer->u64StartTS = pSubTimer->u64FirstTS = u64NanoTS - 1000; /* ugly */
324#endif
325 pSubTimer->iTick++;
326 pSubTimer->u64NextTS = pSubTimer->u64StartTS
327 + pSubTimer->iTick * pTimer->u64NanoInterval;
328 if (pSubTimer->u64NextTS < u64NanoTS)
329 pSubTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
330
331#ifdef RT_USE_LINUX_HRTIMER
332 {
333 /* ASSUMES RTTimeNanoTS() is implemented using ktime_get_ts(). */
334 struct timespec Ts;
335 Ts.tv_sec = pSubTimer->u64NextTS / 1000000000;
336 Ts.tv_nsec = pSubTimer->u64NextTS % 1000000000;
337 pSubTimer->LnxTimer.expires = timespec_to_ktime(Ts);
338 rc = HRTIMER_RESTART;
339 }
340#else
341 {
342 uint64_t offDelta = pSubTimer->u64NextTS - u64NanoTS;
343 uint64_t cJiffies = offDelta / TICK_NSEC; /* (We end up doing 64-bit div here which ever way we go.) */
344 if (cJiffies > MAX_JIFFY_OFFSET)
345 cJiffies = MAX_JIFFY_OFFSET;
346 else if (cJiffies == 0)
347 cJiffies = 1;
348 mod_timer(&pSubTimer->LnxTimer, jiffies + cJiffies);
349 }
350#endif
351
352 /*
353 * Run the timer.
354 */
355 pTimer->pfnTimer(pTimer, pTimer->pvUser);
356 }
357
358#ifdef RT_USE_LINUX_HRTIMER
359 return rc;
360#endif
361}
362
363
364#ifdef CONFIG_SMP
365
366/**
367 * Per-cpu callback function (RTMpOnAll/RTMpOnSpecific).
368 *
369 * @param idCpu The current CPU.
370 * @param pvUser1 Pointer to the timer.
371 * @param pvUser2 Pointer to the argument structure.
372 */
373static DECLCALLBACK(void) rtTimerLnxStartAllOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
374{
375 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
376 PRTTIMER pTimer = (PRTTIMER)pvUser1;
377 Assert(idCpu < pTimer->cCpus);
378 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[idCpu], pArgs->u64Now, pArgs->u64First);
379}
380
381
382/**
383 * Worker for RTTimerStart() that takes care of the ugly bit.s
384 *
385 * @returns RTTimerStart() return value.
386 * @param pTimer The timer.
387 * @param pArgs The argument structure.
388 */
389static int rtTimerLnxStartAll(PRTTIMER pTimer, PRTTIMERLINUXSTARTONCPUARGS pArgs)
390{
391 RTSPINLOCKTMP Tmp;
392 RTCPUID iCpu;
393 RTCPUSET OnlineSet;
394 RTCPUSET OnlineSet2;
395 int rc2;
396
397 /*
398 * Prepare all the sub-timers for the startup and then flag the timer
399 * as a whole as non-suspended, make sure we get them all before
400 * clearing fSuspended as the MP handler will be waiting on this
401 * should something happen while we're looping.
402 */
403 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
404
405 do
406 {
407 RTMpGetOnlineSet(&OnlineSet);
408 for (iCpu = 0; iCpu <= pTimer->cCpus; iCpu++)
409 {
410 Assert(pTimer->aSubTimers[iCpu].enmState != RTTIMERLNXSTATE_MP_STOPPING);
411 rtTimerLnxSetState(&pTimer->aSubTimers[iCpu].enmState,
412 RTCpuSetIsMember(&OnlineSet, iCpu)
413 ? RTTIMERLNXSTATE_STARTING
414 : RTTIMERLNXSTATE_STOPPED);
415 }
416 } while (!RTCpuSetIsEqual(&OnlineSet, RTMpGetOnlineSet(&OnlineSet2)));
417
418 ASMAtomicWriteBool(&pTimer->fSuspended, false);
419
420 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
421
422 /*
423 * Start them (can't find any exported function that allows me to
424 * do this without the cross calls).
425 */
426 pArgs->u64Now = RTTimeNanoTS();
427 rc2 = RTMpOnAll(rtTimerLnxStartAllOnCpu, pTimer, pArgs);
428 AssertRC(rc2); /* screw this if it fails. */
429
430 /*
431 * Reset the sub-timers who didn't start up (ALL CPUs case).
432 * CPUs that comes online between the
433 */
434 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
435
436 for (iCpu = 0; iCpu <= pTimer->cCpus; iCpu++)
437 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_STARTING))
438 {
439 /** @todo very odd case for a rainy day. Cpus that temporarily went offline while
440 * we were between calls needs to nudged as the MP handler will ignore events for
441 * them because of the STARTING state. This is an extremely unlikely case - not that
442 * that means anything in my experience... ;-) */
443 }
444
445 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
446
447 return VINF_SUCCESS;
448}
449
450
451/**
452 * Worker for RTTimerStop() that takes care of the ugly SMP bits.
453 *
454 * @returns RTTimerStop() return value.
455 * @param pTimer The timer (valid).
456 */
457static int rtTimerLnxStopAll(PRTTIMER pTimer)
458{
459 RTCPUID iCpu;
460 RTSPINLOCKTMP Tmp;
461
462
463 /*
464 * Mark the timer as suspended and flag all timers as stopping, except
465 * for those being stopped by an MP event.
466 */
467 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
468
469 ASMAtomicWriteBool(&pTimer->fSuspended, true);
470 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
471 {
472 RTTIMERLNXSTATE enmState;
473 do
474 {
475 enmState = rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState);
476 if ( enmState == RTTIMERLNXSTATE_STOPPED
477 || enmState == RTTIMERLNXSTATE_MP_STOPPING)
478 break;
479 Assert(enmState == RTTIMERLNXSTATE_ACTIVE);
480 } while (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPING, enmState));
481 }
482
483 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
484
485 /*
486 * Do the actual stopping. Fortunately, this doesn't require any IPIs.
487 * Unfortunately it cannot be done synchronously from within the spinlock,
488 * because we might end up in an active waiting for a handler to complete.
489 */
490 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
491 if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) == RTTIMERLNXSTATE_STOPPING)
492 rtTimerLnxStopSubTimer(&pTimer->aSubTimers[iCpu]);
493
494 return VINF_SUCCESS;
495}
496
497
498/**
499 * Per-cpu callback function (RTMpOnSpecific) used by rtTimerLinuxMpEvent()
500 * to start a sub-timer on a cpu that just have come online.
501 *
502 * @param idCpu The current CPU.
503 * @param pvUser1 Pointer to the timer.
504 * @param pvUser2 Pointer to the argument structure.
505 */
506static DECLCALLBACK(void) rtTimerLinuxMpStartOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
507{
508 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
509 PRTTIMER pTimer = (PRTTIMER)pvUser1;
510 RTSPINLOCK hSpinlock;
511 Assert(idCpu < pTimer->cCpus);
512
513 /*
514 * We have to be kind of careful here as we might RTTimerStop (and RTTimerDestroy),
515 * thus the paranoia.
516 */
517 hSpinlock = pTimer->hSpinlock;
518 if ( hSpinlock != NIL_RTSPINLOCK
519 && pTimer->u32Magic == RTTIMER_MAGIC)
520 {
521 RTSPINLOCKTMP Tmp;
522 RTSpinlockAcquire(hSpinlock, &Tmp);
523
524 if ( !pTimer->fSuspended
525 && pTimer->u32Magic == RTTIMER_MAGIC)
526 {
527 /* We're sane and the timer is not suspended yet. */
528 PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
529 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
530 rtTimerLnxStartSubTimer(pSubTimer, pArgs->u64Now, pArgs->u64First);
531 }
532
533 RTSpinlockRelease(hSpinlock, &Tmp);
534 }
535}
536
537
538/**
539 * MP event notification callback.
540 *
541 * @param enmEvent The event.
542 * @param idCpu The cpu it applies to.
543 * @param pvUser The timer.
544 */
545static DECLCALLBACK(void) rtTimerLinuxMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser)
546{
547 PRTTIMER pTimer = (PRTTIMER)pvUser;
548 PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
549 RTSPINLOCK hSpinlock;
550 RTSPINLOCKTMP Tmp;
551
552 Assert(idCpu < pTimer->cCpus);
553
554 /*
555 * Some initial paranoia.
556 */
557 if (pTimer->u32Magic != RTTIMER_MAGIC)
558 return;
559 hSpinlock = pTimer->hSpinlock;
560 if (hSpinlock == NIL_RTSPINLOCK)
561 return;
562
563 RTSpinlockAcquireNoInts(hSpinlock, &Tmp);
564
565 /* Is it active? */
566 if ( !pTimer->fSuspended
567 && !pTimer->u32Magic == RTTIMER_MAGIC)
568 {
569 switch (enmEvent)
570 {
571 /*
572 * Try do it without leaving the spin lock, but if we have to, retake it
573 * when we're on the right cpu.
574 */
575 case RTMPEVENT_ONLINE:
576 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
577 {
578 RTTIMERLINUXSTARTONCPUARGS Args;
579 Args.u64Now = RTTimeNanoTS();
580 Args.u64First = pTimer->u64NanoInterval;
581
582 if (RTMpCpuId() == idCpu)
583 rtTimerLnxStartSubTimer(pSubTimer, Args.u64Now, Args.u64First);
584 else
585 {
586 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED); /* we'll recheck it. */
587 RTSpinlockReleaseNoInts(hSpinlock, &Tmp);
588
589 RTMpOnSpecific(idCpu, rtTimerLinuxMpStartOnCpu, pTimer, &Args);
590 return; /* we've left the spinlock */
591 }
592 }
593 break;
594
595 /*
596 * The CPU is (going) offline, make sure the sub-timer is stopped.
597 *
598 * Linux will migrate it to a different CPU, but we don't want this. The
599 * timer function is checking for this.
600 */
601 case RTMPEVENT_OFFLINE:
602 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STOPPING, RTTIMERLNXSTATE_ACTIVE))
603 {
604 RTSpinlockAcquireNoInts(hSpinlock, &Tmp);
605
606 rtTimerLnxStopSubTimer(pSubTimer);
607 return; /* we've left the spinlock */
608 }
609 break;
610 }
611 }
612
613 RTSpinlockAcquireNoInts(hSpinlock, &Tmp);
614}
615
616#endif /* CONFIG_SMP */
617
618
619/**
620 * Callback function use by RTTimerStart via RTMpOnSpecific to start
621 * a timer running on a specific CPU.
622 *
623 * @param idCpu The current CPU.
624 * @param pvUser1 Pointer to the timer.
625 * @param pvUser2 Pointer to the argument structure.
626 */
627static DECLCALLBACK(void) rtTimerLnxStartOnSpecificCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
628{
629 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
630 PRTTIMER pTimer = (PRTTIMER)pvUser1;
631 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], pArgs->u64Now, pArgs->u64First);
632}
633
634
635RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
636{
637 RTTIMERLINUXSTARTONCPUARGS Args;
638 int rc2;
639
640 /*
641 * Validate.
642 */
643 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
644 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
645
646 if (!pTimer->fSuspended)
647 return VERR_TIMER_ACTIVE;
648
649 Args.u64First = u64First;
650#ifdef CONFIG_SMP
651 if (pTimer->fAllCpus)
652 return rtTimerLnxStartAll(pTimer, &Args);
653#endif
654
655 /*
656 * This is pretty straight forwards.
657 */
658 Args.u64Now = RTTimeNanoTS();
659 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STARTING);
660 ASMAtomicWriteBool(&pTimer->fSuspended, false);
661 if (!pTimer->fSpecificCpu)
662 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], Args.u64Now, Args.u64First);
663 else
664 {
665 rc2 = RTMpOnSpecific(pTimer->idCpu, rtTimerLnxStartOnSpecificCpu, pTimer, &Args);
666 if (RT_FAILURE(rc2))
667 {
668 /* Suspend it, the cpu id is probably invalid or offline. */
669 ASMAtomicWriteBool(&pTimer->fSuspended, true);
670 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPED);
671 return rc2;
672 }
673 }
674
675 return VINF_SUCCESS;
676}
677
678
679RTDECL(int) RTTimerStop(PRTTIMER pTimer)
680{
681
682 /*
683 * Validate.
684 */
685 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
686 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
687
688 if (pTimer->fSuspended)
689 return VERR_TIMER_SUSPENDED;
690
691#ifdef CONFIG_SMP
692 if (pTimer->fAllCpus)
693 return rtTimerLnxStopAll(pTimer);
694#endif
695
696 /*
697 * Cancel the timer.
698 */
699 ASMAtomicWriteBool(&pTimer->fSuspended, true); /* just to be on the safe side. */
700 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPING);
701 rtTimerLnxStopSubTimer(&pTimer->aSubTimers[0]);
702
703 return VINF_SUCCESS;
704}
705
706
707RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
708{
709 RTSPINLOCK hSpinlock;
710
711 /* It's ok to pass NULL pointer. */
712 if (pTimer == /*NIL_RTTIMER*/ NULL)
713 return VINF_SUCCESS;
714 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
715 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
716
717 /*
718 * Remove the MP notifications first because it'll reduce the risk of
719 * us overtaking any MP event that might theoretically be racing us here.
720 */
721 hSpinlock = pTimer->hSpinlock;
722#ifdef CONFIG_SMP
723 if ( pTimer->cCpus > 1
724 && hSpinlock != NIL_RTSPINLOCK)
725 {
726 int rc = RTMpNotificationDeregister(rtTimerLinuxMpEvent, pTimer);
727 AssertRC(rc);
728 }
729#endif /* CONFIG_SMP */
730
731 /*
732 * Stop the timer if it's running.
733 */
734 if (!ASMAtomicUoReadBool(&pTimer->fSuspended)) /* serious paranoia */
735 RTTimerStop(pTimer);
736
737 /*
738 * Uninitialize the structure and free the associated resources.
739 * The spinlock goes last.
740 */
741 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
742 RTMemFree(pTimer);
743 if (hSpinlock != NIL_RTSPINLOCK)
744 RTSpinlockDestroy(hSpinlock);
745
746 return VINF_SUCCESS;
747}
748
749
750RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
751{
752 PRTTIMER pTimer;
753 RTCPUID iCpu;
754 unsigned cCpus;
755
756 *ppTimer = NULL;
757
758 /*
759 * Validate flags.
760 */
761 if (!RTTIMER_FLAGS_IS_VALID(fFlags))
762 return VERR_INVALID_PARAMETER;
763 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
764 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
765 && !RTMpIsCpuOnline(fFlags & RTTIMER_FLAGS_CPU_MASK))
766 return (fFlags & RTTIMER_FLAGS_CPU_MASK) > RTMpGetMaxCpuId()
767 ? VERR_CPU_NOT_FOUND
768 : VERR_CPU_OFFLINE;
769
770 /*
771 * Allocate the timer handler.
772 */
773 cCpus = 1;
774#ifdef CONFIG_SMP
775 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
776 {
777 cCpus = RTMpGetMaxCpuId() + 1;
778 Assert(cCpus <= RTCPUSET_MAX_CPUS); /* On linux we have a 1:1 relationship between cpuid and set index. */
779 AssertReturn(u64NanoInterval, VERR_NOT_IMPLEMENTED); /* We don't implement single shot on all cpus, sorry. */
780 }
781#endif
782
783 pTimer = (PRTTIMER)RTMemAllocZ(RT_OFFSETOF(RTTIMER, aSubTimers[cCpus]));
784 if (!pTimer)
785 return VERR_NO_MEMORY;
786
787 /*
788 * Initialize it.
789 */
790 pTimer->u32Magic = RTTIMER_MAGIC;
791 pTimer->hSpinlock = NIL_RTSPINLOCK;
792 pTimer->fSuspended = true;
793#ifdef CONFIG_SMP
794 pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
795 pTimer->fAllCpus = (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL;
796 pTimer->idCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
797#else
798 pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
799 pTimer->idCpu = RTMpCpuId();
800#endif
801 pTimer->cCpus = cCpus;
802 pTimer->pfnTimer = pfnTimer;
803 pTimer->pvUser = pvUser;
804 pTimer->u64NanoInterval = u64NanoInterval;
805
806 for (iCpu = 0; iCpu < cCpus; iCpu++)
807 {
808#ifdef RT_USE_LINUX_HRTIMER
809 hrtimer_init(&pTimer->aSubTimers[iCpu].LnxTimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
810 pTimer->aSubTimers[iCpu].LnxTimer.function = rtTimerLinuxCallback;
811#else
812 init_timer(&pTimer->aSubTimers[iCpu].LnxTimer);
813 pTimer->aSubTimers[iCpu].LnxTimer.data = (unsigned long)pTimer;
814 pTimer->aSubTimers[iCpu].LnxTimer.function = rtTimerLinuxCallback;
815 pTimer->aSubTimers[iCpu].LnxTimer.expires = jiffies;
816#endif
817 pTimer->aSubTimers[iCpu].u64StartTS = 0;
818 pTimer->aSubTimers[iCpu].u64NextTS = 0;
819 pTimer->aSubTimers[iCpu].iTick = 0;
820 pTimer->aSubTimers[iCpu].pParent = pTimer;
821 pTimer->aSubTimers[iCpu].enmState = RTTIMERLNXSTATE_STOPPED;
822 }
823
824#ifdef CONFIG_SMP
825 /*
826 * If this is running on ALL cpus, we'll have to register a callback
827 * for MP events (so timers can be started/stopped on cpus going
828 * online/offline). We also create the spinlock for syncrhonizing
829 * stop/start/mp-event.
830 */
831 if (cCpus > 1)
832 {
833 int rc = RTSpinlockCreate(&pTimer->hSpinlock);
834 if (RT_SUCCESS(rc))
835 rc = RTMpNotificationRegister(rtTimerLinuxMpEvent, pTimer);
836 else
837 pTimer->hSpinlock = NIL_RTSPINLOCK;
838 if (RT_FAILURE(rc))
839 {
840 RTTimerDestroy(pTimer);
841 return rc;
842 }
843 }
844#endif /* CONFIG_SMP */
845
846 *ppTimer = pTimer;
847 return VINF_SUCCESS;
848}
849
850
851RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
852{
853#ifdef RT_USE_LINUX_HRTIMER
854 /** @todo later... */
855 return 1000000000 / HZ; /* ns */
856#else
857 return 1000000000 / HZ; /* ns */
858#endif
859}
860
861
862RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
863{
864 return VERR_NOT_SUPPORTED;
865}
866
867
868RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
869{
870 return VERR_NOT_SUPPORTED;
871}
872
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