From 65e221879210d46ee2925a8c49593f1204d802cc Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Mon, 13 May 2019 00:23:18 +0200 Subject: [PATCH] Fix ARM64 rewrites with NO_RWX_PAGES Tested on nvidia jetson and Android, so far works great. --- core/hw/sh4/dyna/ngen.h | 4 ++-- core/linux/common.cpp | 6 +----- core/rec-ARM64/rec_arm64.cpp | 7 ++++--- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/core/hw/sh4/dyna/ngen.h b/core/hw/sh4/dyna/ngen.h index 3ec9a8101..88157a304 100644 --- a/core/hw/sh4/dyna/ngen.h +++ b/core/hw/sh4/dyna/ngen.h @@ -55,8 +55,8 @@ // sub/add the pointer offset. CodeCache will point to the RW pointer for simplicity. #ifdef FEAT_NO_RWX_PAGES extern uintptr_t cc_rx_offset; - #define CC_RW2RX(ptr) (void*)(((uintptr_t)ptr) + cc_rx_offset) - #define CC_RX2RW(ptr) (void*)(((uintptr_t)ptr) - cc_rx_offset) + #define CC_RW2RX(ptr) (void*)(((uintptr_t)(ptr)) + cc_rx_offset) + #define CC_RX2RW(ptr) (void*)(((uintptr_t)(ptr)) - cc_rx_offset) #else #define CC_RW2RX(ptr) (ptr) #define CC_RX2RW(ptr) (ptr) diff --git a/core/linux/common.cpp b/core/linux/common.cpp index ce9194436..39e3efd40 100644 --- a/core/linux/common.cpp +++ b/core/linux/common.cpp @@ -59,12 +59,8 @@ void fault_handler (int sn, siginfo_t * si, void *segfault_ctx) context_from_segfault(&ctx, segfault_ctx); - bool dyna_cde = ((unat)ctx.pc>(unat)CodeCache) && ((unat)ctx.pc<(unat)(CodeCache + CODE_SIZE)); + bool dyna_cde = ((unat)CC_RX2RW(ctx.pc) > (unat)CodeCache) && ((unat)CC_RX2RW(ctx.pc) < (unat)(CodeCache + CODE_SIZE)); - //ucontext_t* ctx=(ucontext_t*)ctxr; - //printf("mprot hit @ ptr 0x%08X @@ code: %08X, %d\n",si->si_addr,ctx->uc_mcontext.arm_pc,dyna_cde); - - if (VramLockedWrite((u8*)si->si_addr) || BM_LockedWrite((u8*)si->si_addr)) return; #if FEAT_SHREC == DYNAREC_JIT diff --git a/core/rec-ARM64/rec_arm64.cpp b/core/rec-ARM64/rec_arm64.cpp index 03795ab13..cfca5c4c3 100644 --- a/core/rec-ARM64/rec_arm64.cpp +++ b/core/rec-ARM64/rec_arm64.cpp @@ -1442,13 +1442,14 @@ void ngen_CC_Finish(shil_opcode* op) bool ngen_Rewrite(unat& host_pc, unat, unat) { //printf("ngen_Rewrite pc %p\n", host_pc); - RuntimeBlockInfo *block = bm_GetBlock((void *)host_pc); + void *host_pc_rw = CC_RX2RW(host_pc); + RuntimeBlockInfo *block = bm_GetBlock((void*)host_pc); if (block == NULL) { printf("ngen_Rewrite: Block at %p not found\n", (void *)host_pc); return false; } - u32 *code_ptr = (u32*)host_pc; + u32 *code_ptr = (u32*)host_pc_rw; auto it = block->memory_accesses.find(code_ptr); if (it == block->memory_accesses.end()) { @@ -1466,7 +1467,7 @@ bool ngen_Rewrite(unat& host_pc, unat, unat) assembler->GenWriteMemorySlow(op); assembler->Finalize(true); delete assembler; - host_pc = (unat)(code_ptr - 2); + host_pc = (unat)CC_RW2RX(code_ptr - 2); return true; }