1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
5 * Modifications for ppc64:
6 * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
8 * Copyright 2008 Michael Ellerman, IBM Corporation.
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 <linux/stop_machine.h>
18 #include <asm/cputable.h>
19 #include <asm/code-patching.h>
21 #include <asm/sections.h>
22 #include <asm/setup.h>
23 #include <asm/security_features.h>
24 #include <asm/firmware.h>
35 static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
38 * We store the offset to the code as a negative offset from
39 * the start of the alt_entry, to support the VDSO. This
40 * routine converts that back into an actual address.
42 return (unsigned int *)((unsigned long)fcur + offset);
45 static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
46 unsigned int *alt_start, unsigned int *alt_end)
52 if (instr_is_relative_branch(*src)) {
53 unsigned int *target = (unsigned int *)branch_target(src);
55 /* Branch within the section doesn't need translating */
56 if (target < alt_start || target > alt_end) {
57 instr = translate_branch(dest, src);
63 raw_patch_instruction(dest, instr);
68 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
70 unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
72 start = calc_addr(fcur, fcur->start_off);
73 end = calc_addr(fcur, fcur->end_off);
74 alt_start = calc_addr(fcur, fcur->alt_start_off);
75 alt_end = calc_addr(fcur, fcur->alt_end_off);
77 if ((alt_end - alt_start) > (end - start))
80 if ((value & fcur->mask) == fcur->value)
86 for (; src < alt_end; src++, dest++) {
87 if (patch_alt_instruction(src, dest, alt_start, alt_end))
91 for (; dest < end; dest++)
92 raw_patch_instruction(dest, PPC_INST_NOP);
97 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
99 struct fixup_entry *fcur, *fend;
104 for (; fcur < fend; fcur++) {
105 if (patch_feature_section(value, fcur)) {
107 printk("Unable to patch feature section at %p - %p" \
109 calc_addr(fcur, fcur->start_off),
110 calc_addr(fcur, fcur->end_off),
111 calc_addr(fcur, fcur->alt_start_off),
112 calc_addr(fcur, fcur->alt_end_off));
117 #ifdef CONFIG_PPC_BOOK3S_64
118 static void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
120 unsigned int instrs[3], *dest;
124 start = PTRRELOC(&__start___stf_entry_barrier_fixup),
125 end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
127 instrs[0] = 0x60000000; /* nop */
128 instrs[1] = 0x60000000; /* nop */
129 instrs[2] = 0x60000000; /* nop */
132 if (types & STF_BARRIER_FALLBACK) {
133 instrs[i++] = 0x7d4802a6; /* mflr r10 */
134 instrs[i++] = 0x60000000; /* branch patched below */
135 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
136 } else if (types & STF_BARRIER_EIEIO) {
137 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
138 } else if (types & STF_BARRIER_SYNC_ORI) {
139 instrs[i++] = 0x7c0004ac; /* hwsync */
140 instrs[i++] = 0xe94d0000; /* ld r10,0(r13) */
141 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
144 for (i = 0; start < end; start++, i++) {
145 dest = (void *)start + *start;
147 pr_devel("patching dest %lx\n", (unsigned long)dest);
149 patch_instruction(dest, instrs[0]);
151 if (types & STF_BARRIER_FALLBACK)
152 patch_branch(dest + 1, (unsigned long)&stf_barrier_fallback,
155 patch_instruction(dest + 1, instrs[1]);
157 patch_instruction(dest + 2, instrs[2]);
160 printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
161 (types == STF_BARRIER_NONE) ? "no" :
162 (types == STF_BARRIER_FALLBACK) ? "fallback" :
163 (types == STF_BARRIER_EIEIO) ? "eieio" :
164 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
168 static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
170 unsigned int instrs[6], *dest;
174 start = PTRRELOC(&__start___stf_exit_barrier_fixup),
175 end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
177 instrs[0] = 0x60000000; /* nop */
178 instrs[1] = 0x60000000; /* nop */
179 instrs[2] = 0x60000000; /* nop */
180 instrs[3] = 0x60000000; /* nop */
181 instrs[4] = 0x60000000; /* nop */
182 instrs[5] = 0x60000000; /* nop */
185 if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
186 if (cpu_has_feature(CPU_FTR_HVMODE)) {
187 instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
188 instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
190 instrs[i++] = 0x7db243a6; /* mtsprg 2,r13 */
191 instrs[i++] = 0x7db142a6; /* mfsprg r13,1 */
193 instrs[i++] = 0x7c0004ac; /* hwsync */
194 instrs[i++] = 0xe9ad0000; /* ld r13,0(r13) */
195 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
196 if (cpu_has_feature(CPU_FTR_HVMODE)) {
197 instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
199 instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
201 } else if (types & STF_BARRIER_EIEIO) {
202 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
205 for (i = 0; start < end; start++, i++) {
206 dest = (void *)start + *start;
208 pr_devel("patching dest %lx\n", (unsigned long)dest);
210 patch_instruction(dest, instrs[0]);
211 patch_instruction(dest + 1, instrs[1]);
212 patch_instruction(dest + 2, instrs[2]);
213 patch_instruction(dest + 3, instrs[3]);
214 patch_instruction(dest + 4, instrs[4]);
215 patch_instruction(dest + 5, instrs[5]);
217 printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
218 (types == STF_BARRIER_NONE) ? "no" :
219 (types == STF_BARRIER_FALLBACK) ? "fallback" :
220 (types == STF_BARRIER_EIEIO) ? "eieio" :
221 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
225 static int __do_stf_barrier_fixups(void *data)
227 enum stf_barrier_type *types = data;
229 do_stf_entry_barrier_fixups(*types);
230 do_stf_exit_barrier_fixups(*types);
235 void do_stf_barrier_fixups(enum stf_barrier_type types)
238 * The call to the fallback entry flush, and the fallback/sync-ori exit
239 * flush can not be safely patched in/out while other CPUs are executing
240 * them. So call __do_stf_barrier_fixups() on one CPU while all other CPUs
241 * spin in the stop machine core with interrupts hard disabled.
243 stop_machine(__do_stf_barrier_fixups, &types, NULL);
246 void do_uaccess_flush_fixups(enum l1d_flush_type types)
248 unsigned int instrs[4], *dest;
252 start = PTRRELOC(&__start___uaccess_flush_fixup);
253 end = PTRRELOC(&__stop___uaccess_flush_fixup);
255 instrs[0] = 0x60000000; /* nop */
256 instrs[1] = 0x60000000; /* nop */
257 instrs[2] = 0x60000000; /* nop */
258 instrs[3] = 0x4e800020; /* blr */
261 if (types == L1D_FLUSH_FALLBACK) {
262 instrs[3] = 0x60000000; /* nop */
263 /* fallthrough to fallback flush */
266 if (types & L1D_FLUSH_ORI) {
267 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
268 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
271 if (types & L1D_FLUSH_MTTRIG)
272 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
274 for (i = 0; start < end; start++, i++) {
275 dest = (void *)start + *start;
277 pr_devel("patching dest %lx\n", (unsigned long)dest);
279 patch_instruction(dest, instrs[0]);
281 patch_instruction((dest + 1), instrs[1]);
282 patch_instruction((dest + 2), instrs[2]);
283 patch_instruction((dest + 3), instrs[3]);
286 printk(KERN_DEBUG "uaccess-flush: patched %d locations (%s flush)\n", i,
287 (types == L1D_FLUSH_NONE) ? "no" :
288 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
289 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
292 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
296 static int __do_entry_flush_fixups(void *data)
298 enum l1d_flush_type types = *(enum l1d_flush_type *)data;
299 unsigned int instrs[3], *dest;
303 start = PTRRELOC(&__start___entry_flush_fixup);
304 end = PTRRELOC(&__stop___entry_flush_fixup);
306 instrs[0] = 0x60000000; /* nop */
307 instrs[1] = 0x60000000; /* nop */
308 instrs[2] = 0x60000000; /* nop */
311 if (types == L1D_FLUSH_FALLBACK) {
312 instrs[i++] = 0x7d4802a6; /* mflr r10 */
313 instrs[i++] = 0x60000000; /* branch patched below */
314 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
317 if (types & L1D_FLUSH_ORI) {
318 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
319 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
322 if (types & L1D_FLUSH_MTTRIG)
323 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
325 for (i = 0; start < end; start++, i++) {
326 dest = (void *)start + *start;
328 pr_devel("patching dest %lx\n", (unsigned long)dest);
330 patch_instruction(dest, instrs[0]);
332 if (types == L1D_FLUSH_FALLBACK)
333 patch_branch((dest + 1), (unsigned long)&entry_flush_fallback,
336 patch_instruction((dest + 1), instrs[1]);
338 patch_instruction((dest + 2), instrs[2]);
341 printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
342 (types == L1D_FLUSH_NONE) ? "no" :
343 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
344 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
347 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
353 void do_entry_flush_fixups(enum l1d_flush_type types)
356 * The call to the fallback flush can not be safely patched in/out while
357 * other CPUs are executing it. So call __do_entry_flush_fixups() on one
358 * CPU while all other CPUs spin in the stop machine core with interrupts
361 stop_machine(__do_entry_flush_fixups, &types, NULL);
364 void do_rfi_flush_fixups(enum l1d_flush_type types)
366 unsigned int instrs[3], *dest;
370 start = PTRRELOC(&__start___rfi_flush_fixup),
371 end = PTRRELOC(&__stop___rfi_flush_fixup);
373 instrs[0] = 0x60000000; /* nop */
374 instrs[1] = 0x60000000; /* nop */
375 instrs[2] = 0x60000000; /* nop */
377 if (types & L1D_FLUSH_FALLBACK)
378 /* b .+16 to fallback flush */
379 instrs[0] = 0x48000010;
382 if (types & L1D_FLUSH_ORI) {
383 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
384 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
387 if (types & L1D_FLUSH_MTTRIG)
388 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
390 for (i = 0; start < end; start++, i++) {
391 dest = (void *)start + *start;
393 pr_devel("patching dest %lx\n", (unsigned long)dest);
395 patch_instruction(dest, instrs[0]);
396 patch_instruction(dest + 1, instrs[1]);
397 patch_instruction(dest + 2, instrs[2]);
400 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
401 (types == L1D_FLUSH_NONE) ? "no" :
402 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
403 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
406 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
410 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
412 unsigned int instr, *dest;
419 instr = 0x60000000; /* nop */
422 pr_info("barrier-nospec: using ORI speculation barrier\n");
423 instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
426 for (i = 0; start < end; start++, i++) {
427 dest = (void *)start + *start;
429 pr_devel("patching dest %lx\n", (unsigned long)dest);
430 patch_instruction(dest, instr);
433 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
436 #endif /* CONFIG_PPC_BOOK3S_64 */
438 #ifdef CONFIG_PPC_BARRIER_NOSPEC
439 void do_barrier_nospec_fixups(bool enable)
443 start = PTRRELOC(&__start___barrier_nospec_fixup),
444 end = PTRRELOC(&__stop___barrier_nospec_fixup);
446 do_barrier_nospec_fixups_range(enable, start, end);
448 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
450 #ifdef CONFIG_PPC_FSL_BOOK3E
451 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
453 unsigned int instr[2], *dest;
460 instr[0] = PPC_INST_NOP;
461 instr[1] = PPC_INST_NOP;
464 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
465 instr[0] = PPC_INST_ISYNC;
466 instr[1] = PPC_INST_SYNC;
469 for (i = 0; start < end; start++, i++) {
470 dest = (void *)start + *start;
472 pr_devel("patching dest %lx\n", (unsigned long)dest);
473 patch_instruction(dest, instr[0]);
474 patch_instruction(dest + 1, instr[1]);
477 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
480 static void patch_btb_flush_section(long *curr)
482 unsigned int *start, *end;
484 start = (void *)curr + *curr;
485 end = (void *)curr + *(curr + 1);
486 for (; start < end; start++) {
487 pr_devel("patching dest %lx\n", (unsigned long)start);
488 patch_instruction(start, PPC_INST_NOP);
492 void do_btb_flush_fixups(void)
496 start = PTRRELOC(&__start__btb_flush_fixup);
497 end = PTRRELOC(&__stop__btb_flush_fixup);
499 for (; start < end; start += 2)
500 patch_btb_flush_section(start);
502 #endif /* CONFIG_PPC_FSL_BOOK3E */
504 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
509 if (!(value & CPU_FTR_LWSYNC))
515 for (; start < end; start++) {
516 dest = (void *)start + *start;
517 raw_patch_instruction(dest, PPC_INST_LWSYNC);
521 static void do_final_fixups(void)
523 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
525 unsigned long length;
527 if (PHYSICAL_START == 0)
530 src = (int *)(KERNELBASE + PHYSICAL_START);
531 dest = (int *)KERNELBASE;
532 length = (__end_interrupts - _stext) / sizeof(int);
535 raw_patch_instruction(dest, *src);
542 static unsigned long __initdata saved_cpu_features;
543 static unsigned int __initdata saved_mmu_features;
545 static unsigned long __initdata saved_firmware_features;
548 void __init apply_feature_fixups(void)
550 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
552 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
553 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
556 * Apply the CPU-specific and firmware specific fixups to kernel text
557 * (nop out sections not relevant to this CPU or this firmware).
559 do_feature_fixups(spec->cpu_features,
560 PTRRELOC(&__start___ftr_fixup),
561 PTRRELOC(&__stop___ftr_fixup));
563 do_feature_fixups(spec->mmu_features,
564 PTRRELOC(&__start___mmu_ftr_fixup),
565 PTRRELOC(&__stop___mmu_ftr_fixup));
567 do_lwsync_fixups(spec->cpu_features,
568 PTRRELOC(&__start___lwsync_fixup),
569 PTRRELOC(&__stop___lwsync_fixup));
572 saved_firmware_features = powerpc_firmware_features;
573 do_feature_fixups(powerpc_firmware_features,
574 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
579 void __init setup_feature_keys(void)
582 * Initialise jump label. This causes all the cpu/mmu_has_feature()
583 * checks to take on their correct polarity based on the current set of
587 cpu_feature_keys_init();
588 mmu_feature_keys_init();
591 static int __init check_features(void)
593 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
594 "CPU features changed after feature patching!\n");
595 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
596 "MMU features changed after feature patching!\n");
598 WARN(saved_firmware_features != powerpc_firmware_features,
599 "Firmware features changed after feature patching!\n");
604 late_initcall(check_features);
606 #ifdef CONFIG_FTR_FIXUP_SELFTEST
609 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
611 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
612 static struct fixup_entry fixup;
614 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
616 return (unsigned long)p - (unsigned long)entry;
619 static void test_basic_patching(void)
621 extern unsigned int ftr_fixup_test1[];
622 extern unsigned int end_ftr_fixup_test1[];
623 extern unsigned int ftr_fixup_test1_orig[];
624 extern unsigned int ftr_fixup_test1_expected[];
625 int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
627 fixup.value = fixup.mask = 8;
628 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
629 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
630 fixup.alt_start_off = fixup.alt_end_off = 0;
633 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
635 /* Check we don't patch if the value matches */
636 patch_feature_section(8, &fixup);
637 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
639 /* Check we do patch if the value doesn't match */
640 patch_feature_section(0, &fixup);
641 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
643 /* Check we do patch if the mask doesn't match */
644 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
645 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
646 patch_feature_section(~8, &fixup);
647 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
650 static void test_alternative_patching(void)
652 extern unsigned int ftr_fixup_test2[];
653 extern unsigned int end_ftr_fixup_test2[];
654 extern unsigned int ftr_fixup_test2_orig[];
655 extern unsigned int ftr_fixup_test2_alt[];
656 extern unsigned int ftr_fixup_test2_expected[];
657 int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
659 fixup.value = fixup.mask = 0xF;
660 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
661 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
662 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
663 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
666 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
668 /* Check we don't patch if the value matches */
669 patch_feature_section(0xF, &fixup);
670 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
672 /* Check we do patch if the value doesn't match */
673 patch_feature_section(0, &fixup);
674 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
676 /* Check we do patch if the mask doesn't match */
677 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
678 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
679 patch_feature_section(~0xF, &fixup);
680 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
683 static void test_alternative_case_too_big(void)
685 extern unsigned int ftr_fixup_test3[];
686 extern unsigned int end_ftr_fixup_test3[];
687 extern unsigned int ftr_fixup_test3_orig[];
688 extern unsigned int ftr_fixup_test3_alt[];
689 int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
691 fixup.value = fixup.mask = 0xC;
692 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
693 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
694 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
695 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
698 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
700 /* Expect nothing to be patched, and the error returned to us */
701 check(patch_feature_section(0xF, &fixup) == 1);
702 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
703 check(patch_feature_section(0, &fixup) == 1);
704 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
705 check(patch_feature_section(~0xF, &fixup) == 1);
706 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
709 static void test_alternative_case_too_small(void)
711 extern unsigned int ftr_fixup_test4[];
712 extern unsigned int end_ftr_fixup_test4[];
713 extern unsigned int ftr_fixup_test4_orig[];
714 extern unsigned int ftr_fixup_test4_alt[];
715 extern unsigned int ftr_fixup_test4_expected[];
716 int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
719 /* Check a high-bit flag */
720 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
721 fixup.value = fixup.mask = flag;
722 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
723 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
724 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
725 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
728 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
730 /* Check we don't patch if the value matches */
731 patch_feature_section(flag, &fixup);
732 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
734 /* Check we do patch if the value doesn't match */
735 patch_feature_section(0, &fixup);
736 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
738 /* Check we do patch if the mask doesn't match */
739 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
740 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
741 patch_feature_section(~flag, &fixup);
742 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
745 static void test_alternative_case_with_branch(void)
747 extern unsigned int ftr_fixup_test5[];
748 extern unsigned int end_ftr_fixup_test5[];
749 extern unsigned int ftr_fixup_test5_expected[];
750 int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
752 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
755 static void test_alternative_case_with_external_branch(void)
757 extern unsigned int ftr_fixup_test6[];
758 extern unsigned int end_ftr_fixup_test6[];
759 extern unsigned int ftr_fixup_test6_expected[];
760 int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
762 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
765 static void test_alternative_case_with_branch_to_end(void)
767 extern unsigned int ftr_fixup_test7[];
768 extern unsigned int end_ftr_fixup_test7[];
769 extern unsigned int ftr_fixup_test7_expected[];
770 int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
772 check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
775 static void test_cpu_macros(void)
777 extern u8 ftr_fixup_test_FTR_macros[];
778 extern u8 ftr_fixup_test_FTR_macros_expected[];
779 unsigned long size = ftr_fixup_test_FTR_macros_expected -
780 ftr_fixup_test_FTR_macros;
782 /* The fixups have already been done for us during boot */
783 check(memcmp(ftr_fixup_test_FTR_macros,
784 ftr_fixup_test_FTR_macros_expected, size) == 0);
787 static void test_fw_macros(void)
790 extern u8 ftr_fixup_test_FW_FTR_macros[];
791 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
792 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
793 ftr_fixup_test_FW_FTR_macros;
795 /* The fixups have already been done for us during boot */
796 check(memcmp(ftr_fixup_test_FW_FTR_macros,
797 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
801 static void test_lwsync_macros(void)
803 extern u8 lwsync_fixup_test[];
804 extern u8 end_lwsync_fixup_test[];
805 extern u8 lwsync_fixup_test_expected_LWSYNC[];
806 extern u8 lwsync_fixup_test_expected_SYNC[];
807 unsigned long size = end_lwsync_fixup_test -
810 /* The fixups have already been done for us during boot */
811 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
812 check(memcmp(lwsync_fixup_test,
813 lwsync_fixup_test_expected_LWSYNC, size) == 0);
815 check(memcmp(lwsync_fixup_test,
816 lwsync_fixup_test_expected_SYNC, size) == 0);
820 static int __init test_feature_fixups(void)
822 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
824 test_basic_patching();
825 test_alternative_patching();
826 test_alternative_case_too_big();
827 test_alternative_case_too_small();
828 test_alternative_case_with_branch();
829 test_alternative_case_with_external_branch();
830 test_alternative_case_with_branch_to_end();
833 test_lwsync_macros();
837 late_initcall(test_feature_fixups);
839 #endif /* CONFIG_FTR_FIXUP_SELFTEST */