blob: 16d686171e9af802161a82e89b3b507a7162b6c0 [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
Ingo Molnar2b7d0392008-11-12 13:17:38 +010014/* Disable profiling for userspace code: */
Steven Rostedt2ed84ee2008-11-12 15:24:24 -050015#define DISABLE_BRANCH_PROFILING
Ingo Molnar2b7d0392008-11-12 13:17:38 +010016
Stefani Seibold7a59ed42014-03-17 23:22:09 +010017#include <uapi/linux/time.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020018#include <asm/vgtod.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020019#include <asm/hpet.h>
Stefani Seibold7c031562014-03-17 23:22:10 +010020#include <asm/vvar.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020021#include <asm/unistd.h>
Stefani Seibold7c031562014-03-17 23:22:10 +010022#include <asm/msr.h>
23#include <linux/math64.h>
24#include <linux/time.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020025
Andy Lutomirski8c49d9a2011-05-23 09:31:24 -040026#define gtod (&VVAR(vsyscall_gtod_data))
Andi Kleen2aae9502007-07-21 17:10:01 +020027
Stefani Seibold7a59ed42014-03-17 23:22:09 +010028extern int __vdso_clock_gettime(clockid_t clock, struct timespec *ts);
29extern int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz);
30extern time_t __vdso_time(time_t *t);
31
Stefani Seibold7c031562014-03-17 23:22:10 +010032#ifdef CONFIG_HPET_TIMER
33static inline u32 read_hpet_counter(const volatile void *addr)
34{
35 return *(const volatile u32 *) (addr + HPET_COUNTER);
36}
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
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040046static notrace cycle_t vread_hpet(void)
47{
Stefani Seibold7c031562014-03-17 23:22:10 +010048 return read_hpet_counter((const void *)fix_to_virt(VSYSCALL_HPET));
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040049}
50
Stefani Seibold411f7902014-03-17 23:22:03 +010051notrace 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
59notrace 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 Tosatti51c19b42012-11-27 23:28:57 -020068#ifdef CONFIG_PARAVIRT_CLOCK
69
70static 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
84static 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 Tosatti51c19b42012-11-27 23:28:57 -020090 u8 flags;
91 unsigned cpu, cpu1;
92
93
94 /*
Marcelo Tosattie04c5d72013-07-10 22:21:57 -030095 * 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 Tosatti51c19b42012-11-27 23:28:57 -0200102 */
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 Tosatti51c19b42012-11-27 23:28:57 -0200112 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 Tosattie04c5d72013-07-10 22:21:57 -0300123 pvti->pvti.version != version));
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200124
125 if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT)))
126 *mode = VCLOCK_NONE;
127
128 /* refer to tsc.c read_tsc() comment for rationale */
Stefani Seibold7c031562014-03-17 23:22:10 +0100129 last = gtod->cycle_last;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200130
131 if (likely(ret >= last))
132 return ret;
133
134 return last;
135}
136#endif
137
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100138#else
139
140extern u8 hpet_page
141 __attribute__((visibility("hidden")));
142
143#ifdef CONFIG_HPET_TIMER
144static notrace cycle_t vread_hpet(void)
145{
Stefani Seibold7c031562014-03-17 23:22:10 +0100146 return read_hpet_counter((const void *)(&hpet_page));
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100147}
148#endif
149
150notrace 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"
157 "call VDSO32_vsyscall \n"
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
165notrace 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"
172 "call VDSO32_vsyscall \n"
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
182static notrace cycle_t vread_pvclock(int *mode)
183{
184 *mode = VCLOCK_NONE;
185 return 0;
186}
187#endif
188
189#endif
190
Stefani Seibold411f7902014-03-17 23:22:03 +0100191notrace static cycle_t vread_tsc(void)
Andi Kleen2aae9502007-07-21 17:10:01 +0200192{
Stefani Seibold411f7902014-03-17 23:22:03 +0100193 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 Seibold7a59ed42014-03-17 23:22:09 +0100204 ret = (cycle_t)__native_read_tsc();
Stefani Seibold411f7902014-03-17 23:22:03 +0100205
Stefani Seibold7c031562014-03-17 23:22:10 +0100206 last = gtod->cycle_last;
Stefani Seibold411f7902014-03-17 23:22:03 +0100207
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 Kleen2aae9502007-07-21 17:10:01 +0200221}
222
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200223notrace static inline u64 vgetsns(int *mode)
Andi Kleen2aae9502007-07-21 17:10:01 +0200224{
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100225 u64 v;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400226 cycles_t cycles;
Stefani Seibold7c031562014-03-17 23:22:10 +0100227
228 if (gtod->vclock_mode == VCLOCK_TSC)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400229 cycles = vread_tsc();
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100230#ifdef CONFIG_HPET_TIMER
Stefani Seibold7c031562014-03-17 23:22:10 +0100231 else if (gtod->vclock_mode == VCLOCK_HPET)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400232 cycles = vread_hpet();
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100233#endif
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200234#ifdef CONFIG_PARAVIRT_CLOCK
Stefani Seibold7c031562014-03-17 23:22:10 +0100235 else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200236 cycles = vread_pvclock(mode);
237#endif
John Stultza939e812012-03-01 22:11:09 -0800238 else
239 return 0;
Stefani Seibold7c031562014-03-17 23:22:10 +0100240 v = (cycles - gtod->cycle_last) & gtod->mask;
241 return v * gtod->mult;
Andi Kleen2aae9502007-07-21 17:10:01 +0200242}
243
Andy Lutomirski5f293472012-03-22 21:15:52 -0700244/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
245notrace static int __always_inline do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200246{
John Stultz650ea022012-09-04 16:14:46 -0400247 unsigned long seq;
248 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800249 int mode;
250
Andi Kleen2aae9502007-07-21 17:10:01 +0200251 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100252 seq = gtod_read_begin(gtod);
253 mode = gtod->vclock_mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200254 ts->tv_sec = gtod->wall_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400255 ns = gtod->wall_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200256 ns += vgetsns(&mode);
Stefani Seibold7c031562014-03-17 23:22:10 +0100257 ns >>= gtod->shift;
258 } while (unlikely(gtod_read_retry(gtod, seq)));
John Stultza939e812012-03-01 22:11:09 -0800259
Stefani Seibold7c031562014-03-17 23:22:10 +0100260 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
261 ts->tv_nsec = ns;
262
John Stultza939e812012-03-01 22:11:09 -0800263 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200264}
265
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100266notrace static int __always_inline do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200267{
John Stultz650ea022012-09-04 16:14:46 -0400268 unsigned long seq;
269 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800270 int mode;
271
Andi Kleen2aae9502007-07-21 17:10:01 +0200272 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100273 seq = gtod_read_begin(gtod);
274 mode = gtod->vclock_mode;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700275 ts->tv_sec = gtod->monotonic_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400276 ns = gtod->monotonic_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200277 ns += vgetsns(&mode);
Stefani Seibold7c031562014-03-17 23:22:10 +0100278 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 Lutomirski0f51f282011-05-23 09:31:27 -0400283
John Stultza939e812012-03-01 22:11:09 -0800284 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200285}
286
Stefani Seiboldce39c642014-03-17 23:22:04 +0100287notrace static void do_realtime_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700288{
289 unsigned long seq;
290 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100291 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 stultzda15cfd2009-08-19 19:13:34 -0700295}
296
Stefani Seiboldce39c642014-03-17 23:22:04 +0100297notrace static void do_monotonic_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700298{
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700299 unsigned long seq;
john stultzda15cfd2009-08-19 19:13:34 -0700300 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100301 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 stultzda15cfd2009-08-19 19:13:34 -0700305}
306
Steven Rostedt23adec52008-05-12 21:20:41 +0200307notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200308{
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400309 switch (clock) {
310 case CLOCK_REALTIME:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100311 if (do_realtime(ts) == VCLOCK_NONE)
312 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400313 break;
314 case CLOCK_MONOTONIC:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100315 if (do_monotonic(ts) == VCLOCK_NONE)
316 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400317 break;
318 case CLOCK_REALTIME_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100319 do_realtime_coarse(ts);
320 break;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400321 case CLOCK_MONOTONIC_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100322 do_monotonic_coarse(ts);
323 break;
324 default:
325 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400326 }
327
John Stultza939e812012-03-01 22:11:09 -0800328 return 0;
Stefani Seiboldce39c642014-03-17 23:22:04 +0100329fallback:
330 return vdso_fallback_gettime(clock, ts);
Andi Kleen2aae9502007-07-21 17:10:01 +0200331}
332int clock_gettime(clockid_t, struct timespec *)
333 __attribute__((weak, alias("__vdso_clock_gettime")));
334
Steven Rostedt23adec52008-05-12 21:20:41 +0200335notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200336{
John Stultza939e812012-03-01 22:11:09 -0800337 if (likely(tv != NULL)) {
Stefani Seibold0df1ea22014-03-17 23:22:06 +0100338 if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
339 return vdso_fallback_gtod(tv, tz);
John Stultza939e812012-03-01 22:11:09 -0800340 tv->tv_usec /= 1000;
Andi Kleen2aae9502007-07-21 17:10:01 +0200341 }
John Stultza939e812012-03-01 22:11:09 -0800342 if (unlikely(tz != NULL)) {
Stefani Seibold7c031562014-03-17 23:22:10 +0100343 tz->tz_minuteswest = gtod->tz_minuteswest;
344 tz->tz_dsttime = gtod->tz_dsttime;
John Stultza939e812012-03-01 22:11:09 -0800345 }
346
John Stultza939e812012-03-01 22:11:09 -0800347 return 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200348}
349int gettimeofday(struct timeval *, struct timezone *)
350 __attribute__((weak, alias("__vdso_gettimeofday")));
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400351
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400352/*
353 * This will break when the xtime seconds get inaccurate, but that is
354 * unlikely
355 */
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400356notrace time_t __vdso_time(time_t *t)
357{
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100358 /* This is atomic on x86 so we don't need any locks. */
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100359 time_t result = ACCESS_ONCE(gtod->wall_time_sec);
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400360
361 if (t)
362 *t = result;
363 return result;
364}
365int time(time_t *t)
366 __attribute__((weak, alias("__vdso_time")));