VirtualBox

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

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

Initial GIP change. Missing detection of SMP systems with TSC drift.

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