blob: 0fbda89cd1bb53b43024953469d9c39490dfeb5b [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>
K.Prasad5aae8a52010-06-15 11:35:19 +053037#include <linux/hw_breakpoint.h>
Anton Blanchard7b051f62014-10-13 20:27:15 +110038#include <linux/uaccess.h>
Ram Pai06bb53b2018-01-18 17:50:31 -080039#include <linux/pkeys.h>
Christophe Leroyfb2d9502018-10-06 16:51:14 +000040#include <linux/seq_buf.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100041
Nicholas Piggin3a965702021-01-30 23:08:38 +100042#include <asm/interrupt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100043#include <asm/io.h>
44#include <asm/processor.h>
45#include <asm/mmu.h>
Michael Ellerman76032de2005-11-07 13:12:03 +110046#include <asm/machdep.h>
Paul Mackerrasc6622f62006-02-24 10:06:59 +110047#include <asm/time.h>
David Howellsae3a197e2012-03-28 18:30:02 +010048#include <asm/runlatch.h>
Arnd Bergmanna7f31842006-03-23 00:00:08 +010049#include <asm/syscalls.h>
David Howellsae3a197e2012-03-28 18:30:02 +010050#include <asm/switch_to.h>
Michael Neulingfb096922013-02-13 16:21:37 +000051#include <asm/tm.h>
David Howellsae3a197e2012-03-28 18:30:02 +010052#include <asm/debug.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100053#ifdef CONFIG_PPC64
54#include <asm/firmware.h>
Madhavan Srinivasanc2e480b2017-12-20 09:25:42 +053055#include <asm/hw_irq.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100056#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +110057#include <asm/code-patching.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110058#include <asm/exec.h>
Michael Ellerman5d31a962016-03-24 22:04:04 +110059#include <asm/livepatch.h>
Kevin Haob92a2262016-07-23 14:42:40 +053060#include <asm/cpu_has_feature.h>
Daniel Axtens0545d542016-09-06 15:32:43 +100061#include <asm/asm-prototypes.h>
Christophe Leroyc9386bf2018-10-09 16:46:25 +110062#include <asm/stacktrace.h>
Michael Neulingc1fe190c2019-04-01 17:03:12 +110063#include <asm/hw_breakpoint.h>
Michael Ellerman5d31a962016-03-24 22:04:04 +110064
Luis Machadod6a61bf2008-07-24 02:10:41 +100065#include <linux/kprobes.h>
66#include <linux/kdebug.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100067
Michael Neuling8b3c34c2013-02-13 16:21:32 +000068/* Transactional Memory debug */
69#ifdef TM_DEBUG_SW
70#define TM_DEBUG(x...) printk(KERN_INFO x)
71#else
72#define TM_DEBUG(x...) do { } while(0)
73#endif
74
Paul Mackerras14cf11a2005-09-26 16:04:21 +100075extern unsigned long _get_SP(void);
76
Paul Mackerrasd31626f2014-01-13 15:56:29 +110077#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Michael Ellerman54820532017-10-12 21:17:18 +110078/*
79 * Are we running in "Suspend disabled" mode? If so we have to block any
80 * sigreturn that would get us into suspended state, and we also warn in some
81 * other paths that we should never reach with suspend disabled.
82 */
83bool tm_suspend_disabled __ro_after_init = false;
84
Anton Blanchardb86fd2b2015-10-29 11:43:58 +110085static void check_if_tm_restore_required(struct task_struct *tsk)
Paul Mackerrasd31626f2014-01-13 15:56:29 +110086{
87 /*
88 * If we are saving the current thread's registers, and the
89 * thread is in a transactional state, set the TIF_RESTORE_TM
90 * bit so that we know to restore the registers before
91 * returning to userspace.
92 */
93 if (tsk == current && tsk->thread.regs &&
94 MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
95 !test_thread_flag(TIF_RESTORE_TM)) {
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +100096 regs_set_return_msr(&tsk->thread.ckpt_regs,
97 tsk->thread.regs->msr);
Paul Mackerrasd31626f2014-01-13 15:56:29 +110098 set_thread_flag(TIF_RESTORE_TM);
99 }
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100100}
Cyril Burdc16b552016-09-23 16:18:08 +1000101
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100102#else
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100103static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100104#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
105
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100106bool strict_msr_control;
107EXPORT_SYMBOL(strict_msr_control);
108
109static int __init enable_strict_msr_control(char *str)
110{
111 strict_msr_control = true;
112 pr_info("Enabling strict facility control\n");
113
114 return 0;
115}
116early_param("ppc_strict_facility_enable", enable_strict_msr_control);
117
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000118/* notrace because it's called by restore_math */
119unsigned long notrace msr_check_and_set(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100120{
121 unsigned long oldmsr = mfmsr();
122 unsigned long newmsr;
123
124 newmsr = oldmsr | bits;
125
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100126 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
127 newmsr |= MSR_VSX;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100128
129 if (oldmsr != newmsr)
130 mtmsr_isync(newmsr);
Cyril Bur3cee0702016-09-23 16:18:10 +1000131
132 return newmsr;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100133}
Simon Guod1c72112018-05-23 15:01:44 +0800134EXPORT_SYMBOL_GPL(msr_check_and_set);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100135
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000136/* notrace because it's called by restore_math */
137void notrace __msr_check_and_clear(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100138{
139 unsigned long oldmsr = mfmsr();
140 unsigned long newmsr;
141
142 newmsr = oldmsr & ~bits;
143
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100144 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
145 newmsr &= ~MSR_VSX;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100146
147 if (oldmsr != newmsr)
148 mtmsr_isync(newmsr);
149}
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100150EXPORT_SYMBOL(__msr_check_and_clear);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100151
Kevin Hao037f0ee2013-07-14 17:02:05 +0800152#ifdef CONFIG_PPC_FPU
Mathieu Malaterre1cdf0392018-02-25 18:22:23 +0100153static void __giveup_fpu(struct task_struct *tsk)
Cyril Bur87924682016-02-29 17:53:49 +1100154{
Anton Blanchard8eb98032016-05-29 22:03:50 +1000155 unsigned long msr;
156
Cyril Bur87924682016-02-29 17:53:49 +1100157 save_fpu(tsk);
Anton Blanchard8eb98032016-05-29 22:03:50 +1000158 msr = tsk->thread.regs->msr;
Mark Cave-Aylandfe1ef6b2019-02-08 14:33:19 +0000159 msr &= ~(MSR_FP|MSR_FE0|MSR_FE1);
Cyril Bur87924682016-02-29 17:53:49 +1100160 if (cpu_has_feature(CPU_FTR_VSX))
Anton Blanchard8eb98032016-05-29 22:03:50 +1000161 msr &= ~MSR_VSX;
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +1000162 regs_set_return_msr(tsk->thread.regs, msr);
Cyril Bur87924682016-02-29 17:53:49 +1100163}
164
Anton Blanchard98da5812015-10-29 11:44:01 +1100165void giveup_fpu(struct task_struct *tsk)
166{
Anton Blanchard98da5812015-10-29 11:44:01 +1100167 check_if_tm_restore_required(tsk);
168
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100169 msr_check_and_set(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100170 __giveup_fpu(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100171 msr_check_and_clear(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100172}
173EXPORT_SYMBOL(giveup_fpu);
174
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000175/*
176 * Make sure the floating-point register state in the
177 * the thread_struct is up to date for task tsk.
178 */
179void flush_fp_to_thread(struct task_struct *tsk)
180{
181 if (tsk->thread.regs) {
182 /*
183 * We need to disable preemption here because if we didn't,
184 * another process could get scheduled after the regs->msr
185 * test but before we have finished saving the FP registers
186 * to the thread_struct. That process could take over the
187 * FPU, and then when we get scheduled again we would store
188 * bogus values for the remaining FP registers.
189 */
190 preempt_disable();
191 if (tsk->thread.regs->msr & MSR_FP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000192 /*
193 * This should only ever be called for current or
194 * for a stopped child process. Since we save away
Anton Blanchardaf1bbc32015-10-29 11:43:57 +1100195 * the FP register state on context switch,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000196 * there is something wrong if a stopped child appears
197 * to still have its FP state in the CPU registers.
198 */
199 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100200 giveup_fpu(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000201 }
202 preempt_enable();
203 }
204}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000205EXPORT_SYMBOL_GPL(flush_fp_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000206
207void enable_kernel_fp(void)
208{
Cyril Bure909fb82016-09-23 16:18:11 +1000209 unsigned long cpumsr;
210
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000211 WARN_ON(preemptible());
212
Cyril Bure909fb82016-09-23 16:18:11 +1000213 cpumsr = msr_check_and_set(MSR_FP);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100214
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100215 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
216 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000217 /*
218 * If a thread has already been reclaimed then the
219 * checkpointed registers are on the CPU but have definitely
220 * been saved by the reclaim code. Don't need to and *cannot*
221 * giveup as this would save to the 'live' structure not the
222 * checkpointed structure.
223 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300224 if (!MSR_TM_ACTIVE(cpumsr) &&
225 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000226 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100227 __giveup_fpu(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100228 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000229}
230EXPORT_SYMBOL(enable_kernel_fp);
Christophe Leroyc83c1922020-08-17 05:47:58 +0000231#else
232static inline void __giveup_fpu(struct task_struct *tsk) { }
Anton Blanchardd1e1cf22015-10-29 11:44:11 +1100233#endif /* CONFIG_PPC_FPU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000234
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000235#ifdef CONFIG_ALTIVEC
Cyril Bur6f515d82016-02-29 17:53:50 +1100236static void __giveup_altivec(struct task_struct *tsk)
237{
Anton Blanchard8eb98032016-05-29 22:03:50 +1000238 unsigned long msr;
239
Cyril Bur6f515d82016-02-29 17:53:50 +1100240 save_altivec(tsk);
Anton Blanchard8eb98032016-05-29 22:03:50 +1000241 msr = tsk->thread.regs->msr;
242 msr &= ~MSR_VEC;
Cyril Bur6f515d82016-02-29 17:53:50 +1100243 if (cpu_has_feature(CPU_FTR_VSX))
Anton Blanchard8eb98032016-05-29 22:03:50 +1000244 msr &= ~MSR_VSX;
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +1000245 regs_set_return_msr(tsk->thread.regs, msr);
Cyril Bur6f515d82016-02-29 17:53:50 +1100246}
247
Anton Blanchard98da5812015-10-29 11:44:01 +1100248void giveup_altivec(struct task_struct *tsk)
249{
Anton Blanchard98da5812015-10-29 11:44:01 +1100250 check_if_tm_restore_required(tsk);
251
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100252 msr_check_and_set(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100253 __giveup_altivec(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100254 msr_check_and_clear(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100255}
256EXPORT_SYMBOL(giveup_altivec);
257
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000258void enable_kernel_altivec(void)
259{
Cyril Bure909fb82016-09-23 16:18:11 +1000260 unsigned long cpumsr;
261
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000262 WARN_ON(preemptible());
263
Cyril Bure909fb82016-09-23 16:18:11 +1000264 cpumsr = msr_check_and_set(MSR_VEC);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100265
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100266 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
267 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000268 /*
269 * If a thread has already been reclaimed then the
270 * checkpointed registers are on the CPU but have definitely
271 * been saved by the reclaim code. Don't need to and *cannot*
272 * giveup as this would save to the 'live' structure not the
273 * checkpointed structure.
274 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300275 if (!MSR_TM_ACTIVE(cpumsr) &&
276 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000277 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100278 __giveup_altivec(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100279 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000280}
281EXPORT_SYMBOL(enable_kernel_altivec);
282
283/*
284 * Make sure the VMX/Altivec register state in the
285 * the thread_struct is up to date for task tsk.
286 */
287void flush_altivec_to_thread(struct task_struct *tsk)
288{
289 if (tsk->thread.regs) {
290 preempt_disable();
291 if (tsk->thread.regs->msr & MSR_VEC) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000292 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100293 giveup_altivec(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000294 }
295 preempt_enable();
296 }
297}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000298EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000299#endif /* CONFIG_ALTIVEC */
300
Michael Neulingce48b212008-06-25 14:07:18 +1000301#ifdef CONFIG_VSX
Cyril Burbf6a4d52016-02-29 17:53:51 +1100302static void __giveup_vsx(struct task_struct *tsk)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100303{
Benjamin Herrenschmidtdc801082017-08-16 16:01:17 +1000304 unsigned long msr = tsk->thread.regs->msr;
305
306 /*
Julia Lawall1fd02f62022-04-30 20:56:54 +0200307 * We should never be setting MSR_VSX without also setting
Benjamin Herrenschmidtdc801082017-08-16 16:01:17 +1000308 * MSR_FP and MSR_VEC
309 */
310 WARN_ON((msr & MSR_VSX) && !((msr & MSR_FP) && (msr & MSR_VEC)));
311
312 /* __giveup_fpu will clear MSR_VSX */
313 if (msr & MSR_FP)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100314 __giveup_fpu(tsk);
Benjamin Herrenschmidtdc801082017-08-16 16:01:17 +1000315 if (msr & MSR_VEC)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100316 __giveup_altivec(tsk);
Cyril Burbf6a4d52016-02-29 17:53:51 +1100317}
318
319static void giveup_vsx(struct task_struct *tsk)
320{
321 check_if_tm_restore_required(tsk);
322
323 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100324 __giveup_vsx(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100325 msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100326}
Cyril Burbf6a4d52016-02-29 17:53:51 +1100327
Michael Neulingce48b212008-06-25 14:07:18 +1000328void enable_kernel_vsx(void)
329{
Cyril Bure909fb82016-09-23 16:18:11 +1000330 unsigned long cpumsr;
331
Michael Neulingce48b212008-06-25 14:07:18 +1000332 WARN_ON(preemptible());
333
Cyril Bure909fb82016-09-23 16:18:11 +1000334 cpumsr = msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100335
Benjamin Herrenschmidt5a69aec2017-08-16 16:01:14 +1000336 if (current->thread.regs &&
337 (current->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP))) {
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100338 check_if_tm_restore_required(current);
Cyril Bure909fb82016-09-23 16:18:11 +1000339 /*
340 * If a thread has already been reclaimed then the
341 * checkpointed registers are on the CPU but have definitely
342 * been saved by the reclaim code. Don't need to and *cannot*
343 * giveup as this would save to the 'live' structure not the
344 * checkpointed structure.
345 */
Breno Leitao5c784c82018-08-16 14:21:07 -0300346 if (!MSR_TM_ACTIVE(cpumsr) &&
347 MSR_TM_ACTIVE(current->thread.regs->msr))
Cyril Bure909fb82016-09-23 16:18:11 +1000348 return;
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100349 __giveup_vsx(current);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100350 }
Michael Neulingce48b212008-06-25 14:07:18 +1000351}
352EXPORT_SYMBOL(enable_kernel_vsx);
Michael Neulingce48b212008-06-25 14:07:18 +1000353
354void flush_vsx_to_thread(struct task_struct *tsk)
355{
356 if (tsk->thread.regs) {
357 preempt_disable();
Benjamin Herrenschmidt5a69aec2017-08-16 16:01:14 +1000358 if (tsk->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP)) {
Michael Neulingce48b212008-06-25 14:07:18 +1000359 BUG_ON(tsk != current);
Michael Neulingce48b212008-06-25 14:07:18 +1000360 giveup_vsx(tsk);
361 }
362 preempt_enable();
363 }
364}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000365EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
Michael Neulingce48b212008-06-25 14:07:18 +1000366#endif /* CONFIG_VSX */
367
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000368#ifdef CONFIG_SPE
Anton Blanchard98da5812015-10-29 11:44:01 +1100369void giveup_spe(struct task_struct *tsk)
370{
Anton Blanchard98da5812015-10-29 11:44:01 +1100371 check_if_tm_restore_required(tsk);
372
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100373 msr_check_and_set(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100374 __giveup_spe(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100375 msr_check_and_clear(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100376}
377EXPORT_SYMBOL(giveup_spe);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000378
379void enable_kernel_spe(void)
380{
381 WARN_ON(preemptible());
382
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100383 msr_check_and_set(MSR_SPE);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100384
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100385 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
386 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100387 __giveup_spe(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100388 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000389}
390EXPORT_SYMBOL(enable_kernel_spe);
391
392void flush_spe_to_thread(struct task_struct *tsk)
393{
394 if (tsk->thread.regs) {
395 preempt_disable();
396 if (tsk->thread.regs->msr & MSR_SPE) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000397 BUG_ON(tsk != current);
yu liu685659e2011-06-14 18:34:25 -0500398 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
Kumar Gala0ee6c152007-08-28 21:15:53 -0500399 giveup_spe(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000400 }
401 preempt_enable();
402 }
403}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000404#endif /* CONFIG_SPE */
405
Anton Blanchardc2085052015-10-29 11:44:08 +1100406static unsigned long msr_all_available;
407
408static int __init init_msr_all_available(void)
409{
Christophe Leroyc83c1922020-08-17 05:47:58 +0000410 if (IS_ENABLED(CONFIG_PPC_FPU))
411 msr_all_available |= MSR_FP;
Anton Blanchardc2085052015-10-29 11:44:08 +1100412 if (cpu_has_feature(CPU_FTR_ALTIVEC))
413 msr_all_available |= MSR_VEC;
Anton Blanchardc2085052015-10-29 11:44:08 +1100414 if (cpu_has_feature(CPU_FTR_VSX))
415 msr_all_available |= MSR_VSX;
Anton Blanchardc2085052015-10-29 11:44:08 +1100416 if (cpu_has_feature(CPU_FTR_SPE))
417 msr_all_available |= MSR_SPE;
Anton Blanchardc2085052015-10-29 11:44:08 +1100418
419 return 0;
420}
421early_initcall(init_msr_all_available);
422
423void giveup_all(struct task_struct *tsk)
424{
425 unsigned long usermsr;
426
427 if (!tsk->thread.regs)
428 return;
429
Gustavo Romero8205d5d2019-09-04 00:55:27 -0400430 check_if_tm_restore_required(tsk);
431
Anton Blanchardc2085052015-10-29 11:44:08 +1100432 usermsr = tsk->thread.regs->msr;
433
434 if ((usermsr & msr_all_available) == 0)
435 return;
436
437 msr_check_and_set(msr_all_available);
438
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000439 WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
440
Anton Blanchardc2085052015-10-29 11:44:08 +1100441 if (usermsr & MSR_FP)
442 __giveup_fpu(tsk);
Anton Blanchardc2085052015-10-29 11:44:08 +1100443 if (usermsr & MSR_VEC)
444 __giveup_altivec(tsk);
Anton Blanchardc2085052015-10-29 11:44:08 +1100445 if (usermsr & MSR_SPE)
446 __giveup_spe(tsk);
Anton Blanchardc2085052015-10-29 11:44:08 +1100447
448 msr_check_and_clear(msr_all_available);
449}
450EXPORT_SYMBOL(giveup_all);
451
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000452#ifdef CONFIG_PPC_BOOK3S_64
453#ifdef CONFIG_PPC_FPU
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000454static bool should_restore_fp(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000455{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000456 if (current->thread.load_fp) {
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000457 current->thread.load_fp++;
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000458 return true;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000459 }
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000460 return false;
461}
462
463static void do_restore_fp(void)
464{
465 load_fp_state(&current->thread.fp_state);
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000466}
467#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000468static bool should_restore_fp(void) { return false; }
469static void do_restore_fp(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000470#endif /* CONFIG_PPC_FPU */
471
472#ifdef CONFIG_ALTIVEC
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000473static bool should_restore_altivec(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000474{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000475 if (cpu_has_feature(CPU_FTR_ALTIVEC) && (current->thread.load_vec)) {
476 current->thread.load_vec++;
477 return true;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000478 }
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000479 return false;
480}
481
482static void do_restore_altivec(void)
483{
484 load_vr_state(&current->thread.vr_state);
485 current->thread.used_vr = 1;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000486}
487#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000488static bool should_restore_altivec(void) { return false; }
489static void do_restore_altivec(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000490#endif /* CONFIG_ALTIVEC */
491
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000492static bool should_restore_vsx(void)
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000493{
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000494 if (cpu_has_feature(CPU_FTR_VSX))
495 return true;
496 return false;
497}
Christophe Leroy80739c22020-08-17 05:47:55 +0000498#ifdef CONFIG_VSX
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000499static void do_restore_vsx(void)
500{
501 current->thread.used_vsr = 1;
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000502}
503#else
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000504static void do_restore_vsx(void) { }
Nicholas Piggin6cc0c162020-02-26 03:35:37 +1000505#endif /* CONFIG_VSX */
506
Nicholas Piggine2b36d52019-05-02 15:21:07 +1000507/*
508 * The exception exit path calls restore_math() with interrupts hard disabled
509 * but the soft irq state not "reconciled". ftrace code that calls
510 * local_irq_save/restore causes warnings.
511 *
512 * Rather than complicate the exit path, just don't trace restore_math. This
513 * could be done by having ftrace entry code check for this un-reconciled
514 * condition where MSR[EE]=0 and PACA_IRQ_HARD_DIS is not set, and
515 * temporarily fix it up for the duration of the ftrace call.
516 */
517void notrace restore_math(struct pt_regs *regs)
Cyril Bur70fe3d92016-02-29 17:53:47 +1100518{
519 unsigned long msr;
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000520 unsigned long new_msr = 0;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100521
522 msr = regs->msr;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100523
524 /*
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000525 * new_msr tracks the facilities that are to be restored. Only reload
526 * if the bit is not set in the user MSR (if it is set, the registers
527 * are live for the user thread).
Cyril Bur70fe3d92016-02-29 17:53:47 +1100528 */
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000529 if ((!(msr & MSR_FP)) && should_restore_fp())
Michael Ellermanb91eb512020-08-25 19:34:24 +1000530 new_msr |= MSR_FP;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100531
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000532 if ((!(msr & MSR_VEC)) && should_restore_altivec())
533 new_msr |= MSR_VEC;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100534
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000535 if ((!(msr & MSR_VSX)) && should_restore_vsx()) {
536 if (((msr | new_msr) & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC))
537 new_msr |= MSR_VSX;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100538 }
539
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000540 if (new_msr) {
Michael Ellermanb91eb512020-08-25 19:34:24 +1000541 unsigned long fpexc_mode = 0;
542
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000543 msr_check_and_set(new_msr);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100544
Michael Ellermanb91eb512020-08-25 19:34:24 +1000545 if (new_msr & MSR_FP) {
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000546 do_restore_fp();
547
Michael Ellermanb91eb512020-08-25 19:34:24 +1000548 // This also covers VSX, because VSX implies FP
549 fpexc_mode = current->thread.fpexc_mode;
550 }
551
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000552 if (new_msr & MSR_VEC)
553 do_restore_altivec();
554
555 if (new_msr & MSR_VSX)
556 do_restore_vsx();
557
558 msr_check_and_clear(new_msr);
559
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +1000560 regs_set_return_msr(regs, regs->msr | new_msr | fpexc_mode);
Nicholas Piggin01eb0182020-06-24 09:41:38 +1000561 }
Cyril Bur70fe3d92016-02-29 17:53:47 +1100562}
Christophe Leroy60d62bf2020-08-17 05:46:45 +0000563#endif /* CONFIG_PPC_BOOK3S_64 */
Cyril Bur70fe3d92016-02-29 17:53:47 +1100564
Mathieu Malaterre1cdf0392018-02-25 18:22:23 +0100565static void save_all(struct task_struct *tsk)
Cyril Burde2a20a2016-02-29 17:53:48 +1100566{
567 unsigned long usermsr;
568
569 if (!tsk->thread.regs)
570 return;
571
572 usermsr = tsk->thread.regs->msr;
573
574 if ((usermsr & msr_all_available) == 0)
575 return;
576
577 msr_check_and_set(msr_all_available);
578
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000579 WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
Cyril Burde2a20a2016-02-29 17:53:48 +1100580
Benjamin Herrenschmidt96c79b62017-08-16 16:01:18 +1000581 if (usermsr & MSR_FP)
582 save_fpu(tsk);
583
584 if (usermsr & MSR_VEC)
585 save_altivec(tsk);
Cyril Burde2a20a2016-02-29 17:53:48 +1100586
587 if (usermsr & MSR_SPE)
588 __giveup_spe(tsk);
589
590 msr_check_and_clear(msr_all_available);
591}
592
Anton Blanchard579e6332015-10-29 11:44:09 +1100593void flush_all_to_thread(struct task_struct *tsk)
594{
595 if (tsk->thread.regs) {
596 preempt_disable();
597 BUG_ON(tsk != current);
Anton Blanchard579e6332015-10-29 11:44:09 +1100598#ifdef CONFIG_SPE
599 if (tsk->thread.regs->msr & MSR_SPE)
600 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
601#endif
Felipe Rechiae9013782018-10-24 10:57:22 -0300602 save_all(tsk);
Anton Blanchard579e6332015-10-29 11:44:09 +1100603
604 preempt_enable();
605 }
606}
607EXPORT_SYMBOL(flush_all_to_thread);
608
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000609#ifdef CONFIG_PPC_ADV_DEBUG_REGS
610void do_send_trap(struct pt_regs *regs, unsigned long address,
Eric W. Biederman47355042018-01-16 16:12:38 -0600611 unsigned long error_code, int breakpt)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000612{
Eric W. Biederman47355042018-01-16 16:12:38 -0600613 current->thread.trap_nr = TRAP_HWBKPT;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000614 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
615 11, SIGSEGV) == NOTIFY_STOP)
616 return;
617
618 /* Deliver the signal to userspace */
Eric W. Biedermanf71dd7d2018-01-22 14:37:25 -0600619 force_sig_ptrace_errno_trap(breakpt, /* breakpoint or watchpoint id */
620 (void __user *)address);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000621}
622#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Ravi Bangoria5b905d72020-09-02 09:59:42 +0530623
624static void do_break_handler(struct pt_regs *regs)
625{
626 struct arch_hw_breakpoint null_brk = {0};
627 struct arch_hw_breakpoint *info;
Christophe Leroyc545b9f2021-11-29 18:49:38 +0100628 ppc_inst_t instr = ppc_inst(0);
Ravi Bangoria5b905d72020-09-02 09:59:42 +0530629 int type = 0;
630 int size = 0;
631 unsigned long ea;
632 int i;
633
634 /*
635 * If underneath hw supports only one watchpoint, we know it
636 * caused exception. 8xx also falls into this category.
637 */
638 if (nr_wp_slots() == 1) {
639 __set_breakpoint(0, &null_brk);
640 current->thread.hw_brk[0] = null_brk;
641 current->thread.hw_brk[0].flags |= HW_BRK_FLAG_DISABLED;
642 return;
643 }
644
Julia Lawall1fd02f62022-04-30 20:56:54 +0200645 /* Otherwise find out which DAWR caused exception and disable it. */
Ravi Bangoria5b905d72020-09-02 09:59:42 +0530646 wp_get_instr_detail(regs, &instr, &type, &size, &ea);
647
648 for (i = 0; i < nr_wp_slots(); i++) {
649 info = &current->thread.hw_brk[i];
650 if (!info->address)
651 continue;
652
653 if (wp_check_constraints(regs, instr, ea, type, size, info)) {
654 __set_breakpoint(i, &null_brk);
655 current->thread.hw_brk[i] = null_brk;
656 current->thread.hw_brk[i].flags |= HW_BRK_FLAG_DISABLED;
657 }
658 }
659}
660
Nicholas Piggin3a965702021-01-30 23:08:38 +1000661DEFINE_INTERRUPT_HANDLER(do_break)
Luis Machadod6a61bf2008-07-24 02:10:41 +1000662{
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000663 current->thread.trap_nr = TRAP_HWBKPT;
Nicholas Piggin18722ec2021-01-30 23:08:18 +1000664 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, regs->dsisr,
Luis Machadod6a61bf2008-07-24 02:10:41 +1000665 11, SIGSEGV) == NOTIFY_STOP)
666 return;
667
Michael Neuling9422de32012-12-20 14:06:44 +0000668 if (debugger_break_match(regs))
Luis Machadod6a61bf2008-07-24 02:10:41 +1000669 return;
670
Ravi Bangoria5b905d72020-09-02 09:59:42 +0530671 /*
672 * We reach here only when watchpoint exception is generated by ptrace
673 * event (or hw is buggy!). Now if CONFIG_HAVE_HW_BREAKPOINT is set,
674 * watchpoint is already handled by hw_breakpoint_handler() so we don't
675 * have to do anything. But when CONFIG_HAVE_HW_BREAKPOINT is not set,
676 * we need to manually handle the watchpoint here.
677 */
678 if (!IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT))
679 do_break_handler(regs);
680
Luis Machadod6a61bf2008-07-24 02:10:41 +1000681 /* Deliver the signal to userspace */
Nicholas Piggin18722ec2021-01-30 23:08:18 +1000682 force_sig_fault(SIGTRAP, TRAP_HWBKPT, (void __user *)regs->dar);
Luis Machadod6a61bf2008-07-24 02:10:41 +1000683}
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000684#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000685
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530686static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk[HBP_NUM_MAX]);
Michael Ellermana2ceff52008-03-28 19:11:48 +1100687
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000688#ifdef CONFIG_PPC_ADV_DEBUG_REGS
689/*
690 * Set the debug registers back to their default "safe" values.
691 */
692static void set_debug_reg_defaults(struct thread_struct *thread)
693{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530694 thread->debug.iac1 = thread->debug.iac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000695#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530696 thread->debug.iac3 = thread->debug.iac4 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000697#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530698 thread->debug.dac1 = thread->debug.dac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000699#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530700 thread->debug.dvc1 = thread->debug.dvc2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000701#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530702 thread->debug.dbcr0 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000703#ifdef CONFIG_BOOKE
704 /*
705 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
706 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530707 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000708 DBCR1_IAC3US | DBCR1_IAC4US;
709 /*
710 * Force Data Address Compare User/Supervisor bits to be User-only
711 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
712 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530713 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000714#else
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530715 thread->debug.dbcr1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000716#endif
717}
718
Scott Woodf5f97212013-11-22 15:52:29 -0600719static void prime_debug_regs(struct debug_reg *debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000720{
Scott Wood6cecf762013-05-13 14:14:53 +0000721 /*
722 * We could have inherited MSR_DE from userspace, since
723 * it doesn't get cleared on exception entry. Make sure
724 * MSR_DE is clear before we enable any debug events.
725 */
726 mtmsr(mfmsr() & ~MSR_DE);
727
Scott Woodf5f97212013-11-22 15:52:29 -0600728 mtspr(SPRN_IAC1, debug->iac1);
729 mtspr(SPRN_IAC2, debug->iac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000730#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Scott Woodf5f97212013-11-22 15:52:29 -0600731 mtspr(SPRN_IAC3, debug->iac3);
732 mtspr(SPRN_IAC4, debug->iac4);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000733#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600734 mtspr(SPRN_DAC1, debug->dac1);
735 mtspr(SPRN_DAC2, debug->dac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000736#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Scott Woodf5f97212013-11-22 15:52:29 -0600737 mtspr(SPRN_DVC1, debug->dvc1);
738 mtspr(SPRN_DVC2, debug->dvc2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000739#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600740 mtspr(SPRN_DBCR0, debug->dbcr0);
741 mtspr(SPRN_DBCR1, debug->dbcr1);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000742#ifdef CONFIG_BOOKE
Scott Woodf5f97212013-11-22 15:52:29 -0600743 mtspr(SPRN_DBCR2, debug->dbcr2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000744#endif
745}
746/*
747 * Unless neither the old or new thread are making use of the
748 * debug registers, set the debug registers from the values
749 * stored in the new thread.
750 */
Scott Woodf5f97212013-11-22 15:52:29 -0600751void switch_booke_debug_regs(struct debug_reg *new_debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000752{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530753 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
Scott Woodf5f97212013-11-22 15:52:29 -0600754 || (new_debug->dbcr0 & DBCR0_IDM))
755 prime_debug_regs(new_debug);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000756}
Bharat Bhushan3743c9b2013-07-04 12:27:44 +0530757EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000758#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
K.Prasade0780b72011-02-10 04:44:35 +0000759#ifndef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530760static void set_breakpoint(int i, struct arch_hw_breakpoint *brk)
Christophe Leroyb5ac51d2018-07-05 16:25:05 +0000761{
762 preempt_disable();
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530763 __set_breakpoint(i, brk);
Christophe Leroyb5ac51d2018-07-05 16:25:05 +0000764 preempt_enable();
765}
766
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000767static void set_debug_reg_defaults(struct thread_struct *thread)
768{
Ravi Bangoria303e6a92020-05-14 16:47:34 +0530769 int i;
770 struct arch_hw_breakpoint null_brk = {0};
771
772 for (i = 0; i < nr_wp_slots(); i++) {
773 thread->hw_brk[i] = null_brk;
774 if (ppc_breakpoint_available())
775 set_breakpoint(i, &thread->hw_brk[i]);
776 }
777}
778
779static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
780 struct arch_hw_breakpoint *b)
781{
782 if (a->address != b->address)
783 return false;
784 if (a->type != b->type)
785 return false;
786 if (a->len != b->len)
787 return false;
788 /* no need to check hw_len. it's calculated from address and len */
789 return true;
790}
791
792static void switch_hw_breakpoint(struct task_struct *new)
793{
794 int i;
795
796 for (i = 0; i < nr_wp_slots(); i++) {
797 if (likely(hw_brk_match(this_cpu_ptr(&current_brk[i]),
798 &new->thread.hw_brk[i])))
799 continue;
800
801 __set_breakpoint(i, &new->thread.hw_brk[i]);
802 }
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000803}
K.Prasade0780b72011-02-10 04:44:35 +0000804#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000805#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
806
Michael Neuling9422de32012-12-20 14:06:44 +0000807static inline int set_dabr(struct arch_hw_breakpoint *brk)
808{
809 unsigned long dabr, dabrx;
810
811 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
812 dabrx = ((brk->type >> 3) & 0x7);
813
814 if (ppc_md.set_dabr)
815 return ppc_md.set_dabr(dabr, dabrx);
816
Christophe Leroyad3ed152020-12-04 10:12:51 +0000817 if (IS_ENABLED(CONFIG_PPC_ADV_DEBUG_REGS)) {
818 mtspr(SPRN_DAC1, dabr);
819 if (IS_ENABLED(CONFIG_PPC_47x))
820 isync();
821 return 0;
822 } else if (IS_ENABLED(CONFIG_PPC_BOOK3S)) {
823 mtspr(SPRN_DABR, dabr);
824 if (cpu_has_feature(CPU_FTR_DABRX))
825 mtspr(SPRN_DABRX, dabrx);
826 return 0;
827 } else {
828 return -EINVAL;
829 }
Michael Neuling9422de32012-12-20 14:06:44 +0000830}
831
Christophe Leroy39413ae2019-11-26 17:43:29 +0000832static inline int set_breakpoint_8xx(struct arch_hw_breakpoint *brk)
833{
834 unsigned long lctrl1 = LCTRL1_CTE_GT | LCTRL1_CTF_LT | LCTRL1_CRWE_RW |
835 LCTRL1_CRWF_RW;
836 unsigned long lctrl2 = LCTRL2_LW0EN | LCTRL2_LW0LADC | LCTRL2_SLW0EN;
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530837 unsigned long start_addr = ALIGN_DOWN(brk->address, HW_BREAKPOINT_SIZE);
838 unsigned long end_addr = ALIGN(brk->address + brk->len, HW_BREAKPOINT_SIZE);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000839
840 if (start_addr == 0)
841 lctrl2 |= LCTRL2_LW0LA_F;
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530842 else if (end_addr == 0)
Christophe Leroy39413ae2019-11-26 17:43:29 +0000843 lctrl2 |= LCTRL2_LW0LA_E;
844 else
845 lctrl2 |= LCTRL2_LW0LA_EandF;
846
847 mtspr(SPRN_LCTRL2, 0);
848
849 if ((brk->type & HW_BRK_TYPE_RDWR) == 0)
850 return 0;
851
852 if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_READ)
853 lctrl1 |= LCTRL1_CRWE_RO | LCTRL1_CRWF_RO;
854 if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_WRITE)
855 lctrl1 |= LCTRL1_CRWE_WO | LCTRL1_CRWF_WO;
856
857 mtspr(SPRN_CMPE, start_addr - 1);
Ravi Bangoriae68ef122020-05-14 16:47:37 +0530858 mtspr(SPRN_CMPF, end_addr);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000859 mtspr(SPRN_LCTRL1, lctrl1);
860 mtspr(SPRN_LCTRL2, lctrl2);
861
862 return 0;
863}
864
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530865void __set_breakpoint(int nr, struct arch_hw_breakpoint *brk)
Michael Neuling9422de32012-12-20 14:06:44 +0000866{
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530867 memcpy(this_cpu_ptr(&current_brk[nr]), brk, sizeof(*brk));
Michael Neuling9422de32012-12-20 14:06:44 +0000868
Michael Neulingc1fe190c2019-04-01 17:03:12 +1100869 if (dawr_enabled())
Nicholas Piggin252988c2018-04-01 15:50:36 +1000870 // Power8 or later
Ravi Bangoria4a8a9372020-05-14 16:47:31 +0530871 set_dawr(nr, brk);
Christophe Leroy39413ae2019-11-26 17:43:29 +0000872 else if (IS_ENABLED(CONFIG_PPC_8xx))
873 set_breakpoint_8xx(brk);
Nicholas Piggin252988c2018-04-01 15:50:36 +1000874 else if (!cpu_has_feature(CPU_FTR_ARCH_207S))
875 // Power7 or earlier
Paul Gortmaker04c32a52014-04-29 15:25:16 -0400876 set_dabr(brk);
Nicholas Piggin252988c2018-04-01 15:50:36 +1000877 else
878 // Shouldn't happen due to higher level checks
879 WARN_ON_ONCE(1);
Michael Neuling9422de32012-12-20 14:06:44 +0000880}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000881
Michael Neuling404b27d2018-03-27 15:37:17 +1100882/* Check if we have DAWR or DABR hardware */
883bool ppc_breakpoint_available(void)
884{
Michael Neulingc1fe190c2019-04-01 17:03:12 +1100885 if (dawr_enabled())
886 return true; /* POWER8 DAWR or POWER9 forced DAWR */
Michael Neuling404b27d2018-03-27 15:37:17 +1100887 if (cpu_has_feature(CPU_FTR_ARCH_207S))
888 return false; /* POWER9 with DAWR disabled */
889 /* DABR: Everything but POWER8 and POWER9 */
890 return true;
891}
892EXPORT_SYMBOL_GPL(ppc_breakpoint_available);
893
Michael Neulingfb096922013-02-13 16:21:37 +0000894#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Cyril Bur5d176f72016-09-14 18:02:16 +1000895
896static inline bool tm_enabled(struct task_struct *tsk)
897{
898 return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM);
899}
900
Cyril Buredd00b82018-02-01 12:07:46 +1100901static void tm_reclaim_thread(struct thread_struct *thr, uint8_t cause)
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100902{
Michael Neuling7f821fc2015-11-19 15:44:45 +1100903 /*
904 * Use the current MSR TM suspended bit to track if we have
905 * checkpointed state outstanding.
906 * On signal delivery, we'd normally reclaim the checkpointed
907 * state to obtain stack pointer (see:get_tm_stackpointer()).
908 * This will then directly return to userspace without going
909 * through __switch_to(). However, if the stack frame is bad,
910 * we need to exit this thread which calls __switch_to() which
911 * will again attempt to reclaim the already saved tm state.
912 * Hence we need to check that we've not already reclaimed
913 * this state.
914 * We do this using the current MSR, rather tracking it in
915 * some specific thread_struct bit, as it has the additional
Michael Ellerman027dfac2016-06-01 16:34:37 +1000916 * benefit of checking for a potential TM bad thing exception.
Michael Neuling7f821fc2015-11-19 15:44:45 +1100917 */
918 if (!MSR_TM_SUSPENDED(mfmsr()))
919 return;
920
Cyril Bur91381b92017-11-02 14:09:04 +1100921 giveup_all(container_of(thr, struct task_struct, thread));
922
Cyril Bureb5c3f12017-11-02 14:09:05 +1100923 tm_reclaim(thr, cause);
924
Michael Neulingf48e91e2017-05-08 17:16:26 +1000925 /*
926 * If we are in a transaction and FP is off then we can't have
927 * used FP inside that transaction. Hence the checkpointed
928 * state is the same as the live state. We need to copy the
929 * live state to the checkpointed state so that when the
930 * transaction is restored, the checkpointed state is correct
931 * and the aborted transaction sees the correct state. We use
932 * ckpt_regs.msr here as that's what tm_reclaim will use to
933 * determine if it's going to write the checkpointed state or
934 * not. So either this will write the checkpointed registers,
935 * or reclaim will. Similarly for VMX.
936 */
937 if ((thr->ckpt_regs.msr & MSR_FP) == 0)
938 memcpy(&thr->ckfp_state, &thr->fp_state,
939 sizeof(struct thread_fp_state));
940 if ((thr->ckpt_regs.msr & MSR_VEC) == 0)
941 memcpy(&thr->ckvr_state, &thr->vr_state,
942 sizeof(struct thread_vr_state));
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100943}
944
945void tm_reclaim_current(uint8_t cause)
946{
947 tm_enable();
Cyril Buredd00b82018-02-01 12:07:46 +1100948 tm_reclaim_thread(&current->thread, cause);
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100949}
950
Michael Neulingfb096922013-02-13 16:21:37 +0000951static inline void tm_reclaim_task(struct task_struct *tsk)
952{
953 /* We have to work out if we're switching from/to a task that's in the
954 * middle of a transaction.
955 *
956 * In switching we need to maintain a 2nd register state as
957 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
Cyril Bur000ec282016-09-23 16:18:25 +1000958 * checkpointed (tbegin) state in ckpt_regs, ckfp_state and
959 * ckvr_state
Michael Neulingfb096922013-02-13 16:21:37 +0000960 *
961 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
962 */
963 struct thread_struct *thr = &tsk->thread;
964
965 if (!thr->regs)
966 return;
967
968 if (!MSR_TM_ACTIVE(thr->regs->msr))
969 goto out_and_saveregs;
970
Michael Neuling92fb8692017-10-12 21:17:19 +1100971 WARN_ON(tm_suspend_disabled);
972
Michael Neulingfb096922013-02-13 16:21:37 +0000973 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
974 "ccr=%lx, msr=%lx, trap=%lx)\n",
975 tsk->pid, thr->regs->nip,
976 thr->regs->ccr, thr->regs->msr,
977 thr->regs->trap);
978
Cyril Buredd00b82018-02-01 12:07:46 +1100979 tm_reclaim_thread(thr, TM_CAUSE_RESCHED);
Michael Neulingfb096922013-02-13 16:21:37 +0000980
981 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
982 tsk->pid);
983
984out_and_saveregs:
985 /* Always save the regs here, even if a transaction's not active.
986 * This context-switches a thread's TM info SPRs. We do it here to
987 * be consistent with the restore path (in recheckpoint) which
988 * cannot happen later in _switch().
989 */
990 tm_save_sprs(thr);
991}
992
Cyril Bureb5c3f12017-11-02 14:09:05 +1100993extern void __tm_recheckpoint(struct thread_struct *thread);
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100994
Cyril Bureb5c3f12017-11-02 14:09:05 +1100995void tm_recheckpoint(struct thread_struct *thread)
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100996{
997 unsigned long flags;
998
Cyril Bur5d176f72016-09-14 18:02:16 +1000999 if (!(thread->regs->msr & MSR_TM))
1000 return;
1001
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001002 /* We really can't be interrupted here as the TEXASR registers can't
1003 * change and later in the trecheckpoint code, we have a userspace R1.
1004 * So let's hard disable over this region.
1005 */
1006 local_irq_save(flags);
1007 hard_irq_disable();
1008
1009 /* The TM SPRs are restored here, so that TEXASR.FS can be set
1010 * before the trecheckpoint and no explosion occurs.
1011 */
1012 tm_restore_sprs(thread);
1013
Cyril Bureb5c3f12017-11-02 14:09:05 +11001014 __tm_recheckpoint(thread);
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001015
1016 local_irq_restore(flags);
1017}
1018
Michael Neulingbc2a9402013-02-13 16:21:40 +00001019static inline void tm_recheckpoint_new_task(struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +00001020{
Michael Neulingfb096922013-02-13 16:21:37 +00001021 if (!cpu_has_feature(CPU_FTR_TM))
1022 return;
1023
1024 /* Recheckpoint the registers of the thread we're about to switch to.
1025 *
1026 * If the task was using FP, we non-lazily reload both the original and
1027 * the speculative FP register states. This is because the kernel
1028 * doesn't see if/when a TM rollback occurs, so if we take an FP
Cyril Burdc310662016-09-23 16:18:24 +10001029 * unavailable later, we are unable to determine which set of FP regs
Michael Neulingfb096922013-02-13 16:21:37 +00001030 * need to be restored.
1031 */
Cyril Bur5d176f72016-09-14 18:02:16 +10001032 if (!tm_enabled(new))
Michael Neulingfb096922013-02-13 16:21:37 +00001033 return;
1034
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001035 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
1036 tm_restore_sprs(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +00001037 return;
Michael Neulinge6b8fd02014-04-04 20:19:48 +11001038 }
Michael Neulingfb096922013-02-13 16:21:37 +00001039 /* Recheckpoint to restore original checkpointed register state. */
Cyril Bureb5c3f12017-11-02 14:09:05 +11001040 TM_DEBUG("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n",
1041 new->pid, new->thread.regs->msr);
Michael Neulingfb096922013-02-13 16:21:37 +00001042
Cyril Bureb5c3f12017-11-02 14:09:05 +11001043 tm_recheckpoint(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +00001044
Cyril Burdc310662016-09-23 16:18:24 +10001045 /*
1046 * The checkpointed state has been restored but the live state has
1047 * not, ensure all the math functionality is turned off to trigger
1048 * restore_math() to reload.
1049 */
1050 new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX);
Michael Neulingfb096922013-02-13 16:21:37 +00001051
1052 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
1053 "(kernel msr 0x%lx)\n",
1054 new->pid, mfmsr());
1055}
1056
Cyril Burdc310662016-09-23 16:18:24 +10001057static inline void __switch_to_tm(struct task_struct *prev,
1058 struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +00001059{
1060 if (cpu_has_feature(CPU_FTR_TM)) {
Cyril Bur5d176f72016-09-14 18:02:16 +10001061 if (tm_enabled(prev) || tm_enabled(new))
1062 tm_enable();
1063
1064 if (tm_enabled(prev)) {
1065 prev->thread.load_tm++;
1066 tm_reclaim_task(prev);
1067 if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0)
1068 prev->thread.regs->msr &= ~MSR_TM;
1069 }
1070
Cyril Burdc310662016-09-23 16:18:24 +10001071 tm_recheckpoint_new_task(new);
Michael Neulingfb096922013-02-13 16:21:37 +00001072 }
1073}
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001074
1075/*
1076 * This is called if we are on the way out to userspace and the
1077 * TIF_RESTORE_TM flag is set. It checks if we need to reload
1078 * FP and/or vector state and does so if necessary.
1079 * If userspace is inside a transaction (whether active or
1080 * suspended) and FP/VMX/VSX instructions have ever been enabled
1081 * inside that transaction, then we have to keep them enabled
1082 * and keep the FP/VMX/VSX state loaded while ever the transaction
1083 * continues. The reason is that if we didn't, and subsequently
1084 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
1085 * we don't know whether it's the same transaction, and thus we
1086 * don't know which of the checkpointed state and the transactional
1087 * state to use.
1088 */
1089void restore_tm_state(struct pt_regs *regs)
1090{
1091 unsigned long msr_diff;
1092
Cyril Burdc310662016-09-23 16:18:24 +10001093 /*
1094 * This is the only moment we should clear TIF_RESTORE_TM as
1095 * it is here that ckpt_regs.msr and pt_regs.msr become the same
1096 * again, anything else could lead to an incorrect ckpt_msr being
1097 * saved and therefore incorrect signal contexts.
1098 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001099 clear_thread_flag(TIF_RESTORE_TM);
1100 if (!MSR_TM_ACTIVE(regs->msr))
1101 return;
1102
Anshuman Khandual829023d2015-07-06 16:24:10 +05301103 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001104 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
Cyril Bur70fe3d92016-02-29 17:53:47 +11001105
Cyril Burdc16b552016-09-23 16:18:08 +10001106 /* Ensure that restore_math() will restore */
1107 if (msr_diff & MSR_FP)
1108 current->thread.load_fp = 1;
Valentin Rothberg39715bf2016-10-05 07:57:26 +02001109#ifdef CONFIG_ALTIVEC
Cyril Burdc16b552016-09-23 16:18:08 +10001110 if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC)
1111 current->thread.load_vec = 1;
1112#endif
Cyril Bur70fe3d92016-02-29 17:53:47 +11001113 restore_math(regs);
1114
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10001115 regs_set_return_msr(regs, regs->msr | msr_diff);
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001116}
1117
Christopher M. Riedl2d196302021-02-26 19:12:54 -06001118#else /* !CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neulingfb096922013-02-13 16:21:37 +00001119#define tm_recheckpoint_new_task(new)
Cyril Burdc310662016-09-23 16:18:24 +10001120#define __switch_to_tm(prev, new)
Christopher M. Riedl2d196302021-02-26 19:12:54 -06001121void tm_reclaim_current(uint8_t cause) {}
Michael Neulingfb096922013-02-13 16:21:37 +00001122#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neuling9422de32012-12-20 14:06:44 +00001123
Anton Blanchard152d5232015-10-29 11:43:55 +11001124static inline void save_sprs(struct thread_struct *t)
1125{
1126#ifdef CONFIG_ALTIVEC
Oliver O'Halloran01d7c2a22016-03-08 09:08:47 +11001127 if (cpu_has_feature(CPU_FTR_ALTIVEC))
Anton Blanchard152d5232015-10-29 11:43:55 +11001128 t->vrsave = mfspr(SPRN_VRSAVE);
1129#endif
Christophe Leroy359c2ca2021-05-14 13:14:53 +00001130#ifdef CONFIG_SPE
1131 if (cpu_has_feature(CPU_FTR_SPE))
1132 t->spefscr = mfspr(SPRN_SPEFSCR);
1133#endif
Anton Blanchard152d5232015-10-29 11:43:55 +11001134#ifdef CONFIG_PPC_BOOK3S_64
1135 if (cpu_has_feature(CPU_FTR_DSCR))
1136 t->dscr = mfspr(SPRN_DSCR);
1137
1138 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1139 t->bescr = mfspr(SPRN_BESCR);
1140 t->ebbhr = mfspr(SPRN_EBBHR);
1141 t->ebbrr = mfspr(SPRN_EBBRR);
1142
1143 t->fscr = mfspr(SPRN_FSCR);
1144
1145 /*
1146 * Note that the TAR is not available for use in the kernel.
1147 * (To provide this, the TAR should be backed up/restored on
1148 * exception entry/exit instead, and be in pt_regs. FIXME,
1149 * this should be in pt_regs anyway (for debug).)
1150 */
1151 t->tar = mfspr(SPRN_TAR);
1152 }
1153#endif
1154}
1155
Nicholas Piggin34e119c92021-11-23 19:51:57 +10001156#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1157void kvmppc_save_user_regs(void)
1158{
1159 unsigned long usermsr;
1160
1161 if (!current->thread.regs)
1162 return;
1163
1164 usermsr = current->thread.regs->msr;
1165
1166 if (usermsr & MSR_FP)
1167 save_fpu(current);
1168
1169 if (usermsr & MSR_VEC)
1170 save_altivec(current);
1171
1172#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1173 if (usermsr & MSR_TM) {
1174 current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
1175 current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
1176 current->thread.tm_texasr = mfspr(SPRN_TEXASR);
1177 current->thread.regs->msr &= ~MSR_TM;
1178 }
1179#endif
1180}
1181EXPORT_SYMBOL_GPL(kvmppc_save_user_regs);
Nicholas Piggin52367562021-11-23 19:52:16 +10001182
1183void kvmppc_save_current_sprs(void)
1184{
1185 save_sprs(&current->thread);
1186}
1187EXPORT_SYMBOL_GPL(kvmppc_save_current_sprs);
Nicholas Piggin34e119c92021-11-23 19:51:57 +10001188#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1189
Anton Blanchard152d5232015-10-29 11:43:55 +11001190static inline void restore_sprs(struct thread_struct *old_thread,
1191 struct thread_struct *new_thread)
1192{
1193#ifdef CONFIG_ALTIVEC
1194 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
1195 old_thread->vrsave != new_thread->vrsave)
1196 mtspr(SPRN_VRSAVE, new_thread->vrsave);
1197#endif
Christophe Leroy359c2ca2021-05-14 13:14:53 +00001198#ifdef CONFIG_SPE
1199 if (cpu_has_feature(CPU_FTR_SPE) &&
1200 old_thread->spefscr != new_thread->spefscr)
1201 mtspr(SPRN_SPEFSCR, new_thread->spefscr);
1202#endif
Anton Blanchard152d5232015-10-29 11:43:55 +11001203#ifdef CONFIG_PPC_BOOK3S_64
1204 if (cpu_has_feature(CPU_FTR_DSCR)) {
1205 u64 dscr = get_paca()->dscr_default;
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001206 if (new_thread->dscr_inherit)
Anton Blanchard152d5232015-10-29 11:43:55 +11001207 dscr = new_thread->dscr;
Anton Blanchard152d5232015-10-29 11:43:55 +11001208
1209 if (old_thread->dscr != dscr)
1210 mtspr(SPRN_DSCR, dscr);
Anton Blanchard152d5232015-10-29 11:43:55 +11001211 }
1212
1213 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1214 if (old_thread->bescr != new_thread->bescr)
1215 mtspr(SPRN_BESCR, new_thread->bescr);
1216 if (old_thread->ebbhr != new_thread->ebbhr)
1217 mtspr(SPRN_EBBHR, new_thread->ebbhr);
1218 if (old_thread->ebbrr != new_thread->ebbrr)
1219 mtspr(SPRN_EBBRR, new_thread->ebbrr);
1220
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001221 if (old_thread->fscr != new_thread->fscr)
1222 mtspr(SPRN_FSCR, new_thread->fscr);
1223
Anton Blanchard152d5232015-10-29 11:43:55 +11001224 if (old_thread->tar != new_thread->tar)
1225 mtspr(SPRN_TAR, new_thread->tar);
1226 }
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001227
Alastair D'Silva3449f192018-05-11 16:12:58 +10001228 if (cpu_has_feature(CPU_FTR_P9_TIDR) &&
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001229 old_thread->tidr != new_thread->tidr)
1230 mtspr(SPRN_TIDR, new_thread->tidr);
Anton Blanchard152d5232015-10-29 11:43:55 +11001231#endif
Ram Pai06bb53b2018-01-18 17:50:31 -08001232
Anton Blanchard152d5232015-10-29 11:43:55 +11001233}
1234
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001235struct task_struct *__switch_to(struct task_struct *prev,
1236 struct task_struct *new)
1237{
1238 struct thread_struct *new_thread, *old_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001239 struct task_struct *last;
Nicholas Piggin387e2202021-12-02 00:41:52 +10001240#ifdef CONFIG_PPC_64S_HASH_MMU
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001241 struct ppc64_tlb_batch *batch;
1242#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001243
Anton Blanchard152d5232015-10-29 11:43:55 +11001244 new_thread = &new->thread;
1245 old_thread = &current->thread;
1246
Michael Neuling7ba5fef2013-10-02 17:15:14 +10001247 WARN_ON(!irqs_disabled());
1248
Nicholas Piggin387e2202021-12-02 00:41:52 +10001249#ifdef CONFIG_PPC_64S_HASH_MMU
Christoph Lameter69111ba2014-10-21 15:23:25 -05001250 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001251 if (batch->active) {
1252 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1253 if (batch->index)
1254 __flush_tlb_pending(batch);
1255 batch->active = 0;
1256 }
Nicholas Pigginf35d2f22021-06-22 15:30:36 +10001257
1258 /*
1259 * On POWER9 the copy-paste buffer can only paste into
1260 * foreign real addresses, so unprivileged processes can not
1261 * see the data or use it in any way unless they have
1262 * foreign real mappings. If the new process has the foreign
1263 * real address mappings, we must issue a cp_abort to clear
1264 * any state and prevent snooping, corruption or a covert
1265 * channel. ISA v3.1 supports paste into local memory.
1266 */
1267 if (new->mm && (cpu_has_feature(CPU_FTR_ARCH_31) ||
1268 atomic_read(&new->mm->context.vas_windows)))
1269 asm volatile(PPC_CP_ABORT);
Michael Ellerman4e003742017-10-19 15:08:43 +11001270#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001271
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001272#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1273 switch_booke_debug_regs(&new->thread.debug);
1274#else
1275/*
1276 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1277 * schedule DABR
1278 */
1279#ifndef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria303e6a92020-05-14 16:47:34 +05301280 switch_hw_breakpoint(new);
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001281#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1282#endif
1283
1284 /*
1285 * We need to save SPRs before treclaim/trecheckpoint as these will
1286 * change a number of them.
1287 */
1288 save_sprs(&prev->thread);
1289
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001290 /* Save FPU, Altivec, VSX and SPE state */
1291 giveup_all(prev);
1292
Cyril Burdc310662016-09-23 16:18:24 +10001293 __switch_to_tm(prev, new);
1294
Nicholas Piggine4c0fc52017-06-09 01:36:06 +10001295 if (!radix_enabled()) {
1296 /*
1297 * We can't take a PMU exception inside _switch() since there
1298 * is a window where the kernel stack SLB and the kernel stack
1299 * are out of sync. Hard disable here.
1300 */
1301 hard_irq_disable();
1302 }
Michael Neulingbc2a9402013-02-13 16:21:40 +00001303
Anton Blanchard20dbe672015-12-10 20:44:39 +11001304 /*
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10001305 * Call restore_sprs() and set_return_regs_changed() before calling
1306 * _switch(). If we move it after _switch() then we miss out on calling
1307 * it for new tasks. The reason for this is we manually create a stack
1308 * frame for new tasks that directly returns through ret_from_fork() or
Anton Blanchard20dbe672015-12-10 20:44:39 +11001309 * ret_from_kernel_thread(). See copy_thread() for details.
1310 */
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001311 restore_sprs(old_thread, new_thread);
1312
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10001313 set_return_regs_changed(); /* _switch changes stack (and regs) */
1314
Christophe Leroy42e03bc2021-10-19 09:29:26 +02001315 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
1316 kuap_assert_locked();
1317
Anton Blanchard20dbe672015-12-10 20:44:39 +11001318 last = _switch(old_thread, new_thread);
1319
Nicholas Pigginf35d2f22021-06-22 15:30:36 +10001320 /*
1321 * Nothing after _switch will be run for newly created tasks,
1322 * because they switch directly to ret_from_fork/ret_from_kernel_thread
1323 * etc. Code added here should have a comment explaining why that is
1324 * okay.
1325 */
1326
Michael Ellerman4e003742017-10-19 15:08:43 +11001327#ifdef CONFIG_PPC_BOOK3S_64
Nicholas Piggin387e2202021-12-02 00:41:52 +10001328#ifdef CONFIG_PPC_64S_HASH_MMU
Nicholas Pigginf35d2f22021-06-22 15:30:36 +10001329 /*
1330 * This applies to a process that was context switched while inside
1331 * arch_enter_lazy_mmu_mode(), to re-activate the batch that was
1332 * deactivated above, before _switch(). This will never be the case
1333 * for new tasks.
1334 */
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001335 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1336 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001337 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001338 batch->active = 1;
1339 }
Nicholas Piggin387e2202021-12-02 00:41:52 +10001340#endif
Cyril Bur70fe3d92016-02-29 17:53:47 +11001341
Nicholas Pigginf35d2f22021-06-22 15:30:36 +10001342 /*
1343 * Math facilities are masked out of the child MSR in copy_thread.
1344 * A new task does not need to restore_math because it will
1345 * demand fault them.
1346 */
1347 if (current->thread.regs)
Christophe Leroy05b98792019-01-17 23:25:12 +11001348 restore_math(current->thread.regs);
Michael Ellerman4e003742017-10-19 15:08:43 +11001349#endif /* CONFIG_PPC_BOOK3S_64 */
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001350
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001351 return last;
1352}
1353
Christophe Leroydf131022018-10-06 16:51:16 +00001354#define NR_INSN_TO_PRINT 16
Paul Mackerras06d67d52005-10-10 22:29:05 +10001355
Paul Mackerras06d67d52005-10-10 22:29:05 +10001356static void show_instructions(struct pt_regs *regs)
1357{
1358 int i;
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301359 unsigned long nip = regs->nip;
Christophe Leroydf131022018-10-06 16:51:16 +00001360 unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
Paul Mackerras06d67d52005-10-10 22:29:05 +10001361
1362 printk("Instruction dump:");
1363
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301364 /*
1365 * If we were executing with the MMU off for instructions, adjust pc
1366 * rather than printing XXXXXXXX.
1367 */
1368 if (!IS_ENABLED(CONFIG_BOOKE) && !(regs->msr & MSR_IR)) {
1369 pc = (unsigned long)phys_to_virt(pc);
1370 nip = (unsigned long)phys_to_virt(regs->nip);
1371 }
1372
Christophe Leroydf131022018-10-06 16:51:16 +00001373 for (i = 0; i < NR_INSN_TO_PRINT; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001374 int instr;
1375
1376 if (!(i % 8))
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001377 pr_cont("\n");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001378
Anton Blanchard00ae36d2006-10-13 12:17:16 +10001379 if (!__kernel_text_address(pc) ||
Christoph Hellwig25f12ae2020-06-17 09:37:55 +02001380 get_kernel_nofault(instr, (const void *)pc)) {
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001381 pr_cont("XXXXXXXX ");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001382 } else {
Aneesh Kumar K.Va6e2c222020-05-24 15:08:19 +05301383 if (nip == pc)
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001384 pr_cont("<%08x> ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001385 else
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001386 pr_cont("%08x ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001387 }
1388
1389 pc += sizeof(int);
1390 }
1391
Andrew Donnellan2ffd04d2016-11-04 17:20:40 +11001392 pr_cont("\n");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001393}
1394
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001395void show_user_instructions(struct pt_regs *regs)
1396{
1397 unsigned long pc;
Christophe Leroydf131022018-10-06 16:51:16 +00001398 int n = NR_INSN_TO_PRINT;
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001399 struct seq_buf s;
1400 char buf[96]; /* enough for 8 times 9 + 2 chars */
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001401
Christophe Leroydf131022018-10-06 16:51:16 +00001402 pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001403
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001404 seq_buf_init(&s, buf, sizeof(buf));
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001405
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001406 while (n) {
1407 int i;
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001408
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001409 seq_buf_clear(&s);
1410
1411 for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) {
1412 int instr;
1413
Christoph Hellwigc0ee37e2020-06-17 09:37:54 +02001414 if (copy_from_user_nofault(&instr, (void __user *)pc,
1415 sizeof(instr))) {
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001416 seq_buf_printf(&s, "XXXXXXXX ");
1417 continue;
1418 }
1419 seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr);
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001420 }
1421
Christophe Leroyfb2d9502018-10-06 16:51:14 +00001422 if (!seq_buf_has_overflowed(&s))
1423 pr_info("%s[%d]: code: %s\n", current->comm,
1424 current->pid, s.buffer);
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001425 }
Murilo Opsfelder Araujo88b0fe12018-08-01 18:33:19 -03001426}
1427
Michael Neuling801c0b22015-11-20 15:15:32 +11001428struct regbit {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001429 unsigned long bit;
1430 const char *name;
Michael Neuling801c0b22015-11-20 15:15:32 +11001431};
1432
1433static struct regbit msr_bits[] = {
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001434#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
1435 {MSR_SF, "SF"},
1436 {MSR_HV, "HV"},
1437#endif
1438 {MSR_VEC, "VEC"},
1439 {MSR_VSX, "VSX"},
1440#ifdef CONFIG_BOOKE
1441 {MSR_CE, "CE"},
1442#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001443 {MSR_EE, "EE"},
1444 {MSR_PR, "PR"},
1445 {MSR_FP, "FP"},
1446 {MSR_ME, "ME"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001447#ifdef CONFIG_BOOKE
Kumar Gala1b983262008-11-19 04:39:53 +00001448 {MSR_DE, "DE"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001449#else
1450 {MSR_SE, "SE"},
1451 {MSR_BE, "BE"},
1452#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001453 {MSR_IR, "IR"},
1454 {MSR_DR, "DR"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001455 {MSR_PMM, "PMM"},
1456#ifndef CONFIG_BOOKE
1457 {MSR_RI, "RI"},
1458 {MSR_LE, "LE"},
1459#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001460 {0, NULL}
1461};
1462
Michael Neuling801c0b22015-11-20 15:15:32 +11001463static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
Paul Mackerras06d67d52005-10-10 22:29:05 +10001464{
Michael Neuling801c0b22015-11-20 15:15:32 +11001465 const char *s = "";
Paul Mackerras06d67d52005-10-10 22:29:05 +10001466
Paul Mackerras06d67d52005-10-10 22:29:05 +10001467 for (; bits->bit; ++bits)
1468 if (val & bits->bit) {
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001469 pr_cont("%s%s", s, bits->name);
Michael Neuling801c0b22015-11-20 15:15:32 +11001470 s = sep;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001471 }
Michael Neuling801c0b22015-11-20 15:15:32 +11001472}
1473
1474#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1475static struct regbit msr_tm_bits[] = {
1476 {MSR_TS_T, "T"},
1477 {MSR_TS_S, "S"},
1478 {MSR_TM, "E"},
1479 {0, NULL}
1480};
1481
1482static void print_tm_bits(unsigned long val)
1483{
1484/*
1485 * This only prints something if at least one of the TM bit is set.
1486 * Inside the TM[], the output means:
1487 * E: Enabled (bit 32)
1488 * S: Suspended (bit 33)
1489 * T: Transactional (bit 34)
1490 */
1491 if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001492 pr_cont(",TM[");
Michael Neuling801c0b22015-11-20 15:15:32 +11001493 print_bits(val, msr_tm_bits, "");
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001494 pr_cont("]");
Michael Neuling801c0b22015-11-20 15:15:32 +11001495 }
1496}
1497#else
1498static void print_tm_bits(unsigned long val) {}
1499#endif
1500
1501static void print_msr_bits(unsigned long val)
1502{
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001503 pr_cont("<");
Michael Neuling801c0b22015-11-20 15:15:32 +11001504 print_bits(val, msr_bits, ",");
1505 print_tm_bits(val);
Michael Ellermandb5ba5a2016-11-02 22:20:47 +11001506 pr_cont(">");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001507}
1508
1509#ifdef CONFIG_PPC64
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001510#define REG "%016lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001511#define REGS_PER_LINE 4
Paul Mackerras06d67d52005-10-10 22:29:05 +10001512#else
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001513#define REG "%08lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001514#define REGS_PER_LINE 8
Paul Mackerras06d67d52005-10-10 22:29:05 +10001515#endif
1516
Nicholas Pigginbf13718b2020-11-07 12:33:05 +10001517static void __show_regs(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001518{
1519 int i, trap;
1520
Michael Ellermana6036102017-08-23 23:56:24 +10001521 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
Paul Mackerras06d67d52005-10-10 22:29:05 +10001522 regs->nip, regs->link, regs->ctr);
Michael Ellerman182dc9c2017-12-18 16:33:36 +11001523 printk("REGS: %px TRAP: %04lx %s (%s)\n",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001524 regs, regs->trap, print_tainted(), init_utsname()->release);
Michael Ellermana6036102017-08-23 23:56:24 +10001525 printk("MSR: "REG" ", regs->msr);
Michael Neuling801c0b22015-11-20 15:15:32 +11001526 print_msr_bits(regs->msr);
Michael Ellermanf6fc73f2017-08-23 23:56:23 +10001527 pr_cont(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001528 trap = TRAP(regs);
Nicholas Piggin912237ea2020-05-07 22:13:31 +10001529 if (!trap_is_syscall(regs) && cpu_has_feature(CPU_FTR_CFAR))
Michael Ellerman7dae8652016-11-03 20:45:26 +11001530 pr_cont("CFAR: "REG" ", regs->orig_gpr3);
Xiongwei Song7153d4b2021-04-14 19:00:33 +08001531 if (trap == INTERRUPT_MACHINE_CHECK ||
1532 trap == INTERRUPT_DATA_STORAGE ||
1533 trap == INTERRUPT_ALIGNMENT) {
Christophe Leroy2ec42992020-08-17 05:46:43 +00001534 if (IS_ENABLED(CONFIG_4xx) || IS_ENABLED(CONFIG_BOOKE))
Xiongwei Song4872cbd2021-08-07 09:02:38 +08001535 pr_cont("DEAR: "REG" ESR: "REG" ", regs->dear, regs->esr);
Christophe Leroy2ec42992020-08-17 05:46:43 +00001536 else
1537 pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1538 }
1539
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001540#ifdef CONFIG_PPC64
Nicholas Piggin3130a7b2018-05-10 11:04:24 +10001541 pr_cont("IRQMASK: %lx ", regs->softe);
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001542#endif
1543#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchard6d888d12013-11-18 13:19:17 +11001544 if (MSR_TM_ACTIVE(regs->msr))
Michael Ellerman7dae8652016-11-03 20:45:26 +11001545 pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
Kumar Gala14170782007-07-26 00:46:15 -05001546#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001547
1548 for (i = 0; i < 32; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001549 if ((i % REGS_PER_LINE) == 0)
Michael Ellerman7dae8652016-11-03 20:45:26 +11001550 pr_cont("\nGPR%02d: ", i);
1551 pr_cont(REG " ", regs->gpr[i]);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001552 }
Michael Ellerman7dae8652016-11-03 20:45:26 +11001553 pr_cont("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001554 /*
1555 * Lookup NIP late so we have the best change of getting the
1556 * above info out without failing
1557 */
Christophe Leroy8f020c72020-08-17 05:46:44 +00001558 if (IS_ENABLED(CONFIG_KALLSYMS)) {
1559 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1560 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
1561 }
Nicholas Pigginbf13718b2020-11-07 12:33:05 +10001562}
1563
1564void show_regs(struct pt_regs *regs)
1565{
1566 show_regs_print_info(KERN_DEFAULT);
1567 __show_regs(regs);
Dmitry Safonov9cb8f062020-06-08 21:32:29 -07001568 show_stack(current, (unsigned long *) regs->gpr[1], KERN_DEFAULT);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001569 if (!user_mode(regs))
1570 show_instructions(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001571}
1572
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001573void flush_thread(void)
1574{
K.Prasade0780b72011-02-10 04:44:35 +00001575#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad5aae8a52010-06-15 11:35:19 +05301576 flush_ptrace_hw_breakpoint(current);
K.Prasade0780b72011-02-10 04:44:35 +00001577#else /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001578 set_debug_reg_defaults(&current->thread);
K.Prasade0780b72011-02-10 04:44:35 +00001579#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001580}
1581
Nicholas Piggin425d3312018-09-15 01:30:55 +10001582void arch_setup_new_exec(void)
1583{
Aneesh Kumar K.Vd7df77e2020-11-27 10:14:11 +05301584
1585#ifdef CONFIG_PPC_BOOK3S_64
1586 if (!radix_enabled())
1587 hash__setup_new_exec();
Nicholas Piggin425d3312018-09-15 01:30:55 +10001588#endif
Aneesh Kumar K.Vd7df77e2020-11-27 10:14:11 +05301589 /*
1590 * If we exec out of a kernel thread then thread.regs will not be
1591 * set. Do it now.
1592 */
1593 if (!current->thread.regs) {
1594 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1595 current->thread.regs = regs - 1;
1596 }
Aneesh Kumar K.Vd5fa30e2020-11-27 10:14:14 +05301597
1598#ifdef CONFIG_PPC_MEM_KEYS
1599 current->thread.regs->amr = default_amr;
1600 current->thread.regs->iamr = default_iamr;
1601#endif
Aneesh Kumar K.Vd7df77e2020-11-27 10:14:11 +05301602}
Nicholas Piggin425d3312018-09-15 01:30:55 +10001603
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001604#ifdef CONFIG_PPC64
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001605/**
1606 * Assign a TIDR (thread ID) for task @t and set it in the thread
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001607 * structure. For now, we only support setting TIDR for 'current' task.
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001608 *
1609 * Since the TID value is a truncated form of it PID, it is possible
1610 * (but unlikely) for 2 threads to have the same TID. In the unlikely event
1611 * that 2 threads share the same TID and are waiting, one of the following
1612 * cases will happen:
1613 *
1614 * 1. The correct thread is running, the wrong thread is not
1615 * In this situation, the correct thread is woken and proceeds to pass it's
1616 * condition check.
1617 *
1618 * 2. Neither threads are running
1619 * In this situation, neither thread will be woken. When scheduled, the waiting
1620 * threads will execute either a wait, which will return immediately, followed
1621 * by a condition check, which will pass for the correct thread and fail
1622 * for the wrong thread, or they will execute the condition check immediately.
1623 *
1624 * 3. The wrong thread is running, the correct thread is not
1625 * The wrong thread will be woken, but will fail it's condition check and
1626 * re-execute wait. The correct thread, when scheduled, will execute either
1627 * it's condition check (which will pass), or wait, which returns immediately
1628 * when called the first time after the thread is scheduled, followed by it's
1629 * condition check (which will pass).
1630 *
1631 * 4. Both threads are running
1632 * Both threads will be woken. The wrong thread will fail it's condition check
1633 * and execute another wait, while the correct thread will pass it's condition
1634 * check.
1635 *
1636 * @t: the task to set the thread ID for
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001637 */
1638int set_thread_tidr(struct task_struct *t)
1639{
Alastair D'Silva3449f192018-05-11 16:12:58 +10001640 if (!cpu_has_feature(CPU_FTR_P9_TIDR))
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001641 return -EINVAL;
1642
1643 if (t != current)
1644 return -EINVAL;
1645
Vaibhav Jain7e4d4232017-11-24 14:03:38 +05301646 if (t->thread.tidr)
1647 return 0;
1648
Alastair D'Silva71cc64a2018-05-11 16:12:59 +10001649 t->thread.tidr = (u16)task_pid_nr(t);
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001650 mtspr(SPRN_TIDR, t->thread.tidr);
1651
1652 return 0;
1653}
Christophe Lombardb1db5512018-01-11 09:55:25 +01001654EXPORT_SYMBOL_GPL(set_thread_tidr);
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001655
1656#endif /* CONFIG_PPC64 */
1657
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001658void
1659release_thread(struct task_struct *t)
1660{
1661}
1662
1663/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001664 * this gets called so that we can store coprocessor state into memory and
1665 * copy the current task into the new thread.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001666 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001667int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001668{
Anton Blanchard579e6332015-10-29 11:44:09 +11001669 flush_all_to_thread(src);
Michael Neuling621b5062014-03-03 14:21:40 +11001670 /*
1671 * Flush TM state out so we can copy it. __switch_to_tm() does this
1672 * flush but it removes the checkpointed state from the current CPU and
1673 * transitions the CPU out of TM mode. Hence we need to call
1674 * tm_recheckpoint_new_task() (on the same task) to restore the
1675 * checkpointed state back and the TM mode.
Cyril Bur5d176f72016-09-14 18:02:16 +10001676 *
1677 * Can't pass dst because it isn't ready. Doesn't matter, passing
1678 * dst is only important for __switch_to()
Michael Neuling621b5062014-03-03 14:21:40 +11001679 */
Cyril Burdc310662016-09-23 16:18:24 +10001680 __switch_to_tm(src, src);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001681
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001682 *dst = *src;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001683
1684 clear_task_ebb(dst);
1685
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001686 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001687}
1688
Michael Ellermancec15482014-07-10 12:29:21 +10001689static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1690{
Nicholas Piggin387e2202021-12-02 00:41:52 +10001691#ifdef CONFIG_PPC_64S_HASH_MMU
Michael Ellermancec15482014-07-10 12:29:21 +10001692 unsigned long sp_vsid;
1693 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1694
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001695 if (radix_enabled())
1696 return;
1697
Michael Ellermancec15482014-07-10 12:29:21 +10001698 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1699 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1700 << SLB_VSID_SHIFT_1T;
1701 else
1702 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1703 << SLB_VSID_SHIFT;
1704 sp_vsid |= SLB_VSID_KERNEL | llp;
1705 p->thread.ksp_vsid = sp_vsid;
1706#endif
1707}
1708
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001709/*
1710 * Copy a thread..
1711 */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001712
Alex Dowad6eca8932015-03-13 20:14:46 +02001713/*
1714 * Copy architecture-specific thread state
1715 */
Eric W. Biedermanc5febea2022-04-08 18:07:50 -05001716int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001717{
Eric W. Biedermanc5febea2022-04-08 18:07:50 -05001718 unsigned long clone_flags = args->flags;
1719 unsigned long usp = args->stack;
Eric W. Biedermanc5febea2022-04-08 18:07:50 -05001720 unsigned long tls = args->tls;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001721 struct pt_regs *childregs, *kregs;
1722 extern void ret_from_fork(void);
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001723 extern void ret_from_fork_scv(void);
Al Viro58254e12012-09-12 18:32:42 -04001724 extern void ret_from_kernel_thread(void);
1725 void (*f)(void);
Al Viro0cec6fd2006-01-12 01:06:02 -08001726 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Michael Ellerman5d31a962016-03-24 22:04:04 +11001727 struct thread_info *ti = task_thread_info(p);
Ravi Bangoria6b424ef2020-05-14 16:47:35 +05301728#ifdef CONFIG_HAVE_HW_BREAKPOINT
1729 int i;
1730#endif
Michael Ellerman5d31a962016-03-24 22:04:04 +11001731
Christophe Leroyed1cd6d2019-01-31 10:08:58 +00001732 klp_init_thread_info(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001733
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001734 /* Copy registers */
1735 sp -= sizeof(struct pt_regs);
1736 childregs = (struct pt_regs *) sp;
Eric W. Biederman5bd2e972022-04-12 10:18:48 -05001737 if (unlikely(args->fn)) {
Alex Dowad6eca8932015-03-13 20:14:46 +02001738 /* kernel thread */
Al Viro58254e12012-09-12 18:32:42 -04001739 memset(childregs, 0, sizeof(struct pt_regs));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001740 childregs->gpr[1] = sp + sizeof(struct pt_regs);
Anton Blanchard7cedd602014-02-04 16:08:51 +11001741 /* function */
Eric W. Biederman5bd2e972022-04-12 10:18:48 -05001742 if (args->fn)
1743 childregs->gpr[14] = ppc_function_entry((void *)args->fn);
Al Viro58254e12012-09-12 18:32:42 -04001744#ifdef CONFIG_PPC64
Al Virob5e2fc12006-01-12 01:06:01 -08001745 clear_tsk_thread_flag(p, TIF_32BIT);
Madhavan Srinivasanc2e480b2017-12-20 09:25:42 +05301746 childregs->softe = IRQS_ENABLED;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001747#endif
Eric W. Biederman5bd2e972022-04-12 10:18:48 -05001748 childregs->gpr[15] = (unsigned long)args->fn_arg;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001749 p->thread.regs = NULL; /* no user register state */
Al Viro138d1ce2012-10-11 08:41:43 -04001750 ti->flags |= _TIF_RESTOREALL;
Al Viro58254e12012-09-12 18:32:42 -04001751 f = ret_from_kernel_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001752 } else {
Alex Dowad6eca8932015-03-13 20:14:46 +02001753 /* user thread */
Al Viroafa86fc2012-10-22 22:51:14 -04001754 struct pt_regs *regs = current_pt_regs();
Al Viro58254e12012-09-12 18:32:42 -04001755 *childregs = *regs;
Al Viroea516b12012-10-21 22:28:43 -04001756 if (usp)
1757 childregs->gpr[1] = usp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001758 p->thread.regs = childregs;
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001759 /* 64s sets this in ret_from_fork */
1760 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
1761 childregs->gpr[3] = 0; /* Result from fork() */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001762 if (clone_flags & CLONE_SETTLS) {
Denis Kirjanov9904b002010-07-29 22:04:39 +00001763 if (!is_32bit_task())
Nicholas Pigginfacd04a2019-08-27 13:30:06 +10001764 childregs->gpr[13] = tls;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001765 else
Nicholas Pigginfacd04a2019-08-27 13:30:06 +10001766 childregs->gpr[2] = tls;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001767 }
Al Viro58254e12012-09-12 18:32:42 -04001768
Nicholas Piggin7fa95f92020-06-11 18:12:03 +10001769 if (trap_is_scv(regs))
1770 f = ret_from_fork_scv;
1771 else
1772 f = ret_from_fork;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001773 }
Cyril Burd272f662016-02-29 17:53:46 +11001774 childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001775 sp -= STACK_FRAME_OVERHEAD;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001776
1777 /*
1778 * The way this works is that at some point in the future
1779 * some task will call _switch to switch to the new task.
1780 * That will pop off the stack frame created below and start
1781 * the new task running at ret_from_fork. The new task will
1782 * do some house keeping and then return from the fork or clone
1783 * system call, using the stack frame created above.
1784 */
Li Zhongaf945cf2013-05-06 22:44:41 +00001785 ((unsigned long *)sp)[0] = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001786 sp -= sizeof(struct pt_regs);
1787 kregs = (struct pt_regs *) sp;
1788 sp -= STACK_FRAME_OVERHEAD;
1789 p->thread.ksp = sp;
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001790#ifdef CONFIG_HAVE_HW_BREAKPOINT
Ravi Bangoria6b424ef2020-05-14 16:47:35 +05301791 for (i = 0; i < nr_wp_slots(); i++)
1792 p->thread.ptrace_bps[i] = NULL;
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001793#endif
1794
Christophe Leroyb6254ce2020-08-18 17:19:17 +00001795#ifdef CONFIG_PPC_FPU_REGS
Paul Mackerras18461962013-09-10 20:21:10 +10001796 p->thread.fp_save_area = NULL;
Christophe Leroyb6254ce2020-08-18 17:19:17 +00001797#endif
Paul Mackerras18461962013-09-10 20:21:10 +10001798#ifdef CONFIG_ALTIVEC
1799 p->thread.vr_save_area = NULL;
1800#endif
Christophe Leroy16132522021-06-03 08:41:44 +00001801#if defined(CONFIG_PPC_BOOK3S_32) && defined(CONFIG_PPC_KUAP)
1802 p->thread.kuap = KUAP_NONE;
1803#endif
Christophe Leroy43afcf82021-10-19 09:29:28 +02001804#if defined(CONFIG_BOOKE_OR_40x) && defined(CONFIG_PPC_KUAP)
1805 p->thread.pid = MMU_NO_CONTEXT;
1806#endif
Paul Mackerras18461962013-09-10 20:21:10 +10001807
Michael Ellermancec15482014-07-10 12:29:21 +10001808 setup_ksp_vsid(p, sp);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001809
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001810#ifdef CONFIG_PPC64
1811 if (cpu_has_feature(CPU_FTR_DSCR)) {
Anton Blanchard1021cb22012-09-03 16:49:47 +00001812 p->thread.dscr_inherit = current->thread.dscr_inherit;
Anton Blancharddb1231dc2015-12-09 20:11:47 +11001813 p->thread.dscr = mfspr(SPRN_DSCR);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001814 }
Haren Myneni92779242012-12-06 21:49:56 +00001815 if (cpu_has_feature(CPU_FTR_HAS_PPR))
Nicholas Piggin4c2de742018-10-13 00:15:16 +11001816 childregs->ppr = DEFAULT_PPR;
Sukadev Bhattiproluec233ed2017-11-07 18:23:53 -08001817
1818 p->thread.tidr = 0;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001819#endif
Aneesh Kumar K.Vf643fca2020-11-27 10:14:13 +05301820 /*
1821 * Run with the current AMR value of the kernel
1822 */
1823#ifdef CONFIG_PPC_PKEY
1824 if (mmu_has_feature(MMU_FTR_BOOK3S_KUAP))
1825 kregs->amr = AMR_KUAP_BLOCKED;
1826
1827 if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP))
1828 kregs->iamr = AMR_KUEP_BLOCKED;
1829#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +11001830 kregs->nip = ppc_function_entry(f);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001831 return 0;
1832}
1833
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001834void preload_new_slb_context(unsigned long start, unsigned long sp);
1835
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001836/*
1837 * Set up a thread for executing a new program
1838 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001839void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001840{
Michael Ellerman90eac722005-10-21 16:01:33 +10001841#ifdef CONFIG_PPC64
1842 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001843
Christophe Leroybfac2792020-08-17 05:46:42 +00001844 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled())
Aneesh Kumar K.Vf89bd8b2019-04-09 09:33:28 +05301845 preload_new_slb_context(start, sp);
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001846#endif
Michael Ellerman90eac722005-10-21 16:01:33 +10001847
Cyril Bur8e96a872016-06-17 14:58:34 +10001848#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1849 /*
1850 * Clear any transactional state, we're exec()ing. The cause is
1851 * not important as there will never be a recheckpoint so it's not
1852 * user visible.
1853 */
1854 if (MSR_TM_SUSPENDED(mfmsr()))
1855 tm_reclaim_current(0);
1856#endif
1857
Naveen N. Raoec6d0dd2022-06-09 16:03:28 +05301858 memset(&regs->gpr[1], 0, sizeof(regs->gpr) - sizeof(regs->gpr[0]));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001859 regs->ctr = 0;
1860 regs->link = 0;
1861 regs->xer = 0;
1862 regs->ccr = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001863 regs->gpr[1] = sp;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001864
1865#ifdef CONFIG_PPC32
1866 regs->mq = 0;
1867 regs->nip = start;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001868 regs->msr = MSR_USER;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001869#else
Denis Kirjanov9904b002010-07-29 22:04:39 +00001870 if (!is_32bit_task()) {
Rusty Russell94af3ab2013-11-20 22:15:02 +11001871 unsigned long entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001872
Rusty Russell94af3ab2013-11-20 22:15:02 +11001873 if (is_elf2_task()) {
1874 /* Look ma, no function descriptors! */
1875 entry = start;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001876
Rusty Russell94af3ab2013-11-20 22:15:02 +11001877 /*
1878 * Ulrich says:
1879 * The latest iteration of the ABI requires that when
1880 * calling a function (at its global entry point),
1881 * the caller must ensure r12 holds the entry point
1882 * address (so that the function can quickly
1883 * establish addressability).
1884 */
1885 regs->gpr[12] = start;
1886 /* Make sure that's restored on entry to userspace. */
1887 set_thread_flag(TIF_RESTOREALL);
1888 } else {
1889 unsigned long toc;
1890
1891 /* start is a relocated pointer to the function
1892 * descriptor for the elf _start routine. The first
1893 * entry in the function descriptor is the entry
1894 * address of _start and the second entry is the TOC
1895 * value we need to use.
1896 */
1897 __get_user(entry, (unsigned long __user *)start);
1898 __get_user(toc, (unsigned long __user *)start+1);
1899
1900 /* Check whether the e_entry function descriptor entries
1901 * need to be relocated before we can use them.
1902 */
1903 if (load_addr != 0) {
1904 entry += load_addr;
1905 toc += load_addr;
1906 }
1907 regs->gpr[2] = toc;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001908 }
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10001909 regs_set_return_ip(regs, entry);
1910 regs_set_return_msr(regs, MSR_USER64);
Stephen Rothwelld4bf9a72005-10-13 13:40:54 +10001911 } else {
Stephen Rothwelld4bf9a72005-10-13 13:40:54 +10001912 regs->gpr[2] = 0;
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10001913 regs_set_return_ip(regs, start);
1914 regs_set_return_msr(regs, MSR_USER32);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001915 }
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10001916
Paul Mackerras06d67d52005-10-10 22:29:05 +10001917#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001918#ifdef CONFIG_VSX
1919 current->thread.used_vsr = 0;
1920#endif
Nicholas Piggin5434ae72018-09-15 01:30:56 +10001921 current->thread.load_slb = 0;
Breno Leitao11958922017-06-02 18:43:30 -03001922 current->thread.load_fp = 0;
Christophe Leroyb6254ce2020-08-18 17:19:17 +00001923#ifdef CONFIG_PPC_FPU_REGS
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001924 memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
Paul Mackerras18461962013-09-10 20:21:10 +10001925 current->thread.fp_save_area = NULL;
Christophe Leroyb6254ce2020-08-18 17:19:17 +00001926#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001927#ifdef CONFIG_ALTIVEC
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001928 memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1929 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
Paul Mackerras18461962013-09-10 20:21:10 +10001930 current->thread.vr_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001931 current->thread.vrsave = 0;
1932 current->thread.used_vr = 0;
Breno Leitao11958922017-06-02 18:43:30 -03001933 current->thread.load_vec = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001934#endif /* CONFIG_ALTIVEC */
1935#ifdef CONFIG_SPE
1936 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1937 current->thread.acc = 0;
1938 current->thread.spefscr = 0;
1939 current->thread.used_spe = 0;
1940#endif /* CONFIG_SPE */
Michael Neulingbc2a9402013-02-13 16:21:40 +00001941#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Michael Neulingbc2a9402013-02-13 16:21:40 +00001942 current->thread.tm_tfhar = 0;
1943 current->thread.tm_texasr = 0;
1944 current->thread.tm_tfiar = 0;
Breno Leitao7f22ced2017-06-05 11:40:59 -03001945 current->thread.load_tm = 0;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001946#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001947}
Anton Blancharde1802b02014-08-20 08:00:02 +10001948EXPORT_SYMBOL(start_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001949
1950#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1951 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1952
1953int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1954{
1955 struct pt_regs *regs = tsk->thread.regs;
1956
1957 /* This is a bit hairy. If we are an SPE enabled processor
1958 * (have embedded fp) we store the IEEE exception enable flags in
1959 * fpexc_mode. fpexc_mode is also used for setting FP exception
1960 * mode (asyn, precise, disabled) for 'Classic' FP. */
1961 if (val & PR_FP_EXC_SW_ENABLE) {
Kumar Gala5e14d212007-09-13 01:44:20 -05001962 if (cpu_has_feature(CPU_FTR_SPE)) {
Joseph Myers640e9222013-12-10 23:07:45 +00001963 /*
1964 * When the sticky exception bits are set
1965 * directly by userspace, it must call prctl
1966 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1967 * in the existing prctl settings) or
1968 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1969 * the bits being set). <fenv.h> functions
1970 * saving and restoring the whole
1971 * floating-point environment need to do so
1972 * anyway to restore the prctl settings from
1973 * the saved environment.
1974 */
Christophe Leroy532ed192020-08-17 05:47:57 +00001975#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00001976 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001977 tsk->thread.fpexc_mode = val &
1978 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
Christophe Leroy532ed192020-08-17 05:47:57 +00001979#endif
Kumar Gala5e14d212007-09-13 01:44:20 -05001980 return 0;
1981 } else {
1982 return -EINVAL;
1983 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001984 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001985
1986 /* on a CONFIG_SPE this does not hurt us. The bits that
1987 * __pack_fe01 use do not overlap with bits used for
1988 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1989 * on CONFIG_SPE implementations are reserved so writing to
1990 * them does not change anything */
1991 if (val > PR_FP_EXC_PRECISE)
1992 return -EINVAL;
1993 tsk->thread.fpexc_mode = __pack_fe01(val);
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10001994 if (regs != NULL && (regs->msr & MSR_FP) != 0) {
1995 regs_set_return_msr(regs, (regs->msr & ~(MSR_FE0|MSR_FE1))
1996 | tsk->thread.fpexc_mode);
1997 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001998 return 0;
1999}
2000
2001int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
2002{
Michael Ellermand208e132020-09-17 12:20:16 +10002003 unsigned int val = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002004
Christophe Leroy532ed192020-08-17 05:47:57 +00002005 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE) {
Joseph Myers640e9222013-12-10 23:07:45 +00002006 if (cpu_has_feature(CPU_FTR_SPE)) {
2007 /*
2008 * When the sticky exception bits are set
2009 * directly by userspace, it must call prctl
2010 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
2011 * in the existing prctl settings) or
2012 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
2013 * the bits being set). <fenv.h> functions
2014 * saving and restoring the whole
2015 * floating-point environment need to do so
2016 * anyway to restore the prctl settings from
2017 * the saved environment.
2018 */
Christophe Leroy532ed192020-08-17 05:47:57 +00002019#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00002020 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05002021 val = tsk->thread.fpexc_mode;
Christophe Leroy532ed192020-08-17 05:47:57 +00002022#endif
Joseph Myers640e9222013-12-10 23:07:45 +00002023 } else
Kumar Gala5e14d212007-09-13 01:44:20 -05002024 return -EINVAL;
Christophe Leroy532ed192020-08-17 05:47:57 +00002025 } else {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002026 val = __unpack_fe01(tsk->thread.fpexc_mode);
Christophe Leroy532ed192020-08-17 05:47:57 +00002027 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002028 return put_user(val, (unsigned int __user *) adr);
2029}
2030
Paul Mackerrasfab5db92006-06-07 16:14:40 +10002031int set_endian(struct task_struct *tsk, unsigned int val)
2032{
2033 struct pt_regs *regs = tsk->thread.regs;
2034
2035 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
2036 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
2037 return -EINVAL;
2038
2039 if (regs == NULL)
2040 return -EINVAL;
2041
2042 if (val == PR_ENDIAN_BIG)
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10002043 regs_set_return_msr(regs, regs->msr & ~MSR_LE);
Paul Mackerrasfab5db92006-06-07 16:14:40 +10002044 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10002045 regs_set_return_msr(regs, regs->msr | MSR_LE);
Paul Mackerrasfab5db92006-06-07 16:14:40 +10002046 else
2047 return -EINVAL;
2048
2049 return 0;
2050}
2051
2052int get_endian(struct task_struct *tsk, unsigned long adr)
2053{
2054 struct pt_regs *regs = tsk->thread.regs;
2055 unsigned int val;
2056
2057 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
2058 !cpu_has_feature(CPU_FTR_REAL_LE))
2059 return -EINVAL;
2060
2061 if (regs == NULL)
2062 return -EINVAL;
2063
2064 if (regs->msr & MSR_LE) {
2065 if (cpu_has_feature(CPU_FTR_REAL_LE))
2066 val = PR_ENDIAN_LITTLE;
2067 else
2068 val = PR_ENDIAN_PPC_LITTLE;
2069 } else
2070 val = PR_ENDIAN_BIG;
2071
2072 return put_user(val, (unsigned int __user *)adr);
2073}
2074
Paul Mackerrase9370ae2006-06-07 16:15:39 +10002075int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
2076{
2077 tsk->thread.align_ctl = val;
2078 return 0;
2079}
2080
2081int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
2082{
2083 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
2084}
2085
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002086static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
2087 unsigned long nbytes)
2088{
2089 unsigned long stack_page;
2090 unsigned long cpu = task_cpu(p);
2091
Christophe Leroya7916a12019-01-31 10:09:00 +00002092 stack_page = (unsigned long)hardirq_ctx[cpu];
2093 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2094 return 1;
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002095
Christophe Leroya7916a12019-01-31 10:09:00 +00002096 stack_page = (unsigned long)softirq_ctx[cpu];
2097 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2098 return 1;
2099
Paul Mackerrasbb72c482007-02-19 11:42:42 +11002100 return 0;
2101}
2102
Nicholas Piggina2e36682020-03-25 20:41:44 +10002103static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p,
2104 unsigned long nbytes)
2105{
2106#ifdef CONFIG_PPC64
2107 unsigned long stack_page;
2108 unsigned long cpu = task_cpu(p);
2109
Michael Ellerman0ecf6a92021-02-03 00:02:06 +11002110 if (!paca_ptrs)
2111 return 0;
2112
Nicholas Piggina2e36682020-03-25 20:41:44 +10002113 stack_page = (unsigned long)paca_ptrs[cpu]->emergency_sp - THREAD_SIZE;
2114 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2115 return 1;
2116
2117# ifdef CONFIG_PPC_BOOK3S_64
2118 stack_page = (unsigned long)paca_ptrs[cpu]->nmi_emergency_sp - THREAD_SIZE;
2119 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2120 return 1;
2121
2122 stack_page = (unsigned long)paca_ptrs[cpu]->mc_emergency_sp - THREAD_SIZE;
2123 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2124 return 1;
2125# endif
2126#endif
2127
2128 return 0;
2129}
2130
2131
Anton Blanchard2f251942006-03-27 11:46:18 +11002132int validate_sp(unsigned long sp, struct task_struct *p,
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002133 unsigned long nbytes)
2134{
Al Viro0cec6fd2006-01-12 01:06:02 -08002135 unsigned long stack_page = (unsigned long)task_stack_page(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002136
Christophe Leroya7916a12019-01-31 10:09:00 +00002137 if (sp < THREAD_SIZE)
2138 return 0;
2139
2140 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002141 return 1;
2142
Nicholas Piggina2e36682020-03-25 20:41:44 +10002143 if (valid_irq_stack(sp, p, nbytes))
2144 return 1;
2145
2146 return valid_emergency_stack(sp, p, nbytes);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002147}
2148
Anton Blanchard2f251942006-03-27 11:46:18 +11002149EXPORT_SYMBOL(validate_sp);
2150
Kees Cook42a20f82021-09-29 15:02:14 -07002151static unsigned long ___get_wchan(struct task_struct *p)
Paul Mackerras06d67d52005-10-10 22:29:05 +10002152{
2153 unsigned long ip, sp;
2154 int count = 0;
2155
Paul Mackerras06d67d52005-10-10 22:29:05 +10002156 sp = p->thread.ksp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002157 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10002158 return 0;
2159
2160 do {
He Yinga1b29ba2022-01-20 20:44:18 -05002161 sp = READ_ONCE_NOCHECK(*(unsigned long *)sp);
Kautuk Consul4ca360f2016-04-19 15:48:21 +05302162 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD) ||
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02002163 task_is_running(p))
Paul Mackerras06d67d52005-10-10 22:29:05 +10002164 return 0;
2165 if (count > 0) {
He Yinga1b29ba2022-01-20 20:44:18 -05002166 ip = READ_ONCE_NOCHECK(((unsigned long *)sp)[STACK_FRAME_LR_SAVE]);
Paul Mackerras06d67d52005-10-10 22:29:05 +10002167 if (!in_sched_functions(ip))
2168 return ip;
2169 }
2170 } while (count++ < 16);
2171 return 0;
2172}
Paul Mackerras06d67d52005-10-10 22:29:05 +10002173
Kees Cook42a20f82021-09-29 15:02:14 -07002174unsigned long __get_wchan(struct task_struct *p)
Christophe Leroy018cce32019-01-31 10:08:52 +00002175{
2176 unsigned long ret;
2177
2178 if (!try_get_task_stack(p))
2179 return 0;
2180
Kees Cook42a20f82021-09-29 15:02:14 -07002181 ret = ___get_wchan(p);
Christophe Leroy018cce32019-01-31 10:08:52 +00002182
2183 put_task_stack(p);
2184
2185 return ret;
2186}
2187
Johannes Bergc4d04be2008-11-20 03:24:07 +00002188static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002189
Daniel Axtensb112fb92021-06-14 22:09:07 +10002190void __no_sanitize_address show_stack(struct task_struct *tsk,
2191 unsigned long *stack,
2192 const char *loglvl)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002193{
Paul Mackerras06d67d52005-10-10 22:29:05 +10002194 unsigned long sp, ip, lr, newsp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002195 int count = 0;
Paul Mackerras06d67d52005-10-10 22:29:05 +10002196 int firstframe = 1;
Naveen N. Rao7c1bb6b2019-09-05 23:50:30 +05302197 unsigned long ret_addr;
2198 int ftrace_idx = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002199
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002200 if (tsk == NULL)
2201 tsk = current;
Christophe Leroy018cce32019-01-31 10:08:52 +00002202
2203 if (!try_get_task_stack(tsk))
2204 return;
2205
2206 sp = (unsigned long) stack;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002207 if (sp == 0) {
2208 if (tsk == current)
Michael Ellerman3d13e832020-02-20 22:51:37 +11002209 sp = current_stack_frame();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002210 else
2211 sp = tsk->thread.ksp;
2212 }
2213
Paul Mackerras06d67d52005-10-10 22:29:05 +10002214 lr = 0;
Dmitry Safonovb9677a82020-06-08 21:31:14 -07002215 printk("%sCall Trace:\n", loglvl);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002216 do {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002217 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
Christophe Leroy018cce32019-01-31 10:08:52 +00002218 break;
Paul Mackerras06d67d52005-10-10 22:29:05 +10002219
2220 stack = (unsigned long *) sp;
2221 newsp = stack[0];
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002222 ip = stack[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10002223 if (!firstframe || ip != lr) {
Dmitry Safonovb9677a82020-06-08 21:31:14 -07002224 printk("%s["REG"] ["REG"] %pS",
2225 loglvl, sp, ip, (void *)ip);
Naveen N. Rao7c1bb6b2019-09-05 23:50:30 +05302226 ret_addr = ftrace_graph_ret_addr(current,
2227 &ftrace_idx, ip, stack);
2228 if (ret_addr != ip)
2229 pr_cont(" (%pS)", (void *)ret_addr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10002230 if (firstframe)
Michael Ellerman9a1f4902016-11-02 22:20:46 +11002231 pr_cont(" (unreliable)");
2232 pr_cont("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002233 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10002234 firstframe = 0;
2235
2236 /*
2237 * See if this is an exception frame.
2238 * We look for the "regshere" marker in the current frame.
2239 */
Michael Ellermane3de1e22021-02-10 00:59:20 +11002240 if (validate_sp(sp, tsk, STACK_FRAME_WITH_PT_REGS)
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10002241 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10002242 struct pt_regs *regs = (struct pt_regs *)
2243 (sp + STACK_FRAME_OVERHEAD);
Nicholas Pigginbf13718b2020-11-07 12:33:05 +10002244
Paul Mackerras06d67d52005-10-10 22:29:05 +10002245 lr = regs->link;
Nicholas Pigginbf13718b2020-11-07 12:33:05 +10002246 printk("%s--- interrupt: %lx at %pS\n",
2247 loglvl, regs->trap, (void *)regs->nip);
2248 __show_regs(regs);
2249 printk("%s--- interrupt: %lx\n",
2250 loglvl, regs->trap);
2251
Paul Mackerras06d67d52005-10-10 22:29:05 +10002252 firstframe = 1;
2253 }
2254
2255 sp = newsp;
2256 } while (count++ < kstack_depth_to_print);
Christophe Leroy018cce32019-01-31 10:08:52 +00002257
2258 put_task_stack(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002259}
Paul Mackerras06d67d52005-10-10 22:29:05 +10002260
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002261#ifdef CONFIG_PPC64
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002262/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10002263void notrace __ppc64_runlatch_on(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002264{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002265 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002266
Nicholas Piggind1d0d5f2017-08-12 02:39:07 +10002267 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2268 /*
2269 * Least significant bit (RUN) is the only writable bit of
2270 * the CTRL register, so we can avoid mfspr. 2.06 is not the
2271 * earliest ISA where this is the case, but it's convenient.
2272 */
2273 mtspr(SPRN_CTRLT, CTRL_RUNLATCH);
2274 } else {
2275 unsigned long ctrl;
2276
2277 /*
2278 * Some architectures (e.g., Cell) have writable fields other
2279 * than RUN, so do the read-modify-write.
2280 */
2281 ctrl = mfspr(SPRN_CTRLF);
2282 ctrl |= CTRL_RUNLATCH;
2283 mtspr(SPRN_CTRLT, ctrl);
2284 }
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002285
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10002286 ti->local_flags |= _TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002287}
2288
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002289/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10002290void notrace __ppc64_runlatch_off(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002291{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002292 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002293
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10002294 ti->local_flags &= ~_TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002295
Nicholas Piggind1d0d5f2017-08-12 02:39:07 +10002296 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2297 mtspr(SPRN_CTRLT, 0);
2298 } else {
2299 unsigned long ctrl;
2300
2301 ctrl = mfspr(SPRN_CTRLF);
2302 ctrl &= ~CTRL_RUNLATCH;
2303 mtspr(SPRN_CTRLT, ctrl);
2304 }
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11002305}
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11002306#endif /* CONFIG_PPC64 */
Benjamin Herrenschmidtf6a61682008-04-18 16:56:17 +10002307
Anton Blanchardd8390882009-02-22 01:50:03 +00002308unsigned long arch_align_stack(unsigned long sp)
2309{
2310 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2311 sp -= get_random_int() & ~PAGE_MASK;
2312 return sp & ~0xf;
2313}