Skip to contentSkip to conversation

target/riscv: fix counter-enable checks in ctr()

PATCH In review Xi Wang 2 participants 2 messages Jan 26, 2019 – Jan 29, 2019 RISC-V

lore
Share & tools
lore

Xi Wang

author
#0 lore raw

PATCH 1/1 target/riscv: fix counter-enable checks in ctr()

Access to a counter in U-mode is permitted only if the corresponding bit is set in both mcounteren and scounteren. The current code ignores mcounteren and checks scounteren only for U-mode access.

---
 target/riscv/csr.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
target/riscv/csr.c +9 -3
index 5e7e7d16b8..133f2ff7e2 100644
@@ -56,9 +56,15 @@ static int fs(CPURISCVState *env, int csrno)
 static int ctr(CPURISCVState *env, int csrno)
 {
 #if !defined(CONFIG_USER_ONLY)
-    target_ulong ctr_en = env->priv == PRV_U ? env->scounteren :
-                          env->priv == PRV_S ? env->mcounteren : -1U;
-    if (!(ctr_en & (1 << (csrno & 31)))) {
+    uint32_t ctr_en = ~0u;
+
+    if (env->priv < PRV_M) {
+        ctr_en &= env->mcounteren;
+    }
+    if (env->priv < PRV_S) {
+        ctr_en &= env->scounteren;
+    }
+    if (!(ctr_en & (1u << (csrno & 31)))) {
         return -1;
     }
 #endif
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · On Sat, 26 Jan 2019 15:02:56 PST (-0800), [email protected] wrote:

Access to a counter in U-mode is permitted only if the corresponding bit is set in both mcounteren and scounteren. The current code ignores mcounteren and checks scounteren only for U-mode access.

---
 target/riscv/csr.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
target/riscv/csr.c +2 -3
index 5e7e7d16b8..133f2ff7e2 100644
@@ -56,9 +56,15 @@ static int fs(CPURISCVState *env, int csrno)
 static int ctr(CPURISCVState *env, int csrno)
 {
 #if !defined(CONFIG_USER_ONLY)
-    target_ulong ctr_en = env->priv == PRV_U ? env->scounteren :
-                          env->priv == PRV_S ? env->mcounteren : -1U;
-    if (!(ctr_en & (1 << (csrno & 31)))) {
+    uint32_t ctr_en = ~0u;
+

target/riscv/csr.c static int fs(CPURISCVState *env, int csrno) · line 70

Quoted 10 lines
target/riscv/csr.c +7 -0
+    if (env->priv < PRV_M) {
+        ctr_en &= env->mcounteren;
+    }
+    if (env->priv < PRV_S) {
+        ctr_en &= env->scounteren;
+    }
+    if (!(ctr_en & (1u << (csrno & 31)))) {
         return -1;
     }
 #endif

I'll add this to my next PR.