From patchwork Mon Apr 8 17:49:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 786948 Delivered-To: patch@linaro.org Received: by 2002:adf:fb4d:0:b0:346:15ad:a2a with SMTP id c13csp1846wrs; Mon, 8 Apr 2024 10:55:45 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUZIkPiCL2iliGFCKj4PTgHZU8HKP9T2hsTbxAxrbREme3ERuB2kLbvfkthpA6O/I+1z4pKPXz9/29rqXWdh6FI X-Google-Smtp-Source: AGHT+IFwDwnYSenVG3ERRcSLn5Nr46ZYGxvMHMIPdjfGUaYEtFl8Jb4ZUB8yswyEHzL+BhdUVe7T X-Received: by 2002:a05:6214:2244:b0:696:7d37:1a6b with SMTP id c4-20020a056214224400b006967d371a6bmr13429433qvc.56.1712598945532; Mon, 08 Apr 2024 10:55:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712598945; cv=none; d=google.com; s=arc-20160816; b=BoIWXWWoFbUKGDqrbOv3mOSkR35ZaQvBGcz3HFEYYrtzOQK1eWUYWsRXhEhNzRJ4fB lIeTx80eGftRVObc5UVXe06Vqotfu2bJsJeEL8nbP3mMvRyiIGLZwpOxqaXGQOuYW5qf h+yxeRxSh8ZDGS6DhS45/LhxO2WRHHZluJSFLzRC3fBZLXK96ylTrLXpfmfrgsUbZkED m98I1zXcTX1JySbs+dhV8Yhit3QZVzY8QM/xok8C/pHYcvUBi6I8HPBg6xqgfesHb9Y6 Df3WpLoNmKCdloWYlzxVBQT5YwIoN4AQCkMSrpg0JxbnD8tn7rEuSTiCtcEJqZ1oyhDX bsMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LWeWfrDPztEwkV9YuE7zu+DtIohBh0oWfxn6udjmQ4s=; fh=feciRXMMXjL5Zdx/9FYuVSJ4fTkSvjANJ3wyTAeS3QU=; b=Zvb+lAk8jsZqV8z4N+8MGZy6q0HqaRRrzwdH5WrX4T9qdXpTo6NXNrXymgPDmv/w/6 qmKhYlLkqvz1pD5LFQd5Q6CsNOWhjNFYg66qeWIY15HhZw0Lruomtwec3b/YXFE3M8BI DTeFZoEB+M3yngSrB5pU80jb0PT0eP2JKiiILVa3PxeygujI5hBi4oEclek5lXj6STxf zcybb6SsD+HzkCvJHFgrriAGqBoqo1V/yyidthONd2qr5dqEXpIkKlXMTMPHSSs9AAGF zJp0hNxF07D9DIyFooanCabsFOTcG5IRYtb7+Xpb5B9clWRQF8gWDVir3sBToviuXPQa +4aw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XgqngNY7; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id mh3-20020a056214564300b0069929cfdbddsi8562793qvb.449.2024.04.08.10.55.45 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Apr 2024 10:55:45 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XgqngNY7; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtt8N-00027N-1x; Mon, 08 Apr 2024 13:50:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtt8K-00026s-TE for qemu-devel@nongnu.org; Mon, 08 Apr 2024 13:50:16 -0400 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rtt8J-0003jm-1b for qemu-devel@nongnu.org; Mon, 08 Apr 2024 13:50:16 -0400 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-5d4d15ec7c5so3811216a12.1 for ; Mon, 08 Apr 2024 10:50:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712598612; x=1713203412; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LWeWfrDPztEwkV9YuE7zu+DtIohBh0oWfxn6udjmQ4s=; b=XgqngNY7YmqvH/feZBb2y0Pp4VesJsceCYF1LyLUleUK9m7MvWu5/qGaUrhvc8zSb4 VwsVtMd21dvdUQ8YnfZG/z+Ivi/uqc8DciYHFxygkHide5fdbkRFbm1vDaT04XB9sHz2 F53Ac1ckiysBIXonSUcs1YvJ7LwoM7RGpAzoKIOnRaycsajV7PCIb2vnDs39tF7CWK1o IehPl2S+T+OkqPCXdIJBetDD+Z7k/sHOUnbp01JjLhos4hL+p4k2uRJOfmhAeAYed1+m epnDO2z+FI0GlBeAct9HFS9d7Z0UTB+UKDjCG2l7UanuGagdfPjc/Xx8ZBgMyLmYVSfw v6bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712598612; x=1713203412; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LWeWfrDPztEwkV9YuE7zu+DtIohBh0oWfxn6udjmQ4s=; b=uCsAOScy6qglBj+b54fqHAoeV90FP+tNEXX5aulCZoJbLRfCA0JCcDwmlFmrF+x4iI 0xaG+Oe3SlQ0Sl6xgXw8tgTBLZqZnmGxOogQSvrrVYkEj9koyaGP/HPpc8zP4F17499i 38jKwbYSH97GcXEmIQ1wQOKIRhLAKg8qIe6QKdNYo4sRRQ7YylG+bUSD8CiS+J9rDKgS hg/bdf0igi8cCTkWwjK6hvsBnaaysVrJb1rgW8cJmPVZsAqWv4Whqoz27mS2QWL/7XI+ m7vGe+SLb71X88UMi1dPq48o93XyWKdwh9JmQ9Cv1yzrgdhfTda/Ynq/mh3i6CksR1+U Sw4g== X-Gm-Message-State: AOJu0YyC2Vr9Pl7AmVcBgvDJW+G2GZizR68X4rSSAZqER8Nh8fjrfFZV WabMCsmPCbQxZTtwc1Lyr7O2Uf6Ze5u0Y2SM06wj11KFhURzVlyO1myjMS+n/vQcA02dn5hFlAb K X-Received: by 2002:a17:90b:24a:b0:2a0:8eed:56ec with SMTP id fz10-20020a17090b024a00b002a08eed56ecmr8432988pjb.39.1712598612163; Mon, 08 Apr 2024 10:50:12 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id ga15-20020a17090b038f00b0029c3bac0aa8sm8658432pjb.4.2024.04.08.10.50.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 10:50:11 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?J=C3=B8rgen_Hansen?= , =?utf-8?q?Philip?= =?utf-8?q?pe_Mathieu-Daud=C3=A9?= Subject: [PULL 27/35] accel/tcg: Improve can_do_io management Date: Mon, 8 Apr 2024 07:49:21 -1000 Message-Id: <20240408174929.862917-28-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408174929.862917-1-richard.henderson@linaro.org> References: <20240408174929.862917-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::529; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x529.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We already attempted to set and clear can_do_io before the first and last insns, but only used the initial value of max_insns and the call to translator_io_start to find those insns. Now that we track insn_start in DisasContextBase, and now that we have emit_before_op, we can wait until we have finished translation to identify the true first and last insns and emit the sets of can_do_io at that time. This fixes the case of a translation block which crossed a page boundary, and for which the second page turned out to be mmio. In this case we truncate the block, and the previous logic for can_do_io could leave a block with a single insn with can_do_io set to false, which would fail an assertion in cpu_io_recompile. Reported-by: Jørgen Hansen Reviewed-by: Philippe Mathieu-Daudé Tested-by: Jørgen Hansen Signed-off-by: Richard Henderson --- include/exec/translator.h | 1 - accel/tcg/translator.c | 45 ++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/include/exec/translator.h b/include/exec/translator.h index ceaeca8c91..2c4fb818e7 100644 --- a/include/exec/translator.h +++ b/include/exec/translator.h @@ -87,7 +87,6 @@ typedef struct DisasContextBase { int num_insns; int max_insns; bool singlestep_enabled; - int8_t saved_can_do_io; bool plugin_enabled; struct TCGOp *insn_start; void *host_addr[2]; diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c index ae61c154c2..9de0bc34c8 100644 --- a/accel/tcg/translator.c +++ b/accel/tcg/translator.c @@ -18,20 +18,14 @@ static void set_can_do_io(DisasContextBase *db, bool val) { - if (db->saved_can_do_io != val) { - db->saved_can_do_io = val; - - QEMU_BUILD_BUG_ON(sizeof_field(CPUState, neg.can_do_io) != 1); - tcg_gen_st8_i32(tcg_constant_i32(val), tcg_env, - offsetof(ArchCPU, parent_obj.neg.can_do_io) - - offsetof(ArchCPU, env)); - } + QEMU_BUILD_BUG_ON(sizeof_field(CPUState, neg.can_do_io) != 1); + tcg_gen_st8_i32(tcg_constant_i32(val), tcg_env, + offsetof(ArchCPU, parent_obj.neg.can_do_io) - + offsetof(ArchCPU, env)); } bool translator_io_start(DisasContextBase *db) { - set_can_do_io(db, true); - /* * Ensure that this instruction will be the last in the TB. * The target may override this to something more forceful. @@ -84,13 +78,6 @@ static TCGOp *gen_tb_start(DisasContextBase *db, uint32_t cflags) - offsetof(ArchCPU, env)); } - /* - * cpu->neg.can_do_io is set automatically here at the beginning of - * each translation block. The cost is minimal, plus it would be - * very easy to forget doing it in the translator. - */ - set_can_do_io(db, db->max_insns == 1); - return icount_start_insn; } @@ -129,6 +116,7 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, { uint32_t cflags = tb_cflags(tb); TCGOp *icount_start_insn; + TCGOp *first_insn_start = NULL; bool plugin_enabled; /* Initialize DisasContext */ @@ -139,7 +127,6 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, db->num_insns = 0; db->max_insns = *max_insns; db->singlestep_enabled = cflags & CF_SINGLE_STEP; - db->saved_can_do_io = -1; db->insn_start = NULL; db->host_addr[0] = host_pc; db->host_addr[1] = NULL; @@ -159,6 +146,9 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, *max_insns = ++db->num_insns; ops->insn_start(db, cpu); db->insn_start = tcg_last_op(); + if (first_insn_start == NULL) { + first_insn_start = db->insn_start; + } tcg_debug_assert(db->is_jmp == DISAS_NEXT); /* no early exit */ if (plugin_enabled) { @@ -171,10 +161,6 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, * done next -- either exiting this loop or locate the start of * the next instruction. */ - if (db->num_insns == db->max_insns) { - /* Accept I/O on the last instruction. */ - set_can_do_io(db, true); - } ops->translate_insn(db, cpu); /* @@ -207,6 +193,21 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, ops->tb_stop(db, cpu); gen_tb_end(tb, cflags, icount_start_insn, db->num_insns); + /* + * Manage can_do_io for the translation block: set to false before + * the first insn and set to true before the last insn. + */ + if (db->num_insns == 1) { + tcg_debug_assert(first_insn_start == db->insn_start); + } else { + tcg_debug_assert(first_insn_start != db->insn_start); + tcg_ctx->emit_before_op = first_insn_start; + set_can_do_io(db, false); + } + tcg_ctx->emit_before_op = db->insn_start; + set_can_do_io(db, true); + tcg_ctx->emit_before_op = NULL; + if (plugin_enabled) { plugin_gen_tb_end(cpu, db->num_insns); }