1 | /* $Id: timer-r0drv-solaris.c 29281 2010-05-09 23:40:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Timer, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/mp.h>
|
---|
40 | #include <iprt/spinlock.h>
|
---|
41 | #include <iprt/time.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 | #include "internal/magics.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Structures and Typedefs *
|
---|
48 | *******************************************************************************/
|
---|
49 | /**
|
---|
50 | * The internal representation of a Solaris timer handle.
|
---|
51 | */
|
---|
52 | typedef struct RTTIMER
|
---|
53 | {
|
---|
54 | /** Magic.
|
---|
55 | * This is RTTIMER_MAGIC, but changes to something else before the timer
|
---|
56 | * is destroyed to indicate clearly that thread should exit. */
|
---|
57 | uint32_t volatile u32Magic;
|
---|
58 | /** Flag indicating that the timer is suspended. */
|
---|
59 | uint8_t volatile fSuspended;
|
---|
60 | /** Run on all CPUs if set */
|
---|
61 | uint8_t fAllCpu;
|
---|
62 | /** Whether the timer must run on a specific CPU or not. */
|
---|
63 | uint8_t fSpecificCpu;
|
---|
64 | /** The CPU it must run on if fSpecificCpu is set. */
|
---|
65 | uint8_t iCpu;
|
---|
66 | /** The nano second interval for repeating timers */
|
---|
67 | uint64_t interval;
|
---|
68 | /** simple Solaris timer handle. */
|
---|
69 | vbi_stimer_t *stimer;
|
---|
70 | /** global Solaris timer handle. */
|
---|
71 | vbi_gtimer_t *gtimer;
|
---|
72 | /** The user callback. */
|
---|
73 | PFNRTTIMER pfnTimer;
|
---|
74 | /** The argument for the user callback. */
|
---|
75 | void *pvUser;
|
---|
76 | } RTTIMER;
|
---|
77 |
|
---|
78 |
|
---|
79 | /*******************************************************************************
|
---|
80 | * Defined Constants And Macros *
|
---|
81 | *******************************************************************************/
|
---|
82 | /** Validates that the timer is valid. */
|
---|
83 | #define RTTIMER_ASSERT_VALID_RET(pTimer) \
|
---|
84 | do \
|
---|
85 | { \
|
---|
86 | AssertPtrReturn(pTimer, VERR_INVALID_HANDLE); \
|
---|
87 | AssertReturn((pTimer)->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE); \
|
---|
88 | } while (0)
|
---|
89 |
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Need a wrapper to get the PRTTIMER passed through
|
---|
93 | */
|
---|
94 | static void rtTimerSolarisCallbackWrapper(PRTTIMER pTimer, uint64_t tick)
|
---|
95 | {
|
---|
96 | pTimer->pfnTimer(pTimer, pTimer->pvUser, tick);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
|
---|
103 | {
|
---|
104 | RT_ASSERT_PREEMPTIBLE();
|
---|
105 | *ppTimer = NULL;
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Validate flags.
|
---|
109 | */
|
---|
110 | if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
|
---|
111 | return VERR_INVALID_PARAMETER;
|
---|
112 | if (vbi_revision_level < 2)
|
---|
113 | return VERR_NOT_SUPPORTED;
|
---|
114 |
|
---|
115 | if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
116 | && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
|
---|
117 | && !RTMpIsCpuPossible((fFlags & RTTIMER_FLAGS_CPU_MASK)))
|
---|
118 | return VERR_CPU_NOT_FOUND;
|
---|
119 |
|
---|
120 | if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL && u64NanoInterval == 0)
|
---|
121 | return VERR_NOT_SUPPORTED;
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Allocate and initialize the timer handle.
|
---|
125 | */
|
---|
126 | PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
|
---|
127 | if (!pTimer)
|
---|
128 | return VERR_NO_MEMORY;
|
---|
129 |
|
---|
130 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
131 | pTimer->fSuspended = true;
|
---|
132 | if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
|
---|
133 | {
|
---|
134 | pTimer->fAllCpu = true;
|
---|
135 | pTimer->fSpecificCpu = false;
|
---|
136 | pTimer->iCpu = 255;
|
---|
137 | }
|
---|
138 | else if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
139 | {
|
---|
140 | pTimer->fAllCpu = false;
|
---|
141 | pTimer->fSpecificCpu = true;
|
---|
142 | pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | pTimer->fAllCpu = false;
|
---|
147 | pTimer->fSpecificCpu = false;
|
---|
148 | pTimer->iCpu = 255;
|
---|
149 | }
|
---|
150 | pTimer->interval = u64NanoInterval;
|
---|
151 | pTimer->pfnTimer = pfnTimer;
|
---|
152 | pTimer->pvUser = pvUser;
|
---|
153 | pTimer->stimer = NULL;
|
---|
154 | pTimer->gtimer = NULL;
|
---|
155 |
|
---|
156 | *ppTimer = pTimer;
|
---|
157 | return VINF_SUCCESS;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
162 | {
|
---|
163 | if (pTimer == NULL)
|
---|
164 | return VINF_SUCCESS;
|
---|
165 | RTTIMER_ASSERT_VALID_RET(pTimer);
|
---|
166 | RT_ASSERT_INTS_ON();
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Free the associated resources.
|
---|
170 | */
|
---|
171 | RTTimerStop(pTimer);
|
---|
172 | ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
|
---|
173 | RTMemFree(pTimer);
|
---|
174 | return VINF_SUCCESS;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
179 | {
|
---|
180 | RTTIMER_ASSERT_VALID_RET(pTimer);
|
---|
181 | RT_ASSERT_INTS_ON();
|
---|
182 |
|
---|
183 | if (!pTimer->fSuspended)
|
---|
184 | return VERR_TIMER_ACTIVE;
|
---|
185 |
|
---|
186 | pTimer->fSuspended = false;
|
---|
187 | if (pTimer->fAllCpu)
|
---|
188 | {
|
---|
189 | pTimer->gtimer = vbi_gtimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval);
|
---|
190 | if (pTimer->gtimer == NULL)
|
---|
191 | return VERR_INVALID_PARAMETER;
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | int iCpu = VBI_ANY_CPU;
|
---|
196 | if (pTimer->fSpecificCpu)
|
---|
197 | iCpu = pTimer->iCpu;
|
---|
198 | pTimer->stimer = vbi_stimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval, iCpu);
|
---|
199 | if (pTimer->stimer == NULL)
|
---|
200 | {
|
---|
201 | if (iCpu != VBI_ANY_CPU)
|
---|
202 | return VERR_CPU_OFFLINE;
|
---|
203 | return VERR_INVALID_PARAMETER;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | return VINF_SUCCESS;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
212 | {
|
---|
213 | RTTIMER_ASSERT_VALID_RET(pTimer);
|
---|
214 | RT_ASSERT_INTS_ON();
|
---|
215 |
|
---|
216 | if (pTimer->fSuspended)
|
---|
217 | return VERR_TIMER_SUSPENDED;
|
---|
218 |
|
---|
219 | pTimer->fSuspended = true;
|
---|
220 | if (pTimer->stimer)
|
---|
221 | {
|
---|
222 | vbi_stimer_end(pTimer->stimer);
|
---|
223 | pTimer->stimer = NULL;
|
---|
224 | }
|
---|
225 | else if (pTimer->gtimer)
|
---|
226 | {
|
---|
227 | vbi_gtimer_end(pTimer->gtimer);
|
---|
228 | pTimer->gtimer = NULL;
|
---|
229 | }
|
---|
230 |
|
---|
231 | return VINF_SUCCESS;
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 |
|
---|
236 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
237 | {
|
---|
238 | return vbi_timer_granularity();
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
243 | {
|
---|
244 | return VERR_NOT_SUPPORTED;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
249 | {
|
---|
250 | return VERR_NOT_SUPPORTED;
|
---|
251 | }
|
---|
252 |
|
---|