Skip to content

Commit 25c619e

Browse files
Brian GerstKAGA-KOKO
authored andcommitted
x86/entry/32: Enable pt_regs based syscalls
Enable pt_regs based syscalls for 32-bit. This makes the 32-bit native kernel consistent with the 64-bit kernel, and improves the syscall interface by not needing to push all 6 potential arguments onto the stack. Signed-off-by: Brian Gerst <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Dominik Brodowski <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 121b32a commit 25c619e

File tree

6 files changed

+64
-114
lines changed

6 files changed

+64
-114
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ config X86_64
3030
select MODULES_USE_ELF_RELA
3131
select NEED_DMA_MAP_STATE
3232
select SWIOTLB
33-
select ARCH_HAS_SYSCALL_WRAPPER
3433

3534
config FORCE_DYNAMIC_FTRACE
3635
def_bool y
@@ -80,6 +79,7 @@ config X86
8079
select ARCH_HAS_STRICT_KERNEL_RWX
8180
select ARCH_HAS_STRICT_MODULE_RWX
8281
select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE
82+
select ARCH_HAS_SYSCALL_WRAPPER
8383
select ARCH_HAS_UBSAN_SANITIZE_ALL
8484
select ARCH_HAVE_NMI_SAFE_CMPXCHG
8585
select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI

arch/x86/entry/common.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,7 @@ static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs)
333333

334334
if (likely(nr < IA32_NR_syscalls)) {
335335
nr = array_index_nospec(nr, IA32_NR_syscalls);
336-
#ifdef CONFIG_IA32_EMULATION
337336
regs->ax = ia32_sys_call_table[nr](regs);
338-
#else
339-
/*
340-
* It's possible that a 32-bit syscall implementation
341-
* takes a 64-bit parameter but nonetheless assumes that
342-
* the high bits are zero. Make sure we zero-extend all
343-
* of the args.
344-
*/
345-
regs->ax = ia32_sys_call_table[nr](
346-
(unsigned int)regs->bx, (unsigned int)regs->cx,
347-
(unsigned int)regs->dx, (unsigned int)regs->si,
348-
(unsigned int)regs->di, (unsigned int)regs->bp);
349-
#endif /* CONFIG_IA32_EMULATION */
350337
}
351338

352339
syscall_return_slowpath(regs);
@@ -439,9 +426,7 @@ __visible long do_fast_syscall_32(struct pt_regs *regs)
439426
}
440427
#endif
441428

442-
#ifdef CONFIG_X86_64
443429
SYSCALL_DEFINE0(ni_syscall)
444430
{
445431
return -ENOSYS;
446432
}
447-
#endif

arch/x86/entry/syscall_32.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,22 @@
44
#include <linux/linkage.h>
55
#include <linux/sys.h>
66
#include <linux/cache.h>
7+
#include <linux/syscalls.h>
78
#include <asm/unistd.h>
89
#include <asm/syscall.h>
910

10-
#ifdef CONFIG_IA32_EMULATION
11-
/* On X86_64, we use struct pt_regs * to pass parameters to syscalls */
1211
#define __SYSCALL_I386(nr, sym) extern asmlinkage long __ia32_##sym(const struct pt_regs *);
13-
#define __sys_ni_syscall __ia32_sys_ni_syscall
14-
#else /* CONFIG_IA32_EMULATION */
15-
#define __SYSCALL_I386(nr, sym) extern asmlinkage long sym(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long);
16-
extern asmlinkage long sys_ni_syscall(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long);
17-
#define __sys_ni_syscall sys_ni_syscall
18-
#endif /* CONFIG_IA32_EMULATION */
1912

2013
#include <asm/syscalls_32.h>
2114
#undef __SYSCALL_I386
2215

23-
#ifdef CONFIG_IA32_EMULATION
2416
#define __SYSCALL_I386(nr, sym) [nr] = __ia32_##sym,
25-
#else /* CONFIG_IA32_EMULATION */
26-
#define __SYSCALL_I386(nr, sym) [nr] = sym,
27-
#endif /* CONFIG_IA32_EMULATION */
2817

