xref: /linux/arch/powerpc/lib/code-patching.c (revision 4fd18fc38757217c746aa063ba9e4729814dc737)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright 2008 Michael Ellerman, IBM Corporation.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/kprobes.h>
8 #include <linux/vmalloc.h>
9 #include <linux/init.h>
10 #include <linux/mm.h>
11 #include <linux/cpuhotplug.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 
15 #include <asm/tlbflush.h>
16 #include <asm/page.h>
17 #include <asm/code-patching.h>
18 #include <asm/setup.h>
19 #include <asm/inst.h>
20 
21 static int __patch_instruction(struct ppc_inst *exec_addr, struct ppc_inst instr,
22 			       struct ppc_inst *patch_addr)
23 {
24 	if (!ppc_inst_prefixed(instr))
25 		__put_user_asm_goto(ppc_inst_val(instr), patch_addr, failed, "stw");
26 	else
27 		__put_user_asm_goto(ppc_inst_as_u64(instr), patch_addr, failed, "std");
28 
29 	asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
30 							    "r" (exec_addr));
31 
32 	return 0;
33 
34 failed:
35 	return -EFAULT;
36 }
37 
38 int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
39 {
40 	return __patch_instruction(addr, instr, addr);
41 }
42 
43 #ifdef CONFIG_STRICT_KERNEL_RWX
44 static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
45 
46 static int text_area_cpu_up(unsigned int cpu)
47 {
48 	struct vm_struct *area;
49 
50 	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
51 	if (!area) {
52 		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
53 			cpu);
54 		return -1;
55 	}
56 	this_cpu_write(text_poke_area, area);
57 
58 	return 0;
59 }
60 
61 static int text_area_cpu_down(unsigned int cpu)
62 {
63 	free_vm_area(this_cpu_read(text_poke_area));
64 	return 0;
65 }
66 
67 /*
68  * Run as a late init call. This allows all the boot time patching to be done
69  * simply by patching the code, and then we're called here prior to
70  * mark_rodata_ro(), which happens after all init calls are run. Although
71  * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
72  * it as being preferable to a kernel that will crash later when someone tries
73  * to use patch_instruction().
74  */
75 static int __init setup_text_poke_area(void)
76 {
77 	BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
78 		"powerpc/text_poke:online", text_area_cpu_up,
79 		text_area_cpu_down));
80 
81 	return 0;
82 }
83 late_initcall(setup_text_poke_area);
84 
85 /*
86  * This can be called for kernel text or a module.
87  */
88 static int map_patch_area(void *addr, unsigned long text_poke_addr)
89 {
90 	unsigned long pfn;
91 	int err;
92 
93 	if (is_vmalloc_or_module_addr(addr))
94 		pfn = vmalloc_to_pfn(addr);
95 	else
96 		pfn = __pa_symbol(addr) >> PAGE_SHIFT;
97 
98 	err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
99 
100 	pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
101 	if (err)
102 		return -1;
103 
104 	return 0;
105 }
106 
107 static inline int unmap_patch_area(unsigned long addr)
108 {
109 	pte_t *ptep;
110 	pmd_t *pmdp;
111 	pud_t *pudp;
112 	p4d_t *p4dp;
113 	pgd_t *pgdp;
114 
115 	pgdp = pgd_offset_k(addr);
116 	if (unlikely(!pgdp))
117 		return -EINVAL;
118 
119 	p4dp = p4d_offset(pgdp, addr);
120 	if (unlikely(!p4dp))
121 		return -EINVAL;
122 
123 	pudp = pud_offset(p4dp, addr);
124 	if (unlikely(!pudp))
125 		return -EINVAL;
126 
127 	pmdp = pmd_offset(pudp, addr);
128 	if (unlikely(!pmdp))
129 		return -EINVAL;
130 
131 	ptep = pte_offset_kernel(pmdp, addr);
132 	if (unlikely(!ptep))
133 		return -EINVAL;
134 
135 	pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
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 	return 0;
144 }
145 
146 static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
147 {
148 	int err;
149 	struct ppc_inst *patch_addr = NULL;
150 	unsigned long flags;
151 	unsigned long text_poke_addr;
152 	unsigned long kaddr = (unsigned long)addr;
153 
154 	/*
155 	 * During early early boot patch_instruction is called
156 	 * when text_poke_area is not ready, but we still need
157 	 * to allow patching. We just do the plain old patching
158 	 */
159 	if (!this_cpu_read(text_poke_area))
160 		return raw_patch_instruction(addr, instr);
161 
162 	local_irq_save(flags);
163 
164 	text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
165 	if (map_patch_area(addr, text_poke_addr)) {
166 		err = -1;
167 		goto out;
168 	}
169 
170 	patch_addr = (struct ppc_inst *)(text_poke_addr + (kaddr & ~PAGE_MASK));
171 
172 	__patch_instruction(addr, instr, patch_addr);
173 
174 	err = unmap_patch_area(text_poke_addr);
175 	if (err)
176 		pr_warn("failed to unmap %lx\n", text_poke_addr);
177 
178 out:
179 	local_irq_restore(flags);
180 
181 	return err;
182 }
183 #else /* !CONFIG_STRICT_KERNEL_RWX */
184 
185 static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
186 {
187 	return raw_patch_instruction(addr, instr);
188 }
189 
190 #endif /* CONFIG_STRICT_KERNEL_RWX */
191 
192 int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
193 {
194 	/* Make sure we aren't patching a freed init section */
195 	if (init_mem_is_free && init_section_contains(addr, 4)) {
196 		pr_debug("Skipping init section patching addr: 0x%px\n", addr);
197 		return 0;
198 	}
199 	return do_patch_instruction(addr, instr);
200 }
201 NOKPROBE_SYMBOL(patch_instruction);
202 
203 int patch_branch(struct ppc_inst *addr, unsigned long target, int flags)
204 {
205 	struct ppc_inst instr;
206 
207 	create_branch(&instr, addr, target, flags);
208 	return patch_instruction(addr, instr);
209 }
210 
211 bool is_offset_in_branch_range(long offset)
212 {
213 	/*
214 	 * Powerpc branch instruction is :
215 	 *
216 	 *  0         6                 30   31
217 	 *  +---------+----------------+---+---+
218 	 *  | opcode  |     LI         |AA |LK |
219 	 *  +---------+----------------+---+---+
220 	 *  Where AA = 0 and LK = 0
221 	 *
222 	 * LI is a signed 24 bits integer. The real branch offset is computed
223 	 * by: imm32 = SignExtend(LI:'0b00', 32);
224 	 *
225 	 * So the maximum forward branch should be:
226 	 *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
227 	 * The maximum backward branch should be:
228 	 *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
229 	 */
230 	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
231 }
232 
233 /*
234  * Helper to check if a given instruction is a conditional branch
235  * Derived from the conditional checks in analyse_instr()
236  */
237 bool is_conditional_branch(struct ppc_inst instr)
238 {
239 	unsigned int opcode = ppc_inst_primary_opcode(instr);
240 
241 	if (opcode == 16)       /* bc, bca, bcl, bcla */
242 		return true;
243 	if (opcode == 19) {
244 		switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
245 		case 16:        /* bclr, bclrl */
246 		case 528:       /* bcctr, bcctrl */
247 		case 560:       /* bctar, bctarl */
248 			return true;
249 		}
250 	}
251 	return false;
252 }
253 NOKPROBE_SYMBOL(is_conditional_branch);
254 
255 int create_branch(struct ppc_inst *instr,
256 		  const struct ppc_inst *addr,
257 		  unsigned long target, int flags)
258 {
259 	long offset;
260 
261 	*instr = ppc_inst(0);
262 	offset = target;
263 	if (! (flags & BRANCH_ABSOLUTE))
264 		offset = offset - (unsigned long)addr;
265 
266 	/* Check we can represent the target in the instruction format */
267 	if (!is_offset_in_branch_range(offset))
268 		return 1;
269 
270 	/* Mask out the flags and target, so they don't step on each other. */
271 	*instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
272 
273 	return 0;
274 }
275 
276 int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
277 		       unsigned long target, int flags)
278 {
279 	long offset;
280 
281 	offset = target;
282 	if (! (flags & BRANCH_ABSOLUTE))
283 		offset = offset - (unsigned long)addr;
284 
285 	/* Check we can represent the target in the instruction format */
286 	if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
287 		return 1;
288 
289 	/* Mask out the flags and target, so they don't step on each other. */
290 	*instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
291 
292 	return 0;
293 }
294 
295 static unsigned int branch_opcode(struct ppc_inst instr)
296 {
297 	return ppc_inst_primary_opcode(instr) & 0x3F;
298 }
299 
300 static int instr_is_branch_iform(struct ppc_inst instr)
301 {
302 	return branch_opcode(instr) == 18;
303 }
304 
305 static int instr_is_branch_bform(struct ppc_inst instr)
306 {
307 	return branch_opcode(instr) == 16;
308 }
309 
310 int instr_is_relative_branch(struct ppc_inst instr)
311 {
312 	if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
313 		return 0;
314 
315 	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
316 }
317 
318 int instr_is_relative_link_branch(struct ppc_inst instr)
319 {
320 	return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
321 }
322 
323 static unsigned long branch_iform_target(const struct ppc_inst *instr)
324 {
325 	signed long imm;
326 
327 	imm = ppc_inst_val(*instr) & 0x3FFFFFC;
328 
329 	/* If the top bit of the immediate value is set this is negative */
330 	if (imm & 0x2000000)
331 		imm -= 0x4000000;
332 
333 	if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
334 		imm += (unsigned long)instr;
335 
336 	return (unsigned long)imm;
337 }
338 
339 static unsigned long branch_bform_target(const struct ppc_inst *instr)
340 {
341 	signed long imm;
342 
343 	imm = ppc_inst_val(*instr) & 0xFFFC;
344 
345 	/* If the top bit of the immediate value is set this is negative */
346 	if (imm & 0x8000)
347 		imm -= 0x10000;
348 
349 	if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
350 		imm += (unsigned long)instr;
351 
352 	return (unsigned long)imm;
353 }
354 
355 unsigned long branch_target(const struct ppc_inst *instr)
356 {
357 	if (instr_is_branch_iform(ppc_inst_read(instr)))
358 		return branch_iform_target(instr);
359 	else if (instr_is_branch_bform(ppc_inst_read(instr)))
360 		return branch_bform_target(instr);
361 
362 	return 0;
363 }
364 
365 int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr)
366 {
367 	if (instr_is_branch_iform(ppc_inst_read(instr)) ||
368 	    instr_is_branch_bform(ppc_inst_read(instr)))
369 		return branch_target(instr) == addr;
370 
371 	return 0;
372 }
373 
374 int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
375 		     const struct ppc_inst *src)
376 {
377 	unsigned long target;
378 	target = branch_target(src);
379 
380 	if (instr_is_branch_iform(ppc_inst_read(src)))
381 		return create_branch(instr, dest, target,
382 				     ppc_inst_val(ppc_inst_read(src)));
383 	else if (instr_is_branch_bform(ppc_inst_read(src)))
384 		return create_cond_branch(instr, dest, target,
385 					  ppc_inst_val(ppc_inst_read(src)));
386 
387 	return 1;
388 }
389 
390 #ifdef CONFIG_PPC_BOOK3E_64
391 void __patch_exception(int exc, unsigned long addr)
392 {
393 	extern unsigned int interrupt_base_book3e;
394 	unsigned int *ibase = &interrupt_base_book3e;
395 
396 	/* Our exceptions vectors start with a NOP and -then- a branch
397 	 * to deal with single stepping from userspace which stops on
398 	 * the second instruction. Thus we need to patch the second
399 	 * instruction of the exception, not the first one
400 	 */
401 
402 	patch_branch((struct ppc_inst *)(ibase + (exc / 4) + 1), addr, 0);
403 }
404 #endif
405 
406 #ifdef CONFIG_CODE_PATCHING_SELFTEST
407 
408 static void __init test_trampoline(void)
409 {
410 	asm ("nop;\n");
411 }
412 
413 #define check(x)	\
414 	if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
415 
416 static void __init test_branch_iform(void)
417 {
418 	int err;
419 	struct ppc_inst instr;
420 	unsigned long addr;
421 
422 	addr = (unsigned long)&instr;
423 
424 	/* The simplest case, branch to self, no flags */
425 	check(instr_is_branch_iform(ppc_inst(0x48000000)));
426 	/* All bits of target set, and flags */
427 	check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
428 	/* High bit of opcode set, which is wrong */
429 	check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
430 	/* Middle bits of opcode set, which is wrong */
431 	check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
432 
433 	/* Simplest case, branch to self with link */
434 	check(instr_is_branch_iform(ppc_inst(0x48000001)));
435 	/* All bits of targets set */
436 	check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
437 	/* Some bits of targets set */
438 	check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
439 	/* Must be a valid branch to start with */
440 	check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
441 
442 	/* Absolute branch to 0x100 */
443 	instr = ppc_inst(0x48000103);
444 	check(instr_is_branch_to_addr(&instr, 0x100));
445 	/* Absolute branch to 0x420fc */
446 	instr = ppc_inst(0x480420ff);
447 	check(instr_is_branch_to_addr(&instr, 0x420fc));
448 	/* Maximum positive relative branch, + 20MB - 4B */
449 	instr = ppc_inst(0x49fffffc);
450 	check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
451 	/* Smallest negative relative branch, - 4B */
452 	instr = ppc_inst(0x4bfffffc);
453 	check(instr_is_branch_to_addr(&instr, addr - 4));
454 	/* Largest negative relative branch, - 32 MB */
455 	instr = ppc_inst(0x4a000000);
456 	check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
457 
458 	/* Branch to self, with link */
459 	err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
460 	check(instr_is_branch_to_addr(&instr, addr));
461 
462 	/* Branch to self - 0x100, with link */
463 	err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
464 	check(instr_is_branch_to_addr(&instr, addr - 0x100));
465 
466 	/* Branch to self + 0x100, no link */
467 	err = create_branch(&instr, &instr, addr + 0x100, 0);
468 	check(instr_is_branch_to_addr(&instr, addr + 0x100));
469 
470 	/* Maximum relative negative offset, - 32 MB */
471 	err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
472 	check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
473 
474 	/* Out of range relative negative offset, - 32 MB + 4*/
475 	err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
476 	check(err);
477 
478 	/* Out of range relative positive offset, + 32 MB */
479 	err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
480 	check(err);
481 
482 	/* Unaligned target */
483 	err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
484 	check(err);
485 
486 	/* Check flags are masked correctly */
487 	err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
488 	check(instr_is_branch_to_addr(&instr, addr));
489 	check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
490 }
491 
492 static void __init test_create_function_call(void)
493 {
494 	struct ppc_inst *iptr;
495 	unsigned long dest;
496 	struct ppc_inst instr;
497 
498 	/* Check we can create a function call */
499 	iptr = (struct ppc_inst *)ppc_function_entry(test_trampoline);
500 	dest = ppc_function_entry(test_create_function_call);
501 	create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
502 	patch_instruction(iptr, instr);
503 	check(instr_is_branch_to_addr(iptr, dest));
504 }
505 
506 static void __init test_branch_bform(void)
507 {
508 	int err;
509 	unsigned long addr;
510 	struct ppc_inst *iptr, instr;
511 	unsigned int flags;
512 
513 	iptr = &instr;
514 	addr = (unsigned long)iptr;
515 
516 	/* The simplest case, branch to self, no flags */
517 	check(instr_is_branch_bform(ppc_inst(0x40000000)));
518 	/* All bits of target set, and flags */
519 	check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
520 	/* High bit of opcode set, which is wrong */
521 	check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
522 	/* Middle bits of opcode set, which is wrong */
523 	check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
524 
525 	/* Absolute conditional branch to 0x100 */
526 	instr = ppc_inst(0x43ff0103);
527 	check(instr_is_branch_to_addr(&instr, 0x100));
528 	/* Absolute conditional branch to 0x20fc */
529 	instr = ppc_inst(0x43ff20ff);
530 	check(instr_is_branch_to_addr(&instr, 0x20fc));
531 	/* Maximum positive relative conditional branch, + 32 KB - 4B */
532 	instr = ppc_inst(0x43ff7ffc);
533 	check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
534 	/* Smallest negative relative conditional branch, - 4B */
535 	instr = ppc_inst(0x43fffffc);
536 	check(instr_is_branch_to_addr(&instr, addr - 4));
537 	/* Largest negative relative conditional branch, - 32 KB */
538 	instr = ppc_inst(0x43ff8000);
539 	check(instr_is_branch_to_addr(&instr, addr - 0x8000));
540 
541 	/* All condition code bits set & link */
542 	flags = 0x3ff000 | BRANCH_SET_LINK;
543 
544 	/* Branch to self */
545 	err = create_cond_branch(&instr, iptr, addr, flags);
546 	check(instr_is_branch_to_addr(&instr, addr));
547 
548 	/* Branch to self - 0x100 */
549 	err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
550 	check(instr_is_branch_to_addr(&instr, addr - 0x100));
551 
552 	/* Branch to self + 0x100 */
553 	err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
554 	check(instr_is_branch_to_addr(&instr, addr + 0x100));
555 
556 	/* Maximum relative negative offset, - 32 KB */
557 	err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
558 	check(instr_is_branch_to_addr(&instr, addr - 0x8000));
559 
560 	/* Out of range relative negative offset, - 32 KB + 4*/
561 	err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
562 	check(err);
563 
564 	/* Out of range relative positive offset, + 32 KB */
565 	err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
566 	check(err);
567 
568 	/* Unaligned target */
569 	err = create_cond_branch(&instr, iptr, addr + 3, flags);
570 	check(err);
571 
572 	/* Check flags are masked correctly */
573 	err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
574 	check(instr_is_branch_to_addr(&instr, addr));
575 	check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
576 }
577 
578 static void __init test_translate_branch(void)
579 {
580 	unsigned long addr;
581 	void *p, *q;
582 	struct ppc_inst instr;
583 	void *buf;
584 
585 	buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
586 	check(buf);
587 	if (!buf)
588 		return;
589 
590 	/* Simple case, branch to self moved a little */
591 	p = buf;
592 	addr = (unsigned long)p;
593 	patch_branch(p, addr, 0);
594 	check(instr_is_branch_to_addr(p, addr));
595 	q = p + 4;
596 	translate_branch(&instr, q, p);
597 	patch_instruction(q, instr);
598 	check(instr_is_branch_to_addr(q, addr));
599 
600 	/* Maximum negative case, move b . to addr + 32 MB */
601 	p = buf;
602 	addr = (unsigned long)p;
603 	patch_branch(p, addr, 0);
604 	q = buf + 0x2000000;
605 	translate_branch(&instr, q, p);
606 	patch_instruction(q, instr);
607 	check(instr_is_branch_to_addr(p, addr));
608 	check(instr_is_branch_to_addr(q, addr));
609 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
610 
611 	/* Maximum positive case, move x to x - 32 MB + 4 */
612 	p = buf + 0x2000000;
613 	addr = (unsigned long)p;
614 	patch_branch(p, addr, 0);
615 	q = buf + 4;
616 	translate_branch(&instr, q, p);
617 	patch_instruction(q, instr);
618 	check(instr_is_branch_to_addr(p, addr));
619 	check(instr_is_branch_to_addr(q, addr));
620 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
621 
622 	/* Jump to x + 16 MB moved to x + 20 MB */
623 	p = buf;
624 	addr = 0x1000000 + (unsigned long)buf;
625 	patch_branch(p, addr, BRANCH_SET_LINK);
626 	q = buf + 0x1400000;
627 	translate_branch(&instr, q, p);
628 	patch_instruction(q, instr);
629 	check(instr_is_branch_to_addr(p, addr));
630 	check(instr_is_branch_to_addr(q, addr));
631 
632 	/* Jump to x + 16 MB moved to x - 16 MB + 4 */
633 	p = buf + 0x1000000;
634 	addr = 0x2000000 + (unsigned long)buf;
635 	patch_branch(p, addr, 0);
636 	q = buf + 4;
637 	translate_branch(&instr, q, p);
638 	patch_instruction(q, instr);
639 	check(instr_is_branch_to_addr(p, addr));
640 	check(instr_is_branch_to_addr(q, addr));
641 
642 
643 	/* Conditional branch tests */
644 
645 	/* Simple case, branch to self moved a little */
646 	p = buf;
647 	addr = (unsigned long)p;
648 	create_cond_branch(&instr, p, addr, 0);
649 	patch_instruction(p, instr);
650 	check(instr_is_branch_to_addr(p, addr));
651 	q = buf + 4;
652 	translate_branch(&instr, q, p);
653 	patch_instruction(q, instr);
654 	check(instr_is_branch_to_addr(q, addr));
655 
656 	/* Maximum negative case, move b . to addr + 32 KB */
657 	p = buf;
658 	addr = (unsigned long)p;
659 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
660 	patch_instruction(p, instr);
661 	q = buf + 0x8000;
662 	translate_branch(&instr, q, p);
663 	patch_instruction(q, instr);
664 	check(instr_is_branch_to_addr(p, addr));
665 	check(instr_is_branch_to_addr(q, addr));
666 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
667 
668 	/* Maximum positive case, move x to x - 32 KB + 4 */
669 	p = buf + 0x8000;
670 	addr = (unsigned long)p;
671 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
672 	patch_instruction(p, instr);
673 	q = buf + 4;
674 	translate_branch(&instr, q, p);
675 	patch_instruction(q, instr);
676 	check(instr_is_branch_to_addr(p, addr));
677 	check(instr_is_branch_to_addr(q, addr));
678 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
679 
680 	/* Jump to x + 12 KB moved to x + 20 KB */
681 	p = buf;
682 	addr = 0x3000 + (unsigned long)buf;
683 	create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
684 	patch_instruction(p, instr);
685 	q = buf + 0x5000;
686 	translate_branch(&instr, q, p);
687 	patch_instruction(q, instr);
688 	check(instr_is_branch_to_addr(p, addr));
689 	check(instr_is_branch_to_addr(q, addr));
690 
691 	/* Jump to x + 8 KB moved to x - 8 KB + 4 */
692 	p = buf + 0x2000;
693 	addr = 0x4000 + (unsigned long)buf;
694 	create_cond_branch(&instr, p, addr, 0);
695 	patch_instruction(p, instr);
696 	q = buf + 4;
697 	translate_branch(&instr, q, p);
698 	patch_instruction(q, instr);
699 	check(instr_is_branch_to_addr(p, addr));
700 	check(instr_is_branch_to_addr(q, addr));
701 
702 	/* Free the buffer we were using */
703 	vfree(buf);
704 }
705 
706 #ifdef CONFIG_PPC64
707 static void __init test_prefixed_patching(void)
708 {
709 	extern unsigned int code_patching_test1[];
710 	extern unsigned int code_patching_test1_expected[];
711 	extern unsigned int end_code_patching_test1[];
712 
713 	__patch_instruction((struct ppc_inst *)code_patching_test1,
714 			    ppc_inst_prefix(OP_PREFIX << 26, 0x00000000),
715 			    (struct ppc_inst *)code_patching_test1);
716 
717 	check(!memcmp(code_patching_test1,
718 		      code_patching_test1_expected,
719 		      sizeof(unsigned int) *
720 		      (end_code_patching_test1 - code_patching_test1)));
721 }
722 #else
723 static inline void test_prefixed_patching(void) {}
724 #endif
725 
726 static int __init test_code_patching(void)
727 {
728 	printk(KERN_DEBUG "Running code patching self-tests ...\n");
729 
730 	test_branch_iform();
731 	test_branch_bform();
732 	test_create_function_call();
733 	test_translate_branch();
734 	test_prefixed_patching();
735 
736 	return 0;
737 }
738 late_initcall(test_code_patching);
739 
740 #endif /* CONFIG_CODE_PATCHING_SELFTEST */
741