public inbox for opensbi@lists.infradead.org
 help / color / mirror / Atom feed
From: Yu-Chien Peter Lin <peter.lin@sifive.com>
To: opensbi@lists.infradead.org
Cc: zong.li@sifive.com, greentime.hu@sifive.com, wxjstz@126.com,
	alvinga@andestech.com, anup@brainfault.org,
	Yu-Chien Peter Lin <peter.lin@sifive.com>
Subject: [PATCH v2 4/8] lib: sbi_hart: return error when insufficient PMP entries available
Date: Wed,  8 Oct 2025 16:44:40 +0800	[thread overview]
Message-ID: <20251008084444.3525615-5-peter.lin@sifive.com> (raw)
In-Reply-To: <20251008084444.3525615-1-peter.lin@sifive.com>

Previously, when memory regions exceed available PMP entries,
some regions were silently ignored. If the last entry that covers
the full 64-bit address space is not added to a domain, the next
stage S-mode software won't have permission to access and fetch
instructions from its memory. So return early with error message
to catch such situation.

Signed-off-by: Yu-Chien Peter Lin <peter.lin@sifive.com>
---
 lib/sbi/sbi_hart.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c
index d018619b..032f7dc1 100644
--- a/lib/sbi/sbi_hart.c
+++ b/lib/sbi/sbi_hart.c
@@ -324,6 +324,16 @@ static void sbi_hart_smepmp_set(struct sbi_scratch *scratch,
 	}
 }
 
+static bool is_valid_pmp_idx(unsigned int pmp_count, unsigned int pmp_idx)
+{
+	if (pmp_count > pmp_idx)
+		return true;
+
+	sbi_printf("ERR: insufficient PMP entries\n");
+
+	return false;
+}
+
 static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 				     unsigned int pmp_count,
 				     unsigned int pmp_log2gran,
@@ -348,8 +358,8 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 		/* Skip reserved entry */
 		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
 			pmp_idx++;
-		if (pmp_count <= pmp_idx)
-			break;
+		if (!is_valid_pmp_idx(pmp_count, pmp_idx))
+			return SBI_EFAIL;
 
 		/* Skip shared and SU-only regions */
 		if (!SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
@@ -372,8 +382,8 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 		/* Skip reserved entry */
 		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
 			pmp_idx++;
-		if (pmp_count <= pmp_idx)
-			break;
+		if (!is_valid_pmp_idx(pmp_count, pmp_idx))
+			return SBI_EFAIL;
 
 		/* Skip M-only regions */
 		if (SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
@@ -407,8 +417,8 @@ static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch,
 	unsigned long pmp_addr;
 
 	sbi_domain_for_each_memregion(dom, reg) {
-		if (pmp_count <= pmp_idx)
-			break;
+		if (!is_valid_pmp_idx(pmp_count, pmp_idx))
+			return SBI_EFAIL;
 
 		pmp_flags = 0;
 
-- 
2.48.0


-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

  parent reply	other threads:[~2025-10-08  8:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-08  8:44 [PATCH v2 0/8] SmePMP bugfixes and improvement Yu-Chien Peter Lin
2025-10-08  8:44 ` [PATCH v2 1/8] lib: sbi_hart: move sbi_hart_get_smepmp_flags() to sbi_domain Yu-Chien Peter Lin
2025-11-02 10:27   ` Anup Patel
2025-10-08  8:44 ` [PATCH v2 2/8] lib: sbi_domain: allow specifying inaccessible region Yu-Chien Peter Lin
2025-11-02 10:32   ` Anup Patel
2025-10-08  8:44 ` [PATCH v2 3/8] lib: sbi_domain: print unsupported SmePMP permissions Yu-Chien Peter Lin
2025-11-02 10:40   ` Anup Patel
2025-10-08  8:44 ` Yu-Chien Peter Lin [this message]
2025-11-02 10:55   ` [PATCH v2 4/8] lib: sbi_hart: return error when insufficient PMP entries available Anup Patel
2025-10-08  8:44 ` [PATCH v2 5/8] lib: sbi_domain: add SBI_DOMAIN_MEMREGION_FW memregion flag Yu-Chien Peter Lin
2025-11-02 10:55   ` Anup Patel
2025-10-08  8:44 ` [PATCH v2 6/8] lib: sbi_domain: ensure consistent firmware PMP entries Yu-Chien Peter Lin
2025-11-02 11:04   ` Anup Patel
2025-10-08  8:44 ` [PATCH v2 7/8] lib: sbi: sbi_hart: track firmware PMP entries when configuring SmePMP Yu-Chien Peter Lin
2025-11-02 11:28   ` Anup Patel
2025-10-08  8:44 ` [PATCH v2 8/8] lib: sbi_domain_context: preserve firmware PMP entries during domain context switch Yu-Chien Peter Lin
2025-11-02 11:41   ` Anup Patel
2025-11-02 12:29 ` [PATCH v2 0/8] SmePMP bugfixes and improvement Anup Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251008084444.3525615-5-peter.lin@sifive.com \
    --to=peter.lin@sifive.com \
    --cc=alvinga@andestech.com \
    --cc=anup@brainfault.org \
    --cc=greentime.hu@sifive.com \
    --cc=opensbi@lists.infradead.org \
    --cc=wxjstz@126.com \
    --cc=zong.li@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox