[A64/Function] Initialize macOS JIT threads before thunk

- Add ARM64 macOS thread-local JIT init using pthread_jit_write_protect_np

- Ensure CallImpl enables execute mode before host->guest thunk
This commit is contained in:
Will Martin
2026-01-21 09:20:31 +09:00
parent eb15c34234
commit b18442e197
+30
View File
@@ -9,10 +9,31 @@
#include "xenia/cpu/backend/a64/a64_function.h"
#ifdef XE_PLATFORM_MAC
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <mach/vm_map.h>
#include <pthread.h>
#endif
#include "xenia/base/logging.h"
#include "xenia/cpu/backend/a64/a64_backend.h"
#include "xenia/cpu/processor.h"
#include "xenia/cpu/thread_state.h"
#if XE_PLATFORM_MAC && defined(__aarch64__)
thread_local bool jit_thread_initialized = false;
// Initialize JIT execution for the current thread
static void InitializeJITThread() {
if (!jit_thread_initialized) {
// Ensure this thread can execute JIT code by setting execute mode
pthread_jit_write_protect_np(1); // Enable execute, disable write
jit_thread_initialized = true;
}
}
#endif
namespace xe {
namespace cpu {
namespace backend {
@@ -31,11 +52,20 @@ void A64Function::Setup(uint8_t* machine_code, size_t machine_code_length) {
}
bool A64Function::CallImpl(ThreadState* thread_state, uint32_t return_address) {
#if XE_PLATFORM_MAC && defined(__aarch64__)
// Initialize JIT execution for this thread
// This ensures pthread_jit_write_protect_np is set correctly for execution
InitializeJITThread();
#endif
auto backend =
reinterpret_cast<A64Backend*>(thread_state->processor()->backend());
auto thunk = backend->host_to_guest_thunk();
// Make the actual thunk call
thunk(machine_code_, thread_state->context(),
reinterpret_cast<void*>(uintptr_t(return_address)));
return true;
}