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

air: carry the generated suite into the image, so the Adreno can be asked

vcvkk49383047231a
diff --git a/tools/air/intrincheck.py b/tools/air/intrincheck.py
index feb2669..a15fcb6 100755
--- a/tools/air/intrincheck.py
+++ b/tools/air/intrincheck.py
@@ -886,7 +886,20 @@ RUNNER = """#!/bin/sh
# Parsing happens on the host (intrincheck.py --check); this stays dumb on
# purpose, because what is here may be a Darwin userland with no sed or paste.
D=`dirname "$0"`
-AIR_RUN=${AIR_RUN:-$D/air_run}
+# Where air_run is: the environment first, then one beside the modules (that is
+# a host run, which unpacks both into one directory), then the device's own
+# /redwood/bin. The fallback is what makes this runnable from the phone's job
+# list, which splits a line on blanks and cannot set a variable at all
+# (userland/src/init.c, run_command) -- so the job line is "/bin/sh .../run.sh"
+# and nothing can be passed to it.
+if [ -z "$AIR_RUN" ]; then
+ if [ -x "$D/air_run" ]; then
+ AIR_RUN=$D/air_run
+ else
+ AIR_RUN=/redwood/bin/air_run
+ fi
+fi
+echo "runner: $AIR_RUN"
for bc in "$D"/*.bc; do
id=${bc##*/}
id=${id%.bc}
@@ -980,9 +993,29 @@ def parse_runner_log(path):
screen = None
ident = None
block = []
+ lines = [l.rstrip("\n") for l in open(path, errors="replace")]
+ # A log QEMU produced has every line twice: qemu/run-ios.sh writes the
+ # console to two places that both end up here, and the two copies are
+ # adjacent. Undoing it needs care, because a suite legitimately prints the
+ # same four numbers for two different cases, so no per-line rule is safe.
+ # The test is therefore global: the whole file must consist of adjacent
+ # equal pairs. That is a property a real log does not have by accident, and
+ # the phone's own log (xl run) does not have at all -- it is not doubled.
+ if len(lines) >= 2 and len(lines) % 2 == 0 and \
+ all(lines[i] == lines[i + 1] for i in range(0, len(lines), 2)):
+ lines = lines[0::2]
+ print("intrincheck: %s had every line twice, as a QEMU console log does; "
+ "read every second one" % os.path.basename(path))
def finish():
if ident is None:
return
+ # An id already answered is not replaced. The console log of a phone or
+ # QEMU run repeats lines -- qemu/run-ios.sh prints every one twice --
+ # and the second copy of a marker opens a block with nothing after it,
+ # which would turn an answer into a refusal. A refusal therefore never
+ # overwrites an answer, and the first answer stands.
+ if out.get(ident, ("",))[0] == "ok":
+ return
text = "\n".join(block)
i = text.find("came back as")
if i < 0:
@@ -1002,7 +1035,7 @@ def parse_runner_log(path):
vals.append(t)
out[ident] = ("ok", vals)
- for line in open(path, errors="replace"):
+ for line in lines:
if screen is None and " on " in line and line.startswith("air_run: "):
screen = "softpipe" if "softpipe" in line else line.split(" on ")[-1].strip()
if line.startswith("=== "):
@@ -1010,7 +1043,7 @@ def parse_runner_log(path):
ident = line[4:].strip()
block = []
else:
- block.append(line.rstrip("\n"))
+ block.append(line)
finish()
return out, screen
diff --git a/userland/build.sh b/userland/build.sh
index 44ba84f..af2d2ab 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -19,6 +19,12 @@
# init hands the list to the kernel (etc/inject.conf -> sysctl kern.exec_inject)
# EXTRA="..." more programs for /redwood/bin (files on this machine): one-off
# diagnostics, which a file given as RC_JOBS can then run
+# EXTRA_SHARE="..." directories on this machine to copy whole into /redwood/share,
+# each under its own name: the data a one-off diagnostic reads, when it
+# needs more than a program (tools/air/intrincheck.py --emit writes such
+# a directory). This goes into the boot volume, which every recipe
+# carries and which the flash recipe puts in RAM, so its size is paid
+# for at every boot -- a few megabytes, not more
# RC_JOBS=<file> what the job com.redwood.demo runs: rc.jobs (default), or
# rc.jobs.unlock, which also swipes the lock screen away (for the phone,
# where nothing else can touch the screen yet)
@@ -135,6 +141,15 @@ cp com.redwood.varstore.plist root/LaunchDaemons/
echo "Darwin on redwood: image built $(date '+%Y-%m-%d %H:%M')" > root/etc/motd
# 300000 bytes, byte i = (i*31 + (i>>8)) & 0xff; init verifies it through read() and mmap()
python3 -c "import sys; sys.stdout.buffer.write(bytes((i*31 + (i>>8)) & 0xff for i in range(300000)))" > root/share/pattern.bin
+# EXTRA_SHARE: whole directories of data for a one-off diagnostic, each under its own name. Here and
+# not in root/bin, because what goes in is not a program: a generated test suite is modules and the
+# bytes they read, and the job list runs it by its own run.sh. -L so that a directory assembled out
+# of symbolic links arrives as files, since the image is made from this tree and not from the links'
+# targets.
+for d in $EXTRA_SHARE; do
+ [ -d "$d" ] || { echo "build.sh: EXTRA_SHARE: $d is not a directory"; exit 1; }
+ cp -rL "$d" root/share/
+done
if [ -d $IOS/rootfs/usr/lib ]; then
if [ -z "$PREBUILT" ]; then
# ordinary dynamically linked programs of our own, built against the iOS SDK
diff --git a/userland/rc.jobs.airops b/userland/rc.jobs.airops
new file mode 100644
index 0000000..98c78ec
--- /dev/null
+++ b/userland/rc.jobs.airops
@@ -0,0 +1,45 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+#
+# The first air.* operation executed on this GPU (RC_JOBS=rc.jobs.airops). Every number in
+# docs/air.md section 4 about the arithmetic -- 45607 folded cases, 2586 run ones -- was computed
+# on an x86 host by NIR's constant folder and by softpipe. Neither is an Adreno. An opcode whose
+# Adreno instruction disagrees with what those two compute is invisible to all of it, and the
+# seven shaders that have run on the phone call no air.* function at all. This job is that gap.
+#
+# The first line is the shader M1 is stated against. It is here for two reasons that the suite
+# below cannot cover for itself: it takes no input buffer, so it can be an ordinary job line, and
+# --disasm makes ir3 print the Adreno instructions it generated. The screen's name proves
+# fd_screen_create won; only the disassembly proves the shader went through ir3 and not some
+# later software path, and the four floats look identical either way. Expect
+# 2.10170319e-18 0.178894654 1.48687698e+16 1.21394141e+33
+/redwood/bin/air_run --disasm /System/Library/Extensions/AppleMetalGLRenderer.bundle/default.metallib clearFragment01
+# Then the generated suite: 100 modules over 78 air.* declarations, written by
+# tools/air/intrincheck.py --emit and carried in by EXTRA_SHARE. Each module calls one operation
+# on arguments it reads out of a buffer -- never on immediates, because a shader whose arguments
+# are constants is answered by the compiler and the GPU is never asked.
+#
+# Nothing here says what any of them should return, and that is deliberate: the expected values
+# stay on the host in manifest.json, which is not carried, and `intrincheck.py --check DIR LOG`
+# judges this log afterwards. So the device cannot be made to agree by reading the answer, and
+# this file needs no updating when the suite is regenerated.
+#
+# Two of those modules again, run directly, before the shell is asked for anything. They are here
+# so that this run cannot come back with nothing: a job line cannot set AIR_RUN_BUFFER, so these
+# two read air_run's own pattern buffer instead ((i * 7 + 13) & 0xff) -- which the host can read
+# too, so their numbers are comparable without the manifest and without a shell. If the loop below
+# fails for any reason at all, these two lines still answer the question this whole job exists for:
+# an air.* operation compiled by ir3 and executed on the Adreno.
+#
+# What softpipe computes for them on an x86 host, which is what the Adreno has to match:
+# 0077 air.fast_sqrt.f32 1.44972523e-09 2.15926367e+15 0 0
+# 0061 air.dot.v4f32 -inf -inf 0 0
+# (the pattern bytes read as floats are extreme, so the dot product overflows; -inf is the right
+# answer to the question asked, not a failure)
+/redwood/bin/air_run /redwood/share/airops/0077.bc
+/redwood/bin/air_run /redwood/share/airops/0061.bc
+# A shell, and not 100 job lines: init's run_command splits a line on blanks and cannot set a
+# variable (src/init.c), while every module needs AIR_RUN_BUFFER pointing at its own bytes. The
+# shell is iOS's own /bin/sh -- with the root on the flash this volume is the iOS one, and
+# rc.jobs.jb and rc.jobs.sileo already run it there. run.sh finds air_run in /redwood/bin when
+# there is none beside the modules.
+/bin/sh /redwood/share/airops/run.sh