Skip to contentSkip to conversation

RISC-V: Fix pmpcfg register indexing

PATCH In review Luke Nelson 3 participants 4 messages Jan 30, 2019 – Jun 29, 2019 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RISC-V: Fix pmpcfg register indexing

pmpcfg_csr_{read,write} do not correctly handle accesses to PMP configurations 8 through 15 (CSR pmpcfg2) on RV64.

The current code computes the pmpcfg index using:

(reg_index * sizeof(target_ulong))

This is incorrect on RV64. For example, when reg_index is 2 (i.e., pmpcfg2), the computed configuration index will be 16-23, which should be 8-15.

A correct way is to use (reg_index * 4) instead, which works for both RV32 and RV64.

---
 target/riscv/pmp.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target/riscv/pmp.c +3 -4
index 15a5366616..a1bee56c86 100644
@@ -311,9 +311,8 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
     }
 
     for (i = 0; i < sizeof(target_ulong); i++) {
-        cfg_val = (val >> 8 * i)  & 0xff;
-        pmp_write_cfg(env, (reg_index * sizeof(target_ulong)) + i,
-            cfg_val);
+        cfg_val = (val >> (i * 8)) & 0xff;
+        pmp_write_cfg(env, (reg_index * 4) + i, cfg_val);
     }
 }
 
@@ -328,7 +327,7 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)
     target_ulong val = 0;
 
     for (i = 0; i < sizeof(target_ulong); i++) {
-        val = pmp_read_cfg(env, (reg_index * sizeof(target_ulong)) + i);
+        val = pmp_read_cfg(env, (reg_index * 4) + i);
         cfg_val |= (val << (i * 8));
     }
 
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Luke Nelson

pmpcfg_csr_{read,write} do not correctly handle accesses to PMP configurations 8 through 15 (CSR pmpcfg2) on RV64.

The current code computes the pmpcfg index using:

(reg_index * sizeof(target_ulong))

This is incorrect on RV64. For example, when reg_index is 2 (i.e., pmpcfg2), the computed configuration index will be 16-23, which should be 8-15.

Quoted 5 lines

A correct way is to use (reg_index * 4) instead, which works for both RV32 and RV64.

Good catch!

Alistair

Quoted 33 lines
---
 target/riscv/pmp.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target/riscv/pmp.c +3 -5
index 15a5366616..a1bee56c86 100644
@@ -311,9 +311,8 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
     }
 
     for (i = 0; i < sizeof(target_ulong); i++) {
-        cfg_val = (val >> 8 * i)  & 0xff;
-        pmp_write_cfg(env, (reg_index * sizeof(target_ulong)) + i,
-            cfg_val);
+        cfg_val = (val >> (i * 8)) & 0xff;
+        pmp_write_cfg(env, (reg_index * 4) + i, cfg_val);
     }
 }
 
@@ -328,7 +327,7 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)
     target_ulong val = 0;
 
     for (i = 0; i < sizeof(target_ulong); i++) {
-        val = pmp_read_cfg(env, (reg_index * sizeof(target_ulong)) + i);
+        val = pmp_read_cfg(env, (reg_index * 4) + i);
         cfg_val |= (val << (i * 8));
     }
 
--

2.19.1

#2 lore raw
Quoted 16 lines · On Fri, 08 Feb 2019 10:57:17 PST (-0800), [email protected] wrote:
Quoted 15 lines · Luke Nelson

pmpcfg_csr_{read,write} do not correctly handle accesses to PMP configurations 8 through 15 (CSR pmpcfg2) on RV64.

The current code computes the pmpcfg index using:

(reg_index * sizeof(target_ulong))

This is incorrect on RV64. For example, when reg_index is 2 (i.e., pmpcfg2), the computed configuration index will be 16-23, which should be 8-15.

A correct way is to use (reg_index * 4) instead, which works for both RV32 and RV64.

Quoted 6 lines
Quoted 2 lines

Good catch!

Ya, thanks -- that's a somewhat embarrassing bug, as someone else just fixed one on the line below :). I'll target this for my next PR.

Quoted 36 lines

Alistair

Quoted 33 lines
---
 target/riscv/pmp.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target/riscv/pmp.c +3 -5
index 15a5366616..a1bee56c86 100644
@@ -311,9 +311,8 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
     }
 
     for (i = 0; i < sizeof(target_ulong); i++) {
-        cfg_val = (val >> 8 * i)  & 0xff;
-        pmp_write_cfg(env, (reg_index * sizeof(target_ulong)) + i,
-            cfg_val);
+        cfg_val = (val >> (i * 8)) & 0xff;
+        pmp_write_cfg(env, (reg_index * 4) + i, cfg_val);
     }
 }
 
@@ -328,7 +327,7 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)
     target_ulong val = 0;
 
     for (i = 0; i < sizeof(target_ulong); i++) {
-        val = pmp_read_cfg(env, (reg_index * sizeof(target_ulong)) + i);
+        val = pmp_read_cfg(env, (reg_index * 4) + i);
         cfg_val |= (val << (i * 8));
     }
 
--

2.19.1

#3 lore raw
Quoted 10 lines · Palmer Dabbelt
Quoted 4 lines · On Fri, 08 Feb 2019 10:57:17 PST (-0800), [email protected] wrote:

Good catch!

Ya, thanks -- that's a somewhat embarrassing bug, as someone else just fixed one on the line below :). I'll target this for my next PR.

Is there any chance this patch could make it in the next PR?

Thanks,
- Luke