xref: /linux/arch/x86/include/asm/vdso/gettimeofday.h (revision 52338415)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Fast user context implementation of clock_gettime, gettimeofday, and time.
4  *
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
9  */
10 #ifndef __ASM_VDSO_GETTIMEOFDAY_H
11 #define __ASM_VDSO_GETTIMEOFDAY_H
12 
13 #ifndef __ASSEMBLY__
14 
15 #include <uapi/linux/time.h>
16 #include <asm/vgtod.h>
17 #include <asm/vvar.h>
18 #include <asm/unistd.h>
19 #include <asm/msr.h>
20 #include <asm/pvclock.h>
21 #include <clocksource/hyperv_timer.h>
22 
23 #define __vdso_data (VVAR(_vdso_data))
24 
25 #define VDSO_HAS_TIME 1
26 
27 #define VDSO_HAS_CLOCK_GETRES 1
28 
29 /*
30  * Declare the memory-mapped vclock data pages.  These come from hypervisors.
31  * If we ever reintroduce something like direct access to an MMIO clock like
32  * the HPET again, it will go here as well.
33  *
34  * A load from any of these pages will segfault if the clock in question is
35  * disabled, so appropriate compiler barriers and checks need to be used
36  * to prevent stray loads.
37  *
38  * These declarations MUST NOT be const.  The compiler will assume that
39  * an extern const variable has genuinely constant contents, and the
40  * resulting code won't work, since the whole point is that these pages
41  * change over time, possibly while we're accessing them.
42  */
43 
44 #ifdef CONFIG_PARAVIRT_CLOCK
45 /*
46  * This is the vCPU 0 pvclock page.  We only use pvclock from the vDSO
47  * if the hypervisor tells us that all vCPUs can get valid data from the
48  * vCPU 0 page.
49  */
50 extern struct pvclock_vsyscall_time_info pvclock_page
51 	__attribute__((visibility("hidden")));
52 #endif
53 
54 #ifdef CONFIG_HYPERV_TIMER
55 extern struct ms_hyperv_tsc_page hvclock_page
56 	__attribute__((visibility("hidden")));
57 #endif
58 
59 #ifndef BUILD_VDSO32
60 
61 static __always_inline
62 long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
63 {
64 	long ret;
65 
66 	asm ("syscall" : "=a" (ret), "=m" (*_ts) :
67 	     "0" (__NR_clock_gettime), "D" (_clkid), "S" (_ts) :
68 	     "rcx", "r11");
69 
70 	return ret;
71 }
72 
73 static __always_inline
74 long gettimeofday_fallback(struct __kernel_old_timeval *_tv,
75 			   struct timezone *_tz)
76 {
77 	long ret;
78 
79 	asm("syscall" : "=a" (ret) :
80 	    "0" (__NR_gettimeofday), "D" (_tv), "S" (_tz) : "memory");
81 
82 	return ret;
83 }
84 
85 static __always_inline
86 long clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
87 {
88 	long ret;
89 
90 	asm ("syscall" : "=a" (ret), "=m" (*_ts) :
91 	     "0" (__NR_clock_getres), "D" (_clkid), "S" (_ts) :
92 	     "rcx", "r11");
93 
94 	return ret;
95 }
96 
97 #else
98 
99 #define VDSO_HAS_32BIT_FALLBACK	1
100 
101 static __always_inline
102 long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
103 {
104 	long ret;
105 
106 	asm (
107 		"mov %%ebx, %%edx \n"
108 		"mov %[clock], %%ebx \n"
109 		"call __kernel_vsyscall \n"
110 		"mov %%edx, %%ebx \n"
111 		: "=a" (ret), "=m" (*_ts)
112 		: "0" (__NR_clock_gettime64), [clock] "g" (_clkid), "c" (_ts)
113 		: "edx");
114 
115 	return ret;
116 }
117 
118 static __always_inline
119 long clock_gettime32_fallback(clockid_t _clkid, struct old_timespec32 *_ts)
120 {
121 	long ret;
122 
123 	asm (
124 		"mov %%ebx, %%edx \n"
125 		"mov %[clock], %%ebx \n"
126 		"call __kernel_vsyscall \n"
127 		"mov %%edx, %%ebx \n"
128 		: "=a" (ret), "=m" (*_ts)
129 		: "0" (__NR_clock_gettime), [clock] "g" (_clkid), "c" (_ts)
130 		: "edx");
131 
132 	return ret;
133 }
134 
135 static __always_inline
136 long gettimeofday_fallback(struct __kernel_old_timeval *_tv,
137 			   struct timezone *_tz)
138 {
139 	long ret;
140 
141 	asm(
142 		"mov %%ebx, %%edx \n"
143 		"mov %2, %%ebx \n"
144 		"call __kernel_vsyscall \n"
145 		"mov %%edx, %%ebx \n"
146 		: "=a" (ret)
147 		: "0" (__NR_gettimeofday), "g" (_tv), "c" (_tz)
148 		: "memory", "edx");
149 
150 	return ret;
151 }
152 
153 static __always_inline long
154 clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
155 {
156 	long ret;
157 
158 	asm (
159 		"mov %%ebx, %%edx \n"
160 		"mov %[clock], %%ebx \n"
161 		"call __kernel_vsyscall \n"
162 		"mov %%edx, %%ebx \n"
163 		: "=a" (ret), "=m" (*_ts)
164 		: "0" (__NR_clock_getres_time64), [clock] "g" (_clkid), "c" (_ts)
165 		: "edx");
166 
167 	return ret;
168 }
169 
170 static __always_inline
171 long clock_getres32_fallback(clockid_t _clkid, struct old_timespec32 *_ts)
172 {
173 	long ret;
174 
175 	asm (
176 		"mov %%ebx, %%edx \n"
177 		"mov %[clock], %%ebx \n"
178 		"call __kernel_vsyscall \n"
179 		"mov %%edx, %%ebx \n"
180 		: "=a" (ret), "=m" (*_ts)
181 		: "0" (__NR_clock_getres), [clock] "g" (_clkid), "c" (_ts)
182 		: "edx");
183 
184 	return ret;
185 }
186 
187 #endif
188 
189 #ifdef CONFIG_PARAVIRT_CLOCK
190 static u64 vread_pvclock(void)
191 {
192 	const struct pvclock_vcpu_time_info *pvti = &pvclock_page.pvti;
193 	u32 version;
194 	u64 ret;
195 
196 	/*
197 	 * Note: The kernel and hypervisor must guarantee that cpu ID
198 	 * number maps 1:1 to per-CPU pvclock time info.
199 	 *
200 	 * Because the hypervisor is entirely unaware of guest userspace
201 	 * preemption, it cannot guarantee that per-CPU pvclock time
202 	 * info is updated if the underlying CPU changes or that that
203 	 * version is increased whenever underlying CPU changes.
204 	 *
205 	 * On KVM, we are guaranteed that pvti updates for any vCPU are
206 	 * atomic as seen by *all* vCPUs.  This is an even stronger
207 	 * guarantee than we get with a normal seqlock.
208 	 *
209 	 * On Xen, we don't appear to have that guarantee, but Xen still
210 	 * supplies a valid seqlock using the version field.
211 	 *
212 	 * We only do pvclock vdso timing at all if
213 	 * PVCLOCK_TSC_STABLE_BIT is set, and we interpret that bit to
214 	 * mean that all vCPUs have matching pvti and that the TSC is
215 	 * synced, so we can just look at vCPU 0's pvti.
216 	 */
217 
218 	do {
219 		version = pvclock_read_begin(pvti);
220 
221 		if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT)))
222 			return U64_MAX;
223 
224 		ret = __pvclock_read_cycles(pvti, rdtsc_ordered());
225 	} while (pvclock_read_retry(pvti, version));
226 
227 	return ret;
228 }
229 #endif
230 
231 #ifdef CONFIG_HYPERV_TIMER
232 static u64 vread_hvclock(void)
233 {
234 	return hv_read_tsc_page(&hvclock_page);
235 }
236 #endif
237 
238 static inline u64 __arch_get_hw_counter(s32 clock_mode)
239 {
240 	if (clock_mode == VCLOCK_TSC)
241 		return (u64)rdtsc_ordered();
242 	/*
243 	 * For any memory-mapped vclock type, we need to make sure that gcc
244 	 * doesn't cleverly hoist a load before the mode check.  Otherwise we
245 	 * might end up touching the memory-mapped page even if the vclock in
246 	 * question isn't enabled, which will segfault.  Hence the barriers.
247 	 */
248 #ifdef CONFIG_PARAVIRT_CLOCK
249 	if (clock_mode == VCLOCK_PVCLOCK) {
250 		barrier();
251 		return vread_pvclock();
252 	}
253 #endif
254 #ifdef CONFIG_HYPERV_TIMER
255 	if (clock_mode == VCLOCK_HVCLOCK) {
256 		barrier();
257 		return vread_hvclock();
258 	}
259 #endif
260 	return U64_MAX;
261 }
262 
263 static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
264 {
265 	return __vdso_data;
266 }
267 
268 /*
269  * x86 specific delta calculation.
270  *
271  * The regular implementation assumes that clocksource reads are globally
272  * monotonic. The TSC can be slightly off across sockets which can cause
273  * the regular delta calculation (@cycles - @last) to return a huge time
274  * jump.
275  *
276  * Therefore it needs to be verified that @cycles are greater than
277  * @last. If not then use @last, which is the base time of the current
278  * conversion period.
279  *
280  * This variant also removes the masking of the subtraction because the
281  * clocksource mask of all VDSO capable clocksources on x86 is U64_MAX
282  * which would result in a pointless operation. The compiler cannot
283  * optimize it away as the mask comes from the vdso data and is not compile
284  * time constant.
285  */
286 static __always_inline
287 u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult)
288 {
289 	if (cycles > last)
290 		return (cycles - last) * mult;
291 	return 0;
292 }
293 #define vdso_calc_delta vdso_calc_delta
294 
295 #endif /* !__ASSEMBLY__ */
296 
297 #endif /* __ASM_VDSO_GETTIMEOFDAY_H */
298