Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / vdso / vclock_gettime.c
1 /*
2  * Copyright 2006 Andi Kleen, SUSE Labs.
3  * Subject to the GNU Public License, v.2
4  *
5  * Fast user context implementation of clock_gettime, gettimeofday, and time.
6  *
7  * The code should have no internal unresolved relocations.
8  * Check with readelf after changing.
9  */
10
11 /* Disable profiling for userspace code: */
12 #define DISABLE_BRANCH_PROFILING
13
14 #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>
19 #include <asm/fixmap.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>
25
26 #define gtod (&VVAR(vsyscall_gtod_data))
27
28 #ifdef CONFIG_XEN
29 #define VCLOCK_NONE 0
30 #else
31 notrace static cycle_t vread_tsc(void)
32 {
33         cycle_t ret;
34         u64 last;
35
36         /*
37          * Empirically, a fence (of type that depends on the CPU)
38          * before rdtsc is enough to ensure that rdtsc is ordered
39          * with respect to loads.  The various CPU manuals are unclear
40          * as to whether rdtsc can be reordered with later loads,
41          * but no one has ever seen it happen.
42          */
43         rdtsc_barrier();
44         ret = (cycle_t)vget_cycles();
45
46         last = VVAR(vsyscall_gtod_data).clock.cycle_last;
47
48         if (likely(ret >= last))
49                 return ret;
50
51         /*
52          * GCC likes to generate cmov here, but this branch is extremely
53          * predictable (it's just a funciton of time and the likely is
54          * very likely) and there's a data dependence, so force GCC
55          * to generate a branch instead.  I don't barrier() because
56          * we don't actually need a barrier, and if this function
57          * ever gets inlined it will generate worse code.
58          */
59         asm volatile ("");
60         return last;
61 }
62
63 static notrace cycle_t vread_hpet(void)
64 {
65         return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
66 }
67 #endif /* CONFIG_XEN */
68
69 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
70 {
71         long ret;
72         asm("syscall" : "=a" (ret) :
73             "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
74         return ret;
75 }
76
77 notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
78 {
79         long ret;
80
81         asm("syscall" : "=a" (ret) :
82             "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
83         return ret;
84 }
85
86
87 #ifndef CONFIG_XEN
88 notrace static inline long vgetns(void)
89 {
90         long v;
91         cycles_t cycles;
92         if (gtod->clock.vclock_mode == VCLOCK_TSC)
93                 cycles = vread_tsc();
94         else if (gtod->clock.vclock_mode == VCLOCK_HPET)
95                 cycles = vread_hpet();
96         else
97                 return 0;
98         v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask;
99         return (v * gtod->clock.mult) >> gtod->clock.shift;
100 }
101
102 /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
103 notrace static int __always_inline do_realtime(struct timespec *ts)
104 {
105         unsigned long seq, ns;
106         int mode;
107
108         do {
109                 seq = read_seqcount_begin(&gtod->seq);
110                 mode = gtod->clock.vclock_mode;
111                 ts->tv_sec = gtod->wall_time_sec;
112                 ts->tv_nsec = gtod->wall_time_nsec;
113                 ns = vgetns();
114         } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
115
116         timespec_add_ns(ts, ns);
117         return mode;
118 }
119
120 notrace static int do_monotonic(struct timespec *ts)
121 {
122         unsigned long seq, ns;
123         int mode;
124
125         do {
126                 seq = read_seqcount_begin(&gtod->seq);
127                 mode = gtod->clock.vclock_mode;
128                 ts->tv_sec = gtod->monotonic_time_sec;
129                 ts->tv_nsec = gtod->monotonic_time_nsec;
130                 ns = vgetns();
131         } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
132         timespec_add_ns(ts, ns);
133
134         return mode;
135 }
136 #endif /* CONFIG_XEN */
137
138 notrace static int do_realtime_coarse(struct timespec *ts)
139 {
140         unsigned long seq;
141         do {
142                 seq = read_seqcount_begin(&gtod->seq);
143                 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
144                 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
145         } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
146         return 0;
147 }
148
149 notrace static int do_monotonic_coarse(struct timespec *ts)
150 {
151         unsigned long seq;
152         do {
153                 seq = read_seqcount_begin(&gtod->seq);
154                 ts->tv_sec = gtod->monotonic_time_coarse.tv_sec;
155                 ts->tv_nsec = gtod->monotonic_time_coarse.tv_nsec;
156         } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
157
158         return 0;
159 }
160
161 notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
162 {
163         int ret = VCLOCK_NONE;
164
165         switch (clock) {
166 #ifndef CONFIG_XEN
167         case CLOCK_REALTIME:
168                 ret = do_realtime(ts);
169                 break;
170         case CLOCK_MONOTONIC:
171                 ret = do_monotonic(ts);
172                 break;
173 #endif
174         case CLOCK_REALTIME_COARSE:
175                 return do_realtime_coarse(ts);
176         case CLOCK_MONOTONIC_COARSE:
177                 return do_monotonic_coarse(ts);
178         }
179
180         if (ret == VCLOCK_NONE)
181                 return vdso_fallback_gettime(clock, ts);
182         return 0;
183 }
184 int clock_gettime(clockid_t, struct timespec *)
185         __attribute__((weak, alias("__vdso_clock_gettime")));
186
187 notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
188 {
189         long ret = VCLOCK_NONE;
190
191 #ifndef CONFIG_XEN
192         if (likely(tv != NULL)) {
193                 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
194                              offsetof(struct timespec, tv_nsec) ||
195                              sizeof(*tv) != sizeof(struct timespec));
196                 ret = do_realtime((struct timespec *)tv);
197                 tv->tv_usec /= 1000;
198         }
199         if (unlikely(tz != NULL)) {
200                 /* Avoid memcpy. Some old compilers fail to inline it */
201                 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
202                 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
203         }
204 #endif
205
206         if (ret == VCLOCK_NONE)
207                 return vdso_fallback_gtod(tv, tz);
208         return 0;
209 }
210 int gettimeofday(struct timeval *, struct timezone *)
211         __attribute__((weak, alias("__vdso_gettimeofday")));
212
213 /*
214  * This will break when the xtime seconds get inaccurate, but that is
215  * unlikely
216  */
217 notrace time_t __vdso_time(time_t *t)
218 {
219         /* This is atomic on x86_64 so we don't need any locks. */
220         time_t result = ACCESS_ONCE(VVAR(vsyscall_gtod_data).wall_time_sec);
221
222         if (t)
223                 *t = result;
224         return result;
225 }
226 int time(time_t *t)
227         __attribute__((weak, alias("__vdso_time")));