Skip to content

Commit ffe4db1

Browse files
rockindyalistair23
authored andcommitted
target/riscv: Add check for 16-bit aligned PC for different priv versions.
For privilege version 1.12 or newer, C always implies Zca. We can only check ext_zca to allow 16-bit aligned PC addresses. For older privilege versions, we only check C. Signed-off-by: Yu-Ming Chang <[email protected]> Reviewed-by: Alistair Francis <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]>
1 parent 1a010d2 commit ffe4db1

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

target/riscv/cpu.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,18 @@ static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
765765
}
766766
#endif
767767

768+
static inline bool riscv_cpu_allow_16bit_insn(const RISCVCPUConfig *cfg,
769+
target_long priv_ver,
770+
uint32_t misa_ext)
771+
{
772+
/* In priv spec version 1.12 or newer, C always implies Zca */
773+
if (priv_ver >= PRIV_VERSION_1_12_0) {
774+
return cfg->ext_zca;
775+
} else {
776+
return misa_ext & RVC;
777+
}
778+
}
779+
768780
/*
769781
* Encode LMUL to lmul as follows:
770782
* LMUL vlmul lmul

target/riscv/insn_trans/trans_rvi.c.inc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ static bool trans_jalr(DisasContext *ctx, arg_jalr *a)
151151
tcg_gen_ext32s_tl(target_pc, target_pc);
152152
}
153153

154-
if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) {
154+
if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
155+
ctx->priv_ver,
156+
ctx->misa_ext)) {
155157
TCGv t0 = tcg_temp_new();
156158

157159
misaligned = gen_new_label();
@@ -300,7 +302,9 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TCGCond cond)
300302

301303
gen_set_label(l); /* branch taken */
302304

303-
if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca &&
305+
if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
306+
ctx->priv_ver,
307+
ctx->misa_ext) &&
304308
(a->imm & 0x3)) {
305309
/* misaligned */
306310
TCGv target_pc = tcg_temp_new();

target/riscv/op_helper.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ target_ulong helper_sret(CPURISCVState *env)
279279
}
280280

281281
target_ulong retpc = env->sepc;
282-
if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
282+
if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
283+
env->priv_ver,
284+
env->misa_ext) && (retpc & 0x3)) {
283285
riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
284286
}
285287

@@ -357,7 +359,9 @@ static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc,
357359
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
358360
}
359361

360-
if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
362+
if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
363+
env->priv_ver,
364+
env->misa_ext) && (retpc & 0x3)) {
361365
riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
362366
}
363367

target/riscv/translate.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
606606
TCGv succ_pc = dest_gpr(ctx, rd);
607607

608608
/* check misaligned: */
609-
if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) {
609+
if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
610+
ctx->priv_ver,
611+
ctx->misa_ext)) {
610612
if ((imm & 0x3) != 0) {
611613
TCGv target_pc = tcg_temp_new();
612614
gen_pc_plus_diff(target_pc, ctx, imm);

0 commit comments

Comments
 (0)