VirtualBox

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

Last change on this file since 80360 was 80281, checked in by vboxsync, 5 years ago

VMM,++: Refactoring code to use VMMC & VMMCPUCC. bugref:9217

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