Skip to contentSkip to conversation

target/riscv: Smepmp: Return error when access permission not allowed in PMP

PATCH Superseded Himanshu Chauhan 5 participants 9 messages Jun 5, 2023 – Jun 12, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: Smepmp: Return error when access permission not allowed in PMP

On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.

Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")
---
 target/riscv/pmp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/riscv/pmp.c +3 -2
index 418738afd8..6238528282 100644
@@ -435,8 +435,8 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
              * defined with PMP must be used. We shouldn't fallback on
              * finding default privileges.
              */
-            ret = true;
-            break;
+            ret = ((privs & *allowed_privs) == privs ? true : false);
+            goto _address_matched;
         }
     }
 
@@ -445,6 +445,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
         ret = pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
     }
 
+ _address_matched:
     return ret;
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 14 lines · Himanshu Chauhan

On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.

Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")
---
  target/riscv/pmp.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
target/riscv/pmp.c +0 -0
index 418738afd8..6238528282 100644

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

Quoted 7 lines
target/riscv/pmp.c +2 -2
@@ -435,8 +435,8 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
               * defined with PMP must be used. We shouldn't fallback on
               * finding default privileges.
               */
-            ret = true;
-            break;
+            ret = ((privs & *allowed_privs) == privs ? true : false);
+            goto _address_matched;

goto seems unnecessary. We can directly return (privs & *allowed_privs) == privs here.

And then we can directly return pmp_hart_has_privs_default(env, privs, allowed_privs, mode) after this loop.

Regards,

Weiwei Li

Quoted 12 lines
target/riscv/pmp.c +1 -0
          }
      }
 
@@ -445,6 +445,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
          ret = pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
 
      }
 
+ _address_matched:
      return ret;
  }
#2 lore raw

PATCH v2 1/1 target/riscv: Smepmp: Return error when access permission not allowed in PMP

In reply to Himanshu Chauhan

On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.

v2 Changes:
o Removed goto to return in place when address matches
o Call pmp_hart_has_privs_default at the end of the loop

Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")
---
 target/riscv/pmp.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
target/riscv/pmp.c +2 -8
index 418738afd8..976b199156 100644
@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                         pmp_priv_t *allowed_privs, target_ulong mode)
 {
     int i = 0;
-    bool ret = false;
     int pmp_size = 0;
     target_ulong s = 0;
     target_ulong e = 0;
@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
              * defined with PMP must be used. We shouldn't fallback on
              * finding default privileges.
              */
-            ret = true;
-            break;
+            return (privs & *allowed_privs) == privs ? true : false;
         }
     }
 
     /* No rule matched */
-    if (!ret) {
-        ret = pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
-    }
-
-    return ret;
+    return pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
 }
 
 /*
Signature · 1 line
2.34.1
#3 lore raw
Quoted 26 lines · Himanshu Chauhan

On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.

v2 Changes:
o Removed goto to return in place when address matches
o Call pmp_hart_has_privs_default at the end of the loop

Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")
---
  target/riscv/pmp.c | 10 ++--------
  1 file changed, 2 insertions(+), 8 deletions(-)
target/riscv/pmp.c +0 -1
index 418738afd8..976b199156 100644
@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                          pmp_priv_t *allowed_privs, target_ulong mode)
  {
      int i = 0;
-    bool ret = false;
      int pmp_size = 0;
      target_ulong s = 0;
      target_ulong e = 0;

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

Quoted 6 lines
target/riscv/pmp.c +1 -2
@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
               * defined with PMP must be used. We shouldn't fallback on
               * finding default privileges.
               */
-            ret = true;
-            break;
+            return (privs & *allowed_privs) == privs ? true : false;

This conditional assignment is unnecessary.

+                                  return  (privs & *allowed_privs) == privs;

Otherwise, Reviewed-by: Weiwei Li <[email protected]>

Weiwei Li

Quoted 13 lines
target/riscv/pmp.c +1 -5
          }
      }
 
      /* No rule matched */
-    if (!ret) {
-        ret = pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
-    }
-
-    return ret;
+    return pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
  }
 
  /*
#5 lore raw

PATCH v3 1/1 target/riscv: Smepmp: Return error when access permission not allowed in PMP

In reply to Himanshu Chauhan

On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.

v3 Changes:
o Removed explicit return of boolean value from comparision of priv/allowed_priv

v2 Changes:
o Removed goto to return in place when address matches
o Call pmp_hart_has_privs_default at the end of the loop

Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")
---
 target/riscv/pmp.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
target/riscv/pmp.c +2 -8
index 418738afd8..9d8db493e6 100644
@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                         pmp_priv_t *allowed_privs, target_ulong mode)
 {
     int i = 0;
-    bool ret = false;
     int pmp_size = 0;
     target_ulong s = 0;
     target_ulong e = 0;
@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
              * defined with PMP must be used. We shouldn't fallback on
              * finding default privileges.
              */
