riscv: set vstart_eq_zero on mark_vs_dirty
cover riscv: set vstart_eq_zero on mark_vs_dirty
Hi,
This is my shot to fix https://gitlab.com/qemu-project/qemu/-/issues/1976.
First patch ensures that every vector instruction that changes the vector state will call mark_vs_dirty(). Second patch is a trivial simplification.
Third patch is where the bug is solved: check if 'vstart' is zeroed and set vstart_eq_zero accordingly.
Patches based on alistair/riscv-to-apply.next. It can also be fetched here:
https://gitlab.com/danielhb/qemu/-/tree/vstart_bug1976_v1
Daniel Henrique Barboza (3):
trans_rvv.c.inc: write CSRs must call mark_vs_dirty() too trans_rvv.c.inc: remove redundant mark_vs_dirty() calls target/riscv/translate.c: set vstart_eq_zero in mark_vs_dirty()
target/riscv/insn_trans/trans_rvv.c.inc | 28 +++++++------------------ target/riscv/translate.c | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 21 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 trans_rvv.c.inc: write CSRs must call mark_vs_dirty() too
In the Vector spec section 3.2 [1]:
"When mstatus.VS is set to Initial or Clean, executing any instruction
that changes vector state, including the vector CSRs, will change mstatus.VS to Dirty."
ldst_us_trans(), ldst_stride_trans(), ldst_index_trans() and ldst_whole_trans() will change vector state regardless of being a store op or not. Stores will set env->vstart to zero after execution (see vext_ldst_us() in vector_helper.c), and this is vector CSR state change.
[1] https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc#vector-start-index-csr-vstart
--- target/riscv/insn_trans/trans_rvv.c.inc | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +4 -15
@@ -638,10 +638,7 @@ static bool ldst_us_trans(uint32_t vd, uint32_t rs1, uint32_t data, fn(dest, mask, base, tcg_env, desc); - if (!is_store) { - mark_vs_dirty(s); - } - + mark_vs_dirty(s); gen_set_label(over); return true; }
@@ -799,10 +796,7 @@ static bool ldst_stride_trans(uint32_t vd, uint32_t rs1, uint32_t rs2, fn(dest, mask, base, stride, tcg_env, desc); - if (!is_store) { - mark_vs_dirty(s); - } - + mark_vs_dirty(s); gen_set_label(over); return true; }
@@ -906,10 +900,7 @@ static bool ldst_index_trans(uint32_t vd, uint32_t rs1, uint32_t vs2, fn(dest, mask, base, index, tcg_env, desc); - if (!is_store) { - mark_vs_dirty(s); - } - + mark_vs_dirty(s); gen_set_label(over); return true; }
@@ -1104,9 +1095,7 @@ static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf, fn(dest, base, tcg_env, desc); - if (!is_store) { - mark_vs_dirty(s); - } + mark_vs_dirty(s); gen_set_label(over); return true;
Signature · 1 line
2.43.0
PATCH 2/3 trans_rvv.c.inc: remove redundant mark_vs_dirty() calls
In reply to Daniel Henrique Barbozatrans_vmv_v_i , trans_vfmv_v_f and the trans_##NAME macro from GEN_VMV_WHOLE_TRANS() are calling mark_vs_dirty() in both branches of their 'ifs'. conditionals.
Call it just once in the end like other functions are doing.
--- target/riscv/insn_trans/trans_rvv.c.inc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +3 -6
@@ -2094,7 +2094,6 @@ static bool trans_vmv_v_i(DisasContext *s, arg_vmv_v_i *a) if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) { tcg_gen_gvec_dup_imm(s->sew, vreg_ofs(s, a->rd), MAXSZ(s), MAXSZ(s), simm); - mark_vs_dirty(s); } else { TCGv_i32 desc; TCGv_i64 s1;
@@ -2115,9 +2114,9 @@ static bool trans_vmv_v_i(DisasContext *s, arg_vmv_v_i *a) tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, a->rd)); fns[s->sew](dest, s1, tcg_env, desc); - mark_vs_dirty(s); gen_set_label(over); } + mark_vs_dirty(s); return true; } return false;
@@ -2660,7 +2659,6 @@ static bool trans_vfmv_v_f(DisasContext *s, arg_vfmv_v_f *a) tcg_gen_gvec_dup_i64(s->sew, vreg_ofs(s, a->rd), MAXSZ(s), MAXSZ(s), t1); - mark_vs_dirty(s); } else { TCGv_ptr dest; TCGv_i32 desc;
@@ -2686,9 +2684,9 @@ static bool trans_vfmv_v_f(DisasContext *s, arg_vfmv_v_f *a) fns[s->sew - 1](dest, t1, tcg_env, desc); - mark_vs_dirty(s); gen_set_label(over); } + mark_vs_dirty(s); return true; } return false;
@@ -3632,15 +3630,14 @@ static bool trans_##NAME(DisasContext *s, arg_##NAME * a) \ if (s->vstart_eq_zero) { \ tcg_gen_gvec_mov(s->sew, vreg_ofs(s, a->rd), \ vreg_ofs(s, a->rs2), maxsz, maxsz); \ - mark_vs_dirty(s); \ } else { \ TCGLabel *over = gen_new_label(); \ tcg_gen_brcondi_tl(TCG_COND_GEU, cpu_vstart, maxsz, over); \ tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2), \ tcg_env, maxsz, maxsz, 0, gen_helper_vmvr_v); \ - mark_vs_dirty(s); \ gen_set_label(over); \ } \ + mark_vs_dirty(s); \ return true; \ } \ return false; \
Signature · 1 line
2.43.0
PATCH 3/3 target/riscv/translate.c: set vstart_eq_zero in mark_vs_dirty()
In reply to Daniel Henrique BarbozaThe 'vstart_eq_zero' flag which is used to determine if some insns, like vector reductor operations, should SIGILL. At this moment the flag is being updated only during cpu_get_tb_cpu_state(), at the start of each translation block.
This cadence isn't enough and we're facing situations where a vector instruction successfully updated 'vstart' to zero, but the flag was still marked as 'false', resulting in a SIGILL because instructions are checking the flag.
mark_vs_dirty() is called after any instruction changes Vector CSR state, making it a good place to update 'vstart_eq_zero'.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1976
--- target/riscv/translate.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
target/riscv/translate.c +22 -0
@@ -652,9 +652,13 @@ static inline void mark_fs_dirty(DisasContext *ctx) { }*/ static void mark_vs_dirty (DisasContext *ctx) { +TCGLabel *vstart_zero, *done;TCGv tmp;if (ctx->mstatus_vs !=EXT_STATUS_DIRTY ) { + vstart_zero =gen_new_label (); + done =gen_new_label (); +/* Remember the state change for the rest of the TB. */ ctx->mstatus_vs =EXT_STATUS_DIRTY ;
@@ -668,6 +672,24 @@ static void mark_vs_dirty(DisasContext *ctx)tcg_gen_ori_tl (tmp, tmp,MSTATUS_VS ); tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); } + +/* +* We can safely make 'vl_eq_vlmax = false' if we marked +* VS as dirty with non-zero 'vstart', i.e. there's a fault +* to be handled. If 'vstart' is zero then we should retain +* the existing 'vl_eq_vlmax' - it'll be recalculated on the +* start of the next TB or during vset{i}vl{i} (that forces a +* TB end). +*/ +tcg_gen_brcondi_tl (TCG_COND_EQ , cpu_vstart,0 , vstart_zero); + ctx->vstart_eq_zero = false; + ctx->vl_eq_vlmax = false; +tcg_gen_br (done); + +gen_set_label (vstart_zero); + ctx->vstart_eq_zero = true; + +gen_set_label (done); } }#else
Signature · 1 line
2.43.0
Hi,
Please disregard this version. I just posted a v2 after feedback from people in Gitlab.
Thanks,
Daniel
Quoted 26 lines · Daniel Henrique Barboza
Hi,
This is my shot to fix https://gitlab.com/qemu-project/qemu/-/issues/1976.
First patch ensures that every vector instruction that changes the vector state will call
mark_vs_dirty(). Second patch is a trivial simplification.Third patch is where the bug is solved: check if 'vstart' is zeroed and set
vstart_eq_zeroaccordingly.Patches based on alistair/riscv-to-apply.next. It can also be fetched here:
https://gitlab.com/danielhb/qemu/-/tree/vstart_bug1976_v1
Daniel Henrique Barboza (3):
trans_rvv.c.inc: write CSRs must callmark_vs_dirty()too trans_rvv.c.inc: remove redundantmark_vs_dirty()callstarget/riscv/translate.c: setvstart_eq_zeroinmark_vs_dirty()target/riscv/insn_trans/trans_rvv.c.inc | 28 +++++++------------------ target/riscv/translate.c | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 21 deletions(-)