XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Test init: console as fds 0-2, the child execs Apple's /bin/echo (dyld + shared cache) when the root has it, else /sbin/hello; vm-mk-sysdisk.sh builds an HFS+ system disk image for the host-disk root
Heyang Zhou875240c8c2a4
diff --git a/scripts/vm-mk-sysdisk.sh b/scripts/vm-mk-sysdisk.shnew file mode 100755index 0000000..b06ce6c--- /dev/null+++ b/scripts/vm-mk-sysdisk.sh@@ -0,0 +1,19 @@+#!/bin/bash+# In the VM: an HFS+ system disk image for the host-disk root (/var/tmp/sys.img): Apple's dyld, the dyld shared cache,+# a few /bin tools (from /tmp/sys.tar, made on the macOS host; Apple binaries stay local, never in the repo) and the test init.+set -euo pipefail+IMG=${IMG:-/var/tmp/sys.img}+cd ~/w/poc/tools/init && bash vm-install.sh >/dev/null 2>&1 || true+sudo umount /mnt/sys 2>/dev/null || true+rm -f $IMG; truncate -s 6G $IMG+mkfs.hfsplus -v xnulinux-sys $IMG >/dev/null+sudo mkdir -p /mnt/sys; sudo mount -o loop -t hfsplus $IMG /mnt/sys+sudo tar xf /tmp/sys.tar -C /mnt/sys+sudo mkdir -p /mnt/sys/sbin /mnt/sys/dev /mnt/sys/private/var/select /mnt/sys/private/tmp /mnt/sys/System/Library /mnt/sys/usr/lib+sudo ln -sf private/var /mnt/sys/var; sudo ln -sf private/tmp /mnt/sys/tmp+sudo ln -sf /bin/bash /mnt/sys/private/var/select/sh+[ -e /mnt/sys/System/Library/dyld ] || sudo ln -sf /System/Volumes/Preboot/Cryptexes/OS/System/Library/dyld /mnt/sys/System/Library/dyld+sudo cp ~/w/poc/tools/init/init /mnt/sys/sbin/launchd; sudo cp ~/w/poc/tools/init/hello /mnt/sys/sbin/hello+sudo chmod 755 /mnt/sys/sbin/launchd /mnt/sys/sbin/hello+sudo du -sh /mnt/sys; ls -la /mnt/sys /mnt/sys/bin+sudo umount /mnt/sysdiff --git a/tools/init/start.s b/tools/init/start.sindex 262a90d..22b1577 100644--- a/tools/init/start.s+++ b/tools/init/start.s@@ -2,20 +2,24 @@// open("/dev/console", O_WRONLY), write a line, fork; the child writes its own line and exits 7,// 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 (8: execve failed, 9: /sbin/hello failed).+// open 64+errno, write 128+errno, short write 3, fork 4, wait4 5, child status not 7 or 0: 16+status (8: execve failed, 9: /sbin/hello failed)..text.globl _start.p2align 2_start:mov x9, sp // the kernel hands over sp at the Mach-O stack's argc slot (8-aligned); EL0 checks SP alignmentand sp, x9, #~15+ mov x22, #3 // /dev/console as fds 0, 1, 2 (stdin, stdout, stderr for what the child execs)+Lopen_again:adr x0, Lpath- mov x1, #1 // O_WRONLY+ mov x1, #2 // O_RDWRmov x2, #0mov x16, #5 // SYS_opensvc #0x80b.cs Lopen_failed- mov x19, x0 // fd+ subs x22, x22, #1+ b.ne Lopen_again+ mov x19, #1 // write to stdoutadr x1, Lmsgmov x2, #(Lmsg_end - Lmsg)bl Lwrite@@ -35,7 +39,9 @@ _start:ldr w0, [sp]lsr w0, w0, #8 // WEXITSTATUSand w0, w0, #0xff- cmp w0, #7+ mov x23, x0+ cmp w0, #7 // /sbin/hello exits 7, /bin/echo 0+ ccmp w0, #0, #4, neb.ne Lbad_statusadr x1, Lmsg3mov x2, #(Lmsg3_end - Lmsg3)@@ -49,7 +55,16 @@ Lchild:adr x1, Lmsg2mov x2, #(Lmsg2_end - Lmsg2)bl Lwrite- adr x0, Lhello // execve("/sbin/hello", {"/sbin/hello", NULL}, {NULL}); /sbin/hello exits 7+ adr x0, Lecho // execve("/bin/echo", {"echo", "hello from /bin/echo ...", NULL}, {NULL}): Apple's dynamically+ adr x1, Lechoarg // linked echo through dyld and the shared cache, where the root has them+ stp x1, xzr, [sp, #-16]!+ adr x1, Lecho0+ stp xzr, x1, [sp, #-16]! // sp+0: envp NULL, sp+8: argv[0], sp+16: argv[1], sp+24: NULL+ add x1, sp, #8+ mov x2, sp+ mov x16, #59 // SYS_execve+ svc #0x80+ adr x0, Lhello // no /bin/echo: execve("/sbin/hello", {"/sbin/hello", NULL}, {NULL}); /sbin/hello exits 7stp x0, xzr, [sp, #-16]!mov x1, spadd x2, sp, #8@@ -83,7 +98,7 @@ Lwait_failed:mov x0, #5b LexitLbad_status:- mov x0, #6+ add x0, x23, #16 // 16 + the child's exit statusb LexitLpath:.asciz "/dev/console"@@ -95,6 +110,12 @@ Lmsg2:Lmsg2_end:Lhello:.asciz "/sbin/hello"+Lecho:+ .asciz "/bin/echo"+Lecho0:+ .asciz "echo"+Lechoarg:+ .asciz "hello from Apple's /bin/echo (dyld + dyld shared cache) on Linux"Lmsg3:- .ascii "parent: child exited with 7\n"+ .ascii "parent: child exited with 7 or 0\n"Lmsg3_end: