From 33da24d0e25f36a08bc2907d4a997f9bc666700f Mon Sep 17 00:00:00 2001 From: "C.W. Betts" Date: Wed, 19 May 2021 15:42:50 -0600 Subject: [PATCH] macOS: Add support for Apple Silicon and usage of the MAP_JIT flag. --- src/386_dynarec.c | 9 +++++++++ src/codegen_allocator.c | 4 ++++ src/pc.c | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/src/386_dynarec.c b/src/386_dynarec.c index 9dedf44..5b7e8f4 100644 --- a/src/386_dynarec.c +++ b/src/386_dynarec.c @@ -1,6 +1,9 @@ #include #include #include +#if defined __APPLE__ && defined __aarch64__ +#include +#endif #include "ibm.h" #include "x86.h" #include "x86_ops.h" @@ -410,6 +413,9 @@ static inline void exec_recompiler(void) cpu_new_blocks++; +#if defined __APPLE__ && defined __aarch64__ + pthread_jit_write_protect_np(0); +#endif codegen_block_start_recompile(block); codegen_in_recompile = 1; @@ -481,6 +487,9 @@ static inline void exec_recompiler(void) codegen_reset(); codegen_in_recompile = 0; +#if defined __APPLE__ && defined __aarch64__ + pthread_jit_write_protect_np(1); +#endif } else if (!cpu_state.abrt) { diff --git a/src/codegen_allocator.c b/src/codegen_allocator.c index 5807b5d..a5301d1 100644 --- a/src/codegen_allocator.c +++ b/src/codegen_allocator.c @@ -30,6 +30,10 @@ void codegen_allocator_init() #if defined WIN32 || defined _WIN32 || defined _WIN32 mem_block_alloc = VirtualAlloc(NULL, MEM_BLOCK_NR * MEM_BLOCK_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE); + /* TODO: check deployment target: older Intel-based versions of macOS don't play + nice with MAP_JIT. */ +#elif defined(__APPLE__) && defined(MAP_JIT) + mem_block_alloc = mmap(0, MEM_BLOCK_NR * MEM_BLOCK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE|MAP_JIT, 0, 0); #else mem_block_alloc = mmap(0, MEM_BLOCK_NR * MEM_BLOCK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, 0, 0); #endif diff --git a/src/pc.c b/src/pc.c index 6ed0be9..f362378 100644 --- a/src/pc.c +++ b/src/pc.c @@ -1,6 +1,9 @@ #include #include #include +#if defined(__APPLE__) && defined(__aarch64__) +#include +#endif #include "ibm.h" #include "device.h" @@ -307,7 +310,13 @@ void initpc(int argc, char *argv[]) loadbios(); mem_add_bios(); +#if defined(__APPLE__) && defined(__aarch64__) + pthread_jit_write_protect_np(0); +#endif codegen_init(); +#if defined(__APPLE__) && defined(__aarch64__) + pthread_jit_write_protect_np(1); +#endif timer_reset(); sound_reset();