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