Skip to contentSkip to conversation

Support the true Zicclsm extension

PATCH Superseded [email protected] 3 participants 11 messages Apr 24 – May 13 RISC-V

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover Support the true Zicclsm extension

From: Frank Chang <[email protected]>

Support the true Zicclsm extension so that we can trap misaligned accesses when the extension is disabled.

Zicclsm is set to true by default to be backwards compatible with the old RISC-V QEMU design.

To disable Zicclsm, simply set zicclsm=false, e.g.:
-cpu rv64,zicclsm=false

QEMU will raise a misaligned load/store exception when executing misaligned load/store instructions.

Frank Chang (4):
target/riscv: Add Zicclsm CPU option
target/riscv: Support raising misaligned exceptions for scalar
loads/stores
target/riscv: Support raising misaligned exceptions for vector
loads/stores
target/riscv: Expose Zicclsm extension as a CPU property

 target/riscv/cpu.c                      |  4 +-
 target/riscv/cpu_cfg_fields.h.inc       |  2 +
 target/riscv/insn_trans/trans_rvi.c.inc |  6 ++
 target/riscv/insn_trans/trans_rvv.c.inc |  3 +-
 target/riscv/vector_helper.c            | 82 ++++++++++++++++++++-----
 5 files changed, 80 insertions(+), 17 deletions(-)

--
2.43.0

#1 lore raw

PATCH 1/4 target/riscv: Add Zicclsm CPU option

From: Frank Chang <[email protected]>

Add Zicclsm CPU option so that user can turn on/off misaligned loads and stores support.

---
 target/riscv/cpu.c                | 2 +-
 target/riscv/cpu_cfg_fields.h.inc | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
target/riscv/cpu.c +1 -1
index ce15a17c37d..df56b64a736 100644
@@ -127,7 +127,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(zicboz, PRIV_VERSION_1_12_0, ext_zicboz),
     ISA_EXT_DATA_ENTRY(ziccamoa, PRIV_VERSION_1_11_0, has_priv_1_11),
     ISA_EXT_DATA_ENTRY(ziccif, PRIV_VERSION_1_11_0, has_priv_1_11),
-    ISA_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),
target/riscv/cpu_cfg_fields.h.inc +2 -0
index 734fa079f28..fc55dc6d892 100644
@@ -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
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] target/riscv: Add Zicclsm CPU option

Quoted 8 lines · On Fri, Apr 24, 2026 at 8:29 PM <[email protected]> wrote:

From: Frank Chang <[email protected]>

Add Zicclsm CPU option so that user can turn on/off misaligned loads and stores support.

Alistair

Quoted 35 lines
---
 target/riscv/cpu.c                | 2 +-
 target/riscv/cpu_cfg_fields.h.inc | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
target/riscv/cpu.c +1 -1
index ce15a17c37d..df56b64a736 100644
@@ -127,7 +127,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(zicboz, PRIV_VERSION_1_12_0, ext_zicboz),
     ISA_EXT_DATA_ENTRY(ziccamoa, PRIV_VERSION_1_11_0, has_priv_1_11),
     ISA_EXT_DATA_ENTRY(ziccif, PRIV_VERSION_1_11_0, has_priv_1_11),
-    ISA_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),
target/riscv/cpu_cfg_fields.h.inc +2 -1
index 734fa079f28..fc55dc6d892 100644
@@ -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)
--

2.43.0

#3 lore raw

PATCH 2/4 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/insn_trans/trans_rvi.c.inc | 6 ++++++
 1 file changed, 6 insertions(+)
target/riscv/insn_trans/trans_rvi.c.inc +6 -0
index 2c82ae41a77..2662fc5c2a2 100644
@@ -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
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] target/riscv: Support raising misaligned exceptions for scalar loads/stores

Quoted 8 lines · On Fri, Apr 24, 2026 at 8:29 PM <[email protected]> wrote:

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.

Alistair

Quoted 32 lines
---
 target/riscv/insn_trans/trans_rvi.c.inc | 6 ++++++
 1 file changed, 6 insertions(+)
target/riscv/insn_trans/trans_rvi.c.inc +6 -1
index 2c82ae41a77..2662fc5c2a2 100644
@@ -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);
--

2.43.0

#5 lore raw

PATCH 3/4 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/insn_trans/trans_rvv.c.inc |  3 +-
 target/riscv/vector_helper.c            | 82 ++++++++++++++++++++-----
 2 files changed, 69 insertions(+), 16 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +2 -1
index 5b72926b3c1..c8f106c0bab 100644
@@ -1190,9 +1190,10 @@ static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf,
      * Update vstart with the number of processed elements.
      * Use the helper function if either:
      * - vstart is not 0.
+     * - Zicclsm is disabled, so misaligned accesses could trap.
      */
 
-    bool use_helper_fn = !s->vstart_eq_zero;
+    bool use_helper_fn = !s->vstart_eq_zero || !s->cfg_ptr->ext_zicclsm;
 
     if (!use_helper_fn) {
         uint32_t size = s->cfg_ptr->vlenb * nf;
target/riscv/vector_helper.c +67 -15

124 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH 3/4] target/riscv: Support raising misaligned exceptions for vector loads/stores

Quoted 4 lines · On 4/24/26 20:27, [email protected] wrote:
target/riscv/insn_trans/trans_rvv.c.inc +0 -0
@@ -1190,9 +1190,10 @@ static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf,
       * Update vstart with the number of processed elements.

