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

Metal: who is shown the device is a level of metal= (1: metal_test only; 2: all but the window server; 3: all)

shimoku9a374441fc75
diff --git a/docs/metal-plan.md b/docs/metal-plan.md
index dd4f192..2c57764 100644
--- a/docs/metal-plan.md
+++ b/docs/metal-plan.md
@@ -68,6 +68,13 @@ Run in a loop of rebuild-image, boot, read the log; each pass gave the next requ
and will then ask the device for a library from it, which is where the shader compiler (part A) is first needed for real.
Where CoreAnimation takes that URL from is not known yet; the log shows the device was asked for nothing before it.
+7. **Who is shown the device**, then, is a boot-arg level (the plug-in reads `sysctl kern.bootargs`): `metal=1` only the program
+ `metal_test`, `metal=2` every process but the window server, `metal=3` every process. With the window server shown the device
+ it stops on the missing metallib (QuartzCore.framework has a `default.pipelinelib` that names a `default.metallib` which is
+ not in this image), so it cannot run on Metal here, and it has to be left on its OpenGL ES path. With `mediaserverd` shown it, it
+ ends and starts again. At `metal=1` the window server, SpringBoard and mediaserverd were each logged as not shown the device
+ and none of them ended in a QEMU run (the result of `metal_test` itself is not in this file yet).
+
So the road from here is the library (step 4 of the order below: `newLibraryWithURL:` / `…Data:` through the AIR compiler), then
buffers and textures with real memory, and the command encoders; none of that can be tried without the GPU.
diff --git a/userland/src/HakiosMetal.m b/userland/src/HakiosMetal.m
index 4297390..80a8a46 100644
--- a/userland/src/HakiosMetal.m
+++ b/userland/src/HakiosMetal.m
@@ -25,6 +25,10 @@
#import <Metal/Metal.h>
#import <objc/message.h>
#import <objc/runtime.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/sysctl.h>
+#include <unistd.h>
static Class gBase; /* Metal's private _MTLDevice */
static unsigned int gPort; /* the IOAcceleratorES service this device was made for */
@@ -309,11 +313,54 @@ registerQueueClass(void)
say(@"the class HakiosMetalQueue is registered (a subclass of _MTLCommandQueue)");
}
+/*
+ * Whether this process may be shown the device, by the level of boot-arg metal (read from sysctl kern.bootargs):
+ *
+ * metal=1 only the program metal_test (userland/src/metal_test.m): the safe level, for seeing what Metal does with the device
+ * metal=2 every process but backboardd
+ * metal=3 every process, backboardd too, to find out what the window server asks
+ *
+ * Why not simply everyone. CoreAnimation's window server tries Metal FIRST and goes to its OpenGL ES path (which works on this port)
+ * only when no Metal device exists; with ours it takes Metal and stops on "metallib_url != nil": its own shader library,
+ * QuartzCore.framework/default.metallib, is not in this system image (the framework has only a default.pipelinelib that names
+ * it), so the window server cannot run on Metal here at all. And mediaserverd, given a device that cannot do anything, ends and
+ * is started again. A device that is shown to a process that cannot use it costs that process, so it is shown to as few as the
+ * experiment needs.
+ */
+static BOOL
+shownToThisProcess(void)
+{
+ char args[1024] = "";
+ size_t size = sizeof(args) - 1;
+ const char * name = getprogname();
+ int level = 1;
+
+ if (sysctlbyname("kern.bootargs", args, &size, NULL, 0) == 0) {
+ const char * at = strstr(args, "metal=");
+
+ if (at != NULL) {
+ level = atoi(at + 6);
+ }
+ }
+ if (name == NULL) {
+ return NO;
+ }
+ if (level >= 3 || (level == 2 && strcmp(name, "backboardd") != 0) || (level == 1 && strcmp(name, "metal_test") == 0)) {
+ return YES;
+ }
+ say([NSString stringWithFormat:@"%s is not shown the device (metal=%d; 1 is metal_test only, 2 all but backboardd, 3 all)", name, level]);
+ return NO;
+}
+
__attribute__((constructor)) static void
registerDevice(void)
{
Class device;
+ if (!shownToThisProcess()) {
+ return;
+ }
+
gBase = objc_getClass("_MTLDevice");
if (gBase == Nil) {
say(@"this system has no _MTLDevice: no device class is registered");