Skip to contentSkip to conversation

target/riscv: Add Zilsd and Zclsd extension support

PATCH 刘旭 2 participants 2 messages Jun 11, 2025 – Jun 12, 2025

lore
Share & tools
lore

刘旭

author Reviewed-by Alistair Francis
#0 lore raw

Thanks for your reply.

However, regarding the issue of register restrictions, since I continued to use the format in the basic C extension in insn16.deocode,  I think the register range has thus been compatible with !function=ex_rvc_register.

Alistair Francis<[email protected]&gt;&nbsp;在 2025年6月11日 周三 12:47 写道:

On Thu, May 15, 2025 at 2:07 PM ~liuxu <[email protected]&gt; wrote:
&gt;
&gt; From: lxx <[email protected]&gt;
&gt;
&gt; This patch adds support for the Zilsd and Zclsd extension,
&gt; which is documented at https://github.com/riscv/riscv-zilsd/releases/tag/v1.0
&gt;
&gt; Co-developed-by: SUN Dongya <[email protected]&gt;
&gt; Co-developed-by: LIU Xu <[email protected]&gt;
&gt; Co-developed-by: ZHAO Fujin <[email protected]&gt;
&gt; ---
&gt;&nbsp; target/riscv/cpu.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; 4 + &gt;&nbsp; target/riscv/cpu_cfg.h&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; 2 + &gt;&nbsp; target/riscv/insn16.decode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; 8 ++ &gt;&nbsp; target/riscv/insn32.decode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp; 12 ++-
&gt;&nbsp; target/riscv/insn_trans/trans_zilsd.c.inc | 112 ++++++++++++++++++++++
&gt;&nbsp; target/riscv/tcg/tcg-cpu.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp; 20 ++++
&gt;&nbsp; target/riscv/translate.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; 1 + &gt;&nbsp; 7 files changed, 157 insertions(+), 2 deletions(-)
&gt;&nbsp; create mode 100644 target/riscv/insn_trans/trans_zilsd.c.inc
&gt;
&gt; diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
&gt; index 09ded6829a..54072e284d 100644
&gt; --- a/target/riscv/cpu.c
&gt; +++ b/target/riscv/cpu.c
&gt; @@ -115,6 +115,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zihintntl, PRIV_VERSION_1_10_0, ext_zihintntl),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zihintpause, PRIV_VERSION_1_10_0, ext_zihintpause),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zihpm, PRIV_VERSION_1_12_0, ext_zihpm),
&gt; +&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zilsd, PRIV_VERSION_1_12_0, ext_zilsd),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zimop, PRIV_VERSION_1_13_0, ext_zimop),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zmmul, PRIV_VERSION_1_12_0, ext_zmmul),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(za64rs, PRIV_VERSION_1_12_0, has_priv_1_12),
&gt; @@ -138,6 +139,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zcmop, PRIV_VERSION_1_13_0, ext_zcmop),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zcmp, PRIV_VERSION_1_12_0, ext_zcmp),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zcmt, PRIV_VERSION_1_12_0, ext_zcmt),
&gt; +&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zclsd, PRIV_VERSION_1_12_0, ext_zclsd),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zba, PRIV_VERSION_1_12_0, ext_zba),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zbb, PRIV_VERSION_1_12_0, ext_zbb),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ISA_EXT_DATA_ENTRY(zbc, PRIV_VERSION_1_12_0, ext_zbc),
&gt; @@ -1653,6 +1655,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
&gt;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zicntr", ext_zicntr, true),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zihpm", ext_zihpm, true),
&gt; +&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zilsd", ext_zilsd, false),
&gt;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zba", ext_zba, true),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zbb", ext_zbb, true),
&gt; @@ -1692,6 +1695,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zcmp", ext_zcmp, false),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zcmt", ext_zcmt, false),
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zicond", ext_zicond, false),
&gt; +&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zclsd", ext_zclsd, false),
&gt;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Vector cryptography extensions */
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MULTI_EXT_CFG_BOOL("zvbb", ext_zvbb, false),
&gt; diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h
&gt; index 8a843482cc..1ca11f99f1 100644
&gt; --- a/target/riscv/cpu_cfg.h
&gt; +++ b/target/riscv/cpu_cfg.h
&gt; @@ -51,6 +51,7 @@ struct RISCVCPUConfig {
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zcf;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zcmp;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zcmt;
&gt; +&nbsp;&nbsp;&nbsp; bool ext_zclsd;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zk;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zkn;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zknd;
&gt; @@ -73,6 +74,7 @@ struct RISCVCPUConfig {
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zihintntl;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zihintpause;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zihpm;
&gt; +&nbsp;&nbsp;&nbsp; bool ext_zilsd;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zimop;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_zcmop;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ext_ztso;
&gt; diff --git a/target/riscv/insn16.decode b/target/riscv/insn16.decode
&gt; index bf893d1c2e..c34020e4dc 100644
&gt; --- a/target/riscv/insn16.decode
&gt; +++ b/target/riscv/insn16.decode
&gt; @@ -130,10 +130,14 @@ sw&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 110&nbsp; ... ... .. ... 00 @cs_w &gt;&nbsp; {
&gt;&nbsp;&nbsp;&nbsp; ld&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 011&nbsp; ... ... .. ... 00 @cl_d &gt;&nbsp;&nbsp;&nbsp; c_flw&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 011&nbsp; ... ... .. ... 00 @cl_w
&gt; +&nbsp; # *** Zclsd Extension ***
&gt; +&nbsp; zclsd_ld&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 011&nbsp; ... ... .. ... 00 @cl_d
&gt;&nbsp; }
&gt;&nbsp; {
&gt;&nbsp;&nbsp;&nbsp; sd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 111&nbsp; ... ... .. ... 00 @cs_d &gt;&nbsp;&nbsp;&nbsp; c_fsw&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 111&nbsp; ... ... .. ... 00 @cs_w
&gt; +&nbsp; # *** Zclsd Extension ***
&gt; +&nbsp; zclsd_sd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 111&nbsp; ... ... .. ... 00 @cs_d
&gt;&nbsp; }
&gt;
&gt;&nbsp; # *** RV32/64C Standard Extension (Quadrant 1) ***
&gt; @@ -212,10 +216,14 @@ sw&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 110 .&nbsp; .....&nbsp; ..... 10 @c_swsp &gt;&nbsp;&nbsp;&nbsp; c64_illegal&nbsp;&nbsp;&nbsp;&nbsp; 011 -&nbsp; 00000&nbsp; ----- 10 # c.ldsp, RES rd=0
&gt;&nbsp;&nbsp;&nbsp; ld&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 011 .&nbsp; .....&nbsp; ..... 10 @c_ldsp &gt;&nbsp;&nbsp;&nbsp; c_flw&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 011 .&nbsp; .....&nbsp; ..... 10 @c_lwsp
&gt; +&nbsp; # *** Zclsd Extension ***
&gt; +&nbsp; zclsd_ldsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 011 .&nbsp; .....&nbsp; ..... 10 @c_ldsp
&gt;&nbsp; }
&gt;&nbsp; {
&gt;&nbsp;&nbsp;&nbsp; sd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 111 .&nbsp; .....&nbsp; ..... 10 @c_sdsp &gt;&nbsp;&nbsp;&nbsp; c_fsw&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 111 .&nbsp; .....&nbsp; ..... 10 @c_swsp
&gt; +&nbsp; # *** Zclsd Extension ***
&gt; +&nbsp; zclsd_sd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 111 .&nbsp; .....&nbsp; ..... 10 @c_sdsp
&gt;&nbsp; }
&gt;
&gt;&nbsp; # *** RV64 and RV32 Zcb Extension ***
&gt; diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode
&gt; index 6d1a13c826..57c242c3bb 100644
&gt; --- a/target/riscv/insn32.decode
&gt; +++ b/target/riscv/insn32.decode
&gt; @@ -182,8 +182,16 @@ csrrci&nbsp;&nbsp; ............&nbsp;&nbsp;&nbsp;&nbsp; ..... 111 ..... 1110011 @csr
&gt;
&gt;&nbsp; # *** RV64I Base Instruction Set (in addition to RV32I) ***
&gt;&nbsp; lwu&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ............&nbsp;&nbsp; ..... 110 ..... 0000011 @i
&gt; -ld&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ............&nbsp;&nbsp; ..... 011 ..... 0000011 @i
&gt; -sd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ....... .....&nbsp; ..... 011 ..... 0100011 @s
&gt; +{
&gt; +&nbsp; ld&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ............&nbsp;&nbsp; ..... 011 ..... 0000011 @i
&gt; +&nbsp; # *** Zilsd instructions ***
&gt; +&nbsp; zilsd_ld ............&nbsp;&nbsp; ..... 011 ..... 0000011 @i
&gt; +}
&gt; +{
&gt; +&nbsp; sd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ....... .....&nbsp; ..... 011 ..... 0100011 @s
&gt; +&nbsp; # *** Zilsd instructions ***
&gt; +&nbsp; zilsd_sd ....... .....&nbsp; ..... 011 ..... 0100011 @s
&gt; +}
&gt;&nbsp; addiw&nbsp;&nbsp;&nbsp; ............&nbsp;&nbsp; ..... 000 ..... 0011011 @i
&gt;&nbsp; slliw&nbsp;&nbsp;&nbsp; 0000000 .....&nbsp; ..... 001 ..... 0011011 @sh5
&gt;&nbsp; srliw&nbsp;&nbsp;&nbsp; 0000000 .....&nbsp; ..... 101 ..... 0011011 @sh5
&gt; diff --git a/target/riscv/insn_trans/trans_zilsd.c.inc b/target/riscv/insn_trans/trans_zilsd.c.inc
&gt; new file mode 100644
&gt; index 0000000000..1e3ab362de
&gt; --- /dev/null
&gt; +++ b/target/riscv/insn_trans/trans_zilsd.c.inc
&gt; @@ -0,0 +1,112 @@
&gt; +/*
&gt; + * RISC-V translation routines for the Zilsd &amp; Zclsd Extension.
&gt; + *
&gt; + * Copyright (c) 2024 Nucleisys, Inc.
&gt; + *
&gt; + * This program is free software; you can redistribute it and/or modify it
&gt; + * under the terms and conditions of the GNU General Public License,
&gt; + * version 2 or later, as published by the Free Software Foundation.
&gt; + *
&gt; + * This program is distributed in the hope it will be useful, but WITHOUT
&gt; + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
&gt; + * FITNESS FOR A PARTICULAR PURPOSE.&nbsp; See the GNU General Public License for
&gt; + * more details.
&gt; + *
&gt; + * You should have received a copy of the GNU General Public License along with
&gt; + * this program.&nbsp; If not, see <http://www.gnu.org/licenses/&gt;.
&gt; + */
&gt; +
&gt; +#define REQUIRE_ZILSD(ctx) do {&nbsp;&nbsp;&nbsp; \
&gt; +&nbsp;&nbsp;&nbsp; if (!ctx-&gt;cfg_ptr-&gt;ext_zilsd)&nbsp; \
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
&gt; +} while (0)
&gt; +
&gt; +#define REQUIRE_ZCLSD(ctx) do {&nbsp;&nbsp;&nbsp; \
&gt; +&nbsp;&nbsp;&nbsp; if (!ctx-&gt;cfg_ptr-&gt;ext_zclsd)&nbsp; \
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
&gt; +} while (0)
&gt; +
&gt; +static bool gen_load_i64(DisasContext *ctx, arg_ld *a)
&gt; +{
&gt; +&nbsp;&nbsp;&nbsp; if ((a-&gt;rd) % 2) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;
&gt; +&nbsp;&nbsp;&nbsp; }
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; TCGv dest_low = dest_gpr(ctx, a-&gt;rd);
&gt; +&nbsp;&nbsp;&nbsp; TCGv dest_high = dest_gpr(ctx, a-&gt;rd + 1);
&gt; +&nbsp;&nbsp;&nbsp; TCGv addr = get_address(ctx, a-&gt;rs1, a-&gt;imm);
&gt; +&nbsp;&nbsp;&nbsp; TCGv_i64 tmp = tcg_temp_new_i64();
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; tcg_gen_qemu_ld_i64(tmp, addr, ctx-&gt;mem_idx, MO_TESQ);
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; if (a-&gt;rd == 0) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;
&gt; +&nbsp;&nbsp;&nbsp; }
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; tcg_gen_extr_i64_tl(dest_low, dest_high, tmp);
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; gen_set_gpr(ctx, a-&gt;rd, dest_low);
&gt; +&nbsp;&nbsp;&nbsp; gen_set_gpr(ctx, a-&gt;rd + 1, dest_high);
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; return true;
&gt; +}
&gt; +
&gt; +static bool trans_zilsd_ld(DisasContext *ctx, arg_zilsd_ld *a)
&gt; +{
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_32BIT(ctx);
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_ZILSD(ctx);
&gt; +&nbsp;&nbsp;&nbsp; return gen_load_i64(ctx, a);
&gt; +}
&gt; +
&gt; +static bool trans_zclsd_ld(DisasContext *ctx, arg_zclsd_ld *a)
&gt; +{
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_32BIT(ctx);
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_ZCLSD(ctx);

Shouldn't there be a check that only x8-15 is used?

&gt; +&nbsp;&nbsp;&nbsp; return gen_load_i64(ctx, a);
&gt; +}
&gt; +
&gt; +static bool trans_zclsd_ldsp(DisasContext *ctx, arg_zclsd_ldsp *a)
&gt; +{
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_32BIT(ctx);
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_ZCLSD(ctx);
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; if (a-&gt;rd == 0) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;
&gt; +&nbsp;&nbsp;&nbsp; }
&gt; +&nbsp;&nbsp;&nbsp; return gen_load_i64(ctx, a);
&gt; +}
&gt; +
&gt; +static bool gen_store_i64(DisasContext *ctx, arg_sd *a)
&gt; +{
&gt; +&nbsp;&nbsp;&nbsp; if ((a-&gt;rs2) % 2) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;
&gt; +&nbsp;&nbsp;&nbsp; }
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; TCGv data_low = get_gpr(ctx, a-&gt;rs2, EXT_NONE);
&gt; +&nbsp;&nbsp;&nbsp; TCGv data_high = get_gpr(ctx, a-&gt;rs2 + 1, EXT_NONE); &gt; +&nbsp;&nbsp;&nbsp; TCGv addr = get_address(ctx, a-&gt;rs1, a-&gt;imm);
&gt; +&nbsp;&nbsp;&nbsp; TCGv_i64 tmp = tcg_temp_new_i64();
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; if (a-&gt;rs2 == 0) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tmp = tcg_constant_i64(0);
&gt; +&nbsp;&nbsp;&nbsp; } else {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tcg_gen_concat_tl_i64(tmp, data_low, data_high); &gt; +&nbsp;&nbsp;&nbsp; }
&gt; +&nbsp;&nbsp;&nbsp; tcg_gen_qemu_st_i64(tmp, addr, ctx-&gt;mem_idx, MO_TESQ); &gt; +
&gt; +&nbsp;&nbsp;&nbsp; return true;
&gt; +}
&gt; +
&gt; +static bool trans_zilsd_sd(DisasContext *ctx, arg_zilsd_sd *a)
&gt; +{
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_32BIT(ctx);
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_ZILSD(ctx);
&gt; +&nbsp;&nbsp;&nbsp; return gen_store_i64(ctx, a);
&gt; +}
&gt; +
&gt; +static bool trans_zclsd_sd(DisasContext *ctx, arg_zclsd_sd *a)
&gt; +{
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_32BIT(ctx);
&gt; +&nbsp;&nbsp;&nbsp; REQUIRE_ZCLSD(ctx);

and a x8-15 check here as well

Otherwise:

Alistair

&gt; +&nbsp;&nbsp;&nbsp; return gen_store_i64(ctx, a);
&gt; +}
&gt; diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c
&gt; index 5aef9eef36..ea25817ffc 100644
&gt; --- a/target/riscv/tcg/tcg-cpu.c
&gt; +++ b/target/riscv/tcg/tcg-cpu.c
&gt; @@ -673,6 +673,26 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cpu-&gt;pmu_avail_ctrs = 0;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&gt;
&gt; +&nbsp;&nbsp;&nbsp; if (cpu-&gt;cfg.ext_zilsd &amp;&amp; riscv_has_ext(env, RVC)) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zclsd), true);
&gt; +&nbsp;&nbsp;&nbsp; }
&gt; +
&gt; +&nbsp;&nbsp;&nbsp; if (cpu-&gt;cfg.ext_zclsd) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zca), true);
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zilsd), true);
&gt; +
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (riscv_has_ext(env, RVC) &amp;&amp; riscv_has_ext(env, RVF)) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; error_setg(errp,
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Zclsd cannot be supported together with C and F extension"); &gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (cpu-&gt;cfg.ext_zcf) {
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; error_setg(errp,
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Zclsd cannot be supported together with Zcf extension"); &gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;
&gt; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&gt; +&nbsp;&nbsp;&nbsp; }
&gt; +
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (cpu-&gt;cfg.ext_zicfilp &amp;&amp; !cpu-&gt;cfg.ext_zicsr) { &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; error_setg(errp, "zicfilp extension requires zicsr extension"); &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;
&gt; diff --git a/target/riscv/translate.c b/target/riscv/translate.c
&gt; index d6651f244f..2cf1378dfd 100644
&gt; --- a/target/riscv/translate.c
&gt; +++ b/target/riscv/translate.c
&gt; @@ -1201,6 +1201,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
&gt;&nbsp; /* Include the auto-generated decoder for 16 bit insn */
&gt;&nbsp; #include "decode-insn16.c.inc"
&gt;&nbsp; #include "insn_trans/trans_rvzce.c.inc"
&gt; +#include "insn_trans/trans_zilsd.c.inc"
&gt;&nbsp; #include "insn_trans/trans_rvzcmop.c.inc"
&gt;&nbsp; #include "insn_trans/trans_rvzicfiss.c.inc"
&gt;
&gt; --
&gt; 2.45.3

#1 lore raw
Quoted 4 lines · 刘旭

Thanks for your reply.

However, regarding the issue of register restrictions, since I continued to use the format in the basic C extension in insn16.deocode, I think the register range has thus been compatible with !function=ex_rvc_register.

You are right!

In future please reply inline and in plain text though.

This is good to go then, do you mind rebasing it on
https://github.com/alistair23/qemu/tree/riscv-to-apply.next and sending a v8

Alistair

Quoted 311 lines

Alistair Francis<[email protected]> 在 2025年6月11日 周三 12:47 写道:

Quoted 193 lines · ~liuxu

From: lxx <[email protected]>

This patch adds support for the Zilsd and Zclsd extension,
which is documented at https://github.com/riscv/riscv-zilsd/releases/tag/v1.0

---
 target/riscv/cpu.c                        |   4 +
 target/riscv/cpu_cfg.h                    |   2 +
 target/riscv/insn16.decode                |   8 ++
 target/riscv/insn32.decode                |  12 ++-
 target/riscv/insn_trans/trans_zilsd.c.inc | 112 ++++++++++++++++++++++
 target/riscv/tcg/tcg-cpu.c                |  20 ++++
 target/riscv/translate.c                  |   1 +
 7 files changed, 157 insertions(+), 2 deletions(-)
 create mode 100644 target/riscv/insn_trans/trans_zilsd.c.inc
target/riscv/cpu.c +4 -0
index 09ded6829a..54072e284d 100644
@@ -115,6 +115,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(zihintntl, PRIV_VERSION_1_10_0, ext_zihintntl),
     ISA_EXT_DATA_ENTRY(zihintpause, PRIV_VERSION_1_10_0, ext_zihintpause),
     ISA_EXT_DATA_ENTRY(zihpm, PRIV_VERSION_1_12_0, ext_zihpm),
+    ISA_EXT_DATA_ENTRY(zilsd, PRIV_VERSION_1_12_0, ext_zilsd),
     ISA_EXT_DATA_ENTRY(zimop, PRIV_VERSION_1_13_0, ext_zimop),
     ISA_EXT_DATA_ENTRY(zmmul, PRIV_VERSION_1_12_0, ext_zmmul),
     ISA_EXT_DATA_ENTRY(za64rs, PRIV_VERSION_1_12_0, has_priv_1_12),
@@ -138,6 +139,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(zcmop, PRIV_VERSION_1_13_0, ext_zcmop),
     ISA_EXT_DATA_ENTRY(zcmp, PRIV_VERSION_1_12_0, ext_zcmp),
     ISA_EXT_DATA_ENTRY(zcmt, PRIV_VERSION_1_12_0, ext_zcmt),
+    ISA_EXT_DATA_ENTRY(zclsd, PRIV_VERSION_1_12_0, ext_zclsd),
     ISA_EXT_DATA_ENTRY(zba, PRIV_VERSION_1_12_0, ext_zba),
     ISA_EXT_DATA_ENTRY(zbb, PRIV_VERSION_1_12_0, ext_zbb),
     ISA_EXT_DATA_ENTRY(zbc, PRIV_VERSION_1_12_0, ext_zbc),
@@ -1653,6 +1655,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
 
     MULTI_EXT_CFG_BOOL("zicntr", ext_zicntr, true),
     MULTI_EXT_CFG_BOOL("zihpm", ext_zihpm, true),
+    MULTI_EXT_CFG_BOOL("zilsd", ext_zilsd, false),
 
     MULTI_EXT_CFG_BOOL("zba", ext_zba, true),
     MULTI_EXT_CFG_BOOL("zbb", ext_zbb, true),
@@ -1692,6 +1695,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
     MULTI_EXT_CFG_BOOL("zcmp", ext_zcmp, false),
     MULTI_EXT_CFG_BOOL("zcmt", ext_zcmt, false),
     MULTI_EXT_CFG_BOOL("zicond", ext_zicond, false),
+    MULTI_EXT_CFG_BOOL("zclsd", ext_zclsd, false),
 
     /* Vector cryptography extensions */
     MULTI_EXT_CFG_BOOL("zvbb", ext_zvbb, false),
target/riscv/cpu_cfg.h +2 -0
index 8a843482cc..1ca11f99f1 100644
@@ -51,6 +51,7 @@ struct RISCVCPUConfig {
     bool ext_zcf;
     bool ext_zcmp;
     bool ext_zcmt;
+    bool ext_zclsd;
     bool ext_zk;
     bool ext_zkn;
     bool ext_zknd;
@@ -73,6 +74,7 @@ struct RISCVCPUConfig {
     bool ext_zihintntl;
     bool ext_zihintpause;
     bool ext_zihpm;
+    bool ext_zilsd;
     bool ext_zimop;
     bool ext_zcmop;
     bool ext_ztso;
target/riscv/insn16.decode +8 -0
index bf893d1c2e..c34020e4dc 100644
@@ -130,10 +130,14 @@ sw                110  ... ... .. ... 00 @cs_w
 {
   ld              011  ... ... .. ... 00 @cl_d
   c_flw           011  ... ... .. ... 00 @cl_w
+  # *** Zclsd Extension ***
+  zclsd_ld        011  ... ... .. ... 00 @cl_d
 }
 {
   sd              111  ... ... .. ... 00 @cs_d
   c_fsw           111  ... ... .. ... 00 @cs_w
+  # *** Zclsd Extension ***
+  zclsd_sd        111  ... ... .. ... 00 @cs_d
 }
 
 # *** RV32/64C Standard Extension (Quadrant 1) ***
@@ -212,10 +216,14 @@ sw                110 .  .....  ..... 10 @c_swsp
   c64_illegal     011 -  00000  ----- 10 # c.ldsp, RES rd=0
   ld              011 .  .....  ..... 10 @c_ldsp
   c_flw           011 .  .....  ..... 10 @c_lwsp
+  # *** Zclsd Extension ***
+  zclsd_ldsp      011 .  .....  ..... 10 @c_ldsp
 }
 {
   sd              111 .  .....  ..... 10 @c_sdsp
   c_fsw           111 .  .....  ..... 10 @c_swsp
+  # *** Zclsd Extension ***
+  zclsd_sd        111 .  .....  ..... 10 @c_sdsp
 }
 
 # *** RV64 and RV32 Zcb Extension ***
target/riscv/insn32.decode +10 -2
index 6d1a13c826..57c242c3bb 100644
@@ -182,8 +182,16 @@ csrrci   ............     ..... 111 ..... 1110011 @csr
 
 # *** RV64I Base Instruction Set (in addition to RV32I) ***
 lwu      ............   ..... 110 ..... 0000011 @i
-ld       ............   ..... 011 ..... 0000011 @i
-sd       ....... .....  ..... 011 ..... 0100011 @s
+{
+  ld       ............   ..... 011 ..... 0000011 @i
+  # *** Zilsd instructions ***
+  zilsd_ld ............   ..... 011 ..... 0000011 @i
+}
+{
+  sd       ....... .....  ..... 011 ..... 0100011 @s
+  # *** Zilsd instructions ***
+  zilsd_sd ....... .....  ..... 011 ..... 0100011 @s
+}
 addiw    ............   ..... 000 ..... 0011011 @i
 slliw    0000000 .....  ..... 001 ..... 0011011 @sh5
 srliw    0000000 .....  ..... 101 ..... 0011011 @sh5
target/riscv/insn_trans/trans_zilsd.c.inc new file +54 -0
new file mode 100644
index 0000000000..1e3ab362de
@@ -0,0 +1,112 @@
+/*
+ * RISC-V translation routines for the Zilsd & Zclsd Extension.
+ *
+ * Copyright (c) 2024 Nucleisys, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2 or later, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define REQUIRE_ZILSD(ctx) do {    \
+    if (!ctx->cfg_ptr->ext_zilsd)  \
+        return false;              \
+} while (0)
+
+#define REQUIRE_ZCLSD(ctx) do {    \
+    if (!ctx->cfg_ptr->ext_zclsd)  \
+        return false;              \
+} while (0)
+
+static bool gen_load_i64(DisasContext *ctx, arg_ld *a)
+{
+    if ((a->rd) % 2) {
+        return false;
+    }
+
+    TCGv dest_low = dest_gpr(ctx, a->rd);
+    TCGv dest_high = dest_gpr(ctx, a->rd + 1);
+    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_TESQ);
+
+    if (a->rd == 0) {
+        return true;
+    }
+
+    tcg_gen_extr_i64_tl(dest_low, dest_high, tmp);
+
+    gen_set_gpr(ctx, a->rd, dest_low);
+    gen_set_gpr(ctx, a->rd + 1, dest_high);
+
+    return true;
+}
+
+static bool trans_zilsd_ld(DisasContext *ctx, arg_zilsd_ld *a)

target/riscv/insn_trans/trans_zilsd.c.inc line 64

Quoted 10 lines
target/riscv/insn_trans/trans_zilsd.c.inc new file +10 -0
+{
+    REQUIRE_32BIT(ctx);
+    REQUIRE_ZILSD(ctx);
+    return gen_load_i64(ctx, a);
+}
+
+static bool trans_zclsd_ld(DisasContext *ctx, arg_zclsd_ld *a)
+{
+    REQUIRE_32BIT(ctx);
+    REQUIRE_ZCLSD(ctx);

Shouldn't there be a check that only x8-15 is used?

Quoted 36 lines
target/riscv/insn_trans/trans_zilsd.c.inc new file +36 -0
+    return gen_load_i64(ctx, a);
+}
+
+static bool trans_zclsd_ldsp(DisasContext *ctx, arg_zclsd_ldsp *a)
+{
+    REQUIRE_32BIT(ctx);
+    REQUIRE_ZCLSD(ctx);
+
+    if (a->rd == 0) {
+        return false;
+    }
+    return gen_load_i64(ctx, a);
+}
+
+static bool gen_store_i64(DisasContext *ctx, arg_sd *a)
+{
+    if ((a->rs2) % 2) {
+        return false;
+    }
+
+    TCGv data_low = get_gpr(ctx, a->rs2, EXT_NONE);
+    TCGv data_high = get_gpr(ctx, a->rs2 + 1, EXT_NONE);
+    TCGv addr = get_address(ctx, a->rs1, a->imm);
+    TCGv_i64 tmp = tcg_temp_new_i64();
+
+    if (a->rs2 == 0) {
+        tmp = tcg_constant_i64(0);
+    } else {
+        tcg_gen_concat_tl_i64(tmp, data_low, data_high);
+    }
+    tcg_gen_qemu_st_i64(tmp, addr, ctx->mem_idx, MO_TESQ);
+
+    return true;
+}
+
+static bool trans_zilsd_sd(DisasContext *ctx, arg_zilsd_sd *a)

target/riscv/insn_trans/trans_zilsd.c.inc line 110

Quoted 10 lines
target/riscv/insn_trans/trans_zilsd.c.inc new file +10 -0
+{
+    REQUIRE_32BIT(ctx);
+    REQUIRE_ZILSD(ctx);
+    return gen_store_i64(ctx, a);
+}
+
+static bool trans_zclsd_sd(DisasContext *ctx, arg_zclsd_sd *a)
+{
+    REQUIRE_32BIT(ctx);
+    REQUIRE_ZCLSD(ctx);

and a x8-15 check here as well

Otherwise:

Alistair

Quoted 47 lines
target/riscv/insn_trans/trans_zilsd.c.inc new file +2 -0
+    return gen_store_i64(ctx, a);
+}
target/riscv/tcg/tcg-cpu.c +20 -0
index 5aef9eef36..ea25817ffc 100644
@@ -673,6 +673,26 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)
         cpu->pmu_avail_ctrs = 0;
     }
 
+    if (cpu->cfg.ext_zilsd && riscv_has_ext(env, RVC)) {
+        cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zclsd), true);
+    }
+
+    if (cpu->cfg.ext_zclsd) {
+        cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zca), true);
+        cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zilsd), true);
+
+        if (riscv_has_ext(env, RVC) && riscv_has_ext(env, RVF)) {
+            error_setg(errp,
+                    "Zclsd cannot be supported together with C and F extension");
+            return;
+        }
+        if (cpu->cfg.ext_zcf) {
+            error_setg(errp,
+                    "Zclsd cannot be supported together with Zcf extension");
+            return;
+        }
+    }
+
     if (cpu->cfg.ext_zicfilp && !cpu->cfg.ext_zicsr) {
         error_setg(errp, "zicfilp extension requires zicsr extension");
         return;
target/riscv/translate.c +1 -1
index d6651f244f..2cf1378dfd 100644
@@ -1201,6 +1201,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
 /* Include the auto-generated decoder for 16 bit insn */
 #include "decode-insn16.c.inc"
 #include "insn_trans/trans_rvzce.c.inc"
+#include "insn_trans/trans_zilsd.c.inc"
 #include "insn_trans/trans_rvzcmop.c.inc"
 #include "insn_trans/trans_rvzicfiss.c.inc"
 
--

2.45.3