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