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