18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_CODE_PATCHING_H 38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_CODE_PATCHING_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci/* 68c2ecf20Sopenharmony_ci * Copyright 2008, Michael Ellerman, IBM Corporation. 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <asm/types.h> 108c2ecf20Sopenharmony_ci#include <asm/ppc-opcode.h> 118c2ecf20Sopenharmony_ci#include <linux/string.h> 128c2ecf20Sopenharmony_ci#include <linux/kallsyms.h> 138c2ecf20Sopenharmony_ci#include <asm/asm-compat.h> 148c2ecf20Sopenharmony_ci#include <asm/inst.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci/* Flags for create_branch: 178c2ecf20Sopenharmony_ci * "b" == create_branch(addr, target, 0); 188c2ecf20Sopenharmony_ci * "ba" == create_branch(addr, target, BRANCH_ABSOLUTE); 198c2ecf20Sopenharmony_ci * "bl" == create_branch(addr, target, BRANCH_SET_LINK); 208c2ecf20Sopenharmony_ci * "bla" == create_branch(addr, target, BRANCH_ABSOLUTE | BRANCH_SET_LINK); 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_ci#define BRANCH_SET_LINK 0x1 238c2ecf20Sopenharmony_ci#define BRANCH_ABSOLUTE 0x2 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cibool is_offset_in_branch_range(long offset); 268c2ecf20Sopenharmony_cibool is_offset_in_cond_branch_range(long offset); 278c2ecf20Sopenharmony_ciint create_branch(struct ppc_inst *instr, const struct ppc_inst *addr, 288c2ecf20Sopenharmony_ci unsigned long target, int flags); 298c2ecf20Sopenharmony_ciint create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr, 308c2ecf20Sopenharmony_ci unsigned long target, int flags); 318c2ecf20Sopenharmony_ciint patch_branch(struct ppc_inst *addr, unsigned long target, int flags); 328c2ecf20Sopenharmony_ciint patch_instruction(struct ppc_inst *addr, struct ppc_inst instr); 338c2ecf20Sopenharmony_ciint raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic inline unsigned long patch_site_addr(s32 *site) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci return (unsigned long)site + *site; 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic inline int patch_instruction_site(s32 *site, struct ppc_inst instr) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci return patch_instruction((struct ppc_inst *)patch_site_addr(site), instr); 438c2ecf20Sopenharmony_ci} 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistatic inline int patch_branch_site(s32 *site, unsigned long target, int flags) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci return patch_branch((struct ppc_inst *)patch_site_addr(site), target, flags); 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic inline int modify_instruction(unsigned int *addr, unsigned int clr, 518c2ecf20Sopenharmony_ci unsigned int set) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci return patch_instruction((struct ppc_inst *)addr, ppc_inst((*addr & ~clr) | set)); 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic inline int modify_instruction_site(s32 *site, unsigned int clr, unsigned int set) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci return modify_instruction((unsigned int *)patch_site_addr(site), clr, set); 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ciint instr_is_relative_branch(struct ppc_inst instr); 628c2ecf20Sopenharmony_ciint instr_is_relative_link_branch(struct ppc_inst instr); 638c2ecf20Sopenharmony_ciint instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr); 648c2ecf20Sopenharmony_ciunsigned long branch_target(const struct ppc_inst *instr); 658c2ecf20Sopenharmony_ciint translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest, 668c2ecf20Sopenharmony_ci const struct ppc_inst *src); 678c2ecf20Sopenharmony_ciextern bool is_conditional_branch(struct ppc_inst instr); 688c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3E_64 698c2ecf20Sopenharmony_civoid __patch_exception(int exc, unsigned long addr); 708c2ecf20Sopenharmony_ci#define patch_exception(exc, name) do { \ 718c2ecf20Sopenharmony_ci extern unsigned int name; \ 728c2ecf20Sopenharmony_ci __patch_exception((exc), (unsigned long)&name); \ 738c2ecf20Sopenharmony_ci} while (0) 748c2ecf20Sopenharmony_ci#endif 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci#define OP_RT_RA_MASK 0xffff0000UL 778c2ecf20Sopenharmony_ci#define LIS_R2 0x3c400000UL 788c2ecf20Sopenharmony_ci#define ADDIS_R2_R12 0x3c4c0000UL 798c2ecf20Sopenharmony_ci#define ADDI_R2_R2 0x38420000UL 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic inline unsigned long ppc_function_entry(void *func) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci#ifdef PPC64_ELF_ABI_v2 848c2ecf20Sopenharmony_ci u32 *insn = func; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci /* 878c2ecf20Sopenharmony_ci * A PPC64 ABIv2 function may have a local and a global entry 888c2ecf20Sopenharmony_ci * point. We need to use the local entry point when patching 898c2ecf20Sopenharmony_ci * functions, so identify and step over the global entry point 908c2ecf20Sopenharmony_ci * sequence. 918c2ecf20Sopenharmony_ci * 928c2ecf20Sopenharmony_ci * The global entry point sequence is always of the form: 938c2ecf20Sopenharmony_ci * 948c2ecf20Sopenharmony_ci * addis r2,r12,XXXX 958c2ecf20Sopenharmony_ci * addi r2,r2,XXXX 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * A linker optimisation may convert the addis to lis: 988c2ecf20Sopenharmony_ci * 998c2ecf20Sopenharmony_ci * lis r2,XXXX 1008c2ecf20Sopenharmony_ci * addi r2,r2,XXXX 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ci if ((((*insn & OP_RT_RA_MASK) == ADDIS_R2_R12) || 1038c2ecf20Sopenharmony_ci ((*insn & OP_RT_RA_MASK) == LIS_R2)) && 1048c2ecf20Sopenharmony_ci ((*(insn+1) & OP_RT_RA_MASK) == ADDI_R2_R2)) 1058c2ecf20Sopenharmony_ci return (unsigned long)(insn + 2); 1068c2ecf20Sopenharmony_ci else 1078c2ecf20Sopenharmony_ci return (unsigned long)func; 1088c2ecf20Sopenharmony_ci#elif defined(PPC64_ELF_ABI_v1) 1098c2ecf20Sopenharmony_ci /* 1108c2ecf20Sopenharmony_ci * On PPC64 ABIv1 the function pointer actually points to the 1118c2ecf20Sopenharmony_ci * function's descriptor. The first entry in the descriptor is the 1128c2ecf20Sopenharmony_ci * address of the function text. 1138c2ecf20Sopenharmony_ci */ 1148c2ecf20Sopenharmony_ci return ((func_descr_t *)func)->entry; 1158c2ecf20Sopenharmony_ci#else 1168c2ecf20Sopenharmony_ci return (unsigned long)func; 1178c2ecf20Sopenharmony_ci#endif 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic inline unsigned long ppc_global_function_entry(void *func) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci#ifdef PPC64_ELF_ABI_v2 1238c2ecf20Sopenharmony_ci /* PPC64 ABIv2 the global entry point is at the address */ 1248c2ecf20Sopenharmony_ci return (unsigned long)func; 1258c2ecf20Sopenharmony_ci#else 1268c2ecf20Sopenharmony_ci /* All other cases there is no change vs ppc_function_entry() */ 1278c2ecf20Sopenharmony_ci return ppc_function_entry(func); 1288c2ecf20Sopenharmony_ci#endif 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci/* 1328c2ecf20Sopenharmony_ci * Wrapper around kallsyms_lookup() to return function entry address: 1338c2ecf20Sopenharmony_ci * - For ABIv1, we lookup the dot variant. 1348c2ecf20Sopenharmony_ci * - For ABIv2, we return the local entry point. 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_cistatic inline unsigned long ppc_kallsyms_lookup_name(const char *name) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci unsigned long addr; 1398c2ecf20Sopenharmony_ci#ifdef PPC64_ELF_ABI_v1 1408c2ecf20Sopenharmony_ci /* check for dot variant */ 1418c2ecf20Sopenharmony_ci char dot_name[1 + KSYM_NAME_LEN]; 1428c2ecf20Sopenharmony_ci bool dot_appended = false; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci if (strnlen(name, KSYM_NAME_LEN) >= KSYM_NAME_LEN) 1458c2ecf20Sopenharmony_ci return 0; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci if (name[0] != '.') { 1488c2ecf20Sopenharmony_ci dot_name[0] = '.'; 1498c2ecf20Sopenharmony_ci dot_name[1] = '\0'; 1508c2ecf20Sopenharmony_ci strlcat(dot_name, name, sizeof(dot_name)); 1518c2ecf20Sopenharmony_ci dot_appended = true; 1528c2ecf20Sopenharmony_ci } else { 1538c2ecf20Sopenharmony_ci dot_name[0] = '\0'; 1548c2ecf20Sopenharmony_ci strlcat(dot_name, name, sizeof(dot_name)); 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci addr = kallsyms_lookup_name(dot_name); 1578c2ecf20Sopenharmony_ci if (!addr && dot_appended) 1588c2ecf20Sopenharmony_ci /* Let's try the original non-dot symbol lookup */ 1598c2ecf20Sopenharmony_ci addr = kallsyms_lookup_name(name); 1608c2ecf20Sopenharmony_ci#elif defined(PPC64_ELF_ABI_v2) 1618c2ecf20Sopenharmony_ci addr = kallsyms_lookup_name(name); 1628c2ecf20Sopenharmony_ci if (addr) 1638c2ecf20Sopenharmony_ci addr = ppc_function_entry((void *)addr); 1648c2ecf20Sopenharmony_ci#else 1658c2ecf20Sopenharmony_ci addr = kallsyms_lookup_name(name); 1668c2ecf20Sopenharmony_ci#endif 1678c2ecf20Sopenharmony_ci return addr; 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64 1718c2ecf20Sopenharmony_ci/* 1728c2ecf20Sopenharmony_ci * Some instruction encodings commonly used in dynamic ftracing 1738c2ecf20Sopenharmony_ci * and function live patching. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci/* This must match the definition of STK_GOT in <asm/ppc_asm.h> */ 1778c2ecf20Sopenharmony_ci#ifdef PPC64_ELF_ABI_v2 1788c2ecf20Sopenharmony_ci#define R2_STACK_OFFSET 24 1798c2ecf20Sopenharmony_ci#else 1808c2ecf20Sopenharmony_ci#define R2_STACK_OFFSET 40 1818c2ecf20Sopenharmony_ci#endif 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci#define PPC_INST_LD_TOC (PPC_INST_LD | ___PPC_RT(__REG_R2) | \ 1848c2ecf20Sopenharmony_ci ___PPC_RA(__REG_R1) | R2_STACK_OFFSET) 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci/* usually preceded by a mflr r0 */ 1878c2ecf20Sopenharmony_ci#define PPC_INST_STD_LR (PPC_INST_STD | ___PPC_RS(__REG_R0) | \ 1888c2ecf20Sopenharmony_ci ___PPC_RA(__REG_R1) | PPC_LR_STKOFF) 1898c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC64 */ 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_CODE_PATCHING_H */ 192