8cf13af1323cde6d4f6e31c4dbf53dfe28e8aee8
2 * Userspace implementations of gettimeofday() and friends.
4 * Copyright (C) 2017 Cavium, Inc.
5 * Copyright (C) 2015 Mentor Graphics Corporation
6 * Copyright (C) 2012 ARM Limited
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Author: Will Deacon <will.deacon@arm.com>
21 * Rewriten from arch64 version into C by: Andrew Pinski <apinski@cavium.com>
22 * Reworked and rebased over arm version by: Mark Salyzyn <salyzyn@android.com>
25 #include <linux/compiler.h>
26 #include <linux/hrtimer.h>
27 #include <linux/time.h>
28 #include <asm/arch_timer.h>
29 #include <asm/barrier.h>
32 #include <asm/unistd.h>
33 #include <asm/vdso_datapage.h>
36 #error This code depends on AEABI system call conventions
39 extern struct vdso_data
*__get_datapage(void);
41 static notrace u32
vdso_read_begin(const struct vdso_data
*vdata
)
46 seq
= READ_ONCE(vdata
->tb_seq_count
);
54 smp_rmb(); /* Pairs with second smp_wmb in update_vsyscall */
58 static notrace
int vdso_read_retry(const struct vdso_data
*vdata
, u32 start
)
62 smp_rmb(); /* Pairs with first smp_wmb in update_vsyscall */
63 seq
= READ_ONCE(vdata
->tb_seq_count
);
67 static notrace
long clock_gettime_fallback(clockid_t _clkid
,
70 register struct timespec
*ts
asm("r1") = _ts
;
71 register clockid_t clkid
asm("r0") = _clkid
;
72 register long ret
asm ("r0");
73 register long nr
asm("r7") = __NR_clock_gettime
;
78 : "r" (clkid
), "r" (ts
), "r" (nr
)
84 static notrace
int do_realtime_coarse(struct timespec
*ts
,
85 struct vdso_data
*vdata
)
90 seq
= vdso_read_begin(vdata
);
92 ts
->tv_sec
= vdata
->xtime_coarse_sec
;
93 ts
->tv_nsec
= vdata
->xtime_coarse_nsec
;
95 } while (vdso_read_retry(vdata
, seq
));
100 static notrace
int do_monotonic_coarse(struct timespec
*ts
,
101 struct vdso_data
*vdata
)
103 struct timespec tomono
;
107 seq
= vdso_read_begin(vdata
);
109 ts
->tv_sec
= vdata
->xtime_coarse_sec
;
110 ts
->tv_nsec
= vdata
->xtime_coarse_nsec
;
112 tomono
.tv_sec
= vdata
->wtm_clock_sec
;
113 tomono
.tv_nsec
= vdata
->wtm_clock_nsec
;
115 } while (vdso_read_retry(vdata
, seq
));
117 ts
->tv_sec
+= tomono
.tv_sec
;
118 timespec_add_ns(ts
, tomono
.tv_nsec
);
123 #ifdef CONFIG_ARM_ARCH_TIMER
125 static notrace u64
get_ns(struct vdso_data
*vdata
)
131 cycle_now
= arch_counter_get_cntvct();
133 cycle_delta
= (cycle_now
- vdata
->cs_cycle_last
) & vdata
->cs_mask
;
135 nsec
= (cycle_delta
* vdata
->cs_mono_mult
) + vdata
->xtime_clock_snsec
;
136 nsec
>>= vdata
->cs_shift
;
141 static notrace
int do_realtime(struct timespec
*ts
, struct vdso_data
*vdata
)
147 seq
= vdso_read_begin(vdata
);
149 if (vdata
->use_syscall
)
152 ts
->tv_sec
= vdata
->xtime_clock_sec
;
153 nsecs
= get_ns(vdata
);
155 } while (vdso_read_retry(vdata
, seq
));
158 timespec_add_ns(ts
, nsecs
);
163 static notrace
int do_monotonic(struct timespec
*ts
, struct vdso_data
*vdata
)
165 struct timespec tomono
;
170 seq
= vdso_read_begin(vdata
);
172 if (vdata
->use_syscall
)
175 ts
->tv_sec
= vdata
->xtime_clock_sec
;
176 nsecs
= get_ns(vdata
);
178 tomono
.tv_sec
= vdata
->wtm_clock_sec
;
179 tomono
.tv_nsec
= vdata
->wtm_clock_nsec
;
181 } while (vdso_read_retry(vdata
, seq
));
183 ts
->tv_sec
+= tomono
.tv_sec
;
185 timespec_add_ns(ts
, nsecs
+ tomono
.tv_nsec
);
190 #else /* CONFIG_ARM_ARCH_TIMER */
192 static notrace
int do_realtime(struct timespec
*ts
, struct vdso_data
*vdata
)
197 static notrace
int do_monotonic(struct timespec
*ts
, struct vdso_data
*vdata
)
202 #endif /* CONFIG_ARM_ARCH_TIMER */
204 notrace
int __vdso_clock_gettime(clockid_t clkid
, struct timespec
*ts
)
206 struct vdso_data
*vdata
;
209 vdata
= __get_datapage();
212 case CLOCK_REALTIME_COARSE
:
213 ret
= do_realtime_coarse(ts
, vdata
);
215 case CLOCK_MONOTONIC_COARSE
:
216 ret
= do_monotonic_coarse(ts
, vdata
);
219 ret
= do_realtime(ts
, vdata
);
221 case CLOCK_MONOTONIC
:
222 ret
= do_monotonic(ts
, vdata
);
229 ret
= clock_gettime_fallback(clkid
, ts
);
234 static notrace
long gettimeofday_fallback(struct timeval
*_tv
,
235 struct timezone
*_tz
)
237 register struct timezone
*tz
asm("r1") = _tz
;
238 register struct timeval
*tv
asm("r0") = _tv
;
239 register long ret
asm ("r0");
240 register long nr
asm("r7") = __NR_gettimeofday
;
245 : "r" (tv
), "r" (tz
), "r" (nr
)
251 notrace
int __vdso_gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
254 struct vdso_data
*vdata
;
257 vdata
= __get_datapage();
259 ret
= do_realtime(&ts
, vdata
);
261 return gettimeofday_fallback(tv
, tz
);
264 tv
->tv_sec
= ts
.tv_sec
;
265 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
268 tz
->tz_minuteswest
= vdata
->tz_minuteswest
;
269 tz
->tz_dsttime
= vdata
->tz_dsttime
;
275 /* Avoid unresolved references emitted by GCC */
277 void __aeabi_unwind_cpp_pr0(void)
281 void __aeabi_unwind_cpp_pr1(void)
285 void __aeabi_unwind_cpp_pr2(void)