VirtualBox

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

Last change on this file since 1876 was 1876, checked in by vboxsync, 18 years ago

VBOX_TM_VIRTUALIZED_TSC hack.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 48.9 KB
Line 
1/* $Id: TM.cpp 1876 2007-04-03 11:59:17Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/** @page pg_tm TM - The Time Manager
24 *
25 * The Time Manager abstracts the CPU clocks and manages timers used by VM device.
26 *
27 *
28 *
29 * @section sec_tm_timers Timers
30 *
31 * The timers supports multiple clocks. Currently there are two clocks in the
32 * TM, the host real time clock and the guest virtual clock. Each clock has it's
33 * own set of scheduling facilities which are identical but for the clock source.
34 *
35 * Take one such timer scheduling facility, or timer queue if you like. There are
36 * a few factors which makes it a bit complex. First there is the usual GC vs. HC
37 * thing. Then there is multiple threads, and then there is the fact that on Unix
38 * we might just as well take a timer signal which checks whether it's wise to
39 * schedule timers while we're scheduling them. On API level, all but the create
40 * and save APIs must be mulithreaded.
41 *
42 * The design is using a doubly linked HC list of active timers which is ordered
43 * by expire date. Updates to the list is batched in a singly linked list (linked
44 * by handle not pointer for atomically update support in both GC and HC) and
45 * will be processed by the emulation thread.
46 *
47 * For figuring out when there is need to schedule timers a high frequency
48 * asynchronous timer is employed using Host OS services. Its task is to check if
49 * there are anything batched up or if a head has expired. If this is the case
50 * a forced action is signals and the emulation thread will process this ASAP.
51 *
52 */
53
54
55
56
57/*******************************************************************************
58* Header Files *
59*******************************************************************************/
60#define LOG_GROUP LOG_GROUP_TM
61#include <VBox/tm.h>
62#include <VBox/vmm.h>
63#include <VBox/mm.h>
64#include <VBox/ssm.h>
65#include <VBox/dbgf.h>
66#include <VBox/rem.h>
67#include "TMInternal.h"
68#include <VBox/vm.h>
69
70#include <VBox/param.h>
71#include <VBox/err.h>
72
73#include <VBox/log.h>
74#include <iprt/asm.h>
75#include <iprt/assert.h>
76#include <iprt/thread.h>
77#include <iprt/time.h>
78#include <iprt/timer.h>
79#include <iprt/semaphore.h>
80#include <iprt/string.h>
81#include <iprt/env.h>
82
83
84/*******************************************************************************
85* Defined Constants And Macros *
86*******************************************************************************/
87/** The current saved state version.*/
88#define TM_SAVED_STATE_VERSION 2
89
90
91/*******************************************************************************
92* Internal Functions *
93*******************************************************************************/
94static uint64_t tmR3Calibrate(void);
95static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM);
96static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
97static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser);
98static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue);
99static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
100static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
101static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
102
103
104/**
105 * Internal function for getting the clock time.
106 *
107 * @returns clock time.
108 * @param pVM The VM handle.
109 * @param enmClock The clock.
110 */
111DECLINLINE(uint64_t) tmClock(PVM pVM, TMCLOCK enmClock)
112{
113 switch (enmClock)
114 {
115 case TMCLOCK_VIRTUAL: return TMVirtualGet(pVM);
116 case TMCLOCK_VIRTUAL_SYNC: return TMVirtualGetSync(pVM);
117 case TMCLOCK_REAL: return TMRealGet(pVM);
118 case TMCLOCK_TSC: return TMCpuTickGet(pVM);
119 default:
120 AssertMsgFailed(("enmClock=%d\n", enmClock));
121 return ~(uint64_t)0;
122 }
123}
124
125
126/**
127 * Initializes the TM.
128 *
129 * @returns VBox status code.
130 * @param pVM The VM to operate on.
131 */
132TMR3DECL(int) TMR3Init(PVM pVM)
133{
134 LogFlow(("TMR3Init:\n"));
135
136 /*
137 * Assert alignment and sizes.
138 */
139 AssertRelease(!(RT_OFFSETOF(VM, tm.s) & 31));
140 AssertRelease(sizeof(pVM->tm.s) <= sizeof(pVM->tm.padding));
141
142 /*
143 * Init the structure.
144 */
145 void *pv;
146 int rc = MMHyperAlloc(pVM, sizeof(pVM->tm.s.paTimerQueuesR3[0]) * TMCLOCK_MAX, 0, MM_TAG_TM, &pv);
147 AssertRCReturn(rc, rc);
148 pVM->tm.s.paTimerQueuesR3 = (PTMTIMERQUEUE)pv;
149
150 pVM->tm.s.offVM = RT_OFFSETOF(VM, tm.s);
151 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].enmClock = TMCLOCK_VIRTUAL;
152 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].u64Expire = INT64_MAX;
153 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].enmClock = TMCLOCK_VIRTUAL_SYNC;
154 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].u64Expire = INT64_MAX;
155 pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].enmClock = TMCLOCK_REAL;
156 pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].u64Expire = INT64_MAX;
157 pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].enmClock = TMCLOCK_TSC;
158 pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].u64Expire = INT64_MAX;
159
160 /*
161 * We indirectly - thru RTTimeNanoTS and RTTimeMilliTS - use the global
162 * info page (GIP) for both the virtual and the real clock. By mapping
163 * the GIP into guest context we can get just as accurate time even there.
164 * All that's required is that the g_pSUPGlobalInfoPage symbol is available
165 * to the GC Runtime.
166 */
167 pVM->tm.s.pvGIPR3 = (void *)g_pSUPGlobalInfoPage;
168 AssertMsgReturn(pVM->tm.s.pvGIPR3, ("GIP support is now required!\n"), VERR_INTERNAL_ERROR);
169 RTHCPHYS HCPhysGIP;
170 rc = SUPGipGetPhys(&HCPhysGIP);
171 AssertMsgRCReturn(rc, ("Failed to get GIP physical address!\n"), rc);
172
173 rc = MMR3HyperMapHCPhys(pVM, pVM->tm.s.pvGIPR3, HCPhysGIP, PAGE_SIZE, "GIP", &pVM->tm.s.pvGIPGC);
174 if (VBOX_FAILURE(rc))
175 {
176 AssertMsgFailed(("Failed to map GIP into GC, rc=%Vrc!\n", rc));
177 return rc;
178 }
179 LogFlow(("TMR3Init: HCPhysGIP=%RHp at %VGv\n", HCPhysGIP, pVM->tm.s.pvGIPGC));
180 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
181
182 /*
183 * Determin the TSC configuration and frequency.
184 */
185 /* mode */
186 rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "TSCVirtualized", &pVM->tm.s.fTSCVirtualized);
187 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
188#if 0 /* seems to kind of work... */
189 pVM->tm.s.fTSCVirtualized = true;
190#else
191 pVM->tm.s.fTSCVirtualized = false;
192#endif
193 else if (VBOX_FAILURE(rc))
194 return VMSetError(pVM, rc, RT_SRC_POS,
195 N_("Configuration error: Failed to querying bool value \"UseRealTSC\". (%Vrc)"), rc);
196
197 /* source */
198 rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "UseRealTSC", &pVM->tm.s.fTSCTicking);
199 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
200#if 0 /* doesn't seem to work reliably yet... xp takes several ~2 min to shutdown now. darn. */
201 pVM->tm.s.fTSCUseRealTSC = false; /* virtualize it */
202#else
203 pVM->tm.s.fTSCUseRealTSC = true; /* don't virtualize it */
204#endif
205 else if (VBOX_FAILURE(rc))
206 return VMSetError(pVM, rc, RT_SRC_POS,
207 N_("Configuration error: Failed to querying bool value \"UseRealTSC\". (%Vrc)"), rc);
208#if 1 /* temporary hack */
209 if (RTEnvExist("VBOX_TM_VIRTUALIZED_TSC"))
210 pVM->tm.s.fTSCUseRealTSC = false;
211#endif
212 if (!pVM->tm.s.fTSCUseRealTSC)
213 pVM->tm.s.fTSCVirtualized = true;
214
215 /* frequency */
216 rc = CFGMR3QueryU64(CFGMR3GetRoot(pVM), "TSCTicksPerSecond", &pVM->tm.s.cTSCTicksPerSecond);
217 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
218 {
219#if 0 /* when tmCpuTickGetRawVirtual is done */
220 pVM->tm.s.cTSCTicksPerSecond = tmR3Calibrate();
221#else
222 if (pVM->tm.s.fTSCUseRealTSC)
223 pVM->tm.s.cTSCTicksPerSecond = tmR3Calibrate();
224 else
225 pVM->tm.s.cTSCTicksPerSecond = TMCLOCK_FREQ_VIRTUAL;/* same as the virtual clock. */
226#endif
227 }
228 else if (VBOX_FAILURE(rc))
229 return VMSetError(pVM, rc, RT_SRC_POS,
230 N_("Configuration error: Failed to querying uint64_t value \"TSCTicksPerSecond\". (%Vrc)"), rc);
231#if 0 /* when tmCpuTickGetRawVirtual is done */
232 else if ( pVM->tm.s.cTSCTicksPerSecond < _1M
233 || pVM->tm.s.cTSCTicksPerSecond > _1E)
234 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
235 N_("Configuration error: \"TSCTicksPerSecond\" = %RI64 is not in the range 1MHz..1EHz!"),
236 pVM->tm.s.cTSCTicksPerSecond);
237#else
238 else if (pVM->tm.s.cTSCTicksPerSecond != TMCLOCK_FREQ_VIRTUAL)
239 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
240 N_("Configuration error: \"TSCTicksPerSecond\" = %RI64 is not 1GHz! (temporary restriction)"),
241 pVM->tm.s.cTSCTicksPerSecond);
242#endif
243 else
244 {
245 pVM->tm.s.fTSCUseRealTSC = false;
246 pVM->tm.s.fTSCVirtualized = true;
247 }
248
249 /* setup and report */
250 if (pVM->tm.s.fTSCUseRealTSC)
251 CPUMR3SetCR4Feature(pVM, 0, ~X86_CR4_TSD);
252 else
253 CPUMR3SetCR4Feature(pVM, X86_CR4_TSD, ~X86_CR4_TSD);
254 LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool\n",
255 pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC));
256
257 /*
258 * Register saved state.
259 */
260 rc = SSMR3RegisterInternal(pVM, "tm", 1, TM_SAVED_STATE_VERSION, sizeof(uint64_t) * 8,
261 NULL, tmR3Save, NULL,
262 NULL, tmR3Load, NULL);
263 if (VBOX_FAILURE(rc))
264 return rc;
265
266 /*
267 * Setup the warp drive.
268 */
269 rc = CFGMR3QueryU32(CFGMR3GetRoot(pVM), "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage);
270 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
271 pVM->tm.s.u32VirtualWarpDrivePercentage = 100;
272 else if (VBOX_FAILURE(rc))
273 return VMSetError(pVM, rc, RT_SRC_POS,
274 N_("Configuration error: Failed to querying uint32_t value \"WarpDrivePercent\". (%Vrc)"), rc);
275 else if ( pVM->tm.s.u32VirtualWarpDrivePercentage < 2
276 || pVM->tm.s.u32VirtualWarpDrivePercentage > 20000)
277 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
278 N_("Configuration error: \"WarpDrivePercent\" = %RI32 is not in the range 2..20000!"),
279 pVM->tm.s.u32VirtualWarpDrivePercentage);
280 pVM->tm.s.fVirtualWarpDrive = pVM->tm.s.u32VirtualWarpDrivePercentage != 100;
281 if (pVM->tm.s.fVirtualWarpDrive)
282 LogRel(("TM: u32VirtualWarpDrivePercentage=%RI32\n", pVM->tm.s.u32VirtualWarpDrivePercentage));
283
284 /*
285 * Start the timer (guard against REM not yielding).
286 */
287 uint32_t u32Millies;
288 rc = CFGMR3QueryU32(CFGMR3GetRoot(pVM), "TimerMillies", &u32Millies);
289 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
290 u32Millies = 10;
291 else if (VBOX_FAILURE(rc))
292 return VMSetError(pVM, rc, RT_SRC_POS,
293 N_("Configuration error: Failed to query uint32_t value \"TimerMillies\", rc=%Vrc.\n"), rc);
294 rc = RTTimerCreate(&pVM->tm.s.pTimer, u32Millies, tmR3TimerCallback, pVM);
295 if (VBOX_FAILURE(rc))
296 {
297 AssertMsgFailed(("Failed to create timer, u32Millies=%d rc=%Vrc.\n", u32Millies, rc));
298 return rc;
299 }
300 Log(("TM: Created timer %p firing every %d millieseconds\n", pVM->tm.s.pTimer, u32Millies));
301 pVM->tm.s.u32TimerMillies = u32Millies;
302
303#ifdef VBOX_WITH_STATISTICS
304 /*
305 * Register statistics.
306 */
307 STAM_REG(pVM, &pVM->tm.s.StatDoQueues, STAMTYPE_PROFILE, "/TM/DoQueues", STAMUNIT_TICKS_PER_CALL, "Profiling timer TMR3TimerQueuesDo.");
308 STAM_REG(pVM, &pVM->tm.s.StatDoQueuesSchedule, STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Schedule",STAMUNIT_TICKS_PER_CALL, "The scheduling part.");
309 STAM_REG(pVM, &pVM->tm.s.StatDoQueuesRun, STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Run", STAMUNIT_TICKS_PER_CALL, "The run part.");
310
311 STAM_REG(pVM, &pVM->tm.s.StatPollAlreadySet, STAMTYPE_COUNTER, "/TM/PollAlreadySet", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the FF was already set.");
312 STAM_REG(pVM, &pVM->tm.s.StatPollVirtual, STAMTYPE_COUNTER, "/TM/PollHitsVirtual", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL queue.");
313 STAM_REG(pVM, &pVM->tm.s.StatPollVirtualSync, STAMTYPE_COUNTER, "/TM/PollHitsVirtualSync",STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL_SYNC queue.");
314 STAM_REG(pVM, &pVM->tm.s.StatPollMiss, STAMTYPE_COUNTER, "/TM/PollMiss", STAMUNIT_OCCURENCES, "TMTimerPoll calls where nothing had expired.");
315
316 STAM_REG(pVM, &pVM->tm.s.StatPostponedR3, STAMTYPE_COUNTER, "/TM/PostponedR3", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-3.");
317 STAM_REG(pVM, &pVM->tm.s.StatPostponedR0, STAMTYPE_COUNTER, "/TM/PostponedR0", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-0.");
318 STAM_REG(pVM, &pVM->tm.s.StatPostponedGC, STAMTYPE_COUNTER, "/TM/PostponedGC", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in GC.");
319
320 STAM_REG(pVM, &pVM->tm.s.StatScheduleOneGC, STAMTYPE_PROFILE, "/TM/ScheduleOneGC", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.\n");
321 STAM_REG(pVM, &pVM->tm.s.StatScheduleOneR0, STAMTYPE_PROFILE, "/TM/ScheduleOneR0", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.\n");
322 STAM_REG(pVM, &pVM->tm.s.StatScheduleOneR3, STAMTYPE_PROFILE, "/TM/ScheduleOneR3", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.\n");
323 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.");
324
325 STAM_REG(pVM, &pVM->tm.s.StatTimerSetGC, STAMTYPE_PROFILE, "/TM/TimerSetGC", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in GC.");
326 STAM_REG(pVM, &pVM->tm.s.StatTimerSetR0, STAMTYPE_PROFILE, "/TM/TimerSetR0", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-0.");
327 STAM_REG(pVM, &pVM->tm.s.StatTimerSetR3, STAMTYPE_PROFILE, "/TM/TimerSetR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-3.");
328
329 STAM_REG(pVM, &pVM->tm.s.StatTimerStopGC, STAMTYPE_PROFILE, "/TM/TimerStopGC", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in GC.");
330 STAM_REG(pVM, &pVM->tm.s.StatTimerStopR0, STAMTYPE_PROFILE, "/TM/TimerStopR0", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-0.");
331 STAM_REG(pVM, &pVM->tm.s.StatTimerStopR3, STAMTYPE_PROFILE, "/TM/TimerStopR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-3.");
332
333 STAM_REG(pVM, &pVM->tm.s.StatVirtualGet, STAMTYPE_COUNTER, "/TM/VirtualGet", STAMUNIT_OCCURENCES, "The number of times TMR3TimerGet was called when the clock was running.");
334 STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSync, STAMTYPE_COUNTER, "/TM/VirtualGetSync", STAMUNIT_OCCURENCES, "The number of times TMR3TimerGetSync was called when the clock was running.");
335 STAM_REG(pVM, &pVM->tm.s.StatVirtualPause, STAMTYPE_COUNTER, "/TM/VirtualPause", STAMUNIT_OCCURENCES, "The number of times TMR3TimerPause was called.");
336 STAM_REG(pVM, &pVM->tm.s.StatVirtualResume, STAMTYPE_COUNTER, "/TM/VirtualResume", STAMUNIT_OCCURENCES, "The number of times TMR3TimerResume was called.");
337
338 STAM_REG(pVM, &pVM->tm.s.StatTimerCallbackSetFF,STAMTYPE_COUNTER, "/TM/CallbackSetFF", STAMUNIT_OCCURENCES, "The number of times the timer callback set FF.");
339#endif /* VBOX_WITH_STATISTICS */
340
341 /*
342 * Register info handlers.
343 */
344 DBGFR3InfoRegisterInternal(pVM, "timers", "Dumps all timers. No arguments.", tmR3TimerInfo);
345 DBGFR3InfoRegisterInternal(pVM, "activetimers", "Dumps active all timers. No arguments.", tmR3TimerInfoActive);
346 DBGFR3InfoRegisterInternal(pVM, "clocks", "Display the time of the various clocks.", tmR3InfoClocks);
347
348 return VINF_SUCCESS;
349}
350
351
352/**
353 * Calibrate the CPU tick.
354 *
355 * @returns Number of ticks per second.
356 */
357static uint64_t tmR3Calibrate(void)
358{
359 /*
360 * Use GIP when available present.
361 */
362 uint64_t u64Hz;
363 PCSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
364 if ( pGip
365 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC)
366 {
367 unsigned iCpu = pGip->u32Mode != SUPGIPMODE_ASYNC_TSC ? 0 : ASMGetApicId();
368 if (iCpu >= RT_ELEMENTS(pGip->aCPUs))
369 AssertReleaseMsgFailed(("iCpu=%d - the ApicId is too high. send VBox.log and hardware specs!\n", iCpu));
370 else
371 {
372 RTThreadSleep(32); /* To preserve old behaviour and to get a good CpuHz at startup. */
373 pGip = g_pSUPGlobalInfoPage;
374 if ( pGip
375 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
376 && (u64Hz = pGip->aCPUs[iCpu].u64CpuHz)
377 && u64Hz != ~(uint64_t)0)
378 return u64Hz;
379 }
380 }
381
382 /* call this once first to make sure it's initialized. */
383 RTTimeNanoTS();
384
385 /*
386 * Yield the CPU to increase our chances of getting
387 * a correct value.
388 */
389 RTThreadYield(); /* Try avoid interruptions between TSC and NanoTS samplings. */
390 static const unsigned s_auSleep[5] = { 50, 30, 30, 40, 40 };
391 uint64_t au64Samples[5];
392 unsigned i;
393 for (i = 0; i < ELEMENTS(au64Samples); i++)
394 {
395 unsigned cMillies;
396 int cTries = 5;
397 uint64_t u64Start = ASMReadTSC();
398 uint64_t u64End;
399 uint64_t StartTS = RTTimeNanoTS();
400 uint64_t EndTS;
401 do
402 {
403 RTThreadSleep(s_auSleep[i]);
404 u64End = ASMReadTSC();
405 EndTS = RTTimeNanoTS();
406 cMillies = (unsigned)((EndTS - StartTS + 500000) / 1000000);
407 } while ( cMillies == 0 /* the sleep may be interrupted... */
408 || (cMillies < 20 && --cTries > 0));
409 uint64_t u64Diff = u64End - u64Start;
410
411 au64Samples[i] = (u64Diff * 1000) / cMillies;
412 AssertMsg(cTries > 0, ("cMillies=%d i=%d\n", cMillies, i));
413 }
414
415 /*
416 * Discard the highest and lowest results and calculate the average.
417 */
418 unsigned iHigh = 0;
419 unsigned iLow = 0;
420 for (i = 1; i < ELEMENTS(au64Samples); i++)
421 {
422 if (au64Samples[i] < au64Samples[iLow])
423 iLow = i;
424 if (au64Samples[i] > au64Samples[iHigh])
425 iHigh = i;
426 }
427 au64Samples[iLow] = 0;
428 au64Samples[iHigh] = 0;
429
430 u64Hz = au64Samples[0];
431 for (i = 1; i < ELEMENTS(au64Samples); i++)
432 u64Hz += au64Samples[i];
433 u64Hz /= ELEMENTS(au64Samples) - 2;
434
435 return u64Hz;
436}
437
438
439/**
440 * Applies relocations to data and code managed by this
441 * component. This function will be called at init and
442 * whenever the VMM need to relocate it self inside the GC.
443 *
444 * @param pVM The VM.
445 * @param offDelta Relocation delta relative to old location.
446 */
447TMR3DECL(void) TMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
448{
449 LogFlow(("TMR3Relocate\n"));
450 pVM->tm.s.pvGIPGC = MMHyperR3ToGC(pVM, pVM->tm.s.pvGIPR3);
451 pVM->tm.s.paTimerQueuesGC = MMHyperR3ToGC(pVM, pVM->tm.s.paTimerQueuesR3);
452 pVM->tm.s.paTimerQueuesR0 = MMHyperR3ToR0(pVM, pVM->tm.s.paTimerQueuesR3);
453
454 /*
455 * Iterate the timers updating the pVMGC pointers.
456 */
457 for (PTMTIMER pTimer = pVM->tm.s.pCreated; pTimer; pTimer = pTimer->pBigNext)
458 {
459 pTimer->pVMGC = pVM->pVMGC;
460 pTimer->pVMR0 = (PVMR0)pVM->pVMHC; /// @todo pTimer->pVMR0 = pVM->pVMR0;
461 }
462}
463
464
465/**
466 * Terminates the TM.
467 *
468 * Termination means cleaning up and freeing all resources,
469 * the VM it self is at this point powered off or suspended.
470 *
471 * @returns VBox status code.
472 * @param pVM The VM to operate on.
473 */
474TMR3DECL(int) TMR3Term(PVM pVM)
475{
476 AssertMsg(pVM->tm.s.offVM, ("bad init order!\n"));
477 if (pVM->tm.s.pTimer)
478 {
479 int rc = RTTimerDestroy(pVM->tm.s.pTimer);
480 AssertRC(rc);
481 pVM->tm.s.pTimer = NULL;
482 }
483
484 return VINF_SUCCESS;
485}
486
487
488/**
489 * The VM is being reset.
490 *
491 * For the TM component this means that a rescheduling is preformed,
492 * the FF is cleared and but without running the queues. We'll have to
493 * check if this makes sense or not, but it seems like a good idea now....
494 *
495 * @param pVM VM handle.
496 */
497TMR3DECL(void) TMR3Reset(PVM pVM)
498{
499 LogFlow(("TMR3Reset:\n"));
500 VM_ASSERT_EMT(pVM);
501
502 /*
503 * Process the queues.
504 */
505 for (int i = 0; i < TMCLOCK_MAX; i++)
506 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[i]);
507#ifdef VBOX_STRICT
508 tmTimerQueuesSanityChecks(pVM, "TMR3Reset");
509#endif
510 VM_FF_CLEAR(pVM, VM_FF_TIMER);
511}
512
513
514/**
515 * Resolve a builtin GC symbol.
516 * Called by PDM when loading or relocating GC modules.
517 *
518 * @returns VBox status
519 * @param pVM VM Handle.
520 * @param pszSymbol Symbol to resolv
521 * @param pGCPtrValue Where to store the symbol value.
522 * @remark This has to work before TMR3Relocate() is called.
523 */
524TMR3DECL(int) TMR3GetImportGC(PVM pVM, const char *pszSymbol, PRTGCPTR pGCPtrValue)
525{
526 if (!strcmp(pszSymbol, "g_pSUPGlobalInfoPage"))
527 *pGCPtrValue = MMHyperHC2GC(pVM, &pVM->tm.s.pvGIPGC);
528 //else if (..)
529 else
530 return VERR_SYMBOL_NOT_FOUND;
531 return VINF_SUCCESS;
532}
533
534
535/**
536 * Execute state save operation.
537 *
538 * @returns VBox status code.
539 * @param pVM VM Handle.
540 * @param pSSM SSM operation handle.
541 */
542static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM)
543{
544 LogFlow(("tmR3Save:\n"));
545 Assert(!pVM->tm.s.fTSCTicking);
546 Assert(!pVM->tm.s.fVirtualTicking);
547 Assert(!pVM->tm.s.fVirtualSyncTicking);
548
549 /*
550 * Save the virtual clocks.
551 */
552 /* the virtual clock. */
553 SSMR3PutU64(pSSM, TMCLOCK_FREQ_VIRTUAL);
554 SSMR3PutU64(pSSM, pVM->tm.s.u64Virtual);
555
556 /* the virtual timer synchronous clock. */
557 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSync);
558 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSyncOffset);
559 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSyncCatchUpPrev);
560 SSMR3PutBool(pSSM, pVM->tm.s.fVirtualSyncCatchUp);
561
562 /* real time clock */
563 SSMR3PutU64(pSSM, TMCLOCK_FREQ_REAL);
564
565 /* the cpu tick clock. */
566 SSMR3PutU64(pSSM, TMCpuTickGet(pVM));
567 return SSMR3PutU64(pSSM, pVM->tm.s.cTSCTicksPerSecond);
568}
569
570
571/**
572 * Execute state load operation.
573 *
574 * @returns VBox status code.
575 * @param pVM VM Handle.
576 * @param pSSM SSM operation handle.
577 * @param u32Version Data layout version.
578 */
579static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
580{
581 LogFlow(("tmR3Load:\n"));
582 Assert(!pVM->tm.s.fTSCTicking);
583 Assert(!pVM->tm.s.fVirtualTicking);
584 Assert(!pVM->tm.s.fVirtualSyncTicking);
585
586 /*
587 * Validate version.
588 */
589 if (u32Version != TM_SAVED_STATE_VERSION)
590 {
591 Log(("tmR3Load: Invalid version u32Version=%d!\n", u32Version));
592 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
593 }
594
595 /*
596 * Load the virtual clock.
597 */
598 pVM->tm.s.fVirtualTicking = false;
599 /* the virtual clock. */
600 uint64_t u64Hz;
601 int rc = SSMR3GetU64(pSSM, &u64Hz);
602 if (VBOX_FAILURE(rc))
603 return rc;
604 if (u64Hz != TMCLOCK_FREQ_VIRTUAL)
605 {
606 AssertMsgFailed(("The virtual clock frequency differs! Saved: %RU64 Binary: %RU64\n",
607 u64Hz, TMCLOCK_FREQ_VIRTUAL));
608 return VERR_SSM_VIRTUAL_CLOCK_HZ;
609 }
610 SSMR3GetU64(pSSM, &pVM->tm.s.u64Virtual);
611 pVM->tm.s.u64VirtualOffset = 0;
612
613 /* the virtual timer synchronous clock. */
614 pVM->tm.s.fVirtualSyncTicking = false;
615 SSMR3GetU64(pSSM, &pVM->tm.s.u64VirtualSync);
616 uint64_t u64;
617 SSMR3GetU64(pSSM, &u64);
618 pVM->tm.s.u64VirtualSyncOffset = u64;
619 SSMR3GetU64(pSSM, &u64);
620 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64;
621 bool f;
622 SSMR3GetBool(pSSM, &f);
623 pVM->tm.s.fVirtualSyncCatchUp = f;
624
625 /* the real clock */
626 rc = SSMR3GetU64(pSSM, &u64Hz);
627 if (VBOX_FAILURE(rc))
628 return rc;
629 if (u64Hz != TMCLOCK_FREQ_REAL)
630 {
631 AssertMsgFailed(("The real clock frequency differs! Saved: %RU64 Binary: %RU64\n",
632 u64Hz, TMCLOCK_FREQ_REAL));
633 return VERR_SSM_VIRTUAL_CLOCK_HZ; /* missleading... */
634 }
635
636 /* the cpu tick clock. */
637 pVM->tm.s.fTSCTicking = false;
638 rc = SSMR3GetU64(pSSM, &u64Hz);
639 if (VBOX_FAILURE(rc))
640 return rc;
641 SSMR3GetU64(pSSM, &pVM->tm.s.u64TSC);
642 /** @todo check TSC frequency and virtualize the TSC properly! */
643 pVM->tm.s.u64TSCOffset = 0;
644
645 /*
646 * Make sure timers get rescheduled immediately.
647 */
648 VM_FF_SET(pVM, VM_FF_TIMER);
649
650 return VINF_SUCCESS;
651}
652
653
654/** @todo doc */
655static int tmr3TimerCreate(PVM pVM, TMCLOCK enmClock, const char *pszDesc, PPTMTIMERHC ppTimer)
656{
657 VM_ASSERT_EMT(pVM);
658
659 /*
660 * Allocate the timer.
661 */
662 PTMTIMERHC pTimer = NULL;
663 if (pVM->tm.s.pFree && VM_IS_EMT(pVM))
664 {
665 pTimer = pVM->tm.s.pFree;
666 pVM->tm.s.pFree = pTimer->pBigNext;
667 Log3(("TM: Recycling timer %p, new free head %p.\n", pTimer, pTimer->pBigNext));
668 }
669
670 if (!pTimer)
671 {
672 int rc = MMHyperAlloc(pVM, sizeof(*pTimer), 0, MM_TAG_TM, (void **)&pTimer);
673 if (VBOX_FAILURE(rc))
674 return rc;
675 Log3(("TM: Allocated new timer %p\n", pTimer));
676 }
677
678 /*
679 * Initialize it.
680 */
681 pTimer->u64Expire = 0;
682 pTimer->enmClock = enmClock;
683 pTimer->pVMR3 = pVM;
684 pTimer->pVMR0 = (PVMR0)pVM->pVMHC; /// @todo pTimer->pVMR0 = pVM->pVMR0;
685 pTimer->pVMGC = pVM->pVMGC;
686 pTimer->enmState = TMTIMERSTATE_STOPPED;
687 pTimer->offScheduleNext = 0;
688 pTimer->offNext = 0;
689 pTimer->offPrev = 0;
690 pTimer->pszDesc = pszDesc;
691
692 /* insert into the list of created timers. */
693 pTimer->pBigPrev = NULL;
694 pTimer->pBigNext = pVM->tm.s.pCreated;
695 pVM->tm.s.pCreated = pTimer;
696 if (pTimer->pBigNext)
697 pTimer->pBigNext->pBigPrev = pTimer;
698#ifdef VBOX_STRICT
699 tmTimerQueuesSanityChecks(pVM, "tmR3TimerCreate");
700#endif
701
702 *ppTimer = pTimer;
703 return VINF_SUCCESS;
704}
705
706
707/**
708 * Creates a device timer.
709 *
710 * @returns VBox status.
711 * @param pVM The VM to create the timer in.
712 * @param pDevIns Device instance.
713 * @param enmClock The clock to use on this timer.
714 * @param pfnCallback Callback function.
715 * @param pszDesc Pointer to description string which must stay around
716 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
717 * @param ppTimer Where to store the timer on success.
718 */
719TMR3DECL(int) TMR3TimerCreateDevice(PVM pVM, PPDMDEVINS pDevIns, TMCLOCK enmClock, PFNTMTIMERDEV pfnCallback, const char *pszDesc, PPTMTIMERHC ppTimer)
720{
721 /*
722 * Allocate and init stuff.
723 */
724 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
725 if (VBOX_SUCCESS(rc))
726 {
727 (*ppTimer)->enmType = TMTIMERTYPE_DEV;
728 (*ppTimer)->u.Dev.pfnTimer = pfnCallback;
729 (*ppTimer)->u.Dev.pDevIns = pDevIns;
730 Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
731 }
732
733 return rc;
734}
735
736
737/**
738 * Creates a driver timer.
739 *
740 * @returns VBox status.
741 * @param pVM The VM to create the timer in.
742 * @param pDrvIns Driver instance.
743 * @param enmClock The clock to use on this timer.
744 * @param pfnCallback Callback function.
745 * @param pszDesc Pointer to description string which must stay around
746 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
747 * @param ppTimer Where to store the timer on success.
748 */
749TMR3DECL(int) TMR3TimerCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, const char *pszDesc, PPTMTIMERHC ppTimer)
750{
751 /*
752 * Allocate and init stuff.
753 */
754 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
755 if (VBOX_SUCCESS(rc))
756 {
757 (*ppTimer)->enmType = TMTIMERTYPE_DRV;
758 (*ppTimer)->u.Drv.pfnTimer = pfnCallback;
759 (*ppTimer)->u.Drv.pDrvIns = pDrvIns;
760 Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
761 }
762
763 return rc;
764}
765
766
767/**
768 * Creates an internal timer.
769 *
770 * @returns VBox status.
771 * @param pVM The VM to create the timer in.
772 * @param enmClock The clock to use on this timer.
773 * @param pfnCallback Callback function.
774 * @param pvUser User argument to be passed to the callback.
775 * @param pszDesc Pointer to description string which must stay around
776 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
777 * @param ppTimer Where to store the timer on success.
778 */
779TMR3DECL(int) TMR3TimerCreateInternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMERINT pfnCallback, void *pvUser, const char *pszDesc, PPTMTIMERHC ppTimer)
780{
781 /*
782 * Allocate and init stuff.
783 */
784 PTMTIMER pTimer;
785 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, &pTimer);
786 if (VBOX_SUCCESS(rc))
787 {
788 pTimer->enmType = TMTIMERTYPE_INTERNAL;
789 pTimer->u.Internal.pfnTimer = pfnCallback;
790 pTimer->u.Internal.pvUser = pvUser;
791 *ppTimer = pTimer;
792 Log(("TM: Created internal timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
793 }
794
795 return rc;
796}
797
798/**
799 * Creates an external timer.
800 *
801 * @returns Timer handle on success.
802 * @returns NULL on failure.
803 * @param pVM The VM to create the timer in.
804 * @param enmClock The clock to use on this timer.
805 * @param pfnCallback Callback function.
806 * @param pvUser User argument.
807 * @param pszDesc Pointer to description string which must stay around
808 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
809 */
810TMR3DECL(PTMTIMERHC) TMR3TimerCreateExternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMEREXT pfnCallback, void *pvUser, const char *pszDesc)
811{
812 /*
813 * Allocate and init stuff.
814 */
815 PTMTIMERHC pTimer;
816 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, &pTimer);
817 if (VBOX_SUCCESS(rc))
818 {
819 pTimer->enmType = TMTIMERTYPE_EXTERNAL;
820 pTimer->u.External.pfnTimer = pfnCallback;
821 pTimer->u.External.pvUser = pvUser;
822 Log(("TM: Created external timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
823 return pTimer;
824 }
825
826 return NULL;
827}
828
829
830/**
831 * Destroy all timers owned by a device.
832 *
833 * @returns VBox status.
834 * @param pVM VM handle.
835 * @param pDevIns Device which timers should be destroyed.
836 */
837TMR3DECL(int) TMR3TimerDestroyDevice(PVM pVM, PPDMDEVINS pDevIns)
838{
839 LogFlow(("TMR3TimerDestroyDevice: pDevIns=%p\n", pDevIns));
840 if (!pDevIns)
841 return VERR_INVALID_PARAMETER;
842
843 PTMTIMER pCur = pVM->tm.s.pCreated;
844 while (pCur)
845 {
846 PTMTIMER pDestroy = pCur;
847 pCur = pDestroy->pBigNext;
848 if ( pDestroy->enmType == TMTIMERTYPE_DEV
849 && pDestroy->u.Dev.pDevIns == pDevIns)
850 {
851 int rc = TMTimerDestroy(pDestroy);
852 AssertRC(rc);
853 }
854 }
855 LogFlow(("TMR3TimerDestroyDevice: returns VINF_SUCCESS\n"));
856 return VINF_SUCCESS;
857}
858
859
860/**
861 * Destroy all timers owned by a driver.
862 *
863 * @returns VBox status.
864 * @param pVM VM handle.
865 * @param pDrvIns Driver which timers should be destroyed.
866 */
867TMR3DECL(int) TMR3TimerDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns)
868{
869 LogFlow(("TMR3TimerDestroyDriver: pDrvIns=%p\n", pDrvIns));
870 if (!pDrvIns)
871 return VERR_INVALID_PARAMETER;
872
873 PTMTIMER pCur = pVM->tm.s.pCreated;
874 while (pCur)
875 {
876 PTMTIMER pDestroy = pCur;
877 pCur = pDestroy->pBigNext;
878 if ( pDestroy->enmType == TMTIMERTYPE_DRV
879 && pDestroy->u.Drv.pDrvIns == pDrvIns)
880 {
881 int rc = TMTimerDestroy(pDestroy);
882 AssertRC(rc);
883 }
884 }
885 LogFlow(("TMR3TimerDestroyDriver: returns VINF_SUCCESS\n"));
886 return VINF_SUCCESS;
887}
888
889
890/**
891 * Checks if a queue has a pending timer.
892 *
893 * @returns true if it has a pending timer.
894 * @returns false is no pending timer.
895 *
896 * @param pVM The VM handle.
897 * @param enmClock The queue.
898 */
899DECLINLINE(bool) tmR3HasPending(PVM pVM, TMCLOCK enmClock)
900{
901 const uint64_t u64Expire = pVM->tm.s.CTXALLSUFF(paTimerQueues)[enmClock].u64Expire;
902 return u64Expire != INT64_MAX && u64Expire <= tmClock(pVM, enmClock);
903}
904
905
906/**
907 * Schedulation timer callback.
908 *
909 * @param pTimer Timer handle.
910 * @param pvUser VM handle.
911 * @remark We cannot do the scheduling and queues running from a timer handler
912 * since it's not executing in EMT, and even if it was it would be async
913 * and we wouldn't know the state of the affairs.
914 * So, we'll just raise the timer FF and force any REM execution to exit.
915 */
916static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser)
917{
918 PVM pVM = (PVM)pvUser;
919 AssertCompile(TMCLOCK_MAX == 4);
920#ifdef DEBUG_Sander /* very annoying, keep it private. */
921 if (VM_FF_ISSET(pVM, VM_FF_TIMER))
922 Log(("tmR3TimerCallback: timer event still pending!!\n"));
923#endif
924 if ( !VM_FF_ISSET(pVM, VM_FF_TIMER)
925 && ( pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].offSchedule
926 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].offSchedule
927 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].offSchedule
928 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].offSchedule
929 || tmR3HasPending(pVM, TMCLOCK_VIRTUAL_SYNC)
930 || tmR3HasPending(pVM, TMCLOCK_VIRTUAL)
931 || tmR3HasPending(pVM, TMCLOCK_REAL)
932 || tmR3HasPending(pVM, TMCLOCK_TSC)
933 )
934 && !VM_FF_ISSET(pVM, VM_FF_TIMER)
935 )
936 {
937 VM_FF_SET(pVM, VM_FF_TIMER);
938 REMR3NotifyTimerPending(pVM);
939 VMR3NotifyFF(pVM, true);
940 STAM_COUNTER_INC(&pVM->tm.s.StatTimerCallbackSetFF);
941 }
942}
943
944
945/**
946 * Schedules and runs any pending timers.
947 *
948 * This is normally called from a forced action handler in EMT.
949 *
950 * @param pVM The VM to run the timers for.
951 */
952TMR3DECL(void) TMR3TimerQueuesDo(PVM pVM)
953{
954 STAM_PROFILE_START(&pVM->tm.s.StatDoQueues, a);
955 Log2(("TMR3TimerQueuesDo:\n"));
956
957 /*
958 * Process the queues.
959 */
960 AssertCompile(TMCLOCK_MAX == 4);
961
962 /* TMCLOCK_VIRTUAL */
963 STAM_PROFILE_ADV_START(&pVM->tm.s.StatDoQueuesSchedule, s1);
964 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL]);
965 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesSchedule, s1);
966 STAM_PROFILE_ADV_START(&pVM->tm.s.StatDoQueuesRun, r1);
967 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL]);
968 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesRun, r1);
969
970 /* TMCLOCK_VIRTUAL_SYNC */
971 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesSchedule, s1);
972 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC]);
973 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesSchedule, s2);
974 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesRun, r1);
975 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC]);
976 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesRun, r2);
977
978 /* TMCLOCK_REAL */
979 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesSchedule, s2);
980 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL]);
981 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesSchedule, s3);
982 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesRun, r2);
983 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL]);
984 STAM_PROFILE_ADV_SUSPEND(&pVM->tm.s.StatDoQueuesRun, r3);
985
986 /* TMCLOCK_TSC */
987 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesSchedule, s3);
988 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC]);
989 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatDoQueuesSchedule, s3);
990 STAM_PROFILE_ADV_RESUME(&pVM->tm.s.StatDoQueuesRun, r3);
991 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC]);
992 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatDoQueuesRun, r3);
993
994 /* done. */
995 VM_FF_CLEAR(pVM, VM_FF_TIMER);
996
997#ifdef VBOX_STRICT
998 /* check that we didn't screwup. */
999 tmTimerQueuesSanityChecks(pVM, "TMR3TimerQueuesDo");
1000#endif
1001
1002 Log2(("TMR3TimerQueuesDo: returns void\n"));
1003 STAM_PROFILE_STOP(&pVM->tm.s.StatDoQueues, a);
1004}
1005
1006
1007/**
1008 * Schedules and runs any pending times in the specified queue.
1009 *
1010 * This is normally called from a forced action handler in EMT.
1011 *
1012 * @param pVM The VM to run the timers for.
1013 * @param pQueue The queue to run.
1014 */
1015static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue)
1016{
1017 VM_ASSERT_EMT(pVM);
1018
1019 /*
1020 * Run timers.
1021 *
1022 * We check the clock once and run all timers which are ACTIVE
1023 * and have an expire time less or equal to the time we read.
1024 *
1025 * N.B. A generic unlink must be applied since other threads
1026 * are allowed to mess with any active timer at any time.
1027 * However, we only allow EMT to handle EXPIRED_PENDING
1028 * timers, thus enabling the timer handler function to
1029 * arm the timer again.
1030 */
1031 PTMTIMER pNext = TMTIMER_GET_HEAD(pQueue);
1032 if (!pNext)
1033 return;
1034 /** @todo deal with the VIRTUAL_SYNC pausing and catch calcs ++ */
1035 uint64_t u64Now = tmClock(pVM, pQueue->enmClock);
1036 while (pNext && pNext->u64Expire <= u64Now)
1037 {
1038 PTMTIMER pTimer = pNext;
1039 pNext = TMTIMER_GET_NEXT(pTimer);
1040 Log2(("tmR3TimerQueueRun: pTimer=%p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
1041 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
1042 bool fRc;
1043 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED, TMTIMERSTATE_ACTIVE, fRc);
1044 if (fRc)
1045 {
1046 Assert(!pTimer->offScheduleNext); /* this can trigger falsely */
1047
1048 /* unlink */
1049 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1050 if (pPrev)
1051 TMTIMER_SET_NEXT(pPrev, pNext);
1052 else
1053 {
1054 TMTIMER_SET_HEAD(pQueue, pNext);
1055 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1056 }
1057 if (pNext)
1058 TMTIMER_SET_PREV(pNext, pPrev);
1059 pTimer->offNext = 0;
1060 pTimer->offPrev = 0;
1061
1062
1063 /* fire */
1064 switch (pTimer->enmType)
1065 {
1066 case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer); break;
1067 case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer); break;
1068 case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->u.Internal.pvUser); break;
1069 case TMTIMERTYPE_EXTERNAL: pTimer->u.External.pfnTimer(pTimer->u.External.pvUser); break;
1070 default:
1071 AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->pszDesc));
1072 break;
1073 }
1074
1075 /* change the state if it wasn't changed already in the handler. */
1076 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED, fRc);
1077 Log2(("tmR3TimerQueueRun: new state %s\n", tmTimerState(pTimer->enmState)));
1078 }
1079 } /* run loop */
1080}
1081
1082
1083/**
1084 * Saves the state of a timer to a saved state.
1085 *
1086 * @returns VBox status.
1087 * @param pTimer Timer to save.
1088 * @param pSSM Save State Manager handle.
1089 */
1090TMR3DECL(int) TMR3TimerSave(PTMTIMERHC pTimer, PSSMHANDLE pSSM)
1091{
1092 LogFlow(("TMR3TimerSave: pTimer=%p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
1093 switch (pTimer->enmState)
1094 {
1095 case TMTIMERSTATE_STOPPED:
1096 case TMTIMERSTATE_PENDING_STOP:
1097 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1098 return SSMR3PutU8(pSSM, (uint8_t)TMTIMERSTATE_PENDING_STOP);
1099
1100 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1101 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1102 AssertMsgFailed(("u64Expire is being updated! (%s)\n", pTimer->pszDesc));
1103 if (!RTThreadYield())
1104 RTThreadSleep(1);
1105 /* fall thru */
1106 case TMTIMERSTATE_ACTIVE:
1107 case TMTIMERSTATE_PENDING_SCHEDULE:
1108 case TMTIMERSTATE_PENDING_RESCHEDULE:
1109 SSMR3PutU8(pSSM, (uint8_t)TMTIMERSTATE_PENDING_SCHEDULE);
1110 return SSMR3PutU64(pSSM, pTimer->u64Expire);
1111
1112 case TMTIMERSTATE_EXPIRED:
1113 case TMTIMERSTATE_PENDING_DESTROY:
1114 case TMTIMERSTATE_PENDING_STOP_DESTROY:
1115 case TMTIMERSTATE_FREE:
1116 AssertMsgFailed(("Invalid timer state %d %s (%s)\n", pTimer->enmState, tmTimerState(pTimer->enmState), pTimer->pszDesc));
1117 return SSMR3HandleSetStatus(pSSM, VERR_TM_INVALID_STATE);
1118 }
1119
1120 AssertMsgFailed(("Unknown timer state %d (%s)\n", pTimer->enmState, pTimer->pszDesc));
1121 return SSMR3HandleSetStatus(pSSM, VERR_TM_UNKNOWN_STATE);
1122}
1123
1124
1125/**
1126 * Loads the state of a timer from a saved state.
1127 *
1128 * @returns VBox status.
1129 * @param pTimer Timer to restore.
1130 * @param pSSM Save State Manager handle.
1131 */
1132TMR3DECL(int) TMR3TimerLoad(PTMTIMERHC pTimer, PSSMHANDLE pSSM)
1133{
1134 Assert(pTimer); Assert(pSSM); VM_ASSERT_EMT(pTimer->pVMR3);
1135 LogFlow(("TMR3TimerLoad: pTimer=%p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
1136
1137 /*
1138 * Load the state and validate it.
1139 */
1140 uint8_t u8State;
1141 int rc = SSMR3GetU8(pSSM, &u8State);
1142 if (VBOX_FAILURE(rc))
1143 return rc;
1144 TMTIMERSTATE enmState = (TMTIMERSTATE)u8State;
1145 if ( enmState != TMTIMERSTATE_PENDING_STOP
1146 && enmState != TMTIMERSTATE_PENDING_SCHEDULE
1147 && enmState != TMTIMERSTATE_PENDING_STOP_SCHEDULE)
1148 {
1149 AssertMsgFailed(("enmState=%d %s\n", enmState, tmTimerState(enmState)));
1150 return SSMR3HandleSetStatus(pSSM, VERR_TM_LOAD_STATE);
1151 }
1152
1153 if (enmState == TMTIMERSTATE_PENDING_SCHEDULE)
1154 {
1155 /*
1156 * Load the expire time.
1157 */
1158 uint64_t u64Expire;
1159 rc = SSMR3GetU64(pSSM, &u64Expire);
1160 if (VBOX_FAILURE(rc))
1161 return rc;
1162
1163 /*
1164 * Set it.
1165 */
1166 Log(("enmState=%d %s u64Expire=%llu\n", enmState, tmTimerState(enmState), u64Expire));
1167 rc = TMTimerSet(pTimer, u64Expire);
1168 }
1169 else
1170 {
1171 /*
1172 * Stop it.
1173 */
1174 Log(("enmState=%d %s\n", enmState, tmTimerState(enmState)));
1175 rc = TMTimerStop(pTimer);
1176 }
1177
1178 /*
1179 * On failure set SSM status.
1180 */
1181 if (VBOX_FAILURE(rc))
1182 rc = SSMR3HandleSetStatus(pSSM, rc);
1183 return rc;
1184}
1185
1186
1187/**
1188 * Display all timers.
1189 *
1190 * @param pVM VM Handle.
1191 * @param pHlp The info helpers.
1192 * @param pszArgs Arguments, ignored.
1193 */
1194static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1195{
1196 NOREF(pszArgs);
1197 pHlp->pfnPrintf(pHlp,
1198 "Timers (pVM=%p)\n"
1199 "%.*s %.*s %.*s %.*s Clock %-18s %-18s %-25s Description\n",
1200 pVM,
1201 sizeof(RTR3PTR) * 2, "pTimerR3 ",
1202 sizeof(int32_t) * 2, "offNext ",
1203 sizeof(int32_t) * 2, "offPrev ",
1204 sizeof(int32_t) * 2, "offSched ",
1205 "Time",
1206 "Expire",
1207 "State");
1208 for (PTMTIMERHC pTimer = pVM->tm.s.pCreated; pTimer; pTimer = pTimer->pBigNext)
1209 {
1210 pHlp->pfnPrintf(pHlp,
1211 "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %-25s %s\n",
1212 pTimer,
1213 pTimer->offNext,
1214 pTimer->offPrev,
1215 pTimer->offScheduleNext,
1216 pTimer->enmClock == TMCLOCK_REAL ? "Real " : "Virt ",
1217 TMTimerGet(pTimer),
1218 pTimer->u64Expire,
1219 tmTimerState(pTimer->enmState),
1220 pTimer->pszDesc);
1221 }
1222}
1223
1224
1225/**
1226 * Display all active timers.
1227 *
1228 * @param pVM VM Handle.
1229 * @param pHlp The info helpers.
1230 * @param pszArgs Arguments, ignored.
1231 */
1232static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1233{
1234 NOREF(pszArgs);
1235 pHlp->pfnPrintf(pHlp,
1236 "Active Timers (pVM=%p)\n"
1237 "%.*s %.*s %.*s %.*s Clock %-18s %-18s %-25s Description\n",
1238 pVM,
1239 sizeof(RTR3PTR) * 2, "pTimerR3 ",
1240 sizeof(int32_t) * 2, "offNext ",
1241 sizeof(int32_t) * 2, "offPrev ",
1242 sizeof(int32_t) * 2, "offSched ",
1243 "Time",
1244 "Expire",
1245 "State");
1246 for (unsigned iQueue = 0; iQueue < TMCLOCK_MAX; iQueue++)
1247 {
1248 for (PTMTIMERHC pTimer = TMTIMER_GET_HEAD(&pVM->tm.s.paTimerQueuesR3[iQueue]);
1249 pTimer;
1250 pTimer = TMTIMER_GET_NEXT(pTimer))
1251 {
1252 pHlp->pfnPrintf(pHlp,
1253 "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %-25s %s\n",
1254 pTimer,
1255 pTimer->offNext,
1256 pTimer->offPrev,
1257 pTimer->offScheduleNext,
1258 pTimer->enmClock == TMCLOCK_REAL ? "Real " : "Virt ",
1259 TMTimerGet(pTimer),
1260 pTimer->u64Expire,
1261 tmTimerState(pTimer->enmState),
1262 pTimer->pszDesc);
1263 }
1264 }
1265}
1266
1267
1268/**
1269 * Display all clocks.
1270 *
1271 * @param pVM VM Handle.
1272 * @param pHlp The info helpers.
1273 * @param pszArgs Arguments, ignored.
1274 */
1275static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1276{
1277 NOREF(pszArgs);
1278
1279 /* TSC */
1280 uint64_t u64 = TMCpuTickGet(pVM);
1281 pHlp->pfnPrintf(pHlp,
1282 "Cpu Tick: %#RX64 (%RU64) %RU64Hz %s%s",
1283 u64, u64, TMCpuTicksPerSecond(pVM),
1284 pVM->tm.s.fTSCTicking ? "ticking" : "paused",
1285 pVM->tm.s.fTSCVirtualized ? " - virtualized" : "");
1286 if (pVM->tm.s.fTSCUseRealTSC)
1287 {
1288 pHlp->pfnPrintf(pHlp, "- real tsc");
1289 if (pVM->tm.s.u64TSCOffset)
1290 pHlp->pfnPrintf(pHlp, "\n offset %#RX64", pVM->tm.s.u64TSCOffset);
1291 }
1292 else
1293 pHlp->pfnPrintf(pHlp, "- virtual clock");
1294 pHlp->pfnPrintf(pHlp, "\n");
1295
1296 /* virtual */
1297 u64 = TMVirtualGet(pVM);
1298 pHlp->pfnPrintf(pHlp,
1299 " Virtual: %#RX64 (%RU64) %RU64Hz %s",
1300 u64, u64, TMVirtualGetFreq(pVM),
1301 pVM->tm.s.fVirtualTicking ? "ticking" : "paused");
1302 if (pVM->tm.s.fVirtualWarpDrive)
1303 pHlp->pfnPrintf(pHlp, " WarpDrive %RU32 %%", pVM->tm.s.u32VirtualWarpDrivePercentage);
1304 pHlp->pfnPrintf(pHlp, "\n");
1305
1306 /* virtual sync */
1307 u64 = TMVirtualGetSync(pVM);
1308 pHlp->pfnPrintf(pHlp,
1309 "VirtSync: %#RX64 (%RU64) %s%s",
1310 u64, u64,
1311 pVM->tm.s.fVirtualSyncTicking ? "ticking" : "paused",
1312 pVM->tm.s.fVirtualSyncCatchUp ? " - catchup" : "");
1313 if (pVM->tm.s.u64VirtualSyncOffset)
1314 pHlp->pfnPrintf(pHlp, "\n offset %#RX64", pVM->tm.s.u64VirtualSyncOffset);
1315 pHlp->pfnPrintf(pHlp, "\n");
1316
1317 /* real */
1318 u64 = TMRealGet(pVM);
1319 pHlp->pfnPrintf(pHlp,
1320 " Real: %#RX64 (%RU64) %RU64Hz\n",
1321 u64, u64, TMRealGetFreq(pVM));
1322}
Note: See TracBrowser for help on using the repository browser.

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