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