VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/timer-r0drv-solaris.c@ 54184

Last change on this file since 54184 was 54184, checked in by vboxsync, 10 years ago

uint8_t isn't enough for a cpu index any more.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.4 KB
Line 
1/* $Id: timer-r0drv-solaris.c 54184 2015-02-12 20:58:24Z vboxsync $ */
2/** @file
3 * IPRT - Timer, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2014 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-solaris-kernel.h"
32#include "internal/iprt.h"
33#include <iprt/timer.h>
34
35#include <iprt/asm.h>
36#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
37# include <iprt/asm-amd64-x86.h>
38#endif
39#include <iprt/assert.h>
40#include <iprt/err.h>
41#include <iprt/mem.h>
42#include <iprt/mp.h>
43#include <iprt/spinlock.h>
44#include <iprt/time.h>
45#include <iprt/thread.h>
46#include "internal/magics.h"
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52/**
53 * The internal representation of a Solaris timer handle.
54 */
55typedef struct RTTIMER
56{
57 /** Magic.
58 * This is RTTIMER_MAGIC, but changes to something else before the timer
59 * is destroyed to indicate clearly that thread should exit. */
60 uint32_t volatile u32Magic;
61 /** Reference counter. */
62 uint32_t volatile cRefs;
63 /** Flag indicating that the timer is suspended. */
64 uint8_t volatile fSuspended;
65 /** Whether the timer must run on all CPUs or not. */
66 uint8_t fAllCpus;
67 /** Whether the timer must run on a specific CPU or not. */
68 uint8_t fSpecificCpu;
69 /** The CPU it must run on if fSpecificCpu is set. */
70 uint32_t iCpu;
71 /** The nano second interval for repeating timers. */
72 uint64_t cNsInterval;
73 /** Cyclic timer Id. */
74 cyclic_id_t hCyclicId;
75 /** The user callback. */
76 PFNRTTIMER pfnTimer;
77 /** The argument for the user callback. */
78 void *pvUser;
79 /** Union with timer type specific data. */
80 union
81 {
82 /** Single timer (fAllCpus == false). */
83 struct
84 {
85 /** Cyclic handler. */
86 cyc_handler_t hHandler;
87 /** Cyclic time and interval representation. */
88 cyc_time_t hFireTime;
89 /** Timer ticks. */
90 uint64_t u64Tick;
91 } Single;
92
93 /** Omni timer (fAllCpus == true). */
94 struct
95 {
96 /** Absolute timestamp of when the timer should fire next. */
97 uint64_t u64When;
98 /** Array of timer ticks per CPU. Reinitialized when a CPU is online'd
99 * (variable size). */
100 uint64_t au64Ticks[1];
101 } Omni;
102 } u;
103} RTTIMER;
104
105
106/*******************************************************************************
107* Defined Constants And Macros *
108*******************************************************************************/
109/** Validates that the timer is valid. */
110#define RTTIMER_ASSERT_VALID_RET(pTimer) \
111 do \
112 { \
113 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE); \
114 AssertMsgReturn((pTimer)->u32Magic == RTTIMER_MAGIC, ("pTimer=%p u32Magic=%x expected %x\n", (pTimer), (pTimer)->u32Magic, RTTIMER_MAGIC), \
115 VERR_INVALID_HANDLE); \
116 } while (0)
117
118
119
120/**
121 * Retains a reference to the timer.
122 *
123 * @returns New reference counter value.
124 * @param pTimer The timer.
125 */
126DECLINLINE(uint32_t) rtTimerSolRetain(PRTTIMER pTimer)
127{
128 return ASMAtomicIncU32(&pTimer->cRefs);
129}
130
131
132/**
133 * Destroys the timer when the reference counter has reached zero.
134 *
135 * @returns 0 (new references counter value).
136 * @param pTimer The timer.
137 */
138static uint32_t rtTimeSolReleaseCleanup(PRTTIMER pTimer)
139{
140 Assert(pTimer->hCyclicId == CYCLIC_NONE);
141 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
142 RTMemFree(pTimer);
143}
144
145
146/**
147 * Releases a reference to the timer.
148 *
149 * @returns New reference counter value.
150 * @param pTimer The timer.
151 */
152DECLINLINE(uint32_t) rtTimerSolRelease(PRTTIMER pTimer)
153{
154 uint32_t cRefs = ASMAtomicDecU32(&pTimer->cRefs);
155 if (!cRefs)
156 return rtTimeSolReleaseCleanup(pTimer);
157 return cRefs;
158}
159
160
161/**
162 * RTMpOnSpecific callback used by rtTimerSolCallbackWrapper() to deal with
163 * callouts on the wrong CPU (race with cyclic_bind).
164 *
165 * @param idCpu The CPU this is fired on.
166 * @param pvUser1 Opaque pointer to the timer.
167 * @param pvUser2 Not used, NULL.
168 */
169static void rtTimerSolMpCallbackWrapper(RTCPUID idCpu, void *pvUser1, void *pvUser2)
170{
171 PRTTIMER pTimer = (PRTTIMER)pvUser1;
172 AssertPtrReturnVoid(pTimer);
173 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
174 Assert(pTimer->iCpu == RTMpCpuId()); /* ASSUMES: index == cpuid */
175 Assert(!pTimer->fAllCpus);
176 NOREF(pvUser2);
177
178 /* Make sure one-shots do not fire another time. */
179 Assert( !pTimer->fSuspended
180 || pTimer->cNsInterval != 0);
181
182 /* For one-shot specific timers, allow RTTimer to restart them. */
183 if (pTimer->cNsInterval == 0)
184 pTimer->fSuspended = true;
185
186 uint64_t u64Tick = ++pTimer->u.Single.u64Tick;
187 pTimer->pfnTimer(pTimer, pTimer->pvUser, u64Tick);
188}
189
190
191/**
192 * Callback wrapper for single-CPU timers.
193 *
194 * @param pvArg Opaque pointer to the timer.
195 *
196 * @remarks This will be executed in interrupt context but only at the specified
197 * level i.e. CY_LOCK_LEVEL in our case. We -CANNOT- call into the
198 * cyclic subsystem here, neither should pfnTimer().
199 */
200static void rtTimerSolSingleCallbackWrapper(void *pvArg)
201{
202 PRTTIMER pTimer = (PRTTIMER)pvArg;
203 AssertPtrReturnVoid(pTimer);
204 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
205 Assert(!pTimer->fAllCpus);
206
207 /* Make sure one-shots do not fire another time. */
208 Assert( !pTimer->fSuspended
209 || pTimer->cNsInterval != 0);
210
211 /* For specific timers, we might fire on the wrong CPU between cyclic_add() and cyclic_bind().
212 Redirect these shots to the right CPU as we are temporarily rebinding to the right CPU. */
213 if ( pTimer->fSpecificCpu
214 && pTimer->iCpu != RTMpCpuId()) /* ASSUMES: index == cpuid */
215 {
216 RTMpOnSpecific(pTimer->iCpu, rtTimerSolMpCallbackWrapper, pTimer, NULL);
217 return;
218 }
219
220 /* For one-shot any-cpu timers, allow RTTimer to restart them. */
221 if (pTimer->cNsInterval == 0)
222 pTimer->fSuspended = true;
223
224 uint64_t u64Tick = ++pTimer->u.Single.u64Tick;
225 pTimer->pfnTimer(pTimer, pTimer->pvUser, u64Tick);
226}
227
228
229/**
230 * Callback wrapper for Omni-CPU timers.
231 *
232 * @param pvArg Opaque pointer to the timer.
233 *
234 * @remarks This will be executed in interrupt context but only at the specified
235 * level i.e. CY_LOCK_LEVEL in our case. We -CANNOT- call into the
236 * cyclic subsystem here, neither should pfnTimer().
237 */
238static void rtTimerSolOmniCallbackWrapper(void *pvArg)
239{
240 PRTTIMER pTimer = (PRTTIMER)pvArg;
241 AssertPtrReturnVoid(pTimer);
242 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
243 Assert(pTimer->fAllCpus);
244
245 uint64_t u64Tick = ++pTimer->u.Omni.au64Ticks[CPU->cpu_id];
246 pTimer->pfnTimer(pTimer, pTimer->pvUser, u64Tick);
247}
248
249
250/**
251 * Omni-CPU cyclic online event. This is called before the omni cycle begins to
252 * fire on the specified CPU.
253 *
254 * @param pvArg Opaque pointer to the timer.
255 * @param pCpu Pointer to the CPU on which it will fire.
256 * @param pCyclicHandler Pointer to a cyclic handler to add to the CPU
257 * specified in @a pCpu.
258 * @param pCyclicTime Pointer to the cyclic time and interval object.
259 *
260 * @remarks We -CANNOT- call back into the cyclic subsystem here, we can however
261 * block (sleep).
262 */
263static void rtTimerSolOmniCpuOnline(void *pvArg, cpu_t *pCpu, cyc_handler_t *pCyclicHandler, cyc_time_t *pCyclicTime)
264{
265 PRTTIMER pTimer = (PRTTIMER)pvArg;
266 AssertPtrReturnVoid(pTimer);
267 AssertPtrReturnVoid(pCpu);
268 AssertPtrReturnVoid(pCyclicHandler);
269 AssertPtrReturnVoid(pCyclicTime);
270
271 pTimer->u.Omni.au64Ticks[pCpu->cpu_id] = 0;
272 pCyclicHandler->cyh_func = (cyc_func_t)rtTimerSolOmniCallbackWrapper;
273 pCyclicHandler->cyh_arg = pTimer;
274 pCyclicHandler->cyh_level = CY_LOCK_LEVEL;
275
276 uint64_t u64Now = RTTimeSystemNanoTS();
277 if (pTimer->u.Omni.u64When < u64Now)
278 pCyclicTime->cyt_when = u64Now + pTimer->cNsInterval / 2;
279 else
280 pCyclicTime->cyt_when = pTimer->u.Omni.u64When;
281
282 pCyclicTime->cyt_interval = pTimer->cNsInterval;
283}
284
285
286RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
287{
288 RT_ASSERT_PREEMPTIBLE();
289 *ppTimer = NULL;
290
291 /*
292 * Validate flags.
293 */
294 if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
295 return VERR_INVALID_PARAMETER;
296
297 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
298 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
299 && !RTMpIsCpuPossible(RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK)))
300 return VERR_CPU_NOT_FOUND;
301
302 /* One-shot omni timers are not supported by the cyclic system. */
303 if ( (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL
304 && u64NanoInterval == 0)
305 return VERR_NOT_SUPPORTED;
306
307 /*
308 * Allocate and initialize the timer handle. The omni variant has a
309 * variable sized array of ticks counts, thus the size calculation.
310 */
311 PRTTIMER pTimer = (PRTTIMER)RTMemAllocZ( (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL
312 ? RT_OFFSETOF(RTTIMER, u.Omni.au64Ticks[RTMpGetCount()])
313 : sizeof(RTTIMER));
314 if (!pTimer)
315 return VERR_NO_MEMORY;
316
317 pTimer->u32Magic = RTTIMER_MAGIC;
318 pTimer->cRefs = 1;
319 pTimer->fSuspended = true;
320 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
321 {
322 pTimer->fAllCpus = true;
323 pTimer->fSpecificCpu = false;
324 pTimer->iCpu = UINT32_MAX;
325 }
326 else if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
327 {
328 pTimer->fAllCpus = false;
329 pTimer->fSpecificCpu = true;
330 pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK; /* ASSUMES: index == cpuid */
331 }
332 else
333 {
334 pTimer->fAllCpus = false;
335 pTimer->fSpecificCpu = false;
336 pTimer->iCpu = UINT32_MAX;
337 }
338 pTimer->cNsInterval = u64NanoInterval;
339 pTimer->pfnTimer = pfnTimer;
340 pTimer->pvUser = pvUser;
341 pTimer->hCyclicId = CYCLIC_NONE;
342
343 *ppTimer = pTimer;
344 return VINF_SUCCESS;
345}
346
347
348RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
349{
350 if (pTimer == NULL)
351 return VINF_SUCCESS;
352 RTTIMER_ASSERT_VALID_RET(pTimer);
353 RT_ASSERT_INTS_ON();
354
355 /*
356 * Free the associated resources.
357 */
358 RTTimerStop(pTimer);
359 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
360
361 rtTimerSolRelease(pTimer);
362 return VINF_SUCCESS;
363}
364
365
366RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
367{
368 RTTIMER_ASSERT_VALID_RET(pTimer);
369 RT_ASSERT_INTS_ON();
370
371 if (!pTimer->fSuspended)
372 return VERR_TIMER_ACTIVE;
373
374 pTimer->fSuspended = false;
375 if (pTimer->fAllCpus)
376 {
377 /*
378 * Setup omni (all CPU) timer. The Omni-CPU online event will fire
379 * and from there we setup periodic timers per CPU.
380 */
381 pTimer->u.Omni.u64When = pTimer->cNsInterval + RTTimeSystemNanoTS();
382
383 cyc_omni_handler_t HandlerOmni;
384 HandlerOmni.cyo_online = rtTimerSolOmniCpuOnline;
385 HandlerOmni.cyo_offline = NULL;
386 HandlerOmni.cyo_arg = pTimer;
387
388 mutex_enter(&cpu_lock);
389 pTimer->hCyclicId = cyclic_add_omni(&HandlerOmni);
390 mutex_exit(&cpu_lock);
391 }
392 else
393 {
394 if (pTimer->fSpecificCpu && !RTMpIsCpuOnline(pTimer->iCpu)) /* ASSUMES: index == cpuid */
395 return VERR_CPU_OFFLINE;
396
397 pTimer->u.Single.hHandler.cyh_func = (cyc_func_t)rtTimerSolSingleCallbackWrapper;
398 pTimer->u.Single.hHandler.cyh_arg = pTimer;
399 pTimer->u.Single.hHandler.cyh_level = CY_LOCK_LEVEL;
400
401 mutex_enter(&cpu_lock);
402 if (RT_UNLIKELY( pTimer->fSpecificCpu
403 && !cpu_is_online(cpu[pTimer->iCpu])))
404 {
405 mutex_exit(&cpu_lock);
406 return VERR_CPU_OFFLINE;
407 }
408
409 pTimer->u.Single.hFireTime.cyt_when = u64First + RTTimeSystemNanoTS();
410 if (pTimer->cNsInterval == 0)
411 {
412 /*
413 * cylic_add() comment: "The caller is responsible for assuring that cyt_when + cyt_interval <= INT64_MAX"
414 * but it contradicts itself because cyclic_reprogram() updates only the interval and accepts CY_INFINITY as
415 * a valid, special value. See cyclic_fire().
416 */
417 pTimer->u.Single.hFireTime.cyt_interval = CY_INFINITY;
418 }
419 else
420 pTimer->u.Single.hFireTime.cyt_interval = pTimer->cNsInterval;
421
422 pTimer->hCyclicId = cyclic_add(&pTimer->u.Single.hHandler, &pTimer->u.Single.hFireTime);
423 if (pTimer->fSpecificCpu)
424 cyclic_bind(pTimer->hCyclicId, cpu[pTimer->iCpu], NULL /* cpupart */);
425
426 mutex_exit(&cpu_lock);
427 }
428
429 return VINF_SUCCESS;
430}
431
432
433RTDECL(int) RTTimerStop(PRTTIMER pTimer)
434{
435 RTTIMER_ASSERT_VALID_RET(pTimer);
436 RT_ASSERT_INTS_ON();
437
438 if (pTimer->fSuspended)
439 return VERR_TIMER_SUSPENDED;
440
441 /** @remarks Do -not- call this function from a timer callback,
442 * cyclic_remove() will deadlock the system. */
443 mutex_enter(&cpu_lock);
444
445 pTimer->fSuspended = true;
446 cyclic_remove(pTimer->hCyclicId);
447 pTimer->hCyclicId = CYCLIC_NONE;
448
449 mutex_exit(&cpu_lock);
450
451 return VINF_SUCCESS;
452}
453
454
455RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
456{
457 /*
458 * Validate.
459 */
460 RTTIMER_ASSERT_VALID_RET(pTimer);
461 AssertReturn(u64NanoInterval, VERR_INVALID_PARAMETER);
462
463 if (pTimer->fSuspended)
464 {
465 pTimer->cNsInterval = u64NanoInterval;
466 return VINF_SUCCESS;
467 }
468
469 return VERR_NOT_SUPPORTED;
470}
471
472
473RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
474{
475 return nsec_per_tick;
476}
477
478
479RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
480{
481 return VERR_NOT_SUPPORTED;
482}
483
484
485RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
486{
487 return VERR_NOT_SUPPORTED;
488}
489
490
491RTDECL(bool) RTTimerCanDoHighResolution(void)
492{
493 /** @todo return true; - when missing bits have been implemented and tested*/
494 return false;
495}
496
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