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