Skip to contentSkip to conversation

target/riscv: Fix PMP address alignment

PATCH Superseded Jay Chang 4 participants 7 messages May 18 RISC-V

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jay Chang

author
#0 lore raw

v4 cover target/riscv: Fix PMP address alignment

This series fixes PMP address alignment to comply with the RISC-V specification WARL (Write-Any, Read-Legal) semantics.

When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it needs to be aligned to the PMP granularity.

Changes in v4:
- Rebase riscv-to-apply.next

Changes in v3:
- Use ROUND_UP() macro for NAPOT address alignment

Changes in v2:
- Fixed commit message grammar ("it needs to be aligned")
- Use ROUND_DOWN() macro for TOR address alignment to improve code readability

Jay Chang (2):
target/riscv: Align pmp size to pmp-granularity target/riscv: Use macros for PMP address alignment

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

PATCH v4 1/2 target/riscv: Align pmp size to pmp-granularity

When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it needs to be aligned to the PMP granularity.

---
 target/riscv/pmp.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
target/riscv/pmp.c +8 -2
index a71091a316..de2157c830 100644
@@ -179,11 +179,12 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
             }
             /*
              * When granularity g >= 1 (i.e., granularity > 4 bytes),
-             * the NA4 (Naturally Aligned 4-byte) mode is not selectable
+             * the NA4 (Naturally Aligned 4-byte) mode is not selectable.
+             * In this case, an NA4 setting is reinterpreted as a NAPOT mode.
              */
             if ((riscv_cpu_cfg(env)->pmp_granularity >
                 MIN_RISCV_PMP_GRANULARITY) && (a_field == PMP_AMATCH_NA4)) {
-                    return false;
+                    val |= PMP_AMATCH;
             }
             env->pmp_state.pmp[pmp_index].cfg_reg = val;
             pmp_update_rule_addr(env, pmp_index);
@@ -263,6 +264,11 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
         break;
 
     case PMP_AMATCH_NAPOT:
+        /* Bits [g-2:0] need to be all one to align pmp granularity */
+        if (g >= 2) {
+            this_addr |= ((1ULL << (g - 1ULL)) - 1ULL);
+        }
+
         pmp_decode_napot(this_addr, &sa, &ea);
         break;
 
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH v4 1/2] target/riscv: Align pmp size to pmp-granularity

Hi,

Quoted 13 lines · Jay Chang

When configuring pmpcfg (TOR, NA4, or NAPOT) and pmpaddr, if the value is smaller than the PMP granularity, it needs to be aligned to the PMP granularity.

---
  target/riscv/pmp.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)

target/riscv/pmp.c void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index) · line 269

Quoted 6 lines
target/riscv/pmp.c +3 -0
@@ -263,6 +264,11 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
          break;
 
      case PMP_AMATCH_NAPOT:
+        /* Bits [g-2:0] need to be all one to align pmp granularity */
+        if (g >= 2) {
+            this_addr |= ((1ULL << (g - 1ULL)) - 1ULL);

deposit64() could be easier to read here.

Quoted 5 lines
target/riscv/pmp.c +2 -0
+        }
+
          pmp_decode_napot(this_addr, &sa, &ea);
          break;
 
author
#3 lore raw

PATCH v4 2/2 target/riscv: Use macros for PMP address alignment

In reply to Jay Chang

Replace manual bit manipulation with alignment macros for better readability:

- TOR: Use ROUND_DOWN() to clear lower bits
- NAPOT: Use ROUND_UP() to set lower bits

The behavior remains unchanged.

---
 target/riscv/pmp.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
