Skip to contentSkip to conversation

target/riscv: fix csr check for cycle{h}, instret{h}, time{h}, hpmcounter3-31{h}

PATCH Superseded Weiwei Li 2 participants 2 messages Aug 17, 2022 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Weiwei Li

author
#0 lore raw

PATCH v2 1/1 target/riscv: fix csr check for cycle{h}, instret{h}, time{h}, hpmcounter3-31{h}

- modify check for mcounteren to work in all less-privilege mode
- modify check for scounteren to work only when S mode is enabled
- distinguish the exception type raised by check for scounteren between U and VU mode

---
v2:
- Rebase on patches v13 for "Improve PMU support"

 target/riscv/csr.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
target/riscv/csr.c +9 -4
index 2dcd4e5b2d..a6bf2ff964 100644
@@ -98,17 +98,22 @@ static RISCVException ctr(CPURISCVState *env, int csrno)
 
 skip_ext_pmu_check:
 
-    if (((env->priv == PRV_S) && (!get_field(env->mcounteren, ctr_mask))) ||
-        ((env->priv == PRV_U) && (!get_field(env->scounteren, ctr_mask)))) {
+    if ((env->priv < PRV_M) && (!get_field(env->mcounteren, ctr_mask))) {
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
     if (riscv_cpu_virt_enabled(env)) {
-        if (!get_field(env->hcounteren, ctr_mask) &&
-            get_field(env->mcounteren, ctr_mask)) {
+        if (!get_field(env->hcounteren, ctr_mask) ||
+            ((env->priv == PRV_U) && !get_field(env->scounteren, ctr_mask))) {
             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
     }
+
+    if (riscv_has_ext(env, RVS) && (env->priv == PRV_U) &&
+        !get_field(env->scounteren, ctr_mask)) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
 #endif
     return RISCV_EXCP_NONE;
 }
Signature · 1 line
2.17.1
#1 lore raw
Quoted 19 lines · Weiwei Li

- modify check for mcounteren to work in all less-privilege mode
- modify check for scounteren to work only when S mode is enabled
- distinguish the exception type raised by check for scounteren between U and VU mode

---
v2:
- Rebase on patches v13 for "Improve PMU support"

 target/riscv/csr.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
target/riscv/csr.c +0 -0
index 2dcd4e5b2d..a6bf2ff964 100644

target/riscv/csr.c static RISCVException ctr(CPURISCVState *env, int csrno) · line 101

Quoted 6 lines
target/riscv/csr.c +1 -2
@@ -98,17 +98,22 @@ static RISCVException ctr(CPURISCVState *env, int csrno)
 
 skip_ext_pmu_check:
 
-    if (((env->priv == PRV_S) && (!get_field(env->mcounteren, ctr_mask))) ||
-        ((env->priv == PRV_U) && (!get_field(env->scounteren, ctr_mask)))) {
+    if ((env->priv < PRV_M) && (!get_field(env->mcounteren, ctr_mask))) {

I'd drop the unnecessary ()'s on both sides of the &&

target/riscv/csr.c static RISCVException ctr(CPURISCVState *env, int csrno) · line 107

Quoted 8 lines
target/riscv/csr.c +2 -2
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
     if (riscv_cpu_virt_enabled(env)) {
-        if (!get_field(env->hcounteren, ctr_mask) &&
-            get_field(env->mcounteren, ctr_mask)) {
+        if (!get_field(env->hcounteren, ctr_mask) ||
+            ((env->priv == PRV_U) && !get_field(env->scounteren, ctr_mask))) {

() around env->priv == PRV_U can be dropped too

target/riscv/csr.c static RISCVException ctr(CPURISCVState *env, int csrno) · line 112

Quoted 5 lines
target/riscv/csr.c +2 -0
             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
     }
+
+    if (riscv_has_ext(env, RVS) && (env->priv == PRV_U) &&

And here. Hmm, it seems a lot of env->priv checks in this file have unnecessary ()'s. Oh well, I like lisp.

Thanks,
drew

Quoted 11 lines
target/riscv/csr.c +4 -0
+        !get_field(env->scounteren, ctr_mask)) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
 #endif
     return RISCV_EXCP_NONE;
 }
Signature · 1 line
2.17.1