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