Skip to contentSkip to conversation

Fix some PMP implementations

PATCH Superseded Zong Li 2 participants 7 messages Jul 24, 2020 – Jul 25, 2020 RISC-V

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Zong Li

author
#0 lore raw

v4 cover Fix some PMP implementations

This patch set contains the fixes for wrong index of pmpcfg CSR on rv64, and the pmp range in CSR function table. After 3rd version of this patch series, we also fix the PMP issues such as wrong physical address translation and ignoring PMP checking.

Chagned in v4:
- Refine the implementation. Suggested by Bin Meng.
- Add fix for PMP checking was ignored.

Changed in v3:
- Refine the implementation. Suggested by Bin Meng.
- Add fix for wrong pphysical address translation.

Changed in v2:
- Move out the shifting operation from loop. Suggested by Bin Meng.

Zong Li (4):
target/riscv: Fix the range of pmpcfg of CSR funcion table target/riscv/pmp.c: Fix the index offset on RV64 target/riscv: Fix the translation of physical address target/riscv: Change the TLB page size depends on PMP entries.

 target/riscv/cpu_helper.c | 13 +++++++--
 target/riscv/csr.c        |  2 +-
 target/riscv/pmp.c        | 60 +++++++++++++++++++++++++++++++++++++++
 target/riscv/pmp.h        |  2 ++
 4 files changed, 73 insertions(+), 4 deletions(-)
Signature · 1 line
2.27.0
author
#1 lore raw

PATCH v4 1/4 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
#2 lore raw

PATCH v4 2/4 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
target/riscv/pmp.c +8 -0
index 2a2b9f5363..e0161d6aab 100644
@@ -310,6 +310,10 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
     int i;
     uint8_t cfg_val;
 
+#if defined(TARGET_RISCV64)
+    reg_index >>= 1;
+#endif
+
     trace_pmpcfg_csr_write(env->mhartid, reg_index, val);
 
     if ((reg_index & 1) && (sizeof(target_ulong) == 8)) {
@@ -335,6 +339,10 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)
     target_ulong cfg_val = 0;
     target_ulong val = 0;
 
+#if defined(TARGET_RISCV64)
+    reg_index >>= 1;
+#endif
+
     for (i = 0; i < sizeof(target_ulong); i++) {
         val = pmp_read_cfg(env, (reg_index * sizeof(target_ulong)) + i);
         cfg_val |= (val << (i * 8));
Signature · 1 line
2.27.0
author
#5 lore raw

PATCH v4 3/4 target/riscv: Fix the translation of physical address

In reply to Zong Li

The real physical address should add the 12 bits page offset. It also causes the PMP wrong checking due to the minimum granularity of PMP is 4 byte, but we always get the physical address which is 4KB alignment, that means, we always use the start address of the page to check PMP for all addresses which in the same page.

---
 target/riscv/cpu_helper.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +2 -1
index 75d2ae3434..08b069f0c9 100644
@@ -543,7 +543,8 @@ restart:
             /* for superpage mappings, make a fake leaf PTE for the TLB's
                benefit. */
             target_ulong vpn = addr >> PGSHIFT;
-            *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT;
+            *physical = ((ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT) |
+                        (addr & ~TARGET_PAGE_MASK);
 
             /* set permissions on the TLB entry */
             if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) {
Signature · 1 line
2.27.0
author
#6 lore raw

PATCH v4 4/4 target/riscv: Change the TLB page size depends on PMP entries.

In reply to Zong Li

The minimum granularity of PMP is 4 bytes, it is small than 4KB page size, therefore, the pmp checking would be ignored if its range doesn't start from the alignment of one page. This patch detects the pmp entries and sets the small page size to TLB if there is a PMP entry which cover the page size.

---
 target/riscv/cpu_helper.c | 10 ++++++--
 target/riscv/pmp.c        | 52 +++++++++++++++++++++++++++++++++++++++
 target/riscv/pmp.h        |  2 ++
 3 files changed, 62 insertions(+), 2 deletions(-)
target/riscv/cpu_helper.c +8 -2
index 08b069f0c9..b3013bc91e 100644
@@ -693,6 +693,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
     bool first_stage_error = true;
     int ret = TRANSLATE_FAIL;
     int mode = mmu_idx;
+    target_ulong tlb_size = 0;
 
     env->guest_phys_fault_addr = 0;
 
@@ -784,8 +785,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
     }
 
     if (ret == TRANSLATE_SUCCESS) {
-        tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK,
-                     prot, mmu_idx, TARGET_PAGE_SIZE);
+        if (pmp_is_range_in_tlb(env, pa & TARGET_PAGE_MASK, &tlb_size)) {
+            tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1),
+                         prot, mmu_idx, tlb_size);
+        } else {
+            tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK,
+                         prot, mmu_idx, TARGET_PAGE_SIZE);
+        }
         return true;
     } else if (probe) {
         return false;
target/riscv/pmp.c +52 -0

55 lines not on this page · raw message

target/riscv/pmp.h +2 -0
index 8e19793132..c70f2ea4c4 100644
@@ -60,5 +60,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
 target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index);
 bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
     target_ulong size, pmp_priv_t priv, target_ulong mode);
