Skip to content

Commit 8442eb6

Browse files
authored
qemu/tcg: fix UC_HOOK_MEM_READ on aarch64. (#2028)
* qemu/tcg: fix UC_HOOK_MEM_READ on aarch64. Directly jump into the slow path when there is any hookmem enabled. This fixes #1908. Signed-off-by: Glenn Baker <[email protected]> * qemu/tcg: fix UC_HOOK_MEM_READ on ppc64. Directly jump into the slow path when there is any hookmem enabled. Signed-off-by: Glenn Baker <[email protected]> * qemu/tcg: check for UC_HOOK_MEM_READ_AFTER. Use has_hookmem() helper to determine wether "slow-path" TLB read is needed. Add this helper to x86 architecture as well so that to check for all hookmem. Signed-off-by: Glenn Baker <[email protected]> * 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 <[email protected]> --------- Signed-off-by: Glenn Baker <[email protected]>
1 parent 996ad57 commit 8442eb6

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

qemu/include/tcg/tcg.h

+7
Original file line numberDiff line numberDiff line change
@@ -1578,4 +1578,11 @@ struct jit_code_entry {
15781578
void uc_del_inline_hook(uc_engine *uc, struct hook *hk);
15791579
void uc_add_inline_hook(uc_engine *uc, struct hook *hk, void** args, int args_len);
15801580

1581+
static inline bool tcg_uc_has_hookmem(TCGContext *s)
1582+
{
1583+
return HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ) ||
1584+
HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ_AFTER) ||
1585+
HOOK_EXISTS(s->uc, UC_HOOK_MEM_WRITE);
1586+
}
1587+
15811588
#endif /* TCG_H */

qemu/tcg/aarch64/tcg-target.inc.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,8 @@ static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
15871587
MemOp opc = get_memop(oi);
15881588
MemOp size = opc & MO_SIZE;
15891589

1590-
if (!reloc_pc19(lb->label_ptr[0], s->code_ptr)) {
1590+
const int type = tcg_uc_has_hookmem(s) ? R_AARCH64_JUMP26 : R_AARCH64_CONDBR19;
1591+
if (!patch_reloc(lb->label_ptr[0], type, (intptr_t)s->code_ptr, 0)) {
15911592
return false;
15921593
}
15931594

@@ -1612,7 +1613,8 @@ static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
16121613
MemOp opc = get_memop(oi);
16131614
MemOp size = opc & MO_SIZE;
16141615

1615-
if (!reloc_pc19(lb->label_ptr[0], s->code_ptr)) {
1616+
const int type = tcg_uc_has_hookmem(s) ? R_AARCH64_JUMP26 : R_AARCH64_CONDBR19;
1617+
if (!patch_reloc(lb->label_ptr[0], type, (intptr_t)s->code_ptr, 0)) {
16161618
return false;
16171619
}
16181620

@@ -1711,7 +1713,11 @@ static void tcg_out_tlb_read(TCGContext *s, TCGReg addr_reg, MemOp opc,
17111713

17121714
/* If not equal, we jump to the slow path. */
17131715
*label_ptr = s->code_ptr;
1714-
tcg_out_insn(s, 3202, B_C, TCG_COND_NE, 0);
1716+
// Unicorn: fast path if hookmem is not enabled
1717+
if (!tcg_uc_has_hookmem(s))
1718+
tcg_out_insn(s, 3202, B_C, TCG_COND_NE, 0);
1719+
else
1720+
tcg_out_insn(s, 3206, B, 0);
17151721
}
17161722

17171723
#endif /* CONFIG_SOFTMMU */

qemu/tcg/i386/tcg-target.inc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ static inline void tcg_out_tlb_load(TCGContext *s, TCGReg addrlo, TCGReg addrhi,
17631763
tcg_out_mov(s, ttype, r1, addrlo);
17641764

17651765
// Unicorn: fast path if hookmem is not enable
1766-
if (!HOOK_EXISTS(s->uc, UC_HOOK_MEM_READ) && !HOOK_EXISTS(s->uc, UC_HOOK_MEM_WRITE))
1766+
if (!tcg_uc_has_hookmem(s))
17671767
tcg_out_opc(s, OPC_JCC_long + JCC_JNE, 0, 0, 0);
17681768
else
17691769
/* slow_path, so data access will go via load_helper() */

qemu/tcg/ppc/tcg-target.inc.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,8 @@ static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
20142014
MemOp opc = get_memop(oi);
20152015
TCGReg hi, lo, arg = TCG_REG_R3;
20162016

2017-
if (!reloc_pc14(lb->label_ptr[0], s->code_ptr)) {
2017+
const int type = tcg_uc_has_hookmem(s) ? R_PPC_REL24 : R_PPC_REL14;
2018+
if (!patch_reloc(lb->label_ptr[0], type, (intptr_t)s->code_ptr, 0)) {
20182019
return false;
20192020
}
20202021

@@ -2062,7 +2063,8 @@ static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
20622063
MemOp s_bits = opc & MO_SIZE;
20632064
TCGReg hi, lo, arg = TCG_REG_R3;
20642065

2065-
if (!reloc_pc14(lb->label_ptr[0], s->code_ptr)) {
2066+
const int type = tcg_uc_has_hookmem(s) ? R_PPC_REL24 : R_PPC_REL14;
2067+
if (!patch_reloc(lb->label_ptr[0], type, (intptr_t)s->code_ptr, 0)) {
20662068
return false;
20672069
}
20682070

@@ -2142,7 +2144,11 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
21422144

21432145
/* Load a pointer into the current opcode w/conditional branch-link. */
21442146
label_ptr = s->code_ptr;
2145-
tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
2147+
// Unicorn: fast path if hookmem is not enabled
2148+
if (!tcg_uc_has_hookmem(s))
2149+
tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
2150+
else
2151+
tcg_out32(s, B | LK);
21462152

21472153
rbase = TCG_REG_R3;
21482154
#else /* !CONFIG_SOFTMMU */
@@ -2217,7 +2223,11 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
22172223

22182224
/* Load a pointer into the current opcode w/conditional branch-link. */
22192225
label_ptr = s->code_ptr;
2220-
tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
2226+
// Unicorn: fast path if hookmem is not enabled
2227+
if (!tcg_uc_has_hookmem(s))
2228+
tcg_out32(s, BC | BI(7, CR_EQ) | BO_COND_FALSE | LK);
2229+
else
2230+
tcg_out32(s, B | LK);
22212231

22222232
rbase = TCG_REG_R3;
22232233
#else /* !CONFIG_SOFTMMU */

0 commit comments

Comments
 (0)