1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Fast user context implementation of clock_gettime, gettimeofday, and time.
5 * Copyright (C) 2019 ARM Limited.
6 * Copyright 2006 Andi Kleen, SUSE Labs.
7 * 32 Bit compat layer by Stefani Seibold <stefani@seibold.net>
8 * sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany
10 #ifndef __ASM_VDSO_GETTIMEOFDAY_H
11 #define __ASM_VDSO_GETTIMEOFDAY_H
15 #include <uapi/linux/time.h>
16 #include <asm/vgtod.h>
18 #include <asm/unistd.h>
20 #include <asm/pvclock.h>
21 #include <clocksource/hyperv_timer.h>
23 #define __vdso_data (VVAR(_vdso_data))
24 #define __timens_vdso_data (TIMENS(_vdso_data))
26 #define VDSO_HAS_TIME 1
28 #define VDSO_HAS_CLOCK_GETRES 1
31 * Declare the memory-mapped vclock data pages. These come from hypervisors.
32 * If we ever reintroduce something like direct access to an MMIO clock like
33 * the HPET again, it will go here as well.
35 * A load from any of these pages will segfault if the clock in question is
36 * disabled, so appropriate compiler barriers and checks need to be used
37 * to prevent stray loads.
39 * These declarations MUST NOT be const. The compiler will assume that
40 * an extern const variable has genuinely constant contents, and the
41 * resulting code won't work, since the whole point is that these pages
42 * change over time, possibly while we're accessing them.
45 #ifdef CONFIG_PARAVIRT_CLOCK
47 * This is the vCPU 0 pvclock page. We only use pvclock from the vDSO
48 * if the hypervisor tells us that all vCPUs can get valid data from the
51 extern struct pvclock_vsyscall_time_info pvclock_page
52 __attribute__((visibility("hidden")));
55 #ifdef CONFIG_HYPERV_TIMER
56 extern struct ms_hyperv_tsc_page hvclock_page
57 __attribute__((visibility("hidden")));
61 static __always_inline
62 const struct vdso_data *__arch_get_timens_vdso_data(const struct vdso_data *vd)
64 return __timens_vdso_data;
70 static __always_inline
71 long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
75 asm ("syscall" : "=a" (ret), "=m" (*_ts) :
76 "0" (__NR_clock_gettime), "D" (_clkid), "S" (_ts) :
82 static __always_inline
83 long gettimeofday_fallback(struct __kernel_old_timeval *_tv,
88 asm("syscall" : "=a" (ret) :
89 "0" (__NR_gettimeofday), "D" (_tv), "S" (_tz) : "memory");
94 static __always_inline
95 long clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
99 asm ("syscall" : "=a" (ret), "=m" (*_ts) :
100 "0" (__NR_clock_getres), "D" (_clkid), "S" (_ts) :
108 static __always_inline
109 long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
114 "mov %%ebx, %%edx \n"
115 "mov %[clock], %%ebx \n"
116 "call __kernel_vsyscall \n"
117 "mov %%edx, %%ebx \n"
118 : "=a" (ret), "=m" (*_ts)
119 : "0" (__NR_clock_gettime64), [clock] "g" (_clkid), "c" (_ts)
125 static __always_inline
126 long clock_gettime32_fallback(clockid_t _clkid, struct old_timespec32 *_ts)
131 "mov %%ebx, %%edx \n"
132 "mov %[clock], %%ebx \n"
133 "call __kernel_vsyscall \n"
134 "mov %%edx, %%ebx \n"
135 : "=a" (ret), "=m" (*_ts)
136 : "0" (__NR_clock_gettime), [clock] "g" (_clkid), "c" (_ts)
142 static __always_inline
143 long gettimeofday_fallback(struct __kernel_old_timeval *_tv,
144 struct timezone *_tz)
149 "mov %%ebx, %%edx \n"
151 "call __kernel_vsyscall \n"
152 "mov %%edx, %%ebx \n"
154 : "0" (__NR_gettimeofday), "g" (_tv), "c" (_tz)
160 static __always_inline long
161 clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
166 "mov %%ebx, %%edx \n"
167 "mov %[clock], %%ebx \n"
168 "call __kernel_vsyscall \n"
169 "mov %%edx, %%ebx \n"
170 : "=a" (ret), "=m" (*_ts)
171 : "0" (__NR_clock_getres_time64), [clock] "g" (_clkid), "c" (_ts)
177 static __always_inline
178 long clock_getres32_fallback(clockid_t _clkid, struct old_timespec32 *_ts)
183 "mov %%ebx, %%edx \n"
184 "mov %[clock], %%ebx \n"
185 "call __kernel_vsyscall \n"
186 "mov %%edx, %%ebx \n"
187 : "=a" (ret), "=m" (*_ts)
188 : "0" (__NR_clock_getres), [clock] "g" (_clkid), "c" (_ts)
196 #ifdef CONFIG_PARAVIRT_CLOCK
197 static u64 vread_pvclock(void)
199 const struct pvclock_vcpu_time_info *pvti = &pvclock_page.pvti;
204 * Note: The kernel and hypervisor must guarantee that cpu ID
205 * number maps 1:1 to per-CPU pvclock time info.
207 * Because the hypervisor is entirely unaware of guest userspace
208 * preemption, it cannot guarantee that per-CPU pvclock time
209 * info is updated if the underlying CPU changes or that that
210 * version is increased whenever underlying CPU changes.
212 * On KVM, we are guaranteed that pvti updates for any vCPU are
213 * atomic as seen by *all* vCPUs. This is an even stronger
214 * guarantee than we get with a normal seqlock.
216 * On Xen, we don't appear to have that guarantee, but Xen still
217 * supplies a valid seqlock using the version field.
219 * We only do pvclock vdso timing at all if
220 * PVCLOCK_TSC_STABLE_BIT is set, and we interpret that bit to
221 * mean that all vCPUs have matching pvti and that the TSC is
222 * synced, so we can just look at vCPU 0's pvti.
226 version = pvclock_read_begin(pvti);
228 if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT)))
231 ret = __pvclock_read_cycles(pvti, rdtsc_ordered());
232 } while (pvclock_read_retry(pvti, version));
238 #ifdef CONFIG_HYPERV_TIMER
239 static u64 vread_hvclock(void)
241 return hv_read_tsc_page(&hvclock_page);
245 static inline u64 __arch_get_hw_counter(s32 clock_mode,
246 const struct vdso_data *vd)
248 if (likely(clock_mode == VDSO_CLOCKMODE_TSC))
249 return (u64)rdtsc_ordered();
251 * For any memory-mapped vclock type, we need to make sure that gcc
252 * doesn't cleverly hoist a load before the mode check. Otherwise we
253 * might end up touching the memory-mapped page even if the vclock in
254 * question isn't enabled, which will segfault. Hence the barriers.
256 #ifdef CONFIG_PARAVIRT_CLOCK
257 if (clock_mode == VDSO_CLOCKMODE_PVCLOCK) {
259 return vread_pvclock();
262 #ifdef CONFIG_HYPERV_TIMER
263 if (clock_mode == VDSO_CLOCKMODE_HVCLOCK) {
265 return vread_hvclock();
271 static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
276 static inline bool arch_vdso_clocksource_ok(const struct vdso_data *vd)
280 #define vdso_clocksource_ok arch_vdso_clocksource_ok
283 * Clocksource read value validation to handle PV and HyperV clocksources
284 * which can be invalidated asynchronously and indicate invalidation by
285 * returning U64_MAX, which can be effectively tested by checking for a
286 * negative value after casting it to s64.
288 static inline bool arch_vdso_cycles_ok(u64 cycles)
290 return (s64)cycles >= 0;
292 #define vdso_cycles_ok arch_vdso_cycles_ok
295 * x86 specific delta calculation.
297 * The regular implementation assumes that clocksource reads are globally
298 * monotonic. The TSC can be slightly off across sockets which can cause
299 * the regular delta calculation (@cycles - @last) to return a huge time
302 * Therefore it needs to be verified that @cycles are greater than
303 * @last. If not then use @last, which is the base time of the current
306 * This variant also removes the masking of the subtraction because the
307 * clocksource mask of all VDSO capable clocksources on x86 is U64_MAX
308 * which would result in a pointless operation. The compiler cannot
309 * optimize it away as the mask comes from the vdso data and is not compile
312 static __always_inline
313 u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult)
316 return (cycles - last) * mult;
319 #define vdso_calc_delta vdso_calc_delta
321 #endif /* !__ASSEMBLY__ */
323 #endif /* __ASM_VDSO_GETTIMEOFDAY_H */