Skip to contentSkip to conversation

target/riscv: enforce SSE precedence for ssp CSR access

PATCH New wangyang 1 participant 1 message Oct 15 RISC-V

lore
Share & tools
lore

wangyang

author
#0 lore raw

PATCH 1/1 target/riscv: enforce SSE precedence for ssp CSR access

When menvcfg.SSE is clear, ssp access below M mode raises an illegal-instruction exception before virtual-instruction classification. Apply the henvcfg/senvcfg checks for VS/VU and preserve the user-only path.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4226

---
 target/riscv/tcg/csr.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)
target/riscv/tcg/csr.c +24 -5
index 061bc9d..98dcb0e 100644
@@ -204,15 +204,34 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)
         return RISCV_EXCP_NONE;
     }
 
-    /* if bcfi not active for current env, access to csr is illegal */
-    if (!cpu_get_bcfien(env)) {
 #if !defined(CONFIG_USER_ONLY)
-        if (env->debugger) {
-            return RISCV_EXCP_NONE;
-        } else if (env->virt_enabled) {
+    if (env->debugger) {
+        return RISCV_EXCP_NONE;
+    }
+
+    /* priv < M and menvcfg.SSE = 0 must raise illegal-instruction */
+    if (!(env->menvcfg & MENVCFG_SSE)) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    if (env->virt_enabled) {
+        if (env->priv == PRV_S) {
+            if (env->henvcfg & HENVCFG_SSE) {
+                return RISCV_EXCP_NONE;
+            }
+            return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
+        }
+        if (env->priv == PRV_U) {
+            if ((env->henvcfg & HENVCFG_SSE) &&
+                (env->senvcfg & SENVCFG_SSE)) {
+                return RISCV_EXCP_NONE;
+            }
             return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
         }
+    }
 #endif
+
+    if (!cpu_get_bcfien(env)) {
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
Signature · 1 line
2.43.0