xref: /freebsd/sys/i386/linux/linux_vdso_gtod.c (revision c1839039b193b48c8eb7520c75487f0bd4340c3b)
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/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
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/cpufunc.h>
42 #include <machine/stdarg.h>
43 
44 #include <i386/linux/linux.h>
45 #include <i386/linux/linux_syscall.h>
46 #include <compat/linux/linux_errno.h>
47 #include <compat/linux/linux_time.h>
48 
49 /* The kernel fixup this at vDSO install */
50 uintptr_t *kern_timekeep_base = NULL;
51 uint32_t kern_tsc_selector = 0;
52 uint32_t kern_cpu_selector = 0;
53 
54 #include <x86/linux/linux_vdso_gettc_x86.inc>
55 #include <x86/linux/linux_vdso_getcpu_x86.inc>
56 
57 static int
58 write(int fd, const void *buf, size_t size)
59 {
60 	int res;
61 
62 	__asm__ __volatile__
63 	(
64 	    "int $0x80"
65 	    : "=a"(res)
66 	    : "a"(LINUX_SYS_write), "b"(fd), "c"(buf), "d"(size)
67 	    : "cc", "memory"
68 	);
69 	return (res);
70 }
71 
72 static int
73 __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts)
74 {
75 	int res;
76 
77 	__asm__ __volatile__
78 	(
79 	    "int $0x80"
80 	    : "=a"(res)
81 	    : "a"(LINUX_SYS_linux_clock_gettime), "b"(clock_id), "c"(ts)
82 	    : "cc", "memory"
83 	);
84 	return (res);
85 }
86 
87 static int
88 __vdso_clock_gettime64_fallback(clockid_t clock_id, struct l_timespec64 *ts)
89 {
90 	int res;
91 
92 	__asm__ __volatile__
93 	(
94 	    "int $0x80"
95 	    : "=a"(res)
96 	    : "a"(LINUX_SYS_linux_clock_gettime64), "b"(clock_id), "c"(ts)
97 	    : "cc", "memory"
98 	);
99 	return (res);
100 }
101 
102 static int
103 __vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz)
104 {
105 	int res;
106 
107 	__asm__ __volatile__
108 	(
109 	    "int $0x80"
110 	    : "=a"(res)
111 	    : "a"(LINUX_SYS_gettimeofday), "b"(tv), "c"(tz)
112 	    : "cc", "memory"
113 	);
114 	return (res);
115 }
116 
117 static int
118 __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts)
119 {
120 	int res;
121 
122 	__asm__ __volatile__
123 	(
124 	    "int $0x80"
125 	    : "=a"(res)
126 	    : "a"(LINUX_SYS_linux_clock_getres), "b"(clock_id), "c"(ts)
127 	    : "cc", "memory"
128 	);
129 	return (res);
130 }
131 
132 static int
133 __vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache)
134 {
135 	int res;
136 
137 	__asm__ __volatile__
138 	(
139 	    "int $0x80"
140 	    : "=a"(res)
141 	    : "a"(LINUX_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache)
142 	    : "cc", "memory"
143 	);
144 	return (res);
145 }
146 
147 static int
148 __vdso_time_fallback(long *tm)
149 {
150 	int res;
151 
152 	__asm__ __volatile__
153 	(
154 	    "int $0x80"
155 	    : "=a"(res)
156 	    : "a"(LINUX_SYS_linux_time), "b"(tm)
157 	    : "cc", "memory"
158 	);
159 	return (res);
160 }
161 
162 #include <compat/linux/linux_vdso_gtod.inc>
163