Skip to contentSkip to conversation

RISC-V: Experimental fetch support for 48-bit and 64-bit instructions

RFC PATCH In review TANG Tiancheng 2 participants 16 messages Nov 18 – Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover RISC-V: Experimental fetch support for 48-bit and 64-bit instructions

QEMU's RISC-V TCG implementation currently supports only 16-bit compressed instructions and 32-bit instructions. This series adds experimental support for fetching 48-bit and 64-bit instructions and for halfword alignment, following Zl draft 65c377a [1].

This work is motivated in part by an AME proposal that encodes operand datatypes in instructions and is still under evaluation [3]. Larger immediates and longer-range direct calls are other uses under discussion [1,2]. This series provides instruction-fetch, alignment and trap-reporting support for long instructions, without implementing individual instruction semantics.

The x-zl CPU property is disabled by default and supported only under TCG. When enabled, the translator and built-in disassembler share the length rules, and halfword alignment does not require C/Zca. Compressed decoding remains disabled if C/Zca are disabled. Complete 48/64-bit encodings still raise illegal-instruction exceptions, and the disassembler prints "unknown".

For an undefined length, fetching stops after the third 16-bit parcel and no instruction bits are reported. This avoids additional fetches solely for trap reporting. Feedback on this choice would be useful.

Patch 1 fixes incomplete instruction reads in the disassembler and can be taken independently of Zl support.

The code was built for RV32/RV64 user and system emulation. RV32/RV64 bare-metal tests check RAM-boundary fetches and trap values, halfword jumps and returns, and rejection of compressed instructions with C/Zca disabled. The Zl tests and existing MEPC regression passed with Capstone enabled and disabled.

[1] https://github.com/riscv/riscv-long-instruction/blob/65c377a665feb29a4848384ad566a9873c5fd423/src/unpriv/zl.adoc
[2] https://github.com/riscv/riscv-isa-manual/pull/3269#issuecomment-5328426143
[3] https://riscv.atlassian.net/wiki/spaces/AMEX/pages/2445836308/2026-11-09+AME+Meeting+Notes

---
TANG Tiancheng (8):
disas/riscv: Reject incomplete instruction reads target/riscv: Share instruction length helper target/riscv: Prepare Zl instruction alignment disas/riscv: Decode Zl instruction lengths target/riscv: Limit instruction bits to the trap CSR width target/riscv: Fetch 48-bit and 64-bit instructions target/riscv: Enable experimental Zl fetch support tests/tcg/riscv: Check Zl fetching and halfword alignment

 disas/riscv.c                               |  49 ++++++---
 disas/riscv.h                               |   4 +-
 target/riscv/cpu.c                          |   6 +
 target/riscv/cpu.h                          |  10 +-
 target/riscv/cpu_cfg_fields.h.inc           |   1 +
 target/riscv/insn_length.h                  |  29 +++++
 target/riscv/internals.h                    |  17 +--
 target/riscv/tcg/cpu_helper.c               |   7 +-
 target/riscv/tcg/csr.c                      |   7 +-
 target/riscv/tcg/insn_trans/trans_rvi.c.inc |   8 +-
 target/riscv/tcg/op_helper.c                |  10 +-
 target/riscv/tcg/translate.c                | 101 ++++++++++++-----
 tests/tcg/riscv/zl/zl.S                     | 165 ++++++++++++++++++++++++++++
 tests/tcg/riscv32/system/meson.build        |   9 ++
 tests/tcg/riscv64/system/meson.build        |   9 ++
 15 files changed, 359 insertions(+), 73 deletions(-)

---
base-commit: f29ccc287a6cfdd51d3a4fd17fbe9af3310a0ac5
change-id: 20261117-zl-master-8ebcaae0953f

Best regards,

Signature · 1 line
TANG Tiancheng <[email protected]>
#1 lore raw

RFC PATCH 1/8 disas/riscv: Reject incomplete instruction reads

If a later parcel read fails, print_insn_riscv() decodes the available bits with the missing bits left as zero. A truncated instruction can therefore be displayed as a complete, unrelated instruction.

Report every failed required read at its actual address and return -1. If the first 2-byte read fails, return -1 instead of the error code from read_memory_func(). The disassembly loops would otherwise treat the positive error code as an instruction length.

---
 disas/riscv.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
disas/riscv.c +2 -6
index ca0d6e90b37321e8c2b7ec07f3ae51f0797585cc..eb4851b4cd4b14a43cc6592bd5011c6df53ec219 100644
@@ -3510,12 +3510,8 @@ print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
     for (n = 0; n < len; n += 2) {
         status = (*info->read_memory_func)(memaddr + n, packet, 2, info);
         if (status != 0) {
-            /* Don't fail just because we fell off the end.  */
-            if (n > 0) {
-                break;
-            }
-            (*info->memory_error_func)(status, memaddr, info);
-            return status;
+            (*info->memory_error_func)(status, memaddr + n, info);
+            return -1;
         }
         inst |= ((rv_inst) bfd_getl16(packet)) << (8 * n);
         if (n == 0) {
Signature · 1 line
2.43.0
Reviewed-by on v1 1/8
#2 lore raw

Re: [PATCH RFC 1/8] disas/riscv: Reject incomplete instruction reads

Quoted 11 lines · TANG Tiancheng

If a later parcel read fails, print_insn_riscv() decodes the available bits with the missing bits left as zero. A truncated instruction can therefore be displayed as a complete, unrelated instruction.

Report every failed required read at its actual address and return -1. If the first 2-byte read fails, return -1 instead of the error code from read_memory_func(). The disassembly loops would otherwise treat the positive error code as an instruction length.

Quoted 28 lines
---
 disas/riscv.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
disas/riscv.c +2 -7
index ca0d6e90b37321e8c2b7ec07f3ae51f0797585cc..eb4851b4cd4b14a43cc6592bd5011c6df53ec219 100644
@@ -3510,12 +3510,8 @@ print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
     for (n = 0; n < len; n += 2) {
         status = (*info->read_memory_func)(memaddr + n, packet, 2, info);
         if (status != 0) {
-            /* Don't fail just because we fell off the end.  */
-            if (n > 0) {
-                break;
-            }
-            (*info->memory_error_func)(status, memaddr, info);
-            return status;
+            (*info->memory_error_func)(status, memaddr + n, info);
+            return -1;
         }
         inst |= ((rv_inst) bfd_getl16(packet)) << (8 * n);
         if (n == 0) {
 
--

2.43.0

#3 lore raw

RFC PATCH 2/8 target/riscv: Share instruction length helper

In reply to TANG Tiancheng

The translator and built-in disassembler duplicate the 16/32-bit length rule. Move it into a shared header.

---
 disas/riscv.c              |  9 ++-------
 target/riscv/insn_length.h | 12 ++++++++++++
 target/riscv/internals.h   |  6 +-----
 3 files changed, 15 insertions(+), 12 deletions(-)
disas/riscv.c +2 -7
index eb4851b4cd4b14a43cc6592bd5011c6df53ec219..8c9e06b46f19efd6aa9f605ccd0bf3716fa62d44 100644
@@ -21,6 +21,7 @@
 #include "qemu/bitops.h"
 #include "disas/dis-asm.h"
 #include "target/riscv/cpu_cfg.h"
+#include "target/riscv/insn_length.h"
 #include "disas/riscv.h"
 
 /* Vendor extensions */
@@ -3170,12 +3171,6 @@ static bool check_constraints(rv_decode *dec, const rvc_constraint *c)
     return true;
 }
 
-/* Same as insn_len() from target/riscv/internals.h */
-static size_t inst_length(rv_inst inst)
-{
-    return (inst & 3) == 3 ? 4 : 2;
-}
-
 /* format instruction */
 
 static GString *format_inst(size_t tab, rv_decode *dec,
@@ -3515,7 +3510,7 @@ print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
         }
         inst |= ((rv_inst) bfd_getl16(packet)) << (8 * n);
         if (n == 0) {
-            len = inst_length(inst);
+            len = insn_len(inst);
         }
     }
 
target/riscv/insn_length.h new file +12 -0
new file mode 100644
index 0000000000000000000000000000000000000000..bfb3af0e6358249daf464fed670f09d7184d307b
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* RISC-V instruction length classification. */
+
+#ifndef TARGET_RISCV_INSN_LENGTH_H
+#define TARGET_RISCV_INSN_LENGTH_H
+
+static inline int insn_len(uint16_t first_word)
+{
+    return (first_word & 3) == 3 ? 4 : 2;
+}
+
+#endif
target/riscv/internals.h +1 -5
index 5466879a4aa541db8d6753c7e1eec8f3128a99c8..2d48abeef5dea2cb9d0e97852495fed75d670ae1 100644
@@ -23,6 +23,7 @@
 #include "hw/core/registerfields.h"
 #include "fpu/softfloat-types.h"
 #include "target/riscv/cpu_bits.h"
+#include "target/riscv/insn_length.h"
 
 /*
  * The current MMU Modes are:
@@ -259,11 +260,6 @@ static inline target_ulong adjust_addr_virt(CPURISCVState *env,
     return adjust_addr_body(env, addr, true);
 }
 
-static inline int insn_len(uint16_t first_word)
-{
-    return (first_word & 3) == 3 ? 4 : 2;
-}
-
 int riscv_monitor_get_register_legacy(CPUState *cs, const char *name,
                                       int64_t *pval);
 
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH RFC 2/8] target/riscv: Share instruction length helper

Quoted 51 lines · TANG Tiancheng

The translator and built-in disassembler duplicate the 16/32-bit length rule. Move it into a shared header.

---
 disas/riscv.c              |  9 ++-------
 target/riscv/insn_length.h | 12 ++++++++++++
 target/riscv/internals.h   |  6 +-----
 3 files changed, 15 insertions(+), 12 deletions(-)
disas/riscv.c +2 -7
index eb4851b4cd4b14a43cc6592bd5011c6df53ec219..8c9e06b46f19efd6aa9f605ccd0bf3716fa62d44 100644
@@ -21,6 +21,7 @@
 #include "qemu/bitops.h"
 #include "disas/dis-asm.h"
 #include "target/riscv/cpu_cfg.h"
+#include "target/riscv/insn_length.h"
 #include "disas/riscv.h"
 
 /* Vendor extensions */
@@ -3170,12 +3171,6 @@ static bool check_constraints(rv_decode *dec, const rvc_constraint *c)
     return true;
 }
 
-/* Same as insn_len() from target/riscv/internals.h */
-static size_t inst_length(rv_inst inst)
-{
-    return (inst & 3) == 3 ? 4 : 2;
-}
-
 /* format instruction */
 
 static GString *format_inst(size_t tab, rv_decode *dec,
@@ -3515,7 +3510,7 @@ print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
         }
         inst |= ((rv_inst) bfd_getl16(packet)) << (8 * n);
         if (n == 0) {
-            len = inst_length(inst);
+            len = insn_len(inst);
         }
     }
 
