All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] target/riscv: fix VS-mode sireg access to a counter exception
@ 2026-12-06  2:55 Juliet Mathis
  0 siblings, 0 replies; only message in thread
From: Juliet Mathis @ 2026-12-06  2:55 UTC (permalink / raw)
  To: qemu-devel
  Cc: LIU Zhiwei, Weiwei Li, Daniel Henrique Barboza, Alistair Francis,
	qemu-riscv, Chao Liu, Palmer Dabbelt, Juliet Mathis,
	Samarth Sarda, Dana Coates

When counter delegation is disabled, the counter range is unimplemented.
Any access to a counter while in VS-mode should raise an illegal
instruction exception.

Any failure to access a register in VS-mode raises a virtual instruction
exception without considering the value of menvcfg.CDE.

Our fix makes the registers unavailable in their CSR predicates instead.
Accesses to vsireg* (and sireg*, which VS-mode reaches through vsireg*)
now raise an illegal instruction exception when menvcfg.CDE = 0 and the
selected index (vsiselect, or siselect outside VS-mode) is within the
counter delegation range. Additionally, had to insert a forward
declaration of xiselect_cd_range() in order to minimize the diff and
ensure functionality.

Fixes: 5e33a2082715 ("target/riscv: Support generic CSR indirect access")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4830
Signed-off-by: Samarth Sarda <samarthsarda@utexas.edu>
Signed-off-by: Dana Coates <elladana.coates@gmail.com>
Signed-off-by: Juliet Mathis <julietmathis2004@gmail.com>
---
v2: moved the check from rmw_xireg_csrind() into the csrind predicates
and covered sireg* too, per Daniel's review.

 target/riscv/tcg/csr.c | 43 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..cce7405a59 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -431,21 +431,44 @@ static bool csrind_or_aia_extensions_present(CPURISCVState *env)
     return csrind_extensions_present(env) || aia_extensions_present(env);
 }
 
+/* Forward declaration to minimize diff */
+static bool xiselect_cd_range(uint16_t isel);
+
 static RISCVException csrind_smode(CPURISCVState *env, int csrno)
 {
+    target_ulong isel;
+
     if (!csrind_extensions_present(env)) {
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /* In VS-mode a sireg* access is redirected to vsireg* via vsiselect */
+    isel = env->virt_enabled ? env->vsiselect : env->siselect;
+    if (xiselect_cd_range(isel) && !get_field(env->menvcfg, MENVCFG_CDE)) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
     return smode(env, csrno);
 }
 
 static RISCVException csrind_or_aia_smode(CPURISCVState *env, int csrno)
 {
+    target_ulong isel;
+
     if (!csrind_or_aia_extensions_present(env)) {
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /* siselect is shared with this predicate: only sireg is affected. */
+    if (csrno == CSR_SIREG) {
+        /* In VS-mode a sireg access is redirected to vsireg via vsiselect */
+        isel = env->virt_enabled ? env->vsiselect : env->siselect;
+        if (xiselect_cd_range(isel) &&
+            !get_field(env->menvcfg, MENVCFG_CDE)) {
+            return RISCV_EXCP_ILLEGAL_INST;
+        }
+    }
+
     return smode(env, csrno);
 }
 
@@ -474,6 +497,15 @@ static RISCVException csrind_hmode(CPURISCVState *env, int csrno)
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /*
+     * menvcfg.CDE = 0 makes the counter range unimplemented, so a
+     * vsireg* access selecting it is an illegal instruction.
+     */
+    if (xiselect_cd_range(env->vsiselect) &&
+        !get_field(env->menvcfg, MENVCFG_CDE)) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
     return hmode(env, csrno);
 }
 
@@ -483,6 +515,17 @@ static RISCVException csrind_or_aia_hmode(CPURISCVState *env, int csrno)
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /*
+     * vsiselect is shared with this predicate: only vsireg is affected.
+     * With menvcfg.CDE = 0 the counter range is unimplemented, so the
+     * access is an illegal instruction regardless of privilege mode.
+     */
+    if (csrno == CSR_VSIREG &&
+        xiselect_cd_range(env->vsiselect) &&
+        !get_field(env->menvcfg, MENVCFG_CDE)) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
     return hmode(env, csrno);
 }
 
-- 
2.56.1.windows.1



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-06  2:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  2:55 [PATCH v2] target/riscv: fix VS-mode sireg access to a counter exception Juliet Mathis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.