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> 11ae0dc736SMichael Ellerman #include <linux/vmalloc.h> 12ae0dc736SMichael Ellerman #include <linux/init.h> 1327ac792cSAndrea Righi #include <linux/mm.h> 14ae0dc736SMichael Ellerman #include <asm/page.h> 15aaddd3eaSMichael Ellerman #include <asm/code-patching.h> 167c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 17*7d134b2cSLuis R. Rodriguez #include <linux/kprobes.h> 18aaddd3eaSMichael Ellerman 19aaddd3eaSMichael Ellerman 20b6e37968SSteven Rostedt int patch_instruction(unsigned int *addr, unsigned int instr) 21aaddd3eaSMichael Ellerman { 22b6e37968SSteven Rostedt int err; 23b6e37968SSteven Rostedt 24636802efSBenjamin Herrenschmidt __put_user_size(instr, addr, 4, err); 25b6e37968SSteven Rostedt if (err) 26b6e37968SSteven Rostedt return err; 27e7a57273SMichael Ellerman asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr)); 28b6e37968SSteven Rostedt return 0; 29aaddd3eaSMichael Ellerman } 30aaddd3eaSMichael Ellerman 31b6e37968SSteven Rostedt int patch_branch(unsigned int *addr, unsigned long target, int flags) 32e7a57273SMichael Ellerman { 33b6e37968SSteven Rostedt return patch_instruction(addr, create_branch(addr, target, flags)); 34e7a57273SMichael Ellerman } 35e7a57273SMichael Ellerman 36ebfa50dfSAnju T bool is_offset_in_branch_range(long offset) 37ebfa50dfSAnju T { 38ebfa50dfSAnju T /* 39ebfa50dfSAnju T * Powerpc branch instruction is : 40ebfa50dfSAnju T * 41ebfa50dfSAnju T * 0 6 30 31 42ebfa50dfSAnju T * +---------+----------------+---+---+ 43ebfa50dfSAnju T * | opcode | LI |AA |LK | 44ebfa50dfSAnju T * +---------+----------------+---+---+ 45ebfa50dfSAnju T * Where AA = 0 and LK = 0 46ebfa50dfSAnju T * 47ebfa50dfSAnju T * LI is a signed 24 bits integer. The real branch offset is computed 48ebfa50dfSAnju T * by: imm32 = SignExtend(LI:'0b00', 32); 49ebfa50dfSAnju T * 50ebfa50dfSAnju T * So the maximum forward branch should be: 51ebfa50dfSAnju T * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc 52ebfa50dfSAnju T * The maximum backward branch should be: 53ebfa50dfSAnju T * (0xff800000 << 2) = 0xfe000000 = -0x2000000 54ebfa50dfSAnju T */ 55ebfa50dfSAnju T return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3)); 56ebfa50dfSAnju T } 57ebfa50dfSAnju T 5851c9c084SAnju T /* 5951c9c084SAnju T * Helper to check if a given instruction is a conditional branch 6051c9c084SAnju T * Derived from the conditional checks in analyse_instr() 6151c9c084SAnju T */ 6251c9c084SAnju T bool __kprobes is_conditional_branch(unsigned int instr) 6351c9c084SAnju T { 6451c9c084SAnju T unsigned int opcode = instr >> 26; 6551c9c084SAnju T 6651c9c084SAnju T if (opcode == 16) /* bc, bca, bcl, bcla */ 6751c9c084SAnju T return true; 6851c9c084SAnju T if (opcode == 19) { 6951c9c084SAnju T switch ((instr >> 1) & 0x3ff) { 7051c9c084SAnju T case 16: /* bclr, bclrl */ 7151c9c084SAnju T case 528: /* bcctr, bcctrl */ 7251c9c084SAnju T case 560: /* bctar, bctarl */ 7351c9c084SAnju T return true; 7451c9c084SAnju T } 7551c9c084SAnju T } 7651c9c084SAnju T return false; 7751c9c084SAnju T } 7851c9c084SAnju T 79e7a57273SMichael Ellerman unsigned int create_branch(const unsigned int *addr, 80e7a57273SMichael Ellerman unsigned long target, int flags) 81aaddd3eaSMichael Ellerman { 82aaddd3eaSMichael Ellerman unsigned int instruction; 83053a858eSMichael Ellerman long offset; 84aaddd3eaSMichael Ellerman 85053a858eSMichael Ellerman offset = target; 86aaddd3eaSMichael Ellerman if (! (flags & BRANCH_ABSOLUTE)) 87053a858eSMichael Ellerman offset = offset - (unsigned long)addr; 88053a858eSMichael Ellerman 89053a858eSMichael Ellerman /* Check we can represent the target in the instruction format */ 90ebfa50dfSAnju T if (!is_offset_in_branch_range(offset)) 91053a858eSMichael Ellerman return 0; 92aaddd3eaSMichael Ellerman 93aaddd3eaSMichael Ellerman /* Mask out the flags and target, so they don't step on each other. */ 94053a858eSMichael Ellerman instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC); 95aaddd3eaSMichael Ellerman 96e7a57273SMichael Ellerman return instruction; 97aaddd3eaSMichael Ellerman } 98411781a2SMichael Ellerman 99411781a2SMichael Ellerman unsigned int create_cond_branch(const unsigned int *addr, 100411781a2SMichael Ellerman unsigned long target, int flags) 101411781a2SMichael Ellerman { 102411781a2SMichael Ellerman unsigned int instruction; 103411781a2SMichael Ellerman long offset; 104411781a2SMichael Ellerman 105411781a2SMichael Ellerman offset = target; 106411781a2SMichael Ellerman if (! (flags & BRANCH_ABSOLUTE)) 107411781a2SMichael Ellerman offset = offset - (unsigned long)addr; 108411781a2SMichael Ellerman 109411781a2SMichael Ellerman /* Check we can represent the target in the instruction format */ 110411781a2SMichael Ellerman if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3) 111411781a2SMichael Ellerman return 0; 112411781a2SMichael Ellerman 113411781a2SMichael Ellerman /* Mask out the flags and target, so they don't step on each other. */ 114411781a2SMichael Ellerman instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC); 115411781a2SMichael Ellerman 116411781a2SMichael Ellerman return instruction; 117411781a2SMichael Ellerman } 118411781a2SMichael Ellerman 119411781a2SMichael Ellerman static unsigned int branch_opcode(unsigned int instr) 120411781a2SMichael Ellerman { 121411781a2SMichael Ellerman return (instr >> 26) & 0x3F; 122411781a2SMichael Ellerman } 123411781a2SMichael Ellerman 124411781a2SMichael Ellerman static int instr_is_branch_iform(unsigned int instr) 125411781a2SMichael Ellerman { 126411781a2SMichael Ellerman return branch_opcode(instr) == 18; 127411781a2SMichael Ellerman } 128411781a2SMichael Ellerman 129411781a2SMichael Ellerman static int instr_is_branch_bform(unsigned int instr) 130411781a2SMichael Ellerman { 131411781a2SMichael Ellerman return branch_opcode(instr) == 16; 132411781a2SMichael Ellerman } 133411781a2SMichael Ellerman 134411781a2SMichael Ellerman int instr_is_relative_branch(unsigned int instr) 135411781a2SMichael Ellerman { 136411781a2SMichael Ellerman if (instr & BRANCH_ABSOLUTE) 137411781a2SMichael Ellerman return 0; 138411781a2SMichael Ellerman 139411781a2SMichael Ellerman return instr_is_branch_iform(instr) || instr_is_branch_bform(instr); 140411781a2SMichael Ellerman } 141411781a2SMichael Ellerman 142411781a2SMichael Ellerman static unsigned long branch_iform_target(const unsigned int *instr) 143411781a2SMichael Ellerman { 144411781a2SMichael Ellerman signed long imm; 145411781a2SMichael Ellerman 146411781a2SMichael Ellerman imm = *instr & 0x3FFFFFC; 147411781a2SMichael Ellerman 148411781a2SMichael Ellerman /* If the top bit of the immediate value is set this is negative */ 149411781a2SMichael Ellerman if (imm & 0x2000000) 150411781a2SMichael Ellerman imm -= 0x4000000; 151411781a2SMichael Ellerman 152411781a2SMichael Ellerman if ((*instr & BRANCH_ABSOLUTE) == 0) 153411781a2SMichael Ellerman imm += (unsigned long)instr; 154411781a2SMichael Ellerman 155411781a2SMichael Ellerman return (unsigned long)imm; 156411781a2SMichael Ellerman } 157411781a2SMichael Ellerman 158411781a2SMichael Ellerman static unsigned long branch_bform_target(const unsigned int *instr) 159411781a2SMichael Ellerman { 160411781a2SMichael Ellerman signed long imm; 161411781a2SMichael Ellerman 162411781a2SMichael Ellerman imm = *instr & 0xFFFC; 163411781a2SMichael Ellerman 164411781a2SMichael Ellerman /* If the top bit of the immediate value is set this is negative */ 165411781a2SMichael Ellerman if (imm & 0x8000) 166411781a2SMichael Ellerman imm -= 0x10000; 167411781a2SMichael Ellerman 168411781a2SMichael Ellerman if ((*instr & BRANCH_ABSOLUTE) == 0) 169411781a2SMichael Ellerman imm += (unsigned long)instr; 170411781a2SMichael Ellerman 171411781a2SMichael Ellerman return (unsigned long)imm; 172411781a2SMichael Ellerman } 173411781a2SMichael Ellerman 174411781a2SMichael Ellerman unsigned long branch_target(const unsigned int *instr) 175411781a2SMichael Ellerman { 176411781a2SMichael Ellerman if (instr_is_branch_iform(*instr)) 177411781a2SMichael Ellerman return branch_iform_target(instr); 178411781a2SMichael Ellerman else if (instr_is_branch_bform(*instr)) 179411781a2SMichael Ellerman return branch_bform_target(instr); 180411781a2SMichael Ellerman 181411781a2SMichael Ellerman return 0; 182411781a2SMichael Ellerman } 183411781a2SMichael Ellerman 184411781a2SMichael Ellerman int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr) 185411781a2SMichael Ellerman { 186411781a2SMichael Ellerman if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr)) 187411781a2SMichael Ellerman return branch_target(instr) == addr; 188411781a2SMichael Ellerman 189411781a2SMichael Ellerman return 0; 190411781a2SMichael Ellerman } 191411781a2SMichael Ellerman 192411781a2SMichael Ellerman unsigned int translate_branch(const unsigned int *dest, const unsigned int *src) 193411781a2SMichael Ellerman { 194411781a2SMichael Ellerman unsigned long target; 195411781a2SMichael Ellerman 196411781a2SMichael Ellerman target = branch_target(src); 197411781a2SMichael Ellerman 198411781a2SMichael Ellerman if (instr_is_branch_iform(*src)) 199411781a2SMichael Ellerman return create_branch(dest, target, *src); 200411781a2SMichael Ellerman else if (instr_is_branch_bform(*src)) 201411781a2SMichael Ellerman return create_cond_branch(dest, target, *src); 202411781a2SMichael Ellerman 203411781a2SMichael Ellerman return 0; 204411781a2SMichael Ellerman } 205ae0dc736SMichael Ellerman 2061e8341aeSKevin Hao #ifdef CONFIG_PPC_BOOK3E_64 2071e8341aeSKevin Hao void __patch_exception(int exc, unsigned long addr) 2081e8341aeSKevin Hao { 2091e8341aeSKevin Hao extern unsigned int interrupt_base_book3e; 2101e8341aeSKevin Hao unsigned int *ibase = &interrupt_base_book3e; 2111e8341aeSKevin Hao 2121e8341aeSKevin Hao /* Our exceptions vectors start with a NOP and -then- a branch 2131e8341aeSKevin Hao * to deal with single stepping from userspace which stops on 2141e8341aeSKevin Hao * the second instruction. Thus we need to patch the second 2151e8341aeSKevin Hao * instruction of the exception, not the first one 2161e8341aeSKevin Hao */ 2171e8341aeSKevin Hao 2181e8341aeSKevin Hao patch_branch(ibase + (exc / 4) + 1, addr, 0); 2191e8341aeSKevin Hao } 2201e8341aeSKevin Hao #endif 221ae0dc736SMichael Ellerman 222ae0dc736SMichael Ellerman #ifdef CONFIG_CODE_PATCHING_SELFTEST 223ae0dc736SMichael Ellerman 224ae0dc736SMichael Ellerman static void __init test_trampoline(void) 225ae0dc736SMichael Ellerman { 226ae0dc736SMichael Ellerman asm ("nop;\n"); 227ae0dc736SMichael Ellerman } 228ae0dc736SMichael Ellerman 229ae0dc736SMichael Ellerman #define check(x) \ 230ae0dc736SMichael Ellerman if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__); 231ae0dc736SMichael Ellerman 232ae0dc736SMichael Ellerman static void __init test_branch_iform(void) 233ae0dc736SMichael Ellerman { 234ae0dc736SMichael Ellerman unsigned int instr; 235ae0dc736SMichael Ellerman unsigned long addr; 236ae0dc736SMichael Ellerman 237ae0dc736SMichael Ellerman addr = (unsigned long)&instr; 238ae0dc736SMichael Ellerman 239ae0dc736SMichael Ellerman /* The simplest case, branch to self, no flags */ 240ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x48000000)); 241ae0dc736SMichael Ellerman /* All bits of target set, and flags */ 242ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bffffff)); 243ae0dc736SMichael Ellerman /* High bit of opcode set, which is wrong */ 244ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0xcbffffff)); 245ae0dc736SMichael Ellerman /* Middle bits of opcode set, which is wrong */ 246ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0x7bffffff)); 247ae0dc736SMichael Ellerman 248ae0dc736SMichael Ellerman /* Simplest case, branch to self with link */ 249ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x48000001)); 250ae0dc736SMichael Ellerman /* All bits of targets set */ 251ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bfffffd)); 252ae0dc736SMichael Ellerman /* Some bits of targets set */ 253ae0dc736SMichael Ellerman check(instr_is_branch_iform(0x4bff00fd)); 254ae0dc736SMichael Ellerman /* Must be a valid branch to start with */ 255ae0dc736SMichael Ellerman check(!instr_is_branch_iform(0x7bfffffd)); 256ae0dc736SMichael Ellerman 257ae0dc736SMichael Ellerman /* Absolute branch to 0x100 */ 258ae0dc736SMichael Ellerman instr = 0x48000103; 259ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x100)); 260ae0dc736SMichael Ellerman /* Absolute branch to 0x420fc */ 261ae0dc736SMichael Ellerman instr = 0x480420ff; 262ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x420fc)); 263ae0dc736SMichael Ellerman /* Maximum positive relative branch, + 20MB - 4B */ 264ae0dc736SMichael Ellerman instr = 0x49fffffc; 265ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC)); 266ae0dc736SMichael Ellerman /* Smallest negative relative branch, - 4B */ 267ae0dc736SMichael Ellerman instr = 0x4bfffffc; 268ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 4)); 269ae0dc736SMichael Ellerman /* Largest negative relative branch, - 32 MB */ 270ae0dc736SMichael Ellerman instr = 0x4a000000; 271ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); 272ae0dc736SMichael Ellerman 273ae0dc736SMichael Ellerman /* Branch to self, with link */ 274ae0dc736SMichael Ellerman instr = create_branch(&instr, addr, BRANCH_SET_LINK); 275ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 276ae0dc736SMichael Ellerman 277ae0dc736SMichael Ellerman /* Branch to self - 0x100, with link */ 278ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK); 279ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x100)); 280ae0dc736SMichael Ellerman 281ae0dc736SMichael Ellerman /* Branch to self + 0x100, no link */ 282ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 0x100, 0); 283ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x100)); 284ae0dc736SMichael Ellerman 285ae0dc736SMichael Ellerman /* Maximum relative negative offset, - 32 MB */ 286ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK); 287ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); 288ae0dc736SMichael Ellerman 289ae0dc736SMichael Ellerman /* Out of range relative negative offset, - 32 MB + 4*/ 290ae0dc736SMichael Ellerman instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK); 291ae0dc736SMichael Ellerman check(instr == 0); 292ae0dc736SMichael Ellerman 293ae0dc736SMichael Ellerman /* Out of range relative positive offset, + 32 MB */ 294ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK); 295ae0dc736SMichael Ellerman check(instr == 0); 296ae0dc736SMichael Ellerman 297ae0dc736SMichael Ellerman /* Unaligned target */ 298ae0dc736SMichael Ellerman instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK); 299ae0dc736SMichael Ellerman check(instr == 0); 300ae0dc736SMichael Ellerman 301ae0dc736SMichael Ellerman /* Check flags are masked correctly */ 302ae0dc736SMichael Ellerman instr = create_branch(&instr, addr, 0xFFFFFFFC); 303ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 304ae0dc736SMichael Ellerman check(instr == 0x48000000); 305ae0dc736SMichael Ellerman } 306ae0dc736SMichael Ellerman 307ae0dc736SMichael Ellerman static void __init test_create_function_call(void) 308ae0dc736SMichael Ellerman { 309ae0dc736SMichael Ellerman unsigned int *iptr; 310ae0dc736SMichael Ellerman unsigned long dest; 311ae0dc736SMichael Ellerman 312ae0dc736SMichael Ellerman /* Check we can create a function call */ 313ae0dc736SMichael Ellerman iptr = (unsigned int *)ppc_function_entry(test_trampoline); 314ae0dc736SMichael Ellerman dest = ppc_function_entry(test_create_function_call); 315ae0dc736SMichael Ellerman patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK)); 316ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(iptr, dest)); 317ae0dc736SMichael Ellerman } 318ae0dc736SMichael Ellerman 319ae0dc736SMichael Ellerman static void __init test_branch_bform(void) 320ae0dc736SMichael Ellerman { 321ae0dc736SMichael Ellerman unsigned long addr; 322ae0dc736SMichael Ellerman unsigned int *iptr, instr, flags; 323ae0dc736SMichael Ellerman 324ae0dc736SMichael Ellerman iptr = &instr; 325ae0dc736SMichael Ellerman addr = (unsigned long)iptr; 326ae0dc736SMichael Ellerman 327ae0dc736SMichael Ellerman /* The simplest case, branch to self, no flags */ 328ae0dc736SMichael Ellerman check(instr_is_branch_bform(0x40000000)); 329ae0dc736SMichael Ellerman /* All bits of target set, and flags */ 330ae0dc736SMichael Ellerman check(instr_is_branch_bform(0x43ffffff)); 331ae0dc736SMichael Ellerman /* High bit of opcode set, which is wrong */ 332ae0dc736SMichael Ellerman check(!instr_is_branch_bform(0xc3ffffff)); 333ae0dc736SMichael Ellerman /* Middle bits of opcode set, which is wrong */ 334ae0dc736SMichael Ellerman check(!instr_is_branch_bform(0x7bffffff)); 335ae0dc736SMichael Ellerman 336ae0dc736SMichael Ellerman /* Absolute conditional branch to 0x100 */ 337ae0dc736SMichael Ellerman instr = 0x43ff0103; 338ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x100)); 339ae0dc736SMichael Ellerman /* Absolute conditional branch to 0x20fc */ 340ae0dc736SMichael Ellerman instr = 0x43ff20ff; 341ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, 0x20fc)); 342ae0dc736SMichael Ellerman /* Maximum positive relative conditional branch, + 32 KB - 4B */ 343ae0dc736SMichael Ellerman instr = 0x43ff7ffc; 344ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x7FFC)); 345ae0dc736SMichael Ellerman /* Smallest negative relative conditional branch, - 4B */ 346ae0dc736SMichael Ellerman instr = 0x43fffffc; 347ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 4)); 348ae0dc736SMichael Ellerman /* Largest negative relative conditional branch, - 32 KB */ 349ae0dc736SMichael Ellerman instr = 0x43ff8000; 350ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x8000)); 351ae0dc736SMichael Ellerman 352ae0dc736SMichael Ellerman /* All condition code bits set & link */ 353ae0dc736SMichael Ellerman flags = 0x3ff000 | BRANCH_SET_LINK; 354ae0dc736SMichael Ellerman 355ae0dc736SMichael Ellerman /* Branch to self */ 356ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr, flags); 357ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 358ae0dc736SMichael Ellerman 359ae0dc736SMichael Ellerman /* Branch to self - 0x100 */ 360ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x100, flags); 361ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x100)); 362ae0dc736SMichael Ellerman 363ae0dc736SMichael Ellerman /* Branch to self + 0x100 */ 364ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 0x100, flags); 365ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr + 0x100)); 366ae0dc736SMichael Ellerman 367ae0dc736SMichael Ellerman /* Maximum relative negative offset, - 32 KB */ 368ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x8000, flags); 369ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr - 0x8000)); 370ae0dc736SMichael Ellerman 371ae0dc736SMichael Ellerman /* Out of range relative negative offset, - 32 KB + 4*/ 372ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr - 0x8004, flags); 373ae0dc736SMichael Ellerman check(instr == 0); 374ae0dc736SMichael Ellerman 375ae0dc736SMichael Ellerman /* Out of range relative positive offset, + 32 KB */ 376ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 0x8000, flags); 377ae0dc736SMichael Ellerman check(instr == 0); 378ae0dc736SMichael Ellerman 379ae0dc736SMichael Ellerman /* Unaligned target */ 380ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr + 3, flags); 381ae0dc736SMichael Ellerman check(instr == 0); 382ae0dc736SMichael Ellerman 383ae0dc736SMichael Ellerman /* Check flags are masked correctly */ 384ae0dc736SMichael Ellerman instr = create_cond_branch(iptr, addr, 0xFFFFFFFC); 385ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(&instr, addr)); 386ae0dc736SMichael Ellerman check(instr == 0x43FF0000); 387ae0dc736SMichael Ellerman } 388ae0dc736SMichael Ellerman 389ae0dc736SMichael Ellerman static void __init test_translate_branch(void) 390ae0dc736SMichael Ellerman { 391ae0dc736SMichael Ellerman unsigned long addr; 392ae0dc736SMichael Ellerman unsigned int *p, *q; 393ae0dc736SMichael Ellerman void *buf; 394ae0dc736SMichael Ellerman 395ae0dc736SMichael Ellerman buf = vmalloc(PAGE_ALIGN(0x2000000 + 1)); 396ae0dc736SMichael Ellerman check(buf); 397ae0dc736SMichael Ellerman if (!buf) 398ae0dc736SMichael Ellerman return; 399ae0dc736SMichael Ellerman 400ae0dc736SMichael Ellerman /* Simple case, branch to self moved a little */ 401ae0dc736SMichael Ellerman p = buf; 402ae0dc736SMichael Ellerman addr = (unsigned long)p; 403ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 404ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 405ae0dc736SMichael Ellerman q = p + 1; 406ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 407ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 408ae0dc736SMichael Ellerman 409ae0dc736SMichael Ellerman /* Maximum negative case, move b . to addr + 32 MB */ 410ae0dc736SMichael Ellerman p = buf; 411ae0dc736SMichael Ellerman addr = (unsigned long)p; 412ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 413ae0dc736SMichael Ellerman q = buf + 0x2000000; 414ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 415ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 416ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 417ae0dc736SMichael Ellerman check(*q == 0x4a000000); 418ae0dc736SMichael Ellerman 419ae0dc736SMichael Ellerman /* Maximum positive case, move x to x - 32 MB + 4 */ 420ae0dc736SMichael Ellerman p = buf + 0x2000000; 421ae0dc736SMichael Ellerman addr = (unsigned long)p; 422ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 423ae0dc736SMichael Ellerman q = buf + 4; 424ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 425ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 426ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 427ae0dc736SMichael Ellerman check(*q == 0x49fffffc); 428ae0dc736SMichael Ellerman 429ae0dc736SMichael Ellerman /* Jump to x + 16 MB moved to x + 20 MB */ 430ae0dc736SMichael Ellerman p = buf; 431ae0dc736SMichael Ellerman addr = 0x1000000 + (unsigned long)buf; 432ae0dc736SMichael Ellerman patch_branch(p, addr, BRANCH_SET_LINK); 433ae0dc736SMichael Ellerman q = buf + 0x1400000; 434ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 435ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 436ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 437ae0dc736SMichael Ellerman 438ae0dc736SMichael Ellerman /* Jump to x + 16 MB moved to x - 16 MB + 4 */ 439ae0dc736SMichael Ellerman p = buf + 0x1000000; 440ae0dc736SMichael Ellerman addr = 0x2000000 + (unsigned long)buf; 441ae0dc736SMichael Ellerman patch_branch(p, addr, 0); 442ae0dc736SMichael Ellerman q = buf + 4; 443ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 444ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 445ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 446ae0dc736SMichael Ellerman 447ae0dc736SMichael Ellerman 448ae0dc736SMichael Ellerman /* Conditional branch tests */ 449ae0dc736SMichael Ellerman 450ae0dc736SMichael Ellerman /* Simple case, branch to self moved a little */ 451ae0dc736SMichael Ellerman p = buf; 452ae0dc736SMichael Ellerman addr = (unsigned long)p; 453ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0)); 454ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 455ae0dc736SMichael Ellerman q = p + 1; 456ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 457ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 458ae0dc736SMichael Ellerman 459ae0dc736SMichael Ellerman /* Maximum negative case, move b . to addr + 32 KB */ 460ae0dc736SMichael Ellerman p = buf; 461ae0dc736SMichael Ellerman addr = (unsigned long)p; 462ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC)); 463ae0dc736SMichael Ellerman q = buf + 0x8000; 464ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 465ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 466ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 467ae0dc736SMichael Ellerman check(*q == 0x43ff8000); 468ae0dc736SMichael Ellerman 469ae0dc736SMichael Ellerman /* Maximum positive case, move x to x - 32 KB + 4 */ 470ae0dc736SMichael Ellerman p = buf + 0x8000; 471ae0dc736SMichael Ellerman addr = (unsigned long)p; 472ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC)); 473ae0dc736SMichael Ellerman q = buf + 4; 474ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 475ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 476ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 477ae0dc736SMichael Ellerman check(*q == 0x43ff7ffc); 478ae0dc736SMichael Ellerman 479ae0dc736SMichael Ellerman /* Jump to x + 12 KB moved to x + 20 KB */ 480ae0dc736SMichael Ellerman p = buf; 481ae0dc736SMichael Ellerman addr = 0x3000 + (unsigned long)buf; 482ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK)); 483ae0dc736SMichael Ellerman q = buf + 0x5000; 484ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 485ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 486ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 487ae0dc736SMichael Ellerman 488ae0dc736SMichael Ellerman /* Jump to x + 8 KB moved to x - 8 KB + 4 */ 489ae0dc736SMichael Ellerman p = buf + 0x2000; 490ae0dc736SMichael Ellerman addr = 0x4000 + (unsigned long)buf; 491ae0dc736SMichael Ellerman patch_instruction(p, create_cond_branch(p, addr, 0)); 492ae0dc736SMichael Ellerman q = buf + 4; 493ae0dc736SMichael Ellerman patch_instruction(q, translate_branch(q, p)); 494ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(p, addr)); 495ae0dc736SMichael Ellerman check(instr_is_branch_to_addr(q, addr)); 496ae0dc736SMichael Ellerman 497ae0dc736SMichael Ellerman /* Free the buffer we were using */ 498ae0dc736SMichael Ellerman vfree(buf); 499ae0dc736SMichael Ellerman } 500ae0dc736SMichael Ellerman 501ae0dc736SMichael Ellerman static int __init test_code_patching(void) 502ae0dc736SMichael Ellerman { 503ae0dc736SMichael Ellerman printk(KERN_DEBUG "Running code patching self-tests ...\n"); 504ae0dc736SMichael Ellerman 505ae0dc736SMichael Ellerman test_branch_iform(); 506ae0dc736SMichael Ellerman test_branch_bform(); 507ae0dc736SMichael Ellerman test_create_function_call(); 508ae0dc736SMichael Ellerman test_translate_branch(); 509ae0dc736SMichael Ellerman 510ae0dc736SMichael Ellerman return 0; 511ae0dc736SMichael Ellerman } 512ae0dc736SMichael Ellerman late_initcall(test_code_patching); 513ae0dc736SMichael Ellerman 514ae0dc736SMichael Ellerman #endif /* CONFIG_CODE_PATCHING_SELFTEST */ 515