1 | /* $Id: TMAll.cpp 12989 2008-10-06 02:15:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TM - Timeout Manager, all contexts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_TM
|
---|
27 | #include <VBox/tm.h>
|
---|
28 | #include <VBox/mm.h>
|
---|
29 | #ifdef IN_RING3
|
---|
30 | # include <VBox/rem.h>
|
---|
31 | #endif
|
---|
32 | #include "TMInternal.h"
|
---|
33 | #include <VBox/vm.h>
|
---|
34 |
|
---|
35 | #include <VBox/param.h>
|
---|
36 | #include <VBox/err.h>
|
---|
37 | #include <VBox/log.h>
|
---|
38 | #include <VBox/sup.h>
|
---|
39 | #include <iprt/time.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #ifdef IN_RING3
|
---|
43 | # include <iprt/thread.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Notification that execution is about to start.
|
---|
49 | *
|
---|
50 | * This call must always be paired with a TMNotifyEndOfExecution call.
|
---|
51 | *
|
---|
52 | * The function may, depending on the configuration, resume the TSC and future
|
---|
53 | * clocks that only ticks when we're executing guest code.
|
---|
54 | *
|
---|
55 | * @param pVM Pointer to the shared VM structure.
|
---|
56 | */
|
---|
57 | VMMDECL(void) TMNotifyStartOfExecution(PVM pVM)
|
---|
58 | {
|
---|
59 | if (pVM->tm.s.fTSCTiedToExecution)
|
---|
60 | tmCpuTickResume(pVM);
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Notification that execution is about to start.
|
---|
66 | *
|
---|
67 | * This call must always be paired with a TMNotifyStartOfExecution call.
|
---|
68 | *
|
---|
69 | * The function may, depending on the configuration, suspend the TSC and future
|
---|
70 | * clocks that only ticks when we're executing guest code.
|
---|
71 | *
|
---|
72 | * @param pVM Pointer to the shared VM structure.
|
---|
73 | */
|
---|
74 | VMMDECL(void) TMNotifyEndOfExecution(PVM pVM)
|
---|
75 | {
|
---|
76 | if (pVM->tm.s.fTSCTiedToExecution)
|
---|
77 | tmCpuTickPause(pVM);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Notification that the cpu is entering the halt state
|
---|
83 | *
|
---|
84 | * This call must always be paired with a TMNotifyEndOfExecution call.
|
---|
85 | *
|
---|
86 | * The function may, depending on the configuration, resume the TSC and future
|
---|
87 | * clocks that only ticks when we're halted.
|
---|
88 | *
|
---|
89 | * @param pVM Pointer to the shared VM structure.
|
---|
90 | */
|
---|
91 | VMMDECL(void) TMNotifyStartOfHalt(PVM pVM)
|
---|
92 | {
|
---|
93 | if ( pVM->tm.s.fTSCTiedToExecution
|
---|
94 | && !pVM->tm.s.fTSCNotTiedToHalt)
|
---|
95 | tmCpuTickResume(pVM);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Notification that the cpu is leaving the halt state
|
---|
101 | *
|
---|
102 | * This call must always be paired with a TMNotifyStartOfHalt call.
|
---|
103 | *
|
---|
104 | * The function may, depending on the configuration, suspend the TSC and future
|
---|
105 | * clocks that only ticks when we're halted.
|
---|
106 | *
|
---|
107 | * @param pVM Pointer to the shared VM structure.
|
---|
108 | */
|
---|
109 | VMMDECL(void) TMNotifyEndOfHalt(PVM pVM)
|
---|
110 | {
|
---|
111 | if ( pVM->tm.s.fTSCTiedToExecution
|
---|
112 | && !pVM->tm.s.fTSCNotTiedToHalt)
|
---|
113 | tmCpuTickPause(pVM);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Schedule the queue which was changed.
|
---|
119 | */
|
---|
120 | DECLINLINE(void) tmSchedule(PTMTIMER pTimer)
|
---|
121 | {
|
---|
122 | PVM pVM = pTimer->CTXALLSUFF(pVM);
|
---|
123 | if (VM_IS_EMT(pVM))
|
---|
124 | {
|
---|
125 | STAM_PROFILE_START(&pVM->tm.s.CTXALLSUFF(StatScheduleOne), a);
|
---|
126 | PTMTIMERQUEUE pQueue = &pVM->tm.s.CTXALLSUFF(paTimerQueues)[pTimer->enmClock];
|
---|
127 | Log3(("tmSchedule: tmTimerQueueSchedule\n"));
|
---|
128 | tmTimerQueueSchedule(pVM, pQueue);
|
---|
129 | #ifdef VBOX_STRICT
|
---|
130 | tmTimerQueuesSanityChecks(pVM, "tmSchedule");
|
---|
131 | #endif
|
---|
132 | STAM_PROFILE_STOP(&pVM->tm.s.CTXALLSUFF(StatScheduleOne), a);
|
---|
133 | }
|
---|
134 | else if (!VM_FF_ISSET(pVM, VM_FF_TIMER)) /**@todo only do this when arming the timer. */
|
---|
135 | {
|
---|
136 | STAM_COUNTER_INC(&pVM->tm.s.StatScheduleSetFF);
|
---|
137 | VM_FF_SET(pVM, VM_FF_TIMER);
|
---|
138 | #ifdef IN_RING3
|
---|
139 | REMR3NotifyTimerPending(pVM);
|
---|
140 | VMR3NotifyFF(pVM, true);
|
---|
141 | #endif
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Try change the state to enmStateNew from enmStateOld
|
---|
148 | * and link the timer into the scheduling queue.
|
---|
149 | *
|
---|
150 | * @returns Success indicator.
|
---|
151 | * @param pTimer Timer in question.
|
---|
152 | * @param enmStateNew The new timer state.
|
---|
153 | * @param enmStateOld The old timer state.
|
---|
154 | */
|
---|
155 | DECLINLINE(bool) tmTimerTry(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
|
---|
156 | {
|
---|
157 | /*
|
---|
158 | * Attempt state change.
|
---|
159 | */
|
---|
160 | bool fRc;
|
---|
161 | TM_TRY_SET_STATE(pTimer, enmStateNew, enmStateOld, fRc);
|
---|
162 | return fRc;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Links the timer onto the scheduling queue.
|
---|
168 | *
|
---|
169 | * @param pQueue The timer queue the timer belongs to.
|
---|
170 | * @param pTimer The timer.
|
---|
171 | */
|
---|
172 | DECLINLINE(void) tmTimerLink(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
|
---|
173 | {
|
---|
174 | Assert(!pTimer->offScheduleNext);
|
---|
175 | const int32_t offHeadNew = (intptr_t)pTimer - (intptr_t)pQueue;
|
---|
176 | int32_t offHead;
|
---|
177 | do
|
---|
178 | {
|
---|
179 | offHead = pQueue->offSchedule;
|
---|
180 | if (offHead)
|
---|
181 | pTimer->offScheduleNext = ((intptr_t)pQueue + offHead) - (intptr_t)pTimer;
|
---|
182 | else
|
---|
183 | pTimer->offScheduleNext = 0;
|
---|
184 | } while (!ASMAtomicCmpXchgS32(&pQueue->offSchedule, offHeadNew, offHead));
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Try change the state to enmStateNew from enmStateOld
|
---|
190 | * and link the timer into the scheduling queue.
|
---|
191 | *
|
---|
192 | * @returns Success indicator.
|
---|
193 | * @param pTimer Timer in question.
|
---|
194 | * @param enmStateNew The new timer state.
|
---|
195 | * @param enmStateOld The old timer state.
|
---|
196 | */
|
---|
197 | DECLINLINE(bool) tmTimerTryWithLink(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
|
---|
198 | {
|
---|
199 | if (tmTimerTry(pTimer, enmStateNew, enmStateOld))
|
---|
200 | {
|
---|
201 | tmTimerLink(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(paTimerQueues)[pTimer->enmClock], pTimer);
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 | return false;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | #ifdef VBOX_HIGH_RES_TIMERS_HACK
|
---|
209 | /**
|
---|
210 | * Set FF if we've passed the next virtual event.
|
---|
211 | *
|
---|
212 | * This function is called before FFs are checked in the inner execution EM loops.
|
---|
213 | *
|
---|
214 | * @returns Virtual timer ticks to the next event.
|
---|
215 | * @param pVM Pointer to the shared VM structure.
|
---|
216 | * @thread The emulation thread.
|
---|
217 | */
|
---|
218 | VMMDECL(uint64_t) TMTimerPoll(PVM pVM)
|
---|
219 | {
|
---|
220 | /*
|
---|
221 | * Return straight away if the timer FF is already set.
|
---|
222 | */
|
---|
223 | if (VM_FF_ISSET(pVM, VM_FF_TIMER))
|
---|
224 | {
|
---|
225 | STAM_COUNTER_INC(&pVM->tm.s.StatPollAlreadySet);
|
---|
226 | return 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Get current time and check the expire times of the two relevant queues.
|
---|
231 | */
|
---|
232 | const uint64_t u64Now = TMVirtualGet(pVM);
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * TMCLOCK_VIRTUAL
|
---|
236 | */
|
---|
237 | const uint64_t u64Expire1 = pVM->tm.s.CTXALLSUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire;
|
---|
238 | const int64_t i64Delta1 = u64Expire1 - u64Now;
|
---|
239 | if (i64Delta1 <= 0)
|
---|
240 | {
|
---|
241 | LogFlow(("TMTimerPoll: expire1=%RU64 <= now=%RU64\n", u64Expire1, u64Now));
|
---|
242 | STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtual);
|
---|
243 | VM_FF_SET(pVM, VM_FF_TIMER);
|
---|
244 | #ifdef IN_RING3
|
---|
245 | REMR3NotifyTimerPending(pVM);
|
---|
246 | #endif
|
---|
247 | return 0;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * TMCLOCK_VIRTUAL_SYNC
|
---|
252 | * This isn't quite as stright forward if in a catch-up, not only do
|
---|
253 | * we have to adjust the 'now' but when have to adjust the delta as well.
|
---|
254 | */
|
---|
255 | const uint64_t u64Expire2 = pVM->tm.s.CTXALLSUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire;
|
---|
256 | uint64_t u64VirtualSyncNow;
|
---|
257 | if (!pVM->tm.s.fVirtualSyncTicking)
|
---|
258 | u64VirtualSyncNow = pVM->tm.s.u64VirtualSync;
|
---|
259 | else
|
---|
260 | {
|
---|
261 | if (!pVM->tm.s.fVirtualSyncCatchUp)
|
---|
262 | u64VirtualSyncNow = u64Now - pVM->tm.s.offVirtualSync;
|
---|
263 | else
|
---|
264 | {
|
---|
265 | uint64_t off = pVM->tm.s.offVirtualSync;
|
---|
266 | uint64_t u64Delta = u64Now - pVM->tm.s.u64VirtualSyncCatchUpPrev;
|
---|
267 | if (RT_LIKELY(!(u64Delta >> 32)))
|
---|
268 | {
|
---|
269 | uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
|
---|
270 | if (off > u64Sub + pVM->tm.s.offVirtualSyncGivenUp)
|
---|
271 | off -= u64Sub;
|
---|
272 | else
|
---|
273 | off = pVM->tm.s.offVirtualSyncGivenUp;
|
---|
274 | }
|
---|
275 | u64VirtualSyncNow = u64Now - off;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
|
---|
279 | if (i64Delta2 <= 0)
|
---|
280 | {
|
---|
281 | LogFlow(("TMTimerPoll: expire2=%RU64 <= now=%RU64\n", u64Expire2, u64Now));
|
---|
282 | STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtualSync);
|
---|
283 | VM_FF_SET(pVM, VM_FF_TIMER);
|
---|
284 | #ifdef IN_RING3
|
---|
285 | REMR3NotifyTimerPending(pVM);
|
---|
286 | #endif
|
---|
287 | return 0;
|
---|
288 | }
|
---|
289 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
290 | i64Delta2 = ASMMultU64ByU32DivByU32(i64Delta2, 100, pVM->tm.s.u32VirtualSyncCatchUpPercentage + 100);
|
---|
291 |
|
---|
292 | /*
|
---|
293 | * Return the time left to the next event.
|
---|
294 | */
|
---|
295 | STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
|
---|
296 | return RT_MIN(i64Delta1, i64Delta2);
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Set FF if we've passed the next virtual event.
|
---|
302 | *
|
---|
303 | * This function is called before FFs are checked in the inner execution EM loops.
|
---|
304 | *
|
---|
305 | * @returns The GIP timestamp of the next event.
|
---|
306 | * 0 if the next event has already expired.
|
---|
307 | * @param pVM Pointer to the shared VM structure.
|
---|
308 | * @param pVM Pointer to the shared VM structure.
|
---|
309 | * @param pu64Delta Where to store the delta.
|
---|
310 | * @thread The emulation thread.
|
---|
311 | */
|
---|
312 | VMMDECL(uint64_t) TMTimerPollGIP(PVM pVM, uint64_t *pu64Delta)
|
---|
313 | {
|
---|
314 | /*
|
---|
315 | * Return straight away if the timer FF is already set.
|
---|
316 | */
|
---|
317 | if (VM_FF_ISSET(pVM, VM_FF_TIMER))
|
---|
318 | {
|
---|
319 | STAM_COUNTER_INC(&pVM->tm.s.StatPollAlreadySet);
|
---|
320 | *pu64Delta = 0;
|
---|
321 | return 0;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Get current time and check the expire times of the two relevant queues.
|
---|
326 | */
|
---|
327 | const uint64_t u64Now = TMVirtualGet(pVM);
|
---|
328 |
|
---|
329 | /*
|
---|
330 | * TMCLOCK_VIRTUAL
|
---|
331 | */
|
---|
332 | const uint64_t u64Expire1 = pVM->tm.s.CTXALLSUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire;
|
---|
333 | const int64_t i64Delta1 = u64Expire1 - u64Now;
|
---|
334 | if (i64Delta1 <= 0)
|
---|
335 | {
|
---|
336 | LogFlow(("TMTimerPoll: expire1=%RU64 <= now=%RU64\n", u64Expire1, u64Now));
|
---|
337 | STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtual);
|
---|
338 | VM_FF_SET(pVM, VM_FF_TIMER);
|
---|
339 | #ifdef IN_RING3
|
---|
340 | REMR3NotifyTimerPending(pVM);
|
---|
341 | #endif
|
---|
342 | *pu64Delta = 0;
|
---|
343 | return 0;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * TMCLOCK_VIRTUAL_SYNC
|
---|
348 | * This isn't quite as stright forward if in a catch-up, not only do
|
---|
349 | * we have to adjust the 'now' but when have to adjust the delta as well.
|
---|
350 | */
|
---|
351 | const uint64_t u64Expire2 = pVM->tm.s.CTXALLSUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire;
|
---|
352 | uint64_t u64VirtualSyncNow;
|
---|
353 | if (!pVM->tm.s.fVirtualSyncTicking)
|
---|
354 | u64VirtualSyncNow = pVM->tm.s.u64VirtualSync;
|
---|
355 | else
|
---|
356 | {
|
---|
357 | if (!pVM->tm.s.fVirtualSyncCatchUp)
|
---|
358 | u64VirtualSyncNow = u64Now - pVM->tm.s.offVirtualSync;
|
---|
359 | else
|
---|
360 | {
|
---|
361 | uint64_t off = pVM->tm.s.offVirtualSync;
|
---|
362 | uint64_t u64Delta = u64Now - pVM->tm.s.u64VirtualSyncCatchUpPrev;
|
---|
363 | if (RT_LIKELY(!(u64Delta >> 32)))
|
---|
364 | {
|
---|
365 | uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
|
---|
366 | if (off > u64Sub + pVM->tm.s.offVirtualSyncGivenUp)
|
---|
367 | off -= u64Sub;
|
---|
368 | else
|
---|
369 | off = pVM->tm.s.offVirtualSyncGivenUp;
|
---|
370 | }
|
---|
371 | u64VirtualSyncNow = u64Now - off;
|
---|
372 | }
|
---|
373 | }
|
---|
374 | int64_t i64Delta2 = u64Expire2 - u64VirtualSyncNow;
|
---|
375 | if (i64Delta2 <= 0)
|
---|
376 | {
|
---|
377 | LogFlow(("TMTimerPoll: expire2=%RU64 <= now=%RU64\n", u64Expire2, u64Now));
|
---|
378 | STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtualSync);
|
---|
379 | VM_FF_SET(pVM, VM_FF_TIMER);
|
---|
380 | #ifdef IN_RING3
|
---|
381 | REMR3NotifyTimerPending(pVM);
|
---|
382 | #endif
|
---|
383 | *pu64Delta = 0;
|
---|
384 | return 0;
|
---|
385 | }
|
---|
386 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
387 | i64Delta2 = ASMMultU64ByU32DivByU32(i64Delta2, 100, pVM->tm.s.u32VirtualSyncCatchUpPercentage + 100);
|
---|
388 |
|
---|
389 | /*
|
---|
390 | * Return the GIP time of the next event.
|
---|
391 | * This is the reverse of what tmVirtualGetRaw is doing.
|
---|
392 | */
|
---|
393 | STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
|
---|
394 | uint64_t u64GipTime = RT_MIN(i64Delta1, i64Delta2);
|
---|
395 | *pu64Delta = u64GipTime;
|
---|
396 | u64GipTime += u64Now + pVM->tm.s.u64VirtualOffset;
|
---|
397 | if (RT_UNLIKELY(!pVM->tm.s.fVirtualWarpDrive))
|
---|
398 | {
|
---|
399 | u64GipTime -= pVM->tm.s.u64VirtualWarpDriveStart; /* the start is GIP time. */
|
---|
400 | u64GipTime *= 100;
|
---|
401 | u64GipTime /= pVM->tm.s.u32VirtualWarpDrivePercentage;
|
---|
402 | u64GipTime += pVM->tm.s.u64VirtualWarpDriveStart;
|
---|
403 | }
|
---|
404 | return u64GipTime;
|
---|
405 | }
|
---|
406 | #endif
|
---|
407 |
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Gets the host context ring-3 pointer of the timer.
|
---|
411 | *
|
---|
412 | * @returns HC R3 pointer.
|
---|
413 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
414 | */
|
---|
415 | VMMDECL(PTMTIMERR3) TMTimerR3Ptr(PTMTIMER pTimer)
|
---|
416 | {
|
---|
417 | return (PTMTIMERR3)MMHyperCCToR3(pTimer->CTXALLSUFF(pVM), pTimer);
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Gets the host context ring-0 pointer of the timer.
|
---|
423 | *
|
---|
424 | * @returns HC R0 pointer.
|
---|
425 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
426 | */
|
---|
427 | VMMDECL(PTMTIMERR0) TMTimerR0Ptr(PTMTIMER pTimer)
|
---|
428 | {
|
---|
429 | return (PTMTIMERR0)MMHyperCCToR0(pTimer->CTXALLSUFF(pVM), pTimer);
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Gets the RC pointer of the timer.
|
---|
435 | *
|
---|
436 | * @returns RC pointer.
|
---|
437 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
438 | */
|
---|
439 | VMMDECL(PTMTIMERRC) TMTimerRCPtr(PTMTIMER pTimer)
|
---|
440 | {
|
---|
441 | return (PTMTIMERRC)MMHyperCCToRC(pTimer->CTXALLSUFF(pVM), pTimer);
|
---|
442 | }
|
---|
443 |
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Destroy a timer
|
---|
447 | *
|
---|
448 | * @returns VBox status.
|
---|
449 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
450 | */
|
---|
451 | VMMDECL(int) TMTimerDestroy(PTMTIMER pTimer)
|
---|
452 | {
|
---|
453 | int cRetries = 1000;
|
---|
454 | do
|
---|
455 | {
|
---|
456 | /*
|
---|
457 | * Change to any of the DESTROY states if valid.
|
---|
458 | */
|
---|
459 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
460 | Log2(("TMTimerDestroy: pTimer=%p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
|
---|
461 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries));
|
---|
462 | switch (enmState)
|
---|
463 | {
|
---|
464 | case TMTIMERSTATE_EXPIRED:
|
---|
465 | if (!VM_IS_EMT(pTimer->CTXALLSUFF(pVM)))
|
---|
466 | {
|
---|
467 | AssertMsgFailed(("Attempted timer destruction from other thread while expire pending! (%s)\n", R3STRING(pTimer->pszDesc)));
|
---|
468 | return VERR_INVALID_PARAMETER;
|
---|
469 | }
|
---|
470 | /* fall thru */
|
---|
471 | case TMTIMERSTATE_STOPPED:
|
---|
472 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_DESTROY, enmState))
|
---|
473 | {
|
---|
474 | tmSchedule(pTimer);
|
---|
475 | return VINF_SUCCESS;
|
---|
476 | }
|
---|
477 | break;
|
---|
478 |
|
---|
479 | case TMTIMERSTATE_ACTIVE:
|
---|
480 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_STOP_DESTROY, enmState))
|
---|
481 | {
|
---|
482 | tmSchedule(pTimer);
|
---|
483 | return VINF_SUCCESS;
|
---|
484 | }
|
---|
485 | break;
|
---|
486 |
|
---|
487 | case TMTIMERSTATE_PENDING_STOP:
|
---|
488 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
489 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_DESTROY, enmState))
|
---|
490 | {
|
---|
491 | tmSchedule(pTimer);
|
---|
492 | return VINF_SUCCESS;
|
---|
493 | }
|
---|
494 | break;
|
---|
495 |
|
---|
496 | case TMTIMERSTATE_PENDING_DESTROY:
|
---|
497 | case TMTIMERSTATE_PENDING_STOP_DESTROY:
|
---|
498 | AssertMsgFailed(("How many times do you think you can destroy the same timer... (%s)\n", R3STRING(pTimer->pszDesc)));
|
---|
499 | return VERR_INVALID_PARAMETER;
|
---|
500 |
|
---|
501 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
502 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_DESTROY, enmState))
|
---|
503 | {
|
---|
504 | tmSchedule(pTimer);
|
---|
505 | return VINF_SUCCESS;
|
---|
506 | }
|
---|
507 | break;
|
---|
508 |
|
---|
509 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
510 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_DESTROY, enmState))
|
---|
511 | {
|
---|
512 | tmSchedule(pTimer);
|
---|
513 | return VINF_SUCCESS;
|
---|
514 | }
|
---|
515 | break;
|
---|
516 |
|
---|
517 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
518 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
519 | #ifdef IN_RING3
|
---|
520 | if (!RTThreadYield())
|
---|
521 | RTThreadSleep(1);
|
---|
522 | #endif
|
---|
523 | break;
|
---|
524 |
|
---|
525 | /*
|
---|
526 | * Invalid states.
|
---|
527 | */
|
---|
528 | case TMTIMERSTATE_FREE:
|
---|
529 | AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
530 | return VERR_TM_INVALID_STATE;
|
---|
531 | default:
|
---|
532 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
533 | return VERR_TM_UNKNOWN_STATE;
|
---|
534 | }
|
---|
535 | } while (cRetries-- > 0);
|
---|
536 |
|
---|
537 | AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
|
---|
538 | return VERR_INTERNAL_ERROR;
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Arm a timer with a (new) expire time.
|
---|
544 | *
|
---|
545 | * @returns VBox status.
|
---|
546 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
547 | * @param u64Expire New expire time.
|
---|
548 | */
|
---|
549 | VMMDECL(int) TMTimerSet(PTMTIMER pTimer, uint64_t u64Expire)
|
---|
550 | {
|
---|
551 | STAM_PROFILE_START(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
|
---|
552 |
|
---|
553 | /** @todo find the most frequently used paths and make them skip tmSchedule and tmTimerTryWithLink. */
|
---|
554 | int cRetries = 1000;
|
---|
555 | do
|
---|
556 | {
|
---|
557 | /*
|
---|
558 | * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
|
---|
559 | */
|
---|
560 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
561 | Log2(("TMTimerSet: pTimer=%p:{.enmState=%s, .pszDesc='%s'} cRetries=%d u64Expire=%llu\n",
|
---|
562 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries, u64Expire));
|
---|
563 | switch (enmState)
|
---|
564 | {
|
---|
565 | case TMTIMERSTATE_EXPIRED:
|
---|
566 | case TMTIMERSTATE_STOPPED:
|
---|
567 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
|
---|
568 | {
|
---|
569 | Assert(!pTimer->offPrev);
|
---|
570 | Assert(!pTimer->offNext);
|
---|
571 | AssertMsg( pTimer->enmClock != TMCLOCK_VIRTUAL_SYNC
|
---|
572 | || pTimer->CTXALLSUFF(pVM)->tm.s.fVirtualSyncTicking
|
---|
573 | || u64Expire >= pTimer->CTXALLSUFF(pVM)->tm.s.u64VirtualSync,
|
---|
574 | ("%RU64 < %RU64 %s\n", u64Expire, pTimer->CTXALLSUFF(pVM)->tm.s.u64VirtualSync, R3STRING(pTimer->pszDesc)));
|
---|
575 | pTimer->u64Expire = u64Expire;
|
---|
576 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
|
---|
577 | tmSchedule(pTimer);
|
---|
578 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
|
---|
579 | return VINF_SUCCESS;
|
---|
580 | }
|
---|
581 | break;
|
---|
582 |
|
---|
583 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
584 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
585 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
|
---|
586 | {
|
---|
587 | Assert(!pTimer->offPrev);
|
---|
588 | Assert(!pTimer->offNext);
|
---|
589 | pTimer->u64Expire = u64Expire;
|
---|
590 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
|
---|
591 | tmSchedule(pTimer);
|
---|
592 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
|
---|
593 | return VINF_SUCCESS;
|
---|
594 | }
|
---|
595 | break;
|
---|
596 |
|
---|
597 |
|
---|
598 | case TMTIMERSTATE_ACTIVE:
|
---|
599 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
|
---|
600 | {
|
---|
601 | pTimer->u64Expire = u64Expire;
|
---|
602 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
|
---|
603 | tmSchedule(pTimer);
|
---|
604 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
|
---|
605 | return VINF_SUCCESS;
|
---|
606 | }
|
---|
607 | break;
|
---|
608 |
|
---|
609 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
610 | case TMTIMERSTATE_PENDING_STOP:
|
---|
611 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
|
---|
612 | {
|
---|
613 | pTimer->u64Expire = u64Expire;
|
---|
614 | TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
|
---|
615 | tmSchedule(pTimer);
|
---|
616 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
|
---|
617 | return VINF_SUCCESS;
|
---|
618 | }
|
---|
619 | break;
|
---|
620 |
|
---|
621 |
|
---|
622 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
623 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
624 | #ifdef IN_RING3
|
---|
625 | if (!RTThreadYield())
|
---|
626 | RTThreadSleep(1);
|
---|
627 | #else
|
---|
628 | /** @todo call host context and yield after a couple of iterations */
|
---|
629 | #endif
|
---|
630 | break;
|
---|
631 |
|
---|
632 | /*
|
---|
633 | * Invalid states.
|
---|
634 | */
|
---|
635 | case TMTIMERSTATE_PENDING_DESTROY:
|
---|
636 | case TMTIMERSTATE_PENDING_STOP_DESTROY:
|
---|
637 | case TMTIMERSTATE_FREE:
|
---|
638 | AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
639 | return VERR_TM_INVALID_STATE;
|
---|
640 | default:
|
---|
641 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
642 | return VERR_TM_UNKNOWN_STATE;
|
---|
643 | }
|
---|
644 | } while (cRetries-- > 0);
|
---|
645 |
|
---|
646 | AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
|
---|
647 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
|
---|
648 | return VERR_INTERNAL_ERROR;
|
---|
649 | }
|
---|
650 |
|
---|
651 |
|
---|
652 | /**
|
---|
653 | * Arm a timer with a (new) expire time relative to current time.
|
---|
654 | *
|
---|
655 | * @returns VBox status.
|
---|
656 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
657 | * @param cMilliesToNext Number of millieseconds to the next tick.
|
---|
658 | */
|
---|
659 | VMMDECL(int) TMTimerSetMillies(PTMTIMER pTimer, uint32_t cMilliesToNext)
|
---|
660 | {
|
---|
661 | PVM pVM = pTimer->CTXALLSUFF(pVM);
|
---|
662 | switch (pTimer->enmClock)
|
---|
663 | {
|
---|
664 | case TMCLOCK_VIRTUAL:
|
---|
665 | return TMTimerSet(pTimer, cMilliesToNext * (uint64_t)TMCLOCK_FREQ_VIRTUAL / 1000 + TMVirtualGet(pVM));
|
---|
666 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
667 | return TMTimerSet(pTimer, cMilliesToNext * (uint64_t)TMCLOCK_FREQ_VIRTUAL / 1000 + TMVirtualSyncGet(pVM));
|
---|
668 | case TMCLOCK_REAL:
|
---|
669 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
670 | return TMTimerSet(pTimer, cMilliesToNext + TMRealGet(pVM));
|
---|
671 | case TMCLOCK_TSC:
|
---|
672 | return TMTimerSet(pTimer, cMilliesToNext * pVM->tm.s.cTSCTicksPerSecond / 1000 + TMCpuTickGet(pVM));
|
---|
673 |
|
---|
674 | default:
|
---|
675 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
676 | return VERR_INTERNAL_ERROR;
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * Arm a timer with a (new) expire time relative to current time.
|
---|
683 | *
|
---|
684 | * @returns VBox status.
|
---|
685 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
686 | * @param cMicrosToNext Number of microseconds to the next tick.
|
---|
687 | */
|
---|
688 | VMMDECL(int) TMTimerSetMicro(PTMTIMER pTimer, uint64_t cMicrosToNext)
|
---|
689 | {
|
---|
690 | PVM pVM = pTimer->CTXALLSUFF(pVM);
|
---|
691 | switch (pTimer->enmClock)
|
---|
692 | {
|
---|
693 | case TMCLOCK_VIRTUAL:
|
---|
694 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
695 | return TMTimerSet(pTimer, cMicrosToNext * 1000 + TMVirtualGet(pVM));
|
---|
696 |
|
---|
697 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
698 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
699 | return TMTimerSet(pTimer, cMicrosToNext * 1000 + TMVirtualSyncGet(pVM));
|
---|
700 |
|
---|
701 | case TMCLOCK_REAL:
|
---|
702 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
703 | return TMTimerSet(pTimer, cMicrosToNext / 1000 + TMRealGet(pVM));
|
---|
704 |
|
---|
705 | case TMCLOCK_TSC:
|
---|
706 | return TMTimerSet(pTimer, TMTimerFromMicro(pTimer, cMicrosToNext) + TMCpuTickGet(pVM));
|
---|
707 |
|
---|
708 | default:
|
---|
709 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
710 | return VERR_INTERNAL_ERROR;
|
---|
711 | }
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Arm a timer with a (new) expire time relative to current time.
|
---|
717 | *
|
---|
718 | * @returns VBox status.
|
---|
719 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
720 | * @param cNanosToNext Number of nanoseconds to the next tick.
|
---|
721 | */
|
---|
722 | VMMDECL(int) TMTimerSetNano(PTMTIMER pTimer, uint64_t cNanosToNext)
|
---|
723 | {
|
---|
724 | PVM pVM = pTimer->CTXALLSUFF(pVM);
|
---|
725 | switch (pTimer->enmClock)
|
---|
726 | {
|
---|
727 | case TMCLOCK_VIRTUAL:
|
---|
728 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
729 | return TMTimerSet(pTimer, cNanosToNext + TMVirtualGet(pVM));
|
---|
730 |
|
---|
731 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
732 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
733 | return TMTimerSet(pTimer, cNanosToNext + TMVirtualSyncGet(pVM));
|
---|
734 |
|
---|
735 | case TMCLOCK_REAL:
|
---|
736 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
737 | return TMTimerSet(pTimer, cNanosToNext / 1000000 + TMRealGet(pVM));
|
---|
738 |
|
---|
739 | case TMCLOCK_TSC:
|
---|
740 | return TMTimerSet(pTimer, TMTimerFromNano(pTimer, cNanosToNext) + TMCpuTickGet(pVM));
|
---|
741 |
|
---|
742 | default:
|
---|
743 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
744 | return VERR_INTERNAL_ERROR;
|
---|
745 | }
|
---|
746 | }
|
---|
747 |
|
---|
748 |
|
---|
749 | /**
|
---|
750 | * Stop the timer.
|
---|
751 | * Use TMR3TimerArm() to "un-stop" the timer.
|
---|
752 | *
|
---|
753 | * @returns VBox status.
|
---|
754 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
755 | */
|
---|
756 | VMMDECL(int) TMTimerStop(PTMTIMER pTimer)
|
---|
757 | {
|
---|
758 | STAM_PROFILE_START(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
|
---|
759 | /** @todo see if this function needs optimizing. */
|
---|
760 | int cRetries = 1000;
|
---|
761 | do
|
---|
762 | {
|
---|
763 | /*
|
---|
764 | * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
|
---|
765 | */
|
---|
766 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
767 | Log2(("TMTimerStop: pTimer=%p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
|
---|
768 | pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries));
|
---|
769 | switch (enmState)
|
---|
770 | {
|
---|
771 | case TMTIMERSTATE_EXPIRED:
|
---|
772 | //AssertMsgFailed(("You don't stop an expired timer dude!\n"));
|
---|
773 | return VERR_INVALID_PARAMETER;
|
---|
774 |
|
---|
775 | case TMTIMERSTATE_STOPPED:
|
---|
776 | case TMTIMERSTATE_PENDING_STOP:
|
---|
777 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
778 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
|
---|
779 | return VINF_SUCCESS;
|
---|
780 |
|
---|
781 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
782 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, enmState))
|
---|
783 | {
|
---|
784 | Assert(!pTimer->offPrev);
|
---|
785 | Assert(!pTimer->offNext);
|
---|
786 | tmSchedule(pTimer);
|
---|
787 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
|
---|
788 | return VINF_SUCCESS;
|
---|
789 | }
|
---|
790 |
|
---|
791 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
792 | if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
|
---|
793 | {
|
---|
794 | tmSchedule(pTimer);
|
---|
795 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
|
---|
796 | return VINF_SUCCESS;
|
---|
797 | }
|
---|
798 | break;
|
---|
799 |
|
---|
800 | case TMTIMERSTATE_ACTIVE:
|
---|
801 | if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
|
---|
802 | {
|
---|
803 | tmSchedule(pTimer);
|
---|
804 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
|
---|
805 | return VINF_SUCCESS;
|
---|
806 | }
|
---|
807 | break;
|
---|
808 |
|
---|
809 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
810 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
811 | #ifdef IN_RING3
|
---|
812 | if (!RTThreadYield())
|
---|
813 | RTThreadSleep(1);
|
---|
814 | #else
|
---|
815 | /**@todo call host and yield cpu after a while. */
|
---|
816 | #endif
|
---|
817 | break;
|
---|
818 |
|
---|
819 | /*
|
---|
820 | * Invalid states.
|
---|
821 | */
|
---|
822 | case TMTIMERSTATE_PENDING_DESTROY:
|
---|
823 | case TMTIMERSTATE_PENDING_STOP_DESTROY:
|
---|
824 | case TMTIMERSTATE_FREE:
|
---|
825 | AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
826 | return VERR_TM_INVALID_STATE;
|
---|
827 | default:
|
---|
828 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
829 | return VERR_TM_UNKNOWN_STATE;
|
---|
830 | }
|
---|
831 | } while (cRetries-- > 0);
|
---|
832 |
|
---|
833 | AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
|
---|
834 | STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
|
---|
835 | return VERR_INTERNAL_ERROR;
|
---|
836 | }
|
---|
837 |
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Get the current clock time.
|
---|
841 | * Handy for calculating the new expire time.
|
---|
842 | *
|
---|
843 | * @returns Current clock time.
|
---|
844 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
845 | */
|
---|
846 | VMMDECL(uint64_t) TMTimerGet(PTMTIMER pTimer)
|
---|
847 | {
|
---|
848 | uint64_t u64;
|
---|
849 | PVM pVM = pTimer->CTXALLSUFF(pVM);
|
---|
850 | switch (pTimer->enmClock)
|
---|
851 | {
|
---|
852 | case TMCLOCK_VIRTUAL:
|
---|
853 | u64 = TMVirtualGet(pVM);
|
---|
854 | break;
|
---|
855 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
856 | u64 = TMVirtualSyncGet(pVM);
|
---|
857 | break;
|
---|
858 | case TMCLOCK_REAL:
|
---|
859 | u64 = TMRealGet(pVM);
|
---|
860 | break;
|
---|
861 | case TMCLOCK_TSC:
|
---|
862 | u64 = TMCpuTickGet(pVM);
|
---|
863 | break;
|
---|
864 |
|
---|
865 | default:
|
---|
866 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
867 | return ~(uint64_t)0;
|
---|
868 | }
|
---|
869 | //Log2(("TMTimerGet: returns %llu (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
870 | // u64, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
871 | return u64;
|
---|
872 | }
|
---|
873 |
|
---|
874 |
|
---|
875 | /**
|
---|
876 | * Get the freqency of the timer clock.
|
---|
877 | *
|
---|
878 | * @returns Clock frequency (as Hz of course).
|
---|
879 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
880 | */
|
---|
881 | VMMDECL(uint64_t) TMTimerGetFreq(PTMTIMER pTimer)
|
---|
882 | {
|
---|
883 | switch (pTimer->enmClock)
|
---|
884 | {
|
---|
885 | case TMCLOCK_VIRTUAL:
|
---|
886 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
887 | return TMCLOCK_FREQ_VIRTUAL;
|
---|
888 |
|
---|
889 | case TMCLOCK_REAL:
|
---|
890 | return TMCLOCK_FREQ_REAL;
|
---|
891 |
|
---|
892 | case TMCLOCK_TSC:
|
---|
893 | return TMCpuTicksPerSecond(pTimer->CTXALLSUFF(pVM));
|
---|
894 |
|
---|
895 | default:
|
---|
896 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
897 | return 0;
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 |
|
---|
902 | /**
|
---|
903 | * Get the current clock time as nanoseconds.
|
---|
904 | *
|
---|
905 | * @returns The timer clock as nanoseconds.
|
---|
906 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
907 | */
|
---|
908 | VMMDECL(uint64_t) TMTimerGetNano(PTMTIMER pTimer)
|
---|
909 | {
|
---|
910 | return TMTimerToNano(pTimer, TMTimerGet(pTimer));
|
---|
911 | }
|
---|
912 |
|
---|
913 |
|
---|
914 | /**
|
---|
915 | * Get the current clock time as microseconds.
|
---|
916 | *
|
---|
917 | * @returns The timer clock as microseconds.
|
---|
918 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
919 | */
|
---|
920 | VMMDECL(uint64_t) TMTimerGetMicro(PTMTIMER pTimer)
|
---|
921 | {
|
---|
922 | return TMTimerToMicro(pTimer, TMTimerGet(pTimer));
|
---|
923 | }
|
---|
924 |
|
---|
925 |
|
---|
926 | /**
|
---|
927 | * Get the current clock time as milliseconds.
|
---|
928 | *
|
---|
929 | * @returns The timer clock as milliseconds.
|
---|
930 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
931 | */
|
---|
932 | VMMDECL(uint64_t) TMTimerGetMilli(PTMTIMER pTimer)
|
---|
933 | {
|
---|
934 | return TMTimerToMilli(pTimer, TMTimerGet(pTimer));
|
---|
935 | }
|
---|
936 |
|
---|
937 |
|
---|
938 | /**
|
---|
939 | * Converts the specified timer clock time to nanoseconds.
|
---|
940 | *
|
---|
941 | * @returns nanoseconds.
|
---|
942 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
943 | * @param u64Ticks The clock ticks.
|
---|
944 | * @remark There could be rounding errors here. We just do a simple integere divide
|
---|
945 | * without any adjustments.
|
---|
946 | */
|
---|
947 | VMMDECL(uint64_t) TMTimerToNano(PTMTIMER pTimer, uint64_t u64Ticks)
|
---|
948 | {
|
---|
949 | switch (pTimer->enmClock)
|
---|
950 | {
|
---|
951 | case TMCLOCK_VIRTUAL:
|
---|
952 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
953 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
954 | return u64Ticks;
|
---|
955 |
|
---|
956 | case TMCLOCK_REAL:
|
---|
957 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
958 | return u64Ticks * 1000000;
|
---|
959 |
|
---|
960 | case TMCLOCK_TSC:
|
---|
961 | AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
|
---|
962 | return 0;
|
---|
963 |
|
---|
964 | default:
|
---|
965 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
966 | return 0;
|
---|
967 | }
|
---|
968 | }
|
---|
969 |
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * Converts the specified timer clock time to microseconds.
|
---|
973 | *
|
---|
974 | * @returns microseconds.
|
---|
975 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
976 | * @param u64Ticks The clock ticks.
|
---|
977 | * @remark There could be rounding errors here. We just do a simple integere divide
|
---|
978 | * without any adjustments.
|
---|
979 | */
|
---|
980 | VMMDECL(uint64_t) TMTimerToMicro(PTMTIMER pTimer, uint64_t u64Ticks)
|
---|
981 | {
|
---|
982 | switch (pTimer->enmClock)
|
---|
983 | {
|
---|
984 | case TMCLOCK_VIRTUAL:
|
---|
985 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
986 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
987 | return u64Ticks / 1000;
|
---|
988 |
|
---|
989 | case TMCLOCK_REAL:
|
---|
990 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
991 | return u64Ticks * 1000;
|
---|
992 |
|
---|
993 | case TMCLOCK_TSC:
|
---|
994 | AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
|
---|
995 | return 0;
|
---|
996 |
|
---|
997 | default:
|
---|
998 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
999 | return 0;
|
---|
1000 | }
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 |
|
---|
1004 | /**
|
---|
1005 | * Converts the specified timer clock time to milliseconds.
|
---|
1006 | *
|
---|
1007 | * @returns milliseconds.
|
---|
1008 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1009 | * @param u64Ticks The clock ticks.
|
---|
1010 | * @remark There could be rounding errors here. We just do a simple integere divide
|
---|
1011 | * without any adjustments.
|
---|
1012 | */
|
---|
1013 | VMMDECL(uint64_t) TMTimerToMilli(PTMTIMER pTimer, uint64_t u64Ticks)
|
---|
1014 | {
|
---|
1015 | switch (pTimer->enmClock)
|
---|
1016 | {
|
---|
1017 | case TMCLOCK_VIRTUAL:
|
---|
1018 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
1019 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
1020 | return u64Ticks / 1000000;
|
---|
1021 |
|
---|
1022 | case TMCLOCK_REAL:
|
---|
1023 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
1024 | return u64Ticks;
|
---|
1025 |
|
---|
1026 | case TMCLOCK_TSC:
|
---|
1027 | AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
|
---|
1028 | return 0;
|
---|
1029 |
|
---|
1030 | default:
|
---|
1031 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
1032 | return 0;
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 |
|
---|
1037 | /**
|
---|
1038 | * Converts the specified nanosecond timestamp to timer clock ticks.
|
---|
1039 | *
|
---|
1040 | * @returns timer clock ticks.
|
---|
1041 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1042 | * @param u64NanoTS The nanosecond value ticks to convert.
|
---|
1043 | * @remark There could be rounding and overflow errors here.
|
---|
1044 | */
|
---|
1045 | VMMDECL(uint64_t) TMTimerFromNano(PTMTIMER pTimer, uint64_t u64NanoTS)
|
---|
1046 | {
|
---|
1047 | switch (pTimer->enmClock)
|
---|
1048 | {
|
---|
1049 | case TMCLOCK_VIRTUAL:
|
---|
1050 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
1051 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
1052 | return u64NanoTS;
|
---|
1053 |
|
---|
1054 | case TMCLOCK_REAL:
|
---|
1055 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
1056 | return u64NanoTS / 1000000;
|
---|
1057 |
|
---|
1058 | case TMCLOCK_TSC:
|
---|
1059 | AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
|
---|
1060 | return 0;
|
---|
1061 |
|
---|
1062 | default:
|
---|
1063 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
1064 | return 0;
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 |
|
---|
1069 | /**
|
---|
1070 | * Converts the specified microsecond timestamp to timer clock ticks.
|
---|
1071 | *
|
---|
1072 | * @returns timer clock ticks.
|
---|
1073 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1074 | * @param u64MicroTS The microsecond value ticks to convert.
|
---|
1075 | * @remark There could be rounding and overflow errors here.
|
---|
1076 | */
|
---|
1077 | VMMDECL(uint64_t) TMTimerFromMicro(PTMTIMER pTimer, uint64_t u64MicroTS)
|
---|
1078 | {
|
---|
1079 | switch (pTimer->enmClock)
|
---|
1080 | {
|
---|
1081 | case TMCLOCK_VIRTUAL:
|
---|
1082 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
1083 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
1084 | return u64MicroTS * 1000;
|
---|
1085 |
|
---|
1086 | case TMCLOCK_REAL:
|
---|
1087 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
1088 | return u64MicroTS / 1000;
|
---|
1089 |
|
---|
1090 | case TMCLOCK_TSC:
|
---|
1091 | AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
|
---|
1092 | return 0;
|
---|
1093 |
|
---|
1094 | default:
|
---|
1095 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
1096 | return 0;
|
---|
1097 | }
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Converts the specified millisecond timestamp to timer clock ticks.
|
---|
1103 | *
|
---|
1104 | * @returns timer clock ticks.
|
---|
1105 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1106 | * @param u64MilliTS The millisecond value ticks to convert.
|
---|
1107 | * @remark There could be rounding and overflow errors here.
|
---|
1108 | */
|
---|
1109 | VMMDECL(uint64_t) TMTimerFromMilli(PTMTIMER pTimer, uint64_t u64MilliTS)
|
---|
1110 | {
|
---|
1111 | switch (pTimer->enmClock)
|
---|
1112 | {
|
---|
1113 | case TMCLOCK_VIRTUAL:
|
---|
1114 | case TMCLOCK_VIRTUAL_SYNC:
|
---|
1115 | AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
|
---|
1116 | return u64MilliTS * 1000000;
|
---|
1117 |
|
---|
1118 | case TMCLOCK_REAL:
|
---|
1119 | AssertCompile(TMCLOCK_FREQ_REAL == 1000);
|
---|
1120 | return u64MilliTS;
|
---|
1121 |
|
---|
1122 | case TMCLOCK_TSC:
|
---|
1123 | AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
|
---|
1124 | return 0;
|
---|
1125 |
|
---|
1126 | default:
|
---|
1127 | AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
|
---|
1128 | return 0;
|
---|
1129 | }
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 |
|
---|
1133 | /**
|
---|
1134 | * Get the expire time of the timer.
|
---|
1135 | * Only valid for active timers.
|
---|
1136 | *
|
---|
1137 | * @returns Expire time of the timer.
|
---|
1138 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1139 | */
|
---|
1140 | VMMDECL(uint64_t) TMTimerGetExpire(PTMTIMER pTimer)
|
---|
1141 | {
|
---|
1142 | int cRetries = 1000;
|
---|
1143 | do
|
---|
1144 | {
|
---|
1145 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1146 | switch (enmState)
|
---|
1147 | {
|
---|
1148 | case TMTIMERSTATE_EXPIRED:
|
---|
1149 | case TMTIMERSTATE_STOPPED:
|
---|
1150 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1151 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1152 | Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
1153 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
1154 | return ~(uint64_t)0;
|
---|
1155 |
|
---|
1156 | case TMTIMERSTATE_ACTIVE:
|
---|
1157 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1158 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1159 | Log2(("TMTimerGetExpire: returns %llu (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
1160 | pTimer->u64Expire, pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
1161 | return pTimer->u64Expire;
|
---|
1162 |
|
---|
1163 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1164 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1165 | #ifdef IN_RING3
|
---|
1166 | if (!RTThreadYield())
|
---|
1167 | RTThreadSleep(1);
|
---|
1168 | #endif
|
---|
1169 | break;
|
---|
1170 |
|
---|
1171 | /*
|
---|
1172 | * Invalid states.
|
---|
1173 | */
|
---|
1174 | case TMTIMERSTATE_PENDING_DESTROY:
|
---|
1175 | case TMTIMERSTATE_PENDING_STOP_DESTROY:
|
---|
1176 | case TMTIMERSTATE_FREE:
|
---|
1177 | AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1178 | Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
1179 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
1180 | return ~(uint64_t)0;
|
---|
1181 | default:
|
---|
1182 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1183 | return ~(uint64_t)0;
|
---|
1184 | }
|
---|
1185 | } while (cRetries-- > 0);
|
---|
1186 |
|
---|
1187 | AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, R3STRING(pTimer->pszDesc)));
|
---|
1188 | Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
1189 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
1190 | return ~(uint64_t)0;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 |
|
---|
1194 | /**
|
---|
1195 | * Checks if a timer is active or not.
|
---|
1196 | *
|
---|
1197 | * @returns True if active.
|
---|
1198 | * @returns False if not active.
|
---|
1199 | * @param pTimer Timer handle as returned by one of the create functions.
|
---|
1200 | */
|
---|
1201 | VMMDECL(bool) TMTimerIsActive(PTMTIMER pTimer)
|
---|
1202 | {
|
---|
1203 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1204 | switch (enmState)
|
---|
1205 | {
|
---|
1206 | case TMTIMERSTATE_STOPPED:
|
---|
1207 | case TMTIMERSTATE_EXPIRED:
|
---|
1208 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1209 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1210 | Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
1211 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
1212 | return false;
|
---|
1213 |
|
---|
1214 | case TMTIMERSTATE_ACTIVE:
|
---|
1215 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1216 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1217 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1218 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1219 | Log2(("TMTimerIsActive: returns true (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
1220 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
1221 | return true;
|
---|
1222 |
|
---|
1223 | /*
|
---|
1224 | * Invalid states.
|
---|
1225 | */
|
---|
1226 | case TMTIMERSTATE_PENDING_DESTROY:
|
---|
1227 | case TMTIMERSTATE_PENDING_STOP_DESTROY:
|
---|
1228 | case TMTIMERSTATE_FREE:
|
---|
1229 | AssertMsgFailed(("Invalid timer state %s (%s)\n", tmTimerState(enmState), R3STRING(pTimer->pszDesc)));
|
---|
1230 | Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
|
---|
1231 | pTimer, tmTimerState(pTimer->enmState), R3STRING(pTimer->pszDesc)));
|
---|
1232 | return false;
|
---|
1233 | default:
|
---|
1234 | AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
|
---|
1235 | return false;
|
---|
1236 | }
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 |
|
---|
1240 | /**
|
---|
1241 | * Convert state to string.
|
---|
1242 | *
|
---|
1243 | * @returns Readonly status name.
|
---|
1244 | * @param enmState State.
|
---|
1245 | */
|
---|
1246 | const char *tmTimerState(TMTIMERSTATE enmState)
|
---|
1247 | {
|
---|
1248 | switch (enmState)
|
---|
1249 | {
|
---|
1250 | #define CASE(state) case state: return #state + sizeof("TMTIMERSTATE_") - 1
|
---|
1251 | CASE(TMTIMERSTATE_STOPPED);
|
---|
1252 | CASE(TMTIMERSTATE_ACTIVE);
|
---|
1253 | CASE(TMTIMERSTATE_EXPIRED);
|
---|
1254 | CASE(TMTIMERSTATE_PENDING_STOP);
|
---|
1255 | CASE(TMTIMERSTATE_PENDING_STOP_SCHEDULE);
|
---|
1256 | CASE(TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE);
|
---|
1257 | CASE(TMTIMERSTATE_PENDING_SCHEDULE);
|
---|
1258 | CASE(TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE);
|
---|
1259 | CASE(TMTIMERSTATE_PENDING_RESCHEDULE);
|
---|
1260 | CASE(TMTIMERSTATE_PENDING_STOP_DESTROY);
|
---|
1261 | CASE(TMTIMERSTATE_PENDING_DESTROY);
|
---|
1262 | CASE(TMTIMERSTATE_FREE);
|
---|
1263 | default:
|
---|
1264 | AssertMsgFailed(("Invalid state enmState=%d\n", enmState));
|
---|
1265 | return "Invalid state!";
|
---|
1266 | #undef CASE
|
---|
1267 | }
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 |
|
---|
1271 | /**
|
---|
1272 | * Schedules the given timer on the given queue.
|
---|
1273 | *
|
---|
1274 | * @param pQueue The timer queue.
|
---|
1275 | * @param pTimer The timer that needs scheduling.
|
---|
1276 | */
|
---|
1277 | DECLINLINE(void) tmTimerQueueScheduleOne(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
|
---|
1278 | {
|
---|
1279 | /*
|
---|
1280 | * Processing.
|
---|
1281 | */
|
---|
1282 | unsigned cRetries = 2;
|
---|
1283 | do
|
---|
1284 | {
|
---|
1285 | TMTIMERSTATE enmState = pTimer->enmState;
|
---|
1286 | switch (enmState)
|
---|
1287 | {
|
---|
1288 | /*
|
---|
1289 | * Reschedule timer (in the active list).
|
---|
1290 | */
|
---|
1291 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1292 | {
|
---|
1293 | if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE, TMTIMERSTATE_PENDING_RESCHEDULE)))
|
---|
1294 | break; /* retry */
|
---|
1295 |
|
---|
1296 | const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
|
---|
1297 | const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
|
---|
1298 | if (pPrev)
|
---|
1299 | TMTIMER_SET_NEXT(pPrev, pNext);
|
---|
1300 | else
|
---|
1301 | {
|
---|
1302 | TMTIMER_SET_HEAD(pQueue, pNext);
|
---|
1303 | pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
|
---|
1304 | }
|
---|
1305 | if (pNext)
|
---|
1306 | TMTIMER_SET_PREV(pNext, pPrev);
|
---|
1307 | pTimer->offNext = 0;
|
---|
1308 | pTimer->offPrev = 0;
|
---|
1309 | /* fall thru */
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /*
|
---|
1313 | * Schedule timer (insert into the active list).
|
---|
1314 | */
|
---|
1315 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1316 | {
|
---|
1317 | Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
|
---|
1318 | if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_ACTIVE, TMTIMERSTATE_PENDING_SCHEDULE)))
|
---|
1319 | break; /* retry */
|
---|
1320 |
|
---|
1321 | PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue);
|
---|
1322 | if (pCur)
|
---|
1323 | {
|
---|
1324 | const uint64_t u64Expire = pTimer->u64Expire;
|
---|
1325 | for (;; pCur = TMTIMER_GET_NEXT(pCur))
|
---|
1326 | {
|
---|
1327 | if (pCur->u64Expire > u64Expire)
|
---|
1328 | {
|
---|
1329 | const PTMTIMER pPrev = TMTIMER_GET_PREV(pCur);
|
---|
1330 | TMTIMER_SET_NEXT(pTimer, pCur);
|
---|
1331 | TMTIMER_SET_PREV(pTimer, pPrev);
|
---|
1332 | if (pPrev)
|
---|
1333 | TMTIMER_SET_NEXT(pPrev, pTimer);
|
---|
1334 | else
|
---|
1335 | {
|
---|
1336 | TMTIMER_SET_HEAD(pQueue, pTimer);
|
---|
1337 | pQueue->u64Expire = u64Expire;
|
---|
1338 | }
|
---|
1339 | TMTIMER_SET_PREV(pCur, pTimer);
|
---|
1340 | return;
|
---|
1341 | }
|
---|
1342 | if (!pCur->offNext)
|
---|
1343 | {
|
---|
1344 | TMTIMER_SET_NEXT(pCur, pTimer);
|
---|
1345 | TMTIMER_SET_PREV(pTimer, pCur);
|
---|
1346 | return;
|
---|
1347 | }
|
---|
1348 | }
|
---|
1349 | }
|
---|
1350 | else
|
---|
1351 | {
|
---|
1352 | TMTIMER_SET_HEAD(pQueue, pTimer);
|
---|
1353 | pQueue->u64Expire = pTimer->u64Expire;
|
---|
1354 | }
|
---|
1355 | return;
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | /*
|
---|
1359 | * Stop the timer in active list.
|
---|
1360 | */
|
---|
1361 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1362 | {
|
---|
1363 | if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, TMTIMERSTATE_PENDING_STOP)))
|
---|
1364 | break; /* retry */
|
---|
1365 |
|
---|
1366 | const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
|
---|
1367 | const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
|
---|
1368 | if (pPrev)
|
---|
1369 | TMTIMER_SET_NEXT(pPrev, pNext);
|
---|
1370 | else
|
---|
1371 | {
|
---|
1372 | TMTIMER_SET_HEAD(pQueue, pNext);
|
---|
1373 | pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
|
---|
1374 | }
|
---|
1375 | if (pNext)
|
---|
1376 | TMTIMER_SET_PREV(pNext, pPrev);
|
---|
1377 | pTimer->offNext = 0;
|
---|
1378 | pTimer->offPrev = 0;
|
---|
1379 | /* fall thru */
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | /*
|
---|
1383 | * Stop the timer (not on the active list).
|
---|
1384 | */
|
---|
1385 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1386 | Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
|
---|
1387 | if (RT_UNLIKELY(!tmTimerTry(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_PENDING_STOP_SCHEDULE)))
|
---|
1388 | break;
|
---|
1389 | return;
|
---|
1390 |
|
---|
1391 | /*
|
---|
1392 | * Stop & destroy the timer.
|
---|
1393 | */
|
---|
1394 | case TMTIMERSTATE_PENDING_STOP_DESTROY:
|
---|
1395 | {
|
---|
1396 | const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
|
---|
1397 | const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
|
---|
1398 | if (pPrev)
|
---|
1399 | TMTIMER_SET_NEXT(pPrev, pNext);
|
---|
1400 | else
|
---|
1401 | {
|
---|
1402 | TMTIMER_SET_HEAD(pQueue, pNext);
|
---|
1403 | pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
|
---|
1404 | }
|
---|
1405 | if (pNext)
|
---|
1406 | TMTIMER_SET_PREV(pNext, pPrev);
|
---|
1407 | pTimer->offNext = 0;
|
---|
1408 | pTimer->offPrev = 0;
|
---|
1409 | /* fall thru */
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 | /*
|
---|
1413 | * Destroy the timer.
|
---|
1414 | */
|
---|
1415 | case TMTIMERSTATE_PENDING_DESTROY:
|
---|
1416 | {
|
---|
1417 | Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
|
---|
1418 | PVM pVM = pTimer->CTXALLSUFF(pVM);
|
---|
1419 | const PTMTIMER pBigPrev = (PTMTIMER)(pTimer->pBigPrev ? MMHyperR3ToCC(pVM, pTimer->pBigPrev) : NULL);
|
---|
1420 | const PTMTIMER pBigNext = (PTMTIMER)(pTimer->pBigNext ? MMHyperR3ToCC(pVM, pTimer->pBigNext) : NULL);
|
---|
1421 |
|
---|
1422 | /* unlink from created list */
|
---|
1423 | if (pBigPrev)
|
---|
1424 | pBigPrev->pBigNext = pTimer->pBigNext;
|
---|
1425 | else
|
---|
1426 | pVM->tm.s.pCreated = pTimer->pBigNext;
|
---|
1427 | if (pBigNext)
|
---|
1428 | pBigNext->pBigPrev = pTimer->pBigPrev;
|
---|
1429 | pTimer->pBigNext = 0;
|
---|
1430 | pTimer->pBigPrev = 0;
|
---|
1431 |
|
---|
1432 | /* free */
|
---|
1433 | Log2(("TM: Inserting %p into the free list ahead of %p!\n", pTimer, pVM->tm.s.pFree));
|
---|
1434 | pTimer->pBigNext = pVM->tm.s.pFree;
|
---|
1435 | pVM->tm.s.pFree = (PTMTIMERR3)MMHyperCCToR3(pVM, pTimer);
|
---|
1436 | TM_SET_STATE(pTimer, TMTIMERSTATE_FREE);
|
---|
1437 | return;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | /*
|
---|
1441 | * Postpone these until they get into the right state.
|
---|
1442 | */
|
---|
1443 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1444 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1445 | tmTimerLink(pQueue, pTimer);
|
---|
1446 | STAM_COUNTER_INC(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatPostponed));
|
---|
1447 | return;
|
---|
1448 |
|
---|
1449 | /*
|
---|
1450 | * None of these can be in the schedule.
|
---|
1451 | */
|
---|
1452 | case TMTIMERSTATE_FREE:
|
---|
1453 | case TMTIMERSTATE_STOPPED:
|
---|
1454 | case TMTIMERSTATE_ACTIVE:
|
---|
1455 | case TMTIMERSTATE_EXPIRED:
|
---|
1456 | default:
|
---|
1457 | AssertMsgFailed(("Timer (%p) in the scheduling list has an invalid state %s (%d)!",
|
---|
1458 | pTimer, tmTimerState(pTimer->enmState), pTimer->enmState));
|
---|
1459 | return;
|
---|
1460 | }
|
---|
1461 | } while (cRetries-- > 0);
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 |
|
---|
1465 | /**
|
---|
1466 | * Schedules the specified timer queue.
|
---|
1467 | *
|
---|
1468 | * @param pVM The VM to run the timers for.
|
---|
1469 | * @param pQueue The queue to schedule.
|
---|
1470 | */
|
---|
1471 | void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue)
|
---|
1472 | {
|
---|
1473 | VM_ASSERT_EMT(pVM);
|
---|
1474 |
|
---|
1475 | /*
|
---|
1476 | * Dequeue the scheduling list and iterate it.
|
---|
1477 | */
|
---|
1478 | int32_t offNext = ASMAtomicXchgS32(&pQueue->offSchedule, 0);
|
---|
1479 | Log2(("tmTimerQueueSchedule: pQueue=%p:{.enmClock=%d, offNext=%RI32}\n", pQueue, pQueue->enmClock, offNext));
|
---|
1480 | if (!offNext)
|
---|
1481 | return;
|
---|
1482 | PTMTIMER pNext = (PTMTIMER)((intptr_t)pQueue + offNext);
|
---|
1483 | while (pNext)
|
---|
1484 | {
|
---|
1485 | /*
|
---|
1486 | * Unlink the head timer and find the next one.
|
---|
1487 | */
|
---|
1488 | PTMTIMER pTimer = pNext;
|
---|
1489 | pNext = pNext->offScheduleNext ? (PTMTIMER)((intptr_t)pNext + pNext->offScheduleNext) : NULL;
|
---|
1490 | pTimer->offScheduleNext = 0;
|
---|
1491 |
|
---|
1492 | /*
|
---|
1493 | * Do the scheduling.
|
---|
1494 | */
|
---|
1495 | Log2(("tmTimerQueueSchedule: pTimer=%p:{.enmState=%s, .enmClock=%d, .enmType=%d, .pszDesc=%s}\n",
|
---|
1496 | pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, R3STRING(pTimer->pszDesc)));
|
---|
1497 | tmTimerQueueScheduleOne(pQueue, pTimer);
|
---|
1498 | Log2(("tmTimerQueueSchedule: new %s\n", tmTimerState(pTimer->enmState)));
|
---|
1499 | } /* foreach timer in current schedule batch. */
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 |
|
---|
1503 | #ifdef VBOX_STRICT
|
---|
1504 | /**
|
---|
1505 | * Checks that the timer queues are sane.
|
---|
1506 | *
|
---|
1507 | * @param pVM VM handle.
|
---|
1508 | */
|
---|
1509 | void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere)
|
---|
1510 | {
|
---|
1511 | /*
|
---|
1512 | * Check the linking of the active lists.
|
---|
1513 | */
|
---|
1514 | for (int i = 0; i < TMCLOCK_MAX; i++)
|
---|
1515 | {
|
---|
1516 | PTMTIMERQUEUE pQueue = &pVM->tm.s.CTXALLSUFF(paTimerQueues)[i];
|
---|
1517 | Assert((int)pQueue->enmClock == i);
|
---|
1518 | PTMTIMER pPrev = NULL;
|
---|
1519 | for (PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue); pCur; pPrev = pCur, pCur = TMTIMER_GET_NEXT(pCur))
|
---|
1520 | {
|
---|
1521 | AssertMsg((int)pCur->enmClock == i, ("%s: %d != %d\n", pszWhere, pCur->enmClock, i));
|
---|
1522 | AssertMsg(TMTIMER_GET_PREV(pCur) == pPrev, ("%s: %p != %p\n", pszWhere, TMTIMER_GET_PREV(pCur), pPrev));
|
---|
1523 | TMTIMERSTATE enmState = pCur->enmState;
|
---|
1524 | switch (enmState)
|
---|
1525 | {
|
---|
1526 | case TMTIMERSTATE_ACTIVE:
|
---|
1527 | AssertMsg(!pCur->offScheduleNext, ("%s: %RI32\n", pszWhere, pCur->offScheduleNext));
|
---|
1528 | break;
|
---|
1529 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1530 | case TMTIMERSTATE_PENDING_STOP_DESTROY:
|
---|
1531 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1532 | break;
|
---|
1533 | default:
|
---|
1534 | AssertMsgFailed(("%s: Invalid state enmState=%d %s\n", pszWhere, enmState, tmTimerState(enmState)));
|
---|
1535 | break;
|
---|
1536 | }
|
---|
1537 | }
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 |
|
---|
1541 | # ifdef IN_RING3
|
---|
1542 | /*
|
---|
1543 | * Do the big list and check that active timers all are in the active lists.
|
---|
1544 | */
|
---|
1545 | PTMTIMERR3 pPrev = NULL;
|
---|
1546 | for (PTMTIMERR3 pCur = pVM->tm.s.pCreated; pCur; pPrev = pCur, pCur = pCur->pBigNext)
|
---|
1547 | {
|
---|
1548 | Assert(pCur->pBigPrev == pPrev);
|
---|
1549 | Assert((unsigned)pCur->enmClock < (unsigned)TMCLOCK_MAX);
|
---|
1550 |
|
---|
1551 | TMTIMERSTATE enmState = pCur->enmState;
|
---|
1552 | switch (enmState)
|
---|
1553 | {
|
---|
1554 | case TMTIMERSTATE_ACTIVE:
|
---|
1555 | case TMTIMERSTATE_PENDING_STOP:
|
---|
1556 | case TMTIMERSTATE_PENDING_STOP_DESTROY:
|
---|
1557 | case TMTIMERSTATE_PENDING_RESCHEDULE:
|
---|
1558 | case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
|
---|
1559 | {
|
---|
1560 | PTMTIMERR3 pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTXALLSUFF(paTimerQueues)[pCur->enmClock]);
|
---|
1561 | Assert(pCur->offPrev || pCur == pCurAct);
|
---|
1562 | while (pCurAct && pCurAct != pCur)
|
---|
1563 | pCurAct = TMTIMER_GET_NEXT(pCurAct);
|
---|
1564 | Assert(pCurAct == pCur);
|
---|
1565 | break;
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | case TMTIMERSTATE_PENDING_DESTROY:
|
---|
1569 | case TMTIMERSTATE_PENDING_SCHEDULE:
|
---|
1570 | case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
|
---|
1571 | case TMTIMERSTATE_STOPPED:
|
---|
1572 | case TMTIMERSTATE_EXPIRED:
|
---|
1573 | {
|
---|
1574 | Assert(!pCur->offNext);
|
---|
1575 | Assert(!pCur->offPrev);
|
---|
1576 | for (PTMTIMERR3 pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTXALLSUFF(paTimerQueues)[pCur->enmClock]);
|
---|
1577 | pCurAct;
|
---|
1578 | pCurAct = TMTIMER_GET_NEXT(pCurAct))
|
---|
1579 | {
|
---|
1580 | Assert(pCurAct != pCur);
|
---|
1581 | Assert(TMTIMER_GET_NEXT(pCurAct) != pCur);
|
---|
1582 | Assert(TMTIMER_GET_PREV(pCurAct) != pCur);
|
---|
1583 | }
|
---|
1584 | break;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 | /* ignore */
|
---|
1588 | case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
|
---|
1589 | break;
|
---|
1590 |
|
---|
1591 | /* shouldn't get here! */
|
---|
1592 | default:
|
---|
1593 | AssertMsgFailed(("Invalid state enmState=%d %s\n", enmState, tmTimerState(enmState)));
|
---|
1594 | break;
|
---|
1595 | }
|
---|
1596 | }
|
---|
1597 | # endif /* IN_RING3 */
|
---|
1598 | }
|
---|
1599 | #endif /* !VBOX_STRICT */
|
---|
1600 |
|
---|