Skip to contentSkip to conversation

lib: sbi_domain: Fix PMP condition for addresses

PATCH Superseded [email protected] 3 participants 4 messages Feb 27, 2024 – Mar 4, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi_domain: Fix PMP condition for addresses

From: max <[email protected]>

In order to put an address/size into an NAPOT PMP region, the address needs to be aligned to the size shifted two to the right.

Example:
fw_start: 0x80040000
fw_size: 0x00040000
PMP address: 0x80040000 | (0x00040000 >> 2) fw_size is valid, but not in the current implementation

---
 lib/sbi/sbi_domain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_domain.c +1 -1
index 4e9f742..94ee717 100644
@@ -749,7 +749,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 		return SBI_EINVAL;
 	}
 
-	if ((scratch->fw_start & (scratch->fw_rw_offset - 1)) != 0) {
+	if ((scratch->fw_start & ((scratch->fw_rw_offset >> 2) - 1)) != 0) {
 		sbi_printf("%s: fw_start and fw_rw_offset not aligned\n",
 			   __func__);
 		return SBI_EINVAL;
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/1 lib: sbi_domain: Fix PMP condition for addresses

? 2024-02-27???? 21:07 +0100?cmax at mailbox.org???

Quoted 4 lines

From: max <[email protected]>

In order to put an address/size into an NAPOT PMP region, the address needs to be aligned to the size shifted two to the right.

The address that needs to be aligned is
scratch->fw_start
scratch->fw_start + scratch->fw_rw_offset

Regards,
Xiang W

Quoted 29 lines

Example:
fw_start: 0x80040000
fw_size:? 0x00040000
PMP address: 0x80040000 | (0x00040000 >> 2) fw_size is valid, but not in the current implementation

---
?lib/sbi/sbi_domain.c | 2 +-
?1 file changed, 1 insertion(+), 1 deletion(-)

lib/sbi/sbi_domain.c +0 -0
index 4e9f742..94ee717 100644
@@ -749,7 +749,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 ?		return SBI_EINVAL;
 ?	}

?

lib/sbi/sbi_domain.c +1 -1
-	if ((scratch->fw_start & (scratch->fw_rw_offset - 1)) != 0) {
+	if ((scratch->fw_start & ((scratch->fw_rw_offset >> 2) - 1)) != 0) {

? sbi_printf("%s: fw_start and fw_rw_offset not aligned\n",
? ?? __func__);
? return SBI_EINVAL;

Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 1/1 lib: sbi_domain: Fix PMP condition for addresses

From: max <[email protected]>

In order to put an address/size into an NAPOT PMP region, the fw_start address needs to be aligned to the fw_rw_offset. Since the PMP address is already aligned to 4 byte by design, we have two more bits available.

Example:
fw_start: 0x80040000
fw_rw_offset: 0x00040000
PMP address: 0x80040000 | (0x00040000 >> 2) fw_rw_offset is valid, but not in the current implementation

---
I updated to commit-msg to be more clear. I hope that clears it up. This is my first email patch, so I probably made some mistakes.

kind regards,
Max

 lib/sbi/sbi_domain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_domain.c +1 -1
index 4e9f742..94ee717 100644
@@ -749,7 +749,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 		return SBI_EINVAL;
 	}
 
-	if ((scratch->fw_start & (scratch->fw_rw_offset - 1)) != 0) {
+	if ((scratch->fw_start & ((scratch->fw_rw_offset >> 2) - 1)) != 0) {
 		sbi_printf("%s: fw_start and fw_rw_offset not aligned\n",
 			   __func__);
 		return SBI_EINVAL;
Signature · 1 line
2.44.0
#3 lore raw

PATCH 1/1 lib: sbi_domain: Fix PMP condition for addresses

In reply to [email protected]
Quoted 11 lines · On Wed, Feb 28, 2024 at 1:38?AM <[email protected]> wrote:

From: max <[email protected]>

In order to put an address/size into an NAPOT PMP region, the address needs to be aligned to the size shifted two to the right.

Example:
fw_start: 0x80040000
fw_size: 0x00040000
PMP address: 0x80040000 | (0x00040000 >> 2) fw_size is valid, but not in the current implementation

We are creating two separate PMP regions for FW:
1) Read-only (Text + RO-data)
Base: fw_start
Size: fw_rw_offset
2) Read-write (Data + Stack + Scratch + Heap)
Base: fw_start + fw_rw_offset
Size: fw_size - fw_rw_offset

Due to above, fw_rw_offset MUST be non-zero value and power-of-2 Also, fw_start has to be aligned to the fw_rw_offset size.

Quoted 11 lines
---
 lib/sbi/sbi_domain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_domain.c +0 -0
index 4e9f742..94ee717 100644

lib/sbi/sbi_domain.c int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid) · line 752

Quoted 5 lines
lib/sbi/sbi_domain.c +1 -1
@@ -749,7 +749,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
                return SBI_EINVAL;
        }
 
-       if ((scratch->fw_start & (scratch->fw_rw_offset - 1)) != 0) {
+       if ((scratch->fw_start & ((scratch->fw_rw_offset >> 2) - 1)) != 0) {

For sure, this change breaks the alignment check on fw_start.

Quoted 11 lines
lib/sbi/sbi_domain.c +0 -1
                sbi_printf("%s: fw_start and fw_rw_offset not aligned\n",
                           __func__);
                return SBI_EINVAL;
--

2.43.0

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

Regards,
Anup