x86-64: Remove kernel.vsyscall64 sysctl
[linux-2.6/x86.git] / arch / x86 / vdso / vclock_gettime.c
blobcf54813ac527db10c8168a3a5e915df10f371cf0
1 /*
2 * Copyright 2006 Andi Kleen, SUSE Labs.
3 * Subject to the GNU Public License, v.2
5 * Fast user context implementation of clock_gettime, gettimeofday, and time.
7 * The code should have no internal unresolved relocations.
8 * Check with readelf after changing.
9 * Also alternative() doesn't work.
12 /* Disable profiling for userspace code: */
13 #define DISABLE_BRANCH_PROFILING
15 #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>
20 #include <asm/vgtod.h>
21 #include <asm/timex.h>
22 #include <asm/hpet.h>
23 #include <asm/unistd.h>
24 #include <asm/io.h>
26 #define gtod (&VVAR(vsyscall_gtod_data))
28 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
30 long ret;
31 asm("syscall" : "=a" (ret) :
32 "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
33 return ret;
36 notrace static inline long vgetns(void)
38 long v;
39 cycles_t (*vread)(void);
40 vread = gtod->clock.vread;
41 v = (vread() - gtod->clock.cycle_last) & gtod->clock.mask;
42 return (v * gtod->clock.mult) >> gtod->clock.shift;
45 notrace static noinline int do_realtime(struct timespec *ts)
47 unsigned long seq, ns;
48 do {
49 seq = read_seqbegin(&gtod->lock);
50 ts->tv_sec = gtod->wall_time_sec;
51 ts->tv_nsec = gtod->wall_time_nsec;
52 ns = vgetns();
53 } while (unlikely(read_seqretry(&gtod->lock, seq)));
54 timespec_add_ns(ts, ns);
55 return 0;
58 notrace static noinline int do_monotonic(struct timespec *ts)
60 unsigned long seq, ns, secs;
61 do {
62 seq = read_seqbegin(&gtod->lock);
63 secs = gtod->wall_time_sec;
64 ns = gtod->wall_time_nsec + vgetns();
65 secs += gtod->wall_to_monotonic.tv_sec;
66 ns += gtod->wall_to_monotonic.tv_nsec;
67 } while (unlikely(read_seqretry(&gtod->lock, seq)));
69 /* wall_time_nsec, vgetns(), and wall_to_monotonic.tv_nsec
70 * are all guaranteed to be nonnegative.
72 while (ns >= NSEC_PER_SEC) {
73 ns -= NSEC_PER_SEC;
74 ++secs;
76 ts->tv_sec = secs;
77 ts->tv_nsec = ns;
79 return 0;
82 notrace static noinline int do_realtime_coarse(struct timespec *ts)
84 unsigned long seq;
85 do {
86 seq = read_seqbegin(&gtod->lock);
87 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
88 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
89 } while (unlikely(read_seqretry(&gtod->lock, seq)));
90 return 0;
93 notrace static noinline int do_monotonic_coarse(struct timespec *ts)
95 unsigned long seq, ns, secs;
96 do {
97 seq = read_seqbegin(&gtod->lock);
98 secs = gtod->wall_time_coarse.tv_sec;
99 ns = gtod->wall_time_coarse.tv_nsec;
100 secs += gtod->wall_to_monotonic.tv_sec;
101 ns += gtod->wall_to_monotonic.tv_nsec;
102 } while (unlikely(read_seqretry(&gtod->lock, seq)));
104 /* wall_time_nsec and wall_to_monotonic.tv_nsec are
105 * guaranteed to be between 0 and NSEC_PER_SEC.
107 if (ns >= NSEC_PER_SEC) {
108 ns -= NSEC_PER_SEC;
109 ++secs;
111 ts->tv_sec = secs;
112 ts->tv_nsec = ns;
114 return 0;
117 notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
119 switch (clock) {
120 case CLOCK_REALTIME:
121 if (likely(gtod->clock.vread))
122 return do_realtime(ts);
123 break;
124 case CLOCK_MONOTONIC:
125 if (likely(gtod->clock.vread))
126 return do_monotonic(ts);
127 break;
128 case CLOCK_REALTIME_COARSE:
129 return do_realtime_coarse(ts);
130 case CLOCK_MONOTONIC_COARSE:
131 return do_monotonic_coarse(ts);
134 return vdso_fallback_gettime(clock, ts);
136 int clock_gettime(clockid_t, struct timespec *)
137 __attribute__((weak, alias("__vdso_clock_gettime")));
139 notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
141 long ret;
142 if (likely(gtod->clock.vread)) {
143 if (likely(tv != NULL)) {
144 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
145 offsetof(struct timespec, tv_nsec) ||
146 sizeof(*tv) != sizeof(struct timespec));
147 do_realtime((struct timespec *)tv);
148 tv->tv_usec /= 1000;
150 if (unlikely(tz != NULL)) {
151 /* Avoid memcpy. Some old compilers fail to inline it */
152 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
153 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
155 return 0;
157 asm("syscall" : "=a" (ret) :
158 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
159 return ret;
161 int gettimeofday(struct timeval *, struct timezone *)
162 __attribute__((weak, alias("__vdso_gettimeofday")));
165 * This will break when the xtime seconds get inaccurate, but that is
166 * unlikely
168 notrace time_t __vdso_time(time_t *t)
170 /* This is atomic on x86_64 so we don't need any locks. */
171 time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
173 if (t)
174 *t = result;
175 return result;
177 int time(time_t *t)
178 __attribute__((weak, alias("__vdso_time")));