xref: /openbmc/linux/arch/powerpc/kernel/process.c (revision abe9af53)
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 void show_regs(struct pt_regs * regs)
1470 {
1471 	int i, trap;
1472 
1473 	show_regs_print_info(KERN_DEFAULT);
1474 
1475 	printk("NIP:  "REG" LR: "REG" CTR: "REG"\n",
1476 	       regs->nip, regs->link, regs->ctr);
1477 	printk("REGS: %px TRAP: %04lx   %s  (%s)\n",
1478 	       regs, regs->trap, print_tainted(), init_utsname()->release);
1479 	printk("MSR:  "REG" ", regs->msr);
1480 	print_msr_bits(regs->msr);
1481 	pr_cont("  CR: %08lx  XER: %08lx\n", regs->ccr, regs->xer);
1482 	trap = TRAP(regs);
1483 	if (!trap_is_syscall(regs) && cpu_has_feature(CPU_FTR_CFAR))
1484 		pr_cont("CFAR: "REG" ", regs->orig_gpr3);
1485 	if (trap == 0x200 || trap == 0x300 || trap == 0x600) {
1486 		if (IS_ENABLED(CONFIG_4xx) || IS_ENABLED(CONFIG_BOOKE))
1487 			pr_cont("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
1488 		else
1489 			pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1490 	}
1491 
1492 #ifdef CONFIG_PPC64
1493 	pr_cont("IRQMASK: %lx ", regs->softe);
1494 #endif
1495 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1496 	if (MSR_TM_ACTIVE(regs->msr))
1497 		pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
1498 #endif
1499 
1500 	for (i = 0;  i < 32;  i++) {
1501 		if ((i % REGS_PER_LINE) == 0)
1502 			pr_cont("\nGPR%02d: ", i);
1503 		pr_cont(REG " ", regs->gpr[i]);
1504 		if (i == LAST_VOLATILE && !FULL_REGS(regs))
1505 			break;
1506 	}
1507 	pr_cont("\n");
1508 	/*
1509 	 * Lookup NIP late so we have the best change of getting the
1510 	 * above info out without failing
1511 	 */
1512 	if (IS_ENABLED(CONFIG_KALLSYMS)) {
1513 		printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1514 		printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
1515 	}
1516 	show_stack(current, (unsigned long *) regs->gpr[1], KERN_DEFAULT);
1517 	if (!user_mode(regs))
1518 		show_instructions(regs);
1519 }
1520 
1521 void flush_thread(void)
1522 {
1523 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1524 	flush_ptrace_hw_breakpoint(current);
1525 #else /* CONFIG_HAVE_HW_BREAKPOINT */
1526 	set_debug_reg_defaults(&current->thread);
1527 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1528 }
1529 
1530 #ifdef CONFIG_PPC_BOOK3S_64
1531 void arch_setup_new_exec(void)
1532 {
1533 	if (radix_enabled())
1534 		return;
1535 	hash__setup_new_exec();
1536 }
1537 #endif
1538 
1539 #ifdef CONFIG_PPC64
1540 /**
1541  * Assign a TIDR (thread ID) for task @t and set it in the thread
1542  * structure. For now, we only support setting TIDR for 'current' task.
1543  *
1544  * Since the TID value is a truncated form of it PID, it is possible
1545  * (but unlikely) for 2 threads to have the same TID. In the unlikely event
1546  * that 2 threads share the same TID and are waiting, one of the following
1547  * cases will happen:
1548  *
1549  * 1. The correct thread is running, the wrong thread is not
1550  * In this situation, the correct thread is woken and proceeds to pass it's
1551  * condition check.
1552  *
1553  * 2. Neither threads are running
1554  * In this situation, neither thread will be woken. When scheduled, the waiting
1555  * threads will execute either a wait, which will return immediately, followed
1556  * by a condition check, which will pass for the correct thread and fail
1557  * for the wrong thread, or they will execute the condition check immediately.
1558  *
1559  * 3. The wrong thread is running, the correct thread is not
1560  * The wrong thread will be woken, but will fail it's condition check and
1561  * re-execute wait. The correct thread, when scheduled, will execute either
1562  * it's condition check (which will pass), or wait, which returns immediately
1563  * when called the first time after the thread is scheduled, followed by it's
1564  * condition check (which will pass).
1565  *
1566  * 4. Both threads are running
1567  * Both threads will be woken. The wrong thread will fail it's condition check
1568  * and execute another wait, while the correct thread will pass it's condition
1569  * check.
1570  *
1571  * @t: the task to set the thread ID for
1572  */
1573 int set_thread_tidr(struct task_struct *t)
1574 {
1575 	if (!cpu_has_feature(CPU_FTR_P9_TIDR))
1576 		return -EINVAL;
1577 
1578 	if (t != current)
1579 		return -EINVAL;
1580 
1581 	if (t->thread.tidr)
1582 		return 0;
1583 
1584 	t->thread.tidr = (u16)task_pid_nr(t);
1585 	mtspr(SPRN_TIDR, t->thread.tidr);
1586 
1587 	return 0;
1588 }
1589 EXPORT_SYMBOL_GPL(set_thread_tidr);
1590 
1591 #endif /* CONFIG_PPC64 */
1592 
1593 void
1594 release_thread(struct task_struct *t)
1595 {
1596 }
1597 
1598 /*
1599  * this gets called so that we can store coprocessor state into memory and
1600  * copy the current task into the new thread.
1601  */
1602 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
1603 {
1604 	flush_all_to_thread(src);
1605 	/*
1606 	 * Flush TM state out so we can copy it.  __switch_to_tm() does this
1607 	 * flush but it removes the checkpointed state from the current CPU and
1608 	 * transitions the CPU out of TM mode.  Hence we need to call
1609 	 * tm_recheckpoint_new_task() (on the same task) to restore the
1610 	 * checkpointed state back and the TM mode.
1611 	 *
1612 	 * Can't pass dst because it isn't ready. Doesn't matter, passing
1613 	 * dst is only important for __switch_to()
1614 	 */
1615 	__switch_to_tm(src, src);
1616 
1617 	*dst = *src;
1618 
1619 	clear_task_ebb(dst);
1620 
1621 	return 0;
1622 }
1623 
1624 static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1625 {
1626 #ifdef CONFIG_PPC_BOOK3S_64
1627 	unsigned long sp_vsid;
1628 	unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1629 
1630 	if (radix_enabled())
1631 		return;
1632 
1633 	if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1634 		sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1635 			<< SLB_VSID_SHIFT_1T;
1636 	else
1637 		sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1638 			<< SLB_VSID_SHIFT;
1639 	sp_vsid |= SLB_VSID_KERNEL | llp;
1640 	p->thread.ksp_vsid = sp_vsid;
1641 #endif
1642 }
1643 
1644 /*
1645  * Copy a thread..
1646  */
1647 
1648 /*
1649  * Copy architecture-specific thread state
1650  */
1651 int copy_thread(unsigned long clone_flags, unsigned long usp,
1652 		unsigned long kthread_arg, struct task_struct *p,
1653 		unsigned long tls)
1654 {
1655 	struct pt_regs *childregs, *kregs;
1656 	extern void ret_from_fork(void);
1657 	extern void ret_from_fork_scv(void);
1658 	extern void ret_from_kernel_thread(void);
1659 	void (*f)(void);
1660 	unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
1661 	struct thread_info *ti = task_thread_info(p);
1662 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1663 	int i;
1664 #endif
1665 
1666 	klp_init_thread_info(p);
1667 
1668 	/* Copy registers */
1669 	sp -= sizeof(struct pt_regs);
1670 	childregs = (struct pt_regs *) sp;
1671 	if (unlikely(p->flags & PF_KTHREAD)) {
1672 		/* kernel thread */
1673 		memset(childregs, 0, sizeof(struct pt_regs));
1674 		childregs->gpr[1] = sp + sizeof(struct pt_regs);
1675 		/* function */
1676 		if (usp)
1677 			childregs->gpr[14] = ppc_function_entry((void *)usp);
1678 #ifdef CONFIG_PPC64
1679 		clear_tsk_thread_flag(p, TIF_32BIT);
1680 		childregs->softe = IRQS_ENABLED;
1681 #endif
1682 		childregs->gpr[15] = kthread_arg;
1683 		p->thread.regs = NULL;	/* no user register state */
1684 		ti->flags |= _TIF_RESTOREALL;
1685 		f = ret_from_kernel_thread;
1686 	} else {
1687 		/* user thread */
1688 		struct pt_regs *regs = current_pt_regs();
1689 		CHECK_FULL_REGS(regs);
1690 		*childregs = *regs;
1691 		if (usp)
1692 			childregs->gpr[1] = usp;
1693 		p->thread.regs = childregs;
1694 		/* 64s sets this in ret_from_fork */
1695 		if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
1696 			childregs->gpr[3] = 0;  /* Result from fork() */
1697 		if (clone_flags & CLONE_SETTLS) {
1698 			if (!is_32bit_task())
1699 				childregs->gpr[13] = tls;
1700 			else
1701 				childregs->gpr[2] = tls;
1702 		}
1703 
1704 		if (trap_is_scv(regs))
1705 			f = ret_from_fork_scv;
1706 		else
1707 			f = ret_from_fork;
1708 	}
1709 	childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
1710 	sp -= STACK_FRAME_OVERHEAD;
1711 
1712 	/*
1713 	 * The way this works is that at some point in the future
1714 	 * some task will call _switch to switch to the new task.
1715 	 * That will pop off the stack frame created below and start
1716 	 * the new task running at ret_from_fork.  The new task will
1717 	 * do some house keeping and then return from the fork or clone
1718 	 * system call, using the stack frame created above.
1719 	 */
1720 	((unsigned long *)sp)[0] = 0;
1721 	sp -= sizeof(struct pt_regs);
1722 	kregs = (struct pt_regs *) sp;
1723 	sp -= STACK_FRAME_OVERHEAD;
1724 	p->thread.ksp = sp;
1725 #ifdef CONFIG_PPC32
1726 	p->thread.ksp_limit = (unsigned long)end_of_stack(p);
1727 #endif
1728 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1729 	for (i = 0; i < nr_wp_slots(); i++)
1730 		p->thread.ptrace_bps[i] = NULL;
1731 #endif
1732 
1733 	p->thread.fp_save_area = NULL;
1734 #ifdef CONFIG_ALTIVEC
1735 	p->thread.vr_save_area = NULL;
1736 #endif
1737 
1738 	setup_ksp_vsid(p, sp);
1739 
1740 #ifdef CONFIG_PPC64
1741 	if (cpu_has_feature(CPU_FTR_DSCR)) {
1742 		p->thread.dscr_inherit = current->thread.dscr_inherit;
1743 		p->thread.dscr = mfspr(SPRN_DSCR);
1744 	}
1745 	if (cpu_has_feature(CPU_FTR_HAS_PPR))
1746 		childregs->ppr = DEFAULT_PPR;
1747 
1748 	p->thread.tidr = 0;
1749 #endif
1750 	kregs->nip = ppc_function_entry(f);
1751 	return 0;
1752 }
1753 
1754 void preload_new_slb_context(unsigned long start, unsigned long sp);
1755 
1756 /*
1757  * Set up a thread for executing a new program
1758  */
1759 void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
1760 {
1761 #ifdef CONFIG_PPC64
1762 	unsigned long load_addr = regs->gpr[2];	/* saved by ELF_PLAT_INIT */
1763 
1764 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled())
1765 		preload_new_slb_context(start, sp);
1766 #endif
1767 
1768 	/*
1769 	 * If we exec out of a kernel thread then thread.regs will not be
1770 	 * set.  Do it now.
1771 	 */
1772 	if (!current->thread.regs) {
1773 		struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1774 		current->thread.regs = regs - 1;
1775 	}
1776 
1777 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1778 	/*
1779 	 * Clear any transactional state, we're exec()ing. The cause is
1780 	 * not important as there will never be a recheckpoint so it's not
1781 	 * user visible.
1782 	 */
1783 	if (MSR_TM_SUSPENDED(mfmsr()))
1784 		tm_reclaim_current(0);
1785 #endif
1786 
1787 	memset(regs->gpr, 0, sizeof(regs->gpr));
1788 	regs->ctr = 0;
1789 	regs->link = 0;
1790 	regs->xer = 0;
1791 	regs->ccr = 0;
1792 	regs->gpr[1] = sp;
1793 
1794 	/*
1795 	 * We have just cleared all the nonvolatile GPRs, so make
1796 	 * FULL_REGS(regs) return true.  This is necessary to allow
1797 	 * ptrace to examine the thread immediately after exec.
1798 	 */
1799 	SET_FULL_REGS(regs);
1800 
1801 #ifdef CONFIG_PPC32
1802 	regs->mq = 0;
1803 	regs->nip = start;
1804 	regs->msr = MSR_USER;
1805 #else
1806 	if (!is_32bit_task()) {
1807 		unsigned long entry;
1808 
1809 		if (is_elf2_task()) {
1810 			/* Look ma, no function descriptors! */
1811 			entry = start;
1812 
1813 			/*
1814 			 * Ulrich says:
1815 			 *   The latest iteration of the ABI requires that when
1816 			 *   calling a function (at its global entry point),
1817 			 *   the caller must ensure r12 holds the entry point
1818 			 *   address (so that the function can quickly
1819 			 *   establish addressability).
1820 			 */
1821 			regs->gpr[12] = start;
1822 			/* Make sure that's restored on entry to userspace. */
1823 			set_thread_flag(TIF_RESTOREALL);
1824 		} else {
1825 			unsigned long toc;
1826 
1827 			/* start is a relocated pointer to the function
1828 			 * descriptor for the elf _start routine.  The first
1829 			 * entry in the function descriptor is the entry
1830 			 * address of _start and the second entry is the TOC
1831 			 * value we need to use.
1832 			 */
1833 			__get_user(entry, (unsigned long __user *)start);
1834 			__get_user(toc, (unsigned long __user *)start+1);
1835 
1836 			/* Check whether the e_entry function descriptor entries
1837 			 * need to be relocated before we can use them.
1838 			 */
1839 			if (load_addr != 0) {
1840 				entry += load_addr;
1841 				toc   += load_addr;
1842 			}
1843 			regs->gpr[2] = toc;
1844 		}
1845 		regs->nip = entry;
1846 		regs->msr = MSR_USER64;
1847 	} else {
1848 		regs->nip = start;
1849 		regs->gpr[2] = 0;
1850 		regs->msr = MSR_USER32;
1851 	}
1852 #endif
1853 #ifdef CONFIG_VSX
1854 	current->thread.used_vsr = 0;
1855 #endif
1856 	current->thread.load_slb = 0;
1857 	current->thread.load_fp = 0;
1858 	memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
1859 	current->thread.fp_save_area = NULL;
1860 #ifdef CONFIG_ALTIVEC
1861 	memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1862 	current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
1863 	current->thread.vr_save_area = NULL;
1864 	current->thread.vrsave = 0;
1865 	current->thread.used_vr = 0;
1866 	current->thread.load_vec = 0;
1867 #endif /* CONFIG_ALTIVEC */
1868 #ifdef CONFIG_SPE
1869 	memset(current->thread.evr, 0, sizeof(current->thread.evr));
1870 	current->thread.acc = 0;
1871 	current->thread.spefscr = 0;
1872 	current->thread.used_spe = 0;
1873 #endif /* CONFIG_SPE */
1874 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1875 	current->thread.tm_tfhar = 0;
1876 	current->thread.tm_texasr = 0;
1877 	current->thread.tm_tfiar = 0;
1878 	current->thread.load_tm = 0;
1879 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1880 
1881 	thread_pkey_regs_init(&current->thread);
1882 }
1883 EXPORT_SYMBOL(start_thread);
1884 
1885 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1886 		| PR_FP_EXC_RES | PR_FP_EXC_INV)
1887 
1888 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1889 {
1890 	struct pt_regs *regs = tsk->thread.regs;
1891 
1892 	/* This is a bit hairy.  If we are an SPE enabled  processor
1893 	 * (have embedded fp) we store the IEEE exception enable flags in
1894 	 * fpexc_mode.  fpexc_mode is also used for setting FP exception
1895 	 * mode (asyn, precise, disabled) for 'Classic' FP. */
1896 	if (val & PR_FP_EXC_SW_ENABLE) {
1897 		if (cpu_has_feature(CPU_FTR_SPE)) {
1898 			/*
1899 			 * When the sticky exception bits are set
1900 			 * directly by userspace, it must call prctl
1901 			 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1902 			 * in the existing prctl settings) or
1903 			 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1904 			 * the bits being set).  <fenv.h> functions
1905 			 * saving and restoring the whole
1906 			 * floating-point environment need to do so
1907 			 * anyway to restore the prctl settings from
1908 			 * the saved environment.
1909 			 */
1910 #ifdef CONFIG_SPE
1911 			tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
1912 			tsk->thread.fpexc_mode = val &
1913 				(PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1914 #endif
1915 			return 0;
1916 		} else {
1917 			return -EINVAL;
1918 		}
1919 	}
1920 
1921 	/* on a CONFIG_SPE this does not hurt us.  The bits that
1922 	 * __pack_fe01 use do not overlap with bits used for
1923 	 * PR_FP_EXC_SW_ENABLE.  Additionally, the MSR[FE0,FE1] bits
1924 	 * on CONFIG_SPE implementations are reserved so writing to
1925 	 * them does not change anything */
1926 	if (val > PR_FP_EXC_PRECISE)
1927 		return -EINVAL;
1928 	tsk->thread.fpexc_mode = __pack_fe01(val);
1929 	if (regs != NULL && (regs->msr & MSR_FP) != 0)
1930 		regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1931 			| tsk->thread.fpexc_mode;
1932 	return 0;
1933 }
1934 
1935 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1936 {
1937 	unsigned int val = 0;
1938 
1939 	if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE) {
1940 		if (cpu_has_feature(CPU_FTR_SPE)) {
1941 			/*
1942 			 * When the sticky exception bits are set
1943 			 * directly by userspace, it must call prctl
1944 			 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1945 			 * in the existing prctl settings) or
1946 			 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1947 			 * the bits being set).  <fenv.h> functions
1948 			 * saving and restoring the whole
1949 			 * floating-point environment need to do so
1950 			 * anyway to restore the prctl settings from
1951 			 * the saved environment.
1952 			 */
1953 #ifdef CONFIG_SPE
1954 			tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
1955 			val = tsk->thread.fpexc_mode;
1956 #endif
1957 		} else
1958 			return -EINVAL;
1959 	} else {
1960 		val = __unpack_fe01(tsk->thread.fpexc_mode);
1961 	}
1962 	return put_user(val, (unsigned int __user *) adr);
1963 }
1964 
1965 int set_endian(struct task_struct *tsk, unsigned int val)
1966 {
1967 	struct pt_regs *regs = tsk->thread.regs;
1968 
1969 	if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1970 	    (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1971 		return -EINVAL;
1972 
1973 	if (regs == NULL)
1974 		return -EINVAL;
1975 
1976 	if (val == PR_ENDIAN_BIG)
1977 		regs->msr &= ~MSR_LE;
1978 	else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1979 		regs->msr |= MSR_LE;
1980 	else
1981 		return -EINVAL;
1982 
1983 	return 0;
1984 }
1985 
1986 int get_endian(struct task_struct *tsk, unsigned long adr)
1987 {
1988 	struct pt_regs *regs = tsk->thread.regs;
1989 	unsigned int val;
1990 
1991 	if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1992 	    !cpu_has_feature(CPU_FTR_REAL_LE))
1993 		return -EINVAL;
1994 
1995 	if (regs == NULL)
1996 		return -EINVAL;
1997 
1998 	if (regs->msr & MSR_LE) {
1999 		if (cpu_has_feature(CPU_FTR_REAL_LE))
2000 			val = PR_ENDIAN_LITTLE;
2001 		else
2002 			val = PR_ENDIAN_PPC_LITTLE;
2003 	} else
2004 		val = PR_ENDIAN_BIG;
2005 
2006 	return put_user(val, (unsigned int __user *)adr);
2007 }
2008 
2009 int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
2010 {
2011 	tsk->thread.align_ctl = val;
2012 	return 0;
2013 }
2014 
2015 int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
2016 {
2017 	return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
2018 }
2019 
2020 static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
2021 				  unsigned long nbytes)
2022 {
2023 	unsigned long stack_page;
2024 	unsigned long cpu = task_cpu(p);
2025 
2026 	stack_page = (unsigned long)hardirq_ctx[cpu];
2027 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2028 		return 1;
2029 
2030 	stack_page = (unsigned long)softirq_ctx[cpu];
2031 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2032 		return 1;
2033 
2034 	return 0;
2035 }
2036 
2037 static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p,
2038 					unsigned long nbytes)
2039 {
2040 #ifdef CONFIG_PPC64
2041 	unsigned long stack_page;
2042 	unsigned long cpu = task_cpu(p);
2043 
2044 	stack_page = (unsigned long)paca_ptrs[cpu]->emergency_sp - THREAD_SIZE;
2045 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2046 		return 1;
2047 
2048 # ifdef CONFIG_PPC_BOOK3S_64
2049 	stack_page = (unsigned long)paca_ptrs[cpu]->nmi_emergency_sp - THREAD_SIZE;
2050 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2051 		return 1;
2052 
2053 	stack_page = (unsigned long)paca_ptrs[cpu]->mc_emergency_sp - THREAD_SIZE;
2054 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2055 		return 1;
2056 # endif
2057 #endif
2058 
2059 	return 0;
2060 }
2061 
2062 
2063 int validate_sp(unsigned long sp, struct task_struct *p,
2064 		       unsigned long nbytes)
2065 {
2066 	unsigned long stack_page = (unsigned long)task_stack_page(p);
2067 
2068 	if (sp < THREAD_SIZE)
2069 		return 0;
2070 
2071 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2072 		return 1;
2073 
2074 	if (valid_irq_stack(sp, p, nbytes))
2075 		return 1;
2076 
2077 	return valid_emergency_stack(sp, p, nbytes);
2078 }
2079 
2080 EXPORT_SYMBOL(validate_sp);
2081 
2082 static unsigned long __get_wchan(struct task_struct *p)
2083 {
2084 	unsigned long ip, sp;
2085 	int count = 0;
2086 
2087 	if (!p || p == current || p->state == TASK_RUNNING)
2088 		return 0;
2089 
2090 	sp = p->thread.ksp;
2091 	if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
2092 		return 0;
2093 
2094 	do {
2095 		sp = *(unsigned long *)sp;
2096 		if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD) ||
2097 		    p->state == TASK_RUNNING)
2098 			return 0;
2099 		if (count > 0) {
2100 			ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
2101 			if (!in_sched_functions(ip))
2102 				return ip;
2103 		}
2104 	} while (count++ < 16);
2105 	return 0;
2106 }
2107 
2108 unsigned long get_wchan(struct task_struct *p)
2109 {
2110 	unsigned long ret;
2111 
2112 	if (!try_get_task_stack(p))
2113 		return 0;
2114 
2115 	ret = __get_wchan(p);
2116 
2117 	put_task_stack(p);
2118 
2119 	return ret;
2120 }
2121 
2122 static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
2123 
2124 void show_stack(struct task_struct *tsk, unsigned long *stack,
2125 		const char *loglvl)
2126 {
2127 	unsigned long sp, ip, lr, newsp;
2128 	int count = 0;
2129 	int firstframe = 1;
2130 	unsigned long ret_addr;
2131 	int ftrace_idx = 0;
2132 
2133 	if (tsk == NULL)
2134 		tsk = current;
2135 
2136 	if (!try_get_task_stack(tsk))
2137 		return;
2138 
2139 	sp = (unsigned long) stack;
2140 	if (sp == 0) {
2141 		if (tsk == current)
2142 			sp = current_stack_frame();
2143 		else
2144 			sp = tsk->thread.ksp;
2145 	}
2146 
2147 	lr = 0;
2148 	printk("%sCall Trace:\n", loglvl);
2149 	do {
2150 		if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
2151 			break;
2152 
2153 		stack = (unsigned long *) sp;
2154 		newsp = stack[0];
2155 		ip = stack[STACK_FRAME_LR_SAVE];
2156 		if (!firstframe || ip != lr) {
2157 			printk("%s["REG"] ["REG"] %pS",
2158 				loglvl, sp, ip, (void *)ip);
2159 			ret_addr = ftrace_graph_ret_addr(current,
2160 						&ftrace_idx, ip, stack);
2161 			if (ret_addr != ip)
2162 				pr_cont(" (%pS)", (void *)ret_addr);
2163 			if (firstframe)
2164 				pr_cont(" (unreliable)");
2165 			pr_cont("\n");
2166 		}
2167 		firstframe = 0;
2168 
2169 		/*
2170 		 * See if this is an exception frame.
2171 		 * We look for the "regshere" marker in the current frame.
2172 		 */
2173 		if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
2174 		    && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
2175 			struct pt_regs *regs = (struct pt_regs *)
2176 				(sp + STACK_FRAME_OVERHEAD);
2177 			lr = regs->link;
2178 			printk("%s--- interrupt: %lx at %pS\n    LR = %pS\n",
2179 			       loglvl, regs->trap,
2180 			       (void *)regs->nip, (void *)lr);
2181 			firstframe = 1;
2182 		}
2183 
2184 		sp = newsp;
2185 	} while (count++ < kstack_depth_to_print);
2186 
2187 	put_task_stack(tsk);
2188 }
2189 
2190 #ifdef CONFIG_PPC64
2191 /* Called with hard IRQs off */
2192 void notrace __ppc64_runlatch_on(void)
2193 {
2194 	struct thread_info *ti = current_thread_info();
2195 
2196 	if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2197 		/*
2198 		 * Least significant bit (RUN) is the only writable bit of
2199 		 * the CTRL register, so we can avoid mfspr. 2.06 is not the
2200 		 * earliest ISA where this is the case, but it's convenient.
2201 		 */
2202 		mtspr(SPRN_CTRLT, CTRL_RUNLATCH);
2203 	} else {
2204 		unsigned long ctrl;
2205 
2206 		/*
2207 		 * Some architectures (e.g., Cell) have writable fields other
2208 		 * than RUN, so do the read-modify-write.
2209 		 */
2210 		ctrl = mfspr(SPRN_CTRLF);
2211 		ctrl |= CTRL_RUNLATCH;
2212 		mtspr(SPRN_CTRLT, ctrl);
2213 	}
2214 
2215 	ti->local_flags |= _TLF_RUNLATCH;
2216 }
2217 
2218 /* Called with hard IRQs off */
2219 void notrace __ppc64_runlatch_off(void)
2220 {
2221 	struct thread_info *ti = current_thread_info();
2222 
2223 	ti->local_flags &= ~_TLF_RUNLATCH;
2224 
2225 	if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2226 		mtspr(SPRN_CTRLT, 0);
2227 	} else {
2228 		unsigned long ctrl;
2229 
2230 		ctrl = mfspr(SPRN_CTRLF);
2231 		ctrl &= ~CTRL_RUNLATCH;
2232 		mtspr(SPRN_CTRLT, ctrl);
2233 	}
2234 }
2235 #endif /* CONFIG_PPC64 */
2236 
2237 unsigned long arch_align_stack(unsigned long sp)
2238 {
2239 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2240 		sp -= get_random_int() & ~PAGE_MASK;
2241 	return sp & ~0xf;
2242 }
2243 
2244 static inline unsigned long brk_rnd(void)
2245 {
2246         unsigned long rnd = 0;
2247 
2248 	/* 8MB for 32bit, 1GB for 64bit */
2249 	if (is_32bit_task())
2250 		rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT)));
2251 	else
2252 		rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT)));
2253 
2254 	return rnd << PAGE_SHIFT;
2255 }
2256 
2257 unsigned long arch_randomize_brk(struct mm_struct *mm)
2258 {
2259 	unsigned long base = mm->brk;
2260 	unsigned long ret;
2261 
2262 #ifdef CONFIG_PPC_BOOK3S_64
2263 	/*
2264 	 * If we are using 1TB segments and we are allowed to randomise
2265 	 * the heap, we can put it above 1TB so it is backed by a 1TB
2266 	 * segment. Otherwise the heap will be in the bottom 1TB
2267 	 * which always uses 256MB segments and this may result in a
2268 	 * performance penalty. We don't need to worry about radix. For
2269 	 * radix, mmu_highuser_ssize remains unchanged from 256MB.
2270 	 */
2271 	if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
2272 		base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
2273 #endif
2274 
2275 	ret = PAGE_ALIGN(base + brk_rnd());
2276 
2277 	if (ret < mm->brk)
2278 		return mm->brk;
2279 
2280 	return ret;
2281 }
2282 
2283