1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright 2008 Michael Ellerman, IBM Corporation.
4  */
5 
6 #include <linux/vmalloc.h>
7 #include <linux/init.h>
8 
9 #include <asm/code-patching.h>
10 
11 static int __init instr_is_branch_to_addr(const u32 *instr, unsigned long addr)
12 {
13 	if (instr_is_branch_iform(ppc_inst_read(instr)) ||
14 	    instr_is_branch_bform(ppc_inst_read(instr)))
15 		return branch_target(instr) == addr;
16 
17 	return 0;
18 }
19 
20 static void __init test_trampoline(void)
21 {
22 	asm ("nop;nop;\n");
23 }
24 
25 #define check(x)	do {	\
26 	if (!(x))		\
27 		pr_err("code-patching: test failed at line %d\n", __LINE__); \
28 } while (0)
29 
30 static void __init test_branch_iform(void)
31 {
32 	int err;
33 	ppc_inst_t instr;
34 	u32 tmp[2];
35 	u32 *iptr = tmp;
36 	unsigned long addr = (unsigned long)tmp;
37 
38 	/* The simplest case, branch to self, no flags */
39 	check(instr_is_branch_iform(ppc_inst(0x48000000)));
40 	/* All bits of target set, and flags */
41 	check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
42 	/* High bit of opcode set, which is wrong */
43 	check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
44 	/* Middle bits of opcode set, which is wrong */
45 	check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
46 
47 	/* Simplest case, branch to self with link */
48 	check(instr_is_branch_iform(ppc_inst(0x48000001)));
49 	/* All bits of targets set */
50 	check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
51 	/* Some bits of targets set */
52 	check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
53 	/* Must be a valid branch to start with */
54 	check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
55 
56 	/* Absolute branch to 0x100 */
57 	patch_instruction(iptr, ppc_inst(0x48000103));
58 	check(instr_is_branch_to_addr(iptr, 0x100));
59 	/* Absolute branch to 0x420fc */
60 	patch_instruction(iptr, ppc_inst(0x480420ff));
61 	check(instr_is_branch_to_addr(iptr, 0x420fc));
62 	/* Maximum positive relative branch, + 20MB - 4B */
63 	patch_instruction(iptr, ppc_inst(0x49fffffc));
64 	check(instr_is_branch_to_addr(iptr, addr + 0x1FFFFFC));
65 	/* Smallest negative relative branch, - 4B */
66 	patch_instruction(iptr, ppc_inst(0x4bfffffc));
67 	check(instr_is_branch_to_addr(iptr, addr - 4));
68 	/* Largest negative relative branch, - 32 MB */
69 	patch_instruction(iptr, ppc_inst(0x4a000000));
70 	check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
71 
72 	/* Branch to self, with link */
73 	err = create_branch(&instr, iptr, addr, BRANCH_SET_LINK);
74 	patch_instruction(iptr, instr);
75 	check(instr_is_branch_to_addr(iptr, addr));
76 
77 	/* Branch to self - 0x100, with link */
78 	err = create_branch(&instr, iptr, addr - 0x100, BRANCH_SET_LINK);
79 	patch_instruction(iptr, instr);
80 	check(instr_is_branch_to_addr(iptr, addr - 0x100));
81 
82 	/* Branch to self + 0x100, no link */
83 	err = create_branch(&instr, iptr, addr + 0x100, 0);
84 	patch_instruction(iptr, instr);
85 	check(instr_is_branch_to_addr(iptr, addr + 0x100));
86 
87 	/* Maximum relative negative offset, - 32 MB */
88 	err = create_branch(&instr, iptr, addr - 0x2000000, BRANCH_SET_LINK);
89 	patch_instruction(iptr, instr);
90 	check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
91 
92 	/* Out of range relative negative offset, - 32 MB + 4*/
93 	err = create_branch(&instr, iptr, addr - 0x2000004, BRANCH_SET_LINK);
94 	check(err);
95 
96 	/* Out of range relative positive offset, + 32 MB */
97 	err = create_branch(&instr, iptr, addr + 0x2000000, BRANCH_SET_LINK);
98 	check(err);
99 
100 	/* Unaligned target */
101 	err = create_branch(&instr, iptr, addr + 3, BRANCH_SET_LINK);
102 	check(err);
103 
104 	/* Check flags are masked correctly */
105 	err = create_branch(&instr, iptr, addr, 0xFFFFFFFC);
106 	patch_instruction(iptr, instr);
107 	check(instr_is_branch_to_addr(iptr, addr));
108 	check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
109 }
110 
111 static void __init test_create_function_call(void)
112 {
113 	u32 *iptr;
114 	unsigned long dest;
115 	ppc_inst_t instr;
116 
117 	/* Check we can create a function call */
118 	iptr = (u32 *)ppc_function_entry(test_trampoline);
119 	dest = ppc_function_entry(test_create_function_call);
120 	create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
121 	patch_instruction(iptr, instr);
122 	check(instr_is_branch_to_addr(iptr, dest));
123 }
124 
125 static void __init test_branch_bform(void)
126 {
127 	int err;
128 	unsigned long addr;
129 	ppc_inst_t instr;
130 	u32 tmp[2];
131 	u32 *iptr = tmp;
132 	unsigned int flags;
133 
134 	addr = (unsigned long)iptr;
135 
136 	/* The simplest case, branch to self, no flags */
137 	check(instr_is_branch_bform(ppc_inst(0x40000000)));
138 	/* All bits of target set, and flags */
139 	check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
140 	/* High bit of opcode set, which is wrong */
141 	check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
142 	/* Middle bits of opcode set, which is wrong */
143 	check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
144 
145 	/* Absolute conditional branch to 0x100 */
146 	patch_instruction(iptr, ppc_inst(0x43ff0103));
147 	check(instr_is_branch_to_addr(iptr, 0x100));
148 	/* Absolute conditional branch to 0x20fc */
149 	patch_instruction(iptr, ppc_inst(0x43ff20ff));
150 	check(instr_is_branch_to_addr(iptr, 0x20fc));
151 	/* Maximum positive relative conditional branch, + 32 KB - 4B */
152 	patch_instruction(iptr, ppc_inst(0x43ff7ffc));
153 	check(instr_is_branch_to_addr(iptr, addr + 0x7FFC));
154 	/* Smallest negative relative conditional branch, - 4B */
155 	patch_instruction(iptr, ppc_inst(0x43fffffc));
156 	check(instr_is_branch_to_addr(iptr, addr - 4));
157 	/* Largest negative relative conditional branch, - 32 KB */
158 	patch_instruction(iptr, ppc_inst(0x43ff8000));
159 	check(instr_is_branch_to_addr(iptr, addr - 0x8000));
160 
161 	/* All condition code bits set & link */
162 	flags = 0x3ff000 | BRANCH_SET_LINK;
163 
164 	/* Branch to self */
165 	err = create_cond_branch(&instr, iptr, addr, flags);
166 	patch_instruction(iptr, instr);
167 	check(instr_is_branch_to_addr(iptr, addr));
168 
169 	/* Branch to self - 0x100 */
170 	err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
171 	patch_instruction(iptr, instr);
172 	check(instr_is_branch_to_addr(iptr, addr - 0x100));
173 
174 	/* Branch to self + 0x100 */
175 	err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
176 	patch_instruction(iptr, instr);
177 	check(instr_is_branch_to_addr(iptr, addr + 0x100));
178 
179 	/* Maximum relative negative offset, - 32 KB */
180 	err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
181 	patch_instruction(iptr, instr);
182 	check(instr_is_branch_to_addr(iptr, addr - 0x8000));
183 
184 	/* Out of range relative negative offset, - 32 KB + 4*/
185 	err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
186 	check(err);
187 
188 	/* Out of range relative positive offset, + 32 KB */
189 	err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
190 	check(err);
191 
192 	/* Unaligned target */
193 	err = create_cond_branch(&instr, iptr, addr + 3, flags);
194 	check(err);
195 
196 	/* Check flags are masked correctly */
197 	err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
198 	patch_instruction(iptr, instr);
199 	check(instr_is_branch_to_addr(iptr, addr));
200 	check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
201 }
202 
203 static void __init test_translate_branch(void)
204 {
205 	unsigned long addr;
206 	void *p, *q;
207 	ppc_inst_t instr;
208 	void *buf;
209 
210 	buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
211 	check(buf);
212 	if (!buf)
213 		return;
214 
215 	/* Simple case, branch to self moved a little */
216 	p = buf;
217 	addr = (unsigned long)p;
218 	patch_branch(p, addr, 0);
219 	check(instr_is_branch_to_addr(p, addr));
220 	q = p + 4;
221 	translate_branch(&instr, q, p);
222 	patch_instruction(q, instr);
223 	check(instr_is_branch_to_addr(q, addr));
224 
225 	/* Maximum negative case, move b . to addr + 32 MB */
226 	p = buf;
227 	addr = (unsigned long)p;
228 	patch_branch(p, addr, 0);
229 	q = buf + 0x2000000;
230 	translate_branch(&instr, q, p);
231 	patch_instruction(q, instr);
232 	check(instr_is_branch_to_addr(p, addr));
233 	check(instr_is_branch_to_addr(q, addr));
234 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
235 
236 	/* Maximum positive case, move x to x - 32 MB + 4 */
237 	p = buf + 0x2000000;
238 	addr = (unsigned long)p;
239 	patch_branch(p, addr, 0);
240 	q = buf + 4;
241 	translate_branch(&instr, q, p);
242 	patch_instruction(q, instr);
243 	check(instr_is_branch_to_addr(p, addr));
244 	check(instr_is_branch_to_addr(q, addr));
245 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
246 
247 	/* Jump to x + 16 MB moved to x + 20 MB */
248 	p = buf;
249 	addr = 0x1000000 + (unsigned long)buf;
250 	patch_branch(p, addr, BRANCH_SET_LINK);
251 	q = buf + 0x1400000;
252 	translate_branch(&instr, q, p);
253 	patch_instruction(q, instr);
254 	check(instr_is_branch_to_addr(p, addr));
255 	check(instr_is_branch_to_addr(q, addr));
256 
257 	/* Jump to x + 16 MB moved to x - 16 MB + 4 */
258 	p = buf + 0x1000000;
259 	addr = 0x2000000 + (unsigned long)buf;
260 	patch_branch(p, addr, 0);
261 	q = buf + 4;
262 	translate_branch(&instr, q, p);
263 	patch_instruction(q, instr);
264 	check(instr_is_branch_to_addr(p, addr));
265 	check(instr_is_branch_to_addr(q, addr));
266 
267 
268 	/* Conditional branch tests */
269 
270 	/* Simple case, branch to self moved a little */
271 	p = buf;
272 	addr = (unsigned long)p;
273 	create_cond_branch(&instr, p, addr, 0);
274 	patch_instruction(p, instr);
275 	check(instr_is_branch_to_addr(p, addr));
276 	q = buf + 4;
277 	translate_branch(&instr, q, p);
278 	patch_instruction(q, instr);
279 	check(instr_is_branch_to_addr(q, addr));
280 
281 	/* Maximum negative case, move b . to addr + 32 KB */
282 	p = buf;
283 	addr = (unsigned long)p;
284 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
285 	patch_instruction(p, instr);
286 	q = buf + 0x8000;
287 	translate_branch(&instr, q, p);
288 	patch_instruction(q, instr);
289 	check(instr_is_branch_to_addr(p, addr));
290 	check(instr_is_branch_to_addr(q, addr));
291 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
292 
293 	/* Maximum positive case, move x to x - 32 KB + 4 */
294 	p = buf + 0x8000;
295 	addr = (unsigned long)p;
296 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
297 	patch_instruction(p, instr);
298 	q = buf + 4;
299 	translate_branch(&instr, q, p);
300 	patch_instruction(q, instr);
301 	check(instr_is_branch_to_addr(p, addr));
302 	check(instr_is_branch_to_addr(q, addr));
303 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
304 
305 	/* Jump to x + 12 KB moved to x + 20 KB */
306 	p = buf;
307 	addr = 0x3000 + (unsigned long)buf;
308 	create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
309 	patch_instruction(p, instr);
310 	q = buf + 0x5000;
311 	translate_branch(&instr, q, p);
312 	patch_instruction(q, instr);
313 	check(instr_is_branch_to_addr(p, addr));
314 	check(instr_is_branch_to_addr(q, addr));
315 
316 	/* Jump to x + 8 KB moved to x - 8 KB + 4 */
317 	p = buf + 0x2000;
318 	addr = 0x4000 + (unsigned long)buf;
319 	create_cond_branch(&instr, p, addr, 0);
320 	patch_instruction(p, instr);
321 	q = buf + 4;
322 	translate_branch(&instr, q, p);
323 	patch_instruction(q, instr);
324 	check(instr_is_branch_to_addr(p, addr));
325 	check(instr_is_branch_to_addr(q, addr));
326 
327 	/* Free the buffer we were using */
328 	vfree(buf);
329 }
330 
331 static void __init test_prefixed_patching(void)
332 {
333 	u32 *iptr = (u32 *)ppc_function_entry(test_trampoline);
334 	u32 expected[2] = {OP_PREFIX << 26, 0};
335 	ppc_inst_t inst = ppc_inst_prefix(OP_PREFIX << 26, 0);
336 
337 	if (!IS_ENABLED(CONFIG_PPC64))
338 		return;
339 
340 	patch_instruction(iptr, inst);
341 
342 	check(!memcmp(iptr, expected, sizeof(expected)));
343 }
344 
345 static int __init test_code_patching(void)
346 {
347 	pr_info("Running code patching self-tests ...\n");
348 
349 	test_branch_iform();
350 	test_branch_bform();
351 	test_create_function_call();
352 	test_translate_branch();
353 	test_prefixed_patching();
354 
355 	return 0;
356 }
357 late_initcall(test_code_patching);
358