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> 15*37bc3e5fSBalbir Singh #include <linux/cpuhotplug.h> 16*37bc3e5fSBalbir Singh #include <linux/slab.h> 177c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 187d134b2cSLuis R. Rodriguez #include <linux/kprobes.h> 19aaddd3eaSMichael Ellerman 20*37bc3e5fSBalbir Singh #include <asm/pgtable.h> 21*37bc3e5fSBalbir Singh #include <asm/tlbflush.h> 22*37bc3e5fSBalbir Singh #include <asm/page.h> 23*37bc3e5fSBalbir Singh #include <asm/code-patching.h> 24aaddd3eaSMichael Ellerman 25*37bc3e5fSBalbir Singh static int __patch_instruction(unsigned int *addr, unsigned int instr) 26aaddd3eaSMichael Ellerman { 27b6e37968SSteven Rostedt int err; 28b6e37968SSteven Rostedt 29636802efSBenjamin Herrenschmidt __put_user_size(instr, addr, 4, err); 30b6e37968SSteven Rostedt if (err) 31b6e37968SSteven Rostedt return err; 32*37bc3e5fSBalbir Singh 33e7a57273SMichael Ellerman asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" :: "r" (addr)); 34*37bc3e5fSBalbir Singh 35b6e37968SSteven Rostedt return 0; 36aaddd3eaSMichael Ellerman } 37aaddd3eaSMichael Ellerman 38*37bc3e5fSBalbir Singh #ifdef CONFIG_STRICT_KERNEL_RWX 39*37bc3e5fSBalbir Singh static DEFINE_PER_CPU(struct vm_struct *, text_poke_area); 40*37bc3e5fSBalbir Singh 41*37bc3e5fSBalbir Singh static int text_area_cpu_up(unsigned int cpu) 42*37bc3e5fSBalbir Singh { 43*37bc3e5fSBalbir Singh struct vm_struct *area; 44*37bc3e5fSBalbir Singh 45*37bc3e5fSBalbir Singh area = get_vm_area(PAGE_SIZE, VM_ALLOC); 46*37bc3e5fSBalbir Singh if (!area) { 47*37bc3e5fSBalbir Singh WARN_ONCE(1, "Failed to create text area for cpu %d\n", 48*37bc3e5fSBalbir Singh cpu); 49*37bc3e5fSBalbir Singh return -1; 50*37bc3e5fSBalbir Singh } 51*37bc3e5fSBalbir Singh this_cpu_write(text_poke_area, area); 52*37bc3e5fSBalbir Singh 53*37bc3e5fSBalbir Singh return 0; 54*37bc3e5fSBalbir Singh } 55*37bc3e5fSBalbir Singh 56*37bc3e5fSBalbir Singh static int text_area_cpu_down(unsigned int cpu) 57*37bc3e5fSBalbir Singh { 58*37bc3e5fSBalbir Singh free_vm_area(this_cpu_read(text_poke_area)); 59*37bc3e5fSBalbir Singh return 0; 60*37bc3e5fSBalbir Singh } 61*37bc3e5fSBalbir Singh 62*37bc3e5fSBalbir Singh /* 63*37bc3e5fSBalbir Singh * Run as a late init call. This allows all the boot time patching to be done 64*37bc3e5fSBalbir Singh * simply by patching the code, and then we're called here prior to 65*37bc3e5fSBalbir Singh * mark_rodata_ro(), which happens after all init calls are run. Although 66*37bc3e5fSBalbir Singh * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge 67*37bc3e5fSBalbir Singh * it as being preferable to a kernel that will crash later when someone tries 68*37bc3e5fSBalbir Singh * to use patch_instruction(). 69*37bc3e5fSBalbir Singh */ 70*37bc3e5fSBalbir Singh static int __init setup_text_poke_area(void) 71*37bc3e5fSBalbir Singh { 72*37bc3e5fSBalbir Singh BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, 73*37bc3e5fSBalbir Singh "powerpc/text_poke:online", text_area_cpu_up, 74*37bc3e5fSBalbir Singh text_area_cpu_down)); 75*37bc3e5fSBalbir Singh 76*37bc3e5fSBalbir Singh return 0; 77*37bc3e5fSBalbir Singh } 78*37bc3e5fSBalbir Singh late_initcall(setup_text_poke_area); 79*37bc3e5fSBalbir Singh 80*37bc3e5fSBalbir Singh /* 81*37bc3e5fSBalbir Singh * This can be called for kernel text or a module. 82*37bc3e5fSBalbir Singh */ 83*37bc3e5fSBalbir Singh static int map_patch_area(void *addr, unsigned long text_poke_addr) 84*37bc3e5fSBalbir Singh { 85*37bc3e5fSBalbir Singh unsigned long pfn; 86*37bc3e5fSBalbir Singh int err; 87*37bc3e5fSBalbir Singh 88*37bc3e5fSBalbir Singh if (is_vmalloc_addr(addr)) 89*37bc3e5fSBalbir Singh pfn = vmalloc_to_pfn(addr); 90*37bc3e5fSBalbir Singh else 91*37bc3e5fSBalbir Singh pfn = __pa_symbol(addr) >> PAGE_SHIFT; 92*37bc3e5fSBalbir Singh 93*37bc3e5fSBalbir Singh err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), 94*37bc3e5fSBalbir Singh pgprot_val(PAGE_KERNEL)); 95*37bc3e5fSBalbir Singh 96*37bc3e5fSBalbir Singh pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err); 97*37bc3e5fSBalbir Singh if (err) 98*37bc3e5fSBalbir Singh return -1; 99*37bc3e5fSBalbir Singh 100*37bc3e5fSBalbir Singh return 0; 101*37bc3e5fSBalbir Singh } 102*37bc3e5fSBalbir Singh 103*37bc3e5fSBalbir Singh static inline int unmap_patch_area(unsigned long addr) 104*37bc3e5fSBalbir Singh { 105*37bc3e5fSBalbir Singh pte_t *ptep; 106*37bc3e5fSBalbir Singh pmd_t *pmdp; 107*37bc3e5fSBalbir Singh pud_t *pudp; 108*37bc3e5fSBalbir Singh pgd_t *pgdp; 109*37bc3e5fSBalbir Singh 110*37bc3e5fSBalbir Singh pgdp = pgd_offset_k(addr); 111*37bc3e5fSBalbir Singh if (unlikely(!pgdp)) 112*37bc3e5fSBalbir Singh return -EINVAL; 113*37bc3e5fSBalbir Singh 114*37bc3e5fSBalbir Singh pudp = pud_offset(pgdp, addr); 115*37bc3e5fSBalbir Singh if (unlikely(!pudp)) 116*37bc3e5fSBalbir Singh return -EINVAL; 117*37bc3e5fSBalbir Singh 118*37bc3e5fSBalbir Singh pmdp = pmd_offset(pudp, addr); 119*37bc3e5fSBalbir Singh if (unlikely(!pmdp)) 120*37bc3e5fSBalbir Singh return -EINVAL; 121*37bc3e5fSBalbir Singh 122*37bc3e5fSBalbir Singh ptep = pte_offset_kernel(pmdp, addr); 123*37bc3e5fSBalbir Singh if (unlikely(!ptep)) 124*37bc3e5fSBalbir Singh return -EINVAL; 125*37bc3e5fSBalbir Singh 126*37bc3e5fSBalbir Singh pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr); 127*37bc3e5fSBalbir Singh 128*37bc3e5fSBalbir Singh /* 129*37bc3e5fSBalbir Singh * In hash, pte_clear flushes the tlb, in radix, we have to 130*37bc3e5fSBalbir Singh */ 131*37bc3e5fSBalbir Singh pte_clear(&init_mm, addr, ptep); 132*37bc3e5fSBalbir Singh flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 133*37bc3e5fSBalbir Singh 134*37bc3e5fSBalbir Singh return 0; 135*37bc3e5fSBalbir Singh } 136*37bc3e5fSBalbir Singh 137*37bc3e5fSBalbir Singh int patch_instruction(unsigned int *addr, unsigned int instr) 138*37bc3e5fSBalbir Singh { 139*37bc3e5fSBalbir Singh int err; 140*37bc3e5fSBalbir Singh unsigned int *dest = NULL; 141*37bc3e5fSBalbir Singh unsigned long flags; 142*37bc3e5fSBalbir Singh unsigned long text_poke_addr; 143*37bc3e5fSBalbir Singh unsigned long kaddr = (unsigned long)addr; 144*37bc3e5fSBalbir Singh 145*37bc3e5fSBalbir Singh /* 146*37bc3e5fSBalbir Singh * During early early boot patch_instruction is called 147*37bc3e5fSBalbir Singh * when text_poke_area is not ready, but we still need 148*37bc3e5fSBalbir Singh * to allow patching. We just do the plain old patching 149*37bc3e5fSBalbir Singh * We use slab_is_available and per cpu read * via this_cpu_read 150*37bc3e5fSBalbir Singh * of text_poke_area. Per-CPU areas might not be up early 151*37bc3e5fSBalbir Singh * this can create problems with just using this_cpu_read() 152*37bc3e5fSBalbir Singh */ 153*37bc3e5fSBalbir Singh if (!slab_is_available() || !this_cpu_read(text_poke_area)) 154*37bc3e5fSBalbir Singh return __patch_instruction(addr, instr); 155*37bc3e5fSBalbir Singh 156*37bc3e5fSBalbir Singh local_irq_save(flags); 157*37bc3e5fSBalbir Singh 158*37bc3e5fSBalbir Singh text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr; 159*37bc3e5fSBalbir Singh if (map_patch_area(addr, text_poke_addr)) { 160*37bc3e5fSBalbir Singh err = -1; 161*37bc3e5fSBalbir Singh goto out; 162*37bc3e5fSBalbir Singh } 163*37bc3e5fSBalbir Singh 164*37bc3e5fSBalbir Singh dest = (unsigned int *)(text_poke_addr) + 165*37bc3e5fSBalbir Singh ((kaddr & ~PAGE_MASK) / sizeof(unsigned int)); 166*37bc3e5fSBalbir Singh 167*37bc3e5fSBalbir Singh /* 168*37bc3e5fSBalbir Singh * We use __put_user_size so that we can handle faults while 169*37bc3e5fSBalbir Singh * writing to dest and return err to handle faults gracefully 170*37bc3e5fSBalbir Singh */ 171*37bc3e5fSBalbir Singh __put_user_size(instr, dest, 4, err); 172*37bc3e5fSBalbir Singh if (!err) 173*37bc3e5fSBalbir Singh asm ("dcbst 0, %0; sync; icbi 0,%0; icbi 0,%1; sync; isync" 174*37bc3e5fSBalbir Singh ::"r" (dest), "r"(addr)); 175*37bc3e5fSBalbir Singh 176*37bc3e5fSBalbir Singh err = unmap_patch_area(text_poke_addr); 177*37bc3e5fSBalbir Singh if (err) 178*37bc3e5fSBalbir Singh pr_warn("failed to unmap %lx\n", text_poke_addr); 179*37bc3e5fSBalbir Singh 180*37bc3e5fSBalbir Singh out: 181*37bc3e5fSBalbir Singh local_irq_restore(flags); 182*37bc3e5fSBalbir Singh 183*37bc3e5fSBalbir Singh return err; 184*37bc3e5fSBalbir Singh } 185*37bc3e5fSBalbir Singh #else /* !CONFIG_STRICT_KERNEL_RWX */ 186*37bc3e5fSBalbir Singh 187*37bc3e5fSBalbir Singh int patch_instruction(unsigned int *addr, unsigned int instr) 188*37bc3e5fSBalbir Singh { 189*37bc3e5fSBalbir Singh return __patch_instruction(addr, instr); 190*37bc3e5fSBalbir Singh } 191*37bc3e5fSBalbir Singh 192*37bc3e5fSBalbir Singh #endif /* CONFIG_STRICT_KERNEL_RWX */ 193*37bc3e5fSBalbir Singh NOKPROBE_SYMBOL(patch_instruction); 194*37bc3e5fSBalbir Singh 195b6e37968SSteven Rostedt int patch_branch(unsigned int *addr, unsigned long target, int flags) 196e7a57273SMichael Ellerman { 197b6e37968SSteven Rostedt return patch_instruction(addr, create_branch(addr, target, flags)); 198e7a57273SMichael Ellerman } 199e7a57273SMichael Ellerman 200ebfa50dfSAnju T bool is_offset_in_branch_range(long offset) 201ebfa50dfSAnju T { 202ebfa50dfSAnju T /* 203ebfa50dfSAnju T * Powerpc branch instruction is : 204ebfa50dfSAnju T * 205ebfa50dfSAnju T * 0 6 30 31 206ebfa50dfSAnju T * +---------+----------------+---+---+ 207ebfa50dfSAnju T * | opcode | LI |AA |LK | 208ebfa50dfSAnju T * +---------+----------------+---+---+ 209ebfa50dfSAnju T * Where AA = 0 and LK = 0 210ebfa50dfSAnju T * 211ebfa50dfSAnju T * LI is a signed 24 bits integer. The real branch offset is computed 212ebfa50dfSAnju T * by: imm32 = SignExtend(LI:'0b00', 32); 213ebfa50dfSAnju T * 214ebfa50dfSAnju T * So the maximum forward branch should be: 215ebfa50dfSAnju T * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc 216ebfa50dfSAnju T * The maximum backward branch should be: 217ebfa50dfSAnju T * (0xff800000 << 2) = 0xfe000000 = -0x2000000 218ebfa50dfSAnju T */ 219ebfa50dfSAnju T return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3)); 220ebfa50dfSAnju T } 221ebfa50dfSAnju T 22251c9c084SAnju T /* 22351c9c084SAnju T * Helper to check if a given instruction is a conditional branch 22451c9c084SAnju T * Derived from the conditional checks in analyse_instr() 22551c9c084SAnju T */ 22671f6e58eSNaveen N. Rao bool is_conditional_branch(unsigned int instr) 22751c9c084SAnju T { 22851c9c084SAnju T unsigned int opcode = instr >> 26; 22951c9c084SAnju T 23051c9c084SAnju T if (opcode == 16) /* bc, bca, bcl, bcla */ 23151c9c084SAnju T return true; 23251c9c084SAnju T if (opcode == 19) { 23351c9c084SAnju T switch ((instr >> 1) & 0x3ff) { 23451c9c084SAnju T case 16: /* bclr, bclrl */ 23551c9c084SAnju T case 528: /* bcctr, bcctrl */ 23651c9c084SAnju T case 560: /* bctar, bctarl */ 23751c9c084SAnju T return true; 23851c9c084SAnju T } 23951c9c084SAnju T } 24051c9c084SAnju T return false; 24151c9c084SAnju T } 24271f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(is_conditional_branch); 24351c9c084SAnju T 244e7a57273SMichael Ellerman unsigned int create_branch(const unsigned int *addr, 245e7a57273SMichael Ellerman unsigned long target, int flags) 246aaddd3eaSMichael Ellerman { 247aaddd3eaSMichael Ellerman unsigned int instruction; 248053a858eSMichael Ellerman long offset; 249aaddd3eaSMichael Ellerman 250053a858eSMichael Ellerman offset = target; 251aaddd3eaSMichael Ellerman if (! (flags & BRANCH_ABSOLUTE)) 252053a858eSMichael Ellerman offset = offset - (unsigned long)addr; 253053a858eSMichael Ellerman 254053a858eSMichael Ellerman /* Check we can represent the target in the instruction format */ 255ebfa50dfSAnju T if (!is_offset_in_branch_range(offset)) 256053a858eSMichael Ellerman return 0; 257aaddd3eaSMichael Ellerman 258aaddd3eaSMichael Ellerman /* Mask out the flags and target, so they don't step on each other. */ 259053a858eSMichael Ellerman instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC); 260aaddd3eaSMichael Ellerman 261e7a57273SMichael Ellerman return instruction; 262aaddd3eaSMichael Ellerman } 263411781a2SMichael Ellerman 264411781a2SMichael Ellerman unsigned int create_cond_branch(const unsigned int *addr, 265411781a2SMichael Ellerman unsigned long target, int flags) 266411781a2SMichael Ellerman { 267411781a2SMichael Ellerman unsigned int instruction; 268411781a2SMichael Ellerman long offset; 269411781a2SMichael Ellerman 270411781a2SMichael Ellerman offset = target; 271411781a2SMichael Ellerman if (! (flags & BRANCH_ABSOLUTE)) 272411781a2SMichael Ellerman offset = offset - (unsigned long)addr; 273411781a2SMichael Ellerman 274411781a2SMichael Ellerman /* Check we can represent the target in the instruction format */ 275411781a2SMichael Ellerman if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3) 276411781a2SMichael Ellerman return 0; 277411781a2SMichael Ellerman 278411781a2SMichael Ellerman /* Mask out the flags and target, so they don't step on each other. */ 279411781a2SMichael Ellerman instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC); 280411781a2SMichael Ellerman 281411781a2SMichael Ellerman return instruction; 282411781a2SMichael Ellerman } 283411781a2SMichael Ellerman 284411781a2SMichael Ellerman static unsigned int branch_opcode(unsigned int instr) 285411781a2SMichael Ellerman { 286411781a2SMichael Ellerman return (instr >> 26) & 0x3F; 287411781a2SMichael Ellerman } 288411781a2SMichael Ellerman 289411781a2SMichael Ellerman static int instr_is_branch_iform(unsigned int instr) 290411781a2SMichael Ellerman { 291411781a2SMichael Ellerman return branch_opcode(instr) == 18; 292411781a2SMichael Ellerman } 293411781a2SMichael Ellerman 294411781a2SMichael Ellerman static int instr_is_branch_bform(unsigned int instr) 295411781a2SMichael Ellerman { 296411781a2SMichael Ellerman return branch_opcode(instr) == 16; 297411781a2SMichael Ellerman } 298411781a2SMichael Ellerman 299411781a2SMichael Ellerman int instr_is_relative_branch(unsigned int instr) 300411781a2SMichael Ellerman { 301411781a2SMichael Ellerman if (instr & BRANCH_ABSOLUTE) 302411781a2SMichael Ellerman return 0; 303411781a2SMichael Ellerman 304411781a2SMichael Ellerman return instr_is_branch_iform(instr) || instr_is_branch_bform(instr); 305411781a2SMichael Ellerman } 306411781a2SMichael Ellerman 307411781a2SMichael Ellerman static unsigned long branch_iform_target(const unsigned int *instr) 308411781a2SMichael Ellerman { 309411781a2SMichael Ellerman signed long imm; 310411781a2SMichael Ellerman 311411781a2SMichael Ellerman imm = *instr & 0x3FFFFFC; 312411781a2SMichael Ellerman 313411781a2SMichael Ellerman /* If the top bit of the immediate value is set this is negative */ 314411781a2SMichael Ellerman if (imm & 0x2000000) 315411781a2SMichael Ellerman imm -= 0x4000000; 316411781a2SMichael Ellerman 317411781a2SMichael Ellerman if ((*instr & BRANCH_ABSOLUTE) == 0) 318411781a2SMichael Ellerman imm += (unsigned long)instr; 319411781a2SMichael Ellerman 320411781a2SMichael Ellerman return (unsigned long)imm; 321411781a2SMichael Ellerman } 322411781a2SMichael Ellerman 323411781a2SMichael Ellerman static unsigned long branch_bform_target(const unsigned int *instr) 324411781a2SMichael Ellerman { 325411781a2SMichael Ellerman signed long imm; 326411781a2SMichael Ellerman 327411781a2SMichael Ellerman imm = *instr & 0xFFFC; 328411781a2SMichael Ellerman 329411781a2SMichael Ellerman /* If the top bit of the immediate value is set this is negative */ 330411781a2SMichael Ellerman if (imm & 0x8000) 331411781a2SMichael Ellerman imm -= 0x10000; 332411781a2SMichael Ellerman 333411781a2SMichael Ellerman if ((*instr & BRANCH_ABSOLUTE) == 0) 334411781a2SMichael Ellerman imm += (unsigned long)instr; 335411781a2SMichael Ellerman 336411781a2SMichael Ellerman return (unsigned long)imm; 337411781a2SMichael Ellerman } 338411781a2SMichael Ellerman 339411781a2SMichael Ellerman unsigned long branch_target(const unsigned int *instr) 340411781a2SMichael Ellerman { 341411781a2SMichael Ellerman if (instr_is_branch_iform(*instr)) 342411781a2SMichael Ellerman return branch_iform_target(instr); 343411781a2SMichael Ellerman else if (instr_is_branch_bform(*instr)) 344411781a2SMichael Ellerman return branch_bform_target(instr); 345411781a2SMichael Ellerman 346411781a2SMichael Ellerman return 0; 347411781a2SMichael Ellerman } 348411781a2SMichael Ellerman 349411781a2SMichael Ellerman int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr) 350411781a2SMichael Ellerman { 351411781a2SMichael Ellerman if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr)) 352411781a2SMichael Ellerman return branch_target(instr) == addr; 353411781a2SMichael Ellerman 354411781a2SMichael Ellerman return 0; 355411781a2SMichael Ellerman } 356411781a2SMichael Ellerman 357411781a2SMichael Ellerman unsigned int translate_branch(const unsigned int *dest, const unsigned int *src) 358411781a2SMichael Ellerman { 359411781a2SMichael Ellerman unsigned long target; 360411781a2SMichael Ellerman 361411781a2SMichael Ellerman target = branch_target(src); 362411781a2SMichael Ellerman 363411781a2SMichael Ellerman if (instr_is_branch_iform(*src)) 364411781a2SMichael Ellerman return create_branch(dest, target, *src); 365411781a2SMichael Ellerman else if (instr_is_branch_bform(*src)) 366411781a2SMichael Ellerman return create_cond_branch(dest, target, *src); 367411781a2SMichael Ellerman 368411781a2SMichael Ellerman return 0; 369411781a2SMichael Ellerman } 370ae0dc736SMichael Ellerman 3711e8341aeSKevin Hao #ifdef CONFIG_PPC_BOOK3E_64 3721e8341aeSKevin Hao void __patch_exception(int exc, unsigned long addr) 3731e8341aeSKevin Hao { 3741e8341aeSKevin Hao extern unsigned int interrupt_base_book3e; 3751e8341aeSKevin Hao unsigned int *ibase = &interrupt_base_book3e; 3761e8341aeSKevin Hao 3771e8341aeSKevin Hao /* Our exceptions vectors start with a NOP and -then- a branch 3781e8341aeSKevin Hao * to deal with single stepping from userspace which stops on 3791e8341aeSKevin Hao * the second instruction. Thus we need to patch the second 3801e8341aeSKevin Hao * instruction of the exception, not the first one 3811e8341aeSKevin Hao */ 3821e8341aeSKevin Hao 3831e8341aeSKevin Hao patch_branch(ibase + (exc / 4) + 1, addr, 0); 3841e8341aeSKevin Hao } 3851e8341aeSKevin Hao #endif 386ae0dc736SMichael Ellerman 387ae0dc736SMichael Ellerman #ifdef CONFIG_CODE_PATCHING_SELFTEST 388ae0dc736SMichael Ellerman 389ae0dc736SMichael Ellerman static void __init test_trampoline(void) 390ae0dc736SMichael Ellerman { 391ae0dc736SMichael Ellerman asm ("nop;\n"); 392ae0dc736SMichael Ellerman } 393ae0dc736SMichael Ellerman 394ae0dc736SMichael Ellerman #define check(x) \ 395ae0dc736SMichael Ellerman if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__); 396ae0dc736SMichael Ellerman 397ae0dc736SMichael Ellerman static void __init test_branch_iform(void) 398ae0dc736SMichael Ellerman { 399ae0dc736SMichael Ellerman unsigned int instr; 400ae0dc736SMichael Ellerman unsigned long addr; 401ae0dc736SMichael Ellerman 402ae0dc736SMichael Ellerman addr = (unsigned long)&instr; 403ae0dc736SMichael Ellerman 404ae0dc736SMichael Ellerman /* The simplest case, branch to self, no flags */ 405ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x48000000)); 406ae0dc736SMichael Ellerman /* All bits of target set, and flags */ 407ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bffffff)); 408ae0dc736SMichael Ellerman /* High bit of opcode set, which is wrong */ 409ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0xcbffffff)); 410ae0dc736SMichael Ellerman /* Middle bits of opcode set, which is wrong */ 411ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0x7bffffff)); 412ae0dc736SMichael Ellerman 413ae0dc736SMichael Ellerman /* Simplest case, branch to self with link */ 414ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x48000001)); 415ae0dc736SMichael Ellerman /* All bits of targets set */ 416ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bfffffd)); 417ae0dc736SMichael Ellerman /* Some bits of targets set */ 418ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bff00fd)); 419ae0dc736SMichael Ellerman /* Must be a valid branch to start with */ 420ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0x7bfffffd)); 421ae0dc736SMichael Ellerman 422ae0dc736SMichael Ellerman /* Absolute branch to 0x100 */ 423ae0dc736SMichael Ellerman instr = 0x48000103; 424ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x100)); 425ae0dc736SMichael Ellerman /* Absolute branch to 0x420fc */ 426ae0dc736SMichael Ellerman instr = 0x480420ff; 427ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x420fc)); 428ae0dc736SMichael Ellerman /* Maximum positive relative branch, + 20MB - 4B */ 429ae0dc736SMichael Ellerman instr = 0x49fffffc; 430ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC)); 431ae0dc736SMichael Ellerman /* Smallest negative relative branch, - 4B */ 432ae0dc736SMichael Ellerman instr = 0x4bfffffc; 433ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 4)); 434ae0dc736SMichael Ellerman /* Largest negative relative branch, - 32 MB */ 435ae0dc736SMichael Ellerman instr = 0x4a000000; 436ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); 437ae0dc736SMichael Ellerman 438ae0dc736SMichael Ellerman /* Branch to self, with link */ 439ae0dc736SMichael Ellerman instr = create_branch(&instr, addr, BRANCH_SET_LINK); 440ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 441ae0dc736SMichael Ellerman 442ae0dc736SMichael Ellerman /* Branch to self - 0x100, with link */ 443ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK); 444ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x100)); 445ae0dc736SMichael Ellerman 446ae0dc736SMichael Ellerman /* Branch to self + 0x100, no link */ 447ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 0x100, 0); 448ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x100)); 449ae0dc736SMichael Ellerman 450ae0dc736SMichael Ellerman /* Maximum relative negative offset, - 32 MB */ 451ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK); 452ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); 453ae0dc736SMichael Ellerman 454ae0dc736SMichael Ellerman /* Out of range relative negative offset, - 32 MB + 4*/ 455ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK); 456ae0dc736SMichael Ellerman check(instr == 0); 457ae0dc736SMichael Ellerman 458ae0dc736SMichael Ellerman /* Out of range relative positive offset, + 32 MB */ 459ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK); 460ae0dc736SMichael Ellerman check(instr == 0); 461ae0dc736SMichael Ellerman 462ae0dc736SMichael Ellerman /* Unaligned target */ 463ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK); 464ae0dc736SMichael Ellerman check(instr == 0); 465ae0dc736SMichael Ellerman 466ae0dc736SMichael Ellerman /* Check flags are masked correctly */ 467ae0dc736SMichael Ellerman instr = create_branch(&instr, addr, 0xFFFFFFFC); 468ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 469ae0dc736SMichael Ellerman check(instr == 0x48000000); 470ae0dc736SMichael Ellerman } 471ae0dc736SMichael Ellerman 472ae0dc736SMichael Ellerman static void __init test_create_function_call(void) 473ae0dc736SMichael Ellerman { 474ae0dc736SMichael Ellerman unsigned int *iptr; 475ae0dc736SMichael Ellerman unsigned long dest; 476ae0dc736SMichael Ellerman 477ae0dc736SMichael Ellerman /* Check we can create a function call */ 478ae0dc736SMichael Ellerman iptr = (unsigned int *)ppc_function_entry(test_trampoline); 479ae0dc736SMichael Ellerman dest = ppc_function_entry(test_create_function_call); 480ae0dc736SMichael Ellerman patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK)); 481ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(iptr, dest)); 482ae0dc736SMichael Ellerman } 483ae0dc736SMichael Ellerman 484ae0dc736SMichael Ellerman static void __init test_branch_bform(void) 485ae0dc736SMichael Ellerman { 486ae0dc736SMichael Ellerman unsigned long addr; 487ae0dc736SMichael Ellerman unsigned int *iptr, instr, flags; 488ae0dc736SMichael Ellerman 489ae0dc736SMichael Ellerman iptr = &instr; 490ae0dc736SMichael Ellerman addr = (unsigned long)iptr; 491ae0dc736SMichael Ellerman 492ae0dc736SMichael Ellerman /* The simplest case, branch to self, no flags */ 493ae0dc736SMichael Ellerman check(instr_is_branch_bform(0x40000000)); 494ae0dc736SMichael Ellerman /* All bits of target set, and flags */ 495ae0dc736SMichael Ellerman check(instr_is_branch_bform(0x43ffffff)); 496ae0dc736SMichael Ellerman /* High bit of opcode set, which is wrong */ 497ae0dc736SMichael Ellerman check(!instr_is_branch_bform(0xc3ffffff)); 498ae0dc736SMichael Ellerman /* Middle bits of opcode set, which is wrong */ 499ae0dc736SMichael Ellerman check(!instr_is_branch_bform(0x7bffffff)); 500ae0dc736SMichael Ellerman 501ae0dc736SMichael Ellerman /* Absolute conditional branch to 0x100 */ 502ae0dc736SMichael Ellerman instr = 0x43ff0103; 503ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x100)); 504ae0dc736SMichael Ellerman /* Absolute conditional branch to 0x20fc */ 505ae0dc736SMichael Ellerman instr = 0x43ff20ff; 506ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x20fc)); 507ae0dc736SMichael Ellerman /* Maximum positive relative conditional branch, + 32 KB - 4B */ 508ae0dc736SMichael Ellerman instr = 0x43ff7ffc; 509ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x7FFC)); 510ae0dc736SMichael Ellerman /* Smallest negative relative conditional branch, - 4B */ 511ae0dc736SMichael Ellerman instr = 0x43fffffc; 512ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 4)); 513ae0dc736SMichael Ellerman /* Largest negative relative conditional branch, - 32 KB */ 514ae0dc736SMichael Ellerman instr = 0x43ff8000; 515ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x8000)); 516ae0dc736SMichael Ellerman 517ae0dc736SMichael Ellerman /* All condition code bits set & link */ 518ae0dc736SMichael Ellerman flags = 0x3ff000 | BRANCH_SET_LINK; 519ae0dc736SMichael Ellerman 520ae0dc736SMichael Ellerman /* Branch to self */ 521ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr, flags); 522ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 523ae0dc736SMichael Ellerman 524ae0dc736SMichael Ellerman /* Branch to self - 0x100 */ 525ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x100, flags); 526ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x100)); 527ae0dc736SMichael Ellerman 528ae0dc736SMichael Ellerman /* Branch to self + 0x100 */ 529ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 0x100, flags); 530ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x100)); 531ae0dc736SMichael Ellerman 532ae0dc736SMichael Ellerman /* Maximum relative negative offset, - 32 KB */ 533ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x8000, flags); 534ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x8000)); 535ae0dc736SMichael Ellerman 536ae0dc736SMichael Ellerman /* Out of range relative negative offset, - 32 KB + 4*/ 537ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x8004, flags); 538ae0dc736SMichael Ellerman check(instr == 0); 539ae0dc736SMichael Ellerman 540ae0dc736SMichael Ellerman /* Out of range relative positive offset, + 32 KB */ 541ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 0x8000, flags); 542ae0dc736SMichael Ellerman check(instr == 0); 543ae0dc736SMichael Ellerman 544ae0dc736SMichael Ellerman /* Unaligned target */ 545ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 3, flags); 546ae0dc736SMichael Ellerman check(instr == 0); 547ae0dc736SMichael Ellerman 548ae0dc736SMichael Ellerman /* Check flags are masked correctly */ 549ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr, 0xFFFFFFFC); 550ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 551ae0dc736SMichael Ellerman check(instr == 0x43FF0000); 552ae0dc736SMichael Ellerman } 553ae0dc736SMichael Ellerman 554ae0dc736SMichael Ellerman static void __init test_translate_branch(void) 555ae0dc736SMichael Ellerman { 556ae0dc736SMichael Ellerman unsigned long addr; 557ae0dc736SMichael Ellerman unsigned int *p, *q; 558ae0dc736SMichael Ellerman void *buf; 559ae0dc736SMichael Ellerman 560ae0dc736SMichael Ellerman buf = vmalloc(PAGE_ALIGN(0x2000000 + 1)); 561ae0dc736SMichael Ellerman check(buf); 562ae0dc736SMichael Ellerman if (!buf) 563ae0dc736SMichael Ellerman return; 564ae0dc736SMichael Ellerman 565ae0dc736SMichael Ellerman /* Simple case, branch to self moved a little */ 566ae0dc736SMichael Ellerman p = buf; 567ae0dc736SMichael Ellerman addr = (unsigned long)p; 568ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 569ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 570ae0dc736SMichael Ellerman q = p + 1; 571ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 572ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 573ae0dc736SMichael Ellerman 574ae0dc736SMichael Ellerman /* Maximum negative case, move b . to addr + 32 MB */ 575ae0dc736SMichael Ellerman p = buf; 576ae0dc736SMichael Ellerman addr = (unsigned long)p; 577ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 578ae0dc736SMichael Ellerman q = buf + 0x2000000; 579ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 580ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 581ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 582ae0dc736SMichael Ellerman check(*q == 0x4a000000); 583ae0dc736SMichael Ellerman 584ae0dc736SMichael Ellerman /* Maximum positive case, move x to x - 32 MB + 4 */ 585ae0dc736SMichael Ellerman p = buf + 0x2000000; 586ae0dc736SMichael Ellerman addr = (unsigned long)p; 587ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 588ae0dc736SMichael Ellerman q = buf + 4; 589ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 590ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 591ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 592ae0dc736SMichael Ellerman check(*q == 0x49fffffc); 593ae0dc736SMichael Ellerman 594ae0dc736SMichael Ellerman /* Jump to x + 16 MB moved to x + 20 MB */ 595ae0dc736SMichael Ellerman p = buf; 596ae0dc736SMichael Ellerman addr = 0x1000000 + (unsigned long)buf; 597ae0dc736SMichael Ellerman patch_branch(p, addr, BRANCH_SET_LINK); 598ae0dc736SMichael Ellerman q = buf + 0x1400000; 599ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 600ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 601ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 602ae0dc736SMichael Ellerman 603ae0dc736SMichael Ellerman /* Jump to x + 16 MB moved to x - 16 MB + 4 */ 604ae0dc736SMichael Ellerman p = buf + 0x1000000; 605ae0dc736SMichael Ellerman addr = 0x2000000 + (unsigned long)buf; 606ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 607ae0dc736SMichael Ellerman q = buf + 4; 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 612ae0dc736SMichael Ellerman 613ae0dc736SMichael Ellerman /* Conditional branch tests */ 614ae0dc736SMichael Ellerman 615ae0dc736SMichael Ellerman /* Simple case, branch to self moved a little */ 616ae0dc736SMichael Ellerman p = buf; 617ae0dc736SMichael Ellerman addr = (unsigned long)p; 618ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0)); 619ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 620ae0dc736SMichael Ellerman q = p + 1; 621ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 622ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 623ae0dc736SMichael Ellerman 624ae0dc736SMichael Ellerman /* Maximum negative case, move b . to addr + 32 KB */ 625ae0dc736SMichael Ellerman p = buf; 626ae0dc736SMichael Ellerman addr = (unsigned long)p; 627ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC)); 628ae0dc736SMichael Ellerman q = buf + 0x8000; 629ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 630ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 631ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 632ae0dc736SMichael Ellerman check(*q == 0x43ff8000); 633ae0dc736SMichael Ellerman 634ae0dc736SMichael Ellerman /* Maximum positive case, move x to x - 32 KB + 4 */ 635ae0dc736SMichael Ellerman p = buf + 0x8000; 636ae0dc736SMichael Ellerman addr = (unsigned long)p; 637ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC)); 638ae0dc736SMichael Ellerman q = buf + 4; 639ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 640ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 641ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 642ae0dc736SMichael Ellerman check(*q == 0x43ff7ffc); 643ae0dc736SMichael Ellerman 644ae0dc736SMichael Ellerman /* Jump to x + 12 KB moved to x + 20 KB */ 645ae0dc736SMichael Ellerman p = buf; 646ae0dc736SMichael Ellerman addr = 0x3000 + (unsigned long)buf; 647ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK)); 648ae0dc736SMichael Ellerman q = buf + 0x5000; 649ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 650ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 651ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 652ae0dc736SMichael Ellerman 653ae0dc736SMichael Ellerman /* Jump to x + 8 KB moved to x - 8 KB + 4 */ 654ae0dc736SMichael Ellerman p = buf + 0x2000; 655ae0dc736SMichael Ellerman addr = 0x4000 + (unsigned long)buf; 656ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0)); 657ae0dc736SMichael Ellerman q = buf + 4; 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 662ae0dc736SMichael Ellerman /* Free the buffer we were using */ 663ae0dc736SMichael Ellerman vfree(buf); 664ae0dc736SMichael Ellerman } 665ae0dc736SMichael Ellerman 666ae0dc736SMichael Ellerman static int __init test_code_patching(void) 667ae0dc736SMichael Ellerman { 668ae0dc736SMichael Ellerman printk(KERN_DEBUG "Running code patching self-tests ...\n"); 669ae0dc736SMichael Ellerman 670ae0dc736SMichael Ellerman test_branch_iform(); 671ae0dc736SMichael Ellerman test_branch_bform(); 672ae0dc736SMichael Ellerman test_create_function_call(); 673ae0dc736SMichael Ellerman test_translate_branch(); 674ae0dc736SMichael Ellerman 675ae0dc736SMichael Ellerman return 0; 676ae0dc736SMichael Ellerman } 677ae0dc736SMichael Ellerman late_initcall(test_code_patching); 678ae0dc736SMichael Ellerman 679ae0dc736SMichael Ellerman #endif /* CONFIG_CODE_PATCHING_SELFTEST */ 680