1 | /* $Id: TMInternal.h 23 2007-01-15 14:08:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TM - Internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef __TMInternal_h__
|
---|
23 | #define __TMInternal_h__
|
---|
24 |
|
---|
25 | #include <VBox/cdefs.h>
|
---|
26 | #include <VBox/types.h>
|
---|
27 | #include <iprt/timer.h>
|
---|
28 | #include <VBox/stam.h>
|
---|
29 |
|
---|
30 | __BEGIN_DECLS
|
---|
31 |
|
---|
32 |
|
---|
33 | /** @defgroup grp_tm_int Internal
|
---|
34 | * @ingroup grp_tm
|
---|
35 | * @internal
|
---|
36 | * @{
|
---|
37 | */
|
---|
38 |
|
---|
39 | /** Frequency of the real clock. */
|
---|
40 | #define TMCLOCK_FREQ_REAL UINT32_C(1000)
|
---|
41 | /** Frequency of the virtual clock. */
|
---|
42 | #define TMCLOCK_FREQ_VIRTUAL UINT32_C(1000000000)
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Timer type.
|
---|
47 | */
|
---|
48 | typedef enum TMTIMERTYPE
|
---|
49 | {
|
---|
50 | /** Device timer. */
|
---|
51 | TMTIMERTYPE_DEV = 1,
|
---|
52 | /** Driver timer. */
|
---|
53 | TMTIMERTYPE_DRV,
|
---|
54 | /** Internal timer . */
|
---|
55 | TMTIMERTYPE_INTERNAL,
|
---|
56 | /** External timer. */
|
---|
57 | TMTIMERTYPE_EXTERNAL
|
---|
58 | } TMTIMERTYPE;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Timer state
|
---|
62 | */
|
---|
63 | typedef enum TMTIMERSTATE
|
---|
64 | {
|
---|
65 | /** Timer is stopped. */
|
---|
66 | TMTIMERSTATE_STOPPED = 1,
|
---|
67 | /** Timer is active. */
|
---|
68 | TMTIMERSTATE_ACTIVE,
|
---|
69 | /** Timer is expired, is being delivered. */
|
---|
70 | TMTIMERSTATE_EXPIRED,
|
---|
71 |
|
---|
72 | /** Timer is stopped but still in the active list.
|
---|
73 | * Currently in the ScheduleTimers list. */
|
---|
74 | TMTIMERSTATE_PENDING_STOP,
|
---|
75 | /** Timer is stopped but needs unlinking from the ScheduleTimers list.
|
---|
76 | * Currently in the ScheduleTimers list. */
|
---|
77 | TMTIMERSTATE_PENDING_STOP_SCHEDULE,
|
---|
78 | /** Timer is being modified and will soon be pending scheduling.
|
---|
79 | * Currently in the ScheduleTimers list. */
|
---|
80 | TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE,
|
---|
81 | /** Timer is pending scheduling.
|
---|
82 | * Currently in the ScheduleTimers list. */
|
---|
83 | TMTIMERSTATE_PENDING_SCHEDULE,
|
---|
84 | /** Timer is being modified and will soon be pending rescheduling.
|
---|
85 | * Currently in the ScheduleTimers list and the active list. */
|
---|
86 | TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE,
|
---|
87 | /** Timer is modified and is now pending rescheduling.
|
---|
88 | * Currently in the ScheduleTimers list and the active list. */
|
---|
89 | TMTIMERSTATE_PENDING_RESCHEDULE,
|
---|
90 | /** Timer is destroyed but needs to be replaced from the
|
---|
91 | * active to the free list.
|
---|
92 | * Currently in the ScheduleTimers list and the active list. */
|
---|
93 | TMTIMERSTATE_PENDING_STOP_DESTROY,
|
---|
94 | /** Timer is destroyed but needs moving to the free list.
|
---|
95 | * Currently in the ScheduleTimers list. */
|
---|
96 | TMTIMERSTATE_PENDING_DESTROY,
|
---|
97 | /** Timer is free. */
|
---|
98 | TMTIMERSTATE_FREE
|
---|
99 | } TMTIMERSTATE;
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Internal representation of a timer.
|
---|
104 | *
|
---|
105 | * For correct serialization (without the use of semaphores and
|
---|
106 | * other blocking/slow constructs) certain rules applies to updating
|
---|
107 | * this structure:
|
---|
108 | * - For thread other than EMT only u64Expire, enmState and pScheduleNext*
|
---|
109 | * are changeable. Everything else is out of bounds.
|
---|
110 | * - Updating of u64Expire timer can only happen in the TMTIMERSTATE_STOPPED
|
---|
111 | * and TMTIMERSTATE_PENDING_RESCHEDULING_SET_EXPIRE states.
|
---|
112 | * - Timers in the TMTIMERSTATE_EXPIRED state are only accessible from EMT.
|
---|
113 | * - Actual destruction of a timer can only be done at scheduling time.
|
---|
114 | */
|
---|
115 | typedef struct TMTIMER
|
---|
116 | {
|
---|
117 | /** Expire time. */
|
---|
118 | volatile uint64_t u64Expire;
|
---|
119 | /** Clock to apply to u64Expire. */
|
---|
120 | TMCLOCK enmClock;
|
---|
121 | /** Timer callback type. */
|
---|
122 | TMTIMERTYPE enmType;
|
---|
123 | /** Type specific data. */
|
---|
124 | union
|
---|
125 | {
|
---|
126 | /** TMTIMERTYPE_DEV. */
|
---|
127 | struct
|
---|
128 | {
|
---|
129 | /** Callback. */
|
---|
130 | R3PTRTYPE(PFNTMTIMERDEV) pfnTimer;
|
---|
131 | /** Device instance. */
|
---|
132 | R3PTRTYPE(PPDMDEVINS) pDevIns;
|
---|
133 | } Dev;
|
---|
134 |
|
---|
135 | /** TMTIMERTYPE_DRV. */
|
---|
136 | struct
|
---|
137 | {
|
---|
138 | /** Callback. */
|
---|
139 | R3PTRTYPE(PFNTMTIMERDRV) pfnTimer;
|
---|
140 | /** Device instance. */
|
---|
141 | R3PTRTYPE(PPDMDRVINS) pDrvIns;
|
---|
142 | } Drv;
|
---|
143 |
|
---|
144 | /** TMTIMERTYPE_INTERNAL. */
|
---|
145 | struct
|
---|
146 | {
|
---|
147 | /** Callback. */
|
---|
148 | R3PTRTYPE(PFNTMTIMERINT) pfnTimer;
|
---|
149 | /** User argument. */
|
---|
150 | R3PTRTYPE(void *) pvUser;
|
---|
151 | } Internal;
|
---|
152 |
|
---|
153 | /** TMTIMERTYPE_EXTERNAL. */
|
---|
154 | struct
|
---|
155 | {
|
---|
156 | /** Callback. */
|
---|
157 | R3PTRTYPE(PFNTMTIMEREXT) pfnTimer;
|
---|
158 | /** User data. */
|
---|
159 | R3PTRTYPE(void *) pvUser;
|
---|
160 | } External;
|
---|
161 | } u;
|
---|
162 |
|
---|
163 | /** Timer state. */
|
---|
164 | volatile TMTIMERSTATE enmState;
|
---|
165 | /** Timer relative offset to the next timer in the schedule list. */
|
---|
166 | int32_t offScheduleNext;
|
---|
167 |
|
---|
168 | /** Timer relative offset to the next timer in the chain. */
|
---|
169 | int32_t offNext;
|
---|
170 | /** Timer relative offset to the previous timer in the chain. */
|
---|
171 | int32_t offPrev;
|
---|
172 |
|
---|
173 | /** Pointer to the next timer in the list of created or free timers. (TM::pTimers or TM::pFree) */
|
---|
174 | PTMTIMERR3 pBigNext;
|
---|
175 | /** Pointer to the previous timer in the list of all created timers. (TM::pTimers) */
|
---|
176 | PTMTIMERR3 pBigPrev;
|
---|
177 | /** Pointer to the timer description. */
|
---|
178 | HCPTRTYPE(const char *) pszDesc;
|
---|
179 | /** Pointer to the VM the timer belongs to - R3 Ptr. */
|
---|
180 | PVMR3 pVMR3;
|
---|
181 | /** Pointer to the VM the timer belongs to - R0 Ptr. */
|
---|
182 | PVMR0 pVMR0;
|
---|
183 | /** Pointer to the VM the timer belongs to - GC Ptr. */
|
---|
184 | PVMGC pVMGC;
|
---|
185 | } TMTIMER;
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Updates a timer state in the correct atomic manner.
|
---|
190 | */
|
---|
191 | #if 1
|
---|
192 | # define TM_SET_STATE(pTimer, state) \
|
---|
193 | ASMAtomicXchgSize(&(pTimer)->enmState, state)
|
---|
194 | #else
|
---|
195 | # define TM_SET_STATE(pTimer, state) \
|
---|
196 | do { Log(("%s: %p: %d -> %d\n", __FUNCTION__, (pTimer), (pTimer)->enmState, state)); \
|
---|
197 | ASMAtomicXchgSize(&(pTimer)->enmState, state);\
|
---|
198 | } while (0)
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Tries to updates a timer state in the correct atomic manner.
|
---|
203 | */
|
---|
204 | #if 1
|
---|
205 | # define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
|
---|
206 | ASMAtomicCmpXchgSize(&(pTimer)->enmState, StateNew, StateOld, fRc)
|
---|
207 | #else
|
---|
208 | # define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
|
---|
209 | do { ASMAtomicCmpXchgSize(&(pTimer)->enmState, StateNew, StateOld, fRc); \
|
---|
210 | Log(("%s: %p: %d -> %d %RTbool\n", __FUNCTION__, (pTimer), StateOld, StateNew, fRc)); \
|
---|
211 | } while (0)
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | /** Get the previous timer. */
|
---|
215 | #define TMTIMER_GET_PREV(pTimer) ((PTMTIMER)((pTimer)->offPrev ? (intptr_t)(pTimer) + (pTimer)->offPrev : 0))
|
---|
216 | /** Get the next timer. */
|
---|
217 | #define TMTIMER_GET_NEXT(pTimer) ((PTMTIMER)((pTimer)->offNext ? (intptr_t)(pTimer) + (pTimer)->offNext : 0))
|
---|
218 | /** Set the previous timer link. */
|
---|
219 | #define TMTIMER_SET_PREV(pTimer, pPrev) ((pTimer)->offPrev = (pPrev) ? (intptr_t)(pPrev) - (intptr_t)(pTimer) : 0)
|
---|
220 | /** Set the next timer link. */
|
---|
221 | #define TMTIMER_SET_NEXT(pTimer, pNext) ((pTimer)->offNext = (pNext) ? (intptr_t)(pNext) - (intptr_t)(pTimer) : 0)
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * A timer queue.
|
---|
226 | *
|
---|
227 | * This is allocated on the hyper heap.
|
---|
228 | */
|
---|
229 | typedef struct TMTIMERQUEUE
|
---|
230 | {
|
---|
231 | /** The cached expire time for this queue.
|
---|
232 | * Updated by EMT when scheduling the queue or modifying the head timer.
|
---|
233 | * Assigned UINT64_MAX when there is no head timer. */
|
---|
234 | uint64_t u64Expire;
|
---|
235 | /** Doubly linked list of active timers.
|
---|
236 | *
|
---|
237 | * When no scheduling is pending, this list is will be ordered by expire time (ascending).
|
---|
238 | * Access is serialized by only letting the emulation thread (EMT) do changes.
|
---|
239 | *
|
---|
240 | * The offset is relative to the queue structure.
|
---|
241 | */
|
---|
242 | int32_t offActive;
|
---|
243 | /** List of timers pending scheduling of some kind.
|
---|
244 | *
|
---|
245 | * Timer stats allowed in the list are TMTIMERSTATE_PENDING_STOPPING,
|
---|
246 | * TMTIMERSTATE_PENDING_DESTRUCTION, TMTIMERSTATE_PENDING_STOPPING_DESTRUCTION,
|
---|
247 | * TMTIMERSTATE_PENDING_RESCHEDULING and TMTIMERSTATE_PENDING_SCHEDULE.
|
---|
248 | *
|
---|
249 | * The offset is relative to the queue structure.
|
---|
250 | */
|
---|
251 | int32_t volatile offSchedule;
|
---|
252 | /** The clock for this queue. */
|
---|
253 | TMCLOCK enmClock;
|
---|
254 | /** Pad the structure up to 32 bytes. */
|
---|
255 | uint32_t au32Padding[3];
|
---|
256 | } TMTIMERQUEUE;
|
---|
257 |
|
---|
258 | /** Pointer to a timer queue. */
|
---|
259 | typedef TMTIMERQUEUE *PTMTIMERQUEUE;
|
---|
260 |
|
---|
261 | /** Get the head of the active timer list. */
|
---|
262 | #define TMTIMER_GET_HEAD(pQueue) ((PTMTIMER)((pQueue)->offActive ? (intptr_t)(pQueue) + (pQueue)->offActive : 0))
|
---|
263 | /** Set the head of the active timer list. */
|
---|
264 | #define TMTIMER_SET_HEAD(pQueue, pHead) ((pQueue)->offActive = pHead ? (intptr_t)pHead - (intptr_t)(pQueue) : 0)
|
---|
265 |
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Converts a TM pointer into a VM pointer.
|
---|
269 | * @returns Pointer to the VM structure the TM is part of.
|
---|
270 | * @param pTM Pointer to TM instance data.
|
---|
271 | */
|
---|
272 | #define TM2VM(pTM) ( (PVM)((char*)pTM - pTM->offVM) )
|
---|
273 |
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * TM VM Instance data.
|
---|
277 | * Changes to this must checked against the padding of the cfgm union in VM!
|
---|
278 | */
|
---|
279 | typedef struct TM
|
---|
280 | {
|
---|
281 | /** Offset to the VM structure.
|
---|
282 | * See TM2VM(). */
|
---|
283 | RTUINT offVM;
|
---|
284 |
|
---|
285 | /** CPU timestamp ticking enabled indicator (bool). (RDTSC) */
|
---|
286 | bool fTSCTicking;
|
---|
287 | /** The offset between the host TSC and the Guest TSC.
|
---|
288 | * Only valid if fTicking is set. */
|
---|
289 | uint64_t u64TSCOffset;
|
---|
290 | /** The guest TSC when fTicking is cleared. */
|
---|
291 | uint64_t u64TSC;
|
---|
292 | /** The number of CPU clock ticks per second (TMCLOCK_TSC).
|
---|
293 | * If GIP is available, g_pSUPGlobalInfoPage->u64CpuHz will be used instead. */
|
---|
294 | uint64_t cTSCTicksPerSecond;
|
---|
295 |
|
---|
296 | /** Padding to ensure that 64-bit values are equaly aligned everywhere. */
|
---|
297 | uint32_t uReserved;
|
---|
298 | /** Virtual time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL) */
|
---|
299 | bool fVirtualTicking;
|
---|
300 | /** Virtual timer synchronous time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL_SYNC) */
|
---|
301 | bool fVirtualSyncTicking;
|
---|
302 | /** Virtual timer synchronous time catch-up active. */
|
---|
303 | bool volatile fVirtualSyncCatchUp;
|
---|
304 |
|
---|
305 | /** The offset of the virtual clock relative to it's timesource.
|
---|
306 | * Only valid if fVirtualTicking is set. */
|
---|
307 | uint64_t u64VirtualOffset;
|
---|
308 | /** The guest virtual time when fVirtualTicking is cleared. */
|
---|
309 | uint64_t u64Virtual;
|
---|
310 |
|
---|
311 | /** The offset of the virtual timer synchronous clock (TMCLOCK_VIRTUAL_SYNC) relative
|
---|
312 | * to the virtual clock. */
|
---|
313 | uint64_t volatile u64VirtualSyncOffset;
|
---|
314 | /** The TMCLOCK_VIRTUAL at the previous TMVirtualGetSync call when catch-up is active. */
|
---|
315 | uint64_t volatile u64VirtualSyncCatchUpPrev;
|
---|
316 | /** The guest virtual timer synchronous time when fVirtualSyncTicking is cleared. */
|
---|
317 | uint64_t u64VirtualSync;
|
---|
318 | /** How many precent faster the clock should advance when catch-up is active. */
|
---|
319 | uint32_t u32VirtualSyncCatchupPrecentage;
|
---|
320 | /** When to stop catch-up. */
|
---|
321 | uint32_t u32VirtualSyncCatchupStopThreashold;
|
---|
322 | /** When to start catch-up. */
|
---|
323 | uint64_t u64VirtualSyncCatchupStartTreashold;
|
---|
324 | /** When to give up catch-up. */
|
---|
325 | uint64_t u64VirtualSyncCatchupGiveUpTreashold;
|
---|
326 |
|
---|
327 | /** Timer queues for the different clock types - R3 Ptr */
|
---|
328 | R3PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR3;
|
---|
329 | /** Timer queues for the different clock types - R0 Ptr */
|
---|
330 | R0PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR0;
|
---|
331 | /** Timer queues for the different clock types - GC Ptr */
|
---|
332 | GCPTRTYPE(PTMTIMERQUEUE) paTimerQueuesGC;
|
---|
333 |
|
---|
334 | /** Pointer to our GC mapping of the GIP. */
|
---|
335 | GCPTRTYPE(void *) pvGIPGC;
|
---|
336 | /** Pointer to our R3 mapping of the GIP. */
|
---|
337 | R3PTRTYPE(void *) pvGIPR3;
|
---|
338 |
|
---|
339 | /** Pointer to a singly linked list of free timers.
|
---|
340 | * This chain is using the TMTIMER::pBigNext members.
|
---|
341 | * Only accessible from the emulation thread. */
|
---|
342 | PTMTIMERR3 pFree;
|
---|
343 |
|
---|
344 | /** Pointer to a doubly linked list of created timers.
|
---|
345 | * This chain is using the TMTIMER::pBigNext and TMTIMER::pBigPrev members.
|
---|
346 | * Only accessible from the emulation thread. */
|
---|
347 | PTMTIMERR3 pCreated;
|
---|
348 |
|
---|
349 | /** The schedulation timer timer handle (runtime timer).
|
---|
350 | * This timer will do freqent check on pending queue schedulations and
|
---|
351 | * raise VM_FF_TIMER to pull EMTs attention to them.
|
---|
352 | */
|
---|
353 | HCPTRTYPE(PRTTIMER) pTimer;
|
---|
354 | /** Interval in milliseconds of the pTimer timer. */
|
---|
355 | uint32_t u32TimerMillies;
|
---|
356 |
|
---|
357 | /** Alignment padding to ensure that the statistics are 64-bit aligned when using GCC. */
|
---|
358 | uint32_t u32Padding;
|
---|
359 |
|
---|
360 | /** TMR3TimerQueuesDo
|
---|
361 | * @{ */
|
---|
362 | STAMPROFILE StatDoQueues;
|
---|
363 | STAMPROFILEADV StatDoQueuesSchedule;
|
---|
364 | STAMPROFILEADV StatDoQueuesRun;
|
---|
365 | /** @} */
|
---|
366 | /** tmSchedule
|
---|
367 | * @{ */
|
---|
368 | STAMPROFILE StatScheduleOneGC;
|
---|
369 | STAMPROFILE StatScheduleOneR0;
|
---|
370 | STAMPROFILE StatScheduleOneR3;
|
---|
371 | STAMCOUNTER StatScheduleSetFF;
|
---|
372 | /** @} */
|
---|
373 | STAMCOUNTER StatVirtualGet;
|
---|
374 | STAMCOUNTER StatVirtualGetSync;
|
---|
375 | STAMCOUNTER StatVirtualPause;
|
---|
376 | STAMCOUNTER StatVirtualResume;
|
---|
377 | /** TMTimerPoll
|
---|
378 | * @{ */
|
---|
379 | STAMCOUNTER StatPollAlreadySet;
|
---|
380 | STAMCOUNTER StatPollVirtual;
|
---|
381 | STAMCOUNTER StatPollVirtualSync;
|
---|
382 | STAMCOUNTER StatPollMiss;
|
---|
383 | /** @} */
|
---|
384 | /** TMTimerSet
|
---|
385 | * @{ */
|
---|
386 | STAMPROFILE StatTimerSetGC;
|
---|
387 | STAMPROFILE StatTimerSetR0;
|
---|
388 | STAMPROFILE StatTimerSetR3;
|
---|
389 | /** @} */
|
---|
390 | /** TMTimerStop
|
---|
391 | * @{ */
|
---|
392 | STAMPROFILE StatTimerStopGC;
|
---|
393 | STAMPROFILE StatTimerStopR0;
|
---|
394 | STAMPROFILE StatTimerStopR3;
|
---|
395 | /** @} */
|
---|
396 | /**
|
---|
397 | * @{ */
|
---|
398 | STAMCOUNTER StatPostponedR3;
|
---|
399 | STAMCOUNTER StatPostponedR0;
|
---|
400 | STAMCOUNTER StatPostponedGC;
|
---|
401 | /** @} */
|
---|
402 | /** The timer callback. */
|
---|
403 | STAMCOUNTER StatTimerCallbackSetFF;
|
---|
404 |
|
---|
405 | } TM;
|
---|
406 | /** Pointer to TM VM instance data. */
|
---|
407 | typedef TM *PTM;
|
---|
408 |
|
---|
409 |
|
---|
410 | const char *tmTimerState(TMTIMERSTATE enmState);
|
---|
411 | void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue);
|
---|
412 | #ifdef VBOX_STRICT
|
---|
413 | void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere);
|
---|
414 | #endif
|
---|
415 |
|
---|
416 | /** @} */
|
---|
417 |
|
---|
418 | __END_DECLS
|
---|
419 |
|
---|
420 | #endif
|
---|
421 |
|
---|