diff --git a/qemu/accel/tcg/translator.c b/qemu/accel/tcg/translator.c index 36193d5d..12bfbec6 100644 --- a/qemu/accel/tcg/translator.c +++ b/qemu/accel/tcg/translator.c @@ -114,6 +114,7 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db, or we have executed all of the allowed instructions. */ if (tcg_op_buf_full(tcg_ctx) || db->num_insns >= max_insns) { db->is_jmp = DISAS_TOO_MANY; + db->uc->block_full = true; break; } } diff --git a/qemu/target/i386/translate.c b/qemu/target/i386/translate.c index dba92b05..2fca09fb 100644 --- a/qemu/target/i386/translate.c +++ b/qemu/target/i386/translate.c @@ -9223,9 +9223,24 @@ static int i386_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cpu, // done with initializing TCG variables env->uc->init_tcg = true; + // Unicorn: trace this block on request + // Only hook this block if the previous block was not truncated due to space + if (!env->uc->block_full && HOOK_EXISTS_BOUNDED(env->uc, UC_HOOK_BLOCK, dc->base.pc_first)) { + int arg_i = tcg_ctx->gen_op_buf[tcg_ctx->gen_op_buf[0].prev].args; + env->uc->block_addr = dc->base.pc_first; + env->uc->size_arg = arg_i + 1; + gen_uc_tracecode(tcg_ctx, 0xf8f8f8f8, UC_HOOK_BLOCK_IDX, env->uc, dc->base.pc_first); + } else { + env->uc->size_arg = -1; + } + return max_insns; } +static void i386_tr_tb_start(DisasContextBase *db, CPUState *cpu) +{ +} + static void i386_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu) { DisasContext *dc = container_of(dcbase, DisasContext, base); @@ -9246,7 +9261,7 @@ static bool i386_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu, /* The address covered by the breakpoint must be included in [tb->pc, tb->pc + tb->size) in order to for it to be properly cleared -- thus we increment the PC here so that - the logic setting tb->size below does the right thing. */ + the generic logic setting tb->size later does the right thing. */ dc->base.pc_next += 1; return true; } else { @@ -9313,132 +9328,21 @@ static void i386_tr_disas_log(const DisasContextBase *dcbase, #endif } +static const TranslatorOps i386_tr_ops = { + i386_tr_init_disas_context, + i386_tr_tb_start, + i386_tr_insn_start, + i386_tr_breakpoint_check, + i386_tr_translate_insn, + i386_tr_tb_stop, + i386_tr_disas_log, +}; + /* generate intermediate code for basic block 'tb'. */ -void gen_intermediate_code(CPUState *cs, TranslationBlock *tb) +void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb) { - CPUX86State *env = cs->env_ptr; - DisasContext dc1, *dc = &dc1; - TCGContext *tcg_ctx = env->uc->tcg_ctx; - int num_insns = 0; - int max_insns; - bool block_full = false; - - /* generate intermediate code */ - dc->base.singlestep_enabled = cs->singlestep_enabled; - dc->base.tb = tb; - dc->base.is_jmp = DISAS_NEXT; - dc->base.pc_first = tb->pc; - dc->base.pc_next = dc->base.pc_first; - - // early check to see if the address of this block is the until address - if (tb->pc == env->uc->addr_end) { - // imitate the HLT instruction - gen_tb_start(tcg_ctx, tb); - gen_jmp_im(dc, tb->pc - tb->cs_base); - gen_helper_hlt(tcg_ctx, tcg_ctx->cpu_env, tcg_const_i32(tcg_ctx, 0)); - dc->base.is_jmp = DISAS_NORETURN; - goto done_generating; - } - - dc->base.is_jmp = DISAS_NEXT; - 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 = i386_tr_init_disas_context(&dc->base, cs, max_insns); - - // Unicorn: trace this block on request - // Only hook this block if the previous block was not truncated due to space - if (!env->uc->block_full && HOOK_EXISTS_BOUNDED(env->uc, UC_HOOK_BLOCK, dc->base.pc_first)) { - int arg_i = tcg_ctx->gen_op_buf[tcg_ctx->gen_op_buf[0].prev].args; - env->uc->block_addr = dc->base.pc_first; - env->uc->size_arg = arg_i + 1; - 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); - for(;;) { - i386_tr_insn_start(&dc->base, cs); - num_insns++; - - /* If RF is set, suppress an internally generated breakpoint. */ - if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) { - CPUBreakpoint *bp; - QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { - if (bp->pc == dc->base.pc_next) { - if (i386_tr_breakpoint_check(&dc->base, cs, bp)) { - break; - } - } - } - - if (dc->base.is_jmp == DISAS_NORETURN) { - break; - } - } - - // Unicorn: commented out - //if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) { - // gen_io_start(); - //} - - // Unicorn: save current PC address to sync EIP - dc->prev_pc = dc->base.pc_next; - i386_tr_translate_insn(&dc->base, cs); - /* stop translation if indicated */ - if (dc->base.is_jmp) { - break; - } - /* if single step mode, we generate only one instruction and - generate an exception */ - if (dc->base.singlestep_enabled) { - dc->base.is_jmp = DISAS_TOO_MANY; - break; - } - /* if too long translation, stop generation too */ - if (tcg_op_buf_full(tcg_ctx) || - num_insns >= max_insns) { - dc->base.is_jmp = DISAS_TOO_MANY; - block_full = true; - break; - } - - // Unicorn: if'd out - #if 0 - if (singlestep) { - dc->base.is_jmp = DISAS_TOO_MANY; - break; - } - #endif - } - i386_tr_tb_stop(&dc->base, cs); - //if (tb->cflags & CF_LAST_IO) { - // gen_io_end(); - //} -// Unicorn: Left in for unicorn. -done_generating: - gen_tb_end(tcg_ctx, tb, num_insns); - - tb->size = dc->base.pc_next - dc->base.pc_first; - tb->icount = num_insns; - -#ifdef DEBUG_DISAS - if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) - && qemu_log_in_addr_range(dc->base.pc_first)) { - //qemu_log_lock(); - qemu_log("----------------\n"); - i386_tr_disas_log(&dc->base, cs); - qemu_log("\n"); - //qemu_log_unlock(); - } -#endif - - env->uc->block_full = block_full; + DisasContext dc; + translator_loop(&i386_tr_ops, &dc.base, cpu, tb); } void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb,