xref: /OK3568_Linux_fs/kernel/arch/powerpc/include/asm/code-patching.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun #ifndef _ASM_POWERPC_CODE_PATCHING_H
3*4882a593Smuzhiyun #define _ASM_POWERPC_CODE_PATCHING_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun  * Copyright 2008, Michael Ellerman, IBM Corporation.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <asm/types.h>
10*4882a593Smuzhiyun #include <asm/ppc-opcode.h>
11*4882a593Smuzhiyun #include <linux/string.h>
12*4882a593Smuzhiyun #include <linux/kallsyms.h>
13*4882a593Smuzhiyun #include <asm/asm-compat.h>
14*4882a593Smuzhiyun #include <asm/inst.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /* Flags for create_branch:
17*4882a593Smuzhiyun  * "b"   == create_branch(addr, target, 0);
18*4882a593Smuzhiyun  * "ba"  == create_branch(addr, target, BRANCH_ABSOLUTE);
19*4882a593Smuzhiyun  * "bl"  == create_branch(addr, target, BRANCH_SET_LINK);
20*4882a593Smuzhiyun  * "bla" == create_branch(addr, target, BRANCH_ABSOLUTE | BRANCH_SET_LINK);
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun #define BRANCH_SET_LINK	0x1
23*4882a593Smuzhiyun #define BRANCH_ABSOLUTE	0x2
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun bool is_offset_in_branch_range(long offset);
26*4882a593Smuzhiyun bool is_offset_in_cond_branch_range(long offset);
27*4882a593Smuzhiyun int create_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
28*4882a593Smuzhiyun 		  unsigned long target, int flags);
29*4882a593Smuzhiyun int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
30*4882a593Smuzhiyun 		       unsigned long target, int flags);
31*4882a593Smuzhiyun int patch_branch(struct ppc_inst *addr, unsigned long target, int flags);
32*4882a593Smuzhiyun int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr);
33*4882a593Smuzhiyun int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr);
34*4882a593Smuzhiyun 
patch_site_addr(s32 * site)35*4882a593Smuzhiyun static inline unsigned long patch_site_addr(s32 *site)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	return (unsigned long)site + *site;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
patch_instruction_site(s32 * site,struct ppc_inst instr)40*4882a593Smuzhiyun static inline int patch_instruction_site(s32 *site, struct ppc_inst instr)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	return patch_instruction((struct ppc_inst *)patch_site_addr(site), instr);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
patch_branch_site(s32 * site,unsigned long target,int flags)45*4882a593Smuzhiyun static inline int patch_branch_site(s32 *site, unsigned long target, int flags)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	return patch_branch((struct ppc_inst *)patch_site_addr(site), target, flags);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
modify_instruction(unsigned int * addr,unsigned int clr,unsigned int set)50*4882a593Smuzhiyun static inline int modify_instruction(unsigned int *addr, unsigned int clr,
51*4882a593Smuzhiyun 				     unsigned int set)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	return patch_instruction((struct ppc_inst *)addr, ppc_inst((*addr & ~clr) | set));
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
modify_instruction_site(s32 * site,unsigned int clr,unsigned int set)56*4882a593Smuzhiyun static inline int modify_instruction_site(s32 *site, unsigned int clr, unsigned int set)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	return modify_instruction((unsigned int *)patch_site_addr(site), clr, set);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun int instr_is_relative_branch(struct ppc_inst instr);
62*4882a593Smuzhiyun int instr_is_relative_link_branch(struct ppc_inst instr);
63*4882a593Smuzhiyun int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr);
64*4882a593Smuzhiyun unsigned long branch_target(const struct ppc_inst *instr);
65*4882a593Smuzhiyun int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
66*4882a593Smuzhiyun 		     const struct ppc_inst *src);
67*4882a593Smuzhiyun extern bool is_conditional_branch(struct ppc_inst instr);
68*4882a593Smuzhiyun #ifdef CONFIG_PPC_BOOK3E_64
69*4882a593Smuzhiyun void __patch_exception(int exc, unsigned long addr);
70*4882a593Smuzhiyun #define patch_exception(exc, name) do { \
71*4882a593Smuzhiyun 	extern unsigned int name; \
72*4882a593Smuzhiyun 	__patch_exception((exc), (unsigned long)&name); \
73*4882a593Smuzhiyun } while (0)
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define OP_RT_RA_MASK	0xffff0000UL
77*4882a593Smuzhiyun #define LIS_R2		0x3c400000UL
78*4882a593Smuzhiyun #define ADDIS_R2_R12	0x3c4c0000UL
79*4882a593Smuzhiyun #define ADDI_R2_R2	0x38420000UL
80*4882a593Smuzhiyun 
ppc_function_entry(void * func)81*4882a593Smuzhiyun static inline unsigned long ppc_function_entry(void *func)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun #ifdef PPC64_ELF_ABI_v2
84*4882a593Smuzhiyun 	u32 *insn = func;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/*
87*4882a593Smuzhiyun 	 * A PPC64 ABIv2 function may have a local and a global entry
88*4882a593Smuzhiyun 	 * point. We need to use the local entry point when patching
89*4882a593Smuzhiyun 	 * functions, so identify and step over the global entry point
90*4882a593Smuzhiyun 	 * sequence.
91*4882a593Smuzhiyun 	 *
92*4882a593Smuzhiyun 	 * The global entry point sequence is always of the form:
93*4882a593Smuzhiyun 	 *
94*4882a593Smuzhiyun 	 * addis r2,r12,XXXX
95*4882a593Smuzhiyun 	 * addi  r2,r2,XXXX
96*4882a593Smuzhiyun 	 *
97*4882a593Smuzhiyun 	 * A linker optimisation may convert the addis to lis:
98*4882a593Smuzhiyun 	 *
99*4882a593Smuzhiyun 	 * lis   r2,XXXX
100*4882a593Smuzhiyun 	 * addi  r2,r2,XXXX
101*4882a593Smuzhiyun 	 */
102*4882a593Smuzhiyun 	if ((((*insn & OP_RT_RA_MASK) == ADDIS_R2_R12) ||
103*4882a593Smuzhiyun 	     ((*insn & OP_RT_RA_MASK) == LIS_R2)) &&
104*4882a593Smuzhiyun 	    ((*(insn+1) & OP_RT_RA_MASK) == ADDI_R2_R2))
105*4882a593Smuzhiyun 		return (unsigned long)(insn + 2);
106*4882a593Smuzhiyun 	else
107*4882a593Smuzhiyun 		return (unsigned long)func;
108*4882a593Smuzhiyun #elif defined(PPC64_ELF_ABI_v1)
109*4882a593Smuzhiyun 	/*
110*4882a593Smuzhiyun 	 * On PPC64 ABIv1 the function pointer actually points to the
111*4882a593Smuzhiyun 	 * function's descriptor. The first entry in the descriptor is the
112*4882a593Smuzhiyun 	 * address of the function text.
113*4882a593Smuzhiyun 	 */
114*4882a593Smuzhiyun 	return ((func_descr_t *)func)->entry;
115*4882a593Smuzhiyun #else
116*4882a593Smuzhiyun 	return (unsigned long)func;
117*4882a593Smuzhiyun #endif
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
ppc_global_function_entry(void * func)120*4882a593Smuzhiyun static inline unsigned long ppc_global_function_entry(void *func)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun #ifdef PPC64_ELF_ABI_v2
123*4882a593Smuzhiyun 	/* PPC64 ABIv2 the global entry point is at the address */
124*4882a593Smuzhiyun 	return (unsigned long)func;
125*4882a593Smuzhiyun #else
126*4882a593Smuzhiyun 	/* All other cases there is no change vs ppc_function_entry() */
127*4882a593Smuzhiyun 	return ppc_function_entry(func);
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun  * Wrapper around kallsyms_lookup() to return function entry address:
133*4882a593Smuzhiyun  * - For ABIv1, we lookup the dot variant.
134*4882a593Smuzhiyun  * - For ABIv2, we return the local entry point.
135*4882a593Smuzhiyun  */
ppc_kallsyms_lookup_name(const char * name)136*4882a593Smuzhiyun static inline unsigned long ppc_kallsyms_lookup_name(const char *name)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	unsigned long addr;
139*4882a593Smuzhiyun #ifdef PPC64_ELF_ABI_v1
140*4882a593Smuzhiyun 	/* check for dot variant */
141*4882a593Smuzhiyun 	char dot_name[1 + KSYM_NAME_LEN];
142*4882a593Smuzhiyun 	bool dot_appended = false;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	if (strnlen(name, KSYM_NAME_LEN) >= KSYM_NAME_LEN)
145*4882a593Smuzhiyun 		return 0;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	if (name[0] != '.') {
148*4882a593Smuzhiyun 		dot_name[0] = '.';
149*4882a593Smuzhiyun 		dot_name[1] = '\0';
150*4882a593Smuzhiyun 		strlcat(dot_name, name, sizeof(dot_name));
151*4882a593Smuzhiyun 		dot_appended = true;
152*4882a593Smuzhiyun 	} else {
153*4882a593Smuzhiyun 		dot_name[0] = '\0';
154*4882a593Smuzhiyun 		strlcat(dot_name, name, sizeof(dot_name));
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 	addr = kallsyms_lookup_name(dot_name);
157*4882a593Smuzhiyun 	if (!addr && dot_appended)
158*4882a593Smuzhiyun 		/* Let's try the original non-dot symbol lookup	*/
159*4882a593Smuzhiyun 		addr = kallsyms_lookup_name(name);
160*4882a593Smuzhiyun #elif defined(PPC64_ELF_ABI_v2)
161*4882a593Smuzhiyun 	addr = kallsyms_lookup_name(name);
162*4882a593Smuzhiyun 	if (addr)
163*4882a593Smuzhiyun 		addr = ppc_function_entry((void *)addr);
164*4882a593Smuzhiyun #else
165*4882a593Smuzhiyun 	addr = kallsyms_lookup_name(name);
166*4882a593Smuzhiyun #endif
167*4882a593Smuzhiyun 	return addr;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun #ifdef CONFIG_PPC64
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun  * Some instruction encodings commonly used in dynamic ftracing
173*4882a593Smuzhiyun  * and function live patching.
174*4882a593Smuzhiyun  */
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /* This must match the definition of STK_GOT in <asm/ppc_asm.h> */
177*4882a593Smuzhiyun #ifdef PPC64_ELF_ABI_v2
178*4882a593Smuzhiyun #define R2_STACK_OFFSET         24
179*4882a593Smuzhiyun #else
180*4882a593Smuzhiyun #define R2_STACK_OFFSET         40
181*4882a593Smuzhiyun #endif
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun #define PPC_INST_LD_TOC		(PPC_INST_LD  | ___PPC_RT(__REG_R2) | \
184*4882a593Smuzhiyun 				 ___PPC_RA(__REG_R1) | R2_STACK_OFFSET)
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /* usually preceded by a mflr r0 */
187*4882a593Smuzhiyun #define PPC_INST_STD_LR		(PPC_INST_STD | ___PPC_RS(__REG_R0) | \
188*4882a593Smuzhiyun 				 ___PPC_RA(__REG_R1) | PPC_LR_STKOFF)
189*4882a593Smuzhiyun #endif /* CONFIG_PPC64 */
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun #endif /* _ASM_POWERPC_CODE_PATCHING_H */
192