powerpc: Add self-tests of the feature fixup code
[linux-2.6/mini2440.git] / arch / powerpc / lib / feature-fixups.c
blob48e1ed89052d63e4743f79fe685c39bc4f5b36c9
1 /*
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/kernel.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <asm/cputable.h>
19 #include <asm/code-patching.h>
22 struct fixup_entry {
23 unsigned long mask;
24 unsigned long value;
25 long start_off;
26 long end_off;
27 long alt_start_off;
28 long alt_end_off;
31 static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
34 * We store the offset to the code as a negative offset from
35 * the start of the alt_entry, to support the VDSO. This
36 * routine converts that back into an actual address.
38 return (unsigned int *)((unsigned long)fcur + offset);
41 static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
42 unsigned int *alt_start, unsigned int *alt_end)
44 unsigned int instr;
46 instr = *src;
48 if (instr_is_relative_branch(*src)) {
49 unsigned int *target = (unsigned int *)branch_target(src);
51 /* Branch within the section doesn't need translating */
52 if (target < alt_start || target >= alt_end) {
53 instr = translate_branch(dest, src);
54 if (!instr)
55 return 1;
59 patch_instruction(dest, instr);
61 return 0;
64 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
66 unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
68 start = calc_addr(fcur, fcur->start_off);
69 end = calc_addr(fcur, fcur->end_off);
70 alt_start = calc_addr(fcur, fcur->alt_start_off);
71 alt_end = calc_addr(fcur, fcur->alt_end_off);
73 if ((alt_end - alt_start) > (end - start))
74 return 1;
76 if ((value & fcur->mask) == fcur->value)
77 return 0;
79 src = alt_start;
80 dest = start;
82 for (; src < alt_end; src++, dest++) {
83 if (patch_alt_instruction(src, dest, alt_start, alt_end))
84 return 1;
87 for (; dest < end; dest++)
88 patch_instruction(dest, PPC_NOP_INSTR);
90 return 0;
93 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
95 struct fixup_entry *fcur, *fend;
97 fcur = fixup_start;
98 fend = fixup_end;
100 for (; fcur < fend; fcur++) {
101 if (patch_feature_section(value, fcur)) {
102 __WARN();
103 printk("Unable to patch feature section at %p - %p" \
104 " with %p - %p\n",
105 calc_addr(fcur, fcur->start_off),
106 calc_addr(fcur, fcur->end_off),
107 calc_addr(fcur, fcur->alt_start_off),
108 calc_addr(fcur, fcur->alt_end_off));
113 #ifdef CONFIG_FTR_FIXUP_SELFTEST
115 #define check(x) \
116 if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
118 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
119 static struct fixup_entry fixup;
121 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
123 return (unsigned long)p - (unsigned long)entry;
126 void test_basic_patching(void)
128 extern unsigned int ftr_fixup_test1;
129 extern unsigned int end_ftr_fixup_test1;
130 extern unsigned int ftr_fixup_test1_orig;
131 extern unsigned int ftr_fixup_test1_expected;
132 int size = &end_ftr_fixup_test1 - &ftr_fixup_test1;
134 fixup.value = fixup.mask = 8;
135 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test1 + 1);
136 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test1 + 2);
137 fixup.alt_start_off = fixup.alt_end_off = 0;
139 /* Sanity check */
140 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
142 /* Check we don't patch if the value matches */
143 patch_feature_section(8, &fixup);
144 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
146 /* Check we do patch if the value doesn't match */
147 patch_feature_section(0, &fixup);
148 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
150 /* Check we do patch if the mask doesn't match */
151 memcpy(&ftr_fixup_test1, &ftr_fixup_test1_orig, size);
152 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
153 patch_feature_section(~8, &fixup);
154 check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
157 static void test_alternative_patching(void)
159 extern unsigned int ftr_fixup_test2;
160 extern unsigned int end_ftr_fixup_test2;
161 extern unsigned int ftr_fixup_test2_orig;
162 extern unsigned int ftr_fixup_test2_alt;
163 extern unsigned int ftr_fixup_test2_expected;
164 int size = &end_ftr_fixup_test2 - &ftr_fixup_test2;
166 fixup.value = fixup.mask = 0xF;
167 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test2 + 1);
168 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test2 + 2);
169 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test2_alt);
170 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test2_alt + 1);
172 /* Sanity check */
173 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
175 /* Check we don't patch if the value matches */
176 patch_feature_section(0xF, &fixup);
177 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
179 /* Check we do patch if the value doesn't match */
180 patch_feature_section(0, &fixup);
181 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
183 /* Check we do patch if the mask doesn't match */
184 memcpy(&ftr_fixup_test2, &ftr_fixup_test2_orig, size);
185 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
186 patch_feature_section(~0xF, &fixup);
187 check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
190 static void test_alternative_case_too_big(void)
192 extern unsigned int ftr_fixup_test3;
193 extern unsigned int end_ftr_fixup_test3;
194 extern unsigned int ftr_fixup_test3_orig;
195 extern unsigned int ftr_fixup_test3_alt;
196 int size = &end_ftr_fixup_test3 - &ftr_fixup_test3;
198 fixup.value = fixup.mask = 0xC;
199 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test3 + 1);
200 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test3 + 2);
201 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test3_alt);
202 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test3_alt + 2);
204 /* Sanity check */
205 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
207 /* Expect nothing to be patched, and the error returned to us */
208 check(patch_feature_section(0xF, &fixup) == 1);
209 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
210 check(patch_feature_section(0, &fixup) == 1);
211 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
212 check(patch_feature_section(~0xF, &fixup) == 1);
213 check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
216 static void test_alternative_case_too_small(void)
218 extern unsigned int ftr_fixup_test4;
219 extern unsigned int end_ftr_fixup_test4;
220 extern unsigned int ftr_fixup_test4_orig;
221 extern unsigned int ftr_fixup_test4_alt;
222 extern unsigned int ftr_fixup_test4_expected;
223 int size = &end_ftr_fixup_test4 - &ftr_fixup_test4;
224 unsigned long flag;
226 /* Check a high-bit flag */
227 flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
228 fixup.value = fixup.mask = flag;
229 fixup.start_off = calc_offset(&fixup, &ftr_fixup_test4 + 1);
230 fixup.end_off = calc_offset(&fixup, &ftr_fixup_test4 + 5);
231 fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test4_alt);
232 fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test4_alt + 2);
234 /* Sanity check */
235 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
237 /* Check we don't patch if the value matches */
238 patch_feature_section(flag, &fixup);
239 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
241 /* Check we do patch if the value doesn't match */
242 patch_feature_section(0, &fixup);
243 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
245 /* Check we do patch if the mask doesn't match */
246 memcpy(&ftr_fixup_test4, &ftr_fixup_test4_orig, size);
247 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
248 patch_feature_section(~flag, &fixup);
249 check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
252 static void test_alternative_case_with_branch(void)
254 extern unsigned int ftr_fixup_test5;
255 extern unsigned int end_ftr_fixup_test5;
256 extern unsigned int ftr_fixup_test5_expected;
257 int size = &end_ftr_fixup_test5 - &ftr_fixup_test5;
259 check(memcmp(&ftr_fixup_test5, &ftr_fixup_test5_expected, size) == 0);
262 static void test_alternative_case_with_external_branch(void)
264 extern unsigned int ftr_fixup_test6;
265 extern unsigned int end_ftr_fixup_test6;
266 extern unsigned int ftr_fixup_test6_expected;
267 int size = &end_ftr_fixup_test6 - &ftr_fixup_test6;
269 check(memcmp(&ftr_fixup_test6, &ftr_fixup_test6_expected, size) == 0);
272 static void test_cpu_macros(void)
274 extern void ftr_fixup_test_FTR_macros;
275 extern void ftr_fixup_test_FTR_macros_expected;
276 unsigned long size = &ftr_fixup_test_FTR_macros_expected -
277 &ftr_fixup_test_FTR_macros;
279 /* The fixups have already been done for us during boot */
280 check(memcmp(&ftr_fixup_test_FTR_macros,
281 &ftr_fixup_test_FTR_macros_expected, size) == 0);
284 static void test_fw_macros(void)
286 #ifdef CONFIG_PPC64
287 extern void ftr_fixup_test_FW_FTR_macros;
288 extern void ftr_fixup_test_FW_FTR_macros_expected;
289 unsigned long size = &ftr_fixup_test_FW_FTR_macros_expected -
290 &ftr_fixup_test_FW_FTR_macros;
292 /* The fixups have already been done for us during boot */
293 check(memcmp(&ftr_fixup_test_FW_FTR_macros,
294 &ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
295 #endif
298 static int __init test_feature_fixups(void)
300 printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
302 test_basic_patching();
303 test_alternative_patching();
304 test_alternative_case_too_big();
305 test_alternative_case_too_small();
306 test_alternative_case_with_branch();
307 test_alternative_case_with_external_branch();
308 test_cpu_macros();
309 test_fw_macros();
311 return 0;
313 late_initcall(test_feature_fixups);
315 #endif /* CONFIG_FTR_FIXUP_SELFTEST */