XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
tests/host: checks that the hosted kernel leaves Linux's FP/SIMD state and memory alone (1 min)
Heyang Zhoucd71c3476027
diff --git a/tests/host/fpcheck.c b/tests/host/fpcheck.cnew file mode 100644index 0000000..fb768a9--- /dev/null+++ b/tests/host/fpcheck.c@@ -0,0 +1,34 @@+/* Keep known values in the FP/SIMD registers v8..v15 on one CPU while sleeping in a loop (so the task is switched out+ * and back in), and count the times they came back changed: a probe for a foreign kernel using SIMD on that CPU+ * behind Linux's back (Linux skips reloading a task's FP state when it believes the registers still hold it).+ * Usage: fpcheck CPU SECONDS */+#define _GNU_SOURCE+#include <sched.h>+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>+#include <time.h>+#include <unistd.h>++int main(int argc, char **argv)+{+ int cpu = argc > 1 ? atoi(argv[1]) : 0, secs = argc > 2 ? atoi(argv[2]) : 60;+ cpu_set_t s; CPU_ZERO(&s); CPU_SET(cpu, &s);+ if (sched_setaffinity(0, sizeof(s), &s)) { perror("affinity"); return 1; }+ uint64_t want = 0x0123456789abcdefULL, got[8];+ long rounds = 0, bad = 0;+ time_t end = time(NULL) + secs;+ while (time(NULL) < end) {+ __asm__ volatile("dup v8.2d, %0\n dup v9.2d, %0\n dup v10.2d, %0\n dup v11.2d, %0\n"+ "dup v12.2d, %0\n dup v13.2d, %0\n dup v14.2d, %0\n dup v15.2d, %0" :: "r"(want) : "v8","v9","v10","v11","v12","v13","v14","v15");+ for (int i = 0; i < 20; i++) sched_yield(); /* switched out when anything else on this CPU wants to run */+ usleep(200);+ __asm__ volatile("umov %0, v8.d[0]\n umov %1, v9.d[0]\n umov %2, v10.d[0]\n umov %3, v11.d[0]\n"+ "umov %4, v12.d[0]\n umov %5, v13.d[0]\n umov %6, v14.d[0]\n umov %7, v15.d[0]"+ : "=r"(got[0]), "=r"(got[1]), "=r"(got[2]), "=r"(got[3]), "=r"(got[4]), "=r"(got[5]), "=r"(got[6]), "=r"(got[7]));+ for (int i = 0; i < 8; i++) if (got[i] != want) { if (bad++ < 10) printf("v%d changed: %#018llx\n", 8 + i, (unsigned long long)got[i]); break; }+ rounds++; want = want * 6364136223846793005ULL + 1442695040888963407ULL;+ }+ printf("cpu %d: %ld rounds, %ld with SIMD registers changed\n", cpu, rounds, bad);+ return bad != 0;+}diff --git a/tests/host/memcheck.c b/tests/host/memcheck.cnew file mode 100644index 0000000..f74a022--- /dev/null+++ b/tests/host/memcheck.c@@ -0,0 +1,44 @@+/* Fill GBs of memory with a pattern derived from each word's address, re-verify it in a loop and report every word+ * that changed: its virtual and physical address (/proc/self/pagemap, root) and the value found. A probe for a foreign+ * writer (the hosted kernel, a device) scribbling over Linux's memory. Usage: memcheck GB SECONDS */+#include <fcntl.h>+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>+#include <sys/mman.h>+#include <time.h>+#include <unistd.h>++static uint64_t pat(uint64_t *p) { return ((uint64_t)p * 0x9E3779B97F4A7C15ULL) ^ 0x5A5A5A5AA5A5A5A5ULL; }++static uint64_t phys(int pm, void *va)+{+ uint64_t e = 0;+ if (pread(pm, &e, 8, ((uintptr_t)va / 4096) * 8) != 8 || !(e >> 63)) return 0;+ return ((e & ((1ULL << 55) - 1)) << 12) | ((uintptr_t)va & 4095);+}++int main(int argc, char **argv)+{+ size_t gb = argc > 1 ? atol(argv[1]) : 2, n = gb << 27; /* words */+ int secs = argc > 2 ? atoi(argv[2]) : 120, pm = open("/proc/self/pagemap", O_RDONLY), bad = 0, rounds = 0;+ uint64_t *m = mmap(NULL, n * 8, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);+ if (m == MAP_FAILED) { perror("mmap"); return 1; }+ mlock(m, n * 8);+ for (size_t i = 0; i < n; i++) m[i] = pat(&m[i]);+ time_t end = time(NULL) + secs;+ while (time(NULL) < end) {+ for (size_t i = 0; i < n; i++) {+ uint64_t want = pat(&m[i]);+ if (m[i] != want) {+ if (bad++ < 64)+ printf("changed: va %p pa %#llx found %#018llx want %#018llx\n", (void *)&m[i],+ (unsigned long long)phys(pm, &m[i]), (unsigned long long)m[i], (unsigned long long)want);+ m[i] = want;+ }+ }+ rounds++;+ }+ printf("%zu GB, %d rounds, %d words changed\n", gb, rounds, bad);+ return bad != 0;+}diff --git a/tests/host/run.sh b/tests/host/run.shnew file mode 100755index 0000000..4bc9e20--- /dev/null+++ b/tests/host/run.sh@@ -0,0 +1,12 @@+#!/bin/bash+# In the VM, with a hosted system running: checks that the hosted kernel leaves Linux alone. 1 min.+# fpcheck: a task on the hosted kernel's CPU (0) keeps its FP/SIMD registers across being switched out+# memcheck: 4 GB of Linux memory keep their contents (nothing writes over Linux's pages)+set -e+cd "$(dirname "$0")"+O=${TMPDIR:-/tmp}+cc -O2 -o $O/fpcheck fpcheck.c && cc -O2 -o $O/memcheck memcheck.c+$O/fpcheck 0 25 & f=$!+sudo $O/memcheck 4 25; m=$?+wait $f; f=$?+[ $f = 0 ] && [ $m = 0 ] && echo "host tests: pass" || { echo "host tests: FAIL (fpcheck $f, memcheck $m)"; exit 1; }