target/riscv/insn_length.h new file +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..bfb3af0e6358249daf464fed670f09d7184d307b

target/riscv/insn_length.h line 7

Quoted 7 lines
target/riscv/insn_length.h new file +7 -0
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* RISC-V instruction length classification. */
+
+#ifndef TARGET_RISCV_INSN_LENGTH_H
+#define TARGET_RISCV_INSN_LENGTH_H
+
+static inline int insn_len(uint16_t first_word)

Now that this lives in a header that disas/ includes, a riscv_ prefix (riscv_insn_len) would match the helpers added in 4/8.

Quoted 34 lines
target/riscv/insn_length.h new file +5 -0
+{
+    return (first_word & 3) == 3 ? 4 : 2;
+}
+
+#endif
target/riscv/internals.h +1 -6
index 5466879a4aa541db8d6753c7e1eec8f3128a99c8..2d48abeef5dea2cb9d0e97852495fed75d670ae1 100644
@@ -23,6 +23,7 @@
 #include "hw/core/registerfields.h"
 #include "fpu/softfloat-types.h"
 #include "target/riscv/cpu_bits.h"
+#include "target/riscv/insn_length.h"
 
 /*
  * The current MMU Modes are:
@@ -259,11 +260,6 @@ static inline target_ulong adjust_addr_virt(CPURISCVState *env,
     return adjust_addr_body(env, addr, true);
 }
 
-static inline int insn_len(uint16_t first_word)
-{
-    return (first_word & 3) == 3 ? 4 : 2;
-}
-
 int riscv_monitor_get_register_legacy(CPUState *cs, const char *name,
                                       int64_t *pval);
 
 
--

2.43.0

#5 lore raw

RFC PATCH 3/8 target/riscv: Prepare Zl instruction alignment

In reply to TANG Tiancheng

Zl requires IALIGN=16 even when compressed instructions are disabled. Add an internal ext_zl flag and apply it to jumps, EPC/DPC masking, trap returns and misa.C writes. Keep compressed instruction decoding under its existing extension checks.

---
 target/riscv/cpu.h                          | 10 +++++++---
 target/riscv/cpu_cfg_fields.h.inc           |  1 +
 target/riscv/internals.h                    |  4 ++--
 target/riscv/tcg/csr.c                      |  7 +++++--
 target/riscv/tcg/insn_trans/trans_rvi.c.inc |  8 ++------
 target/riscv/tcg/op_helper.c                | 10 ++++------
 target/riscv/tcg/translate.c                |  4 +---
 7 files changed, 22 insertions(+), 22 deletions(-)
target/riscv/cpu.h +7 -3
index 49a1186bdbb2b9b56f3d9ad1e8ba18ae2010c7cb..f157a40b019a1bc23408d2a70530d1b58b7278d0 100644
@@ -921,10 +921,14 @@ bool riscv_cpu_eff_priv(CPURISCVState *env, privilege_mode_t *priv, bool *virt)
     return mode_modified;
 }
 
-static inline bool riscv_cpu_allow_16bit_insn(const RISCVCPUConfig *cfg,
-                                              uint32_t priv_ver,
-                                              uint32_t misa_ext)
+/* Instruction alignment is independent of compressed instruction decoding. */
+static inline bool riscv_cpu_ialign16(const RISCVCPUConfig *cfg,
+                                    uint32_t priv_ver, uint32_t misa_ext)
 {
+    if (cfg->ext_zl) {
+        return true;
+    }
+
     /* In priv spec version 1.12 or newer, C always implies Zca */
     if (priv_ver >= PRIV_VERSION_1_12_0) {
         return cfg->ext_zca;
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 85fc3ad8bb8ca059b4c391afde2ab14c95018145..30a8d21b7553b7d22b1ca2647dc00789330657f5 100644
@@ -30,6 +30,7 @@ BOOL_FIELD(ext_zks)
 BOOL_FIELD(ext_zksed)
 BOOL_FIELD(ext_zksh)
 BOOL_FIELD(ext_zkt)
+BOOL_FIELD(ext_zl)
 BOOL_FIELD(ext_zifencei)
 BOOL_FIELD(ext_zicntr)
 BOOL_FIELD(ext_zicsr)
target/riscv/internals.h +2 -2
index 2d48abeef5dea2cb9d0e97852495fed75d670ae1..4e11888b03e0dcb050426892e1758d733273bf99 100644
@@ -191,9 +191,9 @@ static inline target_ulong get_xepc_mask(CPURISCVState *env)
 
     /*
      * When IALIGN=32, both low bits must be zero.
-     * When IALIGN=16 (has C or Zc* extensions), only bit 0 must be zero.
+     * When IALIGN=16 (C, Zc* or Zl fetch), only bit 0 must be zero.
      */
-    if (riscv_has_ext(env, RVC) || cpu->cfg.ext_zca ||
+    if (cpu->cfg.ext_zl || riscv_has_ext(env, RVC) || cpu->cfg.ext_zca ||
         cpu->cfg.ext_zcb || cpu->cfg.ext_zcd || cpu->cfg.ext_zce ||
         cpu->cfg.ext_zcf || cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt) {
         return ~(target_ulong)1;
target/riscv/tcg/csr.c +5 -2
index af444e3e6f32f5d0b6cc86eeb530705a8bcad0e6..3972d1f4112f3ad52ae352d4c55a2799aed0c992 100644
@@ -2231,8 +2231,11 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,
     /* Mask extensions that are not supported by this hart */
     val &= env->misa_ext_mask;
 
-    /* drop write if RVC is cleared and next instruction is not aligned */
-    if ((env->misa_ext & RVC) && !(val & RVC) &&
+    /*
+     * Ignore writes that clear C if the next instruction is not 4-byte aligned.
+     * With Zl, IALIGN remains 16 and the write need not be suppressed.
+     */
+    if (!cpu->cfg.ext_zl && (env->misa_ext & RVC) && !(val & RVC) &&
          (get_next_pc(env, ra) & 3) != 0) {
         qemu_log_mask(LOG_GUEST_ERROR, "Unable to write MISA ext value "
                       "0x%x, MISA.C disable failed\n", env->misa_ext);
target/riscv/tcg/insn_trans/trans_rvi.c.inc +2 -6
index 4784ea1607e74eb5b0076425a732b5d3817b3482..ba5e274da3b778d564930386471387a36ddd63ce 100644
@@ -151,9 +151,7 @@ static bool trans_jalr(DisasContext *ctx, arg_jalr *a)
         tcg_gen_ext32s_tl(target_pc, target_pc);
     }
 
-    if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
-                                    ctx->priv_ver,
-                                    ctx->misa_ext)) {
+    if (!riscv_cpu_ialign16(ctx->cfg_ptr, ctx->priv_ver, ctx->misa_ext)) {
         TCGv t0 = tcg_temp_new();
 
         misaligned = gen_new_label();
@@ -302,9 +300,7 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TCGCond cond)
 
     gen_set_label(l); /* branch taken */
 
-    if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
-                                    ctx->priv_ver,
-                                    ctx->misa_ext) &&
+    if (!riscv_cpu_ialign16(ctx->cfg_ptr, ctx->priv_ver, ctx->misa_ext) &&
         (a->imm & 0x3)) {
         /* misaligned */
         TCGv target_pc = tcg_temp_new();
target/riscv/tcg/op_helper.c +4 -6
index f9c79fe6b9006c7667acfda4865ca59fbb970d5d..0dda250203790840e0c0ed20d917eeb645c61a60 100644
@@ -318,9 +318,8 @@ target_ulong HELPER(sret)(CPURISCVState *env)
     }
 
     target_ulong retpc = env->sepc & get_xepc_mask(env);
-    if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
-                                    env->priv_ver,
-                                    env->misa_ext) && (retpc & 0x3)) {
+    if (!riscv_cpu_ialign16(&env_archcpu(env)->cfg, env->priv_ver,
+                          env->misa_ext) && (retpc & 0x3)) {
         riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
     }
 
@@ -395,9 +394,8 @@ static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc,
         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
     }
 
-    if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
-                                    env->priv_ver,
-                                    env->misa_ext) && (retpc & 0x3)) {
+    if (!riscv_cpu_ialign16(&env_archcpu(env)->cfg, env->priv_ver,
+                          env->misa_ext) && (retpc & 0x3)) {
         riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, ra);
     }
 
