]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/lib/feature-fixups.c
powerpc: Add kconfig option to use jump labels for cpu/mmu_has_feature()
[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>
51c52e86
ME
20#include <asm/cputable.h>
21#include <asm/code-patching.h>
d715e433
AB
22#include <asm/page.h>
23#include <asm/sections.h>
9402c684
BH
24#include <asm/setup.h>
25#include <asm/firmware.h>
51c52e86
ME
26
27struct fixup_entry {
28 unsigned long mask;
29 unsigned long value;
30 long start_off;
31 long end_off;
fac23fe4
ME
32 long alt_start_off;
33 long alt_end_off;
51c52e86
ME
34};
35
9b1a735d 36static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
51c52e86 37{
9b1a735d
ME
38 /*
39 * We store the offset to the code as a negative offset from
40 * the start of the alt_entry, to support the VDSO. This
41 * routine converts that back into an actual address.
42 */
43 return (unsigned int *)((unsigned long)fcur + offset);
44}
45
46static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
47 unsigned int *alt_start, unsigned int *alt_end)
48{
49 unsigned int instr;
50
51 instr = *src;
52
53 if (instr_is_relative_branch(*src)) {
54 unsigned int *target = (unsigned int *)branch_target(src);
55
56 /* Branch within the section doesn't need translating */
57 if (target < alt_start || target >= alt_end) {
58 instr = translate_branch(dest, src);
59 if (!instr)
60 return 1;
61 }
62 }
63
64 patch_instruction(dest, instr);
65
66 return 0;
67}
68
69static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
70{
71 unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
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;
51c52e86
ME
80
81 if ((value & fcur->mask) == fcur->value)
9b1a735d 82 return 0;
51c52e86 83
9b1a735d
ME
84 src = alt_start;
85 dest = start;
51c52e86 86
9b1a735d
ME
87 for (; src < alt_end; src++, dest++) {
88 if (patch_alt_instruction(src, dest, alt_start, alt_end))
89 return 1;
51c52e86 90 }
9b1a735d
ME
91
92 for (; dest < end; dest++)
16c57b36 93 patch_instruction(dest, PPC_INST_NOP);
9b1a735d
ME
94
95 return 0;
51c52e86
ME
96}
97
98void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
99{
100 struct fixup_entry *fcur, *fend;
101
102 fcur = fixup_start;
103 fend = fixup_end;
104
9b1a735d
ME
105 for (; fcur < fend; fcur++) {
106 if (patch_feature_section(value, fcur)) {
1856c020 107 WARN_ON(1);
9b1a735d
ME
108 printk("Unable to patch feature section at %p - %p" \
109 " with %p - %p\n",
110 calc_addr(fcur, fcur->start_off),
111 calc_addr(fcur, fcur->end_off),
112 calc_addr(fcur, fcur->alt_start_off),
113 calc_addr(fcur, fcur->alt_end_off));
114 }
115 }
51c52e86 116}
362e7701 117
2d1b2027
KG
118void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
119{
3d98ffbf
BH
120 long *start, *end;
121 unsigned int *dest;
2d1b2027
KG
122
123 if (!(value & CPU_FTR_LWSYNC))
124 return ;
125
126 start = fixup_start;
127 end = fixup_end;
128
129 for (; start < end; start++) {
130 dest = (void *)start + *start;
16c57b36 131 patch_instruction(dest, PPC_INST_LWSYNC);
2d1b2027
KG
132 }
133}
134
9402c684 135static void do_final_fixups(void)
d715e433
AB
136{
137#if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
138 int *src, *dest;
139 unsigned long length;
140
141 if (PHYSICAL_START == 0)
142 return;
143
144 src = (int *)(KERNELBASE + PHYSICAL_START);
145 dest = (int *)KERNELBASE;
146 length = (__end_interrupts - _stext) / sizeof(int);
147
148 while (length--) {
149 patch_instruction(dest, *src);
150 src++;
151 dest++;
152 }
153#endif
154}
155
a28e46f1
ME
156static unsigned long __initdata saved_cpu_features;
157static unsigned int __initdata saved_mmu_features;
158#ifdef CONFIG_PPC64
159static unsigned long __initdata saved_firmware_features;
160#endif
161
162void __init apply_feature_fixups(void)
9402c684
BH
163{
164 struct cpu_spec *spec = *PTRRELOC(&cur_cpu_spec);
165
a28e46f1
ME
166 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
167 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
168
9402c684
BH
169 /*
170 * Apply the CPU-specific and firmware specific fixups to kernel text
171 * (nop out sections not relevant to this CPU or this firmware).
172 */
173 do_feature_fixups(spec->cpu_features,
174 PTRRELOC(&__start___ftr_fixup),
175 PTRRELOC(&__stop___ftr_fixup));
176
177 do_feature_fixups(spec->mmu_features,
178 PTRRELOC(&__start___mmu_ftr_fixup),
179 PTRRELOC(&__stop___mmu_ftr_fixup));
180
181 do_lwsync_fixups(spec->cpu_features,
182 PTRRELOC(&__start___lwsync_fixup),
183 PTRRELOC(&__stop___lwsync_fixup));
184
185#ifdef CONFIG_PPC64
a28e46f1 186 saved_firmware_features = powerpc_firmware_features;
9402c684
BH
187 do_feature_fixups(powerpc_firmware_features,
188 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
189#endif
190 do_final_fixups();
309b315b
AK
191
192 /*
193 * Initialise jump label. This causes all the cpu/mmu_has_feature()
194 * checks to take on their correct polarity based on the current set of
195 * CPU/MMU features.
196 */
197 jump_label_init();
9402c684
BH
198}
199
a28e46f1
ME
200static int __init check_features(void)
201{
202 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
203 "CPU features changed after feature patching!\n");
204 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
205 "MMU features changed after feature patching!\n");
206#ifdef CONFIG_PPC64
207 WARN(saved_firmware_features != powerpc_firmware_features,
208 "Firmware features changed after feature patching!\n");
209#endif
210
211 return 0;
212}
213late_initcall(check_features);
214
362e7701
ME
215#ifdef CONFIG_FTR_FIXUP_SELFTEST
216
217#define check(x) \
218 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
219
220/* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
221static struct fixup_entry fixup;
222
223static long calc_offset(struct fixup_entry *entry, unsigned int *p)
224{
225 return (unsigned long)p - (unsigned long)entry;
226}
227
e51df2c1 228static void test_basic_patching(void)
362e7701
ME
229{
230 extern unsigned int ftr_fixup_test1;
231 extern unsigned int end_ftr_fixup_test1;
232 extern unsigned int ftr_fixup_test1_orig;
233 extern unsigned int ftr_fixup_test1_expected;
234 int size = &end_ftr_fixup_test1 - &ftr_fixup_test1;
235
236 fixup.value = fixup.mask = 8;
237 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test1 + 1);
238 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test1 + 2);
239 fixup.alt_start_off = fixup.alt_end_off = 0;
240
241 /* Sanity check */
242 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
243
244 /* Check we don't patch if the value matches */
245 patch_feature_section(8, &fixup);
246 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
247
248 /* Check we do patch if the value doesn't match */
249 patch_feature_section(0, &fixup);
250 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
251
252 /* Check we do patch if the mask doesn't match */
253 memcpy(&ftr_fixup_test1, &ftr_fixup_test1_orig, size);
254 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
255 patch_feature_section(~8, &fixup);
256 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
257}
258
259static void test_alternative_patching(void)
260{
261 extern unsigned int ftr_fixup_test2;
262 extern unsigned int end_ftr_fixup_test2;
263 extern unsigned int ftr_fixup_test2_orig;
264 extern unsigned int ftr_fixup_test2_alt;
265 extern unsigned int ftr_fixup_test2_expected;
266 int size = &end_ftr_fixup_test2 - &ftr_fixup_test2;
267
268 fixup.value = fixup.mask = 0xF;
269 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test2 + 1);
270 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test2 + 2);
271 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test2_alt);
272 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test2_alt + 1);
273
274 /* Sanity check */
275 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
276
277 /* Check we don't patch if the value matches */
278 patch_feature_section(0xF, &fixup);
279 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
280
281 /* Check we do patch if the value doesn't match */
282 patch_feature_section(0, &fixup);
283 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
284
285 /* Check we do patch if the mask doesn't match */
286 memcpy(&ftr_fixup_test2, &ftr_fixup_test2_orig, size);
287 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
288 patch_feature_section(~0xF, &fixup);
289 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
290}
291
292static void test_alternative_case_too_big(void)
293{
294 extern unsigned int ftr_fixup_test3;
295 extern unsigned int end_ftr_fixup_test3;
296 extern unsigned int ftr_fixup_test3_orig;
297 extern unsigned int ftr_fixup_test3_alt;
298 int size = &end_ftr_fixup_test3 - &ftr_fixup_test3;
299
300 fixup.value = fixup.mask = 0xC;
301 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test3 + 1);
302 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test3 + 2);
303 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test3_alt);
304 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test3_alt + 2);
305
306 /* Sanity check */
307 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
308
309 /* Expect nothing to be patched, and the error returned to us */
310 check(patch_feature_section(0xF, &fixup) == 1);
311 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
312 check(patch_feature_section(0, &fixup) == 1);
313 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
314 check(patch_feature_section(~0xF, &fixup) == 1);
315 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
316}
317
318static void test_alternative_case_too_small(void)
319{
320 extern unsigned int ftr_fixup_test4;
321 extern unsigned int end_ftr_fixup_test4;
322 extern unsigned int ftr_fixup_test4_orig;
323 extern unsigned int ftr_fixup_test4_alt;
324 extern unsigned int ftr_fixup_test4_expected;
325 int size = &end_ftr_fixup_test4 - &ftr_fixup_test4;
326 unsigned long flag;
327
328 /* Check a high-bit flag */
329 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
330 fixup.value = fixup.mask = flag;
331 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test4 + 1);
332 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test4 + 5);
333 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test4_alt);
334 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test4_alt + 2);
335
336 /* Sanity check */
337 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
338
339 /* Check we don't patch if the value matches */
340 patch_feature_section(flag, &fixup);
341 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
342
343 /* Check we do patch if the value doesn't match */
344 patch_feature_section(0, &fixup);
345 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
346
347 /* Check we do patch if the mask doesn't match */
348 memcpy(&ftr_fixup_test4, &ftr_fixup_test4_orig, size);
349 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
350 patch_feature_section(~flag, &fixup);
351 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
352}
353
354static void test_alternative_case_with_branch(void)
355{
356 extern unsigned int ftr_fixup_test5;
357 extern unsigned int end_ftr_fixup_test5;
358 extern unsigned int ftr_fixup_test5_expected;
359 int size = &end_ftr_fixup_test5 - &ftr_fixup_test5;
360
361 check(memcmp(&ftr_fixup_test5, &ftr_fixup_test5_expected, size) == 0);
362}
363
364static void test_alternative_case_with_external_branch(void)
365{
366 extern unsigned int ftr_fixup_test6;
367 extern unsigned int end_ftr_fixup_test6;
368 extern unsigned int ftr_fixup_test6_expected;
369 int size = &end_ftr_fixup_test6 - &ftr_fixup_test6;
370
371 check(memcmp(&ftr_fixup_test6, &ftr_fixup_test6_expected, size) == 0);
372}
373
374static void test_cpu_macros(void)
375{
3880ecb0
SR
376 extern u8 ftr_fixup_test_FTR_macros;
377 extern u8 ftr_fixup_test_FTR_macros_expected;
362e7701
ME
378 unsigned long size = &ftr_fixup_test_FTR_macros_expected -
379 &ftr_fixup_test_FTR_macros;
380
381 /* The fixups have already been done for us during boot */
382 check(memcmp(&ftr_fixup_test_FTR_macros,
383 &ftr_fixup_test_FTR_macros_expected, size) == 0);
384}
385
386static void test_fw_macros(void)
387{
388#ifdef CONFIG_PPC64
3880ecb0
SR
389 extern u8 ftr_fixup_test_FW_FTR_macros;
390 extern u8 ftr_fixup_test_FW_FTR_macros_expected;
362e7701
ME
391 unsigned long size = &ftr_fixup_test_FW_FTR_macros_expected -
392 &ftr_fixup_test_FW_FTR_macros;
393
394 /* The fixups have already been done for us during boot */
395 check(memcmp(&ftr_fixup_test_FW_FTR_macros,
396 &ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
397#endif
398}
399
2d1b2027
KG
400static void test_lwsync_macros(void)
401{
3880ecb0
SR
402 extern u8 lwsync_fixup_test;
403 extern u8 end_lwsync_fixup_test;
404 extern u8 lwsync_fixup_test_expected_LWSYNC;
405 extern u8 lwsync_fixup_test_expected_SYNC;
2d1b2027
KG
406 unsigned long size = &end_lwsync_fixup_test -
407 &lwsync_fixup_test;
408
409 /* The fixups have already been done for us during boot */
410 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
411 check(memcmp(&lwsync_fixup_test,
412 &lwsync_fixup_test_expected_LWSYNC, size) == 0);
413 } else {
414 check(memcmp(&lwsync_fixup_test,
415 &lwsync_fixup_test_expected_SYNC, size) == 0);
416 }
417}
418
362e7701
ME
419static int __init test_feature_fixups(void)
420{
421 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
422
423 test_basic_patching();
424 test_alternative_patching();
425 test_alternative_case_too_big();
426 test_alternative_case_too_small();
427 test_alternative_case_with_branch();
428 test_alternative_case_with_external_branch();
429 test_cpu_macros();
430 test_fw_macros();
2d1b2027 431 test_lwsync_macros();
362e7701
ME
432
433 return 0;
434}
435late_initcall(test_feature_fixups);
436
437#endif /* CONFIG_FTR_FIXUP_SELFTEST */