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/stop_machine.h>
21 #include <asm/cputable.h>
22 #include <asm/code-patching.h>
24 #include <asm/sections.h>
25 #include <asm/setup.h>
26 #include <asm/security_features.h>
27 #include <asm/firmware.h>
28 #include <asm/setup.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 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 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"
230 void do_stf_barrier_fixups(enum stf_barrier_type types)
232 do_stf_entry_barrier_fixups(types);
233 do_stf_exit_barrier_fixups(types);
236 void do_uaccess_flush_fixups(enum l1d_flush_type types)
238 unsigned int instrs[4], *dest;
242 start = PTRRELOC(&__start___uaccess_flush_fixup);
243 end = PTRRELOC(&__stop___uaccess_flush_fixup);
245 instrs[0] = 0x60000000; /* nop */
246 instrs[1] = 0x60000000; /* nop */
247 instrs[2] = 0x60000000; /* nop */
248 instrs[3] = 0x4e800020; /* blr */
251 if (types == L1D_FLUSH_FALLBACK) {
252 instrs[3] = 0x60000000; /* nop */
253 /* fallthrough to fallback flush */
256 if (types & L1D_FLUSH_ORI) {
257 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
258 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
261 if (types & L1D_FLUSH_MTTRIG)
262 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
264 for (i = 0; start < end; start++, i++) {
265 dest = (void *)start + *start;
267 pr_devel("patching dest %lx\n", (unsigned long)dest);
269 patch_instruction(dest, instrs[0]);
271 patch_instruction((dest + 1), instrs[1]);
272 patch_instruction((dest + 2), instrs[2]);
273 patch_instruction((dest + 3), instrs[3]);
276 printk(KERN_DEBUG "uaccess-flush: patched %d locations (%s flush)\n", i,
277 (types == L1D_FLUSH_NONE) ? "no" :
278 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
279 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
282 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
286 static int __do_entry_flush_fixups(void *data)
288 enum l1d_flush_type types = *(enum l1d_flush_type *)data;
289 unsigned int instrs[3], *dest;
293 start = PTRRELOC(&__start___entry_flush_fixup);
294 end = PTRRELOC(&__stop___entry_flush_fixup);
296 instrs[0] = 0x60000000; /* nop */
297 instrs[1] = 0x60000000; /* nop */
298 instrs[2] = 0x60000000; /* nop */
301 if (types == L1D_FLUSH_FALLBACK) {
302 instrs[i++] = 0x7d4802a6; /* mflr r10 */
303 instrs[i++] = 0x60000000; /* branch patched below */
304 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
307 if (types & L1D_FLUSH_ORI) {
308 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
309 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
312 if (types & L1D_FLUSH_MTTRIG)
313 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
315 for (i = 0; start < end; start++, i++) {
316 dest = (void *)start + *start;
318 pr_devel("patching dest %lx\n", (unsigned long)dest);
320 patch_instruction(dest, instrs[0]);
322 if (types == L1D_FLUSH_FALLBACK)
323 patch_branch((dest + 1), (unsigned long)&entry_flush_fallback,
326 patch_instruction((dest + 1), instrs[1]);
328 patch_instruction((dest + 2), instrs[2]);
331 printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
332 (types == L1D_FLUSH_NONE) ? "no" :
333 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
334 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
337 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
343 void do_entry_flush_fixups(enum l1d_flush_type types)
346 * The call to the fallback flush can not be safely patched in/out while
347 * other CPUs are executing it. So call __do_entry_flush_fixups() on one
348 * CPU while all other CPUs spin in the stop machine core with interrupts
351 stop_machine(__do_entry_flush_fixups, &types, NULL);
354 void do_rfi_flush_fixups(enum l1d_flush_type types)
356 unsigned int instrs[3], *dest;
360 start = PTRRELOC(&__start___rfi_flush_fixup),
361 end = PTRRELOC(&__stop___rfi_flush_fixup);
363 instrs[0] = 0x60000000; /* nop */
364 instrs[1] = 0x60000000; /* nop */
365 instrs[2] = 0x60000000; /* nop */
367 if (types & L1D_FLUSH_FALLBACK)
368 /* b .+16 to fallback flush */
369 instrs[0] = 0x48000010;
372 if (types & L1D_FLUSH_ORI) {
373 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
374 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
377 if (types & L1D_FLUSH_MTTRIG)
378 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
380 for (i = 0; start < end; start++, i++) {
381 dest = (void *)start + *start;
383 pr_devel("patching dest %lx\n", (unsigned long)dest);
385 patch_instruction(dest, instrs[0]);
386 patch_instruction(dest + 1, instrs[1]);
387 patch_instruction(dest + 2, instrs[2]);
390 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
391 (types == L1D_FLUSH_NONE) ? "no" :
392 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
393 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
396 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
400 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
402 unsigned int instr, *dest;
409 instr = 0x60000000; /* nop */
412 pr_info("barrier-nospec: using ORI speculation barrier\n");
413 instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
416 for (i = 0; start < end; start++, i++) {
417 dest = (void *)start + *start;
419 pr_devel("patching dest %lx\n", (unsigned long)dest);
420 patch_instruction(dest, instr);
423 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
426 #endif /* CONFIG_PPC_BOOK3S_64 */
428 #ifdef CONFIG_PPC_BARRIER_NOSPEC
429 void do_barrier_nospec_fixups(bool enable)
433 start = PTRRELOC(&__start___barrier_nospec_fixup),
434 end = PTRRELOC(&__stop___barrier_nospec_fixup);
436 do_barrier_nospec_fixups_range(enable, start, end);
438 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
440 #ifdef CONFIG_PPC_FSL_BOOK3E
441 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
443 unsigned int instr[2], *dest;
450 instr[0] = PPC_INST_NOP;
451 instr[1] = PPC_INST_NOP;
454 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
455 instr[0] = PPC_INST_ISYNC;
456 instr[1] = PPC_INST_SYNC;
459 for (i = 0; start < end; start++, i++) {
460 dest = (void *)start + *start;
462 pr_devel("patching dest %lx\n", (unsigned long)dest);
463 patch_instruction(dest, instr[0]);
464 patch_instruction(dest + 1, instr[1]);
467 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
470 static void patch_btb_flush_section(long *curr)
472 unsigned int *start, *end;
474 start = (void *)curr + *curr;
475 end = (void *)curr + *(curr + 1);
476 for (; start < end; start++) {
477 pr_devel("patching dest %lx\n", (unsigned long)start);
478 patch_instruction(start, PPC_INST_NOP);
482 void do_btb_flush_fixups(void)
486 start = PTRRELOC(&__start__btb_flush_fixup);
487 end = PTRRELOC(&__stop__btb_flush_fixup);
489 for (; start < end; start += 2)
490 patch_btb_flush_section(start);
492 #endif /* CONFIG_PPC_FSL_BOOK3E */
494 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
499 if (!(value & CPU_FTR_LWSYNC))
505 for (; start < end; start++) {
506 dest = (void *)start + *start;
507 patch_instruction(dest, PPC_INST_LWSYNC);
511 static void do_final_fixups(void)
513 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
515 unsigned long length;
517 if (PHYSICAL_START == 0)
520 src = (int *)(KERNELBASE + PHYSICAL_START);
521 dest = (int *)KERNELBASE;
522 length = (__end_interrupts - _stext) / sizeof(int);
525 patch_instruction(dest, *src);
532 static unsigned long __initdata saved_cpu_features;
533 static unsigned int __initdata saved_mmu_features;
535 static unsigned long __initdata saved_firmware_features;
538 void __init apply_feature_fixups(void)
540 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
542 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
543 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
546 * Apply the CPU-specific and firmware specific fixups to kernel text
547 * (nop out sections not relevant to this CPU or this firmware).
549 do_feature_fixups(spec->cpu_features,
550 PTRRELOC(&__start___ftr_fixup),
551 PTRRELOC(&__stop___ftr_fixup));
553 do_feature_fixups(spec->mmu_features,
554 PTRRELOC(&__start___mmu_ftr_fixup),
555 PTRRELOC(&__stop___mmu_ftr_fixup));
557 do_lwsync_fixups(spec->cpu_features,
558 PTRRELOC(&__start___lwsync_fixup),
559 PTRRELOC(&__stop___lwsync_fixup));
562 saved_firmware_features = powerpc_firmware_features;
563 do_feature_fixups(powerpc_firmware_features,
564 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
569 void __init setup_feature_keys(void)
572 * Initialise jump label. This causes all the cpu/mmu_has_feature()
573 * checks to take on their correct polarity based on the current set of
577 cpu_feature_keys_init();
578 mmu_feature_keys_init();
581 static int __init check_features(void)
583 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
584 "CPU features changed after feature patching!\n");
585 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
586 "MMU features changed after feature patching!\n");
588 WARN(saved_firmware_features != powerpc_firmware_features,
589 "Firmware features changed after feature patching!\n");
594 late_initcall(check_features);
596 #ifdef CONFIG_FTR_FIXUP_SELFTEST
599 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
601 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
602 static struct fixup_entry fixup;
604 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
606 return (unsigned long)p - (unsigned long)entry;
609 static void test_basic_patching(void)
611 extern unsigned int ftr_fixup_test1;
612 extern unsigned int end_ftr_fixup_test1;
613 extern unsigned int ftr_fixup_test1_orig;
614 extern unsigned int ftr_fixup_test1_expected;
615 int size = &end_ftr_fixup_test1 - &ftr_fixup_test1;
617 fixup.value = fixup.mask = 8;
618 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test1 + 1);
619 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test1 + 2);
620 fixup.alt_start_off = fixup.alt_end_off = 0;
623 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
625 /* Check we don't patch if the value matches */
626 patch_feature_section(8, &fixup);
627 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
629 /* Check we do patch if the value doesn't match */
630 patch_feature_section(0, &fixup);
631 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
633 /* Check we do patch if the mask doesn't match */
634 memcpy(&ftr_fixup_test1, &ftr_fixup_test1_orig, size);
635 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
636 patch_feature_section(~8, &fixup);
637 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
640 static void test_alternative_patching(void)
642 extern unsigned int ftr_fixup_test2;
643 extern unsigned int end_ftr_fixup_test2;
644 extern unsigned int ftr_fixup_test2_orig;
645 extern unsigned int ftr_fixup_test2_alt;
646 extern unsigned int ftr_fixup_test2_expected;
647 int size = &end_ftr_fixup_test2 - &ftr_fixup_test2;
649 fixup.value = fixup.mask = 0xF;
650 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test2 + 1);
651 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test2 + 2);
652 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test2_alt);
653 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test2_alt + 1);
656 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
658 /* Check we don't patch if the value matches */
659 patch_feature_section(0xF, &fixup);
660 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
662 /* Check we do patch if the value doesn't match */
663 patch_feature_section(0, &fixup);
664 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
666 /* Check we do patch if the mask doesn't match */
667 memcpy(&ftr_fixup_test2, &ftr_fixup_test2_orig, size);
668 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
669 patch_feature_section(~0xF, &fixup);
670 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
673 static void test_alternative_case_too_big(void)
675 extern unsigned int ftr_fixup_test3;
676 extern unsigned int end_ftr_fixup_test3;
677 extern unsigned int ftr_fixup_test3_orig;
678 extern unsigned int ftr_fixup_test3_alt;
679 int size = &end_ftr_fixup_test3 - &ftr_fixup_test3;
681 fixup.value = fixup.mask = 0xC;
682 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test3 + 1);
683 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test3 + 2);
684 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test3_alt);
685 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test3_alt + 2);
688 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
690 /* Expect nothing to be patched, and the error returned to us */
691 check(patch_feature_section(0xF, &fixup) == 1);
692 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
693 check(patch_feature_section(0, &fixup) == 1);
694 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
695 check(patch_feature_section(~0xF, &fixup) == 1);
696 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
699 static void test_alternative_case_too_small(void)
701 extern unsigned int ftr_fixup_test4;
702 extern unsigned int end_ftr_fixup_test4;
703 extern unsigned int ftr_fixup_test4_orig;
704 extern unsigned int ftr_fixup_test4_alt;
705 extern unsigned int ftr_fixup_test4_expected;
706 int size = &end_ftr_fixup_test4 - &ftr_fixup_test4;
709 /* Check a high-bit flag */
710 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
711 fixup.value = fixup.mask = flag;
712 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test4 + 1);
713 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test4 + 5);
714 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test4_alt);
715 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test4_alt + 2);
718 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
720 /* Check we don't patch if the value matches */
721 patch_feature_section(flag, &fixup);
722 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
724 /* Check we do patch if the value doesn't match */
725 patch_feature_section(0, &fixup);
726 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
728 /* Check we do patch if the mask doesn't match */
729 memcpy(&ftr_fixup_test4, &ftr_fixup_test4_orig, size);
730 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
731 patch_feature_section(~flag, &fixup);
732 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
735 static void test_alternative_case_with_branch(void)
737 extern unsigned int ftr_fixup_test5;
738 extern unsigned int end_ftr_fixup_test5;
739 extern unsigned int ftr_fixup_test5_expected;
740 int size = &end_ftr_fixup_test5 - &ftr_fixup_test5;
742 check(memcmp(&ftr_fixup_test5, &ftr_fixup_test5_expected, size) == 0);
745 static void test_alternative_case_with_external_branch(void)
747 extern unsigned int ftr_fixup_test6;
748 extern unsigned int end_ftr_fixup_test6;
749 extern unsigned int ftr_fixup_test6_expected;
750 int size = &end_ftr_fixup_test6 - &ftr_fixup_test6;
752 check(memcmp(&ftr_fixup_test6, &ftr_fixup_test6_expected, size) == 0);
755 static void test_cpu_macros(void)
757 extern u8 ftr_fixup_test_FTR_macros;
758 extern u8 ftr_fixup_test_FTR_macros_expected;
759 unsigned long size = &ftr_fixup_test_FTR_macros_expected -
760 &ftr_fixup_test_FTR_macros;
762 /* The fixups have already been done for us during boot */
763 check(memcmp(&ftr_fixup_test_FTR_macros,
764 &ftr_fixup_test_FTR_macros_expected, size) == 0);
767 static void test_fw_macros(void)
770 extern u8 ftr_fixup_test_FW_FTR_macros;
771 extern u8 ftr_fixup_test_FW_FTR_macros_expected;
772 unsigned long size = &ftr_fixup_test_FW_FTR_macros_expected -
773 &ftr_fixup_test_FW_FTR_macros;
775 /* The fixups have already been done for us during boot */
776 check(memcmp(&ftr_fixup_test_FW_FTR_macros,
777 &ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
781 static void test_lwsync_macros(void)
783 extern u8 lwsync_fixup_test;
784 extern u8 end_lwsync_fixup_test;
785 extern u8 lwsync_fixup_test_expected_LWSYNC;
786 extern u8 lwsync_fixup_test_expected_SYNC;
787 unsigned long size = &end_lwsync_fixup_test -
790 /* The fixups have already been done for us during boot */
791 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
792 check(memcmp(&lwsync_fixup_test,
793 &lwsync_fixup_test_expected_LWSYNC, size) == 0);
795 check(memcmp(&lwsync_fixup_test,
796 &lwsync_fixup_test_expected_SYNC, size) == 0);
800 static int __init test_feature_fixups(void)
802 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
804 test_basic_patching();
805 test_alternative_patching();
806 test_alternative_case_too_big();
807 test_alternative_case_too_small();
808 test_alternative_case_with_branch();
809 test_alternative_case_with_external_branch();
812 test_lwsync_macros();
816 late_initcall(test_feature_fixups);
818 #endif /* CONFIG_FTR_FIXUP_SELFTEST */