target/riscv/tcg/translate.c +1 -3
index 79b0f444efbd954c48c14812b0f566ace7830245..51024ee9a37df548ec27587c9b4bb240afd1d5a5 100644
@@ -630,9 +630,7 @@ static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
     TCGv succ_pc = dest_gpr(ctx, rd);
 
     /* check misaligned: */
-    if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
-                                    ctx->priv_ver,
-                                    ctx->misa_ext)) {
+    if (!riscv_cpu_ialign16(ctx->cfg_ptr, ctx->priv_ver, ctx->misa_ext)) {
         if ((imm & 0x3) != 0) {
             TCGv target_pc = tcg_temp_new();
             gen_pc_plus_diff(target_pc, ctx, imm);
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH RFC 3/8] target/riscv: Prepare Zl instruction alignment

Quoted 22 lines · TANG Tiancheng

Zl requires IALIGN=16 even when compressed instructions are disabled. Add an internal ext_zl flag and apply it to jumps, EPC/DPC masking, trap returns and misa.C writes. Keep compressed instruction decoding under its existing extension checks.

---
 target/riscv/cpu.h                          | 10 +++++++---
 target/riscv/cpu_cfg_fields.h.inc           |  1 +
 target/riscv/internals.h                    |  4 ++--
 target/riscv/tcg/csr.c                      |  7 +++++--
 target/riscv/tcg/insn_trans/trans_rvi.c.inc |  8 ++------
 target/riscv/tcg/op_helper.c                | 10 ++++------
 target/riscv/tcg/translate.c                |  4 +---
 7 files changed, 22 insertions(+), 22 deletions(-)
target/riscv/cpu.h +0 -0
index 49a1186bdbb2b9b56f3d9ad1e8ba18ae2010c7cb..f157a40b019a1bc23408d2a70530d1b58b7278d0 100644

target/riscv/cpu.h bool riscv_cpu_eff_priv(CPURISCVState *env, privilege_mode_t *priv, bool *virt) · line 926

Quoted 9 lines
target/riscv/cpu.h +3 -3
@@ -921,10 +921,14 @@ bool riscv_cpu_eff_priv(CPURISCVState *env, privilege_mode_t *priv, bool *virt)
     return mode_modified;
 }
 
-static inline bool riscv_cpu_allow_16bit_insn(const RISCVCPUConfig *cfg,
-                                              uint32_t priv_ver,
-                                              uint32_t misa_ext)
+/* Instruction alignment is independent of compressed instruction decoding. */
+static inline bool riscv_cpu_ialign16(const RISCVCPUConfig *cfg,
+                                    uint32_t priv_ver, uint32_t misa_ext)

Looks like an alignment problem for the continuation line?

Quoted 128 lines
target/riscv/cpu.h +4 -0
 {
+    if (cfg->ext_zl) {
+        return true;
+    }
+
     /* In priv spec version 1.12 or newer, C always implies Zca */
     if (priv_ver >= PRIV_VERSION_1_12_0) {
         return cfg->ext_zca;
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 85fc3ad8bb8ca059b4c391afde2ab14c95018145..30a8d21b7553b7d22b1ca2647dc00789330657f5 100644
@@ -30,6 +30,7 @@ BOOL_FIELD(ext_zks)
 BOOL_FIELD(ext_zksed)
 BOOL_FIELD(ext_zksh)
 BOOL_FIELD(ext_zkt)
+BOOL_FIELD(ext_zl)
 BOOL_FIELD(ext_zifencei)
 BOOL_FIELD(ext_zicntr)
 BOOL_FIELD(ext_zicsr)
target/riscv/internals.h +2 -2
index 2d48abeef5dea2cb9d0e97852495fed75d670ae1..4e11888b03e0dcb050426892e1758d733273bf99 100644
@@ -191,9 +191,9 @@ static inline target_ulong get_xepc_mask(CPURISCVState *env)
 
     /*
      * When IALIGN=32, both low bits must be zero.
-     * When IALIGN=16 (has C or Zc* extensions), only bit 0 must be zero.
+     * When IALIGN=16 (C, Zc* or Zl fetch), only bit 0 must be zero.
      */
-    if (riscv_has_ext(env, RVC) || cpu->cfg.ext_zca ||
+    if (cpu->cfg.ext_zl || riscv_has_ext(env, RVC) || cpu->cfg.ext_zca ||
         cpu->cfg.ext_zcb || cpu->cfg.ext_zcd || cpu->cfg.ext_zce ||
         cpu->cfg.ext_zcf || cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt) {
         return ~(target_ulong)1;
target/riscv/tcg/csr.c +5 -2
index af444e3e6f32f5d0b6cc86eeb530705a8bcad0e6..3972d1f4112f3ad52ae352d4c55a2799aed0c992 100644
@@ -2231,8 +2231,11 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,
     /* Mask extensions that are not supported by this hart */
     val &= env->misa_ext_mask;
 
-    /* drop write if RVC is cleared and next instruction is not aligned */
-    if ((env->misa_ext & RVC) && !(val & RVC) &&
+    /*
+     * Ignore writes that clear C if the next instruction is not 4-byte aligned.
+     * With Zl, IALIGN remains 16 and the write need not be suppressed.
+     */
+    if (!cpu->cfg.ext_zl && (env->misa_ext & RVC) && !(val & RVC) &&
          (get_next_pc(env, ra) & 3) != 0) {
         qemu_log_mask(LOG_GUEST_ERROR, "Unable to write MISA ext value "
                       "0x%x, MISA.C disable failed\n", env->misa_ext);
target/riscv/tcg/insn_trans/trans_rvi.c.inc +2 -6
index 4784ea1607e74eb5b0076425a732b5d3817b3482..ba5e274da3b778d564930386471387a36ddd63ce 100644
@@ -151,9 +151,7 @@ static bool trans_jalr(DisasContext *ctx, arg_jalr *a)
         tcg_gen_ext32s_tl(target_pc, target_pc);
     }
 
-    if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
-                                    ctx->priv_ver,
-                                    ctx->misa_ext)) {
+    if (!riscv_cpu_ialign16(ctx->cfg_ptr, ctx->priv_ver, ctx->misa_ext)) {
         TCGv t0 = tcg_temp_new();
 
         misaligned = gen_new_label();
@@ -302,9 +300,7 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TCGCond cond)
 
     gen_set_label(l); /* branch taken */
 
-    if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
-                                    ctx->priv_ver,
-                                    ctx->misa_ext) &&
+    if (!riscv_cpu_ialign16(ctx->cfg_ptr, ctx->priv_ver, ctx->misa_ext) &&
         (a->imm & 0x3)) {
         /* misaligned */
         TCGv target_pc = tcg_temp_new();
target/riscv/tcg/op_helper.c +4 -6
index f9c79fe6b9006c7667acfda4865ca59fbb970d5d..0dda250203790840e0c0ed20d917eeb645c61a60 100644
@@ -318,9 +318,8 @@ target_ulong HELPER(sret)(CPURISCVState *env)
     }
 
     target_ulong retpc = env->sepc & get_xepc_mask(env);
-    if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
-                                    env->priv_ver,
-                                    env->misa_ext) && (retpc & 0x3)) {
+    if (!riscv_cpu_ialign16(&env_archcpu(env)->cfg, env->priv_ver,
+                          env->misa_ext) && (retpc & 0x3)) {
         riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
     }
 
@@ -395,9 +394,8 @@ static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc,
         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
     }
 
-    if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
-                                    env->priv_ver,
-                                    env->misa_ext) && (retpc & 0x3)) {
+    if (!riscv_cpu_ialign16(&env_archcpu(env)->cfg, env->priv_ver,
+                          env->misa_ext) && (retpc & 0x3)) {
         riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, ra);
     }
 
