VirtualBox

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

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

VMM/TM: Removed pVMR3, pVMR0 and pVMRC from TMTIMER. bugref:9943

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