1 | /* $Id: timesup.cpp 54202 2015-02-13 17:13:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time using SUPLib.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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)
|
---|
51 | static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
|
---|
52 | static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData);
|
---|
53 | static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData);
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 | /*******************************************************************************
|
---|
58 | * Global Variables *
|
---|
59 | *******************************************************************************/
|
---|
60 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
61 | /** The previous timestamp value returned by RTTimeNanoTS. */
|
---|
62 | static uint64_t g_TimeNanoTSPrev = 0;
|
---|
63 |
|
---|
64 | /** The RTTimeNanoTS data structure that's passed down to the worker functions. */
|
---|
65 | static RTTIMENANOTSDATA g_TimeNanoTSData =
|
---|
66 | {
|
---|
67 | /* .pu64Prev = */ &g_TimeNanoTSPrev,
|
---|
68 | /* .pfnBad = */ rtTimeNanoTSInternalBitch,
|
---|
69 | /* .pfnRediscover = */ rtTimeNanoTSInternalRediscover,
|
---|
70 | /* .pvDummy = */ NULL,
|
---|
71 | /* .c1nsSteps = */ 0,
|
---|
72 | /* .cExpired = */ 0,
|
---|
73 | /* .cBadPrev = */ 0,
|
---|
74 | /* .cUpdateRaces = */ 0
|
---|
75 | };
|
---|
76 |
|
---|
77 | /** The index into g_apfnWorkers for the function to use.
|
---|
78 | * This cannot be a pointer because that'll break down in GC due to code relocation. */
|
---|
79 | static uint32_t g_iWorker = 0;
|
---|
80 | /** Array of rtTimeNanoTSInternal worker functions.
|
---|
81 | * This array is indexed by g_iWorker. */
|
---|
82 | static const PFNTIMENANOTSINTERNAL g_apfnWorkers[] =
|
---|
83 | {
|
---|
84 | # define RTTIMENANO_WORKER_DETECT 0
|
---|
85 | rtTimeNanoTSInternalRediscover,
|
---|
86 |
|
---|
87 | # define RTTIMENANO_WORKER_LEGACY_SYNC_NO_DELTA 1
|
---|
88 | RTTimeNanoTSLegacySyncNoDelta,
|
---|
89 | # define RTTIMENANO_WORKER_LEGACY_SYNC_WITH_DELTA 2
|
---|
90 | RTTimeNanoTSLegacySyncWithDelta,
|
---|
91 | # define RTTIMENANO_WORKER_LEGACY_ASYNC 3
|
---|
92 | RTTimeNanoTSLegacyAsync,
|
---|
93 | # define RTTIMENANO_WORKER_LEGACY_INVAR_NO_DELTA 4
|
---|
94 | RTTimeNanoTSLFenceInvariantNoDelta,
|
---|
95 | # define RTTIMENANO_WORKER_LEGACY_INVAR_WITH_DELTA 5
|
---|
96 | RTTimeNanoTSLFenceInvariantWithDelta,
|
---|
97 |
|
---|
98 | # define RTTIMENANO_WORKER_LFENCE_SYNC_NO_DELTA 6
|
---|
99 | RTTimeNanoTSLFenceSyncNoDelta,
|
---|
100 | # define RTTIMENANO_WORKER_LFENCE_SYNC_WITH_DELTA 7
|
---|
101 | RTTimeNanoTSLFenceSyncWithDelta,
|
---|
102 | # define RTTIMENANO_WORKER_LFENCE_ASYNC 8
|
---|
103 | RTTimeNanoTSLFenceAsync,
|
---|
104 | # define RTTIMENANO_WORKER_LFENCE_INVAR_NO_DELTA 9
|
---|
105 | RTTimeNanoTSLFenceInvariantNoDelta,
|
---|
106 | # define RTTIMENANO_WORKER_LFENCE_INVAR_WITH_DELTA 10
|
---|
107 | RTTimeNanoTSLFenceInvariantWithDelta,
|
---|
108 |
|
---|
109 | # define RTTIMENANO_WORKER_FALLBACK 11
|
---|
110 | rtTimeNanoTSInternalFallback,
|
---|
111 | };
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Helper function that's used by the assembly routines when something goes bust.
|
---|
116 | *
|
---|
117 | * @param pData Pointer to the data structure.
|
---|
118 | * @param u64NanoTS The calculated nano ts.
|
---|
119 | * @param u64DeltaPrev The delta relative to the previously returned timestamp.
|
---|
120 | * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
|
---|
121 | */
|
---|
122 | static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS)
|
---|
123 | {
|
---|
124 | pData->cBadPrev++;
|
---|
125 | if ((int64_t)u64DeltaPrev < 0)
|
---|
126 | LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64\n",
|
---|
127 | u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
|
---|
128 | else
|
---|
129 | Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 (debugging?)\n",
|
---|
130 | u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Fallback function.
|
---|
135 | */
|
---|
136 | static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData)
|
---|
137 | {
|
---|
138 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
139 | if ( pGip
|
---|
140 | && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
|
---|
141 | && ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
|
---|
142 | || pGip->u32Mode == SUPGIPMODE_SYNC_TSC
|
---|
143 | || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
|
---|
144 | return rtTimeNanoTSInternalRediscover(pData);
|
---|
145 | NOREF(pData);
|
---|
146 | # ifndef IN_RC
|
---|
147 | return RTTimeSystemNanoTS();
|
---|
148 | # else
|
---|
149 | return 0;
|
---|
150 | # endif
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Checks if we really need to apply the delta values.
|
---|
156 | *
|
---|
157 | * Getting the delta for a CPU is _very_ expensive, it more than doubles the
|
---|
158 | * execution time for RTTimeNanoTS.
|
---|
159 | *
|
---|
160 | * @returns true if deltas needs to be applied, false if not.
|
---|
161 | * @param pGip The GIP.
|
---|
162 | */
|
---|
163 | static bool rtTimeNanoTsInternalReallyNeedDeltas(PSUPGLOBALINFOPAGE pGip)
|
---|
164 | {
|
---|
165 | if (!pGip->fOsTscDeltasInSync)
|
---|
166 | {
|
---|
167 | uint32_t i = pGip->cCpus;
|
---|
168 | while (i-- > 0)
|
---|
169 | if ( pGip->aCPUs[i].enmState == SUPGIPCPUSTATE_ONLINE
|
---|
170 | && ( pGip->aCPUs[i].i64TSCDelta > 384
|
---|
171 | || pGip->aCPUs[i].i64TSCDelta < -384) )
|
---|
172 | return true;
|
---|
173 | }
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Called the first time somebody asks for the time or when the GIP
|
---|
180 | * is mapped/unmapped.
|
---|
181 | */
|
---|
182 | static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData)
|
---|
183 | {
|
---|
184 | uint32_t iWorker;
|
---|
185 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
186 | if ( pGip
|
---|
187 | && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
|
---|
188 | && ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
|
---|
189 | || pGip->u32Mode == SUPGIPMODE_SYNC_TSC
|
---|
190 | || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
|
---|
191 | {
|
---|
192 | if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
|
---|
193 | iWorker = pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
|
---|
194 | ? rtTimeNanoTsInternalReallyNeedDeltas(pGip)
|
---|
195 | ? RTTIMENANO_WORKER_LFENCE_INVAR_WITH_DELTA : RTTIMENANO_WORKER_LFENCE_INVAR_NO_DELTA
|
---|
196 | : pGip->u32Mode == SUPGIPMODE_SYNC_TSC
|
---|
197 | ? false /** @todo !rtTimeNanoTsInternalReallyNeedDeltas(pGip) */
|
---|
198 | ? RTTIMENANO_WORKER_LFENCE_SYNC_WITH_DELTA : RTTIMENANO_WORKER_LFENCE_SYNC_NO_DELTA
|
---|
199 | : RTTIMENANO_WORKER_LFENCE_ASYNC;
|
---|
200 | else
|
---|
201 | iWorker = pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
|
---|
202 | ? rtTimeNanoTsInternalReallyNeedDeltas(pGip)
|
---|
203 | ? RTTIMENANO_WORKER_LEGACY_INVAR_WITH_DELTA : RTTIMENANO_WORKER_LEGACY_INVAR_NO_DELTA
|
---|
204 | : pGip->u32Mode == SUPGIPMODE_SYNC_TSC
|
---|
205 | ? false /** @todo rtTimeNanoTsInternalReallyNeedDeltas(pGip) */
|
---|
206 | ? RTTIMENANO_WORKER_LEGACY_SYNC_WITH_DELTA : RTTIMENANO_WORKER_LEGACY_SYNC_NO_DELTA
|
---|
207 | : RTTIMENANO_WORKER_LEGACY_ASYNC;
|
---|
208 | }
|
---|
209 | else
|
---|
210 | iWorker = RTTIMENANO_WORKER_FALLBACK;
|
---|
211 |
|
---|
212 | ASMAtomicWriteU32((uint32_t volatile *)&g_iWorker, iWorker);
|
---|
213 | return g_apfnWorkers[iWorker](pData);
|
---|
214 | }
|
---|
215 |
|
---|
216 | #endif /* !IN_GUEST && !RT_NO_GIP */
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Internal worker for getting the current nanosecond timestamp.
|
---|
221 | */
|
---|
222 | DECLINLINE(uint64_t) rtTimeNanoTSInternal(void)
|
---|
223 | {
|
---|
224 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
225 | return g_apfnWorkers[g_iWorker](&g_TimeNanoTSData);
|
---|
226 | #else
|
---|
227 | return RTTimeSystemNanoTS();
|
---|
228 | #endif
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Gets the current nanosecond timestamp.
|
---|
234 | *
|
---|
235 | * @returns nanosecond timestamp.
|
---|
236 | */
|
---|
237 | RTDECL(uint64_t) RTTimeNanoTS(void)
|
---|
238 | {
|
---|
239 | return rtTimeNanoTSInternal();
|
---|
240 | }
|
---|
241 | RT_EXPORT_SYMBOL(RTTimeNanoTS);
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Gets the current millisecond timestamp.
|
---|
246 | *
|
---|
247 | * @returns millisecond timestamp.
|
---|
248 | */
|
---|
249 | RTDECL(uint64_t) RTTimeMilliTS(void)
|
---|
250 | {
|
---|
251 | return rtTimeNanoTSInternal() / 1000000;
|
---|
252 | }
|
---|
253 | RT_EXPORT_SYMBOL(RTTimeMilliTS);
|
---|
254 |
|
---|
255 |
|
---|
256 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
257 | /**
|
---|
258 | * Debugging the time api.
|
---|
259 | *
|
---|
260 | * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
|
---|
261 | */
|
---|
262 | RTDECL(uint32_t) RTTimeDbgSteps(void)
|
---|
263 | {
|
---|
264 | return g_TimeNanoTSData.c1nsSteps;
|
---|
265 | }
|
---|
266 | RT_EXPORT_SYMBOL(RTTimeDbgSteps);
|
---|
267 |
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Debugging the time api.
|
---|
271 | *
|
---|
272 | * @returns the number of times the TSC interval expired RTTimeNanoTS().
|
---|
273 | */
|
---|
274 | RTDECL(uint32_t) RTTimeDbgExpired(void)
|
---|
275 | {
|
---|
276 | return g_TimeNanoTSData.cExpired;
|
---|
277 | }
|
---|
278 | RT_EXPORT_SYMBOL(RTTimeDbgExpired);
|
---|
279 |
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Debugging the time api.
|
---|
283 | *
|
---|
284 | * @returns the number of bad previous values encountered by RTTimeNanoTS().
|
---|
285 | */
|
---|
286 | RTDECL(uint32_t) RTTimeDbgBad(void)
|
---|
287 | {
|
---|
288 | return g_TimeNanoTSData.cBadPrev;
|
---|
289 | }
|
---|
290 | RT_EXPORT_SYMBOL(RTTimeDbgBad);
|
---|
291 |
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Debugging the time api.
|
---|
295 | *
|
---|
296 | * @returns the number of update races in RTTimeNanoTS().
|
---|
297 | */
|
---|
298 | RTDECL(uint32_t) RTTimeDbgRaces(void)
|
---|
299 | {
|
---|
300 | return g_TimeNanoTSData.cUpdateRaces;
|
---|
301 | }
|
---|
302 | RT_EXPORT_SYMBOL(RTTimeDbgRaces);
|
---|
303 | #endif /* !IN_GUEST && !RT_NO_GIP */
|
---|
304 |
|
---|