VirtualBox

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

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

VMM/TM: Moved the timers off the hyper heap. Replaced the relative offset addressing with queue array indexing. bugref:9943

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