From nobody Mon Feb 9 18:56:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1665718912933652.7453281424641; Thu, 13 Oct 2022 20:41:52 -0700 (PDT) Received: from localhost ([::1]:58332 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ojBa2-0000x8-QL for importer@patchew.org; Thu, 13 Oct 2022 23:41:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37760) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ojBYj-0000AN-AY for qemu-devel@nongnu.org; Thu, 13 Oct 2022 23:40:29 -0400 Received: from mail.loongson.cn ([114.242.206.163]:39018 helo=loongson.cn) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ojBYg-00050b-Ir for qemu-devel@nongnu.org; Thu, 13 Oct 2022 23:40:29 -0400 Received: from lingfengzhe-ms7c94.loongson.cn (unknown [10.90.50.23]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxReIk2khjntotAA--.33108S2; Fri, 14 Oct 2022 11:40:20 +0800 (CST) From: Qi Hu To: WANG Xuerui , Richard Henderson Cc: qemu-devel@nongnu.org Subject: [PATCH v3] tcg/loongarch64: Add direct jump support Date: Fri, 14 Oct 2022 11:40:20 +0800 Message-Id: <20221014034020.1167864-1-huqi@loongson.cn> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: AQAAf8DxReIk2khjntotAA--.33108S2 X-Coremail-Antispam: 1UD129KBjvJXoWxAFy3XFWfWFy7JrW7JFyxKrg_yoWrXrWUpr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.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=114.242.206.163; envelope-from=huqi@loongson.cn; helo=loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1665718914552100001 Similar to the ARM64, LoongArch has PC-relative instructions such as PCADDU18I. These instructions can be used to support direct jump for LoongArch. Additionally, if instruction "B offset" can cover the target address(target is within =C2=B1128MB range), a single "B offset" plus a nop will be used by "tb_target_set_jump_target". Signed-off-by: Qi Hu Reviewed-by: Richard Henderson Reviewed-by: WANG Xuerui --- tcg/loongarch64/tcg-target.c.inc | 56 +++++++++++++++++++++++++++++--- tcg/loongarch64/tcg-target.h | 5 ++- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/tcg/loongarch64/tcg-target.c.inc b/tcg/loongarch64/tcg-target.= c.inc index f5a214a17f..2a068a52cc 100644 --- a/tcg/loongarch64/tcg-target.c.inc +++ b/tcg/loongarch64/tcg-target.c.inc @@ -1031,6 +1031,35 @@ static void tcg_out_qemu_st(TCGContext *s, const TCG= Arg *args) #endif } =20 +/* LoongArch use `andi zero, zero, 0` as NOP. */ +#define NOP OPC_ANDI +static void tcg_out_nop(TCGContext *s) +{ + tcg_out32(s, NOP); +} + +void tb_target_set_jmp_target(uintptr_t tc_ptr, uintptr_t jmp_rx, + uintptr_t jmp_rw, uintptr_t addr) +{ + tcg_insn_unit i1, i2; + ptrdiff_t upper, lower; + ptrdiff_t offset =3D (addr - jmp_rx) >> 2; + + if (offset =3D=3D sextreg(offset, 0, 28)) { + i1 =3D encode_sd10k16_insn(OPC_B, offset); + i2 =3D NOP; + } else { + lower =3D (int16_t)offset; + upper =3D (offset - lower) >> 16; + + i1 =3D encode_dsj20_insn(OPC_PCADDU18I, TCG_REG_T0, upper); + i2 =3D encode_djsk16_insn(OPC_JIRL, TCG_REG_ZERO, TCG_REG_T0, lowe= r); + } + uint64_t pair =3D ((uint64_t)i2 << 32) | i1; + qatomic_set((uint64_t *)jmp_rw, pair); + flush_idcache_range(jmp_rx, jmp_rw, 8); +} + /* * Entry-points */ @@ -1058,11 +1087,28 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, break; =20 case INDEX_op_goto_tb: - assert(s->tb_jmp_insn_offset =3D=3D 0); - /* indirect jump method */ - tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO, - (uintptr_t)(s->tb_jmp_target_addr + a0)); - tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_TMP0, 0); + if (s->tb_jmp_insn_offset !=3D NULL) { + /* TCG_TARGET_HAS_direct_jump */ + /* + * Ensure that "patch area" are 8-byte aligned so that an + * atomic write can be used to patch the target address. + */ + if ((uintptr_t)s->code_ptr & 7) { + tcg_out_nop(s); + } + s->tb_jmp_insn_offset[a0] =3D tcg_current_code_size(s); + /* + * actual branch destination will be patched by + * tb_target_set_jmp_target later + */ + tcg_out_opc_pcaddu18i(s, TCG_REG_TMP0, 0); + tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_TMP0, 0); + } else { + /* !TCG_TARGET_HAS_direct_jump */ + tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO, + (uintptr_t)(s->tb_jmp_target_addr + a0)); + tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_TMP0, 0); + } set_jmp_reset_offset(s, a0); break; =20 diff --git a/tcg/loongarch64/tcg-target.h b/tcg/loongarch64/tcg-target.h index 67380b2432..ee207ec32c 100644 --- a/tcg/loongarch64/tcg-target.h +++ b/tcg/loongarch64/tcg-target.h @@ -42,7 +42,7 @@ =20 #define TCG_TARGET_INSN_UNIT_SIZE 4 #define TCG_TARGET_NB_REGS 32 -#define MAX_CODE_GEN_BUFFER_SIZE SIZE_MAX +#define MAX_CODE_GEN_BUFFER_SIZE (128 * GiB) =20 typedef enum { TCG_REG_ZERO, @@ -123,7 +123,7 @@ typedef enum { #define TCG_TARGET_HAS_clz_i32 1 #define TCG_TARGET_HAS_ctz_i32 1 #define TCG_TARGET_HAS_ctpop_i32 0 -#define TCG_TARGET_HAS_direct_jump 0 +#define TCG_TARGET_HAS_direct_jump 1 #define TCG_TARGET_HAS_brcond2 0 #define TCG_TARGET_HAS_setcond2 0 #define TCG_TARGET_HAS_qemu_st8_i32 0 @@ -166,7 +166,6 @@ typedef enum { #define TCG_TARGET_HAS_muluh_i64 1 #define TCG_TARGET_HAS_mulsh_i64 1 =20 -/* not defined -- call should be eliminated at compile time */ void tb_target_set_jmp_target(uintptr_t, uintptr_t, uintptr_t, uintptr_t); =20 #define TCG_TARGET_DEFAULT_MO (0) --=20 2.37.3