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