blob: 5a5ec899a21ac5e413ca6e30b2db2905e051aa92 [file] [log] [blame]
Vincenzo Frascino00b26472019-06-21 10:52:29 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Generic userspace implementations of gettimeofday() and similar.
4 */
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>
12
13/*
14 * The generic vDSO implementation requires that gettimeofday.h
15 * provides:
16 * - __arch_get_vdso_data(): to get the vdso datapage.
17 * - __arch_get_hw_counter(): to get the hw counter based on the
18 * clock_mode.
19 * - gettimeofday_fallback(): fallback for gettimeofday.
20 * - clock_gettime_fallback(): fallback for clock_gettime.
21 * - clock_getres_fallback(): fallback for clock_getres.
22 */
Vincenzo Frascino629fdf72019-06-21 10:52:36 +010023#ifdef ENABLE_COMPAT_VDSO
24#include <asm/vdso/compat_gettimeofday.h>
25#else
Vincenzo Frascino00b26472019-06-21 10:52:29 +010026#include <asm/vdso/gettimeofday.h>
Vincenzo Frascino629fdf72019-06-21 10:52:36 +010027#endif /* ENABLE_COMPAT_VDSO */
Vincenzo Frascino00b26472019-06-21 10:52:29 +010028
Thomas Gleixner9d90b932019-06-26 12:02:00 +020029#ifndef vdso_calc_delta
30/*
31 * Default implementation which works for all sane clocksources. That
32 * obviously excludes x86/TSC.
33 */
34static __always_inline
35u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult)
36{
37 return ((cycles - last) & mask) * mult;
38}
39#endif
40
Vincenzo Frascino00b26472019-06-21 10:52:29 +010041static int do_hres(const struct vdso_data *vd, clockid_t clk,
42 struct __kernel_timespec *ts)
43{
44 const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
45 u64 cycles, last, sec, ns;
46 u32 seq;
47
48 do {
49 seq = vdso_read_begin(vd);
Thomas Gleixner9d90b932019-06-26 12:02:00 +020050 cycles = __arch_get_hw_counter(vd->clock_mode);
Vincenzo Frascino00b26472019-06-21 10:52:29 +010051 ns = vdso_ts->nsec;
52 last = vd->cycle_last;
53 if (unlikely((s64)cycles < 0))
Thomas Gleixner502a5902019-07-28 15:12:53 +020054 return -1;
Thomas Gleixner9d90b932019-06-26 12:02:00 +020055
56 ns += vdso_calc_delta(cycles, last, vd->mask, vd->mult);
Vincenzo Frascino00b26472019-06-21 10:52:29 +010057 ns >>= vd->shift;
58 sec = vdso_ts->sec;
59 } while (unlikely(vdso_read_retry(vd, seq)));
60
61 /*
62 * Do this outside the loop: a race inside the loop could result
63 * in __iter_div_u64_rem() being extremely slow.
64 */
65 ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
66 ts->tv_nsec = ns;
67
68 return 0;
69}
70
Christophe Leroy8463cf82019-12-23 14:31:07 +000071static int do_coarse(const struct vdso_data *vd, clockid_t clk,
Vincenzo Frascino00b26472019-06-21 10:52:29 +010072 struct __kernel_timespec *ts)
73{
74 const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
75 u32 seq;
76
77 do {
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)));
Christophe Leroy8463cf82019-12-23 14:31:07 +000082
83 return 0;
Vincenzo Frascino00b26472019-06-21 10:52:29 +010084}
85
86static __maybe_unused int
Thomas Gleixner502a5902019-07-28 15:12:53 +020087__cvdso_clock_gettime_common(clockid_t clock, struct __kernel_timespec *ts)
Vincenzo Frascino00b26472019-06-21 10:52:29 +010088{
89 const struct vdso_data *vd = __arch_get_vdso_data();
90 u32 msk;
91
92 /* Check for negative values or invalid clocks */
93 if (unlikely((u32) clock >= MAX_CLOCKS))
Thomas Gleixner502a5902019-07-28 15:12:53 +020094 return -1;
Vincenzo Frascino00b26472019-06-21 10:52:29 +010095
96 /*
97 * Convert the clockid to a bitmask and use it to check which
98 * clocks are handled in the VDSO directly.
99 */
100 msk = 1U << clock;
Christophe Leroy8463cf82019-12-23 14:31:07 +0000101 if (likely(msk & VDSO_HRES))
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100102 return do_hres(&vd[CS_HRES_COARSE], clock, ts);
Christophe Leroy8463cf82019-12-23 14:31:07 +0000103 else if (msk & VDSO_COARSE)
104 return do_coarse(&vd[CS_HRES_COARSE], clock, ts);
105 else if (msk & VDSO_RAW)
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100106 return do_hres(&vd[CS_RAW], clock, ts);
Christophe Leroy8463cf82019-12-23 14:31:07 +0000107
Thomas Gleixner502a5902019-07-28 15:12:53 +0200108 return -1;
109}
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100110
Thomas Gleixner502a5902019-07-28 15:12:53 +0200111static __maybe_unused int
112__cvdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts)
113{
114 int ret = __cvdso_clock_gettime_common(clock, ts);
115
116 if (unlikely(ret))
117 return clock_gettime_fallback(clock, ts);
118 return 0;
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100119}
120
Vincenzo Frascinobf279842019-08-30 14:58:56 +0100121#ifdef BUILD_VDSO32
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100122static __maybe_unused int
123__cvdso_clock_gettime32(clockid_t clock, struct old_timespec32 *res)
124{
125 struct __kernel_timespec ts;
126 int ret;
127
Thomas Gleixner502a5902019-07-28 15:12:53 +0200128 ret = __cvdso_clock_gettime_common(clock, &ts);
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100129
Thomas Gleixnerc60a32e2019-07-30 11:38:50 +0200130 if (unlikely(ret))
131 return clock_gettime32_fallback(clock, res);
Thomas Gleixner502a5902019-07-28 15:12:53 +0200132
Vincenzo Frascinoa2792352019-08-30 14:58:59 +0100133 /* For ret == 0 */
134 res->tv_sec = ts.tv_sec;
135 res->tv_nsec = ts.tv_nsec;
136
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100137 return ret;
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100138}
Vincenzo Frascinobf279842019-08-30 14:58:56 +0100139#endif /* BUILD_VDSO32 */
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100140
141static __maybe_unused int
142__cvdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
143{
144 const struct vdso_data *vd = __arch_get_vdso_data();
145
146 if (likely(tv != NULL)) {
147 struct __kernel_timespec ts;
148
149 if (do_hres(&vd[CS_HRES_COARSE], CLOCK_REALTIME, &ts))
150 return gettimeofday_fallback(tv, tz);
151
152 tv->tv_sec = ts.tv_sec;
153 tv->tv_usec = (u32)ts.tv_nsec / NSEC_PER_USEC;
154 }
155
156 if (unlikely(tz != NULL)) {
157 tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest;
158 tz->tz_dsttime = vd[CS_HRES_COARSE].tz_dsttime;
159 }
160
161 return 0;
162}
163
164#ifdef VDSO_HAS_TIME
Arnd Bergmann21346562019-11-05 11:10:01 +0100165static __maybe_unused __kernel_old_time_t __cvdso_time(__kernel_old_time_t *time)
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100166{
167 const struct vdso_data *vd = __arch_get_vdso_data();
Arnd Bergmann21346562019-11-05 11:10:01 +0100168 __kernel_old_time_t t = READ_ONCE(vd[CS_HRES_COARSE].basetime[CLOCK_REALTIME].sec);
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100169
170 if (time)
171 *time = t;
172
173 return t;
174}
175#endif /* VDSO_HAS_TIME */
176
177#ifdef VDSO_HAS_CLOCK_GETRES
178static __maybe_unused
Thomas Gleixner502a5902019-07-28 15:12:53 +0200179int __cvdso_clock_getres_common(clockid_t clock, struct __kernel_timespec *res)
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100180{
181 const struct vdso_data *vd = __arch_get_vdso_data();
Thomas Gleixner502a5902019-07-28 15:12:53 +0200182 u64 hrtimer_res;
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100183 u32 msk;
Thomas Gleixner502a5902019-07-28 15:12:53 +0200184 u64 ns;
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100185
186 /* Check for negative values or invalid clocks */
187 if (unlikely((u32) clock >= MAX_CLOCKS))
Thomas Gleixner502a5902019-07-28 15:12:53 +0200188 return -1;
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100189
Thomas Gleixner502a5902019-07-28 15:12:53 +0200190 hrtimer_res = READ_ONCE(vd[CS_HRES_COARSE].hrtimer_res);
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100191 /*
192 * Convert the clockid to a bitmask and use it to check which
193 * clocks are handled in the VDSO directly.
194 */
195 msk = 1U << clock;
196 if (msk & VDSO_HRES) {
197 /*
198 * Preserves the behaviour of posix_get_hrtimer_res().
199 */
200 ns = hrtimer_res;
201 } else if (msk & VDSO_COARSE) {
202 /*
203 * Preserves the behaviour of posix_get_coarse_res().
204 */
205 ns = LOW_RES_NSEC;
206 } else if (msk & VDSO_RAW) {
207 /*
208 * Preserves the behaviour of posix_get_hrtimer_res().
209 */
210 ns = hrtimer_res;
211 } else {
Thomas Gleixner502a5902019-07-28 15:12:53 +0200212 return -1;
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100213 }
214
Thomas Gleixner1638b8f2019-10-21 12:07:15 +0200215 if (likely(res)) {
216 res->tv_sec = 0;
217 res->tv_nsec = ns;
218 }
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100219 return 0;
Thomas Gleixner502a5902019-07-28 15:12:53 +0200220}
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100221
Vincenzo Frascinoffd08732019-11-28 11:17:19 +0000222static __maybe_unused
Thomas Gleixner502a5902019-07-28 15:12:53 +0200223int __cvdso_clock_getres(clockid_t clock, struct __kernel_timespec *res)
224{
225 int ret = __cvdso_clock_getres_common(clock, res);
226
227 if (unlikely(ret))
228 return clock_getres_fallback(clock, res);
229 return 0;
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100230}
231
Vincenzo Frascinobf279842019-08-30 14:58:56 +0100232#ifdef BUILD_VDSO32
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100233static __maybe_unused int
234__cvdso_clock_getres_time32(clockid_t clock, struct old_timespec32 *res)
235{
236 struct __kernel_timespec ts;
237 int ret;
238
Thomas Gleixner502a5902019-07-28 15:12:53 +0200239 ret = __cvdso_clock_getres_common(clock, &ts);
Thomas Gleixnerc60a32e2019-07-30 11:38:50 +0200240
Thomas Gleixnerc60a32e2019-07-30 11:38:50 +0200241 if (unlikely(ret))
242 return clock_getres32_fallback(clock, res);
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100243
Vincenzo Frascinoa2792352019-08-30 14:58:59 +0100244 if (likely(res)) {
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100245 res->tv_sec = ts.tv_sec;
246 res->tv_nsec = ts.tv_nsec;
247 }
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100248 return ret;
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100249}
Vincenzo Frascinobf279842019-08-30 14:58:56 +0100250#endif /* BUILD_VDSO32 */
Vincenzo Frascino00b26472019-06-21 10:52:29 +0100251#endif /* VDSO_HAS_CLOCK_GETRES */