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