+bool pmp_is_range_in_tlb(CPURISCVState *env, hwaddr tlb_sa,
+    target_ulong *tlb_size);
 
 #endif
Signature · 1 line
2.27.0
#3 lore raw

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

In reply to Zong Li

Hi Zong,

Quoted 31 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
target/riscv/pmp.c +4 -0
index 2a2b9f5363..e0161d6aab 100644
@@ -310,6 +310,10 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
     int i;
     uint8_t cfg_val;
 
+#if defined(TARGET_RISCV64)
+    reg_index >>= 1;
+#endif
+
     trace_pmpcfg_csr_write(env->mhartid, reg_index, val);
 
     if ((reg_index & 1) && (sizeof(target_ulong) == 8)) {
@@ -335,6 +339,10 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)
     target_ulong cfg_val = 0;

target/riscv/pmp.c target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)

Quoted 10 lines
target/riscv/pmp.c +4 -1
     target_ulong val = 0;
 
+#if defined(TARGET_RISCV64)
+    reg_index >>= 1;
+#endif
+
     for (i = 0; i < sizeof(target_ulong); i++) {
         val = pmp_read_cfg(env, (reg_index * sizeof(target_ulong)) + i);
         cfg_val |= (val << (i * 8));
--

It seems you missed to address my review comments in v3? reg_index should be shifted after we call the trace function.

Regards,
Bin

author
#4 lore raw

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

Quoted 37 lines · Bin Meng

Hi Zong,

Quoted 33 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
target/riscv/pmp.c +4 -0
index 2a2b9f5363..e0161d6aab 100644
@@ -310,6 +310,10 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
     int i;
     uint8_t cfg_val;
 
+#if defined(TARGET_RISCV64)
+    reg_index >>= 1;
+#endif
+
     trace_pmpcfg_csr_write(env->mhartid, reg_index, val);
 
     if ((reg_index & 1) && (sizeof(target_ulong) == 8)) {
@@ -335,6 +339,10 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)
     target_ulong cfg_val = 0;
     target_ulong val = 0;
 
Quoted 12 lines

target/riscv/pmp.c target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)

Quoted 8 lines
target/riscv/pmp.c +4 -1
+#if defined(TARGET_RISCV64)
+    reg_index >>= 1;
+#endif
+
     for (i = 0; i < sizeof(target_ulong); i++) {
         val = pmp_read_cfg(env, (reg_index * sizeof(target_ulong)) + i);
         cfg_val |= (val << (i * 8));
--

It seems you missed to address my review comments in v3? reg_index should be shifted after we call the trace function.

Sorry for that, there was something wrong in my local tree, I have been posting the 5th version patches, and hope it picks the suggestion already. Thanks.

Quoted 2 lines

Regards,
Bin