diff --git a/.gitignore b/.gitignore index 253d22b4a..fb219d592 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ Release/ new_dynarec_debug.txt new_dynarec_disasm.txt +*.zip diff --git a/libretro/libretro.c b/libretro/libretro.c index 6b086f3da..5e3506764 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -2085,10 +2085,6 @@ static void format_saved_memory(void) bool retro_load_game(const struct retro_game_info *game) { -#if defined(__APPLE__) && defined(__arm64__) - extern void new_dynarec_create_mapping(); - new_dynarec_create_mapping(); -#endif format_saved_memory(); update_variables(true); diff --git a/mupen64plus-core/src/memory/m64p_memory.c b/mupen64plus-core/src/memory/m64p_memory.c index bc3853ea0..ab6385704 100644 --- a/mupen64plus-core/src/memory/m64p_memory.c +++ b/mupen64plus-core/src/memory/m64p_memory.c @@ -51,7 +51,7 @@ #include #include -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) #if NEW_DYNAREC < NEW_DYNAREC_ARM // address : address of the read/write operation being done uint32_t address = 0; diff --git a/mupen64plus-core/src/memory/memory.h b/mupen64plus-core/src/memory/memory.h index abcb5fed8..7d566e739 100644 --- a/mupen64plus-core/src/memory/memory.h +++ b/mupen64plus-core/src/memory/memory.h @@ -42,14 +42,14 @@ #define write_hword_in_memory() writememh[mupencoreaddress >>16]() #define write_dword_in_memory() writememd[mupencoreaddress >>16]() -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) extern uint32_t address, cpu_word; extern uint8_t cpu_byte; extern uint16_t cpu_hword; extern uint64_t cpu_dword; #define mupencoreaddress address #else -#include "../r4300/new_dynarec/arm64/apple_memory_layout.h" +#include "../r4300/new_dynarec/arm64/memory_layout_arm64.h" #define mupencoreaddress (RECOMPILER_MEMORY->rml_address) #define cpu_word (RECOMPILER_MEMORY->rml_cpu_word) #define cpu_byte (RECOMPILER_MEMORY->rml_cpu_byte) diff --git a/mupen64plus-core/src/r4300/cp0.c b/mupen64plus-core/src/r4300/cp0.c index 057e9b541..93b8d6ddd 100644 --- a/mupen64plus-core/src/r4300/cp0.c +++ b/mupen64plus-core/src/r4300/cp0.c @@ -34,7 +34,7 @@ #endif /* global variable */ -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) #if NEW_DYNAREC < NEW_DYNAREC_ARM /* ARM backend requires a different memory layout * and therefore manually allocate that variable */ diff --git a/mupen64plus-core/src/r4300/cp0_private.h b/mupen64plus-core/src/r4300/cp0_private.h index 54ad7bc73..70000ac75 100644 --- a/mupen64plus-core/src/r4300/cp0_private.h +++ b/mupen64plus-core/src/r4300/cp0_private.h @@ -24,10 +24,10 @@ #include "cp0.h" -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) extern uint32_t g_cp0_regs[CP0_REGS_COUNT]; #else -#include "new_dynarec/arm64/apple_memory_layout.h" +#include "new_dynarec/arm64/memory_layout_arm64.h" #define g_cp0_regs (RECOMPILER_MEMORY->rml_g_cp0_regs) #endif diff --git a/mupen64plus-core/src/r4300/cp1.c b/mupen64plus-core/src/r4300/cp1.c index 58da2940a..7f93ba7b3 100644 --- a/mupen64plus-core/src/r4300/cp1.c +++ b/mupen64plus-core/src/r4300/cp1.c @@ -26,7 +26,7 @@ #include "new_dynarec/new_dynarec.h" -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) #if NEW_DYNAREC < NEW_DYNAREC_ARM float *reg_cop1_simple[32]; double *reg_cop1_double[32]; @@ -35,7 +35,7 @@ uint32_t FCR0, FCR31; #else /* ARM backend requires a different memory layout * and therefore manually allocates these variables */ -#include "new_dynarec/arm64/apple_memory_layout.h" +#include "new_dynarec/arm64/memory_layout_arm64.h" #define reg_cop1_simple (RECOMPILER_MEMORY->rml_reg_cop1_simple) #define reg_cop1_double (RECOMPILER_MEMORY->rml_reg_cop1_double) #define FCR0 (RECOMPILER_MEMORY->rml_FCR0) diff --git a/mupen64plus-core/src/r4300/cp1_private.h b/mupen64plus-core/src/r4300/cp1_private.h index 9930457bd..985a8963d 100644 --- a/mupen64plus-core/src/r4300/cp1_private.h +++ b/mupen64plus-core/src/r4300/cp1_private.h @@ -26,12 +26,12 @@ #include "cp1.h" -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) extern float *reg_cop1_simple[32]; extern double *reg_cop1_double[32]; extern uint32_t FCR0, FCR31; #else -#include "new_dynarec/arm64/apple_memory_layout.h" +#include "new_dynarec/arm64/memory_layout_arm64.h" #define reg_cop1_simple (RECOMPILER_MEMORY->rml_reg_cop1_simple) #define reg_cop1_double (RECOMPILER_MEMORY->rml_reg_cop1_double) #define FCR0 (RECOMPILER_MEMORY->rml_FCR0) diff --git a/mupen64plus-core/src/r4300/new_dynarec/arm64/assem_arm64.c b/mupen64plus-core/src/r4300/new_dynarec/arm64/assem_arm64.c index 488892a5b..cb33fe5b0 100644 --- a/mupen64plus-core/src/r4300/new_dynarec/arm64/assem_arm64.c +++ b/mupen64plus-core/src/r4300/new_dynarec/arm64/assem_arm64.c @@ -313,14 +313,10 @@ static void set_jump_target(intptr_t addr,uintptr_t target) static void *dynamic_linker_impl(void * src, u_int vaddr); static void *dynamic_linker(void * src, u_int vaddr) { -#if defined(__APPLE__) && defined(__arm64__) apple_jit_wx_unprotect_enter(); void* r = dynamic_linker_impl(src, vaddr); apple_jit_wx_unprotect_exit(); return r; -#else - return dynamic_linker_impl(src, vaddr); -#endif } static void *dynamic_linker_impl(void * src, u_int vaddr) @@ -427,14 +423,10 @@ static void *dynamic_linker_impl(void * src, u_int vaddr) static void *dynamic_linker_ds_impl(void * src, u_int vaddr); static void *dynamic_linker_ds(void * src, u_int vaddr) { -#if defined(__APPLE__) && defined(__arm64__) apple_jit_wx_unprotect_enter(); void* r = dynamic_linker_ds_impl(src, vaddr); apple_jit_wx_unprotect_exit(); return r; -#else - return dynamic_linker_ds_impl(src, vaddr); -#endif } static void *dynamic_linker_ds_impl(void * src, u_int vaddr) @@ -6763,6 +6755,7 @@ static void arch_init(void) { jump_table_symbols[18] = (intptr_t) cached_interpreter_table.TLBP; #endif + trampoline_init(base_addr); apple_jit_wx_unprotect_enter(); trampolines_reg_jump_t jumps = trampoline_alloc_reg_jump(&jump_vaddr); apple_jit_wx_unprotect_exit(); diff --git a/mupen64plus-core/src/r4300/new_dynarec/arm64/assem_arm64.h b/mupen64plus-core/src/r4300/new_dynarec/arm64/assem_arm64.h index 457787143..c11674949 100644 --- a/mupen64plus-core/src/r4300/new_dynarec/arm64/assem_arm64.h +++ b/mupen64plus-core/src/r4300/new_dynarec/arm64/assem_arm64.h @@ -97,7 +97,7 @@ void write_rdramd_new(void); void breakpoint(void); #ifdef __APPLE__ -#include "apple_memory_layout.h" +#include "memory_layout_arm64.h" #define invc_ptr (RECOMPILER_MEMORY->rml_invc_ptr) #define cycle_count (RECOMPILER_MEMORY->rml_cycle_count) #define last_count (RECOMPILER_MEMORY->rml_last_count) diff --git a/mupen64plus-core/src/r4300/new_dynarec/arm64/linkage_aarch64.S b/mupen64plus-core/src/r4300/new_dynarec/arm64/linkage_aarch64.S index 0370b1db5..51435cb16 100644 --- a/mupen64plus-core/src/r4300/new_dynarec/arm64/linkage_aarch64.S +++ b/mupen64plus-core/src/r4300/new_dynarec/arm64/linkage_aarch64.S @@ -289,11 +289,8 @@ GLOBAL_FUNCTION(_jump_eret): b .E8 GLOBAL_FUNCTION(_new_dyna_start): - adrp x1, _base_addr@PAGE - add x1, x1, _base_addr@PAGEOFF - ldr x16, [x1] /* _extra_memory=base_addr */ - mov x17, #0x2000000 - add x16, x16, x17 /* _dynarec_local */ + adrp x16, _memory_layout@PAGE + add x16, x16, _memory_layout@PAGEOFF add x16, x16, #152 mov w0, #0xa4000000 stp x19,x20,[x16,#0] @@ -303,6 +300,8 @@ GLOBAL_FUNCTION(_new_dyna_start): stp x27,x28,[x16,#64] stp x29,x30,[x16,#80] sub x29, x16, #152 + adrp x1, _base_addr@PAGE + add x1, x1, _base_addr@PAGEOFF ldr x19, [x1] add w0, w0, #0x40 bl _new_recompile_block diff --git a/mupen64plus-core/src/r4300/new_dynarec/arm64/apple_memory_layout.h b/mupen64plus-core/src/r4300/new_dynarec/arm64/memory_layout_arm64.h similarity index 96% rename from mupen64plus-core/src/r4300/new_dynarec/arm64/apple_memory_layout.h rename to mupen64plus-core/src/r4300/new_dynarec/arm64/memory_layout_arm64.h index 538c171ca..d49785ae3 100644 --- a/mupen64plus-core/src/r4300/new_dynarec/arm64/apple_memory_layout.h +++ b/mupen64plus-core/src/r4300/new_dynarec/arm64/memory_layout_arm64.h @@ -1,13 +1,15 @@ -#pragma once +#ifndef M64P_MEMORY_LAYOUT_ARM64_H +#define M64P_MEMORY_LAYOUT_ARM64_H -#ifdef __APPLE__ #include +#ifdef __APPLE__ #include +#endif + #include "../../recomp_types.h" typedef struct recompiler_memory_layout { - char rml_extra_memory[0x2000000]; // rwx memory char rml_dynarec_local[256]; uint32_t rml_next_interrupt; int rml_cycle_count; @@ -44,12 +46,12 @@ typedef struct recompiler_memory_layout uint64_t rml_memory_map[1048576]; } recompiler_memory_layout_t; -extern recompiler_memory_layout_t* base_addr; -#define RECOMPILER_MEMORY (base_addr) +extern recompiler_memory_layout_t memory_layout; +#define RECOMPILER_MEMORY (&memory_layout) // Copied from linkage_aarch64.S declaration #define RML_SIZE_EXTRA_MEMORY 0 -#define RML_SIZE_DYNAREC_LOCAL RML_SIZE_EXTRA_MEMORY + 33554432 +#define RML_SIZE_DYNAREC_LOCAL RML_SIZE_EXTRA_MEMORY + 0 #define RML_SIZE_NEXT_INTERRUPT RML_SIZE_DYNAREC_LOCAL + 256 #define RML_SIZE_CYCLE_COUNT RML_SIZE_NEXT_INTERRUPT + 4 #define RML_SIZE_LAST_COUNT RML_SIZE_CYCLE_COUNT + 4 @@ -82,6 +84,7 @@ extern recompiler_memory_layout_t* base_addr; #define RML_SIZE_LINK_REGISTER RML_SIZE_INSTR_ADDR + 8 #define RML_SIZE_MEMORY_MAP RML_SIZE_LINK_REGISTER + 8 +#if __STDC_VERSION__ > 201112L #define RML_CHECK_SIZE(name, off) _Static_assert(offsetof(recompiler_memory_layout_t, name) == (off), "Recompiler layout for '" #name "' does not match asm") RML_CHECK_SIZE(rml_dynarec_local , RML_SIZE_DYNAREC_LOCAL); RML_CHECK_SIZE(rml_next_interrupt , RML_SIZE_NEXT_INTERRUPT); @@ -116,5 +119,6 @@ RML_CHECK_SIZE(rml_instr_addr , RML_SIZE_INSTR_ADDR); RML_CHECK_SIZE(rml_link_register , RML_SIZE_LINK_REGISTER); RML_CHECK_SIZE(rml_memory_map , RML_SIZE_MEMORY_MAP); #undef RML_CHECK_SIZE +#endif #endif diff --git a/mupen64plus-core/src/r4300/new_dynarec/new_dynarec.h b/mupen64plus-core/src/r4300/new_dynarec/new_dynarec.h index c841457bf..0da5874a6 100644 --- a/mupen64plus-core/src/r4300/new_dynarec/new_dynarec.h +++ b/mupen64plus-core/src/r4300/new_dynarec/new_dynarec.h @@ -30,7 +30,7 @@ #define NEW_DYNAREC_ARM 3 #define NEW_DYNAREC_ARM64 4 -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) #ifdef __cplusplus extern "C" { #endif @@ -40,7 +40,7 @@ extern int pending_exception; } #endif #else -#include "arm64/apple_memory_layout.h" +#include "arm64/memory_layout_arm64.h" #define pcaddr (RECOMPILER_MEMORY->rml_pcaddr) #define pending_exception (RECOMPILER_MEMORY->rml_pending_exception) #endif diff --git a/mupen64plus-core/src/r4300/new_dynarec/new_dynarec_64.c b/mupen64plus-core/src/r4300/new_dynarec/new_dynarec_64.c index ae6408fa9..9fd1a64bc 100644 --- a/mupen64plus-core/src/r4300/new_dynarec/new_dynarec_64.c +++ b/mupen64plus-core/src/r4300/new_dynarec/new_dynarec_64.c @@ -63,7 +63,7 @@ #include "arm/assem_arm.h" #elif NEW_DYNAREC == NEW_DYNAREC_ARM64 #include "arm64/apple_jit_protect.h" -#include "arm64/apple_memory_layout.h" +#include "arm64/memory_layout_arm64.h" #include "arm64/assem_arm64.h" #else #error Unsupported dynarec architecture @@ -107,11 +107,10 @@ struct ll_entry struct ll_entry *next; }; -#ifdef __APPLE__ -recompiler_memory_layout_t* base_addr = NULL; -#else -void *base_addr = NULL; +#ifdef __arm64__ +recompiler_memory_layout_t memory_layout; #endif +void *base_addr = NULL; u_char *out; ALIGN(16, uintptr_t hash_table[65536][4]); @@ -1305,6 +1304,7 @@ void invalidate_block(u_int block) #ifdef USE_MINI_HT memset(mini_ht,-1,sizeof(mini_ht)); #endif + apple_jit_wx_unprotect_exit(); } @@ -7752,57 +7752,6 @@ static void disassemble_inst(int i) } #endif -#if defined(__APPLE__) && defined(__arm64__) -#include "arm64/trampoline_arm64.h" - -void new_dynarec_create_mapping(void) -{ - if (base_addr) - return; - - // For Mac ARM (and it can be ported to other ARM64 platforms), we want a special layout - // <- 0x2000000 -> | - // -------------------|-------------------|------------------- - // RW^X ^ RW^X ^ RW - // base_addr dynarec_local - - // pre base_addr is trampolines we need to have, growing like a stack downwards - // after base_addr is normal JIT stuff generated - // We also want addresses to be in 32-bit space because there are some casts to 32bit unsigned int - - // On macOS we are not allowed to use MAP_FIXED with MAP_JIT so allocate first 2 segments together, then use MAP_FIXED for RW segment - // This is a stupid hack - we want part of mapping to be RWX and other part of map to be RW - // We hope that the 2nd map wont fail but really this should just do it couple hundred times - -#define ATTEMPTS_COUNT 1000 - - for (int i = 0; i < ATTEMPTS_COUNT; i++) - { - char* addr = (char*) mmap(NULL, 2 * (1<= NEW_DYNAREC_ARM - if ((base_addr = mmap ((u_char *)BASE_ADDR, 1<= NEW_DYNAREC_ARM + if ((base_addr = mmap ((u_char *)BASE_ADDR, 1<>12],memory_map[(start+4096)>>12]); - __builtin_debugtrap(); return 1; // Caller will invoke exception handler } //DebugMessage(M64MSG_VERBOSE, "source= %x",(intptr_t)source); diff --git a/mupen64plus-core/src/r4300/pure_interp.c b/mupen64plus-core/src/r4300/pure_interp.c index ebb362959..762a31ffc 100644 --- a/mupen64plus-core/src/r4300/pure_interp.c +++ b/mupen64plus-core/src/r4300/pure_interp.c @@ -21,10 +21,10 @@ #include -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) #define mupencorereg reg #else -#include "new_dynarec/arm64/apple_memory_layout.h" +#include "new_dynarec/arm64/memory_layout_arm64.h" #define mupencorereg (RECOMPILER_MEMORY->rml_reg) #endif diff --git a/mupen64plus-core/src/r4300/r4300.c b/mupen64plus-core/src/r4300/r4300.c index cb9cc589f..ca1ecb767 100644 --- a/mupen64plus-core/src/r4300/r4300.c +++ b/mupen64plus-core/src/r4300/r4300.c @@ -50,7 +50,7 @@ unsigned int r4300emu = 0; unsigned int count_per_op = COUNT_PER_OP_DEFAULT; unsigned int llbit; -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) int stop; #if NEW_DYNAREC < NEW_DYNAREC_ARM int64_t reg[32], hi, lo; diff --git a/mupen64plus-core/src/r4300/r4300.h b/mupen64plus-core/src/r4300/r4300.h index d9cd76332..b1dc4e3a1 100644 --- a/mupen64plus-core/src/r4300/r4300.h +++ b/mupen64plus-core/src/r4300/r4300.h @@ -28,7 +28,7 @@ #include "r4300_core.h" #include "recomp.h" -#if !defined(__APPLE__) || !defined(__arm64__) +#if !defined(__arm64__) extern struct precomp_instr *PC; extern int64_t reg[32], hi, lo; extern uint32_t next_interrupt; @@ -37,7 +37,7 @@ extern int stop; #define mupencorereg reg #define mupencorestop stop #else -#include "new_dynarec/arm64/apple_memory_layout.h" +#include "new_dynarec/arm64/memory_layout_arm64.h" #define mupencorePC (RECOMPILER_MEMORY->rml_PC) #define mupencorereg (RECOMPILER_MEMORY->rml_reg) #define hi (RECOMPILER_MEMORY->rml_hi)