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