blob: b2e4f493e5b0ed8ad385ba3f3d32c7c891608970 [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
Andy Lutomirskif40c3302014-05-05 12:19:36 -070033extern u8 hpet_page
34 __attribute__((visibility("hidden")));
35
36static notrace cycle_t vread_hpet(void)
Stefani Seibold7c031562014-03-17 23:22:10 +010037{
Andy Lutomirskif40c3302014-05-05 12:19:36 -070038 return *(const volatile u32 *)(&hpet_page + HPET_COUNTER);
Stefani Seibold7c031562014-03-17 23:22:10 +010039}
40#endif
41
Stefani Seibold7a59ed42014-03-17 23:22:09 +010042#ifndef BUILD_VDSO32
43
Stefani Seibold7c031562014-03-17 23:22:10 +010044#include <linux/kernel.h>
45#include <asm/vsyscall.h>
46#include <asm/fixmap.h>
47#include <asm/pvclock.h>
48
Stefani Seibold411f7902014-03-17 23:22:03 +010049notrace 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
57notrace 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 Tosatti51c19b42012-11-27 23:28:57 -020066#ifdef CONFIG_PARAVIRT_CLOCK
67
68static 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
82static 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 Tosatti51c19b42012-11-27 23:28:57 -020088 u8 flags;
89 unsigned cpu, cpu1;
90
91
92 /*
Marcelo Tosattie04c5d72013-07-10 22:21:57 -030093 * 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 Tosatti51c19b42012-11-27 23:28:57 -0200100 */
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 Tosatti51c19b42012-11-27 23:28:57 -0200110 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 Tosattie04c5d72013-07-10 22:21:57 -0300121 pvti->pvti.version != version));
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200122
123 if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT)))
124 *mode = VCLOCK_NONE;
125
126 /* refer to tsc.c read_tsc() comment for rationale */
Stefani Seibold7c031562014-03-17 23:22:10 +0100127 last = gtod->cycle_last;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200128
129 if (likely(ret >= last))
130 return ret;
131
132 return last;
133}
134#endif
135
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100136#else
137
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100138notrace 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 Lutomirski6f121e52014-05-05 12:19:34 -0700145 "call __kernel_vsyscall \n"
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100146 "mov %%edx, %%ebx \n"
147 : "=a" (ret)
148 : "0" (__NR_clock_gettime), "g" (clock), "c" (ts)
149 : "memory", "edx");
150 return ret;
151}
152
153notrace 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 Lutomirski6f121e52014-05-05 12:19:34 -0700160 "call __kernel_vsyscall \n"
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100161 "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
170static notrace cycle_t vread_pvclock(int *mode)
171{
172 *mode = VCLOCK_NONE;
173 return 0;
174}
175#endif
176
177#endif
178
Stefani Seibold411f7902014-03-17 23:22:03 +0100179notrace static cycle_t vread_tsc(void)
Andi Kleen2aae9502007-07-21 17:10:01 +0200180{
Stefani Seibold411f7902014-03-17 23:22:03 +0100181 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 Seibold7a59ed42014-03-17 23:22:09 +0100192 ret = (cycle_t)__native_read_tsc();
Stefani Seibold411f7902014-03-17 23:22:03 +0100193
Stefani Seibold7c031562014-03-17 23:22:10 +0100194 last = gtod->cycle_last;
Stefani Seibold411f7902014-03-17 23:22:03 +0100195
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 Kleen2aae9502007-07-21 17:10:01 +0200209}
210
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200211notrace static inline u64 vgetsns(int *mode)
Andi Kleen2aae9502007-07-21 17:10:01 +0200212{
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100213 u64 v;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400214 cycles_t cycles;
Stefani Seibold7c031562014-03-17 23:22:10 +0100215
216 if (gtod->vclock_mode == VCLOCK_TSC)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400217 cycles = vread_tsc();
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100218#ifdef CONFIG_HPET_TIMER
Stefani Seibold7c031562014-03-17 23:22:10 +0100219 else if (gtod->vclock_mode == VCLOCK_HPET)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400220 cycles = vread_hpet();
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100221#endif
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200222#ifdef CONFIG_PARAVIRT_CLOCK
Stefani Seibold7c031562014-03-17 23:22:10 +0100223 else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200224 cycles = vread_pvclock(mode);
225#endif
John Stultza939e812012-03-01 22:11:09 -0800226 else
227 return 0;
Stefani Seibold7c031562014-03-17 23:22:10 +0100228 v = (cycles - gtod->cycle_last) & gtod->mask;
229 return v * gtod->mult;
Andi Kleen2aae9502007-07-21 17:10:01 +0200230}
231
Andy Lutomirski5f293472012-03-22 21:15:52 -0700232/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
233notrace static int __always_inline do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200234{
John Stultz650ea022012-09-04 16:14:46 -0400235 unsigned long seq;
236 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800237 int mode;
238
Andi Kleen2aae9502007-07-21 17:10:01 +0200239 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100240 seq = gtod_read_begin(gtod);
241 mode = gtod->vclock_mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200242 ts->tv_sec = gtod->wall_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400243 ns = gtod->wall_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200244 ns += vgetsns(&mode);
Stefani Seibold7c031562014-03-17 23:22:10 +0100245 ns >>= gtod->shift;
246 } while (unlikely(gtod_read_retry(gtod, seq)));
John Stultza939e812012-03-01 22:11:09 -0800247
Stefani Seibold7c031562014-03-17 23:22:10 +0100248 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
249 ts->tv_nsec = ns;
250
John Stultza939e812012-03-01 22:11:09 -0800251 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200252}
253
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100254notrace static int __always_inline do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200255{
John Stultz650ea022012-09-04 16:14:46 -0400256 unsigned long seq;
257 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800258 int mode;
259
Andi Kleen2aae9502007-07-21 17:10:01 +0200260 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100261 seq = gtod_read_begin(gtod);
262 mode = gtod->vclock_mode;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700263 ts->tv_sec = gtod->monotonic_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400264 ns = gtod->monotonic_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200265 ns += vgetsns(&mode);
Stefani Seibold7c031562014-03-17 23:22:10 +0100266 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 Lutomirski0f51f282011-05-23 09:31:27 -0400271
John Stultza939e812012-03-01 22:11:09 -0800272 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200273}
274
Stefani Seiboldce39c642014-03-17 23:22:04 +0100275notrace static void do_realtime_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700276{
277 unsigned long seq;
278 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100279 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 stultzda15cfd2009-08-19 19:13:34 -0700283}
284
Stefani Seiboldce39c642014-03-17 23:22:04 +0100285notrace static void do_monotonic_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700286{
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700287 unsigned long seq;
john stultzda15cfd2009-08-19 19:13:34 -0700288 do {
Stefani Seibold7c031562014-03-17 23:22:10 +0100289 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 stultzda15cfd2009-08-19 19:13:34 -0700293}
294
Steven Rostedt23adec52008-05-12 21:20:41 +0200295notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200296{
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400297 switch (clock) {
298 case CLOCK_REALTIME:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100299 if (do_realtime(ts) == VCLOCK_NONE)
300 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400301 break;
302 case CLOCK_MONOTONIC:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100303 if (do_monotonic(ts) == VCLOCK_NONE)
304 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400305 break;
306 case CLOCK_REALTIME_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100307 do_realtime_coarse(ts);
308 break;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400309 case CLOCK_MONOTONIC_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100310 do_monotonic_coarse(ts);
311 break;
312 default:
313 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400314 }
315
John Stultza939e812012-03-01 22:11:09 -0800316 return 0;
Stefani Seiboldce39c642014-03-17 23:22:04 +0100317fallback:
318 return vdso_fallback_gettime(clock, ts);
Andi Kleen2aae9502007-07-21 17:10:01 +0200319}
320int clock_gettime(clockid_t, struct timespec *)
321 __attribute__((weak, alias("__vdso_clock_gettime")));
322
Steven Rostedt23adec52008-05-12 21:20:41 +0200323notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200324{
John Stultza939e812012-03-01 22:11:09 -0800325 if (likely(tv != NULL)) {
Stefani Seibold0df1ea22014-03-17 23:22:06 +0100326 if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
327 return vdso_fallback_gtod(tv, tz);
John Stultza939e812012-03-01 22:11:09 -0800328 tv->tv_usec /= 1000;
Andi Kleen2aae9502007-07-21 17:10:01 +0200329 }
John Stultza939e812012-03-01 22:11:09 -0800330 if (unlikely(tz != NULL)) {
Stefani Seibold7c031562014-03-17 23:22:10 +0100331 tz->tz_minuteswest = gtod->tz_minuteswest;
332 tz->tz_dsttime = gtod->tz_dsttime;
John Stultza939e812012-03-01 22:11:09 -0800333 }
334
John Stultza939e812012-03-01 22:11:09 -0800335 return 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200336}
337int gettimeofday(struct timeval *, struct timezone *)
338 __attribute__((weak, alias("__vdso_gettimeofday")));
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400339
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400340/*
341 * This will break when the xtime seconds get inaccurate, but that is
342 * unlikely
343 */
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400344notrace time_t __vdso_time(time_t *t)
345{
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100346 /* This is atomic on x86 so we don't need any locks. */
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100347 time_t result = ACCESS_ONCE(gtod->wall_time_sec);
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400348
349 if (t)
350 *t = result;
351 return result;
352}
353int time(time_t *t)
354 __attribute__((weak, alias("__vdso_time")));