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