target/riscv: Fix target address to update badaddr

Compute the target address before storing it into badaddr
when mis-aligned exception is triggered.
Use a target_pc temp to store the target address to avoid
the confusing operation that udpate target address into
cpu_pc before misalign check, then update it into badaddr
and restore cpu_pc to current pc if exception is triggered.

Signed-off-by: Weiwei Li <liweiwei@iscas.ac.cn>
Signed-off-by: Junqiang Wang <wangjunqiang@iscas.ac.cn>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-Id: <20230526072124.298466-2-liweiwei@iscas.ac.cn>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
This commit is contained in:
Weiwei Li 2023-05-26 15:21:18 +08:00 committed by Alistair Francis
parent 3bd87176ee
commit bfc4f9e351
3 changed files with 28 additions and 20 deletions

View File

@ -51,25 +51,30 @@ static bool trans_jal(DisasContext *ctx, arg_jal *a)
static bool trans_jalr(DisasContext *ctx, arg_jalr *a) static bool trans_jalr(DisasContext *ctx, arg_jalr *a)
{ {
TCGLabel *misaligned = NULL; TCGLabel *misaligned = NULL;
TCGv target_pc = tcg_temp_new();
tcg_gen_addi_tl(cpu_pc, get_gpr(ctx, a->rs1, EXT_NONE), a->imm); tcg_gen_addi_tl(target_pc, get_gpr(ctx, a->rs1, EXT_NONE), a->imm);
tcg_gen_andi_tl(cpu_pc, cpu_pc, (target_ulong)-2); tcg_gen_andi_tl(target_pc, target_pc, (target_ulong)-2);
if (get_xl(ctx) == MXL_RV32) {
tcg_gen_ext32s_tl(target_pc, target_pc);
}
gen_set_pc(ctx, cpu_pc);
if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) { if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) {
TCGv t0 = tcg_temp_new(); TCGv t0 = tcg_temp_new();
misaligned = gen_new_label(); misaligned = gen_new_label();
tcg_gen_andi_tl(t0, cpu_pc, 0x2); tcg_gen_andi_tl(t0, target_pc, 0x2);
tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0x0, misaligned); tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0x0, misaligned);
} }
gen_set_gpri(ctx, a->rd, ctx->pc_succ_insn); gen_set_gpri(ctx, a->rd, ctx->pc_succ_insn);
tcg_gen_mov_tl(cpu_pc, target_pc);
lookup_and_goto_ptr(ctx); lookup_and_goto_ptr(ctx);
if (misaligned) { if (misaligned) {
gen_set_label(misaligned); gen_set_label(misaligned);
gen_exception_inst_addr_mis(ctx); gen_exception_inst_addr_mis(ctx, target_pc);
} }
ctx->base.is_jmp = DISAS_NORETURN; ctx->base.is_jmp = DISAS_NORETURN;
@ -153,6 +158,7 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TCGCond cond)
TCGLabel *l = gen_new_label(); TCGLabel *l = gen_new_label();
TCGv src1 = get_gpr(ctx, a->rs1, EXT_SIGN); TCGv src1 = get_gpr(ctx, a->rs1, EXT_SIGN);
TCGv src2 = get_gpr(ctx, a->rs2, EXT_SIGN); TCGv src2 = get_gpr(ctx, a->rs2, EXT_SIGN);
target_ulong next_pc;
if (get_xl(ctx) == MXL_RV128) { if (get_xl(ctx) == MXL_RV128) {
TCGv src1h = get_gprh(ctx, a->rs1); TCGv src1h = get_gprh(ctx, a->rs1);
@ -169,10 +175,13 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TCGCond cond)
gen_set_label(l); /* branch taken */ gen_set_label(l); /* branch taken */
next_pc = ctx->base.pc_next + a->imm;
if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca && if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca &&
((ctx->base.pc_next + a->imm) & 0x3)) { (next_pc & 0x3)) {
/* misaligned */ /* misaligned */
gen_exception_inst_addr_mis(ctx); TCGv target_pc = tcg_temp_new();
gen_pc_plus_diff(target_pc, ctx, next_pc);
gen_exception_inst_addr_mis(ctx, target_pc);
} else { } else {
gen_goto_tb(ctx, 0, ctx->base.pc_next + a->imm); gen_goto_tb(ctx, 0, ctx->base.pc_next + a->imm);
} }

View File

@ -202,8 +202,8 @@ static bool gen_pop(DisasContext *ctx, arg_cmpp *a, bool ret, bool ret_val)
} }
if (ret) { if (ret) {
TCGv ret_addr = get_gpr(ctx, xRA, EXT_NONE); TCGv ret_addr = get_gpr(ctx, xRA, EXT_SIGN);
gen_set_pc(ctx, ret_addr); tcg_gen_mov_tl(cpu_pc, ret_addr);
tcg_gen_lookup_and_goto_ptr(); tcg_gen_lookup_and_goto_ptr();
ctx->base.is_jmp = DISAS_NORETURN; ctx->base.is_jmp = DISAS_NORETURN;
} }

View File

@ -224,21 +224,18 @@ static void decode_save_opc(DisasContext *ctx)
ctx->insn_start = NULL; ctx->insn_start = NULL;
} }
static void gen_set_pc_imm(DisasContext *ctx, target_ulong dest) static void gen_pc_plus_diff(TCGv target, DisasContext *ctx,
target_ulong dest)
{ {
if (get_xl(ctx) == MXL_RV32) { if (get_xl(ctx) == MXL_RV32) {
dest = (int32_t)dest; dest = (int32_t)dest;
} }
tcg_gen_movi_tl(cpu_pc, dest); tcg_gen_movi_tl(target, dest);
} }
static void gen_set_pc(DisasContext *ctx, TCGv dest) static void gen_set_pc_imm(DisasContext *ctx, target_ulong dest)
{ {
if (get_xl(ctx) == MXL_RV32) { gen_pc_plus_diff(cpu_pc, ctx, dest);
tcg_gen_ext32s_tl(cpu_pc, dest);
} else {
tcg_gen_mov_tl(cpu_pc, dest);
}
} }
static void generate_exception(DisasContext *ctx, int excp) static void generate_exception(DisasContext *ctx, int excp)
@ -259,9 +256,9 @@ static void gen_exception_illegal(DisasContext *ctx)
} }
} }
static void gen_exception_inst_addr_mis(DisasContext *ctx) static void gen_exception_inst_addr_mis(DisasContext *ctx, TCGv target)
{ {
tcg_gen_st_tl(cpu_pc, cpu_env, offsetof(CPURISCVState, badaddr)); tcg_gen_st_tl(target, cpu_env, offsetof(CPURISCVState, badaddr));
generate_exception(ctx, RISCV_EXCP_INST_ADDR_MIS); generate_exception(ctx, RISCV_EXCP_INST_ADDR_MIS);
} }
@ -553,7 +550,9 @@ static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
next_pc = ctx->base.pc_next + imm; next_pc = ctx->base.pc_next + imm;
if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) { if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) {
if ((next_pc & 0x3) != 0) { if ((next_pc & 0x3) != 0) {
gen_exception_inst_addr_mis(ctx); TCGv target_pc = tcg_temp_new();
gen_pc_plus_diff(target_pc, ctx, next_pc);
gen_exception_inst_addr_mis(ctx, target_pc);
return; return;
} }
} }