blob: 1ef0f90813d626ed6be436b93d3d5b6550dbb392 [file] [log] [blame]
Andi Kleen2aae9502007-07-21 17:10:01 +02001/*
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 and gettimeofday.
6 *
7 * The code should have no internal unresolved relocations.
8 * Check with readelf after changing.
9 * Also alternative() doesn't work.
10 */
11
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>
19#include <asm/hpet.h>
20#include <asm/unistd.h>
21#include <asm/io.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020022#include "vextern.h"
23
24#define gtod vdso_vsyscall_gtod_data
25
Steven Rostedt23adec52008-05-12 21:20:41 +020026notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020027{
28 long ret;
29 asm("syscall" : "=a" (ret) :
30 "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
31 return ret;
32}
33
Steven Rostedt23adec52008-05-12 21:20:41 +020034notrace static inline long vgetns(void)
Andi Kleen2aae9502007-07-21 17:10:01 +020035{
Andi Kleen95b08672007-09-11 14:02:09 +020036 long v;
Andi Kleen2aae9502007-07-21 17:10:01 +020037 cycles_t (*vread)(void);
38 vread = gtod->clock.vread;
Andi Kleen95b08672007-09-11 14:02:09 +020039 v = (vread() - gtod->clock.cycle_last) & gtod->clock.mask;
40 return (v * gtod->clock.mult) >> gtod->clock.shift;
Andi Kleen2aae9502007-07-21 17:10:01 +020041}
42
Steven Rostedt23adec52008-05-12 21:20:41 +020043notrace static noinline int do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020044{
45 unsigned long seq, ns;
46 do {
47 seq = read_seqbegin(&gtod->lock);
48 ts->tv_sec = gtod->wall_time_sec;
49 ts->tv_nsec = gtod->wall_time_nsec;
50 ns = vgetns();
51 } while (unlikely(read_seqretry(&gtod->lock, seq)));
52 timespec_add_ns(ts, ns);
53 return 0;
54}
55
56/* Copy of the version in kernel/time.c which we cannot directly access */
Steven Rostedt23adec52008-05-12 21:20:41 +020057notrace static void
58vset_normalized_timespec(struct timespec *ts, long sec, long nsec)
Andi Kleen2aae9502007-07-21 17:10:01 +020059{
60 while (nsec >= NSEC_PER_SEC) {
61 nsec -= NSEC_PER_SEC;
62 ++sec;
63 }
64 while (nsec < 0) {
65 nsec += NSEC_PER_SEC;
66 --sec;
67 }
68 ts->tv_sec = sec;
69 ts->tv_nsec = nsec;
70}
71
Steven Rostedt23adec52008-05-12 21:20:41 +020072notrace static noinline int do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020073{
74 unsigned long seq, ns, secs;
75 do {
76 seq = read_seqbegin(&gtod->lock);
77 secs = gtod->wall_time_sec;
78 ns = gtod->wall_time_nsec + vgetns();
79 secs += gtod->wall_to_monotonic.tv_sec;
80 ns += gtod->wall_to_monotonic.tv_nsec;
81 } while (unlikely(read_seqretry(&gtod->lock, seq)));
82 vset_normalized_timespec(ts, secs, ns);
83 return 0;
84}
85
Steven Rostedt23adec52008-05-12 21:20:41 +020086notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020087{
88 if (likely(gtod->sysctl_enabled && gtod->clock.vread))
89 switch (clock) {
90 case CLOCK_REALTIME:
91 return do_realtime(ts);
92 case CLOCK_MONOTONIC:
93 return do_monotonic(ts);
94 }
95 return vdso_fallback_gettime(clock, ts);
96}
97int clock_gettime(clockid_t, struct timespec *)
98 __attribute__((weak, alias("__vdso_clock_gettime")));
99
Steven Rostedt23adec52008-05-12 21:20:41 +0200100notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200101{
102 long ret;
103 if (likely(gtod->sysctl_enabled && gtod->clock.vread)) {
104 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
105 offsetof(struct timespec, tv_nsec) ||
106 sizeof(*tv) != sizeof(struct timespec));
107 do_realtime((struct timespec *)tv);
108 tv->tv_usec /= 1000;
109 if (unlikely(tz != NULL)) {
Andi Kleena1289642008-05-14 16:10:42 -0700110 /* Avoid memcpy. Some old compilers fail to inline it */
111 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
112 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
Andi Kleen2aae9502007-07-21 17:10:01 +0200113 }
114 return 0;
115 }
116 asm("syscall" : "=a" (ret) :
117 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
118 return ret;
119}
120int gettimeofday(struct timeval *, struct timezone *)
121 __attribute__((weak, alias("__vdso_gettimeofday")));