Skip to contentSkip to conversation

target/riscv: fix scountinhibit clearing non-S-mode mcountinhibit bits

PATCH New Juliet Mathis 1 participant 1 message Dec 1 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: fix scountinhibit clearing non-S-mode mcountinhibit bits

According to the Smcdeleg specification, scountinhibit is a masked alias of mcountinhibit. mcountinhibit includes bits for counters delegated to S-mode and counters that are not. A write to scountinhibit should not change the mcountinhibit bits of counters that are not delegated to S-mode.

write_scountinhibit() calls write_mcountinhibit() with the intersection of the value being written and the env->mcounteren, which clears the mcountinhibit bits that are not delegated to S-mode.

Merge the bits that are not delegated with the bits that are delegated from the value being written. This preserves the mcountinhibit bits that are not delegated to S-mode.

Fixes 6247dc2ef70b ("target/riscv: Add counter delegation/configuration support")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4831

---
 target/riscv/tcg/csr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/riscv/tcg/csr.c +4 -1
index af444e3e6f..da2bd767c8 100644
@@ -3164,7 +3164,10 @@ static RISCVException read_scountinhibit(CPURISCVState *env, int csrno,
 static RISCVException write_scountinhibit(CPURISCVState *env, int csrno,
                                           target_ulong val, uintptr_t ra)
 {
-    return write_mcountinhibit(env, csrno, val & env->mcounteren, ra);
+    target_ulong inhibit = (env->mcountinhibit & ~env->mcounteren) |
+                           (val & env->mcounteren);
+
+    return write_mcountinhibit(env, csrno, inhibit, ra);
 }
 
 static RISCVException read_mcounteren(CPURISCVState *env, int csrno,
Signature · 1 line
2.56.1.windows.1