[PATCH v3 3/4] arm64: ftrace: Preserve original link register value in ftrace_regs

Ard Biesheuvel ardb at kernel.org
Fri Dec 9 07:20:47 PST 2022


In order to be able to add pointer authentication and/or shadow call
stack support to the ftrace asm routines, it will need to reason about
whether or not the callsite's return address was updated to point to
return_to_handler(), as in this case, we want the authentication to
occur there and not before returning to the call site.

To make this a bit easier, preserve the value of register X9, which
carries the callsite's LR value upon entry to ftrace_caller, so in a
later patch, we can compare it to the callsite's effective LR upon
return, and omit the authentication if the caller will be returning via
return_to_handler().

Signed-off-by: Ard Biesheuvel <ardb at kernel.org>
---
 arch/arm64/include/asm/ftrace.h  |  2 +-
 arch/arm64/kernel/entry-ftrace.S | 12 ++++++------
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrace.h
index 5664729800ae1c13..b07501645a74031a 100644
--- a/arch/arm64/include/asm/ftrace.h
+++ b/arch/arm64/include/asm/ftrace.h
@@ -86,7 +86,7 @@ struct ftrace_ops;
 struct ftrace_regs {
 	/* x0 - x8 */
 	unsigned long regs[9];
-	unsigned long __unused;
+	unsigned long orig_lr;		// must follow &regs[8]
 
 	unsigned long fp;
 	unsigned long lr;
diff --git a/arch/arm64/kernel/entry-ftrace.S b/arch/arm64/kernel/entry-ftrace.S
index 30cc2a9d1757a6a7..bccd525241ab615d 100644
--- a/arch/arm64/kernel/entry-ftrace.S
+++ b/arch/arm64/kernel/entry-ftrace.S
@@ -42,12 +42,12 @@ SYM_CODE_START(ftrace_caller)
 	/* Make room for ftrace regs, plus two frame records */
 	sub	sp, sp, #(FREGS_SIZE + 32)
 
-	/* Save function arguments */
+	/* Save function arguments and original callsite LR */
 	stp	x0, x1, [sp, #FREGS_X0]
 	stp	x2, x3, [sp, #FREGS_X2]
 	stp	x4, x5, [sp, #FREGS_X4]
 	stp	x6, x7, [sp, #FREGS_X6]
-	str	x8,     [sp, #FREGS_X8]
+	stp	x8, x9, [sp, #FREGS_X8]
 
 	/* Save the callsite's FP, LR, SP */
 	str	x29, [sp, #FREGS_FP]
@@ -78,22 +78,22 @@ SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
  * x19-x29 per the AAPCS, and we created frame records upon entry, so we need
  * to restore x0-x8, x29, and x30.
  */
-	/* Restore function arguments */
+	/* Restore function arguments and original callsite LR */
 	ldp	x0, x1, [sp, #FREGS_X0]
 	ldp	x2, x3, [sp, #FREGS_X2]
 	ldp	x4, x5, [sp, #FREGS_X4]
 	ldp	x6, x7, [sp, #FREGS_X6]
-	ldr	x8,     [sp, #FREGS_X8]
+	ldp	x8, x9, [sp, #FREGS_X8]
 
 	/* Restore the callsite's FP, LR, PC */
 	ldr	x29, [sp, #FREGS_FP]
 	ldr	x30, [sp, #FREGS_LR]
-	ldr	x9,  [sp, #FREGS_PC]
+	ldr	x10, [sp, #FREGS_PC]
 
 	/* Restore the callsite's SP */
 	add	sp, sp, #FREGS_SIZE + 32
 
-	ret	x9
+	ret	x10
 SYM_CODE_END(ftrace_caller)
 
 #else /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
-- 
2.35.1




More information about the linux-arm-kernel mailing list