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 1/8] lib: sbi_hart: move sbi_hart_get_smepmp_flags() to sbi_domain
Date: Wed, 8 Oct 2025 16:44:37 +0800 [thread overview]
Message-ID: <20251008084444.3525615-2-peter.lin@sifive.com> (raw)
In-Reply-To: <20251008084444.3525615-1-peter.lin@sifive.com>
Move sbi_hart_get_smepmp_flags() from sbi_hart.c to sbi_domain.c and
rename it to sbi_domain_get_smepmp_flags() to better reflect its
purpose of converting domain memory region flags to PMP configuration.
Also removes unused parameters (scratch and dom).
Signed-off-by: Yu-Chien Peter Lin <peter.lin@sifive.com>
---
include/sbi/sbi_domain.h | 7 +++++
lib/sbi/sbi_domain.c | 58 ++++++++++++++++++++++++++++++++++
lib/sbi/sbi_hart.c | 67 ++--------------------------------------
3 files changed, 67 insertions(+), 65 deletions(-)
diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h
index e9cff0b1..a6ee553b 100644
--- a/include/sbi/sbi_domain.h
+++ b/include/sbi/sbi_domain.h
@@ -249,6 +249,13 @@ void sbi_domain_memregion_init(unsigned long addr,
unsigned long flags,
struct sbi_domain_memregion *reg);
+/**
+ * Return the Smepmp pmpcfg LRWX encoding for the flags in @reg.
+ *
+ * @param reg pointer to memory region; its flags field encodes permissions.
+ */
+unsigned int sbi_domain_get_smepmp_flags(struct sbi_domain_memregion *reg);
+
/**
* Check whether we can access specified address for given mode and
* memory region flags under a domain
diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
index 461c7e53..1fa56b6a 100644
--- a/lib/sbi/sbi_domain.c
+++ b/lib/sbi/sbi_domain.c
@@ -122,6 +122,64 @@ void sbi_domain_memregion_init(unsigned long addr,
}
}
+unsigned int sbi_domain_get_smepmp_flags(struct sbi_domain_memregion *reg)
+{
+ unsigned int pmp_flags = 0;
+
+ if (SBI_DOMAIN_MEMREGION_IS_SHARED(reg->flags)) {
+ /* Read only for both M and SU modes */
+ if (SBI_DOMAIN_MEMREGION_IS_SUR_MR(reg->flags))
+ pmp_flags = (PMP_L | PMP_R | PMP_W | PMP_X);
+
+ /* Execute for SU but Read/Execute for M mode */
+ else if (SBI_DOMAIN_MEMREGION_IS_SUX_MRX(reg->flags))
+ /* locked region */
+ pmp_flags = (PMP_L | PMP_W | PMP_X);
+
+ /* Execute only for both M and SU modes */
+ else if (SBI_DOMAIN_MEMREGION_IS_SUX_MX(reg->flags))
+ pmp_flags = (PMP_L | PMP_W);
+
+ /* Read/Write for both M and SU modes */
+ else if (SBI_DOMAIN_MEMREGION_IS_SURW_MRW(reg->flags))
+ pmp_flags = (PMP_W | PMP_X);
+
+ /* Read only for SU mode but Read/Write for M mode */
+ else if (SBI_DOMAIN_MEMREGION_IS_SUR_MRW(reg->flags))
+ pmp_flags = (PMP_W);
+ } else if (SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
+ /*
+ * When smepmp is supported and used, M region cannot have RWX
+ * permissions on any region.
+ */
+ if ((reg->flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK)
+ == SBI_DOMAIN_MEMREGION_M_RWX) {
+ sbi_printf("%s: M-mode only regions cannot have"
+ "RWX permissions\n", __func__);
+ return 0;
+ }
+
+ /* M-mode only access regions are always locked */
+ pmp_flags |= PMP_L;
+
+ if (reg->flags & SBI_DOMAIN_MEMREGION_M_READABLE)
+ pmp_flags |= PMP_R;
+ if (reg->flags & SBI_DOMAIN_MEMREGION_M_WRITABLE)
+ pmp_flags |= PMP_W;
+ if (reg->flags & SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
+ pmp_flags |= PMP_X;
+ } else if (SBI_DOMAIN_MEMREGION_SU_ONLY_ACCESS(reg->flags)) {
+ if (reg->flags & SBI_DOMAIN_MEMREGION_SU_READABLE)
+ pmp_flags |= PMP_R;
+ if (reg->flags & SBI_DOMAIN_MEMREGION_SU_WRITABLE)
+ pmp_flags |= PMP_W;
+ if (reg->flags & SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+ pmp_flags |= PMP_X;
+ }
+
+ return pmp_flags;
+}
+
bool sbi_domain_check_addr(const struct sbi_domain *dom,
unsigned long addr, unsigned long mode,
unsigned long access_flags)
diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c
index 1b50f671..2dca2699 100644
--- a/lib/sbi/sbi_hart.c
+++ b/lib/sbi/sbi_hart.c
@@ -301,69 +301,6 @@ unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch)
return hfeatures->mhpm_bits;
}
-/*
- * Returns Smepmp flags for a given domain and region based on permissions.
- */
-static unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch,
- struct sbi_domain *dom,
- struct sbi_domain_memregion *reg)
-{
- unsigned int pmp_flags = 0;
-
- if (SBI_DOMAIN_MEMREGION_IS_SHARED(reg->flags)) {
- /* Read only for both M and SU modes */
- if (SBI_DOMAIN_MEMREGION_IS_SUR_MR(reg->flags))
- pmp_flags = (PMP_L | PMP_R | PMP_W | PMP_X);
-
- /* Execute for SU but Read/Execute for M mode */
- else if (SBI_DOMAIN_MEMREGION_IS_SUX_MRX(reg->flags))
- /* locked region */
- pmp_flags = (PMP_L | PMP_W | PMP_X);
-
- /* Execute only for both M and SU modes */
- else if (SBI_DOMAIN_MEMREGION_IS_SUX_MX(reg->flags))
- pmp_flags = (PMP_L | PMP_W);
-
- /* Read/Write for both M and SU modes */
- else if (SBI_DOMAIN_MEMREGION_IS_SURW_MRW(reg->flags))
- pmp_flags = (PMP_W | PMP_X);
-
- /* Read only for SU mode but Read/Write for M mode */
- else if (SBI_DOMAIN_MEMREGION_IS_SUR_MRW(reg->flags))
- pmp_flags = (PMP_W);
- } else if (SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
- /*
- * When smepmp is supported and used, M region cannot have RWX
- * permissions on any region.
- */
- if ((reg->flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK)
- == SBI_DOMAIN_MEMREGION_M_RWX) {
- sbi_printf("%s: M-mode only regions cannot have"
- "RWX permissions\n", __func__);
- return 0;
- }
-
- /* M-mode only access regions are always locked */
- pmp_flags |= PMP_L;
-
- if (reg->flags & SBI_DOMAIN_MEMREGION_M_READABLE)
- pmp_flags |= PMP_R;
- if (reg->flags & SBI_DOMAIN_MEMREGION_M_WRITABLE)
- pmp_flags |= PMP_W;
- if (reg->flags & SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
- pmp_flags |= PMP_X;
- } else if (SBI_DOMAIN_MEMREGION_SU_ONLY_ACCESS(reg->flags)) {
- if (reg->flags & SBI_DOMAIN_MEMREGION_SU_READABLE)
- pmp_flags |= PMP_R;
- if (reg->flags & SBI_DOMAIN_MEMREGION_SU_WRITABLE)
- pmp_flags |= PMP_W;
- if (reg->flags & SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
- pmp_flags |= PMP_X;
- }
-
- return pmp_flags;
-}
-
static void sbi_hart_smepmp_set(struct sbi_scratch *scratch,
struct sbi_domain *dom,
struct sbi_domain_memregion *reg,
@@ -420,7 +357,7 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
continue;
}
- pmp_flags = sbi_hart_get_smepmp_flags(scratch, dom, reg);
+ pmp_flags = sbi_domain_get_smepmp_flags(reg);
if (!pmp_flags)
return 0;
@@ -446,7 +383,7 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
continue;
}
- pmp_flags = sbi_hart_get_smepmp_flags(scratch, dom, reg);
+ pmp_flags = sbi_domain_get_smepmp_flags(reg);
if (!pmp_flags)
return 0;
--
2.48.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
next prev 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 ` Yu-Chien Peter Lin [this message]
2025-11-02 10:27 ` [PATCH v2 1/8] lib: sbi_hart: move sbi_hart_get_smepmp_flags() to sbi_domain 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 ` [PATCH v2 4/8] lib: sbi_hart: return error when insufficient PMP entries available Yu-Chien Peter Lin
2025-11-02 10:55 ` 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-2-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