Skip to contentSkip to conversation

target/riscv: Smepmp fixes to match specification

PATCH Superseded Loïc Lefort 2 participants 13 messages Feb 25, 2025 – Mar 13, 2025 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover target/riscv: Smepmp fixes to match specification

Hi,

These patches fix Smepmp implementation to make it compliant with the spec.

First patch limits RLB to CSR changes since RLB should not affect privilege evaluation. Patch 2 extracts some common code into a function (to be used in patch 3). Patch 3 fixes validation of pmpcfg CSR writes in order to match Smepmp specification. Patch 4 is a small optimization and last patch is just removing redundant code.

Loïc Lefort (5):
target/riscv: pmp: don't allow RLB to bypass rule privileges target/riscv: pmp: move Smepmp operation conversion into a function target/riscv: pmp: fix checks on writes to pmpcfg in Smepmp MML mode target/riscv: pmp: exit csr writes early if value was not changed target/riscv: pmp: remove redundant check in pmp_is_locked

 target/riscv/pmp.c | 151 +++++++++++++++++++++++++--------------------
 1 file changed, 83 insertions(+), 68 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/5 target/riscv: pmp: don't allow RLB to bypass rule privileges

When Smepmp is supported, RLB allows bypassing locks when writing CSRs but should not affect interpretation of actual PMP rules.

pmp_is_locked is changed to only check LOCK bit and a new pmp_is_readonly function is added that checks both LOCK bit and mseccfg.RLB. pmp_write_cfg and pmpaddr_csr_write are changed to use pmp_is_readonly while pmp_hart_has_privs keeps using pmp_is_locked.

---
 target/riscv/pmp.c | 43 ++++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 19 deletions(-)
target/riscv/pmp.c +24 -19
index 85ab270dad..ddb7e0d23c 100644
@@ -45,11 +45,6 @@ static inline uint8_t pmp_get_a_field(uint8_t cfg)
  */
 static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index)
 {
-    /* mseccfg.RLB is set */
-    if (MSECCFG_RLB_ISSET(env)) {
-        return 0;
-    }
-
     if (env->pmp_state.pmp[pmp_index].cfg_reg & PMP_LOCK) {
         return 1;
     }
@@ -62,6 +57,15 @@ static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index)
     return 0;
 }
 
+/*
+ * Check whether a PMP is locked for writing or not.
+ * (i.e. has LOCK flag and mseccfg.RLB is unset)
+ */
+static int pmp_is_readonly(CPURISCVState *env, uint32_t pmp_index)
+{
+    return pmp_is_locked(env, pmp_index) && !MSECCFG_RLB_ISSET(env);
+}
+
 /*
  * Count the number of active rules.
  */