2918
__visible const sys_call_ptr_t ia32_sys_call_table[__NR_ia32_syscall_max+1] = {
3019
/*
3120
* Smells like a compiler bug -- it doesn't work
3221
* when the & below is removed.
3322
*/
34-
[0 ... __NR_ia32_syscall_max] = &__sys_ni_syscall,
23+
[0 ... __NR_ia32_syscall_max] = &__ia32_sys_ni_syscall,
3524
#include <asm/syscalls_32.h>
3625
};

arch/x86/include/asm/syscall.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@
1616
#include <asm/thread_info.h> /* for TS_COMPAT */
1717
#include <asm/unistd.h>
1818

19-
#ifdef CONFIG_X86_64
2019
typedef asmlinkage long (*sys_call_ptr_t)(const struct pt_regs *);
21-
#else
22-
typedef asmlinkage long (*sys_call_ptr_t)(unsigned long, unsigned long,
23-
unsigned long, unsigned long,
24-
unsigned long, unsigned long);
25-
#endif /* CONFIG_X86_64 */
2620
extern const sys_call_ptr_t sys_call_table[];
2721

2822
#if defined(CONFIG_X86_32)

arch/x86/include/asm/syscall_wrapper.h

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,47 @@ struct pt_regs;
1111
extern asmlinkage long __x64_sys_ni_syscall(const struct pt_regs *regs);
1212
extern asmlinkage long __ia32_sys_ni_syscall(const struct pt_regs *regs);
1313

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+
* __x32_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+
1455
/* Mapping of registers to parameters for syscalls on x86-64 and x32 */
1556
#define SC_X86_64_REGS_TO_ARGS(x, ...) \
1657
__MAP(x,__SC_ARGS \
@@ -68,6 +109,26 @@ extern asmlinkage long __ia32_sys_ni_syscall(const struct pt_regs *regs);
68109
#define __X64_SYS_NI(name)
69110
#endif /* CONFIG_X86_64 */
70111

112+
#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
113+
#define __IA32_SYS_STUB0(name) \
114+
__SYS_STUB0(ia32, sys_##name)
115+
116+
#define __IA32_SYS_STUBx(x, name, ...) \
117+
__SYS_STUBx(ia32, sys##name, \
118+
SC_IA32_REGS_TO_ARGS(x, __VA_ARGS__))
119+
120+
#define __IA32_COND_SYSCALL(name) \
121+
__COND_SYSCALL(ia32, sys_##name)
122+
123+
#define __IA32_SYS_NI(name) \
124+
__SYS_NI(ia32, sys_##name)
125+
#else /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
126+
#define __IA32_SYS_STUB0(name)
127+
#define __IA32_SYS_STUBx(x, name, ...)
128+
#define __IA32_COND_SYSCALL(name)
129+
#define __IA32_SYS_NI(name)
130+
#endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
131+
71132
#ifdef CONFIG_IA32_EMULATION
72133
/*
73134
* For IA32 emulation, we need to handle "compat" syscalls *and* create
@@ -90,27 +151,11 @@ extern asmlinkage long __ia32_sys_ni_syscall(const struct pt_regs *regs);
90151
#define __IA32_COMPAT_SYS_NI(name) \
91152
__SYS_NI(ia32, compat_sys_##name)
92153

93-
#define __IA32_SYS_STUB0(name) \
94-
__SYS_STUB0(ia32, sys_##name)
95-
96-
#define __IA32_SYS_STUBx(x, name, ...) \
97-
__SYS_STUBx(ia32, sys##name, \
98-
SC_IA32_REGS_TO_ARGS(x, __VA_ARGS__))
99-
100-
#define __IA32_COND_SYSCALL(name) \
101-
__COND_SYSCALL(ia32, sys_##name)
102-
103-
#define __IA32_SYS_NI(name) \
104-
__SYS_NI(ia32, sys_##name)
105154
#else /* CONFIG_IA32_EMULATION */
106155
#define __IA32_COMPAT_SYS_STUB0(name)
107156
#define __IA32_COMPAT_SYS_STUBx(x, name, ...)
108157
#define __IA32_COMPAT_COND_SYSCALL(name)
109158
#define __IA32_COMPAT_SYS_NI(name)
110-
#define __IA32_SYS_STUB0(name)
111-
#define __IA32_SYS_STUBx(x, name, ...)
112-
#define __IA32_COND_SYSCALL(name)
113-
#define __IA32_SYS_NI(name)
114159
#endif /* CONFIG_IA32_EMULATION */
115160

116161

@@ -180,40 +225,6 @@ extern asmlinkage long __ia32_sys_ni_syscall(const struct pt_regs *regs);
180225

181226
#endif /* CONFIG_COMPAT */
182227

183-
184-
/*
185-
* Instead of the generic __SYSCALL_DEFINEx() definition, this macro takes
186-
* struct pt_regs *regs as the only argument of the syscall stub named
187-
* __x64_sys_*(). It decodes just the registers it needs and passes them on to
188-
* the __se_sys_*() wrapper performing sign extension and then to the
189-
* __do_sys_*() function doing the actual job. These wrappers and functions
190-
* are inlined (at least in very most cases), meaning that the assembly looks
191-
* as follows (slightly re-ordered for better readability):
192-
*
193-
* <__x64_sys_recv>: <-- syscall with 4 parameters
194-
* callq <__fentry__>
195-
*
196-
* mov 0x70(%rdi),%rdi <-- decode regs->di
197-
* mov 0x68(%rdi),%rsi <-- decode regs->si
198-
* mov 0x60(%rdi),%rdx <-- decode regs->dx
199-
* mov 0x38(%rdi),%rcx <-- decode regs->r10
200-
*
201-
* xor %r9d,%r9d <-- clear %r9
202-
* xor %r8d,%r8d <-- clear %r8
203-
*
204-
* callq __sys_recvfrom <-- do the actual work in __sys_recvfrom()
205-
* which takes 6 arguments
206-
*
207-
* cltq <-- extend return value to 64-bit
208-
* retq <-- return
209-
*
210-
* This approach avoids leaking random user-provided register content down
211-
* the call chain.
212-
*
213-
* If IA32_EMULATION is enabled, this macro generates an additional wrapper
214-
* named __ia32_sys_*() which decodes the struct pt_regs *regs according
215-
* to the i386 calling convention (bx, cx, dx, si, di, bp).
216-
*/
217228
#define __SYSCALL_DEFINEx(x, name, ...) \
218229
static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \
219230
static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\

arch/x86/include/asm/syscalls.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,4 @@
1717
/* kernel/ioport.c */
1818
long ksys_ioperm(unsigned long from, unsigned long num, int turn_on);
1919

20-
#ifdef CONFIG_X86_32
21-
/*
22-
* These definitions are only valid on pure 32-bit systems; x86-64 uses a
23-
* different syscall calling convention
24-
*/
25-
asmlinkage long sys_ioperm(unsigned long, unsigned long, int);
26-
asmlinkage long sys_iopl(unsigned int);
27-
28-
/* kernel/ldt.c */
29-
asmlinkage long sys_modify_ldt(int, void __user *, unsigned long);
30-
31-
/* kernel/signal.c */
32-
asmlinkage long sys_rt_sigreturn(void);
33-
34-
/* kernel/tls.c */
35-
asmlinkage long sys_set_thread_area(struct user_desc __user *);
36-
asmlinkage long sys_get_thread_area(struct user_desc __user *);
37-
38-
/* X86_32 only */
39-
40-
/* kernel/signal.c */
41-
asmlinkage long sys_sigreturn(void);
42-
43-
/* kernel/vm86_32.c */
44-
struct vm86_struct;
45-
asmlinkage long sys_vm86old(struct vm86_struct __user *);
46-
asmlinkage long sys_vm86(unsigned long, unsigned long);
47-
48-
#endif /* CONFIG_X86_32 */
4920
#endif /* _ASM_X86_SYSCALLS_H */

0 commit comments

Comments
 (0)