From patchwork Fri Nov 8 13:15:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiang W X-Patchwork-Id: 2008476 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=43UTxV5B; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=h9gWqVSP; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XlLc13PJWz1xyS for ; Sat, 9 Nov 2024 01:17:03 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=VenpHGlSq5veFodLPMz4b9ph3S/DZJ4aYm2LL/S5br4=; b=43UTxV5Bzyqjfb HO0SwXSzK4EQKeHwrSl4GhHt9ks4pAtb6gLHujPpENKjNeHwj0MeEIGN+YHm4xkAjDXT4AK3AvH1W w1P4QVKvxTCQ9Y1s6hVYzyppQApqfKMqz3Mw/EDlVLJssO6lXcYiFbaP4RugGy+ORE5lHHSn7UrmZ xzGMazjCl8IXkvFOLf2UjXLbkWMYU/FMyUi8aummAiU89g39vNzvd9an6lL8CLisyRAjHMtrKowoo gqdcKbuxiqxvwXEYFVzofG+rDhbIfIKg4norBxfrYGRV1CXwfHWHc9OK7cpS6PWSiRa2jbWHQXV6s AUxNkQS4eJCgz2S1aJTw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t9PnD-0000000Aov4-1JhW; Fri, 08 Nov 2024 14:16:55 +0000 Received: from m16.mail.126.com ([117.135.210.6]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t9Opt-0000000Ae29-27X7 for opensbi@lists.infradead.org; Fri, 08 Nov 2024 13:15:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version; bh=uQLW5 2LqJz/C90eCPyxM82FTM0k/W6fFTqKlISGWPdk=; b=h9gWqVSPdCvNXIVzAtdcf I3FmVA14mhGKLUSD8n5LyIeGPNXPjqjFhvmj211/v+JjRsAj5ZsmeDJL9f8ra5IT bZlP8SGgPZGRe+gqtXHrLva2BUJHgVLUQJvq0FoQFDNyz7Y+Md0qudW7JV54cpaD 1YC+TWm+FpG+9GMTTI1h60= Received: from T490.lan (unknown [153.35.241.212]) by gzga-smtp-mtada-g0-3 (Coremail) with SMTP id _____wD3n43uDi5nySfRBg--.63683S3; Fri, 08 Nov 2024 21:15:27 +0800 (CST) From: Xiang W To: opensbi@lists.infradead.org Cc: Xiang W , anup@brainfault.org Subject: [RFC v2 PATCH 1/2] lib: sbi: Improve pmp_get/pmp_set Date: Fri, 8 Nov 2024 21:15:15 +0800 Message-ID: <20241108131523.676899-2-wxjstz@126.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241108131523.676899-1-wxjstz@126.com> References: <20241108131523.676899-1-wxjstz@126.com> MIME-Version: 1.0 X-CM-TRANSID: _____wD3n43uDi5nySfRBg--.63683S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxGrWkCryrur1ftFykXF4DXFb_yoWrJw1rpF yxGayfAr40qF1kZa4ayr48Xryrtayvy3ZYvF42kr1F93W5Z34rZa45W3savr47WFykAF1U Zw48Zr409FnIyFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0piG-erUUUUU= X-Originating-IP: [153.35.241.212] X-CM-SenderInfo: pz0m23b26rjloofrz/1tbiFxyROmct9-SVbQAAsR X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241108_051538_325504_6C5C5BE9 X-CRM114-Status: GOOD ( 11.73 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: The previous code used byte addresses, which limited the address range to 0-2^XLEN. Changing to word addresses allows access to all address ranges. Signed-off-by: Xiang W --- lib/sbi/riscv_asm.c | 42 +++++++++++++++++++ lib/sbi/sbi_hart.c | 9 ++++++--- 2 files changed, 25 insertions(+), 26 deletions(-) Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [117.135.210.6 listed in list.dnswl.org] 0.0 RCVD_IN_VALIDITY_SAFE_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [117.135.210.6 listed in sa-accredit.habeas.com] 0.0 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [117.135.210.6 listed in sa-trusted.bondedsender.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 RCVD_IN_VALIDITY_RPBL_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [117.135.210.6 listed in bl.score.senderscore.com] -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [wxjstz(at)126.com] X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The previous code used byte addresses, which limited the address range to 0-2^XLEN. Changing to word addresses allows access to all address ranges. Signed-off-by: Xiang W --- lib/sbi/riscv_asm.c | 42 +++++++++++++++++++----------------------- lib/sbi/sbi_hart.c | 9 ++++++--- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c index c7d75ac..6c9780a 100644 --- a/lib/sbi/riscv_asm.c +++ b/lib/sbi/riscv_asm.c @@ -309,7 +309,7 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr, unsigned long addrmask, pmpaddr; /* check parameters */ - if (n >= PMP_COUNT || log2len > __riscv_xlen || log2len < PMP_SHIFT) + if (n >= PMP_COUNT || log2len > __riscv_xlen + 1) return SBI_EINVAL; /* calculate PMP register and offset */ @@ -326,22 +326,22 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr, /* encode PMP config */ prot &= ~PMP_A; - prot |= (log2len == PMP_SHIFT) ? PMP_A_NA4 : PMP_A_NAPOT; + prot |= (log2len == 0) ? PMP_A_NA4 : PMP_A_NAPOT; cfgmask = ~(0xffUL << pmpcfg_shift); pmpcfg = (csr_read_num(pmpcfg_csr) & cfgmask); pmpcfg |= ((prot << pmpcfg_shift) & ~cfgmask); /* encode PMP address */ - if (log2len == PMP_SHIFT) { - pmpaddr = (addr >> PMP_SHIFT); + if (log2len == 0) { + pmpaddr = addr; + } else if (log2len == __riscv_xlen) { + pmpaddr = -1UL >> 1; + } else if (log2len == __riscv_xlen + 1) { + pmpaddr = -1UL; } else { - if (log2len == __riscv_xlen) { - pmpaddr = -1UL; - } else { - addrmask = (1UL << (log2len - PMP_SHIFT)) - 1; - pmpaddr = ((addr >> PMP_SHIFT) & ~addrmask); - pmpaddr |= (addrmask >> 1); - } + addrmask = (1UL << log2len) - 1; + pmpaddr = addr & ~addrmask; + pmpaddr |= addrmask >> 1; } /* write csrs */ @@ -381,19 +381,15 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out, prot = pmpcfg >> pmpcfg_shift; /* decode PMP address */ + addr = csr_read_num(pmpaddr_csr); + len = 0; if ((prot & PMP_A) == PMP_A_NAPOT) { - addr = csr_read_num(pmpaddr_csr); - if (addr == -1UL) { - addr = 0; - len = __riscv_xlen; - } else { - t1 = ctz(~addr); - addr = (addr & ~((1UL << t1) - 1)) << PMP_SHIFT; - len = (t1 + PMP_SHIFT + 1); - } - } else { - addr = csr_read_num(pmpaddr_csr) << PMP_SHIFT; - len = PMP_SHIFT; + t1 = ctz(~addr); + len = t1 + 1; + if (len >= __riscv_xlen) + addr = 0; + else + addr = addr & ~((1UL << t1) - 1); } /* return details */ diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index 0451fcb..832db7a 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -361,7 +361,8 @@ static void sbi_hart_smepmp_set(struct sbi_scratch *scratch, unsigned long pmp_addr = reg->base >> PMP_SHIFT; if (pmp_log2gran <= reg->order && pmp_addr < pmp_addr_max) { - pmp_set(pmp_idx, pmp_flags, reg->base, reg->order); + pmp_set(pmp_idx, pmp_flags, reg->base >> PMP_SHIFT, + reg->order - PMP_SHIFT); } else { sbi_printf("Can not configure pmp for domain %s because" " memory region address 0x%lx or size 0x%lx " @@ -478,7 +479,8 @@ static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch, pmp_addr = reg->base >> PMP_SHIFT; if (pmp_log2gran <= reg->order && pmp_addr < pmp_addr_max) { - pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order); + pmp_set(pmp_idx++, pmp_flags, reg->base >> PMP_SHIFT, + reg->order - PMP_SHIFT); } else { sbi_printf("Can not configure pmp for domain %s because" " memory region address 0x%lx or size 0x%lx " @@ -518,7 +520,8 @@ int sbi_hart_map_saddr(unsigned long addr, unsigned long size) } } - pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order); + pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base >> PMP_SHIFT, + order - PMP_SHIFT); return SBI_OK; }