1 | /** $Id: timer-r0drv-solaris.c 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Timer, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 |
|
---|
33 | #include <iprt/timer.h>
|
---|
34 | #include <iprt/time.h>
|
---|
35 | #include <iprt/spinlock.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/alloc.h>
|
---|
40 |
|
---|
41 | #include "internal/magics.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Structures and Typedefs *
|
---|
46 | *******************************************************************************/
|
---|
47 | /**
|
---|
48 | * The internal representation of a Solaris timer handle.
|
---|
49 | */
|
---|
50 | typedef struct RTTIMER
|
---|
51 | {
|
---|
52 | /** Magic.
|
---|
53 | * This is RTTIMER_MAGIC, but changes to something else before the timer
|
---|
54 | * is destroyed to indicate clearly that thread should exit. */
|
---|
55 | uint32_t volatile u32Magic;
|
---|
56 | /** Flag indicating the the timer is suspended. */
|
---|
57 | uint8_t volatile fSuspended;
|
---|
58 | /** Whether the timer must run on a specific CPU or not. */
|
---|
59 | uint8_t fSpecificCpu;
|
---|
60 | /** The CPU it must run on if fSpecificCpu is set. */
|
---|
61 | uint8_t iCpu;
|
---|
62 | /** The Solaris cyclic structure. */
|
---|
63 | cyc_handler_t CyclicInfo;
|
---|
64 | /** The Solaris cyclic handle. */
|
---|
65 | cyclic_id_t CyclicID;
|
---|
66 | /** Callback. */
|
---|
67 | PFNRTTIMER pfnTimer;
|
---|
68 | /** User argument. */
|
---|
69 | void *pvUser;
|
---|
70 | /** The timer interval. 0 for one-shot timer */
|
---|
71 | uint64_t u64NanoInterval;
|
---|
72 | } RTTIMER;
|
---|
73 |
|
---|
74 |
|
---|
75 | /*******************************************************************************
|
---|
76 | * Internal Functions *
|
---|
77 | *******************************************************************************/
|
---|
78 | static void rtTimerSolarisCallback(void *pvTimer);
|
---|
79 | static void rtTimerSolarisStop(PRTTIMER pTimer);
|
---|
80 |
|
---|
81 |
|
---|
82 | RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
|
---|
83 | {
|
---|
84 | *ppTimer = NULL;
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Validate flags.
|
---|
88 | */
|
---|
89 | if (!RTTIMER_FLAGS_IS_VALID(fFlags))
|
---|
90 | return VERR_INVALID_PARAMETER;
|
---|
91 | if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
92 | /** @todo implement && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL*/)
|
---|
93 | return VERR_NOT_SUPPORTED;
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Allocate and initialize the timer handle.
|
---|
97 | */
|
---|
98 | PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
|
---|
99 | if (!pTimer)
|
---|
100 | return VERR_NO_MEMORY;
|
---|
101 |
|
---|
102 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
103 | pTimer->fSuspended = true;
|
---|
104 | pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
|
---|
105 | pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
|
---|
106 | pTimer->CyclicInfo.cyh_func = rtTimerSolarisCallback;
|
---|
107 | pTimer->CyclicInfo.cyh_level = CY_LOCK_LEVEL;
|
---|
108 | pTimer->CyclicInfo.cyh_arg = pTimer;
|
---|
109 | pTimer->CyclicID = CYCLIC_NONE;
|
---|
110 | pTimer->u64NanoInterval = u64NanoInterval;
|
---|
111 | pTimer->pfnTimer = pfnTimer;
|
---|
112 | pTimer->pvUser = pvUser;
|
---|
113 |
|
---|
114 | *ppTimer = pTimer;
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Validates the timer handle.
|
---|
121 | *
|
---|
122 | * @returns true if valid, false if invalid.
|
---|
123 | * @param pTimer The handle.
|
---|
124 | */
|
---|
125 | DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
|
---|
126 | {
|
---|
127 | AssertReturn(VALID_PTR(pTimer), false);
|
---|
128 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
134 | {
|
---|
135 | if (pTimer == NULL)
|
---|
136 | return VINF_SUCCESS;
|
---|
137 | if (!rtTimerIsValid(pTimer))
|
---|
138 | return VERR_INVALID_HANDLE;
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * Free the associated resources.
|
---|
142 | */
|
---|
143 | pTimer->u32Magic++;
|
---|
144 | rtTimerSolarisStop(pTimer);
|
---|
145 | RTMemFree(pTimer);
|
---|
146 | return VINF_SUCCESS;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
151 | {
|
---|
152 | cyc_time_t timerSpec;
|
---|
153 |
|
---|
154 | if (!rtTimerIsValid(pTimer))
|
---|
155 | return VERR_INVALID_HANDLE;
|
---|
156 | if (!pTimer->fSuspended)
|
---|
157 | return VERR_TIMER_ACTIVE;
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Calc when it should start fireing.
|
---|
161 | */
|
---|
162 | u64First += RTTimeNanoTS();
|
---|
163 |
|
---|
164 | pTimer->fSuspended = false;
|
---|
165 | timerSpec.cyt_when = u64First;
|
---|
166 | timerSpec.cyt_interval = pTimer->u64NanoInterval == 0 ? u64First : pTimer->u64NanoInterval;
|
---|
167 |
|
---|
168 | mutex_enter(&cpu_lock);
|
---|
169 | pTimer->CyclicID = cyclic_add(&pTimer->CyclicInfo, &timerSpec);
|
---|
170 | mutex_exit(&cpu_lock);
|
---|
171 |
|
---|
172 | return VINF_SUCCESS;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
177 | {
|
---|
178 | if (!rtTimerIsValid(pTimer))
|
---|
179 | return VERR_INVALID_HANDLE;
|
---|
180 | if (pTimer->fSuspended)
|
---|
181 | return VERR_TIMER_SUSPENDED;
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Suspend the timer.
|
---|
185 | */
|
---|
186 | pTimer->fSuspended = true;
|
---|
187 | rtTimerSolarisStop(pTimer);
|
---|
188 |
|
---|
189 | return VINF_SUCCESS;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | static void rtTimerSolarisCallback(void *pvTimer)
|
---|
194 | {
|
---|
195 | PRTTIMER pTimer = (PRTTIMER)pvTimer;
|
---|
196 |
|
---|
197 | /* If this is a one shot timer, call pfnTimer and suspend
|
---|
198 | * as Solaris does not support 0 interval timers implictly
|
---|
199 | */
|
---|
200 | if (!pTimer->u64NanoInterval)
|
---|
201 | {
|
---|
202 | pTimer->fSuspended = true;
|
---|
203 | rtTimerSolarisStop(pTimer);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /* Callback user defined callback function */
|
---|
207 | pTimer->pfnTimer(pTimer, pTimer->pvUser);
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | static void rtTimerSolarisStop(PRTTIMER pTimer)
|
---|
212 | {
|
---|
213 | /* Important we check for invalid cyclic object */
|
---|
214 | if (pTimer->CyclicID != CYCLIC_NONE)
|
---|
215 | {
|
---|
216 | mutex_enter(&cpu_lock);
|
---|
217 | cyclic_remove(pTimer->CyclicID);
|
---|
218 | mutex_exit(&cpu_lock);
|
---|
219 | pTimer->CyclicID = CYCLIC_NONE;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
225 | {
|
---|
226 | return nsec_per_tick;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
231 | {
|
---|
232 | return VERR_NOT_SUPPORTED;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
237 | {
|
---|
238 | return VERR_NOT_SUPPORTED;
|
---|
239 | }
|
---|
240 |
|
---|