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 |
Andy Lutomirski | f40c330 | 2014-05-05 12:19:36 -0700 | [diff] [blame^] | 33 | extern u8 hpet_page |
| 34 | __attribute__((visibility("hidden"))); |
| 35 | |
| 36 | static notrace cycle_t vread_hpet(void) |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 37 | { |
Andy Lutomirski | f40c330 | 2014-05-05 12:19:36 -0700 | [diff] [blame^] | 38 | return *(const volatile u32 *)(&hpet_page + HPET_COUNTER); |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 39 | } |
| 40 | #endif |
| 41 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 42 | #ifndef BUILD_VDSO32 |
| 43 | |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 44 | #include <linux/kernel.h> |
| 45 | #include <asm/vsyscall.h> |
| 46 | #include <asm/fixmap.h> |
| 47 | #include <asm/pvclock.h> |
| 48 | |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 49 | notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) |
| 50 | { |
| 51 | long ret; |
| 52 | asm("syscall" : "=a" (ret) : |
| 53 | "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory"); |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) |
| 58 | { |
| 59 | long ret; |
| 60 | |
| 61 | asm("syscall" : "=a" (ret) : |
| 62 | "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory"); |
| 63 | return ret; |
| 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 */ |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 127 | last = gtod->cycle_last; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 128 | |
| 129 | if (likely(ret >= last)) |
| 130 | return ret; |
| 131 | |
| 132 | return last; |
| 133 | } |
| 134 | #endif |
| 135 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 136 | #else |
| 137 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 138 | notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) |
| 139 | { |
| 140 | long ret; |
| 141 | |
| 142 | asm( |
| 143 | "mov %%ebx, %%edx \n" |
| 144 | "mov %2, %%ebx \n" |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 145 | "call __kernel_vsyscall \n" |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 146 | "mov %%edx, %%ebx \n" |
| 147 | : "=a" (ret) |
| 148 | : "0" (__NR_clock_gettime), "g" (clock), "c" (ts) |
| 149 | : "memory", "edx"); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) |
| 154 | { |
| 155 | long ret; |
| 156 | |
| 157 | asm( |
| 158 | "mov %%ebx, %%edx \n" |
| 159 | "mov %2, %%ebx \n" |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 160 | "call __kernel_vsyscall \n" |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 161 | "mov %%edx, %%ebx \n" |
| 162 | : "=a" (ret) |
| 163 | : "0" (__NR_gettimeofday), "g" (tv), "c" (tz) |
| 164 | : "memory", "edx"); |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | #ifdef CONFIG_PARAVIRT_CLOCK |
| 169 | |
| 170 | static notrace cycle_t vread_pvclock(int *mode) |
| 171 | { |
| 172 | *mode = VCLOCK_NONE; |
| 173 | return 0; |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #endif |
| 178 | |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 179 | notrace static cycle_t vread_tsc(void) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 180 | { |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 181 | cycle_t ret; |
| 182 | u64 last; |
| 183 | |
| 184 | /* |
| 185 | * Empirically, a fence (of type that depends on the CPU) |
| 186 | * before rdtsc is enough to ensure that rdtsc is ordered |
| 187 | * with respect to loads. The various CPU manuals are unclear |
| 188 | * as to whether rdtsc can be reordered with later loads, |
| 189 | * but no one has ever seen it happen. |
| 190 | */ |
| 191 | rdtsc_barrier(); |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 192 | ret = (cycle_t)__native_read_tsc(); |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 193 | |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 194 | last = gtod->cycle_last; |
Stefani Seibold | 411f790 | 2014-03-17 23:22:03 +0100 | [diff] [blame] | 195 | |
| 196 | if (likely(ret >= last)) |
| 197 | return ret; |
| 198 | |
| 199 | /* |
| 200 | * GCC likes to generate cmov here, but this branch is extremely |
| 201 | * predictable (it's just a funciton of time and the likely is |
| 202 | * very likely) and there's a data dependence, so force GCC |
| 203 | * to generate a branch instead. I don't barrier() because |
| 204 | * we don't actually need a barrier, and if this function |
| 205 | * ever gets inlined it will generate worse code. |
| 206 | */ |
| 207 | asm volatile (""); |
| 208 | return last; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 211 | notrace static inline u64 vgetsns(int *mode) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 212 | { |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 213 | u64 v; |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 214 | cycles_t cycles; |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 215 | |
| 216 | if (gtod->vclock_mode == VCLOCK_TSC) |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 217 | cycles = vread_tsc(); |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 218 | #ifdef CONFIG_HPET_TIMER |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 219 | else if (gtod->vclock_mode == VCLOCK_HPET) |
Andy Lutomirski | 98d0ac3 | 2011-07-14 06:47:22 -0400 | [diff] [blame] | 220 | cycles = vread_hpet(); |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 221 | #endif |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 222 | #ifdef CONFIG_PARAVIRT_CLOCK |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 223 | else if (gtod->vclock_mode == VCLOCK_PVCLOCK) |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 224 | cycles = vread_pvclock(mode); |
| 225 | #endif |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 226 | else |
| 227 | return 0; |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 228 | v = (cycles - gtod->cycle_last) & gtod->mask; |
| 229 | return v * gtod->mult; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Andy Lutomirski | 5f29347 | 2012-03-22 21:15:52 -0700 | [diff] [blame] | 232 | /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ |
| 233 | notrace static int __always_inline do_realtime(struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 234 | { |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 235 | unsigned long seq; |
| 236 | u64 ns; |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 237 | int mode; |
| 238 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 239 | do { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 240 | seq = gtod_read_begin(gtod); |
| 241 | mode = gtod->vclock_mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 242 | ts->tv_sec = gtod->wall_time_sec; |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 243 | ns = gtod->wall_time_snsec; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 244 | ns += vgetsns(&mode); |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 245 | ns >>= gtod->shift; |
| 246 | } while (unlikely(gtod_read_retry(gtod, seq))); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 247 | |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 248 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); |
| 249 | ts->tv_nsec = ns; |
| 250 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 251 | return mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 254 | notrace static int __always_inline do_monotonic(struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 255 | { |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 256 | unsigned long seq; |
| 257 | u64 ns; |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 258 | int mode; |
| 259 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 260 | do { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 261 | seq = gtod_read_begin(gtod); |
| 262 | mode = gtod->vclock_mode; |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 263 | ts->tv_sec = gtod->monotonic_time_sec; |
John Stultz | 650ea02 | 2012-09-04 16:14:46 -0400 | [diff] [blame] | 264 | ns = gtod->monotonic_time_snsec; |
Marcelo Tosatti | 51c19b4 | 2012-11-27 23:28:57 -0200 | [diff] [blame] | 265 | ns += vgetsns(&mode); |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 266 | ns >>= gtod->shift; |
| 267 | } while (unlikely(gtod_read_retry(gtod, seq))); |
| 268 | |
| 269 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); |
| 270 | ts->tv_nsec = ns; |
Andy Lutomirski | 0f51f28 | 2011-05-23 09:31:27 -0400 | [diff] [blame] | 271 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 272 | return mode; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 275 | notrace static void do_realtime_coarse(struct timespec *ts) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 276 | { |
| 277 | unsigned long seq; |
| 278 | do { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 279 | seq = gtod_read_begin(gtod); |
| 280 | ts->tv_sec = gtod->wall_time_coarse_sec; |
| 281 | ts->tv_nsec = gtod->wall_time_coarse_nsec; |
| 282 | } while (unlikely(gtod_read_retry(gtod, seq))); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 285 | notrace static void do_monotonic_coarse(struct timespec *ts) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 286 | { |
Andy Lutomirski | 91ec87d | 2012-03-22 21:15:51 -0700 | [diff] [blame] | 287 | unsigned long seq; |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 288 | do { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 289 | seq = gtod_read_begin(gtod); |
| 290 | ts->tv_sec = gtod->monotonic_time_coarse_sec; |
| 291 | ts->tv_nsec = gtod->monotonic_time_coarse_nsec; |
| 292 | } while (unlikely(gtod_read_retry(gtod, seq))); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 295 | notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 296 | { |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 297 | switch (clock) { |
| 298 | case CLOCK_REALTIME: |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 299 | if (do_realtime(ts) == VCLOCK_NONE) |
| 300 | goto fallback; |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 301 | break; |
| 302 | case CLOCK_MONOTONIC: |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 303 | if (do_monotonic(ts) == VCLOCK_NONE) |
| 304 | goto fallback; |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 305 | break; |
| 306 | case CLOCK_REALTIME_COARSE: |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 307 | do_realtime_coarse(ts); |
| 308 | break; |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 309 | case CLOCK_MONOTONIC_COARSE: |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 310 | do_monotonic_coarse(ts); |
| 311 | break; |
| 312 | default: |
| 313 | goto fallback; |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 314 | } |
| 315 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 316 | return 0; |
Stefani Seibold | ce39c64 | 2014-03-17 23:22:04 +0100 | [diff] [blame] | 317 | fallback: |
| 318 | return vdso_fallback_gettime(clock, ts); |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 319 | } |
| 320 | int clock_gettime(clockid_t, struct timespec *) |
| 321 | __attribute__((weak, alias("__vdso_clock_gettime"))); |
| 322 | |
Steven Rostedt | 23adec5 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 323 | notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 324 | { |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 325 | if (likely(tv != NULL)) { |
Stefani Seibold | 0df1ea2 | 2014-03-17 23:22:06 +0100 | [diff] [blame] | 326 | if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE)) |
| 327 | return vdso_fallback_gtod(tv, tz); |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 328 | tv->tv_usec /= 1000; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 329 | } |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 330 | if (unlikely(tz != NULL)) { |
Stefani Seibold | 7c03156 | 2014-03-17 23:22:10 +0100 | [diff] [blame] | 331 | tz->tz_minuteswest = gtod->tz_minuteswest; |
| 332 | tz->tz_dsttime = gtod->tz_dsttime; |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 333 | } |
| 334 | |
John Stultz | a939e81 | 2012-03-01 22:11:09 -0800 | [diff] [blame] | 335 | return 0; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 336 | } |
| 337 | int gettimeofday(struct timeval *, struct timezone *) |
| 338 | __attribute__((weak, alias("__vdso_gettimeofday"))); |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 339 | |
Andy Lutomirski | 0d7b854 | 2011-06-05 13:50:20 -0400 | [diff] [blame] | 340 | /* |
| 341 | * This will break when the xtime seconds get inaccurate, but that is |
| 342 | * unlikely |
| 343 | */ |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 344 | notrace time_t __vdso_time(time_t *t) |
| 345 | { |
Stefani Seibold | 7a59ed4 | 2014-03-17 23:22:09 +0100 | [diff] [blame] | 346 | /* This is atomic on x86 so we don't need any locks. */ |
Stefani Seibold | af8c93d | 2014-03-17 23:22:05 +0100 | [diff] [blame] | 347 | time_t result = ACCESS_ONCE(gtod->wall_time_sec); |
Andy Lutomirski | f144a6b | 2011-05-23 09:31:30 -0400 | [diff] [blame] | 348 | |
| 349 | if (t) |
| 350 | *t = result; |
| 351 | return result; |
| 352 | } |
| 353 | int time(time_t *t) |
| 354 | __attribute__((weak, alias("__vdso_time"))); |