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 <linux/sched/mm.h>
21 #include <asm/cputable.h>
22 #include <asm/code-patching.h>
24 #include <asm/sections.h>
25 #include <asm/setup.h>
26 #include <asm/firmware.h>
37 static unsigned int *calc_addr(struct fixup_entry
*fcur
, long offset
)
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.
44 return (unsigned int *)((unsigned long)fcur
+ offset
);
47 static int patch_alt_instruction(unsigned int *src
, unsigned int *dest
,
48 unsigned int *alt_start
, unsigned int *alt_end
)
54 if (instr_is_relative_branch(*src
)) {
55 unsigned int *target
= (unsigned int *)branch_target(src
);
57 /* Branch within the section doesn't need translating */
58 if (target
< alt_start
|| target
>= alt_end
) {
59 instr
= translate_branch(dest
, src
);
65 patch_instruction(dest
, instr
);
70 static int patch_feature_section(unsigned long value
, struct fixup_entry
*fcur
)
72 unsigned int *start
, *end
, *alt_start
, *alt_end
, *src
, *dest
;
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
);
79 if ((alt_end
- alt_start
) > (end
- start
))
82 if ((value
& fcur
->mask
) == fcur
->value
)
88 for (; src
< alt_end
; src
++, dest
++) {
89 if (patch_alt_instruction(src
, dest
, alt_start
, alt_end
))
93 for (; dest
< end
; dest
++)
94 patch_instruction(dest
, PPC_INST_NOP
);
99 void do_feature_fixups(unsigned long value
, void *fixup_start
, void *fixup_end
)
101 struct fixup_entry
*fcur
, *fend
;
106 for (; fcur
< fend
; fcur
++) {
107 if (patch_feature_section(value
, fcur
)) {
109 printk("Unable to patch feature section at %p - %p" \
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
));
119 void do_lwsync_fixups(unsigned long value
, void *fixup_start
, void *fixup_end
)
124 if (!(value
& CPU_FTR_LWSYNC
))
130 for (; start
< end
; start
++) {
131 dest
= (void *)start
+ *start
;
132 patch_instruction(dest
, PPC_INST_LWSYNC
);
136 static void do_final_fixups(void)
138 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
140 unsigned long length
;
142 if (PHYSICAL_START
== 0)
145 src
= (int *)(KERNELBASE
+ PHYSICAL_START
);
146 dest
= (int *)KERNELBASE
;
147 length
= (__end_interrupts
- _stext
) / sizeof(int);
150 patch_instruction(dest
, *src
);
157 static unsigned long __initdata saved_cpu_features
;
158 static unsigned int __initdata saved_mmu_features
;
160 static unsigned long __initdata saved_firmware_features
;
163 void __init
apply_feature_fixups(void)
165 struct cpu_spec
*spec
= PTRRELOC(*PTRRELOC(&cur_cpu_spec
));
167 *PTRRELOC(&saved_cpu_features
) = spec
->cpu_features
;
168 *PTRRELOC(&saved_mmu_features
) = spec
->mmu_features
;
171 * Apply the CPU-specific and firmware specific fixups to kernel text
172 * (nop out sections not relevant to this CPU or this firmware).
174 do_feature_fixups(spec
->cpu_features
,
175 PTRRELOC(&__start___ftr_fixup
),
176 PTRRELOC(&__stop___ftr_fixup
));
178 do_feature_fixups(spec
->mmu_features
,
179 PTRRELOC(&__start___mmu_ftr_fixup
),
180 PTRRELOC(&__stop___mmu_ftr_fixup
));
182 do_lwsync_fixups(spec
->cpu_features
,
183 PTRRELOC(&__start___lwsync_fixup
),
184 PTRRELOC(&__stop___lwsync_fixup
));
187 saved_firmware_features
= powerpc_firmware_features
;
188 do_feature_fixups(powerpc_firmware_features
,
189 &__start___fw_ftr_fixup
, &__stop___fw_ftr_fixup
);
194 void __init
setup_feature_keys(void)
197 * Initialise jump label. This causes all the cpu/mmu_has_feature()
198 * checks to take on their correct polarity based on the current set of
202 cpu_feature_keys_init();
203 mmu_feature_keys_init();
206 static int __init
check_features(void)
208 WARN(saved_cpu_features
!= cur_cpu_spec
->cpu_features
,
209 "CPU features changed after feature patching!\n");
210 WARN(saved_mmu_features
!= cur_cpu_spec
->mmu_features
,
211 "MMU features changed after feature patching!\n");
213 WARN(saved_firmware_features
!= powerpc_firmware_features
,
214 "Firmware features changed after feature patching!\n");
219 late_initcall(check_features
);
221 #ifdef CONFIG_FTR_FIXUP_SELFTEST
224 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
226 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
227 static struct fixup_entry fixup
;
229 static long calc_offset(struct fixup_entry
*entry
, unsigned int *p
)
231 return (unsigned long)p
- (unsigned long)entry
;
234 static void test_basic_patching(void)
236 extern unsigned int ftr_fixup_test1
;
237 extern unsigned int end_ftr_fixup_test1
;
238 extern unsigned int ftr_fixup_test1_orig
;
239 extern unsigned int ftr_fixup_test1_expected
;
240 int size
= &end_ftr_fixup_test1
- &ftr_fixup_test1
;
242 fixup
.value
= fixup
.mask
= 8;
243 fixup
.start_off
= calc_offset(&fixup
, &ftr_fixup_test1
+ 1);
244 fixup
.end_off
= calc_offset(&fixup
, &ftr_fixup_test1
+ 2);
245 fixup
.alt_start_off
= fixup
.alt_end_off
= 0;
248 check(memcmp(&ftr_fixup_test1
, &ftr_fixup_test1_orig
, size
) == 0);
250 /* Check we don't patch if the value matches */
251 patch_feature_section(8, &fixup
);
252 check(memcmp(&ftr_fixup_test1
, &ftr_fixup_test1_orig
, size
) == 0);
254 /* Check we do patch if the value doesn't match */
255 patch_feature_section(0, &fixup
);
256 check(memcmp(&ftr_fixup_test1
, &ftr_fixup_test1_expected
, size
) == 0);
258 /* Check we do patch if the mask doesn't match */
259 memcpy(&ftr_fixup_test1
, &ftr_fixup_test1_orig
, size
);
260 check(memcmp(&ftr_fixup_test1
, &ftr_fixup_test1_orig
, size
) == 0);
261 patch_feature_section(~8, &fixup
);
262 check(memcmp(&ftr_fixup_test1
, &ftr_fixup_test1_expected
, size
) == 0);
265 static void test_alternative_patching(void)
267 extern unsigned int ftr_fixup_test2
;
268 extern unsigned int end_ftr_fixup_test2
;
269 extern unsigned int ftr_fixup_test2_orig
;
270 extern unsigned int ftr_fixup_test2_alt
;
271 extern unsigned int ftr_fixup_test2_expected
;
272 int size
= &end_ftr_fixup_test2
- &ftr_fixup_test2
;
274 fixup
.value
= fixup
.mask
= 0xF;
275 fixup
.start_off
= calc_offset(&fixup
, &ftr_fixup_test2
+ 1);
276 fixup
.end_off
= calc_offset(&fixup
, &ftr_fixup_test2
+ 2);
277 fixup
.alt_start_off
= calc_offset(&fixup
, &ftr_fixup_test2_alt
);
278 fixup
.alt_end_off
= calc_offset(&fixup
, &ftr_fixup_test2_alt
+ 1);
281 check(memcmp(&ftr_fixup_test2
, &ftr_fixup_test2_orig
, size
) == 0);
283 /* Check we don't patch if the value matches */
284 patch_feature_section(0xF, &fixup
);
285 check(memcmp(&ftr_fixup_test2
, &ftr_fixup_test2_orig
, size
) == 0);
287 /* Check we do patch if the value doesn't match */
288 patch_feature_section(0, &fixup
);
289 check(memcmp(&ftr_fixup_test2
, &ftr_fixup_test2_expected
, size
) == 0);
291 /* Check we do patch if the mask doesn't match */
292 memcpy(&ftr_fixup_test2
, &ftr_fixup_test2_orig
, size
);
293 check(memcmp(&ftr_fixup_test2
, &ftr_fixup_test2_orig
, size
) == 0);
294 patch_feature_section(~0xF, &fixup
);
295 check(memcmp(&ftr_fixup_test2
, &ftr_fixup_test2_expected
, size
) == 0);
298 static void test_alternative_case_too_big(void)
300 extern unsigned int ftr_fixup_test3
;
301 extern unsigned int end_ftr_fixup_test3
;
302 extern unsigned int ftr_fixup_test3_orig
;
303 extern unsigned int ftr_fixup_test3_alt
;
304 int size
= &end_ftr_fixup_test3
- &ftr_fixup_test3
;
306 fixup
.value
= fixup
.mask
= 0xC;
307 fixup
.start_off
= calc_offset(&fixup
, &ftr_fixup_test3
+ 1);
308 fixup
.end_off
= calc_offset(&fixup
, &ftr_fixup_test3
+ 2);
309 fixup
.alt_start_off
= calc_offset(&fixup
, &ftr_fixup_test3_alt
);
310 fixup
.alt_end_off
= calc_offset(&fixup
, &ftr_fixup_test3_alt
+ 2);
313 check(memcmp(&ftr_fixup_test3
, &ftr_fixup_test3_orig
, size
) == 0);
315 /* Expect nothing to be patched, and the error returned to us */
316 check(patch_feature_section(0xF, &fixup
) == 1);
317 check(memcmp(&ftr_fixup_test3
, &ftr_fixup_test3_orig
, size
) == 0);
318 check(patch_feature_section(0, &fixup
) == 1);
319 check(memcmp(&ftr_fixup_test3
, &ftr_fixup_test3_orig
, size
) == 0);
320 check(patch_feature_section(~0xF, &fixup
) == 1);
321 check(memcmp(&ftr_fixup_test3
, &ftr_fixup_test3_orig
, size
) == 0);
324 static void test_alternative_case_too_small(void)
326 extern unsigned int ftr_fixup_test4
;
327 extern unsigned int end_ftr_fixup_test4
;
328 extern unsigned int ftr_fixup_test4_orig
;
329 extern unsigned int ftr_fixup_test4_alt
;
330 extern unsigned int ftr_fixup_test4_expected
;
331 int size
= &end_ftr_fixup_test4
- &ftr_fixup_test4
;
334 /* Check a high-bit flag */
335 flag
= 1UL << ((sizeof(unsigned long) - 1) * 8);
336 fixup
.value
= fixup
.mask
= flag
;
337 fixup
.start_off
= calc_offset(&fixup
, &ftr_fixup_test4
+ 1);
338 fixup
.end_off
= calc_offset(&fixup
, &ftr_fixup_test4
+ 5);
339 fixup
.alt_start_off
= calc_offset(&fixup
, &ftr_fixup_test4_alt
);
340 fixup
.alt_end_off
= calc_offset(&fixup
, &ftr_fixup_test4_alt
+ 2);
343 check(memcmp(&ftr_fixup_test4
, &ftr_fixup_test4_orig
, size
) == 0);
345 /* Check we don't patch if the value matches */
346 patch_feature_section(flag
, &fixup
);
347 check(memcmp(&ftr_fixup_test4
, &ftr_fixup_test4_orig
, size
) == 0);
349 /* Check we do patch if the value doesn't match */
350 patch_feature_section(0, &fixup
);
351 check(memcmp(&ftr_fixup_test4
, &ftr_fixup_test4_expected
, size
) == 0);
353 /* Check we do patch if the mask doesn't match */
354 memcpy(&ftr_fixup_test4
, &ftr_fixup_test4_orig
, size
);
355 check(memcmp(&ftr_fixup_test4
, &ftr_fixup_test4_orig
, size
) == 0);
356 patch_feature_section(~flag
, &fixup
);
357 check(memcmp(&ftr_fixup_test4
, &ftr_fixup_test4_expected
, size
) == 0);
360 static void test_alternative_case_with_branch(void)
362 extern unsigned int ftr_fixup_test5
;
363 extern unsigned int end_ftr_fixup_test5
;
364 extern unsigned int ftr_fixup_test5_expected
;
365 int size
= &end_ftr_fixup_test5
- &ftr_fixup_test5
;
367 check(memcmp(&ftr_fixup_test5
, &ftr_fixup_test5_expected
, size
) == 0);
370 static void test_alternative_case_with_external_branch(void)
372 extern unsigned int ftr_fixup_test6
;
373 extern unsigned int end_ftr_fixup_test6
;
374 extern unsigned int ftr_fixup_test6_expected
;
375 int size
= &end_ftr_fixup_test6
- &ftr_fixup_test6
;
377 check(memcmp(&ftr_fixup_test6
, &ftr_fixup_test6_expected
, size
) == 0);
380 static void test_cpu_macros(void)
382 extern u8 ftr_fixup_test_FTR_macros
;
383 extern u8 ftr_fixup_test_FTR_macros_expected
;
384 unsigned long size
= &ftr_fixup_test_FTR_macros_expected
-
385 &ftr_fixup_test_FTR_macros
;
387 /* The fixups have already been done for us during boot */
388 check(memcmp(&ftr_fixup_test_FTR_macros
,
389 &ftr_fixup_test_FTR_macros_expected
, size
) == 0);
392 static void test_fw_macros(void)
395 extern u8 ftr_fixup_test_FW_FTR_macros
;
396 extern u8 ftr_fixup_test_FW_FTR_macros_expected
;
397 unsigned long size
= &ftr_fixup_test_FW_FTR_macros_expected
-
398 &ftr_fixup_test_FW_FTR_macros
;
400 /* The fixups have already been done for us during boot */
401 check(memcmp(&ftr_fixup_test_FW_FTR_macros
,
402 &ftr_fixup_test_FW_FTR_macros_expected
, size
) == 0);
406 static void test_lwsync_macros(void)
408 extern u8 lwsync_fixup_test
;
409 extern u8 end_lwsync_fixup_test
;
410 extern u8 lwsync_fixup_test_expected_LWSYNC
;
411 extern u8 lwsync_fixup_test_expected_SYNC
;
412 unsigned long size
= &end_lwsync_fixup_test
-
415 /* The fixups have already been done for us during boot */
416 if (cur_cpu_spec
->cpu_features
& CPU_FTR_LWSYNC
) {
417 check(memcmp(&lwsync_fixup_test
,
418 &lwsync_fixup_test_expected_LWSYNC
, size
) == 0);
420 check(memcmp(&lwsync_fixup_test
,
421 &lwsync_fixup_test_expected_SYNC
, size
) == 0);
425 static int __init
test_feature_fixups(void)
427 printk(KERN_DEBUG
"Running feature fixup self-tests ...\n");
429 test_basic_patching();
430 test_alternative_patching();
431 test_alternative_case_too_big();
432 test_alternative_case_too_small();
433 test_alternative_case_with_branch();
434 test_alternative_case_with_external_branch();
437 test_lwsync_macros();
441 late_initcall(test_feature_fixups
);
443 #endif /* CONFIG_FTR_FIXUP_SELFTEST */