-            ret = true;
-            break;
+            return (privs & *allowed_privs) == privs;
         }
     }
 
     /* No rule matched */
-    if (!ret) {
-        ret = pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
-    }
-
-    return ret;
+    return pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
 }
 
 /*
Signature · 1 line
2.34.1
Reviewed-by on v3 1/1
#6 lore raw
Quoted 7 lines · Himanshu Chauhan

On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.

v3 Changes:
o Removed explicit return of boolean value from comparision of priv/allowed_priv

Quoted 7 lines

v2 Changes:
o Removed goto to return in place when address matches
o Call pmp_hart_has_privs_default at the end of the loop

Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")

---

Weiwei Li

Quoted 35 lines
  target/riscv/pmp.c | 10 ++--------
  1 file changed, 2 insertions(+), 8 deletions(-)
target/riscv/pmp.c +2 -8
index 418738afd8..9d8db493e6 100644
@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                          pmp_priv_t *allowed_privs, target_ulong mode)
  {
      int i = 0;
-    bool ret = false;
      int pmp_size = 0;
      target_ulong s = 0;
      target_ulong e = 0;
@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
               * defined with PMP must be used. We shouldn't fallback on
               * finding default privileges.
               */
-            ret = true;
-            break;
+            return (privs & *allowed_privs) == privs;
          }
      }
 
      /* No rule matched */
-    if (!ret) {
-        ret = pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
-    }
-
-    return ret;
+    return pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
  }
 
  /*
Reviewed-by on v3 1/1
#7 lore raw
In reply to Himanshu Chauhan
Quoted 7 lines · Himanshu Chauhan

On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.

v3 Changes:
o Removed explicit return of boolean value from comparision of priv/allowed_priv

Quoted 7 lines

v2 Changes:
o Removed goto to return in place when address matches
o Call pmp_hart_has_privs_default at the end of the loop

Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")

---

Quoted 35 lines
  target/riscv/pmp.c | 10 ++--------
  1 file changed, 2 insertions(+), 8 deletions(-)
target/riscv/pmp.c +2 -8
index 418738afd8..9d8db493e6 100644
@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                          pmp_priv_t *allowed_privs, target_ulong mode)
  {
      int i = 0;
-    bool ret = false;
      int pmp_size = 0;
      target_ulong s = 0;
      target_ulong e = 0;
@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
               * defined with PMP must be used. We shouldn't fallback on
               * finding default privileges.
               */
-            ret = true;
-            break;
+            return (privs & *allowed_privs) == privs;
          }
      }
 
      /* No rule matched */
-    if (!ret) {
-        ret = pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
-    }
-
-    return ret;
+    return pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
  }
 
  /*
#8 lore raw
In reply to Himanshu Chauhan
Quoted 8 lines · Himanshu Chauhan

On an address match, skip checking for default permissions and return error based on access defined in PMP configuration.

v3 Changes:
o Removed explicit return of boolean value from comparision of priv/allowed_priv

Quoted 6 lines

v2 Changes:
o Removed goto to return in place when address matches
o Call pmp_hart_has_privs_default at the end of the loop

Fixes 90b1fafce06 ("target/riscv: Smepmp: Skip applying default rules when address matches")

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 40 lines
---
 target/riscv/pmp.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
target/riscv/pmp.c +2 -9
index 418738afd8..9d8db493e6 100644
@@ -291,7 +291,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                         pmp_priv_t *allowed_privs, target_ulong mode)
 {
     int i = 0;
-    bool ret = false;
     int pmp_size = 0;
     target_ulong s = 0;
     target_ulong e = 0;
@@ -435,17 +434,12 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
              * defined with PMP must be used. We shouldn't fallback on
              * finding default privileges.
              */
-            ret = true;
-            break;
+            return (privs & *allowed_privs) == privs;
         }
     }
 
     /* No rule matched */
-    if (!ret) {
-        ret = pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
-    }
-
-    return ret;
+    return pmp_hart_has_privs_default(env, privs, allowed_privs, mode);
 }
 
 /*
--

2.34.1