Skip to contentSkip to conversation

target/riscv: Check PMP before updating PTE

PATCH Applied Inochi Amaoto 2 participants 3 messages Jun 22 – Jun 29 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 target/riscv: Check PMP before updating PTE

According to the RISC-V spec, the PTE update is a supervisor write operations, and it should also follow the CPU PMP configuration like the PTE read.

---
Changed from v1:
1. Separate code statements and declarations.

---
 target/riscv/cpu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/riscv/cpu_helper.c +8 -0
index 59edcdd370..2db07f5dfb 100644
@@ -1655,10 +1655,18 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
 
     /* Page table updates need to be atomic with MTTCG enabled */
     if (updated_pte != pte && !is_debug) {
+        int pmp_prot, pmp_ret;
+
         if (!adue) {
             return TRANSLATE_FAIL;
         }
 
+        pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr,
+                                           sxlen_bytes, MMU_DATA_STORE, PRV_S);
+        if (pmp_ret != TRANSLATE_SUCCESS) {
+            return TRANSLATE_PMP_FAIL;
+        }
+
         /*
          * - if accessed or dirty bits need updating, and the PTE is
          *   in RAM, then we do so atomically with a compare and swap.

--
2.54.0

Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Inochi Amaoto

According to the RISC-V spec, the PTE update is a supervisor write operations, and it should also follow the CPU PMP configuration like the PTE read.

Alistair

Quoted 34 lines

---
Changed from v1:
1. Separate code statements and declarations.

---
 target/riscv/cpu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/riscv/cpu_helper.c +8 -1
index 59edcdd370..2db07f5dfb 100644
@@ -1655,10 +1655,18 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
 
     /* Page table updates need to be atomic with MTTCG enabled */
     if (updated_pte != pte && !is_debug) {
+        int pmp_prot, pmp_ret;
+
         if (!adue) {
             return TRANSLATE_FAIL;
         }
 
+        pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr,
+                                           sxlen_bytes, MMU_DATA_STORE, PRV_S);
+        if (pmp_ret != TRANSLATE_SUCCESS) {
+            return TRANSLATE_PMP_FAIL;
+        }
+
         /*
          * - if accessed or dirty bits need updating, and the PTE is
          *   in RAM, then we do so atomically with a compare and swap.
--

2.54.0

#2 lore raw
In reply to Inochi Amaoto
Quoted 6 lines · Inochi Amaoto

According to the RISC-V spec, the PTE update is a supervisor write operations, and it should also follow the CPU PMP configuration like the PTE read.

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 34 lines

---
Changed from v1:
1. Separate code statements and declarations.

---
 target/riscv/cpu_helper.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/riscv/cpu_helper.c +8 -1
index 59edcdd370..2db07f5dfb 100644
@@ -1655,10 +1655,18 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
 
     /* Page table updates need to be atomic with MTTCG enabled */
     if (updated_pte != pte && !is_debug) {
+        int pmp_prot, pmp_ret;
+
         if (!adue) {
             return TRANSLATE_FAIL;
         }
 
+        pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr,
+                                           sxlen_bytes, MMU_DATA_STORE, PRV_S);
+        if (pmp_ret != TRANSLATE_SUCCESS) {
+            return TRANSLATE_PMP_FAIL;
+        }
+
         /*
          * - if accessed or dirty bits need updating, and the PTE is
          *   in RAM, then we do so atomically with a compare and swap.
--

2.54.0