From patchwork Mon Mar 19 03:15:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 131995 Delivered-To: patch@linaro.org Received: by 10.46.84.17 with SMTP id i17csp2233256ljb; Sun, 18 Mar 2018 20:16:17 -0700 (PDT) X-Google-Smtp-Source: AG47ELthJAEkEQubvA2vsEAd8KJJfRNY6KjM52thWsypbnC9TAWI03YjaHCXy8FU3quKDzoqvZtk X-Received: by 10.55.164.212 with SMTP id n203mr15390829qke.342.1521429376928; Sun, 18 Mar 2018 20:16:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521429376; cv=none; d=google.com; s=arc-20160816; b=LGbrXKOhzpYYpuV4931ePAOuIv06jhdXhukczoczdp5Nvh6c/1KTaqBJ+591gdDHyV c6vD1K/dndCLXhB/7+AOtT2Xpfp9m360K1aaSgXFQbAJycxF1D0TWgqHj8tMFRNqktVE x0SeRLarqvQ5jEOv7W4pv6Sbf/emL4naVxxsbfIPQsyO7noVm+OuvXpmLebVnzp80rGu oiTsNF2+qFtFB+QyUQj6TgEIV9rbODBulZWa6CYPxilopAH+kEj8jQ+XG1+rvD76++jJ JBaZX7jZ6s0keytvWKfhGDLYKbydtJASYqxgzlI8+0eAuYnZMN1trrqwZdpN6Bys17rH 38Dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:message-id:date:to:from :dkim-signature:arc-authentication-results; bh=60i/2lyrnxPySo8aKd24VqV8n85U+r6l0cE+HK2c7Ow=; b=px0LyKU3OCRE1sn48Z5jhvdvruxRq0cEJOhRjqzlzv1/NtxXzoqQfNpSwvhunwFHFD G4mHHdh4n5lu+lC6f+kH5FqzS81lZ1JmEfWk5xzjoQhy9kMo5LmPgvfjyHbNwOWQazqR ondpguOPtnFC995wuK9MFWHptHuWQqs3LTfGeUNfOsiQCtnCjvsVnn7ykc0Qct/3tjQe hFBe4gLTaA/IQQmYASt5XAgrEanNxYGLu1DyWzRhoceV96cnCeR0AGGLEqv+Gay47AYQ rSsOlH4npGgNauQ5EVmO1DDX37zVDd0aHZ7bYvmFAKKYC5NECdurO41DusIh9dKMtpuP iQlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=ZjOuLxW8; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id b189si5357924qke.470.2018.03.18.20.16.16 for (version=TLS1 cipher=AES128-SHA bits=128/128); Sun, 18 Mar 2018 20:16:16 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=ZjOuLxW8; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:39999 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1exlHU-00064N-Ec for patch@linaro.org; Sun, 18 Mar 2018 23:16:16 -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) 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-Received: by 2002:a17:902:167:: with SMTP id 94-v6mr10922467plb.294.1521429352623; Sun, 18 Mar 2018 20:15:52 -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) 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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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 --- 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(-) -- 2.14.3 Tested-by: Pavel Dovgalyuk Reviewed-by: Philippe Mathieu-Daudé 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 retaddr) CPUArchState *env = cpu->env_ptr; #endif TranslationBlock *tb; - uint32_t n, flags; - target_ulong pc, cs_base; + uint32_t n; tb_lock(); tb = tb_find_pc(retaddr); @@ -1737,44 +1736,33 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr) cpu_abort(cpu, "cpu_io_recompile: could not find TB for pc=%p", (void *)retaddr); } - n = 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 = 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 = 1; #if defined(TARGET_MIPS) - if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) { + if ((env->hflags & MIPS_HFLAG_BMASK) != 0 + && env->active_tc.PC != tb->pc) { env->active_tc.PC -= (env->hflags & MIPS_HFLAG_B16 ? 2 : 4); cpu->icount_decr.u16.low++; env->hflags &= ~MIPS_HFLAG_BMASK; + n = 2; } #elif defined(TARGET_SH4) if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0 - && n > 1) { + && env->pc != tb->pc) { env->pc -= 2; cpu->icount_decr.u16.low++; env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); + n = 2; } #endif - /* This should never happen. */ - if (n > CF_COUNT_MASK) { - cpu_abort(cpu, "TB too big during recompile"); - } - pc = tb->pc; - cs_base = tb->cs_base; - flags = tb->flags; - tb_phys_invalidate(tb, -1); - - /* Execute one IO instruction without caching - instead of creating large TB. */ - cpu->cflags_next_tb = curr_cflags() | CF_LAST_IO | CF_NOCACHE | 1; + /* Generate a new TB executing the I/O insn. */ + cpu->cflags_next_tb = curr_cflags() | CF_LAST_IO | n; if (tb->cflags & CF_NOCACHE) { if (tb->orig_tb) { @@ -1785,11 +1773,6 @@ void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr) tb_remove(tb); } - /* 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 != 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.