target/riscv/pmp.c +9 -5
index de2157c830..238c7c5162 100644
@@ -247,8 +247,9 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
     case PMP_AMATCH_TOR:
         /* Bits pmpaddr[G-1:0] do not affect the TOR address-matching logic. */
         if (g >= 1) {
-            prev_addr &= ~((1ULL << g) - 1ULL);
-            this_addr &= ~((1ULL << g) - 1ULL);
+            target_ulong granule = 1ULL << g;
+            prev_addr = ROUND_DOWN(prev_addr, granule);
+            this_addr = ROUND_DOWN(this_addr, granule);
         }
         if (prev_addr >= this_addr) {
             sa = ea = 0u;
@@ -266,7 +267,8 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
     case PMP_AMATCH_NAPOT:
         /* Bits [g-2:0] need to be all one to align pmp granularity */
         if (g >= 2) {
-            this_addr |= ((1ULL << (g - 1ULL)) - 1ULL);
+            target_ulong granule = 1ULL << (g - 1);
+            this_addr = ROUND_UP(this_addr + 1ULL, granule) - 1ULL;
         }
 
         pmp_decode_napot(this_addr, &sa, &ea);
@@ -641,13 +643,15 @@ target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
         case PMP_AMATCH_TOR:
             /* Bit [g-1:0] read all zero */
             if (g >= 1 && g < TARGET_LONG_BITS) {
-                val &= ~((1ULL << g) - 1ULL);
+                target_ulong granule = 1ULL << g;
+                val = ROUND_DOWN(val, granule);
             }
             break;
         case PMP_AMATCH_NAPOT:
             /* Bit [g-2:0] read all one */
             if (g >= 2 && g < TARGET_LONG_BITS) {
-                val |= ((1ULL << (g - 1)) - 1ULL);
+                target_ulong granule = 1ULL << (g - 1);
+                val = ROUND_UP(val + 1ULL, granule) - 1ULL;
             }
             break;
         default:
Signature · 1 line
2.48.1
#4 lore raw

Re: [PATCH v4 2/2] target/riscv: Use macros for PMP address alignment

Quoted 18 lines · Jay Chang

Replace manual bit manipulation with alignment macros for better readability:

- TOR: Use ROUND_DOWN() to clear lower bits
- NAPOT: Use ROUND_UP() to set lower bits

The behavior remains unchanged.

---
  target/riscv/pmp.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
target/riscv/pmp.c +0 -0
index de2157c830..238c7c5162 100644

target/riscv/pmp.c void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index) · line 250

Quoted 6 lines
target/riscv/pmp.c +1 -2
@@ -247,8 +247,9 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
      case PMP_AMATCH_TOR:
          /* Bits pmpaddr[G-1:0] do not affect the TOR address-matching logic. */
          if (g >= 1) {
-            prev_addr &= ~((1ULL << g) - 1ULL);
-            this_addr &= ~((1ULL << g) - 1ULL);
+            target_ulong granule = 1ULL << g;

Maybe better to directly use uint64_t, in preparation of
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 3 lines
target/riscv/pmp.c +2 -0
+            prev_addr = ROUND_DOWN(prev_addr, granule);
+            this_addr = ROUND_DOWN(this_addr, granule);
          }
#5 lore raw

Re: [PATCH v4 2/2] target/riscv: Use macros for PMP address alignment

Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Jay Chang

Replace manual bit manipulation with alignment macros for better readability:

- TOR: Use ROUND_DOWN() to clear lower bits
- NAPOT: Use ROUND_UP() to set lower bits

The behavior remains unchanged.

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

  1 file changed, 9 insertions(+), 5 deletions(-)
Quoted 14 lines
Quoted 11 lines
target/riscv/pmp.c +0 -0
index de2157c830..238c7c5162 100644
@@ -247,8 +247,9 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)

      case PMP_AMATCH_TOR:
          /* Bits pmpaddr[G-1:0] do not affect the TOR address-matching logic. */ if (g >= 1) {
-            prev_addr &= ~((1ULL << g) - 1ULL);
-            this_addr &= ~((1ULL << g) - 1ULL);
+            target_ulong granule = 1ULL << g;

Maybe better to directly use uint64_t, in preparation of
https://lore.kernel.org/qemu-devel/[email protected]/

Can we please CC [email protected] for the next version? This might be the first time the RISC-V reviewers are made aware that this series exists. It surely is my first time :D

Cheers,
Daniel

Quoted 4 lines
Quoted 4 lines

+            prev_addr = ROUND_DOWN(prev_addr, granule); +            this_addr = ROUND_DOWN(this_addr, granule); }

#6 lore raw

Re: [PATCH v4 2/2] target/riscv: Use macros for PMP address alignment

Quoted 32 lines · Daniel Henrique Barboza
Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Jay Chang

Replace manual bit manipulation with alignment macros for better readability:

- TOR: Use ROUND_DOWN() to clear lower bits
- NAPOT: Use ROUND_UP() to set lower bits

The behavior remains unchanged.

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

  1 file changed, 9 insertions(+), 5 deletions(-)
Quoted 14 lines
Quoted 11 lines
target/riscv/pmp.c +0 -0
index de2157c830..238c7c5162 100644
@@ -247,8 +247,9 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)

      case PMP_AMATCH_TOR:
          /* Bits pmpaddr[G-1:0] do not affect the TOR address-matching logic. */ if (g >= 1) {
-            prev_addr &= ~((1ULL << g) - 1ULL);
-            this_addr &= ~((1ULL << g) - 1ULL);
+            target_ulong granule = 1ULL << g;

Maybe better to directly use uint64_t, in preparation of
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 3 lines

Can we please CC [email protected] for the next version? This might be the first time the RISC-V reviewers are made aware that this series exists. It surely is my first time :D

That's on me, will do!:)

Signature · 2 lines
Anton Johansson
rev.ng Labs Srl.