Skip to contentSkip to conversation

target/riscv: Fix pmp range wraparound on zero

PATCH Applied Vac Chen 2 participants 3 messages Jul 6, 2025 – Jul 8, 2025 RISC-V

lore
Share & tools
lore

Vac Chen

author
#0 lore raw

PATCH 1/1 target/riscv: Fix pmp range wraparound on zero

pmp_is_in_range() prefers to match addresses within the interval [start, end]. To archieve this, pmpaddrX is decremented during the end address update.

In TOR mode, a rule is ignored if its start address is greater than or equal to its end address.

However, if pmpaddrX is set to 0, this decrement operation causes the calulated end address to wrap around to UINT_MAX. In this scenario, the address guard for this PMP entry would become ineffective.

This patch addresses the issue by moving the guard check earlier, preventing the problematic wraparound when pmpaddrX is zero.

---
 target/riscv/pmp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target/riscv/pmp.c +4 -3
index 3540327c9a..72f1372a49 100644
@@ -211,11 +211,12 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
         break;
 
     case PMP_AMATCH_TOR:
-        sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
-        ea = (this_addr << 2) - 1u;
-        if (sa > ea) {
+        if (prev_addr >= this_addr) {
             sa = ea = 0u;
+            break;
         }
+        sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
+        ea = (this_addr << 2) - 1u;
         break;
 
     case PMP_AMATCH_NA4:
Signature · 1 line
2.50.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Vac Chen

pmp_is_in_range() prefers to match addresses within the interval [start, end]. To archieve this, pmpaddrX is decremented during the end address update.

In TOR mode, a rule is ignored if its start address is greater than or equal to its end address.

Quoted 8 lines

However, if pmpaddrX is set to 0, this decrement operation causes the calulated end address to wrap around to UINT_MAX. In this scenario, the address guard for this PMP entry would become ineffective.

This patch addresses the issue by moving the guard check earlier, preventing the problematic wraparound when pmpaddrX is zero.

Alistair

Quoted 28 lines
---
 target/riscv/pmp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target/riscv/pmp.c +4 -4
index 3540327c9a..72f1372a49 100644
@@ -211,11 +211,12 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
         break;
 
     case PMP_AMATCH_TOR:
-        sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
-        ea = (this_addr << 2) - 1u;
-        if (sa > ea) {
+        if (prev_addr >= this_addr) {
             sa = ea = 0u;
+            break;
         }
+        sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
+        ea = (this_addr << 2) - 1u;
         break;
 
     case PMP_AMATCH_NA4:
--

2.50.0

#2 lore raw
In reply to Vac Chen
Quoted 8 lines · Vac Chen

pmp_is_in_range() prefers to match addresses within the interval [start, end]. To archieve this, pmpaddrX is decremented during the end address update.

In TOR mode, a rule is ignored if its start address is greater than or equal to its end address.

Quoted 8 lines

However, if pmpaddrX is set to 0, this decrement operation causes the calulated end address to wrap around to UINT_MAX. In this scenario, the address guard for this PMP entry would become ineffective.

This patch addresses the issue by moving the guard check earlier, preventing the problematic wraparound when pmpaddrX is zero.

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 28 lines
---
 target/riscv/pmp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target/riscv/pmp.c +4 -4
index 3540327c9a..72f1372a49 100644
@@ -211,11 +211,12 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
         break;
 
     case PMP_AMATCH_TOR:
-        sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
-        ea = (this_addr << 2) - 1u;
-        if (sa > ea) {
+        if (prev_addr >= this_addr) {
             sa = ea = 0u;
+            break;
         }
+        sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
+        ea = (this_addr << 2) - 1u;
         break;
 
     case PMP_AMATCH_NA4:
--

2.50.0