VirtualBox

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

Last change on this file since 92296 was 90346, checked in by vboxsync, 3 years ago
  • VMM: Pass pVM to PDMCritSect APIs. bugref:9218 bugref:10074
  • DrvNetShaper: Do bandwidth allocation via PDMDrvHlp. bugref:10074
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 105.0 KB
Line 
1/* $Id: TMAll.cpp 90346 2021-07-26 19:55:53Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, all contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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#ifdef DEBUG_bird
24# define DBGFTRACE_DISABLED /* annoying */
25#endif
26#include <VBox/vmm/tm.h>
27#include <VBox/vmm/mm.h>
28#include <VBox/vmm/dbgftrace.h>
29#ifdef IN_RING3
30#endif
31#include <VBox/vmm/pdmdev.h> /* (for TMTIMER_GET_CRITSECT implementation) */
32#include "TMInternal.h"
33#include <VBox/vmm/vmcc.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#include <iprt/asm-math.h>
43#ifdef IN_RING3
44# include <iprt/thread.h>
45#endif
46
47#include "TMInline.h"
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53#ifdef VBOX_STRICT
54/** @def TMTIMER_GET_CRITSECT
55 * Helper for safely resolving the critical section for a timer belonging to a
56 * device instance.
57 * @todo needs reworking later as it uses PDMDEVINSR0::pDevInsR0RemoveMe. */
58# ifdef IN_RING3
59# define TMTIMER_GET_CRITSECT(a_pVM, a_pTimer) ((a_pTimer)->pCritSect)
60# else
61# define TMTIMER_GET_CRITSECT(a_pVM, a_pTimer) tmRZTimerGetCritSect(a_pVM, a_pTimer)
62# endif
63#endif
64
65/** @def TMTIMER_ASSERT_CRITSECT
66 * Checks that the caller owns the critical section if one is associated with
67 * the timer. */
68#ifdef VBOX_STRICT
69# define TMTIMER_ASSERT_CRITSECT(a_pVM, a_pTimer) \
70 do { \
71 if ((a_pTimer)->pCritSect) \
72 { \
73 VMSTATE enmState; \
74 PPDMCRITSECT pCritSect = TMTIMER_GET_CRITSECT(a_pVM, a_pTimer); \
75 AssertMsg( pCritSect \
76 && ( PDMCritSectIsOwner((a_pVM), pCritSect) \
77 || (enmState = (a_pVM)->enmVMState) == VMSTATE_CREATING \
78 || enmState == VMSTATE_RESETTING \
79 || enmState == VMSTATE_RESETTING_LS ),\
80 ("pTimer=%p (%s) pCritSect=%p (%s)\n", a_pTimer, (a_pTimer)->szName, \
81 (a_pTimer)->pCritSect, R3STRING(PDMR3CritSectName((a_pTimer)->pCritSect)) )); \
82 } \
83 } while (0)
84#else
85# define TMTIMER_ASSERT_CRITSECT(pVM, pTimer) do { } while (0)
86#endif
87
88/** @def TMTIMER_ASSERT_SYNC_CRITSECT_ORDER
89 * Checks for lock order trouble between the timer critsect and the critical
90 * section critsect. The virtual sync critsect must always be entered before
91 * the one associated with the timer (see TMR3TimerQueuesDo). It is OK if there
92 * isn't any critical section associated with the timer or if the calling thread
93 * doesn't own it, ASSUMING of course that the thread using this macro is going
94 * to enter the virtual sync critical section anyway.
95 *
96 * @remarks This is a sligtly relaxed timer locking attitude compared to
97 * TMTIMER_ASSERT_CRITSECT, however, the calling device/whatever code
98 * should know what it's doing if it's stopping or starting a timer
99 * without taking the device lock.
100 */
101#ifdef VBOX_STRICT
102# define TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer) \
103 do { \
104 if ((pTimer)->pCritSect) \
105 { \
106 VMSTATE enmState; \
107 PPDMCRITSECT pCritSect = TMTIMER_GET_CRITSECT(pVM, pTimer); \
108 AssertMsg( pCritSect \
109 && ( !PDMCritSectIsOwner((pVM), pCritSect) \
110 || PDMCritSectIsOwner((pVM), &(pVM)->tm.s.VirtualSyncLock) \
111 || (enmState = (pVM)->enmVMState) == VMSTATE_CREATING \
112 || enmState == VMSTATE_RESETTING \
113 || enmState == VMSTATE_RESETTING_LS ),\
114 ("pTimer=%p (%s) pCritSect=%p (%s)\n", pTimer, pTimer->szName, \
115 (pTimer)->pCritSect, R3STRING(PDMR3CritSectName((pTimer)->pCritSect)) )); \
116 } \
117 } while (0)
118#else
119# define TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer) do { } while (0)
120#endif
121
122
123#if defined(VBOX_STRICT) && defined(IN_RING0)
124/**
125 * Helper for TMTIMER_GET_CRITSECT
126 * @todo This needs a redo!
127 */
128DECLINLINE(PPDMCRITSECT) tmRZTimerGetCritSect(PVMCC pVM, PTMTIMER pTimer)
129{
130 if (pTimer->enmType == TMTIMERTYPE_DEV)
131 {
132 RTCCUINTREG fSavedFlags = ASMAddFlags(X86_EFL_AC); /** @todo fix ring-3 pointer use */
133 PPDMDEVINSR0 pDevInsR0 = ((struct PDMDEVINSR3 *)pTimer->u.Dev.pDevIns)->pDevInsR0RemoveMe; /* !ring-3 read! */
134 ASMSetFlags(fSavedFlags);
135 struct PDMDEVINSR3 *pDevInsR3 = pDevInsR0->pDevInsForR3R0;
136 if (pTimer->pCritSect == pDevInsR3->pCritSectRoR3)
137 return pDevInsR0->pCritSectRoR0;
138 uintptr_t offCritSect = (uintptr_t)pTimer->pCritSect - (uintptr_t)pDevInsR3->pvInstanceDataR3;
139 if (offCritSect < pDevInsR0->pReg->cbInstanceShared)
140 return (PPDMCRITSECT)((uintptr_t)pDevInsR0->pvInstanceDataR0 + offCritSect);
141 }
142 return (PPDMCRITSECT)MMHyperR3ToCC(pVM, pTimer->pCritSect);
143}
144#endif /* VBOX_STRICT && IN_RING0 */
145
146
147/**
148 * Notification that execution is about to start.
149 *
150 * This call must always be paired with a TMNotifyEndOfExecution call.
151 *
152 * The function may, depending on the configuration, resume the TSC and future
153 * clocks that only ticks when we're executing guest code.
154 *
155 * @param pVM The cross context VM structure.
156 * @param pVCpu The cross context virtual CPU structure.
157 */
158VMMDECL(void) TMNotifyStartOfExecution(PVMCC pVM, PVMCPUCC pVCpu)
159{
160#ifndef VBOX_WITHOUT_NS_ACCOUNTING
161 pVCpu->tm.s.uTscStartExecuting = SUPReadTsc();
162 pVCpu->tm.s.fExecuting = true;
163#endif
164 if (pVM->tm.s.fTSCTiedToExecution)
165 tmCpuTickResume(pVM, pVCpu);
166}
167
168
169/**
170 * Notification that execution has ended.
171 *
172 * This call must always be paired with a TMNotifyStartOfExecution call.
173 *
174 * The function may, depending on the configuration, suspend the TSC and future
175 * clocks that only ticks when we're executing guest code.
176 *
177 * @param pVM The cross context VM structure.
178 * @param pVCpu The cross context virtual CPU structure.
179 * @param uTsc TSC value when exiting guest context.
180 */
181VMMDECL(void) TMNotifyEndOfExecution(PVMCC pVM, PVMCPUCC pVCpu, uint64_t uTsc)
182{
183 if (pVM->tm.s.fTSCTiedToExecution)
184 tmCpuTickPause(pVCpu); /** @todo use uTsc here if we can. */
185
186#ifndef VBOX_WITHOUT_NS_ACCOUNTING
187 /*
188 * Calculate the elapsed tick count and convert it to nanoseconds.
189 */
190# ifdef IN_RING3
191 uint64_t cTicks = uTsc - pVCpu->tm.s.uTscStartExecuting - SUPGetTscDelta();
192 uint64_t const uCpuHz = SUPGetCpuHzFromGip(g_pSUPGlobalInfoPage);
193# else
194 uint64_t cTicks = uTsc - pVCpu->tm.s.uTscStartExecuting - SUPGetTscDeltaByCpuSetIndex(pVCpu->iHostCpuSet);
195 uint64_t const uCpuHz = SUPGetCpuHzFromGipBySetIndex(g_pSUPGlobalInfoPage, pVCpu->iHostCpuSet);
196# endif
197 AssertStmt(cTicks <= uCpuHz << 2, cTicks = uCpuHz << 2); /* max 4 sec */
198
199 uint64_t cNsExecutingDelta;
200 if (uCpuHz < _4G)
201 cNsExecutingDelta = ASMMultU64ByU32DivByU32(cTicks, RT_NS_1SEC, uCpuHz);
202 else if (uCpuHz < 16*_1G64)
203 cNsExecutingDelta = ASMMultU64ByU32DivByU32(cTicks >> 2, RT_NS_1SEC, uCpuHz >> 2);
204 else
205 {
206 Assert(uCpuHz < 64 * _1G64);
207 cNsExecutingDelta = ASMMultU64ByU32DivByU32(cTicks >> 4, RT_NS_1SEC, uCpuHz >> 4);
208 }
209
210 /*
211 * Update the data.
212 *
213 * Note! We're not using strict memory ordering here to speed things us.
214 * The data is in a single cache line and this thread is the only
215 * one writing to that line, so I cannot quite imagine why we would
216 * need any strict ordering here.
217 */
218 uint64_t const cNsExecutingNew = pVCpu->tm.s.cNsExecuting + cNsExecutingDelta;
219 uint32_t uGen = ASMAtomicUoIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
220 ASMCompilerBarrier();
221 pVCpu->tm.s.fExecuting = false;
222 pVCpu->tm.s.cNsExecuting = cNsExecutingNew;
223 pVCpu->tm.s.cPeriodsExecuting++;
224 ASMCompilerBarrier();
225 ASMAtomicUoWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
226
227 /*
228 * Update stats.
229 */
230# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
231 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecuting, cNsExecutingDelta);
232 if (cNsExecutingDelta < 5000)
233 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecTiny, cNsExecutingDelta);
234 else if (cNsExecutingDelta < 50000)
235 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecShort, cNsExecutingDelta);
236 else
237 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecLong, cNsExecutingDelta);
238# endif
239
240 /* The timer triggers occational updating of the others and total stats: */
241 if (RT_LIKELY(!pVCpu->tm.s.fUpdateStats))
242 { /*likely*/ }
243 else
244 {
245 pVCpu->tm.s.fUpdateStats = false;
246
247 uint64_t const cNsTotalNew = RTTimeNanoTS() - pVCpu->tm.s.nsStartTotal;
248 uint64_t const cNsOtherNew = cNsTotalNew - cNsExecutingNew - pVCpu->tm.s.cNsHalted;
249
250# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
251 STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsTotal, cNsTotalNew - pVCpu->tm.s.cNsTotalStat);
252 int64_t const cNsOtherNewDelta = cNsOtherNew - pVCpu->tm.s.cNsOtherStat;
253 if (cNsOtherNewDelta > 0)
254 STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsOther, (uint64_t)cNsOtherNewDelta);
255# endif
256
257 pVCpu->tm.s.cNsTotalStat = cNsTotalNew;
258 pVCpu->tm.s.cNsOtherStat = cNsOtherNew;
259 }
260
261#endif
262}
263
264
265/**
266 * Notification that the cpu is entering the halt state
267 *
268 * This call must always be paired with a TMNotifyEndOfExecution call.
269 *
270 * The function may, depending on the configuration, resume the TSC and future
271 * clocks that only ticks when we're halted.
272 *
273 * @param pVCpu The cross context virtual CPU structure.
274 */
275VMM_INT_DECL(void) TMNotifyStartOfHalt(PVMCPUCC pVCpu)
276{
277 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
278
279#ifndef VBOX_WITHOUT_NS_ACCOUNTING
280 pVCpu->tm.s.nsStartHalting = RTTimeNanoTS();
281 pVCpu->tm.s.fHalting = true;
282#endif
283
284 if ( pVM->tm.s.fTSCTiedToExecution
285 && !pVM->tm.s.fTSCNotTiedToHalt)
286 tmCpuTickResume(pVM, pVCpu);
287}
288
289
290/**
291 * Notification that the cpu is leaving the halt state
292 *
293 * This call must always be paired with a TMNotifyStartOfHalt call.
294 *
295 * The function may, depending on the configuration, suspend the TSC and future
296 * clocks that only ticks when we're halted.
297 *
298 * @param pVCpu The cross context virtual CPU structure.
299 */
300VMM_INT_DECL(void) TMNotifyEndOfHalt(PVMCPUCC pVCpu)
301{
302 PVM pVM = pVCpu->CTX_SUFF(pVM);
303
304 if ( pVM->tm.s.fTSCTiedToExecution
305 && !pVM->tm.s.fTSCNotTiedToHalt)
306 tmCpuTickPause(pVCpu);
307
308#ifndef VBOX_WITHOUT_NS_ACCOUNTING
309 uint64_t const u64NsTs = RTTimeNanoTS();
310 uint64_t const cNsTotalNew = u64NsTs - pVCpu->tm.s.nsStartTotal;
311 uint64_t const cNsHaltedDelta = u64NsTs - pVCpu->tm.s.nsStartHalting;
312 uint64_t const cNsHaltedNew = pVCpu->tm.s.cNsHalted + cNsHaltedDelta;
313 uint64_t const cNsOtherNew = cNsTotalNew - pVCpu->tm.s.cNsExecuting - cNsHaltedNew;
314
315 uint32_t uGen = ASMAtomicUoIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
316 ASMCompilerBarrier();
317 pVCpu->tm.s.fHalting = false;
318 pVCpu->tm.s.fUpdateStats = false;
319 pVCpu->tm.s.cNsHalted = cNsHaltedNew;
320 pVCpu->tm.s.cPeriodsHalted++;
321 ASMCompilerBarrier();
322 ASMAtomicUoWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
323
324# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
325 STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsHalted, cNsHaltedDelta);
326 STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsTotal, cNsTotalNew - pVCpu->tm.s.cNsTotalStat);
327 int64_t const cNsOtherNewDelta = cNsOtherNew - pVCpu->tm.s.cNsOtherStat;
328 if (cNsOtherNewDelta > 0)
329 STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsOther, (uint64_t)cNsOtherNewDelta);
330# endif
331 pVCpu->tm.s.cNsTotalStat = cNsTotalNew;
332 pVCpu->tm.s.cNsOtherStat = cNsOtherNew;
333#endif
334}
335
336
337/**
338 * Raise the timer force action flag and notify the dedicated timer EMT.
339 *
340 * @param pVM The cross context VM structure.
341 */
342DECLINLINE(void) tmScheduleNotify(PVMCC pVM)
343{
344 VMCPUID idCpu = pVM->tm.s.idTimerCpu;
345 AssertReturnVoid(idCpu < pVM->cCpus);
346 PVMCPUCC pVCpuDst = VMCC_GET_CPU(pVM, idCpu);
347
348 if (!VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
349 {
350 Log5(("TMAll(%u): FF: 0 -> 1\n", __LINE__));
351 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
352#ifdef IN_RING3
353 VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
354#endif
355 STAM_COUNTER_INC(&pVM->tm.s.StatScheduleSetFF);
356 }
357}
358
359
360/**
361 * Schedule the queue which was changed.
362 */
363DECLINLINE(void) tmSchedule(PVMCC pVM, PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
364{
365 int rc = PDMCritSectTryEnter(pVM, &pQueue->TimerLock);
366 if (RT_SUCCESS_NP(rc))
367 {
368 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
369 Log3(("tmSchedule: tmTimerQueueSchedule\n"));
370 tmTimerQueueSchedule(pVM, pQueueCC, pQueue);
371#ifdef VBOX_STRICT
372 tmTimerQueuesSanityChecks(pVM, "tmSchedule");
373#endif
374 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
375 PDMCritSectLeave(pVM, &pQueue->TimerLock);
376 return;
377 }
378
379 TMTIMERSTATE enmState = pTimer->enmState;
380 if (TMTIMERSTATE_IS_PENDING_SCHEDULING(enmState))
381 tmScheduleNotify(pVM);
382}
383
384
385/**
386 * Try change the state to enmStateNew from enmStateOld
387 * and link the timer into the scheduling queue.
388 *
389 * @returns Success indicator.
390 * @param pTimer Timer in question.
391 * @param enmStateNew The new timer state.
392 * @param enmStateOld The old timer state.
393 */
394DECLINLINE(bool) tmTimerTry(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
395{
396 /*
397 * Attempt state change.
398 */
399 bool fRc;
400 TM_TRY_SET_STATE(pTimer, enmStateNew, enmStateOld, fRc);
401 return fRc;
402}
403
404
405/**
406 * Links the timer onto the scheduling queue.
407 *
408 * @param pQueueCC The current context queue (same as @a pQueue for
409 * ring-3).
410 * @param pQueue The shared queue data.
411 * @param pTimer The timer.
412 *
413 * @todo FIXME: Look into potential race with the thread running the queues
414 * and stuff.
415 */
416DECLINLINE(void) tmTimerLinkSchedule(PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
417{
418 Assert(pTimer->idxScheduleNext == UINT32_MAX);
419 const uint32_t idxHeadNew = pTimer - &pQueueCC->paTimers[0];
420 AssertReturnVoid(idxHeadNew < pQueueCC->cTimersAlloc);
421
422 uint32_t idxHead;
423 do
424 {
425 idxHead = pQueue->idxSchedule;
426 Assert(idxHead == UINT32_MAX || idxHead < pQueueCC->cTimersAlloc);
427 pTimer->idxScheduleNext = idxHead;
428 } while (!ASMAtomicCmpXchgU32(&pQueue->idxSchedule, idxHeadNew, idxHead));
429}
430
431
432/**
433 * Try change the state to enmStateNew from enmStateOld
434 * and link the timer into the scheduling queue.
435 *
436 * @returns Success indicator.
437 * @param pQueueCC The current context queue (same as @a pQueue for
438 * ring-3).
439 * @param pQueue The shared queue data.
440 * @param pTimer Timer in question.
441 * @param enmStateNew The new timer state.
442 * @param enmStateOld The old timer state.
443 */
444DECLINLINE(bool) tmTimerTryWithLink(PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue, PTMTIMER pTimer,
445 TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
446{
447 if (tmTimerTry(pTimer, enmStateNew, enmStateOld))
448 {
449 tmTimerLinkSchedule(pQueueCC, pQueue, pTimer);
450 return true;
451 }
452 return false;
453}
454
455
456/**
457 * Links a timer into the active list of a timer queue.
458 *
459 * @param pVM The cross context VM structure.
460 * @param pQueueCC The current context queue (same as @a pQueue for
461 * ring-3).
462 * @param pQueue The shared queue data.
463 * @param pTimer The timer.
464 * @param u64Expire The timer expiration time.
465 *
466 * @remarks Called while owning the relevant queue lock.
467 */
468DECL_FORCE_INLINE(void) tmTimerQueueLinkActive(PVMCC pVM, PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue,
469 PTMTIMER pTimer, uint64_t u64Expire)
470{
471 Assert(pTimer->idxNext == UINT32_MAX);
472 Assert(pTimer->idxPrev == UINT32_MAX);
473 Assert(pTimer->enmState == TMTIMERSTATE_ACTIVE || pQueue->enmClock != TMCLOCK_VIRTUAL_SYNC); /* (active is not a stable state) */
474 RT_NOREF(pVM);
475
476 PTMTIMER pCur = tmTimerQueueGetHead(pQueueCC, pQueue);
477 if (pCur)
478 {
479 for (;; pCur = tmTimerGetNext(pQueueCC, pCur))
480 {
481 if (pCur->u64Expire > u64Expire)
482 {
483 const PTMTIMER pPrev = tmTimerGetPrev(pQueueCC, pCur);
484 tmTimerSetNext(pQueueCC, pTimer, pCur);
485 tmTimerSetPrev(pQueueCC, pTimer, pPrev);
486 if (pPrev)
487 tmTimerSetNext(pQueueCC, pPrev, pTimer);
488 else
489 {
490 tmTimerQueueSetHead(pQueueCC, pQueue, pTimer);
491 ASMAtomicWriteU64(&pQueue->u64Expire, u64Expire);
492 DBGFTRACE_U64_TAG2(pVM, u64Expire, "tmTimerQueueLinkActive head", pTimer->szName);
493 }
494 tmTimerSetPrev(pQueueCC, pCur, pTimer);
495 return;
496 }
497 if (pCur->idxNext == UINT32_MAX)
498 {
499 tmTimerSetNext(pQueueCC, pCur, pTimer);
500 tmTimerSetPrev(pQueueCC, pTimer, pCur);
501 DBGFTRACE_U64_TAG2(pVM, u64Expire, "tmTimerQueueLinkActive tail", pTimer->szName);
502 return;
503 }
504 }
505 }
506 else
507 {
508 tmTimerQueueSetHead(pQueueCC, pQueue, pTimer);
509 ASMAtomicWriteU64(&pQueue->u64Expire, u64Expire);
510 DBGFTRACE_U64_TAG2(pVM, u64Expire, "tmTimerQueueLinkActive empty", pTimer->szName);
511 }
512}
513
514
515
516/**
517 * Schedules the given timer on the given queue.
518 *
519 * @param pVM The cross context VM structure.
520 * @param pQueueCC The current context queue (same as @a pQueue for
521 * ring-3).
522 * @param pQueue The shared queue data.
523 * @param pTimer The timer that needs scheduling.
524 *
525 * @remarks Called while owning the lock.
526 */
527DECLINLINE(void) tmTimerQueueScheduleOne(PVMCC pVM, PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
528{
529 Assert(pQueue->enmClock != TMCLOCK_VIRTUAL_SYNC);
530 RT_NOREF(pVM);
531
532 /*
533 * Processing.
534 */
535 unsigned cRetries = 2;
536 do
537 {
538 TMTIMERSTATE enmState = pTimer->enmState;
539 switch (enmState)
540 {
541 /*
542 * Reschedule timer (in the active list).
543 */
544 case TMTIMERSTATE_PENDING_RESCHEDULE:
545 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE, TMTIMERSTATE_PENDING_RESCHEDULE)))
546 break; /* retry */
547 tmTimerQueueUnlinkActive(pVM, pQueueCC, pQueue, pTimer);
548 RT_FALL_THRU();
549
550 /*
551 * Schedule timer (insert into the active list).
552 */
553 case TMTIMERSTATE_PENDING_SCHEDULE:
554 Assert(pTimer->idxNext == UINT32_MAX); Assert(pTimer->idxPrev == UINT32_MAX);
555 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, TMTIMERSTATE_PENDING_SCHEDULE)))
556 break; /* retry */
557 tmTimerQueueLinkActive(pVM, pQueueCC, pQueue, pTimer, pTimer->u64Expire);
558 return;
559
560 /*
561 * Stop the timer in active list.
562 */
563 case TMTIMERSTATE_PENDING_STOP:
564 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, TMTIMERSTATE_PENDING_STOP)))
565 break; /* retry */
566 tmTimerQueueUnlinkActive(pVM, pQueueCC, pQueue, pTimer);
567 RT_FALL_THRU();
568
569 /*
570 * Stop the timer (not on the active list).
571 */
572 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
573 Assert(pTimer->idxNext == UINT32_MAX); Assert(pTimer->idxPrev == UINT32_MAX);
574 if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_PENDING_STOP_SCHEDULE)))
575 break;
576 return;
577
578 /*
579 * The timer is pending destruction by TMR3TimerDestroy, our caller.
580 * Nothing to do here.
581 */
582 case TMTIMERSTATE_DESTROY:
583 break;
584
585 /*
586 * Postpone these until they get into the right state.
587 */
588 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
589 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
590 tmTimerLinkSchedule(pQueueCC, pQueue, pTimer);
591 STAM_COUNTER_INC(&pVM->tm.s.CTX_SUFF_Z(StatPostponed));
592 return;
593
594 /*
595 * None of these can be in the schedule.
596 */
597 case TMTIMERSTATE_FREE:
598 case TMTIMERSTATE_STOPPED:
599 case TMTIMERSTATE_ACTIVE:
600 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
601 case TMTIMERSTATE_EXPIRED_DELIVER:
602 default:
603 AssertMsgFailed(("Timer (%p) in the scheduling list has an invalid state %s (%d)!",
604 pTimer, tmTimerState(pTimer->enmState), pTimer->enmState));
605 return;
606 }
607 } while (cRetries-- > 0);
608}
609
610
611/**
612 * Schedules the specified timer queue.
613 *
614 * @param pVM The cross context VM structure.
615 * @param pQueueCC The current context queue (same as @a pQueue for
616 * ring-3) data of the queue to schedule.
617 * @param pQueue The shared queue data of the queue to schedule.
618 *
619 * @remarks Called while owning the lock.
620 */
621void tmTimerQueueSchedule(PVMCC pVM, PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue)
622{
623 Assert(PDMCritSectIsOwner(pVM, &pQueue->TimerLock));
624
625 /*
626 * Dequeue the scheduling list and iterate it.
627 */
628 uint32_t idxNext = ASMAtomicXchgU32(&pQueue->idxSchedule, UINT32_MAX);
629 Log2(("tmTimerQueueSchedule: pQueue=%p:{.enmClock=%d, idxNext=%RI32, .u64Expired=%'RU64}\n", pQueue, pQueue->enmClock, idxNext, pQueue->u64Expire));
630 while (idxNext != UINT32_MAX)
631 {
632 AssertBreak(idxNext < pQueueCC->cTimersAlloc);
633
634 /*
635 * Unlink the head timer and take down the index of the next one.
636 */
637 PTMTIMER pTimer = &pQueueCC->paTimers[idxNext];
638 idxNext = pTimer->idxScheduleNext;
639 pTimer->idxScheduleNext = UINT32_MAX;
640
641 /*
642 * Do the scheduling.
643 */
644 Log2(("tmTimerQueueSchedule: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, .szName=%s}\n",
645 pTimer, tmTimerState(pTimer->enmState), pQueue->enmClock, pTimer->enmType, pTimer->szName));
646 tmTimerQueueScheduleOne(pVM, pQueueCC, pQueue, pTimer);
647 Log2(("tmTimerQueueSchedule: %p: new %s\n", pTimer, tmTimerState(pTimer->enmState)));
648 }
649 Log2(("tmTimerQueueSchedule: u64Expired=%'RU64\n", pQueue->u64Expire));
650}
651
652
653#ifdef VBOX_STRICT
654/**
655 * Checks that the timer queues are sane.
656 *
657 * @param pVM The cross context VM structure.
658 * @param pszWhere Caller location clue.
659 */
660void tmTimerQueuesSanityChecks(PVMCC pVM, const char *pszWhere)
661{
662 for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
663 {
664 PTMTIMERQUEUE const pQueue = &pVM->tm.s.aTimerQueues[idxQueue];
665 PTMTIMERQUEUECC const pQueueCC = TM_GET_TIMER_QUEUE_CC(pVM, idxQueue, pQueue);
666 Assert(pQueue->enmClock == (TMCLOCK)idxQueue);
667
668 int rc = PDMCritSectTryEnter(pVM, &pQueue->TimerLock);
669 if (RT_SUCCESS(rc))
670 {
671 if ( pQueue->enmClock != TMCLOCK_VIRTUAL_SYNC
672 || PDMCritSectTryEnter(pVM, &pVM->tm.s.VirtualSyncLock) == VINF_SUCCESS)
673 {
674 /* Check the linking of the active lists. */
675 PTMTIMER pPrev = NULL;
676 for (PTMTIMER pCur = tmTimerQueueGetHead(pQueueCC, pQueue);
677 pCur;
678 pPrev = pCur, pCur = tmTimerGetNext(pQueueCC, pCur))
679 {
680 AssertMsg(tmTimerGetPrev(pQueueCC, pCur) == pPrev, ("%s: %p != %p\n", pszWhere, tmTimerGetPrev(pQueueCC, pCur), pPrev));
681 TMTIMERSTATE enmState = pCur->enmState;
682 switch (enmState)
683 {
684 case TMTIMERSTATE_ACTIVE:
685 AssertMsg( pCur->idxScheduleNext == UINT32_MAX
686 || pCur->enmState != TMTIMERSTATE_ACTIVE,
687 ("%s: %RI32\n", pszWhere, pCur->idxScheduleNext));
688 break;
689 case TMTIMERSTATE_PENDING_STOP:
690 case TMTIMERSTATE_PENDING_RESCHEDULE:
691 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
692 break;
693 default:
694 AssertMsgFailed(("%s: Invalid state enmState=%d %s\n", pszWhere, enmState, tmTimerState(enmState)));
695 break;
696 }
697 }
698
699# ifdef IN_RING3
700 /* Go thru all the timers and check that the active ones all are in the active lists. */
701 uint32_t idxTimer = pQueue->cTimersAlloc;
702 uint32_t cFree = 0;
703 while (idxTimer-- > 0)
704 {
705 PTMTIMER const pTimer = &pQueue->paTimers[idxTimer];
706 TMTIMERSTATE const enmState = pTimer->enmState;
707 switch (enmState)
708 {
709 case TMTIMERSTATE_FREE:
710 cFree++;
711 break;
712
713 case TMTIMERSTATE_ACTIVE:
714 case TMTIMERSTATE_PENDING_STOP:
715 case TMTIMERSTATE_PENDING_RESCHEDULE:
716 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
717 {
718 PTMTIMERR3 pCurAct = tmTimerQueueGetHead(pQueueCC, pQueue);
719 Assert(pTimer->idxPrev != UINT32_MAX || pTimer == pCurAct);
720 while (pCurAct && pCurAct != pTimer)
721 pCurAct = tmTimerGetNext(pQueueCC, pCurAct);
722 Assert(pCurAct == pTimer);
723 break;
724 }
725
726 case TMTIMERSTATE_PENDING_SCHEDULE:
727 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
728 case TMTIMERSTATE_STOPPED:
729 case TMTIMERSTATE_EXPIRED_DELIVER:
730 {
731 Assert(pTimer->idxNext == UINT32_MAX);
732 Assert(pTimer->idxPrev == UINT32_MAX);
733 for (PTMTIMERR3 pCurAct = tmTimerQueueGetHead(pQueueCC, pQueue);
734 pCurAct;
735 pCurAct = tmTimerGetNext(pQueueCC, pCurAct))
736 {
737 Assert(pCurAct != pTimer);
738 Assert(tmTimerGetNext(pQueueCC, pCurAct) != pTimer);
739 Assert(tmTimerGetPrev(pQueueCC, pCurAct) != pTimer);
740 }
741 break;
742 }
743
744 /* ignore */
745 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
746 break;
747
748 case TMTIMERSTATE_INVALID:
749 Assert(idxTimer == 0);
750 break;
751
752 /* shouldn't get here! */
753 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
754 case TMTIMERSTATE_DESTROY:
755 default:
756 AssertMsgFailed(("Invalid state enmState=%d %s\n", enmState, tmTimerState(enmState)));
757 break;
758 }
759
760 /* Check the handle value. */
761 if (enmState > TMTIMERSTATE_INVALID && enmState < TMTIMERSTATE_DESTROY)
762 {
763 Assert((pTimer->hSelf & TMTIMERHANDLE_TIMER_IDX_MASK) == idxTimer);
764 Assert(((pTimer->hSelf >> TMTIMERHANDLE_QUEUE_IDX_SHIFT) & TMTIMERHANDLE_QUEUE_IDX_SMASK) == idxQueue);
765 }
766 }
767 Assert(cFree == pQueue->cTimersFree);
768# endif /* IN_RING3 */
769
770 if (pQueue->enmClock == TMCLOCK_VIRTUAL_SYNC)
771 PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
772 }
773 PDMCritSectLeave(pVM, &pQueue->TimerLock);
774 }
775 }
776}
777#endif /* !VBOX_STRICT */
778
779#ifdef VBOX_HIGH_RES_TIMERS_HACK
780
781/**
782 * Worker for tmTimerPollInternal that handles misses when the dedicated timer
783 * EMT is polling.
784 *
785 * @returns See tmTimerPollInternal.
786 * @param pVM The cross context VM structure.
787 * @param u64Now Current virtual clock timestamp.
788 * @param u64Delta The delta to the next even in ticks of the
789 * virtual clock.
790 * @param pu64Delta Where to return the delta.
791 */
792DECLINLINE(uint64_t) tmTimerPollReturnMiss(PVM pVM, uint64_t u64Now, uint64_t u64Delta, uint64_t *pu64Delta)
793{
794 Assert(!(u64Delta & RT_BIT_64(63)));
795
796 if (!pVM->tm.s.fVirtualWarpDrive)
797 {
798 *pu64Delta = u64Delta;
799 return u64Delta + u64Now + pVM->tm.s.u64VirtualOffset;
800 }
801
802 /*
803 * Warp drive adjustments - this is the reverse of what tmVirtualGetRaw is doing.
804 */
805 uint64_t const u64Start = pVM->tm.s.u64VirtualWarpDriveStart;
806 uint32_t const u32Pct = pVM->tm.s.u32VirtualWarpDrivePercentage;
807
808 uint64_t u64GipTime = u64Delta + u64Now + pVM->tm.s.u64VirtualOffset;
809 u64GipTime -= u64Start; /* the start is GIP time. */
810 if (u64GipTime >= u64Delta)
811 {
812 ASMMultU64ByU32DivByU32(u64GipTime, 100, u32Pct);
813 ASMMultU64ByU32DivByU32(u64Delta, 100, u32Pct);
814 }
815 else
816 {
817 u64Delta -= u64GipTime;
818 ASMMultU64ByU32DivByU32(u64GipTime, 100, u32Pct);
819 u64Delta += u64GipTime;
820 }
821 *pu64Delta = u64Delta;
822 u64GipTime += u64Start;
823 return u64GipTime;
824}
825
826
827/**
828 * Worker for tmTimerPollInternal dealing with returns on virtual CPUs other
829 * than the one dedicated to timer work.
830 *
831 * @returns See tmTimerPollInternal.
832 * @param pVM The cross context VM structure.
833 * @param u64Now Current virtual clock timestamp.
834 * @param pu64Delta Where to return the delta.
835 */
836DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnOtherCpu(PVM pVM, uint64_t u64Now, uint64_t *pu64Delta)
837{
838 static const uint64_t s_u64OtherRet = 500000000; /* 500 ms for non-timer EMTs. */
839 *pu64Delta = s_u64OtherRet;
840 return u64Now + pVM->tm.s.u64VirtualOffset + s_u64OtherRet;
841}
842
843
844/**
845 * Worker for tmTimerPollInternal.
846 *
847 * @returns See tmTimerPollInternal.
848 * @param pVM The cross context VM structure.
849 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
850 * @param pVCpuDst The cross context virtual CPU structure of the dedicated
851 * timer EMT.
852 * @param u64Now Current virtual clock timestamp.
853 * @param pu64Delta Where to return the delta.
854 * @param pCounter The statistics counter to update.
855 */
856DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnHit(PVM pVM, PVMCPU pVCpu, PVMCPU pVCpuDst, uint64_t u64Now,
857 uint64_t *pu64Delta, PSTAMCOUNTER pCounter)
858{
859 STAM_COUNTER_INC(pCounter); NOREF(pCounter);
860 if (pVCpuDst != pVCpu)
861 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
862 *pu64Delta = 0;
863 return 0;
864}
865
866
867/**
868 * Common worker for TMTimerPollGIP and TMTimerPoll.
869 *
870 * This function is called before FFs are checked in the inner execution EM loops.
871 *
872 * @returns The GIP timestamp of the next event.
873 * 0 if the next event has already expired.
874 *
875 * @param pVM The cross context VM structure.
876 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
877 * @param pu64Delta Where to store the delta.
878 *
879 * @thread The emulation thread.
880 *
881 * @remarks GIP uses ns ticks.
882 */
883DECL_FORCE_INLINE(uint64_t) tmTimerPollInternal(PVMCC pVM, PVMCPUCC pVCpu, uint64_t *pu64Delta)
884{
885 VMCPUID idCpu = pVM->tm.s.idTimerCpu;
886 AssertReturn(idCpu < pVM->cCpus, 0);
887 PVMCPUCC pVCpuDst = VMCC_GET_CPU(pVM, idCpu);
888
889 const uint64_t u64Now = TMVirtualGetNoCheck(pVM);
890 STAM_COUNTER_INC(&pVM->tm.s.StatPoll);
891
892 /*
893 * Return straight away if the timer FF is already set ...
894 */
895 if (VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
896 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
897
898 /*
899 * ... or if timers are being run.
900 */
901 if (ASMAtomicReadBool(&pVM->tm.s.fRunningQueues))
902 {
903 STAM_COUNTER_INC(&pVM->tm.s.StatPollRunning);
904 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
905 }
906
907 /*
908 * Check for TMCLOCK_VIRTUAL expiration.
909 */
910 const uint64_t u64Expire1 = ASMAtomicReadU64(&pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL].u64Expire);
911 const int64_t i64Delta1 = u64Expire1 - u64Now;
912 if (i64Delta1 <= 0)
913 {
914 if (!VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
915 {
916 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)));
917 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
918 }
919 LogFlow(("TMTimerPoll: expire1=%'RU64 <= now=%'RU64\n", u64Expire1, u64Now));
920 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtual);
921 }
922
923 /*
924 * Check for TMCLOCK_VIRTUAL_SYNC expiration.
925 * This isn't quite as straight forward if in a catch-up, not only do
926 * we have to adjust the 'now' but when have to adjust the delta as well.
927 */
928
929 /*
930 * Optimistic lockless approach.
931 */
932 uint64_t u64VirtualSyncNow;
933 uint64_t u64Expire2 = ASMAtomicUoReadU64(&pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].u64Expire);
934 if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
935 {
936 if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
937 {
938 u64VirtualSyncNow = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
939 if (RT_LIKELY( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
940 && !ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
941 && u64VirtualSyncNow == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
942 && u64Expire2 == ASMAtomicUoReadU64(&pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].u64Expire)))
943 {
944 u64VirtualSyncNow = u64Now - u64VirtualSyncNow;
945 int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
946 if (i64Delta2 > 0)
947 {
948 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
949 STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
950
951 if (pVCpu == pVCpuDst)
952 return tmTimerPollReturnMiss(pVM, u64Now, RT_MIN(i64Delta1, i64Delta2), pu64Delta);
953 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
954 }
955
956 if ( !pVM->tm.s.fRunningQueues
957 && !VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
958 {
959 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)));
960 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
961 }
962
963 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
964 LogFlow(("TMTimerPoll: expire2=%'RU64 <= now=%'RU64\n", u64Expire2, u64Now));
965 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
966 }
967 }
968 }
969 else
970 {
971 STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
972 LogFlow(("TMTimerPoll: stopped\n"));
973 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
974 }
975
976 /*
977 * Complicated lockless approach.
978 */
979 uint64_t off;
980 uint32_t u32Pct = 0;
981 bool fCatchUp;
982 int cOuterTries = 42;
983 for (;; cOuterTries--)
984 {
985 fCatchUp = ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp);
986 off = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
987 u64Expire2 = ASMAtomicReadU64(&pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].u64Expire);
988 if (fCatchUp)
989 {
990 /* No changes allowed, try get a consistent set of parameters. */
991 uint64_t const u64Prev = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev);
992 uint64_t const offGivenUp = ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp);
993 u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage);
994 if ( ( u64Prev == ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev)
995 && offGivenUp == ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp)
996 && u32Pct == ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage)
997 && off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
998 && u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].u64Expire)
999 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
1000 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
1001 || cOuterTries <= 0)
1002 {
1003 uint64_t u64Delta = u64Now - u64Prev;
1004 if (RT_LIKELY(!(u64Delta >> 32)))
1005 {
1006 uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, u32Pct, 100);
1007 if (off > u64Sub + offGivenUp)
1008 off -= u64Sub;
1009 else /* we've completely caught up. */
1010 off = offGivenUp;
1011 }
1012 else
1013 /* More than 4 seconds since last time (or negative), ignore it. */
1014 Log(("TMVirtualGetSync: u64Delta=%RX64 (NoLock)\n", u64Delta));
1015
1016 /* Check that we're still running and in catch up. */
1017 if ( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
1018 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
1019 break;
1020 }
1021 }
1022 else if ( off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
1023 && u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].u64Expire)
1024 && !ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
1025 && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
1026 break; /* Got an consistent offset */
1027
1028 /* Repeat the initial checks before iterating. */
1029 if (VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
1030 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
1031 if (ASMAtomicUoReadBool(&pVM->tm.s.fRunningQueues))
1032 {
1033 STAM_COUNTER_INC(&pVM->tm.s.StatPollRunning);
1034 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
1035 }
1036 if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
1037 {
1038 LogFlow(("TMTimerPoll: stopped\n"));
1039 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
1040 }
1041 if (cOuterTries <= 0)
1042 break; /* that's enough */
1043 }
1044 if (cOuterTries <= 0)
1045 STAM_COUNTER_INC(&pVM->tm.s.StatPollELoop);
1046 u64VirtualSyncNow = u64Now - off;
1047
1048 /* Calc delta and see if we've got a virtual sync hit. */
1049 int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
1050 if (i64Delta2 <= 0)
1051 {
1052 if ( !pVM->tm.s.fRunningQueues
1053 && !VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
1054 {
1055 Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)));
1056 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
1057 }
1058 STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtualSync);
1059 LogFlow(("TMTimerPoll: expire2=%'RU64 <= now=%'RU64\n", u64Expire2, u64Now));
1060 return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
1061 }
1062
1063 /*
1064 * Return the time left to the next event.
1065 */
1066 STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
1067 if (pVCpu == pVCpuDst)
1068 {
1069 if (fCatchUp)
1070 i64Delta2 = ASMMultU64ByU32DivByU32(i64Delta2, 100, u32Pct + 100);
1071 return tmTimerPollReturnMiss(pVM, u64Now, RT_MIN(i64Delta1, i64Delta2), pu64Delta);
1072 }
1073 return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
1074}
1075
1076
1077/**
1078 * Set FF if we've passed the next virtual event.
1079 *
1080 * This function is called before FFs are checked in the inner execution EM loops.
1081 *
1082 * @returns true if timers are pending, false if not.
1083 *
1084 * @param pVM The cross context VM structure.
1085 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1086 * @thread The emulation thread.
1087 */
1088VMMDECL(bool) TMTimerPollBool(PVMCC pVM, PVMCPUCC pVCpu)
1089{
1090 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
1091 uint64_t off = 0;
1092 tmTimerPollInternal(pVM, pVCpu, &off);
1093 return off == 0;
1094}
1095
1096
1097/**
1098 * Set FF if we've passed the next virtual event.
1099 *
1100 * This function is called before FFs are checked in the inner execution EM loops.
1101 *
1102 * @param pVM The cross context VM structure.
1103 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1104 * @thread The emulation thread.
1105 */
1106VMM_INT_DECL(void) TMTimerPollVoid(PVMCC pVM, PVMCPUCC pVCpu)
1107{
1108 uint64_t off;
1109 tmTimerPollInternal(pVM, pVCpu, &off);
1110}
1111
1112
1113/**
1114 * Set FF if we've passed the next virtual event.
1115 *
1116 * This function is called before FFs are checked in the inner execution EM loops.
1117 *
1118 * @returns The GIP timestamp of the next event.
1119 * 0 if the next event has already expired.
1120 * @param pVM The cross context VM structure.
1121 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1122 * @param pu64Delta Where to store the delta.
1123 * @thread The emulation thread.
1124 */
1125VMM_INT_DECL(uint64_t) TMTimerPollGIP(PVMCC pVM, PVMCPUCC pVCpu, uint64_t *pu64Delta)
1126{
1127 return tmTimerPollInternal(pVM, pVCpu, pu64Delta);
1128}
1129
1130#endif /* VBOX_HIGH_RES_TIMERS_HACK */
1131
1132/**
1133 * Locks the timer clock.
1134 *
1135 * @returns VINF_SUCCESS on success, @a rcBusy if busy, and VERR_NOT_SUPPORTED
1136 * if the clock does not have a lock.
1137 * @param pVM The cross context VM structure.
1138 * @param hTimer Timer handle as returned by one of the create functions.
1139 * @param rcBusy What to return in ring-0 and raw-mode context if the
1140 * lock is busy. Pass VINF_SUCCESS to acquired the
1141 * critical section thru a ring-3 call if necessary.
1142 *
1143 * @remarks Currently only supported on timers using the virtual sync clock.
1144 */
1145VMMDECL(int) TMTimerLock(PVMCC pVM, TMTIMERHANDLE hTimer, int rcBusy)
1146{
1147 TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
1148 AssertReturn(idxQueue == TMCLOCK_VIRTUAL_SYNC, VERR_NOT_SUPPORTED);
1149 return PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, rcBusy);
1150}
1151
1152
1153/**
1154 * Unlocks a timer clock locked by TMTimerLock.
1155 *
1156 * @param pVM The cross context VM structure.
1157 * @param hTimer Timer handle as returned by one of the create functions.
1158 */
1159VMMDECL(void) TMTimerUnlock(PVMCC pVM, TMTIMERHANDLE hTimer)
1160{
1161 TMTIMER_HANDLE_TO_VARS_RETURN_VOID(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
1162 AssertReturnVoid(idxQueue == TMCLOCK_VIRTUAL_SYNC);
1163 PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
1164}
1165
1166
1167/**
1168 * Checks if the current thread owns the timer clock lock.
1169 *
1170 * @returns @c true if its the owner, @c false if not.
1171 * @param pVM The cross context VM structure.
1172 * @param hTimer Timer handle as returned by one of the create functions.
1173 */
1174VMMDECL(bool) TMTimerIsLockOwner(PVMCC pVM, TMTIMERHANDLE hTimer)
1175{
1176 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, false); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
1177 AssertReturn(idxQueue == TMCLOCK_VIRTUAL_SYNC, false);
1178 return PDMCritSectIsOwner(pVM, &pVM->tm.s.VirtualSyncLock);
1179}
1180
1181
1182/**
1183 * Optimized TMTimerSet code path for starting an inactive timer.
1184 *
1185 * @returns VBox status code.
1186 *
1187 * @param pVM The cross context VM structure.
1188 * @param pTimer The timer handle.
1189 * @param u64Expire The new expire time.
1190 * @param pQueue Pointer to the shared timer queue data.
1191 * @param idxQueue The queue index.
1192 */
1193static int tmTimerSetOptimizedStart(PVMCC pVM, PTMTIMER pTimer, uint64_t u64Expire, PTMTIMERQUEUE pQueue, uint32_t idxQueue)
1194{
1195 Assert(pTimer->idxPrev == UINT32_MAX);
1196 Assert(pTimer->idxNext == UINT32_MAX);
1197 Assert(pTimer->enmState == TMTIMERSTATE_ACTIVE);
1198
1199 /*
1200 * Calculate and set the expiration time.
1201 */
1202 if (idxQueue == TMCLOCK_VIRTUAL_SYNC)
1203 {
1204 uint64_t u64Last = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSync);
1205 AssertMsgStmt(u64Expire >= u64Last,
1206 ("exp=%#llx last=%#llx\n", u64Expire, u64Last),
1207 u64Expire = u64Last);
1208 }
1209 ASMAtomicWriteU64(&pTimer->u64Expire, u64Expire);
1210 Log2(("tmTimerSetOptimizedStart: %p:{.pszDesc='%s', .u64Expire=%'RU64}\n", pTimer, pTimer->szName, u64Expire));
1211
1212 /*
1213 * Link the timer into the active list.
1214 */
1215 tmTimerQueueLinkActive(pVM, TM_GET_TIMER_QUEUE_CC(pVM, idxQueue, pQueue), pQueue, pTimer, u64Expire);
1216
1217 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetOpt);
1218 return VINF_SUCCESS;
1219}
1220
1221
1222/**
1223 * TMTimerSet for the virtual sync timer queue.
1224 *
1225 * This employs a greatly simplified state machine by always acquiring the
1226 * queue lock and bypassing the scheduling list.
1227 *
1228 * @returns VBox status code
1229 * @param pVM The cross context VM structure.
1230 * @param pTimer The timer handle.
1231 * @param u64Expire The expiration time.
1232 */
1233static int tmTimerVirtualSyncSet(PVMCC pVM, PTMTIMER pTimer, uint64_t u64Expire)
1234{
1235 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetVs), a);
1236 VM_ASSERT_EMT(pVM);
1237 TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer);
1238 int rc = PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VINF_SUCCESS);
1239 AssertRCReturn(rc, rc);
1240
1241 PTMTIMERQUEUE const pQueue = &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC];
1242 PTMTIMERQUEUECC const pQueueCC = TM_GET_TIMER_QUEUE_CC(pVM, TMCLOCK_VIRTUAL_SYNC, pQueue);
1243 TMTIMERSTATE const enmState = pTimer->enmState;
1244 switch (enmState)
1245 {
1246 case TMTIMERSTATE_EXPIRED_DELIVER:
1247 case TMTIMERSTATE_STOPPED:
1248 if (enmState == TMTIMERSTATE_EXPIRED_DELIVER)
1249 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetVsStExpDeliver);
1250 else
1251 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetVsStStopped);
1252
1253 AssertMsg(u64Expire >= pVM->tm.s.u64VirtualSync,
1254 ("%'RU64 < %'RU64 %s\n", u64Expire, pVM->tm.s.u64VirtualSync, pTimer->szName));
1255 pTimer->u64Expire = u64Expire;
1256 TM_SET_STATE(pTimer, TMTIMERSTATE_ACTIVE);
1257 tmTimerQueueLinkActive(pVM, pQueueCC, pQueue, pTimer, u64Expire);
1258 rc = VINF_SUCCESS;
1259 break;
1260
1261 case TMTIMERSTATE_ACTIVE:
1262 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetVsStActive);
1263 tmTimerQueueUnlinkActive(pVM, pQueueCC, pQueue, pTimer);
1264 pTimer->u64Expire = u64Expire;
1265 tmTimerQueueLinkActive(pVM, pQueueCC, pQueue, pTimer, u64Expire);
1266 rc = VINF_SUCCESS;
1267 break;
1268
1269 case TMTIMERSTATE_PENDING_RESCHEDULE:
1270 case TMTIMERSTATE_PENDING_STOP:
1271 case TMTIMERSTATE_PENDING_SCHEDULE:
1272 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1273 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1274 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1275 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1276 case TMTIMERSTATE_DESTROY:
1277 case TMTIMERSTATE_FREE:
1278 AssertLogRelMsgFailed(("Invalid timer state %s: %s\n", tmTimerState(enmState), pTimer->szName));
1279 rc = VERR_TM_INVALID_STATE;
1280 break;
1281
1282 default:
1283 AssertMsgFailed(("Unknown timer state %d: %s\n", enmState, pTimer->szName));
1284 rc = VERR_TM_UNKNOWN_STATE;
1285 break;
1286 }
1287
1288 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetVs), a);
1289 PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
1290 return rc;
1291}
1292
1293
1294/**
1295 * Arm a timer with a (new) expire time.
1296 *
1297 * @returns VBox status code.
1298 * @param pVM The cross context VM structure.
1299 * @param hTimer Timer handle as returned by one of the create functions.
1300 * @param u64Expire New expire time.
1301 */
1302VMMDECL(int) TMTimerSet(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t u64Expire)
1303{
1304 TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
1305 STAM_COUNTER_INC(&pTimer->StatSetAbsolute);
1306
1307 /* Treat virtual sync timers specially. */
1308 if (idxQueue == TMCLOCK_VIRTUAL_SYNC)
1309 return tmTimerVirtualSyncSet(pVM, pTimer, u64Expire);
1310
1311 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
1312 TMTIMER_ASSERT_CRITSECT(pVM, pTimer);
1313
1314 DBGFTRACE_U64_TAG2(pVM, u64Expire, "TMTimerSet", pTimer->szName);
1315
1316#ifdef VBOX_WITH_STATISTICS
1317 /*
1318 * Gather optimization info.
1319 */
1320 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSet);
1321 TMTIMERSTATE enmOrgState = pTimer->enmState;
1322 switch (enmOrgState)
1323 {
1324 case TMTIMERSTATE_STOPPED: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStStopped); break;
1325 case TMTIMERSTATE_EXPIRED_DELIVER: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStExpDeliver); break;
1326 case TMTIMERSTATE_ACTIVE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStActive); break;
1327 case TMTIMERSTATE_PENDING_STOP: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendStop); break;
1328 case TMTIMERSTATE_PENDING_STOP_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendStopSched); break;
1329 case TMTIMERSTATE_PENDING_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendSched); break;
1330 case TMTIMERSTATE_PENDING_RESCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendResched); break;
1331 default: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStOther); break;
1332 }
1333#endif
1334
1335#if 1
1336 /*
1337 * The most common case is setting the timer again during the callback.
1338 * The second most common case is starting a timer at some other time.
1339 */
1340 TMTIMERSTATE enmState1 = pTimer->enmState;
1341 if ( enmState1 == TMTIMERSTATE_EXPIRED_DELIVER
1342 || ( enmState1 == TMTIMERSTATE_STOPPED
1343 && pTimer->pCritSect))
1344 {
1345 /* Try take the TM lock and check the state again. */
1346 int rc = PDMCritSectTryEnter(pVM, &pQueue->TimerLock);
1347 if (RT_SUCCESS_NP(rc))
1348 {
1349 if (RT_LIKELY(tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, enmState1)))
1350 {
1351 tmTimerSetOptimizedStart(pVM, pTimer, u64Expire, pQueue, idxQueue);
1352 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
1353 PDMCritSectLeave(pVM, &pQueue->TimerLock);
1354 return VINF_SUCCESS;
1355 }
1356 PDMCritSectLeave(pVM, &pQueue->TimerLock);
1357 }
1358 }
1359#endif
1360
1361 /*
1362 * Unoptimized code path.
1363 */
1364 int cRetries = 1000;
1365 do
1366 {
1367 /*
1368 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
1369 */
1370 TMTIMERSTATE enmState = pTimer->enmState;
1371 Log2(("TMTimerSet: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d u64Expire=%'RU64\n",
1372 pTimer, tmTimerState(enmState), pTimer->szName, cRetries, u64Expire));
1373 switch (enmState)
1374 {
1375 case TMTIMERSTATE_EXPIRED_DELIVER:
1376 case TMTIMERSTATE_STOPPED:
1377 if (tmTimerTryWithLink(pQueueCC, pQueue, pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
1378 {
1379 Assert(pTimer->idxPrev == UINT32_MAX);
1380 Assert(pTimer->idxNext == UINT32_MAX);
1381 pTimer->u64Expire = u64Expire;
1382 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
1383 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
1384 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
1385 return VINF_SUCCESS;
1386 }
1387 break;
1388
1389 case TMTIMERSTATE_PENDING_SCHEDULE:
1390 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1391 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
1392 {
1393 pTimer->u64Expire = u64Expire;
1394 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
1395 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
1396 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
1397 return VINF_SUCCESS;
1398 }
1399 break;
1400
1401
1402 case TMTIMERSTATE_ACTIVE:
1403 if (tmTimerTryWithLink(pQueueCC, pQueue, pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
1404 {
1405 pTimer->u64Expire = u64Expire;
1406 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
1407 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
1408 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
1409 return VINF_SUCCESS;
1410 }
1411 break;
1412
1413 case TMTIMERSTATE_PENDING_RESCHEDULE:
1414 case TMTIMERSTATE_PENDING_STOP:
1415 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
1416 {
1417 pTimer->u64Expire = u64Expire;
1418 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
1419 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
1420 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
1421 return VINF_SUCCESS;
1422 }
1423 break;
1424
1425
1426 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1427 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1428 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1429#ifdef IN_RING3
1430 if (!RTThreadYield())
1431 RTThreadSleep(1);
1432#else
1433/** @todo call host context and yield after a couple of iterations */
1434#endif
1435 break;
1436
1437 /*
1438 * Invalid states.
1439 */
1440 case TMTIMERSTATE_DESTROY:
1441 case TMTIMERSTATE_FREE:
1442 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, pTimer->szName));
1443 return VERR_TM_INVALID_STATE;
1444 default:
1445 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, pTimer->szName));
1446 return VERR_TM_UNKNOWN_STATE;
1447 }
1448 } while (cRetries-- > 0);
1449
1450 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->szName));
1451 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
1452 return VERR_TM_TIMER_UNSTABLE_STATE;
1453}
1454
1455
1456/**
1457 * Return the current time for the specified clock, setting pu64Now if not NULL.
1458 *
1459 * @returns Current time.
1460 * @param pVM The cross context VM structure.
1461 * @param enmClock The clock to query.
1462 * @param pu64Now Optional pointer where to store the return time
1463 */
1464DECL_FORCE_INLINE(uint64_t) tmTimerSetRelativeNowWorker(PVMCC pVM, TMCLOCK enmClock, uint64_t *pu64Now)
1465{
1466 uint64_t u64Now;
1467 switch (enmClock)
1468 {
1469 case TMCLOCK_VIRTUAL_SYNC:
1470 u64Now = TMVirtualSyncGet(pVM);
1471 break;
1472 case TMCLOCK_VIRTUAL:
1473 u64Now = TMVirtualGet(pVM);
1474 break;
1475 case TMCLOCK_REAL:
1476 u64Now = TMRealGet(pVM);
1477 break;
1478 default:
1479 AssertFatalMsgFailed(("%d\n", enmClock));
1480 }
1481
1482 if (pu64Now)
1483 *pu64Now = u64Now;
1484 return u64Now;
1485}
1486
1487
1488/**
1489 * Optimized TMTimerSetRelative code path.
1490 *
1491 * @returns VBox status code.
1492 *
1493 * @param pVM The cross context VM structure.
1494 * @param pTimer The timer handle.
1495 * @param cTicksToNext Clock ticks until the next time expiration.
1496 * @param pu64Now Where to return the current time stamp used.
1497 * Optional.
1498 * @param pQueueCC The context specific queue data (same as @a pQueue
1499 * for ring-3).
1500 * @param pQueue The shared queue data.
1501 */
1502static int tmTimerSetRelativeOptimizedStart(PVMCC pVM, PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now,
1503 PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue)
1504{
1505 Assert(pTimer->idxPrev == UINT32_MAX);
1506 Assert(pTimer->idxNext == UINT32_MAX);
1507 Assert(pTimer->enmState == TMTIMERSTATE_ACTIVE);
1508
1509 /*
1510 * Calculate and set the expiration time.
1511 */
1512 uint64_t const u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, pQueue->enmClock, pu64Now);
1513 pTimer->u64Expire = u64Expire;
1514 Log2(("tmTimerSetRelativeOptimizedStart: %p:{.pszDesc='%s', .u64Expire=%'RU64} cTicksToNext=%'RU64\n", pTimer, pTimer->szName, u64Expire, cTicksToNext));
1515
1516 /*
1517 * Link the timer into the active list.
1518 */
1519 DBGFTRACE_U64_TAG2(pVM, u64Expire, "tmTimerSetRelativeOptimizedStart", pTimer->szName);
1520 tmTimerQueueLinkActive(pVM, pQueueCC, pQueue, pTimer, u64Expire);
1521
1522 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeOpt);
1523 return VINF_SUCCESS;
1524}
1525
1526
1527/**
1528 * TMTimerSetRelative for the virtual sync timer queue.
1529 *
1530 * This employs a greatly simplified state machine by always acquiring the
1531 * queue lock and bypassing the scheduling list.
1532 *
1533 * @returns VBox status code
1534 * @param pVM The cross context VM structure.
1535 * @param pTimer The timer to (re-)arm.
1536 * @param cTicksToNext Clock ticks until the next time expiration.
1537 * @param pu64Now Where to return the current time stamp used.
1538 * Optional.
1539 */
1540static int tmTimerVirtualSyncSetRelative(PVMCC pVM, PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now)
1541{
1542 STAM_PROFILE_START(pVM->tm.s.CTX_SUFF_Z(StatTimerSetRelativeVs), a);
1543 VM_ASSERT_EMT(pVM);
1544 TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer);
1545 int rc = PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VINF_SUCCESS);
1546 AssertRCReturn(rc, rc);
1547
1548 /* Calculate the expiration tick. */
1549 uint64_t u64Expire = TMVirtualSyncGetNoCheck(pVM);
1550 if (pu64Now)
1551 *pu64Now = u64Expire;
1552 u64Expire += cTicksToNext;
1553
1554 /* Update the timer. */
1555 PTMTIMERQUEUE const pQueue = &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC];
1556 PTMTIMERQUEUECC const pQueueCC = TM_GET_TIMER_QUEUE_CC(pVM, TMCLOCK_VIRTUAL_SYNC, pQueue);
1557 TMTIMERSTATE const enmState = pTimer->enmState;
1558 switch (enmState)
1559 {
1560 case TMTIMERSTATE_EXPIRED_DELIVER:
1561 case TMTIMERSTATE_STOPPED:
1562 if (enmState == TMTIMERSTATE_EXPIRED_DELIVER)
1563 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeVsStExpDeliver);
1564 else
1565 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeVsStStopped);
1566 pTimer->u64Expire = u64Expire;
1567 TM_SET_STATE(pTimer, TMTIMERSTATE_ACTIVE);
1568 tmTimerQueueLinkActive(pVM, pQueueCC, pQueue, pTimer, u64Expire);
1569 rc = VINF_SUCCESS;
1570 break;
1571
1572 case TMTIMERSTATE_ACTIVE:
1573 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeVsStActive);
1574 tmTimerQueueUnlinkActive(pVM, pQueueCC, pQueue, pTimer);
1575 pTimer->u64Expire = u64Expire;
1576 tmTimerQueueLinkActive(pVM, pQueueCC, pQueue, pTimer, u64Expire);
1577 rc = VINF_SUCCESS;
1578 break;
1579
1580 case TMTIMERSTATE_PENDING_RESCHEDULE:
1581 case TMTIMERSTATE_PENDING_STOP:
1582 case TMTIMERSTATE_PENDING_SCHEDULE:
1583 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1584 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1585 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1586 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1587 case TMTIMERSTATE_DESTROY:
1588 case TMTIMERSTATE_FREE:
1589 AssertLogRelMsgFailed(("Invalid timer state %s: %s\n", tmTimerState(enmState), pTimer->szName));
1590 rc = VERR_TM_INVALID_STATE;
1591 break;
1592
1593 default:
1594 AssertMsgFailed(("Unknown timer state %d: %s\n", enmState, pTimer->szName));
1595 rc = VERR_TM_UNKNOWN_STATE;
1596 break;
1597 }
1598
1599 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetRelativeVs), a);
1600 PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
1601 return rc;
1602}
1603
1604
1605/**
1606 * Arm a timer with a expire time relative to the current time.
1607 *
1608 * @returns VBox status code.
1609 * @param pVM The cross context VM structure.
1610 * @param pTimer The timer to arm.
1611 * @param cTicksToNext Clock ticks until the next time expiration.
1612 * @param pu64Now Where to return the current time stamp used.
1613 * Optional.
1614 * @param pQueueCC The context specific queue data (same as @a pQueue
1615 * for ring-3).
1616 * @param pQueue The shared queue data.
1617 */
1618static int tmTimerSetRelative(PVMCC pVM, PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now,
1619 PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue)
1620{
1621 STAM_COUNTER_INC(&pTimer->StatSetRelative);
1622
1623 /* Treat virtual sync timers specially. */
1624 if (pQueue->enmClock == TMCLOCK_VIRTUAL_SYNC)
1625 return tmTimerVirtualSyncSetRelative(pVM, pTimer, cTicksToNext, pu64Now);
1626
1627 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
1628 TMTIMER_ASSERT_CRITSECT(pVM, pTimer);
1629
1630 DBGFTRACE_U64_TAG2(pVM, cTicksToNext, "TMTimerSetRelative", pTimer->szName);
1631
1632#ifdef VBOX_WITH_STATISTICS
1633 /*
1634 * Gather optimization info.
1635 */
1636 STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelative);
1637 TMTIMERSTATE enmOrgState = pTimer->enmState;
1638 switch (enmOrgState)
1639 {
1640 case TMTIMERSTATE_STOPPED: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStStopped); break;
1641 case TMTIMERSTATE_EXPIRED_DELIVER: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStExpDeliver); break;
1642 case TMTIMERSTATE_ACTIVE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStActive); break;
1643 case TMTIMERSTATE_PENDING_STOP: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendStop); break;
1644 case TMTIMERSTATE_PENDING_STOP_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendStopSched); break;
1645 case TMTIMERSTATE_PENDING_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendSched); break;
1646 case TMTIMERSTATE_PENDING_RESCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendResched); break;
1647 default: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStOther); break;
1648 }
1649#endif
1650
1651 /*
1652 * Try to take the TM lock and optimize the common cases.
1653 *
1654 * With the TM lock we can safely make optimizations like immediate
1655 * scheduling and we can also be 100% sure that we're not racing the
1656 * running of the timer queues. As an additional restraint we require the
1657 * timer to have a critical section associated with to be 100% there aren't
1658 * concurrent operations on the timer. (This latter isn't necessary any
1659 * longer as this isn't supported for any timers, critsect or not.)
1660 *
1661 * Note! Lock ordering doesn't apply when we only _try_ to
1662 * get the innermost locks.
1663 */
1664 bool fOwnTMLock = RT_SUCCESS_NP(PDMCritSectTryEnter(pVM, &pQueue->TimerLock));
1665#if 1
1666 if ( fOwnTMLock
1667 && pTimer->pCritSect)
1668 {
1669 TMTIMERSTATE enmState = pTimer->enmState;
1670 if (RT_LIKELY( ( enmState == TMTIMERSTATE_EXPIRED_DELIVER
1671 || enmState == TMTIMERSTATE_STOPPED)
1672 && tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, enmState)))
1673 {
1674 tmTimerSetRelativeOptimizedStart(pVM, pTimer, cTicksToNext, pu64Now, pQueueCC, pQueue);
1675 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
1676 PDMCritSectLeave(pVM, &pQueue->TimerLock);
1677 return VINF_SUCCESS;
1678 }
1679
1680 /* Optimize other states when it becomes necessary. */
1681 }
1682#endif
1683
1684 /*
1685 * Unoptimized path.
1686 */
1687 int rc;
1688 for (int cRetries = 1000; ; cRetries--)
1689 {
1690 /*
1691 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
1692 */
1693 TMTIMERSTATE enmState = pTimer->enmState;
1694 switch (enmState)
1695 {
1696 case TMTIMERSTATE_STOPPED:
1697 if (pQueue->enmClock == TMCLOCK_VIRTUAL_SYNC)
1698 {
1699 /** @todo To fix assertion in tmR3TimerQueueRunVirtualSync:
1700 * Figure a safe way of activating this timer while the queue is
1701 * being run.
1702 * (99.9% sure this that the assertion is caused by DevAPIC.cpp
1703 * re-starting the timer in response to a initial_count write.) */
1704 }
1705 RT_FALL_THRU();
1706 case TMTIMERSTATE_EXPIRED_DELIVER:
1707 if (tmTimerTryWithLink(pQueueCC, pQueue, pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
1708 {
1709 Assert(pTimer->idxPrev == UINT32_MAX);
1710 Assert(pTimer->idxNext == UINT32_MAX);
1711 pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, pQueue->enmClock, pu64Now);
1712 Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [EXP/STOP]\n",
1713 pTimer, tmTimerState(enmState), pTimer->szName, pTimer->u64Expire, cRetries));
1714 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
1715 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
1716 rc = VINF_SUCCESS;
1717 break;
1718 }
1719 rc = VERR_TRY_AGAIN;
1720 break;
1721
1722 case TMTIMERSTATE_PENDING_SCHEDULE:
1723 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1724 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
1725 {
1726 pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, pQueue->enmClock, pu64Now);
1727 Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [PEND_SCHED]\n",
1728 pTimer, tmTimerState(enmState), pTimer->szName, pTimer->u64Expire, cRetries));
1729 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
1730 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
1731 rc = VINF_SUCCESS;
1732 break;
1733 }
1734 rc = VERR_TRY_AGAIN;
1735 break;
1736
1737
1738 case TMTIMERSTATE_ACTIVE:
1739 if (tmTimerTryWithLink(pQueueCC, pQueue, pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
1740 {
1741 pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, pQueue->enmClock, pu64Now);
1742 Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [ACTIVE]\n",
1743 pTimer, tmTimerState(enmState), pTimer->szName, pTimer->u64Expire, cRetries));
1744 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
1745 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
1746 rc = VINF_SUCCESS;
1747 break;
1748 }
1749 rc = VERR_TRY_AGAIN;
1750 break;
1751
1752 case TMTIMERSTATE_PENDING_RESCHEDULE:
1753 case TMTIMERSTATE_PENDING_STOP:
1754 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
1755 {
1756 pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, pQueue->enmClock, pu64Now);
1757 Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [PEND_RESCH/STOP]\n",
1758 pTimer, tmTimerState(enmState), pTimer->szName, pTimer->u64Expire, cRetries));
1759 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
1760 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
1761 rc = VINF_SUCCESS;
1762 break;
1763 }
1764 rc = VERR_TRY_AGAIN;
1765 break;
1766
1767
1768 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1769 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1770 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1771#ifdef IN_RING3
1772 if (!RTThreadYield())
1773 RTThreadSleep(1);
1774#else
1775/** @todo call host context and yield after a couple of iterations */
1776#endif
1777 rc = VERR_TRY_AGAIN;
1778 break;
1779
1780 /*
1781 * Invalid states.
1782 */
1783 case TMTIMERSTATE_DESTROY:
1784 case TMTIMERSTATE_FREE:
1785 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, pTimer->szName));
1786 rc = VERR_TM_INVALID_STATE;
1787 break;
1788
1789 default:
1790 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, pTimer->szName));
1791 rc = VERR_TM_UNKNOWN_STATE;
1792 break;
1793 }
1794
1795 /* switch + loop is tedious to break out of. */
1796 if (rc == VINF_SUCCESS)
1797 break;
1798
1799 if (rc != VERR_TRY_AGAIN)
1800 {
1801 tmTimerSetRelativeNowWorker(pVM, pQueue->enmClock, pu64Now);
1802 break;
1803 }
1804 if (cRetries <= 0)
1805 {
1806 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->szName));
1807 rc = VERR_TM_TIMER_UNSTABLE_STATE;
1808 tmTimerSetRelativeNowWorker(pVM, pQueue->enmClock, pu64Now);
1809 break;
1810 }
1811
1812 /*
1813 * Retry to gain locks.
1814 */
1815 if (!fOwnTMLock)
1816 fOwnTMLock = RT_SUCCESS_NP(PDMCritSectTryEnter(pVM, &pQueue->TimerLock));
1817
1818 } /* for (;;) */
1819
1820 /*
1821 * Clean up and return.
1822 */
1823 if (fOwnTMLock)
1824 PDMCritSectLeave(pVM, &pQueue->TimerLock);
1825
1826 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
1827 return rc;
1828}
1829
1830
1831/**
1832 * Arm a timer with a expire time relative to the current time.
1833 *
1834 * @returns VBox status code.
1835 * @param pVM The cross context VM structure.
1836 * @param hTimer Timer handle as returned by one of the create functions.
1837 * @param cTicksToNext Clock ticks until the next time expiration.
1838 * @param pu64Now Where to return the current time stamp used.
1839 * Optional.
1840 */
1841VMMDECL(int) TMTimerSetRelative(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cTicksToNext, uint64_t *pu64Now)
1842{
1843 TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
1844 return tmTimerSetRelative(pVM, pTimer, cTicksToNext, pu64Now, pQueueCC, pQueue);
1845}
1846
1847
1848/**
1849 * Drops a hint about the frequency of the timer.
1850 *
1851 * This is used by TM and the VMM to calculate how often guest execution needs
1852 * to be interrupted. The hint is automatically cleared by TMTimerStop.
1853 *
1854 * @returns VBox status code.
1855 * @param pVM The cross context VM structure.
1856 * @param hTimer Timer handle as returned by one of the create functions.
1857 * @param uHzHint The frequency hint. Pass 0 to clear the hint.
1858 *
1859 * @remarks We're using an integer hertz value here since anything above 1 HZ
1860 * is not going to be any trouble satisfying scheduling wise. The
1861 * range where it makes sense is >= 100 HZ.
1862 */
1863VMMDECL(int) TMTimerSetFrequencyHint(PVMCC pVM, TMTIMERHANDLE hTimer, uint32_t uHzHint)
1864{
1865 TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
1866 TMTIMER_ASSERT_CRITSECT(pVM, pTimer);
1867
1868 uint32_t const uHzOldHint = pTimer->uHzHint;
1869 pTimer->uHzHint = uHzHint;
1870
1871 uint32_t const uMaxHzHint = pQueue->uMaxHzHint;
1872 if ( uHzHint > uMaxHzHint
1873 || uHzOldHint >= uMaxHzHint)
1874 ASMAtomicOrU64(&pVM->tm.s.HzHint.u64Combined, RT_BIT_32(idxQueue) | RT_BIT_32(idxQueue + 16));
1875
1876 return VINF_SUCCESS;
1877}
1878
1879
1880/**
1881 * TMTimerStop for the virtual sync timer queue.
1882 *
1883 * This employs a greatly simplified state machine by always acquiring the
1884 * queue lock and bypassing the scheduling list.
1885 *
1886 * @returns VBox status code
1887 * @param pVM The cross context VM structure.
1888 * @param pTimer The timer handle.
1889 */
1890static int tmTimerVirtualSyncStop(PVMCC pVM, PTMTIMER pTimer)
1891{
1892 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerStopVs), a);
1893 VM_ASSERT_EMT(pVM);
1894 TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer);
1895 int rc = PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VINF_SUCCESS);
1896 AssertRCReturn(rc, rc);
1897
1898 /* Reset the HZ hint. */
1899 uint32_t uOldHzHint = pTimer->uHzHint;
1900 if (uOldHzHint)
1901 {
1902 if (uOldHzHint >= pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].uMaxHzHint)
1903 ASMAtomicOrU64(&pVM->tm.s.HzHint.u64Combined, RT_BIT_32(TMCLOCK_VIRTUAL_SYNC) | RT_BIT_32(TMCLOCK_VIRTUAL_SYNC + 16));
1904 pTimer->uHzHint = 0;
1905 }
1906
1907 /* Update the timer state. */
1908 TMTIMERSTATE const enmState = pTimer->enmState;
1909 switch (enmState)
1910 {
1911 case TMTIMERSTATE_ACTIVE:
1912 {
1913 PTMTIMERQUEUE const pQueue = &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC];
1914 tmTimerQueueUnlinkActive(pVM, TM_GET_TIMER_QUEUE_CC(pVM, TMCLOCK_VIRTUAL_SYNC, pQueue), pQueue, pTimer);
1915 TM_SET_STATE(pTimer, TMTIMERSTATE_STOPPED);
1916 rc = VINF_SUCCESS;
1917 break;
1918 }
1919
1920 case TMTIMERSTATE_EXPIRED_DELIVER:
1921 TM_SET_STATE(pTimer, TMTIMERSTATE_STOPPED);
1922 rc = VINF_SUCCESS;
1923 break;
1924
1925 case TMTIMERSTATE_STOPPED:
1926 rc = VINF_SUCCESS;
1927 break;
1928
1929 case TMTIMERSTATE_PENDING_RESCHEDULE:
1930 case TMTIMERSTATE_PENDING_STOP:
1931 case TMTIMERSTATE_PENDING_SCHEDULE:
1932 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1933 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1934 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1935 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1936 case TMTIMERSTATE_DESTROY:
1937 case TMTIMERSTATE_FREE:
1938 AssertLogRelMsgFailed(("Invalid timer state %s: %s\n", tmTimerState(enmState), pTimer->szName));
1939 rc = VERR_TM_INVALID_STATE;
1940 break;
1941
1942 default:
1943 AssertMsgFailed(("Unknown timer state %d: %s\n", enmState, pTimer->szName));
1944 rc = VERR_TM_UNKNOWN_STATE;
1945 break;
1946 }
1947
1948 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStopVs), a);
1949 PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
1950 return rc;
1951}
1952
1953
1954/**
1955 * Stop the timer.
1956 * Use TMR3TimerArm() to "un-stop" the timer.
1957 *
1958 * @returns VBox status code.
1959 * @param pVM The cross context VM structure.
1960 * @param hTimer Timer handle as returned by one of the create functions.
1961 */
1962VMMDECL(int) TMTimerStop(PVMCC pVM, TMTIMERHANDLE hTimer)
1963{
1964 TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
1965 STAM_COUNTER_INC(&pTimer->StatStop);
1966
1967 /* Treat virtual sync timers specially. */
1968 if (idxQueue == TMCLOCK_VIRTUAL_SYNC)
1969 return tmTimerVirtualSyncStop(pVM, pTimer);
1970
1971 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
1972 TMTIMER_ASSERT_CRITSECT(pVM, pTimer);
1973
1974 /*
1975 * Reset the HZ hint.
1976 */
1977 uint32_t const uOldHzHint = pTimer->uHzHint;
1978 if (uOldHzHint)
1979 {
1980 if (uOldHzHint >= pQueue->uMaxHzHint)
1981 ASMAtomicOrU64(&pVM->tm.s.HzHint.u64Combined, RT_BIT_32(idxQueue) | RT_BIT_32(idxQueue + 16));
1982 pTimer->uHzHint = 0;
1983 }
1984
1985 /** @todo see if this function needs optimizing. */
1986 int cRetries = 1000;
1987 do
1988 {
1989 /*
1990 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
1991 */
1992 TMTIMERSTATE enmState = pTimer->enmState;
1993 Log2(("TMTimerStop: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
1994 pTimer, tmTimerState(enmState), pTimer->szName, cRetries));
1995 switch (enmState)
1996 {
1997 case TMTIMERSTATE_EXPIRED_DELIVER:
1998 //AssertMsgFailed(("You don't stop an expired timer dude!\n"));
1999 return VERR_INVALID_PARAMETER;
2000
2001 case TMTIMERSTATE_STOPPED:
2002 case TMTIMERSTATE_PENDING_STOP:
2003 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
2004 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
2005 return VINF_SUCCESS;
2006
2007 case TMTIMERSTATE_PENDING_SCHEDULE:
2008 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, enmState))
2009 {
2010 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
2011 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
2012 return VINF_SUCCESS;
2013 }
2014 break;
2015
2016 case TMTIMERSTATE_PENDING_RESCHEDULE:
2017 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
2018 {
2019 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
2020 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
2021 return VINF_SUCCESS;
2022 }
2023 break;
2024
2025 case TMTIMERSTATE_ACTIVE:
2026 if (tmTimerTryWithLink(pQueueCC, pQueue, pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
2027 {
2028 tmSchedule(pVM, pQueueCC, pQueue, pTimer);
2029 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
2030 return VINF_SUCCESS;
2031 }
2032 break;
2033
2034 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
2035 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
2036 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2037#ifdef IN_RING3
2038 if (!RTThreadYield())
2039 RTThreadSleep(1);
2040#else
2041/** @todo call host and yield cpu after a while. */
2042#endif
2043 break;
2044
2045 /*
2046 * Invalid states.
2047 */
2048 case TMTIMERSTATE_DESTROY:
2049 case TMTIMERSTATE_FREE:
2050 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, pTimer->szName));
2051 return VERR_TM_INVALID_STATE;
2052 default:
2053 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, pTimer->szName));
2054 return VERR_TM_UNKNOWN_STATE;
2055 }
2056 } while (cRetries-- > 0);
2057
2058 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->szName));
2059 STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
2060 return VERR_TM_TIMER_UNSTABLE_STATE;
2061}
2062
2063
2064/**
2065 * Get the current clock time.
2066 * Handy for calculating the new expire time.
2067 *
2068 * @returns Current clock time.
2069 * @param pVM The cross context VM structure.
2070 * @param hTimer Timer handle as returned by one of the create functions.
2071 */
2072VMMDECL(uint64_t) TMTimerGet(PVMCC pVM, TMTIMERHANDLE hTimer)
2073{
2074 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, 0); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2075 STAM_COUNTER_INC(&pTimer->StatGet);
2076
2077 uint64_t u64;
2078 switch (pQueue->enmClock)
2079 {
2080 case TMCLOCK_VIRTUAL:
2081 u64 = TMVirtualGet(pVM);
2082 break;
2083 case TMCLOCK_VIRTUAL_SYNC:
2084 u64 = TMVirtualSyncGet(pVM);
2085 break;
2086 case TMCLOCK_REAL:
2087 u64 = TMRealGet(pVM);
2088 break;
2089 default:
2090 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2091 return UINT64_MAX;
2092 }
2093 //Log2(("TMTimerGet: returns %'RU64 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
2094 // u64, pTimer, tmTimerState(pTimer->enmState), pTimer->szName));
2095 return u64;
2096}
2097
2098
2099/**
2100 * Get the frequency of the timer clock.
2101 *
2102 * @returns Clock frequency (as Hz of course).
2103 * @param pVM The cross context VM structure.
2104 * @param hTimer Timer handle as returned by one of the create functions.
2105 */
2106VMMDECL(uint64_t) TMTimerGetFreq(PVMCC pVM, TMTIMERHANDLE hTimer)
2107{
2108 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, 0); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2109 switch (pQueue->enmClock)
2110 {
2111 case TMCLOCK_VIRTUAL:
2112 case TMCLOCK_VIRTUAL_SYNC:
2113 return TMCLOCK_FREQ_VIRTUAL;
2114
2115 case TMCLOCK_REAL:
2116 return TMCLOCK_FREQ_REAL;
2117
2118 default:
2119 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2120 return 0;
2121 }
2122}
2123
2124
2125/**
2126 * Get the expire time of the timer.
2127 * Only valid for active timers.
2128 *
2129 * @returns Expire time of the timer.
2130 * @param pVM The cross context VM structure.
2131 * @param hTimer Timer handle as returned by one of the create functions.
2132 */
2133VMMDECL(uint64_t) TMTimerGetExpire(PVMCC pVM, TMTIMERHANDLE hTimer)
2134{
2135 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, UINT64_MAX); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2136 TMTIMER_ASSERT_CRITSECT(pVM, pTimer);
2137 int cRetries = 1000;
2138 do
2139 {
2140 TMTIMERSTATE enmState = pTimer->enmState;
2141 switch (enmState)
2142 {
2143 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
2144 case TMTIMERSTATE_EXPIRED_DELIVER:
2145 case TMTIMERSTATE_STOPPED:
2146 case TMTIMERSTATE_PENDING_STOP:
2147 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
2148 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
2149 pTimer, tmTimerState(pTimer->enmState), pTimer->szName));
2150 return UINT64_MAX;
2151
2152 case TMTIMERSTATE_ACTIVE:
2153 case TMTIMERSTATE_PENDING_RESCHEDULE:
2154 case TMTIMERSTATE_PENDING_SCHEDULE:
2155 Log2(("TMTimerGetExpire: returns %'RU64 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
2156 pTimer->u64Expire, pTimer, tmTimerState(pTimer->enmState), pTimer->szName));
2157 return pTimer->u64Expire;
2158
2159 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
2160 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2161#ifdef IN_RING3
2162 if (!RTThreadYield())
2163 RTThreadSleep(1);
2164#endif
2165 break;
2166
2167 /*
2168 * Invalid states.
2169 */
2170 case TMTIMERSTATE_DESTROY:
2171 case TMTIMERSTATE_FREE:
2172 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, pTimer->szName));
2173 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
2174 pTimer, tmTimerState(pTimer->enmState), pTimer->szName));
2175 return UINT64_MAX;
2176 default:
2177 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, pTimer->szName));
2178 return UINT64_MAX;
2179 }
2180 } while (cRetries-- > 0);
2181
2182 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->szName));
2183 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
2184 pTimer, tmTimerState(pTimer->enmState), pTimer->szName));
2185 return UINT64_MAX;
2186}
2187
2188
2189/**
2190 * Checks if a timer is active or not.
2191 *
2192 * @returns True if active.
2193 * @returns False if not active.
2194 * @param pVM The cross context VM structure.
2195 * @param hTimer Timer handle as returned by one of the create functions.
2196 */
2197VMMDECL(bool) TMTimerIsActive(PVMCC pVM, TMTIMERHANDLE hTimer)
2198{
2199 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, false); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2200 TMTIMERSTATE enmState = pTimer->enmState;
2201 switch (enmState)
2202 {
2203 case TMTIMERSTATE_STOPPED:
2204 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
2205 case TMTIMERSTATE_EXPIRED_DELIVER:
2206 case TMTIMERSTATE_PENDING_STOP:
2207 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
2208 Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
2209 pTimer, tmTimerState(pTimer->enmState), pTimer->szName));
2210 return false;
2211
2212 case TMTIMERSTATE_ACTIVE:
2213 case TMTIMERSTATE_PENDING_RESCHEDULE:
2214 case TMTIMERSTATE_PENDING_SCHEDULE:
2215 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
2216 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2217 Log2(("TMTimerIsActive: returns true (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
2218 pTimer, tmTimerState(pTimer->enmState), pTimer->szName));
2219 return true;
2220
2221 /*
2222 * Invalid states.
2223 */
2224 case TMTIMERSTATE_DESTROY:
2225 case TMTIMERSTATE_FREE:
2226 AssertMsgFailed(("Invalid timer state %s (%s)\n", tmTimerState(enmState), pTimer->szName));
2227 Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
2228 pTimer, tmTimerState(pTimer->enmState), pTimer->szName));
2229 return false;
2230 default:
2231 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, pTimer->szName));
2232 return false;
2233 }
2234}
2235
2236
2237/* -=-=-=-=-=-=- Convenience APIs -=-=-=-=-=-=- */
2238
2239
2240/**
2241 * Arm a timer with a (new) expire time relative to current time.
2242 *
2243 * @returns VBox status code.
2244 * @param pVM The cross context VM structure.
2245 * @param hTimer Timer handle as returned by one of the create functions.
2246 * @param cMilliesToNext Number of milliseconds to the next tick.
2247 */
2248VMMDECL(int) TMTimerSetMillies(PVMCC pVM, TMTIMERHANDLE hTimer, uint32_t cMilliesToNext)
2249{
2250 TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2251 switch (pQueue->enmClock)
2252 {
2253 case TMCLOCK_VIRTUAL:
2254 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2255 return tmTimerSetRelative(pVM, pTimer, cMilliesToNext * UINT64_C(1000000), NULL, pQueueCC, pQueue);
2256
2257 case TMCLOCK_VIRTUAL_SYNC:
2258 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2259 return tmTimerSetRelative(pVM, pTimer, cMilliesToNext * UINT64_C(1000000), NULL, pQueueCC, pQueue);
2260
2261 case TMCLOCK_REAL:
2262 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2263 return tmTimerSetRelative(pVM, pTimer, cMilliesToNext, NULL, pQueueCC, pQueue);
2264
2265 default:
2266 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2267 return VERR_TM_TIMER_BAD_CLOCK;
2268 }
2269}
2270
2271
2272/**
2273 * Arm a timer with a (new) expire time relative to current time.
2274 *
2275 * @returns VBox status code.
2276 * @param pVM The cross context VM structure.
2277 * @param hTimer Timer handle as returned by one of the create functions.
2278 * @param cMicrosToNext Number of microseconds to the next tick.
2279 */
2280VMMDECL(int) TMTimerSetMicro(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cMicrosToNext)
2281{
2282 TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2283 switch (pQueue->enmClock)
2284 {
2285 case TMCLOCK_VIRTUAL:
2286 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2287 return tmTimerSetRelative(pVM, pTimer, cMicrosToNext * 1000, NULL, pQueueCC, pQueue);
2288
2289 case TMCLOCK_VIRTUAL_SYNC:
2290 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2291 return tmTimerSetRelative(pVM, pTimer, cMicrosToNext * 1000, NULL, pQueueCC, pQueue);
2292
2293 case TMCLOCK_REAL:
2294 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2295 return tmTimerSetRelative(pVM, pTimer, cMicrosToNext / 1000, NULL, pQueueCC, pQueue);
2296
2297 default:
2298 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2299 return VERR_TM_TIMER_BAD_CLOCK;
2300 }
2301}
2302
2303
2304/**
2305 * Arm a timer with a (new) expire time relative to current time.
2306 *
2307 * @returns VBox status code.
2308 * @param pVM The cross context VM structure.
2309 * @param hTimer Timer handle as returned by one of the create functions.
2310 * @param cNanosToNext Number of nanoseconds to the next tick.
2311 */
2312VMMDECL(int) TMTimerSetNano(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cNanosToNext)
2313{
2314 TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2315 switch (pQueue->enmClock)
2316 {
2317 case TMCLOCK_VIRTUAL:
2318 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2319 return tmTimerSetRelative(pVM, pTimer, cNanosToNext, NULL, pQueueCC, pQueue);
2320
2321 case TMCLOCK_VIRTUAL_SYNC:
2322 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2323 return tmTimerSetRelative(pVM, pTimer, cNanosToNext, NULL, pQueueCC, pQueue);
2324
2325 case TMCLOCK_REAL:
2326 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2327 return tmTimerSetRelative(pVM, pTimer, cNanosToNext / 1000000, NULL, pQueueCC, pQueue);
2328
2329 default:
2330 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2331 return VERR_TM_TIMER_BAD_CLOCK;
2332 }
2333}
2334
2335
2336/**
2337 * Get the current clock time as nanoseconds.
2338 *
2339 * @returns The timer clock as nanoseconds.
2340 * @param pVM The cross context VM structure.
2341 * @param hTimer Timer handle as returned by one of the create functions.
2342 */
2343VMMDECL(uint64_t) TMTimerGetNano(PVMCC pVM, TMTIMERHANDLE hTimer)
2344{
2345 return TMTimerToNano(pVM, hTimer, TMTimerGet(pVM, hTimer));
2346}
2347
2348
2349/**
2350 * Get the current clock time as microseconds.
2351 *
2352 * @returns The timer clock as microseconds.
2353 * @param pVM The cross context VM structure.
2354 * @param hTimer Timer handle as returned by one of the create functions.
2355 */
2356VMMDECL(uint64_t) TMTimerGetMicro(PVMCC pVM, TMTIMERHANDLE hTimer)
2357{
2358 return TMTimerToMicro(pVM, hTimer, TMTimerGet(pVM, hTimer));
2359}
2360
2361
2362/**
2363 * Get the current clock time as milliseconds.
2364 *
2365 * @returns The timer clock as milliseconds.
2366 * @param pVM The cross context VM structure.
2367 * @param hTimer Timer handle as returned by one of the create functions.
2368 */
2369VMMDECL(uint64_t) TMTimerGetMilli(PVMCC pVM, TMTIMERHANDLE hTimer)
2370{
2371 return TMTimerToMilli(pVM, hTimer, TMTimerGet(pVM, hTimer));
2372}
2373
2374
2375/**
2376 * Converts the specified timer clock time to nanoseconds.
2377 *
2378 * @returns nanoseconds.
2379 * @param pVM The cross context VM structure.
2380 * @param hTimer Timer handle as returned by one of the create functions.
2381 * @param cTicks The clock ticks.
2382 * @remark There could be rounding errors here. We just do a simple integer divide
2383 * without any adjustments.
2384 */
2385VMMDECL(uint64_t) TMTimerToNano(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cTicks)
2386{
2387 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, 0); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2388 switch (pQueue->enmClock)
2389 {
2390 case TMCLOCK_VIRTUAL:
2391 case TMCLOCK_VIRTUAL_SYNC:
2392 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2393 return cTicks;
2394
2395 case TMCLOCK_REAL:
2396 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2397 return cTicks * 1000000;
2398
2399 default:
2400 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2401 return 0;
2402 }
2403}
2404
2405
2406/**
2407 * Converts the specified timer clock time to microseconds.
2408 *
2409 * @returns microseconds.
2410 * @param pVM The cross context VM structure.
2411 * @param hTimer Timer handle as returned by one of the create functions.
2412 * @param cTicks The clock ticks.
2413 * @remark There could be rounding errors here. We just do a simple integer divide
2414 * without any adjustments.
2415 */
2416VMMDECL(uint64_t) TMTimerToMicro(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cTicks)
2417{
2418 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, 0); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2419 switch (pQueue->enmClock)
2420 {
2421 case TMCLOCK_VIRTUAL:
2422 case TMCLOCK_VIRTUAL_SYNC:
2423 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2424 return cTicks / 1000;
2425
2426 case TMCLOCK_REAL:
2427 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2428 return cTicks * 1000;
2429
2430 default:
2431 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2432 return 0;
2433 }
2434}
2435
2436
2437/**
2438 * Converts the specified timer clock time to milliseconds.
2439 *
2440 * @returns milliseconds.
2441 * @param pVM The cross context VM structure.
2442 * @param hTimer Timer handle as returned by one of the create functions.
2443 * @param cTicks The clock ticks.
2444 * @remark There could be rounding errors here. We just do a simple integer divide
2445 * without any adjustments.
2446 */
2447VMMDECL(uint64_t) TMTimerToMilli(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cTicks)
2448{
2449 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, 0); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2450 switch (pQueue->enmClock)
2451 {
2452 case TMCLOCK_VIRTUAL:
2453 case TMCLOCK_VIRTUAL_SYNC:
2454 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2455 return cTicks / 1000000;
2456
2457 case TMCLOCK_REAL:
2458 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2459 return cTicks;
2460
2461 default:
2462 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2463 return 0;
2464 }
2465}
2466
2467
2468/**
2469 * Converts the specified nanosecond timestamp to timer clock ticks.
2470 *
2471 * @returns timer clock ticks.
2472 * @param pVM The cross context VM structure.
2473 * @param hTimer Timer handle as returned by one of the create functions.
2474 * @param cNanoSecs The nanosecond value ticks to convert.
2475 * @remark There could be rounding and overflow errors here.
2476 */
2477VMMDECL(uint64_t) TMTimerFromNano(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cNanoSecs)
2478{
2479 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, 0); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2480 switch (pQueue->enmClock)
2481 {
2482 case TMCLOCK_VIRTUAL:
2483 case TMCLOCK_VIRTUAL_SYNC:
2484 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2485 return cNanoSecs;
2486
2487 case TMCLOCK_REAL:
2488 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2489 return cNanoSecs / 1000000;
2490
2491 default:
2492 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2493 return 0;
2494 }
2495}
2496
2497
2498/**
2499 * Converts the specified microsecond timestamp to timer clock ticks.
2500 *
2501 * @returns timer clock ticks.
2502 * @param pVM The cross context VM structure.
2503 * @param hTimer Timer handle as returned by one of the create functions.
2504 * @param cMicroSecs The microsecond value ticks to convert.
2505 * @remark There could be rounding and overflow errors here.
2506 */
2507VMMDECL(uint64_t) TMTimerFromMicro(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cMicroSecs)
2508{
2509 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, 0); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2510 switch (pQueue->enmClock)
2511 {
2512 case TMCLOCK_VIRTUAL:
2513 case TMCLOCK_VIRTUAL_SYNC:
2514 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2515 return cMicroSecs * 1000;
2516
2517 case TMCLOCK_REAL:
2518 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2519 return cMicroSecs / 1000;
2520
2521 default:
2522 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2523 return 0;
2524 }
2525}
2526
2527
2528/**
2529 * Converts the specified millisecond timestamp to timer clock ticks.
2530 *
2531 * @returns timer clock ticks.
2532 * @param pVM The cross context VM structure.
2533 * @param hTimer Timer handle as returned by one of the create functions.
2534 * @param cMilliSecs The millisecond value ticks to convert.
2535 * @remark There could be rounding and overflow errors here.
2536 */
2537VMMDECL(uint64_t) TMTimerFromMilli(PVMCC pVM, TMTIMERHANDLE hTimer, uint64_t cMilliSecs)
2538{
2539 TMTIMER_HANDLE_TO_VARS_RETURN_EX(pVM, hTimer, 0); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
2540 switch (pQueue->enmClock)
2541 {
2542 case TMCLOCK_VIRTUAL:
2543 case TMCLOCK_VIRTUAL_SYNC:
2544 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
2545 return cMilliSecs * 1000000;
2546
2547 case TMCLOCK_REAL:
2548 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
2549 return cMilliSecs;
2550
2551 default:
2552 AssertMsgFailed(("Invalid enmClock=%d\n", pQueue->enmClock));
2553 return 0;
2554 }
2555}
2556
2557
2558/**
2559 * Convert state to string.
2560 *
2561 * @returns Readonly status name.
2562 * @param enmState State.
2563 */
2564const char *tmTimerState(TMTIMERSTATE enmState)
2565{
2566 switch (enmState)
2567 {
2568#define CASE(num, state) \
2569 case TMTIMERSTATE_##state: \
2570 AssertCompile(TMTIMERSTATE_##state == (num)); \
2571 return #num "-" #state
2572 CASE( 0,INVALID);
2573 CASE( 1,STOPPED);
2574 CASE( 2,ACTIVE);
2575 CASE( 3,EXPIRED_GET_UNLINK);
2576 CASE( 4,EXPIRED_DELIVER);
2577 CASE( 5,PENDING_STOP);
2578 CASE( 6,PENDING_STOP_SCHEDULE);
2579 CASE( 7,PENDING_SCHEDULE_SET_EXPIRE);
2580 CASE( 8,PENDING_SCHEDULE);
2581 CASE( 9,PENDING_RESCHEDULE_SET_EXPIRE);
2582 CASE(10,PENDING_RESCHEDULE);
2583 CASE(11,DESTROY);
2584 CASE(12,FREE);
2585 default:
2586 AssertMsgFailed(("Invalid state enmState=%d\n", enmState));
2587 return "Invalid state!";
2588#undef CASE
2589 }
2590}
2591
2592
2593/**
2594 * The slow path of tmGetFrequencyHint() where we try to recalculate the value.
2595 *
2596 * @returns The highest frequency. 0 if no timers care.
2597 * @param pVM The cross context VM structure.
2598 * @param uOldMaxHzHint The old global hint.
2599 */
2600DECL_NO_INLINE(static, uint32_t) tmGetFrequencyHintSlow(PVMCC pVM, uint32_t uOldMaxHzHint)
2601{
2602 /* Set two bits, though not entirely sure it's needed (too exhaused to think clearly)
2603 but it should force other callers thru the slow path while we're recalculating and
2604 help us detect changes while we're recalculating. */
2605 AssertCompile(RT_ELEMENTS(pVM->tm.s.aTimerQueues) <= 16);
2606
2607 /*
2608 * The "right" highest frequency value isn't so important that we'll block
2609 * waiting on the timer semaphores.
2610 */
2611 uint32_t uMaxHzHint = 0;
2612 for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
2613 {
2614 PTMTIMERQUEUE pQueue = &pVM->tm.s.aTimerQueues[idxQueue];
2615
2616 /* Get the max Hz hint for the queue. */
2617 uint32_t uMaxHzHintQueue;
2618 if ( !(ASMAtomicUoReadU64(&pVM->tm.s.HzHint.u64Combined) & (RT_BIT_32(idxQueue) | RT_BIT_32(idxQueue + 16)))
2619 || RT_FAILURE_NP(PDMCritSectTryEnter(pVM, &pQueue->TimerLock)))
2620 uMaxHzHintQueue = ASMAtomicReadU32(&pQueue->uMaxHzHint);
2621 else
2622 {
2623 /* Is it still necessary to do updating? */
2624 if (ASMAtomicUoReadU64(&pVM->tm.s.HzHint.u64Combined) & (RT_BIT_32(idxQueue) | RT_BIT_32(idxQueue + 16)))
2625 {
2626 ASMAtomicAndU64(&pVM->tm.s.HzHint.u64Combined, ~RT_BIT_64(idxQueue + 16)); /* clear one flag up front */
2627
2628 PTMTIMERQUEUECC pQueueCC = TM_GET_TIMER_QUEUE_CC(pVM, idxQueue, pQueue);
2629 uMaxHzHintQueue = 0;
2630 for (PTMTIMER pCur = tmTimerQueueGetHead(pQueueCC, pQueue);
2631 pCur;
2632 pCur = tmTimerGetNext(pQueueCC, pCur))
2633 {
2634 uint32_t uHzHint = ASMAtomicUoReadU32(&pCur->uHzHint);
2635 if (uHzHint > uMaxHzHintQueue)
2636 {
2637 TMTIMERSTATE enmState = pCur->enmState;
2638 switch (enmState)
2639 {
2640 case TMTIMERSTATE_ACTIVE:
2641 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
2642 case TMTIMERSTATE_EXPIRED_DELIVER:
2643 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
2644 case TMTIMERSTATE_PENDING_SCHEDULE:
2645 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2646 case TMTIMERSTATE_PENDING_RESCHEDULE:
2647 uMaxHzHintQueue = uHzHint;
2648 break;
2649
2650 case TMTIMERSTATE_STOPPED:
2651 case TMTIMERSTATE_PENDING_STOP:
2652 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
2653 case TMTIMERSTATE_DESTROY:
2654 case TMTIMERSTATE_FREE:
2655 case TMTIMERSTATE_INVALID:
2656 break;
2657 /* no default, want gcc warnings when adding more states. */
2658 }
2659 }
2660 }
2661
2662 /* Write the new Hz hint for the quest and clear the other update flag. */
2663 ASMAtomicUoWriteU32(&pQueue->uMaxHzHint, uMaxHzHintQueue);
2664 ASMAtomicAndU64(&pVM->tm.s.HzHint.u64Combined, ~RT_BIT_64(idxQueue));
2665 }
2666 else
2667 uMaxHzHintQueue = ASMAtomicUoReadU32(&pQueue->uMaxHzHint);
2668
2669 PDMCritSectLeave(pVM, &pQueue->TimerLock);
2670 }
2671
2672 /* Update the global max Hz hint. */
2673 if (uMaxHzHint < uMaxHzHintQueue)
2674 uMaxHzHint = uMaxHzHintQueue;
2675 }
2676
2677 /*
2678 * Update the frequency hint if no pending frequency changes and we didn't race anyone thru here.
2679 */
2680 uint64_t u64Actual = RT_MAKE_U64(0 /*no pending updates*/, uOldMaxHzHint);
2681 if (ASMAtomicCmpXchgExU64(&pVM->tm.s.HzHint.u64Combined, RT_MAKE_U64(0, uMaxHzHint), u64Actual, &u64Actual))
2682 Log(("tmGetFrequencyHintSlow: New value %u Hz\n", uMaxHzHint));
2683 else
2684 for (uint32_t iTry = 1;; iTry++)
2685 {
2686 if (RT_LO_U32(u64Actual) != 0)
2687 Log(("tmGetFrequencyHintSlow: Outdated value %u Hz (%#x, try %u)\n", uMaxHzHint, RT_LO_U32(u64Actual), iTry));
2688 else if (iTry >= 4)
2689 Log(("tmGetFrequencyHintSlow: Unable to set %u Hz (try %u)\n", uMaxHzHint, iTry));
2690 else if (ASMAtomicCmpXchgExU64(&pVM->tm.s.HzHint.u64Combined, RT_MAKE_U64(0, uMaxHzHint), u64Actual, &u64Actual))
2691 Log(("tmGetFrequencyHintSlow: New value %u Hz (try %u)\n", uMaxHzHint, iTry));
2692 else
2693 continue;
2694 break;
2695 }
2696 return uMaxHzHint;
2697}
2698
2699
2700/**
2701 * Gets the highest frequency hint for all the important timers.
2702 *
2703 * @returns The highest frequency. 0 if no timers care.
2704 * @param pVM The cross context VM structure.
2705 */
2706DECLINLINE(uint32_t) tmGetFrequencyHint(PVMCC pVM)
2707{
2708 /*
2709 * Query the value, recalculate it if necessary.
2710 */
2711 uint64_t u64Combined = ASMAtomicReadU64(&pVM->tm.s.HzHint.u64Combined);
2712 if (RT_HI_U32(u64Combined) == 0)
2713 return RT_LO_U32(u64Combined); /* hopefully somewhat likely */
2714 return tmGetFrequencyHintSlow(pVM, RT_LO_U32(u64Combined));
2715}
2716
2717
2718/**
2719 * Calculates a host timer frequency that would be suitable for the current
2720 * timer load.
2721 *
2722 * This will take the highest timer frequency, adjust for catch-up and warp
2723 * driver, and finally add a little fudge factor. The caller (VMM) will use
2724 * the result to adjust the per-cpu preemption timer.
2725 *
2726 * @returns The highest frequency. 0 if no important timers around.
2727 * @param pVM The cross context VM structure.
2728 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2729 */
2730VMM_INT_DECL(uint32_t) TMCalcHostTimerFrequency(PVMCC pVM, PVMCPUCC pVCpu)
2731{
2732 uint32_t uHz = tmGetFrequencyHint(pVM);
2733
2734 /* Catch up, we have to be more aggressive than the % indicates at the
2735 beginning of the effort. */
2736 if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
2737 {
2738 uint32_t u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage);
2739 if (ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
2740 {
2741 if (u32Pct <= 100)
2742 u32Pct = u32Pct * pVM->tm.s.cPctHostHzFudgeFactorCatchUp100 / 100;
2743 else if (u32Pct <= 200)
2744 u32Pct = u32Pct * pVM->tm.s.cPctHostHzFudgeFactorCatchUp200 / 100;
2745 else if (u32Pct <= 400)
2746 u32Pct = u32Pct * pVM->tm.s.cPctHostHzFudgeFactorCatchUp400 / 100;
2747 uHz *= u32Pct + 100;
2748 uHz /= 100;
2749 }
2750 }
2751
2752 /* Warp drive. */
2753 if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualWarpDrive))
2754 {
2755 uint32_t u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualWarpDrivePercentage);
2756 if (ASMAtomicReadBool(&pVM->tm.s.fVirtualWarpDrive))
2757 {
2758 uHz *= u32Pct;
2759 uHz /= 100;
2760 }
2761 }
2762
2763 /* Fudge factor. */
2764 if (pVCpu->idCpu == pVM->tm.s.idTimerCpu)
2765 uHz *= pVM->tm.s.cPctHostHzFudgeFactorTimerCpu;
2766 else
2767 uHz *= pVM->tm.s.cPctHostHzFudgeFactorOtherCpu;
2768 uHz /= 100;
2769
2770 /* Make sure it isn't too high. */
2771 if (uHz > pVM->tm.s.cHostHzMax)
2772 uHz = pVM->tm.s.cHostHzMax;
2773
2774 return uHz;
2775}
2776
2777
2778/**
2779 * Whether the guest virtual clock is ticking.
2780 *
2781 * @returns true if ticking, false otherwise.
2782 * @param pVM The cross context VM structure.
2783 */
2784VMM_INT_DECL(bool) TMVirtualIsTicking(PVM pVM)
2785{
2786 return RT_BOOL(pVM->tm.s.cVirtualTicking);
2787}
2788
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