1 | /* $Id: timer-r0drv-freebsd.c 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*********************************************************************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *********************************************************************************************************************************/
|
---|
35 | #include "the-freebsd-kernel.h"
|
---|
36 |
|
---|
37 | #include <iprt/timer.h>
|
---|
38 | #include <iprt/time.h>
|
---|
39 | #include <iprt/spinlock.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/assert.h>
|
---|
43 | #include <iprt/alloc.h>
|
---|
44 |
|
---|
45 | #include "internal/magics.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | /**
|
---|
52 | * The internal representation of an FreeBSD timer handle.
|
---|
53 | */
|
---|
54 | typedef struct RTTIMER
|
---|
55 | {
|
---|
56 | /** Magic.
|
---|
57 | * This is RTTIMER_MAGIC, but changes to something else before the timer
|
---|
58 | * is destroyed to indicate clearly that thread should exit. */
|
---|
59 | uint32_t volatile u32Magic;
|
---|
60 | /** Flag indicating that the timer is suspended. */
|
---|
61 | uint8_t volatile fSuspended;
|
---|
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 | uint32_t iCpu;
|
---|
66 | /** The FreeBSD callout structure. */
|
---|
67 | struct callout Callout;
|
---|
68 | /** Callback. */
|
---|
69 | PFNRTTIMER pfnTimer;
|
---|
70 | /** User argument. */
|
---|
71 | void *pvUser;
|
---|
72 | /** The timer interval. 0 if one-shot. */
|
---|
73 | uint64_t u64NanoInterval;
|
---|
74 | /** The start of the current run.
|
---|
75 | * This is used to calculate when the timer ought to fire the next time. */
|
---|
76 | uint64_t volatile u64StartTS;
|
---|
77 | /** The start of the current run.
|
---|
78 | * This is used to calculate when the timer ought to fire the next time. */
|
---|
79 | uint64_t volatile u64NextTS;
|
---|
80 | /** The current tick number (since u64StartTS). */
|
---|
81 | uint64_t volatile iTick;
|
---|
82 | } RTTIMER;
|
---|
83 |
|
---|
84 |
|
---|
85 | /*********************************************************************************************************************************
|
---|
86 | * Internal Functions *
|
---|
87 | *********************************************************************************************************************************/
|
---|
88 | static void rtTimerFreeBSDCallback(void *pvTimer);
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
|
---|
93 | {
|
---|
94 | *ppTimer = NULL;
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Validate flags.
|
---|
98 | */
|
---|
99 | if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
|
---|
100 | return VERR_INVALID_PARAMETER;
|
---|
101 | if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
102 | && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
|
---|
103 | && (fFlags & RTTIMER_FLAGS_CPU_MASK) > mp_maxid)
|
---|
104 | return VERR_CPU_NOT_FOUND;
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Allocate and initialize the timer handle.
|
---|
108 | */
|
---|
109 | PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
|
---|
110 | if (!pTimer)
|
---|
111 | return VERR_NO_MEMORY;
|
---|
112 |
|
---|
113 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
114 | pTimer->fSuspended = true;
|
---|
115 | pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
|
---|
116 | pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
|
---|
117 | pTimer->pfnTimer = pfnTimer;
|
---|
118 | pTimer->pvUser = pvUser;
|
---|
119 | pTimer->u64NanoInterval = u64NanoInterval;
|
---|
120 | pTimer->u64StartTS = 0;
|
---|
121 | callout_init(&pTimer->Callout, CALLOUT_MPSAFE);
|
---|
122 |
|
---|
123 | *ppTimer = pTimer;
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Validates the timer handle.
|
---|
130 | *
|
---|
131 | * @returns true if valid, false if invalid.
|
---|
132 | * @param pTimer The handle.
|
---|
133 | */
|
---|
134 | DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
|
---|
135 | {
|
---|
136 | AssertReturn(VALID_PTR(pTimer), false);
|
---|
137 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
143 | {
|
---|
144 | /* It's ok to pass NULL pointer. */
|
---|
145 | if (pTimer == /*NIL_RTTIMER*/ NULL)
|
---|
146 | return VINF_SUCCESS;
|
---|
147 | if (!rtTimerIsValid(pTimer))
|
---|
148 | return VERR_INVALID_HANDLE;
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Free the associated resources.
|
---|
152 | */
|
---|
153 | pTimer->u32Magic++;
|
---|
154 | callout_stop(&pTimer->Callout);
|
---|
155 | RTMemFree(pTimer);
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
161 | {
|
---|
162 | struct timeval tv;
|
---|
163 |
|
---|
164 | if (!rtTimerIsValid(pTimer))
|
---|
165 | return VERR_INVALID_HANDLE;
|
---|
166 | if (!pTimer->fSuspended)
|
---|
167 | return VERR_TIMER_ACTIVE;
|
---|
168 | if ( pTimer->fSpecificCpu
|
---|
169 | && !RTMpIsCpuOnline(pTimer->idCpu))
|
---|
170 | return VERR_CPU_OFFLINE;
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * Calc when it should start firing.
|
---|
174 | */
|
---|
175 | u64First += RTTimeNanoTS();
|
---|
176 |
|
---|
177 | pTimer->fSuspended = false;
|
---|
178 | pTimer->iTick = 0;
|
---|
179 | pTimer->u64StartTS = u64First;
|
---|
180 | pTimer->u64NextTS = u64First;
|
---|
181 |
|
---|
182 | tv.tv_sec = u64First / 1000000000;
|
---|
183 | tv.tv_usec = (u64First % 1000000000) / 1000;
|
---|
184 | callout_reset(&pTimer->Callout, tvtohz(&tv), rtTimerFreeBSDCallback, pTimer);
|
---|
185 |
|
---|
186 | return VINF_SUCCESS;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
191 | {
|
---|
192 | if (!rtTimerIsValid(pTimer))
|
---|
193 | return VERR_INVALID_HANDLE;
|
---|
194 | if (pTimer->fSuspended)
|
---|
195 | return VERR_TIMER_SUSPENDED;
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Suspend the timer.
|
---|
199 | */
|
---|
200 | pTimer->fSuspended = true;
|
---|
201 | callout_stop(&pTimer->Callout);
|
---|
202 |
|
---|
203 | return VINF_SUCCESS;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
|
---|
208 | {
|
---|
209 | if (!rtTimerIsValid(pTimer))
|
---|
210 | return VERR_INVALID_HANDLE;
|
---|
211 | return VERR_NOT_SUPPORTED;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * smp_rendezvous action callback.
|
---|
217 | *
|
---|
218 | * This will perform the timer callback if we're on the right CPU.
|
---|
219 | *
|
---|
220 | * @param pvTimer The timer.
|
---|
221 | */
|
---|
222 | static void rtTimerFreeBSDIpiAction(void *pvTimer)
|
---|
223 | {
|
---|
224 | PRTTIMER pTimer = (PRTTIMER)pvTimer;
|
---|
225 | if ( pTimer->iCpu == RTTIMER_FLAGS_CPU_MASK
|
---|
226 | || (u_int)pTimer->iCpu == curcpu)
|
---|
227 | pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | static void rtTimerFreeBSDCallback(void *pvTimer)
|
---|
232 | {
|
---|
233 | PRTTIMER pTimer = (PRTTIMER)pvTimer;
|
---|
234 |
|
---|
235 | /* calculate and set the next timeout */
|
---|
236 | pTimer->iTick++;
|
---|
237 | if (!pTimer->u64NanoInterval)
|
---|
238 | {
|
---|
239 | pTimer->fSuspended = true;
|
---|
240 | callout_stop(&pTimer->Callout);
|
---|
241 | }
|
---|
242 | else
|
---|
243 | {
|
---|
244 | struct timeval tv;
|
---|
245 | const uint64_t u64NanoTS = RTTimeNanoTS();
|
---|
246 | pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
|
---|
247 | if (pTimer->u64NextTS < u64NanoTS)
|
---|
248 | pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
|
---|
249 |
|
---|
250 | tv.tv_sec = pTimer->u64NextTS / 1000000000;
|
---|
251 | tv.tv_usec = (pTimer->u64NextTS % 1000000000) / 1000;
|
---|
252 | callout_reset(&pTimer->Callout, tvtohz(&tv), rtTimerFreeBSDCallback, pTimer);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /* callback */
|
---|
256 | if ( !pTimer->fSpecificCpu
|
---|
257 | || pTimer->iCpu == curcpu)
|
---|
258 | pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
|
---|
259 | else
|
---|
260 | smp_rendezvous(NULL, rtTimerFreeBSDIpiAction, NULL, pvTimer);
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
265 | {
|
---|
266 | return 1000000000 / hz; /* ns */
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
271 | {
|
---|
272 | return VERR_NOT_SUPPORTED;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
277 | {
|
---|
278 | return VERR_NOT_SUPPORTED;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | RTDECL(bool) RTTimerCanDoHighResolution(void)
|
---|
283 | {
|
---|
284 | return false;
|
---|
285 | }
|
---|
286 |
|
---|