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

xnu: the kernel knows its version: sysctl kern.hakios.version and a line in the boot log

shimoku3df61da3260a
diff --git a/userland/src/hello.c b/userland/src/hello.c
index 64b97c8..256a276 100644
--- a/userland/src/hello.c
+++ b/userland/src/hello.c
@@ -19,11 +19,27 @@ syscall3(long num, long a, long b, long c)
return x0;
}
+/* the same with six arguments, for sysctlbyname(name, namelen, old, &oldlen, new, newlen) */
+static long
+syscall6(long num, long a, long b, long c, long d, long e, long f)
+{
+ register long x16 __asm__("x16") = num;
+ register long x0 __asm__("x0") = a;
+ register long x1 __asm__("x1") = b;
+ register long x2 __asm__("x2") = c;
+ register long x3 __asm__("x3") = d;
+ register long x4 __asm__("x4") = e;
+ register long x5 __asm__("x5") = f;
+ __asm__ volatile ("svc #0x80" : "+r" (x0) : "r" (x16), "r" (x1), "r" (x2), "r" (x3), "r" (x4), "r" (x5) : "memory", "cc");
+ return x0;
+}
+
#define SYS_exit 1
#define SYS_write 4
#define SYS_open 5
#define SYS_getpid 20
#define SYS_getuid 24
+#define SYS_sysctlbyname 274
#define O_RDWR 2
static unsigned
@@ -69,6 +85,20 @@ start(void)
put_number(fd, fd);
put(fd, ".\n");
+ /*
+ * The kernel's own version: sysctl kern.hakios.version, which the loader gave the kernel on its command line
+ * (hakios_kver, from the payload's kernel.ver). The same string the banner shows under hello hackios-kernel!
+ */
+ {
+ char version[96];
+ u64 size = sizeof(version);
+ long failed = syscall6(SYS_sysctlbyname, (long)"kern.hakios.version", 19, (long)version, (long)&size, 0, 0);
+
+ put(fd, "kernel.ver=");
+ put(fd, failed ? "(sysctl kern.hakios.version not available)" : version);
+ put(fd, "\n");
+ }
+
/* Process 1 is not supposed to exit; doing so panics the kernel, which ends the test run. */
syscall3(SYS_exit, 0, 0, 0);
for (;;) {
diff --git a/xnu/files/bsd/dev/hakios.version.c b/xnu/files/bsd/dev/hakios.version.c
new file mode 100644
index 0000000..a07dd4a
--- /dev/null
+++ b/xnu/files/bsd/dev/hakios.version.c
@@ -0,0 +1,45 @@
+/*
+ * The version of this kernel: sysctl kern.hakios.version, and a line in the boot log.
+ *
+ * Where the string comes from. VERSION (at the top of the repository) holds VERSION.PATCHLEVEL.SUBLEVEL and
+ * EXTRAVERSION, in the form of the Makefile of Linux; tools/kernel-version.py makes the whole string from it,
+ * from the Darwin the kernel is built on, from CONFIG_SOC of the active configuration and from git
+ * (2.1.13-hakios-darwin22.5-sc7280-g1a2b3c4d). xnuloader/mkpayload.py puts it into the payload as the entry
+ * kernel.ver, the loader prints it under its banner ("kernel.ver=") and hands it to this kernel on its
+ * command line as hakios_kver.
+ *
+ * Why the kernel is told and does not carry it itself: the kernel is built from Apple's open xnu plus this port,
+ * once, and its Mach-O would have to be rebuilt for a version that is only the commit or the SoC of the
+ * configuration; the payload is made in a second, so one kernel binary can be shipped with the version of the
+ * release that carries it. The cost is that a kernel started some other way (no loader, no hakios_kver) says
+ * "unknown": it does not guess.
+ *
+ * Android's kernels give theirs in uname; Darwin's "Darwin Kernel Version 22.5.0: ..." is Apple's and stays.
+ */
+#include <libkern/libkern.h>
+#include <pexpert/pexpert.h>
+#include <sys/hakios.version.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+
+SYSCTL_DECL(_kern_hakios);
+
+#define HAKIOS_VERSION_MAX 64
+
+/* "unknown" until the loader says; one string, written once at boot and then only read */
+static char hakios_version_string[HAKIOS_VERSION_MAX] = "unknown";
+
+SYSCTL_STRING(_kern_hakios, OID_AUTO, version, CTLFLAG_RD | CTLFLAG_LOCKED, hakios_version_string, 0,
+ "the version of this kernel, as the loader printed it under its banner (kernel.ver=)");
+
+void
+hakios_version_init(void)
+{
+ char value[HAKIOS_VERSION_MAX];
+
+ /* the loader leaves it out of the command line when it does not fit: the string stays "unknown" then */
+ if (PE_parse_boot_argn("hakios_kver", value, sizeof(value)) && value[0] != '\0') {
+ strlcpy(hakios_version_string, value, sizeof(hakios_version_string));
+ }
+ printf("hakios-kernel: version %s\n", hakios_version_string);
+}
diff --git a/xnu/files/bsd/sys/hakios.version.h b/xnu/files/bsd/sys/hakios.version.h
new file mode 100644
index 0000000..d10d060
--- /dev/null
+++ b/xnu/files/bsd/sys/hakios.version.h
@@ -0,0 +1,12 @@
+/*
+ * The version of this kernel (bsd/dev/hakios.version.c): sysctl kern.hakios.version and a line of the boot log.
+ */
+#ifndef _SYS_HAKIOS_VERSION_H_
+#define _SYS_HAKIOS_VERSION_H_
+
+#ifdef KERNEL
+/* Read the boot-arg hakios_kver, keep it as kern.hakios.version and say it in the log. Called from bsd_init(). */
+void hakios_version_init(void);
+#endif /* KERNEL */
+
+#endif /* _SYS_HAKIOS_VERSION_H_ */
diff --git a/xnu/hakios.patch b/xnu/hakios.patch
index 769d5f3..dfe6dc2 100644
--- a/xnu/hakios.patch
+++ b/xnu/hakios.patch
@@ -62,10 +62,10 @@ index a4d25040f38f..aad72993d139 100644
kern_debug.o_INCFLAGS += -I$(SRCROOT)/osfmk
diff --git a/bsd/conf/files b/bsd/conf/files
-index 44274016a4cf..be702e28890d 100644
+index 44274016a4cf..711845d36f49 100644
--- a/bsd/conf/files
+++ b/bsd/conf/files
-@@ -109,6 +109,41 @@ bsd/dev/munge.c optional config_requires_u32_munging
+@@ -109,6 +109,42 @@ bsd/dev/munge.c optional config_requires_u32_munging
bsd/dev/unix_startup.c standard
bsd/dev/monotonic.c optional monotonic
@@ -87,6 +87,7 @@ index 44274016a4cf..be702e28890d 100644
+bsd/dev/hakios.virtio_sound.c standard
+bsd/dev/hakios.speaker.c standard
+bsd/dev/hakios.guard.c standard
++bsd/dev/hakios.version.c standard
+bsd/dev/hakios.haptic.c standard
+bsd/dev/hakios.torch.c standard
+bsd/dev/hakios.camera.c standard
@@ -107,7 +108,7 @@ index 44274016a4cf..be702e28890d 100644
bsd/dev/dev_perfmon.c optional config_perfmon
bsd/libkern/crc32.c standard
-@@ -155,6 +190,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
+@@ -155,6 +191,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
bsd/miscfs/mockfs/mockfs_fsnode.c optional mockfs
bsd/miscfs/mockfs/mockfs_vfsops.c optional mockfs
bsd/miscfs/mockfs/mockfs_vnops.c optional mockfs
@@ -122,7 +123,7 @@ index 44274016a4cf..be702e28890d 100644
bsd/miscfs/routefs/routefs_ops.c optional routefs
-@@ -361,6 +404,7 @@ bsd/security/audit/audit_syscalls.c standard
+@@ -361,6 +405,7 @@ bsd/security/audit/audit_syscalls.c standard
bsd/security/audit/audit_worker.c optional config_audit
bsd/pthread/pthread_shims.c standard
@@ -130,7 +131,7 @@ index 44274016a4cf..be702e28890d 100644
bsd/pthread/pthread_priority.c standard
bsd/pthread/pthread_workqueue.c standard
-@@ -383,6 +427,7 @@ bsd/kern/kern_debug.c standard
+@@ -383,6 +428,7 @@ bsd/kern/kern_debug.c standard
bsd/kern/kern_symfile.c standard
bsd/kern/kern_descrip.c standard
bsd/kern/kern_guarded.c standard
@@ -357,10 +358,10 @@ index 3e6bffa8c48e..43cffb98bdcc 100644
mdevremoveall(void)
{
diff --git a/bsd/kern/bsd_init.c b/bsd/kern/bsd_init.c
-index 4df41ba1f61b..4a6e0ccfc590 100644
+index 4df41ba1f61b..b5a79a02a3f5 100644
--- a/bsd/kern/bsd_init.c
+++ b/bsd/kern/bsd_init.c
-@@ -147,6 +147,13 @@
+@@ -147,6 +147,14 @@
#include <net/if_ether.h> /* for ether_family_init() */
#include <net/if_gif.h> /* for gif_init() */
#include <miscfs/devfs/devfsdefs.h> /* for devfs_kernel_mount() */
@@ -368,13 +369,14 @@ index 4df41ba1f61b..4a6e0ccfc590 100644
+#include <sys/hakios.audio.h>
+#include <sys/hakios.virtio_blk.h>
+#include <sys/hakios.sparse.h>
++#include <sys/hakios.version.h>
+#if APFS
+#include <sys/hakios.apfs_check.h>
+#endif
#include <vm/vm_kern.h> /* for kmem_suballoc() */
#include <sys/proc_uuid_policy.h> /* proc_uuid_policy_init() */
#include <netinet/flow_divert.h> /* flow_divert_init() */
-@@ -490,7 +497,7 @@ bsd_init(void)
+@@ -490,7 +498,7 @@ bsd_init(void)
platform_stall_panic_or_spin(PLATFORM_STALL_XNU_LOCATION_BSD_INIT);
#endif
@@ -383,10 +385,11 @@ index 4df41ba1f61b..4a6e0ccfc590 100644
#if DEBUG_BSDINIT
#define bsd_init_kprintf(x, ...) kprintf("bsd_init: " x, ## __VA_ARGS__)
-@@ -976,6 +983,14 @@ bsd_init(void)
+@@ -976,6 +984,15 @@ bsd_init(void)
bsd_init_kprintf("calling devfs_kernel_mount\n");
devfs_kernel_mount(mounthere);
++ hakios_version_init(); /* kern.hakios.version, and the version in the boot log */
+ hakios_input_init(); /* /dev/hakios-touch */
+ hakios_audio_init(); /* /dev/hakios-audio */
+ hakios_virtio_blk_init(); /* /dev/rvblk0, a disk image of the host (QEMU) */