Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

M2a: bind to CPU 0 and give XNU that CPU's MPIDR (ml_get_cpu_number_local)

Heyang Zhouaf023ab2f548
diff --git a/fki-ko/fki.h b/fki-ko/fki.h
index 217a6ad..99cb768 100644
--- a/fki-ko/fki.h
+++ b/fki-ko/fki.h
@@ -52,6 +52,8 @@ void fki_pac_swap(const struct fki_pac_keys *load, struct fki_pac_keys *save);
fki_u64 fki_counter_frequency(void);
fki_u64 fki_counter(void);
void fki_random(void *buf, unsigned long len);
+/* Keep the calling task on one CPU (the hosted kernel sees that CPU's MPIDR). */
+int fki_bind_cpu(int cpu);
#ifdef __cplusplus
}
#endif
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.c
index 82b1555..00b0e31 100644
--- a/fki-ko/fki_mod.c
+++ b/fki-ko/fki_mod.c
@@ -16,6 +16,8 @@
#include <linux/printk.h>
#include <linux/random.h>
#include <linux/slab.h>
+#include <linux/sched.h>
+#include <linux/cpumask.h>
#include <linux/vmalloc.h>
#include <asm/arch_timer.h>
#include <asm/sysreg.h>
@@ -181,5 +183,13 @@ void fki_random(void *buf, unsigned long len)
}
EXPORT_SYMBOL(fki_random);
+int fki_bind_cpu(int cpu)
+{
+ if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu))
+ return -EINVAL;
+ return set_cpus_allowed_ptr(current, cpumask_of(cpu));
+}
+EXPORT_SYMBOL(fki_bind_cpu);
+
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Foreign kernel interface v0 (out-of-tree form for stock kernels)");
diff --git a/linux/drivers/fki/fki.c b/linux/drivers/fki/fki.c
index 49444b5..fb7bd7f 100644
--- a/linux/drivers/fki/fki.c
+++ b/linux/drivers/fki/fki.c
@@ -16,6 +16,8 @@
#include <linux/printk.h>
#include <linux/random.h>
#include <linux/slab.h>
+#include <linux/sched.h>
+#include <linux/cpumask.h>
#include <linux/vmalloc.h>
#include <asm/arch_timer.h>
#include <asm/sysreg.h>
@@ -158,3 +160,11 @@ void fki_random(void *buf, unsigned long len)
get_random_bytes(buf, len);
}
EXPORT_SYMBOL(fki_random);
+
+int fki_bind_cpu(int cpu)
+{
+ if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu))
+ return -EINVAL;
+ return set_cpus_allowed_ptr(current, cpumask_of(cpu));
+}
+EXPORT_SYMBOL(fki_bind_cpu);
diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.h
index 217a6ad..99cb768 100644
--- a/linux/include/linux/fki.h
+++ b/linux/include/linux/fki.h
@@ -52,6 +52,8 @@ void fki_pac_swap(const struct fki_pac_keys *load, struct fki_pac_keys *save);
fki_u64 fki_counter_frequency(void);
fki_u64 fki_counter(void);
void fki_random(void *buf, unsigned long len);
+/* Keep the calling task on one CPU (the hosted kernel sees that CPU's MPIDR). */
+int fki_bind_cpu(int cpu);
#ifdef __cplusplus
}
#endif
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 03f7766..42a7180 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -133,6 +133,12 @@ static int run_m2a(void)
static unsigned char seed[256];
fki_random(seed, sizeof(seed));
+ /* XNU identifies its CPU by MPIDR: run on CPU 0 and describe exactly that CPU. */
+ int berr = fki_bind_cpu(0);
+ if (berr) { logkv("xnu: cannot bind to cpu0, err", (u64_t)berr); return berr; }
+ u64_t mpidr; __asm__ volatile("mrs %0, mpidr_el1" : "=r"(mpidr));
+ mpidr &= 0xffff; /* XNU compares Aff1:Aff0 */
+ logkv("xnu: running on MPIDR Aff1:Aff0", mpidr);
adt_init(&a, base + off_dt, 0x10000);
adt_node(&a, 3, 3); /* / */
adt_prop_str(&a, "name", "device-tree");
@@ -151,7 +157,7 @@ static int run_m2a(void)
adt_prop_str(&a, "name", "cpu0");
adt_prop_str(&a, "device_type", "cpu");
adt_prop_str(&a, "state", "running");
- adt_prop_u32(&a, "reg", 0);
+ adt_prop_u32(&a, "reg", (unsigned)mpidr);
adt_prop_u64(&a, "timebase-frequency", fki_counter_frequency());
adt_node(&a, 3, 0); /* /arm-io: no devices; XNU needs a nonzero SoC base */
adt_prop_str(&a, "name", "arm-io");