Skip to contentSkip to conversation

lib: sbi: Check pmp index for overflow before programming

PATCH New Himanshu Chauhan 2 participants 2 messages Dec 29, 2022 – Dec 31, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi: Check pmp index for overflow before programming

Check the current pmp index if it overflows the number of pmp entries present. If so, skip the programming but print debug information about the region, domain and the last index of pmp programmed.

---
 lib/sbi/sbi_hart.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
lib/sbi/sbi_hart.c +10 -2
index 5447c52..2782c0d 100644
@@ -313,9 +313,17 @@ int sbi_hart_pmp_configure(struct sbi_scratch *scratch)
 			pmp_flags |= PMP_L;
 
 		pmp_addr =  reg->base >> PMP_SHIFT;
-		if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max)
+		if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max) {
+			if (pmp_idx+1 >= pmp_count) {
+				sbi_printf("Cannot program pmp region: 0x%lx (order: 0x%lx) for domain %s.\n",
+					   reg->base, reg->order, dom->name);
+				sbi_printf("Reason: No free PMP entry left. "
+					   "Number of PMP entries: %d Last PMP Index written: %d\n",
+					   pmp_count, pmp_idx);
+				break;
+			}
 			pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
-		else {
+		} else {
 			sbi_printf("Can not configure pmp for domain %s", dom->name);
 			sbi_printf(" because memory region address %lx or size %lx is not in range\n",
 				    reg->base, reg->order);
Signature · 1 line
2.39.0
#1 lore raw

PATCH 1/1 lib: sbi: Check pmp index for overflow before programming

Quoted 16 lines · Himanshu Chauhan

Check the current pmp index if it overflows the number of pmp entries present. If so, skip the programming but print debug information about the region, domain and the last index of pmp programmed.

---
 lib/sbi/sbi_hart.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
lib/sbi/sbi_hart.c +0 -0
index 5447c52..2782c0d 100644

lib/sbi/sbi_hart.c int sbi_hart_pmp_configure(struct sbi_scratch *scratch) · line 317

Quoted 6 lines
lib/sbi/sbi_hart.c +2 -1
@@ -313,9 +313,17 @@ int sbi_hart_pmp_configure(struct sbi_scratch *scratch)
                        pmp_flags |= PMP_L;
 
                pmp_addr =  reg->base >> PMP_SHIFT;
-               if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max)
+               if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max) {
+                       if (pmp_idx+1 >= pmp_count) {

This check is meaningless. There is already a check at the beginning of the loop:

if (pmp_count <= pmp_idx)
break;

Quoted 4 lines
lib/sbi/sbi_hart.c +4 -0
+                               sbi_printf("Cannot program pmp region: 0x%lx (order: 0x%lx) for domain %s.\n",
+                                          reg->base, reg->order, dom->name);
+                               sbi_printf("Reason: No free PMP entry left. "
+                                          "Number of PMP entries: %d Last PMP Index written: %d\n",

lib/sbi/sbi_hart.c int sbi_hart_pmp_configure(struct sbi_scratch *scratch)

Quoted 10 lines
lib/sbi/sbi_hart.c +4 -2
+                                          pmp_count, pmp_idx);
+                               break;
+                       }
                        pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
-               else {
+               } else {
                        sbi_printf("Can not configure pmp for domain %s", dom->name);
                        sbi_printf(" because memory region address %lx or size %lx is not in range\n",
                                    reg->base, reg->order);
--

Regards,
Bin