VirtualBox

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

Last change on this file since 19820 was 19820, checked in by vboxsync, 16 years ago

TM: Joined up the two poll functions and making TMTimerPollGIP lockless as well.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 57.9 KB
Line 
1/* $Id: TMAll.cpp 19820 2009-05-19 13:14:54Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, all contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_TM
27#include <VBox/tm.h>
28#include <VBox/mm.h>
29#ifdef IN_RING3
30# include <VBox/rem.h>
31#endif
32#include "TMInternal.h"
33#include <VBox/vm.h>
34
35#include <VBox/param.h>
36#include <VBox/err.h>
37#include <VBox/log.h>
38#include <VBox/sup.h>
39#include <iprt/time.h>
40#include <iprt/assert.h>
41#include <iprt/asm.h>
42#ifdef IN_RING3
43# include <iprt/thread.h>
44#endif
45
46
47#ifndef tmLock
48
49/**
50 * Try take the EMT/TM lock, wait in ring-3 return VERR_SEM_BUSY in R0/RC.
51 *
52 * @retval VINF_SUCCESS on success (always in ring-3).
53 * @retval VERR_SEM_BUSY in RC and R0 if the semaphore is busy.
54 *
55 * @param pVM The VM handle.
56 */
57int tmLock(PVM pVM)
58{
59 VM_ASSERT_EMT(pVM);
60 int rc = PDMCritSectEnter(&pVM->tm.s.EmtLock, VERR_SEM_BUSY);
61 return rc;
62}
63
64
65/**
66 * Try take the EMT/TM lock, no waiting.
67 *
68 * @retval VINF_SUCCESS on success.
69 * @retval VERR_SEM_BUSY if busy.
70 *
71 * @param pVM The VM handle.
72 */
73int tmTryLock(PVM pVM)
74{
75 VM_ASSERT_EMT(pVM);
76 int rc = PDMCritSectTryEnter(&pVM->tm.s.EmtLock);
77 return rc;
78}
79
80
81/**
82 * Release the EMT/TM lock.
83 *
84 * @param pVM The VM handle.
85 */
86void tmUnlock(PVM pVM)
87{
88 PDMCritSectLeave(&pVM->tm.s.EmtLock);
89}
90
91
92/**
93 * Try take the VirtualSync lock, wait in ring-3 return VERR_SEM_BUSY in R0/RC.
94 *
95 * @retval VINF_SUCCESS on success (always in ring-3).
96 * @retval VERR_SEM_BUSY in RC and R0 if the semaphore is busy.
97 *
98 * @param pVM The VM handle.
99 */
100int tmVirtualSyncLock(PVM pVM)
101{
102 VM_ASSERT_EMT(pVM);
103 int rc = PDMCritSectEnter(&pVM->tm.s.VirtualSyncLock, VERR_SEM_BUSY);
104 return rc;
105}
106
107
108/**
109 * Try take the VirtualSync lock, no waiting.
110 *
111 * @retval VINF_SUCCESS on success.
112 * @retval VERR_SEM_BUSY if busy.
113 *
114 * @param pVM The VM handle.
115 */
116int tmVirtualSyncTryLock(PVM pVM)
117{
118 VM_ASSERT_EMT(pVM);
119 int rc = PDMCritSectTryEnter(&pVM->tm.s.VirtualSyncLock);
120 return rc;
121}
122
123
124/**
125 * Release the VirtualSync lock.
126 *
127 * @param pVM The VM handle.
128 */
129void tmVirtualSyncUnlock(PVM pVM)
130{
131 PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
132}
133
134#endif /* ! macros */
135
136/**
137 * Notification that execution is about to start.
138 *
139 * This call must always be paired with a TMNotifyEndOfExecution call.
140 *
141 * The function may, depending on the configuration, resume the TSC and future
142 * clocks that only ticks when we're executing guest code.
143 *
144 * @param pVCpu The VMCPU to operate on.
145 */
146VMMDECL(void) TMNotifyStartOfExecution(PVMCPU pVCpu)
147{
148 PVM pVM = pVCpu->CTX_SUFF(pVM);
149
150 if (pVM->tm.s.fTSCTiedToExecution)
151 tmCpuTickResume(pVM, pVCpu);
152}
153
154
155/**
156 * Notification that execution is about to start.
157 *
158 * This call must always be paired with a TMNotifyStartOfExecution call.
159 *
160 * The function may, depending on the configuration, suspend 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) TMNotifyEndOfExecution(PVMCPU pVCpu)
166{
167 PVM pVM = pVCpu->CTX_SUFF(pVM);
168
169 if (pVM->tm.s.fTSCTiedToExecution)
170 tmCpuTickPause(pVM, pVCpu);
171}
172
173
174/**
175 * Notification that the cpu is entering the halt state
176 *
177 * This call must always be paired with a TMNotifyEndOfExecution call.
178 *
179 * The function may, depending on the configuration, resume the TSC and future
180 * clocks that only ticks when we're halted.
181 *
182 * @param pVCpu The VMCPU to operate on.
183 */
184VMMDECL(void) TMNotifyStartOfHalt(PVMCPU pVCpu)
185{
186 PVM pVM = pVCpu->CTX_SUFF(pVM);
187
188 if ( pVM->tm.s.fTSCTiedToExecution
189 && !pVM->tm.s.fTSCNotTiedToHalt)
190 tmCpuTickResume(pVM, pVCpu);
191}
192
193
194/**
195 * Notification that the cpu is leaving the halt state
196 *
197 * This call must always be paired with a TMNotifyStartOfHalt call.
198 *
199 * The function may, depending on the configuration, suspend the TSC and future
200 * clocks that only ticks when we're halted.
201 *
202 * @param pVCpu The VMCPU to operate on.
203 */
204VMMDECL(void) TMNotifyEndOfHalt(PVMCPU pVCpu)
205{
206 PVM pVM = pVCpu->CTX_SUFF(pVM);
207
208 if ( pVM->tm.s.fTSCTiedToExecution
209 && !pVM->tm.s.fTSCNotTiedToHalt)
210 tmCpuTickPause(pVM, pVCpu);
211}
212
213
214/**
215 * Raise the timer force action flag and notify the dedicated timer EMT.
216 *
217 * @param pVM The VM handle.
218 */
219DECLINLINE(void) tmScheduleNotify(PVM pVM)
220{
221 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
222 if (!VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
223 {
224 Log5(("TMAll(%u): FF: 0 -> 1\n", __LINE__));
225 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
226#ifdef IN_RING3
227 REMR3NotifyTimerPending(pVM, pVCpuDst);
228 VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
229#endif
230 STAM_COUNTER_INC(&pVM->tm.s.StatScheduleSetFF);
231 }
232}
233
234
235/**
236 * Schedule the queue which was changed.
237 */
238DECLINLINE(void) tmSchedule(PTMTIMER pTimer)
239{
240 PVM pVM = pTimer->CTX_SUFF(pVM);
241 if ( VM_IS_EMT(pVM)
242 && RT_SUCCESS(tmTryLock(pVM)))
243 {
244 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
245 Log3(("tmSchedule: tmTimerQueueSchedule\n"));
246 tmTimerQueueSchedule(pVM, &pVM->tm.s.CTX_SUFF(paTimerQueues)[pTimer->enmClock]);
247#ifdef VBOX_STRICT
248 tmTimerQueuesSanityChecks(pVM, "tmSchedule");
249#endif
250 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
251 tmUnlock(pVM);
252 }
253 else
254 {
255 TMTIMERSTATE enmState = pTimer->enmState;
256 if (TMTIMERSTATE_IS_PENDING_SCHEDULING(enmState))
257 tmScheduleNotify(pVM);
258 }
259}
260
261
262/**
263 * Try change the state to enmStateNew from enmStateOld
264 * and link the timer into the scheduling queue.
265 *
266 * @returns Success indicator.
267 * @param pTimer Timer in question.
268 * @param enmStateNew The new timer state.
269 * @param enmStateOld The old timer state.
270 */
271DECLINLINE(bool) tmTimerTry(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
272{
273 /*
274 * Attempt state change.
275 */
276 bool fRc;
277 TM_TRY_SET_STATE(pTimer, enmStateNew, enmStateOld, fRc);
278 return fRc;
279}
280
281
282/**
283 * Links the timer onto the scheduling queue.
284 *
285 * @param pQueue The timer queue the timer belongs to.
286 * @param pTimer The timer.
287 *
288 * @todo FIXME: Look into potential race with the thread running the queues
289 * and stuff.
290 */
291DECLINLINE(void) tmTimerLink(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
292{
293 Assert(!pTimer->offScheduleNext);
294 const int32_t offHeadNew = (intptr_t)pTimer - (intptr_t)pQueue;
295 int32_t offHead;
296 do
297 {
298 offHead = pQueue->offSchedule;
299 if (offHead)
300 pTimer->offScheduleNext = ((intptr_t)pQueue + offHead) - (intptr_t)pTimer;
301 else
302 pTimer->offScheduleNext = 0;
303 } while (!ASMAtomicCmpXchgS32(&pQueue->offSchedule, offHeadNew, offHead));
304}
305
306
307/**
308 * Try change the state to enmStateNew from enmStateOld
309 * and link the timer into the scheduling queue.
310 *
311 * @returns Success indicator.
312 * @param pTimer Timer in question.
313 * @param enmStateNew The new timer state.
314 * @param enmStateOld The old timer state.
315 */
316DECLINLINE(bool) tmTimerTryWithLink(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
317{
318 if (tmTimerTry(pTimer, enmStateNew, enmStateOld))
319 {
320 tmTimerLink(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF(paTimerQueues)[pTimer->enmClock], pTimer);
321 return true;
322 }
323 return false;
324}
325
326
327#ifdef VBOX_HIGH_RES_TIMERS_HACK
328
329/**
330 * Worker for tmTimerPollInternal that handles misses when the decidate timer
331 * EMT is polling.
332 *
333 * @returns See tmTimerPollInternal.
334 * @param pVM Pointer to the shared VM structure.
335 * @param u64Now Current virtual clock timestamp.
336 * @param u64Delta The delta to the next even in ticks of the
337 * virtual clock.
338 * @param pu64Delta Where to return the delta.
339 * @param pCounter The statistics counter to update.
340 */
341DECLINLINE(uint64_t) tmTimerPollReturnMiss(PVM pVM, uint64_t u64Now, uint64_t u64Delta, uint64_t *pu64Delta)
342{
343 Assert(!(u64Delta & RT_BIT_64(63)));
344
345 if (!pVM->tm.s.fVirtualWarpDrive)
346 {
347 *pu64Delta = u64Delta;
348 return u64Delta + u64Now + pVM->tm.s.u64VirtualOffset;
349 }
350
351 /*
352 * Warp drive adjustments - this is the reverse of what tmVirtualGetRaw is doing.
353 */
354 uint64_t const u64Start = pVM->tm.s.u64VirtualWarpDriveStart;
355 uint32_t const u32Pct = pVM->tm.s.u32VirtualWarpDrivePercentage;
356
357 uint64_t u64GipTime = u64Delta + u64Now + pVM->tm.s.u64VirtualOffset;
358 u64GipTime -= u64Start; /* the start is GIP time. */
359 if (u64GipTime >= u64Delta)
360 {
361 ASMMultU64ByU32DivByU32(u64GipTime, 100, u32Pct);
362 ASMMultU64ByU32DivByU32(u64Delta, 100, u32Pct);
363 }
364 else
365 {
366 u64Delta -= u64GipTime;
367 ASMMultU64ByU32DivByU32(u64GipTime, 100, u32Pct);
368 u64Delta += u64GipTime;
369 }
370 *pu64Delta = u64Delta;
371 u64GipTime += u64Start;
372 return u64GipTime;
373}
374
375
376/**
377 * Worker for tmTimerPollInternal dealing with returns on virtual CPUs other
378 * than the one dedicated to timer work.
379 *
380 * @returns See tmTimerPollInternal.
381 * @param pVM Pointer to the shared VM structure.
382 * @param u64Now Current virtual clock timestamp.
383 * @param pu64Delta Where to return the delta.
384 */
385DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnOtherCpu(PVM pVM, uint64_t u64Now, uint64_t *pu64Delta)
386{
387 static const uint64_t s_u64OtherRet = 500000000; /* 500 ms for non-timer EMTs. */
388 *pu64Delta = s_u64OtherRet;
389 return u64Now + pVM->tm.s.u64VirtualOffset + s_u64OtherRet;
390}
391
392
393/**
394 * Worker for tmTimerPollInternal.
395 *
396 * @returns See tmTimerPollInternal.
397 * @param pVM Pointer to the shared VM structure.
398 * @param pVCpu Pointer to the shared VMCPU structure of the
399 * caller.
400 * @param pVCpuDst Pointer to the shared VMCPU structure of the
401 * dedicated timer EMT.
402 * @param u64Now Current virtual clock timestamp.
403 * @param pu64Delta Where to return the delta.
404 * @param pCounter The statistics counter to update.
405 */
406DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnHit(PVM pVM, PVMCPU pVCpu, PVMCPU pVCpuDst, uint64_t u64Now,
407 uint64_t *pu64Delta, PSTAMCOUNTER pCounter)
408{
409 STAM_COUNTER_INC(pCounter);
410 if (pVCpuDst != pVCpu)
411 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
412 *pu64Delta = 0;
413 return 0;
414}
415
416/**
417 * Common worker for TMTimerPollGIP and TMTimerPoll.
418 *
419 * This function is called before FFs are checked in the inner execution EM loops.
420 *
421 * @returns The GIP timestamp of the next event.
422 * 0 if the next event has already expired.
423 *
424 * @param pVM Pointer to the shared VM structure.
425 * @param pVCpu Pointer to the shared VMCPU structure of the caller.
426 * @param pu64Delta Where to store the delta.
427 *
428 * @thread The emulation thread.
429 *
430 * @remarks GIP uses ns ticks.
431 */
432DECLINLINE(uint64_t) tmTimerPollInternal(PVM pVM, PVMCPU pVCpu, uint64_t *pu64Delta)
433{
434 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
435 const uint64_t u64Now = TMVirtualGetNoCheck(pVM);
436 STAM_COUNTER_INC(&pVM->tm.s.StatPoll);
437
438 /*
439 * Return straight away if the timer FF is already set ...
440 */
441 if (VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
442 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
443
444 /*
445 * ... or if timers are being run.
446 */
447 if (ASMAtomicReadBool(&pVM->tm.s.fRunningQueues))
448 {
449 STAM_COUNTER_INC(&pVM->tm.s.StatPollRunning);
450 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
451 }
452
453 /*
454 * Check for TMCLOCK_VIRTUAL expiration.
455 */
456 const uint64_t u64Expire1 = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire);
457 const int64_t i64Delta1 = u64Expire1 - u64Now;
458 if (i64Delta1 <= 0)
459 {
460 if (!VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
461 {
462 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_ISPENDING(pVCpuDst, VMCPU_FF_TIMER)));
463 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
464#ifdef IN_RING3
465 REMR3NotifyTimerPending(pVM, pVCpuDst);
466#endif
467 }
468 LogFlow(("TMTimerPoll: expire1=%RU64 <= now=%RU64\n", u64Expire1, u64Now));
469 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtual);
470 }
471
472 /*
473 * Check for TMCLOCK_VIRTUAL_SYNC expiration.
474 * This isn't quite as stright forward if in a catch-up, not only do
475 * we have to adjust the 'now' but when have to adjust the delta as well.
476 */
477
478 /*
479 * Optimistic lockless approach.
480 */
481 uint64_t u64VirtualSyncNow;
482 uint64_t u64Expire2 = ASMAtomicUoReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
483 if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
484 {
485 if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
486 {
487 u64VirtualSyncNow = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
488 if (RT_LIKELY( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
489 && !ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
490 && u64VirtualSyncNow == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
491 && u64Expire2 == ASMAtomicUoReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)))
492 {
493 u64VirtualSyncNow = u64Now - u64VirtualSyncNow;
494 int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
495 if (i64Delta2 > 0)
496 {
497 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
498 STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
499
500 if (pVCpu == pVCpuDst)
501 return tmTimerPollReturnMiss(pVM, u64Now, RT_MIN(i64Delta1, i64Delta2), pu64Delta);
502 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
503 }
504
505 if ( !pVM->tm.s.fRunningQueues
506 && !VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
507 {
508 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_ISPENDING(pVCpuDst, VMCPU_FF_TIMER)));
509 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
510#ifdef IN_RING3
511 REMR3NotifyTimerPending(pVM, pVCpuDst);
512#endif
513 }
514
515 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
516 LogFlow(("TMTimerPoll: expire2=%RU64 <= now=%RU64\n", u64Expire2, u64Now));
517 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
518 }
519 }
520 }
521 else
522 {
523 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
524 LogFlow(("TMTimerPoll: stopped\n"));
525 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
526 }
527
528 /*
529 * Complicated lockless approach.
530 */
531 uint64_t off;
532 uint32_t u32Pct = 0;
533 bool fCatchUp;
534 int cOuterTries = 42;
535 for (;; cOuterTries--)
536 {
537 fCatchUp = ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp);
538 off = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
539 u64Expire2 = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
540 if (fCatchUp)
541 {
542 /* No changes allowed, try get a consistent set of parameters. */
543 uint64_t const u64Prev = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev);
544 uint64_t const offGivenUp = ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp);
545 u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage);
546 if ( ( u64Prev == ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev)
547 && offGivenUp == ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp)
548 && u32Pct == ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage)
549 && off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
550 && u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)
551 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
552 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
553 || cOuterTries <= 0)
554 {
555 uint64_t u64Delta = u64Now - u64Prev;
556 if (RT_LIKELY(!(u64Delta >> 32)))
557 {
558 uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, u32Pct, 100);
559 if (off > u64Sub + offGivenUp)
560 off -= u64Sub;
561 else /* we've completely caught up. */
562 off = offGivenUp;
563 }
564 else
565 /* More than 4 seconds since last time (or negative), ignore it. */
566 Log(("TMVirtualGetSync: u64Delta=%RX64 (NoLock)\n", u64Delta));
567
568 /* Check that we're still running and in catch up. */
569 if ( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
570 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
571 break;
572 }
573 }
574 else if ( off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
575 && u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)
576 && !ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
577 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
578 break; /* Got an consistent offset */
579
580 /* Repeat the initial checks before iterating. */
581 if (VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
582 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
583 if (ASMAtomicUoReadBool(&pVM->tm.s.fRunningQueues))
584 {
585 STAM_COUNTER_INC(&pVM->tm.s.StatPollRunning);
586 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
587 }
588 if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
589 {
590 LogFlow(("TMTimerPoll: stopped\n"));
591 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
592 }
593 if (cOuterTries <= 0)
594 break; /* that's enough */
595 }
596 if (cOuterTries <= 0)
597 STAM_COUNTER_INC(&pVM->tm.s.StatPollELoop);
598 u64VirtualSyncNow = u64Now - off;
599
600 /* Calc delta and see if we've got a virtual sync hit. */
601 int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
602 if (i64Delta2 <= 0)
603 {
604 if ( !pVM->tm.s.fRunningQueues
605 && !VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
606 {
607 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_ISPENDING(pVCpuDst, VMCPU_FF_TIMER)));
608 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
609#ifdef IN_RING3
610 REMR3NotifyTimerPending(pVM, pVCpuDst);
611#endif
612 }
613 STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtualSync);
614 LogFlow(("TMTimerPoll: expire2=%RU64 <= now=%RU64\n", u64Expire2, u64Now));
615 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
616 }
617
618 /*
619 * Return the time left to the next event.
620 */
621 STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
622 if (pVCpu == pVCpuDst)
623 {
624 if (fCatchUp)
625 i64Delta2 = ASMMultU64ByU32DivByU32(i64Delta2, 100, u32Pct + 100);
626 return tmTimerPollReturnMiss(pVM, u64Now, RT_MIN(i64Delta1, i64Delta2), pu64Delta);
627 }
628 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
629}
630
631
632/**
633 * Set FF if we've passed the next virtual event.
634 *
635 * This function is called before FFs are checked in the inner execution EM loops.
636 *
637 * @returns Virtual timer ticks to the next event. (I.e. 0 means that an timer
638 * has expired or some important rescheduling is pending.)
639 * @param pVM Pointer to the shared VM structure.
640 * @param pVCpu Pointer to the shared VMCPU structure of the caller.
641 * @thread The emulation thread.
642 */
643VMMDECL(uint64_t) TMTimerPoll(PVM pVM, PVMCPU pVCpu)
644{
645 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
646 uint64_t off = 0;
647 tmTimerPollInternal(pVM, pVCpu, &off);
648 return off;
649}
650
651
652/**
653 * Set FF if we've passed the next virtual event.
654 *
655 * This function is called before FFs are checked in the inner execution EM loops.
656 *
657 * @returns The GIP timestamp of the next event.
658 * 0 if the next event has already expired.
659 * @param pVM Pointer to the shared VM structure.
660 * @param pVCpu Pointer to the shared VMCPU structure of the caller.
661 * @param pu64Delta Where to store the delta.
662 * @thread The emulation thread.
663 */
664VMMDECL(uint64_t) TMTimerPollGIP(PVM pVM, PVMCPU pVCpu, uint64_t *pu64Delta)
665{
666 return tmTimerPollInternal(pVM, pVCpu, pu64Delta);
667}
668
669#endif /* VBOX_HIGH_RES_TIMERS_HACK */
670
671/**
672 * Gets the host context ring-3 pointer of the timer.
673 *
674 * @returns HC R3 pointer.
675 * @param pTimer Timer handle as returned by one of the create functions.
676 */
677VMMDECL(PTMTIMERR3) TMTimerR3Ptr(PTMTIMER pTimer)
678{
679 return (PTMTIMERR3)MMHyperCCToR3(pTimer->CTX_SUFF(pVM), pTimer);
680}
681
682
683/**
684 * Gets the host context ring-0 pointer of the timer.
685 *
686 * @returns HC R0 pointer.
687 * @param pTimer Timer handle as returned by one of the create functions.
688 */
689VMMDECL(PTMTIMERR0) TMTimerR0Ptr(PTMTIMER pTimer)
690{
691 return (PTMTIMERR0)MMHyperCCToR0(pTimer->CTX_SUFF(pVM), pTimer);
692}
693
694
695/**
696 * Gets the RC pointer of the timer.
697 *
698 * @returns RC pointer.
699 * @param pTimer Timer handle as returned by one of the create functions.
700 */
701VMMDECL(PTMTIMERRC) TMTimerRCPtr(PTMTIMER pTimer)
702{
703 return (PTMTIMERRC)MMHyperCCToRC(pTimer->CTX_SUFF(pVM), pTimer);
704}
705
706
707/**
708 * Arm a timer with a (new) expire time.
709 *
710 * @returns VBox status.
711 * @param pTimer Timer handle as returned by one of the create functions.
712 * @param u64Expire New expire time.
713 */
714VMMDECL(int) TMTimerSet(PTMTIMER pTimer, uint64_t u64Expire)
715{
716 STAM_PROFILE_START(&pTimer->CTX_SUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
717
718 /** @todo find the most frequently used paths and make them skip tmSchedule and tmTimerTryWithLink. */
719 int cRetries = 1000;
720 do
721 {
722 /*
723 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
724 */
725 TMTIMERSTATE enmState = pTimer->enmState;
726 Log2(("TMTimerSet: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d u64Expire=%llu\n",
727 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries, u64Expire));
728 switch (enmState)
729 {
730 case TMTIMERSTATE_EXPIRED:
731 case TMTIMERSTATE_STOPPED:
732 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
733 {
734 Assert(!pTimer->offPrev);
735 Assert(!pTimer->offNext);
736 AssertMsg( pTimer->enmClock != TMCLOCK_VIRTUAL_SYNC
737 || pTimer->CTX_SUFF(pVM)->tm.s.fVirtualSyncTicking
738 || u64Expire >= pTimer->CTX_SUFF(pVM)->tm.s.u64VirtualSync,
739 ("%RU64 < %RU64 %s\n", u64Expire, pTimer->CTX_SUFF(pVM)->tm.s.u64VirtualSync, R3STRING(pTimer->pszDesc)));
740 pTimer->u64Expire = u64Expire;
741 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
742 tmSchedule(pTimer);
743 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSet), a);
744 return VINF_SUCCESS;
745 }
746 break;
747
748 case TMTIMERSTATE_PENDING_SCHEDULE:
749 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
750 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
751 {
752 pTimer->u64Expire = u64Expire;
753 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
754 tmSchedule(pTimer);
755 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSet), a);
756 return VINF_SUCCESS;
757 }
758 break;
759
760
761 case TMTIMERSTATE_ACTIVE:
762 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
763 {
764 pTimer->u64Expire = u64Expire;
765 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
766 tmSchedule(pTimer);
767 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSet), a);
768 return VINF_SUCCESS;
769 }
770 break;
771
772 case TMTIMERSTATE_PENDING_RESCHEDULE:
773 case TMTIMERSTATE_PENDING_STOP:
774 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
775 {
776 pTimer->u64Expire = u64Expire;
777 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
778 tmSchedule(pTimer);
779 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSet), a);
780 return VINF_SUCCESS;
781 }
782 break;
783
784
785 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
786 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
787#ifdef IN_RING3
788 if (!RTThreadYield())
789 RTThreadSleep(1);
790#else
791/** @todo call host context and yield after a couple of iterations */
792#endif
793 break;
794
795 /*
796 * Invalid states.
797 */
798 case TMTIMERSTATE_DESTROY:
799 case TMTIMERSTATE_FREE:
800 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
801 return VERR_TM_INVALID_STATE;
802 default:
803 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
804 return VERR_TM_UNKNOWN_STATE;
805 }
806 } while (cRetries-- > 0);
807
808 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
809 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSet), a);
810 return VERR_INTERNAL_ERROR;
811}
812
813
814/**
815 * Arm a timer with a (new) expire time relative to current time.
816 *
817 * @returns VBox status.
818 * @param pTimer Timer handle as returned by one of the create functions.
819 * @param cMilliesToNext Number of millieseconds to the next tick.
820 */
821VMMDECL(int) TMTimerSetMillies(PTMTIMER pTimer, uint32_t cMilliesToNext)
822{
823 PVM pVM = pTimer->CTX_SUFF(pVM);
824 PVMCPU pVCpu = &pVM->aCpus[0]; /* just take the first VCPU */
825
826 switch (pTimer->enmClock)
827 {
828 case TMCLOCK_VIRTUAL:
829 return TMTimerSet(pTimer, cMilliesToNext * (uint64_t)TMCLOCK_FREQ_VIRTUAL / 1000 + TMVirtualGet(pVM));
830 case TMCLOCK_VIRTUAL_SYNC:
831 return TMTimerSet(pTimer, cMilliesToNext * (uint64_t)TMCLOCK_FREQ_VIRTUAL / 1000 + TMVirtualSyncGet(pVM));
832 case TMCLOCK_REAL:
833 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
834 return TMTimerSet(pTimer, cMilliesToNext + TMRealGet(pVM));
835 case TMCLOCK_TSC:
836 return TMTimerSet(pTimer, cMilliesToNext * pVM->tm.s.cTSCTicksPerSecond / 1000 + TMCpuTickGet(pVCpu));
837
838 default:
839 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
840 return VERR_INTERNAL_ERROR;
841 }
842}
843
844
845/**
846 * Arm a timer with a (new) expire time relative to current time.
847 *
848 * @returns VBox status.
849 * @param pTimer Timer handle as returned by one of the create functions.
850 * @param cMicrosToNext Number of microseconds to the next tick.
851 */
852VMMDECL(int) TMTimerSetMicro(PTMTIMER pTimer, uint64_t cMicrosToNext)
853{
854 PVM pVM = pTimer->CTX_SUFF(pVM);
855 PVMCPU pVCpu = &pVM->aCpus[0]; /* just take the first VCPU */
856
857 switch (pTimer->enmClock)
858 {
859 case TMCLOCK_VIRTUAL:
860 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
861 return TMTimerSet(pTimer, cMicrosToNext * 1000 + TMVirtualGet(pVM));
862
863 case TMCLOCK_VIRTUAL_SYNC:
864 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
865 return TMTimerSet(pTimer, cMicrosToNext * 1000 + TMVirtualSyncGet(pVM));
866
867 case TMCLOCK_REAL:
868 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
869 return TMTimerSet(pTimer, cMicrosToNext / 1000 + TMRealGet(pVM));
870
871 case TMCLOCK_TSC:
872 return TMTimerSet(pTimer, TMTimerFromMicro(pTimer, cMicrosToNext) + TMCpuTickGet(pVCpu));
873
874 default:
875 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
876 return VERR_INTERNAL_ERROR;
877 }
878}
879
880
881/**
882 * Arm a timer with a (new) expire time relative to current time.
883 *
884 * @returns VBox status.
885 * @param pTimer Timer handle as returned by one of the create functions.
886 * @param cNanosToNext Number of nanoseconds to the next tick.
887 */
888VMMDECL(int) TMTimerSetNano(PTMTIMER pTimer, uint64_t cNanosToNext)
889{
890 PVM pVM = pTimer->CTX_SUFF(pVM);
891 PVMCPU pVCpu = &pVM->aCpus[0]; /* just take the first VCPU */
892
893 switch (pTimer->enmClock)
894 {
895 case TMCLOCK_VIRTUAL:
896 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
897 return TMTimerSet(pTimer, cNanosToNext + TMVirtualGet(pVM));
898
899 case TMCLOCK_VIRTUAL_SYNC:
900 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
901 return TMTimerSet(pTimer, cNanosToNext + TMVirtualSyncGet(pVM));
902
903 case TMCLOCK_REAL:
904 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
905 return TMTimerSet(pTimer, cNanosToNext / 1000000 + TMRealGet(pVM));
906
907 case TMCLOCK_TSC:
908 return TMTimerSet(pTimer, TMTimerFromNano(pTimer, cNanosToNext) + TMCpuTickGet(pVCpu));
909
910 default:
911 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
912 return VERR_INTERNAL_ERROR;
913 }
914}
915
916
917/**
918 * Stop the timer.
919 * Use TMR3TimerArm() to "un-stop" the timer.
920 *
921 * @returns VBox status.
922 * @param pTimer Timer handle as returned by one of the create functions.
923 */
924VMMDECL(int) TMTimerStop(PTMTIMER pTimer)
925{
926 STAM_PROFILE_START(&pTimer->CTX_SUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
927 /** @todo see if this function needs optimizing. */
928 int cRetries = 1000;
929 do
930 {
931 /*
932 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
933 */
934 TMTIMERSTATE enmState = pTimer->enmState;
935 Log2(("TMTimerStop: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
936 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries));
937 switch (enmState)
938 {
939 case TMTIMERSTATE_EXPIRED:
940 //AssertMsgFailed(("You don't stop an expired timer dude!\n"));
941 return VERR_INVALID_PARAMETER;
942
943 case TMTIMERSTATE_STOPPED:
944 case TMTIMERSTATE_PENDING_STOP:
945 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
946 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
947 return VINF_SUCCESS;
948
949 case TMTIMERSTATE_PENDING_SCHEDULE:
950 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, enmState))
951 {
952 tmSchedule(pTimer);
953 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
954 return VINF_SUCCESS;
955 }
956
957 case TMTIMERSTATE_PENDING_RESCHEDULE:
958 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
959 {
960 tmSchedule(pTimer);
961 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
962 return VINF_SUCCESS;
963 }
964 break;
965
966 case TMTIMERSTATE_ACTIVE:
967 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
968 {
969 tmSchedule(pTimer);
970 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
971 return VINF_SUCCESS;
972 }
973 break;
974
975 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
976 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
977#ifdef IN_RING3
978 if (!RTThreadYield())
979 RTThreadSleep(1);
980#else
981/**@todo call host and yield cpu after a while. */
982#endif
983 break;
984
985 /*
986 * Invalid states.
987 */
988 case TMTIMERSTATE_DESTROY:
989 case TMTIMERSTATE_FREE:
990 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
991 return VERR_TM_INVALID_STATE;
992 default:
993 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
994 return VERR_TM_UNKNOWN_STATE;
995 }
996 } while (cRetries-- > 0);
997
998 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
999 STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerStop), a);
1000 return VERR_INTERNAL_ERROR;
1001}
1002
1003
1004/**
1005 * Get the current clock time.
1006 * Handy for calculating the new expire time.
1007 *
1008 * @returns Current clock time.
1009 * @param pTimer Timer handle as returned by one of the create functions.
1010 */
1011VMMDECL(uint64_t) TMTimerGet(PTMTIMER pTimer)
1012{
1013 uint64_t u64;
1014 PVM pVM = pTimer->CTX_SUFF(pVM);
1015
1016 switch (pTimer->enmClock)
1017 {
1018 case TMCLOCK_VIRTUAL:
1019 u64 = TMVirtualGet(pVM);
1020 break;
1021 case TMCLOCK_VIRTUAL_SYNC:
1022 u64 = TMVirtualSyncGet(pVM);
1023 break;
1024 case TMCLOCK_REAL:
1025 u64 = TMRealGet(pVM);
1026 break;
1027 case TMCLOCK_TSC:
1028 {
1029 PVMCPU pVCpu = &pVM->aCpus[0]; /* just take the first VCPU */
1030 u64 = TMCpuTickGet(pVCpu);
1031 break;
1032 }
1033 default:
1034 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1035 return ~(uint64_t)0;
1036 }
1037 //Log2(("TMTimerGet: returns %llu (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1038 // u64, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1039 return u64;
1040}
1041
1042
1043/**
1044 * Get the freqency of the timer clock.
1045 *
1046 * @returns Clock frequency (as Hz of course).
1047 * @param pTimer Timer handle as returned by one of the create functions.
1048 */
1049VMMDECL(uint64_t) TMTimerGetFreq(PTMTIMER pTimer)
1050{
1051 switch (pTimer->enmClock)
1052 {
1053 case TMCLOCK_VIRTUAL:
1054 case TMCLOCK_VIRTUAL_SYNC:
1055 return TMCLOCK_FREQ_VIRTUAL;
1056
1057 case TMCLOCK_REAL:
1058 return TMCLOCK_FREQ_REAL;
1059
1060 case TMCLOCK_TSC:
1061 return TMCpuTicksPerSecond(pTimer->CTX_SUFF(pVM));
1062
1063 default:
1064 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1065 return 0;
1066 }
1067}
1068
1069
1070/**
1071 * Get the current clock time as nanoseconds.
1072 *
1073 * @returns The timer clock as nanoseconds.
1074 * @param pTimer Timer handle as returned by one of the create functions.
1075 */
1076VMMDECL(uint64_t) TMTimerGetNano(PTMTIMER pTimer)
1077{
1078 return TMTimerToNano(pTimer, TMTimerGet(pTimer));
1079}
1080
1081
1082/**
1083 * Get the current clock time as microseconds.
1084 *
1085 * @returns The timer clock as microseconds.
1086 * @param pTimer Timer handle as returned by one of the create functions.
1087 */
1088VMMDECL(uint64_t) TMTimerGetMicro(PTMTIMER pTimer)
1089{
1090 return TMTimerToMicro(pTimer, TMTimerGet(pTimer));
1091}
1092
1093
1094/**
1095 * Get the current clock time as milliseconds.
1096 *
1097 * @returns The timer clock as milliseconds.
1098 * @param pTimer Timer handle as returned by one of the create functions.
1099 */
1100VMMDECL(uint64_t) TMTimerGetMilli(PTMTIMER pTimer)
1101{
1102 return TMTimerToMilli(pTimer, TMTimerGet(pTimer));
1103}
1104
1105
1106/**
1107 * Converts the specified timer clock time to nanoseconds.
1108 *
1109 * @returns nanoseconds.
1110 * @param pTimer Timer handle as returned by one of the create functions.
1111 * @param u64Ticks The clock ticks.
1112 * @remark There could be rounding errors here. We just do a simple integere divide
1113 * without any adjustments.
1114 */
1115VMMDECL(uint64_t) TMTimerToNano(PTMTIMER pTimer, uint64_t u64Ticks)
1116{
1117 switch (pTimer->enmClock)
1118 {
1119 case TMCLOCK_VIRTUAL:
1120 case TMCLOCK_VIRTUAL_SYNC:
1121 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1122 return u64Ticks;
1123
1124 case TMCLOCK_REAL:
1125 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1126 return u64Ticks * 1000000;
1127
1128 case TMCLOCK_TSC:
1129 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
1130 return 0;
1131
1132 default:
1133 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1134 return 0;
1135 }
1136}
1137
1138
1139/**
1140 * Converts the specified timer clock time to microseconds.
1141 *
1142 * @returns microseconds.
1143 * @param pTimer Timer handle as returned by one of the create functions.
1144 * @param u64Ticks The clock ticks.
1145 * @remark There could be rounding errors here. We just do a simple integere divide
1146 * without any adjustments.
1147 */
1148VMMDECL(uint64_t) TMTimerToMicro(PTMTIMER pTimer, uint64_t u64Ticks)
1149{
1150 switch (pTimer->enmClock)
1151 {
1152 case TMCLOCK_VIRTUAL:
1153 case TMCLOCK_VIRTUAL_SYNC:
1154 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1155 return u64Ticks / 1000;
1156
1157 case TMCLOCK_REAL:
1158 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1159 return u64Ticks * 1000;
1160
1161 case TMCLOCK_TSC:
1162 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
1163 return 0;
1164
1165 default:
1166 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1167 return 0;
1168 }
1169}
1170
1171
1172/**
1173 * Converts the specified timer clock time to milliseconds.
1174 *
1175 * @returns milliseconds.
1176 * @param pTimer Timer handle as returned by one of the create functions.
1177 * @param u64Ticks The clock ticks.
1178 * @remark There could be rounding errors here. We just do a simple integere divide
1179 * without any adjustments.
1180 */
1181VMMDECL(uint64_t) TMTimerToMilli(PTMTIMER pTimer, uint64_t u64Ticks)
1182{
1183 switch (pTimer->enmClock)
1184 {
1185 case TMCLOCK_VIRTUAL:
1186 case TMCLOCK_VIRTUAL_SYNC:
1187 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1188 return u64Ticks / 1000000;
1189
1190 case TMCLOCK_REAL:
1191 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1192 return u64Ticks;
1193
1194 case TMCLOCK_TSC:
1195 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
1196 return 0;
1197
1198 default:
1199 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1200 return 0;
1201 }
1202}
1203
1204
1205/**
1206 * Converts the specified nanosecond timestamp to timer clock ticks.
1207 *
1208 * @returns timer clock ticks.
1209 * @param pTimer Timer handle as returned by one of the create functions.
1210 * @param u64NanoTS The nanosecond value ticks to convert.
1211 * @remark There could be rounding and overflow errors here.
1212 */
1213VMMDECL(uint64_t) TMTimerFromNano(PTMTIMER pTimer, uint64_t u64NanoTS)
1214{
1215 switch (pTimer->enmClock)
1216 {
1217 case TMCLOCK_VIRTUAL:
1218 case TMCLOCK_VIRTUAL_SYNC:
1219 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1220 return u64NanoTS;
1221
1222 case TMCLOCK_REAL:
1223 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1224 return u64NanoTS / 1000000;
1225
1226 case TMCLOCK_TSC:
1227 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
1228 return 0;
1229
1230 default:
1231 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1232 return 0;
1233 }
1234}
1235
1236
1237/**
1238 * Converts the specified microsecond timestamp to timer clock ticks.
1239 *
1240 * @returns timer clock ticks.
1241 * @param pTimer Timer handle as returned by one of the create functions.
1242 * @param u64MicroTS The microsecond value ticks to convert.
1243 * @remark There could be rounding and overflow errors here.
1244 */
1245VMMDECL(uint64_t) TMTimerFromMicro(PTMTIMER pTimer, uint64_t u64MicroTS)
1246{
1247 switch (pTimer->enmClock)
1248 {
1249 case TMCLOCK_VIRTUAL:
1250 case TMCLOCK_VIRTUAL_SYNC:
1251 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1252 return u64MicroTS * 1000;
1253
1254 case TMCLOCK_REAL:
1255 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1256 return u64MicroTS / 1000;
1257
1258 case TMCLOCK_TSC:
1259 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
1260 return 0;
1261
1262 default:
1263 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1264 return 0;
1265 }
1266}
1267
1268
1269/**
1270 * Converts the specified millisecond timestamp to timer clock ticks.
1271 *
1272 * @returns timer clock ticks.
1273 * @param pTimer Timer handle as returned by one of the create functions.
1274 * @param u64MilliTS The millisecond value ticks to convert.
1275 * @remark There could be rounding and overflow errors here.
1276 */
1277VMMDECL(uint64_t) TMTimerFromMilli(PTMTIMER pTimer, uint64_t u64MilliTS)
1278{
1279 switch (pTimer->enmClock)
1280 {
1281 case TMCLOCK_VIRTUAL:
1282 case TMCLOCK_VIRTUAL_SYNC:
1283 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1284 return u64MilliTS * 1000000;
1285
1286 case TMCLOCK_REAL:
1287 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
1288 return u64MilliTS;
1289
1290 case TMCLOCK_TSC:
1291 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
1292 return 0;
1293
1294 default:
1295 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
1296 return 0;
1297 }
1298}
1299
1300
1301/**
1302 * Get the expire time of the timer.
1303 * Only valid for active timers.
1304 *
1305 * @returns Expire time of the timer.
1306 * @param pTimer Timer handle as returned by one of the create functions.
1307 */
1308VMMDECL(uint64_t) TMTimerGetExpire(PTMTIMER pTimer)
1309{
1310 int cRetries = 1000;
1311 do
1312 {
1313 TMTIMERSTATE enmState = pTimer->enmState;
1314 switch (enmState)
1315 {
1316 case TMTIMERSTATE_EXPIRED:
1317 case TMTIMERSTATE_STOPPED:
1318 case TMTIMERSTATE_PENDING_STOP:
1319 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1320 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1321 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1322 return ~(uint64_t)0;
1323
1324 case TMTIMERSTATE_ACTIVE:
1325 case TMTIMERSTATE_PENDING_RESCHEDULE:
1326 case TMTIMERSTATE_PENDING_SCHEDULE:
1327 Log2(("TMTimerGetExpire: returns %llu (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1328 pTimer->u64Expire, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1329 return pTimer->u64Expire;
1330
1331 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1332 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1333#ifdef IN_RING3
1334 if (!RTThreadYield())
1335 RTThreadSleep(1);
1336#endif
1337 break;
1338
1339 /*
1340 * Invalid states.
1341 */
1342 case TMTIMERSTATE_DESTROY:
1343 case TMTIMERSTATE_FREE:
1344 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1345 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1346 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1347 return ~(uint64_t)0;
1348 default:
1349 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1350 return ~(uint64_t)0;
1351 }
1352 } while (cRetries-- > 0);
1353
1354 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
1355 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1356 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1357 return ~(uint64_t)0;
1358}
1359
1360
1361/**
1362 * Checks if a timer is active or not.
1363 *
1364 * @returns True if active.
1365 * @returns False if not active.
1366 * @param pTimer Timer handle as returned by one of the create functions.
1367 */
1368VMMDECL(bool) TMTimerIsActive(PTMTIMER pTimer)
1369{
1370 TMTIMERSTATE enmState = pTimer->enmState;
1371 switch (enmState)
1372 {
1373 case TMTIMERSTATE_STOPPED:
1374 case TMTIMERSTATE_EXPIRED:
1375 case TMTIMERSTATE_PENDING_STOP:
1376 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1377 Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1378 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1379 return false;
1380
1381 case TMTIMERSTATE_ACTIVE:
1382 case TMTIMERSTATE_PENDING_RESCHEDULE:
1383 case TMTIMERSTATE_PENDING_SCHEDULE:
1384 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1385 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1386 Log2(("TMTimerIsActive: returns true (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1387 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1388 return true;
1389
1390 /*
1391 * Invalid states.
1392 */
1393 case TMTIMERSTATE_DESTROY:
1394 case TMTIMERSTATE_FREE:
1395 AssertMsgFailed(("Invalid timer state %s (%s)\n", tmTimerState(enmState), R3STRING(pTimer->pszDesc)));
1396 Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
1397 pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
1398 return false;
1399 default:
1400 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1401 return false;
1402 }
1403}
1404
1405
1406/**
1407 * Convert state to string.
1408 *
1409 * @returns Readonly status name.
1410 * @param enmState State.
1411 */
1412const char *tmTimerState(TMTIMERSTATE enmState)
1413{
1414 switch (enmState)
1415 {
1416#define CASE(num, state) \
1417 case TMTIMERSTATE_##state: \
1418 AssertCompile(TMTIMERSTATE_##state == (num)); \
1419 return #num "-" #state
1420 CASE( 1,STOPPED);
1421 CASE( 2,ACTIVE);
1422 CASE( 3,EXPIRED);
1423 CASE( 4,PENDING_STOP);
1424 CASE( 5,PENDING_STOP_SCHEDULE);
1425 CASE( 6,PENDING_SCHEDULE_SET_EXPIRE);
1426 CASE( 7,PENDING_SCHEDULE);
1427 CASE( 8,PENDING_RESCHEDULE_SET_EXPIRE);
1428 CASE( 9,PENDING_RESCHEDULE);
1429 CASE(10,DESTROY);
1430 CASE(11,FREE);
1431 default:
1432 AssertMsgFailed(("Invalid state enmState=%d\n", enmState));
1433 return "Invalid state!";
1434#undef CASE
1435 }
1436}
1437
1438
1439/**
1440 * Schedules the given timer on the given queue.
1441 *
1442 * @param pQueue The timer queue.
1443 * @param pTimer The timer that needs scheduling.
1444 *
1445 * @remarks Called while owning the lock.
1446 */
1447DECLINLINE(void) tmTimerQueueScheduleOne(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
1448{
1449 /*
1450 * Processing.
1451 */
1452 unsigned cRetries = 2;
1453 do
1454 {
1455 TMTIMERSTATE enmState = pTimer->enmState;
1456 switch (enmState)
1457 {
1458 /*
1459 * Reschedule timer (in the active list).
1460 */
1461 case TMTIMERSTATE_PENDING_RESCHEDULE:
1462 {
1463 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE, TMTIMERSTATE_PENDING_RESCHEDULE)))
1464 break; /* retry */
1465
1466 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1467 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
1468 if (pPrev)
1469 TMTIMER_SET_NEXT(pPrev, pNext);
1470 else
1471 {
1472 TMTIMER_SET_HEAD(pQueue, pNext);
1473 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1474 }
1475 if (pNext)
1476 TMTIMER_SET_PREV(pNext, pPrev);
1477 pTimer->offNext = 0;
1478 pTimer->offPrev = 0;
1479 /* fall thru */
1480 }
1481
1482 /*
1483 * Schedule timer (insert into the active list).
1484 */
1485 case TMTIMERSTATE_PENDING_SCHEDULE:
1486 {
1487 Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
1488 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, TMTIMERSTATE_PENDING_SCHEDULE)))
1489 break; /* retry */
1490
1491 PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue);
1492 if (pCur)
1493 {
1494 const uint64_t u64Expire = pTimer->u64Expire;
1495 for (;; pCur = TMTIMER_GET_NEXT(pCur))
1496 {
1497 if (pCur->u64Expire > u64Expire)
1498 {
1499 const PTMTIMER pPrev = TMTIMER_GET_PREV(pCur);
1500 TMTIMER_SET_NEXT(pTimer, pCur);
1501 TMTIMER_SET_PREV(pTimer, pPrev);
1502 if (pPrev)
1503 TMTIMER_SET_NEXT(pPrev, pTimer);
1504 else
1505 {
1506 TMTIMER_SET_HEAD(pQueue, pTimer);
1507 pQueue->u64Expire = u64Expire;
1508 }
1509 TMTIMER_SET_PREV(pCur, pTimer);
1510 return;
1511 }
1512 if (!pCur->offNext)
1513 {
1514 TMTIMER_SET_NEXT(pCur, pTimer);
1515 TMTIMER_SET_PREV(pTimer, pCur);
1516 return;
1517 }
1518 }
1519 }
1520 else
1521 {
1522 TMTIMER_SET_HEAD(pQueue, pTimer);
1523 pQueue->u64Expire = pTimer->u64Expire;
1524 }
1525 return;
1526 }
1527
1528 /*
1529 * Stop the timer in active list.
1530 */
1531 case TMTIMERSTATE_PENDING_STOP:
1532 {
1533 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, TMTIMERSTATE_PENDING_STOP)))
1534 break; /* retry */
1535
1536 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1537 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
1538 if (pPrev)
1539 TMTIMER_SET_NEXT(pPrev, pNext);
1540 else
1541 {
1542 TMTIMER_SET_HEAD(pQueue, pNext);
1543 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1544 }
1545 if (pNext)
1546 TMTIMER_SET_PREV(pNext, pPrev);
1547 pTimer->offNext = 0;
1548 pTimer->offPrev = 0;
1549 /* fall thru */
1550 }
1551
1552 /*
1553 * Stop the timer (not on the active list).
1554 */
1555 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1556 Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
1557 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_PENDING_STOP_SCHEDULE)))
1558 break;
1559 return;
1560
1561 /*
1562 * The timer is pending destruction by TMR3TimerDestroy, our caller.
1563 * Nothing to do here.
1564 */
1565 case TMTIMERSTATE_DESTROY:
1566 break;
1567
1568 /*
1569 * Postpone these until they get into the right state.
1570 */
1571 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1572 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1573 tmTimerLink(pQueue, pTimer);
1574 STAM_COUNTER_INC(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatPostponed));
1575 return;
1576
1577 /*
1578 * None of these can be in the schedule.
1579 */
1580 case TMTIMERSTATE_FREE:
1581 case TMTIMERSTATE_STOPPED:
1582 case TMTIMERSTATE_ACTIVE:
1583 case TMTIMERSTATE_EXPIRED:
1584 default:
1585 AssertMsgFailed(("Timer (%p) in the scheduling list has an invalid state %s (%d)!",
1586 pTimer, tmTimerState(pTimer->enmState), pTimer->enmState));
1587 return;
1588 }
1589 } while (cRetries-- > 0);
1590}
1591
1592
1593/**
1594 * Schedules the specified timer queue.
1595 *
1596 * @param pVM The VM to run the timers for.
1597 * @param pQueue The queue to schedule.
1598 *
1599 * @remarks Called while owning the lock.
1600 */
1601void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue)
1602{
1603 TM_ASSERT_EMT_LOCK(pVM);
1604
1605 /*
1606 * Dequeue the scheduling list and iterate it.
1607 */
1608 int32_t offNext = ASMAtomicXchgS32(&pQueue->offSchedule, 0);
1609 Log2(("tmTimerQueueSchedule: pQueue=%p:{.enmClock=%d, offNext=%RI32}\n", pQueue, pQueue->enmClock, offNext));
1610 if (!offNext)
1611 return;
1612 PTMTIMER pNext = (PTMTIMER)((intptr_t)pQueue + offNext);
1613 while (pNext)
1614 {
1615 /*
1616 * Unlink the head timer and find the next one.
1617 */
1618 PTMTIMER pTimer = pNext;
1619 pNext = pNext->offScheduleNext ? (PTMTIMER)((intptr_t)pNext + pNext->offScheduleNext) : NULL;
1620 pTimer->offScheduleNext = 0;
1621
1622 /*
1623 * Do the scheduling.
1624 */
1625 Log2(("tmTimerQueueSchedule: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, .pszDesc=%s}\n",
1626 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, R3STRING(pTimer->pszDesc)));
1627 tmTimerQueueScheduleOne(pQueue, pTimer);
1628 Log2(("tmTimerQueueSchedule: %p: new %s\n", pTimer, tmTimerState(pTimer->enmState)));
1629 } /* foreach timer in current schedule batch. */
1630}
1631
1632
1633#ifdef VBOX_STRICT
1634/**
1635 * Checks that the timer queues are sane.
1636 *
1637 * @param pVM VM handle.
1638 *
1639 * @remarks Called while owning the lock.
1640 */
1641void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere)
1642{
1643 TM_ASSERT_EMT_LOCK(pVM);
1644
1645 /*
1646 * Check the linking of the active lists.
1647 */
1648 for (int i = 0; i < TMCLOCK_MAX; i++)
1649 {
1650 PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[i];
1651 Assert((int)pQueue->enmClock == i);
1652 PTMTIMER pPrev = NULL;
1653 for (PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue); pCur; pPrev = pCur, pCur = TMTIMER_GET_NEXT(pCur))
1654 {
1655 AssertMsg((int)pCur->enmClock == i, ("%s: %d != %d\n", pszWhere, pCur->enmClock, i));
1656 AssertMsg(TMTIMER_GET_PREV(pCur) == pPrev, ("%s: %p != %p\n", pszWhere, TMTIMER_GET_PREV(pCur), pPrev));
1657 TMTIMERSTATE enmState = pCur->enmState;
1658 switch (enmState)
1659 {
1660 case TMTIMERSTATE_ACTIVE:
1661 AssertMsg( !pCur->offScheduleNext
1662 || pCur->enmState != TMTIMERSTATE_ACTIVE,
1663 ("%s: %RI32\n", pszWhere, pCur->offScheduleNext));
1664 break;
1665 case TMTIMERSTATE_PENDING_STOP:
1666 case TMTIMERSTATE_PENDING_RESCHEDULE:
1667 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1668 break;
1669 default:
1670 AssertMsgFailed(("%s: Invalid state enmState=%d %s\n", pszWhere, enmState, tmTimerState(enmState)));
1671 break;
1672 }
1673 }
1674 }
1675
1676
1677# ifdef IN_RING3
1678 /*
1679 * Do the big list and check that active timers all are in the active lists.
1680 */
1681 PTMTIMERR3 pPrev = NULL;
1682 for (PTMTIMERR3 pCur = pVM->tm.s.pCreated; pCur; pPrev = pCur, pCur = pCur->pBigNext)
1683 {
1684 Assert(pCur->pBigPrev == pPrev);
1685 Assert((unsigned)pCur->enmClock < (unsigned)TMCLOCK_MAX);
1686
1687 TMTIMERSTATE enmState = pCur->enmState;
1688 switch (enmState)
1689 {
1690 case TMTIMERSTATE_ACTIVE:
1691 case TMTIMERSTATE_PENDING_STOP:
1692 case TMTIMERSTATE_PENDING_RESCHEDULE:
1693 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1694 {
1695 PTMTIMERR3 pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTX_SUFF(paTimerQueues)[pCur->enmClock]);
1696 Assert(pCur->offPrev || pCur == pCurAct);
1697 while (pCurAct && pCurAct != pCur)
1698 pCurAct = TMTIMER_GET_NEXT(pCurAct);
1699 Assert(pCurAct == pCur);
1700 break;
1701 }
1702
1703 case TMTIMERSTATE_PENDING_SCHEDULE:
1704 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1705 case TMTIMERSTATE_STOPPED:
1706 case TMTIMERSTATE_EXPIRED:
1707 {
1708 Assert(!pCur->offNext);
1709 Assert(!pCur->offPrev);
1710 for (PTMTIMERR3 pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTX_SUFF(paTimerQueues)[pCur->enmClock]);
1711 pCurAct;
1712 pCurAct = TMTIMER_GET_NEXT(pCurAct))
1713 {
1714 Assert(pCurAct != pCur);
1715 Assert(TMTIMER_GET_NEXT(pCurAct) != pCur);
1716 Assert(TMTIMER_GET_PREV(pCurAct) != pCur);
1717 }
1718 break;
1719 }
1720
1721 /* ignore */
1722 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1723 break;
1724
1725 /* shouldn't get here! */
1726 case TMTIMERSTATE_DESTROY:
1727 default:
1728 AssertMsgFailed(("Invalid state enmState=%d %s\n", enmState, tmTimerState(enmState)));
1729 break;
1730 }
1731 }
1732# endif /* IN_RING3 */
1733}
1734#endif /* !VBOX_STRICT */
1735
1736
1737/**
1738 * Gets the current warp drive percent.
1739 *
1740 * @returns The warp drive percent.
1741 * @param pVM The VM handle.
1742 */
1743VMMDECL(uint32_t) TMGetWarpDrive(PVM pVM)
1744{
1745 return pVM->tm.s.u32VirtualWarpDrivePercentage;
1746}
1747
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette