VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/time/timesup.cpp@ 69474

Last change on this file since 69474 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 14.2 KB
Line 
1/* $Id: timesup.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT - Time using SUPLib.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_TIME
32#include <iprt/time.h>
33#include "internal/iprt.h"
34
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/log.h>
38#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
39# include <iprt/asm.h>
40# include <iprt/asm-amd64-x86.h>
41# include <iprt/x86.h>
42# include <VBox/sup.h>
43#endif
44#include "internal/time.h"
45
46
47/*********************************************************************************************************************************
48* Internal Functions *
49*********************************************************************************************************************************/
50#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
51static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
52static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData);
53static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData);
54static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalBadCpuIndex(PRTTIMENANOTSDATA pData, uint16_t idApic, uint16_t iCpuSet, uint16_t iGipCpu);
55#endif
56
57
58/*********************************************************************************************************************************
59* Global Variables *
60*********************************************************************************************************************************/
61#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
62/** The previous timestamp value returned by RTTimeNanoTS. */
63static uint64_t g_TimeNanoTSPrev = 0;
64
65/** The RTTimeNanoTS data structure that's passed down to the worker functions. */
66static RTTIMENANOTSDATA g_TimeNanoTSData =
67{
68 /* .pu64Prev = */ &g_TimeNanoTSPrev,
69 /* .pfnBad = */ rtTimeNanoTSInternalBitch,
70 /* .pfnRediscover = */ rtTimeNanoTSInternalRediscover,
71 /* .pfnBadCpuIndex = */ rtTimeNanoTSInternalBadCpuIndex,
72 /* .c1nsSteps = */ 0,
73 /* .cExpired = */ 0,
74 /* .cBadPrev = */ 0,
75 /* .cUpdateRaces = */ 0
76};
77
78# ifdef IN_RC
79/** Array of rtTimeNanoTSInternal worker functions.
80 * This array is indexed by g_iWorker. */
81static const PFNTIMENANOTSINTERNAL g_apfnWorkers[] =
82{
83# define RTTIMENANO_WORKER_DETECT 0
84 rtTimeNanoTSInternalRediscover,
85
86# define RTTIMENANO_WORKER_LEGACY_SYNC_INVAR_NO_DELTA 1
87 RTTimeNanoTSLegacySyncInvarNoDelta,
88# define RTTIMENANO_WORKER_LEGACY_SYNC_INVAR_WITH_DELTA 2
89 RTTimeNanoTSLegacySyncInvarWithDelta,
90# define RTTIMENANO_WORKER_LEGACY_ASYNC 3
91 RTTimeNanoTSLegacyAsync,
92
93# define RTTIMENANO_WORKER_LFENCE_SYNC_INVAR_NO_DELTA 4
94 RTTimeNanoTSLFenceSyncInvarNoDelta,
95# define RTTIMENANO_WORKER_LFENCE_SYNC_INVAR_WITH_DELTA 5
96 RTTimeNanoTSLFenceSyncInvarWithDelta,
97# define RTTIMENANO_WORKER_LFENCE_ASYNC 6
98 RTTimeNanoTSLFenceAsync,
99
100# define RTTIMENANO_WORKER_FALLBACK 7
101 rtTimeNanoTSInternalFallback,
102};
103/** The index into g_apfnWorkers for the function to use.
104 * @remarks This cannot be a pointer because that'll break down in RC due to
105 * code relocation. */
106static uint32_t g_iWorker = RTTIMENANO_WORKER_DETECT;
107# else
108/** Pointer to the worker */
109static PFNTIMENANOTSINTERNAL g_pfnWorker = rtTimeNanoTSInternalRediscover;
110# endif /* IN_RC */
111
112
113/**
114 * @interface_method_impl{RTTIMENANOTSDATA,pfnBad}
115 */
116static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev,
117 uint64_t u64PrevNanoTS)
118{
119 pData->cBadPrev++;
120 if ((int64_t)u64DeltaPrev < 0)
121 LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64\n",
122 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
123 else
124 Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 (debugging?)\n",
125 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
126}
127
128/**
129 * @interface_method_impl{RTTIMENANOTSDATA,pfnBadCpuIndex}
130 */
131static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalBadCpuIndex(PRTTIMENANOTSDATA pData, uint16_t idApic,
132 uint16_t iCpuSet, uint16_t iGipCpu)
133{
134 RT_NOREF_PV(pData); RT_NOREF_PV(idApic); RT_NOREF_PV(iCpuSet); RT_NOREF_PV(iGipCpu);
135# ifndef IN_RC
136 AssertMsgFailed(("idApic=%#x iCpuSet=%#x iGipCpu=%#x\n", idApic, iCpuSet, iGipCpu));
137 return RTTimeSystemNanoTS();
138# else
139 RTAssertReleasePanic();
140 return 0;
141# endif
142}
143
144
145/**
146 * Fallback function.
147 */
148static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData)
149{
150 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
151 if ( pGip
152 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
153 && ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
154 || pGip->u32Mode == SUPGIPMODE_SYNC_TSC
155 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
156 return rtTimeNanoTSInternalRediscover(pData);
157 NOREF(pData);
158# ifndef IN_RC
159 return RTTimeSystemNanoTS();
160# else
161 RTAssertReleasePanic();
162 return 0;
163# endif
164}
165
166
167/**
168 * Called the first time somebody asks for the time or when the GIP
169 * is mapped/unmapped.
170 */
171static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData)
172{
173 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
174# ifdef IN_RC
175 uint32_t iWorker;
176# else
177 PFNTIMENANOTSINTERNAL pfnWorker;
178# endif
179 if ( pGip
180 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
181 && ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
182 || pGip->u32Mode == SUPGIPMODE_SYNC_TSC
183 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
184 {
185 if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
186 {
187# ifdef IN_RC
188 iWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
189 ? RTTIMENANO_WORKER_LFENCE_ASYNC
190 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
191 ? RTTIMENANO_WORKER_LFENCE_SYNC_INVAR_NO_DELTA
192 : RTTIMENANO_WORKER_LFENCE_SYNC_INVAR_WITH_DELTA;
193# elif defined(IN_RING0)
194 pfnWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
195 ? RTTimeNanoTSLFenceAsync
196 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
197 ? RTTimeNanoTSLFenceSyncInvarNoDelta
198 : RTTimeNanoTSLFenceSyncInvarWithDelta;
199# else
200 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
201 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
202 ? RTTimeNanoTSLFenceAsyncUseIdtrLim
203 : pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
204 ? RTTimeNanoTSLFenceAsyncUseRdtscp
205 : pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_GROUP_IN_CH_NUMBER_IN_CL
206 ? RTTimeNanoTSLFenceAsyncUseRdtscpGroupChNumCl
207 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
208 ? RTTimeNanoTSLFenceAsyncUseApicId
209 : rtTimeNanoTSInternalFallback;
210 else
211 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
212 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
213 ? RTTimeNanoTSLFenceSyncInvarNoDelta
214 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim
215 : pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
216 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
217 ? RTTimeNanoTSLFenceSyncInvarNoDelta
218 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp
219 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
220 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
221 ? RTTimeNanoTSLFenceSyncInvarNoDelta
222 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId
223 : rtTimeNanoTSInternalFallback;
224# endif
225 }
226 else
227 {
228# ifdef IN_RC
229 iWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
230 ? RTTIMENANO_WORKER_LEGACY_ASYNC
231 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
232 ? RTTIMENANO_WORKER_LEGACY_SYNC_INVAR_NO_DELTA : RTTIMENANO_WORKER_LEGACY_SYNC_INVAR_WITH_DELTA;
233# elif defined(IN_RING0)
234 pfnWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
235 ? RTTimeNanoTSLegacyAsync
236 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
237 ? RTTimeNanoTSLegacySyncInvarNoDelta
238 : RTTimeNanoTSLegacySyncInvarWithDelta;
239# else
240 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
241 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
242 ? RTTimeNanoTSLegacyAsyncUseRdtscp
243 : pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_GROUP_IN_CH_NUMBER_IN_CL
244 ? RTTimeNanoTSLegacyAsyncUseRdtscpGroupChNumCl
245 : pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
246 ? RTTimeNanoTSLegacyAsyncUseIdtrLim
247 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
248 ? RTTimeNanoTSLegacyAsyncUseApicId
249 : rtTimeNanoTSInternalFallback;
250 else
251 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
252 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
253 ? RTTimeNanoTSLegacySyncInvarNoDelta
254 : RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp
255 : pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
256 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
257 ? RTTimeNanoTSLegacySyncInvarNoDelta
258 : RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim
259 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
260 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
261 ? RTTimeNanoTSLegacySyncInvarNoDelta
262 : RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId
263 : rtTimeNanoTSInternalFallback;
264# endif
265 }
266 }
267 else
268# ifdef IN_RC
269 iWorker = RTTIMENANO_WORKER_FALLBACK;
270# else
271 pfnWorker = rtTimeNanoTSInternalFallback;
272# endif
273
274# ifdef IN_RC
275 ASMAtomicWriteU32((uint32_t volatile *)&g_iWorker, iWorker);
276 return g_apfnWorkers[iWorker](pData);
277# else
278 ASMAtomicWritePtr((void * volatile *)&g_pfnWorker, (void *)(uintptr_t)pfnWorker);
279 return pfnWorker(pData);
280# endif
281}
282
283#endif /* !IN_GUEST && !RT_NO_GIP */
284
285
286/**
287 * Internal worker for getting the current nanosecond timestamp.
288 */
289DECLINLINE(uint64_t) rtTimeNanoTSInternal(void)
290{
291#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
292# ifdef IN_RC
293 return g_apfnWorkers[g_iWorker](&g_TimeNanoTSData);
294# else
295 return g_pfnWorker(&g_TimeNanoTSData);
296# endif
297#else
298 return RTTimeSystemNanoTS();
299#endif
300}
301
302
303/**
304 * Gets the current nanosecond timestamp.
305 *
306 * @returns nanosecond timestamp.
307 */
308RTDECL(uint64_t) RTTimeNanoTS(void)
309{
310 return rtTimeNanoTSInternal();
311}
312RT_EXPORT_SYMBOL(RTTimeNanoTS);
313
314
315/**
316 * Gets the current millisecond timestamp.
317 *
318 * @returns millisecond timestamp.
319 */
320RTDECL(uint64_t) RTTimeMilliTS(void)
321{
322 return rtTimeNanoTSInternal() / 1000000;
323}
324RT_EXPORT_SYMBOL(RTTimeMilliTS);
325
326
327#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
328/**
329 * Debugging the time api.
330 *
331 * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
332 */
333RTDECL(uint32_t) RTTimeDbgSteps(void)
334{
335 return g_TimeNanoTSData.c1nsSteps;
336}
337RT_EXPORT_SYMBOL(RTTimeDbgSteps);
338
339
340/**
341 * Debugging the time api.
342 *
343 * @returns the number of times the TSC interval expired RTTimeNanoTS().
344 */
345RTDECL(uint32_t) RTTimeDbgExpired(void)
346{
347 return g_TimeNanoTSData.cExpired;
348}
349RT_EXPORT_SYMBOL(RTTimeDbgExpired);
350
351
352/**
353 * Debugging the time api.
354 *
355 * @returns the number of bad previous values encountered by RTTimeNanoTS().
356 */
357RTDECL(uint32_t) RTTimeDbgBad(void)
358{
359 return g_TimeNanoTSData.cBadPrev;
360}
361RT_EXPORT_SYMBOL(RTTimeDbgBad);
362
363
364/**
365 * Debugging the time api.
366 *
367 * @returns the number of update races in RTTimeNanoTS().
368 */
369RTDECL(uint32_t) RTTimeDbgRaces(void)
370{
371 return g_TimeNanoTSData.cUpdateRaces;
372}
373RT_EXPORT_SYMBOL(RTTimeDbgRaces);
374#endif /* !IN_GUEST && !RT_NO_GIP */
375
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