blob: 8935c5696bcefcfd59355de87d454250016d0a74 [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
Nicholas Piggin3a965702021-01-30 23:08:38 +100044#include <asm/interrupt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100045#include <asm/io.h>
46#include <asm/processor.h>
47#include <asm/mmu.h>
48#include <asm/prom.h>
Michael Ellerman76032de2005-11-07 13:12:03 +110049#include <asm/machdep.h>
Paul Mackerrasc6622f62006-02-24 10:06:59 +110050#include <asm/time.h>
David Howellsae3a1972012-03-28 18:30:02 +010051#include <asm/runlatch.h>
Arnd Bergmanna7f31842006-03-23 00:00:08 +010052#include <asm/syscalls.h>
David Howellsae3a1972012-03-28 18:30:02 +010053#include <asm/switch_to.h>
Michael Neulingfb096922013-02-13 16:21:37 +000054#include <asm/tm.h>
David Howellsae3a1972012-03-28 18:30:02 +010055#include <asm/debug.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100056#ifdef CONFIG_PPC64
57#include <asm/firmware.h>
Madhavan Srinivasanc2e480b2017-12-20 09:25:42 +053058#include <asm/hw_irq.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100059#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +110060#include <asm/code-patching.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110061#include <asm/exec.h>
Michael Ellerman5d31a962016-03-24 22:04:04 +110062#include <asm/livepatch.h>
Kevin Haob92a2262016-07-23 14:42:40 +053063#include <asm/cpu_has_feature.h>
Daniel Axtens0545d542016-09-06 15:32:43 +100064#include <asm/asm-prototypes.h>
Christophe Leroyc9386bf2018-10-09 16:46:25 +110065#include <asm/stacktrace.h>
Michael Neulingc1fe1902019-04-01 17:03:12 +110066#include <asm/hw_breakpoint.h>
Michael Ellerman5d31a962016-03-24 22:04:04 +110067
Luis Machadod6a61bf2008-07-24 02:10:41 +100068#include <linux/kprobes.h>
69#include <linux/kdebug.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100070
Michael Neuling8b3c34c2013-02-13 16:21:32 +000071/* Transactional Memory debug */
72#ifdef TM_DEBUG_SW
73#define TM_DEBUG(x...) printk(KERN_INFO x)
74#else
75#define TM_DEBUG(x...) do { } while(0)
76#endif
77
Paul Mackerras14cf11a2005-09-26 16:04:21 +100078extern unsigned long _get_SP(void);
79
Paul Mackerrasd31626f2014-01-13 15:56:29 +110080#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Michael Ellerman54820532017-10-12 21:17:18 +110081/*
82 * Are we running in "Suspend disabled" mode? If so we have to block any
83 * sigreturn that would get us into suspended state, and we also warn in some
84 * other paths that we should never reach with suspend disabled.
85 */
86bool tm_suspend_disabled __ro_after_init = false;
87
Anton Blanchardb86fd2b2015-10-29 11:43:58 +110088static void check_if_tm_restore_required(struct task_struct *tsk)
Paul Mackerrasd31626f2014-01-13 15:56:29 +110089{
90 /*
91 * If we are saving the current thread's registers, and the
92 * thread is in a transactional state, set the TIF_RESTORE_TM
93 * bit so that we know to restore the registers before
94 * returning to userspace.
95 */
96 if (tsk == current && tsk->thread.regs &&
97 MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
98 !test_thread_flag(TIF_RESTORE_TM)) {
Anshuman Khandual829023d2015-07-06 16:24:10 +053099 tsk->thread.ckpt_regs.msr = tsk->thread.regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100100 set_thread_flag(TIF_RESTORE_TM);
101 }
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100102}
Cyril Burdc16b552016-09-23 16:18:08 +1000103
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100104#else
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100105static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100106#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
107
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100108bool strict_msr_control;
109EXPORT_SYMBOL(strict_msr_control);
110
111static int __init enable_strict_msr_control(char *str)
112{
113 strict_msr_control = true;
114 pr_info("Enabling strict facility control\n");
115
116 return 0;
117}
118early_param("ppc_strict_facility_enable", enable_strict_msr_control);
119
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000120/* notrace because it's called by restore_math */
121unsigned long notrace msr_check_and_set(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100122{
123 unsigned long oldmsr = mfmsr();
124 unsigned long newmsr;
125
126 newmsr = oldmsr | bits;
127
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100128 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
129 newmsr |= MSR_VSX;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100130
131 if (oldmsr != newmsr)
132 mtmsr_isync(newmsr);
Cyril Bur3cee0702016-09-23 16:18:10 +1000133
134 return newmsr;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100135}
Simon Guod1c72112018-05-23 15:01:44 +0800136EXPORT_SYMBOL_GPL(msr_check_and_set);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100137
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000138/* notrace because it's called by restore_math */
139void notrace __msr_check_and_clear(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100140{
141 unsigned long oldmsr = mfmsr();
142 unsigned long newmsr;
143
144 newmsr = oldmsr & ~bits;
145
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100146 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
147 newmsr &= ~MSR_VSX;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100148
149 if (oldmsr != newmsr)
150 mtmsr_isync(newmsr);
151}
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100152EXPORT_SYMBOL(__msr_check_and_clear);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100153
Kevin Hao037f0ee2013-07-14 17:02:05 +0800154#ifdef CONFIG_PPC_FPU
Mathieu Malaterre1cdf0392018-02-25 18:22:23 +0100155static void __giveup_fpu(struct task_struct *tsk)
Cyril Bur87924682016-02-29 17:53:49 +1100156{
Anton Blanchard8eb98032016-05-29 22:03:50 +1000157 unsigned long msr;
158
Cyril Bur87924682016-02-29 17:53:49 +1100159 save_fpu(tsk);
Anton Blanchard8eb98032016-05-29 22:03:50 +1000160 msr = tsk->thread.regs->msr;
Mark Cave-Aylandfe1ef6b2019-02-08 14:33:19 +0000161 msr &= ~(MSR_FP|MSR_FE0|MSR_FE1);
Cyril Bur87924682016-02-29 17:53:49 +1100162 if (cpu_has_feature(CPU_FTR_VSX))
Anton Blanchard8eb98032016-05-29 22:03:50 +1000163 msr &= ~MSR_VSX;
Anton Blanchard8eb98032016-05-29 22:03:50 +1000164 tsk->thread.regs->msr = msr;
Cyril Bur87924682016-02-29 17:53:49 +1100165}
166
Anton Blanchard98da5812015-10-29 11:44:01 +1100167void giveup_fpu(struct task_struct *tsk)
168{
Anton Blanchard98da5812015-10-29 11:44:01 +1100169 check_if_tm_restore_required(tsk);
170
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100171 msr_check_and_set(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100172 __giveup_fpu(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100173 msr_check_and_clear(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100174}
175EXPORT_SYMBOL(giveup_fpu);
176
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000177/*
178 * Make sure the floating-point register state in the
179 * the thread_struct is up to date for task tsk.
180 */
181void flush_fp_to_thread(struct task_struct *tsk)
182{
183 if (tsk->thread.regs) {
184 /*
185 * We need to disable preemption here because if we didn't,
186 * another process could get scheduled after the regs->msr
187 * test but before we have finished saving the FP registers
188 * to the thread_struct. That process could take over the
189 * FPU, and then when we get scheduled again we would store
190 * bogus values for the remaining FP registers.
191 */
192 preempt_disable();
193 if (tsk->thread.regs->msr & MSR_FP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000194 /*
195 * This should only ever be called for current or
196 * for a stopped child process. Since we save away
Anton Blanchardaf1bbc32015-10-29 11:43:57 +1100197 * the FP register state on context switch,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000198 * there is something wrong if a stopped child appears
199 * to still have its FP state in the CPU registers.
200 */
201 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100202 giveup_fpu(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000203 }
204 preempt_enable();
205 }
206}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000207EXPORT_SYMBOL_GPL(flush_fp_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000208
209void enable_kernel_fp(void)
210{
Cyril Bure909fb82016-09-23 16:18:11 +1000211 unsigned long cpumsr;
212
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000213 WARN_ON(preemptible());
214
Cyril Bure909fb82016-09-23 16:18:11 +1000215 cpumsr = msr_check_and_set(MSR_FP);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100216
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100217 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
218 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000219 /*
220 * If a thread has already been reclaimed then the
221 * checkpointed registers are on the CPU but have definitely
222 * been saved by the reclaim code. Don't need to and *cannot*
223 * giveup as this would save to the 'live' structure not the
224 * checkpointed structure.
225 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300226 if (!MSR_TM_ACTIVE(cpumsr) &&
227 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000228 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100229 __giveup_fpu(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100230 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000231}
232EXPORT_SYMBOL(enable_kernel_fp);
Christophe Leroyc83c1922020-08-17 05:47:58 +0000233#else
234static inline void __giveup_fpu(struct task_struct *tsk) { }
Anton Blanchardd1e1cf22015-10-29 11:44:11 +1100235#endif /* CONFIG_PPC_FPU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000236
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000237#ifdef CONFIG_ALTIVEC
Cyril Bur6f515d82016-02-29 17:53:50 +1100238static void __giveup_altivec(struct task_struct *tsk)
239{
Anton Blanchard8eb98032016-05-29 22:03:50 +1000240 unsigned long msr;
241
Cyril Bur6f515d82016-02-29 17:53:50 +1100242 save_altivec(tsk);
Anton Blanchard8eb98032016-05-29 22:03:50 +1000243 msr = tsk->thread.regs->msr;
244 msr &= ~MSR_VEC;
Cyril Bur6f515d82016-02-29 17:53:50 +1100245 if (cpu_has_feature(CPU_FTR_VSX))
Anton Blanchard8eb98032016-05-29 22:03:50 +1000246 msr &= ~MSR_VSX;
Anton Blanchard8eb98032016-05-29 22:03:50 +1000247 tsk->thread.regs->msr = msr;
Cyril Bur6f515d82016-02-29 17:53:50 +1100248}
249
Anton Blanchard98da5812015-10-29 11:44:01 +1100250void giveup_altivec(struct task_struct *tsk)
251{
Anton Blanchard98da5812015-10-29 11:44:01 +1100252 check_if_tm_restore_required(tsk);
253
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100254 msr_check_and_set(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100255 __giveup_altivec(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100256 msr_check_and_clear(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100257}
258EXPORT_SYMBOL(giveup_altivec);
259
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000260void enable_kernel_altivec(void)
261{
Cyril Bure909fb82016-09-23 16:18:11 +1000262 unsigned long cpumsr;
263
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000264 WARN_ON(preemptible());
265
Cyril Bure909fb82016-09-23 16:18:11 +1000266 cpumsr = msr_check_and_set(MSR_VEC);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100267
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100268 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
269 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000270 /*
271 * If a thread has already been reclaimed then the
272 * checkpointed registers are on the CPU but have definitely
273 * been saved by the reclaim code. Don't need to and *cannot*
274 * giveup as this would save to the 'live' structure not the
275 * checkpointed structure.
276 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300277 if (!MSR_TM_ACTIVE(cpumsr) &&
278 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000279 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100280 __giveup_altivec(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100281 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000282}
283EXPORT_SYMBOL(enable_kernel_altivec);
284
285/*
286 * Make sure the VMX/Altivec register state in the
287 * the thread_struct is up to date for task tsk.
288 */
289void flush_altivec_to_thread(struct task_struct *tsk)
290{
291 if (tsk->thread.regs) {
292 preempt_disable();
293 if (tsk->thread.regs->msr & MSR_VEC) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000294 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100295 giveup_altivec(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000296 }
297 preempt_enable();
298 }
299}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000300EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000301#endif /* CONFIG_ALTIVEC */
302
Michael Neulingce48b212008-06-25 14:07:18 +1000303#ifdef CONFIG_VSX
Cyril Burbf6a4d52016-02-29 17:53:51 +1100304static void __giveup_vsx(struct task_struct *tsk)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100305{
Benjamin Herrenschmidtdc801082017-08-16 16:01:17 +1000306 unsigned long msr = tsk->thread.regs->msr;
307
308 /*
309 * We should never be ssetting MSR_VSX without also setting
310 * MSR_FP and MSR_VEC
311 */
312 WARN_ON((msr & MSR_VSX) && !((msr & MSR_FP) && (msr & MSR_VEC)));
313
314 /* __giveup_fpu will clear MSR_VSX */
315 if (msr & MSR_FP)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100316 __giveup_fpu(tsk);
Benjamin Herrenschmidtdc801082017-08-16 16:01:17 +1000317 if (msr & MSR_VEC)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100318 __giveup_altivec(tsk);
Cyril Burbf6a4d52016-02-29 17:53:51 +1100319}
320
321static void giveup_vsx(struct task_struct *tsk)
322{
323 check_if_tm_restore_required(tsk);
324
325 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100326 __giveup_vsx(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100327 msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100328}
Cyril Burbf6a4d52016-02-29 17:53:51 +1100329
Michael Neulingce48b212008-06-25 14:07:18 +1000330void enable_kernel_vsx(void)
331{
Cyril Bure909fb82016-09-23 16:18:11 +1000332 unsigned long cpumsr;
333
Michael Neulingce48b212008-06-25 14:07:18 +1000334 WARN_ON(preemptible());
335
Cyril Bure909fb82016-09-23 16:18:11 +1000336 cpumsr = msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100337
Benjamin Herrenschmidt5a69aec2017-08-16 16:01:14 +1000338 if (current->thread.regs &&
339 (current->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP))) {
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100340 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000341 /*
342 * If a thread has already been reclaimed then the
343 * checkpointed registers are on the CPU but have definitely
344 * been saved by the reclaim code. Don't need to and *cannot*
345 * giveup as this would save to the 'live' structure not the
346 * checkpointed structure.
347 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300348 if (!MSR_TM_ACTIVE(cpumsr) &&
349 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000350 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100351 __giveup_vsx(current);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100352 }
Michael Neulingce48b212008-06-25 14:07:18 +1000353}
354EXPORT_SYMBOL(enable_kernel_vsx);
Michael Neulingce48b212008-06-25 14:07:18 +1000355
356void flush_vsx_to_thread(struct task_struct *tsk)
357{
358 if (tsk->thread.regs) {
359 preempt_disable();
Benjamin Herrenschmidt5a69aec2017-08-16 16:01:14 +1000360 if (tsk->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP)) {
Michael Neulingce48b212008-06-25 14:07:18 +1000361 BUG_ON(tsk != current);
Michael Neulingce48b212008-06-25 14:07:18 +1000362 giveup_vsx(tsk);
363 }
364 preempt_enable();
365 }
366}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000367EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
Michael Neulingce48b212008-06-25 14:07:18 +1000368#endif /* CONFIG_VSX */
369
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000370#ifdef CONFIG_SPE
Anton Blanchard98da5812015-10-29 11:44:01 +1100371void giveup_spe(struct task_struct *tsk)
372{
Anton Blanchard98da5812015-10-29 11:44:01 +1100373 check_if_tm_restore_required(tsk);
374
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100375 msr_check_and_set(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100376 __giveup_spe(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100377 msr_check_and_clear(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100378}
379EXPORT_SYMBOL(giveup_spe);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000380
381void enable_kernel_spe(void)
382{
383 WARN_ON(preemptible());
384
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100385 msr_check_and_set(MSR_SPE);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100386
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100387 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
388 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100389 __giveup_spe(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100390 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000391}
392EXPORT_SYMBOL(enable_kernel_spe);
393
394void flush_spe_to_thread(struct task_struct *tsk)
395{
396 if (tsk->thread.regs) {
397 preempt_disable();
398 if (tsk->thread.regs->msr & MSR_SPE) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000399 BUG_ON(tsk != current);
yu liu685659e2011-06-14 18:34:25 -0500400 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
Kumar Gala0ee6c152007-08-28 21:15:53 -0500401 giveup_spe(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000402 }
403 preempt_enable();
404 }
405}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000406#endif /* CONFIG_SPE */
407
Anton Blanchardc2085052015-10-29 11:44:08 +1100408static unsigned long msr_all_available;
409
410static int __init init_msr_all_available(void)
411{
Christophe Leroyc83c1922020-08-17 05:47:58 +0000412 if (IS_ENABLED(CONFIG_PPC_FPU))
413 msr_all_available |= MSR_FP;
Anton Blanchardc2085052015-10-29 11:44:08 +1100414 if (cpu_has_feature(CPU_FTR_ALTIVEC))
415 msr_all_available |= MSR_VEC;
Anton Blanchardc2085052015-10-29 11:44:08 +1100416 if (cpu_has_feature(CPU_FTR_VSX))
417 msr_all_available |= MSR_VSX;
Anton Blanchardc2085052015-10-29 11:44:08 +1100418 if (cpu_has_feature(CPU_FTR_SPE))
419 msr_all_available |= MSR_SPE;
Anton Blanchardc2085052015-10-29 11:44:08 +1100420
421 return 0;
422}
423early_initcall(init_msr_all_available);
424
425void giveup_all(struct task_struct *tsk)
426{
427 unsigned long usermsr;
428
429 if (!tsk->thread.regs)
430 return;
431
Gustavo Romero8205d5d2019-09-04 00:55:27 -0400432 check_if_tm_restore_required(tsk);
433
Anton Blanchardc2085052015-10-29 11:44:08 +1100434 usermsr = tsk->thread.regs->msr;
435
436 if ((usermsr & msr_all_available) == 0)
437 return;
438
439 msr_check_and_set(msr_all_available);
440
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000441 WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
442
Anton Blanchardc2085052015-10-29 11:44:08 +1100443 if (usermsr & MSR_FP)
444 __giveup_fpu(tsk);
Anton Blanchardc2085052015-10-29 11:44:08 +1100445 if (usermsr & MSR_VEC)
446 __giveup_altivec(tsk);
Anton Blanchardc2085052015-10-29 11:44:08 +1100447 if (usermsr & MSR_SPE)
448 __giveup_spe(tsk);
Anton Blanchardc2085052015-10-29 11:44:08 +1100449
450 msr_check_and_clear(msr_all_available);
451}
452EXPORT_SYMBOL(giveup_all);
453
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000454#ifdef CONFIG_PPC_BOOK3S_64
455#ifdef CONFIG_PPC_FPU
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000456static bool should_restore_fp(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000457{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000458 if (current->thread.load_fp) {
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000459 current->thread.load_fp++;
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000460 return true;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000461 }
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000462 return false;
463}
464
465static void do_restore_fp(void)
466{
467 load_fp_state(&current->thread.fp_state);
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000468}
469#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000470static bool should_restore_fp(void) { return false; }
471static void do_restore_fp(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000472#endif /* CONFIG_PPC_FPU */
473
474#ifdef CONFIG_ALTIVEC
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000475static bool should_restore_altivec(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000476{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000477 if (cpu_has_feature(CPU_FTR_ALTIVEC) && (current->thread.load_vec)) {
478 current->thread.load_vec++;
479 return true;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000480 }
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000481 return false;
482}
483
484static void do_restore_altivec(void)
485{
486 load_vr_state(&current->thread.vr_state);
487 current->thread.used_vr = 1;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000488}
489#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000490static bool should_restore_altivec(void) { return false; }
491static void do_restore_altivec(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000492#endif /* CONFIG_ALTIVEC */
493
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000494static bool should_restore_vsx(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000495{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000496 if (cpu_has_feature(CPU_FTR_VSX))
497 return true;
498 return false;
499}
Christophe Leroy80739c22020-08-17 05:47:55 +0000500#ifdef CONFIG_VSX
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000501static void do_restore_vsx(void)
502{
503 current->thread.used_vsr = 1;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000504}
505#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000506static void do_restore_vsx(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000507#endif /* CONFIG_VSX */
508
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000509/*
510 * The exception exit path calls restore_math() with interrupts hard disabled
511 * but the soft irq state not "reconciled". ftrace code that calls
512 * local_irq_save/restore causes warnings.
513 *
514 * Rather than complicate the exit path, just don't trace restore_math. This
515 * could be done by having ftrace entry code check for this un-reconciled
516 * condition where MSR[EE]=0 and PACA_IRQ_HARD_DIS is not set, and
517 * temporarily fix it up for the duration of the ftrace call.
518 */
519void notrace restore_math(struct pt_regs *regs)
Cyril Bur70fe3d92016-02-29 17:53:47 +1100520{
521 unsigned long msr;
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000522 unsigned long new_msr = 0;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100523
524 msr = regs->msr;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100525
526 /*
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000527 * new_msr tracks the facilities that are to be restored. Only reload
528 * if the bit is not set in the user MSR (if it is set, the registers
529 * are live for the user thread).
Cyril Bur70fe3d92016-02-29 17:53:47 +1100530 */
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000531 if ((!(msr & MSR_FP)) && should_restore_fp())
Michael Ellermanb91eb512020-08-25 19:34:24 +1000532 new_msr |= MSR_FP;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100533
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000534 if ((!(msr & MSR_VEC)) && should_restore_altivec())
535 new_msr |= MSR_VEC;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100536
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000537 if ((!(msr & MSR_VSX)) && should_restore_vsx()) {
538 if (((msr | new_msr) & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC))
539 new_msr |= MSR_VSX;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100540 }
541
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000542 if (new_msr) {
Michael Ellermanb91eb512020-08-25 19:34:24 +1000543 unsigned long fpexc_mode = 0;
544
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000545 msr_check_and_set(new_msr);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100546
Michael Ellermanb91eb512020-08-25 19:34:24 +1000547 if (new_msr & MSR_FP) {
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000548 do_restore_fp();
549
Michael Ellermanb91eb512020-08-25 19:34:24 +1000550 // This also covers VSX, because VSX implies FP
551 fpexc_mode = current->thread.fpexc_mode;
552 }
553
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000554 if (new_msr & MSR_VEC)
555 do_restore_altivec();
556
557 if (new_msr & MSR_VSX)
558 do_restore_vsx();
559
560 msr_check_and_clear(new_msr);
561
Michael Ellermanb91eb512020-08-25 19:34:24 +1000562 regs->msr |= new_msr | fpexc_mode;
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000563 }
Cyril Bur70fe3d92016-02-29 17:53:47 +1100564}
Christophe Leroy60d62bf2020-08-17 05:46:45 +0000565#endif /* CONFIG_PPC_BOOK3S_64 */
Cyril Bur70fe3d92016-02-29 17:53:47 +1100566
Mathieu Malaterre1cdf0392018-02-25 18:22:23 +0100567static void save_all(struct task_struct *tsk)
Cyril Burde2a20a2016-02-29 17:53:48 +1100568{
569 unsigned long usermsr;
570
571 if (!tsk->thread.regs)
572 return;
573
574 usermsr = tsk->thread.regs->msr;
575
576 if ((usermsr & msr_all_available) == 0)
577 return;
578
579 msr_check_and_set(msr_all_available);
580
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000581 WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
Cyril Burde2a20a2016-02-29 17:53:48 +1100582
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000583 if (usermsr & MSR_FP)
584 save_fpu(tsk);
585
586 if (usermsr & MSR_VEC)
587 save_altivec(tsk);
Cyril Burde2a20a2016-02-29 17:53:48 +1100588
589 if (usermsr & MSR_SPE)
590 __giveup_spe(tsk);
591
592 msr_check_and_clear(msr_all_available);
593}
594
Anton Blanchard579e6332015-10-29 11:44:09 +1100595void flush_all_to_thread(struct task_struct *tsk)
596{
597 if (tsk->thread.regs) {
598 preempt_disable();
599 BUG_ON(tsk != current);
Anton Blanchard579e6332015-10-29 11:44:09 +1100600#ifdef CONFIG_SPE
601 if (tsk->thread.regs->msr & MSR_SPE)
602 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
603#endif
Felipe Rechiae9013782018-10-24 10:57:22 -0300604 save_all(tsk);
Anton Blanchard579e6332015-10-29 11:44:09 +1100605
606 preempt_enable();
607 }
608}
609EXPORT_SYMBOL(flush_all_to_thread);
610
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000611#ifdef CONFIG_PPC_ADV_DEBUG_REGS
612void do_send_trap(struct pt_regs *regs, unsigned long address,
Eric W. Biederman47355042018-01-16 16:12:38 -0600613 unsigned long error_code, int breakpt)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000614{
Eric W. Biederman47355042018-01-16 16:12:38 -0600615 current->thread.trap_nr = TRAP_HWBKPT;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000616 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
617 11, SIGSEGV) == NOTIFY_STOP)
618 return;
619
620 /* Deliver the signal to userspace */
Eric W. Biedermanf71dd7d2018-01-22 14:37:25 -0600621 force_sig_ptrace_errno_trap(breakpt, /* breakpoint or watchpoint id */
622 (void __user *)address);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000623}
624#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Ravi Bangoria5b905d72020-09-02 09:59:42 +0530625
626static void do_break_handler(struct pt_regs *regs)
627{
628 struct arch_hw_breakpoint null_brk = {0};
629 struct arch_hw_breakpoint *info;
630 struct ppc_inst instr = ppc_inst(0);
631 int type = 0;
632 int size = 0;
633 unsigned long ea;
634 int i;
635
636 /*
637 * If underneath hw supports only one watchpoint, we know it
638 * caused exception. 8xx also falls into this category.
639 */
640 if (nr_wp_slots() == 1) {
641 __set_breakpoint(0, &null_brk);
642 current->thread.hw_brk[0] = null_brk;
643 current->thread.hw_brk[0].flags |= HW_BRK_FLAG_DISABLED;
644 return;
645 }
646
647 /* Otherwise findout which DAWR caused exception and disable it. */
648 wp_get_instr_detail(regs, &instr, &type, &size, &ea);
649
650 for (i = 0; i < nr_wp_slots(); i++) {
651 info = &current->thread.hw_brk[i];
652 if (!info->address)
653 continue;
654
655 if (wp_check_constraints(regs, instr, ea, type, size, info)) {
656 __set_breakpoint(i, &null_brk);
657 current->thread.hw_brk[i] = null_brk;
658 current->thread.hw_brk[i].flags |= HW_BRK_FLAG_DISABLED;
659 }
660 }
661}
662
Nicholas Piggin3a965702021-01-30 23:08:38 +1000663DEFINE_INTERRUPT_HANDLER(do_break)
Luis Machadod6a61bf2008-07-24 02:10:41 +1000664{
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000665 current->thread.trap_nr = TRAP_HWBKPT;
Nicholas Piggin18722ec2021-01-30 23:08:18 +1000666 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, regs->dsisr,
Luis Machadod6a61bf2008-07-24 02:10:41 +1000667 11, SIGSEGV) == NOTIFY_STOP)
668 return;
669
Michael Neuling9422de32012-12-20 14:06:44 +0000670 if (debugger_break_match(regs))
Luis Machadod6a61bf2008-07-24 02:10:41 +1000671 return;
672
Ravi Bangoria5b905d72020-09-02 09:59:42 +0530673 /*
674 * We reach here only when watchpoint exception is generated by ptrace
675 * event (or hw is buggy!). Now if CONFIG_HAVE_HW_BREAKPOINT is set,
676 * watchpoint is already handled by hw_breakpoint_handler() so we don't
677 * have to do anything. But when CONFIG_HAVE_HW_BREAKPOINT is not set,
678 * we need to manually handle the watchpoint here.
679 */
680 if (!IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT))
681 do_break_handler(regs);
682
Luis Machadod6a61bf2008-07-24 02:10:41 +1000683 /* Deliver the signal to userspace */
Nicholas Piggin18722ec2021-01-30 23:08:18 +1000684 force_sig_fault(SIGTRAP, TRAP_HWBKPT, (void __user *)regs->dar);
Luis Machadod6a61bf2008-07-24 02:10:41 +1000685}
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000686#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000687
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530688static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk[HBP_NUM_MAX]);
Michael Ellermana2ceff52008-03-28 19:11:48 +1100689
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000690#ifdef CONFIG_PPC_ADV_DEBUG_REGS
691/*
692 * Set the debug registers back to their default "safe" values.
693 */
694static void set_debug_reg_defaults(struct thread_struct *thread)
695{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530696 thread->debug.iac1 = thread->debug.iac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000697#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530698 thread->debug.iac3 = thread->debug.iac4 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000699#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530700 thread->debug.dac1 = thread->debug.dac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000701#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530702 thread->debug.dvc1 = thread->debug.dvc2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000703#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530704 thread->debug.dbcr0 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000705#ifdef CONFIG_BOOKE
706 /*
707 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
708 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530709 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000710 DBCR1_IAC3US | DBCR1_IAC4US;
711 /*
712 * Force Data Address Compare User/Supervisor bits to be User-only
713 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
714 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530715 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000716#else
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530717 thread->debug.dbcr1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000718#endif
719}
720
Scott Woodf5f97212013-11-22 15:52:29 -0600721static void prime_debug_regs(struct debug_reg *debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000722{
Scott Wood6cecf762013-05-13 14:14:53 +0000723 /*
724 * We could have inherited MSR_DE from userspace, since
725 * it doesn't get cleared on exception entry. Make sure
726 * MSR_DE is clear before we enable any debug events.
727 */
728 mtmsr(mfmsr() & ~MSR_DE);
729
Scott Woodf5f97212013-11-22 15:52:29 -0600730 mtspr(SPRN_IAC1, debug->iac1);
731 mtspr(SPRN_IAC2, debug->iac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000732#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Scott Woodf5f97212013-11-22 15:52:29 -0600733 mtspr(SPRN_IAC3, debug->iac3);
734 mtspr(SPRN_IAC4, debug->iac4);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000735#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600736 mtspr(SPRN_DAC1, debug->dac1);
737 mtspr(SPRN_DAC2, debug->dac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000738#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Scott Woodf5f97212013-11-22 15:52:29 -0600739 mtspr(SPRN_DVC1, debug->dvc1);
740 mtspr(SPRN_DVC2, debug->dvc2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000741#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600742 mtspr(SPRN_DBCR0, debug->dbcr0);
743 mtspr(SPRN_DBCR1, debug->dbcr1);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000744#ifdef CONFIG_BOOKE
Scott Woodf5f97212013-11-22 15:52:29 -0600745 mtspr(SPRN_DBCR2, debug->dbcr2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000746#endif
747}
748/*
749 * Unless neither the old or new thread are making use of the
750 * debug registers, set the debug registers from the values
751 * stored in the new thread.
752 */
Scott Woodf5f97212013-11-22 15:52:29 -0600753void switch_booke_debug_regs(struct debug_reg *new_debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000754{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530755 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
Scott Woodf5f97212013-11-22 15:52:29 -0600756 || (new_debug->dbcr0 & DBCR0_IDM))
757 prime_debug_regs(new_debug);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000758}
Bharat Bhushan3743c9b2013-07-04 12:27:44 +0530759EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000760#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
K.Prasade0780b72011-02-10 04:44:35 +0000761#ifndef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530762static void set_breakpoint(int i, struct arch_hw_breakpoint *brk)
Christophe Leroyb5ac51d2018-07-05 16:25:05 +0000763{
764 preempt_disable();
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530765 __set_breakpoint(i, brk);
Christophe Leroyb5ac51d2018-07-05 16:25:05 +0000766 preempt_enable();
767}
768
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000769static void set_debug_reg_defaults(struct thread_struct *thread)
770{
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530771 int i;
772 struct arch_hw_breakpoint null_brk = {0};
773
774 for (i = 0; i < nr_wp_slots(); i++) {
775 thread->hw_brk[i] = null_brk;
776 if (ppc_breakpoint_available())
777 set_breakpoint(i, &thread->hw_brk[i]);
778 }
779}
780
781static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
782 struct arch_hw_breakpoint *b)
783{
784 if (a->address != b->address)
785 return false;
786 if (a->type != b->type)
787 return false;
788 if (a->len != b->len)
789 return false;
790 /* no need to check hw_len. it's calculated from address and len */
791 return true;
792}
793
794static void switch_hw_breakpoint(struct task_struct *new)
795{
796 int i;
797
798 for (i = 0; i < nr_wp_slots(); i++) {
799 if (likely(hw_brk_match(this_cpu_ptr(&current_brk[i]),
800 &new->thread.hw_brk[i])))
801 continue;
802
803 __set_breakpoint(i, &new->thread.hw_brk[i]);
804 }
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000805}
K.Prasade0780b72011-02-10 04:44:35 +0000806#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000807#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
808
Michael Neuling9422de32012-12-20 14:06:44 +0000809static inline int set_dabr(struct arch_hw_breakpoint *brk)
810{
811 unsigned long dabr, dabrx;
812
813 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
814 dabrx = ((brk->type >> 3) & 0x7);
815
816 if (ppc_md.set_dabr)
817 return ppc_md.set_dabr(dabr, dabrx);
818
Christophe Leroyad3ed152020-12-04 10:12:51 +0000819 if (IS_ENABLED(CONFIG_PPC_ADV_DEBUG_REGS)) {
820 mtspr(SPRN_DAC1, dabr);
821 if (IS_ENABLED(CONFIG_PPC_47x))
822 isync();
823 return 0;
824 } else if (IS_ENABLED(CONFIG_PPC_BOOK3S)) {
825 mtspr(SPRN_DABR, dabr);
826 if (cpu_has_feature(CPU_FTR_DABRX))
827 mtspr(SPRN_DABRX, dabrx);
828 return 0;
829 } else {
830 return -EINVAL;
831 }
Michael Neuling9422de32012-12-20 14:06:44 +0000832}
833
Christophe Leroy39413ae2019-11-26 17:43:29 +0000834static inline int set_breakpoint_8xx(struct arch_hw_breakpoint *brk)
835{
836 unsigned long lctrl1 = LCTRL1_CTE_GT | LCTRL1_CTF_LT | LCTRL1_CRWE_RW |
837 LCTRL1_CRWF_RW;
838 unsigned long lctrl2 = LCTRL2_LW0EN | LCTRL2_LW0LADC | LCTRL2_SLW0EN;
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530839 unsigned long start_addr = ALIGN_DOWN(brk->address, HW_BREAKPOINT_SIZE);
840 unsigned long end_addr = ALIGN(brk->address + brk->len, HW_BREAKPOINT_SIZE);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000841
842 if (start_addr == 0)
843 lctrl2 |= LCTRL2_LW0LA_F;
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530844 else if (end_addr == 0)
Christophe Leroy39413ae2019-11-26 17:43:29 +0000845 lctrl2 |= LCTRL2_LW0LA_E;
846 else
847 lctrl2 |= LCTRL2_LW0LA_EandF;
848
849 mtspr(SPRN_LCTRL2, 0);
850
851 if ((brk->type & HW_BRK_TYPE_RDWR) == 0)
852 return 0;
853
854 if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_READ)
855 lctrl1 |= LCTRL1_CRWE_RO | LCTRL1_CRWF_RO;
856 if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_WRITE)
857 lctrl1 |= LCTRL1_CRWE_WO | LCTRL1_CRWF_WO;
858
859 mtspr(SPRN_CMPE, start_addr - 1);
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530860 mtspr(SPRN_CMPF, end_addr);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000861 mtspr(SPRN_LCTRL1, lctrl1);
862 mtspr(SPRN_LCTRL2, lctrl2);
863
864 return 0;
865}
866
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530867void __set_breakpoint(int nr, struct arch_hw_breakpoint *brk)
Michael Neuling9422de32012-12-20 14:06:44 +0000868{
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530869 memcpy(this_cpu_ptr(&current_brk[nr]), brk, sizeof(*brk));
Michael Neuling9422de32012-12-20 14:06:44 +0000870
Michael Neulingc1fe1902019-04-01 17:03:12 +1100871 if (dawr_enabled())
Nicholas Piggin252988c2018-04-01 15:50:36 +1000872 // Power8 or later
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530873 set_dawr(nr, brk);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000874 else if (IS_ENABLED(CONFIG_PPC_8xx))
875 set_breakpoint_8xx(brk);
Nicholas Piggin252988c2018-04-01 15:50:36 +1000876 else if (!cpu_has_feature(CPU_FTR_ARCH_207S))
877 // Power7 or earlier
Paul Gortmaker04c32a52014-04-29 15:25:16 -0400878 set_dabr(brk);
Nicholas Piggin252988c2018-04-01 15:50:36 +1000879 else
880 // Shouldn't happen due to higher level checks
881 WARN_ON_ONCE(1);
Michael Neuling9422de32012-12-20 14:06:44 +0000882}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000883
Michael Neuling404b27d2018-03-27 15:37:17 +1100884/* Check if we have DAWR or DABR hardware */
885bool ppc_breakpoint_available(void)
886{
Michael Neulingc1fe1902019-04-01 17:03:12 +1100887 if (dawr_enabled())
888 return true; /* POWER8 DAWR or POWER9 forced DAWR */
Michael Neuling404b27d2018-03-27 15:37:17 +1100889 if (cpu_has_feature(CPU_FTR_ARCH_207S))
890 return false; /* POWER9 with DAWR disabled */
891 /* DABR: Everything but POWER8 and POWER9 */
892 return true;
893}
894EXPORT_SYMBOL_GPL(ppc_breakpoint_available);
895
Michael Neulingfb096922013-02-13 16:21:37 +0000896#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Cyril Bur5d176f72016-09-14 18:02:16 +1000897
898static inline bool tm_enabled(struct task_struct *tsk)
899{
900 return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM);
901}
902
Cyril Buredd00b82018-02-01 12:07:46 +1100903static void tm_reclaim_thread(struct thread_struct *thr, uint8_t cause)
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100904{
Michael Neuling7f821fc2015-11-19 15:44:45 +1100905 /*
906 * Use the current MSR TM suspended bit to track if we have
907 * checkpointed state outstanding.
908 * On signal delivery, we'd normally reclaim the checkpointed
909 * state to obtain stack pointer (see:get_tm_stackpointer()).
910 * This will then directly return to userspace without going
911 * through __switch_to(). However, if the stack frame is bad,
912 * we need to exit this thread which calls __switch_to() which
913 * will again attempt to reclaim the already saved tm state.
914 * Hence we need to check that we've not already reclaimed
915 * this state.
916 * We do this using the current MSR, rather tracking it in
917 * some specific thread_struct bit, as it has the additional
Michael Ellerman027dfac2016-06-01 16:34:37 +1000918 * benefit of checking for a potential TM bad thing exception.
Michael Neuling7f821fc2015-11-19 15:44:45 +1100919 */
920 if (!MSR_TM_SUSPENDED(mfmsr()))
921 return;
922
Cyril Bur91381b92017-11-02 14:09:04 +1100923 giveup_all(container_of(thr, struct task_struct, thread));
924
Cyril Bureb5c3f12017-11-02 14:09:05 +1100925 tm_reclaim(thr, cause);
926
Michael Neulingf48e91e2017-05-08 17:16:26 +1000927 /*
928 * If we are in a transaction and FP is off then we can't have
929 * used FP inside that transaction. Hence the checkpointed
930 * state is the same as the live state. We need to copy the
931 * live state to the checkpointed state so that when the
932 * transaction is restored, the checkpointed state is correct
933 * and the aborted transaction sees the correct state. We use
934 * ckpt_regs.msr here as that's what tm_reclaim will use to
935 * determine if it's going to write the checkpointed state or
936 * not. So either this will write the checkpointed registers,
937 * or reclaim will. Similarly for VMX.
938 */
939 if ((thr->ckpt_regs.msr & MSR_FP) == 0)
940 memcpy(&thr->ckfp_state, &thr->fp_state,
941 sizeof(struct thread_fp_state));
942 if ((thr->ckpt_regs.msr & MSR_VEC) == 0)
943 memcpy(&thr->ckvr_state, &thr->vr_state,
944 sizeof(struct thread_vr_state));
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100945}
946
947void tm_reclaim_current(uint8_t cause)
948{
949 tm_enable();
Cyril Buredd00b82018-02-01 12:07:46 +1100950 tm_reclaim_thread(&current->thread, cause);
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100951}
952
Michael Neulingfb096922013-02-13 16:21:37 +0000953static inline void tm_reclaim_task(struct task_struct *tsk)
954{
955 /* We have to work out if we're switching from/to a task that's in the
956 * middle of a transaction.
957 *
958 * In switching we need to maintain a 2nd register state as
959 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
Cyril Bur000ec282016-09-23 16:18:25 +1000960 * checkpointed (tbegin) state in ckpt_regs, ckfp_state and
961 * ckvr_state
Michael Neulingfb096922013-02-13 16:21:37 +0000962 *
963 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
964 */
965 struct thread_struct *thr = &tsk->thread;
966
967 if (!thr->regs)
968 return;
969
970 if (!MSR_TM_ACTIVE(thr->regs->msr))
971 goto out_and_saveregs;
972
Michael Neuling92fb8692017-10-12 21:17:19 +1100973 WARN_ON(tm_suspend_disabled);
974
Michael Neulingfb096922013-02-13 16:21:37 +0000975 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
976 "ccr=%lx, msr=%lx, trap=%lx)\n",
977 tsk->pid, thr->regs->nip,
978 thr->regs->ccr, thr->regs->msr,
979 thr->regs->trap);
980
Cyril Buredd00b82018-02-01 12:07:46 +1100981 tm_reclaim_thread(thr, TM_CAUSE_RESCHED);
Michael Neulingfb096922013-02-13 16:21:37 +0000982
983 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
984 tsk->pid);
985
986out_and_saveregs:
987 /* Always save the regs here, even if a transaction's not active.
988 * This context-switches a thread's TM info SPRs. We do it here to
989 * be consistent with the restore path (in recheckpoint) which
990 * cannot happen later in _switch().
991 */
992 tm_save_sprs(thr);
993}
994
Cyril Bureb5c3f12017-11-02 14:09:05 +1100995extern void __tm_recheckpoint(struct thread_struct *thread);
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100996
Cyril Bureb5c3f12017-11-02 14:09:05 +1100997void tm_recheckpoint(struct thread_struct *thread)
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100998{
999 unsigned long flags;
1000
Cyril Bur5d176f72016-09-14 18:02:16 +10001001 if (!(thread->regs->msr & MSR_TM))
1002 return;
1003
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001004 /* We really can't be interrupted here as the TEXASR registers can't
1005 * change and later in the trecheckpoint code, we have a userspace R1.
1006 * So let's hard disable over this region.
1007 */
1008 local_irq_save(flags);
1009 hard_irq_disable();
1010
1011 /* The TM SPRs are restored here, so that TEXASR.FS can be set
1012 * before the trecheckpoint and no explosion occurs.
1013 */
1014 tm_restore_sprs(thread);
1015
Cyril Bureb5c3f12017-11-02 14:09:05 +11001016 __tm_recheckpoint(thread);
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001017
1018 local_irq_restore(flags);
1019}
1020
Michael Neulingbc2a9402013-02-13 16:21:40 +00001021static inline void tm_recheckpoint_new_task(struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +00001022{
Michael Neulingfb096922013-02-13 16:21:37 +00001023 if (!cpu_has_feature(CPU_FTR_TM))
1024 return;
1025
1026 /* Recheckpoint the registers of the thread we're about to switch to.
1027 *
1028 * If the task was using FP, we non-lazily reload both the original and
1029 * the speculative FP register states. This is because the kernel
1030 * doesn't see if/when a TM rollback occurs, so if we take an FP
Cyril Burdc310662016-09-23 16:18:24 +10001031 * unavailable later, we are unable to determine which set of FP regs
Michael Neulingfb096922013-02-13 16:21:37 +00001032 * need to be restored.
1033 */
Cyril Bur5d176f72016-09-14 18:02:16 +10001034 if (!tm_enabled(new))
Michael Neulingfb096922013-02-13 16:21:37 +00001035 return;
1036
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001037 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
1038 tm_restore_sprs(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +00001039 return;
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001040 }
Michael Neulingfb096922013-02-13 16:21:37 +00001041 /* Recheckpoint to restore original checkpointed register state. */
Cyril Bureb5c3f12017-11-02 14:09:05 +11001042 TM_DEBUG("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n",
1043 new->pid, new->thread.regs->msr);
Michael Neulingfb096922013-02-13 16:21:37 +00001044
Cyril Bureb5c3f12017-11-02 14:09:05 +11001045 tm_recheckpoint(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +00001046
Cyril Burdc310662016-09-23 16:18:24 +10001047 /*
1048 * The checkpointed state has been restored but the live state has
1049 * not, ensure all the math functionality is turned off to trigger
1050 * restore_math() to reload.
1051 */
1052 new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX);
Michael Neulingfb096922013-02-13 16:21:37 +00001053
1054 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
1055 "(kernel msr 0x%lx)\n",
1056 new->pid, mfmsr());
1057}
1058
Cyril Burdc310662016-09-23 16:18:24 +10001059static inline void __switch_to_tm(struct task_struct *prev,
1060 struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +00001061{
1062 if (cpu_has_feature(CPU_FTR_TM)) {
Cyril Bur5d176f72016-09-14 18:02:16 +10001063 if (tm_enabled(prev) || tm_enabled(new))
1064 tm_enable();
1065
1066 if (tm_enabled(prev)) {
1067 prev->thread.load_tm++;
1068 tm_reclaim_task(prev);
1069 if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0)
1070 prev->thread.regs->msr &= ~MSR_TM;
1071 }
1072
Cyril Burdc310662016-09-23 16:18:24 +10001073 tm_recheckpoint_new_task(new);
Michael Neulingfb096922013-02-13 16:21:37 +00001074 }
1075}
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001076
1077/*
1078 * This is called if we are on the way out to userspace and the
1079 * TIF_RESTORE_TM flag is set. It checks if we need to reload
1080 * FP and/or vector state and does so if necessary.
1081 * If userspace is inside a transaction (whether active or
1082 * suspended) and FP/VMX/VSX instructions have ever been enabled
1083 * inside that transaction, then we have to keep them enabled
1084 * and keep the FP/VMX/VSX state loaded while ever the transaction
1085 * continues. The reason is that if we didn't, and subsequently
1086 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
1087 * we don't know whether it's the same transaction, and thus we
1088 * don't know which of the checkpointed state and the transactional
1089 * state to use.
1090 */
1091void restore_tm_state(struct pt_regs *regs)
1092{
1093 unsigned long msr_diff;
1094
Cyril Burdc310662016-09-23 16:18:24 +10001095 /*
1096 * This is the only moment we should clear TIF_RESTORE_TM as
1097 * it is here that ckpt_regs.msr and pt_regs.msr become the same
1098 * again, anything else could lead to an incorrect ckpt_msr being
1099 * saved and therefore incorrect signal contexts.
1100 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001101 clear_thread_flag(TIF_RESTORE_TM);
1102 if (!MSR_TM_ACTIVE(regs->msr))
1103 return;
1104
Anshuman Khandual829023d2015-07-06 16:24:10 +05301105 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001106 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
Cyril Bur70fe3d92016-02-29 17:53:47 +11001107
Cyril Burdc16b552016-09-23 16:18:08 +10001108 /* Ensure that restore_math() will restore */
1109 if (msr_diff & MSR_FP)
1110 current->thread.load_fp = 1;
Valentin Rothberg39715bf2016-10-05 07:57:26 +02001111#ifdef CONFIG_ALTIVEC
Cyril Burdc16b552016-09-23 16:18:08 +10001112 if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC)
1113 current->thread.load_vec = 1;
1114#endif
Cyril Bur70fe3d92016-02-29 17:53:47 +11001115 restore_math(regs);
1116
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001117 regs->msr |= msr_diff;
1118}
1119
Christopher M. Riedl2d196302021-02-26 19:12:54 -06001120#else /* !CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neulingfb096922013-02-13 16:21:37 +00001121#define tm_recheckpoint_new_task(new)
Cyril Burdc310662016-09-23 16:18:24 +10001122#define __switch_to_tm(prev, new)
Christopher M. Riedl2d196302021-02-26 19:12:54 -06001123void tm_reclaim_current(uint8_t cause) {}
Michael Neulingfb096922013-02-13 16:21:37 +00001124#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neuling9422de32012-12-20 14:06:44 +00001125
Anton Blanchard152d5232015-10-29 11:43:55 +11001126static inline void save_sprs(struct thread_struct *t)
1127{
1128#ifdef CONFIG_ALTIVEC
Oliver O'Halloran01d7c2a22016-03-08 09:08:47 +11001129 if (cpu_has_feature(CPU_FTR_ALTIVEC))
Anton Blanchard152d5232015-10-29 11:43:55 +11001130 t->vrsave = mfspr(SPRN_VRSAVE);
1131#endif
1132#ifdef CONFIG_PPC_BOOK3S_64
1133 if (cpu_has_feature(CPU_FTR_DSCR))
1134 t->dscr = mfspr(SPRN_DSCR);
1135
1136 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1137 t->bescr = mfspr(SPRN_BESCR);
1138 t->ebbhr = mfspr(SPRN_EBBHR);
1139 t->ebbrr = mfspr(SPRN_EBBRR);
1140
1141 t->fscr = mfspr(SPRN_FSCR);
1142
1143 /*
1144 * Note that the TAR is not available for use in the kernel.
1145 * (To provide this, the TAR should be backed up/restored on
1146 * exception entry/exit instead, and be in pt_regs. FIXME,
1147 * this should be in pt_regs anyway (for debug).)
1148 */
1149 t->tar = mfspr(SPRN_TAR);
1150 }
1151#endif
1152}
1153
1154static inline void restore_sprs(struct thread_struct *old_thread,
1155 struct thread_struct *new_thread)
1156{
1157#ifdef CONFIG_ALTIVEC
1158 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
1159 old_thread->vrsave != new_thread->vrsave)
1160 mtspr(SPRN_VRSAVE, new_thread->vrsave);
1161#endif
1162#ifdef CONFIG_PPC_BOOK3S_64
1163 if (cpu_has_feature(CPU_FTR_DSCR)) {
1164 u64 dscr = get_paca()->dscr_default;
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001165 if (new_thread->dscr_inherit)
Anton Blanchard152d5232015-10-29 11:43:55 +11001166 dscr = new_thread->dscr;
Anton Blanchard152d5232015-10-29 11:43:55 +11001167
1168 if (old_thread->dscr != dscr)
1169 mtspr(SPRN_DSCR, dscr);
Anton Blanchard152d5232015-10-29 11:43:55 +11001170 }
1171
1172 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1173 if (old_thread->bescr != new_thread->bescr)
1174 mtspr(SPRN_BESCR, new_thread->bescr);
1175 if (old_thread->ebbhr != new_thread->ebbhr)
1176 mtspr(SPRN_EBBHR, new_thread->ebbhr);
1177 if (old_thread->ebbrr != new_thread->ebbrr)
1178 mtspr(SPRN_EBBRR, new_thread->ebbrr);
1179
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001180 if (old_thread->fscr != new_thread->fscr)
1181 mtspr(SPRN_FSCR, new_thread->fscr);
1182
Anton Blanchard152d5232015-10-29 11:43:55 +11001183 if (old_thread->tar != new_thread->tar)
1184 mtspr(SPRN_TAR, new_thread->tar);
1185 }
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001186
Alastair D'Silva3449f192018-05-11 16:12:58 +10001187 if (cpu_has_feature(CPU_FTR_P9_TIDR) &&
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001188 old_thread->tidr != new_thread->tidr)
1189 mtspr(SPRN_TIDR, new_thread->tidr);
Anton Blanchard152d5232015-10-29 11:43:55 +11001190#endif
Ram Pai06bb53b2018-01-18 17:50:31 -08001191
Anton Blanchard152d5232015-10-29 11:43:55 +11001192}
1193
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001194struct task_struct *__switch_to(struct task_struct *prev,
1195 struct task_struct *new)
1196{
1197 struct thread_struct *new_thread, *old_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001198 struct task_struct *last;
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001199#ifdef CONFIG_PPC_BOOK3S_64
1200 struct ppc64_tlb_batch *batch;
1201#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001202
Anton Blanchard152d5232015-10-29 11:43:55 +11001203 new_thread = &new->thread;
1204 old_thread = &current->thread;
1205
Michael Neuling7ba5fef2013-10-02 17:15:14 +10001206 WARN_ON(!irqs_disabled());
1207
Michael Ellerman4e003742017-10-19 15:08:43 +11001208#ifdef CONFIG_PPC_BOOK3S_64
Christoph Lameter69111ba2014-10-21 15:23:25 -05001209 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001210 if (batch->active) {
1211 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1212 if (batch->index)
1213 __flush_tlb_pending(batch);
1214 batch->active = 0;
1215 }
Michael Ellerman4e003742017-10-19 15:08:43 +11001216#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001217
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001218#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1219 switch_booke_debug_regs(&new->thread.debug);
1220#else
1221/*
1222 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1223 * schedule DABR
1224 */
1225#ifndef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria303e6a92020-05-14 16:47:34 +05301226 switch_hw_breakpoint(new);
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001227#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1228#endif
1229
1230 /*
1231 * We need to save SPRs before treclaim/trecheckpoint as these will
1232 * change a number of them.
1233 */
1234 save_sprs(&prev->thread);
1235
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001236 /* Save FPU, Altivec, VSX and SPE state */
1237 giveup_all(prev);
1238
Cyril Burdc310662016-09-23 16:18:24 +10001239 __switch_to_tm(prev, new);
1240
Nicholas Piggine4c0fc52017-06-09 01:36:06 +10001241 if (!radix_enabled()) {
1242 /*
1243 * We can't take a PMU exception inside _switch() since there
1244 * is a window where the kernel stack SLB and the kernel stack
1245 * are out of sync. Hard disable here.
1246 */
1247 hard_irq_disable();
1248 }
Michael Neulingbc2a9402013-02-13 16:21:40 +00001249
Anton Blanchard20dbe672015-12-10 20:44:39 +11001250 /*
1251 * Call restore_sprs() before calling _switch(). If we move it after
1252 * _switch() then we miss out on calling it for new tasks. The reason
1253 * for this is we manually create a stack frame for new tasks that
1254 * directly returns through ret_from_fork() or
1255 * ret_from_kernel_thread(). See copy_thread() for details.
1256 */
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001257 restore_sprs(old_thread, new_thread);
1258
Christophe Leroyc1672882021-03-12 12:50:51 +00001259#ifdef CONFIG_PPC32
1260 kuap_assert_locked();
1261#endif
Anton Blanchard20dbe672015-12-10 20:44:39 +11001262 last = _switch(old_thread, new_thread);
1263
Michael Ellerman4e003742017-10-19 15:08:43 +11001264#ifdef CONFIG_PPC_BOOK3S_64
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001265 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1266 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001267 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001268 batch->active = 1;
1269 }
Cyril Bur70fe3d92016-02-29 17:53:47 +11001270
Christophe Leroy05b98792019-01-17 23:25:12 +11001271 if (current->thread.regs) {
1272 restore_math(current->thread.regs);
Nicholas Piggin07d2a622017-06-09 01:36:09 +10001273
1274 /*
Nicholas Piggindc462262020-08-25 17:55:35 +10001275 * On POWER9 the copy-paste buffer can only paste into
1276 * foreign real addresses, so unprivileged processes can not
1277 * see the data or use it in any way unless they have
1278 * foreign real mappings. If the new process has the foreign
1279 * real address mappings, we must issue a cp_abort to clear
1280 * any state and prevent snooping, corruption or a covert
1281 * channel. ISA v3.1 supports paste into local memory.
Nicholas Piggin07d2a622017-06-09 01:36:09 +10001282 */
Haren Mynenic4206442020-04-15 23:08:11 -07001283 if (current->mm &&
Nicholas Piggindc462262020-08-25 17:55:35 +10001284 (cpu_has_feature(CPU_FTR_ARCH_31) ||
1285 atomic_read(&current->mm->context.vas_windows)))
Sukadev Bhattiprolu9d2a4d72017-11-07 18:23:54 -08001286 asm volatile(PPC_CP_ABORT);
Nicholas Piggin07d2a622017-06-09 01:36:09 +10001287 }
Michael Ellerman4e003742017-10-19 15:08:43 +11001288#endif /* CONFIG_PPC_BOOK3S_64 */
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001289
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001290 return last;
1291}
1292
Christophe Leroydf131022018-10-06 16:51:16 +00001293#define NR_INSN_TO_PRINT 16
Paul Mackerras06d67d52005-10-10 22:29:05 +10001294
Paul Mackerras06d67d52005-10-10 22:29:05 +10001295static void show_instructions(struct pt_regs *regs)
1296{
1297 int i;
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301298 unsigned long nip = regs->nip;
Christophe Leroydf131022018-10-06 16:51:16 +00001299 unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
Paul Mackerras06d67d52005-10-10 22:29:05 +10001300
1301 printk("Instruction dump:");
1302
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301303 /*
1304 * If we were executing with the MMU off for instructions, adjust pc
1305 * rather than printing XXXXXXXX.
1306 */
1307 if (!IS_ENABLED(CONFIG_BOOKE) && !(regs->msr & MSR_IR)) {
1308 pc = (unsigned long)phys_to_virt(pc);
1309 nip = (unsigned long)phys_to_virt(regs->nip);
1310 }
1311
Christophe Leroydf131022018-10-06 16:51:16 +00001312 for (i = 0; i < NR_INSN_TO_PRINT; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001313 int instr;
1314
1315 if (!(i % 8))
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001316 pr_cont("\n");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001317
Anton Blanchard00ae36d2006-10-13 12:17:16 +10001318 if (!__kernel_text_address(pc) ||
Christoph Hellwig25f12ae2020-06-17 09:37:55 +02001319 get_kernel_nofault(instr, (const void *)pc)) {
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001320 pr_cont("XXXXXXXX ");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001321 } else {
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301322 if (nip == pc)
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001323 pr_cont("<%08x> ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001324 else
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001325 pr_cont("%08x ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001326 }
1327
1328 pc += sizeof(int);
1329 }
1330
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001331 pr_cont("\n");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001332}
1333
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001334void show_user_instructions(struct pt_regs *regs)
1335{
1336 unsigned long pc;
Christophe Leroydf131022018-10-06 16:51:16 +00001337 int n = NR_INSN_TO_PRINT;
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001338 struct seq_buf s;
1339 char buf[96]; /* enough for 8 times 9 + 2 chars */
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001340
Christophe Leroydf131022018-10-06 16:51:16 +00001341 pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001342
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001343 seq_buf_init(&s, buf, sizeof(buf));
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001344
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001345 while (n) {
1346 int i;
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001347
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001348 seq_buf_clear(&s);
1349
1350 for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) {
1351 int instr;
1352
Christoph Hellwigc0ee37e2020-06-17 09:37:54 +02001353 if (copy_from_user_nofault(&instr, (void __user *)pc,
1354 sizeof(instr))) {
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001355 seq_buf_printf(&s, "XXXXXXXX ");
1356 continue;
1357 }
1358 seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr);
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001359 }
1360
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001361 if (!seq_buf_has_overflowed(&s))
1362 pr_info("%s[%d]: code: %s\n", current->comm,
1363 current->pid, s.buffer);
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001364 }
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001365}
1366
Michael Neuling801c0b22015-11-20 15:15:32 +11001367struct regbit {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001368 unsigned long bit;
1369 const char *name;
Michael Neuling801c0b22015-11-20 15:15:32 +11001370};
1371
1372static struct regbit msr_bits[] = {
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001373#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
1374 {MSR_SF, "SF"},
1375 {MSR_HV, "HV"},
1376#endif
1377 {MSR_VEC, "VEC"},
1378 {MSR_VSX, "VSX"},
1379#ifdef CONFIG_BOOKE
1380 {MSR_CE, "CE"},
1381#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001382 {MSR_EE, "EE"},
1383 {MSR_PR, "PR"},
1384 {MSR_FP, "FP"},
1385 {MSR_ME, "ME"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001386#ifdef CONFIG_BOOKE
Kumar Gala1b983262008-11-19 04:39:53 +00001387 {MSR_DE, "DE"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001388#else
1389 {MSR_SE, "SE"},
1390 {MSR_BE, "BE"},
1391#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001392 {MSR_IR, "IR"},
1393 {MSR_DR, "DR"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001394 {MSR_PMM, "PMM"},
1395#ifndef CONFIG_BOOKE
1396 {MSR_RI, "RI"},
1397 {MSR_LE, "LE"},
1398#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001399 {0, NULL}
1400};
1401
Michael Neuling801c0b22015-11-20 15:15:32 +11001402static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
Paul Mackerras06d67d52005-10-10 22:29:05 +10001403{
Michael Neuling801c0b22015-11-20 15:15:32 +11001404 const char *s = "";
Paul Mackerras06d67d52005-10-10 22:29:05 +10001405
Paul Mackerras06d67d52005-10-10 22:29:05 +10001406 for (; bits->bit; ++bits)
1407 if (val & bits->bit) {
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001408 pr_cont("%s%s", s, bits->name);
Michael Neuling801c0b22015-11-20 15:15:32 +11001409 s = sep;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001410 }
Michael Neuling801c0b22015-11-20 15:15:32 +11001411}
1412
1413#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1414static struct regbit msr_tm_bits[] = {
1415 {MSR_TS_T, "T"},
1416 {MSR_TS_S, "S"},
1417 {MSR_TM, "E"},
1418 {0, NULL}
1419};
1420
1421static void print_tm_bits(unsigned long val)
1422{
1423/*
1424 * This only prints something if at least one of the TM bit is set.
1425 * Inside the TM[], the output means:
1426 * E: Enabled (bit 32)
1427 * S: Suspended (bit 33)
1428 * T: Transactional (bit 34)
1429 */
1430 if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001431 pr_cont(",TM[");
Michael Neuling801c0b22015-11-20 15:15:32 +11001432 print_bits(val, msr_tm_bits, "");
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001433 pr_cont("]");
Michael Neuling801c0b22015-11-20 15:15:32 +11001434 }
1435}
1436#else
1437static void print_tm_bits(unsigned long val) {}
1438#endif
1439
1440static void print_msr_bits(unsigned long val)
1441{
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001442 pr_cont("<");
Michael Neuling801c0b22015-11-20 15:15:32 +11001443 print_bits(val, msr_bits, ",");
1444 print_tm_bits(val);
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001445 pr_cont(">");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001446}
1447
1448#ifdef CONFIG_PPC64
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001449#define REG "%016lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001450#define REGS_PER_LINE 4
Paul Mackerras06d67d52005-10-10 22:29:05 +10001451#else
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001452#define REG "%08lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001453#define REGS_PER_LINE 8
Paul Mackerras06d67d52005-10-10 22:29:05 +10001454#endif
1455
Nicholas Pigginbf13718b2020-11-07 12:33:05 +10001456static void __show_regs(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001457{
1458 int i, trap;
1459
Michael Ellermana6036102017-08-23 23:56:24 +10001460 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
Paul Mackerras06d67d52005-10-10 22:29:05 +10001461 regs->nip, regs->link, regs->ctr);
Michael Ellerman182dc9c2017-12-18 16:33:36 +11001462 printk("REGS: %px TRAP: %04lx %s (%s)\n",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001463 regs, regs->trap, print_tainted(), init_utsname()->release);
Michael Ellermana6036102017-08-23 23:56:24 +10001464 printk("MSR: "REG" ", regs->msr);
Michael Neuling801c0b22015-11-20 15:15:32 +11001465 print_msr_bits(regs->msr);
Michael Ellermanf6fc73f2017-08-23 23:56:23 +10001466 pr_cont(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001467 trap = TRAP(regs);
Nicholas Piggin912237e2020-05-07 22:13:31 +10001468 if (!trap_is_syscall(regs) && cpu_has_feature(CPU_FTR_CFAR))
Michael Ellerman7dae8652016-11-03 20:45:26 +11001469 pr_cont("CFAR: "REG" ", regs->orig_gpr3);
Xiongwei Song7153d4b2021-04-14 19:00:33 +08001470 if (trap == INTERRUPT_MACHINE_CHECK ||
1471 trap == INTERRUPT_DATA_STORAGE ||
1472 trap == INTERRUPT_ALIGNMENT) {
Christophe Leroy2ec42992020-08-17 05:46:43 +00001473 if (IS_ENABLED(CONFIG_4xx) || IS_ENABLED(CONFIG_BOOKE))
1474 pr_cont("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
1475 else
1476 pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1477 }
1478
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001479#ifdef CONFIG_PPC64
Nicholas Piggin3130a7b2018-05-10 11:04:24 +10001480 pr_cont("IRQMASK: %lx ", regs->softe);
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001481#endif
1482#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchard6d888d12013-11-18 13:19:17 +11001483 if (MSR_TM_ACTIVE(regs->msr))
Michael Ellerman7dae8652016-11-03 20:45:26 +11001484 pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
Kumar Gala14170782007-07-26 00:46:15 -05001485#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001486
1487 for (i = 0; i < 32; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001488 if ((i % REGS_PER_LINE) == 0)
Michael Ellerman7dae8652016-11-03 20:45:26 +11001489 pr_cont("\nGPR%02d: ", i);
1490 pr_cont(REG " ", regs->gpr[i]);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001491 }
Michael Ellerman7dae8652016-11-03 20:45:26 +11001492 pr_cont("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001493 /*
1494 * Lookup NIP late so we have the best change of getting the
1495 * above info out without failing
1496 */
Christophe Leroy8f020c72020-08-17 05:46:44 +00001497 if (IS_ENABLED(CONFIG_KALLSYMS)) {
1498 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1499 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
1500 }
Nicholas Pigginbf13718b2020-11-07 12:33:05 +10001501}
1502
1503void show_regs(struct pt_regs *regs)
1504{
1505 show_regs_print_info(KERN_DEFAULT);
1506 __show_regs(regs);
Dmitry Safonov9cb8f062020-06-08 21:32:29 -07001507 show_stack(current, (unsigned long *) regs->gpr[1], KERN_DEFAULT);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001508 if (!user_mode(regs))
1509 show_instructions(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001510}
1511
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001512void flush_thread(void)
1513{
K.Prasade0780b72011-02-10 04:44:35 +00001514#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad5aae8a52010-06-15 11:35:19 +05301515 flush_ptrace_hw_breakpoint(current);
K.Prasade0780b72011-02-10 04:44:35 +00001516#else /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001517 set_debug_reg_defaults(&current->thread);
K.Prasade0780b72011-02-10 04:44:35 +00001518#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001519}
1520
Nicholas Piggin425d3312018-09-15 01:30:55 +10001521void arch_setup_new_exec(void)
1522{
Aneesh Kumar K.Vd7df77e2020-11-27 10:14:11 +05301523
1524#ifdef CONFIG_PPC_BOOK3S_64
1525 if (!radix_enabled())
1526 hash__setup_new_exec();
Nicholas Piggin425d3312018-09-15 01:30:55 +10001527#endif
Aneesh Kumar K.Vd7df77e2020-11-27 10:14:11 +05301528 /*
1529 * If we exec out of a kernel thread then thread.regs will not be
1530 * set. Do it now.
1531 */
1532 if (!current->thread.regs) {
1533 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1534 current->thread.regs = regs - 1;
1535 }
Aneesh Kumar K.Vd5fa30e2020-11-27 10:14:14 +05301536
1537#ifdef CONFIG_PPC_MEM_KEYS
1538 current->thread.regs->amr = default_amr;
1539 current->thread.regs->iamr = default_iamr;
1540#endif
Aneesh Kumar K.Vd7df77e2020-11-27 10:14:11 +05301541}
Nicholas Piggin425d3312018-09-15 01:30:55 +10001542
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001543#ifdef CONFIG_PPC64
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001544/**
1545 * Assign a TIDR (thread ID) for task @t and set it in the thread
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001546 * structure. For now, we only support setting TIDR for 'current' task.
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001547 *
1548 * Since the TID value is a truncated form of it PID, it is possible
1549 * (but unlikely) for 2 threads to have the same TID. In the unlikely event
1550 * that 2 threads share the same TID and are waiting, one of the following
1551 * cases will happen:
1552 *
1553 * 1. The correct thread is running, the wrong thread is not
1554 * In this situation, the correct thread is woken and proceeds to pass it's
1555 * condition check.
1556 *
1557 * 2. Neither threads are running
1558 * In this situation, neither thread will be woken. When scheduled, the waiting
1559 * threads will execute either a wait, which will return immediately, followed
1560 * by a condition check, which will pass for the correct thread and fail
1561 * for the wrong thread, or they will execute the condition check immediately.
1562 *
1563 * 3. The wrong thread is running, the correct thread is not
1564 * The wrong thread will be woken, but will fail it's condition check and
1565 * re-execute wait. The correct thread, when scheduled, will execute either
1566 * it's condition check (which will pass), or wait, which returns immediately
1567 * when called the first time after the thread is scheduled, followed by it's
1568 * condition check (which will pass).
1569 *
1570 * 4. Both threads are running
1571 * Both threads will be woken. The wrong thread will fail it's condition check
1572 * and execute another wait, while the correct thread will pass it's condition
1573 * check.
1574 *
1575 * @t: the task to set the thread ID for
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001576 */
1577int set_thread_tidr(struct task_struct *t)
1578{
Alastair D'Silva3449f192018-05-11 16:12:58 +10001579 if (!cpu_has_feature(CPU_FTR_P9_TIDR))
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001580 return -EINVAL;
1581
1582 if (t != current)
1583 return -EINVAL;
1584
Vaibhav Jain7e4d4232017-11-24 14:03:38 +05301585 if (t->thread.tidr)
1586 return 0;
1587
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001588 t->thread.tidr = (u16)task_pid_nr(t);
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001589 mtspr(SPRN_TIDR, t->thread.tidr);
1590
1591 return 0;
1592}
Christophe Lombardb1db5512018-01-11 09:55:25 +01001593EXPORT_SYMBOL_GPL(set_thread_tidr);
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001594
1595#endif /* CONFIG_PPC64 */
1596
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001597void
1598release_thread(struct task_struct *t)
1599{
1600}
1601
1602/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001603 * this gets called so that we can store coprocessor state into memory and
1604 * copy the current task into the new thread.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001605 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001606int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001607{
Anton Blanchard579e6332015-10-29 11:44:09 +11001608 flush_all_to_thread(src);
Michael Neuling621b5062014-03-03 14:21:40 +11001609 /*
1610 * Flush TM state out so we can copy it. __switch_to_tm() does this
1611 * flush but it removes the checkpointed state from the current CPU and
1612 * transitions the CPU out of TM mode. Hence we need to call
1613 * tm_recheckpoint_new_task() (on the same task) to restore the
1614 * checkpointed state back and the TM mode.
Cyril Bur5d176f72016-09-14 18:02:16 +10001615 *
1616 * Can't pass dst because it isn't ready. Doesn't matter, passing
1617 * dst is only important for __switch_to()
Michael Neuling621b5062014-03-03 14:21:40 +11001618 */
Cyril Burdc310662016-09-23 16:18:24 +10001619 __switch_to_tm(src, src);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001620
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001621 *dst = *src;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001622
1623 clear_task_ebb(dst);
1624
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001625 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001626}
1627
Michael Ellermancec15482014-07-10 12:29:21 +10001628static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1629{
Michael Ellerman4e003742017-10-19 15:08:43 +11001630#ifdef CONFIG_PPC_BOOK3S_64
Michael Ellermancec15482014-07-10 12:29:21 +10001631 unsigned long sp_vsid;
1632 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1633
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001634 if (radix_enabled())
1635 return;
1636
Michael Ellermancec15482014-07-10 12:29:21 +10001637 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1638 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1639 << SLB_VSID_SHIFT_1T;
1640 else
1641 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1642 << SLB_VSID_SHIFT;
1643 sp_vsid |= SLB_VSID_KERNEL | llp;
1644 p->thread.ksp_vsid = sp_vsid;
1645#endif
1646}
1647
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001648/*
1649 * Copy a thread..
1650 */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001651
Alex Dowad6eca8932015-03-13 20:14:46 +02001652/*
1653 * Copy architecture-specific thread state
1654 */
Christian Brauner714acdb2020-06-11 11:04:15 +02001655int copy_thread(unsigned long clone_flags, unsigned long usp,
Nicholas Pigginfacd04a2019-08-27 13:30:06 +10001656 unsigned long kthread_arg, struct task_struct *p,
1657 unsigned long tls)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001658{
1659 struct pt_regs *childregs, *kregs;
1660 extern void ret_from_fork(void);
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001661 extern void ret_from_fork_scv(void);
Al Viro58254e12012-09-12 18:32:42 -04001662 extern void ret_from_kernel_thread(void);
1663 void (*f)(void);
Al Viro0cec6fd2006-01-12 01:06:02 -08001664 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Michael Ellerman5d31a962016-03-24 22:04:04 +11001665 struct thread_info *ti = task_thread_info(p);
Ravi Bangoria6b424ef2020-05-14 16:47:35 +05301666#ifdef CONFIG_HAVE_HW_BREAKPOINT
1667 int i;
1668#endif
Michael Ellerman5d31a962016-03-24 22:04:04 +11001669
Christophe Leroyed1cd6d2019-01-31 10:08:58 +00001670 klp_init_thread_info(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001671
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001672 /* Copy registers */
1673 sp -= sizeof(struct pt_regs);
1674 childregs = (struct pt_regs *) sp;
Jens Axboe0100e6b2021-02-23 11:57:20 -07001675 if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) {
Alex Dowad6eca8932015-03-13 20:14:46 +02001676 /* kernel thread */
Al Viro58254e12012-09-12 18:32:42 -04001677 memset(childregs, 0, sizeof(struct pt_regs));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001678 childregs->gpr[1] = sp + sizeof(struct pt_regs);
Anton Blanchard7cedd602014-02-04 16:08:51 +11001679 /* function */
1680 if (usp)
1681 childregs->gpr[14] = ppc_function_entry((void *)usp);
Al Viro58254e12012-09-12 18:32:42 -04001682#ifdef CONFIG_PPC64
Al Virob5e2fc12006-01-12 01:06:01 -08001683 clear_tsk_thread_flag(p, TIF_32BIT);
Madhavan Srinivasanc2e480b2017-12-20 09:25:42 +05301684 childregs->softe = IRQS_ENABLED;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001685#endif
Alex Dowad6eca8932015-03-13 20:14:46 +02001686 childregs->gpr[15] = kthread_arg;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001687 p->thread.regs = NULL; /* no user register state */
Al Viro138d1ce2012-10-11 08:41:43 -04001688 ti->flags |= _TIF_RESTOREALL;
Al Viro58254e12012-09-12 18:32:42 -04001689 f = ret_from_kernel_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001690 } else {
Alex Dowad6eca8932015-03-13 20:14:46 +02001691 /* user thread */
Al Viroafa86fc2012-10-22 22:51:14 -04001692 struct pt_regs *regs = current_pt_regs();
Al Viro58254e12012-09-12 18:32:42 -04001693 *childregs = *regs;
Al Viroea516b12012-10-21 22:28:43 -04001694 if (usp)
1695 childregs->gpr[1] = usp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001696 p->thread.regs = childregs;
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001697 /* 64s sets this in ret_from_fork */
1698 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
1699 childregs->gpr[3] = 0; /* Result from fork() */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001700 if (clone_flags & CLONE_SETTLS) {
Denis Kirjanov9904b002010-07-29 22:04:39 +00001701 if (!is_32bit_task())
Nicholas Pigginfacd04a2019-08-27 13:30:06 +10001702 childregs->gpr[13] = tls;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001703 else
Nicholas Pigginfacd04a2019-08-27 13:30:06 +10001704 childregs->gpr[2] = tls;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001705 }
Al Viro58254e12012-09-12 18:32:42 -04001706
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001707 if (trap_is_scv(regs))
1708 f = ret_from_fork_scv;
1709 else
1710 f = ret_from_fork;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001711 }
Cyril Burd272f662016-02-29 17:53:46 +11001712 childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001713 sp -= STACK_FRAME_OVERHEAD;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001714
1715 /*
1716 * The way this works is that at some point in the future
1717 * some task will call _switch to switch to the new task.
1718 * That will pop off the stack frame created below and start
1719 * the new task running at ret_from_fork. The new task will
1720 * do some house keeping and then return from the fork or clone
1721 * system call, using the stack frame created above.
1722 */
Li Zhongaf945cf2013-05-06 22:44:41 +00001723 ((unsigned long *)sp)[0] = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001724 sp -= sizeof(struct pt_regs);
1725 kregs = (struct pt_regs *) sp;
1726 sp -= STACK_FRAME_OVERHEAD;
1727 p->thread.ksp = sp;
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001728#ifdef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria6b424ef2020-05-14 16:47:35 +05301729 for (i = 0; i < nr_wp_slots(); i++)
1730 p->thread.ptrace_bps[i] = NULL;
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001731#endif
1732
Christophe Leroyb6254ce2020-08-18 17:19:17 +00001733#ifdef CONFIG_PPC_FPU_REGS
Paul Mackerras18461962013-09-10 20:21:10 +10001734 p->thread.fp_save_area = NULL;
Christophe Leroyb6254ce2020-08-18 17:19:17 +00001735#endif
Paul Mackerras18461962013-09-10 20:21:10 +10001736#ifdef CONFIG_ALTIVEC
1737 p->thread.vr_save_area = NULL;
1738#endif
1739
Michael Ellermancec15482014-07-10 12:29:21 +10001740 setup_ksp_vsid(p, sp);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001741
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001742#ifdef CONFIG_PPC64
1743 if (cpu_has_feature(CPU_FTR_DSCR)) {
Anton Blanchard1021cb22012-09-03 16:49:47 +00001744 p->thread.dscr_inherit = current->thread.dscr_inherit;
Anton Blancharddb1231dc2015-12-09 20:11:47 +11001745 p->thread.dscr = mfspr(SPRN_DSCR);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001746 }
Haren Myneni92779242012-12-06 21:49:56 +00001747 if (cpu_has_feature(CPU_FTR_HAS_PPR))
Nicholas Piggin4c2de742018-10-13 00:15:16 +11001748 childregs->ppr = DEFAULT_PPR;
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001749
1750 p->thread.tidr = 0;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001751#endif
Aneesh Kumar K.Vf643fca2020-11-27 10:14:13 +05301752 /*
1753 * Run with the current AMR value of the kernel
1754 */
1755#ifdef CONFIG_PPC_PKEY
1756 if (mmu_has_feature(MMU_FTR_BOOK3S_KUAP))
1757 kregs->amr = AMR_KUAP_BLOCKED;
1758
1759 if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP))
1760 kregs->iamr = AMR_KUEP_BLOCKED;
1761#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +11001762 kregs->nip = ppc_function_entry(f);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001763 return 0;
1764}
1765
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001766void preload_new_slb_context(unsigned long start, unsigned long sp);
1767
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001768/*
1769 * Set up a thread for executing a new program
1770 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001771void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001772{
Michael Ellerman90eac722005-10-21 16:01:33 +10001773#ifdef CONFIG_PPC64
1774 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001775
Christophe Leroybfac2792020-08-17 05:46:42 +00001776 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled())
Aneesh Kumar K.Vf89bd8b2019-04-09 09:33:28 +05301777 preload_new_slb_context(start, sp);
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001778#endif
Michael Ellerman90eac722005-10-21 16:01:33 +10001779
Cyril Bur8e96a872016-06-17 14:58:34 +10001780#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1781 /*
1782 * Clear any transactional state, we're exec()ing. The cause is
1783 * not important as there will never be a recheckpoint so it's not
1784 * user visible.
1785 */
1786 if (MSR_TM_SUSPENDED(mfmsr()))
1787 tm_reclaim_current(0);
1788#endif
1789
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001790 memset(regs->gpr, 0, sizeof(regs->gpr));
1791 regs->ctr = 0;
1792 regs->link = 0;
1793 regs->xer = 0;
1794 regs->ccr = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001795 regs->gpr[1] = sp;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001796
1797#ifdef CONFIG_PPC32
1798 regs->mq = 0;
1799 regs->nip = start;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001800 regs->msr = MSR_USER;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001801#else
Denis Kirjanov9904b002010-07-29 22:04:39 +00001802 if (!is_32bit_task()) {
Rusty Russell94af3ab2013-11-20 22:15:02 +11001803 unsigned long entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001804
Rusty Russell94af3ab2013-11-20 22:15:02 +11001805 if (is_elf2_task()) {
1806 /* Look ma, no function descriptors! */
1807 entry = start;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001808
Rusty Russell94af3ab2013-11-20 22:15:02 +11001809 /*
1810 * Ulrich says:
1811 * The latest iteration of the ABI requires that when
1812 * calling a function (at its global entry point),
1813 * the caller must ensure r12 holds the entry point
1814 * address (so that the function can quickly
1815 * establish addressability).
1816 */
1817 regs->gpr[12] = start;
1818 /* Make sure that's restored on entry to userspace. */
1819 set_thread_flag(TIF_RESTOREALL);
1820 } else {
1821 unsigned long toc;
1822
1823 /* start is a relocated pointer to the function
1824 * descriptor for the elf _start routine. The first
1825 * entry in the function descriptor is the entry
1826 * address of _start and the second entry is the TOC
1827 * value we need to use.
1828 */
1829 __get_user(entry, (unsigned long __user *)start);
1830 __get_user(toc, (unsigned long __user *)start+1);
1831
1832 /* Check whether the e_entry function descriptor entries
1833 * need to be relocated before we can use them.
1834 */
1835 if (load_addr != 0) {
1836 entry += load_addr;
1837 toc += load_addr;
1838 }
1839 regs->gpr[2] = toc;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001840 }
1841 regs->nip = entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001842 regs->msr = MSR_USER64;
Stephen Rothwelld4bf9a72005-10-13 13:40:54 +10001843 } else {
1844 regs->nip = start;
1845 regs->gpr[2] = 0;
1846 regs->msr = MSR_USER32;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001847 }
1848#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001849#ifdef CONFIG_VSX
1850 current->thread.used_vsr = 0;
1851#endif
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001852 current->thread.load_slb = 0;
Breno Leitao11958922017-06-02 18:43:30 -03001853 current->thread.load_fp = 0;
Christophe Leroyb6254ce2020-08-18 17:19:17 +00001854#ifdef CONFIG_PPC_FPU_REGS
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001855 memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
Paul Mackerras18461962013-09-10 20:21:10 +10001856 current->thread.fp_save_area = NULL;
Christophe Leroyb6254ce2020-08-18 17:19:17 +00001857#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001858#ifdef CONFIG_ALTIVEC
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001859 memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1860 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
Paul Mackerras18461962013-09-10 20:21:10 +10001861 current->thread.vr_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001862 current->thread.vrsave = 0;
1863 current->thread.used_vr = 0;
Breno Leitao11958922017-06-02 18:43:30 -03001864 current->thread.load_vec = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001865#endif /* CONFIG_ALTIVEC */
1866#ifdef CONFIG_SPE
1867 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1868 current->thread.acc = 0;
1869 current->thread.spefscr = 0;
1870 current->thread.used_spe = 0;
1871#endif /* CONFIG_SPE */
Michael Neulingbc2a9402013-02-13 16:21:40 +00001872#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Michael Neulingbc2a9402013-02-13 16:21:40 +00001873 current->thread.tm_tfhar = 0;
1874 current->thread.tm_texasr = 0;
1875 current->thread.tm_tfiar = 0;
Breno Leitao7f22ced2017-06-05 11:40:59 -03001876 current->thread.load_tm = 0;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001877#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Ram Pai06bb53b2018-01-18 17:50:31 -08001878
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001879}
Anton Blancharde1802b02014-08-20 08:00:02 +10001880EXPORT_SYMBOL(start_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001881
1882#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1883 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1884
1885int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1886{
1887 struct pt_regs *regs = tsk->thread.regs;
1888
1889 /* This is a bit hairy. If we are an SPE enabled processor
1890 * (have embedded fp) we store the IEEE exception enable flags in
1891 * fpexc_mode. fpexc_mode is also used for setting FP exception
1892 * mode (asyn, precise, disabled) for 'Classic' FP. */
1893 if (val & PR_FP_EXC_SW_ENABLE) {
Kumar Gala5e14d212007-09-13 01:44:20 -05001894 if (cpu_has_feature(CPU_FTR_SPE)) {
Joseph Myers640e9222013-12-10 23:07:45 +00001895 /*
1896 * When the sticky exception bits are set
1897 * directly by userspace, it must call prctl
1898 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1899 * in the existing prctl settings) or
1900 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1901 * the bits being set). <fenv.h> functions
1902 * saving and restoring the whole
1903 * floating-point environment need to do so
1904 * anyway to restore the prctl settings from
1905 * the saved environment.
1906 */
Christophe Leroy532ed192020-08-17 05:47:57 +00001907#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00001908 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001909 tsk->thread.fpexc_mode = val &
1910 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
Christophe Leroy532ed192020-08-17 05:47:57 +00001911#endif
Kumar Gala5e14d212007-09-13 01:44:20 -05001912 return 0;
1913 } else {
1914 return -EINVAL;
1915 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001916 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001917
1918 /* on a CONFIG_SPE this does not hurt us. The bits that
1919 * __pack_fe01 use do not overlap with bits used for
1920 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1921 * on CONFIG_SPE implementations are reserved so writing to
1922 * them does not change anything */
1923 if (val > PR_FP_EXC_PRECISE)
1924 return -EINVAL;
1925 tsk->thread.fpexc_mode = __pack_fe01(val);
1926 if (regs != NULL && (regs->msr & MSR_FP) != 0)
1927 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1928 | tsk->thread.fpexc_mode;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001929 return 0;
1930}
1931
1932int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1933{
Michael Ellermand208e132020-09-17 12:20:16 +10001934 unsigned int val = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001935
Christophe Leroy532ed192020-08-17 05:47:57 +00001936 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE) {
Joseph Myers640e9222013-12-10 23:07:45 +00001937 if (cpu_has_feature(CPU_FTR_SPE)) {
1938 /*
1939 * When the sticky exception bits are set
1940 * directly by userspace, it must call prctl
1941 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1942 * in the existing prctl settings) or
1943 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1944 * the bits being set). <fenv.h> functions
1945 * saving and restoring the whole
1946 * floating-point environment need to do so
1947 * anyway to restore the prctl settings from
1948 * the saved environment.
1949 */
Christophe Leroy532ed192020-08-17 05:47:57 +00001950#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00001951 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001952 val = tsk->thread.fpexc_mode;
Christophe Leroy532ed192020-08-17 05:47:57 +00001953#endif
Joseph Myers640e9222013-12-10 23:07:45 +00001954 } else
Kumar Gala5e14d212007-09-13 01:44:20 -05001955 return -EINVAL;
Christophe Leroy532ed192020-08-17 05:47:57 +00001956 } else {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001957 val = __unpack_fe01(tsk->thread.fpexc_mode);
Christophe Leroy532ed192020-08-17 05:47:57 +00001958 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001959 return put_user(val, (unsigned int __user *) adr);
1960}
1961
Paul Mackerrasfab5db92006-06-07 16:14:40 +10001962int set_endian(struct task_struct *tsk, unsigned int val)
1963{
1964 struct pt_regs *regs = tsk->thread.regs;
1965
1966 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1967 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1968 return -EINVAL;
1969
1970 if (regs == NULL)
1971 return -EINVAL;
1972
1973 if (val == PR_ENDIAN_BIG)
1974 regs->msr &= ~MSR_LE;
1975 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1976 regs->msr |= MSR_LE;
1977 else
1978 return -EINVAL;
1979
1980 return 0;
1981}
1982
1983int get_endian(struct task_struct *tsk, unsigned long adr)
1984{
1985 struct pt_regs *regs = tsk->thread.regs;
1986 unsigned int val;
1987
1988 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1989 !cpu_has_feature(CPU_FTR_REAL_LE))
1990 return -EINVAL;
1991
1992 if (regs == NULL)
1993 return -EINVAL;
1994
1995 if (regs->msr & MSR_LE) {
1996 if (cpu_has_feature(CPU_FTR_REAL_LE))
1997 val = PR_ENDIAN_LITTLE;
1998 else
1999 val = PR_ENDIAN_PPC_LITTLE;
2000 } else
2001 val = PR_ENDIAN_BIG;
2002
2003 return put_user(val, (unsigned int __user *)adr);
2004}
2005
Paul Mackerrase9370ae2006-06-07 16:15:39 +10002006int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
2007{
2008 tsk->thread.align_ctl = val;
2009 return 0;
2010}
2011
2012int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
2013{
2014 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
2015}
2016
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002017static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
2018 unsigned long nbytes)
2019{
2020 unsigned long stack_page;
2021 unsigned long cpu = task_cpu(p);
2022
Christophe Leroya7916a12019-01-31 10:09:00 +00002023 stack_page = (unsigned long)hardirq_ctx[cpu];
2024 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2025 return 1;
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002026
Christophe Leroya7916a12019-01-31 10:09:00 +00002027 stack_page = (unsigned long)softirq_ctx[cpu];
2028 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2029 return 1;
2030
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002031 return 0;
2032}
2033
Nicholas Piggina2e36682020-03-25 20:41:44 +10002034static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p,
2035 unsigned long nbytes)
2036{
2037#ifdef CONFIG_PPC64
2038 unsigned long stack_page;
2039 unsigned long cpu = task_cpu(p);
2040
Michael Ellerman0ecf6a92021-02-03 00:02:06 +11002041 if (!paca_ptrs)
2042 return 0;
2043
Nicholas Piggina2e36682020-03-25 20:41:44 +10002044 stack_page = (unsigned long)paca_ptrs[cpu]->emergency_sp - THREAD_SIZE;
2045 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2046 return 1;
2047
2048# ifdef CONFIG_PPC_BOOK3S_64
2049 stack_page = (unsigned long)paca_ptrs[cpu]->nmi_emergency_sp - THREAD_SIZE;
2050 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2051 return 1;
2052
2053 stack_page = (unsigned long)paca_ptrs[cpu]->mc_emergency_sp - THREAD_SIZE;
2054 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2055 return 1;
2056# endif
2057#endif
2058
2059 return 0;
2060}
2061
2062
Anton Blanchard2f251942006-03-27 11:46:18 +11002063int validate_sp(unsigned long sp, struct task_struct *p,
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002064 unsigned long nbytes)
2065{
Al Viro0cec6fd2006-01-12 01:06:02 -08002066 unsigned long stack_page = (unsigned long)task_stack_page(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002067
Christophe Leroya7916a12019-01-31 10:09:00 +00002068 if (sp < THREAD_SIZE)
2069 return 0;
2070
2071 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002072 return 1;
2073
Nicholas Piggina2e36682020-03-25 20:41:44 +10002074 if (valid_irq_stack(sp, p, nbytes))
2075 return 1;
2076
2077 return valid_emergency_stack(sp, p, nbytes);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002078}
2079
Anton Blanchard2f251942006-03-27 11:46:18 +11002080EXPORT_SYMBOL(validate_sp);
2081
Christophe Leroy018cce32019-01-31 10:08:52 +00002082static unsigned long __get_wchan(struct task_struct *p)
Paul Mackerras06d67d52005-10-10 22:29:05 +10002083{
2084 unsigned long ip, sp;
2085 int count = 0;
2086
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02002087 if (!p || p == current || task_is_running(p))
Paul Mackerras06d67d52005-10-10 22:29:05 +10002088 return 0;
2089
2090 sp = p->thread.ksp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002091 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10002092 return 0;
2093
2094 do {
2095 sp = *(unsigned long *)sp;
Kautuk Consul4ca360f2016-04-19 15:48:21 +05302096 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD) ||
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02002097 task_is_running(p))
Paul Mackerras06d67d52005-10-10 22:29:05 +10002098 return 0;
2099 if (count > 0) {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002100 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10002101 if (!in_sched_functions(ip))
2102 return ip;
2103 }
2104 } while (count++ < 16);
2105 return 0;
2106}
Paul Mackerras06d67d52005-10-10 22:29:05 +10002107
Christophe Leroy018cce32019-01-31 10:08:52 +00002108unsigned long get_wchan(struct task_struct *p)
2109{
2110 unsigned long ret;
2111
2112 if (!try_get_task_stack(p))
2113 return 0;
2114
2115 ret = __get_wchan(p);
2116
2117 put_task_stack(p);
2118
2119 return ret;
2120}
2121
Johannes Bergc4d04be2008-11-20 03:24:07 +00002122static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002123
Dmitry Safonov9cb8f062020-06-08 21:32:29 -07002124void show_stack(struct task_struct *tsk, unsigned long *stack,
2125 const char *loglvl)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002126{
Paul Mackerras06d67d52005-10-10 22:29:05 +10002127 unsigned long sp, ip, lr, newsp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002128 int count = 0;
Paul Mackerras06d67d52005-10-10 22:29:05 +10002129 int firstframe = 1;
Naveen N. Rao7c1bb6b2019-09-05 23:50:30 +05302130 unsigned long ret_addr;
2131 int ftrace_idx = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002132
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002133 if (tsk == NULL)
2134 tsk = current;
Christophe Leroy018cce32019-01-31 10:08:52 +00002135
2136 if (!try_get_task_stack(tsk))
2137 return;
2138
2139 sp = (unsigned long) stack;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002140 if (sp == 0) {
2141 if (tsk == current)
Michael Ellerman3d13e832020-02-20 22:51:37 +11002142 sp = current_stack_frame();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002143 else
2144 sp = tsk->thread.ksp;
2145 }
2146
Paul Mackerras06d67d52005-10-10 22:29:05 +10002147 lr = 0;
Dmitry Safonovb9677a82020-06-08 21:31:14 -07002148 printk("%sCall Trace:\n", loglvl);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002149 do {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002150 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
Christophe Leroy018cce32019-01-31 10:08:52 +00002151 break;
Paul Mackerras06d67d52005-10-10 22:29:05 +10002152
2153 stack = (unsigned long *) sp;
2154 newsp = stack[0];
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002155 ip = stack[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10002156 if (!firstframe || ip != lr) {
Dmitry Safonovb9677a82020-06-08 21:31:14 -07002157 printk("%s["REG"] ["REG"] %pS",
2158 loglvl, sp, ip, (void *)ip);
Naveen N. Rao7c1bb6b2019-09-05 23:50:30 +05302159 ret_addr = ftrace_graph_ret_addr(current,
2160 &ftrace_idx, ip, stack);
2161 if (ret_addr != ip)
2162 pr_cont(" (%pS)", (void *)ret_addr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10002163 if (firstframe)
Michael Ellerman9a1f4902016-11-02 22:20:46 +11002164 pr_cont(" (unreliable)");
2165 pr_cont("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002166 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10002167 firstframe = 0;
2168
2169 /*
2170 * See if this is an exception frame.
2171 * We look for the "regshere" marker in the current frame.
2172 */
Michael Ellermane3de1e22021-02-10 00:59:20 +11002173 if (validate_sp(sp, tsk, STACK_FRAME_WITH_PT_REGS)
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002174 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10002175 struct pt_regs *regs = (struct pt_regs *)
2176 (sp + STACK_FRAME_OVERHEAD);
Nicholas Pigginbf13718b2020-11-07 12:33:05 +10002177
Paul Mackerras06d67d52005-10-10 22:29:05 +10002178 lr = regs->link;
Nicholas Pigginbf13718b2020-11-07 12:33:05 +10002179 printk("%s--- interrupt: %lx at %pS\n",
2180 loglvl, regs->trap, (void *)regs->nip);
2181 __show_regs(regs);
2182 printk("%s--- interrupt: %lx\n",
2183 loglvl, regs->trap);
2184
Paul Mackerras06d67d52005-10-10 22:29:05 +10002185 firstframe = 1;
2186 }
2187
2188 sp = newsp;
2189 } while (count++ < kstack_depth_to_print);
Christophe Leroy018cce32019-01-31 10:08:52 +00002190
2191 put_task_stack(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002192}
Paul Mackerras06d67d52005-10-10 22:29:05 +10002193
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002194#ifdef CONFIG_PPC64
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002195/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10002196void notrace __ppc64_runlatch_on(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002197{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002198 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002199
Nicholas Piggind1d0d5f2017-08-12 02:39:07 +10002200 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2201 /*
2202 * Least significant bit (RUN) is the only writable bit of
2203 * the CTRL register, so we can avoid mfspr. 2.06 is not the
2204 * earliest ISA where this is the case, but it's convenient.
2205 */
2206 mtspr(SPRN_CTRLT, CTRL_RUNLATCH);
2207 } else {
2208 unsigned long ctrl;
2209
2210 /*
2211 * Some architectures (e.g., Cell) have writable fields other
2212 * than RUN, so do the read-modify-write.
2213 */
2214 ctrl = mfspr(SPRN_CTRLF);
2215 ctrl |= CTRL_RUNLATCH;
2216 mtspr(SPRN_CTRLT, ctrl);
2217 }
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002218
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10002219 ti->local_flags |= _TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002220}
2221
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002222/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10002223void notrace __ppc64_runlatch_off(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002224{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002225 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002226
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10002227 ti->local_flags &= ~_TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002228
Nicholas Piggind1d0d5f2017-08-12 02:39:07 +10002229 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2230 mtspr(SPRN_CTRLT, 0);
2231 } else {
2232 unsigned long ctrl;
2233
2234 ctrl = mfspr(SPRN_CTRLF);
2235 ctrl &= ~CTRL_RUNLATCH;
2236 mtspr(SPRN_CTRLT, ctrl);
2237 }
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002238}
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002239#endif /* CONFIG_PPC64 */
Benjamin Herrenschmidtf6a61682008-04-18 16:56:17 +10002240
Anton Blanchardd8390882009-02-22 01:50:03 +00002241unsigned long arch_align_stack(unsigned long sp)
2242{
2243 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2244 sp -= get_random_int() & ~PAGE_MASK;
2245 return sp & ~0xf;
2246}
Anton Blanchard912f9ee2009-02-22 01:50:04 +00002247
2248static inline unsigned long brk_rnd(void)
2249{
2250 unsigned long rnd = 0;
2251
2252 /* 8MB for 32bit, 1GB for 64bit */
2253 if (is_32bit_task())
Daniel Cashman5ef11c32016-02-26 15:19:37 -08002254 rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT)));
Anton Blanchard912f9ee2009-02-22 01:50:04 +00002255 else
Daniel Cashman5ef11c32016-02-26 15:19:37 -08002256 rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT)));
Anton Blanchard912f9ee2009-02-22 01:50:04 +00002257
2258 return rnd << PAGE_SHIFT;
2259}
2260
2261unsigned long arch_randomize_brk(struct mm_struct *mm)
2262{
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00002263 unsigned long base = mm->brk;
2264 unsigned long ret;
2265
Michael Ellerman4e003742017-10-19 15:08:43 +11002266#ifdef CONFIG_PPC_BOOK3S_64
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00002267 /*
2268 * If we are using 1TB segments and we are allowed to randomise
2269 * the heap, we can put it above 1TB so it is backed by a 1TB
2270 * segment. Otherwise the heap will be in the bottom 1TB
2271 * which always uses 256MB segments and this may result in a
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10002272 * performance penalty. We don't need to worry about radix. For
2273 * radix, mmu_highuser_ssize remains unchanged from 256MB.
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00002274 */
2275 if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
2276 base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
2277#endif
2278
2279 ret = PAGE_ALIGN(base + brk_rnd());
Anton Blanchard912f9ee2009-02-22 01:50:04 +00002280
2281 if (ret < mm->brk)
2282 return mm->brk;
2283
2284 return ret;
2285}
Anton Blanchard501cb162009-02-22 01:50:07 +00002286