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 | * |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 7 | * 32 Bit compat layer by Stefani Seibold <stefani@seibold.net> |
| 8 | * sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany |
| 9 | * |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 10 | * The code should have no internal unresolved relocations. |
| 11 | * Check with readelf after changing. |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 14 | #include <uapi/linux/time.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 15 | #include <asm/vgtod.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 16 | #include <asm/hpet.h> |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 17 | #include <asm/vvar.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 18 | #include <asm/unistd.h> |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 19 | #include <asm/msr.h> |
| 20 | #include <linux/math64.h> |
| 21 | #include <linux/time.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 22 | |
Andy Lutomirski | 8c49d9a | 2011-05-23 09:31:24 -0400 | [diff] [blame] | 23 | #define gtod (&VVAR(vsyscall_gtod_data)) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 24 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 25 | extern int __vdso_clock_gettime(clockid_t clock, struct timespec *ts); |
| 26 | extern int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz); |
| 27 | extern time_t __vdso_time(time_t *t); |
| 28 | |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 29 | #ifdef CONFIG_HPET_TIMER |
Andy Lutomirski | f40c330 | 2014-05-05 12:19:36 -0700 | [diff] [blame] | 30 | extern u8 hpet_page |
| 31 | __attribute__((visibility("hidden"))); |
| 32 | |
| 33 | static notrace cycle_t vread_hpet(void) |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 34 | { |
Andy Lutomirski | f40c330 | 2014-05-05 12:19:36 -0700 | [diff] [blame] | 35 | return *(const volatile u32 *)(&hpet_page + HPET_COUNTER); |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 36 | } |
| 37 | #endif |
| 38 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 39 | #ifndef BUILD_VDSO32 |
| 40 | |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 41 | #include <linux/kernel.h> |
| 42 | #include <asm/vsyscall.h> |
| 43 | #include <asm/fixmap.h> |
| 44 | #include <asm/pvclock.h> |
| 45 | |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 46 | notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) |
| 47 | { |
| 48 | long ret; |
| 49 | asm("syscall" : "=a" (ret) : |
| 50 | "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory"); |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) |
| 55 | { |
| 56 | long ret; |
| 57 | |
| 58 | asm("syscall" : "=a" (ret) : |
| 59 | "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory"); |
| 60 | return ret; |
| 61 | } |
| 62 | |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 63 | #ifdef CONFIG_PARAVIRT_CLOCK |
| 64 | |
| 65 | static notrace const struct pvclock_vsyscall_time_info *get_pvti(int cpu) |
| 66 | { |
| 67 | const struct pvclock_vsyscall_time_info *pvti_base; |
| 68 | int idx = cpu / (PAGE_SIZE/PVTI_SIZE); |
| 69 | int offset = cpu % (PAGE_SIZE/PVTI_SIZE); |
| 70 | |
| 71 | BUG_ON(PVCLOCK_FIXMAP_BEGIN + idx > PVCLOCK_FIXMAP_END); |
| 72 | |
| 73 | pvti_base = (struct pvclock_vsyscall_time_info *) |
| 74 | __fix_to_virt(PVCLOCK_FIXMAP_BEGIN+idx); |
| 75 | |
| 76 | return &pvti_base[offset]; |
| 77 | } |
| 78 | |
| 79 | static notrace cycle_t vread_pvclock(int *mode) |
| 80 | { |
| 81 | const struct pvclock_vsyscall_time_info *pvti; |
| 82 | cycle_t ret; |
| 83 | u64 last; |
| 84 | u32 version; |
Marcelo Tosatti | 0a4e6be | 2015-03-23 20:21:51 -0300 | [diff] [blame] | 85 | u32 migrate_count; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 86 | u8 flags; |
| 87 | unsigned cpu, cpu1; |
| 88 | |
| 89 | |
| 90 | /* |
Marcelo Tosatti | 0a4e6be | 2015-03-23 20:21:51 -0300 | [diff] [blame] | 91 | * When looping to get a consistent (time-info, tsc) pair, we |
| 92 | * also need to deal with the possibility we can switch vcpus, |
| 93 | * so make sure we always re-fetch time-info for the current vcpu. |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 94 | */ |
| 95 | do { |
| 96 | cpu = __getcpu() & VGETCPU_CPU_MASK; |
| 97 | /* TODO: We can put vcpu id into higher bits of pvti.version. |
| 98 | * This will save a couple of cycles by getting rid of |
| 99 | * __getcpu() calls (Gleb). |
| 100 | */ |
| 101 | |
Radim Krčmář | 80f7fdb | 2015-04-02 20:44:23 +0200 | [diff] [blame] | 102 | /* Make sure migrate_count will change if we leave the VCPU. */ |
| 103 | do { |
| 104 | pvti = get_pvti(cpu); |
| 105 | migrate_count = pvti->migrate_count; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 106 | |
Radim Krčmář | 80f7fdb | 2015-04-02 20:44:23 +0200 | [diff] [blame] | 107 | cpu1 = cpu; |
| 108 | cpu = __getcpu() & VGETCPU_CPU_MASK; |
| 109 | } while (unlikely(cpu != cpu1)); |
Marcelo Tosatti | 0a4e6be | 2015-03-23 20:21:51 -0300 | [diff] [blame] | 110 | |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 111 | version = __pvclock_read_cycles(&pvti->pvti, &ret, &flags); |
| 112 | |
| 113 | /* |
| 114 | * Test we're still on the cpu as well as the version. |
Radim Krčmář | 80f7fdb | 2015-04-02 20:44:23 +0200 | [diff] [blame] | 115 | * - We must read TSC of pvti's VCPU. |
| 116 | * - KVM doesn't follow the versioning protocol, so data could |
| 117 | * change before version if we left the VCPU. |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 118 | */ |
Radim Krčmář | 80f7fdb | 2015-04-02 20:44:23 +0200 | [diff] [blame] | 119 | smp_rmb(); |
| 120 | } while (unlikely((pvti->pvti.version & 1) || |
Marcelo Tosatti | 0a4e6be | 2015-03-23 20:21:51 -0300 | [diff] [blame] | 121 | pvti->pvti.version != version || |
| 122 | pvti->migrate_count != migrate_count)); |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 123 | |
| 124 | if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT))) |
| 125 | *mode = VCLOCK_NONE; |
| 126 | |
| 127 | /* refer to tsc.c read_tsc() comment for rationale */ |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 128 | last = gtod->cycle_last; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 129 | |
| 130 | if (likely(ret >= last)) |
| 131 | return ret; |
| 132 | |
| 133 | return last; |
| 134 | } |
| 135 | #endif |
| 136 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 137 | #else |
| 138 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 139 | notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) |
| 140 | { |
| 141 | long ret; |
| 142 | |
| 143 | asm( |
| 144 | "mov %%ebx, %%edx \n" |
| 145 | "mov %2, %%ebx \n" |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 146 | "call __kernel_vsyscall \n" |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 147 | "mov %%edx, %%ebx \n" |
| 148 | : "=a" (ret) |
| 149 | : "0" (__NR_clock_gettime), "g" (clock), "c" (ts) |
| 150 | : "memory", "edx"); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) |
| 155 | { |
| 156 | long ret; |
| 157 | |
| 158 | asm( |
| 159 | "mov %%ebx, %%edx \n" |
| 160 | "mov %2, %%ebx \n" |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 161 | "call __kernel_vsyscall \n" |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 162 | "mov %%edx, %%ebx \n" |
| 163 | : "=a" (ret) |
| 164 | : "0" (__NR_gettimeofday), "g" (tv), "c" (tz) |
| 165 | : "memory", "edx"); |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | #ifdef CONFIG_PARAVIRT_CLOCK |
| 170 | |
| 171 | static notrace cycle_t vread_pvclock(int *mode) |
| 172 | { |
| 173 | *mode = VCLOCK_NONE; |
| 174 | return 0; |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | #endif |
| 179 | |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 180 | notrace static cycle_t vread_tsc(void) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 181 | { |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 182 | cycle_t ret; |
| 183 | u64 last; |
| 184 | |
| 185 | /* |
| 186 | * Empirically, a fence (of type that depends on the CPU) |
| 187 | * before rdtsc is enough to ensure that rdtsc is ordered |
| 188 | * with respect to loads. The various CPU manuals are unclear |
| 189 | * as to whether rdtsc can be reordered with later loads, |
| 190 | * but no one has ever seen it happen. |
| 191 | */ |
| 192 | rdtsc_barrier(); |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 193 | ret = (cycle_t)__native_read_tsc(); |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 194 | |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 195 | last = gtod->cycle_last; |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 196 | |
| 197 | if (likely(ret >= last)) |
| 198 | return ret; |
| 199 | |
| 200 | /* |
| 201 | * GCC likes to generate cmov here, but this branch is extremely |
| 202 | * predictable (it's just a funciton of time and the likely is |
| 203 | * very likely) and there's a data dependence, so force GCC |
| 204 | * to generate a branch instead. I don't barrier() because |
| 205 | * we don't actually need a barrier, and if this function |
| 206 | * ever gets inlined it will generate worse code. |
| 207 | */ |
| 208 | asm volatile (""); |
| 209 | return last; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 212 | notrace static inline u64 vgetsns(int *mode) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 213 | { |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 214 | u64 v; |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 215 | cycles_t cycles; |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 216 | |
| 217 | if (gtod->vclock_mode == VCLOCK_TSC) |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 218 | cycles = vread_tsc(); |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 219 | #ifdef CONFIG_HPET_TIMER |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 220 | else if (gtod->vclock_mode == VCLOCK_HPET) |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 221 | cycles = vread_hpet(); |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 222 | #endif |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 223 | #ifdef CONFIG_PARAVIRT_CLOCK |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 224 | else if (gtod->vclock_mode == VCLOCK_PVCLOCK) |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 225 | cycles = vread_pvclock(mode); |
| 226 | #endif |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 227 | else |
| 228 | return 0; |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 229 | v = (cycles - gtod->cycle_last) & gtod->mask; |
| 230 | return v * gtod->mult; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Andy Lutomirski | 5f29347 | 2012-03-22 21:15:52 -0700 | [diff] [blame] | 233 | /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ |
| 234 | notrace static int __always_inline do_realtime(struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 235 | { |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 236 | unsigned long seq; |
| 237 | u64 ns; |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 238 | int mode; |
| 239 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 240 | do { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 241 | seq = gtod_read_begin(gtod); |
| 242 | mode = gtod->vclock_mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 243 | ts->tv_sec = gtod->wall_time_sec; |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 244 | ns = gtod->wall_time_snsec; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 245 | ns += vgetsns(&mode); |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 246 | ns >>= gtod->shift; |
| 247 | } while (unlikely(gtod_read_retry(gtod, seq))); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 248 | |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 249 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); |
| 250 | ts->tv_nsec = ns; |
| 251 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 252 | return mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 255 | notrace static int __always_inline do_monotonic(struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 256 | { |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 257 | unsigned long seq; |
| 258 | u64 ns; |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 259 | int mode; |
| 260 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 261 | do { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 262 | seq = gtod_read_begin(gtod); |
| 263 | mode = gtod->vclock_mode; |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 264 | ts->tv_sec = gtod->monotonic_time_sec; |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 265 | ns = gtod->monotonic_time_snsec; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 266 | ns += vgetsns(&mode); |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 267 | ns >>= gtod->shift; |
| 268 | } while (unlikely(gtod_read_retry(gtod, seq))); |
| 269 | |
| 270 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); |
| 271 | ts->tv_nsec = ns; |
Andy Lutomirski | 0f51f28 | 2011-05-23 09:31:27 -0400 | [diff] [blame] | 272 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 273 | return mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 276 | notrace static void do_realtime_coarse(struct timespec *ts) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 277 | { |
| 278 | unsigned long seq; |
| 279 | do { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 280 | seq = gtod_read_begin(gtod); |
| 281 | ts->tv_sec = gtod->wall_time_coarse_sec; |
| 282 | ts->tv_nsec = gtod->wall_time_coarse_nsec; |
| 283 | } while (unlikely(gtod_read_retry(gtod, seq))); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 286 | notrace static void do_monotonic_coarse(struct timespec *ts) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 287 | { |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 288 | unsigned long seq; |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 289 | do { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 290 | seq = gtod_read_begin(gtod); |
| 291 | ts->tv_sec = gtod->monotonic_time_coarse_sec; |
| 292 | ts->tv_nsec = gtod->monotonic_time_coarse_nsec; |
| 293 | } while (unlikely(gtod_read_retry(gtod, seq))); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 296 | notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 297 | { |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 298 | switch (clock) { |
| 299 | case CLOCK_REALTIME: |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 300 | if (do_realtime(ts) == VCLOCK_NONE) |
| 301 | goto fallback; |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 302 | break; |
| 303 | case CLOCK_MONOTONIC: |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 304 | if (do_monotonic(ts) == VCLOCK_NONE) |
| 305 | goto fallback; |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 306 | break; |
| 307 | case CLOCK_REALTIME_COARSE: |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 308 | do_realtime_coarse(ts); |
| 309 | break; |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 310 | case CLOCK_MONOTONIC_COARSE: |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 311 | do_monotonic_coarse(ts); |
| 312 | break; |
| 313 | default: |
| 314 | goto fallback; |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 315 | } |
| 316 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 317 | return 0; |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 318 | fallback: |
| 319 | return vdso_fallback_gettime(clock, ts); |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 320 | } |
| 321 | int clock_gettime(clockid_t, struct timespec *) |
| 322 | __attribute__((weak, alias("__vdso_clock_gettime"))); |
| 323 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 324 | notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 325 | { |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 326 | if (likely(tv != NULL)) { |
Stefani Seibold | 0df1ea2 | 2014-03-17 23:22:06 +0100 | [diff] [blame] | 327 | if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE)) |
| 328 | return vdso_fallback_gtod(tv, tz); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 329 | tv->tv_usec /= 1000; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 330 | } |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 331 | if (unlikely(tz != NULL)) { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 332 | tz->tz_minuteswest = gtod->tz_minuteswest; |
| 333 | tz->tz_dsttime = gtod->tz_dsttime; |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 334 | } |
| 335 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 336 | return 0; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 337 | } |
| 338 | int gettimeofday(struct timeval *, struct timezone *) |
| 339 | __attribute__((weak, alias("__vdso_gettimeofday"))); |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 340 | |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 341 | /* |
| 342 | * This will break when the xtime seconds get inaccurate, but that is |
| 343 | * unlikely |
| 344 | */ |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 345 | notrace time_t __vdso_time(time_t *t) |
| 346 | { |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 347 | /* This is atomic on x86 so we don't need any locks. */ |
Stefani Seibold | af8c93d | 2014-03-17 23:22:05 +0100 | [diff] [blame] | 348 | time_t result = ACCESS_ONCE(gtod->wall_time_sec); |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 349 | |
| 350 | if (t) |
| 351 | *t = result; |
| 352 | return result; |
| 353 | } |
| 354 | int time(time_t *t) |
| 355 | __attribute__((weak, alias("__vdso_time"))); |