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