VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/TMAll.cpp@ 31631

Last change on this file since 31631 was 30799, checked in by vboxsync, 14 years ago

TM: Made it possible to enable the resettable accounting stats in release builds (from the makefile).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 76.5 KB
Line 
1/* $Id: TMAll.cpp 30799 2010-07-13 08:16:37Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, all contexts.
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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_TM
23#include <VBox/tm.h>
24#include <VBox/mm.h>
25#ifdef IN_RING3
26# include <VBox/rem.h>
27#endif
28#include "TMInternal.h"
29#include <VBox/vm.h>
30
31#include <VBox/param.h>
32#include <VBox/err.h>
33#include <VBox/log.h>
34#include <VBox/sup.h>
35#include <iprt/time.h>
36#include <iprt/assert.h>
37#include <iprt/asm.h>
38#include <iprt/asm-math.h>
39#ifdef IN_RING3
40# include <iprt/thread.h>
41#endif
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47/** @def TMTIMER_ASSERT_CRITSECT
48 * Checks that the caller owns the critical section if one is associated with
49 * the timer. */
50#ifdef VBOX_STRICT
51# define TMTIMER_ASSERT_CRITSECT(pTimer) \
52 do { \
53 if ((pTimer)->pCritSect) \
54 { \
55 PPDMCRITSECT pCritSect = (PPDMCRITSECT)MMHyperR3ToCC((pTimer)->CTX_SUFF(pVM), (pTimer)->pCritSect); \
56 AssertMsg(pCritSect && PDMCritSectIsOwner(pCritSect), \
57 ("pTimer=%p (%s) pCritSect=%p\n", pTimer, R3STRING(pTimer->pszDesc), (pTimer)->pCritSect)); \
58 } \
59 } while (0)
60#else
61# define TMTIMER_ASSERT_CRITSECT(pTimer) do { } while (0)
62#endif
63
64
65#ifndef tmTimerLock
66
67/**
68 * Try take the timer lock, wait in ring-3 return VERR_SEM_BUSY in R0/RC.
69 *
70 * @retval VINF_SUCCESS on success (always in ring-3).
71 * @retval VERR_SEM_BUSY in RC and R0 if the semaphore is busy.
72 *
73 * @param pVM The VM handle.
74 *
75 * @thread EMTs for the time being.
76 */
77int tmTimerLock(PVM pVM)
78{
79 VM_ASSERT_EMT(pVM);
80 int rc = PDMCritSectEnter(&pVM->tm.s.TimerCritSect, VERR_SEM_BUSY);
81 return rc;
82}
83
84
85/**
86 * Try take the timer lock, no waiting.
87 *
88 * @retval VINF_SUCCESS on success.
89 * @retval VERR_SEM_BUSY if busy.
90 *
91 * @param pVM The VM handle.
92 */
93int tmTimerTryLock(PVM pVM)
94{
95 int rc = PDMCritSectTryEnter(&pVM->tm.s.TimerCritSect);
96 return rc;
97}
98
99
100/**
101 * Release the EMT/TM lock.
102 *
103 * @param pVM The VM handle.
104 */
105void tmTimerUnlock(PVM pVM)
106{
107 PDMCritSectLeave(&pVM->tm.s.TimerCritSect);
108}
109
110
111/**
112 * Try take the VirtualSync lock, wait in ring-3 return VERR_SEM_BUSY in R0/RC.
113 *
114 * @retval VINF_SUCCESS on success (always in ring-3).
115 * @retval VERR_SEM_BUSY in RC and R0 if the semaphore is busy.
116 *
117 * @param pVM The VM handle.
118 */
119int tmVirtualSyncLock(PVM pVM)
120{
121 VM_ASSERT_EMT(pVM);
122 int rc = PDMCritSectEnter(&pVM->tm.s.VirtualSyncLock, VERR_SEM_BUSY);
123 return rc;
124}
125
126
127/**
128 * Try take the VirtualSync lock, no waiting.
129 *
130 * @retval VINF_SUCCESS on success.
131 * @retval VERR_SEM_BUSY if busy.
132 *
133 * @param pVM The VM handle.
134 */
135int tmVirtualSyncTryLock(PVM pVM)
136{
137 VM_ASSERT_EMT(pVM);
138 int rc = PDMCritSectTryEnter(&pVM->tm.s.VirtualSyncLock);
139 return rc;
140}
141
142
143/**
144 * Release the VirtualSync lock.
145 *
146 * @param pVM The VM handle.
147 */
148void tmVirtualSyncUnlock(PVM pVM)
149{
150 PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
151}
152
153#endif /* ! macros */
154
155/**
156 * Notification that execution is about to start.
157 *
158 * This call must always be paired with a TMNotifyEndOfExecution call.
159 *
160 * The function may, depending on the configuration, resume the TSC and future
161 * clocks that only ticks when we're executing guest code.
162 *
163 * @param pVCpu The VMCPU to operate on.
164 */
165VMMDECL(void) TMNotifyStartOfExecution(PVMCPU pVCpu)
166{
167 PVM pVM = pVCpu->CTX_SUFF(pVM);
168
169#ifndef VBOX_WITHOUT_NS_ACCOUNTING
170 pVCpu->tm.s.u64NsTsStartExecuting = RTTimeNanoTS();
171#endif
172 if (pVM->tm.s.fTSCTiedToExecution)
173 tmCpuTickResume(pVM, pVCpu);
174}
175
176
177/**
178 * Notification that execution is about to start.
179 *
180 * This call must always be paired with a TMNotifyStartOfExecution call.
181 *
182 * The function may, depending on the configuration, suspend the TSC and future
183 * clocks that only ticks when we're executing guest code.
184 *
185 * @param pVCpu The VMCPU to operate on.
186 */
187VMMDECL(void) TMNotifyEndOfExecution(PVMCPU pVCpu)
188{
189 PVM pVM = pVCpu->CTX_SUFF(pVM);
190
191 if (pVM->tm.s.fTSCTiedToExecution)
192 tmCpuTickPause(pVM, pVCpu);
193
194#ifndef VBOX_WITHOUT_NS_ACCOUNTING
195 uint64_t const u64NsTs = RTTimeNanoTS();
196 uint64_t const cNsTotalNew = u64NsTs - pVCpu->tm.s.u64NsTsStartTotal;
197 uint64_t const cNsExecutingDelta = u64NsTs - pVCpu->tm.s.u64NsTsStartExecuting;
198 uint64_t const cNsExecutingNew = pVCpu->tm.s.cNsExecuting + cNsExecutingDelta;
199 uint64_t const cNsOtherNew = cNsTotalNew - cNsExecutingNew - pVCpu->tm.s.cNsHalted;
200
201# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
202 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecuting, cNsExecutingDelta);
203 STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsTotal, cNsTotalNew - pVCpu->tm.s.cNsTotal);
204 int64_t const cNsOtherNewDelta = cNsOtherNew - pVCpu->tm.s.cNsOther;
205 if (cNsOtherNewDelta > 0)
206 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsOther, cNsOtherNewDelta); /* (the period before execution) */
207# endif
208
209 uint32_t uGen = ASMAtomicIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
210 pVCpu->tm.s.cNsExecuting = cNsExecutingNew;
211 pVCpu->tm.s.cNsTotal = cNsTotalNew;
212 pVCpu->tm.s.cNsOther = cNsOtherNew;
213 pVCpu->tm.s.cPeriodsExecuting++;
214 ASMAtomicWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
215#endif
216}
217
218
219/**
220 * Notification that the cpu is entering the halt state
221 *
222 * This call must always be paired with a TMNotifyEndOfExecution call.
223 *
224 * The function may, depending on the configuration, resume the TSC and future
225 * clocks that only ticks when we're halted.
226 *
227 * @param pVCpu The VMCPU to operate on.
228 */
229VMM_INT_DECL(void) TMNotifyStartOfHalt(PVMCPU pVCpu)
230{
231 PVM pVM = pVCpu->CTX_SUFF(pVM);
232
233#ifndef VBOX_WITHOUT_NS_ACCOUNTING
234 pVCpu->tm.s.u64NsTsStartHalting = RTTimeNanoTS();
235#endif
236
237 if ( pVM->tm.s.fTSCTiedToExecution
238 && !pVM->tm.s.fTSCNotTiedToHalt)
239 tmCpuTickResume(pVM, pVCpu);
240}
241
242
243/**
244 * Notification that the cpu is leaving the halt state
245 *
246 * This call must always be paired with a TMNotifyStartOfHalt call.
247 *
248 * The function may, depending on the configuration, suspend the TSC and future
249 * clocks that only ticks when we're halted.
250 *
251 * @param pVCpu The VMCPU to operate on.
252 */
253VMM_INT_DECL(void) TMNotifyEndOfHalt(PVMCPU pVCpu)
254{
255 PVM pVM = pVCpu->CTX_SUFF(pVM);
256
257 if ( pVM->tm.s.fTSCTiedToExecution
258 && !pVM->tm.s.fTSCNotTiedToHalt)
259 tmCpuTickPause(pVM, pVCpu);
260
261#ifndef VBOX_WITHOUT_NS_ACCOUNTING
262 uint64_t const u64NsTs = RTTimeNanoTS();
263 uint64_t const cNsTotalNew = u64NsTs - pVCpu->tm.s.u64NsTsStartTotal;
264 uint64_t const cNsHaltedDelta = u64NsTs - pVCpu->tm.s.u64NsTsStartHalting;
265 uint64_t const cNsHaltedNew = pVCpu->tm.s.cNsHalted + cNsHaltedDelta;
266 uint64_t const cNsOtherNew = cNsTotalNew - pVCpu->tm.s.cNsExecuting - cNsHaltedNew;
267
268# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
269 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsHalted, cNsHaltedDelta);
270 STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsTotal, cNsTotalNew - pVCpu->tm.s.cNsTotal);
271 int64_t const cNsOtherNewDelta = cNsOtherNew - pVCpu->tm.s.cNsOther;
272 if (cNsOtherNewDelta > 0)
273 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsOther, cNsOtherNewDelta); /* (the period before halting) */
274# endif
275
276 uint32_t uGen = ASMAtomicIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
277 pVCpu->tm.s.cNsHalted = cNsHaltedNew;
278 pVCpu->tm.s.cNsTotal = cNsTotalNew;
279 pVCpu->tm.s.cNsOther = cNsOtherNew;
280 pVCpu->tm.s.cPeriodsHalted++;
281 ASMAtomicWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
282#endif
283}
284
285
286/**
287 * Raise the timer force action flag and notify the dedicated timer EMT.
288 *
289 * @param pVM The VM handle.
290 */
291DECLINLINE(void) tmScheduleNotify(PVM pVM)
292{
293 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
294 if (!VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
295 {
296 Log5(("TMAll(%u): FF: 0 -> 1\n", __LINE__));
297 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
298#ifdef IN_RING3
299 REMR3NotifyTimerPending(pVM, pVCpuDst);
300 VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
301#endif
302 STAM_COUNTER_INC(&pVM->tm.s.StatScheduleSetFF);
303 }
304}
305
306
307/**
308 * Schedule the queue which was changed.
309 */
310DECLINLINE(void) tmSchedule(PTMTIMER pTimer)
311{
312 PVM pVM = pTimer->CTX_SUFF(pVM);
313 if ( VM_IS_EMT(pVM)
314 && RT_SUCCESS(tmTimerTryLock(pVM)))
315 {
316 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
317 Log3(("tmSchedule: tmTimerQueueSchedule\n"));
318 tmTimerQueueSchedule(pVM, &pVM->tm.s.CTX_SUFF(paTimerQueues)[pTimer->enmClock]);
319#ifdef VBOX_STRICT
320 tmTimerQueuesSanityChecks(pVM, "tmSchedule");
321#endif
322 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
323 tmTimerUnlock(pVM);
324 }
325 else
326 {
327 TMTIMERSTATE enmState = pTimer->enmState;
328 if (TMTIMERSTATE_IS_PENDING_SCHEDULING(enmState))
329 tmScheduleNotify(pVM);
330 }
331}
332
333
334/**
335 * Try change the state to enmStateNew from enmStateOld
336 * and link the timer into the scheduling queue.
337 *
338 * @returns Success indicator.
339 * @param pTimer Timer in question.
340 * @param enmStateNew The new timer state.
341 * @param enmStateOld The old timer state.
342 */
343DECLINLINE(bool) tmTimerTry(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
344{
345 /*
346 * Attempt state change.
347 */
348 bool fRc;
349 TM_TRY_SET_STATE(pTimer, enmStateNew, enmStateOld, fRc);
350 return fRc;
351}
352
353
354/**
355 * Links the timer onto the scheduling queue.
356 *
357 * @param pQueue The timer queue the timer belongs to.
358 * @param pTimer The timer.
359 *
360 * @todo FIXME: Look into potential race with the thread running the queues
361 * and stuff.
362 */
363DECLINLINE(void) tmTimerLink(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
364{
365 Assert(!pTimer->offScheduleNext);
366 const int32_t offHeadNew = (intptr_t)pTimer - (intptr_t)pQueue;
367 int32_t offHead;
368 do
369 {
370 offHead = pQueue->offSchedule;
371 if (offHead)
372 pTimer->offScheduleNext = ((intptr_t)pQueue + offHead) - (intptr_t)pTimer;
373 else
374 pTimer->offScheduleNext = 0;
375 } while (!ASMAtomicCmpXchgS32(&pQueue->offSchedule, offHeadNew, offHead));
376}
377
378
379/**
380 * Try change the state to enmStateNew from enmStateOld
381 * and link the timer into the scheduling queue.
382 *
383 * @returns Success indicator.
384 * @param pTimer Timer in question.
385 * @param enmStateNew The new timer state.
386 * @param enmStateOld The old timer state.
387 */
388DECLINLINE(bool) tmTimerTryWithLink(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
389{
390 if (tmTimerTry(pTimer, enmStateNew, enmStateOld))
391 {
392 tmTimerLink(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF(paTimerQueues)[pTimer->enmClock], pTimer);
393 return true;
394 }
395 return false;
396}
397
398
399#ifdef VBOX_HIGH_RES_TIMERS_HACK
400
401/**
402 * Worker for tmTimerPollInternal that handles misses when the decidate timer
403 * EMT is polling.
404 *
405 * @returns See tmTimerPollInternal.
406 * @param pVM Pointer to the shared VM structure.
407 * @param u64Now Current virtual clock timestamp.
408 * @param u64Delta The delta to the next even in ticks of the
409 * virtual clock.
410 * @param pu64Delta Where to return the delta.
411 * @param pCounter The statistics counter to update.
412 */
413DECLINLINE(uint64_t) tmTimerPollReturnMiss(PVM pVM, uint64_t u64Now, uint64_t u64Delta, uint64_t *pu64Delta)
414{
415 Assert(!(u64Delta & RT_BIT_64(63)));
416
417 if (!pVM->tm.s.fVirtualWarpDrive)
418 {
419 *pu64Delta = u64Delta;
420 return u64Delta + u64Now + pVM->tm.s.u64VirtualOffset;
421 }
422
423 /*
424 * Warp drive adjustments - this is the reverse of what tmVirtualGetRaw is doing.
425 */
426 uint64_t const u64Start = pVM->tm.s.u64VirtualWarpDriveStart;
427 uint32_t const u32Pct = pVM->tm.s.u32VirtualWarpDrivePercentage;
428
429 uint64_t u64GipTime = u64Delta + u64Now + pVM->tm.s.u64VirtualOffset;
430 u64GipTime -= u64Start; /* the start is GIP time. */
431 if (u64GipTime >= u64Delta)
432 {
433 ASMMultU64ByU32DivByU32(u64GipTime, 100, u32Pct);
434 ASMMultU64ByU32DivByU32(u64Delta, 100, u32Pct);
435 }
436 else
437 {
438 u64Delta -= u64GipTime;
439 ASMMultU64ByU32DivByU32(u64GipTime, 100, u32Pct);
440 u64Delta += u64GipTime;
441 }
442 *pu64Delta = u64Delta;
443 u64GipTime += u64Start;
444 return u64GipTime;
445}
446
447
448/**
449 * Worker for tmTimerPollInternal dealing with returns on virtual CPUs other
450 * than the one dedicated to timer work.
451 *
452 * @returns See tmTimerPollInternal.
453 * @param pVM Pointer to the shared VM structure.
454 * @param u64Now Current virtual clock timestamp.
455 * @param pu64Delta Where to return the delta.
456 */
457DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnOtherCpu(PVM pVM, uint64_t u64Now, uint64_t *pu64Delta)
458{
459 static const uint64_t s_u64OtherRet = 500000000; /* 500 ms for non-timer EMTs. */
460 *pu64Delta = s_u64OtherRet;
461 return u64Now + pVM->tm.s.u64VirtualOffset + s_u64OtherRet;
462}
463
464
465/**
466 * Worker for tmTimerPollInternal.
467 *
468 * @returns See tmTimerPollInternal.
469 * @param pVM Pointer to the shared VM structure.
470 * @param pVCpu Pointer to the shared VMCPU structure of the
471 * caller.
472 * @param pVCpuDst Pointer to the shared VMCPU structure of the
473 * dedicated timer EMT.
474 * @param u64Now Current virtual clock timestamp.
475 * @param pu64Delta Where to return the delta.
476 * @param pCounter The statistics counter to update.
477 */
478DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnHit(PVM pVM, PVMCPU pVCpu, PVMCPU pVCpuDst, uint64_t u64Now,
479 uint64_t *pu64Delta, PSTAMCOUNTER pCounter)
480{
481 STAM_COUNTER_INC(pCounter);
482 if (pVCpuDst != pVCpu)
483 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
484 *pu64Delta = 0;
485 return 0;
486}
487
488/**
489 * Common worker for TMTimerPollGIP and TMTimerPoll.
490 *
491 * This function is called before FFs are checked in the inner execution EM loops.
492 *
493 * @returns The GIP timestamp of the next event.
494 * 0 if the next event has already expired.
495 *
496 * @param pVM Pointer to the shared VM structure.
497 * @param pVCpu Pointer to the shared VMCPU structure of the caller.
498 * @param pu64Delta Where to store the delta.
499 *
500 * @thread The emulation thread.
501 *
502 * @remarks GIP uses ns ticks.
503 */
504DECL_FORCE_INLINE(uint64_t) tmTimerPollInternal(PVM pVM, PVMCPU pVCpu, uint64_t *pu64Delta)
505{
506 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
507 const uint64_t u64Now = TMVirtualGetNoCheck(pVM);
508 STAM_COUNTER_INC(&pVM->tm.s.StatPoll);
509
510 /*
511 * Return straight away if the timer FF is already set ...
512 */
513 if (VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
514 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
515
516 /*
517 * ... or if timers are being run.
518 */
519 if (ASMAtomicReadBool(&pVM->tm.s.fRunningQueues))
520 {
521 STAM_COUNTER_INC(&pVM->tm.s.StatPollRunning);
522 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
523 }
524
525 /*
526 * Check for TMCLOCK_VIRTUAL expiration.
527 */
528 const uint64_t u64Expire1 = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire);
529 const int64_t i64Delta1 = u64Expire1 - u64Now;
530 if (i64Delta1 <= 0)
531 {
532 if (!VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
533 {
534 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_ISPENDING(pVCpuDst, VMCPU_FF_TIMER)));
535 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
536#ifdef IN_RING3
537 REMR3NotifyTimerPending(pVM, pVCpuDst);
538#endif
539 }
540 LogFlow(("TMTimerPoll: expire1=%'RU64 <= now=%'RU64\n", u64Expire1, u64Now));
541 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtual);
542 }
543
544 /*
545 * Check for TMCLOCK_VIRTUAL_SYNC expiration.
546 * This isn't quite as stright forward if in a catch-up, not only do
547 * we have to adjust the 'now' but when have to adjust the delta as well.
548 */
549
550 /*
551 * Optimistic lockless approach.
552 */
553 uint64_t u64VirtualSyncNow;
554 uint64_t u64Expire2 = ASMAtomicUoReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
555 if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
556 {
557 if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
558 {
559 u64VirtualSyncNow = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
560 if (RT_LIKELY( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
561 && !ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
562 && u64VirtualSyncNow == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
563 && u64Expire2 == ASMAtomicUoReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)))
564 {
565 u64VirtualSyncNow = u64Now - u64VirtualSyncNow;
566 int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
567 if (i64Delta2 > 0)
568 {
569 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
570 STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
571
572 if (pVCpu == pVCpuDst)
573 return tmTimerPollReturnMiss(pVM, u64Now, RT_MIN(i64Delta1, i64Delta2), pu64Delta);
574 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
575 }
576
577 if ( !pVM->tm.s.fRunningQueues
578 && !VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
579 {
580 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_ISPENDING(pVCpuDst, VMCPU_FF_TIMER)));
581 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
582#ifdef IN_RING3
583 REMR3NotifyTimerPending(pVM, pVCpuDst);
584#endif
585 }
586
587 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
588 LogFlow(("TMTimerPoll: expire2=%'RU64 <= now=%'RU64\n", u64Expire2, u64Now));
589 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
590 }
591 }
592 }
593 else
594 {
595 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
596 LogFlow(("TMTimerPoll: stopped\n"));
597 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
598 }
599
600 /*
601 * Complicated lockless approach.
602 */
603 uint64_t off;
604 uint32_t u32Pct = 0;
605 bool fCatchUp;
606 int cOuterTries = 42;
607 for (;; cOuterTries--)
608 {
609 fCatchUp = ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp);
610 off = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
611 u64Expire2 = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
612 if (fCatchUp)
613 {
614 /* No changes allowed, try get a consistent set of parameters. */
615 uint64_t const u64Prev = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev);
616 uint64_t const offGivenUp = ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp);
617 u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage);
618 if ( ( u64Prev == ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev)
619 && offGivenUp == ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp)
620 && u32Pct == ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage)
621 && off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
622 && u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)
623 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
624 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
625 || cOuterTries <= 0)
626 {
627 uint64_t u64Delta = u64Now - u64Prev;
628 if (RT_LIKELY(!(u64Delta >> 32)))
629 {
630 uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, u32Pct, 100);
631 if (off > u64Sub + offGivenUp)
632 off -= u64Sub;
633 else /* we've completely caught up. */
634 off = offGivenUp;
635 }
636 else
637 /* More than 4 seconds since last time (or negative), ignore it. */
638 Log(("TMVirtualGetSync: u64Delta=%RX64 (NoLock)\n", u64Delta));
639
640 /* Check that we're still running and in catch up. */
641 if ( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
642 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
643 break;
644 }
645 }
646 else if ( off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
647 && u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)
648 && !ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
649 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
650 break; /* Got an consistent offset */
651
652 /* Repeat the initial checks before iterating. */
653 if (VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
654 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
655 if (ASMAtomicUoReadBool(&pVM->tm.s.fRunningQueues))
656 {
657 STAM_COUNTER_INC(&pVM->tm.s.StatPollRunning);
658 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
659 }
660 if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
661 {
662 LogFlow(("TMTimerPoll: stopped\n"));
663 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
664 }
665 if (cOuterTries <= 0)
666 break; /* that's enough */
667 }
668 if (cOuterTries <= 0)
669 STAM_COUNTER_INC(&pVM->tm.s.StatPollELoop);
670 u64VirtualSyncNow = u64Now - off;
671
672 /* Calc delta and see if we've got a virtual sync hit. */
673 int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
674 if (i64Delta2 <= 0)
675 {
676 if ( !pVM->tm.s.fRunningQueues
677 && !VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
678 {
679 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_ISPENDING(pVCpuDst, VMCPU_FF_TIMER)));
680 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
681#ifdef IN_RING3
682 REMR3NotifyTimerPending(pVM, pVCpuDst);
683#endif
684 }
685 STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtualSync);
686 LogFlow(("TMTimerPoll: expire2=%'RU64 <= now=%'RU64\n", u64Expire2, u64Now));
687 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
688 }
689
690 /*
691 * Return the time left to the next event.
692 */
693 STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
694 if (pVCpu == pVCpuDst)
695 {
696 if (fCatchUp)
697 i64Delta2 = ASMMultU64ByU32DivByU32(i64Delta2, 100, u32Pct + 100);
698 return tmTimerPollReturnMiss(pVM, u64Now, RT_MIN(i64Delta1, i64Delta2), pu64Delta);
699 }
700 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
701}
702
703
704/**
705 * Set FF if we've passed the next virtual event.
706 *
707 * This function is called before FFs are checked in the inner execution EM loops.
708 *
709 * @returns true if timers are pending, false if not.
710 *
711 * @param pVM Pointer to the shared VM structure.
712 * @param pVCpu Pointer to the shared VMCPU structure of the caller.
713 * @thread The emulation thread.
714 */
715VMMDECL(bool) TMTimerPollBool(PVM pVM, PVMCPU pVCpu)
716{
717 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
718 uint64_t off = 0;
719 tmTimerPollInternal(pVM, pVCpu, &off);
720 return off == 0;
721}
722
723
724/**
725 * Set FF if we've passed the next virtual event.
726 *
727 * This function is called before FFs are checked in the inner execution EM loops.
728 *
729 * @param pVM Pointer to the shared VM structure.
730 * @param pVCpu Pointer to the shared VMCPU structure of the caller.
731 * @thread The emulation thread.
732 */
733VMM_INT_DECL(void) TMTimerPollVoid(PVM pVM, PVMCPU pVCpu)
734{
735 uint64_t off;
736 tmTimerPollInternal(pVM, pVCpu, &off);
737}
738
739
740/**
741 * Set FF if we've passed the next virtual event.
742 *
743 * This function is called before FFs are checked in the inner execution EM loops.
744 *
745 * @returns The GIP timestamp of the next event.
746 * 0 if the next event has already expired.
747 * @param pVM Pointer to the shared VM structure.
748 * @param pVCpu Pointer to the shared VMCPU structure of the caller.
749 * @param pu64Delta Where to store the delta.
750 * @thread The emulation thread.
751 */
752VMM_INT_DECL(uint64_t) TMTimerPollGIP(PVM pVM, PVMCPU pVCpu, uint64_t *pu64Delta)
753{
754 return tmTimerPollInternal(pVM, pVCpu, pu64Delta);
755}
756
757#endif /* VBOX_HIGH_RES_TIMERS_HACK */
758
759/**
760 * Gets the host context ring-3 pointer of the timer.
761 *
762 * @returns HC R3 pointer.
763 * @param pTimer Timer handle as returned by one of the create functions.
764 */
765VMMDECL(PTMTIMERR3) TMTimerR3Ptr(PTMTIMER pTimer)
766{
767 return (PTMTIMERR3)MMHyperCCToR3(pTimer->CTX_SUFF(pVM), pTimer);
768}
769
770
771/**
772 * Gets the host context ring-0 pointer of the timer.
773 *
774 * @returns HC R0 pointer.
775 * @param pTimer Timer handle as returned by one of the create functions.
776 */
777VMMDECL(PTMTIMERR0) TMTimerR0Ptr(PTMTIMER pTimer)
778{
779 return (PTMTIMERR0)MMHyperCCToR0(pTimer->CTX_SUFF(pVM), pTimer);
780}
781
782
783/**
784 * Gets the RC pointer of the timer.
785 *
786 * @returns RC pointer.
787 * @param pTimer Timer handle as returned by one of the create functions.
788 */
789VMMDECL(PTMTIMERRC) TMTimerRCPtr(PTMTIMER pTimer)
790{
791 return (PTMTIMERRC)MMHyperCCToRC(pTimer->CTX_SUFF(pVM), pTimer);
792}
793
794
795/**
796 * Links a timer into the active list of a timer queue.
797 *
798 * The caller must have taken the TM semaphore before calling this function.
799 *
800 * @param pQueue The queue.
801 * @param pTimer The timer.
802 * @param u64Expire The timer expiration time.
803 */
804DECL_FORCE_INLINE(void) tmTimerActiveLink(PTMTIMERQUEUE pQueue, PTMTIMER pTimer, uint64_t u64Expire)
805{
806 PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue);
807 if (pCur)
808 {
809 for (;; pCur = TMTIMER_GET_NEXT(pCur))
810 {
811 if (pCur->u64Expire > u64Expire)
812 {
813 const PTMTIMER pPrev = TMTIMER_GET_PREV(pCur);
814 TMTIMER_SET_NEXT(pTimer, pCur);
815 TMTIMER_SET_PREV(pTimer, pPrev);
816 if (pPrev)
817 TMTIMER_SET_NEXT(pPrev, pTimer);
818 else
819 {
820 TMTIMER_SET_HEAD(pQueue, pTimer);
821 pQueue->u64Expire = u64Expire;
822 }
823 TMTIMER_SET_PREV(pCur, pTimer);
824 return;
825 }
826 if (!pCur->offNext)
827 {
828 TMTIMER_SET_NEXT(pCur, pTimer);
829 TMTIMER_SET_PREV(pTimer, pCur);
830 return;
831 }
832 }
833 }
834 else
835 {
836 TMTIMER_SET_HEAD(pQueue, pTimer);
837 pQueue->u64Expire = u64Expire;
838 }
839}
840
841
842/**
843 * Optimized TMTimerSet code path for starting an inactive timer.
844 *
845 * @returns VBox status code.
846 *
847 * @param pVM The VM handle.
848 * @param pTimer The timer handle.
849 * @param u64Expire The new expire time.
850 */
851static int tmTimerSetOptimizedStart(PVM pVM, PTMTIMER pTimer, uint64_t u64Expire)
852{
853 Assert(!pTimer->offPrev);
854 Assert(!pTimer->offNext);
855 Assert(pTimer->enmState == TMTIMERSTATE_ACTIVE);
856
857 /*
858 * Calculate and set the expiration time.
859 */
860 pTimer->u64Expire = u64Expire;
861 Log2(("tmTimerSetOptimizedStart: %p:{.pszDesc='%s', .u64Expire=%'RU64}\n", pTimer, R3STRING(pTimer->pszDesc), u64Expire));
862
863 /*
864 * Link the timer into the active list.
865 */
866 TMCLOCK const enmClock = pTimer->enmClock;
867 tmTimerActiveLink(&pVM->tm.s.CTX_SUFF(paTimerQueues)[enmClock], pTimer, u64Expire);
868
869 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetOpt);
870 tmTimerUnlock(pVM);
871 return VINF_SUCCESS;
872}
873
874
875
876
877
878/**
879 * Arm a timer with a (new) expire time.
880 *
881 * @returns VBox status.
882 * @param pTimer Timer handle as returned by one of the create functions.
883 * @param u64Expire New expire time.
884 */
885VMMDECL(int) TMTimerSet(PTMTIMER pTimer, uint64_t u64Expire)
886{
887 PVM pVM = pTimer->CTX_SUFF(pVM);
888 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
889 TMTIMER_ASSERT_CRITSECT(pTimer);
890
891#ifdef VBOX_WITH_STATISTICS
892 /* Gather optimization info. */
893 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSet);
894 TMTIMERSTATE enmOrgState = pTimer->enmState;
895 switch (enmOrgState)
896 {
897 case TMTIMERSTATE_STOPPED: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStStopped); break;
898 case TMTIMERSTATE_EXPIRED_DELIVER: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStExpDeliver); break;
899 case TMTIMERSTATE_ACTIVE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStActive); break;
900 case TMTIMERSTATE_PENDING_STOP: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendStop); break;
901 case TMTIMERSTATE_PENDING_STOP_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendStopSched); break;
902 case TMTIMERSTATE_PENDING_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendSched); break;
903 case TMTIMERSTATE_PENDING_RESCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendResched); break;
904 default: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStOther); break;
905 }
906#endif
907
908 /*
909 * The most common case is setting the timer again during the callback.
910 * The second most common case is starting a timer at some other time.
911 */
912#if 1
913 TMTIMERSTATE enmState1 = pTimer->enmState;
914 if ( enmState1 == TMTIMERSTATE_EXPIRED_DELIVER
915 || ( enmState1 == TMTIMERSTATE_STOPPED
916 && pTimer->pCritSect))
917 {
918 /* Try take the TM lock and check the state again. */
919 if (RT_SUCCESS_NP(tmTimerTryLock(pVM)))
920 {
921 if (RT_LIKELY(tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, enmState1)))
922 {
923 tmTimerSetOptimizedStart(pVM, pTimer, u64Expire);
924 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
925 return VINF_SUCCESS;
926 }
927 tmTimerUnlock(pVM);
928 }
929 }
930#endif
931
932 /*
933 * Unoptimized code path.
934 */
935 int cRetries = 1000;
936 do
937 {
938 /*
939 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
940 */
941 TMTIMERSTATE enmState = pTimer->enmState;
942 Log2(("TMTimerSet: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d u64Expire=%'RU64\n",
943 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries, u64Expire));
944 switch (enmState)
945 {
946 case TMTIMERSTATE_EXPIRED_DELIVER:
947 case TMTIMERSTATE_STOPPED:
948 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
949 {
950 Assert(!pTimer->offPrev);
951 Assert(!pTimer->offNext);
952 AssertMsg( pTimer->enmClock != TMCLOCK_VIRTUAL_SYNC
953 || pVM->tm.s.fVirtualSyncTicking
954 || u64Expire >= pVM->tm.s.u64VirtualSync,
955 ("%'RU64 < %'RU64 %s\n", u64Expire, pVM->tm.s.u64VirtualSync, R3STRING(pTimer->pszDesc)));
956 pTimer->u64Expire = u64Expire;
957 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
958 tmSchedule(pTimer);
959 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
960 return VINF_SUCCESS;
961 }
962 break;
963
964 case TMTIMERSTATE_PENDING_SCHEDULE:
965 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
966 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
967 {
968 pTimer->u64Expire = u64Expire;
969 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
970 tmSchedule(pTimer);
971 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
972 return VINF_SUCCESS;
973 }
974 break;
975
976
977 case TMTIMERSTATE_ACTIVE:
978 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
979 {
980 pTimer->u64Expire = u64Expire;
981 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
982 tmSchedule(pTimer);
983 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
984 return VINF_SUCCESS;
985 }
986 break;
987
988 case TMTIMERSTATE_PENDING_RESCHEDULE:
989 case TMTIMERSTATE_PENDING_STOP:
990 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
991 {
992 pTimer->u64Expire = u64Expire;
993 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
994 tmSchedule(pTimer);
995 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
996 return VINF_SUCCESS;
997 }
998 break;
999
1000
1001 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1002 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1003 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1004#ifdef IN_RING3
1005 if (!RTThreadYield())
1006 RTThreadSleep(1);
1007#else
1008/** @todo call host context and yield after a couple of iterations */
1009#endif
1010 break;
1011
1012 /*
1013 * Invalid states.
1014 */
1015 case TMTIMERSTATE_DESTROY:
1016 case TMTIMERSTATE_FREE:
1017 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1018 return VERR_TM_INVALID_STATE;
1019 default:
1020 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1021 return VERR_TM_UNKNOWN_STATE;
1022 }
1023 } while (cRetries-- > 0);
1024
1025 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
1026 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
1027 return VERR_INTERNAL_ERROR;
1028}
1029
1030
1031/**
1032 * Return the current time for the specified clock, setting pu64Now if not NULL.
1033 *
1034 * @returns Current time.
1035 * @param pVM The VM handle.
1036 * @param enmClock The clock to query.
1037 * @param pu64Now Optional pointer where to store the return time
1038 */
1039DECL_FORCE_INLINE(uint64_t) tmTimerSetRelativeNowWorker(PVM pVM, TMCLOCK enmClock, uint64_t *pu64Now)
1040{
1041 uint64_t u64Now;
1042 switch (enmClock)
1043 {
1044 case TMCLOCK_VIRTUAL_SYNC:
1045 u64Now = TMVirtualSyncGet(pVM);
1046 break;
1047 case TMCLOCK_VIRTUAL:
1048 u64Now = TMVirtualGet(pVM);
1049 break;
1050 case TMCLOCK_REAL:
1051 u64Now = TMRealGet(pVM);
1052 break;
1053 default:
1054 AssertFatalMsgFailed(("%d\n", enmClock));
1055 }
1056
1057 if (pu64Now)
1058 *pu64Now = u64Now;
1059 return u64Now;
1060}
1061
1062
1063/**
1064 * Optimized TMTimerSetRelative code path.
1065 *
1066 * @returns VBox status code.
1067 *
1068 * @param pVM The VM handle.
1069 * @param pTimer The timer handle.
1070 * @param cTicksToNext Clock ticks until the next time expiration.
1071 * @param pu64Now Where to return the current time stamp used.
1072 * Optional.
1073 */
1074static int tmTimerSetRelativeOptimizedStart(PVM pVM, PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now)
1075{
1076 Assert(!pTimer->offPrev);
1077 Assert(!pTimer->offNext);
1078 Assert(pTimer->enmState == TMTIMERSTATE_ACTIVE);
1079
1080 /*
1081 * Calculate and set the expiration time.
1082 */
1083 TMCLOCK const enmClock = pTimer->enmClock;
1084 uint64_t const u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
1085 pTimer->u64Expire = u64Expire;
1086 Log2(("tmTimerSetRelativeOptimizedStart: %p:{.pszDesc='%s', .u64Expire=%'RU64} cTicksToNext=%'RU64\n", pTimer, R3STRING(pTimer->pszDesc), u64Expire, cTicksToNext));
1087
1088 /*
1089 * Link the timer into the active list.
1090 */
1091 tmTimerActiveLink(&pVM->tm.s.CTX_SUFF(paTimerQueues)[enmClock], pTimer, u64Expire);
1092
1093 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeOpt);
1094 tmTimerUnlock(pVM);
1095 return VINF_SUCCESS;
1096}
1097
1098
1099/**
1100 * Arm a timer with a expire time relative to the current time.
1101 *
1102 * @returns VBox status.
1103 * @param pTimer Timer handle as returned by one of the create functions.
1104 * @param cTicksToNext Clock ticks until the next time expiration.
1105 * @param pu64Now Where to return the current time stamp used.
1106 * Optional.
1107 */
1108VMMDECL(int) TMTimerSetRelative(PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now)
1109{
1110 STAM_PROFILE_START(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
1111 TMTIMER_ASSERT_CRITSECT(pTimer);
1112 PVM pVM = pTimer->CTX_SUFF(pVM);
1113 int rc;
1114
1115#ifdef VBOX_WITH_STATISTICS
1116 /* Gather optimization info. */
1117 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelative);
1118 TMTIMERSTATE enmOrgState = pTimer->enmState;
1119 switch (enmOrgState)
1120 {
1121 case TMTIMERSTATE_STOPPED: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStStopped); break;
1122 case TMTIMERSTATE_EXPIRED_DELIVER: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStExpDeliver); break;
1123 case TMTIMERSTATE_ACTIVE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStActive); break;
1124 case TMTIMERSTATE_PENDING_STOP: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendStop); break;
1125 case TMTIMERSTATE_PENDING_STOP_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendStopSched); break;
1126 case TMTIMERSTATE_PENDING_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendSched); break;
1127 case TMTIMERSTATE_PENDING_RESCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendResched); break;
1128 default: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStOther); break;
1129 }
1130#endif
1131
1132 /*
1133 * Try to take the TM lock and optimize the common cases.
1134 *
1135 * With the TM lock we can safely make optimizations like immediate
1136 * scheduling and we can also be 100% sure that we're not racing the
1137 * running of the timer queues. As an additional restraint we require the
1138 * timer to have a critical section associated with to be 100% there aren't
1139 * concurrent operations on the timer. (This latter isn't necessary any
1140 * longer as this isn't supported for any timers, critsect or not.)
1141 *
1142 * Note! Lock ordering doesn't apply when we only tries to
1143 * get the innermost locks.
1144 */
1145 bool fOwnTMLock = RT_SUCCESS_NP(tmTimerTryLock(pVM));
1146#if 1
1147 if ( fOwnTMLock
1148 && pTimer->pCritSect)
1149 {
1150 TMTIMERSTATE enmState = pTimer->enmState;
1151 if (RT_LIKELY( ( enmState == TMTIMERSTATE_EXPIRED_DELIVER
1152 || enmState == TMTIMERSTATE_STOPPED)
1153 && tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, enmState)))
1154 {
1155 tmTimerSetRelativeOptimizedStart(pVM, pTimer, cTicksToNext, pu64Now);
1156 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
1157 return VINF_SUCCESS;
1158 }
1159
1160 /* Optimize other states when it becomes necessary. */
1161 }
1162#endif
1163
1164 /*
1165 * Unoptimized path.
1166 */
1167 TMCLOCK const enmClock = pTimer->enmClock;
1168 bool fOwnVirtSyncLock;
1169 fOwnVirtSyncLock = !fOwnTMLock
1170 && enmClock == TMCLOCK_VIRTUAL_SYNC
1171 && RT_SUCCESS(tmVirtualSyncTryLock(pVM));
1172 for (int cRetries = 1000; ; cRetries--)
1173 {
1174 /*
1175 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
1176 */
1177 TMTIMERSTATE enmState = pTimer->enmState;
1178 switch (enmState)
1179 {
1180 case TMTIMERSTATE_STOPPED:
1181 if (enmClock == TMCLOCK_VIRTUAL_SYNC)
1182 {
1183 /** @todo To fix assertion in tmR3TimerQueueRunVirtualSync:
1184 * Figure a safe way of activating this timer while the queue is
1185 * being run.
1186 * (99.9% sure this that the assertion is caused by DevAPIC.cpp
1187 * re-starting the timer in respons to a initial_count write.) */
1188 }
1189 /* fall thru */
1190 case TMTIMERSTATE_EXPIRED_DELIVER:
1191 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
1192 {
1193 Assert(!pTimer->offPrev);
1194 Assert(!pTimer->offNext);
1195 pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
1196 Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [EXP/STOP]\n",
1197 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), pTimer->u64Expire, cRetries));
1198 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
1199 tmSchedule(pTimer);
1200 rc = VINF_SUCCESS;
1201 break;
1202 }
1203 rc = VERR_TRY_AGAIN;
1204 break;
1205
1206 case TMTIMERSTATE_PENDING_SCHEDULE:
1207 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1208 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
1209 {
1210 pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
1211 Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [PEND_SCHED]\n",
1212 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), pTimer->u64Expire, cRetries));
1213 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
1214 tmSchedule(pTimer);
1215 rc = VINF_SUCCESS;
1216 break;
1217 }
1218 rc = VERR_TRY_AGAIN;
1219 break;
1220
1221
1222 case TMTIMERSTATE_ACTIVE:
1223 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
1224 {
1225 pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
1226 Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [ACTIVE]\n",
1227 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), pTimer->u64Expire, cRetries));
1228 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
1229 tmSchedule(pTimer);
1230 rc = VINF_SUCCESS;
1231 break;
1232 }
1233 rc = VERR_TRY_AGAIN;
1234 break;
1235
1236 case TMTIMERSTATE_PENDING_RESCHEDULE:
1237 case TMTIMERSTATE_PENDING_STOP:
1238 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
1239 {
1240 pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
1241 Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [PEND_RESCH/STOP]\n",
1242 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), pTimer->u64Expire, cRetries));
1243 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
1244 tmSchedule(pTimer);
1245 rc = VINF_SUCCESS;
1246 break;
1247 }
1248 rc = VERR_TRY_AGAIN;
1249 break;
1250
1251
1252 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1253 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1254 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1255#ifdef IN_RING3
1256 if (!RTThreadYield())
1257 RTThreadSleep(1);
1258#else
1259/** @todo call host context and yield after a couple of iterations */
1260#endif
1261 rc = VERR_TRY_AGAIN;
1262 break;
1263
1264 /*
1265 * Invalid states.
1266 */
1267 case TMTIMERSTATE_DESTROY:
1268 case TMTIMERSTATE_FREE:
1269 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1270 rc = VERR_TM_INVALID_STATE;
1271 break;
1272
1273 default:
1274 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1275 rc = VERR_TM_UNKNOWN_STATE;
1276 break;
1277 }
1278
1279 /* switch + loop is tedious to break out of. */
1280 if (rc == VINF_SUCCESS)
1281 break;
1282
1283 if (rc != VERR_TRY_AGAIN)
1284 {
1285 tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
1286 break;
1287 }
1288 if (cRetries <= 0)
1289 {
1290 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
1291 rc = VERR_INTERNAL_ERROR;
1292 tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
1293 break;
1294 }
1295
1296 /*
1297 * Retry to gain locks.
1298 */
1299 if (!fOwnTMLock)
1300 {
1301 fOwnTMLock = RT_SUCCESS_NP(tmTimerTryLock(pVM));
1302 if ( !fOwnTMLock
1303 && enmClock == TMCLOCK_VIRTUAL_SYNC
1304 && !fOwnVirtSyncLock)
1305 fOwnVirtSyncLock = RT_SUCCESS_NP(tmVirtualSyncTryLock(pVM));
1306 }
1307
1308 } /* for (;;) */
1309
1310 /*
1311 * Clean up and return.
1312 */
1313 if (fOwnVirtSyncLock)
1314 tmVirtualSyncUnlock(pVM);
1315 if (fOwnTMLock)
1316 tmTimerUnlock(pVM);
1317
1318 if ( !fOwnTMLock
1319 && !fOwnVirtSyncLock
1320 && enmClock == TMCLOCK_VIRTUAL_SYNC)
1321 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeRacyVirtSync);
1322
1323 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
1324 return rc;
1325}
1326
1327
1328/**
1329 * Arm a timer with a (new) expire time relative to current time.
1330 *
1331 * @returns VBox status.
1332 * @param pTimer Timer handle as returned by one of the create functions.
1333 * @param cMilliesToNext Number of millieseconds to the next tick.
1334 */
1335VMMDECL(int) TMTimerSetMillies(PTMTIMER pTimer, uint32_t cMilliesToNext)
1336{
1337 PVM pVM = pTimer->CTX_SUFF(pVM);
1338 PVMCPU pVCpu = &pVM->aCpus[0]; /* just take the first VCPU */
1339
1340 switch (pTimer->enmClock)
1341 {
1342 case TMCLOCK_VIRTUAL:
1343 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1344 return TMTimerSetRelative(pTimer, cMilliesToNext * UINT64_C(1000000), NULL);
1345
1346 case TMCLOCK_VIRTUAL_SYNC:
1347 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1348 return TMTimerSetRelative(pTimer, cMilliesToNext * UINT64_C(1000000), NULL);
1349
1350 case TMCLOCK_REAL:
1351 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1352 return TMTimerSetRelative(pTimer, cMilliesToNext, NULL);
1353
1354 default:
1355 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1356 return VERR_INTERNAL_ERROR;
1357 }
1358}
1359
1360
1361/**
1362 * Arm a timer with a (new) expire time relative to current time.
1363 *
1364 * @returns VBox status.
1365 * @param pTimer Timer handle as returned by one of the create functions.
1366 * @param cMicrosToNext Number of microseconds to the next tick.
1367 */
1368VMMDECL(int) TMTimerSetMicro(PTMTIMER pTimer, uint64_t cMicrosToNext)
1369{
1370 PVM pVM = pTimer->CTX_SUFF(pVM);
1371 PVMCPU pVCpu = &pVM->aCpus[0]; /* just take the first VCPU */
1372
1373 switch (pTimer->enmClock)
1374 {
1375 case TMCLOCK_VIRTUAL:
1376 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1377 return TMTimerSetRelative(pTimer, cMicrosToNext * 1000, NULL);
1378
1379 case TMCLOCK_VIRTUAL_SYNC:
1380 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1381 return TMTimerSetRelative(pTimer, cMicrosToNext * 1000, NULL);
1382
1383 case TMCLOCK_REAL:
1384 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1385 return TMTimerSetRelative(pTimer, cMicrosToNext / 1000, NULL);
1386
1387 default:
1388 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1389 return VERR_INTERNAL_ERROR;
1390 }
1391}
1392
1393
1394/**
1395 * Arm a timer with a (new) expire time relative to current time.
1396 *
1397 * @returns VBox status.
1398 * @param pTimer Timer handle as returned by one of the create functions.
1399 * @param cNanosToNext Number of nanoseconds to the next tick.
1400 */
1401VMMDECL(int) TMTimerSetNano(PTMTIMER pTimer, uint64_t cNanosToNext)
1402{
1403 PVM pVM = pTimer->CTX_SUFF(pVM);
1404 PVMCPU pVCpu = &pVM->aCpus[0]; /* just take the first VCPU */
1405
1406 switch (pTimer->enmClock)
1407 {
1408 case TMCLOCK_VIRTUAL:
1409 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1410 return TMTimerSetRelative(pTimer, cNanosToNext, NULL);
1411
1412 case TMCLOCK_VIRTUAL_SYNC:
1413 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1414 return TMTimerSetRelative(pTimer, cNanosToNext, NULL);
1415
1416 case TMCLOCK_REAL:
1417 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1418 return TMTimerSetRelative(pTimer, cNanosToNext / 1000000, NULL);
1419
1420 default:
1421 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1422 return VERR_INTERNAL_ERROR;
1423 }
1424}
1425
1426
1427/**
1428 * Stop the timer.
1429 * Use TMR3TimerArm() to "un-stop" the timer.
1430 *
1431 * @returns VBox status.
1432 * @param pTimer Timer handle as returned by one of the create functions.
1433 */
1434VMMDECL(int) TMTimerStop(PTMTIMER pTimer)
1435{
1436 STAM_PROFILE_START(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
1437 TMTIMER_ASSERT_CRITSECT(pTimer);
1438
1439 /** @todo see if this function needs optimizing. */
1440 int cRetries = 1000;
1441 do
1442 {
1443 /*
1444 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
1445 */
1446 TMTIMERSTATE enmState = pTimer->enmState;
1447 Log2(("TMTimerStop: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
1448 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries));
1449 switch (enmState)
1450 {
1451 case TMTIMERSTATE_EXPIRED_DELIVER:
1452 //AssertMsgFailed(("You don't stop an expired timer dude!\n"));
1453 return VERR_INVALID_PARAMETER;
1454
1455 case TMTIMERSTATE_STOPPED:
1456 case TMTIMERSTATE_PENDING_STOP:
1457 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1458 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
1459 return VINF_SUCCESS;
1460
1461 case TMTIMERSTATE_PENDING_SCHEDULE:
1462 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, enmState))
1463 {
1464 tmSchedule(pTimer);
1465 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
1466 return VINF_SUCCESS;
1467 }
1468
1469 case TMTIMERSTATE_PENDING_RESCHEDULE:
1470 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
1471 {
1472 tmSchedule(pTimer);
1473 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
1474 return VINF_SUCCESS;
1475 }
1476 break;
1477
1478 case TMTIMERSTATE_ACTIVE:
1479 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
1480 {
1481 tmSchedule(pTimer);
1482 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
1483 return VINF_SUCCESS;
1484 }
1485 break;
1486
1487 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1488 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1489 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1490#ifdef IN_RING3
1491 if (!RTThreadYield())
1492 RTThreadSleep(1);
1493#else
1494/**@todo call host and yield cpu after a while. */
1495#endif
1496 break;
1497
1498 /*
1499 * Invalid states.
1500 */
1501 case TMTIMERSTATE_DESTROY:
1502 case TMTIMERSTATE_FREE:
1503 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1504 return VERR_TM_INVALID_STATE;
1505 default:
1506 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1507 return VERR_TM_UNKNOWN_STATE;
1508 }
1509 } while (cRetries-- > 0);
1510
1511 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
1512 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
1513 return VERR_INTERNAL_ERROR;
1514}
1515
1516
1517/**
1518 * Get the current clock time.
1519 * Handy for calculating the new expire time.
1520 *
1521 * @returns Current clock time.
1522 * @param pTimer Timer handle as returned by one of the create functions.
1523 */
1524VMMDECL(uint64_t) TMTimerGet(PTMTIMER pTimer)
1525{
1526 uint64_t u64;
1527 PVM pVM = pTimer->CTX_SUFF(pVM);
1528
1529 switch (pTimer->enmClock)
1530 {
1531 case TMCLOCK_VIRTUAL:
1532 u64 = TMVirtualGet(pVM);
1533 break;
1534 case TMCLOCK_VIRTUAL_SYNC:
1535 u64 = TMVirtualSyncGet(pVM);
1536 break;
1537 case TMCLOCK_REAL:
1538 u64 = TMRealGet(pVM);
1539 break;
1540 default:
1541 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1542 return ~(uint64_t)0;
1543 }
1544 //Log2(("TMTimerGet: returns %'RU64 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1545 // u64, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1546 return u64;
1547}
1548
1549
1550/**
1551 * Get the freqency of the timer clock.
1552 *
1553 * @returns Clock frequency (as Hz of course).
1554 * @param pTimer Timer handle as returned by one of the create functions.
1555 */
1556VMMDECL(uint64_t) TMTimerGetFreq(PTMTIMER pTimer)
1557{
1558 switch (pTimer->enmClock)
1559 {
1560 case TMCLOCK_VIRTUAL:
1561 case TMCLOCK_VIRTUAL_SYNC:
1562 return TMCLOCK_FREQ_VIRTUAL;
1563
1564 case TMCLOCK_REAL:
1565 return TMCLOCK_FREQ_REAL;
1566
1567 default:
1568 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1569 return 0;
1570 }
1571}
1572
1573
1574/**
1575 * Get the current clock time as nanoseconds.
1576 *
1577 * @returns The timer clock as nanoseconds.
1578 * @param pTimer Timer handle as returned by one of the create functions.
1579 */
1580VMMDECL(uint64_t) TMTimerGetNano(PTMTIMER pTimer)
1581{
1582 return TMTimerToNano(pTimer, TMTimerGet(pTimer));
1583}
1584
1585
1586/**
1587 * Get the current clock time as microseconds.
1588 *
1589 * @returns The timer clock as microseconds.
1590 * @param pTimer Timer handle as returned by one of the create functions.
1591 */
1592VMMDECL(uint64_t) TMTimerGetMicro(PTMTIMER pTimer)
1593{
1594 return TMTimerToMicro(pTimer, TMTimerGet(pTimer));
1595}
1596
1597
1598/**
1599 * Get the current clock time as milliseconds.
1600 *
1601 * @returns The timer clock as milliseconds.
1602 * @param pTimer Timer handle as returned by one of the create functions.
1603 */
1604VMMDECL(uint64_t) TMTimerGetMilli(PTMTIMER pTimer)
1605{
1606 return TMTimerToMilli(pTimer, TMTimerGet(pTimer));
1607}
1608
1609
1610/**
1611 * Converts the specified timer clock time to nanoseconds.
1612 *
1613 * @returns nanoseconds.
1614 * @param pTimer Timer handle as returned by one of the create functions.
1615 * @param u64Ticks The clock ticks.
1616 * @remark There could be rounding errors here. We just do a simple integere divide
1617 * without any adjustments.
1618 */
1619VMMDECL(uint64_t) TMTimerToNano(PTMTIMER pTimer, uint64_t u64Ticks)
1620{
1621 switch (pTimer->enmClock)
1622 {
1623 case TMCLOCK_VIRTUAL:
1624 case TMCLOCK_VIRTUAL_SYNC:
1625 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1626 return u64Ticks;
1627
1628 case TMCLOCK_REAL:
1629 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1630 return u64Ticks * 1000000;
1631
1632 default:
1633 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1634 return 0;
1635 }
1636}
1637
1638
1639/**
1640 * Converts the specified timer clock time to microseconds.
1641 *
1642 * @returns microseconds.
1643 * @param pTimer Timer handle as returned by one of the create functions.
1644 * @param u64Ticks The clock ticks.
1645 * @remark There could be rounding errors here. We just do a simple integere divide
1646 * without any adjustments.
1647 */
1648VMMDECL(uint64_t) TMTimerToMicro(PTMTIMER pTimer, uint64_t u64Ticks)
1649{
1650 switch (pTimer->enmClock)
1651 {
1652 case TMCLOCK_VIRTUAL:
1653 case TMCLOCK_VIRTUAL_SYNC:
1654 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1655 return u64Ticks / 1000;
1656
1657 case TMCLOCK_REAL:
1658 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1659 return u64Ticks * 1000;
1660
1661 default:
1662 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1663 return 0;
1664 }
1665}
1666
1667
1668/**
1669 * Converts the specified timer clock time to milliseconds.
1670 *
1671 * @returns milliseconds.
1672 * @param pTimer Timer handle as returned by one of the create functions.
1673 * @param u64Ticks The clock ticks.
1674 * @remark There could be rounding errors here. We just do a simple integere divide
1675 * without any adjustments.
1676 */
1677VMMDECL(uint64_t) TMTimerToMilli(PTMTIMER pTimer, uint64_t u64Ticks)
1678{
1679 switch (pTimer->enmClock)
1680 {
1681 case TMCLOCK_VIRTUAL:
1682 case TMCLOCK_VIRTUAL_SYNC:
1683 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1684 return u64Ticks / 1000000;
1685
1686 case TMCLOCK_REAL:
1687 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1688 return u64Ticks;
1689
1690 default:
1691 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1692 return 0;
1693 }
1694}
1695
1696
1697/**
1698 * Converts the specified nanosecond timestamp to timer clock ticks.
1699 *
1700 * @returns timer clock ticks.
1701 * @param pTimer Timer handle as returned by one of the create functions.
1702 * @param u64NanoTS The nanosecond value ticks to convert.
1703 * @remark There could be rounding and overflow errors here.
1704 */
1705VMMDECL(uint64_t) TMTimerFromNano(PTMTIMER pTimer, uint64_t u64NanoTS)
1706{
1707 switch (pTimer->enmClock)
1708 {
1709 case TMCLOCK_VIRTUAL:
1710 case TMCLOCK_VIRTUAL_SYNC:
1711 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1712 return u64NanoTS;
1713
1714 case TMCLOCK_REAL:
1715 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1716 return u64NanoTS / 1000000;
1717
1718 default:
1719 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1720 return 0;
1721 }
1722}
1723
1724
1725/**
1726 * Converts the specified microsecond timestamp to timer clock ticks.
1727 *
1728 * @returns timer clock ticks.
1729 * @param pTimer Timer handle as returned by one of the create functions.
1730 * @param u64MicroTS The microsecond value ticks to convert.
1731 * @remark There could be rounding and overflow errors here.
1732 */
1733VMMDECL(uint64_t) TMTimerFromMicro(PTMTIMER pTimer, uint64_t u64MicroTS)
1734{
1735 switch (pTimer->enmClock)
1736 {
1737 case TMCLOCK_VIRTUAL:
1738 case TMCLOCK_VIRTUAL_SYNC:
1739 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1740 return u64MicroTS * 1000;
1741
1742 case TMCLOCK_REAL:
1743 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1744 return u64MicroTS / 1000;
1745
1746 default:
1747 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1748 return 0;
1749 }
1750}
1751
1752
1753/**
1754 * Converts the specified millisecond timestamp to timer clock ticks.
1755 *
1756 * @returns timer clock ticks.
1757 * @param pTimer Timer handle as returned by one of the create functions.
1758 * @param u64MilliTS The millisecond value ticks to convert.
1759 * @remark There could be rounding and overflow errors here.
1760 */
1761VMMDECL(uint64_t) TMTimerFromMilli(PTMTIMER pTimer, uint64_t u64MilliTS)
1762{
1763 switch (pTimer->enmClock)
1764 {
1765 case TMCLOCK_VIRTUAL:
1766 case TMCLOCK_VIRTUAL_SYNC:
1767 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1768 return u64MilliTS * 1000000;
1769
1770 case TMCLOCK_REAL:
1771 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1772 return u64MilliTS;
1773
1774 default:
1775 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1776 return 0;
1777 }
1778}
1779
1780
1781/**
1782 * Get the expire time of the timer.
1783 * Only valid for active timers.
1784 *
1785 * @returns Expire time of the timer.
1786 * @param pTimer Timer handle as returned by one of the create functions.
1787 */
1788VMMDECL(uint64_t) TMTimerGetExpire(PTMTIMER pTimer)
1789{
1790 TMTIMER_ASSERT_CRITSECT(pTimer);
1791 int cRetries = 1000;
1792 do
1793 {
1794 TMTIMERSTATE enmState = pTimer->enmState;
1795 switch (enmState)
1796 {
1797 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1798 case TMTIMERSTATE_EXPIRED_DELIVER:
1799 case TMTIMERSTATE_STOPPED:
1800 case TMTIMERSTATE_PENDING_STOP:
1801 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1802 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1803 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1804 return ~(uint64_t)0;
1805
1806 case TMTIMERSTATE_ACTIVE:
1807 case TMTIMERSTATE_PENDING_RESCHEDULE:
1808 case TMTIMERSTATE_PENDING_SCHEDULE:
1809 Log2(("TMTimerGetExpire: returns %'RU64 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1810 pTimer->u64Expire, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1811 return pTimer->u64Expire;
1812
1813 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1814 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1815#ifdef IN_RING3
1816 if (!RTThreadYield())
1817 RTThreadSleep(1);
1818#endif
1819 break;
1820
1821 /*
1822 * Invalid states.
1823 */
1824 case TMTIMERSTATE_DESTROY:
1825 case TMTIMERSTATE_FREE:
1826 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1827 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1828 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1829 return ~(uint64_t)0;
1830 default:
1831 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1832 return ~(uint64_t)0;
1833 }
1834 } while (cRetries-- > 0);
1835
1836 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
1837 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1838 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1839 return ~(uint64_t)0;
1840}
1841
1842
1843/**
1844 * Checks if a timer is active or not.
1845 *
1846 * @returns True if active.
1847 * @returns False if not active.
1848 * @param pTimer Timer handle as returned by one of the create functions.
1849 */
1850VMMDECL(bool) TMTimerIsActive(PTMTIMER pTimer)
1851{
1852 TMTIMERSTATE enmState = pTimer->enmState;
1853 switch (enmState)
1854 {
1855 case TMTIMERSTATE_STOPPED:
1856 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1857 case TMTIMERSTATE_EXPIRED_DELIVER:
1858 case TMTIMERSTATE_PENDING_STOP:
1859 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1860 Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1861 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1862 return false;
1863
1864 case TMTIMERSTATE_ACTIVE:
1865 case TMTIMERSTATE_PENDING_RESCHEDULE:
1866 case TMTIMERSTATE_PENDING_SCHEDULE:
1867 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1868 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1869 Log2(("TMTimerIsActive: returns true (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1870 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1871 return true;
1872
1873 /*
1874 * Invalid states.
1875 */
1876 case TMTIMERSTATE_DESTROY:
1877 case TMTIMERSTATE_FREE:
1878 AssertMsgFailed(("Invalid timer state %s (%s)\n", tmTimerState(enmState), R3STRING(pTimer->pszDesc)));
1879 Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1880 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1881 return false;
1882 default:
1883 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1884 return false;
1885 }
1886}
1887
1888
1889/**
1890 * Convert state to string.
1891 *
1892 * @returns Readonly status name.
1893 * @param enmState State.
1894 */
1895const char *tmTimerState(TMTIMERSTATE enmState)
1896{
1897 switch (enmState)
1898 {
1899#define CASE(num, state) \
1900 case TMTIMERSTATE_##state: \
1901 AssertCompile(TMTIMERSTATE_##state == (num)); \
1902 return #num "-" #state
1903 CASE( 1,STOPPED);
1904 CASE( 2,ACTIVE);
1905 CASE( 3,EXPIRED_GET_UNLINK);
1906 CASE( 4,EXPIRED_DELIVER);
1907 CASE( 5,PENDING_STOP);
1908 CASE( 6,PENDING_STOP_SCHEDULE);
1909 CASE( 7,PENDING_SCHEDULE_SET_EXPIRE);
1910 CASE( 8,PENDING_SCHEDULE);
1911 CASE( 9,PENDING_RESCHEDULE_SET_EXPIRE);
1912 CASE(10,PENDING_RESCHEDULE);
1913 CASE(11,DESTROY);
1914 CASE(12,FREE);
1915 default:
1916 AssertMsgFailed(("Invalid state enmState=%d\n", enmState));
1917 return "Invalid state!";
1918#undef CASE
1919 }
1920}
1921
1922
1923/**
1924 * Schedules the given timer on the given queue.
1925 *
1926 * @param pQueue The timer queue.
1927 * @param pTimer The timer that needs scheduling.
1928 *
1929 * @remarks Called while owning the lock.
1930 */
1931DECLINLINE(void) tmTimerQueueScheduleOne(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
1932{
1933 /*
1934 * Processing.
1935 */
1936 unsigned cRetries = 2;
1937 do
1938 {
1939 TMTIMERSTATE enmState = pTimer->enmState;
1940 switch (enmState)
1941 {
1942 /*
1943 * Reschedule timer (in the active list).
1944 */
1945 case TMTIMERSTATE_PENDING_RESCHEDULE:
1946 {
1947 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE, TMTIMERSTATE_PENDING_RESCHEDULE)))
1948 break; /* retry */
1949
1950 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1951 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
1952 if (pPrev)
1953 TMTIMER_SET_NEXT(pPrev, pNext);
1954 else
1955 {
1956 TMTIMER_SET_HEAD(pQueue, pNext);
1957 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1958 }
1959 if (pNext)
1960 TMTIMER_SET_PREV(pNext, pPrev);
1961 pTimer->offNext = 0;
1962 pTimer->offPrev = 0;
1963 /* fall thru */
1964 }
1965
1966 /*
1967 * Schedule timer (insert into the active list).
1968 */
1969 case TMTIMERSTATE_PENDING_SCHEDULE:
1970 {
1971 Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
1972 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, TMTIMERSTATE_PENDING_SCHEDULE)))
1973 break; /* retry */
1974
1975 PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue);
1976 if (pCur)
1977 {
1978 const uint64_t u64Expire = pTimer->u64Expire;
1979 for (;; pCur = TMTIMER_GET_NEXT(pCur))
1980 {
1981 if (pCur->u64Expire > u64Expire)
1982 {
1983 const PTMTIMER pPrev = TMTIMER_GET_PREV(pCur);
1984 TMTIMER_SET_NEXT(pTimer, pCur);
1985 TMTIMER_SET_PREV(pTimer, pPrev);
1986 if (pPrev)
1987 TMTIMER_SET_NEXT(pPrev, pTimer);
1988 else
1989 {
1990 TMTIMER_SET_HEAD(pQueue, pTimer);
1991 pQueue->u64Expire = u64Expire;
1992 }
1993 TMTIMER_SET_PREV(pCur, pTimer);
1994 return;
1995 }
1996 if (!pCur->offNext)
1997 {
1998 TMTIMER_SET_NEXT(pCur, pTimer);
1999 TMTIMER_SET_PREV(pTimer, pCur);
2000 return;
2001 }
2002 }
2003 }
2004 else
2005 {
2006 TMTIMER_SET_HEAD(pQueue, pTimer);
2007 pQueue->u64Expire = pTimer->u64Expire;
2008 }
2009 return;
2010 }
2011
2012 /*
2013 * Stop the timer in active list.
2014 */
2015 case TMTIMERSTATE_PENDING_STOP:
2016 {
2017 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, TMTIMERSTATE_PENDING_STOP)))
2018 break; /* retry */
2019
2020 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
2021 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
2022 if (pPrev)
2023 TMTIMER_SET_NEXT(pPrev, pNext);
2024 else
2025 {
2026 TMTIMER_SET_HEAD(pQueue, pNext);
2027 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
2028 }
2029 if (pNext)
2030 TMTIMER_SET_PREV(pNext, pPrev);
2031 pTimer->offNext = 0;
2032 pTimer->offPrev = 0;
2033 /* fall thru */
2034 }
2035
2036 /*
2037 * Stop the timer (not on the active list).
2038 */
2039 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
2040 Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
2041 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_PENDING_STOP_SCHEDULE)))
2042 break;
2043 return;
2044
2045 /*
2046 * The timer is pending destruction by TMR3TimerDestroy, our caller.
2047 * Nothing to do here.
2048 */
2049 case TMTIMERSTATE_DESTROY:
2050 break;
2051
2052 /*
2053 * Postpone these until they get into the right state.
2054 */
2055 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2056 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
2057 tmTimerLink(pQueue, pTimer);
2058 STAM_COUNTER_INC(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatPostponed));
2059 return;
2060
2061 /*
2062 * None of these can be in the schedule.
2063 */
2064 case TMTIMERSTATE_FREE:
2065 case TMTIMERSTATE_STOPPED:
2066 case TMTIMERSTATE_ACTIVE:
2067 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
2068 case TMTIMERSTATE_EXPIRED_DELIVER:
2069 default:
2070 AssertMsgFailed(("Timer (%p) in the scheduling list has an invalid state %s (%d)!",
2071 pTimer, tmTimerState(pTimer->enmState), pTimer->enmState));
2072 return;
2073 }
2074 } while (cRetries-- > 0);
2075}
2076
2077
2078/**
2079 * Schedules the specified timer queue.
2080 *
2081 * @param pVM The VM to run the timers for.
2082 * @param pQueue The queue to schedule.
2083 *
2084 * @remarks Called while owning the lock.
2085 */
2086void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue)
2087{
2088 TM_ASSERT_LOCK(pVM);
2089
2090 /*
2091 * Dequeue the scheduling list and iterate it.
2092 */
2093 int32_t offNext = ASMAtomicXchgS32(&pQueue->offSchedule, 0);
2094 Log2(("tmTimerQueueSchedule: pQueue=%p:{.enmClock=%d, offNext=%RI32, .u64Expired=%'RU64}\n", pQueue, pQueue->enmClock, offNext, pQueue->u64Expire));
2095 if (!offNext)
2096 return;
2097 PTMTIMER pNext = (PTMTIMER)((intptr_t)pQueue + offNext);
2098 while (pNext)
2099 {
2100 /*
2101 * Unlink the head timer and find the next one.
2102 */
2103 PTMTIMER pTimer = pNext;
2104 pNext = pNext->offScheduleNext ? (PTMTIMER)((intptr_t)pNext + pNext->offScheduleNext) : NULL;
2105 pTimer->offScheduleNext = 0;
2106
2107 /*
2108 * Do the scheduling.
2109 */
2110 Log2(("tmTimerQueueSchedule: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, .pszDesc=%s}\n",
2111 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, R3STRING(pTimer->pszDesc)));
2112 tmTimerQueueScheduleOne(pQueue, pTimer);
2113 Log2(("tmTimerQueueSchedule: %p: new %s\n", pTimer, tmTimerState(pTimer->enmState)));
2114 } /* foreach timer in current schedule batch. */
2115 Log2(("tmTimerQueueSchedule: u64Expired=%'RU64\n", pQueue->u64Expire));
2116}
2117
2118
2119#ifdef VBOX_STRICT
2120/**
2121 * Checks that the timer queues are sane.
2122 *
2123 * @param pVM VM handle.
2124 *
2125 * @remarks Called while owning the lock.
2126 */
2127void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere)
2128{
2129 TM_ASSERT_LOCK(pVM);
2130
2131 /*
2132 * Check the linking of the active lists.
2133 */
2134 for (int i = 0; i < TMCLOCK_MAX; i++)
2135 {
2136 PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[i];
2137 Assert((int)pQueue->enmClock == i);
2138 PTMTIMER pPrev = NULL;
2139 for (PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue); pCur; pPrev = pCur, pCur = TMTIMER_GET_NEXT(pCur))
2140 {
2141 AssertMsg((int)pCur->enmClock == i, ("%s: %d != %d\n", pszWhere, pCur->enmClock, i));
2142 AssertMsg(TMTIMER_GET_PREV(pCur) == pPrev, ("%s: %p != %p\n", pszWhere, TMTIMER_GET_PREV(pCur), pPrev));
2143 TMTIMERSTATE enmState = pCur->enmState;
2144 switch (enmState)
2145 {
2146 case TMTIMERSTATE_ACTIVE:
2147 AssertMsg( !pCur->offScheduleNext
2148 || pCur->enmState != TMTIMERSTATE_ACTIVE,
2149 ("%s: %RI32\n", pszWhere, pCur->offScheduleNext));
2150 break;
2151 case TMTIMERSTATE_PENDING_STOP:
2152 case TMTIMERSTATE_PENDING_RESCHEDULE:
2153 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2154 break;
2155 default:
2156 AssertMsgFailed(("%s: Invalid state enmState=%d %s\n", pszWhere, enmState, tmTimerState(enmState)));
2157 break;
2158 }
2159 }
2160 }
2161
2162
2163# ifdef IN_RING3
2164 /*
2165 * Do the big list and check that active timers all are in the active lists.
2166 */
2167 PTMTIMERR3 pPrev = NULL;
2168 for (PTMTIMERR3 pCur = pVM->tm.s.pCreated; pCur; pPrev = pCur, pCur = pCur->pBigNext)
2169 {
2170 Assert(pCur->pBigPrev == pPrev);
2171 Assert((unsigned)pCur->enmClock < (unsigned)TMCLOCK_MAX);
2172
2173 TMTIMERSTATE enmState = pCur->enmState;
2174 switch (enmState)
2175 {
2176 case TMTIMERSTATE_ACTIVE:
2177 case TMTIMERSTATE_PENDING_STOP:
2178 case TMTIMERSTATE_PENDING_RESCHEDULE:
2179 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2180 {
2181 PTMTIMERR3 pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTX_SUFF(paTimerQueues)[pCur->enmClock]);
2182 Assert(pCur->offPrev || pCur == pCurAct);
2183 while (pCurAct && pCurAct != pCur)
2184 pCurAct = TMTIMER_GET_NEXT(pCurAct);
2185 Assert(pCurAct == pCur);
2186 break;
2187 }
2188
2189 case TMTIMERSTATE_PENDING_SCHEDULE:
2190 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
2191 case TMTIMERSTATE_STOPPED:
2192 case TMTIMERSTATE_EXPIRED_DELIVER:
2193 {
2194 Assert(!pCur->offNext);
2195 Assert(!pCur->offPrev);
2196 for (PTMTIMERR3 pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTX_SUFF(paTimerQueues)[pCur->enmClock]);
2197 pCurAct;
2198 pCurAct = TMTIMER_GET_NEXT(pCurAct))
2199 {
2200 Assert(pCurAct != pCur);
2201 Assert(TMTIMER_GET_NEXT(pCurAct) != pCur);
2202 Assert(TMTIMER_GET_PREV(pCurAct) != pCur);
2203 }
2204 break;
2205 }
2206
2207 /* ignore */
2208 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
2209 break;
2210
2211 /* shouldn't get here! */
2212 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
2213 case TMTIMERSTATE_DESTROY:
2214 default:
2215 AssertMsgFailed(("Invalid state enmState=%d %s\n", enmState, tmTimerState(enmState)));
2216 break;
2217 }
2218 }
2219# endif /* IN_RING3 */
2220}
2221#endif /* !VBOX_STRICT */
2222
2223
2224/**
2225 * Gets the current warp drive percent.
2226 *
2227 * @returns The warp drive percent.
2228 * @param pVM The VM handle.
2229 */
2230VMMDECL(uint32_t) TMGetWarpDrive(PVM pVM)
2231{
2232 return pVM->tm.s.u32VirtualWarpDrivePercentage;
2233}
2234
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