1 | /* $Id: TMAll.cpp 63560 2016-08-16 14:01:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TM - Timeout Manager, all contexts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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/mm.h>
|
---|
25 | #include <VBox/vmm/dbgftrace.h>
|
---|
26 | #ifdef IN_RING3
|
---|
27 | # ifdef VBOX_WITH_REM
|
---|
28 | # include <VBox/vmm/rem.h>
|
---|
29 | # endif
|
---|
30 | #endif
|
---|
31 | #include "TMInternal.h"
|
---|
32 | #include <VBox/vmm/vm.h>
|
---|
33 |
|
---|
34 | #include <VBox/param.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #include <VBox/sup.h>
|
---|
38 | #include <iprt/time.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/asm.h>
|
---|
41 | #include <iprt/asm-math.h>
|
---|
42 | #ifdef IN_RING3
|
---|
43 | # include <iprt/thread.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #include "TMInline.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Defined Constants And Macros *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | /** @def TMTIMER_ASSERT_CRITSECT
|
---|
53 | * Checks that the caller owns the critical section if one is associated with
|
---|
54 | * the timer. */
|
---|
55 | #ifdef VBOX_STRICT
|
---|
56 | # define TMTIMER_ASSERT_CRITSECT(pTimer) \
|
---|
57 | do { \
|
---|
58 | if ((pTimer)->pCritSect) \
|
---|
59 | { \
|
---|
60 | VMSTATE enmState; \
|
---|
61 | PPDMCRITSECT pCritSect = (PPDMCRITSECT)MMHyperR3ToCC((pTimer)->CTX_SUFF(pVM), (pTimer)->pCritSect); \
|
---|
62 | AssertMsg( pCritSect \
|
---|
63 | && ( PDMCritSectIsOwner(pCritSect) \
|
---|
64 | || (enmState = (pTimer)->CTX_SUFF(pVM)->enmVMState) == VMSTATE_CREATING \
|
---|
65 | || enmState == VMSTATE_RESETTING \
|
---|
66 | || enmState == VMSTATE_RESETTING_LS ),\
|
---|
67 | ("pTimer=%p (%s) pCritSect=%p (%s)\n", pTimer, R3STRING(pTimer->pszDesc), \
|
---|
68 | (pTimer)->pCritSect, R3STRING(PDMR3CritSectName((pTimer)->pCritSect)) )); \
|
---|
69 | } \
|
---|
70 | } while (0)
|
---|
71 | #else
|
---|
72 | # define TMTIMER_ASSERT_CRITSECT(pTimer) do { } while (0)
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | /** @def TMTIMER_ASSERT_SYNC_CRITSECT_ORDER
|
---|
76 | * Checks for lock order trouble between the timer critsect and the critical
|
---|
77 | * section critsect. The virtual sync critsect must always be entered before
|
---|
78 | * the one associated with the timer (see TMR3TimerQueuesDo). It is OK if there
|
---|
79 | * isn't any critical section associated with the timer or if the calling thread
|
---|
80 | * doesn't own it, ASSUMING of course that the thread using this macro is going
|
---|
81 | * to enter the virtual sync critical section anyway.
|
---|
82 | *
|
---|
83 | * @remarks This is a sligtly relaxed timer locking attitude compared to
|
---|
84 | * TMTIMER_ASSERT_CRITSECT, however, the calling device/whatever code
|
---|
85 | * should know what it's doing if it's stopping or starting a timer
|
---|
86 | * without taking the device lock.
|
---|
87 | */
|
---|
88 | #ifdef VBOX_STRICT
|
---|
89 | # define TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer) \
|
---|
90 | do { \
|
---|
91 | if ((pTimer)->pCritSect) \
|
---|
92 | { \
|
---|
93 | VMSTATE enmState; \
|
---|
94 | PPDMCRITSECT pCritSect = (PPDMCRITSECT)MMHyperR3ToCC(pVM, (pTimer)->pCritSect); \
|
---|
95 | AssertMsg( pCritSect \
|
---|
96 | && ( !PDMCritSectIsOwner(pCritSect) \
|
---|
97 | || PDMCritSectIsOwner(&pVM->tm.s.VirtualSyncLock) \
|
---|
98 | || (enmState = (pVM)->enmVMState) == VMSTATE_CREATING \
|
---|
99 | || enmState == VMSTATE_RESETTING \
|
---|
100 | || enmState == VMSTATE_RESETTING_LS ),\
|
---|
101 | ("pTimer=%p (%s) pCritSect=%p (%s)\n", pTimer, R3STRING(pTimer->pszDesc), \
|
---|
102 | (pTimer)->pCritSect, R3STRING(PDMR3CritSectName((pTimer)->pCritSect)) )); \
|
---|
103 | } \
|
---|
104 | } while (0)
|
---|
105 | #else
|
---|
106 | # define TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer) do { } while (0)
|
---|
107 | #endif
|
---|
108 |
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Notification that execution is about to start.
|
---|
112 | *
|
---|
113 | * This call must always be paired with a TMNotifyEndOfExecution call.
|
---|
114 | *
|
---|
115 | * The function may, depending on the configuration, resume the TSC and future
|
---|
116 | * clocks that only ticks when we're executing guest code.
|
---|
117 | *
|
---|
118 | * @param pVCpu The cross context virtual CPU structure.
|
---|
119 | */
|
---|
120 | VMMDECL(void) TMNotifyStartOfExecution(PVMCPU pVCpu)
|
---|
121 | {
|
---|
122 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
123 |
|
---|
124 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
125 | pVCpu->tm.s.u64NsTsStartExecuting = RTTimeNanoTS();
|
---|
126 | #endif
|
---|
127 | if (pVM->tm.s.fTSCTiedToExecution)
|
---|
128 | tmCpuTickResume(pVM, pVCpu);
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Notification that execution has ended.
|
---|
134 | *
|
---|
135 | * This call must always be paired with a TMNotifyStartOfExecution call.
|
---|
136 | *
|
---|
137 | * The function may, depending on the configuration, suspend the TSC and future
|
---|
138 | * clocks that only ticks when we're executing guest code.
|
---|
139 | *
|
---|
140 | * @param pVCpu The cross context virtual CPU structure.
|
---|
141 | */
|
---|
142 | VMMDECL(void) TMNotifyEndOfExecution(PVMCPU pVCpu)
|
---|
143 | {
|
---|
144 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
145 |
|
---|
146 | if (pVM->tm.s.fTSCTiedToExecution)
|
---|
147 | tmCpuTickPause(pVCpu);
|
---|
148 |
|
---|
149 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
150 | uint64_t const u64NsTs = RTTimeNanoTS();
|
---|
151 | uint64_t const cNsTotalNew = u64NsTs - pVCpu->tm.s.u64NsTsStartTotal;
|
---|
152 | uint64_t const cNsExecutingDelta = u64NsTs - pVCpu->tm.s.u64NsTsStartExecuting;
|
---|
153 | uint64_t const cNsExecutingNew = pVCpu->tm.s.cNsExecuting + cNsExecutingDelta;
|
---|
154 | uint64_t const cNsOtherNew = cNsTotalNew - cNsExecutingNew - pVCpu->tm.s.cNsHalted;
|
---|
155 |
|
---|
156 | # if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
|
---|
157 | STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecuting, cNsExecutingDelta);
|
---|
158 | if (cNsExecutingDelta < 5000)
|
---|
159 | STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecTiny, cNsExecutingDelta);
|
---|
160 | else if (cNsExecutingDelta < 50000)
|
---|
161 | STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecShort, cNsExecutingDelta);
|
---|
162 | else
|
---|
163 | STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsExecLong, cNsExecutingDelta);
|
---|
164 | STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsTotal, cNsTotalNew - pVCpu->tm.s.cNsTotal);
|
---|
165 | int64_t const cNsOtherNewDelta = cNsOtherNew - pVCpu->tm.s.cNsOther;
|
---|
166 | if (cNsOtherNewDelta > 0)
|
---|
167 | STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsOther, cNsOtherNewDelta); /* (the period before execution) */
|
---|
168 | # endif
|
---|
169 |
|
---|
170 | uint32_t uGen = ASMAtomicIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
|
---|
171 | pVCpu->tm.s.cNsExecuting = cNsExecutingNew;
|
---|
172 | pVCpu->tm.s.cNsTotal = cNsTotalNew;
|
---|
173 | pVCpu->tm.s.cNsOther = cNsOtherNew;
|
---|
174 | pVCpu->tm.s.cPeriodsExecuting++;
|
---|
175 | ASMAtomicWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
|
---|
176 | #endif
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Notification that the cpu is entering the halt state
|
---|
182 | *
|
---|
183 | * This call must always be paired with a TMNotifyEndOfExecution call.
|
---|
184 | *
|
---|
185 | * The function may, depending on the configuration, resume the TSC and future
|
---|
186 | * clocks that only ticks when we're halted.
|
---|
187 | *
|
---|
188 | * @param pVCpu The cross context virtual CPU structure.
|
---|
189 | */
|
---|
190 | VMM_INT_DECL(void) TMNotifyStartOfHalt(PVMCPU pVCpu)
|
---|
191 | {
|
---|
192 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
193 |
|
---|
194 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
195 | pVCpu->tm.s.u64NsTsStartHalting = RTTimeNanoTS();
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | if ( pVM->tm.s.fTSCTiedToExecution
|
---|
199 | && !pVM->tm.s.fTSCNotTiedToHalt)
|
---|
200 | tmCpuTickResume(pVM, pVCpu);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Notification that the cpu is leaving the halt state
|
---|
206 | *
|
---|
207 | * This call must always be paired with a TMNotifyStartOfHalt call.
|
---|
208 | *
|
---|
209 | * The function may, depending on the configuration, suspend the TSC and future
|
---|
210 | * clocks that only ticks when we're halted.
|
---|
211 | *
|
---|
212 | * @param pVCpu The cross context virtual CPU structure.
|
---|
213 | */
|
---|
214 | VMM_INT_DECL(void) TMNotifyEndOfHalt(PVMCPU pVCpu)
|
---|
215 | {
|
---|
216 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
217 |
|
---|
218 | if ( pVM->tm.s.fTSCTiedToExecution
|
---|
219 | && !pVM->tm.s.fTSCNotTiedToHalt)
|
---|
220 | tmCpuTickPause(pVCpu);
|
---|
221 |
|
---|
222 | #ifndef VBOX_WITHOUT_NS_ACCOUNTING
|
---|
223 | uint64_t const u64NsTs = RTTimeNanoTS();
|
---|
224 | uint64_t const cNsTotalNew = u64NsTs - pVCpu->tm.s.u64NsTsStartTotal;
|
---|
225 | uint64_t const cNsHaltedDelta = u64NsTs - pVCpu->tm.s.u64NsTsStartHalting;
|
---|
226 | uint64_t const cNsHaltedNew = pVCpu->tm.s.cNsHalted + cNsHaltedDelta;
|
---|
227 | uint64_t const cNsOtherNew = cNsTotalNew - pVCpu->tm.s.cNsExecuting - cNsHaltedNew;
|
---|
228 |
|
---|
229 | # if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
|
---|
230 | STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsHalted, cNsHaltedDelta);
|
---|
231 | STAM_REL_COUNTER_ADD(&pVCpu->tm.s.StatNsTotal, cNsTotalNew - pVCpu->tm.s.cNsTotal);
|
---|
232 | int64_t const cNsOtherNewDelta = cNsOtherNew - pVCpu->tm.s.cNsOther;
|
---|
233 | if (cNsOtherNewDelta > 0)
|
---|
234 | STAM_REL_PROFILE_ADD_PERIOD(&pVCpu->tm.s.StatNsOther, cNsOtherNewDelta); /* (the period before halting) */
|
---|
235 | # endif
|
---|
236 |
|
---|
237 | uint32_t uGen = ASMAtomicIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
|
---|
238 | pVCpu->tm.s.cNsHalted = cNsHaltedNew;
|
---|
239 | pVCpu->tm.s.cNsTotal = cNsTotalNew;
|
---|
240 | pVCpu->tm.s.cNsOther = cNsOtherNew;
|
---|
241 | pVCpu->tm.s.cPeriodsHalted++;
|
---|
242 | ASMAtomicWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
|
---|
243 | #endif
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Raise the timer force action flag and notify the dedicated timer EMT.
|
---|
249 | *
|
---|
250 | * @param pVM The cross context VM structure.
|
---|
251 | */
|
---|
252 | DECLINLINE(void) tmScheduleNotify(PVM pVM)
|
---|
253 | {
|
---|
254 | PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
|
---|
255 | if (!VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
|
---|
256 | {
|
---|
257 | Log5(("TMAll(%u): FF: 0 -> 1\n", __LINE__));
|
---|
258 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
259 | #ifdef IN_RING3
|
---|
260 | # ifdef VBOX_WITH_REM
|
---|
261 | REMR3NotifyTimerPending(pVM, pVCpuDst);
|
---|
262 | # endif
|
---|
263 | VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM);
|
---|
264 | #endif
|
---|
265 | STAM_COUNTER_INC(&pVM->tm.s.StatScheduleSetFF);
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Schedule the queue which was changed.
|
---|
272 | */
|
---|
273 | DECLINLINE(void) tmSchedule(PTMTIMER pTimer)
|
---|
274 | {
|
---|
275 | PVM pVM = pTimer->CTX_SUFF(pVM);
|
---|
276 | if ( VM_IS_EMT(pVM)
|
---|
277 | && RT_SUCCESS(TM_TRY_LOCK_TIMERS(pVM)))
|
---|
278 | {
|
---|
279 | STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
|
---|
280 | Log3(("tmSchedule: tmTimerQueueSchedule\n"));
|
---|
281 | tmTimerQueueSchedule(pVM, &pVM->tm.s.CTX_SUFF(paTimerQueues)[pTimer->enmClock]);
|
---|
282 | #ifdef VBOX_STRICT
|
---|
283 | tmTimerQueuesSanityChecks(pVM, "tmSchedule");
|
---|
284 | #endif
|
---|
285 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
|
---|
286 | TM_UNLOCK_TIMERS(pVM);
|
---|
287 | }
|
---|
288 | else
|
---|
289 | {
|
---|
290 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
291 | if (TMTIMERSTATE_IS_PENDING_SCHEDULING(enmState))
|
---|
292 | tmScheduleNotify(pVM);
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Try change the state to enmStateNew from enmStateOld
|
---|
299 | * and link the timer into the scheduling queue.
|
---|
300 | *
|
---|
301 | * @returns Success indicator.
|
---|
302 | * @param pTimer Timer in question.
|
---|
303 | * @param enmStateNew The new timer state.
|
---|
304 | * @param enmStateOld The old timer state.
|
---|
305 | */
|
---|
306 | DECLINLINE(bool) tmTimerTry(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
|
---|
307 | {
|
---|
308 | /*
|
---|
309 | * Attempt state change.
|
---|
310 | */
|
---|
311 | bool fRc;
|
---|
312 | TM_TRY_SET_STATE(pTimer, enmStateNew, enmStateOld, fRc);
|
---|
313 | return fRc;
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Links the timer onto the scheduling queue.
|
---|
319 | *
|
---|
320 | * @param pQueue The timer queue the timer belongs to.
|
---|
321 | * @param pTimer The timer.
|
---|
322 | *
|
---|
323 | * @todo FIXME: Look into potential race with the thread running the queues
|
---|
324 | * and stuff.
|
---|
325 | */
|
---|
326 | DECLINLINE(void) tmTimerLinkSchedule(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
|
---|
327 | {
|
---|
328 | Assert(!pTimer->offScheduleNext);
|
---|
329 | const int32_t offHeadNew = (intptr_t)pTimer - (intptr_t)pQueue;
|
---|
330 | int32_t offHead;
|
---|
331 | do
|
---|
332 | {
|
---|
333 | offHead = pQueue->offSchedule;
|
---|
334 | if (offHead)
|
---|
335 | pTimer->offScheduleNext = ((intptr_t)pQueue + offHead) - (intptr_t)pTimer;
|
---|
336 | else
|
---|
337 | pTimer->offScheduleNext = 0;
|
---|
338 | } while (!ASMAtomicCmpXchgS32(&pQueue->offSchedule, offHeadNew, offHead));
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Try change the state to enmStateNew from enmStateOld
|
---|
344 | * and link the timer into the scheduling queue.
|
---|
345 | *
|
---|
346 | * @returns Success indicator.
|
---|
347 | * @param pTimer Timer in question.
|
---|
348 | * @param enmStateNew The new timer state.
|
---|
349 | * @param enmStateOld The old timer state.
|
---|
350 | */
|
---|
351 | DECLINLINE(bool) tmTimerTryWithLink(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
|
---|
352 | {
|
---|
353 | if (tmTimerTry(pTimer, enmStateNew, enmStateOld))
|
---|
354 | {
|
---|
355 | tmTimerLinkSchedule(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF(paTimerQueues)[pTimer->enmClock], pTimer);
|
---|
356 | return true;
|
---|
357 | }
|
---|
358 | return false;
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Links a timer into the active list of a timer queue.
|
---|
364 | *
|
---|
365 | * @param pQueue The queue.
|
---|
366 | * @param pTimer The timer.
|
---|
367 | * @param u64Expire The timer expiration time.
|
---|
368 | *
|
---|
369 | * @remarks Called while owning the relevant queue lock.
|
---|
370 | */
|
---|
371 | DECL_FORCE_INLINE(void) tmTimerQueueLinkActive(PTMTIMERQUEUE pQueue, PTMTIMER pTimer, uint64_t u64Expire)
|
---|
372 | {
|
---|
373 | Assert(!pTimer->offNext);
|
---|
374 | Assert(!pTimer->offPrev);
|
---|
375 | Assert(pTimer->enmState == TMTIMERSTATE_ACTIVE || pTimer->enmClock != TMCLOCK_VIRTUAL_SYNC); /* (active is not a stable state) */
|
---|
376 |
|
---|
377 | PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue);
|
---|
378 | if (pCur)
|
---|
379 | {
|
---|
380 | for (;; pCur = TMTIMER_GET_NEXT(pCur))
|
---|
381 | {
|
---|
382 | if (pCur->u64Expire > u64Expire)
|
---|
383 | {
|
---|
384 | const PTMTIMER pPrev = TMTIMER_GET_PREV(pCur);
|
---|
385 | TMTIMER_SET_NEXT(pTimer, pCur);
|
---|
386 | TMTIMER_SET_PREV(pTimer, pPrev);
|
---|
387 | if (pPrev)
|
---|
388 | TMTIMER_SET_NEXT(pPrev, pTimer);
|
---|
389 | else
|
---|
390 | {
|
---|
391 | TMTIMER_SET_HEAD(pQueue, pTimer);
|
---|
392 | ASMAtomicWriteU64(&pQueue->u64Expire, u64Expire);
|
---|
393 | DBGFTRACE_U64_TAG2(pTimer->CTX_SUFF(pVM), u64Expire, "tmTimerQueueLinkActive head", R3STRING(pTimer->pszDesc));
|
---|
394 | }
|
---|
395 | TMTIMER_SET_PREV(pCur, pTimer);
|
---|
396 | return;
|
---|
397 | }
|
---|
398 | if (!pCur->offNext)
|
---|
399 | {
|
---|
400 | TMTIMER_SET_NEXT(pCur, pTimer);
|
---|
401 | TMTIMER_SET_PREV(pTimer, pCur);
|
---|
402 | DBGFTRACE_U64_TAG2(pTimer->CTX_SUFF(pVM), u64Expire, "tmTimerQueueLinkActive tail", R3STRING(pTimer->pszDesc));
|
---|
403 | return;
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 | else
|
---|
408 | {
|
---|
409 | TMTIMER_SET_HEAD(pQueue, pTimer);
|
---|
410 | ASMAtomicWriteU64(&pQueue->u64Expire, u64Expire);
|
---|
411 | DBGFTRACE_U64_TAG2(pTimer->CTX_SUFF(pVM), u64Expire, "tmTimerQueueLinkActive empty", R3STRING(pTimer->pszDesc));
|
---|
412 | }
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Schedules the given timer on the given queue.
|
---|
419 | *
|
---|
420 | * @param pQueue The timer queue.
|
---|
421 | * @param pTimer The timer that needs scheduling.
|
---|
422 | *
|
---|
423 | * @remarks Called while owning the lock.
|
---|
424 | */
|
---|
425 | DECLINLINE(void) tmTimerQueueScheduleOne(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
|
---|
426 | {
|
---|
427 | Assert(pQueue->enmClock != TMCLOCK_VIRTUAL_SYNC);
|
---|
428 |
|
---|
429 | /*
|
---|
430 | * Processing.
|
---|
431 | */
|
---|
432 | unsigned cRetries = 2;
|
---|
433 | do
|
---|
434 | {
|
---|
435 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
436 | switch (enmState)
|
---|
437 | {
|
---|
438 | /*
|
---|
439 | * Reschedule timer (in the active list).
|
---|
440 | */
|
---|
441 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
442 | if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE, TMTIMERSTATE_PENDING_RESCHEDULE)))
|
---|
443 | break; /* retry */
|
---|
444 | tmTimerQueueUnlinkActive(pQueue, pTimer);
|
---|
445 | /* fall thru */
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Schedule timer (insert into the active list).
|
---|
449 | */
|
---|
450 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
451 | Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
|
---|
452 | if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, TMTIMERSTATE_PENDING_SCHEDULE)))
|
---|
453 | break; /* retry */
|
---|
454 | tmTimerQueueLinkActive(pQueue, pTimer, pTimer->u64Expire);
|
---|
455 | return;
|
---|
456 |
|
---|
457 | /*
|
---|
458 | * Stop the timer in active list.
|
---|
459 | */
|
---|
460 | case TMTIMERSTATE_PENDING_STOP:
|
---|
461 | if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, TMTIMERSTATE_PENDING_STOP)))
|
---|
462 | break; /* retry */
|
---|
463 | tmTimerQueueUnlinkActive(pQueue, pTimer);
|
---|
464 | /* fall thru */
|
---|
465 |
|
---|
466 | /*
|
---|
467 | * Stop the timer (not on the active list).
|
---|
468 | */
|
---|
469 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
470 | Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
|
---|
471 | if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_PENDING_STOP_SCHEDULE)))
|
---|
472 | break;
|
---|
473 | return;
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * The timer is pending destruction by TMR3TimerDestroy, our caller.
|
---|
477 | * Nothing to do here.
|
---|
478 | */
|
---|
479 | case TMTIMERSTATE_DESTROY:
|
---|
480 | break;
|
---|
481 |
|
---|
482 | /*
|
---|
483 | * Postpone these until they get into the right state.
|
---|
484 | */
|
---|
485 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
486 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
487 | tmTimerLinkSchedule(pQueue, pTimer);
|
---|
488 | STAM_COUNTER_INC(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatPostponed));
|
---|
489 | return;
|
---|
490 |
|
---|
491 | /*
|
---|
492 | * None of these can be in the schedule.
|
---|
493 | */
|
---|
494 | case TMTIMERSTATE_FREE:
|
---|
495 | case TMTIMERSTATE_STOPPED:
|
---|
496 | case TMTIMERSTATE_ACTIVE:
|
---|
497 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
498 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
499 | default:
|
---|
500 | AssertMsgFailed(("Timer (%p) in the scheduling list has an invalid state %s (%d)!",
|
---|
501 | pTimer, tmTimerState(pTimer->enmState), pTimer->enmState));
|
---|
502 | return;
|
---|
503 | }
|
---|
504 | } while (cRetries-- > 0);
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Schedules the specified timer queue.
|
---|
510 | *
|
---|
511 | * @param pVM The cross context VM structure.
|
---|
512 | * @param pQueue The queue to schedule.
|
---|
513 | *
|
---|
514 | * @remarks Called while owning the lock.
|
---|
515 | */
|
---|
516 | void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue)
|
---|
517 | {
|
---|
518 | TM_ASSERT_TIMER_LOCK_OWNERSHIP(pVM);
|
---|
519 | NOREF(pVM);
|
---|
520 |
|
---|
521 | /*
|
---|
522 | * Dequeue the scheduling list and iterate it.
|
---|
523 | */
|
---|
524 | int32_t offNext = ASMAtomicXchgS32(&pQueue->offSchedule, 0);
|
---|
525 | Log2(("tmTimerQueueSchedule: pQueue=%p:{.enmClock=%d, offNext=%RI32, .u64Expired=%'RU64}\n", pQueue, pQueue->enmClock, offNext, pQueue->u64Expire));
|
---|
526 | if (!offNext)
|
---|
527 | return;
|
---|
528 | PTMTIMER pNext = (PTMTIMER)((intptr_t)pQueue + offNext);
|
---|
529 | while (pNext)
|
---|
530 | {
|
---|
531 | /*
|
---|
532 | * Unlink the head timer and find the next one.
|
---|
533 | */
|
---|
534 | PTMTIMER pTimer = pNext;
|
---|
535 | pNext = pNext->offScheduleNext ? (PTMTIMER)((intptr_t)pNext + pNext->offScheduleNext) : NULL;
|
---|
536 | pTimer->offScheduleNext = 0;
|
---|
537 |
|
---|
538 | /*
|
---|
539 | * Do the scheduling.
|
---|
540 | */
|
---|
541 | Log2(("tmTimerQueueSchedule: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, .pszDesc=%s}\n",
|
---|
542 | pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, R3STRING(pTimer->pszDesc)));
|
---|
543 | tmTimerQueueScheduleOne(pQueue, pTimer);
|
---|
544 | Log2(("tmTimerQueueSchedule: %p: new %s\n", pTimer, tmTimerState(pTimer->enmState)));
|
---|
545 | } /* foreach timer in current schedule batch. */
|
---|
546 | Log2(("tmTimerQueueSchedule: u64Expired=%'RU64\n", pQueue->u64Expire));
|
---|
547 | }
|
---|
548 |
|
---|
549 |
|
---|
550 | #ifdef VBOX_STRICT
|
---|
551 | /**
|
---|
552 | * Checks that the timer queues are sane.
|
---|
553 | *
|
---|
554 | * @param pVM The cross context VM structure.
|
---|
555 | * @param pszWhere Caller location clue.
|
---|
556 | *
|
---|
557 | * @remarks Called while owning the lock.
|
---|
558 | */
|
---|
559 | void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere)
|
---|
560 | {
|
---|
561 | TM_ASSERT_TIMER_LOCK_OWNERSHIP(pVM);
|
---|
562 |
|
---|
563 | /*
|
---|
564 | * Check the linking of the active lists.
|
---|
565 | */
|
---|
566 | bool fHaveVirtualSyncLock = false;
|
---|
567 | for (int i = 0; i < TMCLOCK_MAX; i++)
|
---|
568 | {
|
---|
569 | PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[i];
|
---|
570 | Assert((int)pQueue->enmClock == i);
|
---|
571 | if (pQueue->enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
572 | {
|
---|
573 | if (PDMCritSectTryEnter(&pVM->tm.s.VirtualSyncLock) != VINF_SUCCESS)
|
---|
574 | continue;
|
---|
575 | fHaveVirtualSyncLock = true;
|
---|
576 | }
|
---|
577 | PTMTIMER pPrev = NULL;
|
---|
578 | for (PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue); pCur; pPrev = pCur, pCur = TMTIMER_GET_NEXT(pCur))
|
---|
579 | {
|
---|
580 | AssertMsg((int)pCur->enmClock == i, ("%s: %d != %d\n", pszWhere, pCur->enmClock, i));
|
---|
581 | AssertMsg(TMTIMER_GET_PREV(pCur) == pPrev, ("%s: %p != %p\n", pszWhere, TMTIMER_GET_PREV(pCur), pPrev));
|
---|
582 | TMTIMERSTATE enmState = pCur->enmState;
|
---|
583 | switch (enmState)
|
---|
584 | {
|
---|
585 | case TMTIMERSTATE_ACTIVE:
|
---|
586 | AssertMsg( !pCur->offScheduleNext
|
---|
587 | || pCur->enmState != TMTIMERSTATE_ACTIVE,
|
---|
588 | ("%s: %RI32\n", pszWhere, pCur->offScheduleNext));
|
---|
589 | break;
|
---|
590 | case TMTIMERSTATE_PENDING_STOP:
|
---|
591 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
592 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
593 | break;
|
---|
594 | default:
|
---|
595 | AssertMsgFailed(("%s: Invalid state enmState=%d %s\n", pszWhere, enmState, tmTimerState(enmState)));
|
---|
596 | break;
|
---|
597 | }
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 |
|
---|
602 | # ifdef IN_RING3
|
---|
603 | /*
|
---|
604 | * Do the big list and check that active timers all are in the active lists.
|
---|
605 | */
|
---|
606 | PTMTIMERR3 pPrev = NULL;
|
---|
607 | for (PTMTIMERR3 pCur = pVM->tm.s.pCreated; pCur; pPrev = pCur, pCur = pCur->pBigNext)
|
---|
608 | {
|
---|
609 | Assert(pCur->pBigPrev == pPrev);
|
---|
610 | Assert((unsigned)pCur->enmClock < (unsigned)TMCLOCK_MAX);
|
---|
611 |
|
---|
612 | TMTIMERSTATE enmState = pCur->enmState;
|
---|
613 | switch (enmState)
|
---|
614 | {
|
---|
615 | case TMTIMERSTATE_ACTIVE:
|
---|
616 | case TMTIMERSTATE_PENDING_STOP:
|
---|
617 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
618 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
619 | if (fHaveVirtualSyncLock || pCur->enmClock != TMCLOCK_VIRTUAL_SYNC)
|
---|
620 | {
|
---|
621 | PTMTIMERR3 pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTX_SUFF(paTimerQueues)[pCur->enmClock]);
|
---|
622 | Assert(pCur->offPrev || pCur == pCurAct);
|
---|
623 | while (pCurAct && pCurAct != pCur)
|
---|
624 | pCurAct = TMTIMER_GET_NEXT(pCurAct);
|
---|
625 | Assert(pCurAct == pCur);
|
---|
626 | }
|
---|
627 | break;
|
---|
628 |
|
---|
629 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
630 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
631 | case TMTIMERSTATE_STOPPED:
|
---|
632 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
633 | if (fHaveVirtualSyncLock || pCur->enmClock != TMCLOCK_VIRTUAL_SYNC)
|
---|
634 | {
|
---|
635 | Assert(!pCur->offNext);
|
---|
636 | Assert(!pCur->offPrev);
|
---|
637 | for (PTMTIMERR3 pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTX_SUFF(paTimerQueues)[pCur->enmClock]);
|
---|
638 | pCurAct;
|
---|
639 | pCurAct = TMTIMER_GET_NEXT(pCurAct))
|
---|
640 | {
|
---|
641 | Assert(pCurAct != pCur);
|
---|
642 | Assert(TMTIMER_GET_NEXT(pCurAct) != pCur);
|
---|
643 | Assert(TMTIMER_GET_PREV(pCurAct) != pCur);
|
---|
644 | }
|
---|
645 | }
|
---|
646 | break;
|
---|
647 |
|
---|
648 | /* ignore */
|
---|
649 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
650 | break;
|
---|
651 |
|
---|
652 | /* shouldn't get here! */
|
---|
653 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
654 | case TMTIMERSTATE_DESTROY:
|
---|
655 | default:
|
---|
656 | AssertMsgFailed(("Invalid state enmState=%d %s\n", enmState, tmTimerState(enmState)));
|
---|
657 | break;
|
---|
658 | }
|
---|
659 | }
|
---|
660 | # endif /* IN_RING3 */
|
---|
661 |
|
---|
662 | if (fHaveVirtualSyncLock)
|
---|
663 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
664 | }
|
---|
665 | #endif /* !VBOX_STRICT */
|
---|
666 |
|
---|
667 | #ifdef VBOX_HIGH_RES_TIMERS_HACK
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Worker for tmTimerPollInternal that handles misses when the dedicated timer
|
---|
671 | * EMT is polling.
|
---|
672 | *
|
---|
673 | * @returns See tmTimerPollInternal.
|
---|
674 | * @param pVM The cross context VM structure.
|
---|
675 | * @param u64Now Current virtual clock timestamp.
|
---|
676 | * @param u64Delta The delta to the next even in ticks of the
|
---|
677 | * virtual clock.
|
---|
678 | * @param pu64Delta Where to return the delta.
|
---|
679 | */
|
---|
680 | DECLINLINE(uint64_t) tmTimerPollReturnMiss(PVM pVM, uint64_t u64Now, uint64_t u64Delta, uint64_t *pu64Delta)
|
---|
681 | {
|
---|
682 | Assert(!(u64Delta & RT_BIT_64(63)));
|
---|
683 |
|
---|
684 | if (!pVM->tm.s.fVirtualWarpDrive)
|
---|
685 | {
|
---|
686 | *pu64Delta = u64Delta;
|
---|
687 | return u64Delta + u64Now + pVM->tm.s.u64VirtualOffset;
|
---|
688 | }
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * Warp drive adjustments - this is the reverse of what tmVirtualGetRaw is doing.
|
---|
692 | */
|
---|
693 | uint64_t const u64Start = pVM->tm.s.u64VirtualWarpDriveStart;
|
---|
694 | uint32_t const u32Pct = pVM->tm.s.u32VirtualWarpDrivePercentage;
|
---|
695 |
|
---|
696 | uint64_t u64GipTime = u64Delta + u64Now + pVM->tm.s.u64VirtualOffset;
|
---|
697 | u64GipTime -= u64Start; /* the start is GIP time. */
|
---|
698 | if (u64GipTime >= u64Delta)
|
---|
699 | {
|
---|
700 | ASMMultU64ByU32DivByU32(u64GipTime, 100, u32Pct);
|
---|
701 | ASMMultU64ByU32DivByU32(u64Delta, 100, u32Pct);
|
---|
702 | }
|
---|
703 | else
|
---|
704 | {
|
---|
705 | u64Delta -= u64GipTime;
|
---|
706 | ASMMultU64ByU32DivByU32(u64GipTime, 100, u32Pct);
|
---|
707 | u64Delta += u64GipTime;
|
---|
708 | }
|
---|
709 | *pu64Delta = u64Delta;
|
---|
710 | u64GipTime += u64Start;
|
---|
711 | return u64GipTime;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Worker for tmTimerPollInternal dealing with returns on virtual CPUs other
|
---|
717 | * than the one dedicated to timer work.
|
---|
718 | *
|
---|
719 | * @returns See tmTimerPollInternal.
|
---|
720 | * @param pVM The cross context VM structure.
|
---|
721 | * @param u64Now Current virtual clock timestamp.
|
---|
722 | * @param pu64Delta Where to return the delta.
|
---|
723 | */
|
---|
724 | DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnOtherCpu(PVM pVM, uint64_t u64Now, uint64_t *pu64Delta)
|
---|
725 | {
|
---|
726 | static const uint64_t s_u64OtherRet = 500000000; /* 500 ms for non-timer EMTs. */
|
---|
727 | *pu64Delta = s_u64OtherRet;
|
---|
728 | return u64Now + pVM->tm.s.u64VirtualOffset + s_u64OtherRet;
|
---|
729 | }
|
---|
730 |
|
---|
731 |
|
---|
732 | /**
|
---|
733 | * Worker for tmTimerPollInternal.
|
---|
734 | *
|
---|
735 | * @returns See tmTimerPollInternal.
|
---|
736 | * @param pVM The cross context VM structure.
|
---|
737 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
738 | * @param pVCpuDst The cross context virtual CPU structure of the dedicated
|
---|
739 | * timer EMT.
|
---|
740 | * @param u64Now Current virtual clock timestamp.
|
---|
741 | * @param pu64Delta Where to return the delta.
|
---|
742 | * @param pCounter The statistics counter to update.
|
---|
743 | */
|
---|
744 | DECL_FORCE_INLINE(uint64_t) tmTimerPollReturnHit(PVM pVM, PVMCPU pVCpu, PVMCPU pVCpuDst, uint64_t u64Now,
|
---|
745 | uint64_t *pu64Delta, PSTAMCOUNTER pCounter)
|
---|
746 | {
|
---|
747 | STAM_COUNTER_INC(pCounter); NOREF(pCounter);
|
---|
748 | if (pVCpuDst != pVCpu)
|
---|
749 | return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
|
---|
750 | *pu64Delta = 0;
|
---|
751 | return 0;
|
---|
752 | }
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Common worker for TMTimerPollGIP and TMTimerPoll.
|
---|
756 | *
|
---|
757 | * This function is called before FFs are checked in the inner execution EM loops.
|
---|
758 | *
|
---|
759 | * @returns The GIP timestamp of the next event.
|
---|
760 | * 0 if the next event has already expired.
|
---|
761 | *
|
---|
762 | * @param pVM The cross context VM structure.
|
---|
763 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
764 | * @param pu64Delta Where to store the delta.
|
---|
765 | *
|
---|
766 | * @thread The emulation thread.
|
---|
767 | *
|
---|
768 | * @remarks GIP uses ns ticks.
|
---|
769 | */
|
---|
770 | DECL_FORCE_INLINE(uint64_t) tmTimerPollInternal(PVM pVM, PVMCPU pVCpu, uint64_t *pu64Delta)
|
---|
771 | {
|
---|
772 | PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
|
---|
773 | const uint64_t u64Now = TMVirtualGetNoCheck(pVM);
|
---|
774 | STAM_COUNTER_INC(&pVM->tm.s.StatPoll);
|
---|
775 |
|
---|
776 | /*
|
---|
777 | * Return straight away if the timer FF is already set ...
|
---|
778 | */
|
---|
779 | if (VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
|
---|
780 | return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
|
---|
781 |
|
---|
782 | /*
|
---|
783 | * ... or if timers are being run.
|
---|
784 | */
|
---|
785 | if (ASMAtomicReadBool(&pVM->tm.s.fRunningQueues))
|
---|
786 | {
|
---|
787 | STAM_COUNTER_INC(&pVM->tm.s.StatPollRunning);
|
---|
788 | return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
|
---|
789 | }
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * Check for TMCLOCK_VIRTUAL expiration.
|
---|
793 | */
|
---|
794 | const uint64_t u64Expire1 = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire);
|
---|
795 | const int64_t i64Delta1 = u64Expire1 - u64Now;
|
---|
796 | if (i64Delta1 <= 0)
|
---|
797 | {
|
---|
798 | if (!VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
|
---|
799 | {
|
---|
800 | Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_PENDING(pVCpuDst, VMCPU_FF_TIMER)));
|
---|
801 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
802 | #if defined(IN_RING3) && defined(VBOX_WITH_REM)
|
---|
803 | REMR3NotifyTimerPending(pVM, pVCpuDst);
|
---|
804 | #endif
|
---|
805 | }
|
---|
806 | LogFlow(("TMTimerPoll: expire1=%'RU64 <= now=%'RU64\n", u64Expire1, u64Now));
|
---|
807 | return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtual);
|
---|
808 | }
|
---|
809 |
|
---|
810 | /*
|
---|
811 | * Check for TMCLOCK_VIRTUAL_SYNC expiration.
|
---|
812 | * This isn't quite as straight forward if in a catch-up, not only do
|
---|
813 | * we have to adjust the 'now' but when have to adjust the delta as well.
|
---|
814 | */
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * Optimistic lockless approach.
|
---|
818 | */
|
---|
819 | uint64_t u64VirtualSyncNow;
|
---|
820 | uint64_t u64Expire2 = ASMAtomicUoReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
|
---|
821 | if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
|
---|
822 | {
|
---|
823 | if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
824 | {
|
---|
825 | u64VirtualSyncNow = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
|
---|
826 | if (RT_LIKELY( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
|
---|
827 | && !ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
|
---|
828 | && u64VirtualSyncNow == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
|
---|
829 | && u64Expire2 == ASMAtomicUoReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)))
|
---|
830 | {
|
---|
831 | u64VirtualSyncNow = u64Now - u64VirtualSyncNow;
|
---|
832 | int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
|
---|
833 | if (i64Delta2 > 0)
|
---|
834 | {
|
---|
835 | STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
|
---|
836 | STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
|
---|
837 |
|
---|
838 | if (pVCpu == pVCpuDst)
|
---|
839 | return tmTimerPollReturnMiss(pVM, u64Now, RT_MIN(i64Delta1, i64Delta2), pu64Delta);
|
---|
840 | return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
|
---|
841 | }
|
---|
842 |
|
---|
843 | if ( !pVM->tm.s.fRunningQueues
|
---|
844 | && !VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
|
---|
845 | {
|
---|
846 | Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_PENDING(pVCpuDst, VMCPU_FF_TIMER)));
|
---|
847 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
848 | #if defined(IN_RING3) && defined(VBOX_WITH_REM)
|
---|
849 | REMR3NotifyTimerPending(pVM, pVCpuDst);
|
---|
850 | #endif
|
---|
851 | }
|
---|
852 |
|
---|
853 | STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
|
---|
854 | LogFlow(("TMTimerPoll: expire2=%'RU64 <= now=%'RU64\n", u64Expire2, u64Now));
|
---|
855 | return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
|
---|
856 | }
|
---|
857 | }
|
---|
858 | }
|
---|
859 | else
|
---|
860 | {
|
---|
861 | STAM_COUNTER_INC(&pVM->tm.s.StatPollSimple);
|
---|
862 | LogFlow(("TMTimerPoll: stopped\n"));
|
---|
863 | return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
|
---|
864 | }
|
---|
865 |
|
---|
866 | /*
|
---|
867 | * Complicated lockless approach.
|
---|
868 | */
|
---|
869 | uint64_t off;
|
---|
870 | uint32_t u32Pct = 0;
|
---|
871 | bool fCatchUp;
|
---|
872 | int cOuterTries = 42;
|
---|
873 | for (;; cOuterTries--)
|
---|
874 | {
|
---|
875 | fCatchUp = ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp);
|
---|
876 | off = ASMAtomicReadU64(&pVM->tm.s.offVirtualSync);
|
---|
877 | u64Expire2 = ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire);
|
---|
878 | if (fCatchUp)
|
---|
879 | {
|
---|
880 | /* No changes allowed, try get a consistent set of parameters. */
|
---|
881 | uint64_t const u64Prev = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev);
|
---|
882 | uint64_t const offGivenUp = ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp);
|
---|
883 | u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage);
|
---|
884 | if ( ( u64Prev == ASMAtomicReadU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev)
|
---|
885 | && offGivenUp == ASMAtomicReadU64(&pVM->tm.s.offVirtualSyncGivenUp)
|
---|
886 | && u32Pct == ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage)
|
---|
887 | && off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
|
---|
888 | && u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)
|
---|
889 | && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
|
---|
890 | && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
|
---|
891 | || cOuterTries <= 0)
|
---|
892 | {
|
---|
893 | uint64_t u64Delta = u64Now - u64Prev;
|
---|
894 | if (RT_LIKELY(!(u64Delta >> 32)))
|
---|
895 | {
|
---|
896 | uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, u32Pct, 100);
|
---|
897 | if (off > u64Sub + offGivenUp)
|
---|
898 | off -= u64Sub;
|
---|
899 | else /* we've completely caught up. */
|
---|
900 | off = offGivenUp;
|
---|
901 | }
|
---|
902 | else
|
---|
903 | /* More than 4 seconds since last time (or negative), ignore it. */
|
---|
904 | Log(("TMVirtualGetSync: u64Delta=%RX64 (NoLock)\n", u64Delta));
|
---|
905 |
|
---|
906 | /* Check that we're still running and in catch up. */
|
---|
907 | if ( ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking)
|
---|
908 | && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
909 | break;
|
---|
910 | }
|
---|
911 | }
|
---|
912 | else if ( off == ASMAtomicReadU64(&pVM->tm.s.offVirtualSync)
|
---|
913 | && u64Expire2 == ASMAtomicReadU64(&pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire)
|
---|
914 | && !ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp)
|
---|
915 | && ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncTicking))
|
---|
916 | break; /* Got an consistent offset */
|
---|
917 |
|
---|
918 | /* Repeat the initial checks before iterating. */
|
---|
919 | if (VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
|
---|
920 | return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollAlreadySet);
|
---|
921 | if (ASMAtomicUoReadBool(&pVM->tm.s.fRunningQueues))
|
---|
922 | {
|
---|
923 | STAM_COUNTER_INC(&pVM->tm.s.StatPollRunning);
|
---|
924 | return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
|
---|
925 | }
|
---|
926 | if (!ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncTicking))
|
---|
927 | {
|
---|
928 | LogFlow(("TMTimerPoll: stopped\n"));
|
---|
929 | return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
|
---|
930 | }
|
---|
931 | if (cOuterTries <= 0)
|
---|
932 | break; /* that's enough */
|
---|
933 | }
|
---|
934 | if (cOuterTries <= 0)
|
---|
935 | STAM_COUNTER_INC(&pVM->tm.s.StatPollELoop);
|
---|
936 | u64VirtualSyncNow = u64Now - off;
|
---|
937 |
|
---|
938 | /* Calc delta and see if we've got a virtual sync hit. */
|
---|
939 | int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
|
---|
940 | if (i64Delta2 <= 0)
|
---|
941 | {
|
---|
942 | if ( !pVM->tm.s.fRunningQueues
|
---|
943 | && !VMCPU_FF_IS_SET(pVCpuDst, VMCPU_FF_TIMER))
|
---|
944 | {
|
---|
945 | Log5(("TMAll(%u): FF: %d -> 1\n", __LINE__, VMCPU_FF_IS_PENDING(pVCpuDst, VMCPU_FF_TIMER)));
|
---|
946 | VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
|
---|
947 | #if defined(IN_RING3) && defined(VBOX_WITH_REM)
|
---|
948 | REMR3NotifyTimerPending(pVM, pVCpuDst);
|
---|
949 | #endif
|
---|
950 | }
|
---|
951 | STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtualSync);
|
---|
952 | LogFlow(("TMTimerPoll: expire2=%'RU64 <= now=%'RU64\n", u64Expire2, u64Now));
|
---|
953 | return tmTimerPollReturnHit(pVM, pVCpu, pVCpuDst, u64Now, pu64Delta, &pVM->tm.s.StatPollVirtualSync);
|
---|
954 | }
|
---|
955 |
|
---|
956 | /*
|
---|
957 | * Return the time left to the next event.
|
---|
958 | */
|
---|
959 | STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
|
---|
960 | if (pVCpu == pVCpuDst)
|
---|
961 | {
|
---|
962 | if (fCatchUp)
|
---|
963 | i64Delta2 = ASMMultU64ByU32DivByU32(i64Delta2, 100, u32Pct + 100);
|
---|
964 | return tmTimerPollReturnMiss(pVM, u64Now, RT_MIN(i64Delta1, i64Delta2), pu64Delta);
|
---|
965 | }
|
---|
966 | return tmTimerPollReturnOtherCpu(pVM, u64Now, pu64Delta);
|
---|
967 | }
|
---|
968 |
|
---|
969 |
|
---|
970 | /**
|
---|
971 | * Set FF if we've passed the next virtual event.
|
---|
972 | *
|
---|
973 | * This function is called before FFs are checked in the inner execution EM loops.
|
---|
974 | *
|
---|
975 | * @returns true if timers are pending, false if not.
|
---|
976 | *
|
---|
977 | * @param pVM The cross context VM structure.
|
---|
978 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
979 | * @thread The emulation thread.
|
---|
980 | */
|
---|
981 | VMMDECL(bool) TMTimerPollBool(PVM pVM, PVMCPU pVCpu)
|
---|
982 | {
|
---|
983 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
984 | uint64_t off = 0;
|
---|
985 | tmTimerPollInternal(pVM, pVCpu, &off);
|
---|
986 | return off == 0;
|
---|
987 | }
|
---|
988 |
|
---|
989 |
|
---|
990 | /**
|
---|
991 | * Set FF if we've passed the next virtual event.
|
---|
992 | *
|
---|
993 | * This function is called before FFs are checked in the inner execution EM loops.
|
---|
994 | *
|
---|
995 | * @param pVM The cross context VM structure.
|
---|
996 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
997 | * @thread The emulation thread.
|
---|
998 | */
|
---|
999 | VMM_INT_DECL(void) TMTimerPollVoid(PVM pVM, PVMCPU pVCpu)
|
---|
1000 | {
|
---|
1001 | uint64_t off;
|
---|
1002 | tmTimerPollInternal(pVM, pVCpu, &off);
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 |
|
---|
1006 | /**
|
---|
1007 | * Set FF if we've passed the next virtual event.
|
---|
1008 | *
|
---|
1009 | * This function is called before FFs are checked in the inner execution EM loops.
|
---|
1010 | *
|
---|
1011 | * @returns The GIP timestamp of the next event.
|
---|
1012 | * 0 if the next event has already expired.
|
---|
1013 | * @param pVM The cross context VM structure.
|
---|
1014 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
1015 | * @param pu64Delta Where to store the delta.
|
---|
1016 | * @thread The emulation thread.
|
---|
1017 | */
|
---|
1018 | VMM_INT_DECL(uint64_t) TMTimerPollGIP(PVM pVM, PVMCPU pVCpu, uint64_t *pu64Delta)
|
---|
1019 | {
|
---|
1020 | return tmTimerPollInternal(pVM, pVCpu, pu64Delta);
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | #endif /* VBOX_HIGH_RES_TIMERS_HACK */
|
---|
1024 |
|
---|
1025 | /**
|
---|
1026 | * Gets the host context ring-3 pointer of the timer.
|
---|
1027 | *
|
---|
1028 | * @returns HC R3 pointer.
|
---|
1029 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1030 | */
|
---|
1031 | VMMDECL(PTMTIMERR3) TMTimerR3Ptr(PTMTIMER pTimer)
|
---|
1032 | {
|
---|
1033 | return (PTMTIMERR3)MMHyperCCToR3(pTimer->CTX_SUFF(pVM), pTimer);
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 |
|
---|
1037 | /**
|
---|
1038 | * Gets the host context ring-0 pointer of the timer.
|
---|
1039 | *
|
---|
1040 | * @returns HC R0 pointer.
|
---|
1041 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1042 | */
|
---|
1043 | VMMDECL(PTMTIMERR0) TMTimerR0Ptr(PTMTIMER pTimer)
|
---|
1044 | {
|
---|
1045 | return (PTMTIMERR0)MMHyperCCToR0(pTimer->CTX_SUFF(pVM), pTimer);
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 |
|
---|
1049 | /**
|
---|
1050 | * Gets the RC pointer of the timer.
|
---|
1051 | *
|
---|
1052 | * @returns RC pointer.
|
---|
1053 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1054 | */
|
---|
1055 | VMMDECL(PTMTIMERRC) TMTimerRCPtr(PTMTIMER pTimer)
|
---|
1056 | {
|
---|
1057 | return (PTMTIMERRC)MMHyperCCToRC(pTimer->CTX_SUFF(pVM), pTimer);
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 |
|
---|
1061 | /**
|
---|
1062 | * Locks the timer clock.
|
---|
1063 | *
|
---|
1064 | * @returns VINF_SUCCESS on success, @a rcBusy if busy, and VERR_NOT_SUPPORTED
|
---|
1065 | * if the clock does not have a lock.
|
---|
1066 | * @param pTimer The timer which clock lock we wish to take.
|
---|
1067 | * @param rcBusy What to return in ring-0 and raw-mode context
|
---|
1068 | * if the lock is busy. Pass VINF_SUCCESS to
|
---|
1069 | * acquired the critical section thru a ring-3
|
---|
1070 | call if necessary.
|
---|
1071 | *
|
---|
1072 | * @remarks Currently only supported on timers using the virtual sync clock.
|
---|
1073 | */
|
---|
1074 | VMMDECL(int) TMTimerLock(PTMTIMER pTimer, int rcBusy)
|
---|
1075 | {
|
---|
1076 | AssertPtr(pTimer);
|
---|
1077 | AssertReturn(pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC, VERR_NOT_SUPPORTED);
|
---|
1078 | return PDMCritSectEnter(&pTimer->CTX_SUFF(pVM)->tm.s.VirtualSyncLock, rcBusy);
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 |
|
---|
1082 | /**
|
---|
1083 | * Unlocks a timer clock locked by TMTimerLock.
|
---|
1084 | *
|
---|
1085 | * @param pTimer The timer which clock to unlock.
|
---|
1086 | */
|
---|
1087 | VMMDECL(void) TMTimerUnlock(PTMTIMER pTimer)
|
---|
1088 | {
|
---|
1089 | AssertPtr(pTimer);
|
---|
1090 | AssertReturnVoid(pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC);
|
---|
1091 | PDMCritSectLeave(&pTimer->CTX_SUFF(pVM)->tm.s.VirtualSyncLock);
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 |
|
---|
1095 | /**
|
---|
1096 | * Checks if the current thread owns the timer clock lock.
|
---|
1097 | *
|
---|
1098 | * @returns @c true if its the owner, @c false if not.
|
---|
1099 | * @param pTimer The timer handle.
|
---|
1100 | */
|
---|
1101 | VMMDECL(bool) TMTimerIsLockOwner(PTMTIMER pTimer)
|
---|
1102 | {
|
---|
1103 | AssertPtr(pTimer);
|
---|
1104 | AssertReturn(pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC, false);
|
---|
1105 | return PDMCritSectIsOwner(&pTimer->CTX_SUFF(pVM)->tm.s.VirtualSyncLock);
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 |
|
---|
1109 | /**
|
---|
1110 | * Optimized TMTimerSet code path for starting an inactive timer.
|
---|
1111 | *
|
---|
1112 | * @returns VBox status code.
|
---|
1113 | *
|
---|
1114 | * @param pVM The cross context VM structure.
|
---|
1115 | * @param pTimer The timer handle.
|
---|
1116 | * @param u64Expire The new expire time.
|
---|
1117 | */
|
---|
1118 | static int tmTimerSetOptimizedStart(PVM pVM, PTMTIMER pTimer, uint64_t u64Expire)
|
---|
1119 | {
|
---|
1120 | Assert(!pTimer->offPrev);
|
---|
1121 | Assert(!pTimer->offNext);
|
---|
1122 | Assert(pTimer->enmState == TMTIMERSTATE_ACTIVE);
|
---|
1123 |
|
---|
1124 | TMCLOCK const enmClock = pTimer->enmClock;
|
---|
1125 |
|
---|
1126 | /*
|
---|
1127 | * Calculate and set the expiration time.
|
---|
1128 | */
|
---|
1129 | if (enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
1130 | {
|
---|
1131 | uint64_t u64Last = ASMAtomicReadU64(&pVM->tm.s.u64VirtualSync);
|
---|
1132 | AssertMsgStmt(u64Expire >= u64Last,
|
---|
1133 | ("exp=%#llx last=%#llx\n", u64Expire, u64Last),
|
---|
1134 | u64Expire = u64Last);
|
---|
1135 | }
|
---|
1136 | ASMAtomicWriteU64(&pTimer->u64Expire, u64Expire);
|
---|
1137 | Log2(("tmTimerSetOptimizedStart: %p:{.pszDesc='%s', .u64Expire=%'RU64}\n", pTimer, R3STRING(pTimer->pszDesc), u64Expire));
|
---|
1138 |
|
---|
1139 | /*
|
---|
1140 | * Link the timer into the active list.
|
---|
1141 | */
|
---|
1142 | tmTimerQueueLinkActive(&pVM->tm.s.CTX_SUFF(paTimerQueues)[enmClock], pTimer, u64Expire);
|
---|
1143 |
|
---|
1144 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetOpt);
|
---|
1145 | TM_UNLOCK_TIMERS(pVM);
|
---|
1146 | return VINF_SUCCESS;
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 |
|
---|
1150 | /**
|
---|
1151 | * TMTimerSet for the virtual sync timer queue.
|
---|
1152 | *
|
---|
1153 | * This employs a greatly simplified state machine by always acquiring the
|
---|
1154 | * queue lock and bypassing the scheduling list.
|
---|
1155 | *
|
---|
1156 | * @returns VBox status code
|
---|
1157 | * @param pVM The cross context VM structure.
|
---|
1158 | * @param pTimer The timer handle.
|
---|
1159 | * @param u64Expire The expiration time.
|
---|
1160 | */
|
---|
1161 | static int tmTimerVirtualSyncSet(PVM pVM, PTMTIMER pTimer, uint64_t u64Expire)
|
---|
1162 | {
|
---|
1163 | STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetVs), a);
|
---|
1164 | VM_ASSERT_EMT(pVM);
|
---|
1165 | TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer);
|
---|
1166 | int rc = PDMCritSectEnter(&pVM->tm.s.VirtualSyncLock, VINF_SUCCESS);
|
---|
1167 | AssertRCReturn(rc, rc);
|
---|
1168 |
|
---|
1169 | PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC];
|
---|
1170 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1171 | switch (enmState)
|
---|
1172 | {
|
---|
1173 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
1174 | case TMTIMERSTATE_STOPPED:
|
---|
1175 | if (enmState == TMTIMERSTATE_EXPIRED_DELIVER)
|
---|
1176 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetVsStExpDeliver);
|
---|
1177 | else
|
---|
1178 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetVsStStopped);
|
---|
1179 |
|
---|
1180 | AssertMsg(u64Expire >= pVM->tm.s.u64VirtualSync,
|
---|
1181 | ("%'RU64 < %'RU64 %s\n", u64Expire, pVM->tm.s.u64VirtualSync, R3STRING(pTimer->pszDesc)));
|
---|
1182 | pTimer->u64Expire = u64Expire;
|
---|
1183 | TM_SET_STATE(pTimer, TMTIMERSTATE_ACTIVE);
|
---|
1184 | tmTimerQueueLinkActive(pQueue, pTimer, u64Expire);
|
---|
1185 | rc = VINF_SUCCESS;
|
---|
1186 | break;
|
---|
1187 |
|
---|
1188 | case TMTIMERSTATE_ACTIVE:
|
---|
1189 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetVsStActive);
|
---|
1190 | tmTimerQueueUnlinkActive(pQueue, pTimer);
|
---|
1191 | pTimer->u64Expire = u64Expire;
|
---|
1192 | tmTimerQueueLinkActive(pQueue, pTimer, u64Expire);
|
---|
1193 | rc = VINF_SUCCESS;
|
---|
1194 | break;
|
---|
1195 |
|
---|
1196 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1197 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1198 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1199 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1200 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
1201 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1202 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1203 | case TMTIMERSTATE_DESTROY:
|
---|
1204 | case TMTIMERSTATE_FREE:
|
---|
1205 | AssertLogRelMsgFailed(("Invalid timer state %s: %s\n", tmTimerState(enmState), R3STRING(pTimer->pszDesc)));
|
---|
1206 | rc = VERR_TM_INVALID_STATE;
|
---|
1207 | break;
|
---|
1208 |
|
---|
1209 | default:
|
---|
1210 | AssertMsgFailed(("Unknown timer state %d: %s\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1211 | rc = VERR_TM_UNKNOWN_STATE;
|
---|
1212 | break;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetVs), a);
|
---|
1216 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
1217 | return rc;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 |
|
---|
1221 | /**
|
---|
1222 | * Arm a timer with a (new) expire time.
|
---|
1223 | *
|
---|
1224 | * @returns VBox status code.
|
---|
1225 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1226 | * @param u64Expire New expire time.
|
---|
1227 | */
|
---|
1228 | VMMDECL(int) TMTimerSet(PTMTIMER pTimer, uint64_t u64Expire)
|
---|
1229 | {
|
---|
1230 | PVM pVM = pTimer->CTX_SUFF(pVM);
|
---|
1231 |
|
---|
1232 | /* Treat virtual sync timers specially. */
|
---|
1233 | if (pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
1234 | return tmTimerVirtualSyncSet(pVM, pTimer, u64Expire);
|
---|
1235 |
|
---|
1236 | STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
|
---|
1237 | TMTIMER_ASSERT_CRITSECT(pTimer);
|
---|
1238 |
|
---|
1239 | DBGFTRACE_U64_TAG2(pVM, u64Expire, "TMTimerSet", R3STRING(pTimer->pszDesc));
|
---|
1240 |
|
---|
1241 | #ifdef VBOX_WITH_STATISTICS
|
---|
1242 | /*
|
---|
1243 | * Gather optimization info.
|
---|
1244 | */
|
---|
1245 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSet);
|
---|
1246 | TMTIMERSTATE enmOrgState = pTimer->enmState;
|
---|
1247 | switch (enmOrgState)
|
---|
1248 | {
|
---|
1249 | case TMTIMERSTATE_STOPPED: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStStopped); break;
|
---|
1250 | case TMTIMERSTATE_EXPIRED_DELIVER: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStExpDeliver); break;
|
---|
1251 | case TMTIMERSTATE_ACTIVE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStActive); break;
|
---|
1252 | case TMTIMERSTATE_PENDING_STOP: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendStop); break;
|
---|
1253 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendStopSched); break;
|
---|
1254 | case TMTIMERSTATE_PENDING_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendSched); break;
|
---|
1255 | case TMTIMERSTATE_PENDING_RESCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStPendResched); break;
|
---|
1256 | default: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetStOther); break;
|
---|
1257 | }
|
---|
1258 | #endif
|
---|
1259 |
|
---|
1260 | /*
|
---|
1261 | * The most common case is setting the timer again during the callback.
|
---|
1262 | * The second most common case is starting a timer at some other time.
|
---|
1263 | */
|
---|
1264 | #if 1
|
---|
1265 | TMTIMERSTATE enmState1 = pTimer->enmState;
|
---|
1266 | if ( enmState1 == TMTIMERSTATE_EXPIRED_DELIVER
|
---|
1267 | || ( enmState1 == TMTIMERSTATE_STOPPED
|
---|
1268 | && pTimer->pCritSect))
|
---|
1269 | {
|
---|
1270 | /* Try take the TM lock and check the state again. */
|
---|
1271 | if (RT_SUCCESS_NP(TM_TRY_LOCK_TIMERS(pVM)))
|
---|
1272 | {
|
---|
1273 | if (RT_LIKELY(tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, enmState1)))
|
---|
1274 | {
|
---|
1275 | tmTimerSetOptimizedStart(pVM, pTimer, u64Expire);
|
---|
1276 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
|
---|
1277 | return VINF_SUCCESS;
|
---|
1278 | }
|
---|
1279 | TM_UNLOCK_TIMERS(pVM);
|
---|
1280 | }
|
---|
1281 | }
|
---|
1282 | #endif
|
---|
1283 |
|
---|
1284 | /*
|
---|
1285 | * Unoptimized code path.
|
---|
1286 | */
|
---|
1287 | int cRetries = 1000;
|
---|
1288 | do
|
---|
1289 | {
|
---|
1290 | /*
|
---|
1291 | * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
|
---|
1292 | */
|
---|
1293 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1294 | Log2(("TMTimerSet: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d u64Expire=%'RU64\n",
|
---|
1295 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries, u64Expire));
|
---|
1296 | switch (enmState)
|
---|
1297 | {
|
---|
1298 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
1299 | case TMTIMERSTATE_STOPPED:
|
---|
1300 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
|
---|
1301 | {
|
---|
1302 | Assert(!pTimer->offPrev);
|
---|
1303 | Assert(!pTimer->offNext);
|
---|
1304 | pTimer->u64Expire = u64Expire;
|
---|
1305 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
|
---|
1306 | tmSchedule(pTimer);
|
---|
1307 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
|
---|
1308 | return VINF_SUCCESS;
|
---|
1309 | }
|
---|
1310 | break;
|
---|
1311 |
|
---|
1312 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1313 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1314 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
|
---|
1315 | {
|
---|
1316 | pTimer->u64Expire = u64Expire;
|
---|
1317 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
|
---|
1318 | tmSchedule(pTimer);
|
---|
1319 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
|
---|
1320 | return VINF_SUCCESS;
|
---|
1321 | }
|
---|
1322 | break;
|
---|
1323 |
|
---|
1324 |
|
---|
1325 | case TMTIMERSTATE_ACTIVE:
|
---|
1326 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
|
---|
1327 | {
|
---|
1328 | pTimer->u64Expire = u64Expire;
|
---|
1329 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
|
---|
1330 | tmSchedule(pTimer);
|
---|
1331 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
|
---|
1332 | return VINF_SUCCESS;
|
---|
1333 | }
|
---|
1334 | break;
|
---|
1335 |
|
---|
1336 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1337 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1338 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
|
---|
1339 | {
|
---|
1340 | pTimer->u64Expire = u64Expire;
|
---|
1341 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
|
---|
1342 | tmSchedule(pTimer);
|
---|
1343 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
|
---|
1344 | return VINF_SUCCESS;
|
---|
1345 | }
|
---|
1346 | break;
|
---|
1347 |
|
---|
1348 |
|
---|
1349 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
1350 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1351 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1352 | #ifdef IN_RING3
|
---|
1353 | if (!RTThreadYield())
|
---|
1354 | RTThreadSleep(1);
|
---|
1355 | #else
|
---|
1356 | /** @todo call host context and yield after a couple of iterations */
|
---|
1357 | #endif
|
---|
1358 | break;
|
---|
1359 |
|
---|
1360 | /*
|
---|
1361 | * Invalid states.
|
---|
1362 | */
|
---|
1363 | case TMTIMERSTATE_DESTROY:
|
---|
1364 | case TMTIMERSTATE_FREE:
|
---|
1365 | AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1366 | return VERR_TM_INVALID_STATE;
|
---|
1367 | default:
|
---|
1368 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1369 | return VERR_TM_UNKNOWN_STATE;
|
---|
1370 | }
|
---|
1371 | } while (cRetries-- > 0);
|
---|
1372 |
|
---|
1373 | AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
|
---|
1374 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSet), a);
|
---|
1375 | return VERR_TM_TIMER_UNSTABLE_STATE;
|
---|
1376 | }
|
---|
1377 |
|
---|
1378 |
|
---|
1379 | /**
|
---|
1380 | * Return the current time for the specified clock, setting pu64Now if not NULL.
|
---|
1381 | *
|
---|
1382 | * @returns Current time.
|
---|
1383 | * @param pVM The cross context VM structure.
|
---|
1384 | * @param enmClock The clock to query.
|
---|
1385 | * @param pu64Now Optional pointer where to store the return time
|
---|
1386 | */
|
---|
1387 | DECL_FORCE_INLINE(uint64_t) tmTimerSetRelativeNowWorker(PVM pVM, TMCLOCK enmClock, uint64_t *pu64Now)
|
---|
1388 | {
|
---|
1389 | uint64_t u64Now;
|
---|
1390 | switch (enmClock)
|
---|
1391 | {
|
---|
1392 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
1393 | u64Now = TMVirtualSyncGet(pVM);
|
---|
1394 | break;
|
---|
1395 | case TMCLOCK_VIRTUAL:
|
---|
1396 | u64Now = TMVirtualGet(pVM);
|
---|
1397 | break;
|
---|
1398 | case TMCLOCK_REAL:
|
---|
1399 | u64Now = TMRealGet(pVM);
|
---|
1400 | break;
|
---|
1401 | default:
|
---|
1402 | AssertFatalMsgFailed(("%d\n", enmClock));
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | if (pu64Now)
|
---|
1406 | *pu64Now = u64Now;
|
---|
1407 | return u64Now;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 |
|
---|
1411 | /**
|
---|
1412 | * Optimized TMTimerSetRelative code path.
|
---|
1413 | *
|
---|
1414 | * @returns VBox status code.
|
---|
1415 | *
|
---|
1416 | * @param pVM The cross context VM structure.
|
---|
1417 | * @param pTimer The timer handle.
|
---|
1418 | * @param cTicksToNext Clock ticks until the next time expiration.
|
---|
1419 | * @param pu64Now Where to return the current time stamp used.
|
---|
1420 | * Optional.
|
---|
1421 | */
|
---|
1422 | static int tmTimerSetRelativeOptimizedStart(PVM pVM, PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now)
|
---|
1423 | {
|
---|
1424 | Assert(!pTimer->offPrev);
|
---|
1425 | Assert(!pTimer->offNext);
|
---|
1426 | Assert(pTimer->enmState == TMTIMERSTATE_ACTIVE);
|
---|
1427 |
|
---|
1428 | /*
|
---|
1429 | * Calculate and set the expiration time.
|
---|
1430 | */
|
---|
1431 | TMCLOCK const enmClock = pTimer->enmClock;
|
---|
1432 | uint64_t const u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
|
---|
1433 | pTimer->u64Expire = u64Expire;
|
---|
1434 | Log2(("tmTimerSetRelativeOptimizedStart: %p:{.pszDesc='%s', .u64Expire=%'RU64} cTicksToNext=%'RU64\n", pTimer, R3STRING(pTimer->pszDesc), u64Expire, cTicksToNext));
|
---|
1435 |
|
---|
1436 | /*
|
---|
1437 | * Link the timer into the active list.
|
---|
1438 | */
|
---|
1439 | DBGFTRACE_U64_TAG2(pVM, u64Expire, "tmTimerSetRelativeOptimizedStart", R3STRING(pTimer->pszDesc));
|
---|
1440 | tmTimerQueueLinkActive(&pVM->tm.s.CTX_SUFF(paTimerQueues)[enmClock], pTimer, u64Expire);
|
---|
1441 |
|
---|
1442 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeOpt);
|
---|
1443 | TM_UNLOCK_TIMERS(pVM);
|
---|
1444 | return VINF_SUCCESS;
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 |
|
---|
1448 | /**
|
---|
1449 | * TMTimerSetRelative for the virtual sync timer queue.
|
---|
1450 | *
|
---|
1451 | * This employs a greatly simplified state machine by always acquiring the
|
---|
1452 | * queue lock and bypassing the scheduling list.
|
---|
1453 | *
|
---|
1454 | * @returns VBox status code
|
---|
1455 | * @param pVM The cross context VM structure.
|
---|
1456 | * @param pTimer The timer to (re-)arm.
|
---|
1457 | * @param cTicksToNext Clock ticks until the next time expiration.
|
---|
1458 | * @param pu64Now Where to return the current time stamp used.
|
---|
1459 | * Optional.
|
---|
1460 | */
|
---|
1461 | static int tmTimerVirtualSyncSetRelative(PVM pVM, PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now)
|
---|
1462 | {
|
---|
1463 | STAM_PROFILE_START(pVM->tm.s.CTX_SUFF_Z(StatTimerSetRelativeVs), a);
|
---|
1464 | VM_ASSERT_EMT(pVM);
|
---|
1465 | TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer);
|
---|
1466 | int rc = PDMCritSectEnter(&pVM->tm.s.VirtualSyncLock, VINF_SUCCESS);
|
---|
1467 | AssertRCReturn(rc, rc);
|
---|
1468 |
|
---|
1469 | /* Calculate the expiration tick. */
|
---|
1470 | uint64_t u64Expire = TMVirtualSyncGetNoCheck(pVM);
|
---|
1471 | if (pu64Now)
|
---|
1472 | *pu64Now = u64Expire;
|
---|
1473 | u64Expire += cTicksToNext;
|
---|
1474 |
|
---|
1475 | /* Update the timer. */
|
---|
1476 | PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC];
|
---|
1477 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1478 | switch (enmState)
|
---|
1479 | {
|
---|
1480 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
1481 | case TMTIMERSTATE_STOPPED:
|
---|
1482 | if (enmState == TMTIMERSTATE_EXPIRED_DELIVER)
|
---|
1483 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeVsStExpDeliver);
|
---|
1484 | else
|
---|
1485 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeVsStStopped);
|
---|
1486 | pTimer->u64Expire = u64Expire;
|
---|
1487 | TM_SET_STATE(pTimer, TMTIMERSTATE_ACTIVE);
|
---|
1488 | tmTimerQueueLinkActive(pQueue, pTimer, u64Expire);
|
---|
1489 | rc = VINF_SUCCESS;
|
---|
1490 | break;
|
---|
1491 |
|
---|
1492 | case TMTIMERSTATE_ACTIVE:
|
---|
1493 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeVsStActive);
|
---|
1494 | tmTimerQueueUnlinkActive(pQueue, pTimer);
|
---|
1495 | pTimer->u64Expire = u64Expire;
|
---|
1496 | tmTimerQueueLinkActive(pQueue, pTimer, u64Expire);
|
---|
1497 | rc = VINF_SUCCESS;
|
---|
1498 | break;
|
---|
1499 |
|
---|
1500 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1501 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1502 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1503 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1504 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
1505 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1506 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1507 | case TMTIMERSTATE_DESTROY:
|
---|
1508 | case TMTIMERSTATE_FREE:
|
---|
1509 | AssertLogRelMsgFailed(("Invalid timer state %s: %s\n", tmTimerState(enmState), R3STRING(pTimer->pszDesc)));
|
---|
1510 | rc = VERR_TM_INVALID_STATE;
|
---|
1511 | break;
|
---|
1512 |
|
---|
1513 | default:
|
---|
1514 | AssertMsgFailed(("Unknown timer state %d: %s\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1515 | rc = VERR_TM_UNKNOWN_STATE;
|
---|
1516 | break;
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetRelativeVs), a);
|
---|
1520 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
1521 | return rc;
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 |
|
---|
1525 | /**
|
---|
1526 | * Arm a timer with a expire time relative to the current time.
|
---|
1527 | *
|
---|
1528 | * @returns VBox status code.
|
---|
1529 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1530 | * @param cTicksToNext Clock ticks until the next time expiration.
|
---|
1531 | * @param pu64Now Where to return the current time stamp used.
|
---|
1532 | * Optional.
|
---|
1533 | */
|
---|
1534 | VMMDECL(int) TMTimerSetRelative(PTMTIMER pTimer, uint64_t cTicksToNext, uint64_t *pu64Now)
|
---|
1535 | {
|
---|
1536 | PVM pVM = pTimer->CTX_SUFF(pVM);
|
---|
1537 |
|
---|
1538 | /* Treat virtual sync timers specially. */
|
---|
1539 | if (pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
1540 | return tmTimerVirtualSyncSetRelative(pVM, pTimer, cTicksToNext, pu64Now);
|
---|
1541 |
|
---|
1542 | STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
|
---|
1543 | TMTIMER_ASSERT_CRITSECT(pTimer);
|
---|
1544 |
|
---|
1545 | DBGFTRACE_U64_TAG2(pVM, cTicksToNext, "TMTimerSetRelative", R3STRING(pTimer->pszDesc));
|
---|
1546 |
|
---|
1547 | #ifdef VBOX_WITH_STATISTICS
|
---|
1548 | /*
|
---|
1549 | * Gather optimization info.
|
---|
1550 | */
|
---|
1551 | STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelative);
|
---|
1552 | TMTIMERSTATE enmOrgState = pTimer->enmState;
|
---|
1553 | switch (enmOrgState)
|
---|
1554 | {
|
---|
1555 | case TMTIMERSTATE_STOPPED: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStStopped); break;
|
---|
1556 | case TMTIMERSTATE_EXPIRED_DELIVER: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStExpDeliver); break;
|
---|
1557 | case TMTIMERSTATE_ACTIVE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStActive); break;
|
---|
1558 | case TMTIMERSTATE_PENDING_STOP: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendStop); break;
|
---|
1559 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendStopSched); break;
|
---|
1560 | case TMTIMERSTATE_PENDING_SCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendSched); break;
|
---|
1561 | case TMTIMERSTATE_PENDING_RESCHEDULE: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStPendResched); break;
|
---|
1562 | default: STAM_COUNTER_INC(&pVM->tm.s.StatTimerSetRelativeStOther); break;
|
---|
1563 | }
|
---|
1564 | #endif
|
---|
1565 |
|
---|
1566 | /*
|
---|
1567 | * Try to take the TM lock and optimize the common cases.
|
---|
1568 | *
|
---|
1569 | * With the TM lock we can safely make optimizations like immediate
|
---|
1570 | * scheduling and we can also be 100% sure that we're not racing the
|
---|
1571 | * running of the timer queues. As an additional restraint we require the
|
---|
1572 | * timer to have a critical section associated with to be 100% there aren't
|
---|
1573 | * concurrent operations on the timer. (This latter isn't necessary any
|
---|
1574 | * longer as this isn't supported for any timers, critsect or not.)
|
---|
1575 | *
|
---|
1576 | * Note! Lock ordering doesn't apply when we only tries to
|
---|
1577 | * get the innermost locks.
|
---|
1578 | */
|
---|
1579 | bool fOwnTMLock = RT_SUCCESS_NP(TM_TRY_LOCK_TIMERS(pVM));
|
---|
1580 | #if 1
|
---|
1581 | if ( fOwnTMLock
|
---|
1582 | && pTimer->pCritSect)
|
---|
1583 | {
|
---|
1584 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1585 | if (RT_LIKELY( ( enmState == TMTIMERSTATE_EXPIRED_DELIVER
|
---|
1586 | || enmState == TMTIMERSTATE_STOPPED)
|
---|
1587 | && tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, enmState)))
|
---|
1588 | {
|
---|
1589 | tmTimerSetRelativeOptimizedStart(pVM, pTimer, cTicksToNext, pu64Now);
|
---|
1590 | STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
|
---|
1591 | return VINF_SUCCESS;
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | /* Optimize other states when it becomes necessary. */
|
---|
1595 | }
|
---|
1596 | #endif
|
---|
1597 |
|
---|
1598 | /*
|
---|
1599 | * Unoptimized path.
|
---|
1600 | */
|
---|
1601 | int rc;
|
---|
1602 | TMCLOCK const enmClock = pTimer->enmClock;
|
---|
1603 | for (int cRetries = 1000; ; cRetries--)
|
---|
1604 | {
|
---|
1605 | /*
|
---|
1606 | * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
|
---|
1607 | */
|
---|
1608 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1609 | switch (enmState)
|
---|
1610 | {
|
---|
1611 | case TMTIMERSTATE_STOPPED:
|
---|
1612 | if (enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
1613 | {
|
---|
1614 | /** @todo To fix assertion in tmR3TimerQueueRunVirtualSync:
|
---|
1615 | * Figure a safe way of activating this timer while the queue is
|
---|
1616 | * being run.
|
---|
1617 | * (99.9% sure this that the assertion is caused by DevAPIC.cpp
|
---|
1618 | * re-starting the timer in response to a initial_count write.) */
|
---|
1619 | }
|
---|
1620 | /* fall thru */
|
---|
1621 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
1622 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
|
---|
1623 | {
|
---|
1624 | Assert(!pTimer->offPrev);
|
---|
1625 | Assert(!pTimer->offNext);
|
---|
1626 | pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
|
---|
1627 | Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [EXP/STOP]\n",
|
---|
1628 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), pTimer->u64Expire, cRetries));
|
---|
1629 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
|
---|
1630 | tmSchedule(pTimer);
|
---|
1631 | rc = VINF_SUCCESS;
|
---|
1632 | break;
|
---|
1633 | }
|
---|
1634 | rc = VERR_TRY_AGAIN;
|
---|
1635 | break;
|
---|
1636 |
|
---|
1637 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1638 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1639 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
|
---|
1640 | {
|
---|
1641 | pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
|
---|
1642 | Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [PEND_SCHED]\n",
|
---|
1643 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), pTimer->u64Expire, cRetries));
|
---|
1644 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
|
---|
1645 | tmSchedule(pTimer);
|
---|
1646 | rc = VINF_SUCCESS;
|
---|
1647 | break;
|
---|
1648 | }
|
---|
1649 | rc = VERR_TRY_AGAIN;
|
---|
1650 | break;
|
---|
1651 |
|
---|
1652 |
|
---|
1653 | case TMTIMERSTATE_ACTIVE:
|
---|
1654 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
|
---|
1655 | {
|
---|
1656 | pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
|
---|
1657 | Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [ACTIVE]\n",
|
---|
1658 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), pTimer->u64Expire, cRetries));
|
---|
1659 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
|
---|
1660 | tmSchedule(pTimer);
|
---|
1661 | rc = VINF_SUCCESS;
|
---|
1662 | break;
|
---|
1663 | }
|
---|
1664 | rc = VERR_TRY_AGAIN;
|
---|
1665 | break;
|
---|
1666 |
|
---|
1667 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1668 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1669 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
|
---|
1670 | {
|
---|
1671 | pTimer->u64Expire = cTicksToNext + tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
|
---|
1672 | Log2(("TMTimerSetRelative: %p:{.enmState=%s, .pszDesc='%s', .u64Expire=%'RU64} cRetries=%d [PEND_RESCH/STOP]\n",
|
---|
1673 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), pTimer->u64Expire, cRetries));
|
---|
1674 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
|
---|
1675 | tmSchedule(pTimer);
|
---|
1676 | rc = VINF_SUCCESS;
|
---|
1677 | break;
|
---|
1678 | }
|
---|
1679 | rc = VERR_TRY_AGAIN;
|
---|
1680 | break;
|
---|
1681 |
|
---|
1682 |
|
---|
1683 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
1684 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1685 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1686 | #ifdef IN_RING3
|
---|
1687 | if (!RTThreadYield())
|
---|
1688 | RTThreadSleep(1);
|
---|
1689 | #else
|
---|
1690 | /** @todo call host context and yield after a couple of iterations */
|
---|
1691 | #endif
|
---|
1692 | rc = VERR_TRY_AGAIN;
|
---|
1693 | break;
|
---|
1694 |
|
---|
1695 | /*
|
---|
1696 | * Invalid states.
|
---|
1697 | */
|
---|
1698 | case TMTIMERSTATE_DESTROY:
|
---|
1699 | case TMTIMERSTATE_FREE:
|
---|
1700 | AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1701 | rc = VERR_TM_INVALID_STATE;
|
---|
1702 | break;
|
---|
1703 |
|
---|
1704 | default:
|
---|
1705 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1706 | rc = VERR_TM_UNKNOWN_STATE;
|
---|
1707 | break;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | /* switch + loop is tedious to break out of. */
|
---|
1711 | if (rc == VINF_SUCCESS)
|
---|
1712 | break;
|
---|
1713 |
|
---|
1714 | if (rc != VERR_TRY_AGAIN)
|
---|
1715 | {
|
---|
1716 | tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
|
---|
1717 | break;
|
---|
1718 | }
|
---|
1719 | if (cRetries <= 0)
|
---|
1720 | {
|
---|
1721 | AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
|
---|
1722 | rc = VERR_TM_TIMER_UNSTABLE_STATE;
|
---|
1723 | tmTimerSetRelativeNowWorker(pVM, enmClock, pu64Now);
|
---|
1724 | break;
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | /*
|
---|
1728 | * Retry to gain locks.
|
---|
1729 | */
|
---|
1730 | if (!fOwnTMLock)
|
---|
1731 | fOwnTMLock = RT_SUCCESS_NP(TM_TRY_LOCK_TIMERS(pVM));
|
---|
1732 |
|
---|
1733 | } /* for (;;) */
|
---|
1734 |
|
---|
1735 | /*
|
---|
1736 | * Clean up and return.
|
---|
1737 | */
|
---|
1738 | if (fOwnTMLock)
|
---|
1739 | TM_UNLOCK_TIMERS(pVM);
|
---|
1740 |
|
---|
1741 | STAM_PROFILE_STOP(&pTimer->CTX_SUFF(pVM)->tm.s.CTX_SUFF_Z(StatTimerSetRelative), a);
|
---|
1742 | return rc;
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 |
|
---|
1746 | /**
|
---|
1747 | * Drops a hint about the frequency of the timer.
|
---|
1748 | *
|
---|
1749 | * This is used by TM and the VMM to calculate how often guest execution needs
|
---|
1750 | * to be interrupted. The hint is automatically cleared by TMTimerStop.
|
---|
1751 | *
|
---|
1752 | * @returns VBox status code.
|
---|
1753 | * @param pTimer Timer handle as returned by one of the create
|
---|
1754 | * functions.
|
---|
1755 | * @param uHzHint The frequency hint. Pass 0 to clear the hint.
|
---|
1756 | *
|
---|
1757 | * @remarks We're using an integer hertz value here since anything above 1 HZ
|
---|
1758 | * is not going to be any trouble satisfying scheduling wise. The
|
---|
1759 | * range where it makes sense is >= 100 HZ.
|
---|
1760 | */
|
---|
1761 | VMMDECL(int) TMTimerSetFrequencyHint(PTMTIMER pTimer, uint32_t uHzHint)
|
---|
1762 | {
|
---|
1763 | TMTIMER_ASSERT_CRITSECT(pTimer);
|
---|
1764 |
|
---|
1765 | uint32_t const uHzOldHint = pTimer->uHzHint;
|
---|
1766 | pTimer->uHzHint = uHzHint;
|
---|
1767 |
|
---|
1768 | PVM pVM = pTimer->CTX_SUFF(pVM);
|
---|
1769 | uint32_t const uMaxHzHint = pVM->tm.s.uMaxHzHint;
|
---|
1770 | if ( uHzHint > uMaxHzHint
|
---|
1771 | || uHzOldHint >= uMaxHzHint)
|
---|
1772 | ASMAtomicWriteBool(&pVM->tm.s.fHzHintNeedsUpdating, true);
|
---|
1773 |
|
---|
1774 | return VINF_SUCCESS;
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 |
|
---|
1778 | /**
|
---|
1779 | * TMTimerStop for the virtual sync timer queue.
|
---|
1780 | *
|
---|
1781 | * This employs a greatly simplified state machine by always acquiring the
|
---|
1782 | * queue lock and bypassing the scheduling list.
|
---|
1783 | *
|
---|
1784 | * @returns VBox status code
|
---|
1785 | * @param pVM The cross context VM structure.
|
---|
1786 | * @param pTimer The timer handle.
|
---|
1787 | */
|
---|
1788 | static int tmTimerVirtualSyncStop(PVM pVM, PTMTIMER pTimer)
|
---|
1789 | {
|
---|
1790 | STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerStopVs), a);
|
---|
1791 | VM_ASSERT_EMT(pVM);
|
---|
1792 | TMTIMER_ASSERT_SYNC_CRITSECT_ORDER(pVM, pTimer);
|
---|
1793 | int rc = PDMCritSectEnter(&pVM->tm.s.VirtualSyncLock, VINF_SUCCESS);
|
---|
1794 | AssertRCReturn(rc, rc);
|
---|
1795 |
|
---|
1796 | /* Reset the HZ hint. */
|
---|
1797 | if (pTimer->uHzHint)
|
---|
1798 | {
|
---|
1799 | if (pTimer->uHzHint >= pVM->tm.s.uMaxHzHint)
|
---|
1800 | ASMAtomicWriteBool(&pVM->tm.s.fHzHintNeedsUpdating, true);
|
---|
1801 | pTimer->uHzHint = 0;
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | /* Update the timer state. */
|
---|
1805 | PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC];
|
---|
1806 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1807 | switch (enmState)
|
---|
1808 | {
|
---|
1809 | case TMTIMERSTATE_ACTIVE:
|
---|
1810 | tmTimerQueueUnlinkActive(pQueue, pTimer);
|
---|
1811 | TM_SET_STATE(pTimer, TMTIMERSTATE_STOPPED);
|
---|
1812 | rc = VINF_SUCCESS;
|
---|
1813 | break;
|
---|
1814 |
|
---|
1815 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
1816 | TM_SET_STATE(pTimer, TMTIMERSTATE_STOPPED);
|
---|
1817 | rc = VINF_SUCCESS;
|
---|
1818 | break;
|
---|
1819 |
|
---|
1820 | case TMTIMERSTATE_STOPPED:
|
---|
1821 | rc = VINF_SUCCESS;
|
---|
1822 | break;
|
---|
1823 |
|
---|
1824 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1825 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1826 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1827 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1828 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
1829 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1830 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1831 | case TMTIMERSTATE_DESTROY:
|
---|
1832 | case TMTIMERSTATE_FREE:
|
---|
1833 | AssertLogRelMsgFailed(("Invalid timer state %s: %s\n", tmTimerState(enmState), R3STRING(pTimer->pszDesc)));
|
---|
1834 | rc = VERR_TM_INVALID_STATE;
|
---|
1835 | break;
|
---|
1836 |
|
---|
1837 | default:
|
---|
1838 | AssertMsgFailed(("Unknown timer state %d: %s\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1839 | rc = VERR_TM_UNKNOWN_STATE;
|
---|
1840 | break;
|
---|
1841 | }
|
---|
1842 |
|
---|
1843 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStopVs), a);
|
---|
1844 | PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
|
---|
1845 | return rc;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 |
|
---|
1849 | /**
|
---|
1850 | * Stop the timer.
|
---|
1851 | * Use TMR3TimerArm() to "un-stop" the timer.
|
---|
1852 | *
|
---|
1853 | * @returns VBox status code.
|
---|
1854 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1855 | */
|
---|
1856 | VMMDECL(int) TMTimerStop(PTMTIMER pTimer)
|
---|
1857 | {
|
---|
1858 | PVM pVM = pTimer->CTX_SUFF(pVM);
|
---|
1859 |
|
---|
1860 | /* Treat virtual sync timers specially. */
|
---|
1861 | if (pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC)
|
---|
1862 | return tmTimerVirtualSyncStop(pVM, pTimer);
|
---|
1863 |
|
---|
1864 | STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
|
---|
1865 | TMTIMER_ASSERT_CRITSECT(pTimer);
|
---|
1866 |
|
---|
1867 | /*
|
---|
1868 | * Reset the HZ hint.
|
---|
1869 | */
|
---|
1870 | if (pTimer->uHzHint)
|
---|
1871 | {
|
---|
1872 | if (pTimer->uHzHint >= pVM->tm.s.uMaxHzHint)
|
---|
1873 | ASMAtomicWriteBool(&pVM->tm.s.fHzHintNeedsUpdating, true);
|
---|
1874 | pTimer->uHzHint = 0;
|
---|
1875 | }
|
---|
1876 |
|
---|
1877 | /** @todo see if this function needs optimizing. */
|
---|
1878 | int cRetries = 1000;
|
---|
1879 | do
|
---|
1880 | {
|
---|
1881 | /*
|
---|
1882 | * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
|
---|
1883 | */
|
---|
1884 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1885 | Log2(("TMTimerStop: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
|
---|
1886 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries));
|
---|
1887 | switch (enmState)
|
---|
1888 | {
|
---|
1889 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
1890 | //AssertMsgFailed(("You don't stop an expired timer dude!\n"));
|
---|
1891 | return VERR_INVALID_PARAMETER;
|
---|
1892 |
|
---|
1893 | case TMTIMERSTATE_STOPPED:
|
---|
1894 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1895 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1896 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
|
---|
1897 | return VINF_SUCCESS;
|
---|
1898 |
|
---|
1899 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1900 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, enmState))
|
---|
1901 | {
|
---|
1902 | tmSchedule(pTimer);
|
---|
1903 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
|
---|
1904 | return VINF_SUCCESS;
|
---|
1905 | }
|
---|
1906 |
|
---|
1907 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1908 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
|
---|
1909 | {
|
---|
1910 | tmSchedule(pTimer);
|
---|
1911 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
|
---|
1912 | return VINF_SUCCESS;
|
---|
1913 | }
|
---|
1914 | break;
|
---|
1915 |
|
---|
1916 | case TMTIMERSTATE_ACTIVE:
|
---|
1917 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
|
---|
1918 | {
|
---|
1919 | tmSchedule(pTimer);
|
---|
1920 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
|
---|
1921 | return VINF_SUCCESS;
|
---|
1922 | }
|
---|
1923 | break;
|
---|
1924 |
|
---|
1925 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
1926 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1927 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1928 | #ifdef IN_RING3
|
---|
1929 | if (!RTThreadYield())
|
---|
1930 | RTThreadSleep(1);
|
---|
1931 | #else
|
---|
1932 | /** @todo call host and yield cpu after a while. */
|
---|
1933 | #endif
|
---|
1934 | break;
|
---|
1935 |
|
---|
1936 | /*
|
---|
1937 | * Invalid states.
|
---|
1938 | */
|
---|
1939 | case TMTIMERSTATE_DESTROY:
|
---|
1940 | case TMTIMERSTATE_FREE:
|
---|
1941 | AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1942 | return VERR_TM_INVALID_STATE;
|
---|
1943 | default:
|
---|
1944 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1945 | return VERR_TM_UNKNOWN_STATE;
|
---|
1946 | }
|
---|
1947 | } while (cRetries-- > 0);
|
---|
1948 |
|
---|
1949 | AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
|
---|
1950 | STAM_PROFILE_STOP(&pVM->tm.s.CTX_SUFF_Z(StatTimerStop), a);
|
---|
1951 | return VERR_TM_TIMER_UNSTABLE_STATE;
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 |
|
---|
1955 | /**
|
---|
1956 | * Get the current clock time.
|
---|
1957 | * Handy for calculating the new expire time.
|
---|
1958 | *
|
---|
1959 | * @returns Current clock time.
|
---|
1960 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1961 | */
|
---|
1962 | VMMDECL(uint64_t) TMTimerGet(PTMTIMER pTimer)
|
---|
1963 | {
|
---|
1964 | PVM pVM = pTimer->CTX_SUFF(pVM);
|
---|
1965 |
|
---|
1966 | uint64_t u64;
|
---|
1967 | switch (pTimer->enmClock)
|
---|
1968 | {
|
---|
1969 | case TMCLOCK_VIRTUAL:
|
---|
1970 | u64 = TMVirtualGet(pVM);
|
---|
1971 | break;
|
---|
1972 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
1973 | u64 = TMVirtualSyncGet(pVM);
|
---|
1974 | break;
|
---|
1975 | case TMCLOCK_REAL:
|
---|
1976 | u64 = TMRealGet(pVM);
|
---|
1977 | break;
|
---|
1978 | default:
|
---|
1979 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
1980 | return UINT64_MAX;
|
---|
1981 | }
|
---|
1982 | //Log2(("TMTimerGet: returns %'RU64 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
1983 | // u64, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
1984 | return u64;
|
---|
1985 | }
|
---|
1986 |
|
---|
1987 |
|
---|
1988 | /**
|
---|
1989 | * Get the frequency of the timer clock.
|
---|
1990 | *
|
---|
1991 | * @returns Clock frequency (as Hz of course).
|
---|
1992 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1993 | */
|
---|
1994 | VMMDECL(uint64_t) TMTimerGetFreq(PTMTIMER pTimer)
|
---|
1995 | {
|
---|
1996 | switch (pTimer->enmClock)
|
---|
1997 | {
|
---|
1998 | case TMCLOCK_VIRTUAL:
|
---|
1999 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2000 | return TMCLOCK_FREQ_VIRTUAL;
|
---|
2001 |
|
---|
2002 | case TMCLOCK_REAL:
|
---|
2003 | return TMCLOCK_FREQ_REAL;
|
---|
2004 |
|
---|
2005 | default:
|
---|
2006 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2007 | return 0;
|
---|
2008 | }
|
---|
2009 | }
|
---|
2010 |
|
---|
2011 |
|
---|
2012 | /**
|
---|
2013 | * Get the expire time of the timer.
|
---|
2014 | * Only valid for active timers.
|
---|
2015 | *
|
---|
2016 | * @returns Expire time of the timer.
|
---|
2017 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2018 | */
|
---|
2019 | VMMDECL(uint64_t) TMTimerGetExpire(PTMTIMER pTimer)
|
---|
2020 | {
|
---|
2021 | TMTIMER_ASSERT_CRITSECT(pTimer);
|
---|
2022 | int cRetries = 1000;
|
---|
2023 | do
|
---|
2024 | {
|
---|
2025 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
2026 | switch (enmState)
|
---|
2027 | {
|
---|
2028 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
2029 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
2030 | case TMTIMERSTATE_STOPPED:
|
---|
2031 | case TMTIMERSTATE_PENDING_STOP:
|
---|
2032 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
2033 | Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
2034 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
2035 | return ~(uint64_t)0;
|
---|
2036 |
|
---|
2037 | case TMTIMERSTATE_ACTIVE:
|
---|
2038 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
2039 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
2040 | Log2(("TMTimerGetExpire: returns %'RU64 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
2041 | pTimer->u64Expire, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
2042 | return pTimer->u64Expire;
|
---|
2043 |
|
---|
2044 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
2045 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
2046 | #ifdef IN_RING3
|
---|
2047 | if (!RTThreadYield())
|
---|
2048 | RTThreadSleep(1);
|
---|
2049 | #endif
|
---|
2050 | break;
|
---|
2051 |
|
---|
2052 | /*
|
---|
2053 | * Invalid states.
|
---|
2054 | */
|
---|
2055 | case TMTIMERSTATE_DESTROY:
|
---|
2056 | case TMTIMERSTATE_FREE:
|
---|
2057 | AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
2058 | Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
2059 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
2060 | return ~(uint64_t)0;
|
---|
2061 | default:
|
---|
2062 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
2063 | return ~(uint64_t)0;
|
---|
2064 | }
|
---|
2065 | } while (cRetries-- > 0);
|
---|
2066 |
|
---|
2067 | AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
|
---|
2068 | Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
2069 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
2070 | return ~(uint64_t)0;
|
---|
2071 | }
|
---|
2072 |
|
---|
2073 |
|
---|
2074 | /**
|
---|
2075 | * Checks if a timer is active or not.
|
---|
2076 | *
|
---|
2077 | * @returns True if active.
|
---|
2078 | * @returns False if not active.
|
---|
2079 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2080 | */
|
---|
2081 | VMMDECL(bool) TMTimerIsActive(PTMTIMER pTimer)
|
---|
2082 | {
|
---|
2083 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
2084 | switch (enmState)
|
---|
2085 | {
|
---|
2086 | case TMTIMERSTATE_STOPPED:
|
---|
2087 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
2088 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
2089 | case TMTIMERSTATE_PENDING_STOP:
|
---|
2090 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
2091 | Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
2092 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
2093 | return false;
|
---|
2094 |
|
---|
2095 | case TMTIMERSTATE_ACTIVE:
|
---|
2096 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
2097 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
2098 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
2099 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
2100 | Log2(("TMTimerIsActive: returns true (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
2101 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
2102 | return true;
|
---|
2103 |
|
---|
2104 | /*
|
---|
2105 | * Invalid states.
|
---|
2106 | */
|
---|
2107 | case TMTIMERSTATE_DESTROY:
|
---|
2108 | case TMTIMERSTATE_FREE:
|
---|
2109 | AssertMsgFailed(("Invalid timer state %s (%s)\n", tmTimerState(enmState), R3STRING(pTimer->pszDesc)));
|
---|
2110 | Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
2111 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
2112 | return false;
|
---|
2113 | default:
|
---|
2114 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
2115 | return false;
|
---|
2116 | }
|
---|
2117 | }
|
---|
2118 |
|
---|
2119 |
|
---|
2120 | /* -=-=-=-=-=-=- Convenience APIs -=-=-=-=-=-=- */
|
---|
2121 |
|
---|
2122 |
|
---|
2123 | /**
|
---|
2124 | * Arm a timer with a (new) expire time relative to current time.
|
---|
2125 | *
|
---|
2126 | * @returns VBox status code.
|
---|
2127 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2128 | * @param cMilliesToNext Number of milliseconds to the next tick.
|
---|
2129 | */
|
---|
2130 | VMMDECL(int) TMTimerSetMillies(PTMTIMER pTimer, uint32_t cMilliesToNext)
|
---|
2131 | {
|
---|
2132 | switch (pTimer->enmClock)
|
---|
2133 | {
|
---|
2134 | case TMCLOCK_VIRTUAL:
|
---|
2135 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2136 | return TMTimerSetRelative(pTimer, cMilliesToNext * UINT64_C(1000000), NULL);
|
---|
2137 |
|
---|
2138 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2139 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2140 | return TMTimerSetRelative(pTimer, cMilliesToNext * UINT64_C(1000000), NULL);
|
---|
2141 |
|
---|
2142 | case TMCLOCK_REAL:
|
---|
2143 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2144 | return TMTimerSetRelative(pTimer, cMilliesToNext, NULL);
|
---|
2145 |
|
---|
2146 | default:
|
---|
2147 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2148 | return VERR_TM_TIMER_BAD_CLOCK;
|
---|
2149 | }
|
---|
2150 | }
|
---|
2151 |
|
---|
2152 |
|
---|
2153 | /**
|
---|
2154 | * Arm a timer with a (new) expire time relative to current time.
|
---|
2155 | *
|
---|
2156 | * @returns VBox status code.
|
---|
2157 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2158 | * @param cMicrosToNext Number of microseconds to the next tick.
|
---|
2159 | */
|
---|
2160 | VMMDECL(int) TMTimerSetMicro(PTMTIMER pTimer, uint64_t cMicrosToNext)
|
---|
2161 | {
|
---|
2162 | switch (pTimer->enmClock)
|
---|
2163 | {
|
---|
2164 | case TMCLOCK_VIRTUAL:
|
---|
2165 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2166 | return TMTimerSetRelative(pTimer, cMicrosToNext * 1000, NULL);
|
---|
2167 |
|
---|
2168 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2169 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2170 | return TMTimerSetRelative(pTimer, cMicrosToNext * 1000, NULL);
|
---|
2171 |
|
---|
2172 | case TMCLOCK_REAL:
|
---|
2173 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2174 | return TMTimerSetRelative(pTimer, cMicrosToNext / 1000, NULL);
|
---|
2175 |
|
---|
2176 | default:
|
---|
2177 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2178 | return VERR_TM_TIMER_BAD_CLOCK;
|
---|
2179 | }
|
---|
2180 | }
|
---|
2181 |
|
---|
2182 |
|
---|
2183 | /**
|
---|
2184 | * Arm a timer with a (new) expire time relative to current time.
|
---|
2185 | *
|
---|
2186 | * @returns VBox status code.
|
---|
2187 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2188 | * @param cNanosToNext Number of nanoseconds to the next tick.
|
---|
2189 | */
|
---|
2190 | VMMDECL(int) TMTimerSetNano(PTMTIMER pTimer, uint64_t cNanosToNext)
|
---|
2191 | {
|
---|
2192 | switch (pTimer->enmClock)
|
---|
2193 | {
|
---|
2194 | case TMCLOCK_VIRTUAL:
|
---|
2195 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2196 | return TMTimerSetRelative(pTimer, cNanosToNext, NULL);
|
---|
2197 |
|
---|
2198 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2199 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2200 | return TMTimerSetRelative(pTimer, cNanosToNext, NULL);
|
---|
2201 |
|
---|
2202 | case TMCLOCK_REAL:
|
---|
2203 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2204 | return TMTimerSetRelative(pTimer, cNanosToNext / 1000000, NULL);
|
---|
2205 |
|
---|
2206 | default:
|
---|
2207 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2208 | return VERR_TM_TIMER_BAD_CLOCK;
|
---|
2209 | }
|
---|
2210 | }
|
---|
2211 |
|
---|
2212 |
|
---|
2213 | /**
|
---|
2214 | * Get the current clock time as nanoseconds.
|
---|
2215 | *
|
---|
2216 | * @returns The timer clock as nanoseconds.
|
---|
2217 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2218 | */
|
---|
2219 | VMMDECL(uint64_t) TMTimerGetNano(PTMTIMER pTimer)
|
---|
2220 | {
|
---|
2221 | return TMTimerToNano(pTimer, TMTimerGet(pTimer));
|
---|
2222 | }
|
---|
2223 |
|
---|
2224 |
|
---|
2225 | /**
|
---|
2226 | * Get the current clock time as microseconds.
|
---|
2227 | *
|
---|
2228 | * @returns The timer clock as microseconds.
|
---|
2229 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2230 | */
|
---|
2231 | VMMDECL(uint64_t) TMTimerGetMicro(PTMTIMER pTimer)
|
---|
2232 | {
|
---|
2233 | return TMTimerToMicro(pTimer, TMTimerGet(pTimer));
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 |
|
---|
2237 | /**
|
---|
2238 | * Get the current clock time as milliseconds.
|
---|
2239 | *
|
---|
2240 | * @returns The timer clock as milliseconds.
|
---|
2241 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2242 | */
|
---|
2243 | VMMDECL(uint64_t) TMTimerGetMilli(PTMTIMER pTimer)
|
---|
2244 | {
|
---|
2245 | return TMTimerToMilli(pTimer, TMTimerGet(pTimer));
|
---|
2246 | }
|
---|
2247 |
|
---|
2248 |
|
---|
2249 | /**
|
---|
2250 | * Converts the specified timer clock time to nanoseconds.
|
---|
2251 | *
|
---|
2252 | * @returns nanoseconds.
|
---|
2253 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2254 | * @param u64Ticks The clock ticks.
|
---|
2255 | * @remark There could be rounding errors here. We just do a simple integer divide
|
---|
2256 | * without any adjustments.
|
---|
2257 | */
|
---|
2258 | VMMDECL(uint64_t) TMTimerToNano(PTMTIMER pTimer, uint64_t u64Ticks)
|
---|
2259 | {
|
---|
2260 | switch (pTimer->enmClock)
|
---|
2261 | {
|
---|
2262 | case TMCLOCK_VIRTUAL:
|
---|
2263 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2264 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2265 | return u64Ticks;
|
---|
2266 |
|
---|
2267 | case TMCLOCK_REAL:
|
---|
2268 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2269 | return u64Ticks * 1000000;
|
---|
2270 |
|
---|
2271 | default:
|
---|
2272 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2273 | return 0;
|
---|
2274 | }
|
---|
2275 | }
|
---|
2276 |
|
---|
2277 |
|
---|
2278 | /**
|
---|
2279 | * Converts the specified timer clock time to microseconds.
|
---|
2280 | *
|
---|
2281 | * @returns microseconds.
|
---|
2282 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2283 | * @param u64Ticks The clock ticks.
|
---|
2284 | * @remark There could be rounding errors here. We just do a simple integer divide
|
---|
2285 | * without any adjustments.
|
---|
2286 | */
|
---|
2287 | VMMDECL(uint64_t) TMTimerToMicro(PTMTIMER pTimer, uint64_t u64Ticks)
|
---|
2288 | {
|
---|
2289 | switch (pTimer->enmClock)
|
---|
2290 | {
|
---|
2291 | case TMCLOCK_VIRTUAL:
|
---|
2292 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2293 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2294 | return u64Ticks / 1000;
|
---|
2295 |
|
---|
2296 | case TMCLOCK_REAL:
|
---|
2297 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2298 | return u64Ticks * 1000;
|
---|
2299 |
|
---|
2300 | default:
|
---|
2301 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2302 | return 0;
|
---|
2303 | }
|
---|
2304 | }
|
---|
2305 |
|
---|
2306 |
|
---|
2307 | /**
|
---|
2308 | * Converts the specified timer clock time to milliseconds.
|
---|
2309 | *
|
---|
2310 | * @returns milliseconds.
|
---|
2311 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2312 | * @param u64Ticks The clock ticks.
|
---|
2313 | * @remark There could be rounding errors here. We just do a simple integer divide
|
---|
2314 | * without any adjustments.
|
---|
2315 | */
|
---|
2316 | VMMDECL(uint64_t) TMTimerToMilli(PTMTIMER pTimer, uint64_t u64Ticks)
|
---|
2317 | {
|
---|
2318 | switch (pTimer->enmClock)
|
---|
2319 | {
|
---|
2320 | case TMCLOCK_VIRTUAL:
|
---|
2321 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2322 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2323 | return u64Ticks / 1000000;
|
---|
2324 |
|
---|
2325 | case TMCLOCK_REAL:
|
---|
2326 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2327 | return u64Ticks;
|
---|
2328 |
|
---|
2329 | default:
|
---|
2330 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2331 | return 0;
|
---|
2332 | }
|
---|
2333 | }
|
---|
2334 |
|
---|
2335 |
|
---|
2336 | /**
|
---|
2337 | * Converts the specified nanosecond timestamp to timer clock ticks.
|
---|
2338 | *
|
---|
2339 | * @returns timer clock ticks.
|
---|
2340 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2341 | * @param cNanoSecs The nanosecond value ticks to convert.
|
---|
2342 | * @remark There could be rounding and overflow errors here.
|
---|
2343 | */
|
---|
2344 | VMMDECL(uint64_t) TMTimerFromNano(PTMTIMER pTimer, uint64_t cNanoSecs)
|
---|
2345 | {
|
---|
2346 | switch (pTimer->enmClock)
|
---|
2347 | {
|
---|
2348 | case TMCLOCK_VIRTUAL:
|
---|
2349 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2350 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2351 | return cNanoSecs;
|
---|
2352 |
|
---|
2353 | case TMCLOCK_REAL:
|
---|
2354 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2355 | return cNanoSecs / 1000000;
|
---|
2356 |
|
---|
2357 | default:
|
---|
2358 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2359 | return 0;
|
---|
2360 | }
|
---|
2361 | }
|
---|
2362 |
|
---|
2363 |
|
---|
2364 | /**
|
---|
2365 | * Converts the specified microsecond timestamp to timer clock ticks.
|
---|
2366 | *
|
---|
2367 | * @returns timer clock ticks.
|
---|
2368 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2369 | * @param cMicroSecs The microsecond value ticks to convert.
|
---|
2370 | * @remark There could be rounding and overflow errors here.
|
---|
2371 | */
|
---|
2372 | VMMDECL(uint64_t) TMTimerFromMicro(PTMTIMER pTimer, uint64_t cMicroSecs)
|
---|
2373 | {
|
---|
2374 | switch (pTimer->enmClock)
|
---|
2375 | {
|
---|
2376 | case TMCLOCK_VIRTUAL:
|
---|
2377 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2378 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2379 | return cMicroSecs * 1000;
|
---|
2380 |
|
---|
2381 | case TMCLOCK_REAL:
|
---|
2382 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2383 | return cMicroSecs / 1000;
|
---|
2384 |
|
---|
2385 | default:
|
---|
2386 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2387 | return 0;
|
---|
2388 | }
|
---|
2389 | }
|
---|
2390 |
|
---|
2391 |
|
---|
2392 | /**
|
---|
2393 | * Converts the specified millisecond timestamp to timer clock ticks.
|
---|
2394 | *
|
---|
2395 | * @returns timer clock ticks.
|
---|
2396 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
2397 | * @param cMilliSecs The millisecond value ticks to convert.
|
---|
2398 | * @remark There could be rounding and overflow errors here.
|
---|
2399 | */
|
---|
2400 | VMMDECL(uint64_t) TMTimerFromMilli(PTMTIMER pTimer, uint64_t cMilliSecs)
|
---|
2401 | {
|
---|
2402 | switch (pTimer->enmClock)
|
---|
2403 | {
|
---|
2404 | case TMCLOCK_VIRTUAL:
|
---|
2405 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
2406 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
2407 | return cMilliSecs * 1000000;
|
---|
2408 |
|
---|
2409 | case TMCLOCK_REAL:
|
---|
2410 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
2411 | return cMilliSecs;
|
---|
2412 |
|
---|
2413 | default:
|
---|
2414 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
2415 | return 0;
|
---|
2416 | }
|
---|
2417 | }
|
---|
2418 |
|
---|
2419 |
|
---|
2420 | /**
|
---|
2421 | * Convert state to string.
|
---|
2422 | *
|
---|
2423 | * @returns Readonly status name.
|
---|
2424 | * @param enmState State.
|
---|
2425 | */
|
---|
2426 | const char *tmTimerState(TMTIMERSTATE enmState)
|
---|
2427 | {
|
---|
2428 | switch (enmState)
|
---|
2429 | {
|
---|
2430 | #define CASE(num, state) \
|
---|
2431 | case TMTIMERSTATE_##state: \
|
---|
2432 | AssertCompile(TMTIMERSTATE_##state == (num)); \
|
---|
2433 | return #num "-" #state
|
---|
2434 | CASE( 1,STOPPED);
|
---|
2435 | CASE( 2,ACTIVE);
|
---|
2436 | CASE( 3,EXPIRED_GET_UNLINK);
|
---|
2437 | CASE( 4,EXPIRED_DELIVER);
|
---|
2438 | CASE( 5,PENDING_STOP);
|
---|
2439 | CASE( 6,PENDING_STOP_SCHEDULE);
|
---|
2440 | CASE( 7,PENDING_SCHEDULE_SET_EXPIRE);
|
---|
2441 | CASE( 8,PENDING_SCHEDULE);
|
---|
2442 | CASE( 9,PENDING_RESCHEDULE_SET_EXPIRE);
|
---|
2443 | CASE(10,PENDING_RESCHEDULE);
|
---|
2444 | CASE(11,DESTROY);
|
---|
2445 | CASE(12,FREE);
|
---|
2446 | default:
|
---|
2447 | AssertMsgFailed(("Invalid state enmState=%d\n", enmState));
|
---|
2448 | return "Invalid state!";
|
---|
2449 | #undef CASE
|
---|
2450 | }
|
---|
2451 | }
|
---|
2452 |
|
---|
2453 |
|
---|
2454 | /**
|
---|
2455 | * Gets the highest frequency hint for all the important timers.
|
---|
2456 | *
|
---|
2457 | * @returns The highest frequency. 0 if no timers care.
|
---|
2458 | * @param pVM The cross context VM structure.
|
---|
2459 | */
|
---|
2460 | static uint32_t tmGetFrequencyHint(PVM pVM)
|
---|
2461 | {
|
---|
2462 | /*
|
---|
2463 | * Query the value, recalculate it if necessary.
|
---|
2464 | *
|
---|
2465 | * The "right" highest frequency value isn't so important that we'll block
|
---|
2466 | * waiting on the timer semaphore.
|
---|
2467 | */
|
---|
2468 | uint32_t uMaxHzHint = ASMAtomicUoReadU32(&pVM->tm.s.uMaxHzHint);
|
---|
2469 | if (RT_UNLIKELY(ASMAtomicReadBool(&pVM->tm.s.fHzHintNeedsUpdating)))
|
---|
2470 | {
|
---|
2471 | if (RT_SUCCESS(TM_TRY_LOCK_TIMERS(pVM)))
|
---|
2472 | {
|
---|
2473 | ASMAtomicWriteBool(&pVM->tm.s.fHzHintNeedsUpdating, false);
|
---|
2474 |
|
---|
2475 | /*
|
---|
2476 | * Loop over the timers associated with each clock.
|
---|
2477 | */
|
---|
2478 | uMaxHzHint = 0;
|
---|
2479 | for (int i = 0; i < TMCLOCK_MAX; i++)
|
---|
2480 | {
|
---|
2481 | PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[i];
|
---|
2482 | for (PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue); pCur; pCur = TMTIMER_GET_NEXT(pCur))
|
---|
2483 | {
|
---|
2484 | uint32_t uHzHint = ASMAtomicUoReadU32(&pCur->uHzHint);
|
---|
2485 | if (uHzHint > uMaxHzHint)
|
---|
2486 | {
|
---|
2487 | switch (pCur->enmState)
|
---|
2488 | {
|
---|
2489 | case TMTIMERSTATE_ACTIVE:
|
---|
2490 | case TMTIMERSTATE_EXPIRED_GET_UNLINK:
|
---|
2491 | case TMTIMERSTATE_EXPIRED_DELIVER:
|
---|
2492 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
2493 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
2494 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
2495 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
2496 | uMaxHzHint = uHzHint;
|
---|
2497 | break;
|
---|
2498 |
|
---|
2499 | case TMTIMERSTATE_STOPPED:
|
---|
2500 | case TMTIMERSTATE_PENDING_STOP:
|
---|
2501 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
2502 | case TMTIMERSTATE_DESTROY:
|
---|
2503 | case TMTIMERSTATE_FREE:
|
---|
2504 | break;
|
---|
2505 | /* no default, want gcc warnings when adding more states. */
|
---|
2506 | }
|
---|
2507 | }
|
---|
2508 | }
|
---|
2509 | }
|
---|
2510 | ASMAtomicWriteU32(&pVM->tm.s.uMaxHzHint, uMaxHzHint);
|
---|
2511 | Log(("tmGetFrequencyHint: New value %u Hz\n", uMaxHzHint));
|
---|
2512 | TM_UNLOCK_TIMERS(pVM);
|
---|
2513 | }
|
---|
2514 | }
|
---|
2515 | return uMaxHzHint;
|
---|
2516 | }
|
---|
2517 |
|
---|
2518 |
|
---|
2519 | /**
|
---|
2520 | * Calculates a host timer frequency that would be suitable for the current
|
---|
2521 | * timer load.
|
---|
2522 | *
|
---|
2523 | * This will take the highest timer frequency, adjust for catch-up and warp
|
---|
2524 | * driver, and finally add a little fudge factor. The caller (VMM) will use
|
---|
2525 | * the result to adjust the per-cpu preemption timer.
|
---|
2526 | *
|
---|
2527 | * @returns The highest frequency. 0 if no important timers around.
|
---|
2528 | * @param pVM The cross context VM structure.
|
---|
2529 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
2530 | */
|
---|
2531 | VMM_INT_DECL(uint32_t) TMCalcHostTimerFrequency(PVM pVM, PVMCPU pVCpu)
|
---|
2532 | {
|
---|
2533 | uint32_t uHz = tmGetFrequencyHint(pVM);
|
---|
2534 |
|
---|
2535 | /* Catch up, we have to be more aggressive than the % indicates at the
|
---|
2536 | beginning of the effort. */
|
---|
2537 | if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
2538 | {
|
---|
2539 | uint32_t u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage);
|
---|
2540 | if (ASMAtomicReadBool(&pVM->tm.s.fVirtualSyncCatchUp))
|
---|
2541 | {
|
---|
2542 | if (u32Pct <= 100)
|
---|
2543 | u32Pct = u32Pct * pVM->tm.s.cPctHostHzFudgeFactorCatchUp100 / 100;
|
---|
2544 | else if (u32Pct <= 200)
|
---|
2545 | u32Pct = u32Pct * pVM->tm.s.cPctHostHzFudgeFactorCatchUp200 / 100;
|
---|
2546 | else if (u32Pct <= 400)
|
---|
2547 | u32Pct = u32Pct * pVM->tm.s.cPctHostHzFudgeFactorCatchUp400 / 100;
|
---|
2548 | uHz *= u32Pct + 100;
|
---|
2549 | uHz /= 100;
|
---|
2550 | }
|
---|
2551 | }
|
---|
2552 |
|
---|
2553 | /* Warp drive. */
|
---|
2554 | if (ASMAtomicUoReadBool(&pVM->tm.s.fVirtualWarpDrive))
|
---|
2555 | {
|
---|
2556 | uint32_t u32Pct = ASMAtomicReadU32(&pVM->tm.s.u32VirtualWarpDrivePercentage);
|
---|
2557 | if (ASMAtomicReadBool(&pVM->tm.s.fVirtualWarpDrive))
|
---|
2558 | {
|
---|
2559 | uHz *= u32Pct;
|
---|
2560 | uHz /= 100;
|
---|
2561 | }
|
---|
2562 | }
|
---|
2563 |
|
---|
2564 | /* Fudge factor. */
|
---|
2565 | if (pVCpu->idCpu == pVM->tm.s.idTimerCpu)
|
---|
2566 | uHz *= pVM->tm.s.cPctHostHzFudgeFactorTimerCpu;
|
---|
2567 | else
|
---|
2568 | uHz *= pVM->tm.s.cPctHostHzFudgeFactorOtherCpu;
|
---|
2569 | uHz /= 100;
|
---|
2570 |
|
---|
2571 | /* Make sure it isn't too high. */
|
---|
2572 | if (uHz > pVM->tm.s.cHostHzMax)
|
---|
2573 | uHz = pVM->tm.s.cHostHzMax;
|
---|
2574 |
|
---|
2575 | return uHz;
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 |
|
---|
2579 | /**
|
---|
2580 | * Whether the guest virtual clock is ticking.
|
---|
2581 | *
|
---|
2582 | * @returns true if ticking, false otherwise.
|
---|
2583 | * @param pVM The cross context VM structure.
|
---|
2584 | */
|
---|
2585 | VMM_INT_DECL(bool) TMVirtualIsTicking(PVM pVM)
|
---|
2586 | {
|
---|
2587 | return RT_BOOL(pVM->tm.s.cVirtualTicking);
|
---|
2588 | }
|
---|
2589 |
|
---|