target/riscv: fix reserved/illegal instruction encodings
v2 cover target/riscv: fix reserved/illegal instruction encodings
This series fixes four RISC-V instruction legality defects in the translator:
target/riscv: honor zicbo* envcfg gating in linux-user mode
cbo.inval/cbo.flush/cbo.zero retired unconditionally in linux-user because check_zicbo_envcfg() skipped the envcfg check entirely in user-only builds. Add a senvcfg-based gate and initialize SENVCFG_CBZE at reset when ext_zicboz is enabled, so cbo.zero stays usable while cbo.inval/cbo.flush remain illegal, matching the user-mode view of a typical firmware/kernel setup.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4107
target/riscv: reject FMV.X.W/FMV.W.X under Zfinx
Zfinx explicitly excludes the FMV transfer instructions, but the translators used REQUIRE_ZFINX_OR_F so a Zfinx-only CPU accepted them. Require RVF instead so the transfers trap with an illegal instruction when only Zfinx is present.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4108
target/riscv: enforce even register constraints for Zdinx fcvt pairs
fcvt.d.h and fcvt.h.d access a 64-bit double held in a register pair, so under Zdinx/Zhinxmin the odd-rd (fcvt.d.h) and odd-rs1 (fcvt.h.d) encodings are reserved. Add the missing REQUIRE_EVEN checks so those encodings raise an illegal-instruction exception instead of retiring.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4109
target/riscv: make cm.mva01s/cm.mvsa01 high-sreg forms illegal on RV32E
The Zcmp pseudocode marks cm.mva01s/cm.mvsa01 as reserved on RV32E when either compressed saved-register selector is above s1, but the translators only checked REQUIRE_ZCMP. Add a gate that returns false when RVE is enabled and either operand exceeds x9, so the high-sreg forms raise an illegal-instruction exception while the s0/s1 forms and all RV32I forms keep retiring.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4110
Each fix was verified with an affected/fixed probe pair against QEMU builds and native RISC-V hardware, with legal-encoding controls regressing unchanged. All four patches were reviewed by Alistair Francis in the v1 thread; v2 adds this cover letter per his request and records the Reviewed-by tags, with no code changes.
wangyang (4):
target/riscv: honor zicbo* envcfg gating in linux-user mode target/riscv: reject FMV.X.W/FMV.W.X under Zfinx target/riscv: enforce even register constraints for Zdinx fcvt pairs target/riscv: make cm.mva01s/cm.mvsa01 high-sreg forms illegal on RV32E
PATCH v2 1/4 target/riscv: honor zicbo* envcfg gating in linux-user mode
In user-only builds check_zicbo_envcfg() skipped the envcfg check entirely (#if !defined(CONFIG_USER_ONLY)), so cbo.inval/cbo.flush/ cbo.zero retired unconditionally in linux-user even though the machine-level envcfg fields are never initialized. Give the user-mode build a senvcfg-based gate, and initialize SENVCFG_CBZE at reset when ext_zicboz is enabled so cbo.zero stays usable while cbo.inval/cbo.flush remain illegal, matching the user-mode view of a typical firmware/kernel setup.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4107
---
2 files changed, 18 insertions(+), 1 deletion(-)
target/riscv/cpu.c +8 -0
@@ -1061,6 +1061,14 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)#else env->priv =PRV_U ; env->senvcfg =0 ; +/* +* Match the user-mode view of a typical firmware/kernel setup where +* cbo.zero is enabled for user mode; the CBCFE/CBIE bits stay zero, +* so the cache-management operations remain illegal in user mode. +*/ +if (riscv_cpu_cfg (env)->ext_zicboz) { + env->senvcfg |=SENVCFG_CBZE ; + } env->menvcfg =0 ;#endif /* !CONFIG_USER_ONLY */
target/riscv/tcg/op_helper.c +10 -1
@@ -146,7 +146,16 @@ target_ulong helper_csrrw_i128(CPURISCVState *env, int csr,static void check_zicbo_envcfg (CPURISCVState *env,target_ulong envbits,uintptr_t ra) { -#ifndef CONFIG_USER_ONLY +#if defined(CONFIG_USER_ONLY ) +/* +* linux-user: the machine-level envcfg fields are not part of the +* user-mode environment; only the user-mode view of the enabling +* bits (senvcfg, as initialized for the guest) applies. +*/ +if (!get_field (env->senvcfg, envbits)) { +riscv_raise_exception (env,RISCV_EXCP_ILLEGAL_INST , ra); + } +#else if ((env->priv <PRV_M ) && !get_field (env->menvcfg, envbits)) {riscv_raise_exception (env,RISCV_EXCP_ILLEGAL_INST , ra); }
PATCH v2 2/4 target/riscv: reject FMV.X.W/FMV.W.X under Zfinx
In reply to wangyangZfinx explicitly excludes the FMV transfer instructions, but trans_fmv_x_w/trans_fmv_w_x used REQUIRE_ZFINX_OR_F so a Zfinx-only CPU accepted them. Require RVF instead so the transfers trap with an illegal instruction when only Zfinx is present.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4108
---
1 file changed, 4 insertions(+), 2 deletions(-)
target/riscv/tcg/insn_trans/trans_rvf.c.inc +4 -2
@@ -428,7 +428,8 @@ static bool trans_fmv_x_w(DisasContext *ctx, arg_fmv_x_w *a) { /* NOTE: This was FMV.X.S in an earlier version of the ISA spec! */ REQUIRE_FPU; - REQUIRE_ZFINX_OR_F(ctx); + /* Zfinx explicitly excludes the FMV transfer instructions. */ + REQUIRE_EXT(ctx, RVF); TCGv dest = dest_gpr(ctx, a->rd); TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
@@ -531,7 +532,8 @@ static bool trans_fmv_w_x(DisasContext *ctx, arg_fmv_w_x *a) { /* NOTE: This was FMV.S.X in an earlier version of the ISA spec! */ REQUIRE_FPU; - REQUIRE_ZFINX_OR_F(ctx); + /* Zfinx explicitly excludes the FMV transfer instructions. */ + REQUIRE_EXT(ctx, RVF); TCGv_i64 dest = dest_fpr(ctx, a->rd); TCGv src = get_gpr(ctx, a->rs1, EXT_ZERO);
PATCH v2 3/4 target/riscv: enforce even register constraints for Zdinx fcvt pairs
In reply to wangyangfcvt.d.h and fcvt.h.d access a 64-bit double held in a register pair, so under Zdinx/Zhinxmin the odd-rd (fcvt.d.h) and odd-rs1 (fcvt.h.d) encodings are reserved. Add the missing REQUIRE_EVEN checks so those encodings raise an illegal-instruction exception instead of retiring.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4109
---
1 file changed, 2 insertions(+), 0 deletions(-)
target/riscv/tcg/insn_trans/trans_rvzfh.c.inc +2 -0
@@ -413,6 +413,7 @@ static bool trans_fcvt_d_h(DisasContext *ctx, arg_fcvt_d_h *a) REQUIRE_FPU; REQUIRE_ZFHMIN_OR_ZHINXMIN(ctx); REQUIRE_ZDINX_OR_D(ctx); + REQUIRE_EVEN(ctx, a->rd); TCGv_i64 dest = dest_fpr(ctx, a->rd); TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
@@ -447,6 +448,7 @@ static bool trans_fcvt_h_d(DisasContext *ctx, arg_fcvt_h_d *a) REQUIRE_FPU; REQUIRE_ZFHMIN_OR_ZHINXMIN(ctx); REQUIRE_ZDINX_OR_D(ctx); + REQUIRE_EVEN(ctx, a->rs1); TCGv_i64 dest = dest_fpr(ctx, a->rd); TCGv_i64 src1 = get_fpr_d(ctx, a->rs1);
PATCH v2 4/4 target/riscv: make cm.mva01s/cm.mvsa01 high-sreg forms illegal on RV32E
In reply to wangyangThe Zcmp pseudocode marks cm.mva01s/cm.mvsa01 as reserved on RV32E when either compressed saved-register selector is above s1, but the translators only checked REQUIRE_ZCMP. Since %r1s/%r2s map the selectors to register numbers (x8/x9 for s0/s1, x18..x23 for s2..s7), add a gate that returns false when RVE is enabled and either operand exceeds x9, so the high-sreg forms raise an illegal-instruction exception while the s0/s1 forms and all RV32I forms keep retiring.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4110
---
1 file changed, 6 insertions(+), 0 deletions(-)
target/riscv/tcg/insn_trans/trans_rvzce.c.inc +8 -0
@@ -273,6 +273,10 @@ static bool trans_cm_mva01s(DisasContext *ctx, arg_cm_mva01s *a) { REQUIRE_ZCMP(ctx); + if (has_ext(ctx, RVE) && (a->rs1 > 9 || a->rs2 > 9)) { + return false; + } + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
@@ -286,6 +290,10 @@ static bool trans_cm_mvsa01(DisasContext *ctx, arg_cm_mvsa01 *a) { REQUIRE_ZCMP(ctx); + if (has_ext(ctx, RVE) && (a->rs1 > 9 || a->rs2 > 9)) { + return false; + } + if (a->rs1 == a->rs2) { return false; }