target/riscv/tcg/translate.c +1 -4
index 79b0f444efbd954c48c14812b0f566ace7830245..51024ee9a37df548ec27587c9b4bb240afd1d5a5 100644
@@ -630,9 +630,7 @@ static void gen_jal(DisasContext *ctx, int rd, target_ulong imm)
     TCGv succ_pc = dest_gpr(ctx, rd);
 
     /* check misaligned: */
-    if (!riscv_cpu_allow_16bit_insn(ctx->cfg_ptr,
-                                    ctx->priv_ver,
-                                    ctx->misa_ext)) {
+    if (!riscv_cpu_ialign16(ctx->cfg_ptr, ctx->priv_ver, ctx->misa_ext)) {
         if ((imm & 0x3) != 0) {
             TCGv target_pc = tcg_temp_new();
             gen_pc_plus_diff(target_pc, ctx, imm);
 
--

2.43.0

#7 lore raw

RFC PATCH 4/8 disas/riscv: Decode Zl instruction lengths

In reply to TANG Tiancheng

The Zl draft identifies long instructions from parcel A and determines their 48/64-bit length from parcel C.

Read complete 48-bit and 64-bit encodings and return their decoded lengths. The disassembler does not yet decode instruction mnemonics for these encodings, so print "unknown" rather than decoding part of a long instruction as a 16-bit or 32-bit instruction. When opcode display is enabled, also print the full encoding in hexadecimal.

---
 disas/riscv.c              | 34 ++++++++++++++++++++++++++++------
 disas/riscv.h              |  4 ++--
 target/riscv/insn_length.h | 17 +++++++++++++++++
 3 files changed, 47 insertions(+), 8 deletions(-)
disas/riscv.c +28 -6
index 8c9e06b46f19efd6aa9f605ccd0bf3716fa62d44..d2a2b322374643dc51b3bc51775484da9a99e039 100644
@@ -3489,17 +3489,21 @@ static GString *disasm_inst(rv_isa isa, uint64_t pc, rv_inst inst,
     return format_inst(24, &dec, op);
 }
 
-#define INST_FMT_2 "%04x              "
-#define INST_FMT_4 "%08x          "
+#define INST_FMT_2 "%04" PRIx64 "              "
+#define INST_FMT_4 "%08" PRIx64 "          "
+#define INST_FMT_6 "%012" PRIx64 "      "
+#define INST_FMT_8 "%016" PRIx64 "  "
 
 static int
 print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
 {
+    const RISCVCPUConfig *cfg = info->target_info;
     bfd_byte packet[2];
     rv_inst inst = 0;
     size_t len = 2;
     bfd_vma n;
     int status;
+    bool zl = cfg && cfg->ext_zl;
 
     /* Instructions are made of 2-byte packets in little-endian order */
     for (n = 0; n < len; n += 2) {
@@ -3510,7 +3514,15 @@ print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
         }
         inst |= ((rv_inst) bfd_getl16(packet)) << (8 * n);
         if (n == 0) {
-            len = insn_len(inst);
+            /* Zl needs parcel C to determine a long instruction's length. */
+            len = zl && riscv_insn_is_long(inst) ? 6 : insn_len(inst);
+        } else if (zl && n == 4) {
+            len = riscv_zl_insn_length(inst >> 32);
+        }
+        if (len == 0) {
+            (*info->fprintf_func)(info->stream,
+                                  "<unsupported instruction length>");
+            return -1;
         }
     }
 
@@ -3522,14 +3534,24 @@ print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
         case 4:
             (*info->fprintf_func)(info->stream, INST_FMT_4, inst);
             break;
+        case 6:
+            (*info->fprintf_func)(info->stream, INST_FMT_6, inst);
+            break;
+        case 8:
+            (*info->fprintf_func)(info->stream, INST_FMT_8, inst);
+            break;
         default:
             g_assert_not_reached();
         }
     }
 
-    g_autoptr(GString) str =
-        disasm_inst(isa, memaddr, inst,
-                    (const RISCVCPUConfig *)info->target_info);
+    /* Long instruction mnemonics are not decoded yet. */
+    if (len > 4) {
+        (*info->fprintf_func)(info->stream, "unknown");
+        return len;
+    }
+
+    g_autoptr(GString) str = disasm_inst(isa, memaddr, inst, cfg);
     (*info->fprintf_func)(info->stream, "%s", str->str);
 
     return len;
disas/riscv.h +2 -2
index 4f1ffc4ae02fb1e5940e833ef972f80e28603a19..b49bcadab8e4fa1f32f28e1d5ed9035c5b55a359 100644
@@ -11,7 +11,7 @@
 
 /* types */
 
-typedef uint32_t rv_inst;
+typedef uint64_t rv_inst;
 typedef uint16_t rv_opcode;
 
 /* enums */
@@ -184,7 +184,7 @@ struct rv_opcode_data {
 typedef struct {
     const RISCVCPUConfig *cfg;
     uint64_t  pc;
-    uint64_t  inst;
+    rv_inst   inst;
     int32_t   imm;
     int32_t   imm1;
     uint8_t   rd;
target/riscv/insn_length.h +17 -0
index bfb3af0e6358249daf464fed670f09d7184d307b..14bf5c7fa98a388705e023ff7d0d0840d1306c63 100644
@@ -9,4 +9,21 @@ static inline int insn_len(uint16_t first_word)
     return (first_word & 3) == 3 ? 4 : 2;
 }
 
+static inline bool riscv_insn_is_long(uint16_t first_parcel)
+{
+    return (first_parcel & 0x1f) == 0x1f;
+}
+
+/* Call only after fetching parcel C; zero means an undefined length. */
+static inline unsigned riscv_zl_insn_length(uint16_t third_parcel)
+{
+    if ((third_parcel & 3) != 3) {
+        return 6;
+    }
+    if ((third_parcel & 0x1f) != 0x1f) {
+        return 8;
+    }
+    return 0;
+}
+
 #endif
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH RFC 4/8] disas/riscv: Decode Zl instruction lengths

Quoted 51 lines · TANG Tiancheng

The Zl draft identifies long instructions from parcel A and determines their 48/64-bit length from parcel C.

Read complete 48-bit and 64-bit encodings and return their decoded lengths. The disassembler does not yet decode instruction mnemonics for these encodings, so print "unknown" rather than decoding part of a long instruction as a 16-bit or 32-bit instruction. When opcode display is enabled, also print the full encoding in hexadecimal.

---
 disas/riscv.c              | 34 ++++++++++++++++++++++++++++------
 disas/riscv.h              |  4 ++--
 target/riscv/insn_length.h | 17 +++++++++++++++++
 3 files changed, 47 insertions(+), 8 deletions(-)
disas/riscv.c +6 -2
index 8c9e06b46f19efd6aa9f605ccd0bf3716fa62d44..d2a2b322374643dc51b3bc51775484da9a99e039 100644
@@ -3489,17 +3489,21 @@ static GString *disasm_inst(rv_isa isa, uint64_t pc, rv_inst inst,
     return format_inst(24, &dec, op);
 }
 
-#define INST_FMT_2 "%04x              "
-#define INST_FMT_4 "%08x          "
+#define INST_FMT_2 "%04" PRIx64 "              "
+#define INST_FMT_4 "%08" PRIx64 "          "
+#define INST_FMT_6 "%012" PRIx64 "      "
+#define INST_FMT_8 "%016" PRIx64 "  "
 
 static int
 print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
 {
+    const RISCVCPUConfig *cfg = info->target_info;
     bfd_byte packet[2];
     rv_inst inst = 0;
     size_t len = 2;
     bfd_vma n;
     int status;
+    bool zl = cfg && cfg->ext_zl;
 
     /* Instructions are made of 2-byte packets in little-endian order */
     for (n = 0; n < len; n += 2) {
@@ -3510,7 +3514,15 @@ print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
         }
         inst |= ((rv_inst) bfd_getl16(packet)) << (8 * n);
         if (n == 0) {

disas/riscv.c print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa) · line 3525

Quoted 10 lines
disas/riscv.c +9 -1
-            len = insn_len(inst);
+            /* Zl needs parcel C to determine a long instruction's length. */
+            len = zl && riscv_insn_is_long(inst) ? 6 : insn_len(inst);
+        } else if (zl && n == 4) {
+            len = riscv_zl_insn_length(inst >> 32);
+        }
+        if (len == 0) {
+            (*info->fprintf_func)(info->stream,
+                                  "<unsupported instruction length>");
+            return -1;

If the translator fetches through ILEN for reserved lengths, shouldn't the shared length helper return 8 here as well?

Quoted 84 lines
disas/riscv.c +13 -3
         }
     }
 
