2 * Copyright 2006 Andi Kleen, SUSE Labs.
3 * Subject to the GNU Public License, v.2
5 * Fast user context implementation of clock_gettime and gettimeofday.
7 * The code should have no internal unresolved relocations.
8 * Check with readelf after changing.
9 * Also alternative() doesn't work.
12 #include <linux/kernel.h>
13 #include <linux/posix-timers.h>
14 #include <linux/time.h>
15 #include <linux/string.h>
16 #include <asm/vsyscall.h>
17 #include <asm/vgtod.h>
18 #include <asm/timex.h>
20 #include <asm/unistd.h>
24 #define gtod vdso_vsyscall_gtod_data
26 static long vdso_fallback_gettime(long clock
, struct timespec
*ts
)
29 asm("syscall" : "=a" (ret
) :
30 "0" (__NR_clock_gettime
),"D" (clock
), "S" (ts
) : "memory");
34 static inline long vgetns(void)
37 cycles_t (*vread
)(void);
38 vread
= gtod
->clock
.vread
;
39 v
= (vread() - gtod
->clock
.cycle_last
) & gtod
->clock
.mask
;
40 return (v
* gtod
->clock
.mult
) >> gtod
->clock
.shift
;
43 static noinline
int do_realtime(struct timespec
*ts
)
45 unsigned long seq
, ns
;
47 seq
= read_seqbegin(>od
->lock
);
48 ts
->tv_sec
= gtod
->wall_time_sec
;
49 ts
->tv_nsec
= gtod
->wall_time_nsec
;
51 } while (unlikely(read_seqretry(>od
->lock
, seq
)));
52 timespec_add_ns(ts
, ns
);
56 /* Copy of the version in kernel/time.c which we cannot directly access */
57 static void vset_normalized_timespec(struct timespec
*ts
, long sec
, long nsec
)
59 while (nsec
>= NSEC_PER_SEC
) {
71 static noinline
int do_monotonic(struct timespec
*ts
)
73 unsigned long seq
, ns
, secs
;
75 seq
= read_seqbegin(>od
->lock
);
76 secs
= gtod
->wall_time_sec
;
77 ns
= gtod
->wall_time_nsec
+ vgetns();
78 secs
+= gtod
->wall_to_monotonic
.tv_sec
;
79 ns
+= gtod
->wall_to_monotonic
.tv_nsec
;
80 } while (unlikely(read_seqretry(>od
->lock
, seq
)));
81 vset_normalized_timespec(ts
, secs
, ns
);
85 int __vdso_clock_gettime(clockid_t clock
, struct timespec
*ts
)
87 if (likely(gtod
->sysctl_enabled
&& gtod
->clock
.vread
))
90 return do_realtime(ts
);
92 return do_monotonic(ts
);
94 return vdso_fallback_gettime(clock
, ts
);
96 int clock_gettime(clockid_t
, struct timespec
*)
97 __attribute__((weak
, alias("__vdso_clock_gettime")));
99 int __vdso_gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
102 if (likely(gtod
->sysctl_enabled
&& gtod
->clock
.vread
)) {
103 BUILD_BUG_ON(offsetof(struct timeval
, tv_usec
) !=
104 offsetof(struct timespec
, tv_nsec
) ||
105 sizeof(*tv
) != sizeof(struct timespec
));
106 do_realtime((struct timespec
*)tv
);
108 if (unlikely(tz
!= NULL
)) {
109 /* Avoid memcpy. Some old compilers fail to inline it */
110 tz
->tz_minuteswest
= gtod
->sys_tz
.tz_minuteswest
;
111 tz
->tz_dsttime
= gtod
->sys_tz
.tz_dsttime
;
115 asm("syscall" : "=a" (ret
) :
116 "0" (__NR_gettimeofday
), "D" (tv
), "S" (tz
) : "memory");
119 int gettimeofday(struct timeval
*, struct timezone
*)
120 __attribute__((weak
, alias("__vdso_gettimeofday")));