From: Shivang Upadhyay <shivangu@linux.ibm.com>
To: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org
Cc: maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com,
chleroy@kernel.org, ritesh.list@gmail.com, sshegde@linux.ibm.com,
jpoimboe@kernel.org, peterz@infradead.org,
shivangu@linux.ibm.com, ardb@kernel.org, will@kernel.org,
sv@linux.ibm.com, joe.lawrence@redhat.com, song@kernel.org,
mingo@kernel.org, mbenes@suse.cz
Subject: [PATCH v3 07/11] powerpc/lib: Extract branch patching helpers to code-patching-branch.c
Date: Wed, 2 Dec 2026 13:10:54 +0530 [thread overview]
Message-ID: <20261202074059.899491-8-shivangu@linux.ibm.com> (raw)
In-Reply-To: <20261202074059.899491-1-shivangu@linux.ibm.com>
Extract portable branch manipulation and decoding functions
(is_conditional_branch, create_cond_branch, instr_is_relative_branch,
instr_is_relative_link_branch, branch_target_at, translate_branch_at)
into arch/powerpc/lib/code-patching-branch.c.
Include code-patching-branch.c directly in arch/powerpc/lib/code-patching.c,
which avoids duplicating branch patching logic between the kernel runtime
and userspace host tools (like objtool).
Also, Add minimal versions of <asm/inst.h> and <asm/text-patching.h> under
tools/arch/powerpc/include/asm/ to provide instruction definitions
(ppc_inst_t) and inline branch creation helpers (create_branch, etc.)
for objtool and other user space tools.
Signed-off-by: Shivang Upadhyay <shivangu@linux.ibm.com>
---
arch/powerpc/include/asm/text-patching.h | 3 +
arch/powerpc/lib/code-patching-branch.c | 128 ++++++++++++++++++
arch/powerpc/lib/code-patching.c | 110 +--------------
tools/arch/powerpc/include/asm/inst.h | 41 ++++++
.../arch/powerpc/include/asm/text-patching.h | 67 +++++++++
5 files changed, 240 insertions(+), 109 deletions(-)
create mode 100644 arch/powerpc/lib/code-patching-branch.c
create mode 100644 tools/arch/powerpc/include/asm/inst.h
create mode 100644 tools/arch/powerpc/include/asm/text-patching.h
diff --git a/arch/powerpc/include/asm/text-patching.h b/arch/powerpc/include/asm/text-patching.h
index e7f14720f630..c3612636a381 100644
--- a/arch/powerpc/include/asm/text-patching.h
+++ b/arch/powerpc/include/asm/text-patching.h
@@ -157,7 +157,10 @@ static inline int instr_is_branch_bform(ppc_inst_t instr)
int instr_is_relative_branch(ppc_inst_t instr);
int instr_is_relative_link_branch(ppc_inst_t instr);
unsigned long branch_target(const u32 *instr);
+unsigned long branch_target_at(const u32 *instr, unsigned long pc);
int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src);
+int translate_branch_at(ppc_inst_t *instr, unsigned long dest,
+ const u32 *src, unsigned long src_addr);
bool is_conditional_branch(ppc_inst_t instr);
#define OP_RT_RA_MASK 0xffff0000UL
diff --git a/arch/powerpc/lib/code-patching-branch.c b/arch/powerpc/lib/code-patching-branch.c
new file mode 100644
index 000000000000..11be0b626748
--- /dev/null
+++ b/arch/powerpc/lib/code-patching-branch.c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2008 Michael Ellerman, IBM Corporation.
+ */
+
+#include <asm/text-patching.h>
+#include <asm/inst.h>
+
+/*
+ * Helper to check if a given instruction is a conditional branch
+ * Derived from the conditional checks in analyse_instr()
+ */
+bool is_conditional_branch(ppc_inst_t instr)
+{
+ unsigned int opcode = ppc_inst_primary_opcode(instr);
+
+ if (opcode == 16) /* bc, bca, bcl, bcla */
+ return true;
+ if (opcode == 19) {
+ switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
+ case 16: /* bclr, bclrl */
+ case 528: /* bcctr, bcctrl */
+ case 560: /* bctar, bctarl */
+ return true;
+ }
+ }
+ return false;
+}
+
+int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
+ unsigned long target, int flags)
+{
+ long offset;
+
+ offset = target;
+ if (! (flags & BRANCH_ABSOLUTE))
+ offset = offset - (unsigned long)addr;
+
+ /* Check we can represent the target in the instruction format */
+ if (!is_offset_in_cond_branch_range(offset))
+ return 1;
+
+ /* Mask out the flags and target, so they don't step on each other. */
+ *instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
+
+ return 0;
+}
+
+int instr_is_relative_branch(ppc_inst_t instr)
+{
+ if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
+ return 0;
+
+ return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
+}
+
+int instr_is_relative_link_branch(ppc_inst_t instr)
+{
+ return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
+}
+
+static unsigned long branch_iform_target(const u32 *instr, unsigned long pc)
+{
+ signed long imm;
+
+ imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC;
+
+ /* If the top bit of the immediate value is set this is negative */
+ if (imm & 0x2000000)
+ imm -= 0x4000000;
+
+ if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
+ imm += pc;
+
+ return (unsigned long)imm;
+}
+
+static unsigned long branch_bform_target(const u32 *instr, unsigned long pc)
+{
+ signed long imm;
+
+ imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC;
+
+ /* If the top bit of the immediate value is set this is negative */
+ if (imm & 0x8000)
+ imm -= 0x10000;
+
+ if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
+ imm += pc;
+
+ return (unsigned long)imm;
+}
+
+unsigned long branch_target_at(const u32 *instr, unsigned long pc)
+{
+ if (instr_is_branch_iform(ppc_inst_read(instr)))
+ return branch_iform_target(instr, pc);
+ else if (instr_is_branch_bform(ppc_inst_read(instr)))
+ return branch_bform_target(instr, pc);
+
+ return 0;
+}
+
+unsigned long branch_target(const u32 *instr)
+{
+ return branch_target_at(instr, (unsigned long)instr);
+}
+
+int translate_branch_at(ppc_inst_t *instr, unsigned long dest,
+ const u32 *src, unsigned long src_addr)
+{
+ unsigned long target;
+ target = branch_target_at(src, src_addr);
+
+ if (instr_is_branch_iform(ppc_inst_read(src)))
+ return create_branch(instr, (const u32 *)dest, target,
+ ppc_inst_val(ppc_inst_read(src)));
+ else if (instr_is_branch_bform(ppc_inst_read(src)))
+ return create_cond_branch(instr, (const u32 *)dest, target,
+ ppc_inst_val(ppc_inst_read(src)));
+
+ return 1;
+}
+
+int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src)
+{
+ return translate_branch_at(instr, (unsigned long)dest, src, (unsigned long)src);
+}
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index 44ff9f684bef..bfe4d93dcb6f 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -537,113 +537,5 @@ int patch_branch(u32 *addr, unsigned long target, int flags)
return patch_instruction(addr, instr);
}
-/*
- * Helper to check if a given instruction is a conditional branch
- * Derived from the conditional checks in analyse_instr()
- */
-bool is_conditional_branch(ppc_inst_t instr)
-{
- unsigned int opcode = ppc_inst_primary_opcode(instr);
-
- if (opcode == 16) /* bc, bca, bcl, bcla */
- return true;
- if (opcode == 19) {
- switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
- case 16: /* bclr, bclrl */
- case 528: /* bcctr, bcctrl */
- case 560: /* bctar, bctarl */
- return true;
- }
- }
- return false;
-}
+#include "code-patching-branch.c"
NOKPROBE_SYMBOL(is_conditional_branch);
-
-int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
- unsigned long target, int flags)
-{
- long offset;
-
- offset = target;
- if (! (flags & BRANCH_ABSOLUTE))
- offset = offset - (unsigned long)addr;
-
- /* Check we can represent the target in the instruction format */
- if (!is_offset_in_cond_branch_range(offset))
- return 1;
-
- /* Mask out the flags and target, so they don't step on each other. */
- *instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
-
- return 0;
-}
-
-int instr_is_relative_branch(ppc_inst_t instr)
-{
- if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
- return 0;
-
- return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
-}
-
-int instr_is_relative_link_branch(ppc_inst_t instr)
-{
- return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
-}
-
-static unsigned long branch_iform_target(const u32 *instr)
-{
- signed long imm;
-
- imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC;
-
- /* If the top bit of the immediate value is set this is negative */
- if (imm & 0x2000000)
- imm -= 0x4000000;
-
- if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
- imm += (unsigned long)instr;
-
- return (unsigned long)imm;
-}
-
-static unsigned long branch_bform_target(const u32 *instr)
-{
- signed long imm;
-
- imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC;
-
- /* If the top bit of the immediate value is set this is negative */
- if (imm & 0x8000)
- imm -= 0x10000;
-
- if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
- imm += (unsigned long)instr;
-
- return (unsigned long)imm;
-}
-
-unsigned long branch_target(const u32 *instr)
-{
- if (instr_is_branch_iform(ppc_inst_read(instr)))
- return branch_iform_target(instr);
- else if (instr_is_branch_bform(ppc_inst_read(instr)))
- return branch_bform_target(instr);
-
- return 0;
-}
-
-int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src)
-{
- unsigned long target;
- target = branch_target(src);
-
- if (instr_is_branch_iform(ppc_inst_read(src)))
- return create_branch(instr, dest, target,
- ppc_inst_val(ppc_inst_read(src)));
- else if (instr_is_branch_bform(ppc_inst_read(src)))
- return create_cond_branch(instr, dest, target,
- ppc_inst_val(ppc_inst_read(src)));
-
- return 1;
-}
diff --git a/tools/arch/powerpc/include/asm/inst.h b/tools/arch/powerpc/include/asm/inst.h
new file mode 100644
index 000000000000..7a2edca7a486
--- /dev/null
+++ b/tools/arch/powerpc/include/asm/inst.h
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _TOOLS_POWERPC_ASM_INST_H
+#define _TOOLS_POWERPC_ASM_INST_H
+
+#include <linux/types.h>
+
+#define OP_PREFIX 1
+
+typedef struct {
+ u32 val;
+ u32 suffix;
+} ppc_inst_t;
+
+#define ppc_inst(x) ((ppc_inst_t){ .val = (x) })
+
+static inline u32 ppc_inst_val(ppc_inst_t x)
+{
+ return x.val;
+}
+
+static inline int ppc_inst_primary_opcode(ppc_inst_t x)
+{
+ return ppc_inst_val(x) >> 26;
+}
+
+static inline ppc_inst_t ppc_inst_read(const u32 *ptr)
+{
+ return ppc_inst(*ptr);
+}
+
+static inline bool ppc_inst_prefixed(ppc_inst_t x)
+{
+ return ppc_inst_primary_opcode(x) == OP_PREFIX;
+}
+
+static inline int ppc_inst_len(ppc_inst_t x)
+{
+ return ppc_inst_prefixed(x) ? 8 : 4;
+}
+
+#endif /* _TOOLS_POWERPC_ASM_INST_H */
diff --git a/tools/arch/powerpc/include/asm/text-patching.h b/tools/arch/powerpc/include/asm/text-patching.h
new file mode 100644
index 000000000000..f953ab290ea7
--- /dev/null
+++ b/tools/arch/powerpc/include/asm/text-patching.h
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _TOOLS_POWERPC_ASM_TEXT_PATCHING_H
+#define _TOOLS_POWERPC_ASM_TEXT_PATCHING_H
+
+#include <linux/types.h>
+#include <asm/inst.h>
+
+#define BRANCH_SET_LINK 0x1
+#define BRANCH_ABSOLUTE 0x2
+
+static inline bool is_offset_in_branch_range(long offset)
+{
+ return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
+}
+
+static inline bool is_offset_in_cond_branch_range(long offset)
+{
+ return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
+}
+
+static inline int create_branch(ppc_inst_t *instr, const u32 *addr,
+ unsigned long target, int flags)
+{
+ long offset;
+
+ *instr = ppc_inst(0);
+ offset = target;
+ if (! (flags & BRANCH_ABSOLUTE))
+ offset = offset - (unsigned long)addr;
+
+ /* Check we can represent the target in the instruction format */
+ if (!is_offset_in_branch_range(offset))
+ return 1;
+
+ /* Mask out the flags and target, so they don't step on each other. */
+ *instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
+
+ return 0;
+}
+
+static inline unsigned int branch_opcode(ppc_inst_t instr)
+{
+ return ppc_inst_primary_opcode(instr) & 0x3F;
+}
+
+static inline int instr_is_branch_iform(ppc_inst_t instr)
+{
+ return branch_opcode(instr) == 18;
+}
+
+static inline int instr_is_branch_bform(ppc_inst_t instr)
+{
+ return branch_opcode(instr) == 16;
+}
+
+int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
+ unsigned long target, int flags);
+int instr_is_relative_branch(ppc_inst_t instr);
+int instr_is_relative_link_branch(ppc_inst_t instr);
+unsigned long branch_target(const u32 *instr);
+unsigned long branch_target_at(const u32 *instr, unsigned long pc);
+int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src);
+int translate_branch_at(ppc_inst_t *instr, unsigned long dest,
+ const u32 *src, unsigned long src_addr);
+bool is_conditional_branch(ppc_inst_t instr);
+
+#endif /* _TOOLS_POWERPC_ASM_TEXT_PATCHING_H */
--
2.56.1
next prev parent reply other threads:[~2026-12-02 7:42 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 7:40 [PATCH v3 00/11] objtool/powerpc: Fix up feature section alternatives at build time Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 01/11] objtool: Make symbol offsets section relative in linked objects Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 02/11] objtool: Make relocation " Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 03/11] objtool: Keep the layout of linked objects when writing them back Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 04/11] objtool: Pass the objtool_file to arch_support_alt_relocation() Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 05/11] objtool: Add --alt-fixup Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 06/11] objtool: Describe special_entry parsing for --alt-fixup Shivang Upadhyay
2026-12-02 7:40 ` Shivang Upadhyay [this message]
2026-12-02 7:40 ` [PATCH v3 08/11] objtool/powerpc: Implement arch_support_alt_relocation() Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 09/11] powerpc: Mark feature section alternatives as executable Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 10/11] powerpc: Always build objtool for powerpc Shivang Upadhyay
2026-12-02 7:40 ` [PATCH v3 11/11] powerpc: Remove runtime checking of jumps in core kernel feature-fixup Shivang Upadhyay
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=20261202074059.899491-8-shivangu@linux.ibm.com \
--to=shivangu@linux.ibm.com \
--cc=ardb@kernel.org \
--cc=chleroy@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=mbenes@suse.cz \
--cc=mingo@kernel.org \
--cc=mpe@ellerman.id.au \
--cc=npiggin@gmail.com \
--cc=peterz@infradead.org \
--cc=ritesh.list@gmail.com \
--cc=song@kernel.org \
--cc=sshegde@linux.ibm.com \
--cc=sv@linux.ibm.com \
--cc=will@kernel.org \
/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
all inboxes | Powered by JetHome®