target/riscv/pmp: unlock all PMP entries on reset
PATCH 1/1 target/riscv/pmp: unlock all PMP entries on reset
pmp_unlock_entries() bounds its loop with pmp_get_num_rules(), which is the count of entries whose A field is not OFF, not the number of implemented PMP entries. When the active rules are not contiguous from index 0, every entry at an index greater than or equal to num_rules keeps its lock bit across a CPU reset.
Those entries stay locked after the reset has cleared mseccfg, so mseccfg.RLB is no longer set and pmp_is_readonly() silently discards subsequent pmpcfg writes. Firmware started after the reset finds PMP entries it did not configure and cannot clear.
Reproduced on -machine virt -cpu rv64,smepmp=true with a bare-metal payload that sets pmpcfg0 = 0x99990000 (entries 2 and 3 as L | NAPOT | R, entries 0 and 1 left OFF) and then resets through the sifive_test device. On the second boot pmpcfg0 reads back 0x99990000 and writing 0 to it leaves the value unchanged. With this patch the same payload reads 0x01010000 on the second boot and clearing pmpcfg0 succeeds.
Iterate over the implemented entries instead.
--- target/riscv/tcg/pmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
target/riscv/tcg/pmp.c +2 -2
@@ -201,10 +201,10 @@ 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); +uint8_t pmp_regions =riscv_cpu_cfg (env)->pmp_regions;int i; -for (i =0 ; i < pmp_num; i++) { +for (i =0 ; i < pmp_regions; i++) { env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK |PMP_AMATCH ); } }
Signature · 1 line
2.43.0
Quoted 34 lines · Sebastian Alba Vives
pmp_unlock_entries()bounds its loop withpmp_get_num_rules(), which is the count of entries whose A field is not OFF, not the number of implemented PMP entries. When the active rules are not contiguous from index 0, every entry at an index greater than or equal tonum_ruleskeeps its lock bit across a CPU reset.Those entries stay locked after the reset has cleared mseccfg, so mseccfg.RLB is no longer set and
pmp_is_readonly()silently discards subsequent pmpcfg writes. Firmware started after the reset finds PMP entries it did not configure and cannot clear.Reproduced on -machine virt -cpu rv64,smepmp=true with a bare-metal payload that sets pmpcfg0 =
0x99990000(entries 2 and 3 as L | NAPOT | R, entries 0 and 1 left OFF) and then resets through thesifive_testdevice. On the second boot pmpcfg0 reads back0x99990000and writing 0 to it leaves the value unchanged. With this patch the same payload reads0x01010000on the second boot and clearing pmpcfg0 succeeds.Iterate over the implemented entries instead.
Fixes 4bf501dc0118 ("target/riscv: pmp: Clear pmp/smepmp bits on reset")Signed-off-by Sebastian Alba Vives <[email protected]>--- target/riscv/tcg/pmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)target/riscv/tcg/pmp.c +0
-0@@ -201,10 +201,10 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)void pmp_unlock_entries (CPURISCVState *env)
target/riscv/tcg/pmp.c static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val) · line 210
Quoted 10 lines
target/riscv/tcg/pmp.c +2
-2{ -uint32_t pmp_num =pmp_get_num_rules (env); +uint8_t pmp_regions =riscv_cpu_cfg (env)->pmp_regions;int i; -for (i =0 ; i < pmp_num; i++) { +for (i =0 ; i < pmp_regions; i++) { env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK |PMP_AMATCH ); } }
Why is the entire env->pmp_state not zeroed instead?
r~
PATCH v2 1/1 target/riscv: pmp: clear the entire PMP state on reset
In reply to Sebastian Alba Vivespmp_unlock_entries() bounded 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.
Clearing only PMP_LOCK and PMP_AMATCH is not enough either. The priv spec only mandates that the A and L fields be reset, but QEMU keeps derived state next to the architectural registers: pmp_state.addr[] and pmp_state.num_rules 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.
Zero the whole structure instead. Zero is the correct reset value for every field: a cfg_reg of 0 is A=OFF with no permissions, and a num_rules of 0 restores the no-rules short cut. Rename the function since it no longer only unlocks entries.
---
Changes in v2:
- Zero the whole env->pmp_state instead of masking PMP_LOCK and PMP_AMATCH over a bounded set of entries, as suggested by Richard. v1 also left pmp_state.addr[] and pmp_state.num_rules stale across reset, which the memset covers as well.
v1: https://lore.kernel.org/qemu-devel/[email protected]/
target/riscv/cpu.c | 4 ++-- target/riscv/tcg/pmp.c | 9 ++------- target/riscv/tcg/pmp.h | 2 +- 3 files changed, 5 insertions(+), 10 deletions(-)
target/riscv/cpu.c +2 -2
@@ -1048,7 +1048,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) }/* -* Clear mseccfg and unlock all the PMP entries upon reset. +* Clear mseccfg and the PMP state upon reset. * This is required as per the priv, smepmp, and other security * extension specifications that share this CSR, and is needed * to clear stale entries across reboots.
@@ -1060,7 +1060,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->mseccfg = 0; } -pmp_unlock_entries (env); +pmp_reset (env);#endif /* ifdef CONFIG_TCG */ #else env->priv =PRV_U ;
target/riscv/tcg/pmp.c +2 -7
@@ -199,14 +199,9 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)return false; } -void pmp_unlock_entries (CPURISCVState *env) +void pmp_reset (CPURISCVState *env) { -uint32_t pmp_num =pmp_get_num_rules (env); -int i; - -for (i =0 ; i < pmp_num; i++) { - env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK |PMP_AMATCH ); - } +memset (&env->pmp_state,0 ,sizeof (env->pmp_state)); }static void pmp_decode_napot (hwaddr a,hwaddr *sa,hwaddr *ea)
target/riscv/tcg/pmp.h +1 -1
@@ -79,7 +79,7 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index);void pmp_update_rule_nums (CPURISCVState *env);uint32_t pmp_get_num_rules (CPURISCVState *env);int pmp_priv_to_page_prot (pmp_priv_t pmp_priv); -void pmp_unlock_entries (CPURISCVState *env); +void pmp_reset (CPURISCVState *env);#define MSECCFG_MML_ISSET (env) get_field(env->mseccfg, MSECCFG_MML)#define MSECCFG_MMWP_ISSET (env) get_field(env->mseccfg, MSECCFG_MMWP)
Signature · 1 line
2.43.0
Re: [PATCH v2] target/riscv: pmp: clear the entire PMP state on reset
Two things, one about reading the test program's output and one about the approach.
The test program decides which boot it is in by reading pmpcfg0: zero means first boot, so it prints 'A', programs entries 2 and 3 as locked NAPOT, and resets through sifive_test. The second-boot path prints 'B', dumps pmpcfg0, attempts to clear it, dumps it again, and is the only path that signals PASS and exits. On an unpatched build the second boot reads back 0x99990000 and the clear attempt is ignored, so the value is printed twice: the entries survive the reset and are immutable from then on, since mseccfg is cleared before pmp_unlock_entries() runs and RLB is gone with it. With this patch pmpcfg0 reads zero after every reset, so the program takes the first-boot path every time and prints 'A' indefinitely, never reaching its exit. The repeating 'A' is the pass condition, not a hang: it means no PMP state survived the reset.
On clearing the whole struct rather than individual fields: Spike reaches the same post-reset state by a different route. In riscv-isa-sim at 17e8a69fb755, state_t::reset() calls csr_init() (riscv/processor.cc:132), which reallocates mseccfg and every pmpaddr and pmpcfg object (riscv/csr_init.cc:328 and 336-342); base_pmpaddr_csr_t's constructor starts from val(0), cfg(0) (riscv/csrs.cc:88-93), and processor_t::reset() then flushes the TLB (riscv/processor.cc:159). It also keeps no derived PMP state that could go stale: state_t holds only the pmpaddr CSR objects (riscv/processor.h:174) and recomputes the range bounds on every check (riscv/csrs.cc:103-116), so there is no counterpart to pmp_state.addr[] or num_rules; num_rules does not occur anywhere under riscv/. Its scan is bounded by the number of implemented entries (mmu_t::pmp_ok calls pmp_lookup(addr, len, 0, proc->n_pmp), riscv/mmu.cc:519), and the A field is tested before any address comparison (base_pmpaddr_csr_t::match4, riscv/csrs.cc:124).
Clearing the whole struct gives QEMU the same reset state, and removes the possibility of the architectural fields and the derived bounds disagreeing.
If you would prefer the bound fix and the state clearing as two separate patches, I can respin it that way.
Quoted 110 lines · Sebastian Alba Vives
pmp_unlock_entries()bounded its loop withpmp_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_rulesis 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, sopmp_is_readonly()then silently discards every subsequent pmpcfg write to those entries. The stale rules survive for the lifetime of the machine.Clearing only
PMP_LOCKandPMP_AMATCHis not enough either. The priv spec only mandates that the A and L fields be reset, but QEMU keeps derived state next to the architectural registers: pmp_state.addr[] and pmp_state.num_rules 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 inpmp_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.Zero the whole structure instead. Zero is the correct reset value for every field: a
cfg_regof 0 is A=OFF with no permissions, and anum_rulesof 0 restores the no-rules short cut. Rename the function since it no longer only unlocks entries.Fixes 4bf501dc0118 ("target/riscv: pmp: Clear pmp/smepmp bits on reset")Suggested-by Richard Henderson <[email protected]>Signed-off-by Sebastian Alba Vives <[email protected]>---
Changes in v2:
- Zero the wholeenv->pmp_stateinstead of maskingPMP_LOCKandPMP_AMATCHover a bounded set of entries, as suggested by Richard. v1 also left pmp_state.addr[] and pmp_state.num_rules stale across reset, which the memset covers as well.v1:
https://lore.kernel.org/qemu-devel/[email protected]/target/riscv/cpu.c | 4 ++-- target/riscv/tcg/pmp.c | 9 ++------- target/riscv/tcg/pmp.h | 2 +- 3 files changed, 5 insertions(+), 10 deletions(-)target/riscv/cpu.c +1
-1@@ -1048,7 +1048,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) }/* -* Clear mseccfg and unlock all the PMP entries upon reset. +* Clear mseccfg and the PMP state upon reset. * This is required as per the priv, smepmp, and other security * extension specifications that share this CSR, and is needed * to clear stale entries across reboots.
target/riscv/cpu.c +1
-1@@ -1060,7 +1060,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->mseccfg = 0; } -pmp_unlock_entries (env); +pmp_reset (env);#endif /* ifdef CONFIG_TCG */ #else env->priv = PRV_U;target/riscv/tcg/pmp.c +2
-7@@ -199,14 +199,9 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val) return false; } -void pmp_unlock_entries (CPURISCVState *env) +void pmp_reset (CPURISCVState *env) { -uint32_t pmp_num =pmp_get_num_rules (env); -int i; - -for (i =0 ; i < pmp_num; i++) { - env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK |PMP_AMATCH ); - } +memset (&env->pmp_state,0 ,sizeof (env->pmp_state)); }static void
pmp_decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea)target/riscv/tcg/pmp.h +1
-1@@ -79,7 +79,7 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index);void pmp_update_rule_nums (CPURISCVState *env);uint32_t pmp_get_num_rules (CPURISCVState *env);int pmp_priv_to_page_prot (pmp_priv_t pmp_priv); -void pmp_unlock_entries (CPURISCVState *env); +void pmp_reset (CPURISCVState *env);#define MSECCFG_MML_ISSET (env) get_field(env->mseccfg, MSECCFG_MML)#define
MSECCFG_MMWP_ISSET(env)get_field(env->mseccfg, MSECCFG_MMWP)
--
2.43.0
Signature · 1 line
Sebastián Alba
Re: [PATCH v2] target/riscv: pmp: clear the entire PMP state on reset
In reply to Sebastian Alba VivesQuoted 32 lines · Sebastian Alba Vives
pmp_unlock_entries()bounded its loop withpmp_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_rulesis 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, sopmp_is_readonly()then silently discards every subsequent pmpcfg write to those entries. The stale rules survive for the lifetime of the machine.Clearing only
PMP_LOCKandPMP_AMATCHis not enough either. The priv spec only mandates that the A and L fields be reset, but QEMU keeps derived state next to the architectural registers: pmp_state.addr[] and pmp_state.num_rules 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 inpmp_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.Zero the whole structure instead. Zero is the correct reset value for every field: a
cfg_regof 0 is A=OFF with no permissions, and anum_rulesof 0 restores the no-rules short cut. Rename the function since it no longer only unlocks entries.Fixes 4bf501dc0118 ("target/riscv: pmp: Clear pmp/smepmp bits onreset")
Quoted 8 lines
Suggested-by Richard Henderson <[email protected]>Signed-off-by Sebastian Alba Vives <[email protected]>---
Changes in v2:
- Zero the wholeenv->pmp_stateinstead of maskingPMP_LOCKandPMP_AMATCHover a bounded set of entries, as suggested by Richard. v1 also left pmp_state.addr[] and pmp_state.num_rules stale across reset, which the memset covers as well.
I think your v1 is actually better.
Richard asked why you don't just zero pmp_state, but the answer is because the RISC-V spec states that on reset "Writable PMP registers’ A and L fields are set to 0". So we should follow that
Alistair
Quoted 70 lines
v1:
https://lore.kernel.org/qemu-devel/[email protected]/target/riscv/cpu.c | 4 ++--
target/riscv/tcg/pmp.c | 9 ++-------
target/riscv/tcg/pmp.h | 2 +-3 files changed, 5 insertions(+), 10 deletions(-)target/riscv/cpu.c +0
-0@@ -1048,7 +1048,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) }
/*
- * Clear mseccfg and unlock all the PMP entries upon reset. + * Clear mseccfg and the PMP state upon reset.
* This is required as per the priv, smepmp, and other security
* extension specifications that share this CSR, and is needed
* to clear stale entries across reboots.target/riscv/cpu.c +0
-0@@ -1060,7 +1060,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->mseccfg = 0; }
-pmp_unlock_entries(env);
+pmp_reset(env);
#endif /* ifdefCONFIG_TCG*/
#else
env->priv=PRV_U;target/riscv/tcg/pmp.c +0
-0@@ -199,14 +199,9 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val) return false; }target/riscv/tcg/pmp.c +1
-1-void pmp_unlock_entries (CPURISCVState *env) +void pmp_reset (CPURISCVState *env){
-uint32_tpmp_num=pmp_get_num_rules(env);
- int i;
-
- for (i = 0; i <pmp_num; i++) {
- env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK |PMP_AMATCH); - }
+memset(&env->pmp_state, 0, sizeof(env->pmp_state)); }
static voidpmp_decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea)target/riscv/tcg/pmp.h +1
-1@@ -79,7 +79,7 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index); void pmp_update_rule_nums(CPURISCVState *env); uint32_t pmp_get_num_rules(CPURISCVState *env); int pmp_priv_to_page_prot(pmp_priv_t pmp_priv); -void pmp_unlock_entries (CPURISCVState *env); +void pmp_reset (CPURISCVState *env);
#defineMSECCFG_MML_ISSET(env)get_field(env->mseccfg, MSECCFG_MML)#defineMSECCFG_MMWP_ISSET(env)get_field(env->mseccfg, MSECCFG_MMWP)
Alistair Francis
Reviewed-byQuoted 49 lines · Richard Henderson
Quoted 48 lines · Sebastian Alba Vives
pmp_unlock_entries()bounds its loop withpmp_get_num_rules(), which is
the count of entries whose A field is not OFF, not the number of implemented PMP entries. When the active rules are not contiguous from
index 0, every entry at an index greater than or equal tonum_ruleskeeps its lock bit across a CPU reset.Those entries stay locked after the reset has cleared mseccfg, so mseccfg.RLB is no longer set and
pmp_is_readonly()silently discards
subsequent pmpcfg writes. Firmware started after the reset finds PMP
entries it did not configure and cannot clear.Reproduced on -machine virt -cpu rv64,smepmp=true with a bare-metal payload that sets pmpcfg0 =
0x99990000(entries 2 and 3 as L | NAPOT | R, entries 0 and 1 left OFF) and then resets through thesifive_testdevice. On the second boot pmpcfg0 reads back0x99990000
and writing 0 to it leaves the value unchanged. With this patch the same payload reads0x01010000on the second boot and clearing pmpcfg0
succeeds.Iterate over the implemented entries instead.
Fixes 4bf501dc0118 ("target/riscv: pmp: Clear pmp/smepmp bits onreset")
Signed-off-by Sebastian Alba Vives <[email protected]>---
target/riscv/tcg/pmp.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)target/riscv/tcg/pmp.c +0
-0@@ -201,10 +201,10 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
voidpmp_unlock_entries(CPURISCVState *env)
{
-uint32_tpmp_num=pmp_get_num_rules(env);
+uint8_tpmp_regions= riscv_cpu_cfg(env)->pmp_regions; int i;
Quoted 8 lines
Quoted 6 lines
- for (i = 0; i <
pmp_num; i++) {
+ for (i = 0; i <pmp_regions; i++) { env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK |PMP_AMATCH);
}
}Why is the entire
env->pmp_statenot zeroed instead?
The RISC-V spec specifically states that on reset:
"Writable PMP registers’ A and L fields are set to 0...", so this does seem to be the correct approach
Alistair
Quoted 3 lines
r~
Re: [PATCH v2] target/riscv: pmp: clear the entire PMP state on reset
In reply to Alistair FrancisFair enough on the A and L fields, I'll follow the spec there.
The reason v2 went further... v1 leaves pmp_state.addr[] and num_rules stale. Those are QEMU's own derived state, recomputed only on the pmpaddr/pmpcfg write paths and never at reset. pmp_is_in_range() compares against addr[].sa and .ea without looking at the A field, and the partially-inside check in pmp_hart_has_privs() runs before A is tested. So a range left from before the reset can still deny a straddling access with A=OFF. That part isn't a spec question, it's bookkeeping the spec doesn't know about.
I'll respin as two patches:
1/2 bound the loop by the implemented entry count instead of
pmp_get_num_rules(), clearing A and L. This is v1.
2/2 reset addr[] and num_rules alongside it.
Happy for you to take just 1/2 and leave 2/2 to stand on its own.
Sebastián
Quoted 122 lines · Alistair Francis
Quoted 32 lines · Sebastian Alba Vives
pmp_unlock_entries()bounded its loop withpmp_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_rulesis 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, sopmp_is_readonly()then silently discards every subsequent pmpcfg write to those entries. The stale rules survive for the lifetime of the machine.Clearing only
PMP_LOCKandPMP_AMATCHis not enough either. The priv spec only mandates that the A and L fields be reset, but QEMU keeps derived state next to the architectural registers: pmp_state.addr[] and pmp_state.num_rules 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 inpmp_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.Zero the whole structure instead. Zero is the correct reset value for every field: a
cfg_regof 0 is A=OFF with no permissions, and anum_rulesof 0 restores the no-rules short cut. Rename the function since it no longer only unlocks entries.Fixes 4bf501dc0118 ("target/riscv: pmp: Clear pmp/smepmp bits onreset")
Quoted 8 lines
Suggested-by Richard Henderson <[email protected]>Signed-off-by Sebastian Alba Vives <[email protected]>---
Changes in v2:
- Zero the wholeenv->pmp_stateinstead of maskingPMP_LOCKandPMP_AMATCHover a bounded set of entries, as suggested by Richard. v1 also left pmp_state.addr[] and pmp_state.num_rules stale across reset, which the memset covers as well.I think your v1 is actually better.
Richard asked why you don't just zero
pmp_state, but the answer is because the RISC-V spec states that on reset "Writable PMP registers’ A and L fields are set to 0". So we should follow thatAlistair
Quoted 3 lines
v1:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 67 lines
target/riscv/cpu.c | 4 ++-- target/riscv/tcg/pmp.c | 9 ++------- target/riscv/tcg/pmp.h | 2 +- 3 files changed, 5 insertions(+), 10 deletions(-)target/riscv/cpu.c +1
-1@@ -1048,7 +1048,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) }/* -* Clear mseccfg and unlock all the PMP entries upon reset. +* Clear mseccfg and the PMP state upon reset. * This is required as per the priv, smepmp, and other security * extension specifications that share this CSR, and is needed * to clear stale entries across reboots.
target/riscv/cpu.c +1
-1@@ -1060,7 +1060,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->mseccfg = 0; } -pmp_unlock_entries (env); +pmp_reset (env);#endif /* ifdef CONFIG_TCG */ #else env->priv = PRV_U;target/riscv/tcg/pmp.c +2
-7@@ -199,14 +199,9 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val) return false; } -void pmp_unlock_entries (CPURISCVState *env) +void pmp_reset (CPURISCVState *env) { -uint32_t pmp_num =pmp_get_num_rules (env); -int i; - -for (i =0 ; i < pmp_num; i++) { - env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK |PMP_AMATCH ); - } +memset (&env->pmp_state,0 ,sizeof (env->pmp_state)); }static void
pmp_decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea)target/riscv/tcg/pmp.h +1
-1@@ -79,7 +79,7 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index);void pmp_update_rule_nums (CPURISCVState *env);uint32_t pmp_get_num_rules (CPURISCVState *env);int pmp_priv_to_page_prot (pmp_priv_t pmp_priv); -void pmp_unlock_entries (CPURISCVState *env); +void pmp_reset (CPURISCVState *env);#define MSECCFG_MML_ISSET (env) get_field(env->mseccfg, MSECCFG_MML)#define
MSECCFG_MMWP_ISSET(env)get_field(env->mseccfg,MSECCFG_MMWP)
Signature · 1 line
Sebastián Alba
I sent a v3 a few minutes ago, before seeing this. It splits the change in two:
1/2 this fix, with a reworded commit message. Functionally the
same, though I kept the variable as pmp_num instead of renaming it, so let me know whether you'd rather I carry your Reviewed-by across or just take v1 from this thread.
2/2 clears pmp_state.addr[] and num_rules. The spec doesn't cover
those, but they stay stale across reset and the difference is observable from a guest. There's a measurement in the v3 cover letter.
v3:
https://lore.kernel.org/qemu-devel/[email protected]/
Sebastián
Quoted 72 lines · Alistair Francis
Quoted 49 lines · Richard Henderson
Quoted 48 lines · Sebastian Alba Vives
pmp_unlock_entries()bounds its loop withpmp_get_num_rules(), which is
the count of entries whose A field is not OFF, not the number of implemented PMP entries. When the active rules are not contiguous from
index 0, every entry at an index greater than or equal tonum_ruleskeeps its lock bit across a CPU reset.Those entries stay locked after the reset has cleared mseccfg, so mseccfg.RLB is no longer set and
pmp_is_readonly()silently discards
subsequent pmpcfg writes. Firmware started after the reset finds PMP
entries it did not configure and cannot clear.Reproduced on -machine virt -cpu rv64,smepmp=true with a bare-metal payload that sets pmpcfg0 =
0x99990000(entries 2 and 3 as L | NAPOT | R, entries 0 and 1 left OFF) and then resets through thesifive_testdevice. On the second boot pmpcfg0 reads back0x99990000
and writing 0 to it leaves the value unchanged. With this patch the same payload reads0x01010000on the second boot and clearing pmpcfg0
succeeds.Iterate over the implemented entries instead.
Fixes 4bf501dc0118 ("target/riscv: pmp: Clear pmp/smepmp bits onreset")
Signed-off-by Sebastian Alba Vives <[email protected]>--- target/riscv/tcg/pmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)target/riscv/tcg/pmp.c +1
-1@@ -201,10 +201,10 @@ 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); + uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;int i;Quoted 8 lines
Quoted 6 lines
target/riscv/tcg/pmp.c +1
-1-for (i =0 ; i < pmp_num; i++) { +for (i =0 ; i < pmp_regions; i++) { env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK |PMP_AMATCH );}}
Why is the entire
env->pmp_statenot zeroed instead?The RISC-V spec specifically states that on reset:
"Writable PMP registers’ A and L fields are set to 0...", so this does seem to be the correct approach
Reviewed-by Alistair Francis <[email protected]>Alistair
Quoted 3 lines
r~
Signature · 1 line
Sebastián Alba