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