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 * 32 Bit compat layer by Stefani Seibold <stefani@seibold.net>
8 * sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany
10 * The code should have no internal unresolved relocations.
11 * Check with readelf after changing.
14 #include <uapi/linux/time.h>
15 #include <asm/vgtod.h>
17 #include <asm/unistd.h>
19 #include <asm/pvclock.h>
20 #include <linux/math64.h>
21 #include <linux/time.h>
22 #include <linux/kernel.h>
24 #define gtod (&VVAR(vsyscall_gtod_data))
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);
30 #ifdef CONFIG_PARAVIRT_CLOCK
31 extern u8 pvclock_page
32 __attribute__((visibility("hidden")));
37 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
40 asm("syscall" : "=a" (ret) :
41 "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory");
45 notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
49 asm("syscall" : "=a" (ret) :
50 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
57 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
64 "call __kernel_vsyscall \n"
67 : "0" (__NR_clock_gettime), "g" (clock), "c" (ts)
72 notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
79 "call __kernel_vsyscall \n"
82 : "0" (__NR_gettimeofday), "g" (tv), "c" (tz)
89 #ifdef CONFIG_PARAVIRT_CLOCK
90 static notrace const struct pvclock_vsyscall_time_info *get_pvti0(void)
92 return (const struct pvclock_vsyscall_time_info *)&pvclock_page;
95 static notrace cycle_t vread_pvclock(int *mode)
97 const struct pvclock_vcpu_time_info *pvti = &get_pvti0()->pvti;
100 u64 last, delta, pvti_system_time;
101 u32 version, pvti_tsc_to_system_mul, pvti_tsc_shift;
104 * Note: The kernel and hypervisor must guarantee that cpu ID
105 * number maps 1:1 to per-CPU pvclock time info.
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.
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.
116 * On Xen, we don't appear to have that guarantee, but Xen still
117 * supplies a valid seqlock using the version field.
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.
126 version = pvclock_read_begin(pvti);
128 if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT))) {
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));
140 delta = tsc - pvti_tsc;
141 ret = pvti_system_time +
142 pvclock_scale_delta(delta, pvti_tsc_to_system_mul,
145 /* refer to vread_tsc() comment for rationale */
146 last = gtod->cycle_last;
148 if (likely(ret >= last))
155 notrace static cycle_t vread_tsc(void)
157 cycle_t ret = (cycle_t)rdtsc_ordered();
158 u64 last = gtod->cycle_last;
160 if (likely(ret >= last))
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.
175 notrace static inline u64 vgetsns(int *mode)
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);
188 v = (cycles - gtod->cycle_last) & gtod->mask;
189 return v * gtod->mult;
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)
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);
206 } while (unlikely(gtod_read_retry(gtod, seq)));
208 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
214 notrace static int __always_inline do_monotonic(struct timespec *ts)
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);
227 } while (unlikely(gtod_read_retry(gtod, seq)));
229 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
235 notrace static void do_realtime_coarse(struct timespec *ts)
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)));
245 notrace static void do_monotonic_coarse(struct timespec *ts)
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)));
255 notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
259 if (do_realtime(ts) == VCLOCK_NONE)
262 case CLOCK_MONOTONIC:
263 if (do_monotonic(ts) == VCLOCK_NONE)
266 case CLOCK_REALTIME_COARSE:
267 do_realtime_coarse(ts);
269 case CLOCK_MONOTONIC_COARSE:
270 do_monotonic_coarse(ts);
278 return vdso_fallback_gettime(clock, ts);
280 int clock_gettime(clockid_t, struct timespec *)
281 __attribute__((weak, alias("__vdso_clock_gettime")));
283 notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
285 if (likely(tv != NULL)) {
286 if (unlikely(do_realtime((struct timespec *)tv) == VCLOCK_NONE))
287 return vdso_fallback_gtod(tv, tz);
290 if (unlikely(tz != NULL)) {
291 tz->tz_minuteswest = gtod->tz_minuteswest;
292 tz->tz_dsttime = gtod->tz_dsttime;
297 int gettimeofday(struct timeval *, struct timezone *)
298 __attribute__((weak, alias("__vdso_gettimeofday")));
301 * This will break when the xtime seconds get inaccurate, but that is
304 notrace time_t __vdso_time(time_t *t)
306 /* This is atomic on x86 so we don't need any locks. */
307 time_t result = ACCESS_ONCE(gtod->wall_time_sec);
314 __attribute__((weak, alias("__vdso_time")));