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
calc_addr(struct fixup_entry * fcur,long offset)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
patch_alt_instruction(struct ppc_inst * src,struct ppc_inst * dest,struct ppc_inst * alt_start,struct ppc_inst * alt_end)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
patch_feature_section(unsigned long value,struct fixup_entry * fcur)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
do_feature_fixups(unsigned long value,void * fixup_start,void * fixup_end)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
do_stf_entry_barrier_fixups(enum stf_barrier_type types)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
do_stf_exit_barrier_fixups(enum stf_barrier_type types)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
do_stf_barrier_fixups(enum stf_barrier_type types)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
do_uaccess_flush_fixups(enum l1d_flush_type types)237 void do_uaccess_flush_fixups(enum l1d_flush_type types)
238 {
239 unsigned int instrs[4], *dest;
240 long *start, *end;
241 int i;
242
243 start = PTRRELOC(&__start___uaccess_flush_fixup);
244 end = PTRRELOC(&__stop___uaccess_flush_fixup);
245
246 instrs[0] = 0x60000000; /* nop */
247 instrs[1] = 0x60000000; /* nop */
248 instrs[2] = 0x60000000; /* nop */
249 instrs[3] = 0x4e800020; /* blr */
250
251 i = 0;
252 if (types == L1D_FLUSH_FALLBACK) {
253 instrs[3] = 0x60000000; /* nop */
254 /* fallthrough to fallback flush */
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 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
273 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
274 patch_instruction((struct ppc_inst *)(dest + 3), ppc_inst(instrs[3]));
275 }
276
277 printk(KERN_DEBUG "uaccess-flush: patched %d locations (%s flush)\n", i,
278 (types == L1D_FLUSH_NONE) ? "no" :
279 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
280 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
281 ? "ori+mttrig type"
282 : "ori type" :
283 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
284 : "unknown");
285 }
286
do_entry_flush_fixups(enum l1d_flush_type types)287 void do_entry_flush_fixups(enum l1d_flush_type types)
288 {
289 unsigned int instrs[3], *dest;
290 long *start, *end;
291 int i;
292
293 start = PTRRELOC(&__start___entry_flush_fixup);
294 end = PTRRELOC(&__stop___entry_flush_fixup);
295
296 instrs[0] = 0x60000000; /* nop */
297 instrs[1] = 0x60000000; /* nop */
298 instrs[2] = 0x60000000; /* nop */
299
300 i = 0;
301 if (types == L1D_FLUSH_FALLBACK) {
302 instrs[i++] = 0x7d4802a6; /* mflr r10 */
303 instrs[i++] = 0x60000000; /* branch patched below */
304 instrs[i++] = 0x7d4803a6; /* mtlr r10 */
305 }
306
307 if (types & L1D_FLUSH_ORI) {
308 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
309 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
310 }
311
312 if (types & L1D_FLUSH_MTTRIG)
313 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
314
315 for (i = 0; start < end; start++, i++) {
316 dest = (void *)start + *start;
317
318 pr_devel("patching dest %lx\n", (unsigned long)dest);
319
320 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
321
322 if (types == L1D_FLUSH_FALLBACK)
323 patch_branch((struct ppc_inst *)(dest + 1), (unsigned long)&entry_flush_fallback,
324 BRANCH_SET_LINK);
325 else
326 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
327
328 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
329 }
330
331 printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
332 (types == L1D_FLUSH_NONE) ? "no" :
333 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
334 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
335 ? "ori+mttrig type"
336 : "ori type" :
337 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
338 : "unknown");
339 }
340
do_rfi_flush_fixups(enum l1d_flush_type types)341 void do_rfi_flush_fixups(enum l1d_flush_type types)
342 {
343 unsigned int instrs[3], *dest;
344 long *start, *end;
345 int i;
346
347 start = PTRRELOC(&__start___rfi_flush_fixup),
348 end = PTRRELOC(&__stop___rfi_flush_fixup);
349
350 instrs[0] = 0x60000000; /* nop */
351 instrs[1] = 0x60000000; /* nop */
352 instrs[2] = 0x60000000; /* nop */
353
354 if (types & L1D_FLUSH_FALLBACK)
355 /* b .+16 to fallback flush */
356 instrs[0] = 0x48000010;
357
358 i = 0;
359 if (types & L1D_FLUSH_ORI) {
360 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
361 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
362 }
363
364 if (types & L1D_FLUSH_MTTRIG)
365 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
366
367 for (i = 0; start < end; start++, i++) {
368 dest = (void *)start + *start;
369
370 pr_devel("patching dest %lx\n", (unsigned long)dest);
371
372 patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
373 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
374 patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
375 }
376
377 printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
378 (types == L1D_FLUSH_NONE) ? "no" :
379 (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
380 (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
381 ? "ori+mttrig type"
382 : "ori type" :
383 (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
384 : "unknown");
385 }
386
do_barrier_nospec_fixups_range(bool enable,void * fixup_start,void * fixup_end)387 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
388 {
389 unsigned int instr, *dest;
390 long *start, *end;
391 int i;
392
393 start = fixup_start;
394 end = fixup_end;
395
396 instr = 0x60000000; /* nop */
397
398 if (enable) {
399 pr_info("barrier-nospec: using ORI speculation barrier\n");
400 instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
401 }
402
403 for (i = 0; start < end; start++, i++) {
404 dest = (void *)start + *start;
405
406 pr_devel("patching dest %lx\n", (unsigned long)dest);
407 patch_instruction((struct ppc_inst *)dest, ppc_inst(instr));
408 }
409
410 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
411 }
412
413 #endif /* CONFIG_PPC_BOOK3S_64 */
414
415 #ifdef CONFIG_PPC_BARRIER_NOSPEC
do_barrier_nospec_fixups(bool enable)416 void do_barrier_nospec_fixups(bool enable)
417 {
418 void *start, *end;
419
420 start = PTRRELOC(&__start___barrier_nospec_fixup),
421 end = PTRRELOC(&__stop___barrier_nospec_fixup);
422
423 do_barrier_nospec_fixups_range(enable, start, end);
424 }
425 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
426
427 #ifdef CONFIG_PPC_FSL_BOOK3E
do_barrier_nospec_fixups_range(bool enable,void * fixup_start,void * fixup_end)428 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
429 {
430 unsigned int instr[2], *dest;
431 long *start, *end;
432 int i;
433
434 start = fixup_start;
435 end = fixup_end;
436
437 instr[0] = PPC_INST_NOP;
438 instr[1] = PPC_INST_NOP;
439
440 if (enable) {
441 pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
442 instr[0] = PPC_INST_ISYNC;
443 instr[1] = PPC_INST_SYNC;
444 }
445
446 for (i = 0; start < end; start++, i++) {
447 dest = (void *)start + *start;
448
449 pr_devel("patching dest %lx\n", (unsigned long)dest);
450 patch_instruction((struct ppc_inst *)dest, ppc_inst(instr[0]));
451 patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instr[1]));
452 }
453
454 printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
455 }
456
patch_btb_flush_section(long * curr)457 static void patch_btb_flush_section(long *curr)
458 {
459 unsigned int *start, *end;
460
461 start = (void *)curr + *curr;
462 end = (void *)curr + *(curr + 1);
463 for (; start < end; start++) {
464 pr_devel("patching dest %lx\n", (unsigned long)start);
465 patch_instruction((struct ppc_inst *)start, ppc_inst(PPC_INST_NOP));
466 }
467 }
468
do_btb_flush_fixups(void)469 void do_btb_flush_fixups(void)
470 {
471 long *start, *end;
472
473 start = PTRRELOC(&__start__btb_flush_fixup);
474 end = PTRRELOC(&__stop__btb_flush_fixup);
475
476 for (; start < end; start += 2)
477 patch_btb_flush_section(start);
478 }
479 #endif /* CONFIG_PPC_FSL_BOOK3E */
480
do_lwsync_fixups(unsigned long value,void * fixup_start,void * fixup_end)481 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
482 {
483 long *start, *end;
484 struct ppc_inst *dest;
485
486 if (!(value & CPU_FTR_LWSYNC))
487 return ;
488
489 start = fixup_start;
490 end = fixup_end;
491
492 for (; start < end; start++) {
493 dest = (void *)start + *start;
494 raw_patch_instruction(dest, ppc_inst(PPC_INST_LWSYNC));
495 }
496 }
497
do_final_fixups(void)498 static void do_final_fixups(void)
499 {
500 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
501 struct ppc_inst inst, *src, *dest, *end;
502
503 if (PHYSICAL_START == 0)
504 return;
505
506 src = (struct ppc_inst *)(KERNELBASE + PHYSICAL_START);
507 dest = (struct ppc_inst *)KERNELBASE;
508 end = (void *)src + (__end_interrupts - _stext);
509
510 while (src < end) {
511 inst = ppc_inst_read(src);
512 raw_patch_instruction(dest, inst);
513 src = ppc_inst_next(src, src);
514 dest = ppc_inst_next(dest, dest);
515 }
516 #endif
517 }
518
519 static unsigned long __initdata saved_cpu_features;
520 static unsigned int __initdata saved_mmu_features;
521 #ifdef CONFIG_PPC64
522 static unsigned long __initdata saved_firmware_features;
523 #endif
524
apply_feature_fixups(void)525 void __init apply_feature_fixups(void)
526 {
527 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
528
529 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
530 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
531
532 /*
533 * Apply the CPU-specific and firmware specific fixups to kernel text
534 * (nop out sections not relevant to this CPU or this firmware).
535 */
536 do_feature_fixups(spec->cpu_features,
537 PTRRELOC(&__start___ftr_fixup),
538 PTRRELOC(&__stop___ftr_fixup));
539
540 do_feature_fixups(spec->mmu_features,
541 PTRRELOC(&__start___mmu_ftr_fixup),
542 PTRRELOC(&__stop___mmu_ftr_fixup));
543
544 do_lwsync_fixups(spec->cpu_features,
545 PTRRELOC(&__start___lwsync_fixup),
546 PTRRELOC(&__stop___lwsync_fixup));
547
548 #ifdef CONFIG_PPC64
549 saved_firmware_features = powerpc_firmware_features;
550 do_feature_fixups(powerpc_firmware_features,
551 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
552 #endif
553 do_final_fixups();
554 }
555
setup_feature_keys(void)556 void __init setup_feature_keys(void)
557 {
558 /*
559 * Initialise jump label. This causes all the cpu/mmu_has_feature()
560 * checks to take on their correct polarity based on the current set of
561 * CPU/MMU features.
562 */
563 jump_label_init();
564 cpu_feature_keys_init();
565 mmu_feature_keys_init();
566 }
567
check_features(void)568 static int __init check_features(void)
569 {
570 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
571 "CPU features changed after feature patching!\n");
572 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
573 "MMU features changed after feature patching!\n");
574 #ifdef CONFIG_PPC64
575 WARN(saved_firmware_features != powerpc_firmware_features,
576 "Firmware features changed after feature patching!\n");
577 #endif
578
579 return 0;
580 }
581 late_initcall(check_features);
582
583 #ifdef CONFIG_FTR_FIXUP_SELFTEST
584
585 #define check(x) \
586 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
587
588 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
589 static struct fixup_entry fixup;
590
calc_offset(struct fixup_entry * entry,unsigned int * p)591 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
592 {
593 return (unsigned long)p - (unsigned long)entry;
594 }
595
test_basic_patching(void)596 static void test_basic_patching(void)
597 {
598 extern unsigned int ftr_fixup_test1[];
599 extern unsigned int end_ftr_fixup_test1[];
600 extern unsigned int ftr_fixup_test1_orig[];
601 extern unsigned int ftr_fixup_test1_expected[];
602 int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
603
604 fixup.value = fixup.mask = 8;
605 fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
606 fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
607 fixup.alt_start_off = fixup.alt_end_off = 0;
608
609 /* Sanity check */
610 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
611
612 /* Check we don't patch if the value matches */
613 patch_feature_section(8, &fixup);
614 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
615
616 /* Check we do patch if the value doesn't match */
617 patch_feature_section(0, &fixup);
618 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
619
620 /* Check we do patch if the mask doesn't match */
621 memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
622 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
623 patch_feature_section(~8, &fixup);
624 check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
625 }
626
test_alternative_patching(void)627 static void test_alternative_patching(void)
628 {
629 extern unsigned int ftr_fixup_test2[];
630 extern unsigned int end_ftr_fixup_test2[];
631 extern unsigned int ftr_fixup_test2_orig[];
632 extern unsigned int ftr_fixup_test2_alt[];
633 extern unsigned int ftr_fixup_test2_expected[];
634 int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
635
636 fixup.value = fixup.mask = 0xF;
637 fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
638 fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
639 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
640 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
641
642 /* Sanity check */
643 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
644
645 /* Check we don't patch if the value matches */
646 patch_feature_section(0xF, &fixup);
647 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
648
649 /* Check we do patch if the value doesn't match */
650 patch_feature_section(0, &fixup);
651 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
652
653 /* Check we do patch if the mask doesn't match */
654 memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
655 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
656 patch_feature_section(~0xF, &fixup);
657 check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
658 }
659
test_alternative_case_too_big(void)660 static void test_alternative_case_too_big(void)
661 {
662 extern unsigned int ftr_fixup_test3[];
663 extern unsigned int end_ftr_fixup_test3[];
664 extern unsigned int ftr_fixup_test3_orig[];
665 extern unsigned int ftr_fixup_test3_alt[];
666 int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
667
668 fixup.value = fixup.mask = 0xC;
669 fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
670 fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
671 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
672 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
673
674 /* Sanity check */
675 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
676
677 /* Expect nothing to be patched, and the error returned to us */
678 check(patch_feature_section(0xF, &fixup) == 1);
679 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
680 check(patch_feature_section(0, &fixup) == 1);
681 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
682 check(patch_feature_section(~0xF, &fixup) == 1);
683 check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
684 }
685
test_alternative_case_too_small(void)686 static void test_alternative_case_too_small(void)
687 {
688 extern unsigned int ftr_fixup_test4[];
689 extern unsigned int end_ftr_fixup_test4[];
690 extern unsigned int ftr_fixup_test4_orig[];
691 extern unsigned int ftr_fixup_test4_alt[];
692 extern unsigned int ftr_fixup_test4_expected[];
693 int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
694 unsigned long flag;
695
696 /* Check a high-bit flag */
697 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
698 fixup.value = fixup.mask = flag;
699 fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
700 fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
701 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
702 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
703
704 /* Sanity check */
705 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
706
707 /* Check we don't patch if the value matches */
708 patch_feature_section(flag, &fixup);
709 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
710
711 /* Check we do patch if the value doesn't match */
712 patch_feature_section(0, &fixup);
713 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
714
715 /* Check we do patch if the mask doesn't match */
716 memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
717 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
718 patch_feature_section(~flag, &fixup);
719 check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
720 }
721
test_alternative_case_with_branch(void)722 static void test_alternative_case_with_branch(void)
723 {
724 extern unsigned int ftr_fixup_test5[];
725 extern unsigned int end_ftr_fixup_test5[];
726 extern unsigned int ftr_fixup_test5_expected[];
727 int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
728
729 check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
730 }
731
test_alternative_case_with_external_branch(void)732 static void test_alternative_case_with_external_branch(void)
733 {
734 extern unsigned int ftr_fixup_test6[];
735 extern unsigned int end_ftr_fixup_test6[];
736 extern unsigned int ftr_fixup_test6_expected[];
737 int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
738
739 check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
740 }
741
test_alternative_case_with_branch_to_end(void)742 static void test_alternative_case_with_branch_to_end(void)
743 {
744 extern unsigned int ftr_fixup_test7[];
745 extern unsigned int end_ftr_fixup_test7[];
746 extern unsigned int ftr_fixup_test7_expected[];
747 int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
748
749 check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
750 }
751
test_cpu_macros(void)752 static void test_cpu_macros(void)
753 {
754 extern u8 ftr_fixup_test_FTR_macros[];
755 extern u8 ftr_fixup_test_FTR_macros_expected[];
756 unsigned long size = ftr_fixup_test_FTR_macros_expected -
757 ftr_fixup_test_FTR_macros;
758
759 /* The fixups have already been done for us during boot */
760 check(memcmp(ftr_fixup_test_FTR_macros,
761 ftr_fixup_test_FTR_macros_expected, size) == 0);
762 }
763
test_fw_macros(void)764 static void test_fw_macros(void)
765 {
766 #ifdef CONFIG_PPC64
767 extern u8 ftr_fixup_test_FW_FTR_macros[];
768 extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
769 unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
770 ftr_fixup_test_FW_FTR_macros;
771
772 /* The fixups have already been done for us during boot */
773 check(memcmp(ftr_fixup_test_FW_FTR_macros,
774 ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
775 #endif
776 }
777
test_lwsync_macros(void)778 static void test_lwsync_macros(void)
779 {
780 extern u8 lwsync_fixup_test[];
781 extern u8 end_lwsync_fixup_test[];
782 extern u8 lwsync_fixup_test_expected_LWSYNC[];
783 extern u8 lwsync_fixup_test_expected_SYNC[];
784 unsigned long size = end_lwsync_fixup_test -
785 lwsync_fixup_test;
786
787 /* The fixups have already been done for us during boot */
788 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
789 check(memcmp(lwsync_fixup_test,
790 lwsync_fixup_test_expected_LWSYNC, size) == 0);
791 } else {
792 check(memcmp(lwsync_fixup_test,
793 lwsync_fixup_test_expected_SYNC, size) == 0);
794 }
795 }
796
797 #ifdef CONFIG_PPC64
test_prefix_patching(void)798 static void __init test_prefix_patching(void)
799 {
800 extern unsigned int ftr_fixup_prefix1[];
801 extern unsigned int end_ftr_fixup_prefix1[];
802 extern unsigned int ftr_fixup_prefix1_orig[];
803 extern unsigned int ftr_fixup_prefix1_expected[];
804 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix1 - ftr_fixup_prefix1);
805
806 fixup.value = fixup.mask = 8;
807 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix1 + 1);
808 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix1 + 3);
809 fixup.alt_start_off = fixup.alt_end_off = 0;
810
811 /* Sanity check */
812 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) == 0);
813
814 patch_feature_section(0, &fixup);
815 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_expected, size) == 0);
816 check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) != 0);
817 }
818
test_prefix_alt_patching(void)819 static void __init test_prefix_alt_patching(void)
820 {
821 extern unsigned int ftr_fixup_prefix2[];
822 extern unsigned int end_ftr_fixup_prefix2[];
823 extern unsigned int ftr_fixup_prefix2_orig[];
824 extern unsigned int ftr_fixup_prefix2_expected[];
825 extern unsigned int ftr_fixup_prefix2_alt[];
826 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix2 - ftr_fixup_prefix2);
827
828 fixup.value = fixup.mask = 8;
829 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix2 + 1);
830 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix2 + 3);
831 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix2_alt);
832 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix2_alt + 2);
833 /* Sanity check */
834 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) == 0);
835
836 patch_feature_section(0, &fixup);
837 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_expected, size) == 0);
838 check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) != 0);
839 }
840
test_prefix_word_alt_patching(void)841 static void __init test_prefix_word_alt_patching(void)
842 {
843 extern unsigned int ftr_fixup_prefix3[];
844 extern unsigned int end_ftr_fixup_prefix3[];
845 extern unsigned int ftr_fixup_prefix3_orig[];
846 extern unsigned int ftr_fixup_prefix3_expected[];
847 extern unsigned int ftr_fixup_prefix3_alt[];
848 int size = sizeof(unsigned int) * (end_ftr_fixup_prefix3 - ftr_fixup_prefix3);
849
850 fixup.value = fixup.mask = 8;
851 fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix3 + 1);
852 fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix3 + 4);
853 fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix3_alt);
854 fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix3_alt + 3);
855 /* Sanity check */
856 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) == 0);
857
858 patch_feature_section(0, &fixup);
859 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_expected, size) == 0);
860 patch_feature_section(0, &fixup);
861 check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) != 0);
862 }
863 #else
test_prefix_patching(void)864 static inline void test_prefix_patching(void) {}
test_prefix_alt_patching(void)865 static inline void test_prefix_alt_patching(void) {}
test_prefix_word_alt_patching(void)866 static inline void test_prefix_word_alt_patching(void) {}
867 #endif /* CONFIG_PPC64 */
868
test_feature_fixups(void)869 static int __init test_feature_fixups(void)
870 {
871 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
872
873 test_basic_patching();
874 test_alternative_patching();
875 test_alternative_case_too_big();
876 test_alternative_case_too_small();
877 test_alternative_case_with_branch();
878 test_alternative_case_with_external_branch();
879 test_alternative_case_with_branch_to_end();
880 test_cpu_macros();
881 test_fw_macros();
882 test_lwsync_macros();
883 test_prefix_patching();
884 test_prefix_alt_patching();
885 test_prefix_word_alt_patching();
886
887 return 0;
888 }
889 late_initcall(test_feature_fixups);
890
891 #endif /* CONFIG_FTR_FIXUP_SELFTEST */
892