1 | /** $Id: timer-r0drv-solaris.c 10954 2008-07-29 19:51:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Timer, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-solaris-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 a Solaris 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 the the timer is suspended. */
|
---|
61 | uint8_t volatile fSuspended;
|
---|
62 | /** Run on all CPUs if set */
|
---|
63 | uint8_t fAllCpu;
|
---|
64 | /** Whether the timer must run on a specific CPU or not. */
|
---|
65 | uint8_t fSpecificCpu;
|
---|
66 | /** The CPU it must run on if fSpecificCpu is set. */
|
---|
67 | uint8_t iCpu;
|
---|
68 | /** The nano second interval for repeating timers */
|
---|
69 | uint64_t interval;
|
---|
70 | /** simple Solaris timer handle. */
|
---|
71 | vbi_stimer_t *stimer;
|
---|
72 | /** global Solaris timer handle. */
|
---|
73 | vbi_gtimer_t *gtimer;
|
---|
74 | /** The user callback. */
|
---|
75 | PFNRTTIMER pfnTimer;
|
---|
76 | /** The argument for the user callback. */
|
---|
77 | void *pvUser;
|
---|
78 | } RTTIMER;
|
---|
79 |
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Need a wrapper to get the PRTTIMER passed through
|
---|
83 | */
|
---|
84 | static void rtTimerSolarisCallbackWrapper(PRTTIMER pTimer, uint64_t tick)
|
---|
85 | {
|
---|
86 | pTimer->pfnTimer(pTimer, pTimer->pvUser, tick);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(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_ARE_VALID(fFlags))
|
---|
100 | return VERR_INVALID_PARAMETER;
|
---|
101 | if (vbi_revision_level < 2)
|
---|
102 | return VERR_NOT_SUPPORTED;
|
---|
103 |
|
---|
104 | if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
105 | && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
|
---|
106 | && !RTMpIsCpuPossible((fFlags & RTTIMER_FLAGS_CPU_MASK)))
|
---|
107 | return VERR_CPU_NOT_FOUND;
|
---|
108 |
|
---|
109 | if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL && u64NanoInterval == 0)
|
---|
110 | return VERR_NOT_SUPPORTED;
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Allocate and initialize the timer handle.
|
---|
114 | */
|
---|
115 | PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
|
---|
116 | if (!pTimer)
|
---|
117 | return VERR_NO_MEMORY;
|
---|
118 |
|
---|
119 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
120 | pTimer->fSuspended = true;
|
---|
121 | if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
|
---|
122 | {
|
---|
123 | pTimer->fAllCpu = true;
|
---|
124 | pTimer->fSpecificCpu = false;
|
---|
125 | }
|
---|
126 | else if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
127 | {
|
---|
128 | pTimer->fAllCpu = false;
|
---|
129 | pTimer->fSpecificCpu = true;
|
---|
130 | pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
|
---|
131 | }
|
---|
132 | pTimer->interval = u64NanoInterval;
|
---|
133 | pTimer->pfnTimer = pfnTimer;
|
---|
134 | pTimer->pvUser = pvUser;
|
---|
135 | pTimer->stimer = NULL;
|
---|
136 | pTimer->gtimer = NULL;
|
---|
137 |
|
---|
138 | *ppTimer = pTimer;
|
---|
139 | return VINF_SUCCESS;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Validates the timer handle.
|
---|
145 | *
|
---|
146 | * @returns true if valid, false if invalid.
|
---|
147 | * @param pTimer The handle.
|
---|
148 | */
|
---|
149 | DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
|
---|
150 | {
|
---|
151 | AssertReturn(VALID_PTR(pTimer), false);
|
---|
152 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
158 | {
|
---|
159 | if (pTimer == NULL)
|
---|
160 | return VINF_SUCCESS;
|
---|
161 | if (!rtTimerIsValid(pTimer))
|
---|
162 | return VERR_INVALID_HANDLE;
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * Free the associated resources.
|
---|
166 | */
|
---|
167 | RTTimerStop(pTimer);
|
---|
168 | pTimer->u32Magic++;
|
---|
169 | RTMemFree(pTimer);
|
---|
170 | return VINF_SUCCESS;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
175 | {
|
---|
176 | int cpu = VBI_ANY_CPU;
|
---|
177 |
|
---|
178 | if (!rtTimerIsValid(pTimer))
|
---|
179 | return VERR_INVALID_HANDLE;
|
---|
180 | if (!pTimer->fSuspended)
|
---|
181 | return VERR_TIMER_ACTIVE;
|
---|
182 |
|
---|
183 | pTimer->fSuspended = false;
|
---|
184 | if (pTimer->fAllCpu)
|
---|
185 | {
|
---|
186 | pTimer->gtimer = vbi_gtimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval);
|
---|
187 | if (pTimer->gtimer == NULL)
|
---|
188 | return VERR_INVALID_PARAMETER;
|
---|
189 | }
|
---|
190 | else
|
---|
191 | {
|
---|
192 | if (pTimer->fSpecificCpu)
|
---|
193 | cpu = pTimer->iCpu;
|
---|
194 | pTimer->stimer = vbi_stimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval, cpu);
|
---|
195 | if (pTimer->stimer == NULL)
|
---|
196 | {
|
---|
197 | if (cpu != VBI_ANY_CPU)
|
---|
198 | return VERR_CPU_OFFLINE;
|
---|
199 | return VERR_INVALID_PARAMETER;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | return VINF_SUCCESS;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
208 | {
|
---|
209 | if (!rtTimerIsValid(pTimer))
|
---|
210 | return VERR_INVALID_HANDLE;
|
---|
211 | if (pTimer->fSuspended)
|
---|
212 | return VERR_TIMER_SUSPENDED;
|
---|
213 |
|
---|
214 | pTimer->fSuspended = true;
|
---|
215 | if (pTimer->stimer)
|
---|
216 | {
|
---|
217 | vbi_stimer_end(pTimer->stimer);
|
---|
218 | pTimer->stimer = NULL;
|
---|
219 | }
|
---|
220 | else if (pTimer->gtimer)
|
---|
221 | {
|
---|
222 | vbi_gtimer_end(pTimer->gtimer);
|
---|
223 | pTimer->gtimer = NULL;
|
---|
224 | }
|
---|
225 |
|
---|
226 | return VINF_SUCCESS;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 |
|
---|
231 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
232 | {
|
---|
233 | return vbi_timer_granularity();
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
238 | {
|
---|
239 | return VERR_NOT_SUPPORTED;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
244 | {
|
---|
245 | return VERR_NOT_SUPPORTED;
|
---|
246 | }
|
---|
247 |
|
---|