Loading...
1/*
2 * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
3 *
4 * Modifications for ppc64:
5 * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
6 *
7 * Copyright 2008 Michael Ellerman, IBM Corporation.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#include <linux/types.h>
16#include <linux/jump_label.h>
17#include <linux/kernel.h>
18#include <linux/string.h>
19#include <linux/init.h>
20#include <linux/sched/mm.h>
21#include <asm/cputable.h>
22#include <asm/code-patching.h>
23#include <asm/page.h>
24#include <asm/sections.h>
25#include <asm/setup.h>
26#include <asm/security_features.h>
27#include <asm/firmware.h>
28
29struct fixup_entry {
30 unsigned long mask;
31 unsigned long value;
32 long start_off;
33 long end_off;
34 long alt_start_off;
35 long alt_end_off;
36};
37
38static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
39{
40 /*
41 * We store the offset to the code as a negative offset from
42 * the start of the alt_entry, to support the VDSO. This
43 * routine converts that back into an actual address.
44 */
45 return (unsigned int *)((unsigned long)fcur + offset);
46}
47
48static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
49 unsigned int *alt_start, unsigned int *alt_end)
50{
51 unsigned int instr;
52
53 instr = *src;
54
55 if (instr_is_relative_branch(*src)) {
56 unsigned int *target = (unsigned int *)branch_target(src);
57
58 /* Branch within the section doesn't need translating */
59 if (target < alt_start || target > alt_end) {
60 instr = translate_branch(dest, src);
61 if (!instr)
62 return 1;
63 }
64 }
65
66 raw_patch_instruction(dest, instr);
67
68 return 0;
69}
70
71static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
72{
73 unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
74
75 start = calc_addr(fcur, fcur->start_off);
76 end = calc_addr(fcur, fcur->end_off);
77 alt_start = calc_addr(fcur, fcur->alt_start_off);
78 alt_end = calc_addr(fcur, fcur->alt_end_off);
79
80 if ((alt_end - alt_start) > (end - start))
81 return 1;
82
83 if ((value & fcur->mask) == fcur->value)
84 return 0;
85
86 src = alt_start;
87 dest = start;
88
89 for (; src < alt_end; src++, dest++) {
90 if (patch_alt_instruction(src, dest, alt_start, alt_end))
91 return 1;
92 }
93
94 for (; dest < end; dest++)
95 raw_patch_instruction(dest, PPC_INST_NOP);
96
97 return 0;
98}
99
100void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
101{
102 struct fixup_entry *fcur, *fend;
103
104 fcur = fixup_start;
105 fend = fixup_end;
106
107 for (; fcur < fend; fcur++) {
108 if (patch_feature_section(value, fcur)) {
109 WARN_ON(1);
110 printk("Unable to patch feature section at %p - %p" \
111 " with %p - %p\n",
112 calc_addr(fcur, fcur->start_off),
113 calc_addr(fcur, fcur->end_off),
114 calc_addr(fcur, fcur->alt_start_off),
115 calc_addr(fcur, fcur->alt_end_off));
116 }
117 }
118}
119
120#ifdef CONFIG_PPC_BOOK3S_64
121void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
122{
123 unsigned int instrs[3], *dest;
124 long *start, *end;
125 int i;
126
127 start = PTRRELOC(&__start___stf_entry_barrier_fixup),
128 end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
129
130 instrs[0] = 0x60000000; /* nop */
131 instrs[1] = 0x60000000; /* nop */
132 instrs[2] = 0x60000000; /* nop */
133
134 i = 0;
135 if (types & STF_BARRIER_FALLBACK) {
136 instrs[i++] = 0x7d4802a6; /* mflr r10 */
137 instrs[i++] = 0x60000000; /* branch patched below */
138 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
139 } else if (types & STF_BARRIER_EIEIO) {
140 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
141 } else if (types & STF_BARRIER_SYNC_ORI) {
142 instrs[i++] = 0x7c0004ac; /* hwsync */
143 instrs[i++] = 0xe94d0000; /* ld r10,0(r13) */
144 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
145 }
146
147 for (i = 0; start < end; start++, i++) {
148 dest = (void *)start + *start;
149
150 pr_devel("patching dest %lx\n", (unsigned long)dest);
151
152 patch_instruction(dest, instrs[0]);
153
154 if (types & STF_BARRIER_FALLBACK)
155 patch_branch(dest + 1, (unsigned long)&stf_barrier_fallback,
156 BRANCH_SET_LINK);
157 else
158 patch_instruction(dest + 1, instrs[1]);
159
160 patch_instruction(dest + 2, instrs[2]);
161 }
162
163 printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
164 (types == STF_BARRIER_NONE) ? "no" :
165 (types == STF_BARRIER_FALLBACK) ? "fallback" :
166 (types == STF_BARRIER_EIEIO) ? "eieio" :
167 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
168 : "unknown");
169}
170
171void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
172{
173 unsigned int instrs[6], *dest;
174 long *start, *end;
175 int i;
176
177 start = PTRRELOC(&__start___stf_exit_barrier_fixup),
178 end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
179
180 instrs[0] = 0x60000000; /* nop */
181 instrs[1] = 0x60000000; /* nop */
182 instrs[2] = 0x60000000; /* nop */
183 instrs[3] = 0x60000000; /* nop */
184 instrs[4] = 0x60000000; /* nop */
185 instrs[5] = 0x60000000; /* nop */
186
187 i = 0;
188 if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
189 if (cpu_has_feature(CPU_FTR_HVMODE)) {
190 instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
191 instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
192 } else {
193 instrs[i++] = 0x7db243a6; /* mtsprg 2,r13 */
194 instrs[i++] = 0x7db142a6; /* mfsprg r13,1 */
195 }
196 instrs[i++] = 0x7c0004ac; /* hwsync */
197 instrs[i++] = 0xe9ad0000; /* ld r13,0(r13) */
198 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
199 if (cpu_has_feature(CPU_FTR_HVMODE)) {
200 instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
201 } else {
202 instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
203 }
204 } else if (types & STF_BARRIER_EIEIO) {
205 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
206 }
207
208 for (i = 0; start < end; start++, i++) {
209 dest = (void *)start + *start;
210
211 pr_devel("patching dest %lx\n", (unsigned long)dest);
212
213 patch_instruction(dest, instrs[0]);
214 patch_instruction(dest + 1, instrs[1]);
215 patch_instruction(dest + 2, instrs[2]);
216 patch_instruction(dest + 3, instrs[3]);
217 patch_instruction(dest + 4, instrs[4]);
218 patch_instruction(dest + 5, instrs[5]);
219 }
220 printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
221 (types == STF_BARRIER_NONE) ? "no" :
222 (types == STF_BARRIER_FALLBACK) ? "fallback" :
223 (types == STF_BARRIER_EIEIO) ? "eieio" :
224 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
225 : "unknown");
226}
227
228
229void do_stf_barrier_fixups(enum stf_barrier_type types)
230{
231 do_stf_entry_barrier_fixups(types);
232 do_stf_exit_barrier_fixups(types);
233}
234
235void do_rfi_flush_fixups(enum l1d_flush_type types)
236{
237 unsigned int instrs[3], *dest;
238 long *start, *end;
239 int i;
240
241 start = PTRRELOC(&__start___rfi_flush_fixup),
242 end = PTRRELOC(&__stop___rfi_flush_fixup);
243
244 instrs[0] = 0x60000000; /* nop */
245 instrs[1] = 0x60000000; /* nop */
246 instrs[2] = 0x60000000; /* nop */
247
248 if (types & L1D_FLUSH_FALLBACK)
249 /* b .+16 to fallback flush */
250 instrs[0] = 0x48000010;
251
252 i = 0;
253 if (types & L1D_FLUSH_ORI) {
254 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
255 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
256 }
257
258 if (types & L1D_FLUSH_MTTRIG)
259 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
260
261 for (i = 0; start < end; start++, i++) {
262 dest = (void *)start + *start;
263
264 pr_devel("patching dest %lx\n", (unsigned long)dest);
265
266 patch_instruction(dest, instrs[0]);
267 patch_instruction(dest + 1, instrs[1]);
268 patch_instruction(dest + 2, instrs[2]);
269 }
270
271 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
272 (types == L1D_FLUSH_NONE) ? "no" :
273 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
274 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
275 ? "ori+mttrig type"
276 : "ori type" :
277 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
278 : "unknown");
279}
280#endif /* CONFIG_PPC_BOOK3S_64 */
281
282void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
283{
284 long *start, *end;
285 unsigned int *dest;
286
287 if (!(value & CPU_FTR_LWSYNC))
288 return ;
289
290 start = fixup_start;
291 end = fixup_end;
292
293 for (; start < end; start++) {
294 dest = (void *)start + *start;
295 raw_patch_instruction(dest, PPC_INST_LWSYNC);
296 }
297}
298
299static void do_final_fixups(void)
300{
301#if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
302 int *src, *dest;
303 unsigned long length;
304
305 if (PHYSICAL_START == 0)
306 return;
307
308 src = (int *)(KERNELBASE + PHYSICAL_START);
309 dest = (int *)KERNELBASE;
310 length = (__end_interrupts - _stext) / sizeof(int);
311
312 while (length--) {
313 raw_patch_instruction(dest, *src);
314 src++;
315 dest++;
316 }
317#endif
318}
319
320static unsigned long __initdata saved_cpu_features;
321static unsigned int __initdata saved_mmu_features;
322#ifdef CONFIG_PPC64
323static unsigned long __initdata saved_firmware_features;
324#endif
325
326void __init apply_feature_fixups(void)
327{
328 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
329
330 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
331 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
332
333 /*
334 * Apply the CPU-specific and firmware specific fixups to kernel text
335 * (nop out sections not relevant to this CPU or this firmware).
336 */
337 do_feature_fixups(spec->cpu_features,
338 PTRRELOC(&__start___ftr_fixup),
339 PTRRELOC(&__stop___ftr_fixup));
340
341 do_feature_fixups(spec->mmu_features,
342 PTRRELOC(&__start___mmu_ftr_fixup),
343 PTRRELOC(&__stop___mmu_ftr_fixup));
344
345 do_lwsync_fixups(spec->cpu_features,
346 PTRRELOC(&__start___lwsync_fixup),
347 PTRRELOC(&__stop___lwsync_fixup));
348
349#ifdef CONFIG_PPC64
350 saved_firmware_features = powerpc_firmware_features;
351 do_feature_fixups(powerpc_firmware_features,
352 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
353#endif
354 do_final_fixups();
355}
356
357void __init setup_feature_keys(void)
358{
359 /*
360 * Initialise jump label. This causes all the cpu/mmu_has_feature()
361 * checks to take on their correct polarity based on the current set of
362 * CPU/MMU features.
363 */
364 jump_label_init();
365 cpu_feature_keys_init();
366 mmu_feature_keys_init();
367}
368
369static int __init check_features(void)
370{
371 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
372 "CPU features changed after feature patching!\n");
373 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
374 "MMU features changed after feature patching!\n");
375#ifdef CONFIG_PPC64
376 WARN(saved_firmware_features != powerpc_firmware_features,
377 "Firmware features changed after feature patching!\n");
378#endif
379
380 return 0;
381}
382late_initcall(check_features);
383
384#ifdef CONFIG_FTR_FIXUP_SELFTEST
385
386#define check(x) \
387 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
388
389/* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
390static struct fixup_entry fixup;
391
392static long calc_offset(struct fixup_entry *entry, unsigned int *p)
393{
394 return (unsigned long)p - (unsigned long)entry;
395}
396
397static void test_basic_patching(void)
398{
399 extern unsigned int ftr_fixup_test1[];
400 extern unsigned int end_ftr_fixup_test1[];
401 extern unsigned int ftr_fixup_test1_orig[];
402 extern unsigned int ftr_fixup_test1_expected[];
403 int size = end_ftr_fixup_test1 - ftr_fixup_test1;
404
405 fixup.value = fixup.mask = 8;
406 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
407 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
408 fixup.alt_start_off = fixup.alt_end_off = 0;
409
410 /* Sanity check */
411 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
412
413 /* Check we don't patch if the value matches */
414 patch_feature_section(8, &fixup);
415 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
416
417 /* Check we do patch if the value doesn't match */
418 patch_feature_section(0, &fixup);
419 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
420
421 /* Check we do patch if the mask doesn't match */
422 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
423 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
424 patch_feature_section(~8, &fixup);
425 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
426}
427
428static void test_alternative_patching(void)
429{
430 extern unsigned int ftr_fixup_test2[];
431 extern unsigned int end_ftr_fixup_test2[];
432 extern unsigned int ftr_fixup_test2_orig[];
433 extern unsigned int ftr_fixup_test2_alt[];
434 extern unsigned int ftr_fixup_test2_expected[];
435 int size = end_ftr_fixup_test2 - ftr_fixup_test2;
436
437 fixup.value = fixup.mask = 0xF;
438 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
439 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
440 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
441 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
442
443 /* Sanity check */
444 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
445
446 /* Check we don't patch if the value matches */
447 patch_feature_section(0xF, &fixup);
448 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
449
450 /* Check we do patch if the value doesn't match */
451 patch_feature_section(0, &fixup);
452 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
453
454 /* Check we do patch if the mask doesn't match */
455 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
456 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
457 patch_feature_section(~0xF, &fixup);
458 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
459}
460
461static void test_alternative_case_too_big(void)
462{
463 extern unsigned int ftr_fixup_test3[];
464 extern unsigned int end_ftr_fixup_test3[];
465 extern unsigned int ftr_fixup_test3_orig[];
466 extern unsigned int ftr_fixup_test3_alt[];
467 int size = end_ftr_fixup_test3 - ftr_fixup_test3;
468
469 fixup.value = fixup.mask = 0xC;
470 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
471 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
472 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
473 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
474
475 /* Sanity check */
476 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
477
478 /* Expect nothing to be patched, and the error returned to us */
479 check(patch_feature_section(0xF, &fixup) == 1);
480 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
481 check(patch_feature_section(0, &fixup) == 1);
482 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
483 check(patch_feature_section(~0xF, &fixup) == 1);
484 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
485}
486
487static void test_alternative_case_too_small(void)
488{
489 extern unsigned int ftr_fixup_test4[];
490 extern unsigned int end_ftr_fixup_test4[];
491 extern unsigned int ftr_fixup_test4_orig[];
492 extern unsigned int ftr_fixup_test4_alt[];
493 extern unsigned int ftr_fixup_test4_expected[];
494 int size = end_ftr_fixup_test4 - ftr_fixup_test4;
495 unsigned long flag;
496
497 /* Check a high-bit flag */
498 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
499 fixup.value = fixup.mask = flag;
500 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
501 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
502 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
503 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
504
505 /* Sanity check */
506 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
507
508 /* Check we don't patch if the value matches */
509 patch_feature_section(flag, &fixup);
510 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
511
512 /* Check we do patch if the value doesn't match */
513 patch_feature_section(0, &fixup);
514 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
515
516 /* Check we do patch if the mask doesn't match */
517 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
518 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
519 patch_feature_section(~flag, &fixup);
520 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
521}
522
523static void test_alternative_case_with_branch(void)
524{
525 extern unsigned int ftr_fixup_test5[];
526 extern unsigned int end_ftr_fixup_test5[];
527 extern unsigned int ftr_fixup_test5_expected[];
528 int size = end_ftr_fixup_test5 - ftr_fixup_test5;
529
530 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
531}
532
533static void test_alternative_case_with_external_branch(void)
534{
535 extern unsigned int ftr_fixup_test6[];
536 extern unsigned int end_ftr_fixup_test6[];
537 extern unsigned int ftr_fixup_test6_expected[];
538 int size = end_ftr_fixup_test6 - ftr_fixup_test6;
539
540 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
541}
542
543static void test_cpu_macros(void)
544{
545 extern u8 ftr_fixup_test_FTR_macros[];
546 extern u8 ftr_fixup_test_FTR_macros_expected[];
547 unsigned long size = ftr_fixup_test_FTR_macros_expected -
548 ftr_fixup_test_FTR_macros;
549
550 /* The fixups have already been done for us during boot */
551 check(memcmp(ftr_fixup_test_FTR_macros,
552 ftr_fixup_test_FTR_macros_expected, size) == 0);
553}
554
555static void test_fw_macros(void)
556{
557#ifdef CONFIG_PPC64
558 extern u8 ftr_fixup_test_FW_FTR_macros[];
559 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
560 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
561 ftr_fixup_test_FW_FTR_macros;
562
563 /* The fixups have already been done for us during boot */
564 check(memcmp(ftr_fixup_test_FW_FTR_macros,
565 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
566#endif
567}
568
569static void test_lwsync_macros(void)
570{
571 extern u8 lwsync_fixup_test[];
572 extern u8 end_lwsync_fixup_test[];
573 extern u8 lwsync_fixup_test_expected_LWSYNC[];
574 extern u8 lwsync_fixup_test_expected_SYNC[];
575 unsigned long size = end_lwsync_fixup_test -
576 lwsync_fixup_test;
577
578 /* The fixups have already been done for us during boot */
579 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
580 check(memcmp(lwsync_fixup_test,
581 lwsync_fixup_test_expected_LWSYNC, size) == 0);
582 } else {
583 check(memcmp(lwsync_fixup_test,
584 lwsync_fixup_test_expected_SYNC, size) == 0);
585 }
586}
587
588static int __init test_feature_fixups(void)
589{
590 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
591
592 test_basic_patching();
593 test_alternative_patching();
594 test_alternative_case_too_big();
595 test_alternative_case_too_small();
596 test_alternative_case_with_branch();
597 test_alternative_case_with_external_branch();
598 test_cpu_macros();
599 test_fw_macros();
600 test_lwsync_macros();
601
602 return 0;
603}
604late_initcall(test_feature_fixups);
605
606#endif /* CONFIG_FTR_FIXUP_SELFTEST */
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
4 *
5 * Modifications for ppc64:
6 * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
7 *
8 * Copyright 2008 Michael Ellerman, IBM Corporation.
9 */
10
11#include <linux/types.h>
12#include <linux/jump_label.h>
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/init.h>
16#include <linux/sched/mm.h>
17#include <asm/cputable.h>
18#include <asm/code-patching.h>
19#include <asm/page.h>
20#include <asm/sections.h>
21#include <asm/setup.h>
22#include <asm/security_features.h>
23#include <asm/firmware.h>
24
25struct fixup_entry {
26 unsigned long mask;
27 unsigned long value;
28 long start_off;
29 long end_off;
30 long alt_start_off;
31 long alt_end_off;
32};
33
34static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
35{
36 /*
37 * We store the offset to the code as a negative offset from
38 * the start of the alt_entry, to support the VDSO. This
39 * routine converts that back into an actual address.
40 */
41 return (unsigned int *)((unsigned long)fcur + offset);
42}
43
44static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
45 unsigned int *alt_start, unsigned int *alt_end)
46{
47 unsigned int instr;
48
49 instr = *src;
50
51 if (instr_is_relative_branch(*src)) {
52 unsigned int *target = (unsigned int *)branch_target(src);
53
54 /* Branch within the section doesn't need translating */
55 if (target < alt_start || target > alt_end) {
56 instr = translate_branch(dest, src);
57 if (!instr)
58 return 1;
59 }
60 }
61
62 raw_patch_instruction(dest, instr);
63
64 return 0;
65}
66
67static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
68{
69 unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
70
71 start = calc_addr(fcur, fcur->start_off);
72 end = calc_addr(fcur, fcur->end_off);
73 alt_start = calc_addr(fcur, fcur->alt_start_off);
74 alt_end = calc_addr(fcur, fcur->alt_end_off);
75
76 if ((alt_end - alt_start) > (end - start))
77 return 1;
78
79 if ((value & fcur->mask) == fcur->value)
80 return 0;
81
82 src = alt_start;
83 dest = start;
84
85 for (; src < alt_end; src++, dest++) {
86 if (patch_alt_instruction(src, dest, alt_start, alt_end))
87 return 1;
88 }
89
90 for (; dest < end; dest++)
91 raw_patch_instruction(dest, PPC_INST_NOP);
92
93 return 0;
94}
95
96void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
97{
98 struct fixup_entry *fcur, *fend;
99
100 fcur = fixup_start;
101 fend = fixup_end;
102
103 for (; fcur < fend; fcur++) {
104 if (patch_feature_section(value, fcur)) {
105 WARN_ON(1);
106 printk("Unable to patch feature section at %p - %p" \
107 " with %p - %p\n",
108 calc_addr(fcur, fcur->start_off),
109 calc_addr(fcur, fcur->end_off),
110 calc_addr(fcur, fcur->alt_start_off),
111 calc_addr(fcur, fcur->alt_end_off));
112 }
113 }
114}
115
116#ifdef CONFIG_PPC_BOOK3S_64
117static void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
118{
119 unsigned int instrs[3], *dest;
120 long *start, *end;
121 int i;
122
123 start = PTRRELOC(&__start___stf_entry_barrier_fixup),
124 end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
125
126 instrs[0] = 0x60000000; /* nop */
127 instrs[1] = 0x60000000; /* nop */
128 instrs[2] = 0x60000000; /* nop */
129
130 i = 0;
131 if (types & STF_BARRIER_FALLBACK) {
132 instrs[i++] = 0x7d4802a6; /* mflr r10 */
133 instrs[i++] = 0x60000000; /* branch patched below */
134 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
135 } else if (types & STF_BARRIER_EIEIO) {
136 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
137 } else if (types & STF_BARRIER_SYNC_ORI) {
138 instrs[i++] = 0x7c0004ac; /* hwsync */
139 instrs[i++] = 0xe94d0000; /* ld r10,0(r13) */
140 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
141 }
142
143 for (i = 0; start < end; start++, i++) {
144 dest = (void *)start + *start;
145
146 pr_devel("patching dest %lx\n", (unsigned long)dest);
147
148 patch_instruction(dest, instrs[0]);
149
150 if (types & STF_BARRIER_FALLBACK)
151 patch_branch(dest + 1, (unsigned long)&stf_barrier_fallback,
152 BRANCH_SET_LINK);
153 else
154 patch_instruction(dest + 1, instrs[1]);
155
156 patch_instruction(dest + 2, instrs[2]);
157 }
158
159 printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
160 (types == STF_BARRIER_NONE) ? "no" :
161 (types == STF_BARRIER_FALLBACK) ? "fallback" :
162 (types == STF_BARRIER_EIEIO) ? "eieio" :
163 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
164 : "unknown");
165}
166
167static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
168{
169 unsigned int instrs[6], *dest;
170 long *start, *end;
171 int i;
172
173 start = PTRRELOC(&__start___stf_exit_barrier_fixup),
174 end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
175
176 instrs[0] = 0x60000000; /* nop */
177 instrs[1] = 0x60000000; /* nop */
178 instrs[2] = 0x60000000; /* nop */
179 instrs[3] = 0x60000000; /* nop */
180 instrs[4] = 0x60000000; /* nop */
181 instrs[5] = 0x60000000; /* nop */
182
183 i = 0;
184 if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
185 if (cpu_has_feature(CPU_FTR_HVMODE)) {
186 instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
187 instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
188 } else {
189 instrs[i++] = 0x7db243a6; /* mtsprg 2,r13 */
190 instrs[i++] = 0x7db142a6; /* mfsprg r13,1 */
191 }
192 instrs[i++] = 0x7c0004ac; /* hwsync */
193 instrs[i++] = 0xe9ad0000; /* ld r13,0(r13) */
194 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
195 if (cpu_has_feature(CPU_FTR_HVMODE)) {
196 instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
197 } else {
198 instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
199 }
200 } else if (types & STF_BARRIER_EIEIO) {
201 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
202 }
203
204 for (i = 0; start < end; start++, i++) {
205 dest = (void *)start + *start;
206
207 pr_devel("patching dest %lx\n", (unsigned long)dest);
208
209 patch_instruction(dest, instrs[0]);
210 patch_instruction(dest + 1, instrs[1]);
211 patch_instruction(dest + 2, instrs[2]);
212 patch_instruction(dest + 3, instrs[3]);
213 patch_instruction(dest + 4, instrs[4]);
214 patch_instruction(dest + 5, instrs[5]);
215 }
216 printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
217 (types == STF_BARRIER_NONE) ? "no" :
218 (types == STF_BARRIER_FALLBACK) ? "fallback" :
219 (types == STF_BARRIER_EIEIO) ? "eieio" :
220 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
221 : "unknown");
222}
223
224
225void do_stf_barrier_fixups(enum stf_barrier_type types)
226{
227 do_stf_entry_barrier_fixups(types);
228 do_stf_exit_barrier_fixups(types);
229}
230
231void do_rfi_flush_fixups(enum l1d_flush_type types)
232{
233 unsigned int instrs[3], *dest;
234 long *start, *end;
235 int i;
236
237 start = PTRRELOC(&__start___rfi_flush_fixup),
238 end = PTRRELOC(&__stop___rfi_flush_fixup);
239
240 instrs[0] = 0x60000000; /* nop */
241 instrs[1] = 0x60000000; /* nop */
242 instrs[2] = 0x60000000; /* nop */
243
244 if (types & L1D_FLUSH_FALLBACK)
245 /* b .+16 to fallback flush */
246 instrs[0] = 0x48000010;
247
248 i = 0;
249 if (types & L1D_FLUSH_ORI) {
250 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
251 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
252 }
253
254 if (types & L1D_FLUSH_MTTRIG)
255 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
256
257 for (i = 0; start < end; start++, i++) {
258 dest = (void *)start + *start;
259
260 pr_devel("patching dest %lx\n", (unsigned long)dest);
261
262 patch_instruction(dest, instrs[0]);
263 patch_instruction(dest + 1, instrs[1]);
264 patch_instruction(dest + 2, instrs[2]);
265 }
266
267 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
268 (types == L1D_FLUSH_NONE) ? "no" :
269 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
270 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
271 ? "ori+mttrig type"
272 : "ori type" :
273 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
274 : "unknown");
275}
276
277void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
278{
279 unsigned int instr, *dest;
280 long *start, *end;
281 int i;
282
283 start = fixup_start;
284 end = fixup_end;
285
286 instr = 0x60000000; /* nop */
287
288 if (enable) {
289 pr_info("barrier-nospec: using ORI speculation barrier\n");
290 instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
291 }
292
293 for (i = 0; start < end; start++, i++) {
294 dest = (void *)start + *start;
295
296 pr_devel("patching dest %lx\n", (unsigned long)dest);
297 patch_instruction(dest, instr);
298 }
299
300 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
301}
302
303#endif /* CONFIG_PPC_BOOK3S_64 */
304
305#ifdef CONFIG_PPC_BARRIER_NOSPEC
306void do_barrier_nospec_fixups(bool enable)
307{
308 void *start, *end;
309
310 start = PTRRELOC(&__start___barrier_nospec_fixup),
311 end = PTRRELOC(&__stop___barrier_nospec_fixup);
312
313 do_barrier_nospec_fixups_range(enable, start, end);
314}
315#endif /* CONFIG_PPC_BARRIER_NOSPEC */
316
317#ifdef CONFIG_PPC_FSL_BOOK3E
318void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
319{
320 unsigned int instr[2], *dest;
321 long *start, *end;
322 int i;
323
324 start = fixup_start;
325 end = fixup_end;
326
327 instr[0] = PPC_INST_NOP;
328 instr[1] = PPC_INST_NOP;
329
330 if (enable) {
331 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
332 instr[0] = PPC_INST_ISYNC;
333 instr[1] = PPC_INST_SYNC;
334 }
335
336 for (i = 0; start < end; start++, i++) {
337 dest = (void *)start + *start;
338
339 pr_devel("patching dest %lx\n", (unsigned long)dest);
340 patch_instruction(dest, instr[0]);
341 patch_instruction(dest + 1, instr[1]);
342 }
343
344 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
345}
346
347static void patch_btb_flush_section(long *curr)
348{
349 unsigned int *start, *end;
350
351 start = (void *)curr + *curr;
352 end = (void *)curr + *(curr + 1);
353 for (; start < end; start++) {
354 pr_devel("patching dest %lx\n", (unsigned long)start);
355 patch_instruction(start, PPC_INST_NOP);
356 }
357}
358
359void do_btb_flush_fixups(void)
360{
361 long *start, *end;
362
363 start = PTRRELOC(&__start__btb_flush_fixup);
364 end = PTRRELOC(&__stop__btb_flush_fixup);
365
366 for (; start < end; start += 2)
367 patch_btb_flush_section(start);
368}
369#endif /* CONFIG_PPC_FSL_BOOK3E */
370
371void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
372{
373 long *start, *end;
374 unsigned int *dest;
375
376 if (!(value & CPU_FTR_LWSYNC))
377 return ;
378
379 start = fixup_start;
380 end = fixup_end;
381
382 for (; start < end; start++) {
383 dest = (void *)start + *start;
384 raw_patch_instruction(dest, PPC_INST_LWSYNC);
385 }
386}
387
388static void do_final_fixups(void)
389{
390#if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
391 int *src, *dest;
392 unsigned long length;
393
394 if (PHYSICAL_START == 0)
395 return;
396
397 src = (int *)(KERNELBASE + PHYSICAL_START);
398 dest = (int *)KERNELBASE;
399 length = (__end_interrupts - _stext) / sizeof(int);
400
401 while (length--) {
402 raw_patch_instruction(dest, *src);
403 src++;
404 dest++;
405 }
406#endif
407}
408
409static unsigned long __initdata saved_cpu_features;
410static unsigned int __initdata saved_mmu_features;
411#ifdef CONFIG_PPC64
412static unsigned long __initdata saved_firmware_features;
413#endif
414
415void __init apply_feature_fixups(void)
416{
417 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
418
419 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
420 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
421
422 /*
423 * Apply the CPU-specific and firmware specific fixups to kernel text
424 * (nop out sections not relevant to this CPU or this firmware).
425 */
426 do_feature_fixups(spec->cpu_features,
427 PTRRELOC(&__start___ftr_fixup),
428 PTRRELOC(&__stop___ftr_fixup));
429
430 do_feature_fixups(spec->mmu_features,
431 PTRRELOC(&__start___mmu_ftr_fixup),
432 PTRRELOC(&__stop___mmu_ftr_fixup));
433
434 do_lwsync_fixups(spec->cpu_features,
435 PTRRELOC(&__start___lwsync_fixup),
436 PTRRELOC(&__stop___lwsync_fixup));
437
438#ifdef CONFIG_PPC64
439 saved_firmware_features = powerpc_firmware_features;
440 do_feature_fixups(powerpc_firmware_features,
441 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
442#endif
443 do_final_fixups();
444}
445
446void __init setup_feature_keys(void)
447{
448 /*
449 * Initialise jump label. This causes all the cpu/mmu_has_feature()
450 * checks to take on their correct polarity based on the current set of
451 * CPU/MMU features.
452 */
453 jump_label_init();
454 cpu_feature_keys_init();
455 mmu_feature_keys_init();
456}
457
458static int __init check_features(void)
459{
460 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
461 "CPU features changed after feature patching!\n");
462 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
463 "MMU features changed after feature patching!\n");
464#ifdef CONFIG_PPC64
465 WARN(saved_firmware_features != powerpc_firmware_features,
466 "Firmware features changed after feature patching!\n");
467#endif
468
469 return 0;
470}
471late_initcall(check_features);
472
473#ifdef CONFIG_FTR_FIXUP_SELFTEST
474
475#define check(x) \
476 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
477
478/* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
479static struct fixup_entry fixup;
480
481static long calc_offset(struct fixup_entry *entry, unsigned int *p)
482{
483 return (unsigned long)p - (unsigned long)entry;
484}
485
486static void test_basic_patching(void)
487{
488 extern unsigned int ftr_fixup_test1[];
489 extern unsigned int end_ftr_fixup_test1[];
490 extern unsigned int ftr_fixup_test1_orig[];
491 extern unsigned int ftr_fixup_test1_expected[];
492 int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
493
494 fixup.value = fixup.mask = 8;
495 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
496 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
497 fixup.alt_start_off = fixup.alt_end_off = 0;
498
499 /* Sanity check */
500 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
501
502 /* Check we don't patch if the value matches */
503 patch_feature_section(8, &fixup);
504 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
505
506 /* Check we do patch if the value doesn't match */
507 patch_feature_section(0, &fixup);
508 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
509
510 /* Check we do patch if the mask doesn't match */
511 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
512 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
513 patch_feature_section(~8, &fixup);
514 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
515}
516
517static void test_alternative_patching(void)
518{
519 extern unsigned int ftr_fixup_test2[];
520 extern unsigned int end_ftr_fixup_test2[];
521 extern unsigned int ftr_fixup_test2_orig[];
522 extern unsigned int ftr_fixup_test2_alt[];
523 extern unsigned int ftr_fixup_test2_expected[];
524 int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
525
526 fixup.value = fixup.mask = 0xF;
527 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
528 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
529 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
530 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
531
532 /* Sanity check */
533 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
534
535 /* Check we don't patch if the value matches */
536 patch_feature_section(0xF, &fixup);
537 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
538
539 /* Check we do patch if the value doesn't match */
540 patch_feature_section(0, &fixup);
541 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
542
543 /* Check we do patch if the mask doesn't match */
544 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
545 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
546 patch_feature_section(~0xF, &fixup);
547 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
548}
549
550static void test_alternative_case_too_big(void)
551{
552 extern unsigned int ftr_fixup_test3[];
553 extern unsigned int end_ftr_fixup_test3[];
554 extern unsigned int ftr_fixup_test3_orig[];
555 extern unsigned int ftr_fixup_test3_alt[];
556 int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
557
558 fixup.value = fixup.mask = 0xC;
559 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
560 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
561 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
562 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
563
564 /* Sanity check */
565 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
566
567 /* Expect nothing to be patched, and the error returned to us */
568 check(patch_feature_section(0xF, &fixup) == 1);
569 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
570 check(patch_feature_section(0, &fixup) == 1);
571 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
572 check(patch_feature_section(~0xF, &fixup) == 1);
573 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
574}
575
576static void test_alternative_case_too_small(void)
577{
578 extern unsigned int ftr_fixup_test4[];
579 extern unsigned int end_ftr_fixup_test4[];
580 extern unsigned int ftr_fixup_test4_orig[];
581 extern unsigned int ftr_fixup_test4_alt[];
582 extern unsigned int ftr_fixup_test4_expected[];
583 int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
584 unsigned long flag;
585
586 /* Check a high-bit flag */
587 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
588 fixup.value = fixup.mask = flag;
589 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
590 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
591 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
592 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
593
594 /* Sanity check */
595 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
596
597 /* Check we don't patch if the value matches */
598 patch_feature_section(flag, &fixup);
599 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
600
601 /* Check we do patch if the value doesn't match */
602 patch_feature_section(0, &fixup);
603 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
604
605 /* Check we do patch if the mask doesn't match */
606 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
607 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
608 patch_feature_section(~flag, &fixup);
609 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
610}
611
612static void test_alternative_case_with_branch(void)
613{
614 extern unsigned int ftr_fixup_test5[];
615 extern unsigned int end_ftr_fixup_test5[];
616 extern unsigned int ftr_fixup_test5_expected[];
617 int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
618
619 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
620}
621
622static void test_alternative_case_with_external_branch(void)
623{
624 extern unsigned int ftr_fixup_test6[];
625 extern unsigned int end_ftr_fixup_test6[];
626 extern unsigned int ftr_fixup_test6_expected[];
627 int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
628
629 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
630}
631
632static void test_alternative_case_with_branch_to_end(void)
633{
634 extern unsigned int ftr_fixup_test7[];
635 extern unsigned int end_ftr_fixup_test7[];
636 extern unsigned int ftr_fixup_test7_expected[];
637 int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
638
639 check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
640}
641
642static void test_cpu_macros(void)
643{
644 extern u8 ftr_fixup_test_FTR_macros[];
645 extern u8 ftr_fixup_test_FTR_macros_expected[];
646 unsigned long size = ftr_fixup_test_FTR_macros_expected -
647 ftr_fixup_test_FTR_macros;
648
649 /* The fixups have already been done for us during boot */
650 check(memcmp(ftr_fixup_test_FTR_macros,
651 ftr_fixup_test_FTR_macros_expected, size) == 0);
652}
653
654static void test_fw_macros(void)
655{
656#ifdef CONFIG_PPC64
657 extern u8 ftr_fixup_test_FW_FTR_macros[];
658 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
659 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
660 ftr_fixup_test_FW_FTR_macros;
661
662 /* The fixups have already been done for us during boot */
663 check(memcmp(ftr_fixup_test_FW_FTR_macros,
664 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
665#endif
666}
667
668static void test_lwsync_macros(void)
669{
670 extern u8 lwsync_fixup_test[];
671 extern u8 end_lwsync_fixup_test[];
672 extern u8 lwsync_fixup_test_expected_LWSYNC[];
673 extern u8 lwsync_fixup_test_expected_SYNC[];
674 unsigned long size = end_lwsync_fixup_test -
675 lwsync_fixup_test;
676
677 /* The fixups have already been done for us during boot */
678 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
679 check(memcmp(lwsync_fixup_test,
680 lwsync_fixup_test_expected_LWSYNC, size) == 0);
681 } else {
682 check(memcmp(lwsync_fixup_test,
683 lwsync_fixup_test_expected_SYNC, size) == 0);
684 }
685}
686
687static int __init test_feature_fixups(void)
688{
689 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
690
691 test_basic_patching();
692 test_alternative_patching();
693 test_alternative_case_too_big();
694 test_alternative_case_too_small();
695 test_alternative_case_with_branch();
696 test_alternative_case_with_external_branch();
697 test_alternative_case_with_branch_to_end();
698 test_cpu_macros();
699 test_fw_macros();
700 test_lwsync_macros();
701
702 return 0;
703}
704late_initcall(test_feature_fixups);
705
706#endif /* CONFIG_FTR_FIXUP_SELFTEST */