Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

Makefile, qemu: make qemu-open works in one command

shimokua00532af65b5
diff --git a/Makefile b/Makefile
index 4c9a842..461f07b 100644
--- a/Makefile
+++ b/Makefile
@@ -46,7 +46,7 @@ images:
kernel:
[ -d xnu-src/xnu-8796.121.2 ] || xnu/apply.sh
[ -e xnu-deps/host/bin/ld64 ] || xnu-deps/setup.sh
- ./xnu-build.sh -j$(JOBS) -k
+ tools/make-kernel.sh -j$(JOBS)
loader:
xnuloader/build.sh
diff --git a/qemu/run.sh b/qemu/run.sh
index dd8667b..e594139 100755
--- a/qemu/run.sh
+++ b/qemu/run.sh
@@ -5,13 +5,20 @@
# default of 1 GB mostly costs host memory)
# QEMU_SMP processor cores of the machine (default 1); the kernel uses as many of them as
# its boot-arg cpus=N says (CMDLINE="cpus=4" for run-ios.sh), one without it
-# QEMU_EFI, QEMU_VARS the UEFI firmware for aarch64 guests (default: where Arch's edk2-aarch64
-# puts it; Debian/Ubuntu: /usr/share/AAVMF/AAVMF_CODE.fd and AAVMF_VARS.fd)
+# QEMU_EFI, QEMU_VARS the UEFI firmware for aarch64 guests (found by itself where Arch's edk2-aarch64
+# puts it, or Debian/Ubuntu's /usr/share/AAVMF/AAVMF_CODE.fd and AAVMF_VARS.fd)
cd "$(dirname "$0")"
T=${1:-20}; shift
-EFI=${QEMU_EFI:-/usr/share/edk2/aarch64/QEMU_EFI.fd}
-[ -f "$EFI" ] || { echo "run.sh: no UEFI firmware at $EFI (set QEMU_EFI and QEMU_VARS)"; exit 1; }
-cp "${QEMU_VARS:-/usr/share/edk2/aarch64/QEMU_VARS.fd}" vars.fd 2>/dev/null
+# the UEFI firmware: where Arch's edk2-aarch64 puts it, or where Debian and Ubuntu's AAVMF does
+EFI=${QEMU_EFI-}; VARS_SRC=${QEMU_VARS-}
+if [ -z "$EFI" ]; then
+ for f in /usr/share/edk2/aarch64/QEMU_EFI.fd /usr/share/AAVMF/AAVMF_CODE.fd; do [ -f "$f" ] && { EFI=$f; break; }; done
+fi
+if [ -z "$VARS_SRC" ]; then
+ for f in /usr/share/edk2/aarch64/QEMU_VARS.fd /usr/share/AAVMF/AAVMF_VARS.fd; do [ -f "$f" ] && { VARS_SRC=$f; break; }; done
+fi
+[ -f "$EFI" ] || { echo "run.sh: no UEFI firmware found (looked for edk2-aarch64's and AAVMF's); set QEMU_EFI and QEMU_VARS"; exit 1; }
+cp "$VARS_SRC" vars.fd 2>/dev/null
# the loader is the boot application of the little FAT volume QEMU makes of esp/:
# the one built here, or else the one that comes with the repository
L=../xnuloader/loader.efi; [ -f $L ] || L=../prebuilt/loader.efi
diff --git a/tools/make-kernel.sh b/tools/make-kernel.sh
new file mode 100644
index 0000000..02f6c62
--- /dev/null
+++ b/tools/make-kernel.sh
@@ -0,0 +1,54 @@
+#!/bin/bash
+# tools/make-kernel.sh [make args]: build the kernel, and judge the result by what failed, not by the exit status.
+#
+# tools/make-kernel.sh -j8 what `make kernel` runs
+# tools/make-kernel.sh --judge LOG judge a log of an earlier build (no build; this is how it is tested)
+#
+# xnu-build.sh has to be run with -k ("keep going"), and with it the build exits 2 on this configuration
+# even though the kernel is linked: the last steps of XNU's own build make the kernel extensions' symbol
+# sets (config/*.symbolset) and run check_all_exports, and they fail with "exported name not in import
+# list" for names this kernel does not have (DriverKit's IOService methods, Apple silicon's _t8110dart_*):
+# the machine configuration for an Android phone leaves those parts out. Nothing here loads a kext. But
+# an exit status that is always 2 says nothing, and ignoring it would let a real compile or link error
+# pass. So this accepts exactly the failures that belong to those symbol-set steps, and any other make
+# target that fails (a compile, a link) fails the build. Then the kernel file must exist.
+cd "$(dirname "$0")/.." || exit 1
+KERNEL=${KERNEL:-xnu-src/xnu-8796.121.2/BUILD/obj/DEVELOPMENT_ARM64_BCM2837/mach.development.bcm2837}
+
+# the make targets that failed, less the ones that are the symbol-set steps and the targets that only
+# stand above them (config_all and the recursion into config/, and the top-level `all`)
+unexpected_failures() {
+ grep -E '^make(\[[0-9]+\])?: \*\*\* ' "$1" |
+ grep -vE '\.symbolset\]|check_all_exports|config_all|_recurse_into_config|\[Makefile:[0-9]+: all\]'
+}
+
+judge() { # judge LOG: 0 if the kernel is built and only the symbol-set steps failed
+ local bad
+ bad=$(unexpected_failures "$1")
+ if [ -n "$bad" ]; then
+ echo "make-kernel: a build step failed that is not one of the kext symbol-set checks:"
+ echo "$bad" | cut -c1-200
+ return 1
+ fi
+ [ -f "$KERNEL" ] || { echo "make-kernel: no kernel was produced: $KERNEL"; return 1; }
+ return 0
+}
+
+if [ "$1" = --judge ]; then
+ judge "$2" && echo "make-kernel: the log shows a built kernel; only the kext symbol-set checks failed"
+ exit
+fi
+
+log=${KERNEL_LOG:-xnu-build.log}
+./xnu-build.sh -k "$@" 2>&1 | tee "$log"
+status=${PIPESTATUS[0]}
+if [ "$status" -eq 0 ]; then
+ echo "make-kernel: built: $KERNEL"
+ exit 0
+fi
+if judge "$log"; then
+ echo "make-kernel: built: $KERNEL"
+ echo "make-kernel: (exit status $status is the kext symbol-set checks failing, as they do on this configuration; see $0)"
+ exit 0
+fi
+exit 1