From patchwork Tue May 16 19:40:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 682408 Delivered-To: patch@linaro.org Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp20532wrt; Tue, 16 May 2023 12:57:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5FIWsIm2qV66C0nxza7Tk6FoiSNW8nAkvAeb33QdI0/hdnjsIxDNjhUecNJxykxOf3UJBm X-Received: by 2002:a5d:448b:0:b0:307:9194:9a94 with SMTP id j11-20020a5d448b000000b0030791949a94mr23968126wrq.17.1684267050385; Tue, 16 May 2023 12:57:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684267050; cv=none; d=google.com; s=arc-20160816; b=V78To7bkVEKOWfgy6s/QbMCxwh83jzFaR0OlvlID8/vcExKD+lviDD2OXcmBcpRGcX /GMA/8vgjkdn2rmH9H/m5O3O/7ls2M1sG/Q9GIhn+YSZuq/SJl6xhH/IgBp0HUaT8CZe 3M6tYA0b8xvAgwJpMBS1sH+Y7vSMUCupTDj0HpEApzcDiuVlvi3Rq6fsEX0LyhbZXkWU D6MbyyypXUOzxN8cIHI516C/xBTrs2fiddU/tyI3Fhts6EpyxcPUVIELF6Rvk+1qwFE+ q1ybxZR+pg4MZh7nNxyQZOms6AaYYWLLGrzJwiQE/O9BlvSwcWtrAh65Gng2ruRsJNPJ 6hlw== 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=RcueoQ7mogTzwxRR1biig20yNfysNgxqkYSawavzYAo=; b=RNLPOilfQ0e6ZninSdA7sB8CX7CnguSP8mbUR9B7dTxMbyn+lE8wOqB9Aej4iuUu0a YyzWf2782UUwJtImYRTDju1mq1dY13g26xcJkl7Bzmz7v90Tox8BqvbHm8C1fsuiM/cb K9YWuEal249qcK0IrmXPgRn0LnJTp00mYls+48XESZyhJqK/UYDHX4V4WK1s+D635c37 BnIDgXU0V/5cs2iz4Eyzy9R8Hx/j6JeHEROgj+SL1f7i6MyX60VX9R0MMyVOTwQ1zZiq y/4/cEl3k1cfoyoz0K5RCKPFh59K9R56b4hYtBkY3lqJvbn5SVGYRTW8uQ1Njk7C//Zt WuRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=b8dcxUmm; 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 c15-20020adfef4f000000b0030639d3dedcsi166394wrp.547.2023.05.16.12.57.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 May 2023 12:57:30 -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=b8dcxUmm; 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 1pz0Ys-0006mH-R9; Tue, 16 May 2023 15:42:18 -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 1pz0Yr-0006lc-RR for qemu-devel@nongnu.org; Tue, 16 May 2023 15:42:17 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pz0Yq-0002tC-5g for qemu-devel@nongnu.org; Tue, 16 May 2023 15:42:17 -0400 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-52c30fa5271so7877690a12.0 for ; Tue, 16 May 2023 12:42:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684266135; x=1686858135; 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=RcueoQ7mogTzwxRR1biig20yNfysNgxqkYSawavzYAo=; b=b8dcxUmm07jotRHjdt55bHkHvuKu/bc3QYVHPyPcmNoHwsBv1iCr86YxNUYcWN1vrJ aBC1E61xrU0Cm9UuYP4nq8UqmkF14MpaIZdIcVJqTdkEDMjzATDOd4m8BZTt463X+t3w pBMKYONPeKPHQ6aSChd5nXVhLBoqcR26FlY9k20+c1Yo49hqn1OdsT6hO/K8JJGZMJQ6 deLYzUTST22956R8j/NYs1Urg9wCOL3q4RLxCyWpmefjs62GP/DBYL0I2VdOyNprV7fj UVQcc7V78IbEAOkECb9Jb2A+CH+peTcV0nWejn74TD6lQ/aIRhYHsypoNU9ooApRQF2X uAWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684266135; x=1686858135; 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=RcueoQ7mogTzwxRR1biig20yNfysNgxqkYSawavzYAo=; b=DIH4FrcfK7RzNlPO+A5p1v2Uwbtjten5fKKrBGv7v9XQobIf3Xsp+KfbeWvrefj3JC CN53FH/e4B/OBpL0PqjRe+DvLPVjn6Trdvfr94j7WFtxgou3jqVIdxDUxp/oFgnKoJvA g/qezODA2NaYZD/E0jZ9HSh/t/wf+TuN3vr4OYAelMcSOFqQGCgpVTwFmZ0l4Fu2Ey2W CpQeCX8gacq8OmhwIMWGw2ge45DfTAjZMaMUDk1r1wyKyQsT+yG1zeqJ1TQBebwzWBsu bHsH6vnkAtSHCNfXh9AgsFapzNoAqrnIW1Wf0NstcwnW2/4cRUB8QLwyfNnQQEMSN2hW 1x2g== X-Gm-Message-State: AC+VfDzd9DjYsL0TAgepOOe82uztOfNNDX/pFkAW/iqQ0uld7KgO3Wal xyYbiOTNnQ2eCu3UlFLJ+S/kSI7vXaMLGGcoJB4= X-Received: by 2002:a05:6a20:734a:b0:107:10b6:4c8a with SMTP id v10-20020a056a20734a00b0010710b64c8amr3200208pzc.24.1684266134824; Tue, 16 May 2023 12:42:14 -0700 (PDT) Received: from stoup.. ([2602:ae:1598:4c01:ec81:440e:33a4:40b9]) by smtp.gmail.com with ESMTPSA id z21-20020aa791d5000000b006260526cf0csm13771165pfa.116.2023.05.16.12.42.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 12:42:14 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: LIU Zhiwei Subject: [PULL 31/80] tcg/riscv: Support softmmu unaligned accesses Date: Tue, 16 May 2023 12:40:56 -0700 Message-Id: <20230516194145.1749305-32-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230516194145.1749305-1-richard.henderson@linaro.org> References: <20230516194145.1749305-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::533; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x533.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, T_SCC_BODY_TEXT_LINE=-0.01 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 The system is required to emulate unaligned accesses, even if the hardware does not support it. The resulting trap may or may not be more efficient than the qemu slow path. There are linux kernel patches in flight to allow userspace to query hardware support; we can re-evaluate whether to enable this by default after that. In the meantime, softmmu now matches useronly, where we already assumed that unaligned accesses are supported. Reviewed-by: LIU Zhiwei Signed-off-by: Richard Henderson --- tcg/riscv/tcg-target.c.inc | 48 ++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/tcg/riscv/tcg-target.c.inc b/tcg/riscv/tcg-target.c.inc index 19cd4507fb..415e6c6e15 100644 --- a/tcg/riscv/tcg-target.c.inc +++ b/tcg/riscv/tcg-target.c.inc @@ -910,12 +910,13 @@ static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, TCGReg *pbase, #ifdef CONFIG_SOFTMMU unsigned s_bits = opc & MO_SIZE; + unsigned s_mask = (1u << s_bits) - 1; int mem_index = get_mmuidx(oi); int fast_ofs = TLB_MASK_TABLE_OFS(mem_index); int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask); int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table); - TCGReg mask_base = TCG_AREG0, table_base = TCG_AREG0; - tcg_target_long compare_mask; + int compare_mask; + TCGReg addr_adj; ldst = new_ldst_label(s); ldst->is_ld = is_ld; @@ -924,14 +925,33 @@ static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, TCGReg *pbase, QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0); QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11)); - tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, mask_base, mask_ofs); - tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, table_base, table_ofs); + tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_AREG0, mask_ofs); + tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, TCG_AREG0, table_ofs); tcg_out_opc_imm(s, OPC_SRLI, TCG_REG_TMP2, addr_reg, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS); tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0); tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1); + /* + * For aligned accesses, we check the first byte and include the alignment + * bits within the address. For unaligned access, we check that we don't + * cross pages using the address of the last byte of the access. + */ + addr_adj = addr_reg; + if (a_bits < s_bits) { + addr_adj = TCG_REG_TMP0; + tcg_out_opc_imm(s, TARGET_LONG_BITS == 32 ? OPC_ADDIW : OPC_ADDI, + addr_adj, addr_reg, s_mask - a_mask); + } + compare_mask = TARGET_PAGE_MASK | a_mask; + if (compare_mask == sextreg(compare_mask, 0, 12)) { + tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addr_adj, compare_mask); + } else { + tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask); + tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addr_adj); + } + /* Load the tlb comparator and the addend. */ tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2, is_ld ? offsetof(CPUTLBEntry, addr_read) @@ -939,29 +959,17 @@ static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, TCGReg *pbase, tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2, offsetof(CPUTLBEntry, addend)); - /* We don't support unaligned accesses. */ - if (a_bits < s_bits) { - a_bits = s_bits; - } - /* Clear the non-page, non-alignment bits from the address. */ - compare_mask = (tcg_target_long)TARGET_PAGE_MASK | a_mask; - if (compare_mask == sextreg(compare_mask, 0, 12)) { - tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addr_reg, compare_mask); - } else { - tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask); - tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addr_reg); - } - /* Compare masked address with the TLB entry. */ ldst->label_ptr[0] = s->code_ptr; tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP0, TCG_REG_TMP1, 0); /* TLB Hit - translate address using addend. */ + addr_adj = addr_reg; if (TARGET_LONG_BITS == 32) { - tcg_out_ext32u(s, TCG_REG_TMP0, addr_reg); - addr_reg = TCG_REG_TMP0; + addr_adj = TCG_REG_TMP0; + tcg_out_ext32u(s, addr_adj, addr_reg); } - tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP2, addr_reg); + tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP2, addr_adj); *pbase = TCG_REG_TMP0; #else if (a_mask) {