@@ -3522,14 +3534,24 @@ print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa)
         case 4:
             (*info->fprintf_func)(info->stream, INST_FMT_4, inst);
             break;
+        case 6:
+            (*info->fprintf_func)(info->stream, INST_FMT_6, inst);
+            break;
+        case 8:
+            (*info->fprintf_func)(info->stream, INST_FMT_8, inst);
+            break;
         default:
             g_assert_not_reached();
         }
     }
 
-    g_autoptr(GString) str =
-        disasm_inst(isa, memaddr, inst,
-                    (const RISCVCPUConfig *)info->target_info);
+    /* Long instruction mnemonics are not decoded yet. */
+    if (len > 4) {
+        (*info->fprintf_func)(info->stream, "unknown");
+        return len;
+    }
+
+    g_autoptr(GString) str = disasm_inst(isa, memaddr, inst, cfg);
     (*info->fprintf_func)(info->stream, "%s", str->str);
 
     return len;
disas/riscv.h +2 -2
index 4f1ffc4ae02fb1e5940e833ef972f80e28603a19..b49bcadab8e4fa1f32f28e1d5ed9035c5b55a359 100644
@@ -11,7 +11,7 @@
 
 /* types */
 
-typedef uint32_t rv_inst;
+typedef uint64_t rv_inst;
 typedef uint16_t rv_opcode;
 
 /* enums */
@@ -184,7 +184,7 @@ struct rv_opcode_data {
 typedef struct {
     const RISCVCPUConfig *cfg;
     uint64_t  pc;
-    uint64_t  inst;
+    rv_inst   inst;
     int32_t   imm;
     int32_t   imm1;
     uint8_t   rd;
target/riscv/insn_length.h +17 -1
index bfb3af0e6358249daf464fed670f09d7184d307b..14bf5c7fa98a388705e023ff7d0d0840d1306c63 100644
@@ -9,4 +9,21 @@ static inline int insn_len(uint16_t first_word)
     return (first_word & 3) == 3 ? 4 : 2;
 }
 
+static inline bool riscv_insn_is_long(uint16_t first_parcel)
+{
+    return (first_parcel & 0x1f) == 0x1f;
+}
+
+/* Call only after fetching parcel C; zero means an undefined length. */
+static inline unsigned riscv_zl_insn_length(uint16_t third_parcel)
+{
+    if ((third_parcel & 3) != 3) {
+        return 6;
+    }
+    if ((third_parcel & 0x1f) != 0x1f) {
+        return 8;
+    }
+    return 0;
+}
+
 #endif
 
--

2.43.0

#9 lore raw

RFC PATCH 5/8 target/riscv: Limit instruction bits to the trap CSR width

In reply to TANG Tiancheng

The instruction bits in env->bins can be wider than the destination trap CSR. Keep env->bins intact and limit the reported value to MXLEN for mtval or SXLEN for stval.

---
 target/riscv/tcg/cpu_helper.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
target/riscv/tcg/cpu_helper.c +6 -1
index 58e98e975c29106f39074283a79038dbad255fae..568af11d24ccdfaf953a4d27d1a172847a547508 100644
@@ -2260,7 +2260,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)
             break;
         case RISCV_EXCP_ILLEGAL_INST:
         case RISCV_EXCP_VIRT_INSTRUCTION_FAULT:
-            tval = env->bins;
+            tval = mxlen == 32 ? (uint32_t)env->bins : env->bins;
             break;
         case RISCV_EXCP_BREAKPOINT:
             tval = env->badaddr;
@@ -2377,6 +2377,11 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         sxlen = 16 << riscv_cpu_sxl(env);
         env->scause = cause | ((target_ulong)async << (sxlen - 1));
         env->sepc = env->pc;
+        if (!async && sxlen == 32 &&
+            (cause == RISCV_EXCP_ILLEGAL_INST ||
+             cause == RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) {
+            tval = (uint32_t)tval;
+        }
         env->stval = tval;
         env->htval = htval;
         env->htinst = tinst;
Signature · 1 line
2.43.0
Reviewed-by on v1 5/8
#10 lore raw

Re: [PATCH RFC 5/8] target/riscv: Limit instruction bits to the trap CSR width

Quoted 6 lines · TANG Tiancheng

The instruction bits in env->bins can be wider than the destination trap CSR. Keep env->bins intact and limit the reported value to MXLEN for mtval or SXLEN for stval.

Quoted 34 lines
---
 target/riscv/tcg/cpu_helper.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
target/riscv/tcg/cpu_helper.c +6 -2
index 58e98e975c29106f39074283a79038dbad255fae..568af11d24ccdfaf953a4d27d1a172847a547508 100644
@@ -2260,7 +2260,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)
             break;
         case RISCV_EXCP_ILLEGAL_INST:
         case RISCV_EXCP_VIRT_INSTRUCTION_FAULT:
-            tval = env->bins;
+            tval = mxlen == 32 ? (uint32_t)env->bins : env->bins;
             break;
         case RISCV_EXCP_BREAKPOINT:
             tval = env->badaddr;
@@ -2377,6 +2377,11 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         sxlen = 16 << riscv_cpu_sxl(env);
         env->scause = cause | ((target_ulong)async << (sxlen - 1));
         env->sepc = env->pc;
+        if (!async && sxlen == 32 &&
+            (cause == RISCV_EXCP_ILLEGAL_INST ||
+             cause == RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) {
+            tval = (uint32_t)tval;
+        }
         env->stval = tval;
         env->htval = htval;
         env->htinst = tinst;
 
--

2.43.0

#11 lore raw

RFC PATCH 6/8 target/riscv: Fetch 48-bit and 64-bit instructions

In reply to TANG Tiancheng

Preserve the existing aligned word fetch and halfword fetch sequence. When Zl is enabled, use parcels A and C to determine the instruction length and fetch parcel D only for 64-bit encodings.

Keep the complete encoding in ctx->opcode so gen_exception_illegal() can report it through env->bins.

For an undefined length, stop after parcel C and set env->bins to zero. This implementation reports no instruction bits rather than fetching additional parcels solely for trap reporting.

Stop translation near page ends before a subsequent instruction could require another page.

---
 target/riscv/internals.h     |  7 ++++
 target/riscv/tcg/translate.c | 97 ++++++++++++++++++++++++++++++++------------
 2 files changed, 79 insertions(+), 25 deletions(-)
target/riscv/internals.h +7 -0
index 4e11888b03e0dcb050426892e1758d733273bf99..ddd5ca65adc99d9538c98f4ce28c3c723a967824 100644
@@ -185,6 +185,13 @@ static inline float16 check_nanbox_bf16(CPURISCVState *env, uint64_t f)
     }
 }
 
