blob: c77c486fbf24949963b94fb4bcfc7b54390c90c5 [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>
Michael Ellermanae0dc732008-06-24 11:32:32 +100011#include <linux/vmalloc.h>
12#include <linux/init.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070013#include <linux/mm.h>
Michael Ellermanae0dc732008-06-24 11:32:32 +100014#include <asm/page.h>
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100015#include <asm/code-patching.h>
Steven Rostedtb6e37962012-04-26 08:31:18 +000016#include <asm/uaccess.h>
Michael Neuling1c38a842019-04-11 21:45:59 +100017#include <asm/setup.h>
18#include <asm/sections.h>
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100019
20
Steven Rostedtb6e37962012-04-26 08:31:18 +000021int patch_instruction(unsigned int *addr, unsigned int instr)
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100022{
Steven Rostedtb6e37962012-04-26 08:31:18 +000023 int err;
24
Michael Neuling1c38a842019-04-11 21:45:59 +100025 /* Make sure we aren't patching a freed init section */
26 if (init_mem_is_free && init_section_contains(addr, 4)) {
27 pr_debug("Skipping init section patching addr: 0x%px\n", addr);
28 return 0;
29 }
30
Benjamin Herrenschmidt636802e2012-09-04 15:08:28 +000031 __put_user_size(instr, addr, 4, err);
Steven Rostedtb6e37962012-04-26 08:31:18 +000032 if (err)
33 return err;
Michael Ellermane7a57272008-06-24 11:32:22 +100034 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr));
Steven Rostedtb6e37962012-04-26 08:31:18 +000035 return 0;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100036}
37
Steven Rostedtb6e37962012-04-26 08:31:18 +000038int patch_branch(unsigned int *addr, unsigned long target, int flags)
Michael Ellermane7a57272008-06-24 11:32:22 +100039{
Steven Rostedtb6e37962012-04-26 08:31:18 +000040 return patch_instruction(addr, create_branch(addr, target, flags));
Michael Ellermane7a57272008-06-24 11:32:22 +100041}
42
43unsigned int create_branch(const unsigned int *addr,
44 unsigned long target, int flags)
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100045{
46 unsigned int instruction;
Michael Ellerman053a8582008-06-24 11:32:24 +100047 long offset;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100048
Michael Ellerman053a8582008-06-24 11:32:24 +100049 offset = target;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100050 if (! (flags & BRANCH_ABSOLUTE))
Michael Ellerman053a8582008-06-24 11:32:24 +100051 offset = offset - (unsigned long)addr;
52
53 /* Check we can represent the target in the instruction format */
54 if (offset < -0x2000000 || offset > 0x1fffffc || offset & 0x3)
55 return 0;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100056
57 /* Mask out the flags and target, so they don't step on each other. */
Michael Ellerman053a8582008-06-24 11:32:24 +100058 instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100059
Michael Ellermane7a57272008-06-24 11:32:22 +100060 return instruction;
Michael Ellermanaaddd3e2008-06-24 11:32:21 +100061}
Michael Ellerman411781a2008-06-24 11:32:29 +100062
63unsigned int create_cond_branch(const unsigned int *addr,
64 unsigned long target, int flags)
65{
66 unsigned int instruction;
67 long offset;
68
69 offset = target;
70 if (! (flags & BRANCH_ABSOLUTE))
71 offset = offset - (unsigned long)addr;
72
73 /* Check we can represent the target in the instruction format */
74 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
75 return 0;
76
77 /* Mask out the flags and target, so they don't step on each other. */
78 instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
79
80 return instruction;
81}
82
83static unsigned int branch_opcode(unsigned int instr)
84{
85 return (instr >> 26) & 0x3F;
86}
87
88static int instr_is_branch_iform(unsigned int instr)
89{
90 return branch_opcode(instr) == 18;
91}
92
93static int instr_is_branch_bform(unsigned int instr)
94{
95 return branch_opcode(instr) == 16;
96}
97
98int instr_is_relative_branch(unsigned int instr)
99{
100 if (instr & BRANCH_ABSOLUTE)
101 return 0;
102
103 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
104}
105
Josh Poimboeuff6edf952017-11-16 11:45:37 -0600106int instr_is_relative_link_branch(unsigned int instr)
107{
108 return instr_is_relative_branch(instr) && (instr & BRANCH_SET_LINK);
109}
110
Michael Ellerman411781a2008-06-24 11:32:29 +1000111static unsigned long branch_iform_target(const unsigned int *instr)
112{
113 signed long imm;
114
115 imm = *instr & 0x3FFFFFC;
116
117 /* If the top bit of the immediate value is set this is negative */
118 if (imm & 0x2000000)
119 imm -= 0x4000000;
120
121 if ((*instr & BRANCH_ABSOLUTE) == 0)
122 imm += (unsigned long)instr;
123
124 return (unsigned long)imm;
125}
126
127static unsigned long branch_bform_target(const unsigned int *instr)
128{
129 signed long imm;
130
131 imm = *instr & 0xFFFC;
132
133 /* If the top bit of the immediate value is set this is negative */
134 if (imm & 0x8000)
135 imm -= 0x10000;
136
137 if ((*instr & BRANCH_ABSOLUTE) == 0)
138 imm += (unsigned long)instr;
139
140 return (unsigned long)imm;
141}
142
143unsigned long branch_target(const unsigned int *instr)
144{
145 if (instr_is_branch_iform(*instr))
146 return branch_iform_target(instr);
147 else if (instr_is_branch_bform(*instr))
148 return branch_bform_target(instr);
149
150 return 0;
151}
152
153int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
154{
155 if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
156 return branch_target(instr) == addr;
157
158 return 0;
159}
160
161unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
162{
163 unsigned long target;
164
165 target = branch_target(src);
166
167 if (instr_is_branch_iform(*src))
168 return create_branch(dest, target, *src);
169 else if (instr_is_branch_bform(*src))
170 return create_cond_branch(dest, target, *src);
171
172 return 0;
173}
Michael Ellermanae0dc732008-06-24 11:32:32 +1000174
Kevin Hao1e8341a2013-05-12 07:26:22 +0800175#ifdef CONFIG_PPC_BOOK3E_64
176void __patch_exception(int exc, unsigned long addr)
177{
178 extern unsigned int interrupt_base_book3e;
179 unsigned int *ibase = &interrupt_base_book3e;
180
181 /* Our exceptions vectors start with a NOP and -then- a branch
182 * to deal with single stepping from userspace which stops on
183 * the second instruction. Thus we need to patch the second
184 * instruction of the exception, not the first one
185 */
186
187 patch_branch(ibase + (exc / 4) + 1, addr, 0);
188}
189#endif
Michael Ellermanae0dc732008-06-24 11:32:32 +1000190
191#ifdef CONFIG_CODE_PATCHING_SELFTEST
192
193static void __init test_trampoline(void)
194{
195 asm ("nop;\n");
196}
197
198#define check(x) \
199 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
200
201static void __init test_branch_iform(void)
202{
203 unsigned int instr;
204 unsigned long addr;
205
206 addr = (unsigned long)&instr;
207
208 /* The simplest case, branch to self, no flags */
209 check(instr_is_branch_iform(0x48000000));
210 /* All bits of target set, and flags */
211 check(instr_is_branch_iform(0x4bffffff));
212 /* High bit of opcode set, which is wrong */
213 check(!instr_is_branch_iform(0xcbffffff));
214 /* Middle bits of opcode set, which is wrong */
215 check(!instr_is_branch_iform(0x7bffffff));
216
217 /* Simplest case, branch to self with link */
218 check(instr_is_branch_iform(0x48000001));
219 /* All bits of targets set */
220 check(instr_is_branch_iform(0x4bfffffd));
221 /* Some bits of targets set */
222 check(instr_is_branch_iform(0x4bff00fd));
223 /* Must be a valid branch to start with */
224 check(!instr_is_branch_iform(0x7bfffffd));
225
226 /* Absolute branch to 0x100 */
227 instr = 0x48000103;
228 check(instr_is_branch_to_addr(&instr, 0x100));
229 /* Absolute branch to 0x420fc */
230 instr = 0x480420ff;
231 check(instr_is_branch_to_addr(&instr, 0x420fc));
232 /* Maximum positive relative branch, + 20MB - 4B */
233 instr = 0x49fffffc;
234 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
235 /* Smallest negative relative branch, - 4B */
236 instr = 0x4bfffffc;
237 check(instr_is_branch_to_addr(&instr, addr - 4));
238 /* Largest negative relative branch, - 32 MB */
239 instr = 0x4a000000;
240 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
241
242 /* Branch to self, with link */
243 instr = create_branch(&instr, addr, BRANCH_SET_LINK);
244 check(instr_is_branch_to_addr(&instr, addr));
245
246 /* Branch to self - 0x100, with link */
247 instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
248 check(instr_is_branch_to_addr(&instr, addr - 0x100));
249
250 /* Branch to self + 0x100, no link */
251 instr = create_branch(&instr, addr + 0x100, 0);
252 check(instr_is_branch_to_addr(&instr, addr + 0x100));
253
254 /* Maximum relative negative offset, - 32 MB */
255 instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
256 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
257
258 /* Out of range relative negative offset, - 32 MB + 4*/
259 instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
260 check(instr == 0);
261
262 /* Out of range relative positive offset, + 32 MB */
263 instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
264 check(instr == 0);
265
266 /* Unaligned target */
267 instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
268 check(instr == 0);
269
270 /* Check flags are masked correctly */
271 instr = create_branch(&instr, addr, 0xFFFFFFFC);
272 check(instr_is_branch_to_addr(&instr, addr));
273 check(instr == 0x48000000);
274}
275
276static void __init test_create_function_call(void)
277{
278 unsigned int *iptr;
279 unsigned long dest;
280
281 /* Check we can create a function call */
282 iptr = (unsigned int *)ppc_function_entry(test_trampoline);
283 dest = ppc_function_entry(test_create_function_call);
284 patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
285 check(instr_is_branch_to_addr(iptr, dest));
286}
287
288static void __init test_branch_bform(void)
289{
290 unsigned long addr;
291 unsigned int *iptr, instr, flags;
292
293 iptr = &instr;
294 addr = (unsigned long)iptr;
295
296 /* The simplest case, branch to self, no flags */
297 check(instr_is_branch_bform(0x40000000));
298 /* All bits of target set, and flags */
299 check(instr_is_branch_bform(0x43ffffff));
300 /* High bit of opcode set, which is wrong */
301 check(!instr_is_branch_bform(0xc3ffffff));
302 /* Middle bits of opcode set, which is wrong */
303 check(!instr_is_branch_bform(0x7bffffff));
304
305 /* Absolute conditional branch to 0x100 */
306 instr = 0x43ff0103;
307 check(instr_is_branch_to_addr(&instr, 0x100));
308 /* Absolute conditional branch to 0x20fc */
309 instr = 0x43ff20ff;
310 check(instr_is_branch_to_addr(&instr, 0x20fc));
311 /* Maximum positive relative conditional branch, + 32 KB - 4B */
312 instr = 0x43ff7ffc;
313 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
314 /* Smallest negative relative conditional branch, - 4B */
315 instr = 0x43fffffc;
316 check(instr_is_branch_to_addr(&instr, addr - 4));
317 /* Largest negative relative conditional branch, - 32 KB */
318 instr = 0x43ff8000;
319 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
320
321 /* All condition code bits set & link */
322 flags = 0x3ff000 | BRANCH_SET_LINK;
323
324 /* Branch to self */
325 instr = create_cond_branch(iptr, addr, flags);
326 check(instr_is_branch_to_addr(&instr, addr));
327
328 /* Branch to self - 0x100 */
329 instr = create_cond_branch(iptr, addr - 0x100, flags);
330 check(instr_is_branch_to_addr(&instr, addr - 0x100));
331
332 /* Branch to self + 0x100 */
333 instr = create_cond_branch(iptr, addr + 0x100, flags);
334 check(instr_is_branch_to_addr(&instr, addr + 0x100));
335
336 /* Maximum relative negative offset, - 32 KB */
337 instr = create_cond_branch(iptr, addr - 0x8000, flags);
338 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
339
340 /* Out of range relative negative offset, - 32 KB + 4*/
341 instr = create_cond_branch(iptr, addr - 0x8004, flags);
342 check(instr == 0);
343
344 /* Out of range relative positive offset, + 32 KB */
345 instr = create_cond_branch(iptr, addr + 0x8000, flags);
346 check(instr == 0);
347
348 /* Unaligned target */
349 instr = create_cond_branch(iptr, addr + 3, flags);
350 check(instr == 0);
351
352 /* Check flags are masked correctly */
353 instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
354 check(instr_is_branch_to_addr(&instr, addr));
355 check(instr == 0x43FF0000);
356}
357
358static void __init test_translate_branch(void)
359{
360 unsigned long addr;
361 unsigned int *p, *q;
362 void *buf;
363
364 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
365 check(buf);
366 if (!buf)
367 return;
368
369 /* Simple case, branch to self moved a little */
370 p = buf;
371 addr = (unsigned long)p;
372 patch_branch(p, addr, 0);
373 check(instr_is_branch_to_addr(p, addr));
374 q = p + 1;
375 patch_instruction(q, translate_branch(q, p));
376 check(instr_is_branch_to_addr(q, addr));
377
378 /* Maximum negative case, move b . to addr + 32 MB */
379 p = buf;
380 addr = (unsigned long)p;
381 patch_branch(p, addr, 0);
382 q = buf + 0x2000000;
383 patch_instruction(q, translate_branch(q, p));
384 check(instr_is_branch_to_addr(p, addr));
385 check(instr_is_branch_to_addr(q, addr));
386 check(*q == 0x4a000000);
387
388 /* Maximum positive case, move x to x - 32 MB + 4 */
389 p = buf + 0x2000000;
390 addr = (unsigned long)p;
391 patch_branch(p, addr, 0);
392 q = buf + 4;
393 patch_instruction(q, translate_branch(q, p));
394 check(instr_is_branch_to_addr(p, addr));
395 check(instr_is_branch_to_addr(q, addr));
396 check(*q == 0x49fffffc);
397
398 /* Jump to x + 16 MB moved to x + 20 MB */
399 p = buf;
400 addr = 0x1000000 + (unsigned long)buf;
401 patch_branch(p, addr, BRANCH_SET_LINK);
402 q = buf + 0x1400000;
403 patch_instruction(q, translate_branch(q, p));
404 check(instr_is_branch_to_addr(p, addr));
405 check(instr_is_branch_to_addr(q, addr));
406
407 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
408 p = buf + 0x1000000;
409 addr = 0x2000000 + (unsigned long)buf;
410 patch_branch(p, addr, 0);
411 q = buf + 4;
412 patch_instruction(q, translate_branch(q, p));
413 check(instr_is_branch_to_addr(p, addr));
414 check(instr_is_branch_to_addr(q, addr));
415
416
417 /* Conditional branch tests */
418
419 /* Simple case, branch to self moved a little */
420 p = buf;
421 addr = (unsigned long)p;
422 patch_instruction(p, create_cond_branch(p, addr, 0));
423 check(instr_is_branch_to_addr(p, addr));
424 q = p + 1;
425 patch_instruction(q, translate_branch(q, p));
426 check(instr_is_branch_to_addr(q, addr));
427
428 /* Maximum negative case, move b . to addr + 32 KB */
429 p = buf;
430 addr = (unsigned long)p;
431 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
432 q = buf + 0x8000;
433 patch_instruction(q, translate_branch(q, p));
434 check(instr_is_branch_to_addr(p, addr));
435 check(instr_is_branch_to_addr(q, addr));
436 check(*q == 0x43ff8000);
437
438 /* Maximum positive case, move x to x - 32 KB + 4 */
439 p = buf + 0x8000;
440 addr = (unsigned long)p;
441 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
442 q = buf + 4;
443 patch_instruction(q, translate_branch(q, p));
444 check(instr_is_branch_to_addr(p, addr));
445 check(instr_is_branch_to_addr(q, addr));
446 check(*q == 0x43ff7ffc);
447
448 /* Jump to x + 12 KB moved to x + 20 KB */
449 p = buf;
450 addr = 0x3000 + (unsigned long)buf;
451 patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
452 q = buf + 0x5000;
453 patch_instruction(q, translate_branch(q, p));
454 check(instr_is_branch_to_addr(p, addr));
455 check(instr_is_branch_to_addr(q, addr));
456
457 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
458 p = buf + 0x2000;
459 addr = 0x4000 + (unsigned long)buf;
460 patch_instruction(p, create_cond_branch(p, addr, 0));
461 q = buf + 4;
462 patch_instruction(q, translate_branch(q, p));
463 check(instr_is_branch_to_addr(p, addr));
464 check(instr_is_branch_to_addr(q, addr));
465
466 /* Free the buffer we were using */
467 vfree(buf);
468}
469
470static int __init test_code_patching(void)
471{
472 printk(KERN_DEBUG "Running code patching self-tests ...\n");
473
474 test_branch_iform();
475 test_branch_bform();
476 test_create_function_call();
477 test_translate_branch();
478
479 return 0;
480}
481late_initcall(test_code_patching);
482
483#endif /* CONFIG_CODE_PATCHING_SELFTEST */