blob: 4df6c373421a435dfeac34d68407ce10602d688d [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>
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
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040028notrace static cycle_t vread_tsc(void)
29{
30 cycle_t ret;
31 u64 last;
32
33 /*
34 * Empirically, a fence (of type that depends on the CPU)
35 * before rdtsc is enough to ensure that rdtsc is ordered
36 * with respect to loads. The various CPU manuals are unclear
37 * as to whether rdtsc can be reordered with later loads,
38 * but no one has ever seen it happen.
39 */
40 rdtsc_barrier();
41 ret = (cycle_t)vget_cycles();
42
43 last = VVAR(vsyscall_gtod_data).clock.cycle_last;
44
45 if (likely(ret >= last))
46 return ret;
47
48 /*
49 * GCC likes to generate cmov here, but this branch is extremely
50 * predictable (it's just a funciton of time and the likely is
51 * very likely) and there's a data dependence, so force GCC
52 * to generate a branch instead. I don't barrier() because
53 * we don't actually need a barrier, and if this function
54 * ever gets inlined it will generate worse code.
55 */
56 asm volatile ("");
57 return last;
58}
59
60static notrace cycle_t vread_hpet(void)
61{
62 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
63}
64
Steven Rostedt23adec52008-05-12 21:20:41 +020065notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020066{
67 long ret;
68 asm("syscall" : "=a" (ret) :
69 "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
70 return ret;
71}
72
John Stultza939e812012-03-01 22:11:09 -080073notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
74{
75 long ret;
76
77 asm("syscall" : "=a" (ret) :
78 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
79 return ret;
80}
81
82
John Stultz650ea022012-09-04 16:14:46 -040083notrace static inline u64 vgetsns(void)
Andi Kleen2aae9502007-07-21 17:10:01 +020084{
Andi Kleen95b08672007-09-11 14:02:09 +020085 long v;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040086 cycles_t cycles;
87 if (gtod->clock.vclock_mode == VCLOCK_TSC)
88 cycles = vread_tsc();
John Stultza939e812012-03-01 22:11:09 -080089 else if (gtod->clock.vclock_mode == VCLOCK_HPET)
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040090 cycles = vread_hpet();
John Stultza939e812012-03-01 22:11:09 -080091 else
92 return 0;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040093 v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
John Stultz650ea022012-09-04 16:14:46 -040094 return v * gtod->clock.mult;
Andi Kleen2aae9502007-07-21 17:10:01 +020095}
96
Andy Lutomirski5f293472012-03-22 21:15:52 -070097/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
98notrace static int __always_inline do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020099{
John Stultz650ea022012-09-04 16:14:46 -0400100 unsigned long seq;
101 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800102 int mode;
103
John Stultz650ea022012-09-04 16:14:46 -0400104 ts->tv_nsec = 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200105 do {
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000106 seq = read_seqcount_begin(&gtod->seq);
John Stultza939e812012-03-01 22:11:09 -0800107 mode = gtod->clock.vclock_mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200108 ts->tv_sec = gtod->wall_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400109 ns = gtod->wall_time_snsec;
110 ns += vgetsns();
111 ns >>= gtod->clock.shift;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000112 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
John Stultza939e812012-03-01 22:11:09 -0800113
Andi Kleen2aae9502007-07-21 17:10:01 +0200114 timespec_add_ns(ts, ns);
John Stultza939e812012-03-01 22:11:09 -0800115 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200116}
117
Andy Lutomirski5f293472012-03-22 21:15:52 -0700118notrace static int do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200119{
John Stultz650ea022012-09-04 16:14:46 -0400120 unsigned long seq;
121 u64 ns;
John Stultza939e812012-03-01 22:11:09 -0800122 int mode;
123
John Stultz650ea022012-09-04 16:14:46 -0400124 ts->tv_nsec = 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200125 do {
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000126 seq = read_seqcount_begin(&gtod->seq);
John Stultza939e812012-03-01 22:11:09 -0800127 mode = gtod->clock.vclock_mode;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700128 ts->tv_sec = gtod->monotonic_time_sec;
John Stultz650ea022012-09-04 16:14:46 -0400129 ns = gtod->monotonic_time_snsec;
130 ns += vgetsns();
131 ns >>= gtod->clock.shift;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000132 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700133 timespec_add_ns(ts, ns);
Andy Lutomirski0f51f282011-05-23 09:31:27 -0400134
John Stultza939e812012-03-01 22:11:09 -0800135 return mode;
Andi Kleen2aae9502007-07-21 17:10:01 +0200136}
137
Andy Lutomirski5f293472012-03-22 21:15:52 -0700138notrace static int do_realtime_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700139{
140 unsigned long seq;
141 do {
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000142 seq = read_seqcount_begin(&gtod->seq);
john stultzda15cfd2009-08-19 19:13:34 -0700143 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
144 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000145 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
john stultzda15cfd2009-08-19 19:13:34 -0700146 return 0;
147}
148
Andy Lutomirski5f293472012-03-22 21:15:52 -0700149notrace static int do_monotonic_coarse(struct timespec *ts)
john stultzda15cfd2009-08-19 19:13:34 -0700150{
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700151 unsigned long seq;
john stultzda15cfd2009-08-19 19:13:34 -0700152 do {
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000153 seq = read_seqcount_begin(&gtod->seq);
Andy Lutomirski91ec87d2012-03-22 21:15:51 -0700154 ts->tv_sec = gtod->monotonic_time_coarse.tv_sec;
155 ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec;
Thomas Gleixner2ab51652012-02-28 19:46:04 +0000156 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
Andy Lutomirski0f51f282011-05-23 09:31:27 -0400157
john stultzda15cfd2009-08-19 19:13:34 -0700158 return 0;
159}
160
Steven Rostedt23adec52008-05-12 21:20:41 +0200161notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200162{
John Stultza939e812012-03-01 22:11:09 -0800163 int ret = VCLOCK_NONE;
164
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400165 switch (clock) {
166 case CLOCK_REALTIME:
John Stultza939e812012-03-01 22:11:09 -0800167 ret = do_realtime(ts);
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400168 break;
169 case CLOCK_MONOTONIC:
John Stultza939e812012-03-01 22:11:09 -0800170 ret = do_monotonic(ts);
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400171 break;
172 case CLOCK_REALTIME_COARSE:
173 return do_realtime_coarse(ts);
174 case CLOCK_MONOTONIC_COARSE:
175 return do_monotonic_coarse(ts);
176 }
177
John Stultza939e812012-03-01 22:11:09 -0800178 if (ret == VCLOCK_NONE)
179 return vdso_fallback_gettime(clock, ts);
180 return 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200181}
182int clock_gettime(clockid_t, struct timespec *)
183 __attribute__((weak, alias("__vdso_clock_gettime")));
184
Steven Rostedt23adec52008-05-12 21:20:41 +0200185notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200186{
John Stultza939e812012-03-01 22:11:09 -0800187 long ret = VCLOCK_NONE;
188
189 if (likely(tv != NULL)) {
190 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
191 offsetof(struct timespec, tv_nsec) ||
192 sizeof(*tv) != sizeof(struct timespec));
193 ret = do_realtime((struct timespec *)tv);
194 tv->tv_usec /= 1000;
Andi Kleen2aae9502007-07-21 17:10:01 +0200195 }
John Stultza939e812012-03-01 22:11:09 -0800196 if (unlikely(tz != NULL)) {
197 /* Avoid memcpy. Some old compilers fail to inline it */
198 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
199 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
200 }
201
202 if (ret == VCLOCK_NONE)
203 return vdso_fallback_gtod(tv, tz);
204 return 0;
Andi Kleen2aae9502007-07-21 17:10:01 +0200205}
206int gettimeofday(struct timeval *, struct timezone *)
207 __attribute__((weak, alias("__vdso_gettimeofday")));
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400208
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400209/*
210 * This will break when the xtime seconds get inaccurate, but that is
211 * unlikely
212 */
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400213notrace time_t __vdso_time(time_t *t)
214{
Andy Lutomirski973aa812011-05-23 09:31:31 -0400215 /* This is atomic on x86_64 so we don't need any locks. */
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400216 time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400217
218 if (t)
219 *t = result;
220 return result;
221}
222int time(time_t *t)
223 __attribute__((weak, alias("__vdso_time")));