Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 Andi Kleen, SUSE Labs. |
| 3 | * Subject to the GNU Public License, v.2 |
| 4 | * |
| 5 | * Fast user context implementation of clock_gettime and gettimeofday. |
| 6 | * |
| 7 | * The code should have no internal unresolved relocations. |
| 8 | * Check with readelf after changing. |
| 9 | * Also alternative() doesn't work. |
| 10 | */ |
| 11 | |
Ingo Molnar | 2b7d039 | 2008-11-12 13:17:38 +0100 | [diff] [blame] | 12 | /* Disable profiling for userspace code: */ |
Steven Rostedt | 2ed84ee | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 13 | #define DISABLE_BRANCH_PROFILING |
Ingo Molnar | 2b7d039 | 2008-11-12 13:17:38 +0100 | [diff] [blame] | 14 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 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> |
| 22 | #include <asm/hpet.h> |
| 23 | #include <asm/unistd.h> |
| 24 | #include <asm/io.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 25 | |
Andy Lutomirski | 8c49d9a | 2011-05-23 09:31:24 -0400 | [diff] [blame^] | 26 | #define gtod (&VVAR(vsyscall_gtod_data)) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 27 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 28 | notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 29 | { |
| 30 | long ret; |
| 31 | asm("syscall" : "=a" (ret) : |
| 32 | "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory"); |
| 33 | return ret; |
| 34 | } |
| 35 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 36 | notrace static inline long vgetns(void) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 37 | { |
Andi Kleen | 95b0867 | 2007-09-11 14:02:09 +0200 | [diff] [blame] | 38 | long v; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 39 | cycles_t (*vread)(void); |
| 40 | vread = gtod->clock.vread; |
Andi Kleen | 95b0867 | 2007-09-11 14:02:09 +0200 | [diff] [blame] | 41 | v = (vread() - gtod->clock.cycle_last) & gtod->clock.mask; |
| 42 | return (v * gtod->clock.mult) >> gtod->clock.shift; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 45 | notrace static noinline int do_realtime(struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 46 | { |
| 47 | unsigned long seq, ns; |
| 48 | do { |
| 49 | seq = read_seqbegin(>od->lock); |
| 50 | ts->tv_sec = gtod->wall_time_sec; |
| 51 | ts->tv_nsec = gtod->wall_time_nsec; |
| 52 | ns = vgetns(); |
| 53 | } while (unlikely(read_seqretry(>od->lock, seq))); |
| 54 | timespec_add_ns(ts, ns); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /* Copy of the version in kernel/time.c which we cannot directly access */ |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 59 | notrace static void |
| 60 | vset_normalized_timespec(struct timespec *ts, long sec, long nsec) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 61 | { |
| 62 | while (nsec >= NSEC_PER_SEC) { |
| 63 | nsec -= NSEC_PER_SEC; |
| 64 | ++sec; |
| 65 | } |
| 66 | while (nsec < 0) { |
| 67 | nsec += NSEC_PER_SEC; |
| 68 | --sec; |
| 69 | } |
| 70 | ts->tv_sec = sec; |
| 71 | ts->tv_nsec = nsec; |
| 72 | } |
| 73 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 74 | notrace static noinline int do_monotonic(struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 75 | { |
| 76 | unsigned long seq, ns, secs; |
| 77 | do { |
| 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); |
| 85 | return 0; |
| 86 | } |
| 87 | |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 88 | notrace static noinline int do_realtime_coarse(struct timespec *ts) |
| 89 | { |
| 90 | unsigned long seq; |
| 91 | do { |
| 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))); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | notrace static noinline int do_monotonic_coarse(struct timespec *ts) |
| 100 | { |
| 101 | unsigned long seq, ns, secs; |
| 102 | do { |
| 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); |
| 110 | return 0; |
| 111 | } |
| 112 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 113 | notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 114 | { |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 115 | if (likely(gtod->sysctl_enabled)) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 116 | switch (clock) { |
| 117 | case CLOCK_REALTIME: |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 118 | if (likely(gtod->clock.vread)) |
| 119 | return do_realtime(ts); |
| 120 | break; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 121 | case CLOCK_MONOTONIC: |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 122 | if (likely(gtod->clock.vread)) |
| 123 | return do_monotonic(ts); |
| 124 | break; |
| 125 | case CLOCK_REALTIME_COARSE: |
| 126 | return do_realtime_coarse(ts); |
| 127 | case CLOCK_MONOTONIC_COARSE: |
| 128 | return do_monotonic_coarse(ts); |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 129 | } |
| 130 | return vdso_fallback_gettime(clock, ts); |
| 131 | } |
| 132 | int clock_gettime(clockid_t, struct timespec *) |
| 133 | __attribute__((weak, alias("__vdso_clock_gettime"))); |
| 134 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 135 | notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 136 | { |
| 137 | long ret; |
| 138 | if (likely(gtod->sysctl_enabled && gtod->clock.vread)) { |
John Wright | 2f65dd4 | 2009-04-29 14:32:01 -0600 | [diff] [blame] | 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); |
| 144 | tv->tv_usec /= 1000; |
| 145 | } |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 146 | if (unlikely(tz != NULL)) { |
Andi Kleen | a128964 | 2008-05-14 16:10:42 -0700 | [diff] [blame] | 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; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | asm("syscall" : "=a" (ret) : |
| 154 | "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory"); |
| 155 | return ret; |
| 156 | } |
| 157 | int gettimeofday(struct timeval *, struct timezone *) |
| 158 | __attribute__((weak, alias("__vdso_gettimeofday"))); |