blob: 72074d5284009a35265580a13eaa99e6f11588b4 [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 -040029notrace static cycle_t vread_tsc(void)
30{
31 cycle_t ret;
32 u64 last;
33
34 /*
35 * Empirically, a fence (of type that depends on the CPU)
36 * before rdtsc is enough to ensure that rdtsc is ordered
37 * with respect to loads. The various CPU manuals are unclear
38 * as to whether rdtsc can be reordered with later loads,
39 * but no one has ever seen it happen.
40 */
41 rdtsc_barrier();
42 ret = (cycle_t)vget_cycles();
43
44 last = VVAR(vsyscall_gtod_data).clock.cycle_last;
45
46 if (likely(ret >= last))
47 return ret;
48
49 /*
50 * GCC likes to generate cmov here, but this branch is extremely
51 * predictable (it's just a funciton of time and the likely is
52 * very likely) and there's a data dependence, so force GCC
53 * to generate a branch instead. I don't barrier() because
54 * we don't actually need a barrier, and if this function
55 * ever gets inlined it will generate worse code.
56 */
57 asm volatile ("");
58 return last;
59}
60
61static notrace cycle_t vread_hpet(void)
62{
Satoru Takeuchi36dfbbf2013-02-15 16:58:14 +090063 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + HPET_COUNTER);
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040064}
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 */
127 last = VVAR(vsyscall_gtod_data).clock.cycle_last;
128
129 if (likely(ret >= last))
130 return ret;
131
132 return last;
133}
134#endif
135
Steven Rostedt23adec52008-05-12 21:20:41 +0200136notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200137{
138 long ret;
139 asm("syscall" : "=a" (ret) :
140 "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
141 return ret;
142}
143
John Stultza939e812012-03-01 22:11:09 -0800144notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
145{
146 long ret;
147
148 asm("syscall" : "=a" (ret) :
149 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
150 return ret;
151}
152
153
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200154notrace static inline u64 vgetsns(int *mode)
Andi Kleen2aae9502007-07-21 17:10:01 +0200155{
Andi Kleen95b08672007-09-11 14:02:09 +0200156 long v;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400157 cycles_t cycles;
158 if (gtod->clock.vclock_mode == VCLOCK_TSC)
159 cycles = vread_tsc();
John Stultza939e812012-03-01 22:11:09 -0800160 else if (gtod->clock.vclock_mode == VCLOCK_HPET)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400161 cycles = vread_hpet();
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200162#ifdef CONFIG_PARAVIRT_CLOCK
163 else if (gtod->clock.vclock_mode == VCLOCK_PVCLOCK)
164 cycles = vread_pvclock(mode);
165#endif
John Stultza939e812012-03-01 22:11:09 -0800166 else
167 return 0;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400168 v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
John Stultz650ea022012-09-04 16:14:46 -0400169 return v * gtod->clock.mult;
Andi Kleen2aae9502007-07-21 17:10:01 +0200170}
171
Andy Lutomirski5f293472012-03-22 21:15:52 -0700172/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
173notrace static int __always_inline do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200174{
John Stultz650ea022012-09-04 16:14:46 -0400175 unsigned long seq;
176 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800177 int mode;
178
John Stultz650ea022012-09-04 16:14:46 -0400179 ts->tv_nsec = 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200180 do {
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000181 seq = read_seqcount_begin(&gtod->seq);
John Stultza939e812012-03-01 22:11:09 -0800182 mode = gtod->clock.vclock_mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200183 ts->tv_sec = gtod->wall_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400184 ns = gtod->wall_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200185 ns += vgetsns(&mode);
John Stultz650ea022012-09-04 16:14:46 -0400186 ns >>= gtod->clock.shift;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000187 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
John Stultza939e812012-03-01 22:11:09 -0800188
Andi Kleen2aae9502007-07-21 17:10:01 +0200189 timespec_add_ns(ts, ns);
John Stultza939e812012-03-01 22:11:09 -0800190 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200191}
192
Andy Lutomirski5f293472012-03-22 21:15:52 -0700193notrace static int do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200194{
John Stultz650ea022012-09-04 16:14:46 -0400195 unsigned long seq;
196 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800197 int mode;
198
John Stultz650ea022012-09-04 16:14:46 -0400199 ts->tv_nsec = 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200200 do {
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000201 seq = read_seqcount_begin(&gtod->seq);
John Stultza939e812012-03-01 22:11:09 -0800202 mode = gtod->clock.vclock_mode;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700203 ts->tv_sec = gtod->monotonic_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400204 ns = gtod->monotonic_time_snsec;
Marcelo Tosatti51c19b42012-11-27 23:28:57 -0200205 ns += vgetsns(&mode);
John Stultz650ea022012-09-04 16:14:46 -0400206 ns >>= gtod->clock.shift;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000207 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700208 timespec_add_ns(ts, ns);
Andy Lutomirski0f51f282011-05-23 09:31:27 -0400209
John Stultza939e812012-03-01 22:11:09 -0800210 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200211}
212
Andy Lutomirski5f293472012-03-22 21:15:52 -0700213notrace static int do_realtime_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700214{
215 unsigned long seq;
216 do {
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000217 seq = read_seqcount_begin(&gtod->seq);
john stultzda15cfd2009-08-19 19:13:34 -0700218 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
219 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000220 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
john stultzda15cfd2009-08-19 19:13:34 -0700221 return 0;
222}
223
Andy Lutomirski5f293472012-03-22 21:15:52 -0700224notrace static int do_monotonic_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700225{
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700226 unsigned long seq;
john stultzda15cfd2009-08-19 19:13:34 -0700227 do {
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000228 seq = read_seqcount_begin(&gtod->seq);
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700229 ts->tv_sec = gtod->monotonic_time_coarse.tv_sec;
230 ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000231 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Andy Lutomirski0f51f282011-05-23 09:31:27 -0400232
john stultzda15cfd2009-08-19 19:13:34 -0700233 return 0;
234}
235
Steven Rostedt23adec52008-05-12 21:20:41 +0200236notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200237{
John Stultza939e812012-03-01 22:11:09 -0800238 int ret = VCLOCK_NONE;
239
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400240 switch (clock) {
241 case CLOCK_REALTIME:
John Stultza939e812012-03-01 22:11:09 -0800242 ret = do_realtime(ts);
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400243 break;
244 case CLOCK_MONOTONIC:
John Stultza939e812012-03-01 22:11:09 -0800245 ret = do_monotonic(ts);
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400246 break;
247 case CLOCK_REALTIME_COARSE:
248 return do_realtime_coarse(ts);
249 case CLOCK_MONOTONIC_COARSE:
250 return do_monotonic_coarse(ts);
251 }
252
John Stultza939e812012-03-01 22:11:09 -0800253 if (ret == VCLOCK_NONE)
254 return vdso_fallback_gettime(clock, ts);
255 return 0;
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 long ret = VCLOCK_NONE;
263
264 if (likely(tv != NULL)) {
265 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
266 offsetof(struct timespec, tv_nsec) ||
267 sizeof(*tv) != sizeof(struct timespec));
268 ret = do_realtime((struct timespec *)tv);
269 tv->tv_usec /= 1000;
Andi Kleen2aae9502007-07-21 17:10:01 +0200270 }
John Stultza939e812012-03-01 22:11:09 -0800271 if (unlikely(tz != NULL)) {
272 /* Avoid memcpy. Some old compilers fail to inline it */
273 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
274 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
275 }
276
277 if (ret == VCLOCK_NONE)
278 return vdso_fallback_gtod(tv, tz);
279 return 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200280}
281int gettimeofday(struct timeval *, struct timezone *)
282 __attribute__((weak, alias("__vdso_gettimeofday")));
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400283
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400284/*
285 * This will break when the xtime seconds get inaccurate, but that is
286 * unlikely
287 */
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400288notrace time_t __vdso_time(time_t *t)
289{
Andy Lutomirski973aa812011-05-23 09:31:31 -0400290 /* This is atomic on x86_64 so we don't need any locks. */
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400291 time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400292
293 if (t)
294 *t = result;
295 return result;
296}
297int time(time_t *t)
298 __attribute__((weak, alias("__vdso_time")));