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>
36 static struct ppc_inst *calc_addr(struct fixup_entry *fcur, long offset)
39 * We store the offset to the code as a negative offset from
40 * the start of the alt_entry, to support the VDSO. This
41 * routine converts that back into an actual address.
43 return (struct ppc_inst *)((unsigned long)fcur + offset);
46 static int patch_alt_instruction(struct ppc_inst *src, struct ppc_inst *dest,
47 struct ppc_inst *alt_start, struct ppc_inst *alt_end)
50 struct ppc_inst instr;
52 instr = ppc_inst_read(src);
54 if (instr_is_relative_branch(*src)) {
55 struct ppc_inst *target = (struct ppc_inst *)branch_target(src);
57 /* Branch within the section doesn't need translating */
58 if (target < alt_start || target > alt_end) {
59 err = translate_branch(&instr, dest, src);
65 raw_patch_instruction(dest, instr);
70 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
72 struct ppc_inst *start, *end, *alt_start, *alt_end, *src, *dest, nop;
74 start = calc_addr(fcur, fcur->start_off);
75 end = calc_addr(fcur, fcur->end_off);
76 alt_start = calc_addr(fcur, fcur->alt_start_off);
77 alt_end = calc_addr(fcur, fcur->alt_end_off);
79 if ((alt_end - alt_start) > (end - start))
82 if ((value & fcur->mask) == fcur->value)
88 for (; src < alt_end; src = ppc_inst_next(src, src),
89 dest = ppc_inst_next(dest, dest)) {
90 if (patch_alt_instruction(src, dest, alt_start, alt_end))
94 nop = ppc_inst(PPC_INST_NOP);
95 for (; dest < end; dest = ppc_inst_next(dest, &nop))
96 raw_patch_instruction(dest, 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 static 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((struct ppc_inst *)dest, ppc_inst(instrs[0]));
155 if (types & STF_BARRIER_FALLBACK)
156 patch_branch((struct ppc_inst *)(dest + 1),
157 (unsigned long)&stf_barrier_fallback,
160 patch_instruction((struct ppc_inst *)(dest + 1),
161 ppc_inst(instrs[1]));
163 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
166 printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
167 (types == STF_BARRIER_NONE) ? "no" :
168 (types == STF_BARRIER_FALLBACK) ? "fallback" :
169 (types == STF_BARRIER_EIEIO) ? "eieio" :
170 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
174 static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
176 unsigned int instrs[6], *dest;
180 start = PTRRELOC(&__start___stf_exit_barrier_fixup),
181 end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
183 instrs[0] = 0x60000000; /* nop */
184 instrs[1] = 0x60000000; /* nop */
185 instrs[2] = 0x60000000; /* nop */
186 instrs[3] = 0x60000000; /* nop */
187 instrs[4] = 0x60000000; /* nop */
188 instrs[5] = 0x60000000; /* nop */
191 if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
192 if (cpu_has_feature(CPU_FTR_HVMODE)) {
193 instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
194 instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
196 instrs[i++] = 0x7db243a6; /* mtsprg 2,r13 */
197 instrs[i++] = 0x7db142a6; /* mfsprg r13,1 */
199 instrs[i++] = 0x7c0004ac; /* hwsync */
200 instrs[i++] = 0xe9ad0000; /* ld r13,0(r13) */
201 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
202 if (cpu_has_feature(CPU_FTR_HVMODE)) {
203 instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
205 instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
207 } else if (types & STF_BARRIER_EIEIO) {
208 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
211 for (i = 0; start < end; start++, i++) {
212 dest = (void *)start + *start;
214 pr_devel("patching dest %lx\n", (unsigned long)dest);
216 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
217 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
218 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
219 patch_instruction((struct ppc_inst *)(dest + 3), ppc_inst(instrs[3]));
220 patch_instruction((struct ppc_inst *)(dest + 4), ppc_inst(instrs[4]));
221 patch_instruction((struct ppc_inst *)(dest + 5), ppc_inst(instrs[5]));
223 printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
224 (types == STF_BARRIER_NONE) ? "no" :
225 (types == STF_BARRIER_FALLBACK) ? "fallback" :
226 (types == STF_BARRIER_EIEIO) ? "eieio" :
227 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
231 static int __do_stf_barrier_fixups(void *data)
233 enum stf_barrier_type *types = data;
235 do_stf_entry_barrier_fixups(*types);
236 do_stf_exit_barrier_fixups(*types);
241 void do_stf_barrier_fixups(enum stf_barrier_type types)
244 * The call to the fallback entry flush, and the fallback/sync-ori exit
245 * flush can not be safely patched in/out while other CPUs are executing
246 * them. So call __do_stf_barrier_fixups() on one CPU while all other CPUs
247 * spin in the stop machine core with interrupts hard disabled.
249 stop_machine(__do_stf_barrier_fixups, &types, NULL);
252 void do_uaccess_flush_fixups(enum l1d_flush_type types)
254 unsigned int instrs[4], *dest;
258 start = PTRRELOC(&__start___uaccess_flush_fixup);
259 end = PTRRELOC(&__stop___uaccess_flush_fixup);
261 instrs[0] = 0x60000000; /* nop */
262 instrs[1] = 0x60000000; /* nop */
263 instrs[2] = 0x60000000; /* nop */
264 instrs[3] = 0x4e800020; /* blr */
267 if (types == L1D_FLUSH_FALLBACK) {
268 instrs[3] = 0x60000000; /* nop */
269 /* fallthrough to fallback flush */
272 if (types & L1D_FLUSH_ORI) {
273 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
274 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
277 if (types & L1D_FLUSH_MTTRIG)
278 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
280 for (i = 0; start < end; start++, i++) {
281 dest = (void *)start + *start;
283 pr_devel("patching dest %lx\n", (unsigned long)dest);
285 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
287 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
288 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
289 patch_instruction((struct ppc_inst *)(dest + 3), ppc_inst(instrs[3]));
292 printk(KERN_DEBUG "uaccess-flush: patched %d locations (%s flush)\n", i,
293 (types == L1D_FLUSH_NONE) ? "no" :
294 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
295 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
298 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
302 static int __do_entry_flush_fixups(void *data)
304 enum l1d_flush_type types = *(enum l1d_flush_type *)data;
305 unsigned int instrs[3], *dest;
309 instrs[0] = 0x60000000; /* nop */
310 instrs[1] = 0x60000000; /* nop */
311 instrs[2] = 0x60000000; /* nop */
314 if (types == L1D_FLUSH_FALLBACK) {
315 instrs[i++] = 0x7d4802a6; /* mflr r10 */
316 instrs[i++] = 0x60000000; /* branch patched below */
317 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
320 if (types & L1D_FLUSH_ORI) {
321 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
322 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
325 if (types & L1D_FLUSH_MTTRIG)
326 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
328 start = PTRRELOC(&__start___entry_flush_fixup);
329 end = PTRRELOC(&__stop___entry_flush_fixup);
330 for (i = 0; start < end; start++, i++) {
331 dest = (void *)start + *start;
333 pr_devel("patching dest %lx\n", (unsigned long)dest);
335 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
337 if (types == L1D_FLUSH_FALLBACK)
338 patch_branch((struct ppc_inst *)(dest + 1), (unsigned long)&entry_flush_fallback,
341 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
343 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
346 start = PTRRELOC(&__start___scv_entry_flush_fixup);
347 end = PTRRELOC(&__stop___scv_entry_flush_fixup);
348 for (; start < end; start++, i++) {
349 dest = (void *)start + *start;
351 pr_devel("patching dest %lx\n", (unsigned long)dest);
353 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
355 if (types == L1D_FLUSH_FALLBACK)
356 patch_branch((struct ppc_inst *)(dest + 1), (unsigned long)&scv_entry_flush_fallback,
359 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
361 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
365 printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
366 (types == L1D_FLUSH_NONE) ? "no" :
367 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
368 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
371 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
377 void do_entry_flush_fixups(enum l1d_flush_type types)
380 * The call to the fallback flush can not be safely patched in/out while
381 * other CPUs are executing it. So call __do_entry_flush_fixups() on one
382 * CPU while all other CPUs spin in the stop machine core with interrupts
385 stop_machine(__do_entry_flush_fixups, &types, NULL);
388 void do_rfi_flush_fixups(enum l1d_flush_type types)
390 unsigned int instrs[3], *dest;
394 start = PTRRELOC(&__start___rfi_flush_fixup),
395 end = PTRRELOC(&__stop___rfi_flush_fixup);
397 instrs[0] = 0x60000000; /* nop */
398 instrs[1] = 0x60000000; /* nop */
399 instrs[2] = 0x60000000; /* nop */
401 if (types & L1D_FLUSH_FALLBACK)
402 /* b .+16 to fallback flush */
403 instrs[0] = 0x48000010;
406 if (types & L1D_FLUSH_ORI) {
407 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
408 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
411 if (types & L1D_FLUSH_MTTRIG)
412 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
414 for (i = 0; start < end; start++, i++) {
415 dest = (void *)start + *start;
417 pr_devel("patching dest %lx\n", (unsigned long)dest);
419 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
420 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
421 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
424 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
425 (types == L1D_FLUSH_NONE) ? "no" :
426 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
427 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
430 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
434 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
436 unsigned int instr, *dest;
443 instr = 0x60000000; /* nop */
446 pr_info("barrier-nospec: using ORI speculation barrier\n");
447 instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
450 for (i = 0; start < end; start++, i++) {
451 dest = (void *)start + *start;
453 pr_devel("patching dest %lx\n", (unsigned long)dest);
454 patch_instruction((struct ppc_inst *)dest, ppc_inst(instr));
457 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
460 #endif /* CONFIG_PPC_BOOK3S_64 */
462 #ifdef CONFIG_PPC_BARRIER_NOSPEC
463 void do_barrier_nospec_fixups(bool enable)
467 start = PTRRELOC(&__start___barrier_nospec_fixup),
468 end = PTRRELOC(&__stop___barrier_nospec_fixup);
470 do_barrier_nospec_fixups_range(enable, start, end);
472 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
474 #ifdef CONFIG_PPC_FSL_BOOK3E
475 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
477 unsigned int instr[2], *dest;
484 instr[0] = PPC_INST_NOP;
485 instr[1] = PPC_INST_NOP;
488 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
489 instr[0] = PPC_INST_ISYNC;
490 instr[1] = PPC_INST_SYNC;
493 for (i = 0; start < end; start++, i++) {
494 dest = (void *)start + *start;
496 pr_devel("patching dest %lx\n", (unsigned long)dest);
497 patch_instruction((struct ppc_inst *)dest, ppc_inst(instr[0]));
498 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instr[1]));
501 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
504 static void patch_btb_flush_section(long *curr)
506 unsigned int *start, *end;
508 start = (void *)curr + *curr;
509 end = (void *)curr + *(curr + 1);
510 for (; start < end; start++) {
511 pr_devel("patching dest %lx\n", (unsigned long)start);
512 patch_instruction((struct ppc_inst *)start, ppc_inst(PPC_INST_NOP));
516 void do_btb_flush_fixups(void)
520 start = PTRRELOC(&__start__btb_flush_fixup);
521 end = PTRRELOC(&__stop__btb_flush_fixup);
523 for (; start < end; start += 2)
524 patch_btb_flush_section(start);
526 #endif /* CONFIG_PPC_FSL_BOOK3E */
528 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
531 struct ppc_inst *dest;
533 if (!(value & CPU_FTR_LWSYNC))
539 for (; start < end; start++) {
540 dest = (void *)start + *start;
541 raw_patch_instruction(dest, ppc_inst(PPC_INST_LWSYNC));
545 static void do_final_fixups(void)
547 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
548 struct ppc_inst inst, *src, *dest, *end;
550 if (PHYSICAL_START == 0)
553 src = (struct ppc_inst *)(KERNELBASE + PHYSICAL_START);
554 dest = (struct ppc_inst *)KERNELBASE;
555 end = (void *)src + (__end_interrupts - _stext);
558 inst = ppc_inst_read(src);
559 raw_patch_instruction(dest, inst);
560 src = ppc_inst_next(src, src);
561 dest = ppc_inst_next(dest, dest);
566 static unsigned long __initdata saved_cpu_features;
567 static unsigned int __initdata saved_mmu_features;
569 static unsigned long __initdata saved_firmware_features;
572 void __init apply_feature_fixups(void)
574 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
576 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
577 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
580 * Apply the CPU-specific and firmware specific fixups to kernel text
581 * (nop out sections not relevant to this CPU or this firmware).
583 do_feature_fixups(spec->cpu_features,
584 PTRRELOC(&__start___ftr_fixup),
585 PTRRELOC(&__stop___ftr_fixup));
587 do_feature_fixups(spec->mmu_features,
588 PTRRELOC(&__start___mmu_ftr_fixup),
589 PTRRELOC(&__stop___mmu_ftr_fixup));
591 do_lwsync_fixups(spec->cpu_features,
592 PTRRELOC(&__start___lwsync_fixup),
593 PTRRELOC(&__stop___lwsync_fixup));
596 saved_firmware_features = powerpc_firmware_features;
597 do_feature_fixups(powerpc_firmware_features,
598 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
603 void __init setup_feature_keys(void)
606 * Initialise jump label. This causes all the cpu/mmu_has_feature()
607 * checks to take on their correct polarity based on the current set of
611 cpu_feature_keys_init();
612 mmu_feature_keys_init();
615 static int __init check_features(void)
617 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
618 "CPU features changed after feature patching!\n");
619 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
620 "MMU features changed after feature patching!\n");
622 WARN(saved_firmware_features != powerpc_firmware_features,
623 "Firmware features changed after feature patching!\n");
628 late_initcall(check_features);
630 #ifdef CONFIG_FTR_FIXUP_SELFTEST
633 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
635 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
636 static struct fixup_entry fixup;
638 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
640 return (unsigned long)p - (unsigned long)entry;
643 static void test_basic_patching(void)
645 extern unsigned int ftr_fixup_test1[];
646 extern unsigned int end_ftr_fixup_test1[];
647 extern unsigned int ftr_fixup_test1_orig[];
648 extern unsigned int ftr_fixup_test1_expected[];
649 int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
651 fixup.value = fixup.mask = 8;
652 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
653 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
654 fixup.alt_start_off = fixup.alt_end_off = 0;
657 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
659 /* Check we don't patch if the value matches */
660 patch_feature_section(8, &fixup);
661 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
663 /* Check we do patch if the value doesn't match */
664 patch_feature_section(0, &fixup);
665 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
667 /* Check we do patch if the mask doesn't match */
668 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
669 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
670 patch_feature_section(~8, &fixup);
671 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
674 static void test_alternative_patching(void)
676 extern unsigned int ftr_fixup_test2[];
677 extern unsigned int end_ftr_fixup_test2[];
678 extern unsigned int ftr_fixup_test2_orig[];
679 extern unsigned int ftr_fixup_test2_alt[];
680 extern unsigned int ftr_fixup_test2_expected[];
681 int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
683 fixup.value = fixup.mask = 0xF;
684 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
685 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
686 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
687 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
690 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
692 /* Check we don't patch if the value matches */
693 patch_feature_section(0xF, &fixup);
694 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
696 /* Check we do patch if the value doesn't match */
697 patch_feature_section(0, &fixup);
698 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
700 /* Check we do patch if the mask doesn't match */
701 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
702 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
703 patch_feature_section(~0xF, &fixup);
704 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
707 static void test_alternative_case_too_big(void)
709 extern unsigned int ftr_fixup_test3[];
710 extern unsigned int end_ftr_fixup_test3[];
711 extern unsigned int ftr_fixup_test3_orig[];
712 extern unsigned int ftr_fixup_test3_alt[];
713 int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
715 fixup.value = fixup.mask = 0xC;
716 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
717 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
718 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
719 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
722 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
724 /* Expect nothing to be patched, and the error returned to us */
725 check(patch_feature_section(0xF, &fixup) == 1);
726 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
727 check(patch_feature_section(0, &fixup) == 1);
728 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
729 check(patch_feature_section(~0xF, &fixup) == 1);
730 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
733 static void test_alternative_case_too_small(void)
735 extern unsigned int ftr_fixup_test4[];
736 extern unsigned int end_ftr_fixup_test4[];
737 extern unsigned int ftr_fixup_test4_orig[];
738 extern unsigned int ftr_fixup_test4_alt[];
739 extern unsigned int ftr_fixup_test4_expected[];
740 int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
743 /* Check a high-bit flag */
744 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
745 fixup.value = fixup.mask = flag;
746 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
747 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
748 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
749 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
752 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
754 /* Check we don't patch if the value matches */
755 patch_feature_section(flag, &fixup);
756 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
758 /* Check we do patch if the value doesn't match */
759 patch_feature_section(0, &fixup);
760 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
762 /* Check we do patch if the mask doesn't match */
763 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
764 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
765 patch_feature_section(~flag, &fixup);
766 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
769 static void test_alternative_case_with_branch(void)
771 extern unsigned int ftr_fixup_test5[];
772 extern unsigned int end_ftr_fixup_test5[];
773 extern unsigned int ftr_fixup_test5_expected[];
774 int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
776 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
779 static void test_alternative_case_with_external_branch(void)
781 extern unsigned int ftr_fixup_test6[];
782 extern unsigned int end_ftr_fixup_test6[];
783 extern unsigned int ftr_fixup_test6_expected[];
784 int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
786 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
789 static void test_alternative_case_with_branch_to_end(void)
791 extern unsigned int ftr_fixup_test7[];
792 extern unsigned int end_ftr_fixup_test7[];
793 extern unsigned int ftr_fixup_test7_expected[];
794 int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
796 check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
799 static void test_cpu_macros(void)
801 extern u8 ftr_fixup_test_FTR_macros[];
802 extern u8 ftr_fixup_test_FTR_macros_expected[];
803 unsigned long size = ftr_fixup_test_FTR_macros_expected -
804 ftr_fixup_test_FTR_macros;
806 /* The fixups have already been done for us during boot */
807 check(memcmp(ftr_fixup_test_FTR_macros,
808 ftr_fixup_test_FTR_macros_expected, size) == 0);
811 static void test_fw_macros(void)
814 extern u8 ftr_fixup_test_FW_FTR_macros[];
815 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
816 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
817 ftr_fixup_test_FW_FTR_macros;
819 /* The fixups have already been done for us during boot */
820 check(memcmp(ftr_fixup_test_FW_FTR_macros,
821 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
825 static void test_lwsync_macros(void)
827 extern u8 lwsync_fixup_test[];
828 extern u8 end_lwsync_fixup_test[];
829 extern u8 lwsync_fixup_test_expected_LWSYNC[];
830 extern u8 lwsync_fixup_test_expected_SYNC[];
831 unsigned long size = end_lwsync_fixup_test -
834 /* The fixups have already been done for us during boot */
835 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
836 check(memcmp(lwsync_fixup_test,
837 lwsync_fixup_test_expected_LWSYNC, size) == 0);
839 check(memcmp(lwsync_fixup_test,
840 lwsync_fixup_test_expected_SYNC, size) == 0);
845 static void __init test_prefix_patching(void)
847 extern unsigned int ftr_fixup_prefix1[];
848 extern unsigned int end_ftr_fixup_prefix1[];
849 extern unsigned int ftr_fixup_prefix1_orig[];
850 extern unsigned int ftr_fixup_prefix1_expected[];
851 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix1 - ftr_fixup_prefix1);
853 fixup.value = fixup.mask = 8;
854 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix1 + 1);
855 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix1 + 3);
856 fixup.alt_start_off = fixup.alt_end_off = 0;
859 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) == 0);
861 patch_feature_section(0, &fixup);
862 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_expected, size) == 0);
863 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) != 0);
866 static void __init test_prefix_alt_patching(void)
868 extern unsigned int ftr_fixup_prefix2[];
869 extern unsigned int end_ftr_fixup_prefix2[];
870 extern unsigned int ftr_fixup_prefix2_orig[];
871 extern unsigned int ftr_fixup_prefix2_expected[];
872 extern unsigned int ftr_fixup_prefix2_alt[];
873 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix2 - ftr_fixup_prefix2);
875 fixup.value = fixup.mask = 8;
876 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix2 + 1);
877 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix2 + 3);
878 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix2_alt);
879 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix2_alt + 2);
881 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) == 0);
883 patch_feature_section(0, &fixup);
884 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_expected, size) == 0);
885 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) != 0);
888 static void __init test_prefix_word_alt_patching(void)
890 extern unsigned int ftr_fixup_prefix3[];
891 extern unsigned int end_ftr_fixup_prefix3[];
892 extern unsigned int ftr_fixup_prefix3_orig[];
893 extern unsigned int ftr_fixup_prefix3_expected[];
894 extern unsigned int ftr_fixup_prefix3_alt[];
895 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix3 - ftr_fixup_prefix3);
897 fixup.value = fixup.mask = 8;
898 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix3 + 1);
899 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix3 + 4);
900 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix3_alt);
901 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix3_alt + 3);
903 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) == 0);
905 patch_feature_section(0, &fixup);
906 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_expected, size) == 0);
907 patch_feature_section(0, &fixup);
908 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) != 0);
911 static inline void test_prefix_patching(void) {}
912 static inline void test_prefix_alt_patching(void) {}
913 static inline void test_prefix_word_alt_patching(void) {}
914 #endif /* CONFIG_PPC64 */
916 static int __init test_feature_fixups(void)
918 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
920 test_basic_patching();
921 test_alternative_patching();
922 test_alternative_case_too_big();
923 test_alternative_case_too_small();
924 test_alternative_case_with_branch();
925 test_alternative_case_with_external_branch();
926 test_alternative_case_with_branch_to_end();
929 test_lwsync_macros();
930 test_prefix_patching();
931 test_prefix_alt_patching();
932 test_prefix_word_alt_patching();
936 late_initcall(test_feature_fixups);
938 #endif /* CONFIG_FTR_FIXUP_SELFTEST */