VirtualBox

source: vbox/trunk/src/VBox/VMM/TMInternal.h@ 3068

Last change on this file since 3068 was 2981, checked in by vboxsync, 18 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 18.6 KB
Line 
1/* $Id: TMInternal.h 2981 2007-06-01 16:01:28Z vboxsync $ */
2/** @file
3 * TM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek 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 */
48typedef 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 */
63typedef 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 */
115typedef 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#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
186 RTGCPTR padding0; /**< pad structure to multiple of 8 bytes. */
187#endif
188} TMTIMER;
189
190
191/**
192 * Updates a timer state in the correct atomic manner.
193 */
194#if 1
195# define TM_SET_STATE(pTimer, state) \
196 ASMAtomicXchgSize(&(pTimer)->enmState, state)
197#else
198# define TM_SET_STATE(pTimer, state) \
199 do { Log(("%s: %p: %d -> %d\n", __FUNCTION__, (pTimer), (pTimer)->enmState, state)); \
200 ASMAtomicXchgSize(&(pTimer)->enmState, state);\
201 } while (0)
202#endif
203
204/**
205 * Tries to updates a timer state in the correct atomic manner.
206 */
207#if 1
208# define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
209 ASMAtomicCmpXchgSize(&(pTimer)->enmState, StateNew, StateOld, fRc)
210#else
211# define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
212 do { ASMAtomicCmpXchgSize(&(pTimer)->enmState, StateNew, StateOld, fRc); \
213 Log(("%s: %p: %d -> %d %RTbool\n", __FUNCTION__, (pTimer), StateOld, StateNew, fRc)); \
214 } while (0)
215#endif
216
217/** Get the previous timer. */
218#define TMTIMER_GET_PREV(pTimer) ((PTMTIMER)((pTimer)->offPrev ? (intptr_t)(pTimer) + (pTimer)->offPrev : 0))
219/** Get the next timer. */
220#define TMTIMER_GET_NEXT(pTimer) ((PTMTIMER)((pTimer)->offNext ? (intptr_t)(pTimer) + (pTimer)->offNext : 0))
221/** Set the previous timer link. */
222#define TMTIMER_SET_PREV(pTimer, pPrev) ((pTimer)->offPrev = (pPrev) ? (intptr_t)(pPrev) - (intptr_t)(pTimer) : 0)
223/** Set the next timer link. */
224#define TMTIMER_SET_NEXT(pTimer, pNext) ((pTimer)->offNext = (pNext) ? (intptr_t)(pNext) - (intptr_t)(pTimer) : 0)
225
226
227/**
228 * A timer queue.
229 *
230 * This is allocated on the hyper heap.
231 */
232typedef struct TMTIMERQUEUE
233{
234 /** The cached expire time for this queue.
235 * Updated by EMT when scheduling the queue or modifying the head timer.
236 * Assigned UINT64_MAX when there is no head timer. */
237 uint64_t u64Expire;
238 /** Doubly linked list of active timers.
239 *
240 * When no scheduling is pending, this list is will be ordered by expire time (ascending).
241 * Access is serialized by only letting the emulation thread (EMT) do changes.
242 *
243 * The offset is relative to the queue structure.
244 */
245 int32_t offActive;
246 /** List of timers pending scheduling of some kind.
247 *
248 * Timer stats allowed in the list are TMTIMERSTATE_PENDING_STOPPING,
249 * TMTIMERSTATE_PENDING_DESTRUCTION, TMTIMERSTATE_PENDING_STOPPING_DESTRUCTION,
250 * TMTIMERSTATE_PENDING_RESCHEDULING and TMTIMERSTATE_PENDING_SCHEDULE.
251 *
252 * The offset is relative to the queue structure.
253 */
254 int32_t volatile offSchedule;
255 /** The clock for this queue. */
256 TMCLOCK enmClock;
257 /** Pad the structure up to 32 bytes. */
258 uint32_t au32Padding[3];
259} TMTIMERQUEUE;
260
261/** Pointer to a timer queue. */
262typedef TMTIMERQUEUE *PTMTIMERQUEUE;
263
264/** Get the head of the active timer list. */
265#define TMTIMER_GET_HEAD(pQueue) ((PTMTIMER)((pQueue)->offActive ? (intptr_t)(pQueue) + (pQueue)->offActive : 0))
266/** Set the head of the active timer list. */
267#define TMTIMER_SET_HEAD(pQueue, pHead) ((pQueue)->offActive = pHead ? (intptr_t)pHead - (intptr_t)(pQueue) : 0)
268
269
270/**
271 * Converts a TM pointer into a VM pointer.
272 * @returns Pointer to the VM structure the TM is part of.
273 * @param pTM Pointer to TM instance data.
274 */
275#define TM2VM(pTM) ( (PVM)((char*)pTM - pTM->offVM) )
276
277
278/**
279 * TM VM Instance data.
280 * Changes to this must checked against the padding of the cfgm union in VM!
281 */
282typedef struct TM
283{
284 /** Offset to the VM structure.
285 * See TM2VM(). */
286 RTUINT offVM;
287
288 /** Flag indicating that the host TSC is suitable for use in AMD-V and VT-x mode.
289 * Config variable: MaybeUseOffsettedHostTSC (boolean) */
290 bool fMaybeUseOffsettedHostTSC;
291 /** CPU timestamp ticking enabled indicator (bool). (RDTSC) */
292 bool fTSCTicking;
293 /** Set if we fully virtualize the TSC, i.e. intercept all rdtsc instructions.
294 * Config variable: TSCVirtualized (bool) */
295 bool fTSCVirtualized;
296 /** Set if we use the real TSC as time source or if we use the virtual clock.
297 * If fTSCVirtualized is set we maintain a offset to the TSC and pausing/resuming the
298 * ticking. fTSCVirtualized = false implies fTSCUseRealTSC = true.
299 * Config variable: TSCUseRealTSC (bool) */
300 bool fTSCUseRealTSC;
301 /** The offset between the host TSC and the Guest TSC.
302 * Only valid if fTicking is set and and fTSCUseRealTSC is clear. */
303 uint64_t u64TSCOffset;
304 /** The guest TSC when fTicking is cleared. */
305 uint64_t u64TSC;
306 /** The number of CPU clock ticks per second (TMCLOCK_TSC).
307 * Config variable: TSCTicksPerSecond (64-bit unsigned int)
308 * The config variable implies fTSCVirtualized = true and fTSCUseRealTSC = false. */
309 uint64_t cTSCTicksPerSecond;
310
311 /** Virtual time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL) */
312 bool fVirtualTicking;
313 /** Virtual time is not running at 100%. */
314 bool fVirtualWarpDrive;
315 /** Virtual timer synchronous time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL_SYNC) */
316 bool volatile fVirtualSyncTicking;
317 /** Virtual timer synchronous time catch-up active. */
318 bool volatile fVirtualSyncCatchUp;
319 /** WarpDrive percentage.
320 * 100% is normal (fVirtualSyncNormal == true). When other than 100% we apply
321 * this percentage to the raw time source for the period it's been valid in,
322 * i.e. since u64VirtualWarpDriveStart. */
323 uint32_t u32VirtualWarpDrivePercentage;
324
325 /** The offset of the virtual clock relative to it's timesource.
326 * Only valid if fVirtualTicking is set. */
327 uint64_t u64VirtualOffset;
328 /** The guest virtual time when fVirtualTicking is cleared. */
329 uint64_t u64Virtual;
330 /** When the Warp drive was started or last adjusted.
331 * Only valid when fVirtualWarpDrive is set. */
332 uint64_t u64VirtualWarpDriveStart;
333 /** The previously returned nano TS.
334 * This handles TSC drift on SMP systems and expired interval.
335 * This is a valid range u64NanoTS to u64NanoTS + 1000000000 (ie. 1sec). */
336 uint64_t volatile u64VirtualRawPrev;
337 /** The number of times we've had to resort to 1ns walking. */
338 uint32_t volatile c1nsVirtualRawSteps;
339 /** Number of times u64VirtualRawPrev has been considered bad. */
340 uint32_t volatile cVirtualRawBadRawPrev;
341
342 /** The guest virtual timer synchronous time when fVirtualSyncTicking is cleared. */
343 uint64_t volatile u64VirtualSync;
344 /** The offset of the timer synchronous virtual clock (TMCLOCK_VIRTUAL_SYNC) relative
345 * to the virtual clock (TMCLOCK_VIRTUAL).
346 * (This is accessed by the timer thread and must be updated atomically.) */
347 uint64_t volatile offVirtualSync;
348 /** The offset into offVirtualSync that's been irrevocably given up by failed catch-up attempts.
349 * Thus the current lag is offVirtualSync - offVirtualSyncGivenUp. */
350 uint64_t offVirtualSyncGivenUp;
351 /** The TMCLOCK_VIRTUAL at the previous TMVirtualGetSync call when catch-up is active. */
352 uint64_t volatile u64VirtualSyncCatchUpPrev;
353 /** The current catch-up percentage. */
354 uint32_t volatile u32VirtualSyncCatchUpPercentage;
355 /** How much slack when processing timers. */
356 uint32_t u32VirtualSyncScheduleSlack;
357 /** When to stop catch-up. */
358 uint64_t u64VirtualSyncCatchUpStopThreshold;
359 /** When to give up catch-up. */
360 uint64_t u64VirtualSyncCatchUpGiveUpThreshold;
361/** @def TM_MAX_CATCHUP_PERIODS
362 * The number of catchup rates. */
363#define TM_MAX_CATCHUP_PERIODS 10
364 /** The agressivness of the catch-up relative to how far we've lagged behind.
365 * The idea is to have increasing catch-up percentage as the lag increases. */
366 struct TMCATCHUPPERIOD
367 {
368 uint64_t u64Start; /**< When this period starts. (u64VirtualSyncOffset). */
369 uint32_t u32Percentage; /**< The catch-up percent to apply. */
370 uint32_t u32Alignment; /**< Structure alignment */
371 } aVirtualSyncCatchUpPeriods[TM_MAX_CATCHUP_PERIODS];
372
373 /** The UTC offset in ns.
374 * This is *NOT* for converting UTC to local time. It is for converting real
375 * world UTC time to VM UTC time. This feature is indented for doing date
376 * testing of software and similar.
377 * @todo Implement warpdrive on UTC. */
378 int64_t offUTC;
379
380 /** Timer queues for the different clock types - R3 Ptr */
381 R3PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR3;
382 /** Timer queues for the different clock types - R0 Ptr */
383 R0PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR0;
384 /** Timer queues for the different clock types - GC Ptr */
385 GCPTRTYPE(PTMTIMERQUEUE) paTimerQueuesGC;
386
387 /** Pointer to our GC mapping of the GIP. */
388 GCPTRTYPE(void *) pvGIPGC;
389 /** Pointer to our R3 mapping of the GIP. */
390 R3PTRTYPE(void *) pvGIPR3;
391
392 /** Pointer to a singly linked list of free timers.
393 * This chain is using the TMTIMER::pBigNext members.
394 * Only accessible from the emulation thread. */
395 PTMTIMERR3 pFree;
396
397 /** Pointer to a doubly linked list of created timers.
398 * This chain is using the TMTIMER::pBigNext and TMTIMER::pBigPrev members.
399 * Only accessible from the emulation thread. */
400 PTMTIMERR3 pCreated;
401
402 /** The schedulation timer timer handle (runtime timer).
403 * This timer will do freqent check on pending queue schedulations and
404 * raise VM_FF_TIMER to pull EMTs attention to them.
405 */
406 HCPTRTYPE(PRTTIMER) pTimer;
407 /** Interval in milliseconds of the pTimer timer. */
408 uint32_t u32TimerMillies;
409
410 /** Alignment padding to ensure that the statistics are 64-bit aligned when using GCC. */
411 uint32_t u32Padding1;
412
413 /** TMR3TimerQueuesDo
414 * @{ */
415 STAMPROFILE StatDoQueues;
416 STAMPROFILEADV StatDoQueuesSchedule;
417 STAMPROFILEADV StatDoQueuesRun;
418 /** @} */
419 /** tmSchedule
420 * @{ */
421 STAMPROFILE StatScheduleOneGC;
422 STAMPROFILE StatScheduleOneR0;
423 STAMPROFILE StatScheduleOneR3;
424 STAMCOUNTER StatScheduleSetFF;
425 STAMCOUNTER StatPostponedR3;
426 STAMCOUNTER StatPostponedR0;
427 STAMCOUNTER StatPostponedGC;
428 /** @} */
429 /** Read the time
430 * @{ */
431 STAMCOUNTER StatVirtualGet;
432 STAMCOUNTER StatVirtualGetSetFF;
433 STAMCOUNTER StatVirtualGetSync;
434 STAMCOUNTER StatVirtualGetSyncSetFF;
435 STAMCOUNTER StatVirtualPause;
436 STAMCOUNTER StatVirtualResume;
437 /* @} */
438 /** TMTimerPoll
439 * @{ */
440 STAMCOUNTER StatPollAlreadySet;
441 STAMCOUNTER StatPollVirtual;
442 STAMCOUNTER StatPollVirtualSync;
443 STAMCOUNTER StatPollMiss;
444 /** @} */
445 /** TMTimerSet
446 * @{ */
447 STAMPROFILE StatTimerSetGC;
448 STAMPROFILE StatTimerSetR0;
449 STAMPROFILE StatTimerSetR3;
450 /** @} */
451 /** TMTimerStop
452 * @{ */
453 STAMPROFILE StatTimerStopGC;
454 STAMPROFILE StatTimerStopR0;
455 STAMPROFILE StatTimerStopR3;
456 /** @} */
457 /** VirtualSync - Running and Catching Up
458 * @{ */
459 STAMCOUNTER StatVirtualSyncRun;
460 STAMCOUNTER StatVirtualSyncRunRestart;
461 STAMPROFILE StatVirtualSyncRunSlack;
462 STAMCOUNTER StatVirtualSyncRunStop;
463 STAMCOUNTER StatVirtualSyncRunStoppedAlready;
464 STAMCOUNTER StatVirtualSyncGiveUp;
465 STAMCOUNTER StatVirtualSyncGiveUpBeforeStarting;
466 STAMPROFILEADV StatVirtualSyncCatchup;
467 STAMCOUNTER aStatVirtualSyncCatchupInitial[TM_MAX_CATCHUP_PERIODS];
468 STAMCOUNTER aStatVirtualSyncCatchupAdjust[TM_MAX_CATCHUP_PERIODS];
469 /** @} */
470 /** The timer callback. */
471 STAMCOUNTER StatTimerCallbackSetFF;
472
473} TM;
474/** Pointer to TM VM instance data. */
475typedef TM *PTM;
476
477
478const char *tmTimerState(TMTIMERSTATE enmState);
479void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue);
480#ifdef VBOX_STRICT
481void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere);
482#endif
483
484/** @} */
485
486__END_DECLS
487
488#endif
489
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette