Skip to contentSkip to conversation

target/riscv: Add User CSRs read-only check

PATCH Superseded LIU Zhiwei 2 participants 2 messages Aug 9, 2021 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

LIU Zhiwei

author
#0 lore raw

PATCH 1/1 target/riscv: Add User CSRs read-only check

For U-mode CSRs, read-only check is also needed.

---
 target/riscv/csr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
target/riscv/csr.c +5 -3
index 9a4ed18ac5..360e4bfa33 100644
@@ -1422,11 +1422,11 @@ RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
     RISCVException ret;
     target_ulong old_value;
     RISCVCPU *cpu = env_archcpu(env);
+    int read_only = get_field(csrno, 0xC00) == 3;
 
     /* check privileges and return -1 if check fails */
 #if !defined(CONFIG_USER_ONLY)
     int effective_priv = env->priv;
-    int read_only = get_field(csrno, 0xC00) == 3;
 
     if (riscv_has_ext(env, RVH) &&
         env->priv == PRV_S &&
@@ -1439,11 +1439,13 @@ RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
         effective_priv++;
     }
 
-    if ((write_mask && read_only) ||
-        (!env->debugger && (effective_priv < get_field(csrno, 0x300)))) {
+    if (!env->debugger && (effective_priv < get_field(csrno, 0x300))) {
         return RISCV_EXCP_ILLEGAL_INST;
     }
 #endif
+    if (write_mask && read_only) {
+        return -RISCV_EXCP_ILLEGAL_INST;
+    }
 
     /* ensure the CSR extension is enabled. */
     if (!cpu->cfg.ext_icsr) {
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · LIU Zhiwei

For U-mode CSRs, read-only check is also needed.

---
 target/riscv/csr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
target/riscv/csr.c +1 -1
index 9a4ed18ac5..360e4bfa33 100644
@@ -1422,11 +1422,11 @@ RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
     RISCVException ret;
     target_ulong old_value;
     RISCVCPU *cpu = env_archcpu(env);
+    int read_only = get_field(csrno, 0xC00) == 3;
 
     /* check privileges and return -1 if check fails */
 #if !defined(CONFIG_USER_ONLY)
     int effective_priv = env->priv;
-    int read_only = get_field(csrno, 0xC00) == 3;
 
     if (riscv_has_ext(env, RVH) &&
         env->priv == PRV_S &&
@@ -1439,11 +1439,13 @@ RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
         effective_priv++;

target/riscv/csr.c RISCVException riscv_csrrw(CPURISCVState *env, int csrno, · line 1447

Quoted 10 lines
target/riscv/csr.c +3 -2
     }
 
-    if ((write_mask && read_only) ||
-        (!env->debugger && (effective_priv < get_field(csrno, 0x300)))) {
+    if (!env->debugger && (effective_priv < get_field(csrno, 0x300))) {
         return RISCV_EXCP_ILLEGAL_INST;
     }
 #endif
+    if (write_mask && read_only) {
+        return -RISCV_EXCP_ILLEGAL_INST;

This should be RISCV_EXCP_ILLEGAL_INST

target/riscv/csr.c RISCVException riscv_csrrw(CPURISCVState *env, int csrno, · line 1451

Quoted 4 lines
target/riscv/csr.c +1 -0
+    }
 
     /* ensure the CSR extension is enabled. */
     if (!cpu->cfg.ext_icsr) {

Otherwise,