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_run is built for the phone and in the image: the slot can now be spent running

vcvkk19aa7a9cd4f4
diff --git a/docs/air.md b/docs/air.md
index 7171472..17ba2ea 100644
--- a/docs/air.md
+++ b/docs/air.md
@@ -442,11 +442,24 @@ the Mesa already on the phone rather than being built from nothing:
screen (`sw_screen_create(null_sw_create())`); on the phone it wants the screen
`osmesa_create_gpu_screen()` makes, and **no line below that changes**.
-What is left to do on the phone is therefore small and known: export that screen from the port's
-Mesa (one function, beside the `osmesa_surface_resource` the patch already exports), cross-build
-`air_run.c` with `userland/build.sh`, and run it. What is *not* known until it is tried is
-whether the kernel's GPU driver and the submit path behave for a draw that does not come from
-CoreAnimation; the GLES path is the only consumer of that Mesa today.
+**That is now built and in the image, and no export was needed.** `air_run` is built *inside* the
+Mesa cross build (`AIR=1 tools/mk-mesa.sh`), so it links Gallium's own static libraries and opens
+the Adreno itself — `drmOpenWithType("msm", …)` through `tools/mesa/redwood-drm`, then
+`fd_drm_screen_create_renderonly()`, exactly as `osmesa_create_gpu_screen()` does. It needs
+nothing exported from `libOSMesa` and does not load it: a 9.8 MB arm64 Mach-O against libSystem,
+libz and IOKit alone. `userland/build.sh` puts it in `/redwood/bin`, and `rc.jobs.air` runs it.
+
+The same source takes the GPU where there is one and falls back to software where there is not,
+so it is the binary tested on this machine — not a variant of it. Its first line says which it
+got (`air_run: clearFragment01 on softpipe` here), and that line must be read before any number
+below it is believed. `AIR_RUN_SOFTWARE=1` forces software even on the phone, for comparing the
+two on the same device.
+
+What is *not* known until it is tried is whether the kernel's GPU driver and the submit path
+behave for a draw that does not come from CoreAnimation: the GLES path is that driver's only
+consumer today. `rc.jobs.air` asks with the GPU idle; `rc.jobs.air2` asks again after SpringBoard
+is up and holding it. A refusal is a finding the whole port needs, not just this frontend, so
+either answer is worth the run.
## 6. Running it
diff --git a/tools/air/mk-host.sh b/tools/air/mk-host.sh
index 70725cd..acc41ee 100755
--- a/tools/air/mk-host.sh
+++ b/tools/air/mk-host.sh
@@ -70,11 +70,11 @@ if [ ! -d "$SRC" ] || [ "$(cat "$SRC/.air-patches" 2>/dev/null)" != "$PATCHES" ]
open(p, 'w').write(s)
PY
- # the frontend itself, as a link: no copy to forget to refresh
+ # the frontend itself, as a link: no copy to forget to refresh. The one
+ # line of meson that reaches it comes from tools/mesa/air-frontend.patch,
+ # applied above, so this build and the cross build get it the same way.
rm -rf "$SRC/src/compiler/air"
ln -s "$ROOT/tools/mesa/air" "$SRC/src/compiler/air"
- grep -q "subdir('air')" "$SRC/src/compiler/meson.build" ||
- sed -i "s|^subdir('spirv')|subdir('spirv')\nsubdir('air')|" "$SRC/src/compiler/meson.build"
echo "$PATCHES" > "$SRC/.air-patches"
fi
diff --git a/tools/mesa/air-frontend.patch b/tools/mesa/air-frontend.patch
new file mode 100644
index 0000000..eedf8b4
--- /dev/null
+++ b/tools/mesa/air-frontend.patch
@@ -0,0 +1,18 @@
+Build the AIR frontend as part of Mesa.
+
+src/compiler/air is this repository's tools/mesa/air: the frontend that reads Apple's shader
+bitcode and produces NIR (docs/air.md). tools/air/mk-host.sh links it there for the build on
+this machine, tools/mk-mesa.sh copies it there for the cross build; this patch is only the one
+line of meson that reaches it, so that both builds get it the same way and neither needs a sed.
+
+--- a/src/compiler/meson.build
++++ b/src/compiler/meson.build
+@@ -63,6 +63,8 @@
+
+ subdir('spirv')
+
++subdir('air')
++
+ if with_clc
+ subdir('clc')
+ endif
diff --git a/tools/mesa/air-ir3.patch b/tools/mesa/air-ir3.patch
index 3a113f2..823d288 100644
--- a/tools/mesa/air-ir3.patch
+++ b/tools/mesa/air-ir3.patch
@@ -193,7 +193,7 @@ dimensions -- and hides on the rest, which is presumably why it survived.
idep_vtn,
idep_mesautil,
],
-@@ -318,6 +319,21 @@
+@@ -318,6 +319,22 @@
install : false,
)
@@ -204,10 +204,11 @@ dimensions -- and hides on the rest, which is presumably why it survived.
+ 'air_run',
+ files('../../../compiler/air/air_run.c'),
+ include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium,
-+ inc_gallium_aux, inc_gallium_winsys_sw,
++ inc_gallium_aux, inc_gallium_winsys_sw, inc_gallium_winsys,
+ include_directories('../../drivers')],
-+ dependencies : [dep_m, idep_air, idep_mesautil, driver_softpipe],
-+ link_with : [libgallium, libgalliumvl_stub, libws_null],
++ dependencies : [dep_m, idep_air, idep_mesautil, driver_softpipe, driver_freedreno,
++ dep_libdrm],
++ link_with : [libgallium, libgalliumvl_stub, libws_null, libfreedrenowinsys],
+ build_by_default : false,
+ install : false,
+)
diff --git a/tools/mesa/air/air_run.c b/tools/mesa/air/air_run.c
index bbb7596..93faea1 100644
--- a/tools/mesa/air/air_run.c
+++ b/tools/mesa/air/air_run.c
@@ -37,14 +37,23 @@
#include "pipe/p_state.h"
#include "target-helpers/inline_sw_helper.h"
#include "null/null_sw_winsys.h"
+#ifdef GALLIUM_FREEDRENO
+#include "freedreno/drm/freedreno_drm_public.h"
+#include <xf86drm.h>
+#endif
#include "util/format/u_format.h"
#include "util/u_inlines.h"
+#include "util/u_debug.h"
#include "util/u_memory.h"
#ifdef __APPLE__
#include <mach-o/dyld.h>
#include <signal.h>
-#include <ucontext.h>
+/* <sys/ucontext.h>, not <ucontext.h>: the latter refuses to be included
+ * without _XOPEN_SOURCE on the iOS SDK, and userland/src/inject.c takes the
+ * same route for the same reason.
+ */
+#include <sys/ucontext.h>
#endif
#define WIDTH 4
@@ -213,13 +222,27 @@ main(int argc, char **argv)
name = lib.functions[pick].name;
}
- /* The screen. Here it is softpipe, drawing into memory with no GPU at all,
- * which is the point: the whole path can be checked on this machine. On the
- * phone it is the freedreno screen that the port's Mesa already makes
- * (osmesa_create_gpu_screen in tools/mesa/freedreno-darwin.patch), and
- * nothing below this line changes.
+ /* The screen: the GPU's where the kernel has one to drive, softpipe
+ * otherwise. The same source therefore runs on this machine (no msm
+ * device, so it falls back and draws in software) and on the phone, where
+ * it opens the Adreno exactly as the port's own Mesa does
+ * (osmesa_create_gpu_screen in tools/mesa/freedreno-darwin.patch).
+ * AIR_RUN_SOFTWARE=1 forces software even where a GPU is present.
*/
- struct pipe_screen *screen = sw_screen_create(null_sw_create());
+ struct pipe_screen *screen = NULL;
+#ifdef GALLIUM_FREEDRENO
+ if (!debug_get_bool_option("AIR_RUN_SOFTWARE", false)) {
+ int fd = drmOpenWithType("msm", NULL, DRM_NODE_RENDER);
+ if (fd >= 0) {
+ static struct pipe_screen_config config;
+ screen = fd_drm_screen_create_renderonly(fd, NULL, &config);
+ if (!screen)
+ fprintf(stderr, "air_run: the msm device opened but gave no screen\n");
+ }
+ }
+#endif
+ if (!screen)
+ screen = sw_screen_create(null_sw_create());
if (!screen) {
fprintf(stderr, "air_run: no gallium screen\n");
return 1;
diff --git a/tools/mk-mesa.sh b/tools/mk-mesa.sh
index 61b1612..4e15eaf 100755
--- a/tools/mk-mesa.sh
+++ b/tools/mk-mesa.sh
@@ -46,6 +46,11 @@ fi
# ours, kept in the repository: always the current ones
rm -rf mesa-$VERSION/src/redwood-drm
cp -r "$ROOT/tools/mesa/redwood-drm" mesa-$VERSION/src/redwood-drm
+# the AIR frontend (docs/air.md): the Metal runtime will call it in this
+# library's own process, and tools/mesa/air-frontend.patch is the meson that
+# reaches it. Copied rather than linked, because this tree is cross-built.
+rm -rf mesa-$VERSION/src/compiler/air
+cp -r "$ROOT/tools/mesa/air" mesa-$VERSION/src/compiler/air
if [ ! -x venv/bin/python3 ]; then
python3 -m venv venv
venv/bin/pip -q install mako pyyaml packaging
@@ -83,6 +88,14 @@ BUILD=build-${DRIVERS//,/-}
-Dlibunwind=disabled -Dglvnd=disabled -Dvideo-codecs= -Dtools= -Dgallium-vdpau=disabled -Dgallium-va=disabled \
-Dgallium-xa=disabled -Dlmsensors=disabled -Dbuild-tests=false > meson-setup.log
ninja -C $BUILD > ninja.log
+# air_run draws with a translated Metal shader and reads the pixels back; it is
+# the only way to find out whether the Adreno takes work that did not come from
+# the window server. Built only when asked, since it is a test program.
+if [ "${AIR:-}" = 1 ]; then
+ ninja -C $BUILD src/gallium/drivers/freedreno/air_run >> ninja.log
+ cp $BUILD/src/gallium/drivers/freedreno/air_run out/air_run
+ echo "mk-mesa.sh: $B/out/air_run ($(stat -c %s out/air_run) bytes)"
+fi
llvm-strip -x -o out/libOSMesa.8.dylib $BUILD/src/gallium/targets/osmesa/libOSMesa.8.dylib
install_name_tool -id /redwood/lib/libOSMesa.8.dylib out/libOSMesa.8.dylib
diff --git a/userland/build.sh b/userland/build.sh
index 2837202..a47401a 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -174,6 +174,12 @@ if [ -d $IOS/rootfs/usr/lib ]; then
src/eagl.c src/gl_osmesa.c
$DYN -framework CoreFoundation -framework IOKit $M/libOSMesa.8.dylib -o obj/gl_test src/gl_test.c
cp $M/libOSMesa.8.dylib obj/
+ # air_run draws with a Metal shader translated by the AIR frontend
+ # and reads the pixels back (docs/air.md). It is built inside the
+ # Mesa build, because it needs Gallium itself and not the gl*
+ # entry points this library exports; AIR=1 tools/mk-mesa.sh makes
+ # it, and it is copied in when it is there.
+ [ -f $M/air_run ] && cp $M/air_run obj/air_run
fi
fi
cp obj/hello_dyn obj/svc_test obj/surf_test obj/rwtouch obj/mg_test root/bin/
@@ -210,6 +216,7 @@ if [ -d $IOS/rootfs/usr/lib ]; then
if [ -n "$GL" ]; then
cp obj/libOSMesa.8.dylib root/lib/
cp obj/gl_test root/bin/
+ [ -f obj/air_run ] && cp obj/air_run root/bin/
[ "$GL" = test ] || cp obj/libredwood-gl.dylib root/lib/
fi
# a program is not started with a library that is not there: it would not start at all
diff --git a/userland/rc.jobs.air b/userland/rc.jobs.air
new file mode 100644
index 0000000..8cd5478
--- /dev/null
+++ b/userland/rc.jobs.air
@@ -0,0 +1,21 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+#
+# The first draw on this GPU by a shader Apple compiled for Metal (RC_JOBS=rc.jobs.air).
+# /redwood/bin/air_run translates one function of a .metallib with the AIR frontend
+# (docs/air.md), hands the NIR to Gallium as PIPE_SHADER_IR_NIR, draws one quad and prints
+# the pixel it reads back. With the Adreno present it opens the msm device and ir3 compiles
+# the shader; without one it falls back to softpipe and says so in its first line, so the
+# line "air_run: <name> on FD643" (or similar) is what tells the two apart -- read it before
+# believing any number below it.
+#
+# clearFragment01 is the shader M1 is stated against: four LLVM instructions, no air.*
+# intrinsic, its colour read straight out of the constant buffer at index 14. The buffer is
+# filled with (i * 7 + 13) & 0xff, so the four floats expected are the ones LLVM itself
+# computes on a computer and softpipe draws there:
+# 2.10170319e-18 0.178894654 1.48687698e+16 1.21394141e+33
+# Anything else from this run is the finding, whatever it is.
+#
+# No wait for SpringBoard: this draws for itself and wants the GPU idle rather than busy with
+# the window server. With gpu=1 the window server has it too, which is worth knowing about on
+# purpose -- but the first run should be the simple one.
+/redwood/bin/air_run /System/Library/Extensions/AppleMetalGLRenderer.bundle/default.metallib clearFragment01
diff --git a/userland/rc.jobs.air2 b/userland/rc.jobs.air2
new file mode 100644
index 0000000..ddcb4ba
--- /dev/null
+++ b/userland/rc.jobs.air2
@@ -0,0 +1,6 @@
+# As rc.jobs.air, but after SpringBoard is up and drawing: does the kernel's GPU driver take
+# work from a second process at all? That question is owed to the whole port, not to the AIR
+# frontend, and a refusal here is as much of a result as a pixel.
+/redwood/bin/sound_test springboard
+/redwood/bin/rwtouch wait 25
+/redwood/bin/air_run /System/Library/Extensions/AppleMetalGLRenderer.bundle/default.metallib clearFragment01