target/riscv/insn_trans/trans_rvv.c.inc static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf, · line 1199

Quoted 10 lines
target/riscv/insn_trans/trans_rvv.c.inc +2 -1
       * Use the helper function if either:
       * - vstart is not 0.
+     * - Zicclsm is disabled, so misaligned accesses could trap.
       */
 
-    bool use_helper_fn = !s->vstart_eq_zero;
+    bool use_helper_fn = !s->vstart_eq_zero || !s->cfg_ptr->ext_zicclsm;
 
      if (!use_helper_fn) {
          uint32_t size = s->cfg_ptr->vlenb * nf;

There is a whole set of MO_ALIGN_* values you can use to select the alignment of the vector element:

log2_esz << MO_ASHIFT

for log2_esz <= 6 (aka MO_ALIGN_64).

r~

#7 lore raw

Re: [PATCH 3/4] target/riscv: Support raising misaligned exceptions for vector loads/stores

In reply to [email protected]
Quoted 11 lines · On 4/24/26 20:27, [email protected] wrote:
target/riscv/vector_helper.c +11 -0
+static MemOpIdx vext_make_memop_idx(CPURISCVState *env, size_t size)
+{
+    int mmu_idx = riscv_env_mmu_index(env, false);
+    MemOp memop;
+
+    switch (size) {
+    case 1:
+        memop = MO_UB;
+        break;
+    case 2:
+        memop = MO_LEUW;

target/riscv/vector_helper.c static inline void vext_set_elem_mask(void *v0, int index, · line 223

Quoted 10 lines
target/riscv/vector_helper.c +10 -0
+        break;
+    case 4:
+        memop = MO_LEUL;
+        break;
+    case 8:
+        memop = MO_LEUQ;
+        break;
+    default:
+        g_assert_not_reached();
+    }

memop = size_memop(size) | mo_endian_env(env);

r~

#8 lore raw

Re: [PATCH 3/4] target/riscv: Support raising misaligned exceptions for vector loads/stores

Richard Henderson <[email protected]> 於 2026年4月24日週五 下午9:21寫道:

Quoted 26 lines · On 4/24/26 20:27, [email protected] wrote:
Quoted 11 lines
target/riscv/vector_helper.c +11 -0
+static MemOpIdx vext_make_memop_idx(CPURISCVState *env, size_t size)
+{
+    int mmu_idx = riscv_env_mmu_index(env, false);
+    MemOp memop;
+
+    switch (size) {
+    case 1:
+        memop = MO_UB;
+        break;
+    case 2:
+        memop = MO_LEUW;

target/riscv/vector_helper.c static inline void vext_set_elem_mask(void *v0, int index, · line 223

Quoted 10 lines
target/riscv/vector_helper.c +10 -0
+        break;
+    case 4:
+        memop = MO_LEUL;
+        break;
+    case 8:
+        memop = MO_LEUQ;
+        break;
+    default:
+        g_assert_not_reached();
+    }

memop = size_memop(size) | mo_endian_env(env);

Quoted 1 line

r~

Thanks Richard,

I've updated the patchset and sent out the v2 patchset:
https://lore.kernel.org/qemu-devel/[email protected]/T/#u

Regards,
Frank Chang

Quoted 1 line
#9 lore raw

PATCH 4/4 target/riscv: Expose Zicclsm extension as a CPU property

In reply to [email protected]

From: Frank Chang <[email protected]>

Expose Zicclsm extension as a CPU property to allow user to turn on/off Zicclsm extension.

Zicclsm is set to true by default to be backward compatible with the old RISC-V QEMU design.

---
 target/riscv/cpu.c | 2 ++
 1 file changed, 2 insertions(+)
target/riscv/cpu.c +2 -0
index df56b64a736..7f5e06ef83d 100644
@@ -1327,6 +1327,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
     MULTI_EXT_CFG_BOOL("zicbop", ext_zicbop, true),
     MULTI_EXT_CFG_BOOL("zicboz", ext_zicboz, true),
 
+    MULTI_EXT_CFG_BOOL("zicclsm", ext_zicclsm, true),
+
     MULTI_EXT_CFG_BOOL("zmmul", ext_zmmul, false),
 
     MULTI_EXT_CFG_BOOL("zca", ext_zca, false),
Signature · 1 line
2.43.0
Reviewed-by on v1 4/4
#10 lore raw

Re: [PATCH 4/4] target/riscv: Expose Zicclsm extension as a CPU property

Quoted 11 lines · On Fri, Apr 24, 2026 at 8:29 PM <[email protected]> wrote:

From: Frank Chang <[email protected]>

Expose Zicclsm extension as a CPU property to allow user to turn on/off Zicclsm extension.

Zicclsm is set to true by default to be backward compatible with the old RISC-V QEMU design.

Alistair

Quoted 21 lines
---
 target/riscv/cpu.c | 2 ++
 1 file changed, 2 insertions(+)
target/riscv/cpu.c +2 -1
index df56b64a736..7f5e06ef83d 100644
@@ -1327,6 +1327,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
     MULTI_EXT_CFG_BOOL("zicbop", ext_zicbop, true),
     MULTI_EXT_CFG_BOOL("zicboz", ext_zicboz, true),
 
+    MULTI_EXT_CFG_BOOL("zicclsm", ext_zicclsm, true),
+
     MULTI_EXT_CFG_BOOL("zmmul", ext_zmmul, false),
 
     MULTI_EXT_CFG_BOOL("zca", ext_zca, false),
--

2.43.0