VirtualBox

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

Last change on this file since 9176 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 19.5 KB
Line 
1/* $Id: TMInternal.h 8155 2008-04-18 15:16:47Z vboxsync $ */
2/** @file
3 * TM - Internal header file.
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#ifndef ___TMInternal_h
23#define ___TMInternal_h
24
25#include <VBox/cdefs.h>
26#include <VBox/types.h>
27#include <iprt/time.h>
28#include <iprt/timer.h>
29#include <VBox/stam.h>
30
31__BEGIN_DECLS
32
33
34/** @defgroup grp_tm_int Internal
35 * @ingroup grp_tm
36 * @internal
37 * @{
38 */
39
40/** Frequency of the real clock. */
41#define TMCLOCK_FREQ_REAL UINT32_C(1000)
42/** Frequency of the virtual clock. */
43#define TMCLOCK_FREQ_VIRTUAL UINT32_C(1000000000)
44
45
46/**
47 * Timer type.
48 */
49typedef enum TMTIMERTYPE
50{
51 /** Device timer. */
52 TMTIMERTYPE_DEV = 1,
53 /** Driver timer. */
54 TMTIMERTYPE_DRV,
55 /** Internal timer . */
56 TMTIMERTYPE_INTERNAL,
57 /** External timer. */
58 TMTIMERTYPE_EXTERNAL
59} TMTIMERTYPE;
60
61/**
62 * Timer state
63 */
64typedef enum TMTIMERSTATE
65{
66 /** Timer is stopped. */
67 TMTIMERSTATE_STOPPED = 1,
68 /** Timer is active. */
69 TMTIMERSTATE_ACTIVE,
70 /** Timer is expired, is being delivered. */
71 TMTIMERSTATE_EXPIRED,
72
73 /** Timer is stopped but still in the active list.
74 * Currently in the ScheduleTimers list. */
75 TMTIMERSTATE_PENDING_STOP,
76 /** Timer is stopped but needs unlinking from the ScheduleTimers list.
77 * Currently in the ScheduleTimers list. */
78 TMTIMERSTATE_PENDING_STOP_SCHEDULE,
79 /** Timer is being modified and will soon be pending scheduling.
80 * Currently in the ScheduleTimers list. */
81 TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE,
82 /** Timer is pending scheduling.
83 * Currently in the ScheduleTimers list. */
84 TMTIMERSTATE_PENDING_SCHEDULE,
85 /** Timer is being modified and will soon be pending rescheduling.
86 * Currently in the ScheduleTimers list and the active list. */
87 TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE,
88 /** Timer is modified and is now pending rescheduling.
89 * Currently in the ScheduleTimers list and the active list. */
90 TMTIMERSTATE_PENDING_RESCHEDULE,
91 /** Timer is destroyed but needs to be replaced from the
92 * active to the free list.
93 * Currently in the ScheduleTimers list and the active list. */
94 TMTIMERSTATE_PENDING_STOP_DESTROY,
95 /** Timer is destroyed but needs moving to the free list.
96 * Currently in the ScheduleTimers list. */
97 TMTIMERSTATE_PENDING_DESTROY,
98 /** Timer is free. */
99 TMTIMERSTATE_FREE
100} TMTIMERSTATE;
101
102
103/**
104 * Internal representation of a timer.
105 *
106 * For correct serialization (without the use of semaphores and
107 * other blocking/slow constructs) certain rules applies to updating
108 * this structure:
109 * - For thread other than EMT only u64Expire, enmState and pScheduleNext*
110 * are changeable. Everything else is out of bounds.
111 * - Updating of u64Expire timer can only happen in the TMTIMERSTATE_STOPPED
112 * and TMTIMERSTATE_PENDING_RESCHEDULING_SET_EXPIRE states.
113 * - Timers in the TMTIMERSTATE_EXPIRED state are only accessible from EMT.
114 * - Actual destruction of a timer can only be done at scheduling time.
115 */
116typedef struct TMTIMER
117{
118 /** Expire time. */
119 volatile uint64_t u64Expire;
120 /** Clock to apply to u64Expire. */
121 TMCLOCK enmClock;
122 /** Timer callback type. */
123 TMTIMERTYPE enmType;
124 /** Type specific data. */
125 union
126 {
127 /** TMTIMERTYPE_DEV. */
128 struct
129 {
130 /** Callback. */
131 R3PTRTYPE(PFNTMTIMERDEV) pfnTimer;
132 /** Device instance. */
133 R3PTRTYPE(PPDMDEVINS) pDevIns;
134 } Dev;
135
136 /** TMTIMERTYPE_DRV. */
137 struct
138 {
139 /** Callback. */
140 R3PTRTYPE(PFNTMTIMERDRV) pfnTimer;
141 /** Device instance. */
142 R3PTRTYPE(PPDMDRVINS) pDrvIns;
143 } Drv;
144
145 /** TMTIMERTYPE_INTERNAL. */
146 struct
147 {
148 /** Callback. */
149 R3PTRTYPE(PFNTMTIMERINT) pfnTimer;
150 /** User argument. */
151 R3PTRTYPE(void *) pvUser;
152 } Internal;
153
154 /** TMTIMERTYPE_EXTERNAL. */
155 struct
156 {
157 /** Callback. */
158 R3PTRTYPE(PFNTMTIMEREXT) pfnTimer;
159 /** User data. */
160 R3PTRTYPE(void *) pvUser;
161 } External;
162 } u;
163
164 /** Timer state. */
165 volatile TMTIMERSTATE enmState;
166 /** Timer relative offset to the next timer in the schedule list. */
167 int32_t offScheduleNext;
168
169 /** Timer relative offset to the next timer in the chain. */
170 int32_t offNext;
171 /** Timer relative offset to the previous timer in the chain. */
172 int32_t offPrev;
173
174 /** Pointer to the next timer in the list of created or free timers. (TM::pTimers or TM::pFree) */
175 PTMTIMERR3 pBigNext;
176 /** Pointer to the previous timer in the list of all created timers. (TM::pTimers) */
177 PTMTIMERR3 pBigPrev;
178 /** Pointer to the timer description. */
179 R3PTRTYPE(const char *) pszDesc;
180 /** Pointer to the VM the timer belongs to - R3 Ptr. */
181 PVMR3 pVMR3;
182 /** Pointer to the VM the timer belongs to - R0 Ptr. */
183 PVMR0 pVMR0;
184 /** Pointer to the VM the timer belongs to - GC Ptr. */
185 PVMGC pVMGC;
186#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
187 RTGCPTR padding0; /**< pad structure to multiple of 8 bytes. */
188#endif
189} TMTIMER;
190
191
192/**
193 * Updates a timer state in the correct atomic manner.
194 */
195#if 1
196# define TM_SET_STATE(pTimer, state) \
197 ASMAtomicXchgSize(&(pTimer)->enmState, state)
198#else
199# define TM_SET_STATE(pTimer, state) \
200 do { Log(("%s: %p: %d -> %d\n", __FUNCTION__, (pTimer), (pTimer)->enmState, state)); \
201 ASMAtomicXchgSize(&(pTimer)->enmState, state);\
202 } while (0)
203#endif
204
205/**
206 * Tries to updates a timer state in the correct atomic manner.
207 */
208#if 1
209# define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
210 ASMAtomicCmpXchgSize(&(pTimer)->enmState, StateNew, StateOld, fRc)
211#else
212# define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
213 do { ASMAtomicCmpXchgSize(&(pTimer)->enmState, StateNew, StateOld, fRc); \
214 Log(("%s: %p: %d -> %d %RTbool\n", __FUNCTION__, (pTimer), StateOld, StateNew, fRc)); \
215 } while (0)
216#endif
217
218/** Get the previous timer. */
219#define TMTIMER_GET_PREV(pTimer) ((PTMTIMER)((pTimer)->offPrev ? (intptr_t)(pTimer) + (pTimer)->offPrev : 0))
220/** Get the next timer. */
221#define TMTIMER_GET_NEXT(pTimer) ((PTMTIMER)((pTimer)->offNext ? (intptr_t)(pTimer) + (pTimer)->offNext : 0))
222/** Set the previous timer link. */
223#define TMTIMER_SET_PREV(pTimer, pPrev) ((pTimer)->offPrev = (pPrev) ? (intptr_t)(pPrev) - (intptr_t)(pTimer) : 0)
224/** Set the next timer link. */
225#define TMTIMER_SET_NEXT(pTimer, pNext) ((pTimer)->offNext = (pNext) ? (intptr_t)(pNext) - (intptr_t)(pTimer) : 0)
226
227
228/**
229 * A timer queue.
230 *
231 * This is allocated on the hyper heap.
232 */
233typedef struct TMTIMERQUEUE
234{
235 /** The cached expire time for this queue.
236 * Updated by EMT when scheduling the queue or modifying the head timer.
237 * Assigned UINT64_MAX when there is no head timer. */
238 uint64_t u64Expire;
239 /** Doubly linked list of active timers.
240 *
241 * When no scheduling is pending, this list is will be ordered by expire time (ascending).
242 * Access is serialized by only letting the emulation thread (EMT) do changes.
243 *
244 * The offset is relative to the queue structure.
245 */
246 int32_t offActive;
247 /** List of timers pending scheduling of some kind.
248 *
249 * Timer stats allowed in the list are TMTIMERSTATE_PENDING_STOPPING,
250 * TMTIMERSTATE_PENDING_DESTRUCTION, TMTIMERSTATE_PENDING_STOPPING_DESTRUCTION,
251 * TMTIMERSTATE_PENDING_RESCHEDULING and TMTIMERSTATE_PENDING_SCHEDULE.
252 *
253 * The offset is relative to the queue structure.
254 */
255 int32_t volatile offSchedule;
256 /** The clock for this queue. */
257 TMCLOCK enmClock;
258 /** Pad the structure up to 32 bytes. */
259 uint32_t au32Padding[3];
260} TMTIMERQUEUE;
261
262/** Pointer to a timer queue. */
263typedef TMTIMERQUEUE *PTMTIMERQUEUE;
264
265/** Get the head of the active timer list. */
266#define TMTIMER_GET_HEAD(pQueue) ((PTMTIMER)((pQueue)->offActive ? (intptr_t)(pQueue) + (pQueue)->offActive : 0))
267/** Set the head of the active timer list. */
268#define TMTIMER_SET_HEAD(pQueue, pHead) ((pQueue)->offActive = pHead ? (intptr_t)pHead - (intptr_t)(pQueue) : 0)
269
270
271/**
272 * Converts a TM pointer into a VM pointer.
273 * @returns Pointer to the VM structure the TM is part of.
274 * @param pTM Pointer to TM instance data.
275 */
276#define TM2VM(pTM) ( (PVM)((char*)pTM - pTM->offVM) )
277
278
279/**
280 * TM VM Instance data.
281 * Changes to this must checked against the padding of the cfgm union in VM!
282 */
283typedef struct TM
284{
285 /** Offset to the VM structure.
286 * See TM2VM(). */
287 RTUINT offVM;
288
289 /** Flag indicating that the host TSC is suitable for use in AMD-V and VT-x mode.
290 * Config variable: MaybeUseOffsettedHostTSC (boolean) */
291 bool fMaybeUseOffsettedHostTSC;
292 /** CPU timestamp ticking enabled indicator (bool). (RDTSC) */
293 bool fTSCTicking;
294 /** Set if we fully virtualize the TSC, i.e. intercept all rdtsc instructions.
295 * Config variable: TSCVirtualized (bool) */
296 bool fTSCVirtualized;
297 /** Set if we use the real TSC as time source or if we use the virtual clock.
298 * If fTSCVirtualized is set we maintain a offset to the TSC and pausing/resuming the
299 * ticking. fTSCVirtualized = false implies fTSCUseRealTSC = true.
300 * Config variable: TSCUseRealTSC (bool) */
301 bool fTSCUseRealTSC;
302 /** The offset between the host TSC and the Guest TSC.
303 * Only valid if fTicking is set and and fTSCUseRealTSC is clear. */
304 uint64_t u64TSCOffset;
305 /** The guest TSC when fTicking is cleared. */
306 uint64_t u64TSC;
307 /** The number of CPU clock ticks per second (TMCLOCK_TSC).
308 * Config variable: TSCTicksPerSecond (64-bit unsigned int)
309 * The config variable implies fTSCVirtualized = true and fTSCUseRealTSC = false. */
310 uint64_t cTSCTicksPerSecond;
311
312 /** Virtual time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL) */
313 bool fVirtualTicking;
314 /** Virtual time is not running at 100%. */
315 bool fVirtualWarpDrive;
316 /** Virtual timer synchronous time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL_SYNC) */
317 bool volatile fVirtualSyncTicking;
318 /** Virtual timer synchronous time catch-up active. */
319 bool volatile fVirtualSyncCatchUp;
320 /** WarpDrive percentage.
321 * 100% is normal (fVirtualSyncNormal == true). When other than 100% we apply
322 * this percentage to the raw time source for the period it's been valid in,
323 * i.e. since u64VirtualWarpDriveStart. */
324 uint32_t u32VirtualWarpDrivePercentage;
325
326 /** The offset of the virtual clock relative to it's timesource.
327 * Only valid if fVirtualTicking is set. */
328 uint64_t u64VirtualOffset;
329 /** The guest virtual time when fVirtualTicking is cleared. */
330 uint64_t u64Virtual;
331 /** When the Warp drive was started or last adjusted.
332 * Only valid when fVirtualWarpDrive is set. */
333 uint64_t u64VirtualWarpDriveStart;
334 /** The previously returned nano TS.
335 * This handles TSC drift on SMP systems and expired interval.
336 * This is a valid range u64NanoTS to u64NanoTS + 1000000000 (ie. 1sec). */
337 uint64_t volatile u64VirtualRawPrev;
338 /** The ring-3 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
339 RTTIMENANOTSDATAR3 VirtualGetRawDataR3;
340 /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
341 RTTIMENANOTSDATAR0 VirtualGetRawDataR0;
342 /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
343 RTTIMENANOTSDATAGC VirtualGetRawDataGC;
344 /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
345 R3PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR3;
346 /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
347 R0PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR0;
348 /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
349 GCPTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawGC;
350 /** Alignment. */
351 RTGCPTR AlignmentGCPtr;
352 /** The guest virtual timer synchronous time when fVirtualSyncTicking is cleared. */
353 uint64_t volatile u64VirtualSync;
354 /** The offset of the timer synchronous virtual clock (TMCLOCK_VIRTUAL_SYNC) relative
355 * to the virtual clock (TMCLOCK_VIRTUAL).
356 * (This is accessed by the timer thread and must be updated atomically.) */
357 uint64_t volatile offVirtualSync;
358 /** The offset into offVirtualSync that's been irrevocably given up by failed catch-up attempts.
359 * Thus the current lag is offVirtualSync - offVirtualSyncGivenUp. */
360 uint64_t offVirtualSyncGivenUp;
361 /** The TMCLOCK_VIRTUAL at the previous TMVirtualGetSync call when catch-up is active. */
362 uint64_t volatile u64VirtualSyncCatchUpPrev;
363 /** The current catch-up percentage. */
364 uint32_t volatile u32VirtualSyncCatchUpPercentage;
365 /** How much slack when processing timers. */
366 uint32_t u32VirtualSyncScheduleSlack;
367 /** When to stop catch-up. */
368 uint64_t u64VirtualSyncCatchUpStopThreshold;
369 /** When to give up catch-up. */
370 uint64_t u64VirtualSyncCatchUpGiveUpThreshold;
371/** @def TM_MAX_CATCHUP_PERIODS
372 * The number of catchup rates. */
373#define TM_MAX_CATCHUP_PERIODS 10
374 /** The agressivness of the catch-up relative to how far we've lagged behind.
375 * The idea is to have increasing catch-up percentage as the lag increases. */
376 struct TMCATCHUPPERIOD
377 {
378 uint64_t u64Start; /**< When this period starts. (u64VirtualSyncOffset). */
379 uint32_t u32Percentage; /**< The catch-up percent to apply. */
380 uint32_t u32Alignment; /**< Structure alignment */
381 } aVirtualSyncCatchUpPeriods[TM_MAX_CATCHUP_PERIODS];
382
383 /** The UTC offset in ns.
384 * This is *NOT* for converting UTC to local time. It is for converting real
385 * world UTC time to VM UTC time. This feature is indented for doing date
386 * testing of software and similar.
387 * @todo Implement warpdrive on UTC. */
388 int64_t offUTC;
389
390 /** Timer queues for the different clock types - R3 Ptr */
391 R3PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR3;
392 /** Timer queues for the different clock types - R0 Ptr */
393 R0PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR0;
394 /** Timer queues for the different clock types - GC Ptr */
395 GCPTRTYPE(PTMTIMERQUEUE) paTimerQueuesGC;
396
397 /** Pointer to our GC mapping of the GIP. */
398 GCPTRTYPE(void *) pvGIPGC;
399 /** Pointer to our R3 mapping of the GIP. */
400 R3PTRTYPE(void *) pvGIPR3;
401
402 /** Pointer to a singly linked list of free timers.
403 * This chain is using the TMTIMER::pBigNext members.
404 * Only accessible from the emulation thread. */
405 PTMTIMERR3 pFree;
406
407 /** Pointer to a doubly linked list of created timers.
408 * This chain is using the TMTIMER::pBigNext and TMTIMER::pBigPrev members.
409 * Only accessible from the emulation thread. */
410 PTMTIMERR3 pCreated;
411
412 /** The schedulation timer timer handle (runtime timer).
413 * This timer will do freqent check on pending queue schedulations and
414 * raise VM_FF_TIMER to pull EMTs attention to them.
415 */
416 R3PTRTYPE(PRTTIMER) pTimer;
417 /** Interval in milliseconds of the pTimer timer. */
418 uint32_t u32TimerMillies;
419
420 /** Alignment padding to ensure that the statistics are 64-bit aligned when using GCC. */
421 uint32_t u32Padding1;
422
423 /** TMR3TimerQueuesDo
424 * @{ */
425 STAMPROFILE StatDoQueues;
426 STAMPROFILEADV StatDoQueuesSchedule;
427 STAMPROFILEADV StatDoQueuesRun;
428 /** @} */
429 /** tmSchedule
430 * @{ */
431 STAMPROFILE StatScheduleOneGC;
432 STAMPROFILE StatScheduleOneR0;
433 STAMPROFILE StatScheduleOneR3;
434 STAMCOUNTER StatScheduleSetFF;
435 STAMCOUNTER StatPostponedR3;
436 STAMCOUNTER StatPostponedR0;
437 STAMCOUNTER StatPostponedGC;
438 /** @} */
439 /** Read the time
440 * @{ */
441 STAMCOUNTER StatVirtualGet;
442 STAMCOUNTER StatVirtualGetSetFF;
443 STAMCOUNTER StatVirtualGetSync;
444 STAMCOUNTER StatVirtualGetSyncSetFF;
445 STAMCOUNTER StatVirtualPause;
446 STAMCOUNTER StatVirtualResume;
447 /* @} */
448 /** TMTimerPoll
449 * @{ */
450 STAMCOUNTER StatPollAlreadySet;
451 STAMCOUNTER StatPollVirtual;
452 STAMCOUNTER StatPollVirtualSync;
453 STAMCOUNTER StatPollMiss;
454 /** @} */
455 /** TMTimerSet
456 * @{ */
457 STAMPROFILE StatTimerSetGC;
458 STAMPROFILE StatTimerSetR0;
459 STAMPROFILE StatTimerSetR3;
460 /** @} */
461 /** TMTimerStop
462 * @{ */
463 STAMPROFILE StatTimerStopGC;
464 STAMPROFILE StatTimerStopR0;
465 STAMPROFILE StatTimerStopR3;
466 /** @} */
467 /** VirtualSync - Running and Catching Up
468 * @{ */
469 STAMCOUNTER StatVirtualSyncRun;
470 STAMCOUNTER StatVirtualSyncRunRestart;
471 STAMPROFILE StatVirtualSyncRunSlack;
472 STAMCOUNTER StatVirtualSyncRunStop;
473 STAMCOUNTER StatVirtualSyncRunStoppedAlready;
474 STAMCOUNTER StatVirtualSyncGiveUp;
475 STAMCOUNTER StatVirtualSyncGiveUpBeforeStarting;
476 STAMPROFILEADV StatVirtualSyncCatchup;
477 STAMCOUNTER aStatVirtualSyncCatchupInitial[TM_MAX_CATCHUP_PERIODS];
478 STAMCOUNTER aStatVirtualSyncCatchupAdjust[TM_MAX_CATCHUP_PERIODS];
479 /** @} */
480 /** The timer callback. */
481 STAMCOUNTER StatTimerCallbackSetFF;
482
483} TM;
484/** Pointer to TM VM instance data. */
485typedef TM *PTM;
486
487
488const char *tmTimerState(TMTIMERSTATE enmState);
489void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue);
490#ifdef VBOX_STRICT
491void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere);
492#endif
493
494DECLEXPORT(void) tmVirtualNanoTSBad(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
495DECLEXPORT(uint64_t) tmVirtualNanoTSRediscover(PRTTIMENANOTSDATA pData);
496
497/** @} */
498
499__END_DECLS
500
501#endif
502
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