VirtualBox

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

Last change on this file since 84246 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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