VirtualBox

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

Last change on this file since 106443 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

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

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