Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Michael Ellerman, IBM Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
Michael Ellerman | ae0dc73 | 2008-06-24 11:32:32 +1000 | [diff] [blame] | 11 | #include <linux/vmalloc.h> |
| 12 | #include <linux/init.h> |
Andrea Righi | 27ac792 | 2008-07-23 21:28:13 -0700 | [diff] [blame] | 13 | #include <linux/mm.h> |
Michael Ellerman | ae0dc73 | 2008-06-24 11:32:32 +1000 | [diff] [blame] | 14 | #include <asm/page.h> |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 15 | #include <asm/code-patching.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 17 | |
| 18 | |
Steven Rostedt | b6e3796 | 2012-04-26 08:31:18 +0000 | [diff] [blame] | 19 | int patch_instruction(unsigned int *addr, unsigned int instr) |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 20 | { |
Steven Rostedt | b6e3796 | 2012-04-26 08:31:18 +0000 | [diff] [blame] | 21 | int err; |
| 22 | |
Benjamin Herrenschmidt | 636802e | 2012-09-04 15:08:28 +0000 | [diff] [blame] | 23 | __put_user_size(instr, addr, 4, err); |
Steven Rostedt | b6e3796 | 2012-04-26 08:31:18 +0000 | [diff] [blame] | 24 | if (err) |
| 25 | return err; |
Michael Ellerman | e7a5727 | 2008-06-24 11:32:22 +1000 | [diff] [blame] | 26 | asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr)); |
Steven Rostedt | b6e3796 | 2012-04-26 08:31:18 +0000 | [diff] [blame] | 27 | return 0; |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 28 | } |
| 29 | |
Steven Rostedt | b6e3796 | 2012-04-26 08:31:18 +0000 | [diff] [blame] | 30 | int patch_branch(unsigned int *addr, unsigned long target, int flags) |
Michael Ellerman | e7a5727 | 2008-06-24 11:32:22 +1000 | [diff] [blame] | 31 | { |
Steven Rostedt | b6e3796 | 2012-04-26 08:31:18 +0000 | [diff] [blame] | 32 | return patch_instruction(addr, create_branch(addr, target, flags)); |
Michael Ellerman | e7a5727 | 2008-06-24 11:32:22 +1000 | [diff] [blame] | 33 | } |
| 34 | |
Anju T | ebfa50d | 2017-02-08 14:27:30 +0530 | [diff] [blame^] | 35 | bool is_offset_in_branch_range(long offset) |
| 36 | { |
| 37 | /* |
| 38 | * Powerpc branch instruction is : |
| 39 | * |
| 40 | * 0 6 30 31 |
| 41 | * +---------+----------------+---+---+ |
| 42 | * | opcode | LI |AA |LK | |
| 43 | * +---------+----------------+---+---+ |
| 44 | * Where AA = 0 and LK = 0 |
| 45 | * |
| 46 | * LI is a signed 24 bits integer. The real branch offset is computed |
| 47 | * by: imm32 = SignExtend(LI:'0b00', 32); |
| 48 | * |
| 49 | * So the maximum forward branch should be: |
| 50 | * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc |
| 51 | * The maximum backward branch should be: |
| 52 | * (0xff800000 << 2) = 0xfe000000 = -0x2000000 |
| 53 | */ |
| 54 | return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3)); |
| 55 | } |
| 56 | |
Michael Ellerman | e7a5727 | 2008-06-24 11:32:22 +1000 | [diff] [blame] | 57 | unsigned int create_branch(const unsigned int *addr, |
| 58 | unsigned long target, int flags) |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 59 | { |
| 60 | unsigned int instruction; |
Michael Ellerman | 053a858 | 2008-06-24 11:32:24 +1000 | [diff] [blame] | 61 | long offset; |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 62 | |
Michael Ellerman | 053a858 | 2008-06-24 11:32:24 +1000 | [diff] [blame] | 63 | offset = target; |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 64 | if (! (flags & BRANCH_ABSOLUTE)) |
Michael Ellerman | 053a858 | 2008-06-24 11:32:24 +1000 | [diff] [blame] | 65 | offset = offset - (unsigned long)addr; |
| 66 | |
| 67 | /* Check we can represent the target in the instruction format */ |
Anju T | ebfa50d | 2017-02-08 14:27:30 +0530 | [diff] [blame^] | 68 | if (!is_offset_in_branch_range(offset)) |
Michael Ellerman | 053a858 | 2008-06-24 11:32:24 +1000 | [diff] [blame] | 69 | return 0; |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 70 | |
| 71 | /* Mask out the flags and target, so they don't step on each other. */ |
Michael Ellerman | 053a858 | 2008-06-24 11:32:24 +1000 | [diff] [blame] | 72 | instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC); |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 73 | |
Michael Ellerman | e7a5727 | 2008-06-24 11:32:22 +1000 | [diff] [blame] | 74 | return instruction; |
Michael Ellerman | aaddd3e | 2008-06-24 11:32:21 +1000 | [diff] [blame] | 75 | } |
Michael Ellerman | 411781a | 2008-06-24 11:32:29 +1000 | [diff] [blame] | 76 | |
| 77 | unsigned int create_cond_branch(const unsigned int *addr, |
| 78 | unsigned long target, int flags) |
| 79 | { |
| 80 | unsigned int instruction; |
| 81 | long offset; |
| 82 | |
| 83 | offset = target; |
| 84 | if (! (flags & BRANCH_ABSOLUTE)) |
| 85 | offset = offset - (unsigned long)addr; |
| 86 | |
| 87 | /* Check we can represent the target in the instruction format */ |
| 88 | if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3) |
| 89 | return 0; |
| 90 | |
| 91 | /* Mask out the flags and target, so they don't step on each other. */ |
| 92 | instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC); |
| 93 | |
| 94 | return instruction; |
| 95 | } |
| 96 | |
| 97 | static unsigned int branch_opcode(unsigned int instr) |
| 98 | { |
| 99 | return (instr >> 26) & 0x3F; |
| 100 | } |
| 101 | |
| 102 | static int instr_is_branch_iform(unsigned int instr) |
| 103 | { |
| 104 | return branch_opcode(instr) == 18; |
| 105 | } |
| 106 | |
| 107 | static int instr_is_branch_bform(unsigned int instr) |
| 108 | { |
| 109 | return branch_opcode(instr) == 16; |
| 110 | } |
| 111 | |
| 112 | int instr_is_relative_branch(unsigned int instr) |
| 113 | { |
| 114 | if (instr & BRANCH_ABSOLUTE) |
| 115 | return 0; |
| 116 | |
| 117 | return instr_is_branch_iform(instr) || instr_is_branch_bform(instr); |
| 118 | } |
| 119 | |
| 120 | static unsigned long branch_iform_target(const unsigned int *instr) |
| 121 | { |
| 122 | signed long imm; |
| 123 | |
| 124 | imm = *instr & 0x3FFFFFC; |
| 125 | |
| 126 | /* If the top bit of the immediate value is set this is negative */ |
| 127 | if (imm & 0x2000000) |
| 128 | imm -= 0x4000000; |
| 129 | |
| 130 | if ((*instr & BRANCH_ABSOLUTE) == 0) |
| 131 | imm += (unsigned long)instr; |
| 132 | |
| 133 | return (unsigned long)imm; |
| 134 | } |
| 135 | |
| 136 | static unsigned long branch_bform_target(const unsigned int *instr) |
| 137 | { |
| 138 | signed long imm; |
| 139 | |
| 140 | imm = *instr & 0xFFFC; |
| 141 | |
| 142 | /* If the top bit of the immediate value is set this is negative */ |
| 143 | if (imm & 0x8000) |
| 144 | imm -= 0x10000; |
| 145 | |
| 146 | if ((*instr & BRANCH_ABSOLUTE) == 0) |
| 147 | imm += (unsigned long)instr; |
| 148 | |
| 149 | return (unsigned long)imm; |
| 150 | } |
| 151 | |
| 152 | unsigned long branch_target(const unsigned int *instr) |
| 153 | { |
| 154 | if (instr_is_branch_iform(*instr)) |
| 155 | return branch_iform_target(instr); |
| 156 | else if (instr_is_branch_bform(*instr)) |
| 157 | return branch_bform_target(instr); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr) |
| 163 | { |
| 164 | if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr)) |
| 165 | return branch_target(instr) == addr; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | unsigned int translate_branch(const unsigned int *dest, const unsigned int *src) |
| 171 | { |
| 172 | unsigned long target; |
| 173 | |
| 174 | target = branch_target(src); |
| 175 | |
| 176 | if (instr_is_branch_iform(*src)) |
| 177 | return create_branch(dest, target, *src); |
| 178 | else if (instr_is_branch_bform(*src)) |
| 179 | return create_cond_branch(dest, target, *src); |
| 180 | |
| 181 | return 0; |
| 182 | } |
Michael Ellerman | ae0dc73 | 2008-06-24 11:32:32 +1000 | [diff] [blame] | 183 | |
Kevin Hao | 1e8341a | 2013-05-12 07:26:22 +0800 | [diff] [blame] | 184 | #ifdef CONFIG_PPC_BOOK3E_64 |
| 185 | void __patch_exception(int exc, unsigned long addr) |
| 186 | { |
| 187 | extern unsigned int interrupt_base_book3e; |
| 188 | unsigned int *ibase = &interrupt_base_book3e; |
| 189 | |
| 190 | /* Our exceptions vectors start with a NOP and -then- a branch |
| 191 | * to deal with single stepping from userspace which stops on |
| 192 | * the second instruction. Thus we need to patch the second |
| 193 | * instruction of the exception, not the first one |
| 194 | */ |
| 195 | |
| 196 | patch_branch(ibase + (exc / 4) + 1, addr, 0); |
| 197 | } |
| 198 | #endif |
Michael Ellerman | ae0dc73 | 2008-06-24 11:32:32 +1000 | [diff] [blame] | 199 | |
| 200 | #ifdef CONFIG_CODE_PATCHING_SELFTEST |
| 201 | |
| 202 | static void __init test_trampoline(void) |
| 203 | { |
| 204 | asm ("nop;\n"); |
| 205 | } |
| 206 | |
| 207 | #define check(x) \ |
| 208 | if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__); |
| 209 | |
| 210 | static void __init test_branch_iform(void) |
| 211 | { |
| 212 | unsigned int instr; |
| 213 | unsigned long addr; |
| 214 | |
| 215 | addr = (unsigned long)&instr; |
| 216 | |
| 217 | /* The simplest case, branch to self, no flags */ |
| 218 | check(instr_is_branch_iform(0x48000000)); |
| 219 | /* All bits of target set, and flags */ |
| 220 | check(instr_is_branch_iform(0x4bffffff)); |
| 221 | /* High bit of opcode set, which is wrong */ |
| 222 | check(!instr_is_branch_iform(0xcbffffff)); |
| 223 | /* Middle bits of opcode set, which is wrong */ |
| 224 | check(!instr_is_branch_iform(0x7bffffff)); |
| 225 | |
| 226 | /* Simplest case, branch to self with link */ |
| 227 | check(instr_is_branch_iform(0x48000001)); |
| 228 | /* All bits of targets set */ |
| 229 | check(instr_is_branch_iform(0x4bfffffd)); |
| 230 | /* Some bits of targets set */ |
| 231 | check(instr_is_branch_iform(0x4bff00fd)); |
| 232 | /* Must be a valid branch to start with */ |
| 233 | check(!instr_is_branch_iform(0x7bfffffd)); |
| 234 | |
| 235 | /* Absolute branch to 0x100 */ |
| 236 | instr = 0x48000103; |
| 237 | check(instr_is_branch_to_addr(&instr, 0x100)); |
| 238 | /* Absolute branch to 0x420fc */ |
| 239 | instr = 0x480420ff; |
| 240 | check(instr_is_branch_to_addr(&instr, 0x420fc)); |
| 241 | /* Maximum positive relative branch, + 20MB - 4B */ |
| 242 | instr = 0x49fffffc; |
| 243 | check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC)); |
| 244 | /* Smallest negative relative branch, - 4B */ |
| 245 | instr = 0x4bfffffc; |
| 246 | check(instr_is_branch_to_addr(&instr, addr - 4)); |
| 247 | /* Largest negative relative branch, - 32 MB */ |
| 248 | instr = 0x4a000000; |
| 249 | check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); |
| 250 | |
| 251 | /* Branch to self, with link */ |
| 252 | instr = create_branch(&instr, addr, BRANCH_SET_LINK); |
| 253 | check(instr_is_branch_to_addr(&instr, addr)); |
| 254 | |
| 255 | /* Branch to self - 0x100, with link */ |
| 256 | instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK); |
| 257 | check(instr_is_branch_to_addr(&instr, addr - 0x100)); |
| 258 | |
| 259 | /* Branch to self + 0x100, no link */ |
| 260 | instr = create_branch(&instr, addr + 0x100, 0); |
| 261 | check(instr_is_branch_to_addr(&instr, addr + 0x100)); |
| 262 | |
| 263 | /* Maximum relative negative offset, - 32 MB */ |
| 264 | instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK); |
| 265 | check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); |
| 266 | |
| 267 | /* Out of range relative negative offset, - 32 MB + 4*/ |
| 268 | instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK); |
| 269 | check(instr == 0); |
| 270 | |
| 271 | /* Out of range relative positive offset, + 32 MB */ |
| 272 | instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK); |
| 273 | check(instr == 0); |
| 274 | |
| 275 | /* Unaligned target */ |
| 276 | instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK); |
| 277 | check(instr == 0); |
| 278 | |
| 279 | /* Check flags are masked correctly */ |
| 280 | instr = create_branch(&instr, addr, 0xFFFFFFFC); |
| 281 | check(instr_is_branch_to_addr(&instr, addr)); |
| 282 | check(instr == 0x48000000); |
| 283 | } |
| 284 | |
| 285 | static void __init test_create_function_call(void) |
| 286 | { |
| 287 | unsigned int *iptr; |
| 288 | unsigned long dest; |
| 289 | |
| 290 | /* Check we can create a function call */ |
| 291 | iptr = (unsigned int *)ppc_function_entry(test_trampoline); |
| 292 | dest = ppc_function_entry(test_create_function_call); |
| 293 | patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK)); |
| 294 | check(instr_is_branch_to_addr(iptr, dest)); |
| 295 | } |
| 296 | |
| 297 | static void __init test_branch_bform(void) |
| 298 | { |
| 299 | unsigned long addr; |
| 300 | unsigned int *iptr, instr, flags; |
| 301 | |
| 302 | iptr = &instr; |
| 303 | addr = (unsigned long)iptr; |
| 304 | |
| 305 | /* The simplest case, branch to self, no flags */ |
| 306 | check(instr_is_branch_bform(0x40000000)); |
| 307 | /* All bits of target set, and flags */ |
| 308 | check(instr_is_branch_bform(0x43ffffff)); |
| 309 | /* High bit of opcode set, which is wrong */ |
| 310 | check(!instr_is_branch_bform(0xc3ffffff)); |
| 311 | /* Middle bits of opcode set, which is wrong */ |
| 312 | check(!instr_is_branch_bform(0x7bffffff)); |
| 313 | |
| 314 | /* Absolute conditional branch to 0x100 */ |
| 315 | instr = 0x43ff0103; |
| 316 | check(instr_is_branch_to_addr(&instr, 0x100)); |
| 317 | /* Absolute conditional branch to 0x20fc */ |
| 318 | instr = 0x43ff20ff; |
| 319 | check(instr_is_branch_to_addr(&instr, 0x20fc)); |
| 320 | /* Maximum positive relative conditional branch, + 32 KB - 4B */ |
| 321 | instr = 0x43ff7ffc; |
| 322 | check(instr_is_branch_to_addr(&instr, addr + 0x7FFC)); |
| 323 | /* Smallest negative relative conditional branch, - 4B */ |
| 324 | instr = 0x43fffffc; |
| 325 | check(instr_is_branch_to_addr(&instr, addr - 4)); |
| 326 | /* Largest negative relative conditional branch, - 32 KB */ |
| 327 | instr = 0x43ff8000; |
| 328 | check(instr_is_branch_to_addr(&instr, addr - 0x8000)); |
| 329 | |
| 330 | /* All condition code bits set & link */ |
| 331 | flags = 0x3ff000 | BRANCH_SET_LINK; |
| 332 | |
| 333 | /* Branch to self */ |
| 334 | instr = create_cond_branch(iptr, addr, flags); |
| 335 | check(instr_is_branch_to_addr(&instr, addr)); |
| 336 | |
| 337 | /* Branch to self - 0x100 */ |
| 338 | instr = create_cond_branch(iptr, addr - 0x100, flags); |
| 339 | check(instr_is_branch_to_addr(&instr, addr - 0x100)); |
| 340 | |
| 341 | /* Branch to self + 0x100 */ |
| 342 | instr = create_cond_branch(iptr, addr + 0x100, flags); |
| 343 | check(instr_is_branch_to_addr(&instr, addr + 0x100)); |
| 344 | |
| 345 | /* Maximum relative negative offset, - 32 KB */ |
| 346 | instr = create_cond_branch(iptr, addr - 0x8000, flags); |
| 347 | check(instr_is_branch_to_addr(&instr, addr - 0x8000)); |
| 348 | |
| 349 | /* Out of range relative negative offset, - 32 KB + 4*/ |
| 350 | instr = create_cond_branch(iptr, addr - 0x8004, flags); |
| 351 | check(instr == 0); |
| 352 | |
| 353 | /* Out of range relative positive offset, + 32 KB */ |
| 354 | instr = create_cond_branch(iptr, addr + 0x8000, flags); |
| 355 | check(instr == 0); |
| 356 | |
| 357 | /* Unaligned target */ |
| 358 | instr = create_cond_branch(iptr, addr + 3, flags); |
| 359 | check(instr == 0); |
| 360 | |
| 361 | /* Check flags are masked correctly */ |
| 362 | instr = create_cond_branch(iptr, addr, 0xFFFFFFFC); |
| 363 | check(instr_is_branch_to_addr(&instr, addr)); |
| 364 | check(instr == 0x43FF0000); |
| 365 | } |
| 366 | |
| 367 | static void __init test_translate_branch(void) |
| 368 | { |
| 369 | unsigned long addr; |
| 370 | unsigned int *p, *q; |
| 371 | void *buf; |
| 372 | |
| 373 | buf = vmalloc(PAGE_ALIGN(0x2000000 + 1)); |
| 374 | check(buf); |
| 375 | if (!buf) |
| 376 | return; |
| 377 | |
| 378 | /* Simple case, branch to self moved a little */ |
| 379 | p = buf; |
| 380 | addr = (unsigned long)p; |
| 381 | patch_branch(p, addr, 0); |
| 382 | check(instr_is_branch_to_addr(p, addr)); |
| 383 | q = p + 1; |
| 384 | patch_instruction(q, translate_branch(q, p)); |
| 385 | check(instr_is_branch_to_addr(q, addr)); |
| 386 | |
| 387 | /* Maximum negative case, move b . to addr + 32 MB */ |
| 388 | p = buf; |
| 389 | addr = (unsigned long)p; |
| 390 | patch_branch(p, addr, 0); |
| 391 | q = buf + 0x2000000; |
| 392 | patch_instruction(q, translate_branch(q, p)); |
| 393 | check(instr_is_branch_to_addr(p, addr)); |
| 394 | check(instr_is_branch_to_addr(q, addr)); |
| 395 | check(*q == 0x4a000000); |
| 396 | |
| 397 | /* Maximum positive case, move x to x - 32 MB + 4 */ |
| 398 | p = buf + 0x2000000; |
| 399 | addr = (unsigned long)p; |
| 400 | patch_branch(p, addr, 0); |
| 401 | q = buf + 4; |
| 402 | patch_instruction(q, translate_branch(q, p)); |
| 403 | check(instr_is_branch_to_addr(p, addr)); |
| 404 | check(instr_is_branch_to_addr(q, addr)); |
| 405 | check(*q == 0x49fffffc); |
| 406 | |
| 407 | /* Jump to x + 16 MB moved to x + 20 MB */ |
| 408 | p = buf; |
| 409 | addr = 0x1000000 + (unsigned long)buf; |
| 410 | patch_branch(p, addr, BRANCH_SET_LINK); |
| 411 | q = buf + 0x1400000; |
| 412 | patch_instruction(q, translate_branch(q, p)); |
| 413 | check(instr_is_branch_to_addr(p, addr)); |
| 414 | check(instr_is_branch_to_addr(q, addr)); |
| 415 | |
| 416 | /* Jump to x + 16 MB moved to x - 16 MB + 4 */ |
| 417 | p = buf + 0x1000000; |
| 418 | addr = 0x2000000 + (unsigned long)buf; |
| 419 | patch_branch(p, addr, 0); |
| 420 | q = buf + 4; |
| 421 | patch_instruction(q, translate_branch(q, p)); |
| 422 | check(instr_is_branch_to_addr(p, addr)); |
| 423 | check(instr_is_branch_to_addr(q, addr)); |
| 424 | |
| 425 | |
| 426 | /* Conditional branch tests */ |
| 427 | |
| 428 | /* Simple case, branch to self moved a little */ |
| 429 | p = buf; |
| 430 | addr = (unsigned long)p; |
| 431 | patch_instruction(p, create_cond_branch(p, addr, 0)); |
| 432 | check(instr_is_branch_to_addr(p, addr)); |
| 433 | q = p + 1; |
| 434 | patch_instruction(q, translate_branch(q, p)); |
| 435 | check(instr_is_branch_to_addr(q, addr)); |
| 436 | |
| 437 | /* Maximum negative case, move b . to addr + 32 KB */ |
| 438 | p = buf; |
| 439 | addr = (unsigned long)p; |
| 440 | patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC)); |
| 441 | q = buf + 0x8000; |
| 442 | patch_instruction(q, translate_branch(q, p)); |
| 443 | check(instr_is_branch_to_addr(p, addr)); |
| 444 | check(instr_is_branch_to_addr(q, addr)); |
| 445 | check(*q == 0x43ff8000); |
| 446 | |
| 447 | /* Maximum positive case, move x to x - 32 KB + 4 */ |
| 448 | p = buf + 0x8000; |
| 449 | addr = (unsigned long)p; |
| 450 | patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC)); |
| 451 | q = buf + 4; |
| 452 | patch_instruction(q, translate_branch(q, p)); |
| 453 | check(instr_is_branch_to_addr(p, addr)); |
| 454 | check(instr_is_branch_to_addr(q, addr)); |
| 455 | check(*q == 0x43ff7ffc); |
| 456 | |
| 457 | /* Jump to x + 12 KB moved to x + 20 KB */ |
| 458 | p = buf; |
| 459 | addr = 0x3000 + (unsigned long)buf; |
| 460 | patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK)); |
| 461 | q = buf + 0x5000; |
| 462 | patch_instruction(q, translate_branch(q, p)); |
| 463 | check(instr_is_branch_to_addr(p, addr)); |
| 464 | check(instr_is_branch_to_addr(q, addr)); |
| 465 | |
| 466 | /* Jump to x + 8 KB moved to x - 8 KB + 4 */ |
| 467 | p = buf + 0x2000; |
| 468 | addr = 0x4000 + (unsigned long)buf; |
| 469 | patch_instruction(p, create_cond_branch(p, addr, 0)); |
| 470 | q = buf + 4; |
| 471 | patch_instruction(q, translate_branch(q, p)); |
| 472 | check(instr_is_branch_to_addr(p, addr)); |
| 473 | check(instr_is_branch_to_addr(q, addr)); |
| 474 | |
| 475 | /* Free the buffer we were using */ |
| 476 | vfree(buf); |
| 477 | } |
| 478 | |
| 479 | static int __init test_code_patching(void) |
| 480 | { |
| 481 | printk(KERN_DEBUG "Running code patching self-tests ...\n"); |
| 482 | |
| 483 | test_branch_iform(); |
| 484 | test_branch_bform(); |
| 485 | test_create_function_call(); |
| 486 | test_translate_branch(); |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | late_initcall(test_code_patching); |
| 491 | |
| 492 | #endif /* CONFIG_CODE_PATCHING_SELFTEST */ |