blob: 09dae4a1c6dc605a1630357e5386cfcc44088a85 [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 *
7 * The code should have no internal unresolved relocations.
8 * Check with readelf after changing.
Andi Kleen2aae9502007-07-21 17:10:01 +02009 */
10
Ingo Molnar2b7d0392008-11-12 13:17:38 +010011/* Disable profiling for userspace code: */
Steven Rostedt2ed84ee2008-11-12 15:24:24 -050012#define DISABLE_BRANCH_PROFILING
Ingo Molnar2b7d0392008-11-12 13:17:38 +010013
Andi Kleen2aae9502007-07-21 17:10:01 +020014#include <linux/kernel.h>
15#include <linux/posix-timers.h>
16#include <linux/time.h>
17#include <linux/string.h>
18#include <asm/vsyscall.h>
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040019#include <asm/fixmap.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020020#include <asm/vgtod.h>
21#include <asm/timex.h>
22#include <asm/hpet.h>
23#include <asm/unistd.h>
24#include <asm/io.h>
Marcelo Tosatti51c19b42012-11-27 23:28:57 -020025#include <asm/pvclock.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020026
Andy Lutomirski8c49d9a2011-05-23 09:31:24 -040027#define gtod (&VVAR(vsyscall_gtod_data))
Andi Kleen2aae9502007-07-21 17:10:01 +020028
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040029static notrace cycle_t vread_hpet(void)
30{
Satoru Takeuchi36dfbbf2013-02-15 16:58:14 +090031 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + HPET_COUNTER);
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040032}
33
Stefani Seibold411f7902014-03-17 23:22:03 +010034notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
35{
36 long ret;
37 asm("syscall" : "=a" (ret) :
38 "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory");
39 return ret;
40}
41
42notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
43{
44 long ret;
45
46 asm("syscall" : "=a" (ret) :
47 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
48 return ret;
49}
50
Marcelo Tosatti51c19b42012-11-27 23:28:57 -020051#ifdef CONFIG_PARAVIRT_CLOCK
52
53static notrace const struct pvclock_vsyscall_time_info *get_pvti(int cpu)
54{
55 const struct pvclock_vsyscall_time_info *pvti_base;
56 int idx = cpu / (PAGE_SIZE/PVTI_SIZE);
57 int offset = cpu % (PAGE_SIZE/PVTI_SIZE);
58
59 BUG_ON(PVCLOCK_FIXMAP_BEGIN + idx > PVCLOCK_FIXMAP_END);
60
61 pvti_base = (struct pvclock_vsyscall_time_info *)
62 __fix_to_virt(PVCLOCK_FIXMAP_BEGIN+idx);
63
64 return &pvti_base[offset];
65}
66
67static notrace cycle_t vread_pvclock(int *mode)
68{
69 const struct pvclock_vsyscall_time_info *pvti;
70 cycle_t ret;
71 u64 last;
72 u32 version;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -020073 u8 flags;
74 unsigned cpu, cpu1;
75
76
77 /*
Marcelo Tosattie04c5d72013-07-10 22:21:57 -030078 * Note: hypervisor must guarantee that:
79 * 1. cpu ID number maps 1:1 to per-CPU pvclock time info.
80 * 2. that per-CPU pvclock time info is updated if the
81 * underlying CPU changes.
82 * 3. that version is increased whenever underlying CPU
83 * changes.
84 *
Marcelo Tosatti51c19b42012-11-27 23:28:57 -020085 */
86 do {
87 cpu = __getcpu() & VGETCPU_CPU_MASK;
88 /* TODO: We can put vcpu id into higher bits of pvti.version.
89 * This will save a couple of cycles by getting rid of
90 * __getcpu() calls (Gleb).
91 */
92
93 pvti = get_pvti(cpu);
94
Marcelo Tosatti51c19b42012-11-27 23:28:57 -020095 version = __pvclock_read_cycles(&pvti->pvti, &ret, &flags);
96
97 /*
98 * Test we're still on the cpu as well as the version.
99 * We could have been migrated just after the first
100 * vgetcpu but before fetching the version, so we
101 * wouldn't notice a version change.
102 */
103 cpu1 = __getcpu() & VGETCPU_CPU_MASK;
104 } while (unlikely(cpu != cpu1 ||
105 (pvti->pvti.version & 1) ||
Marcelo Tosattie04c5d72013-07-10 22:21:57 -0300106 pvti->pvti.version != version));
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200107
108 if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT)))
109 *mode = VCLOCK_NONE;
110
111 /* refer to tsc.c read_tsc() comment for rationale */
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100112 last = gtod->clock.cycle_last;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200113
114 if (likely(ret >= last))
115 return ret;
116
117 return last;
118}
119#endif
120
Stefani Seibold411f7902014-03-17 23:22:03 +0100121notrace static cycle_t vread_tsc(void)
Andi Kleen2aae9502007-07-21 17:10:01 +0200122{
Stefani Seibold411f7902014-03-17 23:22:03 +0100123 cycle_t ret;
124 u64 last;
125
126 /*
127 * Empirically, a fence (of type that depends on the CPU)
128 * before rdtsc is enough to ensure that rdtsc is ordered
129 * with respect to loads. The various CPU manuals are unclear
130 * as to whether rdtsc can be reordered with later loads,
131 * but no one has ever seen it happen.
132 */
133 rdtsc_barrier();
134 ret = (cycle_t)vget_cycles();
135
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100136 last = gtod->clock.cycle_last;
Stefani Seibold411f7902014-03-17 23:22:03 +0100137
138 if (likely(ret >= last))
139 return ret;
140
141 /*
142 * GCC likes to generate cmov here, but this branch is extremely
143 * predictable (it's just a funciton of time and the likely is
144 * very likely) and there's a data dependence, so force GCC
145 * to generate a branch instead. I don't barrier() because
146 * we don't actually need a barrier, and if this function
147 * ever gets inlined it will generate worse code.
148 */
149 asm volatile ("");
150 return last;
Andi Kleen2aae9502007-07-21 17:10:01 +0200151}
152
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200153notrace static inline u64 vgetsns(int *mode)
Andi Kleen2aae9502007-07-21 17:10:01 +0200154{
Andi Kleen95b08672007-09-11 14:02:09 +0200155 long v;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400156 cycles_t cycles;
157 if (gtod->clock.vclock_mode == VCLOCK_TSC)
158 cycles = vread_tsc();
John Stultza939e812012-03-01 22:11:09 -0800159 else if (gtod->clock.vclock_mode == VCLOCK_HPET)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400160 cycles = vread_hpet();
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200161#ifdef CONFIG_PARAVIRT_CLOCK
162 else if (gtod->clock.vclock_mode == VCLOCK_PVCLOCK)
163 cycles = vread_pvclock(mode);
164#endif
John Stultza939e812012-03-01 22:11:09 -0800165 else
166 return 0;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400167 v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
John Stultz650ea022012-09-04 16:14:46 -0400168 return v * gtod->clock.mult;
Andi Kleen2aae9502007-07-21 17:10:01 +0200169}
170
Andy Lutomirski5f293472012-03-22 21:15:52 -0700171/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
172notrace static int __always_inline do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200173{
John Stultz650ea022012-09-04 16:14:46 -0400174 unsigned long seq;
175 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800176 int mode;
177
John Stultz650ea022012-09-04 16:14:46 -0400178 ts->tv_nsec = 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200179 do {
John Stultz0c3351d2014-01-02 15:11:13 -0800180 seq = raw_read_seqcount_begin(&gtod->seq);
John Stultza939e812012-03-01 22:11:09 -0800181 mode = gtod->clock.vclock_mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200182 ts->tv_sec = gtod->wall_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400183 ns = gtod->wall_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200184 ns += vgetsns(&mode);
John Stultz650ea022012-09-04 16:14:46 -0400185 ns >>= gtod->clock.shift;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000186 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
John Stultza939e812012-03-01 22:11:09 -0800187
Andi Kleen2aae9502007-07-21 17:10:01 +0200188 timespec_add_ns(ts, ns);
John Stultza939e812012-03-01 22:11:09 -0800189 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200190}
191
Andy Lutomirski5f293472012-03-22 21:15:52 -0700192notrace static int do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200193{
John Stultz650ea022012-09-04 16:14:46 -0400194 unsigned long seq;
195 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800196 int mode;
197
John Stultz650ea022012-09-04 16:14:46 -0400198 ts->tv_nsec = 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200199 do {
John Stultz0c3351d2014-01-02 15:11:13 -0800200 seq = raw_read_seqcount_begin(&gtod->seq);
John Stultza939e812012-03-01 22:11:09 -0800201 mode = gtod->clock.vclock_mode;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700202 ts->tv_sec = gtod->monotonic_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400203 ns = gtod->monotonic_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200204 ns += vgetsns(&mode);
John Stultz650ea022012-09-04 16:14:46 -0400205 ns >>= gtod->clock.shift;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000206 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700207 timespec_add_ns(ts, ns);
Andy Lutomirski0f51f282011-05-23 09:31:27 -0400208
John Stultza939e812012-03-01 22:11:09 -0800209 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200210}
211
Stefani Seiboldce39c642014-03-17 23:22:04 +0100212notrace static void do_realtime_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700213{
214 unsigned long seq;
215 do {
John Stultz0c3351d2014-01-02 15:11:13 -0800216 seq = raw_read_seqcount_begin(&gtod->seq);
john stultzda15cfd2009-08-19 19:13:34 -0700217 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
218 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000219 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
john stultzda15cfd2009-08-19 19:13:34 -0700220}
221
Stefani Seiboldce39c642014-03-17 23:22:04 +0100222notrace static void do_monotonic_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700223{
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700224 unsigned long seq;
john stultzda15cfd2009-08-19 19:13:34 -0700225 do {
John Stultz0c3351d2014-01-02 15:11:13 -0800226 seq = raw_read_seqcount_begin(&gtod->seq);
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700227 ts->tv_sec = gtod->monotonic_time_coarse.tv_sec;
228 ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000229 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
john stultzda15cfd2009-08-19 19:13:34 -0700230}
231
Steven Rostedt23adec52008-05-12 21:20:41 +0200232notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200233{
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400234 switch (clock) {
235 case CLOCK_REALTIME:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100236 if (do_realtime(ts) == VCLOCK_NONE)
237 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400238 break;
239 case CLOCK_MONOTONIC:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100240 if (do_monotonic(ts) == VCLOCK_NONE)
241 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400242 break;
243 case CLOCK_REALTIME_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100244 do_realtime_coarse(ts);
245 break;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400246 case CLOCK_MONOTONIC_COARSE:
Stefani Seiboldce39c642014-03-17 23:22:04 +0100247 do_monotonic_coarse(ts);
248 break;
249 default:
250 goto fallback;
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400251 }
252
John Stultza939e812012-03-01 22:11:09 -0800253 return 0;
Stefani Seiboldce39c642014-03-17 23:22:04 +0100254fallback:
255 return vdso_fallback_gettime(clock, ts);
Andi Kleen2aae9502007-07-21 17:10:01 +0200256}
257int clock_gettime(clockid_t, struct timespec *)
258 __attribute__((weak, alias("__vdso_clock_gettime")));
259
Steven Rostedt23adec52008-05-12 21:20:41 +0200260notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200261{
John Stultza939e812012-03-01 22:11:09 -0800262 if (likely(tv != NULL)) {
263 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
264 offsetof(struct timespec, tv_nsec) ||
265 sizeof(*tv) != sizeof(struct timespec));
Stefani Seibold0df1ea22014-03-17 23:22:06 +0100266 if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
267 return vdso_fallback_gtod(tv, tz);
John Stultza939e812012-03-01 22:11:09 -0800268 tv->tv_usec /= 1000;
Andi Kleen2aae9502007-07-21 17:10:01 +0200269 }
John Stultza939e812012-03-01 22:11:09 -0800270 if (unlikely(tz != NULL)) {
271 /* Avoid memcpy. Some old compilers fail to inline it */
272 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
273 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
274 }
275
John Stultza939e812012-03-01 22:11:09 -0800276 return 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200277}
278int gettimeofday(struct timeval *, struct timezone *)
279 __attribute__((weak, alias("__vdso_gettimeofday")));
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400280
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400281/*
282 * This will break when the xtime seconds get inaccurate, but that is
283 * unlikely
284 */
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400285notrace time_t __vdso_time(time_t *t)
286{
Andy Lutomirski973aa812011-05-23 09:31:31 -0400287 /* This is atomic on x86_64 so we don't need any locks. */
Stefani Seiboldaf8c93d2014-03-17 23:22:05 +0100288 time_t result = ACCESS_ONCE(gtod->wall_time_sec);
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400289
290 if (t)
291 *t = result;
292 return result;
293}
294int time(time_t *t)
295 __attribute__((weak, alias("__vdso_time")));