xref: /freebsd/sys/arm64/linux/linux_vdso_gtod.c (revision 214e3e09b3381e44bf5d9c1dcd19c4b1b923a796)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
5  * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/elf.h>
31 #include <sys/errno.h>
32 #include <sys/proc.h>
33 #include <sys/stddef.h>
34 #define	_KERNEL
35 #include <sys/vdso.h>
36 #undef	_KERNEL
37 #include <stdbool.h>
38 
39 #include <machine/atomic.h>
40 #include <machine/stdarg.h>
41 
42 #include <arm64/linux/linux.h>
43 #include <arm64/linux/linux_syscall.h>
44 #include <compat/linux/linux_errno.h>
45 #include <compat/linux/linux_time.h>
46 
47 /* The kernel fixup this at vDSO install */
48 uintptr_t *kern_timekeep_base = NULL;
49 uint32_t kern_tsc_selector = 0;
50 
51 static int
52 write(int lfd, const void *lbuf, size_t lsize)
53 {
54 	register long svc asm("x8") = LINUX_SYS_write;
55 	register int fd asm("x0") = lfd;
56 	register const char *buf asm("x1") = lbuf;
57 	register long size asm("x2") = lsize;
58 	register long res asm ("x0");
59 
60 	asm volatile(
61 	"       svc #0\n"
62 	: "=r" (res)
63 	: "r" (fd), "r" (buf), "r" (size), "r" (svc)
64 	: "memory");
65 	return (res);
66 }
67 
68 static int
69 __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *lts)
70 {
71 	register long svc asm("x8") = LINUX_SYS_linux_clock_gettime;
72 	register clockid_t clockid asm("x0") = clock_id;
73 	register struct l_timespec *ts asm("x1") = lts;
74 	register long res asm ("x0");
75 
76 	asm volatile(
77 	"       svc #0\n"
78 	: "=r" (res)
79 	: "r" (clockid), "r" (ts), "r" (svc)
80 	: "memory");
81 	return (res);
82 }
83 
84 static int
85 __vdso_gettimeofday_fallback(l_timeval *ltv, struct timezone *ltz)
86 {
87 	register long svc asm("x8") = LINUX_SYS_gettimeofday;
88 	register l_timeval *tv asm("x0") = ltv;
89 	register struct timezone *tz asm("x1") = ltz;
90 	register long res asm ("x0");
91 
92 	asm volatile(
93 	"       svc #0\n"
94 	: "=r" (res)
95 	: "r" (tv), "r" (tz), "r" (svc)
96 	: "memory");
97 	return (res);
98 }
99 
100 static int
101 __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *lts)
102 {
103 	register long svc asm("x8") = LINUX_SYS_linux_clock_getres;
104 	register clockid_t clockid asm("x0") = clock_id;
105 	register struct l_timespec *ts asm("x1") = lts;
106 	register long res asm ("x0");
107 
108 	asm volatile(
109 	"       svc #0\n"
110 	: "=r" (res)
111 	: "r" (clockid), "r" (ts), "r" (svc)
112 	: "memory");
113 	return (res);
114 }
115 
116 /*
117  * copied from lib/libc/aarch64/sys/__vdso_gettc.c
118  */
119 
120 static inline uint64_t
121 cp15_cntvct_get(void)
122 {
123 	uint64_t reg;
124 
125 	__asm __volatile("mrs %0, cntvct_el0" : "=r" (reg));
126 	return (reg);
127 }
128 
129 static inline uint64_t
130 cp15_cntpct_get(void)
131 {
132 	uint64_t reg;
133 
134 	__asm __volatile("mrs %0, cntpct_el0" : "=r" (reg));
135 	return (reg);
136 }
137 
138 int
139 __vdso_gettc(const struct vdso_timehands *th, u_int *tc)
140 {
141 
142 	if (th->th_algo != VDSO_TH_ALGO_ARM_GENTIM)
143 		return (ENOSYS);
144 	__asm __volatile("isb" : : : "memory");
145 	*tc = th->th_physical == 0 ? cp15_cntvct_get() : cp15_cntpct_get();
146 	return (0);
147 }
148 
149 #include <compat/linux/linux_vdso_gtod.inc>
150