1 | /* $Id: TM.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TM - Time Manager.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /** @page pg_tm TM - The Time Manager
|
---|
29 | *
|
---|
30 | * The Time Manager abstracts the CPU clocks and manages timers used by the VMM,
|
---|
31 | * device and drivers.
|
---|
32 | *
|
---|
33 | * @see grp_tm
|
---|
34 | *
|
---|
35 | *
|
---|
36 | * @section sec_tm_clocks Clocks
|
---|
37 | *
|
---|
38 | * There are currently 4 clocks:
|
---|
39 | * - Virtual (guest).
|
---|
40 | * - Synchronous virtual (guest).
|
---|
41 | * - CPU Tick (TSC) (guest). Only current use is rdtsc emulation. Usually a
|
---|
42 | * function of the virtual clock.
|
---|
43 | * - Real (host). This is only used for display updates atm.
|
---|
44 | *
|
---|
45 | * The most important clocks are the three first ones and of these the second is
|
---|
46 | * the most interesting.
|
---|
47 | *
|
---|
48 | *
|
---|
49 | * The synchronous virtual clock is tied to the virtual clock except that it
|
---|
50 | * will take into account timer delivery lag caused by host scheduling. It will
|
---|
51 | * normally never advance beyond the head timer, and when lagging too far behind
|
---|
52 | * it will gradually speed up to catch up with the virtual clock. All devices
|
---|
53 | * implementing time sources accessible to and used by the guest is using this
|
---|
54 | * clock (for timers and other things). This ensures consistency between the
|
---|
55 | * time sources.
|
---|
56 | *
|
---|
57 | * The virtual clock is implemented as an offset to a monotonic, high
|
---|
58 | * resolution, wall clock. The current time source is using the RTTimeNanoTS()
|
---|
59 | * machinery based upon the Global Info Pages (GIP), that is, we're using TSC
|
---|
60 | * deltas (usually 10 ms) to fill the gaps between GIP updates. The result is
|
---|
61 | * a fairly high res clock that works in all contexts and on all hosts. The
|
---|
62 | * virtual clock is paused when the VM isn't in the running state.
|
---|
63 | *
|
---|
64 | * The CPU tick (TSC) is normally virtualized as a function of the synchronous
|
---|
65 | * virtual clock, where the frequency defaults to the host cpu frequency (as we
|
---|
66 | * measure it). In this mode it is possible to configure the frequency. Another
|
---|
67 | * (non-default) option is to use the raw unmodified host TSC values. And yet
|
---|
68 | * another, to tie it to time spent executing guest code. All these things are
|
---|
69 | * configurable should non-default behavior be desirable.
|
---|
70 | *
|
---|
71 | * The real clock is a monotonic clock (when available) with relatively low
|
---|
72 | * resolution, though this a bit host specific. Note that we're currently not
|
---|
73 | * servicing timers using the real clock when the VM is not running, this is
|
---|
74 | * simply because it has not been needed yet therefore not implemented.
|
---|
75 | *
|
---|
76 | *
|
---|
77 | * @subsection subsec_tm_timesync Guest Time Sync / UTC time
|
---|
78 | *
|
---|
79 | * Guest time syncing is primarily taken care of by the VMM device. The
|
---|
80 | * principle is very simple, the guest additions periodically asks the VMM
|
---|
81 | * device what the current UTC time is and makes adjustments accordingly.
|
---|
82 | *
|
---|
83 | * A complicating factor is that the synchronous virtual clock might be doing
|
---|
84 | * catchups and the guest perception is currently a little bit behind the world
|
---|
85 | * but it will (hopefully) be catching up soon as we're feeding timer interrupts
|
---|
86 | * at a slightly higher rate. Adjusting the guest clock to the current wall
|
---|
87 | * time in the real world would be a bad idea then because the guest will be
|
---|
88 | * advancing too fast and run ahead of world time (if the catchup works out).
|
---|
89 | * To solve this problem TM provides the VMM device with an UTC time source that
|
---|
90 | * gets adjusted with the current lag, so that when the guest eventually catches
|
---|
91 | * up the lag it will be showing correct real world time.
|
---|
92 | *
|
---|
93 | *
|
---|
94 | * @section sec_tm_timers Timers
|
---|
95 | *
|
---|
96 | * The timers can use any of the TM clocks described in the previous section.
|
---|
97 | * Each clock has its own scheduling facility, or timer queue if you like.
|
---|
98 | * There are a few factors which makes it a bit complex. First, there is the
|
---|
99 | * usual R0 vs R3 vs. RC thing. Then there are multiple threads, and then there
|
---|
100 | * is the timer thread that periodically checks whether any timers has expired
|
---|
101 | * without EMT noticing. On the API level, all but the create and save APIs
|
---|
102 | * must be multithreaded. EMT will always run the timers.
|
---|
103 | *
|
---|
104 | * The design is using a doubly linked list of active timers which is ordered
|
---|
105 | * by expire date. This list is only modified by the EMT thread. Updates to
|
---|
106 | * the list are batched in a singly linked list, which is then processed by the
|
---|
107 | * EMT thread at the first opportunity (immediately, next time EMT modifies a
|
---|
108 | * timer on that clock, or next timer timeout). Both lists are offset based and
|
---|
109 | * all the elements are therefore allocated from the hyper heap.
|
---|
110 | *
|
---|
111 | * For figuring out when there is need to schedule and run timers TM will:
|
---|
112 | * - Poll whenever somebody queries the virtual clock.
|
---|
113 | * - Poll the virtual clocks from the EM and REM loops.
|
---|
114 | * - Poll the virtual clocks from trap exit path.
|
---|
115 | * - Poll the virtual clocks and calculate first timeout from the halt loop.
|
---|
116 | * - Employ a thread which periodically (100Hz) polls all the timer queues.
|
---|
117 | *
|
---|
118 | *
|
---|
119 | * @image html TMTIMER-Statechart-Diagram.gif
|
---|
120 | *
|
---|
121 | * @section sec_tm_timer Logging
|
---|
122 | *
|
---|
123 | * Level 2: Logs a most of the timer state transitions and queue servicing.
|
---|
124 | * Level 3: Logs a few oddments.
|
---|
125 | * Level 4: Logs TMCLOCK_VIRTUAL_SYNC catch-up events.
|
---|
126 | *
|
---|
127 | */
|
---|
128 |
|
---|
129 |
|
---|
130 | /*********************************************************************************************************************************
|
---|
131 | * Header Files *
|
---|
132 | *********************************************************************************************************************************/
|
---|
133 | #define LOG_GROUP LOG_GROUP_TM
|
---|
134 | #ifdef DEBUG_bird
|
---|
135 | # define DBGFTRACE_DISABLED /* annoying */
|
---|
136 | #endif
|
---|
137 | #include <VBox/vmm/tm.h>
|
---|
138 | #include <VBox/vmm/vmm.h>
|
---|
139 | #include <VBox/vmm/mm.h>
|
---|
140 | #include <VBox/vmm/hm.h>
|
---|
141 | #include <VBox/vmm/nem.h>
|
---|
142 | #include <VBox/vmm/gim.h>
|
---|
143 | #include <VBox/vmm/ssm.h>
|
---|
144 | #include <VBox/vmm/dbgf.h>
|
---|
145 | #include <VBox/vmm/dbgftrace.h>
|
---|
146 | #include <VBox/vmm/pdmapi.h>
|
---|
147 | #include <VBox/vmm/iom.h>
|
---|
148 | #include "TMInternal.h"
|
---|
149 | #include <VBox/vmm/vm.h>
|
---|
150 | #include <VBox/vmm/uvm.h>
|
---|
151 |
|
---|
152 | #include <VBox/vmm/pdmdev.h>
|
---|
153 | #include <VBox/log.h>
|
---|
154 | #include <VBox/param.h>
|
---|
155 | #include <VBox/err.h>
|
---|
156 |
|
---|
157 | #include <iprt/asm.h>
|
---|
158 | #include <iprt/asm-math.h>
|
---|
159 | #include <iprt/assert.h>
|
---|
160 | #include <iprt/env.h>
|
---|
161 | #include <iprt/file.h>
|
---|
162 | #include <iprt/getopt.h>
|
---|
163 | #include <iprt/mem.h>
|
---|
164 | #include <iprt/rand.h>
|
---|
165 | #include <iprt/semaphore.h>
|
---|
166 | #include <iprt/string.h>
|
---|
167 | #include <iprt/thread.h>
|
---|
168 | #include <iprt/time.h>
|
---|
169 | #include <iprt/timer.h>
|
---|
170 |
|
---|
171 | #include "TMInline.h"
|
---|
172 |
|
---|
173 |
|
---|
174 | /*********************************************************************************************************************************
|
---|
175 | * Defined Constants And Macros *
|
---|
176 | *********************************************************************************************************************************/
|
---|
177 | /** The current saved state version.*/
|
---|
178 | #define TM_SAVED_STATE_VERSION 3
|
---|
179 |
|
---|
180 |
|
---|
181 | /*********************************************************************************************************************************
|
---|
182 | * Internal Functions *
|
---|
183 | *********************************************************************************************************************************/
|
---|
184 | static bool tmR3HasFixedTSC(PVM pVM);
|
---|
185 | static uint64_t tmR3CalibrateTSC(void);
|
---|
186 | static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM);
|
---|
187 | static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
|
---|
188 | #ifdef VBOX_WITH_STATISTICS
|
---|
189 | static void tmR3TimerQueueRegisterStats(PVM pVM, PTMTIMERQUEUE pQueue, uint32_t cTimers);
|
---|
190 | #endif
|
---|
191 | static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
|
---|
192 | static DECLCALLBACK(int) tmR3SetWarpDrive(PUVM pUVM, uint32_t u32Percent);
|
---|
193 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
194 | static DECLCALLBACK(void) tmR3CpuLoadTimer(PVM pVM, TMTIMERHANDLE hTimer, void *pvUser);
|
---|
195 | #endif
|
---|
196 | static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
|
---|
197 | static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
|
---|
198 | static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
|
---|
199 | static DECLCALLBACK(void) tmR3InfoCpuLoad(PVM pVM, PCDBGFINFOHLP pHlp, int cArgs, char **papszArgs);
|
---|
200 | static DECLCALLBACK(VBOXSTRICTRC) tmR3CpuTickParavirtDisable(PVM pVM, PVMCPU pVCpu, void *pvData);
|
---|
201 | static const char *tmR3GetTSCModeName(PVM pVM);
|
---|
202 | static const char *tmR3GetTSCModeNameEx(TMTSCMODE enmMode);
|
---|
203 | static int tmR3TimerQueueGrow(PVM pVM, PTMTIMERQUEUE pQueue, uint32_t cNewTimers);
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Initializes the TM.
|
---|
208 | *
|
---|
209 | * @returns VBox status code.
|
---|
210 | * @param pVM The cross context VM structure.
|
---|
211 | */
|
---|
212 | VMM_INT_DECL(int) TMR3Init(PVM pVM)
|
---|
213 | {
|
---|
214 | LogFlow(("TMR3Init:\n"));
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Assert alignment and sizes.
|
---|
218 | */
|
---|
219 | AssertCompileMemberAlignment(VM, tm.s, 32);
|
---|
220 | AssertCompile(sizeof(pVM->tm.s) <= sizeof(pVM->tm.padding));
|
---|
221 | AssertCompileMemberAlignment(TM, VirtualSyncLock, 8);
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * Init the structure.
|
---|
225 | */
|
---|
226 | pVM->tm.s.idTimerCpu = pVM->cCpus - 1; /* The last CPU. */
|
---|
227 |
|
---|
228 | int rc = PDMR3CritSectInit(pVM, &pVM->tm.s.VirtualSyncLock, RT_SRC_POS, "TM VirtualSync Lock");
|
---|
229 | AssertLogRelRCReturn(rc, rc);
|
---|
230 |
|
---|
231 | strcpy(pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL].szName, "virtual");
|
---|
232 | strcpy(pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].szName, "virtual_sync"); /* Underscore is for STAM ordering issue. */
|
---|
233 | strcpy(pVM->tm.s.aTimerQueues[TMCLOCK_REAL].szName, "real");
|
---|
234 | strcpy(pVM->tm.s.aTimerQueues[TMCLOCK_TSC].szName, "tsc");
|
---|
235 |
|
---|
236 | for (uint32_t i = 0; i < RT_ELEMENTS(pVM->tm.s.aTimerQueues); i++)
|
---|
237 | {
|
---|
238 | Assert(pVM->tm.s.aTimerQueues[i].szName[0] != '\0');
|
---|
239 | pVM->tm.s.aTimerQueues[i].enmClock = (TMCLOCK)i;
|
---|
240 | pVM->tm.s.aTimerQueues[i].u64Expire = INT64_MAX;
|
---|
241 | pVM->tm.s.aTimerQueues[i].idxActive = UINT32_MAX;
|
---|
242 | pVM->tm.s.aTimerQueues[i].idxSchedule = UINT32_MAX;
|
---|
243 | pVM->tm.s.aTimerQueues[i].idxFreeHint = 1;
|
---|
244 | pVM->tm.s.aTimerQueues[i].fBeingProcessed = false;
|
---|
245 | pVM->tm.s.aTimerQueues[i].fCannotGrow = false;
|
---|
246 | pVM->tm.s.aTimerQueues[i].hThread = NIL_RTTHREAD;
|
---|
247 | pVM->tm.s.aTimerQueues[i].hWorkerEvt = NIL_SUPSEMEVENT;
|
---|
248 |
|
---|
249 | rc = PDMR3CritSectInit(pVM, &pVM->tm.s.aTimerQueues[i].TimerLock, RT_SRC_POS,
|
---|
250 | "TM %s queue timer lock", pVM->tm.s.aTimerQueues[i].szName);
|
---|
251 | AssertLogRelRCReturn(rc, rc);
|
---|
252 |
|
---|
253 | rc = PDMR3CritSectRwInit(pVM, &pVM->tm.s.aTimerQueues[i].AllocLock, RT_SRC_POS,
|
---|
254 | "TM %s queue alloc lock", pVM->tm.s.aTimerQueues[i].szName);
|
---|
255 | AssertLogRelRCReturn(rc, rc);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * We directly use the GIP to calculate the virtual time. We map the
|
---|
260 | * the GIP into the guest context so we can do this calculation there
|
---|
261 | * as well and save costly world switches.
|
---|
262 | */
|
---|
263 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
264 | if (pGip || !SUPR3IsDriverless())
|
---|
265 | {
|
---|
266 | pVM->tm.s.pvGIPR3 = (void *)pGip;
|
---|
267 | AssertMsgReturn(pVM->tm.s.pvGIPR3, ("GIP support is now required!\n"), VERR_TM_GIP_REQUIRED);
|
---|
268 | AssertMsgReturn((pGip->u32Version >> 16) == (SUPGLOBALINFOPAGE_VERSION >> 16),
|
---|
269 | ("Unsupported GIP version %#x! (expected=%#x)\n", pGip->u32Version, SUPGLOBALINFOPAGE_VERSION),
|
---|
270 | VERR_TM_GIP_VERSION);
|
---|
271 |
|
---|
272 | /* Check assumptions made in TMAllVirtual.cpp about the GIP update interval. */
|
---|
273 | if ( pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
|
---|
274 | && pGip->u32UpdateIntervalNS >= 250000000 /* 0.25s */)
|
---|
275 | return VMSetError(pVM, VERR_TM_GIP_UPDATE_INTERVAL_TOO_BIG, RT_SRC_POS,
|
---|
276 | N_("The GIP update interval is too big. u32UpdateIntervalNS=%RU32 (u32UpdateHz=%RU32)"),
|
---|
277 | pGip->u32UpdateIntervalNS, pGip->u32UpdateHz);
|
---|
278 |
|
---|
279 | /* Log GIP info that may come in handy. */
|
---|
280 | LogRel(("TM: GIP - u32Mode=%d (%s) u32UpdateHz=%u u32UpdateIntervalNS=%u enmUseTscDelta=%d (%s) fGetGipCpu=%#x cCpus=%d\n",
|
---|
281 | pGip->u32Mode, SUPGetGIPModeName(pGip), pGip->u32UpdateHz, pGip->u32UpdateIntervalNS,
|
---|
282 | pGip->enmUseTscDelta, SUPGetGIPTscDeltaModeName(pGip), pGip->fGetGipCpu, pGip->cCpus));
|
---|
283 | LogRel(("TM: GIP - u64CpuHz=%'RU64 (%#RX64) SUPGetCpuHzFromGip => %'RU64\n",
|
---|
284 | pGip->u64CpuHz, pGip->u64CpuHz, SUPGetCpuHzFromGip(pGip)));
|
---|
285 | for (uint32_t iCpuSet = 0; iCpuSet < RT_ELEMENTS(pGip->aiCpuFromCpuSetIdx); iCpuSet++)
|
---|
286 | {
|
---|
287 | uint16_t iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
|
---|
288 | if (iGipCpu != UINT16_MAX)
|
---|
289 | LogRel(("TM: GIP - CPU: iCpuSet=%#x idCpu=%#x idApic=%#x iGipCpu=%#x i64TSCDelta=%RI64 enmState=%d u64CpuHz=%RU64(*) cErrors=%u\n",
|
---|
290 | iCpuSet, pGip->aCPUs[iGipCpu].idCpu, pGip->aCPUs[iGipCpu].idApic, iGipCpu, pGip->aCPUs[iGipCpu].i64TSCDelta,
|
---|
291 | pGip->aCPUs[iGipCpu].enmState, pGip->aCPUs[iGipCpu].u64CpuHz, pGip->aCPUs[iGipCpu].cErrors));
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Setup the VirtualGetRaw backend.
|
---|
297 | */
|
---|
298 | pVM->tm.s.pfnVirtualGetRaw = tmVirtualNanoTSRediscover;
|
---|
299 | pVM->tm.s.VirtualGetRawData.pfnRediscover = tmVirtualNanoTSRediscover;
|
---|
300 | pVM->tm.s.VirtualGetRawData.pfnBad = tmVirtualNanoTSBad;
|
---|
301 | pVM->tm.s.VirtualGetRawData.pfnBadCpuIndex = tmVirtualNanoTSBadCpuIndex;
|
---|
302 | pVM->tm.s.VirtualGetRawData.pu64Prev = &pVM->tm.s.u64VirtualRawPrev;
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Get our CFGM node, create it if necessary.
|
---|
306 | */
|
---|
307 | PCFGMNODE pCfgHandle = CFGMR3GetChild(CFGMR3GetRoot(pVM), "TM");
|
---|
308 | if (!pCfgHandle)
|
---|
309 | {
|
---|
310 | rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "TM", &pCfgHandle);
|
---|
311 | AssertRCReturn(rc, rc);
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Specific errors about some obsolete TM settings (remove after 2015-12-03).
|
---|
316 | */
|
---|
317 | if (CFGMR3Exists(pCfgHandle, "TSCVirtualized"))
|
---|
318 | return VMSetError(pVM, VERR_CFGM_CONFIG_UNKNOWN_VALUE, RT_SRC_POS,
|
---|
319 | N_("Configuration error: TM setting \"TSCVirtualized\" is no longer supported. Use the \"TSCMode\" setting instead."));
|
---|
320 | if (CFGMR3Exists(pCfgHandle, "UseRealTSC"))
|
---|
321 | return VMSetError(pVM, VERR_CFGM_CONFIG_UNKNOWN_VALUE, RT_SRC_POS,
|
---|
322 | N_("Configuration error: TM setting \"UseRealTSC\" is no longer supported. Use the \"TSCMode\" setting instead."));
|
---|
323 |
|
---|
324 | if (CFGMR3Exists(pCfgHandle, "MaybeUseOffsettedHostTSC"))
|
---|
325 | return VMSetError(pVM, VERR_CFGM_CONFIG_UNKNOWN_VALUE, RT_SRC_POS,
|
---|
326 | N_("Configuration error: TM setting \"MaybeUseOffsettedHostTSC\" is no longer supported. Use the \"TSCMode\" setting instead."));
|
---|
327 |
|
---|
328 | /*
|
---|
329 | * Validate the rest of the TM settings.
|
---|
330 | */
|
---|
331 | rc = CFGMR3ValidateConfig(pCfgHandle, "/TM/",
|
---|
332 | "TSCMode|"
|
---|
333 | "TSCModeSwitchAllowed|"
|
---|
334 | "TSCTicksPerSecond|"
|
---|
335 | "TSCTiedToExecution|"
|
---|
336 | "TSCNotTiedToHalt|"
|
---|
337 | "ScheduleSlack|"
|
---|
338 | "CatchUpStopThreshold|"
|
---|
339 | "CatchUpGiveUpThreshold|"
|
---|
340 | "CatchUpStartThreshold0|CatchUpStartThreshold1|CatchUpStartThreshold2|CatchUpStartThreshold3|"
|
---|
341 | "CatchUpStartThreshold4|CatchUpStartThreshold5|CatchUpStartThreshold6|CatchUpStartThreshold7|"
|
---|
342 | "CatchUpStartThreshold8|CatchUpStartThreshold9|"
|
---|
343 | "CatchUpPrecentage0|CatchUpPrecentage1|CatchUpPrecentage2|CatchUpPrecentage3|"
|
---|
344 | "CatchUpPrecentage4|CatchUpPrecentage5|CatchUpPrecentage6|CatchUpPrecentage7|"
|
---|
345 | "CatchUpPrecentage8|CatchUpPrecentage9|"
|
---|
346 | "UTCOffset|"
|
---|
347 | "UTCTouchFileOnJump|"
|
---|
348 | "WarpDrivePercentage|"
|
---|
349 | "HostHzMax|"
|
---|
350 | "HostHzFudgeFactorTimerCpu|"
|
---|
351 | "HostHzFudgeFactorOtherCpu|"
|
---|
352 | "HostHzFudgeFactorCatchUp100|"
|
---|
353 | "HostHzFudgeFactorCatchUp200|"
|
---|
354 | "HostHzFudgeFactorCatchUp400|"
|
---|
355 | "TimerMillies"
|
---|
356 | ,
|
---|
357 | "",
|
---|
358 | "TM", 0);
|
---|
359 | if (RT_FAILURE(rc))
|
---|
360 | return rc;
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * Determine the TSC configuration and frequency.
|
---|
364 | */
|
---|
365 | /** @cfgm{/TM/TSCMode, string, Depends on the CPU and VM config}
|
---|
366 | * The name of the TSC mode to use: VirtTSCEmulated, RealTSCOffset or Dynamic.
|
---|
367 | * The default depends on the VM configuration and the capabilities of the
|
---|
368 | * host CPU. Other config options or runtime changes may override the TSC
|
---|
369 | * mode specified here.
|
---|
370 | */
|
---|
371 | char szTSCMode[32];
|
---|
372 | rc = CFGMR3QueryString(pCfgHandle, "TSCMode", szTSCMode, sizeof(szTSCMode));
|
---|
373 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
374 | {
|
---|
375 | /** @todo Rainy-day/never: Dynamic mode isn't currently suitable for SMP VMs, so
|
---|
376 | * fall back on the more expensive emulated mode. With the current TSC handling
|
---|
377 | * (frequent switching between offsetted mode and taking VM exits, on all VCPUs
|
---|
378 | * without any kind of coordination) will lead to inconsistent TSC behavior with
|
---|
379 | * guest SMP, including TSC going backwards. */
|
---|
380 | pVM->tm.s.enmTSCMode = NEMR3NeedSpecialTscMode(pVM) ? TMTSCMODE_NATIVE_API
|
---|
381 | : pVM->cCpus == 1 && tmR3HasFixedTSC(pVM) ? TMTSCMODE_DYNAMIC : TMTSCMODE_VIRT_TSC_EMULATED;
|
---|
382 | }
|
---|
383 | else if (RT_FAILURE(rc))
|
---|
384 | return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying string value \"TSCMode\""));
|
---|
385 | else
|
---|
386 | {
|
---|
387 | if (!RTStrCmp(szTSCMode, "VirtTSCEmulated"))
|
---|
388 | pVM->tm.s.enmTSCMode = TMTSCMODE_VIRT_TSC_EMULATED;
|
---|
389 | else if (!RTStrCmp(szTSCMode, "RealTSCOffset"))
|
---|
390 | pVM->tm.s.enmTSCMode = TMTSCMODE_REAL_TSC_OFFSET;
|
---|
391 | else if (!RTStrCmp(szTSCMode, "Dynamic"))
|
---|
392 | pVM->tm.s.enmTSCMode = TMTSCMODE_DYNAMIC;
|
---|
393 | else
|
---|
394 | return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Unrecognized TM TSC mode value \"%s\""), szTSCMode);
|
---|
395 | if (NEMR3NeedSpecialTscMode(pVM))
|
---|
396 | {
|
---|
397 | LogRel(("TM: NEM overrides the /TM/TSCMode=%s settings.\n", szTSCMode));
|
---|
398 | pVM->tm.s.enmTSCMode = TMTSCMODE_NATIVE_API;
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * @cfgm{/TM/TSCModeSwitchAllowed, bool, Whether TM TSC mode switch is allowed
|
---|
404 | * at runtime}
|
---|
405 | * When using paravirtualized guests, we dynamically switch TSC modes to a more
|
---|
406 | * optimal one for performance. This setting allows overriding this behaviour.
|
---|
407 | */
|
---|
408 | rc = CFGMR3QueryBool(pCfgHandle, "TSCModeSwitchAllowed", &pVM->tm.s.fTSCModeSwitchAllowed);
|
---|
409 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
410 | {
|
---|
411 | /* This is finally determined in TMR3InitFinalize() as GIM isn't initialized yet. */
|
---|
412 | pVM->tm.s.fTSCModeSwitchAllowed = true;
|
---|
413 | }
|
---|
414 | else if (RT_FAILURE(rc))
|
---|
415 | return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying bool value \"TSCModeSwitchAllowed\""));
|
---|
416 | if (pVM->tm.s.fTSCModeSwitchAllowed && pVM->tm.s.enmTSCMode == TMTSCMODE_NATIVE_API)
|
---|
417 | {
|
---|
418 | LogRel(("TM: NEM overrides the /TM/TSCModeSwitchAllowed setting.\n"));
|
---|
419 | pVM->tm.s.fTSCModeSwitchAllowed = false;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** @cfgm{/TM/TSCTicksPerSecond, uint32_t, Current TSC frequency from GIP}
|
---|
423 | * The number of TSC ticks per second (i.e. the TSC frequency). This will
|
---|
424 | * override enmTSCMode.
|
---|
425 | */
|
---|
426 | pVM->tm.s.cTSCTicksPerSecondHost = tmR3CalibrateTSC();
|
---|
427 | rc = CFGMR3QueryU64(pCfgHandle, "TSCTicksPerSecond", &pVM->tm.s.cTSCTicksPerSecond);
|
---|
428 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
429 | {
|
---|
430 | pVM->tm.s.cTSCTicksPerSecond = pVM->tm.s.cTSCTicksPerSecondHost;
|
---|
431 | if ( ( pVM->tm.s.enmTSCMode == TMTSCMODE_DYNAMIC
|
---|
432 | || pVM->tm.s.enmTSCMode == TMTSCMODE_VIRT_TSC_EMULATED)
|
---|
433 | && pVM->tm.s.cTSCTicksPerSecond >= _4G)
|
---|
434 | {
|
---|
435 | pVM->tm.s.cTSCTicksPerSecond = _4G - 1; /* (A limitation of our math code) */
|
---|
436 | pVM->tm.s.enmTSCMode = TMTSCMODE_VIRT_TSC_EMULATED;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | else if (RT_FAILURE(rc))
|
---|
440 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
441 | N_("Configuration error: Failed to querying uint64_t value \"TSCTicksPerSecond\""));
|
---|
442 | else if ( pVM->tm.s.cTSCTicksPerSecond < _1M
|
---|
443 | || pVM->tm.s.cTSCTicksPerSecond >= _4G)
|
---|
444 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
445 | N_("Configuration error: \"TSCTicksPerSecond\" = %RI64 is not in the range 1MHz..4GHz-1"),
|
---|
446 | pVM->tm.s.cTSCTicksPerSecond);
|
---|
447 | else if (pVM->tm.s.enmTSCMode != TMTSCMODE_NATIVE_API)
|
---|
448 | pVM->tm.s.enmTSCMode = TMTSCMODE_VIRT_TSC_EMULATED;
|
---|
449 | else
|
---|
450 | {
|
---|
451 | LogRel(("TM: NEM overrides the /TM/TSCTicksPerSecond=%RU64 setting.\n", pVM->tm.s.cTSCTicksPerSecond));
|
---|
452 | pVM->tm.s.cTSCTicksPerSecond = pVM->tm.s.cTSCTicksPerSecondHost;
|
---|
453 | }
|
---|
454 |
|
---|
455 | /** @cfgm{/TM/TSCTiedToExecution, bool, false}
|
---|
456 | * Whether the TSC should be tied to execution. This will exclude most of the
|
---|
457 | * virtualization overhead, but will by default include the time spent in the
|
---|
458 | * halt state (see TM/TSCNotTiedToHalt). This setting will override all other
|
---|
459 | * TSC settings except for TSCTicksPerSecond and TSCNotTiedToHalt, which should
|
---|
460 | * be used avoided or used with great care. Note that this will only work right
|
---|
461 | * together with VT-x or AMD-V, and with a single virtual CPU. */
|
---|
462 | rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCTiedToExecution", &pVM->tm.s.fTSCTiedToExecution, false);
|
---|
463 | if (RT_FAILURE(rc))
|
---|
464 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
465 | N_("Configuration error: Failed to querying bool value \"TSCTiedToExecution\""));
|
---|
466 | if (pVM->tm.s.fTSCTiedToExecution && pVM->tm.s.enmTSCMode == TMTSCMODE_NATIVE_API)
|
---|
467 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("/TM/TSCTiedToExecution is not supported in NEM mode!"));
|
---|
468 | if (pVM->tm.s.fTSCTiedToExecution)
|
---|
469 | pVM->tm.s.enmTSCMode = TMTSCMODE_VIRT_TSC_EMULATED;
|
---|
470 |
|
---|
471 |
|
---|
472 | /** @cfgm{/TM/TSCNotTiedToHalt, bool, false}
|
---|
473 | * This is used with /TM/TSCTiedToExecution to control how TSC operates
|
---|
474 | * accross HLT instructions. When true HLT is considered execution time and
|
---|
475 | * TSC continues to run, while when false (default) TSC stops during halt. */
|
---|
476 | rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCNotTiedToHalt", &pVM->tm.s.fTSCNotTiedToHalt, false);
|
---|
477 | if (RT_FAILURE(rc))
|
---|
478 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
479 | N_("Configuration error: Failed to querying bool value \"TSCNotTiedToHalt\""));
|
---|
480 |
|
---|
481 | /*
|
---|
482 | * Configure the timer synchronous virtual time.
|
---|
483 | */
|
---|
484 | /** @cfgm{/TM/ScheduleSlack, uint32_t, ns, 0, UINT32_MAX, 100000}
|
---|
485 | * Scheduling slack when processing timers. */
|
---|
486 | rc = CFGMR3QueryU32(pCfgHandle, "ScheduleSlack", &pVM->tm.s.u32VirtualSyncScheduleSlack);
|
---|
487 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
488 | pVM->tm.s.u32VirtualSyncScheduleSlack = 100000; /* 0.100ms (ASSUMES virtual time is nanoseconds) */
|
---|
489 | else if (RT_FAILURE(rc))
|
---|
490 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
491 | N_("Configuration error: Failed to querying 32-bit integer value \"ScheduleSlack\""));
|
---|
492 |
|
---|
493 | /** @cfgm{/TM/CatchUpStopThreshold, uint64_t, ns, 0, UINT64_MAX, 500000}
|
---|
494 | * When to stop a catch-up, considering it successful. */
|
---|
495 | rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStopThreshold", &pVM->tm.s.u64VirtualSyncCatchUpStopThreshold);
|
---|
496 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
497 | pVM->tm.s.u64VirtualSyncCatchUpStopThreshold = 500000; /* 0.5ms */
|
---|
498 | else if (RT_FAILURE(rc))
|
---|
499 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
500 | N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpStopThreshold\""));
|
---|
501 |
|
---|
502 | /** @cfgm{/TM/CatchUpGiveUpThreshold, uint64_t, ns, 0, UINT64_MAX, 60000000000}
|
---|
503 | * When to give up a catch-up attempt. */
|
---|
504 | rc = CFGMR3QueryU64(pCfgHandle, "CatchUpGiveUpThreshold", &pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold);
|
---|
505 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
506 | pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold = UINT64_C(60000000000); /* 60 sec */
|
---|
507 | else if (RT_FAILURE(rc))
|
---|
508 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
509 | N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpGiveUpThreshold\""));
|
---|
510 |
|
---|
511 |
|
---|
512 | /** @cfgm{/TM/CatchUpPrecentage[0..9], uint32_t, %, 1, 2000, various}
|
---|
513 | * The catch-up percent for a given period. */
|
---|
514 | /** @cfgm{/TM/CatchUpStartThreshold[0..9], uint64_t, ns, 0, UINT64_MAX}
|
---|
515 | * The catch-up period threshold, or if you like, when a period starts. */
|
---|
516 | #define TM_CFG_PERIOD(iPeriod, DefStart, DefPct) \
|
---|
517 | do \
|
---|
518 | { \
|
---|
519 | uint64_t u64; \
|
---|
520 | rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStartThreshold" #iPeriod, &u64); \
|
---|
521 | if (rc == VERR_CFGM_VALUE_NOT_FOUND) \
|
---|
522 | u64 = UINT64_C(DefStart); \
|
---|
523 | else if (RT_FAILURE(rc)) \
|
---|
524 | return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpThreshold" #iPeriod "\"")); \
|
---|
525 | if ( (iPeriod > 0 && u64 <= pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod - 1].u64Start) \
|
---|
526 | || u64 >= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold) \
|
---|
527 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Configuration error: Invalid start of period #" #iPeriod ": %'RU64"), u64); \
|
---|
528 | pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u64Start = u64; \
|
---|
529 | rc = CFGMR3QueryU32(pCfgHandle, "CatchUpPrecentage" #iPeriod, &pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage); \
|
---|
530 | if (rc == VERR_CFGM_VALUE_NOT_FOUND) \
|
---|
531 | pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage = (DefPct); \
|
---|
532 | else if (RT_FAILURE(rc)) \
|
---|
533 | return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 32-bit integer value \"CatchUpPrecentage" #iPeriod "\"")); \
|
---|
534 | } while (0)
|
---|
535 | /* This needs more tuning. Not sure if we really need so many period and be so gentle. */
|
---|
536 | TM_CFG_PERIOD(0, 750000, 5); /* 0.75ms at 1.05x */
|
---|
537 | TM_CFG_PERIOD(1, 1500000, 10); /* 1.50ms at 1.10x */
|
---|
538 | TM_CFG_PERIOD(2, 8000000, 25); /* 8ms at 1.25x */
|
---|
539 | TM_CFG_PERIOD(3, 30000000, 50); /* 30ms at 1.50x */
|
---|
540 | TM_CFG_PERIOD(4, 75000000, 75); /* 75ms at 1.75x */
|
---|
541 | TM_CFG_PERIOD(5, 175000000, 100); /* 175ms at 2x */
|
---|
542 | TM_CFG_PERIOD(6, 500000000, 200); /* 500ms at 3x */
|
---|
543 | TM_CFG_PERIOD(7, 3000000000, 300); /* 3s at 4x */
|
---|
544 | TM_CFG_PERIOD(8,30000000000, 400); /* 30s at 5x */
|
---|
545 | TM_CFG_PERIOD(9,55000000000, 500); /* 55s at 6x */
|
---|
546 | AssertCompile(RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods) == 10);
|
---|
547 | #undef TM_CFG_PERIOD
|
---|
548 |
|
---|
549 | /*
|
---|
550 | * Configure real world time (UTC).
|
---|
551 | */
|
---|
552 | /** @cfgm{/TM/UTCOffset, int64_t, ns, INT64_MIN, INT64_MAX, 0}
|
---|
553 | * The UTC offset. This is used to put the guest back or forwards in time. */
|
---|
554 | rc = CFGMR3QueryS64(pCfgHandle, "UTCOffset", &pVM->tm.s.offUTC);
|
---|
555 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
556 | pVM->tm.s.offUTC = 0; /* ns */
|
---|
557 | else if (RT_FAILURE(rc))
|
---|
558 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
559 | N_("Configuration error: Failed to querying 64-bit integer value \"UTCOffset\""));
|
---|
560 |
|
---|
561 | /** @cfgm{/TM/UTCTouchFileOnJump, string, none}
|
---|
562 | * File to be written to everytime the host time jumps. */
|
---|
563 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "UTCTouchFileOnJump", &pVM->tm.s.pszUtcTouchFileOnJump);
|
---|
564 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
565 | pVM->tm.s.pszUtcTouchFileOnJump = NULL;
|
---|
566 | else if (RT_FAILURE(rc))
|
---|
567 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
568 | N_("Configuration error: Failed to querying string value \"UTCTouchFileOnJump\""));
|
---|
569 |
|
---|
570 | /*
|
---|
571 | * Setup the warp drive.
|
---|
572 | */
|
---|
573 | /** @cfgm{/TM/WarpDrivePercentage, uint32_t, %, 0, 20000, 100}
|
---|
574 | * The warp drive percentage, 100% is normal speed. This is used to speed up
|
---|
575 | * or slow down the virtual clock, which can be useful for fast forwarding
|
---|
576 | * borring periods during tests. */
|
---|
577 | rc = CFGMR3QueryU32(pCfgHandle, "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage);
|
---|
578 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
579 | rc = CFGMR3QueryU32(CFGMR3GetRoot(pVM), "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage); /* legacy */
|
---|
580 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
581 | pVM->tm.s.u32VirtualWarpDrivePercentage = 100;
|
---|
582 | else if (RT_FAILURE(rc))
|
---|
583 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
584 | N_("Configuration error: Failed to querying uint32_t value \"WarpDrivePercent\""));
|
---|
585 | else if ( pVM->tm.s.u32VirtualWarpDrivePercentage < 2
|
---|
586 | || pVM->tm.s.u32VirtualWarpDrivePercentage > 20000)
|
---|
587 | return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
588 | N_("Configuration error: \"WarpDrivePercent\" = %RI32 is not in the range 2..20000"),
|
---|
589 | pVM->tm.s.u32VirtualWarpDrivePercentage);
|
---|
590 | pVM->tm.s.fVirtualWarpDrive = pVM->tm.s.u32VirtualWarpDrivePercentage != 100;
|
---|
591 | if (pVM->tm.s.fVirtualWarpDrive)
|
---|
592 | {
|
---|
593 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_NATIVE_API)
|
---|
594 | LogRel(("TM: Warp-drive active, escept for TSC which is in NEM mode. u32VirtualWarpDrivePercentage=%RI32\n",
|
---|
595 | pVM->tm.s.u32VirtualWarpDrivePercentage));
|
---|
596 | else
|
---|
597 | {
|
---|
598 | pVM->tm.s.enmTSCMode = TMTSCMODE_VIRT_TSC_EMULATED;
|
---|
599 | LogRel(("TM: Warp-drive active. u32VirtualWarpDrivePercentage=%RI32\n", pVM->tm.s.u32VirtualWarpDrivePercentage));
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | /*
|
---|
604 | * Gather the Host Hz configuration values.
|
---|
605 | */
|
---|
606 | rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzMax", &pVM->tm.s.cHostHzMax, 20000);
|
---|
607 | if (RT_FAILURE(rc))
|
---|
608 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
609 | N_("Configuration error: Failed to querying uint32_t value \"HostHzMax\""));
|
---|
610 |
|
---|
611 | rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorTimerCpu", &pVM->tm.s.cPctHostHzFudgeFactorTimerCpu, 111);
|
---|
612 | if (RT_FAILURE(rc))
|
---|
613 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
614 | N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorTimerCpu\""));
|
---|
615 |
|
---|
616 | rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorOtherCpu", &pVM->tm.s.cPctHostHzFudgeFactorOtherCpu, 110);
|
---|
617 | if (RT_FAILURE(rc))
|
---|
618 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
619 | N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorOtherCpu\""));
|
---|
620 |
|
---|
621 | rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorCatchUp100", &pVM->tm.s.cPctHostHzFudgeFactorCatchUp100, 300);
|
---|
622 | if (RT_FAILURE(rc))
|
---|
623 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
624 | N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorCatchUp100\""));
|
---|
625 |
|
---|
626 | rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorCatchUp200", &pVM->tm.s.cPctHostHzFudgeFactorCatchUp200, 250);
|
---|
627 | if (RT_FAILURE(rc))
|
---|
628 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
629 | N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorCatchUp200\""));
|
---|
630 |
|
---|
631 | rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorCatchUp400", &pVM->tm.s.cPctHostHzFudgeFactorCatchUp400, 200);
|
---|
632 | if (RT_FAILURE(rc))
|
---|
633 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
634 | N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorCatchUp400\""));
|
---|
635 |
|
---|
636 | /*
|
---|
637 | * Finally, setup and report.
|
---|
638 | */
|
---|
639 | pVM->tm.s.enmOriginalTSCMode = pVM->tm.s.enmTSCMode;
|
---|
640 | CPUMR3SetCR4Feature(pVM, X86_CR4_TSD, ~X86_CR4_TSD);
|
---|
641 | LogRel(("TM: cTSCTicksPerSecond=%'RU64 (%#RX64) enmTSCMode=%d (%s)\n"
|
---|
642 | "TM: cTSCTicksPerSecondHost=%'RU64 (%#RX64)\n"
|
---|
643 | "TM: TSCTiedToExecution=%RTbool TSCNotTiedToHalt=%RTbool\n",
|
---|
644 | pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.enmTSCMode, tmR3GetTSCModeName(pVM),
|
---|
645 | pVM->tm.s.cTSCTicksPerSecondHost, pVM->tm.s.cTSCTicksPerSecondHost,
|
---|
646 | pVM->tm.s.fTSCTiedToExecution, pVM->tm.s.fTSCNotTiedToHalt));
|
---|
647 |
|
---|
648 | /*
|
---|
649 | * Start the timer (guard against REM not yielding).
|
---|
650 | */
|
---|
651 | /** @cfgm{/TM/TimerMillies, uint32_t, ms, 1, 1000, 10}
|
---|
652 | * The watchdog timer interval. */
|
---|
653 | uint32_t u32Millies;
|
---|
654 | rc = CFGMR3QueryU32(pCfgHandle, "TimerMillies", &u32Millies);
|
---|
655 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
656 | u32Millies = VM_IS_HM_ENABLED(pVM) ? 1000 : 10;
|
---|
657 | else if (RT_FAILURE(rc))
|
---|
658 | return VMSetError(pVM, rc, RT_SRC_POS,
|
---|
659 | N_("Configuration error: Failed to query uint32_t value \"TimerMillies\""));
|
---|
660 | rc = RTTimerCreate(&pVM->tm.s.pTimer, u32Millies, tmR3TimerCallback, pVM);
|
---|
661 | if (RT_FAILURE(rc))
|
---|
662 | {
|
---|
663 | AssertMsgFailed(("Failed to create timer, u32Millies=%d rc=%Rrc.\n", u32Millies, rc));
|
---|
664 | return rc;
|
---|
665 | }
|
---|
666 | Log(("TM: Created timer %p firing every %d milliseconds\n", pVM->tm.s.pTimer, u32Millies));
|
---|
667 | pVM->tm.s.u32TimerMillies = u32Millies;
|
---|
668 |
|
---|
669 | /*
|
---|
670 | * Register saved state.
|
---|
671 | */
|
---|
672 | rc = SSMR3RegisterInternal(pVM, "tm", 1, TM_SAVED_STATE_VERSION, sizeof(uint64_t) * 8,
|
---|
673 | NULL, NULL, NULL,
|
---|
674 | NULL, tmR3Save, NULL,
|
---|
675 | NULL, tmR3Load, NULL);
|
---|
676 | if (RT_FAILURE(rc))
|
---|
677 | return rc;
|
---|
678 |
|
---|
679 | /*
|
---|
680 | * Register statistics.
|
---|
681 | */
|
---|
682 | STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawData.c1nsSteps,STAMTYPE_U32, "/TM/R3/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
|
---|
683 | STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawData.cBadPrev, STAMTYPE_U32, "/TM/R3/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
|
---|
684 | #if 0 /** @todo retreive from ring-0 */
|
---|
685 | 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).");
|
---|
686 | 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).");
|
---|
687 | #endif
|
---|
688 | 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)");
|
---|
689 | 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 attempted caught up with.");
|
---|
690 | STAM_REL_REG( pVM,(void*)&pVM->tm.s.HzHint.s.uMax, STAMTYPE_U32, "/TM/MaxHzHint", STAMUNIT_HZ, "Max guest timer frequency hint.");
|
---|
691 | for (uint32_t i = 0; i < RT_ELEMENTS(pVM->tm.s.aTimerQueues); i++)
|
---|
692 | {
|
---|
693 | rc = STAMR3RegisterF(pVM, (void *)&pVM->tm.s.aTimerQueues[i].uMaxHzHint, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_HZ,
|
---|
694 | "", "/TM/MaxHzHint/%s", pVM->tm.s.aTimerQueues[i].szName);
|
---|
695 | AssertRC(rc);
|
---|
696 | }
|
---|
697 |
|
---|
698 | #ifdef VBOX_WITH_STATISTICS
|
---|
699 | STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawData.cExpired, STAMTYPE_U32, "/TM/R3/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
|
---|
700 | STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawData.cUpdateRaces,STAMTYPE_U32, "/TM/R3/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
|
---|
701 | # if 0 /** @todo retreive from ring-0 */
|
---|
702 | 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).");
|
---|
703 | STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR0.cUpdateRaces,STAMTYPE_U32, "/TM/R0/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
|
---|
704 | # endif
|
---|
705 | STAM_REG(pVM, &pVM->tm.s.StatDoQueues, STAMTYPE_PROFILE, "/TM/DoQueues", STAMUNIT_TICKS_PER_CALL, "Profiling timer TMR3TimerQueuesDo.");
|
---|
706 | STAM_REG(pVM, &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL].StatDo, STAMTYPE_PROFILE, "/TM/DoQueues/Virtual", STAMUNIT_TICKS_PER_CALL, "Time spent on the virtual clock queue.");
|
---|
707 | STAM_REG(pVM, &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].StatDo,STAMTYPE_PROFILE,"/TM/DoQueues/VirtualSync", STAMUNIT_TICKS_PER_CALL, "Time spent on the virtual sync clock queue.");
|
---|
708 | STAM_REG(pVM, &pVM->tm.s.aTimerQueues[TMCLOCK_REAL].StatDo, STAMTYPE_PROFILE, "/TM/DoQueues/Real", STAMUNIT_TICKS_PER_CALL, "Time spent on the real clock queue.");
|
---|
709 |
|
---|
710 | STAM_REG(pVM, &pVM->tm.s.StatPoll, STAMTYPE_COUNTER, "/TM/Poll", STAMUNIT_OCCURENCES, "TMTimerPoll calls.");
|
---|
711 | STAM_REG(pVM, &pVM->tm.s.StatPollAlreadySet, STAMTYPE_COUNTER, "/TM/Poll/AlreadySet", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the FF was already set.");
|
---|
712 | 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.");
|
---|
713 | STAM_REG(pVM, &pVM->tm.s.StatPollMiss, STAMTYPE_COUNTER, "/TM/Poll/Miss", STAMUNIT_OCCURENCES, "TMTimerPoll calls where nothing had expired.");
|
---|
714 | STAM_REG(pVM, &pVM->tm.s.StatPollRunning, STAMTYPE_COUNTER, "/TM/Poll/Running", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the queues were being run.");
|
---|
715 | STAM_REG(pVM, &pVM->tm.s.StatPollSimple, STAMTYPE_COUNTER, "/TM/Poll/Simple", STAMUNIT_OCCURENCES, "TMTimerPoll calls where we could take the simple path.");
|
---|
716 | 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.");
|
---|
717 | 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.");
|
---|
718 |
|
---|
719 | STAM_REG(pVM, &pVM->tm.s.StatPostponedR3, STAMTYPE_COUNTER, "/TM/PostponedR3", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-3.");
|
---|
720 | STAM_REG(pVM, &pVM->tm.s.StatPostponedRZ, STAMTYPE_COUNTER, "/TM/PostponedRZ", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-0 / RC.");
|
---|
721 |
|
---|
722 | 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.");
|
---|
723 | 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.");
|
---|
724 | 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.");
|
---|
725 |
|
---|
726 | STAM_REG(pVM, &pVM->tm.s.StatTimerSet, STAMTYPE_COUNTER, "/TM/TimerSet", STAMUNIT_OCCURENCES, "Calls, except virtual sync timers");
|
---|
727 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetOpt, STAMTYPE_COUNTER, "/TM/TimerSet/Opt", STAMUNIT_OCCURENCES, "Optimized path taken.");
|
---|
728 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetR3, STAMTYPE_PROFILE, "/TM/TimerSet/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-3.");
|
---|
729 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRZ, STAMTYPE_PROFILE, "/TM/TimerSet/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-0 / RC.");
|
---|
730 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetStActive, STAMTYPE_COUNTER, "/TM/TimerSet/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
|
---|
731 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSet/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
|
---|
732 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetStOther, STAMTYPE_COUNTER, "/TM/TimerSet/StOther", STAMUNIT_OCCURENCES, "Other states");
|
---|
733 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendStop, STAMTYPE_COUNTER, "/TM/TimerSet/StPendStop", STAMUNIT_OCCURENCES, "PENDING_STOP");
|
---|
734 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendStopSched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendStopSched", STAMUNIT_OCCURENCES, "PENDING_STOP_SCHEDULE");
|
---|
735 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendSched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendSched", STAMUNIT_OCCURENCES, "PENDING_SCHEDULE");
|
---|
736 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendResched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendResched", STAMUNIT_OCCURENCES, "PENDING_RESCHEDULE");
|
---|
737 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetStStopped, STAMTYPE_COUNTER, "/TM/TimerSet/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
|
---|
738 |
|
---|
739 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetVs, STAMTYPE_COUNTER, "/TM/TimerSetVs", STAMUNIT_OCCURENCES, "TMTimerSet calls on virtual sync timers");
|
---|
740 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsR3, STAMTYPE_PROFILE, "/TM/TimerSetVs/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-3 on virtual sync timers.");
|
---|
741 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsRZ, STAMTYPE_PROFILE, "/TM/TimerSetVs/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-0 / RC on virtual sync timers.");
|
---|
742 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsStActive, STAMTYPE_COUNTER, "/TM/TimerSetVs/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
|
---|
743 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSetVs/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
|
---|
744 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsStStopped, STAMTYPE_COUNTER, "/TM/TimerSetVs/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
|
---|
745 |
|
---|
746 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelative, STAMTYPE_COUNTER, "/TM/TimerSetRelative", STAMUNIT_OCCURENCES, "Calls, except virtual sync timers");
|
---|
747 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeOpt, STAMTYPE_COUNTER, "/TM/TimerSetRelative/Opt", STAMUNIT_OCCURENCES, "Optimized path taken.");
|
---|
748 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeR3, STAMTYPE_PROFILE, "/TM/TimerSetRelative/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetRelative calls made in ring-3 (sans virtual sync).");
|
---|
749 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeRZ, STAMTYPE_PROFILE, "/TM/TimerSetRelative/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetReltaive calls made in ring-0 / RC (sans virtual sync).");
|
---|
750 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStActive, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
|
---|
751 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
|
---|
752 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStOther, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StOther", STAMUNIT_OCCURENCES, "Other states");
|
---|
753 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendStop, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendStop", STAMUNIT_OCCURENCES, "PENDING_STOP");
|
---|
754 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendStopSched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendStopSched",STAMUNIT_OCCURENCES, "PENDING_STOP_SCHEDULE");
|
---|
755 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendSched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendSched", STAMUNIT_OCCURENCES, "PENDING_SCHEDULE");
|
---|
756 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendResched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendResched", STAMUNIT_OCCURENCES, "PENDING_RESCHEDULE");
|
---|
757 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStStopped, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
|
---|
758 |
|
---|
759 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVs, STAMTYPE_COUNTER, "/TM/TimerSetRelativeVs", STAMUNIT_OCCURENCES, "TMTimerSetRelative calls on virtual sync timers");
|
---|
760 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsR3, STAMTYPE_PROFILE, "/TM/TimerSetRelativeVs/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetRelative calls made in ring-3 on virtual sync timers.");
|
---|
761 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsRZ, STAMTYPE_PROFILE, "/TM/TimerSetRelativeVs/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetReltaive calls made in ring-0 / RC on virtual sync timers.");
|
---|
762 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsStActive, STAMTYPE_COUNTER, "/TM/TimerSetRelativeVs/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
|
---|
763 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSetRelativeVs/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
|
---|
764 | STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsStStopped, STAMTYPE_COUNTER, "/TM/TimerSetRelativeVs/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
|
---|
765 |
|
---|
766 | STAM_REG(pVM, &pVM->tm.s.StatTimerStopR3, STAMTYPE_PROFILE, "/TM/TimerStopR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-3.");
|
---|
767 | STAM_REG(pVM, &pVM->tm.s.StatTimerStopRZ, STAMTYPE_PROFILE, "/TM/TimerStopRZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-0 / RC.");
|
---|
768 |
|
---|
769 | 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.");
|
---|
770 | STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSetFF, STAMTYPE_COUNTER, "/TM/VirtualGetSetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling TMTimerGet.");
|
---|
771 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGet, STAMTYPE_COUNTER, "/TM/VirtualSyncGet", STAMUNIT_OCCURENCES, "The number of times tmVirtualSyncGetEx was called.");
|
---|
772 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetAdjLast, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/AdjLast", STAMUNIT_OCCURENCES, "Times we've adjusted against the last returned time stamp .");
|
---|
773 | 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.");
|
---|
774 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetExpired, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Expired", STAMUNIT_OCCURENCES, "Times tmVirtualSyncGetEx encountered an expired timer stopping the clock.");
|
---|
775 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetLocked, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Locked", STAMUNIT_OCCURENCES, "Times we successfully acquired the lock in tmVirtualSyncGetEx.");
|
---|
776 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetLockless, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Lockless", STAMUNIT_OCCURENCES, "Times tmVirtualSyncGetEx returned without needing to take the lock.");
|
---|
777 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetSetFF, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/SetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling tmVirtualSyncGetEx.");
|
---|
778 | STAM_REG(pVM, &pVM->tm.s.StatVirtualPause, STAMTYPE_COUNTER, "/TM/VirtualPause", STAMUNIT_OCCURENCES, "The number of times TMR3TimerPause was called.");
|
---|
779 | STAM_REG(pVM, &pVM->tm.s.StatVirtualResume, STAMTYPE_COUNTER, "/TM/VirtualResume", STAMUNIT_OCCURENCES, "The number of times TMR3TimerResume was called.");
|
---|
780 |
|
---|
781 | STAM_REG(pVM, &pVM->tm.s.StatTimerCallbackSetFF, STAMTYPE_COUNTER, "/TM/CallbackSetFF", STAMUNIT_OCCURENCES, "The number of times the timer callback set FF.");
|
---|
782 | STAM_REG(pVM, &pVM->tm.s.StatTimerCallback, STAMTYPE_COUNTER, "/TM/Callback", STAMUNIT_OCCURENCES, "The number of times the timer callback is invoked.");
|
---|
783 |
|
---|
784 | STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE010, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE010", STAMUNIT_OCCURENCES, "In catch-up mode, 10% or lower.");
|
---|
785 | STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE025, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE025", STAMUNIT_OCCURENCES, "In catch-up mode, 25%-11%.");
|
---|
786 | STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE100, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE100", STAMUNIT_OCCURENCES, "In catch-up mode, 100%-26%.");
|
---|
787 | STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupOther, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupOther", STAMUNIT_OCCURENCES, "In catch-up mode, > 100%.");
|
---|
788 | 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.");
|
---|
789 | STAM_REG(pVM, &pVM->tm.s.StatTSCNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/NotTicking", STAMUNIT_OCCURENCES, "TSC is not ticking.");
|
---|
790 | STAM_REG(pVM, &pVM->tm.s.StatTSCSyncNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/SyncNotTicking", STAMUNIT_OCCURENCES, "VirtualSync isn't ticking.");
|
---|
791 | STAM_REG(pVM, &pVM->tm.s.StatTSCWarp, STAMTYPE_COUNTER, "/TM/TSC/Intercept/Warp", STAMUNIT_OCCURENCES, "Warpdrive is active.");
|
---|
792 | STAM_REG(pVM, &pVM->tm.s.StatTSCSet, STAMTYPE_COUNTER, "/TM/TSC/Sets", STAMUNIT_OCCURENCES, "Calls to TMCpuTickSet.");
|
---|
793 | STAM_REG(pVM, &pVM->tm.s.StatTSCUnderflow, STAMTYPE_COUNTER, "/TM/TSC/Underflow", STAMUNIT_OCCURENCES, "TSC underflow; corrected with last seen value .");
|
---|
794 | STAM_REG(pVM, &pVM->tm.s.StatVirtualPause, STAMTYPE_COUNTER, "/TM/TSC/Pause", STAMUNIT_OCCURENCES, "The number of times the TSC was paused.");
|
---|
795 | STAM_REG(pVM, &pVM->tm.s.StatVirtualResume, STAMTYPE_COUNTER, "/TM/TSC/Resume", STAMUNIT_OCCURENCES, "The number of times the TSC was resumed.");
|
---|
796 | #endif /* VBOX_WITH_STATISTICS */
|
---|
797 |
|
---|
798 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
799 | {
|
---|
800 | PVMCPU pVCpu = pVM->apCpusR3[i];
|
---|
801 | STAMR3RegisterF(pVM, &pVCpu->tm.s.offTSCRawSrc, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS, "TSC offset relative the raw source", "/TM/TSC/offCPU%u", i);
|
---|
802 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
803 | # if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
|
---|
804 | STAMR3RegisterF(pVM, &pVCpu->tm.s.StatNsTotal, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Resettable: Total CPU run time.", "/TM/CPU/%02u", i);
|
---|
805 | STAMR3RegisterF(pVM, &pVCpu->tm.s.StatNsExecuting, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent executing guest code.", "/TM/CPU/%02u/PrfExecuting", i);
|
---|
806 | STAMR3RegisterF(pVM, &pVCpu->tm.s.StatNsExecLong, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent executing guest code - long hauls.", "/TM/CPU/%02u/PrfExecLong", i);
|
---|
807 | STAMR3RegisterF(pVM, &pVCpu->tm.s.StatNsExecShort, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent executing guest code - short stretches.", "/TM/CPU/%02u/PrfExecShort", i);
|
---|
808 | STAMR3RegisterF(pVM, &pVCpu->tm.s.StatNsExecTiny, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent executing guest code - tiny bits.", "/TM/CPU/%02u/PrfExecTiny", i);
|
---|
809 | STAMR3RegisterF(pVM, &pVCpu->tm.s.StatNsHalted, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent halted.", "/TM/CPU/%02u/PrfHalted", i);
|
---|
810 | STAMR3RegisterF(pVM, &pVCpu->tm.s.StatNsOther, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent in the VMM or preempted.", "/TM/CPU/%02u/PrfOther", i);
|
---|
811 | # endif
|
---|
812 | STAMR3RegisterF(pVM, &pVCpu->tm.s.cNsTotalStat, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Total CPU run time.", "/TM/CPU/%02u/cNsTotal", i);
|
---|
813 | STAMR3RegisterF(pVM, &pVCpu->tm.s.cNsExecuting, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent executing guest code.", "/TM/CPU/%02u/cNsExecuting", i);
|
---|
814 | STAMR3RegisterF(pVM, &pVCpu->tm.s.cNsHalted, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent halted.", "/TM/CPU/%02u/cNsHalted", i);
|
---|
815 | STAMR3RegisterF(pVM, &pVCpu->tm.s.cNsOtherStat, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent in the VMM or preempted.", "/TM/CPU/%02u/cNsOther", i);
|
---|
816 | STAMR3RegisterF(pVM, &pVCpu->tm.s.cPeriodsExecuting, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Times executed guest code.", "/TM/CPU/%02u/cPeriodsExecuting", i);
|
---|
817 | STAMR3RegisterF(pVM, &pVCpu->tm.s.cPeriodsHalted, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Times halted.", "/TM/CPU/%02u/cPeriodsHalted", i);
|
---|
818 | STAMR3RegisterF(pVM, &pVCpu->tm.s.CpuLoad.cPctExecuting, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent executing guest code recently.", "/TM/CPU/%02u/pctExecuting", i);
|
---|
819 | STAMR3RegisterF(pVM, &pVCpu->tm.s.CpuLoad.cPctHalted, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent halted recently.", "/TM/CPU/%02u/pctHalted", i);
|
---|
820 | STAMR3RegisterF(pVM, &pVCpu->tm.s.CpuLoad.cPctOther, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent in the VMM or preempted recently.", "/TM/CPU/%02u/pctOther", i);
|
---|
821 | #endif
|
---|
822 | }
|
---|
823 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
824 | STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctExecuting, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent executing guest code recently.", "/TM/CPU/pctExecuting");
|
---|
825 | STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctHalted, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent halted recently.", "/TM/CPU/pctHalted");
|
---|
826 | STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctOther, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent in the VMM or preempted recently.", "/TM/CPU/pctOther");
|
---|
827 | #endif
|
---|
828 |
|
---|
829 | #ifdef VBOX_WITH_STATISTICS
|
---|
830 | 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.");
|
---|
831 | STAM_REG(pVM, (void *)&pVM->tm.s.fVirtualSyncCatchUp, STAMTYPE_U8, "/TM/VirtualSync/CatchUpActive", STAMUNIT_NONE, "Catch-Up active indicator.");
|
---|
832 | 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)");
|
---|
833 | 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.");
|
---|
834 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGiveUp, STAMTYPE_COUNTER, "/TM/VirtualSync/GiveUp", STAMUNIT_OCCURENCES, "Times the catch-up was abandoned.");
|
---|
835 | 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++.)");
|
---|
836 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRun, STAMTYPE_COUNTER, "/TM/VirtualSync/Run", STAMUNIT_OCCURENCES, "Times the virtual sync timer queue was considered.");
|
---|
837 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunRestart, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/Restarts", STAMUNIT_OCCURENCES, "Times the clock was restarted after a run.");
|
---|
838 | 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.");
|
---|
839 | STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunStoppedAlready, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/StoppedAlready", STAMUNIT_OCCURENCES, "Times the clock was already stopped elsewhere (TMVirtualSyncGet).");
|
---|
840 | 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.)");
|
---|
841 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods); i++)
|
---|
842 | {
|
---|
843 | STAMR3RegisterF(pVM, &pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "The catch-up percentage.", "/TM/VirtualSync/Periods/%u", i);
|
---|
844 | STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupAdjust[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times adjusted to this period.", "/TM/VirtualSync/Periods/%u/Adjust", i);
|
---|
845 | STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupInitial[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times started in this period.", "/TM/VirtualSync/Periods/%u/Initial", i);
|
---|
846 | 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);
|
---|
847 | }
|
---|
848 | #endif /* VBOX_WITH_STATISTICS */
|
---|
849 |
|
---|
850 | /*
|
---|
851 | * Register info handlers.
|
---|
852 | */
|
---|
853 | DBGFR3InfoRegisterInternalEx(pVM, "timers", "Dumps all timers. No arguments.", tmR3TimerInfo, DBGFINFO_FLAGS_RUN_ON_EMT);
|
---|
854 | DBGFR3InfoRegisterInternalEx(pVM, "activetimers", "Dumps active all timers. No arguments.", tmR3TimerInfoActive, DBGFINFO_FLAGS_RUN_ON_EMT);
|
---|
855 | DBGFR3InfoRegisterInternalEx(pVM, "clocks", "Display the time of the various clocks.", tmR3InfoClocks, DBGFINFO_FLAGS_RUN_ON_EMT);
|
---|
856 | DBGFR3InfoRegisterInternalArgv(pVM, "cpuload", "Display the CPU load stats (--help for details).", tmR3InfoCpuLoad, 0);
|
---|
857 |
|
---|
858 | return VINF_SUCCESS;
|
---|
859 | }
|
---|
860 |
|
---|
861 |
|
---|
862 | /**
|
---|
863 | * Checks if the host CPU has a fixed TSC frequency.
|
---|
864 | *
|
---|
865 | * @returns true if it has, false if it hasn't.
|
---|
866 | *
|
---|
867 | * @remarks This test doesn't bother with very old CPUs that don't do power
|
---|
868 | * management or any other stuff that might influence the TSC rate.
|
---|
869 | * This isn't currently relevant.
|
---|
870 | */
|
---|
871 | static bool tmR3HasFixedTSC(PVM pVM)
|
---|
872 | {
|
---|
873 | /*
|
---|
874 | * ASSUME that if the GIP is in invariant TSC mode, it's because the CPU
|
---|
875 | * actually has invariant TSC.
|
---|
876 | *
|
---|
877 | * In driverless mode we just assume sync TSC for now regardless of what
|
---|
878 | * the case actually is.
|
---|
879 | */
|
---|
880 | PSUPGLOBALINFOPAGE const pGip = g_pSUPGlobalInfoPage;
|
---|
881 | SUPGIPMODE const enmGipMode = pGip ? (SUPGIPMODE)pGip->u32Mode : SUPGIPMODE_INVARIANT_TSC;
|
---|
882 | if (enmGipMode == SUPGIPMODE_INVARIANT_TSC)
|
---|
883 | return true;
|
---|
884 |
|
---|
885 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
886 | /*
|
---|
887 | * Go by features and model info from the CPUID instruction.
|
---|
888 | */
|
---|
889 | if (ASMHasCpuId())
|
---|
890 | {
|
---|
891 | uint32_t uEAX, uEBX, uECX, uEDX;
|
---|
892 |
|
---|
893 | /*
|
---|
894 | * By feature. (Used to be AMD specific, intel seems to have picked it up.)
|
---|
895 | */
|
---|
896 | ASMCpuId(0x80000000, &uEAX, &uEBX, &uECX, &uEDX);
|
---|
897 | if (uEAX >= 0x80000007 && RTX86IsValidExtRange(uEAX))
|
---|
898 | {
|
---|
899 | ASMCpuId(0x80000007, &uEAX, &uEBX, &uECX, &uEDX);
|
---|
900 | if ( (uEDX & X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR) /* TscInvariant */
|
---|
901 | && enmGipMode != SUPGIPMODE_ASYNC_TSC) /* No fixed tsc if the gip timer is in async mode. */
|
---|
902 | return true;
|
---|
903 | }
|
---|
904 |
|
---|
905 | /*
|
---|
906 | * By model.
|
---|
907 | */
|
---|
908 | if (CPUMGetHostCpuVendor(pVM) == CPUMCPUVENDOR_AMD)
|
---|
909 | {
|
---|
910 | /*
|
---|
911 | * AuthenticAMD - Check for APM support and that TscInvariant is set.
|
---|
912 | *
|
---|
913 | * This test isn't correct with respect to fixed/non-fixed TSC and
|
---|
914 | * older models, but this isn't relevant since the result is currently
|
---|
915 | * only used for making a decision on AMD-V models.
|
---|
916 | */
|
---|
917 | # if 0 /* Promoted to generic */
|
---|
918 | ASMCpuId(0x80000000, &uEAX, &uEBX, &uECX, &uEDX);
|
---|
919 | if (uEAX >= 0x80000007)
|
---|
920 | {
|
---|
921 | ASMCpuId(0x80000007, &uEAX, &uEBX, &uECX, &uEDX);
|
---|
922 | if ( (uEDX & X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR) /* TscInvariant */
|
---|
923 | && ( enmGipMode == SUPGIPMODE_SYNC_TSC /* No fixed tsc if the gip timer is in async mode. */
|
---|
924 | || enmGipMode == SUPGIPMODE_INVARIANT_TSC))
|
---|
925 | return true;
|
---|
926 | }
|
---|
927 | # endif
|
---|
928 | }
|
---|
929 | else if (CPUMGetHostCpuVendor(pVM) == CPUMCPUVENDOR_INTEL)
|
---|
930 | {
|
---|
931 | /*
|
---|
932 | * GenuineIntel - Check the model number.
|
---|
933 | *
|
---|
934 | * This test is lacking in the same way and for the same reasons
|
---|
935 | * as the AMD test above.
|
---|
936 | */
|
---|
937 | /** @todo use RTX86GetCpuFamily() and RTX86GetCpuModel() here. */
|
---|
938 | ASMCpuId(1, &uEAX, &uEBX, &uECX, &uEDX);
|
---|
939 | unsigned uModel = (uEAX >> 4) & 0x0f;
|
---|
940 | unsigned uFamily = (uEAX >> 8) & 0x0f;
|
---|
941 | if (uFamily == 0x0f)
|
---|
942 | uFamily += (uEAX >> 20) & 0xff;
|
---|
943 | if (uFamily >= 0x06)
|
---|
944 | uModel += ((uEAX >> 16) & 0x0f) << 4;
|
---|
945 | if ( (uFamily == 0x0f /*P4*/ && uModel >= 0x03)
|
---|
946 | || (uFamily == 0x06 /*P2/P3*/ && uModel >= 0x0e))
|
---|
947 | return true;
|
---|
948 | }
|
---|
949 | else if (CPUMGetHostCpuVendor(pVM) == CPUMCPUVENDOR_VIA)
|
---|
950 | {
|
---|
951 | /*
|
---|
952 | * CentaurHauls - Check the model, family and stepping.
|
---|
953 | *
|
---|
954 | * This only checks for VIA CPU models Nano X2, Nano X3,
|
---|
955 | * Eden X2 and QuadCore.
|
---|
956 | */
|
---|
957 | /** @todo use RTX86GetCpuFamily() and RTX86GetCpuModel() here. */
|
---|
958 | ASMCpuId(1, &uEAX, &uEBX, &uECX, &uEDX);
|
---|
959 | unsigned uStepping = (uEAX & 0x0f);
|
---|
960 | unsigned uModel = (uEAX >> 4) & 0x0f;
|
---|
961 | unsigned uFamily = (uEAX >> 8) & 0x0f;
|
---|
962 | if ( uFamily == 0x06
|
---|
963 | && uModel == 0x0f
|
---|
964 | && uStepping >= 0x0c
|
---|
965 | && uStepping <= 0x0f)
|
---|
966 | return true;
|
---|
967 | }
|
---|
968 | else if (CPUMGetHostCpuVendor(pVM) == CPUMCPUVENDOR_SHANGHAI)
|
---|
969 | {
|
---|
970 | /*
|
---|
971 | * Shanghai - Check the model, family and stepping.
|
---|
972 | */
|
---|
973 | /** @todo use RTX86GetCpuFamily() and RTX86GetCpuModel() here. */
|
---|
974 | ASMCpuId(1, &uEAX, &uEBX, &uECX, &uEDX);
|
---|
975 | unsigned uFamily = (uEAX >> 8) & 0x0f;
|
---|
976 | if ( uFamily == 0x06
|
---|
977 | || uFamily == 0x07)
|
---|
978 | {
|
---|
979 | return true;
|
---|
980 | }
|
---|
981 | }
|
---|
982 | }
|
---|
983 |
|
---|
984 | # else /* !X86 && !AMD64 */
|
---|
985 | RT_NOREF_PV(pVM);
|
---|
986 | # endif /* !X86 && !AMD64 */
|
---|
987 | return false;
|
---|
988 | }
|
---|
989 |
|
---|
990 |
|
---|
991 | /**
|
---|
992 | * Calibrate the CPU tick.
|
---|
993 | *
|
---|
994 | * @returns Number of ticks per second.
|
---|
995 | */
|
---|
996 | static uint64_t tmR3CalibrateTSC(void)
|
---|
997 | {
|
---|
998 | uint64_t u64Hz;
|
---|
999 |
|
---|
1000 | /*
|
---|
1001 | * Use GIP when available. Prefere the nominal one, no need to wait for it.
|
---|
1002 | */
|
---|
1003 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
1004 | if (pGip)
|
---|
1005 | {
|
---|
1006 | u64Hz = pGip->u64CpuHz;
|
---|
1007 | if (u64Hz < _1T && u64Hz > _1M)
|
---|
1008 | return u64Hz;
|
---|
1009 | AssertFailed(); /* This shouldn't happen. */
|
---|
1010 |
|
---|
1011 | u64Hz = SUPGetCpuHzFromGip(pGip);
|
---|
1012 | if (u64Hz < _1T && u64Hz > _1M)
|
---|
1013 | return u64Hz;
|
---|
1014 |
|
---|
1015 | AssertFailed(); /* This shouldn't happen. */
|
---|
1016 | }
|
---|
1017 | else
|
---|
1018 | Assert(SUPR3IsDriverless());
|
---|
1019 |
|
---|
1020 | /* Call this once first to make sure it's initialized. */
|
---|
1021 | RTTimeNanoTS();
|
---|
1022 |
|
---|
1023 | /*
|
---|
1024 | * Yield the CPU to increase our chances of getting a correct value.
|
---|
1025 | */
|
---|
1026 | RTThreadYield(); /* Try avoid interruptions between TSC and NanoTS samplings. */
|
---|
1027 | static const unsigned s_auSleep[5] = { 50, 30, 30, 40, 40 };
|
---|
1028 | uint64_t au64Samples[5];
|
---|
1029 | unsigned i;
|
---|
1030 | for (i = 0; i < RT_ELEMENTS(au64Samples); i++)
|
---|
1031 | {
|
---|
1032 | RTMSINTERVAL cMillies;
|
---|
1033 | int cTries = 5;
|
---|
1034 | uint64_t u64Start = ASMReadTSC();
|
---|
1035 | uint64_t u64End;
|
---|
1036 | uint64_t StartTS = RTTimeNanoTS();
|
---|
1037 | uint64_t EndTS;
|
---|
1038 | do
|
---|
1039 | {
|
---|
1040 | RTThreadSleep(s_auSleep[i]);
|
---|
1041 | u64End = ASMReadTSC();
|
---|
1042 | EndTS = RTTimeNanoTS();
|
---|
1043 | cMillies = (RTMSINTERVAL)((EndTS - StartTS + 500000) / 1000000);
|
---|
1044 | } while ( cMillies == 0 /* the sleep may be interrupted... */
|
---|
1045 | || (cMillies < 20 && --cTries > 0));
|
---|
1046 | uint64_t u64Diff = u64End - u64Start;
|
---|
1047 |
|
---|
1048 | au64Samples[i] = (u64Diff * 1000) / cMillies;
|
---|
1049 | AssertMsg(cTries > 0, ("cMillies=%d i=%d\n", cMillies, i));
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | /*
|
---|
1053 | * Discard the highest and lowest results and calculate the average.
|
---|
1054 | */
|
---|
1055 | unsigned iHigh = 0;
|
---|
1056 | unsigned iLow = 0;
|
---|
1057 | for (i = 1; i < RT_ELEMENTS(au64Samples); i++)
|
---|
1058 | {
|
---|
1059 | if (au64Samples[i] < au64Samples[iLow])
|
---|
1060 | iLow = i;
|
---|
1061 | if (au64Samples[i] > au64Samples[iHigh])
|
---|
1062 | iHigh = i;
|
---|
1063 | }
|
---|
1064 | au64Samples[iLow] = 0;
|
---|
1065 | au64Samples[iHigh] = 0;
|
---|
1066 |
|
---|
1067 | u64Hz = au64Samples[0];
|
---|
1068 | for (i = 1; i < RT_ELEMENTS(au64Samples); i++)
|
---|
1069 | u64Hz += au64Samples[i];
|
---|
1070 | u64Hz /= RT_ELEMENTS(au64Samples) - 2;
|
---|
1071 |
|
---|
1072 | return u64Hz;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 |
|
---|
1076 | /**
|
---|
1077 | * Finalizes the TM initialization.
|
---|
1078 | *
|
---|
1079 | * @returns VBox status code.
|
---|
1080 | * @param pVM The cross context VM structure.
|
---|
1081 | */
|
---|
1082 | VMM_INT_DECL(int) TMR3InitFinalize(PVM pVM)
|
---|
1083 | {
|
---|
1084 | int rc;
|
---|
1085 |
|
---|
1086 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
1087 | /*
|
---|
1088 | * Create a timer for refreshing the CPU load stats.
|
---|
1089 | */
|
---|
1090 | TMTIMERHANDLE hTimer;
|
---|
1091 | rc = TMR3TimerCreate(pVM, TMCLOCK_REAL, tmR3CpuLoadTimer, NULL, TMTIMER_FLAGS_NO_RING0, "CPU Load Timer", &hTimer);
|
---|
1092 | if (RT_SUCCESS(rc))
|
---|
1093 | rc = TMTimerSetMillies(pVM, hTimer, 1000);
|
---|
1094 | #endif
|
---|
1095 |
|
---|
1096 | /*
|
---|
1097 | * GIM is now initialized. Determine if TSC mode switching is allowed (respecting CFGM override).
|
---|
1098 | */
|
---|
1099 | pVM->tm.s.fTSCModeSwitchAllowed &= tmR3HasFixedTSC(pVM) && GIMIsEnabled(pVM);
|
---|
1100 | LogRel(("TM: TMR3InitFinalize: fTSCModeSwitchAllowed=%RTbool\n", pVM->tm.s.fTSCModeSwitchAllowed));
|
---|
1101 |
|
---|
1102 | /*
|
---|
1103 | * Grow the virtual & real timer tables so we've got sufficient
|
---|
1104 | * space for dynamically created timers. We cannot allocate more
|
---|
1105 | * after ring-0 init completes.
|
---|
1106 | */
|
---|
1107 | static struct { uint32_t idxQueue, cExtra; } s_aExtra[] = { {TMCLOCK_VIRTUAL, 128}, {TMCLOCK_REAL, 32} };
|
---|
1108 | for (uint32_t i = 0; i < RT_ELEMENTS(s_aExtra); i++)
|
---|
1109 | {
|
---|
1110 | PTMTIMERQUEUE pQueue = &pVM->tm.s.aTimerQueues[s_aExtra[i].idxQueue];
|
---|
1111 | PDMCritSectRwEnterExcl(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
1112 | if (s_aExtra[i].cExtra > pQueue->cTimersFree)
|
---|
1113 | {
|
---|
1114 | uint32_t cTimersAlloc = pQueue->cTimersAlloc + s_aExtra[i].cExtra - pQueue->cTimersFree;
|
---|
1115 | rc = tmR3TimerQueueGrow(pVM, pQueue, cTimersAlloc);
|
---|
1116 | AssertLogRelMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc cTimersAlloc=%u %s\n", rc, cTimersAlloc, pQueue->szName), rc);
|
---|
1117 | }
|
---|
1118 | PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | #ifdef VBOX_WITH_STATISTICS
|
---|
1122 | /*
|
---|
1123 | * Register timer statistics now that we've fixed the timer table sizes.
|
---|
1124 | */
|
---|
1125 | for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
|
---|
1126 | {
|
---|
1127 | pVM->tm.s.aTimerQueues[idxQueue].fCannotGrow = true;
|
---|
1128 | tmR3TimerQueueRegisterStats(pVM, &pVM->tm.s.aTimerQueues[idxQueue], UINT32_MAX);
|
---|
1129 | }
|
---|
1130 | #endif
|
---|
1131 |
|
---|
1132 | return rc;
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 |
|
---|
1136 | /**
|
---|
1137 | * Applies relocations to data and code managed by this
|
---|
1138 | * component. This function will be called at init and
|
---|
1139 | * whenever the VMM need to relocate it self inside the GC.
|
---|
1140 | *
|
---|
1141 | * @param pVM The cross context VM structure.
|
---|
1142 | * @param offDelta Relocation delta relative to old location.
|
---|
1143 | */
|
---|
1144 | VMM_INT_DECL(void) TMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
|
---|
1145 | {
|
---|
1146 | LogFlow(("TMR3Relocate\n"));
|
---|
1147 | RT_NOREF(pVM, offDelta);
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 |
|
---|
1151 | /**
|
---|
1152 | * Terminates the TM.
|
---|
1153 | *
|
---|
1154 | * Termination means cleaning up and freeing all resources,
|
---|
1155 | * the VM it self is at this point powered off or suspended.
|
---|
1156 | *
|
---|
1157 | * @returns VBox status code.
|
---|
1158 | * @param pVM The cross context VM structure.
|
---|
1159 | */
|
---|
1160 | VMM_INT_DECL(int) TMR3Term(PVM pVM)
|
---|
1161 | {
|
---|
1162 | if (pVM->tm.s.pTimer)
|
---|
1163 | {
|
---|
1164 | int rc = RTTimerDestroy(pVM->tm.s.pTimer);
|
---|
1165 | AssertRC(rc);
|
---|
1166 | pVM->tm.s.pTimer = NULL;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | return VINF_SUCCESS;
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 |
|
---|
1173 | /**
|
---|
1174 | * The VM is being reset.
|
---|
1175 | *
|
---|
1176 | * For the TM component this means that a rescheduling is preformed,
|
---|
1177 | * the FF is cleared and but without running the queues. We'll have to
|
---|
1178 | * check if this makes sense or not, but it seems like a good idea now....
|
---|
1179 | *
|
---|
1180 | * @param pVM The cross context VM structure.
|
---|
1181 | */
|
---|
1182 | VMM_INT_DECL(void) TMR3Reset(PVM pVM)
|
---|
1183 | {
|
---|
1184 | LogFlow(("TMR3Reset:\n"));
|
---|
1185 | VM_ASSERT_EMT(pVM);
|
---|
1186 |
|
---|
1187 | /*
|
---|
1188 | * Abort any pending catch up.
|
---|
1189 | * This isn't perfect...
|
---|
1190 | */
|
---|
1191 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
1192 | {
|
---|
1193 | const uint64_t offVirtualNow = TMVirtualGetNoCheck(pVM);
|
---|
1194 | const uint64_t offVirtualSyncNow = TMVirtualSyncGetNoCheck(pVM);
|
---|
1195 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
1196 | {
|
---|
1197 | STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
|
---|
1198 |
|
---|
1199 | const uint64_t offOld = pVM->tm.s.offVirtualSyncGivenUp;
|
---|
1200 | const uint64_t offNew = offVirtualNow - offVirtualSyncNow;
|
---|
1201 | Assert(offOld <= offNew);
|
---|
1202 | ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
|
---|
1203 | ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSync, offNew);
|
---|
1204 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
|
---|
1205 | LogRel(("TM: Aborting catch-up attempt on reset with a %'RU64 ns lag on reset; new total: %'RU64 ns\n", offNew - offOld, offNew));
|
---|
1206 | }
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | /*
|
---|
1210 | * Process the queues.
|
---|
1211 | */
|
---|
1212 | for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
|
---|
1213 | {
|
---|
1214 | PTMTIMERQUEUE pQueue = &pVM->tm.s.aTimerQueues[idxQueue];
|
---|
1215 | PDMCritSectEnter(pVM, &pQueue->TimerLock, VERR_IGNORED);
|
---|
1216 | tmTimerQueueSchedule(pVM, pQueue, pQueue);
|
---|
1217 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
1218 | }
|
---|
1219 | #ifdef VBOX_STRICT
|
---|
1220 | tmTimerQueuesSanityChecks(pVM, "TMR3Reset");
|
---|
1221 | #endif
|
---|
1222 |
|
---|
1223 | PVMCPU pVCpuDst = pVM->apCpusR3[pVM->tm.s.idTimerCpu];
|
---|
1224 | VMCPU_FF_CLEAR(pVCpuDst, VMCPU_FF_TIMER); /** @todo FIXME: this isn't right. */
|
---|
1225 |
|
---|
1226 | /*
|
---|
1227 | * Switch TM TSC mode back to the original mode after a reset for
|
---|
1228 | * paravirtualized guests that alter the TM TSC mode during operation.
|
---|
1229 | * We're already in an EMT rendezvous at this point.
|
---|
1230 | */
|
---|
1231 | if ( pVM->tm.s.fTSCModeSwitchAllowed
|
---|
1232 | && pVM->tm.s.enmTSCMode != pVM->tm.s.enmOriginalTSCMode)
|
---|
1233 | {
|
---|
1234 | VM_ASSERT_EMT0(pVM);
|
---|
1235 | tmR3CpuTickParavirtDisable(pVM, pVM->apCpusR3[0], NULL /* pvData */);
|
---|
1236 | }
|
---|
1237 | Assert(!GIMIsParavirtTscEnabled(pVM));
|
---|
1238 | pVM->tm.s.fParavirtTscEnabled = false;
|
---|
1239 |
|
---|
1240 | /*
|
---|
1241 | * Reset TSC to avoid a Windows 8+ bug (see @bugref{8926}). If Windows
|
---|
1242 | * sees TSC value beyond 0x40000000000 at startup, it will reset the
|
---|
1243 | * TSC on boot-up CPU only, causing confusion and mayhem with SMP.
|
---|
1244 | */
|
---|
1245 | VM_ASSERT_EMT0(pVM);
|
---|
1246 | uint64_t offTscRawSrc;
|
---|
1247 | switch (pVM->tm.s.enmTSCMode)
|
---|
1248 | {
|
---|
1249 | case TMTSCMODE_REAL_TSC_OFFSET:
|
---|
1250 | offTscRawSrc = SUPReadTsc();
|
---|
1251 | break;
|
---|
1252 | case TMTSCMODE_DYNAMIC:
|
---|
1253 | case TMTSCMODE_VIRT_TSC_EMULATED:
|
---|
1254 | offTscRawSrc = TMVirtualSyncGetNoCheck(pVM);
|
---|
1255 | offTscRawSrc = ASMMultU64ByU32DivByU32(offTscRawSrc, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL);
|
---|
1256 | break;
|
---|
1257 | case TMTSCMODE_NATIVE_API:
|
---|
1258 | /** @todo NEM TSC reset on reset for Windows8+ bug workaround. */
|
---|
1259 | offTscRawSrc = 0;
|
---|
1260 | break;
|
---|
1261 | default:
|
---|
1262 | AssertFailedBreakStmt(offTscRawSrc = 0);
|
---|
1263 | }
|
---|
1264 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
1265 | {
|
---|
1266 | PVMCPU pVCpu = pVM->apCpusR3[idCpu];
|
---|
1267 | pVCpu->tm.s.offTSCRawSrc = offTscRawSrc;
|
---|
1268 | pVCpu->tm.s.u64TSC = 0;
|
---|
1269 | pVCpu->tm.s.u64TSCLastSeen = 0;
|
---|
1270 | }
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 |
|
---|
1274 | /**
|
---|
1275 | * Execute state save operation.
|
---|
1276 | *
|
---|
1277 | * @returns VBox status code.
|
---|
1278 | * @param pVM The cross context VM structure.
|
---|
1279 | * @param pSSM SSM operation handle.
|
---|
1280 | */
|
---|
1281 | static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM)
|
---|
1282 | {
|
---|
1283 | LogFlow(("tmR3Save:\n"));
|
---|
1284 | #ifdef VBOX_STRICT
|
---|
1285 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
1286 | {
|
---|
1287 | PVMCPU pVCpu = pVM->apCpusR3[i];
|
---|
1288 | Assert(!pVCpu->tm.s.fTSCTicking);
|
---|
1289 | }
|
---|
1290 | Assert(!pVM->tm.s.cVirtualTicking);
|
---|
1291 | Assert(!pVM->tm.s.fVirtualSyncTicking);
|
---|
1292 | Assert(!pVM->tm.s.cTSCsTicking);
|
---|
1293 | #endif
|
---|
1294 |
|
---|
1295 | /*
|
---|
1296 | * Save the virtual clocks.
|
---|
1297 | */
|
---|
1298 | /* the virtual clock. */
|
---|
1299 | SSMR3PutU64(pSSM, TMCLOCK_FREQ_VIRTUAL);
|
---|
1300 | SSMR3PutU64(pSSM, pVM->tm.s.u64Virtual);
|
---|
1301 |
|
---|
1302 | /* the virtual timer synchronous clock. */
|
---|
1303 | SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSync);
|
---|
1304 | SSMR3PutU64(pSSM, pVM->tm.s.offVirtualSync);
|
---|
1305 | SSMR3PutU64(pSSM, pVM->tm.s.offVirtualSyncGivenUp);
|
---|
1306 | SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSyncCatchUpPrev);
|
---|
1307 | SSMR3PutBool(pSSM, pVM->tm.s.fVirtualSyncCatchUp);
|
---|
1308 |
|
---|
1309 | /* real time clock */
|
---|
1310 | SSMR3PutU64(pSSM, TMCLOCK_FREQ_REAL);
|
---|
1311 |
|
---|
1312 | /* the cpu tick clock. */
|
---|
1313 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
1314 | {
|
---|
1315 | PVMCPU pVCpu = pVM->apCpusR3[i];
|
---|
1316 | SSMR3PutU64(pSSM, TMCpuTickGet(pVCpu));
|
---|
1317 | }
|
---|
1318 | return SSMR3PutU64(pSSM, pVM->tm.s.cTSCTicksPerSecond);
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 |
|
---|
1322 | /**
|
---|
1323 | * Execute state load operation.
|
---|
1324 | *
|
---|
1325 | * @returns VBox status code.
|
---|
1326 | * @param pVM The cross context VM structure.
|
---|
1327 | * @param pSSM SSM operation handle.
|
---|
1328 | * @param uVersion Data layout version.
|
---|
1329 | * @param uPass The data pass.
|
---|
1330 | */
|
---|
1331 | static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
1332 | {
|
---|
1333 | LogFlow(("tmR3Load:\n"));
|
---|
1334 |
|
---|
1335 | Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
|
---|
1336 | #ifdef VBOX_STRICT
|
---|
1337 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
1338 | {
|
---|
1339 | PVMCPU pVCpu = pVM->apCpusR3[i];
|
---|
1340 | Assert(!pVCpu->tm.s.fTSCTicking);
|
---|
1341 | }
|
---|
1342 | Assert(!pVM->tm.s.cVirtualTicking);
|
---|
1343 | Assert(!pVM->tm.s.fVirtualSyncTicking);
|
---|
1344 | Assert(!pVM->tm.s.cTSCsTicking);
|
---|
1345 | #endif
|
---|
1346 |
|
---|
1347 | /*
|
---|
1348 | * Validate version.
|
---|
1349 | */
|
---|
1350 | if (uVersion != TM_SAVED_STATE_VERSION)
|
---|
1351 | {
|
---|
1352 | AssertMsgFailed(("tmR3Load: Invalid version uVersion=%d!\n", uVersion));
|
---|
1353 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | /*
|
---|
1357 | * Load the virtual clock.
|
---|
1358 | */
|
---|
1359 | pVM->tm.s.cVirtualTicking = 0;
|
---|
1360 | /* the virtual clock. */
|
---|
1361 | uint64_t u64Hz;
|
---|
1362 | int rc = SSMR3GetU64(pSSM, &u64Hz);
|
---|
1363 | if (RT_FAILURE(rc))
|
---|
1364 | return rc;
|
---|
1365 | if (u64Hz != TMCLOCK_FREQ_VIRTUAL)
|
---|
1366 | {
|
---|
1367 | AssertMsgFailed(("The virtual clock frequency differs! Saved: %'RU64 Binary: %'RU64\n",
|
---|
1368 | u64Hz, TMCLOCK_FREQ_VIRTUAL));
|
---|
1369 | return VERR_SSM_VIRTUAL_CLOCK_HZ;
|
---|
1370 | }
|
---|
1371 | SSMR3GetU64(pSSM, &pVM->tm.s.u64Virtual);
|
---|
1372 | pVM->tm.s.u64VirtualOffset = 0;
|
---|
1373 |
|
---|
1374 | /* the virtual timer synchronous clock. */
|
---|
1375 | pVM->tm.s.fVirtualSyncTicking = false;
|
---|
1376 | uint64_t u64;
|
---|
1377 | SSMR3GetU64(pSSM, &u64);
|
---|
1378 | pVM->tm.s.u64VirtualSync = u64;
|
---|
1379 | SSMR3GetU64(pSSM, &u64);
|
---|
1380 | pVM->tm.s.offVirtualSync = u64;
|
---|
1381 | SSMR3GetU64(pSSM, &u64);
|
---|
1382 | pVM->tm.s.offVirtualSyncGivenUp = u64;
|
---|
1383 | SSMR3GetU64(pSSM, &u64);
|
---|
1384 | pVM->tm.s.u64VirtualSyncCatchUpPrev = u64;
|
---|
1385 | bool f;
|
---|
1386 | SSMR3GetBool(pSSM, &f);
|
---|
1387 | pVM->tm.s.fVirtualSyncCatchUp = f;
|
---|
1388 |
|
---|
1389 | /* the real clock */
|
---|
1390 | rc = SSMR3GetU64(pSSM, &u64Hz);
|
---|
1391 | if (RT_FAILURE(rc))
|
---|
1392 | return rc;
|
---|
1393 | if (u64Hz != TMCLOCK_FREQ_REAL)
|
---|
1394 | {
|
---|
1395 | AssertMsgFailed(("The real clock frequency differs! Saved: %'RU64 Binary: %'RU64\n",
|
---|
1396 | u64Hz, TMCLOCK_FREQ_REAL));
|
---|
1397 | return VERR_SSM_VIRTUAL_CLOCK_HZ; /* misleading... */
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /* the cpu tick clock. */
|
---|
1401 | pVM->tm.s.cTSCsTicking = 0;
|
---|
1402 | pVM->tm.s.offTSCPause = 0;
|
---|
1403 | pVM->tm.s.u64LastPausedTSC = 0;
|
---|
1404 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
1405 | {
|
---|
1406 | PVMCPU pVCpu = pVM->apCpusR3[i];
|
---|
1407 |
|
---|
1408 | pVCpu->tm.s.fTSCTicking = false;
|
---|
1409 | SSMR3GetU64(pSSM, &pVCpu->tm.s.u64TSC);
|
---|
1410 | if (pVM->tm.s.u64LastPausedTSC < pVCpu->tm.s.u64TSC)
|
---|
1411 | pVM->tm.s.u64LastPausedTSC = pVCpu->tm.s.u64TSC;
|
---|
1412 |
|
---|
1413 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
1414 | pVCpu->tm.s.offTSCRawSrc = 0; /** @todo TSC restore stuff and HWACC. */
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | rc = SSMR3GetU64(pSSM, &u64Hz);
|
---|
1418 | if (RT_FAILURE(rc))
|
---|
1419 | return rc;
|
---|
1420 | if (pVM->tm.s.enmTSCMode != TMTSCMODE_REAL_TSC_OFFSET)
|
---|
1421 | pVM->tm.s.cTSCTicksPerSecond = u64Hz;
|
---|
1422 |
|
---|
1423 | LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%'RU64) enmTSCMode=%d (%s) (state load)\n",
|
---|
1424 | pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.enmTSCMode, tmR3GetTSCModeName(pVM)));
|
---|
1425 |
|
---|
1426 | /* Disabled as this isn't tested, also should this apply only if GIM is enabled etc. */
|
---|
1427 | #if 0
|
---|
1428 | /*
|
---|
1429 | * If the current host TSC frequency is incompatible with what is in the
|
---|
1430 | * saved state of the VM, fall back to emulating TSC and disallow TSC mode
|
---|
1431 | * switches during VM runtime (e.g. by GIM).
|
---|
1432 | */
|
---|
1433 | if ( GIMIsEnabled(pVM)
|
---|
1434 | || pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
1435 | {
|
---|
1436 | uint64_t uGipCpuHz;
|
---|
1437 | bool fRelax = RTSystemIsInsideVM();
|
---|
1438 | bool fCompat = SUPIsTscFreqCompatible(pVM->tm.s.cTSCTicksPerSecond, &uGipCpuHz, fRelax);
|
---|
1439 | if (!fCompat)
|
---|
1440 | {
|
---|
1441 | pVM->tm.s.enmTSCMode = TMTSCMODE_VIRT_TSC_EMULATED;
|
---|
1442 | pVM->tm.s.fTSCModeSwitchAllowed = false;
|
---|
1443 | if (g_pSUPGlobalInfoPage->u32Mode != SUPGIPMODE_ASYNC_TSC)
|
---|
1444 | {
|
---|
1445 | LogRel(("TM: TSC frequency incompatible! uGipCpuHz=%#RX64 (%'RU64) enmTSCMode=%d (%s) fTSCModeSwitchAllowed=%RTbool (state load)\n",
|
---|
1446 | uGipCpuHz, uGipCpuHz, pVM->tm.s.enmTSCMode, tmR3GetTSCModeName(pVM), pVM->tm.s.fTSCModeSwitchAllowed));
|
---|
1447 | }
|
---|
1448 | else
|
---|
1449 | {
|
---|
1450 | LogRel(("TM: GIP is async, enmTSCMode=%d (%s) fTSCModeSwitchAllowed=%RTbool (state load)\n",
|
---|
1451 | uGipCpuHz, uGipCpuHz, pVM->tm.s.enmTSCMode, tmR3GetTSCModeName(pVM), pVM->tm.s.fTSCModeSwitchAllowed));
|
---|
1452 | }
|
---|
1453 | }
|
---|
1454 | }
|
---|
1455 | #endif
|
---|
1456 |
|
---|
1457 | /*
|
---|
1458 | * Make sure timers get rescheduled immediately.
|
---|
1459 | */
|
---|
1460 | PVMCPU pVCpuDst = pVM->apCpusR3[pVM->tm.s.idTimerCpu];
|
---|
1461 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
1462 |
|
---|
1463 | return VINF_SUCCESS;
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | #ifdef VBOX_WITH_STATISTICS
|
---|
1467 |
|
---|
1468 | /**
|
---|
1469 | * Register statistics for a timer.
|
---|
1470 | *
|
---|
1471 | * @param pVM The cross context VM structure.
|
---|
1472 | * @param pQueue The queue the timer belongs to.
|
---|
1473 | * @param pTimer The timer to register statistics for.
|
---|
1474 | */
|
---|
1475 | static void tmR3TimerRegisterStats(PVM pVM, PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
|
---|
1476 | {
|
---|
1477 | STAMR3RegisterF(pVM, &pTimer->StatTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL,
|
---|
1478 | pQueue->szName, "/TM/Timers/%s", pTimer->szName);
|
---|
1479 | STAMR3RegisterF(pVM, &pTimer->StatCritSectEnter, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL,
|
---|
1480 | "", "/TM/Timers/%s/CritSectEnter", pTimer->szName);
|
---|
1481 | STAMR3RegisterF(pVM, &pTimer->StatGet, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS,
|
---|
1482 | "", "/TM/Timers/%s/Get", pTimer->szName);
|
---|
1483 | STAMR3RegisterF(pVM, &pTimer->StatSetAbsolute, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS,
|
---|
1484 | "", "/TM/Timers/%s/SetAbsolute", pTimer->szName);
|
---|
1485 | STAMR3RegisterF(pVM, &pTimer->StatSetRelative, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS,
|
---|
1486 | "", "/TM/Timers/%s/SetRelative", pTimer->szName);
|
---|
1487 | STAMR3RegisterF(pVM, &pTimer->StatStop, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS,
|
---|
1488 | "", "/TM/Timers/%s/Stop", pTimer->szName);
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 |
|
---|
1492 | /**
|
---|
1493 | * Deregister the statistics for a timer.
|
---|
1494 | */
|
---|
1495 | static void tmR3TimerDeregisterStats(PVM pVM, PTMTIMER pTimer)
|
---|
1496 | {
|
---|
1497 | char szPrefix[128];
|
---|
1498 | size_t cchPrefix = RTStrPrintf(szPrefix, sizeof(szPrefix), "/TM/Timers/%s/", pTimer->szName);
|
---|
1499 | STAMR3DeregisterByPrefix(pVM->pUVM, szPrefix);
|
---|
1500 | szPrefix[cchPrefix - 1] = '\0';
|
---|
1501 | STAMR3Deregister(pVM->pUVM, szPrefix);
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 |
|
---|
1505 | /**
|
---|
1506 | * Register statistics for all allocated timers in a queue.
|
---|
1507 | *
|
---|
1508 | * @param pVM The cross context VM structure.
|
---|
1509 | * @param pQueue The queue to register statistics for.
|
---|
1510 | * @param cTimers Number of timers to consider (in growth scenario).
|
---|
1511 | */
|
---|
1512 | static void tmR3TimerQueueRegisterStats(PVM pVM, PTMTIMERQUEUE pQueue, uint32_t cTimers)
|
---|
1513 | {
|
---|
1514 | uint32_t idxTimer = RT_MIN(cTimers, pQueue->cTimersAlloc);
|
---|
1515 | while (idxTimer-- > 0)
|
---|
1516 | {
|
---|
1517 | PTMTIMER pTimer = &pQueue->paTimers[idxTimer];
|
---|
1518 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1519 | if (enmState > TMTIMERSTATE_INVALID && enmState < TMTIMERSTATE_DESTROY)
|
---|
1520 | tmR3TimerRegisterStats(pVM, pQueue, pTimer);
|
---|
1521 | }
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | #endif /* VBOX_WITH_STATISTICS */
|
---|
1525 |
|
---|
1526 |
|
---|
1527 | /**
|
---|
1528 | * Grows a timer queue.
|
---|
1529 | *
|
---|
1530 | * @returns VBox status code (errors are LogRel'ed already).
|
---|
1531 | * @param pVM The cross context VM structure.
|
---|
1532 | * @param pQueue The timer queue to grow.
|
---|
1533 | * @param cNewTimers The minimum number of timers after growing.
|
---|
1534 | * @note Caller owns the queue's allocation lock.
|
---|
1535 | */
|
---|
1536 | static int tmR3TimerQueueGrow(PVM pVM, PTMTIMERQUEUE pQueue, uint32_t cNewTimers)
|
---|
1537 | {
|
---|
1538 | /*
|
---|
1539 | * Validate input and state.
|
---|
1540 | */
|
---|
1541 | VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
1542 | VM_ASSERT_STATE_RETURN(pVM, VMSTATE_CREATING, VERR_VM_INVALID_VM_STATE); /** @todo must do better than this! */
|
---|
1543 | AssertReturn(!pQueue->fCannotGrow, VERR_TM_TIMER_QUEUE_CANNOT_GROW);
|
---|
1544 |
|
---|
1545 | uint32_t const cOldEntries = pQueue->cTimersAlloc;
|
---|
1546 | AssertReturn(cNewTimers > cOldEntries, VERR_TM_IPE_1);
|
---|
1547 | AssertReturn(cNewTimers < _32K, VERR_TM_IPE_1);
|
---|
1548 |
|
---|
1549 | /*
|
---|
1550 | * Do the growing.
|
---|
1551 | */
|
---|
1552 | int rc;
|
---|
1553 | if (!SUPR3IsDriverless())
|
---|
1554 | {
|
---|
1555 | rc = VMMR3CallR0Emt(pVM, VMMGetCpu(pVM), VMMR0_DO_TM_GROW_TIMER_QUEUE,
|
---|
1556 | RT_MAKE_U64(cNewTimers, (uint64_t)(pQueue - &pVM->tm.s.aTimerQueues[0])), NULL);
|
---|
1557 | AssertLogRelRCReturn(rc, rc);
|
---|
1558 | AssertReturn(pQueue->cTimersAlloc >= cNewTimers, VERR_TM_IPE_3);
|
---|
1559 | }
|
---|
1560 | else
|
---|
1561 | {
|
---|
1562 | AssertReturn(cNewTimers <= _32K && cOldEntries <= _32K, VERR_TM_TOO_MANY_TIMERS);
|
---|
1563 | ASMCompilerBarrier();
|
---|
1564 |
|
---|
1565 | /*
|
---|
1566 | * Round up the request to the nearest page and do the allocation.
|
---|
1567 | */
|
---|
1568 | size_t cbNew = sizeof(TMTIMER) * cNewTimers;
|
---|
1569 | cbNew = RT_ALIGN_Z(cbNew, HOST_PAGE_SIZE);
|
---|
1570 | cNewTimers = (uint32_t)(cbNew / sizeof(TMTIMER));
|
---|
1571 |
|
---|
1572 | PTMTIMER paTimers = (PTMTIMER)RTMemPageAllocZ(cbNew);
|
---|
1573 | if (paTimers)
|
---|
1574 | {
|
---|
1575 | /*
|
---|
1576 | * Copy over the old timer, init the new free ones, then switch over
|
---|
1577 | * and free the old ones.
|
---|
1578 | */
|
---|
1579 | PTMTIMER const paOldTimers = pQueue->paTimers;
|
---|
1580 | tmHCTimerQueueGrowInit(paTimers, paOldTimers, cNewTimers, cOldEntries);
|
---|
1581 |
|
---|
1582 | pQueue->paTimers = paTimers;
|
---|
1583 | pQueue->cTimersAlloc = cNewTimers;
|
---|
1584 | pQueue->cTimersFree += cNewTimers - (cOldEntries ? cOldEntries : 1);
|
---|
1585 |
|
---|
1586 | RTMemPageFree(paOldTimers, RT_ALIGN_Z(sizeof(TMTIMER) * cOldEntries, HOST_PAGE_SIZE));
|
---|
1587 | rc = VINF_SUCCESS;
|
---|
1588 | }
|
---|
1589 | else
|
---|
1590 | rc = VERR_NO_PAGE_MEMORY;
|
---|
1591 | }
|
---|
1592 | return rc;
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 |
|
---|
1596 | /**
|
---|
1597 | * Internal TMR3TimerCreate worker.
|
---|
1598 | *
|
---|
1599 | * @returns VBox status code.
|
---|
1600 | * @param pVM The cross context VM structure.
|
---|
1601 | * @param enmClock The timer clock.
|
---|
1602 | * @param fFlags TMTIMER_FLAGS_XXX.
|
---|
1603 | * @param pszName The timer name.
|
---|
1604 | * @param ppTimer Where to store the timer pointer on success.
|
---|
1605 | */
|
---|
1606 | static int tmr3TimerCreate(PVM pVM, TMCLOCK enmClock, uint32_t fFlags, const char *pszName, PPTMTIMERR3 ppTimer)
|
---|
1607 | {
|
---|
1608 | PTMTIMER pTimer;
|
---|
1609 |
|
---|
1610 | /*
|
---|
1611 | * Validate input.
|
---|
1612 | */
|
---|
1613 | VM_ASSERT_EMT(pVM);
|
---|
1614 |
|
---|
1615 | AssertReturn((fFlags & (TMTIMER_FLAGS_RING0 | TMTIMER_FLAGS_NO_RING0)) != (TMTIMER_FLAGS_RING0 | TMTIMER_FLAGS_NO_RING0),
|
---|
1616 | VERR_INVALID_FLAGS);
|
---|
1617 |
|
---|
1618 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
1619 | size_t const cchName = strlen(pszName);
|
---|
1620 | AssertMsgReturn(cchName < sizeof(pTimer->szName), ("timer name too long: %s\n", pszName), VERR_INVALID_NAME);
|
---|
1621 | AssertMsgReturn(cchName > 2, ("Too short timer name: %s\n", pszName), VERR_INVALID_NAME);
|
---|
1622 |
|
---|
1623 | AssertMsgReturn(enmClock >= TMCLOCK_REAL && enmClock < TMCLOCK_MAX,
|
---|
1624 | ("%d\n", enmClock), VERR_INVALID_PARAMETER);
|
---|
1625 | AssertReturn(enmClock != TMCLOCK_TSC, VERR_NOT_SUPPORTED);
|
---|
1626 | if (enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
1627 | VM_ASSERT_STATE_RETURN(pVM, VMSTATE_CREATING, VERR_WRONG_ORDER);
|
---|
1628 |
|
---|
1629 | /*
|
---|
1630 | * Exclusively lock the queue.
|
---|
1631 | *
|
---|
1632 | * Note! This means that it is not possible to allocate timers from a timer callback.
|
---|
1633 | */
|
---|
1634 | PTMTIMERQUEUE pQueue = &pVM->tm.s.aTimerQueues[enmClock];
|
---|
1635 | int rc = PDMCritSectRwEnterExcl(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
1636 | AssertRCReturn(rc, rc);
|
---|
1637 |
|
---|
1638 | /*
|
---|
1639 | * Allocate the timer.
|
---|
1640 | */
|
---|
1641 | if (!pQueue->cTimersFree)
|
---|
1642 | {
|
---|
1643 | rc = tmR3TimerQueueGrow(pVM, pQueue, pQueue->cTimersAlloc + 64);
|
---|
1644 | AssertRCReturnStmt(rc, PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock), rc);
|
---|
1645 | }
|
---|
1646 |
|
---|
1647 | /* Scan the array for free timers. */
|
---|
1648 | pTimer = NULL;
|
---|
1649 | PTMTIMER const paTimers = pQueue->paTimers;
|
---|
1650 | uint32_t const cTimersAlloc = pQueue->cTimersAlloc;
|
---|
1651 | uint32_t idxTimer = pQueue->idxFreeHint;
|
---|
1652 | for (uint32_t iScan = 0; iScan < 2; iScan++)
|
---|
1653 | {
|
---|
1654 | while (idxTimer < cTimersAlloc)
|
---|
1655 | {
|
---|
1656 | if (paTimers[idxTimer].enmState == TMTIMERSTATE_FREE)
|
---|
1657 | {
|
---|
1658 | pTimer = &paTimers[idxTimer];
|
---|
1659 | pQueue->idxFreeHint = idxTimer + 1;
|
---|
1660 | break;
|
---|
1661 | }
|
---|
1662 | idxTimer++;
|
---|
1663 | }
|
---|
1664 | if (pTimer != NULL)
|
---|
1665 | break;
|
---|
1666 | idxTimer = 1;
|
---|
1667 | }
|
---|
1668 | AssertLogRelMsgReturnStmt(pTimer != NULL, ("cTimersFree=%u cTimersAlloc=%u enmClock=%s\n", pQueue->cTimersFree,
|
---|
1669 | pQueue->cTimersAlloc, pQueue->szName),
|
---|
1670 | PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock), VERR_INTERNAL_ERROR_3);
|
---|
1671 | pQueue->cTimersFree -= 1;
|
---|
1672 |
|
---|
1673 | /*
|
---|
1674 | * Initialize it.
|
---|
1675 | */
|
---|
1676 | Assert(idxTimer != 0);
|
---|
1677 | Assert(idxTimer <= TMTIMERHANDLE_TIMER_IDX_MASK);
|
---|
1678 | pTimer->hSelf = idxTimer
|
---|
1679 | | ((uintptr_t)(pQueue - &pVM->tm.s.aTimerQueues[0]) << TMTIMERHANDLE_QUEUE_IDX_SHIFT);
|
---|
1680 | Assert(!(pTimer->hSelf & TMTIMERHANDLE_RANDOM_MASK));
|
---|
1681 | pTimer->hSelf |= (RTRandU64() & TMTIMERHANDLE_RANDOM_MASK);
|
---|
1682 |
|
---|
1683 | pTimer->u64Expire = 0;
|
---|
1684 | pTimer->enmState = TMTIMERSTATE_STOPPED;
|
---|
1685 | pTimer->idxScheduleNext = UINT32_MAX;
|
---|
1686 | pTimer->idxNext = UINT32_MAX;
|
---|
1687 | pTimer->idxPrev = UINT32_MAX;
|
---|
1688 | pTimer->fFlags = fFlags;
|
---|
1689 | pTimer->uHzHint = 0;
|
---|
1690 | pTimer->pvUser = NULL;
|
---|
1691 | pTimer->pCritSect = NULL;
|
---|
1692 | memcpy(pTimer->szName, pszName, cchName);
|
---|
1693 | pTimer->szName[cchName] = '\0';
|
---|
1694 |
|
---|
1695 | #ifdef VBOX_STRICT
|
---|
1696 | tmTimerQueuesSanityChecks(pVM, "tmR3TimerCreate");
|
---|
1697 | #endif
|
---|
1698 |
|
---|
1699 | PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock);
|
---|
1700 |
|
---|
1701 | #ifdef VBOX_WITH_STATISTICS
|
---|
1702 | /*
|
---|
1703 | * Only register statistics if we're passed the no-realloc point.
|
---|
1704 | */
|
---|
1705 | if (pQueue->fCannotGrow)
|
---|
1706 | tmR3TimerRegisterStats(pVM, pQueue, pTimer);
|
---|
1707 | #endif
|
---|
1708 |
|
---|
1709 | *ppTimer = pTimer;
|
---|
1710 | return VINF_SUCCESS;
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 |
|
---|
1714 | /**
|
---|
1715 | * Creates a device timer.
|
---|
1716 | *
|
---|
1717 | * @returns VBox status code.
|
---|
1718 | * @param pVM The cross context VM structure.
|
---|
1719 | * @param pDevIns Device instance.
|
---|
1720 | * @param enmClock The clock to use on this timer.
|
---|
1721 | * @param pfnCallback Callback function.
|
---|
1722 | * @param pvUser The user argument to the callback.
|
---|
1723 | * @param fFlags Timer creation flags, see grp_tm_timer_flags.
|
---|
1724 | * @param pszName Timer name (will be copied). Max 31 chars.
|
---|
1725 | * @param phTimer Where to store the timer handle on success.
|
---|
1726 | */
|
---|
1727 | VMM_INT_DECL(int) TMR3TimerCreateDevice(PVM pVM, PPDMDEVINS pDevIns, TMCLOCK enmClock,
|
---|
1728 | PFNTMTIMERDEV pfnCallback, void *pvUser,
|
---|
1729 | uint32_t fFlags, const char *pszName, PTMTIMERHANDLE phTimer)
|
---|
1730 | {
|
---|
1731 | AssertReturn(!(fFlags & ~(TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_RING0 | TMTIMER_FLAGS_NO_RING0)),
|
---|
1732 | VERR_INVALID_FLAGS);
|
---|
1733 |
|
---|
1734 | /*
|
---|
1735 | * Allocate and init stuff.
|
---|
1736 | */
|
---|
1737 | PTMTIMER pTimer;
|
---|
1738 | int rc = tmr3TimerCreate(pVM, enmClock, fFlags, pszName, &pTimer);
|
---|
1739 | if (RT_SUCCESS(rc))
|
---|
1740 | {
|
---|
1741 | pTimer->enmType = TMTIMERTYPE_DEV;
|
---|
1742 | pTimer->u.Dev.pfnTimer = pfnCallback;
|
---|
1743 | pTimer->u.Dev.pDevIns = pDevIns;
|
---|
1744 | pTimer->pvUser = pvUser;
|
---|
1745 | if (!(fFlags & TMTIMER_FLAGS_NO_CRIT_SECT))
|
---|
1746 | pTimer->pCritSect = PDMR3DevGetCritSect(pVM, pDevIns);
|
---|
1747 | *phTimer = pTimer->hSelf;
|
---|
1748 | Log(("TM: Created device timer %p clock %d callback %p '%s'\n", phTimer, enmClock, pfnCallback, pszName));
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | return rc;
|
---|
1752 | }
|
---|
1753 |
|
---|
1754 |
|
---|
1755 |
|
---|
1756 |
|
---|
1757 | /**
|
---|
1758 | * Creates a USB device timer.
|
---|
1759 | *
|
---|
1760 | * @returns VBox status code.
|
---|
1761 | * @param pVM The cross context VM structure.
|
---|
1762 | * @param pUsbIns The USB device instance.
|
---|
1763 | * @param enmClock The clock to use on this timer.
|
---|
1764 | * @param pfnCallback Callback function.
|
---|
1765 | * @param pvUser The user argument to the callback.
|
---|
1766 | * @param fFlags Timer creation flags, see grp_tm_timer_flags.
|
---|
1767 | * @param pszName Timer name (will be copied). Max 31 chars.
|
---|
1768 | * @param phTimer Where to store the timer handle on success.
|
---|
1769 | */
|
---|
1770 | VMM_INT_DECL(int) TMR3TimerCreateUsb(PVM pVM, PPDMUSBINS pUsbIns, TMCLOCK enmClock,
|
---|
1771 | PFNTMTIMERUSB pfnCallback, void *pvUser,
|
---|
1772 | uint32_t fFlags, const char *pszName, PTMTIMERHANDLE phTimer)
|
---|
1773 | {
|
---|
1774 | AssertReturn(!(fFlags & ~(TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_NO_RING0)), VERR_INVALID_PARAMETER);
|
---|
1775 |
|
---|
1776 | /*
|
---|
1777 | * Allocate and init stuff.
|
---|
1778 | */
|
---|
1779 | PTMTIMER pTimer;
|
---|
1780 | int rc = tmr3TimerCreate(pVM, enmClock, fFlags, pszName, &pTimer);
|
---|
1781 | if (RT_SUCCESS(rc))
|
---|
1782 | {
|
---|
1783 | pTimer->enmType = TMTIMERTYPE_USB;
|
---|
1784 | pTimer->u.Usb.pfnTimer = pfnCallback;
|
---|
1785 | pTimer->u.Usb.pUsbIns = pUsbIns;
|
---|
1786 | pTimer->pvUser = pvUser;
|
---|
1787 | //if (!(fFlags & TMTIMER_FLAGS_NO_CRIT_SECT))
|
---|
1788 | //{
|
---|
1789 | // if (pDevIns->pCritSectR3)
|
---|
1790 | // pTimer->pCritSect = pUsbIns->pCritSectR3;
|
---|
1791 | // else
|
---|
1792 | // pTimer->pCritSect = IOMR3GetCritSect(pVM);
|
---|
1793 | //}
|
---|
1794 | *phTimer = pTimer->hSelf;
|
---|
1795 | Log(("TM: Created USB device timer %p clock %d callback %p '%s'\n", *phTimer, enmClock, pfnCallback, pszName));
|
---|
1796 | }
|
---|
1797 |
|
---|
1798 | return rc;
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 |
|
---|
1802 | /**
|
---|
1803 | * Creates a driver timer.
|
---|
1804 | *
|
---|
1805 | * @returns VBox status code.
|
---|
1806 | * @param pVM The cross context VM structure.
|
---|
1807 | * @param pDrvIns Driver instance.
|
---|
1808 | * @param enmClock The clock to use on this timer.
|
---|
1809 | * @param pfnCallback Callback function.
|
---|
1810 | * @param pvUser The user argument to the callback.
|
---|
1811 | * @param fFlags Timer creation flags, see grp_tm_timer_flags.
|
---|
1812 | * @param pszName Timer name (will be copied). Max 31 chars.
|
---|
1813 | * @param phTimer Where to store the timer handle on success.
|
---|
1814 | */
|
---|
1815 | VMM_INT_DECL(int) TMR3TimerCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, void *pvUser,
|
---|
1816 | uint32_t fFlags, const char *pszName, PTMTIMERHANDLE phTimer)
|
---|
1817 | {
|
---|
1818 | AssertReturn(!(fFlags & ~(TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_RING0 | TMTIMER_FLAGS_NO_RING0)),
|
---|
1819 | VERR_INVALID_FLAGS);
|
---|
1820 |
|
---|
1821 | /*
|
---|
1822 | * Allocate and init stuff.
|
---|
1823 | */
|
---|
1824 | PTMTIMER pTimer;
|
---|
1825 | int rc = tmr3TimerCreate(pVM, enmClock, fFlags, pszName, &pTimer);
|
---|
1826 | if (RT_SUCCESS(rc))
|
---|
1827 | {
|
---|
1828 | pTimer->enmType = TMTIMERTYPE_DRV;
|
---|
1829 | pTimer->u.Drv.pfnTimer = pfnCallback;
|
---|
1830 | pTimer->u.Drv.pDrvIns = pDrvIns;
|
---|
1831 | pTimer->pvUser = pvUser;
|
---|
1832 | *phTimer = pTimer->hSelf;
|
---|
1833 | Log(("TM: Created device timer %p clock %d callback %p '%s'\n", *phTimer, enmClock, pfnCallback, pszName));
|
---|
1834 | }
|
---|
1835 |
|
---|
1836 | return rc;
|
---|
1837 | }
|
---|
1838 |
|
---|
1839 |
|
---|
1840 | /**
|
---|
1841 | * Creates an internal timer.
|
---|
1842 | *
|
---|
1843 | * @returns VBox status code.
|
---|
1844 | * @param pVM The cross context VM structure.
|
---|
1845 | * @param enmClock The clock to use on this timer.
|
---|
1846 | * @param pfnCallback Callback function.
|
---|
1847 | * @param pvUser User argument to be passed to the callback.
|
---|
1848 | * @param fFlags Timer creation flags, see grp_tm_timer_flags.
|
---|
1849 | * @param pszName Timer name (will be copied). Max 31 chars.
|
---|
1850 | * @param phTimer Where to store the timer handle on success.
|
---|
1851 | */
|
---|
1852 | VMMR3DECL(int) TMR3TimerCreate(PVM pVM, TMCLOCK enmClock, PFNTMTIMERINT pfnCallback, void *pvUser,
|
---|
1853 | uint32_t fFlags, const char *pszName, PTMTIMERHANDLE phTimer)
|
---|
1854 | {
|
---|
1855 | AssertReturn(fFlags & (TMTIMER_FLAGS_RING0 | TMTIMER_FLAGS_NO_RING0), VERR_INVALID_FLAGS);
|
---|
1856 | AssertReturn((fFlags & (TMTIMER_FLAGS_RING0 | TMTIMER_FLAGS_NO_RING0)) != (TMTIMER_FLAGS_RING0 | TMTIMER_FLAGS_NO_RING0),
|
---|
1857 | VERR_INVALID_FLAGS);
|
---|
1858 |
|
---|
1859 | /*
|
---|
1860 | * Allocate and init stuff.
|
---|
1861 | */
|
---|
1862 | PTMTIMER pTimer;
|
---|
1863 | int rc = tmr3TimerCreate(pVM, enmClock, fFlags, pszName, &pTimer);
|
---|
1864 | if (RT_SUCCESS(rc))
|
---|
1865 | {
|
---|
1866 | pTimer->enmType = TMTIMERTYPE_INTERNAL;
|
---|
1867 | pTimer->u.Internal.pfnTimer = pfnCallback;
|
---|
1868 | pTimer->pvUser = pvUser;
|
---|
1869 | *phTimer = pTimer->hSelf;
|
---|
1870 | Log(("TM: Created internal timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszName));
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | return rc;
|
---|
1874 | }
|
---|
1875 |
|
---|
1876 |
|
---|
1877 | /**
|
---|
1878 | * Destroy a timer
|
---|
1879 | *
|
---|
1880 | * @returns VBox status code.
|
---|
1881 | * @param pVM The cross context VM structure.
|
---|
1882 | * @param pQueue The queue the timer is on.
|
---|
1883 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1884 | */
|
---|
1885 | static int tmR3TimerDestroy(PVMCC pVM, PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
|
---|
1886 | {
|
---|
1887 | bool fActive = false;
|
---|
1888 | bool fPending = false;
|
---|
1889 |
|
---|
1890 | AssertMsg( !pTimer->pCritSect
|
---|
1891 | || VMR3GetState(pVM) != VMSTATE_RUNNING
|
---|
1892 | || PDMCritSectIsOwner(pVM, pTimer->pCritSect), ("%s\n", pTimer->szName));
|
---|
1893 |
|
---|
1894 | /*
|
---|
1895 | * The rest of the game happens behind the lock, just
|
---|
1896 | * like create does. All the work is done here.
|
---|
1897 | */
|
---|
1898 | PDMCritSectRwEnterExcl(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
1899 | PDMCritSectEnter(pVM, &pQueue->TimerLock, VERR_IGNORED);
|
---|
1900 |
|
---|
1901 | for (int cRetries = 1000;; cRetries--)
|
---|
1902 | {
|
---|
1903 | /*
|
---|
1904 | * Change to the DESTROY state.
|
---|
1905 | */
|
---|
1906 | TMTIMERSTATE const enmState = pTimer->enmState;
|
---|
1907 | Log2(("TMTimerDestroy: %p:{.enmState=%s, .szName='%s'} cRetries=%d\n",
|
---|
1908 | pTimer, tmTimerState(enmState), pTimer->szName, cRetries));
|
---|
1909 | switch (enmState)
|
---|
1910 | {
|
---|
1911 | case TMTIMERSTATE_STOPPED:
|
---|
1912 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
1913 | break;
|
---|
1914 |
|
---|
1915 | case TMTIMERSTATE_ACTIVE:
|
---|
1916 | fActive = true;
|
---|
1917 | break;
|
---|
1918 |
|
---|
1919 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1920 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1921 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1922 | fActive = true;
|
---|
1923 | fPending = true;
|
---|
1924 | break;
|
---|
1925 |
|
---|
1926 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1927 | fPending = true;
|
---|
1928 | break;
|
---|
1929 |
|
---|
1930 | /*
|
---|
1931 | * This shouldn't happen as the caller should make sure there are no races.
|
---|
1932 | */
|
---|
1933 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
1934 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1935 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1936 | AssertMsgFailed(("%p:.enmState=%s %s\n", pTimer, tmTimerState(enmState), pTimer->szName));
|
---|
1937 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
1938 | PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock);
|
---|
1939 |
|
---|
1940 | AssertMsgReturn(cRetries > 0, ("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->szName),
|
---|
1941 | VERR_TM_UNSTABLE_STATE);
|
---|
1942 | if (!RTThreadYield())
|
---|
1943 | RTThreadSleep(1);
|
---|
1944 |
|
---|
1945 | PDMCritSectRwEnterExcl(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
1946 | PDMCritSectEnter(pVM, &pQueue->TimerLock, VERR_IGNORED);
|
---|
1947 | continue;
|
---|
1948 |
|
---|
1949 | /*
|
---|
1950 | * Invalid states.
|
---|
1951 | */
|
---|
1952 | case TMTIMERSTATE_FREE:
|
---|
1953 | case TMTIMERSTATE_DESTROY:
|
---|
1954 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
1955 | PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock);
|
---|
1956 | AssertLogRelMsgFailedReturn(("pTimer=%p %s\n", pTimer, tmTimerState(enmState)), VERR_TM_INVALID_STATE);
|
---|
1957 |
|
---|
1958 | default:
|
---|
1959 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, pTimer->szName));
|
---|
1960 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
1961 | PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock);
|
---|
1962 | return VERR_TM_UNKNOWN_STATE;
|
---|
1963 | }
|
---|
1964 |
|
---|
1965 | /*
|
---|
1966 | * Try switch to the destroy state.
|
---|
1967 | * This should always succeed as the caller should make sure there are no race.
|
---|
1968 | */
|
---|
1969 | bool fRc;
|
---|
1970 | TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_DESTROY, enmState, fRc);
|
---|
1971 | if (fRc)
|
---|
1972 | break;
|
---|
1973 | AssertMsgFailed(("%p:.enmState=%s %s\n", pTimer, tmTimerState(enmState), pTimer->szName));
|
---|
1974 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
1975 | PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock);
|
---|
1976 |
|
---|
1977 | AssertMsgReturn(cRetries > 0, ("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->szName),
|
---|
1978 | VERR_TM_UNSTABLE_STATE);
|
---|
1979 |
|
---|
1980 | PDMCritSectRwEnterExcl(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
1981 | PDMCritSectEnter(pVM, &pQueue->TimerLock, VERR_IGNORED);
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 | /*
|
---|
1985 | * Unlink from the active list.
|
---|
1986 | */
|
---|
1987 | if (fActive)
|
---|
1988 | {
|
---|
1989 | const PTMTIMER pPrev = tmTimerGetPrev(pQueue, pTimer);
|
---|
1990 | const PTMTIMER pNext = tmTimerGetNext(pQueue, pTimer);
|
---|
1991 | if (pPrev)
|
---|
1992 | tmTimerSetNext(pQueue, pPrev, pNext);
|
---|
1993 | else
|
---|
1994 | {
|
---|
1995 | tmTimerQueueSetHead(pQueue, pQueue, pNext);
|
---|
1996 | pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
|
---|
1997 | }
|
---|
1998 | if (pNext)
|
---|
1999 | tmTimerSetPrev(pQueue, pNext, pPrev);
|
---|
2000 | pTimer->idxNext = UINT32_MAX;
|
---|
2001 | pTimer->idxPrev = UINT32_MAX;
|
---|
2002 | }
|
---|
2003 |
|
---|
2004 | /*
|
---|
2005 | * Unlink from the schedule list by running it.
|
---|
2006 | */
|
---|
2007 | if (fPending)
|
---|
2008 | {
|
---|
2009 | Log3(("TMR3TimerDestroy: tmTimerQueueSchedule\n"));
|
---|
2010 | STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
|
---|
2011 | Assert(pQueue->idxSchedule < pQueue->cTimersAlloc);
|
---|
2012 | tmTimerQueueSchedule(pVM, pQueue, pQueue);
|
---|
2013 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
|
---|
2014 | }
|
---|
2015 |
|
---|
2016 | #ifdef VBOX_WITH_STATISTICS
|
---|
2017 | /*
|
---|
2018 | * Deregister statistics.
|
---|
2019 | */
|
---|
2020 | tmR3TimerDeregisterStats(pVM, pTimer);
|
---|
2021 | #endif
|
---|
2022 |
|
---|
2023 | /*
|
---|
2024 | * Change it to free state and update the queue accordingly.
|
---|
2025 | */
|
---|
2026 | Assert(pTimer->idxNext == UINT32_MAX); Assert(pTimer->idxPrev == UINT32_MAX); Assert(pTimer->idxScheduleNext == UINT32_MAX);
|
---|
2027 |
|
---|
2028 | TM_SET_STATE(pTimer, TMTIMERSTATE_FREE);
|
---|
2029 |
|
---|
2030 | pQueue->cTimersFree += 1;
|
---|
2031 | uint32_t idxTimer = (uint32_t)(pTimer - pQueue->paTimers);
|
---|
2032 | if (idxTimer < pQueue->idxFreeHint)
|
---|
2033 | pQueue->idxFreeHint = idxTimer;
|
---|
2034 |
|
---|
2035 | #ifdef VBOX_STRICT
|
---|
2036 | tmTimerQueuesSanityChecks(pVM, "TMR3TimerDestroy");
|
---|
2037 | #endif
|
---|
2038 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
2039 | PDMCritSectRwLeaveExcl(pVM, &pQueue->AllocLock);
|
---|
2040 | return VINF_SUCCESS;
|
---|
2041 | }
|
---|
2042 |
|
---|
2043 |
|
---|
2044 | /**
|
---|
2045 | * Destroy a timer
|
---|
2046 | *
|
---|
2047 | * @returns VBox status code.
|
---|
2048 | * @param pVM The cross context VM structure.
|
---|
2049 | * @param hTimer Timer handle as returned by one of the create functions.
|
---|
2050 | */
|
---|
2051 | VMMR3DECL(int) TMR3TimerDestroy(PVM pVM, TMTIMERHANDLE hTimer)
|
---|
2052 | {
|
---|
2053 | /* We ignore NILs here. */
|
---|
2054 | if (hTimer == NIL_TMTIMERHANDLE)
|
---|
2055 | return VINF_SUCCESS;
|
---|
2056 | TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
|
---|
2057 | return tmR3TimerDestroy(pVM, pQueue, pTimer);
|
---|
2058 | }
|
---|
2059 |
|
---|
2060 |
|
---|
2061 | /**
|
---|
2062 | * Destroy all timers owned by a device.
|
---|
2063 | *
|
---|
2064 | * @returns VBox status code.
|
---|
2065 | * @param pVM The cross context VM structure.
|
---|
2066 | * @param pDevIns Device which timers should be destroyed.
|
---|
2067 | */
|
---|
2068 | VMM_INT_DECL(int) TMR3TimerDestroyDevice(PVM pVM, PPDMDEVINS pDevIns)
|
---|
2069 | {
|
---|
2070 | LogFlow(("TMR3TimerDestroyDevice: pDevIns=%p\n", pDevIns));
|
---|
2071 | if (!pDevIns)
|
---|
2072 | return VERR_INVALID_PARAMETER;
|
---|
2073 |
|
---|
2074 | for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
|
---|
2075 | {
|
---|
2076 | PTMTIMERQUEUE pQueue = &pVM->tm.s.aTimerQueues[idxQueue];
|
---|
2077 | PDMCritSectRwEnterShared(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
2078 | uint32_t idxTimer = pQueue->cTimersAlloc;
|
---|
2079 | while (idxTimer-- > 0)
|
---|
2080 | {
|
---|
2081 | PTMTIMER pTimer = &pQueue->paTimers[idxTimer];
|
---|
2082 | if ( pTimer->enmType == TMTIMERTYPE_DEV
|
---|
2083 | && pTimer->u.Dev.pDevIns == pDevIns
|
---|
2084 | && pTimer->enmState < TMTIMERSTATE_DESTROY)
|
---|
2085 | {
|
---|
2086 | PDMCritSectRwLeaveShared(pVM, &pQueue->AllocLock);
|
---|
2087 |
|
---|
2088 | int rc = tmR3TimerDestroy(pVM, pQueue, pTimer);
|
---|
2089 | AssertRC(rc);
|
---|
2090 |
|
---|
2091 | PDMCritSectRwEnterShared(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
2092 | }
|
---|
2093 | }
|
---|
2094 | PDMCritSectRwLeaveShared(pVM, &pQueue->AllocLock);
|
---|
2095 | }
|
---|
2096 |
|
---|
2097 | LogFlow(("TMR3TimerDestroyDevice: returns VINF_SUCCESS\n"));
|
---|
2098 | return VINF_SUCCESS;
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 |
|
---|
2102 | /**
|
---|
2103 | * Destroy all timers owned by a USB device.
|
---|
2104 | *
|
---|
2105 | * @returns VBox status code.
|
---|
2106 | * @param pVM The cross context VM structure.
|
---|
2107 | * @param pUsbIns USB device which timers should be destroyed.
|
---|
2108 | */
|
---|
2109 | VMM_INT_DECL(int) TMR3TimerDestroyUsb(PVM pVM, PPDMUSBINS pUsbIns)
|
---|
2110 | {
|
---|
2111 | LogFlow(("TMR3TimerDestroyUsb: pUsbIns=%p\n", pUsbIns));
|
---|
2112 | if (!pUsbIns)
|
---|
2113 | return VERR_INVALID_PARAMETER;
|
---|
2114 |
|
---|
2115 | for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
|
---|
2116 | {
|
---|
2117 | PTMTIMERQUEUE pQueue = &pVM->tm.s.aTimerQueues[idxQueue];
|
---|
2118 | PDMCritSectRwEnterShared(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
2119 | uint32_t idxTimer = pQueue->cTimersAlloc;
|
---|
2120 | while (idxTimer-- > 0)
|
---|
2121 | {
|
---|
2122 | PTMTIMER pTimer = &pQueue->paTimers[idxTimer];
|
---|
2123 | if ( pTimer->enmType == TMTIMERTYPE_USB
|
---|
2124 | && pTimer->u.Usb.pUsbIns == pUsbIns
|
---|
2125 | && pTimer->enmState < TMTIMERSTATE_DESTROY)
|
---|
2126 | {
|
---|
2127 | PDMCritSectRwLeaveShared(pVM, &pQueue->AllocLock);
|
---|
2128 |
|
---|
2129 | int rc = tmR3TimerDestroy(pVM, pQueue, pTimer);
|
---|
2130 | AssertRC(rc);
|
---|
2131 |
|
---|
2132 | PDMCritSectRwEnterShared(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
2133 | }
|
---|
2134 | }
|
---|
2135 | PDMCritSectRwLeaveShared(pVM, &pQueue->AllocLock);
|
---|
2136 | }
|
---|
2137 |
|
---|
2138 | LogFlow(("TMR3TimerDestroyUsb: returns VINF_SUCCESS\n"));
|
---|
2139 | return VINF_SUCCESS;
|
---|
2140 | }
|
---|
2141 |
|
---|
2142 |
|
---|
2143 | /**
|
---|
2144 | * Destroy all timers owned by a driver.
|
---|
2145 | *
|
---|
2146 | * @returns VBox status code.
|
---|
2147 | * @param pVM The cross context VM structure.
|
---|
2148 | * @param pDrvIns Driver which timers should be destroyed.
|
---|
2149 | */
|
---|
2150 | VMM_INT_DECL(int) TMR3TimerDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns)
|
---|
2151 | {
|
---|
2152 | LogFlow(("TMR3TimerDestroyDriver: pDrvIns=%p\n", pDrvIns));
|
---|
2153 | if (!pDrvIns)
|
---|
2154 | return VERR_INVALID_PARAMETER;
|
---|
2155 |
|
---|
2156 | for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
|
---|
2157 | {
|
---|
2158 | PTMTIMERQUEUE pQueue = &pVM->tm.s.aTimerQueues[idxQueue];
|
---|
2159 | PDMCritSectRwEnterShared(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
2160 | uint32_t idxTimer = pQueue->cTimersAlloc;
|
---|
2161 | while (idxTimer-- > 0)
|
---|
2162 | {
|
---|
2163 | PTMTIMER pTimer = &pQueue->paTimers[idxTimer];
|
---|
2164 | if ( pTimer->enmType == TMTIMERTYPE_DRV
|
---|
2165 | && pTimer->u.Drv.pDrvIns == pDrvIns
|
---|
2166 | && pTimer->enmState < TMTIMERSTATE_DESTROY)
|
---|
2167 | {
|
---|
2168 | PDMCritSectRwLeaveShared(pVM, &pQueue->AllocLock);
|
---|
2169 |
|
---|
2170 | int rc = tmR3TimerDestroy(pVM, pQueue, pTimer);
|
---|
2171 | AssertRC(rc);
|
---|
2172 |
|
---|
2173 | PDMCritSectRwEnterShared(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
2174 | }
|
---|
2175 | }
|
---|
2176 | PDMCritSectRwLeaveShared(pVM, &pQueue->AllocLock);
|
---|
2177 | }
|
---|
2178 |
|
---|
2179 | LogFlow(("TMR3TimerDestroyDriver: returns VINF_SUCCESS\n"));
|
---|
2180 | return VINF_SUCCESS;
|
---|
2181 | }
|
---|
2182 |
|
---|
2183 |
|
---|
2184 | /**
|
---|
2185 | * Internal function for getting the clock time.
|
---|
2186 | *
|
---|
2187 | * @returns clock time.
|
---|
2188 | * @param pVM The cross context VM structure.
|
---|
2189 | * @param enmClock The clock.
|
---|
2190 | */
|
---|
2191 | DECLINLINE(uint64_t) tmClock(PVM pVM, TMCLOCK enmClock)
|
---|
2192 | {
|
---|
2193 | switch (enmClock)
|
---|
2194 | {
|
---|
2195 | case TMCLOCK_VIRTUAL: return TMVirtualGet(pVM);
|
---|
2196 | case TMCLOCK_VIRTUAL_SYNC: return TMVirtualSyncGet(pVM);
|
---|
2197 | case TMCLOCK_REAL: return TMRealGet(pVM);
|
---|
2198 | case TMCLOCK_TSC: return TMCpuTickGet(pVM->apCpusR3[0] /* just take VCPU 0 */);
|
---|
2199 | default:
|
---|
2200 | AssertMsgFailed(("enmClock=%d\n", enmClock));
|
---|
2201 | return ~(uint64_t)0;
|
---|
2202 | }
|
---|
2203 | }
|
---|
2204 |
|
---|
2205 |
|
---|
2206 | /**
|
---|
2207 | * Checks if the sync queue has one or more expired timers.
|
---|
2208 | *
|
---|
2209 | * @returns true / false.
|
---|
2210 | *
|
---|
2211 | * @param pVM The cross context VM structure.
|
---|
2212 | * @param enmClock The queue.
|
---|
2213 | */
|
---|
2214 | DECLINLINE(bool) tmR3HasExpiredTimer(PVM pVM, TMCLOCK enmClock)
|
---|
2215 | {
|
---|
2216 | const uint64_t u64Expire = pVM->tm.s.aTimerQueues[enmClock].u64Expire;
|
---|
2217 | return u64Expire != INT64_MAX && u64Expire <= tmClock(pVM, enmClock);
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 |
|
---|
2221 | /**
|
---|
2222 | * Checks for expired timers in all the queues.
|
---|
2223 | *
|
---|
2224 | * @returns true / false.
|
---|
2225 | * @param pVM The cross context VM structure.
|
---|
2226 | */
|
---|
2227 | DECLINLINE(bool) tmR3AnyExpiredTimers(PVM pVM)
|
---|
2228 | {
|
---|
2229 | /*
|
---|
2230 | * Combine the time calculation for the first two since we're not on EMT
|
---|
2231 | * TMVirtualSyncGet only permits EMT.
|
---|
2232 | */
|
---|
2233 | uint64_t u64Now = TMVirtualGetNoCheck(pVM);
|
---|
2234 | if (pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL].u64Expire <= u64Now)
|
---|
2235 | return true;
|
---|
2236 | u64Now = pVM->tm.s.fVirtualSyncTicking
|
---|
2237 | ? u64Now - pVM->tm.s.offVirtualSync
|
---|
2238 | : pVM->tm.s.u64VirtualSync;
|
---|
2239 | if (pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].u64Expire <= u64Now)
|
---|
2240 | return true;
|
---|
2241 |
|
---|
2242 | /*
|
---|
2243 | * The remaining timers.
|
---|
2244 | */
|
---|
2245 | if (tmR3HasExpiredTimer(pVM, TMCLOCK_REAL))
|
---|
2246 | return true;
|
---|
2247 | if (tmR3HasExpiredTimer(pVM, TMCLOCK_TSC))
|
---|
2248 | return true;
|
---|
2249 | return false;
|
---|
2250 | }
|
---|
2251 |
|
---|
2252 |
|
---|
2253 | /**
|
---|
2254 | * Schedule timer callback.
|
---|
2255 | *
|
---|
2256 | * @param pTimer Timer handle.
|
---|
2257 | * @param pvUser Pointer to the VM.
|
---|
2258 | * @thread Timer thread.
|
---|
2259 | *
|
---|
2260 | * @remark We cannot do the scheduling and queues running from a timer handler
|
---|
2261 | * since it's not executing in EMT, and even if it was it would be async
|
---|
2262 | * and we wouldn't know the state of the affairs.
|
---|
2263 | * So, we'll just raise the timer FF and force any REM execution to exit.
|
---|
2264 | */
|
---|
2265 | static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t /*iTick*/)
|
---|
2266 | {
|
---|
2267 | PVM pVM = (PVM)pvUser;
|
---|
2268 | PVMCPU pVCpuDst = pVM->apCpusR3[pVM->tm.s.idTimerCpu];
|
---|
2269 | NOREF(pTimer);
|
---|
2270 |
|
---|
2271 | AssertCompile(TMCLOCK_MAX == 4);
|
---|
2272 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerCallback);
|
---|
2273 |
|
---|
2274 | #ifdef DEBUG_Sander /* very annoying, keep it private. */
|
---|
2275 | if (VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
|
---|
2276 | Log(("tmR3TimerCallback: timer event still pending!!\n"));
|
---|
2277 | #endif
|
---|
2278 | if ( !VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)
|
---|
2279 | && ( pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].idxSchedule != UINT32_MAX /** @todo FIXME - reconsider offSchedule as a reason for running the timer queues. */
|
---|
2280 | || pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL].idxSchedule != UINT32_MAX
|
---|
2281 | || pVM->tm.s.aTimerQueues[TMCLOCK_REAL].idxSchedule != UINT32_MAX
|
---|
2282 | || pVM->tm.s.aTimerQueues[TMCLOCK_TSC].idxSchedule != UINT32_MAX
|
---|
2283 | || tmR3AnyExpiredTimers(pVM)
|
---|
2284 | )
|
---|
2285 | && !VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)
|
---|
2286 | && !pVM->tm.s.fRunningQueues
|
---|
2287 | )
|
---|
2288 | {
|
---|
2289 | Log5(("TM(%u): FF: 0 -> 1\n", __LINE__));
|
---|
2290 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
2291 | VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM | VMNOTIFYFF_FLAGS_POKE);
|
---|
2292 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerCallbackSetFF);
|
---|
2293 | }
|
---|
2294 | }
|
---|
2295 |
|
---|
2296 |
|
---|
2297 | /**
|
---|
2298 | * Worker for tmR3TimerQueueDoOne that runs pending timers on the specified
|
---|
2299 | * non-empty timer queue.
|
---|
2300 | *
|
---|
2301 | * @param pVM The cross context VM structure.
|
---|
2302 | * @param pQueue The queue to run.
|
---|
2303 | * @param pTimer The head timer. Caller already check that this is
|
---|
2304 | * not NULL.
|
---|
2305 | */
|
---|
2306 | static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
|
---|
2307 | {
|
---|
2308 | VM_ASSERT_EMT(pVM); /** @todo relax this */
|
---|
2309 |
|
---|
2310 | /*
|
---|
2311 | * Run timers.
|
---|
2312 | *
|
---|
2313 | * We check the clock once and run all timers which are ACTIVE
|
---|
2314 | * and have an expire time less or equal to the time we read.
|
---|
2315 | *
|
---|
2316 | * N.B. A generic unlink must be applied since other threads
|
---|
2317 | * are allowed to mess with any active timer at any time.
|
---|
2318 | *
|
---|
2319 | * However, we only allow EMT to handle EXPIRED_PENDING
|
---|
2320 | * timers, thus enabling the timer handler function to
|
---|
2321 | * arm the timer again.
|
---|
2322 | */
|
---|
2323 | /** @todo the above 'however' is outdated. */
|
---|
2324 | const uint64_t u64Now = tmClock(pVM, pQueue->enmClock);
|
---|
2325 | while (pTimer->u64Expire <= u64Now)
|
---|
2326 | {
|
---|
2327 | PTMTIMER const pNext = tmTimerGetNext(pQueue, pTimer);
|
---|
2328 | PPDMCRITSECT pCritSect = pTimer->pCritSect;
|
---|
2329 | if (pCritSect)
|
---|
2330 | {
|
---|
2331 | STAM_PROFILE_START(&pTimer->StatCritSectEnter, Locking);
|
---|
2332 | PDMCritSectEnter(pVM, pCritSect, VERR_IGNORED);
|
---|
2333 | STAM_PROFILE_STOP(&pTimer->StatCritSectEnter, Locking);
|
---|
2334 | }
|
---|
2335 | Log2(("tmR3TimerQueueRun: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .szName='%s'}\n",
|
---|
2336 | pTimer, tmTimerState(pTimer->enmState), pQueue->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->szName));
|
---|
2337 | bool fRc;
|
---|
2338 | TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_GET_UNLINK, TMTIMERSTATE_ACTIVE, fRc);
|
---|
2339 | if (fRc)
|
---|
2340 | {
|
---|
2341 | Assert(pTimer->idxScheduleNext == UINT32_MAX); /* this can trigger falsely */
|
---|
2342 |
|
---|
2343 | /* unlink */
|
---|
2344 | const PTMTIMER pPrev = tmTimerGetPrev(pQueue, pTimer);
|
---|
2345 | if (pPrev)
|
---|
2346 | tmTimerSetNext(pQueue, pPrev, pNext);
|
---|
2347 | else
|
---|
2348 | {
|
---|
2349 | tmTimerQueueSetHead(pQueue, pQueue, pNext);
|
---|
2350 | pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
|
---|
2351 | }
|
---|
2352 | if (pNext)
|
---|
2353 | tmTimerSetPrev(pQueue, pNext, pPrev);
|
---|
2354 | pTimer->idxNext = UINT32_MAX;
|
---|
2355 | pTimer->idxPrev = UINT32_MAX;
|
---|
2356 |
|
---|
2357 | /* fire */
|
---|
2358 | TM_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_DELIVER);
|
---|
2359 | STAM_PROFILE_START(&pTimer->StatTimer, PrfTimer);
|
---|
2360 | switch (pTimer->enmType)
|
---|
2361 | {
|
---|
2362 | case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer->hSelf, pTimer->pvUser); break;
|
---|
2363 | case TMTIMERTYPE_USB: pTimer->u.Usb.pfnTimer(pTimer->u.Usb.pUsbIns, pTimer->hSelf, pTimer->pvUser); break;
|
---|
2364 | case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer->hSelf, pTimer->pvUser); break;
|
---|
2365 | case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer->hSelf, pTimer->pvUser); break;
|
---|
2366 | default:
|
---|
2367 | AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->szName));
|
---|
2368 | break;
|
---|
2369 | }
|
---|
2370 | STAM_PROFILE_STOP(&pTimer->StatTimer, PrfTimer);
|
---|
2371 |
|
---|
2372 | /* change the state if it wasn't changed already in the handler. */
|
---|
2373 | TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED_DELIVER, fRc);
|
---|
2374 | Log2(("tmR3TimerQueueRun: new state %s\n", tmTimerState(pTimer->enmState)));
|
---|
2375 | }
|
---|
2376 | if (pCritSect)
|
---|
2377 | PDMCritSectLeave(pVM, pCritSect);
|
---|
2378 |
|
---|
2379 | /* Advance? */
|
---|
2380 | pTimer = pNext;
|
---|
2381 | if (!pTimer)
|
---|
2382 | break;
|
---|
2383 | } /* run loop */
|
---|
2384 | }
|
---|
2385 |
|
---|
2386 |
|
---|
2387 | /**
|
---|
2388 | * Service one regular timer queue.
|
---|
2389 | *
|
---|
2390 | * @param pVM The cross context VM structure.
|
---|
2391 | * @param pQueue The queue.
|
---|
2392 | */
|
---|
2393 | static void tmR3TimerQueueDoOne(PVM pVM, PTMTIMERQUEUE pQueue)
|
---|
2394 | {
|
---|
2395 | Assert(pQueue->enmClock != TMCLOCK_VIRTUAL_SYNC);
|
---|
2396 |
|
---|
2397 | /*
|
---|
2398 | * Only one thread should be "doing" the queue.
|
---|
2399 | */
|
---|
2400 | if (ASMAtomicCmpXchgBool(&pQueue->fBeingProcessed, true, false))
|
---|
2401 | {
|
---|
2402 | STAM_PROFILE_START(&pQueue->StatDo, s);
|
---|
2403 | PDMCritSectEnter(pVM, &pQueue->TimerLock, VERR_IGNORED);
|
---|
2404 |
|
---|
2405 | if (pQueue->idxSchedule != UINT32_MAX)
|
---|
2406 | tmTimerQueueSchedule(pVM, pQueue, pQueue);
|
---|
2407 |
|
---|
2408 | PTMTIMER pHead = tmTimerQueueGetHead(pQueue, pQueue);
|
---|
2409 | if (pHead)
|
---|
2410 | tmR3TimerQueueRun(pVM, pQueue, pHead);
|
---|
2411 |
|
---|
2412 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
2413 | STAM_PROFILE_STOP(&pQueue->StatDo, s);
|
---|
2414 | ASMAtomicWriteBool(&pQueue->fBeingProcessed, false);
|
---|
2415 | }
|
---|
2416 | }
|
---|
2417 |
|
---|
2418 |
|
---|
2419 | /**
|
---|
2420 | * Schedules and runs any pending times in the timer queue for the
|
---|
2421 | * synchronous virtual clock.
|
---|
2422 | *
|
---|
2423 | * This scheduling is a bit different from the other queues as it need
|
---|
2424 | * to implement the special requirements of the timer synchronous virtual
|
---|
2425 | * clock, thus this 2nd queue run function.
|
---|
2426 | *
|
---|
2427 | * @param pVM The cross context VM structure.
|
---|
2428 | *
|
---|
2429 | * @remarks The caller must the Virtual Sync lock. Owning the TM lock is no
|
---|
2430 | * longer important.
|
---|
2431 | */
|
---|
2432 | static void tmR3TimerQueueRunVirtualSync(PVM pVM)
|
---|
2433 | {
|
---|
2434 | PTMTIMERQUEUE const pQueue = &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC];
|
---|
2435 | VM_ASSERT_EMT(pVM);
|
---|
2436 | Assert(PDMCritSectIsOwner(pVM, &pVM->tm.s.VirtualSyncLock));
|
---|
2437 |
|
---|
2438 | /*
|
---|
2439 | * Any timers?
|
---|
2440 | */
|
---|
2441 | PTMTIMER pNext = tmTimerQueueGetHead(pQueue, pQueue);
|
---|
2442 | if (RT_UNLIKELY(!pNext))
|
---|
2443 | {
|
---|
2444 | Assert(pVM->tm.s.fVirtualSyncTicking || !pVM->tm.s.cVirtualTicking);
|
---|
2445 | return;
|
---|
2446 | }
|
---|
2447 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRun);
|
---|
2448 |
|
---|
2449 | /*
|
---|
2450 | * Calculate the time frame for which we will dispatch timers.
|
---|
2451 | *
|
---|
2452 | * We use a time frame ranging from the current sync time (which is most likely the
|
---|
2453 | * same as the head timer) and some configurable period (100000ns) up towards the
|
---|
2454 | * current virtual time. This period might also need to be restricted by the catch-up
|
---|
2455 | * rate so frequent calls to this function won't accelerate the time too much, however
|
---|
2456 | * this will be implemented at a later point if necessary.
|
---|
2457 | *
|
---|
2458 | * Without this frame we would 1) having to run timers much more frequently
|
---|
2459 | * and 2) lag behind at a steady rate.
|
---|
2460 | */
|
---|
2461 | const uint64_t u64VirtualNow = TMVirtualGetNoCheck(pVM);
|
---|
2462 | uint64_t const offSyncGivenUp = pVM->tm.s.offVirtualSyncGivenUp;
|
---|
2463 | uint64_t u64Now;
|
---|
2464 | if (!pVM->tm.s.fVirtualSyncTicking)
|
---|
2465 | {
|
---|
2466 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunStoppedAlready);
|
---|
2467 | u64Now = pVM->tm.s.u64VirtualSync;
|
---|
2468 | Assert(u64Now <= pNext->u64Expire);
|
---|
2469 | }
|
---|
2470 | else
|
---|
2471 | {
|
---|
2472 | /* Calc 'now'. */
|
---|
2473 | bool fStopCatchup = false;
|
---|
2474 | bool fUpdateStuff = false;
|
---|
2475 | uint64_t off = pVM->tm.s.offVirtualSync;
|
---|
2476 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
2477 | {
|
---|
2478 | uint64_t u64Delta = u64VirtualNow - pVM->tm.s.u64VirtualSyncCatchUpPrev;
|
---|
2479 | if (RT_LIKELY(!(u64Delta >> 32)))
|
---|
2480 | {
|
---|
2481 | uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
|
---|
2482 | if (off > u64Sub + offSyncGivenUp)
|
---|
2483 | {
|
---|
2484 | off -= u64Sub;
|
---|
2485 | Log4(("TM: %'RU64/-%'8RU64: sub %'RU64 [tmR3TimerQueueRunVirtualSync]\n", u64VirtualNow - off, off - offSyncGivenUp, u64Sub));
|
---|
2486 | }
|
---|
2487 | else
|
---|
2488 | {
|
---|
2489 | STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
|
---|
2490 | fStopCatchup = true;
|
---|
2491 | off = offSyncGivenUp;
|
---|
2492 | }
|
---|
2493 | fUpdateStuff = true;
|
---|
2494 | }
|
---|
2495 | }
|
---|
2496 | u64Now = u64VirtualNow - off;
|
---|
2497 |
|
---|
2498 | /* Adjust against last returned time. */
|
---|
2499 | uint64_t u64Last = ASMAtomicUoReadU64(&pVM->tm.s.u64VirtualSync);
|
---|
2500 | if (u64Last > u64Now)
|
---|
2501 | {
|
---|
2502 | u64Now = u64Last + 1;
|
---|
2503 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetAdjLast);
|
---|
2504 | }
|
---|
2505 |
|
---|
2506 | /* Check if stopped by expired timer. */
|
---|
2507 | uint64_t const u64Expire = pNext->u64Expire;
|
---|
2508 | if (u64Now >= u64Expire)
|
---|
2509 | {
|
---|
2510 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunStop);
|
---|
2511 | u64Now = u64Expire;
|
---|
2512 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64Now);
|
---|
2513 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
|
---|
2514 | Log4(("TM: %'RU64/-%'8RU64: exp tmr [tmR3TimerQueueRunVirtualSync]\n", u64Now, u64VirtualNow - u64Now - offSyncGivenUp));
|
---|
2515 | }
|
---|
2516 | else
|
---|
2517 | {
|
---|
2518 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64Now);
|
---|
2519 | if (fUpdateStuff)
|
---|
2520 | {
|
---|
2521 | ASMAtomicWriteU64(&pVM->tm.s.offVirtualSync, off);
|
---|
2522 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev, u64VirtualNow);
|
---|
2523 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64Now);
|
---|
2524 | if (fStopCatchup)
|
---|
2525 | {
|
---|
2526 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
|
---|
2527 | Log4(("TM: %'RU64/0: caught up [tmR3TimerQueueRunVirtualSync]\n", u64VirtualNow));
|
---|
2528 | }
|
---|
2529 | }
|
---|
2530 | }
|
---|
2531 | }
|
---|
2532 |
|
---|
2533 | /* calc end of frame. */
|
---|
2534 | uint64_t u64Max = u64Now + pVM->tm.s.u32VirtualSyncScheduleSlack;
|
---|
2535 | if (u64Max > u64VirtualNow - offSyncGivenUp)
|
---|
2536 | u64Max = u64VirtualNow - offSyncGivenUp;
|
---|
2537 |
|
---|
2538 | /* assert sanity */
|
---|
2539 | Assert(u64Now <= u64VirtualNow - offSyncGivenUp);
|
---|
2540 | Assert(u64Max <= u64VirtualNow - offSyncGivenUp);
|
---|
2541 | Assert(u64Now <= u64Max);
|
---|
2542 | Assert(offSyncGivenUp == pVM->tm.s.offVirtualSyncGivenUp);
|
---|
2543 |
|
---|
2544 | /*
|
---|
2545 | * Process the expired timers moving the clock along as we progress.
|
---|
2546 | */
|
---|
2547 | #ifdef VBOX_STRICT
|
---|
2548 | uint64_t u64Prev = u64Now; NOREF(u64Prev);
|
---|
2549 | #endif
|
---|
2550 | while (pNext && pNext->u64Expire <= u64Max)
|
---|
2551 | {
|
---|
2552 | /* Advance */
|
---|
2553 | PTMTIMER pTimer = pNext;
|
---|
2554 | pNext = tmTimerGetNext(pQueue, pTimer);
|
---|
2555 |
|
---|
2556 | /* Take the associated lock. */
|
---|
2557 | PPDMCRITSECT pCritSect = pTimer->pCritSect;
|
---|
2558 | if (pCritSect)
|
---|
2559 | {
|
---|
2560 | STAM_PROFILE_START(&pTimer->StatCritSectEnter, Locking);
|
---|
2561 | PDMCritSectEnter(pVM, pCritSect, VERR_IGNORED);
|
---|
2562 | STAM_PROFILE_STOP(&pTimer->StatCritSectEnter, Locking);
|
---|
2563 | }
|
---|
2564 |
|
---|
2565 | Log2(("tmR3TimerQueueRunVirtualSync: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .szName='%s'}\n",
|
---|
2566 | pTimer, tmTimerState(pTimer->enmState), pQueue->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->szName));
|
---|
2567 |
|
---|
2568 | /* Advance the clock - don't permit timers to be out of order or armed
|
---|
2569 | in the 'past'. */
|
---|
2570 | #ifdef VBOX_STRICT
|
---|
2571 | AssertMsg(pTimer->u64Expire >= u64Prev, ("%'RU64 < %'RU64 %s\n", pTimer->u64Expire, u64Prev, pTimer->szName));
|
---|
2572 | u64Prev = pTimer->u64Expire;
|
---|
2573 | #endif
|
---|
2574 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, pTimer->u64Expire);
|
---|
2575 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
|
---|
2576 |
|
---|
2577 | /* Unlink it, change the state and do the callout. */
|
---|
2578 | tmTimerQueueUnlinkActive(pVM, pQueue, pQueue, pTimer);
|
---|
2579 | TM_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_DELIVER);
|
---|
2580 | STAM_PROFILE_START(&pTimer->StatTimer, PrfTimer);
|
---|
2581 | switch (pTimer->enmType)
|
---|
2582 | {
|
---|
2583 | case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer->hSelf, pTimer->pvUser); break;
|
---|
2584 | case TMTIMERTYPE_USB: pTimer->u.Usb.pfnTimer(pTimer->u.Usb.pUsbIns, pTimer->hSelf, pTimer->pvUser); break;
|
---|
2585 | case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer->hSelf, pTimer->pvUser); break;
|
---|
2586 | case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer->hSelf, pTimer->pvUser); break;
|
---|
2587 | default:
|
---|
2588 | AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->szName));
|
---|
2589 | break;
|
---|
2590 | }
|
---|
2591 | STAM_PROFILE_STOP(&pTimer->StatTimer, PrfTimer);
|
---|
2592 |
|
---|
2593 | /* Change the state if it wasn't changed already in the handler.
|
---|
2594 | Reset the Hz hint too since this is the same as TMTimerStop. */
|
---|
2595 | bool fRc;
|
---|
2596 | TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED_DELIVER, fRc);
|
---|
2597 | if (fRc && pTimer->uHzHint)
|
---|
2598 | {
|
---|
2599 | if (pTimer->uHzHint >= pQueue->uMaxHzHint)
|
---|
2600 | ASMAtomicOrU64(&pVM->tm.s.HzHint.u64Combined, RT_BIT_32(TMCLOCK_VIRTUAL_SYNC) | RT_BIT_32(TMCLOCK_VIRTUAL_SYNC + 16));
|
---|
2601 | pTimer->uHzHint = 0;
|
---|
2602 | }
|
---|
2603 | Log2(("tmR3TimerQueueRunVirtualSync: new state %s\n", tmTimerState(pTimer->enmState)));
|
---|
2604 |
|
---|
2605 | /* Leave the associated lock. */
|
---|
2606 | if (pCritSect)
|
---|
2607 | PDMCritSectLeave(pVM, pCritSect);
|
---|
2608 | } /* run loop */
|
---|
2609 |
|
---|
2610 |
|
---|
2611 | /*
|
---|
2612 | * Restart the clock if it was stopped to serve any timers,
|
---|
2613 | * and start/adjust catch-up if necessary.
|
---|
2614 | */
|
---|
2615 | if ( !pVM->tm.s.fVirtualSyncTicking
|
---|
2616 | && pVM->tm.s.cVirtualTicking)
|
---|
2617 | {
|
---|
2618 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunRestart);
|
---|
2619 |
|
---|
2620 | /* calc the slack we've handed out. */
|
---|
2621 | const uint64_t u64VirtualNow2 = TMVirtualGetNoCheck(pVM);
|
---|
2622 | Assert(u64VirtualNow2 >= u64VirtualNow);
|
---|
2623 | AssertMsg(pVM->tm.s.u64VirtualSync >= u64Now, ("%'RU64 < %'RU64\n", pVM->tm.s.u64VirtualSync, u64Now));
|
---|
2624 | const uint64_t offSlack = pVM->tm.s.u64VirtualSync - u64Now;
|
---|
2625 | STAM_STATS({
|
---|
2626 | if (offSlack)
|
---|
2627 | {
|
---|
2628 | PSTAMPROFILE p = &pVM->tm.s.StatVirtualSyncRunSlack;
|
---|
2629 | p->cPeriods++;
|
---|
2630 | p->cTicks += offSlack;
|
---|
2631 | if (p->cTicksMax < offSlack) p->cTicksMax = offSlack;
|
---|
2632 | if (p->cTicksMin > offSlack) p->cTicksMin = offSlack;
|
---|
2633 | }
|
---|
2634 | });
|
---|
2635 |
|
---|
2636 | /* Let the time run a little bit while we were busy running timers(?). */
|
---|
2637 | uint64_t u64Elapsed;
|
---|
2638 | #define MAX_ELAPSED 30000U /* ns */
|
---|
2639 | if (offSlack > MAX_ELAPSED)
|
---|
2640 | u64Elapsed = 0;
|
---|
2641 | else
|
---|
2642 | {
|
---|
2643 | u64Elapsed = u64VirtualNow2 - u64VirtualNow;
|
---|
2644 | if (u64Elapsed > MAX_ELAPSED)
|
---|
2645 | u64Elapsed = MAX_ELAPSED;
|
---|
2646 | u64Elapsed = u64Elapsed > offSlack ? u64Elapsed - offSlack : 0;
|
---|
2647 | }
|
---|
2648 | #undef MAX_ELAPSED
|
---|
2649 |
|
---|
2650 | /* Calc the current offset. */
|
---|
2651 | uint64_t offNew = u64VirtualNow2 - pVM->tm.s.u64VirtualSync - u64Elapsed;
|
---|
2652 | Assert(!(offNew & RT_BIT_64(63)));
|
---|
2653 | uint64_t offLag = offNew - pVM->tm.s.offVirtualSyncGivenUp;
|
---|
2654 | Assert(!(offLag & RT_BIT_64(63)));
|
---|
2655 |
|
---|
2656 | /*
|
---|
2657 | * Deal with starting, adjusting and stopping catchup.
|
---|
2658 | */
|
---|
2659 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
2660 | {
|
---|
2661 | if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpStopThreshold)
|
---|
2662 | {
|
---|
2663 | /* stop */
|
---|
2664 | STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
|
---|
2665 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
|
---|
2666 | Log4(("TM: %'RU64/-%'8RU64: caught up [pt]\n", u64VirtualNow2 - offNew, offLag));
|
---|
2667 | }
|
---|
2668 | else if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold)
|
---|
2669 | {
|
---|
2670 | /* adjust */
|
---|
2671 | unsigned i = 0;
|
---|
2672 | while ( i + 1 < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods)
|
---|
2673 | && offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[i + 1].u64Start)
|
---|
2674 | i++;
|
---|
2675 | if (pVM->tm.s.u32VirtualSyncCatchUpPercentage < pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage)
|
---|
2676 | {
|
---|
2677 | STAM_COUNTER_INC(&pVM->tm.s.aStatVirtualSyncCatchupAdjust[i]);
|
---|
2678 | ASMAtomicWriteU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
|
---|
2679 | Log4(("TM: %'RU64/%'8RU64: adj %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
|
---|
2680 | }
|
---|
2681 | pVM->tm.s.u64VirtualSyncCatchUpPrev = u64VirtualNow2;
|
---|
2682 | }
|
---|
2683 | else
|
---|
2684 | {
|
---|
2685 | /* give up */
|
---|
2686 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGiveUp);
|
---|
2687 | STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
|
---|
2688 | ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
|
---|
2689 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
|
---|
2690 | Log4(("TM: %'RU64/%'8RU64: give up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
|
---|
2691 | LogRel(("TM: Giving up catch-up attempt at a %'RU64 ns lag; new total: %'RU64 ns\n", offLag, offNew));
|
---|
2692 | }
|
---|
2693 | }
|
---|
2694 | else if (offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[0].u64Start)
|
---|
2695 | {
|
---|
2696 | if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold)
|
---|
2697 | {
|
---|
2698 | /* start */
|
---|
2699 | STAM_PROFILE_ADV_START(&pVM->tm.s.StatVirtualSyncCatchup, c);
|
---|
2700 | unsigned i = 0;
|
---|
2701 | while ( i + 1 < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods)
|
---|
2702 | && offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[i + 1].u64Start)
|
---|
2703 | i++;
|
---|
2704 | STAM_COUNTER_INC(&pVM->tm.s.aStatVirtualSyncCatchupInitial[i]);
|
---|
2705 | ASMAtomicWriteU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
|
---|
2706 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, true);
|
---|
2707 | Log4(("TM: %'RU64/%'8RU64: catch-up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
|
---|
2708 | }
|
---|
2709 | else
|
---|
2710 | {
|
---|
2711 | /* don't bother */
|
---|
2712 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGiveUpBeforeStarting);
|
---|
2713 | ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
|
---|
2714 | Log4(("TM: %'RU64/%'8RU64: give up\n", u64VirtualNow2 - offNew, offLag));
|
---|
2715 | LogRel(("TM: Not bothering to attempt catching up a %'RU64 ns lag; new total: %'RU64\n", offLag, offNew));
|
---|
2716 | }
|
---|
2717 | }
|
---|
2718 |
|
---|
2719 | /*
|
---|
2720 | * Update the offset and restart the clock.
|
---|
2721 | */
|
---|
2722 | Assert(!(offNew & RT_BIT_64(63)));
|
---|
2723 | ASMAtomicWriteU64(&pVM->tm.s.offVirtualSync, offNew);
|
---|
2724 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, true);
|
---|
2725 | }
|
---|
2726 | }
|
---|
2727 |
|
---|
2728 |
|
---|
2729 | /**
|
---|
2730 | * Deals with stopped Virtual Sync clock.
|
---|
2731 | *
|
---|
2732 | * This is called by the forced action flag handling code in EM when it
|
---|
2733 | * encounters the VM_FF_TM_VIRTUAL_SYNC flag. It is called by all VCPUs and they
|
---|
2734 | * will block on the VirtualSyncLock until the pending timers has been executed
|
---|
2735 | * and the clock restarted.
|
---|
2736 | *
|
---|
2737 | * @param pVM The cross context VM structure.
|
---|
2738 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
2739 | *
|
---|
2740 | * @thread EMTs
|
---|
2741 | */
|
---|
2742 | VMMR3_INT_DECL(void) TMR3VirtualSyncFF(PVM pVM, PVMCPU pVCpu)
|
---|
2743 | {
|
---|
2744 | Log2(("TMR3VirtualSyncFF:\n"));
|
---|
2745 |
|
---|
2746 | /*
|
---|
2747 | * The EMT doing the timers is diverted to them.
|
---|
2748 | */
|
---|
2749 | if (pVCpu->idCpu == pVM->tm.s.idTimerCpu)
|
---|
2750 | TMR3TimerQueuesDo(pVM);
|
---|
2751 | /*
|
---|
2752 | * The other EMTs will block on the virtual sync lock and the first owner
|
---|
2753 | * will run the queue and thus restarting the clock.
|
---|
2754 | *
|
---|
2755 | * Note! This is very suboptimal code wrt to resuming execution when there
|
---|
2756 | * are more than two Virtual CPUs, since they will all have to enter
|
---|
2757 | * the critical section one by one. But it's a very simple solution
|
---|
2758 | * which will have to do the job for now.
|
---|
2759 | */
|
---|
2760 | else
|
---|
2761 | {
|
---|
2762 | /** @todo Optimize for SMP */
|
---|
2763 | STAM_PROFILE_START(&pVM->tm.s.StatVirtualSyncFF, a);
|
---|
2764 | PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VERR_IGNORED);
|
---|
2765 | if (pVM->tm.s.fVirtualSyncTicking)
|
---|
2766 | {
|
---|
2767 | STAM_PROFILE_STOP(&pVM->tm.s.StatVirtualSyncFF, a); /* before the unlock! */
|
---|
2768 | PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
|
---|
2769 | Log2(("TMR3VirtualSyncFF: ticking\n"));
|
---|
2770 | }
|
---|
2771 | else
|
---|
2772 | {
|
---|
2773 | PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
|
---|
2774 |
|
---|
2775 | /* try run it. */
|
---|
2776 | PDMCritSectEnter(pVM, &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL].TimerLock, VERR_IGNORED);
|
---|
2777 | PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VERR_IGNORED);
|
---|
2778 | if (pVM->tm.s.fVirtualSyncTicking)
|
---|
2779 | Log2(("TMR3VirtualSyncFF: ticking (2)\n"));
|
---|
2780 | else
|
---|
2781 | {
|
---|
2782 | ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, true);
|
---|
2783 | Log2(("TMR3VirtualSyncFF: running queue\n"));
|
---|
2784 |
|
---|
2785 | Assert(pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC].idxSchedule == UINT32_MAX);
|
---|
2786 | tmR3TimerQueueRunVirtualSync(pVM);
|
---|
2787 | if (pVM->tm.s.fVirtualSyncTicking) /** @todo move into tmR3TimerQueueRunVirtualSync - FIXME */
|
---|
2788 | VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
|
---|
2789 |
|
---|
2790 | ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, false);
|
---|
2791 | }
|
---|
2792 | PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
|
---|
2793 | STAM_PROFILE_STOP(&pVM->tm.s.StatVirtualSyncFF, a); /* before the unlock! */
|
---|
2794 | PDMCritSectLeave(pVM, &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL].TimerLock);
|
---|
2795 | }
|
---|
2796 | }
|
---|
2797 | }
|
---|
2798 |
|
---|
2799 |
|
---|
2800 | /**
|
---|
2801 | * Service the special virtual sync timer queue.
|
---|
2802 | *
|
---|
2803 | * @param pVM The cross context VM structure.
|
---|
2804 | * @param pVCpuDst The destination VCpu.
|
---|
2805 | */
|
---|
2806 | static void tmR3TimerQueueDoVirtualSync(PVM pVM, PVMCPU pVCpuDst)
|
---|
2807 | {
|
---|
2808 | PTMTIMERQUEUE pQueue = &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL_SYNC];
|
---|
2809 | if (ASMAtomicCmpXchgBool(&pQueue->fBeingProcessed, true, false))
|
---|
2810 | {
|
---|
2811 | STAM_PROFILE_START(&pQueue->StatDo, s1);
|
---|
2812 | PDMCritSectEnter(pVM, &pQueue->TimerLock, VERR_IGNORED);
|
---|
2813 | PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VERR_IGNORED);
|
---|
2814 | ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, true);
|
---|
2815 | VMCPU_FF_CLEAR(pVCpuDst, VMCPU_FF_TIMER); /* Clear the FF once we started working for real. */
|
---|
2816 |
|
---|
2817 | Assert(pQueue->idxSchedule == UINT32_MAX);
|
---|
2818 | tmR3TimerQueueRunVirtualSync(pVM);
|
---|
2819 | if (pVM->tm.s.fVirtualSyncTicking) /** @todo move into tmR3TimerQueueRunVirtualSync - FIXME */
|
---|
2820 | VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
|
---|
2821 |
|
---|
2822 | ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, false);
|
---|
2823 | PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
|
---|
2824 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
2825 | STAM_PROFILE_STOP(&pQueue->StatDo, s1);
|
---|
2826 | ASMAtomicWriteBool(&pQueue->fBeingProcessed, false);
|
---|
2827 | }
|
---|
2828 | }
|
---|
2829 |
|
---|
2830 |
|
---|
2831 | /**
|
---|
2832 | * Schedules and runs any pending timers.
|
---|
2833 | *
|
---|
2834 | * This is normally called from a forced action handler in EMT.
|
---|
2835 | *
|
---|
2836 | * @param pVM The cross context VM structure.
|
---|
2837 | *
|
---|
2838 | * @thread EMT (actually EMT0, but we fend off the others)
|
---|
2839 | */
|
---|
2840 | VMMR3DECL(void) TMR3TimerQueuesDo(PVM pVM)
|
---|
2841 | {
|
---|
2842 | /*
|
---|
2843 | * Only the dedicated timer EMT should do stuff here.
|
---|
2844 | * (fRunningQueues is only used as an indicator.)
|
---|
2845 | */
|
---|
2846 | Assert(pVM->tm.s.idTimerCpu < pVM->cCpus);
|
---|
2847 | PVMCPU pVCpuDst = pVM->apCpusR3[pVM->tm.s.idTimerCpu];
|
---|
2848 | if (VMMGetCpu(pVM) != pVCpuDst)
|
---|
2849 | {
|
---|
2850 | Assert(pVM->cCpus > 1);
|
---|
2851 | return;
|
---|
2852 | }
|
---|
2853 | STAM_PROFILE_START(&pVM->tm.s.StatDoQueues, a);
|
---|
2854 | Log2(("TMR3TimerQueuesDo:\n"));
|
---|
2855 | Assert(!pVM->tm.s.fRunningQueues);
|
---|
2856 | ASMAtomicWriteBool(&pVM->tm.s.fRunningQueues, true);
|
---|
2857 |
|
---|
2858 | /*
|
---|
2859 | * Process the queues.
|
---|
2860 | */
|
---|
2861 | AssertCompile(TMCLOCK_MAX == 4);
|
---|
2862 |
|
---|
2863 | /*
|
---|
2864 | * TMCLOCK_VIRTUAL_SYNC (see also TMR3VirtualSyncFF)
|
---|
2865 | */
|
---|
2866 | tmR3TimerQueueDoVirtualSync(pVM, pVCpuDst);
|
---|
2867 |
|
---|
2868 | /*
|
---|
2869 | * TMCLOCK_VIRTUAL
|
---|
2870 | */
|
---|
2871 | tmR3TimerQueueDoOne(pVM, &pVM->tm.s.aTimerQueues[TMCLOCK_VIRTUAL]);
|
---|
2872 |
|
---|
2873 | /*
|
---|
2874 | * TMCLOCK_TSC
|
---|
2875 | */
|
---|
2876 | Assert(pVM->tm.s.aTimerQueues[TMCLOCK_TSC].idxActive == UINT32_MAX); /* not used */
|
---|
2877 |
|
---|
2878 | /*
|
---|
2879 | * TMCLOCK_REAL
|
---|
2880 | */
|
---|
2881 | tmR3TimerQueueDoOne(pVM, &pVM->tm.s.aTimerQueues[TMCLOCK_REAL]);
|
---|
2882 |
|
---|
2883 | #ifdef VBOX_STRICT
|
---|
2884 | /* check that we didn't screw up. */
|
---|
2885 | tmTimerQueuesSanityChecks(pVM, "TMR3TimerQueuesDo");
|
---|
2886 | #endif
|
---|
2887 |
|
---|
2888 | /* done */
|
---|
2889 | Log2(("TMR3TimerQueuesDo: returns void\n"));
|
---|
2890 | ASMAtomicWriteBool(&pVM->tm.s.fRunningQueues, false);
|
---|
2891 | STAM_PROFILE_STOP(&pVM->tm.s.StatDoQueues, a);
|
---|
2892 | }
|
---|
2893 |
|
---|
2894 |
|
---|
2895 |
|
---|
2896 | /** @name Saved state values
|
---|
2897 | * @{ */
|
---|
2898 | #define TMTIMERSTATE_SAVED_PENDING_STOP 4
|
---|
2899 | #define TMTIMERSTATE_SAVED_PENDING_SCHEDULE 7
|
---|
2900 | /** @} */
|
---|
2901 |
|
---|
2902 |
|
---|
2903 | /**
|
---|
2904 | * Saves the state of a timer to a saved state.
|
---|
2905 | *
|
---|
2906 | * @returns VBox status code.
|
---|
2907 | * @param pVM The cross context VM structure.
|
---|
2908 | * @param hTimer Timer to save.
|
---|
2909 | * @param pSSM Save State Manager handle.
|
---|
2910 | */
|
---|
2911 | VMMR3DECL(int) TMR3TimerSave(PVM pVM, TMTIMERHANDLE hTimer, PSSMHANDLE pSSM)
|
---|
2912 | {
|
---|
2913 | VM_ASSERT_EMT(pVM);
|
---|
2914 | TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
|
---|
2915 | LogFlow(("TMR3TimerSave: %p:{enmState=%s, .szName='%s'} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->szName, pSSM));
|
---|
2916 |
|
---|
2917 | switch (pTimer->enmState)
|
---|
2918 | {
|
---|
2919 | case TMTIMERSTATE_STOPPED:
|
---|
2920 | case TMTIMERSTATE_PENDING_STOP:
|
---|
2921 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
2922 | return SSMR3PutU8(pSSM, TMTIMERSTATE_SAVED_PENDING_STOP);
|
---|
2923 |
|
---|
2924 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
2925 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
2926 | AssertMsgFailed(("u64Expire is being updated! (%s)\n", pTimer->szName));
|
---|
2927 | if (!RTThreadYield())
|
---|
2928 | RTThreadSleep(1);
|
---|
2929 | RT_FALL_THRU();
|
---|
2930 | case TMTIMERSTATE_ACTIVE:
|
---|
2931 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
2932 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
2933 | SSMR3PutU8(pSSM, TMTIMERSTATE_SAVED_PENDING_SCHEDULE);
|
---|
2934 | return SSMR3PutU64(pSSM, pTimer->u64Expire);
|
---|
2935 |
|
---|
2936 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
2937 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
2938 | case TMTIMERSTATE_DESTROY:
|
---|
2939 | case TMTIMERSTATE_FREE:
|
---|
2940 | case TMTIMERSTATE_INVALID:
|
---|
2941 | AssertMsgFailed(("Invalid timer state %d %s (%s)\n", pTimer->enmState, tmTimerState(pTimer->enmState), pTimer->szName));
|
---|
2942 | return SSMR3HandleSetStatus(pSSM, VERR_TM_INVALID_STATE);
|
---|
2943 | }
|
---|
2944 |
|
---|
2945 | AssertMsgFailed(("Unknown timer state %d (%s)\n", pTimer->enmState, pTimer->szName));
|
---|
2946 | return SSMR3HandleSetStatus(pSSM, VERR_TM_UNKNOWN_STATE);
|
---|
2947 | }
|
---|
2948 |
|
---|
2949 |
|
---|
2950 | /**
|
---|
2951 | * Loads the state of a timer from a saved state.
|
---|
2952 | *
|
---|
2953 | * @returns VBox status code.
|
---|
2954 | * @param pVM The cross context VM structure.
|
---|
2955 | * @param hTimer Handle of Timer to restore.
|
---|
2956 | * @param pSSM Save State Manager handle.
|
---|
2957 | */
|
---|
2958 | VMMR3DECL(int) TMR3TimerLoad(PVM pVM, TMTIMERHANDLE hTimer, PSSMHANDLE pSSM)
|
---|
2959 | {
|
---|
2960 | VM_ASSERT_EMT(pVM);
|
---|
2961 | TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
|
---|
2962 | Assert(pSSM);
|
---|
2963 | LogFlow(("TMR3TimerLoad: %p:{enmState=%s, .szName='%s'} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->szName, pSSM));
|
---|
2964 |
|
---|
2965 | /*
|
---|
2966 | * Load the state and validate it.
|
---|
2967 | */
|
---|
2968 | uint8_t u8State;
|
---|
2969 | int rc = SSMR3GetU8(pSSM, &u8State);
|
---|
2970 | if (RT_FAILURE(rc))
|
---|
2971 | return rc;
|
---|
2972 |
|
---|
2973 | /* TMTIMERSTATE_SAVED_XXX: Workaround for accidental state shift in r47786 (2009-05-26 19:12:12). */
|
---|
2974 | if ( u8State == TMTIMERSTATE_SAVED_PENDING_STOP + 1
|
---|
2975 | || u8State == TMTIMERSTATE_SAVED_PENDING_SCHEDULE + 1)
|
---|
2976 | u8State--;
|
---|
2977 |
|
---|
2978 | if ( u8State != TMTIMERSTATE_SAVED_PENDING_STOP
|
---|
2979 | && u8State != TMTIMERSTATE_SAVED_PENDING_SCHEDULE)
|
---|
2980 | {
|
---|
2981 | AssertLogRelMsgFailed(("u8State=%d\n", u8State));
|
---|
2982 | return SSMR3HandleSetStatus(pSSM, VERR_TM_LOAD_STATE);
|
---|
2983 | }
|
---|
2984 |
|
---|
2985 | /* Enter the critical sections to make TMTimerSet/Stop happy. */
|
---|
2986 | if (pQueue->enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
2987 | PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VERR_IGNORED);
|
---|
2988 | PPDMCRITSECT pCritSect = pTimer->pCritSect;
|
---|
2989 | if (pCritSect)
|
---|
2990 | PDMCritSectEnter(pVM, pCritSect, VERR_IGNORED);
|
---|
2991 |
|
---|
2992 | if (u8State == TMTIMERSTATE_SAVED_PENDING_SCHEDULE)
|
---|
2993 | {
|
---|
2994 | /*
|
---|
2995 | * Load the expire time.
|
---|
2996 | */
|
---|
2997 | uint64_t u64Expire;
|
---|
2998 | rc = SSMR3GetU64(pSSM, &u64Expire);
|
---|
2999 | if (RT_FAILURE(rc))
|
---|
3000 | return rc;
|
---|
3001 |
|
---|
3002 | /*
|
---|
3003 | * Set it.
|
---|
3004 | */
|
---|
3005 | Log(("u8State=%d u64Expire=%llu\n", u8State, u64Expire));
|
---|
3006 | rc = TMTimerSet(pVM, hTimer, u64Expire);
|
---|
3007 | }
|
---|
3008 | else
|
---|
3009 | {
|
---|
3010 | /*
|
---|
3011 | * Stop it.
|
---|
3012 | */
|
---|
3013 | Log(("u8State=%d\n", u8State));
|
---|
3014 | rc = TMTimerStop(pVM, hTimer);
|
---|
3015 | }
|
---|
3016 |
|
---|
3017 | if (pCritSect)
|
---|
3018 | PDMCritSectLeave(pVM, pCritSect);
|
---|
3019 | if (pQueue->enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
3020 | PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
|
---|
3021 |
|
---|
3022 | /*
|
---|
3023 | * On failure set SSM status.
|
---|
3024 | */
|
---|
3025 | if (RT_FAILURE(rc))
|
---|
3026 | rc = SSMR3HandleSetStatus(pSSM, rc);
|
---|
3027 | return rc;
|
---|
3028 | }
|
---|
3029 |
|
---|
3030 |
|
---|
3031 | /**
|
---|
3032 | * Skips the state of a timer in a given saved state.
|
---|
3033 | *
|
---|
3034 | * @returns VBox status.
|
---|
3035 | * @param pSSM Save State Manager handle.
|
---|
3036 | * @param pfActive Where to store whether the timer was active
|
---|
3037 | * when the state was saved.
|
---|
3038 | */
|
---|
3039 | VMMR3DECL(int) TMR3TimerSkip(PSSMHANDLE pSSM, bool *pfActive)
|
---|
3040 | {
|
---|
3041 | Assert(pSSM); AssertPtr(pfActive);
|
---|
3042 | LogFlow(("TMR3TimerSkip: pSSM=%p pfActive=%p\n", pSSM, pfActive));
|
---|
3043 |
|
---|
3044 | /*
|
---|
3045 | * Load the state and validate it.
|
---|
3046 | */
|
---|
3047 | uint8_t u8State;
|
---|
3048 | int rc = SSMR3GetU8(pSSM, &u8State);
|
---|
3049 | if (RT_FAILURE(rc))
|
---|
3050 | return rc;
|
---|
3051 |
|
---|
3052 | /* TMTIMERSTATE_SAVED_XXX: Workaround for accidental state shift in r47786 (2009-05-26 19:12:12). */
|
---|
3053 | if ( u8State == TMTIMERSTATE_SAVED_PENDING_STOP + 1
|
---|
3054 | || u8State == TMTIMERSTATE_SAVED_PENDING_SCHEDULE + 1)
|
---|
3055 | u8State--;
|
---|
3056 |
|
---|
3057 | if ( u8State != TMTIMERSTATE_SAVED_PENDING_STOP
|
---|
3058 | && u8State != TMTIMERSTATE_SAVED_PENDING_SCHEDULE)
|
---|
3059 | {
|
---|
3060 | AssertLogRelMsgFailed(("u8State=%d\n", u8State));
|
---|
3061 | return SSMR3HandleSetStatus(pSSM, VERR_TM_LOAD_STATE);
|
---|
3062 | }
|
---|
3063 |
|
---|
3064 | *pfActive = (u8State == TMTIMERSTATE_SAVED_PENDING_SCHEDULE);
|
---|
3065 | if (*pfActive)
|
---|
3066 | {
|
---|
3067 | /*
|
---|
3068 | * Load the expire time.
|
---|
3069 | */
|
---|
3070 | uint64_t u64Expire;
|
---|
3071 | rc = SSMR3GetU64(pSSM, &u64Expire);
|
---|
3072 | }
|
---|
3073 |
|
---|
3074 | return rc;
|
---|
3075 | }
|
---|
3076 |
|
---|
3077 |
|
---|
3078 | /**
|
---|
3079 | * Associates a critical section with a timer.
|
---|
3080 | *
|
---|
3081 | * The critical section will be entered prior to doing the timer call back, thus
|
---|
3082 | * avoiding potential races between the timer thread and other threads trying to
|
---|
3083 | * stop or adjust the timer expiration while it's being delivered. The timer
|
---|
3084 | * thread will leave the critical section when the timer callback returns.
|
---|
3085 | *
|
---|
3086 | * In strict builds, ownership of the critical section will be asserted by
|
---|
3087 | * TMTimerSet, TMTimerStop, TMTimerGetExpire and TMTimerDestroy (when called at
|
---|
3088 | * runtime).
|
---|
3089 | *
|
---|
3090 | * @retval VINF_SUCCESS on success.
|
---|
3091 | * @retval VERR_INVALID_HANDLE if the timer handle is NULL or invalid
|
---|
3092 | * (asserted).
|
---|
3093 | * @retval VERR_INVALID_PARAMETER if pCritSect is NULL or has an invalid magic
|
---|
3094 | * (asserted).
|
---|
3095 | * @retval VERR_ALREADY_EXISTS if a critical section was already associated
|
---|
3096 | * with the timer (asserted).
|
---|
3097 | * @retval VERR_INVALID_STATE if the timer isn't stopped.
|
---|
3098 | *
|
---|
3099 | * @param pVM The cross context VM structure.
|
---|
3100 | * @param hTimer The timer handle.
|
---|
3101 | * @param pCritSect The critical section. The caller must make sure this
|
---|
3102 | * is around for the life time of the timer.
|
---|
3103 | *
|
---|
3104 | * @thread Any, but the caller is responsible for making sure the timer is not
|
---|
3105 | * active.
|
---|
3106 | */
|
---|
3107 | VMMR3DECL(int) TMR3TimerSetCritSect(PVM pVM, TMTIMERHANDLE hTimer, PPDMCRITSECT pCritSect)
|
---|
3108 | {
|
---|
3109 | TMTIMER_HANDLE_TO_VARS_RETURN(pVM, hTimer); /* => pTimer, pQueueCC, pQueue, idxTimer, idxQueue */
|
---|
3110 | AssertPtrReturn(pCritSect, VERR_INVALID_PARAMETER);
|
---|
3111 | const char *pszName = PDMR3CritSectName(pCritSect); /* exploited for validation */
|
---|
3112 | AssertReturn(pszName, VERR_INVALID_PARAMETER);
|
---|
3113 | AssertReturn(!pTimer->pCritSect, VERR_ALREADY_EXISTS);
|
---|
3114 | AssertReturn(pTimer->enmState == TMTIMERSTATE_STOPPED, VERR_INVALID_STATE);
|
---|
3115 | AssertReturn( pTimer->enmType == TMTIMERTYPE_DEV
|
---|
3116 | || pTimer->enmType == TMTIMERTYPE_DRV
|
---|
3117 | || pTimer->enmType == TMTIMERTYPE_USB,
|
---|
3118 | VERR_NOT_SUPPORTED); /* Not supported on internal timers, see tmRZTimerGetCritSect. */
|
---|
3119 | LogFlow(("pTimer=%p (%s) pCritSect=%p (%s)\n", pTimer, pTimer->szName, pCritSect, pszName));
|
---|
3120 |
|
---|
3121 | pTimer->pCritSect = pCritSect;
|
---|
3122 | return VINF_SUCCESS;
|
---|
3123 | }
|
---|
3124 |
|
---|
3125 |
|
---|
3126 | /**
|
---|
3127 | * Get the real world UTC time adjusted for VM lag.
|
---|
3128 | *
|
---|
3129 | * @returns pTime.
|
---|
3130 | * @param pVM The cross context VM structure.
|
---|
3131 | * @param pTime Where to store the time.
|
---|
3132 | */
|
---|
3133 | VMMR3_INT_DECL(PRTTIMESPEC) TMR3UtcNow(PVM pVM, PRTTIMESPEC pTime)
|
---|
3134 | {
|
---|
3135 | /*
|
---|
3136 | * Get a stable set of VirtualSync parameters and calc the lag.
|
---|
3137 | */
|
---|
3138 | uint64_t offVirtualSync;
|
---|
3139 | uint64_t offVirtualSyncGivenUp;
|
---|
3140 | do
|
---|
3141 | {
|
---|
3142 | offVirtualSync = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
|
---|
3143 | offVirtualSyncGivenUp = ASMAtomicReadU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp);
|
---|
3144 | } while (ASMAtomicReadU64(&pVM->tm.s.offVirtualSync) != offVirtualSync);
|
---|
3145 |
|
---|
3146 | Assert(offVirtualSync >= offVirtualSyncGivenUp);
|
---|
3147 | uint64_t const offLag = offVirtualSync - offVirtualSyncGivenUp;
|
---|
3148 |
|
---|
3149 | /*
|
---|
3150 | * Get current time and adjust for virtual sync lag and do time displacement.
|
---|
3151 | */
|
---|
3152 | RTTimeNow(pTime);
|
---|
3153 | RTTimeSpecSubNano(pTime, offLag);
|
---|
3154 | RTTimeSpecAddNano(pTime, pVM->tm.s.offUTC);
|
---|
3155 |
|
---|
3156 | /*
|
---|
3157 | * Log details if the time changed radically (also triggers on first call).
|
---|
3158 | */
|
---|
3159 | int64_t nsPrev = ASMAtomicXchgS64(&pVM->tm.s.nsLastUtcNow, RTTimeSpecGetNano(pTime));
|
---|
3160 | int64_t cNsDelta = RTTimeSpecGetNano(pTime) - nsPrev;
|
---|
3161 | if ((uint64_t)RT_ABS(cNsDelta) > RT_NS_1HOUR / 2)
|
---|
3162 | {
|
---|
3163 | RTTIMESPEC NowAgain;
|
---|
3164 | RTTimeNow(&NowAgain);
|
---|
3165 | LogRel(("TMR3UtcNow: nsNow=%'RI64 nsPrev=%'RI64 -> cNsDelta=%'RI64 (offLag=%'RI64 offVirtualSync=%'RU64 offVirtualSyncGivenUp=%'RU64, NowAgain=%'RI64)\n",
|
---|
3166 | RTTimeSpecGetNano(pTime), nsPrev, cNsDelta, offLag, offVirtualSync, offVirtualSyncGivenUp, RTTimeSpecGetNano(&NowAgain)));
|
---|
3167 | if (pVM->tm.s.pszUtcTouchFileOnJump && nsPrev != 0)
|
---|
3168 | {
|
---|
3169 | RTFILE hFile;
|
---|
3170 | int rc = RTFileOpen(&hFile, pVM->tm.s.pszUtcTouchFileOnJump,
|
---|
3171 | RTFILE_O_WRITE | RTFILE_O_APPEND | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_NONE);
|
---|
3172 | if (RT_SUCCESS(rc))
|
---|
3173 | {
|
---|
3174 | char szMsg[256];
|
---|
3175 | size_t cch;
|
---|
3176 | cch = RTStrPrintf(szMsg, sizeof(szMsg),
|
---|
3177 | "TMR3UtcNow: nsNow=%'RI64 nsPrev=%'RI64 -> cNsDelta=%'RI64 (offLag=%'RI64 offVirtualSync=%'RU64 offVirtualSyncGivenUp=%'RU64, NowAgain=%'RI64)\n",
|
---|
3178 | RTTimeSpecGetNano(pTime), nsPrev, cNsDelta, offLag, offVirtualSync, offVirtualSyncGivenUp, RTTimeSpecGetNano(&NowAgain));
|
---|
3179 | RTFileWrite(hFile, szMsg, cch, NULL);
|
---|
3180 | RTFileClose(hFile);
|
---|
3181 | }
|
---|
3182 | }
|
---|
3183 | }
|
---|
3184 |
|
---|
3185 | return pTime;
|
---|
3186 | }
|
---|
3187 |
|
---|
3188 |
|
---|
3189 | /**
|
---|
3190 | * Pauses all clocks except TMCLOCK_REAL.
|
---|
3191 | *
|
---|
3192 | * @returns VBox status code, all errors are asserted.
|
---|
3193 | * @param pVM The cross context VM structure.
|
---|
3194 | * @param pVCpu The cross context virtual CPU structure.
|
---|
3195 | * @thread EMT corresponding to Pointer to the VMCPU.
|
---|
3196 | */
|
---|
3197 | VMMR3DECL(int) TMR3NotifySuspend(PVM pVM, PVMCPU pVCpu)
|
---|
3198 | {
|
---|
3199 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
3200 | PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VERR_IGNORED); /* Paranoia: Exploiting the virtual sync lock here. */
|
---|
3201 |
|
---|
3202 | /*
|
---|
3203 | * The shared virtual clock (includes virtual sync which is tied to it).
|
---|
3204 | */
|
---|
3205 | int rc = tmVirtualPauseLocked(pVM);
|
---|
3206 | AssertRCReturnStmt(rc, PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock), rc);
|
---|
3207 |
|
---|
3208 | /*
|
---|
3209 | * Pause the TSC last since it is normally linked to the virtual
|
---|
3210 | * sync clock, so the above code may actually stop both clocks.
|
---|
3211 | */
|
---|
3212 | if (!pVM->tm.s.fTSCTiedToExecution)
|
---|
3213 | {
|
---|
3214 | rc = tmCpuTickPauseLocked(pVM, pVCpu);
|
---|
3215 | AssertRCReturnStmt(rc, PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock), rc);
|
---|
3216 | }
|
---|
3217 |
|
---|
3218 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
3219 | /*
|
---|
3220 | * Update cNsTotal and stats.
|
---|
3221 | */
|
---|
3222 | Assert(!pVCpu->tm.s.fSuspended);
|
---|
3223 | uint64_t const cNsTotalNew = RTTimeNanoTS() - pVCpu->tm.s.nsStartTotal;
|
---|
3224 | uint64_t const cNsOtherNew = cNsTotalNew - pVCpu->tm.s.cNsExecuting - pVCpu->tm.s.cNsHalted;
|
---|
3225 |
|
---|
3226 | # if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
|
---|
3227 | STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsTotal, cNsTotalNew - pVCpu->tm.s.cNsTotalStat);
|
---|
3228 | int64_t const cNsOtherNewDelta = cNsOtherNew - pVCpu->tm.s.cNsOtherStat;
|
---|
3229 | if (cNsOtherNewDelta > 0)
|
---|
3230 | STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsOther, (uint64_t)cNsOtherNewDelta);
|
---|
3231 | # endif
|
---|
3232 |
|
---|
3233 | uint32_t uGen = ASMAtomicIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
|
---|
3234 | pVCpu->tm.s.nsStartTotal = cNsTotalNew;
|
---|
3235 | pVCpu->tm.s.fSuspended = true;
|
---|
3236 | pVCpu->tm.s.cNsTotalStat = cNsTotalNew;
|
---|
3237 | pVCpu->tm.s.cNsOtherStat = cNsOtherNew;
|
---|
3238 | ASMAtomicWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
|
---|
3239 | #endif
|
---|
3240 |
|
---|
3241 | PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
|
---|
3242 | return VINF_SUCCESS;
|
---|
3243 | }
|
---|
3244 |
|
---|
3245 |
|
---|
3246 | /**
|
---|
3247 | * Resumes all clocks except TMCLOCK_REAL.
|
---|
3248 | *
|
---|
3249 | * @returns VBox status code, all errors are asserted.
|
---|
3250 | * @param pVM The cross context VM structure.
|
---|
3251 | * @param pVCpu The cross context virtual CPU structure.
|
---|
3252 | * @thread EMT corresponding to Pointer to the VMCPU.
|
---|
3253 | */
|
---|
3254 | VMMR3DECL(int) TMR3NotifyResume(PVM pVM, PVMCPU pVCpu)
|
---|
3255 | {
|
---|
3256 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
3257 | PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VERR_IGNORED); /* Paranoia: Exploiting the virtual sync lock here. */
|
---|
3258 |
|
---|
3259 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
3260 | /*
|
---|
3261 | * Set u64NsTsStartTotal. There is no need to back this out if either of
|
---|
3262 | * the two calls below fail.
|
---|
3263 | */
|
---|
3264 | uint32_t uGen = ASMAtomicIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
|
---|
3265 | pVCpu->tm.s.nsStartTotal = RTTimeNanoTS() - pVCpu->tm.s.nsStartTotal;
|
---|
3266 | pVCpu->tm.s.fSuspended = false;
|
---|
3267 | ASMAtomicWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
|
---|
3268 | #endif
|
---|
3269 |
|
---|
3270 | /*
|
---|
3271 | * Resume the TSC first since it is normally linked to the virtual sync
|
---|
3272 | * clock, so it may actually not be resumed until we've executed the code
|
---|
3273 | * below.
|
---|
3274 | */
|
---|
3275 | if (!pVM->tm.s.fTSCTiedToExecution)
|
---|
3276 | {
|
---|
3277 | int rc = tmCpuTickResumeLocked(pVM, pVCpu);
|
---|
3278 | AssertRCReturnStmt(rc, PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock), rc);
|
---|
3279 | }
|
---|
3280 |
|
---|
3281 | /*
|
---|
3282 | * The shared virtual clock (includes virtual sync which is tied to it).
|
---|
3283 | */
|
---|
3284 | int rc = tmVirtualResumeLocked(pVM);
|
---|
3285 |
|
---|
3286 | PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
|
---|
3287 | return rc;
|
---|
3288 | }
|
---|
3289 |
|
---|
3290 |
|
---|
3291 | /**
|
---|
3292 | * Sets the warp drive percent of the virtual time.
|
---|
3293 | *
|
---|
3294 | * @returns VBox status code.
|
---|
3295 | * @param pUVM The user mode VM structure.
|
---|
3296 | * @param u32Percent The new percentage. 100 means normal operation.
|
---|
3297 | */
|
---|
3298 | VMMDECL(int) TMR3SetWarpDrive(PUVM pUVM, uint32_t u32Percent)
|
---|
3299 | {
|
---|
3300 | return VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)tmR3SetWarpDrive, 2, pUVM, u32Percent);
|
---|
3301 | }
|
---|
3302 |
|
---|
3303 |
|
---|
3304 | /**
|
---|
3305 | * EMT worker for TMR3SetWarpDrive.
|
---|
3306 | *
|
---|
3307 | * @returns VBox status code.
|
---|
3308 | * @param pUVM The user mode VM handle.
|
---|
3309 | * @param u32Percent See TMR3SetWarpDrive().
|
---|
3310 | * @internal
|
---|
3311 | */
|
---|
3312 | static DECLCALLBACK(int) tmR3SetWarpDrive(PUVM pUVM, uint32_t u32Percent)
|
---|
3313 | {
|
---|
3314 | PVM pVM = pUVM->pVM;
|
---|
3315 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
3316 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
3317 |
|
---|
3318 | /*
|
---|
3319 | * Validate it.
|
---|
3320 | */
|
---|
3321 | AssertMsgReturn(u32Percent >= 2 && u32Percent <= 20000,
|
---|
3322 | ("%RX32 is not between 2 and 20000 (inclusive).\n", u32Percent),
|
---|
3323 | VERR_INVALID_PARAMETER);
|
---|
3324 |
|
---|
3325 | /** @todo This isn't a feature specific to virtual time, move the variables to
|
---|
3326 | * TM level and make it affect TMR3UTCNow as well! */
|
---|
3327 |
|
---|
3328 | PDMCritSectEnter(pVM, &pVM->tm.s.VirtualSyncLock, VERR_IGNORED); /* Paranoia: Exploiting the virtual sync lock here. */
|
---|
3329 |
|
---|
3330 | /*
|
---|
3331 | * If the time is running we'll have to pause it before we can change
|
---|
3332 | * the warp drive settings.
|
---|
3333 | */
|
---|
3334 | bool fPaused = !!pVM->tm.s.cVirtualTicking;
|
---|
3335 | if (fPaused) /** @todo this isn't really working, but wtf. */
|
---|
3336 | TMR3NotifySuspend(pVM, pVCpu);
|
---|
3337 |
|
---|
3338 | /** @todo Should switch TM mode to virt-tsc-emulated if it isn't already! */
|
---|
3339 | pVM->tm.s.u32VirtualWarpDrivePercentage = u32Percent;
|
---|
3340 | pVM->tm.s.fVirtualWarpDrive = u32Percent != 100;
|
---|
3341 | LogRel(("TM: u32VirtualWarpDrivePercentage=%RI32 fVirtualWarpDrive=%RTbool\n",
|
---|
3342 | pVM->tm.s.u32VirtualWarpDrivePercentage, pVM->tm.s.fVirtualWarpDrive));
|
---|
3343 |
|
---|
3344 | if (fPaused)
|
---|
3345 | TMR3NotifyResume(pVM, pVCpu);
|
---|
3346 |
|
---|
3347 | PDMCritSectLeave(pVM, &pVM->tm.s.VirtualSyncLock);
|
---|
3348 | return VINF_SUCCESS;
|
---|
3349 | }
|
---|
3350 |
|
---|
3351 |
|
---|
3352 | /**
|
---|
3353 | * Gets the current TMCLOCK_VIRTUAL time without checking
|
---|
3354 | * timers or anything.
|
---|
3355 | *
|
---|
3356 | * @returns The timestamp.
|
---|
3357 | * @param pUVM The user mode VM structure.
|
---|
3358 | *
|
---|
3359 | * @remarks See TMVirtualGetNoCheck.
|
---|
3360 | */
|
---|
3361 | VMMR3DECL(uint64_t) TMR3TimeVirtGet(PUVM pUVM)
|
---|
3362 | {
|
---|
3363 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT64_MAX);
|
---|
3364 | PVM pVM = pUVM->pVM;
|
---|
3365 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT64_MAX);
|
---|
3366 | return TMVirtualGetNoCheck(pVM);
|
---|
3367 | }
|
---|
3368 |
|
---|
3369 |
|
---|
3370 | /**
|
---|
3371 | * Gets the current TMCLOCK_VIRTUAL time in milliseconds without checking
|
---|
3372 | * timers or anything.
|
---|
3373 | *
|
---|
3374 | * @returns The timestamp in milliseconds.
|
---|
3375 | * @param pUVM The user mode VM structure.
|
---|
3376 | *
|
---|
3377 | * @remarks See TMVirtualGetNoCheck.
|
---|
3378 | */
|
---|
3379 | VMMR3DECL(uint64_t) TMR3TimeVirtGetMilli(PUVM pUVM)
|
---|
3380 | {
|
---|
3381 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT64_MAX);
|
---|
3382 | PVM pVM = pUVM->pVM;
|
---|
3383 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT64_MAX);
|
---|
3384 | return TMVirtualToMilli(pVM, TMVirtualGetNoCheck(pVM));
|
---|
3385 | }
|
---|
3386 |
|
---|
3387 |
|
---|
3388 | /**
|
---|
3389 | * Gets the current TMCLOCK_VIRTUAL time in microseconds without checking
|
---|
3390 | * timers or anything.
|
---|
3391 | *
|
---|
3392 | * @returns The timestamp in microseconds.
|
---|
3393 | * @param pUVM The user mode VM structure.
|
---|
3394 | *
|
---|
3395 | * @remarks See TMVirtualGetNoCheck.
|
---|
3396 | */
|
---|
3397 | VMMR3DECL(uint64_t) TMR3TimeVirtGetMicro(PUVM pUVM)
|
---|
3398 | {
|
---|
3399 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT64_MAX);
|
---|
3400 | PVM pVM = pUVM->pVM;
|
---|
3401 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT64_MAX);
|
---|
3402 | return TMVirtualToMicro(pVM, TMVirtualGetNoCheck(pVM));
|
---|
3403 | }
|
---|
3404 |
|
---|
3405 |
|
---|
3406 | /**
|
---|
3407 | * Gets the current TMCLOCK_VIRTUAL time in nanoseconds without checking
|
---|
3408 | * timers or anything.
|
---|
3409 | *
|
---|
3410 | * @returns The timestamp in nanoseconds.
|
---|
3411 | * @param pUVM The user mode VM structure.
|
---|
3412 | *
|
---|
3413 | * @remarks See TMVirtualGetNoCheck.
|
---|
3414 | */
|
---|
3415 | VMMR3DECL(uint64_t) TMR3TimeVirtGetNano(PUVM pUVM)
|
---|
3416 | {
|
---|
3417 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT64_MAX);
|
---|
3418 | PVM pVM = pUVM->pVM;
|
---|
3419 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT64_MAX);
|
---|
3420 | return TMVirtualToNano(pVM, TMVirtualGetNoCheck(pVM));
|
---|
3421 | }
|
---|
3422 |
|
---|
3423 |
|
---|
3424 | /**
|
---|
3425 | * Gets the current warp drive percent.
|
---|
3426 | *
|
---|
3427 | * @returns The warp drive percent.
|
---|
3428 | * @param pUVM The user mode VM structure.
|
---|
3429 | */
|
---|
3430 | VMMR3DECL(uint32_t) TMR3GetWarpDrive(PUVM pUVM)
|
---|
3431 | {
|
---|
3432 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, UINT32_MAX);
|
---|
3433 | PVM pVM = pUVM->pVM;
|
---|
3434 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
|
---|
3435 | return pVM->tm.s.u32VirtualWarpDrivePercentage;
|
---|
3436 | }
|
---|
3437 |
|
---|
3438 |
|
---|
3439 | #if 0 /* unused - needs a little updating after @bugref{9941}*/
|
---|
3440 | /**
|
---|
3441 | * Gets the performance information for one virtual CPU as seen by the VMM.
|
---|
3442 | *
|
---|
3443 | * The returned times covers the period where the VM is running and will be
|
---|
3444 | * reset when restoring a previous VM state (at least for the time being).
|
---|
3445 | *
|
---|
3446 | * @retval VINF_SUCCESS on success.
|
---|
3447 | * @retval VERR_NOT_IMPLEMENTED if not compiled in.
|
---|
3448 | * @retval VERR_INVALID_STATE if the VM handle is bad.
|
---|
3449 | * @retval VERR_INVALID_CPU_ID if idCpu is out of range.
|
---|
3450 | *
|
---|
3451 | * @param pVM The cross context VM structure.
|
---|
3452 | * @param idCpu The ID of the virtual CPU which times to get.
|
---|
3453 | * @param pcNsTotal Where to store the total run time (nano seconds) of
|
---|
3454 | * the CPU, i.e. the sum of the three other returns.
|
---|
3455 | * Optional.
|
---|
3456 | * @param pcNsExecuting Where to store the time (nano seconds) spent
|
---|
3457 | * executing guest code. Optional.
|
---|
3458 | * @param pcNsHalted Where to store the time (nano seconds) spent
|
---|
3459 | * halted. Optional
|
---|
3460 | * @param pcNsOther Where to store the time (nano seconds) spent
|
---|
3461 | * preempted by the host scheduler, on virtualization
|
---|
3462 | * overhead and on other tasks.
|
---|
3463 | */
|
---|
3464 | VMMR3DECL(int) TMR3GetCpuLoadTimes(PVM pVM, VMCPUID idCpu, uint64_t *pcNsTotal, uint64_t *pcNsExecuting,
|
---|
3465 | uint64_t *pcNsHalted, uint64_t *pcNsOther)
|
---|
3466 | {
|
---|
3467 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_STATE);
|
---|
3468 | AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
3469 |
|
---|
3470 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
3471 | /*
|
---|
3472 | * Get a stable result set.
|
---|
3473 | * This should be way quicker than an EMT request.
|
---|
3474 | */
|
---|
3475 | PVMCPU pVCpu = pVM->apCpusR3[idCpu];
|
---|
3476 | uint32_t uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
|
---|
3477 | uint64_t cNsTotal = pVCpu->tm.s.cNsTotal;
|
---|
3478 | uint64_t cNsExecuting = pVCpu->tm.s.cNsExecuting;
|
---|
3479 | uint64_t cNsHalted = pVCpu->tm.s.cNsHalted;
|
---|
3480 | uint64_t cNsOther = pVCpu->tm.s.cNsOther;
|
---|
3481 | while ( (uTimesGen & 1) /* update in progress */
|
---|
3482 | || uTimesGen != ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen))
|
---|
3483 | {
|
---|
3484 | RTThreadYield();
|
---|
3485 | uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
|
---|
3486 | cNsTotal = pVCpu->tm.s.cNsTotal;
|
---|
3487 | cNsExecuting = pVCpu->tm.s.cNsExecuting;
|
---|
3488 | cNsHalted = pVCpu->tm.s.cNsHalted;
|
---|
3489 | cNsOther = pVCpu->tm.s.cNsOther;
|
---|
3490 | }
|
---|
3491 |
|
---|
3492 | /*
|
---|
3493 | * Fill in the return values.
|
---|
3494 | */
|
---|
3495 | if (pcNsTotal)
|
---|
3496 | *pcNsTotal = cNsTotal;
|
---|
3497 | if (pcNsExecuting)
|
---|
3498 | *pcNsExecuting = cNsExecuting;
|
---|
3499 | if (pcNsHalted)
|
---|
3500 | *pcNsHalted = cNsHalted;
|
---|
3501 | if (pcNsOther)
|
---|
3502 | *pcNsOther = cNsOther;
|
---|
3503 |
|
---|
3504 | return VINF_SUCCESS;
|
---|
3505 |
|
---|
3506 | #else
|
---|
3507 | return VERR_NOT_IMPLEMENTED;
|
---|
3508 | #endif
|
---|
3509 | }
|
---|
3510 | #endif /* unused */
|
---|
3511 |
|
---|
3512 |
|
---|
3513 | /**
|
---|
3514 | * Gets the performance information for one virtual CPU as seen by the VMM in
|
---|
3515 | * percents.
|
---|
3516 | *
|
---|
3517 | * The returned times covers the period where the VM is running and will be
|
---|
3518 | * reset when restoring a previous VM state (at least for the time being).
|
---|
3519 | *
|
---|
3520 | * @retval VINF_SUCCESS on success.
|
---|
3521 | * @retval VERR_NOT_IMPLEMENTED if not compiled in.
|
---|
3522 | * @retval VERR_INVALID_VM_HANDLE if the VM handle is bad.
|
---|
3523 | * @retval VERR_INVALID_CPU_ID if idCpu is out of range.
|
---|
3524 | *
|
---|
3525 | * @param pUVM The usermode VM structure.
|
---|
3526 | * @param idCpu The ID of the virtual CPU which times to get.
|
---|
3527 | * @param pcMsInterval Where to store the interval of the percentages in
|
---|
3528 | * milliseconds. Optional.
|
---|
3529 | * @param pcPctExecuting Where to return the percentage of time spent
|
---|
3530 | * executing guest code. Optional.
|
---|
3531 | * @param pcPctHalted Where to return the percentage of time spent halted.
|
---|
3532 | * Optional
|
---|
3533 | * @param pcPctOther Where to return the percentage of time spent
|
---|
3534 | * preempted by the host scheduler, on virtualization
|
---|
3535 | * overhead and on other tasks.
|
---|
3536 | */
|
---|
3537 | VMMR3DECL(int) TMR3GetCpuLoadPercents(PUVM pUVM, VMCPUID idCpu, uint64_t *pcMsInterval, uint8_t *pcPctExecuting,
|
---|
3538 | uint8_t *pcPctHalted, uint8_t *pcPctOther)
|
---|
3539 | {
|
---|
3540 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
|
---|
3541 | PVM pVM = pUVM->pVM;
|
---|
3542 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
3543 | AssertReturn(idCpu == VMCPUID_ALL || idCpu < pVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
3544 |
|
---|
3545 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
3546 | TMCPULOADSTATE volatile *pState;
|
---|
3547 | if (idCpu == VMCPUID_ALL)
|
---|
3548 | pState = &pVM->tm.s.CpuLoad;
|
---|
3549 | else
|
---|
3550 | pState = &pVM->apCpusR3[idCpu]->tm.s.CpuLoad;
|
---|
3551 |
|
---|
3552 | if (pcMsInterval)
|
---|
3553 | *pcMsInterval = RT_MS_1SEC;
|
---|
3554 | if (pcPctExecuting)
|
---|
3555 | *pcPctExecuting = pState->cPctExecuting;
|
---|
3556 | if (pcPctHalted)
|
---|
3557 | *pcPctHalted = pState->cPctHalted;
|
---|
3558 | if (pcPctOther)
|
---|
3559 | *pcPctOther = pState->cPctOther;
|
---|
3560 |
|
---|
3561 | return VINF_SUCCESS;
|
---|
3562 |
|
---|
3563 | #else
|
---|
3564 | RT_NOREF(pcMsInterval, pcPctExecuting, pcPctHalted, pcPctOther);
|
---|
3565 | return VERR_NOT_IMPLEMENTED;
|
---|
3566 | #endif
|
---|
3567 | }
|
---|
3568 |
|
---|
3569 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
3570 |
|
---|
3571 | /**
|
---|
3572 | * Helper for tmR3CpuLoadTimer.
|
---|
3573 | * @returns
|
---|
3574 | * @param pState The state to update.
|
---|
3575 | * @param cNsTotal Total time.
|
---|
3576 | * @param cNsExecuting Time executing.
|
---|
3577 | * @param cNsHalted Time halted.
|
---|
3578 | */
|
---|
3579 | DECLINLINE(void) tmR3CpuLoadTimerMakeUpdate(PTMCPULOADSTATE pState, uint64_t cNsTotal, uint64_t cNsExecuting, uint64_t cNsHalted)
|
---|
3580 | {
|
---|
3581 | /* Calc & update deltas */
|
---|
3582 | uint64_t cNsTotalDelta = cNsTotal - pState->cNsPrevTotal;
|
---|
3583 | uint64_t cNsExecutingDelta = cNsExecuting - pState->cNsPrevExecuting;
|
---|
3584 | uint64_t cNsHaltedDelta = cNsHalted - pState->cNsPrevHalted;
|
---|
3585 |
|
---|
3586 | if (cNsExecutingDelta + cNsHaltedDelta <= cNsTotalDelta)
|
---|
3587 | { /* likely */ }
|
---|
3588 | else
|
---|
3589 | {
|
---|
3590 | /* Just adjust the executing and halted values down to match the total delta. */
|
---|
3591 | uint64_t const cNsExecAndHalted = cNsExecutingDelta + cNsHaltedDelta;
|
---|
3592 | uint64_t const cNsAdjust = cNsExecAndHalted - cNsTotalDelta + cNsTotalDelta / 64;
|
---|
3593 | cNsExecutingDelta -= (cNsAdjust * cNsExecutingDelta + cNsExecAndHalted - 1) / cNsExecAndHalted;
|
---|
3594 | cNsHaltedDelta -= (cNsAdjust * cNsHaltedDelta + cNsExecAndHalted - 1) / cNsExecAndHalted;
|
---|
3595 | /*Assert(cNsExecutingDelta + cNsHaltedDelta <= cNsTotalDelta); - annoying when debugging */
|
---|
3596 | }
|
---|
3597 |
|
---|
3598 | pState->cNsPrevExecuting = cNsExecuting;
|
---|
3599 | pState->cNsPrevHalted = cNsHalted;
|
---|
3600 | pState->cNsPrevTotal = cNsTotal;
|
---|
3601 |
|
---|
3602 | /* Calc pcts. */
|
---|
3603 | uint8_t cPctExecuting, cPctHalted, cPctOther;
|
---|
3604 | if (!cNsTotalDelta)
|
---|
3605 | {
|
---|
3606 | cPctExecuting = 0;
|
---|
3607 | cPctHalted = 100;
|
---|
3608 | cPctOther = 0;
|
---|
3609 | }
|
---|
3610 | else if (cNsTotalDelta < UINT64_MAX / 4)
|
---|
3611 | {
|
---|
3612 | cPctExecuting = (uint8_t)(cNsExecutingDelta * 100 / cNsTotalDelta);
|
---|
3613 | cPctHalted = (uint8_t)(cNsHaltedDelta * 100 / cNsTotalDelta);
|
---|
3614 | cPctOther = (uint8_t)((cNsTotalDelta - cNsExecutingDelta - cNsHaltedDelta) * 100 / cNsTotalDelta);
|
---|
3615 | }
|
---|
3616 | else
|
---|
3617 | {
|
---|
3618 | cPctExecuting = 0;
|
---|
3619 | cPctHalted = 100;
|
---|
3620 | cPctOther = 0;
|
---|
3621 | }
|
---|
3622 |
|
---|
3623 | /* Update percentages: */
|
---|
3624 | size_t idxHistory = pState->idxHistory + 1;
|
---|
3625 | if (idxHistory >= RT_ELEMENTS(pState->aHistory))
|
---|
3626 | idxHistory = 0;
|
---|
3627 |
|
---|
3628 | pState->cPctExecuting = cPctExecuting;
|
---|
3629 | pState->cPctHalted = cPctHalted;
|
---|
3630 | pState->cPctOther = cPctOther;
|
---|
3631 |
|
---|
3632 | pState->aHistory[idxHistory].cPctExecuting = cPctExecuting;
|
---|
3633 | pState->aHistory[idxHistory].cPctHalted = cPctHalted;
|
---|
3634 | pState->aHistory[idxHistory].cPctOther = cPctOther;
|
---|
3635 |
|
---|
3636 | pState->idxHistory = (uint16_t)idxHistory;
|
---|
3637 | if (pState->cHistoryEntries < RT_ELEMENTS(pState->aHistory))
|
---|
3638 | pState->cHistoryEntries++;
|
---|
3639 | }
|
---|
3640 |
|
---|
3641 |
|
---|
3642 | /**
|
---|
3643 | * @callback_method_impl{FNTMTIMERINT,
|
---|
3644 | * Timer callback that calculates the CPU load since the last
|
---|
3645 | * time it was called.}
|
---|
3646 | */
|
---|
3647 | static DECLCALLBACK(void) tmR3CpuLoadTimer(PVM pVM, TMTIMERHANDLE hTimer, void *pvUser)
|
---|
3648 | {
|
---|
3649 | /*
|
---|
3650 | * Re-arm the timer first.
|
---|
3651 | */
|
---|
3652 | int rc = TMTimerSetMillies(pVM, hTimer, 1000);
|
---|
3653 | AssertLogRelRC(rc);
|
---|
3654 | NOREF(pvUser);
|
---|
3655 |
|
---|
3656 | /*
|
---|
3657 | * Update the values for each CPU.
|
---|
3658 | */
|
---|
3659 | uint64_t cNsTotalAll = 0;
|
---|
3660 | uint64_t cNsExecutingAll = 0;
|
---|
3661 | uint64_t cNsHaltedAll = 0;
|
---|
3662 | for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
|
---|
3663 | {
|
---|
3664 | PVMCPU pVCpu = pVM->apCpusR3[iCpu];
|
---|
3665 |
|
---|
3666 | /* Try get a stable data set. */
|
---|
3667 | uint32_t cTries = 3;
|
---|
3668 | uint64_t nsNow = RTTimeNanoTS();
|
---|
3669 | uint32_t uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
|
---|
3670 | bool fSuspended = pVCpu->tm.s.fSuspended;
|
---|
3671 | uint64_t nsStartTotal = pVCpu->tm.s.nsStartTotal;
|
---|
3672 | uint64_t cNsExecuting = pVCpu->tm.s.cNsExecuting;
|
---|
3673 | uint64_t cNsHalted = pVCpu->tm.s.cNsHalted;
|
---|
3674 | while (RT_UNLIKELY( (uTimesGen & 1) /* update in progress */
|
---|
3675 | || uTimesGen != ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen)))
|
---|
3676 | {
|
---|
3677 | if (!--cTries)
|
---|
3678 | break;
|
---|
3679 | ASMNopPause();
|
---|
3680 | nsNow = RTTimeNanoTS();
|
---|
3681 | uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
|
---|
3682 | fSuspended = pVCpu->tm.s.fSuspended;
|
---|
3683 | nsStartTotal = pVCpu->tm.s.nsStartTotal;
|
---|
3684 | cNsExecuting = pVCpu->tm.s.cNsExecuting;
|
---|
3685 | cNsHalted = pVCpu->tm.s.cNsHalted;
|
---|
3686 | }
|
---|
3687 |
|
---|
3688 | /* Totals */
|
---|
3689 | uint64_t cNsTotal = fSuspended ? nsStartTotal : nsNow - nsStartTotal;
|
---|
3690 | cNsTotalAll += cNsTotal;
|
---|
3691 | cNsExecutingAll += cNsExecuting;
|
---|
3692 | cNsHaltedAll += cNsHalted;
|
---|
3693 |
|
---|
3694 | /* Calc the PCTs and update the state. */
|
---|
3695 | tmR3CpuLoadTimerMakeUpdate(&pVCpu->tm.s.CpuLoad, cNsTotal, cNsExecuting, cNsHalted);
|
---|
3696 |
|
---|
3697 | /* Tell the VCpu to update the other and total stat members. */
|
---|
3698 | ASMAtomicWriteBool(&pVCpu->tm.s.fUpdateStats, true);
|
---|
3699 | }
|
---|
3700 |
|
---|
3701 | /*
|
---|
3702 | * Update the value for all the CPUs.
|
---|
3703 | */
|
---|
3704 | tmR3CpuLoadTimerMakeUpdate(&pVM->tm.s.CpuLoad, cNsTotalAll, cNsExecutingAll, cNsHaltedAll);
|
---|
3705 |
|
---|
3706 | }
|
---|
3707 |
|
---|
3708 | #endif /* !VBOX_WITHOUT_NS_ACCOUNTING */
|
---|
3709 |
|
---|
3710 |
|
---|
3711 | /**
|
---|
3712 | * @callback_method_impl{PFNVMMEMTRENDEZVOUS,
|
---|
3713 | * Worker for TMR3CpuTickParavirtEnable}
|
---|
3714 | */
|
---|
3715 | static DECLCALLBACK(VBOXSTRICTRC) tmR3CpuTickParavirtEnable(PVM pVM, PVMCPU pVCpuEmt, void *pvData)
|
---|
3716 | {
|
---|
3717 | AssertPtr(pVM); Assert(pVM->tm.s.fTSCModeSwitchAllowed); NOREF(pVCpuEmt); NOREF(pvData);
|
---|
3718 | Assert(pVM->tm.s.enmTSCMode != TMTSCMODE_NATIVE_API); /** @todo figure out NEM/win and paravirt */
|
---|
3719 | Assert(tmR3HasFixedTSC(pVM));
|
---|
3720 |
|
---|
3721 | if (pVM->tm.s.enmTSCMode != TMTSCMODE_REAL_TSC_OFFSET)
|
---|
3722 | {
|
---|
3723 | /*
|
---|
3724 | * The return value of TMCpuTickGet() and the guest's TSC value for each
|
---|
3725 | * CPU must remain constant across the TM TSC mode-switch. Thus we have
|
---|
3726 | * the following equation (new/old signifies the new/old tsc modes):
|
---|
3727 | * uNewTsc = uOldTsc
|
---|
3728 | *
|
---|
3729 | * Where (see tmCpuTickGetInternal):
|
---|
3730 | * uOldTsc = uRawOldTsc - offTscRawSrcOld
|
---|
3731 | * uNewTsc = uRawNewTsc - offTscRawSrcNew
|
---|
3732 | *
|
---|
3733 | * Solve it for offTscRawSrcNew without replacing uOldTsc:
|
---|
3734 | * uRawNewTsc - offTscRawSrcNew = uOldTsc
|
---|
3735 | * => -offTscRawSrcNew = uOldTsc - uRawNewTsc
|
---|
3736 | * => offTscRawSrcNew = uRawNewTsc - uOldTsc
|
---|
3737 | */
|
---|
3738 | uint64_t uRawOldTsc = tmR3CpuTickGetRawVirtualNoCheck(pVM);
|
---|
3739 | uint64_t uRawNewTsc = SUPReadTsc();
|
---|
3740 | uint32_t cCpus = pVM->cCpus;
|
---|
3741 | for (uint32_t i = 0; i < cCpus; i++)
|
---|
3742 | {
|
---|
3743 | PVMCPU pVCpu = pVM->apCpusR3[i];
|
---|
3744 | uint64_t uOldTsc = uRawOldTsc - pVCpu->tm.s.offTSCRawSrc;
|
---|
3745 | pVCpu->tm.s.offTSCRawSrc = uRawNewTsc - uOldTsc;
|
---|
3746 | Assert(uRawNewTsc - pVCpu->tm.s.offTSCRawSrc >= uOldTsc); /* paranoia^256 */
|
---|
3747 | }
|
---|
3748 |
|
---|
3749 | LogRel(("TM: Switching TSC mode from '%s' to '%s'\n", tmR3GetTSCModeNameEx(pVM->tm.s.enmTSCMode),
|
---|
3750 | tmR3GetTSCModeNameEx(TMTSCMODE_REAL_TSC_OFFSET)));
|
---|
3751 | pVM->tm.s.enmTSCMode = TMTSCMODE_REAL_TSC_OFFSET;
|
---|
3752 | }
|
---|
3753 | return VINF_SUCCESS;
|
---|
3754 | }
|
---|
3755 |
|
---|
3756 |
|
---|
3757 | /**
|
---|
3758 | * Notify TM that the guest has enabled usage of a paravirtualized TSC.
|
---|
3759 | *
|
---|
3760 | * This may perform a EMT rendezvous and change the TSC virtualization mode.
|
---|
3761 | *
|
---|
3762 | * @returns VBox status code.
|
---|
3763 | * @param pVM The cross context VM structure.
|
---|
3764 | */
|
---|
3765 | VMMR3_INT_DECL(int) TMR3CpuTickParavirtEnable(PVM pVM)
|
---|
3766 | {
|
---|
3767 | int rc = VINF_SUCCESS;
|
---|
3768 | if (pVM->tm.s.fTSCModeSwitchAllowed)
|
---|
3769 | rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, tmR3CpuTickParavirtEnable, NULL);
|
---|
3770 | else
|
---|
3771 | LogRel(("TM: Host/VM is not suitable for using TSC mode '%s', request to change TSC mode ignored\n",
|
---|
3772 | tmR3GetTSCModeNameEx(TMTSCMODE_REAL_TSC_OFFSET)));
|
---|
3773 | pVM->tm.s.fParavirtTscEnabled = true;
|
---|
3774 | return rc;
|
---|
3775 | }
|
---|
3776 |
|
---|
3777 |
|
---|
3778 | /**
|
---|
3779 | * @callback_method_impl{PFNVMMEMTRENDEZVOUS,
|
---|
3780 | * Worker for TMR3CpuTickParavirtDisable}
|
---|
3781 | */
|
---|
3782 | static DECLCALLBACK(VBOXSTRICTRC) tmR3CpuTickParavirtDisable(PVM pVM, PVMCPU pVCpuEmt, void *pvData)
|
---|
3783 | {
|
---|
3784 | AssertPtr(pVM); Assert(pVM->tm.s.fTSCModeSwitchAllowed); NOREF(pVCpuEmt);
|
---|
3785 | RT_NOREF1(pvData);
|
---|
3786 |
|
---|
3787 | if ( pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET
|
---|
3788 | && pVM->tm.s.enmTSCMode != pVM->tm.s.enmOriginalTSCMode)
|
---|
3789 | {
|
---|
3790 | /*
|
---|
3791 | * See tmR3CpuTickParavirtEnable for an explanation of the conversion math.
|
---|
3792 | */
|
---|
3793 | uint64_t uRawOldTsc = SUPReadTsc();
|
---|
3794 | uint64_t uRawNewTsc = tmR3CpuTickGetRawVirtualNoCheck(pVM);
|
---|
3795 | uint32_t cCpus = pVM->cCpus;
|
---|
3796 | for (uint32_t i = 0; i < cCpus; i++)
|
---|
3797 | {
|
---|
3798 | PVMCPU pVCpu = pVM->apCpusR3[i];
|
---|
3799 | uint64_t uOldTsc = uRawOldTsc - pVCpu->tm.s.offTSCRawSrc;
|
---|
3800 | pVCpu->tm.s.offTSCRawSrc = uRawNewTsc - uOldTsc;
|
---|
3801 | Assert(uRawNewTsc - pVCpu->tm.s.offTSCRawSrc >= uOldTsc); /* paranoia^256 */
|
---|
3802 |
|
---|
3803 | /* Update the last-seen tick here as we havent't been updating it (as we don't
|
---|
3804 | need it) while in pure TSC-offsetting mode. */
|
---|
3805 | pVCpu->tm.s.u64TSCLastSeen = uOldTsc;
|
---|
3806 | }
|
---|
3807 |
|
---|
3808 | LogRel(("TM: Switching TSC mode from '%s' to '%s'\n", tmR3GetTSCModeNameEx(pVM->tm.s.enmTSCMode),
|
---|
3809 | tmR3GetTSCModeNameEx(pVM->tm.s.enmOriginalTSCMode)));
|
---|
3810 | pVM->tm.s.enmTSCMode = pVM->tm.s.enmOriginalTSCMode;
|
---|
3811 | }
|
---|
3812 | return VINF_SUCCESS;
|
---|
3813 | }
|
---|
3814 |
|
---|
3815 |
|
---|
3816 | /**
|
---|
3817 | * Notify TM that the guest has disabled usage of a paravirtualized TSC.
|
---|
3818 | *
|
---|
3819 | * If TMR3CpuTickParavirtEnable() changed the TSC virtualization mode, this will
|
---|
3820 | * perform an EMT rendezvous to revert those changes.
|
---|
3821 | *
|
---|
3822 | * @returns VBox status code.
|
---|
3823 | * @param pVM The cross context VM structure.
|
---|
3824 | */
|
---|
3825 | VMMR3_INT_DECL(int) TMR3CpuTickParavirtDisable(PVM pVM)
|
---|
3826 | {
|
---|
3827 | int rc = VINF_SUCCESS;
|
---|
3828 | if (pVM->tm.s.fTSCModeSwitchAllowed)
|
---|
3829 | rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, tmR3CpuTickParavirtDisable, NULL);
|
---|
3830 | pVM->tm.s.fParavirtTscEnabled = false;
|
---|
3831 | return rc;
|
---|
3832 | }
|
---|
3833 |
|
---|
3834 |
|
---|
3835 | /**
|
---|
3836 | * Check whether the guest can be presented a fixed rate & monotonic TSC.
|
---|
3837 | *
|
---|
3838 | * @returns true if TSC is stable, false otherwise.
|
---|
3839 | * @param pVM The cross context VM structure.
|
---|
3840 | * @param fWithParavirtEnabled Whether it's fixed & monotonic when
|
---|
3841 | * paravirt. TSC is enabled or not.
|
---|
3842 | *
|
---|
3843 | * @remarks Must be called only after TMR3InitFinalize().
|
---|
3844 | */
|
---|
3845 | VMMR3_INT_DECL(bool) TMR3CpuTickIsFixedRateMonotonic(PVM pVM, bool fWithParavirtEnabled)
|
---|
3846 | {
|
---|
3847 | /** @todo figure out what exactly we want here later. */
|
---|
3848 | NOREF(fWithParavirtEnabled);
|
---|
3849 | PSUPGLOBALINFOPAGE pGip;
|
---|
3850 | return tmR3HasFixedTSC(pVM) /* Host has fixed-rate TSC. */
|
---|
3851 | && ( (pGip = g_pSUPGlobalInfoPage) == NULL /* Can be NULL in driverless mode. */
|
---|
3852 | || (pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)); /* GIP thinks it's monotonic. */
|
---|
3853 | }
|
---|
3854 |
|
---|
3855 |
|
---|
3856 | /**
|
---|
3857 | * Gets the 5 char clock name for the info tables.
|
---|
3858 | *
|
---|
3859 | * @returns The name.
|
---|
3860 | * @param enmClock The clock.
|
---|
3861 | */
|
---|
3862 | DECLINLINE(const char *) tmR3Get5CharClockName(TMCLOCK enmClock)
|
---|
3863 | {
|
---|
3864 | switch (enmClock)
|
---|
3865 | {
|
---|
3866 | case TMCLOCK_REAL: return "Real ";
|
---|
3867 | case TMCLOCK_VIRTUAL: return "Virt ";
|
---|
3868 | case TMCLOCK_VIRTUAL_SYNC: return "VrSy ";
|
---|
3869 | case TMCLOCK_TSC: return "TSC ";
|
---|
3870 | default: return "Bad ";
|
---|
3871 | }
|
---|
3872 | }
|
---|
3873 |
|
---|
3874 |
|
---|
3875 | /**
|
---|
3876 | * Display all timers.
|
---|
3877 | *
|
---|
3878 | * @param pVM The cross context VM structure.
|
---|
3879 | * @param pHlp The info helpers.
|
---|
3880 | * @param pszArgs Arguments, ignored.
|
---|
3881 | */
|
---|
3882 | static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
3883 | {
|
---|
3884 | NOREF(pszArgs);
|
---|
3885 | pHlp->pfnPrintf(pHlp,
|
---|
3886 | "Timers (pVM=%p)\n"
|
---|
3887 | "%.*s %.*s %.*s %.*s Clock %18s %18s %6s %-25s Description\n",
|
---|
3888 | pVM,
|
---|
3889 | sizeof(RTR3PTR) * 2, "pTimerR3 ",
|
---|
3890 | sizeof(int32_t) * 2, "offNext ",
|
---|
3891 | sizeof(int32_t) * 2, "offPrev ",
|
---|
3892 | sizeof(int32_t) * 2, "offSched ",
|
---|
3893 | "Time",
|
---|
3894 | "Expire",
|
---|
3895 | "HzHint",
|
---|
3896 | "State");
|
---|
3897 | for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
|
---|
3898 | {
|
---|
3899 | PTMTIMERQUEUE const pQueue = &pVM->tm.s.aTimerQueues[idxQueue];
|
---|
3900 | const char * const pszClock = tmR3Get5CharClockName(pQueue->enmClock);
|
---|
3901 | PDMCritSectRwEnterShared(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
3902 | for (uint32_t idxTimer = 0; idxTimer < pQueue->cTimersAlloc; idxTimer++)
|
---|
3903 | {
|
---|
3904 | PTMTIMER pTimer = &pQueue->paTimers[idxTimer];
|
---|
3905 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
3906 | if (enmState < TMTIMERSTATE_DESTROY && enmState > TMTIMERSTATE_INVALID)
|
---|
3907 | pHlp->pfnPrintf(pHlp,
|
---|
3908 | "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %6RU32 %-25s %s\n",
|
---|
3909 | pTimer,
|
---|
3910 | pTimer->idxNext,
|
---|
3911 | pTimer->idxPrev,
|
---|
3912 | pTimer->idxScheduleNext,
|
---|
3913 | pszClock,
|
---|
3914 | TMTimerGet(pVM, pTimer->hSelf),
|
---|
3915 | pTimer->u64Expire,
|
---|
3916 | pTimer->uHzHint,
|
---|
3917 | tmTimerState(enmState),
|
---|
3918 | pTimer->szName);
|
---|
3919 | }
|
---|
3920 | PDMCritSectRwLeaveShared(pVM, &pQueue->AllocLock);
|
---|
3921 | }
|
---|
3922 | }
|
---|
3923 |
|
---|
3924 |
|
---|
3925 | /**
|
---|
3926 | * Display all active timers.
|
---|
3927 | *
|
---|
3928 | * @param pVM The cross context VM structure.
|
---|
3929 | * @param pHlp The info helpers.
|
---|
3930 | * @param pszArgs Arguments, ignored.
|
---|
3931 | */
|
---|
3932 | static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
3933 | {
|
---|
3934 | NOREF(pszArgs);
|
---|
3935 | pHlp->pfnPrintf(pHlp,
|
---|
3936 | "Active Timers (pVM=%p)\n"
|
---|
3937 | "%.*s %.*s %.*s %.*s Clock %18s %18s %6s %-25s Description\n",
|
---|
3938 | pVM,
|
---|
3939 | sizeof(RTR3PTR) * 2, "pTimerR3 ",
|
---|
3940 | sizeof(int32_t) * 2, "offNext ",
|
---|
3941 | sizeof(int32_t) * 2, "offPrev ",
|
---|
3942 | sizeof(int32_t) * 2, "offSched ",
|
---|
3943 | "Time",
|
---|
3944 | "Expire",
|
---|
3945 | "HzHint",
|
---|
3946 | "State");
|
---|
3947 | for (uint32_t idxQueue = 0; idxQueue < RT_ELEMENTS(pVM->tm.s.aTimerQueues); idxQueue++)
|
---|
3948 | {
|
---|
3949 | PTMTIMERQUEUE const pQueue = &pVM->tm.s.aTimerQueues[idxQueue];
|
---|
3950 | const char * const pszClock = tmR3Get5CharClockName(pQueue->enmClock);
|
---|
3951 | PDMCritSectRwEnterShared(pVM, &pQueue->AllocLock, VERR_IGNORED);
|
---|
3952 | PDMCritSectEnter(pVM, &pQueue->TimerLock, VERR_IGNORED);
|
---|
3953 |
|
---|
3954 | for (PTMTIMERR3 pTimer = tmTimerQueueGetHead(pQueue, pQueue);
|
---|
3955 | pTimer;
|
---|
3956 | pTimer = tmTimerGetNext(pQueue, pTimer))
|
---|
3957 | {
|
---|
3958 | pHlp->pfnPrintf(pHlp,
|
---|
3959 | "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %6RU32 %-25s %s\n",
|
---|
3960 | pTimer,
|
---|
3961 | pTimer->idxNext,
|
---|
3962 | pTimer->idxPrev,
|
---|
3963 | pTimer->idxScheduleNext,
|
---|
3964 | pszClock,
|
---|
3965 | TMTimerGet(pVM, pTimer->hSelf),
|
---|
3966 | pTimer->u64Expire,
|
---|
3967 | pTimer->uHzHint,
|
---|
3968 | tmTimerState(pTimer->enmState),
|
---|
3969 | pTimer->szName);
|
---|
3970 | }
|
---|
3971 |
|
---|
3972 | PDMCritSectLeave(pVM, &pQueue->TimerLock);
|
---|
3973 | PDMCritSectRwLeaveShared(pVM, &pQueue->AllocLock);
|
---|
3974 | }
|
---|
3975 | }
|
---|
3976 |
|
---|
3977 |
|
---|
3978 | /**
|
---|
3979 | * Display all clocks.
|
---|
3980 | *
|
---|
3981 | * @param pVM The cross context VM structure.
|
---|
3982 | * @param pHlp The info helpers.
|
---|
3983 | * @param pszArgs Arguments, ignored.
|
---|
3984 | */
|
---|
3985 | static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
3986 | {
|
---|
3987 | NOREF(pszArgs);
|
---|
3988 |
|
---|
3989 | /*
|
---|
3990 | * Read the times first to avoid more than necessary time variation.
|
---|
3991 | */
|
---|
3992 | const uint64_t u64Virtual = TMVirtualGet(pVM);
|
---|
3993 | const uint64_t u64VirtualSync = TMVirtualSyncGet(pVM);
|
---|
3994 | const uint64_t u64Real = TMRealGet(pVM);
|
---|
3995 |
|
---|
3996 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
3997 | {
|
---|
3998 | PVMCPU pVCpu = pVM->apCpusR3[i];
|
---|
3999 | uint64_t u64TSC = TMCpuTickGet(pVCpu);
|
---|
4000 |
|
---|
4001 | /*
|
---|
4002 | * TSC
|
---|
4003 | */
|
---|
4004 | pHlp->pfnPrintf(pHlp,
|
---|
4005 | "Cpu Tick: %18RU64 (%#016RX64) %RU64Hz %s - virtualized",
|
---|
4006 | u64TSC, u64TSC, TMCpuTicksPerSecond(pVM),
|
---|
4007 | pVCpu->tm.s.fTSCTicking ? "ticking" : "paused");
|
---|
4008 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
4009 | {
|
---|
4010 | pHlp->pfnPrintf(pHlp, " - real tsc offset");
|
---|
4011 | if (pVCpu->tm.s.offTSCRawSrc)
|
---|
4012 | pHlp->pfnPrintf(pHlp, "\n offset %RU64", pVCpu->tm.s.offTSCRawSrc);
|
---|
4013 | }
|
---|
4014 | else if (pVM->tm.s.enmTSCMode == TMTSCMODE_NATIVE_API)
|
---|
4015 | pHlp->pfnPrintf(pHlp, " - native api");
|
---|
4016 | else
|
---|
4017 | pHlp->pfnPrintf(pHlp, " - virtual clock");
|
---|
4018 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
4019 | }
|
---|
4020 |
|
---|
4021 | /*
|
---|
4022 | * virtual
|
---|
4023 | */
|
---|
4024 | pHlp->pfnPrintf(pHlp,
|
---|
4025 | " Virtual: %18RU64 (%#016RX64) %RU64Hz %s",
|
---|
4026 | u64Virtual, u64Virtual, TMVirtualGetFreq(pVM),
|
---|
4027 | pVM->tm.s.cVirtualTicking ? "ticking" : "paused");
|
---|
4028 | if (pVM->tm.s.fVirtualWarpDrive)
|
---|
4029 | pHlp->pfnPrintf(pHlp, " WarpDrive %RU32 %%", pVM->tm.s.u32VirtualWarpDrivePercentage);
|
---|
4030 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
4031 |
|
---|
4032 | /*
|
---|
4033 | * virtual sync
|
---|
4034 | */
|
---|
4035 | pHlp->pfnPrintf(pHlp,
|
---|
4036 | "VirtSync: %18RU64 (%#016RX64) %s%s",
|
---|
4037 | u64VirtualSync, u64VirtualSync,
|
---|
4038 | pVM->tm.s.fVirtualSyncTicking ? "ticking" : "paused",
|
---|
4039 | pVM->tm.s.fVirtualSyncCatchUp ? " - catchup" : "");
|
---|
4040 | if (pVM->tm.s.offVirtualSync)
|
---|
4041 | {
|
---|
4042 | pHlp->pfnPrintf(pHlp, "\n offset %RU64", pVM->tm.s.offVirtualSync);
|
---|
4043 | if (pVM->tm.s.u32VirtualSyncCatchUpPercentage)
|
---|
4044 | pHlp->pfnPrintf(pHlp, " catch-up rate %u %%", pVM->tm.s.u32VirtualSyncCatchUpPercentage);
|
---|
4045 | }
|
---|
4046 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
4047 |
|
---|
4048 | /*
|
---|
4049 | * real
|
---|
4050 | */
|
---|
4051 | pHlp->pfnPrintf(pHlp,
|
---|
4052 | " Real: %18RU64 (%#016RX64) %RU64Hz\n",
|
---|
4053 | u64Real, u64Real, TMRealGetFreq(pVM));
|
---|
4054 | }
|
---|
4055 |
|
---|
4056 |
|
---|
4057 | /**
|
---|
4058 | * Helper for tmR3InfoCpuLoad that adjust @a uPct to the given graph width.
|
---|
4059 | */
|
---|
4060 | DECLINLINE(size_t) tmR3InfoCpuLoadAdjustWidth(size_t uPct, size_t cchWidth)
|
---|
4061 | {
|
---|
4062 | if (cchWidth != 100)
|
---|
4063 | uPct = (size_t)(((double)uPct + 0.5) * ((double)cchWidth / 100.0));
|
---|
4064 | return uPct;
|
---|
4065 | }
|
---|
4066 |
|
---|
4067 |
|
---|
4068 | /**
|
---|
4069 | * @callback_method_impl{FNDBGFINFOARGVINT}
|
---|
4070 | */
|
---|
4071 | static DECLCALLBACK(void) tmR3InfoCpuLoad(PVM pVM, PCDBGFINFOHLP pHlp, int cArgs, char **papszArgs)
|
---|
4072 | {
|
---|
4073 | char szTmp[1024];
|
---|
4074 |
|
---|
4075 | /*
|
---|
4076 | * Parse arguments.
|
---|
4077 | */
|
---|
4078 | PTMCPULOADSTATE pState = &pVM->tm.s.CpuLoad;
|
---|
4079 | VMCPUID idCpu = 0;
|
---|
4080 | bool fAllCpus = true;
|
---|
4081 | bool fExpGraph = true;
|
---|
4082 | uint32_t cchWidth = 80;
|
---|
4083 | uint32_t cPeriods = RT_ELEMENTS(pState->aHistory);
|
---|
4084 | uint32_t cRows = 60;
|
---|
4085 |
|
---|
4086 | static const RTGETOPTDEF s_aOptions[] =
|
---|
4087 | {
|
---|
4088 | { "all", 'a', RTGETOPT_REQ_NOTHING },
|
---|
4089 | { "cpu", 'c', RTGETOPT_REQ_UINT32 },
|
---|
4090 | { "periods", 'p', RTGETOPT_REQ_UINT32 },
|
---|
4091 | { "rows", 'r', RTGETOPT_REQ_UINT32 },
|
---|
4092 | { "uni", 'u', RTGETOPT_REQ_NOTHING },
|
---|
4093 | { "uniform", 'u', RTGETOPT_REQ_NOTHING },
|
---|
4094 | { "width", 'w', RTGETOPT_REQ_UINT32 },
|
---|
4095 | { "exp", 'x', RTGETOPT_REQ_NOTHING },
|
---|
4096 | { "exponential", 'x', RTGETOPT_REQ_NOTHING },
|
---|
4097 | };
|
---|
4098 |
|
---|
4099 | RTGETOPTSTATE State;
|
---|
4100 | int rc = RTGetOptInit(&State, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /*fFlags*/);
|
---|
4101 | AssertRC(rc);
|
---|
4102 |
|
---|
4103 | RTGETOPTUNION ValueUnion;
|
---|
4104 | while ((rc = RTGetOpt(&State, &ValueUnion)) != 0)
|
---|
4105 | {
|
---|
4106 | switch (rc)
|
---|
4107 | {
|
---|
4108 | case 'a':
|
---|
4109 | pState = &pVM->apCpusR3[0]->tm.s.CpuLoad;
|
---|
4110 | idCpu = 0;
|
---|
4111 | fAllCpus = true;
|
---|
4112 | break;
|
---|
4113 | case 'c':
|
---|
4114 | if (ValueUnion.u32 < pVM->cCpus)
|
---|
4115 | {
|
---|
4116 | pState = &pVM->apCpusR3[ValueUnion.u32]->tm.s.CpuLoad;
|
---|
4117 | idCpu = ValueUnion.u32;
|
---|
4118 | }
|
---|
4119 | else
|
---|
4120 | {
|
---|
4121 | pState = &pVM->tm.s.CpuLoad;
|
---|
4122 | idCpu = VMCPUID_ALL;
|
---|
4123 | }
|
---|
4124 | fAllCpus = false;
|
---|
4125 | break;
|
---|
4126 | case 'p':
|
---|
4127 | cPeriods = RT_MIN(RT_MAX(ValueUnion.u32, 1), RT_ELEMENTS(pState->aHistory));
|
---|
4128 | break;
|
---|
4129 | case 'r':
|
---|
4130 | cRows = RT_MIN(RT_MAX(ValueUnion.u32, 5), RT_ELEMENTS(pState->aHistory));
|
---|
4131 | break;
|
---|
4132 | case 'w':
|
---|
4133 | cchWidth = RT_MIN(RT_MAX(ValueUnion.u32, 10), sizeof(szTmp) - 32);
|
---|
4134 | break;
|
---|
4135 | case 'x':
|
---|
4136 | fExpGraph = true;
|
---|
4137 | break;
|
---|
4138 | case 'u':
|
---|
4139 | fExpGraph = false;
|
---|
4140 | break;
|
---|
4141 | case 'h':
|
---|
4142 | pHlp->pfnPrintf(pHlp,
|
---|
4143 | "Usage: cpuload [parameters]\n"
|
---|
4144 | " all, -a\n"
|
---|
4145 | " Show statistics for all CPUs. (default)\n"
|
---|
4146 | " cpu=id, -c id\n"
|
---|
4147 | " Show statistics for the specified CPU ID. Show combined stats if out of range.\n"
|
---|
4148 | " periods=count, -p count\n"
|
---|
4149 | " Number of periods to show. Default: all\n"
|
---|
4150 | " rows=count, -r count\n"
|
---|
4151 | " Number of rows in the graphs. Default: 60\n"
|
---|
4152 | " width=count, -w count\n"
|
---|
4153 | " Core graph width in characters. Default: 80\n"
|
---|
4154 | " exp, exponential, -e\n"
|
---|
4155 | " Do 1:1 for more recent half / 30 seconds of the graph, combine the\n"
|
---|
4156 | " rest into increasinly larger chunks. Default.\n"
|
---|
4157 | " uniform, uni, -u\n"
|
---|
4158 | " Combine periods into rows in a uniform manner for the whole graph.\n");
|
---|
4159 | return;
|
---|
4160 | default:
|
---|
4161 | pHlp->pfnGetOptError(pHlp, rc, &ValueUnion, &State);
|
---|
4162 | return;
|
---|
4163 | }
|
---|
4164 | }
|
---|
4165 |
|
---|
4166 | /*
|
---|
4167 | * Do the job.
|
---|
4168 | */
|
---|
4169 | for (;;)
|
---|
4170 | {
|
---|
4171 | uint32_t const cMaxPeriods = pState->cHistoryEntries;
|
---|
4172 | if (cPeriods > cMaxPeriods)
|
---|
4173 | cPeriods = cMaxPeriods;
|
---|
4174 | if (cPeriods > 0)
|
---|
4175 | {
|
---|
4176 | if (fAllCpus)
|
---|
4177 | {
|
---|
4178 | if (idCpu > 0)
|
---|
4179 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
4180 | pHlp->pfnPrintf(pHlp, " CPU load for virtual CPU %#04x\n"
|
---|
4181 | " -------------------------------\n", idCpu);
|
---|
4182 | }
|
---|
4183 |
|
---|
4184 | /*
|
---|
4185 | * Figure number of periods per chunk. We can either do this in a linear
|
---|
4186 | * fashion or a exponential fashion that compresses old history more.
|
---|
4187 | */
|
---|
4188 | size_t cPerRowDecrement = 0;
|
---|
4189 | size_t cPeriodsPerRow = 1;
|
---|
4190 | if (cRows < cPeriods)
|
---|
4191 | {
|
---|
4192 | if (!fExpGraph)
|
---|
4193 | cPeriodsPerRow = (cPeriods + cRows / 2) / cRows;
|
---|
4194 | else
|
---|
4195 | {
|
---|
4196 | /* The last 30 seconds or half of the rows are 1:1, the other part
|
---|
4197 | is in increasing period counts. Code is a little simple but seems
|
---|
4198 | to do the job most of the time, which is all I have time now. */
|
---|
4199 | size_t cPeriodsOneToOne = RT_MIN(30, cRows / 2);
|
---|
4200 | size_t cRestRows = cRows - cPeriodsOneToOne;
|
---|
4201 | size_t cRestPeriods = cPeriods - cPeriodsOneToOne;
|
---|
4202 |
|
---|
4203 | size_t cPeriodsInWindow = 0;
|
---|
4204 | for (cPeriodsPerRow = 0; cPeriodsPerRow <= cRestRows && cPeriodsInWindow < cRestPeriods; cPeriodsPerRow++)
|
---|
4205 | cPeriodsInWindow += cPeriodsPerRow + 1;
|
---|
4206 |
|
---|
4207 | size_t iLower = 1;
|
---|
4208 | while (cPeriodsInWindow < cRestPeriods)
|
---|
4209 | {
|
---|
4210 | cPeriodsPerRow++;
|
---|
4211 | cPeriodsInWindow += cPeriodsPerRow;
|
---|
4212 | cPeriodsInWindow -= iLower;
|
---|
4213 | iLower++;
|
---|
4214 | }
|
---|
4215 |
|
---|
4216 | cPerRowDecrement = 1;
|
---|
4217 | }
|
---|
4218 | }
|
---|
4219 |
|
---|
4220 | /*
|
---|
4221 | * Do the work.
|
---|
4222 | */
|
---|
4223 | size_t cPctExecuting = 0;
|
---|
4224 | size_t cPctOther = 0;
|
---|
4225 | size_t cPeriodsAccumulated = 0;
|
---|
4226 |
|
---|
4227 | size_t cRowsLeft = cRows;
|
---|
4228 | size_t iHistory = (pState->idxHistory - cPeriods) % RT_ELEMENTS(pState->aHistory);
|
---|
4229 | while (cPeriods-- > 0)
|
---|
4230 | {
|
---|
4231 | iHistory++;
|
---|
4232 | if (iHistory >= RT_ELEMENTS(pState->aHistory))
|
---|
4233 | iHistory = 0;
|
---|
4234 |
|
---|
4235 | cPctExecuting += pState->aHistory[iHistory].cPctExecuting;
|
---|
4236 | cPctOther += pState->aHistory[iHistory].cPctOther;
|
---|
4237 | cPeriodsAccumulated += 1;
|
---|
4238 | if ( cPeriodsAccumulated >= cPeriodsPerRow
|
---|
4239 | || cPeriods < cRowsLeft)
|
---|
4240 | {
|
---|
4241 | /*
|
---|
4242 | * Format and output the line.
|
---|
4243 | */
|
---|
4244 | size_t offTmp = 0;
|
---|
4245 | size_t i = tmR3InfoCpuLoadAdjustWidth(cPctExecuting / cPeriodsAccumulated, cchWidth);
|
---|
4246 | while (i-- > 0)
|
---|
4247 | szTmp[offTmp++] = '#';
|
---|
4248 | i = tmR3InfoCpuLoadAdjustWidth(cPctOther / cPeriodsAccumulated, cchWidth);
|
---|
4249 | while (i-- > 0)
|
---|
4250 | szTmp[offTmp++] = 'O';
|
---|
4251 | szTmp[offTmp] = '\0';
|
---|
4252 |
|
---|
4253 | cRowsLeft--;
|
---|
4254 | pHlp->pfnPrintf(pHlp, "%3zus: %s\n", cPeriods + cPeriodsAccumulated / 2, szTmp);
|
---|
4255 |
|
---|
4256 | /* Reset the state: */
|
---|
4257 | cPctExecuting = 0;
|
---|
4258 | cPctOther = 0;
|
---|
4259 | cPeriodsAccumulated = 0;
|
---|
4260 | if (cPeriodsPerRow > cPerRowDecrement)
|
---|
4261 | cPeriodsPerRow -= cPerRowDecrement;
|
---|
4262 | }
|
---|
4263 | }
|
---|
4264 | pHlp->pfnPrintf(pHlp, " (#=guest, O=VMM overhead) idCpu=%#x\n", idCpu);
|
---|
4265 |
|
---|
4266 | }
|
---|
4267 | else
|
---|
4268 | pHlp->pfnPrintf(pHlp, "No load data.\n");
|
---|
4269 |
|
---|
4270 | /*
|
---|
4271 | * Next CPU if we're display all.
|
---|
4272 | */
|
---|
4273 | if (!fAllCpus)
|
---|
4274 | break;
|
---|
4275 | idCpu++;
|
---|
4276 | if (idCpu >= pVM->cCpus)
|
---|
4277 | break;
|
---|
4278 | pState = &pVM->apCpusR3[idCpu]->tm.s.CpuLoad;
|
---|
4279 | }
|
---|
4280 |
|
---|
4281 | }
|
---|
4282 |
|
---|
4283 |
|
---|
4284 | /**
|
---|
4285 | * Gets the descriptive TM TSC mode name given the enum value.
|
---|
4286 | *
|
---|
4287 | * @returns The name.
|
---|
4288 | * @param enmMode The mode to name.
|
---|
4289 | */
|
---|
4290 | static const char *tmR3GetTSCModeNameEx(TMTSCMODE enmMode)
|
---|
4291 | {
|
---|
4292 | switch (enmMode)
|
---|
4293 | {
|
---|
4294 | case TMTSCMODE_REAL_TSC_OFFSET: return "RealTSCOffset";
|
---|
4295 | case TMTSCMODE_VIRT_TSC_EMULATED: return "VirtTSCEmulated";
|
---|
4296 | case TMTSCMODE_DYNAMIC: return "Dynamic";
|
---|
4297 | case TMTSCMODE_NATIVE_API: return "NativeApi";
|
---|
4298 | default: return "???";
|
---|
4299 | }
|
---|
4300 | }
|
---|
4301 |
|
---|
4302 |
|
---|
4303 | /**
|
---|
4304 | * Gets the descriptive TM TSC mode name.
|
---|
4305 | *
|
---|
4306 | * @returns The name.
|
---|
4307 | * @param pVM The cross context VM structure.
|
---|
4308 | */
|
---|
4309 | static const char *tmR3GetTSCModeName(PVM pVM)
|
---|
4310 | {
|
---|
4311 | Assert(pVM);
|
---|
4312 | return tmR3GetTSCModeNameEx(pVM->tm.s.enmTSCMode);
|
---|
4313 | }
|
---|
4314 |
|
---|