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