]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/lib/feature-fixups.c
powerpc/64s: Add support for a store forwarding barrier at kernel entry/exit
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / lib / feature-fixups.c
CommitLineData
51c52e86
ME
1/*
2 * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
3 *
4 * Modifications for ppc64:
5 * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
6 *
7 * Copyright 2008 Michael Ellerman, IBM Corporation.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
3880ecb0 15#include <linux/types.h>
309b315b 16#include <linux/jump_label.h>
51c52e86 17#include <linux/kernel.h>
362e7701
ME
18#include <linux/string.h>
19#include <linux/init.h>
589ee628 20#include <linux/sched/mm.h>
51c52e86
ME
21#include <asm/cputable.h>
22#include <asm/code-patching.h>
d715e433
AB
23#include <asm/page.h>
24#include <asm/sections.h>
9402c684 25#include <asm/setup.h>
cc437c1d 26#include <asm/security_features.h>
9402c684 27#include <asm/firmware.h>
51c52e86
ME
28
29struct fixup_entry {
30 unsigned long mask;
31 unsigned long value;
32 long start_off;
33 long end_off;
fac23fe4
ME
34 long alt_start_off;
35 long alt_end_off;
51c52e86
ME
36};
37
9b1a735d 38static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
51c52e86 39{
9b1a735d
ME
40 /*
41 * We store the offset to the code as a negative offset from
42 * the start of the alt_entry, to support the VDSO. This
43 * routine converts that back into an actual address.
44 */
45 return (unsigned int *)((unsigned long)fcur + offset);
46}
47
48static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
49 unsigned int *alt_start, unsigned int *alt_end)
50{
51 unsigned int instr;
52
53 instr = *src;
54
55 if (instr_is_relative_branch(*src)) {
56 unsigned int *target = (unsigned int *)branch_target(src);
57
58 /* Branch within the section doesn't need translating */
e673e2ad 59 if (target < alt_start || target > alt_end) {
9b1a735d
ME
60 instr = translate_branch(dest, src);
61 if (!instr)
62 return 1;
63 }
64 }
65
66 patch_instruction(dest, instr);
67
68 return 0;
69}
70
71static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
72{
73 unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
74
75 start = calc_addr(fcur, fcur->start_off);
76 end = calc_addr(fcur, fcur->end_off);
77 alt_start = calc_addr(fcur, fcur->alt_start_off);
78 alt_end = calc_addr(fcur, fcur->alt_end_off);
79
80 if ((alt_end - alt_start) > (end - start))
81 return 1;
51c52e86
ME
82
83 if ((value & fcur->mask) == fcur->value)
9b1a735d 84 return 0;
51c52e86 85
9b1a735d
ME
86 src = alt_start;
87 dest = start;
51c52e86 88
9b1a735d
ME
89 for (; src < alt_end; src++, dest++) {
90 if (patch_alt_instruction(src, dest, alt_start, alt_end))
91 return 1;
51c52e86 92 }
9b1a735d
ME
93
94 for (; dest < end; dest++)
16c57b36 95 patch_instruction(dest, PPC_INST_NOP);
9b1a735d
ME
96
97 return 0;
51c52e86
ME
98}
99
100void 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
9b1a735d
ME
107 for (; fcur < fend; fcur++) {
108 if (patch_feature_section(value, fcur)) {
1856c020 109 WARN_ON(1);
9b1a735d
ME
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 }
51c52e86 118}
362e7701 119
aa8a5e00 120#ifdef CONFIG_PPC_BOOK3S_64
cc437c1d
NP
121void 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(dest, instrs[0]);
153
154 if (types & STF_BARRIER_FALLBACK)
155 patch_branch(dest + 1, (unsigned long)&stf_barrier_fallback,
156 BRANCH_SET_LINK);
157 else
158 patch_instruction(dest + 1, instrs[1]);
159
160 patch_instruction(dest + 2, instrs[2]);
161 }
162
163 printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
164 (types == STF_BARRIER_NONE) ? "no" :
165 (types == STF_BARRIER_FALLBACK) ? "fallback" :
166 (types == STF_BARRIER_EIEIO) ? "eieio" :
167 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
168 : "unknown");
169}
170
171void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
172{
173 unsigned int instrs[6], *dest;
174 long *start, *end;
175 int i;
176
177 start = PTRRELOC(&__start___stf_exit_barrier_fixup),
178 end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
179
180 instrs[0] = 0x60000000; /* nop */
181 instrs[1] = 0x60000000; /* nop */
182 instrs[2] = 0x60000000; /* nop */
183 instrs[3] = 0x60000000; /* nop */
184 instrs[4] = 0x60000000; /* nop */
185 instrs[5] = 0x60000000; /* nop */
186
187 i = 0;
188 if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
189 if (cpu_has_feature(CPU_FTR_HVMODE)) {
190 instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
191 instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
192 } else {
193 instrs[i++] = 0x7db243a6; /* mtsprg 2,r13 */
194 instrs[i++] = 0x7db142a6; /* mfsprg r13,1 */
195 }
196 instrs[i++] = 0x7c0004ac; /* hwsync */
197 instrs[i++] = 0xe9ad0000; /* ld r13,0(r13) */
198 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
199 if (cpu_has_feature(CPU_FTR_HVMODE)) {
200 instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
201 } else {
202 instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
203 }
204 } else if (types & STF_BARRIER_EIEIO) {
205 instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
206 }
207
208 for (i = 0; start < end; start++, i++) {
209 dest = (void *)start + *start;
210
211 pr_devel("patching dest %lx\n", (unsigned long)dest);
212
213 patch_instruction(dest, instrs[0]);
214 patch_instruction(dest + 1, instrs[1]);
215 patch_instruction(dest + 2, instrs[2]);
216 patch_instruction(dest + 3, instrs[3]);
217 patch_instruction(dest + 4, instrs[4]);
218 patch_instruction(dest + 5, instrs[5]);
219 }
220 printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
221 (types == STF_BARRIER_NONE) ? "no" :
222 (types == STF_BARRIER_FALLBACK) ? "fallback" :
223 (types == STF_BARRIER_EIEIO) ? "eieio" :
224 (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
225 : "unknown");
226}
227
228
229void do_stf_barrier_fixups(enum stf_barrier_type types)
230{
231 do_stf_entry_barrier_fixups(types);
232 do_stf_exit_barrier_fixups(types);
233}
234
aa8a5e00
ME
235void do_rfi_flush_fixups(enum l1d_flush_type types)
236{
237 unsigned int instrs[3], *dest;
238 long *start, *end;
239 int i;
240
241 start = PTRRELOC(&__start___rfi_flush_fixup),
242 end = PTRRELOC(&__stop___rfi_flush_fixup);
243
244 instrs[0] = 0x60000000; /* nop */
245 instrs[1] = 0x60000000; /* nop */
246 instrs[2] = 0x60000000; /* nop */
247
248 if (types & L1D_FLUSH_FALLBACK)
249 /* b .+16 to fallback flush */
250 instrs[0] = 0x48000010;
251
252 i = 0;
253 if (types & L1D_FLUSH_ORI) {
254 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
255 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
256 }
257
258 if (types & L1D_FLUSH_MTTRIG)
259 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
260
261 for (i = 0; start < end; start++, i++) {
262 dest = (void *)start + *start;
263
264 pr_devel("patching dest %lx\n", (unsigned long)dest);
265
266 patch_instruction(dest, instrs[0]);
267 patch_instruction(dest + 1, instrs[1]);
268 patch_instruction(dest + 2, instrs[2]);
269 }
270
0271696c
MFO
271 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
272 (types == L1D_FLUSH_NONE) ? "no" :
273 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
274 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
275 ? "ori+mttrig type"
276 : "ori type" :
277 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
278 : "unknown");
aa8a5e00
ME
279}
280#endif /* CONFIG_PPC_BOOK3S_64 */
281
2d1b2027
KG
282void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
283{
3d98ffbf
BH
284 long *start, *end;
285 unsigned int *dest;
2d1b2027
KG
286
287 if (!(value & CPU_FTR_LWSYNC))
288 return ;
289
290 start = fixup_start;
291 end = fixup_end;
292
293 for (; start < end; start++) {
294 dest = (void *)start + *start;
16c57b36 295 patch_instruction(dest, PPC_INST_LWSYNC);
2d1b2027
KG
296 }
297}
298
9402c684 299static void do_final_fixups(void)
d715e433
AB
300{
301#if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
302 int *src, *dest;
303 unsigned long length;
304
305 if (PHYSICAL_START == 0)
306 return;
307
308 src = (int *)(KERNELBASE + PHYSICAL_START);
309 dest = (int *)KERNELBASE;
310 length = (__end_interrupts - _stext) / sizeof(int);
311
312 while (length--) {
313 patch_instruction(dest, *src);
314 src++;
315 dest++;
316 }
317#endif
318}
319
a28e46f1
ME
320static unsigned long __initdata saved_cpu_features;
321static unsigned int __initdata saved_mmu_features;
322#ifdef CONFIG_PPC64
323static unsigned long __initdata saved_firmware_features;
324#endif
325
326void __init apply_feature_fixups(void)
9402c684 327{
2c0f9951 328 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
9402c684 329
a28e46f1
ME
330 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
331 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
332
9402c684
BH
333 /*
334 * Apply the CPU-specific and firmware specific fixups to kernel text
335 * (nop out sections not relevant to this CPU or this firmware).
336 */
337 do_feature_fixups(spec->cpu_features,
338 PTRRELOC(&__start___ftr_fixup),
339 PTRRELOC(&__stop___ftr_fixup));
340
341 do_feature_fixups(spec->mmu_features,
342 PTRRELOC(&__start___mmu_ftr_fixup),
343 PTRRELOC(&__stop___mmu_ftr_fixup));
344
345 do_lwsync_fixups(spec->cpu_features,
346 PTRRELOC(&__start___lwsync_fixup),
347 PTRRELOC(&__stop___lwsync_fixup));
348
349#ifdef CONFIG_PPC64
a28e46f1 350 saved_firmware_features = powerpc_firmware_features;
9402c684
BH
351 do_feature_fixups(powerpc_firmware_features,
352 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
353#endif
354 do_final_fixups();
97f6e0cc 355}
309b315b 356
97f6e0cc
BH
357void __init setup_feature_keys(void)
358{
309b315b
AK
359 /*
360 * Initialise jump label. This causes all the cpu/mmu_has_feature()
361 * checks to take on their correct polarity based on the current set of
362 * CPU/MMU features.
363 */
364 jump_label_init();
4db73271 365 cpu_feature_keys_init();
c12e6f24 366 mmu_feature_keys_init();
9402c684
BH
367}
368
a28e46f1
ME
369static int __init check_features(void)
370{
371 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
372 "CPU features changed after feature patching!\n");
373 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
374 "MMU features changed after feature patching!\n");
375#ifdef CONFIG_PPC64
376 WARN(saved_firmware_features != powerpc_firmware_features,
377 "Firmware features changed after feature patching!\n");
378#endif
379
380 return 0;
381}
382late_initcall(check_features);
383
362e7701
ME
384#ifdef CONFIG_FTR_FIXUP_SELFTEST
385
386#define check(x) \
387 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
388
389/* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
390static struct fixup_entry fixup;
391
392static long calc_offset(struct fixup_entry *entry, unsigned int *p)
393{
394 return (unsigned long)p - (unsigned long)entry;
395}
396
e51df2c1 397static void test_basic_patching(void)
362e7701 398{
c69a48cd
DA
399 extern unsigned int ftr_fixup_test1[];
400 extern unsigned int end_ftr_fixup_test1[];
401 extern unsigned int ftr_fixup_test1_orig[];
402 extern unsigned int ftr_fixup_test1_expected[];
403 int size = end_ftr_fixup_test1 - ftr_fixup_test1;
362e7701
ME
404
405 fixup.value = fixup.mask = 8;
c69a48cd
DA
406 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
407 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
362e7701
ME
408 fixup.alt_start_off = fixup.alt_end_off = 0;
409
410 /* Sanity check */
c69a48cd 411 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
362e7701
ME
412
413 /* Check we don't patch if the value matches */
414 patch_feature_section(8, &fixup);
c69a48cd 415 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
362e7701
ME
416
417 /* Check we do patch if the value doesn't match */
418 patch_feature_section(0, &fixup);
c69a48cd 419 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
362e7701
ME
420
421 /* Check we do patch if the mask doesn't match */
c69a48cd
DA
422 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
423 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
362e7701 424 patch_feature_section(~8, &fixup);
c69a48cd 425 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
362e7701
ME
426}
427
428static void test_alternative_patching(void)
429{
c69a48cd
DA
430 extern unsigned int ftr_fixup_test2[];
431 extern unsigned int end_ftr_fixup_test2[];
432 extern unsigned int ftr_fixup_test2_orig[];
433 extern unsigned int ftr_fixup_test2_alt[];
434 extern unsigned int ftr_fixup_test2_expected[];
435 int size = end_ftr_fixup_test2 - ftr_fixup_test2;
362e7701
ME
436
437 fixup.value = fixup.mask = 0xF;
c69a48cd
DA
438 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
439 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
440 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
441 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
362e7701
ME
442
443 /* Sanity check */
c69a48cd 444 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
362e7701
ME
445
446 /* Check we don't patch if the value matches */
447 patch_feature_section(0xF, &fixup);
c69a48cd 448 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
362e7701
ME
449
450 /* Check we do patch if the value doesn't match */
451 patch_feature_section(0, &fixup);
c69a48cd 452 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
362e7701
ME
453
454 /* Check we do patch if the mask doesn't match */
c69a48cd
DA
455 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
456 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
362e7701 457 patch_feature_section(~0xF, &fixup);
c69a48cd 458 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
362e7701
ME
459}
460
461static void test_alternative_case_too_big(void)
462{
c69a48cd
DA
463 extern unsigned int ftr_fixup_test3[];
464 extern unsigned int end_ftr_fixup_test3[];
465 extern unsigned int ftr_fixup_test3_orig[];
466 extern unsigned int ftr_fixup_test3_alt[];
467 int size = end_ftr_fixup_test3 - ftr_fixup_test3;
362e7701
ME
468
469 fixup.value = fixup.mask = 0xC;
c69a48cd
DA
470 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
471 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
472 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
473 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
362e7701
ME
474
475 /* Sanity check */
c69a48cd 476 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
362e7701
ME
477
478 /* Expect nothing to be patched, and the error returned to us */
479 check(patch_feature_section(0xF, &fixup) == 1);
c69a48cd 480 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
362e7701 481 check(patch_feature_section(0, &fixup) == 1);
c69a48cd 482 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
362e7701 483 check(patch_feature_section(~0xF, &fixup) == 1);
c69a48cd 484 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
362e7701
ME
485}
486
487static void test_alternative_case_too_small(void)
488{
c69a48cd
DA
489 extern unsigned int ftr_fixup_test4[];
490 extern unsigned int end_ftr_fixup_test4[];
491 extern unsigned int ftr_fixup_test4_orig[];
492 extern unsigned int ftr_fixup_test4_alt[];
493 extern unsigned int ftr_fixup_test4_expected[];
494 int size = end_ftr_fixup_test4 - ftr_fixup_test4;
362e7701
ME
495 unsigned long flag;
496
497 /* Check a high-bit flag */
498 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
499 fixup.value = fixup.mask = flag;
c69a48cd
DA
500 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
501 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
502 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
503 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
362e7701
ME
504
505 /* Sanity check */
c69a48cd 506 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
362e7701
ME
507
508 /* Check we don't patch if the value matches */
509 patch_feature_section(flag, &fixup);
c69a48cd 510 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
362e7701
ME
511
512 /* Check we do patch if the value doesn't match */
513 patch_feature_section(0, &fixup);
c69a48cd 514 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
362e7701
ME
515
516 /* Check we do patch if the mask doesn't match */
c69a48cd
DA
517 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
518 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
362e7701 519 patch_feature_section(~flag, &fixup);
c69a48cd 520 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
362e7701
ME
521}
522
523static void test_alternative_case_with_branch(void)
524{
c69a48cd
DA
525 extern unsigned int ftr_fixup_test5[];
526 extern unsigned int end_ftr_fixup_test5[];
527 extern unsigned int ftr_fixup_test5_expected[];
528 int size = end_ftr_fixup_test5 - ftr_fixup_test5;
362e7701 529
c69a48cd 530 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
362e7701
ME
531}
532
533static void test_alternative_case_with_external_branch(void)
534{
c69a48cd
DA
535 extern unsigned int ftr_fixup_test6[];
536 extern unsigned int end_ftr_fixup_test6[];
537 extern unsigned int ftr_fixup_test6_expected[];
538 int size = end_ftr_fixup_test6 - ftr_fixup_test6;
362e7701 539
c69a48cd 540 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
362e7701
ME
541}
542
543static void test_cpu_macros(void)
544{
c69a48cd
DA
545 extern u8 ftr_fixup_test_FTR_macros[];
546 extern u8 ftr_fixup_test_FTR_macros_expected[];
547 unsigned long size = ftr_fixup_test_FTR_macros_expected -
548 ftr_fixup_test_FTR_macros;
362e7701
ME
549
550 /* The fixups have already been done for us during boot */
c69a48cd
DA
551 check(memcmp(ftr_fixup_test_FTR_macros,
552 ftr_fixup_test_FTR_macros_expected, size) == 0);
362e7701
ME
553}
554
555static void test_fw_macros(void)
556{
557#ifdef CONFIG_PPC64
c69a48cd
DA
558 extern u8 ftr_fixup_test_FW_FTR_macros[];
559 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
560 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
561 ftr_fixup_test_FW_FTR_macros;
362e7701
ME
562
563 /* The fixups have already been done for us during boot */
c69a48cd
DA
564 check(memcmp(ftr_fixup_test_FW_FTR_macros,
565 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
362e7701
ME
566#endif
567}
568
2d1b2027
KG
569static void test_lwsync_macros(void)
570{
c69a48cd
DA
571 extern u8 lwsync_fixup_test[];
572 extern u8 end_lwsync_fixup_test[];
573 extern u8 lwsync_fixup_test_expected_LWSYNC[];
574 extern u8 lwsync_fixup_test_expected_SYNC[];
575 unsigned long size = end_lwsync_fixup_test -
576 lwsync_fixup_test;
2d1b2027
KG
577
578 /* The fixups have already been done for us during boot */
579 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
c69a48cd
DA
580 check(memcmp(lwsync_fixup_test,
581 lwsync_fixup_test_expected_LWSYNC, size) == 0);
2d1b2027 582 } else {
c69a48cd
DA
583 check(memcmp(lwsync_fixup_test,
584 lwsync_fixup_test_expected_SYNC, size) == 0);
2d1b2027
KG
585 }
586}
587
362e7701
ME
588static int __init test_feature_fixups(void)
589{
590 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
591
592 test_basic_patching();
593 test_alternative_patching();
594 test_alternative_case_too_big();
595 test_alternative_case_too_small();
596 test_alternative_case_with_branch();
597 test_alternative_case_with_external_branch();
598 test_cpu_macros();
599 test_fw_macros();
2d1b2027 600 test_lwsync_macros();
362e7701
ME
601
602 return 0;
603}
604late_initcall(test_feature_fixups);
605
606#endif /* CONFIG_FTR_FIXUP_SELFTEST */