VirtualBox

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

Last change on this file since 81071 was 81071, checked in by vboxsync, 5 years ago

SUPDrv,IPRT,VMM: Support host APIC ID above 256 in GIP. (Only tested on 4 core intel.) bugref:9501

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