blob: a2e4f864b63d2ea1c4e10c185849b4e06350bc39 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright 2008 Michael Ellerman, IBM Corporation.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015#include <asm/tlbflush.h>
16#include <asm/page.h>
17#include <asm/code-patching.h>
18#include <asm/setup.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020019#include <asm/inst.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
Olivier Deprez157378f2022-04-04 15:47:50 +020021static int __patch_instruction(struct ppc_inst *exec_addr, struct ppc_inst instr,
22 struct ppc_inst *patch_addr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023{
Olivier Deprez157378f2022-04-04 15:47:50 +020024 if (!ppc_inst_prefixed(instr))
25 __put_user_asm_goto(ppc_inst_val(instr), patch_addr, failed, "stw");
26 else
27 __put_user_asm_goto(ppc_inst_as_u64(instr), patch_addr, failed, "std");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028
29 asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
30 "r" (exec_addr));
31
32 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +020033
34failed:
35 return -EFAULT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036}
37
Olivier Deprez157378f2022-04-04 15:47:50 +020038int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039{
40 return __patch_instruction(addr, instr, addr);
41}
42
43#ifdef CONFIG_STRICT_KERNEL_RWX
44static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
45
46static int text_area_cpu_up(unsigned int cpu)
47{
48 struct vm_struct *area;
49
50 area = get_vm_area(PAGE_SIZE, VM_ALLOC);
51 if (!area) {
52 WARN_ONCE(1, "Failed to create text area for cpu %d\n",
53 cpu);
54 return -1;
55 }
56 this_cpu_write(text_poke_area, area);
57
58 return 0;
59}
60
61static int text_area_cpu_down(unsigned int cpu)
62{
63 free_vm_area(this_cpu_read(text_poke_area));
64 return 0;
65}
66
67/*
68 * Run as a late init call. This allows all the boot time patching to be done
69 * simply by patching the code, and then we're called here prior to
70 * mark_rodata_ro(), which happens after all init calls are run. Although
71 * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
72 * it as being preferable to a kernel that will crash later when someone tries
73 * to use patch_instruction().
74 */
75static int __init setup_text_poke_area(void)
76{
77 BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
78 "powerpc/text_poke:online", text_area_cpu_up,
79 text_area_cpu_down));
80
81 return 0;
82}
83late_initcall(setup_text_poke_area);
84
85/*
86 * This can be called for kernel text or a module.
87 */
88static int map_patch_area(void *addr, unsigned long text_poke_addr)
89{
90 unsigned long pfn;
91 int err;
92
Olivier Deprez157378f2022-04-04 15:47:50 +020093 if (is_vmalloc_or_module_addr(addr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 pfn = vmalloc_to_pfn(addr);
95 else
96 pfn = __pa_symbol(addr) >> PAGE_SHIFT;
97
David Brazdil0f672f62019-12-10 10:32:29 +000098 err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099
100 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
101 if (err)
102 return -1;
103
104 return 0;
105}
106
107static inline int unmap_patch_area(unsigned long addr)
108{
109 pte_t *ptep;
110 pmd_t *pmdp;
111 pud_t *pudp;
Olivier Deprez157378f2022-04-04 15:47:50 +0200112 p4d_t *p4dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113 pgd_t *pgdp;
114
115 pgdp = pgd_offset_k(addr);
116 if (unlikely(!pgdp))
117 return -EINVAL;
118
Olivier Deprez157378f2022-04-04 15:47:50 +0200119 p4dp = p4d_offset(pgdp, addr);
120 if (unlikely(!p4dp))
121 return -EINVAL;
122
123 pudp = pud_offset(p4dp, addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200146static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147{
148 int err;
Olivier Deprez157378f2022-04-04 15:47:50 +0200149 struct ppc_inst *patch_addr = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200170 patch_addr = (struct ppc_inst *)(text_poke_addr + (kaddr & ~PAGE_MASK));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171
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
178out:
179 local_irq_restore(flags);
180
181 return err;
182}
183#else /* !CONFIG_STRICT_KERNEL_RWX */
184
Olivier Deprez157378f2022-04-04 15:47:50 +0200185static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000186{
187 return raw_patch_instruction(addr, instr);
188}
189
190#endif /* CONFIG_STRICT_KERNEL_RWX */
191
Olivier Deprez157378f2022-04-04 15:47:50 +0200192int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193{
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}
201NOKPROBE_SYMBOL(patch_instruction);
202
Olivier Deprez157378f2022-04-04 15:47:50 +0200203int patch_branch(struct ppc_inst *addr, unsigned long target, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204{
Olivier Deprez157378f2022-04-04 15:47:50 +0200205 struct ppc_inst instr;
206
207 create_branch(&instr, addr, target, flags);
208 return patch_instruction(addr, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000209}
210
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000211bool is_offset_in_branch_range(long offset)
212{
213 /*
214 * Powerpc branch instruction is :
215 *
216 * 0 6 30 31
217 * +---------+----------------+---+---+
218 * | opcode | LI |AA |LK |
219 * +---------+----------------+---+---+
220 * Where AA = 0 and LK = 0
221 *
222 * LI is a signed 24 bits integer. The real branch offset is computed
223 * by: imm32 = SignExtend(LI:'0b00', 32);
224 *
225 * So the maximum forward branch should be:
226 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
227 * The maximum backward branch should be:
228 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
229 */
230 return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
231}
232
Olivier Deprez157378f2022-04-04 15:47:50 +0200233bool is_offset_in_cond_branch_range(long offset)
234{
235 return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
236}
237
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238/*
239 * Helper to check if a given instruction is a conditional branch
240 * Derived from the conditional checks in analyse_instr()
241 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200242bool is_conditional_branch(struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000243{
Olivier Deprez157378f2022-04-04 15:47:50 +0200244 unsigned int opcode = ppc_inst_primary_opcode(instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000245
246 if (opcode == 16) /* bc, bca, bcl, bcla */
247 return true;
248 if (opcode == 19) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200249 switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250 case 16: /* bclr, bclrl */
251 case 528: /* bcctr, bcctrl */
252 case 560: /* bctar, bctarl */
253 return true;
254 }
255 }
256 return false;
257}
258NOKPROBE_SYMBOL(is_conditional_branch);
259
Olivier Deprez157378f2022-04-04 15:47:50 +0200260int create_branch(struct ppc_inst *instr,
261 const struct ppc_inst *addr,
262 unsigned long target, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000264 long offset;
265
Olivier Deprez157378f2022-04-04 15:47:50 +0200266 *instr = ppc_inst(0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 offset = target;
268 if (! (flags & BRANCH_ABSOLUTE))
269 offset = offset - (unsigned long)addr;
270
271 /* Check we can represent the target in the instruction format */
272 if (!is_offset_in_branch_range(offset))
Olivier Deprez157378f2022-04-04 15:47:50 +0200273 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274
275 /* Mask out the flags and target, so they don't step on each other. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200276 *instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000277
Olivier Deprez157378f2022-04-04 15:47:50 +0200278 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279}
280
Olivier Deprez157378f2022-04-04 15:47:50 +0200281int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
282 unsigned long target, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000284 long offset;
285
286 offset = target;
287 if (! (flags & BRANCH_ABSOLUTE))
288 offset = offset - (unsigned long)addr;
289
290 /* Check we can represent the target in the instruction format */
Olivier Deprez157378f2022-04-04 15:47:50 +0200291 if (!is_offset_in_cond_branch_range(offset))
292 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000293
294 /* Mask out the flags and target, so they don't step on each other. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200295 *instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000296
Olivier Deprez157378f2022-04-04 15:47:50 +0200297 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298}
299
Olivier Deprez157378f2022-04-04 15:47:50 +0200300static unsigned int branch_opcode(struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000301{
Olivier Deprez157378f2022-04-04 15:47:50 +0200302 return ppc_inst_primary_opcode(instr) & 0x3F;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303}
304
Olivier Deprez157378f2022-04-04 15:47:50 +0200305static int instr_is_branch_iform(struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306{
307 return branch_opcode(instr) == 18;
308}
309
Olivier Deprez157378f2022-04-04 15:47:50 +0200310static int instr_is_branch_bform(struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311{
312 return branch_opcode(instr) == 16;
313}
314
Olivier Deprez157378f2022-04-04 15:47:50 +0200315int instr_is_relative_branch(struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316{
Olivier Deprez157378f2022-04-04 15:47:50 +0200317 if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000318 return 0;
319
320 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
321}
322
Olivier Deprez157378f2022-04-04 15:47:50 +0200323int instr_is_relative_link_branch(struct ppc_inst instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000324{
Olivier Deprez157378f2022-04-04 15:47:50 +0200325 return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326}
327
Olivier Deprez157378f2022-04-04 15:47:50 +0200328static unsigned long branch_iform_target(const struct ppc_inst *instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329{
330 signed long imm;
331
Olivier Deprez157378f2022-04-04 15:47:50 +0200332 imm = ppc_inst_val(*instr) & 0x3FFFFFC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000333
334 /* If the top bit of the immediate value is set this is negative */
335 if (imm & 0x2000000)
336 imm -= 0x4000000;
337
Olivier Deprez157378f2022-04-04 15:47:50 +0200338 if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 imm += (unsigned long)instr;
340
341 return (unsigned long)imm;
342}
343
Olivier Deprez157378f2022-04-04 15:47:50 +0200344static unsigned long branch_bform_target(const struct ppc_inst *instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000345{
346 signed long imm;
347
Olivier Deprez157378f2022-04-04 15:47:50 +0200348 imm = ppc_inst_val(*instr) & 0xFFFC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349
350 /* If the top bit of the immediate value is set this is negative */
351 if (imm & 0x8000)
352 imm -= 0x10000;
353
Olivier Deprez157378f2022-04-04 15:47:50 +0200354 if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000355 imm += (unsigned long)instr;
356
357 return (unsigned long)imm;
358}
359
Olivier Deprez157378f2022-04-04 15:47:50 +0200360unsigned long branch_target(const struct ppc_inst *instr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361{
Olivier Deprez157378f2022-04-04 15:47:50 +0200362 if (instr_is_branch_iform(ppc_inst_read(instr)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000363 return branch_iform_target(instr);
Olivier Deprez157378f2022-04-04 15:47:50 +0200364 else if (instr_is_branch_bform(ppc_inst_read(instr)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000365 return branch_bform_target(instr);
366
367 return 0;
368}
369
Olivier Deprez157378f2022-04-04 15:47:50 +0200370int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000371{
Olivier Deprez157378f2022-04-04 15:47:50 +0200372 if (instr_is_branch_iform(ppc_inst_read(instr)) ||
373 instr_is_branch_bform(ppc_inst_read(instr)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374 return branch_target(instr) == addr;
375
376 return 0;
377}
378
Olivier Deprez157378f2022-04-04 15:47:50 +0200379int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
380 const struct ppc_inst *src)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000381{
382 unsigned long target;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000383 target = branch_target(src);
384
Olivier Deprez157378f2022-04-04 15:47:50 +0200385 if (instr_is_branch_iform(ppc_inst_read(src)))
386 return create_branch(instr, dest, target,
387 ppc_inst_val(ppc_inst_read(src)));
388 else if (instr_is_branch_bform(ppc_inst_read(src)))
389 return create_cond_branch(instr, dest, target,
390 ppc_inst_val(ppc_inst_read(src)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000391
Olivier Deprez157378f2022-04-04 15:47:50 +0200392 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393}
394
395#ifdef CONFIG_PPC_BOOK3E_64
396void __patch_exception(int exc, unsigned long addr)
397{
398 extern unsigned int interrupt_base_book3e;
399 unsigned int *ibase = &interrupt_base_book3e;
400
401 /* Our exceptions vectors start with a NOP and -then- a branch
402 * to deal with single stepping from userspace which stops on
403 * the second instruction. Thus we need to patch the second
404 * instruction of the exception, not the first one
405 */
406
Olivier Deprez157378f2022-04-04 15:47:50 +0200407 patch_branch((struct ppc_inst *)(ibase + (exc / 4) + 1), addr, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408}
409#endif
410
411#ifdef CONFIG_CODE_PATCHING_SELFTEST
412
413static void __init test_trampoline(void)
414{
415 asm ("nop;\n");
416}
417
418#define check(x) \
419 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
420
421static void __init test_branch_iform(void)
422{
Olivier Deprez157378f2022-04-04 15:47:50 +0200423 int err;
424 struct ppc_inst instr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000425 unsigned long addr;
426
427 addr = (unsigned long)&instr;
428
429 /* The simplest case, branch to self, no flags */
Olivier Deprez157378f2022-04-04 15:47:50 +0200430 check(instr_is_branch_iform(ppc_inst(0x48000000)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000431 /* All bits of target set, and flags */
Olivier Deprez157378f2022-04-04 15:47:50 +0200432 check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000433 /* High bit of opcode set, which is wrong */
Olivier Deprez157378f2022-04-04 15:47:50 +0200434 check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000435 /* Middle bits of opcode set, which is wrong */
Olivier Deprez157378f2022-04-04 15:47:50 +0200436 check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000437
438 /* Simplest case, branch to self with link */
Olivier Deprez157378f2022-04-04 15:47:50 +0200439 check(instr_is_branch_iform(ppc_inst(0x48000001)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000440 /* All bits of targets set */
Olivier Deprez157378f2022-04-04 15:47:50 +0200441 check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442 /* Some bits of targets set */
Olivier Deprez157378f2022-04-04 15:47:50 +0200443 check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000444 /* Must be a valid branch to start with */
Olivier Deprez157378f2022-04-04 15:47:50 +0200445 check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000446
447 /* Absolute branch to 0x100 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200448 instr = ppc_inst(0x48000103);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 check(instr_is_branch_to_addr(&instr, 0x100));
450 /* Absolute branch to 0x420fc */
Olivier Deprez157378f2022-04-04 15:47:50 +0200451 instr = ppc_inst(0x480420ff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000452 check(instr_is_branch_to_addr(&instr, 0x420fc));
453 /* Maximum positive relative branch, + 20MB - 4B */
Olivier Deprez157378f2022-04-04 15:47:50 +0200454 instr = ppc_inst(0x49fffffc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000455 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
456 /* Smallest negative relative branch, - 4B */
Olivier Deprez157378f2022-04-04 15:47:50 +0200457 instr = ppc_inst(0x4bfffffc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000458 check(instr_is_branch_to_addr(&instr, addr - 4));
459 /* Largest negative relative branch, - 32 MB */
Olivier Deprez157378f2022-04-04 15:47:50 +0200460 instr = ppc_inst(0x4a000000);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000461 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
462
463 /* Branch to self, with link */
Olivier Deprez157378f2022-04-04 15:47:50 +0200464 err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000465 check(instr_is_branch_to_addr(&instr, addr));
466
467 /* Branch to self - 0x100, with link */
Olivier Deprez157378f2022-04-04 15:47:50 +0200468 err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000469 check(instr_is_branch_to_addr(&instr, addr - 0x100));
470
471 /* Branch to self + 0x100, no link */
Olivier Deprez157378f2022-04-04 15:47:50 +0200472 err = create_branch(&instr, &instr, addr + 0x100, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473 check(instr_is_branch_to_addr(&instr, addr + 0x100));
474
475 /* Maximum relative negative offset, - 32 MB */
Olivier Deprez157378f2022-04-04 15:47:50 +0200476 err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
478
479 /* Out of range relative negative offset, - 32 MB + 4*/
Olivier Deprez157378f2022-04-04 15:47:50 +0200480 err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
481 check(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482
483 /* Out of range relative positive offset, + 32 MB */
Olivier Deprez157378f2022-04-04 15:47:50 +0200484 err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
485 check(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486
487 /* Unaligned target */
Olivier Deprez157378f2022-04-04 15:47:50 +0200488 err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
489 check(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000490
491 /* Check flags are masked correctly */
Olivier Deprez157378f2022-04-04 15:47:50 +0200492 err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000493 check(instr_is_branch_to_addr(&instr, addr));
Olivier Deprez157378f2022-04-04 15:47:50 +0200494 check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495}
496
497static void __init test_create_function_call(void)
498{
Olivier Deprez157378f2022-04-04 15:47:50 +0200499 struct ppc_inst *iptr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500 unsigned long dest;
Olivier Deprez157378f2022-04-04 15:47:50 +0200501 struct ppc_inst instr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502
503 /* Check we can create a function call */
Olivier Deprez157378f2022-04-04 15:47:50 +0200504 iptr = (struct ppc_inst *)ppc_function_entry(test_trampoline);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505 dest = ppc_function_entry(test_create_function_call);
Olivier Deprez157378f2022-04-04 15:47:50 +0200506 create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
507 patch_instruction(iptr, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000508 check(instr_is_branch_to_addr(iptr, dest));
509}
510
511static void __init test_branch_bform(void)
512{
Olivier Deprez157378f2022-04-04 15:47:50 +0200513 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000514 unsigned long addr;
Olivier Deprez157378f2022-04-04 15:47:50 +0200515 struct ppc_inst *iptr, instr;
516 unsigned int flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517
518 iptr = &instr;
519 addr = (unsigned long)iptr;
520
521 /* The simplest case, branch to self, no flags */
Olivier Deprez157378f2022-04-04 15:47:50 +0200522 check(instr_is_branch_bform(ppc_inst(0x40000000)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 /* All bits of target set, and flags */
Olivier Deprez157378f2022-04-04 15:47:50 +0200524 check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000525 /* High bit of opcode set, which is wrong */
Olivier Deprez157378f2022-04-04 15:47:50 +0200526 check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000527 /* Middle bits of opcode set, which is wrong */
Olivier Deprez157378f2022-04-04 15:47:50 +0200528 check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000529
530 /* Absolute conditional branch to 0x100 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200531 instr = ppc_inst(0x43ff0103);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000532 check(instr_is_branch_to_addr(&instr, 0x100));
533 /* Absolute conditional branch to 0x20fc */
Olivier Deprez157378f2022-04-04 15:47:50 +0200534 instr = ppc_inst(0x43ff20ff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000535 check(instr_is_branch_to_addr(&instr, 0x20fc));
536 /* Maximum positive relative conditional branch, + 32 KB - 4B */
Olivier Deprez157378f2022-04-04 15:47:50 +0200537 instr = ppc_inst(0x43ff7ffc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000538 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
539 /* Smallest negative relative conditional branch, - 4B */
Olivier Deprez157378f2022-04-04 15:47:50 +0200540 instr = ppc_inst(0x43fffffc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000541 check(instr_is_branch_to_addr(&instr, addr - 4));
542 /* Largest negative relative conditional branch, - 32 KB */
Olivier Deprez157378f2022-04-04 15:47:50 +0200543 instr = ppc_inst(0x43ff8000);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000544 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
545
546 /* All condition code bits set & link */
547 flags = 0x3ff000 | BRANCH_SET_LINK;
548
549 /* Branch to self */
Olivier Deprez157378f2022-04-04 15:47:50 +0200550 err = create_cond_branch(&instr, iptr, addr, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551 check(instr_is_branch_to_addr(&instr, addr));
552
553 /* Branch to self - 0x100 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200554 err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000555 check(instr_is_branch_to_addr(&instr, addr - 0x100));
556
557 /* Branch to self + 0x100 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200558 err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000559 check(instr_is_branch_to_addr(&instr, addr + 0x100));
560
561 /* Maximum relative negative offset, - 32 KB */
Olivier Deprez157378f2022-04-04 15:47:50 +0200562 err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
564
565 /* Out of range relative negative offset, - 32 KB + 4*/
Olivier Deprez157378f2022-04-04 15:47:50 +0200566 err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
567 check(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568
569 /* Out of range relative positive offset, + 32 KB */
Olivier Deprez157378f2022-04-04 15:47:50 +0200570 err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
571 check(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572
573 /* Unaligned target */
Olivier Deprez157378f2022-04-04 15:47:50 +0200574 err = create_cond_branch(&instr, iptr, addr + 3, flags);
575 check(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000576
577 /* Check flags are masked correctly */
Olivier Deprez157378f2022-04-04 15:47:50 +0200578 err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579 check(instr_is_branch_to_addr(&instr, addr));
Olivier Deprez157378f2022-04-04 15:47:50 +0200580 check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000581}
582
583static void __init test_translate_branch(void)
584{
585 unsigned long addr;
Olivier Deprez157378f2022-04-04 15:47:50 +0200586 void *p, *q;
587 struct ppc_inst instr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000588 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));
Olivier Deprez157378f2022-04-04 15:47:50 +0200600 q = p + 4;
601 translate_branch(&instr, q, p);
602 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000603 check(instr_is_branch_to_addr(q, addr));
604
605 /* Maximum negative case, move b . to addr + 32 MB */
606 p = buf;
607 addr = (unsigned long)p;
608 patch_branch(p, addr, 0);
609 q = buf + 0x2000000;
Olivier Deprez157378f2022-04-04 15:47:50 +0200610 translate_branch(&instr, q, p);
611 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000612 check(instr_is_branch_to_addr(p, addr));
613 check(instr_is_branch_to_addr(q, addr));
Olivier Deprez157378f2022-04-04 15:47:50 +0200614 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000615
616 /* Maximum positive case, move x to x - 32 MB + 4 */
617 p = buf + 0x2000000;
618 addr = (unsigned long)p;
619 patch_branch(p, addr, 0);
620 q = buf + 4;
Olivier Deprez157378f2022-04-04 15:47:50 +0200621 translate_branch(&instr, q, p);
622 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000623 check(instr_is_branch_to_addr(p, addr));
624 check(instr_is_branch_to_addr(q, addr));
Olivier Deprez157378f2022-04-04 15:47:50 +0200625 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000626
627 /* Jump to x + 16 MB moved to x + 20 MB */
628 p = buf;
629 addr = 0x1000000 + (unsigned long)buf;
630 patch_branch(p, addr, BRANCH_SET_LINK);
631 q = buf + 0x1400000;
Olivier Deprez157378f2022-04-04 15:47:50 +0200632 translate_branch(&instr, q, p);
633 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000634 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;
Olivier Deprez157378f2022-04-04 15:47:50 +0200642 translate_branch(&instr, q, p);
643 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644 check(instr_is_branch_to_addr(p, addr));
645 check(instr_is_branch_to_addr(q, addr));
646
647
648 /* Conditional branch tests */
649
650 /* Simple case, branch to self moved a little */
651 p = buf;
652 addr = (unsigned long)p;
Olivier Deprez157378f2022-04-04 15:47:50 +0200653 create_cond_branch(&instr, p, addr, 0);
654 patch_instruction(p, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000655 check(instr_is_branch_to_addr(p, addr));
Olivier Deprez157378f2022-04-04 15:47:50 +0200656 q = buf + 4;
657 translate_branch(&instr, q, p);
658 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000659 check(instr_is_branch_to_addr(q, addr));
660
661 /* Maximum negative case, move b . to addr + 32 KB */
662 p = buf;
663 addr = (unsigned long)p;
Olivier Deprez157378f2022-04-04 15:47:50 +0200664 create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
665 patch_instruction(p, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000666 q = buf + 0x8000;
Olivier Deprez157378f2022-04-04 15:47:50 +0200667 translate_branch(&instr, q, p);
668 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000669 check(instr_is_branch_to_addr(p, addr));
670 check(instr_is_branch_to_addr(q, addr));
Olivier Deprez157378f2022-04-04 15:47:50 +0200671 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000672
673 /* Maximum positive case, move x to x - 32 KB + 4 */
674 p = buf + 0x8000;
675 addr = (unsigned long)p;
Olivier Deprez157378f2022-04-04 15:47:50 +0200676 create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
677 patch_instruction(p, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000678 q = buf + 4;
Olivier Deprez157378f2022-04-04 15:47:50 +0200679 translate_branch(&instr, q, p);
680 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681 check(instr_is_branch_to_addr(p, addr));
682 check(instr_is_branch_to_addr(q, addr));
Olivier Deprez157378f2022-04-04 15:47:50 +0200683 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000684
685 /* Jump to x + 12 KB moved to x + 20 KB */
686 p = buf;
687 addr = 0x3000 + (unsigned long)buf;
Olivier Deprez157378f2022-04-04 15:47:50 +0200688 create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
689 patch_instruction(p, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000690 q = buf + 0x5000;
Olivier Deprez157378f2022-04-04 15:47:50 +0200691 translate_branch(&instr, q, p);
692 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693 check(instr_is_branch_to_addr(p, addr));
694 check(instr_is_branch_to_addr(q, addr));
695
696 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
697 p = buf + 0x2000;
698 addr = 0x4000 + (unsigned long)buf;
Olivier Deprez157378f2022-04-04 15:47:50 +0200699 create_cond_branch(&instr, p, addr, 0);
700 patch_instruction(p, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000701 q = buf + 4;
Olivier Deprez157378f2022-04-04 15:47:50 +0200702 translate_branch(&instr, q, p);
703 patch_instruction(q, instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000704 check(instr_is_branch_to_addr(p, addr));
705 check(instr_is_branch_to_addr(q, addr));
706
707 /* Free the buffer we were using */
708 vfree(buf);
709}
710
Olivier Deprez157378f2022-04-04 15:47:50 +0200711#ifdef CONFIG_PPC64
712static void __init test_prefixed_patching(void)
713{
714 extern unsigned int code_patching_test1[];
715 extern unsigned int code_patching_test1_expected[];
716 extern unsigned int end_code_patching_test1[];
717
718 __patch_instruction((struct ppc_inst *)code_patching_test1,
719 ppc_inst_prefix(OP_PREFIX << 26, 0x00000000),
720 (struct ppc_inst *)code_patching_test1);
721
722 check(!memcmp(code_patching_test1,
723 code_patching_test1_expected,
724 sizeof(unsigned int) *
725 (end_code_patching_test1 - code_patching_test1)));
726}
727#else
728static inline void test_prefixed_patching(void) {}
729#endif
730
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000731static int __init test_code_patching(void)
732{
733 printk(KERN_DEBUG "Running code patching self-tests ...\n");
734
735 test_branch_iform();
736 test_branch_bform();
737 test_create_function_call();
738 test_translate_branch();
Olivier Deprez157378f2022-04-04 15:47:50 +0200739 test_prefixed_patching();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000740
741 return 0;
742}
743late_initcall(test_code_patching);
744
745#endif /* CONFIG_CODE_PATCHING_SELFTEST */