2 * Copyright 2008 Michael Ellerman, IBM Corporation.
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.
10 #include <linux/kernel.h>
11 #include <linux/vmalloc.h>
12 #include <linux/init.h>
15 #include <asm/code-patching.h>
16 #include <asm/uaccess.h>
17 #include <asm/setup.h>
18 #include <asm/sections.h>
21 int patch_instruction(unsigned int *addr, unsigned int instr)
25 /* Make sure we aren't patching a freed init section */
26 if (*PTRRELOC(&init_mem_is_free) && init_section_contains(addr, 4)) {
27 pr_debug("Skipping init section patching addr: 0x%px\n", addr);
31 __put_user_size(instr, addr, 4, err);
34 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr));
38 int patch_branch(unsigned int *addr, unsigned long target, int flags)
40 return patch_instruction(addr, create_branch(addr, target, flags));
43 int patch_branch_site(s32 *site, unsigned long target, int flags)
47 addr = (unsigned int *)((unsigned long)site + *site);
48 return patch_instruction(addr, create_branch(addr, target, flags));
51 int patch_instruction_site(s32 *site, unsigned int instr)
55 addr = (unsigned int *)((unsigned long)site + *site);
56 return patch_instruction(addr, instr);
59 unsigned int create_branch(const unsigned int *addr,
60 unsigned long target, int flags)
62 unsigned int instruction;
66 if (! (flags & BRANCH_ABSOLUTE))
67 offset = offset - (unsigned long)addr;
69 /* Check we can represent the target in the instruction format */
70 if (offset < -0x2000000 || offset > 0x1fffffc || offset & 0x3)
73 /* Mask out the flags and target, so they don't step on each other. */
74 instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
79 unsigned int create_cond_branch(const unsigned int *addr,
80 unsigned long target, int flags)
82 unsigned int instruction;
86 if (! (flags & BRANCH_ABSOLUTE))
87 offset = offset - (unsigned long)addr;
89 /* Check we can represent the target in the instruction format */
90 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
93 /* Mask out the flags and target, so they don't step on each other. */
94 instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
99 static unsigned int branch_opcode(unsigned int instr)
101 return (instr >> 26) & 0x3F;
104 static int instr_is_branch_iform(unsigned int instr)
106 return branch_opcode(instr) == 18;
109 static int instr_is_branch_bform(unsigned int instr)
111 return branch_opcode(instr) == 16;
114 int instr_is_relative_branch(unsigned int instr)
116 if (instr & BRANCH_ABSOLUTE)
119 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
122 int instr_is_relative_link_branch(unsigned int instr)
124 return instr_is_relative_branch(instr) && (instr & BRANCH_SET_LINK);
127 static unsigned long branch_iform_target(const unsigned int *instr)
131 imm = *instr & 0x3FFFFFC;
133 /* If the top bit of the immediate value is set this is negative */
137 if ((*instr & BRANCH_ABSOLUTE) == 0)
138 imm += (unsigned long)instr;
140 return (unsigned long)imm;
143 static unsigned long branch_bform_target(const unsigned int *instr)
147 imm = *instr & 0xFFFC;
149 /* If the top bit of the immediate value is set this is negative */
153 if ((*instr & BRANCH_ABSOLUTE) == 0)
154 imm += (unsigned long)instr;
156 return (unsigned long)imm;
159 unsigned long branch_target(const unsigned int *instr)
161 if (instr_is_branch_iform(*instr))
162 return branch_iform_target(instr);
163 else if (instr_is_branch_bform(*instr))
164 return branch_bform_target(instr);
169 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
171 if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
172 return branch_target(instr) == addr;
177 unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
179 unsigned long target;
181 target = branch_target(src);
183 if (instr_is_branch_iform(*src))
184 return create_branch(dest, target, *src);
185 else if (instr_is_branch_bform(*src))
186 return create_cond_branch(dest, target, *src);
191 #ifdef CONFIG_PPC_BOOK3E_64
192 void __patch_exception(int exc, unsigned long addr)
194 extern unsigned int interrupt_base_book3e;
195 unsigned int *ibase = &interrupt_base_book3e;
197 /* Our exceptions vectors start with a NOP and -then- a branch
198 * to deal with single stepping from userspace which stops on
199 * the second instruction. Thus we need to patch the second
200 * instruction of the exception, not the first one
203 patch_branch(ibase + (exc / 4) + 1, addr, 0);
207 #ifdef CONFIG_CODE_PATCHING_SELFTEST
209 static void __init test_trampoline(void)
215 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
217 static void __init test_branch_iform(void)
222 addr = (unsigned long)&instr;
224 /* The simplest case, branch to self, no flags */
225 check(instr_is_branch_iform(0x48000000));
226 /* All bits of target set, and flags */
227 check(instr_is_branch_iform(0x4bffffff));
228 /* High bit of opcode set, which is wrong */
229 check(!instr_is_branch_iform(0xcbffffff));
230 /* Middle bits of opcode set, which is wrong */
231 check(!instr_is_branch_iform(0x7bffffff));
233 /* Simplest case, branch to self with link */
234 check(instr_is_branch_iform(0x48000001));
235 /* All bits of targets set */
236 check(instr_is_branch_iform(0x4bfffffd));
237 /* Some bits of targets set */
238 check(instr_is_branch_iform(0x4bff00fd));
239 /* Must be a valid branch to start with */
240 check(!instr_is_branch_iform(0x7bfffffd));
242 /* Absolute branch to 0x100 */
244 check(instr_is_branch_to_addr(&instr, 0x100));
245 /* Absolute branch to 0x420fc */
247 check(instr_is_branch_to_addr(&instr, 0x420fc));
248 /* Maximum positive relative branch, + 20MB - 4B */
250 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
251 /* Smallest negative relative branch, - 4B */
253 check(instr_is_branch_to_addr(&instr, addr - 4));
254 /* Largest negative relative branch, - 32 MB */
256 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
258 /* Branch to self, with link */
259 instr = create_branch(&instr, addr, BRANCH_SET_LINK);
260 check(instr_is_branch_to_addr(&instr, addr));
262 /* Branch to self - 0x100, with link */
263 instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
264 check(instr_is_branch_to_addr(&instr, addr - 0x100));
266 /* Branch to self + 0x100, no link */
267 instr = create_branch(&instr, addr + 0x100, 0);
268 check(instr_is_branch_to_addr(&instr, addr + 0x100));
270 /* Maximum relative negative offset, - 32 MB */
271 instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
272 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
274 /* Out of range relative negative offset, - 32 MB + 4*/
275 instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
278 /* Out of range relative positive offset, + 32 MB */
279 instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
282 /* Unaligned target */
283 instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
286 /* Check flags are masked correctly */
287 instr = create_branch(&instr, addr, 0xFFFFFFFC);
288 check(instr_is_branch_to_addr(&instr, addr));
289 check(instr == 0x48000000);
292 static void __init test_create_function_call(void)
297 /* Check we can create a function call */
298 iptr = (unsigned int *)ppc_function_entry(test_trampoline);
299 dest = ppc_function_entry(test_create_function_call);
300 patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
301 check(instr_is_branch_to_addr(iptr, dest));
304 static void __init test_branch_bform(void)
307 unsigned int *iptr, instr, flags;
310 addr = (unsigned long)iptr;
312 /* The simplest case, branch to self, no flags */
313 check(instr_is_branch_bform(0x40000000));
314 /* All bits of target set, and flags */
315 check(instr_is_branch_bform(0x43ffffff));
316 /* High bit of opcode set, which is wrong */
317 check(!instr_is_branch_bform(0xc3ffffff));
318 /* Middle bits of opcode set, which is wrong */
319 check(!instr_is_branch_bform(0x7bffffff));
321 /* Absolute conditional branch to 0x100 */
323 check(instr_is_branch_to_addr(&instr, 0x100));
324 /* Absolute conditional branch to 0x20fc */
326 check(instr_is_branch_to_addr(&instr, 0x20fc));
327 /* Maximum positive relative conditional branch, + 32 KB - 4B */
329 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
330 /* Smallest negative relative conditional branch, - 4B */
332 check(instr_is_branch_to_addr(&instr, addr - 4));
333 /* Largest negative relative conditional branch, - 32 KB */
335 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
337 /* All condition code bits set & link */
338 flags = 0x3ff000 | BRANCH_SET_LINK;
341 instr = create_cond_branch(iptr, addr, flags);
342 check(instr_is_branch_to_addr(&instr, addr));
344 /* Branch to self - 0x100 */
345 instr = create_cond_branch(iptr, addr - 0x100, flags);
346 check(instr_is_branch_to_addr(&instr, addr - 0x100));
348 /* Branch to self + 0x100 */
349 instr = create_cond_branch(iptr, addr + 0x100, flags);
350 check(instr_is_branch_to_addr(&instr, addr + 0x100));
352 /* Maximum relative negative offset, - 32 KB */
353 instr = create_cond_branch(iptr, addr - 0x8000, flags);
354 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
356 /* Out of range relative negative offset, - 32 KB + 4*/
357 instr = create_cond_branch(iptr, addr - 0x8004, flags);
360 /* Out of range relative positive offset, + 32 KB */
361 instr = create_cond_branch(iptr, addr + 0x8000, flags);
364 /* Unaligned target */
365 instr = create_cond_branch(iptr, addr + 3, flags);
368 /* Check flags are masked correctly */
369 instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
370 check(instr_is_branch_to_addr(&instr, addr));
371 check(instr == 0x43FF0000);
374 static void __init test_translate_branch(void)
380 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
385 /* Simple case, branch to self moved a little */
387 addr = (unsigned long)p;
388 patch_branch(p, addr, 0);
389 check(instr_is_branch_to_addr(p, addr));
391 patch_instruction(q, translate_branch(q, p));
392 check(instr_is_branch_to_addr(q, addr));
394 /* Maximum negative case, move b . to addr + 32 MB */
396 addr = (unsigned long)p;
397 patch_branch(p, addr, 0);
399 patch_instruction(q, translate_branch(q, p));
400 check(instr_is_branch_to_addr(p, addr));
401 check(instr_is_branch_to_addr(q, addr));
402 check(*q == 0x4a000000);
404 /* Maximum positive case, move x to x - 32 MB + 4 */
406 addr = (unsigned long)p;
407 patch_branch(p, addr, 0);
409 patch_instruction(q, translate_branch(q, p));
410 check(instr_is_branch_to_addr(p, addr));
411 check(instr_is_branch_to_addr(q, addr));
412 check(*q == 0x49fffffc);
414 /* Jump to x + 16 MB moved to x + 20 MB */
416 addr = 0x1000000 + (unsigned long)buf;
417 patch_branch(p, addr, BRANCH_SET_LINK);
419 patch_instruction(q, translate_branch(q, p));
420 check(instr_is_branch_to_addr(p, addr));
421 check(instr_is_branch_to_addr(q, addr));
423 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
425 addr = 0x2000000 + (unsigned long)buf;
426 patch_branch(p, addr, 0);
428 patch_instruction(q, translate_branch(q, p));
429 check(instr_is_branch_to_addr(p, addr));
430 check(instr_is_branch_to_addr(q, addr));
433 /* Conditional branch tests */
435 /* Simple case, branch to self moved a little */
437 addr = (unsigned long)p;
438 patch_instruction(p, create_cond_branch(p, addr, 0));
439 check(instr_is_branch_to_addr(p, addr));
441 patch_instruction(q, translate_branch(q, p));
442 check(instr_is_branch_to_addr(q, addr));
444 /* Maximum negative case, move b . to addr + 32 KB */
446 addr = (unsigned long)p;
447 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
449 patch_instruction(q, translate_branch(q, p));
450 check(instr_is_branch_to_addr(p, addr));
451 check(instr_is_branch_to_addr(q, addr));
452 check(*q == 0x43ff8000);
454 /* Maximum positive case, move x to x - 32 KB + 4 */
456 addr = (unsigned long)p;
457 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
459 patch_instruction(q, translate_branch(q, p));
460 check(instr_is_branch_to_addr(p, addr));
461 check(instr_is_branch_to_addr(q, addr));
462 check(*q == 0x43ff7ffc);
464 /* Jump to x + 12 KB moved to x + 20 KB */
466 addr = 0x3000 + (unsigned long)buf;
467 patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
469 patch_instruction(q, translate_branch(q, p));
470 check(instr_is_branch_to_addr(p, addr));
471 check(instr_is_branch_to_addr(q, addr));
473 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
475 addr = 0x4000 + (unsigned long)buf;
476 patch_instruction(p, create_cond_branch(p, addr, 0));
478 patch_instruction(q, translate_branch(q, p));
479 check(instr_is_branch_to_addr(p, addr));
480 check(instr_is_branch_to_addr(q, addr));
482 /* Free the buffer we were using */
486 static int __init test_code_patching(void)
488 printk(KERN_DEBUG "Running code patching self-tests ...\n");
492 test_create_function_call();
493 test_translate_branch();
497 late_initcall(test_code_patching);
499 #endif /* CONFIG_CODE_PATCHING_SELFTEST */