Skip to content

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

C

fki: Linux tasks on the hosted kernel's CPU no longer inherit its FP/SIMD registers

Heyang Zhou2cb44e25de89
diff --git a/fki-ko/fki.h b/fki-ko/fki.h
index aac3732..f67be16 100644
--- a/fki-ko/fki.h
+++ b/fki-ko/fki.h
@@ -44,7 +44,8 @@ void fki_region_free(struct fki_region *r);
/* A named blob from the firmware search path. */
int fki_blob_get(const char *name, const void **data, unsigned long *size, void **handle);
void fki_blob_put(void *handle);
-/* Enter and leave a foreign context on this CPU: no preemption, no interrupts, foreign PAC keys. */
+/* Enter and leave a foreign context on this CPU: no preemption, no interrupts, foreign PAC keys; the FP/SIMD registers are
+ * the foreign kernel's to use (the current task's state saved first). */
unsigned long fki_enter(const struct fki_pac_keys *foreign, struct fki_pac_keys *saved);
void fki_leave(unsigned long cookie, const struct fki_pac_keys *saved);
/* Swap PAC keys only (caller already in a foreign context): save current into *save, load *load. */
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.c
index 1020439..dc07192 100644
--- a/fki-ko/fki_mod.c
+++ b/fki-ko/fki_mod.c
@@ -23,6 +23,7 @@
#include <linux/cpumask.h>
#include <linux/vmalloc.h>
#include <asm/arch_timer.h>
+#include <asm/neon.h>
#include <asm/sysreg.h>
#include <asm/tlbflush.h>
#include <asm/barrier.h>
@@ -168,10 +169,28 @@ static __always_inline void keys_write(const struct fki_pac_keys *k)
isb();
}
+/*
+ * The foreign kernel uses the FP/SIMD registers (its user threads' state, its own SIMD code) without Linux knowing.
+ * Linux skips reloading a task's FP/SIMD state when it believes this CPU's registers still hold it, so a task that ran
+ * here before would come back with the foreign kernel's register contents (random user-space crashes on the hosted
+ * kernel's CPU). Before the foreign kernel runs (on entry, and each time it resumes after Linux ran), save the current
+ * task's state and make Linux forget whose state the registers hold: an empty kernel-mode NEON section does both.
+ * Nothing of the foreign kernel's needs keeping across that: its kernel code does not keep FP/SIMD values across calls
+ * into the host, and it saves its user threads' registers on each exception.
+ */
+static void fki_fpsimd_forget(void)
+{
+ struct user_fpsimd_state scratch; /* where Linux would save a kernel-mode state over a context switch: none here */
+
+ kernel_neon_begin(&scratch);
+ kernel_neon_end(&scratch);
+}
+
__attribute__((target("branch-protection=none"))) unsigned long fki_enter(const struct fki_pac_keys *foreign, struct fki_pac_keys *saved)
{
unsigned long flags;
+ fki_fpsimd_forget();
preempt_disable();
local_irq_save(flags);
keys_read(saved);
@@ -548,11 +567,28 @@ static void fki_linux_runs_body(void (*fn)(void *), void *arg);
* Linux runs with SError (PSTATE.A) unmasked; the foreign kernel keeps D, A, I and F masked and asserts so in its
* idle loop. Give it back exactly the exception mask it called with.
*/
+struct fki_runs {
+ void (*fn)(void *);
+ void *arg;
+};
+
+/* fn runs with interrupts on and turns them off again; then, before the foreign kernel resumes, fki_fpsimd_forget */
+static void fki_runs_then_forget(void *p)
+{
+ struct fki_runs *r = p;
+
+ r->fn(r->arg);
+ local_irq_enable();
+ fki_fpsimd_forget();
+ local_irq_disable();
+}
+
static void fki_linux_runs(void (*fn)(void *), void *arg)
{
unsigned long daif = read_sysreg(daif);
+ struct fki_runs r = { fn, arg };
- fki_linux_runs_body(fn, arg);
+ fki_linux_runs_body(fki_runs_then_forget, &r);
write_sysreg(daif, daif);
}