Skip to content

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

C

xnu-patches: host calls clobber FP/SIMD; urgent preemption on EL1 hosts; a copyio fault is not I/O-throttled

Fred Nix2f395ced9baa
diff --git a/.github/workflows/linux64-hostcall.yml b/.github/workflows/linux64-hostcall.yml
new file mode 100644
index 0000000..244e584
--- /dev/null
+++ b/.github/workflows/linux64-hostcall.yml
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# What xnu-patches/include/linux64.h tells the compiler about a call into the host is what the host does (it keeps none of
+# the FP/SIMD registers): tests/xnu-patches/hostcall-fp.sh builds a small program against the header alone and runs it on
+# arm64 with a hook that returns with v0-v31 changed. Linux (gcc) and macOS (clang).
+name: linux64-hostcall
+on:
+ push:
+ paths:
+ - .github/workflows/linux64-hostcall.yml
+ - tests/xnu-patches/hostcall*
+ - xnu-patches/include/**
+ pull_request:
+ paths:
+ - .github/workflows/linux64-hostcall.yml
+ - tests/xnu-patches/hostcall*
+ - xnu-patches/include/**
+jobs:
+ fp:
+ strategy:
+ fail-fast: false
+ matrix:
+ os: [ubuntu-24.04-arm, macos-latest]
+ runs-on: ${{ matrix.os }}
+ steps:
+ - uses: actions/checkout@v4
+ - name: A host call that changes every FP/SIMD register
+ run: uname -sm; cc --version | head -1; bash tests/xnu-patches/hostcall-fp.sh
diff --git a/tests/hosted/copyio-throttle.c b/tests/hosted/copyio-throttle.c
new file mode 100644
index 0000000..4211eb6
--- /dev/null
+++ b/tests/hosted/copyio-throttle.c
@@ -0,0 +1,210 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * A page-in taken inside copyin/copyout does not sleep the thread's I/O throttle window there, with the system call's
+ * locks held (xnu-patches/0017). A program for the hosted system (PureDarwin's userland or macOS; no privileges):
+ *
+ * cc -O2 -mmacosx-version-min=11.0 copyio-throttle.c -o copyio-throttle (on a Mac; put it on the hosted disk)
+ * copyio-throttle DIR [SECONDS (10)] in the hosted shell; DIR is on the disk to test, two 64 MiB files are
+ * made there and removed. Exit status 1: the pipe was held.
+ *
+ * writer thread in the throttled disk I/O tier (setiopolicy_np, thread scope). It write()s a file-backed mapping
+ * into a pipe, a page that is not in memory each time, so the kernel's copyin takes a page-in from
+ * disk inside write(), with the pipe locked (pipe_write holds the pipe's I/O lock across uiomove).
+ * noise thread normal tier: reads another file from the same disk, uncached, so the throttle has something to
+ * give way to.
+ * main thread normal tier: every 2 ms one non-blocking read() from the pipe, timed. A read of a pipe whose I/O
+ * lock is held waits for the lock, O_NONBLOCK or not.
+ *
+ * XNU marks a fault taken in copyin/copyout so that vm_fault does not sleep the thread's I/O throttle window there;
+ * the thread sleeps it on the way out of the system call, holding nothing. Then every read here takes microseconds
+ * however long the writer's write()s take. If the kernel sleeps the window inside copyin (the hosted kernel's own
+ * fault paths before 0017), reads wait for it: the "reads over 20 ms" line counts them. (A read also waits for a
+ * page-in that is in flight, in any kernel: milliseconds, which is why the line is drawn at 20.)
+ *
+ * The throttle must be on for the disk, or nothing is tested and the last line says so: XNU switches it off for a
+ * disk that schedules I/O tiers itself (a Mac's own), and keeps it for the hosted disk, with the rotating-disk
+ * numbers (one throttled I/O per 200 ms).
+ */
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
+#define FILE_BYTES (64u << 20)
+#define CHUNK 4096u
+#define SLOW_US 20000u /* above any one page-in, at or below one throttle period (25 ms on an SSD) */
+
+static volatile int stop;
+static int pipe_r, pipe_w, noise_fd;
+static const char *map;
+static size_t pagesz;
+static unsigned long writes, write_max_us, write_total_us, write_slow;
+static int policy_rc = -1, policy_errno;
+
+static uint64_t
+now_us(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (uint64_t)ts.tv_sec * 1000000u + (uint64_t)ts.tv_nsec / 1000u;
+}
+
+static void *
+writer(void *arg)
+{
+ size_t npages = FILE_BYTES / pagesz, page = 0;
+
+ (void)arg;
+ policy_rc = setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD, IOPOL_THROTTLE);
+ policy_errno = errno;
+ while (!stop && page < npages) {
+ uint64_t t0 = now_us();
+ ssize_t n = write(pipe_w, map + page * pagesz, CHUNK); /* copyin of a page that is still on disk */
+ uint64_t d = now_us() - t0;
+
+ if (n < 0 && errno != EINTR) {
+ break;
+ }
+ writes++;
+ write_total_us += d;
+ if (d > write_max_us) {
+ write_max_us = d;
+ }
+ if (d > SLOW_US) {
+ write_slow++;
+ }
+ page += 3; /* never the next page: no read-ahead does this one's work */
+ }
+ return NULL;
+}
+
+static void *
+noise(void *arg)
+{
+ static char buf[256 << 10];
+ off_t off = 0;
+
+ (void)arg;
+ while (!stop) {
+ if (pread(noise_fd, buf, sizeof(buf), off) <= 0) {
+ off = 0;
+ continue;
+ }
+ off += sizeof(buf);
+ if (off >= (off_t)FILE_BYTES) {
+ off = 0;
+ }
+ usleep(1000);
+ }
+ return NULL;
+}
+
+static int
+make_file(const char *path)
+{
+ static char block[1 << 20];
+ int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
+
+ if (fd < 0) {
+ return -1;
+ }
+ fcntl(fd, F_NOCACHE, 1); /* written past the cache: its pages are on disk only */
+ for (unsigned i = 0; i < sizeof(block); i++) {
+ block[i] = (char)(i * 131u + 7u);
+ }
+ for (unsigned done = 0; done < FILE_BYTES; done += sizeof(block)) {
+ if (write(fd, block, sizeof(block)) != (ssize_t)sizeof(block)) {
+ close(fd);
+ return -1;
+ }
+ }
+ fsync(fd);
+ return fd;
+}
+
+static int
+cmp_u32(const void *a, const void *b)
+{
+ uint32_t x = *(const uint32_t *)a, y = *(const uint32_t *)b;
+
+ return x < y ? -1 : x > y;
+}
+
+int
+main(int argc, char **argv)
+{
+ const char *dir = argc > 1 ? argv[1] : ".";
+ int seconds = argc > 2 ? atoi(argv[2]) : 10;
+ char a[1024], b[1024];
+ static char buf[64 << 10]; /* everything in the pipe at once: the writer never waits for room */
+ int fds[2], map_fd;
+ pthread_t tw, tn;
+ uint32_t *lat;
+ unsigned long nread = 0, got = 0, slow = 0, cap = (unsigned long)seconds * 600u + 16u;
+
+ signal(SIGPIPE, SIG_IGN);
+ pagesz = (size_t)sysconf(_SC_PAGESIZE);
+ snprintf(a, sizeof(a), "%s/copyio-throttle.map.%d", dir, (int)getpid());
+ snprintf(b, sizeof(b), "%s/copyio-throttle.noise.%d", dir, (int)getpid());
+ map_fd = make_file(a);
+ noise_fd = make_file(b);
+ if (map_fd < 0 || noise_fd < 0) {
+ perror("copyio-throttle: making the two files");
+ unlink(a);
+ unlink(b);
+ return 2;
+ }
+ map = mmap(NULL, FILE_BYTES, PROT_READ, MAP_PRIVATE, map_fd, 0);
+ if (map == MAP_FAILED || pipe(fds) != 0 || (lat = calloc(cap, sizeof(*lat))) == NULL) {
+ perror("copyio-throttle: mmap/pipe");
+ unlink(a);
+ unlink(b);
+ return 2;
+ }
+ madvise((void *)(uintptr_t)map, FILE_BYTES, MADV_RANDOM); /* a fault brings in its own page only */
+ pipe_r = fds[0];
+ pipe_w = fds[1];
+ fcntl(pipe_r, F_SETFL, O_NONBLOCK);
+ pthread_create(&tn, NULL, noise, NULL);
+ pthread_create(&tw, NULL, writer, NULL);
+
+ uint64_t end = now_us() + (uint64_t)seconds * 1000000u;
+ while (now_us() < end && nread < cap) {
+ usleep(2000);
+ uint64_t t0 = now_us();
+ ssize_t n = read(pipe_r, buf, sizeof(buf));
+ uint64_t d = now_us() - t0;
+
+ lat[nread++] = d > 0xffffffffu ? 0xffffffffu : (uint32_t)d;
+ got += n > 0;
+ slow += d > SLOW_US;
+ }
+ stop = 1;
+ close(pipe_r); /* a writer waiting for room in the pipe comes back with EPIPE */
+ pthread_join(tw, NULL);
+ pthread_join(tn, NULL);
+ unlink(a);
+ unlink(b);
+
+ qsort(lat, nread, sizeof(*lat), cmp_u32);
+ printf("copyio-throttle: %d s on %s, page size %zu; writer's I/O tier: %s\n", seconds, dir, pagesz,
+ policy_rc == 0 ? "throttled (setiopolicy_np ok)" : strerror(policy_errno));
+ printf("copyio-throttle: writer %lu write()s of %u bytes, each from a page still on disk: longest %lu us, mean %lu us, over 20 ms %lu\n",
+ writes, CHUNK, write_max_us, writes ? write_total_us / writes : 0, write_slow);
+ printf("copyio-throttle: reader %lu read()s (%lu with data): longest %u us, 99%% %u us, median %u us\n",
+ nread, got, nread ? lat[nread - 1] : 0, nread ? lat[nread * 99 / 100] : 0, nread ? lat[nread / 2] : 0);
+ printf("copyio-throttle: reads over 20 ms: %lu%s\n", slow,
+ write_slow == 0 ? " (the writer was never slowed: the throttle did not engage, nothing was tested)" :
+ slow ? " <- the pipe was held while its writer slept" : " (the writer slept, never with the pipe held)");
+ return slow ? 1 : 0;
+}
diff --git a/tests/xnu-patches/hostcall-fp.sh b/tests/xnu-patches/hostcall-fp.sh
new file mode 100755
index 0000000..db958cd
--- /dev/null
+++ b/tests/xnu-patches/hostcall-fp.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+# What xnu-patches/include/linux64.h tells the compiler about a call into the host is what the host does: it keeps none
+# of the FP/SIMD registers (Linux schedules during the call). hostcall_fp_test.c is built against the header alone, at
+# three optimisation levels, and run as an ordinary program with a hook that returns with v0-v31 changed. arm64 machines
+# only (it runs the header's own inline assembly); a few seconds.
+# tests/xnu-patches/hostcall-fp.sh [REPO] CC names the compiler (cc)
+set -eu
+here=$(cd "$(dirname "$0")" && pwd)
+repo=${1:-$(cd "$here/../.." && pwd)}
+case "$(uname -m)" in
+arm64|aarch64) ;;
+*) echo "SKIP: this test runs arm64 code (machine is $(uname -m))"; exit 0 ;;
+esac
+tmp=$(mktemp -d)
+trap 'rm -rf "$tmp"' EXIT
+mkdir "$tmp/inc"
+ln -s "$repo/xnu-patches/include" "$tmp/inc/linux64"
+rc=0
+for opt in -O1 -O2 -Os; do
+ "${CC:-cc}" $opt -Wall -Wextra -Werror -I"$tmp/inc" -o "$tmp/t" "$here/hostcall_fp_test.c"
+ echo "== ${CC:-cc} $opt"
+ "$tmp/t" || rc=1
+done
+exit $rc
diff --git a/tests/xnu-patches/hostcall_fp_test.c b/tests/xnu-patches/hostcall_fp_test.c
new file mode 100644
index 0000000..6e5161c
--- /dev/null
+++ b/tests/xnu-patches/hostcall_fp_test.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: APSL-2.0
+/*
+ * A call into the host does not keep the FP/SIMD registers: Linux schedules inside it (idle, the preempted call, disk
+ * and network I/O) and a kernel thread's v0-v31 are neither saved nor put back. Code around linux64_call2 must
+ * therefore come out right when the hook returns with every one of them changed, d8-d15 included.
+ *
+ * Built against xnu-patches/include only and run as an ordinary program on an arm64 machine: the hook below stands
+ * in for the host and overwrites v0-v31.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <linux64/linux64.h>
+
+unsigned long linux64_hook;
+unsigned long linux64_dbg[LINUX64_NCOUNTERS];
+unsigned int linux64_el0;
+
+static volatile unsigned long hook_calls;
+
+/* The host: answers 0 and leaves all 32 FP/SIMD registers changed (nothing is declared: the compiler saves none). */
+__attribute__((noinline, used)) static unsigned long
+host_hook(unsigned long call, unsigned long a1, unsigned long a2)
+{
+ (void)call; (void)a1; (void)a2;
+ hook_calls++;
+ __asm__ volatile (
+ "movi v0.16b, #0xff\n movi v1.16b, #0xff\n movi v2.16b, #0xff\n movi v3.16b, #0xff\n"
+ "movi v4.16b, #0xff\n movi v5.16b, #0xff\n movi v6.16b, #0xff\n movi v7.16b, #0xff\n"
+ "movi v8.16b, #0xff\n movi v9.16b, #0xff\n movi v10.16b, #0xff\n movi v11.16b, #0xff\n"
+ "movi v12.16b, #0xff\n movi v13.16b, #0xff\n movi v14.16b, #0xff\n movi v15.16b, #0xff\n"
+ "movi v16.16b, #0xff\n movi v17.16b, #0xff\n movi v18.16b, #0xff\n movi v19.16b, #0xff\n"
+ "movi v20.16b, #0xff\n movi v21.16b, #0xff\n movi v22.16b, #0xff\n movi v23.16b, #0xff\n"
+ "movi v24.16b, #0xff\n movi v25.16b, #0xff\n movi v26.16b, #0xff\n movi v27.16b, #0xff\n"
+ "movi v28.16b, #0xff\n movi v29.16b, #0xff\n movi v30.16b, #0xff\n movi v31.16b, #0xff\n");
+ return 0;
+}
+
+struct s { long a[8]; };
+
+/* a structure zeroed before the call and another after it: the compiler may keep one zero vector for both */
+__attribute__((noinline)) static void
+zero_twice(struct s *p, struct s *q)
+{
+ *p = (struct s){ 0 };
+ linux64_call2(LINUX64_CALL_PREEMPTED, 1, 0);
+ *q = (struct s){ 0 };
+}
+
+/* a floating-point value used after the call, in the function that makes it */
+__attribute__((noinline)) static double
+fp_across(double x)
+{
+ linux64_call2(LINUX64_CALL_PREEMPTED, 1, 0);
+ return x + x;
+}
+
+/* a function that only makes the call, and a caller that keeps a double of its own across it (in d8-d15) */
+__attribute__((noinline)) static void
+leaf(void)
+{
+ linux64_call2(LINUX64_CALL_PREEMPTED, 1, 0);
+}
+
+__attribute__((noinline)) static double
+fp_caller(double x)
+{
+ leaf();
+ return x + x;
+}
+
+int
+main(void)
+{
+ static volatile double in = 21.0;
+ struct s p, q;
+ int bad = 0;
+
+ linux64_hook = (unsigned long)&host_hook;
+
+ memset(&p, 0x55, sizeof(p));
+ memset(&q, 0x55, sizeof(q));
+ zero_twice(&p, &q);
+ int nz = 0;
+ for (int i = 0; i < 8; i++) {
+ nz += p.a[i] != 0 || q.a[i] != 0;
+ }
+ printf("%s a structure zeroed after a host call is all zero\n", nz ? "FAIL" : "ok ");
+ bad += nz != 0;
+
+ double r = fp_across(in);
+ printf("%s a double used after a host call in the same function: 21 + 21 = %g\n", r == 42.0 ? "ok " : "FAIL", r);
+ bad += r != 42.0;
+
+ r = fp_caller(in);
+ printf("%s a double a caller keeps across a function that makes a host call: 21 + 21 = %g\n",
+ r == 42.0 ? "ok " : "FAIL", r);
+ bad += r != 42.0;
+
+ if (hook_calls != 3) {
+ printf("FAIL the hook ran %lu times, not 3\n", (unsigned long)hook_calls);
+ bad++;
+ }
+ printf("%d of 3 checks failed\n", bad > 3 ? 3 : bad);
+ return bad != 0;
+}
diff --git a/xnu-patches/0016-linux64-el1-host-urgent-preemption-when-preemption-is-re-enabled.patch b/xnu-patches/0016-linux64-el1-host-urgent-preemption-when-preemption-is-re-enabled.patch
new file mode 100644
index 0000000..646be42
--- /dev/null
+++ b/xnu-patches/0016-linux64-el1-host-urgent-preemption-when-preemption-is-re-enabled.patch
@@ -0,0 +1,35 @@
+From: xnulinux
+Subject: [PATCH] linux64: on an EL1 host too, a pending urgent preemption is taken when preemption is re-enabled
+
+kernel_preempt_check is what _enable_preemption calls when the count reaches 0 with AST_URGENT pending: the place a
+thread is preempted when the preemption was asked for while it had preemption off (it woke a thread that should run
+instead while it held a spinlock; the hosted timer, run from where interrupts are re-enabled, expired its quantum).
+The pure EL0 host gave the function a hosted branch on XNU's virtual interrupt flag, because DAIF cannot be read at
+EL0, under `if (linux64_el0)`. On an EL1 host it still read DAIF. xnu.ko enters XNU with every interrupt masked
+(`msr daifset, #0xf`) and nothing hosted clears the mask (ml_set_interrupts_enabled only flips the virtual flag), so
+the test `state & DAIF_IRQF` was always true and the function returned: on an EL1 host enable_preemption() never
+preempted. The thread kept its CPU until it next turned XNU's interrupts back on with preemption on, blocked, or went
+back to user space, and the thread that should have run (a real-time thread, a thread_call) waited that long.
+
+The hosted branch is now taken on every host. It is the code a pure EL0 host has run all along; where the hosted
+kernel delivers its timer and IPIs (ml_set_interrupts_enabled, linux64_cpu_idle, linux64_spin_relax, the EL0 IRQ
+path) the virtual flag is off, and the branch returns before anything.
+---
+--- a/osfmk/arm/preemption_disable.c
++++ b/osfmk/arm/preemption_disable.c
+@@ -69,7 +69,14 @@ kernel_preempt_check(void)
+ {
+ uint64_t state;
+
+- if (linux64_el0) {
++ /*
++ * LINUX64 (xnulinux): hosted, XNU's interrupt state is the virtual flag (ml_set_interrupts_enabled), on an EL1 host
++ * as on a pure EL0 one. The hardware's mask, which the test below reads, stays set for as long as XNU's kernel runs
++ * on an EL1 host: with it an urgent AST was never taken here, and a preemption asked for while preemption was off
++ * (a wakeup under a spinlock, the timer run from where interrupts are re-enabled) waited for the next place
++ * interrupts are turned back on with preemption on, or for the return to user space.
++ */
++ if (linux64_hook) {
+ if (!ml_get_interrupts_enabled()) {
+ return;
+ }
diff --git a/xnu-patches/0017-linux64-copyio-fault-is-not-throttled.patch b/xnu-patches/0017-linux64-copyio-fault-is-not-throttled.patch
new file mode 100644
index 0000000..16865da
--- /dev/null
+++ b/xnu-patches/0017-linux64-copyio-fault-is-not-throttled.patch
@@ -0,0 +1,77 @@
+From: xnulinux
+Subject: [PATCH] linux64: a page-in taken inside copyin/copyout is not I/O-throttled there
+
+vm_fault ends in throttle_lowpri_io(1) when it paged in from disk: a thread whose I/O is being throttled (utility or
+background tier, with higher-tier I/O going on) sleeps its throttle window there. For a fault taken in the kernel's
+copyin/copyout that is wrong: the thread is in the middle of a system call and holds what the call holds (a pipe's
+mutex, a file's lock in a write), so everything else that needs it waits out a background thread's throttle window.
+XNU's own abort handler therefore marks such a fault (handle_kernel_abort: thread->recover around vm_fault, "we don't
+want to throttle kernel faults for copyio faults"), throttle_lowpri_io asks current_thread_in_kernel_fault() and
+returns, and the thread sleeps its window on the way back to user space instead (unix_syscall's own
+throttle_lowpri_io).
+
+Hosted, XNU's abort handler does not see these faults, and neither stand-in set the mark: linux64_kernel_fault (an
+EL1 host: Linux takes the abort and forwards it) and linux64_el0_fault_user_page (a pure EL0 host: copyio resolves
+the fault itself). Both set it now, linux64_kernel_fault for a fault on a user address: with PAN on those are the
+kernel's copyin/copyout faults.
+---
+--- a/osfmk/arm64/copyio.c
++++ b/osfmk/arm64/copyio.c
+@@ -155,11 +155,14 @@ linux64_el0_fault_user_page(vm_map_t map, user_addr_t uaddr, bool write)
+ fault_type, FALSE, FALSE) == KERN_SUCCESS) {
+ return 0;
+ }
+- if (vm_fault(map, (vm_map_offset_t)(uaddr & ~0xfffULL), fault_type,
+- FALSE, VM_KERN_MEMORY_NONE, THREAD_UNINT, NULL, 0) != KERN_SUCCESS) {
+- return EFAULT;
+- }
+- return 0;
++ /* a copyio fault, as linux64_kernel_fault's on an EL1 host (sleh.c): not one for vm_fault to throttle I/O on */
++ thread_t thread = current_thread();
++ bool const was_recover = thread->recover;
++ thread->recover = true;
++ kern_return_t kr = vm_fault(map, (vm_map_offset_t)(uaddr & ~0xfffULL), fault_type,
++ FALSE, VM_KERN_MEMORY_NONE, THREAD_UNINT, NULL, 0);
++ thread->recover = was_recover;
++ return kr == KERN_SUCCESS ? 0 : EFAULT;
+ }
+
+ static int
+--- a/osfmk/arm64/sleh.c
++++ b/osfmk/arm64/sleh.c
+@@ -3684,8 +3684,8 @@ linux64_kernel_fault(uint64_t far, uint64_t esr)
+ }
+ /* a kernel address faults on the kernel map; a user one (copyio) on the current thread's map */
+ vm_map_t map = kernel_map;
++ thread_t thread = current_thread();
+ if (!VM_KERNEL_ADDRESS(far)) {
+- thread_t thread = current_thread();
+ if (thread == THREAD_NULL || thread->map == VM_MAP_NULL || thread->map == kernel_map) {
+ return KERN_FAILURE;
+ }
+@@ -3695,11 +3695,23 @@ linux64_kernel_fault(uint64_t far, uint64_t esr)
+ if (!is_translation_fault(fault_code)) {
+ ffr = arm_fast_fault(map->pmap, far, fault_type, (fault_code == FSC_ACCESS_FLAG_FAULT_L3), FALSE);
+ }
+- kern_return_t kr = ffr == KERN_SUCCESS ? KERN_SUCCESS :
+- vm_fault(map, far, fault_type, FALSE, VM_KERN_MEMORY_NONE, THREAD_UNINT, NULL, 0);
+ if (ffr == KERN_SUCCESS) {
+ return KERN_SUCCESS;
+ }
++ /*
++ * As handle_kernel_abort marks a fault taken in copyin/copyout (hosted: the kernel's faults on a user address are
++ * those): vm_fault ends in throttle_lowpri_io, which asks current_thread_in_kernel_fault() and does not throttle
++ * such a fault. Without the mark a thread with throttled I/O behind it slept its throttle window here, inside
++ * the system call, holding whatever the call holds; it sleeps that window on the way back to user space.
++ */
++ bool const was_recover = (map != kernel_map) ? thread->recover : false;
++ if (map != kernel_map) {
++ thread->recover = true;
++ }
++ kern_return_t kr = vm_fault(map, far, fault_type, FALSE, VM_KERN_MEMORY_NONE, THREAD_UNINT, NULL, 0);
++ if (map != kernel_map) {
++ thread->recover = was_recover;
++ }
+ return kr;
+ }
+
diff --git a/xnu-patches/0018-linux64-host-disk-through-linux64-call.patch b/xnu-patches/0018-linux64-host-disk-through-linux64-call.patch
new file mode 100644
index 0000000..45a7266
--- /dev/null
+++ b/xnu-patches/0018-linux64-host-disk-through-linux64-call.patch
@@ -0,0 +1,26 @@
+From: xnulinux
+Subject: [PATCH] linux64: the host-file md disk calls the host through linux64_call2
+
+memdev.c had its own copy of the call into the host (mdev_host_call: the `blr` and its list of registers the call
+does not keep), from before linux64.h had linux64_call2. The list now also names the FP/SIMD registers, which the host
+does not keep (linux64.h); a second copy of it would have to follow every such change. The disk's two calls
+(LINUX64_CALL_RAMDISK_SIZE, LINUX64_CALL_RAMDISK_RW) go through linux64_call2 like every other host call.
+---
+--- a/bsd/dev/memdev.c
++++ b/bsd/dev/memdev.c
+@@ -194,13 +194,8 @@ struct mdev_host_req { uint64_t write, off, len; void *buf; long result; }; /* t
+ static long
+ mdev_host_call(unsigned long stage, unsigned long arg)
+ {
+- register unsigned long x0 __asm__("x0") = stage;
+- register unsigned long x1 __asm__("x1") = arg;
+- register unsigned long x2 __asm__("x2") = 0;
+- __asm__ volatile ("blr %3" : "+r"(x0), "+r"(x1), "+r"(x2) : "r"(linux64_hook)
+- : "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13",
+- "x14", "x15", "x16", "x17", "x30", "cc", "memory");
+- return (long)x0;
++ /* through linux64_call2, which says what a host call does not keep (the FP/SIMD registers too) */
++ return (long)linux64_call2((enum linux64_call)stage, arg, 0);
+ }
+
+ static int
diff --git a/xnu-patches/include/linux64.h b/xnu-patches/include/linux64.h
index 45929be..f93bcfc 100644
--- a/xnu-patches/include/linux64.h
+++ b/xnu-patches/include/linux64.h
@@ -62,8 +62,16 @@ extern unsigned int linux64_ipi_pending[LINUX64_CPU_SLOTS];
/* enum linux64_counter */
extern unsigned long linux64_dbg[LINUX64_NCOUNTERS];
-/* A call into the host (enum linux64_call). The host's answer: 0 to go on. */
-static inline unsigned long
+/*
+ * A call into the host (enum linux64_call). The host's answer: 0 to go on.
+ *
+ * The host keeps none of the FP/SIMD registers across it: it saves none of them, and where it lets Linux run (idle,
+ * preempted, file I/O, the network, a host call) they come back as the last Linux task or kernel-mode NEON user left
+ * them (Linux keeps no FP/SIMD state for a kernel thread). That includes d8-d15, which a callee owes its caller: the
+ * function that makes the call saves them. Not inline, so that this is the one function, and only when a call is made
+ * (inline, every function with a call in it saved them on every entry: ml_set_interrupts_enabled, each time).
+ */
+__attribute__((noinline, unused)) static unsigned long
linux64_call2(enum linux64_call call, unsigned long a1, unsigned long a2)
{
register unsigned long x0 __asm__("x0") = (unsigned long)call;
@@ -76,7 +84,10 @@ linux64_call2(enum linux64_call call, unsigned long a1, unsigned long a2)
: "+r"(x0), "+r"(x1), "+r"(x2)
: "r"(linux64_hook)
: "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17",
- "x30", "cc", "memory");
+ "x30", "cc", "memory",
+ "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
+ "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30",
+ "v31");
}
return x0;
}