GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / powerpc / lib / code-patching.c
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),
102                                 pgprot_val(PAGE_KERNEL));
103
104         pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
105         if (err)
106                 return -1;
107
108         return 0;
109 }
110
111 static inline int unmap_patch_area(unsigned long addr)
112 {
113         pte_t *ptep;
114         pmd_t *pmdp;
115         pud_t *pudp;
116         pgd_t *pgdp;
117
118         pgdp = pgd_offset_k(addr);
119         if (unlikely(!pgdp))
120                 return -EINVAL;
121
122         pudp = pud_offset(pgdp, addr);
123         if (unlikely(!pudp))
124                 return -EINVAL;
125
126         pmdp = pmd_offset(pudp, addr);
127         if (unlikely(!pmdp))
128                 return -EINVAL;
129
130         ptep = pte_offset_kernel(pmdp, addr);
131         if (unlikely(!ptep))
132                 return -EINVAL;
133
134         pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
135
136         /*
137          * In hash, pte_clear flushes the tlb, in radix, we have to
138          */
139         pte_clear(&init_mm, addr, ptep);
140         flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
141
142         return 0;
143 }
144
145 static int do_patch_instruction(unsigned int *addr, unsigned int instr)
146 {
147         int err;
148         unsigned int *patch_addr = NULL;
149         unsigned long flags;
150         unsigned long text_poke_addr;
151         unsigned long kaddr = (unsigned long)addr;
152
153         /*
154          * During early early boot patch_instruction is called
155          * when text_poke_area is not ready, but we still need
156          * to allow patching. We just do the plain old patching
157          */
158         if (!this_cpu_read(text_poke_area))
159                 return raw_patch_instruction(addr, instr);
160
161         local_irq_save(flags);
162
163         text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
164         if (map_patch_area(addr, text_poke_addr)) {
165                 err = -1;
166                 goto out;
167         }
168
169         patch_addr = (unsigned int *)(text_poke_addr) +
170                         ((kaddr & ~PAGE_MASK) / sizeof(unsigned int));
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(unsigned int *addr, unsigned int instr)
186 {
187         return raw_patch_instruction(addr, instr);
188 }
189
190 #endif /* CONFIG_STRICT_KERNEL_RWX */
191
192 int patch_instruction(unsigned int *addr, unsigned int 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(unsigned int *addr, unsigned long target, int flags)
204 {
205         return patch_instruction(addr, create_branch(addr, target, flags));
206 }
207
208 int patch_branch_site(s32 *site, unsigned long target, int flags)
209 {
210         unsigned int *addr;
211
212         addr = (unsigned int *)((unsigned long)site + *site);
213         return patch_instruction(addr, create_branch(addr, target, flags));
214 }
215
216 int patch_instruction_site(s32 *site, unsigned int instr)
217 {
218         unsigned int *addr;
219
220         addr = (unsigned int *)((unsigned long)site + *site);
221         return patch_instruction(addr, instr);
222 }
223
224 bool is_offset_in_branch_range(long offset)
225 {
226         /*
227          * Powerpc branch instruction is :
228          *
229          *  0         6                 30   31
230          *  +---------+----------------+---+---+
231          *  | opcode  |     LI         |AA |LK |
232          *  +---------+----------------+---+---+
233          *  Where AA = 0 and LK = 0
234          *
235          * LI is a signed 24 bits integer. The real branch offset is computed
236          * by: imm32 = SignExtend(LI:'0b00', 32);
237          *
238          * So the maximum forward branch should be:
239          *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
240          * The maximum backward branch should be:
241          *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
242          */
243         return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
244 }
245
246 bool is_offset_in_cond_branch_range(long offset)
247 {
248         return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
249 }
250
251 /*
252  * Helper to check if a given instruction is a conditional branch
253  * Derived from the conditional checks in analyse_instr()
254  */
255 bool is_conditional_branch(unsigned int instr)
256 {
257         unsigned int opcode = instr >> 26;
258
259         if (opcode == 16)       /* bc, bca, bcl, bcla */
260                 return true;
261         if (opcode == 19) {
262                 switch ((instr >> 1) & 0x3ff) {
263                 case 16:        /* bclr, bclrl */
264                 case 528:       /* bcctr, bcctrl */
265                 case 560:       /* bctar, bctarl */
266                         return true;
267                 }
268         }
269         return false;
270 }
271 NOKPROBE_SYMBOL(is_conditional_branch);
272
273 unsigned int create_branch(const unsigned int *addr,
274                            unsigned long target, int flags)
275 {
276         unsigned int instruction;
277         long offset;
278
279         offset = target;
280         if (! (flags & BRANCH_ABSOLUTE))
281                 offset = offset - (unsigned long)addr;
282
283         /* Check we can represent the target in the instruction format */
284         if (!is_offset_in_branch_range(offset))
285                 return 0;
286
287         /* Mask out the flags and target, so they don't step on each other. */
288         instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
289
290         return instruction;
291 }
292
293 unsigned int create_cond_branch(const unsigned int *addr,
294                                 unsigned long target, int flags)
295 {
296         unsigned int instruction;
297         long offset;
298
299         offset = target;
300         if (! (flags & BRANCH_ABSOLUTE))
301                 offset = offset - (unsigned long)addr;
302
303         /* Check we can represent the target in the instruction format */
304         if (!is_offset_in_cond_branch_range(offset))
305                 return 0;
306
307         /* Mask out the flags and target, so they don't step on each other. */
308         instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
309
310         return instruction;
311 }
312
313 static unsigned int branch_opcode(unsigned int instr)
314 {
315         return (instr >> 26) & 0x3F;
316 }
317
318 static int instr_is_branch_iform(unsigned int instr)
319 {
320         return branch_opcode(instr) == 18;
321 }
322
323 static int instr_is_branch_bform(unsigned int instr)
324 {
325         return branch_opcode(instr) == 16;
326 }
327
328 int instr_is_relative_branch(unsigned int instr)
329 {
330         if (instr & BRANCH_ABSOLUTE)
331                 return 0;
332
333         return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
334 }
335
336 int instr_is_relative_link_branch(unsigned int instr)
337 {
338         return instr_is_relative_branch(instr) && (instr & BRANCH_SET_LINK);
339 }
340
341 static unsigned long branch_iform_target(const unsigned int *instr)
342 {
343         signed long imm;
344
345         imm = *instr & 0x3FFFFFC;
346
347         /* If the top bit of the immediate value is set this is negative */
348         if (imm & 0x2000000)
349                 imm -= 0x4000000;
350
351         if ((*instr & BRANCH_ABSOLUTE) == 0)
352                 imm += (unsigned long)instr;
353
354         return (unsigned long)imm;
355 }
356
357 static unsigned long branch_bform_target(const unsigned int *instr)
358 {
359         signed long imm;
360
361         imm = *instr & 0xFFFC;
362
363         /* If the top bit of the immediate value is set this is negative */
364         if (imm & 0x8000)
365                 imm -= 0x10000;
366
367         if ((*instr & BRANCH_ABSOLUTE) == 0)
368                 imm += (unsigned long)instr;
369
370         return (unsigned long)imm;
371 }
372
373 unsigned long branch_target(const unsigned int *instr)
374 {
375         if (instr_is_branch_iform(*instr))
376                 return branch_iform_target(instr);
377         else if (instr_is_branch_bform(*instr))
378                 return branch_bform_target(instr);
379
380         return 0;
381 }
382
383 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
384 {
385         if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
386                 return branch_target(instr) == addr;
387
388         return 0;
389 }
390
391 unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
392 {
393         unsigned long target;
394
395         target = branch_target(src);
396
397         if (instr_is_branch_iform(*src))
398                 return create_branch(dest, target, *src);
399         else if (instr_is_branch_bform(*src))
400                 return create_cond_branch(dest, target, *src);
401
402         return 0;
403 }
404
405 #ifdef CONFIG_PPC_BOOK3E_64
406 void __patch_exception(int exc, unsigned long addr)
407 {
408         extern unsigned int interrupt_base_book3e;
409         unsigned int *ibase = &interrupt_base_book3e;
410
411         /* Our exceptions vectors start with a NOP and -then- a branch
412          * to deal with single stepping from userspace which stops on
413          * the second instruction. Thus we need to patch the second
414          * instruction of the exception, not the first one
415          */
416
417         patch_branch(ibase + (exc / 4) + 1, addr, 0);
418 }
419 #endif
420
421 #ifdef CONFIG_CODE_PATCHING_SELFTEST
422
423 static void __init test_trampoline(void)
424 {
425         asm ("nop;\n");
426 }
427
428 #define check(x)        \
429         if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
430
431 static void __init test_branch_iform(void)
432 {
433         unsigned int instr;
434         unsigned long addr;
435
436         addr = (unsigned long)&instr;
437
438         /* The simplest case, branch to self, no flags */
439         check(instr_is_branch_iform(0x48000000));
440         /* All bits of target set, and flags */
441         check(instr_is_branch_iform(0x4bffffff));
442         /* High bit of opcode set, which is wrong */
443         check(!instr_is_branch_iform(0xcbffffff));
444         /* Middle bits of opcode set, which is wrong */
445         check(!instr_is_branch_iform(0x7bffffff));
446
447         /* Simplest case, branch to self with link */
448         check(instr_is_branch_iform(0x48000001));
449         /* All bits of targets set */
450         check(instr_is_branch_iform(0x4bfffffd));
451         /* Some bits of targets set */
452         check(instr_is_branch_iform(0x4bff00fd));
453         /* Must be a valid branch to start with */
454         check(!instr_is_branch_iform(0x7bfffffd));
455
456         /* Absolute branch to 0x100 */
457         instr = 0x48000103;
458         check(instr_is_branch_to_addr(&instr, 0x100));
459         /* Absolute branch to 0x420fc */
460         instr = 0x480420ff;
461         check(instr_is_branch_to_addr(&instr, 0x420fc));
462         /* Maximum positive relative branch, + 20MB - 4B */
463         instr = 0x49fffffc;
464         check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
465         /* Smallest negative relative branch, - 4B */
466         instr = 0x4bfffffc;
467         check(instr_is_branch_to_addr(&instr, addr - 4));
468         /* Largest negative relative branch, - 32 MB */
469         instr = 0x4a000000;
470         check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
471
472         /* Branch to self, with link */
473         instr = create_branch(&instr, addr, BRANCH_SET_LINK);
474         check(instr_is_branch_to_addr(&instr, addr));
475
476         /* Branch to self - 0x100, with link */
477         instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
478         check(instr_is_branch_to_addr(&instr, addr - 0x100));
479
480         /* Branch to self + 0x100, no link */
481         instr = create_branch(&instr, addr + 0x100, 0);
482         check(instr_is_branch_to_addr(&instr, addr + 0x100));
483
484         /* Maximum relative negative offset, - 32 MB */
485         instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
486         check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
487
488         /* Out of range relative negative offset, - 32 MB + 4*/
489         instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
490         check(instr == 0);
491
492         /* Out of range relative positive offset, + 32 MB */
493         instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
494         check(instr == 0);
495
496         /* Unaligned target */
497         instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
498         check(instr == 0);
499
500         /* Check flags are masked correctly */
501         instr = create_branch(&instr, addr, 0xFFFFFFFC);
502         check(instr_is_branch_to_addr(&instr, addr));
503         check(instr == 0x48000000);
504 }
505
506 static void __init test_create_function_call(void)
507 {
508         unsigned int *iptr;
509         unsigned long dest;
510
511         /* Check we can create a function call */
512         iptr = (unsigned int *)ppc_function_entry(test_trampoline);
513         dest = ppc_function_entry(test_create_function_call);
514         patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
515         check(instr_is_branch_to_addr(iptr, dest));
516 }
517
518 static void __init test_branch_bform(void)
519 {
520         unsigned long addr;
521         unsigned int *iptr, instr, flags;
522
523         iptr = &instr;
524         addr = (unsigned long)iptr;
525
526         /* The simplest case, branch to self, no flags */
527         check(instr_is_branch_bform(0x40000000));
528         /* All bits of target set, and flags */
529         check(instr_is_branch_bform(0x43ffffff));
530         /* High bit of opcode set, which is wrong */
531         check(!instr_is_branch_bform(0xc3ffffff));
532         /* Middle bits of opcode set, which is wrong */
533         check(!instr_is_branch_bform(0x7bffffff));
534
535         /* Absolute conditional branch to 0x100 */
536         instr = 0x43ff0103;
537         check(instr_is_branch_to_addr(&instr, 0x100));
538         /* Absolute conditional branch to 0x20fc */
539         instr = 0x43ff20ff;
540         check(instr_is_branch_to_addr(&instr, 0x20fc));
541         /* Maximum positive relative conditional branch, + 32 KB - 4B */
542         instr = 0x43ff7ffc;
543         check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
544         /* Smallest negative relative conditional branch, - 4B */
545         instr = 0x43fffffc;
546         check(instr_is_branch_to_addr(&instr, addr - 4));
547         /* Largest negative relative conditional branch, - 32 KB */
548         instr = 0x43ff8000;
549         check(instr_is_branch_to_addr(&instr, addr - 0x8000));
550
551         /* All condition code bits set & link */
552         flags = 0x3ff000 | BRANCH_SET_LINK;
553
554         /* Branch to self */
555         instr = create_cond_branch(iptr, addr, flags);
556         check(instr_is_branch_to_addr(&instr, addr));
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         /* Branch to self + 0x100 */
563         instr = create_cond_branch(iptr, addr + 0x100, flags);
564         check(instr_is_branch_to_addr(&instr, addr + 0x100));
565
566         /* Maximum relative negative offset, - 32 KB */
567         instr = create_cond_branch(iptr, addr - 0x8000, flags);
568         check(instr_is_branch_to_addr(&instr, addr - 0x8000));
569
570         /* Out of range relative negative offset, - 32 KB + 4*/
571         instr = create_cond_branch(iptr, addr - 0x8004, flags);
572         check(instr == 0);
573
574         /* Out of range relative positive offset, + 32 KB */
575         instr = create_cond_branch(iptr, addr + 0x8000, flags);
576         check(instr == 0);
577
578         /* Unaligned target */
579         instr = create_cond_branch(iptr, addr + 3, flags);
580         check(instr == 0);
581
582         /* Check flags are masked correctly */
583         instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
584         check(instr_is_branch_to_addr(&instr, addr));
585         check(instr == 0x43FF0000);
586 }
587
588 static void __init test_translate_branch(void)
589 {
590         unsigned long addr;
591         unsigned int *p, *q;
592         void *buf;
593
594         buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
595         check(buf);
596         if (!buf)
597                 return;
598
599         /* Simple case, branch to self moved a little */
600         p = buf;
601         addr = (unsigned long)p;
602         patch_branch(p, addr, 0);
603         check(instr_is_branch_to_addr(p, addr));
604         q = p + 1;
605         patch_instruction(q, translate_branch(q, p));
606         check(instr_is_branch_to_addr(q, addr));
607
608         /* Maximum negative case, move b . to addr + 32 MB */
609         p = buf;
610         addr = (unsigned long)p;
611         patch_branch(p, addr, 0);
612         q = buf + 0x2000000;
613         patch_instruction(q, translate_branch(q, p));
614         check(instr_is_branch_to_addr(p, addr));
615         check(instr_is_branch_to_addr(q, addr));
616         check(*q == 0x4a000000);
617
618         /* Maximum positive case, move x to x - 32 MB + 4 */
619         p = buf + 0x2000000;
620         addr = (unsigned long)p;
621         patch_branch(p, addr, 0);
622         q = buf + 4;
623         patch_instruction(q, translate_branch(q, p));
624         check(instr_is_branch_to_addr(p, addr));
625         check(instr_is_branch_to_addr(q, addr));
626         check(*q == 0x49fffffc);
627
628         /* Jump to x + 16 MB moved to x + 20 MB */
629         p = buf;
630         addr = 0x1000000 + (unsigned long)buf;
631         patch_branch(p, addr, BRANCH_SET_LINK);
632         q = buf + 0x1400000;
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         /* Jump to x + 16 MB moved to x - 16 MB + 4 */
638         p = buf + 0x1000000;
639         addr = 0x2000000 + (unsigned long)buf;
640         patch_branch(p, addr, 0);
641         q = buf + 4;
642         patch_instruction(q, translate_branch(q, p));
643         check(instr_is_branch_to_addr(p, addr));
644         check(instr_is_branch_to_addr(q, addr));
645
646
647         /* Conditional branch tests */
648
649         /* Simple case, branch to self moved a little */
650         p = buf;
651         addr = (unsigned long)p;
652         patch_instruction(p, create_cond_branch(p, addr, 0));
653         check(instr_is_branch_to_addr(p, addr));
654         q = p + 1;
655         patch_instruction(q, translate_branch(q, p));
656         check(instr_is_branch_to_addr(q, addr));
657
658         /* Maximum negative case, move b . to addr + 32 KB */
659         p = buf;
660         addr = (unsigned long)p;
661         patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
662         q = buf + 0x8000;
663         patch_instruction(q, translate_branch(q, p));
664         check(instr_is_branch_to_addr(p, addr));
665         check(instr_is_branch_to_addr(q, addr));
666         check(*q == 0x43ff8000);
667
668         /* Maximum positive case, move x to x - 32 KB + 4 */
669         p = buf + 0x8000;
670         addr = (unsigned long)p;
671         patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
672         q = buf + 4;
673         patch_instruction(q, translate_branch(q, p));
674         check(instr_is_branch_to_addr(p, addr));
675         check(instr_is_branch_to_addr(q, addr));
676         check(*q == 0x43ff7ffc);
677
678         /* Jump to x + 12 KB moved to x + 20 KB */
679         p = buf;
680         addr = 0x3000 + (unsigned long)buf;
681         patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
682         q = buf + 0x5000;
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         /* Jump to x + 8 KB moved to x - 8 KB + 4 */
688         p = buf + 0x2000;
689         addr = 0x4000 + (unsigned long)buf;
690         patch_instruction(p, create_cond_branch(p, addr, 0));
691         q = buf + 4;
692         patch_instruction(q, translate_branch(q, p));
693         check(instr_is_branch_to_addr(p, addr));
694         check(instr_is_branch_to_addr(q, addr));
695
696         /* Free the buffer we were using */
697         vfree(buf);
698 }
699
700 static int __init test_code_patching(void)
701 {
702         printk(KERN_DEBUG "Running code patching self-tests ...\n");
703
704         test_branch_iform();
705         test_branch_bform();
706         test_create_function_call();
707         test_translate_branch();
708
709         return 0;
710 }
711 late_initcall(test_code_patching);
712
713 #endif /* CONFIG_CODE_PATCHING_SELFTEST */