2 * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
4 * Modifications for ppc64:
5 * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
7 * Copyright 2008 Michael Ellerman, IBM Corporation.
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.
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 <asm/cputable.h>
21 #include <asm/code-patching.h>
23 #include <asm/sections.h>
24 #include <asm/setup.h>
25 #include <asm/firmware.h>
36 static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
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.
43 return (unsigned int *)((unsigned long)fcur + offset);
46 static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
47 unsigned int *alt_start, unsigned int *alt_end)
53 if (instr_is_relative_branch(*src)) {
54 unsigned int *target = (unsigned int *)branch_target(src);
56 /* Branch within the section doesn't need translating */
57 if (target < alt_start || target >= alt_end) {
58 instr = translate_branch(dest, src);
64 patch_instruction(dest, instr);
69 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
71 unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
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);
78 if ((alt_end - alt_start) > (end - start))
81 if ((value & fcur->mask) == fcur->value)
87 for (; src < alt_end; src++, dest++) {
88 if (patch_alt_instruction(src, dest, alt_start, alt_end))
92 for (; dest < end; dest++)
93 patch_instruction(dest, PPC_INST_NOP);
98 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
100 struct fixup_entry *fcur, *fend;
105 for (; fcur < fend; fcur++) {
106 if (patch_feature_section(value, fcur)) {
108 printk("Unable to patch feature section at %p - %p" \
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));
118 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
123 if (!(value & CPU_FTR_LWSYNC))
129 for (; start < end; start++) {
130 dest = (void *)start + *start;
131 patch_instruction(dest, PPC_INST_LWSYNC);
135 static void do_final_fixups(void)
137 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
139 unsigned long length;
141 if (PHYSICAL_START == 0)
144 src = (int *)(KERNELBASE + PHYSICAL_START);
145 dest = (int *)KERNELBASE;
146 length = (__end_interrupts - _stext) / sizeof(int);
149 patch_instruction(dest, *src);
156 static unsigned long __initdata saved_cpu_features;
157 static unsigned int __initdata saved_mmu_features;
159 static unsigned long __initdata saved_firmware_features;
162 void __init apply_feature_fixups(void)
164 struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
166 *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
167 *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
170 * Apply the CPU-specific and firmware specific fixups to kernel text
171 * (nop out sections not relevant to this CPU or this firmware).
173 do_feature_fixups(spec->cpu_features,
174 PTRRELOC(&__start___ftr_fixup),
175 PTRRELOC(&__stop___ftr_fixup));
177 do_feature_fixups(spec->mmu_features,
178 PTRRELOC(&__start___mmu_ftr_fixup),
179 PTRRELOC(&__stop___mmu_ftr_fixup));
181 do_lwsync_fixups(spec->cpu_features,
182 PTRRELOC(&__start___lwsync_fixup),
183 PTRRELOC(&__stop___lwsync_fixup));
186 saved_firmware_features = powerpc_firmware_features;
187 do_feature_fixups(powerpc_firmware_features,
188 &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
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
198 cpu_feature_keys_init();
199 mmu_feature_keys_init();
202 static int __init check_features(void)
204 WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
205 "CPU features changed after feature patching!\n");
206 WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
207 "MMU features changed after feature patching!\n");
209 WARN(saved_firmware_features != powerpc_firmware_features,
210 "Firmware features changed after feature patching!\n");
215 late_initcall(check_features);
217 #ifdef CONFIG_FTR_FIXUP_SELFTEST
220 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
222 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
223 static struct fixup_entry fixup;
225 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
227 return (unsigned long)p - (unsigned long)entry;
230 static void test_basic_patching(void)
232 extern unsigned int ftr_fixup_test1;
233 extern unsigned int end_ftr_fixup_test1;
234 extern unsigned int ftr_fixup_test1_orig;
235 extern unsigned int ftr_fixup_test1_expected;
236 int size = &end_ftr_fixup_test1 - &ftr_fixup_test1;
238 fixup.value = fixup.mask = 8;
239 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test1 + 1);
240 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test1 + 2);
241 fixup.alt_start_off = fixup.alt_end_off = 0;
244 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
246 /* Check we don't patch if the value matches */
247 patch_feature_section(8, &fixup);
248 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
250 /* Check we do patch if the value doesn't match */
251 patch_feature_section(0, &fixup);
252 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
254 /* Check we do patch if the mask doesn't match */
255 memcpy(&ftr_fixup_test1, &ftr_fixup_test1_orig, size);
256 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
257 patch_feature_section(~8, &fixup);
258 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
261 static void test_alternative_patching(void)
263 extern unsigned int ftr_fixup_test2;
264 extern unsigned int end_ftr_fixup_test2;
265 extern unsigned int ftr_fixup_test2_orig;
266 extern unsigned int ftr_fixup_test2_alt;
267 extern unsigned int ftr_fixup_test2_expected;
268 int size = &end_ftr_fixup_test2 - &ftr_fixup_test2;
270 fixup.value = fixup.mask = 0xF;
271 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test2 + 1);
272 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test2 + 2);
273 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test2_alt);
274 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test2_alt + 1);
277 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
279 /* Check we don't patch if the value matches */
280 patch_feature_section(0xF, &fixup);
281 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
283 /* Check we do patch if the value doesn't match */
284 patch_feature_section(0, &fixup);
285 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
287 /* Check we do patch if the mask doesn't match */
288 memcpy(&ftr_fixup_test2, &ftr_fixup_test2_orig, size);
289 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
290 patch_feature_section(~0xF, &fixup);
291 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
294 static void test_alternative_case_too_big(void)
296 extern unsigned int ftr_fixup_test3;
297 extern unsigned int end_ftr_fixup_test3;
298 extern unsigned int ftr_fixup_test3_orig;
299 extern unsigned int ftr_fixup_test3_alt;
300 int size = &end_ftr_fixup_test3 - &ftr_fixup_test3;
302 fixup.value = fixup.mask = 0xC;
303 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test3 + 1);
304 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test3 + 2);
305 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test3_alt);
306 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test3_alt + 2);
309 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
311 /* Expect nothing to be patched, and the error returned to us */
312 check(patch_feature_section(0xF, &fixup) == 1);
313 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
314 check(patch_feature_section(0, &fixup) == 1);
315 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
316 check(patch_feature_section(~0xF, &fixup) == 1);
317 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
320 static void test_alternative_case_too_small(void)
322 extern unsigned int ftr_fixup_test4;
323 extern unsigned int end_ftr_fixup_test4;
324 extern unsigned int ftr_fixup_test4_orig;
325 extern unsigned int ftr_fixup_test4_alt;
326 extern unsigned int ftr_fixup_test4_expected;
327 int size = &end_ftr_fixup_test4 - &ftr_fixup_test4;
330 /* Check a high-bit flag */
331 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
332 fixup.value = fixup.mask = flag;
333 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test4 + 1);
334 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test4 + 5);
335 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test4_alt);
336 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test4_alt + 2);
339 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
341 /* Check we don't patch if the value matches */
342 patch_feature_section(flag, &fixup);
343 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
345 /* Check we do patch if the value doesn't match */
346 patch_feature_section(0, &fixup);
347 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
349 /* Check we do patch if the mask doesn't match */
350 memcpy(&ftr_fixup_test4, &ftr_fixup_test4_orig, size);
351 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
352 patch_feature_section(~flag, &fixup);
353 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
356 static void test_alternative_case_with_branch(void)
358 extern unsigned int ftr_fixup_test5;
359 extern unsigned int end_ftr_fixup_test5;
360 extern unsigned int ftr_fixup_test5_expected;
361 int size = &end_ftr_fixup_test5 - &ftr_fixup_test5;
363 check(memcmp(&ftr_fixup_test5, &ftr_fixup_test5_expected, size) == 0);
366 static void test_alternative_case_with_external_branch(void)
368 extern unsigned int ftr_fixup_test6;
369 extern unsigned int end_ftr_fixup_test6;
370 extern unsigned int ftr_fixup_test6_expected;
371 int size = &end_ftr_fixup_test6 - &ftr_fixup_test6;
373 check(memcmp(&ftr_fixup_test6, &ftr_fixup_test6_expected, size) == 0);
376 static void test_cpu_macros(void)
378 extern u8 ftr_fixup_test_FTR_macros;
379 extern u8 ftr_fixup_test_FTR_macros_expected;
380 unsigned long size = &ftr_fixup_test_FTR_macros_expected -
381 &ftr_fixup_test_FTR_macros;
383 /* The fixups have already been done for us during boot */
384 check(memcmp(&ftr_fixup_test_FTR_macros,
385 &ftr_fixup_test_FTR_macros_expected, size) == 0);
388 static void test_fw_macros(void)
391 extern u8 ftr_fixup_test_FW_FTR_macros;
392 extern u8 ftr_fixup_test_FW_FTR_macros_expected;
393 unsigned long size = &ftr_fixup_test_FW_FTR_macros_expected -
394 &ftr_fixup_test_FW_FTR_macros;
396 /* The fixups have already been done for us during boot */
397 check(memcmp(&ftr_fixup_test_FW_FTR_macros,
398 &ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
402 static void test_lwsync_macros(void)
404 extern u8 lwsync_fixup_test;
405 extern u8 end_lwsync_fixup_test;
406 extern u8 lwsync_fixup_test_expected_LWSYNC;
407 extern u8 lwsync_fixup_test_expected_SYNC;
408 unsigned long size = &end_lwsync_fixup_test -
411 /* The fixups have already been done for us during boot */
412 if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
413 check(memcmp(&lwsync_fixup_test,
414 &lwsync_fixup_test_expected_LWSYNC, size) == 0);
416 check(memcmp(&lwsync_fixup_test,
417 &lwsync_fixup_test_expected_SYNC, size) == 0);
421 static int __init test_feature_fixups(void)
423 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
425 test_basic_patching();
426 test_alternative_patching();
427 test_alternative_case_too_big();
428 test_alternative_case_too_small();
429 test_alternative_case_with_branch();
430 test_alternative_case_with_external_branch();
433 test_lwsync_macros();
437 late_initcall(test_feature_fixups);
439 #endif /* CONFIG_FTR_FIXUP_SELFTEST */