VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/TMAllVirtual.cpp@ 19602

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

TM: Moved the WarpDrive feature out of TMVirtual and made the setter ring-3 only.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 24.5 KB
Line 
1/* $Id: TMAllVirtual.cpp 19500 2009-05-07 18:23:22Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, Virtual Time, All Contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_TM
27#include <VBox/tm.h>
28#ifdef IN_RING3
29# include <VBox/rem.h>
30# include <iprt/thread.h>
31#endif
32#include "TMInternal.h"
33#include <VBox/vm.h>
34#include <VBox/vmm.h>
35#include <VBox/err.h>
36#include <VBox/log.h>
37#include <VBox/sup.h>
38
39#include <iprt/time.h>
40#include <iprt/assert.h>
41#include <iprt/asm.h>
42
43
44
45/**
46 * Helper function that's used by the assembly routines when something goes bust.
47 *
48 * @param pData Pointer to the data structure.
49 * @param u64NanoTS The calculated nano ts.
50 * @param u64DeltaPrev The delta relative to the previously returned timestamp.
51 * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
52 */
53DECLEXPORT(void) tmVirtualNanoTSBad(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS)
54{
55 //PVM pVM = (PVM)((uint8_t *)pData - RT_OFFSETOF(VM, CTXALLSUFF(s.tm.VirtualGetRawData)));
56 pData->cBadPrev++;
57 if ((int64_t)u64DeltaPrev < 0)
58 LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64\n",
59 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
60 else
61 Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 (debugging?)\n",
62 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
63}
64
65
66/**
67 * Called the first time somebody asks for the time or when the GIP
68 * is mapped/unmapped.
69 *
70 * This should never ever happen.
71 */
72DECLEXPORT(uint64_t) tmVirtualNanoTSRediscover(PRTTIMENANOTSDATA pData)
73{
74 //PVM pVM = (PVM)((uint8_t *)pData - RT_OFFSETOF(VM, CTXALLSUFF(s.tm.VirtualGetRawData)));
75 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
76 AssertFatalMsgFailed(("pGip=%p u32Magic=%#x\n", pGip, VALID_PTR(pGip) ? pGip->u32Magic : 0));
77}
78
79
80#if 1
81
82/**
83 * Wrapper around the IPRT GIP time methods.
84 */
85DECLINLINE(uint64_t) tmVirtualGetRawNanoTS(PVM pVM)
86{
87#ifdef IN_RING3
88 return CTXALLSUFF(pVM->tm.s.pfnVirtualGetRaw)(&CTXALLSUFF(pVM->tm.s.VirtualGetRawData));
89# else /* !IN_RING3 */
90 uint32_t cPrevSteps = pVM->tm.s.CTX_SUFF(VirtualGetRawData).c1nsSteps;
91 uint64_t u64 = pVM->tm.s.CTX_SUFF(pfnVirtualGetRaw)(&pVM->tm.s.CTX_SUFF(VirtualGetRawData));
92 if (cPrevSteps != pVM->tm.s.CTX_SUFF(VirtualGetRawData).c1nsSteps)
93 VMCPU_FF_SET(VMMGetCpu(pVM), VMCPU_FF_TO_R3);
94 return u64;
95# endif /* !IN_RING3 */
96}
97
98#else
99
100/**
101 * This is (mostly) the same as rtTimeNanoTSInternal() except
102 * for the two globals which live in TM.
103 *
104 * @returns Nanosecond timestamp.
105 * @param pVM The VM handle.
106 */
107static uint64_t tmVirtualGetRawNanoTS(PVM pVM)
108{
109 uint64_t u64Delta;
110 uint32_t u32NanoTSFactor0;
111 uint64_t u64TSC;
112 uint64_t u64NanoTS;
113 uint32_t u32UpdateIntervalTSC;
114 uint64_t u64PrevNanoTS;
115
116 /*
117 * Read the GIP data and the previous value.
118 */
119 for (;;)
120 {
121 uint32_t u32TransactionId;
122 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
123#ifdef IN_RING3
124 if (RT_UNLIKELY(!pGip || pGip->u32Magic != SUPGLOBALINFOPAGE_MAGIC))
125 return RTTimeSystemNanoTS();
126#endif
127
128 if (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
129 {
130 u32TransactionId = pGip->aCPUs[0].u32TransactionId;
131#ifdef RT_OS_L4
132 Assert((u32TransactionId & 1) == 0);
133#endif
134 u32UpdateIntervalTSC = pGip->aCPUs[0].u32UpdateIntervalTSC;
135 u64NanoTS = pGip->aCPUs[0].u64NanoTS;
136 u64TSC = pGip->aCPUs[0].u64TSC;
137 u32NanoTSFactor0 = pGip->u32UpdateIntervalNS;
138 u64Delta = ASMReadTSC();
139 u64PrevNanoTS = ASMAtomicReadU64(&pVM->tm.s.u64VirtualRawPrev);
140 if (RT_UNLIKELY( pGip->aCPUs[0].u32TransactionId != u32TransactionId
141 || (u32TransactionId & 1)))
142 continue;
143 }
144 else
145 {
146 /* SUPGIPMODE_ASYNC_TSC */
147 PSUPGIPCPU pGipCpu;
148
149 uint8_t u8ApicId = ASMGetApicId();
150 if (RT_LIKELY(u8ApicId < RT_ELEMENTS(pGip->aCPUs)))
151 pGipCpu = &pGip->aCPUs[u8ApicId];
152 else
153 {
154 AssertMsgFailed(("%x\n", u8ApicId));
155 pGipCpu = &pGip->aCPUs[0];
156 }
157
158 u32TransactionId = pGipCpu->u32TransactionId;
159#ifdef RT_OS_L4
160 Assert((u32TransactionId & 1) == 0);
161#endif
162 u32UpdateIntervalTSC = pGipCpu->u32UpdateIntervalTSC;
163 u64NanoTS = pGipCpu->u64NanoTS;
164 u64TSC = pGipCpu->u64TSC;
165 u32NanoTSFactor0 = pGip->u32UpdateIntervalNS;
166 u64Delta = ASMReadTSC();
167 u64PrevNanoTS = ASMAtomicReadU64(&pVM->tm.s.u64VirtualRawPrev);
168#ifdef IN_RC
169 Assert(!(ASMGetFlags() & X86_EFL_IF));
170#else
171 if (RT_UNLIKELY(u8ApicId != ASMGetApicId()))
172 continue;
173 if (RT_UNLIKELY( pGipCpu->u32TransactionId != u32TransactionId
174 || (u32TransactionId & 1)))
175 continue;
176#endif
177 }
178 break;
179 }
180
181 /*
182 * Calc NanoTS delta.
183 */
184 u64Delta -= u64TSC;
185 if (u64Delta > u32UpdateIntervalTSC)
186 {
187 /*
188 * We've expired the interval, cap it. If we're here for the 2nd
189 * time without any GIP update inbetween, the checks against
190 * pVM->tm.s.u64VirtualRawPrev below will force 1ns stepping.
191 */
192 u64Delta = u32UpdateIntervalTSC;
193 }
194#if !defined(_MSC_VER) || defined(RT_ARCH_AMD64) /* GCC makes very pretty code from these two inline calls, while MSC cannot. */
195 u64Delta = ASMMult2xU32RetU64((uint32_t)u64Delta, u32NanoTSFactor0);
196 u64Delta = ASMDivU64ByU32RetU32(u64Delta, u32UpdateIntervalTSC);
197#else
198 __asm
199 {
200 mov eax, dword ptr [u64Delta]
201 mul dword ptr [u32NanoTSFactor0]
202 div dword ptr [u32UpdateIntervalTSC]
203 mov dword ptr [u64Delta], eax
204 xor edx, edx
205 mov dword ptr [u64Delta + 4], edx
206 }
207#endif
208
209 /*
210 * Calculate the time and compare it with the previously returned value.
211 *
212 * Since this function is called *very* frequently when the VM is running
213 * and then mostly on EMT, we can restrict the valid range of the delta
214 * (-1s to 2*GipUpdates) and simplify/optimize the default path.
215 */
216 u64NanoTS += u64Delta;
217 uint64_t u64DeltaPrev = u64NanoTS - u64PrevNanoTS;
218 if (RT_LIKELY(u64DeltaPrev < 1000000000 /* 1s */))
219 /* frequent - less than 1s since last call. */;
220 else if ( (int64_t)u64DeltaPrev < 0
221 && (int64_t)u64DeltaPrev + u32NanoTSFactor0 * 2 > 0)
222 {
223 /* occasional - u64NanoTS is in the 'past' relative to previous returns. */
224 ASMAtomicIncU32(&pVM->tm.s.CTX_SUFF(VirtualGetRawData).c1nsSteps);
225 u64NanoTS = u64PrevNanoTS + 1;
226#ifndef IN_RING3
227 VM_FF_SET(pVM, VM_FF_TO_R3); /* S10 hack */
228#endif
229 }
230 else if (u64PrevNanoTS)
231 {
232 /* Something has gone bust, if negative offset it's real bad. */
233 ASMAtomicIncU32(&pVM->tm.s.CTX_SUFF(VirtualGetRawData).cBadPrev);
234 if ((int64_t)u64DeltaPrev < 0)
235 LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 u64Delta=%#RX64\n",
236 u64DeltaPrev, u64PrevNanoTS, u64NanoTS, u64Delta));
237 else
238 Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 u64Delta=%#RX64 (debugging?)\n",
239 u64DeltaPrev, u64PrevNanoTS, u64NanoTS, u64Delta));
240#ifdef DEBUG_bird
241 /** @todo there are some hickups during boot and reset that can cause 2-5 seconds delays. Investigate... */
242 AssertMsg(u64PrevNanoTS > UINT64_C(100000000000) /* 100s */,
243 ("u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 u64Delta=%#RX64\n",
244 u64DeltaPrev, u64PrevNanoTS, u64NanoTS, u64Delta));
245#endif
246 }
247 /* else: We're resuming (see TMVirtualResume). */
248 if (RT_LIKELY(ASMAtomicCmpXchgU64(&pVM->tm.s.u64VirtualRawPrev, u64NanoTS, u64PrevNanoTS)))
249 return u64NanoTS;
250
251 /*
252 * Attempt updating the previous value, provided we're still ahead of it.
253 *
254 * There is no point in recalculating u64NanoTS because we got preemted or if
255 * we raced somebody while the GIP was updated, since these are events
256 * that might occure at any point in the return path as well.
257 */
258 for (int cTries = 50;;)
259 {
260 u64PrevNanoTS = ASMAtomicReadU64(&pVM->tm.s.u64VirtualRawPrev);
261 if (u64PrevNanoTS >= u64NanoTS)
262 break;
263 if (ASMAtomicCmpXchgU64(&pVM->tm.s.u64VirtualRawPrev, u64NanoTS, u64PrevNanoTS))
264 break;
265 AssertBreak(--cTries <= 0);
266 if (cTries < 25 && !VM_IS_EMT(pVM)) /* give up early */
267 break;
268 }
269
270 return u64NanoTS;
271}
272
273#endif
274
275
276/**
277 * Get the time when we're not running at 100%
278 *
279 * @returns The timestamp.
280 * @param pVM The VM handle.
281 */
282static uint64_t tmVirtualGetRawNonNormal(PVM pVM)
283{
284 /*
285 * Recalculate the RTTimeNanoTS() value for the period where
286 * warp drive has been enabled.
287 */
288 uint64_t u64 = tmVirtualGetRawNanoTS(pVM);
289 u64 -= pVM->tm.s.u64VirtualWarpDriveStart;
290 u64 *= pVM->tm.s.u32VirtualWarpDrivePercentage;
291 u64 /= 100;
292 u64 += pVM->tm.s.u64VirtualWarpDriveStart;
293
294 /*
295 * Now we apply the virtual time offset.
296 * (Which is the negated tmVirtualGetRawNanoTS() value for when the virtual
297 * machine started if it had been running continuously without any suspends.)
298 */
299 u64 -= pVM->tm.s.u64VirtualOffset;
300 return u64;
301}
302
303
304/**
305 * Get the raw virtual time.
306 *
307 * @returns The current time stamp.
308 * @param pVM The VM handle.
309 */
310DECLINLINE(uint64_t) tmVirtualGetRaw(PVM pVM)
311{
312 if (RT_LIKELY(!pVM->tm.s.fVirtualWarpDrive))
313 return tmVirtualGetRawNanoTS(pVM) - pVM->tm.s.u64VirtualOffset;
314 return tmVirtualGetRawNonNormal(pVM);
315}
316
317
318/**
319 * Inlined version of tmVirtualGetEx.
320 */
321DECLINLINE(uint64_t) tmVirtualGet(PVM pVM, bool fCheckTimers)
322{
323 uint64_t u64;
324 if (RT_LIKELY(pVM->tm.s.cVirtualTicking))
325 {
326 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualGet);
327 u64 = tmVirtualGetRaw(pVM);
328
329 /*
330 * Use the chance to check for expired timers.
331 */
332 if ( fCheckTimers
333 && !VM_FF_ISSET(pVM, VM_FF_TIMER)
334 && !pVM->tm.s.fRunningQueues
335 && ( pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire <= u64
336 || ( pVM->tm.s.fVirtualSyncTicking
337 && pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire <= u64 - pVM->tm.s.offVirtualSync
338 )
339 )
340 )
341 {
342 VM_FF_SET(pVM, VM_FF_TIMER);
343 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualGetSetFF);
344#ifdef IN_RING3
345 REMR3NotifyTimerPending(pVM);
346 VMR3NotifyGlobalFFU(pVM->pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
347#endif
348 }
349 }
350 else
351 u64 = pVM->tm.s.u64Virtual;
352 return u64;
353}
354
355
356/**
357 * Gets the current TMCLOCK_VIRTUAL time
358 *
359 * @returns The timestamp.
360 * @param pVM VM handle.
361 *
362 * @remark While the flow of time will never go backwards, the speed of the
363 * progress varies due to inaccurate RTTimeNanoTS and TSC. The latter can be
364 * influenced by power saving (SpeedStep, PowerNow!), while the former
365 * makes use of TSC and kernel timers.
366 */
367VMMDECL(uint64_t) TMVirtualGet(PVM pVM)
368{
369 return tmVirtualGet(pVM, true /* check timers */);
370}
371
372
373/**
374 * Gets the current TMCLOCK_VIRTUAL time
375 *
376 * @returns The timestamp.
377 * @param pVM VM handle.
378 * @param fCheckTimers Check timers or not
379 *
380 * @remark While the flow of time will never go backwards, the speed of the
381 * progress varies due to inaccurate RTTimeNanoTS and TSC. The latter can be
382 * influenced by power saving (SpeedStep, PowerNow!), while the former
383 * makes use of TSC and kernel timers.
384 */
385VMMDECL(uint64_t) TMVirtualGetEx(PVM pVM, bool fCheckTimers)
386{
387 return tmVirtualGet(pVM, fCheckTimers);
388}
389
390
391/**
392 * Gets the current TMCLOCK_VIRTUAL_SYNC time.
393 *
394 * @returns The timestamp.
395 * @param pVM VM handle.
396 * @param fCheckTimers Check timers or not
397 * @thread EMT.
398 */
399VMMDECL(uint64_t) TMVirtualSyncGetEx(PVM pVM, bool fCheckTimers)
400{
401 uint64_t u64;
402 if (pVM->tm.s.fVirtualSyncTicking)
403 {
404 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualGetSync);
405
406 /*
407 * Query the virtual clock and do the usual expired timer check.
408 */
409 Assert(pVM->tm.s.cVirtualTicking);
410 u64 = tmVirtualGetRaw(pVM);
411 if ( fCheckTimers
412 && !VM_FF_ISSET(pVM, VM_FF_TIMER)
413 && pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire <= u64)
414 {
415 VM_FF_SET(pVM, VM_FF_TIMER);
416#ifdef IN_RING3
417 REMR3NotifyTimerPending(pVM);
418 VMR3NotifyGlobalFFU(pVM->pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
419#endif
420 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualGetSyncSetFF);
421 }
422
423 /*
424 * Read the offset and adjust if we're playing catch-up.
425 *
426 * The catch-up adjusting work by us decrementing the offset by a percentage of
427 * the time elapsed since the previous TMVirtualGetSync call.
428 *
429 * It's possible to get a very long or even negative interval between two read
430 * for the following reasons:
431 * - Someone might have suspended the process execution, frequently the case when
432 * debugging the process.
433 * - We might be on a different CPU which TSC isn't quite in sync with the
434 * other CPUs in the system.
435 * - Another thread is racing us and we might have been preemnted while inside
436 * this function.
437 *
438 * Assuming nano second virtual time, we can simply ignore any intervals which has
439 * any of the upper 32 bits set.
440 */
441 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
442 uint64_t off = pVM->tm.s.offVirtualSync;
443 if (pVM->tm.s.fVirtualSyncCatchUp)
444 {
445 int rc = tmTryLock(pVM); /** @todo SMP: Here be dragons... Need to get back to this later. */
446
447 const uint64_t u64Prev = pVM->tm.s.u64VirtualSyncCatchUpPrev;
448 uint64_t u64Delta = u64 - u64Prev;
449 if (RT_LIKELY(!(u64Delta >> 32)))
450 {
451 uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
452 if (off > u64Sub + pVM->tm.s.offVirtualSyncGivenUp)
453 {
454 off -= u64Sub;
455 ASMAtomicXchgU64(&pVM->tm.s.offVirtualSync, off);
456 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64;
457 Log4(("TM: %RU64/%RU64: sub %RU32\n", u64 - off, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp, u64Sub));
458 }
459 else
460 {
461 /* we've completely caught up. */
462 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
463 off = pVM->tm.s.offVirtualSyncGivenUp;
464 ASMAtomicXchgU64(&pVM->tm.s.offVirtualSync, off);
465 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
466 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64;
467 Log4(("TM: %RU64/0: caught up\n", u64));
468 }
469 }
470 else
471 {
472 /* More than 4 seconds since last time (or negative), ignore it. */
473 if (!(u64Delta & RT_BIT_64(63)))
474 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64;
475 Log(("TMVirtualGetSync: u64Delta=%RX64\n", u64Delta));
476 }
477
478 if (RT_SUCCESS(rc))
479 tmUnlock(pVM);
480 }
481
482 /*
483 * Complete the calculation of the current TMCLOCK_VIRTUAL_SYNC time. The current
484 * approach is to never pass the head timer. So, when we do stop the clock and
485 * set the timer pending flag.
486 */
487 u64 -= off;
488 const uint64_t u64Expire = pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire;
489 if (u64 >= u64Expire)
490 {
491 u64 = u64Expire;
492 int rc = tmTryLock(pVM); /** @todo SMP: Here be dragons... Need to get back to this later. */
493 if (RT_SUCCESS(rc))
494 {
495 ASMAtomicXchgU64(&pVM->tm.s.u64VirtualSync, u64);
496 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncTicking, false);
497 tmUnlock(pVM);
498 }
499 if ( fCheckTimers
500 && !VM_FF_ISSET(pVM, VM_FF_TIMER))
501 {
502 VM_FF_SET(pVM, VM_FF_TIMER);
503#ifdef IN_RING3
504 REMR3NotifyTimerPending(pVM);
505 VMR3NotifyGlobalFFU(pVM->pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
506#endif
507 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualGetSyncSetFF);
508 Log4(("TM: %RU64/%RU64: exp tmr=>ff\n", u64, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp));
509 }
510 else
511 Log4(("TM: %RU64/%RU64: exp tmr\n", u64, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp));
512 }
513 }
514 else
515 {
516 u64 = pVM->tm.s.u64VirtualSync;
517
518 /*
519 * If it looks like a halt caused by pending timers, make sure the FF is raised.
520 * This is a safeguard against timer queue runner leaving the virtual sync clock stopped.
521 */
522 if ( fCheckTimers
523 && pVM->tm.s.cVirtualTicking
524 && !VM_FF_ISSET(pVM, VM_FF_TIMER))
525 {
526 const uint64_t u64Expire = pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire;
527 if (u64 >= u64Expire)
528 {
529 VM_FF_SET(pVM, VM_FF_TIMER);
530#ifdef IN_RING3
531 REMR3NotifyTimerPending(pVM);
532 VMR3NotifyGlobalFFU(pVM->pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
533#endif
534 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualGetSyncSetFF);
535 Log4(("TM: %RU64/%RU64: exp tmr=>ff (!)\n", u64, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp));
536 }
537 }
538 }
539 return u64;
540}
541
542
543/**
544 * Gets the current TMCLOCK_VIRTUAL_SYNC time.
545 *
546 * @returns The timestamp.
547 * @param pVM VM handle.
548 * @thread EMT.
549 */
550VMMDECL(uint64_t) TMVirtualSyncGet(PVM pVM)
551{
552 return TMVirtualSyncGetEx(pVM, true /* check timers */);
553}
554
555
556/**
557 * Gets the current lag of the synchronous virtual clock (relative to the virtual clock).
558 *
559 * @return The current lag.
560 * @param pVM VM handle.
561 */
562VMMDECL(uint64_t) TMVirtualSyncGetLag(PVM pVM)
563{
564 return pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp;
565}
566
567
568/**
569 * Get the current catch-up percent.
570 *
571 * @return The current catch0up percent. 0 means running at the same speed as the virtual clock.
572 * @param pVM VM handle.
573 */
574VMMDECL(uint32_t) TMVirtualSyncGetCatchUpPct(PVM pVM)
575{
576 if (pVM->tm.s.fVirtualSyncCatchUp)
577 return pVM->tm.s.u32VirtualSyncCatchUpPercentage;
578 return 0;
579}
580
581
582/**
583 * Gets the current TMCLOCK_VIRTUAL frequency.
584 *
585 * @returns The freqency.
586 * @param pVM VM handle.
587 */
588VMMDECL(uint64_t) TMVirtualGetFreq(PVM pVM)
589{
590 return TMCLOCK_FREQ_VIRTUAL;
591}
592
593
594/**
595 * Resumes the virtual clock.
596 *
597 * @returns VINF_SUCCESS on success.
598 * @returns VINF_INTERNAL_ERROR and VBOX_STRICT assertion if called out of order.
599 * @param pVM VM handle.
600 */
601VMMDECL(int) TMVirtualResume(PVM pVM)
602{
603 /*
604 * Note! this is done only in specific cases (vcpu 0 init, termination, debug,
605 * out of memory conditions; there is at least a race for fVirtualSyncTicking.
606 */
607 if (ASMAtomicIncU32(&pVM->tm.s.cVirtualTicking) == 1)
608 {
609 int rc = tmLock(pVM); /* paranoia */
610
611 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualResume);
612 pVM->tm.s.u64VirtualRawPrev = 0;
613 pVM->tm.s.u64VirtualWarpDriveStart = tmVirtualGetRawNanoTS(pVM);
614 pVM->tm.s.u64VirtualOffset = pVM->tm.s.u64VirtualWarpDriveStart - pVM->tm.s.u64Virtual;
615 pVM->tm.s.fVirtualSyncTicking = true;
616
617 if (RT_SUCCESS(rc))
618 tmUnlock(pVM);
619 return VINF_SUCCESS;
620 }
621 AssertMsgReturn(pVM->tm.s.cVirtualTicking <= pVM->cCPUs, ("%d vs %d\n", pVM->tm.s.cVirtualTicking, pVM->cCPUs), VERR_INTERNAL_ERROR);
622 return VINF_SUCCESS;
623}
624
625
626/**
627 * Pauses the virtual clock.
628 *
629 * @returns VINF_SUCCESS on success.
630 * @returns VINF_INTERNAL_ERROR and VBOX_STRICT assertion if called out of order.
631 * @param pVM VM handle.
632 */
633VMMDECL(int) TMVirtualPause(PVM pVM)
634{
635 /*
636 * Note! this is done only in specific cases (vcpu 0 init, termination, debug,
637 * out of memory conditions; there is at least a race for fVirtualSyncTicking.
638 */
639 if (ASMAtomicDecU32(&pVM->tm.s.cVirtualTicking) == 0)
640 {
641 int rc = tmLock(pVM); /* paranoia */
642
643 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualPause);
644 pVM->tm.s.u64Virtual = tmVirtualGetRaw(pVM);
645 pVM->tm.s.fVirtualSyncTicking = false;
646
647 if (RT_SUCCESS(rc))
648 tmUnlock(pVM);
649 return VINF_SUCCESS;
650 }
651 AssertMsgReturn(pVM->tm.s.cVirtualTicking <= pVM->cCPUs, ("%d vs %d\n", pVM->tm.s.cVirtualTicking, pVM->cCPUs), VERR_INTERNAL_ERROR);
652 return VINF_SUCCESS;
653}
654
655
656/**
657 * Converts from virtual ticks to nanoseconds.
658 *
659 * @returns nanoseconds.
660 * @param pVM The VM handle.
661 * @param u64VirtualTicks The virtual ticks to convert.
662 * @remark There could be rounding errors here. We just do a simple integere divide
663 * without any adjustments.
664 */
665VMMDECL(uint64_t) TMVirtualToNano(PVM pVM, uint64_t u64VirtualTicks)
666{
667 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
668 return u64VirtualTicks;
669}
670
671
672/**
673 * Converts from virtual ticks to microseconds.
674 *
675 * @returns microseconds.
676 * @param pVM The VM handle.
677 * @param u64VirtualTicks The virtual ticks to convert.
678 * @remark There could be rounding errors here. We just do a simple integere divide
679 * without any adjustments.
680 */
681VMMDECL(uint64_t) TMVirtualToMicro(PVM pVM, uint64_t u64VirtualTicks)
682{
683 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
684 return u64VirtualTicks / 1000;
685}
686
687
688/**
689 * Converts from virtual ticks to milliseconds.
690 *
691 * @returns milliseconds.
692 * @param pVM The VM handle.
693 * @param u64VirtualTicks The virtual ticks to convert.
694 * @remark There could be rounding errors here. We just do a simple integere divide
695 * without any adjustments.
696 */
697VMMDECL(uint64_t) TMVirtualToMilli(PVM pVM, uint64_t u64VirtualTicks)
698{
699 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
700 return u64VirtualTicks / 1000000;
701}
702
703
704/**
705 * Converts from nanoseconds to virtual ticks.
706 *
707 * @returns virtual ticks.
708 * @param pVM The VM handle.
709 * @param u64NanoTS The nanosecond value ticks to convert.
710 * @remark There could be rounding and overflow errors here.
711 */
712VMMDECL(uint64_t) TMVirtualFromNano(PVM pVM, uint64_t u64NanoTS)
713{
714 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
715 return u64NanoTS;
716}
717
718
719/**
720 * Converts from microseconds to virtual ticks.
721 *
722 * @returns virtual ticks.
723 * @param pVM The VM handle.
724 * @param u64MicroTS The microsecond value ticks to convert.
725 * @remark There could be rounding and overflow errors here.
726 */
727VMMDECL(uint64_t) TMVirtualFromMicro(PVM pVM, uint64_t u64MicroTS)
728{
729 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
730 return u64MicroTS * 1000;
731}
732
733
734/**
735 * Converts from milliseconds to virtual ticks.
736 *
737 * @returns virtual ticks.
738 * @param pVM The VM handle.
739 * @param u64MilliTS The millisecond value ticks to convert.
740 * @remark There could be rounding and overflow errors here.
741 */
742VMMDECL(uint64_t) TMVirtualFromMilli(PVM pVM, uint64_t u64MilliTS)
743{
744 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
745 return u64MilliTS * 1000000;
746}
747
Note: See TracBrowser for help on using the repository browser.

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