VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/timer-r0drv-freebsd.c@ 5197

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

Some freebsd fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/* $Id: timer-r0drv-freebsd.c 4542 2007-09-05 20:07:47Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Memory Allocation, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
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 */
54typedef 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 the 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 uint8_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*******************************************************************************/
88static void rtTimerFreeBSDCallback(void *pvTimer);
89
90
91
92RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
93{
94 *ppTimer = NULL;
95
96 /*
97 * Validate flags.
98 */
99 if (!RTTIMER_FLAGS_IS_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_INVALID_PARAMETER;
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 */
134DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
135{
136 AssertReturn(VALID_PTR(pTimer), false);
137 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
138 return true;
139}
140
141
142RTDECL(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
160RTDECL(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
169 /*
170 * Calc when it should start fireing.
171 */
172 u64First += RTTimeNanoTS();
173
174 pTimer->fSuspended = false;
175 pTimer->iTick = 0;
176 pTimer->u64StartTS = u64First;
177 pTimer->u64NextTS = u64First;
178
179 tv.tv_sec = u64First / 1000000000;
180 tv.tv_usec = (u64First % 1000000000) / 1000;
181 callout_reset(&pTimer->Callout, tvtohz(&tv), rtTimerFreeBSDCallback, pTimer);
182
183 return VINF_SUCCESS;
184}
185
186
187RTDECL(int) RTTimerStop(PRTTIMER pTimer)
188{
189 if (!rtTimerIsValid(pTimer))
190 return VERR_INVALID_HANDLE;
191 if (pTimer->fSuspended)
192 return VERR_TIMER_SUSPENDED;
193
194 /*
195 * Suspend the timer.
196 */
197 pTimer->fSuspended = true;
198 callout_stop(&pTimer->Callout);
199
200 return VINF_SUCCESS;
201}
202
203
204/**
205 * smp_rendezvous action callback.
206 *
207 * This will perform the timer callback if we're on the right CPU.
208 *
209 * @param pvTimer The timer.
210 */
211static void rtTimerFreeBSDIpiAction(void *pvTimer)
212{
213 PRTTIMER pTimer = (PRTTIMER)pvTimer;
214 if ( pTimer->iCpu == RTTIMER_FLAGS_CPU_MASK
215 || (u_int)pTimer->iCpu == curcpu)
216 pTimer->pfnTimer(pTimer, pTimer->pvUser);
217}
218
219
220static void rtTimerFreeBSDCallback(void *pvTimer)
221{
222 PRTTIMER pTimer = (PRTTIMER)pvTimer;
223
224 /* calculate and set the next timeout */
225 if (!pTimer->u64NanoInterval)
226 {
227 pTimer->fSuspended = true;
228 callout_stop(&pTimer->Callout);
229 }
230 else
231 {
232 struct timeval tv;
233 const uint64_t u64NanoTS = RTTimeNanoTS();
234 pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
235 if (pTimer->u64NextTS < u64NanoTS)
236 pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
237
238 tv.tv_sec = pTimer->u64NextTS / 1000000000;
239 tv.tv_usec = (pTimer->u64NextTS % 1000000000) / 1000;
240 callout_reset(&pTimer->Callout, tvtohz(&tv), rtTimerFreeBSDCallback, pTimer);
241 }
242
243 /* callback */
244 if ( !pTimer->fSpecificCpu
245 || pTimer->iCpu == curcpu)
246 pTimer->pfnTimer(pTimer, pTimer->pvUser);
247 else
248 smp_rendezvous(NULL, rtTimerFreeBSDIpiAction, NULL, pvTimer);
249}
250
251
252RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
253{
254 return 1000000000 / hz; /* ns */
255}
256
257
258RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
259{
260 return VERR_NOT_SUPPORTED;
261}
262
263
264RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
265{
266 return VERR_NOT_SUPPORTED;
267}
268
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