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