Skip to contentSkip to conversation

target/riscv: pmp: fix stale PMP state across reset

PATCH Applied Sebastian Alba Vives 2 participants 6 messages Oct 22 RISC-V

lore
Share & tools
lore
#0 lore raw

v3 cover target/riscv: pmp: fix stale PMP state across reset

v2 cleared the whole pmp_state with a memset. Alistair pointed out that the priv spec only requires the A and L fields to be reset, so v3 splits the change: 1/2 stays within what the spec mandates, and 2/2 handles the derived state QEMU keeps alongside the architectural registers.

1/2 is the v1 fix. It bounds the loop by the number of implemented regions rather than pmp_get_num_rules(), so entries past the last active rule are no longer skipped.

2/2 clears pmp_state.addr[] and pmp_state.num_rules. Those are not architectural, so the spec does not speak to them, but they are recomputed on the pmpaddr and pmpcfg write paths only and never at reset.

The second one is observable from a guest. A bare-metal M-mode program configures PMP entry 0 as NA4 over [0x80100000, 0x80100003], resets through sifive_test, and on the second boot performs an 8-byte load at 0x80100000, which starts inside the old range and ends outside it:

  tree          pmpcfg0 after reset   8-byte load at 0x80100000
  1/2 only      0x00000001            load access fault
  1/2 + 2/2     0x00000001            succeeds

The architectural state is identical in both runs. The difference is that with 1/2 alone num_rules is still 1 and addr[0] still holds the old bounds, so pmp_hart_has_privs() skips the no-rules short cut and reaches the partially-inside check. That check calls pmp_is_in_range(), which compares against addr[].sa and addr[].ea only, and returns before the A field is ever tested.

v2: https://lore.kernel.org/qemu-devel/[email protected]/
v1: https://lore.kernel.org/qemu-devel/[email protected]/

Sebastian Alba Vives (2):
target/riscv: pmp: clear A and L on every implemented PMP entry at
reset
target/riscv: pmp: reset the derived PMP bounds and rule count

 target/riscv/tcg/pmp.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/2 target/riscv: pmp: clear A and L on every implemented PMP entry at reset

pmp_unlock_entries() bounds its loop with pmp_get_num_rules(), which returns the number of entries whose A field is not OFF rather than the number of implemented entries. With a sparse configuration the loop stops early: with entries 0 and 1 OFF and entries 2 and 3 locked, num_rules is 2, the loop covers i = 0 and i = 1, and entries 2 and 3 keep their lock and address-match bits across reset.

riscv_cpu_reset_hold() clears mseccfg, and with it RLB, before calling this, so pmp_is_readonly() then silently discards every subsequent pmpcfg write to those entries. The stale rules survive for the lifetime of the machine.

Bound the loop by the number of implemented regions instead.

Fixes 4bf501dc0118 ("target/riscv: pmp: Clear pmp/smepmp bits on reset")
---
 target/riscv/tcg/pmp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/tcg/pmp.c +1 -1
