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>
20 #include <asm/interrupt.h>
22 #include <asm/sections.h>
23 #include <asm/setup.h>
24 #include <asm/security_features.h>
25 #include <asm/firmware.h>
37 static u32 *calc_addr(struct fixup_entry *fcur, long offset)
40 * We store the offset to the code as a negative offset from
41 * the start of the alt_entry, to support the VDSO. This
42 * routine converts that back into an actual address.
44 return (u32 *)((unsigned long)fcur + offset);
47 static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start, u32 *alt_end)
52 instr = ppc_inst_read(src);
54 if (instr_is_relative_branch(ppc_inst_read(src))) {
55 u32 *target = (u32 *)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 u32 *start, *end, *alt_start, *alt_end, *src, *dest;
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 for (; dest < end; dest++)
95 raw_patch_instruction(dest, ppc_inst(PPC_RAW_NOP()));
100 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
102 struct fixup_entry *fcur, *fend;
107 for (; fcur < fend; fcur++) {
108 if (patch_feature_section(value, fcur)) {
110 printk("Unable to patch feature section at %p - %p" \
112 calc_addr(fcur, fcur->start_off),
113 calc_addr(fcur, fcur->end_off),
114 calc_addr(fcur, fcur->alt_start_off),
115 calc_addr(fcur, fcur->alt_end_off));
120 #ifdef CONFIG_PPC_BARRIER_NOSPEC
121 static bool is_fixup_addr_valid(void *dest, size_t size)
123 return system_state < SYSTEM_FREEING_INITMEM ||
124 !init_section_contains(dest, size);
127 static int do_patch_fixups(long *start, long *end, unsigned int *instrs, int num)
131 for (i = 0; start < end; start++, i++) {
133 unsigned int *dest = (void *)start + *start;
135 if (!is_fixup_addr_valid(dest, sizeof(*instrs) * num))
138 pr_devel("patching dest %lx\n", (unsigned long)dest);
140 for (j = 0; j < num; j++)
141 patch_instruction(dest + j, ppc_inst(instrs[j]));
147 #ifdef CONFIG_PPC_BOOK3S_64
148 static int do_patch_entry_fixups(long *start, long *end, unsigned int *instrs,
149 bool do_fallback, void *fallback)
153 for (i = 0; start < end; start++, i++) {
154 unsigned int *dest = (void *)start + *start;
156 if (!is_fixup_addr_valid(dest, sizeof(*instrs) * 3))
159 pr_devel("patching dest %lx\n", (unsigned long)dest);
161 // See comment in do_entry_flush_fixups() RE order of patching
163 patch_instruction(dest, ppc_inst(instrs[0]));
164 patch_instruction(dest + 2, ppc_inst(instrs[2]));
165 patch_branch(dest + 1, (unsigned long)fallback, BRANCH_SET_LINK);
167 patch_instruction(dest + 1, ppc_inst(instrs[1]));
168 patch_instruction(dest + 2, ppc_inst(instrs[2]));
169 patch_instruction(dest, ppc_inst(instrs[0]));
175 static void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
177 unsigned int instrs[3];
181 start = PTRRELOC(&__start___stf_entry_barrier_fixup);
182 end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
184 instrs[0] = PPC_RAW_NOP();
185 instrs[1] = PPC_RAW_NOP();
186 instrs[2] = PPC_RAW_NOP();
189 if (types & STF_BARRIER_FALLBACK) {
190 instrs[i++] = PPC_RAW_MFLR(_R10);
191 instrs[i++] = PPC_RAW_NOP(); /* branch patched below */
192 instrs[i++] = PPC_RAW_MTLR(_R10);
193 } else if (types & STF_BARRIER_EIEIO) {
194 instrs[i++] = PPC_RAW_EIEIO() | 0x02000000; /* eieio + bit 6 hint */
195 } else if (types & STF_BARRIER_SYNC_ORI) {
196 instrs[i++] = PPC_RAW_SYNC();
197 instrs[i++] = PPC_RAW_LD(_R10, _R13, 0);
198 instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
201 i = do_patch_entry_fixups(start, end, instrs, types & STF_BARRIER_FALLBACK,
202 &stf_barrier_fallback);
204 printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
205 (types == STF_BARRIER_NONE) ? "no" :
206 (types == STF_BARRIER_FALLBACK) ? "fallback" :
207 (types == STF_BARRIER_EIEIO) ? "eieio" :
208 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
212 static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
214 unsigned int instrs[6];
218 start = PTRRELOC(&__start___stf_exit_barrier_fixup);
219 end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
221 instrs[0] = PPC_RAW_NOP();
222 instrs[1] = PPC_RAW_NOP();
223 instrs[2] = PPC_RAW_NOP();
224 instrs[3] = PPC_RAW_NOP();
225 instrs[4] = PPC_RAW_NOP();
226 instrs[5] = PPC_RAW_NOP();
229 if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
230 if (cpu_has_feature(CPU_FTR_HVMODE)) {
231 instrs[i++] = PPC_RAW_MTSPR(SPRN_HSPRG1, _R13);
232 instrs[i++] = PPC_RAW_MFSPR(_R13, SPRN_HSPRG0);
234 instrs[i++] = PPC_RAW_MTSPR(SPRN_SPRG2, _R13);
235 instrs[i++] = PPC_RAW_MFSPR(_R13, SPRN_SPRG1);
237 instrs[i++] = PPC_RAW_SYNC();
238 instrs[i++] = PPC_RAW_LD(_R13, _R13, 0);
239 instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
240 if (cpu_has_feature(CPU_FTR_HVMODE))
241 instrs[i++] = PPC_RAW_MFSPR(_R13, SPRN_HSPRG1);
243 instrs[i++] = PPC_RAW_MFSPR(_R13, SPRN_SPRG2);
244 } else if (types & STF_BARRIER_EIEIO) {
245 instrs[i++] = PPC_RAW_EIEIO() | 0x02000000; /* eieio + bit 6 hint */
248 i = do_patch_fixups(start, end, instrs, ARRAY_SIZE(instrs));
250 printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
251 (types == STF_BARRIER_NONE) ? "no" :
252 (types == STF_BARRIER_FALLBACK) ? "fallback" :
253 (types == STF_BARRIER_EIEIO) ? "eieio" :
254 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
258 static bool stf_exit_reentrant = false;
259 static bool rfi_exit_reentrant = false;
260 static DEFINE_MUTEX(exit_flush_lock);
262 static int __do_stf_barrier_fixups(void *data)
264 enum stf_barrier_type *types = data;
266 do_stf_entry_barrier_fixups(*types);
267 do_stf_exit_barrier_fixups(*types);
272 void do_stf_barrier_fixups(enum stf_barrier_type types)
275 * The call to the fallback entry flush, and the fallback/sync-ori exit
276 * flush can not be safely patched in/out while other CPUs are
277 * executing them. So call __do_stf_barrier_fixups() on one CPU while
278 * all other CPUs spin in the stop machine core with interrupts hard
281 * The branch to mark interrupt exits non-reentrant is enabled first,
282 * then stop_machine runs which will ensure all CPUs are out of the
283 * low level interrupt exit code before patching. After the patching,
284 * if allowed, then flip the branch to allow fast exits.
287 // Prevent static key update races with do_rfi_flush_fixups()
288 mutex_lock(&exit_flush_lock);
289 static_branch_enable(&interrupt_exit_not_reentrant);
291 stop_machine(__do_stf_barrier_fixups, &types, NULL);
293 if ((types & STF_BARRIER_FALLBACK) || (types & STF_BARRIER_SYNC_ORI))
294 stf_exit_reentrant = false;
296 stf_exit_reentrant = true;
298 if (stf_exit_reentrant && rfi_exit_reentrant)
299 static_branch_disable(&interrupt_exit_not_reentrant);
301 mutex_unlock(&exit_flush_lock);
304 void do_uaccess_flush_fixups(enum l1d_flush_type types)
306 unsigned int instrs[4];
310 start = PTRRELOC(&__start___uaccess_flush_fixup);
311 end = PTRRELOC(&__stop___uaccess_flush_fixup);
313 instrs[0] = PPC_RAW_NOP();
314 instrs[1] = PPC_RAW_NOP();
315 instrs[2] = PPC_RAW_NOP();
316 instrs[3] = PPC_RAW_BLR();
319 if (types == L1D_FLUSH_FALLBACK) {
320 instrs[3] = PPC_RAW_NOP();
321 /* fallthrough to fallback flush */
324 if (types & L1D_FLUSH_ORI) {
325 instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
326 instrs[i++] = PPC_RAW_ORI(_R30, _R30, 0); /* L1d flush */
329 if (types & L1D_FLUSH_MTTRIG)
330 instrs[i++] = PPC_RAW_MTSPR(SPRN_TRIG2, _R0);
332 i = do_patch_fixups(start, end, instrs, ARRAY_SIZE(instrs));
334 printk(KERN_DEBUG "uaccess-flush: patched %d locations (%s flush)\n", i,
335 (types == L1D_FLUSH_NONE) ? "no" :
336 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
337 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
340 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
344 static int __do_entry_flush_fixups(void *data)
346 enum l1d_flush_type types = *(enum l1d_flush_type *)data;
347 unsigned int instrs[3];
351 instrs[0] = PPC_RAW_NOP();
352 instrs[1] = PPC_RAW_NOP();
353 instrs[2] = PPC_RAW_NOP();
356 if (types == L1D_FLUSH_FALLBACK) {
357 instrs[i++] = PPC_RAW_MFLR(_R10);
358 instrs[i++] = PPC_RAW_NOP(); /* branch patched below */
359 instrs[i++] = PPC_RAW_MTLR(_R10);
362 if (types & L1D_FLUSH_ORI) {
363 instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
364 instrs[i++] = PPC_RAW_ORI(_R30, _R30, 0); /* L1d flush */
367 if (types & L1D_FLUSH_MTTRIG)
368 instrs[i++] = PPC_RAW_MTSPR(SPRN_TRIG2, _R0);
371 * If we're patching in or out the fallback flush we need to be careful about the
372 * order in which we patch instructions. That's because it's possible we could
373 * take a page fault after patching one instruction, so the sequence of
374 * instructions must be safe even in a half patched state.
376 * To make that work, when patching in the fallback flush we patch in this order:
378 * - the mtlr (dest + 2)
379 * - the branch (dest + 1)
381 * That ensures the sequence is safe to execute at any point. In contrast if we
382 * patch the mtlr last, it's possible we could return from the branch and not
383 * restore LR, leading to a crash later.
385 * When patching out the fallback flush (either with nops or another flush type),
386 * we patch in this order:
387 * - the branch (dest + 1)
388 * - the mtlr (dest + 2)
391 * Note we are protected by stop_machine() from other CPUs executing the code in a
392 * semi-patched state.
395 start = PTRRELOC(&__start___entry_flush_fixup);
396 end = PTRRELOC(&__stop___entry_flush_fixup);
397 i = do_patch_entry_fixups(start, end, instrs, types == L1D_FLUSH_FALLBACK,
398 &entry_flush_fallback);
400 start = PTRRELOC(&__start___scv_entry_flush_fixup);
401 end = PTRRELOC(&__stop___scv_entry_flush_fixup);
402 i += do_patch_entry_fixups(start, end, instrs, types == L1D_FLUSH_FALLBACK,
403 &scv_entry_flush_fallback);
405 printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
406 (types == L1D_FLUSH_NONE) ? "no" :
407 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
408 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
411 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
417 void do_entry_flush_fixups(enum l1d_flush_type types)
420 * The call to the fallback flush can not be safely patched in/out while
421 * other CPUs are executing it. So call __do_entry_flush_fixups() on one
422 * CPU while all other CPUs spin in the stop machine core with interrupts
425 stop_machine(__do_entry_flush_fixups, &types, NULL);
428 static int __do_rfi_flush_fixups(void *data)
430 enum l1d_flush_type types = *(enum l1d_flush_type *)data;
431 unsigned int instrs[3];
435 start = PTRRELOC(&__start___rfi_flush_fixup);
436 end = PTRRELOC(&__stop___rfi_flush_fixup);
438 instrs[0] = PPC_RAW_NOP();
439 instrs[1] = PPC_RAW_NOP();
440 instrs[2] = PPC_RAW_NOP();
442 if (types & L1D_FLUSH_FALLBACK)
443 /* b .+16 to fallback flush */
444 instrs[0] = PPC_RAW_BRANCH(16);
447 if (types & L1D_FLUSH_ORI) {
448 instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
449 instrs[i++] = PPC_RAW_ORI(_R30, _R30, 0); /* L1d flush */
452 if (types & L1D_FLUSH_MTTRIG)
453 instrs[i++] = PPC_RAW_MTSPR(SPRN_TRIG2, _R0);
455 i = do_patch_fixups(start, end, instrs, ARRAY_SIZE(instrs));
457 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
458 (types == L1D_FLUSH_NONE) ? "no" :
459 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
460 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
463 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
469 void do_rfi_flush_fixups(enum l1d_flush_type types)
472 * stop_machine gets all CPUs out of the interrupt exit handler same
473 * as do_stf_barrier_fixups. do_rfi_flush_fixups patching can run
474 * without stop_machine, so this could be achieved with a broadcast
475 * IPI instead, but this matches the stf sequence.
478 // Prevent static key update races with do_stf_barrier_fixups()
479 mutex_lock(&exit_flush_lock);
480 static_branch_enable(&interrupt_exit_not_reentrant);
482 stop_machine(__do_rfi_flush_fixups, &types, NULL);
484 if (types & L1D_FLUSH_FALLBACK)
485 rfi_exit_reentrant = false;
487 rfi_exit_reentrant = true;
489 if (stf_exit_reentrant && rfi_exit_reentrant)
490 static_branch_disable(&interrupt_exit_not_reentrant);
492 mutex_unlock(&exit_flush_lock);
495 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
504 instr = PPC_RAW_NOP();
507 pr_info("barrier-nospec: using ORI speculation barrier\n");
508 instr = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
511 i = do_patch_fixups(start, end, &instr, 1);
513 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
516 #endif /* CONFIG_PPC_BOOK3S_64 */
518 #ifdef CONFIG_PPC_BARRIER_NOSPEC
519 void do_barrier_nospec_fixups(bool enable)
523 start = PTRRELOC(&__start___barrier_nospec_fixup);
524 end = PTRRELOC(&__stop___barrier_nospec_fixup);
526 do_barrier_nospec_fixups_range(enable, start, end);
528 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
530 #ifdef CONFIG_PPC_E500
531 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
533 unsigned int instr[2];
540 instr[0] = PPC_RAW_NOP();
541 instr[1] = PPC_RAW_NOP();
544 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
545 instr[0] = PPC_RAW_ISYNC();
546 instr[1] = PPC_RAW_SYNC();
549 i = do_patch_fixups(start, end, instr, ARRAY_SIZE(instr));
551 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
554 static void __init patch_btb_flush_section(long *curr)
556 unsigned int *start, *end;
558 start = (void *)curr + *curr;
559 end = (void *)curr + *(curr + 1);
560 for (; start < end; start++) {
561 pr_devel("patching dest %lx\n", (unsigned long)start);
562 patch_instruction(start, ppc_inst(PPC_RAW_NOP()));
566 void __init do_btb_flush_fixups(void)
570 start = PTRRELOC(&__start__btb_flush_fixup);
571 end = PTRRELOC(&__stop__btb_flush_fixup);
573 for (; start < end; start += 2)
574 patch_btb_flush_section(start);
576 #endif /* CONFIG_PPC_E500 */
578 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
583 if (!(value & CPU_FTR_LWSYNC))
589 for (; start < end; start++) {
590 dest = (void *)start + *start;
591 raw_patch_instruction(dest, ppc_inst(PPC_INST_LWSYNC));
595 static void __init do_final_fixups(void)
597 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
599 u32 *src, *dest, *end;
601 if (PHYSICAL_START == 0)
604 src = (u32 *)(KERNELBASE + PHYSICAL_START);
605 dest = (u32 *)KERNELBASE;
606 end = (void *)src + (__end_interrupts - _stext);
609 inst = ppc_inst_read(src);
610 raw_patch_instruction(dest, inst);
611 src = ppc_inst_next(src, src);
612 dest = ppc_inst_next(dest, dest);
617 static unsigned long __initdata saved_cpu_features;
618 static unsigned int __initdata saved_mmu_features;
620 static unsigned long __initdata saved_firmware_features;
623 void __init apply_feature_fixups(void)
625 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
627 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
628 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
631 * Apply the CPU-specific and firmware specific fixups to kernel text
632 * (nop out sections not relevant to this CPU or this firmware).
634 do_feature_fixups(spec->cpu_features,
635 PTRRELOC(&__start___ftr_fixup),
636 PTRRELOC(&__stop___ftr_fixup));
638 do_feature_fixups(spec->mmu_features,
639 PTRRELOC(&__start___mmu_ftr_fixup),
640 PTRRELOC(&__stop___mmu_ftr_fixup));
642 do_lwsync_fixups(spec->cpu_features,
643 PTRRELOC(&__start___lwsync_fixup),
644 PTRRELOC(&__stop___lwsync_fixup));
647 saved_firmware_features = powerpc_firmware_features;
648 do_feature_fixups(powerpc_firmware_features,
649 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
654 void __init setup_feature_keys(void)
657 * Initialise jump label. This causes all the cpu/mmu_has_feature()
658 * checks to take on their correct polarity based on the current set of
662 cpu_feature_keys_init();
663 mmu_feature_keys_init();
666 static int __init check_features(void)
668 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
669 "CPU features changed after feature patching!\n");
670 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
671 "MMU features changed after feature patching!\n");
673 WARN(saved_firmware_features != powerpc_firmware_features,
674 "Firmware features changed after feature patching!\n");
679 late_initcall(check_features);
681 #ifdef CONFIG_FTR_FIXUP_SELFTEST
684 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
686 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
687 static struct fixup_entry fixup;
689 static long __init calc_offset(struct fixup_entry *entry, unsigned int *p)
691 return (unsigned long)p - (unsigned long)entry;
694 static void __init test_basic_patching(void)
696 extern unsigned int ftr_fixup_test1[];
697 extern unsigned int end_ftr_fixup_test1[];
698 extern unsigned int ftr_fixup_test1_orig[];
699 extern unsigned int ftr_fixup_test1_expected[];
700 int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
702 fixup.value = fixup.mask = 8;
703 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
704 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
705 fixup.alt_start_off = fixup.alt_end_off = 0;
708 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
710 /* Check we don't patch if the value matches */
711 patch_feature_section(8, &fixup);
712 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
714 /* Check we do patch if the value doesn't match */
715 patch_feature_section(0, &fixup);
716 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
718 /* Check we do patch if the mask doesn't match */
719 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
720 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
721 patch_feature_section(~8, &fixup);
722 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
725 static void __init test_alternative_patching(void)
727 extern unsigned int ftr_fixup_test2[];
728 extern unsigned int end_ftr_fixup_test2[];
729 extern unsigned int ftr_fixup_test2_orig[];
730 extern unsigned int ftr_fixup_test2_alt[];
731 extern unsigned int ftr_fixup_test2_expected[];
732 int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
734 fixup.value = fixup.mask = 0xF;
735 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
736 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
737 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
738 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
741 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
743 /* Check we don't patch if the value matches */
744 patch_feature_section(0xF, &fixup);
745 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
747 /* Check we do patch if the value doesn't match */
748 patch_feature_section(0, &fixup);
749 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
751 /* Check we do patch if the mask doesn't match */
752 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
753 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
754 patch_feature_section(~0xF, &fixup);
755 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
758 static void __init test_alternative_case_too_big(void)
760 extern unsigned int ftr_fixup_test3[];
761 extern unsigned int end_ftr_fixup_test3[];
762 extern unsigned int ftr_fixup_test3_orig[];
763 extern unsigned int ftr_fixup_test3_alt[];
764 int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
766 fixup.value = fixup.mask = 0xC;
767 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
768 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
769 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
770 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
773 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
775 /* Expect nothing to be patched, and the error returned to us */
776 check(patch_feature_section(0xF, &fixup) == 1);
777 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
778 check(patch_feature_section(0, &fixup) == 1);
779 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
780 check(patch_feature_section(~0xF, &fixup) == 1);
781 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
784 static void __init test_alternative_case_too_small(void)
786 extern unsigned int ftr_fixup_test4[];
787 extern unsigned int end_ftr_fixup_test4[];
788 extern unsigned int ftr_fixup_test4_orig[];
789 extern unsigned int ftr_fixup_test4_alt[];
790 extern unsigned int ftr_fixup_test4_expected[];
791 int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
794 /* Check a high-bit flag */
795 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
796 fixup.value = fixup.mask = flag;
797 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
798 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
799 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
800 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
803 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
805 /* Check we don't patch if the value matches */
806 patch_feature_section(flag, &fixup);
807 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
809 /* Check we do patch if the value doesn't match */
810 patch_feature_section(0, &fixup);
811 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
813 /* Check we do patch if the mask doesn't match */
814 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
815 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
816 patch_feature_section(~flag, &fixup);
817 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
820 static void test_alternative_case_with_branch(void)
822 extern unsigned int ftr_fixup_test5[];
823 extern unsigned int end_ftr_fixup_test5[];
824 extern unsigned int ftr_fixup_test5_expected[];
825 int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
827 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
830 static void __init test_alternative_case_with_external_branch(void)
832 extern unsigned int ftr_fixup_test6[];
833 extern unsigned int end_ftr_fixup_test6[];
834 extern unsigned int ftr_fixup_test6_expected[];
835 int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
837 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
840 static void __init test_alternative_case_with_branch_to_end(void)
842 extern unsigned int ftr_fixup_test7[];
843 extern unsigned int end_ftr_fixup_test7[];
844 extern unsigned int ftr_fixup_test7_expected[];
845 int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
847 check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
850 static void __init test_cpu_macros(void)
852 extern u8 ftr_fixup_test_FTR_macros[];
853 extern u8 ftr_fixup_test_FTR_macros_expected[];
854 unsigned long size = ftr_fixup_test_FTR_macros_expected -
855 ftr_fixup_test_FTR_macros;
857 /* The fixups have already been done for us during boot */
858 check(memcmp(ftr_fixup_test_FTR_macros,
859 ftr_fixup_test_FTR_macros_expected, size) == 0);
862 static void __init test_fw_macros(void)
865 extern u8 ftr_fixup_test_FW_FTR_macros[];
866 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
867 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
868 ftr_fixup_test_FW_FTR_macros;
870 /* The fixups have already been done for us during boot */
871 check(memcmp(ftr_fixup_test_FW_FTR_macros,
872 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
876 static void __init test_lwsync_macros(void)
878 extern u8 lwsync_fixup_test[];
879 extern u8 end_lwsync_fixup_test[];
880 extern u8 lwsync_fixup_test_expected_LWSYNC[];
881 extern u8 lwsync_fixup_test_expected_SYNC[];
882 unsigned long size = end_lwsync_fixup_test -
885 /* The fixups have already been done for us during boot */
886 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
887 check(memcmp(lwsync_fixup_test,
888 lwsync_fixup_test_expected_LWSYNC, size) == 0);
890 check(memcmp(lwsync_fixup_test,
891 lwsync_fixup_test_expected_SYNC, size) == 0);
896 static void __init test_prefix_patching(void)
898 extern unsigned int ftr_fixup_prefix1[];
899 extern unsigned int end_ftr_fixup_prefix1[];
900 extern unsigned int ftr_fixup_prefix1_orig[];
901 extern unsigned int ftr_fixup_prefix1_expected[];
902 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix1 - ftr_fixup_prefix1);
904 fixup.value = fixup.mask = 8;
905 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix1 + 1);
906 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix1 + 3);
907 fixup.alt_start_off = fixup.alt_end_off = 0;
910 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) == 0);
912 patch_feature_section(0, &fixup);
913 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_expected, size) == 0);
914 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) != 0);
917 static void __init test_prefix_alt_patching(void)
919 extern unsigned int ftr_fixup_prefix2[];
920 extern unsigned int end_ftr_fixup_prefix2[];
921 extern unsigned int ftr_fixup_prefix2_orig[];
922 extern unsigned int ftr_fixup_prefix2_expected[];
923 extern unsigned int ftr_fixup_prefix2_alt[];
924 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix2 - ftr_fixup_prefix2);
926 fixup.value = fixup.mask = 8;
927 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix2 + 1);
928 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix2 + 3);
929 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix2_alt);
930 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix2_alt + 2);
932 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) == 0);
934 patch_feature_section(0, &fixup);
935 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_expected, size) == 0);
936 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) != 0);
939 static void __init test_prefix_word_alt_patching(void)
941 extern unsigned int ftr_fixup_prefix3[];
942 extern unsigned int end_ftr_fixup_prefix3[];
943 extern unsigned int ftr_fixup_prefix3_orig[];
944 extern unsigned int ftr_fixup_prefix3_expected[];
945 extern unsigned int ftr_fixup_prefix3_alt[];
946 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix3 - ftr_fixup_prefix3);
948 fixup.value = fixup.mask = 8;
949 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix3 + 1);
950 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix3 + 4);
951 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix3_alt);
952 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix3_alt + 3);
954 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) == 0);
956 patch_feature_section(0, &fixup);
957 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_expected, size) == 0);
958 patch_feature_section(0, &fixup);
959 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) != 0);
962 static inline void test_prefix_patching(void) {}
963 static inline void test_prefix_alt_patching(void) {}
964 static inline void test_prefix_word_alt_patching(void) {}
965 #endif /* CONFIG_PPC64 */
967 static int __init test_feature_fixups(void)
969 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
971 test_basic_patching();
972 test_alternative_patching();
973 test_alternative_case_too_big();
974 test_alternative_case_too_small();
975 test_alternative_case_with_branch();
976 test_alternative_case_with_external_branch();
977 test_alternative_case_with_branch_to_end();
980 test_lwsync_macros();
981 test_prefix_patching();
982 test_prefix_alt_patching();
983 test_prefix_word_alt_patching();
987 late_initcall(test_feature_fixups);
989 #endif /* CONFIG_FTR_FIXUP_SELFTEST */