blob: 3bc671df5b5175a05305d8096d04fd75b524ed07 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +10003 * Derived from "arch/i386/kernel/process.c"
4 * Copyright (C) 1995 Linus Torvalds
5 *
6 * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
7 * Paul Mackerras (paulus@cs.anu.edu.au)
8 *
9 * PowerPC version
10 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100011 */
12
Paul Mackerras14cf11a2005-09-26 16:04:21 +100013#include <linux/errno.h>
14#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010015#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010016#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010017#include <linux/sched/task_stack.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100018#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/smp.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100021#include <linux/stddef.h>
22#include <linux/unistd.h>
23#include <linux/ptrace.h>
24#include <linux/slab.h>
25#include <linux/user.h>
26#include <linux/elf.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100027#include <linux/prctl.h>
28#include <linux/init_task.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040029#include <linux/export.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100030#include <linux/kallsyms.h>
31#include <linux/mqueue.h>
32#include <linux/hardirq.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100033#include <linux/utsname.h>
Steven Rostedt6794c782009-02-09 21:10:27 -080034#include <linux/ftrace.h>
Martin Schwidefsky79741dd2008-12-31 15:11:38 +010035#include <linux/kernel_stat.h>
Anton Blanchardd8390882009-02-22 01:50:03 +000036#include <linux/personality.h>
37#include <linux/random.h>
K.Prasad5aae8a52010-06-15 11:35:19 +053038#include <linux/hw_breakpoint.h>
Anton Blanchard7b051f62014-10-13 20:27:15 +110039#include <linux/uaccess.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110040#include <linux/elf-randomize.h>
Ram Pai06bb53b2018-01-18 17:50:31 -080041#include <linux/pkeys.h>
Christophe Leroyfb2d9502018-10-06 16:51:14 +000042#include <linux/seq_buf.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100043
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044#include <asm/io.h>
45#include <asm/processor.h>
46#include <asm/mmu.h>
47#include <asm/prom.h>
Michael Ellerman76032de2005-11-07 13:12:03 +110048#include <asm/machdep.h>
Paul Mackerrasc6622f62006-02-24 10:06:59 +110049#include <asm/time.h>
David Howellsae3a1972012-03-28 18:30:02 +010050#include <asm/runlatch.h>
Arnd Bergmanna7f31842006-03-23 00:00:08 +010051#include <asm/syscalls.h>
David Howellsae3a1972012-03-28 18:30:02 +010052#include <asm/switch_to.h>
Michael Neulingfb096922013-02-13 16:21:37 +000053#include <asm/tm.h>
David Howellsae3a1972012-03-28 18:30:02 +010054#include <asm/debug.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100055#ifdef CONFIG_PPC64
56#include <asm/firmware.h>
Madhavan Srinivasanc2e480b2017-12-20 09:25:42 +053057#include <asm/hw_irq.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100058#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +110059#include <asm/code-patching.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110060#include <asm/exec.h>
Michael Ellerman5d31a962016-03-24 22:04:04 +110061#include <asm/livepatch.h>
Kevin Haob92a2262016-07-23 14:42:40 +053062#include <asm/cpu_has_feature.h>
Daniel Axtens0545d542016-09-06 15:32:43 +100063#include <asm/asm-prototypes.h>
Christophe Leroyc9386bf2018-10-09 16:46:25 +110064#include <asm/stacktrace.h>
Michael Neulingc1fe1902019-04-01 17:03:12 +110065#include <asm/hw_breakpoint.h>
Michael Ellerman5d31a962016-03-24 22:04:04 +110066
Luis Machadod6a61bf2008-07-24 02:10:41 +100067#include <linux/kprobes.h>
68#include <linux/kdebug.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100069
Michael Neuling8b3c34c2013-02-13 16:21:32 +000070/* Transactional Memory debug */
71#ifdef TM_DEBUG_SW
72#define TM_DEBUG(x...) printk(KERN_INFO x)
73#else
74#define TM_DEBUG(x...) do { } while(0)
75#endif
76
Paul Mackerras14cf11a2005-09-26 16:04:21 +100077extern unsigned long _get_SP(void);
78
Paul Mackerrasd31626f2014-01-13 15:56:29 +110079#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Michael Ellerman54820532017-10-12 21:17:18 +110080/*
81 * Are we running in "Suspend disabled" mode? If so we have to block any
82 * sigreturn that would get us into suspended state, and we also warn in some
83 * other paths that we should never reach with suspend disabled.
84 */
85bool tm_suspend_disabled __ro_after_init = false;
86
Anton Blanchardb86fd2b2015-10-29 11:43:58 +110087static void check_if_tm_restore_required(struct task_struct *tsk)
Paul Mackerrasd31626f2014-01-13 15:56:29 +110088{
89 /*
90 * If we are saving the current thread's registers, and the
91 * thread is in a transactional state, set the TIF_RESTORE_TM
92 * bit so that we know to restore the registers before
93 * returning to userspace.
94 */
95 if (tsk == current && tsk->thread.regs &&
96 MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
97 !test_thread_flag(TIF_RESTORE_TM)) {
Anshuman Khandual829023d2015-07-06 16:24:10 +053098 tsk->thread.ckpt_regs.msr = tsk->thread.regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +110099 set_thread_flag(TIF_RESTORE_TM);
100 }
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100101}
Cyril Burdc16b552016-09-23 16:18:08 +1000102
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100103#else
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100104static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100105#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
106
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100107bool strict_msr_control;
108EXPORT_SYMBOL(strict_msr_control);
109
110static int __init enable_strict_msr_control(char *str)
111{
112 strict_msr_control = true;
113 pr_info("Enabling strict facility control\n");
114
115 return 0;
116}
117early_param("ppc_strict_facility_enable", enable_strict_msr_control);
118
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000119/* notrace because it's called by restore_math */
120unsigned long notrace msr_check_and_set(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100121{
122 unsigned long oldmsr = mfmsr();
123 unsigned long newmsr;
124
125 newmsr = oldmsr | bits;
126
127#ifdef CONFIG_VSX
128 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
129 newmsr |= MSR_VSX;
130#endif
131
132 if (oldmsr != newmsr)
133 mtmsr_isync(newmsr);
Cyril Bur3cee0702016-09-23 16:18:10 +1000134
135 return newmsr;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100136}
Simon Guod1c72112018-05-23 15:01:44 +0800137EXPORT_SYMBOL_GPL(msr_check_and_set);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100138
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000139/* notrace because it's called by restore_math */
140void notrace __msr_check_and_clear(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100141{
142 unsigned long oldmsr = mfmsr();
143 unsigned long newmsr;
144
145 newmsr = oldmsr & ~bits;
146
147#ifdef CONFIG_VSX
148 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
149 newmsr &= ~MSR_VSX;
150#endif
151
152 if (oldmsr != newmsr)
153 mtmsr_isync(newmsr);
154}
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100155EXPORT_SYMBOL(__msr_check_and_clear);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100156
Kevin Hao037f0ee2013-07-14 17:02:05 +0800157#ifdef CONFIG_PPC_FPU
Mathieu Malaterre1cdf0392018-02-25 18:22:23 +0100158static void __giveup_fpu(struct task_struct *tsk)
Cyril Bur87924682016-02-29 17:53:49 +1100159{
Anton Blanchard8eb98032016-05-29 22:03:50 +1000160 unsigned long msr;
161
Cyril Bur87924682016-02-29 17:53:49 +1100162 save_fpu(tsk);
Anton Blanchard8eb98032016-05-29 22:03:50 +1000163 msr = tsk->thread.regs->msr;
Mark Cave-Aylandfe1ef6b2019-02-08 14:33:19 +0000164 msr &= ~(MSR_FP|MSR_FE0|MSR_FE1);
Cyril Bur87924682016-02-29 17:53:49 +1100165#ifdef CONFIG_VSX
166 if (cpu_has_feature(CPU_FTR_VSX))
Anton Blanchard8eb98032016-05-29 22:03:50 +1000167 msr &= ~MSR_VSX;
Cyril Bur87924682016-02-29 17:53:49 +1100168#endif
Anton Blanchard8eb98032016-05-29 22:03:50 +1000169 tsk->thread.regs->msr = msr;
Cyril Bur87924682016-02-29 17:53:49 +1100170}
171
Anton Blanchard98da5812015-10-29 11:44:01 +1100172void giveup_fpu(struct task_struct *tsk)
173{
Anton Blanchard98da5812015-10-29 11:44:01 +1100174 check_if_tm_restore_required(tsk);
175
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100176 msr_check_and_set(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100177 __giveup_fpu(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100178 msr_check_and_clear(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100179}
180EXPORT_SYMBOL(giveup_fpu);
181
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000182/*
183 * Make sure the floating-point register state in the
184 * the thread_struct is up to date for task tsk.
185 */
186void flush_fp_to_thread(struct task_struct *tsk)
187{
188 if (tsk->thread.regs) {
189 /*
190 * We need to disable preemption here because if we didn't,
191 * another process could get scheduled after the regs->msr
192 * test but before we have finished saving the FP registers
193 * to the thread_struct. That process could take over the
194 * FPU, and then when we get scheduled again we would store
195 * bogus values for the remaining FP registers.
196 */
197 preempt_disable();
198 if (tsk->thread.regs->msr & MSR_FP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000199 /*
200 * This should only ever be called for current or
201 * for a stopped child process. Since we save away
Anton Blanchardaf1bbc32015-10-29 11:43:57 +1100202 * the FP register state on context switch,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000203 * there is something wrong if a stopped child appears
204 * to still have its FP state in the CPU registers.
205 */
206 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100207 giveup_fpu(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000208 }
209 preempt_enable();
210 }
211}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000212EXPORT_SYMBOL_GPL(flush_fp_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000213
214void enable_kernel_fp(void)
215{
Cyril Bure909fb82016-09-23 16:18:11 +1000216 unsigned long cpumsr;
217
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000218 WARN_ON(preemptible());
219
Cyril Bure909fb82016-09-23 16:18:11 +1000220 cpumsr = msr_check_and_set(MSR_FP);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100221
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100222 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
223 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000224 /*
225 * If a thread has already been reclaimed then the
226 * checkpointed registers are on the CPU but have definitely
227 * been saved by the reclaim code. Don't need to and *cannot*
228 * giveup as this would save to the 'live' structure not the
229 * checkpointed structure.
230 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300231 if (!MSR_TM_ACTIVE(cpumsr) &&
232 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000233 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100234 __giveup_fpu(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100235 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000236}
237EXPORT_SYMBOL(enable_kernel_fp);
Anton Blanchardd1e1cf22015-10-29 11:44:11 +1100238#endif /* CONFIG_PPC_FPU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000239
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000240#ifdef CONFIG_ALTIVEC
Cyril Bur6f515d82016-02-29 17:53:50 +1100241static void __giveup_altivec(struct task_struct *tsk)
242{
Anton Blanchard8eb98032016-05-29 22:03:50 +1000243 unsigned long msr;
244
Cyril Bur6f515d82016-02-29 17:53:50 +1100245 save_altivec(tsk);
Anton Blanchard8eb98032016-05-29 22:03:50 +1000246 msr = tsk->thread.regs->msr;
247 msr &= ~MSR_VEC;
Cyril Bur6f515d82016-02-29 17:53:50 +1100248#ifdef CONFIG_VSX
249 if (cpu_has_feature(CPU_FTR_VSX))
Anton Blanchard8eb98032016-05-29 22:03:50 +1000250 msr &= ~MSR_VSX;
Cyril Bur6f515d82016-02-29 17:53:50 +1100251#endif
Anton Blanchard8eb98032016-05-29 22:03:50 +1000252 tsk->thread.regs->msr = msr;
Cyril Bur6f515d82016-02-29 17:53:50 +1100253}
254
Anton Blanchard98da5812015-10-29 11:44:01 +1100255void giveup_altivec(struct task_struct *tsk)
256{
Anton Blanchard98da5812015-10-29 11:44:01 +1100257 check_if_tm_restore_required(tsk);
258
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100259 msr_check_and_set(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100260 __giveup_altivec(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100261 msr_check_and_clear(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100262}
263EXPORT_SYMBOL(giveup_altivec);
264
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000265void enable_kernel_altivec(void)
266{
Cyril Bure909fb82016-09-23 16:18:11 +1000267 unsigned long cpumsr;
268
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000269 WARN_ON(preemptible());
270
Cyril Bure909fb82016-09-23 16:18:11 +1000271 cpumsr = msr_check_and_set(MSR_VEC);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100272
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100273 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
274 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000275 /*
276 * If a thread has already been reclaimed then the
277 * checkpointed registers are on the CPU but have definitely
278 * been saved by the reclaim code. Don't need to and *cannot*
279 * giveup as this would save to the 'live' structure not the
280 * checkpointed structure.
281 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300282 if (!MSR_TM_ACTIVE(cpumsr) &&
283 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000284 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100285 __giveup_altivec(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100286 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000287}
288EXPORT_SYMBOL(enable_kernel_altivec);
289
290/*
291 * Make sure the VMX/Altivec register state in the
292 * the thread_struct is up to date for task tsk.
293 */
294void flush_altivec_to_thread(struct task_struct *tsk)
295{
296 if (tsk->thread.regs) {
297 preempt_disable();
298 if (tsk->thread.regs->msr & MSR_VEC) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000299 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100300 giveup_altivec(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000301 }
302 preempt_enable();
303 }
304}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000305EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000306#endif /* CONFIG_ALTIVEC */
307
Michael Neulingce48b212008-06-25 14:07:18 +1000308#ifdef CONFIG_VSX
Cyril Burbf6a4d52016-02-29 17:53:51 +1100309static void __giveup_vsx(struct task_struct *tsk)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100310{
Benjamin Herrenschmidtdc801082017-08-16 16:01:17 +1000311 unsigned long msr = tsk->thread.regs->msr;
312
313 /*
314 * We should never be ssetting MSR_VSX without also setting
315 * MSR_FP and MSR_VEC
316 */
317 WARN_ON((msr & MSR_VSX) && !((msr & MSR_FP) && (msr & MSR_VEC)));
318
319 /* __giveup_fpu will clear MSR_VSX */
320 if (msr & MSR_FP)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100321 __giveup_fpu(tsk);
Benjamin Herrenschmidtdc801082017-08-16 16:01:17 +1000322 if (msr & MSR_VEC)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100323 __giveup_altivec(tsk);
Cyril Burbf6a4d52016-02-29 17:53:51 +1100324}
325
326static void giveup_vsx(struct task_struct *tsk)
327{
328 check_if_tm_restore_required(tsk);
329
330 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100331 __giveup_vsx(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100332 msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100333}
Cyril Burbf6a4d52016-02-29 17:53:51 +1100334
Michael Neulingce48b212008-06-25 14:07:18 +1000335void enable_kernel_vsx(void)
336{
Cyril Bure909fb82016-09-23 16:18:11 +1000337 unsigned long cpumsr;
338
Michael Neulingce48b212008-06-25 14:07:18 +1000339 WARN_ON(preemptible());
340
Cyril Bure909fb82016-09-23 16:18:11 +1000341 cpumsr = msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100342
Benjamin Herrenschmidt5a69aec2017-08-16 16:01:14 +1000343 if (current->thread.regs &&
344 (current->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP))) {
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100345 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000346 /*
347 * If a thread has already been reclaimed then the
348 * checkpointed registers are on the CPU but have definitely
349 * been saved by the reclaim code. Don't need to and *cannot*
350 * giveup as this would save to the 'live' structure not the
351 * checkpointed structure.
352 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300353 if (!MSR_TM_ACTIVE(cpumsr) &&
354 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000355 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100356 __giveup_vsx(current);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100357 }
Michael Neulingce48b212008-06-25 14:07:18 +1000358}
359EXPORT_SYMBOL(enable_kernel_vsx);
Michael Neulingce48b212008-06-25 14:07:18 +1000360
361void flush_vsx_to_thread(struct task_struct *tsk)
362{
363 if (tsk->thread.regs) {
364 preempt_disable();
Benjamin Herrenschmidt5a69aec2017-08-16 16:01:14 +1000365 if (tsk->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP)) {
Michael Neulingce48b212008-06-25 14:07:18 +1000366 BUG_ON(tsk != current);
Michael Neulingce48b212008-06-25 14:07:18 +1000367 giveup_vsx(tsk);
368 }
369 preempt_enable();
370 }
371}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000372EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
Michael Neulingce48b212008-06-25 14:07:18 +1000373#endif /* CONFIG_VSX */
374
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000375#ifdef CONFIG_SPE
Anton Blanchard98da5812015-10-29 11:44:01 +1100376void giveup_spe(struct task_struct *tsk)
377{
Anton Blanchard98da5812015-10-29 11:44:01 +1100378 check_if_tm_restore_required(tsk);
379
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100380 msr_check_and_set(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100381 __giveup_spe(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100382 msr_check_and_clear(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100383}
384EXPORT_SYMBOL(giveup_spe);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000385
386void enable_kernel_spe(void)
387{
388 WARN_ON(preemptible());
389
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100390 msr_check_and_set(MSR_SPE);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100391
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100392 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
393 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100394 __giveup_spe(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100395 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000396}
397EXPORT_SYMBOL(enable_kernel_spe);
398
399void flush_spe_to_thread(struct task_struct *tsk)
400{
401 if (tsk->thread.regs) {
402 preempt_disable();
403 if (tsk->thread.regs->msr & MSR_SPE) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000404 BUG_ON(tsk != current);
yu liu685659e2011-06-14 18:34:25 -0500405 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
Kumar Gala0ee6c152007-08-28 21:15:53 -0500406 giveup_spe(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000407 }
408 preempt_enable();
409 }
410}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000411#endif /* CONFIG_SPE */
412
Anton Blanchardc2085052015-10-29 11:44:08 +1100413static unsigned long msr_all_available;
414
415static int __init init_msr_all_available(void)
416{
417#ifdef CONFIG_PPC_FPU
418 msr_all_available |= MSR_FP;
419#endif
420#ifdef CONFIG_ALTIVEC
421 if (cpu_has_feature(CPU_FTR_ALTIVEC))
422 msr_all_available |= MSR_VEC;
423#endif
424#ifdef CONFIG_VSX
425 if (cpu_has_feature(CPU_FTR_VSX))
426 msr_all_available |= MSR_VSX;
427#endif
428#ifdef CONFIG_SPE
429 if (cpu_has_feature(CPU_FTR_SPE))
430 msr_all_available |= MSR_SPE;
431#endif
432
433 return 0;
434}
435early_initcall(init_msr_all_available);
436
437void giveup_all(struct task_struct *tsk)
438{
439 unsigned long usermsr;
440
441 if (!tsk->thread.regs)
442 return;
443
Gustavo Romero8205d5d2019-09-04 00:55:27 -0400444 check_if_tm_restore_required(tsk);
445
Anton Blanchardc2085052015-10-29 11:44:08 +1100446 usermsr = tsk->thread.regs->msr;
447
448 if ((usermsr & msr_all_available) == 0)
449 return;
450
451 msr_check_and_set(msr_all_available);
452
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000453 WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
454
Anton Blanchardc2085052015-10-29 11:44:08 +1100455#ifdef CONFIG_PPC_FPU
456 if (usermsr & MSR_FP)
457 __giveup_fpu(tsk);
458#endif
459#ifdef CONFIG_ALTIVEC
460 if (usermsr & MSR_VEC)
461 __giveup_altivec(tsk);
462#endif
Anton Blanchardc2085052015-10-29 11:44:08 +1100463#ifdef CONFIG_SPE
464 if (usermsr & MSR_SPE)
465 __giveup_spe(tsk);
466#endif
467
468 msr_check_and_clear(msr_all_available);
469}
470EXPORT_SYMBOL(giveup_all);
471
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000472#ifdef CONFIG_PPC_BOOK3S_64
473#ifdef CONFIG_PPC_FPU
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000474static bool should_restore_fp(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000475{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000476 if (current->thread.load_fp) {
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000477 current->thread.load_fp++;
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000478 return true;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000479 }
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000480 return false;
481}
482
483static void do_restore_fp(void)
484{
485 load_fp_state(&current->thread.fp_state);
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000486}
487#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000488static bool should_restore_fp(void) { return false; }
489static void do_restore_fp(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000490#endif /* CONFIG_PPC_FPU */
491
492#ifdef CONFIG_ALTIVEC
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000493static bool should_restore_altivec(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000494{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000495 if (cpu_has_feature(CPU_FTR_ALTIVEC) && (current->thread.load_vec)) {
496 current->thread.load_vec++;
497 return true;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000498 }
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000499 return false;
500}
501
502static void do_restore_altivec(void)
503{
504 load_vr_state(&current->thread.vr_state);
505 current->thread.used_vr = 1;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000506}
507#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000508static bool should_restore_altivec(void) { return false; }
509static void do_restore_altivec(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000510#endif /* CONFIG_ALTIVEC */
511
512#ifdef CONFIG_VSX
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000513static bool should_restore_vsx(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000514{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000515 if (cpu_has_feature(CPU_FTR_VSX))
516 return true;
517 return false;
518}
519static void do_restore_vsx(void)
520{
521 current->thread.used_vsr = 1;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000522}
523#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000524static bool should_restore_vsx(void) { return false; }
525static void do_restore_vsx(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000526#endif /* CONFIG_VSX */
527
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000528/*
529 * The exception exit path calls restore_math() with interrupts hard disabled
530 * but the soft irq state not "reconciled". ftrace code that calls
531 * local_irq_save/restore causes warnings.
532 *
533 * Rather than complicate the exit path, just don't trace restore_math. This
534 * could be done by having ftrace entry code check for this un-reconciled
535 * condition where MSR[EE]=0 and PACA_IRQ_HARD_DIS is not set, and
536 * temporarily fix it up for the duration of the ftrace call.
537 */
538void notrace restore_math(struct pt_regs *regs)
Cyril Bur70fe3d92016-02-29 17:53:47 +1100539{
540 unsigned long msr;
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000541 unsigned long new_msr = 0;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100542
543 msr = regs->msr;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100544
545 /*
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000546 * new_msr tracks the facilities that are to be restored. Only reload
547 * if the bit is not set in the user MSR (if it is set, the registers
548 * are live for the user thread).
Cyril Bur70fe3d92016-02-29 17:53:47 +1100549 */
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000550 if ((!(msr & MSR_FP)) && should_restore_fp())
Michael Ellermanb91eb512020-08-25 19:34:24 +1000551 new_msr |= MSR_FP;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100552
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000553 if ((!(msr & MSR_VEC)) && should_restore_altivec())
554 new_msr |= MSR_VEC;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100555
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000556 if ((!(msr & MSR_VSX)) && should_restore_vsx()) {
557 if (((msr | new_msr) & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC))
558 new_msr |= MSR_VSX;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100559 }
560
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000561 if (new_msr) {
Michael Ellermanb91eb512020-08-25 19:34:24 +1000562 unsigned long fpexc_mode = 0;
563
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000564 msr_check_and_set(new_msr);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100565
Michael Ellermanb91eb512020-08-25 19:34:24 +1000566 if (new_msr & MSR_FP) {
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000567 do_restore_fp();
568
Michael Ellermanb91eb512020-08-25 19:34:24 +1000569 // This also covers VSX, because VSX implies FP
570 fpexc_mode = current->thread.fpexc_mode;
571 }
572
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000573 if (new_msr & MSR_VEC)
574 do_restore_altivec();
575
576 if (new_msr & MSR_VSX)
577 do_restore_vsx();
578
579 msr_check_and_clear(new_msr);
580
Michael Ellermanb91eb512020-08-25 19:34:24 +1000581 regs->msr |= new_msr | fpexc_mode;
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000582 }
Cyril Bur70fe3d92016-02-29 17:53:47 +1100583}
Christophe Leroy60d62bf2020-08-17 05:46:45 +0000584#endif /* CONFIG_PPC_BOOK3S_64 */
Cyril Bur70fe3d92016-02-29 17:53:47 +1100585
Mathieu Malaterre1cdf0392018-02-25 18:22:23 +0100586static void save_all(struct task_struct *tsk)
Cyril Burde2a20a2016-02-29 17:53:48 +1100587{
588 unsigned long usermsr;
589
590 if (!tsk->thread.regs)
591 return;
592
593 usermsr = tsk->thread.regs->msr;
594
595 if ((usermsr & msr_all_available) == 0)
596 return;
597
598 msr_check_and_set(msr_all_available);
599
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000600 WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
Cyril Burde2a20a2016-02-29 17:53:48 +1100601
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000602 if (usermsr & MSR_FP)
603 save_fpu(tsk);
604
605 if (usermsr & MSR_VEC)
606 save_altivec(tsk);
Cyril Burde2a20a2016-02-29 17:53:48 +1100607
608 if (usermsr & MSR_SPE)
609 __giveup_spe(tsk);
610
611 msr_check_and_clear(msr_all_available);
Ram Paic76662e2018-07-17 06:51:05 -0700612 thread_pkey_regs_save(&tsk->thread);
Cyril Burde2a20a2016-02-29 17:53:48 +1100613}
614
Anton Blanchard579e6332015-10-29 11:44:09 +1100615void flush_all_to_thread(struct task_struct *tsk)
616{
617 if (tsk->thread.regs) {
618 preempt_disable();
619 BUG_ON(tsk != current);
Anton Blanchard579e6332015-10-29 11:44:09 +1100620#ifdef CONFIG_SPE
621 if (tsk->thread.regs->msr & MSR_SPE)
622 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
623#endif
Felipe Rechiae9013782018-10-24 10:57:22 -0300624 save_all(tsk);
Anton Blanchard579e6332015-10-29 11:44:09 +1100625
626 preempt_enable();
627 }
628}
629EXPORT_SYMBOL(flush_all_to_thread);
630
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000631#ifdef CONFIG_PPC_ADV_DEBUG_REGS
632void do_send_trap(struct pt_regs *regs, unsigned long address,
Eric W. Biederman47355042018-01-16 16:12:38 -0600633 unsigned long error_code, int breakpt)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000634{
Eric W. Biederman47355042018-01-16 16:12:38 -0600635 current->thread.trap_nr = TRAP_HWBKPT;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000636 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
637 11, SIGSEGV) == NOTIFY_STOP)
638 return;
639
640 /* Deliver the signal to userspace */
Eric W. Biedermanf71dd7d2018-01-22 14:37:25 -0600641 force_sig_ptrace_errno_trap(breakpt, /* breakpoint or watchpoint id */
642 (void __user *)address);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000643}
644#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Ravi Bangoria5b905d72020-09-02 09:59:42 +0530645
646static void do_break_handler(struct pt_regs *regs)
647{
648 struct arch_hw_breakpoint null_brk = {0};
649 struct arch_hw_breakpoint *info;
650 struct ppc_inst instr = ppc_inst(0);
651 int type = 0;
652 int size = 0;
653 unsigned long ea;
654 int i;
655
656 /*
657 * If underneath hw supports only one watchpoint, we know it
658 * caused exception. 8xx also falls into this category.
659 */
660 if (nr_wp_slots() == 1) {
661 __set_breakpoint(0, &null_brk);
662 current->thread.hw_brk[0] = null_brk;
663 current->thread.hw_brk[0].flags |= HW_BRK_FLAG_DISABLED;
664 return;
665 }
666
667 /* Otherwise findout which DAWR caused exception and disable it. */
668 wp_get_instr_detail(regs, &instr, &type, &size, &ea);
669
670 for (i = 0; i < nr_wp_slots(); i++) {
671 info = &current->thread.hw_brk[i];
672 if (!info->address)
673 continue;
674
675 if (wp_check_constraints(regs, instr, ea, type, size, info)) {
676 __set_breakpoint(i, &null_brk);
677 current->thread.hw_brk[i] = null_brk;
678 current->thread.hw_brk[i].flags |= HW_BRK_FLAG_DISABLED;
679 }
680 }
681}
682
Michael Neuling9422de32012-12-20 14:06:44 +0000683void do_break (struct pt_regs *regs, unsigned long address,
Luis Machadod6a61bf2008-07-24 02:10:41 +1000684 unsigned long error_code)
685{
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000686 current->thread.trap_nr = TRAP_HWBKPT;
Luis Machadod6a61bf2008-07-24 02:10:41 +1000687 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
688 11, SIGSEGV) == NOTIFY_STOP)
689 return;
690
Michael Neuling9422de32012-12-20 14:06:44 +0000691 if (debugger_break_match(regs))
Luis Machadod6a61bf2008-07-24 02:10:41 +1000692 return;
693
Ravi Bangoria5b905d72020-09-02 09:59:42 +0530694 /*
695 * We reach here only when watchpoint exception is generated by ptrace
696 * event (or hw is buggy!). Now if CONFIG_HAVE_HW_BREAKPOINT is set,
697 * watchpoint is already handled by hw_breakpoint_handler() so we don't
698 * have to do anything. But when CONFIG_HAVE_HW_BREAKPOINT is not set,
699 * we need to manually handle the watchpoint here.
700 */
701 if (!IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT))
702 do_break_handler(regs);
703
Luis Machadod6a61bf2008-07-24 02:10:41 +1000704 /* Deliver the signal to userspace */
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500705 force_sig_fault(SIGTRAP, TRAP_HWBKPT, (void __user *)address);
Luis Machadod6a61bf2008-07-24 02:10:41 +1000706}
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000707#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000708
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530709static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk[HBP_NUM_MAX]);
Michael Ellermana2ceff52008-03-28 19:11:48 +1100710
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000711#ifdef CONFIG_PPC_ADV_DEBUG_REGS
712/*
713 * Set the debug registers back to their default "safe" values.
714 */
715static void set_debug_reg_defaults(struct thread_struct *thread)
716{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530717 thread->debug.iac1 = thread->debug.iac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000718#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530719 thread->debug.iac3 = thread->debug.iac4 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000720#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530721 thread->debug.dac1 = thread->debug.dac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000722#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530723 thread->debug.dvc1 = thread->debug.dvc2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000724#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530725 thread->debug.dbcr0 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000726#ifdef CONFIG_BOOKE
727 /*
728 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
729 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530730 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000731 DBCR1_IAC3US | DBCR1_IAC4US;
732 /*
733 * Force Data Address Compare User/Supervisor bits to be User-only
734 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
735 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530736 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000737#else
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530738 thread->debug.dbcr1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000739#endif
740}
741
Scott Woodf5f97212013-11-22 15:52:29 -0600742static void prime_debug_regs(struct debug_reg *debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000743{
Scott Wood6cecf762013-05-13 14:14:53 +0000744 /*
745 * We could have inherited MSR_DE from userspace, since
746 * it doesn't get cleared on exception entry. Make sure
747 * MSR_DE is clear before we enable any debug events.
748 */
749 mtmsr(mfmsr() & ~MSR_DE);
750
Scott Woodf5f97212013-11-22 15:52:29 -0600751 mtspr(SPRN_IAC1, debug->iac1);
752 mtspr(SPRN_IAC2, debug->iac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000753#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Scott Woodf5f97212013-11-22 15:52:29 -0600754 mtspr(SPRN_IAC3, debug->iac3);
755 mtspr(SPRN_IAC4, debug->iac4);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000756#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600757 mtspr(SPRN_DAC1, debug->dac1);
758 mtspr(SPRN_DAC2, debug->dac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000759#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Scott Woodf5f97212013-11-22 15:52:29 -0600760 mtspr(SPRN_DVC1, debug->dvc1);
761 mtspr(SPRN_DVC2, debug->dvc2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000762#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600763 mtspr(SPRN_DBCR0, debug->dbcr0);
764 mtspr(SPRN_DBCR1, debug->dbcr1);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000765#ifdef CONFIG_BOOKE
Scott Woodf5f97212013-11-22 15:52:29 -0600766 mtspr(SPRN_DBCR2, debug->dbcr2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000767#endif
768}
769/*
770 * Unless neither the old or new thread are making use of the
771 * debug registers, set the debug registers from the values
772 * stored in the new thread.
773 */
Scott Woodf5f97212013-11-22 15:52:29 -0600774void switch_booke_debug_regs(struct debug_reg *new_debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000775{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530776 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
Scott Woodf5f97212013-11-22 15:52:29 -0600777 || (new_debug->dbcr0 & DBCR0_IDM))
778 prime_debug_regs(new_debug);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000779}
Bharat Bhushan3743c9b2013-07-04 12:27:44 +0530780EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000781#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
K.Prasade0780b72011-02-10 04:44:35 +0000782#ifndef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530783static void set_breakpoint(int i, struct arch_hw_breakpoint *brk)
Christophe Leroyb5ac51d2018-07-05 16:25:05 +0000784{
785 preempt_disable();
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530786 __set_breakpoint(i, brk);
Christophe Leroyb5ac51d2018-07-05 16:25:05 +0000787 preempt_enable();
788}
789
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000790static void set_debug_reg_defaults(struct thread_struct *thread)
791{
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530792 int i;
793 struct arch_hw_breakpoint null_brk = {0};
794
795 for (i = 0; i < nr_wp_slots(); i++) {
796 thread->hw_brk[i] = null_brk;
797 if (ppc_breakpoint_available())
798 set_breakpoint(i, &thread->hw_brk[i]);
799 }
800}
801
802static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
803 struct arch_hw_breakpoint *b)
804{
805 if (a->address != b->address)
806 return false;
807 if (a->type != b->type)
808 return false;
809 if (a->len != b->len)
810 return false;
811 /* no need to check hw_len. it's calculated from address and len */
812 return true;
813}
814
815static void switch_hw_breakpoint(struct task_struct *new)
816{
817 int i;
818
819 for (i = 0; i < nr_wp_slots(); i++) {
820 if (likely(hw_brk_match(this_cpu_ptr(&current_brk[i]),
821 &new->thread.hw_brk[i])))
822 continue;
823
824 __set_breakpoint(i, &new->thread.hw_brk[i]);
825 }
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000826}
K.Prasade0780b72011-02-10 04:44:35 +0000827#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000828#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
829
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000830#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling9422de32012-12-20 14:06:44 +0000831static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
832{
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000833 mtspr(SPRN_DAC1, dabr);
Christophe Leroy04d476b2020-08-17 05:46:41 +0000834 if (IS_ENABLED(CONFIG_PPC_47x))
835 isync();
Michael Neuling9422de32012-12-20 14:06:44 +0000836 return 0;
837}
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000838#elif defined(CONFIG_PPC_BOOK3S)
Michael Neuling9422de32012-12-20 14:06:44 +0000839static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
840{
Michael Ellermancab0af92005-11-03 15:30:49 +1100841 mtspr(SPRN_DABR, dabr);
Michael Neuling82a9f162013-05-16 20:27:31 +0000842 if (cpu_has_feature(CPU_FTR_DABRX))
843 mtspr(SPRN_DABRX, dabrx);
Michael Ellermancab0af92005-11-03 15:30:49 +1100844 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000845}
Michael Neuling9422de32012-12-20 14:06:44 +0000846#else
847static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
848{
849 return -EINVAL;
850}
851#endif
852
853static inline int set_dabr(struct arch_hw_breakpoint *brk)
854{
855 unsigned long dabr, dabrx;
856
857 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
858 dabrx = ((brk->type >> 3) & 0x7);
859
860 if (ppc_md.set_dabr)
861 return ppc_md.set_dabr(dabr, dabrx);
862
863 return __set_dabr(dabr, dabrx);
864}
865
Christophe Leroy39413ae2019-11-26 17:43:29 +0000866static inline int set_breakpoint_8xx(struct arch_hw_breakpoint *brk)
867{
868 unsigned long lctrl1 = LCTRL1_CTE_GT | LCTRL1_CTF_LT | LCTRL1_CRWE_RW |
869 LCTRL1_CRWF_RW;
870 unsigned long lctrl2 = LCTRL2_LW0EN | LCTRL2_LW0LADC | LCTRL2_SLW0EN;
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530871 unsigned long start_addr = ALIGN_DOWN(brk->address, HW_BREAKPOINT_SIZE);
872 unsigned long end_addr = ALIGN(brk->address + brk->len, HW_BREAKPOINT_SIZE);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000873
874 if (start_addr == 0)
875 lctrl2 |= LCTRL2_LW0LA_F;
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530876 else if (end_addr == 0)
Christophe Leroy39413ae2019-11-26 17:43:29 +0000877 lctrl2 |= LCTRL2_LW0LA_E;
878 else
879 lctrl2 |= LCTRL2_LW0LA_EandF;
880
881 mtspr(SPRN_LCTRL2, 0);
882
883 if ((brk->type & HW_BRK_TYPE_RDWR) == 0)
884 return 0;
885
886 if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_READ)
887 lctrl1 |= LCTRL1_CRWE_RO | LCTRL1_CRWF_RO;
888 if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_WRITE)
889 lctrl1 |= LCTRL1_CRWE_WO | LCTRL1_CRWF_WO;
890
891 mtspr(SPRN_CMPE, start_addr - 1);
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530892 mtspr(SPRN_CMPF, end_addr);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000893 mtspr(SPRN_LCTRL1, lctrl1);
894 mtspr(SPRN_LCTRL2, lctrl2);
895
896 return 0;
897}
898
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530899void __set_breakpoint(int nr, struct arch_hw_breakpoint *brk)
Michael Neuling9422de32012-12-20 14:06:44 +0000900{
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530901 memcpy(this_cpu_ptr(&current_brk[nr]), brk, sizeof(*brk));
Michael Neuling9422de32012-12-20 14:06:44 +0000902
Michael Neulingc1fe1902019-04-01 17:03:12 +1100903 if (dawr_enabled())
Nicholas Piggin252988c2018-04-01 15:50:36 +1000904 // Power8 or later
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530905 set_dawr(nr, brk);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000906 else if (IS_ENABLED(CONFIG_PPC_8xx))
907 set_breakpoint_8xx(brk);
Nicholas Piggin252988c2018-04-01 15:50:36 +1000908 else if (!cpu_has_feature(CPU_FTR_ARCH_207S))
909 // Power7 or earlier
Paul Gortmaker04c32a52014-04-29 15:25:16 -0400910 set_dabr(brk);
Nicholas Piggin252988c2018-04-01 15:50:36 +1000911 else
912 // Shouldn't happen due to higher level checks
913 WARN_ON_ONCE(1);
Michael Neuling9422de32012-12-20 14:06:44 +0000914}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000915
Michael Neuling404b27d2018-03-27 15:37:17 +1100916/* Check if we have DAWR or DABR hardware */
917bool ppc_breakpoint_available(void)
918{
Michael Neulingc1fe1902019-04-01 17:03:12 +1100919 if (dawr_enabled())
920 return true; /* POWER8 DAWR or POWER9 forced DAWR */
Michael Neuling404b27d2018-03-27 15:37:17 +1100921 if (cpu_has_feature(CPU_FTR_ARCH_207S))
922 return false; /* POWER9 with DAWR disabled */
923 /* DABR: Everything but POWER8 and POWER9 */
924 return true;
925}
926EXPORT_SYMBOL_GPL(ppc_breakpoint_available);
927
Michael Neulingfb096922013-02-13 16:21:37 +0000928#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Cyril Bur5d176f72016-09-14 18:02:16 +1000929
930static inline bool tm_enabled(struct task_struct *tsk)
931{
932 return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM);
933}
934
Cyril Buredd00b82018-02-01 12:07:46 +1100935static void tm_reclaim_thread(struct thread_struct *thr, uint8_t cause)
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100936{
Michael Neuling7f821fc2015-11-19 15:44:45 +1100937 /*
938 * Use the current MSR TM suspended bit to track if we have
939 * checkpointed state outstanding.
940 * On signal delivery, we'd normally reclaim the checkpointed
941 * state to obtain stack pointer (see:get_tm_stackpointer()).
942 * This will then directly return to userspace without going
943 * through __switch_to(). However, if the stack frame is bad,
944 * we need to exit this thread which calls __switch_to() which
945 * will again attempt to reclaim the already saved tm state.
946 * Hence we need to check that we've not already reclaimed
947 * this state.
948 * We do this using the current MSR, rather tracking it in
949 * some specific thread_struct bit, as it has the additional
Michael Ellerman027dfac2016-06-01 16:34:37 +1000950 * benefit of checking for a potential TM bad thing exception.
Michael Neuling7f821fc2015-11-19 15:44:45 +1100951 */
952 if (!MSR_TM_SUSPENDED(mfmsr()))
953 return;
954
Cyril Bur91381b92017-11-02 14:09:04 +1100955 giveup_all(container_of(thr, struct task_struct, thread));
956
Cyril Bureb5c3f12017-11-02 14:09:05 +1100957 tm_reclaim(thr, cause);
958
Michael Neulingf48e91e2017-05-08 17:16:26 +1000959 /*
960 * If we are in a transaction and FP is off then we can't have
961 * used FP inside that transaction. Hence the checkpointed
962 * state is the same as the live state. We need to copy the
963 * live state to the checkpointed state so that when the
964 * transaction is restored, the checkpointed state is correct
965 * and the aborted transaction sees the correct state. We use
966 * ckpt_regs.msr here as that's what tm_reclaim will use to
967 * determine if it's going to write the checkpointed state or
968 * not. So either this will write the checkpointed registers,
969 * or reclaim will. Similarly for VMX.
970 */
971 if ((thr->ckpt_regs.msr & MSR_FP) == 0)
972 memcpy(&thr->ckfp_state, &thr->fp_state,
973 sizeof(struct thread_fp_state));
974 if ((thr->ckpt_regs.msr & MSR_VEC) == 0)
975 memcpy(&thr->ckvr_state, &thr->vr_state,
976 sizeof(struct thread_vr_state));
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100977}
978
979void tm_reclaim_current(uint8_t cause)
980{
981 tm_enable();
Cyril Buredd00b82018-02-01 12:07:46 +1100982 tm_reclaim_thread(&current->thread, cause);
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100983}
984
Michael Neulingfb096922013-02-13 16:21:37 +0000985static inline void tm_reclaim_task(struct task_struct *tsk)
986{
987 /* We have to work out if we're switching from/to a task that's in the
988 * middle of a transaction.
989 *
990 * In switching we need to maintain a 2nd register state as
991 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
Cyril Bur000ec282016-09-23 16:18:25 +1000992 * checkpointed (tbegin) state in ckpt_regs, ckfp_state and
993 * ckvr_state
Michael Neulingfb096922013-02-13 16:21:37 +0000994 *
995 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
996 */
997 struct thread_struct *thr = &tsk->thread;
998
999 if (!thr->regs)
1000 return;
1001
1002 if (!MSR_TM_ACTIVE(thr->regs->msr))
1003 goto out_and_saveregs;
1004
Michael Neuling92fb8692017-10-12 21:17:19 +11001005 WARN_ON(tm_suspend_disabled);
1006
Michael Neulingfb096922013-02-13 16:21:37 +00001007 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
1008 "ccr=%lx, msr=%lx, trap=%lx)\n",
1009 tsk->pid, thr->regs->nip,
1010 thr->regs->ccr, thr->regs->msr,
1011 thr->regs->trap);
1012
Cyril Buredd00b82018-02-01 12:07:46 +11001013 tm_reclaim_thread(thr, TM_CAUSE_RESCHED);
Michael Neulingfb096922013-02-13 16:21:37 +00001014
1015 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
1016 tsk->pid);
1017
1018out_and_saveregs:
1019 /* Always save the regs here, even if a transaction's not active.
1020 * This context-switches a thread's TM info SPRs. We do it here to
1021 * be consistent with the restore path (in recheckpoint) which
1022 * cannot happen later in _switch().
1023 */
1024 tm_save_sprs(thr);
1025}
1026
Cyril Bureb5c3f12017-11-02 14:09:05 +11001027extern void __tm_recheckpoint(struct thread_struct *thread);
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001028
Cyril Bureb5c3f12017-11-02 14:09:05 +11001029void tm_recheckpoint(struct thread_struct *thread)
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001030{
1031 unsigned long flags;
1032
Cyril Bur5d176f72016-09-14 18:02:16 +10001033 if (!(thread->regs->msr & MSR_TM))
1034 return;
1035
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001036 /* We really can't be interrupted here as the TEXASR registers can't
1037 * change and later in the trecheckpoint code, we have a userspace R1.
1038 * So let's hard disable over this region.
1039 */
1040 local_irq_save(flags);
1041 hard_irq_disable();
1042
1043 /* The TM SPRs are restored here, so that TEXASR.FS can be set
1044 * before the trecheckpoint and no explosion occurs.
1045 */
1046 tm_restore_sprs(thread);
1047
Cyril Bureb5c3f12017-11-02 14:09:05 +11001048 __tm_recheckpoint(thread);
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001049
1050 local_irq_restore(flags);
1051}
1052
Michael Neulingbc2a9402013-02-13 16:21:40 +00001053static inline void tm_recheckpoint_new_task(struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +00001054{
Michael Neulingfb096922013-02-13 16:21:37 +00001055 if (!cpu_has_feature(CPU_FTR_TM))
1056 return;
1057
1058 /* Recheckpoint the registers of the thread we're about to switch to.
1059 *
1060 * If the task was using FP, we non-lazily reload both the original and
1061 * the speculative FP register states. This is because the kernel
1062 * doesn't see if/when a TM rollback occurs, so if we take an FP
Cyril Burdc310662016-09-23 16:18:24 +10001063 * unavailable later, we are unable to determine which set of FP regs
Michael Neulingfb096922013-02-13 16:21:37 +00001064 * need to be restored.
1065 */
Cyril Bur5d176f72016-09-14 18:02:16 +10001066 if (!tm_enabled(new))
Michael Neulingfb096922013-02-13 16:21:37 +00001067 return;
1068
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001069 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
1070 tm_restore_sprs(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +00001071 return;
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001072 }
Michael Neulingfb096922013-02-13 16:21:37 +00001073 /* Recheckpoint to restore original checkpointed register state. */
Cyril Bureb5c3f12017-11-02 14:09:05 +11001074 TM_DEBUG("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n",
1075 new->pid, new->thread.regs->msr);
Michael Neulingfb096922013-02-13 16:21:37 +00001076
Cyril Bureb5c3f12017-11-02 14:09:05 +11001077 tm_recheckpoint(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +00001078
Cyril Burdc310662016-09-23 16:18:24 +10001079 /*
1080 * The checkpointed state has been restored but the live state has
1081 * not, ensure all the math functionality is turned off to trigger
1082 * restore_math() to reload.
1083 */
1084 new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX);
Michael Neulingfb096922013-02-13 16:21:37 +00001085
1086 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
1087 "(kernel msr 0x%lx)\n",
1088 new->pid, mfmsr());
1089}
1090
Cyril Burdc310662016-09-23 16:18:24 +10001091static inline void __switch_to_tm(struct task_struct *prev,
1092 struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +00001093{
1094 if (cpu_has_feature(CPU_FTR_TM)) {
Cyril Bur5d176f72016-09-14 18:02:16 +10001095 if (tm_enabled(prev) || tm_enabled(new))
1096 tm_enable();
1097
1098 if (tm_enabled(prev)) {
1099 prev->thread.load_tm++;
1100 tm_reclaim_task(prev);
1101 if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0)
1102 prev->thread.regs->msr &= ~MSR_TM;
1103 }
1104
Cyril Burdc310662016-09-23 16:18:24 +10001105 tm_recheckpoint_new_task(new);
Michael Neulingfb096922013-02-13 16:21:37 +00001106 }
1107}
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001108
1109/*
1110 * This is called if we are on the way out to userspace and the
1111 * TIF_RESTORE_TM flag is set. It checks if we need to reload
1112 * FP and/or vector state and does so if necessary.
1113 * If userspace is inside a transaction (whether active or
1114 * suspended) and FP/VMX/VSX instructions have ever been enabled
1115 * inside that transaction, then we have to keep them enabled
1116 * and keep the FP/VMX/VSX state loaded while ever the transaction
1117 * continues. The reason is that if we didn't, and subsequently
1118 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
1119 * we don't know whether it's the same transaction, and thus we
1120 * don't know which of the checkpointed state and the transactional
1121 * state to use.
1122 */
1123void restore_tm_state(struct pt_regs *regs)
1124{
1125 unsigned long msr_diff;
1126
Cyril Burdc310662016-09-23 16:18:24 +10001127 /*
1128 * This is the only moment we should clear TIF_RESTORE_TM as
1129 * it is here that ckpt_regs.msr and pt_regs.msr become the same
1130 * again, anything else could lead to an incorrect ckpt_msr being
1131 * saved and therefore incorrect signal contexts.
1132 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001133 clear_thread_flag(TIF_RESTORE_TM);
1134 if (!MSR_TM_ACTIVE(regs->msr))
1135 return;
1136
Anshuman Khandual829023d2015-07-06 16:24:10 +05301137 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001138 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
Cyril Bur70fe3d92016-02-29 17:53:47 +11001139
Cyril Burdc16b552016-09-23 16:18:08 +10001140 /* Ensure that restore_math() will restore */
1141 if (msr_diff & MSR_FP)
1142 current->thread.load_fp = 1;
Valentin Rothberg39715bf2016-10-05 07:57:26 +02001143#ifdef CONFIG_ALTIVEC
Cyril Burdc16b552016-09-23 16:18:08 +10001144 if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC)
1145 current->thread.load_vec = 1;
1146#endif
Cyril Bur70fe3d92016-02-29 17:53:47 +11001147 restore_math(regs);
1148
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001149 regs->msr |= msr_diff;
1150}
1151
Michael Neulingfb096922013-02-13 16:21:37 +00001152#else
1153#define tm_recheckpoint_new_task(new)
Cyril Burdc310662016-09-23 16:18:24 +10001154#define __switch_to_tm(prev, new)
Michael Neulingfb096922013-02-13 16:21:37 +00001155#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neuling9422de32012-12-20 14:06:44 +00001156
Anton Blanchard152d5232015-10-29 11:43:55 +11001157static inline void save_sprs(struct thread_struct *t)
1158{
1159#ifdef CONFIG_ALTIVEC
Oliver O'Halloran01d7c2a22016-03-08 09:08:47 +11001160 if (cpu_has_feature(CPU_FTR_ALTIVEC))
Anton Blanchard152d5232015-10-29 11:43:55 +11001161 t->vrsave = mfspr(SPRN_VRSAVE);
1162#endif
1163#ifdef CONFIG_PPC_BOOK3S_64
1164 if (cpu_has_feature(CPU_FTR_DSCR))
1165 t->dscr = mfspr(SPRN_DSCR);
1166
1167 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1168 t->bescr = mfspr(SPRN_BESCR);
1169 t->ebbhr = mfspr(SPRN_EBBHR);
1170 t->ebbrr = mfspr(SPRN_EBBRR);
1171
1172 t->fscr = mfspr(SPRN_FSCR);
1173
1174 /*
1175 * Note that the TAR is not available for use in the kernel.
1176 * (To provide this, the TAR should be backed up/restored on
1177 * exception entry/exit instead, and be in pt_regs. FIXME,
1178 * this should be in pt_regs anyway (for debug).)
1179 */
1180 t->tar = mfspr(SPRN_TAR);
1181 }
1182#endif
Ram Pai06bb53b2018-01-18 17:50:31 -08001183
1184 thread_pkey_regs_save(t);
Anton Blanchard152d5232015-10-29 11:43:55 +11001185}
1186
1187static inline void restore_sprs(struct thread_struct *old_thread,
1188 struct thread_struct *new_thread)
1189{
1190#ifdef CONFIG_ALTIVEC
1191 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
1192 old_thread->vrsave != new_thread->vrsave)
1193 mtspr(SPRN_VRSAVE, new_thread->vrsave);
1194#endif
1195#ifdef CONFIG_PPC_BOOK3S_64
1196 if (cpu_has_feature(CPU_FTR_DSCR)) {
1197 u64 dscr = get_paca()->dscr_default;
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001198 if (new_thread->dscr_inherit)
Anton Blanchard152d5232015-10-29 11:43:55 +11001199 dscr = new_thread->dscr;
Anton Blanchard152d5232015-10-29 11:43:55 +11001200
1201 if (old_thread->dscr != dscr)
1202 mtspr(SPRN_DSCR, dscr);
Anton Blanchard152d5232015-10-29 11:43:55 +11001203 }
1204
1205 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1206 if (old_thread->bescr != new_thread->bescr)
1207 mtspr(SPRN_BESCR, new_thread->bescr);
1208 if (old_thread->ebbhr != new_thread->ebbhr)
1209 mtspr(SPRN_EBBHR, new_thread->ebbhr);
1210 if (old_thread->ebbrr != new_thread->ebbrr)
1211 mtspr(SPRN_EBBRR, new_thread->ebbrr);
1212
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001213 if (old_thread->fscr != new_thread->fscr)
1214 mtspr(SPRN_FSCR, new_thread->fscr);
1215
Anton Blanchard152d5232015-10-29 11:43:55 +11001216 if (old_thread->tar != new_thread->tar)
1217 mtspr(SPRN_TAR, new_thread->tar);
1218 }
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001219
Alastair D'Silva3449f192018-05-11 16:12:58 +10001220 if (cpu_has_feature(CPU_FTR_P9_TIDR) &&
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001221 old_thread->tidr != new_thread->tidr)
1222 mtspr(SPRN_TIDR, new_thread->tidr);
Anton Blanchard152d5232015-10-29 11:43:55 +11001223#endif
Ram Pai06bb53b2018-01-18 17:50:31 -08001224
1225 thread_pkey_regs_restore(new_thread, old_thread);
Anton Blanchard152d5232015-10-29 11:43:55 +11001226}
1227
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001228struct task_struct *__switch_to(struct task_struct *prev,
1229 struct task_struct *new)
1230{
1231 struct thread_struct *new_thread, *old_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001232 struct task_struct *last;
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001233#ifdef CONFIG_PPC_BOOK3S_64
1234 struct ppc64_tlb_batch *batch;
1235#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001236
Anton Blanchard152d5232015-10-29 11:43:55 +11001237 new_thread = &new->thread;
1238 old_thread = &current->thread;
1239
Michael Neuling7ba5fef2013-10-02 17:15:14 +10001240 WARN_ON(!irqs_disabled());
1241
Michael Ellerman4e003742017-10-19 15:08:43 +11001242#ifdef CONFIG_PPC_BOOK3S_64
Christoph Lameter69111ba2014-10-21 15:23:25 -05001243 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001244 if (batch->active) {
1245 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1246 if (batch->index)
1247 __flush_tlb_pending(batch);
1248 batch->active = 0;
1249 }
Michael Ellerman4e003742017-10-19 15:08:43 +11001250#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001251
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001252#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1253 switch_booke_debug_regs(&new->thread.debug);
1254#else
1255/*
1256 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1257 * schedule DABR
1258 */
1259#ifndef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria303e6a92020-05-14 16:47:34 +05301260 switch_hw_breakpoint(new);
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001261#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1262#endif
1263
1264 /*
1265 * We need to save SPRs before treclaim/trecheckpoint as these will
1266 * change a number of them.
1267 */
1268 save_sprs(&prev->thread);
1269
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001270 /* Save FPU, Altivec, VSX and SPE state */
1271 giveup_all(prev);
1272
Cyril Burdc310662016-09-23 16:18:24 +10001273 __switch_to_tm(prev, new);
1274
Nicholas Piggine4c0fc52017-06-09 01:36:06 +10001275 if (!radix_enabled()) {
1276 /*
1277 * We can't take a PMU exception inside _switch() since there
1278 * is a window where the kernel stack SLB and the kernel stack
1279 * are out of sync. Hard disable here.
1280 */
1281 hard_irq_disable();
1282 }
Michael Neulingbc2a9402013-02-13 16:21:40 +00001283
Anton Blanchard20dbe672015-12-10 20:44:39 +11001284 /*
1285 * Call restore_sprs() before calling _switch(). If we move it after
1286 * _switch() then we miss out on calling it for new tasks. The reason
1287 * for this is we manually create a stack frame for new tasks that
1288 * directly returns through ret_from_fork() or
1289 * ret_from_kernel_thread(). See copy_thread() for details.
1290 */
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001291 restore_sprs(old_thread, new_thread);
1292
Anton Blanchard20dbe672015-12-10 20:44:39 +11001293 last = _switch(old_thread, new_thread);
1294
Michael Ellerman4e003742017-10-19 15:08:43 +11001295#ifdef CONFIG_PPC_BOOK3S_64
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001296 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1297 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001298 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001299 batch->active = 1;
1300 }
Cyril Bur70fe3d92016-02-29 17:53:47 +11001301
Christophe Leroy05b98792019-01-17 23:25:12 +11001302 if (current->thread.regs) {
1303 restore_math(current->thread.regs);
Nicholas Piggin07d2a622017-06-09 01:36:09 +10001304
1305 /*
Nicholas Piggindc462262020-08-25 17:55:35 +10001306 * On POWER9 the copy-paste buffer can only paste into
1307 * foreign real addresses, so unprivileged processes can not
1308 * see the data or use it in any way unless they have
1309 * foreign real mappings. If the new process has the foreign
1310 * real address mappings, we must issue a cp_abort to clear
1311 * any state and prevent snooping, corruption or a covert
1312 * channel. ISA v3.1 supports paste into local memory.
Nicholas Piggin07d2a622017-06-09 01:36:09 +10001313 */
Haren Mynenic4206442020-04-15 23:08:11 -07001314 if (current->mm &&
Nicholas Piggindc462262020-08-25 17:55:35 +10001315 (cpu_has_feature(CPU_FTR_ARCH_31) ||
1316 atomic_read(&current->mm->context.vas_windows)))
Sukadev Bhattiprolu9d2a4d72017-11-07 18:23:54 -08001317 asm volatile(PPC_CP_ABORT);
Nicholas Piggin07d2a622017-06-09 01:36:09 +10001318 }
Michael Ellerman4e003742017-10-19 15:08:43 +11001319#endif /* CONFIG_PPC_BOOK3S_64 */
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001320
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001321 return last;
1322}
1323
Christophe Leroydf131022018-10-06 16:51:16 +00001324#define NR_INSN_TO_PRINT 16
Paul Mackerras06d67d52005-10-10 22:29:05 +10001325
Paul Mackerras06d67d52005-10-10 22:29:05 +10001326static void show_instructions(struct pt_regs *regs)
1327{
1328 int i;
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301329 unsigned long nip = regs->nip;
Christophe Leroydf131022018-10-06 16:51:16 +00001330 unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
Paul Mackerras06d67d52005-10-10 22:29:05 +10001331
1332 printk("Instruction dump:");
1333
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301334 /*
1335 * If we were executing with the MMU off for instructions, adjust pc
1336 * rather than printing XXXXXXXX.
1337 */
1338 if (!IS_ENABLED(CONFIG_BOOKE) && !(regs->msr & MSR_IR)) {
1339 pc = (unsigned long)phys_to_virt(pc);
1340 nip = (unsigned long)phys_to_virt(regs->nip);
1341 }
1342
Christophe Leroydf131022018-10-06 16:51:16 +00001343 for (i = 0; i < NR_INSN_TO_PRINT; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001344 int instr;
1345
1346 if (!(i % 8))
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001347 pr_cont("\n");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001348
Anton Blanchard00ae36d2006-10-13 12:17:16 +10001349 if (!__kernel_text_address(pc) ||
Christoph Hellwig25f12ae2020-06-17 09:37:55 +02001350 get_kernel_nofault(instr, (const void *)pc)) {
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001351 pr_cont("XXXXXXXX ");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001352 } else {
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301353 if (nip == pc)
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001354 pr_cont("<%08x> ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001355 else
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001356 pr_cont("%08x ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001357 }
1358
1359 pc += sizeof(int);
1360 }
1361
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001362 pr_cont("\n");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001363}
1364
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001365void show_user_instructions(struct pt_regs *regs)
1366{
1367 unsigned long pc;
Christophe Leroydf131022018-10-06 16:51:16 +00001368 int n = NR_INSN_TO_PRINT;
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001369 struct seq_buf s;
1370 char buf[96]; /* enough for 8 times 9 + 2 chars */
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001371
Christophe Leroydf131022018-10-06 16:51:16 +00001372 pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001373
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001374 seq_buf_init(&s, buf, sizeof(buf));
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001375
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001376 while (n) {
1377 int i;
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001378
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001379 seq_buf_clear(&s);
1380
1381 for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) {
1382 int instr;
1383
Christoph Hellwigc0ee37e2020-06-17 09:37:54 +02001384 if (copy_from_user_nofault(&instr, (void __user *)pc,
1385 sizeof(instr))) {
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001386 seq_buf_printf(&s, "XXXXXXXX ");
1387 continue;
1388 }
1389 seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr);
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001390 }
1391
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001392 if (!seq_buf_has_overflowed(&s))
1393 pr_info("%s[%d]: code: %s\n", current->comm,
1394 current->pid, s.buffer);
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001395 }
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001396}
1397
Michael Neuling801c0b22015-11-20 15:15:32 +11001398struct regbit {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001399 unsigned long bit;
1400 const char *name;
Michael Neuling801c0b22015-11-20 15:15:32 +11001401};
1402
1403static struct regbit msr_bits[] = {
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001404#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
1405 {MSR_SF, "SF"},
1406 {MSR_HV, "HV"},
1407#endif
1408 {MSR_VEC, "VEC"},
1409 {MSR_VSX, "VSX"},
1410#ifdef CONFIG_BOOKE
1411 {MSR_CE, "CE"},
1412#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001413 {MSR_EE, "EE"},
1414 {MSR_PR, "PR"},
1415 {MSR_FP, "FP"},
1416 {MSR_ME, "ME"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001417#ifdef CONFIG_BOOKE
Kumar Gala1b983262008-11-19 04:39:53 +00001418 {MSR_DE, "DE"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001419#else
1420 {MSR_SE, "SE"},
1421 {MSR_BE, "BE"},
1422#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001423 {MSR_IR, "IR"},
1424 {MSR_DR, "DR"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001425 {MSR_PMM, "PMM"},
1426#ifndef CONFIG_BOOKE
1427 {MSR_RI, "RI"},
1428 {MSR_LE, "LE"},
1429#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001430 {0, NULL}
1431};
1432
Michael Neuling801c0b22015-11-20 15:15:32 +11001433static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
Paul Mackerras06d67d52005-10-10 22:29:05 +10001434{
Michael Neuling801c0b22015-11-20 15:15:32 +11001435 const char *s = "";
Paul Mackerras06d67d52005-10-10 22:29:05 +10001436
Paul Mackerras06d67d52005-10-10 22:29:05 +10001437 for (; bits->bit; ++bits)
1438 if (val & bits->bit) {
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001439 pr_cont("%s%s", s, bits->name);
Michael Neuling801c0b22015-11-20 15:15:32 +11001440 s = sep;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001441 }
Michael Neuling801c0b22015-11-20 15:15:32 +11001442}
1443
1444#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1445static struct regbit msr_tm_bits[] = {
1446 {MSR_TS_T, "T"},
1447 {MSR_TS_S, "S"},
1448 {MSR_TM, "E"},
1449 {0, NULL}
1450};
1451
1452static void print_tm_bits(unsigned long val)
1453{
1454/*
1455 * This only prints something if at least one of the TM bit is set.
1456 * Inside the TM[], the output means:
1457 * E: Enabled (bit 32)
1458 * S: Suspended (bit 33)
1459 * T: Transactional (bit 34)
1460 */
1461 if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001462 pr_cont(",TM[");
Michael Neuling801c0b22015-11-20 15:15:32 +11001463 print_bits(val, msr_tm_bits, "");
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001464 pr_cont("]");
Michael Neuling801c0b22015-11-20 15:15:32 +11001465 }
1466}
1467#else
1468static void print_tm_bits(unsigned long val) {}
1469#endif
1470
1471static void print_msr_bits(unsigned long val)
1472{
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001473 pr_cont("<");
Michael Neuling801c0b22015-11-20 15:15:32 +11001474 print_bits(val, msr_bits, ",");
1475 print_tm_bits(val);
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001476 pr_cont(">");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001477}
1478
1479#ifdef CONFIG_PPC64
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001480#define REG "%016lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001481#define REGS_PER_LINE 4
1482#define LAST_VOLATILE 13
1483#else
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001484#define REG "%08lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001485#define REGS_PER_LINE 8
1486#define LAST_VOLATILE 12
1487#endif
1488
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001489void show_regs(struct pt_regs * regs)
1490{
1491 int i, trap;
1492
Tejun Heoa43cb952013-04-30 15:27:17 -07001493 show_regs_print_info(KERN_DEFAULT);
1494
Michael Ellermana6036102017-08-23 23:56:24 +10001495 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
Paul Mackerras06d67d52005-10-10 22:29:05 +10001496 regs->nip, regs->link, regs->ctr);
Michael Ellerman182dc9c2017-12-18 16:33:36 +11001497 printk("REGS: %px TRAP: %04lx %s (%s)\n",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001498 regs, regs->trap, print_tainted(), init_utsname()->release);
Michael Ellermana6036102017-08-23 23:56:24 +10001499 printk("MSR: "REG" ", regs->msr);
Michael Neuling801c0b22015-11-20 15:15:32 +11001500 print_msr_bits(regs->msr);
Michael Ellermanf6fc73f2017-08-23 23:56:23 +10001501 pr_cont(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001502 trap = TRAP(regs);
Nicholas Piggin912237e2020-05-07 22:13:31 +10001503 if (!trap_is_syscall(regs) && cpu_has_feature(CPU_FTR_CFAR))
Michael Ellerman7dae8652016-11-03 20:45:26 +11001504 pr_cont("CFAR: "REG" ", regs->orig_gpr3);
Christophe Leroy2ec42992020-08-17 05:46:43 +00001505 if (trap == 0x200 || trap == 0x300 || trap == 0x600) {
1506 if (IS_ENABLED(CONFIG_4xx) || IS_ENABLED(CONFIG_BOOKE))
1507 pr_cont("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
1508 else
1509 pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1510 }
1511
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001512#ifdef CONFIG_PPC64
Nicholas Piggin3130a7b2018-05-10 11:04:24 +10001513 pr_cont("IRQMASK: %lx ", regs->softe);
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001514#endif
1515#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchard6d888d12013-11-18 13:19:17 +11001516 if (MSR_TM_ACTIVE(regs->msr))
Michael Ellerman7dae8652016-11-03 20:45:26 +11001517 pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
Kumar Gala14170782007-07-26 00:46:15 -05001518#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001519
1520 for (i = 0; i < 32; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001521 if ((i % REGS_PER_LINE) == 0)
Michael Ellerman7dae8652016-11-03 20:45:26 +11001522 pr_cont("\nGPR%02d: ", i);
1523 pr_cont(REG " ", regs->gpr[i]);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001524 if (i == LAST_VOLATILE && !FULL_REGS(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001525 break;
1526 }
Michael Ellerman7dae8652016-11-03 20:45:26 +11001527 pr_cont("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001528 /*
1529 * Lookup NIP late so we have the best change of getting the
1530 * above info out without failing
1531 */
Christophe Leroy8f020c72020-08-17 05:46:44 +00001532 if (IS_ENABLED(CONFIG_KALLSYMS)) {
1533 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1534 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
1535 }
Dmitry Safonov9cb8f062020-06-08 21:32:29 -07001536 show_stack(current, (unsigned long *) regs->gpr[1], KERN_DEFAULT);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001537 if (!user_mode(regs))
1538 show_instructions(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001539}
1540
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001541void flush_thread(void)
1542{
K.Prasade0780b72011-02-10 04:44:35 +00001543#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad5aae8a52010-06-15 11:35:19 +05301544 flush_ptrace_hw_breakpoint(current);
K.Prasade0780b72011-02-10 04:44:35 +00001545#else /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001546 set_debug_reg_defaults(&current->thread);
K.Prasade0780b72011-02-10 04:44:35 +00001547#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001548}
1549
Nicholas Piggin425d3312018-09-15 01:30:55 +10001550#ifdef CONFIG_PPC_BOOK3S_64
1551void arch_setup_new_exec(void)
1552{
1553 if (radix_enabled())
1554 return;
1555 hash__setup_new_exec();
1556}
1557#endif
1558
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001559#ifdef CONFIG_PPC64
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001560/**
1561 * Assign a TIDR (thread ID) for task @t and set it in the thread
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001562 * structure. For now, we only support setting TIDR for 'current' task.
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001563 *
1564 * Since the TID value is a truncated form of it PID, it is possible
1565 * (but unlikely) for 2 threads to have the same TID. In the unlikely event
1566 * that 2 threads share the same TID and are waiting, one of the following
1567 * cases will happen:
1568 *
1569 * 1. The correct thread is running, the wrong thread is not
1570 * In this situation, the correct thread is woken and proceeds to pass it's
1571 * condition check.
1572 *
1573 * 2. Neither threads are running
1574 * In this situation, neither thread will be woken. When scheduled, the waiting
1575 * threads will execute either a wait, which will return immediately, followed
1576 * by a condition check, which will pass for the correct thread and fail
1577 * for the wrong thread, or they will execute the condition check immediately.
1578 *
1579 * 3. The wrong thread is running, the correct thread is not
1580 * The wrong thread will be woken, but will fail it's condition check and
1581 * re-execute wait. The correct thread, when scheduled, will execute either
1582 * it's condition check (which will pass), or wait, which returns immediately
1583 * when called the first time after the thread is scheduled, followed by it's
1584 * condition check (which will pass).
1585 *
1586 * 4. Both threads are running
1587 * Both threads will be woken. The wrong thread will fail it's condition check
1588 * and execute another wait, while the correct thread will pass it's condition
1589 * check.
1590 *
1591 * @t: the task to set the thread ID for
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001592 */
1593int set_thread_tidr(struct task_struct *t)
1594{
Alastair D'Silva3449f192018-05-11 16:12:58 +10001595 if (!cpu_has_feature(CPU_FTR_P9_TIDR))
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001596 return -EINVAL;
1597
1598 if (t != current)
1599 return -EINVAL;
1600
Vaibhav Jain7e4d4232017-11-24 14:03:38 +05301601 if (t->thread.tidr)
1602 return 0;
1603
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001604 t->thread.tidr = (u16)task_pid_nr(t);
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001605 mtspr(SPRN_TIDR, t->thread.tidr);
1606
1607 return 0;
1608}
Christophe Lombardb1db5512018-01-11 09:55:25 +01001609EXPORT_SYMBOL_GPL(set_thread_tidr);
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001610
1611#endif /* CONFIG_PPC64 */
1612
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001613void
1614release_thread(struct task_struct *t)
1615{
1616}
1617
1618/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001619 * this gets called so that we can store coprocessor state into memory and
1620 * copy the current task into the new thread.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001621 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001622int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001623{
Anton Blanchard579e6332015-10-29 11:44:09 +11001624 flush_all_to_thread(src);
Michael Neuling621b5062014-03-03 14:21:40 +11001625 /*
1626 * Flush TM state out so we can copy it. __switch_to_tm() does this
1627 * flush but it removes the checkpointed state from the current CPU and
1628 * transitions the CPU out of TM mode. Hence we need to call
1629 * tm_recheckpoint_new_task() (on the same task) to restore the
1630 * checkpointed state back and the TM mode.
Cyril Bur5d176f72016-09-14 18:02:16 +10001631 *
1632 * Can't pass dst because it isn't ready. Doesn't matter, passing
1633 * dst is only important for __switch_to()
Michael Neuling621b5062014-03-03 14:21:40 +11001634 */
Cyril Burdc310662016-09-23 16:18:24 +10001635 __switch_to_tm(src, src);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001636
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001637 *dst = *src;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001638
1639 clear_task_ebb(dst);
1640
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001641 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001642}
1643
Michael Ellermancec15482014-07-10 12:29:21 +10001644static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1645{
Michael Ellerman4e003742017-10-19 15:08:43 +11001646#ifdef CONFIG_PPC_BOOK3S_64
Michael Ellermancec15482014-07-10 12:29:21 +10001647 unsigned long sp_vsid;
1648 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1649
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001650 if (radix_enabled())
1651 return;
1652
Michael Ellermancec15482014-07-10 12:29:21 +10001653 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1654 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1655 << SLB_VSID_SHIFT_1T;
1656 else
1657 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1658 << SLB_VSID_SHIFT;
1659 sp_vsid |= SLB_VSID_KERNEL | llp;
1660 p->thread.ksp_vsid = sp_vsid;
1661#endif
1662}
1663
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001664/*
1665 * Copy a thread..
1666 */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001667
Alex Dowad6eca8932015-03-13 20:14:46 +02001668/*
1669 * Copy architecture-specific thread state
1670 */
Christian Brauner714acdb2020-06-11 11:04:15 +02001671int copy_thread(unsigned long clone_flags, unsigned long usp,
Nicholas Pigginfacd04a2019-08-27 13:30:06 +10001672 unsigned long kthread_arg, struct task_struct *p,
1673 unsigned long tls)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001674{
1675 struct pt_regs *childregs, *kregs;
1676 extern void ret_from_fork(void);
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001677 extern void ret_from_fork_scv(void);
Al Viro58254e12012-09-12 18:32:42 -04001678 extern void ret_from_kernel_thread(void);
1679 void (*f)(void);
Al Viro0cec6fd2006-01-12 01:06:02 -08001680 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Michael Ellerman5d31a962016-03-24 22:04:04 +11001681 struct thread_info *ti = task_thread_info(p);
Ravi Bangoria6b424ef2020-05-14 16:47:35 +05301682#ifdef CONFIG_HAVE_HW_BREAKPOINT
1683 int i;
1684#endif
Michael Ellerman5d31a962016-03-24 22:04:04 +11001685
Christophe Leroyed1cd6d2019-01-31 10:08:58 +00001686 klp_init_thread_info(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001687
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001688 /* Copy registers */
1689 sp -= sizeof(struct pt_regs);
1690 childregs = (struct pt_regs *) sp;
Al Viroab758192012-10-21 22:33:39 -04001691 if (unlikely(p->flags & PF_KTHREAD)) {
Alex Dowad6eca8932015-03-13 20:14:46 +02001692 /* kernel thread */
Al Viro58254e12012-09-12 18:32:42 -04001693 memset(childregs, 0, sizeof(struct pt_regs));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001694 childregs->gpr[1] = sp + sizeof(struct pt_regs);
Anton Blanchard7cedd602014-02-04 16:08:51 +11001695 /* function */
1696 if (usp)
1697 childregs->gpr[14] = ppc_function_entry((void *)usp);
Al Viro58254e12012-09-12 18:32:42 -04001698#ifdef CONFIG_PPC64
Al Virob5e2fc12006-01-12 01:06:01 -08001699 clear_tsk_thread_flag(p, TIF_32BIT);
Madhavan Srinivasanc2e480b2017-12-20 09:25:42 +05301700 childregs->softe = IRQS_ENABLED;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001701#endif
Alex Dowad6eca8932015-03-13 20:14:46 +02001702 childregs->gpr[15] = kthread_arg;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001703 p->thread.regs = NULL; /* no user register state */
Al Viro138d1ce2012-10-11 08:41:43 -04001704 ti->flags |= _TIF_RESTOREALL;
Al Viro58254e12012-09-12 18:32:42 -04001705 f = ret_from_kernel_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001706 } else {
Alex Dowad6eca8932015-03-13 20:14:46 +02001707 /* user thread */
Al Viroafa86fc2012-10-22 22:51:14 -04001708 struct pt_regs *regs = current_pt_regs();
Al Viro58254e12012-09-12 18:32:42 -04001709 CHECK_FULL_REGS(regs);
1710 *childregs = *regs;
Al Viroea516b12012-10-21 22:28:43 -04001711 if (usp)
1712 childregs->gpr[1] = usp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001713 p->thread.regs = childregs;
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001714 /* 64s sets this in ret_from_fork */
1715 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
1716 childregs->gpr[3] = 0; /* Result from fork() */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001717 if (clone_flags & CLONE_SETTLS) {
Denis Kirjanov9904b002010-07-29 22:04:39 +00001718 if (!is_32bit_task())
Nicholas Pigginfacd04a2019-08-27 13:30:06 +10001719 childregs->gpr[13] = tls;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001720 else
Nicholas Pigginfacd04a2019-08-27 13:30:06 +10001721 childregs->gpr[2] = tls;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001722 }
Al Viro58254e12012-09-12 18:32:42 -04001723
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001724 if (trap_is_scv(regs))
1725 f = ret_from_fork_scv;
1726 else
1727 f = ret_from_fork;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001728 }
Cyril Burd272f662016-02-29 17:53:46 +11001729 childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001730 sp -= STACK_FRAME_OVERHEAD;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001731
1732 /*
1733 * The way this works is that at some point in the future
1734 * some task will call _switch to switch to the new task.
1735 * That will pop off the stack frame created below and start
1736 * the new task running at ret_from_fork. The new task will
1737 * do some house keeping and then return from the fork or clone
1738 * system call, using the stack frame created above.
1739 */
Li Zhongaf945cf2013-05-06 22:44:41 +00001740 ((unsigned long *)sp)[0] = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001741 sp -= sizeof(struct pt_regs);
1742 kregs = (struct pt_regs *) sp;
1743 sp -= STACK_FRAME_OVERHEAD;
1744 p->thread.ksp = sp;
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001745#ifdef CONFIG_PPC32
Christophe Leroya7916a12019-01-31 10:09:00 +00001746 p->thread.ksp_limit = (unsigned long)end_of_stack(p);
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001747#endif
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001748#ifdef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria6b424ef2020-05-14 16:47:35 +05301749 for (i = 0; i < nr_wp_slots(); i++)
1750 p->thread.ptrace_bps[i] = NULL;
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001751#endif
1752
Paul Mackerras18461962013-09-10 20:21:10 +10001753 p->thread.fp_save_area = NULL;
1754#ifdef CONFIG_ALTIVEC
1755 p->thread.vr_save_area = NULL;
1756#endif
1757
Michael Ellermancec15482014-07-10 12:29:21 +10001758 setup_ksp_vsid(p, sp);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001759
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001760#ifdef CONFIG_PPC64
1761 if (cpu_has_feature(CPU_FTR_DSCR)) {
Anton Blanchard1021cb22012-09-03 16:49:47 +00001762 p->thread.dscr_inherit = current->thread.dscr_inherit;
Anton Blancharddb1231dc2015-12-09 20:11:47 +11001763 p->thread.dscr = mfspr(SPRN_DSCR);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001764 }
Haren Myneni92779242012-12-06 21:49:56 +00001765 if (cpu_has_feature(CPU_FTR_HAS_PPR))
Nicholas Piggin4c2de742018-10-13 00:15:16 +11001766 childregs->ppr = DEFAULT_PPR;
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001767
1768 p->thread.tidr = 0;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001769#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +11001770 kregs->nip = ppc_function_entry(f);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001771 return 0;
1772}
1773
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001774void preload_new_slb_context(unsigned long start, unsigned long sp);
1775
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001776/*
1777 * Set up a thread for executing a new program
1778 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001779void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001780{
Michael Ellerman90eac722005-10-21 16:01:33 +10001781#ifdef CONFIG_PPC64
1782 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001783
Christophe Leroybfac2792020-08-17 05:46:42 +00001784 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled())
Aneesh Kumar K.Vf89bd8b2019-04-09 09:33:28 +05301785 preload_new_slb_context(start, sp);
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001786#endif
Michael Ellerman90eac722005-10-21 16:01:33 +10001787
Paul Mackerras06d67d52005-10-10 22:29:05 +10001788 /*
1789 * If we exec out of a kernel thread then thread.regs will not be
1790 * set. Do it now.
1791 */
1792 if (!current->thread.regs) {
Al Viro0cec6fd2006-01-12 01:06:02 -08001793 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1794 current->thread.regs = regs - 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001795 }
1796
Cyril Bur8e96a872016-06-17 14:58:34 +10001797#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1798 /*
1799 * Clear any transactional state, we're exec()ing. The cause is
1800 * not important as there will never be a recheckpoint so it's not
1801 * user visible.
1802 */
1803 if (MSR_TM_SUSPENDED(mfmsr()))
1804 tm_reclaim_current(0);
1805#endif
1806
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001807 memset(regs->gpr, 0, sizeof(regs->gpr));
1808 regs->ctr = 0;
1809 regs->link = 0;
1810 regs->xer = 0;
1811 regs->ccr = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001812 regs->gpr[1] = sp;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001813
Roland McGrath474f8192007-09-24 16:52:44 -07001814 /*
1815 * We have just cleared all the nonvolatile GPRs, so make
1816 * FULL_REGS(regs) return true. This is necessary to allow
1817 * ptrace to examine the thread immediately after exec.
1818 */
Nicholas Pigginfeb9df32020-05-07 22:13:29 +10001819 SET_FULL_REGS(regs);
Roland McGrath474f8192007-09-24 16:52:44 -07001820
Paul Mackerras06d67d52005-10-10 22:29:05 +10001821#ifdef CONFIG_PPC32
1822 regs->mq = 0;
1823 regs->nip = start;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001824 regs->msr = MSR_USER;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001825#else
Denis Kirjanov9904b002010-07-29 22:04:39 +00001826 if (!is_32bit_task()) {
Rusty Russell94af3ab2013-11-20 22:15:02 +11001827 unsigned long entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001828
Rusty Russell94af3ab2013-11-20 22:15:02 +11001829 if (is_elf2_task()) {
1830 /* Look ma, no function descriptors! */
1831 entry = start;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001832
Rusty Russell94af3ab2013-11-20 22:15:02 +11001833 /*
1834 * Ulrich says:
1835 * The latest iteration of the ABI requires that when
1836 * calling a function (at its global entry point),
1837 * the caller must ensure r12 holds the entry point
1838 * address (so that the function can quickly
1839 * establish addressability).
1840 */
1841 regs->gpr[12] = start;
1842 /* Make sure that's restored on entry to userspace. */
1843 set_thread_flag(TIF_RESTOREALL);
1844 } else {
1845 unsigned long toc;
1846
1847 /* start is a relocated pointer to the function
1848 * descriptor for the elf _start routine. The first
1849 * entry in the function descriptor is the entry
1850 * address of _start and the second entry is the TOC
1851 * value we need to use.
1852 */
1853 __get_user(entry, (unsigned long __user *)start);
1854 __get_user(toc, (unsigned long __user *)start+1);
1855
1856 /* Check whether the e_entry function descriptor entries
1857 * need to be relocated before we can use them.
1858 */
1859 if (load_addr != 0) {
1860 entry += load_addr;
1861 toc += load_addr;
1862 }
1863 regs->gpr[2] = toc;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001864 }
1865 regs->nip = entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001866 regs->msr = MSR_USER64;
Stephen Rothwelld4bf9a72005-10-13 13:40:54 +10001867 } else {
1868 regs->nip = start;
1869 regs->gpr[2] = 0;
1870 regs->msr = MSR_USER32;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001871 }
1872#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001873#ifdef CONFIG_VSX
1874 current->thread.used_vsr = 0;
1875#endif
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001876 current->thread.load_slb = 0;
Breno Leitao11958922017-06-02 18:43:30 -03001877 current->thread.load_fp = 0;
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001878 memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
Paul Mackerras18461962013-09-10 20:21:10 +10001879 current->thread.fp_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001880#ifdef CONFIG_ALTIVEC
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001881 memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1882 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
Paul Mackerras18461962013-09-10 20:21:10 +10001883 current->thread.vr_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001884 current->thread.vrsave = 0;
1885 current->thread.used_vr = 0;
Breno Leitao11958922017-06-02 18:43:30 -03001886 current->thread.load_vec = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001887#endif /* CONFIG_ALTIVEC */
1888#ifdef CONFIG_SPE
1889 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1890 current->thread.acc = 0;
1891 current->thread.spefscr = 0;
1892 current->thread.used_spe = 0;
1893#endif /* CONFIG_SPE */
Michael Neulingbc2a9402013-02-13 16:21:40 +00001894#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Michael Neulingbc2a9402013-02-13 16:21:40 +00001895 current->thread.tm_tfhar = 0;
1896 current->thread.tm_texasr = 0;
1897 current->thread.tm_tfiar = 0;
Breno Leitao7f22ced2017-06-05 11:40:59 -03001898 current->thread.load_tm = 0;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001899#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Ram Pai06bb53b2018-01-18 17:50:31 -08001900
1901 thread_pkey_regs_init(&current->thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001902}
Anton Blancharde1802b02014-08-20 08:00:02 +10001903EXPORT_SYMBOL(start_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001904
1905#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1906 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1907
1908int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1909{
1910 struct pt_regs *regs = tsk->thread.regs;
1911
1912 /* This is a bit hairy. If we are an SPE enabled processor
1913 * (have embedded fp) we store the IEEE exception enable flags in
1914 * fpexc_mode. fpexc_mode is also used for setting FP exception
1915 * mode (asyn, precise, disabled) for 'Classic' FP. */
1916 if (val & PR_FP_EXC_SW_ENABLE) {
1917#ifdef CONFIG_SPE
Kumar Gala5e14d212007-09-13 01:44:20 -05001918 if (cpu_has_feature(CPU_FTR_SPE)) {
Joseph Myers640e9222013-12-10 23:07:45 +00001919 /*
1920 * When the sticky exception bits are set
1921 * directly by userspace, it must call prctl
1922 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1923 * in the existing prctl settings) or
1924 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1925 * the bits being set). <fenv.h> functions
1926 * saving and restoring the whole
1927 * floating-point environment need to do so
1928 * anyway to restore the prctl settings from
1929 * the saved environment.
1930 */
1931 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001932 tsk->thread.fpexc_mode = val &
1933 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1934 return 0;
1935 } else {
1936 return -EINVAL;
1937 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001938#else
1939 return -EINVAL;
1940#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001941 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001942
1943 /* on a CONFIG_SPE this does not hurt us. The bits that
1944 * __pack_fe01 use do not overlap with bits used for
1945 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1946 * on CONFIG_SPE implementations are reserved so writing to
1947 * them does not change anything */
1948 if (val > PR_FP_EXC_PRECISE)
1949 return -EINVAL;
1950 tsk->thread.fpexc_mode = __pack_fe01(val);
1951 if (regs != NULL && (regs->msr & MSR_FP) != 0)
1952 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1953 | tsk->thread.fpexc_mode;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001954 return 0;
1955}
1956
1957int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1958{
1959 unsigned int val;
1960
1961 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
1962#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00001963 if (cpu_has_feature(CPU_FTR_SPE)) {
1964 /*
1965 * When the sticky exception bits are set
1966 * directly by userspace, it must call prctl
1967 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1968 * in the existing prctl settings) or
1969 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1970 * the bits being set). <fenv.h> functions
1971 * saving and restoring the whole
1972 * floating-point environment need to do so
1973 * anyway to restore the prctl settings from
1974 * the saved environment.
1975 */
1976 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001977 val = tsk->thread.fpexc_mode;
Joseph Myers640e9222013-12-10 23:07:45 +00001978 } else
Kumar Gala5e14d212007-09-13 01:44:20 -05001979 return -EINVAL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001980#else
1981 return -EINVAL;
1982#endif
1983 else
1984 val = __unpack_fe01(tsk->thread.fpexc_mode);
1985 return put_user(val, (unsigned int __user *) adr);
1986}
1987
Paul Mackerrasfab5db92006-06-07 16:14:40 +10001988int set_endian(struct task_struct *tsk, unsigned int val)
1989{
1990 struct pt_regs *regs = tsk->thread.regs;
1991
1992 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1993 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1994 return -EINVAL;
1995
1996 if (regs == NULL)
1997 return -EINVAL;
1998
1999 if (val == PR_ENDIAN_BIG)
2000 regs->msr &= ~MSR_LE;
2001 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
2002 regs->msr |= MSR_LE;
2003 else
2004 return -EINVAL;
2005
2006 return 0;
2007}
2008
2009int get_endian(struct task_struct *tsk, unsigned long adr)
2010{
2011 struct pt_regs *regs = tsk->thread.regs;
2012 unsigned int val;
2013
2014 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
2015 !cpu_has_feature(CPU_FTR_REAL_LE))
2016 return -EINVAL;
2017
2018 if (regs == NULL)
2019 return -EINVAL;
2020
2021 if (regs->msr & MSR_LE) {
2022 if (cpu_has_feature(CPU_FTR_REAL_LE))
2023 val = PR_ENDIAN_LITTLE;
2024 else
2025 val = PR_ENDIAN_PPC_LITTLE;
2026 } else
2027 val = PR_ENDIAN_BIG;
2028
2029 return put_user(val, (unsigned int __user *)adr);
2030}
2031
Paul Mackerrase9370ae2006-06-07 16:15:39 +10002032int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
2033{
2034 tsk->thread.align_ctl = val;
2035 return 0;
2036}
2037
2038int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
2039{
2040 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
2041}
2042
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002043static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
2044 unsigned long nbytes)
2045{
2046 unsigned long stack_page;
2047 unsigned long cpu = task_cpu(p);
2048
Christophe Leroya7916a12019-01-31 10:09:00 +00002049 stack_page = (unsigned long)hardirq_ctx[cpu];
2050 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2051 return 1;
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002052
Christophe Leroya7916a12019-01-31 10:09:00 +00002053 stack_page = (unsigned long)softirq_ctx[cpu];
2054 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2055 return 1;
2056
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002057 return 0;
2058}
2059
Nicholas Piggina2e36682020-03-25 20:41:44 +10002060static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p,
2061 unsigned long nbytes)
2062{
2063#ifdef CONFIG_PPC64
2064 unsigned long stack_page;
2065 unsigned long cpu = task_cpu(p);
2066
2067 stack_page = (unsigned long)paca_ptrs[cpu]->emergency_sp - THREAD_SIZE;
2068 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2069 return 1;
2070
2071# ifdef CONFIG_PPC_BOOK3S_64
2072 stack_page = (unsigned long)paca_ptrs[cpu]->nmi_emergency_sp - THREAD_SIZE;
2073 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2074 return 1;
2075
2076 stack_page = (unsigned long)paca_ptrs[cpu]->mc_emergency_sp - THREAD_SIZE;
2077 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2078 return 1;
2079# endif
2080#endif
2081
2082 return 0;
2083}
2084
2085
Anton Blanchard2f251942006-03-27 11:46:18 +11002086int validate_sp(unsigned long sp, struct task_struct *p,
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002087 unsigned long nbytes)
2088{
Al Viro0cec6fd2006-01-12 01:06:02 -08002089 unsigned long stack_page = (unsigned long)task_stack_page(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002090
Christophe Leroya7916a12019-01-31 10:09:00 +00002091 if (sp < THREAD_SIZE)
2092 return 0;
2093
2094 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002095 return 1;
2096
Nicholas Piggina2e36682020-03-25 20:41:44 +10002097 if (valid_irq_stack(sp, p, nbytes))
2098 return 1;
2099
2100 return valid_emergency_stack(sp, p, nbytes);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002101}
2102
Anton Blanchard2f251942006-03-27 11:46:18 +11002103EXPORT_SYMBOL(validate_sp);
2104
Christophe Leroy018cce32019-01-31 10:08:52 +00002105static unsigned long __get_wchan(struct task_struct *p)
Paul Mackerras06d67d52005-10-10 22:29:05 +10002106{
2107 unsigned long ip, sp;
2108 int count = 0;
2109
2110 if (!p || p == current || p->state == TASK_RUNNING)
2111 return 0;
2112
2113 sp = p->thread.ksp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002114 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10002115 return 0;
2116
2117 do {
2118 sp = *(unsigned long *)sp;
Kautuk Consul4ca360f2016-04-19 15:48:21 +05302119 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD) ||
2120 p->state == TASK_RUNNING)
Paul Mackerras06d67d52005-10-10 22:29:05 +10002121 return 0;
2122 if (count > 0) {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002123 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10002124 if (!in_sched_functions(ip))
2125 return ip;
2126 }
2127 } while (count++ < 16);
2128 return 0;
2129}
Paul Mackerras06d67d52005-10-10 22:29:05 +10002130
Christophe Leroy018cce32019-01-31 10:08:52 +00002131unsigned long get_wchan(struct task_struct *p)
2132{
2133 unsigned long ret;
2134
2135 if (!try_get_task_stack(p))
2136 return 0;
2137
2138 ret = __get_wchan(p);
2139
2140 put_task_stack(p);
2141
2142 return ret;
2143}
2144
Johannes Bergc4d04be2008-11-20 03:24:07 +00002145static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002146
Dmitry Safonov9cb8f062020-06-08 21:32:29 -07002147void show_stack(struct task_struct *tsk, unsigned long *stack,
2148 const char *loglvl)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002149{
Paul Mackerras06d67d52005-10-10 22:29:05 +10002150 unsigned long sp, ip, lr, newsp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002151 int count = 0;
Paul Mackerras06d67d52005-10-10 22:29:05 +10002152 int firstframe = 1;
Naveen N. Rao7c1bb6b2019-09-05 23:50:30 +05302153 unsigned long ret_addr;
2154 int ftrace_idx = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002155
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002156 if (tsk == NULL)
2157 tsk = current;
Christophe Leroy018cce32019-01-31 10:08:52 +00002158
2159 if (!try_get_task_stack(tsk))
2160 return;
2161
2162 sp = (unsigned long) stack;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002163 if (sp == 0) {
2164 if (tsk == current)
Michael Ellerman3d13e832020-02-20 22:51:37 +11002165 sp = current_stack_frame();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002166 else
2167 sp = tsk->thread.ksp;
2168 }
2169
Paul Mackerras06d67d52005-10-10 22:29:05 +10002170 lr = 0;
Dmitry Safonovb9677a82020-06-08 21:31:14 -07002171 printk("%sCall Trace:\n", loglvl);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002172 do {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002173 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
Christophe Leroy018cce32019-01-31 10:08:52 +00002174 break;
Paul Mackerras06d67d52005-10-10 22:29:05 +10002175
2176 stack = (unsigned long *) sp;
2177 newsp = stack[0];
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002178 ip = stack[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10002179 if (!firstframe || ip != lr) {
Dmitry Safonovb9677a82020-06-08 21:31:14 -07002180 printk("%s["REG"] ["REG"] %pS",
2181 loglvl, sp, ip, (void *)ip);
Naveen N. Rao7c1bb6b2019-09-05 23:50:30 +05302182 ret_addr = ftrace_graph_ret_addr(current,
2183 &ftrace_idx, ip, stack);
2184 if (ret_addr != ip)
2185 pr_cont(" (%pS)", (void *)ret_addr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10002186 if (firstframe)
Michael Ellerman9a1f4902016-11-02 22:20:46 +11002187 pr_cont(" (unreliable)");
2188 pr_cont("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002189 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10002190 firstframe = 0;
2191
2192 /*
2193 * See if this is an exception frame.
2194 * We look for the "regshere" marker in the current frame.
2195 */
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002196 if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
2197 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10002198 struct pt_regs *regs = (struct pt_regs *)
2199 (sp + STACK_FRAME_OVERHEAD);
Paul Mackerras06d67d52005-10-10 22:29:05 +10002200 lr = regs->link;
Dmitry Safonovb9677a82020-06-08 21:31:14 -07002201 printk("%s--- interrupt: %lx at %pS\n LR = %pS\n",
2202 loglvl, regs->trap,
2203 (void *)regs->nip, (void *)lr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10002204 firstframe = 1;
2205 }
2206
2207 sp = newsp;
2208 } while (count++ < kstack_depth_to_print);
Christophe Leroy018cce32019-01-31 10:08:52 +00002209
2210 put_task_stack(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002211}
Paul Mackerras06d67d52005-10-10 22:29:05 +10002212
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002213#ifdef CONFIG_PPC64
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002214/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10002215void notrace __ppc64_runlatch_on(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002216{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002217 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002218
Nicholas Piggind1d0d5f2017-08-12 02:39:07 +10002219 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2220 /*
2221 * Least significant bit (RUN) is the only writable bit of
2222 * the CTRL register, so we can avoid mfspr. 2.06 is not the
2223 * earliest ISA where this is the case, but it's convenient.
2224 */
2225 mtspr(SPRN_CTRLT, CTRL_RUNLATCH);
2226 } else {
2227 unsigned long ctrl;
2228
2229 /*
2230 * Some architectures (e.g., Cell) have writable fields other
2231 * than RUN, so do the read-modify-write.
2232 */
2233 ctrl = mfspr(SPRN_CTRLF);
2234 ctrl |= CTRL_RUNLATCH;
2235 mtspr(SPRN_CTRLT, ctrl);
2236 }
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002237
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10002238 ti->local_flags |= _TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002239}
2240
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002241/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10002242void notrace __ppc64_runlatch_off(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002243{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002244 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002245
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10002246 ti->local_flags &= ~_TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002247
Nicholas Piggind1d0d5f2017-08-12 02:39:07 +10002248 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2249 mtspr(SPRN_CTRLT, 0);
2250 } else {
2251 unsigned long ctrl;
2252
2253 ctrl = mfspr(SPRN_CTRLF);
2254 ctrl &= ~CTRL_RUNLATCH;
2255 mtspr(SPRN_CTRLT, ctrl);
2256 }
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002257}
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002258#endif /* CONFIG_PPC64 */
Benjamin Herrenschmidtf6a61682008-04-18 16:56:17 +10002259
Anton Blanchardd8390882009-02-22 01:50:03 +00002260unsigned long arch_align_stack(unsigned long sp)
2261{
2262 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2263 sp -= get_random_int() & ~PAGE_MASK;
2264 return sp & ~0xf;
2265}
Anton Blanchard912f9ee2009-02-22 01:50:04 +00002266
2267static inline unsigned long brk_rnd(void)
2268{
2269 unsigned long rnd = 0;
2270
2271 /* 8MB for 32bit, 1GB for 64bit */
2272 if (is_32bit_task())
Daniel Cashman5ef11c32016-02-26 15:19:37 -08002273 rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT)));
Anton Blanchard912f9ee2009-02-22 01:50:04 +00002274 else
Daniel Cashman5ef11c32016-02-26 15:19:37 -08002275 rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT)));
Anton Blanchard912f9ee2009-02-22 01:50:04 +00002276
2277 return rnd << PAGE_SHIFT;
2278}
2279
2280unsigned long arch_randomize_brk(struct mm_struct *mm)
2281{
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00002282 unsigned long base = mm->brk;
2283 unsigned long ret;
2284
Michael Ellerman4e003742017-10-19 15:08:43 +11002285#ifdef CONFIG_PPC_BOOK3S_64
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00002286 /*
2287 * If we are using 1TB segments and we are allowed to randomise
2288 * the heap, we can put it above 1TB so it is backed by a 1TB
2289 * segment. Otherwise the heap will be in the bottom 1TB
2290 * which always uses 256MB segments and this may result in a
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10002291 * performance penalty. We don't need to worry about radix. For
2292 * radix, mmu_highuser_ssize remains unchanged from 256MB.
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00002293 */
2294 if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
2295 base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
2296#endif
2297
2298 ret = PAGE_ALIGN(base + brk_rnd());
Anton Blanchard912f9ee2009-02-22 01:50:04 +00002299
2300 if (ret < mm->brk)
2301 return mm->brk;
2302
2303 return ret;
2304}
Anton Blanchard501cb162009-02-22 01:50:07 +00002305