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 <asm/code-patching.h>
14 void patch_instruction(unsigned int *addr
, unsigned int instr
)
17 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr
));
20 void patch_branch(unsigned int *addr
, unsigned long target
, int flags
)
22 patch_instruction(addr
, create_branch(addr
, target
, flags
));
25 unsigned int create_branch(const unsigned int *addr
,
26 unsigned long target
, int flags
)
28 unsigned int instruction
;
32 if (! (flags
& BRANCH_ABSOLUTE
))
33 offset
= offset
- (unsigned long)addr
;
35 /* Check we can represent the target in the instruction format */
36 if (offset
< -0x2000000 || offset
> 0x1fffffc || offset
& 0x3)
39 /* Mask out the flags and target, so they don't step on each other. */
40 instruction
= 0x48000000 | (flags
& 0x3) | (offset
& 0x03FFFFFC);
45 unsigned int create_cond_branch(const unsigned int *addr
,
46 unsigned long target
, int flags
)
48 unsigned int instruction
;
52 if (! (flags
& BRANCH_ABSOLUTE
))
53 offset
= offset
- (unsigned long)addr
;
55 /* Check we can represent the target in the instruction format */
56 if (offset
< -0x8000 || offset
> 0x7FFF || offset
& 0x3)
59 /* Mask out the flags and target, so they don't step on each other. */
60 instruction
= 0x40000000 | (flags
& 0x3FF0003) | (offset
& 0xFFFC);
65 static unsigned int branch_opcode(unsigned int instr
)
67 return (instr
>> 26) & 0x3F;
70 static int instr_is_branch_iform(unsigned int instr
)
72 return branch_opcode(instr
) == 18;
75 static int instr_is_branch_bform(unsigned int instr
)
77 return branch_opcode(instr
) == 16;
80 int instr_is_relative_branch(unsigned int instr
)
82 if (instr
& BRANCH_ABSOLUTE
)
85 return instr_is_branch_iform(instr
) || instr_is_branch_bform(instr
);
88 static unsigned long branch_iform_target(const unsigned int *instr
)
92 imm
= *instr
& 0x3FFFFFC;
94 /* If the top bit of the immediate value is set this is negative */
98 if ((*instr
& BRANCH_ABSOLUTE
) == 0)
99 imm
+= (unsigned long)instr
;
101 return (unsigned long)imm
;
104 static unsigned long branch_bform_target(const unsigned int *instr
)
108 imm
= *instr
& 0xFFFC;
110 /* If the top bit of the immediate value is set this is negative */
114 if ((*instr
& BRANCH_ABSOLUTE
) == 0)
115 imm
+= (unsigned long)instr
;
117 return (unsigned long)imm
;
120 unsigned long branch_target(const unsigned int *instr
)
122 if (instr_is_branch_iform(*instr
))
123 return branch_iform_target(instr
);
124 else if (instr_is_branch_bform(*instr
))
125 return branch_bform_target(instr
);
130 int instr_is_branch_to_addr(const unsigned int *instr
, unsigned long addr
)
132 if (instr_is_branch_iform(*instr
) || instr_is_branch_bform(*instr
))
133 return branch_target(instr
) == addr
;
138 unsigned int translate_branch(const unsigned int *dest
, const unsigned int *src
)
140 unsigned long target
;
142 target
= branch_target(src
);
144 if (instr_is_branch_iform(*src
))
145 return create_branch(dest
, target
, *src
);
146 else if (instr_is_branch_bform(*src
))
147 return create_cond_branch(dest
, target
, *src
);