[PATCH RFC 10/28] ARM: entry: move all tracing invocation to C

From: Linus Walleij
Date: Thu Oct 10 2024 - 07:35:29 EST


The traced invocation of syscall is rewritten in C, moving
over also the call to syscall_trace_exit() to C.

To do this we cannot have invoke_syscall_trace_asm() be a
leaf call, which is the mechanism we hithereto relied on
to make sure the stack is pulled back to the state we were
at before we called out to the code written in C.

Push the registers potentially used by C on the stack, and
make a copy of the two stack-pushed syscall arguments
on the top of the stack before invoking the syscall and
then drop these copied arguments and pop back the
registers before returning from invoke_syscall_trace_asm().

As we are now calling from and returning to C, we no
longer need to pass the regs pointer around so drop it.

Signed-off-by: Linus Walleij <linus.walleij@xxxxxxxxxx>
---
arch/arm/include/asm/syscall.h | 2 +-
arch/arm/kernel/entry-common.S | 32 +++++++++++++++-----------------
arch/arm/kernel/ptrace.c | 2 +-
arch/arm/kernel/syscall.c | 30 +++++++++++++++++++++---------
4 files changed, 38 insertions(+), 28 deletions(-)

diff --git a/arch/arm/include/asm/syscall.h b/arch/arm/include/asm/syscall.h
index 66067d165ba3..cb0073c4151b 100644
--- a/arch/arm/include/asm/syscall.h
+++ b/arch/arm/include/asm/syscall.h
@@ -20,7 +20,7 @@
extern const unsigned long sys_call_table[];

int invoke_syscall(void *table, struct pt_regs *regs, int scno, void *retp);
-int invoke_syscall_trace(void *table, struct pt_regs *regs, void *retp);
+void invoke_syscall_trace(void *table, struct pt_regs *regs);

static inline int syscall_get_nr(struct task_struct *task,
struct pt_regs *regs)
diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S
index 8baab7f97f59..dbc947d301ec 100644
--- a/arch/arm/kernel/entry-common.S
+++ b/arch/arm/kernel/entry-common.S
@@ -296,22 +296,11 @@ ENDPROC(vector_swi)
__sys_trace:
add r1, sp, #S_R0 + S_OFF @ pointer to regs
mov r0, tbl
- badr r2, __sys_trace_return
bl invoke_syscall_trace
- cmp r0, #-1
- bne __sys_trace_return
- add sp, sp, #S_OFF @ restore stack
-
-__sys_trace_return_nosave:
- enable_irq_notrace
- mov r0, sp
- bl syscall_trace_exit
+ add sp, sp, #S_OFF @ restore stack pointer
b ret_to_user

-__sys_trace_return:
- str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
- mov r0, sp
- bl syscall_trace_exit
+__sys_trace_return_nosave:
b ret_to_user

.macro syscall_table_start, sym
@@ -437,18 +426,27 @@ SYM_FUNC_END(invoke_syscall_asm)
* r0: syscall table
* r1: regs
* r2: syscall number
- * r3: pointer to return function
*/
SYM_TYPED_FUNC_START(invoke_syscall_trace_asm)
#ifdef CONFIG_CPU_SPECTRE
csdb
#endif
+ /* Save registers because we are being called from C */
+ push {r4 - r10, lr}
mov tbl, r0
+ /* Make space to copy the two syscall stack arguments */
+ sub sp, sp, #S_OFF
mov scno, r2
- mov lr, r3 @ return address
- ldmia r1, {r0 - r6} @ reload r0-r6
- stmia sp, {r4, r5} @ update stack arguments
+ badr lr, __invoke_syscall_trace_ret @ return right here
+ ldmia r1, {r0 - r6} @ reload r0-r6 from regs
+ stmia sp, {r4, r5} @ copy stack arguments
ldr pc, [tbl, scno, lsl #2] @ call sys_* routine
+__invoke_syscall_trace_ret:
+ /* Drop the copied stack arguments */
+ add sp, sp, #S_OFF
+ pop {r4 - r10, lr}
+ ARM( mov pc, lr )
+ THUMB( bx lr )
SYM_FUNC_END(invoke_syscall_trace_asm)

#ifdef CONFIG_OABI_COMPAT
diff --git a/arch/arm/kernel/ptrace.c b/arch/arm/kernel/ptrace.c
index 07b0daf47441..ac7b98ae4724 100644
--- a/arch/arm/kernel/ptrace.c
+++ b/arch/arm/kernel/ptrace.c
@@ -888,7 +888,7 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs)
return scno;
}

-asmlinkage void syscall_trace_exit(struct pt_regs *regs)
+void syscall_trace_exit(struct pt_regs *regs)
{
/*
* Audit the syscall before anything else, as a debugger may
diff --git a/arch/arm/kernel/syscall.c b/arch/arm/kernel/syscall.c
index 815312f7b254..3ee367958298 100644
--- a/arch/arm/kernel/syscall.c
+++ b/arch/arm/kernel/syscall.c
@@ -17,22 +17,34 @@ __visible int invoke_syscall(void *table, struct pt_regs *regs, int scno, void *
return sys_ni_syscall();
}

-int invoke_syscall_trace_asm(void *table, struct pt_regs *regs, int scno, void *retp);
+int invoke_syscall_trace_asm(void *table, struct pt_regs *regs, int scno);

-__visible int invoke_syscall_trace(void *table, struct pt_regs *regs, void *retp)
+__visible void invoke_syscall_trace(void *table, struct pt_regs *regs)
{
int scno;
+ int ret;

scno = syscall_trace_enter(regs);
if (scno == -1)
- return -1;
+ goto trace_exit_nosave;

- if (scno < NR_syscalls)
- /* Doing this with return makes sure the stack gets pop:ed */
- return invoke_syscall_trace_asm(table, regs, scno, retp);
+ if (scno < NR_syscalls) {
+ ret = invoke_syscall_trace_asm(table, regs, scno);
+ goto trace_exit_save;
+ }

- if (scno >= __ARM_NR_BASE)
- return arm_syscall(scno, regs);
+ if (scno >= __ARM_NR_BASE) {
+ ret = arm_syscall(scno, regs);
+ goto trace_exit_save;
+ }

- return sys_ni_syscall();
+ ret = sys_ni_syscall();
+
+trace_exit_save:
+ /* Save return value from syscall */
+ regs->ARM_r0 = ret;
+
+trace_exit_nosave:
+ local_irq_enable();
+ syscall_trace_exit(regs);
}

--
2.46.2