VirtualBox

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

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

TM: LogRel GIP mode and update rate.

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