From: Bo Gan <ganboing@gmail.com>
To: linux-riscv@lists.infradead.org, samuel.holland@sifive.com,
david@redhat.com, palmer@dabbelt.com, pjw@kernel.org,
gaohan@iscas.ac.cn, me@ziyao.cc
Cc: lizhi2@eswincomputing.com, hal.feng@starfivetech.com,
marcel@ziswiler.com, conor@kernel.org, kernel@esmil.dk,
devicetree@vger.kernel.org
Subject: [RFC PATCH 2/6] riscv: alternatives: support auipc+load pair
Date: Fri, 13 Mar 2026 01:44:03 -0700 [thread overview]
Message-ID: <20260313084407.29669-3-ganboing@gmail.com> (raw)
In-Reply-To: <20260313084407.29669-1-ganboing@gmail.com>
Previously only auipc+jalr pair is supported. Add auipc+load pair
to support PC-relative memory load instruction as well.
Signed-off-by: Bo Gan <ganboing@gmail.com>
---
arch/riscv/include/asm/insn.h | 8 ++++++++
arch/riscv/kernel/alternative.c | 11 ++++++-----
2 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h
index c3005573e8c99..1c791a8732efc 100644
--- a/arch/riscv/include/asm/insn.h
+++ b/arch/riscv/include/asm/insn.h
@@ -135,6 +135,8 @@
#define RVC_C2_RS1_MASK GENMASK(4, 0)
/* parts of opcode for RVG*/
+#define RVG_OPCODE_LOAD 0x03
+#define RVG_OPCODE_STORE 0x23
#define RVG_OPCODE_FENCE 0x0f
#define RVG_OPCODE_AUIPC 0x17
#define RVG_OPCODE_BRANCH 0x63
@@ -198,6 +200,8 @@
#define RVG_MATCH_BGE (RV_ENCODE_FUNCT3(BGE) | RVG_OPCODE_BRANCH)
#define RVG_MATCH_BLTU (RV_ENCODE_FUNCT3(BLTU) | RVG_OPCODE_BRANCH)
#define RVG_MATCH_BGEU (RV_ENCODE_FUNCT3(BGEU) | RVG_OPCODE_BRANCH)
+#define RVG_MATCH_LOAD (RVG_OPCODE_LOAD)
+#define RVG_MATCH_STORE (RVG_OPCODE_STORE)
#define RVG_MATCH_EBREAK (RV_ENCODE_FUNCT12(EBREAK) | RVG_OPCODE_SYSTEM)
#define RVG_MATCH_SRET (RV_ENCODE_FUNCT12(SRET) | RVG_OPCODE_SYSTEM)
#define RVC_MATCH_C_BEQZ (RVC_ENCODE_FUNCT3(C_BEQZ) | RVC_OPCODE_C1)
@@ -222,6 +226,8 @@
#define RVG_MASK_BGE (RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
#define RVG_MASK_BLTU (RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
#define RVG_MASK_BGEU (RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
+#define RVG_MASK_LOAD (RV_INSN_OPCODE_MASK)
+#define RVG_MASK_STORE (RV_INSN_OPCODE_MASK)
#define RVC_MASK_C_BEQZ (RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
#define RVC_MASK_C_BNEZ (RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
#define RVC_MASK_C_EBREAK 0xffff
@@ -262,6 +268,8 @@ __RISCV_INSN_FUNCS(c_ebreak, RVC_MASK_C_EBREAK, RVC_MATCH_C_EBREAK)
__RISCV_INSN_FUNCS(ebreak, RVG_MASK_EBREAK, RVG_MATCH_EBREAK)
__RISCV_INSN_FUNCS(sret, RVG_MASK_SRET, RVG_MATCH_SRET)
__RISCV_INSN_FUNCS(fence, RVG_MASK_FENCE, RVG_MATCH_FENCE);
+__RISCV_INSN_FUNCS(load, RVG_MASK_LOAD, RVG_MATCH_LOAD);
+__RISCV_INSN_FUNCS(store, RVG_MASK_STORE, RVG_MATCH_STORE);
/* special case to catch _any_ system instruction */
static __always_inline bool riscv_insn_is_system(u32 code)
diff --git a/arch/riscv/kernel/alternative.c b/arch/riscv/kernel/alternative.c
index 7642704c7f184..04a9d3aed4647 100644
--- a/arch/riscv/kernel/alternative.c
+++ b/arch/riscv/kernel/alternative.c
@@ -74,7 +74,7 @@ static u32 riscv_instruction_at(void *p)
return (u32)parcel[0] | (u32)parcel[1] << 16;
}
-static void riscv_alternative_fix_auipc_jalr(void *ptr, u32 auipc_insn,
+static void riscv_alternative_fix_auipc_pair(void *ptr, u32 auipc_insn,
u32 jalr_insn, int patch_offset)
{
u32 call[2] = { auipc_insn, jalr_insn };
@@ -123,14 +123,15 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,
if (riscv_insn_is_auipc(insn) && i < num_insn - 1) {
u32 insn2 = riscv_instruction_at(alt_ptr + (i + 1) * sizeof(u32));
- if (!riscv_insn_is_jalr(insn2))
+ if (!riscv_insn_is_jalr(insn2) &&
+ !riscv_insn_is_load(insn2))
continue;
- /* if instruction pair is a call, it will use the ra register */
- if (RV_EXTRACT_RD_REG(insn) != 1)
+ if (RV_EXTRACT_RD_REG(insn) != RV_EXTRACT_RS1_REG(insn2))
continue;
- riscv_alternative_fix_auipc_jalr(alt_ptr + i * sizeof(u32),
+ /* insn2 use rd of insn as rs1, patch it */
+ riscv_alternative_fix_auipc_pair(alt_ptr + i * sizeof(u32),
insn, insn2, patch_offset);
i++;
}
--
2.34.1
next prev parent reply other threads:[~2026-03-13 8:46 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-13 8:44 [RFC PATCH 0/6] riscv: support EIC770X/JH7110 noncoherent devices with XPbmtUC Bo Gan
2026-03-13 8:44 ` [RFC PATCH 1/6] riscv: Add a custom, simplified version of Svpbmt "XPbmtUC" Bo Gan
2026-03-13 13:24 ` Conor Dooley
2026-03-13 21:33 ` Bo Gan
2026-03-13 23:55 ` Conor Dooley
2026-03-14 0:29 ` Bo Gan
2026-03-14 1:18 ` Conor Dooley
2026-03-14 5:06 ` Bo Gan
2026-03-14 12:17 ` Conor Dooley
2026-03-16 21:22 ` Bo Gan
2026-03-15 12:05 ` Conor Dooley
2026-03-13 8:44 ` Bo Gan [this message]
2026-03-13 8:44 ` [RFC PATCH 3/6] riscv: apply page table attribute bits for XPbmtUC Bo Gan
2026-03-13 13:24 ` Conor Dooley
2026-03-13 21:34 ` Bo Gan
2026-03-13 8:44 ` [RFC PATCH 4/6] riscv: select RISCV_ISA_XPBMTUC in STARFIVE and ESWIN SoC Bo Gan
2026-03-13 13:28 ` Conor Dooley
2026-03-13 21:35 ` Bo Gan
2026-03-13 8:44 ` [RFC PATCH 5/6] riscv: dts: starfive: jh7110: activate XPbmtUC Bo Gan
2026-03-13 13:48 ` Conor Dooley
2026-03-13 21:59 ` Bo Gan
2026-03-13 23:46 ` Conor Dooley
2026-03-13 8:44 ` [RFC PATCH 6/6] [TESTING-ONLY] riscv: dts: eswin: eic7700: " Bo Gan
2026-03-13 12:30 ` [RFC PATCH 0/6] riscv: support EIC770X/JH7110 noncoherent devices with XPbmtUC Conor Dooley
2026-03-13 22:17 ` Bo Gan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260313084407.29669-3-ganboing@gmail.com \
--to=ganboing@gmail.com \
--cc=conor@kernel.org \
--cc=david@redhat.com \
--cc=devicetree@vger.kernel.org \
--cc=gaohan@iscas.ac.cn \
--cc=hal.feng@starfivetech.com \
--cc=kernel@esmil.dk \
--cc=linux-riscv@lists.infradead.org \
--cc=lizhi2@eswincomputing.com \
--cc=marcel@ziswiler.com \
--cc=me@ziyao.cc \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=samuel.holland@sifive.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox