7b539ceebe13a189b6b09a877bae3d0cbacce0f3
[muen/linux.git] / arch / sparc / 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  * The code should have no internal unresolved relocations.
8  * Check with readelf after changing.
9  * Also alternative() doesn't work.
10  */
11 /*
12  * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/time.h>
17 #include <linux/string.h>
18 #include <asm/io.h>
19 #include <asm/unistd.h>
20 #include <asm/timex.h>
21 #include <asm/clocksource.h>
22 #include <asm/vvar.h>
23
24 #ifdef  CONFIG_SPARC64
25 #define SYSCALL_STRING                                                  \
26         "ta     0x6d;"                                                  \
27         "bcs,a  1f;"                                                    \
28         " sub   %%g0, %%o0, %%o0;"                                      \
29         "1:"
30 #else
31 #define SYSCALL_STRING                                                  \
32         "ta     0x10;"                                                  \
33         "bcs,a  1f;"                                                    \
34         " sub   %%g0, %%o0, %%o0;"                                      \
35         "1:"
36 #endif
37
38 #define SYSCALL_CLOBBERS                                                \
39         "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",                 \
40         "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",           \
41         "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",         \
42         "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",         \
43         "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46",         \
44         "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62",         \
45         "cc", "memory"
46
47 /*
48  * Compute the vvar page's address in the process address space, and return it
49  * as a pointer to the vvar_data.
50  */
51 notrace static __always_inline struct vvar_data *get_vvar_data(void)
52 {
53         unsigned long ret;
54
55         /*
56          * vdso data page is the first vDSO page so grab the PC
57          * and move up a page to get to the data page.
58          */
59         __asm__("rd %%pc, %0" : "=r" (ret));
60         ret &= ~(8192 - 1);
61         ret -= 8192;
62
63         return (struct vvar_data *) ret;
64 }
65
66 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
67 {
68         register long num __asm__("g1") = __NR_clock_gettime;
69         register long o0 __asm__("o0") = clock;
70         register long o1 __asm__("o1") = (long) ts;
71
72         __asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
73                              "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
74         return o0;
75 }
76
77 notrace static long vdso_fallback_gettimeofday(struct timeval *tv, struct timezone *tz)
78 {
79         register long num __asm__("g1") = __NR_gettimeofday;
80         register long o0 __asm__("o0") = (long) tv;
81         register long o1 __asm__("o1") = (long) tz;
82
83         __asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
84                              "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
85         return o0;
86 }
87
88 #ifdef  CONFIG_SPARC64
89 notrace static __always_inline u64 vread_tick(void)
90 {
91         u64     ret;
92
93         __asm__ __volatile__("1:\n\t"
94                              "rd                %%tick, %0\n\t"
95                              ".pushsection      .tick_patch, \"a\"\n\t"
96                              ".word             1b - ., 1f - .\n\t"
97                              ".popsection\n\t"
98                              ".pushsection      .tick_patch_replacement, \"ax\"\n\t"
99                              "1:\n\t"
100                              "rd                %%asr24, %0\n\t"
101                              ".popsection\n"
102                              : "=r" (ret));
103         return ret;
104 }
105 #else
106 notrace static __always_inline u64 vread_tick(void)
107 {
108         register unsigned long long ret asm("o4");
109
110         __asm__ __volatile__("1:\n\t"
111                              "rd                %%tick, %L0\n\t"
112                              "srlx              %L0, 32, %H0\n\t"
113                              ".pushsection      .tick_patch, \"a\"\n\t"
114                              ".word             1b - ., 1f - .\n\t"
115                              ".popsection\n\t"
116                              ".pushsection      .tick_patch_replacement, \"ax\"\n\t"
117                              "1:\n\t"
118                              "rd                %%asr24, %L0\n\t"
119                              ".popsection\n"
120                              : "=r" (ret));
121         return ret;
122 }
123 #endif
124
125 notrace static __always_inline u64 vgetsns(struct vvar_data *vvar)
126 {
127         u64 v;
128         u64 cycles;
129
130         cycles = vread_tick();
131         v = (cycles - vvar->clock.cycle_last) & vvar->clock.mask;
132         return v * vvar->clock.mult;
133 }
134
135 notrace static __always_inline int do_realtime(struct vvar_data *vvar,
136                                                struct timespec *ts)
137 {
138         unsigned long seq;
139         u64 ns;
140
141         do {
142                 seq = vvar_read_begin(vvar);
143                 ts->tv_sec = vvar->wall_time_sec;
144                 ns = vvar->wall_time_snsec;
145                 ns += vgetsns(vvar);
146                 ns >>= vvar->clock.shift;
147         } while (unlikely(vvar_read_retry(vvar, seq)));
148
149         ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
150         ts->tv_nsec = ns;
151
152         return 0;
153 }
154
155 notrace static __always_inline int do_monotonic(struct vvar_data *vvar,
156                                                 struct timespec *ts)
157 {
158         unsigned long seq;
159         u64 ns;
160
161         do {
162                 seq = vvar_read_begin(vvar);
163                 ts->tv_sec = vvar->monotonic_time_sec;
164                 ns = vvar->monotonic_time_snsec;
165                 ns += vgetsns(vvar);
166                 ns >>= vvar->clock.shift;
167         } while (unlikely(vvar_read_retry(vvar, seq)));
168
169         ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
170         ts->tv_nsec = ns;
171
172         return 0;
173 }
174
175 notrace static int do_realtime_coarse(struct vvar_data *vvar,
176                                       struct timespec *ts)
177 {
178         unsigned long seq;
179
180         do {
181                 seq = vvar_read_begin(vvar);
182                 ts->tv_sec = vvar->wall_time_coarse_sec;
183                 ts->tv_nsec = vvar->wall_time_coarse_nsec;
184         } while (unlikely(vvar_read_retry(vvar, seq)));
185         return 0;
186 }
187
188 notrace static int do_monotonic_coarse(struct vvar_data *vvar,
189                                        struct timespec *ts)
190 {
191         unsigned long seq;
192
193         do {
194                 seq = vvar_read_begin(vvar);
195                 ts->tv_sec = vvar->monotonic_time_coarse_sec;
196                 ts->tv_nsec = vvar->monotonic_time_coarse_nsec;
197         } while (unlikely(vvar_read_retry(vvar, seq)));
198
199         return 0;
200 }
201
202 notrace int
203 __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
204 {
205         struct vvar_data *vvd = get_vvar_data();
206
207         switch (clock) {
208         case CLOCK_REALTIME:
209                 if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
210                         break;
211                 return do_realtime(vvd, ts);
212         case CLOCK_MONOTONIC:
213                 if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
214                         break;
215                 return do_monotonic(vvd, ts);
216         case CLOCK_REALTIME_COARSE:
217                 return do_realtime_coarse(vvd, ts);
218         case CLOCK_MONOTONIC_COARSE:
219                 return do_monotonic_coarse(vvd, ts);
220         }
221         /*
222          * Unknown clock ID ? Fall back to the syscall.
223          */
224         return vdso_fallback_gettime(clock, ts);
225 }
226 int
227 clock_gettime(clockid_t, struct timespec *)
228         __attribute__((weak, alias("__vdso_clock_gettime")));
229
230 notrace int
231 __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
232 {
233         struct vvar_data *vvd = get_vvar_data();
234
235         if (likely(vvd->vclock_mode != VCLOCK_NONE)) {
236                 if (likely(tv != NULL)) {
237                         union tstv_t {
238                                 struct timespec ts;
239                                 struct timeval tv;
240                         } *tstv = (union tstv_t *) tv;
241                         do_realtime(vvd, &tstv->ts);
242                         /*
243                          * Assign before dividing to ensure that the division is
244                          * done in the type of tv_usec, not tv_nsec.
245                          *
246                          * There cannot be > 1 billion usec in a second:
247                          * do_realtime() has already distributed such overflow
248                          * into tv_sec.  So we can assign it to an int safely.
249                          */
250                         tstv->tv.tv_usec = tstv->ts.tv_nsec;
251                         tstv->tv.tv_usec /= 1000;
252                 }
253                 if (unlikely(tz != NULL)) {
254                         /* Avoid memcpy. Some old compilers fail to inline it */
255                         tz->tz_minuteswest = vvd->tz_minuteswest;
256                         tz->tz_dsttime = vvd->tz_dsttime;
257                 }
258                 return 0;
259         }
260         return vdso_fallback_gettimeofday(tv, tz);
261 }
262 int
263 gettimeofday(struct timeval *, struct timezone *)
264         __attribute__((weak, alias("__vdso_gettimeofday")));