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