VirtualBox

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

Last change on this file since 69950 was 69950, checked in by vboxsync, 7 years ago

VMM/TMR3UtcNow: Log time changes between two calls larger than 30min.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 32.5 KB
Line 
1/* $Id: TMInternal.h 69950 2017-12-06 10:43:06Z vboxsync $ */
2/** @file
3 * TM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
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
18#ifndef ___TMInternal_h
19#define ___TMInternal_h
20
21#include <VBox/cdefs.h>
22#include <VBox/types.h>
23#include <iprt/time.h>
24#include <iprt/timer.h>
25#include <iprt/assert.h>
26#include <VBox/vmm/stam.h>
27#include <VBox/vmm/pdmcritsect.h>
28
29RT_C_DECLS_BEGIN
30
31
32/** @defgroup grp_tm_int Internal
33 * @ingroup grp_tm
34 * @internal
35 * @{
36 */
37
38/** Frequency of the real clock. */
39#define TMCLOCK_FREQ_REAL UINT32_C(1000)
40/** Frequency of the virtual clock. */
41#define TMCLOCK_FREQ_VIRTUAL UINT32_C(1000000000)
42
43
44/**
45 * Timer type.
46 */
47typedef enum TMTIMERTYPE
48{
49 /** Device timer. */
50 TMTIMERTYPE_DEV = 1,
51 /** USB device timer. */
52 TMTIMERTYPE_USB,
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, getting expire and unlinking. */
71 TMTIMERSTATE_EXPIRED_GET_UNLINK,
72 /** Timer is expired and is being delivered. */
73 TMTIMERSTATE_EXPIRED_DELIVER,
74
75 /** Timer is stopped but still in the active list.
76 * Currently in the ScheduleTimers list. */
77 TMTIMERSTATE_PENDING_STOP,
78 /** Timer is stopped but needs unlinking from the ScheduleTimers list.
79 * Currently in the ScheduleTimers list. */
80 TMTIMERSTATE_PENDING_STOP_SCHEDULE,
81 /** Timer is being modified and will soon be pending scheduling.
82 * Currently in the ScheduleTimers list. */
83 TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE,
84 /** Timer is pending scheduling.
85 * Currently in the ScheduleTimers list. */
86 TMTIMERSTATE_PENDING_SCHEDULE,
87 /** Timer is being modified and will soon be pending rescheduling.
88 * Currently in the ScheduleTimers list and the active list. */
89 TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE,
90 /** Timer is modified and is now pending rescheduling.
91 * Currently in the ScheduleTimers list and the active list. */
92 TMTIMERSTATE_PENDING_RESCHEDULE,
93 /** Timer is being destroyed. */
94 TMTIMERSTATE_DESTROY,
95 /** Timer is free. */
96 TMTIMERSTATE_FREE
97} TMTIMERSTATE;
98
99/** Predicate that returns true if the give state is pending scheduling or
100 * rescheduling of any kind. Will reference the argument more than once! */
101#define TMTIMERSTATE_IS_PENDING_SCHEDULING(enmState) \
102 ( (enmState) <= TMTIMERSTATE_PENDING_RESCHEDULE \
103 && (enmState) >= TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE)
104
105
106/**
107 * Internal representation of a timer.
108 *
109 * For correct serialization (without the use of semaphores and
110 * other blocking/slow constructs) certain rules applies to updating
111 * this structure:
112 * - For thread other than EMT only u64Expire, enmState and pScheduleNext*
113 * are changeable. Everything else is out of bounds.
114 * - Updating of u64Expire timer can only happen in the TMTIMERSTATE_STOPPED
115 * and TMTIMERSTATE_PENDING_RESCHEDULING_SET_EXPIRE states.
116 * - Timers in the TMTIMERSTATE_EXPIRED state are only accessible from EMT.
117 * - Actual destruction of a timer can only be done at scheduling time.
118 */
119typedef struct TMTIMER
120{
121 /** Expire time. */
122 volatile uint64_t u64Expire;
123 /** Clock to apply to u64Expire. */
124 TMCLOCK enmClock;
125 /** Timer callback type. */
126 TMTIMERTYPE enmType;
127 /** Type specific data. */
128 union
129 {
130 /** TMTIMERTYPE_DEV. */
131 struct
132 {
133 /** Callback. */
134 R3PTRTYPE(PFNTMTIMERDEV) pfnTimer;
135 /** Device instance. */
136 PPDMDEVINSR3 pDevIns;
137 } Dev;
138
139 /** TMTIMERTYPE_DEV. */
140 struct
141 {
142 /** Callback. */
143 R3PTRTYPE(PFNTMTIMERUSB) pfnTimer;
144 /** USB device instance. */
145 PPDMUSBINS pUsbIns;
146 } Usb;
147
148 /** TMTIMERTYPE_DRV. */
149 struct
150 {
151 /** Callback. */
152 R3PTRTYPE(PFNTMTIMERDRV) pfnTimer;
153 /** Device instance. */
154 R3PTRTYPE(PPDMDRVINS) pDrvIns;
155 } Drv;
156
157 /** TMTIMERTYPE_INTERNAL. */
158 struct
159 {
160 /** Callback. */
161 R3PTRTYPE(PFNTMTIMERINT) pfnTimer;
162 } Internal;
163
164 /** TMTIMERTYPE_EXTERNAL. */
165 struct
166 {
167 /** Callback. */
168 R3PTRTYPE(PFNTMTIMEREXT) pfnTimer;
169 } External;
170 } u;
171
172 /** Timer state. */
173 volatile TMTIMERSTATE enmState;
174 /** Timer relative offset to the next timer in the schedule list. */
175 int32_t volatile offScheduleNext;
176
177 /** Timer relative offset to the next timer in the chain. */
178 int32_t offNext;
179 /** Timer relative offset to the previous timer in the chain. */
180 int32_t offPrev;
181
182 /** Pointer to the VM the timer belongs to - R3 Ptr. */
183 PVMR3 pVMR3;
184 /** Pointer to the VM the timer belongs to - R0 Ptr. */
185 PVMR0 pVMR0;
186 /** Pointer to the VM the timer belongs to - RC Ptr. */
187 PVMRC pVMRC;
188 /** The timer frequency hint. This is 0 if not hint was given. */
189 uint32_t volatile uHzHint;
190
191 /** User argument. */
192 RTR3PTR pvUser;
193 /** The critical section associated with the lock. */
194 R3PTRTYPE(PPDMCRITSECT) pCritSect;
195
196 /** Pointer to the next timer in the list of created or free timers. (TM::pTimers or TM::pFree) */
197 PTMTIMERR3 pBigNext;
198 /** Pointer to the previous timer in the list of all created timers. (TM::pTimers) */
199 PTMTIMERR3 pBigPrev;
200 /** Pointer to the timer description. */
201 R3PTRTYPE(const char *) pszDesc;
202#if HC_ARCH_BITS == 32
203 uint32_t padding0; /**< pad structure to multiple of 8 bytes. */
204#endif
205} TMTIMER;
206AssertCompileMemberSize(TMTIMER, enmState, sizeof(uint32_t));
207
208
209/**
210 * Updates a timer state in the correct atomic manner.
211 */
212#if 1
213# define TM_SET_STATE(pTimer, state) \
214 ASMAtomicWriteU32((uint32_t volatile *)&(pTimer)->enmState, state)
215#else
216# define TM_SET_STATE(pTimer, state) \
217 do { \
218 uint32_t uOld1 = (pTimer)->enmState; \
219 Log(("%s: %p: %d -> %d\n", __FUNCTION__, (pTimer), (pTimer)->enmState, state)); \
220 uint32_t uOld2 = ASMAtomicXchgU32((uint32_t volatile *)&(pTimer)->enmState, state); \
221 Assert(uOld1 == uOld2); \
222 } while (0)
223#endif
224
225/**
226 * Tries to updates a timer state in the correct atomic manner.
227 */
228#if 1
229# define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
230 (fRc) = ASMAtomicCmpXchgU32((uint32_t volatile *)&(pTimer)->enmState, StateNew, StateOld)
231#else
232# define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
233 do { (fRc) = ASMAtomicCmpXchgU32((uint32_t volatile *)&(pTimer)->enmState, StateNew, StateOld); \
234 Log(("%s: %p: %d -> %d %RTbool\n", __FUNCTION__, (pTimer), StateOld, StateNew, fRc)); \
235 } while (0)
236#endif
237
238/** Get the previous timer. */
239#define TMTIMER_GET_PREV(pTimer) ((PTMTIMER)((pTimer)->offPrev ? (intptr_t)(pTimer) + (pTimer)->offPrev : 0))
240/** Get the next timer. */
241#define TMTIMER_GET_NEXT(pTimer) ((PTMTIMER)((pTimer)->offNext ? (intptr_t)(pTimer) + (pTimer)->offNext : 0))
242/** Set the previous timer link. */
243#define TMTIMER_SET_PREV(pTimer, pPrev) ((pTimer)->offPrev = (pPrev) ? (intptr_t)(pPrev) - (intptr_t)(pTimer) : 0)
244/** Set the next timer link. */
245#define TMTIMER_SET_NEXT(pTimer, pNext) ((pTimer)->offNext = (pNext) ? (intptr_t)(pNext) - (intptr_t)(pTimer) : 0)
246
247
248/**
249 * A timer queue.
250 *
251 * This is allocated on the hyper heap.
252 */
253typedef struct TMTIMERQUEUE
254{
255 /** The cached expire time for this queue.
256 * Updated by EMT when scheduling the queue or modifying the head timer.
257 * Assigned UINT64_MAX when there is no head timer. */
258 uint64_t u64Expire;
259 /** Doubly linked list of active timers.
260 *
261 * When no scheduling is pending, this list is will be ordered by expire time (ascending).
262 * Access is serialized by only letting the emulation thread (EMT) do changes.
263 *
264 * The offset is relative to the queue structure.
265 */
266 int32_t offActive;
267 /** List of timers pending scheduling of some kind.
268 *
269 * Timer stats allowed in the list are TMTIMERSTATE_PENDING_STOPPING,
270 * TMTIMERSTATE_PENDING_DESTRUCTION, TMTIMERSTATE_PENDING_STOPPING_DESTRUCTION,
271 * TMTIMERSTATE_PENDING_RESCHEDULING and TMTIMERSTATE_PENDING_SCHEDULE.
272 *
273 * The offset is relative to the queue structure.
274 */
275 int32_t volatile offSchedule;
276 /** The clock for this queue. */
277 TMCLOCK enmClock;
278 /** Pad the structure up to 32 bytes. */
279 uint32_t au32Padding[3];
280} TMTIMERQUEUE;
281
282/** Pointer to a timer queue. */
283typedef TMTIMERQUEUE *PTMTIMERQUEUE;
284
285/** Get the head of the active timer list. */
286#define TMTIMER_GET_HEAD(pQueue) ((PTMTIMER)((pQueue)->offActive ? (intptr_t)(pQueue) + (pQueue)->offActive : 0))
287/** Set the head of the active timer list. */
288#define TMTIMER_SET_HEAD(pQueue, pHead) ((pQueue)->offActive = pHead ? (intptr_t)pHead - (intptr_t)(pQueue) : 0)
289
290
291/**
292 * CPU load data set.
293 * Mainly used by tmR3CpuLoadTimer.
294 */
295typedef struct TMCPULOADSTATE
296{
297 /** The percent of the period spent executing guest code. */
298 uint8_t cPctExecuting;
299 /** The percent of the period spent halted. */
300 uint8_t cPctHalted;
301 /** The percent of the period spent on other things. */
302 uint8_t cPctOther;
303 /** Explicit alignment padding */
304 uint8_t au8Alignment[5];
305
306 /** Previous cNsTotal value. */
307 uint64_t cNsPrevTotal;
308 /** Previous cNsExecuting value. */
309 uint64_t cNsPrevExecuting;
310 /** Previous cNsHalted value. */
311 uint64_t cNsPrevHalted;
312} TMCPULOADSTATE;
313AssertCompileSizeAlignment(TMCPULOADSTATE, 8);
314AssertCompileMemberAlignment(TMCPULOADSTATE, cNsPrevTotal, 8);
315/** Pointer to a CPU load data set. */
316typedef TMCPULOADSTATE *PTMCPULOADSTATE;
317
318
319/**
320 * TSC mode.
321 *
322 * The main modes of how TM implements the TSC clock (TMCLOCK_TSC).
323 */
324typedef enum TMTSCMODE
325{
326 /** The guest TSC is an emulated, virtual TSC. */
327 TMTSCMODE_VIRT_TSC_EMULATED = 1,
328 /** The guest TSC is an offset of the real TSC. */
329 TMTSCMODE_REAL_TSC_OFFSET,
330 /** The guest TSC is dynamically derived through emulating or offsetting. */
331 TMTSCMODE_DYNAMIC
332} TMTSCMODE;
333AssertCompileSize(TMTSCMODE, sizeof(uint32_t));
334
335
336/**
337 * Converts a TM pointer into a VM pointer.
338 * @returns Pointer to the VM structure the TM is part of.
339 * @param pTM Pointer to TM instance data.
340 */
341#define TM2VM(pTM) ( (PVM)((char*)pTM - pTM->offVM) )
342
343
344/**
345 * TM VM Instance data.
346 * Changes to this must checked against the padding of the cfgm union in VM!
347 */
348typedef struct TM
349{
350 /** Offset to the VM structure.
351 * See TM2VM(). */
352 RTUINT offVM;
353
354 /** The current TSC mode of the VM.
355 * Config variable: Mode (string). */
356 TMTSCMODE enmTSCMode;
357 /** The original TSC mode of the VM. */
358 TMTSCMODE enmOriginalTSCMode;
359 /** Alignment padding. */
360 uint32_t u32Alignment0;
361 /** Whether the TSC is tied to the execution of code.
362 * Config variable: TSCTiedToExecution (bool) */
363 bool fTSCTiedToExecution;
364 /** Modifier for fTSCTiedToExecution which pauses the TSC while halting if true.
365 * Config variable: TSCNotTiedToHalt (bool) */
366 bool fTSCNotTiedToHalt;
367 /** Whether TM TSC mode switching is allowed at runtime. */
368 bool fTSCModeSwitchAllowed;
369 /** Whether the guest has enabled use of paravirtualized TSC. */
370 bool fParavirtTscEnabled;
371 /** The ID of the virtual CPU that normally runs the timers. */
372 VMCPUID idTimerCpu;
373
374 /** The number of CPU clock ticks per second (TMCLOCK_TSC).
375 * Config variable: TSCTicksPerSecond (64-bit unsigned int)
376 * The config variable implies @c enmTSCMode would be
377 * TMTSCMODE_VIRT_TSC_EMULATED. */
378 uint64_t cTSCTicksPerSecond;
379 /** The TSC difference introduced by pausing the VM. */
380 uint64_t offTSCPause;
381 /** The TSC value when the last TSC was paused. */
382 uint64_t u64LastPausedTSC;
383 /** CPU TSCs ticking indicator (one for each VCPU). */
384 uint32_t volatile cTSCsTicking;
385
386 /** Virtual time ticking enabled indicator (counter for each VCPU). (TMCLOCK_VIRTUAL) */
387 uint32_t volatile cVirtualTicking;
388 /** Virtual time is not running at 100%. */
389 bool fVirtualWarpDrive;
390 /** Virtual timer synchronous time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL_SYNC) */
391 bool volatile fVirtualSyncTicking;
392 /** Virtual timer synchronous time catch-up active. */
393 bool volatile fVirtualSyncCatchUp;
394 /** Alignment padding. */
395 bool afAlignment1[1];
396 /** WarpDrive percentage.
397 * 100% is normal (fVirtualSyncNormal == true). When other than 100% we apply
398 * this percentage to the raw time source for the period it's been valid in,
399 * i.e. since u64VirtualWarpDriveStart. */
400 uint32_t u32VirtualWarpDrivePercentage;
401
402 /** The offset of the virtual clock relative to it's timesource.
403 * Only valid if fVirtualTicking is set. */
404 uint64_t u64VirtualOffset;
405 /** The guest virtual time when fVirtualTicking is cleared. */
406 uint64_t u64Virtual;
407 /** When the Warp drive was started or last adjusted.
408 * Only valid when fVirtualWarpDrive is set. */
409 uint64_t u64VirtualWarpDriveStart;
410 /** The previously returned nano TS.
411 * This handles TSC drift on SMP systems and expired interval.
412 * This is a valid range u64NanoTS to u64NanoTS + 1000000000 (ie. 1sec). */
413 uint64_t volatile u64VirtualRawPrev;
414 /** The ring-3 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
415 RTTIMENANOTSDATAR3 VirtualGetRawDataR3;
416 /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
417 RTTIMENANOTSDATAR0 VirtualGetRawDataR0;
418 /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
419 RTTIMENANOTSDATARC VirtualGetRawDataRC;
420 /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
421 R3PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR3;
422 /** Pointer to the ring-0 tmVirtualGetRawNanoTS worker function. */
423 R0PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR0;
424 /** Pointer to the raw-mode tmVirtualGetRawNanoTS worker function. */
425 RCPTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawRC;
426 /** Alignment. */
427 RTRCPTR AlignmentRCPtr;
428 /** The guest virtual timer synchronous time when fVirtualSyncTicking is cleared.
429 * When fVirtualSyncTicking is set it holds the last time returned to
430 * the guest (while the lock was held). */
431 uint64_t volatile u64VirtualSync;
432 /** The offset of the timer synchronous virtual clock (TMCLOCK_VIRTUAL_SYNC) relative
433 * to the virtual clock (TMCLOCK_VIRTUAL).
434 * (This is accessed by the timer thread and must be updated atomically.) */
435 uint64_t volatile offVirtualSync;
436 /** The offset into offVirtualSync that's been irrevocably given up by failed catch-up attempts.
437 * Thus the current lag is offVirtualSync - offVirtualSyncGivenUp. */
438 uint64_t offVirtualSyncGivenUp;
439 /** The TMCLOCK_VIRTUAL at the previous TMVirtualGetSync call when catch-up is active. */
440 uint64_t volatile u64VirtualSyncCatchUpPrev;
441 /** The current catch-up percentage. */
442 uint32_t volatile u32VirtualSyncCatchUpPercentage;
443 /** How much slack when processing timers. */
444 uint32_t u32VirtualSyncScheduleSlack;
445 /** When to stop catch-up. */
446 uint64_t u64VirtualSyncCatchUpStopThreshold;
447 /** When to give up catch-up. */
448 uint64_t u64VirtualSyncCatchUpGiveUpThreshold;
449/** @def TM_MAX_CATCHUP_PERIODS
450 * The number of catchup rates. */
451#define TM_MAX_CATCHUP_PERIODS 10
452 /** The aggressiveness of the catch-up relative to how far we've lagged behind.
453 * The idea is to have increasing catch-up percentage as the lag increases. */
454 struct TMCATCHUPPERIOD
455 {
456 uint64_t u64Start; /**< When this period starts. (u64VirtualSyncOffset). */
457 uint32_t u32Percentage; /**< The catch-up percent to apply. */
458 uint32_t u32Alignment; /**< Structure alignment */
459 } aVirtualSyncCatchUpPeriods[TM_MAX_CATCHUP_PERIODS];
460
461 /** The current max timer Hz hint. */
462 uint32_t volatile uMaxHzHint;
463 /** Whether to recalulate the HzHint next time its queried. */
464 bool volatile fHzHintNeedsUpdating;
465 /** Alignment */
466 bool afAlignment2[3];
467 /** @cfgm{/TM/HostHzMax, uint32_t, Hz, 0, UINT32_MAX, 20000}
468 * The max host Hz frequency hint returned by TMCalcHostTimerFrequency. */
469 uint32_t cHostHzMax;
470 /** @cfgm{/TM/HostHzFudgeFactorTimerCpu, uint32_t, Hz, 0, UINT32_MAX, 111}
471 * The number of Hz TMCalcHostTimerFrequency adds for the timer CPU. */
472 uint32_t cPctHostHzFudgeFactorTimerCpu;
473 /** @cfgm{/TM/HostHzFudgeFactorOtherCpu, uint32_t, Hz, 0, UINT32_MAX, 110}
474 * The number of Hz TMCalcHostTimerFrequency adds for the other CPUs. */
475 uint32_t cPctHostHzFudgeFactorOtherCpu;
476 /** @cfgm{/TM/HostHzFudgeFactorCatchUp100, uint32_t, Hz, 0, UINT32_MAX, 300}
477 * The fudge factor (expressed in percent) that catch-up percentages below
478 * 100% is multiplied by. */
479 uint32_t cPctHostHzFudgeFactorCatchUp100;
480 /** @cfgm{/TM/HostHzFudgeFactorCatchUp200, uint32_t, Hz, 0, UINT32_MAX, 250}
481 * The fudge factor (expressed in percent) that catch-up percentages
482 * 100%-199% is multiplied by. */
483 uint32_t cPctHostHzFudgeFactorCatchUp200;
484 /** @cfgm{/TM/HostHzFudgeFactorCatchUp400, uint32_t, Hz, 0, UINT32_MAX, 200}
485 * The fudge factor (expressed in percent) that catch-up percentages
486 * 200%-399% is multiplied by. */
487 uint32_t cPctHostHzFudgeFactorCatchUp400;
488
489 /** The UTC offset in ns.
490 * This is *NOT* for converting UTC to local time. It is for converting real
491 * world UTC time to VM UTC time. This feature is indented for doing date
492 * testing of software and similar.
493 * @todo Implement warpdrive on UTC. */
494 int64_t offUTC;
495 /** The last value TMR3UtcNow returned. */
496 int64_t volatile nsLastUtcNow;
497
498 /** Timer queues for the different clock types - R3 Ptr */
499 R3PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR3;
500 /** Timer queues for the different clock types - R0 Ptr */
501 R0PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR0;
502 /** Timer queues for the different clock types - RC Ptr */
503 RCPTRTYPE(PTMTIMERQUEUE) paTimerQueuesRC;
504
505 /** Pointer to our RC mapping of the GIP. */
506 RCPTRTYPE(void *) pvGIPRC;
507 /** Pointer to our R3 mapping of the GIP. */
508 R3PTRTYPE(void *) pvGIPR3;
509
510 /** Pointer to a singly linked list of free timers.
511 * This chain is using the TMTIMER::pBigNext members.
512 * Only accessible from the emulation thread. */
513 PTMTIMERR3 pFree;
514
515 /** Pointer to a doubly linked list of created timers.
516 * This chain is using the TMTIMER::pBigNext and TMTIMER::pBigPrev members.
517 * Only accessible from the emulation thread. */
518 PTMTIMERR3 pCreated;
519
520 /** The schedule timer timer handle (runtime timer).
521 * This timer will do frequent check on pending queue schedules and
522 * raise VM_FF_TIMER to pull EMTs attention to them.
523 */
524 R3PTRTYPE(PRTTIMER) pTimer;
525 /** Interval in milliseconds of the pTimer timer. */
526 uint32_t u32TimerMillies;
527
528 /** Indicates that queues are being run. */
529 bool volatile fRunningQueues;
530 /** Indicates that the virtual sync queue is being run. */
531 bool volatile fRunningVirtualSyncQueue;
532 /** Alignment */
533 bool afAlignment3[2];
534
535 /** Lock serializing access to the timer lists. */
536 PDMCRITSECT TimerCritSect;
537 /** Lock serializing access to the VirtualSync clock and the associated
538 * timer queue. */
539 PDMCRITSECT VirtualSyncLock;
540
541 /** CPU load state for all the virtual CPUs (tmR3CpuLoadTimer). */
542 TMCPULOADSTATE CpuLoad;
543
544 /** TMR3TimerQueuesDo
545 * @{ */
546 STAMPROFILE StatDoQueues;
547 STAMPROFILEADV aStatDoQueues[TMCLOCK_MAX];
548 /** @} */
549 /** tmSchedule
550 * @{ */
551 STAMPROFILE StatScheduleOneRZ;
552 STAMPROFILE StatScheduleOneR3;
553 STAMCOUNTER StatScheduleSetFF;
554 STAMCOUNTER StatPostponedR3;
555 STAMCOUNTER StatPostponedRZ;
556 /** @} */
557 /** Read the time
558 * @{ */
559 STAMCOUNTER StatVirtualGet;
560 STAMCOUNTER StatVirtualGetSetFF;
561 STAMCOUNTER StatVirtualSyncGet;
562 STAMCOUNTER StatVirtualSyncGetAdjLast;
563 STAMCOUNTER StatVirtualSyncGetELoop;
564 STAMCOUNTER StatVirtualSyncGetExpired;
565 STAMCOUNTER StatVirtualSyncGetLockless;
566 STAMCOUNTER StatVirtualSyncGetLocked;
567 STAMCOUNTER StatVirtualSyncGetSetFF;
568 STAMCOUNTER StatVirtualPause;
569 STAMCOUNTER StatVirtualResume;
570 /** @} */
571 /** TMTimerPoll
572 * @{ */
573 STAMCOUNTER StatPoll;
574 STAMCOUNTER StatPollAlreadySet;
575 STAMCOUNTER StatPollELoop;
576 STAMCOUNTER StatPollMiss;
577 STAMCOUNTER StatPollRunning;
578 STAMCOUNTER StatPollSimple;
579 STAMCOUNTER StatPollVirtual;
580 STAMCOUNTER StatPollVirtualSync;
581 /** @} */
582 /** TMTimerSet sans virtual sync timers.
583 * @{ */
584 STAMCOUNTER StatTimerSet;
585 STAMCOUNTER StatTimerSetOpt;
586 STAMPROFILE StatTimerSetRZ;
587 STAMPROFILE StatTimerSetR3;
588 STAMCOUNTER StatTimerSetStStopped;
589 STAMCOUNTER StatTimerSetStExpDeliver;
590 STAMCOUNTER StatTimerSetStActive;
591 STAMCOUNTER StatTimerSetStPendStop;
592 STAMCOUNTER StatTimerSetStPendStopSched;
593 STAMCOUNTER StatTimerSetStPendSched;
594 STAMCOUNTER StatTimerSetStPendResched;
595 STAMCOUNTER StatTimerSetStOther;
596 /** @} */
597 /** TMTimerSet on virtual sync timers.
598 * @{ */
599 STAMCOUNTER StatTimerSetVs;
600 STAMPROFILE StatTimerSetVsRZ;
601 STAMPROFILE StatTimerSetVsR3;
602 STAMCOUNTER StatTimerSetVsStStopped;
603 STAMCOUNTER StatTimerSetVsStExpDeliver;
604 STAMCOUNTER StatTimerSetVsStActive;
605 /** @} */
606 /** TMTimerSetRelative sans virtual sync timers
607 * @{ */
608 STAMCOUNTER StatTimerSetRelative;
609 STAMPROFILE StatTimerSetRelativeRZ;
610 STAMPROFILE StatTimerSetRelativeR3;
611 STAMCOUNTER StatTimerSetRelativeOpt;
612 STAMCOUNTER StatTimerSetRelativeStStopped;
613 STAMCOUNTER StatTimerSetRelativeStExpDeliver;
614 STAMCOUNTER StatTimerSetRelativeStActive;
615 STAMCOUNTER StatTimerSetRelativeStPendStop;
616 STAMCOUNTER StatTimerSetRelativeStPendStopSched;
617 STAMCOUNTER StatTimerSetRelativeStPendSched;
618 STAMCOUNTER StatTimerSetRelativeStPendResched;
619 STAMCOUNTER StatTimerSetRelativeStOther;
620 /** @} */
621 /** TMTimerSetRelative on virtual sync timers.
622 * @{ */
623 STAMCOUNTER StatTimerSetRelativeVs;
624 STAMPROFILE StatTimerSetRelativeVsRZ;
625 STAMPROFILE StatTimerSetRelativeVsR3;
626 STAMCOUNTER StatTimerSetRelativeVsStStopped;
627 STAMCOUNTER StatTimerSetRelativeVsStExpDeliver;
628 STAMCOUNTER StatTimerSetRelativeVsStActive;
629 /** @} */
630 /** TMTimerStop sans virtual sync.
631 * @{ */
632 STAMPROFILE StatTimerStopRZ;
633 STAMPROFILE StatTimerStopR3;
634 /** @} */
635 /** TMTimerStop on virtual sync timers.
636 * @{ */
637 STAMPROFILE StatTimerStopVsRZ;
638 STAMPROFILE StatTimerStopVsR3;
639 /** @} */
640 /** VirtualSync - Running and Catching Up
641 * @{ */
642 STAMCOUNTER StatVirtualSyncRun;
643 STAMCOUNTER StatVirtualSyncRunRestart;
644 STAMPROFILE StatVirtualSyncRunSlack;
645 STAMCOUNTER StatVirtualSyncRunStop;
646 STAMCOUNTER StatVirtualSyncRunStoppedAlready;
647 STAMCOUNTER StatVirtualSyncGiveUp;
648 STAMCOUNTER StatVirtualSyncGiveUpBeforeStarting;
649 STAMPROFILEADV StatVirtualSyncCatchup;
650 STAMCOUNTER aStatVirtualSyncCatchupInitial[TM_MAX_CATCHUP_PERIODS];
651 STAMCOUNTER aStatVirtualSyncCatchupAdjust[TM_MAX_CATCHUP_PERIODS];
652 /** @} */
653 /** TMR3VirtualSyncFF (non dedicated EMT). */
654 STAMPROFILE StatVirtualSyncFF;
655 /** The timer callback. */
656 STAMCOUNTER StatTimerCallbackSetFF;
657 STAMCOUNTER StatTimerCallback;
658
659 /** Calls to TMCpuTickSet. */
660 STAMCOUNTER StatTSCSet;
661
662 /** TSC starts and stops. */
663 STAMCOUNTER StatTSCPause;
664 STAMCOUNTER StatTSCResume;
665
666 /** @name Reasons for refusing TSC offsetting in TMCpuTickCanUseRealTSC.
667 * @{ */
668 STAMCOUNTER StatTSCNotFixed;
669 STAMCOUNTER StatTSCNotTicking;
670 STAMCOUNTER StatTSCCatchupLE010;
671 STAMCOUNTER StatTSCCatchupLE025;
672 STAMCOUNTER StatTSCCatchupLE100;
673 STAMCOUNTER StatTSCCatchupOther;
674 STAMCOUNTER StatTSCWarp;
675 STAMCOUNTER StatTSCUnderflow;
676 STAMCOUNTER StatTSCSyncNotTicking;
677 /** @} */
678} TM;
679/** Pointer to TM VM instance data. */
680typedef TM *PTM;
681
682/**
683 * TM VMCPU Instance data.
684 * Changes to this must checked against the padding of the tm union in VM!
685 */
686typedef struct TMCPU
687{
688 /** Offset to the VMCPU structure.
689 * See TMCPU2VM(). */
690 RTUINT offVMCPU;
691
692 /** CPU timestamp ticking enabled indicator (bool). (RDTSC) */
693 bool fTSCTicking;
694 bool afAlignment0[3]; /**< alignment padding */
695
696 /** The offset between the host tick (TSC/virtual depending on the TSC mode) and
697 * the guest tick. */
698 uint64_t offTSCRawSrc;
699
700 /** The guest TSC when fTicking is cleared. */
701 uint64_t u64TSC;
702
703 /** The last seen TSC by the guest. */
704 uint64_t u64TSCLastSeen;
705
706#ifndef VBOX_WITHOUT_NS_ACCOUNTING
707 /** The nanosecond timestamp of the CPU start or resume.
708 * This is recalculated when the VM is started so that
709 * cNsTotal = RTTimeNanoTS() - u64NsTsStartCpu. */
710 uint64_t u64NsTsStartTotal;
711 /** The nanosecond timestamp of the last start-execute notification. */
712 uint64_t u64NsTsStartExecuting;
713 /** The nanosecond timestamp of the last start-halt notification. */
714 uint64_t u64NsTsStartHalting;
715 /** The cNsXXX generation. */
716 uint32_t volatile uTimesGen;
717 /** Explicit alignment padding. */
718 uint32_t u32Alignment;
719 /** The number of nanoseconds total run time.
720 * @remarks This is updated when cNsExecuting and cNsHalted are updated. */
721 uint64_t cNsTotal;
722 /** The number of nanoseconds spent executing. */
723 uint64_t cNsExecuting;
724 /** The number of nanoseconds being halted. */
725 uint64_t cNsHalted;
726 /** The number of nanoseconds spent on other things.
727 * @remarks This is updated when cNsExecuting and cNsHalted are updated. */
728 uint64_t cNsOther;
729 /** The number of halts. */
730 uint64_t cPeriodsHalted;
731 /** The number of guest execution runs. */
732 uint64_t cPeriodsExecuting;
733# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
734 /** Resettable version of cNsTotal. */
735 STAMCOUNTER StatNsTotal;
736 /** Resettable version of cNsExecuting. */
737 STAMPROFILE StatNsExecuting;
738 /** Long execution intervals. */
739 STAMPROFILE StatNsExecLong;
740 /** Short execution intervals . */
741 STAMPROFILE StatNsExecShort;
742 /** Tiny execution intervals . */
743 STAMPROFILE StatNsExecTiny;
744 /** Resettable version of cNsHalted. */
745 STAMPROFILE StatNsHalted;
746 /** Resettable version of cNsOther. */
747 STAMPROFILE StatNsOther;
748# endif
749
750 /** CPU load state for this virtual CPU (tmR3CpuLoadTimer). */
751 TMCPULOADSTATE CpuLoad;
752#endif
753} TMCPU;
754/** Pointer to TM VMCPU instance data. */
755typedef TMCPU *PTMCPU;
756
757const char *tmTimerState(TMTIMERSTATE enmState);
758void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue);
759#ifdef VBOX_STRICT
760void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere);
761#endif
762
763uint64_t tmR3CpuTickGetRawVirtualNoCheck(PVM pVM);
764int tmCpuTickPause(PVMCPU pVCpu);
765int tmCpuTickPauseLocked(PVM pVM, PVMCPU pVCpu);
766int tmCpuTickResume(PVM pVM, PVMCPU pVCpu);
767int tmCpuTickResumeLocked(PVM pVM, PVMCPU pVCpu);
768
769int tmVirtualPauseLocked(PVM pVM);
770int tmVirtualResumeLocked(PVM pVM);
771DECLCALLBACK(DECLEXPORT(void)) tmVirtualNanoTSBad(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS,
772 uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
773DECLCALLBACK(DECLEXPORT(uint64_t)) tmVirtualNanoTSRediscover(PRTTIMENANOTSDATA pData);
774DECLCALLBACK(DECLEXPORT(uint64_t)) tmVirtualNanoTSBadCpuIndex(PRTTIMENANOTSDATA pData, uint16_t idApic,
775 uint16_t iCpuSet, uint16_t iGipCpu);
776
777/**
778 * Try take the timer lock, wait in ring-3 return VERR_SEM_BUSY in R0/RC.
779 *
780 * @retval VINF_SUCCESS on success (always in ring-3).
781 * @retval VERR_SEM_BUSY in RC and R0 if the semaphore is busy.
782 *
783 * @param a_pVM Pointer to the VM.
784 *
785 * @remarks The virtual sync timer queue requires the virtual sync lock.
786 */
787#define TM_LOCK_TIMERS(a_pVM) PDMCritSectEnter(&(a_pVM)->tm.s.TimerCritSect, VERR_SEM_BUSY)
788
789/**
790 * Try take the timer lock, no waiting.
791 *
792 * @retval VINF_SUCCESS on success.
793 * @retval VERR_SEM_BUSY if busy.
794 *
795 * @param a_pVM Pointer to the VM.
796 *
797 * @remarks The virtual sync timer queue requires the virtual sync lock.
798 */
799#define TM_TRY_LOCK_TIMERS(a_pVM) PDMCritSectTryEnter(&(a_pVM)->tm.s.TimerCritSect)
800
801/** Lock the timers (sans the virtual sync queue). */
802#define TM_UNLOCK_TIMERS(a_pVM) do { PDMCritSectLeave(&(a_pVM)->tm.s.TimerCritSect); } while (0)
803
804/** Checks that the caller owns the timer lock. */
805#define TM_ASSERT_TIMER_LOCK_OWNERSHIP(a_pVM) \
806 Assert(PDMCritSectIsOwner(&(a_pVM)->tm.s.TimerCritSect))
807
808/** @} */
809
810RT_C_DECLS_END
811
812#endif
813
Note: See TracBrowser for help on using the repository browser.

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