Skip to contentSkip to conversation

Fix some PMP implementation

PATCH Superseded Zong Li 3 participants 7 messages Jul 20, 2020 – Jul 21, 2020 RISC-V

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Zong Li

author
#0 lore raw

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
author
#1 lore raw

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
index ac01c835e1..6a96a01b1c 100644
@@ -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
author
#4 lore raw

PATCH 2/2 target/riscv/pmp.c: Fix the index offset on RV64

In reply to Zong Li

On 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
index 2a2b9f5363..adcdd411e6 100644
@@ -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
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] target/riscv: Fix the range of pmpcfg of CSR funcion table

In reply to Zong Li
Quoted 5 lines · Zong Li

The range of Physical Memory Protection should be from CSR_PMPCFG0 to CSR_PMPCFG3, not to CSR_PMPADDR9.

Alistair

Quoted 21 lines
---
 target/riscv/csr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/csr.c +1 -2
index ac01c835e1..6a96a01b1c 100644
@@ -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

#5 lore raw

Re: [PATCH 2/2] target/riscv/pmp.c: Fix the index offset on RV64

In reply to Zong Li

Hi Zong,

Quoted 19 lines · Zong Li

On 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 +0 -0
index 2a2b9f5363..adcdd411e6 100644

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 -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);

Regards,
Bin

author
#6 lore raw

Re: [PATCH 2/2] target/riscv/pmp.c: Fix the index offset on RV64

In reply to Bin Meng
Quoted 24 lines · Bin Meng

Hi Zong,

Quoted 20 lines · Zong Li

On 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 +0 -0
index 2a2b9f5363..adcdd411e6 100644
@@ -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 -0
     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?

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 -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);

Regards,
Bin