XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
CPU Metal: Mesa's threads leave the hardware-fault signals unblocked (#32)
Michael Klikushin4533f1ee203e
diff --git a/gpu/xnu-cpu-metal/patches/mesa/0003-util-thread-fault-signals-unblocked.patch b/gpu/xnu-cpu-metal/patches/mesa/0003-util-thread-fault-signals-unblocked.patchnew file mode 100644index 0000000..06e2bae--- /dev/null+++ b/gpu/xnu-cpu-metal/patches/mesa/0003-util-thread-fault-signals-unblocked.patch@@ -0,0 +1,30 @@+Upstream: Mesa 26.3.1 (https://archive.mesa3d.org/mesa-26.3.1.tar.xz)+Upstream-License: MIT+Patch-License: MIT (provided under the same license as upstream)+Subject: util: threads Mesa starts leave the hardware-fault signals unblocked++Mesa blocks every signal but SIGSYS and SIGSEGV on the threads it starts (u_thread_create_ex). On Darwin a hardware+fault whose signal is blocked on the faulting thread is not delivered: the thread runs the same instruction again+and faults again, forever, at full CPU (gpu/xnu-cpu-metal/test/faultmask.c shows it, on a Mac too). A protection+fault there is SIGBUS, not SIGSEGV, so a rasteriser thread that jumps to non-executable memory never ends its+process: iconservicesagent was seen at 100% of a CPU with 150,000 faults a second. With SIGBUS, SIGILL, SIGFPE and+SIGTRAP left unblocked the process dies with a crash report and launchd starts it again.++--- a/src/util/u_thread.c++++ b/src/util/u_thread.c+@@ -84,6 +84,15 @@+ * that tracking mechanism.+ */+ sigdelset(&new_set, SIGSEGV);++++ /* A hardware fault on a thread that has its signal blocked is not delivered on Darwin: the thread takes the++ * same fault again and again, at full CPU, and the process never dies (a protection fault there is SIGBUS,++ * not SIGSEGV). Leave the fault signals to their default action.++ */++ sigdelset(&new_set, SIGBUS);++ sigdelset(&new_set, SIGILL);++ sigdelset(&new_set, SIGFPE);++ sigdelset(&new_set, SIGTRAP);+ pthread_sigmask(SIG_BLOCK, &new_set, &saved_set);+ ret = thrd_create(thrd, thread_routine, paramp);+ pthread_sigmask(SIG_SETMASK, &saved_set, NULL);diff --git a/gpu/xnu-cpu-metal/test/faultmask.c b/gpu/xnu-cpu-metal/test/faultmask.cnew file mode 100644index 0000000..aa6d34f--- /dev/null+++ b/gpu/xnu-cpu-metal/test/faultmask.c@@ -0,0 +1,59 @@+// SPDX-License-Identifier: GPL-2.0-or-later+// What Darwin does with a hardware fault on a thread that has the fault's signal blocked: the signal stays pending, the+// thread runs the same instruction again and faults again, forever, at full CPU. A real Mac does the same. It is why+// patches/mesa/0003 leaves the fault signals unblocked on the threads Mesa starts: lavapipe's rasteriser+// threads had SIGBUS blocked, and one that jumped to non-executable memory never ended its process.+//+// clang -o faultmask faultmask.c && ./faultmask+//+// A child process takes the fault on such a thread (a jump into heap memory: SIGBUS on Darwin); this process watches it+// for two seconds and kills it. Prints one line per case and exits 0 when both behave as on a Mac:+// fault signals blocked: still faulting after 2 s+// fault signals unblocked: killed by signal 10+#include <stdio.h>+#include <stdlib.h>+#include <signal.h>+#include <pthread.h>+#include <unistd.h>+#include <sys/wait.h>++static int leave_fault_signals;++static void *worker(void *arg) {+ sigset_t set;+ sigfillset(&set);+ if (leave_fault_signals) { sigdelset(&set, SIGBUS); sigdelset(&set, SIGSEGV); sigdelset(&set, SIGILL); sigdelset(&set, SIGTRAP); }+ pthread_sigmask(SIG_BLOCK, &set, NULL);+ unsigned int *code = malloc(4096);+ for (int i = 0; i < 1024; i++) code[i] = 0xd65f03c0; // ret+ ((void (*)(void))code)(); // readable and writable, not executable+ return arg;+}++static int run(int leave) { // 0: the child was still faulting after 2 s; otherwise the signal that ended it; -1: neither+ pid_t pid = fork();+ if (pid == 0) {+ leave_fault_signals = leave;+ pthread_t t;+ pthread_create(&t, NULL, worker, NULL);+ pthread_join(t, NULL);+ _exit(3); // the jump returned: the heap was executable+ }+ int status = 0;+ for (int i = 0; i < 20; i++) {+ if (waitpid(pid, &status, WNOHANG) == pid) return WIFSIGNALED(status) ? WTERMSIG(status) : -1;+ usleep(100000);+ }+ kill(pid, SIGKILL);+ waitpid(pid, &status, 0);+ return 0;+}++int main(void) {+ int blocked = run(0), open = run(1);+ if (blocked == 0) printf("fault signals blocked: still faulting after 2 s\n");+ else printf("fault signals blocked: %s %d\n", blocked > 0 ? "killed by signal" : "ended, status", blocked);+ if (open > 0) printf("fault signals unblocked: killed by signal %d\n", open);+ else printf("fault signals unblocked: %s\n", open == 0 ? "still faulting after 2 s" : "ended without a signal");+ return blocked == 0 && open == SIGBUS ? 0 : 1;+}