clocksource: Use a plain u64 instead of cycle_t
[linux-2.6-block.git] / arch / x86 / entry / vdso / vclock_gettime.c
CommitLineData
2aae950b
AK
1/*
2 * Copyright 2006 Andi Kleen, SUSE Labs.
3 * Subject to the GNU Public License, v.2
4 *
f144a6b4 5 * Fast user context implementation of clock_gettime, gettimeofday, and time.
2aae950b 6 *
7a59ed41
SS
7 * 32 Bit compat layer by Stefani Seibold <stefani@seibold.net>
8 * sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany
9 *
2aae950b
AK
10 * The code should have no internal unresolved relocations.
11 * Check with readelf after changing.
2aae950b
AK
12 */
13
7a59ed41 14#include <uapi/linux/time.h>
2aae950b 15#include <asm/vgtod.h>
7c03156f 16#include <asm/vvar.h>
2aae950b 17#include <asm/unistd.h>
7c03156f 18#include <asm/msr.h>
76480a6a 19#include <asm/pvclock.h>
7c03156f
SS
20#include <linux/math64.h>
21#include <linux/time.h>
76480a6a 22#include <linux/kernel.h>
2aae950b 23
8c49d9a7 24#define gtod (&VVAR(vsyscall_gtod_data))
2aae950b 25
7a59ed41
SS
26extern int __vdso_clock_gettime(clockid_t clock, struct timespec *ts);
27extern int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz);
28extern time_t __vdso_time(time_t *t);
29
dac16fba
AL
30#ifdef CONFIG_PARAVIRT_CLOCK
31extern u8 pvclock_page
32 __attribute__((visibility("hidden")));
33#endif
34
7a59ed41
SS
35#ifndef BUILD_VDSO32
36
411f790c
SS
37notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
38{
39 long ret;
40 asm("syscall" : "=a" (ret) :
41 "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory");
42 return ret;
98d0ac38
AL
43}
44
411f790c 45notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
98d0ac38 46{
411f790c
SS
47 long ret;
48
49 asm("syscall" : "=a" (ret) :
50 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
51 return ret;
98d0ac38
AL
52}
53
51c19b4f 54
76480a6a
AL
55#else
56
57notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
58{
59 long ret;
60
61 asm(
62 "mov %%ebx, %%edx \n"
63 "mov %2, %%ebx \n"
64 "call __kernel_vsyscall \n"
65 "mov %%edx, %%ebx \n"
66 : "=a" (ret)
67 : "0" (__NR_clock_gettime), "g" (clock), "c" (ts)
68 : "memory", "edx");
69 return ret;
70}
71
72notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
73{
74 long ret;
75
76 asm(
77 "mov %%ebx, %%edx \n"
78 "mov %2, %%ebx \n"
79 "call __kernel_vsyscall \n"
80 "mov %%edx, %%ebx \n"
81 : "=a" (ret)
82 : "0" (__NR_gettimeofday), "g" (tv), "c" (tz)
83 : "memory", "edx");
84 return ret;
85}
86
87#endif
88
89#ifdef CONFIG_PARAVIRT_CLOCK
dac16fba 90static notrace const struct pvclock_vsyscall_time_info *get_pvti0(void)
51c19b4f 91{
dac16fba 92 return (const struct pvclock_vsyscall_time_info *)&pvclock_page;
51c19b4f
MT
93}
94
a5a1d1c2 95static notrace u64 vread_pvclock(int *mode)
51c19b4f 96{
dac16fba 97 const struct pvclock_vcpu_time_info *pvti = &get_pvti0()->pvti;
a5a1d1c2 98 u64 ret;
abe9efa7
PB
99 u64 last;
100 u32 version;
51c19b4f
MT
101
102 /*
6b078f5d
AL
103 * Note: The kernel and hypervisor must guarantee that cpu ID
104 * number maps 1:1 to per-CPU pvclock time info.
105 *
106 * Because the hypervisor is entirely unaware of guest userspace
107 * preemption, it cannot guarantee that per-CPU pvclock time
108 * info is updated if the underlying CPU changes or that that
109 * version is increased whenever underlying CPU changes.
110 *
111 * On KVM, we are guaranteed that pvti updates for any vCPU are
112 * atomic as seen by *all* vCPUs. This is an even stronger
113 * guarantee than we get with a normal seqlock.
73459e2a 114 *
6b078f5d
AL
115 * On Xen, we don't appear to have that guarantee, but Xen still
116 * supplies a valid seqlock using the version field.
78fd8c72 117 *
6b078f5d
AL
118 * We only do pvclock vdso timing at all if
119 * PVCLOCK_TSC_STABLE_BIT is set, and we interpret that bit to
120 * mean that all vCPUs have matching pvti and that the TSC is
121 * synced, so we can just look at vCPU 0's pvti.
51c19b4f 122 */
6b078f5d 123
6b078f5d 124 do {
3aed64f6 125 version = pvclock_read_begin(pvti);
6b078f5d 126
78fd8c72
AL
127 if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT))) {
128 *mode = VCLOCK_NONE;
129 return 0;
130 }
131
108b249c 132 ret = __pvclock_read_cycles(pvti, rdtsc_ordered());
3aed64f6 133 } while (pvclock_read_retry(pvti, version));
6b078f5d 134
76480a6a 135 /* refer to vread_tsc() comment for rationale */
7c03156f 136 last = gtod->cycle_last;
51c19b4f
MT
137
138 if (likely(ret >= last))
139 return ret;
140
141 return last;
142}
143#endif
144
a5a1d1c2 145notrace static u64 vread_tsc(void)
2aae950b 146{
a5a1d1c2 147 u64 ret = (u64)rdtsc_ordered();
03b9730b 148 u64 last = gtod->cycle_last;
a939e817 149
411f790c
SS
150 if (likely(ret >= last))
151 return ret;
152
153 /*
154 * GCC likes to generate cmov here, but this branch is extremely
6a6256f9 155 * predictable (it's just a function of time and the likely is
411f790c
SS
156 * very likely) and there's a data dependence, so force GCC
157 * to generate a branch instead. I don't barrier() because
158 * we don't actually need a barrier, and if this function
159 * ever gets inlined it will generate worse code.
160 */
161 asm volatile ("");
162 return last;
163}
a939e817 164
51c19b4f 165notrace static inline u64 vgetsns(int *mode)
2aae950b 166{
7a59ed41 167 u64 v;
98d0ac38 168 cycles_t cycles;
7c03156f
SS
169
170 if (gtod->vclock_mode == VCLOCK_TSC)
98d0ac38 171 cycles = vread_tsc();
51c19b4f 172#ifdef CONFIG_PARAVIRT_CLOCK
7c03156f 173 else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
51c19b4f
MT
174 cycles = vread_pvclock(mode);
175#endif
a939e817
JS
176 else
177 return 0;
7c03156f
SS
178 v = (cycles - gtod->cycle_last) & gtod->mask;
179 return v * gtod->mult;
2aae950b
AK
180}
181
5f293474
AL
182/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
183notrace static int __always_inline do_realtime(struct timespec *ts)
2aae950b 184{
650ea024
JS
185 unsigned long seq;
186 u64 ns;
a939e817
JS
187 int mode;
188
2aae950b 189 do {
7c03156f
SS
190 seq = gtod_read_begin(gtod);
191 mode = gtod->vclock_mode;
2aae950b 192 ts->tv_sec = gtod->wall_time_sec;
650ea024 193 ns = gtod->wall_time_snsec;
51c19b4f 194 ns += vgetsns(&mode);
7c03156f
SS
195 ns >>= gtod->shift;
196 } while (unlikely(gtod_read_retry(gtod, seq)));
197
198 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
199 ts->tv_nsec = ns;
a939e817 200
a939e817 201 return mode;
2aae950b
AK
202}
203
7a59ed41 204notrace static int __always_inline do_monotonic(struct timespec *ts)
2aae950b 205{
650ea024
JS
206 unsigned long seq;
207 u64 ns;
a939e817
JS
208 int mode;
209
2aae950b 210 do {
7c03156f
SS
211 seq = gtod_read_begin(gtod);
212 mode = gtod->vclock_mode;
91ec87d5 213 ts->tv_sec = gtod->monotonic_time_sec;
650ea024 214 ns = gtod->monotonic_time_snsec;
51c19b4f 215 ns += vgetsns(&mode);
7c03156f
SS
216 ns >>= gtod->shift;
217 } while (unlikely(gtod_read_retry(gtod, seq)));
218
219 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
220 ts->tv_nsec = ns;
0f51f285 221
a939e817 222 return mode;
2aae950b
AK
223}
224
ce39c640 225notrace static void do_realtime_coarse(struct timespec *ts)
da15cfda 226{
227 unsigned long seq;
228 do {
7c03156f
SS
229 seq = gtod_read_begin(gtod);
230 ts->tv_sec = gtod->wall_time_coarse_sec;
231 ts->tv_nsec = gtod->wall_time_coarse_nsec;
232 } while (unlikely(gtod_read_retry(gtod, seq)));
da15cfda 233}
234
ce39c640 235notrace static void do_monotonic_coarse(struct timespec *ts)
da15cfda 236{
91ec87d5 237 unsigned long seq;
da15cfda 238 do {
7c03156f
SS
239 seq = gtod_read_begin(gtod);
240 ts->tv_sec = gtod->monotonic_time_coarse_sec;
241 ts->tv_nsec = gtod->monotonic_time_coarse_nsec;
242 } while (unlikely(gtod_read_retry(gtod, seq)));
da15cfda 243}
244
23adec55 245notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
2aae950b 246{
0d7b8547
AL
247 switch (clock) {
248 case CLOCK_REALTIME:
ce39c640
SS
249 if (do_realtime(ts) == VCLOCK_NONE)
250 goto fallback;
0d7b8547
AL
251 break;
252 case CLOCK_MONOTONIC:
ce39c640
SS
253 if (do_monotonic(ts) == VCLOCK_NONE)
254 goto fallback;
0d7b8547
AL
255 break;
256 case CLOCK_REALTIME_COARSE:
ce39c640
SS
257 do_realtime_coarse(ts);
258 break;
0d7b8547 259 case CLOCK_MONOTONIC_COARSE:
ce39c640
SS
260 do_monotonic_coarse(ts);
261 break;
262 default:
263 goto fallback;
0d7b8547
AL
264 }
265
a939e817 266 return 0;
ce39c640
SS
267fallback:
268 return vdso_fallback_gettime(clock, ts);
2aae950b
AK
269}
270int clock_gettime(clockid_t, struct timespec *)
271 __attribute__((weak, alias("__vdso_clock_gettime")));
272
23adec55 273notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
2aae950b 274{
a939e817 275 if (likely(tv != NULL)) {
0df1ea2b
SS
276 if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
277 return vdso_fallback_gtod(tv, tz);
a939e817 278 tv->tv_usec /= 1000;
2aae950b 279 }
a939e817 280 if (unlikely(tz != NULL)) {
7c03156f
SS
281 tz->tz_minuteswest = gtod->tz_minuteswest;
282 tz->tz_dsttime = gtod->tz_dsttime;
a939e817
JS
283 }
284
a939e817 285 return 0;
2aae950b
AK
286}
287int gettimeofday(struct timeval *, struct timezone *)
288 __attribute__((weak, alias("__vdso_gettimeofday")));
f144a6b4 289
0d7b8547
AL
290/*
291 * This will break when the xtime seconds get inaccurate, but that is
292 * unlikely
293 */
f144a6b4
AL
294notrace time_t __vdso_time(time_t *t)
295{
7a59ed41 296 /* This is atomic on x86 so we don't need any locks. */
af8c93d8 297 time_t result = ACCESS_ONCE(gtod->wall_time_sec);
f144a6b4
AL
298
299 if (t)
300 *t = result;
301 return result;
302}
303int time(time_t *t)
304 __attribute__((weak, alias("__vdso_time")));