David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008 Michael Ellerman, IBM Corporation. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/kprobes.h> |
| 8 | #include <linux/vmalloc.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/cpuhotplug.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/uaccess.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 14 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 15 | #include <asm/tlbflush.h> |
| 16 | #include <asm/page.h> |
| 17 | #include <asm/code-patching.h> |
| 18 | #include <asm/setup.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 19 | #include <asm/inst.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 21 | static int __patch_instruction(struct ppc_inst *exec_addr, struct ppc_inst instr, |
| 22 | struct ppc_inst *patch_addr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 24 | 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 Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | |
| 29 | asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr), |
| 30 | "r" (exec_addr)); |
| 31 | |
| 32 | return 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 33 | |
| 34 | failed: |
| 35 | return -EFAULT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 38 | int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 39 | { |
| 40 | return __patch_instruction(addr, instr, addr); |
| 41 | } |
| 42 | |
| 43 | #ifdef CONFIG_STRICT_KERNEL_RWX |
| 44 | static DEFINE_PER_CPU(struct vm_struct *, text_poke_area); |
| 45 | |
| 46 | static 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 | |
| 61 | static 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 | */ |
| 75 | static 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 | } |
| 83 | late_initcall(setup_text_poke_area); |
| 84 | |
| 85 | /* |
| 86 | * This can be called for kernel text or a module. |
| 87 | */ |
| 88 | static int map_patch_area(void *addr, unsigned long text_poke_addr) |
| 89 | { |
| 90 | unsigned long pfn; |
| 91 | int err; |
| 92 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 93 | if (is_vmalloc_or_module_addr(addr)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | pfn = vmalloc_to_pfn(addr); |
| 95 | else |
| 96 | pfn = __pa_symbol(addr) >> PAGE_SHIFT; |
| 97 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 98 | err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 99 | |
| 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 | |
| 107 | static inline int unmap_patch_area(unsigned long addr) |
| 108 | { |
| 109 | pte_t *ptep; |
| 110 | pmd_t *pmdp; |
| 111 | pud_t *pudp; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 112 | p4d_t *p4dp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 113 | pgd_t *pgdp; |
| 114 | |
| 115 | pgdp = pgd_offset_k(addr); |
| 116 | if (unlikely(!pgdp)) |
| 117 | return -EINVAL; |
| 118 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 119 | p4dp = p4d_offset(pgdp, addr); |
| 120 | if (unlikely(!p4dp)) |
| 121 | return -EINVAL; |
| 122 | |
| 123 | pudp = pud_offset(p4dp, addr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 124 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 146 | static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 147 | { |
| 148 | int err; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 149 | struct ppc_inst *patch_addr = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 150 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 170 | patch_addr = (struct ppc_inst *)(text_poke_addr + (kaddr & ~PAGE_MASK)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 171 | |
| 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 | |
| 178 | out: |
| 179 | local_irq_restore(flags); |
| 180 | |
| 181 | return err; |
| 182 | } |
| 183 | #else /* !CONFIG_STRICT_KERNEL_RWX */ |
| 184 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 185 | static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 186 | { |
| 187 | return raw_patch_instruction(addr, instr); |
| 188 | } |
| 189 | |
| 190 | #endif /* CONFIG_STRICT_KERNEL_RWX */ |
| 191 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 192 | int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 193 | { |
| 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 | } |
| 201 | NOKPROBE_SYMBOL(patch_instruction); |
| 202 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 203 | int patch_branch(struct ppc_inst *addr, unsigned long target, int flags) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 204 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 205 | struct ppc_inst instr; |
| 206 | |
| 207 | create_branch(&instr, addr, target, flags); |
| 208 | return patch_instruction(addr, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 211 | bool 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 233 | bool is_offset_in_cond_branch_range(long offset) |
| 234 | { |
| 235 | return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3); |
| 236 | } |
| 237 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 238 | /* |
| 239 | * Helper to check if a given instruction is a conditional branch |
| 240 | * Derived from the conditional checks in analyse_instr() |
| 241 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 242 | bool is_conditional_branch(struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 243 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 244 | unsigned int opcode = ppc_inst_primary_opcode(instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 245 | |
| 246 | if (opcode == 16) /* bc, bca, bcl, bcla */ |
| 247 | return true; |
| 248 | if (opcode == 19) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 249 | switch ((ppc_inst_val(instr) >> 1) & 0x3ff) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 250 | case 16: /* bclr, bclrl */ |
| 251 | case 528: /* bcctr, bcctrl */ |
| 252 | case 560: /* bctar, bctarl */ |
| 253 | return true; |
| 254 | } |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | NOKPROBE_SYMBOL(is_conditional_branch); |
| 259 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 260 | int create_branch(struct ppc_inst *instr, |
| 261 | const struct ppc_inst *addr, |
| 262 | unsigned long target, int flags) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 263 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 264 | long offset; |
| 265 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 266 | *instr = ppc_inst(0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 267 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 273 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 274 | |
| 275 | /* Mask out the flags and target, so they don't step on each other. */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 276 | *instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 277 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 278 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 281 | int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr, |
| 282 | unsigned long target, int flags) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 283 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 284 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 291 | if (!is_offset_in_cond_branch_range(offset)) |
| 292 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 293 | |
| 294 | /* Mask out the flags and target, so they don't step on each other. */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 295 | *instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 296 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 297 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 300 | static unsigned int branch_opcode(struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 301 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 302 | return ppc_inst_primary_opcode(instr) & 0x3F; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 305 | static int instr_is_branch_iform(struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 306 | { |
| 307 | return branch_opcode(instr) == 18; |
| 308 | } |
| 309 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 310 | static int instr_is_branch_bform(struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 311 | { |
| 312 | return branch_opcode(instr) == 16; |
| 313 | } |
| 314 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 315 | int instr_is_relative_branch(struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 316 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 317 | if (ppc_inst_val(instr) & BRANCH_ABSOLUTE) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | |
| 320 | return instr_is_branch_iform(instr) || instr_is_branch_bform(instr); |
| 321 | } |
| 322 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 323 | int instr_is_relative_link_branch(struct ppc_inst instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 324 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 325 | return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 328 | static unsigned long branch_iform_target(const struct ppc_inst *instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 329 | { |
| 330 | signed long imm; |
| 331 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 332 | imm = ppc_inst_val(*instr) & 0x3FFFFFC; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 333 | |
| 334 | /* If the top bit of the immediate value is set this is negative */ |
| 335 | if (imm & 0x2000000) |
| 336 | imm -= 0x4000000; |
| 337 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 338 | if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 339 | imm += (unsigned long)instr; |
| 340 | |
| 341 | return (unsigned long)imm; |
| 342 | } |
| 343 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 344 | static unsigned long branch_bform_target(const struct ppc_inst *instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 345 | { |
| 346 | signed long imm; |
| 347 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 348 | imm = ppc_inst_val(*instr) & 0xFFFC; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 349 | |
| 350 | /* If the top bit of the immediate value is set this is negative */ |
| 351 | if (imm & 0x8000) |
| 352 | imm -= 0x10000; |
| 353 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 354 | if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 355 | imm += (unsigned long)instr; |
| 356 | |
| 357 | return (unsigned long)imm; |
| 358 | } |
| 359 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 360 | unsigned long branch_target(const struct ppc_inst *instr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 361 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 362 | if (instr_is_branch_iform(ppc_inst_read(instr))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 363 | return branch_iform_target(instr); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 364 | else if (instr_is_branch_bform(ppc_inst_read(instr))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 365 | return branch_bform_target(instr); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 370 | int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 371 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 372 | if (instr_is_branch_iform(ppc_inst_read(instr)) || |
| 373 | instr_is_branch_bform(ppc_inst_read(instr))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 374 | return branch_target(instr) == addr; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 379 | int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest, |
| 380 | const struct ppc_inst *src) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 381 | { |
| 382 | unsigned long target; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 383 | target = branch_target(src); |
| 384 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 385 | 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 Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 391 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 392 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | #ifdef CONFIG_PPC_BOOK3E_64 |
| 396 | void __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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 407 | patch_branch((struct ppc_inst *)(ibase + (exc / 4) + 1), addr, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 408 | } |
| 409 | #endif |
| 410 | |
| 411 | #ifdef CONFIG_CODE_PATCHING_SELFTEST |
| 412 | |
| 413 | static 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 | |
| 421 | static void __init test_branch_iform(void) |
| 422 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 423 | int err; |
| 424 | struct ppc_inst instr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 425 | unsigned long addr; |
| 426 | |
| 427 | addr = (unsigned long)&instr; |
| 428 | |
| 429 | /* The simplest case, branch to self, no flags */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 430 | check(instr_is_branch_iform(ppc_inst(0x48000000))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 431 | /* All bits of target set, and flags */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 432 | check(instr_is_branch_iform(ppc_inst(0x4bffffff))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 433 | /* High bit of opcode set, which is wrong */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 434 | check(!instr_is_branch_iform(ppc_inst(0xcbffffff))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 435 | /* Middle bits of opcode set, which is wrong */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 436 | check(!instr_is_branch_iform(ppc_inst(0x7bffffff))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 437 | |
| 438 | /* Simplest case, branch to self with link */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 439 | check(instr_is_branch_iform(ppc_inst(0x48000001))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 440 | /* All bits of targets set */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 441 | check(instr_is_branch_iform(ppc_inst(0x4bfffffd))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 442 | /* Some bits of targets set */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 443 | check(instr_is_branch_iform(ppc_inst(0x4bff00fd))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 444 | /* Must be a valid branch to start with */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 445 | check(!instr_is_branch_iform(ppc_inst(0x7bfffffd))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 446 | |
| 447 | /* Absolute branch to 0x100 */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 448 | instr = ppc_inst(0x48000103); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 449 | check(instr_is_branch_to_addr(&instr, 0x100)); |
| 450 | /* Absolute branch to 0x420fc */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 451 | instr = ppc_inst(0x480420ff); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 452 | check(instr_is_branch_to_addr(&instr, 0x420fc)); |
| 453 | /* Maximum positive relative branch, + 20MB - 4B */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 454 | instr = ppc_inst(0x49fffffc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 455 | check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC)); |
| 456 | /* Smallest negative relative branch, - 4B */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 457 | instr = ppc_inst(0x4bfffffc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 458 | check(instr_is_branch_to_addr(&instr, addr - 4)); |
| 459 | /* Largest negative relative branch, - 32 MB */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 460 | instr = ppc_inst(0x4a000000); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 461 | check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); |
| 462 | |
| 463 | /* Branch to self, with link */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 464 | err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 465 | check(instr_is_branch_to_addr(&instr, addr)); |
| 466 | |
| 467 | /* Branch to self - 0x100, with link */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 468 | err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 469 | check(instr_is_branch_to_addr(&instr, addr - 0x100)); |
| 470 | |
| 471 | /* Branch to self + 0x100, no link */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 472 | err = create_branch(&instr, &instr, addr + 0x100, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 473 | check(instr_is_branch_to_addr(&instr, addr + 0x100)); |
| 474 | |
| 475 | /* Maximum relative negative offset, - 32 MB */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 476 | err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 477 | check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); |
| 478 | |
| 479 | /* Out of range relative negative offset, - 32 MB + 4*/ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 480 | err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK); |
| 481 | check(err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 482 | |
| 483 | /* Out of range relative positive offset, + 32 MB */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 484 | err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK); |
| 485 | check(err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 486 | |
| 487 | /* Unaligned target */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 488 | err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK); |
| 489 | check(err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 490 | |
| 491 | /* Check flags are masked correctly */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 492 | err = create_branch(&instr, &instr, addr, 0xFFFFFFFC); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 493 | check(instr_is_branch_to_addr(&instr, addr)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 494 | check(ppc_inst_equal(instr, ppc_inst(0x48000000))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static void __init test_create_function_call(void) |
| 498 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 499 | struct ppc_inst *iptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 500 | unsigned long dest; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 501 | struct ppc_inst instr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 502 | |
| 503 | /* Check we can create a function call */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 504 | iptr = (struct ppc_inst *)ppc_function_entry(test_trampoline); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 505 | dest = ppc_function_entry(test_create_function_call); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 506 | create_branch(&instr, iptr, dest, BRANCH_SET_LINK); |
| 507 | patch_instruction(iptr, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 508 | check(instr_is_branch_to_addr(iptr, dest)); |
| 509 | } |
| 510 | |
| 511 | static void __init test_branch_bform(void) |
| 512 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 513 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 514 | unsigned long addr; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 515 | struct ppc_inst *iptr, instr; |
| 516 | unsigned int flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 517 | |
| 518 | iptr = &instr; |
| 519 | addr = (unsigned long)iptr; |
| 520 | |
| 521 | /* The simplest case, branch to self, no flags */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 522 | check(instr_is_branch_bform(ppc_inst(0x40000000))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 523 | /* All bits of target set, and flags */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 524 | check(instr_is_branch_bform(ppc_inst(0x43ffffff))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 525 | /* High bit of opcode set, which is wrong */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 526 | check(!instr_is_branch_bform(ppc_inst(0xc3ffffff))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 527 | /* Middle bits of opcode set, which is wrong */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 528 | check(!instr_is_branch_bform(ppc_inst(0x7bffffff))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 529 | |
| 530 | /* Absolute conditional branch to 0x100 */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 531 | instr = ppc_inst(0x43ff0103); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 532 | check(instr_is_branch_to_addr(&instr, 0x100)); |
| 533 | /* Absolute conditional branch to 0x20fc */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 534 | instr = ppc_inst(0x43ff20ff); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 535 | check(instr_is_branch_to_addr(&instr, 0x20fc)); |
| 536 | /* Maximum positive relative conditional branch, + 32 KB - 4B */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 537 | instr = ppc_inst(0x43ff7ffc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 538 | check(instr_is_branch_to_addr(&instr, addr + 0x7FFC)); |
| 539 | /* Smallest negative relative conditional branch, - 4B */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 540 | instr = ppc_inst(0x43fffffc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 541 | check(instr_is_branch_to_addr(&instr, addr - 4)); |
| 542 | /* Largest negative relative conditional branch, - 32 KB */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 543 | instr = ppc_inst(0x43ff8000); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 544 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 550 | err = create_cond_branch(&instr, iptr, addr, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 551 | check(instr_is_branch_to_addr(&instr, addr)); |
| 552 | |
| 553 | /* Branch to self - 0x100 */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 554 | err = create_cond_branch(&instr, iptr, addr - 0x100, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 555 | check(instr_is_branch_to_addr(&instr, addr - 0x100)); |
| 556 | |
| 557 | /* Branch to self + 0x100 */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 558 | err = create_cond_branch(&instr, iptr, addr + 0x100, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 559 | check(instr_is_branch_to_addr(&instr, addr + 0x100)); |
| 560 | |
| 561 | /* Maximum relative negative offset, - 32 KB */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 562 | err = create_cond_branch(&instr, iptr, addr - 0x8000, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 563 | check(instr_is_branch_to_addr(&instr, addr - 0x8000)); |
| 564 | |
| 565 | /* Out of range relative negative offset, - 32 KB + 4*/ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 566 | err = create_cond_branch(&instr, iptr, addr - 0x8004, flags); |
| 567 | check(err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 568 | |
| 569 | /* Out of range relative positive offset, + 32 KB */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 570 | err = create_cond_branch(&instr, iptr, addr + 0x8000, flags); |
| 571 | check(err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 572 | |
| 573 | /* Unaligned target */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 574 | err = create_cond_branch(&instr, iptr, addr + 3, flags); |
| 575 | check(err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 576 | |
| 577 | /* Check flags are masked correctly */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 578 | err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 579 | check(instr_is_branch_to_addr(&instr, addr)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 580 | check(ppc_inst_equal(instr, ppc_inst(0x43FF0000))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static void __init test_translate_branch(void) |
| 584 | { |
| 585 | unsigned long addr; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 586 | void *p, *q; |
| 587 | struct ppc_inst instr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 588 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 600 | q = p + 4; |
| 601 | translate_branch(&instr, q, p); |
| 602 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 603 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 610 | translate_branch(&instr, q, p); |
| 611 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 612 | check(instr_is_branch_to_addr(p, addr)); |
| 613 | check(instr_is_branch_to_addr(q, addr)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 614 | check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 615 | |
| 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 621 | translate_branch(&instr, q, p); |
| 622 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 623 | check(instr_is_branch_to_addr(p, addr)); |
| 624 | check(instr_is_branch_to_addr(q, addr)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 625 | check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 626 | |
| 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 632 | translate_branch(&instr, q, p); |
| 633 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 634 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 642 | translate_branch(&instr, q, p); |
| 643 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 644 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 653 | create_cond_branch(&instr, p, addr, 0); |
| 654 | patch_instruction(p, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 655 | check(instr_is_branch_to_addr(p, addr)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 656 | q = buf + 4; |
| 657 | translate_branch(&instr, q, p); |
| 658 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 659 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 664 | create_cond_branch(&instr, p, addr, 0xFFFFFFFC); |
| 665 | patch_instruction(p, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 666 | q = buf + 0x8000; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 667 | translate_branch(&instr, q, p); |
| 668 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 669 | check(instr_is_branch_to_addr(p, addr)); |
| 670 | check(instr_is_branch_to_addr(q, addr)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 671 | check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 672 | |
| 673 | /* Maximum positive case, move x to x - 32 KB + 4 */ |
| 674 | p = buf + 0x8000; |
| 675 | addr = (unsigned long)p; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 676 | create_cond_branch(&instr, p, addr, 0xFFFFFFFC); |
| 677 | patch_instruction(p, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 678 | q = buf + 4; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 679 | translate_branch(&instr, q, p); |
| 680 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 681 | check(instr_is_branch_to_addr(p, addr)); |
| 682 | check(instr_is_branch_to_addr(q, addr)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 683 | check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 684 | |
| 685 | /* Jump to x + 12 KB moved to x + 20 KB */ |
| 686 | p = buf; |
| 687 | addr = 0x3000 + (unsigned long)buf; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 688 | create_cond_branch(&instr, p, addr, BRANCH_SET_LINK); |
| 689 | patch_instruction(p, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 690 | q = buf + 0x5000; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 691 | translate_branch(&instr, q, p); |
| 692 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 693 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 699 | create_cond_branch(&instr, p, addr, 0); |
| 700 | patch_instruction(p, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 701 | q = buf + 4; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 702 | translate_branch(&instr, q, p); |
| 703 | patch_instruction(q, instr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 704 | 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 711 | #ifdef CONFIG_PPC64 |
| 712 | static 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 |
| 728 | static inline void test_prefixed_patching(void) {} |
| 729 | #endif |
| 730 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 731 | static 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 Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 739 | test_prefixed_patching(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | late_initcall(test_code_patching); |
| 744 | |
| 745 | #endif /* CONFIG_CODE_PATCHING_SELFTEST */ |