+/* Long encodings must already include parcel C. */
+static inline bool riscv_zl_insn_has_undefined_length(uint64_t opcode)
+{
+    return riscv_insn_is_long(opcode) &&
+           riscv_zl_insn_length(opcode >> 32) == 0;
+}
+
 static inline target_ulong get_xepc_mask(CPURISCVState *env)
 {
     RISCVCPU *cpu = env_archcpu(env);
target/riscv/tcg/translate.c +72 -25

161 lines not on this page · raw message

Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH RFC 6/8] target/riscv: Fetch 48-bit and 64-bit instructions

Quoted 65 lines · TANG Tiancheng

Preserve the existing aligned word fetch and halfword fetch sequence. When Zl is enabled, use parcels A and C to determine the instruction length and fetch parcel D only for 64-bit encodings.

Keep the complete encoding in ctx->opcode so gen_exception_illegal() can report it through env->bins.

For an undefined length, stop after parcel C and set env->bins to zero. This implementation reports no instruction bits rather than fetching additional parcels solely for trap reporting.

Stop translation near page ends before a subsequent instruction could require another page.

---
 target/riscv/internals.h     |  7 ++++
 target/riscv/tcg/translate.c | 97 ++++++++++++++++++++++++++++++++------------
 2 files changed, 79 insertions(+), 25 deletions(-)
target/riscv/internals.h +7 -0
index 4e11888b03e0dcb050426892e1758d733273bf99..ddd5ca65adc99d9538c98f4ce28c3c723a967824 100644
@@ -185,6 +185,13 @@ static inline float16 check_nanbox_bf16(CPURISCVState *env, uint64_t f)
     }
 }
 
