1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
4  *
5  *  Modifications for ppc64:
6  *      Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
7  *
8  *  Copyright 2008 Michael Ellerman, IBM Corporation.
9  */
10 
11 #include <linux/types.h>
12 #include <linux/jump_label.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/init.h>
16 #include <linux/sched/mm.h>
17 #include <linux/stop_machine.h>
18 #include <asm/cputable.h>
19 #include <asm/code-patching.h>
20 #include <asm/page.h>
21 #include <asm/sections.h>
22 #include <asm/setup.h>
23 #include <asm/security_features.h>
24 #include <asm/firmware.h>
25 #include <asm/inst.h>
26 
27 struct fixup_entry {
28 	unsigned long	mask;
29 	unsigned long	value;
30 	long		start_off;
31 	long		end_off;
32 	long		alt_start_off;
33 	long		alt_end_off;
34 };
35 
36 static struct ppc_inst *calc_addr(struct fixup_entry *fcur, long offset)
37 {
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 (struct ppc_inst *)((unsigned long)fcur + offset);
44 }
45 
46 static int patch_alt_instruction(struct ppc_inst *src, struct ppc_inst *dest,
47 				 struct ppc_inst *alt_start, struct ppc_inst *alt_end)
48 {
49 	int err;
50 	struct ppc_inst instr;
51 
52 	instr = ppc_inst_read(src);
53 
54 	if (instr_is_relative_branch(*src)) {
55 		struct ppc_inst *target = (struct ppc_inst *)branch_target(src);
56 
57 		/* Branch within the section doesn't need translating */
58 		if (target < alt_start || target > alt_end) {
59 			err = translate_branch(&instr, dest, src);
60 			if (err)
61 				return 1;
62 		}
63 	}
64 
65 	raw_patch_instruction(dest, instr);
66 
67 	return 0;
68 }
69 
70 static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
71 {
72 	struct ppc_inst *start, *end, *alt_start, *alt_end, *src, *dest, nop;
73 
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);
78 
79 	if ((alt_end - alt_start) > (end - start))
80 		return 1;
81 
82 	if ((value & fcur->mask) == fcur->value)
83 		return 0;
84 
85 	src = alt_start;
86 	dest = start;
87 
88 	for (; src < alt_end; src = ppc_inst_next(src, src),
89 			      dest = ppc_inst_next(dest, dest)) {
90 		if (patch_alt_instruction(src, dest, alt_start, alt_end))
91 			return 1;
92 	}
93 
94 	nop = ppc_inst(PPC_INST_NOP);
95 	for (; dest < end; dest = ppc_inst_next(dest, &nop))
96 		raw_patch_instruction(dest, nop);
97 
98 	return 0;
99 }
100 
101 void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
102 {
103 	struct fixup_entry *fcur, *fend;
104 
105 	fcur = fixup_start;
106 	fend = fixup_end;
107 
108 	for (; fcur < fend; fcur++) {
109 		if (patch_feature_section(value, fcur)) {
110 			WARN_ON(1);
111 			printk("Unable to patch feature section at %p - %p" \
112 				" with %p - %p\n",
113 				calc_addr(fcur, fcur->start_off),
114 				calc_addr(fcur, fcur->end_off),
115 				calc_addr(fcur, fcur->alt_start_off),
116 				calc_addr(fcur, fcur->alt_end_off));
117 		}
118 	}
119 }
120 
121 #ifdef CONFIG_PPC_BOOK3S_64
122 static void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
123 {
124 	unsigned int instrs[3], *dest;
125 	long *start, *end;
126 	int i;
127 
128 	start = PTRRELOC(&__start___stf_entry_barrier_fixup);
129 	end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
130 
131 	instrs[0] = 0x60000000; /* nop */
132 	instrs[1] = 0x60000000; /* nop */
133 	instrs[2] = 0x60000000; /* nop */
134 
135 	i = 0;
136 	if (types & STF_BARRIER_FALLBACK) {
137 		instrs[i++] = 0x7d4802a6; /* mflr r10		*/
138 		instrs[i++] = 0x60000000; /* branch patched below */
139 		instrs[i++] = 0x7d4803a6; /* mtlr r10		*/
140 	} else if (types & STF_BARRIER_EIEIO) {
141 		instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
142 	} else if (types & STF_BARRIER_SYNC_ORI) {
143 		instrs[i++] = 0x7c0004ac; /* hwsync		*/
144 		instrs[i++] = 0xe94d0000; /* ld r10,0(r13)	*/
145 		instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
146 	}
147 
148 	for (i = 0; start < end; start++, i++) {
149 		dest = (void *)start + *start;
150 
151 		pr_devel("patching dest %lx\n", (unsigned long)dest);
152 
153 		// See comment in do_entry_flush_fixups() RE order of patching
154 		if (types & STF_BARRIER_FALLBACK) {
155 			patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
156 			patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
157 			patch_branch((struct ppc_inst *)(dest + 1),
158 				     (unsigned long)&stf_barrier_fallback, BRANCH_SET_LINK);
159 		} else {
160 			patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
161 			patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
162 			patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
163 		}
164 	}
165 
166 	printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
167 		(types == STF_BARRIER_NONE)                  ? "no" :
168 		(types == STF_BARRIER_FALLBACK)              ? "fallback" :
169 		(types == STF_BARRIER_EIEIO)                 ? "eieio" :
170 		(types == (STF_BARRIER_SYNC_ORI))            ? "hwsync"
171 		                                           : "unknown");
172 }
173 
174 static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
175 {
176 	unsigned int instrs[6], *dest;
177 	long *start, *end;
178 	int i;
179 
180 	start = PTRRELOC(&__start___stf_exit_barrier_fixup);
181 	end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
182 
183 	instrs[0] = 0x60000000; /* nop */
184 	instrs[1] = 0x60000000; /* nop */
185 	instrs[2] = 0x60000000; /* nop */
186 	instrs[3] = 0x60000000; /* nop */
187 	instrs[4] = 0x60000000; /* nop */
188 	instrs[5] = 0x60000000; /* nop */
189 
190 	i = 0;
191 	if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
192 		if (cpu_has_feature(CPU_FTR_HVMODE)) {
193 			instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
194 			instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
195 		} else {
196 			instrs[i++] = 0x7db243a6; /* mtsprg 2,r13	*/
197 			instrs[i++] = 0x7db142a6; /* mfsprg r13,1    */
198 	        }
199 		instrs[i++] = 0x7c0004ac; /* hwsync		*/
200 		instrs[i++] = 0xe9ad0000; /* ld r13,0(r13)	*/
201 		instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
202 		if (cpu_has_feature(CPU_FTR_HVMODE)) {
203 			instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
204 		} else {
205 			instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
206 		}
207 	} else if (types & STF_BARRIER_EIEIO) {
208 		instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
209 	}
210 
211 	for (i = 0; start < end; start++, i++) {
212 		dest = (void *)start + *start;
213 
214 		pr_devel("patching dest %lx\n", (unsigned long)dest);
215 
216 		patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
217 		patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
218 		patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
219 		patch_instruction((struct ppc_inst *)(dest + 3), ppc_inst(instrs[3]));
220 		patch_instruction((struct ppc_inst *)(dest + 4), ppc_inst(instrs[4]));
221 		patch_instruction((struct ppc_inst *)(dest + 5), ppc_inst(instrs[5]));
222 	}
223 	printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
224 		(types == STF_BARRIER_NONE)                  ? "no" :
225 		(types == STF_BARRIER_FALLBACK)              ? "fallback" :
226 		(types == STF_BARRIER_EIEIO)                 ? "eieio" :
227 		(types == (STF_BARRIER_SYNC_ORI))            ? "hwsync"
228 		                                           : "unknown");
229 }
230 
231 static int __do_stf_barrier_fixups(void *data)
232 {
233 	enum stf_barrier_type *types = data;
234 
235 	do_stf_entry_barrier_fixups(*types);
236 	do_stf_exit_barrier_fixups(*types);
237 
238 	return 0;
239 }
240 
241 void do_stf_barrier_fixups(enum stf_barrier_type types)
242 {
243 	/*
244 	 * The call to the fallback entry flush, and the fallback/sync-ori exit
245 	 * flush can not be safely patched in/out while other CPUs are executing
246 	 * them. So call __do_stf_barrier_fixups() on one CPU while all other CPUs
247 	 * spin in the stop machine core with interrupts hard disabled.
248 	 */
249 	stop_machine(__do_stf_barrier_fixups, &types, NULL);
250 }
251 
252 void do_uaccess_flush_fixups(enum l1d_flush_type types)
253 {
254 	unsigned int instrs[4], *dest;
255 	long *start, *end;
256 	int i;
257 
258 	start = PTRRELOC(&__start___uaccess_flush_fixup);
259 	end = PTRRELOC(&__stop___uaccess_flush_fixup);
260 
261 	instrs[0] = 0x60000000; /* nop */
262 	instrs[1] = 0x60000000; /* nop */
263 	instrs[2] = 0x60000000; /* nop */
264 	instrs[3] = 0x4e800020; /* blr */
265 
266 	i = 0;
267 	if (types == L1D_FLUSH_FALLBACK) {
268 		instrs[3] = 0x60000000; /* nop */
269 		/* fallthrough to fallback flush */
270 	}
271 
272 	if (types & L1D_FLUSH_ORI) {
273 		instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
274 		instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
275 	}
276 
277 	if (types & L1D_FLUSH_MTTRIG)
278 		instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
279 
280 	for (i = 0; start < end; start++, i++) {
281 		dest = (void *)start + *start;
282 
283 		pr_devel("patching dest %lx\n", (unsigned long)dest);
284 
285 		patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
286 
287 		patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
288 		patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
289 		patch_instruction((struct ppc_inst *)(dest + 3), ppc_inst(instrs[3]));
290 	}
291 
292 	printk(KERN_DEBUG "uaccess-flush: patched %d locations (%s flush)\n", i,
293 		(types == L1D_FLUSH_NONE)       ? "no" :
294 		(types == L1D_FLUSH_FALLBACK)   ? "fallback displacement" :
295 		(types &  L1D_FLUSH_ORI)        ? (types & L1D_FLUSH_MTTRIG)
296 							? "ori+mttrig type"
297 							: "ori type" :
298 		(types &  L1D_FLUSH_MTTRIG)     ? "mttrig type"
299 						: "unknown");
300 }
301 
302 static int __do_entry_flush_fixups(void *data)
303 {
304 	enum l1d_flush_type types = *(enum l1d_flush_type *)data;
305 	unsigned int instrs[3], *dest;
306 	long *start, *end;
307 	int i;
308 
309 	instrs[0] = 0x60000000; /* nop */
310 	instrs[1] = 0x60000000; /* nop */
311 	instrs[2] = 0x60000000; /* nop */
312 
313 	i = 0;
314 	if (types == L1D_FLUSH_FALLBACK) {
315 		instrs[i++] = 0x7d4802a6; /* mflr r10		*/
316 		instrs[i++] = 0x60000000; /* branch patched below */
317 		instrs[i++] = 0x7d4803a6; /* mtlr r10		*/
318 	}
319 
320 	if (types & L1D_FLUSH_ORI) {
321 		instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
322 		instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
323 	}
324 
325 	if (types & L1D_FLUSH_MTTRIG)
326 		instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
327 
328 	/*
329 	 * If we're patching in or out the fallback flush we need to be careful about the
330 	 * order in which we patch instructions. That's because it's possible we could
331 	 * take a page fault after patching one instruction, so the sequence of
332 	 * instructions must be safe even in a half patched state.
333 	 *
334 	 * To make that work, when patching in the fallback flush we patch in this order:
335 	 *  - the mflr		(dest)
336 	 *  - the mtlr		(dest + 2)
337 	 *  - the branch	(dest + 1)
338 	 *
339 	 * That ensures the sequence is safe to execute at any point. In contrast if we
340 	 * patch the mtlr last, it's possible we could return from the branch and not
341 	 * restore LR, leading to a crash later.
342 	 *
343 	 * When patching out the fallback flush (either with nops or another flush type),
344 	 * we patch in this order:
345 	 *  - the branch	(dest + 1)
346 	 *  - the mtlr		(dest + 2)
347 	 *  - the mflr		(dest)
348 	 *
349 	 * Note we are protected by stop_machine() from other CPUs executing the code in a
350 	 * semi-patched state.
351 	 */
352 
353 	start = PTRRELOC(&__start___entry_flush_fixup);
354 	end = PTRRELOC(&__stop___entry_flush_fixup);
355 	for (i = 0; start < end; start++, i++) {
356 		dest = (void *)start + *start;
357 
358 		pr_devel("patching dest %lx\n", (unsigned long)dest);
359 
360 		if (types == L1D_FLUSH_FALLBACK) {
361 			patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
362 			patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
363 			patch_branch((struct ppc_inst *)(dest + 1),
364 				     (unsigned long)&entry_flush_fallback, BRANCH_SET_LINK);
365 		} else {
366 			patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
367 			patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
368 			patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
369 		}
370 	}
371 
372 	start = PTRRELOC(&__start___scv_entry_flush_fixup);
373 	end = PTRRELOC(&__stop___scv_entry_flush_fixup);
374 	for (; start < end; start++, i++) {
375 		dest = (void *)start + *start;
376 
377 		pr_devel("patching dest %lx\n", (unsigned long)dest);
378 
379 		if (types == L1D_FLUSH_FALLBACK) {
380 			patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
381 			patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
382 			patch_branch((struct ppc_inst *)(dest + 1),
383 				     (unsigned long)&scv_entry_flush_fallback, BRANCH_SET_LINK);
384 		} else {
385 			patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
386 			patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
387 			patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
388 		}
389 	}
390 
391 
392 	printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
393 		(types == L1D_FLUSH_NONE)       ? "no" :
394 		(types == L1D_FLUSH_FALLBACK)   ? "fallback displacement" :
395 		(types &  L1D_FLUSH_ORI)        ? (types & L1D_FLUSH_MTTRIG)
396 							? "ori+mttrig type"
397 							: "ori type" :
398 		(types &  L1D_FLUSH_MTTRIG)     ? "mttrig type"
399 						: "unknown");
400 
401 	return 0;
402 }
403 
404 void do_entry_flush_fixups(enum l1d_flush_type types)
405 {
406 	/*
407 	 * The call to the fallback flush can not be safely patched in/out while
408 	 * other CPUs are executing it. So call __do_entry_flush_fixups() on one
409 	 * CPU while all other CPUs spin in the stop machine core with interrupts
410 	 * hard disabled.
411 	 */
412 	stop_machine(__do_entry_flush_fixups, &types, NULL);
413 }
414 
415 void do_rfi_flush_fixups(enum l1d_flush_type types)
416 {
417 	unsigned int instrs[3], *dest;
418 	long *start, *end;
419 	int i;
420 
421 	start = PTRRELOC(&__start___rfi_flush_fixup);
422 	end = PTRRELOC(&__stop___rfi_flush_fixup);
423 
424 	instrs[0] = 0x60000000; /* nop */
425 	instrs[1] = 0x60000000; /* nop */
426 	instrs[2] = 0x60000000; /* nop */
427 
428 	if (types & L1D_FLUSH_FALLBACK)
429 		/* b .+16 to fallback flush */
430 		instrs[0] = 0x48000010;
431 
432 	i = 0;
433 	if (types & L1D_FLUSH_ORI) {
434 		instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
435 		instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
436 	}
437 
438 	if (types & L1D_FLUSH_MTTRIG)
439 		instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
440 
441 	for (i = 0; start < end; start++, i++) {
442 		dest = (void *)start + *start;
443 
444 		pr_devel("patching dest %lx\n", (unsigned long)dest);
445 
446 		patch_instruction((struct ppc_inst *)dest, ppc_inst(instrs[0]));
447 		patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instrs[1]));
448 		patch_instruction((struct ppc_inst *)(dest + 2), ppc_inst(instrs[2]));
449 	}
450 
451 	printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
452 		(types == L1D_FLUSH_NONE)       ? "no" :
453 		(types == L1D_FLUSH_FALLBACK)   ? "fallback displacement" :
454 		(types &  L1D_FLUSH_ORI)        ? (types & L1D_FLUSH_MTTRIG)
455 							? "ori+mttrig type"
456 							: "ori type" :
457 		(types &  L1D_FLUSH_MTTRIG)     ? "mttrig type"
458 						: "unknown");
459 }
460 
461 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
462 {
463 	unsigned int instr, *dest;
464 	long *start, *end;
465 	int i;
466 
467 	start = fixup_start;
468 	end = fixup_end;
469 
470 	instr = 0x60000000; /* nop */
471 
472 	if (enable) {
473 		pr_info("barrier-nospec: using ORI speculation barrier\n");
474 		instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
475 	}
476 
477 	for (i = 0; start < end; start++, i++) {
478 		dest = (void *)start + *start;
479 
480 		pr_devel("patching dest %lx\n", (unsigned long)dest);
481 		patch_instruction((struct ppc_inst *)dest, ppc_inst(instr));
482 	}
483 
484 	printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
485 }
486 
487 #endif /* CONFIG_PPC_BOOK3S_64 */
488 
489 #ifdef CONFIG_PPC_BARRIER_NOSPEC
490 void do_barrier_nospec_fixups(bool enable)
491 {
492 	void *start, *end;
493 
494 	start = PTRRELOC(&__start___barrier_nospec_fixup);
495 	end = PTRRELOC(&__stop___barrier_nospec_fixup);
496 
497 	do_barrier_nospec_fixups_range(enable, start, end);
498 }
499 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
500 
501 #ifdef CONFIG_PPC_FSL_BOOK3E
502 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
503 {
504 	unsigned int instr[2], *dest;
505 	long *start, *end;
506 	int i;
507 
508 	start = fixup_start;
509 	end = fixup_end;
510 
511 	instr[0] = PPC_INST_NOP;
512 	instr[1] = PPC_INST_NOP;
513 
514 	if (enable) {
515 		pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
516 		instr[0] = PPC_INST_ISYNC;
517 		instr[1] = PPC_INST_SYNC;
518 	}
519 
520 	for (i = 0; start < end; start++, i++) {
521 		dest = (void *)start + *start;
522 
523 		pr_devel("patching dest %lx\n", (unsigned long)dest);
524 		patch_instruction((struct ppc_inst *)dest, ppc_inst(instr[0]));
525 		patch_instruction((struct ppc_inst *)(dest + 1), ppc_inst(instr[1]));
526 	}
527 
528 	printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
529 }
530 
531 static void patch_btb_flush_section(long *curr)
532 {
533 	unsigned int *start, *end;
534 
535 	start = (void *)curr + *curr;
536 	end = (void *)curr + *(curr + 1);
537 	for (; start < end; start++) {
538 		pr_devel("patching dest %lx\n", (unsigned long)start);
539 		patch_instruction((struct ppc_inst *)start, ppc_inst(PPC_INST_NOP));
540 	}
541 }
542 
543 void do_btb_flush_fixups(void)
544 {
545 	long *start, *end;
546 
547 	start = PTRRELOC(&__start__btb_flush_fixup);
548 	end = PTRRELOC(&__stop__btb_flush_fixup);
549 
550 	for (; start < end; start += 2)
551 		patch_btb_flush_section(start);
552 }
553 #endif /* CONFIG_PPC_FSL_BOOK3E */
554 
555 void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
556 {
557 	long *start, *end;
558 	struct ppc_inst *dest;
559 
560 	if (!(value & CPU_FTR_LWSYNC))
561 		return ;
562 
563 	start = fixup_start;
564 	end = fixup_end;
565 
566 	for (; start < end; start++) {
567 		dest = (void *)start + *start;
568 		raw_patch_instruction(dest, ppc_inst(PPC_INST_LWSYNC));
569 	}
570 }
571 
572 static void do_final_fixups(void)
573 {
574 #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
575 	struct ppc_inst inst, *src, *dest, *end;
576 
577 	if (PHYSICAL_START == 0)
578 		return;
579 
580 	src = (struct ppc_inst *)(KERNELBASE + PHYSICAL_START);
581 	dest = (struct ppc_inst *)KERNELBASE;
582 	end = (void *)src + (__end_interrupts - _stext);
583 
584 	while (src < end) {
585 		inst = ppc_inst_read(src);
586 		raw_patch_instruction(dest, inst);
587 		src = ppc_inst_next(src, src);
588 		dest = ppc_inst_next(dest, dest);
589 	}
590 #endif
591 }
592 
593 static unsigned long __initdata saved_cpu_features;
594 static unsigned int __initdata saved_mmu_features;
595 #ifdef CONFIG_PPC64
596 static unsigned long __initdata saved_firmware_features;
597 #endif
598 
599 void __init apply_feature_fixups(void)
600 {
601 	struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
602 
603 	*PTRRELOC(&saved_cpu_features) = spec->cpu_features;
604 	*PTRRELOC(&saved_mmu_features) = spec->mmu_features;
605 
606 	/*
607 	 * Apply the CPU-specific and firmware specific fixups to kernel text
608 	 * (nop out sections not relevant to this CPU or this firmware).
609 	 */
610 	do_feature_fixups(spec->cpu_features,
611 			  PTRRELOC(&__start___ftr_fixup),
612 			  PTRRELOC(&__stop___ftr_fixup));
613 
614 	do_feature_fixups(spec->mmu_features,
615 			  PTRRELOC(&__start___mmu_ftr_fixup),
616 			  PTRRELOC(&__stop___mmu_ftr_fixup));
617 
618 	do_lwsync_fixups(spec->cpu_features,
619 			 PTRRELOC(&__start___lwsync_fixup),
620 			 PTRRELOC(&__stop___lwsync_fixup));
621 
622 #ifdef CONFIG_PPC64
623 	saved_firmware_features = powerpc_firmware_features;
624 	do_feature_fixups(powerpc_firmware_features,
625 			  &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
626 #endif
627 	do_final_fixups();
628 }
629 
630 void __init setup_feature_keys(void)
631 {
632 	/*
633 	 * Initialise jump label. This causes all the cpu/mmu_has_feature()
634 	 * checks to take on their correct polarity based on the current set of
635 	 * CPU/MMU features.
636 	 */
637 	jump_label_init();
638 	cpu_feature_keys_init();
639 	mmu_feature_keys_init();
640 }
641 
642 static int __init check_features(void)
643 {
644 	WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
645 	     "CPU features changed after feature patching!\n");
646 	WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
647 	     "MMU features changed after feature patching!\n");
648 #ifdef CONFIG_PPC64
649 	WARN(saved_firmware_features != powerpc_firmware_features,
650 	     "Firmware features changed after feature patching!\n");
651 #endif
652 
653 	return 0;
654 }
655 late_initcall(check_features);
656 
657 #ifdef CONFIG_FTR_FIXUP_SELFTEST
658 
659 #define check(x)	\
660 	if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
661 
662 /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
663 static struct fixup_entry fixup;
664 
665 static long calc_offset(struct fixup_entry *entry, unsigned int *p)
666 {
667 	return (unsigned long)p - (unsigned long)entry;
668 }
669 
670 static void test_basic_patching(void)
671 {
672 	extern unsigned int ftr_fixup_test1[];
673 	extern unsigned int end_ftr_fixup_test1[];
674 	extern unsigned int ftr_fixup_test1_orig[];
675 	extern unsigned int ftr_fixup_test1_expected[];
676 	int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
677 
678 	fixup.value = fixup.mask = 8;
679 	fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
680 	fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
681 	fixup.alt_start_off = fixup.alt_end_off = 0;
682 
683 	/* Sanity check */
684 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
685 
686 	/* Check we don't patch if the value matches */
687 	patch_feature_section(8, &fixup);
688 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
689 
690 	/* Check we do patch if the value doesn't match */
691 	patch_feature_section(0, &fixup);
692 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
693 
694 	/* Check we do patch if the mask doesn't match */
695 	memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
696 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
697 	patch_feature_section(~8, &fixup);
698 	check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
699 }
700 
701 static void test_alternative_patching(void)
702 {
703 	extern unsigned int ftr_fixup_test2[];
704 	extern unsigned int end_ftr_fixup_test2[];
705 	extern unsigned int ftr_fixup_test2_orig[];
706 	extern unsigned int ftr_fixup_test2_alt[];
707 	extern unsigned int ftr_fixup_test2_expected[];
708 	int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
709 
710 	fixup.value = fixup.mask = 0xF;
711 	fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
712 	fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
713 	fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
714 	fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
715 
716 	/* Sanity check */
717 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
718 
719 	/* Check we don't patch if the value matches */
720 	patch_feature_section(0xF, &fixup);
721 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
722 
723 	/* Check we do patch if the value doesn't match */
724 	patch_feature_section(0, &fixup);
725 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
726 
727 	/* Check we do patch if the mask doesn't match */
728 	memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
729 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
730 	patch_feature_section(~0xF, &fixup);
731 	check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
732 }
733 
734 static void test_alternative_case_too_big(void)
735 {
736 	extern unsigned int ftr_fixup_test3[];
737 	extern unsigned int end_ftr_fixup_test3[];
738 	extern unsigned int ftr_fixup_test3_orig[];
739 	extern unsigned int ftr_fixup_test3_alt[];
740 	int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
741 
742 	fixup.value = fixup.mask = 0xC;
743 	fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
744 	fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
745 	fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
746 	fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
747 
748 	/* Sanity check */
749 	check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
750 
751 	/* Expect nothing to be patched, and the error returned to us */
752 	check(patch_feature_section(0xF, &fixup) == 1);
753 	check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
754 	check(patch_feature_section(0, &fixup) == 1);
755 	check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
756 	check(patch_feature_section(~0xF, &fixup) == 1);
757 	check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
758 }
759 
760 static void test_alternative_case_too_small(void)
761 {
762 	extern unsigned int ftr_fixup_test4[];
763 	extern unsigned int end_ftr_fixup_test4[];
764 	extern unsigned int ftr_fixup_test4_orig[];
765 	extern unsigned int ftr_fixup_test4_alt[];
766 	extern unsigned int ftr_fixup_test4_expected[];
767 	int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
768 	unsigned long flag;
769 
770 	/* Check a high-bit flag */
771 	flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
772 	fixup.value = fixup.mask = flag;
773 	fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
774 	fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
775 	fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
776 	fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
777 
778 	/* Sanity check */
779 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
780 
781 	/* Check we don't patch if the value matches */
782 	patch_feature_section(flag, &fixup);
783 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
784 
785 	/* Check we do patch if the value doesn't match */
786 	patch_feature_section(0, &fixup);
787 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
788 
789 	/* Check we do patch if the mask doesn't match */
790 	memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
791 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
792 	patch_feature_section(~flag, &fixup);
793 	check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
794 }
795 
796 static void test_alternative_case_with_branch(void)
797 {
798 	extern unsigned int ftr_fixup_test5[];
799 	extern unsigned int end_ftr_fixup_test5[];
800 	extern unsigned int ftr_fixup_test5_expected[];
801 	int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
802 
803 	check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
804 }
805 
806 static void test_alternative_case_with_external_branch(void)
807 {
808 	extern unsigned int ftr_fixup_test6[];
809 	extern unsigned int end_ftr_fixup_test6[];
810 	extern unsigned int ftr_fixup_test6_expected[];
811 	int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
812 
813 	check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
814 }
815 
816 static void test_alternative_case_with_branch_to_end(void)
817 {
818 	extern unsigned int ftr_fixup_test7[];
819 	extern unsigned int end_ftr_fixup_test7[];
820 	extern unsigned int ftr_fixup_test7_expected[];
821 	int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
822 
823 	check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
824 }
825 
826 static void test_cpu_macros(void)
827 {
828 	extern u8 ftr_fixup_test_FTR_macros[];
829 	extern u8 ftr_fixup_test_FTR_macros_expected[];
830 	unsigned long size = ftr_fixup_test_FTR_macros_expected -
831 			     ftr_fixup_test_FTR_macros;
832 
833 	/* The fixups have already been done for us during boot */
834 	check(memcmp(ftr_fixup_test_FTR_macros,
835 		     ftr_fixup_test_FTR_macros_expected, size) == 0);
836 }
837 
838 static void test_fw_macros(void)
839 {
840 #ifdef CONFIG_PPC64
841 	extern u8 ftr_fixup_test_FW_FTR_macros[];
842 	extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
843 	unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
844 			     ftr_fixup_test_FW_FTR_macros;
845 
846 	/* The fixups have already been done for us during boot */
847 	check(memcmp(ftr_fixup_test_FW_FTR_macros,
848 		     ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
849 #endif
850 }
851 
852 static void test_lwsync_macros(void)
853 {
854 	extern u8 lwsync_fixup_test[];
855 	extern u8 end_lwsync_fixup_test[];
856 	extern u8 lwsync_fixup_test_expected_LWSYNC[];
857 	extern u8 lwsync_fixup_test_expected_SYNC[];
858 	unsigned long size = end_lwsync_fixup_test -
859 			     lwsync_fixup_test;
860 
861 	/* The fixups have already been done for us during boot */
862 	if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
863 		check(memcmp(lwsync_fixup_test,
864 			     lwsync_fixup_test_expected_LWSYNC, size) == 0);
865 	} else {
866 		check(memcmp(lwsync_fixup_test,
867 			     lwsync_fixup_test_expected_SYNC, size) == 0);
868 	}
869 }
870 
871 #ifdef CONFIG_PPC64
872 static void __init test_prefix_patching(void)
873 {
874 	extern unsigned int ftr_fixup_prefix1[];
875 	extern unsigned int end_ftr_fixup_prefix1[];
876 	extern unsigned int ftr_fixup_prefix1_orig[];
877 	extern unsigned int ftr_fixup_prefix1_expected[];
878 	int size = sizeof(unsigned int) * (end_ftr_fixup_prefix1 - ftr_fixup_prefix1);
879 
880 	fixup.value = fixup.mask = 8;
881 	fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix1 + 1);
882 	fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix1 + 3);
883 	fixup.alt_start_off = fixup.alt_end_off = 0;
884 
885 	/* Sanity check */
886 	check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) == 0);
887 
888 	patch_feature_section(0, &fixup);
889 	check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_expected, size) == 0);
890 	check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) != 0);
891 }
892 
893 static void __init test_prefix_alt_patching(void)
894 {
895 	extern unsigned int ftr_fixup_prefix2[];
896 	extern unsigned int end_ftr_fixup_prefix2[];
897 	extern unsigned int ftr_fixup_prefix2_orig[];
898 	extern unsigned int ftr_fixup_prefix2_expected[];
899 	extern unsigned int ftr_fixup_prefix2_alt[];
900 	int size = sizeof(unsigned int) * (end_ftr_fixup_prefix2 - ftr_fixup_prefix2);
901 
902 	fixup.value = fixup.mask = 8;
903 	fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix2 + 1);
904 	fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix2 + 3);
905 	fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix2_alt);
906 	fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix2_alt + 2);
907 	/* Sanity check */
908 	check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) == 0);
909 
910 	patch_feature_section(0, &fixup);
911 	check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_expected, size) == 0);
912 	check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) != 0);
913 }
914 
915 static void __init test_prefix_word_alt_patching(void)
916 {
917 	extern unsigned int ftr_fixup_prefix3[];
918 	extern unsigned int end_ftr_fixup_prefix3[];
919 	extern unsigned int ftr_fixup_prefix3_orig[];
920 	extern unsigned int ftr_fixup_prefix3_expected[];
921 	extern unsigned int ftr_fixup_prefix3_alt[];
922 	int size = sizeof(unsigned int) * (end_ftr_fixup_prefix3 - ftr_fixup_prefix3);
923 
924 	fixup.value = fixup.mask = 8;
925 	fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix3 + 1);
926 	fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix3 + 4);
927 	fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix3_alt);
928 	fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix3_alt + 3);
929 	/* Sanity check */
930 	check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) == 0);
931 
932 	patch_feature_section(0, &fixup);
933 	check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_expected, size) == 0);
934 	patch_feature_section(0, &fixup);
935 	check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) != 0);
936 }
937 #else
938 static inline void test_prefix_patching(void) {}
939 static inline void test_prefix_alt_patching(void) {}
940 static inline void test_prefix_word_alt_patching(void) {}
941 #endif /* CONFIG_PPC64 */
942 
943 static int __init test_feature_fixups(void)
944 {
945 	printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
946 
947 	test_basic_patching();
948 	test_alternative_patching();
949 	test_alternative_case_too_big();
950 	test_alternative_case_too_small();
951 	test_alternative_case_with_branch();
952 	test_alternative_case_with_external_branch();
953 	test_alternative_case_with_branch_to_end();
954 	test_cpu_macros();
955 	test_fw_macros();
956 	test_lwsync_macros();
957 	test_prefix_patching();
958 	test_prefix_alt_patching();
959 	test_prefix_word_alt_patching();
960 
961 	return 0;
962 }
963 late_initcall(test_feature_fixups);
964 
965 #endif /* CONFIG_FTR_FIXUP_SELFTEST */
966