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