thunderbolt: Use kcalloc
[deliverable/linux.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 * 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 /* Disable profiling for userspace code: */
15 #define DISABLE_BRANCH_PROFILING
16
17 #include <uapi/linux/time.h>
18 #include <asm/vgtod.h>
19 #include <asm/hpet.h>
20 #include <asm/vvar.h>
21 #include <asm/unistd.h>
22 #include <asm/msr.h>
23 #include <linux/math64.h>
24 #include <linux/time.h>
25
26 #define gtod (&VVAR(vsyscall_gtod_data))
27
28 extern int __vdso_clock_gettime(clockid_t clock, struct timespec *ts);
29 extern int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz);
30 extern time_t __vdso_time(time_t *t);
31
32 #ifdef CONFIG_HPET_TIMER
33 extern u8 hpet_page
34 __attribute__((visibility("hidden")));
35
36 static notrace cycle_t vread_hpet(void)
37 {
38 return *(const volatile u32 *)(&hpet_page + HPET_COUNTER);
39 }
40 #endif
41
42 #ifndef BUILD_VDSO32
43
44 #include <linux/kernel.h>
45 #include <asm/vsyscall.h>
46 #include <asm/fixmap.h>
47 #include <asm/pvclock.h>
48
49 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
50 {
51 long ret;
52 asm("syscall" : "=a" (ret) :
53 "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory");
54 return ret;
55 }
56
57 notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
58 {
59 long ret;
60
61 asm("syscall" : "=a" (ret) :
62 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
63 return ret;
64 }
65
66 #ifdef CONFIG_PARAVIRT_CLOCK
67
68 static notrace const struct pvclock_vsyscall_time_info *get_pvti(int cpu)
69 {
70 const struct pvclock_vsyscall_time_info *pvti_base;
71 int idx = cpu / (PAGE_SIZE/PVTI_SIZE);
72 int offset = cpu % (PAGE_SIZE/PVTI_SIZE);
73
74 BUG_ON(PVCLOCK_FIXMAP_BEGIN + idx > PVCLOCK_FIXMAP_END);
75
76 pvti_base = (struct pvclock_vsyscall_time_info *)
77 __fix_to_virt(PVCLOCK_FIXMAP_BEGIN+idx);
78
79 return &pvti_base[offset];
80 }
81
82 static notrace cycle_t vread_pvclock(int *mode)
83 {
84 const struct pvclock_vsyscall_time_info *pvti;
85 cycle_t ret;
86 u64 last;
87 u32 version;
88 u8 flags;
89 unsigned cpu, cpu1;
90
91
92 /*
93 * Note: hypervisor must guarantee that:
94 * 1. cpu ID number maps 1:1 to per-CPU pvclock time info.
95 * 2. that per-CPU pvclock time info is updated if the
96 * underlying CPU changes.
97 * 3. that version is increased whenever underlying CPU
98 * changes.
99 *
100 */
101 do {
102 cpu = __getcpu() & VGETCPU_CPU_MASK;
103 /* TODO: We can put vcpu id into higher bits of pvti.version.
104 * This will save a couple of cycles by getting rid of
105 * __getcpu() calls (Gleb).
106 */
107
108 pvti = get_pvti(cpu);
109
110 version = __pvclock_read_cycles(&pvti->pvti, &ret, &flags);
111
112 /*
113 * Test we're still on the cpu as well as the version.
114 * We could have been migrated just after the first
115 * vgetcpu but before fetching the version, so we
116 * wouldn't notice a version change.
117 */
118 cpu1 = __getcpu() & VGETCPU_CPU_MASK;
119 } while (unlikely(cpu != cpu1 ||
120 (pvti->pvti.version & 1) ||
121 pvti->pvti.version != version));
122
123 if (unlikely(!(flags & PVCLOCK_TSC_STABLE_BIT)))
124 *mode = VCLOCK_NONE;
125
126 /* refer to tsc.c read_tsc() comment for rationale */
127 last = gtod->cycle_last;
128
129 if (likely(ret >= last))
130 return ret;
131
132 return last;
133 }
134 #endif
135
136 #else
137
138 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
139 {
140 long ret;
141
142 asm(
143 "mov %%ebx, %%edx \n"
144 "mov %2, %%ebx \n"
145 "call __kernel_vsyscall \n"
146 "mov %%edx, %%ebx \n"
147 : "=a" (ret)
148 : "0" (__NR_clock_gettime), "g" (clock), "c" (ts)
149 : "memory", "edx");
150 return ret;
151 }
152
153 notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
154 {
155 long ret;
156
157 asm(
158 "mov %%ebx, %%edx \n"
159 "mov %2, %%ebx \n"
160 "call __kernel_vsyscall \n"
161 "mov %%edx, %%ebx \n"
162 : "=a" (ret)
163 : "0" (__NR_gettimeofday), "g" (tv), "c" (tz)
164 : "memory", "edx");
165 return ret;
166 }
167
168 #ifdef CONFIG_PARAVIRT_CLOCK
169
170 static notrace cycle_t vread_pvclock(int *mode)
171 {
172 *mode = VCLOCK_NONE;
173 return 0;
174 }
175 #endif
176
177 #endif
178
179 notrace static cycle_t vread_tsc(void)
180 {
181 cycle_t ret;
182 u64 last;
183
184 /*
185 * Empirically, a fence (of type that depends on the CPU)
186 * before rdtsc is enough to ensure that rdtsc is ordered
187 * with respect to loads. The various CPU manuals are unclear
188 * as to whether rdtsc can be reordered with later loads,
189 * but no one has ever seen it happen.
190 */
191 rdtsc_barrier();
192 ret = (cycle_t)__native_read_tsc();
193
194 last = gtod->cycle_last;
195
196 if (likely(ret >= last))
197 return ret;
198
199 /*
200 * GCC likes to generate cmov here, but this branch is extremely
201 * predictable (it's just a funciton of time and the likely is
202 * very likely) and there's a data dependence, so force GCC
203 * to generate a branch instead. I don't barrier() because
204 * we don't actually need a barrier, and if this function
205 * ever gets inlined it will generate worse code.
206 */
207 asm volatile ("");
208 return last;
209 }
210
211 notrace static inline u64 vgetsns(int *mode)
212 {
213 u64 v;
214 cycles_t cycles;
215
216 if (gtod->vclock_mode == VCLOCK_TSC)
217 cycles = vread_tsc();
218 #ifdef CONFIG_HPET_TIMER
219 else if (gtod->vclock_mode == VCLOCK_HPET)
220 cycles = vread_hpet();
221 #endif
222 #ifdef CONFIG_PARAVIRT_CLOCK
223 else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
224 cycles = vread_pvclock(mode);
225 #endif
226 else
227 return 0;
228 v = (cycles - gtod->cycle_last) & gtod->mask;
229 return v * gtod->mult;
230 }
231
232 /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */
233 notrace static int __always_inline do_realtime(struct timespec *ts)
234 {
235 unsigned long seq;
236 u64 ns;
237 int mode;
238
239 do {
240 seq = gtod_read_begin(gtod);
241 mode = gtod->vclock_mode;
242 ts->tv_sec = gtod->wall_time_sec;
243 ns = gtod->wall_time_snsec;
244 ns += vgetsns(&mode);
245 ns >>= gtod->shift;
246 } while (unlikely(gtod_read_retry(gtod, seq)));
247
248 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
249 ts->tv_nsec = ns;
250
251 return mode;
252 }
253
254 notrace static int __always_inline do_monotonic(struct timespec *ts)
255 {
256 unsigned long seq;
257 u64 ns;
258 int mode;
259
260 do {
261 seq = gtod_read_begin(gtod);
262 mode = gtod->vclock_mode;
263 ts->tv_sec = gtod->monotonic_time_sec;
264 ns = gtod->monotonic_time_snsec;
265 ns += vgetsns(&mode);
266 ns >>= gtod->shift;
267 } while (unlikely(gtod_read_retry(gtod, seq)));
268
269 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
270 ts->tv_nsec = ns;
271
272 return mode;
273 }
274
275 notrace static void do_realtime_coarse(struct timespec *ts)
276 {
277 unsigned long seq;
278 do {
279 seq = gtod_read_begin(gtod);
280 ts->tv_sec = gtod->wall_time_coarse_sec;
281 ts->tv_nsec = gtod->wall_time_coarse_nsec;
282 } while (unlikely(gtod_read_retry(gtod, seq)));
283 }
284
285 notrace static void do_monotonic_coarse(struct timespec *ts)
286 {
287 unsigned long seq;
288 do {
289 seq = gtod_read_begin(gtod);
290 ts->tv_sec = gtod->monotonic_time_coarse_sec;
291 ts->tv_nsec = gtod->monotonic_time_coarse_nsec;
292 } while (unlikely(gtod_read_retry(gtod, seq)));
293 }
294
295 notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
296 {
297 switch (clock) {
298 case CLOCK_REALTIME:
299 if (do_realtime(ts) == VCLOCK_NONE)
300 goto fallback;
301 break;
302 case CLOCK_MONOTONIC:
303 if (do_monotonic(ts) == VCLOCK_NONE)
304 goto fallback;
305 break;
306 case CLOCK_REALTIME_COARSE:
307 do_realtime_coarse(ts);
308 break;
309 case CLOCK_MONOTONIC_COARSE:
310 do_monotonic_coarse(ts);
311 break;
312 default:
313 goto fallback;
314 }
315
316 return 0;
317 fallback:
318 return vdso_fallback_gettime(clock, ts);
319 }
320 int clock_gettime(clockid_t, struct timespec *)
321 __attribute__((weak, alias("__vdso_clock_gettime")));
322
323 notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
324 {
325 if (likely(tv != NULL)) {
326 if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
327 return vdso_fallback_gtod(tv, tz);
328 tv->tv_usec /= 1000;
329 }
330 if (unlikely(tz != NULL)) {
331 tz->tz_minuteswest = gtod->tz_minuteswest;
332 tz->tz_dsttime = gtod->tz_dsttime;
333 }
334
335 return 0;
336 }
337 int gettimeofday(struct timeval *, struct timezone *)
338 __attribute__((weak, alias("__vdso_gettimeofday")));
339
340 /*
341 * This will break when the xtime seconds get inaccurate, but that is
342 * unlikely
343 */
344 notrace time_t __vdso_time(time_t *t)
345 {
346 /* This is atomic on x86 so we don't need any locks. */
347 time_t result = ACCESS_ONCE(gtod->wall_time_sec);
348
349 if (t)
350 *t = result;
351 return result;
352 }
353 int time(time_t *t)
354 __attribute__((weak, alias("__vdso_time")));
This page took 0.15874 seconds and 5 git commands to generate.