1 | /* $Id: timesup.cpp 5461 2007-10-24 02:18:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Time using SUPLib.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
23 | #include <iprt/time.h>
|
---|
24 | #include <iprt/asm.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/err.h>
|
---|
27 | #include <iprt/log.h>
|
---|
28 | #ifndef IN_GUEST
|
---|
29 | # include <VBox/sup.h>
|
---|
30 | # include <VBox/x86.h>
|
---|
31 | #endif
|
---|
32 | #include "internal/time.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | /*******************************************************************************
|
---|
36 | * Internal Functions *
|
---|
37 | *******************************************************************************/
|
---|
38 | #ifndef IN_GUEST
|
---|
39 | static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
|
---|
40 | static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData);
|
---|
41 | static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData);
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Global Variables *
|
---|
47 | *******************************************************************************/
|
---|
48 | #ifndef IN_GUEST
|
---|
49 | /** The previous timestamp value returned by RTTimeNanoTS. */
|
---|
50 | static uint64_t g_TimeNanoTSPrev = 0;
|
---|
51 |
|
---|
52 | /** The RTTimeNanoTS data structure that's passed down to the worker functions. */
|
---|
53 | static RTTIMENANOTSDATA g_TimeNanoTSData =
|
---|
54 | {
|
---|
55 | /* .pu64Prev = */ &g_TimeNanoTSPrev,
|
---|
56 | /* .pfnBad = */ rtTimeNanoTSInternalBitch,
|
---|
57 | /* .pfnRediscover = */ rtTimeNanoTSInternalRediscover,
|
---|
58 | /* .c1nsSteps = */ 0,
|
---|
59 | /* .cExpired = */ 0,
|
---|
60 | /* .cBadPrev = */ 0,
|
---|
61 | /* .cUpdateRaces = */ 0
|
---|
62 | };
|
---|
63 |
|
---|
64 | /** The index into g_apfnWorkers for the function to use.
|
---|
65 | * This cannot be a pointer because that'll break down in GC due to code relocation. */
|
---|
66 | static uint32_t g_iWorker = 0;
|
---|
67 | /** Array of rtTimeNanoTSInternal worker functions.
|
---|
68 | * This array is indexed by g_iWorker. */
|
---|
69 | static const PFNTIMENANOTSINTERNAL g_apfnWorkers[] =
|
---|
70 | {
|
---|
71 | #define RTTIMENANO_WORKER_DETECT 0
|
---|
72 | rtTimeNanoTSInternalRediscover,
|
---|
73 | #define RTTIMENANO_WORKER_SYNC_CPUID 1
|
---|
74 | RTTimeNanoTSLegacySync,
|
---|
75 | #define RTTIMENANO_WORKER_ASYNC_CPUID 2
|
---|
76 | RTTimeNanoTSLegacyAsync,
|
---|
77 | #define RTTIMENANO_WORKER_SYNC_LFENCE 3
|
---|
78 | RTTimeNanoTSLFenceSync,
|
---|
79 | #define RTTIMENANO_WORKER_ASYNC_LFENCE 4
|
---|
80 | RTTimeNanoTSLFenceAsync,
|
---|
81 | #define RTTIMENANO_WORKER_FALLBACK 5
|
---|
82 | rtTimeNanoTSInternalFallback,
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Helper function that's used by the assembly routines when something goes bust.
|
---|
88 | *
|
---|
89 | * @param pData Pointer to the data structure.
|
---|
90 | * @param u64NanoTS The calculated nano ts.
|
---|
91 | * @param u64DeltaPrev The delta relative to the previously returned timestamp.
|
---|
92 | * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
|
---|
93 | */
|
---|
94 | static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS)
|
---|
95 | {
|
---|
96 | pData->cBadPrev++;
|
---|
97 | if ((int64_t)u64DeltaPrev < 0)
|
---|
98 | LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64\n",
|
---|
99 | u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
|
---|
100 | else
|
---|
101 | Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 (debugging?)\n",
|
---|
102 | u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Fallback function.
|
---|
107 | */
|
---|
108 | static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData)
|
---|
109 | {
|
---|
110 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
111 | if ( pGip
|
---|
112 | && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
|
---|
113 | && ( pGip->u32Mode == SUPGIPMODE_SYNC_TSC
|
---|
114 | || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
|
---|
115 | return rtTimeNanoTSInternalRediscover(pData);
|
---|
116 | NOREF(pData);
|
---|
117 | return RTTimeSystemNanoTS();
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Called the first time somebody asks for the time or when the GIP
|
---|
123 | * is mapped/unmapped.
|
---|
124 | */
|
---|
125 | static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData)
|
---|
126 | {
|
---|
127 | uint32_t iWorker;
|
---|
128 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
129 | if ( pGip
|
---|
130 | && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
|
---|
131 | && ( pGip->u32Mode == SUPGIPMODE_SYNC_TSC
|
---|
132 | || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
|
---|
133 | {
|
---|
134 | if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
|
---|
135 | iWorker = pGip->u32Mode == SUPGIPMODE_SYNC_TSC
|
---|
136 | ? RTTIMENANO_WORKER_SYNC_LFENCE
|
---|
137 | : RTTIMENANO_WORKER_ASYNC_LFENCE;
|
---|
138 | else
|
---|
139 | iWorker = pGip->u32Mode == SUPGIPMODE_SYNC_TSC
|
---|
140 | ? RTTIMENANO_WORKER_SYNC_CPUID
|
---|
141 | : RTTIMENANO_WORKER_ASYNC_CPUID;
|
---|
142 | }
|
---|
143 | else
|
---|
144 | iWorker = RTTIMENANO_WORKER_FALLBACK;
|
---|
145 |
|
---|
146 | ASMAtomicXchgU32((uint32_t volatile *)&g_iWorker, iWorker);
|
---|
147 | return g_apfnWorkers[iWorker](pData);
|
---|
148 | }
|
---|
149 |
|
---|
150 | #endif /* !IN_GUEST */
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Internal worker for getting the current nanosecond timestamp.
|
---|
155 | */
|
---|
156 | DECLINLINE(uint64_t) rtTimeNanoTSInternal(void)
|
---|
157 | {
|
---|
158 | #ifndef IN_GUEST
|
---|
159 | return g_apfnWorkers[g_iWorker](&g_TimeNanoTSData);
|
---|
160 | #else
|
---|
161 | return RTTimeSystemNanoTS();
|
---|
162 | #endif
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Gets the current nanosecond timestamp.
|
---|
168 | *
|
---|
169 | * @returns nanosecond timestamp.
|
---|
170 | */
|
---|
171 | RTDECL(uint64_t) RTTimeNanoTS(void)
|
---|
172 | {
|
---|
173 | return rtTimeNanoTSInternal();
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Gets the current millisecond timestamp.
|
---|
179 | *
|
---|
180 | * @returns millisecond timestamp.
|
---|
181 | */
|
---|
182 | RTDECL(uint64_t) RTTimeMilliTS(void)
|
---|
183 | {
|
---|
184 | return rtTimeNanoTSInternal() / 1000000;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | #ifndef IN_GUEST
|
---|
189 | /**
|
---|
190 | * Debugging the time api.
|
---|
191 | *
|
---|
192 | * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
|
---|
193 | */
|
---|
194 | RTDECL(uint32_t) RTTimeDbgSteps(void)
|
---|
195 | {
|
---|
196 | return g_TimeNanoTSData.c1nsSteps;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Debugging the time api.
|
---|
202 | *
|
---|
203 | * @returns the number of times the TSC interval expired RTTimeNanoTS().
|
---|
204 | */
|
---|
205 | RTDECL(uint32_t) RTTimeDbgExpired(void)
|
---|
206 | {
|
---|
207 | return g_TimeNanoTSData.cExpired;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Debugging the time api.
|
---|
213 | *
|
---|
214 | * @returns the number of bad previous values encountered by RTTimeNanoTS().
|
---|
215 | */
|
---|
216 | RTDECL(uint32_t) RTTimeDbgBad(void)
|
---|
217 | {
|
---|
218 | return g_TimeNanoTSData.cBadPrev;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Debugging the time api.
|
---|
224 | *
|
---|
225 | * @returns the number of update races in RTTimeNanoTS().
|
---|
226 | */
|
---|
227 | RTDECL(uint32_t) RTTimeDbgRaces(void)
|
---|
228 | {
|
---|
229 | return g_TimeNanoTSData.cUpdateRaces;
|
---|
230 | }
|
---|
231 | #endif
|
---|