VirtualBox

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

Last change on this file since 87766 was 87766, checked in by vboxsync, 4 years ago

VMM/TM,VMM/*: Refactored the TM timer APIs to use 'handles' and take a pVM parameter. Only internal callbacks have been updated with a hTimer parameter, so far. bugref:9943

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