1 // SPDX-License-Identifier: GPL-2.0
3 * Generic userspace implementations of gettimeofday() and similar.
5 #include <linux/compiler.h>
6 #include <linux/math64.h>
7 #include <linux/time.h>
8 #include <linux/kernel.h>
9 #include <linux/hrtimer_defs.h>
10 #include <vdso/datapage.h>
11 #include <vdso/helpers.h>
14 * The generic vDSO implementation requires that gettimeofday.h
16 * - __arch_get_vdso_data(): to get the vdso datapage.
17 * - __arch_get_hw_counter(): to get the hw counter based on the
19 * - gettimeofday_fallback(): fallback for gettimeofday.
20 * - clock_gettime_fallback(): fallback for clock_gettime.
21 * - clock_getres_fallback(): fallback for clock_getres.
23 #ifdef ENABLE_COMPAT_VDSO
24 #include <asm/vdso/compat_gettimeofday.h>
26 #include <asm/vdso/gettimeofday.h>
27 #endif /* ENABLE_COMPAT_VDSO */
29 #ifndef vdso_calc_delta
31 * Default implementation which works for all sane clocksources. That
32 * obviously excludes x86/TSC.
34 static __always_inline
35 u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult)
37 return ((cycles - last) & mask) * mult;
41 static int do_hres(const struct vdso_data *vd, clockid_t clk,
42 struct __kernel_timespec *ts)
44 const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
45 u64 cycles, last, sec, ns;
49 seq = vdso_read_begin(vd);
50 cycles = __arch_get_hw_counter(vd->clock_mode);
52 last = vd->cycle_last;
53 if (unlikely((s64)cycles < 0))
56 ns += vdso_calc_delta(cycles, last, vd->mask, vd->mult);
59 } while (unlikely(vdso_read_retry(vd, seq)));
62 * Do this outside the loop: a race inside the loop could result
63 * in __iter_div_u64_rem() being extremely slow.
65 ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
71 static void do_coarse(const struct vdso_data *vd, clockid_t clk,
72 struct __kernel_timespec *ts)
74 const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
78 seq = vdso_read_begin(vd);
79 ts->tv_sec = vdso_ts->sec;
80 ts->tv_nsec = vdso_ts->nsec;
81 } while (unlikely(vdso_read_retry(vd, seq)));
84 static __maybe_unused int
85 __cvdso_clock_gettime_common(clockid_t clock, struct __kernel_timespec *ts)
87 const struct vdso_data *vd = __arch_get_vdso_data();
90 /* Check for negative values or invalid clocks */
91 if (unlikely((u32) clock >= MAX_CLOCKS))
95 * Convert the clockid to a bitmask and use it to check which
96 * clocks are handled in the VDSO directly.
99 if (likely(msk & VDSO_HRES)) {
100 return do_hres(&vd[CS_HRES_COARSE], clock, ts);
101 } else if (msk & VDSO_COARSE) {
102 do_coarse(&vd[CS_HRES_COARSE], clock, ts);
104 } else if (msk & VDSO_RAW) {
105 return do_hres(&vd[CS_RAW], clock, ts);
110 static __maybe_unused int
111 __cvdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts)
113 int ret = __cvdso_clock_gettime_common(clock, ts);
116 return clock_gettime_fallback(clock, ts);
121 static __maybe_unused int
122 __cvdso_clock_gettime32(clockid_t clock, struct old_timespec32 *res)
124 struct __kernel_timespec ts;
127 ret = __cvdso_clock_gettime_common(clock, &ts);
129 #ifdef VDSO_HAS_32BIT_FALLBACK
131 return clock_gettime32_fallback(clock, res);
134 ret = clock_gettime_fallback(clock, &ts);
138 res->tv_sec = ts.tv_sec;
139 res->tv_nsec = ts.tv_nsec;
143 #endif /* BUILD_VDSO32 */
145 static __maybe_unused int
146 __cvdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
148 const struct vdso_data *vd = __arch_get_vdso_data();
150 if (likely(tv != NULL)) {
151 struct __kernel_timespec ts;
153 if (do_hres(&vd[CS_HRES_COARSE], CLOCK_REALTIME, &ts))
154 return gettimeofday_fallback(tv, tz);
156 tv->tv_sec = ts.tv_sec;
157 tv->tv_usec = (u32)ts.tv_nsec / NSEC_PER_USEC;
160 if (unlikely(tz != NULL)) {
161 tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest;
162 tz->tz_dsttime = vd[CS_HRES_COARSE].tz_dsttime;
169 static __maybe_unused __kernel_old_time_t __cvdso_time(__kernel_old_time_t *time)
171 const struct vdso_data *vd = __arch_get_vdso_data();
172 __kernel_old_time_t t = READ_ONCE(vd[CS_HRES_COARSE].basetime[CLOCK_REALTIME].sec);
179 #endif /* VDSO_HAS_TIME */
181 #ifdef VDSO_HAS_CLOCK_GETRES
182 static __maybe_unused
183 int __cvdso_clock_getres_common(clockid_t clock, struct __kernel_timespec *res)
185 const struct vdso_data *vd = __arch_get_vdso_data();
190 /* Check for negative values or invalid clocks */
191 if (unlikely((u32) clock >= MAX_CLOCKS))
194 hrtimer_res = READ_ONCE(vd[CS_HRES_COARSE].hrtimer_res);
196 * Convert the clockid to a bitmask and use it to check which
197 * clocks are handled in the VDSO directly.
200 if (msk & VDSO_HRES) {
202 * Preserves the behaviour of posix_get_hrtimer_res().
205 } else if (msk & VDSO_COARSE) {
207 * Preserves the behaviour of posix_get_coarse_res().
210 } else if (msk & VDSO_RAW) {
212 * Preserves the behaviour of posix_get_hrtimer_res().
226 static __maybe_unused
227 int __cvdso_clock_getres(clockid_t clock, struct __kernel_timespec *res)
229 int ret = __cvdso_clock_getres_common(clock, res);
232 return clock_getres_fallback(clock, res);
237 static __maybe_unused int
238 __cvdso_clock_getres_time32(clockid_t clock, struct old_timespec32 *res)
240 struct __kernel_timespec ts;
243 ret = __cvdso_clock_getres_common(clock, &ts);
245 #ifdef VDSO_HAS_32BIT_FALLBACK
247 return clock_getres32_fallback(clock, res);
250 ret = clock_getres_fallback(clock, &ts);
253 if (likely(!ret && res)) {
254 res->tv_sec = ts.tv_sec;
255 res->tv_nsec = ts.tv_nsec;
259 #endif /* BUILD_VDSO32 */
260 #endif /* VDSO_HAS_CLOCK_GETRES */