VirtualBox

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

Last change on this file since 62556 was 62556, checked in by vboxsync, 9 years ago

IPRT: Unused parameters.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.9 KB
Line 
1/* $Id: timesup.cpp 62556 2016-07-26 10:20:00Z vboxsync $ */
2/** @file
3 * IPRT - Time using SUPLib.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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_APIC_ID
206 ? RTTimeNanoTSLFenceAsyncUseApicId
207 : rtTimeNanoTSInternalFallback;
208 else
209 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
210 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
211 ? RTTimeNanoTSLFenceSyncInvarNoDelta
212 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim
213 : pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
214 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
215 ? RTTimeNanoTSLFenceSyncInvarNoDelta
216 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp
217 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
218 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
219 ? RTTimeNanoTSLFenceSyncInvarNoDelta
220 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId
221 : rtTimeNanoTSInternalFallback;
222# endif
223 }
224 else
225 {
226# ifdef IN_RC
227 iWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
228 ? RTTIMENANO_WORKER_LEGACY_ASYNC
229 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
230 ? RTTIMENANO_WORKER_LEGACY_SYNC_INVAR_NO_DELTA : RTTIMENANO_WORKER_LEGACY_SYNC_INVAR_WITH_DELTA;
231# elif defined(IN_RING0)
232 pfnWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
233 ? RTTimeNanoTSLegacyAsync
234 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
235 ? RTTimeNanoTSLegacySyncInvarNoDelta
236 : RTTimeNanoTSLegacySyncInvarWithDelta;
237# else
238 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
239 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
240 ? RTTimeNanoTSLegacyAsyncUseRdtscp
241 : pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
242 ? RTTimeNanoTSLegacyAsyncUseIdtrLim
243 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
244 ? RTTimeNanoTSLegacyAsyncUseApicId
245 : rtTimeNanoTSInternalFallback;
246 else
247 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
248 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
249 ? RTTimeNanoTSLegacySyncInvarNoDelta
250 : RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp
251 : pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
252 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
253 ? RTTimeNanoTSLegacySyncInvarNoDelta
254 : RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim
255 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
256 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
257 ? RTTimeNanoTSLegacySyncInvarNoDelta
258 : RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId
259 : rtTimeNanoTSInternalFallback;
260# endif
261 }
262 }
263 else
264# ifdef IN_RC
265 iWorker = RTTIMENANO_WORKER_FALLBACK;
266# else
267 pfnWorker = rtTimeNanoTSInternalFallback;
268# endif
269
270# ifdef IN_RC
271 ASMAtomicWriteU32((uint32_t volatile *)&g_iWorker, iWorker);
272 return g_apfnWorkers[iWorker](pData);
273# else
274 ASMAtomicWritePtr((void * volatile *)&g_pfnWorker, (void *)(uintptr_t)pfnWorker);
275 return pfnWorker(pData);
276# endif
277}
278
279#endif /* !IN_GUEST && !RT_NO_GIP */
280
281
282/**
283 * Internal worker for getting the current nanosecond timestamp.
284 */
285DECLINLINE(uint64_t) rtTimeNanoTSInternal(void)
286{
287#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
288# ifdef IN_RC
289 return g_apfnWorkers[g_iWorker](&g_TimeNanoTSData);
290# else
291 return g_pfnWorker(&g_TimeNanoTSData);
292# endif
293#else
294 return RTTimeSystemNanoTS();
295#endif
296}
297
298
299/**
300 * Gets the current nanosecond timestamp.
301 *
302 * @returns nanosecond timestamp.
303 */
304RTDECL(uint64_t) RTTimeNanoTS(void)
305{
306 return rtTimeNanoTSInternal();
307}
308RT_EXPORT_SYMBOL(RTTimeNanoTS);
309
310
311/**
312 * Gets the current millisecond timestamp.
313 *
314 * @returns millisecond timestamp.
315 */
316RTDECL(uint64_t) RTTimeMilliTS(void)
317{
318 return rtTimeNanoTSInternal() / 1000000;
319}
320RT_EXPORT_SYMBOL(RTTimeMilliTS);
321
322
323#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
324/**
325 * Debugging the time api.
326 *
327 * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
328 */
329RTDECL(uint32_t) RTTimeDbgSteps(void)
330{
331 return g_TimeNanoTSData.c1nsSteps;
332}
333RT_EXPORT_SYMBOL(RTTimeDbgSteps);
334
335
336/**
337 * Debugging the time api.
338 *
339 * @returns the number of times the TSC interval expired RTTimeNanoTS().
340 */
341RTDECL(uint32_t) RTTimeDbgExpired(void)
342{
343 return g_TimeNanoTSData.cExpired;
344}
345RT_EXPORT_SYMBOL(RTTimeDbgExpired);
346
347
348/**
349 * Debugging the time api.
350 *
351 * @returns the number of bad previous values encountered by RTTimeNanoTS().
352 */
353RTDECL(uint32_t) RTTimeDbgBad(void)
354{
355 return g_TimeNanoTSData.cBadPrev;
356}
357RT_EXPORT_SYMBOL(RTTimeDbgBad);
358
359
360/**
361 * Debugging the time api.
362 *
363 * @returns the number of update races in RTTimeNanoTS().
364 */
365RTDECL(uint32_t) RTTimeDbgRaces(void)
366{
367 return g_TimeNanoTSData.cUpdateRaces;
368}
369RT_EXPORT_SYMBOL(RTTimeDbgRaces);
370#endif /* !IN_GUEST && !RT_NO_GIP */
371
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