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