162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_CODE_PATCHING_H 362306a36Sopenharmony_ci#define _ASM_POWERPC_CODE_PATCHING_H 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci/* 662306a36Sopenharmony_ci * Copyright 2008, Michael Ellerman, IBM Corporation. 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <asm/types.h> 1062306a36Sopenharmony_ci#include <asm/ppc-opcode.h> 1162306a36Sopenharmony_ci#include <linux/string.h> 1262306a36Sopenharmony_ci#include <linux/kallsyms.h> 1362306a36Sopenharmony_ci#include <asm/asm-compat.h> 1462306a36Sopenharmony_ci#include <asm/inst.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci/* Flags for create_branch: 1762306a36Sopenharmony_ci * "b" == create_branch(addr, target, 0); 1862306a36Sopenharmony_ci * "ba" == create_branch(addr, target, BRANCH_ABSOLUTE); 1962306a36Sopenharmony_ci * "bl" == create_branch(addr, target, BRANCH_SET_LINK); 2062306a36Sopenharmony_ci * "bla" == create_branch(addr, target, BRANCH_ABSOLUTE | BRANCH_SET_LINK); 2162306a36Sopenharmony_ci */ 2262306a36Sopenharmony_ci#define BRANCH_SET_LINK 0x1 2362306a36Sopenharmony_ci#define BRANCH_ABSOLUTE 0x2 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci/* 2662306a36Sopenharmony_ci * Powerpc branch instruction is : 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * 0 6 30 31 2962306a36Sopenharmony_ci * +---------+----------------+---+---+ 3062306a36Sopenharmony_ci * | opcode | LI |AA |LK | 3162306a36Sopenharmony_ci * +---------+----------------+---+---+ 3262306a36Sopenharmony_ci * Where AA = 0 and LK = 0 3362306a36Sopenharmony_ci * 3462306a36Sopenharmony_ci * LI is a signed 24 bits integer. The real branch offset is computed 3562306a36Sopenharmony_ci * by: imm32 = SignExtend(LI:'0b00', 32); 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * So the maximum forward branch should be: 3862306a36Sopenharmony_ci * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc 3962306a36Sopenharmony_ci * The maximum backward branch should be: 4062306a36Sopenharmony_ci * (0xff800000 << 2) = 0xfe000000 = -0x2000000 4162306a36Sopenharmony_ci */ 4262306a36Sopenharmony_cistatic inline bool is_offset_in_branch_range(long offset) 4362306a36Sopenharmony_ci{ 4462306a36Sopenharmony_ci return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3)); 4562306a36Sopenharmony_ci} 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_cistatic inline bool is_offset_in_cond_branch_range(long offset) 4862306a36Sopenharmony_ci{ 4962306a36Sopenharmony_ci return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3); 5062306a36Sopenharmony_ci} 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_cistatic inline int create_branch(ppc_inst_t *instr, const u32 *addr, 5362306a36Sopenharmony_ci unsigned long target, int flags) 5462306a36Sopenharmony_ci{ 5562306a36Sopenharmony_ci long offset; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci *instr = ppc_inst(0); 5862306a36Sopenharmony_ci offset = target; 5962306a36Sopenharmony_ci if (! (flags & BRANCH_ABSOLUTE)) 6062306a36Sopenharmony_ci offset = offset - (unsigned long)addr; 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci /* Check we can represent the target in the instruction format */ 6362306a36Sopenharmony_ci if (!is_offset_in_branch_range(offset)) 6462306a36Sopenharmony_ci return 1; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci /* Mask out the flags and target, so they don't step on each other. */ 6762306a36Sopenharmony_ci *instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC)); 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci return 0; 7062306a36Sopenharmony_ci} 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ciint create_cond_branch(ppc_inst_t *instr, const u32 *addr, 7362306a36Sopenharmony_ci unsigned long target, int flags); 7462306a36Sopenharmony_ciint patch_branch(u32 *addr, unsigned long target, int flags); 7562306a36Sopenharmony_ciint patch_instruction(u32 *addr, ppc_inst_t instr); 7662306a36Sopenharmony_ciint raw_patch_instruction(u32 *addr, ppc_inst_t instr); 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_cistatic inline unsigned long patch_site_addr(s32 *site) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci return (unsigned long)site + *site; 8162306a36Sopenharmony_ci} 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_cistatic inline int patch_instruction_site(s32 *site, ppc_inst_t instr) 8462306a36Sopenharmony_ci{ 8562306a36Sopenharmony_ci return patch_instruction((u32 *)patch_site_addr(site), instr); 8662306a36Sopenharmony_ci} 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_cistatic inline int patch_branch_site(s32 *site, unsigned long target, int flags) 8962306a36Sopenharmony_ci{ 9062306a36Sopenharmony_ci return patch_branch((u32 *)patch_site_addr(site), target, flags); 9162306a36Sopenharmony_ci} 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_cistatic inline int modify_instruction(unsigned int *addr, unsigned int clr, 9462306a36Sopenharmony_ci unsigned int set) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci return patch_instruction(addr, ppc_inst((*addr & ~clr) | set)); 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_cistatic inline int modify_instruction_site(s32 *site, unsigned int clr, unsigned int set) 10062306a36Sopenharmony_ci{ 10162306a36Sopenharmony_ci return modify_instruction((unsigned int *)patch_site_addr(site), clr, set); 10262306a36Sopenharmony_ci} 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_cistatic inline unsigned int branch_opcode(ppc_inst_t instr) 10562306a36Sopenharmony_ci{ 10662306a36Sopenharmony_ci return ppc_inst_primary_opcode(instr) & 0x3F; 10762306a36Sopenharmony_ci} 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_cistatic inline int instr_is_branch_iform(ppc_inst_t instr) 11062306a36Sopenharmony_ci{ 11162306a36Sopenharmony_ci return branch_opcode(instr) == 18; 11262306a36Sopenharmony_ci} 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_cistatic inline int instr_is_branch_bform(ppc_inst_t instr) 11562306a36Sopenharmony_ci{ 11662306a36Sopenharmony_ci return branch_opcode(instr) == 16; 11762306a36Sopenharmony_ci} 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ciint instr_is_relative_branch(ppc_inst_t instr); 12062306a36Sopenharmony_ciint instr_is_relative_link_branch(ppc_inst_t instr); 12162306a36Sopenharmony_ciunsigned long branch_target(const u32 *instr); 12262306a36Sopenharmony_ciint translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src); 12362306a36Sopenharmony_cibool is_conditional_branch(ppc_inst_t instr); 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci#define OP_RT_RA_MASK 0xffff0000UL 12662306a36Sopenharmony_ci#define LIS_R2 (PPC_RAW_LIS(_R2, 0)) 12762306a36Sopenharmony_ci#define ADDIS_R2_R12 (PPC_RAW_ADDIS(_R2, _R12, 0)) 12862306a36Sopenharmony_ci#define ADDI_R2_R2 (PPC_RAW_ADDI(_R2, _R2, 0)) 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_cistatic inline unsigned long ppc_function_entry(void *func) 13262306a36Sopenharmony_ci{ 13362306a36Sopenharmony_ci#ifdef CONFIG_PPC64_ELF_ABI_V2 13462306a36Sopenharmony_ci u32 *insn = func; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci /* 13762306a36Sopenharmony_ci * A PPC64 ABIv2 function may have a local and a global entry 13862306a36Sopenharmony_ci * point. We need to use the local entry point when patching 13962306a36Sopenharmony_ci * functions, so identify and step over the global entry point 14062306a36Sopenharmony_ci * sequence. 14162306a36Sopenharmony_ci * 14262306a36Sopenharmony_ci * The global entry point sequence is always of the form: 14362306a36Sopenharmony_ci * 14462306a36Sopenharmony_ci * addis r2,r12,XXXX 14562306a36Sopenharmony_ci * addi r2,r2,XXXX 14662306a36Sopenharmony_ci * 14762306a36Sopenharmony_ci * A linker optimisation may convert the addis to lis: 14862306a36Sopenharmony_ci * 14962306a36Sopenharmony_ci * lis r2,XXXX 15062306a36Sopenharmony_ci * addi r2,r2,XXXX 15162306a36Sopenharmony_ci */ 15262306a36Sopenharmony_ci if ((((*insn & OP_RT_RA_MASK) == ADDIS_R2_R12) || 15362306a36Sopenharmony_ci ((*insn & OP_RT_RA_MASK) == LIS_R2)) && 15462306a36Sopenharmony_ci ((*(insn+1) & OP_RT_RA_MASK) == ADDI_R2_R2)) 15562306a36Sopenharmony_ci return (unsigned long)(insn + 2); 15662306a36Sopenharmony_ci else 15762306a36Sopenharmony_ci return (unsigned long)func; 15862306a36Sopenharmony_ci#elif defined(CONFIG_PPC64_ELF_ABI_V1) 15962306a36Sopenharmony_ci /* 16062306a36Sopenharmony_ci * On PPC64 ABIv1 the function pointer actually points to the 16162306a36Sopenharmony_ci * function's descriptor. The first entry in the descriptor is the 16262306a36Sopenharmony_ci * address of the function text. 16362306a36Sopenharmony_ci */ 16462306a36Sopenharmony_ci return ((struct func_desc *)func)->addr; 16562306a36Sopenharmony_ci#else 16662306a36Sopenharmony_ci return (unsigned long)func; 16762306a36Sopenharmony_ci#endif 16862306a36Sopenharmony_ci} 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_cistatic inline unsigned long ppc_global_function_entry(void *func) 17162306a36Sopenharmony_ci{ 17262306a36Sopenharmony_ci#ifdef CONFIG_PPC64_ELF_ABI_V2 17362306a36Sopenharmony_ci /* PPC64 ABIv2 the global entry point is at the address */ 17462306a36Sopenharmony_ci return (unsigned long)func; 17562306a36Sopenharmony_ci#else 17662306a36Sopenharmony_ci /* All other cases there is no change vs ppc_function_entry() */ 17762306a36Sopenharmony_ci return ppc_function_entry(func); 17862306a36Sopenharmony_ci#endif 17962306a36Sopenharmony_ci} 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci/* 18262306a36Sopenharmony_ci * Wrapper around kallsyms_lookup() to return function entry address: 18362306a36Sopenharmony_ci * - For ABIv1, we lookup the dot variant. 18462306a36Sopenharmony_ci * - For ABIv2, we return the local entry point. 18562306a36Sopenharmony_ci */ 18662306a36Sopenharmony_cistatic inline unsigned long ppc_kallsyms_lookup_name(const char *name) 18762306a36Sopenharmony_ci{ 18862306a36Sopenharmony_ci unsigned long addr; 18962306a36Sopenharmony_ci#ifdef CONFIG_PPC64_ELF_ABI_V1 19062306a36Sopenharmony_ci /* check for dot variant */ 19162306a36Sopenharmony_ci char dot_name[1 + KSYM_NAME_LEN]; 19262306a36Sopenharmony_ci bool dot_appended = false; 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci if (strnlen(name, KSYM_NAME_LEN) >= KSYM_NAME_LEN) 19562306a36Sopenharmony_ci return 0; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci if (name[0] != '.') { 19862306a36Sopenharmony_ci dot_name[0] = '.'; 19962306a36Sopenharmony_ci dot_name[1] = '\0'; 20062306a36Sopenharmony_ci strlcat(dot_name, name, sizeof(dot_name)); 20162306a36Sopenharmony_ci dot_appended = true; 20262306a36Sopenharmony_ci } else { 20362306a36Sopenharmony_ci dot_name[0] = '\0'; 20462306a36Sopenharmony_ci strlcat(dot_name, name, sizeof(dot_name)); 20562306a36Sopenharmony_ci } 20662306a36Sopenharmony_ci addr = kallsyms_lookup_name(dot_name); 20762306a36Sopenharmony_ci if (!addr && dot_appended) 20862306a36Sopenharmony_ci /* Let's try the original non-dot symbol lookup */ 20962306a36Sopenharmony_ci addr = kallsyms_lookup_name(name); 21062306a36Sopenharmony_ci#elif defined(CONFIG_PPC64_ELF_ABI_V2) 21162306a36Sopenharmony_ci addr = kallsyms_lookup_name(name); 21262306a36Sopenharmony_ci if (addr) 21362306a36Sopenharmony_ci addr = ppc_function_entry((void *)addr); 21462306a36Sopenharmony_ci#else 21562306a36Sopenharmony_ci addr = kallsyms_lookup_name(name); 21662306a36Sopenharmony_ci#endif 21762306a36Sopenharmony_ci return addr; 21862306a36Sopenharmony_ci} 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci/* 22162306a36Sopenharmony_ci * Some instruction encodings commonly used in dynamic ftracing 22262306a36Sopenharmony_ci * and function live patching. 22362306a36Sopenharmony_ci */ 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci/* This must match the definition of STK_GOT in <asm/ppc_asm.h> */ 22662306a36Sopenharmony_ci#ifdef CONFIG_PPC64_ELF_ABI_V2 22762306a36Sopenharmony_ci#define R2_STACK_OFFSET 24 22862306a36Sopenharmony_ci#else 22962306a36Sopenharmony_ci#define R2_STACK_OFFSET 40 23062306a36Sopenharmony_ci#endif 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci#define PPC_INST_LD_TOC PPC_RAW_LD(_R2, _R1, R2_STACK_OFFSET) 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci/* usually preceded by a mflr r0 */ 23562306a36Sopenharmony_ci#define PPC_INST_STD_LR PPC_RAW_STD(_R0, _R1, PPC_LR_STKOFF) 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci#endif /* _ASM_POWERPC_CODE_PATCHING_H */ 238