xref: /freebsd/sys/i386/linux/linux_vdso_gtod.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/stdarg.h>
42 
43 #include <i386/linux/linux.h>
44 #include <i386/linux/linux_syscall.h>
45 #include <compat/linux/linux_errno.h>
46 #include <compat/linux/linux_timer.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 #include <x86/linux/linux_vdso_gettc_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"(LINUX_SYS_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"(LINUX_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"(LINUX_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"(LINUX_SYS_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"(LINUX_SYS_linux_clock_getres), "b"(clock_id), "c"(ts)
124 	    : "cc", "memory"
125 	);
126 	return (res);
127 }
128 
129 static int
130 __vdso_time_fallback(long *tm)
131 {
132 	int res;
133 
134 	__asm__ __volatile__
135 	(
136 	    "int $0x80"
137 	    : "=a"(res)
138 	    : "a"(LINUX_SYS_linux_time), "b"(tm)
139 	    : "cc", "memory"
140 	);
141 	return (res);
142 }
143 
144 #include <compat/linux/linux_vdso_gtod.inc>
145