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