VirtualBox

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

Last change on this file since 62477 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.8 KB
Line 
1/* $Id: timesup.cpp 62477 2016-07-22 18:27:37Z 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# ifndef IN_RC
135 AssertMsgFailed(("idApic=%#x iCpuSet=%#x iGipCpu=%#x\n", idApic, iCpuSet, iGipCpu));
136 return RTTimeSystemNanoTS();
137# else
138 RTAssertReleasePanic();
139 return 0;
140# endif
141}
142
143
144/**
145 * Fallback function.
146 */
147static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData)
148{
149 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
150 if ( pGip
151 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
152 && ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
153 || pGip->u32Mode == SUPGIPMODE_SYNC_TSC
154 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
155 return rtTimeNanoTSInternalRediscover(pData);
156 NOREF(pData);
157# ifndef IN_RC
158 return RTTimeSystemNanoTS();
159# else
160 RTAssertReleasePanic();
161 return 0;
162# endif
163}
164
165
166/**
167 * Called the first time somebody asks for the time or when the GIP
168 * is mapped/unmapped.
169 */
170static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData)
171{
172 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
173# ifdef IN_RC
174 uint32_t iWorker;
175# else
176 PFNTIMENANOTSINTERNAL pfnWorker;
177# endif
178 if ( pGip
179 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
180 && ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
181 || pGip->u32Mode == SUPGIPMODE_SYNC_TSC
182 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
183 {
184 if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
185 {
186# ifdef IN_RC
187 iWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
188 ? RTTIMENANO_WORKER_LFENCE_ASYNC
189 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
190 ? RTTIMENANO_WORKER_LFENCE_SYNC_INVAR_NO_DELTA
191 : RTTIMENANO_WORKER_LFENCE_SYNC_INVAR_WITH_DELTA;
192# elif defined(IN_RING0)
193 pfnWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
194 ? RTTimeNanoTSLFenceAsync
195 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
196 ? RTTimeNanoTSLFenceSyncInvarNoDelta
197 : RTTimeNanoTSLFenceSyncInvarWithDelta;
198# else
199 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
200 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
201 ? RTTimeNanoTSLFenceAsyncUseIdtrLim
202 : pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
203 ? RTTimeNanoTSLFenceAsyncUseRdtscp
204 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
205 ? RTTimeNanoTSLFenceAsyncUseApicId
206 : rtTimeNanoTSInternalFallback;
207 else
208 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
209 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
210 ? RTTimeNanoTSLFenceSyncInvarNoDelta
211 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim
212 : pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
213 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
214 ? RTTimeNanoTSLFenceSyncInvarNoDelta
215 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp
216 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
217 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
218 ? RTTimeNanoTSLFenceSyncInvarNoDelta
219 : RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId
220 : rtTimeNanoTSInternalFallback;
221# endif
222 }
223 else
224 {
225# ifdef IN_RC
226 iWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
227 ? RTTIMENANO_WORKER_LEGACY_ASYNC
228 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
229 ? RTTIMENANO_WORKER_LEGACY_SYNC_INVAR_NO_DELTA : RTTIMENANO_WORKER_LEGACY_SYNC_INVAR_WITH_DELTA;
230# elif defined(IN_RING0)
231 pfnWorker = pGip->u32Mode == SUPGIPMODE_ASYNC_TSC
232 ? RTTimeNanoTSLegacyAsync
233 : pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
234 ? RTTimeNanoTSLegacySyncInvarNoDelta
235 : RTTimeNanoTSLegacySyncInvarWithDelta;
236# else
237 if (pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
238 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
239 ? RTTimeNanoTSLegacyAsyncUseRdtscp
240 : pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
241 ? RTTimeNanoTSLegacyAsyncUseIdtrLim
242 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
243 ? RTTimeNanoTSLegacyAsyncUseApicId
244 : rtTimeNanoTSInternalFallback;
245 else
246 pfnWorker = pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
247 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
248 ? RTTimeNanoTSLegacySyncInvarNoDelta
249 : RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp
250 : pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
251 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_PRACTICALLY_ZERO
252 ? RTTimeNanoTSLegacySyncInvarNoDelta
253 : RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim
254 : pGip->fGetGipCpu & SUPGIPGETCPU_APIC_ID
255 ? pGip->enmUseTscDelta <= SUPGIPUSETSCDELTA_ROUGHLY_ZERO
256 ? RTTimeNanoTSLegacySyncInvarNoDelta
257 : RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId
258 : rtTimeNanoTSInternalFallback;
259# endif
260 }
261 }
262 else
263# ifdef IN_RC
264 iWorker = RTTIMENANO_WORKER_FALLBACK;
265# else
266 pfnWorker = rtTimeNanoTSInternalFallback;
267# endif
268
269# ifdef IN_RC
270 ASMAtomicWriteU32((uint32_t volatile *)&g_iWorker, iWorker);
271 return g_apfnWorkers[iWorker](pData);
272# else
273 ASMAtomicWritePtr((void * volatile *)&g_pfnWorker, (void *)(uintptr_t)pfnWorker);
274 return pfnWorker(pData);
275# endif
276}
277
278#endif /* !IN_GUEST && !RT_NO_GIP */
279
280
281/**
282 * Internal worker for getting the current nanosecond timestamp.
283 */
284DECLINLINE(uint64_t) rtTimeNanoTSInternal(void)
285{
286#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
287# ifdef IN_RC
288 return g_apfnWorkers[g_iWorker](&g_TimeNanoTSData);
289# else
290 return g_pfnWorker(&g_TimeNanoTSData);
291# endif
292#else
293 return RTTimeSystemNanoTS();
294#endif
295}
296
297
298/**
299 * Gets the current nanosecond timestamp.
300 *
301 * @returns nanosecond timestamp.
302 */
303RTDECL(uint64_t) RTTimeNanoTS(void)
304{
305 return rtTimeNanoTSInternal();
306}
307RT_EXPORT_SYMBOL(RTTimeNanoTS);
308
309
310/**
311 * Gets the current millisecond timestamp.
312 *
313 * @returns millisecond timestamp.
314 */
315RTDECL(uint64_t) RTTimeMilliTS(void)
316{
317 return rtTimeNanoTSInternal() / 1000000;
318}
319RT_EXPORT_SYMBOL(RTTimeMilliTS);
320
321
322#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
323/**
324 * Debugging the time api.
325 *
326 * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
327 */
328RTDECL(uint32_t) RTTimeDbgSteps(void)
329{
330 return g_TimeNanoTSData.c1nsSteps;
331}
332RT_EXPORT_SYMBOL(RTTimeDbgSteps);
333
334
335/**
336 * Debugging the time api.
337 *
338 * @returns the number of times the TSC interval expired RTTimeNanoTS().
339 */
340RTDECL(uint32_t) RTTimeDbgExpired(void)
341{
342 return g_TimeNanoTSData.cExpired;
343}
344RT_EXPORT_SYMBOL(RTTimeDbgExpired);
345
346
347/**
348 * Debugging the time api.
349 *
350 * @returns the number of bad previous values encountered by RTTimeNanoTS().
351 */
352RTDECL(uint32_t) RTTimeDbgBad(void)
353{
354 return g_TimeNanoTSData.cBadPrev;
355}
356RT_EXPORT_SYMBOL(RTTimeDbgBad);
357
358
359/**
360 * Debugging the time api.
361 *
362 * @returns the number of update races in RTTimeNanoTS().
363 */
364RTDECL(uint32_t) RTTimeDbgRaces(void)
365{
366 return g_TimeNanoTSData.cUpdateRaces;
367}
368RT_EXPORT_SYMBOL(RTTimeDbgRaces);
369#endif /* !IN_GUEST && !RT_NO_GIP */
370
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