powerpc/64s: flush L1D on kernel entry
[linux-2.6-microblaze.git] / arch / powerpc / lib / feature-fixups.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
4  *
5  *  Modifications for ppc64:
6  *      Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
7  *
8  *  Copyright 2008 Michael Ellerman, IBM Corporation.
9  */
10
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 <asm/cputable.h>
18 #include <asm/code-patching.h>
19 #include <asm/page.h>
20 #include <asm/sections.h>
21 #include <asm/setup.h>
22 #include <asm/security_features.h>
23 #include <asm/firmware.h>
24 #include <asm/inst.h>
25
26 struct fixup_entry {
27         unsigned long   mask;
28         unsigned long   value;
29         long            start_off;
30         long            end_off;
31         long            alt_start_off;
32         long            alt_end_off;
33 };
34
35 static struct ppc_inst *calc_addr(struct fixup_entry *fcur, long offset)
36 {
37         /*
38          * We store the offset to the code as a negative offset from
39          * the start of the alt_entry, to support the VDSO. This
40          * routine converts that back into an actual address.
41          */
42         return (struct ppc_inst *)((unsigned long)fcur + offset);
43 }
44
45 static int patch_alt_instruction(struct ppc_inst *src, struct ppc_inst *dest,
46                                  struct ppc_inst *alt_start, struct ppc_inst *alt_end)
47 {
48         int err;
49         struct ppc_inst instr;
50
51         instr = ppc_inst_read(src);
52
53         if (instr_is_relative_branch(*src)) {
54                 struct ppc_inst *target = (struct ppc_inst *)branch_target(src);
55
56                 /* Branch within the section doesn't need translating */
57                 if (target < alt_start || target > alt_end) {
58                         err = translate_branch(&instr, dest, src);
59                         if (err)
60                                 return 1;
61                 }
62         }
63
64         raw_patch_instruction(dest, instr);
65
66         return 0;
67 }
68
69 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
70 {
71         struct ppc_inst *start, *end, *alt_start, *alt_end, *src, *dest, nop;
72
73         start = calc_addr(fcur, fcur->start_off);
74         end = calc_addr(fcur, fcur->end_off);
75         alt_start = calc_addr(fcur, fcur->alt_start_off);
76         alt_end = calc_addr(fcur, fcur->alt_end_off);
77
78         if ((alt_end - alt_start) > (end - start))
79                 return 1;
80
81         if ((value & fcur->mask) == fcur->value)
82                 return 0;
83
84         src = alt_start;
85         dest = start;
86
87         for (; src < alt_end; src = ppc_inst_next(src, src),
88                               dest = ppc_inst_next(dest, dest)) {
89                 if (patch_alt_instruction(src, dest, alt_start, alt_end))
90                         return 1;
91         }
92
93         nop = ppc_inst(PPC_INST_NOP);
94         for (; dest < end; dest = ppc_inst_next(dest, &nop))
95                 raw_patch_instruction(dest, nop);
96
97         return 0;
98 }
99
100 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
101 {
102         struct fixup_entry *fcur, *fend;
103
104         fcur = fixup_start;
105         fend = fixup_end;
106
107         for (; fcur < fend; fcur++) {
108                 if (patch_feature_section(value, fcur)) {
109                         WARN_ON(1);
110                         printk("Unable to patch feature section at %p - %p" \
111                                 " with %p - %p\n",
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));
116                 }
117         }
118 }
119
120 #ifdef CONFIG_PPC_BOOK3S_64
121 static void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
122 {
123         unsigned int instrs[3], *dest;
124         long *start, *end;
125         int i;
126
127         start = PTRRELOC(&__start___stf_entry_barrier_fixup),
128         end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
129
130         instrs[0] = 0x60000000; /* nop */
131         instrs[1] = 0x60000000; /* nop */
132         instrs[2] = 0x60000000; /* nop */
133
134         i = 0;
135         if (types & STF_BARRIER_FALLBACK) {
136                 instrs[i++] = 0x7d4802a6; /* mflr r10           */
137                 instrs[i++] = 0x60000000; /* branch patched below */
138                 instrs[i++] = 0x7d4803a6; /* mtlr r10           */
139         } else if (types & STF_BARRIER_EIEIO) {
140                 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
141         } else if (types & STF_BARRIER_SYNC_ORI) {
142                 instrs[i++] = 0x7c0004ac; /* hwsync             */
143                 instrs[i++] = 0xe94d0000; /* ld r10,0(r13)      */
144                 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
145         }
146
147         for (i = 0; start < end; start++, i++) {
148                 dest = (void *)start + *start;
149
150                 pr_devel("patching dest %lx\n", (unsigned long)dest);
151
152                 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
153
154                 if (types & STF_BARRIER_FALLBACK)
155                         patch_branch((struct ppc_inst *)(dest + 1),
156                                      (unsigned long)&stf_barrier_fallback,
157                                      BRANCH_SET_LINK);
158                 else
159                         patch_instruction((struct ppc_inst *)(dest + 1),
160                                           ppc_inst(instrs[1]));
161
162                 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
163         }
164
165         printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
166                 (types == STF_BARRIER_NONE)                  ? "no" :
167                 (types == STF_BARRIER_FALLBACK)              ? "fallback" :
168                 (types == STF_BARRIER_EIEIO)                 ? "eieio" :
169                 (types == (STF_BARRIER_SYNC_ORI))            ? "hwsync"
170                                                            : "unknown");
171 }
172
173 static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
174 {
175         unsigned int instrs[6], *dest;
176         long *start, *end;
177         int i;
178
179         start = PTRRELOC(&__start___stf_exit_barrier_fixup),
180         end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
181
182         instrs[0] = 0x60000000; /* nop */
183         instrs[1] = 0x60000000; /* nop */
184         instrs[2] = 0x60000000; /* nop */
185         instrs[3] = 0x60000000; /* nop */
186         instrs[4] = 0x60000000; /* nop */
187         instrs[5] = 0x60000000; /* nop */
188
189         i = 0;
190         if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
191                 if (cpu_has_feature(CPU_FTR_HVMODE)) {
192                         instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
193                         instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
194                 } else {
195                         instrs[i++] = 0x7db243a6; /* mtsprg 2,r13       */
196                         instrs[i++] = 0x7db142a6; /* mfsprg r13,1    */
197                 }
198                 instrs[i++] = 0x7c0004ac; /* hwsync             */
199                 instrs[i++] = 0xe9ad0000; /* ld r13,0(r13)      */
200                 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
201                 if (cpu_has_feature(CPU_FTR_HVMODE)) {
202                         instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
203                 } else {
204                         instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
205                 }
206         } else if (types & STF_BARRIER_EIEIO) {
207                 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
208         }
209
210         for (i = 0; start < end; start++, i++) {
211                 dest = (void *)start + *start;
212
213                 pr_devel("patching dest %lx\n", (unsigned long)dest);
214
215                 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
216                 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
217                 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
218                 patch_instruction((struct ppc_inst *)(dest + 3), ppc_inst(instrs[3]));
219                 patch_instruction((struct ppc_inst *)(dest + 4), ppc_inst(instrs[4]));
220                 patch_instruction((struct ppc_inst *)(dest + 5), ppc_inst(instrs[5]));
221         }
222         printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
223                 (types == STF_BARRIER_NONE)                  ? "no" :
224                 (types == STF_BARRIER_FALLBACK)              ? "fallback" :
225                 (types == STF_BARRIER_EIEIO)                 ? "eieio" :
226                 (types == (STF_BARRIER_SYNC_ORI))            ? "hwsync"
227                                                            : "unknown");
228 }
229
230
231 void do_stf_barrier_fixups(enum stf_barrier_type types)
232 {
233         do_stf_entry_barrier_fixups(types);
234         do_stf_exit_barrier_fixups(types);
235 }
236
237 void do_entry_flush_fixups(enum l1d_flush_type types)
238 {
239         unsigned int instrs[3], *dest;
240         long *start, *end;
241         int i;
242
243         start = PTRRELOC(&__start___entry_flush_fixup);
244         end = PTRRELOC(&__stop___entry_flush_fixup);
245
246         instrs[0] = 0x60000000; /* nop */
247         instrs[1] = 0x60000000; /* nop */
248         instrs[2] = 0x60000000; /* nop */
249
250         i = 0;
251         if (types == L1D_FLUSH_FALLBACK) {
252                 instrs[i++] = 0x7d4802a6; /* mflr r10           */
253                 instrs[i++] = 0x60000000; /* branch patched below */
254                 instrs[i++] = 0x7d4803a6; /* mtlr r10           */
255         }
256
257         if (types & L1D_FLUSH_ORI) {
258                 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
259                 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
260         }
261
262         if (types & L1D_FLUSH_MTTRIG)
263                 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
264
265         for (i = 0; start < end; start++, i++) {
266                 dest = (void *)start + *start;
267
268                 pr_devel("patching dest %lx\n", (unsigned long)dest);
269
270                 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
271
272                 if (types == L1D_FLUSH_FALLBACK)
273                         patch_branch((struct ppc_inst *)(dest + 1), (unsigned long)&entry_flush_fallback,
274                                      BRANCH_SET_LINK);
275                 else
276                         patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
277
278                 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
279         }
280
281         printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
282                 (types == L1D_FLUSH_NONE)       ? "no" :
283                 (types == L1D_FLUSH_FALLBACK)   ? "fallback displacement" :
284                 (types &  L1D_FLUSH_ORI)        ? (types & L1D_FLUSH_MTTRIG)
285                                                         ? "ori+mttrig type"
286                                                         : "ori type" :
287                 (types &  L1D_FLUSH_MTTRIG)     ? "mttrig type"
288                                                 : "unknown");
289 }
290
291 void do_rfi_flush_fixups(enum l1d_flush_type types)
292 {
293         unsigned int instrs[3], *dest;
294         long *start, *end;
295         int i;
296
297         start = PTRRELOC(&__start___rfi_flush_fixup),
298         end = PTRRELOC(&__stop___rfi_flush_fixup);
299
300         instrs[0] = 0x60000000; /* nop */
301         instrs[1] = 0x60000000; /* nop */
302         instrs[2] = 0x60000000; /* nop */
303
304         if (types & L1D_FLUSH_FALLBACK)
305                 /* b .+16 to fallback flush */
306                 instrs[0] = 0x48000010;
307
308         i = 0;
309         if (types & L1D_FLUSH_ORI) {
310                 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
311                 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
312         }
313
314         if (types & L1D_FLUSH_MTTRIG)
315                 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
316
317         for (i = 0; start < end; start++, i++) {
318                 dest = (void *)start + *start;
319
320                 pr_devel("patching dest %lx\n", (unsigned long)dest);
321
322                 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
323                 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
324                 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
325         }
326
327         printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
328                 (types == L1D_FLUSH_NONE)       ? "no" :
329                 (types == L1D_FLUSH_FALLBACK)   ? "fallback displacement" :
330                 (types &  L1D_FLUSH_ORI)        ? (types & L1D_FLUSH_MTTRIG)
331                                                         ? "ori+mttrig type"
332                                                         : "ori type" :
333                 (types &  L1D_FLUSH_MTTRIG)     ? "mttrig type"
334                                                 : "unknown");
335 }
336
337 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
338 {
339         unsigned int instr, *dest;
340         long *start, *end;
341         int i;
342
343         start = fixup_start;
344         end = fixup_end;
345
346         instr = 0x60000000; /* nop */
347
348         if (enable) {
349                 pr_info("barrier-nospec: using ORI speculation barrier\n");
350                 instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
351         }
352
353         for (i = 0; start < end; start++, i++) {
354                 dest = (void *)start + *start;
355
356                 pr_devel("patching dest %lx\n", (unsigned long)dest);
357                 patch_instruction((struct ppc_inst *)dest, ppc_inst(instr));
358         }
359
360         printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
361 }
362
363 #endif /* CONFIG_PPC_BOOK3S_64 */
364
365 #ifdef CONFIG_PPC_BARRIER_NOSPEC
366 void do_barrier_nospec_fixups(bool enable)
367 {
368         void *start, *end;
369
370         start = PTRRELOC(&__start___barrier_nospec_fixup),
371         end = PTRRELOC(&__stop___barrier_nospec_fixup);
372
373         do_barrier_nospec_fixups_range(enable, start, end);
374 }
375 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
376
377 #ifdef CONFIG_PPC_FSL_BOOK3E
378 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
379 {
380         unsigned int instr[2], *dest;
381         long *start, *end;
382         int i;
383
384         start = fixup_start;
385         end = fixup_end;
386
387         instr[0] = PPC_INST_NOP;
388         instr[1] = PPC_INST_NOP;
389
390         if (enable) {
391                 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
392                 instr[0] = PPC_INST_ISYNC;
393                 instr[1] = PPC_INST_SYNC;
394         }
395
396         for (i = 0; start < end; start++, i++) {
397                 dest = (void *)start + *start;
398
399                 pr_devel("patching dest %lx\n", (unsigned long)dest);
400                 patch_instruction((struct ppc_inst *)dest, ppc_inst(instr[0]));
401                 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instr[1]));
402         }
403
404         printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
405 }
406
407 static void patch_btb_flush_section(long *curr)
408 {
409         unsigned int *start, *end;
410
411         start = (void *)curr + *curr;
412         end = (void *)curr + *(curr + 1);
413         for (; start < end; start++) {
414                 pr_devel("patching dest %lx\n", (unsigned long)start);
415                 patch_instruction((struct ppc_inst *)start, ppc_inst(PPC_INST_NOP));
416         }
417 }
418
419 void do_btb_flush_fixups(void)
420 {
421         long *start, *end;
422
423         start = PTRRELOC(&__start__btb_flush_fixup);
424         end = PTRRELOC(&__stop__btb_flush_fixup);
425
426         for (; start < end; start += 2)
427                 patch_btb_flush_section(start);
428 }
429 #endif /* CONFIG_PPC_FSL_BOOK3E */
430
431 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
432 {
433         long *start, *end;
434         struct ppc_inst *dest;
435
436         if (!(value & CPU_FTR_LWSYNC))
437                 return ;
438
439         start = fixup_start;
440         end = fixup_end;
441
442         for (; start < end; start++) {
443                 dest = (void *)start + *start;
444                 raw_patch_instruction(dest, ppc_inst(PPC_INST_LWSYNC));
445         }
446 }
447
448 static void do_final_fixups(void)
449 {
450 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
451         struct ppc_inst inst, *src, *dest, *end;
452
453         if (PHYSICAL_START == 0)
454                 return;
455
456         src = (struct ppc_inst *)(KERNELBASE + PHYSICAL_START);
457         dest = (struct ppc_inst *)KERNELBASE;
458         end = (void *)src + (__end_interrupts - _stext);
459
460         while (src < end) {
461                 inst = ppc_inst_read(src);
462                 raw_patch_instruction(dest, inst);
463                 src = ppc_inst_next(src, src);
464                 dest = ppc_inst_next(dest, dest);
465         }
466 #endif
467 }
468
469 static unsigned long __initdata saved_cpu_features;
470 static unsigned int __initdata saved_mmu_features;
471 #ifdef CONFIG_PPC64
472 static unsigned long __initdata saved_firmware_features;
473 #endif
474
475 void __init apply_feature_fixups(void)
476 {
477         struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
478
479         *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
480         *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
481
482         /*
483          * Apply the CPU-specific and firmware specific fixups to kernel text
484          * (nop out sections not relevant to this CPU or this firmware).
485          */
486         do_feature_fixups(spec->cpu_features,
487                           PTRRELOC(&__start___ftr_fixup),
488                           PTRRELOC(&__stop___ftr_fixup));
489
490         do_feature_fixups(spec->mmu_features,
491                           PTRRELOC(&__start___mmu_ftr_fixup),
492                           PTRRELOC(&__stop___mmu_ftr_fixup));
493
494         do_lwsync_fixups(spec->cpu_features,
495                          PTRRELOC(&__start___lwsync_fixup),
496                          PTRRELOC(&__stop___lwsync_fixup));
497
498 #ifdef CONFIG_PPC64
499         saved_firmware_features = powerpc_firmware_features;
500         do_feature_fixups(powerpc_firmware_features,
501                           &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
502 #endif
503         do_final_fixups();
504 }
505
506 void __init setup_feature_keys(void)
507 {
508         /*
509          * Initialise jump label. This causes all the cpu/mmu_has_feature()
510          * checks to take on their correct polarity based on the current set of
511          * CPU/MMU features.
512          */
513         jump_label_init();
514         cpu_feature_keys_init();
515         mmu_feature_keys_init();
516 }
517
518 static int __init check_features(void)
519 {
520         WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
521              "CPU features changed after feature patching!\n");
522         WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
523              "MMU features changed after feature patching!\n");
524 #ifdef CONFIG_PPC64
525         WARN(saved_firmware_features != powerpc_firmware_features,
526              "Firmware features changed after feature patching!\n");
527 #endif
528
529         return 0;
530 }
531 late_initcall(check_features);
532
533 #ifdef CONFIG_FTR_FIXUP_SELFTEST
534
535 #define check(x)        \
536         if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
537
538 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
539 static struct fixup_entry fixup;
540
541 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
542 {
543         return (unsigned long)p - (unsigned long)entry;
544 }
545
546 static void test_basic_patching(void)
547 {
548         extern unsigned int ftr_fixup_test1[];
549         extern unsigned int end_ftr_fixup_test1[];
550         extern unsigned int ftr_fixup_test1_orig[];
551         extern unsigned int ftr_fixup_test1_expected[];
552         int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
553
554         fixup.value = fixup.mask = 8;
555         fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
556         fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
557         fixup.alt_start_off = fixup.alt_end_off = 0;
558
559         /* Sanity check */
560         check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
561
562         /* Check we don't patch if the value matches */
563         patch_feature_section(8, &fixup);
564         check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
565
566         /* Check we do patch if the value doesn't match */
567         patch_feature_section(0, &fixup);
568         check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
569
570         /* Check we do patch if the mask doesn't match */
571         memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
572         check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
573         patch_feature_section(~8, &fixup);
574         check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
575 }
576
577 static void test_alternative_patching(void)
578 {
579         extern unsigned int ftr_fixup_test2[];
580         extern unsigned int end_ftr_fixup_test2[];
581         extern unsigned int ftr_fixup_test2_orig[];
582         extern unsigned int ftr_fixup_test2_alt[];
583         extern unsigned int ftr_fixup_test2_expected[];
584         int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
585
586         fixup.value = fixup.mask = 0xF;
587         fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
588         fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
589         fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
590         fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
591
592         /* Sanity check */
593         check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
594
595         /* Check we don't patch if the value matches */
596         patch_feature_section(0xF, &fixup);
597         check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
598
599         /* Check we do patch if the value doesn't match */
600         patch_feature_section(0, &fixup);
601         check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
602
603         /* Check we do patch if the mask doesn't match */
604         memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
605         check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
606         patch_feature_section(~0xF, &fixup);
607         check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
608 }
609
610 static void test_alternative_case_too_big(void)
611 {
612         extern unsigned int ftr_fixup_test3[];
613         extern unsigned int end_ftr_fixup_test3[];
614         extern unsigned int ftr_fixup_test3_orig[];
615         extern unsigned int ftr_fixup_test3_alt[];
616         int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
617
618         fixup.value = fixup.mask = 0xC;
619         fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
620         fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
621         fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
622         fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
623
624         /* Sanity check */
625         check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
626
627         /* Expect nothing to be patched, and the error returned to us */
628         check(patch_feature_section(0xF, &fixup) == 1);
629         check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
630         check(patch_feature_section(0, &fixup) == 1);
631         check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
632         check(patch_feature_section(~0xF, &fixup) == 1);
633         check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
634 }
635
636 static void test_alternative_case_too_small(void)
637 {
638         extern unsigned int ftr_fixup_test4[];
639         extern unsigned int end_ftr_fixup_test4[];
640         extern unsigned int ftr_fixup_test4_orig[];
641         extern unsigned int ftr_fixup_test4_alt[];
642         extern unsigned int ftr_fixup_test4_expected[];
643         int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
644         unsigned long flag;
645
646         /* Check a high-bit flag */
647         flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
648         fixup.value = fixup.mask = flag;
649         fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
650         fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
651         fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
652         fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
653
654         /* Sanity check */
655         check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
656
657         /* Check we don't patch if the value matches */
658         patch_feature_section(flag, &fixup);
659         check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
660
661         /* Check we do patch if the value doesn't match */
662         patch_feature_section(0, &fixup);
663         check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
664
665         /* Check we do patch if the mask doesn't match */
666         memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
667         check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
668         patch_feature_section(~flag, &fixup);
669         check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
670 }
671
672 static void test_alternative_case_with_branch(void)
673 {
674         extern unsigned int ftr_fixup_test5[];
675         extern unsigned int end_ftr_fixup_test5[];
676         extern unsigned int ftr_fixup_test5_expected[];
677         int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
678
679         check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
680 }
681
682 static void test_alternative_case_with_external_branch(void)
683 {
684         extern unsigned int ftr_fixup_test6[];
685         extern unsigned int end_ftr_fixup_test6[];
686         extern unsigned int ftr_fixup_test6_expected[];
687         int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
688
689         check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
690 }
691
692 static void test_alternative_case_with_branch_to_end(void)
693 {
694         extern unsigned int ftr_fixup_test7[];
695         extern unsigned int end_ftr_fixup_test7[];
696         extern unsigned int ftr_fixup_test7_expected[];
697         int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
698
699         check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
700 }
701
702 static void test_cpu_macros(void)
703 {
704         extern u8 ftr_fixup_test_FTR_macros[];
705         extern u8 ftr_fixup_test_FTR_macros_expected[];
706         unsigned long size = ftr_fixup_test_FTR_macros_expected -
707                              ftr_fixup_test_FTR_macros;
708
709         /* The fixups have already been done for us during boot */
710         check(memcmp(ftr_fixup_test_FTR_macros,
711                      ftr_fixup_test_FTR_macros_expected, size) == 0);
712 }
713
714 static void test_fw_macros(void)
715 {
716 #ifdef CONFIG_PPC64
717         extern u8 ftr_fixup_test_FW_FTR_macros[];
718         extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
719         unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
720                              ftr_fixup_test_FW_FTR_macros;
721
722         /* The fixups have already been done for us during boot */
723         check(memcmp(ftr_fixup_test_FW_FTR_macros,
724                      ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
725 #endif
726 }
727
728 static void test_lwsync_macros(void)
729 {
730         extern u8 lwsync_fixup_test[];
731         extern u8 end_lwsync_fixup_test[];
732         extern u8 lwsync_fixup_test_expected_LWSYNC[];
733         extern u8 lwsync_fixup_test_expected_SYNC[];
734         unsigned long size = end_lwsync_fixup_test -
735                              lwsync_fixup_test;
736
737         /* The fixups have already been done for us during boot */
738         if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
739                 check(memcmp(lwsync_fixup_test,
740                              lwsync_fixup_test_expected_LWSYNC, size) == 0);
741         } else {
742                 check(memcmp(lwsync_fixup_test,
743                              lwsync_fixup_test_expected_SYNC, size) == 0);
744         }
745 }
746
747 #ifdef CONFIG_PPC64
748 static void __init test_prefix_patching(void)
749 {
750         extern unsigned int ftr_fixup_prefix1[];
751         extern unsigned int end_ftr_fixup_prefix1[];
752         extern unsigned int ftr_fixup_prefix1_orig[];
753         extern unsigned int ftr_fixup_prefix1_expected[];
754         int size = sizeof(unsigned int) * (end_ftr_fixup_prefix1 - ftr_fixup_prefix1);
755
756         fixup.value = fixup.mask = 8;
757         fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix1 + 1);
758         fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix1 + 3);
759         fixup.alt_start_off = fixup.alt_end_off = 0;
760
761         /* Sanity check */
762         check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) == 0);
763
764         patch_feature_section(0, &fixup);
765         check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_expected, size) == 0);
766         check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) != 0);
767 }
768
769 static void __init test_prefix_alt_patching(void)
770 {
771         extern unsigned int ftr_fixup_prefix2[];
772         extern unsigned int end_ftr_fixup_prefix2[];
773         extern unsigned int ftr_fixup_prefix2_orig[];
774         extern unsigned int ftr_fixup_prefix2_expected[];
775         extern unsigned int ftr_fixup_prefix2_alt[];
776         int size = sizeof(unsigned int) * (end_ftr_fixup_prefix2 - ftr_fixup_prefix2);
777
778         fixup.value = fixup.mask = 8;
779         fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix2 + 1);
780         fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix2 + 3);
781         fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix2_alt);
782         fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix2_alt + 2);
783         /* Sanity check */
784         check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) == 0);
785
786         patch_feature_section(0, &fixup);
787         check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_expected, size) == 0);
788         check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) != 0);
789 }
790
791 static void __init test_prefix_word_alt_patching(void)
792 {
793         extern unsigned int ftr_fixup_prefix3[];
794         extern unsigned int end_ftr_fixup_prefix3[];
795         extern unsigned int ftr_fixup_prefix3_orig[];
796         extern unsigned int ftr_fixup_prefix3_expected[];
797         extern unsigned int ftr_fixup_prefix3_alt[];
798         int size = sizeof(unsigned int) * (end_ftr_fixup_prefix3 - ftr_fixup_prefix3);
799
800         fixup.value = fixup.mask = 8;
801         fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix3 + 1);
802         fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix3 + 4);
803         fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix3_alt);
804         fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix3_alt + 3);
805         /* Sanity check */
806         check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) == 0);
807
808         patch_feature_section(0, &fixup);
809         check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_expected, size) == 0);
810         patch_feature_section(0, &fixup);
811         check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) != 0);
812 }
813 #else
814 static inline void test_prefix_patching(void) {}
815 static inline void test_prefix_alt_patching(void) {}
816 static inline void test_prefix_word_alt_patching(void) {}
817 #endif /* CONFIG_PPC64 */
818
819 static int __init test_feature_fixups(void)
820 {
821         printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
822
823         test_basic_patching();
824         test_alternative_patching();
825         test_alternative_case_too_big();
826         test_alternative_case_too_small();
827         test_alternative_case_with_branch();
828         test_alternative_case_with_external_branch();
829         test_alternative_case_with_branch_to_end();
830         test_cpu_macros();
831         test_fw_macros();
832         test_lwsync_macros();
833         test_prefix_patching();
834         test_prefix_alt_patching();
835         test_prefix_word_alt_patching();
836
837         return 0;
838 }
839 late_initcall(test_feature_fixups);
840
841 #endif /* CONFIG_FTR_FIXUP_SELFTEST */