From 784d580f0139e9ac383ebde1a75179998e93d1d3 Mon Sep 17 00:00:00 2001 From: Charles Ferguson Date: Tue, 14 Jan 2020 09:49:41 -0500 Subject: [PATCH] Ensure that PC is not fixed up when code tracing or timing. (#1179) Under some circumstances, the PC is not fixed up properly when returning from the execution of a block in cpu_tb_exec. This appears to be caused by the resetting of the PC from the tb. This change removes the additional fixup in the cases where there is code tracing or timing active. Either of these cases would result in the wrong PC being reported. Closes unicorn-engine#1105. Backports commit b59632fb645d456338472e3d757c065c0ed74ad5 from unicorn --- qemu/accel/tcg/cpu-exec.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/qemu/accel/tcg/cpu-exec.c b/qemu/accel/tcg/cpu-exec.c index ae02e407..901c8d4d 100644 --- a/qemu/accel/tcg/cpu-exec.c +++ b/qemu/accel/tcg/cpu-exec.c @@ -54,33 +54,34 @@ static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, TranslationBlock *itb) * of the start of the TB. */ CPUClass *cc = CPU_GET_CLASS(env->uc, cpu); - // Unicorn: commented out - //qemu_log_mask_and_addr(CPU_LOG_EXEC, last_tb->pc, - // "Stopped execution of TB chain before %p [" - // TARGET_FMT_lx "] %s\n", - // last_tb->tc.ptr, last_tb->pc, - // lookup_symbol(last_tb->pc)); - if (cc->synchronize_from_tb) { - // avoid sync twice when helper_uc_tracecode() already did this. - if (env->uc->emu_counter <= env->uc->emu_count && - !env->uc->stop_request && !env->uc->quit_request) { - cc->synchronize_from_tb(cpu, last_tb); - } - } else { - assert(cc->set_pc); - // avoid sync twice when helper_uc_tracecode() already did this. - if (env->uc->emu_counter <= env->uc->emu_count && - !env->uc->stop_request && !env->uc->quit_request) { - cc->set_pc(cpu, last_tb->pc); + + /* + * Both set_pc() & synchronize_fromtb() can be ignored when code tracing hook is installed, + * or timer mode is in effect, since these already fix the PC. + */ + if (!HOOK_EXISTS(env->uc, UC_HOOK_CODE) && !env->uc->timeout) { + if (cc->synchronize_from_tb) { + // avoid sync twice when helper_uc_tracecode() already did this. + if (env->uc->emu_counter <= env->uc->emu_count && + !env->uc->stop_request && !env->uc->quit_request) + cc->synchronize_from_tb(cpu, last_tb); + } else { + assert(cc->set_pc); + // avoid sync twice when helper_uc_tracecode() already did this. + if (env->uc->emu_counter <= env->uc->emu_count && + !env->uc->stop_request && !env->uc->quit_request) + cc->set_pc(cpu, last_tb->pc); } } } + if (tb_exit == TB_EXIT_REQUESTED) { /* We were asked to stop executing TBs (probably a pending * interrupt. We've now stopped, so clear the flag. */ atomic_set(&cpu->tcg_exit_req, 0); } + return ret; }