From nobody Sat Oct 25 11:05:05 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1521429427660534.2589967195489; Sun, 18 Mar 2018 20:17:07 -0700 (PDT) Received: from localhost ([::1]:40000 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1exlII-0006P7-D4 for importer@patchew.org; Sun, 18 Mar 2018 23:17:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54092) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1exlHC-000642-0k for qemu-devel@nongnu.org; Sun, 18 Mar 2018 23:15:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1exlH8-0000L3-Rv for qemu-devel@nongnu.org; Sun, 18 Mar 2018 23:15:58 -0400 Received: from mail-pl0-x243.google.com ([2607:f8b0:400e:c01::243]:33355) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1exlH8-0000II-Jr for qemu-devel@nongnu.org; Sun, 18 Mar 2018 23:15:54 -0400 Received: by mail-pl0-x243.google.com with SMTP id c11-v6so9431915plo.0 for ; Sun, 18 Mar 2018 20:15:54 -0700 (PDT) Received: from cloudburst.twiddle.net ([218.255.99.6]) by smtp.gmail.com with ESMTPSA id y83sm5655228pfk.123.2018.03.18.20.15.50 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 18 Mar 2018 20:15:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=60i/2lyrnxPySo8aKd24VqV8n85U+r6l0cE+HK2c7Ow=; b=ZjOuLxW8HB/6v6hY2E+3hdBhRN74jsXDTFr5QlpiXP6nGDnqjHpcU7yxUveKvupPnK iQRpAuIk+mtiK9mFlbObO+qDS5evkXtXUGCrl/awYxSL3rZx5ENs7osB10emt+9jMfOT pkDw/5XJiaS1dnWU67jIf9VcoU+/ruK5EfBJo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=60i/2lyrnxPySo8aKd24VqV8n85U+r6l0cE+HK2c7Ow=; b=GTpRgMPB+FEytfSVIQGVgjXvh540jg4iNHTdNCxGOQg+rnhjWDFNs20JwlxEPc8tyA E1u9IbmwZS4+eGZwY2qVIYH+h8pKvaZ002rS1vnNWTRHwbMFISlovOdyfLNKKxHwvtW9 Z9nmETEfgz5HM2pTlgVaokl5SCDZ2dqPnrv+cyUhaFq2PMhEM2vrMi3IPTV1oMLtU5Qj CiP7+uSAqtm5SHi1sGJdu9Nt4peKZTCfppHoBvFZvY5MCo979CL2KZGHrVVPa3y/ekTX VPq7wZPePeRWgMOPjMjM0G7eSAeezFs+CGGRojIaRXgZxkOEzSedsBLFtLtH1I4wqDnX ggZQ== X-Gm-Message-State: AElRT7ExhA2oXYlcScDOTQ98P1e6SxOpCd1IQGg5mVWhYKvS1nbXsSl9 P1ZuzK9Zsq3kxxTLMHDtGCxSGiuxG2o= X-Google-Smtp-Source: AG47ELvyNF6oC5sPiTFe4xtEbhVipION33I/gZv8TwnlGsIDbirOnUgL9IlxHrqqXjoNmh3NGxZJjw== X-Received: by 2002:a17:902:167:: with SMTP id 94-v6mr10922467plb.294.1521429352623; Sun, 18 Mar 2018 20:15:52 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 19 Mar 2018 11:15:45 +0800 Message-Id: <20180319031545.29359-1-richard.henderson@linaro.org> X-Mailer: git-send-email 2.14.3 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c01::243 Subject: [Qemu-devel] [PATCH v2] tcg: Really fix cpu_io_recompile X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, Pavel.Dovgaluk@ispras.ru, pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We have confused the number of instructions that have been executed in the TB with the number of instructions needed to repeat the I/O instruction. We have used cpu_restore_state_from_tb, which means that the guest pc is pointing to the I/O instruction. The only time the answer to the later question is not 1 is when MIPS or SH4 need to re-execute the branch for the delay slot as well. We must rely on cpu->cflags_next_tb to generate the next TB, as otherwise we have a race condition with other guest cpus within the TB cache. Fixes: 0790f86861079b1932679d0f011e431aaf4ee9e2 Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Pavel Dovgalyuk --- My v1 raced with Paolo's pull request, so v2 now fixes Pavel's fix. r~ --- accel/tcg/translate-all.c | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 5ad1b919bc..d4190602d1 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1728,8 +1728,7 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retadd= r) CPUArchState *env =3D cpu->env_ptr; #endif TranslationBlock *tb; - uint32_t n, flags; - target_ulong pc, cs_base; + uint32_t n; =20 tb_lock(); tb =3D tb_find_pc(retaddr); @@ -1737,44 +1736,33 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t reta= ddr) cpu_abort(cpu, "cpu_io_recompile: could not find TB for pc=3D%p", (void *)retaddr); } - n =3D cpu->icount_decr.u16.low + tb->icount; cpu_restore_state_from_tb(cpu, tb, retaddr); - /* Calculate how many instructions had been executed before the fault - occurred. */ - n =3D n - cpu->icount_decr.u16.low; - /* Generate a new TB ending on the I/O insn. */ - n++; + /* On MIPS and SH, delay slot instructions can only be restarted if they were already the first instruction in the TB. If this is not the first instruction in a TB then re-execute the preceding branch. */ + n =3D 1; #if defined(TARGET_MIPS) - if ((env->hflags & MIPS_HFLAG_BMASK) !=3D 0 && n > 1) { + if ((env->hflags & MIPS_HFLAG_BMASK) !=3D 0 + && env->active_tc.PC !=3D tb->pc) { env->active_tc.PC -=3D (env->hflags & MIPS_HFLAG_B16 ? 2 : 4); cpu->icount_decr.u16.low++; env->hflags &=3D ~MIPS_HFLAG_BMASK; + n =3D 2; } #elif defined(TARGET_SH4) if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) !=3D 0 - && n > 1) { + && env->pc !=3D tb->pc) { env->pc -=3D 2; cpu->icount_decr.u16.low++; env->flags &=3D ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); + n =3D 2; } #endif - /* This should never happen. */ - if (n > CF_COUNT_MASK) { - cpu_abort(cpu, "TB too big during recompile"); - } =20 - pc =3D tb->pc; - cs_base =3D tb->cs_base; - flags =3D tb->flags; - tb_phys_invalidate(tb, -1); - - /* Execute one IO instruction without caching - instead of creating large TB. */ - cpu->cflags_next_tb =3D curr_cflags() | CF_LAST_IO | CF_NOCACHE | 1; + /* Generate a new TB executing the I/O insn. */ + cpu->cflags_next_tb =3D curr_cflags() | CF_LAST_IO | n; =20 if (tb->cflags & CF_NOCACHE) { if (tb->orig_tb) { @@ -1785,11 +1773,6 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retad= dr) tb_remove(tb); } =20 - /* Generate new TB instead of the current one. */ - /* FIXME: In theory this could raise an exception. In practice - we have already translated the block once so it's probably ok. */ - tb_gen_code(cpu, pc, cs_base, flags, curr_cflags() | CF_LAST_IO | n); - /* TODO: If env->pc !=3D tb->pc (i.e. the faulting instruction was not * the first in the TB) then we end up generating a whole new TB and * repeating the fault, which is horribly inefficient. --=20 2.14.3