target/riscv: Align pmp size to pmp-granularity
PATCH 1/1 target/riscv: Align pmp size to pmp-granularity
When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it automatically aligned to the PMP granularity.
--- target/riscv/pmp.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
target/riscv/pmp.c +8 -2
@@ -167,11 +167,12 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)uint8_t a_field =pmp_get_a_field (val);/* * When granularity g >= 1 (i.e., granularity > 4 bytes), -* the NA4 (Naturally Aligned 4-byte) mode is not selectable +* the NA4 (Naturally Aligned 4-byte) mode is not selectable. +* In this case, an NA4 setting is reinterpreted as a NAPOT mode. */ if ((riscv_cpu_cfg (env)->pmp_granularity >MIN_RISCV_PMP_GRANULARITY ) && (a_field ==PMP_AMATCH_NA4 )) { -return false; + val |=PMP_AMATCH ; } env->pmp_state.pmp[pmp_index].cfg_reg = val;pmp_update_rule_addr (env, pmp_index);
@@ -251,6 +252,11 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)break ;case PMP_AMATCH_NAPOT : +/* Align to pmp_granularity */ +if (g >=2 ) { + this_addr |= ((1ULL << (g -1ULL )) -1ULL ); + } +pmp_decode_napot (this_addr, &sa, &ea);break ;
Signature · 1 line
2.48.1
Quoted 7 lines · Jay Chang
When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it automatically aligned to the PMP granularity.
Signed-off-by Jay Chang <[email protected]>Reviewed-by Frank Chang <[email protected]>---
Quoted 34 lines
target/riscv/pmp.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)target/riscv/pmp.c +8
-2@@ -167,11 +167,12 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)uint8_t a_field =pmp_get_a_field (val);/* * When granularity g >= 1 (i.e., granularity > 4 bytes), -* the NA4 (Naturally Aligned 4-byte) mode is not selectable +* the NA4 (Naturally Aligned 4-byte) mode is not selectable. +* In this case, an NA4 setting is reinterpreted as a NAPOT mode. */ if ((riscv_cpu_cfg (env)->pmp_granularity >MIN_RISCV_PMP_GRANULARITY ) && (a_field ==PMP_AMATCH_NA4 )) { -return false; + val |=PMP_AMATCH ; } env->pmp_state.pmp[pmp_index].cfg_reg = val;pmp_update_rule_addr (env, pmp_index);@@ -251,6 +252,11 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)break ;case PMP_AMATCH_NAPOT : +/* Align to pmp_granularity */ +if (g >=2 ) { + this_addr |= ((1ULL << (g -1ULL )) -1ULL ); + } +pmp_decode_napot (this_addr, &sa, &ea);break ;
Hi,
Quoted 30 lines · Jay Chang
When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it automatically aligned to the PMP granularity.
Signed-off-by Jay Chang <[email protected]>Reviewed-by Frank Chang <[email protected]>--- target/riscv/pmp.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)target/riscv/pmp.c +3
-2@@ -167,11 +167,12 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)uint8_t a_field =pmp_get_a_field (val);/* * When granularity g >= 1 (i.e., granularity > 4 bytes), -* the NA4 (Naturally Aligned 4-byte) mode is not selectable +* the NA4 (Naturally Aligned 4-byte) mode is not selectable. +* In this case, an NA4 setting is reinterpreted as a NAPOT mode. */ if ((riscv_cpu_cfg (env)->pmp_granularity >MIN_RISCV_PMP_GRANULARITY ) && (a_field ==PMP_AMATCH_NA4 )) { -return false; + val |=PMP_AMATCH ; } env->pmp_state.pmp[pmp_index].cfg_reg = val;pmp_update_rule_addr (env, pmp_index);
target/riscv/pmp.c void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index) · line 257
Quoted 6 lines
target/riscv/pmp.c +3
-0@@ -251,6 +252,11 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)break ;case PMP_AMATCH_NAPOT : +/* Align to pmp_granularity */ +if (g >=2 ) { + this_addr |= ((1ULL << (g -1ULL )) -1ULL );
Could we use QEMU_ALIGN_UP() here?
Quoted 5 lines
target/riscv/pmp.c +2
-0+ } +pmp_decode_napot (this_addr, &sa, &ea);break ;
Hi Philippe,
Thank you for the suggestion. QEMU_ALIGN_UP() is not suitable for NAPOT because the logic is different:
- QEMU_ALIGN_UP() performs alignment (rounds up to the nearest multiple)
- NAPOT requires setting lower bits to 1 (not rounding up)
For example, with g=3 and this_addr=0x1005:
- Current code: 0x1005 | 0x3 = 0x1007 (sets lower 2 bits)
- QEMU_ALIGN_UP: QEMU_ALIGN_UP(0x1005, 4) = 0x1008 (rounds up)
However, you're right that the TOR case can be improved using ROUND_DOWN() since it clears lower bits (which is alignment)
I'll update the patch with this improvement.
Quoted 54 lines · Philippe Mathieu-Daudé
Hi,
target/riscv/pmp.c static bool pmp_write_cfg(CPURISCVState *env,
Quoted 15 lines · Jay Chang
When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it automatically aligned to the PMP granularity.
Signed-off-by Jay Chang <[email protected]>Reviewed-by Frank Chang <[email protected]>--- target/riscv/pmp.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)target/riscv/pmp.c +0
-0@@ -167,11 +167,12 @@ static bool pmp_write_cfg(CPURISCVState *env,
uint32_tpmp_index,uint8_tval)target/riscv/pmp.c static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
Quoted 5 lines
target/riscv/pmp.c +1
-1uint8_t a_field =pmp_get_a_field (val);/* * When granularity g >= 1 (i.e., granularity > 4 bytes), -* the NA4 (Naturally Aligned 4-byte) mode is not selectable +* the NA4 (Naturally Aligned 4-byte) mode is not selectable.
target/riscv/pmp.c static bool pmp_write_cfg(CPURISCVState *env, · line 171
Quoted 1 line
target/riscv/pmp.c +1
-0+* In this case, an NA4 setting is reinterpreted as a NAPOT mode.
target/riscv/pmp.c static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
Quoted 3 lines
target/riscv/pmp.c +0
-0*/ if ((riscv_cpu_cfg(env)->pmp_granularity > MIN_RISCV_PMP_GRANULARITY) && (a_field ==
PMP_AMATCH_NA4)) {target/riscv/pmp.c void pmp_update_rule_addr(CPURISCVState *env,
Quoted 6 lines
target/riscv/pmp.c +1
-1-return false; + val |=PMP_AMATCH ; } env->pmp_state.pmp[pmp_index].cfg_reg = val;pmp_update_rule_addr (env, pmp_index);@@ -251,6 +252,11 @@ void pmp_update_rule_addr(CPURISCVState *env,
uint32_tpmp_index)target/riscv/pmp.c void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index) · line 257
Quoted 6 lines
target/riscv/pmp.c +3
-0break ;case PMP_AMATCH_NAPOT : +/* Align to pmp_granularity */ +if (g >=2 ) { + this_addr |= ((1ULL << (g -1ULL )) -1ULL );Could we use
QEMU_ALIGN_UP()here?Quoted 5 lines
target/riscv/pmp.c +2
-0+ } +pmp_decode_napot (this_addr, &sa, &ea);break ;
Quoted 3 lines · Jay Chang
When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it automatically aligned to the PMP granularity.
There's a verb missing here. It "should be"? "needs to be" aligned?
Is it a qemu-stable material?
/mjt
Hi Michael
Thank you for the review. You're right, the sentence needs a verb.
It should read:
" it needs to be aligned to the PMP granularity."
Yes, this is qemu-stable material. The PMP alignment implements WARL from the RISC-V specification.
Quoted 11 lines · Michael Tokarev
Quoted 3 lines · Jay Chang
When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it automatically aligned to the PMP granularity.
There's a verb missing here. It "should be"? "needs to be" aligned?
Is it a qemu-stable material?
/mjt