VirtualBox

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

Last change on this file since 90638 was 90638, checked in by vboxsync, 3 years ago

VMM,VMMDev: Force 32-byte alignment of PDMCRITSECTRW so all members that are being updated atomically are in the same cacheline. This also ensures that the 128-bit union is correctly aligned. bugref:6695

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