target/riscv: Check ext_zca for misaligned return address of mret/sret.
PATCH v2 1/1 target/riscv: Check ext_zca for misaligned return address of mret/sret.
From: Yu-Ming Chang <[email protected]>
We only check RVC to allow 16-bit aligned return addreses. This will cause issues when only ext_zca is enabled without RVC: 16-bit instructions are allowed, but 16-bit aligned return address are not. We should also check ext_zca to permit 16-bit aligned return addresses.
---
The v2 has been updated to provide more explanation.
target/riscv/op_helper.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
target/riscv/op_helper.c +8 -4
@@ -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.3
Quoted 20 lines · ~yuming
From: Yu-Ming Chang <[email protected]>
We only check RVC to allow 16-bit aligned return addreses. This will cause issues when only
ext_zcais enabled without RVC: 16-bit instructions are allowed, but 16-bit aligned return address are not. We should also checkext_zcato permit 16-bit aligned return addresses.Signed-off-by Yu-Ming Chang <[email protected]>---
The v2 has been updated to provide more explanation.target/riscv/op_helper.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)target/riscv/op_helper.c +0
-0
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) {
You can just check ext_zca as C always implies 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.3
Hi Alistair,
I have sent another patch according to your suggestion:
[PATCH qemu] target/riscv: Only check ext_zca for 16-bit aligned PC.
Yuming
-----Original Message-----
From: Alistair Francis <[email protected]>
Sent: Monday, February 24, 2025 11:45 AM
To: Yuming Yu-Ming Chang(張育銘) <[email protected]>
Cc: [email protected]; [email protected]; Palmer Dabbelt <[email protected]>; Alistair Francis <[email protected]>; Bin Meng <[email protected]>; Weiwei Li <[email protected]>; Daniel Henrique Barboza <[email protected]>; Liu Zhiwei <[email protected]>
Subject: Re: [PATCH qemu v2] target/riscv: Check ext_zca for misaligned return address of mret/sret.
[EXTERNAL MAIL]
Quoted 20 lines · ~yuming
From: Yu-Ming Chang <[email protected]>
We only check RVC to allow 16-bit aligned return addreses. This will cause issues when only
ext_zcais enabled without RVC: 16-bit instructions are allowed, but 16-bit aligned return address are not. We should also checkext_zcato permit 16-bit aligned return addresses.Signed-off-by Yu-Ming Chang <[email protected]>---
The v2 has been updated to provide more explanation.target/riscv/op_helper.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)target/riscv/op_helper.c +0
-0
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) {
You can just check ext_zca as C always implies Zca
Alistair
Quoted 20 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; --
Quoted 2 lines
2.45.3
CONFIDENTIALITY NOTICE:
This e-mail (and its attachments) may contain confidential and legally privileged information or information protected from disclosure. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein is strictly prohibited. In this case, please immediately notify the sender by return e-mail, delete the message (and any accompanying documents) and destroy all printed hard copies. Thank you for your cooperation.
Copyright ANDES TECHNOLOGY CORPORATION - All Rights Reserved.