19931033bSDmitry Chagin /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
39931033bSDmitry Chagin *
49931033bSDmitry Chagin * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
59931033bSDmitry Chagin * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org>
69931033bSDmitry Chagin *
79931033bSDmitry Chagin * Redistribution and use in source and binary forms, with or without
89931033bSDmitry Chagin * modification, are permitted provided that the following conditions
99931033bSDmitry Chagin * are met:
109931033bSDmitry Chagin * 1. Redistributions of source code must retain the above copyright
119931033bSDmitry Chagin * notice, this list of conditions and the following disclaimer.
129931033bSDmitry Chagin * 2. Redistributions in binary form must reproduce the above copyright
139931033bSDmitry Chagin * notice, this list of conditions and the following disclaimer in the
149931033bSDmitry Chagin * documentation and/or other materials provided with the distribution.
159931033bSDmitry Chagin *
169931033bSDmitry Chagin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179931033bSDmitry Chagin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189931033bSDmitry Chagin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199931033bSDmitry Chagin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209931033bSDmitry Chagin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219931033bSDmitry Chagin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229931033bSDmitry Chagin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239931033bSDmitry Chagin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249931033bSDmitry Chagin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259931033bSDmitry Chagin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269931033bSDmitry Chagin * SUCH DAMAGE.
279931033bSDmitry Chagin */
289931033bSDmitry Chagin
299931033bSDmitry Chagin #include <sys/elf.h>
309931033bSDmitry Chagin #include <sys/errno.h>
319931033bSDmitry Chagin #include <sys/proc.h>
329931033bSDmitry Chagin #include <sys/stddef.h>
339931033bSDmitry Chagin #define _KERNEL
349931033bSDmitry Chagin #include <sys/vdso.h>
359931033bSDmitry Chagin #undef _KERNEL
369931033bSDmitry Chagin #include <stdbool.h>
379931033bSDmitry Chagin
389931033bSDmitry Chagin #include <machine/atomic.h>
399931033bSDmitry Chagin #include <machine/stdarg.h>
409931033bSDmitry Chagin
419931033bSDmitry Chagin #include <arm64/linux/linux.h>
429931033bSDmitry Chagin #include <arm64/linux/linux_syscall.h>
439931033bSDmitry Chagin #include <compat/linux/linux_errno.h>
44c8a79231SDmitry Chagin #include <compat/linux/linux_time.h>
459931033bSDmitry Chagin
469931033bSDmitry Chagin /* The kernel fixup this at vDSO install */
479931033bSDmitry Chagin uintptr_t *kern_timekeep_base = NULL;
489931033bSDmitry Chagin uint32_t kern_tsc_selector = 0;
499931033bSDmitry Chagin
509931033bSDmitry Chagin static int
write(int lfd,const void * lbuf,size_t lsize)519931033bSDmitry Chagin write(int lfd, const void *lbuf, size_t lsize)
529931033bSDmitry Chagin {
53*e58ff664SDmitry Chagin register long svc asm("x8") = LINUX_SYS_linux_write;
549931033bSDmitry Chagin register int fd asm("x0") = lfd;
559931033bSDmitry Chagin register const char *buf asm("x1") = lbuf;
569931033bSDmitry Chagin register long size asm("x2") = lsize;
579931033bSDmitry Chagin register long res asm ("x0");
589931033bSDmitry Chagin
599931033bSDmitry Chagin asm volatile(
609931033bSDmitry Chagin " svc #0\n"
619931033bSDmitry Chagin : "=r" (res)
629931033bSDmitry Chagin : "r" (fd), "r" (buf), "r" (size), "r" (svc)
639931033bSDmitry Chagin : "memory");
649931033bSDmitry Chagin return (res);
659931033bSDmitry Chagin }
669931033bSDmitry Chagin
679931033bSDmitry Chagin static int
__vdso_clock_gettime_fallback(clockid_t clock_id,struct l_timespec * lts)689931033bSDmitry Chagin __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *lts)
699931033bSDmitry Chagin {
709931033bSDmitry Chagin register long svc asm("x8") = LINUX_SYS_linux_clock_gettime;
719931033bSDmitry Chagin register clockid_t clockid asm("x0") = clock_id;
729931033bSDmitry Chagin register struct l_timespec *ts asm("x1") = lts;
739931033bSDmitry Chagin register long res asm ("x0");
749931033bSDmitry Chagin
759931033bSDmitry Chagin asm volatile(
769931033bSDmitry Chagin " svc #0\n"
779931033bSDmitry Chagin : "=r" (res)
789931033bSDmitry Chagin : "r" (clockid), "r" (ts), "r" (svc)
799931033bSDmitry Chagin : "memory");
809931033bSDmitry Chagin return (res);
819931033bSDmitry Chagin }
829931033bSDmitry Chagin
839931033bSDmitry Chagin static int
__vdso_gettimeofday_fallback(l_timeval * ltv,struct timezone * ltz)849931033bSDmitry Chagin __vdso_gettimeofday_fallback(l_timeval *ltv, struct timezone *ltz)
859931033bSDmitry Chagin {
869931033bSDmitry Chagin register long svc asm("x8") = LINUX_SYS_gettimeofday;
879931033bSDmitry Chagin register l_timeval *tv asm("x0") = ltv;
889931033bSDmitry Chagin register struct timezone *tz asm("x1") = ltz;
899931033bSDmitry Chagin register long res asm ("x0");
909931033bSDmitry Chagin
919931033bSDmitry Chagin asm volatile(
929931033bSDmitry Chagin " svc #0\n"
939931033bSDmitry Chagin : "=r" (res)
949931033bSDmitry Chagin : "r" (tv), "r" (tz), "r" (svc)
959931033bSDmitry Chagin : "memory");
969931033bSDmitry Chagin return (res);
979931033bSDmitry Chagin }
989931033bSDmitry Chagin
999931033bSDmitry Chagin static int
__vdso_clock_getres_fallback(clockid_t clock_id,struct l_timespec * lts)1009931033bSDmitry Chagin __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *lts)
1019931033bSDmitry Chagin {
1029931033bSDmitry Chagin register long svc asm("x8") = LINUX_SYS_linux_clock_getres;
1039931033bSDmitry Chagin register clockid_t clockid asm("x0") = clock_id;
1049931033bSDmitry Chagin register struct l_timespec *ts asm("x1") = lts;
1059931033bSDmitry Chagin register long res asm ("x0");
1069931033bSDmitry Chagin
1079931033bSDmitry Chagin asm volatile(
1089931033bSDmitry Chagin " svc #0\n"
1099931033bSDmitry Chagin : "=r" (res)
1109931033bSDmitry Chagin : "r" (clockid), "r" (ts), "r" (svc)
1119931033bSDmitry Chagin : "memory");
1129931033bSDmitry Chagin return (res);
1139931033bSDmitry Chagin }
1149931033bSDmitry Chagin
1159931033bSDmitry Chagin /*
1169931033bSDmitry Chagin * copied from lib/libc/aarch64/sys/__vdso_gettc.c
1179931033bSDmitry Chagin */
1189931033bSDmitry Chagin
1199931033bSDmitry Chagin static inline uint64_t
cp15_cntvct_get(void)1209931033bSDmitry Chagin cp15_cntvct_get(void)
1219931033bSDmitry Chagin {
1229931033bSDmitry Chagin uint64_t reg;
1239931033bSDmitry Chagin
1249931033bSDmitry Chagin __asm __volatile("mrs %0, cntvct_el0" : "=r" (reg));
1259931033bSDmitry Chagin return (reg);
1269931033bSDmitry Chagin }
1279931033bSDmitry Chagin
1289931033bSDmitry Chagin static inline uint64_t
cp15_cntpct_get(void)1299931033bSDmitry Chagin cp15_cntpct_get(void)
1309931033bSDmitry Chagin {
1319931033bSDmitry Chagin uint64_t reg;
1329931033bSDmitry Chagin
1339931033bSDmitry Chagin __asm __volatile("mrs %0, cntpct_el0" : "=r" (reg));
1349931033bSDmitry Chagin return (reg);
1359931033bSDmitry Chagin }
1369931033bSDmitry Chagin
1379931033bSDmitry Chagin int
__vdso_gettc(const struct vdso_timehands * th,u_int * tc)1389931033bSDmitry Chagin __vdso_gettc(const struct vdso_timehands *th, u_int *tc)
1399931033bSDmitry Chagin {
1409931033bSDmitry Chagin
1419931033bSDmitry Chagin if (th->th_algo != VDSO_TH_ALGO_ARM_GENTIM)
1429931033bSDmitry Chagin return (ENOSYS);
1439931033bSDmitry Chagin __asm __volatile("isb" : : : "memory");
1449931033bSDmitry Chagin *tc = th->th_physical == 0 ? cp15_cntvct_get() : cp15_cntpct_get();
1459931033bSDmitry Chagin return (0);
1469931033bSDmitry Chagin }
1479931033bSDmitry Chagin
1489931033bSDmitry Chagin #include <compat/linux/linux_vdso_gtod.inc>
149