Skip to contentSkip to conversation

lib: sbi: Fix PMP granularity handling in sbi_hart_map_saddr()

PATCH New Anup Patel 3 participants 3 messages Nov 20, 2023 – Nov 22, 2023

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH 1/1 lib: sbi: Fix PMP granularity handling in sbi_hart_map_saddr()

The sbi_hart_map_saddr() must create PMP mapping of size greater than or equal to PMP granularity otherwise PMP mapping does not work when size parameter less than sbi_hart_pmp_granularity(scratch).

Fixes 6e44ef686a9b ("lib: sbi: Add functions to map/unmap shared

memory")

---
 lib/sbi/sbi_hart.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/sbi/sbi_hart.c +2 -1
index 77eef49..bf8792a 100644
@@ -508,7 +508,8 @@ int sbi_hart_map_saddr(unsigned long addr, unsigned long size)
 	if (is_pmp_entry_mapped(SBI_SMEPMP_RESV_ENTRY))
 		return SBI_ENOSPC;
 
-	for (order = log2roundup(size) ; order <= __riscv_xlen; order++) {
+	for (order = log2roundup(MAX(sbi_hart_pmp_granularity(scratch), size));
+	     order <= __riscv_xlen; order++) {
 		if (order < __riscv_xlen) {
 			base = addr & ~((1UL << order) - 1UL);
 			if ((base <= addr) &&
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/1 lib: sbi: Fix PMP granularity handling in sbi_hart_map_saddr()

Hi Anup,

Quoted 6 lines · Anup Patel

The sbi_hart_map_saddr() must create PMP mapping of size greater than or equal to PMP granularity otherwise PMP mapping does not work when size parameter less than sbi_hart_pmp_granularity(scratch).

Fixes 6e44ef686a9b ("lib: sbi: Add functions to map/unmap shared

memory")

This line should not be wrapped. Otherwise,

Quoted 10 lines
---
 lib/sbi/sbi_hart.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/sbi/sbi_hart.c +0 -0
index 77eef49..bf8792a 100644

lib/sbi/sbi_hart.c int sbi_hart_map_saddr(unsigned long addr, unsigned long size) · line 511

Quoted 5 lines
lib/sbi/sbi_hart.c +1 -1
@@ -508,7 +508,8 @@ int sbi_hart_map_saddr(unsigned long addr, unsigned long size)
 	if (is_pmp_entry_mapped(SBI_SMEPMP_RESV_ENTRY))
 		return SBI_ENOSPC;
 
-	for (order = log2roundup(size) ; order <= __riscv_xlen; order++) {
+	for (order = log2roundup(MAX(sbi_hart_pmp_granularity(scratch), size));

I noticed that sbi_hart_pmp_granularity() is always passed to log2roundup(), and hfeatures->pmp_gran is originally generated by shifting, so maybe the value should be stored as the log base 2 to avoid the conversions?

Regards,
Samuel

Quoted 4 lines
lib/sbi/sbi_hart.c +1 -0
+	     order <= __riscv_xlen; order++) {
 		if (order < __riscv_xlen) {
 			base = addr & ~((1UL << order) - 1UL);
 			if ((base <= addr) &&
Applied
#2 lore raw

PATCH 1/1 lib: sbi: Fix PMP granularity handling in sbi_hart_map_saddr()

Quoted 8 lines · On Wed, Nov 22, 2023 at 6:00?AM Samuel Holland <[email protected]>…

Hi Anup,

Quoted 4 lines · Anup Patel

The sbi_hart_map_saddr() must create PMP mapping of size greater than or equal to PMP granularity otherwise PMP mapping does not work when size parameter less than sbi_hart_pmp_granularity(scratch).

Quoted 6 lines
Quoted 2 lines
Fixes 6e44ef686a9b ("lib: sbi: Add functions to map/unmap shared

memory")

This line should not be wrapped. Otherwise,

Thanks, updated the commit description at the time of merging this patch.

Applied this patch to the riscv/opensbi repo.

Quoted 14 lines
Quoted 14 lines
---
 lib/sbi/sbi_hart.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/sbi/sbi_hart.c +0 -0
index 77eef49..bf8792a 100644
@@ -508,7 +508,8 @@ int sbi_hart_map_saddr(unsigned long addr, unsigned long size)
      if (is_pmp_entry_mapped(SBI_SMEPMP_RESV_ENTRY))
              return SBI_ENOSPC;
 
Quoted 6 lines

lib/sbi/sbi_hart.c int sbi_hart_map_saddr(unsigned long addr, unsigned long size) · line 511

Quoted 2 lines
lib/sbi/sbi_hart.c +1 -1
-     for (order = log2roundup(size) ; order <= __riscv_xlen; order++) {
+     for (order = log2roundup(MAX(sbi_hart_pmp_granularity(scratch), size));

I noticed that sbi_hart_pmp_granularity() is always passed to log2roundup(), and hfeatures->pmp_gran is originally generated by shifting, so maybe the value should be stored as the log base 2 to avoid the conversions?

I agree with your suggestion. Please go ahead and submit a patch for this.

Quoted 9 lines

Regards,
Samuel

Quoted 4 lines
lib/sbi/sbi_hart.c +1 -0
+          order <= __riscv_xlen; order++) {
              if (order < __riscv_xlen) {
                      base = addr & ~((1UL << order) - 1UL);
                      if ((base <= addr) &&

Regards,
Anup