2 * Copyright 2006 Andi Kleen, SUSE Labs.
3 * Subject to the GNU Public License, v.2
5 * Fast user context implementation of clock_gettime, gettimeofday, and time.
7 * The code should have no internal unresolved relocations.
8 * Check with readelf after changing.
9 * Also alternative() doesn't work.
12 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
15 /* Disable profiling for userspace code: */
16 #ifndef DISABLE_BRANCH_PROFILING
17 #define DISABLE_BRANCH_PROFILING
20 #include <linux/kernel.h>
21 #include <linux/time.h>
22 #include <linux/string.h>
24 #include <asm/unistd.h>
25 #include <asm/timex.h>
26 #include <asm/clocksource.h>
31 #define TICK_PRIV_BIT (1UL << 63)
33 #define TICK_PRIV_BIT (1ULL << 63)
37 #define SYSCALL_STRING \
40 " sub %%g0, %%o0, %%o0;" \
43 #define SYSCALL_STRING \
46 " sub %%g0, %%o0, %%o0;" \
50 #define SYSCALL_CLOBBERS \
51 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \
52 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \
53 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
54 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", \
55 "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46", \
56 "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62", \
60 * Compute the vvar page's address in the process address space, and return it
61 * as a pointer to the vvar_data.
63 static notrace noinline struct vvar_data *
69 * vdso data page is the first vDSO page so grab the return address
70 * and move up a page to get to the data page.
72 ret = (unsigned long)__builtin_return_address(0);
76 return (struct vvar_data *) ret;
80 vdso_fallback_gettime(long clock, struct timespec *ts)
82 register long num __asm__("g1") = __NR_clock_gettime;
83 register long o0 __asm__("o0") = clock;
84 register long o1 __asm__("o1") = (long) ts;
86 __asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
87 "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
91 static notrace __always_inline long
92 vdso_fallback_gettimeofday(struct timeval *tv, struct timezone *tz)
94 register long num __asm__("g1") = __NR_gettimeofday;
95 register long o0 __asm__("o0") = (long) tv;
96 register long o1 __asm__("o1") = (long) tz;
98 __asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
99 "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
103 #ifdef CONFIG_SPARC64
104 static notrace noinline u64
108 __asm__ __volatile__("rd %%asr24, %0 \n"
109 ".section .vread_tick_patch, \"ax\" \n"
113 return ret & ~TICK_PRIV_BIT;
116 static notrace noinline u64
121 __asm__ __volatile__("rd %%asr24, %%g1\n\t"
122 "srlx %%g1, 32, %1\n\t"
124 ".section .vread_tick_patch, \"ax\" \n"
127 : "=&r" (lo), "=&r" (hi)
130 return lo | ((u64)hi << 32);
134 static notrace inline u64
135 vgetsns(struct vvar_data *vvar)
140 cycles = vread_tick();
141 v = (cycles - vvar->clock.cycle_last) & vvar->clock.mask;
142 return v * vvar->clock.mult;
145 static notrace noinline int
146 do_realtime(struct vvar_data *vvar, struct timespec *ts)
153 seq = vvar_read_begin(vvar);
154 ts->tv_sec = vvar->wall_time_sec;
155 ns = vvar->wall_time_snsec;
157 ns >>= vvar->clock.shift;
158 } while (unlikely(vvar_read_retry(vvar, seq)));
160 timespec_add_ns(ts, ns);
165 static notrace noinline int
166 do_monotonic(struct vvar_data *vvar, struct timespec *ts)
173 seq = vvar_read_begin(vvar);
174 ts->tv_sec = vvar->monotonic_time_sec;
175 ns = vvar->monotonic_time_snsec;
177 ns >>= vvar->clock.shift;
178 } while (unlikely(vvar_read_retry(vvar, seq)));
180 timespec_add_ns(ts, ns);
185 static notrace noinline int
186 do_realtime_coarse(struct vvar_data *vvar, struct timespec *ts)
191 seq = vvar_read_begin(vvar);
192 ts->tv_sec = vvar->wall_time_coarse_sec;
193 ts->tv_nsec = vvar->wall_time_coarse_nsec;
194 } while (unlikely(vvar_read_retry(vvar, seq)));
198 static notrace noinline int
199 do_monotonic_coarse(struct vvar_data *vvar, struct timespec *ts)
204 seq = vvar_read_begin(vvar);
205 ts->tv_sec = vvar->monotonic_time_coarse_sec;
206 ts->tv_nsec = vvar->monotonic_time_coarse_nsec;
207 } while (unlikely(vvar_read_retry(vvar, seq)));
213 __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
215 struct vvar_data *vvd = get_vvar_data();
219 if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
221 return do_realtime(vvd, ts);
222 case CLOCK_MONOTONIC:
223 if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
225 return do_monotonic(vvd, ts);
226 case CLOCK_REALTIME_COARSE:
227 return do_realtime_coarse(vvd, ts);
228 case CLOCK_MONOTONIC_COARSE:
229 return do_monotonic_coarse(vvd, ts);
232 * Unknown clock ID ? Fall back to the syscall.
234 return vdso_fallback_gettime(clock, ts);
237 clock_gettime(clockid_t, struct timespec *)
238 __attribute__((weak, alias("__vdso_clock_gettime")));
241 __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
243 struct vvar_data *vvd = get_vvar_data();
245 if (likely(vvd->vclock_mode != VCLOCK_NONE)) {
246 if (likely(tv != NULL)) {
250 } *tstv = (union tstv_t *) tv;
251 do_realtime(vvd, &tstv->ts);
253 * Assign before dividing to ensure that the division is
254 * done in the type of tv_usec, not tv_nsec.
256 * There cannot be > 1 billion usec in a second:
257 * do_realtime() has already distributed such overflow
258 * into tv_sec. So we can assign it to an int safely.
260 tstv->tv.tv_usec = tstv->ts.tv_nsec;
261 tstv->tv.tv_usec /= 1000;
263 if (unlikely(tz != NULL)) {
264 /* Avoid memcpy. Some old compilers fail to inline it */
265 tz->tz_minuteswest = vvd->tz_minuteswest;
266 tz->tz_dsttime = vvd->tz_dsttime;
270 return vdso_fallback_gettimeofday(tv, tz);
273 gettimeofday(struct timeval *, struct timezone *)
274 __attribute__((weak, alias("__vdso_gettimeofday")));