From 2e2ca3c8fa52c03c3725edfdd726972a223b74f3 Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 15 May 2023 21:00:42 +0800 Subject: [PATCH 1/2] target/loongarch: Fix LD/ST{LE/GT} instructions get wrong CSR_ERA and CSR_BADV 1.helper_asrtle_d/helper_asrtgt_d need use GETPC() to get PC; 2 LD/ST{LE/GT} need set CSR_BADV = gpr[rj]; 3 ASRTLE.D/ASRTGT.D also write CSR_BADV, but this value is random and has no reference value. Signed-off-by: Song Gao Reviewed-by: Richard Henderson Message-Id: <20230515130042.2719712-1-gaosong@loongson.cn> --- target/loongarch/cpu.c | 2 +- target/loongarch/op_helper.c | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c index c0afc21b2f..ad93ecac92 100644 --- a/target/loongarch/cpu.c +++ b/target/loongarch/cpu.c @@ -187,10 +187,10 @@ static void loongarch_cpu_do_interrupt(CPUState *cs) case EXCCODE_IPE: case EXCCODE_FPD: case EXCCODE_FPE: - case EXCCODE_BCE: case EXCCODE_SXD: env->CSR_BADV = env->pc; QEMU_FALLTHROUGH; + case EXCCODE_BCE: case EXCCODE_ADEM: case EXCCODE_PIL: case EXCCODE_PIS: diff --git a/target/loongarch/op_helper.c b/target/loongarch/op_helper.c index 568c071601..60335a05e2 100644 --- a/target/loongarch/op_helper.c +++ b/target/loongarch/op_helper.c @@ -49,14 +49,16 @@ target_ulong helper_bitswap(target_ulong v) void helper_asrtle_d(CPULoongArchState *env, target_ulong rj, target_ulong rk) { if (rj > rk) { - do_raise_exception(env, EXCCODE_BCE, 0); + env->CSR_BADV = rj; + do_raise_exception(env, EXCCODE_BCE, GETPC()); } } void helper_asrtgt_d(CPULoongArchState *env, target_ulong rj, target_ulong rk) { if (rj <= rk) { - do_raise_exception(env, EXCCODE_BCE, 0); + env->CSR_BADV = rj; + do_raise_exception(env, EXCCODE_BCE, GETPC()); } } From 65bfaaae6ac79ebc623acc0ce28cc3bd4fe8b5e5 Mon Sep 17 00:00:00 2001 From: Song Gao Date: Thu, 25 May 2023 20:00:05 +0800 Subject: [PATCH 2/2] target/loongarch: Fix the vinsgr2vr/vpickve2gr instructions cause system coredump The vinsgr2vr/vpickve2gr instructions need use get_src/get_dst to get gpr registers value, not cpu_gpr[]. The $zero register does not have cpu_gpr[0] allocated. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1662 Signed-off-by: Song Gao Reviewed-by: Richard Henderson Message-Id: <20230525120005.2223413-1-gaosong@loongson.cn> --- target/loongarch/insn_trans/trans_lsx.c.inc | 39 ++++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/target/loongarch/insn_trans/trans_lsx.c.inc b/target/loongarch/insn_trans/trans_lsx.c.inc index 0be2b5a3a8..68779daff6 100644 --- a/target/loongarch/insn_trans/trans_lsx.c.inc +++ b/target/loongarch/insn_trans/trans_lsx.c.inc @@ -3963,106 +3963,119 @@ TRANS(vsetallnez_d, gen_cv, gen_helper_vsetallnez_d) static bool trans_vinsgr2vr_b(DisasContext *ctx, arg_vr_i *a) { + TCGv src = gpr_src(ctx, a->rj, EXT_NONE); CHECK_SXE; - tcg_gen_st8_i64(cpu_gpr[a->rj], cpu_env, + tcg_gen_st8_i64(src, cpu_env, offsetof(CPULoongArchState, fpr[a->vd].vreg.B(a->imm))); return true; } static bool trans_vinsgr2vr_h(DisasContext *ctx, arg_vr_i *a) { + TCGv src = gpr_src(ctx, a->rj, EXT_NONE); CHECK_SXE; - tcg_gen_st16_i64(cpu_gpr[a->rj], cpu_env, + tcg_gen_st16_i64(src, cpu_env, offsetof(CPULoongArchState, fpr[a->vd].vreg.H(a->imm))); return true; } static bool trans_vinsgr2vr_w(DisasContext *ctx, arg_vr_i *a) { + TCGv src = gpr_src(ctx, a->rj, EXT_NONE); CHECK_SXE; - tcg_gen_st32_i64(cpu_gpr[a->rj], cpu_env, + tcg_gen_st32_i64(src, cpu_env, offsetof(CPULoongArchState, fpr[a->vd].vreg.W(a->imm))); return true; } static bool trans_vinsgr2vr_d(DisasContext *ctx, arg_vr_i *a) { + TCGv src = gpr_src(ctx, a->rj, EXT_NONE); CHECK_SXE; - tcg_gen_st_i64(cpu_gpr[a->rj], cpu_env, + tcg_gen_st_i64(src, cpu_env, offsetof(CPULoongArchState, fpr[a->vd].vreg.D(a->imm))); return true; } static bool trans_vpickve2gr_b(DisasContext *ctx, arg_rv_i *a) { + TCGv dst = gpr_dst(ctx, a->rd, EXT_NONE); CHECK_SXE; - tcg_gen_ld8s_i64(cpu_gpr[a->rd], cpu_env, + tcg_gen_ld8s_i64(dst, cpu_env, offsetof(CPULoongArchState, fpr[a->vj].vreg.B(a->imm))); return true; } static bool trans_vpickve2gr_h(DisasContext *ctx, arg_rv_i *a) { + TCGv dst = gpr_dst(ctx, a->rd, EXT_NONE); CHECK_SXE; - tcg_gen_ld16s_i64(cpu_gpr[a->rd], cpu_env, + tcg_gen_ld16s_i64(dst, cpu_env, offsetof(CPULoongArchState, fpr[a->vj].vreg.H(a->imm))); return true; } static bool trans_vpickve2gr_w(DisasContext *ctx, arg_rv_i *a) { + TCGv dst = gpr_dst(ctx, a->rd, EXT_NONE); CHECK_SXE; - tcg_gen_ld32s_i64(cpu_gpr[a->rd], cpu_env, + tcg_gen_ld32s_i64(dst, cpu_env, offsetof(CPULoongArchState, fpr[a->vj].vreg.W(a->imm))); return true; } static bool trans_vpickve2gr_d(DisasContext *ctx, arg_rv_i *a) { + TCGv dst = gpr_dst(ctx, a->rd, EXT_NONE); CHECK_SXE; - tcg_gen_ld_i64(cpu_gpr[a->rd], cpu_env, + tcg_gen_ld_i64(dst, cpu_env, offsetof(CPULoongArchState, fpr[a->vj].vreg.D(a->imm))); return true; } static bool trans_vpickve2gr_bu(DisasContext *ctx, arg_rv_i *a) { + TCGv dst = gpr_dst(ctx, a->rd, EXT_NONE); CHECK_SXE; - tcg_gen_ld8u_i64(cpu_gpr[a->rd], cpu_env, + tcg_gen_ld8u_i64(dst, cpu_env, offsetof(CPULoongArchState, fpr[a->vj].vreg.B(a->imm))); return true; } static bool trans_vpickve2gr_hu(DisasContext *ctx, arg_rv_i *a) { + TCGv dst = gpr_dst(ctx, a->rd, EXT_NONE); CHECK_SXE; - tcg_gen_ld16u_i64(cpu_gpr[a->rd], cpu_env, + tcg_gen_ld16u_i64(dst, cpu_env, offsetof(CPULoongArchState, fpr[a->vj].vreg.H(a->imm))); return true; } static bool trans_vpickve2gr_wu(DisasContext *ctx, arg_rv_i *a) { + TCGv dst = gpr_dst(ctx, a->rd, EXT_NONE); CHECK_SXE; - tcg_gen_ld32u_i64(cpu_gpr[a->rd], cpu_env, + tcg_gen_ld32u_i64(dst, cpu_env, offsetof(CPULoongArchState, fpr[a->vj].vreg.W(a->imm))); return true; } static bool trans_vpickve2gr_du(DisasContext *ctx, arg_rv_i *a) { + TCGv dst = gpr_dst(ctx, a->rd, EXT_NONE); CHECK_SXE; - tcg_gen_ld_i64(cpu_gpr[a->rd], cpu_env, + tcg_gen_ld_i64(dst, cpu_env, offsetof(CPULoongArchState, fpr[a->vj].vreg.D(a->imm))); return true; } static bool gvec_dup(DisasContext *ctx, arg_vr *a, MemOp mop) { + TCGv src = gpr_src(ctx, a->rj, EXT_NONE); CHECK_SXE; tcg_gen_gvec_dup_i64(mop, vec_full_offset(a->vd), - 16, ctx->vl/8, cpu_gpr[a->rj]); + 16, ctx->vl/8, src); return true; }