target/riscv: Support the true Zicclsm extension
Subsystems (2)
v8 cover target/riscv: Support the true Zicclsm extension
From: Frank Chang <[email protected]>
Support the true Zicclsm extension so that we can trap misaligned accesses when Zicclsm is disabled.
To enable/disable Zicclsm, simply set zicclsm=[true|false], e.g.:
-cpu rv64,zicclsm=[true|false]
QEMU will raise a misaligned load/store exception when executing misaligned load/store instructions if Zicclsm is disabled.
Changelog:
v8:
* Squash patch#1 into patch#2.
v7:
* Raise the misaligned exceptions for floating-point instructions (RVF, RVD, Zfh) when Zicclsm is disabled.
* Add Zicclsm unit tests.
v6:
* Reorder and squash Zicclsm CPU property and RVA22U64 profile patches to make every single commit to pass make check.
v5:
* Add Zicclsm to RVA22U64 profile explicitly.
* Rebase to the latest riscv-to-apply.next.
v4:
* Align ROM reset vector data at 8-byte aligned offsets.
v3:
* Enable Zicclsm for the compatible CPUs.
* Rebase to the latest riscv-to-apply.next.
v2:
* Use (size_memop(size) | mo_endian_env(env)) to calculate MemOp.
* Use (log2_esz << MO_ASHIFT) to calculate aligment MemOp for vector load/store whole register instructions.
Frank Chang (7):
target/riscv: Add Zicclsm CPU option and enable it for the eligible
CPUs
hw/riscv: sifive_u: Align ROM reset vector data target/riscv: Support raising misaligned exceptions for scalar
loads/stores
target/riscv: Support raising misaligned exceptions for floating-point
loads/stores
target/riscv: Support raising misaligned exceptions for vector
loads/stores
target/riscv: Expose Zicclsm as a CPU property and update RVA22U64
profile
tests/tcg/riscv64: Add test for Zicclsm
hw/riscv/sifive_u.c | 11 +- target/riscv/cpu.c | 10 +- target/riscv/cpu_cfg_fields.h.inc | 2 + target/riscv/tcg/insn_trans/trans_rvd.c.inc | 6 + target/riscv/tcg/insn_trans/trans_rvf.c.inc | 6 + target/riscv/tcg/insn_trans/trans_rvi.c.inc | 6 + target/riscv/tcg/insn_trans/trans_rvv.c.inc | 18 +- target/riscv/tcg/insn_trans/trans_rvzfh.c.inc | 6 + target/riscv/tcg/vector_helper.c | 65 +++- tests/tcg/riscv64/Makefile.softmmu-target | 22 ++ tests/tcg/riscv64/test-zicclsm.S | 368 ++++++++++++++++++ 11 files changed, 496 insertions(+), 24 deletions(-) create mode 100644 tests/tcg/riscv64/test-zicclsm.S
--
2.43.0
PATCH v8 1/7 target/riscv: Add Zicclsm CPU option and enable it for the eligible CPUs
From: Frank Chang <[email protected]>
Add Zicclsm CPU option and enable it for the following eligible CPUs:
- Base 32 CPU (to be backward compatible)
- Base 64 CPU (to be backward compatible)
- XuanTie (T-Head) C908
- Tenstorrent Ascalon
- Ventana Veyron V1
- XiangShan Kunminghu
- MIPS P8700 (ISA doesn't include Zicclsm, but their datasheet claims that it has unaligned load/store support in hardware)
--- target/riscv/cpu.c | 7 +++++++ target/riscv/cpu_cfg_fields.h.inc | 2 ++ 2 files changed, 9 insertions(+)
target/riscv/cpu.c +7 -0
@@ -3268,6 +3268,7 @@ static const TypeInfo riscv_cpu_type_infos[] = { .cfg.ext_zicbom = true, .cfg.ext_zicbop = true, .cfg.ext_zicboz = true, + .cfg.ext_zicclsm = true, .cfg.ext_zicntr = true, .cfg.ext_zicsr = true, .cfg.ext_zifencei = true,
@@ -3340,6 +3341,7 @@ static const TypeInfo riscv_cpu_type_infos[] = { .cfg.ext_zicbom = true, .cfg.ext_zicbop = true, .cfg.ext_zicboz = true, + .cfg.ext_zicclsm = true, .cfg.ext_zicntr = true, .cfg.ext_zicsr = true, .cfg.ext_zifencei = true,
@@ -3426,6 +3428,7 @@ static const TypeInfo riscv_cpu_type_infos[] = { .cfg.ext_zbs = true, .cfg.ext_zkt = true, .cfg.ext_zbkc = true, + .cfg.ext_zicclsm = true, .cfg.ext_zicsr = true, .cfg.ext_zifencei = true, .cfg.ext_zihintpause = true,
@@ -3469,6 +3472,7 @@ static const TypeInfo riscv_cpu_type_infos[] = { .cfg.ext_zicbom = true, .cfg.ext_zicbop = true, .cfg.ext_zicboz = true, + .cfg.ext_zicclsm = true, .cfg.ext_zicntr = true, .cfg.ext_zicond = true, .cfg.ext_zicsr = true,
@@ -3518,6 +3522,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {/* ISA extensions */ .cfg.mmu = true, + .cfg.ext_zicclsm = true, .cfg.ext_zifencei = true, .cfg.ext_zicsr = true, .cfg.pmp = true,
@@ -3577,6 +3582,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {* The RISC-V Instruction Set Manual: Volume I * Unprivileged Architecture */ + .cfg.ext_zicclsm = true, .cfg.ext_zicntr = true, .cfg.ext_zihpm = true, .cfg.ext_zihintntl = true,
@@ -3633,6 +3639,7 @@ static const TypeInfo riscv_cpu_type_infos[] = { .misa_ext = RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU, .priv_spec = PRIV_VERSION_1_12_0, .cfg.max_satp_mode = VM_1_10_SV48, + .cfg.ext_zicclsm = true, .cfg.ext_zifencei = true, .cfg.ext_zicsr = true, .cfg.mmu = true,
target/riscv/cpu_cfg_fields.h.inc +2 -0
@@ -139,6 +139,8 @@ BOOL_FIELD(has_priv_1_11) /* Always enabled for TCG if has_priv_1_11 */ BOOL_FIELD(ext_ziccrse) +BOOL_FIELD(ext_zicclsm) + /* Vendor-specific custom extensions */ BOOL_FIELD(ext_xtheadba) BOOL_FIELD(ext_xtheadbb)
Signature · 1 line
2.43.0
PATCH v8 2/7 hw/riscv: sifive_u: Align ROM reset vector data
In reply to [email protected]From: Frank Chang <[email protected]>
The SiFive U ROM reset vector data needs proper 8-byte alignment for RV64 ld instructions. The misaligned load will cause exception when Zicclsm is supported as SiFive U CPU doesn't support hardware misaligned loads and stores.
Add padding to ensure start_addr and fdt_load_addr are placed at 8-byte aligned offsets and adjust the load instruction offsets to match the new data layout.
--- hw/riscv/sifive_u.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
hw/riscv/sifive_u.c +6 -5
@@ -564,19 +564,20 @@ static void sifive_u_machine_init(MachineState *machine)0 ,0 ,0x00028067 ,/* jr t0 */ +0x00000000 ,/* padding for alignment */ start_addr,/* start: .dword */ start_addr_hi32, fdt_load_addr,/* fdt_laddr: .dword */ fdt_load_addr_hi32, - 0x00000000, /* fw_dyn: */ }; +if (riscv_is_32bit (&s->soc.u_cpus)) { - reset_vec[4 ] =0x0202a583 ;/* lw a1, 32(t0) */ - reset_vec[5 ] =0x0182a283 ;/* lw t0, 24(t0) */ + reset_vec[4 ] =0x0242a583 ;/* lw a1, 36(t0) */ + reset_vec[5 ] =0x01c2a283 ;/* lw t0, 28(t0) */ }else { - reset_vec[4 ] =0x0202b583 ;/* ld a1, 32(t0) */ - reset_vec[5 ] =0x0182b283 ;/* ld t0, 24(t0) */ + reset_vec[4 ] =0x0242b583 ;/* ld a1, 36(t0) */ + reset_vec[5 ] =0x01c2b283 ;/* ld t0, 28(t0) */ }
Signature · 1 line
2.43.0
PATCH v8 3/7 target/riscv: Support raising misaligned exceptions for scalar loads/stores
In reply to [email protected]From: Frank Chang <[email protected]>
When the Zicclsm extension is not enabled, raise misaligned load/store exceptions for misaligned accesses from scalar load/store instructions.
--- target/riscv/tcg/insn_trans/trans_rvi.c.inc | 6 ++++++ 1 file changed, 6 insertions(+)
target/riscv/tcg/insn_trans/trans_rvi.c.inc +6 -0
@@ -413,6 +413,9 @@ static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; } + if (!ctx->cfg_ptr->ext_zicclsm) { + memop |= MO_ALIGN; + } decode_save_opc(ctx, 0); if (get_xl(ctx) == MXL_RV128) { out = gen_load_i128(ctx, a, memop);
@@ -524,6 +527,9 @@ static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; } + if (!ctx->cfg_ptr->ext_zicclsm) { + memop |= MO_ALIGN; + } decode_save_opc(ctx, 0); if (get_xl(ctx) == MXL_RV128) { return gen_store_i128(ctx, a, memop);
Signature · 1 line
2.43.0
PATCH v8 4/7 target/riscv: Support raising misaligned exceptions for floating-point loads/stores
In reply to [email protected]From: Frank Chang <[email protected]>
When the Zicclsm extension is not enabled, raise misaligned load/store exceptions for misaligned accesses from floating-point load/store instructions (RVF, RVD, Zfh).
--- target/riscv/tcg/insn_trans/trans_rvd.c.inc | 6 ++++++ target/riscv/tcg/insn_trans/trans_rvf.c.inc | 6 ++++++ target/riscv/tcg/insn_trans/trans_rvzfh.c.inc | 6 ++++++ 3 files changed, 18 insertions(+)
target/riscv/tcg/insn_trans/trans_rvd.c.inc +6 -0
@@ -61,6 +61,9 @@ static bool trans_fld(DisasContext *ctx, arg_fld *a) memop |= MO_ATOM_IFALIGN; } memop |= ctx->mo_endianness; + if (!ctx->cfg_ptr->ext_zicclsm) { + memop |= MO_ALIGN; + } decode_save_opc(ctx, 0); addr = get_address(ctx, a->rs1, a->imm);
@@ -86,6 +89,9 @@ static bool trans_fsd(DisasContext *ctx, arg_fsd *a) memop |= MO_ATOM_IFALIGN; } memop |= ctx->mo_endianness; + if (!ctx->cfg_ptr->ext_zicclsm) { + memop |= MO_ALIGN; + } decode_save_opc(ctx, 0); addr = get_address(ctx, a->rs1, a->imm);
target/riscv/tcg/insn_trans/trans_rvf.c.inc +6 -0
@@ -52,6 +52,9 @@ static bool trans_flw(DisasContext *ctx, arg_flw *a) if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; } + if (!ctx->cfg_ptr->ext_zicclsm) { + memop |= MO_ALIGN; + } decode_save_opc(ctx, 0); addr = get_address(ctx, a->rs1, a->imm);
@@ -75,6 +78,9 @@ static bool trans_fsw(DisasContext *ctx, arg_fsw *a) if (ctx->cfg_ptr->ext_zama16b) { memop |= MO_ATOM_WITHIN16; } + if (!ctx->cfg_ptr->ext_zicclsm) { + memop |= MO_ALIGN; + } decode_save_opc(ctx, 0); addr = get_address(ctx, a->rs1, a->imm);
target/riscv/tcg/insn_trans/trans_rvzfh.c.inc +6 -0
@@ -50,6 +50,9 @@ static bool trans_flh(DisasContext *ctx, arg_flh *a) REQUIRE_ZFHMIN_OR_ZFBFMIN(ctx); memop |= ctx->mo_endianness; + if (!ctx->cfg_ptr->ext_zicclsm) { + memop |= MO_ALIGN; + } decode_save_opc(ctx, 0); t0 = get_gpr(ctx, a->rs1, EXT_NONE); if (a->imm) {
@@ -75,6 +78,9 @@ static bool trans_fsh(DisasContext *ctx, arg_fsh *a) REQUIRE_ZFHMIN_OR_ZFBFMIN(ctx); memop |= ctx->mo_endianness; + if (!ctx->cfg_ptr->ext_zicclsm) { + memop |= MO_ALIGN; + } decode_save_opc(ctx, 0); t0 = get_gpr(ctx, a->rs1, EXT_NONE); if (a->imm) {
Signature · 1 line
2.43.0
PATCH v8 5/7 target/riscv: Support raising misaligned exceptions for vector loads/stores
In reply to [email protected]From: Frank Chang <[email protected]>
When the Zicclsm extension is not enabled, raise misaligned load/store exceptions for misaligned accesses from vector load/store instructions.
We will skip the host fast-path and fall back to the slow TLB-path to raise misaligned load/store exceptions for the misaligned accesses when Zicclsm extension is disabled.
--- target/riscv/tcg/insn_trans/trans_rvv.c.inc | 18 +++++- target/riscv/tcg/vector_helper.c | 65 ++++++++++++++++----- 2 files changed, 65 insertions(+), 18 deletions(-)
target/riscv/tcg/insn_trans/trans_rvv.c.inc +15 -3
@@ -1191,26 +1191,38 @@ static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf, * Use the helper function if either: * - vstart is not 0. */ - bool use_helper_fn = !s->vstart_eq_zero; if (!use_helper_fn) { uint32_t size = s->cfg_ptr->vlenb * nf; TCGv_i64 t8 = tcg_temp_new_i64(); MemOp atomicity = MO_ATOM_NONE; + MemOp alignment = MO_UNALN; + + /* + * If Zicclsm is disabled, require alignment based on element size. + * Use MO_ALIGN_* based on log2_esz (0 = MO_UNALN, 1 = MO_ALIGN_2, etc). + */ + if (!s->cfg_ptr->ext_zicclsm) { + alignment = log2_esz << MO_ASHIFT; + } + if (log2_esz == 0) { atomicity = MO_ATOM_NONE; } else { atomicity = MO_ATOM_IFALIGN_PAIR; } + for (int i = 0; i < size; i += 8) { TCGv addr = get_address(s, rs1, i); if (is_load) { - tcg_gen_qemu_ld_i64(t8, addr, s->mem_idx, MO_LEUQ | atomicity); + tcg_gen_qemu_ld_i64(t8, addr, s->mem_idx, + MO_LEUQ | atomicity | alignment); tcg_gen_st_i64(t8, tcg_env, vreg_ofs(s, vd) + i); } else { tcg_gen_ld_i64(t8, tcg_env, vreg_ofs(s, vd) + i); - tcg_gen_qemu_st_i64(t8, addr, s->mem_idx, MO_LEUQ | atomicity); + tcg_gen_qemu_st_i64(t8, addr, s->mem_idx, + MO_LEUQ | atomicity | alignment); } if (i == size - 8) { tcg_gen_movi_i32(cpu_vstart, 0);
target/riscv/tcg/vector_helper.c +50 -15
107 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 6/7 target/riscv: Expose Zicclsm as a CPU property and update RVA22U64 profile
In reply to [email protected]From: Frank Chang <[email protected]>
Update Zicclsm ISA string and expose it as a CPU property to allow user to turn on/off Zicclsm extension.
In addition, Zicclsm extension is mandatory for the RVA22U64 profile. Previously, Zicclsm was enabled automatically when has_priv_1_11 was true. Now that Zicclsm has been converted to an explicit CPU option, it must be explicitly added to the RVA22U64 profile's extension list to ensure the profile remains compliant with the specification.
--- target/riscv/cpu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/riscv/cpu.c +2 -1
@@ -164,7 +164,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zicboz,PRIV_VERSION_1_12_0 , ext_zicboz),ISA_INTERNAL_EXT_DATA_ENTRY (ziccamoa,PRIV_VERSION_1_11_0 , has_priv_1_11),ISA_INTERNAL_EXT_DATA_ENTRY (ziccif,PRIV_VERSION_1_11_0 , has_priv_1_11), -ISA_INTERNAL_EXT_DATA_ENTRY (zicclsm,PRIV_VERSION_1_11_0 , has_priv_1_11), +ISA_EXT_DATA_ENTRY (zicclsm,PRIV_VERSION_1_11_0 , ext_zicclsm),ISA_EXT_DATA_ENTRY (ziccrse,PRIV_VERSION_1_11_0 , ext_ziccrse),ISA_EXT_DATA_ENTRY (zicfilp,PRIV_VERSION_1_12_0 , ext_zicfilp), ISA_EXT_DATA_ENTRY(zicfiss, PRIV_VERSION_1_13_0, ext_zicfiss),
@@ -2237,6 +2237,7 @@ static RISCVCPUProfile RVA22U64 = {CPU_CFG_OFFSET (ext_zkt),CPU_CFG_OFFSET (ext_zicntr),CPU_CFG_OFFSET (ext_zihpm),CPU_CFG_OFFSET (ext_zicbom),CPU_CFG_OFFSET (ext_zicbop),CPU_CFG_OFFSET (ext_zicboz), +CPU_CFG_OFFSET (ext_zicclsm),/* mandatory named features for this profile */ CPU_CFG_OFFSET(ext_zic64b),
Signature · 1 line
2.43.0
PATCH v8 7/7 tests/tcg/riscv64: Add test for Zicclsm
In reply to [email protected]From: Frank Chang <[email protected]>
Cover scalar, floating-point, vector, and segmented misaligned accesses with Zicclsm enabled and disabled. Clean up both generated test binaries.
To build and run the tests:
make -C build/tests/tcg/riscv64-softmmu \
CC=riscv64-unknown-elf-gcc LD=riscv64-unknown-elf-ld \
test-zicclsm test-zicclsm-off
make -C build/tests/tcg/riscv64-softmmu \
run-test-zicclsm run-test-zicclsm-offTo clean the generated binaries and objects:
make -C build/tests/tcg/riscv64-softmmu clean
--- tests/tcg/riscv64/Makefile.softmmu-target | 22 ++ tests/tcg/riscv64/test-zicclsm.S | 368 ++++++++++++++++++++++ 2 files changed, 390 insertions(+) create mode 100644 tests/tcg/riscv64/test-zicclsm.S
tests/tcg/riscv64/Makefile.softmmu-target +22 -0
@@ -41,5 +41,27 @@ comma:= , run-test-crc32: test-crc32 $(call run-test, $<, $(QEMU) -cpu rv64$(comma)xlrbr=true $(QEMU_OPTS)$<) +# Zicclsm: misaligned load/store support. Assemble one source twice: the +# default build expects every misaligned access to succeed (zicclsm=true), +# the -DZICCLSM_DISABLED build expects every one to trap (zicclsm=false). +ZICCLSM_MARCH = -march=rv64gcv_zfh +CLEANFILES += test-zicclsm test-zicclsm-off + +test-zicclsm: test-zicclsm.S $(LINK_SCRIPT) + $(CC) $(CFLAGS) $(ZICCLSM_MARCH) $< -Wa,--noexecstack -c -o test-zicclsm.o + $(LD) $(LDFLAGS) test-zicclsm.o -o $@ + +test-zicclsm-off: test-zicclsm.S $(LINK_SCRIPT) + $(CC) $(CFLAGS) $(ZICCLSM_MARCH) -DZICCLSM_DISABLED $< -Wa,--noexecstack -c -o test-zicclsm-off.o + $(LD) $(LDFLAGS) test-zicclsm-off.o -o $@ + +EXTRA_RUNS += run-test-zicclsm +run-test-zicclsm: test-zicclsm + $(call run-test, $<, $(QEMU) -cpu rv64$(comma)v=true$(comma)zfh=true$(comma)zicclsm=true $(QEMU_OPTS)$<) + +EXTRA_RUNS += run-test-zicclsm-off +run-test-zicclsm-off: test-zicclsm-off + $(call run-test, $<, $(QEMU) -cpu rv64$(comma)v=true$(comma)zfh=true$(comma)zicclsm=false $(QEMU_OPTS)$<) + # We don't currently support the multiarch system tests undefine MULTIARCH_TESTS
tests/tcg/riscv64/test-zicclsm.S new file +368 -0
368 lines not on this page · raw message
Signature · 1 line
2.43.0
Alistair
AppliedQuoted 37 lines · On Mon, 2026-08-10 at 13:56 +0800, [email protected] wrote:
From: Frank Chang <[email protected]>
Support the true Zicclsm extension so that we can trap misaligned accesses
when Zicclsm is disabled.To enable/disable Zicclsm, simply set zicclsm=[true|false], e.g.:
-cpu rv64,zicclsm=[true|false]QEMU will raise a misaligned load/store exception when executing misaligned
load/store instructions if Zicclsm is disabled.Changelog:
v8:
* Squash patch#1 into patch#2.
v7:
* Raise the misaligned exceptions for floating-point instructions (RVF, RVD, Zfh) when Zicclsm is disabled.
* Add Zicclsm unit tests.
v6:
* Reorder and squash Zicclsm CPU property and RVA22U64 profile patches to
make every single commit to passmake check.
v5:
* Add Zicclsm to RVA22U64 profile explicitly.
* Rebase to the latest riscv-to-apply.next.
v4:
* Align ROM reset vector data at 8-byte aligned offsets. v3:
* Enable Zicclsm for the compatible CPUs.
* Rebase to the latest riscv-to-apply.next.
v2:
* Use (size_memop(size)|mo_endian_env(env)) to calculate MemOp.
* Use (log2_esz<<MO_ASHIFT) to calculate aligment MemOp for vector load/store whole register instructions.
Quoted 14 lines
Frank Chang (7):
target/riscv: Add Zicclsm CPU option and enable it for the eligible CPUs
hw/riscv:sifive_u: Align ROM reset vector data target/riscv: Support raising misaligned exceptions for scalar loads/stores
target/riscv: Support raising misaligned exceptions for floating- point
loads/stores
target/riscv: Support raising misaligned exceptions for vector loads/stores
target/riscv: Expose Zicclsm as a CPU property and update RVA22U64 profile
tests/tcg/riscv64: Add test for Zicclsm
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 19 lines
hw/riscv/sifive_u.c | 11 +- target/riscv/cpu.c | 10 +- target/riscv/cpu_cfg_fields.h.inc | 2 +
target/riscv/tcg/insn_trans/trans_rvd.c.inc | 6 + target/riscv/tcg/insn_trans/trans_rvf.c.inc | 6 + target/riscv/tcg/insn_trans/trans_rvi.c.inc | 6 + target/riscv/tcg/insn_trans/trans_rvv.c.inc | 18 +-target/riscv/tcg/insn_trans/trans_rvzfh.c.inc | 6 + target/riscv/tcg/vector_helper.c | 65 +++- tests/tcg/riscv64/Makefile.softmmu-target | 22 ++ tests/tcg/riscv64/test-zicclsm.S | 368
++++++++++++++++++11 files changed, 496 insertions(+), 24 deletions(-)create mode 100644
tests/tcg/riscv64/test-zicclsm.S--
2.43.0