diff --git a/qemu/target/arm/translate.c b/qemu/target/arm/translate.c index 9e2f1389..1e568895 100644 --- a/qemu/target/arm/translate.c +++ b/qemu/target/arm/translate.c @@ -12271,117 +12271,16 @@ static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) dc->base.pc_next = dc->pc; } -/* generate intermediate code for basic block 'tb'. */ -void gen_intermediate_code(CPUState *cs, TranslationBlock *tb) +static void arm_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu) { - TCGContext *tcg_ctx = cs->uc->tcg_ctx; - CPUARMState *env = cs->env_ptr; - DisasContext dc1, *dc = &dc1; - int max_insns; - bool block_full = false; + DisasContext *dc = container_of(dcbase, DisasContext, base); + TCGContext *tcg_ctx = cpu->uc->tcg_ctx; - /* generate intermediate code */ - - /* The A64 decoder has its own top level loop, because it doesn't need - * the A32/T32 complexity to do with conditional execution/IT blocks/etc. - */ - if (ARM_TBFLAG_AARCH64_STATE(tb->flags)) { - gen_intermediate_code_a64(&dc->base, cs, tb); - return; + if (dc->base.tb->cflags & CF_LAST_IO && dc->condjmp) { + /* FIXME: This can theoretically happen with self-modifying code. */ + cpu_abort(cpu, "IO on conditional branch instruction"); } - dc->base.tb = tb; - dc->base.pc_first = dc->base.tb->pc; - dc->base.pc_next = dc->base.pc_first; - dc->base.is_jmp = DISAS_NEXT; - dc->base.num_insns = 0; - dc->base.singlestep_enabled = cs->singlestep_enabled; - - max_insns = tb->cflags & CF_COUNT_MASK; - if (max_insns == 0) { - max_insns = CF_COUNT_MASK; - } - if (max_insns > TCG_MAX_INSNS) { - max_insns = TCG_MAX_INSNS; - } - max_insns = arm_tr_init_disas_context(&dc->base, cs, max_insns); - - tcg_clear_temp_count(); - - // Unicorn: early check to see if the address of this block is the until address - if (tb->pc == env->uc->addr_end) { - // imitate WFI instruction to halt emulation - gen_tb_start(tcg_ctx, tb); - dc->base.is_jmp = DISAS_WFI; - goto tb_end; - } - - // Unicorn: trace this block on request - // Only hook this block if it is not broken from previous translation due to - // full translation cache - if (!env->uc->block_full && HOOK_EXISTS_BOUNDED(env->uc, UC_HOOK_BLOCK, dc->base.pc_first)) { - // save block address to see if we need to patch block size later - env->uc->block_addr = dc->base.pc_first; - env->uc->size_arg = tcg_ctx->gen_op_buf[tcg_ctx->gen_op_buf[0].prev].args; - gen_uc_tracecode(tcg_ctx, 0xf8f8f8f8, UC_HOOK_BLOCK_IDX, env->uc, dc->base.pc_first); - } else { - env->uc->size_arg = -1; - } - - gen_tb_start(tcg_ctx, tb); - arm_tr_tb_start(&dc->base, cs); - - do { - dc->base.num_insns++; - arm_tr_insn_start(&dc->base, cs); - - if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) { - CPUBreakpoint *bp; - QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { - if (bp->pc == dc->base.pc_next) { - if (arm_tr_breakpoint_check(&dc->base, cs, bp)) { - break; - } - } - } - if (dc->base.is_jmp > DISAS_TOO_MANY) { - break; - } - } - - //if (dc->base.num_insns == max_insns && (tb->cflags & CF_LAST_IO)) { - // gen_io_start(); - //} - - arm_tr_translate_insn(&dc->base, cs); - - if (tcg_check_temp_count()) { - fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n", - dc->pc); - } - - if (!dc->base.is_jmp && (tcg_op_buf_full(tcg_ctx) || //singlestep || // Unicorn: commented out - dc->base.num_insns >= max_insns)) { - dc->base.is_jmp = DISAS_TOO_MANY; - } - } while (!dc->base.is_jmp); - - if (tb->cflags & CF_LAST_IO) { - if (dc->condjmp) { - /* FIXME: This can theoretically happen with self-modifying - code. */ - cpu_abort(cs, "IO on conditional branch instruction"); - } - //gen_io_end(); - } - - /* if too long translation, save this info */ - if (tcg_op_buf_full(tcg_ctx) || dc->base.num_insns >= max_insns) { - block_full = true; - } - -tb_end: - /* At this stage dc->condjmp will only be set when the skipped instruction was a conditional branch or trap, and the PC has already been written. */ @@ -12485,10 +12384,119 @@ tb_end: gen_goto_tb(dc, 1, dc->pc); } } +} + +/* generate intermediate code for basic block 'tb'. */ +void gen_intermediate_code(CPUState *cs, TranslationBlock *tb) +{ + DisasContext dc1, *dc = &dc1; + CPUARMState *env = cs->env_ptr; + TCGContext *tcg_ctx = cs->uc->tcg_ctx; + int max_insns; + + /* generate intermediate code */ + + /* The A64 decoder has its own top level loop, because it doesn't need + * the A32/T32 complexity to do with conditional execution/IT blocks/etc. + */ + if (ARM_TBFLAG_AARCH64_STATE(tb->flags)) { + gen_intermediate_code_a64(&dc->base, cs, tb); + return; + } + + dc->base.tb = tb; + dc->base.pc_first = dc->base.tb->pc; + dc->base.pc_next = dc->base.pc_first; + dc->base.is_jmp = DISAS_NEXT; + dc->base.num_insns = 0; + dc->base.singlestep_enabled = cs->singlestep_enabled; + + max_insns = tb->cflags & CF_COUNT_MASK; + if (max_insns == 0) { + max_insns = CF_COUNT_MASK; + } + if (max_insns > TCG_MAX_INSNS) { + max_insns = TCG_MAX_INSNS; + } + max_insns = arm_tr_init_disas_context(&dc->base, cs, max_insns); + + gen_tb_start(tcg_ctx, tb); + + tcg_clear_temp_count(); + arm_tr_tb_start(&dc->base, cs); + +// Unicorn: early check to see if the address of this block is the until address + if (tb->pc == env->uc->addr_end) { + // imitate WFI instruction to halt emulation + gen_tb_start(tcg_ctx, tb); + dc->base.is_jmp = DISAS_WFI; + goto tb_end; + } + + // Unicorn: trace this block on request + // Only hook this block if it is not broken from previous translation due to + // full translation cache + if (!env->uc->block_full && HOOK_EXISTS_BOUNDED(env->uc, UC_HOOK_BLOCK, dc->base.pc_first)) { + // save block address to see if we need to patch block size later + env->uc->block_addr = dc->base.pc_first; + env->uc->size_arg = tcg_ctx->gen_op_buf[tcg_ctx->gen_op_buf[0].prev].args; + gen_uc_tracecode(tcg_ctx, 0xf8f8f8f8, UC_HOOK_BLOCK_IDX, env->uc, dc->base.pc_first); + } else { + env->uc->size_arg = -1; + } + + do { + dc->base.num_insns++; + arm_tr_insn_start(&dc->base, cs); + + if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) { + CPUBreakpoint *bp; + QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { + if (bp->pc == dc->base.pc_next) { + if (arm_tr_breakpoint_check(&dc->base, cs, bp)) { + break; + } + } + } + if (dc->base.is_jmp > DISAS_TOO_MANY) { + break; + } + } + + // Unicorn: commented out +#if 0 + if (dc->base.num_insns == max_insns && (tb->cflags & CF_LAST_IO)) { + gen_io_start(); + } +#endif + + arm_tr_translate_insn(&dc->base, cs); + + if (tcg_check_temp_count()) { + fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n", + dc->pc); + } + + if (!dc->base.is_jmp && (tcg_op_buf_full(tcg_ctx) || // singlestep || // Unicorn: commented out + dc->base.num_insns >= max_insns)) { + dc->base.is_jmp = DISAS_TOO_MANY; + env->uc->block_full = true; + } + } while (!dc->base.is_jmp); + + // Unicorn: if'd out +#if 0 + if (dc->base.tb->cflags & CF_LAST_IO) { + gen_io_end(); + } +#endif + +tb_end: + arm_tr_tb_stop(&dc->base, cs); gen_tb_end(tcg_ctx, tb, dc->base.num_insns); - // Unicorn: commented out + // Unicorn: if'd out #if 0 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(dc->base.pc_first)) { @@ -12504,8 +12512,6 @@ tb_end: tb->size = dc->pc - dc->base.pc_first; tb->icount = dc->base.num_insns; - - env->uc->block_full = block_full; } #if 0