xref: /freebsd/sys/amd64/linux32/linux32_vdso_gtod.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/elf.h>
29 #include <sys/errno.h>
30 #include <sys/proc.h>
31 #include <sys/stddef.h>
32 #define	_KERNEL
33 #include <sys/vdso.h>
34 #undef	_KERNEL
35 #include <stdbool.h>
36 
37 #include <i386/include/atomic.h>
38 #include <i386/include/cpufunc.h>
39 #include <i386/include/stdarg.h>
40 
41 #include <amd64/linux32/linux.h>
42 #include <amd64/linux32/linux32_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 uint32_t kern_cpu_selector = 0;
50 
51 #include <x86/linux/linux_vdso_gettc_x86.inc>
52 #include <x86/linux/linux_vdso_getcpu_x86.inc>
53 
54 static int
55 write(int fd, const void *buf, size_t size)
56 {
57 	int res;
58 
59 	__asm__ __volatile__
60 	(
61 	    "int $0x80"
62 	    : "=a"(res)
63 	    : "a"(LINUX32_SYS_linux_write), "b"(fd), "c"(buf), "d"(size)
64 	    : "cc", "memory"
65 	);
66 	return (res);
67 }
68 
69 static int
70 __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts)
71 {
72 	int res;
73 
74 	__asm__ __volatile__
75 	(
76 	    "int $0x80"
77 	    : "=a"(res)
78 	    : "a"(LINUX32_SYS_linux_clock_gettime), "b"(clock_id), "c"(ts)
79 	    : "cc", "memory"
80 	);
81 	return (res);
82 }
83 
84 static int
85 __vdso_clock_gettime64_fallback(clockid_t clock_id, struct l_timespec64 *ts)
86 {
87 	int res;
88 
89 	__asm__ __volatile__
90 	(
91 	    "int $0x80"
92 	    : "=a"(res)
93 	    : "a"(LINUX32_SYS_linux_clock_gettime64), "b"(clock_id), "c"(ts)
94 	    : "cc", "memory"
95 	);
96 	return (res);
97 }
98 
99 static int
100 __vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz)
101 {
102 	int res;
103 
104 	__asm__ __volatile__
105 	(
106 	    "int $0x80"
107 	    : "=a"(res)
108 	    : "a"(LINUX32_SYS_linux_gettimeofday), "b"(tv), "c"(tz)
109 	    : "cc", "memory"
110 	);
111 	return (res);
112 }
113 
114 static int
115 __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts)
116 {
117 	int res;
118 
119 	__asm__ __volatile__
120 	(
121 	    "int $0x80"
122 	    : "=a"(res)
123 	    : "a"(LINUX32_SYS_linux_clock_getres), "b"(clock_id), "c"(ts)
124 	    : "cc", "memory"
125 	);
126 	return (res);
127 }
128 
129 static int
130 __vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache)
131 {
132 	int res;
133 
134 	__asm__ __volatile__
135 	(
136 	    "int $0x80"
137 	    : "=a"(res)
138 	    : "a"(LINUX32_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache)
139 	    : "cc", "memory"
140 	);
141 	return (res);
142 }
143 
144 static int
145 __vdso_time_fallback(long *tm)
146 {
147 	int res;
148 
149 	__asm__ __volatile__
150 	(
151 	    "int $0x80"
152 	    : "=a"(res)
153 	    : "a"(LINUX32_SYS_linux_time), "b"(tm)
154 	    : "cc", "memory"
155 	);
156 	return (res);
157 }
158 
159 #include <compat/linux/linux_vdso_gtod.inc>
160