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