Skip to contentSkip to conversation

target/riscv: Check ext_zca for misaligned return address of mret/sret.

PATCH Superseded ~yuming 2 participants 2 messages Jan 16, 2025 – Feb 3, 2025 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

~yuming

author
#0 lore raw

PATCH 1/1 target/riscv: Check ext_zca for misaligned return address of mret/sret.

From: Yu-Ming Chang <[email protected]>

Only check misa.C will cause issues when ext_zca is enabled without misa.C being set. For example, only enable ext_zce.

---
 target/riscv/op_helper.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
target/riscv/op_helper.c +8 -4
index eddedacf4b..891002f954 100644
@@ -269,8 +269,10 @@ target_ulong helper_sret(CPURISCVState *env)
     }
 
     target_ulong retpc = env->sepc;
-    if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
-        riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
+    if (!riscv_has_ext(env, RVC) && !env_archcpu(env)->cfg.ext_zca) {
+        if ((retpc & 0x3) != 0) {
+            riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
+        }
     }
 
     if (get_field(env->mstatus, MSTATUS_TSR) && !(env->priv >= PRV_M)) {
@@ -328,8 +330,10 @@ target_ulong helper_mret(CPURISCVState *env)
     }
 
     target_ulong retpc = env->mepc;
-    if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
-        riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
+    if (!riscv_has_ext(env, RVC) && !env_archcpu(env)->cfg.ext_zca) {
+        if ((retpc & 0x3) != 0) {
+            riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
+        }
     }
 
     uint64_t mstatus = env->mstatus;
Signature · 1 line
2.45.2
#1 lore raw
Quoted 5 lines · ~yuming

From: Yu-Ming Chang <[email protected]>

Only check misa.C will cause issues when ext_zca is enabled without misa.C being set. For example, only enable ext_zce.

Thanks for the patch!

I'm not clear what the problem is and what this commit fixes. Do you mind updating this to clearly describe what you are fixing?

Quoted 11 lines
---
 target/riscv/op_helper.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
target/riscv/op_helper.c +0 -0
index eddedacf4b..891002f954 100644

target/riscv/op_helper.c target_ulong helper_sret(CPURISCVState *env) · line 272

Quoted 6 lines
target/riscv/op_helper.c +1 -2
@@ -269,8 +269,10 @@ target_ulong helper_sret(CPURISCVState *env)
     }
 
     target_ulong retpc = env->sepc;
-    if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
-        riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
+    if (!riscv_has_ext(env, RVC) && !env_archcpu(env)->cfg.ext_zca) {

I think you can just check ext_zca, as misa.C enables ext_zca

Alistair

Quoted 22 lines
target/riscv/op_helper.c +7 -3
+        if ((retpc & 0x3) != 0) {
+            riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
+        }
     }
 
     if (get_field(env->mstatus, MSTATUS_TSR) && !(env->priv >= PRV_M)) {
@@ -328,8 +330,10 @@ target_ulong helper_mret(CPURISCVState *env)
     }
 
     target_ulong retpc = env->mepc;
-    if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
-        riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
+    if (!riscv_has_ext(env, RVC) && !env_archcpu(env)->cfg.ext_zca) {
+        if ((retpc & 0x3) != 0) {
+            riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
+        }
     }
 
     uint64_t mstatus = env->mstatus;
--

2.45.2