1 | /* $Id: TMAllVirtual.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TM - Timeout Manager, Virtual Time, All Contexts.
|
---|
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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_TM
|
---|
23 | #include <VBox/vmm/tm.h>
|
---|
24 | #include <VBox/vmm/dbgftrace.h>
|
---|
25 | #ifdef IN_RING3
|
---|
26 | # include <iprt/thread.h>
|
---|
27 | #endif
|
---|
28 | #include "TMInternal.h"
|
---|
29 | #include <VBox/vmm/vmcc.h>
|
---|
30 | #include <VBox/vmm/vmm.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <VBox/sup.h>
|
---|
34 |
|
---|
35 | #include <iprt/time.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/asm-math.h>
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @interface_method_impl{RTTIMENANOTSDATA,pfnBad}
|
---|
44 | */
|
---|
45 | DECLCALLBACK(DECLEXPORT(void)) tmVirtualNanoTSBad(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev,
|
---|
46 | uint64_t u64PrevNanoTS)
|
---|
47 | {
|
---|
48 | PVM pVM = RT_FROM_MEMBER(pData, VM, CTX_SUFF(tm.s.VirtualGetRawData));
|
---|
49 | pData->cBadPrev++;
|
---|
50 | if ((int64_t)u64DeltaPrev < 0)
|
---|
51 | LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 pVM=%p\n",
|
---|
52 | u64DeltaPrev, u64PrevNanoTS, u64NanoTS, pVM));
|
---|
53 | else
|
---|
54 | Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 pVM=%p (debugging?)\n",
|
---|
55 | u64DeltaPrev, u64PrevNanoTS, u64NanoTS, pVM));
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * @interface_method_impl{RTTIMENANOTSDATA,pfnRediscover}
|
---|
61 | *
|
---|
62 | * This is the initial worker, so the first call in each context ends up here.
|
---|
63 | * It is also used should the delta rating of the host CPUs change or if the
|
---|
64 | * fGetGipCpu feature the current worker relies upon becomes unavailable. The
|
---|
65 | * last two events may occur as CPUs are taken online.
|
---|
66 | */
|
---|
67 | DECLCALLBACK(DECLEXPORT(uint64_t)) tmVirtualNanoTSRediscover(PRTTIMENANOTSDATA pData)
|
---|
68 | {
|
---|
69 | PVM pVM = RT_FROM_MEMBER(pData, VM, CTX_SUFF(tm.s.VirtualGetRawData));
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * We require a valid GIP for the selection below. Invalid GIP is fatal.
|
---|
73 | */
|
---|
74 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
75 | AssertFatalMsg(RT_VALID_PTR(pGip), ("pVM=%p pGip=%p\n", pVM, pGip));
|
---|
76 | AssertFatalMsg(pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC, ("pVM=%p pGip=%p u32Magic=%#x\n", pVM, pGip, pGip->u32Magic));
|
---|
77 | AssertFatalMsg(pGip->u32Mode > SUPGIPMODE_INVALID && pGip->u32Mode < SUPGIPMODE_END,
|
---|
78 | ("pVM=%p pGip=%p u32Mode=%#x\n", pVM, pGip, pGip->u32Mode));
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Determine the new worker.
|
---|
82 | */
|
---|
83 | PFNTIMENANOTSINTERNAL pfnWorker;
|
---|
84 | bool const fLFence = RT_BOOL(ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2);
|
---|
85 | switch (pGip->u32Mode)
|
---|
86 | {
|
---|
87 | case SUPGIPMODE_SYNC_TSC:
|
---|
88 | case SUPGIPMODE_INVARIANT_TSC:
|
---|
89 | #ifdef IN_RING0
|
---|
90 | if (pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO)
|
---|
91 | pfnWorker = fLFence ? RTTimeNanoTSLFenceSyncInvarNoDelta : RTTimeNanoTSLegacySyncInvarNoDelta;
|
---|
92 | else
|
---|
93 | pfnWorker = fLFence ? RTTimeNanoTSLFenceSyncInvarWithDelta : RTTimeNanoTSLegacySyncInvarWithDelta;
|
---|
94 | #else
|
---|
95 | if (pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS)
|
---|
96 | pfnWorker = pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
|
---|
97 | ? fLFence ? RTTimeNanoTSLFenceSyncInvarNoDelta : RTTimeNanoTSLegacySyncInvarNoDelta
|
---|
98 | : fLFence ? RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim : RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim;
|
---|
99 | else if (pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS)
|
---|
100 | pfnWorker = pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
|
---|
101 | ? fLFence ? RTTimeNanoTSLFenceSyncInvarNoDelta : RTTimeNanoTSLegacySyncInvarNoDelta
|
---|
102 | : fLFence ? RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp : RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp;
|
---|
103 | else if (pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID_EXT_0B)
|
---|
104 | pfnWorker = pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
|
---|
105 | ? fLFence ? RTTimeNanoTSLFenceSyncInvarNoDelta : RTTimeNanoTSLegacySyncInvarNoDelta
|
---|
106 | : fLFence ? RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt0B : RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt0B;
|
---|
107 | else if (pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID_EXT_8000001E)
|
---|
108 | pfnWorker = pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
|
---|
109 | ? fLFence ? RTTimeNanoTSLFenceSyncInvarNoDelta : RTTimeNanoTSLegacySyncInvarNoDelta
|
---|
110 | : fLFence ? RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt8000001E : RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt8000001E;
|
---|
111 | else
|
---|
112 | pfnWorker = pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
|
---|
113 | ? fLFence ? RTTimeNanoTSLFenceSyncInvarNoDelta : RTTimeNanoTSLegacySyncInvarNoDelta
|
---|
114 | : fLFence ? RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId : RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId;
|
---|
115 | #endif
|
---|
116 | break;
|
---|
117 |
|
---|
118 | case SUPGIPMODE_ASYNC_TSC:
|
---|
119 | #ifdef IN_RING0
|
---|
120 | pfnWorker = fLFence ? RTTimeNanoTSLFenceAsync : RTTimeNanoTSLegacyAsync;
|
---|
121 | #else
|
---|
122 | if (pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS)
|
---|
123 | pfnWorker = fLFence ? RTTimeNanoTSLFenceAsyncUseIdtrLim : RTTimeNanoTSLegacyAsyncUseIdtrLim;
|
---|
124 | else if (pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS)
|
---|
125 | pfnWorker = fLFence ? RTTimeNanoTSLFenceAsyncUseRdtscp : RTTimeNanoTSLegacyAsyncUseRdtscp;
|
---|
126 | else if (pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_GROUP_IN_CH_NUMBER_IN_CL)
|
---|
127 | pfnWorker = fLFence ? RTTimeNanoTSLFenceAsyncUseRdtscpGroupChNumCl : RTTimeNanoTSLegacyAsyncUseRdtscpGroupChNumCl;
|
---|
128 | else if (pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID_EXT_0B)
|
---|
129 | pfnWorker = fLFence ? RTTimeNanoTSLFenceAsyncUseApicIdExt0B : RTTimeNanoTSLegacyAsyncUseApicIdExt0B;
|
---|
130 | else if (pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID_EXT_8000001E)
|
---|
131 | pfnWorker = fLFence ? RTTimeNanoTSLFenceAsyncUseApicIdExt8000001E : RTTimeNanoTSLegacyAsyncUseApicIdExt8000001E;
|
---|
132 | else
|
---|
133 | pfnWorker = fLFence ? RTTimeNanoTSLFenceAsyncUseApicId : RTTimeNanoTSLegacyAsyncUseApicId;
|
---|
134 | #endif
|
---|
135 | break;
|
---|
136 |
|
---|
137 | default:
|
---|
138 | AssertFatalMsgFailed(("pVM=%p pGip=%p u32Mode=%#x\n", pVM, pGip, pGip->u32Mode));
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Update the pfnVirtualGetRaw pointer and call the worker we selected.
|
---|
143 | */
|
---|
144 | ASMAtomicWritePtr((void * volatile *)&CTX_SUFF(pVM->tm.s.pfnVirtualGetRaw), (void *)(uintptr_t)pfnWorker);
|
---|
145 | return pfnWorker(pData);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * @interface_method_impl{RTTIMENANOTSDATA,pfnBadCpuIndex}
|
---|
151 | */
|
---|
152 | DECLEXPORT(uint64_t) tmVirtualNanoTSBadCpuIndex(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu)
|
---|
153 | {
|
---|
154 | PVM pVM = RT_FROM_MEMBER(pData, VM, CTX_SUFF(tm.s.VirtualGetRawData));
|
---|
155 | AssertFatalMsgFailed(("pVM=%p idApic=%#x iCpuSet=%#x iGipCpu=%#x\n", pVM, idApic, iCpuSet, iGipCpu));
|
---|
156 | #ifndef _MSC_VER
|
---|
157 | return UINT64_MAX;
|
---|
158 | #endif
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Wrapper around the IPRT GIP time methods.
|
---|
164 | */
|
---|
165 | DECLINLINE(uint64_t) tmVirtualGetRawNanoTS(PVMCC pVM)
|
---|
166 | {
|
---|
167 | # ifdef IN_RING3
|
---|
168 | uint64_t u64 = CTXALLSUFF(pVM->tm.s.pfnVirtualGetRaw)(&CTXALLSUFF(pVM->tm.s.VirtualGetRawData));
|
---|
169 | # else /* !IN_RING3 */
|
---|
170 | uint32_t cPrevSteps = pVM->tm.s.CTX_SUFF(VirtualGetRawData).c1nsSteps;
|
---|
171 | uint64_t u64 = pVM->tm.s.CTX_SUFF(pfnVirtualGetRaw)(&pVM->tm.s.CTX_SUFF(VirtualGetRawData));
|
---|
172 | if (cPrevSteps != pVM->tm.s.CTX_SUFF(VirtualGetRawData).c1nsSteps)
|
---|
173 | VMCPU_FF_SET(VMMGetCpu(pVM), VMCPU_FF_TO_R3);
|
---|
174 | # endif /* !IN_RING3 */
|
---|
175 | /*DBGFTRACE_POS_U64(pVM, u64);*/
|
---|
176 | return u64;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Get the time when we're not running at 100%
|
---|
182 | *
|
---|
183 | * @returns The timestamp.
|
---|
184 | * @param pVM The cross context VM structure.
|
---|
185 | */
|
---|
186 | static uint64_t tmVirtualGetRawNonNormal(PVMCC pVM)
|
---|
187 | {
|
---|
188 | /*
|
---|
189 | * Recalculate the RTTimeNanoTS() value for the period where
|
---|
190 | * warp drive has been enabled.
|
---|
191 | */
|
---|
192 | uint64_t u64 = tmVirtualGetRawNanoTS(pVM);
|
---|
193 | u64 -= pVM->tm.s.u64VirtualWarpDriveStart;
|
---|
194 | u64 *= pVM->tm.s.u32VirtualWarpDrivePercentage;
|
---|
195 | u64 /= 100;
|
---|
196 | u64 += pVM->tm.s.u64VirtualWarpDriveStart;
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * Now we apply the virtual time offset.
|
---|
200 | * (Which is the negated tmVirtualGetRawNanoTS() value for when the virtual
|
---|
201 | * machine started if it had been running continuously without any suspends.)
|
---|
202 | */
|
---|
203 | u64 -= pVM->tm.s.u64VirtualOffset;
|
---|
204 | return u64;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Get the raw virtual time.
|
---|
210 | *
|
---|
211 | * @returns The current time stamp.
|
---|
212 | * @param pVM The cross context VM structure.
|
---|
213 | */
|
---|
214 | DECLINLINE(uint64_t) tmVirtualGetRaw(PVMCC pVM)
|
---|
215 | {
|
---|
216 | if (RT_LIKELY(!pVM->tm.s.fVirtualWarpDrive))
|
---|
217 | return tmVirtualGetRawNanoTS(pVM) - pVM->tm.s.u64VirtualOffset;
|
---|
218 | return tmVirtualGetRawNonNormal(pVM);
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Inlined version of tmVirtualGetEx.
|
---|
224 | */
|
---|
225 | DECLINLINE(uint64_t) tmVirtualGet(PVMCC pVM, bool fCheckTimers)
|
---|
226 | {
|
---|
227 | uint64_t u64;
|
---|
228 | if (RT_LIKELY(pVM->tm.s.cVirtualTicking))
|
---|
229 | {
|
---|
230 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualGet);
|
---|
231 | u64 = tmVirtualGetRaw(pVM);
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * Use the chance to check for expired timers.
|
---|
235 | */
|
---|
236 | if (fCheckTimers)
|
---|
237 | {
|
---|
238 | PVMCPUCC pVCpuDst = VMCC_GET_CPU(pVM, pVM->tm.s.idTimerCpu);
|
---|
239 | if ( !VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)
|
---|
240 | && !pVM->tm.s.fRunningQueues
|
---|
241 | && ( pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire <= u64
|
---|
242 | || ( pVM->tm.s.fVirtualSyncTicking
|
---|
243 | && pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire <= u64 - pVM->tm.s.offVirtualSync
|
---|
244 | )
|
---|
245 | )
|
---|
246 | && !pVM->tm.s.fRunningQueues
|
---|
247 | )
|
---|
248 | {
|
---|
249 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualGetSetFF);
|
---|
250 | Log5(("TMAllVirtual(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)));
|
---|
251 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
252 | #ifdef IN_RING3
|
---|
253 | VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
|
---|
254 | #endif
|
---|
255 | }
|
---|
256 | }
|
---|
257 | }
|
---|
258 | else
|
---|
259 | u64 = pVM->tm.s.u64Virtual;
|
---|
260 | return u64;
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Gets the current TMCLOCK_VIRTUAL time
|
---|
266 | *
|
---|
267 | * @returns The timestamp.
|
---|
268 | * @param pVM The cross context VM structure.
|
---|
269 | *
|
---|
270 | * @remark While the flow of time will never go backwards, the speed of the
|
---|
271 | * progress varies due to inaccurate RTTimeNanoTS and TSC. The latter can be
|
---|
272 | * influenced by power saving (SpeedStep, PowerNow!), while the former
|
---|
273 | * makes use of TSC and kernel timers.
|
---|
274 | */
|
---|
275 | VMM_INT_DECL(uint64_t) TMVirtualGet(PVMCC pVM)
|
---|
276 | {
|
---|
277 | return tmVirtualGet(pVM, true /*fCheckTimers*/);
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Gets the current TMCLOCK_VIRTUAL time without checking
|
---|
283 | * timers or anything.
|
---|
284 | *
|
---|
285 | * Meaning, this has no side effect on FFs like TMVirtualGet may have.
|
---|
286 | *
|
---|
287 | * @returns The timestamp.
|
---|
288 | * @param pVM The cross context VM structure.
|
---|
289 | *
|
---|
290 | * @remarks See TMVirtualGet.
|
---|
291 | */
|
---|
292 | VMM_INT_DECL(uint64_t) TMVirtualGetNoCheck(PVMCC pVM)
|
---|
293 | {
|
---|
294 | return tmVirtualGet(pVM, false /*fCheckTimers*/);
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Converts the dead line interval from TMCLOCK_VIRTUAL to host nano seconds.
|
---|
300 | *
|
---|
301 | * @returns Host nano second count.
|
---|
302 | * @param pVM The cross context VM structure.
|
---|
303 | * @param cVirtTicksToDeadline The TMCLOCK_VIRTUAL interval.
|
---|
304 | */
|
---|
305 | DECLINLINE(uint64_t) tmVirtualVirtToNsDeadline(PVM pVM, uint64_t cVirtTicksToDeadline)
|
---|
306 | {
|
---|
307 | if (RT_UNLIKELY(pVM->tm.s.fVirtualWarpDrive))
|
---|
308 | return ASMMultU64ByU32DivByU32(cVirtTicksToDeadline, 100, pVM->tm.s.u32VirtualWarpDrivePercentage);
|
---|
309 | return cVirtTicksToDeadline;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * tmVirtualSyncGetLocked worker for handling catch-up when owning the lock.
|
---|
315 | *
|
---|
316 | * @returns The timestamp.
|
---|
317 | * @param pVM The cross context VM structure.
|
---|
318 | * @param u64 raw virtual time.
|
---|
319 | * @param off offVirtualSync.
|
---|
320 | * @param pcNsToDeadline Where to return the number of nano seconds to
|
---|
321 | * the next virtual sync timer deadline. Can be
|
---|
322 | * NULL.
|
---|
323 | */
|
---|
324 | DECLINLINE(uint64_t) tmVirtualSyncGetHandleCatchUpLocked(PVMCC pVM, uint64_t u64, uint64_t off, uint64_t *pcNsToDeadline)
|
---|
325 | {
|
---|
326 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetLocked);
|
---|
327 |
|
---|
328 | /*
|
---|
329 | * Don't make updates until we've check the timer queue.
|
---|
330 | */
|
---|
331 | bool fUpdatePrev = true;
|
---|
332 | bool fUpdateOff = true;
|
---|
333 | bool fStop = false;
|
---|
334 | const uint64_t u64Prev = pVM->tm.s.u64VirtualSyncCatchUpPrev;
|
---|
335 | uint64_t u64Delta = u64 - u64Prev;
|
---|
336 | if (RT_LIKELY(!(u64Delta >> 32)))
|
---|
337 | {
|
---|
338 | uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
|
---|
339 | if (off > u64Sub + pVM->tm.s.offVirtualSyncGivenUp)
|
---|
340 | {
|
---|
341 | off -= u64Sub;
|
---|
342 | Log4(("TM: %'RU64/-%'8RU64: sub %RU32 [vsghcul]\n", u64 - off, off - pVM->tm.s.offVirtualSyncGivenUp, u64Sub));
|
---|
343 | }
|
---|
344 | else
|
---|
345 | {
|
---|
346 | /* we've completely caught up. */
|
---|
347 | STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
|
---|
348 | off = pVM->tm.s.offVirtualSyncGivenUp;
|
---|
349 | fStop = true;
|
---|
350 | Log4(("TM: %'RU64/0: caught up [vsghcul]\n", u64));
|
---|
351 | }
|
---|
352 | }
|
---|
353 | else
|
---|
354 | {
|
---|
355 | /* More than 4 seconds since last time (or negative), ignore it. */
|
---|
356 | fUpdateOff = false;
|
---|
357 | fUpdatePrev = !(u64Delta & RT_BIT_64(63));
|
---|
358 | Log(("TMVirtualGetSync: u64Delta=%RX64\n", u64Delta));
|
---|
359 | }
|
---|
360 |
|
---|
361 | /*
|
---|
362 | * Complete the calculation of the current TMCLOCK_VIRTUAL_SYNC time. The current
|
---|
363 | * approach is to never pass the head timer. So, when we do stop the clock and
|
---|
364 | * set the timer pending flag.
|
---|
365 | */
|
---|
366 | u64 -= off;
|
---|
367 |
|
---|
368 | uint64_t u64Last = ASMAtomicUoReadU64(&pVM->tm.s.u64VirtualSync);
|
---|
369 | if (u64Last > u64)
|
---|
370 | {
|
---|
371 | u64 = u64Last + 1;
|
---|
372 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetAdjLast);
|
---|
373 | }
|
---|
374 |
|
---|
375 | uint64_t u64Expire = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
|
---|
376 | if (u64 < u64Expire)
|
---|
377 | {
|
---|
378 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64);
|
---|
379 | if (fUpdateOff)
|
---|
380 | ASMAtomicWriteU64(&pVM->tm.s.offVirtualSync, off);
|
---|
381 | if (fStop)
|
---|
382 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
|
---|
383 | if (fUpdatePrev)
|
---|
384 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev, u64);
|
---|
385 | if (pcNsToDeadline)
|
---|
386 | {
|
---|
387 | uint64_t cNsToDeadline = u64Expire - u64;
|
---|
388 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
389 | cNsToDeadline = ASMMultU64ByU32DivByU32(cNsToDeadline, 100,
|
---|
390 | pVM->tm.s.u32VirtualSyncCatchUpPercentage + 100);
|
---|
391 | *pcNsToDeadline = tmVirtualVirtToNsDeadline(pVM, cNsToDeadline);
|
---|
392 | }
|
---|
393 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
394 | }
|
---|
395 | else
|
---|
396 | {
|
---|
397 | u64 = u64Expire;
|
---|
398 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64);
|
---|
399 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
|
---|
400 |
|
---|
401 | VM_FF_SET(pVM, VM_FF_TM_VIRTUAL_SYNC);
|
---|
402 | PVMCPUCC pVCpuDst = VMCC_GET_CPU(pVM, pVM->tm.s.idTimerCpu);
|
---|
403 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
404 | Log5(("TMAllVirtual(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)));
|
---|
405 | Log4(("TM: %'RU64/-%'8RU64: exp tmr=>ff [vsghcul]\n", u64, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp));
|
---|
406 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
407 |
|
---|
408 | if (pcNsToDeadline)
|
---|
409 | *pcNsToDeadline = 0;
|
---|
410 | #ifdef IN_RING3
|
---|
411 | VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
|
---|
412 | #endif
|
---|
413 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetSetFF);
|
---|
414 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetExpired);
|
---|
415 | }
|
---|
416 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetLocked);
|
---|
417 |
|
---|
418 | Log6(("tmVirtualSyncGetHandleCatchUpLocked -> %'RU64\n", u64));
|
---|
419 | DBGFTRACE_U64_TAG(pVM, u64, "tmVirtualSyncGetHandleCatchUpLocked");
|
---|
420 | return u64;
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * tmVirtualSyncGetEx worker for when we get the lock.
|
---|
426 | *
|
---|
427 | * @returns timesamp.
|
---|
428 | * @param pVM The cross context VM structure.
|
---|
429 | * @param u64 The virtual clock timestamp.
|
---|
430 | * @param pcNsToDeadline Where to return the number of nano seconds to
|
---|
431 | * the next virtual sync timer deadline. Can be
|
---|
432 | * NULL.
|
---|
433 | */
|
---|
434 | DECLINLINE(uint64_t) tmVirtualSyncGetLocked(PVMCC pVM, uint64_t u64, uint64_t *pcNsToDeadline)
|
---|
435 | {
|
---|
436 | /*
|
---|
437 | * Not ticking?
|
---|
438 | */
|
---|
439 | if (!pVM->tm.s.fVirtualSyncTicking)
|
---|
440 | {
|
---|
441 | u64 = ASMAtomicUoReadU64(&pVM->tm.s.u64VirtualSync);
|
---|
442 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
443 | if (pcNsToDeadline)
|
---|
444 | *pcNsToDeadline = 0;
|
---|
445 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetLocked);
|
---|
446 | Log6(("tmVirtualSyncGetLocked -> %'RU64 [stopped]\n", u64));
|
---|
447 | DBGFTRACE_U64_TAG(pVM, u64, "tmVirtualSyncGetLocked-stopped");
|
---|
448 | return u64;
|
---|
449 | }
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * Handle catch up in a separate function.
|
---|
453 | */
|
---|
454 | uint64_t off = ASMAtomicUoReadU64(&pVM->tm.s.offVirtualSync);
|
---|
455 | if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
456 | return tmVirtualSyncGetHandleCatchUpLocked(pVM, u64, off, pcNsToDeadline);
|
---|
457 |
|
---|
458 | /*
|
---|
459 | * Complete the calculation of the current TMCLOCK_VIRTUAL_SYNC time. The current
|
---|
460 | * approach is to never pass the head timer. So, when we do stop the clock and
|
---|
461 | * set the timer pending flag.
|
---|
462 | */
|
---|
463 | u64 -= off;
|
---|
464 |
|
---|
465 | uint64_t u64Last = ASMAtomicUoReadU64(&pVM->tm.s.u64VirtualSync);
|
---|
466 | if (u64Last > u64)
|
---|
467 | {
|
---|
468 | u64 = u64Last + 1;
|
---|
469 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetAdjLast);
|
---|
470 | }
|
---|
471 |
|
---|
472 | uint64_t u64Expire = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
|
---|
473 | if (u64 < u64Expire)
|
---|
474 | {
|
---|
475 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64);
|
---|
476 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
477 | if (pcNsToDeadline)
|
---|
478 | *pcNsToDeadline = tmVirtualVirtToNsDeadline(pVM, u64Expire - u64);
|
---|
479 | }
|
---|
480 | else
|
---|
481 | {
|
---|
482 | u64 = u64Expire;
|
---|
483 | ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64);
|
---|
484 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
|
---|
485 |
|
---|
486 | VM_FF_SET(pVM, VM_FF_TM_VIRTUAL_SYNC);
|
---|
487 | PVMCPUCC pVCpuDst = VMCC_GET_CPU(pVM, pVM->tm.s.idTimerCpu);
|
---|
488 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
489 | Log5(("TMAllVirtual(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)));
|
---|
490 | Log4(("TM: %'RU64/-%'8RU64: exp tmr=>ff [vsgl]\n", u64, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp));
|
---|
491 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
492 |
|
---|
493 | #ifdef IN_RING3
|
---|
494 | VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
|
---|
495 | #endif
|
---|
496 | if (pcNsToDeadline)
|
---|
497 | *pcNsToDeadline = 0;
|
---|
498 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetSetFF);
|
---|
499 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetExpired);
|
---|
500 | }
|
---|
501 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetLocked);
|
---|
502 | Log6(("tmVirtualSyncGetLocked -> %'RU64\n", u64));
|
---|
503 | DBGFTRACE_U64_TAG(pVM, u64, "tmVirtualSyncGetLocked");
|
---|
504 | return u64;
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Gets the current TMCLOCK_VIRTUAL_SYNC time.
|
---|
510 | *
|
---|
511 | * @returns The timestamp.
|
---|
512 | * @param pVM The cross context VM structure.
|
---|
513 | * @param fCheckTimers Check timers or not
|
---|
514 | * @param pcNsToDeadline Where to return the number of nano seconds to
|
---|
515 | * the next virtual sync timer deadline. Can be
|
---|
516 | * NULL.
|
---|
517 | * @thread EMT.
|
---|
518 | */
|
---|
519 | DECLINLINE(uint64_t) tmVirtualSyncGetEx(PVMCC pVM, bool fCheckTimers, uint64_t *pcNsToDeadline)
|
---|
520 | {
|
---|
521 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGet);
|
---|
522 |
|
---|
523 | uint64_t u64;
|
---|
524 | if (!pVM->tm.s.fVirtualSyncTicking)
|
---|
525 | {
|
---|
526 | if (pcNsToDeadline)
|
---|
527 | *pcNsToDeadline = 0;
|
---|
528 | u64 = pVM->tm.s.u64VirtualSync;
|
---|
529 | DBGFTRACE_U64_TAG(pVM, u64, "tmVirtualSyncGetEx-stopped1");
|
---|
530 | return u64;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * Query the virtual clock and do the usual expired timer check.
|
---|
535 | */
|
---|
536 | Assert(pVM->tm.s.cVirtualTicking);
|
---|
537 | u64 = tmVirtualGetRaw(pVM);
|
---|
538 | if (fCheckTimers)
|
---|
539 | {
|
---|
540 | PVMCPUCC pVCpuDst = VMCC_GET_CPU(pVM, pVM->tm.s.idTimerCpu);
|
---|
541 | if ( !VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)
|
---|
542 | && pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire <= u64)
|
---|
543 | {
|
---|
544 | Log5(("TMAllVirtual(%u): FF: 0 -> 1\n", __LINE__));
|
---|
545 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
546 | #ifdef IN_RING3
|
---|
547 | VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM /** @todo |VMNOTIFYFF_FLAGS_POKE*/);
|
---|
548 | #endif
|
---|
549 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetSetFF);
|
---|
550 | }
|
---|
551 | }
|
---|
552 |
|
---|
553 | /*
|
---|
554 | * If we can get the lock, get it. The result is much more reliable.
|
---|
555 | *
|
---|
556 | * Note! This is where all clock source devices branch off because they
|
---|
557 | * will be owning the lock already. The 'else' is taken by code
|
---|
558 | * which is less picky or hasn't been adjusted yet
|
---|
559 | */
|
---|
560 | if (PDMCritSectTryEnter(&pVM->tm.s.VirtualSyncLock) == VINF_SUCCESS)
|
---|
561 | return tmVirtualSyncGetLocked(pVM, u64, pcNsToDeadline);
|
---|
562 |
|
---|
563 | /*
|
---|
564 | * When the clock is ticking, not doing catch ups and not running into an
|
---|
565 | * expired time, we can get away without locking. Try this first.
|
---|
566 | */
|
---|
567 | uint64_t off;
|
---|
568 | if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
|
---|
569 | {
|
---|
570 | if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
571 | {
|
---|
572 | off = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
|
---|
573 | if (RT_LIKELY( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
|
---|
574 | && !ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
|
---|
575 | && off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)))
|
---|
576 | {
|
---|
577 | off = u64 - off;
|
---|
578 | uint64_t const u64Expire = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
|
---|
579 | if (off < u64Expire)
|
---|
580 | {
|
---|
581 | if (pcNsToDeadline)
|
---|
582 | *pcNsToDeadline = tmVirtualVirtToNsDeadline(pVM, u64Expire - off);
|
---|
583 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetLockless);
|
---|
584 | Log6(("tmVirtualSyncGetEx -> %'RU64 [lockless]\n", off));
|
---|
585 | DBGFTRACE_U64_TAG(pVM, off, "tmVirtualSyncGetEx-lockless");
|
---|
586 | return off;
|
---|
587 | }
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 | else
|
---|
592 | {
|
---|
593 | off = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSync);
|
---|
594 | if (RT_LIKELY(!ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking)))
|
---|
595 | {
|
---|
596 | if (pcNsToDeadline)
|
---|
597 | *pcNsToDeadline = 0;
|
---|
598 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetLockless);
|
---|
599 | Log6(("tmVirtualSyncGetEx -> %'RU64 [lockless/stopped]\n", off));
|
---|
600 | DBGFTRACE_U64_TAG(pVM, off, "tmVirtualSyncGetEx-stopped2");
|
---|
601 | return off;
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * Read the offset and adjust if we're playing catch-up.
|
---|
607 | *
|
---|
608 | * The catch-up adjusting work by us decrementing the offset by a percentage of
|
---|
609 | * the time elapsed since the previous TMVirtualGetSync call.
|
---|
610 | *
|
---|
611 | * It's possible to get a very long or even negative interval between two read
|
---|
612 | * for the following reasons:
|
---|
613 | * - Someone might have suspended the process execution, frequently the case when
|
---|
614 | * debugging the process.
|
---|
615 | * - We might be on a different CPU which TSC isn't quite in sync with the
|
---|
616 | * other CPUs in the system.
|
---|
617 | * - Another thread is racing us and we might have been preempted while inside
|
---|
618 | * this function.
|
---|
619 | *
|
---|
620 | * Assuming nano second virtual time, we can simply ignore any intervals which has
|
---|
621 | * any of the upper 32 bits set.
|
---|
622 | */
|
---|
623 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
624 | int cOuterTries = 42;
|
---|
625 | for (;; cOuterTries--)
|
---|
626 | {
|
---|
627 | /* Try grab the lock, things get simpler when owning the lock. */
|
---|
628 | int rcLock = PDMCritSectTryEnter(&pVM->tm.s.VirtualSyncLock);
|
---|
629 | if (RT_SUCCESS_NP(rcLock))
|
---|
630 | return tmVirtualSyncGetLocked(pVM, u64, pcNsToDeadline);
|
---|
631 |
|
---|
632 | /* Re-check the ticking flag. */
|
---|
633 | if (!ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
|
---|
634 | {
|
---|
635 | off = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSync);
|
---|
636 | if ( ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking)
|
---|
637 | && cOuterTries > 0)
|
---|
638 | continue;
|
---|
639 | if (pcNsToDeadline)
|
---|
640 | *pcNsToDeadline = 0;
|
---|
641 | Log6(("tmVirtualSyncGetEx -> %'RU64 [stopped]\n", off));
|
---|
642 | DBGFTRACE_U64_TAG(pVM, off, "tmVirtualSyncGetEx-stopped3");
|
---|
643 | return off;
|
---|
644 | }
|
---|
645 |
|
---|
646 | off = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
|
---|
647 | if (ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
648 | {
|
---|
649 | /* No changes allowed, try get a consistent set of parameters. */
|
---|
650 | uint64_t const u64Prev = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev);
|
---|
651 | uint64_t const offGivenUp = ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp);
|
---|
652 | uint32_t const u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage);
|
---|
653 | if ( ( u64Prev == ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev)
|
---|
654 | && offGivenUp == ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp)
|
---|
655 | && u32Pct == ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage)
|
---|
656 | && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
657 | || cOuterTries <= 0)
|
---|
658 | {
|
---|
659 | uint64_t u64Delta = u64 - u64Prev;
|
---|
660 | if (RT_LIKELY(!(u64Delta >> 32)))
|
---|
661 | {
|
---|
662 | uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, u32Pct, 100);
|
---|
663 | if (off > u64Sub + offGivenUp)
|
---|
664 | {
|
---|
665 | off -= u64Sub;
|
---|
666 | Log4(("TM: %'RU64/-%'8RU64: sub %RU32 [NoLock]\n", u64 - off, pVM->tm.s.offVirtualSync - offGivenUp, u64Sub));
|
---|
667 | }
|
---|
668 | else
|
---|
669 | {
|
---|
670 | /* we've completely caught up. */
|
---|
671 | STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
|
---|
672 | off = offGivenUp;
|
---|
673 | Log4(("TM: %'RU64/0: caught up [NoLock]\n", u64));
|
---|
674 | }
|
---|
675 | }
|
---|
676 | else
|
---|
677 | /* More than 4 seconds since last time (or negative), ignore it. */
|
---|
678 | Log(("TMVirtualGetSync: u64Delta=%RX64 (NoLock)\n", u64Delta));
|
---|
679 |
|
---|
680 | /* Check that we're still running and in catch up. */
|
---|
681 | if ( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
|
---|
682 | && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
683 | break;
|
---|
684 | if (cOuterTries <= 0)
|
---|
685 | break; /* enough */
|
---|
686 | }
|
---|
687 | }
|
---|
688 | else if ( off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
|
---|
689 | && !ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
690 | break; /* Got an consistent offset */
|
---|
691 | else if (cOuterTries <= 0)
|
---|
692 | break; /* enough */
|
---|
693 | }
|
---|
694 | if (cOuterTries <= 0)
|
---|
695 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetELoop);
|
---|
696 |
|
---|
697 | /*
|
---|
698 | * Complete the calculation of the current TMCLOCK_VIRTUAL_SYNC time. The current
|
---|
699 | * approach is to never pass the head timer. So, when we do stop the clock and
|
---|
700 | * set the timer pending flag.
|
---|
701 | */
|
---|
702 | u64 -= off;
|
---|
703 | /** @todo u64VirtualSyncLast */
|
---|
704 | uint64_t u64Expire = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
|
---|
705 | if (u64 >= u64Expire)
|
---|
706 | {
|
---|
707 | PVMCPUCC pVCpuDst = VMCC_GET_CPU(pVM, pVM->tm.s.idTimerCpu);
|
---|
708 | if (!VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
|
---|
709 | {
|
---|
710 | Log5(("TMAllVirtual(%u): FF: %d -> 1 (NoLock)\n", __LINE__, VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER)));
|
---|
711 | VM_FF_SET(pVM, VM_FF_TM_VIRTUAL_SYNC); /* Hmm? */
|
---|
712 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
713 | #ifdef IN_RING3
|
---|
714 | VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
|
---|
715 | #endif
|
---|
716 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetSetFF);
|
---|
717 | Log4(("TM: %'RU64/-%'8RU64: exp tmr=>ff [NoLock]\n", u64, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp));
|
---|
718 | }
|
---|
719 | else
|
---|
720 | Log4(("TM: %'RU64/-%'8RU64: exp tmr [NoLock]\n", u64, pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp));
|
---|
721 | if (pcNsToDeadline)
|
---|
722 | *pcNsToDeadline = 0;
|
---|
723 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetExpired);
|
---|
724 | }
|
---|
725 | else if (pcNsToDeadline)
|
---|
726 | {
|
---|
727 | uint64_t cNsToDeadline = u64Expire - u64;
|
---|
728 | if (ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
729 | cNsToDeadline = ASMMultU64ByU32DivByU32(cNsToDeadline, 100,
|
---|
730 | ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage) + 100);
|
---|
731 | *pcNsToDeadline = tmVirtualVirtToNsDeadline(pVM, cNsToDeadline);
|
---|
732 | }
|
---|
733 |
|
---|
734 | Log6(("tmVirtualSyncGetEx -> %'RU64\n", u64));
|
---|
735 | DBGFTRACE_U64_TAG(pVM, u64, "tmVirtualSyncGetEx-nolock");
|
---|
736 | return u64;
|
---|
737 | }
|
---|
738 |
|
---|
739 |
|
---|
740 | /**
|
---|
741 | * Gets the current TMCLOCK_VIRTUAL_SYNC time.
|
---|
742 | *
|
---|
743 | * @returns The timestamp.
|
---|
744 | * @param pVM The cross context VM structure.
|
---|
745 | * @thread EMT.
|
---|
746 | * @remarks May set the timer and virtual sync FFs.
|
---|
747 | */
|
---|
748 | VMM_INT_DECL(uint64_t) TMVirtualSyncGet(PVMCC pVM)
|
---|
749 | {
|
---|
750 | return tmVirtualSyncGetEx(pVM, true /*fCheckTimers*/, NULL /*pcNsToDeadline*/);
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Gets the current TMCLOCK_VIRTUAL_SYNC time without checking timers running on
|
---|
756 | * TMCLOCK_VIRTUAL.
|
---|
757 | *
|
---|
758 | * @returns The timestamp.
|
---|
759 | * @param pVM The cross context VM structure.
|
---|
760 | * @thread EMT.
|
---|
761 | * @remarks May set the timer and virtual sync FFs.
|
---|
762 | */
|
---|
763 | VMM_INT_DECL(uint64_t) TMVirtualSyncGetNoCheck(PVMCC pVM)
|
---|
764 | {
|
---|
765 | return tmVirtualSyncGetEx(pVM, false /*fCheckTimers*/, NULL /*pcNsToDeadline*/);
|
---|
766 | }
|
---|
767 |
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Gets the current TMCLOCK_VIRTUAL_SYNC time.
|
---|
771 | *
|
---|
772 | * @returns The timestamp.
|
---|
773 | * @param pVM The cross context VM structure.
|
---|
774 | * @param fCheckTimers Check timers on the virtual clock or not.
|
---|
775 | * @thread EMT.
|
---|
776 | * @remarks May set the timer and virtual sync FFs.
|
---|
777 | */
|
---|
778 | VMM_INT_DECL(uint64_t) TMVirtualSyncGetEx(PVMCC pVM, bool fCheckTimers)
|
---|
779 | {
|
---|
780 | return tmVirtualSyncGetEx(pVM, fCheckTimers, NULL /*pcNsToDeadline*/);
|
---|
781 | }
|
---|
782 |
|
---|
783 |
|
---|
784 | /**
|
---|
785 | * Gets the current TMCLOCK_VIRTUAL_SYNC time and ticks to the next deadline
|
---|
786 | * without checking timers running on TMCLOCK_VIRTUAL.
|
---|
787 | *
|
---|
788 | * @returns The timestamp.
|
---|
789 | * @param pVM The cross context VM structure.
|
---|
790 | * @param pcNsToDeadline Where to return the number of nano seconds to
|
---|
791 | * the next virtual sync timer deadline.
|
---|
792 | * @thread EMT.
|
---|
793 | * @remarks May set the timer and virtual sync FFs.
|
---|
794 | */
|
---|
795 | VMM_INT_DECL(uint64_t) TMVirtualSyncGetWithDeadlineNoCheck(PVMCC pVM, uint64_t *pcNsToDeadline)
|
---|
796 | {
|
---|
797 | uint64_t cNsToDeadlineTmp; /* try convince the compiler to skip the if tests. */
|
---|
798 | uint64_t u64Now = tmVirtualSyncGetEx(pVM, false /*fCheckTimers*/, &cNsToDeadlineTmp);
|
---|
799 | *pcNsToDeadline = cNsToDeadlineTmp;
|
---|
800 | return u64Now;
|
---|
801 | }
|
---|
802 |
|
---|
803 |
|
---|
804 | /**
|
---|
805 | * Gets the number of nano seconds to the next virtual sync deadline.
|
---|
806 | *
|
---|
807 | * @returns The number of TMCLOCK_VIRTUAL ticks.
|
---|
808 | * @param pVM The cross context VM structure.
|
---|
809 | * @thread EMT.
|
---|
810 | * @remarks May set the timer and virtual sync FFs.
|
---|
811 | */
|
---|
812 | VMMDECL(uint64_t) TMVirtualSyncGetNsToDeadline(PVMCC pVM)
|
---|
813 | {
|
---|
814 | uint64_t cNsToDeadline;
|
---|
815 | tmVirtualSyncGetEx(pVM, false /*fCheckTimers*/, &cNsToDeadline);
|
---|
816 | return cNsToDeadline;
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * Gets the current lag of the synchronous virtual clock (relative to the virtual clock).
|
---|
822 | *
|
---|
823 | * @return The current lag.
|
---|
824 | * @param pVM The cross context VM structure.
|
---|
825 | */
|
---|
826 | VMM_INT_DECL(uint64_t) TMVirtualSyncGetLag(PVMCC pVM)
|
---|
827 | {
|
---|
828 | return pVM->tm.s.offVirtualSync - pVM->tm.s.offVirtualSyncGivenUp;
|
---|
829 | }
|
---|
830 |
|
---|
831 |
|
---|
832 | /**
|
---|
833 | * Get the current catch-up percent.
|
---|
834 | *
|
---|
835 | * @return The current catch0up percent. 0 means running at the same speed as the virtual clock.
|
---|
836 | * @param pVM The cross context VM structure.
|
---|
837 | */
|
---|
838 | VMM_INT_DECL(uint32_t) TMVirtualSyncGetCatchUpPct(PVMCC pVM)
|
---|
839 | {
|
---|
840 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
841 | return pVM->tm.s.u32VirtualSyncCatchUpPercentage;
|
---|
842 | return 0;
|
---|
843 | }
|
---|
844 |
|
---|
845 |
|
---|
846 | /**
|
---|
847 | * Gets the current TMCLOCK_VIRTUAL frequency.
|
---|
848 | *
|
---|
849 | * @returns The frequency.
|
---|
850 | * @param pVM The cross context VM structure.
|
---|
851 | */
|
---|
852 | VMM_INT_DECL(uint64_t) TMVirtualGetFreq(PVM pVM)
|
---|
853 | {
|
---|
854 | NOREF(pVM);
|
---|
855 | return TMCLOCK_FREQ_VIRTUAL;
|
---|
856 | }
|
---|
857 |
|
---|
858 |
|
---|
859 | /**
|
---|
860 | * Worker for TMR3PauseClocks.
|
---|
861 | *
|
---|
862 | * @returns VINF_SUCCESS or VERR_TM_VIRTUAL_TICKING_IPE (asserted).
|
---|
863 | * @param pVM The cross context VM structure.
|
---|
864 | */
|
---|
865 | int tmVirtualPauseLocked(PVMCC pVM)
|
---|
866 | {
|
---|
867 | uint32_t c = ASMAtomicDecU32(&pVM->tm.s.cVirtualTicking);
|
---|
868 | AssertMsgReturn(c < pVM->cCpus, ("%u vs %u\n", c, pVM->cCpus), VERR_TM_VIRTUAL_TICKING_IPE);
|
---|
869 | if (c == 0)
|
---|
870 | {
|
---|
871 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualPause);
|
---|
872 | pVM->tm.s.u64Virtual = tmVirtualGetRaw(pVM);
|
---|
873 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
|
---|
874 | }
|
---|
875 | return VINF_SUCCESS;
|
---|
876 | }
|
---|
877 |
|
---|
878 |
|
---|
879 | /**
|
---|
880 | * Worker for TMR3ResumeClocks.
|
---|
881 | *
|
---|
882 | * @returns VINF_SUCCESS or VERR_TM_VIRTUAL_TICKING_IPE (asserted).
|
---|
883 | * @param pVM The cross context VM structure.
|
---|
884 | */
|
---|
885 | int tmVirtualResumeLocked(PVMCC pVM)
|
---|
886 | {
|
---|
887 | uint32_t c = ASMAtomicIncU32(&pVM->tm.s.cVirtualTicking);
|
---|
888 | AssertMsgReturn(c <= pVM->cCpus, ("%u vs %u\n", c, pVM->cCpus), VERR_TM_VIRTUAL_TICKING_IPE);
|
---|
889 | if (c == 1)
|
---|
890 | {
|
---|
891 | STAM_COUNTER_INC(&pVM->tm.s.StatVirtualResume);
|
---|
892 | pVM->tm.s.u64VirtualRawPrev = 0;
|
---|
893 | pVM->tm.s.u64VirtualWarpDriveStart = tmVirtualGetRawNanoTS(pVM);
|
---|
894 | pVM->tm.s.u64VirtualOffset = pVM->tm.s.u64VirtualWarpDriveStart - pVM->tm.s.u64Virtual;
|
---|
895 | ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, true);
|
---|
896 | }
|
---|
897 | return VINF_SUCCESS;
|
---|
898 | }
|
---|
899 |
|
---|
900 |
|
---|
901 | /**
|
---|
902 | * Converts from virtual ticks to nanoseconds.
|
---|
903 | *
|
---|
904 | * @returns nanoseconds.
|
---|
905 | * @param pVM The cross context VM structure.
|
---|
906 | * @param u64VirtualTicks The virtual ticks to convert.
|
---|
907 | * @remark There could be rounding errors here. We just do a simple integer divide
|
---|
908 | * without any adjustments.
|
---|
909 | */
|
---|
910 | VMM_INT_DECL(uint64_t) TMVirtualToNano(PVM pVM, uint64_t u64VirtualTicks)
|
---|
911 | {
|
---|
912 | NOREF(pVM);
|
---|
913 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
914 | return u64VirtualTicks;
|
---|
915 | }
|
---|
916 |
|
---|
917 |
|
---|
918 | /**
|
---|
919 | * Converts from virtual ticks to microseconds.
|
---|
920 | *
|
---|
921 | * @returns microseconds.
|
---|
922 | * @param pVM The cross context VM structure.
|
---|
923 | * @param u64VirtualTicks The virtual ticks to convert.
|
---|
924 | * @remark There could be rounding errors here. We just do a simple integer divide
|
---|
925 | * without any adjustments.
|
---|
926 | */
|
---|
927 | VMM_INT_DECL(uint64_t) TMVirtualToMicro(PVM pVM, uint64_t u64VirtualTicks)
|
---|
928 | {
|
---|
929 | NOREF(pVM);
|
---|
930 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
931 | return u64VirtualTicks / 1000;
|
---|
932 | }
|
---|
933 |
|
---|
934 |
|
---|
935 | /**
|
---|
936 | * Converts from virtual ticks to milliseconds.
|
---|
937 | *
|
---|
938 | * @returns milliseconds.
|
---|
939 | * @param pVM The cross context VM structure.
|
---|
940 | * @param u64VirtualTicks The virtual ticks to convert.
|
---|
941 | * @remark There could be rounding errors here. We just do a simple integer divide
|
---|
942 | * without any adjustments.
|
---|
943 | */
|
---|
944 | VMM_INT_DECL(uint64_t) TMVirtualToMilli(PVM pVM, uint64_t u64VirtualTicks)
|
---|
945 | {
|
---|
946 | NOREF(pVM);
|
---|
947 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
948 | return u64VirtualTicks / 1000000;
|
---|
949 | }
|
---|
950 |
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * Converts from nanoseconds to virtual ticks.
|
---|
954 | *
|
---|
955 | * @returns virtual ticks.
|
---|
956 | * @param pVM The cross context VM structure.
|
---|
957 | * @param u64NanoTS The nanosecond value ticks to convert.
|
---|
958 | * @remark There could be rounding and overflow errors here.
|
---|
959 | */
|
---|
960 | VMM_INT_DECL(uint64_t) TMVirtualFromNano(PVM pVM, uint64_t u64NanoTS)
|
---|
961 | {
|
---|
962 | NOREF(pVM);
|
---|
963 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
964 | return u64NanoTS;
|
---|
965 | }
|
---|
966 |
|
---|
967 |
|
---|
968 | /**
|
---|
969 | * Converts from microseconds to virtual ticks.
|
---|
970 | *
|
---|
971 | * @returns virtual ticks.
|
---|
972 | * @param pVM The cross context VM structure.
|
---|
973 | * @param u64MicroTS The microsecond value ticks to convert.
|
---|
974 | * @remark There could be rounding and overflow errors here.
|
---|
975 | */
|
---|
976 | VMM_INT_DECL(uint64_t) TMVirtualFromMicro(PVM pVM, uint64_t u64MicroTS)
|
---|
977 | {
|
---|
978 | NOREF(pVM);
|
---|
979 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
980 | return u64MicroTS * 1000;
|
---|
981 | }
|
---|
982 |
|
---|
983 |
|
---|
984 | /**
|
---|
985 | * Converts from milliseconds to virtual ticks.
|
---|
986 | *
|
---|
987 | * @returns virtual ticks.
|
---|
988 | * @param pVM The cross context VM structure.
|
---|
989 | * @param u64MilliTS The millisecond value ticks to convert.
|
---|
990 | * @remark There could be rounding and overflow errors here.
|
---|
991 | */
|
---|
992 | VMM_INT_DECL(uint64_t) TMVirtualFromMilli(PVM pVM, uint64_t u64MilliTS)
|
---|
993 | {
|
---|
994 | NOREF(pVM);
|
---|
995 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
996 | return u64MilliTS * 1000000;
|
---|
997 | }
|
---|
998 |
|
---|