From 7a5711ce1d6ce3f17fa756b6e3cbecdd8a52e156 Mon Sep 17 00:00:00 2001 From: Glenn Baker Date: Tue, 22 Oct 2024 16:33:23 +0200 Subject: [PATCH] qemu/tcg: factor out has_hookmem(). It's the same implementation for all architectures, so factor out has_hookmem() into tcg_uc_has_hookmem(). Signed-off-by: Glenn Baker --- qemu/include/tcg/tcg.h | 7 +++++++ qemu/tcg/aarch64/tcg-target.inc.c | 13 +++---------- qemu/tcg/i386/tcg-target.inc.c | 9 +-------- qemu/tcg/ppc/tcg-target.inc.c | 15 ++++----------- 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/qemu/include/tcg/tcg.h b/qemu/include/tcg/tcg.h index 9908acaa..e5afa988 100644 --- a/qemu/include/tcg/tcg.h +++ b/qemu/include/tcg/tcg.h @@ -1577,4 +1577,11 @@ struct jit_code_entry { void uc_del_inline_hook(uc_engine *uc, struct hook *hk); void uc_add_inline_hook(uc_engine *uc, struct hook *hk, void** args, int args_len); +static inline bool tcg_uc_has_hookmem(TCGContext *s) +{ + return HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ) || + HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ_AFTER) || + HOOK_EXISTS(s->uc, UC_HOOK_MEM_WRITE); +} + #endif /* TCG_H */ diff --git a/qemu/tcg/aarch64/tcg-target.inc.c b/qemu/tcg/aarch64/tcg-target.inc.c index dfc11c96..3c2a148a 100644 --- a/qemu/tcg/aarch64/tcg-target.inc.c +++ b/qemu/tcg/aarch64/tcg-target.inc.c @@ -1581,20 +1581,13 @@ static inline void tcg_out_adr(TCGContext *s, TCGReg rd, void *target) tcg_out_insn(s, 3406, ADR, rd, offset); } -static inline bool has_hookmem(TCGContext *s) -{ - return HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ) || - HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ_AFTER) || - HOOK_EXISTS(s->uc, UC_HOOK_MEM_WRITE); -} - static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) { TCGMemOpIdx oi = lb->oi; MemOp opc = get_memop(oi); MemOp size = opc & MO_SIZE; - const int type = has_hookmem(s) ? R_AARCH64_JUMP26 : R_AARCH64_CONDBR19; + const int type = tcg_uc_has_hookmem(s) ? R_AARCH64_JUMP26 : R_AARCH64_CONDBR19; if (!patch_reloc(lb->label_ptr[0], type, (intptr_t)s->code_ptr, 0)) { return false; } @@ -1620,7 +1613,7 @@ static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) MemOp opc = get_memop(oi); MemOp size = opc & MO_SIZE; - const int type = has_hookmem(s) ? R_AARCH64_JUMP26 : R_AARCH64_CONDBR19; + const int type = tcg_uc_has_hookmem(s) ? R_AARCH64_JUMP26 : R_AARCH64_CONDBR19; if (!patch_reloc(lb->label_ptr[0], type, (intptr_t)s->code_ptr, 0)) { return false; } @@ -1721,7 +1714,7 @@ static void tcg_out_tlb_read(TCGContext *s, TCGReg addr_reg, MemOp opc, /* If not equal, we jump to the slow path. */ *label_ptr = s->code_ptr; // Unicorn: fast path if hookmem is not enabled - if (!has_hookmem(s)) + if (!tcg_uc_has_hookmem(s)) tcg_out_insn(s, 3202, B_C, TCG_COND_NE, 0); else tcg_out_insn(s, 3206, B, 0); diff --git a/qemu/tcg/i386/tcg-target.inc.c b/qemu/tcg/i386/tcg-target.inc.c index 8e51ddeb..15cc1c05 100644 --- a/qemu/tcg/i386/tcg-target.inc.c +++ b/qemu/tcg/i386/tcg-target.inc.c @@ -1679,13 +1679,6 @@ static void * const qemu_st_helpers[16] = { [MO_BEQ] = helper_be_stq_mmu, }; -static inline bool has_hookmem(TCGContext *s) -{ - return HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ) || - HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ_AFTER) || - HOOK_EXISTS(s->uc, UC_HOOK_MEM_WRITE); -} - /* Perform the TLB load and compare. Inputs: @@ -1770,7 +1763,7 @@ static inline void tcg_out_tlb_load(TCGContext *s, TCGReg addrlo, TCGReg addrhi, tcg_out_mov(s, ttype, r1, addrlo); // Unicorn: fast path if hookmem is not enable - if (!has_hookmem(s)) + if (!tcg_uc_has_hookmem(s)) tcg_out_opc(s, OPC_JCC_long + JCC_JNE, 0, 0, 0); else /* slow_path, so data access will go via load_helper() */ diff --git a/qemu/tcg/ppc/tcg-target.inc.c b/qemu/tcg/ppc/tcg-target.inc.c index ba5ef05e..00b79429 100644 --- a/qemu/tcg/ppc/tcg-target.inc.c +++ b/qemu/tcg/ppc/tcg-target.inc.c @@ -2008,20 +2008,13 @@ static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOpIdx oi, label->label_ptr[0] = lptr; } -static inline bool has_hookmem(TCGContext *s) -{ - return HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ) || - HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ_AFTER) || - HOOK_EXISTS(s->uc, UC_HOOK_MEM_WRITE); -} - static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) { TCGMemOpIdx oi = lb->oi; MemOp opc = get_memop(oi); TCGReg hi, lo, arg = TCG_REG_R3; - const int type = has_hookmem(s) ? R_PPC_REL24 : R_PPC_REL14; + const int type = tcg_uc_has_hookmem(s) ? R_PPC_REL24 : R_PPC_REL14; if (!patch_reloc(lb->label_ptr[0], type, (intptr_t)s->code_ptr, 0)) { return false; } @@ -2070,7 +2063,7 @@ static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) MemOp s_bits = opc & MO_SIZE; TCGReg hi, lo, arg = TCG_REG_R3; - const int type = has_hookmem(s) ? R_PPC_REL24 : R_PPC_REL14; + const int type = tcg_uc_has_hookmem(s) ? R_PPC_REL24 : R_PPC_REL14; if (!patch_reloc(lb->label_ptr[0], type, (intptr_t)s->code_ptr, 0)) { return false; } @@ -2152,7 +2145,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64) /* Load a pointer into the current opcode w/conditional branch-link. */ label_ptr = s->code_ptr; // Unicorn: fast path if hookmem is not enabled - if (!has_hookmem(s)) + if (!tcg_uc_has_hookmem(s)) tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK); else tcg_out32(s, B | LK); @@ -2231,7 +2224,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64) /* Load a pointer into the current opcode w/conditional branch-link. */ label_ptr = s->code_ptr; // Unicorn: fast path if hookmem is not enabled - if (!has_hookmem(s)) + if (!tcg_uc_has_hookmem(s)) tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK); else tcg_out32(s, B | LK);