1aaddd3eaSMichael Ellerman /* 2aaddd3eaSMichael Ellerman * Copyright 2008 Michael Ellerman, IBM Corporation. 3aaddd3eaSMichael Ellerman * 4aaddd3eaSMichael Ellerman * This program is free software; you can redistribute it and/or 5aaddd3eaSMichael Ellerman * modify it under the terms of the GNU General Public License 6aaddd3eaSMichael Ellerman * as published by the Free Software Foundation; either version 7aaddd3eaSMichael Ellerman * 2 of the License, or (at your option) any later version. 8aaddd3eaSMichael Ellerman */ 9aaddd3eaSMichael Ellerman 10aaddd3eaSMichael Ellerman #include <linux/kernel.h> 1171f6e58eSNaveen N. Rao #include <linux/kprobes.h> 12ae0dc736SMichael Ellerman #include <linux/vmalloc.h> 13ae0dc736SMichael Ellerman #include <linux/init.h> 1427ac792cSAndrea Righi #include <linux/mm.h> 1537bc3e5fSBalbir Singh #include <linux/cpuhotplug.h> 1637bc3e5fSBalbir Singh #include <linux/slab.h> 177c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 187d134b2cSLuis R. Rodriguez #include <linux/kprobes.h> 19aaddd3eaSMichael Ellerman 2037bc3e5fSBalbir Singh #include <asm/pgtable.h> 2137bc3e5fSBalbir Singh #include <asm/tlbflush.h> 2237bc3e5fSBalbir Singh #include <asm/page.h> 2337bc3e5fSBalbir Singh #include <asm/code-patching.h> 24252eb558SChristophe Leroy #include <asm/setup.h> 25aaddd3eaSMichael Ellerman 268cf4c057SChristophe Leroy static int __patch_instruction(unsigned int *exec_addr, unsigned int instr, 278cf4c057SChristophe Leroy unsigned int *patch_addr) 28aaddd3eaSMichael Ellerman { 29b6e37968SSteven Rostedt int err; 30b6e37968SSteven Rostedt 318cf4c057SChristophe Leroy __put_user_size(instr, patch_addr, 4, err); 32b6e37968SSteven Rostedt if (err) 33b6e37968SSteven Rostedt return err; 3437bc3e5fSBalbir Singh 358cf4c057SChristophe Leroy asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr), 368cf4c057SChristophe Leroy "r" (exec_addr)); 3737bc3e5fSBalbir Singh 38b6e37968SSteven Rostedt return 0; 39aaddd3eaSMichael Ellerman } 40aaddd3eaSMichael Ellerman 418183d99fSChristophe Leroy int raw_patch_instruction(unsigned int *addr, unsigned int instr) 428cf4c057SChristophe Leroy { 438cf4c057SChristophe Leroy return __patch_instruction(addr, instr, addr); 448cf4c057SChristophe Leroy } 458cf4c057SChristophe Leroy 4637bc3e5fSBalbir Singh #ifdef CONFIG_STRICT_KERNEL_RWX 4737bc3e5fSBalbir Singh static DEFINE_PER_CPU(struct vm_struct *, text_poke_area); 4837bc3e5fSBalbir Singh 4937bc3e5fSBalbir Singh static int text_area_cpu_up(unsigned int cpu) 5037bc3e5fSBalbir Singh { 5137bc3e5fSBalbir Singh struct vm_struct *area; 5237bc3e5fSBalbir Singh 5337bc3e5fSBalbir Singh area = get_vm_area(PAGE_SIZE, VM_ALLOC); 5437bc3e5fSBalbir Singh if (!area) { 5537bc3e5fSBalbir Singh WARN_ONCE(1, "Failed to create text area for cpu %d\n", 5637bc3e5fSBalbir Singh cpu); 5737bc3e5fSBalbir Singh return -1; 5837bc3e5fSBalbir Singh } 5937bc3e5fSBalbir Singh this_cpu_write(text_poke_area, area); 6037bc3e5fSBalbir Singh 6137bc3e5fSBalbir Singh return 0; 6237bc3e5fSBalbir Singh } 6337bc3e5fSBalbir Singh 6437bc3e5fSBalbir Singh static int text_area_cpu_down(unsigned int cpu) 6537bc3e5fSBalbir Singh { 6637bc3e5fSBalbir Singh free_vm_area(this_cpu_read(text_poke_area)); 6737bc3e5fSBalbir Singh return 0; 6837bc3e5fSBalbir Singh } 6937bc3e5fSBalbir Singh 7037bc3e5fSBalbir Singh /* 7137bc3e5fSBalbir Singh * Run as a late init call. This allows all the boot time patching to be done 7237bc3e5fSBalbir Singh * simply by patching the code, and then we're called here prior to 7337bc3e5fSBalbir Singh * mark_rodata_ro(), which happens after all init calls are run. Although 7437bc3e5fSBalbir Singh * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge 7537bc3e5fSBalbir Singh * it as being preferable to a kernel that will crash later when someone tries 7637bc3e5fSBalbir Singh * to use patch_instruction(). 7737bc3e5fSBalbir Singh */ 7837bc3e5fSBalbir Singh static int __init setup_text_poke_area(void) 7937bc3e5fSBalbir Singh { 8037bc3e5fSBalbir Singh BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, 8137bc3e5fSBalbir Singh "powerpc/text_poke:online", text_area_cpu_up, 8237bc3e5fSBalbir Singh text_area_cpu_down)); 8337bc3e5fSBalbir Singh 8437bc3e5fSBalbir Singh return 0; 8537bc3e5fSBalbir Singh } 8637bc3e5fSBalbir Singh late_initcall(setup_text_poke_area); 8737bc3e5fSBalbir Singh 8837bc3e5fSBalbir Singh /* 8937bc3e5fSBalbir Singh * This can be called for kernel text or a module. 9037bc3e5fSBalbir Singh */ 9137bc3e5fSBalbir Singh static int map_patch_area(void *addr, unsigned long text_poke_addr) 9237bc3e5fSBalbir Singh { 9337bc3e5fSBalbir Singh unsigned long pfn; 9437bc3e5fSBalbir Singh int err; 9537bc3e5fSBalbir Singh 9637bc3e5fSBalbir Singh if (is_vmalloc_addr(addr)) 9737bc3e5fSBalbir Singh pfn = vmalloc_to_pfn(addr); 9837bc3e5fSBalbir Singh else 9937bc3e5fSBalbir Singh pfn = __pa_symbol(addr) >> PAGE_SHIFT; 10037bc3e5fSBalbir Singh 10137bc3e5fSBalbir Singh err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), 10237bc3e5fSBalbir Singh pgprot_val(PAGE_KERNEL)); 10337bc3e5fSBalbir Singh 10437bc3e5fSBalbir Singh pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err); 10537bc3e5fSBalbir Singh if (err) 10637bc3e5fSBalbir Singh return -1; 10737bc3e5fSBalbir Singh 10837bc3e5fSBalbir Singh return 0; 10937bc3e5fSBalbir Singh } 11037bc3e5fSBalbir Singh 11137bc3e5fSBalbir Singh static inline int unmap_patch_area(unsigned long addr) 11237bc3e5fSBalbir Singh { 11337bc3e5fSBalbir Singh pte_t *ptep; 11437bc3e5fSBalbir Singh pmd_t *pmdp; 11537bc3e5fSBalbir Singh pud_t *pudp; 11637bc3e5fSBalbir Singh pgd_t *pgdp; 11737bc3e5fSBalbir Singh 11837bc3e5fSBalbir Singh pgdp = pgd_offset_k(addr); 11937bc3e5fSBalbir Singh if (unlikely(!pgdp)) 12037bc3e5fSBalbir Singh return -EINVAL; 12137bc3e5fSBalbir Singh 12237bc3e5fSBalbir Singh pudp = pud_offset(pgdp, addr); 12337bc3e5fSBalbir Singh if (unlikely(!pudp)) 12437bc3e5fSBalbir Singh return -EINVAL; 12537bc3e5fSBalbir Singh 12637bc3e5fSBalbir Singh pmdp = pmd_offset(pudp, addr); 12737bc3e5fSBalbir Singh if (unlikely(!pmdp)) 12837bc3e5fSBalbir Singh return -EINVAL; 12937bc3e5fSBalbir Singh 13037bc3e5fSBalbir Singh ptep = pte_offset_kernel(pmdp, addr); 13137bc3e5fSBalbir Singh if (unlikely(!ptep)) 13237bc3e5fSBalbir Singh return -EINVAL; 13337bc3e5fSBalbir Singh 13437bc3e5fSBalbir Singh pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr); 13537bc3e5fSBalbir Singh 13637bc3e5fSBalbir Singh /* 13737bc3e5fSBalbir Singh * In hash, pte_clear flushes the tlb, in radix, we have to 13837bc3e5fSBalbir Singh */ 13937bc3e5fSBalbir Singh pte_clear(&init_mm, addr, ptep); 14037bc3e5fSBalbir Singh flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 14137bc3e5fSBalbir Singh 14237bc3e5fSBalbir Singh return 0; 14337bc3e5fSBalbir Singh } 14437bc3e5fSBalbir Singh 145*b45ba4a5SChristophe Leroy static int do_patch_instruction(unsigned int *addr, unsigned int instr) 14637bc3e5fSBalbir Singh { 14737bc3e5fSBalbir Singh int err; 1488cf4c057SChristophe Leroy unsigned int *patch_addr = NULL; 14937bc3e5fSBalbir Singh unsigned long flags; 15037bc3e5fSBalbir Singh unsigned long text_poke_addr; 15137bc3e5fSBalbir Singh unsigned long kaddr = (unsigned long)addr; 15237bc3e5fSBalbir Singh 15337bc3e5fSBalbir Singh /* 15437bc3e5fSBalbir Singh * During early early boot patch_instruction is called 15537bc3e5fSBalbir Singh * when text_poke_area is not ready, but we still need 15637bc3e5fSBalbir Singh * to allow patching. We just do the plain old patching 15737bc3e5fSBalbir Singh */ 1588183d99fSChristophe Leroy if (!this_cpu_read(text_poke_area)) 1598cf4c057SChristophe Leroy return raw_patch_instruction(addr, instr); 16037bc3e5fSBalbir Singh 16137bc3e5fSBalbir Singh local_irq_save(flags); 16237bc3e5fSBalbir Singh 16337bc3e5fSBalbir Singh text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr; 16437bc3e5fSBalbir Singh if (map_patch_area(addr, text_poke_addr)) { 16537bc3e5fSBalbir Singh err = -1; 16637bc3e5fSBalbir Singh goto out; 16737bc3e5fSBalbir Singh } 16837bc3e5fSBalbir Singh 1698cf4c057SChristophe Leroy patch_addr = (unsigned int *)(text_poke_addr) + 17037bc3e5fSBalbir Singh ((kaddr & ~PAGE_MASK) / sizeof(unsigned int)); 17137bc3e5fSBalbir Singh 1728cf4c057SChristophe Leroy __patch_instruction(addr, instr, patch_addr); 17337bc3e5fSBalbir Singh 17437bc3e5fSBalbir Singh err = unmap_patch_area(text_poke_addr); 17537bc3e5fSBalbir Singh if (err) 17637bc3e5fSBalbir Singh pr_warn("failed to unmap %lx\n", text_poke_addr); 17737bc3e5fSBalbir Singh 17837bc3e5fSBalbir Singh out: 17937bc3e5fSBalbir Singh local_irq_restore(flags); 18037bc3e5fSBalbir Singh 18137bc3e5fSBalbir Singh return err; 18237bc3e5fSBalbir Singh } 18337bc3e5fSBalbir Singh #else /* !CONFIG_STRICT_KERNEL_RWX */ 18437bc3e5fSBalbir Singh 185*b45ba4a5SChristophe Leroy static int do_patch_instruction(unsigned int *addr, unsigned int instr) 18637bc3e5fSBalbir Singh { 1878cf4c057SChristophe Leroy return raw_patch_instruction(addr, instr); 18837bc3e5fSBalbir Singh } 18937bc3e5fSBalbir Singh 19037bc3e5fSBalbir Singh #endif /* CONFIG_STRICT_KERNEL_RWX */ 191*b45ba4a5SChristophe Leroy 192*b45ba4a5SChristophe Leroy int patch_instruction(unsigned int *addr, unsigned int instr) 193*b45ba4a5SChristophe Leroy { 194*b45ba4a5SChristophe Leroy /* Make sure we aren't patching a freed init section */ 195*b45ba4a5SChristophe Leroy if (init_mem_is_free && init_section_contains(addr, 4)) { 196*b45ba4a5SChristophe Leroy pr_debug("Skipping init section patching addr: 0x%px\n", addr); 197*b45ba4a5SChristophe Leroy return 0; 198*b45ba4a5SChristophe Leroy } 199*b45ba4a5SChristophe Leroy return do_patch_instruction(addr, instr); 200*b45ba4a5SChristophe Leroy } 20137bc3e5fSBalbir Singh NOKPROBE_SYMBOL(patch_instruction); 20237bc3e5fSBalbir Singh 203b6e37968SSteven Rostedt int patch_branch(unsigned int *addr, unsigned long target, int flags) 204e7a57273SMichael Ellerman { 205b6e37968SSteven Rostedt return patch_instruction(addr, create_branch(addr, target, flags)); 206e7a57273SMichael Ellerman } 207e7a57273SMichael Ellerman 20806d0bbc6SMichael Ellerman int patch_branch_site(s32 *site, unsigned long target, int flags) 20906d0bbc6SMichael Ellerman { 21006d0bbc6SMichael Ellerman unsigned int *addr; 21106d0bbc6SMichael Ellerman 21206d0bbc6SMichael Ellerman addr = (unsigned int *)((unsigned long)site + *site); 21306d0bbc6SMichael Ellerman return patch_instruction(addr, create_branch(addr, target, flags)); 21406d0bbc6SMichael Ellerman } 21506d0bbc6SMichael Ellerman 21606d0bbc6SMichael Ellerman int patch_instruction_site(s32 *site, unsigned int instr) 21706d0bbc6SMichael Ellerman { 21806d0bbc6SMichael Ellerman unsigned int *addr; 21906d0bbc6SMichael Ellerman 22006d0bbc6SMichael Ellerman addr = (unsigned int *)((unsigned long)site + *site); 22106d0bbc6SMichael Ellerman return patch_instruction(addr, instr); 22206d0bbc6SMichael Ellerman } 22306d0bbc6SMichael Ellerman 224ebfa50dfSAnju T bool is_offset_in_branch_range(long offset) 225ebfa50dfSAnju T { 226ebfa50dfSAnju T /* 227ebfa50dfSAnju T * Powerpc branch instruction is : 228ebfa50dfSAnju T * 229ebfa50dfSAnju T * 0 6 30 31 230ebfa50dfSAnju T * +---------+----------------+---+---+ 231ebfa50dfSAnju T * | opcode | LI |AA |LK | 232ebfa50dfSAnju T * +---------+----------------+---+---+ 233ebfa50dfSAnju T * Where AA = 0 and LK = 0 234ebfa50dfSAnju T * 235ebfa50dfSAnju T * LI is a signed 24 bits integer. The real branch offset is computed 236ebfa50dfSAnju T * by: imm32 = SignExtend(LI:'0b00', 32); 237ebfa50dfSAnju T * 238ebfa50dfSAnju T * So the maximum forward branch should be: 239ebfa50dfSAnju T * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc 240ebfa50dfSAnju T * The maximum backward branch should be: 241ebfa50dfSAnju T * (0xff800000 << 2) = 0xfe000000 = -0x2000000 242ebfa50dfSAnju T */ 243ebfa50dfSAnju T return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3)); 244ebfa50dfSAnju T } 245ebfa50dfSAnju T 24651c9c084SAnju T /* 24751c9c084SAnju T * Helper to check if a given instruction is a conditional branch 24851c9c084SAnju T * Derived from the conditional checks in analyse_instr() 24951c9c084SAnju T */ 25071f6e58eSNaveen N. Rao bool is_conditional_branch(unsigned int instr) 25151c9c084SAnju T { 25251c9c084SAnju T unsigned int opcode = instr >> 26; 25351c9c084SAnju T 25451c9c084SAnju T if (opcode == 16) /* bc, bca, bcl, bcla */ 25551c9c084SAnju T return true; 25651c9c084SAnju T if (opcode == 19) { 25751c9c084SAnju T switch ((instr >> 1) & 0x3ff) { 25851c9c084SAnju T case 16: /* bclr, bclrl */ 25951c9c084SAnju T case 528: /* bcctr, bcctrl */ 26051c9c084SAnju T case 560: /* bctar, bctarl */ 26151c9c084SAnju T return true; 26251c9c084SAnju T } 26351c9c084SAnju T } 26451c9c084SAnju T return false; 26551c9c084SAnju T } 26671f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(is_conditional_branch); 26751c9c084SAnju T 268e7a57273SMichael Ellerman unsigned int create_branch(const unsigned int *addr, 269e7a57273SMichael Ellerman unsigned long target, int flags) 270aaddd3eaSMichael Ellerman { 271aaddd3eaSMichael Ellerman unsigned int instruction; 272053a858eSMichael Ellerman long offset; 273aaddd3eaSMichael Ellerman 274053a858eSMichael Ellerman offset = target; 275aaddd3eaSMichael Ellerman if (! (flags & BRANCH_ABSOLUTE)) 276053a858eSMichael Ellerman offset = offset - (unsigned long)addr; 277053a858eSMichael Ellerman 278053a858eSMichael Ellerman /* Check we can represent the target in the instruction format */ 279ebfa50dfSAnju T if (!is_offset_in_branch_range(offset)) 280053a858eSMichael Ellerman return 0; 281aaddd3eaSMichael Ellerman 282aaddd3eaSMichael Ellerman /* Mask out the flags and target, so they don't step on each other. */ 283053a858eSMichael Ellerman instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC); 284aaddd3eaSMichael Ellerman 285e7a57273SMichael Ellerman return instruction; 286aaddd3eaSMichael Ellerman } 287411781a2SMichael Ellerman 288411781a2SMichael Ellerman unsigned int create_cond_branch(const unsigned int *addr, 289411781a2SMichael Ellerman unsigned long target, int flags) 290411781a2SMichael Ellerman { 291411781a2SMichael Ellerman unsigned int instruction; 292411781a2SMichael Ellerman long offset; 293411781a2SMichael Ellerman 294411781a2SMichael Ellerman offset = target; 295411781a2SMichael Ellerman if (! (flags & BRANCH_ABSOLUTE)) 296411781a2SMichael Ellerman offset = offset - (unsigned long)addr; 297411781a2SMichael Ellerman 298411781a2SMichael Ellerman /* Check we can represent the target in the instruction format */ 299411781a2SMichael Ellerman if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3) 300411781a2SMichael Ellerman return 0; 301411781a2SMichael Ellerman 302411781a2SMichael Ellerman /* Mask out the flags and target, so they don't step on each other. */ 303411781a2SMichael Ellerman instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC); 304411781a2SMichael Ellerman 305411781a2SMichael Ellerman return instruction; 306411781a2SMichael Ellerman } 307411781a2SMichael Ellerman 308411781a2SMichael Ellerman static unsigned int branch_opcode(unsigned int instr) 309411781a2SMichael Ellerman { 310411781a2SMichael Ellerman return (instr >> 26) & 0x3F; 311411781a2SMichael Ellerman } 312411781a2SMichael Ellerman 313411781a2SMichael Ellerman static int instr_is_branch_iform(unsigned int instr) 314411781a2SMichael Ellerman { 315411781a2SMichael Ellerman return branch_opcode(instr) == 18; 316411781a2SMichael Ellerman } 317411781a2SMichael Ellerman 318411781a2SMichael Ellerman static int instr_is_branch_bform(unsigned int instr) 319411781a2SMichael Ellerman { 320411781a2SMichael Ellerman return branch_opcode(instr) == 16; 321411781a2SMichael Ellerman } 322411781a2SMichael Ellerman 323411781a2SMichael Ellerman int instr_is_relative_branch(unsigned int instr) 324411781a2SMichael Ellerman { 325411781a2SMichael Ellerman if (instr & BRANCH_ABSOLUTE) 326411781a2SMichael Ellerman return 0; 327411781a2SMichael Ellerman 328411781a2SMichael Ellerman return instr_is_branch_iform(instr) || instr_is_branch_bform(instr); 329411781a2SMichael Ellerman } 330411781a2SMichael Ellerman 331b9eab08dSJosh Poimboeuf int instr_is_relative_link_branch(unsigned int instr) 332b9eab08dSJosh Poimboeuf { 333b9eab08dSJosh Poimboeuf return instr_is_relative_branch(instr) && (instr & BRANCH_SET_LINK); 334b9eab08dSJosh Poimboeuf } 335b9eab08dSJosh Poimboeuf 336411781a2SMichael Ellerman static unsigned long branch_iform_target(const unsigned int *instr) 337411781a2SMichael Ellerman { 338411781a2SMichael Ellerman signed long imm; 339411781a2SMichael Ellerman 340411781a2SMichael Ellerman imm = *instr & 0x3FFFFFC; 341411781a2SMichael Ellerman 342411781a2SMichael Ellerman /* If the top bit of the immediate value is set this is negative */ 343411781a2SMichael Ellerman if (imm & 0x2000000) 344411781a2SMichael Ellerman imm -= 0x4000000; 345411781a2SMichael Ellerman 346411781a2SMichael Ellerman if ((*instr & BRANCH_ABSOLUTE) == 0) 347411781a2SMichael Ellerman imm += (unsigned long)instr; 348411781a2SMichael Ellerman 349411781a2SMichael Ellerman return (unsigned long)imm; 350411781a2SMichael Ellerman } 351411781a2SMichael Ellerman 352411781a2SMichael Ellerman static unsigned long branch_bform_target(const unsigned int *instr) 353411781a2SMichael Ellerman { 354411781a2SMichael Ellerman signed long imm; 355411781a2SMichael Ellerman 356411781a2SMichael Ellerman imm = *instr & 0xFFFC; 357411781a2SMichael Ellerman 358411781a2SMichael Ellerman /* If the top bit of the immediate value is set this is negative */ 359411781a2SMichael Ellerman if (imm & 0x8000) 360411781a2SMichael Ellerman imm -= 0x10000; 361411781a2SMichael Ellerman 362411781a2SMichael Ellerman if ((*instr & BRANCH_ABSOLUTE) == 0) 363411781a2SMichael Ellerman imm += (unsigned long)instr; 364411781a2SMichael Ellerman 365411781a2SMichael Ellerman return (unsigned long)imm; 366411781a2SMichael Ellerman } 367411781a2SMichael Ellerman 368411781a2SMichael Ellerman unsigned long branch_target(const unsigned int *instr) 369411781a2SMichael Ellerman { 370411781a2SMichael Ellerman if (instr_is_branch_iform(*instr)) 371411781a2SMichael Ellerman return branch_iform_target(instr); 372411781a2SMichael Ellerman else if (instr_is_branch_bform(*instr)) 373411781a2SMichael Ellerman return branch_bform_target(instr); 374411781a2SMichael Ellerman 375411781a2SMichael Ellerman return 0; 376411781a2SMichael Ellerman } 377411781a2SMichael Ellerman 378411781a2SMichael Ellerman int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr) 379411781a2SMichael Ellerman { 380411781a2SMichael Ellerman if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr)) 381411781a2SMichael Ellerman return branch_target(instr) == addr; 382411781a2SMichael Ellerman 383411781a2SMichael Ellerman return 0; 384411781a2SMichael Ellerman } 385411781a2SMichael Ellerman 386411781a2SMichael Ellerman unsigned int translate_branch(const unsigned int *dest, const unsigned int *src) 387411781a2SMichael Ellerman { 388411781a2SMichael Ellerman unsigned long target; 389411781a2SMichael Ellerman 390411781a2SMichael Ellerman target = branch_target(src); 391411781a2SMichael Ellerman 392411781a2SMichael Ellerman if (instr_is_branch_iform(*src)) 393411781a2SMichael Ellerman return create_branch(dest, target, *src); 394411781a2SMichael Ellerman else if (instr_is_branch_bform(*src)) 395411781a2SMichael Ellerman return create_cond_branch(dest, target, *src); 396411781a2SMichael Ellerman 397411781a2SMichael Ellerman return 0; 398411781a2SMichael Ellerman } 399ae0dc736SMichael Ellerman 4001e8341aeSKevin Hao #ifdef CONFIG_PPC_BOOK3E_64 4011e8341aeSKevin Hao void __patch_exception(int exc, unsigned long addr) 4021e8341aeSKevin Hao { 4031e8341aeSKevin Hao extern unsigned int interrupt_base_book3e; 4041e8341aeSKevin Hao unsigned int *ibase = &interrupt_base_book3e; 4051e8341aeSKevin Hao 4061e8341aeSKevin Hao /* Our exceptions vectors start with a NOP and -then- a branch 4071e8341aeSKevin Hao * to deal with single stepping from userspace which stops on 4081e8341aeSKevin Hao * the second instruction. Thus we need to patch the second 4091e8341aeSKevin Hao * instruction of the exception, not the first one 4101e8341aeSKevin Hao */ 4111e8341aeSKevin Hao 4121e8341aeSKevin Hao patch_branch(ibase + (exc / 4) + 1, addr, 0); 4131e8341aeSKevin Hao } 4141e8341aeSKevin Hao #endif 415ae0dc736SMichael Ellerman 416ae0dc736SMichael Ellerman #ifdef CONFIG_CODE_PATCHING_SELFTEST 417ae0dc736SMichael Ellerman 418ae0dc736SMichael Ellerman static void __init test_trampoline(void) 419ae0dc736SMichael Ellerman { 420ae0dc736SMichael Ellerman asm ("nop;\n"); 421ae0dc736SMichael Ellerman } 422ae0dc736SMichael Ellerman 423ae0dc736SMichael Ellerman #define check(x) \ 424ae0dc736SMichael Ellerman if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__); 425ae0dc736SMichael Ellerman 426ae0dc736SMichael Ellerman static void __init test_branch_iform(void) 427ae0dc736SMichael Ellerman { 428ae0dc736SMichael Ellerman unsigned int instr; 429ae0dc736SMichael Ellerman unsigned long addr; 430ae0dc736SMichael Ellerman 431ae0dc736SMichael Ellerman addr = (unsigned long)&instr; 432ae0dc736SMichael Ellerman 433ae0dc736SMichael Ellerman /* The simplest case, branch to self, no flags */ 434ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x48000000)); 435ae0dc736SMichael Ellerman /* All bits of target set, and flags */ 436ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bffffff)); 437ae0dc736SMichael Ellerman /* High bit of opcode set, which is wrong */ 438ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0xcbffffff)); 439ae0dc736SMichael Ellerman /* Middle bits of opcode set, which is wrong */ 440ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0x7bffffff)); 441ae0dc736SMichael Ellerman 442ae0dc736SMichael Ellerman /* Simplest case, branch to self with link */ 443ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x48000001)); 444ae0dc736SMichael Ellerman /* All bits of targets set */ 445ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bfffffd)); 446ae0dc736SMichael Ellerman /* Some bits of targets set */ 447ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bff00fd)); 448ae0dc736SMichael Ellerman /* Must be a valid branch to start with */ 449ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0x7bfffffd)); 450ae0dc736SMichael Ellerman 451ae0dc736SMichael Ellerman /* Absolute branch to 0x100 */ 452ae0dc736SMichael Ellerman instr = 0x48000103; 453ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x100)); 454ae0dc736SMichael Ellerman /* Absolute branch to 0x420fc */ 455ae0dc736SMichael Ellerman instr = 0x480420ff; 456ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x420fc)); 457ae0dc736SMichael Ellerman /* Maximum positive relative branch, + 20MB - 4B */ 458ae0dc736SMichael Ellerman instr = 0x49fffffc; 459ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC)); 460ae0dc736SMichael Ellerman /* Smallest negative relative branch, - 4B */ 461ae0dc736SMichael Ellerman instr = 0x4bfffffc; 462ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 4)); 463ae0dc736SMichael Ellerman /* Largest negative relative branch, - 32 MB */ 464ae0dc736SMichael Ellerman instr = 0x4a000000; 465ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); 466ae0dc736SMichael Ellerman 467ae0dc736SMichael Ellerman /* Branch to self, with link */ 468ae0dc736SMichael Ellerman instr = create_branch(&instr, addr, BRANCH_SET_LINK); 469ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 470ae0dc736SMichael Ellerman 471ae0dc736SMichael Ellerman /* Branch to self - 0x100, with link */ 472ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK); 473ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x100)); 474ae0dc736SMichael Ellerman 475ae0dc736SMichael Ellerman /* Branch to self + 0x100, no link */ 476ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 0x100, 0); 477ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x100)); 478ae0dc736SMichael Ellerman 479ae0dc736SMichael Ellerman /* Maximum relative negative offset, - 32 MB */ 480ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK); 481ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); 482ae0dc736SMichael Ellerman 483ae0dc736SMichael Ellerman /* Out of range relative negative offset, - 32 MB + 4*/ 484ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK); 485ae0dc736SMichael Ellerman check(instr == 0); 486ae0dc736SMichael Ellerman 487ae0dc736SMichael Ellerman /* Out of range relative positive offset, + 32 MB */ 488ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK); 489ae0dc736SMichael Ellerman check(instr == 0); 490ae0dc736SMichael Ellerman 491ae0dc736SMichael Ellerman /* Unaligned target */ 492ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK); 493ae0dc736SMichael Ellerman check(instr == 0); 494ae0dc736SMichael Ellerman 495ae0dc736SMichael Ellerman /* Check flags are masked correctly */ 496ae0dc736SMichael Ellerman instr = create_branch(&instr, addr, 0xFFFFFFFC); 497ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 498ae0dc736SMichael Ellerman check(instr == 0x48000000); 499ae0dc736SMichael Ellerman } 500ae0dc736SMichael Ellerman 501ae0dc736SMichael Ellerman static void __init test_create_function_call(void) 502ae0dc736SMichael Ellerman { 503ae0dc736SMichael Ellerman unsigned int *iptr; 504ae0dc736SMichael Ellerman unsigned long dest; 505ae0dc736SMichael Ellerman 506ae0dc736SMichael Ellerman /* Check we can create a function call */ 507ae0dc736SMichael Ellerman iptr = (unsigned int *)ppc_function_entry(test_trampoline); 508ae0dc736SMichael Ellerman dest = ppc_function_entry(test_create_function_call); 509ae0dc736SMichael Ellerman patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK)); 510ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(iptr, dest)); 511ae0dc736SMichael Ellerman } 512ae0dc736SMichael Ellerman 513ae0dc736SMichael Ellerman static void __init test_branch_bform(void) 514ae0dc736SMichael Ellerman { 515ae0dc736SMichael Ellerman unsigned long addr; 516ae0dc736SMichael Ellerman unsigned int *iptr, instr, flags; 517ae0dc736SMichael Ellerman 518ae0dc736SMichael Ellerman iptr = &instr; 519ae0dc736SMichael Ellerman addr = (unsigned long)iptr; 520ae0dc736SMichael Ellerman 521ae0dc736SMichael Ellerman /* The simplest case, branch to self, no flags */ 522ae0dc736SMichael Ellerman check(instr_is_branch_bform(0x40000000)); 523ae0dc736SMichael Ellerman /* All bits of target set, and flags */ 524ae0dc736SMichael Ellerman check(instr_is_branch_bform(0x43ffffff)); 525ae0dc736SMichael Ellerman /* High bit of opcode set, which is wrong */ 526ae0dc736SMichael Ellerman check(!instr_is_branch_bform(0xc3ffffff)); 527ae0dc736SMichael Ellerman /* Middle bits of opcode set, which is wrong */ 528ae0dc736SMichael Ellerman check(!instr_is_branch_bform(0x7bffffff)); 529ae0dc736SMichael Ellerman 530ae0dc736SMichael Ellerman /* Absolute conditional branch to 0x100 */ 531ae0dc736SMichael Ellerman instr = 0x43ff0103; 532ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x100)); 533ae0dc736SMichael Ellerman /* Absolute conditional branch to 0x20fc */ 534ae0dc736SMichael Ellerman instr = 0x43ff20ff; 535ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x20fc)); 536ae0dc736SMichael Ellerman /* Maximum positive relative conditional branch, + 32 KB - 4B */ 537ae0dc736SMichael Ellerman instr = 0x43ff7ffc; 538ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x7FFC)); 539ae0dc736SMichael Ellerman /* Smallest negative relative conditional branch, - 4B */ 540ae0dc736SMichael Ellerman instr = 0x43fffffc; 541ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 4)); 542ae0dc736SMichael Ellerman /* Largest negative relative conditional branch, - 32 KB */ 543ae0dc736SMichael Ellerman instr = 0x43ff8000; 544ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x8000)); 545ae0dc736SMichael Ellerman 546ae0dc736SMichael Ellerman /* All condition code bits set & link */ 547ae0dc736SMichael Ellerman flags = 0x3ff000 | BRANCH_SET_LINK; 548ae0dc736SMichael Ellerman 549ae0dc736SMichael Ellerman /* Branch to self */ 550ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr, flags); 551ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 552ae0dc736SMichael Ellerman 553ae0dc736SMichael Ellerman /* Branch to self - 0x100 */ 554ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x100, flags); 555ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x100)); 556ae0dc736SMichael Ellerman 557ae0dc736SMichael Ellerman /* Branch to self + 0x100 */ 558ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 0x100, flags); 559ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x100)); 560ae0dc736SMichael Ellerman 561ae0dc736SMichael Ellerman /* Maximum relative negative offset, - 32 KB */ 562ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x8000, flags); 563ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x8000)); 564ae0dc736SMichael Ellerman 565ae0dc736SMichael Ellerman /* Out of range relative negative offset, - 32 KB + 4*/ 566ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x8004, flags); 567ae0dc736SMichael Ellerman check(instr == 0); 568ae0dc736SMichael Ellerman 569ae0dc736SMichael Ellerman /* Out of range relative positive offset, + 32 KB */ 570ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 0x8000, flags); 571ae0dc736SMichael Ellerman check(instr == 0); 572ae0dc736SMichael Ellerman 573ae0dc736SMichael Ellerman /* Unaligned target */ 574ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 3, flags); 575ae0dc736SMichael Ellerman check(instr == 0); 576ae0dc736SMichael Ellerman 577ae0dc736SMichael Ellerman /* Check flags are masked correctly */ 578ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr, 0xFFFFFFFC); 579ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 580ae0dc736SMichael Ellerman check(instr == 0x43FF0000); 581ae0dc736SMichael Ellerman } 582ae0dc736SMichael Ellerman 583ae0dc736SMichael Ellerman static void __init test_translate_branch(void) 584ae0dc736SMichael Ellerman { 585ae0dc736SMichael Ellerman unsigned long addr; 586ae0dc736SMichael Ellerman unsigned int *p, *q; 587ae0dc736SMichael Ellerman void *buf; 588ae0dc736SMichael Ellerman 589ae0dc736SMichael Ellerman buf = vmalloc(PAGE_ALIGN(0x2000000 + 1)); 590ae0dc736SMichael Ellerman check(buf); 591ae0dc736SMichael Ellerman if (!buf) 592ae0dc736SMichael Ellerman return; 593ae0dc736SMichael Ellerman 594ae0dc736SMichael Ellerman /* Simple case, branch to self moved a little */ 595ae0dc736SMichael Ellerman p = buf; 596ae0dc736SMichael Ellerman addr = (unsigned long)p; 597ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 598ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 599ae0dc736SMichael Ellerman q = p + 1; 600ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 601ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 602ae0dc736SMichael Ellerman 603ae0dc736SMichael Ellerman /* Maximum negative case, move b . to addr + 32 MB */ 604ae0dc736SMichael Ellerman p = buf; 605ae0dc736SMichael Ellerman addr = (unsigned long)p; 606ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 607ae0dc736SMichael Ellerman q = buf + 0x2000000; 608ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 609ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 610ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 611ae0dc736SMichael Ellerman check(*q == 0x4a000000); 612ae0dc736SMichael Ellerman 613ae0dc736SMichael Ellerman /* Maximum positive case, move x to x - 32 MB + 4 */ 614ae0dc736SMichael Ellerman p = buf + 0x2000000; 615ae0dc736SMichael Ellerman addr = (unsigned long)p; 616ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 617ae0dc736SMichael Ellerman q = buf + 4; 618ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 619ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 620ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 621ae0dc736SMichael Ellerman check(*q == 0x49fffffc); 622ae0dc736SMichael Ellerman 623ae0dc736SMichael Ellerman /* Jump to x + 16 MB moved to x + 20 MB */ 624ae0dc736SMichael Ellerman p = buf; 625ae0dc736SMichael Ellerman addr = 0x1000000 + (unsigned long)buf; 626ae0dc736SMichael Ellerman patch_branch(p, addr, BRANCH_SET_LINK); 627ae0dc736SMichael Ellerman q = buf + 0x1400000; 628ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 629ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 630ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 631ae0dc736SMichael Ellerman 632ae0dc736SMichael Ellerman /* Jump to x + 16 MB moved to x - 16 MB + 4 */ 633ae0dc736SMichael Ellerman p = buf + 0x1000000; 634ae0dc736SMichael Ellerman addr = 0x2000000 + (unsigned long)buf; 635ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 636ae0dc736SMichael Ellerman q = buf + 4; 637ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 638ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 639ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 640ae0dc736SMichael Ellerman 641ae0dc736SMichael Ellerman 642ae0dc736SMichael Ellerman /* Conditional branch tests */ 643ae0dc736SMichael Ellerman 644ae0dc736SMichael Ellerman /* Simple case, branch to self moved a little */ 645ae0dc736SMichael Ellerman p = buf; 646ae0dc736SMichael Ellerman addr = (unsigned long)p; 647ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0)); 648ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 649ae0dc736SMichael Ellerman q = p + 1; 650ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 651ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 652ae0dc736SMichael Ellerman 653ae0dc736SMichael Ellerman /* Maximum negative case, move b . to addr + 32 KB */ 654ae0dc736SMichael Ellerman p = buf; 655ae0dc736SMichael Ellerman addr = (unsigned long)p; 656ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC)); 657ae0dc736SMichael Ellerman q = buf + 0x8000; 658ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 659ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 660ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 661ae0dc736SMichael Ellerman check(*q == 0x43ff8000); 662ae0dc736SMichael Ellerman 663ae0dc736SMichael Ellerman /* Maximum positive case, move x to x - 32 KB + 4 */ 664ae0dc736SMichael Ellerman p = buf + 0x8000; 665ae0dc736SMichael Ellerman addr = (unsigned long)p; 666ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC)); 667ae0dc736SMichael Ellerman q = buf + 4; 668ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 669ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 670ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 671ae0dc736SMichael Ellerman check(*q == 0x43ff7ffc); 672ae0dc736SMichael Ellerman 673ae0dc736SMichael Ellerman /* Jump to x + 12 KB moved to x + 20 KB */ 674ae0dc736SMichael Ellerman p = buf; 675ae0dc736SMichael Ellerman addr = 0x3000 + (unsigned long)buf; 676ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK)); 677ae0dc736SMichael Ellerman q = buf + 0x5000; 678ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 679ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 680ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 681ae0dc736SMichael Ellerman 682ae0dc736SMichael Ellerman /* Jump to x + 8 KB moved to x - 8 KB + 4 */ 683ae0dc736SMichael Ellerman p = buf + 0x2000; 684ae0dc736SMichael Ellerman addr = 0x4000 + (unsigned long)buf; 685ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0)); 686ae0dc736SMichael Ellerman q = buf + 4; 687ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 688ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 689ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 690ae0dc736SMichael Ellerman 691ae0dc736SMichael Ellerman /* Free the buffer we were using */ 692ae0dc736SMichael Ellerman vfree(buf); 693ae0dc736SMichael Ellerman } 694ae0dc736SMichael Ellerman 695ae0dc736SMichael Ellerman static int __init test_code_patching(void) 696ae0dc736SMichael Ellerman { 697ae0dc736SMichael Ellerman printk(KERN_DEBUG "Running code patching self-tests ...\n"); 698ae0dc736SMichael Ellerman 699ae0dc736SMichael Ellerman test_branch_iform(); 700ae0dc736SMichael Ellerman test_branch_bform(); 701ae0dc736SMichael Ellerman test_create_function_call(); 702ae0dc736SMichael Ellerman test_translate_branch(); 703ae0dc736SMichael Ellerman 704ae0dc736SMichael Ellerman return 0; 705ae0dc736SMichael Ellerman } 706ae0dc736SMichael Ellerman late_initcall(test_code_patching); 707ae0dc736SMichael Ellerman 708ae0dc736SMichael Ellerman #endif /* CONFIG_CODE_PATCHING_SELFTEST */ 709