+/* Long encodings must already include parcel C. */
+static inline bool riscv_zl_insn_has_undefined_length(uint64_t opcode)
+{
+    return riscv_insn_is_long(opcode) &&
+           riscv_zl_insn_length(opcode >> 32) == 0;
+}
+
 static inline target_ulong get_xepc_mask(CPURISCVState *env)
 {
     RISCVCPU *cpu = env_archcpu(env);
target/riscv/tcg/translate.c +2 -1
index 51024ee9a37df548ec27587c9b4bb240afd1d5a5..b301c41d3341dce777422a9c70a407818d3407bd 100644
@@ -30,6 +30,7 @@
 #include "semihosting/semihost.h"
 
 #include "internals.h"
+#include "insn_length.h"
 
 #define HELPER_H "helper.h"
 #include "exec/helper-info.c.inc"
@@ -64,7 +65,7 @@ typedef struct DisasContext {
     RISCVMXL xl;
     RISCVMXL address_xl;
     uint32_t misa_ext;
-    uint32_t opcode;
+    uint64_t opcode;
     RISCVExtStatus mstatus_fs;
     RISCVExtStatus mstatus_vs;
     uint32_t mem_idx;
@@ -265,7 +266,11 @@ static void generate_exception(DisasContext *ctx, RISCVException excp)
 
 static void gen_exception_illegal(DisasContext *ctx)

target/riscv/tcg/translate.c static void generate_exception(DisasContext *ctx, RISCVException excp) · line 276

Quoted 10 lines
target/riscv/tcg/translate.c +5 -1
 {
-    tcg_gen_st_i64(tcg_constant_i64(ctx->opcode), tcg_env,
+    uint64_t bits = ctx->cfg_ptr->ext_zl &&
+        riscv_zl_insn_has_undefined_length(ctx->opcode) ? 0 : ctx->opcode;
+
+    /* Undefined lengths have no complete instruction to report. */
+    tcg_gen_st_i64(tcg_constant_i64(bits), tcg_env,
                    offsetof(CPURISCVState, bins));
     if (ctx->virt_inst_excp) {
         generate_exception(ctx, RISCV_EXCP_VIRT_INSTRUCTION_FAULT);

gen_exception_illegal() is shared by every decoder; rather than re-deriving "undefined length" from ctx->opcode here, could fetch_insn() record it (or clear ctx->opcode) once?

Related, for when long loads/stores arrive: env->bins can now hold a 48/64-bit encoding, but riscv_transformed_insn() treats any value with [1:0] == 11 as a 32-bit instruction, so htinst/mtinst would be garbage for a long load/store. It should return 0 for long encodings.

Quoted 57 lines
target/riscv/tcg/translate.c +21 -9
@@ -1240,8 +1245,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
 /* Include decoders for factored-out extensions */
 #include "decode-XVentanaCondOps.c.inc"
 
-/* The specification allows for longer insns, but not supported by qemu. */
-#define MAX_INSN_LEN  4
+#define MAX_INSN_LEN  8
 
 const RISCVDecoder decoder_table[] = {
     { always_true_p, decode_insn32 },
@@ -1254,12 +1258,11 @@ const RISCVDecoder decoder_table[] = {
 
 const size_t decoder_table_size = ARRAY_SIZE(decoder_table);
 
-static void decode_opc(CPURISCVState *env, DisasContext *ctx)
+static bool fetch_insn(CPURISCVState *env, DisasContext *ctx)
 {
-    uint32_t opcode;
+    uint64_t opcode;
     bool pc_is_4byte_align = ((ctx->base.pc_next % 4) == 0);
 
-    ctx->virt_inst_excp = false;
     if (pc_is_4byte_align) {
         /*
          * Load 4 bytes at once to make instruction fetch atomically.
@@ -1276,42 +1279,82 @@ static void decode_opc(CPURISCVState *env, DisasContext *ctx)
          * For unaligned pc, instruction preload may trigger additional
          * page fault so we only load 2 bytes here.
          */
-        opcode = (uint32_t) translator_lduw_end(env, &ctx->base,
-                                                ctx->base.pc_next,
-                                                MO_LE);
+        opcode = translator_lduw_end(env, &ctx->base, ctx->base.pc_next, MO_LE);
     }
-    ctx->ol = ctx->xl;
 
     ctx->cur_insn_len = insn_len((uint16_t)opcode);
     /* Check for compressed insn */
     if (ctx->cur_insn_len == 2) {
         ctx->opcode = (uint16_t)opcode;
+        return true;
+    }
+
+    if (!pc_is_4byte_align) {
+        /* Load last 2 bytes of instruction here */
+        opcode = deposit64(opcode, 16, 16,
+                           translator_lduw_end(env, &ctx->base,
+                                               ctx->base.pc_next + 2, MO_LE));
+    }
+    ctx->opcode = opcode;
+
+    if (ctx->cfg_ptr->ext_zl && riscv_insn_is_long(opcode)) {
+        uint16_t third_parcel =
+            translator_lduw_end(env, &ctx->base, ctx->base.pc_next + 4, MO_LE);
+        unsigned len = riscv_zl_insn_length(third_parcel);
+
+        ctx->opcode = deposit64(ctx->opcode, 32, 16, third_parcel);

target/riscv/tcg/translate.c static void decode_opc(CPURISCVState *env, DisasContext *ctx) · line 1315

Quoted 10 lines
target/riscv/tcg/translate.c +10 -0
+        if (len == 8) {
+            uint16_t fourth_parcel =
+                translator_lduw_end(env, &ctx->base, ctx->base.pc_next + 6,
+                                    MO_LE);
+
+            ctx->opcode = deposit64(ctx->opcode, 48, 16, fourth_parcel);
+        }
+        /* An undefined length stops fetching after three parcels. */
+        ctx->cur_insn_len = len ? len : 6;
+        return len != 0;

This leaves stval = 0 on an illegal-instruction exception, but Sstvala, which QEMU advertises, requires the faulting instruction. For an encoding longer than ILEN, the priv spec defines that as the first ILEN bits, i.e. 64 here.
Counterexample: delegate illegal-instruction to S, execute 003f 2468 5a7f 9abc (reserved length, D in RAM):
cause:2 epc:0x80000090 tval:0x0000000000000000 (expected 0x9abc5a7f2468003f)

Also: without x-zl, the all-ones encoding reported tval 0xffffffff; with x-zl it reports 0.

Quoted 40 lines
target/riscv/tcg/translate.c +21 -12
+    }
+    return true;
+}
+
+static void decode_opc(CPURISCVState *env, DisasContext *ctx)
+{
+    bool valid_length;
+
+    ctx->virt_inst_excp = false;
+    ctx->ol = ctx->xl;
+    valid_length = fetch_insn(env, ctx);
+    if (!valid_length) {
+        goto illegal;
+    }
+
+    switch (ctx->cur_insn_len) {
+    case 2:
         /*
          * The Zca extension is added as way to refer to instructions in the C
-         * extension that do not include the floating-point loads and stores
+         * extension that do not include the floating-point loads and stores.
          */
         if ((has_ext(ctx, RVC) || ctx->cfg_ptr->ext_zca) &&
-            decode_insn16(ctx, opcode)) {
+            decode_insn16(ctx, (uint16_t)ctx->opcode)) {
             return;
         }
-    } else {
-        if (!pc_is_4byte_align) {
-            /* Load last 2 bytes of instruction here */
-            opcode = deposit32(opcode, 16, 16,
-                               translator_lduw_end(env, &ctx->base,
-                                                   ctx->base.pc_next + 2,
-                                                   MO_LE));
-        }
-        ctx->opcode = opcode;
-
+        break;
+    case 4:
         for (guint i = 0; i < ctx->decoders->len; ++i) {

target/riscv/tcg/translate.c static void decode_opc(CPURISCVState *env, DisasContext *ctx) · line 1352

Quoted 10 lines
target/riscv/tcg/translate.c +5 -1
             riscv_cpu_decode_fn func = g_ptr_array_index(ctx->decoders, i);
-            if (func(ctx, opcode)) {
+            if (func(ctx, (uint32_t)ctx->opcode)) {
                 return;
             }
         }
+        break;
+    case 6:
+    case 8:
+        break;

decodetree already supports --insnwidth=64 (target/ppc uses it for prefixed instructions); could this add a decode_insn64 (or a 48/64 slot in decoder_table) now, even if empty?

Quoted 9 lines
target/riscv/tcg/translate.c +3 -0
+    default:
+        g_assert_not_reached();
     }
 
+illegal:
     gen_exception_illegal(ctx);
 }
 

target/riscv/tcg/translate.c static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 1467

Quoted 7 lines
target/riscv/tcg/translate.c +3 -1
@@ -1419,7 +1462,11 @@ static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
         } else {
             unsigned page_ofs = ctx->base.pc_next & ~TARGET_PAGE_MASK;
 
-            if (page_ofs > TARGET_PAGE_SIZE - MAX_INSN_LEN) {
+            if (ctx->cfg_ptr->ext_zl) {
+                if (page_ofs > TARGET_PAGE_SIZE - MAX_INSN_LEN) {
+                    ctx->base.is_jmp = DISAS_TOO_MANY;

This ends the TB for every instruction in the last 7 bytes of a page. With -d in_asm, a 32-bit addi at PAGE-4 that fits on the page becomes its own TB under x-zl.

Quoted 10 lines
target/riscv/tcg/translate.c +2 -1
+                }
+            } else if (page_ofs > TARGET_PAGE_SIZE - 4) {
                 uint16_t next_insn =
                     translator_lduw_end(env, &ctx->base, ctx->base.pc_next,
                                         MO_LE);
 
--

2.43.0

#13 lore raw

RFC PATCH 7/8 target/riscv: Enable experimental Zl fetch support

In reply to TANG Tiancheng

Expose the default-off x-zl property for TCG.

Use the built-in disassembler when enabled because Capstone does not implement this draft's instruction lengths.

---
 target/riscv/cpu.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/riscv/cpu.c +6 -0
index 362e5618091483e0ef10f091cf79106fb780c57b..ce6d55cd1143b59232ca2c677b28ffc2e8d6e78b 100644
@@ -196,6 +196,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(zfhmin, PRIV_VERSION_1_11_0, ext_zfhmin),
     ISA_EXT_DATA_ENTRY(zfinx, PRIV_VERSION_1_12_0, ext_zfinx),
     ISA_EXT_DATA_ENTRY(zdinx, PRIV_VERSION_1_12_0, ext_zdinx),
+    ISA_EXPERIMENTAL_EXT_DATA_ENTRY(zl, PRIV_VERSION_1_10_0, ext_zl),
     ISA_EXT_DATA_ENTRY(zca, PRIV_VERSION_1_12_0, ext_zca),
     ISA_EXT_DATA_ENTRY(zcb, PRIV_VERSION_1_12_0, ext_zcb),
     ISA_EXT_DATA_ENTRY(zcf, PRIV_VERSION_1_12_0, ext_zcf),
@@ -1155,6 +1156,11 @@ static void riscv_cpu_disas_set_info(const CPUState *s, disassemble_info *info)
         g_assert_not_reached();
     }
 
+    if (cpu->cfg.ext_zl) {
+        /* Capstone does not implement this draft's instruction lengths. */
+        return;
+    }
+
     info->cap_arch = CS_ARCH_RISCV;
     info->cap_insn_unit = 4;
     info->cap_insn_split = 4;
Signature · 1 line
2.43.0
#14 lore raw

RFC PATCH 8/8 tests/tcg/riscv: Check Zl fetching and halfword alignment

In reply to TANG Tiancheng

Run the same bare-metal program on RV32 and RV64 with C and Zca disabled. Check complete 48/64-bit encodings and undefined lengths.

Place a 48-bit encoding and an encoding with an undefined instruction length at the end of RAM to detect unnecessary reads of parcel D. Place required parcels C and D beyond the end of RAM in separate cases to check instruction access faults.
Check mcause, mepc and mtval, including the failing fetch address, RV32 truncation of instruction bits, and zero for undefined lengths.

Check IALIGN=16 for JAL, JALR, BEQ and MRET. Check that a compressed instruction remains illegal.

---
 tests/tcg/riscv/zl/zl.S              | 165 +++++++++++++++++++++++++++++++++++
 tests/tcg/riscv32/system/meson.build |   9 ++
 tests/tcg/riscv64/system/meson.build |   9 ++
 3 files changed, 183 insertions(+)
tests/tcg/riscv/zl/zl.S new file +165 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c289f64f1262ab240d11a96b365b97aa5922ca54
@@ -0,0 +1,165 @@
+/*
+ * Zl lengths, required fetches and IALIGN=16 with C/Zca disabled.
+ * Run on virt with 16 MiB RAM: 0x80000000 <= address < 0x81000000.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+    .option norvc
+    .option norelax
+    .text
+    .global _start
+
+    /* s0: faulting PC; s1: cause; s2: tval; s3: resume PC. */
+    .macro expect_exception cause, tval
+    li s1, \cause
+    li s2, \tval
+    lla s3, 1f
+    jr s0
+1:
+    .endm
+
+_start:
+    lla t0, trap_handler
+    csrw mtvec, t0
+    csrw mie, zero
+    li s4, 0
+    csrr t0, misa
+    andi t0, t0, 4
+    bnez t0, failed
+
+    /* JALR to a 48-bit candidate at RAM end; D must not be fetched. */
+    li s0, 0x80fffffa
+    lla t0, insn48
+    lhu t1, 0(t0)
+    sh t1, 0(s0)
+    lhu t1, 2(t0)
+    sh t1, 2(s0)
+    lhu t1, 4(t0)
+    sh t1, 4(s0)
+    fence.i
+#if __riscv_xlen == 64
+    expect_exception 2, 0x5a692468003f
+#else
+    expect_exception 2, 0x2468003f
+#endif
+
+    /* Undefined C still stops after A/B/C, with no instruction bits. */
+    li t0, 0x5a7f
+    sh t0, 4(s0)
+    fence.i
+    expect_exception 2, 0
+
+    lla s0, insn64
+#if __riscv_xlen == 64
+    expect_exception 2, 0x9abc5a632468001f
+#else
+    expect_exception 2, 0x2468001f
+#endif
+
+    /* A/B present, C missing: fault at the first inaccessible parcel. */
+    li s0, 0x80fffffc
+    li t0, 0x2468003f
+    sw t0, 0(s0)
+    fence.i
+    expect_exception 1, 0x81000000
+
+    /* A/B/C present, D missing; C requests eight bytes. */
+    li s0, 0x80fffffa
+    lla t0, insn64
+    lhu t1, 0(t0)
+    sh t1, 0(s0)
+    lhu t1, 2(t0)
+    sh t1, 2(s0)
+    lhu t1, 4(t0)
+    sh t1, 4(s0)
+    fence.i
+    expect_exception 1, 0x81000000
+
+    /* JAL and a word-aligned BEQ must reach halfword targets. */
+    jal zero, half_jal
+    j failed
+    .balign 4, 0
+    .2byte 0
+half_jal:
+    j branch_entry
+    .balign 4, 0
+branch_entry:
+    beq zero, zero, half_branch
+    j failed
+    .balign 4, 0
+    .2byte 0
+half_branch:
+    lla t0, half_return
+    andi t1, t0, 3
+    li t2, 2
+    bne t1, t2, failed
+    csrw mepc, t0
+    csrr t1, mepc
+    bne t0, t1, failed
+    li t1, 0x1800
+    csrs mstatus, t1
+    mret
+    .balign 4, 0
+    .2byte 0
+half_return:
+    lla s0, compressed
+    expect_exception 2, 1
+    li t0, 6
+    bne s4, t0, failed
+    j passed
+
+    .balign 4, 0
+trap_handler:
+    csrr t0, mcause
+    bne t0, s1, failed
+    csrr t0, mepc
+    bne t0, s0, failed
+    csrr t0, mtval
+    bne t0, s2, failed
+    addi s4, s4, 1
+    csrw mepc, s3
+    mret
+
+    /* A alone would select the opposite length under the old encoding. */
+insn48:
+    .2byte 0x003f, 0x2468, 0x5a69
+insn64:
+    .2byte 0x001f, 0x2468, 0x5a63, 0x9abc
+compressed:
+    .2byte 0x0001, 0
+
+    .balign 4, 0
+passed:
+    li a0, 4
+    lla a1, pass
+    .balign 16
+    slli zero, zero, 31
+    ebreak
+    srai zero, zero, 7
+    li a0, 0
+    j exit
+failed:
+    li a0, 1
+exit:
+    lla a1, semiargs
+#if __riscv_xlen == 64
+    sd a0, 8(a1)
+#else
+    sw a0, 4(a1)
+#endif
+    li a0, 0x20
+    .balign 16
+    slli zero, zero, 31
+    ebreak
+    srai zero, zero, 7
+    j .
+
+    .data
+    .balign 8
+semiargs:
+#if __riscv_xlen == 64
+    .dword 0x20026, 0
+#else
+    .word 0x20026, 0
+#endif
+pass:
+    .asciz "Zl PASS\n"
tests/tcg/riscv32/system/meson.build +9 -0
index 117ae6fbce4a0e26fea242a2a86fc7f6ecb448a4..b36047a6240f5afa3dc22817fed12b979427f50f 100644
@@ -22,6 +22,15 @@ tests += {
   },
 }
 
+tests += {
+  '../riscv/zl/zl.S': {
+    'cflags': cflags + ['-march=rv32i_zicsr_zifencei'],
+    'qemu_args': ['-M', 'virt', '-m', '16M', '-bios', 'none', '-display', 'none',
+                  '-serial', 'none', '-monitor', 'none', '-semihosting',
+                  '-cpu', 'rv32,x-zl=true,c=false,zca=false', '-kernel'],
+  },
+}
+
 if 'qemu-system-riscv32' in emulators
   tcg_tests += {
     'riscv32-softmmu': {
tests/tcg/riscv64/system/meson.build +9 -0
index 3c8b018d9e98e0b5498952d2b50b48894c99c377..0b5a6db4a4926d342c6e613490731fb7a663fb5c 100644
@@ -89,6 +89,15 @@ tests += {
   }
 }
 
+tests += {
+  '../riscv/zl/zl.S': {
+    'cflags': cflags + ['-march=rv64i_zicsr_zifencei', '-mabi=lp64'],
+    'qemu_args': ['-M', 'virt', '-m', '16M', '-bios', 'none', '-display', 'none',
+                  '-serial', 'none', '-monitor', 'none', '-semihosting',
+                  '-cpu', 'rv64,x-zl=true,c=false,zca=false', '-kernel'],
+  },
+}
+
 if 'qemu-system-riscv64' in emulators
   tcg_tests += {
     'riscv64-softmmu': {
Signature · 1 line
2.43.0
#15 lore raw
In reply to TANG Tiancheng
Quoted 20 lines · TANG Tiancheng

QEMU's RISC-V TCG implementation currently supports only 16-bit compressed instructions and 32-bit instructions. This series adds experimental support for fetching 48-bit and 64-bit instructions and for halfword alignment, following Zl draft 65c377a [1].

This work is motivated in part by an AME proposal that encodes operand datatypes in instructions and is still under evaluation [3]. Larger immediates and longer-range direct calls are other uses under discussion [1,2]. This series provides instruction-fetch, alignment and trap-reporting support for long instructions, without implementing individual instruction semantics.

The x-zl CPU property is disabled by default and supported only under TCG. When enabled, the translator and built-in disassembler share the length rules, and halfword alignment does not require C/Zca. Compressed decoding remains disabled if C/Zca are disabled. Complete 48/64-bit encodings still raise illegal-instruction exceptions, and the disassembler prints "unknown".

Quoted 3 lines

For an undefined length, fetching stops after the third 16-bit parcel and no instruction bits are reported. This avoids additional fetches solely for trap reporting. Feedback on this choice would be useful.

QEMU advertises Sstvala/Shvstvala on every CPU at priv >= 1.12 (they show up in riscv,isa), and Sstvala requires stval to hold the faulting instruction for illegal-instruction exceptions. For an encoding longer than ILEN that is the first ILEN (64) bits, so parcel D must be fetched. Details on 6/8.

Quoted 2 lines

Patch 1 fixes incomplete instruction reads in the disassembler and can be taken independently of Zl support.

Given that 1/8 is a standalone fix, it could go in now. Just submit it as a separate fix.

Quoted 9 lines

The code was built for RV32/RV64 user and system emulation. RV32/RV64 bare-metal tests check RAM-boundary fetches and trap values, halfword jumps and returns, and rejection of compressed instructions with C/Zca disabled. The Zl tests and existing MEPC regression passed with Capstone enabled and disabled.

[1] https://github.com/riscv/riscv-long-instruction/blob/65c377a665feb29a4848384ad566a9873c5fd423/src/unpriv/zl.adoc
[2] https://github.com/riscv/riscv-isa-manual/pull/3269#issuecomment-5328426143
[3] https://riscv.atlassian.net/wiki/spaces/AMEX/pages/2445836308/2026-11-09+AME+Meeting+Notes

This link is not publicly accessible; please drop it or point to a public document.

Quoted 37 lines

---
TANG Tiancheng (8):
disas/riscv: Reject incomplete instruction reads target/riscv: Share instruction length helper target/riscv: Prepare Zl instruction alignment disas/riscv: Decode Zl instruction lengths target/riscv: Limit instruction bits to the trap CSR width target/riscv: Fetch 48-bit and 64-bit instructions target/riscv: Enable experimental Zl fetch support tests/tcg/riscv: Check Zl fetching and halfword alignment

 disas/riscv.c                               |  49 ++++++---
 disas/riscv.h                               |   4 +-
 target/riscv/cpu.c                          |   6 +
 target/riscv/cpu.h                          |  10 +-
 target/riscv/cpu_cfg_fields.h.inc           |   1 +
 target/riscv/insn_length.h                  |  29 +++++
 target/riscv/internals.h                    |  17 +--
 target/riscv/tcg/cpu_helper.c               |   7 +-
 target/riscv/tcg/csr.c                      |   7 +-
 target/riscv/tcg/insn_trans/trans_rvi.c.inc |   8 +-
 target/riscv/tcg/op_helper.c                |  10 +-
 target/riscv/tcg/translate.c                | 101 ++++++++++++-----
 tests/tcg/riscv/zl/zl.S                     | 165 ++++++++++++++++++++++++++++
 tests/tcg/riscv32/system/meson.build        |   9 ++
 tests/tcg/riscv64/system/meson.build        |   9 ++
 15 files changed, 359 insertions(+), 73 deletions(-)

---
base-commit: f29ccc287a6cfdd51d3a4fd17fbe9af3310a0ac5
change-id: 20261117-zl-master-8ebcaae0953f

Best regards,
--
TANG Tiancheng <[email protected]>