XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
tools/init: the forked child execve()s /sbin/hello (a second static binary that writes a line and exits 7)
Heyang Zhou37e62f542185
diff --git a/tools/init/hello.s b/tools/init/hello.snew file mode 100644index 0000000..3264c57--- /dev/null+++ b/tools/init/hello.s@@ -0,0 +1,31 @@+// /sbin/hello for the test root: write a line to /dev/console (fd 0 is not set up: open it) and exit 7.+ .text+ .globl _start+ .p2align 2+_start:+ mov x9, sp+ and sp, x9, #~15+ adr x0, Lpath+ mov x1, #1 // O_WRONLY+ mov x2, #0+ mov x16, #5 // SYS_open+ svc #0x80+ b.cs Lfail+ adr x1, Lmsg+ mov x2, #(Lmsg_end - Lmsg)+ mov x16, #4 // SYS_write+ svc #0x80+ b.cs Lfail+ mov x0, #7+Lexit:+ mov x16, #1 // SYS_exit+ svc #0x80+1: b 1b+Lfail:+ mov x0, #9+ b Lexit+Lpath:+ .asciz "/dev/console"+Lmsg:+ .ascii "hello from /sbin/hello, exec'd by the child\n"+Lmsg_end:diff --git a/tools/init/start.s b/tools/init/start.sindex fec1db9..262a90d 100644--- a/tools/init/start.s+++ b/tools/init/start.s@@ -1,8 +1,8 @@// A minimal static arm64 Mach-O for /sbin/launchd on a test root, BSD syscalls through svc #0x80 only:// open("/dev/console", O_WRONLY), write a line, fork; the child writes its own line and exits 7,-// the parent wait4()s for it, writes a closing line and exits 42.+// the child then execve()s /sbin/hello (which writes its line and exits 7); the parent wait4()s for it, writes a closing line and exits 42.// Darwin reports a failed syscall with the carry flag set and errno in x0. Exit codes on failure:-// open 64+errno, write 128+errno, short write 3, fork 4, wait4 5, child status not 7 6.+// open 64+errno, write 128+errno, short write 3, fork 4, wait4 5, child status not 7 6 (8: execve failed, 9: /sbin/hello failed)..text.globl _start.p2align 2@@ -49,7 +49,13 @@ Lchild:adr x1, Lmsg2mov x2, #(Lmsg2_end - Lmsg2)bl Lwrite- mov x0, #7+ adr x0, Lhello // execve("/sbin/hello", {"/sbin/hello", NULL}, {NULL}); /sbin/hello exits 7+ stp x0, xzr, [sp, #-16]!+ mov x1, sp+ add x2, sp, #8+ mov x16, #59 // SYS_execve+ svc #0x80+ mov x0, #8 // execve returned: failedb Lexit// write(x19, x1, x2), exits on failureLwrite:@@ -87,6 +93,8 @@ Lmsg_end:Lmsg2:.ascii "hello from the forked child\n"Lmsg2_end:+Lhello:+ .asciz "/sbin/hello"Lmsg3:.ascii "parent: child exited with 7\n"Lmsg3_end:diff --git a/tools/init/vm-install.sh b/tools/init/vm-install.shindex 0d8ed4a..8a6bb16 100755--- a/tools/init/vm-install.sh+++ b/tools/init/vm-install.sh@@ -4,7 +4,9 @@ set -euo pipefailcd "$(dirname "$0")"; TC=~/w/dvm/toolchains/linux/bin26$TC/target-clang -target arm64-apple-macos14.0 -c start.s -o start.o$TC/target-clang -target arm64-apple-macos14.0 -nostdlib -static -e _start -Wl,-adhoc_codesign start.o -o init+$TC/target-clang -target arm64-apple-macos14.0 -c hello.s -o hello.o+$TC/target-clang -target arm64-apple-macos14.0 -nostdlib -static -e _start -Wl,-adhoc_codesign hello.o -o hellocp /lib/firmware/xnu/root.img /tmp/root.img; sudo mkdir -p /mnt/rdsudo mount -o loop -t hfsplus /tmp/root.img /mnt/rd-sudo cp init /mnt/rd/sbin/launchd; sudo chmod 755 /mnt/rd/sbin/launchd; ls -la /mnt/rd/sbin /mnt/rd/dev+sudo cp init /mnt/rd/sbin/launchd; sudo chmod 755 /mnt/rd/sbin/launchd; sudo cp hello /mnt/rd/sbin/hello; sudo chmod 755 /mnt/rd/sbin/hello; ls -la /mnt/rd/sbin /mnt/rd/devsudo umount /mnt/rd; sudo cp /tmp/root.img /lib/firmware/xnu/root.img