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 
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>
23 #include <asm/page.h>
24 #include <asm/sections.h>
25 #include <asm/setup.h>
26 #include <asm/security_features.h>
27 #include <asm/firmware.h>
28 
29 struct fixup_entry {
30 	unsigned long	mask;
31 	unsigned long	value;
32 	long		start_off;
33 	long		end_off;
34 	long		alt_start_off;
35 	long		alt_end_off;
36 };
37 
calc_addr(struct fixup_entry * fcur,long offset)38 static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
39 {
40 	/*
41 	 * We store the offset to the code as a negative offset from
42 	 * the start of the alt_entry, to support the VDSO. This
43 	 * routine converts that back into an actual address.
44 	 */
45 	return (unsigned int *)((unsigned long)fcur + offset);
46 }
47 
patch_alt_instruction(unsigned int * src,unsigned int * dest,unsigned int * alt_start,unsigned int * alt_end)48 static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
49 				 unsigned int *alt_start, unsigned int *alt_end)
50 {
51 	unsigned int instr;
52 
53 	instr = *src;
54 
55 	if (instr_is_relative_branch(*src)) {
56 		unsigned int *target = (unsigned int *)branch_target(src);
57 
58 		/* Branch within the section doesn't need translating */
59 		if (target < alt_start || target > alt_end) {
60 			instr = translate_branch(dest, src);
61 			if (!instr)
62 				return 1;
63 		}
64 	}
65 
66 	raw_patch_instruction(dest, instr);
67 
68 	return 0;
69 }
70 
patch_feature_section(unsigned long value,struct fixup_entry * fcur)71 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
72 {
73 	unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
74 
75 	start = calc_addr(fcur, fcur->start_off);
76 	end = calc_addr(fcur, fcur->end_off);
77 	alt_start = calc_addr(fcur, fcur->alt_start_off);
78 	alt_end = calc_addr(fcur, fcur->alt_end_off);
79 
80 	if ((alt_end - alt_start) > (end - start))
81 		return 1;
82 
83 	if ((value & fcur->mask) == fcur->value)
84 		return 0;
85 
86 	src = alt_start;
87 	dest = start;
88 
89 	for (; src < alt_end; src++, dest++) {
90 		if (patch_alt_instruction(src, dest, alt_start, alt_end))
91 			return 1;
92 	}
93 
94 	for (; dest < end; dest++)
95 		raw_patch_instruction(dest, PPC_INST_NOP);
96 
97 	return 0;
98 }
99 
do_feature_fixups(unsigned long value,void * fixup_start,void * fixup_end)100 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
101 {
102 	struct fixup_entry *fcur, *fend;
103 
104 	fcur = fixup_start;
105 	fend = fixup_end;
106 
107 	for (; fcur < fend; fcur++) {
108 		if (patch_feature_section(value, fcur)) {
109 			WARN_ON(1);
110 			printk("Unable to patch feature section at %p - %p" \
111 				" with %p - %p\n",
112 				calc_addr(fcur, fcur->start_off),
113 				calc_addr(fcur, fcur->end_off),
114 				calc_addr(fcur, fcur->alt_start_off),
115 				calc_addr(fcur, fcur->alt_end_off));
116 		}
117 	}
118 }
119 
120 #ifdef CONFIG_PPC_BOOK3S_64
do_stf_entry_barrier_fixups(enum stf_barrier_type types)121 void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
122 {
123 	unsigned int instrs[3], *dest;
124 	long *start, *end;
125 	int i;
126 
127 	start = PTRRELOC(&__start___stf_entry_barrier_fixup),
128 	end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
129 
130 	instrs[0] = 0x60000000; /* nop */
131 	instrs[1] = 0x60000000; /* nop */
132 	instrs[2] = 0x60000000; /* nop */
133 
134 	i = 0;
135 	if (types & STF_BARRIER_FALLBACK) {
136 		instrs[i++] = 0x7d4802a6; /* mflr r10		*/
137 		instrs[i++] = 0x60000000; /* branch patched below */
138 		instrs[i++] = 0x7d4803a6; /* mtlr r10		*/
139 	} else if (types & STF_BARRIER_EIEIO) {
140 		instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
141 	} else if (types & STF_BARRIER_SYNC_ORI) {
142 		instrs[i++] = 0x7c0004ac; /* hwsync		*/
143 		instrs[i++] = 0xe94d0000; /* ld r10,0(r13)	*/
144 		instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
145 	}
146 
147 	for (i = 0; start < end; start++, i++) {
148 		dest = (void *)start + *start;
149 
150 		pr_devel("patching dest %lx\n", (unsigned long)dest);
151 
152 		patch_instruction(dest, instrs[0]);
153 
154 		if (types & STF_BARRIER_FALLBACK)
155 			patch_branch(dest + 1, (unsigned long)&stf_barrier_fallback,
156 				     BRANCH_SET_LINK);
157 		else
158 			patch_instruction(dest + 1, instrs[1]);
159 
160 		patch_instruction(dest + 2, instrs[2]);
161 	}
162 
163 	printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
164 		(types == STF_BARRIER_NONE)                  ? "no" :
165 		(types == STF_BARRIER_FALLBACK)              ? "fallback" :
166 		(types == STF_BARRIER_EIEIO)                 ? "eieio" :
167 		(types == (STF_BARRIER_SYNC_ORI))            ? "hwsync"
168 		                                           : "unknown");
169 }
170 
do_stf_exit_barrier_fixups(enum stf_barrier_type types)171 void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
172 {
173 	unsigned int instrs[6], *dest;
174 	long *start, *end;
175 	int i;
176 
177 	start = PTRRELOC(&__start___stf_exit_barrier_fixup),
178 	end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
179 
180 	instrs[0] = 0x60000000; /* nop */
181 	instrs[1] = 0x60000000; /* nop */
182 	instrs[2] = 0x60000000; /* nop */
183 	instrs[3] = 0x60000000; /* nop */
184 	instrs[4] = 0x60000000; /* nop */
185 	instrs[5] = 0x60000000; /* nop */
186 
187 	i = 0;
188 	if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
189 		if (cpu_has_feature(CPU_FTR_HVMODE)) {
190 			instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
191 			instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
192 		} else {
193 			instrs[i++] = 0x7db243a6; /* mtsprg 2,r13	*/
194 			instrs[i++] = 0x7db142a6; /* mfsprg r13,1    */
195 	        }
196 		instrs[i++] = 0x7c0004ac; /* hwsync		*/
197 		instrs[i++] = 0xe9ad0000; /* ld r13,0(r13)	*/
198 		instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
199 		if (cpu_has_feature(CPU_FTR_HVMODE)) {
200 			instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
201 		} else {
202 			instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
203 		}
204 	} else if (types & STF_BARRIER_EIEIO) {
205 		instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
206 	}
207 
208 	for (i = 0; start < end; start++, i++) {
209 		dest = (void *)start + *start;
210 
211 		pr_devel("patching dest %lx\n", (unsigned long)dest);
212 
213 		patch_instruction(dest, instrs[0]);
214 		patch_instruction(dest + 1, instrs[1]);
215 		patch_instruction(dest + 2, instrs[2]);
216 		patch_instruction(dest + 3, instrs[3]);
217 		patch_instruction(dest + 4, instrs[4]);
218 		patch_instruction(dest + 5, instrs[5]);
219 	}
220 	printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
221 		(types == STF_BARRIER_NONE)                  ? "no" :
222 		(types == STF_BARRIER_FALLBACK)              ? "fallback" :
223 		(types == STF_BARRIER_EIEIO)                 ? "eieio" :
224 		(types == (STF_BARRIER_SYNC_ORI))            ? "hwsync"
225 		                                           : "unknown");
226 }
227 
228 
do_stf_barrier_fixups(enum stf_barrier_type types)229 void do_stf_barrier_fixups(enum stf_barrier_type types)
230 {
231 	do_stf_entry_barrier_fixups(types);
232 	do_stf_exit_barrier_fixups(types);
233 }
234 
do_rfi_flush_fixups(enum l1d_flush_type types)235 void do_rfi_flush_fixups(enum l1d_flush_type types)
236 {
237 	unsigned int instrs[3], *dest;
238 	long *start, *end;
239 	int i;
240 
241 	start = PTRRELOC(&__start___rfi_flush_fixup),
242 	end = PTRRELOC(&__stop___rfi_flush_fixup);
243 
244 	instrs[0] = 0x60000000; /* nop */
245 	instrs[1] = 0x60000000; /* nop */
246 	instrs[2] = 0x60000000; /* nop */
247 
248 	if (types & L1D_FLUSH_FALLBACK)
249 		/* b .+16 to fallback flush */
250 		instrs[0] = 0x48000010;
251 
252 	i = 0;
253 	if (types & L1D_FLUSH_ORI) {
254 		instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
255 		instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
256 	}
257 
258 	if (types & L1D_FLUSH_MTTRIG)
259 		instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
260 
261 	for (i = 0; start < end; start++, i++) {
262 		dest = (void *)start + *start;
263 
264 		pr_devel("patching dest %lx\n", (unsigned long)dest);
265 
266 		patch_instruction(dest, instrs[0]);
267 		patch_instruction(dest + 1, instrs[1]);
268 		patch_instruction(dest + 2, instrs[2]);
269 	}
270 
271 	printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
272 		(types == L1D_FLUSH_NONE)       ? "no" :
273 		(types == L1D_FLUSH_FALLBACK)   ? "fallback displacement" :
274 		(types &  L1D_FLUSH_ORI)        ? (types & L1D_FLUSH_MTTRIG)
275 							? "ori+mttrig type"
276 							: "ori type" :
277 		(types &  L1D_FLUSH_MTTRIG)     ? "mttrig type"
278 						: "unknown");
279 }
280 
do_barrier_nospec_fixups_range(bool enable,void * fixup_start,void * fixup_end)281 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
282 {
283 	unsigned int instr, *dest;
284 	long *start, *end;
285 	int i;
286 
287 	start = fixup_start;
288 	end = fixup_end;
289 
290 	instr = 0x60000000; /* nop */
291 
292 	if (enable) {
293 		pr_info("barrier-nospec: using ORI speculation barrier\n");
294 		instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
295 	}
296 
297 	for (i = 0; start < end; start++, i++) {
298 		dest = (void *)start + *start;
299 
300 		pr_devel("patching dest %lx\n", (unsigned long)dest);
301 		patch_instruction(dest, instr);
302 	}
303 
304 	printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
305 }
306 
307 #endif /* CONFIG_PPC_BOOK3S_64 */
308 
309 #ifdef CONFIG_PPC_BARRIER_NOSPEC
do_barrier_nospec_fixups(bool enable)310 void do_barrier_nospec_fixups(bool enable)
311 {
312 	void *start, *end;
313 
314 	start = PTRRELOC(&__start___barrier_nospec_fixup),
315 	end = PTRRELOC(&__stop___barrier_nospec_fixup);
316 
317 	do_barrier_nospec_fixups_range(enable, start, end);
318 }
319 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
320 
321 #ifdef CONFIG_PPC_FSL_BOOK3E
do_barrier_nospec_fixups_range(bool enable,void * fixup_start,void * fixup_end)322 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
323 {
324 	unsigned int instr[2], *dest;
325 	long *start, *end;
326 	int i;
327 
328 	start = fixup_start;
329 	end = fixup_end;
330 
331 	instr[0] = PPC_INST_NOP;
332 	instr[1] = PPC_INST_NOP;
333 
334 	if (enable) {
335 		pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
336 		instr[0] = PPC_INST_ISYNC;
337 		instr[1] = PPC_INST_SYNC;
338 	}
339 
340 	for (i = 0; start < end; start++, i++) {
341 		dest = (void *)start + *start;
342 
343 		pr_devel("patching dest %lx\n", (unsigned long)dest);
344 		patch_instruction(dest, instr[0]);
345 		patch_instruction(dest + 1, instr[1]);
346 	}
347 
348 	printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
349 }
350 #endif /* CONFIG_PPC_FSL_BOOK3E */
351 
do_lwsync_fixups(unsigned long value,void * fixup_start,void * fixup_end)352 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
353 {
354 	long *start, *end;
355 	unsigned int *dest;
356 
357 	if (!(value & CPU_FTR_LWSYNC))
358 		return ;
359 
360 	start = fixup_start;
361 	end = fixup_end;
362 
363 	for (; start < end; start++) {
364 		dest = (void *)start + *start;
365 		raw_patch_instruction(dest, PPC_INST_LWSYNC);
366 	}
367 }
368 
do_final_fixups(void)369 static void do_final_fixups(void)
370 {
371 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
372 	int *src, *dest;
373 	unsigned long length;
374 
375 	if (PHYSICAL_START == 0)
376 		return;
377 
378 	src = (int *)(KERNELBASE + PHYSICAL_START);
379 	dest = (int *)KERNELBASE;
380 	length = (__end_interrupts - _stext) / sizeof(int);
381 
382 	while (length--) {
383 		raw_patch_instruction(dest, *src);
384 		src++;
385 		dest++;
386 	}
387 #endif
388 }
389 
390 static unsigned long __initdata saved_cpu_features;
391 static unsigned int __initdata saved_mmu_features;
392 #ifdef CONFIG_PPC64
393 static unsigned long __initdata saved_firmware_features;
394 #endif
395 
apply_feature_fixups(void)396 void __init apply_feature_fixups(void)
397 {
398 	struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
399 
400 	*PTRRELOC(&saved_cpu_features) = spec->cpu_features;
401 	*PTRRELOC(&saved_mmu_features) = spec->mmu_features;
402 
403 	/*
404 	 * Apply the CPU-specific and firmware specific fixups to kernel text
405 	 * (nop out sections not relevant to this CPU or this firmware).
406 	 */
407 	do_feature_fixups(spec->cpu_features,
408 			  PTRRELOC(&__start___ftr_fixup),
409 			  PTRRELOC(&__stop___ftr_fixup));
410 
411 	do_feature_fixups(spec->mmu_features,
412 			  PTRRELOC(&__start___mmu_ftr_fixup),
413 			  PTRRELOC(&__stop___mmu_ftr_fixup));
414 
415 	do_lwsync_fixups(spec->cpu_features,
416 			 PTRRELOC(&__start___lwsync_fixup),
417 			 PTRRELOC(&__stop___lwsync_fixup));
418 
419 #ifdef CONFIG_PPC64
420 	saved_firmware_features = powerpc_firmware_features;
421 	do_feature_fixups(powerpc_firmware_features,
422 			  &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
423 #endif
424 	do_final_fixups();
425 }
426 
setup_feature_keys(void)427 void __init setup_feature_keys(void)
428 {
429 	/*
430 	 * Initialise jump label. This causes all the cpu/mmu_has_feature()
431 	 * checks to take on their correct polarity based on the current set of
432 	 * CPU/MMU features.
433 	 */
434 	jump_label_init();
435 	cpu_feature_keys_init();
436 	mmu_feature_keys_init();
437 }
438 
check_features(void)439 static int __init check_features(void)
440 {
441 	WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
442 	     "CPU features changed after feature patching!\n");
443 	WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
444 	     "MMU features changed after feature patching!\n");
445 #ifdef CONFIG_PPC64
446 	WARN(saved_firmware_features != powerpc_firmware_features,
447 	     "Firmware features changed after feature patching!\n");
448 #endif
449 
450 	return 0;
451 }
452 late_initcall(check_features);
453 
454 #ifdef CONFIG_FTR_FIXUP_SELFTEST
455 
456 #define check(x)	\
457 	if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
458 
459 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
460 static struct fixup_entry fixup;
461 
calc_offset(struct fixup_entry * entry,unsigned int * p)462 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
463 {
464 	return (unsigned long)p - (unsigned long)entry;
465 }
466 
test_basic_patching(void)467 static void test_basic_patching(void)
468 {
469 	extern unsigned int ftr_fixup_test1[];
470 	extern unsigned int end_ftr_fixup_test1[];
471 	extern unsigned int ftr_fixup_test1_orig[];
472 	extern unsigned int ftr_fixup_test1_expected[];
473 	int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
474 
475 	fixup.value = fixup.mask = 8;
476 	fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
477 	fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
478 	fixup.alt_start_off = fixup.alt_end_off = 0;
479 
480 	/* Sanity check */
481 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
482 
483 	/* Check we don't patch if the value matches */
484 	patch_feature_section(8, &fixup);
485 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
486 
487 	/* Check we do patch if the value doesn't match */
488 	patch_feature_section(0, &fixup);
489 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
490 
491 	/* Check we do patch if the mask doesn't match */
492 	memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
493 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
494 	patch_feature_section(~8, &fixup);
495 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
496 }
497 
test_alternative_patching(void)498 static void test_alternative_patching(void)
499 {
500 	extern unsigned int ftr_fixup_test2[];
501 	extern unsigned int end_ftr_fixup_test2[];
502 	extern unsigned int ftr_fixup_test2_orig[];
503 	extern unsigned int ftr_fixup_test2_alt[];
504 	extern unsigned int ftr_fixup_test2_expected[];
505 	int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
506 
507 	fixup.value = fixup.mask = 0xF;
508 	fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
509 	fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
510 	fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
511 	fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
512 
513 	/* Sanity check */
514 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
515 
516 	/* Check we don't patch if the value matches */
517 	patch_feature_section(0xF, &fixup);
518 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
519 
520 	/* Check we do patch if the value doesn't match */
521 	patch_feature_section(0, &fixup);
522 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
523 
524 	/* Check we do patch if the mask doesn't match */
525 	memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
526 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
527 	patch_feature_section(~0xF, &fixup);
528 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
529 }
530 
test_alternative_case_too_big(void)531 static void test_alternative_case_too_big(void)
532 {
533 	extern unsigned int ftr_fixup_test3[];
534 	extern unsigned int end_ftr_fixup_test3[];
535 	extern unsigned int ftr_fixup_test3_orig[];
536 	extern unsigned int ftr_fixup_test3_alt[];
537 	int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
538 
539 	fixup.value = fixup.mask = 0xC;
540 	fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
541 	fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
542 	fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
543 	fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
544 
545 	/* Sanity check */
546 	check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
547 
548 	/* Expect nothing to be patched, and the error returned to us */
549 	check(patch_feature_section(0xF, &fixup) == 1);
550 	check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
551 	check(patch_feature_section(0, &fixup) == 1);
552 	check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
553 	check(patch_feature_section(~0xF, &fixup) == 1);
554 	check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
555 }
556 
test_alternative_case_too_small(void)557 static void test_alternative_case_too_small(void)
558 {
559 	extern unsigned int ftr_fixup_test4[];
560 	extern unsigned int end_ftr_fixup_test4[];
561 	extern unsigned int ftr_fixup_test4_orig[];
562 	extern unsigned int ftr_fixup_test4_alt[];
563 	extern unsigned int ftr_fixup_test4_expected[];
564 	int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
565 	unsigned long flag;
566 
567 	/* Check a high-bit flag */
568 	flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
569 	fixup.value = fixup.mask = flag;
570 	fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
571 	fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
572 	fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
573 	fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
574 
575 	/* Sanity check */
576 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
577 
578 	/* Check we don't patch if the value matches */
579 	patch_feature_section(flag, &fixup);
580 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
581 
582 	/* Check we do patch if the value doesn't match */
583 	patch_feature_section(0, &fixup);
584 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
585 
586 	/* Check we do patch if the mask doesn't match */
587 	memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
588 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
589 	patch_feature_section(~flag, &fixup);
590 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
591 }
592 
test_alternative_case_with_branch(void)593 static void test_alternative_case_with_branch(void)
594 {
595 	extern unsigned int ftr_fixup_test5[];
596 	extern unsigned int end_ftr_fixup_test5[];
597 	extern unsigned int ftr_fixup_test5_expected[];
598 	int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
599 
600 	check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
601 }
602 
test_alternative_case_with_external_branch(void)603 static void test_alternative_case_with_external_branch(void)
604 {
605 	extern unsigned int ftr_fixup_test6[];
606 	extern unsigned int end_ftr_fixup_test6[];
607 	extern unsigned int ftr_fixup_test6_expected[];
608 	int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
609 
610 	check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
611 }
612 
test_alternative_case_with_branch_to_end(void)613 static void test_alternative_case_with_branch_to_end(void)
614 {
615 	extern unsigned int ftr_fixup_test7[];
616 	extern unsigned int end_ftr_fixup_test7[];
617 	extern unsigned int ftr_fixup_test7_expected[];
618 	int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
619 
620 	check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
621 }
622 
test_cpu_macros(void)623 static void test_cpu_macros(void)
624 {
625 	extern u8 ftr_fixup_test_FTR_macros[];
626 	extern u8 ftr_fixup_test_FTR_macros_expected[];
627 	unsigned long size = ftr_fixup_test_FTR_macros_expected -
628 			     ftr_fixup_test_FTR_macros;
629 
630 	/* The fixups have already been done for us during boot */
631 	check(memcmp(ftr_fixup_test_FTR_macros,
632 		     ftr_fixup_test_FTR_macros_expected, size) == 0);
633 }
634 
test_fw_macros(void)635 static void test_fw_macros(void)
636 {
637 #ifdef CONFIG_PPC64
638 	extern u8 ftr_fixup_test_FW_FTR_macros[];
639 	extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
640 	unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
641 			     ftr_fixup_test_FW_FTR_macros;
642 
643 	/* The fixups have already been done for us during boot */
644 	check(memcmp(ftr_fixup_test_FW_FTR_macros,
645 		     ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
646 #endif
647 }
648 
test_lwsync_macros(void)649 static void test_lwsync_macros(void)
650 {
651 	extern u8 lwsync_fixup_test[];
652 	extern u8 end_lwsync_fixup_test[];
653 	extern u8 lwsync_fixup_test_expected_LWSYNC[];
654 	extern u8 lwsync_fixup_test_expected_SYNC[];
655 	unsigned long size = end_lwsync_fixup_test -
656 			     lwsync_fixup_test;
657 
658 	/* The fixups have already been done for us during boot */
659 	if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
660 		check(memcmp(lwsync_fixup_test,
661 			     lwsync_fixup_test_expected_LWSYNC, size) == 0);
662 	} else {
663 		check(memcmp(lwsync_fixup_test,
664 			     lwsync_fixup_test_expected_SYNC, size) == 0);
665 	}
666 }
667 
test_feature_fixups(void)668 static int __init test_feature_fixups(void)
669 {
670 	printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
671 
672 	test_basic_patching();
673 	test_alternative_patching();
674 	test_alternative_case_too_big();
675 	test_alternative_case_too_small();
676 	test_alternative_case_with_branch();
677 	test_alternative_case_with_external_branch();
678 	test_alternative_case_with_branch_to_end();
679 	test_cpu_macros();
680 	test_fw_macros();
681 	test_lwsync_macros();
682 
683 	return 0;
684 }
685 late_initcall(test_feature_fixups);
686 
687 #endif /* CONFIG_FTR_FIXUP_SELFTEST */
688