1 | /* $Id: timesupref.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time using SUPLib, the C Implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 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 | #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Header Files *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | #include <iprt/time.h>
|
---|
44 | #include "internal/iprt.h"
|
---|
45 |
|
---|
46 | #include <iprt/asm.h>
|
---|
47 | #include <iprt/asm-math.h>
|
---|
48 | #include <iprt/asm-amd64-x86.h>
|
---|
49 | #include <iprt/param.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <VBox/sup.h>
|
---|
52 | #ifdef IN_RC
|
---|
53 | # include <VBox/vmm/vmm.h>
|
---|
54 | # include <VBox/vmm/vm.h>
|
---|
55 | #endif
|
---|
56 | #include "internal/time.h"
|
---|
57 |
|
---|
58 |
|
---|
59 | #define TMPL_MODE_SYNC_INVAR_NO_DELTA 1
|
---|
60 | #define TMPL_MODE_SYNC_INVAR_WITH_DELTA 2
|
---|
61 | #define TMPL_MODE_ASYNC 3
|
---|
62 |
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Use the XCHG instruction for some kind of serialization.
|
---|
66 | */
|
---|
67 | #define TMPL_READ_FENCE() ASMReadFence()
|
---|
68 |
|
---|
69 | #undef TMPL_MODE
|
---|
70 | #define TMPL_MODE TMPL_MODE_SYNC_INVAR_NO_DELTA
|
---|
71 | #undef TMPL_GET_CPU_METHOD
|
---|
72 | #define TMPL_GET_CPU_METHOD 0
|
---|
73 | #undef rtTimeNanoTSInternalRef
|
---|
74 | #define rtTimeNanoTSInternalRef RTTimeNanoTSLegacySyncInvarNoDelta
|
---|
75 | #include "timesupref.h"
|
---|
76 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacySyncInvarNoDelta);
|
---|
77 |
|
---|
78 | #ifdef IN_RING3
|
---|
79 |
|
---|
80 | # undef TMPL_MODE
|
---|
81 | # define TMPL_MODE TMPL_MODE_SYNC_INVAR_WITH_DELTA
|
---|
82 | # undef TMPL_GET_CPU_METHOD
|
---|
83 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID
|
---|
84 | # undef rtTimeNanoTSInternalRef
|
---|
85 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId
|
---|
86 | # include "timesupref.h"
|
---|
87 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacySyncInvarWithDeltaUseApicId);
|
---|
88 |
|
---|
89 | # undef TMPL_MODE
|
---|
90 | # define TMPL_MODE TMPL_MODE_SYNC_INVAR_WITH_DELTA
|
---|
91 | # undef TMPL_GET_CPU_METHOD
|
---|
92 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID_EXT_0B
|
---|
93 | # undef rtTimeNanoTSInternalRef
|
---|
94 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt0B
|
---|
95 | # include "timesupref.h"
|
---|
96 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt0B);
|
---|
97 |
|
---|
98 | # undef TMPL_MODE
|
---|
99 | # define TMPL_MODE TMPL_MODE_SYNC_INVAR_WITH_DELTA
|
---|
100 | # undef TMPL_GET_CPU_METHOD
|
---|
101 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID_EXT_8000001E
|
---|
102 | # undef rtTimeNanoTSInternalRef
|
---|
103 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt8000001E
|
---|
104 | # include "timesupref.h"
|
---|
105 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacySyncInvarWithDeltaUseApicIdExt8000001E);
|
---|
106 |
|
---|
107 | # undef TMPL_GET_CPU_METHOD
|
---|
108 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
|
---|
109 | # undef rtTimeNanoTSInternalRef
|
---|
110 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp
|
---|
111 | # include "timesupref.h"
|
---|
112 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacySyncInvarWithDeltaUseRdtscp);
|
---|
113 |
|
---|
114 | # undef TMPL_GET_CPU_METHOD
|
---|
115 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
|
---|
116 | # undef rtTimeNanoTSInternalRef
|
---|
117 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim
|
---|
118 | # include "timesupref.h"
|
---|
119 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacySyncInvarWithDeltaUseIdtrLim);
|
---|
120 |
|
---|
121 | # undef TMPL_MODE
|
---|
122 | # define TMPL_MODE TMPL_MODE_ASYNC
|
---|
123 | # undef TMPL_GET_CPU_METHOD
|
---|
124 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID
|
---|
125 | # undef rtTimeNanoTSInternalRef
|
---|
126 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacyAsyncUseApicId
|
---|
127 | # include "timesupref.h"
|
---|
128 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacyAsyncUseApicId);
|
---|
129 |
|
---|
130 | # undef TMPL_MODE
|
---|
131 | # define TMPL_MODE TMPL_MODE_ASYNC
|
---|
132 | # undef TMPL_GET_CPU_METHOD
|
---|
133 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID_EXT_0B
|
---|
134 | # undef rtTimeNanoTSInternalRef
|
---|
135 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacyAsyncUseApicIdExt0B
|
---|
136 | # include "timesupref.h"
|
---|
137 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacyAsyncUseApicIdExt0B);
|
---|
138 |
|
---|
139 | # undef TMPL_MODE
|
---|
140 | # define TMPL_MODE TMPL_MODE_ASYNC
|
---|
141 | # undef TMPL_GET_CPU_METHOD
|
---|
142 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID_EXT_8000001E
|
---|
143 | # undef rtTimeNanoTSInternalRef
|
---|
144 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacyAsyncUseApicIdExt8000001E
|
---|
145 | # include "timesupref.h"
|
---|
146 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacyAsyncUseApicIdExt8000001E);
|
---|
147 |
|
---|
148 | # undef TMPL_GET_CPU_METHOD
|
---|
149 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
|
---|
150 | # undef rtTimeNanoTSInternalRef
|
---|
151 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacyAsyncUseRdtscp
|
---|
152 | # include "timesupref.h"
|
---|
153 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacyAsyncUseRdtscp);
|
---|
154 |
|
---|
155 | # undef TMPL_GET_CPU_METHOD
|
---|
156 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
|
---|
157 | # undef rtTimeNanoTSInternalRef
|
---|
158 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacyAsyncUseIdtrLim
|
---|
159 | # include "timesupref.h"
|
---|
160 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacyAsyncUseIdtrLim);
|
---|
161 |
|
---|
162 | # undef TMPL_GET_CPU_METHOD
|
---|
163 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_RDTSCP_GROUP_IN_CH_NUMBER_IN_CL
|
---|
164 | # undef rtTimeNanoTSInternalRef
|
---|
165 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacyAsyncUseRdtscpGroupChNumCl
|
---|
166 | # include "timesupref.h"
|
---|
167 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacyAsyncUseRdtscpGroupChNumCl);
|
---|
168 |
|
---|
169 | #else /* IN_RC || IN_RING0: Disable interrupts and call getter function. */
|
---|
170 |
|
---|
171 | # undef TMPL_MODE
|
---|
172 | # define TMPL_MODE TMPL_MODE_SYNC_INVAR_WITH_DELTA
|
---|
173 | # undef TMPL_GET_CPU_METHOD
|
---|
174 | # define TMPL_GET_CPU_METHOD UINT32_MAX
|
---|
175 | # undef rtTimeNanoTSInternalRef
|
---|
176 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacySyncInvarWithDelta
|
---|
177 | # include "timesupref.h"
|
---|
178 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacySyncInvarWithDelta);
|
---|
179 |
|
---|
180 | # undef TMPL_MODE
|
---|
181 | # define TMPL_MODE TMPL_MODE_ASYNC
|
---|
182 | # undef rtTimeNanoTSInternalRef
|
---|
183 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLegacyAsync
|
---|
184 | # include "timesupref.h"
|
---|
185 | RT_EXPORT_SYMBOL(RTTimeNanoTSLegacyAsync);
|
---|
186 |
|
---|
187 | #endif
|
---|
188 |
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Use LFENCE for load serialization.
|
---|
192 | */
|
---|
193 | #undef TMPL_READ_FENCE
|
---|
194 | #define TMPL_READ_FENCE() ASMReadFenceSSE2()
|
---|
195 |
|
---|
196 | #undef TMPL_MODE
|
---|
197 | #define TMPL_MODE TMPL_MODE_SYNC_INVAR_NO_DELTA
|
---|
198 | #undef TMPL_GET_CPU_METHOD
|
---|
199 | #define TMPL_GET_CPU_METHOD 0
|
---|
200 | #undef rtTimeNanoTSInternalRef
|
---|
201 | #define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceSyncInvarNoDelta
|
---|
202 | #include "timesupref.h"
|
---|
203 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceSyncInvarNoDelta);
|
---|
204 |
|
---|
205 | #ifdef IN_RING3
|
---|
206 |
|
---|
207 | # undef TMPL_MODE
|
---|
208 | # define TMPL_MODE TMPL_MODE_SYNC_INVAR_WITH_DELTA
|
---|
209 | # undef TMPL_GET_CPU_METHOD
|
---|
210 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID
|
---|
211 | # undef rtTimeNanoTSInternalRef
|
---|
212 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId
|
---|
213 | # include "timesupref.h"
|
---|
214 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicId);
|
---|
215 |
|
---|
216 | # undef TMPL_MODE
|
---|
217 | # define TMPL_MODE TMPL_MODE_SYNC_INVAR_WITH_DELTA
|
---|
218 | # undef TMPL_GET_CPU_METHOD
|
---|
219 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID_EXT_0B
|
---|
220 | # undef rtTimeNanoTSInternalRef
|
---|
221 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt0B
|
---|
222 | # include "timesupref.h"
|
---|
223 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt0B);
|
---|
224 |
|
---|
225 | # undef TMPL_MODE
|
---|
226 | # define TMPL_MODE TMPL_MODE_SYNC_INVAR_WITH_DELTA
|
---|
227 | # undef TMPL_GET_CPU_METHOD
|
---|
228 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID_EXT_8000001E
|
---|
229 | # undef rtTimeNanoTSInternalRef
|
---|
230 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt8000001E
|
---|
231 | # include "timesupref.h"
|
---|
232 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceSyncInvarWithDeltaUseApicIdExt8000001E);
|
---|
233 |
|
---|
234 | # undef TMPL_GET_CPU_METHOD
|
---|
235 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
|
---|
236 | # undef rtTimeNanoTSInternalRef
|
---|
237 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp
|
---|
238 | # include "timesupref.h"
|
---|
239 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceSyncInvarWithDeltaUseRdtscp);
|
---|
240 |
|
---|
241 | # undef TMPL_GET_CPU_METHOD
|
---|
242 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
|
---|
243 | # undef rtTimeNanoTSInternalRef
|
---|
244 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim
|
---|
245 | # include "timesupref.h"
|
---|
246 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceSyncInvarWithDeltaUseIdtrLim);
|
---|
247 |
|
---|
248 | # undef TMPL_MODE
|
---|
249 | # define TMPL_MODE TMPL_MODE_ASYNC
|
---|
250 | # undef TMPL_GET_CPU_METHOD
|
---|
251 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID
|
---|
252 | # undef rtTimeNanoTSInternalRef
|
---|
253 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceAsyncUseApicId
|
---|
254 | # include "timesupref.h"
|
---|
255 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceAsyncUseApicId);
|
---|
256 |
|
---|
257 | # undef TMPL_MODE
|
---|
258 | # define TMPL_MODE TMPL_MODE_ASYNC
|
---|
259 | # undef TMPL_GET_CPU_METHOD
|
---|
260 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID_EXT_0B
|
---|
261 | # undef rtTimeNanoTSInternalRef
|
---|
262 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceAsyncUseApicIdExt0B
|
---|
263 | # include "timesupref.h"
|
---|
264 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceAsyncUseApicIdExt0B);
|
---|
265 |
|
---|
266 | # undef TMPL_MODE
|
---|
267 | # define TMPL_MODE TMPL_MODE_ASYNC
|
---|
268 | # undef TMPL_GET_CPU_METHOD
|
---|
269 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_APIC_ID_EXT_8000001E
|
---|
270 | # undef rtTimeNanoTSInternalRef
|
---|
271 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceAsyncUseApicIdExt8000001E
|
---|
272 | # include "timesupref.h"
|
---|
273 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceAsyncUseApicIdExt8000001E);
|
---|
274 |
|
---|
275 | # undef TMPL_GET_CPU_METHOD
|
---|
276 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS
|
---|
277 | # undef rtTimeNanoTSInternalRef
|
---|
278 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceAsyncUseRdtscp
|
---|
279 | # include "timesupref.h"
|
---|
280 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceAsyncUseRdtscp);
|
---|
281 |
|
---|
282 | # undef TMPL_GET_CPU_METHOD
|
---|
283 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS
|
---|
284 | # undef rtTimeNanoTSInternalRef
|
---|
285 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceAsyncUseIdtrLim
|
---|
286 | # include "timesupref.h"
|
---|
287 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceAsyncUseIdtrLim);
|
---|
288 |
|
---|
289 | # undef TMPL_GET_CPU_METHOD
|
---|
290 | # define TMPL_GET_CPU_METHOD SUPGIPGETCPU_RDTSCP_GROUP_IN_CH_NUMBER_IN_CL
|
---|
291 | # undef rtTimeNanoTSInternalRef
|
---|
292 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceAsyncUseRdtscpGroupChNumCl
|
---|
293 | # include "timesupref.h"
|
---|
294 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceAsyncUseRdtscpGroupChNumCl);
|
---|
295 |
|
---|
296 | #else /* IN_RC || IN_RING0: Disable interrupts and call getter function. */
|
---|
297 |
|
---|
298 | # undef TMPL_MODE
|
---|
299 | # define TMPL_MODE TMPL_MODE_SYNC_INVAR_WITH_DELTA
|
---|
300 | # undef TMPL_GET_CPU_METHOD
|
---|
301 | # define TMPL_GET_CPU_METHOD UINT32_MAX
|
---|
302 | # undef rtTimeNanoTSInternalRef
|
---|
303 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceSyncInvarWithDelta
|
---|
304 | # include "timesupref.h"
|
---|
305 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceSyncInvarWithDelta);
|
---|
306 |
|
---|
307 | # undef TMPL_MODE
|
---|
308 | # define TMPL_MODE TMPL_MODE_ASYNC
|
---|
309 | # undef rtTimeNanoTSInternalRef
|
---|
310 | # define rtTimeNanoTSInternalRef RTTimeNanoTSLFenceAsync
|
---|
311 | # include "timesupref.h"
|
---|
312 | RT_EXPORT_SYMBOL(RTTimeNanoTSLFenceAsync);
|
---|
313 |
|
---|
314 | #endif
|
---|
315 |
|
---|
316 |
|
---|
317 | #endif /* !IN_GUEST && !RT_NO_GIP */
|
---|
318 |
|
---|