VirtualBox

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

Last change on this file since 3942 was 3942, checked in by vboxsync, 17 years ago

fixed detecting the CPU vendor (in DevAPIC) by introducing symbolic constants

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