diff --git a/qemu/target-mips/translate.c b/qemu/target-mips/translate.c index 8dd8b5f3..75465915 100644 --- a/qemu/target-mips/translate.c +++ b/qemu/target-mips/translate.c @@ -18501,7 +18501,23 @@ static void gen_msa(CPUMIPSState *env, DisasContext *ctx) } } -static void decode_opc (CPUMIPSState *env, DisasContext *ctx, bool *insn_need_patch) +// Unicorn: trace this instruction on request +static void hook_insn(CPUMIPSState *env, DisasContext *ctx, bool *insn_need_patch, int *insn_patch_offset, int offset_value) +{ + if (env->uc->hook_insn) { + TCGContext *tcg_ctx = ctx->uc->tcg_ctx; + struct hook_struct *trace = hook_find(env->uc, UC_HOOK_CODE, ctx->pc); + if (trace) { + gen_uc_tracecode(tcg_ctx, 0xf8f8f8f8, trace->callback, env->uc, ctx->pc, trace->user_data); + *insn_need_patch = true; + } + // the callback might want to stop emulation immediately + check_exit_request(tcg_ctx); + *insn_patch_offset = offset_value; + } +} + +static void decode_opc (CPUMIPSState *env, DisasContext *ctx, bool *insn_need_patch, int *insn_patch_offset) { TCGContext *tcg_ctx = ctx->uc->tcg_ctx; #if defined(TARGET_MIPS64) @@ -18519,17 +18535,6 @@ static void decode_opc (CPUMIPSState *env, DisasContext *ctx, bool *insn_need_pa return; } - // Unicorn: trace this instruction on request - if (env->uc->hook_insn) { - struct hook_struct *trace = hook_find(env->uc, UC_HOOK_CODE, ctx->pc); - if (trace) { - gen_uc_tracecode(tcg_ctx, 0xf8f8f8f8, trace->callback, env->uc, ctx->pc, trace->user_data); - *insn_need_patch = true; - } - // the callback might want to stop emulation immediately - check_exit_request(tcg_ctx); - } - /* Handle blikely not taken case */ if ((ctx->hflags & MIPS_HFLAG_BMASK_BASE) == MIPS_HFLAG_BL) { int l1 = gen_new_label(tcg_ctx); @@ -18539,6 +18544,9 @@ static void decode_opc (CPUMIPSState *env, DisasContext *ctx, bool *insn_need_pa tcg_gen_movi_i32(tcg_ctx, tcg_ctx->hflags, ctx->hflags & ~MIPS_HFLAG_BMASK); gen_goto_tb(ctx, 1, ctx->pc + 4); gen_set_label(tcg_ctx, l1); + hook_insn(env, ctx, insn_need_patch, insn_patch_offset, 14); + } else { + hook_insn(env, ctx, insn_need_patch, insn_patch_offset, 1); } if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) { @@ -19264,6 +19272,8 @@ gen_intermediate_code_internal(MIPSCPU *cpu, TranslationBlock *tb, break; } else { bool insn_need_patch = false; + int insn_patch_offset = 1; + // Unicorn: save param buffer if (env->uc->hook_insn) save_opparam_ptr = tcg_ctx->gen_opparam_ptr; @@ -19273,7 +19283,7 @@ gen_intermediate_code_internal(MIPSCPU *cpu, TranslationBlock *tb, if (!(ctx.hflags & MIPS_HFLAG_M16)) { ctx.opcode = cpu_ldl_code(env, ctx.pc); insn_bytes = 4; - decode_opc(env, &ctx, &insn_need_patch); + decode_opc(env, &ctx, &insn_need_patch, &insn_patch_offset); } else if (ctx.insn_flags & ASE_MICROMIPS) { ctx.opcode = cpu_lduw_code(env, ctx.pc); insn_bytes = decode_micromips_opc(env, &ctx, &insn_need_patch); @@ -19287,8 +19297,15 @@ gen_intermediate_code_internal(MIPSCPU *cpu, TranslationBlock *tb, } // Unicorn: patch the callback for the instruction size - if (insn_need_patch) - *(save_opparam_ptr + 1) = insn_bytes; + if (insn_need_patch) { + /* + int i; + for (i = 0; i < 30; i++) + printf("[%u] = %x\n", i, *(save_opparam_ptr + i)); + printf("\n"); + */ + *(save_opparam_ptr + insn_patch_offset) = insn_bytes; + } } if (ctx.hflags & MIPS_HFLAG_BMASK) { diff --git a/tests/regress/mips_branch_likely_issue.c b/tests/regress/mips_branch_likely_issue.c index 039d50e1..84f49cdc 100644 --- a/tests/regress/mips_branch_likely_issue.c +++ b/tests/regress/mips_branch_likely_issue.c @@ -33,38 +33,44 @@ const uint64_t addr = 0x100000; -// This code SHOULD execute the instruction at 0x10000C. +// This code SHOULD execute the instruction at 0x100010. const unsigned char test_code_1[] = { - 0x01,0x00,0x02,0x24, // 100000: li $v0, 1 - 0x02,0x00,0x03,0x24, // 100004: li $v1, 2 - 0x01,0x00,0x62,0x54, // 100008: bnel $v1, $v0, 0x100010 - 0x00,0x00,0x00,0x00, // 10000C: nop + 0x00,0x00,0x04,0x24, // 100000: li $a0, 0 + 0x01,0x00,0x02,0x24, // 100004: li $v0, 1 + 0x02,0x00,0x03,0x24, // 100008: li $v1, 2 + 0x01,0x00,0x62,0x54, // 10000C: bnel $v1, $v0, 0x100014 + 0x21,0x20,0x62,0x00, // 100010: addu $a0, $v1, $v0 }; -// This code SHOULD NOT execute the instruction at 0x10000C. +// This code SHOULD NOT execute the instruction at 0x100010. const unsigned char test_code_2[] = { - 0x01,0x00,0x02,0x24, // 100000: li $v0, 1 - 0x01,0x00,0x03,0x24, // 100004: li $v1, 1 - 0x01,0x00,0x62,0x54, // 100008: bnel $v1, $v0, 0x100010 - 0x00,0x00,0x00,0x00, // 10000C: nop + 0x00,0x00,0x04,0x24, // 100000: li $a0, 0 + 0x01,0x00,0x02,0x24, // 100004: li $v0, 1 + 0x01,0x00,0x03,0x24, // 100008: li $v1, 1 + 0x01,0x00,0x62,0x54, // 10000C: bnel $v1, $v0, 0x100014 + 0x21,0x20,0x62,0x00, // 100010: addu $a0, $v1, $v0 }; int test_num = 0; +// flag for whether the delay slot was executed by the emulator bool test1_delayslot_executed = false; bool test2_delayslot_executed = false; +// flag for whether the delay slot had a code hook called for it +bool test1_delayslot_hooked = false; +bool test2_delayslot_hooked = false; // This hook is used to show that code is executing in the emulator. static void mips_codehook(uc_engine *uc, uint64_t address, uint32_t size, void *user_data) { printf("Test %d Executing: %llX\n", test_num, address); - if( test_num == 1 && address == 0x10000C ) + if( test_num == 1 && address == 0x100010 ) { - printf("Delay slot executed!\n"); - test1_delayslot_executed = true; + printf("Delay slot hook called!\n"); + test1_delayslot_hooked = true; } - if( test_num == 2 && address == 0x10000C ) + if( test_num == 2 && address == 0x100010 ) { - printf("Delay slot executed!\n"); - test2_delayslot_executed = true; + printf("Delay slot hook called!\n"); + test2_delayslot_hooked = true; } } @@ -74,6 +80,7 @@ int main(int argc, char **argv, char **envp) uc_engine *uc; uc_err err; uc_hook hhc; + uint32_t val; // dynamically load shared library #ifdef DYNLOAD @@ -120,6 +127,10 @@ int main(int argc, char **argv, char **envp) // start executing test code 1 printf("uc_emu_start(1)\n"); uc_emu_start(uc, addr, addr+sizeof(test_code_1), 0, 0); + // read the value from a0 when finished executing + uc_reg_read(uc, UC_MIPS_REG_A0, &val); printf("a0 is %X\n", val); + if( val != 0 ) + test1_delayslot_executed = true; // write test2 code to be emulated to memory @@ -134,26 +145,48 @@ int main(int argc, char **argv, char **envp) // start executing test code 2 printf("uc_emu_start(2)\n"); uc_emu_start(uc, addr, addr+sizeof(test_code_2), 0, 0); + // read the value from a0 when finished executing + uc_reg_read(uc, UC_MIPS_REG_A0, &val); printf("a0 is %X\n", val); + if( val != 0 ) + test2_delayslot_executed = true; // free resources printf("\nuc_close()\n"); uc_close(uc); - // print test results - // test 1 SHOULD execute the instruction at 0x10000C. - if( test1_delayslot_executed == true ) + // print test results + printf("\n\nTest 1 SHOULD execute the delay slot instruction:\n"); + printf(" Emulator %s execute the delay slot: %s\n", + test1_delayslot_executed ? "did" : "did not", + test1_delayslot_executed ? "CORRECT" : "WRONG"); + printf(" Emulator %s hook the delay slot: %s\n", + test1_delayslot_hooked ? "did" : "did not", + test1_delayslot_hooked ? "CORRECT" : "WRONG"); + + printf("\n\nTest 2 SHOULD NOT execute the delay slot instruction:\n"); + printf(" Emulator %s execute the delay slot: %s\n", + test2_delayslot_executed ? "did" : "did not", + !test2_delayslot_executed ? "CORRECT" : "WRONG"); + printf(" Emulator %s hook the delay slot: %s\n", + test2_delayslot_hooked ? "did" : "did not", + !test2_delayslot_hooked ? "CORRECT" : "WRONG"); + + + // test 1 SHOULD execute the instruction in the delay slot + if( test1_delayslot_hooked == true && test1_delayslot_executed == true ) printf("\n\nTEST 1 PASSED!\n"); else printf("\n\nTEST 1 FAILED!\n"); - // test 2 SHOULD NOT execute the instruction at 0x10000C. - if( test2_delayslot_executed == false ) + // test 2 SHOULD NOT execute the instruction in the delay slot + if( test2_delayslot_hooked == false && test2_delayslot_executed == false ) printf("TEST 2 PASSED!\n\n"); else printf("TEST 2 FAILED!\n\n"); + // dynamically free shared library #ifdef DYNLOAD uc_dyn_free();