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