1 /* SPDX-License-Identifier: GPL-2.0 */
3 * syscall_wrapper.h - x86 specific wrappers to syscall definitions
6 #ifndef _ASM_X86_SYSCALL_WRAPPER_H
7 #define _ASM_X86_SYSCALL_WRAPPER_H
9 /* Mapping of registers to parameters for syscalls on x86-64 and x32 */
10 #define SC_X86_64_REGS_TO_ARGS(x, ...) \
12 ,,regs->di,,regs->si,,regs->dx \
13 ,,regs->r10,,regs->r8,,regs->r9) \
15 /* Mapping of registers to parameters for syscalls on i386 */
16 #define SC_IA32_REGS_TO_ARGS(x, ...) \
18 ,,(unsigned int)regs->bx,,(unsigned int)regs->cx \
19 ,,(unsigned int)regs->dx,,(unsigned int)regs->si \
20 ,,(unsigned int)regs->di,,(unsigned int)regs->bp)
22 #ifdef CONFIG_IA32_EMULATION
24 * For IA32 emulation, we need to handle "compat" syscalls *and* create
25 * additional wrappers (aptly named __ia32_sys_xyzzy) which decode the
26 * ia32 regs in the proper order for shared or "common" syscalls. As some
27 * syscalls may not be implemented, we need to expand COND_SYSCALL in
28 * kernel/sys_ni.c and SYS_NI in kernel/time/posix-stubs.c to cover this
31 #define __IA32_COMPAT_SYS_STUBx(x, name, ...) \
32 asmlinkage long __ia32_compat_sys##name(const struct pt_regs *regs);\
33 ALLOW_ERROR_INJECTION(__ia32_compat_sys##name, ERRNO); \
34 asmlinkage long __ia32_compat_sys##name(const struct pt_regs *regs)\
36 return __se_compat_sys##name(SC_IA32_REGS_TO_ARGS(x,__VA_ARGS__));\
39 #define __IA32_SYS_STUBx(x, name, ...) \
40 asmlinkage long __ia32_sys##name(const struct pt_regs *regs); \
41 ALLOW_ERROR_INJECTION(__ia32_sys##name, ERRNO); \
42 asmlinkage long __ia32_sys##name(const struct pt_regs *regs) \
44 return __se_sys##name(SC_IA32_REGS_TO_ARGS(x,__VA_ARGS__));\
48 * To keep the naming coherent, re-define SYSCALL_DEFINE0 to create an alias
49 * named __ia32_sys_*()
52 #define SYSCALL_DEFINE0(sname) \
53 SYSCALL_METADATA(_##sname, 0); \
54 asmlinkage long __x64_sys_##sname(const struct pt_regs *__unused);\
55 ALLOW_ERROR_INJECTION(__x64_sys_##sname, ERRNO); \
56 SYSCALL_ALIAS(__ia32_sys_##sname, __x64_sys_##sname); \
57 asmlinkage long __x64_sys_##sname(const struct pt_regs *__unused)
59 #define COND_SYSCALL(name) \
60 cond_syscall(__x64_sys_##name); \
61 cond_syscall(__ia32_sys_##name)
63 #define SYS_NI(name) \
64 SYSCALL_ALIAS(__x64_sys_##name, sys_ni_posix_timers); \
65 SYSCALL_ALIAS(__ia32_sys_##name, sys_ni_posix_timers)
67 #else /* CONFIG_IA32_EMULATION */
68 #define __IA32_COMPAT_SYS_STUBx(x, name, ...)
69 #define __IA32_SYS_STUBx(x, fullname, name, ...)
70 #endif /* CONFIG_IA32_EMULATION */
75 * For the x32 ABI, we need to create a stub for compat_sys_*() which is aware
76 * of the x86-64-style parameter ordering of x32 syscalls. The syscalls common
77 * with x86_64 obviously do not need such care.
79 #define __X32_COMPAT_SYS_STUBx(x, name, ...) \
80 asmlinkage long __x32_compat_sys##name(const struct pt_regs *regs);\
81 ALLOW_ERROR_INJECTION(__x32_compat_sys##name, ERRNO); \
82 asmlinkage long __x32_compat_sys##name(const struct pt_regs *regs)\
84 return __se_compat_sys##name(SC_X86_64_REGS_TO_ARGS(x,__VA_ARGS__));\
87 #else /* CONFIG_X86_X32 */
88 #define __X32_COMPAT_SYS_STUBx(x, name, ...)
89 #endif /* CONFIG_X86_X32 */
94 * Compat means IA32_EMULATION and/or X86_X32. As they use a different
95 * mapping of registers to parameters, we need to generate stubs for each
98 #define COMPAT_SYSCALL_DEFINEx(x, name, ...) \
99 static long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \
100 static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
101 __IA32_COMPAT_SYS_STUBx(x, name, __VA_ARGS__) \
102 __X32_COMPAT_SYS_STUBx(x, name, __VA_ARGS__) \
103 static long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \
105 return __do_compat_sys##name(__MAP(x,__SC_DELOUSE,__VA_ARGS__));\
107 static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
110 * As some compat syscalls may not be implemented, we need to expand
111 * COND_SYSCALL_COMPAT in kernel/sys_ni.c and COMPAT_SYS_NI in
112 * kernel/time/posix-stubs.c to cover this case as well.
114 #define COND_SYSCALL_COMPAT(name) \
115 cond_syscall(__ia32_compat_sys_##name); \
116 cond_syscall(__x32_compat_sys_##name)
118 #define COMPAT_SYS_NI(name) \
119 SYSCALL_ALIAS(__ia32_compat_sys_##name, sys_ni_posix_timers); \
120 SYSCALL_ALIAS(__x32_compat_sys_##name, sys_ni_posix_timers)
122 #endif /* CONFIG_COMPAT */
126 * Instead of the generic __SYSCALL_DEFINEx() definition, this macro takes
127 * struct pt_regs *regs as the only argument of the syscall stub named
128 * __x64_sys_*(). It decodes just the registers it needs and passes them on to
129 * the __se_sys_*() wrapper performing sign extension and then to the
130 * __do_sys_*() function doing the actual job. These wrappers and functions
131 * are inlined (at least in very most cases), meaning that the assembly looks
132 * as follows (slightly re-ordered for better readability):
134 * <__x64_sys_recv>: <-- syscall with 4 parameters
137 * mov 0x70(%rdi),%rdi <-- decode regs->di
138 * mov 0x68(%rdi),%rsi <-- decode regs->si
139 * mov 0x60(%rdi),%rdx <-- decode regs->dx
140 * mov 0x38(%rdi),%rcx <-- decode regs->r10
142 * xor %r9d,%r9d <-- clear %r9
143 * xor %r8d,%r8d <-- clear %r8
145 * callq __sys_recvfrom <-- do the actual work in __sys_recvfrom()
146 * which takes 6 arguments
148 * cltq <-- extend return value to 64-bit
151 * This approach avoids leaking random user-provided register content down
154 * If IA32_EMULATION is enabled, this macro generates an additional wrapper
155 * named __ia32_sys_*() which decodes the struct pt_regs *regs according
156 * to the i386 calling convention (bx, cx, dx, si, di, bp).
158 #define __SYSCALL_DEFINEx(x, name, ...) \
159 asmlinkage long __x64_sys##name(const struct pt_regs *regs); \
160 ALLOW_ERROR_INJECTION(__x64_sys##name, ERRNO); \
161 static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \
162 static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
163 asmlinkage long __x64_sys##name(const struct pt_regs *regs) \
165 return __se_sys##name(SC_X86_64_REGS_TO_ARGS(x,__VA_ARGS__));\
167 __IA32_SYS_STUBx(x, name, __VA_ARGS__) \
168 static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \
170 long ret = __do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__));\
171 __MAP(x,__SC_TEST,__VA_ARGS__); \
172 __PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__)); \
175 static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
178 * As the generic SYSCALL_DEFINE0() macro does not decode any parameters for
179 * obvious reasons, and passing struct pt_regs *regs to it in %rdi does not
180 * hurt, we only need to re-define it here to keep the naming congruent to
181 * SYSCALL_DEFINEx() -- which is essential for the COND_SYSCALL() and SYS_NI()
182 * macros to work correctly.
184 #ifndef SYSCALL_DEFINE0
185 #define SYSCALL_DEFINE0(sname) \
186 SYSCALL_METADATA(_##sname, 0); \
187 asmlinkage long __x64_sys_##sname(const struct pt_regs *__unused);\
188 ALLOW_ERROR_INJECTION(__x64_sys_##sname, ERRNO); \
189 asmlinkage long __x64_sys_##sname(const struct pt_regs *__unused)
193 #define COND_SYSCALL(name) cond_syscall(__x64_sys_##name)
197 #define SYS_NI(name) SYSCALL_ALIAS(__x64_sys_##name, sys_ni_posix_timers);
202 * For VSYSCALLS, we need to declare these three syscalls with the new
203 * pt_regs-based calling convention for in-kernel use.
206 asmlinkage long __x64_sys_getcpu(const struct pt_regs *regs);
207 asmlinkage long __x64_sys_gettimeofday(const struct pt_regs *regs);
208 asmlinkage long __x64_sys_time(const struct pt_regs *regs);
210 #endif /* _ASM_X86_SYSCALL_WRAPPER_H */