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 // See comment in do_entry_flush_fixups() RE order of patching
154 if (types & STF_BARRIER_FALLBACK) {
155 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
156 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
157 patch_branch((struct ppc_inst *)(dest + 1),
158 (unsigned long)&stf_barrier_fallback, BRANCH_SET_LINK);
160 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
161 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
162 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
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) */
329 * If we're patching in or out the fallback flush we need to be careful about the
330 * order in which we patch instructions. That's because it's possible we could
331 * take a page fault after patching one instruction, so the sequence of
332 * instructions must be safe even in a half patched state.
334 * To make that work, when patching in the fallback flush we patch in this order:
336 * - the mtlr (dest + 2)
337 * - the branch (dest + 1)
339 * That ensures the sequence is safe to execute at any point. In contrast if we
340 * patch the mtlr last, it's possible we could return from the branch and not
341 * restore LR, leading to a crash later.
343 * When patching out the fallback flush (either with nops or another flush type),
344 * we patch in this order:
345 * - the branch (dest + 1)
346 * - the mtlr (dest + 2)
349 * Note we are protected by stop_machine() from other CPUs executing the code in a
350 * semi-patched state.
353 start = PTRRELOC(&__start___entry_flush_fixup);
354 end = PTRRELOC(&__stop___entry_flush_fixup);
355 for (i = 0; start < end; start++, i++) {
356 dest = (void *)start + *start;
358 pr_devel("patching dest %lx\n", (unsigned long)dest);
360 if (types == L1D_FLUSH_FALLBACK) {
361 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
362 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
363 patch_branch((struct ppc_inst *)(dest + 1),
364 (unsigned long)&entry_flush_fallback, BRANCH_SET_LINK);
366 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
367 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
368 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
372 start = PTRRELOC(&__start___scv_entry_flush_fixup);
373 end = PTRRELOC(&__stop___scv_entry_flush_fixup);
374 for (; start < end; start++, i++) {
375 dest = (void *)start + *start;
377 pr_devel("patching dest %lx\n", (unsigned long)dest);
379 if (types == L1D_FLUSH_FALLBACK) {
380 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
381 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
382 patch_branch((struct ppc_inst *)(dest + 1),
383 (unsigned long)&scv_entry_flush_fallback, BRANCH_SET_LINK);
385 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
386 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
387 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
392 printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
393 (types == L1D_FLUSH_NONE) ? "no" :
394 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
395 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
398 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
404 void do_entry_flush_fixups(enum l1d_flush_type types)
407 * The call to the fallback flush can not be safely patched in/out while
408 * other CPUs are executing it. So call __do_entry_flush_fixups() on one
409 * CPU while all other CPUs spin in the stop machine core with interrupts
412 stop_machine(__do_entry_flush_fixups, &types, NULL);
415 void do_rfi_flush_fixups(enum l1d_flush_type types)
417 unsigned int instrs[3], *dest;
421 start = PTRRELOC(&__start___rfi_flush_fixup);
422 end = PTRRELOC(&__stop___rfi_flush_fixup);
424 instrs[0] = 0x60000000; /* nop */
425 instrs[1] = 0x60000000; /* nop */
426 instrs[2] = 0x60000000; /* nop */
428 if (types & L1D_FLUSH_FALLBACK)
429 /* b .+16 to fallback flush */
430 instrs[0] = 0x48000010;
433 if (types & L1D_FLUSH_ORI) {
434 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
435 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
438 if (types & L1D_FLUSH_MTTRIG)
439 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
441 for (i = 0; start < end; start++, i++) {
442 dest = (void *)start + *start;
444 pr_devel("patching dest %lx\n", (unsigned long)dest);
446 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
447 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
448 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
451 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
452 (types == L1D_FLUSH_NONE) ? "no" :
453 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
454 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
457 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
461 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
463 unsigned int instr, *dest;
470 instr = 0x60000000; /* nop */
473 pr_info("barrier-nospec: using ORI speculation barrier\n");
474 instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
477 for (i = 0; start < end; start++, i++) {
478 dest = (void *)start + *start;
480 pr_devel("patching dest %lx\n", (unsigned long)dest);
481 patch_instruction((struct ppc_inst *)dest, ppc_inst(instr));
484 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
487 #endif /* CONFIG_PPC_BOOK3S_64 */
489 #ifdef CONFIG_PPC_BARRIER_NOSPEC
490 void do_barrier_nospec_fixups(bool enable)
494 start = PTRRELOC(&__start___barrier_nospec_fixup);
495 end = PTRRELOC(&__stop___barrier_nospec_fixup);
497 do_barrier_nospec_fixups_range(enable, start, end);
499 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
501 #ifdef CONFIG_PPC_FSL_BOOK3E
502 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
504 unsigned int instr[2], *dest;
511 instr[0] = PPC_INST_NOP;
512 instr[1] = PPC_INST_NOP;
515 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
516 instr[0] = PPC_INST_ISYNC;
517 instr[1] = PPC_INST_SYNC;
520 for (i = 0; start < end; start++, i++) {
521 dest = (void *)start + *start;
523 pr_devel("patching dest %lx\n", (unsigned long)dest);
524 patch_instruction((struct ppc_inst *)dest, ppc_inst(instr[0]));
525 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instr[1]));
528 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
531 static void patch_btb_flush_section(long *curr)
533 unsigned int *start, *end;
535 start = (void *)curr + *curr;
536 end = (void *)curr + *(curr + 1);
537 for (; start < end; start++) {
538 pr_devel("patching dest %lx\n", (unsigned long)start);
539 patch_instruction((struct ppc_inst *)start, ppc_inst(PPC_INST_NOP));
543 void do_btb_flush_fixups(void)
547 start = PTRRELOC(&__start__btb_flush_fixup);
548 end = PTRRELOC(&__stop__btb_flush_fixup);
550 for (; start < end; start += 2)
551 patch_btb_flush_section(start);
553 #endif /* CONFIG_PPC_FSL_BOOK3E */
555 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
558 struct ppc_inst *dest;
560 if (!(value & CPU_FTR_LWSYNC))
566 for (; start < end; start++) {
567 dest = (void *)start + *start;
568 raw_patch_instruction(dest, ppc_inst(PPC_INST_LWSYNC));
572 static void do_final_fixups(void)
574 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
575 struct ppc_inst inst, *src, *dest, *end;
577 if (PHYSICAL_START == 0)
580 src = (struct ppc_inst *)(KERNELBASE + PHYSICAL_START);
581 dest = (struct ppc_inst *)KERNELBASE;
582 end = (void *)src + (__end_interrupts - _stext);
585 inst = ppc_inst_read(src);
586 raw_patch_instruction(dest, inst);
587 src = ppc_inst_next(src, src);
588 dest = ppc_inst_next(dest, dest);
593 static unsigned long __initdata saved_cpu_features;
594 static unsigned int __initdata saved_mmu_features;
596 static unsigned long __initdata saved_firmware_features;
599 void __init apply_feature_fixups(void)
601 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
603 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
604 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
607 * Apply the CPU-specific and firmware specific fixups to kernel text
608 * (nop out sections not relevant to this CPU or this firmware).
610 do_feature_fixups(spec->cpu_features,
611 PTRRELOC(&__start___ftr_fixup),
612 PTRRELOC(&__stop___ftr_fixup));
614 do_feature_fixups(spec->mmu_features,
615 PTRRELOC(&__start___mmu_ftr_fixup),
616 PTRRELOC(&__stop___mmu_ftr_fixup));
618 do_lwsync_fixups(spec->cpu_features,
619 PTRRELOC(&__start___lwsync_fixup),
620 PTRRELOC(&__stop___lwsync_fixup));
623 saved_firmware_features = powerpc_firmware_features;
624 do_feature_fixups(powerpc_firmware_features,
625 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
630 void __init setup_feature_keys(void)
633 * Initialise jump label. This causes all the cpu/mmu_has_feature()
634 * checks to take on their correct polarity based on the current set of
638 cpu_feature_keys_init();
639 mmu_feature_keys_init();
642 static int __init check_features(void)
644 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
645 "CPU features changed after feature patching!\n");
646 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
647 "MMU features changed after feature patching!\n");
649 WARN(saved_firmware_features != powerpc_firmware_features,
650 "Firmware features changed after feature patching!\n");
655 late_initcall(check_features);
657 #ifdef CONFIG_FTR_FIXUP_SELFTEST
660 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
662 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
663 static struct fixup_entry fixup;
665 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
667 return (unsigned long)p - (unsigned long)entry;
670 static void test_basic_patching(void)
672 extern unsigned int ftr_fixup_test1[];
673 extern unsigned int end_ftr_fixup_test1[];
674 extern unsigned int ftr_fixup_test1_orig[];
675 extern unsigned int ftr_fixup_test1_expected[];
676 int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
678 fixup.value = fixup.mask = 8;
679 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
680 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
681 fixup.alt_start_off = fixup.alt_end_off = 0;
684 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
686 /* Check we don't patch if the value matches */
687 patch_feature_section(8, &fixup);
688 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
690 /* Check we do patch if the value doesn't match */
691 patch_feature_section(0, &fixup);
692 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
694 /* Check we do patch if the mask doesn't match */
695 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
696 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
697 patch_feature_section(~8, &fixup);
698 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
701 static void test_alternative_patching(void)
703 extern unsigned int ftr_fixup_test2[];
704 extern unsigned int end_ftr_fixup_test2[];
705 extern unsigned int ftr_fixup_test2_orig[];
706 extern unsigned int ftr_fixup_test2_alt[];
707 extern unsigned int ftr_fixup_test2_expected[];
708 int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
710 fixup.value = fixup.mask = 0xF;
711 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
712 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
713 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
714 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
717 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
719 /* Check we don't patch if the value matches */
720 patch_feature_section(0xF, &fixup);
721 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
723 /* Check we do patch if the value doesn't match */
724 patch_feature_section(0, &fixup);
725 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
727 /* Check we do patch if the mask doesn't match */
728 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
729 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
730 patch_feature_section(~0xF, &fixup);
731 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
734 static void test_alternative_case_too_big(void)
736 extern unsigned int ftr_fixup_test3[];
737 extern unsigned int end_ftr_fixup_test3[];
738 extern unsigned int ftr_fixup_test3_orig[];
739 extern unsigned int ftr_fixup_test3_alt[];
740 int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
742 fixup.value = fixup.mask = 0xC;
743 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
744 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
745 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
746 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
749 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
751 /* Expect nothing to be patched, and the error returned to us */
752 check(patch_feature_section(0xF, &fixup) == 1);
753 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
754 check(patch_feature_section(0, &fixup) == 1);
755 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
756 check(patch_feature_section(~0xF, &fixup) == 1);
757 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
760 static void test_alternative_case_too_small(void)
762 extern unsigned int ftr_fixup_test4[];
763 extern unsigned int end_ftr_fixup_test4[];
764 extern unsigned int ftr_fixup_test4_orig[];
765 extern unsigned int ftr_fixup_test4_alt[];
766 extern unsigned int ftr_fixup_test4_expected[];
767 int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
770 /* Check a high-bit flag */
771 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
772 fixup.value = fixup.mask = flag;
773 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
774 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
775 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
776 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
779 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
781 /* Check we don't patch if the value matches */
782 patch_feature_section(flag, &fixup);
783 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
785 /* Check we do patch if the value doesn't match */
786 patch_feature_section(0, &fixup);
787 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
789 /* Check we do patch if the mask doesn't match */
790 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
791 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
792 patch_feature_section(~flag, &fixup);
793 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
796 static void test_alternative_case_with_branch(void)
798 extern unsigned int ftr_fixup_test5[];
799 extern unsigned int end_ftr_fixup_test5[];
800 extern unsigned int ftr_fixup_test5_expected[];
801 int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
803 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
806 static void test_alternative_case_with_external_branch(void)
808 extern unsigned int ftr_fixup_test6[];
809 extern unsigned int end_ftr_fixup_test6[];
810 extern unsigned int ftr_fixup_test6_expected[];
811 int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
813 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
816 static void test_alternative_case_with_branch_to_end(void)
818 extern unsigned int ftr_fixup_test7[];
819 extern unsigned int end_ftr_fixup_test7[];
820 extern unsigned int ftr_fixup_test7_expected[];
821 int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
823 check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
826 static void test_cpu_macros(void)
828 extern u8 ftr_fixup_test_FTR_macros[];
829 extern u8 ftr_fixup_test_FTR_macros_expected[];
830 unsigned long size = ftr_fixup_test_FTR_macros_expected -
831 ftr_fixup_test_FTR_macros;
833 /* The fixups have already been done for us during boot */
834 check(memcmp(ftr_fixup_test_FTR_macros,
835 ftr_fixup_test_FTR_macros_expected, size) == 0);
838 static void test_fw_macros(void)
841 extern u8 ftr_fixup_test_FW_FTR_macros[];
842 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
843 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
844 ftr_fixup_test_FW_FTR_macros;
846 /* The fixups have already been done for us during boot */
847 check(memcmp(ftr_fixup_test_FW_FTR_macros,
848 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
852 static void test_lwsync_macros(void)
854 extern u8 lwsync_fixup_test[];
855 extern u8 end_lwsync_fixup_test[];
856 extern u8 lwsync_fixup_test_expected_LWSYNC[];
857 extern u8 lwsync_fixup_test_expected_SYNC[];
858 unsigned long size = end_lwsync_fixup_test -
861 /* The fixups have already been done for us during boot */
862 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
863 check(memcmp(lwsync_fixup_test,
864 lwsync_fixup_test_expected_LWSYNC, size) == 0);
866 check(memcmp(lwsync_fixup_test,
867 lwsync_fixup_test_expected_SYNC, size) == 0);
872 static void __init test_prefix_patching(void)
874 extern unsigned int ftr_fixup_prefix1[];
875 extern unsigned int end_ftr_fixup_prefix1[];
876 extern unsigned int ftr_fixup_prefix1_orig[];
877 extern unsigned int ftr_fixup_prefix1_expected[];
878 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix1 - ftr_fixup_prefix1);
880 fixup.value = fixup.mask = 8;
881 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix1 + 1);
882 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix1 + 3);
883 fixup.alt_start_off = fixup.alt_end_off = 0;
886 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) == 0);
888 patch_feature_section(0, &fixup);
889 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_expected, size) == 0);
890 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) != 0);
893 static void __init test_prefix_alt_patching(void)
895 extern unsigned int ftr_fixup_prefix2[];
896 extern unsigned int end_ftr_fixup_prefix2[];
897 extern unsigned int ftr_fixup_prefix2_orig[];
898 extern unsigned int ftr_fixup_prefix2_expected[];
899 extern unsigned int ftr_fixup_prefix2_alt[];
900 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix2 - ftr_fixup_prefix2);
902 fixup.value = fixup.mask = 8;
903 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix2 + 1);
904 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix2 + 3);
905 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix2_alt);
906 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix2_alt + 2);
908 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) == 0);
910 patch_feature_section(0, &fixup);
911 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_expected, size) == 0);
912 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) != 0);
915 static void __init test_prefix_word_alt_patching(void)
917 extern unsigned int ftr_fixup_prefix3[];
918 extern unsigned int end_ftr_fixup_prefix3[];
919 extern unsigned int ftr_fixup_prefix3_orig[];
920 extern unsigned int ftr_fixup_prefix3_expected[];
921 extern unsigned int ftr_fixup_prefix3_alt[];
922 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix3 - ftr_fixup_prefix3);
924 fixup.value = fixup.mask = 8;
925 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix3 + 1);
926 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix3 + 4);
927 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix3_alt);
928 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix3_alt + 3);
930 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) == 0);
932 patch_feature_section(0, &fixup);
933 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_expected, size) == 0);
934 patch_feature_section(0, &fixup);
935 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) != 0);
938 static inline void test_prefix_patching(void) {}
939 static inline void test_prefix_alt_patching(void) {}
940 static inline void test_prefix_word_alt_patching(void) {}
941 #endif /* CONFIG_PPC64 */
943 static int __init test_feature_fixups(void)
945 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
947 test_basic_patching();
948 test_alternative_patching();
949 test_alternative_case_too_big();
950 test_alternative_case_too_small();
951 test_alternative_case_with_branch();
952 test_alternative_case_with_external_branch();
953 test_alternative_case_with_branch_to_end();
956 test_lwsync_macros();
957 test_prefix_patching();
958 test_prefix_alt_patching();
959 test_prefix_word_alt_patching();
963 late_initcall(test_feature_fixups);
965 #endif /* CONFIG_FTR_FIXUP_SELFTEST */