xref: /linux-6.15/arch/powerpc/lib/code-patching.c (revision b0337678)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright 2008 Michael Ellerman, IBM Corporation.
4  */
5 
6 #include <linux/kprobes.h>
7 #include <linux/vmalloc.h>
8 #include <linux/init.h>
9 #include <linux/cpuhotplug.h>
10 #include <linux/uaccess.h>
11 #include <linux/jump_label.h>
12 
13 #include <asm/tlbflush.h>
14 #include <asm/page.h>
15 #include <asm/code-patching.h>
16 #include <asm/inst.h>
17 
18 static int __patch_instruction(u32 *exec_addr, ppc_inst_t instr, u32 *patch_addr)
19 {
20 	if (!ppc_inst_prefixed(instr)) {
21 		u32 val = ppc_inst_val(instr);
22 
23 		__put_kernel_nofault(patch_addr, &val, u32, failed);
24 	} else {
25 		u64 val = ppc_inst_as_ulong(instr);
26 
27 		__put_kernel_nofault(patch_addr, &val, u64, failed);
28 	}
29 
30 	asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
31 							    "r" (exec_addr));
32 
33 	return 0;
34 
35 failed:
36 	return -EFAULT;
37 }
38 
39 int raw_patch_instruction(u32 *addr, ppc_inst_t instr)
40 {
41 	return __patch_instruction(addr, instr, addr);
42 }
43 
44 #ifdef CONFIG_STRICT_KERNEL_RWX
45 static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
46 
47 static int map_patch_area(void *addr, unsigned long text_poke_addr);
48 static void unmap_patch_area(unsigned long addr);
49 
50 static int text_area_cpu_up(unsigned int cpu)
51 {
52 	struct vm_struct *area;
53 	unsigned long addr;
54 	int err;
55 
56 	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
57 	if (!area) {
58 		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
59 			cpu);
60 		return -1;
61 	}
62 
63 	// Map/unmap the area to ensure all page tables are pre-allocated
64 	addr = (unsigned long)area->addr;
65 	err = map_patch_area(empty_zero_page, addr);
66 	if (err)
67 		return err;
68 
69 	unmap_patch_area(addr);
70 
71 	this_cpu_write(text_poke_area, area);
72 
73 	return 0;
74 }
75 
76 static int text_area_cpu_down(unsigned int cpu)
77 {
78 	free_vm_area(this_cpu_read(text_poke_area));
79 	return 0;
80 }
81 
82 /*
83  * Although BUG_ON() is rude, in this case it should only happen if ENOMEM, and
84  * we judge it as being preferable to a kernel that will crash later when
85  * someone tries to use patch_instruction().
86  */
87 void __init poking_init(void)
88 {
89 	BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
90 		"powerpc/text_poke:online", text_area_cpu_up,
91 		text_area_cpu_down));
92 }
93 
94 /*
95  * This can be called for kernel text or a module.
96  */
97 static int map_patch_area(void *addr, unsigned long text_poke_addr)
98 {
99 	unsigned long pfn;
100 
101 	if (IS_ENABLED(CONFIG_MODULES) && is_vmalloc_or_module_addr(addr))
102 		pfn = vmalloc_to_pfn(addr);
103 	else
104 		pfn = __pa_symbol(addr) >> PAGE_SHIFT;
105 
106 	return map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
107 }
108 
109 static void unmap_patch_area(unsigned long addr)
110 {
111 	pte_t *ptep;
112 	pmd_t *pmdp;
113 	pud_t *pudp;
114 	p4d_t *p4dp;
115 	pgd_t *pgdp;
116 
117 	pgdp = pgd_offset_k(addr);
118 	if (WARN_ON(pgd_none(*pgdp)))
119 		return;
120 
121 	p4dp = p4d_offset(pgdp, addr);
122 	if (WARN_ON(p4d_none(*p4dp)))
123 		return;
124 
125 	pudp = pud_offset(p4dp, addr);
126 	if (WARN_ON(pud_none(*pudp)))
127 		return;
128 
129 	pmdp = pmd_offset(pudp, addr);
130 	if (WARN_ON(pmd_none(*pmdp)))
131 		return;
132 
133 	ptep = pte_offset_kernel(pmdp, addr);
134 	if (WARN_ON(pte_none(*ptep)))
135 		return;
136 
137 	/*
138 	 * In hash, pte_clear flushes the tlb, in radix, we have to
139 	 */
140 	pte_clear(&init_mm, addr, ptep);
141 	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
142 }
143 
144 static int __do_patch_instruction(u32 *addr, ppc_inst_t instr)
145 {
146 	int err;
147 	u32 *patch_addr;
148 	unsigned long text_poke_addr;
149 
150 	text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
151 	patch_addr = (u32 *)(text_poke_addr + offset_in_page(addr));
152 
153 	err = map_patch_area(addr, text_poke_addr);
154 	if (err)
155 		return err;
156 
157 	err = __patch_instruction(addr, instr, patch_addr);
158 
159 	unmap_patch_area(text_poke_addr);
160 
161 	return err;
162 }
163 
164 static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
165 {
166 	int err;
167 	unsigned long flags;
168 
169 	/*
170 	 * During early early boot patch_instruction is called
171 	 * when text_poke_area is not ready, but we still need
172 	 * to allow patching. We just do the plain old patching
173 	 */
174 	if (!this_cpu_read(text_poke_area))
175 		return raw_patch_instruction(addr, instr);
176 
177 	local_irq_save(flags);
178 	err = __do_patch_instruction(addr, instr);
179 	local_irq_restore(flags);
180 
181 	return err;
182 }
183 #else /* !CONFIG_STRICT_KERNEL_RWX */
184 
185 static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
186 {
187 	return raw_patch_instruction(addr, instr);
188 }
189 
190 #endif /* CONFIG_STRICT_KERNEL_RWX */
191 
192 __ro_after_init DEFINE_STATIC_KEY_FALSE(init_mem_is_free);
193 
194 int patch_instruction(u32 *addr, ppc_inst_t instr)
195 {
196 	/* Make sure we aren't patching a freed init section */
197 	if (static_branch_likely(&init_mem_is_free) && init_section_contains(addr, 4))
198 		return 0;
199 
200 	return do_patch_instruction(addr, instr);
201 }
202 NOKPROBE_SYMBOL(patch_instruction);
203 
204 int patch_branch(u32 *addr, unsigned long target, int flags)
205 {
206 	ppc_inst_t instr;
207 
208 	if (create_branch(&instr, addr, target, flags))
209 		return -ERANGE;
210 
211 	return patch_instruction(addr, instr);
212 }
213 
214 bool is_offset_in_branch_range(long offset)
215 {
216 	/*
217 	 * Powerpc branch instruction is :
218 	 *
219 	 *  0         6                 30   31
220 	 *  +---------+----------------+---+---+
221 	 *  | opcode  |     LI         |AA |LK |
222 	 *  +---------+----------------+---+---+
223 	 *  Where AA = 0 and LK = 0
224 	 *
225 	 * LI is a signed 24 bits integer. The real branch offset is computed
226 	 * by: imm32 = SignExtend(LI:'0b00', 32);
227 	 *
228 	 * So the maximum forward branch should be:
229 	 *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
230 	 * The maximum backward branch should be:
231 	 *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
232 	 */
233 	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
234 }
235 
236 bool is_offset_in_cond_branch_range(long offset)
237 {
238 	return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
239 }
240 
241 /*
242  * Helper to check if a given instruction is a conditional branch
243  * Derived from the conditional checks in analyse_instr()
244  */
245 bool is_conditional_branch(ppc_inst_t instr)
246 {
247 	unsigned int opcode = ppc_inst_primary_opcode(instr);
248 
249 	if (opcode == 16)       /* bc, bca, bcl, bcla */
250 		return true;
251 	if (opcode == 19) {
252 		switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
253 		case 16:        /* bclr, bclrl */
254 		case 528:       /* bcctr, bcctrl */
255 		case 560:       /* bctar, bctarl */
256 			return true;
257 		}
258 	}
259 	return false;
260 }
261 NOKPROBE_SYMBOL(is_conditional_branch);
262 
263 int create_branch(ppc_inst_t *instr, const u32 *addr,
264 		  unsigned long target, int flags)
265 {
266 	long offset;
267 
268 	*instr = ppc_inst(0);
269 	offset = target;
270 	if (! (flags & BRANCH_ABSOLUTE))
271 		offset = offset - (unsigned long)addr;
272 
273 	/* Check we can represent the target in the instruction format */
274 	if (!is_offset_in_branch_range(offset))
275 		return 1;
276 
277 	/* Mask out the flags and target, so they don't step on each other. */
278 	*instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
279 
280 	return 0;
281 }
282 
283 int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
284 		       unsigned long target, int flags)
285 {
286 	long offset;
287 
288 	offset = target;
289 	if (! (flags & BRANCH_ABSOLUTE))
290 		offset = offset - (unsigned long)addr;
291 
292 	/* Check we can represent the target in the instruction format */
293 	if (!is_offset_in_cond_branch_range(offset))
294 		return 1;
295 
296 	/* Mask out the flags and target, so they don't step on each other. */
297 	*instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
298 
299 	return 0;
300 }
301 
302 int instr_is_relative_branch(ppc_inst_t instr)
303 {
304 	if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
305 		return 0;
306 
307 	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
308 }
309 
310 int instr_is_relative_link_branch(ppc_inst_t instr)
311 {
312 	return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
313 }
314 
315 static unsigned long branch_iform_target(const u32 *instr)
316 {
317 	signed long imm;
318 
319 	imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC;
320 
321 	/* If the top bit of the immediate value is set this is negative */
322 	if (imm & 0x2000000)
323 		imm -= 0x4000000;
324 
325 	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
326 		imm += (unsigned long)instr;
327 
328 	return (unsigned long)imm;
329 }
330 
331 static unsigned long branch_bform_target(const u32 *instr)
332 {
333 	signed long imm;
334 
335 	imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC;
336 
337 	/* If the top bit of the immediate value is set this is negative */
338 	if (imm & 0x8000)
339 		imm -= 0x10000;
340 
341 	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
342 		imm += (unsigned long)instr;
343 
344 	return (unsigned long)imm;
345 }
346 
347 unsigned long branch_target(const u32 *instr)
348 {
349 	if (instr_is_branch_iform(ppc_inst_read(instr)))
350 		return branch_iform_target(instr);
351 	else if (instr_is_branch_bform(ppc_inst_read(instr)))
352 		return branch_bform_target(instr);
353 
354 	return 0;
355 }
356 
357 int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src)
358 {
359 	unsigned long target;
360 	target = branch_target(src);
361 
362 	if (instr_is_branch_iform(ppc_inst_read(src)))
363 		return create_branch(instr, dest, target,
364 				     ppc_inst_val(ppc_inst_read(src)));
365 	else if (instr_is_branch_bform(ppc_inst_read(src)))
366 		return create_cond_branch(instr, dest, target,
367 					  ppc_inst_val(ppc_inst_read(src)));
368 
369 	return 1;
370 }
371