VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/timer-r0drv-nt.cpp@ 52320

Last change on this file since 52320 was 47637, checked in by vboxsync, 11 years ago

Runtime/r0drv/nt: fix Windows r0drv timers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.2 KB
Line 
1/* $Id: timer-r0drv-nt.cpp 47637 2013-08-09 13:12:19Z vboxsync $ */
2/** @file
3 * IPRT - Timers, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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* Header Files *
29*******************************************************************************/
30#include "the-nt-kernel.h"
31
32#include <iprt/timer.h>
33#include <iprt/mp.h>
34#include <iprt/cpuset.h>
35#include <iprt/err.h>
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/alloc.h>
39
40#include "internal-r0drv-nt.h"
41#include "internal/magics.h"
42
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47/**
48 * A sub timer structure.
49 *
50 * This is used for keeping the per-cpu tick and DPC object.
51 */
52typedef struct RTTIMERNTSUBTIMER
53{
54 /** The tick counter. */
55 uint64_t iTick;
56 /** Pointer to the parent timer. */
57 PRTTIMER pParent;
58 /** The NT DPC object. */
59 KDPC NtDpc;
60} RTTIMERNTSUBTIMER;
61/** Pointer to a NT sub-timer structure. */
62typedef RTTIMERNTSUBTIMER *PRTTIMERNTSUBTIMER;
63
64/**
65 * The internal representation of an Linux timer handle.
66 */
67typedef struct RTTIMER
68{
69 /** Magic.
70 * This is RTTIMER_MAGIC, but changes to something else before the timer
71 * is destroyed to indicate clearly that thread should exit. */
72 uint32_t volatile u32Magic;
73 /** Flag indicating the timer is suspended. */
74 bool volatile fSuspended;
75 /** Whether the timer must run on one specific CPU or not. */
76 bool fSpecificCpu;
77 /** Whether the timer must run on all CPUs or not. */
78 bool fOmniTimer;
79 /** The CPU it must run on if fSpecificCpu is set.
80 * The master CPU for an omni-timer. */
81 RTCPUID idCpu;
82 /** Callback. */
83 PFNRTTIMER pfnTimer;
84 /** User argument. */
85 void *pvUser;
86 /** The timer interval. 0 if one-shot. */
87 uint64_t u64NanoInterval;
88 /** The Nt timer object. */
89 KTIMER NtTimer;
90 /** The number of sub-timers. */
91 RTCPUID cSubTimers;
92 /** Sub-timers.
93 * Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
94 * an entry for all possible cpus. In that case the index will be the same as
95 * for the RTCpuSet. */
96 RTTIMERNTSUBTIMER aSubTimers[1];
97} RTTIMER;
98
99
100
101/**
102 * Timer callback function for the non-omni timers.
103 *
104 * @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a one-shot or interval timer.
105 * @param pDpc Pointer to the DPC.
106 * @param pvUser Pointer to our internal timer structure.
107 * @param SystemArgument1 Some system argument.
108 * @param SystemArgument2 Some system argument.
109 */
110static void _stdcall rtTimerNtSimpleCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
111{
112 PRTTIMER pTimer = (PRTTIMER)pvUser;
113 AssertPtr(pTimer);
114#ifdef RT_STRICT
115 if (KeGetCurrentIrql() < DISPATCH_LEVEL)
116 RTAssertMsg2Weak("rtTimerNtSimpleCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
117#endif
118
119 /*
120 * Check that we haven't been suspended before doing the callout.
121 */
122 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
123 && pTimer->u32Magic == RTTIMER_MAGIC)
124 {
125 if (!pTimer->u64NanoInterval)
126 ASMAtomicWriteBool(&pTimer->fSuspended, true);
127 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->aSubTimers[0].iTick);
128 }
129
130 NOREF(pDpc); NOREF(SystemArgument1); NOREF(SystemArgument2);
131}
132
133
134/**
135 * The slave DPC callback for an omni timer.
136 *
137 * @param pDpc The DPC object.
138 * @param pvUser Pointer to the sub-timer.
139 * @param SystemArgument1 Some system stuff.
140 * @param SystemArgument2 Some system stuff.
141 */
142static void _stdcall rtTimerNtOmniSlaveCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
143{
144 PRTTIMERNTSUBTIMER pSubTimer = (PRTTIMERNTSUBTIMER)pvUser;
145 PRTTIMER pTimer = pSubTimer->pParent;
146
147 AssertPtr(pTimer);
148#ifdef RT_STRICT
149 if (KeGetCurrentIrql() < DISPATCH_LEVEL)
150 RTAssertMsg2Weak("rtTimerNtOmniSlaveCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
151 int iCpuSelf = RTMpCpuIdToSetIndex(RTMpCpuId());
152 if (pSubTimer - &pTimer->aSubTimers[0] != iCpuSelf)
153 RTAssertMsg2Weak("rtTimerNtOmniSlaveCallback: iCpuSelf=%d pSubTimer=%p / %d\n", iCpuSelf, pSubTimer, pSubTimer - &pTimer->aSubTimers[0]);
154#endif
155
156 /*
157 * Check that we haven't been suspended before doing the callout.
158 */
159 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
160 && pTimer->u32Magic == RTTIMER_MAGIC)
161 {
162 if (!pTimer->u64NanoInterval)
163 ASMAtomicWriteBool(&pTimer->fSuspended, true);
164 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
165 }
166
167 NOREF(pDpc); NOREF(SystemArgument1); NOREF(SystemArgument2);
168}
169
170
171/**
172 * The timer callback for an omni-timer.
173 *
174 * This is responsible for queueing the DPCs for the other CPUs and
175 * perform the callback on the CPU on which it is called.
176 *
177 * @param pDpc The DPC object.
178 * @param pvUser Pointer to the sub-timer.
179 * @param SystemArgument1 Some system stuff.
180 * @param SystemArgument2 Some system stuff.
181 */
182static void _stdcall rtTimerNtOmniMasterCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
183{
184 PRTTIMERNTSUBTIMER pSubTimer = (PRTTIMERNTSUBTIMER)pvUser;
185 PRTTIMER pTimer = pSubTimer->pParent;
186 int iCpuSelf = RTMpCpuIdToSetIndex(RTMpCpuId());
187
188 AssertPtr(pTimer);
189#ifdef RT_STRICT
190 if (KeGetCurrentIrql() < DISPATCH_LEVEL)
191 RTAssertMsg2Weak("rtTimerNtOmniMasterCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
192 if (pSubTimer - &pTimer->aSubTimers[0] != iCpuSelf)
193 RTAssertMsg2Weak("rtTimerNtOmniMasterCallback: iCpuSelf=%d pSubTimer=%p / %d\n", iCpuSelf, pSubTimer, pSubTimer - &pTimer->aSubTimers[0]);
194#endif
195
196 /*
197 * Check that we haven't been suspended before scheduling the other DPCs
198 * and doing the callout.
199 */
200 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
201 && pTimer->u32Magic == RTTIMER_MAGIC)
202 {
203 RTCPUSET OnlineSet;
204 RTMpGetOnlineSet(&OnlineSet);
205 for (int iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
206 if ( RTCpuSetIsMemberByIndex(&OnlineSet, iCpu)
207 && iCpuSelf != iCpu)
208 KeInsertQueueDpc(&pTimer->aSubTimers[iCpu].NtDpc, 0, 0);
209
210 if (!pTimer->u64NanoInterval)
211 ASMAtomicWriteBool(&pTimer->fSuspended, true);
212 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
213 }
214
215 NOREF(pDpc); NOREF(SystemArgument1); NOREF(SystemArgument2);
216}
217
218
219
220RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
221{
222 /*
223 * Validate.
224 */
225 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
226 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
227
228 if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
229 return VERR_TIMER_ACTIVE;
230 if ( pTimer->fSpecificCpu
231 && !RTMpIsCpuOnline(pTimer->idCpu))
232 return VERR_CPU_OFFLINE;
233
234 /*
235 * Start the timer.
236 */
237 PKDPC pMasterDpc = pTimer->fOmniTimer
238 ? &pTimer->aSubTimers[RTMpCpuIdToSetIndex(pTimer->idCpu)].NtDpc
239 : &pTimer->aSubTimers[0].NtDpc;
240
241 uint64_t u64Interval = pTimer->u64NanoInterval / 1000000; /* This is ms, believe it or not. */
242 ULONG ulInterval = (ULONG)u64Interval;
243 if (ulInterval != u64Interval)
244 ulInterval = MAXLONG;
245 else if (!ulInterval && pTimer->u64NanoInterval)
246 ulInterval = 1;
247
248 LARGE_INTEGER DueTime;
249 DueTime.QuadPart = -(int64_t)(u64First / 100); /* Relative, NT time. */
250 if (!DueTime.QuadPart)
251 DueTime.QuadPart = -1;
252
253 unsigned cSubTimers = pTimer->fOmniTimer ? pTimer->cSubTimers : 1;
254 for (unsigned iCpu = 0; iCpu < cSubTimers; iCpu++)
255 pTimer->aSubTimers[iCpu].iTick = 0;
256 ASMAtomicWriteBool(&pTimer->fSuspended, false);
257 KeSetTimerEx(&pTimer->NtTimer, DueTime, ulInterval, pMasterDpc);
258 return VINF_SUCCESS;
259}
260
261
262/**
263 * Worker function that stops an active timer.
264 *
265 * Shared by RTTimerStop and RTTimerDestroy.
266 *
267 * @param pTimer The active timer.
268 */
269static void rtTimerNtStopWorker(PRTTIMER pTimer)
270{
271 /*
272 * Just cancel the timer, dequeue the DPCs and flush them (if this is supported).
273 */
274 ASMAtomicWriteBool(&pTimer->fSuspended, true);
275 KeCancelTimer(&pTimer->NtTimer);
276
277 for (RTCPUID iCpu = 0; iCpu < pTimer->cSubTimers; iCpu++)
278 KeRemoveQueueDpc(&pTimer->aSubTimers[iCpu].NtDpc);
279
280 /*
281 * I'm a bit uncertain whether this should be done during RTTimerStop
282 * or only in RTTimerDestroy()... Linux and Solaris will wait AFAIK,
283 * which is why I'm keeping this here for now.
284 */
285 if (g_pfnrtNtKeFlushQueuedDpcs)
286 g_pfnrtNtKeFlushQueuedDpcs();
287}
288
289
290RTDECL(int) RTTimerStop(PRTTIMER pTimer)
291{
292 /*
293 * Validate.
294 */
295 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
296 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
297
298 if (ASMAtomicUoReadBool(&pTimer->fSuspended))
299 return VERR_TIMER_SUSPENDED;
300
301 /*
302 * Call the worker we share with RTTimerDestroy.
303 */
304 rtTimerNtStopWorker(pTimer);
305 return VINF_SUCCESS;
306}
307
308
309RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
310{
311 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
312 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
313
314 return VERR_NOT_SUPPORTED;
315}
316
317
318RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
319{
320 /* It's ok to pass NULL pointer. */
321 if (pTimer == /*NIL_RTTIMER*/ NULL)
322 return VINF_SUCCESS;
323 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
324 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
325
326 /*
327 * Invalidate the timer, stop it if it's running and finally
328 * free up the memory.
329 */
330 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
331 if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
332 rtTimerNtStopWorker(pTimer);
333 RTMemFree(pTimer);
334
335 return VINF_SUCCESS;
336}
337
338
339RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
340{
341 *ppTimer = NULL;
342
343 /*
344 * Validate flags.
345 */
346 if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
347 return VERR_INVALID_PARAMETER;
348 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
349 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
350 && !RTMpIsCpuPossible(RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK)))
351 return VERR_CPU_NOT_FOUND;
352
353 /*
354 * Allocate the timer handler.
355 */
356 RTCPUID cSubTimers = 1;
357 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
358 {
359 cSubTimers = RTMpGetMaxCpuId() + 1;
360 Assert(cSubTimers <= RTCPUSET_MAX_CPUS); /* On Windows we have a 1:1 relationship between cpuid and set index. */
361 }
362
363 PRTTIMER pTimer = (PRTTIMER)RTMemAllocZ(RT_OFFSETOF(RTTIMER, aSubTimers[cSubTimers]));
364 if (!pTimer)
365 return VERR_NO_MEMORY;
366
367 /*
368 * Initialize it.
369 */
370 pTimer->u32Magic = RTTIMER_MAGIC;
371 pTimer->fSuspended = true;
372 pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
373 pTimer->fOmniTimer = (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL;
374 pTimer->idCpu = pTimer->fSpecificCpu ? RTMpCpuIdFromSetIndex(fFlags & RTTIMER_FLAGS_CPU_MASK) : NIL_RTCPUID;
375 pTimer->cSubTimers = cSubTimers;
376 pTimer->pfnTimer = pfnTimer;
377 pTimer->pvUser = pvUser;
378 pTimer->u64NanoInterval = u64NanoInterval;
379 KeInitializeTimerEx(&pTimer->NtTimer, SynchronizationTimer);
380 if (pTimer->fOmniTimer)
381 {
382 /*
383 * Initialize the per-cpu "sub-timers", select the first online cpu
384 * to be the master.
385 * ASSUMES that no cpus will ever go offline.
386 */
387 pTimer->idCpu = NIL_RTCPUID;
388 for (unsigned iCpu = 0; iCpu < cSubTimers; iCpu++)
389 {
390 pTimer->aSubTimers[iCpu].iTick = 0;
391 pTimer->aSubTimers[iCpu].pParent = pTimer;
392
393 if ( pTimer->idCpu == NIL_RTCPUID
394 && RTMpIsCpuOnline(RTMpCpuIdFromSetIndex(iCpu)))
395 {
396 pTimer->idCpu = RTMpCpuIdFromSetIndex(iCpu);
397 KeInitializeDpc(&pTimer->aSubTimers[iCpu].NtDpc, rtTimerNtOmniMasterCallback, &pTimer->aSubTimers[iCpu]);
398 }
399 else
400 KeInitializeDpc(&pTimer->aSubTimers[iCpu].NtDpc, rtTimerNtOmniSlaveCallback, &pTimer->aSubTimers[iCpu]);
401 KeSetImportanceDpc(&pTimer->aSubTimers[iCpu].NtDpc, HighImportance);
402 KeSetTargetProcessorDpc(&pTimer->aSubTimers[iCpu].NtDpc, (int)RTMpCpuIdFromSetIndex(iCpu));
403 }
404 Assert(pTimer->idCpu != NIL_RTCPUID);
405 }
406 else
407 {
408 /*
409 * Initialize the first "sub-timer", target the DPC on a specific processor
410 * if requested to do so.
411 */
412 pTimer->aSubTimers[0].iTick = 0;
413 pTimer->aSubTimers[0].pParent = pTimer;
414
415 KeInitializeDpc(&pTimer->aSubTimers[0].NtDpc, rtTimerNtSimpleCallback, pTimer);
416 KeSetImportanceDpc(&pTimer->aSubTimers[0].NtDpc, HighImportance);
417 if (pTimer->fSpecificCpu)
418 KeSetTargetProcessorDpc(&pTimer->aSubTimers[0].NtDpc, (int)pTimer->idCpu);
419 }
420
421 *ppTimer = pTimer;
422 return VINF_SUCCESS;
423}
424
425
426RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
427{
428 if (!g_pfnrtNtExSetTimerResolution)
429 return VERR_NOT_SUPPORTED;
430
431 ULONG ulGranted = g_pfnrtNtExSetTimerResolution(u32Request / 100, TRUE);
432 if (pu32Granted)
433 *pu32Granted = ulGranted * 100; /* NT -> ns */
434 return VINF_SUCCESS;
435}
436
437
438RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
439{
440 if (!g_pfnrtNtExSetTimerResolution)
441 return VERR_NOT_SUPPORTED;
442
443 g_pfnrtNtExSetTimerResolution(0 /* ignored */, FALSE);
444 NOREF(u32Granted);
445 return VINF_SUCCESS;
446}
447
448
449RTDECL(bool) RTTimerCanDoHighResolution(void)
450{
451 return false;
452}
453
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