mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-12-03 03:03:50 +01:00
Merge pull request #22376 from hrydgard/mac-memory-fault-handler
Mac memory fault handler, remove remains of support for Win32 ARM
This commit is contained in:
10 files changed
+57
-170
No files matched your search
@@ -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 }}
|
||||
|
||||
@@ -10,13 +10,11 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
||||
#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 <signal.h>
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <cstdint>
|
||||
|
||||
// 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.
|
||||
|
||||
@@ -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"
|
||||
|
||||
+17
-14
@@ -61,28 +61,19 @@ 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)
|
||||
#elif PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS)
|
||||
|
||||
// for modules:
|
||||
#define _XOPEN_SOURCE
|
||||
#include <ucontext.h>
|
||||
|
||||
#include <mach/mach.h>
|
||||
#include <mach/message.h>
|
||||
// The context handed to the fault handler is the thread state inside the signal's mcontext
|
||||
// (uc_mcontext->__ss). <sys/ucontext.h> gives us that without the _XOPEN_SOURCE that <ucontext.h> demands.
|
||||
#include <sys/ucontext.h>
|
||||
|
||||
#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 +92,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
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-9
@@ -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
|
||||
|
||||
Reference in new issue
Block a user