xref: /freebsd/sys/arm64/linux/linux_vdso_gtod.c (revision 3f9d360c82e0724bfb61346038236bf15c5d4d84)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/elf.h>
34 #include <sys/errno.h>
35 #include <sys/proc.h>
36 #include <sys/stddef.h>
37 #define	_KERNEL
38 #include <sys/vdso.h>
39 #undef	_KERNEL
40 #include <stdbool.h>
41 #include <strings.h>
42 
43 #include <machine/atomic.h>
44 #include <machine/stdarg.h>
45 
46 #include <arm64/linux/linux.h>
47 #include <arm64/linux/linux_syscall.h>
48 #include <compat/linux/linux_errno.h>
49 #include <compat/linux/linux_timer.h>
50 
51 /* The kernel fixup this at vDSO install */
52 uintptr_t *kern_timekeep_base = NULL;
53 uint32_t kern_tsc_selector = 0;
54 
55 static int
56 write(int lfd, const void *lbuf, size_t lsize)
57 {
58 	register long svc asm("x8") = LINUX_SYS_write;
59 	register int fd asm("x0") = lfd;
60 	register const char *buf asm("x1") = lbuf;
61 	register long size asm("x2") = lsize;
62 	register long res asm ("x0");
63 
64 	asm volatile(
65 	"       svc #0\n"
66 	: "=r" (res)
67 	: "r" (fd), "r" (buf), "r" (size), "r" (svc)
68 	: "memory");
69 	return (res);
70 }
71 
72 static int
73 __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *lts)
74 {
75 	register long svc asm("x8") = LINUX_SYS_linux_clock_gettime;
76 	register clockid_t clockid asm("x0") = clock_id;
77 	register struct l_timespec *ts asm("x1") = lts;
78 	register long res asm ("x0");
79 
80 	asm volatile(
81 	"       svc #0\n"
82 	: "=r" (res)
83 	: "r" (clockid), "r" (ts), "r" (svc)
84 	: "memory");
85 	return (res);
86 }
87 
88 static int
89 __vdso_gettimeofday_fallback(l_timeval *ltv, struct timezone *ltz)
90 {
91 	register long svc asm("x8") = LINUX_SYS_gettimeofday;
92 	register l_timeval *tv asm("x0") = ltv;
93 	register struct timezone *tz asm("x1") = ltz;
94 	register long res asm ("x0");
95 
96 	asm volatile(
97 	"       svc #0\n"
98 	: "=r" (res)
99 	: "r" (tv), "r" (tz), "r" (svc)
100 	: "memory");
101 	return (res);
102 }
103 
104 static int
105 __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *lts)
106 {
107 	register long svc asm("x8") = LINUX_SYS_linux_clock_getres;
108 	register clockid_t clockid asm("x0") = clock_id;
109 	register struct l_timespec *ts asm("x1") = lts;
110 	register long res asm ("x0");
111 
112 	asm volatile(
113 	"       svc #0\n"
114 	: "=r" (res)
115 	: "r" (clockid), "r" (ts), "r" (svc)
116 	: "memory");
117 	return (res);
118 }
119 
120 /*
121  * copied from lib/libc/aarch64/sys/__vdso_gettc.c
122  */
123 
124 static inline uint64_t
125 cp15_cntvct_get(void)
126 {
127 	uint64_t reg;
128 
129 	__asm __volatile("mrs %0, cntvct_el0" : "=r" (reg));
130 	return (reg);
131 }
132 
133 static inline uint64_t
134 cp15_cntpct_get(void)
135 {
136 	uint64_t reg;
137 
138 	__asm __volatile("mrs %0, cntpct_el0" : "=r" (reg));
139 	return (reg);
140 }
141 
142 int
143 __vdso_gettc(const struct vdso_timehands *th, u_int *tc)
144 {
145 
146 	if (th->th_algo != VDSO_TH_ALGO_ARM_GENTIM)
147 		return (ENOSYS);
148 	__asm __volatile("isb" : : : "memory");
149 	*tc = th->th_physical == 0 ? cp15_cntvct_get() : cp15_cntpct_get();
150 	return (0);
151 }
152 
153 #include <compat/linux/linux_vdso_gtod.inc>
154