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