Add RISC-V big-endian target support
Subsystems (2)
v11 cover Add RISC-V big-endian target support
v11:
- rewrite some patches while reviewing for mainstream integration
v10 cover from Djordje:
Hi all,
This series adds big-endian data support for RISC-V system emulation.
Following the discussion on previous versions, this series models fixed-endian harts, not a mixed-endian implementation where software can toggle MBE/SBE/UBE at runtime. The reset value of MBE/SBE/UBE is selected from the CPU configuration. These bits remain outside the mstatus/mstatush/sstatus writable masks, so they are effectively hardwired to 0 for the default little-endian CPUs and to 1 when big-endian=on.
The user-visible big-endian CPU property is exposed only after the data path, boot path, and page-table walk support are in place, so the series remains bisectable.
Djordje Todorovic (6):
target/riscv: Implement runtime data endianness via MSTATUS bits target/riscv: Fix page table walk endianness for big-endian harts target/riscv: Add big-endian CPU configuration field and reset logic hw/riscv/boot: Honour data endianness
target/riscv: Expose and document the CPU 'big-endian' property tests/functional: Add RISC-V endianness test
Philippe Mathieu-Daudé (6):
target/riscv: Initialize DisasContext::mo_endian once target/riscv: Assert PTE.N/PBMT bits are not expected on RV32 target/riscv: De-indent some code in get_physical_address() target/riscv: Remove target_ulong use in get_physical_address() hw/riscv/boot: Rewrite setup_rom_reset_vec() using load/store API hw/riscv/boot: Replace cpu_to_le32() -> const_le32()
docs/system/target-riscv.rst | 29 ++++++ target/riscv/cpu.h | 1 + target/riscv/internals.h | 29 ++++-- target/riscv/cpu_cfg_fields.h.inc | 1 + hw/riscv/boot.c | 93 ++++++++++--------- target/riscv/cpu.c | 15 ++- target/riscv/cpu_helper.c | 44 +++++---- target/riscv/tcg/tcg-cpu.c | 2 + target/riscv/translate.c | 22 ++--- target/riscv/insn_trans/trans_rva.c.inc | 4 +- target/riscv/insn_trans/trans_rvd.c.inc | 4 +- target/riscv/insn_trans/trans_rvf.c.inc | 4 +- target/riscv/insn_trans/trans_rvi.c.inc | 8 +- target/riscv/insn_trans/trans_rvzacas.c.inc | 4 +- target/riscv/insn_trans/trans_rvzalasr.c.inc | 4 +- target/riscv/insn_trans/trans_rvzce.c.inc | 4 +- target/riscv/insn_trans/trans_rvzfh.c.inc | 4 +- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 4 +- target/riscv/insn_trans/trans_xmips.c.inc | 8 +- target/riscv/insn_trans/trans_xthead.c.inc | 16 ++-- target/riscv/insn_trans/trans_zilsd.c.inc | 4 +- tests/functional/riscv64/meson.build | 1 + tests/functional/riscv64/test_endianness.py | 57 ++++++++++++ 23 files changed, 240 insertions(+), 122 deletions(-) create mode 100644 tests/functional/riscv64/test_endianness.py
Signature · 1 line
2.53.0
PATCH v11 1/12 target/riscv: Initialize DisasContext::mo_endian once
The data access endianness is constant during a translation block; rather than calling the mo_endian() method each time, initialize the DisasContext::mo_endianness field once in TranslatorOps::init_disas_context().
--- target/riscv/translate.c | 9 ++++++--- target/riscv/insn_trans/trans_rva.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvd.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvf.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvi.c.inc | 8 ++++---- target/riscv/insn_trans/trans_rvzacas.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzalasr.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzce.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzfh.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 4 ++-- target/riscv/insn_trans/trans_xmips.c.inc | 8 ++++---- target/riscv/insn_trans/trans_xthead.c.inc | 16 ++++++++-------- target/riscv/insn_trans/trans_zilsd.c.inc | 4 ++-- 13 files changed, 40 insertions(+), 37 deletions(-)
target/riscv/translate.c +6 -3
@@ -120,6 +120,8 @@ typedef struct DisasContext {bool fcfi_lp_expected;/* zicfiss extension, if shadow stack was enabled during TB gen */ bool bcfi_enabled; +/* Data endianness from MSTATUS UBE/SBE/MBE */ +MemOp mo_endianness; } DisasContext;static inline bool has_ext (DisasContext *ctx,uint32_t ext)
@@ -155,7 +157,7 @@ static inline MemOp mo_endian(DisasContext *ctx)#define get_address_xl (ctx) ((ctx)->address_xl)#endif -#define mxl_memop (ctx) ((get_xl(ctx) + 1) | mo_endian(ctx)) +#define mxl_memop (ctx) ((get_xl(ctx) + 1) | (ctx)->mo_endianness)/* The word size for this machine mode. */ static inline int __attribute__((unused))get_xlen (DisasContext *ctx)
@@ -1156,7 +1158,7 @@ static bool gen_amo(DisasContext *ctx, arg_atomic *a,TCGv src1, src2 =get_gpr (ctx, a->rs2,EXT_NONE );MemOp size = mop &MO_SIZE ; - mop |=mo_endian (ctx); + mop |= ctx->mo_endianness;if (ctx->cfg_ptr->ext_zama16b && size >=MO_32 ) { mop |=MO_ATOM_WITHIN16 ; }else {
@@ -1177,7 +1179,7 @@ static bool gen_cmpxchg(DisasContext *ctx, arg_atomic *a, MemOp mop)TCGv src1 =get_address (ctx, a->rs1,0 );TCGv src2 =get_gpr (ctx, a->rs2,EXT_NONE ); - mop |=mo_endian (ctx); + mop |= ctx->mo_endianness;decode_save_opc (ctx,RISCV_UW2_ALWAYS_STORE_AMO );tcg_gen_atomic_cmpxchg_tl (dest, src1, dest, src2, ctx->mem_idx, mop);
@@ -1360,6 +1362,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->zero =tcg_constant_tl (0 ); ctx->virt_inst_excp = false; ctx->decoders = cpu->decoders; + ctx->mo_endianness =mo_endian (ctx); }static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu)
target/riscv/insn_trans/trans_rva.c.inc +2 -2
@@ -35,7 +35,7 @@ static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) TCGv src1; mop |= MO_ALIGN; - mop |= mo_endian(ctx); + mop |= ctx->mo_endianness; decode_save_opc(ctx, 0); src1 = get_address(ctx, a->rs1, 0);
@@ -65,7 +65,7 @@ static bool gen_sc(DisasContext *ctx, arg_atomic *a, MemOp mop) TCGLabel *l2 = gen_new_label(); mop |= MO_ALIGN; - mop |= mo_endian(ctx); + mop |= ctx->mo_endianness; decode_save_opc(ctx, 0); src1 = get_address(ctx, a->rs1, 0);
target/riscv/insn_trans/trans_rvd.c.inc +2 -2
@@ -60,7 +60,7 @@ static bool trans_fld(DisasContext *ctx, arg_fld *a) } else { memop |= MO_ATOM_IFALIGN; } - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; decode_save_opc(ctx, 0); addr = get_address(ctx, a->rs1, a->imm);
@@ -85,7 +85,7 @@ static bool trans_fsd(DisasContext *ctx, arg_fsd *a) } else { memop |= MO_ATOM_IFALIGN; } - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; decode_save_opc(ctx, 0); addr = get_address(ctx, a->rs1, a->imm);
target/riscv/insn_trans/trans_rvf.c.inc +2 -2
@@ -48,7 +48,7 @@ static bool trans_flw(DisasContext *ctx, arg_flw *a) REQUIRE_FPU; REQUIRE_EXT(ctx, RVF); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }
@@ -71,7 +71,7 @@ static bool trans_fsw(DisasContext *ctx, arg_fsw *a) REQUIRE_FPU; REQUIRE_EXT(ctx, RVF); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }
target/riscv/insn_trans/trans_rvi.c.inc +4 -4
@@ -392,7 +392,7 @@ static bool gen_load_i128(DisasContext *ctx, arg_lb *a, MemOp memop) } } else { tcg_gen_qemu_ld_i128(t16, addrl, ctx->mem_idx, memop); - if (mo_endian(ctx) == MO_LE) { + if (ctx->mo_endianness == MO_LE) { tcg_gen_extr_i128_i64(tl, th, t16); } else { tcg_gen_extr_i128_i64(th, tl, t16);
@@ -409,7 +409,7 @@ static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) { bool out; - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }
@@ -508,7 +508,7 @@ static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop) tcg_gen_ext_tl_i64(tl, src2l); tcg_gen_ext_tl_i64(th, src2h); - if (mo_endian(ctx) == MO_LE) { + if (ctx->mo_endianness == MO_LE) { tcg_gen_concat_i64_i128(t16, tl, th); } else { tcg_gen_concat_i64_i128(t16, th, tl);
@@ -520,7 +520,7 @@ static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop) static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) { - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }
target/riscv/insn_trans/trans_rvzacas.c.inc +2 -2
@@ -76,7 +76,7 @@ static bool gen_cmpxchg64(DisasContext *ctx, arg_atomic *a, MemOp mop) TCGv src1 = get_address(ctx, a->rs1, 0); TCGv_i64 src2 = get_gpr_pair(ctx, a->rs2); - mop |= mo_endian(ctx); + mop |= ctx->mo_endianness; decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO); tcg_gen_atomic_cmpxchg_i64(dest, src1, dest, src2, ctx->mem_idx, mop);
@@ -121,7 +121,7 @@ static bool trans_amocas_q(DisasContext *ctx, arg_amocas_q *a) TCGv_i64 desth = get_gpr(ctx, a->rd == 0 ? 0 : a->rd + 1, EXT_NONE); MemOp memop = MO_ALIGN | MO_UO; - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_concat_i64_i128(src2, src2l, src2h); tcg_gen_concat_i64_i128(dest, destl, desth); decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
target/riscv/insn_trans/trans_rvzalasr.c.inc +2 -2
@@ -29,7 +29,7 @@ static bool gen_load_acquire(DisasContext *ctx, arg_lb_aqrl *a, MemOp memop) return false; } - memop |= MO_ALIGN | mo_endian(ctx); + memop |= MO_ALIGN | ctx->mo_endianness; memop |= (ctx->cfg_ptr->ext_zama16b) ? MO_ATOM_WITHIN16 : 0; tcg_gen_qemu_ld_tl(dest, addr, ctx->mem_idx, memop);
@@ -79,7 +79,7 @@ static bool gen_store_release(DisasContext *ctx, arg_sb_aqrl *a, MemOp memop) return false; } - memop |= MO_ALIGN | mo_endian(ctx); + memop |= MO_ALIGN | ctx->mo_endianness; memop |= (ctx->cfg_ptr->ext_zama16b) ? MO_ATOM_WITHIN16 : 0; /* Add a memory barrier implied by RL (mandatory) and AQ (optional) */
target/riscv/insn_trans/trans_rvzce.c.inc +2 -2
@@ -185,7 +185,7 @@ static bool gen_pop(DisasContext *ctx, arg_cmpp *a, bool ret, bool ret_val) tcg_gen_addi_tl(addr, sp, stack_adj - reg_size); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; for (i = X_Sn + 11; i >= 0; i--) { if (reg_bitmap & (1 << i)) { TCGv dest = dest_gpr(ctx, i);
@@ -239,7 +239,7 @@ static bool trans_cm_push(DisasContext *ctx, arg_cm_push *a) tcg_gen_subi_tl(addr, sp, reg_size); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; for (i = X_Sn + 11; i >= 0; i--) { if (reg_bitmap & (1 << i)) { TCGv val = get_gpr(ctx, i, EXT_NONE);
target/riscv/insn_trans/trans_rvzfh.c.inc +2 -2
@@ -49,7 +49,7 @@ static bool trans_flh(DisasContext *ctx, arg_flh *a) REQUIRE_FPU; REQUIRE_ZFHMIN_OR_ZFBFMIN(ctx); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; decode_save_opc(ctx, 0); t0 = get_gpr(ctx, a->rs1, EXT_NONE); if (a->imm) {
@@ -74,7 +74,7 @@ static bool trans_fsh(DisasContext *ctx, arg_fsh *a) REQUIRE_FPU; REQUIRE_ZFHMIN_OR_ZFBFMIN(ctx); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; decode_save_opc(ctx, 0); t0 = get_gpr(ctx, a->rs1, EXT_NONE); if (a->imm) {
target/riscv/insn_trans/trans_rvzicfiss.c.inc +2 -2
@@ -105,7 +105,7 @@ static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO); src1 = get_address(ctx, a->rs1, 0); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_atomic_xchg_tl(dest, src1, src2, SS_MMU_INDEX(ctx), memop); gen_set_gpr(ctx, a->rd, dest); return true;
@@ -135,7 +135,7 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a) decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO); src1 = get_address(ctx, a->rs1, 0); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_atomic_xchg_tl(dest, src1, src2, SS_MMU_INDEX(ctx), memop); gen_set_gpr(ctx, a->rd, dest); return true;
target/riscv/insn_trans/trans_xmips.c.inc +4 -4
@@ -47,7 +47,7 @@ static bool trans_ccmov(DisasContext *ctx, arg_ccmov *a) /* Load Doubleword Pair. */ static bool trans_ldp(DisasContext *ctx, arg_ldp *a) { - MemOp memop = MO_SQ | mo_endian(ctx); + MemOp memop = MO_SQ | ctx->mo_endianness; REQUIRE_XMIPSLSP(ctx); REQUIRE_64_OR_128BIT(ctx);
@@ -71,7 +71,7 @@ static bool trans_ldp(DisasContext *ctx, arg_ldp *a) /* Load Word Pair. */ static bool trans_lwp(DisasContext *ctx, arg_lwp *a) { - MemOp memop = MO_SL | mo_endian(ctx); + MemOp memop = MO_SL | ctx->mo_endianness; REQUIRE_XMIPSLSP(ctx);
@@ -94,7 +94,7 @@ static bool trans_lwp(DisasContext *ctx, arg_lwp *a) /* Store Doubleword Pair. */ static bool trans_sdp(DisasContext *ctx, arg_sdp *a) { - MemOp memop = MO_UQ | mo_endian(ctx); + MemOp memop = MO_UQ | ctx->mo_endianness; REQUIRE_XMIPSLSP(ctx); REQUIRE_64_OR_128BIT(ctx);
@@ -116,7 +116,7 @@ static bool trans_sdp(DisasContext *ctx, arg_sdp *a) /* Store Word Pair. */ static bool trans_swp(DisasContext *ctx, arg_swp *a) { - MemOp memop = MO_SL | mo_endian(ctx); + MemOp memop = MO_SL | ctx->mo_endianness; REQUIRE_XMIPSLSP(ctx);
target/riscv/insn_trans/trans_xthead.c.inc +8 -8
@@ -349,7 +349,7 @@ static bool gen_fload_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop, TCGv_i64 rd = cpu_fpr[a->rd]; TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_ld_i64(rd, addr, ctx->mem_idx, memop); if ((memop & MO_SIZE) == MO_32) { gen_nanbox_s(rd, rd);
@@ -370,7 +370,7 @@ static bool gen_fstore_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop, TCGv_i64 rd = cpu_fpr[a->rd]; TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_st_i64(rd, addr, ctx->mem_idx, memop); return true;
@@ -570,7 +570,7 @@ static bool gen_load_inc(DisasContext *ctx, arg_th_meminc *a, MemOp memop, TCGv rd = dest_gpr(ctx, a->rd); TCGv rs1 = get_gpr(ctx, a->rs1, EXT_NONE); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_ld_tl(rd, addr, ctx->mem_idx, memop); tcg_gen_addi_tl(rs1, rs1, imm); gen_set_gpr(ctx, a->rd, rd);
@@ -591,7 +591,7 @@ static bool gen_store_inc(DisasContext *ctx, arg_th_meminc *a, MemOp memop, TCGv data = get_gpr(ctx, a->rd, EXT_NONE); TCGv rs1 = get_gpr(ctx, a->rs1, EXT_NONE); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop); tcg_gen_addi_tl(rs1, rs1, imm); gen_set_gpr(ctx, a->rs1, rs1);
@@ -747,7 +747,7 @@ static bool gen_load_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop, TCGv rd = dest_gpr(ctx, a->rd); TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_ld_tl(rd, addr, ctx->mem_idx, memop); gen_set_gpr(ctx, a->rd, rd);
@@ -765,7 +765,7 @@ static bool gen_store_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop, TCGv data = get_gpr(ctx, a->rd, EXT_NONE); TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop); return true;
@@ -926,7 +926,7 @@ static bool gen_loadpair_tl(DisasContext *ctx, arg_th_pair *a, MemOp memop, addr1 = get_address(ctx, a->rs, imm); addr2 = get_address(ctx, a->rs, memop_size(memop) + imm); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_ld_tl(t1, addr1, ctx->mem_idx, memop); tcg_gen_qemu_ld_tl(t2, addr2, ctx->mem_idx, memop); gen_set_gpr(ctx, a->rd1, t1);
@@ -965,7 +965,7 @@ static bool gen_storepair_tl(DisasContext *ctx, arg_th_pair *a, MemOp memop, addr1 = get_address(ctx, a->rs, imm); addr2 = get_address(ctx, a->rs, memop_size(memop) + imm); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_st_tl(data1, addr1, ctx->mem_idx, memop); tcg_gen_qemu_st_tl(data2, addr2, ctx->mem_idx, memop); return true;
target/riscv/insn_trans/trans_zilsd.c.inc +2 -2
@@ -30,7 +30,7 @@ static bool gen_load_i64(DisasContext *ctx, arg_ld *a) TCGv addr = get_address(ctx, a->rs1, a->imm); TCGv_i64 tmp = tcg_temp_new_i64(); - tcg_gen_qemu_ld_i64(tmp, addr, ctx->mem_idx, MO_SQ | mo_endian(ctx)); + tcg_gen_qemu_ld_i64(tmp, addr, ctx->mem_idx, MO_SQ | ctx->mo_endianness); if (a->rd == 0) { return true;
@@ -85,7 +85,7 @@ static bool gen_store_i64(DisasContext *ctx, arg_sd *a) } else { tcg_gen_concat_tl_i64(tmp, data_low, data_high); } - tcg_gen_qemu_st_i64(tmp, addr, ctx->mem_idx, MO_SQ | mo_endian(ctx)); + tcg_gen_qemu_st_i64(tmp, addr, ctx->mem_idx, MO_SQ | ctx->mo_endianness); return true; }
Signature · 1 line
2.53.0
Re: [PATCH v11 01/12] target/riscv: Initialize DisasContext::mo_endian once
Quoted 7 lines · Philippe Mathieu-Daudé
The data access endianness is constant during a translation block; rather than calling the
mo_endian()method each time, initialize theDisasContext::mo_endiannessfield once inTranslatorOps::init_disas_context().Signed-off-by Djordje Todorovic <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 16 lines
--- target/riscv/translate.c | 9 ++++++--- target/riscv/insn_trans/trans_rva.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvd.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvf.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvi.c.inc | 8 ++++---- target/riscv/insn_trans/trans_rvzacas.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzalasr.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzce.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzfh.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 4 ++-- target/riscv/insn_trans/trans_xmips.c.inc | 8 ++++---- target/riscv/insn_trans/trans_xthead.c.inc | 16 ++++++++-------- target/riscv/insn_trans/trans_zilsd.c.inc | 4 ++-- 13 files changed, 40 insertions(+), 37 deletions(-)
Re: [PATCH v11 01/12] target/riscv: Initialize DisasContext::mo_endian once
In reply to Philippe Mathieu-DaudéQuoted 8 lines · Philippe Mathieu-Daudé
The data access endianness is constant during a translation block; rather than calling the
mo_endian()method each time, initialize theDisasContext::mo_endiannessfield once inTranslatorOps::init_disas_context().Signed-off-by Djordje Todorovic <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 422 lines
--- target/riscv/translate.c | 9 ++++++--- target/riscv/insn_trans/trans_rva.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvd.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvf.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvi.c.inc | 8 ++++---- target/riscv/insn_trans/trans_rvzacas.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzalasr.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzce.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzfh.c.inc | 4 ++-- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 4 ++-- target/riscv/insn_trans/trans_xmips.c.inc | 8 ++++---- target/riscv/insn_trans/trans_xthead.c.inc | 16 ++++++++-------- target/riscv/insn_trans/trans_zilsd.c.inc | 4 ++-- 13 files changed, 40 insertions(+), 37 deletions(-)target/riscv/translate.c +6
-3@@ -120,6 +120,8 @@ typedef struct DisasContext {bool fcfi_lp_expected;/* zicfiss extension, if shadow stack was enabled during TB gen */ bool bcfi_enabled; +/* Data endianness from MSTATUS UBE/SBE/MBE */ +MemOp mo_endianness; } DisasContext;static inline bool has_ext (DisasContext *ctx,uint32_t ext)@@ -155,7 +157,7 @@ static inline MemOp mo_endian(DisasContext *ctx)#define get_address_xl (ctx) ((ctx)->address_xl)#endif -#define mxl_memop (ctx) ((get_xl(ctx) + 1) | mo_endian(ctx)) +#define mxl_memop (ctx) ((get_xl(ctx) + 1) | (ctx)->mo_endianness)/* The word size for this machine mode. */ static inline int __attribute__((unused))get_xlen (DisasContext *ctx)@@ -1156,7 +1158,7 @@ static bool gen_amo(DisasContext *ctx, arg_atomic *a,TCGv src1, src2 =get_gpr (ctx, a->rs2,EXT_NONE );MemOp size = mop &MO_SIZE ; - mop |=mo_endian (ctx); + mop |= ctx->mo_endianness;if (ctx->cfg_ptr->ext_zama16b && size >=MO_32 ) { mop |=MO_ATOM_WITHIN16 ; }else {@@ -1177,7 +1179,7 @@ static bool gen_cmpxchg(DisasContext *ctx, arg_atomic *a, MemOp mop)TCGv src1 =get_address (ctx, a->rs1,0 );TCGv src2 =get_gpr (ctx, a->rs2,EXT_NONE ); - mop |=mo_endian (ctx); + mop |= ctx->mo_endianness;decode_save_opc (ctx,RISCV_UW2_ALWAYS_STORE_AMO );tcg_gen_atomic_cmpxchg_tl (dest, src1, dest, src2, ctx->mem_idx, mop);@@ -1360,6 +1362,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->zero =tcg_constant_tl (0 ); ctx->virt_inst_excp = false; ctx->decoders = cpu->decoders; + ctx->mo_endianness =mo_endian (ctx); }static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu)target/riscv/insn_trans/trans_rva.c.inc +2
-2@@ -35,7 +35,7 @@ static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) TCGv src1; mop |= MO_ALIGN; - mop |= mo_endian(ctx); + mop |= ctx->mo_endianness; decode_save_opc(ctx, 0); src1 = get_address(ctx, a->rs1, 0);@@ -65,7 +65,7 @@ static bool gen_sc(DisasContext *ctx, arg_atomic *a, MemOp mop) TCGLabel *l2 = gen_new_label(); mop |= MO_ALIGN; - mop |= mo_endian(ctx); + mop |= ctx->mo_endianness; decode_save_opc(ctx, 0); src1 = get_address(ctx, a->rs1, 0);target/riscv/insn_trans/trans_rvd.c.inc +2
-2@@ -60,7 +60,7 @@ static bool trans_fld(DisasContext *ctx, arg_fld *a) } else { memop |= MO_ATOM_IFALIGN; } - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; decode_save_opc(ctx, 0); addr = get_address(ctx, a->rs1, a->imm);@@ -85,7 +85,7 @@ static bool trans_fsd(DisasContext *ctx, arg_fsd *a) } else { memop |= MO_ATOM_IFALIGN; } - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; decode_save_opc(ctx, 0); addr = get_address(ctx, a->rs1, a->imm);target/riscv/insn_trans/trans_rvf.c.inc +2
-2@@ -48,7 +48,7 @@ static bool trans_flw(DisasContext *ctx, arg_flw *a) REQUIRE_FPU; REQUIRE_EXT(ctx, RVF); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }@@ -71,7 +71,7 @@ static bool trans_fsw(DisasContext *ctx, arg_fsw *a) REQUIRE_FPU; REQUIRE_EXT(ctx, RVF); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }target/riscv/insn_trans/trans_rvi.c.inc +4
-4@@ -392,7 +392,7 @@ static bool gen_load_i128(DisasContext *ctx, arg_lb *a, MemOp memop) } } else { tcg_gen_qemu_ld_i128(t16, addrl, ctx->mem_idx, memop); - if (mo_endian(ctx) == MO_LE) { + if (ctx->mo_endianness == MO_LE) { tcg_gen_extr_i128_i64(tl, th, t16); } else { tcg_gen_extr_i128_i64(th, tl, t16);@@ -409,7 +409,7 @@ static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) { bool out; - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }@@ -508,7 +508,7 @@ static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop) tcg_gen_ext_tl_i64(tl, src2l); tcg_gen_ext_tl_i64(th, src2h); - if (mo_endian(ctx) == MO_LE) { + if (ctx->mo_endianness == MO_LE) { tcg_gen_concat_i64_i128(t16, tl, th); } else { tcg_gen_concat_i64_i128(t16, th, tl);@@ -520,7 +520,7 @@ static bool gen_store_i128(DisasContext *ctx, arg_sb *a, MemOp memop) static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) { - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; }target/riscv/insn_trans/trans_rvzacas.c.inc +2
-2@@ -76,7 +76,7 @@ static bool gen_cmpxchg64(DisasContext *ctx, arg_atomic *a, MemOp mop) TCGv src1 = get_address(ctx, a->rs1, 0); TCGv_i64 src2 = get_gpr_pair(ctx, a->rs2); - mop |= mo_endian(ctx); + mop |= ctx->mo_endianness; decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO); tcg_gen_atomic_cmpxchg_i64(dest, src1, dest, src2, ctx->mem_idx, mop);@@ -121,7 +121,7 @@ static bool trans_amocas_q(DisasContext *ctx, arg_amocas_q *a) TCGv_i64 desth = get_gpr(ctx, a->rd == 0 ? 0 : a->rd + 1, EXT_NONE); MemOp memop = MO_ALIGN | MO_UO; - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_concat_i64_i128(src2, src2l, src2h); tcg_gen_concat_i64_i128(dest, destl, desth); decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);target/riscv/insn_trans/trans_rvzalasr.c.inc +2
-2@@ -29,7 +29,7 @@ static bool gen_load_acquire(DisasContext *ctx, arg_lb_aqrl *a, MemOp memop) return false; } - memop |= MO_ALIGN | mo_endian(ctx); + memop |= MO_ALIGN | ctx->mo_endianness; memop |= (ctx->cfg_ptr->ext_zama16b) ? MO_ATOM_WITHIN16 : 0; tcg_gen_qemu_ld_tl(dest, addr, ctx->mem_idx, memop);@@ -79,7 +79,7 @@ static bool gen_store_release(DisasContext *ctx, arg_sb_aqrl *a, MemOp memop) return false; } - memop |= MO_ALIGN | mo_endian(ctx); + memop |= MO_ALIGN | ctx->mo_endianness; memop |= (ctx->cfg_ptr->ext_zama16b) ? MO_ATOM_WITHIN16 : 0; /* Add a memory barrier implied by RL (mandatory) and AQ (optional) */target/riscv/insn_trans/trans_rvzce.c.inc +2
-2@@ -185,7 +185,7 @@ static bool gen_pop(DisasContext *ctx, arg_cmpp *a, bool ret, bool ret_val) tcg_gen_addi_tl(addr, sp, stack_adj - reg_size); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; for (i = X_Sn + 11; i >= 0; i--) { if (reg_bitmap & (1 << i)) { TCGv dest = dest_gpr(ctx, i);@@ -239,7 +239,7 @@ static bool trans_cm_push(DisasContext *ctx, arg_cm_push *a) tcg_gen_subi_tl(addr, sp, reg_size); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; for (i = X_Sn + 11; i >= 0; i--) { if (reg_bitmap & (1 << i)) { TCGv val = get_gpr(ctx, i, EXT_NONE);target/riscv/insn_trans/trans_rvzfh.c.inc +2
-2@@ -49,7 +49,7 @@ static bool trans_flh(DisasContext *ctx, arg_flh *a) REQUIRE_FPU; REQUIRE_ZFHMIN_OR_ZFBFMIN(ctx); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; decode_save_opc(ctx, 0); t0 = get_gpr(ctx, a->rs1, EXT_NONE); if (a->imm) {@@ -74,7 +74,7 @@ static bool trans_fsh(DisasContext *ctx, arg_fsh *a) REQUIRE_FPU; REQUIRE_ZFHMIN_OR_ZFBFMIN(ctx); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; decode_save_opc(ctx, 0); t0 = get_gpr(ctx, a->rs1, EXT_NONE); if (a->imm) {target/riscv/insn_trans/trans_rvzicfiss.c.inc +2
-2@@ -105,7 +105,7 @@ static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO); src1 = get_address(ctx, a->rs1, 0); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_atomic_xchg_tl(dest, src1, src2, SS_MMU_INDEX(ctx), memop); gen_set_gpr(ctx, a->rd, dest); return true;@@ -135,7 +135,7 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a) decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO); src1 = get_address(ctx, a->rs1, 0); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_atomic_xchg_tl(dest, src1, src2, SS_MMU_INDEX(ctx), memop); gen_set_gpr(ctx, a->rd, dest); return true;target/riscv/insn_trans/trans_xmips.c.inc +4
-4@@ -47,7 +47,7 @@ static bool trans_ccmov(DisasContext *ctx, arg_ccmov *a) /* Load Doubleword Pair. */ static bool trans_ldp(DisasContext *ctx, arg_ldp *a) { - MemOp memop = MO_SQ | mo_endian(ctx); + MemOp memop = MO_SQ | ctx->mo_endianness; REQUIRE_XMIPSLSP(ctx); REQUIRE_64_OR_128BIT(ctx);@@ -71,7 +71,7 @@ static bool trans_ldp(DisasContext *ctx, arg_ldp *a) /* Load Word Pair. */ static bool trans_lwp(DisasContext *ctx, arg_lwp *a) { - MemOp memop = MO_SL | mo_endian(ctx); + MemOp memop = MO_SL | ctx->mo_endianness; REQUIRE_XMIPSLSP(ctx);@@ -94,7 +94,7 @@ static bool trans_lwp(DisasContext *ctx, arg_lwp *a) /* Store Doubleword Pair. */ static bool trans_sdp(DisasContext *ctx, arg_sdp *a) { - MemOp memop = MO_UQ | mo_endian(ctx); + MemOp memop = MO_UQ | ctx->mo_endianness; REQUIRE_XMIPSLSP(ctx); REQUIRE_64_OR_128BIT(ctx);@@ -116,7 +116,7 @@ static bool trans_sdp(DisasContext *ctx, arg_sdp *a) /* Store Word Pair. */ static bool trans_swp(DisasContext *ctx, arg_swp *a) { - MemOp memop = MO_SL | mo_endian(ctx); + MemOp memop = MO_SL | ctx->mo_endianness; REQUIRE_XMIPSLSP(ctx);target/riscv/insn_trans/trans_xthead.c.inc +8
-8@@ -349,7 +349,7 @@ static bool gen_fload_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop, TCGv_i64 rd = cpu_fpr[a->rd]; TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_ld_i64(rd, addr, ctx->mem_idx, memop); if ((memop & MO_SIZE) == MO_32) { gen_nanbox_s(rd, rd);@@ -370,7 +370,7 @@ static bool gen_fstore_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop, TCGv_i64 rd = cpu_fpr[a->rd]; TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_st_i64(rd, addr, ctx->mem_idx, memop); return true;@@ -570,7 +570,7 @@ static bool gen_load_inc(DisasContext *ctx, arg_th_meminc *a, MemOp memop, TCGv rd = dest_gpr(ctx, a->rd); TCGv rs1 = get_gpr(ctx, a->rs1, EXT_NONE); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_ld_tl(rd, addr, ctx->mem_idx, memop); tcg_gen_addi_tl(rs1, rs1, imm); gen_set_gpr(ctx, a->rd, rd);@@ -591,7 +591,7 @@ static bool gen_store_inc(DisasContext *ctx, arg_th_meminc *a, MemOp memop, TCGv data = get_gpr(ctx, a->rd, EXT_NONE); TCGv rs1 = get_gpr(ctx, a->rs1, EXT_NONE); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop); tcg_gen_addi_tl(rs1, rs1, imm); gen_set_gpr(ctx, a->rs1, rs1);@@ -747,7 +747,7 @@ static bool gen_load_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop, TCGv rd = dest_gpr(ctx, a->rd); TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_ld_tl(rd, addr, ctx->mem_idx, memop); gen_set_gpr(ctx, a->rd, rd);@@ -765,7 +765,7 @@ static bool gen_store_idx(DisasContext *ctx, arg_th_memidx *a, MemOp memop, TCGv data = get_gpr(ctx, a->rd, EXT_NONE); TCGv addr = get_th_address_indexed(ctx, a->rs1, a->rs2, a->imm2, zext_offs); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop); return true;@@ -926,7 +926,7 @@ static bool gen_loadpair_tl(DisasContext *ctx, arg_th_pair *a, MemOp memop, addr1 = get_address(ctx, a->rs, imm); addr2 = get_address(ctx, a->rs, memop_size(memop) + imm); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_ld_tl(t1, addr1, ctx->mem_idx, memop); tcg_gen_qemu_ld_tl(t2, addr2, ctx->mem_idx, memop); gen_set_gpr(ctx, a->rd1, t1);@@ -965,7 +965,7 @@ static bool gen_storepair_tl(DisasContext *ctx, arg_th_pair *a, MemOp memop, addr1 = get_address(ctx, a->rs, imm); addr2 = get_address(ctx, a->rs, memop_size(memop) + imm); - memop |= mo_endian(ctx); + memop |= ctx->mo_endianness; tcg_gen_qemu_st_tl(data1, addr1, ctx->mem_idx, memop); tcg_gen_qemu_st_tl(data2, addr2, ctx->mem_idx, memop); return true;target/riscv/insn_trans/trans_zilsd.c.inc +2
-3@@ -30,7 +30,7 @@ static bool gen_load_i64(DisasContext *ctx, arg_ld *a) TCGv addr = get_address(ctx, a->rs1, a->imm); TCGv_i64 tmp = tcg_temp_new_i64(); - tcg_gen_qemu_ld_i64(tmp, addr, ctx->mem_idx, MO_SQ | mo_endian(ctx)); + tcg_gen_qemu_ld_i64(tmp, addr, ctx->mem_idx, MO_SQ | ctx->mo_endianness); if (a->rd == 0) { return true;@@ -85,7 +85,7 @@ static bool gen_store_i64(DisasContext *ctx, arg_sd *a) } else { tcg_gen_concat_tl_i64(tmp, data_low, data_high); } - tcg_gen_qemu_st_i64(tmp, addr, ctx->mem_idx, MO_SQ | mo_endian(ctx)); + tcg_gen_qemu_st_i64(tmp, addr, ctx->mem_idx, MO_SQ | ctx->mo_endianness); return true; } --2.53.0
PATCH v11 2/12 target/riscv: Implement runtime data endianness via MSTATUS bits
In reply to Philippe Mathieu-DaudéFrom: Djordje Todorovic <[email protected]>
Make data accesses honour the MSTATUS MBE/SBE/UBE endianness bits instead of being hardcoded to little-endian. Update mo_endian_env() to pick the bit corresponding to the current privilege level (MBE for M, SBE for S, UBE for U). Remove the now unused mo_endian() helper.
Note, TB_FLAGS has no free bits, so the data endianness is carried in the extended RISC-V TB flags stored in cs_base. It uses EXT_TB_FLAGS.BIG_ENDIAN at bit 33, leaving bit 32 for EXT_TB_FLAGS.ALTFMT. This keys TBs correctly on the current data endianness.
Instruction fetches remain MO_LE unconditionally; RISC-V instructions are always little-endian per the ISA specification. Update the disassembler comment to clarify that BFD_ENDIAN_LITTLE is correct.
--- target/riscv/cpu.h | 1 + target/riscv/internals.h | 29 +++++++++++++++++++++-------- target/riscv/cpu.c | 7 ++----- target/riscv/tcg/tcg-cpu.c | 2 ++ target/riscv/translate.c | 15 ++------------- 5 files changed, 28 insertions(+), 26 deletions(-)
target/riscv/cpu.h +1 -0
@@ -706,6 +706,7 @@ FIELD(TB_FLAGS, PM_SIGNEXTEND, 31, 1) FIELD(EXT_TB_FLAGS, MISA_EXT, 0, 32) FIELD(EXT_TB_FLAGS, ALTFMT, 32, 1) +FIELD(EXT_TB_FLAGS, BIG_ENDIAN, 33, 1)#ifdef TARGET_RISCV32 #define riscv_cpu_mxl (env) ((void)(env), MXL_RV32)
target/riscv/internals.h +21 -8
@@ -62,16 +62,29 @@ static inline bool mmuidx_2stage(int mmu_idx)return mmu_idx &MMU_2STAGE_BIT ; } +/* +* Return the endianness for the current privilege +* level, based on the MSTATUS MBE/SBE/UBE bits. +*/ static inline MemOp mo_endian_env (CPURISCVState *env) { -/* -* A couple of bits in MSTATUS set the endianness: -* - MSTATUS_UBE (User-mode), -* - MSTATUS_SBE (Supervisor-mode), -* - MSTATUS_MBE (Machine-mode) -* but we don't implement that yet. -*/ -return MO_LE ; +bool be = false; +#if !defined(CONFIG_USER_ONLY ) +switch (env->priv) { +case PRV_M : + be = env->mstatus &MSTATUS_MBE ; +break ; +case PRV_S : + be = env->mstatus &MSTATUS_SBE ; +break ; +case PRV_U : + be = env->mstatus &MSTATUS_UBE ; +break ; +default : +g_assert_not_reached (); + } +#endif +return be ?MO_BE :MO_LE ; }/* share data between vector helpers and decode code */
target/riscv/cpu.c +2 -5
@@ -830,11 +830,8 @@ static void riscv_cpu_disas_set_info(const CPUState *s, disassemble_info *info) info->target_info = &cpu->cfg;/* -* A couple of bits in MSTATUS set the endianness: -* - MSTATUS_UBE (User-mode), -* - MSTATUS_SBE (Supervisor-mode), -* - MSTATUS_MBE (Machine-mode) -* but we don't implement that yet. +* RISC-V instructions are always little-endian, regardless of the +* data endianness configured via MSTATUS UBE/SBE/MBE bits. */ info->endian =BFD_ENDIAN_LITTLE ;
target/riscv/tcg/tcg-cpu.c +2 -0
@@ -193,6 +193,8 @@ static TCGTBCPUState riscv_get_tb_cpu_state(CPUState *cs) flags =FIELD_DP32 (flags,TB_FLAGS ,PM_SIGNEXTEND , pm_signext); ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,MISA_EXT , env->misa_ext); + ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,BIG_ENDIAN , +mo_endian_env (env) ==MO_BE );return (TCGTBCPUState ){ .pc = env->xl ==MXL_RV32 ? env->pc &UINT32_MAX : env->pc,
target/riscv/translate.c +2 -13
@@ -129,18 +129,6 @@ static inline bool has_ext(DisasContext *ctx, uint32_t ext)return ctx->misa_ext & ext; } -static inline MemOp mo_endian (DisasContext *ctx) -{ -/* -* A couple of bits in MSTATUS set the endianness: -* - MSTATUS_UBE (User-mode), -* - MSTATUS_SBE (Supervisor-mode), -* - MSTATUS_MBE (Machine-mode) -* but we don't implement that yet. -*/ -return MO_LE ; -} -#ifdef TARGET_RISCV32 #define get_xl (ctx) MXL_RV32#elif defined(CONFIG_USER_ONLY )
@@ -1362,7 +1350,8 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->zero =tcg_constant_tl (0 ); ctx->virt_inst_excp = false; ctx->decoders = cpu->decoders; - ctx->mo_endianness =mo_endian (ctx); + ctx->mo_endianness =FIELD_EX64 (ext_tb_flags,EXT_TB_FLAGS ,BIG_ENDIAN ) + ?MO_BE :MO_LE ; }static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu)
Signature · 1 line
2.53.0
Re: [PATCH v11 02/12] target/riscv: Implement runtime data endianness via MSTATUS bits
Quoted 21 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Make data accesses honour the MSTATUS MBE/SBE/UBE endianness bits instead of being hardcoded to little-endian. Update
mo_endian_env()to pick the bit corresponding to the current privilege level (MBE for M, SBE for S, UBE for U). Remove the now unusedmo_endian()helper.Note,
TB_FLAGShas no free bits, so the data endianness is carried in the extended RISC-V TB flags stored incs_base. It uses EXT_TB_FLAGS.BIG_ENDIAN at bit 33, leaving bit 32 for EXT_TB_FLAGS.ALTFMT. This keys TBs correctly on the current data endianness.Instruction fetches remain
MO_LEunconditionally; RISC-V instructions are always little-endian per the ISA specification. Update the disassembler comment to clarify thatBFD_ENDIAN_LITTLEis correct.Signed-off-by Djordje Todorovic <[email protected]>Co-developed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 8 lines
--- target/riscv/cpu.h | 1 + target/riscv/internals.h | 29 +++++++++++++++++++++-------- target/riscv/cpu.c | 7 ++----- target/riscv/tcg/tcg-cpu.c | 2 ++ target/riscv/translate.c | 15 ++------------- 5 files changed, 28 insertions(+), 26 deletions(-)
Patch 1 makes more sense with this one, but order is not too important.
Re: [PATCH v11 02/12] target/riscv: Implement runtime data endianness via MSTATUS bits
Quoted 31 lines · Pierrick Bouvier
Quoted 21 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Make data accesses honour the MSTATUS MBE/SBE/UBE endianness bits instead of being hardcoded to little-endian. Update
mo_endian_env()to pick the bit corresponding to the current privilege level (MBE for M, SBE for S, UBE for U). Remove the now unusedmo_endian()helper.Note,
TB_FLAGShas no free bits, so the data endianness is carried in the extended RISC-V TB flags stored incs_base. It uses EXT_TB_FLAGS.BIG_ENDIAN at bit 33, leaving bit 32 for EXT_TB_FLAGS.ALTFMT. This keys TBs correctly on the current data endianness.Instruction fetches remain
MO_LEunconditionally; RISC-V instructions are always little-endian per the ISA specification. Update the disassembler comment to clarify thatBFD_ENDIAN_LITTLEis correct.Signed-off-by Djordje Todorovic <[email protected]>Co-developed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>Quoted 8 lines
--- target/riscv/cpu.h | 1 + target/riscv/internals.h | 29 +++++++++++++++++++++-------- target/riscv/cpu.c | 7 ++----- target/riscv/tcg/tcg-cpu.c | 2 ++ target/riscv/translate.c | 15 ++------------- 5 files changed, 28 insertions(+), 26 deletions(-)
Quoted 1 line
Patch 1 makes more sense with this one, but order is not too important.
I can reword the previous patch, amending something like "This will allow next commit to change mo_endian() in a single place".
Quoted 2 lines
Reviewed-by Pierrick Bouvier <[email protected]>
Re: [PATCH v11 02/12] target/riscv: Implement runtime data endianness via MSTATUS bits
In reply to Philippe Mathieu-DaudéQuoted 15 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Make data accesses honour the MSTATUS MBE/SBE/UBE endianness bits instead of being hardcoded to little-endian. Update
mo_endian_env()to pick the bit corresponding to the current privilege level (MBE for M, SBE for S, UBE for U). Remove the now unusedmo_endian()helper.Note,
TB_FLAGShas no free bits, so the data endianness is carried in the extended RISC-V TB flags stored incs_base. It uses EXT_TB_FLAGS.BIG_ENDIAN at bit 33, leaving bit 32 for EXT_TB_FLAGS.ALTFMT. This keys TBs correctly on the current data endianness.
Quoted 7 lines
Instruction fetches remain
MO_LEunconditionally; RISC-V instructions are always little-endian per the ISA specification. Update the disassembler comment to clarify thatBFD_ENDIAN_LITTLEis correct.Signed-off-by Djordje Todorovic <[email protected]>Co-developed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 130 lines
--- target/riscv/cpu.h | 1 + target/riscv/internals.h | 29 +++++++++++++++++++++-------- target/riscv/cpu.c | 7 ++----- target/riscv/tcg/tcg-cpu.c | 2 ++ target/riscv/translate.c | 15 ++------------- 5 files changed, 28 insertions(+), 26 deletions(-)target/riscv/cpu.h +1
-0@@ -706,6 +706,7 @@ FIELD(TB_FLAGS, PM_SIGNEXTEND, 31, 1) FIELD(EXT_TB_FLAGS, MISA_EXT, 0, 32) FIELD(EXT_TB_FLAGS, ALTFMT, 32, 1) +FIELD(EXT_TB_FLAGS, BIG_ENDIAN, 33, 1)#ifdef TARGET_RISCV32 #define riscv_cpu_mxl (env) ((void)(env), MXL_RV32)target/riscv/internals.h +21
-8@@ -62,16 +62,29 @@ static inline bool mmuidx_2stage(int mmu_idx)return mmu_idx &MMU_2STAGE_BIT ; } +/* +* Return the endianness for the current privilege +* level, based on the MSTATUS MBE/SBE/UBE bits. +*/ static inline MemOp mo_endian_env (CPURISCVState *env) { -/* -* A couple of bits in MSTATUS set the endianness: -* - MSTATUS_UBE (User-mode), -* - MSTATUS_SBE (Supervisor-mode), -* - MSTATUS_MBE (Machine-mode) -* but we don't implement that yet. -*/ -return MO_LE ; +bool be = false; +#if !defined(CONFIG_USER_ONLY ) +switch (env->priv) { +case PRV_M : + be = env->mstatus &MSTATUS_MBE ; +break ; +case PRV_S : + be = env->mstatus &MSTATUS_SBE ; +break ; +case PRV_U : + be = env->mstatus &MSTATUS_UBE ; +break ; +default : +g_assert_not_reached (); + } +#endif +return be ?MO_BE :MO_LE ; }/* share data between vector helpers and decode code */ target/riscv/cpu.c +2
-5@@ -830,11 +830,8 @@ static void riscv_cpu_disas_set_info(const CPUState *s, disassemble_info *info) info->target_info = &cpu->cfg;/* -* A couple of bits in MSTATUS set the endianness: -* - MSTATUS_UBE (User-mode), -* - MSTATUS_SBE (Supervisor-mode), -* - MSTATUS_MBE (Machine-mode) -* but we don't implement that yet. +* RISC-V instructions are always little-endian, regardless of the +* data endianness configured via MSTATUS UBE/SBE/MBE bits. */ info->endian =BFD_ENDIAN_LITTLE ;target/riscv/tcg/tcg-cpu.c +2
-0@@ -193,6 +193,8 @@ static TCGTBCPUState riscv_get_tb_cpu_state(CPUState *cs) flags =FIELD_DP32 (flags,TB_FLAGS ,PM_SIGNEXTEND , pm_signext); ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,MISA_EXT , env->misa_ext); + ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,BIG_ENDIAN , +mo_endian_env (env) ==MO_BE );return (TCGTBCPUState ){ .pc = env->xl ==MXL_RV32 ? env->pc &UINT32_MAX : env->pc,target/riscv/translate.c +2
-14@@ -129,18 +129,6 @@ static inline bool has_ext(DisasContext *ctx, uint32_t ext)return ctx->misa_ext & ext; } -static inline MemOp mo_endian (DisasContext *ctx) -{ -/* -* A couple of bits in MSTATUS set the endianness: -* - MSTATUS_UBE (User-mode), -* - MSTATUS_SBE (Supervisor-mode), -* - MSTATUS_MBE (Machine-mode) -* but we don't implement that yet. -*/ -return MO_LE ; -} -#ifdef TARGET_RISCV32 #define get_xl (ctx) MXL_RV32#elif defined(CONFIG_USER_ONLY )@@ -1362,7 +1350,8 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->zero =tcg_constant_tl (0 ); ctx->virt_inst_excp = false; ctx->decoders = cpu->decoders; - ctx->mo_endianness =mo_endian (ctx); + ctx->mo_endianness =FIELD_EX64 (ext_tb_flags,EXT_TB_FLAGS ,BIG_ENDIAN ) + ?MO_BE :MO_LE ; }static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu) --2.53.0
PATCH v11 3/12 target/riscv: Assert PTE.N/PBMT bits are not expected on RV32
In reply to Philippe Mathieu-DaudéPTE_N and PTE_PBMT are undefined on RV64, assert we are not under RV32.
--- target/riscv/cpu_helper.c | 2 ++ 1 file changed, 2 insertions(+)
target/riscv/cpu_helper.c +2 -0
@@ -1490,6 +1490,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,return TRANSLATE_FAIL ; }if (!pbmte && (pte &PTE_PBMT )) { +assert (riscv_cpu_sxl (env) !=MXL_RV32 );/* Reserved without Svpbmt. */ qemu_log_mask (LOG_GUEST_ERROR ,"%s: PBMT bits set in PTE, " "and Svpbmt extension is disabled: "
@@ -1643,6 +1644,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,target_ulong vpn = addr >>PGSHIFT ;if (riscv_cpu_cfg (env)->ext_svnapot && (pte &PTE_N )) { +assert (riscv_cpu_sxl (env) !=MXL_RV32 ); napot_bits =ctzl (ppn) +1 ;if ((i != (levels -1 )) || (napot_bits !=4 )) {return TRANSLATE_FAIL ;
Signature · 1 line
2.53.0
Re: [PATCH v11 03/12] target/riscv: Assert PTE.N/PBMT bits are not expected on RV32
Quoted 8 lines · Philippe Mathieu-Daudé
PTE_NandPTE_PBMTare undefined on RV64, assert we are not under RV32.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/riscv/cpu_helper.c | 2 ++ 1 file changed, 2 insertions(+)
Re: [PATCH v11 03/12] target/riscv: Assert PTE.N/PBMT bits are not expected on RV32
In reply to Philippe Mathieu-DaudéQuoted 3 lines · Philippe Mathieu-Daudé
PTE_NandPTE_PBMTare undefined on RV64, assert we are not under RV32.
I didn't find this info in the RV priv ISA. Can you please quote the except it in the commit msg?
Maybe you meant "undefined on RV32"?
Quoted 10 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/riscv/cpu_helper.c | 2 ++ 1 file changed, 2 insertions(+)target/riscv/cpu_helper.c +0
-0
target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState *env, hwaddr *physical, · line 1493
Quoted 4 lines
target/riscv/cpu_helper.c +1
-0@@ -1490,6 +1490,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,return TRANSLATE_FAIL ; }if (!pbmte && (pte &PTE_PBMT )) { +assert (riscv_cpu_sxl (env) !=MXL_RV32 );
What I understood from the RV priv ISA w.r.t PTE_PMBT is that it doesn't have much to do with SXLEN/riscv_cpu_sxl(). PTE_PBMT is a bits 62|61 field that determine override mem attributes when we have the 'svpbmt' extension enabled. 'svpbmt' is dependent on satp_mode sv39 or greater, which happens to be riscv64 only.
I didn't see any restrictions of SXLEN=32 in the 'svpbmt' chapter in the RISC-V ISA, e.g. PTE_PMBT set to a non-zero val and SXLEN=32 seems to be a valid combo - a riscv64 target running sv39+ can use svpbmt to override PTE properties in a SXLEN=32 context.
Now, PTE_PBMT is not writable when MXLEN=32 (i.e. riscv_cpu_mxl() == MXL_RV32), and maybe this is what you're trying to do. In this case the assert might be justifiable, although we just landed a recent fix in Alistair's tree that disables svpbmt for riscv32 entirely, and this also means that PTE_PBMT won't ever be set. But if the assert (with MXL, not SXL) would give some sort of guarantee aside from the bit being set, by all means go ahead.
target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState *env, hwaddr *physical, · line 1647
Quoted 8 lines
target/riscv/cpu_helper.c +1
-0/* Reserved without Svpbmt. */ qemu_log_mask (LOG_GUEST_ERROR ,"%s: PBMT bits set in PTE, " "and Svpbmt extension is disabled: "@@ -1643,6 +1644,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,target_ulong vpn = addr >>PGSHIFT ;if (riscv_cpu_cfg (env)->ext_svnapot && (pte &PTE_N )) { +assert (riscv_cpu_sxl (env) !=MXL_RV32 );
All the stuff I said above happens to be applicable for PTE_N. This is a bit that indicates if the PTE represents a "naturally aligned power-of-2" range, or NAPOT range, and this is specified by another extension called 'svnapot'. This extension is also sv39+ exclusive, which means riscv64 exclusive, and it doesn't seem to have any restrictions with SXLEN=32.
Thanks,
Daniel
Quoted 3 lines
target/riscv/cpu_helper.c +0
-0napot_bits =ctzl (ppn) +1 ;if ((i != (levels -1 )) || (napot_bits !=4 )) {return TRANSLATE_FAIL ;
Re: [PATCH v11 03/12] target/riscv: Assert PTE.N/PBMT bits are not expected on RV32
Quoted 11 lines · Daniel Henrique Barboza
Quoted 3 lines · Philippe Mathieu-Daudé
PTE_NandPTE_PBMTare undefined on RV64, assert we are not under RV32.I didn't find this info in the RV priv ISA. Can you please quote the except it in the commit msg?
Maybe you meant "undefined on RV32"?
Yes :)
Quoted 23 lines
Quoted 10 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
target/riscv/cpu_helper.c| 2 ++1 file changed, 2 insertions(+)target/riscv/cpu_helper.c +0
-0target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState · line 1494
Quoted 5 lines
target/riscv/cpu_helper.c +1
-0@@ -1490,6 +1490,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, return TRANSLATE_FAIL; } if (!pbmte && (pte & PTE_PBMT)) { + assert(riscv_cpu_sxl(env) != MXL_RV32);What I understood from the RV priv ISA w.r.t
PTE_PMBTis that it doesn't have much to do with SXLEN/riscv_cpu_sxl().PTE_PBMTis a bits 62|61 field that determine override mem attributes when we have the 'svpbmt' extension enabled. 'svpbmt' is dependent onsatp_modesv39 or greater, which happens to be riscv64 only.
Quoted 4 lines
I didn't see any restrictions of SXLEN=32 in the 'svpbmt' chapter in the RISC-V ISA, e.g.
PTE_PMBTset to a non-zero val and SXLEN=32 seems to be a valid combo - a riscv64 target running sv39+ can use svpbmt to override PTE properties in a SXLEN=32 context.
It was hard to figure that out by myself.
Quoted 2 lines
Now,
PTE_PBMTis not writable when MXLEN=32 (i.e.riscv_cpu_mxl()==MXL_RV32), and maybe this is what you're trying to do.
Yes.
Quoted 5 lines
In this case the assert might
be justifiable, although we just landed a recent fix in Alistair's tree that disables svpbmt for riscv32 entirely, and this also means thatPTE_PBMTwon't ever be set. But if the assert (with MXL, not SXL) would give some sort of guarantee aside from the bit being set, by all means go ahead.
I'll wait to see that merged before revisiting. This patch is not required for this series, I was trying to make sense of this huge function while reviewing it.
Quoted 7 lines
Quoted 7 lines
target/riscv/cpu_helper.c +0
-0/* Reserved without Svpbmt. */qemu_log_mask(LOG_GUEST_ERROR, "%s: PBMT bits set in PTE, " "and Svpbmt extension is disabled: "
target/riscv/cpu_helper.c +0
-0@@ -1643,6 +1644,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, target_ulong vpn = addr >> PGSHIFT; if (riscv_cpu_cfg(env)->ext_svnapot && (pte & PTE_N)) {
Quoted 8 lines
target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState · line 1647
Quoted 1 line
target/riscv/cpu_helper.c +1
-0+ assert(riscv_cpu_sxl(env) != MXL_RV32);All the stuff I said above happens to be applicable for PTE_N. This is a bit that indicates if the PTE represents a "naturally aligned power-of-2" range, or NAPOT range, and this is specified by another extension called 'svnapot'. This extension is also sv39+ exclusive, which means riscv64 exclusive, and it doesn't seem to have any restrictions with SXLEN=32.
OK. Let's discard this patch :)
Quoted 9 lines
Thanks,
DanielQuoted 3 lines
target/riscv/cpu_helper.c +0
-0napot_bits = ctzl(ppn) + 1; if ((i != (levels - 1)) || (napot_bits != 4)) { return TRANSLATE_FAIL;
PATCH v11 4/12 target/riscv: De-indent some code in get_physical_address()
In reply to Philippe Mathieu-Daudéget_physical_address() is quite complex already. In order to make the two next commits simplers, de-indent one if() ladder. No logical change intended.
--- target/riscv/cpu_helper.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-)
target/riscv/cpu_helper.c +18 -15
@@ -1617,27 +1617,30 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,hwaddr l = sxlen_bytes, addr1; mr =address_space_translate (cs->as, pte_addr, &addr1, &l, false,MEMTXATTRS_UNSPECIFIED ); -if (memory_region_is_ram (mr)) { -target_ulong *pte_pa =qemu_map_ram_ptr (mr->ram_block, addr1); -target_ulong old_pte; -if (riscv_cpu_sxl (env) ==MXL_RV32 ) { - old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa,cpu_to_le32 (pte),cpu_to_le32 (updated_pte)); - old_pte =le32_to_cpu (old_pte); - }else { - old_pte =qatomic_cmpxchg (pte_pa,cpu_to_le64 (pte),cpu_to_le64 (updated_pte)); - old_pte =le64_to_cpu (old_pte); - } -if (old_pte != pte) { - goto restart; - } - pte = updated_pte; - }else { +if (!memory_region_is_ram (mr)) {/* * Misconfigured PTE in ROM (AD bits are not preset) or * PTE is in IO space and can't be updated atomically. */ return TRANSLATE_FAIL ; } + +target_ulong *pte_pa =qemu_map_ram_ptr (mr->ram_block, addr1); +target_ulong old_pte; + +if (riscv_cpu_sxl (env) ==MXL_RV32 ) { + old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa,cpu_to_le32 (pte), +cpu_to_le32 (updated_pte)); + old_pte =le32_to_cpu (old_pte); + }else { + old_pte =qatomic_cmpxchg (pte_pa,cpu_to_le64 (pte), +cpu_to_le64 (updated_pte)); + old_pte =le64_to_cpu (old_pte); + } +if (old_pte != pte) { + goto restart; + } + pte = updated_pte; }/* For superpage mappings, make a fake leaf PTE for the TLB's benefit. */
Signature · 1 line
2.53.0
Re: [PATCH v11 04/12] target/riscv: De-indent some code in get_physical_address()
Quoted 9 lines · Philippe Mathieu-Daudé
get_physical_address()is quite complex already. In order to make the two next commits simplers, de-indent oneif()ladder. No logical change intended.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/riscv/cpu_helper.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-)
Re: [PATCH v11 04/12] target/riscv: De-indent some code in get_physical_address()
In reply to Philippe Mathieu-DaudéQuoted 6 lines · Philippe Mathieu-Daudé
get_physical_address()is quite complex already. In order to make the two next commits simplers, de-indent oneif()ladder. No logical change intended.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 58 lines
--- target/riscv/cpu_helper.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-)target/riscv/cpu_helper.c +18
-16@@ -1617,27 +1617,30 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,hwaddr l = sxlen_bytes, addr1; mr =address_space_translate (cs->as, pte_addr, &addr1, &l, false,MEMTXATTRS_UNSPECIFIED ); -if (memory_region_is_ram (mr)) { -target_ulong *pte_pa =qemu_map_ram_ptr (mr->ram_block, addr1); -target_ulong old_pte; -if (riscv_cpu_sxl (env) ==MXL_RV32 ) { - old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa,cpu_to_le32 (pte),cpu_to_le32 (updated_pte)); - old_pte =le32_to_cpu (old_pte); - }else { - old_pte =qatomic_cmpxchg (pte_pa,cpu_to_le64 (pte),cpu_to_le64 (updated_pte)); - old_pte =le64_to_cpu (old_pte); - } -if (old_pte != pte) { - goto restart; - } - pte = updated_pte; - }else { +if (!memory_region_is_ram (mr)) {/* * Misconfigured PTE in ROM (AD bits are not preset) or * PTE is in IO space and can't be updated atomically. */ return TRANSLATE_FAIL ; } + +target_ulong *pte_pa =qemu_map_ram_ptr (mr->ram_block, addr1); +target_ulong old_pte; + +if (riscv_cpu_sxl (env) ==MXL_RV32 ) { + old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa,cpu_to_le32 (pte), +cpu_to_le32 (updated_pte)); + old_pte =le32_to_cpu (old_pte); + }else { + old_pte =qatomic_cmpxchg (pte_pa,cpu_to_le64 (pte), +cpu_to_le64 (updated_pte)); + old_pte =le64_to_cpu (old_pte); + } +if (old_pte != pte) { + goto restart; + } + pte = updated_pte; }/* For superpage mappings, make a fake leaf PTE for the TLB's benefit. */ --2.53.0
PATCH v11 5/12 target/riscv: Remove target_ulong use in get_physical_address()
In reply to Philippe Mathieu-DaudéUse uint32_t for RV32, uint64_t otherwise.
--- target/riscv/cpu_helper.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
target/riscv/cpu_helper.c +8 -6
@@ -1625,16 +1625,18 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,return TRANSLATE_FAIL ; } -target_ulong *pte_pa =qemu_map_ram_ptr (mr->ram_block, addr1); -target_ulong old_pte; +void *pte_pa =qemu_map_ram_ptr (mr->ram_block, addr1); +uint64_t old_pte;if (riscv_cpu_sxl (env) ==MXL_RV32 ) { - old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa,cpu_to_le32 (pte), -cpu_to_le32 (updated_pte)); +uint32_t cmp =cpu_to_le32 (pte); +uint32_t val =cpu_to_le32 (updated_pte); + old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa, cmp, val); old_pte =le32_to_cpu (old_pte); }else { - old_pte =qatomic_cmpxchg (pte_pa,cpu_to_le64 (pte), -cpu_to_le64 (updated_pte)); +uint64_t cmp =cpu_to_le64 (pte); +uint64_t val =cpu_to_le64 (updated_pte); + old_pte =qatomic_cmpxchg ((uint64_t *)pte_pa, cmp, val); old_pte =le64_to_cpu (old_pte); }if (old_pte != pte) {
Signature · 1 line
2.53.0
Re: [PATCH v11 05/12] target/riscv: Remove target_ulong use in get_physical_address()
Quoted 7 lines · Philippe Mathieu-Daudé
Use
uint32_tfor RV32,uint64_totherwise.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/riscv/cpu_helper.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
Re: [PATCH v11 05/12] target/riscv: Remove target_ulong use in get_physical_address()
In reply to Philippe Mathieu-DaudéQuoted 4 lines · Philippe Mathieu-Daudé
Use
uint32_tfor RV32,uint64_totherwise.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 37 lines
--- target/riscv/cpu_helper.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)target/riscv/cpu_helper.c +8
-7@@ -1625,16 +1625,18 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,return TRANSLATE_FAIL ; } -target_ulong *pte_pa =qemu_map_ram_ptr (mr->ram_block, addr1); -target_ulong old_pte; +void *pte_pa =qemu_map_ram_ptr (mr->ram_block, addr1); +uint64_t old_pte;if (riscv_cpu_sxl (env) ==MXL_RV32 ) { - old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa,cpu_to_le32 (pte), -cpu_to_le32 (updated_pte)); +uint32_t cmp =cpu_to_le32 (pte); +uint32_t val =cpu_to_le32 (updated_pte); + old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa, cmp, val); old_pte =le32_to_cpu (old_pte); }else { - old_pte =qatomic_cmpxchg (pte_pa,cpu_to_le64 (pte), -cpu_to_le64 (updated_pte)); +uint64_t cmp =cpu_to_le64 (pte); +uint64_t val =cpu_to_le64 (updated_pte); + old_pte =qatomic_cmpxchg ((uint64_t *)pte_pa, cmp, val); old_pte =le64_to_cpu (old_pte); }if (old_pte != pte) { --2.53.0
PATCH v11 6/12 target/riscv: Fix page table walk endianness for big-endian harts
In reply to Philippe Mathieu-DaudéFrom: Djordje Todorovic <[email protected]>
The page table walker reads PTEs using address_space_ldl/ldq which use compile-time native endianness (always LE for RISC-V). However, when a big-endian kernel writes PTEs via normal store instructions, they are stored in big-endian byte order. The walker then misinterprets the PTE values, causing page faults and a hang when the kernel enables the MMU.
The RISC-V privileged specification states that implicit data memory accesses to supervisor-level memory management data structures follow the hart's endianness setting (MSTATUS SBE/MBE bits).
Fix both PTE reads and atomic A/D bit updates to use the explicit _le or _be memory access variants based on the hart's runtime endianness.
--- target/riscv/cpu_helper.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
target/riscv/cpu_helper.c +11 -8
@@ -1370,6 +1370,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,target_ulong pte;hwaddr pte_addr;const hwaddr base_root = base; +const bool be =mo_endian_env (env) ==MO_BE ;int i; restart:
@@ -1418,9 +1419,11 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, }if (riscv_cpu_mxl (env) ==MXL_RV32 ) { - pte =address_space_ldl_le (cs->as, pte_addr, attrs, &res); + pte = be ?address_space_ldl_be (cs->as, pte_addr, attrs, &res) + :address_space_ldl_le (cs->as, pte_addr, attrs, &res); }else { - pte =address_space_ldq_le (cs->as, pte_addr, attrs, &res); + pte = be ?address_space_ldq_be (cs->as, pte_addr, attrs, &res) + :address_space_ldq_le (cs->as, pte_addr, attrs, &res); }if (res !=MEMTX_OK ) {
@@ -1629,15 +1632,15 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,uint64_t old_pte;if (riscv_cpu_sxl (env) ==MXL_RV32 ) { -uint32_t cmp =cpu_to_le32 (pte); -uint32_t val =cpu_to_le32 (updated_pte); +uint32_t cmp = be ?cpu_to_be32 (pte) :cpu_to_le32 (pte); +uint32_t val = be ?cpu_to_be32 (updated_pte) :cpu_to_le32 (updated_pte); old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa, cmp, val); - old_pte =le32_to_cpu (old_pte); + old_pte = be ?be32_to_cpu (old_pte) :le32_to_cpu (old_pte); }else { -uint64_t cmp =cpu_to_le64 (pte); -uint64_t val =cpu_to_le64 (updated_pte); +uint64_t cmp = be ?cpu_to_be64 (pte) :cpu_to_le64 (pte); +uint64_t val = be ?cpu_to_be64 (updated_pte) :cpu_to_le64 (updated_pte); old_pte =qatomic_cmpxchg ((uint64_t *)pte_pa, cmp, val); - old_pte =le64_to_cpu (old_pte); + old_pte = be ?be64_to_cpu (old_pte) :le64_to_cpu (old_pte); }if (old_pte != pte) { goto restart;
Signature · 1 line
2.53.0
Re: [PATCH v11 06/12] target/riscv: Fix page table walk endianness for big-endian harts
Quoted 15 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
The page table walker reads PTEs using address_space_ldl/ldq which use compile-time native endianness (always LE for RISC-V). However, when a big-endian kernel writes PTEs via normal store instructions, they are stored in big-endian byte order. The walker then misinterprets the PTE values, causing page faults and a hang when the kernel enables the MMU.
The RISC-V privileged specification states that implicit data memory accesses to supervisor-level memory management data structures follow the hart's endianness setting (MSTATUS SBE/MBE bits).
Fix both PTE reads and atomic A/D bit updates to use the explicit
_leor_bememory access variants based on the hart's runtime endianness.
Quoted 7 lines
Signed-off-by Djordje Todorovic <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/riscv/cpu_helper.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
Re: [PATCH v11 06/12] target/riscv: Fix page table walk endianness for big-endian harts
In reply to Philippe Mathieu-DaudéQuoted 13 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
The page table walker reads PTEs using address_space_ldl/ldq which use compile-time native endianness (always LE for RISC-V). However, when a big-endian kernel writes PTEs via normal store instructions, they are stored in big-endian byte order. The walker then misinterprets the PTE values, causing page faults and a hang when the kernel enables the MMU.
The RISC-V privileged specification states that implicit data memory accesses to supervisor-level memory management data structures follow the hart's endianness setting (MSTATUS SBE/MBE bits).
Quoted 6 lines
Fix both PTE reads and atomic A/D bit updates to use the explicit
_leor_bememory access variants based on the hart's runtime endianness.Signed-off-by Djordje Todorovic <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 56 lines
--- target/riscv/cpu_helper.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)target/riscv/cpu_helper.c +11
-9@@ -1370,6 +1370,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,target_ulong pte;hwaddr pte_addr;const hwaddr base_root = base; +const bool be =mo_endian_env (env) ==MO_BE ;int i; restart:@@ -1418,9 +1419,11 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, }if (riscv_cpu_mxl (env) ==MXL_RV32 ) { - pte =address_space_ldl_le (cs->as, pte_addr, attrs, &res); + pte = be ?address_space_ldl_be (cs->as, pte_addr, attrs, &res) + :address_space_ldl_le (cs->as, pte_addr, attrs, &res); }else { - pte =address_space_ldq_le (cs->as, pte_addr, attrs, &res); + pte = be ?address_space_ldq_be (cs->as, pte_addr, attrs, &res) + :address_space_ldq_le (cs->as, pte_addr, attrs, &res); }if (res !=MEMTX_OK ) {@@ -1629,15 +1632,15 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,uint64_t old_pte;if (riscv_cpu_sxl (env) ==MXL_RV32 ) { -uint32_t cmp =cpu_to_le32 (pte); -uint32_t val =cpu_to_le32 (updated_pte); +uint32_t cmp = be ?cpu_to_be32 (pte) :cpu_to_le32 (pte); +uint32_t val = be ?cpu_to_be32 (updated_pte) :cpu_to_le32 (updated_pte); old_pte =qatomic_cmpxchg ((uint32_t *)pte_pa, cmp, val); - old_pte =le32_to_cpu (old_pte); + old_pte = be ?be32_to_cpu (old_pte) :le32_to_cpu (old_pte); }else { -uint64_t cmp =cpu_to_le64 (pte); -uint64_t val =cpu_to_le64 (updated_pte); +uint64_t cmp = be ?cpu_to_be64 (pte) :cpu_to_le64 (pte); +uint64_t val = be ?cpu_to_be64 (updated_pte) :cpu_to_le64 (updated_pte); old_pte =qatomic_cmpxchg ((uint64_t *)pte_pa, cmp, val); - old_pte =le64_to_cpu (old_pte); + old_pte = be ?be64_to_cpu (old_pte) :le64_to_cpu (old_pte); }if (old_pte != pte) { goto restart; --2.53.0
PATCH v11 7/12 hw/riscv/boot: Rewrite setup_rom_reset_vec() using load/store API
In reply to Philippe Mathieu-DaudéIn order to make the following commits easier to review, do not pre-initialize the reset_vec[] array, fill each word one by one. Set the start and FDT load addresses using the load/ store APIs.
--- hw/riscv/boot.c | 53 ++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 29 deletions(-)
hw/riscv/boot.c +24 -29
@@ -18,6 +18,7 @@*/ #include "qemu/osdep.h" +#include "qemu/bswap.h" #include "qemu/datadir.h" #include "qemu/units.h" #include "qemu/error-report.h"
@@ -444,6 +445,9 @@ void riscv_rom_copy_firmware_info(MachineState *machine, &address_space_memory); } +#define CODE_WORDS 6 +#define DATA_WORDS 4 + void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts, hwaddr start_addr, hwaddr rom_base, hwaddr rom_size,
@@ -451,43 +455,34 @@ void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts uint64_t fdt_load_addr) {int i; -uint32_t start_addr_hi32 =0x00000000 ; -uint32_t fdt_load_addr_hi32 =0x00000000 ; +const bool rv32 =riscv_is_32bit (harts); +uint32_t reset_vec[CODE_WORDS +DATA_WORDS ]; -if (!riscv_is_32bit (harts)) { - start_addr_hi32 = start_addr >>32 ; - fdt_load_addr_hi32 = fdt_load_addr >>32 ; - } -/* reset vector */ -uint32_t reset_vec[10 ] = { -0x00000297 ,/* 1: auipc t0, %pcrel_hi(fw_dyn) */ -0x02828613 ,/* addi a2, t0, %pcrel_lo(1b) */ -0xf1402573 ,/* csrr a0, mhartid */ -0 , -0 , -0x00028067 ,/* jr t0 */ - start_addr,/* start: .dword */ - start_addr_hi32, - fdt_load_addr,/* fdt_laddr: .dword */ - fdt_load_addr_hi32, -/* fw_dyn: */ - }; -if (riscv_is_32bit (harts)) { - reset_vec[3 ] =0x0202a583 ;/* lw a1, 32(t0) */ - reset_vec[4 ] =0x0182a283 ;/* lw t0, 24(t0) */ +/* .text */ + reset_vec[0 ] =0x00000297 ;/* 1: auipc t0, %pcrel_hi(fw_dyn) */ + reset_vec[1 ] =0x02828613 ;/* addi a2, t0, %pcrel_lo(1b) */ +if (harts->harts[0 ].cfg.ext_zicsr) { + reset_vec[2 ] =0xf1402573 ;/* csrr a0, mhartid */ }else { - reset_vec[3 ] =0x0202b583 ;/* ld a1, 32(t0) */ - reset_vec[4 ] =0x0182b283 ;/* ld t0, 24(t0) */ - } - -if (!harts->harts[0 ].cfg.ext_zicsr) {/* * The Zicsr extension has been disabled, so let's ensure we don't * run the CSR instruction. Let's fill the address with a non * compressed nop. */ - reset_vec[2 ] =0x00000013 ;/* addi x0, x0, 0 */ + reset_vec[2 ] =0x00000013 ;/* addi x0, x0, 0 */ } +if (rv32) { + reset_vec[3 ] =0x0202a583 ;/* lw a1, 32(t0) */ + reset_vec[4 ] =0x0182a283 ;/* lw t0, 24(t0) */ + }else { + reset_vec[3 ] =0x0202b583 ;/* ld a1, 32(t0) */ + reset_vec[4 ] =0x0182b283 ;/* ld t0, 24(t0) */ + } + reset_vec[5 ] =0x00028067 ;/* jr t0 */ + +/* .data */ +stq_p (&reset_vec[6 ], start_addr);/* start: .dword */ +stq_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ /* copy in the reset vector in little_endian byte order */ for (i =0 ; i <ARRAY_SIZE (reset_vec); i++) {
Signature · 1 line
2.53.0
Re: [PATCH v11 07/12] hw/riscv/boot: Rewrite setup_rom_reset_vec() using load/store API
Quoted 10 lines · Philippe Mathieu-Daudé
In order to make the following commits easier to review, do not pre-initialize the reset_vec[] array, fill each word one by one. Set the start and FDT load addresses using the load/ store APIs.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/riscv/boot.c | 53 ++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 29 deletions(-)
Re: [PATCH v11 07/12] hw/riscv/boot: Rewrite setup_rom_reset_vec() using load/store API
In reply to Philippe Mathieu-DaudéQuoted 7 lines · Philippe Mathieu-Daudé
In order to make the following commits easier to review, do not pre-initialize the reset_vec[] array, fill each word one by one. Set the start and FDT load addresses using the load/ store APIs.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 94 lines
--- hw/riscv/boot.c | 53 ++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 29 deletions(-)hw/riscv/boot.c +24
-30@@ -18,6 +18,7 @@*/ #include "qemu/osdep.h" +#include "qemu/bswap.h" #include "qemu/datadir.h" #include "qemu/units.h" #include "qemu/error-report.h" @@ -444,6 +445,9 @@ void riscv_rom_copy_firmware_info(MachineState *machine, &address_space_memory); } +#define CODE_WORDS 6 +#define DATA_WORDS 4 + void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts, hwaddr start_addr, hwaddr rom_base, hwaddr rom_size,@@ -451,43 +455,34 @@ void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts uint64_t fdt_load_addr) {int i; -uint32_t start_addr_hi32 =0x00000000 ; -uint32_t fdt_load_addr_hi32 =0x00000000 ; +const bool rv32 =riscv_is_32bit (harts); +uint32_t reset_vec[CODE_WORDS +DATA_WORDS ]; -if (!riscv_is_32bit (harts)) { - start_addr_hi32 = start_addr >>32 ; - fdt_load_addr_hi32 = fdt_load_addr >>32 ; - } -/* reset vector */ -uint32_t reset_vec[10 ] = { -0x00000297 ,/* 1: auipc t0, %pcrel_hi(fw_dyn) */ -0x02828613 ,/* addi a2, t0, %pcrel_lo(1b) */ -0xf1402573 ,/* csrr a0, mhartid */ -0 , -0 , -0x00028067 ,/* jr t0 */ - start_addr,/* start: .dword */ - start_addr_hi32, - fdt_load_addr,/* fdt_laddr: .dword */ - fdt_load_addr_hi32, -/* fw_dyn: */ - }; -if (riscv_is_32bit (harts)) { - reset_vec[3 ] =0x0202a583 ;/* lw a1, 32(t0) */ - reset_vec[4 ] =0x0182a283 ;/* lw t0, 24(t0) */ +/* .text */ + reset_vec[0 ] =0x00000297 ;/* 1: auipc t0, %pcrel_hi(fw_dyn) */ + reset_vec[1 ] =0x02828613 ;/* addi a2, t0, %pcrel_lo(1b) */ +if (harts->harts[0 ].cfg.ext_zicsr) { + reset_vec[2 ] =0xf1402573 ;/* csrr a0, mhartid */ }else { - reset_vec[3 ] =0x0202b583 ;/* ld a1, 32(t0) */ - reset_vec[4 ] =0x0182b283 ;/* ld t0, 24(t0) */ - } - -if (!harts->harts[0 ].cfg.ext_zicsr) {/* * The Zicsr extension has been disabled, so let's ensure we don't * run the CSR instruction. Let's fill the address with a non * compressed nop. */ - reset_vec[2 ] =0x00000013 ;/* addi x0, x0, 0 */ + reset_vec[2 ] =0x00000013 ;/* addi x0, x0, 0 */ } +if (rv32) { + reset_vec[3 ] =0x0202a583 ;/* lw a1, 32(t0) */ + reset_vec[4 ] =0x0182a283 ;/* lw t0, 24(t0) */ + }else { + reset_vec[3 ] =0x0202b583 ;/* ld a1, 32(t0) */ + reset_vec[4 ] =0x0182b283 ;/* ld t0, 24(t0) */ + } + reset_vec[5 ] =0x00028067 ;/* jr t0 */ + +/* .data */ +stq_p (&reset_vec[6 ], start_addr);/* start: .dword */ +stq_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ /* copy in the reset vector in little_endian byte order */ for (i =0 ; i <ARRAY_SIZE (reset_vec); i++) { --2.53.0
PATCH v11 8/12 hw/riscv/boot: Replace cpu_to_le32() -> const_le32()
In reply to Philippe Mathieu-DaudéRather than adapting the array endianness when it it filled, directly initialize the CODE words with the correct endianness.
--- hw/riscv/boot.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)
hw/riscv/boot.c +13 -17
@@ -454,40 +454,36 @@ void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts uint64_t kernel_entry, uint64_t fdt_load_addr) { - int i;const bool rv32 =riscv_is_32bit (harts);uint32_t reset_vec[CODE_WORDS +DATA_WORDS ]; -/* .text */ - reset_vec[0 ] =0x00000297 ;/* 1: auipc t0, %pcrel_hi(fw_dyn) */ - reset_vec[1 ] =0x02828613 ;/* addi a2, t0, %pcrel_lo(1b) */ +/* .text (RISC-V instructions are always little-endian) */ + reset_vec[0 ] =const_le32 (0x00000297 );/* 1: auipc t0, %pcrel_hi(fw_dyn) */ + reset_vec[1 ] =const_le32 (0x02828613 );/* addi a2, t0, %pcrel_lo(1b) */ + reset_vec[2 ] =const_le32 (0xf1402573 );/* csrr a0, mhartid */ if (harts->harts[0 ].cfg.ext_zicsr) { - reset_vec[2 ] =0xf1402573 ;/* csrr a0, mhartid */ + reset_vec[2 ] =const_le32 (0xf1402573 );/* csrr a0, mhartid */ }else {/* * The Zicsr extension has been disabled, so let's ensure we don't * run the CSR instruction. Let's fill the address with a non * compressed nop. */ - reset_vec[2 ] =0x00000013 ;/* addi x0, x0, 0 */ + reset_vec[2 ] =const_le32 (0x00000013 );/* addi x0, x0, 0 */ }if (rv32) { - reset_vec[3 ] =0x0202a583 ;/* lw a1, 32(t0) */ - reset_vec[4 ] =0x0182a283 ;/* lw t0, 24(t0) */ + reset_vec[3 ] =const_le32 (0x0202a583 );/* lw a1, 32(t0) */ + reset_vec[4 ] =const_le32 (0x0182a283 );/* lw t0, 24(t0) */ }else { - reset_vec[3 ] =0x0202b583 ;/* ld a1, 32(t0) */ - reset_vec[4 ] =0x0182b283 ;/* ld t0, 24(t0) */ + reset_vec[3 ] =const_le32 (0x0202b583 );/* ld a1, 32(t0) */ + reset_vec[4 ] =const_le32 (0x0182b283 );/* ld t0, 24(t0) */ } - reset_vec[5 ] =0x00028067 ;/* jr t0 */ + reset_vec[5 ] =const_le32 (0x00028067 );/* jr t0 */ /* .data */ -stq_p (&reset_vec[6 ], start_addr);/* start: .dword */ -stq_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ +stq_le_p (&reset_vec[6 ], start_addr);/* start: .dword */ +stq_le_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ -/* copy in the reset vector in little_endian byte order */ -for (i =0 ; i <ARRAY_SIZE (reset_vec); i++) { - reset_vec[i] =cpu_to_le32 (reset_vec[i]); - }rom_add_blob_fixed_as ("mrom.reset" , reset_vec,sizeof (reset_vec), rom_base, &address_space_memory); riscv_rom_copy_firmware_info(machine, harts,
Signature · 1 line
2.53.0
Re: [PATCH v11 08/12] hw/riscv/boot: Replace cpu_to_le32() -> const_le32()
Quoted 9 lines · Philippe Mathieu-Daudé
Rather than adapting the array endianness when it it filled, directly initialize the CODE words with the correct endianness.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/riscv/boot.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)
One could argue original version is more readable.
Any specific reason for rewriting things this way (performance, personal preference, other)?
Not a blocker, just want to understand the original motivation.
Regards,
Pierrick
Re: [PATCH v11 08/12] hw/riscv/boot: Replace cpu_to_le32() -> const_le32()
Quoted 11 lines · Pierrick Bouvier
Quoted 9 lines · Philippe Mathieu-Daudé
Rather than adapting the array endianness when it it filled, directly initialize the CODE words with the correct endianness.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/riscv/boot.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)
Quoted 4 lines
One could argue original version is more readable.
Any specific reason for rewriting things this way (performance, personal preference, other)?
Not a blocker, just want to understand the original motivation.
Looking at the following changes, I think it makes more sense to have the CODE section const little-endian, while the DATA section is swapped. Otherwise we'd need 2 distinct loops.
Quoted 5 lines
Reviewed-by Pierrick Bouvier <[email protected]>Regards,
Pierrick
Re: [PATCH v11 08/12] hw/riscv/boot: Replace cpu_to_le32() -> const_le32()
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Pierrick Bouvier
Quoted 9 lines · Philippe Mathieu-Daudé
Rather than adapting the array endianness when it it filled, directly initialize the CODE words with the correct endianness.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
hw/riscv/boot.c| 30 +++++++++++++-----------------1 file changed, 13 insertions(+), 17 deletions(-)
Quoted 9 lines
Quoted 4 lines
One could argue original version is more readable.
Any specific reason for rewriting things this way (performance, personal preference, other)?
Not a blocker, just want to understand the original motivation.Looking at the following changes, I think it makes more sense to have the CODE section const little-endian, while the DATA section is swapped. Otherwise we'd need 2 distinct loops.
In this case, having two distinct arrays, and two distinct loops might be more clear IMHO.
The function is called only once, we can afford this.
Quoted 6 lines
Quoted 5 lines
Reviewed-by Pierrick Bouvier <[email protected]>Regards,
Pierrick
Re: [PATCH v11 08/12] hw/riscv/boot: Replace cpu_to_le32() -> const_le32()
Quoted 18 lines · Pierrick Bouvier
Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Pierrick Bouvier
Quoted 9 lines · Philippe Mathieu-Daudé
Rather than adapting the array endianness when it it filled, directly initialize the CODE words with the correct endianness.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/riscv/boot.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)One could argue original version is more readable.
Any specific reason for rewriting things this way (performance, personal preference, other)?
Not a blocker, just want to understand the original motivation.
Quoted 8 lines
Quoted 4 lines
Looking at the following changes, I think it makes more sense to have the CODE section const little-endian, while the DATA section is swapped. Otherwise we'd need 2 distinct loops.
In this case, having two distinct arrays, and two distinct loops might be more clear IMHO.
The function is called only once, we can afford this.
This was my first approach until I realized this array is registered with rom_add_blob_fixed_as().
We could register 2 distincts arrays, mrom.reset.code and mrom.reset.rodata. Now than the patch is written & reviewed, it sure it is worth it.
Re: [PATCH v11 08/12] hw/riscv/boot: Replace cpu_to_le32() -> const_le32()
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Pierrick Bouvier
Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Pierrick Bouvier
Quoted 9 lines · Philippe Mathieu-Daudé
Rather than adapting the array endianness when it it filled, directly initialize the CODE words with the correct endianness.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
hw/riscv/boot.c| 30 +++++++++++++-----------------1 file changed, 13 insertions(+), 17 deletions(-)One could argue original version is more readable. Any specific reason for rewriting things this way (performance, personal
preference, other)?
Not a blocker, just want to understand the original motivation.Quoted 8 lines
Quoted 4 lines
Looking at the following changes, I think it makes more sense to have the CODE section const little-endian, while the DATA section is swapped. Otherwise we'd need 2 distinct loops.
In this case, having two distinct arrays, and two distinct loops might be more clear IMHO.
The function is called only once, we can afford this.
Quoted 6 lines
This was my first approach until I realized this array is registered with
rom_add_blob_fixed_as().We could register 2 distincts arrays, mrom.reset.code and mrom.reset.rodata. Now than the patch is written & reviewed, it sure it is worth it.
Not blocking for sure, things can be enhanced and refactored later.
Re: [PATCH v11 08/12] hw/riscv/boot: Replace cpu_to_le32() -> const_le32()
In reply to Philippe Mathieu-DaudéQuoted 6 lines · Philippe Mathieu-Daudé
Rather than adapting the array endianness when it it filled, directly initialize the CODE words with the correct endianness.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 66 lines
--- hw/riscv/boot.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)hw/riscv/boot.c +13
-18@@ -454,40 +454,36 @@ void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts uint64_t kernel_entry, uint64_t fdt_load_addr) { - int i;const bool rv32 =riscv_is_32bit (harts);uint32_t reset_vec[CODE_WORDS +DATA_WORDS ]; -/* .text */ - reset_vec[0 ] =0x00000297 ;/* 1: auipc t0, %pcrel_hi(fw_dyn) */ - reset_vec[1 ] =0x02828613 ;/* addi a2, t0, %pcrel_lo(1b) */ +/* .text (RISC-V instructions are always little-endian) */ + reset_vec[0 ] =const_le32 (0x00000297 );/* 1: auipc t0, %pcrel_hi(fw_dyn) */ + reset_vec[1 ] =const_le32 (0x02828613 );/* addi a2, t0, %pcrel_lo(1b) */ + reset_vec[2 ] =const_le32 (0xf1402573 );/* csrr a0, mhartid */ if (harts->harts[0 ].cfg.ext_zicsr) { - reset_vec[2 ] =0xf1402573 ;/* csrr a0, mhartid */ + reset_vec[2 ] =const_le32 (0xf1402573 );/* csrr a0, mhartid */ }else {/* * The Zicsr extension has been disabled, so let's ensure we don't * run the CSR instruction. Let's fill the address with a non * compressed nop. */ - reset_vec[2 ] =0x00000013 ;/* addi x0, x0, 0 */ + reset_vec[2 ] =const_le32 (0x00000013 );/* addi x0, x0, 0 */ }if (rv32) { - reset_vec[3 ] =0x0202a583 ;/* lw a1, 32(t0) */ - reset_vec[4 ] =0x0182a283 ;/* lw t0, 24(t0) */ + reset_vec[3 ] =const_le32 (0x0202a583 );/* lw a1, 32(t0) */ + reset_vec[4 ] =const_le32 (0x0182a283 );/* lw t0, 24(t0) */ }else { - reset_vec[3 ] =0x0202b583 ;/* ld a1, 32(t0) */ - reset_vec[4 ] =0x0182b283 ;/* ld t0, 24(t0) */ + reset_vec[3 ] =const_le32 (0x0202b583 );/* ld a1, 32(t0) */ + reset_vec[4 ] =const_le32 (0x0182b283 );/* ld t0, 24(t0) */ } - reset_vec[5 ] =0x00028067 ;/* jr t0 */ + reset_vec[5 ] =const_le32 (0x00028067 );/* jr t0 */ /* .data */ -stq_p (&reset_vec[6 ], start_addr);/* start: .dword */ -stq_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ +stq_le_p (&reset_vec[6 ], start_addr);/* start: .dword */ +stq_le_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ -/* copy in the reset vector in little_endian byte order */ -for (i =0 ; i <ARRAY_SIZE (reset_vec); i++) { - reset_vec[i] =cpu_to_le32 (reset_vec[i]); - }rom_add_blob_fixed_as ("mrom.reset" , reset_vec,sizeof (reset_vec), rom_base, &address_space_memory); riscv_rom_copy_firmware_info(machine, harts, --2.53.0
PATCH v11 9/12 target/riscv: Add big-endian CPU configuration field and reset logic
In reply to Philippe Mathieu-DaudéFrom: Djordje Todorovic <[email protected]>
Add a big_endian field to RISCVCPUConfig and wire it into the CPU reset path. When cfg.big_endian is set, riscv_cpu_reset_hold() writes 1 into the MSTATUS MBE/SBE/UBE fields using set_field(); otherwise it writes 0. This makes the reset value deterministic on both cold and warm reset.
This models fixed-endian harts, not mixed-endian implementations where the guest can toggle MBE/SBE/UBE at runtime. The MBE/SBE/UBE bits are not included in the writable mask of any mstatus/mstatush/sstatus CSR write path (unchanged by this series), so the value chosen at reset is effectively hardwired per section 3.1.6.5 of the RISC-V Privileged Specification.
The user-facing property and documentation are added in a later patch, once the full endianness support is in place.
--- target/riscv/cpu_cfg_fields.h.inc | 1 + target/riscv/cpu.c | 7 +++++++ 2 files changed, 8 insertions(+)
target/riscv/cpu_cfg_fields.h.inc +1 -0
@@ -157,6 +157,7 @@ BOOL_FIELD(ext_xmipscmov) BOOL_FIELD(ext_xmipslsp) BOOL_FIELD(ext_xlrbr) +BOOL_FIELD(big_endian) BOOL_FIELD(mmu) BOOL_FIELD(pmp) BOOL_FIELD(debug)
target/riscv/cpu.c +7 -0
@@ -739,6 +739,13 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->mstatus =set_field (env->mstatus,MSTATUS_MDT ,1 ); } } +/* +* Model fixed-endian harts: MBE/SBE/UBE are initialized from the +* CPU configuration and are intentionally not writable via status CSRs. +*/ + env->mstatus =set_field (env->mstatus,MSTATUS_MBE , cpu->cfg.big_endian); + env->mstatus =set_field (env->mstatus,MSTATUS_SBE , cpu->cfg.big_endian); + env->mstatus =set_field (env->mstatus,MSTATUS_UBE , cpu->cfg.big_endian); env->mcause =0 ; env->miclaim =MIP_SGEIP ; env->pc = env->resetvec;
Signature · 1 line
2.53.0
Re: [PATCH v11 09/12] target/riscv: Add big-endian CPU configuration field and reset logic
Quoted 18 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Add a
big_endianfield to RISCVCPUConfig and wire it into the CPU reset path. When cfg.big_endian is set,riscv_cpu_reset_hold()writes 1 into the MSTATUS MBE/SBE/UBE fields usingset_field(); otherwise it writes 0. This makes the reset value deterministic on both cold and warm reset.This models fixed-endian harts, not mixed-endian implementations where the guest can toggle MBE/SBE/UBE at runtime. The MBE/SBE/UBE bits are not included in the writable mask of any mstatus/mstatush/sstatus CSR write path (unchanged by this series), so the value chosen at reset is effectively hardwired per section 3.1.6.5 of the RISC-V Privileged Specification.
The user-facing property and documentation are added in a later patch, once the full endianness support is in place.
Quoted 7 lines
Signed-off-by Djordje Todorovic <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/riscv/cpu_cfg_fields.h.inc | 1 + target/riscv/cpu.c | 7 +++++++ 2 files changed, 8 insertions(+)
Re: [PATCH v11 09/12] target/riscv: Add big-endian CPU configuration field and reset logic
In reply to Philippe Mathieu-DaudéQuoted 16 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Add a
big_endianfield to RISCVCPUConfig and wire it into the CPU reset path. When cfg.big_endian is set,riscv_cpu_reset_hold()writes 1 into the MSTATUS MBE/SBE/UBE fields usingset_field(); otherwise it writes 0. This makes the reset value deterministic on both cold and warm reset.This models fixed-endian harts, not mixed-endian implementations where the guest can toggle MBE/SBE/UBE at runtime. The MBE/SBE/UBE bits are not included in the writable mask of any mstatus/mstatush/sstatus CSR write path (unchanged by this series), so the value chosen at reset is effectively hardwired per section 3.1.6.5 of the RISC-V Privileged Specification.
Quoted 5 lines
The user-facing property and documentation are added in a later patch, once the full endianness support is in place.
Signed-off-by Djordje Todorovic <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 39 lines
--- target/riscv/cpu_cfg_fields.h.inc | 1 + target/riscv/cpu.c | 7 +++++++ 2 files changed, 8 insertions(+)target/riscv/cpu_cfg_fields.h.inc +1
-0@@ -157,6 +157,7 @@ BOOL_FIELD(ext_xmipscmov) BOOL_FIELD(ext_xmipslsp) BOOL_FIELD(ext_xlrbr) +BOOL_FIELD(big_endian) BOOL_FIELD(mmu) BOOL_FIELD(pmp) BOOL_FIELD(debug)target/riscv/cpu.c +7
-1@@ -739,6 +739,13 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->mstatus =set_field (env->mstatus,MSTATUS_MDT ,1 ); } } +/* +* Model fixed-endian harts: MBE/SBE/UBE are initialized from the +* CPU configuration and are intentionally not writable via status CSRs. +*/ + env->mstatus =set_field (env->mstatus,MSTATUS_MBE , cpu->cfg.big_endian); + env->mstatus =set_field (env->mstatus,MSTATUS_SBE , cpu->cfg.big_endian); + env->mstatus =set_field (env->mstatus,MSTATUS_UBE , cpu->cfg.big_endian); env->mcause =0 ; env->miclaim =MIP_SGEIP ; env->pc = env->resetvec; --2.53.0
PATCH v11 10/12 hw/riscv/boot: Honour data endianness
In reply to Philippe Mathieu-DaudéFrom: Djordje Todorovic <[email protected]>
Check the hart endianness property and use it throughout the boot code:
- ELF loading: pass ELFDATA2MSB or ELFDATA2LSB based on endianness
- Firmware dynamic info
- Reset vector: instructions (entries 0-5) remain always little-endian, data words (entries 6-9) use target data endianness.
--- hw/riscv/boot.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-)
hw/riscv/boot.c +28 -12
@@ -407,21 +407,31 @@ void riscv_rom_copy_firmware_info(MachineState *machine,struct fw_dynamic_info64 dinfo64;void *dinfo_ptr =NULL ;size_t dinfo_len; +const bool rv32 =riscv_is_32bit (harts); +const bool be = harts->harts[0 ].cfg.big_endian; -if (riscv_is_32bit (harts)) { - dinfo32.magic =cpu_to_le32 (FW_DYNAMIC_INFO_MAGIC_VALUE ); - dinfo32.version =cpu_to_le32 (FW_DYNAMIC_INFO_VERSION ); - dinfo32.next_mode =cpu_to_le32 (FW_DYNAMIC_INFO_NEXT_MODE_S ); - dinfo32.next_addr =cpu_to_le32 (kernel_entry); +if (rv32) { + dinfo32.magic = be ?cpu_to_be32 (FW_DYNAMIC_INFO_MAGIC_VALUE ) + :cpu_to_le32 (FW_DYNAMIC_INFO_MAGIC_VALUE ); + dinfo32.version = be ?cpu_to_be32 (FW_DYNAMIC_INFO_VERSION ) + :cpu_to_le32 (FW_DYNAMIC_INFO_VERSION ); + dinfo32.next_mode = be ?cpu_to_be32 (FW_DYNAMIC_INFO_NEXT_MODE_S ) + :cpu_to_le32 (FW_DYNAMIC_INFO_NEXT_MODE_S ); + dinfo32.next_addr = be ?cpu_to_be32 (kernel_entry) + :cpu_to_le32 (kernel_entry); dinfo32.options =0 ; dinfo32.boot_hart =0 ; dinfo_ptr = &dinfo32; dinfo_len =sizeof (dinfo32); }else { - dinfo64.magic =cpu_to_le64 (FW_DYNAMIC_INFO_MAGIC_VALUE ); - dinfo64.version =cpu_to_le64 (FW_DYNAMIC_INFO_VERSION ); - dinfo64.next_mode =cpu_to_le64 (FW_DYNAMIC_INFO_NEXT_MODE_S ); - dinfo64.next_addr =cpu_to_le64 (kernel_entry); + dinfo64.magic = be ?cpu_to_be64 (FW_DYNAMIC_INFO_MAGIC_VALUE ) + :cpu_to_le64 (FW_DYNAMIC_INFO_MAGIC_VALUE ); + dinfo64.version = be ?cpu_to_be64 (FW_DYNAMIC_INFO_VERSION ) + :cpu_to_le64 (FW_DYNAMIC_INFO_VERSION ); + dinfo64.next_mode = be ?cpu_to_be64 (FW_DYNAMIC_INFO_NEXT_MODE_S ) + :cpu_to_le64 (FW_DYNAMIC_INFO_NEXT_MODE_S ); + dinfo64.next_addr = be ?cpu_to_be64 (kernel_entry) + :cpu_to_le64 (kernel_entry); dinfo64.options =0 ; dinfo64.boot_hart =0 ; dinfo_ptr = &dinfo64;
@@ -455,6 +465,7 @@ void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts uint64_t fdt_load_addr) {const bool rv32 =riscv_is_32bit (harts); +const bool big_endian = harts->harts[0 ].cfg.big_endian;uint32_t reset_vec[CODE_WORDS +DATA_WORDS ];/* .text (RISC-V instructions are always little-endian) */
@@ -480,9 +491,14 @@ void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts } reset_vec[5 ] =const_le32 (0x00028067 );/* jr t0 */ -/* .data */ -stq_le_p (&reset_vec[6 ], start_addr);/* start: .dword */ -stq_le_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ +/* .data (must match the firmware's data endianness) */ +if (big_endian) { +stq_be_p (&reset_vec[6 ], start_addr);/* start: .dword */ +stq_be_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ + }else { +stq_le_p (&reset_vec[6 ], start_addr); +stq_le_p (&reset_vec[8 ], fdt_load_addr); + }rom_add_blob_fixed_as ("mrom.reset" , reset_vec,sizeof (reset_vec), rom_base, &address_space_memory);
Signature · 1 line
2.53.0
Re: [PATCH v11 10/12] hw/riscv/boot: Honour data endianness
Quoted 9 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Check the hart endianness property and use it throughout the boot code:
- ELF loading: pass ELFDATA2MSB or ELFDATA2LSB based on endianness
- Firmware dynamic info
- Reset vector: instructions (entries 0-5) remain always little-endian, data words (entries 6-9) use target data endianness.
Quoted 7 lines
Signed-off-by Djordje Todorovic <[email protected]>Co-developed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/riscv/boot.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-)
Re: [PATCH v11 10/12] hw/riscv/boot: Honour data endianness
In reply to Philippe Mathieu-DaudéQuoted 5 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Check the hart endianness property and use it throughout the boot code:
Quoted 8 lines
- ELF loading: pass ELFDATA2MSB or ELFDATA2LSB based on endianness
- Firmware dynamic info
- Reset vector: instructions (entries 0-5) remain always little-endian, data words (entries 6-9) use target data endianness.Signed-off-by Djordje Todorovic <[email protected]>Co-developed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 79 lines
--- hw/riscv/boot.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-)hw/riscv/boot.c +28
-13@@ -407,21 +407,31 @@ void riscv_rom_copy_firmware_info(MachineState *machine,struct fw_dynamic_info64 dinfo64;void *dinfo_ptr =NULL ;size_t dinfo_len; +const bool rv32 =riscv_is_32bit (harts); +const bool be = harts->harts[0 ].cfg.big_endian; -if (riscv_is_32bit (harts)) { - dinfo32.magic =cpu_to_le32 (FW_DYNAMIC_INFO_MAGIC_VALUE ); - dinfo32.version =cpu_to_le32 (FW_DYNAMIC_INFO_VERSION ); - dinfo32.next_mode =cpu_to_le32 (FW_DYNAMIC_INFO_NEXT_MODE_S ); - dinfo32.next_addr =cpu_to_le32 (kernel_entry); +if (rv32) { + dinfo32.magic = be ?cpu_to_be32 (FW_DYNAMIC_INFO_MAGIC_VALUE ) + :cpu_to_le32 (FW_DYNAMIC_INFO_MAGIC_VALUE ); + dinfo32.version = be ?cpu_to_be32 (FW_DYNAMIC_INFO_VERSION ) + :cpu_to_le32 (FW_DYNAMIC_INFO_VERSION ); + dinfo32.next_mode = be ?cpu_to_be32 (FW_DYNAMIC_INFO_NEXT_MODE_S ) + :cpu_to_le32 (FW_DYNAMIC_INFO_NEXT_MODE_S ); + dinfo32.next_addr = be ?cpu_to_be32 (kernel_entry) + :cpu_to_le32 (kernel_entry); dinfo32.options =0 ; dinfo32.boot_hart =0 ; dinfo_ptr = &dinfo32; dinfo_len =sizeof (dinfo32); }else { - dinfo64.magic =cpu_to_le64 (FW_DYNAMIC_INFO_MAGIC_VALUE ); - dinfo64.version =cpu_to_le64 (FW_DYNAMIC_INFO_VERSION ); - dinfo64.next_mode =cpu_to_le64 (FW_DYNAMIC_INFO_NEXT_MODE_S ); - dinfo64.next_addr =cpu_to_le64 (kernel_entry); + dinfo64.magic = be ?cpu_to_be64 (FW_DYNAMIC_INFO_MAGIC_VALUE ) + :cpu_to_le64 (FW_DYNAMIC_INFO_MAGIC_VALUE ); + dinfo64.version = be ?cpu_to_be64 (FW_DYNAMIC_INFO_VERSION ) + :cpu_to_le64 (FW_DYNAMIC_INFO_VERSION ); + dinfo64.next_mode = be ?cpu_to_be64 (FW_DYNAMIC_INFO_NEXT_MODE_S ) + :cpu_to_le64 (FW_DYNAMIC_INFO_NEXT_MODE_S ); + dinfo64.next_addr = be ?cpu_to_be64 (kernel_entry) + :cpu_to_le64 (kernel_entry); dinfo64.options =0 ; dinfo64.boot_hart =0 ; dinfo_ptr = &dinfo64;@@ -455,6 +465,7 @@ void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts uint64_t fdt_load_addr) {const bool rv32 =riscv_is_32bit (harts); +const bool big_endian = harts->harts[0 ].cfg.big_endian;uint32_t reset_vec[CODE_WORDS +DATA_WORDS ];/* .text (RISC-V instructions are always little-endian) */ @@ -480,9 +491,14 @@ void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts } reset_vec[5 ] =const_le32 (0x00028067 );/* jr t0 */ -/* .data */ -stq_le_p (&reset_vec[6 ], start_addr);/* start: .dword */ -stq_le_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ +/* .data (must match the firmware's data endianness) */ +if (big_endian) { +stq_be_p (&reset_vec[6 ], start_addr);/* start: .dword */ +stq_be_p (&reset_vec[8 ], fdt_load_addr);/* fdt_laddr: .dword */ + }else { +stq_le_p (&reset_vec[6 ], start_addr); +stq_le_p (&reset_vec[8 ], fdt_load_addr); + }rom_add_blob_fixed_as ("mrom.reset" , reset_vec,sizeof (reset_vec), rom_base, &address_space_memory); --2.53.0
PATCH v11 11/12 target/riscv: Expose and document the CPU 'big-endian' property
In reply to Philippe Mathieu-DaudéFrom: Djordje Todorovic <[email protected]>
Now that the full big-endian data path is in place (runtime MSTATUS bits, boot code, and page-table walks), expose the "big-endian" property to users via DEFINE_PROP_BOOL and document it in docs/system/target-riscv.rst.
Document that the property models fixed-endian hardware: it selects harts whose MBE/SBE/UBE fields are fixed to 1, and it does not model a mixed-endian implementation where software can toggle those bits at runtime.
The property can be enabled from the command line, e.g.:
-cpu <cpu>,big-endian=on
--- docs/system/target-riscv.rst | 29 +++++++++++++++++++++++++++++ target/riscv/cpu.c | 1 + 2 files changed, 30 insertions(+)
docs/system/target-riscv.rst +29 -0
@@ -95,3 +95,32 @@ the images they need. * ``-bios <file>`` Tells QEMU to load the specified file as the firmware. + +RISC-V CPU endianness +--------------------- + +The RISC-V ISA specifies that instruction fetches are always little-endian, +while data accesses can be either little-endian or big-endian under control +of the MSTATUS ``MBE``/``SBE``/``UBE`` bits (see section 3.1.6.5, "Memory +Endianness", in the RISC-V Privileged Specification). + +QEMU implements the full data-endianness behaviour described by those bits. +In addition, the RISC-V CPU object exposes a ``big-endian`` boolean property +which models a big-endian-only hardware implementation, where the +``MBE``/``SBE``/``UBE`` bits are hardwired to 1. When the property is set, +the CPU is reset with all three bits initialised to 1, so the guest starts +executing in big-endian data mode from the reset vector. The property is a +static, per-CPU hardware configuration option and is not meant to be toggled +at runtime. + +The property does not model a mixed-endian implementation where software can +toggle ``MBE``/``SBE``/``UBE`` at runtime. QEMU's RISC-V CPUs treat these +fields as fixed by the CPU configuration: they are reset to 0 by default and +to 1 when ``big-endian`` is enabled. + +The property can be enabled from the command line, for example:: + + -cpu <cpu>,big-endian=on + +No upstream CPU model currently defaults to big-endian; the property is +provided so that big-endian-only RISC-V CPU variants can be modelled.
target/riscv/cpu.c +1 -0
@@ -2520,6 +2520,7 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = {static const Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("debug" ,RISCVCPU , cfg.debug, true), +DEFINE_PROP_BOOL ("big-endian" ,RISCVCPU , cfg.big_endian, false), {.name ="pmu-mask" , .info = &prop_pmu_mask}, {.name ="pmu-num" , .info = &prop_pmu_num},/* Deprecated */
Signature · 1 line
2.53.0
Re: [PATCH v11 11/12] target/riscv: Expose and document the CPU 'big-endian' property
Quoted 14 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Now that the full big-endian data path is in place (runtime MSTATUS bits, boot code, and page-table walks), expose the "big-endian" property to users via
DEFINE_PROP_BOOLand document it indocs/system/target-riscv.rst.Document that the property models fixed-endian hardware: it selects harts whose MBE/SBE/UBE fields are fixed to 1, and it does not model a mixed-endian implementation where software can toggle those bits at runtime.
The property can be enabled from the command line, e.g.:
Quoted 9 lines
-cpu <cpu>,big-endian=on
Signed-off-by Djordje Todorovic <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- docs/system/target-riscv.rst | 29 +++++++++++++++++++++++++++++ target/riscv/cpu.c | 1 + 2 files changed, 30 insertions(+)
Re: [PATCH v11 11/12] target/riscv: Expose and document the CPU 'big-endian' property
In reply to Philippe Mathieu-DaudéQuoted 13 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Now that the full big-endian data path is in place (runtime MSTATUS bits, boot code, and page-table walks), expose the "big-endian" property to users via
DEFINE_PROP_BOOLand document it indocs/system/target-riscv.rst.Document that the property models fixed-endian hardware: it selects harts whose MBE/SBE/UBE fields are fixed to 1, and it does not model a mixed-endian implementation where software can toggle those bits at runtime.
Quoted 6 lines
The property can be enabled from the command line, e.g.:
-cpu <cpu>,big-endian=on
Signed-off-by Djordje Todorovic <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 58 lines
--- docs/system/target-riscv.rst | 29 +++++++++++++++++++++++++++++ target/riscv/cpu.c | 1 + 2 files changed, 30 insertions(+)docs/system/target-riscv.rst +29
-0@@ -95,3 +95,32 @@ the images they need. * ``-bios <file>`` Tells QEMU to load the specified file as the firmware. + +RISC-V CPU endianness +--------------------- + +The RISC-V ISA specifies that instruction fetches are always little-endian, +while data accesses can be either little-endian or big-endian under control +of the MSTATUS ``MBE``/``SBE``/``UBE`` bits (see section 3.1.6.5, "Memory +Endianness", in the RISC-V Privileged Specification). + +QEMU implements the full data-endianness behaviour described by those bits. +In addition, the RISC-V CPU object exposes a ``big-endian`` boolean property +which models a big-endian-only hardware implementation, where the +``MBE``/``SBE``/``UBE`` bits are hardwired to 1. When the property is set, +the CPU is reset with all three bits initialised to 1, so the guest starts +executing in big-endian data mode from the reset vector. The property is a +static, per-CPU hardware configuration option and is not meant to be toggled +at runtime. + +The property does not model a mixed-endian implementation where software can +toggle ``MBE``/``SBE``/``UBE`` at runtime. QEMU's RISC-V CPUs treat these +fields as fixed by the CPU configuration: they are reset to 0 by default and +to 1 when ``big-endian`` is enabled. + +The property can be enabled from the command line, for example:: + + -cpu <cpu>,big-endian=on + +No upstream CPU model currently defaults to big-endian; the property is +provided so that big-endian-only RISC-V CPU variants can be modelled.target/riscv/cpu.c +1
-1@@ -2520,6 +2520,7 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = {static const Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("debug" ,RISCVCPU , cfg.debug, true), +DEFINE_PROP_BOOL ("big-endian" ,RISCVCPU , cfg.big_endian, false), {.name ="pmu-mask" , .info = &prop_pmu_mask}, {.name ="pmu-num" , .info = &prop_pmu_num},/* Deprecated */ --2.53.0
PATCH v11 12/12 tests/functional: Add RISC-V endianness test
In reply to Philippe Mathieu-DaudéFrom: Djordje Todorovic <[email protected]>
Add functional test for the RISC-V 'big-endian' CPU property.
--- tests/functional/riscv64/meson.build | 1 + tests/functional/riscv64/test_endianness.py | 57 +++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/functional/riscv64/test_endianness.py
tests/functional/riscv64/meson.build +1 -0
@@ -11,6 +11,7 @@ tests_riscv64_system_quick = [ ] tests_riscv64_system_thorough = [ + 'endianness', 'boston', 'sifive_u', 'tuxrun',
tests/functional/riscv64/test_endianness.py new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH v11 12/12] tests/functional: Add RISC-V endianness test
Quoted 9 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Add functional test for the RISC-V 'big-endian' CPU property.
Signed-off-by Djordje Todorovic <[email protected]>Reviewed-by Thomas Huth <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 6 lines
--- tests/functional/riscv64/meson.build | 1 + tests/functional/riscv64/test_endianness.py | 57 +++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/functional/riscv64/test_endianness.py
Re: [PATCH v11 12/12] tests/functional: Add RISC-V endianness test
In reply to Philippe Mathieu-DaudéQuoted 10 lines · Philippe Mathieu-Daudé
From: Djordje Todorovic <[email protected]>
Add functional test for the RISC-V 'big-endian' CPU property.
Signed-off-by Djordje Todorovic <[email protected]>Reviewed-by Thomas Huth <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 85 lines
--- tests/functional/riscv64/meson.build | 1 + tests/functional/riscv64/test_endianness.py | 57 +++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/functional/riscv64/test_endianness.pytests/functional/riscv64/meson.build +1
-0@@ -11,6 +11,7 @@ tests_riscv64_system_quick = [ ] tests_riscv64_system_thorough = [ + 'endianness', 'boston', 'sifive_u', 'tuxrun',tests/functional/riscv64/test_endianness.py new file +57
-1@@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# +# Functional tests for RISC-V big-endian support +# +# Copyright (c) 2026 MIPS +# +# SPDX-License-Identifier: GPL-2.0-or-later + +from qemu_test import QemuSystemTest, Asset +from qemu_test import wait_for_console_pattern + + +class RiscvBigEndian(QemuSystemTest): + """ + Tests for RISC-V runtime big-endian data support. + + Uses a bare-metal RV64 ELF that detects data endianness at runtime + by storing a 32-bit word and reading back byte 0. Prints "ENDIAN: BE" + or "ENDIAN: LE" to the NS16550A UART on the virt machine. + """ + + timeout = 10 + + ASSET_BE_TEST = Asset( + 'https://github.com/MIPS/linux-test-downloads/raw/main/' + 'riscvbe-baremetal/be-test-bare-metal.elf', + '9ad51b675e101de65908fadbac064ed1d0564c17463715d09dd734db86ea0f58') + + def _run_bare_metal(self, big_endian=False): + self.set_machine('virt') + kernel = self.ASSET_BE_TEST.fetch() + self.vm.add_args('-bios', 'none') + self.vm.add_args('-kernel', kernel) + if big_endian: + self.vm.add_args('-cpu', 'rv64,big-endian=on') + self.vm.set_console() + self.vm.launch() + expected = 'ENDIAN: BE' if big_endian else 'ENDIAN: LE' + wait_for_console_pattern(self, expected) + + def test_bare_metal_littleendian(self): + """ + Boot bare-metal ELF on virt with default little-endian CPU. + Expects "ENDIAN: LE" on UART. + """ + self._run_bare_metal(big_endian=False) + + def test_bare_metal_bigendian(self): + """ + Boot bare-metal ELF on virt with big-endian=on CPU property. + Expects "ENDIAN: BE" on UART. + """ + self._run_bare_metal(big_endian=True) + + +if __name__ == '__main__': + QemuSystemTest.main() --2.53.0
Quoted 53 lines · Philippe Mathieu-Daudé
v11:
- rewrite some patches while reviewing for mainstream integrationv10 cover from Djordje:
Hi all,
This series adds big-endian data support for RISC-V system emulation.
Following the discussion on previous versions, this series models fixed-endian harts, not a mixed-endian implementation where software can toggle MBE/SBE/UBE at runtime. The reset value of MBE/SBE/UBE is selected from the CPU configuration. These bits remain outside the mstatus/mstatush/sstatus writable masks, so they are effectively hardwired to 0 for the default little-endian CPUs and to 1 when
big-endian=on.The user-visible
big-endianCPU property is exposed only after the data path, boot path, and page-table walk support are in place, so the series remains bisectable.Djordje Todorovic (6):
target/riscv: Implement runtime data endianness via MSTATUS bits target/riscv: Fix page table walk endianness for big-endian harts target/riscv: Add big-endian CPU configuration field and reset logic hw/riscv/boot: Honour data endianness
target/riscv: Expose and document the CPU 'big-endian' property tests/functional: Add RISC-V endianness testPhilippe Mathieu-Daudé (6):
target/riscv: InitializeDisasContext::mo_endianonce target/riscv: Assert PTE.N/PBMT bits are not expected on RV32 target/riscv: De-indent some code inget_physical_address()target/riscv: Removetarget_ulonguse inget_physical_address()hw/riscv/boot: Rewritesetup_rom_reset_vec()using load/store API hw/riscv/boot: Replacecpu_to_le32()->const_le32()docs/system/target-riscv.rst | 29 ++++++ target/riscv/cpu.h | 1 + target/riscv/internals.h | 29 ++++-- target/riscv/cpu_cfg_fields.h.inc | 1 + hw/riscv/boot.c | 93 ++++++++++--------- target/riscv/cpu.c | 15 ++- target/riscv/cpu_helper.c | 44 +++++---- target/riscv/tcg/tcg-cpu.c | 2 + target/riscv/translate.c | 22 ++--- target/riscv/insn_trans/trans_rva.c.inc | 4 +- target/riscv/insn_trans/trans_rvd.c.inc | 4 +- target/riscv/insn_trans/trans_rvf.c.inc | 4 +- target/riscv/insn_trans/trans_rvi.c.inc | 8 +- target/riscv/insn_trans/trans_rvzacas.c.inc | 4 +- target/riscv/insn_trans/trans_rvzalasr.c.inc | 4 +- target/riscv/insn_trans/trans_rvzce.c.inc | 4 +- target/riscv/insn_trans/trans_rvzfh.c.inc | 4 +-
Quoted 9 lines
target/riscv/insn_trans/trans_rvzicfiss.c.inc | 4 +- target/riscv/insn_trans/trans_xmips.c.inc | 8 +- target/riscv/insn_trans/trans_xthead.c.inc | 16 ++-- target/riscv/insn_trans/trans_zilsd.c.inc | 4 +- tests/functional/riscv64/meson.build | 1 + tests/functional/riscv64/test_endianness.py | 57 ++++++++++++ 23 files changed, 240 insertions(+), 122 deletions(-) create mode 100644 tests/functional/riscv64/test_endianness.py
Seems there are some build failures:
https://github.com/p-b-o/qemu-ci/actions/runs/26537039673
Regards,
Pierrick
Quoted 64 lines · Pierrick Bouvier
Quoted 60 lines · Philippe Mathieu-Daudé
v11:
- rewrite some patches while reviewing for mainstream integrationv10 cover from Djordje:
Hi all,
This series adds big-endian data support for RISC-V system emulation.
Following the discussion on previous versions, this series models fixed-endian harts, not a mixed-endian implementation where software can toggle MBE/SBE/UBE at runtime. The reset value of MBE/SBE/UBE is selected from the CPU configuration. These bits remain outside the mstatus/mstatush/sstatus writable masks, so they are effectively hardwired to 0 for the default little-endian CPUs and to 1 when
big-endian=on.The user-visible
big-endianCPU property is exposed only after the data path, boot path, and page-table walk support are in place, so the series remains bisectable.Djordje Todorovic (6):
target/riscv: Implement runtime data endianness via MSTATUS bits target/riscv: Fix page table walk endianness for big-endian harts target/riscv: Add big-endian CPU configuration field and reset logic hw/riscv/boot: Honour data endianness
target/riscv: Expose and document the CPU 'big-endian' property tests/functional: Add RISC-V endianness testPhilippe Mathieu-Daudé (6):
target/riscv: InitializeDisasContext::mo_endianonce target/riscv: Assert PTE.N/PBMT bits are not expected on RV32 target/riscv: De-indent some code inget_physical_address()target/riscv: Removetarget_ulonguse inget_physical_address()hw/riscv/boot: Rewritesetup_rom_reset_vec()using load/store API hw/riscv/boot: Replacecpu_to_le32()->const_le32()docs/system/target-riscv.rst | 29 ++++++ target/riscv/cpu.h | 1 + target/riscv/internals.h | 29 ++++-- target/riscv/cpu_cfg_fields.h.inc | 1 + hw/riscv/boot.c | 93 ++++++++++--------- target/riscv/cpu.c | 15 ++- target/riscv/cpu_helper.c | 44 +++++---- target/riscv/tcg/tcg-cpu.c | 2 + target/riscv/translate.c | 22 ++--- target/riscv/insn_trans/trans_rva.c.inc | 4 +- target/riscv/insn_trans/trans_rvd.c.inc | 4 +- target/riscv/insn_trans/trans_rvf.c.inc | 4 +- target/riscv/insn_trans/trans_rvi.c.inc | 8 +- target/riscv/insn_trans/trans_rvzacas.c.inc | 4 +- target/riscv/insn_trans/trans_rvzalasr.c.inc | 4 +- target/riscv/insn_trans/trans_rvzce.c.inc | 4 +- target/riscv/insn_trans/trans_rvzfh.c.inc | 4 +- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 4 +- target/riscv/insn_trans/trans_xmips.c.inc | 8 +- target/riscv/insn_trans/trans_xthead.c.inc | 16 ++-- target/riscv/insn_trans/trans_zilsd.c.inc | 4 +- tests/functional/riscv64/meson.build | 1 + tests/functional/riscv64/test_endianness.py | 57 ++++++++++++ 23 files changed, 240 insertions(+), 122 deletions(-)Quoted 2 lines
create mode 100644
tests/functional/riscv64/test_endianness.py
Quoted 2 lines
Seems there are some build failures:
https://github.com/p-b-o/qemu-ci/actions/runs/26537039673
IIUC this branch was applied /before/ commit c2444a426ec ("Merge tag 'single-binary-20260527'). Now it depends on Alistair riscv queue so I need to wait before respinning.
Quoted 3 lines
Regards,
Pierrick
Alistair Francis
AppliedQuoted 29 lines · Philippe Mathieu-Daudé
v11:
- rewrite some patches while reviewing for mainstream integrationv10 cover from Djordje:
Hi all,
This series adds big-endian data support for RISC-V system emulation.
Following the discussion on previous versions, this series models fixed-endian harts, not a mixed-endian implementation where software can toggle MBE/SBE/UBE at runtime. The reset value of MBE/SBE/UBE is selected from the CPU configuration. These bits remain outside the mstatus/mstatush/sstatus writable masks, so they are effectively hardwired to 0 for the default little-endian CPUs and to 1 when
big-endian=on.The user-visible
big-endianCPU property is exposed only after the data path, boot path, and page-table walk support are in place, so the series remains bisectable.Djordje Todorovic (6):
target/riscv: Implement runtime data endianness via MSTATUS bits target/riscv: Fix page table walk endianness for big-endian harts target/riscv: Add big-endian CPU configuration field and reset logic hw/riscv/boot: Honour data endianness
target/riscv: Expose and document the CPU 'big-endian' property tests/functional: Add RISC-V endianness test
Quoted 7 lines
Philippe Mathieu-Daudé (6):
target/riscv: InitializeDisasContext::mo_endianonce target/riscv: Assert PTE.N/PBMT bits are not expected on RV32 target/riscv: De-indent some code inget_physical_address()target/riscv: Removetarget_ulonguse inget_physical_address()hw/riscv/boot: Rewritesetup_rom_reset_vec()using load/store API hw/riscv/boot: Replacecpu_to_le32()->const_le32()
Thanks!
Applied to riscv-to-apply.next with patch 3 dropped
Alistair
Quoted 31 lines
docs/system/target-riscv.rst | 29 ++++++ target/riscv/cpu.h | 1 + target/riscv/internals.h | 29 ++++-- target/riscv/cpu_cfg_fields.h.inc | 1 + hw/riscv/boot.c | 93 ++++++++++--------- target/riscv/cpu.c | 15 ++- target/riscv/cpu_helper.c | 44 +++++---- target/riscv/tcg/tcg-cpu.c | 2 + target/riscv/translate.c | 22 ++--- target/riscv/insn_trans/trans_rva.c.inc | 4 +- target/riscv/insn_trans/trans_rvd.c.inc | 4 +- target/riscv/insn_trans/trans_rvf.c.inc | 4 +- target/riscv/insn_trans/trans_rvi.c.inc | 8 +- target/riscv/insn_trans/trans_rvzacas.c.inc | 4 +- target/riscv/insn_trans/trans_rvzalasr.c.inc | 4 +- target/riscv/insn_trans/trans_rvzce.c.inc | 4 +- target/riscv/insn_trans/trans_rvzfh.c.inc | 4 +- target/riscv/insn_trans/trans_rvzicfiss.c.inc | 4 +- target/riscv/insn_trans/trans_xmips.c.inc | 8 +- target/riscv/insn_trans/trans_xthead.c.inc | 16 ++-- target/riscv/insn_trans/trans_zilsd.c.inc | 4 +- tests/functional/riscv64/meson.build | 1 + tests/functional/riscv64/test_endianness.py | 57 ++++++++++++ 23 files changed, 240 insertions(+), 122 deletions(-) create mode 100644 tests/functional/riscv64/test_endianness.py--
2.53.0