VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/vbi/timer-r0drv-solaris.c@ 33868

Last change on this file since 33868 was 32572, checked in by vboxsync, 14 years ago

VMM,SUPDrv,IPRT: More changes for related to the priodic preemption timer. (still disabled)

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