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>
19 int patch_instruction(unsigned int *addr
, unsigned int instr
)
23 err
= __put_user(instr
, addr
);
26 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr
));
30 int patch_branch(unsigned int *addr
, unsigned long target
, int flags
)
32 return patch_instruction(addr
, create_branch(addr
, target
, flags
));
35 unsigned int create_branch(const unsigned int *addr
,
36 unsigned long target
, int flags
)
38 unsigned int instruction
;
42 if (! (flags
& BRANCH_ABSOLUTE
))
43 offset
= offset
- (unsigned long)addr
;
45 /* Check we can represent the target in the instruction format */
46 if (offset
< -0x2000000 || offset
> 0x1fffffc || offset
& 0x3)
49 /* Mask out the flags and target, so they don't step on each other. */
50 instruction
= 0x48000000 | (flags
& 0x3) | (offset
& 0x03FFFFFC);
55 unsigned int create_cond_branch(const unsigned int *addr
,
56 unsigned long target
, int flags
)
58 unsigned int instruction
;
62 if (! (flags
& BRANCH_ABSOLUTE
))
63 offset
= offset
- (unsigned long)addr
;
65 /* Check we can represent the target in the instruction format */
66 if (offset
< -0x8000 || offset
> 0x7FFF || offset
& 0x3)
69 /* Mask out the flags and target, so they don't step on each other. */
70 instruction
= 0x40000000 | (flags
& 0x3FF0003) | (offset
& 0xFFFC);
75 static unsigned int branch_opcode(unsigned int instr
)
77 return (instr
>> 26) & 0x3F;
80 static int instr_is_branch_iform(unsigned int instr
)
82 return branch_opcode(instr
) == 18;
85 static int instr_is_branch_bform(unsigned int instr
)
87 return branch_opcode(instr
) == 16;
90 int instr_is_relative_branch(unsigned int instr
)
92 if (instr
& BRANCH_ABSOLUTE
)
95 return instr_is_branch_iform(instr
) || instr_is_branch_bform(instr
);
98 static unsigned long branch_iform_target(const unsigned int *instr
)
102 imm
= *instr
& 0x3FFFFFC;
104 /* If the top bit of the immediate value is set this is negative */
108 if ((*instr
& BRANCH_ABSOLUTE
) == 0)
109 imm
+= (unsigned long)instr
;
111 return (unsigned long)imm
;
114 static unsigned long branch_bform_target(const unsigned int *instr
)
118 imm
= *instr
& 0xFFFC;
120 /* If the top bit of the immediate value is set this is negative */
124 if ((*instr
& BRANCH_ABSOLUTE
) == 0)
125 imm
+= (unsigned long)instr
;
127 return (unsigned long)imm
;
130 unsigned long branch_target(const unsigned int *instr
)
132 if (instr_is_branch_iform(*instr
))
133 return branch_iform_target(instr
);
134 else if (instr_is_branch_bform(*instr
))
135 return branch_bform_target(instr
);
140 int instr_is_branch_to_addr(const unsigned int *instr
, unsigned long addr
)
142 if (instr_is_branch_iform(*instr
) || instr_is_branch_bform(*instr
))
143 return branch_target(instr
) == addr
;
148 unsigned int translate_branch(const unsigned int *dest
, const unsigned int *src
)
150 unsigned long target
;
152 target
= branch_target(src
);
154 if (instr_is_branch_iform(*src
))
155 return create_branch(dest
, target
, *src
);
156 else if (instr_is_branch_bform(*src
))
157 return create_cond_branch(dest
, target
, *src
);
163 #ifdef CONFIG_CODE_PATCHING_SELFTEST
165 static void __init
test_trampoline(void)
171 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
173 static void __init
test_branch_iform(void)
178 addr
= (unsigned long)&instr
;
180 /* The simplest case, branch to self, no flags */
181 check(instr_is_branch_iform(0x48000000));
182 /* All bits of target set, and flags */
183 check(instr_is_branch_iform(0x4bffffff));
184 /* High bit of opcode set, which is wrong */
185 check(!instr_is_branch_iform(0xcbffffff));
186 /* Middle bits of opcode set, which is wrong */
187 check(!instr_is_branch_iform(0x7bffffff));
189 /* Simplest case, branch to self with link */
190 check(instr_is_branch_iform(0x48000001));
191 /* All bits of targets set */
192 check(instr_is_branch_iform(0x4bfffffd));
193 /* Some bits of targets set */
194 check(instr_is_branch_iform(0x4bff00fd));
195 /* Must be a valid branch to start with */
196 check(!instr_is_branch_iform(0x7bfffffd));
198 /* Absolute branch to 0x100 */
200 check(instr_is_branch_to_addr(&instr
, 0x100));
201 /* Absolute branch to 0x420fc */
203 check(instr_is_branch_to_addr(&instr
, 0x420fc));
204 /* Maximum positive relative branch, + 20MB - 4B */
206 check(instr_is_branch_to_addr(&instr
, addr
+ 0x1FFFFFC));
207 /* Smallest negative relative branch, - 4B */
209 check(instr_is_branch_to_addr(&instr
, addr
- 4));
210 /* Largest negative relative branch, - 32 MB */
212 check(instr_is_branch_to_addr(&instr
, addr
- 0x2000000));
214 /* Branch to self, with link */
215 instr
= create_branch(&instr
, addr
, BRANCH_SET_LINK
);
216 check(instr_is_branch_to_addr(&instr
, addr
));
218 /* Branch to self - 0x100, with link */
219 instr
= create_branch(&instr
, addr
- 0x100, BRANCH_SET_LINK
);
220 check(instr_is_branch_to_addr(&instr
, addr
- 0x100));
222 /* Branch to self + 0x100, no link */
223 instr
= create_branch(&instr
, addr
+ 0x100, 0);
224 check(instr_is_branch_to_addr(&instr
, addr
+ 0x100));
226 /* Maximum relative negative offset, - 32 MB */
227 instr
= create_branch(&instr
, addr
- 0x2000000, BRANCH_SET_LINK
);
228 check(instr_is_branch_to_addr(&instr
, addr
- 0x2000000));
230 /* Out of range relative negative offset, - 32 MB + 4*/
231 instr
= create_branch(&instr
, addr
- 0x2000004, BRANCH_SET_LINK
);
234 /* Out of range relative positive offset, + 32 MB */
235 instr
= create_branch(&instr
, addr
+ 0x2000000, BRANCH_SET_LINK
);
238 /* Unaligned target */
239 instr
= create_branch(&instr
, addr
+ 3, BRANCH_SET_LINK
);
242 /* Check flags are masked correctly */
243 instr
= create_branch(&instr
, addr
, 0xFFFFFFFC);
244 check(instr_is_branch_to_addr(&instr
, addr
));
245 check(instr
== 0x48000000);
248 static void __init
test_create_function_call(void)
253 /* Check we can create a function call */
254 iptr
= (unsigned int *)ppc_function_entry(test_trampoline
);
255 dest
= ppc_function_entry(test_create_function_call
);
256 patch_instruction(iptr
, create_branch(iptr
, dest
, BRANCH_SET_LINK
));
257 check(instr_is_branch_to_addr(iptr
, dest
));
260 static void __init
test_branch_bform(void)
263 unsigned int *iptr
, instr
, flags
;
266 addr
= (unsigned long)iptr
;
268 /* The simplest case, branch to self, no flags */
269 check(instr_is_branch_bform(0x40000000));
270 /* All bits of target set, and flags */
271 check(instr_is_branch_bform(0x43ffffff));
272 /* High bit of opcode set, which is wrong */
273 check(!instr_is_branch_bform(0xc3ffffff));
274 /* Middle bits of opcode set, which is wrong */
275 check(!instr_is_branch_bform(0x7bffffff));
277 /* Absolute conditional branch to 0x100 */
279 check(instr_is_branch_to_addr(&instr
, 0x100));
280 /* Absolute conditional branch to 0x20fc */
282 check(instr_is_branch_to_addr(&instr
, 0x20fc));
283 /* Maximum positive relative conditional branch, + 32 KB - 4B */
285 check(instr_is_branch_to_addr(&instr
, addr
+ 0x7FFC));
286 /* Smallest negative relative conditional branch, - 4B */
288 check(instr_is_branch_to_addr(&instr
, addr
- 4));
289 /* Largest negative relative conditional branch, - 32 KB */
291 check(instr_is_branch_to_addr(&instr
, addr
- 0x8000));
293 /* All condition code bits set & link */
294 flags
= 0x3ff000 | BRANCH_SET_LINK
;
297 instr
= create_cond_branch(iptr
, addr
, flags
);
298 check(instr_is_branch_to_addr(&instr
, addr
));
300 /* Branch to self - 0x100 */
301 instr
= create_cond_branch(iptr
, addr
- 0x100, flags
);
302 check(instr_is_branch_to_addr(&instr
, addr
- 0x100));
304 /* Branch to self + 0x100 */
305 instr
= create_cond_branch(iptr
, addr
+ 0x100, flags
);
306 check(instr_is_branch_to_addr(&instr
, addr
+ 0x100));
308 /* Maximum relative negative offset, - 32 KB */
309 instr
= create_cond_branch(iptr
, addr
- 0x8000, flags
);
310 check(instr_is_branch_to_addr(&instr
, addr
- 0x8000));
312 /* Out of range relative negative offset, - 32 KB + 4*/
313 instr
= create_cond_branch(iptr
, addr
- 0x8004, flags
);
316 /* Out of range relative positive offset, + 32 KB */
317 instr
= create_cond_branch(iptr
, addr
+ 0x8000, flags
);
320 /* Unaligned target */
321 instr
= create_cond_branch(iptr
, addr
+ 3, flags
);
324 /* Check flags are masked correctly */
325 instr
= create_cond_branch(iptr
, addr
, 0xFFFFFFFC);
326 check(instr_is_branch_to_addr(&instr
, addr
));
327 check(instr
== 0x43FF0000);
330 static void __init
test_translate_branch(void)
336 buf
= vmalloc(PAGE_ALIGN(0x2000000 + 1));
341 /* Simple case, branch to self moved a little */
343 addr
= (unsigned long)p
;
344 patch_branch(p
, addr
, 0);
345 check(instr_is_branch_to_addr(p
, addr
));
347 patch_instruction(q
, translate_branch(q
, p
));
348 check(instr_is_branch_to_addr(q
, addr
));
350 /* Maximum negative case, move b . to addr + 32 MB */
352 addr
= (unsigned long)p
;
353 patch_branch(p
, addr
, 0);
355 patch_instruction(q
, translate_branch(q
, p
));
356 check(instr_is_branch_to_addr(p
, addr
));
357 check(instr_is_branch_to_addr(q
, addr
));
358 check(*q
== 0x4a000000);
360 /* Maximum positive case, move x to x - 32 MB + 4 */
362 addr
= (unsigned long)p
;
363 patch_branch(p
, addr
, 0);
365 patch_instruction(q
, translate_branch(q
, p
));
366 check(instr_is_branch_to_addr(p
, addr
));
367 check(instr_is_branch_to_addr(q
, addr
));
368 check(*q
== 0x49fffffc);
370 /* Jump to x + 16 MB moved to x + 20 MB */
372 addr
= 0x1000000 + (unsigned long)buf
;
373 patch_branch(p
, addr
, BRANCH_SET_LINK
);
375 patch_instruction(q
, translate_branch(q
, p
));
376 check(instr_is_branch_to_addr(p
, addr
));
377 check(instr_is_branch_to_addr(q
, addr
));
379 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
381 addr
= 0x2000000 + (unsigned long)buf
;
382 patch_branch(p
, addr
, 0);
384 patch_instruction(q
, translate_branch(q
, p
));
385 check(instr_is_branch_to_addr(p
, addr
));
386 check(instr_is_branch_to_addr(q
, addr
));
389 /* Conditional branch tests */
391 /* Simple case, branch to self moved a little */
393 addr
= (unsigned long)p
;
394 patch_instruction(p
, create_cond_branch(p
, addr
, 0));
395 check(instr_is_branch_to_addr(p
, addr
));
397 patch_instruction(q
, translate_branch(q
, p
));
398 check(instr_is_branch_to_addr(q
, addr
));
400 /* Maximum negative case, move b . to addr + 32 KB */
402 addr
= (unsigned long)p
;
403 patch_instruction(p
, create_cond_branch(p
, addr
, 0xFFFFFFFC));
405 patch_instruction(q
, translate_branch(q
, p
));
406 check(instr_is_branch_to_addr(p
, addr
));
407 check(instr_is_branch_to_addr(q
, addr
));
408 check(*q
== 0x43ff8000);
410 /* Maximum positive case, move x to x - 32 KB + 4 */
412 addr
= (unsigned long)p
;
413 patch_instruction(p
, create_cond_branch(p
, addr
, 0xFFFFFFFC));
415 patch_instruction(q
, translate_branch(q
, p
));
416 check(instr_is_branch_to_addr(p
, addr
));
417 check(instr_is_branch_to_addr(q
, addr
));
418 check(*q
== 0x43ff7ffc);
420 /* Jump to x + 12 KB moved to x + 20 KB */
422 addr
= 0x3000 + (unsigned long)buf
;
423 patch_instruction(p
, create_cond_branch(p
, addr
, BRANCH_SET_LINK
));
425 patch_instruction(q
, translate_branch(q
, p
));
426 check(instr_is_branch_to_addr(p
, addr
));
427 check(instr_is_branch_to_addr(q
, addr
));
429 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
431 addr
= 0x4000 + (unsigned long)buf
;
432 patch_instruction(p
, create_cond_branch(p
, addr
, 0));
434 patch_instruction(q
, translate_branch(q
, p
));
435 check(instr_is_branch_to_addr(p
, addr
));
436 check(instr_is_branch_to_addr(q
, addr
));
438 /* Free the buffer we were using */
442 static int __init
test_code_patching(void)
444 printk(KERN_DEBUG
"Running code patching self-tests ...\n");
448 test_create_function_call();
449 test_translate_branch();
453 late_initcall(test_code_patching
);
455 #endif /* CONFIG_CODE_PATCHING_SELFTEST */