Skip to contentSkip to conversation

target/riscv: improve code accuracy and

PATCH In review Ruibo Lu 2 participants 4 messages Jul 8, 2023 – Jul 9, 2023 RISC-V

lore
Share & tools
lore

Ruibo Lu

author
#0 lore raw

v3 cover target/riscv: improve code accuracy and

I'm so sorry. As a newcomer, I'm not familiar with the patch mechanism. I mistakenly added the reviewer's "Reviewed-by" line into the wrong commit, So I have resent this patchset

Changes in v3:
* fix the allignment of pmp_is_in_range parameter line

Changes in v2:
* change the initial values of sa_in and ea_in to false
* change the condition expression when address area fully in range

Ruibo Lu (2):
target/riscv: Remove redundant check in pmp_is_locked target/riscv: Optimize ambiguous local variable in pmp_hart_has_privs

 target/riscv/pmp.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)
Signature · 1 line
2.41.0
author
#1 lore raw

PATCH v3 1/2 target/riscv: Remove redundant check in pmp_is_locked

the check of top PMP is redundant and will not influence the return value, so consider remove it

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1733

---
 target/riscv/pmp.c | 5 -----
 1 file changed, 5 deletions(-)
target/riscv/pmp.c +0 -5
index 9d8db493e6..1a9279ba88 100644
@@ -49,11 +49,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.41.0
author
#2 lore raw

PATCH v3 2/2 target/riscv: Optimize ambiguous local variable in pmp_hart_has_privs

In reply to Ruibo Lu

These two values represents whether start/end address is in pmp_range. However, the type and name of them is ambiguous. This commit change the name and type of them to improve code readability and accuracy.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1735

---
 target/riscv/pmp.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
target/riscv/pmp.c +11 -11
index 1a9279ba88..ea3d29217a 100644
@@ -203,16 +203,16 @@ void pmp_update_rule_nums(CPURISCVState *env)
     }
 }
 
-static int pmp_is_in_range(CPURISCVState *env, int pmp_index,
-                           target_ulong addr)
+static bool pmp_is_in_range(CPURISCVState *env, int pmp_index,
+                            target_ulong addr)
 {
-    int result = 0;
+    bool result = false;
 
     if ((addr >= env->pmp_state.addr[pmp_index].sa) &&
         (addr <= env->pmp_state.addr[pmp_index].ea)) {
-        result = 1;
+        result = true;
     } else {
-        result = 0;
+        result = false;
     }
 
     return result;
@@ -287,8 +287,8 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
 {
     int i = 0;
     int pmp_size = 0;
-    target_ulong s = 0;
-    target_ulong e = 0;
+    bool sa_in = false;
+    bool ea_in = false;
 
     /* Short cut if no rules */
     if (0 == pmp_get_num_rules(env)) {
@@ -314,11 +314,11 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
      * from low to high
      */
     for (i = 0; i < MAX_RISCV_PMPS; i++) {
-        s = pmp_is_in_range(env, i, addr);
-        e = pmp_is_in_range(env, i, addr + pmp_size - 1);
+        sa_in = pmp_is_in_range(env, i, addr);
+        ea_in = pmp_is_in_range(env, i, addr + pmp_size - 1);
 
         /* partially inside */
-        if ((s + e) == 1) {
+        if (sa_in ^ ea_in) {
             qemu_log_mask(LOG_GUEST_ERROR,
                           "pmp violation - access is partially inside\n");
             *allowed_privs = 0;
@@ -339,7 +339,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
             (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 (sa_in && ea_in && (PMP_AMATCH_OFF != a_field)) {
             /*
              * If the PMP entry is not off and the address is in range,
              * do the priv check
Signature · 1 line
2.41.0
#3 lore raw

Re: [PATCH v3 2/2] target/riscv: Optimize ambiguous local variable in pmp_hart_has_privs

08.07.2023 12:10, Ruibo Lu пишет:

Quoted 28 lines

These two values represents whether start/end address is in pmp_range. However, the type and name of them is ambiguous. This commit change the name and type of them to improve code readability and accuracy.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1735

---
  target/riscv/pmp.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
target/riscv/pmp.c +3 -3
index 1a9279ba88..ea3d29217a 100644
@@ -203,16 +203,16 @@ void pmp_update_rule_nums(CPURISCVState *env)
      }
  }
 
-static int pmp_is_in_range(CPURISCVState *env, int pmp_index,
-                           target_ulong addr)
+static bool pmp_is_in_range(CPURISCVState *env, int pmp_index,
+                            target_ulong addr)
  {
-    int result = 0;
+    bool result = false;
 

target/riscv/pmp.c void pmp_update_rule_nums(CPURISCVState *env) · line 218

Quoted 10 lines
target/riscv/pmp.c +2 -2
      if ((addr >= env->pmp_state.addr[pmp_index].sa) &&
          (addr <= env->pmp_state.addr[pmp_index].ea)) {
-        result = 1;
+        result = true;
      } else {
-        result = 0;
+        result = false;
      }
 
      return result;

And the initial assignment of result isn't needed. How about this:

return (addr >= env->pmp_state.addr[pmp_index].sa) &&

        (addr <= env->pmp_state.addr[pmp_index].ea);

instead? :)

Quoted 13 lines
target/riscv/pmp.c +2 -2
@@ -287,8 +287,8 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
  {
      int i = 0;
      int pmp_size = 0;
-    target_ulong s = 0;
-    target_ulong e = 0;
+    bool sa_in = false;
+    bool ea_in = false;
 
      /* Short cut if no rules */
      if (0 == pmp_get_num_rules(env)) {
@@ -314,11 +314,11 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
       * from low to high

target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, · line 321

Quoted 10 lines
target/riscv/pmp.c +3 -3
       */
      for (i = 0; i < MAX_RISCV_PMPS; i++) {
-        s = pmp_is_in_range(env, i, addr);
-        e = pmp_is_in_range(env, i, addr + pmp_size - 1);
+        sa_in = pmp_is_in_range(env, i, addr);
+        ea_in = pmp_is_in_range(env, i, addr + pmp_size - 1);
 
          /* partially inside */
-        if ((s + e) == 1) {
+        if (sa_in ^ ea_in) {

Dunno how for others, but to me this is a bit difficult to read, as an exclusive or isn't a commonly used operation. Maybe sa_in != ea_in ?

Thanks,

/mjt

Quoted 12 lines
target/riscv/pmp.c +1 -1
              qemu_log_mask(LOG_GUEST_ERROR,
                            "pmp violation - access is partially inside\n");
              *allowed_privs = 0;
@@ -339,7 +339,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
              (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 (sa_in && ea_in && (PMP_AMATCH_OFF != a_field)) {
              /*
               * If the PMP entry is not off and the address is in range,
               * do the priv check