100b26474SVincenzo Frascino // SPDX-License-Identifier: GPL-2.0
200b26474SVincenzo Frascino /*
300b26474SVincenzo Frascino * Generic userspace implementations of gettimeofday() and similar.
400b26474SVincenzo Frascino */
500b26474SVincenzo Frascino #include <vdso/datapage.h>
600b26474SVincenzo Frascino #include <vdso/helpers.h>
700b26474SVincenzo Frascino
85b26ef66SAdrian Hunter #ifndef vdso_calc_ns
9c8e3a8b6SAdrian Hunter
10c8e3a8b6SAdrian Hunter #ifdef VDSO_DELTA_NOMASK
118ff1e6c5SAdrian Hunter # define VDSO_DELTA_MASK(vd) ULLONG_MAX
12c8e3a8b6SAdrian Hunter #else
135b26ef66SAdrian Hunter # define VDSO_DELTA_MASK(vd) (vd->mask)
149d90b93bSThomas Gleixner #endif
159d90b93bSThomas Gleixner
16456e3788SAdrian Hunter #ifdef CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT
vdso_delta_ok(const struct vdso_data * vd,u64 delta)17456e3788SAdrian Hunter static __always_inline bool vdso_delta_ok(const struct vdso_data *vd, u64 delta)
18456e3788SAdrian Hunter {
19456e3788SAdrian Hunter return delta < vd->max_cycles;
20456e3788SAdrian Hunter }
21456e3788SAdrian Hunter #else
vdso_delta_ok(const struct vdso_data * vd,u64 delta)22456e3788SAdrian Hunter static __always_inline bool vdso_delta_ok(const struct vdso_data *vd, u64 delta)
23456e3788SAdrian Hunter {
24456e3788SAdrian Hunter return true;
25456e3788SAdrian Hunter }
26456e3788SAdrian Hunter #endif
27456e3788SAdrian Hunter
288345228cSChristophe Leroy #ifndef vdso_shift_ns
vdso_shift_ns(u64 ns,u32 shift)298345228cSChristophe Leroy static __always_inline u64 vdso_shift_ns(u64 ns, u32 shift)
308345228cSChristophe Leroy {
318345228cSChristophe Leroy return ns >> shift;
328345228cSChristophe Leroy }
338345228cSChristophe Leroy #endif
348345228cSChristophe Leroy
355b26ef66SAdrian Hunter /*
365b26ef66SAdrian Hunter * Default implementation which works for all sane clocksources. That
375b26ef66SAdrian Hunter * obviously excludes x86/TSC.
385b26ef66SAdrian Hunter */
vdso_calc_ns(const struct vdso_data * vd,u64 cycles,u64 base)395b26ef66SAdrian Hunter static __always_inline u64 vdso_calc_ns(const struct vdso_data *vd, u64 cycles, u64 base)
405b26ef66SAdrian Hunter {
415b26ef66SAdrian Hunter u64 delta = (cycles - vd->cycle_last) & VDSO_DELTA_MASK(vd);
425b26ef66SAdrian Hunter
43456e3788SAdrian Hunter if (likely(vdso_delta_ok(vd, delta)))
445b26ef66SAdrian Hunter return vdso_shift_ns((delta * vd->mult) + base, vd->shift);
45456e3788SAdrian Hunter
46456e3788SAdrian Hunter return mul_u64_u32_add_u64_shr(delta, vd->mult, base, vd->shift);
475b26ef66SAdrian Hunter }
485b26ef66SAdrian Hunter #endif /* vdso_calc_ns */
495b26ef66SAdrian Hunter
501dff4156SThomas Gleixner #ifndef __arch_vdso_hres_capable
__arch_vdso_hres_capable(void)511dff4156SThomas Gleixner static inline bool __arch_vdso_hres_capable(void)
521dff4156SThomas Gleixner {
531dff4156SThomas Gleixner return true;
541dff4156SThomas Gleixner }
551dff4156SThomas Gleixner #endif
561dff4156SThomas Gleixner
57ae12e085SChristophe Leroy #ifndef vdso_clocksource_ok
vdso_clocksource_ok(const struct vdso_data * vd)58ae12e085SChristophe Leroy static inline bool vdso_clocksource_ok(const struct vdso_data *vd)
59ae12e085SChristophe Leroy {
60ae12e085SChristophe Leroy return vd->clock_mode != VDSO_CLOCKMODE_NONE;
61ae12e085SChristophe Leroy }
62ae12e085SChristophe Leroy #endif
63ae12e085SChristophe Leroy
6472ce7780SThomas Gleixner #ifndef vdso_cycles_ok
vdso_cycles_ok(u64 cycles)6572ce7780SThomas Gleixner static inline bool vdso_cycles_ok(u64 cycles)
6672ce7780SThomas Gleixner {
6772ce7780SThomas Gleixner return true;
6872ce7780SThomas Gleixner }
6972ce7780SThomas Gleixner #endif
7072ce7780SThomas Gleixner
71660fd04fSThomas Gleixner #ifdef CONFIG_TIME_NS
do_hres_timens(const struct vdso_data * vdns,clockid_t clk,struct __kernel_timespec * ts)7258efe9f6SChristophe Leroy static __always_inline int do_hres_timens(const struct vdso_data *vdns, clockid_t clk,
73660fd04fSThomas Gleixner struct __kernel_timespec *ts)
74660fd04fSThomas Gleixner {
75660fd04fSThomas Gleixner const struct timens_offset *offs = &vdns->offset[clk];
76660fd04fSThomas Gleixner const struct vdso_timestamp *vdso_ts;
775b26ef66SAdrian Hunter const struct vdso_data *vd;
785b26ef66SAdrian Hunter u64 cycles, ns;
79660fd04fSThomas Gleixner u32 seq;
80660fd04fSThomas Gleixner s64 sec;
81660fd04fSThomas Gleixner
82808094fcSChristophe Leroy vd = vdns - (clk == CLOCK_MONOTONIC_RAW ? CS_RAW : CS_HRES_COARSE);
83808094fcSChristophe Leroy vd = __arch_get_timens_vdso_data(vd);
84660fd04fSThomas Gleixner if (clk != CLOCK_MONOTONIC_RAW)
85660fd04fSThomas Gleixner vd = &vd[CS_HRES_COARSE];
86660fd04fSThomas Gleixner else
87660fd04fSThomas Gleixner vd = &vd[CS_RAW];
88660fd04fSThomas Gleixner vdso_ts = &vd->basetime[clk];
89660fd04fSThomas Gleixner
90660fd04fSThomas Gleixner do {
91660fd04fSThomas Gleixner seq = vdso_read_begin(vd);
92f86fd32dSThomas Gleixner
93ae12e085SChristophe Leroy if (unlikely(!vdso_clocksource_ok(vd)))
945d51bee7SThomas Gleixner return -1;
95f86fd32dSThomas Gleixner
964c5a116aSThomas Gleixner cycles = __arch_get_hw_counter(vd->clock_mode, vd);
9772ce7780SThomas Gleixner if (unlikely(!vdso_cycles_ok(cycles)))
9872ce7780SThomas Gleixner return -1;
995b26ef66SAdrian Hunter ns = vdso_calc_ns(vd, cycles, vdso_ts->nsec);
100660fd04fSThomas Gleixner sec = vdso_ts->sec;
101660fd04fSThomas Gleixner } while (unlikely(vdso_read_retry(vd, seq)));
102660fd04fSThomas Gleixner
103660fd04fSThomas Gleixner /* Add the namespace offset */
104660fd04fSThomas Gleixner sec += offs->sec;
105660fd04fSThomas Gleixner ns += offs->nsec;
106660fd04fSThomas Gleixner
107660fd04fSThomas Gleixner /*
108660fd04fSThomas Gleixner * Do this outside the loop: a race inside the loop could result
109660fd04fSThomas Gleixner * in __iter_div_u64_rem() being extremely slow.
110660fd04fSThomas Gleixner */
111660fd04fSThomas Gleixner ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
112660fd04fSThomas Gleixner ts->tv_nsec = ns;
113660fd04fSThomas Gleixner
114660fd04fSThomas Gleixner return 0;
115660fd04fSThomas Gleixner }
116660fd04fSThomas Gleixner #else
117808094fcSChristophe Leroy static __always_inline
__arch_get_timens_vdso_data(const struct vdso_data * vd)118808094fcSChristophe Leroy const struct vdso_data *__arch_get_timens_vdso_data(const struct vdso_data *vd)
119660fd04fSThomas Gleixner {
120660fd04fSThomas Gleixner return NULL;
121660fd04fSThomas Gleixner }
122660fd04fSThomas Gleixner
do_hres_timens(const struct vdso_data * vdns,clockid_t clk,struct __kernel_timespec * ts)12358efe9f6SChristophe Leroy static __always_inline int do_hres_timens(const struct vdso_data *vdns, clockid_t clk,
124660fd04fSThomas Gleixner struct __kernel_timespec *ts)
125660fd04fSThomas Gleixner {
126660fd04fSThomas Gleixner return -EINVAL;
127660fd04fSThomas Gleixner }
128660fd04fSThomas Gleixner #endif
129660fd04fSThomas Gleixner
do_hres(const struct vdso_data * vd,clockid_t clk,struct __kernel_timespec * ts)130c966533fSAndrei Vagin static __always_inline int do_hres(const struct vdso_data *vd, clockid_t clk,
13100b26474SVincenzo Frascino struct __kernel_timespec *ts)
13200b26474SVincenzo Frascino {
13300b26474SVincenzo Frascino const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
1345b26ef66SAdrian Hunter u64 cycles, sec, ns;
13500b26474SVincenzo Frascino u32 seq;
13600b26474SVincenzo Frascino
1371dff4156SThomas Gleixner /* Allows to compile the high resolution parts out */
1381dff4156SThomas Gleixner if (!__arch_vdso_hres_capable())
1391dff4156SThomas Gleixner return -1;
1401dff4156SThomas Gleixner
14100b26474SVincenzo Frascino do {
142660fd04fSThomas Gleixner /*
143*f48955e0SAnna-Maria Behnsen * Open coded function vdso_read_begin() to handle
144*f48955e0SAnna-Maria Behnsen * VDSO_CLOCKMODE_TIMENS. Time namespace enabled tasks have a
145*f48955e0SAnna-Maria Behnsen * special VVAR page installed which has vd->seq set to 1 and
146*f48955e0SAnna-Maria Behnsen * vd->clock_mode set to VDSO_CLOCKMODE_TIMENS. For non time
147*f48955e0SAnna-Maria Behnsen * namespace affected tasks this does not affect performance
148*f48955e0SAnna-Maria Behnsen * because if vd->seq is odd, i.e. a concurrent update is in
149*f48955e0SAnna-Maria Behnsen * progress the extra check for vd->clock_mode is just a few
150*f48955e0SAnna-Maria Behnsen * extra instructions while spin waiting for vd->seq to become
151660fd04fSThomas Gleixner * even again.
152660fd04fSThomas Gleixner */
153660fd04fSThomas Gleixner while (unlikely((seq = READ_ONCE(vd->seq)) & 1)) {
154660fd04fSThomas Gleixner if (IS_ENABLED(CONFIG_TIME_NS) &&
1552d6b01bdSThomas Gleixner vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
156660fd04fSThomas Gleixner return do_hres_timens(vd, clk, ts);
157660fd04fSThomas Gleixner cpu_relax();
158660fd04fSThomas Gleixner }
159660fd04fSThomas Gleixner smp_rmb();
160660fd04fSThomas Gleixner
161ae12e085SChristophe Leroy if (unlikely(!vdso_clocksource_ok(vd)))
1625d51bee7SThomas Gleixner return -1;
163f86fd32dSThomas Gleixner
1644c5a116aSThomas Gleixner cycles = __arch_get_hw_counter(vd->clock_mode, vd);
16572ce7780SThomas Gleixner if (unlikely(!vdso_cycles_ok(cycles)))
16672ce7780SThomas Gleixner return -1;
1675b26ef66SAdrian Hunter ns = vdso_calc_ns(vd, cycles, vdso_ts->nsec);
16800b26474SVincenzo Frascino sec = vdso_ts->sec;
16900b26474SVincenzo Frascino } while (unlikely(vdso_read_retry(vd, seq)));
17000b26474SVincenzo Frascino
17100b26474SVincenzo Frascino /*
17200b26474SVincenzo Frascino * Do this outside the loop: a race inside the loop could result
17300b26474SVincenzo Frascino * in __iter_div_u64_rem() being extremely slow.
17400b26474SVincenzo Frascino */
17500b26474SVincenzo Frascino ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
17600b26474SVincenzo Frascino ts->tv_nsec = ns;
17700b26474SVincenzo Frascino
17800b26474SVincenzo Frascino return 0;
17900b26474SVincenzo Frascino }
18000b26474SVincenzo Frascino
181660fd04fSThomas Gleixner #ifdef CONFIG_TIME_NS
do_coarse_timens(const struct vdso_data * vdns,clockid_t clk,struct __kernel_timespec * ts)18258efe9f6SChristophe Leroy static __always_inline int do_coarse_timens(const struct vdso_data *vdns, clockid_t clk,
183660fd04fSThomas Gleixner struct __kernel_timespec *ts)
184660fd04fSThomas Gleixner {
185808094fcSChristophe Leroy const struct vdso_data *vd = __arch_get_timens_vdso_data(vdns);
186660fd04fSThomas Gleixner const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
187660fd04fSThomas Gleixner const struct timens_offset *offs = &vdns->offset[clk];
188660fd04fSThomas Gleixner u64 nsec;
189660fd04fSThomas Gleixner s64 sec;
190660fd04fSThomas Gleixner s32 seq;
191660fd04fSThomas Gleixner
192660fd04fSThomas Gleixner do {
193660fd04fSThomas Gleixner seq = vdso_read_begin(vd);
194660fd04fSThomas Gleixner sec = vdso_ts->sec;
195660fd04fSThomas Gleixner nsec = vdso_ts->nsec;
196660fd04fSThomas Gleixner } while (unlikely(vdso_read_retry(vd, seq)));
197660fd04fSThomas Gleixner
198660fd04fSThomas Gleixner /* Add the namespace offset */
199660fd04fSThomas Gleixner sec += offs->sec;
200660fd04fSThomas Gleixner nsec += offs->nsec;
201660fd04fSThomas Gleixner
202660fd04fSThomas Gleixner /*
203660fd04fSThomas Gleixner * Do this outside the loop: a race inside the loop could result
204660fd04fSThomas Gleixner * in __iter_div_u64_rem() being extremely slow.
205660fd04fSThomas Gleixner */
206660fd04fSThomas Gleixner ts->tv_sec = sec + __iter_div_u64_rem(nsec, NSEC_PER_SEC, &nsec);
207660fd04fSThomas Gleixner ts->tv_nsec = nsec;
208660fd04fSThomas Gleixner return 0;
209660fd04fSThomas Gleixner }
210660fd04fSThomas Gleixner #else
do_coarse_timens(const struct vdso_data * vdns,clockid_t clk,struct __kernel_timespec * ts)21158efe9f6SChristophe Leroy static __always_inline int do_coarse_timens(const struct vdso_data *vdns, clockid_t clk,
212660fd04fSThomas Gleixner struct __kernel_timespec *ts)
213660fd04fSThomas Gleixner {
214660fd04fSThomas Gleixner return -1;
215660fd04fSThomas Gleixner }
216660fd04fSThomas Gleixner #endif
217660fd04fSThomas Gleixner
do_coarse(const struct vdso_data * vd,clockid_t clk,struct __kernel_timespec * ts)218c966533fSAndrei Vagin static __always_inline int do_coarse(const struct vdso_data *vd, clockid_t clk,
21900b26474SVincenzo Frascino struct __kernel_timespec *ts)
22000b26474SVincenzo Frascino {
22100b26474SVincenzo Frascino const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
22200b26474SVincenzo Frascino u32 seq;
22300b26474SVincenzo Frascino
22400b26474SVincenzo Frascino do {
225660fd04fSThomas Gleixner /*
226*f48955e0SAnna-Maria Behnsen * Open coded function vdso_read_begin() to handle
227*f48955e0SAnna-Maria Behnsen * VDSO_CLOCK_TIMENS. See comment in do_hres().
228660fd04fSThomas Gleixner */
229660fd04fSThomas Gleixner while ((seq = READ_ONCE(vd->seq)) & 1) {
230660fd04fSThomas Gleixner if (IS_ENABLED(CONFIG_TIME_NS) &&
2312d6b01bdSThomas Gleixner vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
232660fd04fSThomas Gleixner return do_coarse_timens(vd, clk, ts);
233660fd04fSThomas Gleixner cpu_relax();
234660fd04fSThomas Gleixner }
235660fd04fSThomas Gleixner smp_rmb();
236660fd04fSThomas Gleixner
23700b26474SVincenzo Frascino ts->tv_sec = vdso_ts->sec;
23800b26474SVincenzo Frascino ts->tv_nsec = vdso_ts->nsec;
23900b26474SVincenzo Frascino } while (unlikely(vdso_read_retry(vd, seq)));
2408463cf80SChristophe Leroy
2418463cf80SChristophe Leroy return 0;
24200b26474SVincenzo Frascino }
24300b26474SVincenzo Frascino
244b91c8c42SChristophe Leroy static __always_inline int
__cvdso_clock_gettime_common(const struct vdso_data * vd,clockid_t clock,struct __kernel_timespec * ts)245e876f0b6SChristophe Leroy __cvdso_clock_gettime_common(const struct vdso_data *vd, clockid_t clock,
246e876f0b6SChristophe Leroy struct __kernel_timespec *ts)
24700b26474SVincenzo Frascino {
24800b26474SVincenzo Frascino u32 msk;
24900b26474SVincenzo Frascino
25000b26474SVincenzo Frascino /* Check for negative values or invalid clocks */
25100b26474SVincenzo Frascino if (unlikely((u32) clock >= MAX_CLOCKS))
252502a590aSThomas Gleixner return -1;
25300b26474SVincenzo Frascino
25400b26474SVincenzo Frascino /*
25500b26474SVincenzo Frascino * Convert the clockid to a bitmask and use it to check which
25600b26474SVincenzo Frascino * clocks are handled in the VDSO directly.
25700b26474SVincenzo Frascino */
25800b26474SVincenzo Frascino msk = 1U << clock;
2598463cf80SChristophe Leroy if (likely(msk & VDSO_HRES))
260c966533fSAndrei Vagin vd = &vd[CS_HRES_COARSE];
2618463cf80SChristophe Leroy else if (msk & VDSO_COARSE)
2628463cf80SChristophe Leroy return do_coarse(&vd[CS_HRES_COARSE], clock, ts);
2638463cf80SChristophe Leroy else if (msk & VDSO_RAW)
264c966533fSAndrei Vagin vd = &vd[CS_RAW];
265c966533fSAndrei Vagin else
266502a590aSThomas Gleixner return -1;
267c966533fSAndrei Vagin
268c966533fSAndrei Vagin return do_hres(vd, clock, ts);
269502a590aSThomas Gleixner }
27000b26474SVincenzo Frascino
271502a590aSThomas Gleixner static __maybe_unused int
__cvdso_clock_gettime_data(const struct vdso_data * vd,clockid_t clock,struct __kernel_timespec * ts)272e876f0b6SChristophe Leroy __cvdso_clock_gettime_data(const struct vdso_data *vd, clockid_t clock,
273e876f0b6SChristophe Leroy struct __kernel_timespec *ts)
274502a590aSThomas Gleixner {
275e876f0b6SChristophe Leroy int ret = __cvdso_clock_gettime_common(vd, clock, ts);
276502a590aSThomas Gleixner
277502a590aSThomas Gleixner if (unlikely(ret))
27800b26474SVincenzo Frascino return clock_gettime_fallback(clock, ts);
279502a590aSThomas Gleixner return 0;
28000b26474SVincenzo Frascino }
28100b26474SVincenzo Frascino
282e876f0b6SChristophe Leroy static __maybe_unused int
__cvdso_clock_gettime(clockid_t clock,struct __kernel_timespec * ts)283e876f0b6SChristophe Leroy __cvdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts)
284e876f0b6SChristophe Leroy {
285e876f0b6SChristophe Leroy return __cvdso_clock_gettime_data(__arch_get_vdso_data(), clock, ts);
286e876f0b6SChristophe Leroy }
287e876f0b6SChristophe Leroy
288bf279849SVincenzo Frascino #ifdef BUILD_VDSO32
28900b26474SVincenzo Frascino static __maybe_unused int
__cvdso_clock_gettime32_data(const struct vdso_data * vd,clockid_t clock,struct old_timespec32 * res)290e876f0b6SChristophe Leroy __cvdso_clock_gettime32_data(const struct vdso_data *vd, clockid_t clock,
291e876f0b6SChristophe Leroy struct old_timespec32 *res)
29200b26474SVincenzo Frascino {
29300b26474SVincenzo Frascino struct __kernel_timespec ts;
29400b26474SVincenzo Frascino int ret;
29500b26474SVincenzo Frascino
296e876f0b6SChristophe Leroy ret = __cvdso_clock_gettime_common(vd, clock, &ts);
29700b26474SVincenzo Frascino
298c60a32eaSThomas Gleixner if (unlikely(ret))
299c60a32eaSThomas Gleixner return clock_gettime32_fallback(clock, res);
300502a590aSThomas Gleixner
301a279235dSVincenzo Frascino /* For ret == 0 */
30200b26474SVincenzo Frascino res->tv_sec = ts.tv_sec;
30300b26474SVincenzo Frascino res->tv_nsec = ts.tv_nsec;
304a279235dSVincenzo Frascino
30500b26474SVincenzo Frascino return ret;
30600b26474SVincenzo Frascino }
307e876f0b6SChristophe Leroy
308e876f0b6SChristophe Leroy static __maybe_unused int
__cvdso_clock_gettime32(clockid_t clock,struct old_timespec32 * res)309e876f0b6SChristophe Leroy __cvdso_clock_gettime32(clockid_t clock, struct old_timespec32 *res)
310e876f0b6SChristophe Leroy {
311e876f0b6SChristophe Leroy return __cvdso_clock_gettime32_data(__arch_get_vdso_data(), clock, res);
312e876f0b6SChristophe Leroy }
313bf279849SVincenzo Frascino #endif /* BUILD_VDSO32 */
31400b26474SVincenzo Frascino
31500b26474SVincenzo Frascino static __maybe_unused int
__cvdso_gettimeofday_data(const struct vdso_data * vd,struct __kernel_old_timeval * tv,struct timezone * tz)316e876f0b6SChristophe Leroy __cvdso_gettimeofday_data(const struct vdso_data *vd,
317e876f0b6SChristophe Leroy struct __kernel_old_timeval *tv, struct timezone *tz)
31800b26474SVincenzo Frascino {
31900b26474SVincenzo Frascino
32000b26474SVincenzo Frascino if (likely(tv != NULL)) {
32100b26474SVincenzo Frascino struct __kernel_timespec ts;
32200b26474SVincenzo Frascino
32300b26474SVincenzo Frascino if (do_hres(&vd[CS_HRES_COARSE], CLOCK_REALTIME, &ts))
32400b26474SVincenzo Frascino return gettimeofday_fallback(tv, tz);
32500b26474SVincenzo Frascino
32600b26474SVincenzo Frascino tv->tv_sec = ts.tv_sec;
32700b26474SVincenzo Frascino tv->tv_usec = (u32)ts.tv_nsec / NSEC_PER_USEC;
32800b26474SVincenzo Frascino }
32900b26474SVincenzo Frascino
33000b26474SVincenzo Frascino if (unlikely(tz != NULL)) {
331660fd04fSThomas Gleixner if (IS_ENABLED(CONFIG_TIME_NS) &&
3322d6b01bdSThomas Gleixner vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
333808094fcSChristophe Leroy vd = __arch_get_timens_vdso_data(vd);
334660fd04fSThomas Gleixner
33500b26474SVincenzo Frascino tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest;
33600b26474SVincenzo Frascino tz->tz_dsttime = vd[CS_HRES_COARSE].tz_dsttime;
33700b26474SVincenzo Frascino }
33800b26474SVincenzo Frascino
33900b26474SVincenzo Frascino return 0;
34000b26474SVincenzo Frascino }
34100b26474SVincenzo Frascino
342e876f0b6SChristophe Leroy static __maybe_unused int
__cvdso_gettimeofday(struct __kernel_old_timeval * tv,struct timezone * tz)343e876f0b6SChristophe Leroy __cvdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
34400b26474SVincenzo Frascino {
345e876f0b6SChristophe Leroy return __cvdso_gettimeofday_data(__arch_get_vdso_data(), tv, tz);
346e876f0b6SChristophe Leroy }
347e876f0b6SChristophe Leroy
348e876f0b6SChristophe Leroy #ifdef VDSO_HAS_TIME
349e876f0b6SChristophe Leroy static __maybe_unused __kernel_old_time_t
__cvdso_time_data(const struct vdso_data * vd,__kernel_old_time_t * time)350e876f0b6SChristophe Leroy __cvdso_time_data(const struct vdso_data *vd, __kernel_old_time_t *time)
351e876f0b6SChristophe Leroy {
352660fd04fSThomas Gleixner __kernel_old_time_t t;
353660fd04fSThomas Gleixner
3542d6b01bdSThomas Gleixner if (IS_ENABLED(CONFIG_TIME_NS) &&
3552d6b01bdSThomas Gleixner vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
356808094fcSChristophe Leroy vd = __arch_get_timens_vdso_data(vd);
357660fd04fSThomas Gleixner
358660fd04fSThomas Gleixner t = READ_ONCE(vd[CS_HRES_COARSE].basetime[CLOCK_REALTIME].sec);
35900b26474SVincenzo Frascino
36000b26474SVincenzo Frascino if (time)
36100b26474SVincenzo Frascino *time = t;
36200b26474SVincenzo Frascino
36300b26474SVincenzo Frascino return t;
36400b26474SVincenzo Frascino }
365e876f0b6SChristophe Leroy
__cvdso_time(__kernel_old_time_t * time)366e876f0b6SChristophe Leroy static __maybe_unused __kernel_old_time_t __cvdso_time(__kernel_old_time_t *time)
367e876f0b6SChristophe Leroy {
368e876f0b6SChristophe Leroy return __cvdso_time_data(__arch_get_vdso_data(), time);
369e876f0b6SChristophe Leroy }
37000b26474SVincenzo Frascino #endif /* VDSO_HAS_TIME */
37100b26474SVincenzo Frascino
37200b26474SVincenzo Frascino #ifdef VDSO_HAS_CLOCK_GETRES
37300b26474SVincenzo Frascino static __maybe_unused
__cvdso_clock_getres_common(const struct vdso_data * vd,clockid_t clock,struct __kernel_timespec * res)374e876f0b6SChristophe Leroy int __cvdso_clock_getres_common(const struct vdso_data *vd, clockid_t clock,
375e876f0b6SChristophe Leroy struct __kernel_timespec *res)
37600b26474SVincenzo Frascino {
37700b26474SVincenzo Frascino u32 msk;
378502a590aSThomas Gleixner u64 ns;
37900b26474SVincenzo Frascino
38000b26474SVincenzo Frascino /* Check for negative values or invalid clocks */
38100b26474SVincenzo Frascino if (unlikely((u32) clock >= MAX_CLOCKS))
382502a590aSThomas Gleixner return -1;
38300b26474SVincenzo Frascino
3842d6b01bdSThomas Gleixner if (IS_ENABLED(CONFIG_TIME_NS) &&
3852d6b01bdSThomas Gleixner vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
386808094fcSChristophe Leroy vd = __arch_get_timens_vdso_data(vd);
387660fd04fSThomas Gleixner
38800b26474SVincenzo Frascino /*
38900b26474SVincenzo Frascino * Convert the clockid to a bitmask and use it to check which
39000b26474SVincenzo Frascino * clocks are handled in the VDSO directly.
39100b26474SVincenzo Frascino */
39200b26474SVincenzo Frascino msk = 1U << clock;
393cdb7c5a9SChristophe Leroy if (msk & (VDSO_HRES | VDSO_RAW)) {
39400b26474SVincenzo Frascino /*
39500b26474SVincenzo Frascino * Preserves the behaviour of posix_get_hrtimer_res().
39600b26474SVincenzo Frascino */
39749a101d7SChristophe Leroy ns = READ_ONCE(vd[CS_HRES_COARSE].hrtimer_res);
39800b26474SVincenzo Frascino } else if (msk & VDSO_COARSE) {
39900b26474SVincenzo Frascino /*
40000b26474SVincenzo Frascino * Preserves the behaviour of posix_get_coarse_res().
40100b26474SVincenzo Frascino */
40200b26474SVincenzo Frascino ns = LOW_RES_NSEC;
40300b26474SVincenzo Frascino } else {
404502a590aSThomas Gleixner return -1;
40500b26474SVincenzo Frascino }
40600b26474SVincenzo Frascino
4071638b8f0SThomas Gleixner if (likely(res)) {
40800b26474SVincenzo Frascino res->tv_sec = 0;
40900b26474SVincenzo Frascino res->tv_nsec = ns;
4101638b8f0SThomas Gleixner }
41100b26474SVincenzo Frascino return 0;
412502a590aSThomas Gleixner }
41300b26474SVincenzo Frascino
414ffd08731SVincenzo Frascino static __maybe_unused
__cvdso_clock_getres_data(const struct vdso_data * vd,clockid_t clock,struct __kernel_timespec * res)415e876f0b6SChristophe Leroy int __cvdso_clock_getres_data(const struct vdso_data *vd, clockid_t clock,
416e876f0b6SChristophe Leroy struct __kernel_timespec *res)
417502a590aSThomas Gleixner {
418e876f0b6SChristophe Leroy int ret = __cvdso_clock_getres_common(vd, clock, res);
419502a590aSThomas Gleixner
420502a590aSThomas Gleixner if (unlikely(ret))
42100b26474SVincenzo Frascino return clock_getres_fallback(clock, res);
422502a590aSThomas Gleixner return 0;
42300b26474SVincenzo Frascino }
42400b26474SVincenzo Frascino
425e876f0b6SChristophe Leroy static __maybe_unused
__cvdso_clock_getres(clockid_t clock,struct __kernel_timespec * res)426e876f0b6SChristophe Leroy int __cvdso_clock_getres(clockid_t clock, struct __kernel_timespec *res)
427e876f0b6SChristophe Leroy {
428e876f0b6SChristophe Leroy return __cvdso_clock_getres_data(__arch_get_vdso_data(), clock, res);
429e876f0b6SChristophe Leroy }
430e876f0b6SChristophe Leroy
431bf279849SVincenzo Frascino #ifdef BUILD_VDSO32
43200b26474SVincenzo Frascino static __maybe_unused int
__cvdso_clock_getres_time32_data(const struct vdso_data * vd,clockid_t clock,struct old_timespec32 * res)433e876f0b6SChristophe Leroy __cvdso_clock_getres_time32_data(const struct vdso_data *vd, clockid_t clock,
434e876f0b6SChristophe Leroy struct old_timespec32 *res)
43500b26474SVincenzo Frascino {
43600b26474SVincenzo Frascino struct __kernel_timespec ts;
43700b26474SVincenzo Frascino int ret;
43800b26474SVincenzo Frascino
439e876f0b6SChristophe Leroy ret = __cvdso_clock_getres_common(vd, clock, &ts);
440c60a32eaSThomas Gleixner
441c60a32eaSThomas Gleixner if (unlikely(ret))
442c60a32eaSThomas Gleixner return clock_getres32_fallback(clock, res);
44300b26474SVincenzo Frascino
444a279235dSVincenzo Frascino if (likely(res)) {
44500b26474SVincenzo Frascino res->tv_sec = ts.tv_sec;
44600b26474SVincenzo Frascino res->tv_nsec = ts.tv_nsec;
44700b26474SVincenzo Frascino }
44800b26474SVincenzo Frascino return ret;
44900b26474SVincenzo Frascino }
450e876f0b6SChristophe Leroy
451e876f0b6SChristophe Leroy static __maybe_unused int
__cvdso_clock_getres_time32(clockid_t clock,struct old_timespec32 * res)452e876f0b6SChristophe Leroy __cvdso_clock_getres_time32(clockid_t clock, struct old_timespec32 *res)
453e876f0b6SChristophe Leroy {
454e876f0b6SChristophe Leroy return __cvdso_clock_getres_time32_data(__arch_get_vdso_data(),
455e876f0b6SChristophe Leroy clock, res);
456e876f0b6SChristophe Leroy }
457bf279849SVincenzo Frascino #endif /* BUILD_VDSO32 */
45800b26474SVincenzo Frascino #endif /* VDSO_HAS_CLOCK_GETRES */
459