xref: /linux/arch/powerpc/kernel/process.c (revision d5fa30e6993ffcdd1859d8dab1a07a6f6c6e7c3f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Derived from "arch/i386/kernel/process.c"
4  *    Copyright (C) 1995  Linus Torvalds
5  *
6  *  Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
7  *  Paul Mackerras (paulus@cs.anu.edu.au)
8  *
9  *  PowerPC version
10  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11  */
12 
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/sched/debug.h>
16 #include <linux/sched/task.h>
17 #include <linux/sched/task_stack.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #include <linux/stddef.h>
22 #include <linux/unistd.h>
23 #include <linux/ptrace.h>
24 #include <linux/slab.h>
25 #include <linux/user.h>
26 #include <linux/elf.h>
27 #include <linux/prctl.h>
28 #include <linux/init_task.h>
29 #include <linux/export.h>
30 #include <linux/kallsyms.h>
31 #include <linux/mqueue.h>
32 #include <linux/hardirq.h>
33 #include <linux/utsname.h>
34 #include <linux/ftrace.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/personality.h>
37 #include <linux/random.h>
38 #include <linux/hw_breakpoint.h>
39 #include <linux/uaccess.h>
40 #include <linux/elf-randomize.h>
41 #include <linux/pkeys.h>
42 #include <linux/seq_buf.h>
43 
44 #include <asm/io.h>
45 #include <asm/processor.h>
46 #include <asm/mmu.h>
47 #include <asm/prom.h>
48 #include <asm/machdep.h>
49 #include <asm/time.h>
50 #include <asm/runlatch.h>
51 #include <asm/syscalls.h>
52 #include <asm/switch_to.h>
53 #include <asm/tm.h>
54 #include <asm/debug.h>
55 #ifdef CONFIG_PPC64
56 #include <asm/firmware.h>
57 #include <asm/hw_irq.h>
58 #endif
59 #include <asm/code-patching.h>
60 #include <asm/exec.h>
61 #include <asm/livepatch.h>
62 #include <asm/cpu_has_feature.h>
63 #include <asm/asm-prototypes.h>
64 #include <asm/stacktrace.h>
65 #include <asm/hw_breakpoint.h>
66 
67 #include <linux/kprobes.h>
68 #include <linux/kdebug.h>
69 
70 /* Transactional Memory debug */
71 #ifdef TM_DEBUG_SW
72 #define TM_DEBUG(x...) printk(KERN_INFO x)
73 #else
74 #define TM_DEBUG(x...) do { } while(0)
75 #endif
76 
77 extern unsigned long _get_SP(void);
78 
79 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
80 /*
81  * Are we running in "Suspend disabled" mode? If so we have to block any
82  * sigreturn that would get us into suspended state, and we also warn in some
83  * other paths that we should never reach with suspend disabled.
84  */
85 bool tm_suspend_disabled __ro_after_init = false;
86 
87 static void check_if_tm_restore_required(struct task_struct *tsk)
88 {
89 	/*
90 	 * If we are saving the current thread's registers, and the
91 	 * thread is in a transactional state, set the TIF_RESTORE_TM
92 	 * bit so that we know to restore the registers before
93 	 * returning to userspace.
94 	 */
95 	if (tsk == current && tsk->thread.regs &&
96 	    MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
97 	    !test_thread_flag(TIF_RESTORE_TM)) {
98 		tsk->thread.ckpt_regs.msr = tsk->thread.regs->msr;
99 		set_thread_flag(TIF_RESTORE_TM);
100 	}
101 }
102 
103 #else
104 static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
105 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
106 
107 bool strict_msr_control;
108 EXPORT_SYMBOL(strict_msr_control);
109 
110 static int __init enable_strict_msr_control(char *str)
111 {
112 	strict_msr_control = true;
113 	pr_info("Enabling strict facility control\n");
114 
115 	return 0;
116 }
117 early_param("ppc_strict_facility_enable", enable_strict_msr_control);
118 
119 /* notrace because it's called by restore_math */
120 unsigned long notrace msr_check_and_set(unsigned long bits)
121 {
122 	unsigned long oldmsr = mfmsr();
123 	unsigned long newmsr;
124 
125 	newmsr = oldmsr | bits;
126 
127 	if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
128 		newmsr |= MSR_VSX;
129 
130 	if (oldmsr != newmsr)
131 		mtmsr_isync(newmsr);
132 
133 	return newmsr;
134 }
135 EXPORT_SYMBOL_GPL(msr_check_and_set);
136 
137 /* notrace because it's called by restore_math */
138 void notrace __msr_check_and_clear(unsigned long bits)
139 {
140 	unsigned long oldmsr = mfmsr();
141 	unsigned long newmsr;
142 
143 	newmsr = oldmsr & ~bits;
144 
145 	if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
146 		newmsr &= ~MSR_VSX;
147 
148 	if (oldmsr != newmsr)
149 		mtmsr_isync(newmsr);
150 }
151 EXPORT_SYMBOL(__msr_check_and_clear);
152 
153 #ifdef CONFIG_PPC_FPU
154 static void __giveup_fpu(struct task_struct *tsk)
155 {
156 	unsigned long msr;
157 
158 	save_fpu(tsk);
159 	msr = tsk->thread.regs->msr;
160 	msr &= ~(MSR_FP|MSR_FE0|MSR_FE1);
161 	if (cpu_has_feature(CPU_FTR_VSX))
162 		msr &= ~MSR_VSX;
163 	tsk->thread.regs->msr = msr;
164 }
165 
166 void giveup_fpu(struct task_struct *tsk)
167 {
168 	check_if_tm_restore_required(tsk);
169 
170 	msr_check_and_set(MSR_FP);
171 	__giveup_fpu(tsk);
172 	msr_check_and_clear(MSR_FP);
173 }
174 EXPORT_SYMBOL(giveup_fpu);
175 
176 /*
177  * Make sure the floating-point register state in the
178  * the thread_struct is up to date for task tsk.
179  */
180 void flush_fp_to_thread(struct task_struct *tsk)
181 {
182 	if (tsk->thread.regs) {
183 		/*
184 		 * We need to disable preemption here because if we didn't,
185 		 * another process could get scheduled after the regs->msr
186 		 * test but before we have finished saving the FP registers
187 		 * to the thread_struct.  That process could take over the
188 		 * FPU, and then when we get scheduled again we would store
189 		 * bogus values for the remaining FP registers.
190 		 */
191 		preempt_disable();
192 		if (tsk->thread.regs->msr & MSR_FP) {
193 			/*
194 			 * This should only ever be called for current or
195 			 * for a stopped child process.  Since we save away
196 			 * the FP register state on context switch,
197 			 * there is something wrong if a stopped child appears
198 			 * to still have its FP state in the CPU registers.
199 			 */
200 			BUG_ON(tsk != current);
201 			giveup_fpu(tsk);
202 		}
203 		preempt_enable();
204 	}
205 }
206 EXPORT_SYMBOL_GPL(flush_fp_to_thread);
207 
208 void enable_kernel_fp(void)
209 {
210 	unsigned long cpumsr;
211 
212 	WARN_ON(preemptible());
213 
214 	cpumsr = msr_check_and_set(MSR_FP);
215 
216 	if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
217 		check_if_tm_restore_required(current);
218 		/*
219 		 * If a thread has already been reclaimed then the
220 		 * checkpointed registers are on the CPU but have definitely
221 		 * been saved by the reclaim code. Don't need to and *cannot*
222 		 * giveup as this would save  to the 'live' structure not the
223 		 * checkpointed structure.
224 		 */
225 		if (!MSR_TM_ACTIVE(cpumsr) &&
226 		     MSR_TM_ACTIVE(current->thread.regs->msr))
227 			return;
228 		__giveup_fpu(current);
229 	}
230 }
231 EXPORT_SYMBOL(enable_kernel_fp);
232 #else
233 static inline void __giveup_fpu(struct task_struct *tsk) { }
234 #endif /* CONFIG_PPC_FPU */
235 
236 #ifdef CONFIG_ALTIVEC
237 static void __giveup_altivec(struct task_struct *tsk)
238 {
239 	unsigned long msr;
240 
241 	save_altivec(tsk);
242 	msr = tsk->thread.regs->msr;
243 	msr &= ~MSR_VEC;
244 	if (cpu_has_feature(CPU_FTR_VSX))
245 		msr &= ~MSR_VSX;
246 	tsk->thread.regs->msr = msr;
247 }
248 
249 void giveup_altivec(struct task_struct *tsk)
250 {
251 	check_if_tm_restore_required(tsk);
252 
253 	msr_check_and_set(MSR_VEC);
254 	__giveup_altivec(tsk);
255 	msr_check_and_clear(MSR_VEC);
256 }
257 EXPORT_SYMBOL(giveup_altivec);
258 
259 void enable_kernel_altivec(void)
260 {
261 	unsigned long cpumsr;
262 
263 	WARN_ON(preemptible());
264 
265 	cpumsr = msr_check_and_set(MSR_VEC);
266 
267 	if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
268 		check_if_tm_restore_required(current);
269 		/*
270 		 * If a thread has already been reclaimed then the
271 		 * checkpointed registers are on the CPU but have definitely
272 		 * been saved by the reclaim code. Don't need to and *cannot*
273 		 * giveup as this would save  to the 'live' structure not the
274 		 * checkpointed structure.
275 		 */
276 		if (!MSR_TM_ACTIVE(cpumsr) &&
277 		     MSR_TM_ACTIVE(current->thread.regs->msr))
278 			return;
279 		__giveup_altivec(current);
280 	}
281 }
282 EXPORT_SYMBOL(enable_kernel_altivec);
283 
284 /*
285  * Make sure the VMX/Altivec register state in the
286  * the thread_struct is up to date for task tsk.
287  */
288 void flush_altivec_to_thread(struct task_struct *tsk)
289 {
290 	if (tsk->thread.regs) {
291 		preempt_disable();
292 		if (tsk->thread.regs->msr & MSR_VEC) {
293 			BUG_ON(tsk != current);
294 			giveup_altivec(tsk);
295 		}
296 		preempt_enable();
297 	}
298 }
299 EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
300 #endif /* CONFIG_ALTIVEC */
301 
302 #ifdef CONFIG_VSX
303 static void __giveup_vsx(struct task_struct *tsk)
304 {
305 	unsigned long msr = tsk->thread.regs->msr;
306 
307 	/*
308 	 * We should never be ssetting MSR_VSX without also setting
309 	 * MSR_FP and MSR_VEC
310 	 */
311 	WARN_ON((msr & MSR_VSX) && !((msr & MSR_FP) && (msr & MSR_VEC)));
312 
313 	/* __giveup_fpu will clear MSR_VSX */
314 	if (msr & MSR_FP)
315 		__giveup_fpu(tsk);
316 	if (msr & MSR_VEC)
317 		__giveup_altivec(tsk);
318 }
319 
320 static void giveup_vsx(struct task_struct *tsk)
321 {
322 	check_if_tm_restore_required(tsk);
323 
324 	msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
325 	__giveup_vsx(tsk);
326 	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
327 }
328 
329 void enable_kernel_vsx(void)
330 {
331 	unsigned long cpumsr;
332 
333 	WARN_ON(preemptible());
334 
335 	cpumsr = msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
336 
337 	if (current->thread.regs &&
338 	    (current->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP))) {
339 		check_if_tm_restore_required(current);
340 		/*
341 		 * If a thread has already been reclaimed then the
342 		 * checkpointed registers are on the CPU but have definitely
343 		 * been saved by the reclaim code. Don't need to and *cannot*
344 		 * giveup as this would save  to the 'live' structure not the
345 		 * checkpointed structure.
346 		 */
347 		if (!MSR_TM_ACTIVE(cpumsr) &&
348 		     MSR_TM_ACTIVE(current->thread.regs->msr))
349 			return;
350 		__giveup_vsx(current);
351 	}
352 }
353 EXPORT_SYMBOL(enable_kernel_vsx);
354 
355 void flush_vsx_to_thread(struct task_struct *tsk)
356 {
357 	if (tsk->thread.regs) {
358 		preempt_disable();
359 		if (tsk->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP)) {
360 			BUG_ON(tsk != current);
361 			giveup_vsx(tsk);
362 		}
363 		preempt_enable();
364 	}
365 }
366 EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
367 #endif /* CONFIG_VSX */
368 
369 #ifdef CONFIG_SPE
370 void giveup_spe(struct task_struct *tsk)
371 {
372 	check_if_tm_restore_required(tsk);
373 
374 	msr_check_and_set(MSR_SPE);
375 	__giveup_spe(tsk);
376 	msr_check_and_clear(MSR_SPE);
377 }
378 EXPORT_SYMBOL(giveup_spe);
379 
380 void enable_kernel_spe(void)
381 {
382 	WARN_ON(preemptible());
383 
384 	msr_check_and_set(MSR_SPE);
385 
386 	if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
387 		check_if_tm_restore_required(current);
388 		__giveup_spe(current);
389 	}
390 }
391 EXPORT_SYMBOL(enable_kernel_spe);
392 
393 void flush_spe_to_thread(struct task_struct *tsk)
394 {
395 	if (tsk->thread.regs) {
396 		preempt_disable();
397 		if (tsk->thread.regs->msr & MSR_SPE) {
398 			BUG_ON(tsk != current);
399 			tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
400 			giveup_spe(tsk);
401 		}
402 		preempt_enable();
403 	}
404 }
405 #endif /* CONFIG_SPE */
406 
407 static unsigned long msr_all_available;
408 
409 static int __init init_msr_all_available(void)
410 {
411 	if (IS_ENABLED(CONFIG_PPC_FPU))
412 		msr_all_available |= MSR_FP;
413 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
414 		msr_all_available |= MSR_VEC;
415 	if (cpu_has_feature(CPU_FTR_VSX))
416 		msr_all_available |= MSR_VSX;
417 	if (cpu_has_feature(CPU_FTR_SPE))
418 		msr_all_available |= MSR_SPE;
419 
420 	return 0;
421 }
422 early_initcall(init_msr_all_available);
423 
424 void giveup_all(struct task_struct *tsk)
425 {
426 	unsigned long usermsr;
427 
428 	if (!tsk->thread.regs)
429 		return;
430 
431 	check_if_tm_restore_required(tsk);
432 
433 	usermsr = tsk->thread.regs->msr;
434 
435 	if ((usermsr & msr_all_available) == 0)
436 		return;
437 
438 	msr_check_and_set(msr_all_available);
439 
440 	WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
441 
442 	if (usermsr & MSR_FP)
443 		__giveup_fpu(tsk);
444 	if (usermsr & MSR_VEC)
445 		__giveup_altivec(tsk);
446 	if (usermsr & MSR_SPE)
447 		__giveup_spe(tsk);
448 
449 	msr_check_and_clear(msr_all_available);
450 }
451 EXPORT_SYMBOL(giveup_all);
452 
453 #ifdef CONFIG_PPC_BOOK3S_64
454 #ifdef CONFIG_PPC_FPU
455 static bool should_restore_fp(void)
456 {
457 	if (current->thread.load_fp) {
458 		current->thread.load_fp++;
459 		return true;
460 	}
461 	return false;
462 }
463 
464 static void do_restore_fp(void)
465 {
466 	load_fp_state(&current->thread.fp_state);
467 }
468 #else
469 static bool should_restore_fp(void) { return false; }
470 static void do_restore_fp(void) { }
471 #endif /* CONFIG_PPC_FPU */
472 
473 #ifdef CONFIG_ALTIVEC
474 static bool should_restore_altivec(void)
475 {
476 	if (cpu_has_feature(CPU_FTR_ALTIVEC) && (current->thread.load_vec)) {
477 		current->thread.load_vec++;
478 		return true;
479 	}
480 	return false;
481 }
482 
483 static void do_restore_altivec(void)
484 {
485 	load_vr_state(&current->thread.vr_state);
486 	current->thread.used_vr = 1;
487 }
488 #else
489 static bool should_restore_altivec(void) { return false; }
490 static void do_restore_altivec(void) { }
491 #endif /* CONFIG_ALTIVEC */
492 
493 static bool should_restore_vsx(void)
494 {
495 	if (cpu_has_feature(CPU_FTR_VSX))
496 		return true;
497 	return false;
498 }
499 #ifdef CONFIG_VSX
500 static void do_restore_vsx(void)
501 {
502 	current->thread.used_vsr = 1;
503 }
504 #else
505 static void do_restore_vsx(void) { }
506 #endif /* CONFIG_VSX */
507 
508 /*
509  * The exception exit path calls restore_math() with interrupts hard disabled
510  * but the soft irq state not "reconciled". ftrace code that calls
511  * local_irq_save/restore causes warnings.
512  *
513  * Rather than complicate the exit path, just don't trace restore_math. This
514  * could be done by having ftrace entry code check for this un-reconciled
515  * condition where MSR[EE]=0 and PACA_IRQ_HARD_DIS is not set, and
516  * temporarily fix it up for the duration of the ftrace call.
517  */
518 void notrace restore_math(struct pt_regs *regs)
519 {
520 	unsigned long msr;
521 	unsigned long new_msr = 0;
522 
523 	msr = regs->msr;
524 
525 	/*
526 	 * new_msr tracks the facilities that are to be restored. Only reload
527 	 * if the bit is not set in the user MSR (if it is set, the registers
528 	 * are live for the user thread).
529 	 */
530 	if ((!(msr & MSR_FP)) && should_restore_fp())
531 		new_msr |= MSR_FP;
532 
533 	if ((!(msr & MSR_VEC)) && should_restore_altivec())
534 		new_msr |= MSR_VEC;
535 
536 	if ((!(msr & MSR_VSX)) && should_restore_vsx()) {
537 		if (((msr | new_msr) & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC))
538 			new_msr |= MSR_VSX;
539 	}
540 
541 	if (new_msr) {
542 		unsigned long fpexc_mode = 0;
543 
544 		msr_check_and_set(new_msr);
545 
546 		if (new_msr & MSR_FP) {
547 			do_restore_fp();
548 
549 			// This also covers VSX, because VSX implies FP
550 			fpexc_mode = current->thread.fpexc_mode;
551 		}
552 
553 		if (new_msr & MSR_VEC)
554 			do_restore_altivec();
555 
556 		if (new_msr & MSR_VSX)
557 			do_restore_vsx();
558 
559 		msr_check_and_clear(new_msr);
560 
561 		regs->msr |= new_msr | fpexc_mode;
562 	}
563 }
564 #endif /* CONFIG_PPC_BOOK3S_64 */
565 
566 static void save_all(struct task_struct *tsk)
567 {
568 	unsigned long usermsr;
569 
570 	if (!tsk->thread.regs)
571 		return;
572 
573 	usermsr = tsk->thread.regs->msr;
574 
575 	if ((usermsr & msr_all_available) == 0)
576 		return;
577 
578 	msr_check_and_set(msr_all_available);
579 
580 	WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
581 
582 	if (usermsr & MSR_FP)
583 		save_fpu(tsk);
584 
585 	if (usermsr & MSR_VEC)
586 		save_altivec(tsk);
587 
588 	if (usermsr & MSR_SPE)
589 		__giveup_spe(tsk);
590 
591 	msr_check_and_clear(msr_all_available);
592 	thread_pkey_regs_save(&tsk->thread);
593 }
594 
595 void flush_all_to_thread(struct task_struct *tsk)
596 {
597 	if (tsk->thread.regs) {
598 		preempt_disable();
599 		BUG_ON(tsk != current);
600 #ifdef CONFIG_SPE
601 		if (tsk->thread.regs->msr & MSR_SPE)
602 			tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
603 #endif
604 		save_all(tsk);
605 
606 		preempt_enable();
607 	}
608 }
609 EXPORT_SYMBOL(flush_all_to_thread);
610 
611 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
612 void do_send_trap(struct pt_regs *regs, unsigned long address,
613 		  unsigned long error_code, int breakpt)
614 {
615 	current->thread.trap_nr = TRAP_HWBKPT;
616 	if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
617 			11, SIGSEGV) == NOTIFY_STOP)
618 		return;
619 
620 	/* Deliver the signal to userspace */
621 	force_sig_ptrace_errno_trap(breakpt, /* breakpoint or watchpoint id */
622 				    (void __user *)address);
623 }
624 #else	/* !CONFIG_PPC_ADV_DEBUG_REGS */
625 
626 static void do_break_handler(struct pt_regs *regs)
627 {
628 	struct arch_hw_breakpoint null_brk = {0};
629 	struct arch_hw_breakpoint *info;
630 	struct ppc_inst instr = ppc_inst(0);
631 	int type = 0;
632 	int size = 0;
633 	unsigned long ea;
634 	int i;
635 
636 	/*
637 	 * If underneath hw supports only one watchpoint, we know it
638 	 * caused exception. 8xx also falls into this category.
639 	 */
640 	if (nr_wp_slots() == 1) {
641 		__set_breakpoint(0, &null_brk);
642 		current->thread.hw_brk[0] = null_brk;
643 		current->thread.hw_brk[0].flags |= HW_BRK_FLAG_DISABLED;
644 		return;
645 	}
646 
647 	/* Otherwise findout which DAWR caused exception and disable it. */
648 	wp_get_instr_detail(regs, &instr, &type, &size, &ea);
649 
650 	for (i = 0; i < nr_wp_slots(); i++) {
651 		info = &current->thread.hw_brk[i];
652 		if (!info->address)
653 			continue;
654 
655 		if (wp_check_constraints(regs, instr, ea, type, size, info)) {
656 			__set_breakpoint(i, &null_brk);
657 			current->thread.hw_brk[i] = null_brk;
658 			current->thread.hw_brk[i].flags |= HW_BRK_FLAG_DISABLED;
659 		}
660 	}
661 }
662 
663 void do_break (struct pt_regs *regs, unsigned long address,
664 		    unsigned long error_code)
665 {
666 	current->thread.trap_nr = TRAP_HWBKPT;
667 	if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
668 			11, SIGSEGV) == NOTIFY_STOP)
669 		return;
670 
671 	if (debugger_break_match(regs))
672 		return;
673 
674 	/*
675 	 * We reach here only when watchpoint exception is generated by ptrace
676 	 * event (or hw is buggy!). Now if CONFIG_HAVE_HW_BREAKPOINT is set,
677 	 * watchpoint is already handled by hw_breakpoint_handler() so we don't
678 	 * have to do anything. But when CONFIG_HAVE_HW_BREAKPOINT is not set,
679 	 * we need to manually handle the watchpoint here.
680 	 */
681 	if (!IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT))
682 		do_break_handler(regs);
683 
684 	/* Deliver the signal to userspace */
685 	force_sig_fault(SIGTRAP, TRAP_HWBKPT, (void __user *)address);
686 }
687 #endif	/* CONFIG_PPC_ADV_DEBUG_REGS */
688 
689 static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk[HBP_NUM_MAX]);
690 
691 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
692 /*
693  * Set the debug registers back to their default "safe" values.
694  */
695 static void set_debug_reg_defaults(struct thread_struct *thread)
696 {
697 	thread->debug.iac1 = thread->debug.iac2 = 0;
698 #if CONFIG_PPC_ADV_DEBUG_IACS > 2
699 	thread->debug.iac3 = thread->debug.iac4 = 0;
700 #endif
701 	thread->debug.dac1 = thread->debug.dac2 = 0;
702 #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
703 	thread->debug.dvc1 = thread->debug.dvc2 = 0;
704 #endif
705 	thread->debug.dbcr0 = 0;
706 #ifdef CONFIG_BOOKE
707 	/*
708 	 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
709 	 */
710 	thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
711 			DBCR1_IAC3US | DBCR1_IAC4US;
712 	/*
713 	 * Force Data Address Compare User/Supervisor bits to be User-only
714 	 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
715 	 */
716 	thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
717 #else
718 	thread->debug.dbcr1 = 0;
719 #endif
720 }
721 
722 static void prime_debug_regs(struct debug_reg *debug)
723 {
724 	/*
725 	 * We could have inherited MSR_DE from userspace, since
726 	 * it doesn't get cleared on exception entry.  Make sure
727 	 * MSR_DE is clear before we enable any debug events.
728 	 */
729 	mtmsr(mfmsr() & ~MSR_DE);
730 
731 	mtspr(SPRN_IAC1, debug->iac1);
732 	mtspr(SPRN_IAC2, debug->iac2);
733 #if CONFIG_PPC_ADV_DEBUG_IACS > 2
734 	mtspr(SPRN_IAC3, debug->iac3);
735 	mtspr(SPRN_IAC4, debug->iac4);
736 #endif
737 	mtspr(SPRN_DAC1, debug->dac1);
738 	mtspr(SPRN_DAC2, debug->dac2);
739 #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
740 	mtspr(SPRN_DVC1, debug->dvc1);
741 	mtspr(SPRN_DVC2, debug->dvc2);
742 #endif
743 	mtspr(SPRN_DBCR0, debug->dbcr0);
744 	mtspr(SPRN_DBCR1, debug->dbcr1);
745 #ifdef CONFIG_BOOKE
746 	mtspr(SPRN_DBCR2, debug->dbcr2);
747 #endif
748 }
749 /*
750  * Unless neither the old or new thread are making use of the
751  * debug registers, set the debug registers from the values
752  * stored in the new thread.
753  */
754 void switch_booke_debug_regs(struct debug_reg *new_debug)
755 {
756 	if ((current->thread.debug.dbcr0 & DBCR0_IDM)
757 		|| (new_debug->dbcr0 & DBCR0_IDM))
758 			prime_debug_regs(new_debug);
759 }
760 EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
761 #else	/* !CONFIG_PPC_ADV_DEBUG_REGS */
762 #ifndef CONFIG_HAVE_HW_BREAKPOINT
763 static void set_breakpoint(int i, struct arch_hw_breakpoint *brk)
764 {
765 	preempt_disable();
766 	__set_breakpoint(i, brk);
767 	preempt_enable();
768 }
769 
770 static void set_debug_reg_defaults(struct thread_struct *thread)
771 {
772 	int i;
773 	struct arch_hw_breakpoint null_brk = {0};
774 
775 	for (i = 0; i < nr_wp_slots(); i++) {
776 		thread->hw_brk[i] = null_brk;
777 		if (ppc_breakpoint_available())
778 			set_breakpoint(i, &thread->hw_brk[i]);
779 	}
780 }
781 
782 static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
783 				struct arch_hw_breakpoint *b)
784 {
785 	if (a->address != b->address)
786 		return false;
787 	if (a->type != b->type)
788 		return false;
789 	if (a->len != b->len)
790 		return false;
791 	/* no need to check hw_len. it's calculated from address and len */
792 	return true;
793 }
794 
795 static void switch_hw_breakpoint(struct task_struct *new)
796 {
797 	int i;
798 
799 	for (i = 0; i < nr_wp_slots(); i++) {
800 		if (likely(hw_brk_match(this_cpu_ptr(&current_brk[i]),
801 					&new->thread.hw_brk[i])))
802 			continue;
803 
804 		__set_breakpoint(i, &new->thread.hw_brk[i]);
805 	}
806 }
807 #endif /* !CONFIG_HAVE_HW_BREAKPOINT */
808 #endif	/* CONFIG_PPC_ADV_DEBUG_REGS */
809 
810 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
811 static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
812 {
813 	mtspr(SPRN_DAC1, dabr);
814 	if (IS_ENABLED(CONFIG_PPC_47x))
815 		isync();
816 	return 0;
817 }
818 #elif defined(CONFIG_PPC_BOOK3S)
819 static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
820 {
821 	mtspr(SPRN_DABR, dabr);
822 	if (cpu_has_feature(CPU_FTR_DABRX))
823 		mtspr(SPRN_DABRX, dabrx);
824 	return 0;
825 }
826 #else
827 static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
828 {
829 	return -EINVAL;
830 }
831 #endif
832 
833 static inline int set_dabr(struct arch_hw_breakpoint *brk)
834 {
835 	unsigned long dabr, dabrx;
836 
837 	dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
838 	dabrx = ((brk->type >> 3) & 0x7);
839 
840 	if (ppc_md.set_dabr)
841 		return ppc_md.set_dabr(dabr, dabrx);
842 
843 	return __set_dabr(dabr, dabrx);
844 }
845 
846 static inline int set_breakpoint_8xx(struct arch_hw_breakpoint *brk)
847 {
848 	unsigned long lctrl1 = LCTRL1_CTE_GT | LCTRL1_CTF_LT | LCTRL1_CRWE_RW |
849 			       LCTRL1_CRWF_RW;
850 	unsigned long lctrl2 = LCTRL2_LW0EN | LCTRL2_LW0LADC | LCTRL2_SLW0EN;
851 	unsigned long start_addr = ALIGN_DOWN(brk->address, HW_BREAKPOINT_SIZE);
852 	unsigned long end_addr = ALIGN(brk->address + brk->len, HW_BREAKPOINT_SIZE);
853 
854 	if (start_addr == 0)
855 		lctrl2 |= LCTRL2_LW0LA_F;
856 	else if (end_addr == 0)
857 		lctrl2 |= LCTRL2_LW0LA_E;
858 	else
859 		lctrl2 |= LCTRL2_LW0LA_EandF;
860 
861 	mtspr(SPRN_LCTRL2, 0);
862 
863 	if ((brk->type & HW_BRK_TYPE_RDWR) == 0)
864 		return 0;
865 
866 	if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_READ)
867 		lctrl1 |= LCTRL1_CRWE_RO | LCTRL1_CRWF_RO;
868 	if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_WRITE)
869 		lctrl1 |= LCTRL1_CRWE_WO | LCTRL1_CRWF_WO;
870 
871 	mtspr(SPRN_CMPE, start_addr - 1);
872 	mtspr(SPRN_CMPF, end_addr);
873 	mtspr(SPRN_LCTRL1, lctrl1);
874 	mtspr(SPRN_LCTRL2, lctrl2);
875 
876 	return 0;
877 }
878 
879 void __set_breakpoint(int nr, struct arch_hw_breakpoint *brk)
880 {
881 	memcpy(this_cpu_ptr(&current_brk[nr]), brk, sizeof(*brk));
882 
883 	if (dawr_enabled())
884 		// Power8 or later
885 		set_dawr(nr, brk);
886 	else if (IS_ENABLED(CONFIG_PPC_8xx))
887 		set_breakpoint_8xx(brk);
888 	else if (!cpu_has_feature(CPU_FTR_ARCH_207S))
889 		// Power7 or earlier
890 		set_dabr(brk);
891 	else
892 		// Shouldn't happen due to higher level checks
893 		WARN_ON_ONCE(1);
894 }
895 
896 /* Check if we have DAWR or DABR hardware */
897 bool ppc_breakpoint_available(void)
898 {
899 	if (dawr_enabled())
900 		return true; /* POWER8 DAWR or POWER9 forced DAWR */
901 	if (cpu_has_feature(CPU_FTR_ARCH_207S))
902 		return false; /* POWER9 with DAWR disabled */
903 	/* DABR: Everything but POWER8 and POWER9 */
904 	return true;
905 }
906 EXPORT_SYMBOL_GPL(ppc_breakpoint_available);
907 
908 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
909 
910 static inline bool tm_enabled(struct task_struct *tsk)
911 {
912 	return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM);
913 }
914 
915 static void tm_reclaim_thread(struct thread_struct *thr, uint8_t cause)
916 {
917 	/*
918 	 * Use the current MSR TM suspended bit to track if we have
919 	 * checkpointed state outstanding.
920 	 * On signal delivery, we'd normally reclaim the checkpointed
921 	 * state to obtain stack pointer (see:get_tm_stackpointer()).
922 	 * This will then directly return to userspace without going
923 	 * through __switch_to(). However, if the stack frame is bad,
924 	 * we need to exit this thread which calls __switch_to() which
925 	 * will again attempt to reclaim the already saved tm state.
926 	 * Hence we need to check that we've not already reclaimed
927 	 * this state.
928 	 * We do this using the current MSR, rather tracking it in
929 	 * some specific thread_struct bit, as it has the additional
930 	 * benefit of checking for a potential TM bad thing exception.
931 	 */
932 	if (!MSR_TM_SUSPENDED(mfmsr()))
933 		return;
934 
935 	giveup_all(container_of(thr, struct task_struct, thread));
936 
937 	tm_reclaim(thr, cause);
938 
939 	/*
940 	 * If we are in a transaction and FP is off then we can't have
941 	 * used FP inside that transaction. Hence the checkpointed
942 	 * state is the same as the live state. We need to copy the
943 	 * live state to the checkpointed state so that when the
944 	 * transaction is restored, the checkpointed state is correct
945 	 * and the aborted transaction sees the correct state. We use
946 	 * ckpt_regs.msr here as that's what tm_reclaim will use to
947 	 * determine if it's going to write the checkpointed state or
948 	 * not. So either this will write the checkpointed registers,
949 	 * or reclaim will. Similarly for VMX.
950 	 */
951 	if ((thr->ckpt_regs.msr & MSR_FP) == 0)
952 		memcpy(&thr->ckfp_state, &thr->fp_state,
953 		       sizeof(struct thread_fp_state));
954 	if ((thr->ckpt_regs.msr & MSR_VEC) == 0)
955 		memcpy(&thr->ckvr_state, &thr->vr_state,
956 		       sizeof(struct thread_vr_state));
957 }
958 
959 void tm_reclaim_current(uint8_t cause)
960 {
961 	tm_enable();
962 	tm_reclaim_thread(&current->thread, cause);
963 }
964 
965 static inline void tm_reclaim_task(struct task_struct *tsk)
966 {
967 	/* We have to work out if we're switching from/to a task that's in the
968 	 * middle of a transaction.
969 	 *
970 	 * In switching we need to maintain a 2nd register state as
971 	 * oldtask->thread.ckpt_regs.  We tm_reclaim(oldproc); this saves the
972 	 * checkpointed (tbegin) state in ckpt_regs, ckfp_state and
973 	 * ckvr_state
974 	 *
975 	 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
976 	 */
977 	struct thread_struct *thr = &tsk->thread;
978 
979 	if (!thr->regs)
980 		return;
981 
982 	if (!MSR_TM_ACTIVE(thr->regs->msr))
983 		goto out_and_saveregs;
984 
985 	WARN_ON(tm_suspend_disabled);
986 
987 	TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
988 		 "ccr=%lx, msr=%lx, trap=%lx)\n",
989 		 tsk->pid, thr->regs->nip,
990 		 thr->regs->ccr, thr->regs->msr,
991 		 thr->regs->trap);
992 
993 	tm_reclaim_thread(thr, TM_CAUSE_RESCHED);
994 
995 	TM_DEBUG("--- tm_reclaim on pid %d complete\n",
996 		 tsk->pid);
997 
998 out_and_saveregs:
999 	/* Always save the regs here, even if a transaction's not active.
1000 	 * This context-switches a thread's TM info SPRs.  We do it here to
1001 	 * be consistent with the restore path (in recheckpoint) which
1002 	 * cannot happen later in _switch().
1003 	 */
1004 	tm_save_sprs(thr);
1005 }
1006 
1007 extern void __tm_recheckpoint(struct thread_struct *thread);
1008 
1009 void tm_recheckpoint(struct thread_struct *thread)
1010 {
1011 	unsigned long flags;
1012 
1013 	if (!(thread->regs->msr & MSR_TM))
1014 		return;
1015 
1016 	/* We really can't be interrupted here as the TEXASR registers can't
1017 	 * change and later in the trecheckpoint code, we have a userspace R1.
1018 	 * So let's hard disable over this region.
1019 	 */
1020 	local_irq_save(flags);
1021 	hard_irq_disable();
1022 
1023 	/* The TM SPRs are restored here, so that TEXASR.FS can be set
1024 	 * before the trecheckpoint and no explosion occurs.
1025 	 */
1026 	tm_restore_sprs(thread);
1027 
1028 	__tm_recheckpoint(thread);
1029 
1030 	local_irq_restore(flags);
1031 }
1032 
1033 static inline void tm_recheckpoint_new_task(struct task_struct *new)
1034 {
1035 	if (!cpu_has_feature(CPU_FTR_TM))
1036 		return;
1037 
1038 	/* Recheckpoint the registers of the thread we're about to switch to.
1039 	 *
1040 	 * If the task was using FP, we non-lazily reload both the original and
1041 	 * the speculative FP register states.  This is because the kernel
1042 	 * doesn't see if/when a TM rollback occurs, so if we take an FP
1043 	 * unavailable later, we are unable to determine which set of FP regs
1044 	 * need to be restored.
1045 	 */
1046 	if (!tm_enabled(new))
1047 		return;
1048 
1049 	if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
1050 		tm_restore_sprs(&new->thread);
1051 		return;
1052 	}
1053 	/* Recheckpoint to restore original checkpointed register state. */
1054 	TM_DEBUG("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n",
1055 		 new->pid, new->thread.regs->msr);
1056 
1057 	tm_recheckpoint(&new->thread);
1058 
1059 	/*
1060 	 * The checkpointed state has been restored but the live state has
1061 	 * not, ensure all the math functionality is turned off to trigger
1062 	 * restore_math() to reload.
1063 	 */
1064 	new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX);
1065 
1066 	TM_DEBUG("*** tm_recheckpoint of pid %d complete "
1067 		 "(kernel msr 0x%lx)\n",
1068 		 new->pid, mfmsr());
1069 }
1070 
1071 static inline void __switch_to_tm(struct task_struct *prev,
1072 		struct task_struct *new)
1073 {
1074 	if (cpu_has_feature(CPU_FTR_TM)) {
1075 		if (tm_enabled(prev) || tm_enabled(new))
1076 			tm_enable();
1077 
1078 		if (tm_enabled(prev)) {
1079 			prev->thread.load_tm++;
1080 			tm_reclaim_task(prev);
1081 			if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0)
1082 				prev->thread.regs->msr &= ~MSR_TM;
1083 		}
1084 
1085 		tm_recheckpoint_new_task(new);
1086 	}
1087 }
1088 
1089 /*
1090  * This is called if we are on the way out to userspace and the
1091  * TIF_RESTORE_TM flag is set.  It checks if we need to reload
1092  * FP and/or vector state and does so if necessary.
1093  * If userspace is inside a transaction (whether active or
1094  * suspended) and FP/VMX/VSX instructions have ever been enabled
1095  * inside that transaction, then we have to keep them enabled
1096  * and keep the FP/VMX/VSX state loaded while ever the transaction
1097  * continues.  The reason is that if we didn't, and subsequently
1098  * got a FP/VMX/VSX unavailable interrupt inside a transaction,
1099  * we don't know whether it's the same transaction, and thus we
1100  * don't know which of the checkpointed state and the transactional
1101  * state to use.
1102  */
1103 void restore_tm_state(struct pt_regs *regs)
1104 {
1105 	unsigned long msr_diff;
1106 
1107 	/*
1108 	 * This is the only moment we should clear TIF_RESTORE_TM as
1109 	 * it is here that ckpt_regs.msr and pt_regs.msr become the same
1110 	 * again, anything else could lead to an incorrect ckpt_msr being
1111 	 * saved and therefore incorrect signal contexts.
1112 	 */
1113 	clear_thread_flag(TIF_RESTORE_TM);
1114 	if (!MSR_TM_ACTIVE(regs->msr))
1115 		return;
1116 
1117 	msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
1118 	msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
1119 
1120 	/* Ensure that restore_math() will restore */
1121 	if (msr_diff & MSR_FP)
1122 		current->thread.load_fp = 1;
1123 #ifdef CONFIG_ALTIVEC
1124 	if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC)
1125 		current->thread.load_vec = 1;
1126 #endif
1127 	restore_math(regs);
1128 
1129 	regs->msr |= msr_diff;
1130 }
1131 
1132 #else
1133 #define tm_recheckpoint_new_task(new)
1134 #define __switch_to_tm(prev, new)
1135 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1136 
1137 static inline void save_sprs(struct thread_struct *t)
1138 {
1139 #ifdef CONFIG_ALTIVEC
1140 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
1141 		t->vrsave = mfspr(SPRN_VRSAVE);
1142 #endif
1143 #ifdef CONFIG_PPC_BOOK3S_64
1144 	if (cpu_has_feature(CPU_FTR_DSCR))
1145 		t->dscr = mfspr(SPRN_DSCR);
1146 
1147 	if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1148 		t->bescr = mfspr(SPRN_BESCR);
1149 		t->ebbhr = mfspr(SPRN_EBBHR);
1150 		t->ebbrr = mfspr(SPRN_EBBRR);
1151 
1152 		t->fscr = mfspr(SPRN_FSCR);
1153 
1154 		/*
1155 		 * Note that the TAR is not available for use in the kernel.
1156 		 * (To provide this, the TAR should be backed up/restored on
1157 		 * exception entry/exit instead, and be in pt_regs.  FIXME,
1158 		 * this should be in pt_regs anyway (for debug).)
1159 		 */
1160 		t->tar = mfspr(SPRN_TAR);
1161 	}
1162 #endif
1163 
1164 	thread_pkey_regs_save(t);
1165 }
1166 
1167 static inline void restore_sprs(struct thread_struct *old_thread,
1168 				struct thread_struct *new_thread)
1169 {
1170 #ifdef CONFIG_ALTIVEC
1171 	if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
1172 	    old_thread->vrsave != new_thread->vrsave)
1173 		mtspr(SPRN_VRSAVE, new_thread->vrsave);
1174 #endif
1175 #ifdef CONFIG_PPC_BOOK3S_64
1176 	if (cpu_has_feature(CPU_FTR_DSCR)) {
1177 		u64 dscr = get_paca()->dscr_default;
1178 		if (new_thread->dscr_inherit)
1179 			dscr = new_thread->dscr;
1180 
1181 		if (old_thread->dscr != dscr)
1182 			mtspr(SPRN_DSCR, dscr);
1183 	}
1184 
1185 	if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1186 		if (old_thread->bescr != new_thread->bescr)
1187 			mtspr(SPRN_BESCR, new_thread->bescr);
1188 		if (old_thread->ebbhr != new_thread->ebbhr)
1189 			mtspr(SPRN_EBBHR, new_thread->ebbhr);
1190 		if (old_thread->ebbrr != new_thread->ebbrr)
1191 			mtspr(SPRN_EBBRR, new_thread->ebbrr);
1192 
1193 		if (old_thread->fscr != new_thread->fscr)
1194 			mtspr(SPRN_FSCR, new_thread->fscr);
1195 
1196 		if (old_thread->tar != new_thread->tar)
1197 			mtspr(SPRN_TAR, new_thread->tar);
1198 	}
1199 
1200 	if (cpu_has_feature(CPU_FTR_P9_TIDR) &&
1201 	    old_thread->tidr != new_thread->tidr)
1202 		mtspr(SPRN_TIDR, new_thread->tidr);
1203 #endif
1204 
1205 	thread_pkey_regs_restore(new_thread, old_thread);
1206 }
1207 
1208 struct task_struct *__switch_to(struct task_struct *prev,
1209 	struct task_struct *new)
1210 {
1211 	struct thread_struct *new_thread, *old_thread;
1212 	struct task_struct *last;
1213 #ifdef CONFIG_PPC_BOOK3S_64
1214 	struct ppc64_tlb_batch *batch;
1215 #endif
1216 
1217 	new_thread = &new->thread;
1218 	old_thread = &current->thread;
1219 
1220 	WARN_ON(!irqs_disabled());
1221 
1222 #ifdef CONFIG_PPC_BOOK3S_64
1223 	batch = this_cpu_ptr(&ppc64_tlb_batch);
1224 	if (batch->active) {
1225 		current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1226 		if (batch->index)
1227 			__flush_tlb_pending(batch);
1228 		batch->active = 0;
1229 	}
1230 #endif /* CONFIG_PPC_BOOK3S_64 */
1231 
1232 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1233 	switch_booke_debug_regs(&new->thread.debug);
1234 #else
1235 /*
1236  * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1237  * schedule DABR
1238  */
1239 #ifndef CONFIG_HAVE_HW_BREAKPOINT
1240 	switch_hw_breakpoint(new);
1241 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1242 #endif
1243 
1244 	/*
1245 	 * We need to save SPRs before treclaim/trecheckpoint as these will
1246 	 * change a number of them.
1247 	 */
1248 	save_sprs(&prev->thread);
1249 
1250 	/* Save FPU, Altivec, VSX and SPE state */
1251 	giveup_all(prev);
1252 
1253 	__switch_to_tm(prev, new);
1254 
1255 	if (!radix_enabled()) {
1256 		/*
1257 		 * We can't take a PMU exception inside _switch() since there
1258 		 * is a window where the kernel stack SLB and the kernel stack
1259 		 * are out of sync. Hard disable here.
1260 		 */
1261 		hard_irq_disable();
1262 	}
1263 
1264 	/*
1265 	 * Call restore_sprs() before calling _switch(). If we move it after
1266 	 * _switch() then we miss out on calling it for new tasks. The reason
1267 	 * for this is we manually create a stack frame for new tasks that
1268 	 * directly returns through ret_from_fork() or
1269 	 * ret_from_kernel_thread(). See copy_thread() for details.
1270 	 */
1271 	restore_sprs(old_thread, new_thread);
1272 
1273 	last = _switch(old_thread, new_thread);
1274 
1275 #ifdef CONFIG_PPC_BOOK3S_64
1276 	if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1277 		current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
1278 		batch = this_cpu_ptr(&ppc64_tlb_batch);
1279 		batch->active = 1;
1280 	}
1281 
1282 	if (current->thread.regs) {
1283 		restore_math(current->thread.regs);
1284 
1285 		/*
1286 		 * On POWER9 the copy-paste buffer can only paste into
1287 		 * foreign real addresses, so unprivileged processes can not
1288 		 * see the data or use it in any way unless they have
1289 		 * foreign real mappings. If the new process has the foreign
1290 		 * real address mappings, we must issue a cp_abort to clear
1291 		 * any state and prevent snooping, corruption or a covert
1292 		 * channel. ISA v3.1 supports paste into local memory.
1293 		 */
1294 		if (current->mm &&
1295 			(cpu_has_feature(CPU_FTR_ARCH_31) ||
1296 			atomic_read(&current->mm->context.vas_windows)))
1297 			asm volatile(PPC_CP_ABORT);
1298 	}
1299 #endif /* CONFIG_PPC_BOOK3S_64 */
1300 
1301 	return last;
1302 }
1303 
1304 #define NR_INSN_TO_PRINT	16
1305 
1306 static void show_instructions(struct pt_regs *regs)
1307 {
1308 	int i;
1309 	unsigned long nip = regs->nip;
1310 	unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
1311 
1312 	printk("Instruction dump:");
1313 
1314 	/*
1315 	 * If we were executing with the MMU off for instructions, adjust pc
1316 	 * rather than printing XXXXXXXX.
1317 	 */
1318 	if (!IS_ENABLED(CONFIG_BOOKE) && !(regs->msr & MSR_IR)) {
1319 		pc = (unsigned long)phys_to_virt(pc);
1320 		nip = (unsigned long)phys_to_virt(regs->nip);
1321 	}
1322 
1323 	for (i = 0; i < NR_INSN_TO_PRINT; i++) {
1324 		int instr;
1325 
1326 		if (!(i % 8))
1327 			pr_cont("\n");
1328 
1329 		if (!__kernel_text_address(pc) ||
1330 		    get_kernel_nofault(instr, (const void *)pc)) {
1331 			pr_cont("XXXXXXXX ");
1332 		} else {
1333 			if (nip == pc)
1334 				pr_cont("<%08x> ", instr);
1335 			else
1336 				pr_cont("%08x ", instr);
1337 		}
1338 
1339 		pc += sizeof(int);
1340 	}
1341 
1342 	pr_cont("\n");
1343 }
1344 
1345 void show_user_instructions(struct pt_regs *regs)
1346 {
1347 	unsigned long pc;
1348 	int n = NR_INSN_TO_PRINT;
1349 	struct seq_buf s;
1350 	char buf[96]; /* enough for 8 times 9 + 2 chars */
1351 
1352 	pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
1353 
1354 	seq_buf_init(&s, buf, sizeof(buf));
1355 
1356 	while (n) {
1357 		int i;
1358 
1359 		seq_buf_clear(&s);
1360 
1361 		for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) {
1362 			int instr;
1363 
1364 			if (copy_from_user_nofault(&instr, (void __user *)pc,
1365 					sizeof(instr))) {
1366 				seq_buf_printf(&s, "XXXXXXXX ");
1367 				continue;
1368 			}
1369 			seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr);
1370 		}
1371 
1372 		if (!seq_buf_has_overflowed(&s))
1373 			pr_info("%s[%d]: code: %s\n", current->comm,
1374 				current->pid, s.buffer);
1375 	}
1376 }
1377 
1378 struct regbit {
1379 	unsigned long bit;
1380 	const char *name;
1381 };
1382 
1383 static struct regbit msr_bits[] = {
1384 #if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
1385 	{MSR_SF,	"SF"},
1386 	{MSR_HV,	"HV"},
1387 #endif
1388 	{MSR_VEC,	"VEC"},
1389 	{MSR_VSX,	"VSX"},
1390 #ifdef CONFIG_BOOKE
1391 	{MSR_CE,	"CE"},
1392 #endif
1393 	{MSR_EE,	"EE"},
1394 	{MSR_PR,	"PR"},
1395 	{MSR_FP,	"FP"},
1396 	{MSR_ME,	"ME"},
1397 #ifdef CONFIG_BOOKE
1398 	{MSR_DE,	"DE"},
1399 #else
1400 	{MSR_SE,	"SE"},
1401 	{MSR_BE,	"BE"},
1402 #endif
1403 	{MSR_IR,	"IR"},
1404 	{MSR_DR,	"DR"},
1405 	{MSR_PMM,	"PMM"},
1406 #ifndef CONFIG_BOOKE
1407 	{MSR_RI,	"RI"},
1408 	{MSR_LE,	"LE"},
1409 #endif
1410 	{0,		NULL}
1411 };
1412 
1413 static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
1414 {
1415 	const char *s = "";
1416 
1417 	for (; bits->bit; ++bits)
1418 		if (val & bits->bit) {
1419 			pr_cont("%s%s", s, bits->name);
1420 			s = sep;
1421 		}
1422 }
1423 
1424 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1425 static struct regbit msr_tm_bits[] = {
1426 	{MSR_TS_T,	"T"},
1427 	{MSR_TS_S,	"S"},
1428 	{MSR_TM,	"E"},
1429 	{0,		NULL}
1430 };
1431 
1432 static void print_tm_bits(unsigned long val)
1433 {
1434 /*
1435  * This only prints something if at least one of the TM bit is set.
1436  * Inside the TM[], the output means:
1437  *   E: Enabled		(bit 32)
1438  *   S: Suspended	(bit 33)
1439  *   T: Transactional	(bit 34)
1440  */
1441 	if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
1442 		pr_cont(",TM[");
1443 		print_bits(val, msr_tm_bits, "");
1444 		pr_cont("]");
1445 	}
1446 }
1447 #else
1448 static void print_tm_bits(unsigned long val) {}
1449 #endif
1450 
1451 static void print_msr_bits(unsigned long val)
1452 {
1453 	pr_cont("<");
1454 	print_bits(val, msr_bits, ",");
1455 	print_tm_bits(val);
1456 	pr_cont(">");
1457 }
1458 
1459 #ifdef CONFIG_PPC64
1460 #define REG		"%016lx"
1461 #define REGS_PER_LINE	4
1462 #define LAST_VOLATILE	13
1463 #else
1464 #define REG		"%08lx"
1465 #define REGS_PER_LINE	8
1466 #define LAST_VOLATILE	12
1467 #endif
1468 
1469 static void __show_regs(struct pt_regs *regs)
1470 {
1471 	int i, trap;
1472 
1473 	printk("NIP:  "REG" LR: "REG" CTR: "REG"\n",
1474 	       regs->nip, regs->link, regs->ctr);
1475 	printk("REGS: %px TRAP: %04lx   %s  (%s)\n",
1476 	       regs, regs->trap, print_tainted(), init_utsname()->release);
1477 	printk("MSR:  "REG" ", regs->msr);
1478 	print_msr_bits(regs->msr);
1479 	pr_cont("  CR: %08lx  XER: %08lx\n", regs->ccr, regs->xer);
1480 	trap = TRAP(regs);
1481 	if (!trap_is_syscall(regs) && cpu_has_feature(CPU_FTR_CFAR))
1482 		pr_cont("CFAR: "REG" ", regs->orig_gpr3);
1483 	if (trap == 0x200 || trap == 0x300 || trap == 0x600) {
1484 		if (IS_ENABLED(CONFIG_4xx) || IS_ENABLED(CONFIG_BOOKE))
1485 			pr_cont("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
1486 		else
1487 			pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1488 	}
1489 
1490 #ifdef CONFIG_PPC64
1491 	pr_cont("IRQMASK: %lx ", regs->softe);
1492 #endif
1493 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1494 	if (MSR_TM_ACTIVE(regs->msr))
1495 		pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
1496 #endif
1497 
1498 	for (i = 0;  i < 32;  i++) {
1499 		if ((i % REGS_PER_LINE) == 0)
1500 			pr_cont("\nGPR%02d: ", i);
1501 		pr_cont(REG " ", regs->gpr[i]);
1502 		if (i == LAST_VOLATILE && !FULL_REGS(regs))
1503 			break;
1504 	}
1505 	pr_cont("\n");
1506 	/*
1507 	 * Lookup NIP late so we have the best change of getting the
1508 	 * above info out without failing
1509 	 */
1510 	if (IS_ENABLED(CONFIG_KALLSYMS)) {
1511 		printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1512 		printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
1513 	}
1514 }
1515 
1516 void show_regs(struct pt_regs *regs)
1517 {
1518 	show_regs_print_info(KERN_DEFAULT);
1519 	__show_regs(regs);
1520 	show_stack(current, (unsigned long *) regs->gpr[1], KERN_DEFAULT);
1521 	if (!user_mode(regs))
1522 		show_instructions(regs);
1523 }
1524 
1525 void flush_thread(void)
1526 {
1527 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1528 	flush_ptrace_hw_breakpoint(current);
1529 #else /* CONFIG_HAVE_HW_BREAKPOINT */
1530 	set_debug_reg_defaults(&current->thread);
1531 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1532 }
1533 
1534 void arch_setup_new_exec(void)
1535 {
1536 
1537 #ifdef CONFIG_PPC_BOOK3S_64
1538 	if (!radix_enabled())
1539 		hash__setup_new_exec();
1540 #endif
1541 	/*
1542 	 * If we exec out of a kernel thread then thread.regs will not be
1543 	 * set.  Do it now.
1544 	 */
1545 	if (!current->thread.regs) {
1546 		struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1547 		current->thread.regs = regs - 1;
1548 	}
1549 
1550 #ifdef CONFIG_PPC_MEM_KEYS
1551 	current->thread.regs->amr  = default_amr;
1552 	current->thread.regs->iamr  = default_iamr;
1553 #endif
1554 }
1555 
1556 #ifdef CONFIG_PPC64
1557 /**
1558  * Assign a TIDR (thread ID) for task @t and set it in the thread
1559  * structure. For now, we only support setting TIDR for 'current' task.
1560  *
1561  * Since the TID value is a truncated form of it PID, it is possible
1562  * (but unlikely) for 2 threads to have the same TID. In the unlikely event
1563  * that 2 threads share the same TID and are waiting, one of the following
1564  * cases will happen:
1565  *
1566  * 1. The correct thread is running, the wrong thread is not
1567  * In this situation, the correct thread is woken and proceeds to pass it's
1568  * condition check.
1569  *
1570  * 2. Neither threads are running
1571  * In this situation, neither thread will be woken. When scheduled, the waiting
1572  * threads will execute either a wait, which will return immediately, followed
1573  * by a condition check, which will pass for the correct thread and fail
1574  * for the wrong thread, or they will execute the condition check immediately.
1575  *
1576  * 3. The wrong thread is running, the correct thread is not
1577  * The wrong thread will be woken, but will fail it's condition check and
1578  * re-execute wait. The correct thread, when scheduled, will execute either
1579  * it's condition check (which will pass), or wait, which returns immediately
1580  * when called the first time after the thread is scheduled, followed by it's
1581  * condition check (which will pass).
1582  *
1583  * 4. Both threads are running
1584  * Both threads will be woken. The wrong thread will fail it's condition check
1585  * and execute another wait, while the correct thread will pass it's condition
1586  * check.
1587  *
1588  * @t: the task to set the thread ID for
1589  */
1590 int set_thread_tidr(struct task_struct *t)
1591 {
1592 	if (!cpu_has_feature(CPU_FTR_P9_TIDR))
1593 		return -EINVAL;
1594 
1595 	if (t != current)
1596 		return -EINVAL;
1597 
1598 	if (t->thread.tidr)
1599 		return 0;
1600 
1601 	t->thread.tidr = (u16)task_pid_nr(t);
1602 	mtspr(SPRN_TIDR, t->thread.tidr);
1603 
1604 	return 0;
1605 }
1606 EXPORT_SYMBOL_GPL(set_thread_tidr);
1607 
1608 #endif /* CONFIG_PPC64 */
1609 
1610 void
1611 release_thread(struct task_struct *t)
1612 {
1613 }
1614 
1615 /*
1616  * this gets called so that we can store coprocessor state into memory and
1617  * copy the current task into the new thread.
1618  */
1619 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
1620 {
1621 	flush_all_to_thread(src);
1622 	/*
1623 	 * Flush TM state out so we can copy it.  __switch_to_tm() does this
1624 	 * flush but it removes the checkpointed state from the current CPU and
1625 	 * transitions the CPU out of TM mode.  Hence we need to call
1626 	 * tm_recheckpoint_new_task() (on the same task) to restore the
1627 	 * checkpointed state back and the TM mode.
1628 	 *
1629 	 * Can't pass dst because it isn't ready. Doesn't matter, passing
1630 	 * dst is only important for __switch_to()
1631 	 */
1632 	__switch_to_tm(src, src);
1633 
1634 	*dst = *src;
1635 
1636 	clear_task_ebb(dst);
1637 
1638 	return 0;
1639 }
1640 
1641 static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1642 {
1643 #ifdef CONFIG_PPC_BOOK3S_64
1644 	unsigned long sp_vsid;
1645 	unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1646 
1647 	if (radix_enabled())
1648 		return;
1649 
1650 	if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1651 		sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1652 			<< SLB_VSID_SHIFT_1T;
1653 	else
1654 		sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1655 			<< SLB_VSID_SHIFT;
1656 	sp_vsid |= SLB_VSID_KERNEL | llp;
1657 	p->thread.ksp_vsid = sp_vsid;
1658 #endif
1659 }
1660 
1661 /*
1662  * Copy a thread..
1663  */
1664 
1665 /*
1666  * Copy architecture-specific thread state
1667  */
1668 int copy_thread(unsigned long clone_flags, unsigned long usp,
1669 		unsigned long kthread_arg, struct task_struct *p,
1670 		unsigned long tls)
1671 {
1672 	struct pt_regs *childregs, *kregs;
1673 	extern void ret_from_fork(void);
1674 	extern void ret_from_fork_scv(void);
1675 	extern void ret_from_kernel_thread(void);
1676 	void (*f)(void);
1677 	unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
1678 	struct thread_info *ti = task_thread_info(p);
1679 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1680 	int i;
1681 #endif
1682 
1683 	klp_init_thread_info(p);
1684 
1685 	/* Copy registers */
1686 	sp -= sizeof(struct pt_regs);
1687 	childregs = (struct pt_regs *) sp;
1688 	if (unlikely(p->flags & PF_KTHREAD)) {
1689 		/* kernel thread */
1690 		memset(childregs, 0, sizeof(struct pt_regs));
1691 		childregs->gpr[1] = sp + sizeof(struct pt_regs);
1692 		/* function */
1693 		if (usp)
1694 			childregs->gpr[14] = ppc_function_entry((void *)usp);
1695 #ifdef CONFIG_PPC64
1696 		clear_tsk_thread_flag(p, TIF_32BIT);
1697 		childregs->softe = IRQS_ENABLED;
1698 #endif
1699 		childregs->gpr[15] = kthread_arg;
1700 		p->thread.regs = NULL;	/* no user register state */
1701 		ti->flags |= _TIF_RESTOREALL;
1702 		f = ret_from_kernel_thread;
1703 	} else {
1704 		/* user thread */
1705 		struct pt_regs *regs = current_pt_regs();
1706 		CHECK_FULL_REGS(regs);
1707 		*childregs = *regs;
1708 		if (usp)
1709 			childregs->gpr[1] = usp;
1710 		p->thread.regs = childregs;
1711 		/* 64s sets this in ret_from_fork */
1712 		if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
1713 			childregs->gpr[3] = 0;  /* Result from fork() */
1714 		if (clone_flags & CLONE_SETTLS) {
1715 			if (!is_32bit_task())
1716 				childregs->gpr[13] = tls;
1717 			else
1718 				childregs->gpr[2] = tls;
1719 		}
1720 
1721 		if (trap_is_scv(regs))
1722 			f = ret_from_fork_scv;
1723 		else
1724 			f = ret_from_fork;
1725 	}
1726 	childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
1727 	sp -= STACK_FRAME_OVERHEAD;
1728 
1729 	/*
1730 	 * The way this works is that at some point in the future
1731 	 * some task will call _switch to switch to the new task.
1732 	 * That will pop off the stack frame created below and start
1733 	 * the new task running at ret_from_fork.  The new task will
1734 	 * do some house keeping and then return from the fork or clone
1735 	 * system call, using the stack frame created above.
1736 	 */
1737 	((unsigned long *)sp)[0] = 0;
1738 	sp -= sizeof(struct pt_regs);
1739 	kregs = (struct pt_regs *) sp;
1740 	sp -= STACK_FRAME_OVERHEAD;
1741 	p->thread.ksp = sp;
1742 #ifdef CONFIG_PPC32
1743 	p->thread.ksp_limit = (unsigned long)end_of_stack(p);
1744 #endif
1745 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1746 	for (i = 0; i < nr_wp_slots(); i++)
1747 		p->thread.ptrace_bps[i] = NULL;
1748 #endif
1749 
1750 #ifdef CONFIG_PPC_FPU_REGS
1751 	p->thread.fp_save_area = NULL;
1752 #endif
1753 #ifdef CONFIG_ALTIVEC
1754 	p->thread.vr_save_area = NULL;
1755 #endif
1756 
1757 	setup_ksp_vsid(p, sp);
1758 
1759 #ifdef CONFIG_PPC64
1760 	if (cpu_has_feature(CPU_FTR_DSCR)) {
1761 		p->thread.dscr_inherit = current->thread.dscr_inherit;
1762 		p->thread.dscr = mfspr(SPRN_DSCR);
1763 	}
1764 	if (cpu_has_feature(CPU_FTR_HAS_PPR))
1765 		childregs->ppr = DEFAULT_PPR;
1766 
1767 	p->thread.tidr = 0;
1768 #endif
1769 	/*
1770 	 * Run with the current AMR value of the kernel
1771 	 */
1772 #ifdef CONFIG_PPC_PKEY
1773 	if (mmu_has_feature(MMU_FTR_BOOK3S_KUAP))
1774 		kregs->amr = AMR_KUAP_BLOCKED;
1775 
1776 	if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP))
1777 		kregs->iamr = AMR_KUEP_BLOCKED;
1778 #endif
1779 	kregs->nip = ppc_function_entry(f);
1780 	return 0;
1781 }
1782 
1783 void preload_new_slb_context(unsigned long start, unsigned long sp);
1784 
1785 /*
1786  * Set up a thread for executing a new program
1787  */
1788 void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
1789 {
1790 #ifdef CONFIG_PPC64
1791 	unsigned long load_addr = regs->gpr[2];	/* saved by ELF_PLAT_INIT */
1792 
1793 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled())
1794 		preload_new_slb_context(start, sp);
1795 #endif
1796 
1797 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1798 	/*
1799 	 * Clear any transactional state, we're exec()ing. The cause is
1800 	 * not important as there will never be a recheckpoint so it's not
1801 	 * user visible.
1802 	 */
1803 	if (MSR_TM_SUSPENDED(mfmsr()))
1804 		tm_reclaim_current(0);
1805 #endif
1806 
1807 	memset(regs->gpr, 0, sizeof(regs->gpr));
1808 	regs->ctr = 0;
1809 	regs->link = 0;
1810 	regs->xer = 0;
1811 	regs->ccr = 0;
1812 	regs->gpr[1] = sp;
1813 
1814 	/*
1815 	 * We have just cleared all the nonvolatile GPRs, so make
1816 	 * FULL_REGS(regs) return true.  This is necessary to allow
1817 	 * ptrace to examine the thread immediately after exec.
1818 	 */
1819 	SET_FULL_REGS(regs);
1820 
1821 #ifdef CONFIG_PPC32
1822 	regs->mq = 0;
1823 	regs->nip = start;
1824 	regs->msr = MSR_USER;
1825 #else
1826 	if (!is_32bit_task()) {
1827 		unsigned long entry;
1828 
1829 		if (is_elf2_task()) {
1830 			/* Look ma, no function descriptors! */
1831 			entry = start;
1832 
1833 			/*
1834 			 * Ulrich says:
1835 			 *   The latest iteration of the ABI requires that when
1836 			 *   calling a function (at its global entry point),
1837 			 *   the caller must ensure r12 holds the entry point
1838 			 *   address (so that the function can quickly
1839 			 *   establish addressability).
1840 			 */
1841 			regs->gpr[12] = start;
1842 			/* Make sure that's restored on entry to userspace. */
1843 			set_thread_flag(TIF_RESTOREALL);
1844 		} else {
1845 			unsigned long toc;
1846 
1847 			/* start is a relocated pointer to the function
1848 			 * descriptor for the elf _start routine.  The first
1849 			 * entry in the function descriptor is the entry
1850 			 * address of _start and the second entry is the TOC
1851 			 * value we need to use.
1852 			 */
1853 			__get_user(entry, (unsigned long __user *)start);
1854 			__get_user(toc, (unsigned long __user *)start+1);
1855 
1856 			/* Check whether the e_entry function descriptor entries
1857 			 * need to be relocated before we can use them.
1858 			 */
1859 			if (load_addr != 0) {
1860 				entry += load_addr;
1861 				toc   += load_addr;
1862 			}
1863 			regs->gpr[2] = toc;
1864 		}
1865 		regs->nip = entry;
1866 		regs->msr = MSR_USER64;
1867 	} else {
1868 		regs->nip = start;
1869 		regs->gpr[2] = 0;
1870 		regs->msr = MSR_USER32;
1871 	}
1872 #endif
1873 #ifdef CONFIG_VSX
1874 	current->thread.used_vsr = 0;
1875 #endif
1876 	current->thread.load_slb = 0;
1877 	current->thread.load_fp = 0;
1878 #ifdef CONFIG_PPC_FPU_REGS
1879 	memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
1880 	current->thread.fp_save_area = NULL;
1881 #endif
1882 #ifdef CONFIG_ALTIVEC
1883 	memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1884 	current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
1885 	current->thread.vr_save_area = NULL;
1886 	current->thread.vrsave = 0;
1887 	current->thread.used_vr = 0;
1888 	current->thread.load_vec = 0;
1889 #endif /* CONFIG_ALTIVEC */
1890 #ifdef CONFIG_SPE
1891 	memset(current->thread.evr, 0, sizeof(current->thread.evr));
1892 	current->thread.acc = 0;
1893 	current->thread.spefscr = 0;
1894 	current->thread.used_spe = 0;
1895 #endif /* CONFIG_SPE */
1896 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1897 	current->thread.tm_tfhar = 0;
1898 	current->thread.tm_texasr = 0;
1899 	current->thread.tm_tfiar = 0;
1900 	current->thread.load_tm = 0;
1901 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1902 
1903 }
1904 EXPORT_SYMBOL(start_thread);
1905 
1906 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1907 		| PR_FP_EXC_RES | PR_FP_EXC_INV)
1908 
1909 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1910 {
1911 	struct pt_regs *regs = tsk->thread.regs;
1912 
1913 	/* This is a bit hairy.  If we are an SPE enabled  processor
1914 	 * (have embedded fp) we store the IEEE exception enable flags in
1915 	 * fpexc_mode.  fpexc_mode is also used for setting FP exception
1916 	 * mode (asyn, precise, disabled) for 'Classic' FP. */
1917 	if (val & PR_FP_EXC_SW_ENABLE) {
1918 		if (cpu_has_feature(CPU_FTR_SPE)) {
1919 			/*
1920 			 * When the sticky exception bits are set
1921 			 * directly by userspace, it must call prctl
1922 			 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1923 			 * in the existing prctl settings) or
1924 			 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1925 			 * the bits being set).  <fenv.h> functions
1926 			 * saving and restoring the whole
1927 			 * floating-point environment need to do so
1928 			 * anyway to restore the prctl settings from
1929 			 * the saved environment.
1930 			 */
1931 #ifdef CONFIG_SPE
1932 			tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
1933 			tsk->thread.fpexc_mode = val &
1934 				(PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1935 #endif
1936 			return 0;
1937 		} else {
1938 			return -EINVAL;
1939 		}
1940 	}
1941 
1942 	/* on a CONFIG_SPE this does not hurt us.  The bits that
1943 	 * __pack_fe01 use do not overlap with bits used for
1944 	 * PR_FP_EXC_SW_ENABLE.  Additionally, the MSR[FE0,FE1] bits
1945 	 * on CONFIG_SPE implementations are reserved so writing to
1946 	 * them does not change anything */
1947 	if (val > PR_FP_EXC_PRECISE)
1948 		return -EINVAL;
1949 	tsk->thread.fpexc_mode = __pack_fe01(val);
1950 	if (regs != NULL && (regs->msr & MSR_FP) != 0)
1951 		regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1952 			| tsk->thread.fpexc_mode;
1953 	return 0;
1954 }
1955 
1956 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1957 {
1958 	unsigned int val = 0;
1959 
1960 	if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE) {
1961 		if (cpu_has_feature(CPU_FTR_SPE)) {
1962 			/*
1963 			 * When the sticky exception bits are set
1964 			 * directly by userspace, it must call prctl
1965 			 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1966 			 * in the existing prctl settings) or
1967 			 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1968 			 * the bits being set).  <fenv.h> functions
1969 			 * saving and restoring the whole
1970 			 * floating-point environment need to do so
1971 			 * anyway to restore the prctl settings from
1972 			 * the saved environment.
1973 			 */
1974 #ifdef CONFIG_SPE
1975 			tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
1976 			val = tsk->thread.fpexc_mode;
1977 #endif
1978 		} else
1979 			return -EINVAL;
1980 	} else {
1981 		val = __unpack_fe01(tsk->thread.fpexc_mode);
1982 	}
1983 	return put_user(val, (unsigned int __user *) adr);
1984 }
1985 
1986 int set_endian(struct task_struct *tsk, unsigned int val)
1987 {
1988 	struct pt_regs *regs = tsk->thread.regs;
1989 
1990 	if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1991 	    (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1992 		return -EINVAL;
1993 
1994 	if (regs == NULL)
1995 		return -EINVAL;
1996 
1997 	if (val == PR_ENDIAN_BIG)
1998 		regs->msr &= ~MSR_LE;
1999 	else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
2000 		regs->msr |= MSR_LE;
2001 	else
2002 		return -EINVAL;
2003 
2004 	return 0;
2005 }
2006 
2007 int get_endian(struct task_struct *tsk, unsigned long adr)
2008 {
2009 	struct pt_regs *regs = tsk->thread.regs;
2010 	unsigned int val;
2011 
2012 	if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
2013 	    !cpu_has_feature(CPU_FTR_REAL_LE))
2014 		return -EINVAL;
2015 
2016 	if (regs == NULL)
2017 		return -EINVAL;
2018 
2019 	if (regs->msr & MSR_LE) {
2020 		if (cpu_has_feature(CPU_FTR_REAL_LE))
2021 			val = PR_ENDIAN_LITTLE;
2022 		else
2023 			val = PR_ENDIAN_PPC_LITTLE;
2024 	} else
2025 		val = PR_ENDIAN_BIG;
2026 
2027 	return put_user(val, (unsigned int __user *)adr);
2028 }
2029 
2030 int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
2031 {
2032 	tsk->thread.align_ctl = val;
2033 	return 0;
2034 }
2035 
2036 int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
2037 {
2038 	return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
2039 }
2040 
2041 static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
2042 				  unsigned long nbytes)
2043 {
2044 	unsigned long stack_page;
2045 	unsigned long cpu = task_cpu(p);
2046 
2047 	stack_page = (unsigned long)hardirq_ctx[cpu];
2048 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2049 		return 1;
2050 
2051 	stack_page = (unsigned long)softirq_ctx[cpu];
2052 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2053 		return 1;
2054 
2055 	return 0;
2056 }
2057 
2058 static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p,
2059 					unsigned long nbytes)
2060 {
2061 #ifdef CONFIG_PPC64
2062 	unsigned long stack_page;
2063 	unsigned long cpu = task_cpu(p);
2064 
2065 	stack_page = (unsigned long)paca_ptrs[cpu]->emergency_sp - THREAD_SIZE;
2066 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2067 		return 1;
2068 
2069 # ifdef CONFIG_PPC_BOOK3S_64
2070 	stack_page = (unsigned long)paca_ptrs[cpu]->nmi_emergency_sp - THREAD_SIZE;
2071 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2072 		return 1;
2073 
2074 	stack_page = (unsigned long)paca_ptrs[cpu]->mc_emergency_sp - THREAD_SIZE;
2075 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2076 		return 1;
2077 # endif
2078 #endif
2079 
2080 	return 0;
2081 }
2082 
2083 
2084 int validate_sp(unsigned long sp, struct task_struct *p,
2085 		       unsigned long nbytes)
2086 {
2087 	unsigned long stack_page = (unsigned long)task_stack_page(p);
2088 
2089 	if (sp < THREAD_SIZE)
2090 		return 0;
2091 
2092 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2093 		return 1;
2094 
2095 	if (valid_irq_stack(sp, p, nbytes))
2096 		return 1;
2097 
2098 	return valid_emergency_stack(sp, p, nbytes);
2099 }
2100 
2101 EXPORT_SYMBOL(validate_sp);
2102 
2103 static unsigned long __get_wchan(struct task_struct *p)
2104 {
2105 	unsigned long ip, sp;
2106 	int count = 0;
2107 
2108 	if (!p || p == current || p->state == TASK_RUNNING)
2109 		return 0;
2110 
2111 	sp = p->thread.ksp;
2112 	if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
2113 		return 0;
2114 
2115 	do {
2116 		sp = *(unsigned long *)sp;
2117 		if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD) ||
2118 		    p->state == TASK_RUNNING)
2119 			return 0;
2120 		if (count > 0) {
2121 			ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
2122 			if (!in_sched_functions(ip))
2123 				return ip;
2124 		}
2125 	} while (count++ < 16);
2126 	return 0;
2127 }
2128 
2129 unsigned long get_wchan(struct task_struct *p)
2130 {
2131 	unsigned long ret;
2132 
2133 	if (!try_get_task_stack(p))
2134 		return 0;
2135 
2136 	ret = __get_wchan(p);
2137 
2138 	put_task_stack(p);
2139 
2140 	return ret;
2141 }
2142 
2143 static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
2144 
2145 void show_stack(struct task_struct *tsk, unsigned long *stack,
2146 		const char *loglvl)
2147 {
2148 	unsigned long sp, ip, lr, newsp;
2149 	int count = 0;
2150 	int firstframe = 1;
2151 	unsigned long ret_addr;
2152 	int ftrace_idx = 0;
2153 
2154 	if (tsk == NULL)
2155 		tsk = current;
2156 
2157 	if (!try_get_task_stack(tsk))
2158 		return;
2159 
2160 	sp = (unsigned long) stack;
2161 	if (sp == 0) {
2162 		if (tsk == current)
2163 			sp = current_stack_frame();
2164 		else
2165 			sp = tsk->thread.ksp;
2166 	}
2167 
2168 	lr = 0;
2169 	printk("%sCall Trace:\n", loglvl);
2170 	do {
2171 		if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
2172 			break;
2173 
2174 		stack = (unsigned long *) sp;
2175 		newsp = stack[0];
2176 		ip = stack[STACK_FRAME_LR_SAVE];
2177 		if (!firstframe || ip != lr) {
2178 			printk("%s["REG"] ["REG"] %pS",
2179 				loglvl, sp, ip, (void *)ip);
2180 			ret_addr = ftrace_graph_ret_addr(current,
2181 						&ftrace_idx, ip, stack);
2182 			if (ret_addr != ip)
2183 				pr_cont(" (%pS)", (void *)ret_addr);
2184 			if (firstframe)
2185 				pr_cont(" (unreliable)");
2186 			pr_cont("\n");
2187 		}
2188 		firstframe = 0;
2189 
2190 		/*
2191 		 * See if this is an exception frame.
2192 		 * We look for the "regshere" marker in the current frame.
2193 		 */
2194 		if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
2195 		    && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
2196 			struct pt_regs *regs = (struct pt_regs *)
2197 				(sp + STACK_FRAME_OVERHEAD);
2198 
2199 			lr = regs->link;
2200 			printk("%s--- interrupt: %lx at %pS\n",
2201 			       loglvl, regs->trap, (void *)regs->nip);
2202 			__show_regs(regs);
2203 			printk("%s--- interrupt: %lx\n",
2204 			       loglvl, regs->trap);
2205 
2206 			firstframe = 1;
2207 		}
2208 
2209 		sp = newsp;
2210 	} while (count++ < kstack_depth_to_print);
2211 
2212 	put_task_stack(tsk);
2213 }
2214 
2215 #ifdef CONFIG_PPC64
2216 /* Called with hard IRQs off */
2217 void notrace __ppc64_runlatch_on(void)
2218 {
2219 	struct thread_info *ti = current_thread_info();
2220 
2221 	if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2222 		/*
2223 		 * Least significant bit (RUN) is the only writable bit of
2224 		 * the CTRL register, so we can avoid mfspr. 2.06 is not the
2225 		 * earliest ISA where this is the case, but it's convenient.
2226 		 */
2227 		mtspr(SPRN_CTRLT, CTRL_RUNLATCH);
2228 	} else {
2229 		unsigned long ctrl;
2230 
2231 		/*
2232 		 * Some architectures (e.g., Cell) have writable fields other
2233 		 * than RUN, so do the read-modify-write.
2234 		 */
2235 		ctrl = mfspr(SPRN_CTRLF);
2236 		ctrl |= CTRL_RUNLATCH;
2237 		mtspr(SPRN_CTRLT, ctrl);
2238 	}
2239 
2240 	ti->local_flags |= _TLF_RUNLATCH;
2241 }
2242 
2243 /* Called with hard IRQs off */
2244 void notrace __ppc64_runlatch_off(void)
2245 {
2246 	struct thread_info *ti = current_thread_info();
2247 
2248 	ti->local_flags &= ~_TLF_RUNLATCH;
2249 
2250 	if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2251 		mtspr(SPRN_CTRLT, 0);
2252 	} else {
2253 		unsigned long ctrl;
2254 
2255 		ctrl = mfspr(SPRN_CTRLF);
2256 		ctrl &= ~CTRL_RUNLATCH;
2257 		mtspr(SPRN_CTRLT, ctrl);
2258 	}
2259 }
2260 #endif /* CONFIG_PPC64 */
2261 
2262 unsigned long arch_align_stack(unsigned long sp)
2263 {
2264 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2265 		sp -= get_random_int() & ~PAGE_MASK;
2266 	return sp & ~0xf;
2267 }
2268 
2269 static inline unsigned long brk_rnd(void)
2270 {
2271         unsigned long rnd = 0;
2272 
2273 	/* 8MB for 32bit, 1GB for 64bit */
2274 	if (is_32bit_task())
2275 		rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT)));
2276 	else
2277 		rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT)));
2278 
2279 	return rnd << PAGE_SHIFT;
2280 }
2281 
2282 unsigned long arch_randomize_brk(struct mm_struct *mm)
2283 {
2284 	unsigned long base = mm->brk;
2285 	unsigned long ret;
2286 
2287 #ifdef CONFIG_PPC_BOOK3S_64
2288 	/*
2289 	 * If we are using 1TB segments and we are allowed to randomise
2290 	 * the heap, we can put it above 1TB so it is backed by a 1TB
2291 	 * segment. Otherwise the heap will be in the bottom 1TB
2292 	 * which always uses 256MB segments and this may result in a
2293 	 * performance penalty. We don't need to worry about radix. For
2294 	 * radix, mmu_highuser_ssize remains unchanged from 256MB.
2295 	 */
2296 	if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
2297 		base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
2298 #endif
2299 
2300 	ret = PAGE_ALIGN(base + brk_rnd());
2301 
2302 	if (ret < mm->brk)
2303 		return mm->brk;
2304 
2305 	return ret;
2306 }
2307 
2308