Skip to contentSkip to conversation

target/riscv: Optimize ambiguous local variable in pmp_hart_has_privs

PATCH In review Ruibo Lu 2 participants 2 messages Jun 28, 2023 RISC-V

lore
Share & tools
lore

Ruibo Lu

author
#0 lore raw

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

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.

---
 target/riscv/pmp.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
target/riscv/pmp.c +10 -10
index 1a9279ba88..aa573bab14 100644
@@ -203,16 +203,16 @@ void pmp_update_rule_nums(CPURISCVState *env)
     }
 }
 
-static int pmp_is_in_range(CPURISCVState *env, int pmp_index,
+static bool pmp_is_in_range(CPURISCVState *env, int pmp_index,
                            target_ulong addr)
 {
-    int result = 0;
+    int 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 = 0;
+    bool ea_in = 0;
 
     /* 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
#1 lore raw
Quoted 14 lines · 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.

---
  target/riscv/pmp.c | 20 ++++++++++----------
  1 file changed, 10 insertions(+), 10 deletions(-)
target/riscv/pmp.c +0 -0
index 1a9279ba88..aa573bab14 100644

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

Quoted 6 lines
target/riscv/pmp.c +1 -1
@@ -203,16 +203,16 @@ void pmp_update_rule_nums(CPURISCVState *env)
      }
  }
 
-static int pmp_is_in_range(CPURISCVState *env, int pmp_index,
+static bool pmp_is_in_range(CPURISCVState *env, int pmp_index,
                             target_ulong addr)

Maintain the alignment here.

Quoted 15 lines
target/riscv/pmp.c +3 -3
  {
-    int result = 0;
+    int 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;

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

Quoted 7 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 = 0;
+    bool ea_in = 0;

Better to use false here.

Quoted 19 lines
target/riscv/pmp.c +3 -3
      /* 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;

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

Quoted 5 lines
target/riscv/pmp.c +1 -1
@@ -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)) {

I think it's better to use "sa_in && ea_in &&(...)" here.

Regards,

Weiwei Li

Quoted 3 lines
target/riscv/pmp.c +0 -0
              /*
               * If the PMP entry is not off and the address is in range,
               * do the priv check