index 41b55519a8..061f0e078d 100644
@@ -201,7 +201,7 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
 
 void pmp_unlock_entries(CPURISCVState *env)
 {
-    uint32_t pmp_num = pmp_get_num_rules(env);
+    uint32_t pmp_num = riscv_cpu_cfg(env)->pmp_regions;
     int i;
 
     for (i = 0; i < pmp_num; i++) {
Signature · 1 line
2.43.0
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] target/riscv: pmp: clear A and L on every implemented PMP entry at reset

Quoted 14 lines · Sebastian Alba Vives

pmp_unlock_entries() bounds its loop with pmp_get_num_rules(), which returns the number of entries whose A field is not OFF rather than the
number of implemented entries. With a sparse configuration the loop stops early: with entries 0 and 1 OFF and entries 2 and 3 locked, num_rules is 2, the loop covers i = 0 and i = 1, and entries 2 and 3 keep their lock and address-match bits across reset.

riscv_cpu_reset_hold() clears mseccfg, and with it RLB, before calling
this, so pmp_is_readonly() then silently discards every subsequent pmpcfg write to those entries. The stale rules survive for the lifetime of the machine.

Quoted 5 lines

Bound the loop by the number of implemented regions instead.

Fixes 4bf501dc0118 ("target/riscv: pmp: Clear pmp/smepmp bits on

reset")

Alistair

Quoted 18 lines

---
 target/riscv/tcg/pmp.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/tcg/pmp.c +0 -0
index 41b55519a8..061f0e078d 100644
@@ -201,7 +201,7 @@ static bool pmp_write_cfg(CPURISCVState *env,
 uint32_t pmp_index, uint8_t val)


 void pmp_unlock_entries(CPURISCVState *env)
 {
-    uint32_t pmp_num = pmp_get_num_rules(env); +    uint32_t pmp_num = riscv_cpu_cfg(env)->pmp_regions; int i;

     for (i = 0; i < pmp_num; i++) {

#3 lore raw

PATCH v3 2/2 target/riscv: pmp: reset the derived PMP bounds and rule count

In reply to Sebastian Alba Vives

The priv spec only requires the A and L fields to be reset, which the previous patch now does for every implemented entry. QEMU additionally keeps derived state next to the architectural registers:
pmp_state.addr[] holds the decoded start and end address of each entry and pmp_state.num_rules caches the active rule count. Both are recomputed from the pmpaddr and pmpcfg write paths only, never at reset.

pmp_is_in_range() compares against addr[].sa and addr[].ea without consulting the A field, and the partially-inside check in pmp_hart_has_privs() runs before the A field is tested, so a range left over from before the reset can still deny an access that straddles its boundary once the guest configures any entry again.

Clear the derived state alongside the architectural fields so the two cannot disagree.

---
 target/riscv/tcg/pmp.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/riscv/tcg/pmp.c +8 -0
index 061f0e078d..04df53c234 100644
@@ -207,6 +207,14 @@ void pmp_unlock_entries(CPURISCVState *env)
     for (i = 0; i < pmp_num; i++) {
         env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK | PMP_AMATCH);
     }
+
+    /*
+     * addr[] and num_rules are derived from the pmpaddr and pmpcfg write
+     * paths and are not recomputed at reset. Clear them here so they
+     * cannot disagree with the architectural fields cleared above.
+     */
+    memset(env->pmp_state.addr, 0, sizeof(env->pmp_state.addr));
+    env->pmp_state.num_rules = 0;
 }
 
 static void pmp_decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea)
Signature · 1 line
2.43.0
Reviewed-by on v3 2/2
#4 lore raw

Re: [PATCH v3 2/2] target/riscv: pmp: reset the derived PMP bounds and rule count

Quoted 16 lines · Sebastian Alba Vives

The priv spec only requires the A and L fields to be reset, which the previous patch now does for every implemented entry. QEMU additionally
keeps derived state next to the architectural registers:
pmp_state.addr[] holds the decoded start and end address of each entry
and pmp_state.num_rules caches the active rule count. Both are recomputed from the pmpaddr and pmpcfg write paths only, never at reset.

pmp_is_in_range() compares against addr[].sa and addr[].ea without consulting the A field, and the partially-inside check in pmp_hart_has_privs() runs before the A field is tested, so a range left over from before the reset can still deny an access that straddles its boundary once the guest configures any entry again.

Quoted 4 lines

Clear the derived state alongside the architectural fields so the two cannot disagree.

Alistair

Quoted 25 lines

---
 target/riscv/tcg/pmp.c | 8 ++++++++

 1 file changed, 8 insertions(+)
target/riscv/tcg/pmp.c +3 -0
index 061f0e078d..04df53c234 100644
@@ -207,6 +207,14 @@ void pmp_unlock_entries(CPURISCVState *env)
      for (i = 0; i < pmp_num; i++) {
          env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK | PMP_AMATCH);
      }
+
+    /*
+     * addr[] and num_rules are derived from the pmpaddr and pmpcfg

write
+     * paths and are not recomputed at reset. Clear them here so they
+     * cannot disagree with the architectural fields cleared above. +     */
+    memset(env->pmp_state.addr, 0, sizeof(env->pmp_state.addr)); +    env->pmp_state.num_rules = 0;
 }

 static void pmp_decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea)

#5 lore raw
In reply to Sebastian Alba Vives
Quoted 39 lines · Sebastian Alba Vives

v2 cleared the whole pmp_state with a memset. Alistair pointed out that
the priv spec only requires the A and L fields to be reset, so v3 splits
the change: 1/2 stays within what the spec mandates, and 2/2 handles the
derived state QEMU keeps alongside the architectural registers.

1/2 is the v1 fix. It bounds the loop by the number of implemented regions rather than pmp_get_num_rules(), so entries past the last active
rule are no longer skipped.

2/2 clears pmp_state.addr[] and pmp_state.num_rules. Those are not architectural, so the spec does not speak to them, but they are recomputed on the pmpaddr and pmpcfg write paths only and never at reset.

The second one is observable from a guest. A bare-metal M-mode program
configures PMP entry 0 as NA4 over [0x80100000, 0x80100003], resets through sifive_test, and on the second boot performs an 8-byte load at
0x80100000, which starts inside the old range and ends outside it:

  tree          pmpcfg0 after reset   8-byte load at 0x80100000 1/2 only      0x00000001            load access fault 1/2 + 2/2     0x00000001            succeeds

The architectural state is identical in both runs. The difference is that with 1/2 alone num_rules is still 1 and addr[0] still holds the old
bounds, so pmp_hart_has_privs() skips the no-rules short cut and reaches
the partially-inside check. That check calls pmp_is_in_range(), which compares against addr[].sa and addr[].ea only, and returns before the A
field is ever tested.

Quoted 4 lines

v2:
https://lore.kernel.org/qemu-devel/[email protected]/
v1:
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 8 lines

Sebastian Alba Vives (2):
  target/riscv: pmp: clear A and L on every implemented PMP entry at reset
  target/riscv: pmp: reset the derived PMP bounds and rule count

 target/riscv/tcg/pmp.c | 10 +++++++++-

 1 file changed, 9 insertions(+), 1 deletion(-)