VirtualBox

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

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

VMM/TM: Moved uMaxHzHint up in the queue structure. bugref:9943

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 34.4 KB
Line 
1/* $Id: TMInternal.h 87815 2021-02-19 22:07:50Z 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 if we've disabled growing. */
295 bool fCannotGrow;
296 /** Align on 64-byte boundrary. */
297 bool afAlignment1[3];
298 /** The current max timer Hz hint. */
299 uint32_t volatile uMaxHzHint;
300
301 /* --- new cache line (64-bit / 64 bytes) --- */
302
303 /** Time spent doing scheduling and timer callbacks. */
304 STAMPROFILE StatDo;
305 uint64_t u64Alignment2[4];
306
307 /** Lock serializing the active timer list and associated work. */
308 PDMCRITSECT TimerLock;
309 /** Lock serializing timer allocation and deallocation.
310 * @note This may be used in read-mode all over the place if we later
311 * implement runtime array growing. */
312 PDMCRITSECTRW AllocLock;
313} TMTIMERQUEUE;
314AssertCompileSizeAlignment(TMTIMERQUEUE, 64);
315/** Pointer to a timer queue. */
316typedef TMTIMERQUEUE *PTMTIMERQUEUE;
317
318/**
319 * A timer queue, ring-0 only bits.
320 */
321typedef struct TMTIMERQUEUER0
322{
323 /** The size of the paTimers allocation (in entries). */
324 uint32_t cTimersAlloc;
325 uint32_t uAlignment;
326 /** The ring-0 mapping of the timer table. */
327 R0PTRTYPE(PTMTIMER) paTimers;
328 /** Handle to the timer table allocation. */
329 RTR0MEMOBJ hMemObj;
330 /** Handle to the ring-3 mapping of the timer table. */
331 RTR0MEMOBJ hMapObj;
332} TMTIMERQUEUER0;
333/** Pointer to the ring-0 timer queue data. */
334typedef TMTIMERQUEUER0 *PTMTIMERQUEUER0;
335
336/** Pointer to the current context data for a timer queue.
337 * @note In ring-3 this is the same as the shared data. */
338#ifdef IN_RING3
339typedef TMTIMERQUEUE *PTMTIMERQUEUECC;
340#else
341typedef TMTIMERQUEUER0 *PTMTIMERQUEUECC;
342#endif
343/** Helper macro for getting the current context queue point. */
344#ifdef IN_RING3
345# define TM_GET_TIMER_QUEUE_CC(a_pVM, a_idxQueue, a_pQueueShared) (a_pQueueShared)
346#else
347# define TM_GET_TIMER_QUEUE_CC(a_pVM, a_idxQueue, a_pQueueShared) (&(a_pVM)->tmr0.s.aTimerQueues[a_idxQueue])
348#endif
349
350
351/**
352 * CPU load data set.
353 * Mainly used by tmR3CpuLoadTimer.
354 */
355typedef struct TMCPULOADSTATE
356{
357 /** The percent of the period spent executing guest code. */
358 uint8_t cPctExecuting;
359 /** The percent of the period spent halted. */
360 uint8_t cPctHalted;
361 /** The percent of the period spent on other things. */
362 uint8_t cPctOther;
363 /** Explicit alignment padding */
364 uint8_t au8Alignment[1];
365 /** Index into aHistory of the current entry. */
366 uint16_t volatile idxHistory;
367 /** Number of valid history entries before idxHistory. */
368 uint16_t volatile cHistoryEntries;
369
370 /** Previous cNsTotal value. */
371 uint64_t cNsPrevTotal;
372 /** Previous cNsExecuting value. */
373 uint64_t cNsPrevExecuting;
374 /** Previous cNsHalted value. */
375 uint64_t cNsPrevHalted;
376 /** Data for the last 30 min (given an interval of 1 second). */
377 struct
378 {
379 uint8_t cPctExecuting;
380 /** The percent of the period spent halted. */
381 uint8_t cPctHalted;
382 /** The percent of the period spent on other things. */
383 uint8_t cPctOther;
384 } aHistory[30*60];
385} TMCPULOADSTATE;
386AssertCompileSizeAlignment(TMCPULOADSTATE, 8);
387AssertCompileMemberAlignment(TMCPULOADSTATE, cNsPrevTotal, 8);
388/** Pointer to a CPU load data set. */
389typedef TMCPULOADSTATE *PTMCPULOADSTATE;
390
391
392/**
393 * TSC mode.
394 *
395 * The main modes of how TM implements the TSC clock (TMCLOCK_TSC).
396 */
397typedef enum TMTSCMODE
398{
399 /** The guest TSC is an emulated, virtual TSC. */
400 TMTSCMODE_VIRT_TSC_EMULATED = 1,
401 /** The guest TSC is an offset of the real TSC. */
402 TMTSCMODE_REAL_TSC_OFFSET,
403 /** The guest TSC is dynamically derived through emulating or offsetting. */
404 TMTSCMODE_DYNAMIC,
405 /** The native API provides it. */
406 TMTSCMODE_NATIVE_API
407} TMTSCMODE;
408AssertCompileSize(TMTSCMODE, sizeof(uint32_t));
409
410
411/**
412 * TM VM Instance data.
413 * Changes to this must checked against the padding of the cfgm union in VM!
414 */
415typedef struct TM
416{
417 /** Timer queues for the different clock types.
418 * @note is first in the structure to ensure cache-line alignment. */
419 TMTIMERQUEUE aTimerQueues[TMCLOCK_MAX];
420
421 /** The current TSC mode of the VM.
422 * Config variable: Mode (string). */
423 TMTSCMODE enmTSCMode;
424 /** The original TSC mode of the VM. */
425 TMTSCMODE enmOriginalTSCMode;
426 /** Whether the TSC is tied to the execution of code.
427 * Config variable: TSCTiedToExecution (bool) */
428 bool fTSCTiedToExecution;
429 /** Modifier for fTSCTiedToExecution which pauses the TSC while halting if true.
430 * Config variable: TSCNotTiedToHalt (bool) */
431 bool fTSCNotTiedToHalt;
432 /** Whether TM TSC mode switching is allowed at runtime. */
433 bool fTSCModeSwitchAllowed;
434 /** Whether the guest has enabled use of paravirtualized TSC. */
435 bool fParavirtTscEnabled;
436 /** The ID of the virtual CPU that normally runs the timers. */
437 VMCPUID idTimerCpu;
438
439 /** The number of CPU clock ticks per second (TMCLOCK_TSC).
440 * Config variable: TSCTicksPerSecond (64-bit unsigned int)
441 * The config variable implies @c enmTSCMode would be
442 * TMTSCMODE_VIRT_TSC_EMULATED. */
443 uint64_t cTSCTicksPerSecond;
444 /** The TSC difference introduced by pausing the VM. */
445 uint64_t offTSCPause;
446 /** The TSC value when the last TSC was paused. */
447 uint64_t u64LastPausedTSC;
448 /** CPU TSCs ticking indicator (one for each VCPU). */
449 uint32_t volatile cTSCsTicking;
450
451 /** Virtual time ticking enabled indicator (counter for each VCPU). (TMCLOCK_VIRTUAL) */
452 uint32_t volatile cVirtualTicking;
453 /** Virtual time is not running at 100%. */
454 bool fVirtualWarpDrive;
455 /** Virtual timer synchronous time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL_SYNC) */
456 bool volatile fVirtualSyncTicking;
457 /** Virtual timer synchronous time catch-up active. */
458 bool volatile fVirtualSyncCatchUp;
459 /** Alignment padding. */
460 bool afAlignment1[1];
461 /** WarpDrive percentage.
462 * 100% is normal (fVirtualSyncNormal == true). When other than 100% we apply
463 * this percentage to the raw time source for the period it's been valid in,
464 * i.e. since u64VirtualWarpDriveStart. */
465 uint32_t u32VirtualWarpDrivePercentage;
466
467 /** The offset of the virtual clock relative to it's timesource.
468 * Only valid if fVirtualTicking is set. */
469 uint64_t u64VirtualOffset;
470 /** The guest virtual time when fVirtualTicking is cleared. */
471 uint64_t u64Virtual;
472 /** When the Warp drive was started or last adjusted.
473 * Only valid when fVirtualWarpDrive is set. */
474 uint64_t u64VirtualWarpDriveStart;
475 /** The previously returned nano TS.
476 * This handles TSC drift on SMP systems and expired interval.
477 * This is a valid range u64NanoTS to u64NanoTS + 1000000000 (ie. 1sec). */
478 uint64_t volatile u64VirtualRawPrev;
479 /** The ring-3 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
480 RTTIMENANOTSDATAR3 VirtualGetRawDataR3;
481 /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
482 RTTIMENANOTSDATAR0 VirtualGetRawDataR0;
483 /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
484 RTTIMENANOTSDATARC VirtualGetRawDataRC;
485 /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
486 R3PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR3;
487 /** Pointer to the ring-0 tmVirtualGetRawNanoTS worker function. */
488 R0PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR0;
489 /** Pointer to the raw-mode tmVirtualGetRawNanoTS worker function. */
490 RCPTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawRC;
491 /** Alignment. */
492 RTRCPTR AlignmentRCPtr;
493 /** The guest virtual timer synchronous time when fVirtualSyncTicking is cleared.
494 * When fVirtualSyncTicking is set it holds the last time returned to
495 * the guest (while the lock was held). */
496 uint64_t volatile u64VirtualSync;
497 /** The offset of the timer synchronous virtual clock (TMCLOCK_VIRTUAL_SYNC) relative
498 * to the virtual clock (TMCLOCK_VIRTUAL).
499 * (This is accessed by the timer thread and must be updated atomically.) */
500 uint64_t volatile offVirtualSync;
501 /** The offset into offVirtualSync that's been irrevocably given up by failed catch-up attempts.
502 * Thus the current lag is offVirtualSync - offVirtualSyncGivenUp. */
503 uint64_t offVirtualSyncGivenUp;
504 /** The TMCLOCK_VIRTUAL at the previous TMVirtualGetSync call when catch-up is active. */
505 uint64_t volatile u64VirtualSyncCatchUpPrev;
506 /** The current catch-up percentage. */
507 uint32_t volatile u32VirtualSyncCatchUpPercentage;
508 /** How much slack when processing timers. */
509 uint32_t u32VirtualSyncScheduleSlack;
510 /** When to stop catch-up. */
511 uint64_t u64VirtualSyncCatchUpStopThreshold;
512 /** When to give up catch-up. */
513 uint64_t u64VirtualSyncCatchUpGiveUpThreshold;
514/** @def TM_MAX_CATCHUP_PERIODS
515 * The number of catchup rates. */
516#define TM_MAX_CATCHUP_PERIODS 10
517 /** The aggressiveness of the catch-up relative to how far we've lagged behind.
518 * The idea is to have increasing catch-up percentage as the lag increases. */
519 struct TMCATCHUPPERIOD
520 {
521 uint64_t u64Start; /**< When this period starts. (u64VirtualSyncOffset). */
522 uint32_t u32Percentage; /**< The catch-up percent to apply. */
523 uint32_t u32Alignment; /**< Structure alignment */
524 } aVirtualSyncCatchUpPeriods[TM_MAX_CATCHUP_PERIODS];
525
526 union
527 {
528 /** Combined value for updating. */
529 uint64_t volatile u64Combined;
530 struct
531 {
532 /** Bitmap indicating which timer queues needs their uMaxHzHint updated. */
533 uint32_t volatile bmNeedsUpdating;
534 /** The current max timer Hz hint. */
535 uint32_t volatile uMax;
536 } s;
537 } HzHint;
538 /** @cfgm{/TM/HostHzMax, uint32_t, Hz, 0, UINT32_MAX, 20000}
539 * The max host Hz frequency hint returned by TMCalcHostTimerFrequency. */
540 uint32_t cHostHzMax;
541 /** @cfgm{/TM/HostHzFudgeFactorTimerCpu, uint32_t, Hz, 0, UINT32_MAX, 111}
542 * The number of Hz TMCalcHostTimerFrequency adds for the timer CPU. */
543 uint32_t cPctHostHzFudgeFactorTimerCpu;
544 /** @cfgm{/TM/HostHzFudgeFactorOtherCpu, uint32_t, Hz, 0, UINT32_MAX, 110}
545 * The number of Hz TMCalcHostTimerFrequency adds for the other CPUs. */
546 uint32_t cPctHostHzFudgeFactorOtherCpu;
547 /** @cfgm{/TM/HostHzFudgeFactorCatchUp100, uint32_t, Hz, 0, UINT32_MAX, 300}
548 * The fudge factor (expressed in percent) that catch-up percentages below
549 * 100% is multiplied by. */
550 uint32_t cPctHostHzFudgeFactorCatchUp100;
551 /** @cfgm{/TM/HostHzFudgeFactorCatchUp200, uint32_t, Hz, 0, UINT32_MAX, 250}
552 * The fudge factor (expressed in percent) that catch-up percentages
553 * 100%-199% is multiplied by. */
554 uint32_t cPctHostHzFudgeFactorCatchUp200;
555 /** @cfgm{/TM/HostHzFudgeFactorCatchUp400, uint32_t, Hz, 0, UINT32_MAX, 200}
556 * The fudge factor (expressed in percent) that catch-up percentages
557 * 200%-399% is multiplied by. */
558 uint32_t cPctHostHzFudgeFactorCatchUp400;
559
560 /** The UTC offset in ns.
561 * This is *NOT* for converting UTC to local time. It is for converting real
562 * world UTC time to VM UTC time. This feature is indented for doing date
563 * testing of software and similar.
564 * @todo Implement warpdrive on UTC. */
565 int64_t offUTC;
566 /** The last value TMR3UtcNow returned. */
567 int64_t volatile nsLastUtcNow;
568 /** File to touch on UTC jump. */
569 R3PTRTYPE(char *) pszUtcTouchFileOnJump;
570 /** Just to avoid dealing with 32-bit alignment trouble. */
571 R3PTRTYPE(char *) pszAlignment2b;
572
573 /** Pointer to our RC mapping of the GIP. */
574 RCPTRTYPE(void *) pvGIPRC;
575 /** Pointer to our R3 mapping of the GIP. */
576 R3PTRTYPE(void *) pvGIPR3;
577
578
579 /** The schedule timer timer handle (runtime timer).
580 * This timer will do frequent check on pending queue schedules and
581 * raise VM_FF_TIMER to pull EMTs attention to them.
582 */
583 R3PTRTYPE(PRTTIMER) pTimer;
584 /** Interval in milliseconds of the pTimer timer. */
585 uint32_t u32TimerMillies;
586
587 /** Indicates that queues are being run. */
588 bool volatile fRunningQueues;
589 /** Indicates that the virtual sync queue is being run. */
590 bool volatile fRunningVirtualSyncQueue;
591 /** Alignment */
592 bool afAlignment3[2];
593
594 /** Lock serializing access to the VirtualSync clock and the associated
595 * timer queue. */
596 PDMCRITSECT VirtualSyncLock;
597
598 /** CPU load state for all the virtual CPUs (tmR3CpuLoadTimer). */
599 TMCPULOADSTATE CpuLoad;
600
601 /** TMR3TimerQueuesDo
602 * @{ */
603 STAMPROFILE StatDoQueues;
604 /** @} */
605 /** tmSchedule
606 * @{ */
607 STAMPROFILE StatScheduleOneRZ;
608 STAMPROFILE StatScheduleOneR3;
609 STAMCOUNTER StatScheduleSetFF;
610 STAMCOUNTER StatPostponedR3;
611 STAMCOUNTER StatPostponedRZ;
612 /** @} */
613 /** Read the time
614 * @{ */
615 STAMCOUNTER StatVirtualGet;
616 STAMCOUNTER StatVirtualGetSetFF;
617 STAMCOUNTER StatVirtualSyncGet;
618 STAMCOUNTER StatVirtualSyncGetAdjLast;
619 STAMCOUNTER StatVirtualSyncGetELoop;
620 STAMCOUNTER StatVirtualSyncGetExpired;
621 STAMCOUNTER StatVirtualSyncGetLockless;
622 STAMCOUNTER StatVirtualSyncGetLocked;
623 STAMCOUNTER StatVirtualSyncGetSetFF;
624 STAMCOUNTER StatVirtualPause;
625 STAMCOUNTER StatVirtualResume;
626 /** @} */
627 /** TMTimerPoll
628 * @{ */
629 STAMCOUNTER StatPoll;
630 STAMCOUNTER StatPollAlreadySet;
631 STAMCOUNTER StatPollELoop;
632 STAMCOUNTER StatPollMiss;
633 STAMCOUNTER StatPollRunning;
634 STAMCOUNTER StatPollSimple;
635 STAMCOUNTER StatPollVirtual;
636 STAMCOUNTER StatPollVirtualSync;
637 /** @} */
638 /** TMTimerSet sans virtual sync timers.
639 * @{ */
640 STAMCOUNTER StatTimerSet;
641 STAMCOUNTER StatTimerSetOpt;
642 STAMPROFILE StatTimerSetRZ;
643 STAMPROFILE StatTimerSetR3;
644 STAMCOUNTER StatTimerSetStStopped;
645 STAMCOUNTER StatTimerSetStExpDeliver;
646 STAMCOUNTER StatTimerSetStActive;
647 STAMCOUNTER StatTimerSetStPendStop;
648 STAMCOUNTER StatTimerSetStPendStopSched;
649 STAMCOUNTER StatTimerSetStPendSched;
650 STAMCOUNTER StatTimerSetStPendResched;
651 STAMCOUNTER StatTimerSetStOther;
652 /** @} */
653 /** TMTimerSet on virtual sync timers.
654 * @{ */
655 STAMCOUNTER StatTimerSetVs;
656 STAMPROFILE StatTimerSetVsRZ;
657 STAMPROFILE StatTimerSetVsR3;
658 STAMCOUNTER StatTimerSetVsStStopped;
659 STAMCOUNTER StatTimerSetVsStExpDeliver;
660 STAMCOUNTER StatTimerSetVsStActive;
661 /** @} */
662 /** TMTimerSetRelative sans virtual sync timers
663 * @{ */
664 STAMCOUNTER StatTimerSetRelative;
665 STAMPROFILE StatTimerSetRelativeRZ;
666 STAMPROFILE StatTimerSetRelativeR3;
667 STAMCOUNTER StatTimerSetRelativeOpt;
668 STAMCOUNTER StatTimerSetRelativeStStopped;
669 STAMCOUNTER StatTimerSetRelativeStExpDeliver;
670 STAMCOUNTER StatTimerSetRelativeStActive;
671 STAMCOUNTER StatTimerSetRelativeStPendStop;
672 STAMCOUNTER StatTimerSetRelativeStPendStopSched;
673 STAMCOUNTER StatTimerSetRelativeStPendSched;
674 STAMCOUNTER StatTimerSetRelativeStPendResched;
675 STAMCOUNTER StatTimerSetRelativeStOther;
676 /** @} */
677 /** TMTimerSetRelative on virtual sync timers.
678 * @{ */
679 STAMCOUNTER StatTimerSetRelativeVs;
680 STAMPROFILE StatTimerSetRelativeVsRZ;
681 STAMPROFILE StatTimerSetRelativeVsR3;
682 STAMCOUNTER StatTimerSetRelativeVsStStopped;
683 STAMCOUNTER StatTimerSetRelativeVsStExpDeliver;
684 STAMCOUNTER StatTimerSetRelativeVsStActive;
685 /** @} */
686 /** TMTimerStop sans virtual sync.
687 * @{ */
688 STAMPROFILE StatTimerStopRZ;
689 STAMPROFILE StatTimerStopR3;
690 /** @} */
691 /** TMTimerStop on virtual sync timers.
692 * @{ */
693 STAMPROFILE StatTimerStopVsRZ;
694 STAMPROFILE StatTimerStopVsR3;
695 /** @} */
696 /** VirtualSync - Running and Catching Up
697 * @{ */
698 STAMCOUNTER StatVirtualSyncRun;
699 STAMCOUNTER StatVirtualSyncRunRestart;
700 STAMPROFILE StatVirtualSyncRunSlack;
701 STAMCOUNTER StatVirtualSyncRunStop;
702 STAMCOUNTER StatVirtualSyncRunStoppedAlready;
703 STAMCOUNTER StatVirtualSyncGiveUp;
704 STAMCOUNTER StatVirtualSyncGiveUpBeforeStarting;
705 STAMPROFILEADV StatVirtualSyncCatchup;
706 STAMCOUNTER aStatVirtualSyncCatchupInitial[TM_MAX_CATCHUP_PERIODS];
707 STAMCOUNTER aStatVirtualSyncCatchupAdjust[TM_MAX_CATCHUP_PERIODS];
708 /** @} */
709 /** TMR3VirtualSyncFF (non dedicated EMT). */
710 STAMPROFILE StatVirtualSyncFF;
711 /** The timer callback. */
712 STAMCOUNTER StatTimerCallbackSetFF;
713 STAMCOUNTER StatTimerCallback;
714
715 /** Calls to TMCpuTickSet. */
716 STAMCOUNTER StatTSCSet;
717
718 /** TSC starts and stops. */
719 STAMCOUNTER StatTSCPause;
720 STAMCOUNTER StatTSCResume;
721
722 /** @name Reasons for refusing TSC offsetting in TMCpuTickCanUseRealTSC.
723 * @{ */
724 STAMCOUNTER StatTSCNotFixed;
725 STAMCOUNTER StatTSCNotTicking;
726 STAMCOUNTER StatTSCCatchupLE010;
727 STAMCOUNTER StatTSCCatchupLE025;
728 STAMCOUNTER StatTSCCatchupLE100;
729 STAMCOUNTER StatTSCCatchupOther;
730 STAMCOUNTER StatTSCWarp;
731 STAMCOUNTER StatTSCUnderflow;
732 STAMCOUNTER StatTSCSyncNotTicking;
733 /** @} */
734} TM;
735/** Pointer to TM VM instance data. */
736typedef TM *PTM;
737
738
739/**
740 * TM VMCPU Instance data.
741 * Changes to this must checked against the padding of the tm union in VM!
742 */
743typedef struct TMCPU
744{
745 /** The offset between the host tick (TSC/virtual depending on the TSC mode) and
746 * the guest tick. */
747 uint64_t offTSCRawSrc;
748 /** The guest TSC when fTicking is cleared. */
749 uint64_t u64TSC;
750 /** The last seen TSC by the guest. */
751 uint64_t u64TSCLastSeen;
752 /** CPU timestamp ticking enabled indicator (bool). (RDTSC) */
753 bool fTSCTicking;
754#ifdef VBOX_WITHOUT_NS_ACCOUNTING
755 bool afAlignment1[7]; /**< alignment padding */
756#else /* !VBOX_WITHOUT_NS_ACCOUNTING */
757
758 /** Set by the timer callback to trigger updating of statistics in
759 * TMNotifyEndOfExecution. */
760 bool volatile fUpdateStats;
761 bool afAlignment1[6];
762 /** The time not spent executing or halted.
763 * @note Only updated after halting and after the timer runs. */
764 uint64_t cNsOtherStat;
765 /** Reasonably up to date total run time value.
766 * @note Only updated after halting and after the timer runs. */
767 uint64_t cNsTotalStat;
768# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
769 /** Resettable copy of version of cNsOtherStat.
770 * @note Only updated after halting. */
771 STAMCOUNTER StatNsOther;
772 /** Resettable copy of cNsTotalStat.
773 * @note Only updated after halting. */
774 STAMCOUNTER StatNsTotal;
775# else
776 uint64_t auAlignment2[2];
777# endif
778
779 /** @name Core accounting data.
780 * @note Must be cache-line aligned and only written to by the EMT owning it.
781 * @{ */
782 /** The cNsXXX generation. */
783 uint32_t volatile uTimesGen;
784 /** Set if executing (between TMNotifyStartOfExecution and
785 * TMNotifyEndOfExecution). */
786 bool volatile fExecuting;
787 /** Set if halting (between TMNotifyStartOfHalt and TMNotifyEndOfHalt). */
788 bool volatile fHalting;
789 /** Set if we're suspended and u64NsTsStartTotal is to be cNsTotal. */
790 bool volatile fSuspended;
791 bool afAlignment;
792 /** The nanosecond timestamp of the CPU start or resume.
793 * This is recalculated when the VM is started so that
794 * cNsTotal = RTTimeNanoTS() - u64NsTsStartCpu. */
795 uint64_t nsStartTotal;
796 /** The TSC of the last start-execute notification. */
797 uint64_t uTscStartExecuting;
798 /** The number of nanoseconds spent executing. */
799 uint64_t cNsExecuting;
800 /** The number of guest execution runs. */
801 uint64_t cPeriodsExecuting;
802 /** The nanosecond timestamp of the last start-halt notification. */
803 uint64_t nsStartHalting;
804 /** The number of nanoseconds being halted. */
805 uint64_t cNsHalted;
806 /** The number of halts. */
807 uint64_t cPeriodsHalted;
808 /** @} */
809
810# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
811 /** Resettable version of cNsExecuting. */
812 STAMPROFILE StatNsExecuting;
813 /** Long execution intervals. */
814 STAMPROFILE StatNsExecLong;
815 /** Short execution intervals. */
816 STAMPROFILE StatNsExecShort;
817 /** Tiny execution intervals. */
818 STAMPROFILE StatNsExecTiny;
819 /** Resettable version of cNsHalted. */
820 STAMPROFILE StatNsHalted;
821# endif
822
823 /** CPU load state for this virtual CPU (tmR3CpuLoadTimer). */
824 TMCPULOADSTATE CpuLoad;
825#endif
826} TMCPU;
827#ifndef VBOX_WITHOUT_NS_ACCOUNTING
828AssertCompileMemberAlignment(TMCPU, uTimesGen, 64);
829# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
830AssertCompileMemberAlignment(TMCPU, StatNsExecuting, 64);
831# else
832AssertCompileMemberAlignment(TMCPU, CpuLoad, 64);
833# endif
834#endif
835/** Pointer to TM VMCPU instance data. */
836typedef TMCPU *PTMCPU;
837
838
839/**
840 * TM data kept in the ring-0 GVM.
841 */
842typedef struct TMR0PERVM
843{
844 /** Timer queues for the different clock types. */
845 TMTIMERQUEUER0 aTimerQueues[TMCLOCK_MAX];
846} TMR0PERVM;
847
848
849const char *tmTimerState(TMTIMERSTATE enmState);
850void tmTimerQueueSchedule(PVMCC pVM, PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue);
851#ifdef VBOX_STRICT
852void tmTimerQueuesSanityChecks(PVMCC pVM, const char *pszWhere);
853#endif
854
855uint64_t tmR3CpuTickGetRawVirtualNoCheck(PVM pVM);
856int tmCpuTickPause(PVMCPUCC pVCpu);
857int tmCpuTickPauseLocked(PVMCC pVM, PVMCPUCC pVCpu);
858int tmCpuTickResume(PVMCC pVM, PVMCPUCC pVCpu);
859int tmCpuTickResumeLocked(PVMCC pVM, PVMCPUCC pVCpu);
860
861int tmVirtualPauseLocked(PVMCC pVM);
862int tmVirtualResumeLocked(PVMCC pVM);
863DECLCALLBACK(DECLEXPORT(void)) tmVirtualNanoTSBad(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS,
864 uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
865DECLCALLBACK(DECLEXPORT(uint64_t)) tmVirtualNanoTSRediscover(PRTTIMENANOTSDATA pData, PRTITMENANOTSEXTRA pExtra);
866DECLCALLBACK(DECLEXPORT(uint64_t)) tmVirtualNanoTSBadCpuIndex(PRTTIMENANOTSDATA pData, PRTITMENANOTSEXTRA pExtra,
867 uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu);
868/** @} */
869
870RT_C_DECLS_END
871
872#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