VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/nt/time-nt.cpp@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.8 KB
Line 
1/* $Id: time-nt.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Time, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_TIME
42#include "internal-r3-nt.h"
43
44#include <iprt/time.h>
45#include <iprt/asm.h>
46#include <iprt/assert.h>
47#include <iprt/errcore.h>
48#include <iprt/ldr.h>
49#include <iprt/uint128.h>
50#include "internal/time.h"
51
52
53/*********************************************************************************************************************************
54* Global Variables *
55*********************************************************************************************************************************/
56/** Whether we've tried to resolve g_pfnRtlGetSystemTimePrecise or not. */
57static bool g_fInitialized = false;
58/** Pointer to RtlGetSystemTimePrecise, added in 6.2 (windows 8). */
59static PFNRTLGETSYSTEMTIMEPRECISE g_pfnRtlGetSystemTimePrecise = NULL;
60
61
62/**
63 * Initializes globals.
64 */
65static void rtTimeNtInitialize(void)
66{
67 /*
68 * Make sure we don't recurse here when calling into RTLdr.
69 */
70 if (ASMAtomicCmpXchgBool(&g_fInitialized, true, false))
71 {
72 void *pvFunc = RTLdrGetSystemSymbol("ntdll.dll", "RtlGetSystemTimePrecise");
73 if (pvFunc)
74 ASMAtomicWritePtr((void * volatile *)&g_pfnRtlGetSystemTimePrecise, pvFunc);
75 ASMCompilerBarrier();
76 }
77}
78
79
80static uint64_t rtTimeGetSystemNanoTS(void)
81{
82 if (RT_UNLIKELY(!g_fInitialized))
83 rtTimeNtInitialize();
84
85 KUSER_SHARED_DATA volatile *pUserSharedData = (KUSER_SHARED_DATA volatile *)MM_SHARED_USER_DATA_VA;
86
87#if 1
88 /*
89 * If there is precise time, get the precise system time and calculate the
90 * interrupt time from it. (Microsoft doesn't expose interrupt time to user
91 * application, which is very unfortunate as there are a lot place where
92 * monotonic time is applicable but developer is "forced" to use wall clock.)
93 */
94 if (g_pfnRtlGetSystemTimePrecise)
95 {
96 for (;;)
97 {
98 uint64_t uUpdateLockBefore;
99 while ((uUpdateLockBefore = pUserSharedData->TimeUpdateLock) & 1)
100 ASMNopPause();
101
102 uint64_t uInterruptTime = *(uint64_t volatile *)&pUserSharedData->InterruptTime;
103 uint64_t uBaselineInterruptTimeQpc = pUserSharedData->BaselineInterruptTimeQpc;
104 uint64_t uQpcInterruptTimeIncrement = pUserSharedData->QpcInterruptTimeIncrement;
105 uint8_t uQpcInterruptTimeIncrementShift = pUserSharedData->QpcInterruptTimeIncrementShift;
106 LARGE_INTEGER QpcValue;
107 RtlQueryPerformanceCounter(&QpcValue);
108
109 if (pUserSharedData->TimeUpdateLock == uUpdateLockBefore)
110 {
111 uint64_t uQpcValue = QpcValue.QuadPart;
112 if (uQpcValue <= uBaselineInterruptTimeQpc)
113 return uInterruptTime * 100;
114
115 /* Calc QPC delta since base line. */
116 uQpcValue -= uBaselineInterruptTimeQpc;
117 uQpcValue--;
118
119 /* Multiply by 10 million. */
120 uQpcValue *= UINT32_C(10000000);
121
122 /* Multiply by QPC interrupt time increment value. */
123 RTUINT128U Tmp128;
124 RTUInt128MulU64ByU64(&Tmp128, uQpcValue, uQpcInterruptTimeIncrement);
125
126 /* Shift the upper 64 bits by the increment shift factor. */
127 uint64_t uResult = Tmp128.s.Hi >> uQpcInterruptTimeIncrementShift;
128
129 /* Add to base interrupt time value. */
130 uResult += uInterruptTime;
131
132 /* Convert from NT unit to nano seconds. */
133 return uResult * 100;
134 }
135
136 ASMNopPause();
137 }
138 }
139#endif
140
141 /*
142 * Just read interrupt time.
143 */
144#if ARCH_BITS >= 64
145 uint64_t uRet = *(uint64_t volatile *)&pUserSharedData->InterruptTime; /* This is what KeQueryInterruptTime does. */
146 uRet *= 100;
147 return uRet;
148#else
149
150 LARGE_INTEGER NtTime;
151 do
152 {
153 NtTime.HighPart = pUserSharedData->InterruptTime.High1Time;
154 NtTime.LowPart = pUserSharedData->InterruptTime.LowPart;
155 } while (pUserSharedData->InterruptTime.High2Time != NtTime.HighPart);
156
157 return (uint64_t)NtTime.QuadPart * 100;
158#endif
159}
160
161
162RTDECL(uint64_t) RTTimeSystemNanoTS(void)
163{
164 return rtTimeGetSystemNanoTS();
165}
166
167
168RTDECL(uint64_t) RTTimeSystemMilliTS(void)
169{
170 return rtTimeGetSystemNanoTS() / RT_NS_1MS;
171}
172
173
174RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
175{
176 /*
177 * Get the precise time if possible.
178 */
179 if (RT_UNLIKELY(!g_fInitialized))
180 rtTimeNtInitialize();
181 if (g_pfnRtlGetSystemTimePrecise != NULL)
182 return RTTimeSpecSetNtTime(pTime, g_pfnRtlGetSystemTimePrecise());
183
184 /*
185 * Just read system time.
186 */
187 KUSER_SHARED_DATA volatile *pUserSharedData = (KUSER_SHARED_DATA volatile *)MM_SHARED_USER_DATA_VA;
188#ifdef RT_ARCH_AMD64
189 uint64_t uRet = *(uint64_t volatile *)&pUserSharedData->SystemTime; /* This is what KeQuerySystemTime does. */
190 return RTTimeSpecSetNtTime(pTime, uRet);
191#else
192
193 LARGE_INTEGER NtTime;
194 do
195 {
196 NtTime.HighPart = pUserSharedData->SystemTime.High1Time;
197 NtTime.LowPart = pUserSharedData->SystemTime.LowPart;
198 } while (pUserSharedData->SystemTime.High2Time != NtTime.HighPart);
199 return RTTimeSpecSetNtTime(pTime, NtTime.QuadPart);
200#endif
201}
202
203
204RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime)
205{
206 return RTTimeSpecAddNano(RTTimeNow(pTime), RTTimeLocalDeltaNano());
207}
208
209
210RTDECL(int64_t) RTTimeLocalDeltaNano(void)
211{
212 /*
213 * UTC = local + TimeZoneBias; The bias is given in NT units.
214 */
215 KUSER_SHARED_DATA volatile *pUserSharedData = (KUSER_SHARED_DATA volatile *)MM_SHARED_USER_DATA_VA;
216 LARGE_INTEGER Delta;
217#if ARCH_BITS == 64
218 Delta.QuadPart = *(int64_t volatile *)&pUserSharedData->TimeZoneBias;
219#else
220 do
221 {
222 Delta.HighPart = pUserSharedData->TimeZoneBias.High1Time;
223 Delta.LowPart = pUserSharedData->TimeZoneBias.LowPart;
224 } while (pUserSharedData->TimeZoneBias.High2Time != Delta.HighPart);
225#endif
226 return Delta.QuadPart * -100;
227}
228
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