VirtualBox

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

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

Split TM for SMP guests.

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

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