All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juliet Mathis <julietmathis2004@gmail.com>
To: qemu-devel@nongnu.org
Cc: LIU Zhiwei <zhiwei_liu@linux.alibaba.com>,
	Weiwei Li <liwei1518@gmail.com>,
	Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>,
	Alistair Francis <alistair.francis@wdc.com>,
	qemu-riscv@nongnu.org, Chao Liu <chao.liu@processmission.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Juliet Mathis <julietmathis2004@gmail.com>,
	Samarth Sarda <samarthsarda@utexas.edu>,
	Dana Coates <elladana.coates@gmail.com>
Subject: [PATCH v2] target/riscv: fix VS-mode sireg access to a counter exception
Date: Sat,  5 Dec 2026 20:55:10 -0600	[thread overview]
Message-ID: <20261206025510.245-1-julietmathis2004@gmail.com> (raw)

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



                 reply	other threads:[~2026-12-06  2:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206025510.245-1-julietmathis2004@gmail.com \
    --to=julietmathis2004@gmail.com \
    --cc=alistair.francis@wdc.com \
    --cc=chao.liu@processmission.com \
    --cc=daniel.barboza@oss.qualcomm.com \
    --cc=elladana.coates@gmail.com \
    --cc=liwei1518@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=samarthsarda@utexas.edu \
    --cc=zhiwei_liu@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.