From patchwork Fri Feb 22 18:10:02 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 15033 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id A45B423F78 for ; Fri, 22 Feb 2013 18:10:20 +0000 (UTC) Received: from mail-vc0-f180.google.com (mail-vc0-f180.google.com [209.85.220.180]) by fiordland.canonical.com (Postfix) with ESMTP id 45647A18B66 for ; Fri, 22 Feb 2013 18:10:20 +0000 (UTC) Received: by mail-vc0-f180.google.com with SMTP id fo13so588522vcb.11 for ; Fri, 22 Feb 2013 10:10:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:x-forwarded-to:x-forwarded-for:delivered-to:x-received :received-spf:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references:x-gm-message-state; bh=DDr113a5T/b/NaN4PgzYFvuqq8NGzNYqwpCklmgIwsk=; b=ZC4fdiNNBuhxfWhfmGhItvvuuviRU0DsAlwTFo1vaAe8zV3pCCOu4od7DvLvrlKolx wQZBdS1QQlTV4hkBIS4kcww0vbx9sdDlMiMp/F0ODeL6DVwr6ini3Ax6FclQ4s3yr5bH O4MgUJx/JPnIY0XGjF2GXFB/sOIdfsZKYlDmkM0EtTcjetWPrWmOE1rhn1fei1GfhS/Q yrPFZ50mHlCquPJMqPzP8PXe/sDRub42sWYuRyLaMwXfMRITtSHw/ZF0jd1gIjI/F1Pp QhkPMUxSC1OYeCnV4QWgRh9KHYnKb/f5IvjXgh6g0u9woewPn0NOq+lWylFXLhYX3fRQ NzTw== X-Received: by 10.52.18.148 with SMTP id w20mr3430790vdd.8.1361556619771; Fri, 22 Feb 2013 10:10:19 -0800 (PST) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.58.145.101 with SMTP id st5csp101293veb; Fri, 22 Feb 2013 10:10:18 -0800 (PST) X-Received: by 10.66.3.205 with SMTP id e13mr3740126pae.144.1361556613673; Fri, 22 Feb 2013 10:10:13 -0800 (PST) Received: from mnementh.archaic.org.uk (1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.d.1.0.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:1d0::1]) by mx.google.com with ESMTPS id rf10si3218580pbc.126.2013.02.22.10.10.12 (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 22 Feb 2013 10:10:13 -0800 (PST) Received-SPF: neutral (google.com: 2001:8b0:1d0::1 is neither permitted nor denied by best guess record for domain of pm215@archaic.org.uk) client-ip=2001:8b0:1d0::1; Authentication-Results: mx.google.com; spf=neutral (google.com: 2001:8b0:1d0::1 is neither permitted nor denied by best guess record for domain of pm215@archaic.org.uk) smtp.mail=pm215@archaic.org.uk Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.72) (envelope-from ) id 1U8x4Q-0005im-0r; Fri, 22 Feb 2013 18:10:06 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Blue Swirl , =?UTF-8?q?Andreas=20F=C3=A4rber?= , Paul Brook , Anthony Liguori , Richard Henderson , Alexander Graf Subject: [PATCH 3/6] cpu-exec: wrap tcg_qemu_tb_exec() in a fn to restore the PC Date: Fri, 22 Feb 2013 18:10:02 +0000 Message-Id: <1361556605-21963-4-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1361556605-21963-1-git-send-email-peter.maydell@linaro.org> References: <1361556605-21963-1-git-send-email-peter.maydell@linaro.org> X-Gm-Message-State: ALoCoQke1tWUPdNLdSUkXsbKu/gC+0mDMpmMBYtvcHXUY2k7/hzu1BfKTadfdnv7lra+dgwNCdU6 If tcg_qemu_tb_exec() returns a value whose low bits don't indicate a link to an indexed next TB, this means that the TB execution never started (eg because the instruction counter hit zero). In this case the guest PC has to be reset to the address of the start of the TB. Refactor the cpu-exec code to make all tcg_qemu_tb_exec() calls pass through a wrapper function which does this restoration if necessary. Note that the apparent change in cpu_exec_nocache() from calling cpu_pc_from_tb() with the old TB to calling it with the TB returned by do_tcg_qemu_tb_exec() is safe, because in the nocache case we can guarantee that the TB we try to execute is not linked to any others, so the only possible returned TB is the one we started at. That is, we should arguably previously have included in cpu_exec_nocache() an assert(next_tb & ~TB_EXIT_MASK) == tb), since the API requires restore from next_tb but we were using tb. Signed-off-by: Peter Maydell --- cpu-exec.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/cpu-exec.c b/cpu-exec.c index ea63e7d..e80626a 100644 --- a/cpu-exec.c +++ b/cpu-exec.c @@ -51,13 +51,28 @@ void cpu_resume_from_signal(CPUArchState *env, void *puc) } #endif +/* Execute a TB, and fix up the CPU state afterwards if necessary */ +static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, uint8_t *tb_ptr) +{ + CPUArchState *env = cpu->env_ptr; + tcg_target_ulong next_tb = tcg_qemu_tb_exec(env, tb_ptr); + if ((next_tb & TB_EXIT_MASK) > TB_EXIT_IDX1) { + /* We didn't start executing this TB (eg because the instruction + * counter hit zero); we must restore the guest PC to the address + * of the start of the TB. + */ + TranslationBlock *tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK); + cpu_pc_from_tb(env, tb); + } + return next_tb; +} + /* Execute the code without caching the generated code. An interpreter could be used if available. */ static void cpu_exec_nocache(CPUArchState *env, int max_cycles, TranslationBlock *orig_tb) { CPUState *cpu = ENV_GET_CPU(env); - tcg_target_ulong next_tb; TranslationBlock *tb; /* Should never happen. @@ -69,14 +84,8 @@ static void cpu_exec_nocache(CPUArchState *env, int max_cycles, max_cycles); cpu->current_tb = tb; /* execute the generated code */ - next_tb = tcg_qemu_tb_exec(env, tb->tc_ptr); + cpu_tb_exec(cpu, tb->tc_ptr); cpu->current_tb = NULL; - - if ((next_tb & TB_EXIT_MASK) == TB_EXIT_ICOUNT_EXPIRED) { - /* Restore PC. This may happen if async event occurs before - the TB starts executing. */ - cpu_pc_from_tb(env, tb); - } tb_phys_invalidate(tb, -1); tb_free(tb); } @@ -598,13 +607,11 @@ int cpu_exec(CPUArchState *env) if (likely(!cpu->exit_request)) { tc_ptr = tb->tc_ptr; /* execute the generated code */ - next_tb = tcg_qemu_tb_exec(env, tc_ptr); + next_tb = cpu_tb_exec(cpu, tc_ptr); if ((next_tb & TB_EXIT_MASK) == TB_EXIT_ICOUNT_EXPIRED) { /* Instruction counter expired. */ int insns_left; tb = (TranslationBlock *)(next_tb & ~TB_EXIT_MASK); - /* Restore PC. */ - cpu_pc_from_tb(env, tb); insns_left = env->icount_decr.u32; if (env->icount_extra && insns_left >= 0) { /* Refill decrementer and continue execution. */