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