1 | /* $Id: time-r0drv-nt.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time, Ring-0 Driver, Nt.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2020 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 "the-nt-kernel.h"
|
---|
33 | #include "internal-r0drv-nt.h"
|
---|
34 | #include <iprt/time.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * The KeQueryTickCount macro isn't compatible with NT 3.1, use the
|
---|
39 | * exported KPI instead.
|
---|
40 | */
|
---|
41 | #ifdef RT_ARCH_X86
|
---|
42 | # undef KeQueryTickCount
|
---|
43 | extern "C" NTKERNELAPI void NTAPI KeQueryTickCount(PLARGE_INTEGER);
|
---|
44 | #endif
|
---|
45 |
|
---|
46 |
|
---|
47 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
48 | {
|
---|
49 | /*
|
---|
50 | * Note! The time source we use here must be exactly the same as in
|
---|
51 | * the ring-3 code!
|
---|
52 | *
|
---|
53 | * Using interrupt time is the simplest and requires the least calculation.
|
---|
54 | * It is also accounting for suspended time. Unfortuantely, there is no
|
---|
55 | * ring-3 for reading it... but that won't stop us.
|
---|
56 | *
|
---|
57 | * Using the tick count is problematic in ring-3 on older windows version
|
---|
58 | * as we can only get the 32-bit tick value, i.e. we'll roll over sooner or
|
---|
59 | * later.
|
---|
60 | */
|
---|
61 | #if 1
|
---|
62 | /* Interrupt time. */
|
---|
63 | LARGE_INTEGER InterruptTime;
|
---|
64 | if (g_pfnrtKeQueryInterruptTimePrecise)
|
---|
65 | {
|
---|
66 | ULONG64 QpcTsIgnored;
|
---|
67 | InterruptTime.QuadPart = g_pfnrtKeQueryInterruptTimePrecise(&QpcTsIgnored);
|
---|
68 | }
|
---|
69 | # ifdef RT_ARCH_X86
|
---|
70 | else if (g_pfnrtKeQueryInterruptTime) /* W2K+ */
|
---|
71 | InterruptTime.QuadPart = g_pfnrtKeQueryInterruptTime();
|
---|
72 | else if (g_uRtNtVersion >= RTNT_MAKE_VERSION(3, 50))
|
---|
73 | {
|
---|
74 | /* NT 3.50 and later, also pre-init: Use the user shared data. */
|
---|
75 | do
|
---|
76 | {
|
---|
77 | InterruptTime.HighPart = ((KUSER_SHARED_DATA volatile *)SharedUserData)->InterruptTime.High1Time;
|
---|
78 | InterruptTime.LowPart = ((KUSER_SHARED_DATA volatile *)SharedUserData)->InterruptTime.LowPart;
|
---|
79 | } while (((KUSER_SHARED_DATA volatile *)SharedUserData)->InterruptTime.High2Time != InterruptTime.HighPart);
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | /*
|
---|
84 | * There is no KUSER_SHARED_DATA structure on NT 3.1, so we have no choice
|
---|
85 | * but to use the tick count. We must also avoid the KeQueryTickCount macro
|
---|
86 | * in the WDK, since NT 3.1 doesn't have the KeTickCount data export either (see above).
|
---|
87 | */
|
---|
88 | static ULONG volatile s_uTimeIncrement = 0;
|
---|
89 | ULONG uTimeIncrement = s_uTimeIncrement;
|
---|
90 | if (!uTimeIncrement)
|
---|
91 | {
|
---|
92 | uTimeIncrement = KeQueryTimeIncrement();
|
---|
93 | Assert(uTimeIncrement != 0);
|
---|
94 | Assert(uTimeIncrement * 100 / 100 == uTimeIncrement);
|
---|
95 | uTimeIncrement *= 100;
|
---|
96 | s_uTimeIncrement = uTimeIncrement;
|
---|
97 | }
|
---|
98 |
|
---|
99 | KeQueryTickCount(&InterruptTime);
|
---|
100 | return (uint64_t)InterruptTime.QuadPart * uTimeIncrement;
|
---|
101 | }
|
---|
102 | # else
|
---|
103 | else
|
---|
104 | InterruptTime.QuadPart = KeQueryInterruptTime(); /* Macro on AMD64. */
|
---|
105 | # endif
|
---|
106 | return (uint64_t)InterruptTime.QuadPart * 100;
|
---|
107 | #else
|
---|
108 | /* Tick count. Works all the way back to NT 3.1 with #undef above. */
|
---|
109 | LARGE_INTEGER Tick;
|
---|
110 | KeQueryTickCount(&Tick);
|
---|
111 | return (uint64_t)Tick.QuadPart * KeQueryTimeIncrement() * 100;
|
---|
112 | #endif
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | RTDECL(uint64_t) RTTimeNanoTS(void)
|
---|
117 | {
|
---|
118 | return rtTimeGetSystemNanoTS();
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | RTDECL(uint64_t) RTTimeMilliTS(void)
|
---|
123 | {
|
---|
124 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
129 | {
|
---|
130 | return rtTimeGetSystemNanoTS();
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
135 | {
|
---|
136 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
141 | {
|
---|
142 | LARGE_INTEGER SystemTime;
|
---|
143 | if (g_pfnrtKeQuerySystemTimePrecise)
|
---|
144 | g_pfnrtKeQuerySystemTimePrecise(&SystemTime);
|
---|
145 | else
|
---|
146 | KeQuerySystemTime(&SystemTime); /* Macro on AMD64, export on X86. */
|
---|
147 | return RTTimeSpecSetNtTime(pTime, SystemTime.QuadPart);
|
---|
148 | }
|
---|
149 |
|
---|