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>
14 #include <asm/code-patching.h>
17 void patch_instruction(unsigned int *addr
, unsigned int instr
)
20 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr
));
23 void patch_branch(unsigned int *addr
, unsigned long target
, int flags
)
25 patch_instruction(addr
, create_branch(addr
, target
, flags
));
28 unsigned int create_branch(const unsigned int *addr
,
29 unsigned long target
, int flags
)
31 unsigned int instruction
;
35 if (! (flags
& BRANCH_ABSOLUTE
))
36 offset
= offset
- (unsigned long)addr
;
38 /* Check we can represent the target in the instruction format */
39 if (offset
< -0x2000000 || offset
> 0x1fffffc || offset
& 0x3)
42 /* Mask out the flags and target, so they don't step on each other. */
43 instruction
= 0x48000000 | (flags
& 0x3) | (offset
& 0x03FFFFFC);
48 unsigned int create_cond_branch(const unsigned int *addr
,
49 unsigned long target
, int flags
)
51 unsigned int instruction
;
55 if (! (flags
& BRANCH_ABSOLUTE
))
56 offset
= offset
- (unsigned long)addr
;
58 /* Check we can represent the target in the instruction format */
59 if (offset
< -0x8000 || offset
> 0x7FFF || offset
& 0x3)
62 /* Mask out the flags and target, so they don't step on each other. */
63 instruction
= 0x40000000 | (flags
& 0x3FF0003) | (offset
& 0xFFFC);
68 static unsigned int branch_opcode(unsigned int instr
)
70 return (instr
>> 26) & 0x3F;
73 static int instr_is_branch_iform(unsigned int instr
)
75 return branch_opcode(instr
) == 18;
78 static int instr_is_branch_bform(unsigned int instr
)
80 return branch_opcode(instr
) == 16;
83 int instr_is_relative_branch(unsigned int instr
)
85 if (instr
& BRANCH_ABSOLUTE
)
88 return instr_is_branch_iform(instr
) || instr_is_branch_bform(instr
);
91 static unsigned long branch_iform_target(const unsigned int *instr
)
95 imm
= *instr
& 0x3FFFFFC;
97 /* If the top bit of the immediate value is set this is negative */
101 if ((*instr
& BRANCH_ABSOLUTE
) == 0)
102 imm
+= (unsigned long)instr
;
104 return (unsigned long)imm
;
107 static unsigned long branch_bform_target(const unsigned int *instr
)
111 imm
= *instr
& 0xFFFC;
113 /* If the top bit of the immediate value is set this is negative */
117 if ((*instr
& BRANCH_ABSOLUTE
) == 0)
118 imm
+= (unsigned long)instr
;
120 return (unsigned long)imm
;
123 unsigned long branch_target(const unsigned int *instr
)
125 if (instr_is_branch_iform(*instr
))
126 return branch_iform_target(instr
);
127 else if (instr_is_branch_bform(*instr
))
128 return branch_bform_target(instr
);
133 int instr_is_branch_to_addr(const unsigned int *instr
, unsigned long addr
)
135 if (instr_is_branch_iform(*instr
) || instr_is_branch_bform(*instr
))
136 return branch_target(instr
) == addr
;
141 unsigned int translate_branch(const unsigned int *dest
, const unsigned int *src
)
143 unsigned long target
;
145 target
= branch_target(src
);
147 if (instr_is_branch_iform(*src
))
148 return create_branch(dest
, target
, *src
);
149 else if (instr_is_branch_bform(*src
))
150 return create_cond_branch(dest
, target
, *src
);
156 #ifdef CONFIG_CODE_PATCHING_SELFTEST
158 static void __init
test_trampoline(void)
164 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
166 static void __init
test_branch_iform(void)
171 addr
= (unsigned long)&instr
;
173 /* The simplest case, branch to self, no flags */
174 check(instr_is_branch_iform(0x48000000));
175 /* All bits of target set, and flags */
176 check(instr_is_branch_iform(0x4bffffff));
177 /* High bit of opcode set, which is wrong */
178 check(!instr_is_branch_iform(0xcbffffff));
179 /* Middle bits of opcode set, which is wrong */
180 check(!instr_is_branch_iform(0x7bffffff));
182 /* Simplest case, branch to self with link */
183 check(instr_is_branch_iform(0x48000001));
184 /* All bits of targets set */
185 check(instr_is_branch_iform(0x4bfffffd));
186 /* Some bits of targets set */
187 check(instr_is_branch_iform(0x4bff00fd));
188 /* Must be a valid branch to start with */
189 check(!instr_is_branch_iform(0x7bfffffd));
191 /* Absolute branch to 0x100 */
193 check(instr_is_branch_to_addr(&instr
, 0x100));
194 /* Absolute branch to 0x420fc */
196 check(instr_is_branch_to_addr(&instr
, 0x420fc));
197 /* Maximum positive relative branch, + 20MB - 4B */
199 check(instr_is_branch_to_addr(&instr
, addr
+ 0x1FFFFFC));
200 /* Smallest negative relative branch, - 4B */
202 check(instr_is_branch_to_addr(&instr
, addr
- 4));
203 /* Largest negative relative branch, - 32 MB */
205 check(instr_is_branch_to_addr(&instr
, addr
- 0x2000000));
207 /* Branch to self, with link */
208 instr
= create_branch(&instr
, addr
, BRANCH_SET_LINK
);
209 check(instr_is_branch_to_addr(&instr
, addr
));
211 /* Branch to self - 0x100, with link */
212 instr
= create_branch(&instr
, addr
- 0x100, BRANCH_SET_LINK
);
213 check(instr_is_branch_to_addr(&instr
, addr
- 0x100));
215 /* Branch to self + 0x100, no link */
216 instr
= create_branch(&instr
, addr
+ 0x100, 0);
217 check(instr_is_branch_to_addr(&instr
, addr
+ 0x100));
219 /* Maximum relative negative offset, - 32 MB */
220 instr
= create_branch(&instr
, addr
- 0x2000000, BRANCH_SET_LINK
);
221 check(instr_is_branch_to_addr(&instr
, addr
- 0x2000000));
223 /* Out of range relative negative offset, - 32 MB + 4*/
224 instr
= create_branch(&instr
, addr
- 0x2000004, BRANCH_SET_LINK
);
227 /* Out of range relative positive offset, + 32 MB */
228 instr
= create_branch(&instr
, addr
+ 0x2000000, BRANCH_SET_LINK
);
231 /* Unaligned target */
232 instr
= create_branch(&instr
, addr
+ 3, BRANCH_SET_LINK
);
235 /* Check flags are masked correctly */
236 instr
= create_branch(&instr
, addr
, 0xFFFFFFFC);
237 check(instr_is_branch_to_addr(&instr
, addr
));
238 check(instr
== 0x48000000);
241 static void __init
test_create_function_call(void)
246 /* Check we can create a function call */
247 iptr
= (unsigned int *)ppc_function_entry(test_trampoline
);
248 dest
= ppc_function_entry(test_create_function_call
);
249 patch_instruction(iptr
, create_branch(iptr
, dest
, BRANCH_SET_LINK
));
250 check(instr_is_branch_to_addr(iptr
, dest
));
253 static void __init
test_branch_bform(void)
256 unsigned int *iptr
, instr
, flags
;
259 addr
= (unsigned long)iptr
;
261 /* The simplest case, branch to self, no flags */
262 check(instr_is_branch_bform(0x40000000));
263 /* All bits of target set, and flags */
264 check(instr_is_branch_bform(0x43ffffff));
265 /* High bit of opcode set, which is wrong */
266 check(!instr_is_branch_bform(0xc3ffffff));
267 /* Middle bits of opcode set, which is wrong */
268 check(!instr_is_branch_bform(0x7bffffff));
270 /* Absolute conditional branch to 0x100 */
272 check(instr_is_branch_to_addr(&instr
, 0x100));
273 /* Absolute conditional branch to 0x20fc */
275 check(instr_is_branch_to_addr(&instr
, 0x20fc));
276 /* Maximum positive relative conditional branch, + 32 KB - 4B */
278 check(instr_is_branch_to_addr(&instr
, addr
+ 0x7FFC));
279 /* Smallest negative relative conditional branch, - 4B */
281 check(instr_is_branch_to_addr(&instr
, addr
- 4));
282 /* Largest negative relative conditional branch, - 32 KB */
284 check(instr_is_branch_to_addr(&instr
, addr
- 0x8000));
286 /* All condition code bits set & link */
287 flags
= 0x3ff000 | BRANCH_SET_LINK
;
290 instr
= create_cond_branch(iptr
, addr
, flags
);
291 check(instr_is_branch_to_addr(&instr
, addr
));
293 /* Branch to self - 0x100 */
294 instr
= create_cond_branch(iptr
, addr
- 0x100, flags
);
295 check(instr_is_branch_to_addr(&instr
, addr
- 0x100));
297 /* Branch to self + 0x100 */
298 instr
= create_cond_branch(iptr
, addr
+ 0x100, flags
);
299 check(instr_is_branch_to_addr(&instr
, addr
+ 0x100));
301 /* Maximum relative negative offset, - 32 KB */
302 instr
= create_cond_branch(iptr
, addr
- 0x8000, flags
);
303 check(instr_is_branch_to_addr(&instr
, addr
- 0x8000));
305 /* Out of range relative negative offset, - 32 KB + 4*/
306 instr
= create_cond_branch(iptr
, addr
- 0x8004, flags
);
309 /* Out of range relative positive offset, + 32 KB */
310 instr
= create_cond_branch(iptr
, addr
+ 0x8000, flags
);
313 /* Unaligned target */
314 instr
= create_cond_branch(iptr
, addr
+ 3, flags
);
317 /* Check flags are masked correctly */
318 instr
= create_cond_branch(iptr
, addr
, 0xFFFFFFFC);
319 check(instr_is_branch_to_addr(&instr
, addr
));
320 check(instr
== 0x43FF0000);
323 static void __init
test_translate_branch(void)
329 buf
= vmalloc(PAGE_ALIGN(0x2000000 + 1));
334 /* Simple case, branch to self moved a little */
336 addr
= (unsigned long)p
;
337 patch_branch(p
, addr
, 0);
338 check(instr_is_branch_to_addr(p
, addr
));
340 patch_instruction(q
, translate_branch(q
, p
));
341 check(instr_is_branch_to_addr(q
, addr
));
343 /* Maximum negative case, move b . to addr + 32 MB */
345 addr
= (unsigned long)p
;
346 patch_branch(p
, addr
, 0);
348 patch_instruction(q
, translate_branch(q
, p
));
349 check(instr_is_branch_to_addr(p
, addr
));
350 check(instr_is_branch_to_addr(q
, addr
));
351 check(*q
== 0x4a000000);
353 /* Maximum positive case, move x to x - 32 MB + 4 */
355 addr
= (unsigned long)p
;
356 patch_branch(p
, addr
, 0);
358 patch_instruction(q
, translate_branch(q
, p
));
359 check(instr_is_branch_to_addr(p
, addr
));
360 check(instr_is_branch_to_addr(q
, addr
));
361 check(*q
== 0x49fffffc);
363 /* Jump to x + 16 MB moved to x + 20 MB */
365 addr
= 0x1000000 + (unsigned long)buf
;
366 patch_branch(p
, addr
, BRANCH_SET_LINK
);
368 patch_instruction(q
, translate_branch(q
, p
));
369 check(instr_is_branch_to_addr(p
, addr
));
370 check(instr_is_branch_to_addr(q
, addr
));
372 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
374 addr
= 0x2000000 + (unsigned long)buf
;
375 patch_branch(p
, addr
, 0);
377 patch_instruction(q
, translate_branch(q
, p
));
378 check(instr_is_branch_to_addr(p
, addr
));
379 check(instr_is_branch_to_addr(q
, addr
));
382 /* Conditional branch tests */
384 /* Simple case, branch to self moved a little */
386 addr
= (unsigned long)p
;
387 patch_instruction(p
, create_cond_branch(p
, addr
, 0));
388 check(instr_is_branch_to_addr(p
, addr
));
390 patch_instruction(q
, translate_branch(q
, p
));
391 check(instr_is_branch_to_addr(q
, addr
));
393 /* Maximum negative case, move b . to addr + 32 KB */
395 addr
= (unsigned long)p
;
396 patch_instruction(p
, create_cond_branch(p
, addr
, 0xFFFFFFFC));
398 patch_instruction(q
, translate_branch(q
, p
));
399 check(instr_is_branch_to_addr(p
, addr
));
400 check(instr_is_branch_to_addr(q
, addr
));
401 check(*q
== 0x43ff8000);
403 /* Maximum positive case, move x to x - 32 KB + 4 */
405 addr
= (unsigned long)p
;
406 patch_instruction(p
, create_cond_branch(p
, addr
, 0xFFFFFFFC));
408 patch_instruction(q
, translate_branch(q
, p
));
409 check(instr_is_branch_to_addr(p
, addr
));
410 check(instr_is_branch_to_addr(q
, addr
));
411 check(*q
== 0x43ff7ffc);
413 /* Jump to x + 12 KB moved to x + 20 KB */
415 addr
= 0x3000 + (unsigned long)buf
;
416 patch_instruction(p
, create_cond_branch(p
, addr
, BRANCH_SET_LINK
));
418 patch_instruction(q
, translate_branch(q
, p
));
419 check(instr_is_branch_to_addr(p
, addr
));
420 check(instr_is_branch_to_addr(q
, addr
));
422 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
424 addr
= 0x4000 + (unsigned long)buf
;
425 patch_instruction(p
, create_cond_branch(p
, addr
, 0));
427 patch_instruction(q
, translate_branch(q
, p
));
428 check(instr_is_branch_to_addr(p
, addr
));
429 check(instr_is_branch_to_addr(q
, addr
));
431 /* Free the buffer we were using */
435 static int __init
test_code_patching(void)
437 printk(KERN_DEBUG
"Running code patching self-tests ...\n");
441 test_create_function_call();
442 test_translate_branch();
446 late_initcall(test_code_patching
);
448 #endif /* CONFIG_CODE_PATCHING_SELFTEST */