VirtualBox

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

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

VMM/TM: Replaced the global timer active list lock with per queue locks. bugref:9943

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