From 2a014d73faf4cec54f8bf51134828173f0debfaa Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 14 Nov 2021 01:14:55 +0200 Subject: [PATCH] drc: new far call mechanism somewhat inspired by mupen64plus, but a bit different --- Makefile | 4 +- configure | 2 +- libpcsxcore/new_dynarec/assem_arm.c | 82 +++++++-------- libpcsxcore/new_dynarec/assem_arm.h | 20 +--- libpcsxcore/new_dynarec/assem_arm64.c | 55 +++++----- libpcsxcore/new_dynarec/assem_arm64.h | 17 ++-- libpcsxcore/new_dynarec/new_dynarec.c | 140 ++++++++++++++++++-------- 7 files changed, 186 insertions(+), 134 deletions(-) diff --git a/Makefile b/Makefile index db5ab2cf..0db94f77 100644 --- a/Makefile +++ b/Makefile @@ -76,8 +76,8 @@ ifdef DRC_DBG libpcsxcore/new_dynarec/emu_if.o: CFLAGS += -D_FILE_OFFSET_BITS=64 CFLAGS += -DDRC_DBG endif -ifeq "$(DRC_CACHE_BASE)" "1" -libpcsxcore/new_dynarec/%.o: CFLAGS += -DBASE_ADDR_FIXED=1 +ifeq "$(BASE_ADDR_DYNAMIC)" "1" +libpcsxcore/new_dynarec/%.o: CFLAGS += -DBASE_ADDR_DYNAMIC=1 endif # spu diff --git a/configure b/configure index 20ff1d53..b053a7a8 100755 --- a/configure +++ b/configure @@ -584,7 +584,7 @@ if [ "$enable_dynarec" = "yes" ]; then echo "USE_DYNAREC = 1" >> $config_mak fi if [ "$drc_cache_base" = "yes" ]; then - echo "DRC_CACHE_BASE = 1" >> $config_mak + echo "BASE_ADDR_DYNAMIC = 1" >> $config_mak fi if [ "$have_c64x_dsp" = "yes" ]; then echo "HAVE_C64_TOOLS = 1" >> $config_mak diff --git a/libpcsxcore/new_dynarec/assem_arm.c b/libpcsxcore/new_dynarec/assem_arm.c index c6114564..62038a20 100644 --- a/libpcsxcore/new_dynarec/assem_arm.c +++ b/libpcsxcore/new_dynarec/assem_arm.c @@ -1,7 +1,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Mupen64plus/PCSX - assem_arm.c * * Copyright (C) 2009-2011 Ari64 * - * Copyright (C) 2010-2011 Gražvydas "notaz" Ignotas * + * Copyright (C) 2010-2021 Gražvydas "notaz" Ignotas * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -28,13 +28,6 @@ #include "pcnt.h" #include "arm_features.h" -#if defined(BASE_ADDR_FIXED) -#elif defined(BASE_ADDR_DYNAMIC) -u_char *translation_cache; -#else -u_char translation_cache[1 << TARGET_SIZE_2] __attribute__((aligned(4096))); -#endif - #ifndef __MACH__ #define CALLER_SAVE_REGS 0x100f #else @@ -1034,6 +1027,12 @@ static void emit_set_if_carry32(int rs1, int rs2, int rt) emit_cmovb_imm(1,rt); } +static int can_jump_or_call(const void *a) +{ + intptr_t offset = (u_char *)a - out - 8; + return (-33554432 <= offset && offset < 33554432); +} + static void emit_call(const void *a_) { int a = (int)a_; @@ -1598,7 +1597,7 @@ static void emit_extjump2(u_char *addr, u_int target, void *linker) emit_writeword(ECX,&last_count); #endif //DEBUG < - emit_jmp(linker); + emit_far_jump(linker); } static void check_extjump2(void *src) @@ -1738,7 +1737,7 @@ static void do_readstub(int n) if(cc<0) emit_loadreg(CCREG,2); emit_addimm(cc<0?2:cc,CLOCK_ADJUST((int)stubs[n].d+1),2); - emit_call(handler); + emit_far_call(handler); if(itype[i]==C1LS||itype[i]==C2LS||(rt>=0&&rt1[i]!=0)) { mov_loadtype_adj(type,0,rt); } @@ -1754,7 +1753,7 @@ static void inline_readstub(enum stub_type type, int i, u_int addr, signed char int rt=get_reg(regmap,target); if(rs<0) rs=get_reg(regmap,-1); assert(rs>=0); - u_int is_dynamic,far_call=0; + u_int is_dynamic; uintptr_t host_addr = 0; void *handler; int cc=get_reg(regmap,CCREG); @@ -1794,12 +1793,6 @@ static void inline_readstub(enum stub_type type, int i, u_int addr, signed char emit_movimm(addr,0); else if(rs!=0) emit_mov(rs,0); - int offset=(u_char *)handler-out-8; - if(offset<-33554432||offset>=33554432) { - // unreachable memhandler, a plugin func perhaps - emit_movimm((u_int)handler,12); - far_call=1; - } if(cc<0) emit_loadreg(CCREG,2); if(is_dynamic) { @@ -1813,10 +1806,7 @@ static void inline_readstub(enum stub_type type, int i, u_int addr, signed char emit_writeword(2,&Count); } - if(far_call) - emit_callreg(12); - else - emit_call(handler); + emit_far_call(handler); if(rt>=0&&rt1[i]!=0) { switch(type) { @@ -1902,7 +1892,7 @@ static void do_writestub(int n) emit_loadreg(CCREG,2); emit_addimm(cc<0?2:cc,CLOCK_ADJUST((int)stubs[n].d+1),2); // returns new cycle_count - emit_call(handler); + emit_far_call(handler); emit_addimm(0,-CLOCK_ADJUST((int)stubs[n].d+1),cc<0?2:cc); if(cc<0) emit_storereg(CCREG,2); @@ -1941,7 +1931,7 @@ static void inline_writestub(enum stub_type type, int i, u_int addr, signed char emit_addimm(cc<0?2:cc,CLOCK_ADJUST(adj+1),2); emit_movimm((u_int)handler,3); // returns new cycle_count - emit_call(jump_handler_write_h); + emit_far_call(jump_handler_write_h); emit_addimm(0,-CLOCK_ADJUST(adj+1),cc<0?2:cc); if(cc<0) emit_storereg(CCREG,2); @@ -1969,7 +1959,7 @@ static void *do_dirty_stub(int i) { assem_debug("do_dirty_stub %x\n",start+i*4); do_dirty_stub_emit_args(start + i*4); - emit_call(verify_code); + emit_far_call(verify_code); void *entry = out; load_regs_entry(i); if (entry == out) @@ -1981,7 +1971,7 @@ static void *do_dirty_stub(int i) static void do_dirty_stub_ds() { do_dirty_stub_emit_args(start + 1); - emit_call(verify_code_ds); + emit_far_call(verify_code_ds); } /* Special assem */ @@ -1991,7 +1981,7 @@ static void c2op_prologue(u_int op,u_int reglist) save_regs_all(reglist); #ifdef PCNT emit_movimm(op,0); - emit_call(pcnt_gte_start); + emit_far_call(pcnt_gte_start); #endif emit_addimm(FP,(int)&psxRegs.CP2D.r[0]-(int)&dynarec_local,0); // cop2 regs } @@ -2000,7 +1990,7 @@ static void c2op_epilogue(u_int op,u_int reglist) { #ifdef PCNT emit_movimm(op,0); - emit_call(pcnt_gte_end); + emit_far_call(pcnt_gte_end); #endif restore_regs_all(reglist); } @@ -2008,19 +1998,19 @@ static void c2op_epilogue(u_int op,u_int reglist) static void c2op_call_MACtoIR(int lm,int need_flags) { if(need_flags) - emit_call(lm?gteMACtoIR_lm1:gteMACtoIR_lm0); + emit_far_call(lm?gteMACtoIR_lm1:gteMACtoIR_lm0); else - emit_call(lm?gteMACtoIR_lm1_nf:gteMACtoIR_lm0_nf); + emit_far_call(lm?gteMACtoIR_lm1_nf:gteMACtoIR_lm0_nf); } static void c2op_call_rgb_func(void *func,int lm,int need_ir,int need_flags) { - emit_call(func); + emit_far_call(func); // func is C code and trashes r0 emit_addimm(FP,(int)&psxRegs.CP2D.r[0]-(int)&dynarec_local,0); if(need_flags||need_ir) c2op_call_MACtoIR(lm,need_flags); - emit_call(need_flags?gteMACtoRGB:gteMACtoRGB_nf); + emit_far_call(need_flags?gteMACtoRGB:gteMACtoRGB_nf); } static void c2op_assemble(int i,struct regstat *i_regs) @@ -2070,17 +2060,17 @@ static void c2op_assemble(int i,struct regstat *i_regs) emit_readword(&zeromem_ptr,7); #ifdef __ARM_NEON__ emit_movimm(source[i],1); // opcode - emit_call(gteMVMVA_part_neon); + emit_far_call(gteMVMVA_part_neon); if(need_flags) { emit_movimm(lm,1); - emit_call(gteMACtoIR_flags_neon); + emit_far_call(gteMACtoIR_flags_neon); } #else if(cv==3&&shift) - emit_call((int)gteMVMVA_part_cv3sh12_arm); + emit_far_call((int)gteMVMVA_part_cv3sh12_arm); else { emit_movimm(shift,1); - emit_call((int)(need_flags?gteMVMVA_part_arm:gteMVMVA_part_nf_arm)); + emit_far_call((int)(need_flags?gteMVMVA_part_arm:gteMVMVA_part_nf_arm)); } if(need_flags||need_ir) c2op_call_MACtoIR(lm,need_flags); @@ -2089,13 +2079,13 @@ static void c2op_assemble(int i,struct regstat *i_regs) c2op_prologue(c2op,reglist); emit_movimm(source[i],1); // opcode emit_writeword(1,&psxRegs.code); - emit_call((int)(need_flags?gte_handlers[c2op]:gte_handlers_nf[c2op])); + emit_far_call(need_flags?gte_handlers[c2op]:gte_handlers_nf[c2op]); #endif break; } case GTE_OP: c2op_prologue(c2op,reglist); - emit_call(shift?gteOP_part_shift:gteOP_part_noshift); + emit_far_call(shift?gteOP_part_shift:gteOP_part_noshift); if(need_flags||need_ir) { emit_addimm(FP,(int)&psxRegs.CP2D.r[0]-(int)&dynarec_local,0); c2op_call_MACtoIR(lm,need_flags); @@ -2111,7 +2101,7 @@ static void c2op_assemble(int i,struct regstat *i_regs) break; case GTE_SQR: c2op_prologue(c2op,reglist); - emit_call(shift?gteSQR_part_shift:gteSQR_part_noshift); + emit_far_call(shift?gteSQR_part_shift:gteSQR_part_noshift); if(need_flags||need_ir) { emit_addimm(FP,(int)&psxRegs.CP2D.r[0]-(int)&dynarec_local,0); c2op_call_MACtoIR(lm,need_flags); @@ -2136,7 +2126,7 @@ static void c2op_assemble(int i,struct regstat *i_regs) emit_movimm(source[i],1); // opcode emit_writeword(1,&psxRegs.code); #endif - emit_call(need_flags?gte_handlers[c2op]:gte_handlers_nf[c2op]); + emit_far_call(need_flags?gte_handlers[c2op]:gte_handlers_nf[c2op]); break; } c2op_epilogue(c2op,reglist); @@ -2309,7 +2299,7 @@ static void multdiv_assemble_arm(int i,struct regstat *i_regs) static void do_jump_vaddr(int rs) { - emit_jmp(jump_vaddr_reg[rs]); + emit_far_jump(jump_vaddr_reg[rs]); } static void do_preload_rhash(int r) { @@ -2402,7 +2392,17 @@ static void do_clear_cache() } // CPU-architecture-specific initialization -static void arch_init() { +static void arch_init(void) +{ + uintptr_t diff = (u_char *)&ndrc->tramp.f - (u_char *)&ndrc->tramp.ops - 8; + struct tramp_insns *ops = ndrc->tramp.ops; + size_t i; + assert(!(diff & 3)); + assert(diff < 0x1000); + start_tcache_write(ops, (u_char *)ops + sizeof(ndrc->tramp.ops)); + for (i = 0; i < ARRAY_SIZE(ndrc->tramp.ops); i++) + ops[i].ldrpc = 0xe5900000 | rd_rn_rm(15,15,0) | diff; // ldr pc, [=val] + end_tcache_write(ops, (u_char *)ops + sizeof(ndrc->tramp.ops)); } // vim:shiftwidth=2:expandtab diff --git a/libpcsxcore/new_dynarec/assem_arm.h b/libpcsxcore/new_dynarec/assem_arm.h index bbaf5b9e..6b3c672c 100644 --- a/libpcsxcore/new_dynarec/assem_arm.h +++ b/libpcsxcore/new_dynarec/assem_arm.h @@ -31,18 +31,8 @@ extern char *invc_ptr; #define TARGET_SIZE_2 24 // 2^24 = 16 megabytes -// Code generator target address -#if defined(BASE_ADDR_FIXED) - // "round" address helpful for debug - // this produces best code, but not many platforms allow it, - // only use if you are sure this range is always free - #define BASE_ADDR_ 0x1000000 - #define translation_cache (u_char *)BASE_ADDR_ -#elif defined(BASE_ADDR_DYNAMIC) - // for platforms that can't just use .bss buffer, like vita - // otherwise better to use the next option for closer branches - extern u_char *translation_cache; -#else - // using a static buffer in .bss - extern u_char translation_cache[1 << TARGET_SIZE_2]; -#endif +struct tramp_insns +{ + u_int ldrpc; +}; + diff --git a/libpcsxcore/new_dynarec/assem_arm64.c b/libpcsxcore/new_dynarec/assem_arm64.c index 27f9141d..5483da15 100644 --- a/libpcsxcore/new_dynarec/assem_arm64.c +++ b/libpcsxcore/new_dynarec/assem_arm64.c @@ -23,14 +23,6 @@ #include "pcnt.h" #include "arm_features.h" -#if defined(BASE_ADDR_FIXED) -#elif defined(BASE_ADDR_DYNAMIC) -u_char *translation_cache; -#else -u_char translation_cache[1 << TARGET_SIZE_2] __attribute__((aligned(4096))); -#endif -static u_int needs_clear_cache[1<<(TARGET_SIZE_2-17)]; - #define CALLER_SAVE_REGS 0x0007ffff #define unused __attribute__((unused)) @@ -889,6 +881,12 @@ static void emit_set_if_carry32(u_int rs1, u_int rs2, u_int rt) emit_cmovb_imm(1,rt); } +static int can_jump_or_call(const void *a) +{ + intptr_t diff = (u_char *)a - out; + return (-134217728 <= diff && diff <= 134217727); +} + static void emit_call(const void *a) { intptr_t diff = (u_char *)a - out; @@ -1295,7 +1293,7 @@ static void emit_extjump2(u_char *addr, u_int target, void *linker) // addr is in the current recompiled block (max 256k) // offset shouldn't exceed +/-1MB emit_adr(addr, 1); - emit_jmp(linker); + emit_far_jump(linker); } static void check_extjump2(void *src) @@ -1439,7 +1437,7 @@ static void do_readstub(int n) if(cc<0) emit_loadreg(CCREG,2); emit_addimm(cc<0?2:cc,CLOCK_ADJUST((int)stubs[n].d+1),2); - emit_call(handler); + emit_far_call(handler); // (no cycle reload after read) if(itype[i]==C1LS||itype[i]==C2LS||(rt>=0&&rt1[i]!=0)) { loadstore_extend(type,0,rt); @@ -1508,9 +1506,9 @@ static void inline_readstub(enum stub_type type, int i, u_int addr, signed char emit_addimm64(1, l1 & 0xfff, 1); } else - emit_call(do_memhandler_pre); + emit_far_call(do_memhandler_pre); - emit_call(handler); + emit_far_call(handler); // (no cycle reload after read) if(rt>=0&&rt1[i]!=0) @@ -1599,7 +1597,7 @@ static void do_writestub(int n) emit_loadreg(CCREG,2); emit_addimm(cc<0?2:cc,CLOCK_ADJUST((int)stubs[n].d+1),2); // returns new cycle_count - emit_call(handler); + emit_far_call(handler); emit_addimm(0,-CLOCK_ADJUST((int)stubs[n].d+1),cc<0?2:cc); if(cc<0) emit_storereg(CCREG,2); @@ -1642,9 +1640,9 @@ static void inline_writestub(enum stub_type type, int i, u_int addr, signed char emit_loadreg(CCREG, (cc_use = 2)); emit_addimm(cc_use, CLOCK_ADJUST(adj+1), 2); - emit_call(do_memhandler_pre); - emit_call(handler); - emit_call(do_memhandler_post); + emit_far_call(do_memhandler_pre); + emit_far_call(handler); + emit_far_call(do_memhandler_post); emit_addimm(0, -CLOCK_ADJUST(adj+1), cc_use); if (cc < 0) emit_storereg(CCREG, cc_use); @@ -1665,12 +1663,12 @@ static void do_dirty_stub_base(u_int vaddr) emit_loadlp_ofs(0, 0); // ldr x1, source emit_loadlp_ofs(0, 1); // ldr x2, copy emit_movz(slen*4, 2); - emit_call(verify_code_arm64); + emit_far_call(verify_code_arm64); void *jmp = out; emit_cbz(0, 0); emit_movz(vaddr & 0xffff, 0); emit_movk_lsl16(vaddr >> 16, 0); - emit_call(get_addr); + emit_far_call(get_addr); emit_jmpreg(0); set_jump_target(jmp, out); } @@ -1784,7 +1782,7 @@ static void c2op_prologue(u_int op,u_int reglist) save_load_regs_all(1, reglist); #ifdef PCNT emit_movimm(op, 0); - emit_call(pcnt_gte_start); + emit_far_call(pcnt_gte_start); #endif // pointer to cop2 regs emit_addimm64(FP, (u_char *)&psxRegs.CP2D.r[0] - (u_char *)&dynarec_local, 0); @@ -1794,7 +1792,7 @@ static void c2op_epilogue(u_int op,u_int reglist) { #ifdef PCNT emit_movimm(op, 0); - emit_call(pcnt_gte_end); + emit_far_call(pcnt_gte_end); #endif save_load_regs_all(0, reglist); } @@ -1824,7 +1822,7 @@ static void c2op_assemble(int i,struct regstat *i_regs) c2op_prologue(c2op,reglist); emit_movimm(source[i],1); // opcode emit_writeword(1,&psxRegs.code); - emit_call(need_flags?gte_handlers[c2op]:gte_handlers_nf[c2op]); + emit_far_call(need_flags?gte_handlers[c2op]:gte_handlers_nf[c2op]); break; } c2op_epilogue(c2op,reglist); @@ -1973,7 +1971,7 @@ static void do_jump_vaddr(u_int rs) { if (rs != 0) emit_mov(rs, 0); - emit_call(get_addr_ht); + emit_far_call(get_addr_ht); emit_jmpreg(0); } @@ -2061,7 +2059,18 @@ static void do_clear_cache() } // CPU-architecture-specific initialization -static void arch_init() { +static void arch_init(void) +{ + uintptr_t diff = (u_char *)&ndrc->tramp.f - (u_char *)&ndrc->tramp.ops; + struct tramp_insns *ops = ndrc->tramp.ops; + size_t i; + assert(!(diff & 3)); + start_tcache_write(ops, (u_char *)ops + sizeof(ndrc->tramp.ops)); + for (i = 0; i < ARRAY_SIZE(ndrc->tramp.ops); i++) { + ops[i].ldr = 0x58000000 | imm19_rt(diff >> 2, 17); // ldr x17, [=val] + ops[i].br = 0xd61f0000 | rm_rn_rd(0, 17, 0); // br x17 + } + end_tcache_write(ops, (u_char *)ops + sizeof(ndrc->tramp.ops)); } // vim:shiftwidth=2:expandtab diff --git a/libpcsxcore/new_dynarec/assem_arm64.h b/libpcsxcore/new_dynarec/assem_arm64.h index fe12ad75..1360bfad 100644 --- a/libpcsxcore/new_dynarec/assem_arm64.h +++ b/libpcsxcore/new_dynarec/assem_arm64.h @@ -29,19 +29,18 @@ #define SSP_CALLER_REGS (8*20) #define SSP_ALL (SSP_CALLEE_REGS+SSP_CALLER_REGS) +#define TARGET_SIZE_2 24 // 2^24 = 16 megabytes + #ifndef __ASSEMBLER__ extern char *invc_ptr; -#define TARGET_SIZE_2 24 // 2^24 = 16 megabytes +struct tramp_insns +{ + u_int ldr; + u_int br; +}; -// Code generator target address -#if defined(BASE_ADDR_DYNAMIC) - // for platforms that can't just use .bss buffer (are there any on arm64?) - extern u_char *translation_cache; -#else - // using a static buffer in .bss - extern u_char translation_cache[1 << TARGET_SIZE_2]; -#endif +static void clear_cache_arm64(char *start, char *end); #endif // !__ASSEMBLY__ diff --git a/libpcsxcore/new_dynarec/new_dynarec.c b/libpcsxcore/new_dynarec/new_dynarec.c index 9ce1f069..cfeddc29 100644 --- a/libpcsxcore/new_dynarec/new_dynarec.c +++ b/libpcsxcore/new_dynarec/new_dynarec.c @@ -66,6 +66,23 @@ static int sceBlock; #define MAXBLOCK 4096 #define MAX_OUTPUT_BLOCK_SIZE 262144 +struct ndrc_mem +{ + u_char translation_cache[1 << TARGET_SIZE_2]; + struct + { + struct tramp_insns ops[2048 / sizeof(struct tramp_insns)]; + const void *f[2048 / sizeof(void *)]; + } tramp; +}; + +#ifdef BASE_ADDR_DYNAMIC +static struct ndrc_mem *ndrc; +#else +static struct ndrc_mem ndrc_ __attribute__((aligned(4096))); +static struct ndrc_mem *ndrc = &ndrc_; +#endif + // stubs enum stub_type { CC_STUB = 1, @@ -308,6 +325,8 @@ static void *emit_fastpath_cmp_jump(int i,int addr,int *addr_reg_override); static void *get_direct_memhandler(void *table, u_int addr, enum stub_type type, uintptr_t *addr_host); static void pass_args(int a0, int a1); +static void emit_far_jump(const void *f); +static void emit_far_call(const void *f); static void mprotect_w_x(void *start, void *end, int is_x) { @@ -360,8 +379,8 @@ static void end_tcache_write(void *start, void *end) static void *start_block(void) { u_char *end = out + MAX_OUTPUT_BLOCK_SIZE; - if (end > translation_cache + (1< ndrc->translation_cache + sizeof(ndrc->translation_cache)) + end = ndrc->translation_cache + sizeof(ndrc->translation_cache); start_tcache_write(out, end); return out; } @@ -866,6 +885,48 @@ static const char *func_name(const void *a) #include "assem_arm64.c" #endif +static void *get_trampoline(const void *f) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(ndrc->tramp.f); i++) { + if (ndrc->tramp.f[i] == f || ndrc->tramp.f[i] == NULL) + break; + } + if (i == ARRAY_SIZE(ndrc->tramp.f)) { + SysPrintf("trampoline table is full, last func %p\n", f); + abort(); + } + if (ndrc->tramp.f[i] == NULL) { + start_tcache_write(&ndrc->tramp.f[i], &ndrc->tramp.f[i + 1]); + ndrc->tramp.f[i] = f; + end_tcache_write(&ndrc->tramp.f[i], &ndrc->tramp.f[i + 1]); + } + return &ndrc->tramp.ops[i]; +} + +static void emit_far_jump(const void *f) +{ + if (can_jump_or_call(f)) { + emit_jmp(f); + return; + } + + f = get_trampoline(f); + emit_jmp(f); +} + +static void emit_far_call(const void *f) +{ + if (can_jump_or_call(f)) { + emit_call(f); + return; + } + + f = get_trampoline(f); + emit_call(f); +} + // Add virtual address mapping to linked list void ll_add(struct ll_entry **head,int vaddr,void *addr) { @@ -1167,7 +1228,7 @@ static void do_invstub(int n) set_jump_target(stubs[n].addr, out); save_regs(reglist); if(stubs[n].b!=0) emit_mov(stubs[n].b,0); - emit_call(invalidate_addr); + emit_far_call(invalidate_addr); restore_regs(reglist); emit_jmp(stubs[n].retaddr); // return address } @@ -2865,7 +2926,7 @@ void store_assemble(int i,struct regstat *i_regs) emit_movimm(start+i*4+4,0); emit_writeword(0,&pcaddr); emit_addimm(HOST_CCREG,2,HOST_CCREG); - emit_call(get_addr_ht); + emit_far_call(get_addr_ht); emit_jmpreg(0); } } @@ -3046,7 +3107,7 @@ static void cop0_assemble(int i,struct regstat *i_regs) emit_storereg(CCREG,HOST_CCREG); emit_loadreg(rs1[i],1); emit_movimm(copr,0); - emit_call(pcsx_mtc0_ds); + emit_far_call(pcsx_mtc0_ds); emit_loadreg(rs1[i],s); return; } @@ -3055,14 +3116,12 @@ static void cop0_assemble(int i,struct regstat *i_regs) emit_movimm(0,HOST_TEMPREG); emit_writeword(HOST_TEMPREG,&pending_exception); } - //else if(copr==12&&is_delayslot) emit_call((int)MTC0_R12); - //else if(s==HOST_CCREG) emit_loadreg(rs1[i],1); else if(s!=1) emit_mov(s,1); emit_movimm(copr,0); - emit_call(pcsx_mtc0); + emit_far_call(pcsx_mtc0); if(copr==9||copr==11||copr==12||copr==13) { emit_readword(&Count,HOST_CCREG); emit_readword(&next_interupt,HOST_TEMPREG); @@ -3079,7 +3138,7 @@ static void cop0_assemble(int i,struct regstat *i_regs) emit_jeq(0); emit_readword(&pcaddr, 0); emit_addimm(HOST_CCREG,2,HOST_CCREG); - emit_call(get_addr_ht); + emit_far_call(get_addr_ht); emit_jmpreg(0); set_jump_target(jaddr, out); } @@ -3139,7 +3198,7 @@ static void do_cop1stub(int n) if(regs[i].regmap_entry[HOST_CCREG]!=CCREG) emit_loadreg(CCREG,HOST_CCREG); emit_movimm(start+(i-ds)*4,EAX); // Get PC emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i]),HOST_CCREG); // CHECK: is this right? There should probably be an extra cycle... - emit_jmp(ds?fp_exception_ds:fp_exception); + emit_far_jump(ds?fp_exception_ds:fp_exception); } static void cop2_get_dreg(u_int copr,signed char tl,signed char temp) @@ -3396,7 +3455,7 @@ static void do_unalignedwritestub(int n) if(cc<0) emit_loadreg(CCREG,2); emit_addimm(cc<0?2:cc,CLOCK_ADJUST((int)stubs[n].d+1),2); - emit_call((opcode[i]==0x2a?jump_handle_swl:jump_handle_swr)); + emit_far_call((opcode[i]==0x2a?jump_handle_swl:jump_handle_swr)); emit_addimm(0,-CLOCK_ADJUST((int)stubs[n].d+1),cc<0?2:cc); if(cc<0) emit_storereg(CCREG,2); @@ -3490,8 +3549,8 @@ static void call_c_cpu_handler(int i, const struct regstat *i_regs, u_int pc, vo emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i]),HOST_CCREG); // XXX emit_add(2,HOST_CCREG,2); emit_writeword(2,&psxRegs.cycle); - emit_call(func); - emit_jmp(jump_to_new_pc); + emit_far_call(func); + emit_far_jump(jump_to_new_pc); } static void syscall_assemble(int i,struct regstat *i_regs) @@ -4227,7 +4286,7 @@ static void drc_dbg_emit_do_cmp(int i) save_regs(reglist); emit_movimm(start+i*4,0); emit_writeword(0,&pcaddr); - emit_call(do_insn_cmp); + emit_far_call(do_insn_cmp); //emit_readword(&cycle,0); //emit_addimm(0,2,0); //emit_writeword(0,&cycle); @@ -4558,7 +4617,7 @@ static void do_ccstub(int n) // Update cycle count assert(branch_regs[i].regmap[HOST_CCREG]==CCREG||branch_regs[i].regmap[HOST_CCREG]==-1); if(stubs[n].a) emit_addimm(HOST_CCREG,CLOCK_ADJUST((signed int)stubs[n].a),HOST_CCREG); - emit_call(cc_interrupt); + emit_far_call(cc_interrupt); if(stubs[n].a) emit_addimm(HOST_CCREG,-CLOCK_ADJUST((signed int)stubs[n].a),HOST_CCREG); if(stubs[n].d==TAKEN) { if(internal_branch(ba[i])) @@ -6398,7 +6457,7 @@ static void new_dynarec_test(void) ((volatile u_int *)out)[0]++; // make cache dirty for (i = 0; i < ARRAY_SIZE(ret); i++) { - out = translation_cache; + out = ndrc->translation_cache; beginning = start_block(); emit_movimm(DRC_TEST_VAL + i, 0); // test emit_ret(); @@ -6412,7 +6471,7 @@ static void new_dynarec_test(void) SysPrintf("test passed.\n"); else SysPrintf("test failed, will likely crash soon (r=%08x %08x)\n", ret[0], ret[1]); - out = translation_cache; + out = ndrc->translation_cache; } // clear the state completely, instead of just marking @@ -6420,7 +6479,7 @@ static void new_dynarec_test(void) void new_dynarec_clear_full() { int n; - out = translation_cache; + out = ndrc->translation_cache; memset(invalid_code,1,sizeof(invalid_code)); memset(hash_table,0xff,sizeof(hash_table)); memset(mini_ht,-1,sizeof(mini_ht)); @@ -6442,30 +6501,24 @@ void new_dynarec_init() { SysPrintf("Init new dynarec\n"); - // allocate/prepare a buffer for translation cache - // see assem_arm.h for some explanation -#if defined(BASE_ADDR_FIXED) - if (mmap(translation_cache, 1 << TARGET_SIZE_2, - PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_PRIVATE | MAP_ANONYMOUS, - -1, 0) != translation_cache) { - SysPrintf("mmap() failed: %s\n", strerror(errno)); - SysPrintf("disable BASE_ADDR_FIXED and recompile\n"); - abort(); - } -#elif defined(BASE_ADDR_DYNAMIC) +#ifdef BASE_ADDR_DYNAMIC #ifdef VITA sceBlock = sceKernelAllocMemBlockForVM("code", 1 << TARGET_SIZE_2); if (sceBlock < 0) SysPrintf("sceKernelAllocMemBlockForVM failed\n"); - int ret = sceKernelGetMemBlockBase(sceBlock, (void **)&translation_cache); + int ret = sceKernelGetMemBlockBase(sceBlock, (void **)&ndrc); if (ret < 0) SysPrintf("sceKernelGetMemBlockBase failed\n"); #else - translation_cache = mmap (NULL, 1 << TARGET_SIZE_2, + uintptr_t desired_addr = 0; + #ifdef __ELF__ + extern char _end; + desired_addr = ((uintptr_t)&_end + 0xffffff) & ~0xffffffl; + #endif + ndrc = mmap((void *)desired_addr, sizeof(*ndrc), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (translation_cache == MAP_FAILED) { + if (ndrc == MAP_FAILED) { SysPrintf("mmap() failed: %s\n", strerror(errno)); abort(); } @@ -6473,11 +6526,12 @@ void new_dynarec_init() #else #ifndef NO_WRITE_EXEC // not all systems allow execute in data segment by default - if (mprotect(translation_cache, 1<translation_cache) + sizeof(ndrc->tramp.ops), + PROT_READ | PROT_WRITE | PROT_EXEC) != 0) SysPrintf("mprotect() failed: %s\n", strerror(errno)); #endif #endif - out = translation_cache; + out = ndrc->translation_cache; cycle_multiplier=200; new_dynarec_clear_full(); #ifdef HOST_IMM8 @@ -6496,12 +6550,12 @@ void new_dynarec_init() void new_dynarec_cleanup() { int n; -#if defined(BASE_ADDR_FIXED) || defined(BASE_ADDR_DYNAMIC) +#ifdef BASE_ADDR_DYNAMIC #ifdef VITA sceKernelFreeMemBlock(sceBlock); sceBlock = -1; #else - if (munmap(translation_cache, 1<>12]=0; emit_movimm(start,0); emit_writeword(0,&pcaddr); - emit_jmp(new_dyna_leave); + emit_far_jump(new_dyna_leave); literal_pool(0); end_block(beginning); ll_add_flags(jump_in+page,start,state_rflags,(void *)beginning); @@ -8696,7 +8750,7 @@ int new_recompile_block(u_int addr) emit_cmp(0,1); #ifdef __aarch64__ emit_jeq(out + 4*2); - emit_jmp(new_dyna_leave); + emit_far_jump(new_dyna_leave); #else emit_jne(new_dyna_leave); #endif @@ -8968,8 +9022,8 @@ int new_recompile_block(u_int addr) // If we're within 256K of the end of the buffer, // start over from the beginning. (Is 256K enough?) - if (out > translation_cache+(1< ndrc->translation_cache + sizeof(ndrc->translation_cache) - MAX_OUTPUT_BLOCK_SIZE) + out = ndrc->translation_cache; // Trap writes to any of the pages we compiled for(i=start>>12;i<=(start+slen*4)>>12;i++) { @@ -8986,11 +9040,11 @@ int new_recompile_block(u_int addr) /* Pass 10 - Free memory by expiring oldest blocks */ - int end=(((out-translation_cache)>>(TARGET_SIZE_2-16))+16384)&65535; + int end=(((out-ndrc->translation_cache)>>(TARGET_SIZE_2-16))+16384)&65535; while(expirep!=end) { int shift=TARGET_SIZE_2-3; // Divide into 8 blocks - uintptr_t base=(uintptr_t)translation_cache+((expirep>>13)<translation_cache+((expirep>>13)<>11)&3) { -- 2.39.2