From patchwork Mon May 15 14:32:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 681961 Delivered-To: patch@linaro.org Received: by 2002:adf:fd8f:0:0:0:0:0 with SMTP id d15csp1152162wrr; Mon, 15 May 2023 07:43:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5+GeoGgp8aPno7J0Y78eHJwRLyJRi/dhh6QMM9JY0rqIvpXaD7YDgMLZVxzBf28GQi7d45 X-Received: by 2002:adf:e909:0:b0:306:2db9:cc2c with SMTP id f9-20020adfe909000000b003062db9cc2cmr27032211wrm.32.1684161792902; Mon, 15 May 2023 07:43:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684161792; cv=none; d=google.com; s=arc-20160816; b=BX8PajZRS6uNIWwMrMf2vJ8Kg/pNWHgBRXsJejrqNnVyF1xzNEQqfNKmKILWk8RvgA I9R8F91pAPlRp8pkoq6wd7/9N80ft11hsoJQdDCv8fu1RDZCIFMbFLB9xwayO91f5GQQ ZKCyXeIDGvAM1SKiIe1kFK4ydHqYbfRkd9PIP2P0gV+DlOOfl/swdo0RywWw7hjVABEp MBowgE1zbX0/0auATJnH84fPCAaVnHpZmiItKzvUqNse39FjQz+ywFr1zlQptNKklKGi SWmcbpCZnRcU/7qqXPeyvHMLOVQ63IqWZd+Tlh3E1RpPyxZIUduYGREvWqKpEHuUt+6R ldQQ== 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=lyCBhaspxp1L6j79z6PtNM9NIFE9Typ451X6vQTwT1u9k+qbl5yeiLxRC7tq/h2ZOf UegtG5H0pXMwKwy1exYFTeUYfbHD4Mq5IkzjRF2PgyEQD0G1KnKKW9Bhk+EoqDn0Arky Y1d0/h8UdE/o/X/cw/pAuV9NWyFD5jBxzaREIzXF+Yp58Mo9ce+LMHnyL072uEX6jqEp Bf81m4048i69QyIaRkNgxN3sK3NkcGZu/0XtqkxhFrEDQtBZK+C00f5m5yftwTRUs3fC OwkL+xBP3WvyPMTovSmasAZabCDkUbZ3lTsVSmoydj8I9eEJ5BaWr/wUpFejZa+a2RpR oOuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZZ9BTpD6; 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 u11-20020adfdd4b000000b003062aa1a9ccsi11501wrm.41.2023.05.15.07.43.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 May 2023 07:43:12 -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=ZZ9BTpD6; 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 1pyZJS-0003KL-KS; Mon, 15 May 2023 10:36:34 -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 1pyZHh-00079p-54 for qemu-devel@nongnu.org; Mon, 15 May 2023 10:34:47 -0400 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pyZHa-0004sJ-3L for qemu-devel@nongnu.org; Mon, 15 May 2023 10:34:43 -0400 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1ab05018381so118665145ad.2 for ; Mon, 15 May 2023 07:33:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684161223; x=1686753223; 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=ZZ9BTpD69lKmUJLOZGgT+fUwwlkRO7Q8KlzqvE7E66V5RcJsnMgpex4J0b+xF37yex N1OmRfIPGD5PxmtgNAX718e/cUG4a8DOm5jWetYRPuO6PutWEPLAgiGTitSAN6J5ICxE HwCU6kb3yJ3CuNWr9qc/RK/qebrPOZKzUjWl752dapPMHoiE3Bv5NbJN8HBNF6unepbZ 2kvOOg30yKhONpODMPKGapqe8dMJSY22UgsuVRMhGt/RVq6qwfIpS+fuQbm0YUMReQ5y o0OYJ04jD8M6tPK7r47iRhR7YHzI44C89Zw8APREQ9GP/gM97qdPGQOQv9bw/PEiWHwZ 5nJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684161223; x=1686753223; 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=Hy7gGjPI+CFC4tXXI5bfI2WQPlYmvS5YLQXfu/giIkIq1xqOPVvi11Or2p2Iuf0YYE RNzxfCvpJkQsvQIu1kfu81svZeHzrt+vPidGxKM10RJanFtT/I1ysKDfK6HxQO3mTDUk Oel2w9KcV5A1SJkJAug3r/CynnCw10y2OC1n1Oy3MvhlaLO8hphTlnrjFF3XH13WZWE0 iHe9vVAQxnUYx208Ku52jRVTnSTAE/AnCpbannDTKiabJp0A/LOumxlycLntGg5hot6/ zWtkcSJmsGaiT8W5rathaaOjhH//uZGsbOtDujEK2wrmhzMosG3/hX1Do1sZcKeuHNx6 /xJw== X-Gm-Message-State: AC+VfDziBVrZpPSImEcPSYT0fMl2IWPraDLVUSHzQMW5HcjkuQK/j20K asUQdAwreXdg+iFmkCMg2Isq2N9YoP85SUco7sg= X-Received: by 2002:a17:903:244d:b0:1a5:2540:729 with SMTP id l13-20020a170903244d00b001a525400729mr44025420pls.56.1684161223150; Mon, 15 May 2023 07:33:43 -0700 (PDT) Received: from stoup.. ([2602:ae:1598:4c01:9902:96ac:8d8c:4366]) by smtp.gmail.com with ESMTPSA id ik9-20020a170902ab0900b001ab0aec388bsm13694335plb.135.2023.05.15.07.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 07:33:42 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, qemu-s390x@nongnu.org, LIU Zhiwei Subject: [PATCH v5 33/54] tcg/riscv: Support softmmu unaligned accesses Date: Mon, 15 May 2023 07:32:52 -0700 Message-Id: <20230515143313.734053-34-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230515143313.734053-1-richard.henderson@linaro.org> References: <20230515143313.734053-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62a; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62a.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) {