Fix some PMP implementation
cover Fix some PMP implementation
This patch set contains the fixes for wrong index of pmpcfg CSR on rv64, and the pmp range in CSR function table.
Zong Li (2):
target/riscv: Fix the range of pmpcfg of CSR funcion table target/riscv/pmp.c: Fix the index offset on RV64
target/riscv/csr.c | 2 +- target/riscv/pmp.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-)
Signature · 1 line
2.27.0
PATCH 1/2 target/riscv: Fix the range of pmpcfg of CSR funcion table
The range of Physical Memory Protection should be from CSR_PMPCFG0 to CSR_PMPCFG3, not to CSR_PMPADDR9.
--- target/riscv/csr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -1
@@ -1353,7 +1353,7 @@ static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTINST] = { hmode, read_mtinst, write_mtinst },/* Physical Memory Protection */ - [CSR_PMPCFG0 ... CSR_PMPADDR9] = { pmp, read_pmpcfg, write_pmpcfg }, + [CSR_PMPCFG0 ... CSR_PMPCFG3] = { pmp, read_pmpcfg, write_pmpcfg }, [CSR_PMPADDR0 ... CSR_PMPADDR15] = { pmp, read_pmpaddr, write_pmpaddr }, /* Performance Counters */
Signature · 1 line
2.27.0
PATCH 2/2 target/riscv/pmp.c: Fix the index offset on RV64
In reply to Zong LiOn RV64, the reg_index is 2 (pmpcfg2 CSR) after the seventh pmp entry, it is not 1 (pmpcfg1 CSR) like RV32. In the original implementation, the second parameter of pmp_write_cfg is "reg_index * sizeof(target_ulong)", and we get the the result which is started from 16 if reg_index is 2, but we expect that it should be started from 8. Separate the implementation for RV32 and RV64 respectively.
--- target/riscv/pmp.c | 9 +++++++++ 1 file changed, 9 insertions(+)
target/riscv/pmp.c +9 -0
@@ -320,8 +320,13 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,for (i =0 ; i <sizeof (target_ulong); i++) { cfg_val = (val >>8 * i) &0xff ; +#if defined(TARGET_RISCV32 )pmp_write_cfg (env, (reg_index *sizeof (target_ulong)) + i, cfg_val); +#elif defined(TARGET_RISCV64 ) +pmp_write_cfg (env, ((reg_index >>1 ) *sizeof (target_ulong)) + i, + cfg_val); +#endif } }
@@ -336,7 +341,11 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)target_ulong val =0 ;for (i =0 ; i <sizeof (target_ulong); i++) { +#if defined(TARGET_RISCV32 ) val =pmp_read_cfg (env, (reg_index *sizeof (target_ulong)) + i); +#elif defined(TARGET_RISCV64 ) + val =pmp_read_cfg (env, ((reg_index >>1 ) *sizeof (target_ulong)) + i); +#endif cfg_val |= (val << (i *8 )); }trace_pmpcfg_csr_read (env->mhartid, reg_index, cfg_val);
Signature · 1 line
2.27.0
Re: [PATCH 1/2] target/riscv: Fix the range of pmpcfg of CSR funcion table
In reply to Zong LiQuoted 5 lines · Zong Li
The range of Physical Memory Protection should be from
CSR_PMPCFG0toCSR_PMPCFG3, not toCSR_PMPADDR9.Signed-off-by Zong Li <[email protected]>
Alistair
Quoted 21 lines
--- target/riscv/csr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/csr.c +1
-2@@ -1353,7 +1353,7 @@ static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MTINST] = { hmode, read_mtinst, write_mtinst },/* Physical Memory Protection */ - [CSR_PMPCFG0 ... CSR_PMPADDR9] = { pmp, read_pmpcfg, write_pmpcfg }, + [CSR_PMPCFG0 ... CSR_PMPCFG3] = { pmp, read_pmpcfg, write_pmpcfg }, [CSR_PMPADDR0 ... CSR_PMPADDR15] = { pmp, read_pmpaddr, write_pmpaddr }, /* Performance Counters */ --2.27.0
Re: [PATCH 2/2] target/riscv/pmp.c: Fix the index offset on RV64
In reply to Zong LiHi Zong,
Quoted 19 lines · Zong Li
On RV64, the
reg_indexis 2 (pmpcfg2 CSR) after the seventh pmp entry, it is not 1 (pmpcfg1 CSR) like RV32. In the original implementation, the second parameter ofpmp_write_cfgis "reg_index*sizeof(target_ulong)", and we get the the result which is started from 16 ifreg_indexis 2, but we expect that it should be started from 8. Separate the implementation for RV32 and RV64 respectively.Signed-off-by Zong Li <[email protected]>--- target/riscv/pmp.c | 9 +++++++++ 1 file changed, 9 insertions(+)target/riscv/pmp.c +0
-0
target/riscv/pmp.c void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, · line 329
Quoted 10 lines
target/riscv/pmp.c +5
-0@@ -320,8 +320,13 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,for (i =0 ; i <sizeof (target_ulong); i++) { cfg_val = (val >>8 * i) &0xff ; +#if defined(TARGET_RISCV32 )pmp_write_cfg (env, (reg_index *sizeof (target_ulong)) + i, cfg_val); +#elif defined(TARGET_RISCV64 ) +pmp_write_cfg (env, ((reg_index >>1 ) *sizeof (target_ulong)) + i, + cfg_val); +#endif
Can you please simplify this by shifting reg_index outside the for loop for RV64?
Quoted 5 lines
target/riscv/pmp.c +0
-0} }@@ -336,7 +341,11 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)target_ulong val =0 ;
target/riscv/pmp.c target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index) · line 351
Quoted 10 lines
target/riscv/pmp.c +4
-0for (i =0 ; i <sizeof (target_ulong); i++) { +#if defined(TARGET_RISCV32 ) val =pmp_read_cfg (env, (reg_index *sizeof (target_ulong)) + i); +#elif defined(TARGET_RISCV64 ) + val =pmp_read_cfg (env, ((reg_index >>1 ) *sizeof (target_ulong)) + i); +#endif cfg_val |= (val << (i *8 )); }trace_pmpcfg_csr_read (env->mhartid, reg_index, cfg_val);
Regards,
Bin
Re: [PATCH 1/2] target/riscv: Fix the range of pmpcfg of CSR funcion table
In reply to Zong LiQuoted 9 lines · Zong Li
The range of Physical Memory Protection should be from
CSR_PMPCFG0toCSR_PMPCFG3, not toCSR_PMPADDR9.Signed-off-by Zong Li <[email protected]>--- target/riscv/csr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Re: [PATCH 2/2] target/riscv/pmp.c: Fix the index offset on RV64
In reply to Bin MengQuoted 24 lines · Bin Meng
Hi Zong,
Quoted 20 lines · Zong Li
On RV64, the
reg_indexis 2 (pmpcfg2 CSR) after the seventh pmp entry, it is not 1 (pmpcfg1 CSR) like RV32. In the original implementation, the second parameter ofpmp_write_cfgis "reg_index*sizeof(target_ulong)", and we get the the result which is started from 16 ifreg_indexis 2, but we expect that it should be started from 8. Separate the implementation for RV32 and RV64 respectively.Signed-off-by Zong Li <[email protected]>--- target/riscv/pmp.c | 9 +++++++++ 1 file changed, 9 insertions(+)target/riscv/pmp.c +0
-0@@ -320,8 +320,13 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
Quoted 13 lines
target/riscv/pmp.c void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, · line 329
Quoted 9 lines
target/riscv/pmp.c +5
-0for (i =0 ; i <sizeof (target_ulong); i++) { cfg_val = (val >>8 * i) &0xff ; +#if defined(TARGET_RISCV32 )pmp_write_cfg (env, (reg_index *sizeof (target_ulong)) + i, cfg_val); +#elif defined(TARGET_RISCV64 ) +pmp_write_cfg (env, ((reg_index >>1 ) *sizeof (target_ulong)) + i, + cfg_val); +#endif Can you please simplify this by shifting
reg_indexoutside the for loop for RV64?
OK, that would be great. Change it in the next version, thanks.
Quoted 18 lines
Quoted 5 lines
target/riscv/pmp.c +0
-0} }@@ -336,7 +341,11 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)target_ulong val =0 ;target/riscv/pmp.c target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index) · line 351
Quoted 10 lines
target/riscv/pmp.c +4
-0for (i =0 ; i <sizeof (target_ulong); i++) { +#if defined(TARGET_RISCV32 ) val =pmp_read_cfg (env, (reg_index *sizeof (target_ulong)) + i); +#elif defined(TARGET_RISCV64 ) + val =pmp_read_cfg (env, ((reg_index >>1 ) *sizeof (target_ulong)) + i); +#endif cfg_val |= (val << (i *8 )); }trace_pmpcfg_csr_read (env->mhartid, reg_index, cfg_val);Regards,
Bin