b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Generic userspace implementations of gettimeofday() and similar. |
| 4 | */ |
| 5 | #include <vdso/datapage.h> |
| 6 | #include <vdso/helpers.h> |
| 7 | |
| 8 | #ifndef vdso_calc_delta |
| 9 | /* |
| 10 | * Default implementation which works for all sane clocksources. That |
| 11 | * obviously excludes x86/TSC. |
| 12 | */ |
| 13 | static __always_inline |
| 14 | u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult) |
| 15 | { |
| 16 | return ((cycles - last) & mask) * mult; |
| 17 | } |
| 18 | #endif |
| 19 | |
| 20 | #ifndef __arch_vdso_hres_capable |
| 21 | static inline bool __arch_vdso_hres_capable(void) |
| 22 | { |
| 23 | return true; |
| 24 | } |
| 25 | #endif |
| 26 | |
| 27 | static __always_inline int do_hres(const struct vdso_data *vd, clockid_t clk, |
| 28 | struct __kernel_timespec *ts) |
| 29 | { |
| 30 | const struct vdso_timestamp *vdso_ts = &vd->basetime[clk]; |
| 31 | u64 cycles, last, sec, ns; |
| 32 | u32 seq; |
| 33 | |
| 34 | /* Allows to compile the high resolution parts out */ |
| 35 | if (!__arch_vdso_hres_capable()) |
| 36 | return -1; |
| 37 | |
| 38 | do { |
| 39 | seq = vdso_read_begin(vd); |
| 40 | cycles = __arch_get_hw_counter(vd->clock_mode); |
| 41 | ns = vdso_ts->nsec; |
| 42 | last = vd->cycle_last; |
| 43 | if (unlikely((s64)cycles < 0)) |
| 44 | return -1; |
| 45 | |
| 46 | ns += vdso_calc_delta(cycles, last, vd->mask, vd->mult); |
| 47 | ns >>= vd->shift; |
| 48 | sec = vdso_ts->sec; |
| 49 | } while (unlikely(vdso_read_retry(vd, seq))); |
| 50 | |
| 51 | /* |
| 52 | * Do this outside the loop: a race inside the loop could result |
| 53 | * in __iter_div_u64_rem() being extremely slow. |
| 54 | */ |
| 55 | ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); |
| 56 | ts->tv_nsec = ns; |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static __always_inline int do_coarse(const struct vdso_data *vd, clockid_t clk, |
| 62 | struct __kernel_timespec *ts) |
| 63 | { |
| 64 | const struct vdso_timestamp *vdso_ts = &vd->basetime[clk]; |
| 65 | u32 seq; |
| 66 | |
| 67 | do { |
| 68 | seq = vdso_read_begin(vd); |
| 69 | ts->tv_sec = vdso_ts->sec; |
| 70 | ts->tv_nsec = vdso_ts->nsec; |
| 71 | } while (unlikely(vdso_read_retry(vd, seq))); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static __maybe_unused int |
| 77 | __cvdso_clock_gettime_common(clockid_t clock, struct __kernel_timespec *ts) |
| 78 | { |
| 79 | const struct vdso_data *vd = __arch_get_vdso_data(); |
| 80 | u32 msk; |
| 81 | |
| 82 | /* Check for negative values or invalid clocks */ |
| 83 | if (unlikely((u32) clock >= MAX_CLOCKS)) |
| 84 | return -1; |
| 85 | |
| 86 | /* |
| 87 | * Convert the clockid to a bitmask and use it to check which |
| 88 | * clocks are handled in the VDSO directly. |
| 89 | */ |
| 90 | msk = 1U << clock; |
| 91 | if (likely(msk & VDSO_HRES)) |
| 92 | vd = &vd[CS_HRES_COARSE]; |
| 93 | else if (msk & VDSO_COARSE) |
| 94 | return do_coarse(&vd[CS_HRES_COARSE], clock, ts); |
| 95 | else if (msk & VDSO_RAW) |
| 96 | vd = &vd[CS_RAW]; |
| 97 | else |
| 98 | return -1; |
| 99 | |
| 100 | return do_hres(vd, clock, ts); |
| 101 | } |
| 102 | |
| 103 | static __maybe_unused int |
| 104 | __cvdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts) |
| 105 | { |
| 106 | int ret = __cvdso_clock_gettime_common(clock, ts); |
| 107 | |
| 108 | if (unlikely(ret)) |
| 109 | return clock_gettime_fallback(clock, ts); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | #ifdef BUILD_VDSO32 |
| 114 | static __maybe_unused int |
| 115 | __cvdso_clock_gettime32(clockid_t clock, struct old_timespec32 *res) |
| 116 | { |
| 117 | struct __kernel_timespec ts; |
| 118 | int ret; |
| 119 | |
| 120 | ret = __cvdso_clock_gettime_common(clock, &ts); |
| 121 | |
| 122 | #ifdef VDSO_HAS_32BIT_FALLBACK |
| 123 | if (unlikely(ret)) |
| 124 | return clock_gettime32_fallback(clock, res); |
| 125 | #else |
| 126 | if (unlikely(ret)) |
| 127 | ret = clock_gettime_fallback(clock, &ts); |
| 128 | #endif |
| 129 | |
| 130 | /* For ret == 0 */ |
| 131 | res->tv_sec = ts.tv_sec; |
| 132 | res->tv_nsec = ts.tv_nsec; |
| 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | #endif /* BUILD_VDSO32 */ |
| 137 | |
| 138 | static __maybe_unused int |
| 139 | __cvdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz) |
| 140 | { |
| 141 | const struct vdso_data *vd = __arch_get_vdso_data(); |
| 142 | |
| 143 | if (likely(tv != NULL)) { |
| 144 | struct __kernel_timespec ts; |
| 145 | |
| 146 | if (do_hres(&vd[CS_HRES_COARSE], CLOCK_REALTIME, &ts)) |
| 147 | return gettimeofday_fallback(tv, tz); |
| 148 | |
| 149 | tv->tv_sec = ts.tv_sec; |
| 150 | tv->tv_usec = (u32)ts.tv_nsec / NSEC_PER_USEC; |
| 151 | } |
| 152 | |
| 153 | if (unlikely(tz != NULL)) { |
| 154 | tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest; |
| 155 | tz->tz_dsttime = vd[CS_HRES_COARSE].tz_dsttime; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | #ifdef VDSO_HAS_TIME |
| 162 | static __maybe_unused __kernel_old_time_t __cvdso_time(__kernel_old_time_t *time) |
| 163 | { |
| 164 | const struct vdso_data *vd = __arch_get_vdso_data(); |
| 165 | __kernel_old_time_t t = READ_ONCE(vd[CS_HRES_COARSE].basetime[CLOCK_REALTIME].sec); |
| 166 | |
| 167 | if (time) |
| 168 | *time = t; |
| 169 | |
| 170 | return t; |
| 171 | } |
| 172 | #endif /* VDSO_HAS_TIME */ |
| 173 | |
| 174 | #ifdef VDSO_HAS_CLOCK_GETRES |
| 175 | static __maybe_unused |
| 176 | int __cvdso_clock_getres_common(clockid_t clock, struct __kernel_timespec *res) |
| 177 | { |
| 178 | const struct vdso_data *vd = __arch_get_vdso_data(); |
| 179 | u32 msk; |
| 180 | u64 ns; |
| 181 | |
| 182 | /* Check for negative values or invalid clocks */ |
| 183 | if (unlikely((u32) clock >= MAX_CLOCKS)) |
| 184 | return -1; |
| 185 | |
| 186 | /* |
| 187 | * Convert the clockid to a bitmask and use it to check which |
| 188 | * clocks are handled in the VDSO directly. |
| 189 | */ |
| 190 | msk = 1U << clock; |
| 191 | if (msk & (VDSO_HRES | VDSO_RAW)) { |
| 192 | /* |
| 193 | * Preserves the behaviour of posix_get_hrtimer_res(). |
| 194 | */ |
| 195 | ns = READ_ONCE(vd[CS_HRES_COARSE].hrtimer_res); |
| 196 | } else if (msk & VDSO_COARSE) { |
| 197 | /* |
| 198 | * Preserves the behaviour of posix_get_coarse_res(). |
| 199 | */ |
| 200 | ns = LOW_RES_NSEC; |
| 201 | } else { |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | if (likely(res)) { |
| 206 | res->tv_sec = 0; |
| 207 | res->tv_nsec = ns; |
| 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static __maybe_unused |
| 213 | int __cvdso_clock_getres(clockid_t clock, struct __kernel_timespec *res) |
| 214 | { |
| 215 | int ret = __cvdso_clock_getres_common(clock, res); |
| 216 | |
| 217 | if (unlikely(ret)) |
| 218 | return clock_getres_fallback(clock, res); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | #ifdef BUILD_VDSO32 |
| 223 | static __maybe_unused int |
| 224 | __cvdso_clock_getres_time32(clockid_t clock, struct old_timespec32 *res) |
| 225 | { |
| 226 | struct __kernel_timespec ts; |
| 227 | int ret; |
| 228 | |
| 229 | ret = __cvdso_clock_getres_common(clock, &ts); |
| 230 | |
| 231 | #ifdef VDSO_HAS_32BIT_FALLBACK |
| 232 | if (unlikely(ret)) |
| 233 | return clock_getres32_fallback(clock, res); |
| 234 | #else |
| 235 | if (unlikely(ret)) |
| 236 | ret = clock_getres_fallback(clock, &ts); |
| 237 | #endif |
| 238 | |
| 239 | if (likely(res)) { |
| 240 | res->tv_sec = ts.tv_sec; |
| 241 | res->tv_nsec = ts.tv_nsec; |
| 242 | } |
| 243 | return ret; |
| 244 | } |
| 245 | #endif /* BUILD_VDSO32 */ |
| 246 | #endif /* VDSO_HAS_CLOCK_GETRES */ |