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>
26 #define gtod (&VVAR(vsyscall_gtod_data))
28 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
31 asm("syscall" : "=a" (ret) :
32 "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
36 notrace static inline long vgetns(void)
39 cycles_t (*vread)(void);
40 vread = gtod->clock.vread;
41 v = (vread() - gtod->clock.cycle_last) & gtod->clock.mask;
42 return (v * gtod->clock.mult) >> gtod->clock.shift;
45 notrace static noinline int do_realtime(struct timespec *ts)
47 unsigned long seq, ns;
49 seq = read_seqbegin(>od->lock);
50 ts->tv_sec = gtod->wall_time_sec;
51 ts->tv_nsec = gtod->wall_time_nsec;
53 } while (unlikely(read_seqretry(>od->lock, seq)));
54 timespec_add_ns(ts, ns);
58 /* Copy of the version in kernel/time.c which we cannot directly access */
60 vset_normalized_timespec(struct timespec *ts, long sec, long nsec)
62 while (nsec >= NSEC_PER_SEC) {
74 notrace static noinline int do_monotonic(struct timespec *ts)
76 unsigned long seq, ns, secs;
78 seq = read_seqbegin(>od->lock);
79 secs = gtod->wall_time_sec;
80 ns = gtod->wall_time_nsec + vgetns();
81 secs += gtod->wall_to_monotonic.tv_sec;
82 ns += gtod->wall_to_monotonic.tv_nsec;
83 } while (unlikely(read_seqretry(>od->lock, seq)));
84 vset_normalized_timespec(ts, secs, ns);
88 notrace static noinline int do_realtime_coarse(struct timespec *ts)
92 seq = read_seqbegin(>od->lock);
93 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
94 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
95 } while (unlikely(read_seqretry(>od->lock, seq)));
99 notrace static noinline int do_monotonic_coarse(struct timespec *ts)
101 unsigned long seq, ns, secs;
103 seq = read_seqbegin(>od->lock);
104 secs = gtod->wall_time_coarse.tv_sec;
105 ns = gtod->wall_time_coarse.tv_nsec;
106 secs += gtod->wall_to_monotonic.tv_sec;
107 ns += gtod->wall_to_monotonic.tv_nsec;
108 } while (unlikely(read_seqretry(>od->lock, seq)));
109 vset_normalized_timespec(ts, secs, ns);
113 notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
115 if (likely(gtod->sysctl_enabled))
118 if (likely(gtod->clock.vread))
119 return do_realtime(ts);
121 case CLOCK_MONOTONIC:
122 if (likely(gtod->clock.vread))
123 return do_monotonic(ts);
125 case CLOCK_REALTIME_COARSE:
126 return do_realtime_coarse(ts);
127 case CLOCK_MONOTONIC_COARSE:
128 return do_monotonic_coarse(ts);
130 return vdso_fallback_gettime(clock, ts);
132 int clock_gettime(clockid_t, struct timespec *)
133 __attribute__((weak, alias("__vdso_clock_gettime")));
135 notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
138 if (likely(gtod->sysctl_enabled && gtod->clock.vread)) {
139 if (likely(tv != NULL)) {
140 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
141 offsetof(struct timespec, tv_nsec) ||
142 sizeof(*tv) != sizeof(struct timespec));
143 do_realtime((struct timespec *)tv);
146 if (unlikely(tz != NULL)) {
147 /* Avoid memcpy. Some old compilers fail to inline it */
148 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
149 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
153 asm("syscall" : "=a" (ret) :
154 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
157 int gettimeofday(struct timeval *, struct timezone *)
158 __attribute__((weak, alias("__vdso_gettimeofday")));