[PATCH v3 07/11] powerpc/lib: Extract branch patching helpers to code-patching-branch.c

From: Shivang Upadhyay

Date: Wed Dec 02 2026 - 02:42:41 EST


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@xxxxxxxxxxxxx>
---
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