blob: e86bfa111f3c9774350bbdce4c08a3d530efa1d9 [file] [log] [blame]
Michael Ellerman51c52e82008-06-24 11:32:36 +10001/*
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
Stephen Rothwell3880ecb2010-06-28 21:08:29 +000015#include <linux/types.h>
Aneesh Kumar K.V309b3152016-07-23 14:42:38 +053016#include <linux/jump_label.h>
Michael Ellerman51c52e82008-06-24 11:32:36 +100017#include <linux/kernel.h>
Michael Ellerman362e7702008-06-24 11:33:03 +100018#include <linux/string.h>
19#include <linux/init.h>
Michael Ellerman51c52e82008-06-24 11:32:36 +100020#include <asm/cputable.h>
21#include <asm/code-patching.h>
Anton Blanchardd715e432011-11-14 12:54:47 +000022#include <asm/page.h>
23#include <asm/sections.h>
Benjamin Herrenschmidt9402c682016-07-05 15:03:41 +100024#include <asm/setup.h>
25#include <asm/firmware.h>
Michael Ellermanc3b82eb2018-01-10 03:07:15 +110026#include <asm/setup.h>
Michael Ellerman51c52e82008-06-24 11:32:36 +100027
28struct fixup_entry {
29 unsigned long mask;
30 unsigned long value;
31 long start_off;
32 long end_off;
Michael Ellermanfac23fe2008-06-24 11:32:54 +100033 long alt_start_off;
34 long alt_end_off;
Michael Ellerman51c52e82008-06-24 11:32:36 +100035};
36
Michael Ellerman9b1a7352008-06-24 11:33:02 +100037static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
Michael Ellerman51c52e82008-06-24 11:32:36 +100038{
Michael Ellerman9b1a7352008-06-24 11:33:02 +100039 /*
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
47static 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
70static 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;
Michael Ellerman51c52e82008-06-24 11:32:36 +100081
82 if ((value & fcur->mask) == fcur->value)
Michael Ellerman9b1a7352008-06-24 11:33:02 +100083 return 0;
Michael Ellerman51c52e82008-06-24 11:32:36 +100084
Michael Ellerman9b1a7352008-06-24 11:33:02 +100085 src = alt_start;
86 dest = start;
Michael Ellerman51c52e82008-06-24 11:32:36 +100087
Michael Ellerman9b1a7352008-06-24 11:33:02 +100088 for (; src < alt_end; src++, dest++) {
89 if (patch_alt_instruction(src, dest, alt_start, alt_end))
90 return 1;
Michael Ellerman51c52e82008-06-24 11:32:36 +100091 }
Michael Ellerman9b1a7352008-06-24 11:33:02 +100092
93 for (; dest < end; dest++)
Kumar Gala16c57b32009-02-10 20:10:44 +000094 patch_instruction(dest, PPC_INST_NOP);
Michael Ellerman9b1a7352008-06-24 11:33:02 +100095
96 return 0;
Michael Ellerman51c52e82008-06-24 11:32:36 +100097}
98
99void 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
Michael Ellerman9b1a7352008-06-24 11:33:02 +1000106 for (; fcur < fend; fcur++) {
107 if (patch_feature_section(value, fcur)) {
Michael Ellerman1856c022008-07-17 14:46:00 +1000108 WARN_ON(1);
Michael Ellerman9b1a7352008-06-24 11:33:02 +1000109 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 }
Michael Ellerman51c52e82008-06-24 11:32:36 +1000117}
Michael Ellerman362e7702008-06-24 11:33:03 +1000118
Michael Ellermanc3b82eb2018-01-10 03:07:15 +1100119#ifdef CONFIG_PPC_BOOK3S_64
120void do_rfi_flush_fixups(enum l1d_flush_type types)
121{
122 unsigned int instrs[3], *dest;
123 long *start, *end;
124 int i;
125
126 start = PTRRELOC(&__start___rfi_flush_fixup),
127 end = PTRRELOC(&__stop___rfi_flush_fixup);
128
129 instrs[0] = 0x60000000; /* nop */
130 instrs[1] = 0x60000000; /* nop */
131 instrs[2] = 0x60000000; /* nop */
132
133 if (types & L1D_FLUSH_FALLBACK)
134 /* b .+16 to fallback flush */
135 instrs[0] = 0x48000010;
136
137 i = 0;
138 if (types & L1D_FLUSH_ORI) {
139 instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
140 instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
141 }
142
143 if (types & L1D_FLUSH_MTTRIG)
144 instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
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 patch_instruction(dest + 1, instrs[1]);
153 patch_instruction(dest + 2, instrs[2]);
154 }
155
156 printk(KERN_DEBUG "rfi-flush: patched %d locations\n", i);
157}
158#endif /* CONFIG_PPC_BOOK3S_64 */
159
Kumar Gala2d1b2022008-07-02 01:16:40 +1000160void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
161{
Benjamin Herrenschmidt3d98ffb2010-02-26 18:29:17 +1100162 long *start, *end;
163 unsigned int *dest;
Kumar Gala2d1b2022008-07-02 01:16:40 +1000164
165 if (!(value & CPU_FTR_LWSYNC))
166 return ;
167
168 start = fixup_start;
169 end = fixup_end;
170
171 for (; start < end; start++) {
172 dest = (void *)start + *start;
Kumar Gala16c57b32009-02-10 20:10:44 +0000173 patch_instruction(dest, PPC_INST_LWSYNC);
Kumar Gala2d1b2022008-07-02 01:16:40 +1000174 }
175}
176
Benjamin Herrenschmidt9402c682016-07-05 15:03:41 +1000177static void do_final_fixups(void)
Anton Blanchardd715e432011-11-14 12:54:47 +0000178{
179#if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
180 int *src, *dest;
181 unsigned long length;
182
183 if (PHYSICAL_START == 0)
184 return;
185
186 src = (int *)(KERNELBASE + PHYSICAL_START);
187 dest = (int *)KERNELBASE;
188 length = (__end_interrupts - _stext) / sizeof(int);
189
190 while (length--) {
191 patch_instruction(dest, *src);
192 src++;
193 dest++;
194 }
195#endif
196}
197
Michael Ellermana28e46f2016-07-26 22:29:18 +1000198static unsigned long __initdata saved_cpu_features;
199static unsigned int __initdata saved_mmu_features;
200#ifdef CONFIG_PPC64
201static unsigned long __initdata saved_firmware_features;
202#endif
203
204void __init apply_feature_fixups(void)
Benjamin Herrenschmidt9402c682016-07-05 15:03:41 +1000205{
Benjamin Herrenschmidt2c0f9952016-08-02 15:53:01 +1000206 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
Benjamin Herrenschmidt9402c682016-07-05 15:03:41 +1000207
Michael Ellermana28e46f2016-07-26 22:29:18 +1000208 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
209 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
210
Benjamin Herrenschmidt9402c682016-07-05 15:03:41 +1000211 /*
212 * Apply the CPU-specific and firmware specific fixups to kernel text
213 * (nop out sections not relevant to this CPU or this firmware).
214 */
215 do_feature_fixups(spec->cpu_features,
216 PTRRELOC(&__start___ftr_fixup),
217 PTRRELOC(&__stop___ftr_fixup));
218
219 do_feature_fixups(spec->mmu_features,
220 PTRRELOC(&__start___mmu_ftr_fixup),
221 PTRRELOC(&__stop___mmu_ftr_fixup));
222
223 do_lwsync_fixups(spec->cpu_features,
224 PTRRELOC(&__start___lwsync_fixup),
225 PTRRELOC(&__stop___lwsync_fixup));
226
227#ifdef CONFIG_PPC64
Michael Ellermana28e46f2016-07-26 22:29:18 +1000228 saved_firmware_features = powerpc_firmware_features;
Benjamin Herrenschmidt9402c682016-07-05 15:03:41 +1000229 do_feature_fixups(powerpc_firmware_features,
230 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
231#endif
232 do_final_fixups();
Benjamin Herrenschmidt97f6e0c2016-08-10 17:27:34 +1000233}
Aneesh Kumar K.V309b3152016-07-23 14:42:38 +0530234
Benjamin Herrenschmidt97f6e0c2016-08-10 17:27:34 +1000235void __init setup_feature_keys(void)
236{
Aneesh Kumar K.V309b3152016-07-23 14:42:38 +0530237 /*
238 * Initialise jump label. This causes all the cpu/mmu_has_feature()
239 * checks to take on their correct polarity based on the current set of
240 * CPU/MMU features.
241 */
242 jump_label_init();
Kevin Hao4db73272016-07-23 14:42:41 +0530243 cpu_feature_keys_init();
Kevin Haoc12e6f22016-07-23 14:42:42 +0530244 mmu_feature_keys_init();
Benjamin Herrenschmidt9402c682016-07-05 15:03:41 +1000245}
246
Michael Ellermana28e46f2016-07-26 22:29:18 +1000247static int __init check_features(void)
248{
249 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
250 "CPU features changed after feature patching!\n");
251 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
252 "MMU features changed after feature patching!\n");
253#ifdef CONFIG_PPC64
254 WARN(saved_firmware_features != powerpc_firmware_features,
255 "Firmware features changed after feature patching!\n");
256#endif
257
258 return 0;
259}
260late_initcall(check_features);
261
Michael Ellerman362e7702008-06-24 11:33:03 +1000262#ifdef CONFIG_FTR_FIXUP_SELFTEST
263
264#define check(x) \
265 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
266
267/* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
268static struct fixup_entry fixup;
269
270static long calc_offset(struct fixup_entry *entry, unsigned int *p)
271{
272 return (unsigned long)p - (unsigned long)entry;
273}
274
Anton Blancharde51df2c2014-08-20 08:55:18 +1000275static void test_basic_patching(void)
Michael Ellerman362e7702008-06-24 11:33:03 +1000276{
277 extern unsigned int ftr_fixup_test1;
278 extern unsigned int end_ftr_fixup_test1;
279 extern unsigned int ftr_fixup_test1_orig;
280 extern unsigned int ftr_fixup_test1_expected;
281 int size = &end_ftr_fixup_test1 - &ftr_fixup_test1;
282
283 fixup.value = fixup.mask = 8;
284 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test1 + 1);
285 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test1 + 2);
286 fixup.alt_start_off = fixup.alt_end_off = 0;
287
288 /* Sanity check */
289 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
290
291 /* Check we don't patch if the value matches */
292 patch_feature_section(8, &fixup);
293 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
294
295 /* Check we do patch if the value doesn't match */
296 patch_feature_section(0, &fixup);
297 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
298
299 /* Check we do patch if the mask doesn't match */
300 memcpy(&ftr_fixup_test1, &ftr_fixup_test1_orig, size);
301 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
302 patch_feature_section(~8, &fixup);
303 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
304}
305
306static void test_alternative_patching(void)
307{
308 extern unsigned int ftr_fixup_test2;
309 extern unsigned int end_ftr_fixup_test2;
310 extern unsigned int ftr_fixup_test2_orig;
311 extern unsigned int ftr_fixup_test2_alt;
312 extern unsigned int ftr_fixup_test2_expected;
313 int size = &end_ftr_fixup_test2 - &ftr_fixup_test2;
314
315 fixup.value = fixup.mask = 0xF;
316 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test2 + 1);
317 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test2 + 2);
318 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test2_alt);
319 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test2_alt + 1);
320
321 /* Sanity check */
322 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
323
324 /* Check we don't patch if the value matches */
325 patch_feature_section(0xF, &fixup);
326 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
327
328 /* Check we do patch if the value doesn't match */
329 patch_feature_section(0, &fixup);
330 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
331
332 /* Check we do patch if the mask doesn't match */
333 memcpy(&ftr_fixup_test2, &ftr_fixup_test2_orig, size);
334 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
335 patch_feature_section(~0xF, &fixup);
336 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
337}
338
339static void test_alternative_case_too_big(void)
340{
341 extern unsigned int ftr_fixup_test3;
342 extern unsigned int end_ftr_fixup_test3;
343 extern unsigned int ftr_fixup_test3_orig;
344 extern unsigned int ftr_fixup_test3_alt;
345 int size = &end_ftr_fixup_test3 - &ftr_fixup_test3;
346
347 fixup.value = fixup.mask = 0xC;
348 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test3 + 1);
349 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test3 + 2);
350 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test3_alt);
351 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test3_alt + 2);
352
353 /* Sanity check */
354 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
355
356 /* Expect nothing to be patched, and the error returned to us */
357 check(patch_feature_section(0xF, &fixup) == 1);
358 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
359 check(patch_feature_section(0, &fixup) == 1);
360 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
361 check(patch_feature_section(~0xF, &fixup) == 1);
362 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
363}
364
365static void test_alternative_case_too_small(void)
366{
367 extern unsigned int ftr_fixup_test4;
368 extern unsigned int end_ftr_fixup_test4;
369 extern unsigned int ftr_fixup_test4_orig;
370 extern unsigned int ftr_fixup_test4_alt;
371 extern unsigned int ftr_fixup_test4_expected;
372 int size = &end_ftr_fixup_test4 - &ftr_fixup_test4;
373 unsigned long flag;
374
375 /* Check a high-bit flag */
376 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
377 fixup.value = fixup.mask = flag;
378 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test4 + 1);
379 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test4 + 5);
380 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test4_alt);
381 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test4_alt + 2);
382
383 /* Sanity check */
384 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
385
386 /* Check we don't patch if the value matches */
387 patch_feature_section(flag, &fixup);
388 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
389
390 /* Check we do patch if the value doesn't match */
391 patch_feature_section(0, &fixup);
392 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
393
394 /* Check we do patch if the mask doesn't match */
395 memcpy(&ftr_fixup_test4, &ftr_fixup_test4_orig, size);
396 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
397 patch_feature_section(~flag, &fixup);
398 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
399}
400
401static void test_alternative_case_with_branch(void)
402{
403 extern unsigned int ftr_fixup_test5;
404 extern unsigned int end_ftr_fixup_test5;
405 extern unsigned int ftr_fixup_test5_expected;
406 int size = &end_ftr_fixup_test5 - &ftr_fixup_test5;
407
408 check(memcmp(&ftr_fixup_test5, &ftr_fixup_test5_expected, size) == 0);
409}
410
411static void test_alternative_case_with_external_branch(void)
412{
413 extern unsigned int ftr_fixup_test6;
414 extern unsigned int end_ftr_fixup_test6;
415 extern unsigned int ftr_fixup_test6_expected;
416 int size = &end_ftr_fixup_test6 - &ftr_fixup_test6;
417
418 check(memcmp(&ftr_fixup_test6, &ftr_fixup_test6_expected, size) == 0);
419}
420
421static void test_cpu_macros(void)
422{
Stephen Rothwell3880ecb2010-06-28 21:08:29 +0000423 extern u8 ftr_fixup_test_FTR_macros;
424 extern u8 ftr_fixup_test_FTR_macros_expected;
Michael Ellerman362e7702008-06-24 11:33:03 +1000425 unsigned long size = &ftr_fixup_test_FTR_macros_expected -
426 &ftr_fixup_test_FTR_macros;
427
428 /* The fixups have already been done for us during boot */
429 check(memcmp(&ftr_fixup_test_FTR_macros,
430 &ftr_fixup_test_FTR_macros_expected, size) == 0);
431}
432
433static void test_fw_macros(void)
434{
435#ifdef CONFIG_PPC64
Stephen Rothwell3880ecb2010-06-28 21:08:29 +0000436 extern u8 ftr_fixup_test_FW_FTR_macros;
437 extern u8 ftr_fixup_test_FW_FTR_macros_expected;
Michael Ellerman362e7702008-06-24 11:33:03 +1000438 unsigned long size = &ftr_fixup_test_FW_FTR_macros_expected -
439 &ftr_fixup_test_FW_FTR_macros;
440
441 /* The fixups have already been done for us during boot */
442 check(memcmp(&ftr_fixup_test_FW_FTR_macros,
443 &ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
444#endif
445}
446
Kumar Gala2d1b2022008-07-02 01:16:40 +1000447static void test_lwsync_macros(void)
448{
Stephen Rothwell3880ecb2010-06-28 21:08:29 +0000449 extern u8 lwsync_fixup_test;
450 extern u8 end_lwsync_fixup_test;
451 extern u8 lwsync_fixup_test_expected_LWSYNC;
452 extern u8 lwsync_fixup_test_expected_SYNC;
Kumar Gala2d1b2022008-07-02 01:16:40 +1000453 unsigned long size = &end_lwsync_fixup_test -
454 &lwsync_fixup_test;
455
456 /* The fixups have already been done for us during boot */
457 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
458 check(memcmp(&lwsync_fixup_test,
459 &lwsync_fixup_test_expected_LWSYNC, size) == 0);
460 } else {
461 check(memcmp(&lwsync_fixup_test,
462 &lwsync_fixup_test_expected_SYNC, size) == 0);
463 }
464}
465
Michael Ellerman362e7702008-06-24 11:33:03 +1000466static int __init test_feature_fixups(void)
467{
468 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
469
470 test_basic_patching();
471 test_alternative_patching();
472 test_alternative_case_too_big();
473 test_alternative_case_too_small();
474 test_alternative_case_with_branch();
475 test_alternative_case_with_external_branch();
476 test_cpu_macros();
477 test_fw_macros();
Kumar Gala2d1b2022008-07-02 01:16:40 +1000478 test_lwsync_macros();
Michael Ellerman362e7702008-06-24 11:33:03 +1000479
480 return 0;
481}
482late_initcall(test_feature_fixups);
483
484#endif /* CONFIG_FTR_FIXUP_SELFTEST */