VirtualBox

source: vbox/trunk/src/VBox/VMM/TM.cpp@ 19495

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

TM/doxygen: show the nice timer state chart.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 92.5 KB
Line 
1/* $Id: TM.cpp 19491 2009-05-07 15:46:54Z vboxsync $ */
2/** @file
3 * TM - Time Manager.
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/** @page pg_tm TM - The Time Manager
23 *
24 * The Time Manager abstracts the CPU clocks and manages timers used by the VMM,
25 * device and drivers.
26 *
27 * @see grp_tm
28 *
29 *
30 * @section sec_tm_clocks Clocks
31 *
32 * There are currently 4 clocks:
33 * - Virtual (guest).
34 * - Synchronous virtual (guest).
35 * - CPU Tick (TSC) (guest). Only current use is rdtsc emulation. Usually a
36 * function of the virtual clock.
37 * - Real (host). This is only used for display updates atm.
38 *
39 * The most important clocks are the three first ones and of these the second is
40 * the most interesting.
41 *
42 *
43 * The synchronous virtual clock is tied to the virtual clock except that it
44 * will take into account timer delivery lag caused by host scheduling. It will
45 * normally never advance beyond the head timer, and when lagging too far behind
46 * it will gradually speed up to catch up with the virtual clock. All devices
47 * implementing time sources accessible to and used by the guest is using this
48 * clock (for timers and other things). This ensures consistency between the
49 * time sources.
50 *
51 * The virtual clock is implemented as an offset to a monotonic, high
52 * resolution, wall clock. The current time source is using the RTTimeNanoTS()
53 * machinery based upon the Global Info Pages (GIP), that is, we're using TSC
54 * deltas (usually 10 ms) to fill the gaps between GIP updates. The result is
55 * a fairly high res clock that works in all contexts and on all hosts. The
56 * virtual clock is paused when the VM isn't in the running state.
57 *
58 * The CPU tick (TSC) is normally virtualized as a function of the synchronous
59 * virtual clock, where the frequency defaults to the host cpu frequency (as we
60 * measure it). In this mode it is possible to configure the frequency. Another
61 * (non-default) option is to use the raw unmodified host TSC values. And yet
62 * another, to tie it to time spent executing guest code. All these things are
63 * configurable should non-default behavior be desirable.
64 *
65 * The real clock is a monotonic clock (when available) with relatively low
66 * resolution, though this a bit host specific. Note that we're currently not
67 * servicing timers using the real clock when the VM is not running, this is
68 * simply because it has not been needed yet therefore not implemented.
69 *
70 *
71 * @subsection subsec_tm_timesync Guest Time Sync / UTC time
72 *
73 * Guest time syncing is primarily taken care of by the VMM device. The
74 * principle is very simple, the guest additions periodically asks the VMM
75 * device what the current UTC time is and makes adjustments accordingly.
76 *
77 * A complicating factor is that the synchronous virtual clock might be doing
78 * catchups and the guest perception is currently a little bit behind the world
79 * but it will (hopefully) be catching up soon as we're feeding timer interrupts
80 * at a slightly higher rate. Adjusting the guest clock to the current wall
81 * time in the real world would be a bad idea then because the guest will be
82 * advancing too fast and run ahead of world time (if the catchup works out).
83 * To solve this problem TM provides the VMM device with an UTC time source that
84 * gets adjusted with the current lag, so that when the guest eventually catches
85 * up the lag it will be showing correct real world time.
86 *
87 *
88 * @section sec_tm_timers Timers
89 *
90 * The timers can use any of the TM clocks described in the previous section.
91 * Each clock has its own scheduling facility, or timer queue if you like.
92 * There are a few factors which makes it a bit complex. First, there is the
93 * usual R0 vs R3 vs. RC thing. Then there are multiple threads, and then there
94 * is the timer thread that periodically checks whether any timers has expired
95 * without EMT noticing. On the API level, all but the create and save APIs
96 * must be mulithreaded. EMT will always run the timers.
97 *
98 * The design is using a doubly linked list of active timers which is ordered
99 * by expire date. This list is only modified by the EMT thread. Updates to
100 * the list are batched in a singly linked list, which is then processed by the
101 * EMT thread at the first opportunity (immediately, next time EMT modifies a
102 * timer on that clock, or next timer timeout). Both lists are offset based and
103 * all the elements are therefore allocated from the hyper heap.
104 *
105 * For figuring out when there is need to schedule and run timers TM will:
106 * - Poll whenever somebody queries the virtual clock.
107 * - Poll the virtual clocks from the EM and REM loops.
108 * - Poll the virtual clocks from trap exit path.
109 * - Poll the virtual clocks and calculate first timeout from the halt loop.
110 * - Employ a thread which periodically (100Hz) polls all the timer queues.
111 *
112 *
113 * @image html TMTIMER-Statechart-Diagram.gif
114 *
115 * @section sec_tm_timer Logging
116 *
117 * Level 2: Logs a most of the timer state transitions and queue servicing.
118 * Level 3: Logs a few oddments.
119 * Level 4: Logs TMCLOCK_VIRTUAL_SYNC catch-up events.
120 *
121 */
122
123/*******************************************************************************
124* Header Files *
125*******************************************************************************/
126#define LOG_GROUP LOG_GROUP_TM
127#include <VBox/tm.h>
128#include <VBox/vmm.h>
129#include <VBox/mm.h>
130#include <VBox/ssm.h>
131#include <VBox/dbgf.h>
132#include <VBox/rem.h>
133#include <VBox/pdm.h>
134#include "TMInternal.h"
135#include <VBox/vm.h>
136
137#include <VBox/param.h>
138#include <VBox/err.h>
139
140#include <VBox/log.h>
141#include <iprt/asm.h>
142#include <iprt/assert.h>
143#include <iprt/thread.h>
144#include <iprt/time.h>
145#include <iprt/timer.h>
146#include <iprt/semaphore.h>
147#include <iprt/string.h>
148#include <iprt/env.h>
149
150
151/*******************************************************************************
152* Defined Constants And Macros *
153*******************************************************************************/
154/** The current saved state version.*/
155#define TM_SAVED_STATE_VERSION 3
156
157
158/*******************************************************************************
159* Internal Functions *
160*******************************************************************************/
161static bool tmR3HasFixedTSC(PVM pVM);
162static uint64_t tmR3CalibrateTSC(PVM pVM);
163static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM);
164static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
165static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
166static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue);
167static void tmR3TimerQueueRunVirtualSync(PVM pVM);
168static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
169static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
170static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
171
172
173/**
174 * Initializes the TM.
175 *
176 * @returns VBox status code.
177 * @param pVM The VM to operate on.
178 */
179VMMR3DECL(int) TMR3Init(PVM pVM)
180{
181 LogFlow(("TMR3Init:\n"));
182
183 /*
184 * Assert alignment and sizes.
185 */
186 AssertRelease(!(RT_OFFSETOF(VM, tm.s) & 31));
187 AssertRelease(sizeof(pVM->tm.s) <= sizeof(pVM->tm.padding));
188
189 /*
190 * Init the structure.
191 */
192 void *pv;
193 int rc = MMHyperAlloc(pVM, sizeof(pVM->tm.s.paTimerQueuesR3[0]) * TMCLOCK_MAX, 0, MM_TAG_TM, &pv);
194 AssertRCReturn(rc, rc);
195 pVM->tm.s.paTimerQueuesR3 = (PTMTIMERQUEUE)pv;
196 pVM->tm.s.paTimerQueuesR0 = MMHyperR3ToR0(pVM, pv);
197 pVM->tm.s.paTimerQueuesRC = MMHyperR3ToRC(pVM, pv);
198
199 pVM->tm.s.offVM = RT_OFFSETOF(VM, tm.s);
200 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].enmClock = TMCLOCK_VIRTUAL;
201 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].u64Expire = INT64_MAX;
202 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].enmClock = TMCLOCK_VIRTUAL_SYNC;
203 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].u64Expire = INT64_MAX;
204 pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].enmClock = TMCLOCK_REAL;
205 pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].u64Expire = INT64_MAX;
206 pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].enmClock = TMCLOCK_TSC;
207 pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].u64Expire = INT64_MAX;
208
209 /*
210 * We directly use the GIP to calculate the virtual time. We map the
211 * the GIP into the guest context so we can do this calculation there
212 * as well and save costly world switches.
213 */
214 pVM->tm.s.pvGIPR3 = (void *)g_pSUPGlobalInfoPage;
215 AssertMsgReturn(pVM->tm.s.pvGIPR3, ("GIP support is now required!\n"), VERR_INTERNAL_ERROR);
216 RTHCPHYS HCPhysGIP;
217 rc = SUPGipGetPhys(&HCPhysGIP);
218 AssertMsgRCReturn(rc, ("Failed to get GIP physical address!\n"), rc);
219
220 RTGCPTR GCPtr;
221 rc = MMR3HyperMapHCPhys(pVM, pVM->tm.s.pvGIPR3, NIL_RTR0PTR, HCPhysGIP, PAGE_SIZE, "GIP", &GCPtr);
222 if (RT_FAILURE(rc))
223 {
224 AssertMsgFailed(("Failed to map GIP into GC, rc=%Rrc!\n", rc));
225 return rc;
226 }
227 pVM->tm.s.pvGIPRC = GCPtr;
228 LogFlow(("TMR3Init: HCPhysGIP=%RHp at %RRv\n", HCPhysGIP, pVM->tm.s.pvGIPRC));
229 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
230
231 /* Check assumptions made in TMAllVirtual.cpp about the GIP update interval. */
232 if ( g_pSUPGlobalInfoPage->u32Magic == SUPGLOBALINFOPAGE_MAGIC
233 && g_pSUPGlobalInfoPage->u32UpdateIntervalNS >= 250000000 /* 0.25s */)
234 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
235 N_("The GIP update interval is too big. u32UpdateIntervalNS=%RU32 (u32UpdateHz=%RU32)"),
236 g_pSUPGlobalInfoPage->u32UpdateIntervalNS, g_pSUPGlobalInfoPage->u32UpdateHz);
237 LogRel(("TM: GIP - u32Mode=%d (%s) u32UpdateHz=%u\n", g_pSUPGlobalInfoPage->u32Mode,
238 g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC ? "SyncTSC"
239 : g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_ASYNC_TSC ? "AsyncTSC" : "Unknown",
240 g_pSUPGlobalInfoPage->u32UpdateHz));
241
242 /*
243 * Setup the VirtualGetRaw backend.
244 */
245 pVM->tm.s.VirtualGetRawDataR3.pu64Prev = &pVM->tm.s.u64VirtualRawPrev;
246 pVM->tm.s.VirtualGetRawDataR3.pfnBad = tmVirtualNanoTSBad;
247 pVM->tm.s.VirtualGetRawDataR3.pfnRediscover = tmVirtualNanoTSRediscover;
248 if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
249 {
250 if (g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC)
251 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLFenceSync;
252 else
253 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLFenceAsync;
254 }
255 else
256 {
257 if (g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC)
258 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLegacySync;
259 else
260 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLegacyAsync;
261 }
262
263 pVM->tm.s.VirtualGetRawDataRC.pu64Prev = MMHyperR3ToRC(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
264 pVM->tm.s.VirtualGetRawDataR0.pu64Prev = MMHyperR3ToR0(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
265 AssertReturn(pVM->tm.s.VirtualGetRawDataR0.pu64Prev, VERR_INTERNAL_ERROR);
266 /* The rest is done in TMR3InitFinalize since it's too early to call PDM. */
267
268 /*
269 * Init the lock.
270 */
271 rc = PDMR3CritSectInit(pVM, &pVM->tm.s.EmtLock, "TM EMT Lock");
272 if (RT_FAILURE(rc))
273 return rc;
274
275 /*
276 * Get our CFGM node, create it if necessary.
277 */
278 PCFGMNODE pCfgHandle = CFGMR3GetChild(CFGMR3GetRoot(pVM), "TM");
279 if (!pCfgHandle)
280 {
281 rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "TM", &pCfgHandle);
282 AssertRCReturn(rc, rc);
283 }
284
285 /*
286 * Determin the TSC configuration and frequency.
287 */
288 /* mode */
289 /** @cfgm{/TM/TSCVirtualized,bool,true}
290 * Use a virtualize TSC, i.e. trap all TSC access. */
291 rc = CFGMR3QueryBool(pCfgHandle, "TSCVirtualized", &pVM->tm.s.fTSCVirtualized);
292 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
293 pVM->tm.s.fTSCVirtualized = true; /* trap rdtsc */
294 else if (RT_FAILURE(rc))
295 return VMSetError(pVM, rc, RT_SRC_POS,
296 N_("Configuration error: Failed to querying bool value \"UseRealTSC\""));
297
298 /* source */
299 /** @cfgm{/TM/UseRealTSC,bool,false}
300 * Use the real TSC as time source for the TSC instead of the synchronous
301 * virtual clock (false, default). */
302 rc = CFGMR3QueryBool(pCfgHandle, "UseRealTSC", &pVM->tm.s.fTSCUseRealTSC);
303 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
304 pVM->tm.s.fTSCUseRealTSC = false; /* use virtual time */
305 else if (RT_FAILURE(rc))
306 return VMSetError(pVM, rc, RT_SRC_POS,
307 N_("Configuration error: Failed to querying bool value \"UseRealTSC\""));
308 if (!pVM->tm.s.fTSCUseRealTSC)
309 pVM->tm.s.fTSCVirtualized = true;
310
311 /* TSC reliability */
312 /** @cfgm{/TM/MaybeUseOffsettedHostTSC,bool,detect}
313 * Whether the CPU has a fixed TSC rate and may be used in offsetted mode with
314 * VT-x/AMD-V execution. This is autodetected in a very restrictive way by
315 * default. */
316 rc = CFGMR3QueryBool(pCfgHandle, "MaybeUseOffsettedHostTSC", &pVM->tm.s.fMaybeUseOffsettedHostTSC);
317 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
318 {
319 if (!pVM->tm.s.fTSCUseRealTSC)
320 {
321 /* @todo simple case for guest SMP; always emulate RDTSC */
322 if (pVM->cCPUs == 1)
323 pVM->tm.s.fMaybeUseOffsettedHostTSC = tmR3HasFixedTSC(pVM);
324 }
325 else
326 pVM->tm.s.fMaybeUseOffsettedHostTSC = true;
327 }
328
329 /** @cfgm{TM/TSCTicksPerSecond, uint32_t, Current TSC frequency from GIP}
330 * The number of TSC ticks per second (i.e. the TSC frequency). This will
331 * override TSCUseRealTSC, TSCVirtualized and MaybeUseOffsettedHostTSC.
332 */
333 rc = CFGMR3QueryU64(pCfgHandle, "TSCTicksPerSecond", &pVM->tm.s.cTSCTicksPerSecond);
334 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
335 {
336 pVM->tm.s.cTSCTicksPerSecond = tmR3CalibrateTSC(pVM);
337 if ( !pVM->tm.s.fTSCUseRealTSC
338 && pVM->tm.s.cTSCTicksPerSecond >= _4G)
339 {
340 pVM->tm.s.cTSCTicksPerSecond = _4G - 1; /* (A limitation of our math code) */
341 pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
342 }
343 }
344 else if (RT_FAILURE(rc))
345 return VMSetError(pVM, rc, RT_SRC_POS,
346 N_("Configuration error: Failed to querying uint64_t value \"TSCTicksPerSecond\""));
347 else if ( pVM->tm.s.cTSCTicksPerSecond < _1M
348 || pVM->tm.s.cTSCTicksPerSecond >= _4G)
349 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
350 N_("Configuration error: \"TSCTicksPerSecond\" = %RI64 is not in the range 1MHz..4GHz-1"),
351 pVM->tm.s.cTSCTicksPerSecond);
352 else
353 {
354 pVM->tm.s.fTSCUseRealTSC = pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
355 pVM->tm.s.fTSCVirtualized = true;
356 }
357
358 /** @cfgm{TM/TSCTiedToExecution, bool, false}
359 * Whether the TSC should be tied to execution. This will exclude most of the
360 * virtualization overhead, but will by default include the time spent in the
361 * halt state (see TM/TSCNotTiedToHalt). This setting will override all other
362 * TSC settings except for TSCTicksPerSecond and TSCNotTiedToHalt, which should
363 * be used avoided or used with great care. Note that this will only work right
364 * together with VT-x or AMD-V, and with a single virtual CPU. */
365 rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCTiedToExecution", &pVM->tm.s.fTSCTiedToExecution, false);
366 if (RT_FAILURE(rc))
367 return VMSetError(pVM, rc, RT_SRC_POS,
368 N_("Configuration error: Failed to querying bool value \"TSCTiedToExecution\""));
369 if (pVM->tm.s.fTSCTiedToExecution)
370 {
371 /* tied to execution, override all other settings. */
372 pVM->tm.s.fTSCVirtualized = true;
373 pVM->tm.s.fTSCUseRealTSC = true;
374 pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
375 }
376
377 /** @cfgm{TM/TSCNotTiedToHalt, bool, true}
378 * For overriding the default of TM/TSCTiedToExecution, i.e. set this to false
379 * to make the TSC freeze during HLT. */
380 rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCNotTiedToHalt", &pVM->tm.s.fTSCNotTiedToHalt, false);
381 if (RT_FAILURE(rc))
382 return VMSetError(pVM, rc, RT_SRC_POS,
383 N_("Configuration error: Failed to querying bool value \"TSCNotTiedToHalt\""));
384
385 /* setup and report */
386 if (pVM->tm.s.fTSCVirtualized)
387 CPUMR3SetCR4Feature(pVM, X86_CR4_TSD, ~X86_CR4_TSD);
388 else
389 CPUMR3SetCR4Feature(pVM, 0, ~X86_CR4_TSD);
390 LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool\n"
391 "TM: fMaybeUseOffsettedHostTSC=%RTbool TSCTiedToExecution=%RTbool TSCNotTiedToHalt=%RTbool\n",
392 pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC,
393 pVM->tm.s.fMaybeUseOffsettedHostTSC, pVM->tm.s.fTSCTiedToExecution, pVM->tm.s.fTSCNotTiedToHalt));
394
395 /*
396 * Configure the timer synchronous virtual time.
397 */
398 /** @cfgm{TM/ScheduleSlack, uint32_t, ns, 0, UINT32_MAX, 100000}
399 * Scheduling slack when processing timers. */
400 rc = CFGMR3QueryU32(pCfgHandle, "ScheduleSlack", &pVM->tm.s.u32VirtualSyncScheduleSlack);
401 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
402 pVM->tm.s.u32VirtualSyncScheduleSlack = 100000; /* 0.100ms (ASSUMES virtual time is nanoseconds) */
403 else if (RT_FAILURE(rc))
404 return VMSetError(pVM, rc, RT_SRC_POS,
405 N_("Configuration error: Failed to querying 32-bit integer value \"ScheduleSlack\""));
406
407 /** @cfgm{TM/CatchUpStopThreshold, uint64_t, ns, 0, UINT64_MAX, 500000}
408 * When to stop a catch-up, considering it successful. */
409 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStopThreshold", &pVM->tm.s.u64VirtualSyncCatchUpStopThreshold);
410 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
411 pVM->tm.s.u64VirtualSyncCatchUpStopThreshold = 500000; /* 0.5ms */
412 else if (RT_FAILURE(rc))
413 return VMSetError(pVM, rc, RT_SRC_POS,
414 N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpStopThreshold\""));
415
416 /** @cfgm{TM/CatchUpGiveUpThreshold, uint64_t, ns, 0, UINT64_MAX, 60000000000}
417 * When to give up a catch-up attempt. */
418 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpGiveUpThreshold", &pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold);
419 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
420 pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold = UINT64_C(60000000000); /* 60 sec */
421 else if (RT_FAILURE(rc))
422 return VMSetError(pVM, rc, RT_SRC_POS,
423 N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpGiveUpThreshold\""));
424
425
426 /** @cfgm{TM/CatchUpPrecentage[0..9], uint32_t, %, 1, 2000, various}
427 * The catch-up percent for a given period. */
428 /** @cfgm{TM/CatchUpStartThreshold[0..9], uint64_t, ns, 0, UINT64_MAX,
429 * The catch-up period threshold, or if you like, when a period starts. */
430#define TM_CFG_PERIOD(iPeriod, DefStart, DefPct) \
431 do \
432 { \
433 uint64_t u64; \
434 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStartThreshold" #iPeriod, &u64); \
435 if (rc == VERR_CFGM_VALUE_NOT_FOUND) \
436 u64 = UINT64_C(DefStart); \
437 else if (RT_FAILURE(rc)) \
438 return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpThreshold" #iPeriod "\"")); \
439 if ( (iPeriod > 0 && u64 <= pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod - 1].u64Start) \
440 || u64 >= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold) \
441 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Configuration error: Invalid start of period #" #iPeriod ": %RU64"), u64); \
442 pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u64Start = u64; \
443 rc = CFGMR3QueryU32(pCfgHandle, "CatchUpPrecentage" #iPeriod, &pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage); \
444 if (rc == VERR_CFGM_VALUE_NOT_FOUND) \
445 pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage = (DefPct); \
446 else if (RT_FAILURE(rc)) \
447 return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 32-bit integer value \"CatchUpPrecentage" #iPeriod "\"")); \
448 } while (0)
449 /* This needs more tuning. Not sure if we really need so many period and be so gentle. */
450 TM_CFG_PERIOD(0, 750000, 5); /* 0.75ms at 1.05x */
451 TM_CFG_PERIOD(1, 1500000, 10); /* 1.50ms at 1.10x */
452 TM_CFG_PERIOD(2, 8000000, 25); /* 8ms at 1.25x */
453 TM_CFG_PERIOD(3, 30000000, 50); /* 30ms at 1.50x */
454 TM_CFG_PERIOD(4, 75000000, 75); /* 75ms at 1.75x */
455 TM_CFG_PERIOD(5, 175000000, 100); /* 175ms at 2x */
456 TM_CFG_PERIOD(6, 500000000, 200); /* 500ms at 3x */
457 TM_CFG_PERIOD(7, 3000000000, 300); /* 3s at 4x */
458 TM_CFG_PERIOD(8,30000000000, 400); /* 30s at 5x */
459 TM_CFG_PERIOD(9,55000000000, 500); /* 55s at 6x */
460 AssertCompile(RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods) == 10);
461#undef TM_CFG_PERIOD
462
463 /*
464 * Configure real world time (UTC).
465 */
466 /** @cfgm{TM/UTCOffset, int64_t, ns, INT64_MIN, INT64_MAX, 0}
467 * The UTC offset. This is used to put the guest back or forwards in time. */
468 rc = CFGMR3QueryS64(pCfgHandle, "UTCOffset", &pVM->tm.s.offUTC);
469 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
470 pVM->tm.s.offUTC = 0; /* ns */
471 else if (RT_FAILURE(rc))
472 return VMSetError(pVM, rc, RT_SRC_POS,
473 N_("Configuration error: Failed to querying 64-bit integer value \"UTCOffset\""));
474
475 /*
476 * Setup the warp drive.
477 */
478 /** @cfgm{TM/WarpDrivePercentage, uint32_t, %, 0, 20000, 100}
479 * The warp drive percentage, 100% is normal speed. This is used to speed up
480 * or slow down the virtual clock, which can be useful for fast forwarding
481 * borring periods during tests. */
482 rc = CFGMR3QueryU32(pCfgHandle, "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage);
483 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
484 rc = CFGMR3QueryU32(CFGMR3GetRoot(pVM), "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage); /* legacy */
485 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
486 pVM->tm.s.u32VirtualWarpDrivePercentage = 100;
487 else if (RT_FAILURE(rc))
488 return VMSetError(pVM, rc, RT_SRC_POS,
489 N_("Configuration error: Failed to querying uint32_t value \"WarpDrivePercent\""));
490 else if ( pVM->tm.s.u32VirtualWarpDrivePercentage < 2
491 || pVM->tm.s.u32VirtualWarpDrivePercentage > 20000)
492 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
493 N_("Configuration error: \"WarpDrivePercent\" = %RI32 is not in the range 2..20000"),
494 pVM->tm.s.u32VirtualWarpDrivePercentage);
495 pVM->tm.s.fVirtualWarpDrive = pVM->tm.s.u32VirtualWarpDrivePercentage != 100;
496 if (pVM->tm.s.fVirtualWarpDrive)
497 LogRel(("TM: u32VirtualWarpDrivePercentage=%RI32\n", pVM->tm.s.u32VirtualWarpDrivePercentage));
498
499 /*
500 * Start the timer (guard against REM not yielding).
501 */
502 /** @cfgm{TM/TimerMillies, uint32_t, ms, 1, 1000, 10}
503 * The watchdog timer interval. */
504 uint32_t u32Millies;
505 rc = CFGMR3QueryU32(pCfgHandle, "TimerMillies", &u32Millies);
506 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
507 u32Millies = 10;
508 else if (RT_FAILURE(rc))
509 return VMSetError(pVM, rc, RT_SRC_POS,
510 N_("Configuration error: Failed to query uint32_t value \"TimerMillies\""));
511 rc = RTTimerCreate(&pVM->tm.s.pTimer, u32Millies, tmR3TimerCallback, pVM);
512 if (RT_FAILURE(rc))
513 {
514 AssertMsgFailed(("Failed to create timer, u32Millies=%d rc=%Rrc.\n", u32Millies, rc));
515 return rc;
516 }
517 Log(("TM: Created timer %p firing every %d millieseconds\n", pVM->tm.s.pTimer, u32Millies));
518 pVM->tm.s.u32TimerMillies = u32Millies;
519
520 /*
521 * Register saved state.
522 */
523 rc = SSMR3RegisterInternal(pVM, "tm", 1, TM_SAVED_STATE_VERSION, sizeof(uint64_t) * 8,
524 NULL, tmR3Save, NULL,
525 NULL, tmR3Load, NULL);
526 if (RT_FAILURE(rc))
527 return rc;
528
529 /*
530 * Register statistics.
531 */
532 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR3.c1nsSteps,STAMTYPE_U32, "/TM/R3/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
533 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR3.cBadPrev, STAMTYPE_U32, "/TM/R3/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
534 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR0.c1nsSteps,STAMTYPE_U32, "/TM/R0/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
535 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR0.cBadPrev, STAMTYPE_U32, "/TM/R0/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
536 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataRC.c1nsSteps,STAMTYPE_U32, "/TM/GC/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
537 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataRC.cBadPrev, STAMTYPE_U32, "/TM/GC/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
538 STAM_REL_REG( pVM,(void*)&pVM->tm.s.offVirtualSync, STAMTYPE_U64, "/TM/VirtualSync/CurrentOffset", STAMUNIT_NS, "The current offset. (subtract GivenUp to get the lag)");
539 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.offVirtualSyncGivenUp, STAMTYPE_U64, "/TM/VirtualSync/GivenUp", STAMUNIT_NS, "Nanoseconds of the 'CurrentOffset' that's been given up and won't ever be attemted caught up with.");
540
541#ifdef VBOX_WITH_STATISTICS
542 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR3.cExpired, STAMTYPE_U32, "/TM/R3/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
543 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR3.cUpdateRaces,STAMTYPE_U32, "/TM/R3/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
544 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR0.cExpired, STAMTYPE_U32, "/TM/R0/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
545 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR0.cUpdateRaces,STAMTYPE_U32, "/TM/R0/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
546 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataRC.cExpired, STAMTYPE_U32, "/TM/GC/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
547 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataRC.cUpdateRaces,STAMTYPE_U32, "/TM/GC/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
548 STAM_REG(pVM, &pVM->tm.s.StatDoQueues, STAMTYPE_PROFILE, "/TM/DoQueues", STAMUNIT_TICKS_PER_CALL, "Profiling timer TMR3TimerQueuesDo.");
549 STAM_REG(pVM, &pVM->tm.s.StatDoQueuesSchedule, STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Schedule", STAMUNIT_TICKS_PER_CALL, "The scheduling part.");
550 STAM_REG(pVM, &pVM->tm.s.StatDoQueuesRun, STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Run", STAMUNIT_TICKS_PER_CALL, "The run part.");
551
552 STAM_REG(pVM, &pVM->tm.s.StatPollAlreadySet, STAMTYPE_COUNTER, "/TM/PollAlreadySet", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the FF was already set.");
553 STAM_REG(pVM, &pVM->tm.s.StatPollVirtual, STAMTYPE_COUNTER, "/TM/PollHitsVirtual", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL queue.");
554 STAM_REG(pVM, &pVM->tm.s.StatPollVirtualSync, STAMTYPE_COUNTER, "/TM/PollHitsVirtualSync", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL_SYNC queue.");
555 STAM_REG(pVM, &pVM->tm.s.StatPollMiss, STAMTYPE_COUNTER, "/TM/PollMiss", STAMUNIT_OCCURENCES, "TMTimerPoll calls where nothing had expired.");
556
557 STAM_REG(pVM, &pVM->tm.s.StatPostponedR3, STAMTYPE_COUNTER, "/TM/PostponedR3", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-3.");
558 STAM_REG(pVM, &pVM->tm.s.StatPostponedRZ, STAMTYPE_COUNTER, "/TM/PostponedRZ", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-0 / RC.");
559
560 STAM_REG(pVM, &pVM->tm.s.StatScheduleOneR3, STAMTYPE_PROFILE, "/TM/ScheduleOneR3", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.");
561 STAM_REG(pVM, &pVM->tm.s.StatScheduleOneRZ, STAMTYPE_PROFILE, "/TM/ScheduleOneRZ", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.");
562 STAM_REG(pVM, &pVM->tm.s.StatScheduleSetFF, STAMTYPE_COUNTER, "/TM/ScheduleSetFF", STAMUNIT_OCCURENCES, "The number of times the timer FF was set instead of doing scheduling.");
563
564 STAM_REG(pVM, &pVM->tm.s.StatTimerSetR3, STAMTYPE_PROFILE, "/TM/TimerSetR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-3.");
565 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRZ, STAMTYPE_PROFILE, "/TM/TimerSetRZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-0 / RC.");
566
567 STAM_REG(pVM, &pVM->tm.s.StatTimerStopR3, STAMTYPE_PROFILE, "/TM/TimerStopR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-3.");
568 STAM_REG(pVM, &pVM->tm.s.StatTimerStopRZ, STAMTYPE_PROFILE, "/TM/TimerStopRZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-0 / RC.");
569
570 STAM_REG(pVM, &pVM->tm.s.StatVirtualGet, STAMTYPE_COUNTER, "/TM/VirtualGet", STAMUNIT_OCCURENCES, "The number of times TMTimerGet was called when the clock was running.");
571 STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSetFF, STAMTYPE_COUNTER, "/TM/VirtualGetSetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling TMTimerGet.");
572 STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSync, STAMTYPE_COUNTER, "/TM/VirtualGetSync", STAMUNIT_OCCURENCES, "The number of times TMTimerGetSync was called when the clock was running.");
573 STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSyncSetFF, STAMTYPE_COUNTER, "/TM/VirtualGetSyncSetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling TMTimerGetSync.");
574 STAM_REG(pVM, &pVM->tm.s.StatVirtualPause, STAMTYPE_COUNTER, "/TM/VirtualPause", STAMUNIT_OCCURENCES, "The number of times TMR3TimerPause was called.");
575 STAM_REG(pVM, &pVM->tm.s.StatVirtualResume, STAMTYPE_COUNTER, "/TM/VirtualResume", STAMUNIT_OCCURENCES, "The number of times TMR3TimerResume was called.");
576
577 STAM_REG(pVM, &pVM->tm.s.StatTimerCallbackSetFF, STAMTYPE_COUNTER, "/TM/CallbackSetFF", STAMUNIT_OCCURENCES, "The number of times the timer callback set FF.");
578
579 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE010, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE010", STAMUNIT_OCCURENCES, "In catch-up mode, 10% or lower.");
580 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE025, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE025", STAMUNIT_OCCURENCES, "In catch-up mode, 25%-11%.");
581 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE100, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE100", STAMUNIT_OCCURENCES, "In catch-up mode, 100%-26%.");
582 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupOther, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupOther", STAMUNIT_OCCURENCES, "In catch-up mode, > 100%.");
583 STAM_REG(pVM, &pVM->tm.s.StatTSCNotFixed, STAMTYPE_COUNTER, "/TM/TSC/Intercept/NotFixed", STAMUNIT_OCCURENCES, "TSC is not fixed, it may run at variable speed.");
584 STAM_REG(pVM, &pVM->tm.s.StatTSCNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/NotTicking", STAMUNIT_OCCURENCES, "TSC is not ticking.");
585 STAM_REG(pVM, &pVM->tm.s.StatTSCSyncNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/SyncNotTicking", STAMUNIT_OCCURENCES, "VirtualSync isn't ticking.");
586 STAM_REG(pVM, &pVM->tm.s.StatTSCWarp, STAMTYPE_COUNTER, "/TM/TSC/Intercept/Warp", STAMUNIT_OCCURENCES, "Warpdrive is active.");
587
588
589 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncCatchup, STAMTYPE_PROFILE_ADV, "/TM/VirtualSync/CatchUp", STAMUNIT_TICKS_PER_OCCURENCE, "Counting and measuring the times spent catching up.");
590 STAM_REG(pVM, (void *)&pVM->tm.s.fVirtualSyncCatchUp, STAMTYPE_U8, "/TM/VirtualSync/CatchUpActive", STAMUNIT_NONE, "Catch-Up active indicator.");
591 STAM_REG(pVM, (void *)&pVM->tm.s.u32VirtualSyncCatchUpPercentage, STAMTYPE_U32, "/TM/VirtualSync/CatchUpPercentage", STAMUNIT_PCT, "The catch-up percentage. (+100/100 to get clock multiplier)");
592 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGiveUp, STAMTYPE_COUNTER, "/TM/VirtualSync/GiveUp", STAMUNIT_OCCURENCES, "Times the catch-up was abandoned.");
593 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGiveUpBeforeStarting, STAMTYPE_COUNTER, "/TM/VirtualSync/GiveUpBeforeStarting",STAMUNIT_OCCURENCES, "Times the catch-up was abandoned before even starting. (Typically debugging++.)");
594 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRun, STAMTYPE_COUNTER, "/TM/VirtualSync/Run", STAMUNIT_OCCURENCES, "Times the virtual sync timer queue was considered.");
595 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunRestart, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/Restarts", STAMUNIT_OCCURENCES, "Times the clock was restarted after a run.");
596 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunStop, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/Stop", STAMUNIT_OCCURENCES, "Times the clock was stopped when calculating the current time before examining the timers.");
597 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunStoppedAlready, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/StoppedAlready", STAMUNIT_OCCURENCES, "Times the clock was already stopped elsewhere (TMVirtualSyncGet).");
598 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunSlack, STAMTYPE_PROFILE, "/TM/VirtualSync/Run/Slack", STAMUNIT_NS_PER_OCCURENCE, "The scheduling slack. (Catch-up handed out when running timers.)");
599 for (unsigned i = 0; i < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods); i++)
600 {
601 STAMR3RegisterF(pVM, &pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "The catch-up percentage.", "/TM/VirtualSync/Periods/%u", i);
602 STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupAdjust[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times adjusted to this period.", "/TM/VirtualSync/Periods/%u/Adjust", i);
603 STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupInitial[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times started in this period.", "/TM/VirtualSync/Periods/%u/Initial", i);
604 STAMR3RegisterF(pVM, &pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u64Start, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Start of this period (lag).", "/TM/VirtualSync/Periods/%u/Start", i);
605 }
606
607#endif /* VBOX_WITH_STATISTICS */
608
609 /*
610 * Register info handlers.
611 */
612 DBGFR3InfoRegisterInternalEx(pVM, "timers", "Dumps all timers. No arguments.", tmR3TimerInfo, DBGFINFO_FLAGS_RUN_ON_EMT);
613 DBGFR3InfoRegisterInternalEx(pVM, "activetimers", "Dumps active all timers. No arguments.", tmR3TimerInfoActive, DBGFINFO_FLAGS_RUN_ON_EMT);
614 DBGFR3InfoRegisterInternalEx(pVM, "clocks", "Display the time of the various clocks.", tmR3InfoClocks, DBGFINFO_FLAGS_RUN_ON_EMT);
615
616 return VINF_SUCCESS;
617}
618
619
620/**
621 * Initializes the per-VCPU TM.
622 *
623 * @returns VBox status code.
624 * @param pVM The VM to operate on.
625 */
626VMMR3DECL(int) TMR3InitCPU(PVM pVM)
627{
628 LogFlow(("TMR3InitCPU\n"));
629 return VINF_SUCCESS;
630}
631
632
633/**
634 * Checks if the host CPU has a fixed TSC frequency.
635 *
636 * @returns true if it has, false if it hasn't.
637 *
638 * @remark This test doesn't bother with very old CPUs that don't do power
639 * management or any other stuff that might influence the TSC rate.
640 * This isn't currently relevant.
641 */
642static bool tmR3HasFixedTSC(PVM pVM)
643{
644 if (ASMHasCpuId())
645 {
646 uint32_t uEAX, uEBX, uECX, uEDX;
647
648 if (CPUMGetCPUVendor(pVM) == CPUMCPUVENDOR_AMD)
649 {
650 /*
651 * AuthenticAMD - Check for APM support and that TscInvariant is set.
652 *
653 * This test isn't correct with respect to fixed/non-fixed TSC and
654 * older models, but this isn't relevant since the result is currently
655 * only used for making a descision on AMD-V models.
656 */
657 ASMCpuId(0x80000000, &uEAX, &uEBX, &uECX, &uEDX);
658 if (uEAX >= 0x80000007)
659 {
660 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
661
662 ASMCpuId(0x80000007, &uEAX, &uEBX, &uECX, &uEDX);
663 if ( (uEDX & X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR) /* TscInvariant */
664 && pGip->u32Mode == SUPGIPMODE_SYNC_TSC /* no fixed tsc if the gip timer is in async mode */)
665 return true;
666 }
667 }
668 else if (CPUMGetCPUVendor(pVM) == CPUMCPUVENDOR_INTEL)
669 {
670 /*
671 * GenuineIntel - Check the model number.
672 *
673 * This test is lacking in the same way and for the same reasons
674 * as the AMD test above.
675 */
676 ASMCpuId(1, &uEAX, &uEBX, &uECX, &uEDX);
677 unsigned uModel = (uEAX >> 4) & 0x0f;
678 unsigned uFamily = (uEAX >> 8) & 0x0f;
679 if (uFamily == 0x0f)
680 uFamily += (uEAX >> 20) & 0xff;
681 if (uFamily >= 0x06)
682 uModel += ((uEAX >> 16) & 0x0f) << 4;
683 if ( (uFamily == 0x0f /*P4*/ && uModel >= 0x03)
684 || (uFamily == 0x06 /*P2/P3*/ && uModel >= 0x0e))
685 return true;
686 }
687 }
688 return false;
689}
690
691
692/**
693 * Calibrate the CPU tick.
694 *
695 * @returns Number of ticks per second.
696 */
697static uint64_t tmR3CalibrateTSC(PVM pVM)
698{
699 /*
700 * Use GIP when available present.
701 */
702 uint64_t u64Hz;
703 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
704 if ( pGip
705 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC)
706 {
707 unsigned iCpu = pGip->u32Mode != SUPGIPMODE_ASYNC_TSC ? 0 : ASMGetApicId();
708 if (iCpu >= RT_ELEMENTS(pGip->aCPUs))
709 AssertReleaseMsgFailed(("iCpu=%d - the ApicId is too high. send VBox.log and hardware specs!\n", iCpu));
710 else
711 {
712 if (tmR3HasFixedTSC(pVM))
713 /* Sleep a bit to get a more reliable CpuHz value. */
714 RTThreadSleep(32);
715 else
716 {
717 /* Spin for 40ms to try push up the CPU frequency and get a more reliable CpuHz value. */
718 const uint64_t u64 = RTTimeMilliTS();
719 while ((RTTimeMilliTS() - u64) < 40 /*ms*/)
720 /* nothing */;
721 }
722
723 pGip = g_pSUPGlobalInfoPage;
724 if ( pGip
725 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
726 && (u64Hz = pGip->aCPUs[iCpu].u64CpuHz)
727 && u64Hz != ~(uint64_t)0)
728 return u64Hz;
729 }
730 }
731
732 /* call this once first to make sure it's initialized. */
733 RTTimeNanoTS();
734
735 /*
736 * Yield the CPU to increase our chances of getting
737 * a correct value.
738 */
739 RTThreadYield(); /* Try avoid interruptions between TSC and NanoTS samplings. */
740 static const unsigned s_auSleep[5] = { 50, 30, 30, 40, 40 };
741 uint64_t au64Samples[5];
742 unsigned i;
743 for (i = 0; i < RT_ELEMENTS(au64Samples); i++)
744 {
745 unsigned cMillies;
746 int cTries = 5;
747 uint64_t u64Start = ASMReadTSC();
748 uint64_t u64End;
749 uint64_t StartTS = RTTimeNanoTS();
750 uint64_t EndTS;
751 do
752 {
753 RTThreadSleep(s_auSleep[i]);
754 u64End = ASMReadTSC();
755 EndTS = RTTimeNanoTS();
756 cMillies = (unsigned)((EndTS - StartTS + 500000) / 1000000);
757 } while ( cMillies == 0 /* the sleep may be interrupted... */
758 || (cMillies < 20 && --cTries > 0));
759 uint64_t u64Diff = u64End - u64Start;
760
761 au64Samples[i] = (u64Diff * 1000) / cMillies;
762 AssertMsg(cTries > 0, ("cMillies=%d i=%d\n", cMillies, i));
763 }
764
765 /*
766 * Discard the highest and lowest results and calculate the average.
767 */
768 unsigned iHigh = 0;
769 unsigned iLow = 0;
770 for (i = 1; i < RT_ELEMENTS(au64Samples); i++)
771 {
772 if (au64Samples[i] < au64Samples[iLow])
773 iLow = i;
774 if (au64Samples[i] > au64Samples[iHigh])
775 iHigh = i;
776 }
777 au64Samples[iLow] = 0;
778 au64Samples[iHigh] = 0;
779
780 u64Hz = au64Samples[0];
781 for (i = 1; i < RT_ELEMENTS(au64Samples); i++)
782 u64Hz += au64Samples[i];
783 u64Hz /= RT_ELEMENTS(au64Samples) - 2;
784
785 return u64Hz;
786}
787
788
789/**
790 * Finalizes the TM initialization.
791 *
792 * @returns VBox status code.
793 * @param pVM The VM to operate on.
794 */
795VMMR3DECL(int) TMR3InitFinalize(PVM pVM)
796{
797 int rc;
798
799 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataRC.pfnBad);
800 AssertRCReturn(rc, rc);
801 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataRC.pfnRediscover);
802 AssertRCReturn(rc, rc);
803 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
804 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawRC);
805 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
806 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawRC);
807 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
808 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawRC);
809 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
810 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawRC);
811 else
812 AssertFatalFailed();
813 AssertRCReturn(rc, rc);
814
815 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataR0.pfnBad);
816 AssertRCReturn(rc, rc);
817 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataR0.pfnRediscover);
818 AssertRCReturn(rc, rc);
819 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
820 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawR0);
821 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
822 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawR0);
823 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
824 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawR0);
825 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
826 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawR0);
827 else
828 AssertFatalFailed();
829 AssertRCReturn(rc, rc);
830
831 return VINF_SUCCESS;
832}
833
834
835/**
836 * Applies relocations to data and code managed by this
837 * component. This function will be called at init and
838 * whenever the VMM need to relocate it self inside the GC.
839 *
840 * @param pVM The VM.
841 * @param offDelta Relocation delta relative to old location.
842 */
843VMMR3DECL(void) TMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
844{
845 int rc;
846 LogFlow(("TMR3Relocate\n"));
847
848 pVM->tm.s.pvGIPRC = MMHyperR3ToRC(pVM, pVM->tm.s.pvGIPR3);
849 pVM->tm.s.paTimerQueuesRC = MMHyperR3ToRC(pVM, pVM->tm.s.paTimerQueuesR3);
850 pVM->tm.s.paTimerQueuesR0 = MMHyperR3ToR0(pVM, pVM->tm.s.paTimerQueuesR3);
851
852 pVM->tm.s.VirtualGetRawDataRC.pu64Prev = MMHyperR3ToRC(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
853 AssertFatal(pVM->tm.s.VirtualGetRawDataRC.pu64Prev);
854 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataRC.pfnBad);
855 AssertFatalRC(rc);
856 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataRC.pfnRediscover);
857 AssertFatalRC(rc);
858
859 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
860 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawRC);
861 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
862 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawRC);
863 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
864 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawRC);
865 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
866 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawRC);
867 else
868 AssertFatalFailed();
869 AssertFatalRC(rc);
870
871 /*
872 * Iterate the timers updating the pVMRC pointers.
873 */
874 for (PTMTIMER pTimer = pVM->tm.s.pCreated; pTimer; pTimer = pTimer->pBigNext)
875 {
876 pTimer->pVMRC = pVM->pVMRC;
877 pTimer->pVMR0 = pVM->pVMR0;
878 }
879}
880
881
882/**
883 * Terminates the TM.
884 *
885 * Termination means cleaning up and freeing all resources,
886 * the VM it self is at this point powered off or suspended.
887 *
888 * @returns VBox status code.
889 * @param pVM The VM to operate on.
890 */
891VMMR3DECL(int) TMR3Term(PVM pVM)
892{
893 AssertMsg(pVM->tm.s.offVM, ("bad init order!\n"));
894 if (pVM->tm.s.pTimer)
895 {
896 int rc = RTTimerDestroy(pVM->tm.s.pTimer);
897 AssertRC(rc);
898 pVM->tm.s.pTimer = NULL;
899 }
900
901 return VINF_SUCCESS;
902}
903
904
905/**
906 * Terminates the per-VCPU TM.
907 *
908 * Termination means cleaning up and freeing all resources,
909 * the VM it self is at this point powered off or suspended.
910 *
911 * @returns VBox status code.
912 * @param pVM The VM to operate on.
913 */
914VMMR3DECL(int) TMR3TermCPU(PVM pVM)
915{
916 return 0;
917}
918
919
920/**
921 * The VM is being reset.
922 *
923 * For the TM component this means that a rescheduling is preformed,
924 * the FF is cleared and but without running the queues. We'll have to
925 * check if this makes sense or not, but it seems like a good idea now....
926 *
927 * @param pVM VM handle.
928 */
929VMMR3DECL(void) TMR3Reset(PVM pVM)
930{
931 LogFlow(("TMR3Reset:\n"));
932 VM_ASSERT_EMT(pVM);
933 tmLock(pVM);
934
935 /*
936 * Abort any pending catch up.
937 * This isn't perfect,
938 */
939 if (pVM->tm.s.fVirtualSyncCatchUp)
940 {
941 const uint64_t offVirtualNow = TMVirtualGetEx(pVM, false /* don't check timers */);
942 const uint64_t offVirtualSyncNow = TMVirtualSyncGetEx(pVM, false /* don't check timers */);
943 if (pVM->tm.s.fVirtualSyncCatchUp)
944 {
945 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
946
947 const uint64_t offOld = pVM->tm.s.offVirtualSyncGivenUp;
948 const uint64_t offNew = offVirtualNow - offVirtualSyncNow;
949 Assert(offOld <= offNew);
950 ASMAtomicXchgU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
951 ASMAtomicXchgU64((uint64_t volatile *)&pVM->tm.s.offVirtualSync, offNew);
952 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
953 LogRel(("TM: Aborting catch-up attempt on reset with a %RU64 ns lag on reset; new total: %RU64 ns\n", offNew - offOld, offNew));
954 }
955 }
956
957 /*
958 * Process the queues.
959 */
960 for (int i = 0; i < TMCLOCK_MAX; i++)
961 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[i]);
962#ifdef VBOX_STRICT
963 tmTimerQueuesSanityChecks(pVM, "TMR3Reset");
964#endif
965
966 VM_FF_CLEAR(pVM, VM_FF_TIMER);
967 tmUnlock(pVM);
968}
969
970
971/**
972 * Resolve a builtin RC symbol.
973 * Called by PDM when loading or relocating GC modules.
974 *
975 * @returns VBox status
976 * @param pVM VM Handle.
977 * @param pszSymbol Symbol to resolve.
978 * @param pRCPtrValue Where to store the symbol value.
979 * @remark This has to work before TMR3Relocate() is called.
980 */
981VMMR3DECL(int) TMR3GetImportRC(PVM pVM, const char *pszSymbol, PRTRCPTR pRCPtrValue)
982{
983 if (!strcmp(pszSymbol, "g_pSUPGlobalInfoPage"))
984 *pRCPtrValue = MMHyperR3ToRC(pVM, &pVM->tm.s.pvGIPRC);
985 //else if (..)
986 else
987 return VERR_SYMBOL_NOT_FOUND;
988 return VINF_SUCCESS;
989}
990
991
992/**
993 * Execute state save operation.
994 *
995 * @returns VBox status code.
996 * @param pVM VM Handle.
997 * @param pSSM SSM operation handle.
998 */
999static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM)
1000{
1001 LogFlow(("tmR3Save:\n"));
1002#ifdef VBOX_STRICT
1003 for (VMCPUID i = 0; i < pVM->cCPUs; i++)
1004 {
1005 PVMCPU pVCpu = &pVM->aCpus[i];
1006 Assert(!pVCpu->tm.s.fTSCTicking);
1007 }
1008 Assert(!pVM->tm.s.cVirtualTicking);
1009 Assert(!pVM->tm.s.fVirtualSyncTicking);
1010#endif
1011
1012 /*
1013 * Save the virtual clocks.
1014 */
1015 /* the virtual clock. */
1016 SSMR3PutU64(pSSM, TMCLOCK_FREQ_VIRTUAL);
1017 SSMR3PutU64(pSSM, pVM->tm.s.u64Virtual);
1018
1019 /* the virtual timer synchronous clock. */
1020 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSync);
1021 SSMR3PutU64(pSSM, pVM->tm.s.offVirtualSync);
1022 SSMR3PutU64(pSSM, pVM->tm.s.offVirtualSyncGivenUp);
1023 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSyncCatchUpPrev);
1024 SSMR3PutBool(pSSM, pVM->tm.s.fVirtualSyncCatchUp);
1025
1026 /* real time clock */
1027 SSMR3PutU64(pSSM, TMCLOCK_FREQ_REAL);
1028
1029 for (VMCPUID i = 0; i < pVM->cCPUs; i++)
1030 {
1031 PVMCPU pVCpu = &pVM->aCpus[i];
1032
1033 /* the cpu tick clock. */
1034 SSMR3PutU64(pSSM, TMCpuTickGet(pVCpu));
1035 }
1036 return SSMR3PutU64(pSSM, pVM->tm.s.cTSCTicksPerSecond);
1037}
1038
1039
1040/**
1041 * Execute state load operation.
1042 *
1043 * @returns VBox status code.
1044 * @param pVM VM Handle.
1045 * @param pSSM SSM operation handle.
1046 * @param u32Version Data layout version.
1047 */
1048static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
1049{
1050 LogFlow(("tmR3Load:\n"));
1051
1052#ifdef VBOX_STRICT
1053 for (VMCPUID i = 0; i < pVM->cCPUs; i++)
1054 {
1055 PVMCPU pVCpu = &pVM->aCpus[i];
1056 Assert(!pVCpu->tm.s.fTSCTicking);
1057 }
1058 Assert(!pVM->tm.s.cVirtualTicking);
1059 Assert(!pVM->tm.s.fVirtualSyncTicking);
1060#endif
1061
1062 /*
1063 * Validate version.
1064 */
1065 if (u32Version != TM_SAVED_STATE_VERSION)
1066 {
1067 AssertMsgFailed(("tmR3Load: Invalid version u32Version=%d!\n", u32Version));
1068 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1069 }
1070
1071 /*
1072 * Load the virtual clock.
1073 */
1074 pVM->tm.s.cVirtualTicking = 0;
1075 /* the virtual clock. */
1076 uint64_t u64Hz;
1077 int rc = SSMR3GetU64(pSSM, &u64Hz);
1078 if (RT_FAILURE(rc))
1079 return rc;
1080 if (u64Hz != TMCLOCK_FREQ_VIRTUAL)
1081 {
1082 AssertMsgFailed(("The virtual clock frequency differs! Saved: %RU64 Binary: %RU64\n",
1083 u64Hz, TMCLOCK_FREQ_VIRTUAL));
1084 return VERR_SSM_VIRTUAL_CLOCK_HZ;
1085 }
1086 SSMR3GetU64(pSSM, &pVM->tm.s.u64Virtual);
1087 pVM->tm.s.u64VirtualOffset = 0;
1088
1089 /* the virtual timer synchronous clock. */
1090 pVM->tm.s.fVirtualSyncTicking = false;
1091 uint64_t u64;
1092 SSMR3GetU64(pSSM, &u64);
1093 pVM->tm.s.u64VirtualSync = u64;
1094 SSMR3GetU64(pSSM, &u64);
1095 pVM->tm.s.offVirtualSync = u64;
1096 SSMR3GetU64(pSSM, &u64);
1097 pVM->tm.s.offVirtualSyncGivenUp = u64;
1098 SSMR3GetU64(pSSM, &u64);
1099 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64;
1100 bool f;
1101 SSMR3GetBool(pSSM, &f);
1102 pVM->tm.s.fVirtualSyncCatchUp = f;
1103
1104 /* the real clock */
1105 rc = SSMR3GetU64(pSSM, &u64Hz);
1106 if (RT_FAILURE(rc))
1107 return rc;
1108 if (u64Hz != TMCLOCK_FREQ_REAL)
1109 {
1110 AssertMsgFailed(("The real clock frequency differs! Saved: %RU64 Binary: %RU64\n",
1111 u64Hz, TMCLOCK_FREQ_REAL));
1112 return VERR_SSM_VIRTUAL_CLOCK_HZ; /* missleading... */
1113 }
1114
1115 /* the cpu tick clock. */
1116 for (VMCPUID i = 0; i < pVM->cCPUs; i++)
1117 {
1118 PVMCPU pVCpu = &pVM->aCpus[i];
1119
1120 pVCpu->tm.s.fTSCTicking = false;
1121 SSMR3GetU64(pSSM, &pVCpu->tm.s.u64TSC);
1122
1123 if (pVM->tm.s.fTSCUseRealTSC)
1124 pVCpu->tm.s.u64TSCOffset = 0; /** @todo TSC restore stuff and HWACC. */
1125 }
1126
1127 rc = SSMR3GetU64(pSSM, &u64Hz);
1128 if (RT_FAILURE(rc))
1129 return rc;
1130 if (!pVM->tm.s.fTSCUseRealTSC)
1131 pVM->tm.s.cTSCTicksPerSecond = u64Hz;
1132
1133 LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool (state load)\n",
1134 pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC));
1135
1136 /*
1137 * Make sure timers get rescheduled immediately.
1138 */
1139 VM_FF_SET(pVM, VM_FF_TIMER);
1140
1141 return VINF_SUCCESS;
1142}
1143
1144
1145/**
1146 * Internal TMR3TimerCreate worker.
1147 *
1148 * @returns VBox status code.
1149 * @param pVM The VM handle.
1150 * @param enmClock The timer clock.
1151 * @param pszDesc The timer description.
1152 * @param ppTimer Where to store the timer pointer on success.
1153 */
1154static int tmr3TimerCreate(PVM pVM, TMCLOCK enmClock, const char *pszDesc, PPTMTIMERR3 ppTimer)
1155{
1156 VM_ASSERT_EMT(pVM);
1157
1158 /*
1159 * Allocate the timer.
1160 */
1161 PTMTIMERR3 pTimer = NULL;
1162 if (pVM->tm.s.pFree && VM_IS_EMT(pVM))
1163 {
1164 pTimer = pVM->tm.s.pFree;
1165 pVM->tm.s.pFree = pTimer->pBigNext;
1166 Log3(("TM: Recycling timer %p, new free head %p.\n", pTimer, pTimer->pBigNext));
1167 }
1168
1169 if (!pTimer)
1170 {
1171 int rc = MMHyperAlloc(pVM, sizeof(*pTimer), 0, MM_TAG_TM, (void **)&pTimer);
1172 if (RT_FAILURE(rc))
1173 return rc;
1174 Log3(("TM: Allocated new timer %p\n", pTimer));
1175 }
1176
1177 /*
1178 * Initialize it.
1179 */
1180 pTimer->u64Expire = 0;
1181 pTimer->enmClock = enmClock;
1182 pTimer->pVMR3 = pVM;
1183 pTimer->pVMR0 = pVM->pVMR0;
1184 pTimer->pVMRC = pVM->pVMRC;
1185 pTimer->enmState = TMTIMERSTATE_STOPPED;
1186 pTimer->offScheduleNext = 0;
1187 pTimer->offNext = 0;
1188 pTimer->offPrev = 0;
1189 pTimer->pszDesc = pszDesc;
1190
1191 /* insert into the list of created timers. */
1192 tmLock(pVM);
1193 pTimer->pBigPrev = NULL;
1194 pTimer->pBigNext = pVM->tm.s.pCreated;
1195 pVM->tm.s.pCreated = pTimer;
1196 if (pTimer->pBigNext)
1197 pTimer->pBigNext->pBigPrev = pTimer;
1198#ifdef VBOX_STRICT
1199 tmTimerQueuesSanityChecks(pVM, "tmR3TimerCreate");
1200#endif
1201 tmUnlock(pVM);
1202
1203 *ppTimer = pTimer;
1204 return VINF_SUCCESS;
1205}
1206
1207
1208/**
1209 * Creates a device timer.
1210 *
1211 * @returns VBox status.
1212 * @param pVM The VM to create the timer in.
1213 * @param pDevIns Device instance.
1214 * @param enmClock The clock to use on this timer.
1215 * @param pfnCallback Callback function.
1216 * @param pszDesc Pointer to description string which must stay around
1217 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1218 * @param ppTimer Where to store the timer on success.
1219 */
1220VMMR3DECL(int) TMR3TimerCreateDevice(PVM pVM, PPDMDEVINS pDevIns, TMCLOCK enmClock, PFNTMTIMERDEV pfnCallback, const char *pszDesc, PPTMTIMERR3 ppTimer)
1221{
1222 /*
1223 * Allocate and init stuff.
1224 */
1225 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
1226 if (RT_SUCCESS(rc))
1227 {
1228 (*ppTimer)->enmType = TMTIMERTYPE_DEV;
1229 (*ppTimer)->u.Dev.pfnTimer = pfnCallback;
1230 (*ppTimer)->u.Dev.pDevIns = pDevIns;
1231 Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
1232 }
1233
1234 return rc;
1235}
1236
1237
1238/**
1239 * Creates a driver timer.
1240 *
1241 * @returns VBox status.
1242 * @param pVM The VM to create the timer in.
1243 * @param pDrvIns Driver instance.
1244 * @param enmClock The clock to use on this timer.
1245 * @param pfnCallback Callback function.
1246 * @param pszDesc Pointer to description string which must stay around
1247 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1248 * @param ppTimer Where to store the timer on success.
1249 */
1250VMMR3DECL(int) TMR3TimerCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, const char *pszDesc, PPTMTIMERR3 ppTimer)
1251{
1252 /*
1253 * Allocate and init stuff.
1254 */
1255 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
1256 if (RT_SUCCESS(rc))
1257 {
1258 (*ppTimer)->enmType = TMTIMERTYPE_DRV;
1259 (*ppTimer)->u.Drv.pfnTimer = pfnCallback;
1260 (*ppTimer)->u.Drv.pDrvIns = pDrvIns;
1261 Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
1262 }
1263
1264 return rc;
1265}
1266
1267
1268/**
1269 * Creates an internal timer.
1270 *
1271 * @returns VBox status.
1272 * @param pVM The VM to create the timer in.
1273 * @param enmClock The clock to use on this timer.
1274 * @param pfnCallback Callback function.
1275 * @param pvUser User argument to be passed to the callback.
1276 * @param pszDesc Pointer to description string which must stay around
1277 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1278 * @param ppTimer Where to store the timer on success.
1279 */
1280VMMR3DECL(int) TMR3TimerCreateInternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMERINT pfnCallback, void *pvUser, const char *pszDesc, PPTMTIMERR3 ppTimer)
1281{
1282 /*
1283 * Allocate and init stuff.
1284 */
1285 PTMTIMER pTimer;
1286 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, &pTimer);
1287 if (RT_SUCCESS(rc))
1288 {
1289 pTimer->enmType = TMTIMERTYPE_INTERNAL;
1290 pTimer->u.Internal.pfnTimer = pfnCallback;
1291 pTimer->u.Internal.pvUser = pvUser;
1292 *ppTimer = pTimer;
1293 Log(("TM: Created internal timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
1294 }
1295
1296 return rc;
1297}
1298
1299/**
1300 * Creates an external timer.
1301 *
1302 * @returns Timer handle on success.
1303 * @returns NULL on failure.
1304 * @param pVM The VM to create the timer in.
1305 * @param enmClock The clock to use on this timer.
1306 * @param pfnCallback Callback function.
1307 * @param pvUser User argument.
1308 * @param pszDesc Pointer to description string which must stay around
1309 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1310 */
1311VMMR3DECL(PTMTIMERR3) TMR3TimerCreateExternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMEREXT pfnCallback, void *pvUser, const char *pszDesc)
1312{
1313 /*
1314 * Allocate and init stuff.
1315 */
1316 PTMTIMERR3 pTimer;
1317 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, &pTimer);
1318 if (RT_SUCCESS(rc))
1319 {
1320 pTimer->enmType = TMTIMERTYPE_EXTERNAL;
1321 pTimer->u.External.pfnTimer = pfnCallback;
1322 pTimer->u.External.pvUser = pvUser;
1323 Log(("TM: Created external timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
1324 return pTimer;
1325 }
1326
1327 return NULL;
1328}
1329
1330
1331/**
1332 * Destroy all timers owned by a device.
1333 *
1334 * @returns VBox status.
1335 * @param pVM VM handle.
1336 * @param pDevIns Device which timers should be destroyed.
1337 */
1338VMMR3DECL(int) TMR3TimerDestroyDevice(PVM pVM, PPDMDEVINS pDevIns)
1339{
1340 LogFlow(("TMR3TimerDestroyDevice: pDevIns=%p\n", pDevIns));
1341 if (!pDevIns)
1342 return VERR_INVALID_PARAMETER;
1343
1344 tmLock(pVM);
1345 PTMTIMER pCur = pVM->tm.s.pCreated;
1346 while (pCur)
1347 {
1348 PTMTIMER pDestroy = pCur;
1349 pCur = pDestroy->pBigNext;
1350 if ( pDestroy->enmType == TMTIMERTYPE_DEV
1351 && pDestroy->u.Dev.pDevIns == pDevIns)
1352 {
1353 int rc = TMTimerDestroy(pDestroy);
1354 AssertRC(rc);
1355 }
1356 }
1357 tmUnlock(pVM);
1358
1359 LogFlow(("TMR3TimerDestroyDevice: returns VINF_SUCCESS\n"));
1360 return VINF_SUCCESS;
1361}
1362
1363
1364/**
1365 * Destroy all timers owned by a driver.
1366 *
1367 * @returns VBox status.
1368 * @param pVM VM handle.
1369 * @param pDrvIns Driver which timers should be destroyed.
1370 */
1371VMMR3DECL(int) TMR3TimerDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns)
1372{
1373 LogFlow(("TMR3TimerDestroyDriver: pDrvIns=%p\n", pDrvIns));
1374 if (!pDrvIns)
1375 return VERR_INVALID_PARAMETER;
1376
1377 tmLock(pVM);
1378 PTMTIMER pCur = pVM->tm.s.pCreated;
1379 while (pCur)
1380 {
1381 PTMTIMER pDestroy = pCur;
1382 pCur = pDestroy->pBigNext;
1383 if ( pDestroy->enmType == TMTIMERTYPE_DRV
1384 && pDestroy->u.Drv.pDrvIns == pDrvIns)
1385 {
1386 int rc = TMTimerDestroy(pDestroy);
1387 AssertRC(rc);
1388 }
1389 }
1390 tmUnlock(pVM);
1391
1392 LogFlow(("TMR3TimerDestroyDriver: returns VINF_SUCCESS\n"));
1393 return VINF_SUCCESS;
1394}
1395
1396
1397/**
1398 * Internal function for getting the clock time.
1399 *
1400 * @returns clock time.
1401 * @param pVM The VM handle.
1402 * @param enmClock The clock.
1403 */
1404DECLINLINE(uint64_t) tmClock(PVM pVM, TMCLOCK enmClock)
1405{
1406 switch (enmClock)
1407 {
1408 case TMCLOCK_VIRTUAL: return TMVirtualGet(pVM);
1409 case TMCLOCK_VIRTUAL_SYNC: return TMVirtualSyncGet(pVM);
1410 case TMCLOCK_REAL: return TMRealGet(pVM);
1411 case TMCLOCK_TSC: return TMCpuTickGet(&pVM->aCpus[0] /* just take VCPU 0 */);
1412 default:
1413 AssertMsgFailed(("enmClock=%d\n", enmClock));
1414 return ~(uint64_t)0;
1415 }
1416}
1417
1418
1419/**
1420 * Checks if the sync queue has one or more expired timers.
1421 *
1422 * @returns true / false.
1423 *
1424 * @param pVM The VM handle.
1425 * @param enmClock The queue.
1426 */
1427DECLINLINE(bool) tmR3HasExpiredTimer(PVM pVM, TMCLOCK enmClock)
1428{
1429 const uint64_t u64Expire = pVM->tm.s.CTX_SUFF(paTimerQueues)[enmClock].u64Expire;
1430 return u64Expire != INT64_MAX && u64Expire <= tmClock(pVM, enmClock);
1431}
1432
1433
1434/**
1435 * Checks for expired timers in all the queues.
1436 *
1437 * @returns true / false.
1438 * @param pVM The VM handle.
1439 */
1440DECLINLINE(bool) tmR3AnyExpiredTimers(PVM pVM)
1441{
1442 /*
1443 * Combine the time calculation for the first two since we're not on EMT
1444 * TMVirtualSyncGet only permits EMT.
1445 */
1446 uint64_t u64Now = TMVirtualGet(pVM);
1447 if (pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire <= u64Now)
1448 return true;
1449 u64Now = pVM->tm.s.fVirtualSyncTicking
1450 ? u64Now - pVM->tm.s.offVirtualSync
1451 : pVM->tm.s.u64VirtualSync;
1452 if (pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire <= u64Now)
1453 return true;
1454
1455 /*
1456 * The remaining timers.
1457 */
1458 if (tmR3HasExpiredTimer(pVM, TMCLOCK_REAL))
1459 return true;
1460 if (tmR3HasExpiredTimer(pVM, TMCLOCK_TSC))
1461 return true;
1462 return false;
1463}
1464
1465
1466/**
1467 * Schedulation timer callback.
1468 *
1469 * @param pTimer Timer handle.
1470 * @param pvUser VM handle.
1471 * @thread Timer thread.
1472 *
1473 * @remark We cannot do the scheduling and queues running from a timer handler
1474 * since it's not executing in EMT, and even if it was it would be async
1475 * and we wouldn't know the state of the affairs.
1476 * So, we'll just raise the timer FF and force any REM execution to exit.
1477 */
1478static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t /*iTick*/)
1479{
1480 PVM pVM = (PVM)pvUser;
1481 AssertCompile(TMCLOCK_MAX == 4);
1482#ifdef DEBUG_Sander /* very annoying, keep it private. */
1483 if (VM_FF_ISSET(pVM, VM_FF_TIMER))
1484 Log(("tmR3TimerCallback: timer event still pending!!\n"));
1485#endif
1486 if ( !VM_FF_ISSET(pVM, VM_FF_TIMER)
1487 && ( pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].offSchedule
1488 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].offSchedule
1489 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].offSchedule
1490 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].offSchedule
1491 || tmR3AnyExpiredTimers(pVM)
1492 )
1493 && !VM_FF_ISSET(pVM, VM_FF_TIMER)
1494 && !pVM->tm.s.fRunningQueues
1495 )
1496 {
1497 VM_FF_SET(pVM, VM_FF_TIMER);
1498 REMR3NotifyTimerPending(pVM);
1499 VMR3NotifyGlobalFFU(pVM->pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
1500 STAM_COUNTER_INC(&pVM->tm.s.StatTimerCallbackSetFF);
1501 }
1502}
1503
1504
1505/**
1506 * Schedules and runs any pending timers.
1507 *
1508 * This is normally called from a forced action handler in EMT.
1509 *
1510 * @param pVM The VM to run the timers for.
1511 *
1512 * @thread EMT (actually EMT0, but we fend off the others)
1513 */
1514VMMR3DECL(void) TMR3TimerQueuesDo(PVM pVM)
1515{
1516 /*
1517 * Only one EMT should be doing this at a time.
1518 */
1519 VM_FF_CLEAR(pVM, VM_FF_TIMER);
1520 if (ASMBitTestAndSet(&pVM->tm.s.fRunningQueues, 0))
1521 {
1522 Assert(pVM->cCPUs > 1);
1523 return;
1524 }
1525
1526 STAM_PROFILE_START(&pVM->tm.s.StatDoQueues, a);
1527 Log2(("TMR3TimerQueuesDo:\n"));
1528 tmLock(pVM);
1529
1530 /*
1531 * Process the queues.
1532 */
1533 AssertCompile(TMCLOCK_MAX == 4);
1534
1535 /* TMCLOCK_VIRTUAL_SYNC */
1536 STAM_PROFILE_ADV_START(&pVM->tm.s.StatDoQueuesSchedule, s1);
1537 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC]);
1538 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesSchedule, s1);
1539 STAM_PROFILE_ADV_START(&pVM->tm.s.StatDoQueuesRun, r1);
1540 tmR3TimerQueueRunVirtualSync(pVM);
1541 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesRun, r1);
1542
1543 /* TMCLOCK_VIRTUAL */
1544 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesSchedule, s1);
1545 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL]);
1546 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesSchedule, s2);
1547 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesRun, r1);
1548 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL]);
1549 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesRun, r2);
1550
1551#if 0 /** @todo if ever used, remove this and fix the stam prefixes on TMCLOCK_REAL below. */
1552 /* TMCLOCK_TSC */
1553 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesSchedule, s2);
1554 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC]);
1555 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesSchedule, s3);
1556 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesRun, r2);
1557 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC]);
1558 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesRun, r3);
1559#endif
1560
1561 /* TMCLOCK_REAL */
1562 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesSchedule, s2);
1563 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL]);
1564 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatDoQueuesSchedule, s3);
1565 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesRun, r2);
1566 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL]);
1567 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatDoQueuesRun, r3);
1568
1569#ifdef VBOX_STRICT
1570 /* check that we didn't screwup. */
1571 tmTimerQueuesSanityChecks(pVM, "TMR3TimerQueuesDo");
1572#endif
1573
1574 Log2(("TMR3TimerQueuesDo: returns void\n"));
1575 STAM_PROFILE_STOP(&pVM->tm.s.StatDoQueues, a);
1576
1577 /* done */
1578 ASMAtomicBitClear(&pVM->tm.s.fRunningQueues, 0);
1579 tmUnlock(pVM);
1580}
1581
1582
1583/**
1584 * Schedules and runs any pending times in the specified queue.
1585 *
1586 * This is normally called from a forced action handler in EMT.
1587 *
1588 * @param pVM The VM to run the timers for.
1589 * @param pQueue The queue to run.
1590 */
1591static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue)
1592{
1593 VM_ASSERT_EMT(pVM);
1594
1595 /*
1596 * Run timers.
1597 *
1598 * We check the clock once and run all timers which are ACTIVE
1599 * and have an expire time less or equal to the time we read.
1600 *
1601 * N.B. A generic unlink must be applied since other threads
1602 * are allowed to mess with any active timer at any time.
1603 * However, we only allow EMT to handle EXPIRED_PENDING
1604 * timers, thus enabling the timer handler function to
1605 * arm the timer again.
1606 */
1607 PTMTIMER pNext = TMTIMER_GET_HEAD(pQueue);
1608 if (!pNext)
1609 return;
1610 const uint64_t u64Now = tmClock(pVM, pQueue->enmClock);
1611 while (pNext && pNext->u64Expire <= u64Now)
1612 {
1613 PTMTIMER pTimer = pNext;
1614 pNext = TMTIMER_GET_NEXT(pTimer);
1615 Log2(("tmR3TimerQueueRun: pTimer=%p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
1616 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
1617 bool fRc;
1618 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED, TMTIMERSTATE_ACTIVE, fRc);
1619 if (fRc)
1620 {
1621 Assert(!pTimer->offScheduleNext); /* this can trigger falsely */
1622
1623 /* unlink */
1624 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1625 if (pPrev)
1626 TMTIMER_SET_NEXT(pPrev, pNext);
1627 else
1628 {
1629 TMTIMER_SET_HEAD(pQueue, pNext);
1630 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1631 }
1632 if (pNext)
1633 TMTIMER_SET_PREV(pNext, pPrev);
1634 pTimer->offNext = 0;
1635 pTimer->offPrev = 0;
1636
1637
1638 /* fire */
1639 switch (pTimer->enmType)
1640 {
1641 case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer); break;
1642 case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer); break;
1643 case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->u.Internal.pvUser); break;
1644 case TMTIMERTYPE_EXTERNAL: pTimer->u.External.pfnTimer(pTimer->u.External.pvUser); break;
1645 default:
1646 AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->pszDesc));
1647 break;
1648 }
1649
1650 /* change the state if it wasn't changed already in the handler. */
1651 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED, fRc);
1652 Log2(("tmR3TimerQueueRun: new state %s\n", tmTimerState(pTimer->enmState)));
1653 }
1654 } /* run loop */
1655}
1656
1657
1658/**
1659 * Schedules and runs any pending times in the timer queue for the
1660 * synchronous virtual clock.
1661 *
1662 * This scheduling is a bit different from the other queues as it need
1663 * to implement the special requirements of the timer synchronous virtual
1664 * clock, thus this 2nd queue run funcion.
1665 *
1666 * @param pVM The VM to run the timers for.
1667 */
1668static void tmR3TimerQueueRunVirtualSync(PVM pVM)
1669{
1670 PTMTIMERQUEUE const pQueue = &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC];
1671 VM_ASSERT_EMT(pVM);
1672
1673 /*
1674 * Any timers?
1675 */
1676 PTMTIMER pNext = TMTIMER_GET_HEAD(pQueue);
1677 if (RT_UNLIKELY(!pNext))
1678 {
1679 Assert(pVM->tm.s.fVirtualSyncTicking || !pVM->tm.s.cVirtualTicking);
1680 return;
1681 }
1682 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRun);
1683
1684 /*
1685 * Calculate the time frame for which we will dispatch timers.
1686 *
1687 * We use a time frame ranging from the current sync time (which is most likely the
1688 * same as the head timer) and some configurable period (100000ns) up towards the
1689 * current virtual time. This period might also need to be restricted by the catch-up
1690 * rate so frequent calls to this function won't accelerate the time too much, however
1691 * this will be implemented at a later point if neccessary.
1692 *
1693 * Without this frame we would 1) having to run timers much more frequently
1694 * and 2) lag behind at a steady rate.
1695 */
1696 const uint64_t u64VirtualNow = TMVirtualGetEx(pVM, false /* don't check timers */);
1697 uint64_t u64Now;
1698 if (!pVM->tm.s.fVirtualSyncTicking)
1699 {
1700 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunStoppedAlready);
1701 u64Now = pVM->tm.s.u64VirtualSync;
1702 Assert(u64Now <= pNext->u64Expire);
1703 }
1704 else
1705 {
1706 /* Calc 'now'. (update order doesn't really matter here) */
1707 uint64_t off = pVM->tm.s.offVirtualSync;
1708 if (pVM->tm.s.fVirtualSyncCatchUp)
1709 {
1710 uint64_t u64Delta = u64VirtualNow - pVM->tm.s.u64VirtualSyncCatchUpPrev;
1711 if (RT_LIKELY(!(u64Delta >> 32)))
1712 {
1713 uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
1714 if (off > u64Sub + pVM->tm.s.offVirtualSyncGivenUp)
1715 {
1716 off -= u64Sub;
1717 Log4(("TM: %RU64/%RU64: sub %RU64 (run)\n", u64VirtualNow - off, off - pVM->tm.s.offVirtualSyncGivenUp, u64Sub));
1718 }
1719 else
1720 {
1721 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
1722 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
1723 off = pVM->tm.s.offVirtualSyncGivenUp;
1724 Log4(("TM: %RU64/0: caught up (run)\n", u64VirtualNow));
1725 }
1726 }
1727 ASMAtomicXchgU64(&pVM->tm.s.offVirtualSync, off);
1728 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64VirtualNow;
1729 }
1730 u64Now = u64VirtualNow - off;
1731
1732 /* Check if stopped by expired timer. */
1733 if (u64Now >= pNext->u64Expire)
1734 {
1735 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunStop);
1736 u64Now = pNext->u64Expire;
1737 ASMAtomicXchgU64(&pVM->tm.s.u64VirtualSync, u64Now);
1738 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncTicking, false);
1739 Log4(("TM: %RU64/%RU64: exp tmr (run)\n", u64Now, u64VirtualNow - u64Now - pVM->tm.s.offVirtualSyncGivenUp));
1740
1741 }
1742 }
1743
1744 /* calc end of frame. */
1745 uint64_t u64Max = u64Now + pVM->tm.s.u32VirtualSyncScheduleSlack;
1746 if (u64Max > u64VirtualNow - pVM->tm.s.offVirtualSyncGivenUp)
1747 u64Max = u64VirtualNow - pVM->tm.s.offVirtualSyncGivenUp;
1748
1749 /* assert sanity */
1750 Assert(u64Now <= u64VirtualNow - pVM->tm.s.offVirtualSyncGivenUp);
1751 Assert(u64Max <= u64VirtualNow - pVM->tm.s.offVirtualSyncGivenUp);
1752 Assert(u64Now <= u64Max);
1753
1754 /*
1755 * Process the expired timers moving the clock along as we progress.
1756 */
1757#ifdef VBOX_STRICT
1758 uint64_t u64Prev = u64Now; NOREF(u64Prev);
1759#endif
1760 while (pNext && pNext->u64Expire <= u64Max)
1761 {
1762 PTMTIMER pTimer = pNext;
1763 pNext = TMTIMER_GET_NEXT(pTimer);
1764 Log2(("tmR3TimerQueueRun: pTimer=%p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
1765 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
1766 bool fRc;
1767 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED, TMTIMERSTATE_ACTIVE, fRc);
1768 if (fRc)
1769 {
1770 /* unlink */
1771 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1772 if (pPrev)
1773 TMTIMER_SET_NEXT(pPrev, pNext);
1774 else
1775 {
1776 TMTIMER_SET_HEAD(pQueue, pNext);
1777 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1778 }
1779 if (pNext)
1780 TMTIMER_SET_PREV(pNext, pPrev);
1781 pTimer->offNext = 0;
1782 pTimer->offPrev = 0;
1783
1784 /* advance the clock - don't permit timers to be out of order or armed in the 'past'. */
1785#ifdef VBOX_STRICT
1786 AssertMsg(pTimer->u64Expire >= u64Prev, ("%RU64 < %RU64 %s\n", pTimer->u64Expire, u64Prev, pTimer->pszDesc));
1787 u64Prev = pTimer->u64Expire;
1788#endif
1789 ASMAtomicXchgSize(&pVM->tm.s.fVirtualSyncTicking, false);
1790 ASMAtomicXchgU64(&pVM->tm.s.u64VirtualSync, pTimer->u64Expire);
1791
1792 /* fire */
1793 switch (pTimer->enmType)
1794 {
1795 case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer); break;
1796 case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer); break;
1797 case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->u.Internal.pvUser); break;
1798 case TMTIMERTYPE_EXTERNAL: pTimer->u.External.pfnTimer(pTimer->u.External.pvUser); break;
1799 default:
1800 AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->pszDesc));
1801 break;
1802 }
1803
1804 /* change the state if it wasn't changed already in the handler. */
1805 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED, fRc);
1806 Log2(("tmR3TimerQueueRun: new state %s\n", tmTimerState(pTimer->enmState)));
1807 }
1808 } /* run loop */
1809
1810 /*
1811 * Restart the clock if it was stopped to serve any timers,
1812 * and start/adjust catch-up if necessary.
1813 */
1814 if ( !pVM->tm.s.fVirtualSyncTicking
1815 && pVM->tm.s.cVirtualTicking)
1816 {
1817 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunRestart);
1818
1819 /* calc the slack we've handed out. */
1820 const uint64_t u64VirtualNow2 = TMVirtualGetEx(pVM, false /* don't check timers */);
1821 Assert(u64VirtualNow2 >= u64VirtualNow);
1822 AssertMsg(pVM->tm.s.u64VirtualSync >= u64Now, ("%RU64 < %RU64\n", pVM->tm.s.u64VirtualSync, u64Now));
1823 const uint64_t offSlack = pVM->tm.s.u64VirtualSync - u64Now;
1824 STAM_STATS({
1825 if (offSlack)
1826 {
1827 PSTAMPROFILE p = &pVM->tm.s.StatVirtualSyncRunSlack;
1828 p->cPeriods++;
1829 p->cTicks += offSlack;
1830 if (p->cTicksMax < offSlack) p->cTicksMax = offSlack;
1831 if (p->cTicksMin > offSlack) p->cTicksMin = offSlack;
1832 }
1833 });
1834
1835 /* Let the time run a little bit while we were busy running timers(?). */
1836 uint64_t u64Elapsed;
1837#define MAX_ELAPSED 30000 /* ns */
1838 if (offSlack > MAX_ELAPSED)
1839 u64Elapsed = 0;
1840 else
1841 {
1842 u64Elapsed = u64VirtualNow2 - u64VirtualNow;
1843 if (u64Elapsed > MAX_ELAPSED)
1844 u64Elapsed = MAX_ELAPSED;
1845 u64Elapsed = u64Elapsed > offSlack ? u64Elapsed - offSlack : 0;
1846 }
1847#undef MAX_ELAPSED
1848
1849 /* Calc the current offset. */
1850 uint64_t offNew = u64VirtualNow2 - pVM->tm.s.u64VirtualSync - u64Elapsed;
1851 Assert(!(offNew & RT_BIT_64(63)));
1852 uint64_t offLag = offNew - pVM->tm.s.offVirtualSyncGivenUp;
1853 Assert(!(offLag & RT_BIT_64(63)));
1854
1855 /*
1856 * Deal with starting, adjusting and stopping catchup.
1857 */
1858 if (pVM->tm.s.fVirtualSyncCatchUp)
1859 {
1860 if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpStopThreshold)
1861 {
1862 /* stop */
1863 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
1864 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
1865 Log4(("TM: %RU64/%RU64: caught up\n", u64VirtualNow2 - offNew, offLag));
1866 }
1867 else if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold)
1868 {
1869 /* adjust */
1870 unsigned i = 0;
1871 while ( i + 1 < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods)
1872 && offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[i + 1].u64Start)
1873 i++;
1874 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage < pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage)
1875 {
1876 STAM_COUNTER_INC(&pVM->tm.s.aStatVirtualSyncCatchupAdjust[i]);
1877 ASMAtomicXchgU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
1878 Log4(("TM: %RU64/%RU64: adj %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
1879 }
1880 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64VirtualNow2;
1881 }
1882 else
1883 {
1884 /* give up */
1885 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGiveUp);
1886 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
1887 ASMAtomicXchgU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
1888 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
1889 Log4(("TM: %RU64/%RU64: give up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
1890 LogRel(("TM: Giving up catch-up attempt at a %RU64 ns lag; new total: %RU64 ns\n", offLag, offNew));
1891 }
1892 }
1893 else if (offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[0].u64Start)
1894 {
1895 if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold)
1896 {
1897 /* start */
1898 STAM_PROFILE_ADV_START(&pVM->tm.s.StatVirtualSyncCatchup, c);
1899 unsigned i = 0;
1900 while ( i + 1 < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods)
1901 && offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[i + 1].u64Start)
1902 i++;
1903 STAM_COUNTER_INC(&pVM->tm.s.aStatVirtualSyncCatchupInitial[i]);
1904 ASMAtomicXchgU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
1905 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncCatchUp, true);
1906 Log4(("TM: %RU64/%RU64: catch-up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
1907 }
1908 else
1909 {
1910 /* don't bother */
1911 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGiveUpBeforeStarting);
1912 ASMAtomicXchgU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
1913 Log4(("TM: %RU64/%RU64: give up\n", u64VirtualNow2 - offNew, offLag));
1914 LogRel(("TM: Not bothering to attempt catching up a %RU64 ns lag; new total: %RU64\n", offLag, offNew));
1915 }
1916 }
1917
1918 /*
1919 * Update the offset and restart the clock.
1920 */
1921 Assert(!(offNew & RT_BIT_64(63)));
1922 ASMAtomicXchgU64(&pVM->tm.s.offVirtualSync, offNew);
1923 ASMAtomicXchgBool(&pVM->tm.s.fVirtualSyncTicking, true);
1924 }
1925}
1926
1927
1928/**
1929 * Saves the state of a timer to a saved state.
1930 *
1931 * @returns VBox status.
1932 * @param pTimer Timer to save.
1933 * @param pSSM Save State Manager handle.
1934 */
1935VMMR3DECL(int) TMR3TimerSave(PTMTIMERR3 pTimer, PSSMHANDLE pSSM)
1936{
1937 LogFlow(("TMR3TimerSave: pTimer=%p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
1938 switch (pTimer->enmState)
1939 {
1940 case TMTIMERSTATE_STOPPED:
1941 case TMTIMERSTATE_PENDING_STOP:
1942 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1943 return SSMR3PutU8(pSSM, (uint8_t)TMTIMERSTATE_PENDING_STOP);
1944
1945 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1946 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1947 AssertMsgFailed(("u64Expire is being updated! (%s)\n", pTimer->pszDesc));
1948 if (!RTThreadYield())
1949 RTThreadSleep(1);
1950 /* fall thru */
1951 case TMTIMERSTATE_ACTIVE:
1952 case TMTIMERSTATE_PENDING_SCHEDULE:
1953 case TMTIMERSTATE_PENDING_RESCHEDULE:
1954 SSMR3PutU8(pSSM, (uint8_t)TMTIMERSTATE_PENDING_SCHEDULE);
1955 return SSMR3PutU64(pSSM, pTimer->u64Expire);
1956
1957 case TMTIMERSTATE_EXPIRED:
1958 case TMTIMERSTATE_PENDING_DESTROY:
1959 case TMTIMERSTATE_PENDING_STOP_DESTROY:
1960 case TMTIMERSTATE_FREE:
1961 AssertMsgFailed(("Invalid timer state %d %s (%s)\n", pTimer->enmState, tmTimerState(pTimer->enmState), pTimer->pszDesc));
1962 return SSMR3HandleSetStatus(pSSM, VERR_TM_INVALID_STATE);
1963 }
1964
1965 AssertMsgFailed(("Unknown timer state %d (%s)\n", pTimer->enmState, pTimer->pszDesc));
1966 return SSMR3HandleSetStatus(pSSM, VERR_TM_UNKNOWN_STATE);
1967}
1968
1969
1970/**
1971 * Loads the state of a timer from a saved state.
1972 *
1973 * @returns VBox status.
1974 * @param pTimer Timer to restore.
1975 * @param pSSM Save State Manager handle.
1976 */
1977VMMR3DECL(int) TMR3TimerLoad(PTMTIMERR3 pTimer, PSSMHANDLE pSSM)
1978{
1979 Assert(pTimer); Assert(pSSM); VM_ASSERT_EMT(pTimer->pVMR3);
1980 LogFlow(("TMR3TimerLoad: pTimer=%p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
1981
1982 /*
1983 * Load the state and validate it.
1984 */
1985 uint8_t u8State;
1986 int rc = SSMR3GetU8(pSSM, &u8State);
1987 if (RT_FAILURE(rc))
1988 return rc;
1989 TMTIMERSTATE enmState = (TMTIMERSTATE)u8State;
1990 if ( enmState != TMTIMERSTATE_PENDING_STOP
1991 && enmState != TMTIMERSTATE_PENDING_SCHEDULE
1992 && enmState != TMTIMERSTATE_PENDING_STOP_SCHEDULE)
1993 {
1994 AssertMsgFailed(("enmState=%d %s\n", enmState, tmTimerState(enmState)));
1995 return SSMR3HandleSetStatus(pSSM, VERR_TM_LOAD_STATE);
1996 }
1997
1998 if (enmState == TMTIMERSTATE_PENDING_SCHEDULE)
1999 {
2000 /*
2001 * Load the expire time.
2002 */
2003 uint64_t u64Expire;
2004 rc = SSMR3GetU64(pSSM, &u64Expire);
2005 if (RT_FAILURE(rc))
2006 return rc;
2007
2008 /*
2009 * Set it.
2010 */
2011 Log(("enmState=%d %s u64Expire=%llu\n", enmState, tmTimerState(enmState), u64Expire));
2012 rc = TMTimerSet(pTimer, u64Expire);
2013 }
2014 else
2015 {
2016 /*
2017 * Stop it.
2018 */
2019 Log(("enmState=%d %s\n", enmState, tmTimerState(enmState)));
2020 rc = TMTimerStop(pTimer);
2021 }
2022
2023 /*
2024 * On failure set SSM status.
2025 */
2026 if (RT_FAILURE(rc))
2027 rc = SSMR3HandleSetStatus(pSSM, rc);
2028 return rc;
2029}
2030
2031
2032/**
2033 * Get the real world UTC time adjusted for VM lag.
2034 *
2035 * @returns pTime.
2036 * @param pVM The VM instance.
2037 * @param pTime Where to store the time.
2038 */
2039VMMR3DECL(PRTTIMESPEC) TMR3UTCNow(PVM pVM, PRTTIMESPEC pTime)
2040{
2041 RTTimeNow(pTime);
2042 RTTimeSpecSubNano(pTime, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp);
2043 RTTimeSpecAddNano(pTime, pVM->tm.s.offUTC);
2044 return pTime;
2045}
2046
2047
2048/**
2049 * Display all timers.
2050 *
2051 * @param pVM VM Handle.
2052 * @param pHlp The info helpers.
2053 * @param pszArgs Arguments, ignored.
2054 */
2055static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2056{
2057 NOREF(pszArgs);
2058 pHlp->pfnPrintf(pHlp,
2059 "Timers (pVM=%p)\n"
2060 "%.*s %.*s %.*s %.*s Clock %-18s %-18s %-25s Description\n",
2061 pVM,
2062 sizeof(RTR3PTR) * 2, "pTimerR3 ",
2063 sizeof(int32_t) * 2, "offNext ",
2064 sizeof(int32_t) * 2, "offPrev ",
2065 sizeof(int32_t) * 2, "offSched ",
2066 "Time",
2067 "Expire",
2068 "State");
2069 tmLock(pVM);
2070 for (PTMTIMERR3 pTimer = pVM->tm.s.pCreated; pTimer; pTimer = pTimer->pBigNext)
2071 {
2072 pHlp->pfnPrintf(pHlp,
2073 "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %-25s %s\n",
2074 pTimer,
2075 pTimer->offNext,
2076 pTimer->offPrev,
2077 pTimer->offScheduleNext,
2078 pTimer->enmClock == TMCLOCK_REAL ? "Real " : "Virt ",
2079 TMTimerGet(pTimer),
2080 pTimer->u64Expire,
2081 tmTimerState(pTimer->enmState),
2082 pTimer->pszDesc);
2083 }
2084 tmUnlock(pVM);
2085}
2086
2087
2088/**
2089 * Display all active timers.
2090 *
2091 * @param pVM VM Handle.
2092 * @param pHlp The info helpers.
2093 * @param pszArgs Arguments, ignored.
2094 */
2095static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2096{
2097 NOREF(pszArgs);
2098 pHlp->pfnPrintf(pHlp,
2099 "Active Timers (pVM=%p)\n"
2100 "%.*s %.*s %.*s %.*s Clock %-18s %-18s %-25s Description\n",
2101 pVM,
2102 sizeof(RTR3PTR) * 2, "pTimerR3 ",
2103 sizeof(int32_t) * 2, "offNext ",
2104 sizeof(int32_t) * 2, "offPrev ",
2105 sizeof(int32_t) * 2, "offSched ",
2106 "Time",
2107 "Expire",
2108 "State");
2109 for (unsigned iQueue = 0; iQueue < TMCLOCK_MAX; iQueue++)
2110 {
2111 tmLock(pVM);
2112 for (PTMTIMERR3 pTimer = TMTIMER_GET_HEAD(&pVM->tm.s.paTimerQueuesR3[iQueue]);
2113 pTimer;
2114 pTimer = TMTIMER_GET_NEXT(pTimer))
2115 {
2116 pHlp->pfnPrintf(pHlp,
2117 "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %-25s %s\n",
2118 pTimer,
2119 pTimer->offNext,
2120 pTimer->offPrev,
2121 pTimer->offScheduleNext,
2122 pTimer->enmClock == TMCLOCK_REAL
2123 ? "Real "
2124 : pTimer->enmClock == TMCLOCK_VIRTUAL
2125 ? "Virt "
2126 : pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC
2127 ? "VrSy "
2128 : "TSC ",
2129 TMTimerGet(pTimer),
2130 pTimer->u64Expire,
2131 tmTimerState(pTimer->enmState),
2132 pTimer->pszDesc);
2133 }
2134 tmUnlock(pVM);
2135 }
2136}
2137
2138
2139/**
2140 * Display all clocks.
2141 *
2142 * @param pVM VM Handle.
2143 * @param pHlp The info helpers.
2144 * @param pszArgs Arguments, ignored.
2145 */
2146static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2147{
2148 NOREF(pszArgs);
2149
2150 /*
2151 * Read the times first to avoid more than necessary time variation.
2152 */
2153 const uint64_t u64Virtual = TMVirtualGet(pVM);
2154 const uint64_t u64VirtualSync = TMVirtualSyncGet(pVM);
2155 const uint64_t u64Real = TMRealGet(pVM);
2156
2157 for (unsigned i = 0; i < pVM->cCPUs; i++)
2158 {
2159 PVMCPU pVCpu = &pVM->aCpus[i];
2160 uint64_t u64TSC = TMCpuTickGet(pVCpu);
2161
2162 /*
2163 * TSC
2164 */
2165 pHlp->pfnPrintf(pHlp,
2166 "Cpu Tick: %18RU64 (%#016RX64) %RU64Hz %s%s",
2167 u64TSC, u64TSC, TMCpuTicksPerSecond(pVM),
2168 pVCpu->tm.s.fTSCTicking ? "ticking" : "paused",
2169 pVM->tm.s.fTSCVirtualized ? " - virtualized" : "");
2170 if (pVM->tm.s.fTSCUseRealTSC)
2171 {
2172 pHlp->pfnPrintf(pHlp, " - real tsc");
2173 if (pVCpu->tm.s.u64TSCOffset)
2174 pHlp->pfnPrintf(pHlp, "\n offset %RU64", pVCpu->tm.s.u64TSCOffset);
2175 }
2176 else
2177 pHlp->pfnPrintf(pHlp, " - virtual clock");
2178 pHlp->pfnPrintf(pHlp, "\n");
2179 }
2180
2181 /*
2182 * virtual
2183 */
2184 pHlp->pfnPrintf(pHlp,
2185 " Virtual: %18RU64 (%#016RX64) %RU64Hz %s",
2186 u64Virtual, u64Virtual, TMVirtualGetFreq(pVM),
2187 pVM->tm.s.cVirtualTicking ? "ticking" : "paused");
2188 if (pVM->tm.s.fVirtualWarpDrive)
2189 pHlp->pfnPrintf(pHlp, " WarpDrive %RU32 %%", pVM->tm.s.u32VirtualWarpDrivePercentage);
2190 pHlp->pfnPrintf(pHlp, "\n");
2191
2192 /*
2193 * virtual sync
2194 */
2195 pHlp->pfnPrintf(pHlp,
2196 "VirtSync: %18RU64 (%#016RX64) %s%s",
2197 u64VirtualSync, u64VirtualSync,
2198 pVM->tm.s.fVirtualSyncTicking ? "ticking" : "paused",
2199 pVM->tm.s.fVirtualSyncCatchUp ? " - catchup" : "");
2200 if (pVM->tm.s.offVirtualSync)
2201 {
2202 pHlp->pfnPrintf(pHlp, "\n offset %RU64", pVM->tm.s.offVirtualSync);
2203 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage)
2204 pHlp->pfnPrintf(pHlp, " catch-up rate %u %%", pVM->tm.s.u32VirtualSyncCatchUpPercentage);
2205 }
2206 pHlp->pfnPrintf(pHlp, "\n");
2207
2208 /*
2209 * real
2210 */
2211 pHlp->pfnPrintf(pHlp,
2212 " Real: %18RU64 (%#016RX64) %RU64Hz\n",
2213 u64Real, u64Real, TMRealGetFreq(pVM));
2214}
2215
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