xref: /linux/arch/powerpc/kernel/process.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
214cf11afSPaul Mackerras /*
314cf11afSPaul Mackerras  *  Derived from "arch/i386/kernel/process.c"
414cf11afSPaul Mackerras  *    Copyright (C) 1995  Linus Torvalds
514cf11afSPaul Mackerras  *
614cf11afSPaul Mackerras  *  Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
714cf11afSPaul Mackerras  *  Paul Mackerras (paulus@cs.anu.edu.au)
814cf11afSPaul Mackerras  *
914cf11afSPaul Mackerras  *  PowerPC version
1014cf11afSPaul Mackerras  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
1114cf11afSPaul Mackerras  */
1214cf11afSPaul Mackerras 
1314cf11afSPaul Mackerras #include <linux/errno.h>
1414cf11afSPaul Mackerras #include <linux/sched.h>
15b17b0153SIngo Molnar #include <linux/sched/debug.h>
1629930025SIngo Molnar #include <linux/sched/task.h>
1768db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
1814cf11afSPaul Mackerras #include <linux/kernel.h>
1914cf11afSPaul Mackerras #include <linux/mm.h>
2014cf11afSPaul Mackerras #include <linux/smp.h>
2114cf11afSPaul Mackerras #include <linux/stddef.h>
2214cf11afSPaul Mackerras #include <linux/unistd.h>
2314cf11afSPaul Mackerras #include <linux/ptrace.h>
2414cf11afSPaul Mackerras #include <linux/slab.h>
2514cf11afSPaul Mackerras #include <linux/user.h>
2614cf11afSPaul Mackerras #include <linux/elf.h>
2714cf11afSPaul Mackerras #include <linux/prctl.h>
2814cf11afSPaul Mackerras #include <linux/init_task.h>
294b16f8e2SPaul Gortmaker #include <linux/export.h>
3014cf11afSPaul Mackerras #include <linux/kallsyms.h>
3114cf11afSPaul Mackerras #include <linux/mqueue.h>
3214cf11afSPaul Mackerras #include <linux/hardirq.h>
3306d67d54SPaul Mackerras #include <linux/utsname.h>
346794c782SSteven Rostedt #include <linux/ftrace.h>
3579741dd3SMartin Schwidefsky #include <linux/kernel_stat.h>
36d839088cSAnton Blanchard #include <linux/personality.h>
375aae8a53SK.Prasad #include <linux/hw_breakpoint.h>
387b051f66SAnton Blanchard #include <linux/uaccess.h>
3906bb53b3SRam Pai #include <linux/pkeys.h>
40fb2d9505SChristophe Leroy #include <linux/seq_buf.h>
4114cf11afSPaul Mackerras 
423a96570fSNicholas Piggin #include <asm/interrupt.h>
4314cf11afSPaul Mackerras #include <asm/io.h>
4414cf11afSPaul Mackerras #include <asm/processor.h>
4514cf11afSPaul Mackerras #include <asm/mmu.h>
4676032de8SMichael Ellerman #include <asm/machdep.h>
47c6622f63SPaul Mackerras #include <asm/time.h>
48ae3a197eSDavid Howells #include <asm/runlatch.h>
49a7f31841SArnd Bergmann #include <asm/syscalls.h>
50ae3a197eSDavid Howells #include <asm/switch_to.h>
51fb09692eSMichael Neuling #include <asm/tm.h>
52ae3a197eSDavid Howells #include <asm/debug.h>
5306d67d54SPaul Mackerras #ifdef CONFIG_PPC64
5406d67d54SPaul Mackerras #include <asm/firmware.h>
55c2e480baSMadhavan Srinivasan #include <asm/hw_irq.h>
5606d67d54SPaul Mackerras #endif
577cedd601SAnton Blanchard #include <asm/code-patching.h>
587f92bc56SDaniel Axtens #include <asm/exec.h>
595d31a96eSMichael Ellerman #include <asm/livepatch.h>
60b92a226eSKevin Hao #include <asm/cpu_has_feature.h>
610545d543SDaniel Axtens #include <asm/asm-prototypes.h>
62c9386bfdSChristophe Leroy #include <asm/stacktrace.h>
63c1fe190cSMichael Neuling #include <asm/hw_breakpoint.h>
645d31a96eSMichael Ellerman 
65d6a61bfcSLuis Machado #include <linux/kprobes.h>
66d6a61bfcSLuis Machado #include <linux/kdebug.h>
6714cf11afSPaul Mackerras 
688b3c34cfSMichael Neuling /* Transactional Memory debug */
698b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
708b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
718b3c34cfSMichael Neuling #else
728b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
738b3c34cfSMichael Neuling #endif
748b3c34cfSMichael Neuling 
75d31626f7SPaul Mackerras #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
7654820530SMichael Ellerman /*
7754820530SMichael Ellerman  * Are we running in "Suspend disabled" mode? If so we have to block any
7854820530SMichael Ellerman  * sigreturn that would get us into suspended state, and we also warn in some
7954820530SMichael Ellerman  * other paths that we should never reach with suspend disabled.
8054820530SMichael Ellerman  */
8154820530SMichael Ellerman bool tm_suspend_disabled __ro_after_init = false;
8254820530SMichael Ellerman 
check_if_tm_restore_required(struct task_struct * tsk)83b86fd2bdSAnton Blanchard static void check_if_tm_restore_required(struct task_struct *tsk)
84d31626f7SPaul Mackerras {
85d31626f7SPaul Mackerras 	/*
86d31626f7SPaul Mackerras 	 * If we are saving the current thread's registers, and the
87d31626f7SPaul Mackerras 	 * thread is in a transactional state, set the TIF_RESTORE_TM
88d31626f7SPaul Mackerras 	 * bit so that we know to restore the registers before
89d31626f7SPaul Mackerras 	 * returning to userspace.
90d31626f7SPaul Mackerras 	 */
91d31626f7SPaul Mackerras 	if (tsk == current && tsk->thread.regs &&
92d31626f7SPaul Mackerras 	    MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
93d31626f7SPaul Mackerras 	    !test_thread_flag(TIF_RESTORE_TM)) {
9459dc5bfcSNicholas Piggin 		regs_set_return_msr(&tsk->thread.ckpt_regs,
9559dc5bfcSNicholas Piggin 						tsk->thread.regs->msr);
96d31626f7SPaul Mackerras 		set_thread_flag(TIF_RESTORE_TM);
97d31626f7SPaul Mackerras 	}
98d31626f7SPaul Mackerras }
99dc16b553SCyril Bur 
100d31626f7SPaul Mackerras #else
check_if_tm_restore_required(struct task_struct * tsk)101b86fd2bdSAnton Blanchard static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
102d31626f7SPaul Mackerras #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
103d31626f7SPaul Mackerras 
1043eb5d588SAnton Blanchard bool strict_msr_control;
1053eb5d588SAnton Blanchard EXPORT_SYMBOL(strict_msr_control);
1063eb5d588SAnton Blanchard 
enable_strict_msr_control(char * str)1073eb5d588SAnton Blanchard static int __init enable_strict_msr_control(char *str)
1083eb5d588SAnton Blanchard {
1093eb5d588SAnton Blanchard 	strict_msr_control = true;
1103eb5d588SAnton Blanchard 	pr_info("Enabling strict facility control\n");
1113eb5d588SAnton Blanchard 
1123eb5d588SAnton Blanchard 	return 0;
1133eb5d588SAnton Blanchard }
1143eb5d588SAnton Blanchard early_param("ppc_strict_facility_enable", enable_strict_msr_control);
1153eb5d588SAnton Blanchard 
116e2b36d59SNicholas Piggin /* notrace because it's called by restore_math */
msr_check_and_set(unsigned long bits)117e2b36d59SNicholas Piggin unsigned long notrace msr_check_and_set(unsigned long bits)
118a0e72cf1SAnton Blanchard {
119a0e72cf1SAnton Blanchard 	unsigned long oldmsr = mfmsr();
120a0e72cf1SAnton Blanchard 	unsigned long newmsr;
121a0e72cf1SAnton Blanchard 
122a0e72cf1SAnton Blanchard 	newmsr = oldmsr | bits;
123a0e72cf1SAnton Blanchard 
124a0e72cf1SAnton Blanchard 	if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
125a0e72cf1SAnton Blanchard 		newmsr |= MSR_VSX;
126a0e72cf1SAnton Blanchard 
127a0e72cf1SAnton Blanchard 	if (oldmsr != newmsr)
1280fa68318SNicholas Piggin 		newmsr = mtmsr_isync_irqsafe(newmsr);
1293cee070aSCyril Bur 
1303cee070aSCyril Bur 	return newmsr;
131a0e72cf1SAnton Blanchard }
132d1c72112SSimon Guo EXPORT_SYMBOL_GPL(msr_check_and_set);
133a0e72cf1SAnton Blanchard 
134e2b36d59SNicholas Piggin /* notrace because it's called by restore_math */
__msr_check_and_clear(unsigned long bits)135e2b36d59SNicholas Piggin void notrace __msr_check_and_clear(unsigned long bits)
136a0e72cf1SAnton Blanchard {
137a0e72cf1SAnton Blanchard 	unsigned long oldmsr = mfmsr();
138a0e72cf1SAnton Blanchard 	unsigned long newmsr;
139a0e72cf1SAnton Blanchard 
140a0e72cf1SAnton Blanchard 	newmsr = oldmsr & ~bits;
141a0e72cf1SAnton Blanchard 
142a0e72cf1SAnton Blanchard 	if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
143a0e72cf1SAnton Blanchard 		newmsr &= ~MSR_VSX;
144a0e72cf1SAnton Blanchard 
145a0e72cf1SAnton Blanchard 	if (oldmsr != newmsr)
1460fa68318SNicholas Piggin 		mtmsr_isync_irqsafe(newmsr);
147a0e72cf1SAnton Blanchard }
1483eb5d588SAnton Blanchard EXPORT_SYMBOL(__msr_check_and_clear);
149a0e72cf1SAnton Blanchard 
150037f0eedSKevin Hao #ifdef CONFIG_PPC_FPU
__giveup_fpu(struct task_struct * tsk)1511cdf039bSMathieu Malaterre static void __giveup_fpu(struct task_struct *tsk)
1528792468dSCyril Bur {
1538eb98037SAnton Blanchard 	unsigned long msr;
1548eb98037SAnton Blanchard 
1558792468dSCyril Bur 	save_fpu(tsk);
1568eb98037SAnton Blanchard 	msr = tsk->thread.regs->msr;
157fe1ef6bcSMark Cave-Ayland 	msr &= ~(MSR_FP|MSR_FE0|MSR_FE1);
1588792468dSCyril Bur 	if (cpu_has_feature(CPU_FTR_VSX))
1598eb98037SAnton Blanchard 		msr &= ~MSR_VSX;
16059dc5bfcSNicholas Piggin 	regs_set_return_msr(tsk->thread.regs, msr);
1618792468dSCyril Bur }
1628792468dSCyril Bur 
giveup_fpu(struct task_struct * tsk)16398da581eSAnton Blanchard void giveup_fpu(struct task_struct *tsk)
16498da581eSAnton Blanchard {
16598da581eSAnton Blanchard 	check_if_tm_restore_required(tsk);
16698da581eSAnton Blanchard 
167a0e72cf1SAnton Blanchard 	msr_check_and_set(MSR_FP);
16898da581eSAnton Blanchard 	__giveup_fpu(tsk);
169a0e72cf1SAnton Blanchard 	msr_check_and_clear(MSR_FP);
17098da581eSAnton Blanchard }
17198da581eSAnton Blanchard EXPORT_SYMBOL(giveup_fpu);
17298da581eSAnton Blanchard 
17314cf11afSPaul Mackerras /*
17414cf11afSPaul Mackerras  * Make sure the floating-point register state in the
17514cf11afSPaul Mackerras  * the thread_struct is up to date for task tsk.
17614cf11afSPaul Mackerras  */
flush_fp_to_thread(struct task_struct * tsk)17714cf11afSPaul Mackerras void flush_fp_to_thread(struct task_struct *tsk)
17814cf11afSPaul Mackerras {
17914cf11afSPaul Mackerras 	if (tsk->thread.regs) {
18014cf11afSPaul Mackerras 		/*
18114cf11afSPaul Mackerras 		 * We need to disable preemption here because if we didn't,
18214cf11afSPaul Mackerras 		 * another process could get scheduled after the regs->msr
18314cf11afSPaul Mackerras 		 * test but before we have finished saving the FP registers
18414cf11afSPaul Mackerras 		 * to the thread_struct.  That process could take over the
18514cf11afSPaul Mackerras 		 * FPU, and then when we get scheduled again we would store
18614cf11afSPaul Mackerras 		 * bogus values for the remaining FP registers.
18714cf11afSPaul Mackerras 		 */
18814cf11afSPaul Mackerras 		preempt_disable();
18914cf11afSPaul Mackerras 		if (tsk->thread.regs->msr & MSR_FP) {
19014cf11afSPaul Mackerras 			/*
19114cf11afSPaul Mackerras 			 * This should only ever be called for current or
19214cf11afSPaul Mackerras 			 * for a stopped child process.  Since we save away
193af1bbc3dSAnton Blanchard 			 * the FP register state on context switch,
19414cf11afSPaul Mackerras 			 * there is something wrong if a stopped child appears
19514cf11afSPaul Mackerras 			 * to still have its FP state in the CPU registers.
19614cf11afSPaul Mackerras 			 */
19714cf11afSPaul Mackerras 			BUG_ON(tsk != current);
198b86fd2bdSAnton Blanchard 			giveup_fpu(tsk);
19914cf11afSPaul Mackerras 		}
20014cf11afSPaul Mackerras 		preempt_enable();
20114cf11afSPaul Mackerras 	}
20214cf11afSPaul Mackerras }
203de56a948SPaul Mackerras EXPORT_SYMBOL_GPL(flush_fp_to_thread);
20414cf11afSPaul Mackerras 
enable_kernel_fp(void)20514cf11afSPaul Mackerras void enable_kernel_fp(void)
20614cf11afSPaul Mackerras {
207e909fb83SCyril Bur 	unsigned long cpumsr;
208e909fb83SCyril Bur 
20914cf11afSPaul Mackerras 	WARN_ON(preemptible());
21014cf11afSPaul Mackerras 
211e909fb83SCyril Bur 	cpumsr = msr_check_and_set(MSR_FP);
212611b0e5cSAnton Blanchard 
213d64d02ceSAnton Blanchard 	if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
214d64d02ceSAnton Blanchard 		check_if_tm_restore_required(current);
215e909fb83SCyril Bur 		/*
216e909fb83SCyril Bur 		 * If a thread has already been reclaimed then the
217e909fb83SCyril Bur 		 * checkpointed registers are on the CPU but have definitely
218e909fb83SCyril Bur 		 * been saved by the reclaim code. Don't need to and *cannot*
219e909fb83SCyril Bur 		 * giveup as this would save  to the 'live' structure not the
220e909fb83SCyril Bur 		 * checkpointed structure.
221e909fb83SCyril Bur 		 */
2225c784c84SBreno Leitao 		if (!MSR_TM_ACTIVE(cpumsr) &&
2235c784c84SBreno Leitao 		     MSR_TM_ACTIVE(current->thread.regs->msr))
224e909fb83SCyril Bur 			return;
225a0e72cf1SAnton Blanchard 		__giveup_fpu(current);
226b86fd2bdSAnton Blanchard 	}
227d64d02ceSAnton Blanchard }
22814cf11afSPaul Mackerras EXPORT_SYMBOL(enable_kernel_fp);
229c83c192aSChristophe Leroy #else
__giveup_fpu(struct task_struct * tsk)230c83c192aSChristophe Leroy static inline void __giveup_fpu(struct task_struct *tsk) { }
231d1e1cf2eSAnton Blanchard #endif /* CONFIG_PPC_FPU */
23214cf11afSPaul Mackerras 
23314cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
__giveup_altivec(struct task_struct * tsk)2346f515d84SCyril Bur static void __giveup_altivec(struct task_struct *tsk)
2356f515d84SCyril Bur {
2368eb98037SAnton Blanchard 	unsigned long msr;
2378eb98037SAnton Blanchard 
2386f515d84SCyril Bur 	save_altivec(tsk);
2398eb98037SAnton Blanchard 	msr = tsk->thread.regs->msr;
2408eb98037SAnton Blanchard 	msr &= ~MSR_VEC;
2416f515d84SCyril Bur 	if (cpu_has_feature(CPU_FTR_VSX))
2428eb98037SAnton Blanchard 		msr &= ~MSR_VSX;
24359dc5bfcSNicholas Piggin 	regs_set_return_msr(tsk->thread.regs, msr);
2446f515d84SCyril Bur }
2456f515d84SCyril Bur 
giveup_altivec(struct task_struct * tsk)24698da581eSAnton Blanchard void giveup_altivec(struct task_struct *tsk)
24798da581eSAnton Blanchard {
24898da581eSAnton Blanchard 	check_if_tm_restore_required(tsk);
24998da581eSAnton Blanchard 
250a0e72cf1SAnton Blanchard 	msr_check_and_set(MSR_VEC);
25198da581eSAnton Blanchard 	__giveup_altivec(tsk);
252a0e72cf1SAnton Blanchard 	msr_check_and_clear(MSR_VEC);
25398da581eSAnton Blanchard }
25498da581eSAnton Blanchard EXPORT_SYMBOL(giveup_altivec);
25598da581eSAnton Blanchard 
enable_kernel_altivec(void)25614cf11afSPaul Mackerras void enable_kernel_altivec(void)
25714cf11afSPaul Mackerras {
258e909fb83SCyril Bur 	unsigned long cpumsr;
259e909fb83SCyril Bur 
26014cf11afSPaul Mackerras 	WARN_ON(preemptible());
26114cf11afSPaul Mackerras 
262e909fb83SCyril Bur 	cpumsr = msr_check_and_set(MSR_VEC);
263611b0e5cSAnton Blanchard 
264d64d02ceSAnton Blanchard 	if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
265d64d02ceSAnton Blanchard 		check_if_tm_restore_required(current);
266e909fb83SCyril Bur 		/*
267e909fb83SCyril Bur 		 * If a thread has already been reclaimed then the
268e909fb83SCyril Bur 		 * checkpointed registers are on the CPU but have definitely
269e909fb83SCyril Bur 		 * been saved by the reclaim code. Don't need to and *cannot*
270e909fb83SCyril Bur 		 * giveup as this would save  to the 'live' structure not the
271e909fb83SCyril Bur 		 * checkpointed structure.
272e909fb83SCyril Bur 		 */
2735c784c84SBreno Leitao 		if (!MSR_TM_ACTIVE(cpumsr) &&
2745c784c84SBreno Leitao 		     MSR_TM_ACTIVE(current->thread.regs->msr))
275e909fb83SCyril Bur 			return;
276a0e72cf1SAnton Blanchard 		__giveup_altivec(current);
277b86fd2bdSAnton Blanchard 	}
278d64d02ceSAnton Blanchard }
27914cf11afSPaul Mackerras EXPORT_SYMBOL(enable_kernel_altivec);
28014cf11afSPaul Mackerras 
28114cf11afSPaul Mackerras /*
28214cf11afSPaul Mackerras  * Make sure the VMX/Altivec register state in the
28314cf11afSPaul Mackerras  * the thread_struct is up to date for task tsk.
28414cf11afSPaul Mackerras  */
flush_altivec_to_thread(struct task_struct * tsk)28514cf11afSPaul Mackerras void flush_altivec_to_thread(struct task_struct *tsk)
28614cf11afSPaul Mackerras {
28714cf11afSPaul Mackerras 	if (tsk->thread.regs) {
28814cf11afSPaul Mackerras 		preempt_disable();
28914cf11afSPaul Mackerras 		if (tsk->thread.regs->msr & MSR_VEC) {
29014cf11afSPaul Mackerras 			BUG_ON(tsk != current);
291b86fd2bdSAnton Blanchard 			giveup_altivec(tsk);
29214cf11afSPaul Mackerras 		}
29314cf11afSPaul Mackerras 		preempt_enable();
29414cf11afSPaul Mackerras 	}
29514cf11afSPaul Mackerras }
296de56a948SPaul Mackerras EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
29714cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
29814cf11afSPaul Mackerras 
299ce48b210SMichael Neuling #ifdef CONFIG_VSX
__giveup_vsx(struct task_struct * tsk)300bf6a4d5bSCyril Bur static void __giveup_vsx(struct task_struct *tsk)
301a7d623d4SAnton Blanchard {
302dc801081SBenjamin Herrenschmidt 	unsigned long msr = tsk->thread.regs->msr;
303dc801081SBenjamin Herrenschmidt 
304dc801081SBenjamin Herrenschmidt 	/*
3051fd02f66SJulia Lawall 	 * We should never be setting MSR_VSX without also setting
306dc801081SBenjamin Herrenschmidt 	 * MSR_FP and MSR_VEC
307dc801081SBenjamin Herrenschmidt 	 */
308dc801081SBenjamin Herrenschmidt 	WARN_ON((msr & MSR_VSX) && !((msr & MSR_FP) && (msr & MSR_VEC)));
309dc801081SBenjamin Herrenschmidt 
310dc801081SBenjamin Herrenschmidt 	/* __giveup_fpu will clear MSR_VSX */
311dc801081SBenjamin Herrenschmidt 	if (msr & MSR_FP)
312a7d623d4SAnton Blanchard 		__giveup_fpu(tsk);
313dc801081SBenjamin Herrenschmidt 	if (msr & MSR_VEC)
314a7d623d4SAnton Blanchard 		__giveup_altivec(tsk);
315bf6a4d5bSCyril Bur }
316bf6a4d5bSCyril Bur 
giveup_vsx(struct task_struct * tsk)317bf6a4d5bSCyril Bur static void giveup_vsx(struct task_struct *tsk)
318bf6a4d5bSCyril Bur {
319bf6a4d5bSCyril Bur 	check_if_tm_restore_required(tsk);
320bf6a4d5bSCyril Bur 
321bf6a4d5bSCyril Bur 	msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
322a7d623d4SAnton Blanchard 	__giveup_vsx(tsk);
323a0e72cf1SAnton Blanchard 	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
324a7d623d4SAnton Blanchard }
325bf6a4d5bSCyril Bur 
enable_kernel_vsx(void)326ce48b210SMichael Neuling void enable_kernel_vsx(void)
327ce48b210SMichael Neuling {
328e909fb83SCyril Bur 	unsigned long cpumsr;
329e909fb83SCyril Bur 
330ce48b210SMichael Neuling 	WARN_ON(preemptible());
331ce48b210SMichael Neuling 
332e909fb83SCyril Bur 	cpumsr = msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
333611b0e5cSAnton Blanchard 
3345a69aec9SBenjamin Herrenschmidt 	if (current->thread.regs &&
3355a69aec9SBenjamin Herrenschmidt 	    (current->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP))) {
336d64d02ceSAnton Blanchard 		check_if_tm_restore_required(current);
337e909fb83SCyril Bur 		/*
338e909fb83SCyril Bur 		 * If a thread has already been reclaimed then the
339e909fb83SCyril Bur 		 * checkpointed registers are on the CPU but have definitely
340e909fb83SCyril Bur 		 * been saved by the reclaim code. Don't need to and *cannot*
341e909fb83SCyril Bur 		 * giveup as this would save  to the 'live' structure not the
342e909fb83SCyril Bur 		 * checkpointed structure.
343e909fb83SCyril Bur 		 */
3445c784c84SBreno Leitao 		if (!MSR_TM_ACTIVE(cpumsr) &&
3455c784c84SBreno Leitao 		     MSR_TM_ACTIVE(current->thread.regs->msr))
346e909fb83SCyril Bur 			return;
347a0e72cf1SAnton Blanchard 		__giveup_vsx(current);
348611b0e5cSAnton Blanchard 	}
349ce48b210SMichael Neuling }
350ce48b210SMichael Neuling EXPORT_SYMBOL(enable_kernel_vsx);
351ce48b210SMichael Neuling 
flush_vsx_to_thread(struct task_struct * tsk)352ce48b210SMichael Neuling void flush_vsx_to_thread(struct task_struct *tsk)
353ce48b210SMichael Neuling {
354ce48b210SMichael Neuling 	if (tsk->thread.regs) {
355ce48b210SMichael Neuling 		preempt_disable();
3565a69aec9SBenjamin Herrenschmidt 		if (tsk->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP)) {
357ce48b210SMichael Neuling 			BUG_ON(tsk != current);
358ce48b210SMichael Neuling 			giveup_vsx(tsk);
359ce48b210SMichael Neuling 		}
360ce48b210SMichael Neuling 		preempt_enable();
361ce48b210SMichael Neuling 	}
362ce48b210SMichael Neuling }
363de56a948SPaul Mackerras EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
364ce48b210SMichael Neuling #endif /* CONFIG_VSX */
365ce48b210SMichael Neuling 
36614cf11afSPaul Mackerras #ifdef CONFIG_SPE
giveup_spe(struct task_struct * tsk)36798da581eSAnton Blanchard void giveup_spe(struct task_struct *tsk)
36898da581eSAnton Blanchard {
36998da581eSAnton Blanchard 	check_if_tm_restore_required(tsk);
37098da581eSAnton Blanchard 
371a0e72cf1SAnton Blanchard 	msr_check_and_set(MSR_SPE);
37298da581eSAnton Blanchard 	__giveup_spe(tsk);
373a0e72cf1SAnton Blanchard 	msr_check_and_clear(MSR_SPE);
37498da581eSAnton Blanchard }
37598da581eSAnton Blanchard EXPORT_SYMBOL(giveup_spe);
37614cf11afSPaul Mackerras 
enable_kernel_spe(void)37714cf11afSPaul Mackerras void enable_kernel_spe(void)
37814cf11afSPaul Mackerras {
37914cf11afSPaul Mackerras 	WARN_ON(preemptible());
38014cf11afSPaul Mackerras 
381a0e72cf1SAnton Blanchard 	msr_check_and_set(MSR_SPE);
382611b0e5cSAnton Blanchard 
383d64d02ceSAnton Blanchard 	if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
384d64d02ceSAnton Blanchard 		check_if_tm_restore_required(current);
385a0e72cf1SAnton Blanchard 		__giveup_spe(current);
38614cf11afSPaul Mackerras 	}
387d64d02ceSAnton Blanchard }
38814cf11afSPaul Mackerras EXPORT_SYMBOL(enable_kernel_spe);
38914cf11afSPaul Mackerras 
flush_spe_to_thread(struct task_struct * tsk)39014cf11afSPaul Mackerras void flush_spe_to_thread(struct task_struct *tsk)
39114cf11afSPaul Mackerras {
39214cf11afSPaul Mackerras 	if (tsk->thread.regs) {
39314cf11afSPaul Mackerras 		preempt_disable();
39414cf11afSPaul Mackerras 		if (tsk->thread.regs->msr & MSR_SPE) {
39514cf11afSPaul Mackerras 			BUG_ON(tsk != current);
396685659eeSyu liu 			tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
3970ee6c15eSKumar Gala 			giveup_spe(tsk);
39814cf11afSPaul Mackerras 		}
39914cf11afSPaul Mackerras 		preempt_enable();
40014cf11afSPaul Mackerras 	}
40114cf11afSPaul Mackerras }
40214cf11afSPaul Mackerras #endif /* CONFIG_SPE */
40314cf11afSPaul Mackerras 
404c2085059SAnton Blanchard static unsigned long msr_all_available;
405c2085059SAnton Blanchard 
init_msr_all_available(void)406c2085059SAnton Blanchard static int __init init_msr_all_available(void)
407c2085059SAnton Blanchard {
408c83c192aSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_FPU))
409c2085059SAnton Blanchard 		msr_all_available |= MSR_FP;
410c2085059SAnton Blanchard 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
411c2085059SAnton Blanchard 		msr_all_available |= MSR_VEC;
412c2085059SAnton Blanchard 	if (cpu_has_feature(CPU_FTR_VSX))
413c2085059SAnton Blanchard 		msr_all_available |= MSR_VSX;
414c2085059SAnton Blanchard 	if (cpu_has_feature(CPU_FTR_SPE))
415c2085059SAnton Blanchard 		msr_all_available |= MSR_SPE;
416c2085059SAnton Blanchard 
417c2085059SAnton Blanchard 	return 0;
418c2085059SAnton Blanchard }
419c2085059SAnton Blanchard early_initcall(init_msr_all_available);
420c2085059SAnton Blanchard 
giveup_all(struct task_struct * tsk)421c2085059SAnton Blanchard void giveup_all(struct task_struct *tsk)
422c2085059SAnton Blanchard {
423c2085059SAnton Blanchard 	unsigned long usermsr;
424c2085059SAnton Blanchard 
425c2085059SAnton Blanchard 	if (!tsk->thread.regs)
426c2085059SAnton Blanchard 		return;
427c2085059SAnton Blanchard 
4288205d5d9SGustavo Romero 	check_if_tm_restore_required(tsk);
4298205d5d9SGustavo Romero 
430c2085059SAnton Blanchard 	usermsr = tsk->thread.regs->msr;
431c2085059SAnton Blanchard 
432c2085059SAnton Blanchard 	if ((usermsr & msr_all_available) == 0)
433c2085059SAnton Blanchard 		return;
434c2085059SAnton Blanchard 
435c2085059SAnton Blanchard 	msr_check_and_set(msr_all_available);
436c2085059SAnton Blanchard 
43796c79b6bSBenjamin Herrenschmidt 	WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
43896c79b6bSBenjamin Herrenschmidt 
439c2085059SAnton Blanchard 	if (usermsr & MSR_FP)
440c2085059SAnton Blanchard 		__giveup_fpu(tsk);
441c2085059SAnton Blanchard 	if (usermsr & MSR_VEC)
442c2085059SAnton Blanchard 		__giveup_altivec(tsk);
443c2085059SAnton Blanchard 	if (usermsr & MSR_SPE)
444c2085059SAnton Blanchard 		__giveup_spe(tsk);
445c2085059SAnton Blanchard 
446c2085059SAnton Blanchard 	msr_check_and_clear(msr_all_available);
447c2085059SAnton Blanchard }
448c2085059SAnton Blanchard EXPORT_SYMBOL(giveup_all);
449c2085059SAnton Blanchard 
4506cc0c16dSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
4516cc0c16dSNicholas Piggin #ifdef CONFIG_PPC_FPU
should_restore_fp(void)45201eb0187SNicholas Piggin static bool should_restore_fp(void)
4536cc0c16dSNicholas Piggin {
45401eb0187SNicholas Piggin 	if (current->thread.load_fp) {
4556cc0c16dSNicholas Piggin 		current->thread.load_fp++;
45601eb0187SNicholas Piggin 		return true;
4576cc0c16dSNicholas Piggin 	}
45801eb0187SNicholas Piggin 	return false;
45901eb0187SNicholas Piggin }
46001eb0187SNicholas Piggin 
do_restore_fp(void)46101eb0187SNicholas Piggin static void do_restore_fp(void)
46201eb0187SNicholas Piggin {
46301eb0187SNicholas Piggin 	load_fp_state(&current->thread.fp_state);
4646cc0c16dSNicholas Piggin }
4656cc0c16dSNicholas Piggin #else
should_restore_fp(void)46601eb0187SNicholas Piggin static bool should_restore_fp(void) { return false; }
do_restore_fp(void)46701eb0187SNicholas Piggin static void do_restore_fp(void) { }
4686cc0c16dSNicholas Piggin #endif /* CONFIG_PPC_FPU */
4696cc0c16dSNicholas Piggin 
4706cc0c16dSNicholas Piggin #ifdef CONFIG_ALTIVEC
should_restore_altivec(void)47101eb0187SNicholas Piggin static bool should_restore_altivec(void)
4726cc0c16dSNicholas Piggin {
47301eb0187SNicholas Piggin 	if (cpu_has_feature(CPU_FTR_ALTIVEC) && (current->thread.load_vec)) {
47401eb0187SNicholas Piggin 		current->thread.load_vec++;
47501eb0187SNicholas Piggin 		return true;
4766cc0c16dSNicholas Piggin 	}
47701eb0187SNicholas Piggin 	return false;
47801eb0187SNicholas Piggin }
47901eb0187SNicholas Piggin 
do_restore_altivec(void)48001eb0187SNicholas Piggin static void do_restore_altivec(void)
48101eb0187SNicholas Piggin {
48201eb0187SNicholas Piggin 	load_vr_state(&current->thread.vr_state);
48301eb0187SNicholas Piggin 	current->thread.used_vr = 1;
4846cc0c16dSNicholas Piggin }
4856cc0c16dSNicholas Piggin #else
should_restore_altivec(void)48601eb0187SNicholas Piggin static bool should_restore_altivec(void) { return false; }
do_restore_altivec(void)48701eb0187SNicholas Piggin static void do_restore_altivec(void) { }
4886cc0c16dSNicholas Piggin #endif /* CONFIG_ALTIVEC */
4896cc0c16dSNicholas Piggin 
should_restore_vsx(void)49001eb0187SNicholas Piggin static bool should_restore_vsx(void)
4916cc0c16dSNicholas Piggin {
49201eb0187SNicholas Piggin 	if (cpu_has_feature(CPU_FTR_VSX))
49301eb0187SNicholas Piggin 		return true;
49401eb0187SNicholas Piggin 	return false;
4956cc0c16dSNicholas Piggin }
49680739c2bSChristophe Leroy #ifdef CONFIG_VSX
do_restore_vsx(void)49701eb0187SNicholas Piggin static void do_restore_vsx(void)
49801eb0187SNicholas Piggin {
49901eb0187SNicholas Piggin 	current->thread.used_vsr = 1;
5006cc0c16dSNicholas Piggin }
5016cc0c16dSNicholas Piggin #else
do_restore_vsx(void)50201eb0187SNicholas Piggin static void do_restore_vsx(void) { }
5036cc0c16dSNicholas Piggin #endif /* CONFIG_VSX */
5046cc0c16dSNicholas Piggin 
505e2b36d59SNicholas Piggin /*
506e2b36d59SNicholas Piggin  * The exception exit path calls restore_math() with interrupts hard disabled
507e2b36d59SNicholas Piggin  * but the soft irq state not "reconciled". ftrace code that calls
508e2b36d59SNicholas Piggin  * local_irq_save/restore causes warnings.
509e2b36d59SNicholas Piggin  *
510e2b36d59SNicholas Piggin  * Rather than complicate the exit path, just don't trace restore_math. This
511e2b36d59SNicholas Piggin  * could be done by having ftrace entry code check for this un-reconciled
512e2b36d59SNicholas Piggin  * condition where MSR[EE]=0 and PACA_IRQ_HARD_DIS is not set, and
513e2b36d59SNicholas Piggin  * temporarily fix it up for the duration of the ftrace call.
514e2b36d59SNicholas Piggin  */
restore_math(struct pt_regs * regs)515e2b36d59SNicholas Piggin void notrace restore_math(struct pt_regs *regs)
51670fe3d98SCyril Bur {
51770fe3d98SCyril Bur 	unsigned long msr;
51801eb0187SNicholas Piggin 	unsigned long new_msr = 0;
51970fe3d98SCyril Bur 
52070fe3d98SCyril Bur 	msr = regs->msr;
52170fe3d98SCyril Bur 
52270fe3d98SCyril Bur 	/*
52301eb0187SNicholas Piggin 	 * new_msr tracks the facilities that are to be restored. Only reload
52401eb0187SNicholas Piggin 	 * if the bit is not set in the user MSR (if it is set, the registers
52501eb0187SNicholas Piggin 	 * are live for the user thread).
52670fe3d98SCyril Bur 	 */
52701eb0187SNicholas Piggin 	if ((!(msr & MSR_FP)) && should_restore_fp())
528b91eb518SMichael Ellerman 		new_msr |= MSR_FP;
52970fe3d98SCyril Bur 
53001eb0187SNicholas Piggin 	if ((!(msr & MSR_VEC)) && should_restore_altivec())
53101eb0187SNicholas Piggin 		new_msr |= MSR_VEC;
53270fe3d98SCyril Bur 
53301eb0187SNicholas Piggin 	if ((!(msr & MSR_VSX)) && should_restore_vsx()) {
53401eb0187SNicholas Piggin 		if (((msr | new_msr) & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC))
53501eb0187SNicholas Piggin 			new_msr |= MSR_VSX;
53670fe3d98SCyril Bur 	}
53770fe3d98SCyril Bur 
53801eb0187SNicholas Piggin 	if (new_msr) {
539b91eb518SMichael Ellerman 		unsigned long fpexc_mode = 0;
540b91eb518SMichael Ellerman 
54101eb0187SNicholas Piggin 		msr_check_and_set(new_msr);
54270fe3d98SCyril Bur 
543b91eb518SMichael Ellerman 		if (new_msr & MSR_FP) {
54401eb0187SNicholas Piggin 			do_restore_fp();
54501eb0187SNicholas Piggin 
546b91eb518SMichael Ellerman 			// This also covers VSX, because VSX implies FP
547b91eb518SMichael Ellerman 			fpexc_mode = current->thread.fpexc_mode;
548b91eb518SMichael Ellerman 		}
549b91eb518SMichael Ellerman 
55001eb0187SNicholas Piggin 		if (new_msr & MSR_VEC)
55101eb0187SNicholas Piggin 			do_restore_altivec();
55201eb0187SNicholas Piggin 
55301eb0187SNicholas Piggin 		if (new_msr & MSR_VSX)
55401eb0187SNicholas Piggin 			do_restore_vsx();
55501eb0187SNicholas Piggin 
55601eb0187SNicholas Piggin 		msr_check_and_clear(new_msr);
55701eb0187SNicholas Piggin 
55859dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, regs->msr | new_msr | fpexc_mode);
55901eb0187SNicholas Piggin 	}
56070fe3d98SCyril Bur }
56160d62bfdSChristophe Leroy #endif /* CONFIG_PPC_BOOK3S_64 */
56270fe3d98SCyril Bur 
save_all(struct task_struct * tsk)5631cdf039bSMathieu Malaterre static void save_all(struct task_struct *tsk)
564de2a20aaSCyril Bur {
565de2a20aaSCyril Bur 	unsigned long usermsr;
566de2a20aaSCyril Bur 
567de2a20aaSCyril Bur 	if (!tsk->thread.regs)
568de2a20aaSCyril Bur 		return;
569de2a20aaSCyril Bur 
570de2a20aaSCyril Bur 	usermsr = tsk->thread.regs->msr;
571de2a20aaSCyril Bur 
572de2a20aaSCyril Bur 	if ((usermsr & msr_all_available) == 0)
573de2a20aaSCyril Bur 		return;
574de2a20aaSCyril Bur 
575de2a20aaSCyril Bur 	msr_check_and_set(msr_all_available);
576de2a20aaSCyril Bur 
57796c79b6bSBenjamin Herrenschmidt 	WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
57896c79b6bSBenjamin Herrenschmidt 
579de2a20aaSCyril Bur 	if (usermsr & MSR_FP)
5808792468dSCyril Bur 		save_fpu(tsk);
581de2a20aaSCyril Bur 
582de2a20aaSCyril Bur 	if (usermsr & MSR_VEC)
5836f515d84SCyril Bur 		save_altivec(tsk);
584de2a20aaSCyril Bur 
585de2a20aaSCyril Bur 	if (usermsr & MSR_SPE)
586de2a20aaSCyril Bur 		__giveup_spe(tsk);
587de2a20aaSCyril Bur 
588de2a20aaSCyril Bur 	msr_check_and_clear(msr_all_available);
589de2a20aaSCyril Bur }
590de2a20aaSCyril Bur 
flush_all_to_thread(struct task_struct * tsk)591579e633eSAnton Blanchard void flush_all_to_thread(struct task_struct *tsk)
592579e633eSAnton Blanchard {
593579e633eSAnton Blanchard 	if (tsk->thread.regs) {
594579e633eSAnton Blanchard 		preempt_disable();
595579e633eSAnton Blanchard 		BUG_ON(tsk != current);
596579e633eSAnton Blanchard #ifdef CONFIG_SPE
597579e633eSAnton Blanchard 		if (tsk->thread.regs->msr & MSR_SPE)
598579e633eSAnton Blanchard 			tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
599579e633eSAnton Blanchard #endif
600e9013785SFelipe Rechia 		save_all(tsk);
601579e633eSAnton Blanchard 
602579e633eSAnton Blanchard 		preempt_enable();
603579e633eSAnton Blanchard 	}
604579e633eSAnton Blanchard }
605579e633eSAnton Blanchard EXPORT_SYMBOL(flush_all_to_thread);
606579e633eSAnton Blanchard 
6073bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
do_send_trap(struct pt_regs * regs,unsigned long address,unsigned long error_code,int breakpt)6083bffb652SDave Kleikamp void do_send_trap(struct pt_regs *regs, unsigned long address,
60947355040SEric W. Biederman 		  unsigned long error_code, int breakpt)
6103bffb652SDave Kleikamp {
61147355040SEric W. Biederman 	current->thread.trap_nr = TRAP_HWBKPT;
6123bffb652SDave Kleikamp 	if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
6133bffb652SDave Kleikamp 			11, SIGSEGV) == NOTIFY_STOP)
6143bffb652SDave Kleikamp 		return;
6153bffb652SDave Kleikamp 
6163bffb652SDave Kleikamp 	/* Deliver the signal to userspace */
617f71dd7dcSEric W. Biederman 	force_sig_ptrace_errno_trap(breakpt, /* breakpoint or watchpoint id */
618f71dd7dcSEric W. Biederman 				    (void __user *)address);
6193bffb652SDave Kleikamp }
6203bffb652SDave Kleikamp #else	/* !CONFIG_PPC_ADV_DEBUG_REGS */
6215b905d77SRavi Bangoria 
do_break_handler(struct pt_regs * regs)6225b905d77SRavi Bangoria static void do_break_handler(struct pt_regs *regs)
6235b905d77SRavi Bangoria {
6245b905d77SRavi Bangoria 	struct arch_hw_breakpoint null_brk = {0};
6255b905d77SRavi Bangoria 	struct arch_hw_breakpoint *info;
626c545b9f0SChristophe Leroy 	ppc_inst_t instr = ppc_inst(0);
6275b905d77SRavi Bangoria 	int type = 0;
6285b905d77SRavi Bangoria 	int size = 0;
6295b905d77SRavi Bangoria 	unsigned long ea;
6305b905d77SRavi Bangoria 	int i;
6315b905d77SRavi Bangoria 
6325b905d77SRavi Bangoria 	/*
6335b905d77SRavi Bangoria 	 * If underneath hw supports only one watchpoint, we know it
6345b905d77SRavi Bangoria 	 * caused exception. 8xx also falls into this category.
6355b905d77SRavi Bangoria 	 */
6365b905d77SRavi Bangoria 	if (nr_wp_slots() == 1) {
6375b905d77SRavi Bangoria 		__set_breakpoint(0, &null_brk);
6385b905d77SRavi Bangoria 		current->thread.hw_brk[0] = null_brk;
6395b905d77SRavi Bangoria 		current->thread.hw_brk[0].flags |= HW_BRK_FLAG_DISABLED;
6405b905d77SRavi Bangoria 		return;
6415b905d77SRavi Bangoria 	}
6425b905d77SRavi Bangoria 
6435b905d77SRavi Bangoria 	/* Otherwise find out which DAWR caused exception and disable it. */
6445b905d77SRavi Bangoria 	wp_get_instr_detail(regs, &instr, &type, &size, &ea);
6455b905d77SRavi Bangoria 
6465b905d77SRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
6475b905d77SRavi Bangoria 		info = &current->thread.hw_brk[i];
6485b905d77SRavi Bangoria 		if (!info->address)
6495b905d77SRavi Bangoria 			continue;
6505b905d77SRavi Bangoria 
6515b905d77SRavi Bangoria 		if (wp_check_constraints(regs, instr, ea, type, size, info)) {
6525b905d77SRavi Bangoria 			__set_breakpoint(i, &null_brk);
6535b905d77SRavi Bangoria 			current->thread.hw_brk[i] = null_brk;
6545b905d77SRavi Bangoria 			current->thread.hw_brk[i].flags |= HW_BRK_FLAG_DISABLED;
6555b905d77SRavi Bangoria 		}
6565b905d77SRavi Bangoria 	}
6575b905d77SRavi Bangoria }
6585b905d77SRavi Bangoria 
DEFINE_INTERRUPT_HANDLER(do_break)6593a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(do_break)
660d6a61bfcSLuis Machado {
66141ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = TRAP_HWBKPT;
66218722ecfSNicholas Piggin 	if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, regs->dsisr,
663d6a61bfcSLuis Machado 			11, SIGSEGV) == NOTIFY_STOP)
664d6a61bfcSLuis Machado 		return;
665d6a61bfcSLuis Machado 
6669422de3eSMichael Neuling 	if (debugger_break_match(regs))
667d6a61bfcSLuis Machado 		return;
668d6a61bfcSLuis Machado 
6695b905d77SRavi Bangoria 	/*
6705b905d77SRavi Bangoria 	 * We reach here only when watchpoint exception is generated by ptrace
6715b905d77SRavi Bangoria 	 * event (or hw is buggy!). Now if CONFIG_HAVE_HW_BREAKPOINT is set,
6725b905d77SRavi Bangoria 	 * watchpoint is already handled by hw_breakpoint_handler() so we don't
6735b905d77SRavi Bangoria 	 * have to do anything. But when CONFIG_HAVE_HW_BREAKPOINT is not set,
6745b905d77SRavi Bangoria 	 * we need to manually handle the watchpoint here.
6755b905d77SRavi Bangoria 	 */
6765b905d77SRavi Bangoria 	if (!IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT))
6775b905d77SRavi Bangoria 		do_break_handler(regs);
6785b905d77SRavi Bangoria 
679d6a61bfcSLuis Machado 	/* Deliver the signal to userspace */
68018722ecfSNicholas Piggin 	force_sig_fault(SIGTRAP, TRAP_HWBKPT, (void __user *)regs->dar);
681d6a61bfcSLuis Machado }
6823bffb652SDave Kleikamp #endif	/* CONFIG_PPC_ADV_DEBUG_REGS */
683d6a61bfcSLuis Machado 
6844a8a9379SRavi Bangoria static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk[HBP_NUM_MAX]);
685a2ceff5eSMichael Ellerman 
6863bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
6873bffb652SDave Kleikamp /*
6883bffb652SDave Kleikamp  * Set the debug registers back to their default "safe" values.
6893bffb652SDave Kleikamp  */
set_debug_reg_defaults(struct thread_struct * thread)6903bffb652SDave Kleikamp static void set_debug_reg_defaults(struct thread_struct *thread)
6913bffb652SDave Kleikamp {
69251ae8d4aSBharat Bhushan 	thread->debug.iac1 = thread->debug.iac2 = 0;
6933bffb652SDave Kleikamp #if CONFIG_PPC_ADV_DEBUG_IACS > 2
69451ae8d4aSBharat Bhushan 	thread->debug.iac3 = thread->debug.iac4 = 0;
6953bffb652SDave Kleikamp #endif
69651ae8d4aSBharat Bhushan 	thread->debug.dac1 = thread->debug.dac2 = 0;
6973bffb652SDave Kleikamp #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
69851ae8d4aSBharat Bhushan 	thread->debug.dvc1 = thread->debug.dvc2 = 0;
6993bffb652SDave Kleikamp #endif
70051ae8d4aSBharat Bhushan 	thread->debug.dbcr0 = 0;
7013bffb652SDave Kleikamp #ifdef CONFIG_BOOKE
7023bffb652SDave Kleikamp 	/*
7033bffb652SDave Kleikamp 	 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
7043bffb652SDave Kleikamp 	 */
70551ae8d4aSBharat Bhushan 	thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
7063bffb652SDave Kleikamp 			DBCR1_IAC3US | DBCR1_IAC4US;
7073bffb652SDave Kleikamp 	/*
7083bffb652SDave Kleikamp 	 * Force Data Address Compare User/Supervisor bits to be User-only
7093bffb652SDave Kleikamp 	 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
7103bffb652SDave Kleikamp 	 */
71151ae8d4aSBharat Bhushan 	thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
7123bffb652SDave Kleikamp #else
71351ae8d4aSBharat Bhushan 	thread->debug.dbcr1 = 0;
7143bffb652SDave Kleikamp #endif
7153bffb652SDave Kleikamp }
7163bffb652SDave Kleikamp 
prime_debug_regs(struct debug_reg * debug)717f5f97210SScott Wood static void prime_debug_regs(struct debug_reg *debug)
7183bffb652SDave Kleikamp {
7196cecf76bSScott Wood 	/*
7206cecf76bSScott Wood 	 * We could have inherited MSR_DE from userspace, since
7216cecf76bSScott Wood 	 * it doesn't get cleared on exception entry.  Make sure
7226cecf76bSScott Wood 	 * MSR_DE is clear before we enable any debug events.
7236cecf76bSScott Wood 	 */
7246cecf76bSScott Wood 	mtmsr(mfmsr() & ~MSR_DE);
7256cecf76bSScott Wood 
726f5f97210SScott Wood 	mtspr(SPRN_IAC1, debug->iac1);
727f5f97210SScott Wood 	mtspr(SPRN_IAC2, debug->iac2);
7283bffb652SDave Kleikamp #if CONFIG_PPC_ADV_DEBUG_IACS > 2
729f5f97210SScott Wood 	mtspr(SPRN_IAC3, debug->iac3);
730f5f97210SScott Wood 	mtspr(SPRN_IAC4, debug->iac4);
7313bffb652SDave Kleikamp #endif
732f5f97210SScott Wood 	mtspr(SPRN_DAC1, debug->dac1);
733f5f97210SScott Wood 	mtspr(SPRN_DAC2, debug->dac2);
7343bffb652SDave Kleikamp #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
735f5f97210SScott Wood 	mtspr(SPRN_DVC1, debug->dvc1);
736f5f97210SScott Wood 	mtspr(SPRN_DVC2, debug->dvc2);
7373bffb652SDave Kleikamp #endif
738f5f97210SScott Wood 	mtspr(SPRN_DBCR0, debug->dbcr0);
739f5f97210SScott Wood 	mtspr(SPRN_DBCR1, debug->dbcr1);
7403bffb652SDave Kleikamp #ifdef CONFIG_BOOKE
741f5f97210SScott Wood 	mtspr(SPRN_DBCR2, debug->dbcr2);
7423bffb652SDave Kleikamp #endif
7433bffb652SDave Kleikamp }
7443bffb652SDave Kleikamp /*
7453bffb652SDave Kleikamp  * Unless neither the old or new thread are making use of the
7463bffb652SDave Kleikamp  * debug registers, set the debug registers from the values
7473bffb652SDave Kleikamp  * stored in the new thread.
7483bffb652SDave Kleikamp  */
switch_booke_debug_regs(struct debug_reg * new_debug)749f5f97210SScott Wood void switch_booke_debug_regs(struct debug_reg *new_debug)
7503bffb652SDave Kleikamp {
75151ae8d4aSBharat Bhushan 	if ((current->thread.debug.dbcr0 & DBCR0_IDM)
752f5f97210SScott Wood 		|| (new_debug->dbcr0 & DBCR0_IDM))
753f5f97210SScott Wood 			prime_debug_regs(new_debug);
7543bffb652SDave Kleikamp }
7553743c9b8SBharat Bhushan EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
7563bffb652SDave Kleikamp #else	/* !CONFIG_PPC_ADV_DEBUG_REGS */
757e0780b72SK.Prasad #ifndef CONFIG_HAVE_HW_BREAKPOINT
set_breakpoint(int i,struct arch_hw_breakpoint * brk)758303e6a9dSRavi Bangoria static void set_breakpoint(int i, struct arch_hw_breakpoint *brk)
759b5ac51d7SChristophe Leroy {
760b5ac51d7SChristophe Leroy 	preempt_disable();
761303e6a9dSRavi Bangoria 	__set_breakpoint(i, brk);
762b5ac51d7SChristophe Leroy 	preempt_enable();
763b5ac51d7SChristophe Leroy }
764b5ac51d7SChristophe Leroy 
set_debug_reg_defaults(struct thread_struct * thread)7653bffb652SDave Kleikamp static void set_debug_reg_defaults(struct thread_struct *thread)
7663bffb652SDave Kleikamp {
767303e6a9dSRavi Bangoria 	int i;
768303e6a9dSRavi Bangoria 	struct arch_hw_breakpoint null_brk = {0};
769303e6a9dSRavi Bangoria 
770303e6a9dSRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
771303e6a9dSRavi Bangoria 		thread->hw_brk[i] = null_brk;
772252988cbSNicholas Piggin 		if (ppc_breakpoint_available())
773303e6a9dSRavi Bangoria 			set_breakpoint(i, &thread->hw_brk[i]);
774303e6a9dSRavi Bangoria 	}
775303e6a9dSRavi Bangoria }
776303e6a9dSRavi Bangoria 
hw_brk_match(struct arch_hw_breakpoint * a,struct arch_hw_breakpoint * b)777303e6a9dSRavi Bangoria static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
778303e6a9dSRavi Bangoria 				struct arch_hw_breakpoint *b)
779303e6a9dSRavi Bangoria {
780303e6a9dSRavi Bangoria 	if (a->address != b->address)
781303e6a9dSRavi Bangoria 		return false;
782303e6a9dSRavi Bangoria 	if (a->type != b->type)
783303e6a9dSRavi Bangoria 		return false;
784303e6a9dSRavi Bangoria 	if (a->len != b->len)
785303e6a9dSRavi Bangoria 		return false;
786303e6a9dSRavi Bangoria 	/* no need to check hw_len. it's calculated from address and len */
787303e6a9dSRavi Bangoria 	return true;
788303e6a9dSRavi Bangoria }
789303e6a9dSRavi Bangoria 
switch_hw_breakpoint(struct task_struct * new)790303e6a9dSRavi Bangoria static void switch_hw_breakpoint(struct task_struct *new)
791303e6a9dSRavi Bangoria {
792303e6a9dSRavi Bangoria 	int i;
793303e6a9dSRavi Bangoria 
794303e6a9dSRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++) {
795303e6a9dSRavi Bangoria 		if (likely(hw_brk_match(this_cpu_ptr(&current_brk[i]),
796303e6a9dSRavi Bangoria 					&new->thread.hw_brk[i])))
797303e6a9dSRavi Bangoria 			continue;
798303e6a9dSRavi Bangoria 
799303e6a9dSRavi Bangoria 		__set_breakpoint(i, &new->thread.hw_brk[i]);
800303e6a9dSRavi Bangoria 	}
8013bffb652SDave Kleikamp }
802e0780b72SK.Prasad #endif /* !CONFIG_HAVE_HW_BREAKPOINT */
8033bffb652SDave Kleikamp #endif	/* CONFIG_PPC_ADV_DEBUG_REGS */
8043bffb652SDave Kleikamp 
set_dabr(struct arch_hw_breakpoint * brk)8059422de3eSMichael Neuling static inline int set_dabr(struct arch_hw_breakpoint *brk)
8069422de3eSMichael Neuling {
8079422de3eSMichael Neuling 	unsigned long dabr, dabrx;
8089422de3eSMichael Neuling 
8099422de3eSMichael Neuling 	dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
8109422de3eSMichael Neuling 	dabrx = ((brk->type >> 3) & 0x7);
8119422de3eSMichael Neuling 
8129422de3eSMichael Neuling 	if (ppc_md.set_dabr)
8139422de3eSMichael Neuling 		return ppc_md.set_dabr(dabr, dabrx);
8149422de3eSMichael Neuling 
815ad3ed15cSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_ADV_DEBUG_REGS)) {
816ad3ed15cSChristophe Leroy 		mtspr(SPRN_DAC1, dabr);
817ad3ed15cSChristophe Leroy 		if (IS_ENABLED(CONFIG_PPC_47x))
818ad3ed15cSChristophe Leroy 			isync();
819ad3ed15cSChristophe Leroy 		return 0;
820ad3ed15cSChristophe Leroy 	} else if (IS_ENABLED(CONFIG_PPC_BOOK3S)) {
821ad3ed15cSChristophe Leroy 		mtspr(SPRN_DABR, dabr);
822ad3ed15cSChristophe Leroy 		if (cpu_has_feature(CPU_FTR_DABRX))
823ad3ed15cSChristophe Leroy 			mtspr(SPRN_DABRX, dabrx);
824ad3ed15cSChristophe Leroy 		return 0;
825ad3ed15cSChristophe Leroy 	} else {
826ad3ed15cSChristophe Leroy 		return -EINVAL;
827ad3ed15cSChristophe Leroy 	}
8289422de3eSMichael Neuling }
8299422de3eSMichael Neuling 
set_breakpoint_8xx(struct arch_hw_breakpoint * brk)83039413ae0SChristophe Leroy static inline int set_breakpoint_8xx(struct arch_hw_breakpoint *brk)
83139413ae0SChristophe Leroy {
83239413ae0SChristophe Leroy 	unsigned long lctrl1 = LCTRL1_CTE_GT | LCTRL1_CTF_LT | LCTRL1_CRWE_RW |
83339413ae0SChristophe Leroy 			       LCTRL1_CRWF_RW;
83439413ae0SChristophe Leroy 	unsigned long lctrl2 = LCTRL2_LW0EN | LCTRL2_LW0LADC | LCTRL2_SLW0EN;
835e68ef121SRavi Bangoria 	unsigned long start_addr = ALIGN_DOWN(brk->address, HW_BREAKPOINT_SIZE);
836e68ef121SRavi Bangoria 	unsigned long end_addr = ALIGN(brk->address + brk->len, HW_BREAKPOINT_SIZE);
83739413ae0SChristophe Leroy 
83839413ae0SChristophe Leroy 	if (start_addr == 0)
83939413ae0SChristophe Leroy 		lctrl2 |= LCTRL2_LW0LA_F;
840e68ef121SRavi Bangoria 	else if (end_addr == 0)
84139413ae0SChristophe Leroy 		lctrl2 |= LCTRL2_LW0LA_E;
84239413ae0SChristophe Leroy 	else
84339413ae0SChristophe Leroy 		lctrl2 |= LCTRL2_LW0LA_EandF;
84439413ae0SChristophe Leroy 
84539413ae0SChristophe Leroy 	mtspr(SPRN_LCTRL2, 0);
84639413ae0SChristophe Leroy 
84739413ae0SChristophe Leroy 	if ((brk->type & HW_BRK_TYPE_RDWR) == 0)
84839413ae0SChristophe Leroy 		return 0;
84939413ae0SChristophe Leroy 
85039413ae0SChristophe Leroy 	if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_READ)
85139413ae0SChristophe Leroy 		lctrl1 |= LCTRL1_CRWE_RO | LCTRL1_CRWF_RO;
85239413ae0SChristophe Leroy 	if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_WRITE)
85339413ae0SChristophe Leroy 		lctrl1 |= LCTRL1_CRWE_WO | LCTRL1_CRWF_WO;
85439413ae0SChristophe Leroy 
85539413ae0SChristophe Leroy 	mtspr(SPRN_CMPE, start_addr - 1);
856e68ef121SRavi Bangoria 	mtspr(SPRN_CMPF, end_addr);
85739413ae0SChristophe Leroy 	mtspr(SPRN_LCTRL1, lctrl1);
85839413ae0SChristophe Leroy 	mtspr(SPRN_LCTRL2, lctrl2);
85939413ae0SChristophe Leroy 
86039413ae0SChristophe Leroy 	return 0;
86139413ae0SChristophe Leroy }
86239413ae0SChristophe Leroy 
set_hw_breakpoint(int nr,struct arch_hw_breakpoint * brk)8633671f4ebSJordan Niethe static void set_hw_breakpoint(int nr, struct arch_hw_breakpoint *brk)
8649422de3eSMichael Neuling {
865c1fe190cSMichael Neuling 	if (dawr_enabled())
866252988cbSNicholas Piggin 		// Power8 or later
8674a8a9379SRavi Bangoria 		set_dawr(nr, brk);
86839413ae0SChristophe Leroy 	else if (IS_ENABLED(CONFIG_PPC_8xx))
86939413ae0SChristophe Leroy 		set_breakpoint_8xx(brk);
870252988cbSNicholas Piggin 	else if (!cpu_has_feature(CPU_FTR_ARCH_207S))
871252988cbSNicholas Piggin 		// Power7 or earlier
87204c32a51SPaul Gortmaker 		set_dabr(brk);
873252988cbSNicholas Piggin 	else
874252988cbSNicholas Piggin 		// Shouldn't happen due to higher level checks
875252988cbSNicholas Piggin 		WARN_ON_ONCE(1);
8769422de3eSMichael Neuling }
87714cf11afSPaul Mackerras 
__set_breakpoint(int nr,struct arch_hw_breakpoint * brk)8783671f4ebSJordan Niethe void __set_breakpoint(int nr, struct arch_hw_breakpoint *brk)
8793671f4ebSJordan Niethe {
8803671f4ebSJordan Niethe 	memcpy(this_cpu_ptr(&current_brk[nr]), brk, sizeof(*brk));
8813671f4ebSJordan Niethe 	set_hw_breakpoint(nr, brk);
8823671f4ebSJordan Niethe }
8833671f4ebSJordan Niethe 
884404b27d6SMichael Neuling /* Check if we have DAWR or DABR hardware */
ppc_breakpoint_available(void)885404b27d6SMichael Neuling bool ppc_breakpoint_available(void)
886404b27d6SMichael Neuling {
887c1fe190cSMichael Neuling 	if (dawr_enabled())
888c1fe190cSMichael Neuling 		return true; /* POWER8 DAWR or POWER9 forced DAWR */
889404b27d6SMichael Neuling 	if (cpu_has_feature(CPU_FTR_ARCH_207S))
890404b27d6SMichael Neuling 		return false; /* POWER9 with DAWR disabled */
891404b27d6SMichael Neuling 	/* DABR: Everything but POWER8 and POWER9 */
892404b27d6SMichael Neuling 	return true;
893404b27d6SMichael Neuling }
894404b27d6SMichael Neuling EXPORT_SYMBOL_GPL(ppc_breakpoint_available);
895404b27d6SMichael Neuling 
8963671f4ebSJordan Niethe /* Disable the breakpoint in hardware without touching current_brk[] */
suspend_breakpoints(void)8973671f4ebSJordan Niethe void suspend_breakpoints(void)
8983671f4ebSJordan Niethe {
8993671f4ebSJordan Niethe 	struct arch_hw_breakpoint brk = {0};
9003671f4ebSJordan Niethe 	int i;
9013671f4ebSJordan Niethe 
9023671f4ebSJordan Niethe 	if (!ppc_breakpoint_available())
9033671f4ebSJordan Niethe 		return;
9043671f4ebSJordan Niethe 
9053671f4ebSJordan Niethe 	for (i = 0; i < nr_wp_slots(); i++)
9063671f4ebSJordan Niethe 		set_hw_breakpoint(i, &brk);
9073671f4ebSJordan Niethe }
9083671f4ebSJordan Niethe 
9093671f4ebSJordan Niethe /*
9103671f4ebSJordan Niethe  * Re-enable breakpoints suspended by suspend_breakpoints() in hardware
9113671f4ebSJordan Niethe  * from current_brk[]
9123671f4ebSJordan Niethe  */
restore_breakpoints(void)9133671f4ebSJordan Niethe void restore_breakpoints(void)
9143671f4ebSJordan Niethe {
9153671f4ebSJordan Niethe 	int i;
9163671f4ebSJordan Niethe 
9173671f4ebSJordan Niethe 	if (!ppc_breakpoint_available())
9183671f4ebSJordan Niethe 		return;
9193671f4ebSJordan Niethe 
9203671f4ebSJordan Niethe 	for (i = 0; i < nr_wp_slots(); i++)
9213671f4ebSJordan Niethe 		set_hw_breakpoint(i, this_cpu_ptr(&current_brk[i]));
9223671f4ebSJordan Niethe }
9233671f4ebSJordan Niethe 
924fb09692eSMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
9255d176f75SCyril Bur 
tm_enabled(struct task_struct * tsk)9265d176f75SCyril Bur static inline bool tm_enabled(struct task_struct *tsk)
9275d176f75SCyril Bur {
9285d176f75SCyril Bur 	return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM);
9295d176f75SCyril Bur }
9305d176f75SCyril Bur 
tm_reclaim_thread(struct thread_struct * thr,uint8_t cause)931edd00b83SCyril Bur static void tm_reclaim_thread(struct thread_struct *thr, uint8_t cause)
932d31626f7SPaul Mackerras {
9337f821fc9SMichael Neuling 	/*
9347f821fc9SMichael Neuling 	 * Use the current MSR TM suspended bit to track if we have
9357f821fc9SMichael Neuling 	 * checkpointed state outstanding.
9367f821fc9SMichael Neuling 	 * On signal delivery, we'd normally reclaim the checkpointed
9377f821fc9SMichael Neuling 	 * state to obtain stack pointer (see:get_tm_stackpointer()).
9387f821fc9SMichael Neuling 	 * This will then directly return to userspace without going
9397f821fc9SMichael Neuling 	 * through __switch_to(). However, if the stack frame is bad,
9407f821fc9SMichael Neuling 	 * we need to exit this thread which calls __switch_to() which
9417f821fc9SMichael Neuling 	 * will again attempt to reclaim the already saved tm state.
9427f821fc9SMichael Neuling 	 * Hence we need to check that we've not already reclaimed
9437f821fc9SMichael Neuling 	 * this state.
9447f821fc9SMichael Neuling 	 * We do this using the current MSR, rather tracking it in
9457f821fc9SMichael Neuling 	 * some specific thread_struct bit, as it has the additional
946027dfac6SMichael Ellerman 	 * benefit of checking for a potential TM bad thing exception.
9477f821fc9SMichael Neuling 	 */
9487f821fc9SMichael Neuling 	if (!MSR_TM_SUSPENDED(mfmsr()))
9497f821fc9SMichael Neuling 		return;
9507f821fc9SMichael Neuling 
95191381b9cSCyril Bur 	giveup_all(container_of(thr, struct task_struct, thread));
95291381b9cSCyril Bur 
953eb5c3f1cSCyril Bur 	tm_reclaim(thr, cause);
954eb5c3f1cSCyril Bur 
955f48e91e8SMichael Neuling 	/*
956f48e91e8SMichael Neuling 	 * If we are in a transaction and FP is off then we can't have
957f48e91e8SMichael Neuling 	 * used FP inside that transaction. Hence the checkpointed
958f48e91e8SMichael Neuling 	 * state is the same as the live state. We need to copy the
959f48e91e8SMichael Neuling 	 * live state to the checkpointed state so that when the
960f48e91e8SMichael Neuling 	 * transaction is restored, the checkpointed state is correct
961f48e91e8SMichael Neuling 	 * and the aborted transaction sees the correct state. We use
962f48e91e8SMichael Neuling 	 * ckpt_regs.msr here as that's what tm_reclaim will use to
963f48e91e8SMichael Neuling 	 * determine if it's going to write the checkpointed state or
964f48e91e8SMichael Neuling 	 * not. So either this will write the checkpointed registers,
965f48e91e8SMichael Neuling 	 * or reclaim will. Similarly for VMX.
966f48e91e8SMichael Neuling 	 */
967f48e91e8SMichael Neuling 	if ((thr->ckpt_regs.msr & MSR_FP) == 0)
968f48e91e8SMichael Neuling 		memcpy(&thr->ckfp_state, &thr->fp_state,
969f48e91e8SMichael Neuling 		       sizeof(struct thread_fp_state));
970f48e91e8SMichael Neuling 	if ((thr->ckpt_regs.msr & MSR_VEC) == 0)
971f48e91e8SMichael Neuling 		memcpy(&thr->ckvr_state, &thr->vr_state,
972f48e91e8SMichael Neuling 		       sizeof(struct thread_vr_state));
973d31626f7SPaul Mackerras }
974d31626f7SPaul Mackerras 
tm_reclaim_current(uint8_t cause)975d31626f7SPaul Mackerras void tm_reclaim_current(uint8_t cause)
976d31626f7SPaul Mackerras {
977d31626f7SPaul Mackerras 	tm_enable();
978edd00b83SCyril Bur 	tm_reclaim_thread(&current->thread, cause);
979d31626f7SPaul Mackerras }
980d31626f7SPaul Mackerras 
tm_reclaim_task(struct task_struct * tsk)981fb09692eSMichael Neuling static inline void tm_reclaim_task(struct task_struct *tsk)
982fb09692eSMichael Neuling {
983fb09692eSMichael Neuling 	/* We have to work out if we're switching from/to a task that's in the
984fb09692eSMichael Neuling 	 * middle of a transaction.
985fb09692eSMichael Neuling 	 *
986fb09692eSMichael Neuling 	 * In switching we need to maintain a 2nd register state as
987fb09692eSMichael Neuling 	 * oldtask->thread.ckpt_regs.  We tm_reclaim(oldproc); this saves the
988000ec280SCyril Bur 	 * checkpointed (tbegin) state in ckpt_regs, ckfp_state and
989000ec280SCyril Bur 	 * ckvr_state
990fb09692eSMichael Neuling 	 *
991fb09692eSMichael Neuling 	 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
992fb09692eSMichael Neuling 	 */
993fb09692eSMichael Neuling 	struct thread_struct *thr = &tsk->thread;
994fb09692eSMichael Neuling 
995fb09692eSMichael Neuling 	if (!thr->regs)
996fb09692eSMichael Neuling 		return;
997fb09692eSMichael Neuling 
998fb09692eSMichael Neuling 	if (!MSR_TM_ACTIVE(thr->regs->msr))
999fb09692eSMichael Neuling 		goto out_and_saveregs;
1000fb09692eSMichael Neuling 
100192fb8690SMichael Neuling 	WARN_ON(tm_suspend_disabled);
100292fb8690SMichael Neuling 
1003fb09692eSMichael Neuling 	TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
1004fb09692eSMichael Neuling 		 "ccr=%lx, msr=%lx, trap=%lx)\n",
1005fb09692eSMichael Neuling 		 tsk->pid, thr->regs->nip,
1006fb09692eSMichael Neuling 		 thr->regs->ccr, thr->regs->msr,
1007fb09692eSMichael Neuling 		 thr->regs->trap);
1008fb09692eSMichael Neuling 
1009edd00b83SCyril Bur 	tm_reclaim_thread(thr, TM_CAUSE_RESCHED);
1010fb09692eSMichael Neuling 
1011fb09692eSMichael Neuling 	TM_DEBUG("--- tm_reclaim on pid %d complete\n",
1012fb09692eSMichael Neuling 		 tsk->pid);
1013fb09692eSMichael Neuling 
1014fb09692eSMichael Neuling out_and_saveregs:
1015fb09692eSMichael Neuling 	/* Always save the regs here, even if a transaction's not active.
1016fb09692eSMichael Neuling 	 * This context-switches a thread's TM info SPRs.  We do it here to
1017fb09692eSMichael Neuling 	 * be consistent with the restore path (in recheckpoint) which
1018fb09692eSMichael Neuling 	 * cannot happen later in _switch().
1019fb09692eSMichael Neuling 	 */
1020fb09692eSMichael Neuling 	tm_save_sprs(thr);
1021fb09692eSMichael Neuling }
1022fb09692eSMichael Neuling 
1023eb5c3f1cSCyril Bur extern void __tm_recheckpoint(struct thread_struct *thread);
1024e6b8fd02SMichael Neuling 
tm_recheckpoint(struct thread_struct * thread)1025eb5c3f1cSCyril Bur void tm_recheckpoint(struct thread_struct *thread)
1026e6b8fd02SMichael Neuling {
1027e6b8fd02SMichael Neuling 	unsigned long flags;
1028e6b8fd02SMichael Neuling 
10295d176f75SCyril Bur 	if (!(thread->regs->msr & MSR_TM))
10305d176f75SCyril Bur 		return;
10315d176f75SCyril Bur 
1032e6b8fd02SMichael Neuling 	/* We really can't be interrupted here as the TEXASR registers can't
1033e6b8fd02SMichael Neuling 	 * change and later in the trecheckpoint code, we have a userspace R1.
1034e6b8fd02SMichael Neuling 	 * So let's hard disable over this region.
1035e6b8fd02SMichael Neuling 	 */
1036e6b8fd02SMichael Neuling 	local_irq_save(flags);
1037e6b8fd02SMichael Neuling 	hard_irq_disable();
1038e6b8fd02SMichael Neuling 
1039e6b8fd02SMichael Neuling 	/* The TM SPRs are restored here, so that TEXASR.FS can be set
1040e6b8fd02SMichael Neuling 	 * before the trecheckpoint and no explosion occurs.
1041e6b8fd02SMichael Neuling 	 */
1042e6b8fd02SMichael Neuling 	tm_restore_sprs(thread);
1043e6b8fd02SMichael Neuling 
1044eb5c3f1cSCyril Bur 	__tm_recheckpoint(thread);
1045e6b8fd02SMichael Neuling 
1046e6b8fd02SMichael Neuling 	local_irq_restore(flags);
1047e6b8fd02SMichael Neuling }
1048e6b8fd02SMichael Neuling 
tm_recheckpoint_new_task(struct task_struct * new)1049bc2a9408SMichael Neuling static inline void tm_recheckpoint_new_task(struct task_struct *new)
1050fb09692eSMichael Neuling {
1051fb09692eSMichael Neuling 	if (!cpu_has_feature(CPU_FTR_TM))
1052fb09692eSMichael Neuling 		return;
1053fb09692eSMichael Neuling 
1054fb09692eSMichael Neuling 	/* Recheckpoint the registers of the thread we're about to switch to.
1055fb09692eSMichael Neuling 	 *
1056fb09692eSMichael Neuling 	 * If the task was using FP, we non-lazily reload both the original and
1057fb09692eSMichael Neuling 	 * the speculative FP register states.  This is because the kernel
1058fb09692eSMichael Neuling 	 * doesn't see if/when a TM rollback occurs, so if we take an FP
1059dc310669SCyril Bur 	 * unavailable later, we are unable to determine which set of FP regs
1060fb09692eSMichael Neuling 	 * need to be restored.
1061fb09692eSMichael Neuling 	 */
10625d176f75SCyril Bur 	if (!tm_enabled(new))
1063fb09692eSMichael Neuling 		return;
1064fb09692eSMichael Neuling 
1065e6b8fd02SMichael Neuling 	if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
1066fb09692eSMichael Neuling 		tm_restore_sprs(&new->thread);
1067fb09692eSMichael Neuling 		return;
1068e6b8fd02SMichael Neuling 	}
1069fb09692eSMichael Neuling 	/* Recheckpoint to restore original checkpointed register state. */
1070eb5c3f1cSCyril Bur 	TM_DEBUG("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n",
1071eb5c3f1cSCyril Bur 		 new->pid, new->thread.regs->msr);
1072fb09692eSMichael Neuling 
1073eb5c3f1cSCyril Bur 	tm_recheckpoint(&new->thread);
1074fb09692eSMichael Neuling 
1075dc310669SCyril Bur 	/*
1076dc310669SCyril Bur 	 * The checkpointed state has been restored but the live state has
1077dc310669SCyril Bur 	 * not, ensure all the math functionality is turned off to trigger
1078dc310669SCyril Bur 	 * restore_math() to reload.
1079dc310669SCyril Bur 	 */
1080dc310669SCyril Bur 	new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX);
1081fb09692eSMichael Neuling 
1082fb09692eSMichael Neuling 	TM_DEBUG("*** tm_recheckpoint of pid %d complete "
1083fb09692eSMichael Neuling 		 "(kernel msr 0x%lx)\n",
1084fb09692eSMichael Neuling 		 new->pid, mfmsr());
1085fb09692eSMichael Neuling }
1086fb09692eSMichael Neuling 
__switch_to_tm(struct task_struct * prev,struct task_struct * new)1087dc310669SCyril Bur static inline void __switch_to_tm(struct task_struct *prev,
1088dc310669SCyril Bur 		struct task_struct *new)
1089fb09692eSMichael Neuling {
1090fb09692eSMichael Neuling 	if (cpu_has_feature(CPU_FTR_TM)) {
10915d176f75SCyril Bur 		if (tm_enabled(prev) || tm_enabled(new))
1092fb09692eSMichael Neuling 			tm_enable();
10935d176f75SCyril Bur 
10945d176f75SCyril Bur 		if (tm_enabled(prev)) {
10955d176f75SCyril Bur 			prev->thread.load_tm++;
1096fb09692eSMichael Neuling 			tm_reclaim_task(prev);
10975d176f75SCyril Bur 			if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0)
10985d176f75SCyril Bur 				prev->thread.regs->msr &= ~MSR_TM;
10995d176f75SCyril Bur 		}
11005d176f75SCyril Bur 
1101dc310669SCyril Bur 		tm_recheckpoint_new_task(new);
1102fb09692eSMichael Neuling 	}
1103fb09692eSMichael Neuling }
1104d31626f7SPaul Mackerras 
1105d31626f7SPaul Mackerras /*
1106d31626f7SPaul Mackerras  * This is called if we are on the way out to userspace and the
1107d31626f7SPaul Mackerras  * TIF_RESTORE_TM flag is set.  It checks if we need to reload
1108d31626f7SPaul Mackerras  * FP and/or vector state and does so if necessary.
1109d31626f7SPaul Mackerras  * If userspace is inside a transaction (whether active or
1110d31626f7SPaul Mackerras  * suspended) and FP/VMX/VSX instructions have ever been enabled
1111d31626f7SPaul Mackerras  * inside that transaction, then we have to keep them enabled
1112d31626f7SPaul Mackerras  * and keep the FP/VMX/VSX state loaded while ever the transaction
1113d31626f7SPaul Mackerras  * continues.  The reason is that if we didn't, and subsequently
1114d31626f7SPaul Mackerras  * got a FP/VMX/VSX unavailable interrupt inside a transaction,
1115d31626f7SPaul Mackerras  * we don't know whether it's the same transaction, and thus we
1116d31626f7SPaul Mackerras  * don't know which of the checkpointed state and the transactional
1117d31626f7SPaul Mackerras  * state to use.
1118d31626f7SPaul Mackerras  */
restore_tm_state(struct pt_regs * regs)1119d31626f7SPaul Mackerras void restore_tm_state(struct pt_regs *regs)
1120d31626f7SPaul Mackerras {
1121d31626f7SPaul Mackerras 	unsigned long msr_diff;
1122d31626f7SPaul Mackerras 
1123dc310669SCyril Bur 	/*
1124dc310669SCyril Bur 	 * This is the only moment we should clear TIF_RESTORE_TM as
1125dc310669SCyril Bur 	 * it is here that ckpt_regs.msr and pt_regs.msr become the same
1126dc310669SCyril Bur 	 * again, anything else could lead to an incorrect ckpt_msr being
1127dc310669SCyril Bur 	 * saved and therefore incorrect signal contexts.
1128dc310669SCyril Bur 	 */
1129d31626f7SPaul Mackerras 	clear_thread_flag(TIF_RESTORE_TM);
1130d31626f7SPaul Mackerras 	if (!MSR_TM_ACTIVE(regs->msr))
1131d31626f7SPaul Mackerras 		return;
1132d31626f7SPaul Mackerras 
1133829023dfSAnshuman Khandual 	msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
1134d31626f7SPaul Mackerras 	msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
113570fe3d98SCyril Bur 
1136dc16b553SCyril Bur 	/* Ensure that restore_math() will restore */
1137dc16b553SCyril Bur 	if (msr_diff & MSR_FP)
1138dc16b553SCyril Bur 		current->thread.load_fp = 1;
113939715bf9SValentin Rothberg #ifdef CONFIG_ALTIVEC
1140dc16b553SCyril Bur 	if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC)
1141dc16b553SCyril Bur 		current->thread.load_vec = 1;
1142dc16b553SCyril Bur #endif
114370fe3d98SCyril Bur 	restore_math(regs);
114470fe3d98SCyril Bur 
114559dc5bfcSNicholas Piggin 	regs_set_return_msr(regs, regs->msr | msr_diff);
1146d31626f7SPaul Mackerras }
1147d31626f7SPaul Mackerras 
11482d19630eSChristopher M. Riedl #else /* !CONFIG_PPC_TRANSACTIONAL_MEM */
1149fb09692eSMichael Neuling #define tm_recheckpoint_new_task(new)
1150dc310669SCyril Bur #define __switch_to_tm(prev, new)
tm_reclaim_current(uint8_t cause)11512d19630eSChristopher M. Riedl void tm_reclaim_current(uint8_t cause) {}
1152fb09692eSMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
11539422de3eSMichael Neuling 
save_sprs(struct thread_struct * t)1154152d523eSAnton Blanchard static inline void save_sprs(struct thread_struct *t)
1155152d523eSAnton Blanchard {
1156152d523eSAnton Blanchard #ifdef CONFIG_ALTIVEC
115701d7c2a2SOliver O'Halloran 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
1158152d523eSAnton Blanchard 		t->vrsave = mfspr(SPRN_VRSAVE);
1159152d523eSAnton Blanchard #endif
1160359c2ca7SChristophe Leroy #ifdef CONFIG_SPE
1161359c2ca7SChristophe Leroy 	if (cpu_has_feature(CPU_FTR_SPE))
1162359c2ca7SChristophe Leroy 		t->spefscr = mfspr(SPRN_SPEFSCR);
1163359c2ca7SChristophe Leroy #endif
1164152d523eSAnton Blanchard #ifdef CONFIG_PPC_BOOK3S_64
1165152d523eSAnton Blanchard 	if (cpu_has_feature(CPU_FTR_DSCR))
1166152d523eSAnton Blanchard 		t->dscr = mfspr(SPRN_DSCR);
1167152d523eSAnton Blanchard 
1168152d523eSAnton Blanchard 	if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1169152d523eSAnton Blanchard 		t->bescr = mfspr(SPRN_BESCR);
1170152d523eSAnton Blanchard 		t->ebbhr = mfspr(SPRN_EBBHR);
1171152d523eSAnton Blanchard 		t->ebbrr = mfspr(SPRN_EBBRR);
1172152d523eSAnton Blanchard 
1173152d523eSAnton Blanchard 		t->fscr = mfspr(SPRN_FSCR);
1174152d523eSAnton Blanchard 
1175152d523eSAnton Blanchard 		/*
1176152d523eSAnton Blanchard 		 * Note that the TAR is not available for use in the kernel.
1177152d523eSAnton Blanchard 		 * (To provide this, the TAR should be backed up/restored on
1178152d523eSAnton Blanchard 		 * exception entry/exit instead, and be in pt_regs.  FIXME,
1179152d523eSAnton Blanchard 		 * this should be in pt_regs anyway (for debug).)
1180152d523eSAnton Blanchard 		 */
1181152d523eSAnton Blanchard 		t->tar = mfspr(SPRN_TAR);
1182152d523eSAnton Blanchard 	}
1183be98fcf7SBenjamin Gray 
1184be98fcf7SBenjamin Gray 	if (cpu_has_feature(CPU_FTR_DEXCR_NPHIE))
1185be98fcf7SBenjamin Gray 		t->hashkeyr = mfspr(SPRN_HASHKEYR);
118675171f06SBenjamin Gray 
118775171f06SBenjamin Gray 	if (cpu_has_feature(CPU_FTR_ARCH_31))
118875171f06SBenjamin Gray 		t->dexcr = mfspr(SPRN_DEXCR);
1189152d523eSAnton Blanchard #endif
1190152d523eSAnton Blanchard }
1191152d523eSAnton Blanchard 
119234e119c9SNicholas Piggin #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
kvmppc_save_user_regs(void)119334e119c9SNicholas Piggin void kvmppc_save_user_regs(void)
119434e119c9SNicholas Piggin {
119534e119c9SNicholas Piggin 	unsigned long usermsr;
119634e119c9SNicholas Piggin 
119734e119c9SNicholas Piggin 	if (!current->thread.regs)
119834e119c9SNicholas Piggin 		return;
119934e119c9SNicholas Piggin 
120034e119c9SNicholas Piggin 	usermsr = current->thread.regs->msr;
120134e119c9SNicholas Piggin 
1202dc158d23SNicholas Piggin 	/* Caller has enabled FP/VEC/VSX/TM in MSR */
120334e119c9SNicholas Piggin 	if (usermsr & MSR_FP)
1204dc158d23SNicholas Piggin 		__giveup_fpu(current);
120534e119c9SNicholas Piggin 	if (usermsr & MSR_VEC)
1206dc158d23SNicholas Piggin 		__giveup_altivec(current);
120734e119c9SNicholas Piggin 
120834e119c9SNicholas Piggin #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
120934e119c9SNicholas Piggin 	if (usermsr & MSR_TM) {
121034e119c9SNicholas Piggin 		current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
121134e119c9SNicholas Piggin 		current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
121234e119c9SNicholas Piggin 		current->thread.tm_texasr = mfspr(SPRN_TEXASR);
121334e119c9SNicholas Piggin 		current->thread.regs->msr &= ~MSR_TM;
121434e119c9SNicholas Piggin 	}
121534e119c9SNicholas Piggin #endif
121634e119c9SNicholas Piggin }
121734e119c9SNicholas Piggin EXPORT_SYMBOL_GPL(kvmppc_save_user_regs);
12185236756dSNicholas Piggin 
kvmppc_save_current_sprs(void)12195236756dSNicholas Piggin void kvmppc_save_current_sprs(void)
12205236756dSNicholas Piggin {
12215236756dSNicholas Piggin 	save_sprs(&current->thread);
12225236756dSNicholas Piggin }
12235236756dSNicholas Piggin EXPORT_SYMBOL_GPL(kvmppc_save_current_sprs);
122434e119c9SNicholas Piggin #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
122534e119c9SNicholas Piggin 
restore_sprs(struct thread_struct * old_thread,struct thread_struct * new_thread)1226152d523eSAnton Blanchard static inline void restore_sprs(struct thread_struct *old_thread,
1227152d523eSAnton Blanchard 				struct thread_struct *new_thread)
1228152d523eSAnton Blanchard {
1229152d523eSAnton Blanchard #ifdef CONFIG_ALTIVEC
1230152d523eSAnton Blanchard 	if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
1231152d523eSAnton Blanchard 	    old_thread->vrsave != new_thread->vrsave)
1232152d523eSAnton Blanchard 		mtspr(SPRN_VRSAVE, new_thread->vrsave);
1233152d523eSAnton Blanchard #endif
1234359c2ca7SChristophe Leroy #ifdef CONFIG_SPE
1235359c2ca7SChristophe Leroy 	if (cpu_has_feature(CPU_FTR_SPE) &&
1236359c2ca7SChristophe Leroy 	    old_thread->spefscr != new_thread->spefscr)
1237359c2ca7SChristophe Leroy 		mtspr(SPRN_SPEFSCR, new_thread->spefscr);
1238359c2ca7SChristophe Leroy #endif
1239152d523eSAnton Blanchard #ifdef CONFIG_PPC_BOOK3S_64
1240152d523eSAnton Blanchard 	if (cpu_has_feature(CPU_FTR_DSCR)) {
1241152d523eSAnton Blanchard 		u64 dscr = get_paca()->dscr_default;
1242b57bd2deSMichael Neuling 		if (new_thread->dscr_inherit)
1243152d523eSAnton Blanchard 			dscr = new_thread->dscr;
1244152d523eSAnton Blanchard 
1245152d523eSAnton Blanchard 		if (old_thread->dscr != dscr)
1246152d523eSAnton Blanchard 			mtspr(SPRN_DSCR, dscr);
1247152d523eSAnton Blanchard 	}
1248152d523eSAnton Blanchard 
1249152d523eSAnton Blanchard 	if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1250152d523eSAnton Blanchard 		if (old_thread->bescr != new_thread->bescr)
1251152d523eSAnton Blanchard 			mtspr(SPRN_BESCR, new_thread->bescr);
1252152d523eSAnton Blanchard 		if (old_thread->ebbhr != new_thread->ebbhr)
1253152d523eSAnton Blanchard 			mtspr(SPRN_EBBHR, new_thread->ebbhr);
1254152d523eSAnton Blanchard 		if (old_thread->ebbrr != new_thread->ebbrr)
1255152d523eSAnton Blanchard 			mtspr(SPRN_EBBRR, new_thread->ebbrr);
1256152d523eSAnton Blanchard 
1257b57bd2deSMichael Neuling 		if (old_thread->fscr != new_thread->fscr)
1258b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, new_thread->fscr);
1259b57bd2deSMichael Neuling 
1260152d523eSAnton Blanchard 		if (old_thread->tar != new_thread->tar)
1261152d523eSAnton Blanchard 			mtspr(SPRN_TAR, new_thread->tar);
1262152d523eSAnton Blanchard 	}
1263ec233edeSSukadev Bhattiprolu 
12643449f191SAlastair D'Silva 	if (cpu_has_feature(CPU_FTR_P9_TIDR) &&
1265ec233edeSSukadev Bhattiprolu 	    old_thread->tidr != new_thread->tidr)
1266ec233edeSSukadev Bhattiprolu 		mtspr(SPRN_TIDR, new_thread->tidr);
1267be98fcf7SBenjamin Gray 
1268be98fcf7SBenjamin Gray 	if (cpu_has_feature(CPU_FTR_DEXCR_NPHIE) &&
1269be98fcf7SBenjamin Gray 	    old_thread->hashkeyr != new_thread->hashkeyr)
1270be98fcf7SBenjamin Gray 		mtspr(SPRN_HASHKEYR, new_thread->hashkeyr);
127175171f06SBenjamin Gray 
127275171f06SBenjamin Gray 	if (cpu_has_feature(CPU_FTR_ARCH_31) &&
127375171f06SBenjamin Gray 	    old_thread->dexcr != new_thread->dexcr)
127475171f06SBenjamin Gray 		mtspr(SPRN_DEXCR, new_thread->dexcr);
1275152d523eSAnton Blanchard #endif
127606bb53b3SRam Pai 
1277152d523eSAnton Blanchard }
1278152d523eSAnton Blanchard 
__switch_to(struct task_struct * prev,struct task_struct * new)127914cf11afSPaul Mackerras struct task_struct *__switch_to(struct task_struct *prev,
128014cf11afSPaul Mackerras 	struct task_struct *new)
128114cf11afSPaul Mackerras {
128214cf11afSPaul Mackerras 	struct thread_struct *new_thread, *old_thread;
128314cf11afSPaul Mackerras 	struct task_struct *last;
1284387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
1285d6bf29b4SPeter Zijlstra 	struct ppc64_tlb_batch *batch;
1286d6bf29b4SPeter Zijlstra #endif
128714cf11afSPaul Mackerras 
1288152d523eSAnton Blanchard 	new_thread = &new->thread;
1289152d523eSAnton Blanchard 	old_thread = &current->thread;
1290152d523eSAnton Blanchard 
12917ba5fef7SMichael Neuling 	WARN_ON(!irqs_disabled());
12927ba5fef7SMichael Neuling 
1293387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
129469111bacSChristoph Lameter 	batch = this_cpu_ptr(&ppc64_tlb_batch);
1295d6bf29b4SPeter Zijlstra 	if (batch->active) {
1296d6bf29b4SPeter Zijlstra 		current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1297d6bf29b4SPeter Zijlstra 		if (batch->index)
1298d6bf29b4SPeter Zijlstra 			__flush_tlb_pending(batch);
1299d6bf29b4SPeter Zijlstra 		batch->active = 0;
1300d6bf29b4SPeter Zijlstra 	}
1301f35d2f24SNicholas Piggin 
1302f35d2f24SNicholas Piggin 	/*
1303f35d2f24SNicholas Piggin 	 * On POWER9 the copy-paste buffer can only paste into
1304f35d2f24SNicholas Piggin 	 * foreign real addresses, so unprivileged processes can not
1305f35d2f24SNicholas Piggin 	 * see the data or use it in any way unless they have
1306f35d2f24SNicholas Piggin 	 * foreign real mappings. If the new process has the foreign
1307f35d2f24SNicholas Piggin 	 * real address mappings, we must issue a cp_abort to clear
1308f35d2f24SNicholas Piggin 	 * any state and prevent snooping, corruption or a covert
1309f35d2f24SNicholas Piggin 	 * channel. ISA v3.1 supports paste into local memory.
1310f35d2f24SNicholas Piggin 	 */
1311f35d2f24SNicholas Piggin 	if (new->mm && (cpu_has_feature(CPU_FTR_ARCH_31) ||
1312f35d2f24SNicholas Piggin 			atomic_read(&new->mm->context.vas_windows)))
1313f35d2f24SNicholas Piggin 		asm volatile(PPC_CP_ABORT);
13144e003747SMichael Ellerman #endif /* CONFIG_PPC_BOOK3S_64 */
131506d67d54SPaul Mackerras 
1316f3d885ccSAnton Blanchard #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1317f3d885ccSAnton Blanchard 	switch_booke_debug_regs(&new->thread.debug);
1318f3d885ccSAnton Blanchard #else
1319f3d885ccSAnton Blanchard /*
1320f3d885ccSAnton Blanchard  * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1321f3d885ccSAnton Blanchard  * schedule DABR
1322f3d885ccSAnton Blanchard  */
1323f3d885ccSAnton Blanchard #ifndef CONFIG_HAVE_HW_BREAKPOINT
1324303e6a9dSRavi Bangoria 	switch_hw_breakpoint(new);
1325f3d885ccSAnton Blanchard #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1326f3d885ccSAnton Blanchard #endif
1327f3d885ccSAnton Blanchard 
1328f3d885ccSAnton Blanchard 	/*
1329f3d885ccSAnton Blanchard 	 * We need to save SPRs before treclaim/trecheckpoint as these will
1330f3d885ccSAnton Blanchard 	 * change a number of them.
1331f3d885ccSAnton Blanchard 	 */
1332f3d885ccSAnton Blanchard 	save_sprs(&prev->thread);
1333f3d885ccSAnton Blanchard 
1334f3d885ccSAnton Blanchard 	/* Save FPU, Altivec, VSX and SPE state */
1335f3d885ccSAnton Blanchard 	giveup_all(prev);
1336f3d885ccSAnton Blanchard 
1337dc310669SCyril Bur 	__switch_to_tm(prev, new);
1338dc310669SCyril Bur 
1339e4c0fc5fSNicholas Piggin 	if (!radix_enabled()) {
134044387e9fSAnton Blanchard 		/*
1341e4c0fc5fSNicholas Piggin 		 * We can't take a PMU exception inside _switch() since there
1342e4c0fc5fSNicholas Piggin 		 * is a window where the kernel stack SLB and the kernel stack
1343e4c0fc5fSNicholas Piggin 		 * are out of sync. Hard disable here.
134444387e9fSAnton Blanchard 		 */
134544387e9fSAnton Blanchard 		hard_irq_disable();
1346e4c0fc5fSNicholas Piggin 	}
1347bc2a9408SMichael Neuling 
134820dbe670SAnton Blanchard 	/*
134959dc5bfcSNicholas Piggin 	 * Call restore_sprs() and set_return_regs_changed() before calling
135059dc5bfcSNicholas Piggin 	 * _switch(). If we move it after _switch() then we miss out on calling
135159dc5bfcSNicholas Piggin 	 * it for new tasks. The reason for this is we manually create a stack
135259dc5bfcSNicholas Piggin 	 * frame for new tasks that directly returns through ret_from_fork() or
135320dbe670SAnton Blanchard 	 * ret_from_kernel_thread(). See copy_thread() for details.
135420dbe670SAnton Blanchard 	 */
1355f3d885ccSAnton Blanchard 	restore_sprs(old_thread, new_thread);
1356f3d885ccSAnton Blanchard 
135759dc5bfcSNicholas Piggin 	set_return_regs_changed(); /* _switch changes stack (and regs) */
135859dc5bfcSNicholas Piggin 
135942e03bc5SChristophe Leroy 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
1360c1672883SChristophe Leroy 		kuap_assert_locked();
136142e03bc5SChristophe Leroy 
136220dbe670SAnton Blanchard 	last = _switch(old_thread, new_thread);
136320dbe670SAnton Blanchard 
1364f35d2f24SNicholas Piggin 	/*
1365f35d2f24SNicholas Piggin 	 * Nothing after _switch will be run for newly created tasks,
1366f35d2f24SNicholas Piggin 	 * because they switch directly to ret_from_fork/ret_from_kernel_thread
1367f35d2f24SNicholas Piggin 	 * etc. Code added here should have a comment explaining why that is
1368f35d2f24SNicholas Piggin 	 * okay.
1369f35d2f24SNicholas Piggin 	 */
1370f35d2f24SNicholas Piggin 
13714e003747SMichael Ellerman #ifdef CONFIG_PPC_BOOK3S_64
1372387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
1373f35d2f24SNicholas Piggin 	/*
1374f35d2f24SNicholas Piggin 	 * This applies to a process that was context switched while inside
1375f35d2f24SNicholas Piggin 	 * arch_enter_lazy_mmu_mode(), to re-activate the batch that was
1376f35d2f24SNicholas Piggin 	 * deactivated above, before _switch(). This will never be the case
1377f35d2f24SNicholas Piggin 	 * for new tasks.
1378f35d2f24SNicholas Piggin 	 */
1379d6bf29b4SPeter Zijlstra 	if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1380d6bf29b4SPeter Zijlstra 		current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
138169111bacSChristoph Lameter 		batch = this_cpu_ptr(&ppc64_tlb_batch);
1382d6bf29b4SPeter Zijlstra 		batch->active = 1;
1383d6bf29b4SPeter Zijlstra 	}
1384387e220aSNicholas Piggin #endif
138570fe3d98SCyril Bur 
138607d2a628SNicholas Piggin 	/*
1387f35d2f24SNicholas Piggin 	 * Math facilities are masked out of the child MSR in copy_thread.
1388f35d2f24SNicholas Piggin 	 * A new task does not need to restore_math because it will
1389f35d2f24SNicholas Piggin 	 * demand fault them.
139007d2a628SNicholas Piggin 	 */
1391f35d2f24SNicholas Piggin 	if (current->thread.regs)
1392f35d2f24SNicholas Piggin 		restore_math(current->thread.regs);
13934e003747SMichael Ellerman #endif /* CONFIG_PPC_BOOK3S_64 */
1394d6bf29b4SPeter Zijlstra 
139514cf11afSPaul Mackerras 	return last;
139614cf11afSPaul Mackerras }
139714cf11afSPaul Mackerras 
1398df13102fSChristophe Leroy #define NR_INSN_TO_PRINT	16
139906d67d54SPaul Mackerras 
show_instructions(struct pt_regs * regs)140006d67d54SPaul Mackerras static void show_instructions(struct pt_regs *regs)
140106d67d54SPaul Mackerras {
140206d67d54SPaul Mackerras 	int i;
1403a6e2c226SAneesh Kumar K.V 	unsigned long nip = regs->nip;
1404df13102fSChristophe Leroy 	unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
140506d67d54SPaul Mackerras 
14063e654127SMichael Ellerman 	printk("Code: ");
140706d67d54SPaul Mackerras 
1408a6e2c226SAneesh Kumar K.V 	/*
1409a6e2c226SAneesh Kumar K.V 	 * If we were executing with the MMU off for instructions, adjust pc
1410a6e2c226SAneesh Kumar K.V 	 * rather than printing XXXXXXXX.
1411a6e2c226SAneesh Kumar K.V 	 */
1412a6e2c226SAneesh Kumar K.V 	if (!IS_ENABLED(CONFIG_BOOKE) && !(regs->msr & MSR_IR)) {
1413a6e2c226SAneesh Kumar K.V 		pc = (unsigned long)phys_to_virt(pc);
1414a6e2c226SAneesh Kumar K.V 		nip = (unsigned long)phys_to_virt(regs->nip);
1415a6e2c226SAneesh Kumar K.V 	}
1416a6e2c226SAneesh Kumar K.V 
1417df13102fSChristophe Leroy 	for (i = 0; i < NR_INSN_TO_PRINT; i++) {
141806d67d54SPaul Mackerras 		int instr;
141906d67d54SPaul Mackerras 
1420d9ab6da6SChristophe Leroy 		if (get_kernel_nofault(instr, (const void *)pc)) {
14212ffd04deSAndrew Donnellan 			pr_cont("XXXXXXXX ");
142206d67d54SPaul Mackerras 		} else {
1423a6e2c226SAneesh Kumar K.V 			if (nip == pc)
14242ffd04deSAndrew Donnellan 				pr_cont("<%08x> ", instr);
142506d67d54SPaul Mackerras 			else
14262ffd04deSAndrew Donnellan 				pr_cont("%08x ", instr);
142706d67d54SPaul Mackerras 		}
142806d67d54SPaul Mackerras 
142906d67d54SPaul Mackerras 		pc += sizeof(int);
143006d67d54SPaul Mackerras 	}
143106d67d54SPaul Mackerras 
14322ffd04deSAndrew Donnellan 	pr_cont("\n");
143306d67d54SPaul Mackerras }
143406d67d54SPaul Mackerras 
show_user_instructions(struct pt_regs * regs)143588b0fe17SMurilo Opsfelder Araujo void show_user_instructions(struct pt_regs *regs)
143688b0fe17SMurilo Opsfelder Araujo {
143788b0fe17SMurilo Opsfelder Araujo 	unsigned long pc;
1438df13102fSChristophe Leroy 	int n = NR_INSN_TO_PRINT;
1439fb2d9505SChristophe Leroy 	struct seq_buf s;
1440fb2d9505SChristophe Leroy 	char buf[96]; /* enough for 8 times 9 + 2 chars */
144188b0fe17SMurilo Opsfelder Araujo 
1442df13102fSChristophe Leroy 	pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
144388b0fe17SMurilo Opsfelder Araujo 
1444fb2d9505SChristophe Leroy 	seq_buf_init(&s, buf, sizeof(buf));
144588b0fe17SMurilo Opsfelder Araujo 
1446fb2d9505SChristophe Leroy 	while (n) {
1447fb2d9505SChristophe Leroy 		int i;
1448fb2d9505SChristophe Leroy 
1449fb2d9505SChristophe Leroy 		seq_buf_clear(&s);
1450fb2d9505SChristophe Leroy 
1451fb2d9505SChristophe Leroy 		for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) {
145288b0fe17SMurilo Opsfelder Araujo 			int instr;
145388b0fe17SMurilo Opsfelder Araujo 
1454c0ee37e8SChristoph Hellwig 			if (copy_from_user_nofault(&instr, (void __user *)pc,
1455c0ee37e8SChristoph Hellwig 					sizeof(instr))) {
1456fb2d9505SChristophe Leroy 				seq_buf_printf(&s, "XXXXXXXX ");
1457fb2d9505SChristophe Leroy 				continue;
1458fb2d9505SChristophe Leroy 			}
1459fb2d9505SChristophe Leroy 			seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr);
146088b0fe17SMurilo Opsfelder Araujo 		}
146188b0fe17SMurilo Opsfelder Araujo 
1462fb2d9505SChristophe Leroy 		if (!seq_buf_has_overflowed(&s))
1463fb2d9505SChristophe Leroy 			pr_info("%s[%d]: code: %s\n", current->comm,
1464fb2d9505SChristophe Leroy 				current->pid, s.buffer);
146588b0fe17SMurilo Opsfelder Araujo 	}
146688b0fe17SMurilo Opsfelder Araujo }
146788b0fe17SMurilo Opsfelder Araujo 
1468801c0b2cSMichael Neuling struct regbit {
146906d67d54SPaul Mackerras 	unsigned long bit;
147006d67d54SPaul Mackerras 	const char *name;
1471801c0b2cSMichael Neuling };
1472801c0b2cSMichael Neuling 
1473801c0b2cSMichael Neuling static struct regbit msr_bits[] = {
14743bfd0c9cSAnton Blanchard #if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
14753bfd0c9cSAnton Blanchard 	{MSR_SF,	"SF"},
14763bfd0c9cSAnton Blanchard 	{MSR_HV,	"HV"},
14773bfd0c9cSAnton Blanchard #endif
14783bfd0c9cSAnton Blanchard 	{MSR_VEC,	"VEC"},
14793bfd0c9cSAnton Blanchard 	{MSR_VSX,	"VSX"},
14803bfd0c9cSAnton Blanchard #ifdef CONFIG_BOOKE
14813bfd0c9cSAnton Blanchard 	{MSR_CE,	"CE"},
14823bfd0c9cSAnton Blanchard #endif
148306d67d54SPaul Mackerras 	{MSR_EE,	"EE"},
148406d67d54SPaul Mackerras 	{MSR_PR,	"PR"},
148506d67d54SPaul Mackerras 	{MSR_FP,	"FP"},
148606d67d54SPaul Mackerras 	{MSR_ME,	"ME"},
14873bfd0c9cSAnton Blanchard #ifdef CONFIG_BOOKE
14881b98326bSKumar Gala 	{MSR_DE,	"DE"},
14893bfd0c9cSAnton Blanchard #else
14903bfd0c9cSAnton Blanchard 	{MSR_SE,	"SE"},
14913bfd0c9cSAnton Blanchard 	{MSR_BE,	"BE"},
14923bfd0c9cSAnton Blanchard #endif
149306d67d54SPaul Mackerras 	{MSR_IR,	"IR"},
149406d67d54SPaul Mackerras 	{MSR_DR,	"DR"},
14953bfd0c9cSAnton Blanchard 	{MSR_PMM,	"PMM"},
14963bfd0c9cSAnton Blanchard #ifndef CONFIG_BOOKE
14973bfd0c9cSAnton Blanchard 	{MSR_RI,	"RI"},
14983bfd0c9cSAnton Blanchard 	{MSR_LE,	"LE"},
14993bfd0c9cSAnton Blanchard #endif
150006d67d54SPaul Mackerras 	{0,		NULL}
150106d67d54SPaul Mackerras };
150206d67d54SPaul Mackerras 
print_bits(unsigned long val,struct regbit * bits,const char * sep)1503801c0b2cSMichael Neuling static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
150406d67d54SPaul Mackerras {
1505801c0b2cSMichael Neuling 	const char *s = "";
150606d67d54SPaul Mackerras 
150706d67d54SPaul Mackerras 	for (; bits->bit; ++bits)
150806d67d54SPaul Mackerras 		if (val & bits->bit) {
1509db5ba5aeSMichael Ellerman 			pr_cont("%s%s", s, bits->name);
1510801c0b2cSMichael Neuling 			s = sep;
151106d67d54SPaul Mackerras 		}
1512801c0b2cSMichael Neuling }
1513801c0b2cSMichael Neuling 
1514801c0b2cSMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1515801c0b2cSMichael Neuling static struct regbit msr_tm_bits[] = {
1516801c0b2cSMichael Neuling 	{MSR_TS_T,	"T"},
1517801c0b2cSMichael Neuling 	{MSR_TS_S,	"S"},
1518801c0b2cSMichael Neuling 	{MSR_TM,	"E"},
1519801c0b2cSMichael Neuling 	{0,		NULL}
1520801c0b2cSMichael Neuling };
1521801c0b2cSMichael Neuling 
print_tm_bits(unsigned long val)1522801c0b2cSMichael Neuling static void print_tm_bits(unsigned long val)
1523801c0b2cSMichael Neuling {
1524801c0b2cSMichael Neuling /*
1525801c0b2cSMichael Neuling  * This only prints something if at least one of the TM bit is set.
1526801c0b2cSMichael Neuling  * Inside the TM[], the output means:
1527801c0b2cSMichael Neuling  *   E: Enabled		(bit 32)
1528801c0b2cSMichael Neuling  *   S: Suspended	(bit 33)
1529801c0b2cSMichael Neuling  *   T: Transactional	(bit 34)
1530801c0b2cSMichael Neuling  */
1531801c0b2cSMichael Neuling 	if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
1532db5ba5aeSMichael Ellerman 		pr_cont(",TM[");
1533801c0b2cSMichael Neuling 		print_bits(val, msr_tm_bits, "");
1534db5ba5aeSMichael Ellerman 		pr_cont("]");
1535801c0b2cSMichael Neuling 	}
1536801c0b2cSMichael Neuling }
1537801c0b2cSMichael Neuling #else
print_tm_bits(unsigned long val)1538801c0b2cSMichael Neuling static void print_tm_bits(unsigned long val) {}
1539801c0b2cSMichael Neuling #endif
1540801c0b2cSMichael Neuling 
print_msr_bits(unsigned long val)1541801c0b2cSMichael Neuling static void print_msr_bits(unsigned long val)
1542801c0b2cSMichael Neuling {
1543db5ba5aeSMichael Ellerman 	pr_cont("<");
1544801c0b2cSMichael Neuling 	print_bits(val, msr_bits, ",");
1545801c0b2cSMichael Neuling 	print_tm_bits(val);
1546db5ba5aeSMichael Ellerman 	pr_cont(">");
154706d67d54SPaul Mackerras }
154806d67d54SPaul Mackerras 
154906d67d54SPaul Mackerras #ifdef CONFIG_PPC64
1550f6f7dde3Santon@samba.org #define REG		"%016lx"
155106d67d54SPaul Mackerras #define REGS_PER_LINE	4
155206d67d54SPaul Mackerras #else
1553f6f7dde3Santon@samba.org #define REG		"%08lx"
155406d67d54SPaul Mackerras #define REGS_PER_LINE	8
155506d67d54SPaul Mackerras #endif
155606d67d54SPaul Mackerras 
__show_regs(struct pt_regs * regs)1557bf13718bSNicholas Piggin static void __show_regs(struct pt_regs *regs)
155814cf11afSPaul Mackerras {
155914cf11afSPaul Mackerras 	int i, trap;
156014cf11afSPaul Mackerras 
156106d67d54SPaul Mackerras 	printk("NIP:  "REG" LR: "REG" CTR: "REG"\n",
156206d67d54SPaul Mackerras 	       regs->nip, regs->link, regs->ctr);
1563182dc9c7SMichael Ellerman 	printk("REGS: %px TRAP: %04lx   %s  (%s)\n",
156496b644bdSSerge E. Hallyn 	       regs, regs->trap, print_tainted(), init_utsname()->release);
156506d67d54SPaul Mackerras 	printk("MSR:  "REG" ", regs->msr);
1566801c0b2cSMichael Neuling 	print_msr_bits(regs->msr);
1567f6fc73fbSMichael Ellerman 	pr_cont("  CR: %08lx  XER: %08lx\n", regs->ccr, regs->xer);
156814cf11afSPaul Mackerras 	trap = TRAP(regs);
1569912237eaSNicholas Piggin 	if (!trap_is_syscall(regs) && cpu_has_feature(CPU_FTR_CFAR))
15707dae865fSMichael Ellerman 		pr_cont("CFAR: "REG" ", regs->orig_gpr3);
15717153d4bfSXiongwei Song 	if (trap == INTERRUPT_MACHINE_CHECK ||
15727153d4bfSXiongwei Song 	    trap == INTERRUPT_DATA_STORAGE ||
15737153d4bfSXiongwei Song 	    trap == INTERRUPT_ALIGNMENT) {
15747bf5f056SMichael Ellerman 		if (IS_ENABLED(CONFIG_BOOKE))
15754872cbd0SXiongwei Song 			pr_cont("DEAR: "REG" ESR: "REG" ", regs->dear, regs->esr);
15762ec42996SChristophe Leroy 		else
15777dae865fSMichael Ellerman 			pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
15782ec42996SChristophe Leroy 	}
15792ec42996SChristophe Leroy 
15809db8bcfdSAnton Blanchard #ifdef CONFIG_PPC64
15813130a7bbSNicholas Piggin 	pr_cont("IRQMASK: %lx ", regs->softe);
15829db8bcfdSAnton Blanchard #endif
15839db8bcfdSAnton Blanchard #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
15846d888d1aSAnton Blanchard 	if (MSR_TM_ACTIVE(regs->msr))
15857dae865fSMichael Ellerman 		pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
158614170789SKumar Gala #endif
158714cf11afSPaul Mackerras 
158814cf11afSPaul Mackerras 	for (i = 0;  i < 32;  i++) {
158906d67d54SPaul Mackerras 		if ((i % REGS_PER_LINE) == 0)
15907dae865fSMichael Ellerman 			pr_cont("\nGPR%02d: ", i);
15917dae865fSMichael Ellerman 		pr_cont(REG " ", regs->gpr[i]);
159214cf11afSPaul Mackerras 	}
15937dae865fSMichael Ellerman 	pr_cont("\n");
159414cf11afSPaul Mackerras 	/*
159514cf11afSPaul Mackerras 	 * Lookup NIP late so we have the best change of getting the
159614cf11afSPaul Mackerras 	 * above info out without failing
159714cf11afSPaul Mackerras 	 */
15988f020c7cSChristophe Leroy 	if (IS_ENABLED(CONFIG_KALLSYMS)) {
1599058c78f4SBenjamin Herrenschmidt 		printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1600058c78f4SBenjamin Herrenschmidt 		printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
16018f020c7cSChristophe Leroy 	}
1602bf13718bSNicholas Piggin }
1603bf13718bSNicholas Piggin 
show_regs(struct pt_regs * regs)1604bf13718bSNicholas Piggin void show_regs(struct pt_regs *regs)
1605bf13718bSNicholas Piggin {
1606bf13718bSNicholas Piggin 	show_regs_print_info(KERN_DEFAULT);
1607bf13718bSNicholas Piggin 	__show_regs(regs);
16089cb8f069SDmitry Safonov 	show_stack(current, (unsigned long *) regs->gpr[1], KERN_DEFAULT);
160906d67d54SPaul Mackerras 	if (!user_mode(regs))
161006d67d54SPaul Mackerras 		show_instructions(regs);
161114cf11afSPaul Mackerras }
161214cf11afSPaul Mackerras 
flush_thread(void)161314cf11afSPaul Mackerras void flush_thread(void)
161414cf11afSPaul Mackerras {
1615e0780b72SK.Prasad #ifdef CONFIG_HAVE_HW_BREAKPOINT
16165aae8a53SK.Prasad 	flush_ptrace_hw_breakpoint(current);
1617e0780b72SK.Prasad #else /* CONFIG_HAVE_HW_BREAKPOINT */
16183bffb652SDave Kleikamp 	set_debug_reg_defaults(&current->thread);
1619e0780b72SK.Prasad #endif /* CONFIG_HAVE_HW_BREAKPOINT */
162014cf11afSPaul Mackerras }
162114cf11afSPaul Mackerras 
arch_setup_new_exec(void)1622425d3314SNicholas Piggin void arch_setup_new_exec(void)
1623425d3314SNicholas Piggin {
1624d7df77e8SAneesh Kumar K.V 
1625d7df77e8SAneesh Kumar K.V #ifdef CONFIG_PPC_BOOK3S_64
1626d7df77e8SAneesh Kumar K.V 	if (!radix_enabled())
1627425d3314SNicholas Piggin 		hash__setup_new_exec();
1628425d3314SNicholas Piggin #endif
1629d7df77e8SAneesh Kumar K.V 	/*
1630d7df77e8SAneesh Kumar K.V 	 * If we exec out of a kernel thread then thread.regs will not be
1631d7df77e8SAneesh Kumar K.V 	 * set.  Do it now.
1632d7df77e8SAneesh Kumar K.V 	 */
1633d7df77e8SAneesh Kumar K.V 	if (!current->thread.regs) {
1634d7df77e8SAneesh Kumar K.V 		struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1635d7df77e8SAneesh Kumar K.V 		current->thread.regs = regs - 1;
1636d7df77e8SAneesh Kumar K.V 	}
1637d5fa30e6SAneesh Kumar K.V 
1638d5fa30e6SAneesh Kumar K.V #ifdef CONFIG_PPC_MEM_KEYS
1639d5fa30e6SAneesh Kumar K.V 	current->thread.regs->amr  = default_amr;
1640d5fa30e6SAneesh Kumar K.V 	current->thread.regs->iamr  = default_iamr;
1641d5fa30e6SAneesh Kumar K.V #endif
1642bbd99922SBenjamin Gray 
1643bbd99922SBenjamin Gray #ifdef CONFIG_PPC_BOOK3S_64
1644bbd99922SBenjamin Gray 	if (cpu_has_feature(CPU_FTR_ARCH_31)) {
1645bbd99922SBenjamin Gray 		current->thread.dexcr = current->thread.dexcr_onexec;
1646bbd99922SBenjamin Gray 		mtspr(SPRN_DEXCR, current->thread.dexcr);
1647bbd99922SBenjamin Gray 	}
1648bbd99922SBenjamin Gray #endif /* CONFIG_PPC_BOOK3S_64 */
1649d7df77e8SAneesh Kumar K.V }
1650425d3314SNicholas Piggin 
1651ec233edeSSukadev Bhattiprolu #ifdef CONFIG_PPC64
1652be994293SBo Liu /*
165371cc64a8SAlastair D'Silva  * Assign a TIDR (thread ID) for task @t and set it in the thread
1654ec233edeSSukadev Bhattiprolu  * structure. For now, we only support setting TIDR for 'current' task.
165571cc64a8SAlastair D'Silva  *
165671cc64a8SAlastair D'Silva  * Since the TID value is a truncated form of it PID, it is possible
165771cc64a8SAlastair D'Silva  * (but unlikely) for 2 threads to have the same TID. In the unlikely event
165871cc64a8SAlastair D'Silva  * that 2 threads share the same TID and are waiting, one of the following
165971cc64a8SAlastair D'Silva  * cases will happen:
166071cc64a8SAlastair D'Silva  *
166171cc64a8SAlastair D'Silva  * 1. The correct thread is running, the wrong thread is not
16620ddbbb89SBjorn Helgaas  * In this situation, the correct thread is woken and proceeds to pass its
166371cc64a8SAlastair D'Silva  * condition check.
166471cc64a8SAlastair D'Silva  *
166571cc64a8SAlastair D'Silva  * 2. Neither threads are running
166671cc64a8SAlastair D'Silva  * In this situation, neither thread will be woken. When scheduled, the waiting
166771cc64a8SAlastair D'Silva  * threads will execute either a wait, which will return immediately, followed
166871cc64a8SAlastair D'Silva  * by a condition check, which will pass for the correct thread and fail
166971cc64a8SAlastair D'Silva  * for the wrong thread, or they will execute the condition check immediately.
167071cc64a8SAlastair D'Silva  *
167171cc64a8SAlastair D'Silva  * 3. The wrong thread is running, the correct thread is not
16720ddbbb89SBjorn Helgaas  * The wrong thread will be woken, but will fail its condition check and
167371cc64a8SAlastair D'Silva  * re-execute wait. The correct thread, when scheduled, will execute either
16740ddbbb89SBjorn Helgaas  * its condition check (which will pass), or wait, which returns immediately
16750ddbbb89SBjorn Helgaas  * when called the first time after the thread is scheduled, followed by its
167671cc64a8SAlastair D'Silva  * condition check (which will pass).
167771cc64a8SAlastair D'Silva  *
167871cc64a8SAlastair D'Silva  * 4. Both threads are running
16790ddbbb89SBjorn Helgaas  * Both threads will be woken. The wrong thread will fail its condition check
16800ddbbb89SBjorn Helgaas  * and execute another wait, while the correct thread will pass its condition
168171cc64a8SAlastair D'Silva  * check.
168271cc64a8SAlastair D'Silva  *
168371cc64a8SAlastair D'Silva  * @t: the task to set the thread ID for
1684ec233edeSSukadev Bhattiprolu  */
set_thread_tidr(struct task_struct * t)1685ec233edeSSukadev Bhattiprolu int set_thread_tidr(struct task_struct *t)
1686ec233edeSSukadev Bhattiprolu {
16873449f191SAlastair D'Silva 	if (!cpu_has_feature(CPU_FTR_P9_TIDR))
1688ec233edeSSukadev Bhattiprolu 		return -EINVAL;
1689ec233edeSSukadev Bhattiprolu 
1690ec233edeSSukadev Bhattiprolu 	if (t != current)
1691ec233edeSSukadev Bhattiprolu 		return -EINVAL;
1692ec233edeSSukadev Bhattiprolu 
16937e4d4233SVaibhav Jain 	if (t->thread.tidr)
16947e4d4233SVaibhav Jain 		return 0;
16957e4d4233SVaibhav Jain 
169671cc64a8SAlastair D'Silva 	t->thread.tidr = (u16)task_pid_nr(t);
1697ec233edeSSukadev Bhattiprolu 	mtspr(SPRN_TIDR, t->thread.tidr);
1698ec233edeSSukadev Bhattiprolu 
1699ec233edeSSukadev Bhattiprolu 	return 0;
1700ec233edeSSukadev Bhattiprolu }
1701b1db5513SChristophe Lombard EXPORT_SYMBOL_GPL(set_thread_tidr);
1702ec233edeSSukadev Bhattiprolu 
1703ec233edeSSukadev Bhattiprolu #endif /* CONFIG_PPC64 */
1704ec233edeSSukadev Bhattiprolu 
170514cf11afSPaul Mackerras /*
170655ccf3feSSuresh Siddha  * this gets called so that we can store coprocessor state into memory and
170755ccf3feSSuresh Siddha  * copy the current task into the new thread.
170814cf11afSPaul Mackerras  */
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)170955ccf3feSSuresh Siddha int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
171014cf11afSPaul Mackerras {
1711579e633eSAnton Blanchard 	flush_all_to_thread(src);
1712621b5060SMichael Neuling 	/*
1713621b5060SMichael Neuling 	 * Flush TM state out so we can copy it.  __switch_to_tm() does this
1714621b5060SMichael Neuling 	 * flush but it removes the checkpointed state from the current CPU and
1715621b5060SMichael Neuling 	 * transitions the CPU out of TM mode.  Hence we need to call
1716621b5060SMichael Neuling 	 * tm_recheckpoint_new_task() (on the same task) to restore the
1717621b5060SMichael Neuling 	 * checkpointed state back and the TM mode.
17185d176f75SCyril Bur 	 *
17195d176f75SCyril Bur 	 * Can't pass dst because it isn't ready. Doesn't matter, passing
17205d176f75SCyril Bur 	 * dst is only important for __switch_to()
1721621b5060SMichael Neuling 	 */
1722dc310669SCyril Bur 	__switch_to_tm(src, src);
1723330a1eb7SMichael Ellerman 
172455ccf3feSSuresh Siddha 	*dst = *src;
1725330a1eb7SMichael Ellerman 
1726330a1eb7SMichael Ellerman 	clear_task_ebb(dst);
1727330a1eb7SMichael Ellerman 
172855ccf3feSSuresh Siddha 	return 0;
172914cf11afSPaul Mackerras }
173014cf11afSPaul Mackerras 
setup_ksp_vsid(struct task_struct * p,unsigned long sp)1731cec15488SMichael Ellerman static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1732cec15488SMichael Ellerman {
1733387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
1734cec15488SMichael Ellerman 	unsigned long sp_vsid;
1735cec15488SMichael Ellerman 	unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1736cec15488SMichael Ellerman 
1737caca285eSAneesh Kumar K.V 	if (radix_enabled())
1738caca285eSAneesh Kumar K.V 		return;
1739caca285eSAneesh Kumar K.V 
1740cec15488SMichael Ellerman 	if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1741cec15488SMichael Ellerman 		sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1742cec15488SMichael Ellerman 			<< SLB_VSID_SHIFT_1T;
1743cec15488SMichael Ellerman 	else
1744cec15488SMichael Ellerman 		sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1745cec15488SMichael Ellerman 			<< SLB_VSID_SHIFT;
1746cec15488SMichael Ellerman 	sp_vsid |= SLB_VSID_KERNEL | llp;
1747cec15488SMichael Ellerman 	p->thread.ksp_vsid = sp_vsid;
1748cec15488SMichael Ellerman #endif
1749cec15488SMichael Ellerman }
1750cec15488SMichael Ellerman 
175114cf11afSPaul Mackerras /*
175214cf11afSPaul Mackerras  * Copy a thread..
175314cf11afSPaul Mackerras  */
1754efcac658SAlexey Kardashevskiy 
17556eca8933SAlex Dowad /*
17566eca8933SAlex Dowad  * Copy architecture-specific thread state
17576eca8933SAlex Dowad  */
copy_thread(struct task_struct * p,const struct kernel_clone_args * args)1758c5febea0SEric W. Biederman int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
175914cf11afSPaul Mackerras {
1760eed7c420SNicholas Piggin 	struct pt_regs *kregs; /* Switch frame regs */
176114cf11afSPaul Mackerras 	extern void ret_from_fork(void);
17627fa95f9aSNicholas Piggin 	extern void ret_from_fork_scv(void);
1763b504b6aaSNicholas Piggin 	extern void ret_from_kernel_user_thread(void);
1764b504b6aaSNicholas Piggin 	extern void start_kernel_thread(void);
176558254e10SAl Viro 	void (*f)(void);
17660cec6fd1SAl Viro 	unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
17676b424efaSRavi Bangoria #ifdef CONFIG_HAVE_HW_BREAKPOINT
17686b424efaSRavi Bangoria 	int i;
17696b424efaSRavi Bangoria #endif
17705d31a96eSMichael Ellerman 
1771ed1cd6deSChristophe Leroy 	klp_init_thread_info(p);
177214cf11afSPaul Mackerras 
1773eed7c420SNicholas Piggin 	if (unlikely(p->flags & PF_KTHREAD)) {
1774eed7c420SNicholas Piggin 		/* kernel thread */
1775eed7c420SNicholas Piggin 
1776eed7c420SNicholas Piggin 		/* Create initial minimum stack frame. */
1777eed7c420SNicholas Piggin 		sp -= STACK_FRAME_MIN_SIZE;
1778eed7c420SNicholas Piggin 		((unsigned long *)sp)[0] = 0;
1779eed7c420SNicholas Piggin 
1780b504b6aaSNicholas Piggin 		f = start_kernel_thread;
1781eed7c420SNicholas Piggin 		p->thread.regs = NULL;	/* no user register state */
1782eed7c420SNicholas Piggin 		clear_tsk_compat_task(p);
1783eed7c420SNicholas Piggin 	} else {
1784eed7c420SNicholas Piggin 		/* user thread */
1785eed7c420SNicholas Piggin 		struct pt_regs *childregs;
1786eed7c420SNicholas Piggin 
1787eed7c420SNicholas Piggin 		/* Create initial user return stack frame. */
17881223e5a2SNicholas Piggin 		sp -= STACK_USER_INT_FRAME_SIZE;
17896895dfc0SNicholas Piggin 		*(unsigned long *)(sp + STACK_INT_FRAME_MARKER) = STACK_FRAME_REGS_MARKER;
1790bc067736SNicholas Piggin 
17911223e5a2SNicholas Piggin 		childregs = (struct pt_regs *)(sp + STACK_INT_FRAME_REGS);
1792eed7c420SNicholas Piggin 
17935bd2e97cSEric W. Biederman 		if (unlikely(args->fn)) {
1794eed7c420SNicholas Piggin 			/*
1795eed7c420SNicholas Piggin 			 * A user space thread, but it first runs a kernel
1796eed7c420SNicholas Piggin 			 * thread, and then returns as though it had called
1797eed7c420SNicholas Piggin 			 * execve rather than fork, so user regs will be
1798eed7c420SNicholas Piggin 			 * filled in (e.g., by kernel_execve()).
1799eed7c420SNicholas Piggin 			 */
18006895dfc0SNicholas Piggin 			((unsigned long *)sp)[0] = 0;
180158254e10SAl Viro 			memset(childregs, 0, sizeof(struct pt_regs));
180258254e10SAl Viro #ifdef CONFIG_PPC64
1803c2e480baSMadhavan Srinivasan 			childregs->softe = IRQS_ENABLED;
180406d67d54SPaul Mackerras #endif
1805b504b6aaSNicholas Piggin 			f = ret_from_kernel_user_thread;
180614cf11afSPaul Mackerras 		} else {
1807afa86fc4SAl Viro 			struct pt_regs *regs = current_pt_regs();
1808eed7c420SNicholas Piggin 			unsigned long clone_flags = args->flags;
1809eed7c420SNicholas Piggin 			unsigned long usp = args->stack;
1810eed7c420SNicholas Piggin 
1811eed7c420SNicholas Piggin 			/* Copy registers */
181258254e10SAl Viro 			*childregs = *regs;
1813ea516b11SAl Viro 			if (usp)
181414cf11afSPaul Mackerras 				childregs->gpr[1] = usp;
18156895dfc0SNicholas Piggin 			((unsigned long *)sp)[0] = childregs->gpr[1];
1816eed7c420SNicholas Piggin #ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
1817eed7c420SNicholas Piggin 			WARN_ON_ONCE(childregs->softe != IRQS_ENABLED);
1818eed7c420SNicholas Piggin #endif
181906d67d54SPaul Mackerras 			if (clone_flags & CLONE_SETTLS) {
1820eed7c420SNicholas Piggin 				unsigned long tls = args->tls;
1821eed7c420SNicholas Piggin 
18229904b005SDenis Kirjanov 				if (!is_32bit_task())
1823facd04a9SNicholas Piggin 					childregs->gpr[13] = tls;
182406d67d54SPaul Mackerras 				else
1825facd04a9SNicholas Piggin 					childregs->gpr[2] = tls;
182614cf11afSPaul Mackerras 			}
182758254e10SAl Viro 
18287fa95f9aSNicholas Piggin 			if (trap_is_scv(regs))
18297fa95f9aSNicholas Piggin 				f = ret_from_fork_scv;
18307fa95f9aSNicholas Piggin 			else
183158254e10SAl Viro 				f = ret_from_fork;
183206d67d54SPaul Mackerras 		}
1833eed7c420SNicholas Piggin 
1834d272f667SCyril Bur 		childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
1835eed7c420SNicholas Piggin 		p->thread.regs = childregs;
1836eed7c420SNicholas Piggin 	}
183714cf11afSPaul Mackerras 
183814cf11afSPaul Mackerras 	/*
183914cf11afSPaul Mackerras 	 * The way this works is that at some point in the future
184014cf11afSPaul Mackerras 	 * some task will call _switch to switch to the new task.
184114cf11afSPaul Mackerras 	 * That will pop off the stack frame created below and start
184214cf11afSPaul Mackerras 	 * the new task running at ret_from_fork.  The new task will
184314cf11afSPaul Mackerras 	 * do some house keeping and then return from the fork or clone
184414cf11afSPaul Mackerras 	 * system call, using the stack frame created above.
184514cf11afSPaul Mackerras 	 */
1846edbd0387SNicholas Piggin 	((unsigned long *)sp)[STACK_FRAME_LR_SAVE] = (unsigned long)f;
18476f291a03SNicholas Piggin 	sp -= STACK_SWITCH_FRAME_SIZE;
1848edbd0387SNicholas Piggin 	((unsigned long *)sp)[0] = sp + STACK_SWITCH_FRAME_SIZE;
18496f291a03SNicholas Piggin 	kregs = (struct pt_regs *)(sp + STACK_SWITCH_FRAME_REGS);
1850c013e9f2SNicholas Piggin 	kregs->nip = ppc_function_entry(f);
1851af5ca9d5SNicholas Piggin 	if (unlikely(args->fn)) {
1852af5ca9d5SNicholas Piggin 		/*
1853af5ca9d5SNicholas Piggin 		 * Put kthread fn, arg parameters in non-volatile GPRs in the
1854af5ca9d5SNicholas Piggin 		 * switch frame so they are loaded by _switch before it returns
1855af5ca9d5SNicholas Piggin 		 * to ret_from_kernel_thread.
1856af5ca9d5SNicholas Piggin 		 */
1857af5ca9d5SNicholas Piggin 		kregs->gpr[14] = ppc_function_entry((void *)args->fn);
1858af5ca9d5SNicholas Piggin 		kregs->gpr[15] = (unsigned long)args->fn_arg;
1859af5ca9d5SNicholas Piggin 	}
186014cf11afSPaul Mackerras 	p->thread.ksp = sp;
18616f291a03SNicholas Piggin 
186228d170abSOleg Nesterov #ifdef CONFIG_HAVE_HW_BREAKPOINT
18636b424efaSRavi Bangoria 	for (i = 0; i < nr_wp_slots(); i++)
18646b424efaSRavi Bangoria 		p->thread.ptrace_bps[i] = NULL;
186528d170abSOleg Nesterov #endif
186628d170abSOleg Nesterov 
1867b6254cedSChristophe Leroy #ifdef CONFIG_PPC_FPU_REGS
186818461960SPaul Mackerras 	p->thread.fp_save_area = NULL;
1869b6254cedSChristophe Leroy #endif
187018461960SPaul Mackerras #ifdef CONFIG_ALTIVEC
187118461960SPaul Mackerras 	p->thread.vr_save_area = NULL;
187218461960SPaul Mackerras #endif
187316132529SChristophe Leroy #if defined(CONFIG_PPC_BOOK3S_32) && defined(CONFIG_PPC_KUAP)
187416132529SChristophe Leroy 	p->thread.kuap = KUAP_NONE;
187516132529SChristophe Leroy #endif
1876002b27a5SMichael Ellerman #if defined(CONFIG_BOOKE) && defined(CONFIG_PPC_KUAP)
187743afcf8fSChristophe Leroy 	p->thread.pid = MMU_NO_CONTEXT;
187843afcf8fSChristophe Leroy #endif
187918461960SPaul Mackerras 
1880cec15488SMichael Ellerman 	setup_ksp_vsid(p, sp);
188106d67d54SPaul Mackerras 
1882efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1883efcac658SAlexey Kardashevskiy 	if (cpu_has_feature(CPU_FTR_DSCR)) {
18841021cb26SAnton Blanchard 		p->thread.dscr_inherit = current->thread.dscr_inherit;
1885db1231dcSAnton Blanchard 		p->thread.dscr = mfspr(SPRN_DSCR);
1886efcac658SAlexey Kardashevskiy 	}
1887ec233edeSSukadev Bhattiprolu 
1888ec233edeSSukadev Bhattiprolu 	p->thread.tidr = 0;
1889efcac658SAlexey Kardashevskiy #endif
1890be98fcf7SBenjamin Gray #ifdef CONFIG_PPC_BOOK3S_64
1891be98fcf7SBenjamin Gray 	if (cpu_has_feature(CPU_FTR_DEXCR_NPHIE))
1892be98fcf7SBenjamin Gray 		p->thread.hashkeyr = current->thread.hashkeyr;
189375171f06SBenjamin Gray 
189475171f06SBenjamin Gray 	if (cpu_has_feature(CPU_FTR_ARCH_31))
189575171f06SBenjamin Gray 		p->thread.dexcr = mfspr(SPRN_DEXCR);
1896be98fcf7SBenjamin Gray #endif
189714cf11afSPaul Mackerras 	return 0;
189814cf11afSPaul Mackerras }
189914cf11afSPaul Mackerras 
19005434ae74SNicholas Piggin void preload_new_slb_context(unsigned long start, unsigned long sp);
19015434ae74SNicholas Piggin 
190214cf11afSPaul Mackerras /*
190314cf11afSPaul Mackerras  * Set up a thread for executing a new program
190414cf11afSPaul Mackerras  */
start_thread(struct pt_regs * regs,unsigned long start,unsigned long sp)190506d67d54SPaul Mackerras void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
190614cf11afSPaul Mackerras {
190790eac727SMichael Ellerman #ifdef CONFIG_PPC64
190890eac727SMichael Ellerman 	unsigned long load_addr = regs->gpr[2];	/* saved by ELF_PLAT_INIT */
19095434ae74SNicholas Piggin 
1910bfac2799SChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled())
19115434ae74SNicholas Piggin 		preload_new_slb_context(start, sp);
19125434ae74SNicholas Piggin #endif
191390eac727SMichael Ellerman 
19148e96a87cSCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
19158e96a87cSCyril Bur 	/*
19168e96a87cSCyril Bur 	 * Clear any transactional state, we're exec()ing. The cause is
19178e96a87cSCyril Bur 	 * not important as there will never be a recheckpoint so it's not
19188e96a87cSCyril Bur 	 * user visible.
19198e96a87cSCyril Bur 	 */
19208e96a87cSCyril Bur 	if (MSR_TM_SUSPENDED(mfmsr()))
19218e96a87cSCyril Bur 		tm_reclaim_current(0);
19228e96a87cSCyril Bur #endif
19238e96a87cSCyril Bur 
1924ec6d0ddeSNaveen N. Rao 	memset(&regs->gpr[1], 0, sizeof(regs->gpr) - sizeof(regs->gpr[0]));
192514cf11afSPaul Mackerras 	regs->ctr = 0;
192614cf11afSPaul Mackerras 	regs->link = 0;
192714cf11afSPaul Mackerras 	regs->xer = 0;
192814cf11afSPaul Mackerras 	regs->ccr = 0;
192914cf11afSPaul Mackerras 	regs->gpr[1] = sp;
193006d67d54SPaul Mackerras 
193106d67d54SPaul Mackerras #ifdef CONFIG_PPC32
193206d67d54SPaul Mackerras 	regs->mq = 0;
193306d67d54SPaul Mackerras 	regs->nip = start;
193414cf11afSPaul Mackerras 	regs->msr = MSR_USER;
193506d67d54SPaul Mackerras #else
19369904b005SDenis Kirjanov 	if (!is_32bit_task()) {
193794af3abfSRusty Russell 		unsigned long entry;
193806d67d54SPaul Mackerras 
193994af3abfSRusty Russell 		if (is_elf2_task()) {
194094af3abfSRusty Russell 			/* Look ma, no function descriptors! */
194194af3abfSRusty Russell 			entry = start;
194294af3abfSRusty Russell 
194394af3abfSRusty Russell 			/*
194494af3abfSRusty Russell 			 * Ulrich says:
194594af3abfSRusty Russell 			 *   The latest iteration of the ABI requires that when
194694af3abfSRusty Russell 			 *   calling a function (at its global entry point),
194794af3abfSRusty Russell 			 *   the caller must ensure r12 holds the entry point
194894af3abfSRusty Russell 			 *   address (so that the function can quickly
194994af3abfSRusty Russell 			 *   establish addressability).
195094af3abfSRusty Russell 			 */
195194af3abfSRusty Russell 			regs->gpr[12] = start;
195294af3abfSRusty Russell 			/* Make sure that's restored on entry to userspace. */
195394af3abfSRusty Russell 			set_thread_flag(TIF_RESTOREALL);
195494af3abfSRusty Russell 		} else {
195594af3abfSRusty Russell 			unsigned long toc;
195694af3abfSRusty Russell 
195794af3abfSRusty Russell 			/* start is a relocated pointer to the function
195894af3abfSRusty Russell 			 * descriptor for the elf _start routine.  The first
195994af3abfSRusty Russell 			 * entry in the function descriptor is the entry
196094af3abfSRusty Russell 			 * address of _start and the second entry is the TOC
196194af3abfSRusty Russell 			 * value we need to use.
196206d67d54SPaul Mackerras 			 */
196306d67d54SPaul Mackerras 			__get_user(entry, (unsigned long __user *)start);
196406d67d54SPaul Mackerras 			__get_user(toc, (unsigned long __user *)start+1);
196506d67d54SPaul Mackerras 
196606d67d54SPaul Mackerras 			/* Check whether the e_entry function descriptor entries
196706d67d54SPaul Mackerras 			 * need to be relocated before we can use them.
196806d67d54SPaul Mackerras 			 */
196906d67d54SPaul Mackerras 			if (load_addr != 0) {
197006d67d54SPaul Mackerras 				entry += load_addr;
197106d67d54SPaul Mackerras 				toc   += load_addr;
197206d67d54SPaul Mackerras 			}
197306d67d54SPaul Mackerras 			regs->gpr[2] = toc;
197494af3abfSRusty Russell 		}
197559dc5bfcSNicholas Piggin 		regs_set_return_ip(regs, entry);
197659dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, MSR_USER64);
1977d4bf9a78SStephen Rothwell 	} else {
1978d4bf9a78SStephen Rothwell 		regs->gpr[2] = 0;
197959dc5bfcSNicholas Piggin 		regs_set_return_ip(regs, start);
198059dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, MSR_USER32);
198106d67d54SPaul Mackerras 	}
198259dc5bfcSNicholas Piggin 
198306d67d54SPaul Mackerras #endif
1984ce48b210SMichael Neuling #ifdef CONFIG_VSX
1985ce48b210SMichael Neuling 	current->thread.used_vsr = 0;
1986ce48b210SMichael Neuling #endif
19875434ae74SNicholas Piggin 	current->thread.load_slb = 0;
19881195892cSBreno Leitao 	current->thread.load_fp = 0;
1989b6254cedSChristophe Leroy #ifdef CONFIG_PPC_FPU_REGS
1990de79f7b9SPaul Mackerras 	memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
199118461960SPaul Mackerras 	current->thread.fp_save_area = NULL;
1992b6254cedSChristophe Leroy #endif
199314cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1994de79f7b9SPaul Mackerras 	memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1995de79f7b9SPaul Mackerras 	current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
199618461960SPaul Mackerras 	current->thread.vr_save_area = NULL;
199714cf11afSPaul Mackerras 	current->thread.vrsave = 0;
199814cf11afSPaul Mackerras 	current->thread.used_vr = 0;
19991195892cSBreno Leitao 	current->thread.load_vec = 0;
200014cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
200114cf11afSPaul Mackerras #ifdef CONFIG_SPE
200214cf11afSPaul Mackerras 	memset(current->thread.evr, 0, sizeof(current->thread.evr));
200314cf11afSPaul Mackerras 	current->thread.acc = 0;
200414cf11afSPaul Mackerras 	current->thread.spefscr = 0;
200514cf11afSPaul Mackerras 	current->thread.used_spe = 0;
200614cf11afSPaul Mackerras #endif /* CONFIG_SPE */
2007bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2008bc2a9408SMichael Neuling 	current->thread.tm_tfhar = 0;
2009bc2a9408SMichael Neuling 	current->thread.tm_texasr = 0;
2010bc2a9408SMichael Neuling 	current->thread.tm_tfiar = 0;
20117f22ced4SBreno Leitao 	current->thread.load_tm = 0;
2012bc2a9408SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
2013be98fcf7SBenjamin Gray #ifdef CONFIG_PPC_BOOK3S_64
2014be98fcf7SBenjamin Gray 	if (cpu_has_feature(CPU_FTR_DEXCR_NPHIE)) {
2015be98fcf7SBenjamin Gray 		current->thread.hashkeyr = get_random_long();
2016be98fcf7SBenjamin Gray 		mtspr(SPRN_HASHKEYR, current->thread.hashkeyr);
2017be98fcf7SBenjamin Gray 	}
2018be98fcf7SBenjamin Gray #endif /* CONFIG_PPC_BOOK3S_64 */
201914cf11afSPaul Mackerras }
2020e1802b06SAnton Blanchard EXPORT_SYMBOL(start_thread);
202114cf11afSPaul Mackerras 
202214cf11afSPaul Mackerras #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
202314cf11afSPaul Mackerras 		| PR_FP_EXC_RES | PR_FP_EXC_INV)
202414cf11afSPaul Mackerras 
set_fpexc_mode(struct task_struct * tsk,unsigned int val)202514cf11afSPaul Mackerras int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
202614cf11afSPaul Mackerras {
202714cf11afSPaul Mackerras 	struct pt_regs *regs = tsk->thread.regs;
202814cf11afSPaul Mackerras 
202914cf11afSPaul Mackerras 	/* This is a bit hairy.  If we are an SPE enabled  processor
203014cf11afSPaul Mackerras 	 * (have embedded fp) we store the IEEE exception enable flags in
203114cf11afSPaul Mackerras 	 * fpexc_mode.  fpexc_mode is also used for setting FP exception
203214cf11afSPaul Mackerras 	 * mode (asyn, precise, disabled) for 'Classic' FP. */
203314cf11afSPaul Mackerras 	if (val & PR_FP_EXC_SW_ENABLE) {
20345e14d21eSKumar Gala 		if (cpu_has_feature(CPU_FTR_SPE)) {
2035640e9225SJoseph Myers 			/*
2036640e9225SJoseph Myers 			 * When the sticky exception bits are set
2037640e9225SJoseph Myers 			 * directly by userspace, it must call prctl
2038640e9225SJoseph Myers 			 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
2039640e9225SJoseph Myers 			 * in the existing prctl settings) or
2040640e9225SJoseph Myers 			 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
2041640e9225SJoseph Myers 			 * the bits being set).  <fenv.h> functions
2042640e9225SJoseph Myers 			 * saving and restoring the whole
2043640e9225SJoseph Myers 			 * floating-point environment need to do so
2044640e9225SJoseph Myers 			 * anyway to restore the prctl settings from
2045640e9225SJoseph Myers 			 * the saved environment.
2046640e9225SJoseph Myers 			 */
2047532ed190SChristophe Leroy #ifdef CONFIG_SPE
2048640e9225SJoseph Myers 			tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
204914cf11afSPaul Mackerras 			tsk->thread.fpexc_mode = val &
205014cf11afSPaul Mackerras 				(PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
2051532ed190SChristophe Leroy #endif
205206d67d54SPaul Mackerras 			return 0;
20535e14d21eSKumar Gala 		} else {
20545e14d21eSKumar Gala 			return -EINVAL;
20555e14d21eSKumar Gala 		}
205606d67d54SPaul Mackerras 	}
205706d67d54SPaul Mackerras 
205814cf11afSPaul Mackerras 	/* on a CONFIG_SPE this does not hurt us.  The bits that
205914cf11afSPaul Mackerras 	 * __pack_fe01 use do not overlap with bits used for
206014cf11afSPaul Mackerras 	 * PR_FP_EXC_SW_ENABLE.  Additionally, the MSR[FE0,FE1] bits
206114cf11afSPaul Mackerras 	 * on CONFIG_SPE implementations are reserved so writing to
206214cf11afSPaul Mackerras 	 * them does not change anything */
206314cf11afSPaul Mackerras 	if (val > PR_FP_EXC_PRECISE)
206414cf11afSPaul Mackerras 		return -EINVAL;
206514cf11afSPaul Mackerras 	tsk->thread.fpexc_mode = __pack_fe01(val);
206659dc5bfcSNicholas Piggin 	if (regs != NULL && (regs->msr & MSR_FP) != 0) {
206759dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, (regs->msr & ~(MSR_FE0|MSR_FE1))
206859dc5bfcSNicholas Piggin 						| tsk->thread.fpexc_mode);
206959dc5bfcSNicholas Piggin 	}
207014cf11afSPaul Mackerras 	return 0;
207114cf11afSPaul Mackerras }
207214cf11afSPaul Mackerras 
get_fpexc_mode(struct task_struct * tsk,unsigned long adr)207314cf11afSPaul Mackerras int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
207414cf11afSPaul Mackerras {
2075d208e13cSMichael Ellerman 	unsigned int val = 0;
207614cf11afSPaul Mackerras 
2077532ed190SChristophe Leroy 	if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE) {
2078640e9225SJoseph Myers 		if (cpu_has_feature(CPU_FTR_SPE)) {
2079640e9225SJoseph Myers 			/*
2080640e9225SJoseph Myers 			 * When the sticky exception bits are set
2081640e9225SJoseph Myers 			 * directly by userspace, it must call prctl
2082640e9225SJoseph Myers 			 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
2083640e9225SJoseph Myers 			 * in the existing prctl settings) or
2084640e9225SJoseph Myers 			 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
2085640e9225SJoseph Myers 			 * the bits being set).  <fenv.h> functions
2086640e9225SJoseph Myers 			 * saving and restoring the whole
2087640e9225SJoseph Myers 			 * floating-point environment need to do so
2088640e9225SJoseph Myers 			 * anyway to restore the prctl settings from
2089640e9225SJoseph Myers 			 * the saved environment.
2090640e9225SJoseph Myers 			 */
2091532ed190SChristophe Leroy #ifdef CONFIG_SPE
2092640e9225SJoseph Myers 			tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
209314cf11afSPaul Mackerras 			val = tsk->thread.fpexc_mode;
2094532ed190SChristophe Leroy #endif
2095640e9225SJoseph Myers 		} else
20965e14d21eSKumar Gala 			return -EINVAL;
2097532ed190SChristophe Leroy 	} else {
209814cf11afSPaul Mackerras 		val = __unpack_fe01(tsk->thread.fpexc_mode);
2099532ed190SChristophe Leroy 	}
210014cf11afSPaul Mackerras 	return put_user(val, (unsigned int __user *) adr);
210114cf11afSPaul Mackerras }
210214cf11afSPaul Mackerras 
set_endian(struct task_struct * tsk,unsigned int val)2103fab5db97SPaul Mackerras int set_endian(struct task_struct *tsk, unsigned int val)
2104fab5db97SPaul Mackerras {
2105fab5db97SPaul Mackerras 	struct pt_regs *regs = tsk->thread.regs;
2106fab5db97SPaul Mackerras 
2107fab5db97SPaul Mackerras 	if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
2108fab5db97SPaul Mackerras 	    (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
2109fab5db97SPaul Mackerras 		return -EINVAL;
2110fab5db97SPaul Mackerras 
2111fab5db97SPaul Mackerras 	if (regs == NULL)
2112fab5db97SPaul Mackerras 		return -EINVAL;
2113fab5db97SPaul Mackerras 
2114fab5db97SPaul Mackerras 	if (val == PR_ENDIAN_BIG)
211559dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, regs->msr & ~MSR_LE);
2116fab5db97SPaul Mackerras 	else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
211759dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, regs->msr | MSR_LE);
2118fab5db97SPaul Mackerras 	else
2119fab5db97SPaul Mackerras 		return -EINVAL;
2120fab5db97SPaul Mackerras 
2121fab5db97SPaul Mackerras 	return 0;
2122fab5db97SPaul Mackerras }
2123fab5db97SPaul Mackerras 
get_endian(struct task_struct * tsk,unsigned long adr)2124fab5db97SPaul Mackerras int get_endian(struct task_struct *tsk, unsigned long adr)
2125fab5db97SPaul Mackerras {
2126fab5db97SPaul Mackerras 	struct pt_regs *regs = tsk->thread.regs;
2127fab5db97SPaul Mackerras 	unsigned int val;
2128fab5db97SPaul Mackerras 
2129fab5db97SPaul Mackerras 	if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
2130fab5db97SPaul Mackerras 	    !cpu_has_feature(CPU_FTR_REAL_LE))
2131fab5db97SPaul Mackerras 		return -EINVAL;
2132fab5db97SPaul Mackerras 
2133fab5db97SPaul Mackerras 	if (regs == NULL)
2134fab5db97SPaul Mackerras 		return -EINVAL;
2135fab5db97SPaul Mackerras 
2136fab5db97SPaul Mackerras 	if (regs->msr & MSR_LE) {
2137fab5db97SPaul Mackerras 		if (cpu_has_feature(CPU_FTR_REAL_LE))
2138fab5db97SPaul Mackerras 			val = PR_ENDIAN_LITTLE;
2139fab5db97SPaul Mackerras 		else
2140fab5db97SPaul Mackerras 			val = PR_ENDIAN_PPC_LITTLE;
2141fab5db97SPaul Mackerras 	} else
2142fab5db97SPaul Mackerras 		val = PR_ENDIAN_BIG;
2143fab5db97SPaul Mackerras 
2144fab5db97SPaul Mackerras 	return put_user(val, (unsigned int __user *)adr);
2145fab5db97SPaul Mackerras }
2146fab5db97SPaul Mackerras 
set_unalign_ctl(struct task_struct * tsk,unsigned int val)2147e9370ae1SPaul Mackerras int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
2148e9370ae1SPaul Mackerras {
2149e9370ae1SPaul Mackerras 	tsk->thread.align_ctl = val;
2150e9370ae1SPaul Mackerras 	return 0;
2151e9370ae1SPaul Mackerras }
2152e9370ae1SPaul Mackerras 
get_unalign_ctl(struct task_struct * tsk,unsigned long adr)2153e9370ae1SPaul Mackerras int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
2154e9370ae1SPaul Mackerras {
2155e9370ae1SPaul Mackerras 	return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
2156e9370ae1SPaul Mackerras }
2157e9370ae1SPaul Mackerras 
valid_irq_stack(unsigned long sp,struct task_struct * p,unsigned long nbytes)2158bb72c481SPaul Mackerras static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
2159bb72c481SPaul Mackerras 				  unsigned long nbytes)
2160bb72c481SPaul Mackerras {
2161bb72c481SPaul Mackerras 	unsigned long stack_page;
2162bb72c481SPaul Mackerras 	unsigned long cpu = task_cpu(p);
2163bb72c481SPaul Mackerras 
21641ee4e350SNicholas Piggin 	if (!hardirq_ctx[cpu] || !softirq_ctx[cpu])
21651ee4e350SNicholas Piggin 		return 0;
21661ee4e350SNicholas Piggin 
2167bb72c481SPaul Mackerras 	stack_page = (unsigned long)hardirq_ctx[cpu];
2168a7916a1dSChristophe Leroy 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2169bb72c481SPaul Mackerras 		return 1;
2170bb72c481SPaul Mackerras 
2171bb72c481SPaul Mackerras 	stack_page = (unsigned long)softirq_ctx[cpu];
2172a7916a1dSChristophe Leroy 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2173bb72c481SPaul Mackerras 		return 1;
2174a7916a1dSChristophe Leroy 
2175bb72c481SPaul Mackerras 	return 0;
2176bb72c481SPaul Mackerras }
2177bb72c481SPaul Mackerras 
2178*dca5b1d6SChristophe Leroy #ifdef CONFIG_PPC64
valid_emergency_stack(unsigned long sp,struct task_struct * p,unsigned long nbytes)2179a2e36683SNicholas Piggin static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p,
2180a2e36683SNicholas Piggin 					unsigned long nbytes)
2181a2e36683SNicholas Piggin {
2182a2e36683SNicholas Piggin 	unsigned long stack_page;
2183a2e36683SNicholas Piggin 	unsigned long cpu = task_cpu(p);
2184a2e36683SNicholas Piggin 
21850ecf6a9eSMichael Ellerman 	if (!paca_ptrs)
21860ecf6a9eSMichael Ellerman 		return 0;
21870ecf6a9eSMichael Ellerman 
21881ee4e350SNicholas Piggin 	if (!paca_ptrs[cpu]->emergency_sp)
21891ee4e350SNicholas Piggin 		return 0;
21901ee4e350SNicholas Piggin 
21911ee4e350SNicholas Piggin # ifdef CONFIG_PPC_BOOK3S_64
21921ee4e350SNicholas Piggin 	if (!paca_ptrs[cpu]->nmi_emergency_sp || !paca_ptrs[cpu]->mc_emergency_sp)
21931ee4e350SNicholas Piggin 		return 0;
21941ee4e350SNicholas Piggin #endif
21951ee4e350SNicholas Piggin 
2196a2e36683SNicholas Piggin 	stack_page = (unsigned long)paca_ptrs[cpu]->emergency_sp - THREAD_SIZE;
2197a2e36683SNicholas Piggin 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2198a2e36683SNicholas Piggin 		return 1;
2199a2e36683SNicholas Piggin 
2200a2e36683SNicholas Piggin # ifdef CONFIG_PPC_BOOK3S_64
2201a2e36683SNicholas Piggin 	stack_page = (unsigned long)paca_ptrs[cpu]->nmi_emergency_sp - THREAD_SIZE;
2202a2e36683SNicholas Piggin 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2203a2e36683SNicholas Piggin 		return 1;
2204a2e36683SNicholas Piggin 
2205a2e36683SNicholas Piggin 	stack_page = (unsigned long)paca_ptrs[cpu]->mc_emergency_sp - THREAD_SIZE;
2206a2e36683SNicholas Piggin 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2207a2e36683SNicholas Piggin 		return 1;
2208a2e36683SNicholas Piggin # endif
2209a2e36683SNicholas Piggin 
2210a2e36683SNicholas Piggin 	return 0;
2211a2e36683SNicholas Piggin }
2212*dca5b1d6SChristophe Leroy #else
valid_emergency_stack(unsigned long sp,struct task_struct * p,unsigned long nbytes)2213*dca5b1d6SChristophe Leroy static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p,
2214*dca5b1d6SChristophe Leroy 					unsigned long nbytes)
2215*dca5b1d6SChristophe Leroy {
2216*dca5b1d6SChristophe Leroy 	unsigned long stack_page;
2217*dca5b1d6SChristophe Leroy 	unsigned long cpu = task_cpu(p);
2218*dca5b1d6SChristophe Leroy 
2219*dca5b1d6SChristophe Leroy 	if (!IS_ENABLED(CONFIG_VMAP_STACK))
2220*dca5b1d6SChristophe Leroy 		return 0;
2221*dca5b1d6SChristophe Leroy 
2222*dca5b1d6SChristophe Leroy 	stack_page = (unsigned long)emergency_ctx[cpu] - THREAD_SIZE;
2223*dca5b1d6SChristophe Leroy 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2224*dca5b1d6SChristophe Leroy 		return 1;
2225*dca5b1d6SChristophe Leroy 
2226*dca5b1d6SChristophe Leroy 	return 0;
2227*dca5b1d6SChristophe Leroy }
2228*dca5b1d6SChristophe Leroy #endif
2229a2e36683SNicholas Piggin 
22304cefb0f6SNicholas Piggin /*
22314cefb0f6SNicholas Piggin  * validate the stack frame of a particular minimum size, used for when we are
22324cefb0f6SNicholas Piggin  * looking at a certain object in the stack beyond the minimum.
22334cefb0f6SNicholas Piggin  */
validate_sp_size(unsigned long sp,struct task_struct * p,unsigned long nbytes)22344cefb0f6SNicholas Piggin int validate_sp_size(unsigned long sp, struct task_struct *p,
223514cf11afSPaul Mackerras 		     unsigned long nbytes)
223614cf11afSPaul Mackerras {
22370cec6fd1SAl Viro 	unsigned long stack_page = (unsigned long)task_stack_page(p);
223814cf11afSPaul Mackerras 
2239a7916a1dSChristophe Leroy 	if (sp < THREAD_SIZE)
2240a7916a1dSChristophe Leroy 		return 0;
2241a7916a1dSChristophe Leroy 
2242a7916a1dSChristophe Leroy 	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
224314cf11afSPaul Mackerras 		return 1;
224414cf11afSPaul Mackerras 
2245a2e36683SNicholas Piggin 	if (valid_irq_stack(sp, p, nbytes))
2246a2e36683SNicholas Piggin 		return 1;
2247a2e36683SNicholas Piggin 
2248a2e36683SNicholas Piggin 	return valid_emergency_stack(sp, p, nbytes);
224914cf11afSPaul Mackerras }
225014cf11afSPaul Mackerras 
validate_sp(unsigned long sp,struct task_struct * p)22514cefb0f6SNicholas Piggin int validate_sp(unsigned long sp, struct task_struct *p)
22524cefb0f6SNicholas Piggin {
225390f1b431SNicholas Piggin 	return validate_sp_size(sp, p, STACK_FRAME_MIN_SIZE);
22544cefb0f6SNicholas Piggin }
22552f25194dSAnton Blanchard 
___get_wchan(struct task_struct * p)225642a20f86SKees Cook static unsigned long ___get_wchan(struct task_struct *p)
225706d67d54SPaul Mackerras {
225806d67d54SPaul Mackerras 	unsigned long ip, sp;
225906d67d54SPaul Mackerras 	int count = 0;
226006d67d54SPaul Mackerras 
226106d67d54SPaul Mackerras 	sp = p->thread.ksp;
22624cefb0f6SNicholas Piggin 	if (!validate_sp(sp, p))
226306d67d54SPaul Mackerras 		return 0;
226406d67d54SPaul Mackerras 
226506d67d54SPaul Mackerras 	do {
2266a1b29ba2SHe Ying 		sp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
22674cefb0f6SNicholas Piggin 		if (!validate_sp(sp, p) || task_is_running(p))
226806d67d54SPaul Mackerras 			return 0;
226906d67d54SPaul Mackerras 		if (count > 0) {
2270a1b29ba2SHe Ying 			ip = READ_ONCE_NOCHECK(((unsigned long *)sp)[STACK_FRAME_LR_SAVE]);
227106d67d54SPaul Mackerras 			if (!in_sched_functions(ip))
227206d67d54SPaul Mackerras 				return ip;
227306d67d54SPaul Mackerras 		}
227406d67d54SPaul Mackerras 	} while (count++ < 16);
227506d67d54SPaul Mackerras 	return 0;
227606d67d54SPaul Mackerras }
227706d67d54SPaul Mackerras 
__get_wchan(struct task_struct * p)227842a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
2279018cce33SChristophe Leroy {
2280018cce33SChristophe Leroy 	unsigned long ret;
2281018cce33SChristophe Leroy 
2282018cce33SChristophe Leroy 	if (!try_get_task_stack(p))
2283018cce33SChristophe Leroy 		return 0;
2284018cce33SChristophe Leroy 
228542a20f86SKees Cook 	ret = ___get_wchan(p);
2286018cce33SChristophe Leroy 
2287018cce33SChristophe Leroy 	put_task_stack(p);
2288018cce33SChristophe Leroy 
2289018cce33SChristophe Leroy 	return ret;
2290018cce33SChristophe Leroy }
2291018cce33SChristophe Leroy 
empty_user_regs(struct pt_regs * regs,struct task_struct * tsk)2292d45c4b48SMichael Ellerman static bool empty_user_regs(struct pt_regs *regs, struct task_struct *tsk)
2293d45c4b48SMichael Ellerman {
2294d45c4b48SMichael Ellerman 	unsigned long stack_page;
2295d45c4b48SMichael Ellerman 
2296d45c4b48SMichael Ellerman 	// A non-empty pt_regs should never have a zero MSR or TRAP value.
2297d45c4b48SMichael Ellerman 	if (regs->msr || regs->trap)
2298d45c4b48SMichael Ellerman 		return false;
2299d45c4b48SMichael Ellerman 
2300d45c4b48SMichael Ellerman 	// Check it sits at the very base of the stack
2301d45c4b48SMichael Ellerman 	stack_page = (unsigned long)task_stack_page(tsk);
2302d45c4b48SMichael Ellerman 	if ((unsigned long)(regs + 1) != stack_page + THREAD_SIZE)
2303d45c4b48SMichael Ellerman 		return false;
2304d45c4b48SMichael Ellerman 
2305d45c4b48SMichael Ellerman 	return true;
2306d45c4b48SMichael Ellerman }
2307d45c4b48SMichael Ellerman 
2308c4d04be1SJohannes Berg static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
230914cf11afSPaul Mackerras 
show_stack(struct task_struct * tsk,unsigned long * stack,const char * loglvl)2310b112fb91SDaniel Axtens void __no_sanitize_address show_stack(struct task_struct *tsk,
2311b112fb91SDaniel Axtens 				      unsigned long *stack,
2312b9677a8cSDmitry Safonov 				      const char *loglvl)
231314cf11afSPaul Mackerras {
231406d67d54SPaul Mackerras 	unsigned long sp, ip, lr, newsp;
231514cf11afSPaul Mackerras 	int count = 0;
231606d67d54SPaul Mackerras 	int firstframe = 1;
23177c1bb6bbSNaveen N. Rao 	unsigned long ret_addr;
23187c1bb6bbSNaveen N. Rao 	int ftrace_idx = 0;
231914cf11afSPaul Mackerras 
232014cf11afSPaul Mackerras 	if (tsk == NULL)
232114cf11afSPaul Mackerras 		tsk = current;
2322018cce33SChristophe Leroy 
2323018cce33SChristophe Leroy 	if (!try_get_task_stack(tsk))
2324018cce33SChristophe Leroy 		return;
2325018cce33SChristophe Leroy 
2326018cce33SChristophe Leroy 	sp = (unsigned long) stack;
232714cf11afSPaul Mackerras 	if (sp == 0) {
232814cf11afSPaul Mackerras 		if (tsk == current)
23293d13e839SMichael Ellerman 			sp = current_stack_frame();
233014cf11afSPaul Mackerras 		else
233114cf11afSPaul Mackerras 			sp = tsk->thread.ksp;
233214cf11afSPaul Mackerras 	}
233314cf11afSPaul Mackerras 
233406d67d54SPaul Mackerras 	lr = 0;
2335b9677a8cSDmitry Safonov 	printk("%sCall Trace:\n", loglvl);
233614cf11afSPaul Mackerras 	do {
23374cefb0f6SNicholas Piggin 		if (!validate_sp(sp, tsk))
2338018cce33SChristophe Leroy 			break;
233906d67d54SPaul Mackerras 
234006d67d54SPaul Mackerras 		stack = (unsigned long *) sp;
234106d67d54SPaul Mackerras 		newsp = stack[0];
2342ec2b36b9SBenjamin Herrenschmidt 		ip = stack[STACK_FRAME_LR_SAVE];
234306d67d54SPaul Mackerras 		if (!firstframe || ip != lr) {
2344b9677a8cSDmitry Safonov 			printk("%s["REG"] ["REG"] %pS",
2345b9677a8cSDmitry Safonov 				loglvl, sp, ip, (void *)ip);
23467c1bb6bbSNaveen N. Rao 			ret_addr = ftrace_graph_ret_addr(current,
23477c1bb6bbSNaveen N. Rao 						&ftrace_idx, ip, stack);
23487c1bb6bbSNaveen N. Rao 			if (ret_addr != ip)
23497c1bb6bbSNaveen N. Rao 				pr_cont(" (%pS)", (void *)ret_addr);
235006d67d54SPaul Mackerras 			if (firstframe)
23519a1f490fSMichael Ellerman 				pr_cont(" (unreliable)");
23529a1f490fSMichael Ellerman 			pr_cont("\n");
235314cf11afSPaul Mackerras 		}
235406d67d54SPaul Mackerras 		firstframe = 0;
235506d67d54SPaul Mackerras 
235606d67d54SPaul Mackerras 		/*
235706d67d54SPaul Mackerras 		 * See if this is an exception frame.
2358c03be0a3SNicholas Piggin 		 * We look for the "regs" marker in the current frame.
23596f291a03SNicholas Piggin 		 *
23606f291a03SNicholas Piggin 		 * STACK_SWITCH_FRAME_SIZE being the smallest frame that
23616f291a03SNicholas Piggin 		 * could hold a pt_regs, if that does not fit then it can't
23626f291a03SNicholas Piggin 		 * have regs.
236306d67d54SPaul Mackerras 		 */
23644cefb0f6SNicholas Piggin 		if (validate_sp_size(sp, tsk, STACK_SWITCH_FRAME_SIZE)
2365e856e336SNicholas Piggin 		    && stack[STACK_INT_FRAME_MARKER_LONGS] == STACK_FRAME_REGS_MARKER) {
236606d67d54SPaul Mackerras 			struct pt_regs *regs = (struct pt_regs *)
2367c03be0a3SNicholas Piggin 				(sp + STACK_INT_FRAME_REGS);
2368bf13718bSNicholas Piggin 
236906d67d54SPaul Mackerras 			lr = regs->link;
2370bf13718bSNicholas Piggin 			printk("%s--- interrupt: %lx at %pS\n",
2371bf13718bSNicholas Piggin 			       loglvl, regs->trap, (void *)regs->nip);
2372d45c4b48SMichael Ellerman 
2373d45c4b48SMichael Ellerman 			// Detect the case of an empty pt_regs at the very base
2374d45c4b48SMichael Ellerman 			// of the stack and suppress showing it in full.
2375d45c4b48SMichael Ellerman 			if (!empty_user_regs(regs, tsk)) {
2376bf13718bSNicholas Piggin 				__show_regs(regs);
2377d45c4b48SMichael Ellerman 				printk("%s--- interrupt: %lx\n", loglvl, regs->trap);
2378d45c4b48SMichael Ellerman 			}
2379bf13718bSNicholas Piggin 
238006d67d54SPaul Mackerras 			firstframe = 1;
238114cf11afSPaul Mackerras 		}
238206d67d54SPaul Mackerras 
238306d67d54SPaul Mackerras 		sp = newsp;
238406d67d54SPaul Mackerras 	} while (count++ < kstack_depth_to_print);
2385018cce33SChristophe Leroy 
2386018cce33SChristophe Leroy 	put_task_stack(tsk);
238706d67d54SPaul Mackerras }
238806d67d54SPaul Mackerras 
2389cb2c9b27SAnton Blanchard #ifdef CONFIG_PPC64
2390fe1952fcSBenjamin Herrenschmidt /* Called with hard IRQs off */
__ppc64_runlatch_on(void)23910e37739bSMichael Ellerman void notrace __ppc64_runlatch_on(void)
2392cb2c9b27SAnton Blanchard {
2393fe1952fcSBenjamin Herrenschmidt 	struct thread_info *ti = current_thread_info();
2394d1d0d5ffSNicholas Piggin 
2395d1d0d5ffSNicholas Piggin 	if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2396d1d0d5ffSNicholas Piggin 		/*
2397d1d0d5ffSNicholas Piggin 		 * Least significant bit (RUN) is the only writable bit of
2398d1d0d5ffSNicholas Piggin 		 * the CTRL register, so we can avoid mfspr. 2.06 is not the
2399d1d0d5ffSNicholas Piggin 		 * earliest ISA where this is the case, but it's convenient.
2400d1d0d5ffSNicholas Piggin 		 */
2401d1d0d5ffSNicholas Piggin 		mtspr(SPRN_CTRLT, CTRL_RUNLATCH);
2402d1d0d5ffSNicholas Piggin 	} else {
2403cb2c9b27SAnton Blanchard 		unsigned long ctrl;
2404cb2c9b27SAnton Blanchard 
2405d1d0d5ffSNicholas Piggin 		/*
2406d1d0d5ffSNicholas Piggin 		 * Some architectures (e.g., Cell) have writable fields other
2407d1d0d5ffSNicholas Piggin 		 * than RUN, so do the read-modify-write.
2408d1d0d5ffSNicholas Piggin 		 */
2409cb2c9b27SAnton Blanchard 		ctrl = mfspr(SPRN_CTRLF);
2410cb2c9b27SAnton Blanchard 		ctrl |= CTRL_RUNLATCH;
2411cb2c9b27SAnton Blanchard 		mtspr(SPRN_CTRLT, ctrl);
2412d1d0d5ffSNicholas Piggin 	}
2413cb2c9b27SAnton Blanchard 
2414fae2e0fbSBenjamin Herrenschmidt 	ti->local_flags |= _TLF_RUNLATCH;
2415cb2c9b27SAnton Blanchard }
2416cb2c9b27SAnton Blanchard 
2417fe1952fcSBenjamin Herrenschmidt /* Called with hard IRQs off */
__ppc64_runlatch_off(void)24180e37739bSMichael Ellerman void notrace __ppc64_runlatch_off(void)
2419cb2c9b27SAnton Blanchard {
2420fe1952fcSBenjamin Herrenschmidt 	struct thread_info *ti = current_thread_info();
2421cb2c9b27SAnton Blanchard 
2422fae2e0fbSBenjamin Herrenschmidt 	ti->local_flags &= ~_TLF_RUNLATCH;
2423cb2c9b27SAnton Blanchard 
2424d1d0d5ffSNicholas Piggin 	if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2425d1d0d5ffSNicholas Piggin 		mtspr(SPRN_CTRLT, 0);
2426d1d0d5ffSNicholas Piggin 	} else {
2427d1d0d5ffSNicholas Piggin 		unsigned long ctrl;
2428d1d0d5ffSNicholas Piggin 
2429cb2c9b27SAnton Blanchard 		ctrl = mfspr(SPRN_CTRLF);
2430cb2c9b27SAnton Blanchard 		ctrl &= ~CTRL_RUNLATCH;
2431cb2c9b27SAnton Blanchard 		mtspr(SPRN_CTRLT, ctrl);
2432cb2c9b27SAnton Blanchard 	}
2433d1d0d5ffSNicholas Piggin }
2434fe1952fcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
2435f6a61680SBenjamin Herrenschmidt 
arch_align_stack(unsigned long sp)2436d839088cSAnton Blanchard unsigned long arch_align_stack(unsigned long sp)
2437d839088cSAnton Blanchard {
2438d839088cSAnton Blanchard 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
24398032bf12SJason A. Donenfeld 		sp -= get_random_u32_below(PAGE_SIZE);
2440d839088cSAnton Blanchard 	return sp & ~0xf;
2441d839088cSAnton Blanchard }
2442