VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/* $Id: timesup.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Time using SUPLib.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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/asm.h>
36#include <iprt/assert.h>
37#include <iprt/err.h>
38#include <iprt/log.h>
39#ifndef IN_GUEST
40# include <VBox/sup.h>
41# include <VBox/x86.h>
42#endif
43#include "internal/time.h"
44
45
46/*******************************************************************************
47* Internal Functions *
48*******************************************************************************/
49#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
50static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
51static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData);
52static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData);
53#endif
54
55
56/*******************************************************************************
57* Global Variables *
58*******************************************************************************/
59#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
60/** The previous timestamp value returned by RTTimeNanoTS. */
61static uint64_t g_TimeNanoTSPrev = 0;
62
63/** The RTTimeNanoTS data structure that's passed down to the worker functions. */
64static RTTIMENANOTSDATA g_TimeNanoTSData =
65{
66 /* .pu64Prev = */ &g_TimeNanoTSPrev,
67 /* .pfnBad = */ rtTimeNanoTSInternalBitch,
68 /* .pfnRediscover = */ rtTimeNanoTSInternalRediscover,
69 /* .pvDummy = */ NULL,
70 /* .c1nsSteps = */ 0,
71 /* .cExpired = */ 0,
72 /* .cBadPrev = */ 0,
73 /* .cUpdateRaces = */ 0
74};
75
76/** The index into g_apfnWorkers for the function to use.
77 * This cannot be a pointer because that'll break down in GC due to code relocation. */
78static uint32_t g_iWorker = 0;
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#define RTTIMENANO_WORKER_SYNC_CPUID 1
86 RTTimeNanoTSLegacySync,
87#define RTTIMENANO_WORKER_ASYNC_CPUID 2
88 RTTimeNanoTSLegacyAsync,
89#define RTTIMENANO_WORKER_SYNC_LFENCE 3
90 RTTimeNanoTSLFenceSync,
91#define RTTIMENANO_WORKER_ASYNC_LFENCE 4
92 RTTimeNanoTSLFenceAsync,
93#define RTTIMENANO_WORKER_FALLBACK 5
94 rtTimeNanoTSInternalFallback,
95};
96
97
98/**
99 * Helper function that's used by the assembly routines when something goes bust.
100 *
101 * @param pData Pointer to the data structure.
102 * @param u64NanoTS The calculated nano ts.
103 * @param u64DeltaPrev The delta relative to the previously returned timestamp.
104 * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
105 */
106static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS)
107{
108 pData->cBadPrev++;
109 if ((int64_t)u64DeltaPrev < 0)
110 LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64\n",
111 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
112 else
113 Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 (debugging?)\n",
114 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
115}
116
117/**
118 * Fallback function.
119 */
120static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData)
121{
122 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
123 if ( pGip
124 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
125 && ( pGip->u32Mode == SUPGIPMODE_SYNC_TSC
126 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
127 return rtTimeNanoTSInternalRediscover(pData);
128 NOREF(pData);
129#if defined(IN_RING3) /** @todo Add ring-0 RTTimeSystemNanoTS to all hosts. */
130 return RTTimeSystemNanoTS();
131#else
132 return 0;
133#endif
134}
135
136
137/**
138 * Called the first time somebody asks for the time or when the GIP
139 * is mapped/unmapped.
140 */
141static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData)
142{
143 uint32_t iWorker;
144 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
145 if ( pGip
146 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
147 && ( pGip->u32Mode == SUPGIPMODE_SYNC_TSC
148 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
149 {
150 if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
151 iWorker = pGip->u32Mode == SUPGIPMODE_SYNC_TSC
152 ? RTTIMENANO_WORKER_SYNC_LFENCE
153 : RTTIMENANO_WORKER_ASYNC_LFENCE;
154 else
155 iWorker = pGip->u32Mode == SUPGIPMODE_SYNC_TSC
156 ? RTTIMENANO_WORKER_SYNC_CPUID
157 : RTTIMENANO_WORKER_ASYNC_CPUID;
158 }
159 else
160 iWorker = RTTIMENANO_WORKER_FALLBACK;
161
162 ASMAtomicXchgU32((uint32_t volatile *)&g_iWorker, iWorker);
163 return g_apfnWorkers[iWorker](pData);
164}
165
166#endif /* !IN_GUEST && !RT_NO_GIP */
167
168
169/**
170 * Internal worker for getting the current nanosecond timestamp.
171 */
172DECLINLINE(uint64_t) rtTimeNanoTSInternal(void)
173{
174#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
175 return g_apfnWorkers[g_iWorker](&g_TimeNanoTSData);
176#else
177 return RTTimeSystemNanoTS();
178#endif
179}
180
181
182/**
183 * Gets the current nanosecond timestamp.
184 *
185 * @returns nanosecond timestamp.
186 */
187RTDECL(uint64_t) RTTimeNanoTS(void)
188{
189 return rtTimeNanoTSInternal();
190}
191RT_EXPORT_SYMBOL(RTTimeNanoTS);
192
193
194/**
195 * Gets the current millisecond timestamp.
196 *
197 * @returns millisecond timestamp.
198 */
199RTDECL(uint64_t) RTTimeMilliTS(void)
200{
201 return rtTimeNanoTSInternal() / 1000000;
202}
203RT_EXPORT_SYMBOL(RTTimeMilliTS);
204
205
206#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
207/**
208 * Debugging the time api.
209 *
210 * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
211 */
212RTDECL(uint32_t) RTTimeDbgSteps(void)
213{
214 return g_TimeNanoTSData.c1nsSteps;
215}
216RT_EXPORT_SYMBOL(RTTimeDbgSteps);
217
218
219/**
220 * Debugging the time api.
221 *
222 * @returns the number of times the TSC interval expired RTTimeNanoTS().
223 */
224RTDECL(uint32_t) RTTimeDbgExpired(void)
225{
226 return g_TimeNanoTSData.cExpired;
227}
228RT_EXPORT_SYMBOL(RTTimeDbgExpired);
229
230
231/**
232 * Debugging the time api.
233 *
234 * @returns the number of bad previous values encountered by RTTimeNanoTS().
235 */
236RTDECL(uint32_t) RTTimeDbgBad(void)
237{
238 return g_TimeNanoTSData.cBadPrev;
239}
240RT_EXPORT_SYMBOL(RTTimeDbgBad);
241
242
243/**
244 * Debugging the time api.
245 *
246 * @returns the number of update races in RTTimeNanoTS().
247 */
248RTDECL(uint32_t) RTTimeDbgRaces(void)
249{
250 return g_TimeNanoTSData.cUpdateRaces;
251}
252RT_EXPORT_SYMBOL(RTTimeDbgRaces);
253#endif /* !IN_GUEST && !RT_NO_GIP */
254
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette