Skip to content

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

C

Hosted XNU: more than 8 CPUs (up to 32) (#30)

Michael Klikushin6c29bea3ef63
diff --git a/docs/smp.md b/docs/smp.md
index 0198001..cfd2954 100644
--- a/docs/smp.md
+++ b/docs/smp.md
@@ -76,5 +76,8 @@ spinning lets the host take the spinning CPU's interrupts (`linux64_spin_relax`,
- Paravirtual ticket locks (`xnu-patches/0005`): waiters sleep in the host and are kicked; a spinning CPU takes a
pending IPI as its FIQ would.
- Linux's device interrupts are kept off XNU's CPUs (`xnulinux-boot.sh`).
+ More than 8 (`xnu-patches/0010`): the virtual board's `MAX_CPUS` was 8 and a ninth CPU faulted in
+ `ml_processor_register`. The limit is now 32 (`LINUX64_MAX_CPUS`, the kernel's and `xnu.ko`'s). Run with 12, 14
+ and 16 CPUs on a 16-vCPU host (desktop, `hw.ncpu` right); more than 16 has not been run.
Open: the default for `cpus=`; cross-CPU costs (pipe round trip 11.9 µs on 2 CPUs against 3.3 µs on one,
fork 2.2 ms against 0.97 ms).
diff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.sh
index ee3835b..378f116 100755
--- a/scripts/xnulinux-boot.sh
+++ b/scripts/xnulinux-boot.sh
@@ -14,7 +14,7 @@
# environment:
# DISK (/var/tmp/mac.img) RAM_MB (7900) BOOTARGS CONSOLE_IN (/var/tmp/console-in) INPUT_IN (/var/tmp/xnu-input)
# FB_DUMP (/var/tmp/xnu-fb.raw) KEYSTORE (/var/tmp/xnu-keystore.bin) TRUSTCACHE (xnu/trustcache-mac.bin; empty: none)
-# IMAGE (xnu/kc.bin: the kernel collection under /lib/firmware) GPU (auto|off) CPUS (1: the hosted system's CPUs) XNU_ARGS (more xnu.ko parameters)
+# IMAGE (xnu/kc.bin: the kernel collection under /lib/firmware) GPU (auto|off) CPUS (1: the hosted system's CPUs, 32 at most) XNU_ARGS (more xnu.ko parameters)
set -euo pipefail
cd "$(dirname "$0")/.."
if [ "${1:-}" = stop ]; then # end the running hosted system (its disk synced first) and remove xnu.ko
@@ -84,10 +84,11 @@ if [ "$NCPU" -gt "$CPUS" ]; then
l = [int(c) for c in '$XCPUS'.split(',') if c][:$CPUS] or list(range($CPUS))
m = (1 << $NCPU) - 1
for c in l: m &= ~(1 << c)
-print(format(m, 'x'))")
+h = format(m, 'x') # Linux reads a CPU mask in comma-separated groups of 32 bits
+print(','.join(reversed([h[max(i - 8, 0):i] for i in range(len(h), 0, -8)])))")
echo "$MASK" | sudo tee /proc/irq/default_smp_affinity >/dev/null 2>&1 || true
for f in /proc/irq/*/smp_affinity; do echo "$MASK" | sudo tee "$f" >/dev/null 2>&1 || true; done
- HOSTCPUS=$(python3 -c "print(','.join(str(c) for c in range($NCPU) if 0x$MASK >> c & 1))")
+ HOSTCPUS=$(python3 -c "print(','.join(str(c) for c in range($NCPU) if 0x${MASK//,/} >> c & 1))")
fi
# XNU's CPUs stay out of Linux's deep idle states: a hosted CPU with nothing to run sleeps in Linux, and waking it from
# one (a thread made runnable on another CPU: every Mach message) took 0.6 ms on average on a DGX Spark, 10 us without.
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index bbdf087..6c3912b 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -42,8 +42,12 @@ static int quiet_line(const char *l, unsigned n)
if (!x_strncmp(l + k, "triggered unnest", 16)) return 1;
return 0;
}
-/* XNU's CPUs: Linux CPUs 0 .. cpus-1 (XNU's CPU 0 is Linux CPU 0, where xnu.ko's loading task runs) */
-#define XNU_MAX_CPUS 16
+/*
+ * XNU's CPUs: Linux CPUs 0 .. cpus-1 (XNU's CPU 0 is Linux CPU 0, where xnu.ko's loading task runs). No more than
+ * XNU's own tables hold (LINUX64_MAX_CPUS): XNU faulted registering a CPU beyond them (12 asked of a kernel built for
+ * 8: ml_processor_register read the ninth entry of an eight-entry table).
+ */
+#define XNU_MAX_CPUS LINUX64_MAX_CPUS
static int cpus = 1;
module_param(cpus, int, 0444);
static struct xnu_world worlds[XNU_MAX_CPUS]; /* worlds[0] is xnu_W's twin: see xnu_worlds */
@@ -1165,7 +1169,10 @@ static struct boot_args_v2 *build_boot(unsigned char *base_va, u64_t phys, u64_t
int berr = fki_bind_cpu(0);
if (berr) { logkv("xnu: cannot bind to cpu0, err", (u64_t)berr); return 0; }
if (cpus < 1) cpus = 1;
- if (cpus > XNU_MAX_CPUS) cpus = XNU_MAX_CPUS;
+ if (cpus > XNU_MAX_CPUS) {
+ logkv("xnu: more CPUs asked for than XNU is built for, using", (u64_t)XNU_MAX_CPUS);
+ cpus = XNU_MAX_CPUS;
+ }
if (cpus > fki_num_cpus()) cpus = fki_num_cpus();
for (int i = 0; i < LINUX64_CPU_SLOTS; i++) linux_cpu_of_slot[i] = -1;
for (int c = 0; c < cpus; c++) {
@@ -1271,7 +1278,7 @@ static struct boot_args_v2 *build_boot(unsigned char *base_va, u64_t phys, u64_t
for (int c = 0; c < cpus; c++) { /* compatible: kexts/XnuCPU registers each with XNU and starts it */
static char cname[XNU_MAX_CPUS][8];
cname[c][0] = 'c'; cname[c][1] = 'p'; cname[c][2] = 'u';
- if (c < 10) { cname[c][3] = '0' + c; cname[c][4] = 0; } else { cname[c][3] = '1'; cname[c][4] = '0' + c - 10; cname[c][5] = 0; }
+ if (c < 10) { cname[c][3] = '0' + c; cname[c][4] = 0; } else { cname[c][3] = '0' + c / 10; cname[c][4] = '0' + c % 10; cname[c][5] = 0; }
adt_node(&a, 6, 0);
adt_prop_str(&a, "name", cname[c]);
adt_prop_str(&a, "compatible", "xnulinux,cpu");
@@ -1511,7 +1518,8 @@ static int monitor_fn(void *arg)
}
}
if (perf && cpus > 1) { /* each XNU CPU's host calls so far and the last one's number */
- char cb[200]; char *q = cb;
+ static char cb[16 + XNU_MAX_CPUS * 42]; /* room for every CPU: two 64-bit decimals and two separators each */
+ char *q = cb;
put_str(&q, "xnu: calls");
for (int c = 0; c < cpus; c++) {
unsigned s = LINUX64_CPU_SLOT(cpu_mpidr[c]);
diff --git a/xnu-patches/0010-linux64-up-to-32-cpus.patch b/xnu-patches/0010-linux64-up-to-32-cpus.patch
new file mode 100644
index 0000000..f47404a
--- /dev/null
+++ b/xnu-patches/0010-linux64-up-to-32-cpus.patch
@@ -0,0 +1,54 @@
+From: xnulinux
+Subject: [PATCH] linux64: up to 32 CPUs
+
+The virtual board's kernel was built for 8 CPUs (MAX_CPUS, the SM8550's and SM8350's eight cores), and xnu.ko gave
+XNU up to 16. A host asked for 12 ended the boot: the device tree's topology stopped at 8, XnuCPU registered the
+ninth CPU, and ml_processor_register read CpuDataEntries[8] (cpu_datap) before its own range check: a data abort at
+address 0 ("XNU took a fault it cannot handle", pc in _ml_processor_register).
+
+MAX_CPUS is now 32, and LINUX64_MAX_CPUS (linux64_abi.h) names the same limit for the host: xnu.ko gives XNU no more,
+and a static assertion here keeps the two together. ml_processor_register checks the CPU's number before it reads the
+entry, so a CPU beyond the topology is refused (KERN_FAILURE, which XnuCPU logs) instead of faulting.
+---
+--- a/pexpert/pexpert/arm64/board_config.h
++++ b/pexpert/pexpert/arm64/board_config.h
+@@ -308,7 +308,12 @@
+ #include <pexpert/arm64/BCM2837.h>
+
+ #define MAX_L2_CLINE 6
+-#define MAX_CPUS 8 /* SM8550 (sheng) and SM8350 (zeta) have eight cores */
++/*
++ * LINUX64 (xnulinux): as many as the hosted system can be given (LINUX64_MAX_CPUS in linux64_abi.h, which
++ * machine_routines.c checks against this). With 8, the SM8550's and SM8350's eight cores, a host that gave XNU more
++ * ended the boot: XnuCPU registered the ninth, and ml_processor_register read past CpuDataEntries.
++ */
++#define MAX_CPUS 32
+ /*
+ * Two clusters: the device tree's cpus carry cluster-type 'E' or 'P' from the host's cpu capacities (make-droidvm-adt.py; all
+ * 'P' when the host gives every vCPU the same). The AMP scheduler (sched_amp, not Edge: Edge places threads by CLPC's thread group
+--- a/osfmk/arm64/machine_routines.c
++++ b/osfmk/arm64/machine_routines.c
+@@ -1527,6 +1527,8 @@
+ }
+
+ static_assert(MAX_CPUS <= 256, "MAX_CPUS must fit in _COMM_PAGE_CPU_TO_CLUSTER; Increase table size if needed");
++/* LINUX64 (xnulinux): the host gives XNU up to LINUX64_MAX_CPUS CPUs (xnu.ko), and the per-CPU tables must hold them */
++static_assert(MAX_CPUS >= LINUX64_MAX_CPUS, "MAX_CPUS (board_config.h) is below LINUX64_MAX_CPUS (linux64_abi.h)");
+
+ void
+ ml_map_cpus_to_clusters(uint8_t *table)
+@@ -1690,6 +1692,14 @@
+ processor_t *processor_out, ipi_handler_t *ipi_handler_out,
+ perfmon_interrupt_handler_func *pmi_handler_out)
+ {
++ /*
++ * LINUX64 (xnulinux): a CPU the topology does not have is refused before its entry is read. The check below came
++ * after cpu_datap(), which reads CpuDataEntries[log_id]: a device tree with more CPUs than MAX_CPUS (a host
++ * giving XNU more than it was built for) took a data abort here instead of KERN_FAILURE.
++ */
++ if (in_processor_info->log_id > (uint32_t)ml_get_max_cpu_number()) {
++ return KERN_FAILURE;
++ }
+ cpu_data_t *this_cpu_datap = cpu_datap(in_processor_info->log_id);
+ assert3u(this_cpu_datap->cpu_number, ==, in_processor_info->log_id); /* from ml_bootstrap_processors() */
+
diff --git a/xnu-patches/include/linux64_abi.h b/xnu-patches/include/linux64_abi.h
index 3833936..a864c83 100644
--- a/xnu-patches/include/linux64_abi.h
+++ b/xnu-patches/include/linux64_abi.h
@@ -191,13 +191,21 @@ struct linux64_input_event {
#define LINUX64_CPU_SLOTS 256
#define LINUX64_CPU_SLOT(mpidr) ((unsigned)(((mpidr) & 0xf) | (((mpidr) >> 4) & 0xf0)))
+/*
+ * The most CPUs the hosted system can have: the size of XNU's per-CPU tables on the virtual board (MAX_CPUS in
+ * pexpert's board_config.h, which xnu-patches/0010 sets and osfmk/arm64/machine_routines.c checks against this) and
+ * of the host's (xnu.ko gives XNU no more, whatever its cpus parameter asks). A power of two (LINUX64_EL0_CPUS).
+ */
+#define LINUX64_MAX_CPUS 32
+
/*
* linux64_el0_stat[cpu][0][class]: exceptions taken from user space, by class; [cpu][1][class]: counter ticks from
* taking one until the CPU next enters user space (blocking included). Classes: SVC, data abort, instruction abort,
- * host IRQ, other. By XNU CPU number (cpu_number() & (LINUX64_EL0_CPUS - 1)): small, the kernel's data must not grow.
+ * host IRQ, other. By XNU CPU number (cpu_number() & (LINUX64_EL0_CPUS - 1)): one row for each CPU there can be (two
+ * CPUs sharing a row would also share its pending start time and class).
*/
#define LINUX64_EL0_CLASSES 5
-#define LINUX64_EL0_CPUS 8
+#define LINUX64_EL0_CPUS LINUX64_MAX_CPUS
enum linux64_counter {
LINUX64_CTR_IDLE_THREAD = 0, /* XNU's idle thread loops */