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