From c4884040ee91cb10cdf12e75c86a557c940b2d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 28 Sep 2026 13:54:23 -0600 Subject: [PATCH 1/3] Mac/iOS: Hook up the memory fault handler, including on ARM64 Apple platforms now use the same SIGSEGV/SIGBUS handler as Linux instead of a Mach exception port. The Mach port was only set on the installing thread, which is the loader thread, not the one running JIT code. ARM64 had no context definition at all. Also pass the thread state (not the mcontext) to the handler, accept SIGBUS fault codes, and fix restoring a disabled altstack on Darwin. Co-Authored-By: Claude Opus 5.5 (1M context) --- Common/ExceptionHandlerSetup.cpp | 139 +++---------------------------- Common/ExceptionHandlerSetup.h | 2 +- Common/MachineContext.h | 25 ++++-- 3 files changed, 29 insertions(+), 137 deletions(-) diff --git a/Common/ExceptionHandlerSetup.cpp b/Common/ExceptionHandlerSetup.cpp index 2cdb36a87f..fa2b57926f 100644 --- a/Common/ExceptionHandlerSetup.cpp +++ b/Common/ExceptionHandlerSetup.cpp @@ -10,13 +10,11 @@ #include #include #include -#include #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/Log.h" #include "Common/StringUtils.h" -#include "Common/Thread/ThreadUtil.h" #include "Common/MachineContext.h" #include "Common/ExceptionHandlerSetup.h" @@ -186,123 +184,6 @@ void UninstallExceptionHandler() { g_badAccessHandler = nullptr; } -#elif defined(__APPLE__) - -static void CheckKR(const char* name, kern_return_t kr) { - _assert_msg_(kr == 0, "%s failed: kr=%x", name, kr); -} - -static void ExceptionThread(mach_port_t port) { - SetCurrentThreadName("Mach exception thread"); -#pragma pack(4) - struct { - mach_msg_header_t Head; - NDR_record_t NDR; - exception_type_t exception; - mach_msg_type_number_t codeCnt; - int64_t code[2]; - int flavor; - mach_msg_type_number_t old_stateCnt; - natural_t old_state[x86_THREAD_STATE64_COUNT]; - mach_msg_trailer_t trailer; - } msg_in; - - struct { - mach_msg_header_t Head; - NDR_record_t NDR; - kern_return_t RetCode; - int flavor; - mach_msg_type_number_t new_stateCnt; - natural_t new_state[x86_THREAD_STATE64_COUNT]; - } msg_out; -#pragma pack() - memset(&msg_in, 0xee, sizeof(msg_in)); - memset(&msg_out, 0xee, sizeof(msg_out)); - mach_msg_header_t* send_msg = &msg_out.Head; - mach_msg_size_t send_size = 0; - mach_msg_option_t option = MACH_RCV_MSG; - while (true) { - // If this isn't the first run, send the reply message. Then, receive - // a message: either a mach_exception_raise_state RPC due to - // thread_set_exception_ports, or MACH_NOTIFY_NO_SENDERS due to - // mach_port_request_notification. - CheckKR("mach_msg_overwrite", - mach_msg_overwrite(send_msg, option, send_size, sizeof(msg_in), port, - MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL, &msg_in.Head, 0)); - - if (msg_in.Head.msgh_id == MACH_NOTIFY_NO_SENDERS) { - // the other thread exited - mach_port_destroy(mach_task_self(), port); - return; - } - - _assert_msg_(msg_in.Head.msgh_id == 2406, "unknown message received"); - _assert_msg_(msg_in.flavor == x86_THREAD_STATE64, "unknown flavor %d (expected %d)", msg_in.flavor, x86_THREAD_STATE64); - - x86_thread_state64_t* state = (x86_thread_state64_t*)msg_in.old_state; - - bool ok = g_badAccessHandler((uintptr_t)msg_in.code[1], state); - - // Set up the reply. - msg_out.Head.msgh_bits = MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(msg_in.Head.msgh_bits), 0); - msg_out.Head.msgh_remote_port = msg_in.Head.msgh_remote_port; - msg_out.Head.msgh_local_port = MACH_PORT_NULL; - msg_out.Head.msgh_id = msg_in.Head.msgh_id + 100; - msg_out.NDR = msg_in.NDR; - if (ok) { - msg_out.RetCode = KERN_SUCCESS; - msg_out.flavor = x86_THREAD_STATE64; - msg_out.new_stateCnt = x86_THREAD_STATE64_COUNT; - memcpy(msg_out.new_state, msg_in.old_state, x86_THREAD_STATE64_COUNT * sizeof(natural_t)); - } else { - // Pass the exception to the next handler (debugger or crash). - msg_out.RetCode = KERN_FAILURE; - msg_out.flavor = 0; - msg_out.new_stateCnt = 0; - } - msg_out.Head.msgh_size = - offsetof(__typeof__(msg_out), new_state) + msg_out.new_stateCnt * sizeof(natural_t); - - send_msg = &msg_out.Head; - send_size = msg_out.Head.msgh_size; - option |= MACH_SEND_MSG; - } -} - -void InstallExceptionHandler(BadAccessHandler badAccessHandler, bool logStackTraceOnCrash) { - if (g_badAccessHandler) { - // The rest of the setup we don't need to do again. - g_badAccessHandler = badAccessHandler; - return; - } - g_badAccessHandler = badAccessHandler; - - INFO_LOG(Log::System, "Installing exception handler"); - mach_port_t port; - CheckKR("mach_port_allocate", - mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port)); - std::thread exc_thread(ExceptionThread, port); - exc_thread.detach(); - // Obtain a send right for thread_set_exception_ports to copy... - CheckKR("mach_port_insert_right", - mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND)); - // Mach tries the following exception ports in order: thread, task, host. - // Debuggers set the task port, so we grab the thread port. - CheckKR("thread_set_exception_ports", - thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, port, - EXCEPTION_STATE | MACH_EXCEPTION_CODES, x86_THREAD_STATE64)); - // ...and get rid of our copy so that MACH_NOTIFY_NO_SENDERS works. - CheckKR("mach_port_mod_refs", - mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, -1)); - mach_port_t previous; - CheckKR("mach_port_request_notification", - mach_port_request_notification(mach_task_self(), port, MACH_NOTIFY_NO_SENDERS, 0, port, - MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous)); -} - -void UninstallExceptionHandler() { -} - #else #include @@ -341,7 +222,9 @@ static void sigsegv_handler(int sig, siginfo_t* info, void* raw_context) { } ucontext_t* context = (ucontext_t*)raw_context; int sicode = info->si_code; - if (sicode != SEGV_MAPERR && sicode != SEGV_ACCERR) { + // Darwin reports some faults (e.g. on PROT_NONE pages) as SIGBUS. + bool addressFault = sig == SIGSEGV ? (sicode == SEGV_MAPERR || sicode == SEGV_ACCERR) : (sicode == BUS_ADRERR || sicode == BUS_ADRALN); + if (!addressFault) { // Not an address fault we can do anything with - an MTE, protection-key or shadow // stack fault, or a signal sent with kill(). Returning here would re-run the // faulting instruction forever at 100% CPU, and would also swallow the signal from @@ -354,17 +237,13 @@ static void sigsegv_handler(int sig, siginfo_t* info, void* raw_context) { // Get all the information we can out of the context. #ifdef __OpenBSD__ ucontext_t* ctx = context; +#elif defined(__APPLE__) + // uc_mcontext is a pointer here, and the registers are in its thread state. + SContext* ctx = &context->uc_mcontext->__ss; #else mcontext_t* ctx = &context->uc_mcontext; #endif - // assume it's not a write - if (!g_badAccessHandler(bad_address, -#ifdef __APPLE__ - *ctx -#else - ctx -#endif - )) { + if (!g_badAccessHandler(bad_address, ctx)) { // retry and crash // According to the sigaction man page, if sa_flags "SA_SIGINFO" is set to the sigaction // function pointer, otherwise sa_handler contains one of: @@ -424,6 +303,10 @@ void UninstallExceptionHandler() { return; } if (old_signal_stack_valid) { + // Darwin fails with ENOMEM on a size below MINSIGSTKSZ, even with SS_DISABLE. + if ((old_signal_stack.ss_flags & SS_DISABLE) && old_signal_stack.ss_size < MINSIGSTKSZ) { + old_signal_stack.ss_size = MINSIGSTKSZ; + } if (0 != sigaltstack(&old_signal_stack, nullptr)) { ERROR_LOG(Log::System, "Could not restore previous signal altstack"); } diff --git a/Common/ExceptionHandlerSetup.h b/Common/ExceptionHandlerSetup.h index caaccaee8b..87d036f659 100644 --- a/Common/ExceptionHandlerSetup.h +++ b/Common/ExceptionHandlerSetup.h @@ -7,7 +7,7 @@ #include // On Windows, context is a CONTEXT object. -// On Apple, context is a x86_thread_state64_t. +// On Apple, context is the thread state from the signal context (x86_thread_state64_t or arm_thread_state64_t). // On Unix/Linux, context is a mcontext_t. // On OpenBSD, context is a ucontext_t. // Ugh, might need to abstract this better. diff --git a/Common/MachineContext.h b/Common/MachineContext.h index b704b2e6a2..f8c2a4b1cc 100644 --- a/Common/MachineContext.h +++ b/Common/MachineContext.h @@ -69,20 +69,17 @@ typedef CONTEXT SContext; #endif -#elif PPSSPP_PLATFORM(MAC) +#elif PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) -// for modules: -#define _XOPEN_SOURCE -#include - -#include -#include +// The context handed to the fault handler is the thread state inside the signal's mcontext +// (uc_mcontext->__ss). gives us that without the _XOPEN_SOURCE that demands. +#include #if PPSSPP_ARCH(AMD64) #define MACHINE_CONTEXT_SUPPORTED -typedef x86_thread_state64_t SContext; +typedef _STRUCT_X86_THREAD_STATE64 SContext; #define CTX_RAX __rax #define CTX_RBX __rbx #define CTX_RCX __rcx @@ -101,6 +98,18 @@ typedef x86_thread_state64_t SContext; #define CTX_R15 __r15 #define CTX_RIP __rip +#elif PPSSPP_ARCH(ARM64) && !__DARWIN_OPAQUE_ARM_THREAD_STATE64 + +// (The opaque, pointer-authenticated thread state is arm64e only.) +#define MACHINE_CONTEXT_SUPPORTED + +typedef _STRUCT_ARM_THREAD_STATE64 SContext; + +// Only valid for x0-x28. x29 and x30 are __fp and __lr. +#define CTX_REG(x) __x[x] +#define CTX_SP __sp +#define CTX_PC __pc + #else // No context definition for architecture From aad83c044f46acd783fd826a5468c0a0d56c6139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 28 Sep 2026 13:58:29 -0600 Subject: [PATCH 2/3] docs: Native debuggers vs. the memory fault handler How to keep lldb and gdb from stopping on the faults the handler is meant to catch, and the crash_*.prx tests as a quick check that it works. Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/debugging.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/debugging.md b/docs/debugging.md index 319ac06f43..81168851ff 100644 --- a/docs/debugging.md +++ b/docs/debugging.md @@ -226,6 +226,32 @@ Concretely, as measured against the headless build (2026-08-16), per CPU backend | `memory.breakpoint.*` (memchecks) | works | **only for constant addresses** | | `cpu.regBreakpoint.*` | works | never trips (as documented) | +## Native debuggers and the memory fault handler + +With fast memory on, a bad guest access from JIT code is a real host SIGSEGV/SIGBUS (an access violation on +Windows), which `Memory::HandleFault` (`Core/MemFault.cpp`) catches and turns into a clean emulator-side +exception. A native debugger sees the fault first and stops there, so a run that would have reported +`Read Word: SIGSEGV at ...` looks like a crash instead. Continuing hands the fault to the handler, like a +first-chance exception in Visual Studio. To keep the debugger out of the way when the faults are expected: + +- **lldb (macOS/iOS)** needs both of these, verified 2026-09-28 against `pspautotests/tests/cpu/crash`. The + first skips the stop on the Mach-level EXC_BAD_ACCESS, the second the stop on the signal it turns into: + + ``` + settings set platform.plugin.darwin.ignored-exceptions EXC_BAD_ACCESS + process launch --stop-at-entry + process handle SIGSEGV SIGBUS -s false -n false -p true + continue + ``` + + (`process handle` needs a live process, hence the `--stop-at-entry`.) The catch is that a genuine host crash + then kills the process without stopping in the debugger. +- **gdb (Linux/Android)**: `handle SIGSEGV nostop noprint pass`. + +The `pspautotests/tests/cpu/crash/crash_*.prx` binaries each make one bad access (read, write, float, bad jump), +which makes them the quickest check that the handler works on a platform: run one with `-j` in headless. They +should print the guest exception and exit 0, not die with signal 11. + ## Debugging a game that works on hardware but not in PPSSPP First, turn on `bAutoSaveLoadSymbols` (`--auto-save-load-symbols` in headless): when homebrew ships its From c70fc6826125cc8d5149fdd5ba93d8602f4d1cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 28 Sep 2026 13:58:29 -0600 Subject: [PATCH 3/3] Remove leftovers of 32-bit ARM Windows support Co-Authored-By: Claude Opus 5.5 (1M context) --- .github/workflows/manual_generate_uwp.yml | 3 +-- Common/GPU/ShaderTranslation.cpp | 5 ----- Common/MachineContext.h | 6 ------ Core/MIPS/ARM/ArmCompVFPU.cpp | 5 ----- Core/Reporting.cpp | 2 -- Windows/MainWindow.cpp | 4 ---- ppsspp_config.h | 10 +--------- 7 files changed, 2 insertions(+), 33 deletions(-) diff --git a/.github/workflows/manual_generate_uwp.yml b/.github/workflows/manual_generate_uwp.yml index 10ea95311b..8995d81ce1 100644 --- a/.github/workflows/manual_generate_uwp.yml +++ b/.github/workflows/manual_generate_uwp.yml @@ -21,7 +21,6 @@ on: - Bundle - x64 - ARM64 - - ARM signedPackage: type: boolean @@ -101,7 +100,7 @@ jobs: if: github.event.inputs.buildPlatform == 'Bundle' run: | echo "include symbols = ${{ env.INCLUDE_SYMBOLS }}" - msbuild UWP/PPSSPP_UWP.sln /m /p:TrackFileAccess=false /p:Configuration=${{ github.event.inputs.buildConfiguration }} /p:Platform=x64 /p:IncludeSymbols=${{ env.INCLUDE_SYMBOLS }} /p:AppxSymbolPackageEnabled=${{ env.INCLUDE_SYMBOLS }} /p:AppxPackageSigningEnabled=${{ github.event.inputs.signedPackage }} /p:PackageCertificateKeyFile=PPSSPP_UWP_TemporaryKey.pfx /p:AppxBundle=Always /p:UapAppxPackageBuildMode=SideloadOnly /p:AppxBundlePlatforms="x64|ARM64|ARM" + msbuild UWP/PPSSPP_UWP.sln /m /p:TrackFileAccess=false /p:Configuration=${{ github.event.inputs.buildConfiguration }} /p:Platform=x64 /p:IncludeSymbols=${{ env.INCLUDE_SYMBOLS }} /p:AppxSymbolPackageEnabled=${{ env.INCLUDE_SYMBOLS }} /p:AppxPackageSigningEnabled=${{ github.event.inputs.signedPackage }} /p:PackageCertificateKeyFile=PPSSPP_UWP_TemporaryKey.pfx /p:AppxBundle=Always /p:UapAppxPackageBuildMode=SideloadOnly /p:AppxBundlePlatforms="x64|ARM64" - name: Package build working-directory: ${{ github.workspace }} diff --git a/Common/GPU/ShaderTranslation.cpp b/Common/GPU/ShaderTranslation.cpp index c9a2b0029b..fd36a6419b 100644 --- a/Common/GPU/ShaderTranslation.cpp +++ b/Common/GPU/ShaderTranslation.cpp @@ -29,11 +29,6 @@ #undef realloc #endif -// Weird issue -#if PPSSPP_PLATFORM(WINDOWS) && PPSSPP_ARCH(ARM) -#undef free -#endif - #include "Common/Log.h" #include "Common/StringUtils.h" #include "Common/GPU/Shader.h" diff --git a/Common/MachineContext.h b/Common/MachineContext.h index f8c2a4b1cc..f581471d0e 100644 --- a/Common/MachineContext.h +++ b/Common/MachineContext.h @@ -61,12 +61,6 @@ typedef CONTEXT SContext; #define CTX_SP Sp #define CTX_PC Pc -#elif PPSSPP_ARCH(ARM) - -//#define CTX_REG(x) R##x -#define CTX_SP Sp -#define CTX_PC Pc - #endif #elif PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) diff --git a/Core/MIPS/ARM/ArmCompVFPU.cpp b/Core/MIPS/ARM/ArmCompVFPU.cpp index c4b51f7b13..4afda56d61 100644 --- a/Core/MIPS/ARM/ArmCompVFPU.cpp +++ b/Core/MIPS/ARM/ArmCompVFPU.cpp @@ -2215,7 +2215,6 @@ namespace MIPSComp // Very heavily used by FF:CC. Should be replaced by a fast approximation instead of // calling the math library. - // Apparently this may not work on hardfp. I don't think we have any platforms using this though. void ArmJit::Comp_VRot(MIPSOpcode op) { // VRot probably doesn't accept prefixes anyway. CONDITIONAL_DISABLE(VFPU_VEC); @@ -2223,10 +2222,6 @@ namespace MIPSComp DISABLE; } -#if PPSSPP_ARCH(ARM_HARDFP) - DISABLE; -#endif - int vd = _VD; int vs = _VS; diff --git a/Core/Reporting.cpp b/Core/Reporting.cpp index 67b61949a9..2a767cdcf1 100644 --- a/Core/Reporting.cpp +++ b/Core/Reporting.cpp @@ -328,8 +328,6 @@ namespace Reporting return "Windows ARM64"; #elif defined(_WIN64) return "Windows 64"; -#elif defined(_WIN32) && defined(_M_ARM) - return "Windows ARM32"; #elif defined(_WIN32) return "Windows"; #elif PPSSPP_PLATFORM(IOS) diff --git a/Windows/MainWindow.cpp b/Windows/MainWindow.cpp index 000bdbb711..9ca51ade99 100644 --- a/Windows/MainWindow.cpp +++ b/Windows/MainWindow.cpp @@ -63,9 +63,7 @@ #include "Common/GPU/GraphicsContext.h" #include "Windows/main.h" -#ifndef _M_ARM #include "Windows/DinputDevice.h" -#endif #include "Windows/resource.h" #include "Windows/MainWindow.h" @@ -1002,9 +1000,7 @@ namespace MainWindow { return WindowsRawInput::ProcessChar(hWnd, wParam, lParam); case WM_DEVICECHANGE: -#ifndef _M_ARM DinputDevice::CheckDevices(); -#endif if (winCamera) winCamera->CheckDevices(); if (winMic) diff --git a/ppsspp_config.h b/ppsspp_config.h index 7984a3d1b8..f35e1493b8 100644 --- a/ppsspp_config.h +++ b/ppsspp_config.h @@ -32,7 +32,7 @@ #endif #endif -#if defined(__arm__) || defined(_M_ARM) +#if defined(__arm__) #define PPSSPP_ARCH_ARM 1 #define PPSSPP_ARCH_32BIT 1 @@ -49,11 +49,6 @@ #if defined(__ARM_NEON) || defined(__ARM_NEON__) #define PPSSPP_ARCH_ARM_NEON 1 #endif - - #if defined(_M_ARM) - #define PPSSPP_ARCH_ARMV7 1 - #define PPSSPP_ARCH_ARM_NEON 1 - #endif #endif #if defined(__aarch64__) || defined(_M_ARM64) @@ -94,9 +89,6 @@ #if defined(_WIN32) // Covers both 32 and 64bit Windows #define PPSSPP_PLATFORM_WINDOWS 1 - #ifdef _M_ARM - #define PPSSPP_ARCH_ARM_HARDFP 1 - #endif // UWP trickery #if defined(WINAPI_FAMILY) && defined(WINAPI_FAMILY_PARTITION) #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP