2 * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
4 * Modifications for ppc64:
5 * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
7 * Copyright 2008 Michael Ellerman, IBM Corporation.
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.
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 <linux/stop_machine.h>
22 #include <asm/cputable.h>
23 #include <asm/code-patching.h>
25 #include <asm/sections.h>
26 #include <asm/setup.h>
27 #include <asm/security_features.h>
28 #include <asm/firmware.h>
39 static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
42 * We store the offset to the code as a negative offset from
43 * the start of the alt_entry, to support the VDSO. This
44 * routine converts that back into an actual address.
46 return (unsigned int *)((unsigned long)fcur + offset);
49 static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
50 unsigned int *alt_start, unsigned int *alt_end)
56 if (instr_is_relative_branch(*src)) {
57 unsigned int *target = (unsigned int *)branch_target(src);
59 /* Branch within the section doesn't need translating */
60 if (target < alt_start || target > alt_end) {
61 instr = translate_branch(dest, src);
67 raw_patch_instruction(dest, instr);
72 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
74 unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
76 start = calc_addr(fcur, fcur->start_off);
77 end = calc_addr(fcur, fcur->end_off);
78 alt_start = calc_addr(fcur, fcur->alt_start_off);
79 alt_end = calc_addr(fcur, fcur->alt_end_off);
81 if ((alt_end - alt_start) > (end - start))
84 if ((value & fcur->mask) == fcur->value)
90 for (; src < alt_end; src++, dest++) {
91 if (patch_alt_instruction(src, dest, alt_start, alt_end))
95 for (; dest < end; dest++)
96 raw_patch_instruction(dest, PPC_INST_NOP);
101 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
103 struct fixup_entry *fcur, *fend;
108 for (; fcur < fend; fcur++) {
109 if (patch_feature_section(value, fcur)) {
111 printk("Unable to patch feature section at %p - %p" \
113 calc_addr(fcur, fcur->start_off),
114 calc_addr(fcur, fcur->end_off),
115 calc_addr(fcur, fcur->alt_start_off),
116 calc_addr(fcur, fcur->alt_end_off));
121 #ifdef CONFIG_PPC_BOOK3S_64
122 void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
124 unsigned int instrs[3], *dest;
128 start = PTRRELOC(&__start___stf_entry_barrier_fixup),
129 end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
131 instrs[0] = 0x60000000; /* nop */
132 instrs[1] = 0x60000000; /* nop */
133 instrs[2] = 0x60000000; /* nop */
136 if (types & STF_BARRIER_FALLBACK) {
137 instrs[i++] = 0x7d4802a6; /* mflr r10 */
138 instrs[i++] = 0x60000000; /* branch patched below */
139 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
140 } else if (types & STF_BARRIER_EIEIO) {
141 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
142 } else if (types & STF_BARRIER_SYNC_ORI) {
143 instrs[i++] = 0x7c0004ac; /* hwsync */
144 instrs[i++] = 0xe94d0000; /* ld r10,0(r13) */
145 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
148 for (i = 0; start < end; start++, i++) {
149 dest = (void *)start + *start;
151 pr_devel("patching dest %lx\n", (unsigned long)dest);
153 patch_instruction(dest, instrs[0]);
155 if (types & STF_BARRIER_FALLBACK)
156 patch_branch(dest + 1, (unsigned long)&stf_barrier_fallback,
159 patch_instruction(dest + 1, instrs[1]);
161 patch_instruction(dest + 2, instrs[2]);
164 printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
165 (types == STF_BARRIER_NONE) ? "no" :
166 (types == STF_BARRIER_FALLBACK) ? "fallback" :
167 (types == STF_BARRIER_EIEIO) ? "eieio" :
168 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
172 void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
174 unsigned int instrs[6], *dest;
178 start = PTRRELOC(&__start___stf_exit_barrier_fixup),
179 end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
181 instrs[0] = 0x60000000; /* nop */
182 instrs[1] = 0x60000000; /* nop */
183 instrs[2] = 0x60000000; /* nop */
184 instrs[3] = 0x60000000; /* nop */
185 instrs[4] = 0x60000000; /* nop */
186 instrs[5] = 0x60000000; /* nop */
189 if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
190 if (cpu_has_feature(CPU_FTR_HVMODE)) {
191 instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
192 instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
194 instrs[i++] = 0x7db243a6; /* mtsprg 2,r13 */
195 instrs[i++] = 0x7db142a6; /* mfsprg r13,1 */
197 instrs[i++] = 0x7c0004ac; /* hwsync */
198 instrs[i++] = 0xe9ad0000; /* ld r13,0(r13) */
199 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
200 if (cpu_has_feature(CPU_FTR_HVMODE)) {
201 instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
203 instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
205 } else if (types & STF_BARRIER_EIEIO) {
206 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
209 for (i = 0; start < end; start++, i++) {
210 dest = (void *)start + *start;
212 pr_devel("patching dest %lx\n", (unsigned long)dest);
214 patch_instruction(dest, instrs[0]);
215 patch_instruction(dest + 1, instrs[1]);
216 patch_instruction(dest + 2, instrs[2]);
217 patch_instruction(dest + 3, instrs[3]);
218 patch_instruction(dest + 4, instrs[4]);
219 patch_instruction(dest + 5, instrs[5]);
221 printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
222 (types == STF_BARRIER_NONE) ? "no" :
223 (types == STF_BARRIER_FALLBACK) ? "fallback" :
224 (types == STF_BARRIER_EIEIO) ? "eieio" :
225 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
229 static int __do_stf_barrier_fixups(void *data)
231 enum stf_barrier_type *types = data;
233 do_stf_entry_barrier_fixups(*types);
234 do_stf_exit_barrier_fixups(*types);
239 void do_stf_barrier_fixups(enum stf_barrier_type types)
242 * The call to the fallback entry flush, and the fallback/sync-ori exit
243 * flush can not be safely patched in/out while other CPUs are executing
244 * them. So call __do_stf_barrier_fixups() on one CPU while all other CPUs
245 * spin in the stop machine core with interrupts hard disabled.
247 stop_machine(__do_stf_barrier_fixups, &types, NULL);
250 void do_uaccess_flush_fixups(enum l1d_flush_type types)
252 unsigned int instrs[4], *dest;
256 start = PTRRELOC(&__start___uaccess_flush_fixup);
257 end = PTRRELOC(&__stop___uaccess_flush_fixup);
259 instrs[0] = 0x60000000; /* nop */
260 instrs[1] = 0x60000000; /* nop */
261 instrs[2] = 0x60000000; /* nop */
262 instrs[3] = 0x4e800020; /* blr */
265 if (types == L1D_FLUSH_FALLBACK) {
266 instrs[3] = 0x60000000; /* nop */
267 /* fallthrough to fallback flush */
270 if (types & L1D_FLUSH_ORI) {
271 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
272 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
275 if (types & L1D_FLUSH_MTTRIG)
276 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
278 for (i = 0; start < end; start++, i++) {
279 dest = (void *)start + *start;
281 pr_devel("patching dest %lx\n", (unsigned long)dest);
283 patch_instruction(dest, instrs[0]);
285 patch_instruction((dest + 1), instrs[1]);
286 patch_instruction((dest + 2), instrs[2]);
287 patch_instruction((dest + 3), instrs[3]);
290 printk(KERN_DEBUG "uaccess-flush: patched %d locations (%s flush)\n", i,
291 (types == L1D_FLUSH_NONE) ? "no" :
292 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
293 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
296 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
300 static int __do_entry_flush_fixups(void *data)
302 enum l1d_flush_type types = *(enum l1d_flush_type *)data;
303 unsigned int instrs[3], *dest;
307 start = PTRRELOC(&__start___entry_flush_fixup);
308 end = PTRRELOC(&__stop___entry_flush_fixup);
310 instrs[0] = 0x60000000; /* nop */
311 instrs[1] = 0x60000000; /* nop */
312 instrs[2] = 0x60000000; /* nop */
315 if (types == L1D_FLUSH_FALLBACK) {
316 instrs[i++] = 0x7d4802a6; /* mflr r10 */
317 instrs[i++] = 0x60000000; /* branch patched below */
318 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
321 if (types & L1D_FLUSH_ORI) {
322 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
323 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
326 if (types & L1D_FLUSH_MTTRIG)
327 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
329 for (i = 0; start < end; start++, i++) {
330 dest = (void *)start + *start;
332 pr_devel("patching dest %lx\n", (unsigned long)dest);
334 patch_instruction(dest, instrs[0]);
336 if (types == L1D_FLUSH_FALLBACK)
337 patch_branch((dest + 1), (unsigned long)&entry_flush_fallback,
340 patch_instruction((dest + 1), instrs[1]);
342 patch_instruction((dest + 2), instrs[2]);
345 printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
346 (types == L1D_FLUSH_NONE) ? "no" :
347 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
348 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
351 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
357 void do_entry_flush_fixups(enum l1d_flush_type types)
360 * The call to the fallback flush can not be safely patched in/out while
361 * other CPUs are executing it. So call __do_entry_flush_fixups() on one
362 * CPU while all other CPUs spin in the stop machine core with interrupts
365 stop_machine(__do_entry_flush_fixups, &types, NULL);
368 void do_rfi_flush_fixups(enum l1d_flush_type types)
370 unsigned int instrs[3], *dest;
374 start = PTRRELOC(&__start___rfi_flush_fixup),
375 end = PTRRELOC(&__stop___rfi_flush_fixup);
377 instrs[0] = 0x60000000; /* nop */
378 instrs[1] = 0x60000000; /* nop */
379 instrs[2] = 0x60000000; /* nop */
381 if (types & L1D_FLUSH_FALLBACK)
382 /* b .+16 to fallback flush */
383 instrs[0] = 0x48000010;
386 if (types & L1D_FLUSH_ORI) {
387 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
388 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
391 if (types & L1D_FLUSH_MTTRIG)
392 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
394 for (i = 0; start < end; start++, i++) {
395 dest = (void *)start + *start;
397 pr_devel("patching dest %lx\n", (unsigned long)dest);
399 patch_instruction(dest, instrs[0]);
400 patch_instruction(dest + 1, instrs[1]);
401 patch_instruction(dest + 2, instrs[2]);
404 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
405 (types == L1D_FLUSH_NONE) ? "no" :
406 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
407 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
410 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
414 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
416 unsigned int instr, *dest;
423 instr = 0x60000000; /* nop */
426 pr_info("barrier-nospec: using ORI speculation barrier\n");
427 instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
430 for (i = 0; start < end; start++, i++) {
431 dest = (void *)start + *start;
433 pr_devel("patching dest %lx\n", (unsigned long)dest);
434 patch_instruction(dest, instr);
437 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
440 #endif /* CONFIG_PPC_BOOK3S_64 */
442 #ifdef CONFIG_PPC_BARRIER_NOSPEC
443 void do_barrier_nospec_fixups(bool enable)
447 start = PTRRELOC(&__start___barrier_nospec_fixup),
448 end = PTRRELOC(&__stop___barrier_nospec_fixup);
450 do_barrier_nospec_fixups_range(enable, start, end);
452 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
454 #ifdef CONFIG_PPC_FSL_BOOK3E
455 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
457 unsigned int instr[2], *dest;
464 instr[0] = PPC_INST_NOP;
465 instr[1] = PPC_INST_NOP;
468 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
469 instr[0] = PPC_INST_ISYNC;
470 instr[1] = PPC_INST_SYNC;
473 for (i = 0; start < end; start++, i++) {
474 dest = (void *)start + *start;
476 pr_devel("patching dest %lx\n", (unsigned long)dest);
477 patch_instruction(dest, instr[0]);
478 patch_instruction(dest + 1, instr[1]);
481 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
484 static void patch_btb_flush_section(long *curr)
486 unsigned int *start, *end;
488 start = (void *)curr + *curr;
489 end = (void *)curr + *(curr + 1);
490 for (; start < end; start++) {
491 pr_devel("patching dest %lx\n", (unsigned long)start);
492 patch_instruction(start, PPC_INST_NOP);
496 void do_btb_flush_fixups(void)
500 start = PTRRELOC(&__start__btb_flush_fixup);
501 end = PTRRELOC(&__stop__btb_flush_fixup);
503 for (; start < end; start += 2)
504 patch_btb_flush_section(start);
506 #endif /* CONFIG_PPC_FSL_BOOK3E */
508 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
513 if (!(value & CPU_FTR_LWSYNC))
519 for (; start < end; start++) {
520 dest = (void *)start + *start;
521 raw_patch_instruction(dest, PPC_INST_LWSYNC);
525 static void do_final_fixups(void)
527 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
529 unsigned long length;
531 if (PHYSICAL_START == 0)
534 src = (int *)(KERNELBASE + PHYSICAL_START);
535 dest = (int *)KERNELBASE;
536 length = (__end_interrupts - _stext) / sizeof(int);
539 raw_patch_instruction(dest, *src);
546 static unsigned long __initdata saved_cpu_features;
547 static unsigned int __initdata saved_mmu_features;
549 static unsigned long __initdata saved_firmware_features;
552 void __init apply_feature_fixups(void)
554 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
556 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
557 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
560 * Apply the CPU-specific and firmware specific fixups to kernel text
561 * (nop out sections not relevant to this CPU or this firmware).
563 do_feature_fixups(spec->cpu_features,
564 PTRRELOC(&__start___ftr_fixup),
565 PTRRELOC(&__stop___ftr_fixup));
567 do_feature_fixups(spec->mmu_features,
568 PTRRELOC(&__start___mmu_ftr_fixup),
569 PTRRELOC(&__stop___mmu_ftr_fixup));
571 do_lwsync_fixups(spec->cpu_features,
572 PTRRELOC(&__start___lwsync_fixup),
573 PTRRELOC(&__stop___lwsync_fixup));
576 saved_firmware_features = powerpc_firmware_features;
577 do_feature_fixups(powerpc_firmware_features,
578 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
583 void __init setup_feature_keys(void)
586 * Initialise jump label. This causes all the cpu/mmu_has_feature()
587 * checks to take on their correct polarity based on the current set of
591 cpu_feature_keys_init();
592 mmu_feature_keys_init();
595 static int __init check_features(void)
597 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
598 "CPU features changed after feature patching!\n");
599 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
600 "MMU features changed after feature patching!\n");
602 WARN(saved_firmware_features != powerpc_firmware_features,
603 "Firmware features changed after feature patching!\n");
608 late_initcall(check_features);
610 #ifdef CONFIG_FTR_FIXUP_SELFTEST
613 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
615 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
616 static struct fixup_entry fixup;
618 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
620 return (unsigned long)p - (unsigned long)entry;
623 static void test_basic_patching(void)
625 extern unsigned int ftr_fixup_test1[];
626 extern unsigned int end_ftr_fixup_test1[];
627 extern unsigned int ftr_fixup_test1_orig[];
628 extern unsigned int ftr_fixup_test1_expected[];
629 int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
631 fixup.value = fixup.mask = 8;
632 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
633 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
634 fixup.alt_start_off = fixup.alt_end_off = 0;
637 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
639 /* Check we don't patch if the value matches */
640 patch_feature_section(8, &fixup);
641 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
643 /* Check we do patch if the value doesn't match */
644 patch_feature_section(0, &fixup);
645 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
647 /* Check we do patch if the mask doesn't match */
648 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
649 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
650 patch_feature_section(~8, &fixup);
651 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
654 static void test_alternative_patching(void)
656 extern unsigned int ftr_fixup_test2[];
657 extern unsigned int end_ftr_fixup_test2[];
658 extern unsigned int ftr_fixup_test2_orig[];
659 extern unsigned int ftr_fixup_test2_alt[];
660 extern unsigned int ftr_fixup_test2_expected[];
661 int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
663 fixup.value = fixup.mask = 0xF;
664 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
665 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
666 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
667 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
670 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
672 /* Check we don't patch if the value matches */
673 patch_feature_section(0xF, &fixup);
674 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
676 /* Check we do patch if the value doesn't match */
677 patch_feature_section(0, &fixup);
678 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
680 /* Check we do patch if the mask doesn't match */
681 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
682 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
683 patch_feature_section(~0xF, &fixup);
684 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
687 static void test_alternative_case_too_big(void)
689 extern unsigned int ftr_fixup_test3[];
690 extern unsigned int end_ftr_fixup_test3[];
691 extern unsigned int ftr_fixup_test3_orig[];
692 extern unsigned int ftr_fixup_test3_alt[];
693 int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
695 fixup.value = fixup.mask = 0xC;
696 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
697 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
698 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
699 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
702 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
704 /* Expect nothing to be patched, and the error returned to us */
705 check(patch_feature_section(0xF, &fixup) == 1);
706 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
707 check(patch_feature_section(0, &fixup) == 1);
708 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
709 check(patch_feature_section(~0xF, &fixup) == 1);
710 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
713 static void test_alternative_case_too_small(void)
715 extern unsigned int ftr_fixup_test4[];
716 extern unsigned int end_ftr_fixup_test4[];
717 extern unsigned int ftr_fixup_test4_orig[];
718 extern unsigned int ftr_fixup_test4_alt[];
719 extern unsigned int ftr_fixup_test4_expected[];
720 int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
723 /* Check a high-bit flag */
724 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
725 fixup.value = fixup.mask = flag;
726 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
727 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
728 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
729 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
732 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
734 /* Check we don't patch if the value matches */
735 patch_feature_section(flag, &fixup);
736 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
738 /* Check we do patch if the value doesn't match */
739 patch_feature_section(0, &fixup);
740 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
742 /* Check we do patch if the mask doesn't match */
743 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
744 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
745 patch_feature_section(~flag, &fixup);
746 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
749 static void test_alternative_case_with_branch(void)
751 extern unsigned int ftr_fixup_test5[];
752 extern unsigned int end_ftr_fixup_test5[];
753 extern unsigned int ftr_fixup_test5_expected[];
754 int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
756 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
759 static void test_alternative_case_with_external_branch(void)
761 extern unsigned int ftr_fixup_test6[];
762 extern unsigned int end_ftr_fixup_test6[];
763 extern unsigned int ftr_fixup_test6_expected[];
764 int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
766 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
769 static void test_alternative_case_with_branch_to_end(void)
771 extern unsigned int ftr_fixup_test7[];
772 extern unsigned int end_ftr_fixup_test7[];
773 extern unsigned int ftr_fixup_test7_expected[];
774 int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
776 check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
779 static void test_cpu_macros(void)
781 extern u8 ftr_fixup_test_FTR_macros[];
782 extern u8 ftr_fixup_test_FTR_macros_expected[];
783 unsigned long size = ftr_fixup_test_FTR_macros_expected -
784 ftr_fixup_test_FTR_macros;
786 /* The fixups have already been done for us during boot */
787 check(memcmp(ftr_fixup_test_FTR_macros,
788 ftr_fixup_test_FTR_macros_expected, size) == 0);
791 static void test_fw_macros(void)
794 extern u8 ftr_fixup_test_FW_FTR_macros[];
795 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
796 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
797 ftr_fixup_test_FW_FTR_macros;
799 /* The fixups have already been done for us during boot */
800 check(memcmp(ftr_fixup_test_FW_FTR_macros,
801 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
805 static void test_lwsync_macros(void)
807 extern u8 lwsync_fixup_test[];
808 extern u8 end_lwsync_fixup_test[];
809 extern u8 lwsync_fixup_test_expected_LWSYNC[];
810 extern u8 lwsync_fixup_test_expected_SYNC[];
811 unsigned long size = end_lwsync_fixup_test -
814 /* The fixups have already been done for us during boot */
815 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
816 check(memcmp(lwsync_fixup_test,
817 lwsync_fixup_test_expected_LWSYNC, size) == 0);
819 check(memcmp(lwsync_fixup_test,
820 lwsync_fixup_test_expected_SYNC, size) == 0);
824 static int __init test_feature_fixups(void)
826 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
828 test_basic_patching();
829 test_alternative_patching();
830 test_alternative_case_too_big();
831 test_alternative_case_too_small();
832 test_alternative_case_with_branch();
833 test_alternative_case_with_external_branch();
834 test_alternative_case_with_branch_to_end();
837 test_lwsync_macros();
841 late_initcall(test_feature_fixups);
843 #endif /* CONFIG_FTR_FIXUP_SELFTEST */