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