VirtualBox

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

Last change on this file since 30604 was 30586, checked in by vboxsync, 15 years ago

TM: Added timer that calculates the cpu load % every 1000 ms (real time).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 125.6 KB
Line 
1/* $Id: TM.cpp 30586 2010-07-02 18:12:17Z vboxsync $ */
2/** @file
3 * TM - Time Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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
18/** @page pg_tm TM - The Time Manager
19 *
20 * The Time Manager abstracts the CPU clocks and manages timers used by the VMM,
21 * device and drivers.
22 *
23 * @see grp_tm
24 *
25 *
26 * @section sec_tm_clocks Clocks
27 *
28 * There are currently 4 clocks:
29 * - Virtual (guest).
30 * - Synchronous virtual (guest).
31 * - CPU Tick (TSC) (guest). Only current use is rdtsc emulation. Usually a
32 * function of the virtual clock.
33 * - Real (host). This is only used for display updates atm.
34 *
35 * The most important clocks are the three first ones and of these the second is
36 * the most interesting.
37 *
38 *
39 * The synchronous virtual clock is tied to the virtual clock except that it
40 * will take into account timer delivery lag caused by host scheduling. It will
41 * normally never advance beyond the head timer, and when lagging too far behind
42 * it will gradually speed up to catch up with the virtual clock. All devices
43 * implementing time sources accessible to and used by the guest is using this
44 * clock (for timers and other things). This ensures consistency between the
45 * time sources.
46 *
47 * The virtual clock is implemented as an offset to a monotonic, high
48 * resolution, wall clock. The current time source is using the RTTimeNanoTS()
49 * machinery based upon the Global Info Pages (GIP), that is, we're using TSC
50 * deltas (usually 10 ms) to fill the gaps between GIP updates. The result is
51 * a fairly high res clock that works in all contexts and on all hosts. The
52 * virtual clock is paused when the VM isn't in the running state.
53 *
54 * The CPU tick (TSC) is normally virtualized as a function of the synchronous
55 * virtual clock, where the frequency defaults to the host cpu frequency (as we
56 * measure it). In this mode it is possible to configure the frequency. Another
57 * (non-default) option is to use the raw unmodified host TSC values. And yet
58 * another, to tie it to time spent executing guest code. All these things are
59 * configurable should non-default behavior be desirable.
60 *
61 * The real clock is a monotonic clock (when available) with relatively low
62 * resolution, though this a bit host specific. Note that we're currently not
63 * servicing timers using the real clock when the VM is not running, this is
64 * simply because it has not been needed yet therefore not implemented.
65 *
66 *
67 * @subsection subsec_tm_timesync Guest Time Sync / UTC time
68 *
69 * Guest time syncing is primarily taken care of by the VMM device. The
70 * principle is very simple, the guest additions periodically asks the VMM
71 * device what the current UTC time is and makes adjustments accordingly.
72 *
73 * A complicating factor is that the synchronous virtual clock might be doing
74 * catchups and the guest perception is currently a little bit behind the world
75 * but it will (hopefully) be catching up soon as we're feeding timer interrupts
76 * at a slightly higher rate. Adjusting the guest clock to the current wall
77 * time in the real world would be a bad idea then because the guest will be
78 * advancing too fast and run ahead of world time (if the catchup works out).
79 * To solve this problem TM provides the VMM device with an UTC time source that
80 * gets adjusted with the current lag, so that when the guest eventually catches
81 * up the lag it will be showing correct real world time.
82 *
83 *
84 * @section sec_tm_timers Timers
85 *
86 * The timers can use any of the TM clocks described in the previous section.
87 * Each clock has its own scheduling facility, or timer queue if you like.
88 * There are a few factors which makes it a bit complex. First, there is the
89 * usual R0 vs R3 vs. RC thing. Then there are multiple threads, and then there
90 * is the timer thread that periodically checks whether any timers has expired
91 * without EMT noticing. On the API level, all but the create and save APIs
92 * must be mulithreaded. EMT will always run the timers.
93 *
94 * The design is using a doubly linked list of active timers which is ordered
95 * by expire date. This list is only modified by the EMT thread. Updates to
96 * the list are batched in a singly linked list, which is then processed by the
97 * EMT thread at the first opportunity (immediately, next time EMT modifies a
98 * timer on that clock, or next timer timeout). Both lists are offset based and
99 * all the elements are therefore allocated from the hyper heap.
100 *
101 * For figuring out when there is need to schedule and run timers TM will:
102 * - Poll whenever somebody queries the virtual clock.
103 * - Poll the virtual clocks from the EM and REM loops.
104 * - Poll the virtual clocks from trap exit path.
105 * - Poll the virtual clocks and calculate first timeout from the halt loop.
106 * - Employ a thread which periodically (100Hz) polls all the timer queues.
107 *
108 *
109 * @image html TMTIMER-Statechart-Diagram.gif
110 *
111 * @section sec_tm_timer Logging
112 *
113 * Level 2: Logs a most of the timer state transitions and queue servicing.
114 * Level 3: Logs a few oddments.
115 * Level 4: Logs TMCLOCK_VIRTUAL_SYNC catch-up events.
116 *
117 */
118
119/*******************************************************************************
120* Header Files *
121*******************************************************************************/
122#define LOG_GROUP LOG_GROUP_TM
123#include <VBox/tm.h>
124#include <VBox/vmm.h>
125#include <VBox/mm.h>
126#include <VBox/ssm.h>
127#include <VBox/dbgf.h>
128#include <VBox/rem.h>
129#include <VBox/pdmapi.h>
130#include <VBox/iom.h>
131#include "TMInternal.h"
132#include <VBox/vm.h>
133
134#include <VBox/pdmdev.h>
135#include <VBox/param.h>
136#include <VBox/err.h>
137
138#include <VBox/log.h>
139#include <iprt/asm.h>
140#include <iprt/asm-math.h>
141#include <iprt/asm-amd64-x86.h>
142#include <iprt/assert.h>
143#include <iprt/thread.h>
144#include <iprt/time.h>
145#include <iprt/timer.h>
146#include <iprt/semaphore.h>
147#include <iprt/string.h>
148#include <iprt/env.h>
149
150
151/*******************************************************************************
152* Defined Constants And Macros *
153*******************************************************************************/
154/** The current saved state version.*/
155#define TM_SAVED_STATE_VERSION 3
156
157
158/*******************************************************************************
159* Internal Functions *
160*******************************************************************************/
161static bool tmR3HasFixedTSC(PVM pVM);
162static uint64_t tmR3CalibrateTSC(PVM pVM);
163static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM);
164static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
165static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
166static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue);
167static void tmR3TimerQueueRunVirtualSync(PVM pVM);
168static DECLCALLBACK(int) tmR3SetWarpDrive(PVM pVM, uint32_t u32Percent);
169#ifndef VBOX_WITHOUT_NS_ACCOUNTING
170static DECLCALLBACK(void) tmR3CpuLoadTimer(PVM pVM, PTMTIMER pTimer, void *pvUser);
171#endif
172static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
173static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
174static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
175
176
177/**
178 * Initializes the TM.
179 *
180 * @returns VBox status code.
181 * @param pVM The VM to operate on.
182 */
183VMM_INT_DECL(int) TMR3Init(PVM pVM)
184{
185 LogFlow(("TMR3Init:\n"));
186
187 /*
188 * Assert alignment and sizes.
189 */
190 AssertCompileMemberAlignment(VM, tm.s, 32);
191 AssertCompile(sizeof(pVM->tm.s) <= sizeof(pVM->tm.padding));
192 AssertCompileMemberAlignment(TM, TimerCritSect, 8);
193 AssertCompileMemberAlignment(TM, VirtualSyncLock, 8);
194
195 /*
196 * Init the structure.
197 */
198 void *pv;
199 int rc = MMHyperAlloc(pVM, sizeof(pVM->tm.s.paTimerQueuesR3[0]) * TMCLOCK_MAX, 0, MM_TAG_TM, &pv);
200 AssertRCReturn(rc, rc);
201 pVM->tm.s.paTimerQueuesR3 = (PTMTIMERQUEUE)pv;
202 pVM->tm.s.paTimerQueuesR0 = MMHyperR3ToR0(pVM, pv);
203 pVM->tm.s.paTimerQueuesRC = MMHyperR3ToRC(pVM, pv);
204
205 pVM->tm.s.offVM = RT_OFFSETOF(VM, tm.s);
206 pVM->tm.s.idTimerCpu = pVM->cCpus - 1; /* The last CPU. */
207 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].enmClock = TMCLOCK_VIRTUAL;
208 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].u64Expire = INT64_MAX;
209 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].enmClock = TMCLOCK_VIRTUAL_SYNC;
210 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].u64Expire = INT64_MAX;
211 pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].enmClock = TMCLOCK_REAL;
212 pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].u64Expire = INT64_MAX;
213 pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].enmClock = TMCLOCK_TSC;
214 pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].u64Expire = INT64_MAX;
215
216
217 /*
218 * We directly use the GIP to calculate the virtual time. We map the
219 * the GIP into the guest context so we can do this calculation there
220 * as well and save costly world switches.
221 */
222 pVM->tm.s.pvGIPR3 = (void *)g_pSUPGlobalInfoPage;
223 AssertMsgReturn(pVM->tm.s.pvGIPR3, ("GIP support is now required!\n"), VERR_INTERNAL_ERROR);
224 RTHCPHYS HCPhysGIP;
225 rc = SUPR3GipGetPhys(&HCPhysGIP);
226 AssertMsgRCReturn(rc, ("Failed to get GIP physical address!\n"), rc);
227
228 RTGCPTR GCPtr;
229 rc = MMR3HyperMapHCPhys(pVM, pVM->tm.s.pvGIPR3, NIL_RTR0PTR, HCPhysGIP, PAGE_SIZE, "GIP", &GCPtr);
230 if (RT_FAILURE(rc))
231 {
232 AssertMsgFailed(("Failed to map GIP into GC, rc=%Rrc!\n", rc));
233 return rc;
234 }
235 pVM->tm.s.pvGIPRC = GCPtr;
236 LogFlow(("TMR3Init: HCPhysGIP=%RHp at %RRv\n", HCPhysGIP, pVM->tm.s.pvGIPRC));
237 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
238
239 /* Check assumptions made in TMAllVirtual.cpp about the GIP update interval. */
240 if ( g_pSUPGlobalInfoPage->u32Magic == SUPGLOBALINFOPAGE_MAGIC
241 && g_pSUPGlobalInfoPage->u32UpdateIntervalNS >= 250000000 /* 0.25s */)
242 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
243 N_("The GIP update interval is too big. u32UpdateIntervalNS=%RU32 (u32UpdateHz=%RU32)"),
244 g_pSUPGlobalInfoPage->u32UpdateIntervalNS, g_pSUPGlobalInfoPage->u32UpdateHz);
245 LogRel(("TM: GIP - u32Mode=%d (%s) u32UpdateHz=%u\n", g_pSUPGlobalInfoPage->u32Mode,
246 g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC ? "SyncTSC"
247 : g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_ASYNC_TSC ? "AsyncTSC" : "Unknown",
248 g_pSUPGlobalInfoPage->u32UpdateHz));
249
250 /*
251 * Setup the VirtualGetRaw backend.
252 */
253 pVM->tm.s.VirtualGetRawDataR3.pu64Prev = &pVM->tm.s.u64VirtualRawPrev;
254 pVM->tm.s.VirtualGetRawDataR3.pfnBad = tmVirtualNanoTSBad;
255 pVM->tm.s.VirtualGetRawDataR3.pfnRediscover = tmVirtualNanoTSRediscover;
256 if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
257 {
258 if (g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC)
259 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLFenceSync;
260 else
261 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLFenceAsync;
262 }
263 else
264 {
265 if (g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC)
266 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLegacySync;
267 else
268 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLegacyAsync;
269 }
270
271 pVM->tm.s.VirtualGetRawDataRC.pu64Prev = MMHyperR3ToRC(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
272 pVM->tm.s.VirtualGetRawDataR0.pu64Prev = MMHyperR3ToR0(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
273 AssertReturn(pVM->tm.s.VirtualGetRawDataR0.pu64Prev, VERR_INTERNAL_ERROR);
274 /* The rest is done in TMR3InitFinalize since it's too early to call PDM. */
275
276 /*
277 * Init the locks.
278 */
279 rc = PDMR3CritSectInit(pVM, &pVM->tm.s.TimerCritSect, RT_SRC_POS, "TM Timer Lock");
280 if (RT_FAILURE(rc))
281 return rc;
282 rc = PDMR3CritSectInit(pVM, &pVM->tm.s.VirtualSyncLock, RT_SRC_POS, "TM VirtualSync Lock");
283 if (RT_FAILURE(rc))
284 return rc;
285
286 /*
287 * Get our CFGM node, create it if necessary.
288 */
289 PCFGMNODE pCfgHandle = CFGMR3GetChild(CFGMR3GetRoot(pVM), "TM");
290 if (!pCfgHandle)
291 {
292 rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "TM", &pCfgHandle);
293 AssertRCReturn(rc, rc);
294 }
295
296 /*
297 * Determin the TSC configuration and frequency.
298 */
299 /* mode */
300 /** @cfgm{/TM/TSCVirtualized,bool,true}
301 * Use a virtualize TSC, i.e. trap all TSC access. */
302 rc = CFGMR3QueryBool(pCfgHandle, "TSCVirtualized", &pVM->tm.s.fTSCVirtualized);
303 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
304 pVM->tm.s.fTSCVirtualized = true; /* trap rdtsc */
305 else if (RT_FAILURE(rc))
306 return VMSetError(pVM, rc, RT_SRC_POS,
307 N_("Configuration error: Failed to querying bool value \"UseRealTSC\""));
308
309 /* source */
310 /** @cfgm{/TM/UseRealTSC,bool,false}
311 * Use the real TSC as time source for the TSC instead of the synchronous
312 * virtual clock (false, default). */
313 rc = CFGMR3QueryBool(pCfgHandle, "UseRealTSC", &pVM->tm.s.fTSCUseRealTSC);
314 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
315 pVM->tm.s.fTSCUseRealTSC = false; /* use virtual time */
316 else if (RT_FAILURE(rc))
317 return VMSetError(pVM, rc, RT_SRC_POS,
318 N_("Configuration error: Failed to querying bool value \"UseRealTSC\""));
319 if (!pVM->tm.s.fTSCUseRealTSC)
320 pVM->tm.s.fTSCVirtualized = true;
321
322 /* TSC reliability */
323 /** @cfgm{/TM/MaybeUseOffsettedHostTSC,bool,detect}
324 * Whether the CPU has a fixed TSC rate and may be used in offsetted mode with
325 * VT-x/AMD-V execution. This is autodetected in a very restrictive way by
326 * default. */
327 rc = CFGMR3QueryBool(pCfgHandle, "MaybeUseOffsettedHostTSC", &pVM->tm.s.fMaybeUseOffsettedHostTSC);
328 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
329 {
330 if (!pVM->tm.s.fTSCUseRealTSC)
331 pVM->tm.s.fMaybeUseOffsettedHostTSC = tmR3HasFixedTSC(pVM);
332 else
333 pVM->tm.s.fMaybeUseOffsettedHostTSC = true;
334 }
335
336 /** @cfgm{TM/TSCTicksPerSecond, uint32_t, Current TSC frequency from GIP}
337 * The number of TSC ticks per second (i.e. the TSC frequency). This will
338 * override TSCUseRealTSC, TSCVirtualized and MaybeUseOffsettedHostTSC.
339 */
340 rc = CFGMR3QueryU64(pCfgHandle, "TSCTicksPerSecond", &pVM->tm.s.cTSCTicksPerSecond);
341 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
342 {
343 pVM->tm.s.cTSCTicksPerSecond = tmR3CalibrateTSC(pVM);
344 if ( !pVM->tm.s.fTSCUseRealTSC
345 && pVM->tm.s.cTSCTicksPerSecond >= _4G)
346 {
347 pVM->tm.s.cTSCTicksPerSecond = _4G - 1; /* (A limitation of our math code) */
348 pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
349 }
350 }
351 else if (RT_FAILURE(rc))
352 return VMSetError(pVM, rc, RT_SRC_POS,
353 N_("Configuration error: Failed to querying uint64_t value \"TSCTicksPerSecond\""));
354 else if ( pVM->tm.s.cTSCTicksPerSecond < _1M
355 || pVM->tm.s.cTSCTicksPerSecond >= _4G)
356 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
357 N_("Configuration error: \"TSCTicksPerSecond\" = %RI64 is not in the range 1MHz..4GHz-1"),
358 pVM->tm.s.cTSCTicksPerSecond);
359 else
360 {
361 pVM->tm.s.fTSCUseRealTSC = pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
362 pVM->tm.s.fTSCVirtualized = true;
363 }
364
365 /** @cfgm{TM/TSCTiedToExecution, bool, false}
366 * Whether the TSC should be tied to execution. This will exclude most of the
367 * virtualization overhead, but will by default include the time spent in the
368 * halt state (see TM/TSCNotTiedToHalt). This setting will override all other
369 * TSC settings except for TSCTicksPerSecond and TSCNotTiedToHalt, which should
370 * be used avoided or used with great care. Note that this will only work right
371 * together with VT-x or AMD-V, and with a single virtual CPU. */
372 rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCTiedToExecution", &pVM->tm.s.fTSCTiedToExecution, false);
373 if (RT_FAILURE(rc))
374 return VMSetError(pVM, rc, RT_SRC_POS,
375 N_("Configuration error: Failed to querying bool value \"TSCTiedToExecution\""));
376 if (pVM->tm.s.fTSCTiedToExecution)
377 {
378 /* tied to execution, override all other settings. */
379 pVM->tm.s.fTSCVirtualized = true;
380 pVM->tm.s.fTSCUseRealTSC = true;
381 pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
382 }
383
384 /** @cfgm{TM/TSCNotTiedToHalt, bool, true}
385 * For overriding the default of TM/TSCTiedToExecution, i.e. set this to false
386 * to make the TSC freeze during HLT. */
387 rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCNotTiedToHalt", &pVM->tm.s.fTSCNotTiedToHalt, false);
388 if (RT_FAILURE(rc))
389 return VMSetError(pVM, rc, RT_SRC_POS,
390 N_("Configuration error: Failed to querying bool value \"TSCNotTiedToHalt\""));
391
392 /* setup and report */
393 if (pVM->tm.s.fTSCVirtualized)
394 CPUMR3SetCR4Feature(pVM, X86_CR4_TSD, ~X86_CR4_TSD);
395 else
396 CPUMR3SetCR4Feature(pVM, 0, ~X86_CR4_TSD);
397 LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%'RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool\n"
398 "TM: fMaybeUseOffsettedHostTSC=%RTbool TSCTiedToExecution=%RTbool TSCNotTiedToHalt=%RTbool\n",
399 pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC,
400 pVM->tm.s.fMaybeUseOffsettedHostTSC, pVM->tm.s.fTSCTiedToExecution, pVM->tm.s.fTSCNotTiedToHalt));
401
402 /*
403 * Configure the timer synchronous virtual time.
404 */
405 /** @cfgm{TM/ScheduleSlack, uint32_t, ns, 0, UINT32_MAX, 100000}
406 * Scheduling slack when processing timers. */
407 rc = CFGMR3QueryU32(pCfgHandle, "ScheduleSlack", &pVM->tm.s.u32VirtualSyncScheduleSlack);
408 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
409 pVM->tm.s.u32VirtualSyncScheduleSlack = 100000; /* 0.100ms (ASSUMES virtual time is nanoseconds) */
410 else if (RT_FAILURE(rc))
411 return VMSetError(pVM, rc, RT_SRC_POS,
412 N_("Configuration error: Failed to querying 32-bit integer value \"ScheduleSlack\""));
413
414 /** @cfgm{TM/CatchUpStopThreshold, uint64_t, ns, 0, UINT64_MAX, 500000}
415 * When to stop a catch-up, considering it successful. */
416 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStopThreshold", &pVM->tm.s.u64VirtualSyncCatchUpStopThreshold);
417 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
418 pVM->tm.s.u64VirtualSyncCatchUpStopThreshold = 500000; /* 0.5ms */
419 else if (RT_FAILURE(rc))
420 return VMSetError(pVM, rc, RT_SRC_POS,
421 N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpStopThreshold\""));
422
423 /** @cfgm{TM/CatchUpGiveUpThreshold, uint64_t, ns, 0, UINT64_MAX, 60000000000}
424 * When to give up a catch-up attempt. */
425 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpGiveUpThreshold", &pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold);
426 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
427 pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold = UINT64_C(60000000000); /* 60 sec */
428 else if (RT_FAILURE(rc))
429 return VMSetError(pVM, rc, RT_SRC_POS,
430 N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpGiveUpThreshold\""));
431
432
433 /** @cfgm{TM/CatchUpPrecentage[0..9], uint32_t, %, 1, 2000, various}
434 * The catch-up percent for a given period. */
435 /** @cfgm{TM/CatchUpStartThreshold[0..9], uint64_t, ns, 0, UINT64_MAX,
436 * The catch-up period threshold, or if you like, when a period starts. */
437#define TM_CFG_PERIOD(iPeriod, DefStart, DefPct) \
438 do \
439 { \
440 uint64_t u64; \
441 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStartThreshold" #iPeriod, &u64); \
442 if (rc == VERR_CFGM_VALUE_NOT_FOUND) \
443 u64 = UINT64_C(DefStart); \
444 else if (RT_FAILURE(rc)) \
445 return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpThreshold" #iPeriod "\"")); \
446 if ( (iPeriod > 0 && u64 <= pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod - 1].u64Start) \
447 || u64 >= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold) \
448 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Configuration error: Invalid start of period #" #iPeriod ": %'RU64"), u64); \
449 pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u64Start = u64; \
450 rc = CFGMR3QueryU32(pCfgHandle, "CatchUpPrecentage" #iPeriod, &pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage); \
451 if (rc == VERR_CFGM_VALUE_NOT_FOUND) \
452 pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage = (DefPct); \
453 else if (RT_FAILURE(rc)) \
454 return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 32-bit integer value \"CatchUpPrecentage" #iPeriod "\"")); \
455 } while (0)
456 /* This needs more tuning. Not sure if we really need so many period and be so gentle. */
457 TM_CFG_PERIOD(0, 750000, 5); /* 0.75ms at 1.05x */
458 TM_CFG_PERIOD(1, 1500000, 10); /* 1.50ms at 1.10x */
459 TM_CFG_PERIOD(2, 8000000, 25); /* 8ms at 1.25x */
460 TM_CFG_PERIOD(3, 30000000, 50); /* 30ms at 1.50x */
461 TM_CFG_PERIOD(4, 75000000, 75); /* 75ms at 1.75x */
462 TM_CFG_PERIOD(5, 175000000, 100); /* 175ms at 2x */
463 TM_CFG_PERIOD(6, 500000000, 200); /* 500ms at 3x */
464 TM_CFG_PERIOD(7, 3000000000, 300); /* 3s at 4x */
465 TM_CFG_PERIOD(8,30000000000, 400); /* 30s at 5x */
466 TM_CFG_PERIOD(9,55000000000, 500); /* 55s at 6x */
467 AssertCompile(RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods) == 10);
468#undef TM_CFG_PERIOD
469
470 /*
471 * Configure real world time (UTC).
472 */
473 /** @cfgm{TM/UTCOffset, int64_t, ns, INT64_MIN, INT64_MAX, 0}
474 * The UTC offset. This is used to put the guest back or forwards in time. */
475 rc = CFGMR3QueryS64(pCfgHandle, "UTCOffset", &pVM->tm.s.offUTC);
476 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
477 pVM->tm.s.offUTC = 0; /* ns */
478 else if (RT_FAILURE(rc))
479 return VMSetError(pVM, rc, RT_SRC_POS,
480 N_("Configuration error: Failed to querying 64-bit integer value \"UTCOffset\""));
481
482 /*
483 * Setup the warp drive.
484 */
485 /** @cfgm{TM/WarpDrivePercentage, uint32_t, %, 0, 20000, 100}
486 * The warp drive percentage, 100% is normal speed. This is used to speed up
487 * or slow down the virtual clock, which can be useful for fast forwarding
488 * borring periods during tests. */
489 rc = CFGMR3QueryU32(pCfgHandle, "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage);
490 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
491 rc = CFGMR3QueryU32(CFGMR3GetRoot(pVM), "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage); /* legacy */
492 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
493 pVM->tm.s.u32VirtualWarpDrivePercentage = 100;
494 else if (RT_FAILURE(rc))
495 return VMSetError(pVM, rc, RT_SRC_POS,
496 N_("Configuration error: Failed to querying uint32_t value \"WarpDrivePercent\""));
497 else if ( pVM->tm.s.u32VirtualWarpDrivePercentage < 2
498 || pVM->tm.s.u32VirtualWarpDrivePercentage > 20000)
499 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
500 N_("Configuration error: \"WarpDrivePercent\" = %RI32 is not in the range 2..20000"),
501 pVM->tm.s.u32VirtualWarpDrivePercentage);
502 pVM->tm.s.fVirtualWarpDrive = pVM->tm.s.u32VirtualWarpDrivePercentage != 100;
503 if (pVM->tm.s.fVirtualWarpDrive)
504 LogRel(("TM: u32VirtualWarpDrivePercentage=%RI32\n", pVM->tm.s.u32VirtualWarpDrivePercentage));
505
506 /*
507 * Start the timer (guard against REM not yielding).
508 */
509 /** @cfgm{TM/TimerMillies, uint32_t, ms, 1, 1000, 10}
510 * The watchdog timer interval. */
511 uint32_t u32Millies;
512 rc = CFGMR3QueryU32(pCfgHandle, "TimerMillies", &u32Millies);
513 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
514 u32Millies = 10;
515 else if (RT_FAILURE(rc))
516 return VMSetError(pVM, rc, RT_SRC_POS,
517 N_("Configuration error: Failed to query uint32_t value \"TimerMillies\""));
518 rc = RTTimerCreate(&pVM->tm.s.pTimer, u32Millies, tmR3TimerCallback, pVM);
519 if (RT_FAILURE(rc))
520 {
521 AssertMsgFailed(("Failed to create timer, u32Millies=%d rc=%Rrc.\n", u32Millies, rc));
522 return rc;
523 }
524 Log(("TM: Created timer %p firing every %d millieseconds\n", pVM->tm.s.pTimer, u32Millies));
525 pVM->tm.s.u32TimerMillies = u32Millies;
526
527 /*
528 * Register saved state.
529 */
530 rc = SSMR3RegisterInternal(pVM, "tm", 1, TM_SAVED_STATE_VERSION, sizeof(uint64_t) * 8,
531 NULL, NULL, NULL,
532 NULL, tmR3Save, NULL,
533 NULL, tmR3Load, NULL);
534 if (RT_FAILURE(rc))
535 return rc;
536
537 /*
538 * Register statistics.
539 */
540 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).");
541 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).");
542 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).");
543 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).");
544 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataRC.c1nsSteps,STAMTYPE_U32, "/TM/RC/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
545 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataRC.cBadPrev, STAMTYPE_U32, "/TM/RC/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
546 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)");
547 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.");
548
549#ifdef VBOX_WITH_STATISTICS
550 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).");
551 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR3.cUpdateRaces,STAMTYPE_U32, "/TM/R3/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
552 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).");
553 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR0.cUpdateRaces,STAMTYPE_U32, "/TM/R0/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
554 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataRC.cExpired, STAMTYPE_U32, "/TM/RC/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
555 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataRC.cUpdateRaces,STAMTYPE_U32, "/TM/RC/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
556 STAM_REG(pVM, &pVM->tm.s.StatDoQueues, STAMTYPE_PROFILE, "/TM/DoQueues", STAMUNIT_TICKS_PER_CALL, "Profiling timer TMR3TimerQueuesDo.");
557 STAM_REG(pVM, &pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL], STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Virtual", STAMUNIT_TICKS_PER_CALL, "Time spent on the virtual clock queue.");
558 STAM_REG(pVM, &pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL_SYNC], STAMTYPE_PROFILE_ADV, "/TM/DoQueues/VirtualSync", STAMUNIT_TICKS_PER_CALL, "Time spent on the virtual sync clock queue.");
559 STAM_REG(pVM, &pVM->tm.s.aStatDoQueues[TMCLOCK_REAL], STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Real", STAMUNIT_TICKS_PER_CALL, "Time spent on the real clock queue.");
560
561 STAM_REG(pVM, &pVM->tm.s.StatPoll, STAMTYPE_COUNTER, "/TM/Poll", STAMUNIT_OCCURENCES, "TMTimerPoll calls.");
562 STAM_REG(pVM, &pVM->tm.s.StatPollAlreadySet, STAMTYPE_COUNTER, "/TM/Poll/AlreadySet", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the FF was already set.");
563 STAM_REG(pVM, &pVM->tm.s.StatPollELoop, STAMTYPE_COUNTER, "/TM/Poll/ELoop", STAMUNIT_OCCURENCES, "Times TMTimerPoll has given up getting a consistent virtual sync data set.");
564 STAM_REG(pVM, &pVM->tm.s.StatPollMiss, STAMTYPE_COUNTER, "/TM/Poll/Miss", STAMUNIT_OCCURENCES, "TMTimerPoll calls where nothing had expired.");
565 STAM_REG(pVM, &pVM->tm.s.StatPollRunning, STAMTYPE_COUNTER, "/TM/Poll/Running", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the queues were being run.");
566 STAM_REG(pVM, &pVM->tm.s.StatPollSimple, STAMTYPE_COUNTER, "/TM/Poll/Simple", STAMUNIT_OCCURENCES, "TMTimerPoll calls where we could take the simple path.");
567 STAM_REG(pVM, &pVM->tm.s.StatPollVirtual, STAMTYPE_COUNTER, "/TM/Poll/HitsVirtual", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL queue.");
568 STAM_REG(pVM, &pVM->tm.s.StatPollVirtualSync, STAMTYPE_COUNTER, "/TM/Poll/HitsVirtualSync", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL_SYNC queue.");
569
570 STAM_REG(pVM, &pVM->tm.s.StatPostponedR3, STAMTYPE_COUNTER, "/TM/PostponedR3", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-3.");
571 STAM_REG(pVM, &pVM->tm.s.StatPostponedRZ, STAMTYPE_COUNTER, "/TM/PostponedRZ", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-0 / RC.");
572
573 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.");
574 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.");
575 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.");
576
577 STAM_REG(pVM, &pVM->tm.s.StatTimerSet, STAMTYPE_COUNTER, "/TM/TimerSet", STAMUNIT_OCCURENCES, "Calls");
578 STAM_REG(pVM, &pVM->tm.s.StatTimerSetOpt, STAMTYPE_COUNTER, "/TM/TimerSet/Opt", STAMUNIT_OCCURENCES, "Optimized path taken.");
579 STAM_REG(pVM, &pVM->tm.s.StatTimerSetR3, STAMTYPE_PROFILE, "/TM/TimerSet/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-3.");
580 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRZ, STAMTYPE_PROFILE, "/TM/TimerSet/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-0 / RC.");
581 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStActive, STAMTYPE_COUNTER, "/TM/TimerSet/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
582 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSet/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
583 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStOther, STAMTYPE_COUNTER, "/TM/TimerSet/StOther", STAMUNIT_OCCURENCES, "Other states");
584 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendStop, STAMTYPE_COUNTER, "/TM/TimerSet/StPendStop", STAMUNIT_OCCURENCES, "PENDING_STOP");
585 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendStopSched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendStopSched", STAMUNIT_OCCURENCES, "PENDING_STOP_SCHEDULE");
586 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendSched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendSched", STAMUNIT_OCCURENCES, "PENDING_SCHEDULE");
587 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendResched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendResched", STAMUNIT_OCCURENCES, "PENDING_RESCHEDULE");
588 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStStopped, STAMTYPE_COUNTER, "/TM/TimerSet/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
589
590 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelative, STAMTYPE_COUNTER, "/TM/TimerSetRelative", STAMUNIT_OCCURENCES, "Calls");
591 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeOpt, STAMTYPE_COUNTER, "/TM/TimerSetRelative/Opt", STAMUNIT_OCCURENCES, "Optimized path taken.");
592 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeR3, STAMTYPE_PROFILE, "/TM/TimerSetRelative/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetRelative calls made in ring-3.");
593 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeRZ, STAMTYPE_PROFILE, "/TM/TimerSetRelative/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetReltaive calls made in ring-0 / RC.");
594 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeRacyVirtSync, STAMTYPE_COUNTER, "/TM/TimerSetRelative/RacyVirtSync", STAMUNIT_OCCURENCES, "Potentially racy virtual sync timer update.");
595 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStActive, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
596 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
597 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStOther, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StOther", STAMUNIT_OCCURENCES, "Other states");
598 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendStop, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendStop", STAMUNIT_OCCURENCES, "PENDING_STOP");
599 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendStopSched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendStopSched",STAMUNIT_OCCURENCES, "PENDING_STOP_SCHEDULE");
600 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendSched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendSched", STAMUNIT_OCCURENCES, "PENDING_SCHEDULE");
601 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendResched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendResched", STAMUNIT_OCCURENCES, "PENDING_RESCHEDULE");
602 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStStopped, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
603
604 STAM_REG(pVM, &pVM->tm.s.StatTimerStopR3, STAMTYPE_PROFILE, "/TM/TimerStopR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-3.");
605 STAM_REG(pVM, &pVM->tm.s.StatTimerStopRZ, STAMTYPE_PROFILE, "/TM/TimerStopRZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-0 / RC.");
606
607 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.");
608 STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSetFF, STAMTYPE_COUNTER, "/TM/VirtualGetSetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling TMTimerGet.");
609 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGet, STAMTYPE_COUNTER, "/TM/VirtualSyncGet", STAMUNIT_OCCURENCES, "The number of times tmVirtualSyncGetEx was called.");
610 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetELoop, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/ELoop", STAMUNIT_OCCURENCES, "Times tmVirtualSyncGetEx has given up getting a consistent virtual sync data set.");
611 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetExpired, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Expired", STAMUNIT_OCCURENCES, "Times tmVirtualSyncGetEx encountered an expired timer stopping the clock.");
612 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetLocked, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Locked", STAMUNIT_OCCURENCES, "Times we successfully acquired the lock in tmVirtualSyncGetEx.");
613 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetLockless, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Lockless", STAMUNIT_OCCURENCES, "Times tmVirtualSyncGetEx returned without needing to take the lock.");
614 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetSetFF, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/SetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling tmVirtualSyncGetEx.");
615 STAM_REG(pVM, &pVM->tm.s.StatVirtualPause, STAMTYPE_COUNTER, "/TM/VirtualPause", STAMUNIT_OCCURENCES, "The number of times TMR3TimerPause was called.");
616 STAM_REG(pVM, &pVM->tm.s.StatVirtualResume, STAMTYPE_COUNTER, "/TM/VirtualResume", STAMUNIT_OCCURENCES, "The number of times TMR3TimerResume was called.");
617
618 STAM_REG(pVM, &pVM->tm.s.StatTimerCallbackSetFF, STAMTYPE_COUNTER, "/TM/CallbackSetFF", STAMUNIT_OCCURENCES, "The number of times the timer callback set FF.");
619
620 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE010, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE010", STAMUNIT_OCCURENCES, "In catch-up mode, 10% or lower.");
621 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE025, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE025", STAMUNIT_OCCURENCES, "In catch-up mode, 25%-11%.");
622 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE100, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE100", STAMUNIT_OCCURENCES, "In catch-up mode, 100%-26%.");
623 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupOther, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupOther", STAMUNIT_OCCURENCES, "In catch-up mode, > 100%.");
624 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.");
625 STAM_REG(pVM, &pVM->tm.s.StatTSCNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/NotTicking", STAMUNIT_OCCURENCES, "TSC is not ticking.");
626 STAM_REG(pVM, &pVM->tm.s.StatTSCSyncNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/SyncNotTicking", STAMUNIT_OCCURENCES, "VirtualSync isn't ticking.");
627 STAM_REG(pVM, &pVM->tm.s.StatTSCWarp, STAMTYPE_COUNTER, "/TM/TSC/Intercept/Warp", STAMUNIT_OCCURENCES, "Warpdrive is active.");
628 STAM_REG(pVM, &pVM->tm.s.StatTSCSet, STAMTYPE_COUNTER, "/TM/TSC/Sets", STAMUNIT_OCCURENCES, "Calls to TMCpuTickSet.");
629 STAM_REG(pVM, &pVM->tm.s.StatTSCUnderflow, STAMTYPE_COUNTER, "/TM/TSC/Underflow", STAMUNIT_OCCURENCES, "TSC underflow; corrected with last seen value .");
630#endif /* VBOX_WITH_STATISTICS */
631
632 for (VMCPUID i = 0; i < pVM->cCpus; i++)
633 {
634 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.offTSCRawSrc, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS, "TSC offset relative the raw source", "/TM/TSC/offCPU%u", i);
635#ifndef VBOX_WITHOUT_NS_ACCOUNTING
636 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cNsTotal, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Total CPU run time.", "/TM/CPU/%02u", i);
637 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cNsExecuting, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent executing guest code.", "/TM/CPU/%02u/NsExecuting", i);
638 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cNsHalted, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent halted.", "/TM/CPU/%02u/NsHalted", i);
639 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cNsOther, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent in the VMM or preempted.", "/TM/CPU/%02u/NsOther", i);
640 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.CpuLoad.cPctExecuting, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent executing guest code recently.", "/TM/CPU/%02u/PctExecuting", i);
641 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.CpuLoad.cPctHalted, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent halted recently.", "/TM/CPU/%02u/PctHalted", i);
642 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.CpuLoad.cPctOther, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent in the VMM or preempted recently.", "/TM/CPU/%02u/PctOther", i);
643#endif
644 }
645#ifndef VBOX_WITHOUT_NS_ACCOUNTING
646 STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctExecuting, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent executing guest code recently.", "/TM/CPU/PctExecuting");
647 STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctHalted, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent halted recently.", "/TM/CPU/PctHalted");
648 STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctOther, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent in the VMM or preempted recently.", "/TM/CPU/PctOther");
649#endif
650
651#ifdef VBOX_WITH_STATISTICS
652 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.");
653 STAM_REG(pVM, (void *)&pVM->tm.s.fVirtualSyncCatchUp, STAMTYPE_U8, "/TM/VirtualSync/CatchUpActive", STAMUNIT_NONE, "Catch-Up active indicator.");
654 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)");
655 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncFF, STAMTYPE_PROFILE, "/TM/VirtualSync/FF", STAMUNIT_TICKS_PER_OCCURENCE, "Time spent in TMR3VirtualSyncFF by all but the dedicate timer EMT.");
656 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGiveUp, STAMTYPE_COUNTER, "/TM/VirtualSync/GiveUp", STAMUNIT_OCCURENCES, "Times the catch-up was abandoned.");
657 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++.)");
658 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRun, STAMTYPE_COUNTER, "/TM/VirtualSync/Run", STAMUNIT_OCCURENCES, "Times the virtual sync timer queue was considered.");
659 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunRestart, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/Restarts", STAMUNIT_OCCURENCES, "Times the clock was restarted after a run.");
660 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.");
661 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunStoppedAlready, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/StoppedAlready", STAMUNIT_OCCURENCES, "Times the clock was already stopped elsewhere (TMVirtualSyncGet).");
662 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.)");
663 for (unsigned i = 0; i < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods); i++)
664 {
665 STAMR3RegisterF(pVM, &pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "The catch-up percentage.", "/TM/VirtualSync/Periods/%u", i);
666 STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupAdjust[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times adjusted to this period.", "/TM/VirtualSync/Periods/%u/Adjust", i);
667 STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupInitial[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times started in this period.", "/TM/VirtualSync/Periods/%u/Initial", i);
668 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);
669 }
670#endif /* VBOX_WITH_STATISTICS */
671
672 /*
673 * Register info handlers.
674 */
675 DBGFR3InfoRegisterInternalEx(pVM, "timers", "Dumps all timers. No arguments.", tmR3TimerInfo, DBGFINFO_FLAGS_RUN_ON_EMT);
676 DBGFR3InfoRegisterInternalEx(pVM, "activetimers", "Dumps active all timers. No arguments.", tmR3TimerInfoActive, DBGFINFO_FLAGS_RUN_ON_EMT);
677 DBGFR3InfoRegisterInternalEx(pVM, "clocks", "Display the time of the various clocks.", tmR3InfoClocks, DBGFINFO_FLAGS_RUN_ON_EMT);
678
679 return VINF_SUCCESS;
680}
681
682
683/**
684 * Initializes the per-VCPU TM.
685 *
686 * @returns VBox status code.
687 * @param pVM The VM to operate on.
688 */
689VMM_INT_DECL(int) TMR3InitCPU(PVM pVM)
690{
691 LogFlow(("TMR3InitCPU\n"));
692 return VINF_SUCCESS;
693}
694
695
696/**
697 * Checks if the host CPU has a fixed TSC frequency.
698 *
699 * @returns true if it has, false if it hasn't.
700 *
701 * @remark This test doesn't bother with very old CPUs that don't do power
702 * management or any other stuff that might influence the TSC rate.
703 * This isn't currently relevant.
704 */
705static bool tmR3HasFixedTSC(PVM pVM)
706{
707 if (ASMHasCpuId())
708 {
709 uint32_t uEAX, uEBX, uECX, uEDX;
710
711 if (CPUMGetHostCpuVendor(pVM) == CPUMCPUVENDOR_AMD)
712 {
713 /*
714 * AuthenticAMD - Check for APM support and that TscInvariant is set.
715 *
716 * This test isn't correct with respect to fixed/non-fixed TSC and
717 * older models, but this isn't relevant since the result is currently
718 * only used for making a descision on AMD-V models.
719 */
720 ASMCpuId(0x80000000, &uEAX, &uEBX, &uECX, &uEDX);
721 if (uEAX >= 0x80000007)
722 {
723 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
724
725 ASMCpuId(0x80000007, &uEAX, &uEBX, &uECX, &uEDX);
726 if ( (uEDX & X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR) /* TscInvariant */
727 && pGip->u32Mode == SUPGIPMODE_SYNC_TSC /* no fixed tsc if the gip timer is in async mode */)
728 return true;
729 }
730 }
731 else if (CPUMGetHostCpuVendor(pVM) == CPUMCPUVENDOR_INTEL)
732 {
733 /*
734 * GenuineIntel - Check the model number.
735 *
736 * This test is lacking in the same way and for the same reasons
737 * as the AMD test above.
738 */
739 ASMCpuId(1, &uEAX, &uEBX, &uECX, &uEDX);
740 unsigned uModel = (uEAX >> 4) & 0x0f;
741 unsigned uFamily = (uEAX >> 8) & 0x0f;
742 if (uFamily == 0x0f)
743 uFamily += (uEAX >> 20) & 0xff;
744 if (uFamily >= 0x06)
745 uModel += ((uEAX >> 16) & 0x0f) << 4;
746 if ( (uFamily == 0x0f /*P4*/ && uModel >= 0x03)
747 || (uFamily == 0x06 /*P2/P3*/ && uModel >= 0x0e))
748 return true;
749 }
750 }
751 return false;
752}
753
754
755/**
756 * Calibrate the CPU tick.
757 *
758 * @returns Number of ticks per second.
759 */
760static uint64_t tmR3CalibrateTSC(PVM pVM)
761{
762 /*
763 * Use GIP when available present.
764 */
765 uint64_t u64Hz;
766 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
767 if ( pGip
768 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC)
769 {
770 unsigned iCpu = pGip->u32Mode != SUPGIPMODE_ASYNC_TSC ? 0 : ASMGetApicId();
771 if (iCpu >= RT_ELEMENTS(pGip->aCPUs))
772 AssertReleaseMsgFailed(("iCpu=%d - the ApicId is too high. send VBox.log and hardware specs!\n", iCpu));
773 else
774 {
775 if (tmR3HasFixedTSC(pVM))
776 /* Sleep a bit to get a more reliable CpuHz value. */
777 RTThreadSleep(32);
778 else
779 {
780 /* Spin for 40ms to try push up the CPU frequency and get a more reliable CpuHz value. */
781 const uint64_t u64 = RTTimeMilliTS();
782 while ((RTTimeMilliTS() - u64) < 40 /*ms*/)
783 /* nothing */;
784 }
785
786 pGip = g_pSUPGlobalInfoPage;
787 if ( pGip
788 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
789 && (u64Hz = pGip->aCPUs[iCpu].u64CpuHz)
790 && u64Hz != ~(uint64_t)0)
791 return u64Hz;
792 }
793 }
794
795 /* call this once first to make sure it's initialized. */
796 RTTimeNanoTS();
797
798 /*
799 * Yield the CPU to increase our chances of getting
800 * a correct value.
801 */
802 RTThreadYield(); /* Try avoid interruptions between TSC and NanoTS samplings. */
803 static const unsigned s_auSleep[5] = { 50, 30, 30, 40, 40 };
804 uint64_t au64Samples[5];
805 unsigned i;
806 for (i = 0; i < RT_ELEMENTS(au64Samples); i++)
807 {
808 RTMSINTERVAL cMillies;
809 int cTries = 5;
810 uint64_t u64Start = ASMReadTSC();
811 uint64_t u64End;
812 uint64_t StartTS = RTTimeNanoTS();
813 uint64_t EndTS;
814 do
815 {
816 RTThreadSleep(s_auSleep[i]);
817 u64End = ASMReadTSC();
818 EndTS = RTTimeNanoTS();
819 cMillies = (RTMSINTERVAL)((EndTS - StartTS + 500000) / 1000000);
820 } while ( cMillies == 0 /* the sleep may be interrupted... */
821 || (cMillies < 20 && --cTries > 0));
822 uint64_t u64Diff = u64End - u64Start;
823
824 au64Samples[i] = (u64Diff * 1000) / cMillies;
825 AssertMsg(cTries > 0, ("cMillies=%d i=%d\n", cMillies, i));
826 }
827
828 /*
829 * Discard the highest and lowest results and calculate the average.
830 */
831 unsigned iHigh = 0;
832 unsigned iLow = 0;
833 for (i = 1; i < RT_ELEMENTS(au64Samples); i++)
834 {
835 if (au64Samples[i] < au64Samples[iLow])
836 iLow = i;
837 if (au64Samples[i] > au64Samples[iHigh])
838 iHigh = i;
839 }
840 au64Samples[iLow] = 0;
841 au64Samples[iHigh] = 0;
842
843 u64Hz = au64Samples[0];
844 for (i = 1; i < RT_ELEMENTS(au64Samples); i++)
845 u64Hz += au64Samples[i];
846 u64Hz /= RT_ELEMENTS(au64Samples) - 2;
847
848 return u64Hz;
849}
850
851
852/**
853 * Finalizes the TM initialization.
854 *
855 * @returns VBox status code.
856 * @param pVM The VM to operate on.
857 */
858VMM_INT_DECL(int) TMR3InitFinalize(PVM pVM)
859{
860 int rc;
861
862 /*
863 * Resolve symbols.
864 */
865 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataRC.pfnBad);
866 AssertRCReturn(rc, rc);
867 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataRC.pfnRediscover);
868 AssertRCReturn(rc, rc);
869 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
870 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawRC);
871 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
872 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawRC);
873 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
874 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawRC);
875 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
876 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawRC);
877 else
878 AssertFatalFailed();
879 AssertRCReturn(rc, rc);
880
881 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataR0.pfnBad);
882 AssertRCReturn(rc, rc);
883 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataR0.pfnRediscover);
884 AssertRCReturn(rc, rc);
885 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
886 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawR0);
887 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
888 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawR0);
889 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
890 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawR0);
891 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
892 rc = PDMR3LdrGetSymbolR0Lazy(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawR0);
893 else
894 AssertFatalFailed();
895 AssertRCReturn(rc, rc);
896
897#ifndef VBOX_WITHOUT_NS_ACCOUNTING
898 /*
899 * Create a timer for freshing the CPU load stats.
900 */
901 PTMTIMER pTimer;
902 rc = TMR3TimerCreateInternal(pVM, TMCLOCK_REAL, tmR3CpuLoadTimer, NULL, "CPU Load Timer", &pTimer);
903 if (RT_SUCCESS(rc))
904 rc = TMTimerSetMillies(pTimer, 1000);
905#endif
906
907 return rc;
908}
909
910
911/**
912 * Applies relocations to data and code managed by this
913 * component. This function will be called at init and
914 * whenever the VMM need to relocate it self inside the GC.
915 *
916 * @param pVM The VM.
917 * @param offDelta Relocation delta relative to old location.
918 */
919VMM_INT_DECL(void) TMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
920{
921 int rc;
922 LogFlow(("TMR3Relocate\n"));
923
924 pVM->tm.s.pvGIPRC = MMHyperR3ToRC(pVM, pVM->tm.s.pvGIPR3);
925 pVM->tm.s.paTimerQueuesRC = MMHyperR3ToRC(pVM, pVM->tm.s.paTimerQueuesR3);
926 pVM->tm.s.paTimerQueuesR0 = MMHyperR3ToR0(pVM, pVM->tm.s.paTimerQueuesR3);
927
928 pVM->tm.s.VirtualGetRawDataRC.pu64Prev = MMHyperR3ToRC(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
929 AssertFatal(pVM->tm.s.VirtualGetRawDataRC.pu64Prev);
930 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataRC.pfnBad);
931 AssertFatalRC(rc);
932 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataRC.pfnRediscover);
933 AssertFatalRC(rc);
934
935 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
936 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawRC);
937 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
938 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawRC);
939 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
940 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawRC);
941 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
942 rc = PDMR3LdrGetSymbolRCLazy(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawRC);
943 else
944 AssertFatalFailed();
945 AssertFatalRC(rc);
946
947 /*
948 * Iterate the timers updating the pVMRC pointers.
949 */
950 for (PTMTIMER pTimer = pVM->tm.s.pCreated; pTimer; pTimer = pTimer->pBigNext)
951 {
952 pTimer->pVMRC = pVM->pVMRC;
953 pTimer->pVMR0 = pVM->pVMR0;
954 }
955}
956
957
958/**
959 * Terminates the TM.
960 *
961 * Termination means cleaning up and freeing all resources,
962 * the VM it self is at this point powered off or suspended.
963 *
964 * @returns VBox status code.
965 * @param pVM The VM to operate on.
966 */
967VMM_INT_DECL(int) TMR3Term(PVM pVM)
968{
969 AssertMsg(pVM->tm.s.offVM, ("bad init order!\n"));
970 if (pVM->tm.s.pTimer)
971 {
972 int rc = RTTimerDestroy(pVM->tm.s.pTimer);
973 AssertRC(rc);
974 pVM->tm.s.pTimer = NULL;
975 }
976
977 return VINF_SUCCESS;
978}
979
980
981/**
982 * Terminates the per-VCPU TM.
983 *
984 * Termination means cleaning up and freeing all resources,
985 * the VM it self is at this point powered off or suspended.
986 *
987 * @returns VBox status code.
988 * @param pVM The VM to operate on.
989 */
990VMM_INT_DECL(int) TMR3TermCPU(PVM pVM)
991{
992 return VINF_SUCCESS;
993}
994
995
996/**
997 * The VM is being reset.
998 *
999 * For the TM component this means that a rescheduling is preformed,
1000 * the FF is cleared and but without running the queues. We'll have to
1001 * check if this makes sense or not, but it seems like a good idea now....
1002 *
1003 * @param pVM VM handle.
1004 */
1005VMM_INT_DECL(void) TMR3Reset(PVM pVM)
1006{
1007 LogFlow(("TMR3Reset:\n"));
1008 VM_ASSERT_EMT(pVM);
1009 tmTimerLock(pVM);
1010
1011 /*
1012 * Abort any pending catch up.
1013 * This isn't perfect...
1014 */
1015 if (pVM->tm.s.fVirtualSyncCatchUp)
1016 {
1017 const uint64_t offVirtualNow = TMVirtualGetNoCheck(pVM);
1018 const uint64_t offVirtualSyncNow = TMVirtualSyncGetNoCheck(pVM);
1019 if (pVM->tm.s.fVirtualSyncCatchUp)
1020 {
1021 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
1022
1023 const uint64_t offOld = pVM->tm.s.offVirtualSyncGivenUp;
1024 const uint64_t offNew = offVirtualNow - offVirtualSyncNow;
1025 Assert(offOld <= offNew);
1026 ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
1027 ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSync, offNew);
1028 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
1029 LogRel(("TM: Aborting catch-up attempt on reset with a %'RU64 ns lag on reset; new total: %'RU64 ns\n", offNew - offOld, offNew));
1030 }
1031 }
1032
1033 /*
1034 * Process the queues.
1035 */
1036 for (int i = 0; i < TMCLOCK_MAX; i++)
1037 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[i]);
1038#ifdef VBOX_STRICT
1039 tmTimerQueuesSanityChecks(pVM, "TMR3Reset");
1040#endif
1041
1042 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
1043 VMCPU_FF_CLEAR(pVCpuDst, VMCPU_FF_TIMER); /** @todo FIXME: this isn't right. */
1044 tmTimerUnlock(pVM);
1045}
1046
1047
1048/**
1049 * Resolve a builtin RC symbol.
1050 * Called by PDM when loading or relocating GC modules.
1051 *
1052 * @returns VBox status
1053 * @param pVM VM Handle.
1054 * @param pszSymbol Symbol to resolve.
1055 * @param pRCPtrValue Where to store the symbol value.
1056 * @remark This has to work before TMR3Relocate() is called.
1057 */
1058VMM_INT_DECL(int) TMR3GetImportRC(PVM pVM, const char *pszSymbol, PRTRCPTR pRCPtrValue)
1059{
1060 if (!strcmp(pszSymbol, "g_pSUPGlobalInfoPage"))
1061 *pRCPtrValue = MMHyperR3ToRC(pVM, &pVM->tm.s.pvGIPRC);
1062 //else if (..)
1063 else
1064 return VERR_SYMBOL_NOT_FOUND;
1065 return VINF_SUCCESS;
1066}
1067
1068
1069/**
1070 * Execute state save operation.
1071 *
1072 * @returns VBox status code.
1073 * @param pVM VM Handle.
1074 * @param pSSM SSM operation handle.
1075 */
1076static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM)
1077{
1078 LogFlow(("tmR3Save:\n"));
1079#ifdef VBOX_STRICT
1080 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1081 {
1082 PVMCPU pVCpu = &pVM->aCpus[i];
1083 Assert(!pVCpu->tm.s.fTSCTicking);
1084 }
1085 Assert(!pVM->tm.s.cVirtualTicking);
1086 Assert(!pVM->tm.s.fVirtualSyncTicking);
1087#endif
1088
1089 /*
1090 * Save the virtual clocks.
1091 */
1092 /* the virtual clock. */
1093 SSMR3PutU64(pSSM, TMCLOCK_FREQ_VIRTUAL);
1094 SSMR3PutU64(pSSM, pVM->tm.s.u64Virtual);
1095
1096 /* the virtual timer synchronous clock. */
1097 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSync);
1098 SSMR3PutU64(pSSM, pVM->tm.s.offVirtualSync);
1099 SSMR3PutU64(pSSM, pVM->tm.s.offVirtualSyncGivenUp);
1100 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSyncCatchUpPrev);
1101 SSMR3PutBool(pSSM, pVM->tm.s.fVirtualSyncCatchUp);
1102
1103 /* real time clock */
1104 SSMR3PutU64(pSSM, TMCLOCK_FREQ_REAL);
1105
1106 /* the cpu tick clock. */
1107 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1108 {
1109 PVMCPU pVCpu = &pVM->aCpus[i];
1110 SSMR3PutU64(pSSM, TMCpuTickGet(pVCpu));
1111 }
1112 return SSMR3PutU64(pSSM, pVM->tm.s.cTSCTicksPerSecond);
1113}
1114
1115
1116/**
1117 * Execute state load operation.
1118 *
1119 * @returns VBox status code.
1120 * @param pVM VM Handle.
1121 * @param pSSM SSM operation handle.
1122 * @param uVersion Data layout version.
1123 * @param uPass The data pass.
1124 */
1125static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1126{
1127 LogFlow(("tmR3Load:\n"));
1128
1129 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1130#ifdef VBOX_STRICT
1131 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1132 {
1133 PVMCPU pVCpu = &pVM->aCpus[i];
1134 Assert(!pVCpu->tm.s.fTSCTicking);
1135 }
1136 Assert(!pVM->tm.s.cVirtualTicking);
1137 Assert(!pVM->tm.s.fVirtualSyncTicking);
1138#endif
1139
1140 /*
1141 * Validate version.
1142 */
1143 if (uVersion != TM_SAVED_STATE_VERSION)
1144 {
1145 AssertMsgFailed(("tmR3Load: Invalid version uVersion=%d!\n", uVersion));
1146 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1147 }
1148
1149 /*
1150 * Load the virtual clock.
1151 */
1152 pVM->tm.s.cVirtualTicking = 0;
1153 /* the virtual clock. */
1154 uint64_t u64Hz;
1155 int rc = SSMR3GetU64(pSSM, &u64Hz);
1156 if (RT_FAILURE(rc))
1157 return rc;
1158 if (u64Hz != TMCLOCK_FREQ_VIRTUAL)
1159 {
1160 AssertMsgFailed(("The virtual clock frequency differs! Saved: %'RU64 Binary: %'RU64\n",
1161 u64Hz, TMCLOCK_FREQ_VIRTUAL));
1162 return VERR_SSM_VIRTUAL_CLOCK_HZ;
1163 }
1164 SSMR3GetU64(pSSM, &pVM->tm.s.u64Virtual);
1165 pVM->tm.s.u64VirtualOffset = 0;
1166
1167 /* the virtual timer synchronous clock. */
1168 pVM->tm.s.fVirtualSyncTicking = false;
1169 uint64_t u64;
1170 SSMR3GetU64(pSSM, &u64);
1171 pVM->tm.s.u64VirtualSync = u64;
1172 SSMR3GetU64(pSSM, &u64);
1173 pVM->tm.s.offVirtualSync = u64;
1174 SSMR3GetU64(pSSM, &u64);
1175 pVM->tm.s.offVirtualSyncGivenUp = u64;
1176 SSMR3GetU64(pSSM, &u64);
1177 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64;
1178 bool f;
1179 SSMR3GetBool(pSSM, &f);
1180 pVM->tm.s.fVirtualSyncCatchUp = f;
1181
1182 /* the real clock */
1183 rc = SSMR3GetU64(pSSM, &u64Hz);
1184 if (RT_FAILURE(rc))
1185 return rc;
1186 if (u64Hz != TMCLOCK_FREQ_REAL)
1187 {
1188 AssertMsgFailed(("The real clock frequency differs! Saved: %'RU64 Binary: %'RU64\n",
1189 u64Hz, TMCLOCK_FREQ_REAL));
1190 return VERR_SSM_VIRTUAL_CLOCK_HZ; /* missleading... */
1191 }
1192
1193 /* the cpu tick clock. */
1194 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1195 {
1196 PVMCPU pVCpu = &pVM->aCpus[i];
1197
1198 pVCpu->tm.s.fTSCTicking = false;
1199 SSMR3GetU64(pSSM, &pVCpu->tm.s.u64TSC);
1200
1201 if (pVM->tm.s.fTSCUseRealTSC)
1202 pVCpu->tm.s.offTSCRawSrc = 0; /** @todo TSC restore stuff and HWACC. */
1203 }
1204
1205 rc = SSMR3GetU64(pSSM, &u64Hz);
1206 if (RT_FAILURE(rc))
1207 return rc;
1208 if (!pVM->tm.s.fTSCUseRealTSC)
1209 pVM->tm.s.cTSCTicksPerSecond = u64Hz;
1210
1211 LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%'RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool (state load)\n",
1212 pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC));
1213
1214 /*
1215 * Make sure timers get rescheduled immediately.
1216 */
1217 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
1218 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
1219
1220 return VINF_SUCCESS;
1221}
1222
1223
1224/**
1225 * Internal TMR3TimerCreate worker.
1226 *
1227 * @returns VBox status code.
1228 * @param pVM The VM handle.
1229 * @param enmClock The timer clock.
1230 * @param pszDesc The timer description.
1231 * @param ppTimer Where to store the timer pointer on success.
1232 */
1233static int tmr3TimerCreate(PVM pVM, TMCLOCK enmClock, const char *pszDesc, PPTMTIMERR3 ppTimer)
1234{
1235 VM_ASSERT_EMT(pVM);
1236
1237 /*
1238 * Allocate the timer.
1239 */
1240 PTMTIMERR3 pTimer = NULL;
1241 if (pVM->tm.s.pFree && VM_IS_EMT(pVM))
1242 {
1243 pTimer = pVM->tm.s.pFree;
1244 pVM->tm.s.pFree = pTimer->pBigNext;
1245 Log3(("TM: Recycling timer %p, new free head %p.\n", pTimer, pTimer->pBigNext));
1246 }
1247
1248 if (!pTimer)
1249 {
1250 int rc = MMHyperAlloc(pVM, sizeof(*pTimer), 0, MM_TAG_TM, (void **)&pTimer);
1251 if (RT_FAILURE(rc))
1252 return rc;
1253 Log3(("TM: Allocated new timer %p\n", pTimer));
1254 }
1255
1256 /*
1257 * Initialize it.
1258 */
1259 pTimer->u64Expire = 0;
1260 pTimer->enmClock = enmClock;
1261 pTimer->pVMR3 = pVM;
1262 pTimer->pVMR0 = pVM->pVMR0;
1263 pTimer->pVMRC = pVM->pVMRC;
1264 pTimer->enmState = TMTIMERSTATE_STOPPED;
1265 pTimer->offScheduleNext = 0;
1266 pTimer->offNext = 0;
1267 pTimer->offPrev = 0;
1268 pTimer->pvUser = NULL;
1269 pTimer->pCritSect = NULL;
1270 pTimer->pszDesc = pszDesc;
1271
1272 /* insert into the list of created timers. */
1273 tmTimerLock(pVM);
1274 pTimer->pBigPrev = NULL;
1275 pTimer->pBigNext = pVM->tm.s.pCreated;
1276 pVM->tm.s.pCreated = pTimer;
1277 if (pTimer->pBigNext)
1278 pTimer->pBigNext->pBigPrev = pTimer;
1279#ifdef VBOX_STRICT
1280 tmTimerQueuesSanityChecks(pVM, "tmR3TimerCreate");
1281#endif
1282 tmTimerUnlock(pVM);
1283
1284 *ppTimer = pTimer;
1285 return VINF_SUCCESS;
1286}
1287
1288
1289/**
1290 * Creates a device timer.
1291 *
1292 * @returns VBox status.
1293 * @param pVM The VM to create the timer in.
1294 * @param pDevIns Device instance.
1295 * @param enmClock The clock to use on this timer.
1296 * @param pfnCallback Callback function.
1297 * @param pvUser The user argument to the callback.
1298 * @param fFlags Timer creation flags, see grp_tm_timer_flags.
1299 * @param pszDesc Pointer to description string which must stay around
1300 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1301 * @param ppTimer Where to store the timer on success.
1302 */
1303VMM_INT_DECL(int) TMR3TimerCreateDevice(PVM pVM, PPDMDEVINS pDevIns, TMCLOCK enmClock, PFNTMTIMERDEV pfnCallback, void *pvUser, uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1304{
1305 AssertReturn(!(fFlags & ~(TMTIMER_FLAGS_NO_CRIT_SECT)), VERR_INVALID_PARAMETER);
1306
1307 /*
1308 * Allocate and init stuff.
1309 */
1310 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
1311 if (RT_SUCCESS(rc))
1312 {
1313 (*ppTimer)->enmType = TMTIMERTYPE_DEV;
1314 (*ppTimer)->u.Dev.pfnTimer = pfnCallback;
1315 (*ppTimer)->u.Dev.pDevIns = pDevIns;
1316 (*ppTimer)->pvUser = pvUser;
1317 if (fFlags & TMTIMER_FLAGS_DEFAULT_CRIT_SECT)
1318 {
1319 if (pDevIns->pCritSectR3)
1320 (*ppTimer)->pCritSect = pDevIns->pCritSectR3;
1321 else
1322 (*ppTimer)->pCritSect = IOMR3GetCritSect(pVM);
1323 }
1324 Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
1325 }
1326
1327 return rc;
1328}
1329
1330
1331/**
1332 * Creates a driver timer.
1333 *
1334 * @returns VBox status.
1335 * @param pVM The VM to create the timer in.
1336 * @param pDrvIns Driver instance.
1337 * @param enmClock The clock to use on this timer.
1338 * @param pfnCallback Callback function.
1339 * @param pvUser The user argument to the callback.
1340 * @param fFlags Timer creation flags, see grp_tm_timer_flags.
1341 * @param pszDesc Pointer to description string which must stay around
1342 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1343 * @param ppTimer Where to store the timer on success.
1344 */
1345VMM_INT_DECL(int) TMR3TimerCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, void *pvUser,
1346 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1347{
1348 AssertReturn(!(fFlags & ~(TMTIMER_FLAGS_NO_CRIT_SECT)), VERR_INVALID_PARAMETER);
1349
1350 /*
1351 * Allocate and init stuff.
1352 */
1353 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
1354 if (RT_SUCCESS(rc))
1355 {
1356 (*ppTimer)->enmType = TMTIMERTYPE_DRV;
1357 (*ppTimer)->u.Drv.pfnTimer = pfnCallback;
1358 (*ppTimer)->u.Drv.pDrvIns = pDrvIns;
1359 (*ppTimer)->pvUser = pvUser;
1360 Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
1361 }
1362
1363 return rc;
1364}
1365
1366
1367/**
1368 * Creates an internal timer.
1369 *
1370 * @returns VBox status.
1371 * @param pVM The VM to create the timer in.
1372 * @param enmClock The clock to use on this timer.
1373 * @param pfnCallback Callback function.
1374 * @param pvUser User argument to be passed to the callback.
1375 * @param pszDesc Pointer to description string which must stay around
1376 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1377 * @param ppTimer Where to store the timer on success.
1378 */
1379VMMR3DECL(int) TMR3TimerCreateInternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMERINT pfnCallback, void *pvUser, const char *pszDesc, PPTMTIMERR3 ppTimer)
1380{
1381 /*
1382 * Allocate and init stuff.
1383 */
1384 PTMTIMER pTimer;
1385 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, &pTimer);
1386 if (RT_SUCCESS(rc))
1387 {
1388 pTimer->enmType = TMTIMERTYPE_INTERNAL;
1389 pTimer->u.Internal.pfnTimer = pfnCallback;
1390 pTimer->pvUser = pvUser;
1391 *ppTimer = pTimer;
1392 Log(("TM: Created internal timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
1393 }
1394
1395 return rc;
1396}
1397
1398/**
1399 * Creates an external timer.
1400 *
1401 * @returns Timer handle on success.
1402 * @returns NULL on failure.
1403 * @param pVM The VM to create the timer in.
1404 * @param enmClock The clock to use on this timer.
1405 * @param pfnCallback Callback function.
1406 * @param pvUser User argument.
1407 * @param pszDesc Pointer to description string which must stay around
1408 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1409 */
1410VMMR3DECL(PTMTIMERR3) TMR3TimerCreateExternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMEREXT pfnCallback, void *pvUser, const char *pszDesc)
1411{
1412 /*
1413 * Allocate and init stuff.
1414 */
1415 PTMTIMERR3 pTimer;
1416 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, &pTimer);
1417 if (RT_SUCCESS(rc))
1418 {
1419 pTimer->enmType = TMTIMERTYPE_EXTERNAL;
1420 pTimer->u.External.pfnTimer = pfnCallback;
1421 pTimer->pvUser = pvUser;
1422 Log(("TM: Created external timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
1423 return pTimer;
1424 }
1425
1426 return NULL;
1427}
1428
1429
1430/**
1431 * Destroy a timer
1432 *
1433 * @returns VBox status.
1434 * @param pTimer Timer handle as returned by one of the create functions.
1435 */
1436VMMR3DECL(int) TMR3TimerDestroy(PTMTIMER pTimer)
1437{
1438 /*
1439 * Be extra careful here.
1440 */
1441 if (!pTimer)
1442 return VINF_SUCCESS;
1443 AssertPtr(pTimer);
1444 Assert((unsigned)pTimer->enmClock < (unsigned)TMCLOCK_MAX);
1445
1446 PVM pVM = pTimer->CTX_SUFF(pVM);
1447 PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[pTimer->enmClock];
1448 bool fActive = false;
1449 bool fPending = false;
1450
1451 AssertMsg( !pTimer->pCritSect
1452 || VMR3GetState(pVM) != VMSTATE_RUNNING
1453 || PDMCritSectIsOwner(pTimer->pCritSect), ("%s\n", pTimer->pszDesc));
1454
1455 /*
1456 * The rest of the game happens behind the lock, just
1457 * like create does. All the work is done here.
1458 */
1459 tmTimerLock(pVM);
1460 for (int cRetries = 1000;; cRetries--)
1461 {
1462 /*
1463 * Change to the DESTROY state.
1464 */
1465 TMTIMERSTATE enmState = pTimer->enmState;
1466 TMTIMERSTATE enmNewState = enmState;
1467 Log2(("TMTimerDestroy: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
1468 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries));
1469 switch (enmState)
1470 {
1471 case TMTIMERSTATE_STOPPED:
1472 case TMTIMERSTATE_EXPIRED_DELIVER:
1473 break;
1474
1475 case TMTIMERSTATE_ACTIVE:
1476 fActive = true;
1477 break;
1478
1479 case TMTIMERSTATE_PENDING_STOP:
1480 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1481 case TMTIMERSTATE_PENDING_RESCHEDULE:
1482 fActive = true;
1483 fPending = true;
1484 break;
1485
1486 case TMTIMERSTATE_PENDING_SCHEDULE:
1487 fPending = true;
1488 break;
1489
1490 /*
1491 * This shouldn't happen as the caller should make sure there are no races.
1492 */
1493 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1494 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1495 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1496 AssertMsgFailed(("%p:.enmState=%s %s\n", pTimer, tmTimerState(enmState), pTimer->pszDesc));
1497 tmTimerUnlock(pVM);
1498 if (!RTThreadYield())
1499 RTThreadSleep(1);
1500 AssertMsgReturn(cRetries > 0, ("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->pszDesc),
1501 VERR_TM_UNSTABLE_STATE);
1502 tmTimerLock(pVM);
1503 continue;
1504
1505 /*
1506 * Invalid states.
1507 */
1508 case TMTIMERSTATE_FREE:
1509 case TMTIMERSTATE_DESTROY:
1510 tmTimerUnlock(pVM);
1511 AssertLogRelMsgFailedReturn(("pTimer=%p %s\n", pTimer, tmTimerState(enmState)), VERR_TM_INVALID_STATE);
1512
1513 default:
1514 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1515 tmTimerUnlock(pVM);
1516 return VERR_TM_UNKNOWN_STATE;
1517 }
1518
1519 /*
1520 * Try switch to the destroy state.
1521 * This should always succeed as the caller should make sure there are no race.
1522 */
1523 bool fRc;
1524 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_DESTROY, enmState, fRc);
1525 if (fRc)
1526 break;
1527 AssertMsgFailed(("%p:.enmState=%s %s\n", pTimer, tmTimerState(enmState), pTimer->pszDesc));
1528 tmTimerUnlock(pVM);
1529 AssertMsgReturn(cRetries > 0, ("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->pszDesc),
1530 VERR_TM_UNSTABLE_STATE);
1531 tmTimerLock(pVM);
1532 }
1533
1534 /*
1535 * Unlink from the active list.
1536 */
1537 if (fActive)
1538 {
1539 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1540 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
1541 if (pPrev)
1542 TMTIMER_SET_NEXT(pPrev, pNext);
1543 else
1544 {
1545 TMTIMER_SET_HEAD(pQueue, pNext);
1546 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1547 }
1548 if (pNext)
1549 TMTIMER_SET_PREV(pNext, pPrev);
1550 pTimer->offNext = 0;
1551 pTimer->offPrev = 0;
1552 }
1553
1554 /*
1555 * Unlink from the schedule list by running it.
1556 */
1557 if (fPending)
1558 {
1559 Log3(("TMR3TimerDestroy: tmTimerQueueSchedule\n"));
1560 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
1561 Assert(pQueue->offSchedule);
1562 tmTimerQueueSchedule(pVM, pQueue);
1563 }
1564
1565 /*
1566 * Read to move the timer from the created list and onto the free list.
1567 */
1568 Assert(!pTimer->offNext); Assert(!pTimer->offPrev); Assert(!pTimer->offScheduleNext);
1569
1570 /* unlink from created list */
1571 if (pTimer->pBigPrev)
1572 pTimer->pBigPrev->pBigNext = pTimer->pBigNext;
1573 else
1574 pVM->tm.s.pCreated = pTimer->pBigNext;
1575 if (pTimer->pBigNext)
1576 pTimer->pBigNext->pBigPrev = pTimer->pBigPrev;
1577 pTimer->pBigNext = 0;
1578 pTimer->pBigPrev = 0;
1579
1580 /* free */
1581 Log2(("TM: Inserting %p into the free list ahead of %p!\n", pTimer, pVM->tm.s.pFree));
1582 TM_SET_STATE(pTimer, TMTIMERSTATE_FREE);
1583 pTimer->pBigNext = pVM->tm.s.pFree;
1584 pVM->tm.s.pFree = pTimer;
1585
1586#ifdef VBOX_STRICT
1587 tmTimerQueuesSanityChecks(pVM, "TMR3TimerDestroy");
1588#endif
1589 tmTimerUnlock(pVM);
1590 return VINF_SUCCESS;
1591}
1592
1593
1594/**
1595 * Destroy all timers owned by a device.
1596 *
1597 * @returns VBox status.
1598 * @param pVM VM handle.
1599 * @param pDevIns Device which timers should be destroyed.
1600 */
1601VMM_INT_DECL(int) TMR3TimerDestroyDevice(PVM pVM, PPDMDEVINS pDevIns)
1602{
1603 LogFlow(("TMR3TimerDestroyDevice: pDevIns=%p\n", pDevIns));
1604 if (!pDevIns)
1605 return VERR_INVALID_PARAMETER;
1606
1607 tmTimerLock(pVM);
1608 PTMTIMER pCur = pVM->tm.s.pCreated;
1609 while (pCur)
1610 {
1611 PTMTIMER pDestroy = pCur;
1612 pCur = pDestroy->pBigNext;
1613 if ( pDestroy->enmType == TMTIMERTYPE_DEV
1614 && pDestroy->u.Dev.pDevIns == pDevIns)
1615 {
1616 int rc = TMR3TimerDestroy(pDestroy);
1617 AssertRC(rc);
1618 }
1619 }
1620 tmTimerUnlock(pVM);
1621
1622 LogFlow(("TMR3TimerDestroyDevice: returns VINF_SUCCESS\n"));
1623 return VINF_SUCCESS;
1624}
1625
1626
1627/**
1628 * Destroy all timers owned by a driver.
1629 *
1630 * @returns VBox status.
1631 * @param pVM VM handle.
1632 * @param pDrvIns Driver which timers should be destroyed.
1633 */
1634VMM_INT_DECL(int) TMR3TimerDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns)
1635{
1636 LogFlow(("TMR3TimerDestroyDriver: pDrvIns=%p\n", pDrvIns));
1637 if (!pDrvIns)
1638 return VERR_INVALID_PARAMETER;
1639
1640 tmTimerLock(pVM);
1641 PTMTIMER pCur = pVM->tm.s.pCreated;
1642 while (pCur)
1643 {
1644 PTMTIMER pDestroy = pCur;
1645 pCur = pDestroy->pBigNext;
1646 if ( pDestroy->enmType == TMTIMERTYPE_DRV
1647 && pDestroy->u.Drv.pDrvIns == pDrvIns)
1648 {
1649 int rc = TMR3TimerDestroy(pDestroy);
1650 AssertRC(rc);
1651 }
1652 }
1653 tmTimerUnlock(pVM);
1654
1655 LogFlow(("TMR3TimerDestroyDriver: returns VINF_SUCCESS\n"));
1656 return VINF_SUCCESS;
1657}
1658
1659
1660/**
1661 * Internal function for getting the clock time.
1662 *
1663 * @returns clock time.
1664 * @param pVM The VM handle.
1665 * @param enmClock The clock.
1666 */
1667DECLINLINE(uint64_t) tmClock(PVM pVM, TMCLOCK enmClock)
1668{
1669 switch (enmClock)
1670 {
1671 case TMCLOCK_VIRTUAL: return TMVirtualGet(pVM);
1672 case TMCLOCK_VIRTUAL_SYNC: return TMVirtualSyncGet(pVM);
1673 case TMCLOCK_REAL: return TMRealGet(pVM);
1674 case TMCLOCK_TSC: return TMCpuTickGet(&pVM->aCpus[0] /* just take VCPU 0 */);
1675 default:
1676 AssertMsgFailed(("enmClock=%d\n", enmClock));
1677 return ~(uint64_t)0;
1678 }
1679}
1680
1681
1682/**
1683 * Checks if the sync queue has one or more expired timers.
1684 *
1685 * @returns true / false.
1686 *
1687 * @param pVM The VM handle.
1688 * @param enmClock The queue.
1689 */
1690DECLINLINE(bool) tmR3HasExpiredTimer(PVM pVM, TMCLOCK enmClock)
1691{
1692 const uint64_t u64Expire = pVM->tm.s.CTX_SUFF(paTimerQueues)[enmClock].u64Expire;
1693 return u64Expire != INT64_MAX && u64Expire <= tmClock(pVM, enmClock);
1694}
1695
1696
1697/**
1698 * Checks for expired timers in all the queues.
1699 *
1700 * @returns true / false.
1701 * @param pVM The VM handle.
1702 */
1703DECLINLINE(bool) tmR3AnyExpiredTimers(PVM pVM)
1704{
1705 /*
1706 * Combine the time calculation for the first two since we're not on EMT
1707 * TMVirtualSyncGet only permits EMT.
1708 */
1709 uint64_t u64Now = TMVirtualGetNoCheck(pVM);
1710 if (pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire <= u64Now)
1711 return true;
1712 u64Now = pVM->tm.s.fVirtualSyncTicking
1713 ? u64Now - pVM->tm.s.offVirtualSync
1714 : pVM->tm.s.u64VirtualSync;
1715 if (pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire <= u64Now)
1716 return true;
1717
1718 /*
1719 * The remaining timers.
1720 */
1721 if (tmR3HasExpiredTimer(pVM, TMCLOCK_REAL))
1722 return true;
1723 if (tmR3HasExpiredTimer(pVM, TMCLOCK_TSC))
1724 return true;
1725 return false;
1726}
1727
1728
1729/**
1730 * Schedulation timer callback.
1731 *
1732 * @param pTimer Timer handle.
1733 * @param pvUser VM handle.
1734 * @thread Timer thread.
1735 *
1736 * @remark We cannot do the scheduling and queues running from a timer handler
1737 * since it's not executing in EMT, and even if it was it would be async
1738 * and we wouldn't know the state of the affairs.
1739 * So, we'll just raise the timer FF and force any REM execution to exit.
1740 */
1741static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t /*iTick*/)
1742{
1743 PVM pVM = (PVM)pvUser;
1744 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
1745
1746 AssertCompile(TMCLOCK_MAX == 4);
1747#ifdef DEBUG_Sander /* very annoying, keep it private. */
1748 if (VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
1749 Log(("tmR3TimerCallback: timer event still pending!!\n"));
1750#endif
1751 if ( !VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER)
1752 && ( pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].offSchedule /** @todo FIXME - reconsider offSchedule as a reason for running the timer queues. */
1753 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].offSchedule
1754 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].offSchedule
1755 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].offSchedule
1756 || tmR3AnyExpiredTimers(pVM)
1757 )
1758 && !VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER)
1759 && !pVM->tm.s.fRunningQueues
1760 )
1761 {
1762 Log5(("TM(%u): FF: 0 -> 1\n", __LINE__));
1763 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
1764 REMR3NotifyTimerPending(pVM, pVCpuDst);
1765 VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM /** @todo | VMNOTIFYFF_FLAGS_POKE ?*/);
1766 STAM_COUNTER_INC(&pVM->tm.s.StatTimerCallbackSetFF);
1767 }
1768}
1769
1770
1771/**
1772 * Schedules and runs any pending timers.
1773 *
1774 * This is normally called from a forced action handler in EMT.
1775 *
1776 * @param pVM The VM to run the timers for.
1777 *
1778 * @thread EMT (actually EMT0, but we fend off the others)
1779 */
1780VMMR3DECL(void) TMR3TimerQueuesDo(PVM pVM)
1781{
1782 /*
1783 * Only the dedicated timer EMT should do stuff here.
1784 * (fRunningQueues is only used as an indicator.)
1785 */
1786 Assert(pVM->tm.s.idTimerCpu < pVM->cCpus);
1787 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
1788 if (VMMGetCpu(pVM) != pVCpuDst)
1789 {
1790 Assert(pVM->cCpus > 1);
1791 return;
1792 }
1793 STAM_PROFILE_START(&pVM->tm.s.StatDoQueues, a);
1794 Log2(("TMR3TimerQueuesDo:\n"));
1795 Assert(!pVM->tm.s.fRunningQueues);
1796 ASMAtomicWriteBool(&pVM->tm.s.fRunningQueues, true);
1797 tmTimerLock(pVM);
1798
1799 /*
1800 * Process the queues.
1801 */
1802 AssertCompile(TMCLOCK_MAX == 4);
1803
1804 /* TMCLOCK_VIRTUAL_SYNC (see also TMR3VirtualSyncFF) */
1805 STAM_PROFILE_ADV_START(&pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL_SYNC], s1);
1806 tmVirtualSyncLock(pVM);
1807 ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, true);
1808 VMCPU_FF_CLEAR(pVCpuDst, VMCPU_FF_TIMER); /* Clear the FF once we started working for real. */
1809
1810 if (pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].offSchedule)
1811 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC]);
1812 tmR3TimerQueueRunVirtualSync(pVM);
1813 if (pVM->tm.s.fVirtualSyncTicking) /** @todo move into tmR3TimerQueueRunVirtualSync - FIXME */
1814 VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
1815
1816 ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, false);
1817 tmVirtualSyncUnlock(pVM);
1818 STAM_PROFILE_ADV_STOP(&pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL_SYNC], s1);
1819
1820 /* TMCLOCK_VIRTUAL */
1821 STAM_PROFILE_ADV_START(&pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL], s2);
1822 if (pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].offSchedule)
1823 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL]);
1824 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL]);
1825 STAM_PROFILE_ADV_STOP(&pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL], s2);
1826
1827 /* TMCLOCK_TSC */
1828 Assert(!pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].offActive); /* not used */
1829
1830 /* TMCLOCK_REAL */
1831 STAM_PROFILE_ADV_START(&pVM->tm.s.aStatDoQueues[TMCLOCK_REAL], s3);
1832 if (pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].offSchedule)
1833 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL]);
1834 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL]);
1835 STAM_PROFILE_ADV_STOP(&pVM->tm.s.aStatDoQueues[TMCLOCK_REAL], s3);
1836
1837#ifdef VBOX_STRICT
1838 /* check that we didn't screwup. */
1839 tmTimerQueuesSanityChecks(pVM, "TMR3TimerQueuesDo");
1840#endif
1841
1842 /* done */
1843 Log2(("TMR3TimerQueuesDo: returns void\n"));
1844 ASMAtomicWriteBool(&pVM->tm.s.fRunningQueues, false);
1845 tmTimerUnlock(pVM);
1846 STAM_PROFILE_STOP(&pVM->tm.s.StatDoQueues, a);
1847}
1848
1849//RT_C_DECLS_BEGIN
1850//int iomLock(PVM pVM);
1851//void iomUnlock(PVM pVM);
1852//RT_C_DECLS_END
1853
1854
1855/**
1856 * Schedules and runs any pending times in the specified queue.
1857 *
1858 * This is normally called from a forced action handler in EMT.
1859 *
1860 * @param pVM The VM to run the timers for.
1861 * @param pQueue The queue to run.
1862 */
1863static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue)
1864{
1865 VM_ASSERT_EMT(pVM);
1866
1867 /*
1868 * Run timers.
1869 *
1870 * We check the clock once and run all timers which are ACTIVE
1871 * and have an expire time less or equal to the time we read.
1872 *
1873 * N.B. A generic unlink must be applied since other threads
1874 * are allowed to mess with any active timer at any time.
1875 * However, we only allow EMT to handle EXPIRED_PENDING
1876 * timers, thus enabling the timer handler function to
1877 * arm the timer again.
1878 */
1879 PTMTIMER pNext = TMTIMER_GET_HEAD(pQueue);
1880 if (!pNext)
1881 return;
1882 const uint64_t u64Now = tmClock(pVM, pQueue->enmClock);
1883 while (pNext && pNext->u64Expire <= u64Now)
1884 {
1885 PTMTIMER pTimer = pNext;
1886 pNext = TMTIMER_GET_NEXT(pTimer);
1887 PPDMCRITSECT pCritSect = pTimer->pCritSect;
1888 if (pCritSect)
1889 PDMCritSectEnter(pCritSect, VERR_INTERNAL_ERROR);
1890 Log2(("tmR3TimerQueueRun: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
1891 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
1892 bool fRc;
1893 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_GET_UNLINK, TMTIMERSTATE_ACTIVE, fRc);
1894 if (fRc)
1895 {
1896 Assert(!pTimer->offScheduleNext); /* this can trigger falsely */
1897
1898 /* unlink */
1899 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1900 if (pPrev)
1901 TMTIMER_SET_NEXT(pPrev, pNext);
1902 else
1903 {
1904 TMTIMER_SET_HEAD(pQueue, pNext);
1905 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1906 }
1907 if (pNext)
1908 TMTIMER_SET_PREV(pNext, pPrev);
1909 pTimer->offNext = 0;
1910 pTimer->offPrev = 0;
1911
1912 /* fire */
1913 TM_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_DELIVER);
1914 switch (pTimer->enmType)
1915 {
1916 case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer, pTimer->pvUser); break;
1917 case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer, pTimer->pvUser); break;
1918 case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->pvUser); break;
1919 case TMTIMERTYPE_EXTERNAL: pTimer->u.External.pfnTimer(pTimer->pvUser); break;
1920 default:
1921 AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->pszDesc));
1922 break;
1923 }
1924
1925 /* change the state if it wasn't changed already in the handler. */
1926 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED_DELIVER, fRc);
1927 Log2(("tmR3TimerQueueRun: new state %s\n", tmTimerState(pTimer->enmState)));
1928 }
1929 if (pCritSect)
1930 PDMCritSectLeave(pCritSect);
1931 } /* run loop */
1932}
1933
1934
1935/**
1936 * Schedules and runs any pending times in the timer queue for the
1937 * synchronous virtual clock.
1938 *
1939 * This scheduling is a bit different from the other queues as it need
1940 * to implement the special requirements of the timer synchronous virtual
1941 * clock, thus this 2nd queue run funcion.
1942 *
1943 * @param pVM The VM to run the timers for.
1944 *
1945 * @remarks The caller must own both the TM/EMT and the Virtual Sync locks.
1946 */
1947static void tmR3TimerQueueRunVirtualSync(PVM pVM)
1948{
1949 PTMTIMERQUEUE const pQueue = &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC];
1950 VM_ASSERT_EMT(pVM);
1951
1952 /*
1953 * Any timers?
1954 */
1955 PTMTIMER pNext = TMTIMER_GET_HEAD(pQueue);
1956 if (RT_UNLIKELY(!pNext))
1957 {
1958 Assert(pVM->tm.s.fVirtualSyncTicking || !pVM->tm.s.cVirtualTicking);
1959 return;
1960 }
1961 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRun);
1962
1963 /*
1964 * Calculate the time frame for which we will dispatch timers.
1965 *
1966 * We use a time frame ranging from the current sync time (which is most likely the
1967 * same as the head timer) and some configurable period (100000ns) up towards the
1968 * current virtual time. This period might also need to be restricted by the catch-up
1969 * rate so frequent calls to this function won't accelerate the time too much, however
1970 * this will be implemented at a later point if neccessary.
1971 *
1972 * Without this frame we would 1) having to run timers much more frequently
1973 * and 2) lag behind at a steady rate.
1974 */
1975 const uint64_t u64VirtualNow = TMVirtualGetNoCheck(pVM);
1976 uint64_t const offSyncGivenUp = pVM->tm.s.offVirtualSyncGivenUp;
1977 uint64_t u64Now;
1978 if (!pVM->tm.s.fVirtualSyncTicking)
1979 {
1980 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunStoppedAlready);
1981 u64Now = pVM->tm.s.u64VirtualSync;
1982#ifdef DEBUG_bird
1983 Assert(u64Now <= pNext->u64Expire);
1984#endif
1985 }
1986 else
1987 {
1988 /* Calc 'now'. */
1989 bool fStopCatchup = false;
1990 bool fUpdateStuff = false;
1991 uint64_t off = pVM->tm.s.offVirtualSync;
1992 if (pVM->tm.s.fVirtualSyncCatchUp)
1993 {
1994 uint64_t u64Delta = u64VirtualNow - pVM->tm.s.u64VirtualSyncCatchUpPrev;
1995 if (RT_LIKELY(!(u64Delta >> 32)))
1996 {
1997 uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
1998 if (off > u64Sub + offSyncGivenUp)
1999 {
2000 off -= u64Sub;
2001 Log4(("TM: %'RU64/-%'8RU64: sub %'RU64 [tmR3TimerQueueRunVirtualSync]\n", u64VirtualNow - off, off - offSyncGivenUp, u64Sub));
2002 }
2003 else
2004 {
2005 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
2006 fStopCatchup = true;
2007 off = offSyncGivenUp;
2008 }
2009 fUpdateStuff = true;
2010 }
2011 }
2012 u64Now = u64VirtualNow - off;
2013
2014 /* Check if stopped by expired timer. */
2015 uint64_t u64Expire = pNext->u64Expire;
2016 if (u64Now >= pNext->u64Expire)
2017 {
2018 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunStop);
2019 u64Now = pNext->u64Expire;
2020 ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64Now);
2021 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
2022 Log4(("TM: %'RU64/-%'8RU64: exp tmr [tmR3TimerQueueRunVirtualSync]\n", u64Now, u64VirtualNow - u64Now - offSyncGivenUp));
2023 }
2024 else if (fUpdateStuff)
2025 {
2026 ASMAtomicWriteU64(&pVM->tm.s.offVirtualSync, off);
2027 ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev, u64VirtualNow);
2028 if (fStopCatchup)
2029 {
2030 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
2031 Log4(("TM: %'RU64/0: caught up [tmR3TimerQueueRunVirtualSync]\n", u64VirtualNow));
2032 }
2033 }
2034 }
2035
2036 /* calc end of frame. */
2037 uint64_t u64Max = u64Now + pVM->tm.s.u32VirtualSyncScheduleSlack;
2038 if (u64Max > u64VirtualNow - offSyncGivenUp)
2039 u64Max = u64VirtualNow - offSyncGivenUp;
2040
2041 /* assert sanity */
2042#ifdef DEBUG_bird
2043 Assert(u64Now <= u64VirtualNow - offSyncGivenUp);
2044 Assert(u64Max <= u64VirtualNow - offSyncGivenUp);
2045 Assert(u64Now <= u64Max);
2046 Assert(offSyncGivenUp == pVM->tm.s.offVirtualSyncGivenUp);
2047#endif
2048
2049 /*
2050 * Process the expired timers moving the clock along as we progress.
2051 */
2052#ifdef DEBUG_bird
2053#ifdef VBOX_STRICT
2054 uint64_t u64Prev = u64Now; NOREF(u64Prev);
2055#endif
2056#endif
2057 while (pNext && pNext->u64Expire <= u64Max)
2058 {
2059 PTMTIMER pTimer = pNext;
2060 pNext = TMTIMER_GET_NEXT(pTimer);
2061 PPDMCRITSECT pCritSect = pTimer->pCritSect;
2062 if (pCritSect)
2063 PDMCritSectEnter(pCritSect, VERR_INTERNAL_ERROR);
2064 Log2(("tmR3TimerQueueRun: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
2065 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
2066 bool fRc;
2067 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_GET_UNLINK, TMTIMERSTATE_ACTIVE, fRc);
2068 if (fRc)
2069 {
2070 /* unlink */
2071 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
2072 if (pPrev)
2073 TMTIMER_SET_NEXT(pPrev, pNext);
2074 else
2075 {
2076 TMTIMER_SET_HEAD(pQueue, pNext);
2077 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
2078 }
2079 if (pNext)
2080 TMTIMER_SET_PREV(pNext, pPrev);
2081 pTimer->offNext = 0;
2082 pTimer->offPrev = 0;
2083
2084 /* advance the clock - don't permit timers to be out of order or armed in the 'past'. */
2085#ifdef DEBUG_bird
2086#ifdef VBOX_STRICT
2087 AssertMsg(pTimer->u64Expire >= u64Prev, ("%'RU64 < %'RU64 %s\n", pTimer->u64Expire, u64Prev, pTimer->pszDesc));
2088 u64Prev = pTimer->u64Expire;
2089#endif
2090#endif
2091 ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, pTimer->u64Expire);
2092 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
2093
2094 /* fire */
2095 TM_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_DELIVER);
2096 switch (pTimer->enmType)
2097 {
2098 case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer, pTimer->pvUser); break;
2099 case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer, pTimer->pvUser); break;
2100 case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->pvUser); break;
2101 case TMTIMERTYPE_EXTERNAL: pTimer->u.External.pfnTimer(pTimer->pvUser); break;
2102 default:
2103 AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->pszDesc));
2104 break;
2105 }
2106
2107 /* change the state if it wasn't changed already in the handler. */
2108 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED_DELIVER, fRc);
2109 Log2(("tmR3TimerQueueRun: new state %s\n", tmTimerState(pTimer->enmState)));
2110 }
2111 if (pCritSect)
2112 PDMCritSectLeave(pCritSect);
2113 } /* run loop */
2114
2115 /*
2116 * Restart the clock if it was stopped to serve any timers,
2117 * and start/adjust catch-up if necessary.
2118 */
2119 if ( !pVM->tm.s.fVirtualSyncTicking
2120 && pVM->tm.s.cVirtualTicking)
2121 {
2122 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunRestart);
2123
2124 /* calc the slack we've handed out. */
2125 const uint64_t u64VirtualNow2 = TMVirtualGetNoCheck(pVM);
2126 Assert(u64VirtualNow2 >= u64VirtualNow);
2127#ifdef DEBUG_bird
2128 AssertMsg(pVM->tm.s.u64VirtualSync >= u64Now, ("%'RU64 < %'RU64\n", pVM->tm.s.u64VirtualSync, u64Now));
2129#endif
2130 const uint64_t offSlack = pVM->tm.s.u64VirtualSync - u64Now;
2131 STAM_STATS({
2132 if (offSlack)
2133 {
2134 PSTAMPROFILE p = &pVM->tm.s.StatVirtualSyncRunSlack;
2135 p->cPeriods++;
2136 p->cTicks += offSlack;
2137 if (p->cTicksMax < offSlack) p->cTicksMax = offSlack;
2138 if (p->cTicksMin > offSlack) p->cTicksMin = offSlack;
2139 }
2140 });
2141
2142 /* Let the time run a little bit while we were busy running timers(?). */
2143 uint64_t u64Elapsed;
2144#define MAX_ELAPSED 30000U /* ns */
2145 if (offSlack > MAX_ELAPSED)
2146 u64Elapsed = 0;
2147 else
2148 {
2149 u64Elapsed = u64VirtualNow2 - u64VirtualNow;
2150 if (u64Elapsed > MAX_ELAPSED)
2151 u64Elapsed = MAX_ELAPSED;
2152 u64Elapsed = u64Elapsed > offSlack ? u64Elapsed - offSlack : 0;
2153 }
2154#undef MAX_ELAPSED
2155
2156 /* Calc the current offset. */
2157 uint64_t offNew = u64VirtualNow2 - pVM->tm.s.u64VirtualSync - u64Elapsed;
2158 Assert(!(offNew & RT_BIT_64(63)));
2159 uint64_t offLag = offNew - pVM->tm.s.offVirtualSyncGivenUp;
2160 Assert(!(offLag & RT_BIT_64(63)));
2161
2162 /*
2163 * Deal with starting, adjusting and stopping catchup.
2164 */
2165 if (pVM->tm.s.fVirtualSyncCatchUp)
2166 {
2167 if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpStopThreshold)
2168 {
2169 /* stop */
2170 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
2171 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
2172 Log4(("TM: %'RU64/-%'8RU64: caught up [pt]\n", u64VirtualNow2 - offNew, offLag));
2173 }
2174 else if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold)
2175 {
2176 /* adjust */
2177 unsigned i = 0;
2178 while ( i + 1 < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods)
2179 && offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[i + 1].u64Start)
2180 i++;
2181 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage < pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage)
2182 {
2183 STAM_COUNTER_INC(&pVM->tm.s.aStatVirtualSyncCatchupAdjust[i]);
2184 ASMAtomicWriteU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
2185 Log4(("TM: %'RU64/%'8RU64: adj %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
2186 }
2187 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64VirtualNow2;
2188 }
2189 else
2190 {
2191 /* give up */
2192 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGiveUp);
2193 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
2194 ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
2195 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
2196 Log4(("TM: %'RU64/%'8RU64: give up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
2197 LogRel(("TM: Giving up catch-up attempt at a %'RU64 ns lag; new total: %'RU64 ns\n", offLag, offNew));
2198 }
2199 }
2200 else if (offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[0].u64Start)
2201 {
2202 if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold)
2203 {
2204 /* start */
2205 STAM_PROFILE_ADV_START(&pVM->tm.s.StatVirtualSyncCatchup, c);
2206 unsigned i = 0;
2207 while ( i + 1 < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods)
2208 && offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[i + 1].u64Start)
2209 i++;
2210 STAM_COUNTER_INC(&pVM->tm.s.aStatVirtualSyncCatchupInitial[i]);
2211 ASMAtomicWriteU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
2212 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, true);
2213 Log4(("TM: %'RU64/%'8RU64: catch-up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
2214 }
2215 else
2216 {
2217 /* don't bother */
2218 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGiveUpBeforeStarting);
2219 ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
2220 Log4(("TM: %'RU64/%'8RU64: give up\n", u64VirtualNow2 - offNew, offLag));
2221 LogRel(("TM: Not bothering to attempt catching up a %'RU64 ns lag; new total: %'RU64\n", offLag, offNew));
2222 }
2223 }
2224
2225 /*
2226 * Update the offset and restart the clock.
2227 */
2228 Assert(!(offNew & RT_BIT_64(63)));
2229 ASMAtomicWriteU64(&pVM->tm.s.offVirtualSync, offNew);
2230 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, true);
2231 }
2232}
2233
2234
2235/**
2236 * Deals with stopped Virtual Sync clock.
2237 *
2238 * This is called by the forced action flag handling code in EM when it
2239 * encounters the VM_FF_TM_VIRTUAL_SYNC flag. It is called by all VCPUs and they
2240 * will block on the VirtualSyncLock until the pending timers has been executed
2241 * and the clock restarted.
2242 *
2243 * @param pVM The VM to run the timers for.
2244 * @param pVCpu The virtual CPU we're running at.
2245 *
2246 * @thread EMTs
2247 */
2248VMM_INT_DECL(void) TMR3VirtualSyncFF(PVM pVM, PVMCPU pVCpu)
2249{
2250 Log2(("TMR3VirtualSyncFF:\n"));
2251
2252 /*
2253 * The EMT doing the timers is diverted to them.
2254 */
2255 if (pVCpu->idCpu == pVM->tm.s.idTimerCpu)
2256 TMR3TimerQueuesDo(pVM);
2257 /*
2258 * The other EMTs will block on the virtual sync lock and the first owner
2259 * will run the queue and thus restarting the clock.
2260 *
2261 * Note! This is very suboptimal code wrt to resuming execution when there
2262 * are more than two Virtual CPUs, since they will all have to enter
2263 * the critical section one by one. But it's a very simple solution
2264 * which will have to do the job for now.
2265 */
2266 else
2267 {
2268 STAM_PROFILE_START(&pVM->tm.s.StatVirtualSyncFF, a);
2269 tmVirtualSyncLock(pVM);
2270 if (pVM->tm.s.fVirtualSyncTicking)
2271 {
2272 STAM_PROFILE_STOP(&pVM->tm.s.StatVirtualSyncFF, a); /* before the unlock! */
2273 tmVirtualSyncUnlock(pVM);
2274 Log2(("TMR3VirtualSyncFF: ticking\n"));
2275 }
2276 else
2277 {
2278 tmVirtualSyncUnlock(pVM);
2279
2280 /* try run it. */
2281 tmTimerLock(pVM);
2282 tmVirtualSyncLock(pVM);
2283 if (pVM->tm.s.fVirtualSyncTicking)
2284 Log2(("TMR3VirtualSyncFF: ticking (2)\n"));
2285 else
2286 {
2287 ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, true);
2288 Log2(("TMR3VirtualSyncFF: running queue\n"));
2289
2290 if (pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].offSchedule)
2291 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC]);
2292 tmR3TimerQueueRunVirtualSync(pVM);
2293 if (pVM->tm.s.fVirtualSyncTicking) /** @todo move into tmR3TimerQueueRunVirtualSync - FIXME */
2294 VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
2295
2296 ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, false);
2297 }
2298 STAM_PROFILE_STOP(&pVM->tm.s.StatVirtualSyncFF, a); /* before the unlock! */
2299 tmVirtualSyncUnlock(pVM);
2300 tmTimerUnlock(pVM);
2301 }
2302 }
2303}
2304
2305
2306/** @name Saved state values
2307 * @{ */
2308#define TMTIMERSTATE_SAVED_PENDING_STOP 4
2309#define TMTIMERSTATE_SAVED_PENDING_SCHEDULE 7
2310/** @} */
2311
2312
2313/**
2314 * Saves the state of a timer to a saved state.
2315 *
2316 * @returns VBox status.
2317 * @param pTimer Timer to save.
2318 * @param pSSM Save State Manager handle.
2319 */
2320VMMR3DECL(int) TMR3TimerSave(PTMTIMERR3 pTimer, PSSMHANDLE pSSM)
2321{
2322 LogFlow(("TMR3TimerSave: %p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
2323 switch (pTimer->enmState)
2324 {
2325 case TMTIMERSTATE_STOPPED:
2326 case TMTIMERSTATE_PENDING_STOP:
2327 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
2328 return SSMR3PutU8(pSSM, TMTIMERSTATE_SAVED_PENDING_STOP);
2329
2330 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
2331 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2332 AssertMsgFailed(("u64Expire is being updated! (%s)\n", pTimer->pszDesc));
2333 if (!RTThreadYield())
2334 RTThreadSleep(1);
2335 /* fall thru */
2336 case TMTIMERSTATE_ACTIVE:
2337 case TMTIMERSTATE_PENDING_SCHEDULE:
2338 case TMTIMERSTATE_PENDING_RESCHEDULE:
2339 SSMR3PutU8(pSSM, TMTIMERSTATE_SAVED_PENDING_SCHEDULE);
2340 return SSMR3PutU64(pSSM, pTimer->u64Expire);
2341
2342 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
2343 case TMTIMERSTATE_EXPIRED_DELIVER:
2344 case TMTIMERSTATE_DESTROY:
2345 case TMTIMERSTATE_FREE:
2346 AssertMsgFailed(("Invalid timer state %d %s (%s)\n", pTimer->enmState, tmTimerState(pTimer->enmState), pTimer->pszDesc));
2347 return SSMR3HandleSetStatus(pSSM, VERR_TM_INVALID_STATE);
2348 }
2349
2350 AssertMsgFailed(("Unknown timer state %d (%s)\n", pTimer->enmState, pTimer->pszDesc));
2351 return SSMR3HandleSetStatus(pSSM, VERR_TM_UNKNOWN_STATE);
2352}
2353
2354
2355/**
2356 * Loads the state of a timer from a saved state.
2357 *
2358 * @returns VBox status.
2359 * @param pTimer Timer to restore.
2360 * @param pSSM Save State Manager handle.
2361 */
2362VMMR3DECL(int) TMR3TimerLoad(PTMTIMERR3 pTimer, PSSMHANDLE pSSM)
2363{
2364 Assert(pTimer); Assert(pSSM); VM_ASSERT_EMT(pTimer->pVMR3);
2365 LogFlow(("TMR3TimerLoad: %p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
2366
2367 /*
2368 * Load the state and validate it.
2369 */
2370 uint8_t u8State;
2371 int rc = SSMR3GetU8(pSSM, &u8State);
2372 if (RT_FAILURE(rc))
2373 return rc;
2374#if 1 /* Workaround for accidental state shift in r47786 (2009-05-26 19:12:12). */ /** @todo remove this in a few weeks! */
2375 if ( u8State == TMTIMERSTATE_SAVED_PENDING_STOP + 1
2376 || u8State == TMTIMERSTATE_SAVED_PENDING_SCHEDULE + 1)
2377 u8State--;
2378#endif
2379 if ( u8State != TMTIMERSTATE_SAVED_PENDING_STOP
2380 && u8State != TMTIMERSTATE_SAVED_PENDING_SCHEDULE)
2381 {
2382 AssertLogRelMsgFailed(("u8State=%d\n", u8State));
2383 return SSMR3HandleSetStatus(pSSM, VERR_TM_LOAD_STATE);
2384 }
2385
2386 /* Enter the critical section to make TMTimerSet/Stop happy. */
2387 PPDMCRITSECT pCritSect = pTimer->pCritSect;
2388 if (pCritSect)
2389 PDMCritSectEnter(pCritSect, VERR_INTERNAL_ERROR);
2390
2391 if (u8State == TMTIMERSTATE_SAVED_PENDING_SCHEDULE)
2392 {
2393 /*
2394 * Load the expire time.
2395 */
2396 uint64_t u64Expire;
2397 rc = SSMR3GetU64(pSSM, &u64Expire);
2398 if (RT_FAILURE(rc))
2399 return rc;
2400
2401 /*
2402 * Set it.
2403 */
2404 Log(("u8State=%d u64Expire=%llu\n", u8State, u64Expire));
2405 rc = TMTimerSet(pTimer, u64Expire);
2406 }
2407 else
2408 {
2409 /*
2410 * Stop it.
2411 */
2412 Log(("u8State=%d\n", u8State));
2413 rc = TMTimerStop(pTimer);
2414 }
2415
2416 if (pCritSect)
2417 PDMCritSectLeave(pCritSect);
2418
2419 /*
2420 * On failure set SSM status.
2421 */
2422 if (RT_FAILURE(rc))
2423 rc = SSMR3HandleSetStatus(pSSM, rc);
2424 return rc;
2425}
2426
2427
2428/**
2429 * Associates a critical section with a timer.
2430 *
2431 * The critical section will be entered prior to doing the timer call back, thus
2432 * avoiding potential races between the timer thread and other threads trying to
2433 * stop or adjust the timer expiration while it's being delivered. The timer
2434 * thread will leave the critical section when the timer callback returns.
2435 *
2436 * In strict builds, ownership of the critical section will be asserted by
2437 * TMTimerSet, TMTimerStop, TMTimerGetExpire and TMTimerDestroy (when called at
2438 * runtime).
2439 *
2440 * @retval VINF_SUCCESS on success.
2441 * @retval VERR_INVALID_HANDLE if the timer handle is NULL or invalid
2442 * (asserted).
2443 * @retval VERR_INVALID_PARAMETER if pCritSect is NULL or has an invalid magic
2444 * (asserted).
2445 * @retval VERR_ALREADY_EXISTS if a critical section was already associated
2446 * with the timer (asserted).
2447 * @retval VERR_INVALID_STATE if the timer isn't stopped.
2448 *
2449 * @param pTimer The timer handle.
2450 * @param pCritSect The critical section. The caller must make sure this
2451 * is around for the life time of the timer.
2452 *
2453 * @thread Any, but the caller is responsible for making sure the timer is not
2454 * active.
2455 */
2456VMMR3DECL(int) TMR3TimerSetCritSect(PTMTIMERR3 pTimer, PPDMCRITSECT pCritSect)
2457{
2458 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
2459 AssertPtrReturn(pCritSect, VERR_INVALID_PARAMETER);
2460 const char *pszName = PDMR3CritSectName(pCritSect); /* exploited for validation */
2461 AssertReturn(pszName, VERR_INVALID_PARAMETER);
2462 AssertReturn(!pTimer->pCritSect, VERR_ALREADY_EXISTS);
2463 AssertReturn(pTimer->enmState == TMTIMERSTATE_STOPPED, VERR_INVALID_STATE);
2464 LogFlow(("pTimer=%p (%s) pCritSect=%p (%s)\n", pTimer, pTimer->pszDesc, pCritSect, pszName));
2465
2466 pTimer->pCritSect = pCritSect;
2467 return VINF_SUCCESS;
2468}
2469
2470
2471/**
2472 * Get the real world UTC time adjusted for VM lag.
2473 *
2474 * @returns pTime.
2475 * @param pVM The VM instance.
2476 * @param pTime Where to store the time.
2477 */
2478VMM_INT_DECL(PRTTIMESPEC) TMR3UtcNow(PVM pVM, PRTTIMESPEC pTime)
2479{
2480 RTTimeNow(pTime);
2481 RTTimeSpecSubNano(pTime, ASMAtomicReadU64(&pVM->tm.s.offVirtualSync) - ASMAtomicReadU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp));
2482 RTTimeSpecAddNano(pTime, pVM->tm.s.offUTC);
2483 return pTime;
2484}
2485
2486
2487/**
2488 * Pauses all clocks except TMCLOCK_REAL.
2489 *
2490 * @returns VBox status code, all errors are asserted.
2491 * @param pVM The VM handle.
2492 * @param pVCpu The virtual CPU handle.
2493 * @thread EMT corrsponding to the virtual CPU handle.
2494 */
2495VMMR3DECL(int) TMR3NotifySuspend(PVM pVM, PVMCPU pVCpu)
2496{
2497 VMCPU_ASSERT_EMT(pVCpu);
2498
2499 /*
2500 * The shared virtual clock (includes virtual sync which is tied to it).
2501 */
2502 tmTimerLock(pVM); /* Paranoia: Exploiting the timer lock here. */
2503 int rc = tmVirtualPauseLocked(pVM);
2504 tmTimerUnlock(pVM);
2505 if (RT_FAILURE(rc))
2506 return rc;
2507
2508 /*
2509 * Pause the TSC last since it is normally linked to the virtual
2510 * sync clock, so the above code may actually stop both clock.
2511 */
2512 rc = tmCpuTickPause(pVM, pVCpu);
2513 if (RT_FAILURE(rc))
2514 return rc;
2515
2516#ifndef VBOX_WITHOUT_NS_ACCOUNTING
2517 /*
2518 * Update cNsTotal.
2519 */
2520 uint32_t uGen = ASMAtomicIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
2521 pVCpu->tm.s.cNsTotal = RTTimeNanoTS() - pVCpu->tm.s.u64NsTsStartTotal;
2522 pVCpu->tm.s.cNsOther = pVCpu->tm.s.cNsTotal - pVCpu->tm.s.cNsExecuting - pVCpu->tm.s.cNsHalted;
2523 ASMAtomicWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
2524#endif
2525
2526 return VINF_SUCCESS;
2527}
2528
2529
2530/**
2531 * Resumes all clocks except TMCLOCK_REAL.
2532 *
2533 * @returns VBox status code, all errors are asserted.
2534 * @param pVM The VM handle.
2535 * @param pVCpu The virtual CPU handle.
2536 * @thread EMT corrsponding to the virtual CPU handle.
2537 */
2538VMMR3DECL(int) TMR3NotifyResume(PVM pVM, PVMCPU pVCpu)
2539{
2540 VMCPU_ASSERT_EMT(pVCpu);
2541 int rc;
2542
2543#ifndef VBOX_WITHOUT_NS_ACCOUNTING
2544 /*
2545 * Set u64NsTsStartTotal. There is no need to back this out if either of
2546 * the two calls below fail.
2547 */
2548 pVCpu->tm.s.u64NsTsStartTotal = RTTimeNanoTS() - pVCpu->tm.s.cNsTotal;
2549#endif
2550
2551 /*
2552 * Resume the TSC first since it is normally linked to the virtual sync
2553 * clock, so it may actually not be resumed until we've executed the code
2554 * below.
2555 */
2556 if (!pVM->tm.s.fTSCTiedToExecution)
2557 {
2558 rc = tmCpuTickResume(pVM, pVCpu);
2559 if (RT_FAILURE(rc))
2560 return rc;
2561 }
2562
2563 /*
2564 * The shared virtual clock (includes virtual sync which is tied to it).
2565 */
2566 tmTimerLock(pVM); /* Paranoia: Exploiting the timer lock here. */
2567 rc = tmVirtualResumeLocked(pVM);
2568 tmTimerUnlock(pVM);
2569
2570 return rc;
2571}
2572
2573
2574/**
2575 * Sets the warp drive percent of the virtual time.
2576 *
2577 * @returns VBox status code.
2578 * @param pVM The VM handle.
2579 * @param u32Percent The new percentage. 100 means normal operation.
2580 *
2581 * @todo Move to Ring-3!
2582 */
2583VMMDECL(int) TMR3SetWarpDrive(PVM pVM, uint32_t u32Percent)
2584{
2585 return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)tmR3SetWarpDrive, 2, pVM, u32Percent);
2586}
2587
2588
2589/**
2590 * EMT worker for TMR3SetWarpDrive.
2591 *
2592 * @returns VBox status code.
2593 * @param pVM The VM handle.
2594 * @param u32Percent See TMR3SetWarpDrive().
2595 * @internal
2596 */
2597static DECLCALLBACK(int) tmR3SetWarpDrive(PVM pVM, uint32_t u32Percent)
2598{
2599 PVMCPU pVCpu = VMMGetCpu(pVM);
2600
2601 /*
2602 * Validate it.
2603 */
2604 AssertMsgReturn(u32Percent >= 2 && u32Percent <= 20000,
2605 ("%RX32 is not between 2 and 20000 (inclusive).\n", u32Percent),
2606 VERR_INVALID_PARAMETER);
2607
2608/** @todo This isn't a feature specific to virtual time, move the variables to
2609 * TM level and make it affect TMR3UCTNow as well! */
2610
2611 /*
2612 * If the time is running we'll have to pause it before we can change
2613 * the warp drive settings.
2614 */
2615 tmTimerLock(pVM); /* Paranoia: Exploiting the timer lock here. */
2616 bool fPaused = !!pVM->tm.s.cVirtualTicking;
2617 if (fPaused) /** @todo this isn't really working, but wtf. */
2618 TMR3NotifySuspend(pVM, pVCpu);
2619
2620 pVM->tm.s.u32VirtualWarpDrivePercentage = u32Percent;
2621 pVM->tm.s.fVirtualWarpDrive = u32Percent != 100;
2622 LogRel(("TM: u32VirtualWarpDrivePercentage=%RI32 fVirtualWarpDrive=%RTbool\n",
2623 pVM->tm.s.u32VirtualWarpDrivePercentage, pVM->tm.s.fVirtualWarpDrive));
2624
2625 if (fPaused)
2626 TMR3NotifyResume(pVM, pVCpu);
2627 tmTimerUnlock(pVM);
2628 return VINF_SUCCESS;
2629}
2630
2631
2632/**
2633 * Gets the performance information for one virtual CPU as seen by the VMM.
2634 *
2635 * The returned times covers the period where the VM is running and will be
2636 * reset when restoring a previous VM state (at least for the time being).
2637 *
2638 * @retval VINF_SUCCESS on success.
2639 * @retval VERR_NOT_IMPLEMENTED if not compiled in.
2640 * @retval VERR_INVALID_STATE if the VM handle is bad.
2641 * @retval VERR_INVALID_PARAMETER if idCpu is out of range.
2642 *
2643 * @param pVM The VM handle.
2644 * @param idCpu The ID of the virtual CPU which times to get.
2645 * @param pcNsTotal Where to store the total run time (nano seconds) of
2646 * the CPU, i.e. the sum of the three other returns.
2647 * Optional.
2648 * @param pcNsExecuting Where to store the time (nano seconds) spent
2649 * executing guest code. Optional.
2650 * @param pcNsHalted Where to store the time (nano seconds) spent
2651 * halted. Optional
2652 * @param pcNsOther Where to store the time (nano seconds) spent
2653 * preempted by the host scheduler, on virtualization
2654 * overhead and on other tasks.
2655 */
2656VMMR3DECL(int) TMR3GetCpuLoadTimes(PVM pVM, VMCPUID idCpu, uint64_t *pcNsTotal, uint64_t *pcNsExecuting,
2657 uint64_t *pcNsHalted, uint64_t *pcNsOther)
2658{
2659 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_STATE);
2660 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
2661
2662#ifndef VBOX_WITHOUT_NS_ACCOUNTING
2663 /*
2664 * Get a stable result set.
2665 * This should be way quicker than an EMT request.
2666 */
2667 PVMCPU pVCpu = &pVM->aCpus[idCpu];
2668 uint32_t uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
2669 uint64_t cNsTotal = pVCpu->tm.s.cNsTotal;
2670 uint64_t cNsExecuting = pVCpu->tm.s.cNsExecuting;
2671 uint64_t cNsHalted = pVCpu->tm.s.cNsHalted;
2672 uint64_t cNsOther = pVCpu->tm.s.cNsOther;
2673 while ( (uTimesGen & 1) /* update in progress */
2674 || uTimesGen != ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen))
2675 {
2676 RTThreadYield();
2677 uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
2678 cNsTotal = pVCpu->tm.s.cNsTotal;
2679 cNsExecuting = pVCpu->tm.s.cNsExecuting;
2680 cNsHalted = pVCpu->tm.s.cNsHalted;
2681 cNsOther = pVCpu->tm.s.cNsOther;
2682 }
2683
2684 /*
2685 * Fill in the return values.
2686 */
2687 if (pcNsTotal)
2688 *pcNsTotal = cNsTotal;
2689 if (pcNsExecuting)
2690 *pcNsExecuting = cNsExecuting;
2691 if (pcNsHalted)
2692 *pcNsHalted = cNsHalted;
2693 if (pcNsOther)
2694 *pcNsOther = cNsOther;
2695
2696 return VINF_SUCCESS;
2697
2698#else
2699 return VERR_NOT_IMPLEMENTED;
2700#endif
2701}
2702
2703#ifndef VBOX_WITHOUT_NS_ACCOUNTING
2704
2705/**
2706 * Helper for tmR3CpuLoadTimer.
2707 * @returns
2708 * @param pState The state to update.
2709 * @param cNsTotalDelta Total time.
2710 * @param cNsExecutingDelta Time executing.
2711 * @param cNsHaltedDelta Time halted.
2712 */
2713DECLINLINE(void) tmR3CpuLoadTimerMakeUpdate(PTMCPULOADSTATE pState,
2714 uint64_t cNsTotal,
2715 uint64_t cNsExecuting,
2716 uint64_t cNsHalted)
2717{
2718 /* Calc deltas */
2719 uint64_t cNsTotalDelta = cNsTotal - pState->cNsPrevTotal;
2720 pState->cNsPrevTotal = cNsTotal;
2721
2722 uint64_t cNsExecutingDelta = cNsExecuting - pState->cNsPrevExecuting;
2723 pState->cNsPrevExecuting = cNsExecuting;
2724
2725 uint64_t cNsHaltedDelta = cNsHalted - pState->cNsPrevHalted;
2726 pState->cNsPrevHalted = cNsHalted;
2727
2728 /* Calc pcts. */
2729 if (!cNsTotalDelta)
2730 {
2731 pState->cPctExecuting = 0;
2732 pState->cPctHalted = 100;
2733 pState->cPctOther = 0;
2734 }
2735 else if (cNsTotalDelta < UINT64_MAX / 4)
2736 {
2737 pState->cPctExecuting = (uint8_t)(cNsExecutingDelta * 100 / cNsTotalDelta);
2738 pState->cPctHalted = (uint8_t)(cNsHaltedDelta * 100 / cNsTotalDelta);
2739 pState->cPctOther = (uint8_t)((cNsTotalDelta - cNsExecutingDelta - cNsHaltedDelta) * 100 / cNsTotalDelta);
2740 }
2741 else
2742 {
2743 pState->cPctExecuting = 0;
2744 pState->cPctHalted = 100;
2745 pState->cPctOther = 0;
2746 }
2747}
2748
2749
2750/**
2751 * Timer callback that calculates the CPU load since the last time it was
2752 * called.
2753 *
2754 * @param pVM The VM handle.
2755 * @param pTimer The timer.
2756 * @param pvUser NULL, unused.
2757 */
2758static DECLCALLBACK(void) tmR3CpuLoadTimer(PVM pVM, PTMTIMER pTimer, void *pvUser)
2759{
2760 /*
2761 * Re-arm the timer first.
2762 */
2763 int rc = TMTimerSetMillies(pTimer, 1000);
2764 AssertLogRelRC(rc);
2765 NOREF(pvUser);
2766
2767 /*
2768 * Update the values for each CPU.
2769 */
2770 uint64_t cNsTotalAll = 0;
2771 uint64_t cNsExecutingAll = 0;
2772 uint64_t cNsHaltedAll = 0;
2773 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2774 {
2775 PVMCPU pVCpu = &pVM->aCpus[iCpu];
2776
2777 /* Try get a stable data set. */
2778 uint32_t cTries = 3;
2779 uint32_t uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
2780 uint64_t cNsTotal = pVCpu->tm.s.cNsTotal;
2781 uint64_t cNsExecuting = pVCpu->tm.s.cNsExecuting;
2782 uint64_t cNsHalted = pVCpu->tm.s.cNsHalted;
2783 while (RT_UNLIKELY( (uTimesGen & 1) /* update in progress */
2784 || uTimesGen != ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen)))
2785 {
2786 if (!--cTries)
2787 break;
2788 ASMNopPause();
2789 uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
2790 cNsTotal = pVCpu->tm.s.cNsTotal;
2791 cNsExecuting = pVCpu->tm.s.cNsExecuting;
2792 cNsHalted = pVCpu->tm.s.cNsHalted;
2793 }
2794
2795 /* Totals */
2796 cNsTotalAll += cNsTotal;
2797 cNsExecutingAll += cNsExecuting;
2798 cNsHaltedAll += cNsHalted;
2799
2800 /* Calc the PCTs and update the state. */
2801 tmR3CpuLoadTimerMakeUpdate(&pVCpu->tm.s.CpuLoad, cNsTotal, cNsExecuting, cNsHalted);
2802 }
2803
2804 /*
2805 * Update the value for all the CPUs.
2806 */
2807 tmR3CpuLoadTimerMakeUpdate(&pVM->tm.s.CpuLoad, cNsTotalAll, cNsExecutingAll, cNsHaltedAll);
2808
2809 /** @todo Try add 1, 5 and 15 min load stats. */
2810
2811}
2812
2813#endif /* !VBOX_WITHOUT_NS_ACCOUNTING */
2814
2815
2816/**
2817 * Display all timers.
2818 *
2819 * @param pVM VM Handle.
2820 * @param pHlp The info helpers.
2821 * @param pszArgs Arguments, ignored.
2822 */
2823static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2824{
2825 NOREF(pszArgs);
2826 pHlp->pfnPrintf(pHlp,
2827 "Timers (pVM=%p)\n"
2828 "%.*s %.*s %.*s %.*s Clock %-18s %-18s %-25s Description\n",
2829 pVM,
2830 sizeof(RTR3PTR) * 2, "pTimerR3 ",
2831 sizeof(int32_t) * 2, "offNext ",
2832 sizeof(int32_t) * 2, "offPrev ",
2833 sizeof(int32_t) * 2, "offSched ",
2834 "Time",
2835 "Expire",
2836 "State");
2837 tmTimerLock(pVM);
2838 for (PTMTIMERR3 pTimer = pVM->tm.s.pCreated; pTimer; pTimer = pTimer->pBigNext)
2839 {
2840 pHlp->pfnPrintf(pHlp,
2841 "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %-25s %s\n",
2842 pTimer,
2843 pTimer->offNext,
2844 pTimer->offPrev,
2845 pTimer->offScheduleNext,
2846 pTimer->enmClock == TMCLOCK_REAL ? "Real " : "Virt ",
2847 TMTimerGet(pTimer),
2848 pTimer->u64Expire,
2849 tmTimerState(pTimer->enmState),
2850 pTimer->pszDesc);
2851 }
2852 tmTimerUnlock(pVM);
2853}
2854
2855
2856/**
2857 * Display all active timers.
2858 *
2859 * @param pVM VM Handle.
2860 * @param pHlp The info helpers.
2861 * @param pszArgs Arguments, ignored.
2862 */
2863static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2864{
2865 NOREF(pszArgs);
2866 pHlp->pfnPrintf(pHlp,
2867 "Active Timers (pVM=%p)\n"
2868 "%.*s %.*s %.*s %.*s Clock %-18s %-18s %-25s Description\n",
2869 pVM,
2870 sizeof(RTR3PTR) * 2, "pTimerR3 ",
2871 sizeof(int32_t) * 2, "offNext ",
2872 sizeof(int32_t) * 2, "offPrev ",
2873 sizeof(int32_t) * 2, "offSched ",
2874 "Time",
2875 "Expire",
2876 "State");
2877 for (unsigned iQueue = 0; iQueue < TMCLOCK_MAX; iQueue++)
2878 {
2879 tmTimerLock(pVM);
2880 for (PTMTIMERR3 pTimer = TMTIMER_GET_HEAD(&pVM->tm.s.paTimerQueuesR3[iQueue]);
2881 pTimer;
2882 pTimer = TMTIMER_GET_NEXT(pTimer))
2883 {
2884 pHlp->pfnPrintf(pHlp,
2885 "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %-25s %s\n",
2886 pTimer,
2887 pTimer->offNext,
2888 pTimer->offPrev,
2889 pTimer->offScheduleNext,
2890 pTimer->enmClock == TMCLOCK_REAL
2891 ? "Real "
2892 : pTimer->enmClock == TMCLOCK_VIRTUAL
2893 ? "Virt "
2894 : pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC
2895 ? "VrSy "
2896 : "TSC ",
2897 TMTimerGet(pTimer),
2898 pTimer->u64Expire,
2899 tmTimerState(pTimer->enmState),
2900 pTimer->pszDesc);
2901 }
2902 tmTimerUnlock(pVM);
2903 }
2904}
2905
2906
2907/**
2908 * Display all clocks.
2909 *
2910 * @param pVM VM Handle.
2911 * @param pHlp The info helpers.
2912 * @param pszArgs Arguments, ignored.
2913 */
2914static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2915{
2916 NOREF(pszArgs);
2917
2918 /*
2919 * Read the times first to avoid more than necessary time variation.
2920 */
2921 const uint64_t u64Virtual = TMVirtualGet(pVM);
2922 const uint64_t u64VirtualSync = TMVirtualSyncGet(pVM);
2923 const uint64_t u64Real = TMRealGet(pVM);
2924
2925 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2926 {
2927 PVMCPU pVCpu = &pVM->aCpus[i];
2928 uint64_t u64TSC = TMCpuTickGet(pVCpu);
2929
2930 /*
2931 * TSC
2932 */
2933 pHlp->pfnPrintf(pHlp,
2934 "Cpu Tick: %18RU64 (%#016RX64) %RU64Hz %s%s",
2935 u64TSC, u64TSC, TMCpuTicksPerSecond(pVM),
2936 pVCpu->tm.s.fTSCTicking ? "ticking" : "paused",
2937 pVM->tm.s.fTSCVirtualized ? " - virtualized" : "");
2938 if (pVM->tm.s.fTSCUseRealTSC)
2939 {
2940 pHlp->pfnPrintf(pHlp, " - real tsc");
2941 if (pVCpu->tm.s.offTSCRawSrc)
2942 pHlp->pfnPrintf(pHlp, "\n offset %RU64", pVCpu->tm.s.offTSCRawSrc);
2943 }
2944 else
2945 pHlp->pfnPrintf(pHlp, " - virtual clock");
2946 pHlp->pfnPrintf(pHlp, "\n");
2947 }
2948
2949 /*
2950 * virtual
2951 */
2952 pHlp->pfnPrintf(pHlp,
2953 " Virtual: %18RU64 (%#016RX64) %RU64Hz %s",
2954 u64Virtual, u64Virtual, TMVirtualGetFreq(pVM),
2955 pVM->tm.s.cVirtualTicking ? "ticking" : "paused");
2956 if (pVM->tm.s.fVirtualWarpDrive)
2957 pHlp->pfnPrintf(pHlp, " WarpDrive %RU32 %%", pVM->tm.s.u32VirtualWarpDrivePercentage);
2958 pHlp->pfnPrintf(pHlp, "\n");
2959
2960 /*
2961 * virtual sync
2962 */
2963 pHlp->pfnPrintf(pHlp,
2964 "VirtSync: %18RU64 (%#016RX64) %s%s",
2965 u64VirtualSync, u64VirtualSync,
2966 pVM->tm.s.fVirtualSyncTicking ? "ticking" : "paused",
2967 pVM->tm.s.fVirtualSyncCatchUp ? " - catchup" : "");
2968 if (pVM->tm.s.offVirtualSync)
2969 {
2970 pHlp->pfnPrintf(pHlp, "\n offset %RU64", pVM->tm.s.offVirtualSync);
2971 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage)
2972 pHlp->pfnPrintf(pHlp, " catch-up rate %u %%", pVM->tm.s.u32VirtualSyncCatchUpPercentage);
2973 }
2974 pHlp->pfnPrintf(pHlp, "\n");
2975
2976 /*
2977 * real
2978 */
2979 pHlp->pfnPrintf(pHlp,
2980 " Real: %18RU64 (%#016RX64) %RU64Hz\n",
2981 u64Real, u64Real, TMRealGetFreq(pVM));
2982}
2983
Note: See TracBrowser for help on using the repository browser.

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