VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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