From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3BB8606A334 for ; Wed, 2 Dec 2026 07:42:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=m6AonC8M Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4jfV8m48TQz2y32; Wed, 02 Dec 2026 18:42:32 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1796197352; cv=none; b=Td5kKFZAkLYYHHK2SuBpzodZJsipcwqarOJOBPVx0M4icuNv1/uO4dozm/VGat5NxIt3V4GB9fTqepkX0yoxJlc6ehuytryc1hciN1UtzO6onK8ArSgVGkmAn0Q6cdp/sb8s/MQasZ8qB/eFIOIQxKZtHp9BHdE7R9bhE4SmCoUSC8DVTFg3EsUXl2JEqsM1HFerLXluluPScueeBIoTXkOZOFI29Nr6mg2Fbzpz4VAsSI4iCPQ5TMBvQO+d51f0wAv6VaZAR8B7+VXw1t9jW6enKigUHWBMgQlwMDMX+PYVdHxNTTIVBdpvFucraRt/bJdFlrk9RnE2OqkYU8ocYw== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1796197352; c=relaxed/relaxed; bh=H1NCRhmorOD+3Y+lDe0P1kd4fxQq8KJDUGmCVmbMY7M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FZXSQ1gUrZz959cK/yISdjLr/QRXGcp6bmpQVdkyf5dRtja7AXcCOBPldDcVdqmBJAD3sPxpeltsY2yUkxqfG6aaW2lO+OvvHw2bFejSSfXUGkwT3h7n0Zzcv0xFHf6Ob4R6kKcI2/lXKehBoEeYgfdcFxzCiChXfBnx0hx42yWUBG5cPnjwHxjnODt1o5/TCsdbQd6bPlBPPc2obt2IxQlIuouQzZvogKJuygEWW1l6cVdZA6RxlfEtxc4jewEFwV25bPmVcSwVrEyF1qvK8LjAUyx+1fZGjnuqKjMDV6zkFJY5UTJg+kJnktT3VUNVNsmcYf+eKVLH2+4pu23lUA== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=m6AonC8M; dkim-atps=neutral; spf=pass (client-ip=148.163.158.5; helo=mx0b-001b2d01.pphosted.com; envelope-from=shivangu@linux.ibm.com; receiver=lists.ozlabs.org) smtp.mailfrom=linux.ibm.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=m6AonC8M; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.ibm.com (client-ip=148.163.158.5; helo=mx0b-001b2d01.pphosted.com; envelope-from=shivangu@linux.ibm.com; receiver=lists.ozlabs.org) Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4jfV8l49wSz2y1W for ; Wed, 02 Dec 2026 18:42:31 +1100 (AEDT) Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B274d9b3987611; Wed, 2 Dec 2026 07:42:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=H1NCRhmorOD+3Y+lD e0P1kd4fxQq8KJDUGmCVmbMY7M=; b=m6AonC8MV1/0DL4hURvrMSbQtXRQn0pTX QL/h9/0pk9GRURQbV8MlV87iJMqQKAp9NSh3y5WvueHt/YZzhRS/r4iBfkxcHEpq 5W24PLzCOkP4jLk/hbyXgkmdDv+m0lDxo7cbLuVoPpHjAOuHMeiJkuVDs6nyD2vA 2au8a/JXmE5GLxIJn1lOBR48bbOTE9Vz7Js6gEvJlhSQ69/dhtTqBK+/u90c2XKT CDwGaEQH03+4YVK8M9MVF8OOhkgzReYAh5XHgH2/eOK8jlV7lVPhRKLeuNN4DRe/ TtsjP6kFCi+gtVa10frrPE4I/YZEanCZkNnEV83pakTJyqoQoXzMw== Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4j7mckgtmd-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 02 Dec 2026 07:42:16 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B26nL8d1627002; Wed, 2 Dec 2026 07:42:16 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 4ja0pq320t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 02 Dec 2026 07:42:16 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B27gA9Z42926442 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 2 Dec 2026 07:42:11 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CE2BE2004F; Wed, 2 Dec 2026 07:42:10 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D88E02004B; Wed, 2 Dec 2026 07:42:03 +0000 (GMT) Received: from shivang.com (unknown [9.67.68.246]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 2 Dec 2026 07:42:03 +0000 (GMT) From: Shivang Upadhyay 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 Message-ID: <20261202074059.899491-8-shivangu@linux.ibm.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202074059.899491-1-shivangu@linux.ibm.com> References: <20261202074059.899491-1-shivangu@linux.ibm.com> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , Precedence: list X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Reinject: loops=2 maxloops=12 X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAyMDAzMCBTYWx0ZWRfX3/GXrJcC7nO6 BEQPBl55FkihoyU2NwCnwIJNDjr/rxCZwgTjmeU/xh8MhlOXu0oVPFnJwzvC1DksBr47rMwwjkb PIX5TEteX+mbBDkGST5sC/FC4WPSWRc= X-Authority-Analysis: v=2.4 cv=MtfHeGae c=1 sm=1 tr=0 ts=6b0fcbd9 cx=c_pps a=AfN7/Ok6k8XGzOShvHwTGQ==:117 a=AfN7/Ok6k8XGzOShvHwTGQ==:17 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=Y2IxJ9c9Rs8Kov3niI8_:22 a=VnNF1IyMAAAA:8 a=neoqO9jvogbrDXLz9qAA:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAyMDAzMCBTYWx0ZWRfX+e84YxBCD7ZA 9r4am51KGdulCd44ZI8ErnkFIbtplIHcxP/n4RSBP+KGfqKSCB96IdrR4L60OPDuhNPLU3DdVDK MgAXo3Ebjn+bEXtxRbhE+HVTdnGWm0UdEFCMfStBOyrKdSS24JfUgOziUsAZEXS5CBpZrXlLZwZ lcFeIefSQrfKpYrrR9+XKR1i0Ida32d697Rs+mG1XWvb53JJv/FOns1hxRnjUWeXaNPZj7wlG9E mmdB5er0Gazj6vKSe+P1xk+1Rw3xxV8++nPL1YPZsmrswd4viHt35eo4KEpZqhPYsdHr4uUYz0v S9KZJiyn1dL3gMsKtcZf7ewgRHG4dfGkA6UI89x23xJOsBls9NV9dptzcisFzO1e/FIIVO9/wAx bUbwCyokZjajvEYJh29GWqg6aRCLa2P7nwGoeUAKQ8P5vJodK51lX4b9BXvCvTf6YMiMpGi74Xf vVqSVQo8IFZnnc1lF8Q== X-Proofpoint-GUID: J46AVgk_22fA_-iAjgrfD2verIQREh6W X-Proofpoint-ORIG-GUID: UN4Kc7YcSrJf_nKWHZJKAzVRd2yy-LJ8 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-02_02,2026-12-01_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 bulkscore=0 malwarescore=0 spamscore=0 phishscore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 suspectscore=0 impostorscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612020030 X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 and 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 --- 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 +#include + +/* + * 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 + +#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 +#include + +#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