1 | /* $Id: TMInternal.h 20087 2009-05-27 14:31:18Z 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 <iprt/assert.h>
|
---|
30 | #include <VBox/stam.h>
|
---|
31 | #include <VBox/pdmcritsect.h>
|
---|
32 |
|
---|
33 | __BEGIN_DECLS
|
---|
34 |
|
---|
35 |
|
---|
36 | /** @defgroup grp_tm_int Internal
|
---|
37 | * @ingroup grp_tm
|
---|
38 | * @internal
|
---|
39 | * @{
|
---|
40 | */
|
---|
41 |
|
---|
42 | /** Frequency of the real clock. */
|
---|
43 | #define TMCLOCK_FREQ_REAL UINT32_C(1000)
|
---|
44 | /** Frequency of the virtual clock. */
|
---|
45 | #define TMCLOCK_FREQ_VIRTUAL UINT32_C(1000000000)
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Timer type.
|
---|
50 | */
|
---|
51 | typedef enum TMTIMERTYPE
|
---|
52 | {
|
---|
53 | /** Device timer. */
|
---|
54 | TMTIMERTYPE_DEV = 1,
|
---|
55 | /** Driver timer. */
|
---|
56 | TMTIMERTYPE_DRV,
|
---|
57 | /** Internal timer . */
|
---|
58 | TMTIMERTYPE_INTERNAL,
|
---|
59 | /** External timer. */
|
---|
60 | TMTIMERTYPE_EXTERNAL
|
---|
61 | } TMTIMERTYPE;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Timer state
|
---|
65 | */
|
---|
66 | typedef enum TMTIMERSTATE
|
---|
67 | {
|
---|
68 | /** Timer is stopped. */
|
---|
69 | TMTIMERSTATE_STOPPED = 1,
|
---|
70 | /** Timer is active. */
|
---|
71 | TMTIMERSTATE_ACTIVE,
|
---|
72 | /** Timer is expired, getting expire and unlinking. */
|
---|
73 | TMTIMERSTATE_EXPIRED_GET_UNLINK,
|
---|
74 | /** Timer is expired and is being delivered. */
|
---|
75 | TMTIMERSTATE_EXPIRED_DELIVER,
|
---|
76 |
|
---|
77 | /** Timer is stopped but still in the active list.
|
---|
78 | * Currently in the ScheduleTimers list. */
|
---|
79 | TMTIMERSTATE_PENDING_STOP,
|
---|
80 | /** Timer is stopped but needs unlinking from the ScheduleTimers list.
|
---|
81 | * Currently in the ScheduleTimers list. */
|
---|
82 | TMTIMERSTATE_PENDING_STOP_SCHEDULE,
|
---|
83 | /** Timer is being modified and will soon be pending scheduling.
|
---|
84 | * Currently in the ScheduleTimers list. */
|
---|
85 | TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE,
|
---|
86 | /** Timer is pending scheduling.
|
---|
87 | * Currently in the ScheduleTimers list. */
|
---|
88 | TMTIMERSTATE_PENDING_SCHEDULE,
|
---|
89 | /** Timer is being modified and will soon be pending rescheduling.
|
---|
90 | * Currently in the ScheduleTimers list and the active list. */
|
---|
91 | TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE,
|
---|
92 | /** Timer is modified and is now pending rescheduling.
|
---|
93 | * Currently in the ScheduleTimers list and the active list. */
|
---|
94 | TMTIMERSTATE_PENDING_RESCHEDULE,
|
---|
95 | /** Timer is being destroyed. */
|
---|
96 | TMTIMERSTATE_DESTROY,
|
---|
97 | /** Timer is free. */
|
---|
98 | TMTIMERSTATE_FREE
|
---|
99 | } TMTIMERSTATE;
|
---|
100 |
|
---|
101 | /** Predicate that returns true if the give state is pending scheduling or
|
---|
102 | * rescheduling of any kind. Will reference the argument more than once! */
|
---|
103 | #define TMTIMERSTATE_IS_PENDING_SCHEDULING(enmState) \
|
---|
104 | ( (enmState) <= TMTIMERSTATE_PENDING_RESCHEDULE \
|
---|
105 | && (enmState) >= TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE)
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Internal representation of a timer.
|
---|
110 | *
|
---|
111 | * For correct serialization (without the use of semaphores and
|
---|
112 | * other blocking/slow constructs) certain rules applies to updating
|
---|
113 | * this structure:
|
---|
114 | * - For thread other than EMT only u64Expire, enmState and pScheduleNext*
|
---|
115 | * are changeable. Everything else is out of bounds.
|
---|
116 | * - Updating of u64Expire timer can only happen in the TMTIMERSTATE_STOPPED
|
---|
117 | * and TMTIMERSTATE_PENDING_RESCHEDULING_SET_EXPIRE states.
|
---|
118 | * - Timers in the TMTIMERSTATE_EXPIRED state are only accessible from EMT.
|
---|
119 | * - Actual destruction of a timer can only be done at scheduling time.
|
---|
120 | */
|
---|
121 | typedef struct TMTIMER
|
---|
122 | {
|
---|
123 | /** Expire time. */
|
---|
124 | volatile uint64_t u64Expire;
|
---|
125 | /** Clock to apply to u64Expire. */
|
---|
126 | TMCLOCK enmClock;
|
---|
127 | /** Timer callback type. */
|
---|
128 | TMTIMERTYPE enmType;
|
---|
129 | /** Type specific data. */
|
---|
130 | union
|
---|
131 | {
|
---|
132 | /** TMTIMERTYPE_DEV. */
|
---|
133 | struct
|
---|
134 | {
|
---|
135 | /** Callback. */
|
---|
136 | R3PTRTYPE(PFNTMTIMERDEV) pfnTimer;
|
---|
137 | /** Device instance. */
|
---|
138 | PPDMDEVINSR3 pDevIns;
|
---|
139 | } Dev;
|
---|
140 |
|
---|
141 | /** TMTIMERTYPE_DRV. */
|
---|
142 | struct
|
---|
143 | {
|
---|
144 | /** Callback. */
|
---|
145 | R3PTRTYPE(PFNTMTIMERDRV) pfnTimer;
|
---|
146 | /** Device instance. */
|
---|
147 | R3PTRTYPE(PPDMDRVINS) pDrvIns;
|
---|
148 | } Drv;
|
---|
149 |
|
---|
150 | /** TMTIMERTYPE_INTERNAL. */
|
---|
151 | struct
|
---|
152 | {
|
---|
153 | /** Callback. */
|
---|
154 | R3PTRTYPE(PFNTMTIMERINT) pfnTimer;
|
---|
155 | } Internal;
|
---|
156 |
|
---|
157 | /** TMTIMERTYPE_EXTERNAL. */
|
---|
158 | struct
|
---|
159 | {
|
---|
160 | /** Callback. */
|
---|
161 | R3PTRTYPE(PFNTMTIMEREXT) pfnTimer;
|
---|
162 | } External;
|
---|
163 | } u;
|
---|
164 |
|
---|
165 | /** Timer state. */
|
---|
166 | volatile TMTIMERSTATE enmState;
|
---|
167 | /** Timer relative offset to the next timer in the schedule list. */
|
---|
168 | int32_t volatile offScheduleNext;
|
---|
169 |
|
---|
170 | /** Timer relative offset to the next timer in the chain. */
|
---|
171 | int32_t offNext;
|
---|
172 | /** Timer relative offset to the previous timer in the chain. */
|
---|
173 | int32_t offPrev;
|
---|
174 |
|
---|
175 | /** User argument. */
|
---|
176 | RTR3PTR pvUser;
|
---|
177 | /** The critical section associated with the lock. */
|
---|
178 | R3PTRTYPE(PPDMCRITSECT) pCritSect;
|
---|
179 |
|
---|
180 | /** Pointer to the next timer in the list of created or free timers. (TM::pTimers or TM::pFree) */
|
---|
181 | PTMTIMERR3 pBigNext;
|
---|
182 | /** Pointer to the previous timer in the list of all created timers. (TM::pTimers) */
|
---|
183 | PTMTIMERR3 pBigPrev;
|
---|
184 | /** Pointer to the timer description. */
|
---|
185 | R3PTRTYPE(const char *) pszDesc;
|
---|
186 | /** Pointer to the VM the timer belongs to - R3 Ptr. */
|
---|
187 | PVMR3 pVMR3;
|
---|
188 | /** Pointer to the VM the timer belongs to - R0 Ptr. */
|
---|
189 | PVMR0 pVMR0;
|
---|
190 | /** Pointer to the VM the timer belongs to - RC Ptr. */
|
---|
191 | PVMRC pVMRC;
|
---|
192 | #if HC_ARCH_BITS == 64
|
---|
193 | RTRCPTR padding0; /**< pad structure to multiple of 8 bytes. */
|
---|
194 | #endif
|
---|
195 | } TMTIMER;
|
---|
196 | AssertCompileMemberSize(TMTIMER, enmState, sizeof(uint32_t));
|
---|
197 |
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Updates a timer state in the correct atomic manner.
|
---|
201 | */
|
---|
202 | #if 1
|
---|
203 | # define TM_SET_STATE(pTimer, state) \
|
---|
204 | ASMAtomicWriteU32((uint32_t volatile *)&(pTimer)->enmState, state)
|
---|
205 | #else
|
---|
206 | # define TM_SET_STATE(pTimer, state) \
|
---|
207 | do { \
|
---|
208 | uint32_t uOld1 = (pTimer)->enmState; \
|
---|
209 | Log(("%s: %p: %d -> %d\n", __FUNCTION__, (pTimer), (pTimer)->enmState, state)); \
|
---|
210 | uint32_t uOld2 = ASMAtomicXchgU32((uint32_t volatile *)&(pTimer)->enmState, state); \
|
---|
211 | Assert(uOld1 == uOld2); \
|
---|
212 | } while (0)
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Tries to updates a timer state in the correct atomic manner.
|
---|
217 | */
|
---|
218 | #if 1
|
---|
219 | # define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
|
---|
220 | (fRc) = ASMAtomicCmpXchgU32((uint32_t volatile *)&(pTimer)->enmState, StateNew, StateOld)
|
---|
221 | #else
|
---|
222 | # define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
|
---|
223 | do { (fRc) = ASMAtomicCmpXchgU32((uint32_t volatile *)&(pTimer)->enmState, StateNew, StateOld); \
|
---|
224 | Log(("%s: %p: %d -> %d %RTbool\n", __FUNCTION__, (pTimer), StateOld, StateNew, fRc)); \
|
---|
225 | } while (0)
|
---|
226 | #endif
|
---|
227 |
|
---|
228 | /** Get the previous timer. */
|
---|
229 | #define TMTIMER_GET_PREV(pTimer) ((PTMTIMER)((pTimer)->offPrev ? (intptr_t)(pTimer) + (pTimer)->offPrev : 0))
|
---|
230 | /** Get the next timer. */
|
---|
231 | #define TMTIMER_GET_NEXT(pTimer) ((PTMTIMER)((pTimer)->offNext ? (intptr_t)(pTimer) + (pTimer)->offNext : 0))
|
---|
232 | /** Set the previous timer link. */
|
---|
233 | #define TMTIMER_SET_PREV(pTimer, pPrev) ((pTimer)->offPrev = (pPrev) ? (intptr_t)(pPrev) - (intptr_t)(pTimer) : 0)
|
---|
234 | /** Set the next timer link. */
|
---|
235 | #define TMTIMER_SET_NEXT(pTimer, pNext) ((pTimer)->offNext = (pNext) ? (intptr_t)(pNext) - (intptr_t)(pTimer) : 0)
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * A timer queue.
|
---|
240 | *
|
---|
241 | * This is allocated on the hyper heap.
|
---|
242 | */
|
---|
243 | typedef struct TMTIMERQUEUE
|
---|
244 | {
|
---|
245 | /** The cached expire time for this queue.
|
---|
246 | * Updated by EMT when scheduling the queue or modifying the head timer.
|
---|
247 | * Assigned UINT64_MAX when there is no head timer. */
|
---|
248 | uint64_t u64Expire;
|
---|
249 | /** Doubly linked list of active timers.
|
---|
250 | *
|
---|
251 | * When no scheduling is pending, this list is will be ordered by expire time (ascending).
|
---|
252 | * Access is serialized by only letting the emulation thread (EMT) do changes.
|
---|
253 | *
|
---|
254 | * The offset is relative to the queue structure.
|
---|
255 | */
|
---|
256 | int32_t offActive;
|
---|
257 | /** List of timers pending scheduling of some kind.
|
---|
258 | *
|
---|
259 | * Timer stats allowed in the list are TMTIMERSTATE_PENDING_STOPPING,
|
---|
260 | * TMTIMERSTATE_PENDING_DESTRUCTION, TMTIMERSTATE_PENDING_STOPPING_DESTRUCTION,
|
---|
261 | * TMTIMERSTATE_PENDING_RESCHEDULING and TMTIMERSTATE_PENDING_SCHEDULE.
|
---|
262 | *
|
---|
263 | * The offset is relative to the queue structure.
|
---|
264 | */
|
---|
265 | int32_t volatile offSchedule;
|
---|
266 | /** The clock for this queue. */
|
---|
267 | TMCLOCK enmClock;
|
---|
268 | /** Pad the structure up to 32 bytes. */
|
---|
269 | uint32_t au32Padding[3];
|
---|
270 | } TMTIMERQUEUE;
|
---|
271 |
|
---|
272 | /** Pointer to a timer queue. */
|
---|
273 | typedef TMTIMERQUEUE *PTMTIMERQUEUE;
|
---|
274 |
|
---|
275 | /** Get the head of the active timer list. */
|
---|
276 | #define TMTIMER_GET_HEAD(pQueue) ((PTMTIMER)((pQueue)->offActive ? (intptr_t)(pQueue) + (pQueue)->offActive : 0))
|
---|
277 | /** Set the head of the active timer list. */
|
---|
278 | #define TMTIMER_SET_HEAD(pQueue, pHead) ((pQueue)->offActive = pHead ? (intptr_t)pHead - (intptr_t)(pQueue) : 0)
|
---|
279 |
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Converts a TM pointer into a VM pointer.
|
---|
283 | * @returns Pointer to the VM structure the TM is part of.
|
---|
284 | * @param pTM Pointer to TM instance data.
|
---|
285 | */
|
---|
286 | #define TM2VM(pTM) ( (PVM)((char*)pTM - pTM->offVM) )
|
---|
287 |
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * TM VM Instance data.
|
---|
291 | * Changes to this must checked against the padding of the cfgm union in VM!
|
---|
292 | */
|
---|
293 | typedef struct TM
|
---|
294 | {
|
---|
295 | /** Offset to the VM structure.
|
---|
296 | * See TM2VM(). */
|
---|
297 | RTUINT offVM;
|
---|
298 |
|
---|
299 | /** Set if we fully virtualize the TSC, i.e. intercept all rdtsc instructions.
|
---|
300 | * Config variable: TSCVirtualized (bool) */
|
---|
301 | bool fTSCVirtualized;
|
---|
302 | /** Set if we use the real TSC as time source or if we use the virtual clock.
|
---|
303 | * If fTSCVirtualized is set we maintain a offset to the TSC and pausing/resuming the
|
---|
304 | * ticking. fTSCVirtualized = false implies fTSCUseRealTSC = true.
|
---|
305 | * Config variable: TSCUseRealTSC (bool) */
|
---|
306 | bool fTSCUseRealTSC;
|
---|
307 | /** Flag indicating that the host TSC is suitable for use in AMD-V and VT-x mode.
|
---|
308 | * Config variable: MaybeUseOffsettedHostTSC (boolean) */
|
---|
309 | bool fMaybeUseOffsettedHostTSC;
|
---|
310 | /** Whether the TSC is tied to the execution of code.
|
---|
311 | * Config variable: TSCTiedToExecution (bool) */
|
---|
312 | bool fTSCTiedToExecution;
|
---|
313 | /** Modifier for fTSCTiedToExecution which pauses the TSC while halting if true.
|
---|
314 | * Config variable: TSCNotTiedToHalt (bool) */
|
---|
315 | bool fTSCNotTiedToHalt;
|
---|
316 | bool afAlignment0[2]; /**< alignment padding */
|
---|
317 | /** The ID of the virtual CPU that normally runs the timers. */
|
---|
318 | VMCPUID idTimerCpu;
|
---|
319 | /** The number of CPU clock ticks per second (TMCLOCK_TSC).
|
---|
320 | * Config variable: TSCTicksPerSecond (64-bit unsigned int)
|
---|
321 | * The config variable implies fTSCVirtualized = true and fTSCUseRealTSC = false. */
|
---|
322 | uint64_t cTSCTicksPerSecond;
|
---|
323 |
|
---|
324 | /** Virtual time ticking enabled indicator (counter for each VCPU). (TMCLOCK_VIRTUAL) */
|
---|
325 | uint32_t volatile cVirtualTicking;
|
---|
326 | /** Virtual time is not running at 100%. */
|
---|
327 | bool fVirtualWarpDrive;
|
---|
328 | /** Virtual timer synchronous time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL_SYNC) */
|
---|
329 | bool volatile fVirtualSyncTicking;
|
---|
330 | /** Virtual timer synchronous time catch-up active. */
|
---|
331 | bool volatile fVirtualSyncCatchUp;
|
---|
332 | bool afAlignment[5]; /**< alignment padding */
|
---|
333 | /** WarpDrive percentage.
|
---|
334 | * 100% is normal (fVirtualSyncNormal == true). When other than 100% we apply
|
---|
335 | * this percentage to the raw time source for the period it's been valid in,
|
---|
336 | * i.e. since u64VirtualWarpDriveStart. */
|
---|
337 | uint32_t u32VirtualWarpDrivePercentage;
|
---|
338 |
|
---|
339 | /** The offset of the virtual clock relative to it's timesource.
|
---|
340 | * Only valid if fVirtualTicking is set. */
|
---|
341 | uint64_t u64VirtualOffset;
|
---|
342 | /** The guest virtual time when fVirtualTicking is cleared. */
|
---|
343 | uint64_t u64Virtual;
|
---|
344 | /** When the Warp drive was started or last adjusted.
|
---|
345 | * Only valid when fVirtualWarpDrive is set. */
|
---|
346 | uint64_t u64VirtualWarpDriveStart;
|
---|
347 | /** The previously returned nano TS.
|
---|
348 | * This handles TSC drift on SMP systems and expired interval.
|
---|
349 | * This is a valid range u64NanoTS to u64NanoTS + 1000000000 (ie. 1sec). */
|
---|
350 | uint64_t volatile u64VirtualRawPrev;
|
---|
351 | /** The ring-3 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
|
---|
352 | RTTIMENANOTSDATAR3 VirtualGetRawDataR3;
|
---|
353 | /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
|
---|
354 | RTTIMENANOTSDATAR0 VirtualGetRawDataR0;
|
---|
355 | /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
|
---|
356 | RTTIMENANOTSDATARC VirtualGetRawDataRC;
|
---|
357 | /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
|
---|
358 | R3PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR3;
|
---|
359 | /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
|
---|
360 | R0PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR0;
|
---|
361 | /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
|
---|
362 | RCPTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawRC;
|
---|
363 | /** Alignment. */
|
---|
364 | RTRCPTR AlignmentRCPtr;
|
---|
365 | /** The guest virtual timer synchronous time when fVirtualSyncTicking is cleared. */
|
---|
366 | uint64_t volatile u64VirtualSync;
|
---|
367 | /** The offset of the timer synchronous virtual clock (TMCLOCK_VIRTUAL_SYNC) relative
|
---|
368 | * to the virtual clock (TMCLOCK_VIRTUAL).
|
---|
369 | * (This is accessed by the timer thread and must be updated atomically.) */
|
---|
370 | uint64_t volatile offVirtualSync;
|
---|
371 | /** The offset into offVirtualSync that's been irrevocably given up by failed catch-up attempts.
|
---|
372 | * Thus the current lag is offVirtualSync - offVirtualSyncGivenUp. */
|
---|
373 | uint64_t offVirtualSyncGivenUp;
|
---|
374 | /** The TMCLOCK_VIRTUAL at the previous TMVirtualGetSync call when catch-up is active. */
|
---|
375 | uint64_t volatile u64VirtualSyncCatchUpPrev;
|
---|
376 | /** The current catch-up percentage. */
|
---|
377 | uint32_t volatile u32VirtualSyncCatchUpPercentage;
|
---|
378 | /** How much slack when processing timers. */
|
---|
379 | uint32_t u32VirtualSyncScheduleSlack;
|
---|
380 | /** When to stop catch-up. */
|
---|
381 | uint64_t u64VirtualSyncCatchUpStopThreshold;
|
---|
382 | /** When to give up catch-up. */
|
---|
383 | uint64_t u64VirtualSyncCatchUpGiveUpThreshold;
|
---|
384 | /** @def TM_MAX_CATCHUP_PERIODS
|
---|
385 | * The number of catchup rates. */
|
---|
386 | #define TM_MAX_CATCHUP_PERIODS 10
|
---|
387 | /** The agressivness of the catch-up relative to how far we've lagged behind.
|
---|
388 | * The idea is to have increasing catch-up percentage as the lag increases. */
|
---|
389 | struct TMCATCHUPPERIOD
|
---|
390 | {
|
---|
391 | uint64_t u64Start; /**< When this period starts. (u64VirtualSyncOffset). */
|
---|
392 | uint32_t u32Percentage; /**< The catch-up percent to apply. */
|
---|
393 | uint32_t u32Alignment; /**< Structure alignment */
|
---|
394 | } aVirtualSyncCatchUpPeriods[TM_MAX_CATCHUP_PERIODS];
|
---|
395 |
|
---|
396 | /** The UTC offset in ns.
|
---|
397 | * This is *NOT* for converting UTC to local time. It is for converting real
|
---|
398 | * world UTC time to VM UTC time. This feature is indented for doing date
|
---|
399 | * testing of software and similar.
|
---|
400 | * @todo Implement warpdrive on UTC. */
|
---|
401 | int64_t offUTC;
|
---|
402 |
|
---|
403 | /** Timer queues for the different clock types - R3 Ptr */
|
---|
404 | R3PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR3;
|
---|
405 | /** Timer queues for the different clock types - R0 Ptr */
|
---|
406 | R0PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR0;
|
---|
407 | /** Timer queues for the different clock types - RC Ptr */
|
---|
408 | RCPTRTYPE(PTMTIMERQUEUE) paTimerQueuesRC;
|
---|
409 |
|
---|
410 | /** Pointer to our RC mapping of the GIP. */
|
---|
411 | RCPTRTYPE(void *) pvGIPRC;
|
---|
412 | /** Pointer to our R3 mapping of the GIP. */
|
---|
413 | R3PTRTYPE(void *) pvGIPR3;
|
---|
414 |
|
---|
415 | /** Pointer to a singly linked list of free timers.
|
---|
416 | * This chain is using the TMTIMER::pBigNext members.
|
---|
417 | * Only accessible from the emulation thread. */
|
---|
418 | PTMTIMERR3 pFree;
|
---|
419 |
|
---|
420 | /** Pointer to a doubly linked list of created timers.
|
---|
421 | * This chain is using the TMTIMER::pBigNext and TMTIMER::pBigPrev members.
|
---|
422 | * Only accessible from the emulation thread. */
|
---|
423 | PTMTIMERR3 pCreated;
|
---|
424 |
|
---|
425 | /** The schedulation timer timer handle (runtime timer).
|
---|
426 | * This timer will do freqent check on pending queue schedulations and
|
---|
427 | * raise VM_FF_TIMER to pull EMTs attention to them.
|
---|
428 | */
|
---|
429 | R3PTRTYPE(PRTTIMER) pTimer;
|
---|
430 | /** Interval in milliseconds of the pTimer timer. */
|
---|
431 | uint32_t u32TimerMillies;
|
---|
432 |
|
---|
433 | /** Indicates that queues are being run. */
|
---|
434 | bool volatile fRunningQueues;
|
---|
435 | /** Indicates that the virtual sync queue is being run. */
|
---|
436 | bool volatile fRunningVirtualSyncQueue;
|
---|
437 | /* Alignment */
|
---|
438 | bool u8Alignment[2];
|
---|
439 |
|
---|
440 | /** Lock serializing EMT access to TM. */
|
---|
441 | PDMCRITSECT EmtLock;
|
---|
442 | /** Lock serializing access to the VirtualSync clock. */
|
---|
443 | PDMCRITSECT VirtualSyncLock;
|
---|
444 |
|
---|
445 | /** TMR3TimerQueuesDo
|
---|
446 | * @{ */
|
---|
447 | STAMPROFILE StatDoQueues;
|
---|
448 | STAMPROFILEADV aStatDoQueues[TMCLOCK_MAX];
|
---|
449 | /** @} */
|
---|
450 | /** tmSchedule
|
---|
451 | * @{ */
|
---|
452 | STAMPROFILE StatScheduleOneRZ;
|
---|
453 | STAMPROFILE StatScheduleOneR3;
|
---|
454 | STAMCOUNTER StatScheduleSetFF;
|
---|
455 | STAMCOUNTER StatPostponedR3;
|
---|
456 | STAMCOUNTER StatPostponedRZ;
|
---|
457 | /** @} */
|
---|
458 | /** Read the time
|
---|
459 | * @{ */
|
---|
460 | STAMCOUNTER StatVirtualGet;
|
---|
461 | STAMCOUNTER StatVirtualGetSetFF;
|
---|
462 | STAMCOUNTER StatVirtualSyncGet;
|
---|
463 | STAMCOUNTER StatVirtualSyncGetELoop;
|
---|
464 | STAMCOUNTER StatVirtualSyncGetExpired;
|
---|
465 | STAMCOUNTER StatVirtualSyncGetLockless;
|
---|
466 | STAMCOUNTER StatVirtualSyncGetLocked;
|
---|
467 | STAMCOUNTER StatVirtualSyncGetSetFF;
|
---|
468 | STAMCOUNTER StatVirtualPause;
|
---|
469 | STAMCOUNTER StatVirtualResume;
|
---|
470 | /* @} */
|
---|
471 | /** TMTimerPoll
|
---|
472 | * @{ */
|
---|
473 | STAMCOUNTER StatPoll;
|
---|
474 | STAMCOUNTER StatPollAlreadySet;
|
---|
475 | STAMCOUNTER StatPollELoop;
|
---|
476 | STAMCOUNTER StatPollMiss;
|
---|
477 | STAMCOUNTER StatPollRunning;
|
---|
478 | STAMCOUNTER StatPollSimple;
|
---|
479 | STAMCOUNTER StatPollVirtual;
|
---|
480 | STAMCOUNTER StatPollVirtualSync;
|
---|
481 | /** @} */
|
---|
482 | /** TMTimerSet
|
---|
483 | * @{ */
|
---|
484 | STAMPROFILE StatTimerSetRZ;
|
---|
485 | STAMPROFILE StatTimerSetR3;
|
---|
486 | /** @} */
|
---|
487 | /** TMTimerStop
|
---|
488 | * @{ */
|
---|
489 | STAMPROFILE StatTimerStopRZ;
|
---|
490 | STAMPROFILE StatTimerStopR3;
|
---|
491 | /** @} */
|
---|
492 | /** VirtualSync - Running and Catching Up
|
---|
493 | * @{ */
|
---|
494 | STAMCOUNTER StatVirtualSyncRun;
|
---|
495 | STAMCOUNTER StatVirtualSyncRunRestart;
|
---|
496 | STAMPROFILE StatVirtualSyncRunSlack;
|
---|
497 | STAMCOUNTER StatVirtualSyncRunStop;
|
---|
498 | STAMCOUNTER StatVirtualSyncRunStoppedAlready;
|
---|
499 | STAMCOUNTER StatVirtualSyncGiveUp;
|
---|
500 | STAMCOUNTER StatVirtualSyncGiveUpBeforeStarting;
|
---|
501 | STAMPROFILEADV StatVirtualSyncCatchup;
|
---|
502 | STAMCOUNTER aStatVirtualSyncCatchupInitial[TM_MAX_CATCHUP_PERIODS];
|
---|
503 | STAMCOUNTER aStatVirtualSyncCatchupAdjust[TM_MAX_CATCHUP_PERIODS];
|
---|
504 | /** @} */
|
---|
505 | /** TMR3VirtualSyncFF (non dedicated EMT). */
|
---|
506 | STAMPROFILE StatVirtualSyncFF;
|
---|
507 | /** The timer callback. */
|
---|
508 | STAMCOUNTER StatTimerCallbackSetFF;
|
---|
509 |
|
---|
510 | /** @name Reasons for refusing TSC offsetting in TMCpuTickCanUseRealTSC.
|
---|
511 | * @{ */
|
---|
512 | STAMCOUNTER StatTSCNotFixed;
|
---|
513 | STAMCOUNTER StatTSCNotTicking;
|
---|
514 | STAMCOUNTER StatTSCCatchupLE010;
|
---|
515 | STAMCOUNTER StatTSCCatchupLE025;
|
---|
516 | STAMCOUNTER StatTSCCatchupLE100;
|
---|
517 | STAMCOUNTER StatTSCCatchupOther;
|
---|
518 | STAMCOUNTER StatTSCWarp;
|
---|
519 | STAMCOUNTER StatTSCSyncNotTicking;
|
---|
520 | /** @} */
|
---|
521 | } TM;
|
---|
522 | /** Pointer to TM VM instance data. */
|
---|
523 | typedef TM *PTM;
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * TM VMCPU Instance data.
|
---|
527 | * Changes to this must checked against the padding of the tm union in VM!
|
---|
528 | */
|
---|
529 | typedef struct TMCPU
|
---|
530 | {
|
---|
531 | /** Offset to the VMCPU structure.
|
---|
532 | * See TMCPU2VM(). */
|
---|
533 | RTUINT offVMCPU;
|
---|
534 |
|
---|
535 | /** CPU timestamp ticking enabled indicator (bool). (RDTSC) */
|
---|
536 | bool fTSCTicking;
|
---|
537 | bool afAlignment0[3]; /**< alignment padding */
|
---|
538 |
|
---|
539 | /** The offset between the host TSC and the Guest TSC.
|
---|
540 | * Only valid if fTicking is set and and fTSCUseRealTSC is clear. */
|
---|
541 | uint64_t u64TSCOffset;
|
---|
542 |
|
---|
543 | /** The guest TSC when fTicking is cleared. */
|
---|
544 | uint64_t u64TSC;
|
---|
545 |
|
---|
546 | } TMCPU;
|
---|
547 | /** Pointer to TM VMCPU instance data. */
|
---|
548 | typedef TMCPU *PTMCPU;
|
---|
549 |
|
---|
550 | #if 0 /* enable this to rule out locking bugs on single cpu guests. */
|
---|
551 | # define tmLock(pVM) VINF_SUCCESS
|
---|
552 | # define tmTryLock(pVM) VINF_SUCCESS
|
---|
553 | # define tmUnlock(pVM) ((void)0)
|
---|
554 | # define tmVirtualSyncLock(pVM) VINF_SUCCESS
|
---|
555 | # define tmVirtualSyncTryLock(pVM) VINF_SUCCESS
|
---|
556 | # define tmVirtualSyncUnlock(pVM) ((void)0)
|
---|
557 | # define TM_ASSERT_EMT_LOCK(pVM) VM_ASSERT_EMT(pVM)
|
---|
558 | #else
|
---|
559 | int tmLock(PVM pVM);
|
---|
560 | int tmTryLock(PVM pVM);
|
---|
561 | void tmUnlock(PVM pVM);
|
---|
562 | /** Checks that the caller owns the EMT lock. */
|
---|
563 | #define TM_ASSERT_EMT_LOCK(pVM) Assert(PDMCritSectIsOwner(&pVM->tm.s.EmtLock))
|
---|
564 | int tmVirtualSyncLock(PVM pVM);
|
---|
565 | int tmVirtualSyncTryLock(PVM pVM);
|
---|
566 | void tmVirtualSyncUnlock(PVM pVM);
|
---|
567 | #endif
|
---|
568 |
|
---|
569 | const char *tmTimerState(TMTIMERSTATE enmState);
|
---|
570 | void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue);
|
---|
571 | #ifdef VBOX_STRICT
|
---|
572 | void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere);
|
---|
573 | #endif
|
---|
574 |
|
---|
575 | int tmCpuTickPause(PVM pVM, PVMCPU pVCpu);
|
---|
576 | int tmCpuTickResume(PVM pVM, PVMCPU pVCpu);
|
---|
577 |
|
---|
578 | int tmVirtualPauseLocked(PVM pVM);
|
---|
579 | int tmVirtualResumeLocked(PVM pVM);
|
---|
580 | DECLEXPORT(void) tmVirtualNanoTSBad(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
|
---|
581 | DECLEXPORT(uint64_t) tmVirtualNanoTSRediscover(PRTTIMENANOTSDATA pData);
|
---|
582 |
|
---|
583 |
|
---|
584 | /** @} */
|
---|
585 |
|
---|
586 | __END_DECLS
|
---|
587 |
|
---|
588 | #endif
|
---|
589 |
|
---|