Skip to content

Commit 4141c85

Browse files
mrutland-armwildea01
authored andcommitted
arm64: convert raw syscall invocation to C
As a first step towards invoking syscalls with a pt_regs argument, convert the raw syscall invocation logic to C. We end up with a bit more register shuffling, but the unified invocation logic means we can unify the tracing paths, too. Previously, assembly had to open-code calls to ni_sys() when the system call number was out-of-bounds for the relevant syscall table. This case is now handled by invoke_syscall(), and the assembly no longer need to handle this case explicitly. This allows the tracing paths to be simplified and unified, as we no longer need the __ni_sys_trace path and the __sys_trace_return label. This only converts the invocation of the syscall. The rest of the syscall triage and tracing is left in assembly for now, and will be converted in subsequent patches. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 27d83e6 commit 4141c85

4 files changed

Lines changed: 59 additions & 43 deletions

File tree

‎arch/arm64/kernel/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ arm64-obj-y := debug-monitors.o entry.o irq.o fpsimd.o \
1818
hyp-stub.o psci.o cpu_ops.o insn.o \
1919
return_address.o cpuinfo.o cpu_errata.o \
2020
cpufeature.o alternative.o cacheinfo.o \
21-
smp.o smp_spin_table.o topology.o smccc-call.o
21+
smp.o smp_spin_table.o topology.o smccc-call.o \
22+
syscall.o
2223

2324
extra-$(CONFIG_EFI) := efi-entry.o
2425

‎arch/arm64/kernel/entry.S‎

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,6 @@ ENDPROC(el0_error)
903903
*/
904904
ret_fast_syscall:
905905
disable_daif
906-
str x0, [sp, #S_X0] // returned x0
907906
#ifndef CONFIG_DEBUG_RSEQ
908907
ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for syscall tracing
909908
and x2, x1, #_TIF_SYSCALL_WORK
@@ -978,15 +977,11 @@ el0_svc_naked: // compat entry point
978977

979978
tst x16, #_TIF_SYSCALL_WORK // check for syscall hooks
980979
b.ne __sys_trace
981-
cmp wscno, wsc_nr // check upper syscall limit
982-
b.hs ni_sys
983-
mask_nospec64 xscno, xsc_nr, x19 // enforce bounds for syscall number
984-
ldr x16, [stbl, xscno, lsl #3] // address in the syscall table
985-
blr x16 // call sys_* routine
986-
b ret_fast_syscall
987-
ni_sys:
988980
mov x0, sp
989-
bl do_ni_syscall
981+
mov w1, wscno
982+
mov w2, wsc_nr
983+
mov x3, stbl
984+
bl invoke_syscall
990985
b ret_fast_syscall
991986
ENDPROC(el0_svc)
992987

@@ -1003,29 +998,18 @@ __sys_trace:
1003998
bl syscall_trace_enter
1004999
cmp w0, #NO_SYSCALL // skip the syscall?
10051000
b.eq __sys_trace_return_skipped
1006-
mov wscno, w0 // syscall number (possibly new)
1007-
mov x1, sp // pointer to regs
1008-
cmp wscno, wsc_nr // check upper syscall limit
1009-
b.hs __ni_sys_trace
1010-
ldp x0, x1, [sp] // restore the syscall args
1011-
ldp x2, x3, [sp, #S_X2]
1012-
ldp x4, x5, [sp, #S_X4]
1013-
ldp x6, x7, [sp, #S_X6]
1014-
ldr x16, [stbl, xscno, lsl #3] // address in the syscall table
1015-
blr x16 // call sys_* routine
10161001

1017-
__sys_trace_return:
1018-
str x0, [sp, #S_X0] // save returned x0
1002+
mov x0, sp
1003+
mov w1, wscno
1004+
mov w2, wsc_nr
1005+
mov x3, stbl
1006+
bl invoke_syscall
1007+
10191008
__sys_trace_return_skipped:
10201009
mov x0, sp
10211010
bl syscall_trace_exit
10221011
b ret_to_user
10231012

1024-
__ni_sys_trace:
1025-
mov x0, sp
1026-
bl do_ni_syscall
1027-
b __sys_trace_return
1028-
10291013
.popsection // .entry.text
10301014

10311015
#ifdef CONFIG_UNMAP_KERNEL_AT_EL0

‎arch/arm64/kernel/syscall.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/errno.h>
4+
#include <linux/nospec.h>
5+
#include <linux/ptrace.h>
6+
#include <linux/syscalls.h>
7+
8+
#include <asm/syscall.h>
9+
10+
long compat_arm_syscall(struct pt_regs *regs);
11+
12+
asmlinkage long do_ni_syscall(struct pt_regs *regs)
13+
{
14+
#ifdef CONFIG_COMPAT
15+
long ret;
16+
if (is_compat_task()) {
17+
ret = compat_arm_syscall(regs);
18+
if (ret != -ENOSYS)
19+
return ret;
20+
}
21+
#endif
22+
23+
return sys_ni_syscall();
24+
}
25+
26+
static long __invoke_syscall(struct pt_regs *regs, syscall_fn_t syscall_fn)
27+
{
28+
return syscall_fn(regs->regs[0], regs->regs[1], regs->regs[2],
29+
regs->regs[3], regs->regs[4], regs->regs[5]);
30+
}
31+
32+
asmlinkage void invoke_syscall(struct pt_regs *regs, unsigned int scno,
33+
unsigned int sc_nr,
34+
const syscall_fn_t syscall_table[])
35+
{
36+
long ret;
37+
38+
if (scno < sc_nr) {
39+
syscall_fn_t syscall_fn;
40+
syscall_fn = syscall_table[array_index_nospec(scno, sc_nr)];
41+
ret = __invoke_syscall(regs, syscall_fn);
42+
} else {
43+
ret = do_ni_syscall(regs);
44+
}
45+
46+
regs->regs[0] = ret;
47+
}

‎arch/arm64/kernel/traps.c‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -547,22 +547,6 @@ asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
547547
do_undefinstr(regs);
548548
}
549549

550-
long compat_arm_syscall(struct pt_regs *regs);
551-
552-
asmlinkage long do_ni_syscall(struct pt_regs *regs)
553-
{
554-
#ifdef CONFIG_COMPAT
555-
long ret;
556-
if (is_compat_task()) {
557-
ret = compat_arm_syscall(regs);
558-
if (ret != -ENOSYS)
559-
return ret;
560-
}
561-
#endif
562-
563-
return sys_ni_syscall();
564-
}
565-
566550
static const char *esr_class_str[] = {
567551
[0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
568552
[ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",

0 commit comments

Comments
 (0)