Skip to contentSkip to conversation

target/riscv: Smepmp: Skip applying default rules when address matches

PATCH Applied Himanshu Chauhan 4 participants 7 messages Feb 9, 2023 – Feb 13, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: Smepmp: Skip applying default rules when address matches

When MSECCFG.MML is set, after checking the address range in PMP if the asked permissions are not same as programmed in PMP, the default permissions are applied. This should only be the case when there is no matching address is found.

This patch skips applying default rules when matching address range is found. It returns the index of the match PMP entry.

Fixes 824cac681c3 (target/riscv: Fix PMP propagation for tlb)
---
 target/riscv/pmp.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
target/riscv/pmp.c +6 -3
index d85ad07caa..0dfdb35828 100644
@@ -446,9 +446,12 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                 }
             }
 
-            if ((privs & *allowed_privs) == privs) {
-                ret = i;
-            }
+            /*
+             * If matching address range was found, the protection bits
+             * defined with PMP must be used. We shouldn't fallback on
+             * finding default privileges.
+             */
+            ret = i;
             break;
         }
     }
Signature · 1 line
2.39.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Himanshu Chauhan

When MSECCFG.MML is set, after checking the address range in PMP if the asked permissions are not same as programmed in PMP, the default permissions are applied. This should only be the case when there is no matching address is found.

This patch skips applying default rules when matching address range is found. It returns the index of the match PMP entry.

Fixes 824cac681c3 (target/riscv: Fix PMP propagation for tlb)

Nit: tag starts with capital "F":

Fixes 824cac681c3 (target/riscv: Fix PMP propagation for tlb)
Quoted 3 lines

---

Quoted 23 lines
  target/riscv/pmp.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
target/riscv/pmp.c +6 -3
index d85ad07caa..0dfdb35828 100644
@@ -446,9 +446,12 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                  }
              }
 
-            if ((privs & *allowed_privs) == privs) {
-                ret = i;
-            }
+            /*
+             * If matching address range was found, the protection bits
+             * defined with PMP must be used. We shouldn't fallback on
+             * finding default privileges.
+             */
+            ret = i;
              break;
          }
      }
Reviewed-by on v1 1/1
#2 lore raw
In reply to Himanshu Chauhan
Quoted 6 lines · Himanshu Chauhan

When MSECCFG.MML is set, after checking the address range in PMP if the asked permissions are not same as programmed in PMP, the default permissions are applied. This should only be the case when there is no matching address is found.

Quoted 6 lines

This patch skips applying default rules when matching address range is found. It returns the index of the match PMP entry.

Fixes 824cac681c3 (target/riscv: Fix PMP propagation for tlb)

Alistair

Quoted 28 lines
---
 target/riscv/pmp.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
target/riscv/pmp.c +6 -4
index d85ad07caa..0dfdb35828 100644
@@ -446,9 +446,12 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                 }
             }
 
-            if ((privs & *allowed_privs) == privs) {
-                ret = i;
-            }
+            /*
+             * If matching address range was found, the protection bits
+             * defined with PMP must be used. We shouldn't fallback on
+             * finding default privileges.
+             */
+            ret = i;
             break;
         }
     }
--

2.39.1

#3 lore raw
In reply to Himanshu Chauhan
Quoted 6 lines · Himanshu Chauhan

When MSECCFG.MML is set, after checking the address range in PMP if the asked permissions are not same as programmed in PMP, the default permissions are applied. This should only be the case when there is no matching address is found.

Quoted 6 lines

This patch skips applying default rules when matching address range is found. It returns the index of the match PMP entry.

Fixes 824cac681c3 (target/riscv: Fix PMP propagation for tlb)

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 28 lines
---
 target/riscv/pmp.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
target/riscv/pmp.c +6 -4
index d85ad07caa..0dfdb35828 100644
@@ -446,9 +446,12 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                 }
             }
 
-            if ((privs & *allowed_privs) == privs) {
-                ret = i;
-            }
+            /*
+             * If matching address range was found, the protection bits
+             * defined with PMP must be used. We shouldn't fallback on
+             * finding default privileges.
+             */
+            ret = i;
             break;
         }
     }
--

2.39.1

#4 lore raw
In reply to Himanshu Chauhan
Quoted 22 lines · Himanshu Chauhan

When MSECCFG.MML is set, after checking the address range in PMP if the asked permissions are not same as programmed in PMP, the default permissions are applied. This should only be the case when there is no matching address is found.

This patch skips applying default rules when matching address range is found. It returns the index of the match PMP entry.

Fixes 824cac681c3 (target/riscv: Fix PMP propagation for tlb)
---
  target/riscv/pmp.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
target/riscv/pmp.c +0 -0
index d85ad07caa..0dfdb35828 100644
@@ -446,9 +446,12 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
                  }
              }

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

Quoted 10 lines
target/riscv/pmp.c +6 -3
 
-            if ((privs & *allowed_privs) == privs) {
-                ret = i;
-            }
+            /*
+             * If matching address range was found, the protection bits
+             * defined with PMP must be used. We shouldn't fallback on
+             * finding default privileges.
+             */
+            ret = i;

Notice the return value is the matching rule index, which includes

1) the address range is matching.

2) the permission of the PMP rule and the memory access type are matching.

