blob: 8792d6e0a2c30f22b3d2f54899226f9fbe0ee3fb [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.
9 * Also alternative() doesn't work.
10 */
11
Ingo Molnar2b7d0392008-11-12 13:17:38 +010012/* Disable profiling for userspace code: */
Steven Rostedt2ed84ee2008-11-12 15:24:24 -050013#define DISABLE_BRANCH_PROFILING
Ingo Molnar2b7d0392008-11-12 13:17:38 +010014
Andi Kleen2aae9502007-07-21 17:10:01 +020015#include <linux/kernel.h>
16#include <linux/posix-timers.h>
17#include <linux/time.h>
18#include <linux/string.h>
19#include <asm/vsyscall.h>
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040020#include <asm/fixmap.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020021#include <asm/vgtod.h>
22#include <asm/timex.h>
23#include <asm/hpet.h>
24#include <asm/unistd.h>
25#include <asm/io.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{
63 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
64}
65
Steven Rostedt23adec52008-05-12 21:20:41 +020066notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020067{
68 long ret;
69 asm("syscall" : "=a" (ret) :
70 "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
71 return ret;
72}
73
Steven Rostedt23adec52008-05-12 21:20:41 +020074notrace static inline long vgetns(void)
Andi Kleen2aae9502007-07-21 17:10:01 +020075{
Andi Kleen95b08672007-09-11 14:02:09 +020076 long v;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -040077 cycles_t cycles;
78 if (gtod->clock.vclock_mode == VCLOCK_TSC)
79 cycles = vread_tsc();
80 else
81 cycles = vread_hpet();
82 v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
Andi Kleen95b08672007-09-11 14:02:09 +020083 return (v * gtod->clock.mult) >> gtod->clock.shift;
Andi Kleen2aae9502007-07-21 17:10:01 +020084}
85
Steven Rostedt23adec52008-05-12 21:20:41 +020086notrace static noinline int do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020087{
88 unsigned long seq, ns;
89 do {
90 seq = read_seqbegin(&gtod->lock);
91 ts->tv_sec = gtod->wall_time_sec;
92 ts->tv_nsec = gtod->wall_time_nsec;
93 ns = vgetns();
94 } while (unlikely(read_seqretry(&gtod->lock, seq)));
95 timespec_add_ns(ts, ns);
96 return 0;
97}
98
Steven Rostedt23adec52008-05-12 21:20:41 +020099notrace static noinline int do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200100{
101 unsigned long seq, ns, secs;
102 do {
103 seq = read_seqbegin(&gtod->lock);
104 secs = gtod->wall_time_sec;
105 ns = gtod->wall_time_nsec + vgetns();
106 secs += gtod->wall_to_monotonic.tv_sec;
107 ns += gtod->wall_to_monotonic.tv_nsec;
108 } while (unlikely(read_seqretry(&gtod->lock, seq)));
Andy Lutomirski0f51f282011-05-23 09:31:27 -0400109
110 /* wall_time_nsec, vgetns(), and wall_to_monotonic.tv_nsec
111 * are all guaranteed to be nonnegative.
112 */
113 while (ns >= NSEC_PER_SEC) {
114 ns -= NSEC_PER_SEC;
115 ++secs;
116 }
117 ts->tv_sec = secs;
118 ts->tv_nsec = ns;
119
Andi Kleen2aae9502007-07-21 17:10:01 +0200120 return 0;
121}
122
john stultzda15cfd2009-08-19 19:13:34 -0700123notrace static noinline int do_realtime_coarse(struct timespec *ts)
124{
125 unsigned long seq;
126 do {
127 seq = read_seqbegin(&gtod->lock);
128 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
129 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
130 } while (unlikely(read_seqretry(&gtod->lock, seq)));
131 return 0;
132}
133
134notrace static noinline int do_monotonic_coarse(struct timespec *ts)
135{
136 unsigned long seq, ns, secs;
137 do {
138 seq = read_seqbegin(&gtod->lock);
139 secs = gtod->wall_time_coarse.tv_sec;
140 ns = gtod->wall_time_coarse.tv_nsec;
141 secs += gtod->wall_to_monotonic.tv_sec;
142 ns += gtod->wall_to_monotonic.tv_nsec;
143 } while (unlikely(read_seqretry(&gtod->lock, seq)));
Andy Lutomirski0f51f282011-05-23 09:31:27 -0400144
145 /* wall_time_nsec and wall_to_monotonic.tv_nsec are
146 * guaranteed to be between 0 and NSEC_PER_SEC.
147 */
148 if (ns >= NSEC_PER_SEC) {
149 ns -= NSEC_PER_SEC;
150 ++secs;
151 }
152 ts->tv_sec = secs;
153 ts->tv_nsec = ns;
154
john stultzda15cfd2009-08-19 19:13:34 -0700155 return 0;
156}
157
Steven Rostedt23adec52008-05-12 21:20:41 +0200158notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200159{
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400160 switch (clock) {
161 case CLOCK_REALTIME:
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400162 if (likely(gtod->clock.vclock_mode != VCLOCK_NONE))
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400163 return do_realtime(ts);
164 break;
165 case CLOCK_MONOTONIC:
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400166 if (likely(gtod->clock.vclock_mode != VCLOCK_NONE))
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400167 return do_monotonic(ts);
168 break;
169 case CLOCK_REALTIME_COARSE:
170 return do_realtime_coarse(ts);
171 case CLOCK_MONOTONIC_COARSE:
172 return do_monotonic_coarse(ts);
173 }
174
Andi Kleen2aae9502007-07-21 17:10:01 +0200175 return vdso_fallback_gettime(clock, ts);
176}
177int clock_gettime(clockid_t, struct timespec *)
178 __attribute__((weak, alias("__vdso_clock_gettime")));
179
Steven Rostedt23adec52008-05-12 21:20:41 +0200180notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200181{
182 long ret;
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400183 if (likely(gtod->clock.vclock_mode != VCLOCK_NONE)) {
John Wright2f65dd42009-04-29 14:32:01 -0600184 if (likely(tv != NULL)) {
185 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
186 offsetof(struct timespec, tv_nsec) ||
187 sizeof(*tv) != sizeof(struct timespec));
188 do_realtime((struct timespec *)tv);
189 tv->tv_usec /= 1000;
190 }
Andi Kleen2aae9502007-07-21 17:10:01 +0200191 if (unlikely(tz != NULL)) {
Andi Kleena1289642008-05-14 16:10:42 -0700192 /* Avoid memcpy. Some old compilers fail to inline it */
193 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
194 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
Andi Kleen2aae9502007-07-21 17:10:01 +0200195 }
196 return 0;
197 }
198 asm("syscall" : "=a" (ret) :
199 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
200 return ret;
201}
202int gettimeofday(struct timeval *, struct timezone *)
203 __attribute__((weak, alias("__vdso_gettimeofday")));
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400204
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400205/*
206 * This will break when the xtime seconds get inaccurate, but that is
207 * unlikely
208 */
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400209notrace time_t __vdso_time(time_t *t)
210{
Andy Lutomirski973aa812011-05-23 09:31:31 -0400211 /* This is atomic on x86_64 so we don't need any locks. */
Andy Lutomirski0d7b8542011-06-05 13:50:20 -0400212 time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
Andy Lutomirskif144a6b2011-05-23 09:31:30 -0400213
214 if (t)
215 *t = result;
216 return result;
217}
218int time(time_t *t)
219 __attribute__((weak, alias("__vdso_time")));