Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251208174940.949856-10-bill.roberts@arm.com>
Date: Mon,  8 Dec 2025 11:44:52 -0600
From: Bill Roberts <bill.roberts@....com>
To: musl@...ts.openwall.com
Cc: Bill Roberts <bill.roberts@....com>
Subject: [RFC 09/14] aarch64: rewrite longjmp routines in C using inline asm

Rewrite the AArch64 _longjmp and longjmp routines from assembly into
implementations using inline assembly.

This change eliminates the need for handwritten function prologues and
epilogues in longjmp.s, which simplifies maintenance and allows the compiler
to automatically insert architecture features such as BTI landing pads and
pointer authentication (PAC) sequences where applicable.

Moving to C also enables the compiler to manage register allocation,
stack usage, and ABI compliance automatically while keeping the low-level
behavior (bitmasks and register accesses) explicit and verifiable.

No functional changes intended.

Signed-off-by: Bill Roberts <bill.roberts@....com>
---
 src/setjmp/aarch64/longjmp.c | 39 ++++++++++++++++++++++++++++++++++++
 src/setjmp/aarch64/longjmp.s | 23 ---------------------
 2 files changed, 39 insertions(+), 23 deletions(-)
 create mode 100644 src/setjmp/aarch64/longjmp.c
 delete mode 100644 src/setjmp/aarch64/longjmp.s

diff --git a/src/setjmp/aarch64/longjmp.c b/src/setjmp/aarch64/longjmp.c
new file mode 100644
index 00000000..1ac107e5
--- /dev/null
+++ b/src/setjmp/aarch64/longjmp.c
@@ -0,0 +1,39 @@
+#include <setjmp.h>
+
+_Noreturn void longjmp(jmp_buf env, int val)
+{
+	__asm__ __volatile__(
+		/* Restore integer callee-saved regs x19..x30 */
+		"ldp x19, x20, [x0, #0]\n\t"
+		"ldp x21, x22, [x0, #16]\n\t"
+		"ldp x23, x24, [x0, #32]\n\t"
+		"ldp x25, x26, [x0, #48]\n\t"
+		"ldp x27, x28, [x0, #64]\n\t"
+		"ldp x29, x30, [x0, #80]\n\t"
+
+		/* Restore SP from [x0 + 104] */
+		"ldr x2, [x0, #104]\n\t"
+		"mov sp, x2\n\t"
+
+		/* Restore FP callee-saved d8..d15 */
+		"ldp d8 , d9 , [x0, #112]\n\t"
+		"ldp d10, d11, [x0, #128]\n\t"
+		"ldp d12, d13, [x0, #144]\n\t"
+		"ldp d14, d15, [x0, #160]\n\t"
+
+		/* Compute return value in w0: (w1 != 0 ? w1 : 1) */
+		"cmp  w1, #0\n\t"
+		"csinc w0, w1, wzr, ne\n\t"
+
+		/* Jump to saved LR */
+		"br x30\n\t"
+		:
+		:
+		: "memory", "cc" /* no clobbers, we need the register state */
+	);
+
+	__builtin_unreachable();
+}
+
+/* Export _longjmp as an alias of longjmp (same TU). */
+__attribute__((alias("longjmp"))) void _longjmp(jmp_buf env, int val);
diff --git a/src/setjmp/aarch64/longjmp.s b/src/setjmp/aarch64/longjmp.s
deleted file mode 100644
index 0af9c50e..00000000
--- a/src/setjmp/aarch64/longjmp.s
+++ /dev/null
@@ -1,23 +0,0 @@
-.global _longjmp
-.global longjmp
-.type _longjmp,%function
-.type longjmp,%function
-_longjmp:
-longjmp:
-	// IHI0055B_aapcs64.pdf 5.1.1, 5.1.2 callee saved registers
-	ldp x19, x20, [x0,#0]
-	ldp x21, x22, [x0,#16]
-	ldp x23, x24, [x0,#32]
-	ldp x25, x26, [x0,#48]
-	ldp x27, x28, [x0,#64]
-	ldp x29, x30, [x0,#80]
-	ldr x2, [x0,#104]
-	mov sp, x2
-	ldp d8 , d9, [x0,#112]
-	ldp d10, d11, [x0,#128]
-	ldp d12, d13, [x0,#144]
-	ldp d14, d15, [x0,#160]
-
-	cmp w1, 0
-	csinc w0, w1, wzr, ne
-	br x30
-- 
2.51.0

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.