@@ -90,39 +94,40 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
 static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
 {
     if (pmp_index < MAX_RISCV_PMPS) {
-        bool locked = true;
+        bool readonly = true;
 
         if (riscv_cpu_cfg(env)->ext_smepmp) {
             /* mseccfg.RLB is set */
             if (MSECCFG_RLB_ISSET(env)) {
-                locked = false;
+                readonly = false;
             }
 
             /* mseccfg.MML is not set */
-            if (!MSECCFG_MML_ISSET(env) && !pmp_is_locked(env, pmp_index)) {
-                locked = false;
+            if (!MSECCFG_MML_ISSET(env) && !pmp_is_readonly(env, pmp_index)) {
+                readonly = false;
             }
 
             /* mseccfg.MML is set */
             if (MSECCFG_MML_ISSET(env)) {
                 /* not adding execute bit */
                 if ((val & PMP_LOCK) != 0 && (val & PMP_EXEC) != PMP_EXEC) {
-                    locked = false;
+                    readonly = false;
                 }
                 /* shared region and not adding X bit */
                 if ((val & PMP_LOCK) != PMP_LOCK &&
                     (val & 0x7) != (PMP_WRITE | PMP_EXEC)) {
-                    locked = false;
+                    readonly = false;
                 }
             }
         } else {
-            if (!pmp_is_locked(env, pmp_index)) {
-                locked = false;
+            if (!pmp_is_readonly(env, pmp_index)) {
+                readonly = false;
             }
         }
 
-        if (locked) {
-            qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpcfg write - locked\n");
+        if (readonly) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "ignoring pmpcfg write - read only\n");
         } else if (env->pmp_state.pmp[pmp_index].cfg_reg != val) {
             /* If !mseccfg.MML then ignore writes with encoding RW=01 */
             if ((val & PMP_WRITE) && !(val & PMP_READ) &&
@@ -524,14 +529,14 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
             uint8_t pmp_cfg = env->pmp_state.pmp[addr_index + 1].cfg_reg;
             is_next_cfg_tor = PMP_AMATCH_TOR == pmp_get_a_field(pmp_cfg);
 
-            if (pmp_is_locked(env, addr_index + 1) && is_next_cfg_tor) {
+            if (pmp_is_readonly(env, addr_index + 1) && is_next_cfg_tor) {
                 qemu_log_mask(LOG_GUEST_ERROR,
-                              "ignoring pmpaddr write - pmpcfg + 1 locked\n");
+                              "ignoring pmpaddr write - pmpcfg+1 read only\n");
                 return;
             }
         }
 
-        if (!pmp_is_locked(env, addr_index)) {
+        if (!pmp_is_readonly(env, addr_index)) {
             if (env->pmp_state.pmp[addr_index].addr_reg != val) {
                 env->pmp_state.pmp[addr_index].addr_reg = val;
                 pmp_update_rule_addr(env, addr_index);
@@ -542,7 +547,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
             }
         } else {
             qemu_log_mask(LOG_GUEST_ERROR,
-                          "ignoring pmpaddr write - locked\n");
+                          "ignoring pmpaddr write - read only\n");
         }
     } else {
         qemu_log_mask(LOG_GUEST_ERROR,
Signature · 1 line
2.47.2
#2 lore raw

Re: [PATCH 1/5] target/riscv: pmp: don't allow RLB to bypass rule privileges

Quoted 7 lines · Loïc Lefort

When Smepmp is supported, RLB allows bypassing locks when writing CSRs but should not affect interpretation of actual PMP rules.

pmp_is_locked is changed to only check LOCK bit and a new pmp_is_readonly function is added that checks both LOCK bit and mseccfg.RLB. pmp_write_cfg and pmpaddr_csr_write are changed to use pmp_is_readonly while pmp_hart_has_privs keeps using pmp_is_locked.

Note that this change (removing MSECCFG_RLB_ISSET(env) check from pmp_is_locked()) can impact the behavior of at least one caller in pmp_hart_has_privs():

            if (!MSECCFG_MML_ISSET(env)) {
                /*
                 * If mseccfg.MML Bit is not set, do pmp priv check
                 * This will always apply to regular PMP.
                 */
                *allowed_privs = PMP_READ | PMP_WRITE | PMP_EXEC;
                if ((mode != PRV_M) || pmp_is_locked(env, i)) {
                    *allowed_privs &= env->pmp_state.pmp[i].cfg_reg;
                }
            }

Setting allowed_privs requires !MSECCFG_RLB_ISSET(env), and after this patch allowed_privs will be set regardless of MSECCFG_RLB_ISSET(env), at least w.r.t how pmp_is_locked() works.

This might not be an issue and there's not behavioral change. In this case it would be good to mention in the commit msg why this is the case.

We can just add a !MSECCFG_RLB_ISSET(env) at this point to preserve behavior too.

Quoted 68 lines
---
  target/riscv/pmp.c | 43 ++++++++++++++++++++++++-------------------
  1 file changed, 24 insertions(+), 19 deletions(-)
target/riscv/pmp.c +14 -10
index 85ab270dad..ddb7e0d23c 100644
@@ -45,11 +45,6 @@ static inline uint8_t pmp_get_a_field(uint8_t cfg)
   */
  static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index)
  {
-    /* mseccfg.RLB is set */
-    if (MSECCFG_RLB_ISSET(env)) {
-        return 0;
-    }
-
      if (env->pmp_state.pmp[pmp_index].cfg_reg & PMP_LOCK) {
          return 1;
      }
@@ -62,6 +57,15 @@ static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index)
      return 0;
  }
 
+/*
+ * Check whether a PMP is locked for writing or not.
+ * (i.e. has LOCK flag and mseccfg.RLB is unset)
+ */
+static int pmp_is_readonly(CPURISCVState *env, uint32_t pmp_index)
+{
+    return pmp_is_locked(env, pmp_index) && !MSECCFG_RLB_ISSET(env);
+}
+
  /*
   * Count the number of active rules.
   */
@@ -90,39 +94,40 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
  static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
  {
      if (pmp_index < MAX_RISCV_PMPS) {
-        bool locked = true;
+        bool readonly = true;
 
          if (riscv_cpu_cfg(env)->ext_smepmp) {
              /* mseccfg.RLB is set */
              if (MSECCFG_RLB_ISSET(env)) {
-                locked = false;
+                readonly = false;
              }
 
              /* mseccfg.MML is not set */
-            if (!MSECCFG_MML_ISSET(env) && !pmp_is_locked(env, pmp_index)) {
-                locked = false;
+            if (!MSECCFG_MML_ISSET(env) && !pmp_is_readonly(env, pmp_index)) {
+                readonly = false;
              }
 
              /* mseccfg.MML is set */
              if (MSECCFG_MML_ISSET(env)) {
                  /* not adding execute bit */
                  if ((val & PMP_LOCK) != 0 && (val & PMP_EXEC) != PMP_EXEC) {
-                    locked = false;
+                    readonly = false;
                  }
                  /* shared region and not adding X bit */
                  if ((val & PMP_LOCK) != PMP_LOCK &&

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index) · line 124

Quoted 10 lines
target/riscv/pmp.c +3 -3
                      (val & 0x7) != (PMP_WRITE | PMP_EXEC)) {
-                    locked = false;
+                    readonly = false;
                  }
              }
          } else {
-            if (!pmp_is_locked(env, pmp_index)) {
-                locked = false;
+            if (!pmp_is_readonly(env, pmp_index)) {
+                readonly = false;

Here we can do:

                  readonly = pmp_is_readonly(env, pmp_index);

And spare an extra if.

Thanks,

Daniel

Quoted 38 lines
target/riscv/pmp.c +7 -6
              }
          }
 
-        if (locked) {
-            qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpcfg write - locked\n");
+        if (readonly) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "ignoring pmpcfg write - read only\n");
          } else if (env->pmp_state.pmp[pmp_index].cfg_reg != val) {
              /* If !mseccfg.MML then ignore writes with encoding RW=01 */
              if ((val & PMP_WRITE) && !(val & PMP_READ) &&
@@ -524,14 +529,14 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
              uint8_t pmp_cfg = env->pmp_state.pmp[addr_index + 1].cfg_reg;
              is_next_cfg_tor = PMP_AMATCH_TOR == pmp_get_a_field(pmp_cfg);
 
-            if (pmp_is_locked(env, addr_index + 1) && is_next_cfg_tor) {
+            if (pmp_is_readonly(env, addr_index + 1) && is_next_cfg_tor) {
                  qemu_log_mask(LOG_GUEST_ERROR,
-                              "ignoring pmpaddr write - pmpcfg + 1 locked\n");
+                              "ignoring pmpaddr write - pmpcfg+1 read only\n");
                  return;
              }
          }
 
-        if (!pmp_is_locked(env, addr_index)) {
+        if (!pmp_is_readonly(env, addr_index)) {
              if (env->pmp_state.pmp[addr_index].addr_reg != val) {
                  env->pmp_state.pmp[addr_index].addr_reg = val;
                  pmp_update_rule_addr(env, addr_index);
@@ -542,7 +547,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
              }
          } else {
              qemu_log_mask(LOG_GUEST_ERROR,
-                          "ignoring pmpaddr write - locked\n");
+                          "ignoring pmpaddr write - read only\n");
          }
      } else {
          qemu_log_mask(LOG_GUEST_ERROR,
#3 lore raw

Re: [PATCH 1/5] target/riscv: pmp: don't allow RLB to bypass rule privileges

Quoted 36 lines · Daniel Henrique Barboza
Quoted 1 line · Loïc Lefort

When Smepmp is supported, RLB allows bypassing locks when writing CSRs

but

Quoted 4 lines

should not affect interpretation of actual PMP rules.

pmp_is_locked is changed to only check LOCK bit and a new pmp_is_readonly function is added that checks both LOCK bit and mseccfg.RLB.

pmp_write_cfg and

Quoted 1 line

pmpaddr_csr_write are changed to use pmp_is_readonly while

pmp_hart_has_privs

Quoted 1 line

keeps using pmp_is_locked.

Note that this change (removing MSECCFG_RLB_ISSET(env) check from pmp_is_locked())
can impact the behavior of at least one caller in pmp_hart_has_privs():

             if (!MSECCFG_MML_ISSET(env)) {
                 /*
                  * If mseccfg.MML Bit is not set, do pmp priv check
                  * This will always apply to regular PMP.
                  */
                 *allowed_privs = PMP_READ | PMP_WRITE | PMP_EXEC;
                 if ((mode != PRV_M) || pmp_is_locked(env, i)) {
                     *allowed_privs &= env->pmp_state.pmp[i].cfg_reg;
                 }
             }

Setting allowed_privs requires !MSECCFG_RLB_ISSET(env), and after this patch allowed_privs
will be set regardless of MSECCFG_RLB_ISSET(env), at least w.r.t how pmp_is_locked() works.

Quoted 8 lines

This might not be an issue and there's not behavioral change. In this case it would be good
to mention in the commit msg why this is the case.

We can just add a !MSECCFG_RLB_ISSET(env) at this point to preserve behavior too.

Not checking RLB in this code path is the main point of this commit:

allowed_privs should not depend on RLB.
I'll reword the commit message in v2 to make it more explicit.

Quoted 148 lines

target/riscv/pmp.c static inline int pmp_is_locked(CPURISCVState *env,

Quoted 23 lines
---
  target/riscv/pmp.c | 43 ++++++++++++++++++++++++-------------------
  1 file changed, 24 insertions(+), 19 deletions(-)
target/riscv/pmp.c +0 -5
index 85ab270dad..ddb7e0d23c 100644
@@ -45,11 +45,6 @@ static inline uint8_t pmp_get_a_field(uint8_t cfg)
   */
  static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index)
  {
-    /* mseccfg.RLB is set */
-    if (MSECCFG_RLB_ISSET(env)) {
-        return 0;
-    }
-
      if (env->pmp_state.pmp[pmp_index].cfg_reg & PMP_LOCK) {
          return 1;
      }
@@ -62,6 +57,15 @@ static inline int pmp_is_locked(CPURISCVState *env,

uint32_t pmp_index)

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState

Quoted 16 lines
target/riscv/pmp.c +9 -0
      return 0;
  }
 
+/*
+ * Check whether a PMP is locked for writing or not.
+ * (i.e. has LOCK flag and mseccfg.RLB is unset)
+ */
+static int pmp_is_readonly(CPURISCVState *env, uint32_t pmp_index)
+{
+    return pmp_is_locked(env, pmp_index) && !MSECCFG_RLB_ISSET(env);
+}
+
  /*
   * Count the number of active rules.
   */
@@ -90,39 +94,40 @@ static inline uint8_t pmp_read_cfg(CPURISCVState

*env, uint32_t pmp_index)

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState · line 94

Quoted 1 line
target/riscv/pmp.c +0 -0
  static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index,

uint8_t val)

Quoted 4 lines
target/riscv/pmp.c +1 -1
  {
      if (pmp_index < MAX_RISCV_PMPS) {
-        bool locked = true;
+        bool readonly = true;

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)

Quoted 10 lines
target/riscv/pmp.c +1 -2
 
          if (riscv_cpu_cfg(env)->ext_smepmp) {
              /* mseccfg.RLB is set */
              if (MSECCFG_RLB_ISSET(env)) {
-                locked = false;
+                readonly = false;
              }
 
              /* mseccfg.MML is not set */
-            if (!MSECCFG_MML_ISSET(env) && !pmp_is_locked(env,

pmp_index)) {

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState · line 106

Quoted 2 lines
target/riscv/pmp.c +1 -1
-                locked = false;
+            if (!MSECCFG_MML_ISSET(env) && !pmp_is_readonly(env,

pmp_index)) {

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)

Quoted 7 lines
target/riscv/pmp.c +1 -0
+                readonly = false;
              }
 
              /* mseccfg.MML is set */
              if (MSECCFG_MML_ISSET(env)) {
                  /* not adding execute bit */
                  if ((val & PMP_LOCK) != 0 && (val & PMP_EXEC) !=

PMP_EXEC) {

Quoted 5 lines
target/riscv/pmp.c +1 -1
-                    locked = false;
+                    readonly = false;
                  }
                  /* shared region and not adding X bit */
                  if ((val & PMP_LOCK) != PMP_LOCK &&

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index) · line 124

Quoted 10 lines
target/riscv/pmp.c +3 -3
                      (val & 0x7) != (PMP_WRITE | PMP_EXEC)) {
-                    locked = false;
+                    readonly = false;
                  }
              }
          } else {
-            if (!pmp_is_locked(env, pmp_index)) {
-                locked = false;
+            if (!pmp_is_readonly(env, pmp_index)) {
+                readonly = false;

Here we can do:

                   readonly = pmp_is_readonly(env, pmp_index);

And spare an extra if.

Sure, I will change this in v2.

Thanks,

Daniel

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)

Quoted 5 lines
target/riscv/pmp.c +0 -2
              }
          }
 
-        if (locked) {
-            qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpcfg write -

locked\n");

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)

Quoted 5 lines
target/riscv/pmp.c +3 -0
+        if (readonly) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "ignoring pmpcfg write - read only\n");
          } else if (env->pmp_state.pmp[pmp_index].cfg_reg != val) {
              /* If !mseccfg.MML then ignore writes with encoding RW=01

*/

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env,

Quoted 2 lines
target/riscv/pmp.c +0 -0
              if ((val & PMP_WRITE) && !(val & PMP_READ) &&
@@ -524,14 +529,14 @@ void pmpaddr_csr_write(CPURISCVState *env,

uint32_t addr_index,

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, · line 529

Quoted 1 line
target/riscv/pmp.c +0 -0
              uint8_t pmp_cfg = env->pmp_state.pmp[addr_index +

1].cfg_reg;

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, · line 530

Quoted 1 line
target/riscv/pmp.c +0 -0
              is_next_cfg_tor = PMP_AMATCH_TOR ==

pmp_get_a_field(pmp_cfg);

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, · line 531

Quoted 3 lines
target/riscv/pmp.c +1 -1
-            if (pmp_is_locked(env, addr_index + 1) && is_next_cfg_tor) {
+            if (pmp_is_readonly(env, addr_index + 1) &&

is_next_cfg_tor) {

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, · line 528

Quoted 2 lines
target/riscv/pmp.c +0 -1
                  qemu_log_mask(LOG_GUEST_ERROR,
-                              "ignoring pmpaddr write - pmpcfg + 1

locked\n");

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, · line 533

Quoted 1 line
target/riscv/pmp.c +1 -0
+                              "ignoring pmpaddr write - pmpcfg+1 read

only\n");

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, uint32_t

Quoted 10 lines
target/riscv/pmp.c +1 -1
                  return;
              }
          }
 
-        if (!pmp_is_locked(env, addr_index)) {
+        if (!pmp_is_readonly(env, addr_index)) {
              if (env->pmp_state.pmp[addr_index].addr_reg != val) {
                  env->pmp_state.pmp[addr_index].addr_reg = val;
                  pmp_update_rule_addr(env, addr_index);
@@ -542,7 +547,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t

addr_index,

Quoted 8 lines
target/riscv/pmp.c +1 -1
              }
          } else {
              qemu_log_mask(LOG_GUEST_ERROR,
-                          "ignoring pmpaddr write - locked\n");
+                          "ignoring pmpaddr write - read only\n");
          }
      } else {
          qemu_log_mask(LOG_GUEST_ERROR,
#4 lore raw

PATCH 2/5 target/riscv: pmp: move Smepmp operation conversion into a function

In reply to Loïc Lefort
---
 target/riscv/pmp.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
target/riscv/pmp.c +12 -10
index ddb7e0d23c..b7f1430ff8 100644
@@ -31,6 +31,15 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t addr_index,
                           uint8_t val);
 static uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t addr_index);
 
+/*
+ * Convert the PMP permissions to match the truth table in the Smepmp spec.
+ */
+static inline uint8_t pmp_get_smepmp_operation(uint8_t cfg)
+{
+    return ((cfg & PMP_LOCK) >> 4) | ((cfg & PMP_READ) << 2) |
+           (cfg & PMP_WRITE) | ((cfg & PMP_EXEC) >> 2);
+}
+
 /*
  * Accessor method to extract address matching type 'a field' from cfg reg
  */
@@ -357,16 +366,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
         const uint8_t a_field =
             pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
 
-        /*
-         * Convert the PMP permissions to match the truth table in the
-         * Smepmp spec.
-         */
-        const uint8_t smepmp_operation =
-            ((env->pmp_state.pmp[i].cfg_reg & PMP_LOCK) >> 4) |
-            ((env->pmp_state.pmp[i].cfg_reg & PMP_READ) << 2) |
-            (env->pmp_state.pmp[i].cfg_reg & PMP_WRITE) |
-            ((env->pmp_state.pmp[i].cfg_reg & PMP_EXEC) >> 2);
-
         if (((s + e) == 2) && (PMP_AMATCH_OFF != a_field)) {
             /*
              * If the PMP entry is not off and the address is in range,
@@ -385,6 +384,9 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
                 /*
                  * If mseccfg.MML Bit set, do the enhanced pmp priv check
                  */
+                const uint8_t smepmp_operation =
+                    pmp_get_smepmp_operation(env->pmp_state.pmp[i].cfg_reg);
+
                 if (mode == PRV_M) {
                     switch (smepmp_operation) {
                     case 0:
Signature · 1 line
2.47.2
Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] target/riscv: pmp: move Smepmp operation conversion into a function

Quoted 2 lines · Loïc Lefort

---

Quoted 50 lines
  target/riscv/pmp.c | 22 ++++++++++++----------
  1 file changed, 12 insertions(+), 10 deletions(-)
target/riscv/pmp.c +12 -10
index ddb7e0d23c..b7f1430ff8 100644
@@ -31,6 +31,15 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t addr_index,
                            uint8_t val);
  static uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t addr_index);
 
+/*
+ * Convert the PMP permissions to match the truth table in the Smepmp spec.
+ */
+static inline uint8_t pmp_get_smepmp_operation(uint8_t cfg)
+{
+    return ((cfg & PMP_LOCK) >> 4) | ((cfg & PMP_READ) << 2) |
+           (cfg & PMP_WRITE) | ((cfg & PMP_EXEC) >> 2);
+}
+
  /*
   * Accessor method to extract address matching type 'a field' from cfg reg
   */
@@ -357,16 +366,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
          const uint8_t a_field =
              pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
 
-        /*
-         * Convert the PMP permissions to match the truth table in the
-         * Smepmp spec.
-         */
-        const uint8_t smepmp_operation =
-            ((env->pmp_state.pmp[i].cfg_reg & PMP_LOCK) >> 4) |
-            ((env->pmp_state.pmp[i].cfg_reg & PMP_READ) << 2) |
-            (env->pmp_state.pmp[i].cfg_reg & PMP_WRITE) |
-            ((env->pmp_state.pmp[i].cfg_reg & PMP_EXEC) >> 2);
-
          if (((s + e) == 2) && (PMP_AMATCH_OFF != a_field)) {
              /*
               * If the PMP entry is not off and the address is in range,
@@ -385,6 +384,9 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
                  /*
                   * If mseccfg.MML Bit set, do the enhanced pmp priv check
                   */
+                const uint8_t smepmp_operation =
+                    pmp_get_smepmp_operation(env->pmp_state.pmp[i].cfg_reg);
+
                  if (mode == PRV_M) {
                      switch (smepmp_operation) {
                      case 0:
#6 lore raw

PATCH 3/5 target/riscv: pmp: fix checks on writes to pmpcfg in Smepmp MML mode

In reply to Loïc Lefort

With Machine Mode Lockdown (mseccfg.MML) set and RLB not set, checks on pmpcfg writes would match the wrong cases of Smepmp truth table.

The existing code allows writes for the following cases:
- L=1, X=0: cases 8, 10, 12, 14
- L=0, RWX!=WX: cases 0-2, 4-6
This leaves cases 3, 7, 9, 11, 13, 15 for which writes are ignored.

From the Smepmp specification: "Adding a rule with executable privileges that either is M-mode-only or a locked Shared-Region is not possible (...)" This description matches cases 9-11, 13 of the truth table.

This commit implements an explicit check for these cases by using pmp_get_epmp_operation to convert between PMP configuration and Smepmp truth table cases.

---
 target/riscv/pmp.c | 81 ++++++++++++++++++++++++----------------------
 1 file changed, 43 insertions(+), 38 deletions(-)
target/riscv/pmp.c +43 -38
index b7f1430ff8..e0ea436f8e 100644
@@ -75,6 +75,44 @@ static int pmp_is_readonly(CPURISCVState *env, uint32_t pmp_index)
     return pmp_is_locked(env, pmp_index) && !MSECCFG_RLB_ISSET(env);
 }
 
+/*
+ * Check whether `val` is an invalid Smepmp config value
+ */
+static int pmp_is_invalid_smepmp_cfg(CPURISCVState *env, uint8_t val)
+{
+    /* No check if mseccfg.MML is not set or if mseccfg.RLB is set */
+    if (!MSECCFG_MML_ISSET(env) || MSECCFG_RLB_ISSET(env)) {
+        return 0;
+    }
+
+    /*
+     * Adding a rule with executable privileges that either is M-mode-only
+     * or a locked Shared-Region is not possible
+     */
+    switch (pmp_get_smepmp_operation(val)) {
+    case 0:
+    case 1:
+    case 2:
+    case 3:
+    case 4:
+    case 5:
+    case 6:
+    case 7:
+    case 8:
+    case 12:
+    case 14:
+    case 15:
+        return 0;
+    case 9:
+    case 10:
+    case 11:
+    case 13:
+        return 1;
+    default:
+        g_assert_not_reached();
+    }
+}
+
 /*
  * Count the number of active rules.
  */
@@ -103,46 +141,13 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
 static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
 {
     if (pmp_index < MAX_RISCV_PMPS) {
-        bool readonly = true;
-
-        if (riscv_cpu_cfg(env)->ext_smepmp) {
-            /* mseccfg.RLB is set */
-            if (MSECCFG_RLB_ISSET(env)) {
-                readonly = false;
-            }
-
-            /* mseccfg.MML is not set */
-            if (!MSECCFG_MML_ISSET(env) && !pmp_is_readonly(env, pmp_index)) {
-                readonly = false;
-            }
-
-            /* mseccfg.MML is set */
-            if (MSECCFG_MML_ISSET(env)) {
-                /* not adding execute bit */
-                if ((val & PMP_LOCK) != 0 && (val & PMP_EXEC) != PMP_EXEC) {
-                    readonly = false;
-                }
-                /* shared region and not adding X bit */
-                if ((val & PMP_LOCK) != PMP_LOCK &&
-                    (val & 0x7) != (PMP_WRITE | PMP_EXEC)) {
-                    readonly = false;
-                }
-            }
-        } else {
-            if (!pmp_is_readonly(env, pmp_index)) {
-                readonly = false;
-            }
-        }
-
-        if (readonly) {
+        if (pmp_is_readonly(env, pmp_index)) {
             qemu_log_mask(LOG_GUEST_ERROR,
                           "ignoring pmpcfg write - read only\n");
-        } else if (env->pmp_state.pmp[pmp_index].cfg_reg != val) {
-            /* If !mseccfg.MML then ignore writes with encoding RW=01 */
-            if ((val & PMP_WRITE) && !(val & PMP_READ) &&
-                !MSECCFG_MML_ISSET(env)) {
-                return false;
-            }
+        } else if (pmp_is_invalid_smepmp_cfg(env, val)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "ignoring pmpcfg write - invalid\n");
+        } else {
             env->pmp_state.pmp[pmp_index].cfg_reg = val;
             pmp_update_rule_addr(env, pmp_index);
             return true;
Signature · 1 line
2.47.2
Reviewed-by on v1 3/5
#7 lore raw

Re: [PATCH 3/5] target/riscv: pmp: fix checks on writes to pmpcfg in Smepmp MML mode

Quoted 12 lines · Loïc Lefort

With Machine Mode Lockdown (mseccfg.MML) set and RLB not set, checks on pmpcfg writes would match the wrong cases of Smepmp truth table.

The existing code allows writes for the following cases:
- L=1, X=0: cases 8, 10, 12, 14
- L=0, RWX!=WX: cases 0-2, 4-6
This leaves cases 3, 7, 9, 11, 13, 15 for which writes are ignored.

From the Smepmp specification: "Adding a rule with executable privileges that
either is M-mode-only or a locked Shared-Region is not possible (...)" This description matches cases 9-11, 13 of the truth table.

Quoted 6 lines

This commit implements an explicit check for these cases by using pmp_get_epmp_operation to convert between PMP configuration and Smepmp truth table cases.

---

Quoted 104 lines
  target/riscv/pmp.c | 81 ++++++++++++++++++++++++----------------------
  1 file changed, 43 insertions(+), 38 deletions(-)
target/riscv/pmp.c +43 -38
index b7f1430ff8..e0ea436f8e 100644
@@ -75,6 +75,44 @@ static int pmp_is_readonly(CPURISCVState *env, uint32_t pmp_index)
      return pmp_is_locked(env, pmp_index) && !MSECCFG_RLB_ISSET(env);
  }
 
+/*
+ * Check whether `val` is an invalid Smepmp config value
+ */
+static int pmp_is_invalid_smepmp_cfg(CPURISCVState *env, uint8_t val)
+{
+    /* No check if mseccfg.MML is not set or if mseccfg.RLB is set */
+    if (!MSECCFG_MML_ISSET(env) || MSECCFG_RLB_ISSET(env)) {
+        return 0;
+    }
+
+    /*
+     * Adding a rule with executable privileges that either is M-mode-only
+     * or a locked Shared-Region is not possible
+     */
+    switch (pmp_get_smepmp_operation(val)) {
+    case 0:
+    case 1:
+    case 2:
+    case 3:
+    case 4:
+    case 5:
+    case 6:
+    case 7:
+    case 8:
+    case 12:
+    case 14:
+    case 15:
+        return 0;
+    case 9:
+    case 10:
+    case 11:
+    case 13:
+        return 1;
+    default:
+        g_assert_not_reached();
+    }
+}
+
  /*
   * Count the number of active rules.
   */
@@ -103,46 +141,13 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
  static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
  {
      if (pmp_index < MAX_RISCV_PMPS) {
-        bool readonly = true;
-
-        if (riscv_cpu_cfg(env)->ext_smepmp) {
-            /* mseccfg.RLB is set */
-            if (MSECCFG_RLB_ISSET(env)) {
-                readonly = false;
-            }
-
-            /* mseccfg.MML is not set */
-            if (!MSECCFG_MML_ISSET(env) && !pmp_is_readonly(env, pmp_index)) {
-                readonly = false;
-            }
-
-            /* mseccfg.MML is set */
-            if (MSECCFG_MML_ISSET(env)) {
-                /* not adding execute bit */
-                if ((val & PMP_LOCK) != 0 && (val & PMP_EXEC) != PMP_EXEC) {
-                    readonly = false;
-                }
-                /* shared region and not adding X bit */
-                if ((val & PMP_LOCK) != PMP_LOCK &&
-                    (val & 0x7) != (PMP_WRITE | PMP_EXEC)) {
-                    readonly = false;
-                }
-            }
-        } else {
-            if (!pmp_is_readonly(env, pmp_index)) {
-                readonly = false;
-            }
-        }
-
-        if (readonly) {
+        if (pmp_is_readonly(env, pmp_index)) {
              qemu_log_mask(LOG_GUEST_ERROR,
                            "ignoring pmpcfg write - read only\n");
-        } else if (env->pmp_state.pmp[pmp_index].cfg_reg != val) {
-            /* If !mseccfg.MML then ignore writes with encoding RW=01 */
-            if ((val & PMP_WRITE) && !(val & PMP_READ) &&
-                !MSECCFG_MML_ISSET(env)) {
-                return false;
-            }
+        } else if (pmp_is_invalid_smepmp_cfg(env, val)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "ignoring pmpcfg write - invalid\n");
+        } else {
              env->pmp_state.pmp[pmp_index].cfg_reg = val;
              pmp_update_rule_addr(env, pmp_index);
              return true;
#8 lore raw

PATCH 4/5 target/riscv: pmp: exit csr writes early if value was not changed

In reply to Loïc Lefort
---
 target/riscv/pmp.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
target/riscv/pmp.c +15 -7
index e0ea436f8e..e4fee10d93 100644
@@ -141,6 +141,11 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
 static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
 {
     if (pmp_index < MAX_RISCV_PMPS) {
+        if (env->pmp_state.pmp[pmp_index].cfg_reg == val) {
+            /* no change */
+            return false;
+        }
+
         if (pmp_is_readonly(env, pmp_index)) {
             qemu_log_mask(LOG_GUEST_ERROR,
                           "ignoring pmpcfg write - read only\n");
@@ -528,6 +533,11 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
     bool is_next_cfg_tor = false;
 
     if (addr_index < MAX_RISCV_PMPS) {
+        if (env->pmp_state.pmp[addr_index].addr_reg == val) {
+            /* no change */
+            return;
+        }
+
         /*
          * In TOR mode, need to check the lock bit of the next pmp
          * (if there is a next).
@@ -544,14 +554,12 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
         }
 
         if (!pmp_is_readonly(env, addr_index)) {
-            if (env->pmp_state.pmp[addr_index].addr_reg != val) {
-                env->pmp_state.pmp[addr_index].addr_reg = val;
-                pmp_update_rule_addr(env, addr_index);
-                if (is_next_cfg_tor) {
-                    pmp_update_rule_addr(env, addr_index + 1);
-                }
-                tlb_flush(env_cpu(env));
+            env->pmp_state.pmp[addr_index].addr_reg = val;
+            pmp_update_rule_addr(env, addr_index);
+            if (is_next_cfg_tor) {
+                pmp_update_rule_addr(env, addr_index + 1);
             }
+            tlb_flush(env_cpu(env));
         } else {
             qemu_log_mask(LOG_GUEST_ERROR,
                           "ignoring pmpaddr write - read only\n");
Signature · 1 line
2.47.2
Reviewed-by on v1 4/5
#9 lore raw

Re: [PATCH 4/5] target/riscv: pmp: exit csr writes early if value was not changed

Quoted 2 lines · Loïc Lefort

---

Quoted 51 lines
  target/riscv/pmp.c | 22 +++++++++++++++-------
  1 file changed, 15 insertions(+), 7 deletions(-)
target/riscv/pmp.c +15 -7
index e0ea436f8e..e4fee10d93 100644
@@ -141,6 +141,11 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
  static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
  {
      if (pmp_index < MAX_RISCV_PMPS) {
+        if (env->pmp_state.pmp[pmp_index].cfg_reg == val) {
+            /* no change */
+            return false;
+        }
+
          if (pmp_is_readonly(env, pmp_index)) {
              qemu_log_mask(LOG_GUEST_ERROR,
                            "ignoring pmpcfg write - read only\n");
@@ -528,6 +533,11 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
      bool is_next_cfg_tor = false;
 
      if (addr_index < MAX_RISCV_PMPS) {
+        if (env->pmp_state.pmp[addr_index].addr_reg == val) {
+            /* no change */
+            return;
+        }
+
          /*
           * In TOR mode, need to check the lock bit of the next pmp
           * (if there is a next).
@@ -544,14 +554,12 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
          }
 
          if (!pmp_is_readonly(env, addr_index)) {
-            if (env->pmp_state.pmp[addr_index].addr_reg != val) {
-                env->pmp_state.pmp[addr_index].addr_reg = val;
-                pmp_update_rule_addr(env, addr_index);
-                if (is_next_cfg_tor) {
-                    pmp_update_rule_addr(env, addr_index + 1);
-                }
-                tlb_flush(env_cpu(env));
+            env->pmp_state.pmp[addr_index].addr_reg = val;
+            pmp_update_rule_addr(env, addr_index);
+            if (is_next_cfg_tor) {
+                pmp_update_rule_addr(env, addr_index + 1);
              }
+            tlb_flush(env_cpu(env));
          } else {
              qemu_log_mask(LOG_GUEST_ERROR,
                            "ignoring pmpaddr write - read only\n");
#10 lore raw

PATCH 5/5 target/riscv: pmp: remove redundant check in pmp_is_locked

In reply to Loïc Lefort

Remove useless check in pmp_is_locked, the function will return 0 in either case.

---
 target/riscv/pmp.c | 5 -----
 1 file changed, 5 deletions(-)
target/riscv/pmp.c +0 -5
index e4fee10d93..81c8bd71ce 100644
@@ -58,11 +58,6 @@ static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index)
         return 1;
     }
 
-    /* Top PMP has no 'next' to check */
-    if ((pmp_index + 1u) >= MAX_RISCV_PMPS) {
-        return 0;
-    }
-
     return 0;
 }
 
Signature · 1 line
2.47.2
Reviewed-by on v1 5/5
#11 lore raw

Re: [PATCH 5/5] target/riscv: pmp: remove redundant check in pmp_is_locked

Quoted 5 lines · Loïc Lefort

Remove useless check in pmp_is_locked, the function will return 0 in either case.

---

Quoted 19 lines
  target/riscv/pmp.c | 5 -----
  1 file changed, 5 deletions(-)
target/riscv/pmp.c +0 -5
index e4fee10d93..81c8bd71ce 100644
@@ -58,11 +58,6 @@ static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index)
          return 1;
      }
 
-    /* Top PMP has no 'next' to check */
-    if ((pmp_index + 1u) >= MAX_RISCV_PMPS) {
-        return 0;
-    }
-
      return 0;
  }
 
#12 lore raw
In reply to Loïc Lefort
Quoted 27 lines · Loïc Lefort

Hi,

These patches fix Smepmp implementation to make it compliant with the spec.

First patch limits RLB to CSR changes since RLB should not affect privilege evaluation. Patch 2 extracts some common code into a function (to be used in
patch 3). Patch 3 fixes validation of pmpcfg CSR writes in order to match Smepmp
specification. Patch 4 is a small optimization and last patch is just removing
redundant code.

Loïc Lefort (5):
target/riscv: pmp: don't allow RLB to bypass rule privileges target/riscv: pmp: move Smepmp operation conversion into a function target/riscv: pmp: fix checks on writes to pmpcfg in Smepmp MML mode target/riscv: pmp: exit csr writes early if value was not changed target/riscv: pmp: remove redundant check in pmp_is_locked

 target/riscv/pmp.c | 151 +++++++++++++++++++++++++--------------------
 1 file changed, 83 insertions(+), 68 deletions(-)

--
2.47.2

Ping?