blob: 40d2473836c923acc5705018bf3aebf50cfb12b8 [file] [log] [blame]
Andi Kleen2aae9502007-07-21 17:10:01 +02001/*
2 * Copyright 2006 Andi Kleen, SUSE Labs.
3 * Subject to the GNU Public License, v.2
4 *
Andy Lutomirskif144a6b2011-05-23 09:31:30 -04005 * Fast user context implementation of clock_gettime, gettimeofday, and time.
Andi Kleen2aae9502007-07-21 17:10:01 +02006 *
Stefani Seibold7a59ed42014-03-17 23:22:09 +01007 * 32 Bit compat layer by Stefani Seibold <stefani@seibold.net>
8 * sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany
9 *
Andi Kleen2aae9502007-07-21 17:10:01 +020010 * The code should have no internal unresolved relocations.
11 * Check with readelf after changing.
Andi Kleen2aae9502007-07-21 17:10:01 +020012 */
13
Stefani Seibold7a59ed42014-03-17 23:22:09 +010014#include <uapi/linux/time.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020015#include <asm/vgtod.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020016#include <asm/hpet.h>
Stefani Seibold7c031562014-03-17 23:22:10 +010017#include <asm/vvar.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020018#include <asm/unistd.h>
Stefani Seibold7c031562014-03-17 23:22:10 +010019#include <asm/msr.h>
20#include <linux/math64.h>
21#include <linux/time.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020022
Andy Lutomirski8c49d9a2011-05-23 09:31:24 -040023#define gtod (&VVAR(vsyscall_gtod_data))
Andi Kleen2aae9502007-07-21 17:10:01 +020024
Stefani Seibold7a59ed42014-03-17 23:22:09 +010025extern int __vdso_clock_gettime(clockid_t clock, struct timespec *ts);
26extern int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz);
27extern time_t __vdso_time(time_t *t);
28
Stefani Seibold7c031562014-03-17 23:22:10 +010029#ifdef CONFIG_HPET_TIMER
Andy Lutomirskif40c3302014-05-05 12:19:36 -070030extern u8 hpet_page
31 __attribute__((visibility("hidden")));
32
33static notrace cycle_t vread_hpet(void)
Stefani Seibold7c031562014-03-17 23:22:10 +010034{
Andy Lutomirskif40c3302014-05-05 12:19:36 -070035 return *(const volatile u32 *)(&hpet_page + HPET_COUNTER);
Stefani Seibold7c031562014-03-17 23:22:10 +010036}
37#endif
38
Stefani Seibold7a59ed42014-03-17 23:22:09 +010039#ifndef BUILD_VDSO32
40
Stefani Seibold7c031562014-03-17 23:22:10 +010041#include <linux/kernel.h>
42#include <asm/vsyscall.h>
43#include <asm/fixmap.h>
44#include <asm/pvclock.h>
45
Stefani Seibold411f7902014-03-17 23:22:03 +010046notrace 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
54notrace 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 Tosatti51c19b42012-11-27 23:28:57 -020063#ifdef CONFIG_PARAVIRT_CLOCK
64
65static 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
79static 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 Tosatti0a4e6be2015-03-23 20:21:51 -030085 u32 migrate_count;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -020086 u8 flags;
87 unsigned cpu, cpu1;
88
89
90 /*
Marcelo Tosatti0a4e6be2015-03-23 20:21:51 -030091 * 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 Tosatti51c19b42012-11-27 23:28:57 -020094 */
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ář80f7fdb2015-04-02 20:44:23 +0200102 /* 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 Tosatti51c19b42012-11-27 23:28:57 -0200106
Radim Krčmář80f7fdb2015-04-02 20:44:23 +0200107 cpu1 = cpu;
108 cpu = __getcpu() & VGETCPU_CPU_MASK;
109 } while (unlikely(cpu != cpu1));
Marcelo Tosatti0a4e6be2015-03-23 20:21:51 -0300110
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200111 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ář80f7fdb2015-04-02 20:44:23 +0200115 * - 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 Tosatti51c19b42012-11-27 23:28:57 -0200118 */
Radim Krčmář80f7fdb2015-04-02 20:44:23 +0200119 smp_rmb();
120 } while (unlikely((pvti->pvti.version & 1) ||
Marcelo Tosatti0a4e6be2015-03-23 20:21:51 -0300121 pvti->pvti.version != version ||
122 pvti->migrate_count != migrate_count));
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200123
124 if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT)))
125 *mode = VCLOCK_NONE;
126
127 /* refer to tsc.c read_tsc() comment for rationale */
Stefani Seibold7c031562014-03-17 23:22:10 +0100128 last = gtod->cycle_last;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200129
130 if (likely(ret >= last))
131 return ret;
132
133 return last;
134}
135#endif
136
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100137#else
138
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100139notrace 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 Lutomirski6f121e52014-05-05 12:19:34 -0700146 "call __kernel_vsyscall \n"
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100147 "mov %%edx, %%ebx \n"
148 : "=a" (ret)
149 : "0" (__NR_clock_gettime), "g" (clock), "c" (ts)
150 : "memory", "edx");
151 return ret;
152}
153
154notrace 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 Lutomirski6f121e52014-05-05 12:19:34 -0700161 "call __kernel_vsyscall \n"
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100162 "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
171static notrace cycle_t vread_pvclock(int *mode)
172{
173 *mode = VCLOCK_NONE;
174 return 0;
175}
176#endif
177
178#endif
179
Stefani Seibold411f7902014-03-17 23:22:03 +0100180notrace static cycle_t vread_tsc(void)
Andi Kleen2aae9502007-07-21 17:10:01 +0200181{
Stefani Seibold411f7902014-03-17 23:22:03 +0100182 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 Seibold7a59ed42014-03-17 23:22:09 +0100193 ret = (cycle_t)__native_read_tsc();
Stefani Seibold411f7902014-03-17 23:22:03 +0100194
Stefani Seibold7c031562014-03-17 23:22:10 +0100195 last = gtod->cycle_last;
Stefani Seibold411f7902014-03-17 23:22:03 +0100196
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 Kleen2aae9502007-07-21 17:10:01 +0200210}
211
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200212notrace static inline u64 vgetsns(int *mode)
Andi Kleen2aae9502007-07-21 17:10:01 +0200213{
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100214 u64 v;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400215 cycles_t cycles;
Stefani Seibold7c031562014-03-17 23:22:10 +0100216
217 if (gtod->vclock_mode == VCLOCK_TSC)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400218 cycles = vread_tsc();
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100219#ifdef CONFIG_HPET_TIMER
Stefani Seibold7c031562014-03-17 23:22:10 +0100220 else if (gtod->vclock_mode == VCLOCK_HPET)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400221 cycles = vread_hpet();
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100222#endif
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200223#ifdef CONFIG_PARAVIRT_CLOCK
Stefani Seibold7c031562014-03-17 23:22:10 +0100224 else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200225 cycles = vread_pvclock(mode);
226#endif
John Stultza939e812012-03-01 22:11:09 -0800227 else
228 return 0;
Stefani Seibold7c031562014-03-17 23:22:10 +0100229 v = (cycles - gtod->cycle_last) & gtod->mask;
230 return v * gtod->mult;
Andi Kleen2aae9502007-07-21 17:10:01 +0200231}
232
Andy Lutomirski5f293472012-03-22 21:15:52 -0700233/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
234notrace static int __always_inline do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200235{
John Stultz650ea022012-09-04 16:14:46 -0400236 unsigned long seq;
237 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800238 int mode;
239
Andi Kleen2aae9502007-07-21 17:10:01 +0200240 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100241 seq = gtod_read_begin(gtod);
242 mode = gtod->vclock_mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200243 ts->tv_sec = gtod->wall_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400244 ns = gtod->wall_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200245 ns += vgetsns(&mode);
Stefani Seibold7c031562014-03-17 23:22:10 +0100246 ns >>= gtod->shift;
247 } while (unlikely(gtod_read_retry(gtod, seq)));
John Stultza939e812012-03-01 22:11:09 -0800248
Stefani Seibold7c031562014-03-17 23:22:10 +0100249 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
250 ts->tv_nsec = ns;
251
John Stultza939e812012-03-01 22:11:09 -0800252 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200253}
254
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100255notrace static int __always_inline do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200256{
John Stultz650ea022012-09-04 16:14:46 -0400257 unsigned long seq;
258 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800259 int mode;
260
Andi Kleen2aae9502007-07-21 17:10:01 +0200261 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100262 seq = gtod_read_begin(gtod);
263 mode = gtod->vclock_mode;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700264 ts->tv_sec = gtod->monotonic_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400265 ns = gtod->monotonic_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200266 ns += vgetsns(&mode);
Stefani Seibold7c031562014-03-17 23:22:10 +0100267 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 Lutomirski0f51f282011-05-23 09:31:27 -0400272
John Stultza939e812012-03-01 22:11:09 -0800273 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200274}
275
Stefani Seiboldce39c642014-03-17 23:22:04 +0100276notrace static void do_realtime_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700277{
278 unsigned long seq;
279 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100280 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 stultzda15cfd2009-08-19 19:13:34 -0700284}
285
Stefani Seiboldce39c642014-03-17 23:22:04 +0100286notrace static void do_monotonic_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700287{
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700288 unsigned long seq;
john stultzda15cfd2009-08-19 19:13:34 -0700289 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100290 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 stultzda15cfd2009-08-19 19:13:34 -0700294}
295
Steven Rostedt23adec52008-05-12 21:20:41 +0200296notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200297{
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400298 switch (clock) {
299 case CLOCK_REALTIME:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100300 if (do_realtime(ts) == VCLOCK_NONE)
301 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400302 break;
303 case CLOCK_MONOTONIC:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100304 if (do_monotonic(ts) == VCLOCK_NONE)
305 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400306 break;
307 case CLOCK_REALTIME_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100308 do_realtime_coarse(ts);
309 break;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400310 case CLOCK_MONOTONIC_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100311 do_monotonic_coarse(ts);
312 break;
313 default:
314 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400315 }
316
John Stultza939e812012-03-01 22:11:09 -0800317 return 0;
Stefani Seiboldce39c642014-03-17 23:22:04 +0100318fallback:
319 return vdso_fallback_gettime(clock, ts);
Andi Kleen2aae9502007-07-21 17:10:01 +0200320}
321int clock_gettime(clockid_t, struct timespec *)
322 __attribute__((weak, alias("__vdso_clock_gettime")));
323
Steven Rostedt23adec52008-05-12 21:20:41 +0200324notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200325{
John Stultza939e812012-03-01 22:11:09 -0800326 if (likely(tv != NULL)) {
Stefani Seibold0df1ea22014-03-17 23:22:06 +0100327 if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
328 return vdso_fallback_gtod(tv, tz);
John Stultza939e812012-03-01 22:11:09 -0800329 tv->tv_usec /= 1000;
Andi Kleen2aae9502007-07-21 17:10:01 +0200330 }
John Stultza939e812012-03-01 22:11:09 -0800331 if (unlikely(tz != NULL)) {
Stefani Seibold7c031562014-03-17 23:22:10 +0100332 tz->tz_minuteswest = gtod->tz_minuteswest;
333 tz->tz_dsttime = gtod->tz_dsttime;
John Stultza939e812012-03-01 22:11:09 -0800334 }
335
John Stultza939e812012-03-01 22:11:09 -0800336 return 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200337}
338int gettimeofday(struct timeval *, struct timezone *)
339 __attribute__((weak, alias("__vdso_gettimeofday")));
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400340
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400341/*
342 * This will break when the xtime seconds get inaccurate, but that is
343 * unlikely
344 */
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400345notrace time_t __vdso_time(time_t *t)
346{
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100347 /* This is atomic on x86 so we don't need any locks. */
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100348 time_t result = ACCESS_ONCE(gtod->wall_time_sec);
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400349
350 if (t)
351 *t = result;
352 return result;
353}
354int time(time_t *t)
355 __attribute__((weak, alias("__vdso_time")));