1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * syscall_wrapper.h - x86 specific wrappers to syscall definitions
4  */
5 
6 #ifndef _ASM_X86_SYSCALL_WRAPPER_H
7 #define _ASM_X86_SYSCALL_WRAPPER_H
8 
9 #include <asm/ptrace.h>
10 
11 extern long __x64_sys_ni_syscall(const struct pt_regs *regs);
12 extern long __ia32_sys_ni_syscall(const struct pt_regs *regs);
13 
14 /*
15  * Instead of the generic __SYSCALL_DEFINEx() definition, the x86 version takes
16  * struct pt_regs *regs as the only argument of the syscall stub(s) named as:
17  * __x64_sys_*()         - 64-bit native syscall
18  * __ia32_sys_*()        - 32-bit native syscall or common compat syscall
19  * __ia32_compat_sys_*() - 32-bit compat syscall
20  * __x64_compat_sys_*()  - 64-bit X32 compat syscall
21  *
22  * The registers are decoded according to the ABI:
23  * 64-bit: RDI, RSI, RDX, R10, R8, R9
24  * 32-bit: EBX, ECX, EDX, ESI, EDI, EBP
25  *
26  * The stub then passes the decoded arguments to the __se_sys_*() wrapper to
27  * perform sign-extension (omitted for zero-argument syscalls).  Finally the
28  * arguments are passed to the __do_sys_*() function which is the actual
29  * syscall.  These wrappers are marked as inline so the compiler can optimize
30  * the functions where appropriate.
31  *
32  * Example assembly (slightly re-ordered for better readability):
33  *
34  * <__x64_sys_recv>:		<-- syscall with 4 parameters
35  *	callq	<__fentry__>
36  *
37  *	mov	0x70(%rdi),%rdi	<-- decode regs->di
38  *	mov	0x68(%rdi),%rsi	<-- decode regs->si
39  *	mov	0x60(%rdi),%rdx	<-- decode regs->dx
40  *	mov	0x38(%rdi),%rcx	<-- decode regs->r10
41  *
42  *	xor	%r9d,%r9d	<-- clear %r9
43  *	xor	%r8d,%r8d	<-- clear %r8
44  *
45  *	callq	__sys_recvfrom	<-- do the actual work in __sys_recvfrom()
46  *				    which takes 6 arguments
47  *
48  *	cltq			<-- extend return value to 64-bit
49  *	retq			<-- return
50  *
51  * This approach avoids leaking random user-provided register content down
52  * the call chain.
53  */
54 
55 /* Mapping of registers to parameters for syscalls on x86-64 and x32 */
56 #define SC_X86_64_REGS_TO_ARGS(x, ...)					\
57 	__MAP(x,__SC_ARGS						\
58 		,,regs->di,,regs->si,,regs->dx				\
59 		,,regs->r10,,regs->r8,,regs->r9)			\
60 
61 /* Mapping of registers to parameters for syscalls on i386 */
62 #define SC_IA32_REGS_TO_ARGS(x, ...)					\
63 	__MAP(x,__SC_ARGS						\
64 	      ,,(unsigned int)regs->bx,,(unsigned int)regs->cx		\
65 	      ,,(unsigned int)regs->dx,,(unsigned int)regs->si		\
66 	      ,,(unsigned int)regs->di,,(unsigned int)regs->bp)
67 
68 #define __SYS_STUB0(abi, name)						\
69 	long __##abi##_##name(const struct pt_regs *regs);		\
70 	ALLOW_ERROR_INJECTION(__##abi##_##name, ERRNO);			\
71 	long __##abi##_##name(const struct pt_regs *regs)		\
72 		__alias(__do_##name);
73 
74 #define __SYS_STUBx(abi, name, ...)					\
75 	long __##abi##_##name(const struct pt_regs *regs);		\
76 	ALLOW_ERROR_INJECTION(__##abi##_##name, ERRNO);			\
77 	long __##abi##_##name(const struct pt_regs *regs)		\
78 	{								\
79 		return __se_##name(__VA_ARGS__);			\
80 	}
81 
82 #define __COND_SYSCALL(abi, name)					\
83 	__weak long __##abi##_##name(const struct pt_regs *__unused);	\
84 	__weak long __##abi##_##name(const struct pt_regs *__unused)	\
85 	{								\
86 		return sys_ni_syscall();				\
87 	}
88 
89 #ifdef CONFIG_X86_64
90 #define __X64_SYS_STUB0(name)						\
91 	__SYS_STUB0(x64, sys_##name)
92 
93 #define __X64_SYS_STUBx(x, name, ...)					\
94 	__SYS_STUBx(x64, sys##name,					\
95 		    SC_X86_64_REGS_TO_ARGS(x, __VA_ARGS__))
96 
97 #define __X64_COND_SYSCALL(name)					\
98 	__COND_SYSCALL(x64, sys_##name)
99 
100 #else /* CONFIG_X86_64 */
101 #define __X64_SYS_STUB0(name)
102 #define __X64_SYS_STUBx(x, name, ...)
103 #define __X64_COND_SYSCALL(name)
104 #endif /* CONFIG_X86_64 */
105 
106 #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
107 #define __IA32_SYS_STUB0(name)						\
108 	__SYS_STUB0(ia32, sys_##name)
109 
110 #define __IA32_SYS_STUBx(x, name, ...)					\
111 	__SYS_STUBx(ia32, sys##name,					\
112 		    SC_IA32_REGS_TO_ARGS(x, __VA_ARGS__))
113 
114 #define __IA32_COND_SYSCALL(name)					\
115 	__COND_SYSCALL(ia32, sys_##name)
116 
117 #else /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
118 #define __IA32_SYS_STUB0(name)
119 #define __IA32_SYS_STUBx(x, name, ...)
120 #define __IA32_COND_SYSCALL(name)
121 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
122 
123 #ifdef CONFIG_IA32_EMULATION
124 /*
125  * For IA32 emulation, we need to handle "compat" syscalls *and* create
126  * additional wrappers (aptly named __ia32_sys_xyzzy) which decode the
127  * ia32 regs in the proper order for shared or "common" syscalls. As some
128  * syscalls may not be implemented, we need to expand COND_SYSCALL in
129  * kernel/sys_ni.c to cover this case as well.
130  */
131 #define __IA32_COMPAT_SYS_STUB0(name)					\
132 	__SYS_STUB0(ia32, compat_sys_##name)
133 
134 #define __IA32_COMPAT_SYS_STUBx(x, name, ...)				\
135 	__SYS_STUBx(ia32, compat_sys##name,				\
136 		    SC_IA32_REGS_TO_ARGS(x, __VA_ARGS__))
137 
138 #define __IA32_COMPAT_COND_SYSCALL(name)				\
139 	__COND_SYSCALL(ia32, compat_sys_##name)
140 
141 #else /* CONFIG_IA32_EMULATION */
142 #define __IA32_COMPAT_SYS_STUB0(name)
143 #define __IA32_COMPAT_SYS_STUBx(x, name, ...)
144 #define __IA32_COMPAT_COND_SYSCALL(name)
145 #endif /* CONFIG_IA32_EMULATION */
146 
147 
148 #ifdef CONFIG_X86_X32_ABI
149 /*
150  * For the x32 ABI, we need to create a stub for compat_sys_*() which is aware
151  * of the x86-64-style parameter ordering of x32 syscalls. The syscalls common
152  * with x86_64 obviously do not need such care.
153  */
154 #define __X32_COMPAT_SYS_STUB0(name)					\
155 	__SYS_STUB0(x64, compat_sys_##name)
156 
157 #define __X32_COMPAT_SYS_STUBx(x, name, ...)				\
158 	__SYS_STUBx(x64, compat_sys##name,				\
159 		    SC_X86_64_REGS_TO_ARGS(x, __VA_ARGS__))
160 
161 #define __X32_COMPAT_COND_SYSCALL(name)					\
162 	__COND_SYSCALL(x64, compat_sys_##name)
163 
164 #else /* CONFIG_X86_X32_ABI */
165 #define __X32_COMPAT_SYS_STUB0(name)
166 #define __X32_COMPAT_SYS_STUBx(x, name, ...)
167 #define __X32_COMPAT_COND_SYSCALL(name)
168 #endif /* CONFIG_X86_X32_ABI */
169 
170 
171 #ifdef CONFIG_COMPAT
172 /*
173  * Compat means IA32_EMULATION and/or X86_X32. As they use a different
174  * mapping of registers to parameters, we need to generate stubs for each
175  * of them.
176  */
177 #define COMPAT_SYSCALL_DEFINE0(name)					\
178 	static long							\
179 	__do_compat_sys_##name(const struct pt_regs *__unused);		\
180 	__IA32_COMPAT_SYS_STUB0(name)					\
181 	__X32_COMPAT_SYS_STUB0(name)					\
182 	static long							\
183 	__do_compat_sys_##name(const struct pt_regs *__unused)
184 
185 #define COMPAT_SYSCALL_DEFINEx(x, name, ...)					\
186 	static long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__));	\
187 	static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
188 	__IA32_COMPAT_SYS_STUBx(x, name, __VA_ARGS__)				\
189 	__X32_COMPAT_SYS_STUBx(x, name, __VA_ARGS__)				\
190 	static long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__))	\
191 	{									\
192 		return __do_compat_sys##name(__MAP(x,__SC_DELOUSE,__VA_ARGS__));\
193 	}									\
194 	static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
195 
196 /*
197  * As some compat syscalls may not be implemented, we need to expand
198  * COND_SYSCALL_COMPAT in kernel/sys_ni.c to cover this case as well.
199  */
200 #define COND_SYSCALL_COMPAT(name) 					\
201 	__IA32_COMPAT_COND_SYSCALL(name)				\
202 	__X32_COMPAT_COND_SYSCALL(name)
203 
204 #endif /* CONFIG_COMPAT */
205 
206 #define __SYSCALL_DEFINEx(x, name, ...)					\
207 	static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__));	\
208 	static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
209 	__X64_SYS_STUBx(x, name, __VA_ARGS__)				\
210 	__IA32_SYS_STUBx(x, name, __VA_ARGS__)				\
211 	static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__))	\
212 	{								\
213 		long ret = __do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__));\
214 		__MAP(x,__SC_TEST,__VA_ARGS__);				\
215 		__PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__));	\
216 		return ret;						\
217 	}								\
218 	static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
219 
220 /*
221  * As the generic SYSCALL_DEFINE0() macro does not decode any parameters for
222  * obvious reasons, and passing struct pt_regs *regs to it in %rdi does not
223  * hurt, we only need to re-define it here to keep the naming congruent to
224  * SYSCALL_DEFINEx() -- which is essential for the COND_SYSCALL() macro
225  * to work correctly.
226  */
227 #define SYSCALL_DEFINE0(sname)						\
228 	SYSCALL_METADATA(_##sname, 0);					\
229 	static long __do_sys_##sname(const struct pt_regs *__unused);	\
230 	__X64_SYS_STUB0(sname)						\
231 	__IA32_SYS_STUB0(sname)						\
232 	static long __do_sys_##sname(const struct pt_regs *__unused)
233 
234 #define COND_SYSCALL(name)						\
235 	__X64_COND_SYSCALL(name)					\
236 	__IA32_COND_SYSCALL(name)
237 
238 
239 /*
240  * For VSYSCALLS, we need to declare these three syscalls with the new
241  * pt_regs-based calling convention for in-kernel use.
242  */
243 long __x64_sys_getcpu(const struct pt_regs *regs);
244 long __x64_sys_gettimeofday(const struct pt_regs *regs);
245 long __x64_sys_time(const struct pt_regs *regs);
246 
247 #endif /* _ASM_X86_SYSCALL_WRAPPER_H */
248