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