So we can't simply remove the second check.  I think the right fix is:

           if ((privs & *allowed_privs) == privs) {
                ret = i;

- }
+ } else {
+ ret = -2;
+ }

The -2 return value avoids finding the default privileges. And it implies no matching rule is found.

Zhiwei

Quoted 3 lines
target/riscv/pmp.c +0 -0
              break;
          }
      }
#5 lore raw
Quoted 51 lines · LIU Zhiwei
Quoted 22 lines · Himanshu Chauhan

When MSECCFG.MML is set, after checking the address range in PMP if the asked permissions are not same as programmed in PMP, the default permissions are applied. This should only be the case when there is no matching address is found.

This patch skips applying default rules when matching address range is found. It returns the index of the match PMP entry.

Fixes 824cac681c3 (target/riscv: Fix PMP propagation for tlb)

---
  target/riscv/pmp.c | 9 ++++++---

  1 file changed, 6 insertions(+), 3 deletions(-)
target/riscv/pmp.c +0 -0
index d85ad07caa..0dfdb35828 100644
@@ -446,9 +446,12 @@ int pmp_hart_has_privs(CPURISCVState *env, 
 target_ulong addr,
                   }

target/riscv/pmp.c int pmp_hart_has_privs(CPURISCVState *env, · line 455

Quoted 10 lines
target/riscv/pmp.c +6 -2
               }
   -            if ((privs & *allowed_privs) == privs) {
-                ret = i;
-            }
+            /*
+             * If matching address range was found, the protection bits
+             * defined with PMP must be used. We shouldn't fallback on
+             * finding default privileges.
+             */
+            ret = i;

Notice the return value is the matching rule index, which includes

1) the address range is matching.

2) the permission of the PMP rule and the memory access type are matching.

So we can't simply remove the second check.  I think the right fix is:

           if ((privs & *allowed_privs) == privs) { ret = i;
 -         }
 +         } else {
 +        ret = -2;
 +         }

Quoted 3 lines

The -2 return value avoids finding the default privileges. And it implies no matching rule is found.

Zhiwei

Hi Zhiwei,

In case the address range is matched and MSECCFG.MML is set, the permission in *allowed_privs* are binding. So if the index matching is returned, the binding permissions are applied by the caller function.

Which case does my patch break?

- Himanshu

Quoted 4 lines
Quoted 4 lines
target/riscv/pmp.c +0 -0
               break;
           }

      }

#6 lore raw
Quoted 54 lines · Himanshu Chauhan
Quoted 52 lines · LIU Zhiwei
Quoted 28 lines · Himanshu Chauhan

When MSECCFG.MML is set, after checking the address range in PMP if the asked permissions are not same as programmed in PMP, the default permissions are applied. This should only be the case when there is no matching address is found.

This patch skips applying default rules when matching address range is found. It returns the index of the match PMP entry.

Fixes 824cac681c3 (target/riscv: Fix PMP propagation for tlb)

---
  target/riscv/pmp.c | 9 ++++++---

  1 file changed, 6 insertions(+), 3 deletions(-)
target/riscv/pmp.c +2 -2
index d85ad07caa..0dfdb35828 100644
@@ -446,9 +446,12 @@ int pmp_hart_has_privs(CPURISCVState *env, 
 target_ulong addr,
                   }
               }
   -            if ((privs & *allowed_privs) == privs) {
-                ret = i;
-            }
+            /*
+             * If matching address range was found, the protection 
Quoted 5 lines

bits
+             * defined with PMP must be used. We shouldn't fallback on +             * finding default privileges.
+             */
+            ret = i;

Notice the return value is the matching rule index, which includes

1) the address range is matching.

2) the permission of the PMP rule and the memory access type are matching.

So we can't simply remove the second check.  I think the right fix is:

           if ((privs & *allowed_privs) == privs) { ret = i;
 -         }
 +         } else {
 +        ret = -2;
 +         }

Quoted 7 lines
Quoted 3 lines

The -2 return value avoids finding the default privileges. And it implies no matching rule is found.

Zhiwei

Hi Zhiwei,

In case the address range is matched and MSECCFG.MML is set, the permission in *allowed_privs* are binding.

Yes.

Quoted 1 line

So if the index matching is returned, the binding permissions are applied by the caller function.

Yes. And the index will also be used. So we should check both conditions in this function.

Quoted 2 lines

Which case does my patch break?

Look at the get_physical_address_pmp which calls pmp_hart_has_privs,

    pmp_index = pmp_hart_has_privs(env, addr, size, 1 << access_type,

                                   &pmp_priv, mode);

    if (pmp_index < 0) {

        *prot = 0;

        return TRANSLATE_PMP_FAIL;

    }

    *prot = pmp_priv_to_page_prot(pmp_priv);

    if ((tlb_size != NULL) && pmp_index != MAX_RISCV_PMPS) {

        target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1);

        target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1;

        *tlb_size = pmp_get_tlb_size(env, pmp_index, tlb_sa, tlb_ea);

    }

returnTRANSLATE_SUCCESS;

You break the pmp_index < 0 condition.  If ((privs & *allowed_privs) != privs,  the get_physical_address_pmp should return fail.

Zhiwei

Quoted 7 lines

- Himanshu

Quoted 4 lines
Quoted 4 lines

              break;
          }
      }