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 | * |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 5 | * Fast user context implementation of clock_gettime, gettimeofday, and time. |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 6 | * |
| 7 | * The code should have no internal unresolved relocations. |
| 8 | * Check with readelf after changing. |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
Ingo Molnar | 2b7d039 | 2008-11-12 13:17:38 +0100 | [diff] [blame] | 11 | /* Disable profiling for userspace code: */ |
Steven Rostedt | 2ed84ee | 2008-11-12 15:24:24 -0500 | [diff] [blame] | 12 | #define DISABLE_BRANCH_PROFILING |
Ingo Molnar | 2b7d039 | 2008-11-12 13:17:38 +0100 | [diff] [blame] | 13 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/posix-timers.h> |
| 16 | #include <linux/time.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <asm/vsyscall.h> |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 19 | #include <asm/fixmap.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 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> |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 25 | #include <asm/pvclock.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 26 | |
Andy Lutomirski | 8c49d9a | 2011-05-23 09:31:24 -0400 | [diff] [blame] | 27 | #define gtod (&VVAR(vsyscall_gtod_data)) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 28 | |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 29 | notrace static cycle_t vread_tsc(void) |
| 30 | { |
| 31 | cycle_t ret; |
| 32 | u64 last; |
| 33 | |
| 34 | /* |
| 35 | * Empirically, a fence (of type that depends on the CPU) |
| 36 | * before rdtsc is enough to ensure that rdtsc is ordered |
| 37 | * with respect to loads. The various CPU manuals are unclear |
| 38 | * as to whether rdtsc can be reordered with later loads, |
| 39 | * but no one has ever seen it happen. |
| 40 | */ |
| 41 | rdtsc_barrier(); |
| 42 | ret = (cycle_t)vget_cycles(); |
| 43 | |
| 44 | last = VVAR(vsyscall_gtod_data).clock.cycle_last; |
| 45 | |
| 46 | if (likely(ret >= last)) |
| 47 | return ret; |
| 48 | |
| 49 | /* |
| 50 | * GCC likes to generate cmov here, but this branch is extremely |
| 51 | * predictable (it's just a funciton of time and the likely is |
| 52 | * very likely) and there's a data dependence, so force GCC |
| 53 | * to generate a branch instead. I don't barrier() because |
| 54 | * we don't actually need a barrier, and if this function |
| 55 | * ever gets inlined it will generate worse code. |
| 56 | */ |
| 57 | asm volatile (""); |
| 58 | return last; |
| 59 | } |
| 60 | |
| 61 | static notrace cycle_t vread_hpet(void) |
| 62 | { |
Satoru Takeuchi | 36dfbbf | 2013-02-15 16:58:14 +0900 | [diff] [blame] | 63 | return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + HPET_COUNTER); |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 66 | #ifdef CONFIG_PARAVIRT_CLOCK |
| 67 | |
| 68 | static notrace const struct pvclock_vsyscall_time_info *get_pvti(int cpu) |
| 69 | { |
| 70 | const struct pvclock_vsyscall_time_info *pvti_base; |
| 71 | int idx = cpu / (PAGE_SIZE/PVTI_SIZE); |
| 72 | int offset = cpu % (PAGE_SIZE/PVTI_SIZE); |
| 73 | |
| 74 | BUG_ON(PVCLOCK_FIXMAP_BEGIN + idx > PVCLOCK_FIXMAP_END); |
| 75 | |
| 76 | pvti_base = (struct pvclock_vsyscall_time_info *) |
| 77 | __fix_to_virt(PVCLOCK_FIXMAP_BEGIN+idx); |
| 78 | |
| 79 | return &pvti_base[offset]; |
| 80 | } |
| 81 | |
| 82 | static notrace cycle_t vread_pvclock(int *mode) |
| 83 | { |
| 84 | const struct pvclock_vsyscall_time_info *pvti; |
| 85 | cycle_t ret; |
| 86 | u64 last; |
| 87 | u32 version; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 88 | u8 flags; |
| 89 | unsigned cpu, cpu1; |
| 90 | |
| 91 | |
| 92 | /* |
Marcelo Tosatti | e04c5d7 | 2013-07-10 22:21:57 -0300 | [diff] [blame^] | 93 | * Note: hypervisor must guarantee that: |
| 94 | * 1. cpu ID number maps 1:1 to per-CPU pvclock time info. |
| 95 | * 2. that per-CPU pvclock time info is updated if the |
| 96 | * underlying CPU changes. |
| 97 | * 3. that version is increased whenever underlying CPU |
| 98 | * changes. |
| 99 | * |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 100 | */ |
| 101 | do { |
| 102 | cpu = __getcpu() & VGETCPU_CPU_MASK; |
| 103 | /* TODO: We can put vcpu id into higher bits of pvti.version. |
| 104 | * This will save a couple of cycles by getting rid of |
| 105 | * __getcpu() calls (Gleb). |
| 106 | */ |
| 107 | |
| 108 | pvti = get_pvti(cpu); |
| 109 | |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 110 | version = __pvclock_read_cycles(&pvti->pvti, &ret, &flags); |
| 111 | |
| 112 | /* |
| 113 | * Test we're still on the cpu as well as the version. |
| 114 | * We could have been migrated just after the first |
| 115 | * vgetcpu but before fetching the version, so we |
| 116 | * wouldn't notice a version change. |
| 117 | */ |
| 118 | cpu1 = __getcpu() & VGETCPU_CPU_MASK; |
| 119 | } while (unlikely(cpu != cpu1 || |
| 120 | (pvti->pvti.version & 1) || |
Marcelo Tosatti | e04c5d7 | 2013-07-10 22:21:57 -0300 | [diff] [blame^] | 121 | pvti->pvti.version != version)); |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 122 | |
| 123 | if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT))) |
| 124 | *mode = VCLOCK_NONE; |
| 125 | |
| 126 | /* refer to tsc.c read_tsc() comment for rationale */ |
| 127 | last = VVAR(vsyscall_gtod_data).clock.cycle_last; |
| 128 | |
| 129 | if (likely(ret >= last)) |
| 130 | return ret; |
| 131 | |
| 132 | return last; |
| 133 | } |
| 134 | #endif |
| 135 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 136 | notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 137 | { |
| 138 | long ret; |
| 139 | asm("syscall" : "=a" (ret) : |
| 140 | "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory"); |
| 141 | return ret; |
| 142 | } |
| 143 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 144 | notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) |
| 145 | { |
| 146 | long ret; |
| 147 | |
| 148 | asm("syscall" : "=a" (ret) : |
| 149 | "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory"); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 154 | notrace static inline u64 vgetsns(int *mode) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 155 | { |
Andi Kleen | 95b0867 | 2007-09-11 14:02:09 +0200 | [diff] [blame] | 156 | long v; |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 157 | cycles_t cycles; |
| 158 | if (gtod->clock.vclock_mode == VCLOCK_TSC) |
| 159 | cycles = vread_tsc(); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 160 | else if (gtod->clock.vclock_mode == VCLOCK_HPET) |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 161 | cycles = vread_hpet(); |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 162 | #ifdef CONFIG_PARAVIRT_CLOCK |
| 163 | else if (gtod->clock.vclock_mode == VCLOCK_PVCLOCK) |
| 164 | cycles = vread_pvclock(mode); |
| 165 | #endif |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 166 | else |
| 167 | return 0; |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 168 | v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask; |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 169 | return v * gtod->clock.mult; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Andy Lutomirski | 5f29347 | 2012-03-22 21:15:52 -0700 | [diff] [blame] | 172 | /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ |
| 173 | notrace static int __always_inline do_realtime(struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 174 | { |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 175 | unsigned long seq; |
| 176 | u64 ns; |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 177 | int mode; |
| 178 | |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 179 | ts->tv_nsec = 0; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 180 | do { |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 181 | seq = read_seqcount_begin(>od->seq); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 182 | mode = gtod->clock.vclock_mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 183 | ts->tv_sec = gtod->wall_time_sec; |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 184 | ns = gtod->wall_time_snsec; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 185 | ns += vgetsns(&mode); |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 186 | ns >>= gtod->clock.shift; |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 187 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 188 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 189 | timespec_add_ns(ts, ns); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 190 | return mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Andy Lutomirski | 5f29347 | 2012-03-22 21:15:52 -0700 | [diff] [blame] | 193 | notrace static int do_monotonic(struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 194 | { |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 195 | unsigned long seq; |
| 196 | u64 ns; |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 197 | int mode; |
| 198 | |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 199 | ts->tv_nsec = 0; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 200 | do { |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 201 | seq = read_seqcount_begin(>od->seq); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 202 | mode = gtod->clock.vclock_mode; |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 203 | ts->tv_sec = gtod->monotonic_time_sec; |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 204 | ns = gtod->monotonic_time_snsec; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 205 | ns += vgetsns(&mode); |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 206 | ns >>= gtod->clock.shift; |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 207 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 208 | timespec_add_ns(ts, ns); |
Andy Lutomirski | 0f51f28 | 2011-05-23 09:31:27 -0400 | [diff] [blame] | 209 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 210 | return mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Andy Lutomirski | 5f29347 | 2012-03-22 21:15:52 -0700 | [diff] [blame] | 213 | notrace static int do_realtime_coarse(struct timespec *ts) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 214 | { |
| 215 | unsigned long seq; |
| 216 | do { |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 217 | seq = read_seqcount_begin(>od->seq); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 218 | ts->tv_sec = gtod->wall_time_coarse.tv_sec; |
| 219 | ts->tv_nsec = gtod->wall_time_coarse.tv_nsec; |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 220 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Andy Lutomirski | 5f29347 | 2012-03-22 21:15:52 -0700 | [diff] [blame] | 224 | notrace static int do_monotonic_coarse(struct timespec *ts) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 225 | { |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 226 | unsigned long seq; |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 227 | do { |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 228 | seq = read_seqcount_begin(>od->seq); |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 229 | ts->tv_sec = gtod->monotonic_time_coarse.tv_sec; |
| 230 | ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec; |
Thomas Gleixner | 2ab5165 | 2012-02-28 19:46:04 +0000 | [diff] [blame] | 231 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
Andy Lutomirski | 0f51f28 | 2011-05-23 09:31:27 -0400 | [diff] [blame] | 232 | |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 236 | notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 237 | { |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 238 | int ret = VCLOCK_NONE; |
| 239 | |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 240 | switch (clock) { |
| 241 | case CLOCK_REALTIME: |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 242 | ret = do_realtime(ts); |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 243 | break; |
| 244 | case CLOCK_MONOTONIC: |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 245 | ret = do_monotonic(ts); |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 246 | break; |
| 247 | case CLOCK_REALTIME_COARSE: |
| 248 | return do_realtime_coarse(ts); |
| 249 | case CLOCK_MONOTONIC_COARSE: |
| 250 | return do_monotonic_coarse(ts); |
| 251 | } |
| 252 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 253 | if (ret == VCLOCK_NONE) |
| 254 | return vdso_fallback_gettime(clock, ts); |
| 255 | return 0; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 256 | } |
| 257 | int clock_gettime(clockid_t, struct timespec *) |
| 258 | __attribute__((weak, alias("__vdso_clock_gettime"))); |
| 259 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 260 | notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 261 | { |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 262 | long ret = VCLOCK_NONE; |
| 263 | |
| 264 | if (likely(tv != NULL)) { |
| 265 | BUILD_BUG_ON(offsetof(struct timeval, tv_usec) != |
| 266 | offsetof(struct timespec, tv_nsec) || |
| 267 | sizeof(*tv) != sizeof(struct timespec)); |
| 268 | ret = do_realtime((struct timespec *)tv); |
| 269 | tv->tv_usec /= 1000; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 270 | } |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 271 | if (unlikely(tz != NULL)) { |
| 272 | /* Avoid memcpy. Some old compilers fail to inline it */ |
| 273 | tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest; |
| 274 | tz->tz_dsttime = gtod->sys_tz.tz_dsttime; |
| 275 | } |
| 276 | |
| 277 | if (ret == VCLOCK_NONE) |
| 278 | return vdso_fallback_gtod(tv, tz); |
| 279 | return 0; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 280 | } |
| 281 | int gettimeofday(struct timeval *, struct timezone *) |
| 282 | __attribute__((weak, alias("__vdso_gettimeofday"))); |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 283 | |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 284 | /* |
| 285 | * This will break when the xtime seconds get inaccurate, but that is |
| 286 | * unlikely |
| 287 | */ |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 288 | notrace time_t __vdso_time(time_t *t) |
| 289 | { |
Andy Lutomirski | 973aa81 | 2011-05-23 09:31:31 -0400 | [diff] [blame] | 290 | /* This is atomic on x86_64 so we don't need any locks. */ |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 291 | time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec); |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 292 | |
| 293 | if (t) |
| 294 | *t = result; |
| 295 | return result; |
| 296 | } |
| 297 | int time(time_t *t) |
| 298 | __attribute__((weak, alias("__vdso_time"))); |