2 * Copyright 2006 Andi Kleen, SUSE Labs.
3 * Subject to the GNU Public License, v.2
5 * Fast user context implementation of clock_gettime and gettimeofday.
7 * The code should have no internal unresolved relocations.
8 * Check with readelf after changing.
9 * Also alternative() doesn't work.
12 /* Disable profiling for userspace code: */
13 #define DISABLE_BRANCH_PROFILING
15 #include <linux/kernel.h>
16 #include <linux/posix-timers.h>
17 #include <linux/time.h>
18 #include <linux/string.h>
19 #include <asm/vsyscall.h>
20 #include <asm/vgtod.h>
21 #include <asm/timex.h>
23 #include <asm/unistd.h>
27 #define gtod vdso_vsyscall_gtod_data
29 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
32 asm("syscall" : "=a" (ret) :
33 "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
37 notrace static inline long vgetns(void)
40 cycles_t (*vread)(void);
41 vread = gtod->clock.vread;
42 v = (vread() - gtod->clock.cycle_last) & gtod->clock.mask;
43 return (v * gtod->clock.mult) >> gtod->clock.shift;
46 notrace static noinline int do_realtime(struct timespec *ts)
48 unsigned long seq, ns;
50 seq = read_seqbegin(>od->lock);
51 ts->tv_sec = gtod->wall_time_sec;
52 ts->tv_nsec = gtod->wall_time_nsec;
54 } while (unlikely(read_seqretry(>od->lock, seq)));
55 timespec_add_ns(ts, ns);
59 /* Copy of the version in kernel/time.c which we cannot directly access */
61 vset_normalized_timespec(struct timespec *ts, long sec, long nsec)
63 while (nsec >= NSEC_PER_SEC) {
75 notrace static noinline int do_monotonic(struct timespec *ts)
77 unsigned long seq, ns, secs;
79 seq = read_seqbegin(>od->lock);
80 secs = gtod->wall_time_sec;
81 ns = gtod->wall_time_nsec + vgetns();
82 secs += gtod->wall_to_monotonic.tv_sec;
83 ns += gtod->wall_to_monotonic.tv_nsec;
84 } while (unlikely(read_seqretry(>od->lock, seq)));
85 vset_normalized_timespec(ts, secs, ns);
89 notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
91 if (likely(gtod->sysctl_enabled && gtod->clock.vread))
94 return do_realtime(ts);
96 return do_monotonic(ts);
98 return vdso_fallback_gettime(clock, ts);
100 int clock_gettime(clockid_t, struct timespec *)
101 __attribute__((weak, alias("__vdso_clock_gettime")));
103 notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
106 if (likely(gtod->sysctl_enabled && gtod->clock.vread)) {
107 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
108 offsetof(struct timespec, tv_nsec) ||
109 sizeof(*tv) != sizeof(struct timespec));
110 do_realtime((struct timespec *)tv);
112 if (unlikely(tz != NULL)) {
113 /* Avoid memcpy. Some old compilers fail to inline it */
114 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
115 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
119 asm("syscall" : "=a" (ret) :
120 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
123 int gettimeofday(struct timeval *, struct timezone *)
124 __attribute__((weak, alias("__vdso_gettimeofday")));