blob: 27957c4ea9e0e90bd150491fb04d81c2502689b0 [file] [log] [blame]
Michael Ellermanaaddd3e2008-06-24 11:32:21 +10001/*
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>
11#include <asm/code-patching.h>
12
13
Michael Ellermane7a57272008-06-24 11:32:22 +100014void patch_instruction(unsigned int *addr, unsigned int instr)
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100015{
Michael Ellermane7a57272008-06-24 11:32:22 +100016 *addr = instr;
17 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr));
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100018}
19
Michael Ellermane7a57272008-06-24 11:32:22 +100020void patch_branch(unsigned int *addr, unsigned long target, int flags)
21{
22 patch_instruction(addr, create_branch(addr, target, flags));
23}
24
25unsigned int create_branch(const unsigned int *addr,
26 unsigned long target, int flags)
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100027{
28 unsigned int instruction;
Michael Ellerman053a8582008-06-24 11:32:24 +100029 long offset;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100030
Michael Ellerman053a8582008-06-24 11:32:24 +100031 offset = target;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100032 if (! (flags & BRANCH_ABSOLUTE))
Michael Ellerman053a8582008-06-24 11:32:24 +100033 offset = offset - (unsigned long)addr;
34
35 /* Check we can represent the target in the instruction format */
36 if (offset < -0x2000000 || offset > 0x1fffffc || offset & 0x3)
37 return 0;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100038
39 /* Mask out the flags and target, so they don't step on each other. */
Michael Ellerman053a8582008-06-24 11:32:24 +100040 instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100041
Michael Ellermane7a57272008-06-24 11:32:22 +100042 return instruction;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100043}
Michael Ellerman411781a2008-06-24 11:32:29 +100044
45unsigned int create_cond_branch(const unsigned int *addr,
46 unsigned long target, int flags)
47{
48 unsigned int instruction;
49 long offset;
50
51 offset = target;
52 if (! (flags & BRANCH_ABSOLUTE))
53 offset = offset - (unsigned long)addr;
54
55 /* Check we can represent the target in the instruction format */
56 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
57 return 0;
58
59 /* Mask out the flags and target, so they don't step on each other. */
60 instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
61
62 return instruction;
63}
64
65static unsigned int branch_opcode(unsigned int instr)
66{
67 return (instr >> 26) & 0x3F;
68}
69
70static int instr_is_branch_iform(unsigned int instr)
71{
72 return branch_opcode(instr) == 18;
73}
74
75static int instr_is_branch_bform(unsigned int instr)
76{
77 return branch_opcode(instr) == 16;
78}
79
80int instr_is_relative_branch(unsigned int instr)
81{
82 if (instr & BRANCH_ABSOLUTE)
83 return 0;
84
85 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
86}
87
88static unsigned long branch_iform_target(const unsigned int *instr)
89{
90 signed long imm;
91
92 imm = *instr & 0x3FFFFFC;
93
94 /* If the top bit of the immediate value is set this is negative */
95 if (imm & 0x2000000)
96 imm -= 0x4000000;
97
98 if ((*instr & BRANCH_ABSOLUTE) == 0)
99 imm += (unsigned long)instr;
100
101 return (unsigned long)imm;
102}
103
104static unsigned long branch_bform_target(const unsigned int *instr)
105{
106 signed long imm;
107
108 imm = *instr & 0xFFFC;
109
110 /* If the top bit of the immediate value is set this is negative */
111 if (imm & 0x8000)
112 imm -= 0x10000;
113
114 if ((*instr & BRANCH_ABSOLUTE) == 0)
115 imm += (unsigned long)instr;
116
117 return (unsigned long)imm;
118}
119
120unsigned long branch_target(const unsigned int *instr)
121{
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);
126
127 return 0;
128}
129
130int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
131{
132 if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
133 return branch_target(instr) == addr;
134
135 return 0;
136}
137
138unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
139{
140 unsigned long target;
141
142 target = branch_target(src);
143
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);
148
149 return 0;
150}