xref: /linux/arch/powerpc/lib/code-patching.c (revision 27ac792ca0b0a1e7e65f20342260650516c95864)
1aaddd3eaSMichael Ellerman /*
2aaddd3eaSMichael Ellerman  *  Copyright 2008 Michael Ellerman, IBM Corporation.
3aaddd3eaSMichael Ellerman  *
4aaddd3eaSMichael Ellerman  *  This program is free software; you can redistribute it and/or
5aaddd3eaSMichael Ellerman  *  modify it under the terms of the GNU General Public License
6aaddd3eaSMichael Ellerman  *  as published by the Free Software Foundation; either version
7aaddd3eaSMichael Ellerman  *  2 of the License, or (at your option) any later version.
8aaddd3eaSMichael Ellerman  */
9aaddd3eaSMichael Ellerman 
10aaddd3eaSMichael Ellerman #include <linux/kernel.h>
11ae0dc736SMichael Ellerman #include <linux/vmalloc.h>
12ae0dc736SMichael Ellerman #include <linux/init.h>
13*27ac792cSAndrea Righi #include <linux/mm.h>
14ae0dc736SMichael Ellerman #include <asm/page.h>
15aaddd3eaSMichael Ellerman #include <asm/code-patching.h>
16aaddd3eaSMichael Ellerman 
17aaddd3eaSMichael Ellerman 
18e7a57273SMichael Ellerman void patch_instruction(unsigned int *addr, unsigned int instr)
19aaddd3eaSMichael Ellerman {
20e7a57273SMichael Ellerman 	*addr = instr;
21e7a57273SMichael Ellerman 	asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr));
22aaddd3eaSMichael Ellerman }
23aaddd3eaSMichael Ellerman 
24e7a57273SMichael Ellerman void patch_branch(unsigned int *addr, unsigned long target, int flags)
25e7a57273SMichael Ellerman {
26e7a57273SMichael Ellerman 	patch_instruction(addr, create_branch(addr, target, flags));
27e7a57273SMichael Ellerman }
28e7a57273SMichael Ellerman 
29e7a57273SMichael Ellerman unsigned int create_branch(const unsigned int *addr,
30e7a57273SMichael Ellerman 			   unsigned long target, int flags)
31aaddd3eaSMichael Ellerman {
32aaddd3eaSMichael Ellerman 	unsigned int instruction;
33053a858eSMichael Ellerman 	long offset;
34aaddd3eaSMichael Ellerman 
35053a858eSMichael Ellerman 	offset = target;
36aaddd3eaSMichael Ellerman 	if (! (flags & BRANCH_ABSOLUTE))
37053a858eSMichael Ellerman 		offset = offset - (unsigned long)addr;
38053a858eSMichael Ellerman 
39053a858eSMichael Ellerman 	/* Check we can represent the target in the instruction format */
40053a858eSMichael Ellerman 	if (offset < -0x2000000 || offset > 0x1fffffc || offset & 0x3)
41053a858eSMichael Ellerman 		return 0;
42aaddd3eaSMichael Ellerman 
43aaddd3eaSMichael Ellerman 	/* Mask out the flags and target, so they don't step on each other. */
44053a858eSMichael Ellerman 	instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
45aaddd3eaSMichael Ellerman 
46e7a57273SMichael Ellerman 	return instruction;
47aaddd3eaSMichael Ellerman }
48411781a2SMichael Ellerman 
49411781a2SMichael Ellerman unsigned int create_cond_branch(const unsigned int *addr,
50411781a2SMichael Ellerman 				unsigned long target, int flags)
51411781a2SMichael Ellerman {
52411781a2SMichael Ellerman 	unsigned int instruction;
53411781a2SMichael Ellerman 	long offset;
54411781a2SMichael Ellerman 
55411781a2SMichael Ellerman 	offset = target;
56411781a2SMichael Ellerman 	if (! (flags & BRANCH_ABSOLUTE))
57411781a2SMichael Ellerman 		offset = offset - (unsigned long)addr;
58411781a2SMichael Ellerman 
59411781a2SMichael Ellerman 	/* Check we can represent the target in the instruction format */
60411781a2SMichael Ellerman 	if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
61411781a2SMichael Ellerman 		return 0;
62411781a2SMichael Ellerman 
63411781a2SMichael Ellerman 	/* Mask out the flags and target, so they don't step on each other. */
64411781a2SMichael Ellerman 	instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
65411781a2SMichael Ellerman 
66411781a2SMichael Ellerman 	return instruction;
67411781a2SMichael Ellerman }
68411781a2SMichael Ellerman 
69411781a2SMichael Ellerman static unsigned int branch_opcode(unsigned int instr)
70411781a2SMichael Ellerman {
71411781a2SMichael Ellerman 	return (instr >> 26) & 0x3F;
72411781a2SMichael Ellerman }
73411781a2SMichael Ellerman 
74411781a2SMichael Ellerman static int instr_is_branch_iform(unsigned int instr)
75411781a2SMichael Ellerman {
76411781a2SMichael Ellerman 	return branch_opcode(instr) == 18;
77411781a2SMichael Ellerman }
78411781a2SMichael Ellerman 
79411781a2SMichael Ellerman static int instr_is_branch_bform(unsigned int instr)
80411781a2SMichael Ellerman {
81411781a2SMichael Ellerman 	return branch_opcode(instr) == 16;
82411781a2SMichael Ellerman }
83411781a2SMichael Ellerman 
84411781a2SMichael Ellerman int instr_is_relative_branch(unsigned int instr)
85411781a2SMichael Ellerman {
86411781a2SMichael Ellerman 	if (instr & BRANCH_ABSOLUTE)
87411781a2SMichael Ellerman 		return 0;
88411781a2SMichael Ellerman 
89411781a2SMichael Ellerman 	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
90411781a2SMichael Ellerman }
91411781a2SMichael Ellerman 
92411781a2SMichael Ellerman static unsigned long branch_iform_target(const unsigned int *instr)
93411781a2SMichael Ellerman {
94411781a2SMichael Ellerman 	signed long imm;
95411781a2SMichael Ellerman 
96411781a2SMichael Ellerman 	imm = *instr & 0x3FFFFFC;
97411781a2SMichael Ellerman 
98411781a2SMichael Ellerman 	/* If the top bit of the immediate value is set this is negative */
99411781a2SMichael Ellerman 	if (imm & 0x2000000)
100411781a2SMichael Ellerman 		imm -= 0x4000000;
101411781a2SMichael Ellerman 
102411781a2SMichael Ellerman 	if ((*instr & BRANCH_ABSOLUTE) == 0)
103411781a2SMichael Ellerman 		imm += (unsigned long)instr;
104411781a2SMichael Ellerman 
105411781a2SMichael Ellerman 	return (unsigned long)imm;
106411781a2SMichael Ellerman }
107411781a2SMichael Ellerman 
108411781a2SMichael Ellerman static unsigned long branch_bform_target(const unsigned int *instr)
109411781a2SMichael Ellerman {
110411781a2SMichael Ellerman 	signed long imm;
111411781a2SMichael Ellerman 
112411781a2SMichael Ellerman 	imm = *instr & 0xFFFC;
113411781a2SMichael Ellerman 
114411781a2SMichael Ellerman 	/* If the top bit of the immediate value is set this is negative */
115411781a2SMichael Ellerman 	if (imm & 0x8000)
116411781a2SMichael Ellerman 		imm -= 0x10000;
117411781a2SMichael Ellerman 
118411781a2SMichael Ellerman 	if ((*instr & BRANCH_ABSOLUTE) == 0)
119411781a2SMichael Ellerman 		imm += (unsigned long)instr;
120411781a2SMichael Ellerman 
121411781a2SMichael Ellerman 	return (unsigned long)imm;
122411781a2SMichael Ellerman }
123411781a2SMichael Ellerman 
124411781a2SMichael Ellerman unsigned long branch_target(const unsigned int *instr)
125411781a2SMichael Ellerman {
126411781a2SMichael Ellerman 	if (instr_is_branch_iform(*instr))
127411781a2SMichael Ellerman 		return branch_iform_target(instr);
128411781a2SMichael Ellerman 	else if (instr_is_branch_bform(*instr))
129411781a2SMichael Ellerman 		return branch_bform_target(instr);
130411781a2SMichael Ellerman 
131411781a2SMichael Ellerman 	return 0;
132411781a2SMichael Ellerman }
133411781a2SMichael Ellerman 
134411781a2SMichael Ellerman int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
135411781a2SMichael Ellerman {
136411781a2SMichael Ellerman 	if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
137411781a2SMichael Ellerman 		return branch_target(instr) == addr;
138411781a2SMichael Ellerman 
139411781a2SMichael Ellerman 	return 0;
140411781a2SMichael Ellerman }
141411781a2SMichael Ellerman 
142411781a2SMichael Ellerman unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
143411781a2SMichael Ellerman {
144411781a2SMichael Ellerman 	unsigned long target;
145411781a2SMichael Ellerman 
146411781a2SMichael Ellerman 	target = branch_target(src);
147411781a2SMichael Ellerman 
148411781a2SMichael Ellerman 	if (instr_is_branch_iform(*src))
149411781a2SMichael Ellerman 		return create_branch(dest, target, *src);
150411781a2SMichael Ellerman 	else if (instr_is_branch_bform(*src))
151411781a2SMichael Ellerman 		return create_cond_branch(dest, target, *src);
152411781a2SMichael Ellerman 
153411781a2SMichael Ellerman 	return 0;
154411781a2SMichael Ellerman }
155ae0dc736SMichael Ellerman 
156ae0dc736SMichael Ellerman 
157ae0dc736SMichael Ellerman #ifdef CONFIG_CODE_PATCHING_SELFTEST
158ae0dc736SMichael Ellerman 
159ae0dc736SMichael Ellerman static void __init test_trampoline(void)
160ae0dc736SMichael Ellerman {
161ae0dc736SMichael Ellerman 	asm ("nop;\n");
162ae0dc736SMichael Ellerman }
163ae0dc736SMichael Ellerman 
164ae0dc736SMichael Ellerman #define check(x)	\
165ae0dc736SMichael Ellerman 	if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
166ae0dc736SMichael Ellerman 
167ae0dc736SMichael Ellerman static void __init test_branch_iform(void)
168ae0dc736SMichael Ellerman {
169ae0dc736SMichael Ellerman 	unsigned int instr;
170ae0dc736SMichael Ellerman 	unsigned long addr;
171ae0dc736SMichael Ellerman 
172ae0dc736SMichael Ellerman 	addr = (unsigned long)&instr;
173ae0dc736SMichael Ellerman 
174ae0dc736SMichael Ellerman 	/* The simplest case, branch to self, no flags */
175ae0dc736SMichael Ellerman 	check(instr_is_branch_iform(0x48000000));
176ae0dc736SMichael Ellerman 	/* All bits of target set, and flags */
177ae0dc736SMichael Ellerman 	check(instr_is_branch_iform(0x4bffffff));
178ae0dc736SMichael Ellerman 	/* High bit of opcode set, which is wrong */
179ae0dc736SMichael Ellerman 	check(!instr_is_branch_iform(0xcbffffff));
180ae0dc736SMichael Ellerman 	/* Middle bits of opcode set, which is wrong */
181ae0dc736SMichael Ellerman 	check(!instr_is_branch_iform(0x7bffffff));
182ae0dc736SMichael Ellerman 
183ae0dc736SMichael Ellerman 	/* Simplest case, branch to self with link */
184ae0dc736SMichael Ellerman 	check(instr_is_branch_iform(0x48000001));
185ae0dc736SMichael Ellerman 	/* All bits of targets set */
186ae0dc736SMichael Ellerman 	check(instr_is_branch_iform(0x4bfffffd));
187ae0dc736SMichael Ellerman 	/* Some bits of targets set */
188ae0dc736SMichael Ellerman 	check(instr_is_branch_iform(0x4bff00fd));
189ae0dc736SMichael Ellerman 	/* Must be a valid branch to start with */
190ae0dc736SMichael Ellerman 	check(!instr_is_branch_iform(0x7bfffffd));
191ae0dc736SMichael Ellerman 
192ae0dc736SMichael Ellerman 	/* Absolute branch to 0x100 */
193ae0dc736SMichael Ellerman 	instr = 0x48000103;
194ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, 0x100));
195ae0dc736SMichael Ellerman 	/* Absolute branch to 0x420fc */
196ae0dc736SMichael Ellerman 	instr = 0x480420ff;
197ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, 0x420fc));
198ae0dc736SMichael Ellerman 	/* Maximum positive relative branch, + 20MB - 4B */
199ae0dc736SMichael Ellerman 	instr = 0x49fffffc;
200ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
201ae0dc736SMichael Ellerman 	/* Smallest negative relative branch, - 4B */
202ae0dc736SMichael Ellerman 	instr = 0x4bfffffc;
203ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 4));
204ae0dc736SMichael Ellerman 	/* Largest negative relative branch, - 32 MB */
205ae0dc736SMichael Ellerman 	instr = 0x4a000000;
206ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
207ae0dc736SMichael Ellerman 
208ae0dc736SMichael Ellerman 	/* Branch to self, with link */
209ae0dc736SMichael Ellerman 	instr = create_branch(&instr, addr, BRANCH_SET_LINK);
210ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr));
211ae0dc736SMichael Ellerman 
212ae0dc736SMichael Ellerman 	/* Branch to self - 0x100, with link */
213ae0dc736SMichael Ellerman 	instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
214ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x100));
215ae0dc736SMichael Ellerman 
216ae0dc736SMichael Ellerman 	/* Branch to self + 0x100, no link */
217ae0dc736SMichael Ellerman 	instr = create_branch(&instr, addr + 0x100, 0);
218ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr + 0x100));
219ae0dc736SMichael Ellerman 
220ae0dc736SMichael Ellerman 	/* Maximum relative negative offset, - 32 MB */
221ae0dc736SMichael Ellerman 	instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
222ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
223ae0dc736SMichael Ellerman 
224ae0dc736SMichael Ellerman 	/* Out of range relative negative offset, - 32 MB + 4*/
225ae0dc736SMichael Ellerman 	instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
226ae0dc736SMichael Ellerman 	check(instr == 0);
227ae0dc736SMichael Ellerman 
228ae0dc736SMichael Ellerman 	/* Out of range relative positive offset, + 32 MB */
229ae0dc736SMichael Ellerman 	instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
230ae0dc736SMichael Ellerman 	check(instr == 0);
231ae0dc736SMichael Ellerman 
232ae0dc736SMichael Ellerman 	/* Unaligned target */
233ae0dc736SMichael Ellerman 	instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
234ae0dc736SMichael Ellerman 	check(instr == 0);
235ae0dc736SMichael Ellerman 
236ae0dc736SMichael Ellerman 	/* Check flags are masked correctly */
237ae0dc736SMichael Ellerman 	instr = create_branch(&instr, addr, 0xFFFFFFFC);
238ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr));
239ae0dc736SMichael Ellerman 	check(instr == 0x48000000);
240ae0dc736SMichael Ellerman }
241ae0dc736SMichael Ellerman 
242ae0dc736SMichael Ellerman static void __init test_create_function_call(void)
243ae0dc736SMichael Ellerman {
244ae0dc736SMichael Ellerman 	unsigned int *iptr;
245ae0dc736SMichael Ellerman 	unsigned long dest;
246ae0dc736SMichael Ellerman 
247ae0dc736SMichael Ellerman 	/* Check we can create a function call */
248ae0dc736SMichael Ellerman 	iptr = (unsigned int *)ppc_function_entry(test_trampoline);
249ae0dc736SMichael Ellerman 	dest = ppc_function_entry(test_create_function_call);
250ae0dc736SMichael Ellerman 	patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
251ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(iptr, dest));
252ae0dc736SMichael Ellerman }
253ae0dc736SMichael Ellerman 
254ae0dc736SMichael Ellerman static void __init test_branch_bform(void)
255ae0dc736SMichael Ellerman {
256ae0dc736SMichael Ellerman 	unsigned long addr;
257ae0dc736SMichael Ellerman 	unsigned int *iptr, instr, flags;
258ae0dc736SMichael Ellerman 
259ae0dc736SMichael Ellerman 	iptr = &instr;
260ae0dc736SMichael Ellerman 	addr = (unsigned long)iptr;
261ae0dc736SMichael Ellerman 
262ae0dc736SMichael Ellerman 	/* The simplest case, branch to self, no flags */
263ae0dc736SMichael Ellerman 	check(instr_is_branch_bform(0x40000000));
264ae0dc736SMichael Ellerman 	/* All bits of target set, and flags */
265ae0dc736SMichael Ellerman 	check(instr_is_branch_bform(0x43ffffff));
266ae0dc736SMichael Ellerman 	/* High bit of opcode set, which is wrong */
267ae0dc736SMichael Ellerman 	check(!instr_is_branch_bform(0xc3ffffff));
268ae0dc736SMichael Ellerman 	/* Middle bits of opcode set, which is wrong */
269ae0dc736SMichael Ellerman 	check(!instr_is_branch_bform(0x7bffffff));
270ae0dc736SMichael Ellerman 
271ae0dc736SMichael Ellerman 	/* Absolute conditional branch to 0x100 */
272ae0dc736SMichael Ellerman 	instr = 0x43ff0103;
273ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, 0x100));
274ae0dc736SMichael Ellerman 	/* Absolute conditional branch to 0x20fc */
275ae0dc736SMichael Ellerman 	instr = 0x43ff20ff;
276ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, 0x20fc));
277ae0dc736SMichael Ellerman 	/* Maximum positive relative conditional branch, + 32 KB - 4B */
278ae0dc736SMichael Ellerman 	instr = 0x43ff7ffc;
279ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
280ae0dc736SMichael Ellerman 	/* Smallest negative relative conditional branch, - 4B */
281ae0dc736SMichael Ellerman 	instr = 0x43fffffc;
282ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 4));
283ae0dc736SMichael Ellerman 	/* Largest negative relative conditional branch, - 32 KB */
284ae0dc736SMichael Ellerman 	instr = 0x43ff8000;
285ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x8000));
286ae0dc736SMichael Ellerman 
287ae0dc736SMichael Ellerman 	/* All condition code bits set & link */
288ae0dc736SMichael Ellerman 	flags = 0x3ff000 | BRANCH_SET_LINK;
289ae0dc736SMichael Ellerman 
290ae0dc736SMichael Ellerman 	/* Branch to self */
291ae0dc736SMichael Ellerman 	instr = create_cond_branch(iptr, addr, flags);
292ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr));
293ae0dc736SMichael Ellerman 
294ae0dc736SMichael Ellerman 	/* Branch to self - 0x100 */
295ae0dc736SMichael Ellerman 	instr = create_cond_branch(iptr, addr - 0x100, flags);
296ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x100));
297ae0dc736SMichael Ellerman 
298ae0dc736SMichael Ellerman 	/* Branch to self + 0x100 */
299ae0dc736SMichael Ellerman 	instr = create_cond_branch(iptr, addr + 0x100, flags);
300ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr + 0x100));
301ae0dc736SMichael Ellerman 
302ae0dc736SMichael Ellerman 	/* Maximum relative negative offset, - 32 KB */
303ae0dc736SMichael Ellerman 	instr = create_cond_branch(iptr, addr - 0x8000, flags);
304ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x8000));
305ae0dc736SMichael Ellerman 
306ae0dc736SMichael Ellerman 	/* Out of range relative negative offset, - 32 KB + 4*/
307ae0dc736SMichael Ellerman 	instr = create_cond_branch(iptr, addr - 0x8004, flags);
308ae0dc736SMichael Ellerman 	check(instr == 0);
309ae0dc736SMichael Ellerman 
310ae0dc736SMichael Ellerman 	/* Out of range relative positive offset, + 32 KB */
311ae0dc736SMichael Ellerman 	instr = create_cond_branch(iptr, addr + 0x8000, flags);
312ae0dc736SMichael Ellerman 	check(instr == 0);
313ae0dc736SMichael Ellerman 
314ae0dc736SMichael Ellerman 	/* Unaligned target */
315ae0dc736SMichael Ellerman 	instr = create_cond_branch(iptr, addr + 3, flags);
316ae0dc736SMichael Ellerman 	check(instr == 0);
317ae0dc736SMichael Ellerman 
318ae0dc736SMichael Ellerman 	/* Check flags are masked correctly */
319ae0dc736SMichael Ellerman 	instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
320ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr));
321ae0dc736SMichael Ellerman 	check(instr == 0x43FF0000);
322ae0dc736SMichael Ellerman }
323ae0dc736SMichael Ellerman 
324ae0dc736SMichael Ellerman static void __init test_translate_branch(void)
325ae0dc736SMichael Ellerman {
326ae0dc736SMichael Ellerman 	unsigned long addr;
327ae0dc736SMichael Ellerman 	unsigned int *p, *q;
328ae0dc736SMichael Ellerman 	void *buf;
329ae0dc736SMichael Ellerman 
330ae0dc736SMichael Ellerman 	buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
331ae0dc736SMichael Ellerman 	check(buf);
332ae0dc736SMichael Ellerman 	if (!buf)
333ae0dc736SMichael Ellerman 		return;
334ae0dc736SMichael Ellerman 
335ae0dc736SMichael Ellerman 	/* Simple case, branch to self moved a little */
336ae0dc736SMichael Ellerman 	p = buf;
337ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
338ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
339ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
340ae0dc736SMichael Ellerman 	q = p + 1;
341ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
342ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
343ae0dc736SMichael Ellerman 
344ae0dc736SMichael Ellerman 	/* Maximum negative case, move b . to addr + 32 MB */
345ae0dc736SMichael Ellerman 	p = buf;
346ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
347ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
348ae0dc736SMichael Ellerman 	q = buf + 0x2000000;
349ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
350ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
351ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
352ae0dc736SMichael Ellerman 	check(*q == 0x4a000000);
353ae0dc736SMichael Ellerman 
354ae0dc736SMichael Ellerman 	/* Maximum positive case, move x to x - 32 MB + 4 */
355ae0dc736SMichael Ellerman 	p = buf + 0x2000000;
356ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
357ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
358ae0dc736SMichael Ellerman 	q = buf + 4;
359ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
360ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
361ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
362ae0dc736SMichael Ellerman 	check(*q == 0x49fffffc);
363ae0dc736SMichael Ellerman 
364ae0dc736SMichael Ellerman 	/* Jump to x + 16 MB moved to x + 20 MB */
365ae0dc736SMichael Ellerman 	p = buf;
366ae0dc736SMichael Ellerman 	addr = 0x1000000 + (unsigned long)buf;
367ae0dc736SMichael Ellerman 	patch_branch(p, addr, BRANCH_SET_LINK);
368ae0dc736SMichael Ellerman 	q = buf + 0x1400000;
369ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
370ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
371ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
372ae0dc736SMichael Ellerman 
373ae0dc736SMichael Ellerman 	/* Jump to x + 16 MB moved to x - 16 MB + 4 */
374ae0dc736SMichael Ellerman 	p = buf + 0x1000000;
375ae0dc736SMichael Ellerman 	addr = 0x2000000 + (unsigned long)buf;
376ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
377ae0dc736SMichael Ellerman 	q = buf + 4;
378ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
379ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
380ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
381ae0dc736SMichael Ellerman 
382ae0dc736SMichael Ellerman 
383ae0dc736SMichael Ellerman 	/* Conditional branch tests */
384ae0dc736SMichael Ellerman 
385ae0dc736SMichael Ellerman 	/* Simple case, branch to self moved a little */
386ae0dc736SMichael Ellerman 	p = buf;
387ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
388ae0dc736SMichael Ellerman 	patch_instruction(p, create_cond_branch(p, addr, 0));
389ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
390ae0dc736SMichael Ellerman 	q = p + 1;
391ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
392ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
393ae0dc736SMichael Ellerman 
394ae0dc736SMichael Ellerman 	/* Maximum negative case, move b . to addr + 32 KB */
395ae0dc736SMichael Ellerman 	p = buf;
396ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
397ae0dc736SMichael Ellerman 	patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
398ae0dc736SMichael Ellerman 	q = buf + 0x8000;
399ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
400ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
401ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
402ae0dc736SMichael Ellerman 	check(*q == 0x43ff8000);
403ae0dc736SMichael Ellerman 
404ae0dc736SMichael Ellerman 	/* Maximum positive case, move x to x - 32 KB + 4 */
405ae0dc736SMichael Ellerman 	p = buf + 0x8000;
406ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
407ae0dc736SMichael Ellerman 	patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
408ae0dc736SMichael Ellerman 	q = buf + 4;
409ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
410ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
411ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
412ae0dc736SMichael Ellerman 	check(*q == 0x43ff7ffc);
413ae0dc736SMichael Ellerman 
414ae0dc736SMichael Ellerman 	/* Jump to x + 12 KB moved to x + 20 KB */
415ae0dc736SMichael Ellerman 	p = buf;
416ae0dc736SMichael Ellerman 	addr = 0x3000 + (unsigned long)buf;
417ae0dc736SMichael Ellerman 	patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
418ae0dc736SMichael Ellerman 	q = buf + 0x5000;
419ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
420ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
421ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
422ae0dc736SMichael Ellerman 
423ae0dc736SMichael Ellerman 	/* Jump to x + 8 KB moved to x - 8 KB + 4 */
424ae0dc736SMichael Ellerman 	p = buf + 0x2000;
425ae0dc736SMichael Ellerman 	addr = 0x4000 + (unsigned long)buf;
426ae0dc736SMichael Ellerman 	patch_instruction(p, create_cond_branch(p, addr, 0));
427ae0dc736SMichael Ellerman 	q = buf + 4;
428ae0dc736SMichael Ellerman 	patch_instruction(q, translate_branch(q, p));
429ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
430ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
431ae0dc736SMichael Ellerman 
432ae0dc736SMichael Ellerman 	/* Free the buffer we were using */
433ae0dc736SMichael Ellerman 	vfree(buf);
434ae0dc736SMichael Ellerman }
435ae0dc736SMichael Ellerman 
436ae0dc736SMichael Ellerman static int __init test_code_patching(void)
437ae0dc736SMichael Ellerman {
438ae0dc736SMichael Ellerman 	printk(KERN_DEBUG "Running code patching self-tests ...\n");
439ae0dc736SMichael Ellerman 
440ae0dc736SMichael Ellerman 	test_branch_iform();
441ae0dc736SMichael Ellerman 	test_branch_bform();
442ae0dc736SMichael Ellerman 	test_create_function_call();
443ae0dc736SMichael Ellerman 	test_translate_branch();
444ae0dc736SMichael Ellerman 
445ae0dc736SMichael Ellerman 	return 0;
446ae0dc736SMichael Ellerman }
447ae0dc736SMichael Ellerman late_initcall(test_code_patching);
448ae0dc736SMichael Ellerman 
449ae0dc736SMichael Ellerman #endif /* CONFIG_CODE_PATCHING_SELFTEST */
450