blob: 90bb5e8027ac613fcbd317cd06b5223115162955 [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
Andi Kleen2aae9502007-07-21 17:10:01 +020017#include <linux/kernel.h>
Stefani Seibold7a59ed42014-03-17 23:22:09 +010018#include <uapi/linux/time.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020019#include <linux/string.h>
20#include <asm/vsyscall.h>
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040021#include <asm/fixmap.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020022#include <asm/vgtod.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020023#include <asm/hpet.h>
24#include <asm/unistd.h>
25#include <asm/io.h>
Marcelo Tosatti51c19b42012-11-27 23:28:57 -020026#include <asm/pvclock.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020027
Andy Lutomirski8c49d9a2011-05-23 09:31:24 -040028#define gtod (&VVAR(vsyscall_gtod_data))
Andi Kleen2aae9502007-07-21 17:10:01 +020029
Stefani Seibold7a59ed42014-03-17 23:22:09 +010030extern int __vdso_clock_gettime(clockid_t clock, struct timespec *ts);
31extern int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz);
32extern time_t __vdso_time(time_t *t);
33
34#ifndef BUILD_VDSO32
35
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040036static notrace cycle_t vread_hpet(void)
37{
Satoru Takeuchi36dfbbf2013-02-15 16:58:14 +090038 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + HPET_COUNTER);
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040039}
40
Stefani Seibold411f7902014-03-17 23:22:03 +010041notrace 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
49notrace 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 Tosatti51c19b42012-11-27 23:28:57 -020058#ifdef CONFIG_PARAVIRT_CLOCK
59
60static 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
74static 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 Tosatti51c19b42012-11-27 23:28:57 -020080 u8 flags;
81 unsigned cpu, cpu1;
82
83
84 /*
Marcelo Tosattie04c5d72013-07-10 22:21:57 -030085 * 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 Tosatti51c19b42012-11-27 23:28:57 -020092 */
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 Tosatti51c19b42012-11-27 23:28:57 -0200102 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 Tosattie04c5d72013-07-10 22:21:57 -0300113 pvti->pvti.version != version));
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200114
115 if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT)))
116 *mode = VCLOCK_NONE;
117
118 /* refer to tsc.c read_tsc() comment for rationale */
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100119 last = gtod->clock.cycle_last;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200120
121 if (likely(ret >= last))
122 return ret;
123
124 return last;
125}
126#endif
127
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100128#else
129
130extern u8 hpet_page
131 __attribute__((visibility("hidden")));
132
133#ifdef CONFIG_HPET_TIMER
134static notrace cycle_t vread_hpet(void)
135{
136 return readl((const void __iomem *)(&hpet_page + HPET_COUNTER));
137}
138#endif
139
140notrace 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
155notrace 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
172static notrace cycle_t vread_pvclock(int *mode)
173{
174 *mode = VCLOCK_NONE;
175 return 0;
176}
177#endif
178
179#endif
180
Stefani Seibold411f7902014-03-17 23:22:03 +0100181notrace static cycle_t vread_tsc(void)
Andi Kleen2aae9502007-07-21 17:10:01 +0200182{
Stefani Seibold411f7902014-03-17 23:22:03 +0100183 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 Seibold7a59ed42014-03-17 23:22:09 +0100194 ret = (cycle_t)__native_read_tsc();
Stefani Seibold411f7902014-03-17 23:22:03 +0100195
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100196 last = gtod->clock.cycle_last;
Stefani Seibold411f7902014-03-17 23:22:03 +0100197
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 Kleen2aae9502007-07-21 17:10:01 +0200211}
212
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200213notrace static inline u64 vgetsns(int *mode)
Andi Kleen2aae9502007-07-21 17:10:01 +0200214{
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100215 u64 v;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400216 cycles_t cycles;
217 if (gtod->clock.vclock_mode == VCLOCK_TSC)
218 cycles = vread_tsc();
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100219#ifdef CONFIG_HPET_TIMER
John Stultza939e812012-03-01 22:11:09 -0800220 else if (gtod->clock.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
224 else if (gtod->clock.vclock_mode == VCLOCK_PVCLOCK)
225 cycles = vread_pvclock(mode);
226#endif
John Stultza939e812012-03-01 22:11:09 -0800227 else
228 return 0;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400229 v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
John Stultz650ea022012-09-04 16:14:46 -0400230 return v * gtod->clock.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
John Stultz650ea022012-09-04 16:14:46 -0400240 ts->tv_nsec = 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200241 do {
John Stultz0c3351d2014-01-02 15:11:13 -0800242 seq = raw_read_seqcount_begin(&gtod->seq);
John Stultza939e812012-03-01 22:11:09 -0800243 mode = gtod->clock.vclock_mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200244 ts->tv_sec = gtod->wall_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400245 ns = gtod->wall_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200246 ns += vgetsns(&mode);
John Stultz650ea022012-09-04 16:14:46 -0400247 ns >>= gtod->clock.shift;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000248 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
John Stultza939e812012-03-01 22:11:09 -0800249
Andi Kleen2aae9502007-07-21 17:10:01 +0200250 timespec_add_ns(ts, ns);
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
John Stultz650ea022012-09-04 16:14:46 -0400260 ts->tv_nsec = 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200261 do {
John Stultz0c3351d2014-01-02 15:11:13 -0800262 seq = raw_read_seqcount_begin(&gtod->seq);
John Stultza939e812012-03-01 22:11:09 -0800263 mode = gtod->clock.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);
John Stultz650ea022012-09-04 16:14:46 -0400267 ns >>= gtod->clock.shift;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000268 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700269 timespec_add_ns(ts, ns);
Andy Lutomirski0f51f282011-05-23 09:31:27 -0400270
John Stultza939e812012-03-01 22:11:09 -0800271 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200272}
273
Stefani Seiboldce39c642014-03-17 23:22:04 +0100274notrace static void do_realtime_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700275{
276 unsigned long seq;
277 do {
John Stultz0c3351d2014-01-02 15:11:13 -0800278 seq = raw_read_seqcount_begin(&gtod->seq);
john stultzda15cfd2009-08-19 19:13:34 -0700279 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
280 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000281 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
john stultzda15cfd2009-08-19 19:13:34 -0700282}
283
Stefani Seiboldce39c642014-03-17 23:22:04 +0100284notrace static void do_monotonic_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700285{
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700286 unsigned long seq;
john stultzda15cfd2009-08-19 19:13:34 -0700287 do {
John Stultz0c3351d2014-01-02 15:11:13 -0800288 seq = raw_read_seqcount_begin(&gtod->seq);
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700289 ts->tv_sec = gtod->monotonic_time_coarse.tv_sec;
290 ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000291 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
john stultzda15cfd2009-08-19 19:13:34 -0700292}
293
Steven Rostedt23adec52008-05-12 21:20:41 +0200294notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200295{
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400296 switch (clock) {
297 case CLOCK_REALTIME:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100298 if (do_realtime(ts) == VCLOCK_NONE)
299 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400300 break;
301 case CLOCK_MONOTONIC:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100302 if (do_monotonic(ts) == VCLOCK_NONE)
303 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400304 break;
305 case CLOCK_REALTIME_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100306 do_realtime_coarse(ts);
307 break;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400308 case CLOCK_MONOTONIC_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100309 do_monotonic_coarse(ts);
310 break;
311 default:
312 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400313 }
314
John Stultza939e812012-03-01 22:11:09 -0800315 return 0;
Stefani Seiboldce39c642014-03-17 23:22:04 +0100316fallback:
317 return vdso_fallback_gettime(clock, ts);
Andi Kleen2aae9502007-07-21 17:10:01 +0200318}
319int clock_gettime(clockid_t, struct timespec *)
320 __attribute__((weak, alias("__vdso_clock_gettime")));
321
Steven Rostedt23adec52008-05-12 21:20:41 +0200322notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200323{
John Stultza939e812012-03-01 22:11:09 -0800324 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 Seibold0df1ea22014-03-17 23:22:06 +0100328 if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
329 return vdso_fallback_gtod(tv, tz);
John Stultza939e812012-03-01 22:11:09 -0800330 tv->tv_usec /= 1000;
Andi Kleen2aae9502007-07-21 17:10:01 +0200331 }
John Stultza939e812012-03-01 22:11:09 -0800332 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 Stultza939e812012-03-01 22:11:09 -0800338 return 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200339}
340int gettimeofday(struct timeval *, struct timezone *)
341 __attribute__((weak, alias("__vdso_gettimeofday")));
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400342
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400343/*
344 * This will break when the xtime seconds get inaccurate, but that is
345 * unlikely
346 */
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400347notrace time_t __vdso_time(time_t *t)
348{
Stefani Seibold7a59ed42014-03-17 23:22:09 +0100349 /* This is atomic on x86 so we don't need any locks. */
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100350 time_t result = ACCESS_ONCE(gtod->wall_time_sec);
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400351
352 if (t)
353 *t = result;
354 return result;
355}
356int time(time_t *t)
357 __attribute__((weak, alias("__vdso_time")));