blob: ee55754cc3c5ff378b76f2065a610b72e757f088 [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
Ingo Molnar2b7d0392008-11-12 13:17:38 +010012/* Disable profiling for userspace code: */
Steven Rostedt2ed84ee2008-11-12 15:24:24 -050013#define DISABLE_BRANCH_PROFILING
Ingo Molnar2b7d0392008-11-12 13:17:38 +010014
Andi Kleen2aae9502007-07-21 17:10:01 +020015#include <linux/kernel.h>
16#include <linux/posix-timers.h>
17#include <linux/time.h>
18#include <linux/string.h>
19#include <asm/vsyscall.h>
20#include <asm/vgtod.h>
21#include <asm/timex.h>
22#include <asm/hpet.h>
23#include <asm/unistd.h>
24#include <asm/io.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020025#include "vextern.h"
26
27#define gtod vdso_vsyscall_gtod_data
28
Steven Rostedt23adec52008-05-12 21:20:41 +020029notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020030{
31 long ret;
32 asm("syscall" : "=a" (ret) :
33 "0" (__NR_clock_gettime),"D" (clock), "S" (ts) : "memory");
34 return ret;
35}
36
Steven Rostedt23adec52008-05-12 21:20:41 +020037notrace static inline long vgetns(void)
Andi Kleen2aae9502007-07-21 17:10:01 +020038{
Andi Kleen95b08672007-09-11 14:02:09 +020039 long v;
Andi Kleen2aae9502007-07-21 17:10:01 +020040 cycles_t (*vread)(void);
41 vread = gtod->clock.vread;
Andi Kleen95b08672007-09-11 14:02:09 +020042 v = (vread() - gtod->clock.cycle_last) & gtod->clock.mask;
43 return (v * gtod->clock.mult) >> gtod->clock.shift;
Andi Kleen2aae9502007-07-21 17:10:01 +020044}
45
Steven Rostedt23adec52008-05-12 21:20:41 +020046notrace static noinline int do_realtime(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020047{
48 unsigned long seq, ns;
49 do {
50 seq = read_seqbegin(&gtod->lock);
51 ts->tv_sec = gtod->wall_time_sec;
52 ts->tv_nsec = gtod->wall_time_nsec;
53 ns = vgetns();
54 } while (unlikely(read_seqretry(&gtod->lock, seq)));
55 timespec_add_ns(ts, ns);
56 return 0;
57}
58
59/* Copy of the version in kernel/time.c which we cannot directly access */
Steven Rostedt23adec52008-05-12 21:20:41 +020060notrace static void
61vset_normalized_timespec(struct timespec *ts, long sec, long nsec)
Andi Kleen2aae9502007-07-21 17:10:01 +020062{
63 while (nsec >= NSEC_PER_SEC) {
64 nsec -= NSEC_PER_SEC;
65 ++sec;
66 }
67 while (nsec < 0) {
68 nsec += NSEC_PER_SEC;
69 --sec;
70 }
71 ts->tv_sec = sec;
72 ts->tv_nsec = nsec;
73}
74
Steven Rostedt23adec52008-05-12 21:20:41 +020075notrace static noinline int do_monotonic(struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +020076{
77 unsigned long seq, ns, secs;
78 do {
79 seq = read_seqbegin(&gtod->lock);
80 secs = gtod->wall_time_sec;
81 ns = gtod->wall_time_nsec + vgetns();
82 secs += gtod->wall_to_monotonic.tv_sec;
83 ns += gtod->wall_to_monotonic.tv_nsec;
84 } while (unlikely(read_seqretry(&gtod->lock, seq)));
85 vset_normalized_timespec(ts, secs, ns);
86 return 0;
87}
88
john stultzda15cfd2009-08-19 19:13:34 -070089notrace static noinline int do_realtime_coarse(struct timespec *ts)
90{
91 unsigned long seq;
92 do {
93 seq = read_seqbegin(&gtod->lock);
94 ts->tv_sec = gtod->wall_time_coarse.tv_sec;
95 ts->tv_nsec = gtod->wall_time_coarse.tv_nsec;
96 } while (unlikely(read_seqretry(&gtod->lock, seq)));
97 return 0;
98}
99
100notrace static noinline int do_monotonic_coarse(struct timespec *ts)
101{
102 unsigned long seq, ns, secs;
103 do {
104 seq = read_seqbegin(&gtod->lock);
105 secs = gtod->wall_time_coarse.tv_sec;
106 ns = gtod->wall_time_coarse.tv_nsec;
107 secs += gtod->wall_to_monotonic.tv_sec;
108 ns += gtod->wall_to_monotonic.tv_nsec;
109 } while (unlikely(read_seqretry(&gtod->lock, seq)));
110 vset_normalized_timespec(ts, secs, ns);
111 return 0;
112}
113
Steven Rostedt23adec52008-05-12 21:20:41 +0200114notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
Andi Kleen2aae9502007-07-21 17:10:01 +0200115{
john stultzda15cfd2009-08-19 19:13:34 -0700116 if (likely(gtod->sysctl_enabled))
Andi Kleen2aae9502007-07-21 17:10:01 +0200117 switch (clock) {
118 case CLOCK_REALTIME:
john stultzda15cfd2009-08-19 19:13:34 -0700119 if (likely(gtod->clock.vread))
120 return do_realtime(ts);
121 break;
Andi Kleen2aae9502007-07-21 17:10:01 +0200122 case CLOCK_MONOTONIC:
john stultzda15cfd2009-08-19 19:13:34 -0700123 if (likely(gtod->clock.vread))
124 return do_monotonic(ts);
125 break;
126 case CLOCK_REALTIME_COARSE:
127 return do_realtime_coarse(ts);
128 case CLOCK_MONOTONIC_COARSE:
129 return do_monotonic_coarse(ts);
Andi Kleen2aae9502007-07-21 17:10:01 +0200130 }
131 return vdso_fallback_gettime(clock, ts);
132}
133int clock_gettime(clockid_t, struct timespec *)
134 __attribute__((weak, alias("__vdso_clock_gettime")));
135
Steven Rostedt23adec52008-05-12 21:20:41 +0200136notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
Andi Kleen2aae9502007-07-21 17:10:01 +0200137{
138 long ret;
139 if (likely(gtod->sysctl_enabled && gtod->clock.vread)) {
John Wright2f65dd42009-04-29 14:32:01 -0600140 if (likely(tv != NULL)) {
141 BUILD_BUG_ON(offsetof(struct timeval, tv_usec) !=
142 offsetof(struct timespec, tv_nsec) ||
143 sizeof(*tv) != sizeof(struct timespec));
144 do_realtime((struct timespec *)tv);
145 tv->tv_usec /= 1000;
146 }
Andi Kleen2aae9502007-07-21 17:10:01 +0200147 if (unlikely(tz != NULL)) {
Andi Kleena1289642008-05-14 16:10:42 -0700148 /* Avoid memcpy. Some old compilers fail to inline it */
149 tz->tz_minuteswest = gtod->sys_tz.tz_minuteswest;
150 tz->tz_dsttime = gtod->sys_tz.tz_dsttime;
Andi Kleen2aae9502007-07-21 17:10:01 +0200151 }
152 return 0;
153 }
154 asm("syscall" : "=a" (ret) :
155 "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "memory");
156 return ret;
157}
158int gettimeofday(struct timeval *, struct timezone *)
159 __attribute__((weak, alias("__vdso_gettimeofday")));