VirtualBox

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

Last change on this file since 77481 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

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