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-5-bill.roberts@arm.com>
Date: Mon,  8 Dec 2025 11:44:47 -0600
From: Bill Roberts <bill.roberts@....com>
To: musl@...ts.openwall.com
Cc: Bill Roberts <bill.roberts@....com>
Subject: [RFC 04/14] aarch64: rewrite clone routine in C using inline asm

Rewrite the AArch64 clean routine from assembly into
C implementations using inline assembly.

This change eliminates the need for handwritten function prologues and
epilogues in vfork.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/thread/aarch64/clone.c | 44 ++++++++++++++++++++++++++++++++++++++
 src/thread/aarch64/clone.s | 31 ---------------------------
 2 files changed, 44 insertions(+), 31 deletions(-)
 create mode 100644 src/thread/aarch64/clone.c
 delete mode 100644 src/thread/aarch64/clone.s

diff --git a/src/thread/aarch64/clone.c b/src/thread/aarch64/clone.c
new file mode 100644
index 00000000..f69d7b42
--- /dev/null
+++ b/src/thread/aarch64/clone.c
@@ -0,0 +1,44 @@
+// __clone(func, stack, flags, arg, ptid, tls, ctid)
+//         x0,   x1,    w2,    x3,  x4,   x5,  x6
+
+// syscall(SYS_clone, flags, stack, ptid, tls, ctid)
+//         x8,        x0,    x1,    x2,   x3,  x4
+
+#include "syscall.h"
+
+__attribute__((visibility("hidden")))
+int __clone(int (*fn)(void *), void *stack, int flags, void *arg,
+			int *ptid, void *tls, int *ctid)
+{
+	__asm__ __volatile__(
+		// align stack and save func,arg
+		"and x1, x1, #-16\n\t"
+		"stp x0, x3, [x1, #-16]!\n\t"
+
+		// syscall: clone(flags, stack, ptid, tls, ctid)
+		"uxtw x0, w2\n\t"      // x0 = (uint32_t)flags
+		"mov  x2, x4\n\t"      // ptid
+		"mov  x3, x5\n\t"      // tls
+		"mov  x4, x6\n\t"      // ctid
+		"mov  x8, #220\n\t"    // SYS_clone
+		"svc  #0\n\t"
+
+		"cbz  x0, 1f\n\t"      // child gets 0
+		// parent: returns to caller with x0 = pid / -errno
+		"ret\n\t"
+
+		// child
+	"1:\n\t"
+		"mov  x29, xzr\n\t"
+		"ldp  x1, x0, [sp], #16\n\t"  // x1=fn, x0=arg
+		"blr  x1\n\t"                 // fn(arg) -> x0
+		"mov  x8, #93\n\t"            // SYS_exit
+		"svc  #0\n\t"
+		:
+		:
+		: "x1","x2","x3","x4","x8",
+		  "memory","cc"
+	);
+	__builtin_unreachable();
+}
+
diff --git a/src/thread/aarch64/clone.s b/src/thread/aarch64/clone.s
deleted file mode 100644
index aff8155b..00000000
--- a/src/thread/aarch64/clone.s
+++ /dev/null
@@ -1,31 +0,0 @@
-// __clone(func, stack, flags, arg, ptid, tls, ctid)
-//         x0,   x1,    w2,    x3,  x4,   x5,  x6
-
-// syscall(SYS_clone, flags, stack, ptid, tls, ctid)
-//         x8,        x0,    x1,    x2,   x3,  x4
-
-.global __clone
-.hidden __clone
-.type   __clone,%function
-__clone:
-	// align stack and save func,arg
-	and x1,x1,#-16
-	stp x0,x3,[x1,#-16]!
-
-	// syscall
-	uxtw x0,w2
-	mov x2,x4
-	mov x3,x5
-	mov x4,x6
-	mov x8,#220 // SYS_clone
-	svc #0
-
-	cbz x0,1f
-	// parent
-	ret
-	// child
-1:	mov x29, 0
-	ldp x1,x0,[sp],#16
-	blr x1
-	mov x8,#93 // SYS_exit
-	svc #0
-- 
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.