blob: c5c3ae2ef3c10258418a2c45b62ef21634b8560d [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002 * Derived from "arch/i386/kernel/process.c"
3 * Copyright (C) 1995 Linus Torvalds
4 *
5 * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
6 * Paul Mackerras (paulus@cs.anu.edu.au)
7 *
8 * PowerPC version
9 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
Paul Mackerras14cf11a2005-09-26 16:04:21 +100017#include <linux/errno.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/smp.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100022#include <linux/stddef.h>
23#include <linux/unistd.h>
24#include <linux/ptrace.h>
25#include <linux/slab.h>
26#include <linux/user.h>
27#include <linux/elf.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100028#include <linux/prctl.h>
29#include <linux/init_task.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040030#include <linux/export.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100031#include <linux/kallsyms.h>
32#include <linux/mqueue.h>
33#include <linux/hardirq.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100034#include <linux/utsname.h>
Steven Rostedt6794c782009-02-09 21:10:27 -080035#include <linux/ftrace.h>
Martin Schwidefsky79741dd2008-12-31 15:11:38 +010036#include <linux/kernel_stat.h>
Anton Blanchardd8390882009-02-22 01:50:03 +000037#include <linux/personality.h>
38#include <linux/random.h>
K.Prasad5aae8a52010-06-15 11:35:19 +053039#include <linux/hw_breakpoint.h>
Anton Blanchard7b051f62014-10-13 20:27:15 +110040#include <linux/uaccess.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110041#include <linux/elf-randomize.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100042
43#include <asm/pgtable.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044#include <asm/io.h>
45#include <asm/processor.h>
46#include <asm/mmu.h>
47#include <asm/prom.h>
Michael Ellerman76032de2005-11-07 13:12:03 +110048#include <asm/machdep.h>
Paul Mackerrasc6622f62006-02-24 10:06:59 +110049#include <asm/time.h>
David Howellsae3a1972012-03-28 18:30:02 +010050#include <asm/runlatch.h>
Arnd Bergmanna7f31842006-03-23 00:00:08 +010051#include <asm/syscalls.h>
David Howellsae3a1972012-03-28 18:30:02 +010052#include <asm/switch_to.h>
Michael Neulingfb096922013-02-13 16:21:37 +000053#include <asm/tm.h>
David Howellsae3a1972012-03-28 18:30:02 +010054#include <asm/debug.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100055#ifdef CONFIG_PPC64
56#include <asm/firmware.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100057#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +110058#include <asm/code-patching.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110059#include <asm/exec.h>
Michael Ellerman5d31a962016-03-24 22:04:04 +110060#include <asm/livepatch.h>
61
Luis Machadod6a61bf2008-07-24 02:10:41 +100062#include <linux/kprobes.h>
63#include <linux/kdebug.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100064
Michael Neuling8b3c34c2013-02-13 16:21:32 +000065/* Transactional Memory debug */
66#ifdef TM_DEBUG_SW
67#define TM_DEBUG(x...) printk(KERN_INFO x)
68#else
69#define TM_DEBUG(x...) do { } while(0)
70#endif
71
Paul Mackerras14cf11a2005-09-26 16:04:21 +100072extern unsigned long _get_SP(void);
73
Paul Mackerrasd31626f2014-01-13 15:56:29 +110074#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchardb86fd2b2015-10-29 11:43:58 +110075static void check_if_tm_restore_required(struct task_struct *tsk)
Paul Mackerrasd31626f2014-01-13 15:56:29 +110076{
77 /*
78 * If we are saving the current thread's registers, and the
79 * thread is in a transactional state, set the TIF_RESTORE_TM
80 * bit so that we know to restore the registers before
81 * returning to userspace.
82 */
83 if (tsk == current && tsk->thread.regs &&
84 MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
85 !test_thread_flag(TIF_RESTORE_TM)) {
Anshuman Khandual829023d2015-07-06 16:24:10 +053086 tsk->thread.ckpt_regs.msr = tsk->thread.regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +110087 set_thread_flag(TIF_RESTORE_TM);
88 }
Paul Mackerrasd31626f2014-01-13 15:56:29 +110089}
Paul Mackerrasd31626f2014-01-13 15:56:29 +110090#else
Anton Blanchardb86fd2b2015-10-29 11:43:58 +110091static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
Paul Mackerrasd31626f2014-01-13 15:56:29 +110092#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
93
Anton Blanchard3eb5d582015-10-29 11:44:06 +110094bool strict_msr_control;
95EXPORT_SYMBOL(strict_msr_control);
96
97static int __init enable_strict_msr_control(char *str)
98{
99 strict_msr_control = true;
100 pr_info("Enabling strict facility control\n");
101
102 return 0;
103}
104early_param("ppc_strict_facility_enable", enable_strict_msr_control);
105
106void msr_check_and_set(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100107{
108 unsigned long oldmsr = mfmsr();
109 unsigned long newmsr;
110
111 newmsr = oldmsr | bits;
112
113#ifdef CONFIG_VSX
114 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
115 newmsr |= MSR_VSX;
116#endif
117
118 if (oldmsr != newmsr)
119 mtmsr_isync(newmsr);
120}
121
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100122void __msr_check_and_clear(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100123{
124 unsigned long oldmsr = mfmsr();
125 unsigned long newmsr;
126
127 newmsr = oldmsr & ~bits;
128
129#ifdef CONFIG_VSX
130 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
131 newmsr &= ~MSR_VSX;
132#endif
133
134 if (oldmsr != newmsr)
135 mtmsr_isync(newmsr);
136}
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100137EXPORT_SYMBOL(__msr_check_and_clear);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100138
Kevin Hao037f0ee2013-07-14 17:02:05 +0800139#ifdef CONFIG_PPC_FPU
Cyril Bur87924682016-02-29 17:53:49 +1100140void __giveup_fpu(struct task_struct *tsk)
141{
Anton Blanchard8eb98032016-05-29 22:03:50 +1000142 unsigned long msr;
143
Cyril Bur87924682016-02-29 17:53:49 +1100144 save_fpu(tsk);
Anton Blanchard8eb98032016-05-29 22:03:50 +1000145 msr = tsk->thread.regs->msr;
146 msr &= ~MSR_FP;
Cyril Bur87924682016-02-29 17:53:49 +1100147#ifdef CONFIG_VSX
148 if (cpu_has_feature(CPU_FTR_VSX))
Anton Blanchard8eb98032016-05-29 22:03:50 +1000149 msr &= ~MSR_VSX;
Cyril Bur87924682016-02-29 17:53:49 +1100150#endif
Anton Blanchard8eb98032016-05-29 22:03:50 +1000151 tsk->thread.regs->msr = msr;
Cyril Bur87924682016-02-29 17:53:49 +1100152}
153
Anton Blanchard98da5812015-10-29 11:44:01 +1100154void giveup_fpu(struct task_struct *tsk)
155{
Anton Blanchard98da5812015-10-29 11:44:01 +1100156 check_if_tm_restore_required(tsk);
157
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100158 msr_check_and_set(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100159 __giveup_fpu(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100160 msr_check_and_clear(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100161}
162EXPORT_SYMBOL(giveup_fpu);
163
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000164/*
165 * Make sure the floating-point register state in the
166 * the thread_struct is up to date for task tsk.
167 */
168void flush_fp_to_thread(struct task_struct *tsk)
169{
170 if (tsk->thread.regs) {
171 /*
172 * We need to disable preemption here because if we didn't,
173 * another process could get scheduled after the regs->msr
174 * test but before we have finished saving the FP registers
175 * to the thread_struct. That process could take over the
176 * FPU, and then when we get scheduled again we would store
177 * bogus values for the remaining FP registers.
178 */
179 preempt_disable();
180 if (tsk->thread.regs->msr & MSR_FP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000181 /*
182 * This should only ever be called for current or
183 * for a stopped child process. Since we save away
Anton Blanchardaf1bbc32015-10-29 11:43:57 +1100184 * the FP register state on context switch,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000185 * there is something wrong if a stopped child appears
186 * to still have its FP state in the CPU registers.
187 */
188 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100189 giveup_fpu(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000190 }
191 preempt_enable();
192 }
193}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000194EXPORT_SYMBOL_GPL(flush_fp_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000195
196void enable_kernel_fp(void)
197{
198 WARN_ON(preemptible());
199
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100200 msr_check_and_set(MSR_FP);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100201
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100202 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
203 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100204 __giveup_fpu(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100205 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000206}
207EXPORT_SYMBOL(enable_kernel_fp);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100208
209static int restore_fp(struct task_struct *tsk) {
210 if (tsk->thread.load_fp) {
211 load_fp_state(&current->thread.fp_state);
212 current->thread.load_fp++;
213 return 1;
214 }
215 return 0;
216}
217#else
218static int restore_fp(struct task_struct *tsk) { return 0; }
Anton Blanchardd1e1cf22015-10-29 11:44:11 +1100219#endif /* CONFIG_PPC_FPU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000220
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000221#ifdef CONFIG_ALTIVEC
Cyril Bur70fe3d92016-02-29 17:53:47 +1100222#define loadvec(thr) ((thr).load_vec)
223
Cyril Bur6f515d82016-02-29 17:53:50 +1100224static void __giveup_altivec(struct task_struct *tsk)
225{
Anton Blanchard8eb98032016-05-29 22:03:50 +1000226 unsigned long msr;
227
Cyril Bur6f515d82016-02-29 17:53:50 +1100228 save_altivec(tsk);
Anton Blanchard8eb98032016-05-29 22:03:50 +1000229 msr = tsk->thread.regs->msr;
230 msr &= ~MSR_VEC;
Cyril Bur6f515d82016-02-29 17:53:50 +1100231#ifdef CONFIG_VSX
232 if (cpu_has_feature(CPU_FTR_VSX))
Anton Blanchard8eb98032016-05-29 22:03:50 +1000233 msr &= ~MSR_VSX;
Cyril Bur6f515d82016-02-29 17:53:50 +1100234#endif
Anton Blanchard8eb98032016-05-29 22:03:50 +1000235 tsk->thread.regs->msr = msr;
Cyril Bur6f515d82016-02-29 17:53:50 +1100236}
237
Anton Blanchard98da5812015-10-29 11:44:01 +1100238void giveup_altivec(struct task_struct *tsk)
239{
Anton Blanchard98da5812015-10-29 11:44:01 +1100240 check_if_tm_restore_required(tsk);
241
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100242 msr_check_and_set(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100243 __giveup_altivec(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100244 msr_check_and_clear(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100245}
246EXPORT_SYMBOL(giveup_altivec);
247
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000248void enable_kernel_altivec(void)
249{
250 WARN_ON(preemptible());
251
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100252 msr_check_and_set(MSR_VEC);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100253
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100254 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
255 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100256 __giveup_altivec(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100257 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000258}
259EXPORT_SYMBOL(enable_kernel_altivec);
260
261/*
262 * Make sure the VMX/Altivec register state in the
263 * the thread_struct is up to date for task tsk.
264 */
265void flush_altivec_to_thread(struct task_struct *tsk)
266{
267 if (tsk->thread.regs) {
268 preempt_disable();
269 if (tsk->thread.regs->msr & MSR_VEC) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000270 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100271 giveup_altivec(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000272 }
273 preempt_enable();
274 }
275}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000276EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100277
278static int restore_altivec(struct task_struct *tsk)
279{
280 if (cpu_has_feature(CPU_FTR_ALTIVEC) && tsk->thread.load_vec) {
281 load_vr_state(&tsk->thread.vr_state);
282 tsk->thread.used_vr = 1;
283 tsk->thread.load_vec++;
284
285 return 1;
286 }
287 return 0;
288}
289#else
290#define loadvec(thr) 0
291static inline int restore_altivec(struct task_struct *tsk) { return 0; }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000292#endif /* CONFIG_ALTIVEC */
293
Michael Neulingce48b212008-06-25 14:07:18 +1000294#ifdef CONFIG_VSX
Cyril Burbf6a4d52016-02-29 17:53:51 +1100295static void __giveup_vsx(struct task_struct *tsk)
Anton Blancharda7d623d2015-10-29 11:44:02 +1100296{
Anton Blancharda7d623d2015-10-29 11:44:02 +1100297 if (tsk->thread.regs->msr & MSR_FP)
298 __giveup_fpu(tsk);
299 if (tsk->thread.regs->msr & MSR_VEC)
300 __giveup_altivec(tsk);
Cyril Burbf6a4d52016-02-29 17:53:51 +1100301 tsk->thread.regs->msr &= ~MSR_VSX;
302}
303
304static void giveup_vsx(struct task_struct *tsk)
305{
306 check_if_tm_restore_required(tsk);
307
308 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100309 __giveup_vsx(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100310 msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100311}
Cyril Burbf6a4d52016-02-29 17:53:51 +1100312
313static void save_vsx(struct task_struct *tsk)
314{
315 if (tsk->thread.regs->msr & MSR_FP)
316 save_fpu(tsk);
317 if (tsk->thread.regs->msr & MSR_VEC)
318 save_altivec(tsk);
319}
Anton Blancharda7d623d2015-10-29 11:44:02 +1100320
Michael Neulingce48b212008-06-25 14:07:18 +1000321void enable_kernel_vsx(void)
322{
323 WARN_ON(preemptible());
324
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100325 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100326
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100327 if (current->thread.regs && (current->thread.regs->msr & MSR_VSX)) {
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100328 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100329 if (current->thread.regs->msr & MSR_FP)
330 __giveup_fpu(current);
331 if (current->thread.regs->msr & MSR_VEC)
332 __giveup_altivec(current);
333 __giveup_vsx(current);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100334 }
Michael Neulingce48b212008-06-25 14:07:18 +1000335}
336EXPORT_SYMBOL(enable_kernel_vsx);
Michael Neulingce48b212008-06-25 14:07:18 +1000337
338void flush_vsx_to_thread(struct task_struct *tsk)
339{
340 if (tsk->thread.regs) {
341 preempt_disable();
342 if (tsk->thread.regs->msr & MSR_VSX) {
Michael Neulingce48b212008-06-25 14:07:18 +1000343 BUG_ON(tsk != current);
Michael Neulingce48b212008-06-25 14:07:18 +1000344 giveup_vsx(tsk);
345 }
346 preempt_enable();
347 }
348}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000349EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100350
351static int restore_vsx(struct task_struct *tsk)
352{
353 if (cpu_has_feature(CPU_FTR_VSX)) {
354 tsk->thread.used_vsr = 1;
355 return 1;
356 }
357
358 return 0;
359}
360#else
361static inline int restore_vsx(struct task_struct *tsk) { return 0; }
Cyril Burbf6a4d52016-02-29 17:53:51 +1100362static inline void save_vsx(struct task_struct *tsk) { }
Michael Neulingce48b212008-06-25 14:07:18 +1000363#endif /* CONFIG_VSX */
364
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000365#ifdef CONFIG_SPE
Anton Blanchard98da5812015-10-29 11:44:01 +1100366void giveup_spe(struct task_struct *tsk)
367{
Anton Blanchard98da5812015-10-29 11:44:01 +1100368 check_if_tm_restore_required(tsk);
369
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100370 msr_check_and_set(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100371 __giveup_spe(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100372 msr_check_and_clear(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100373}
374EXPORT_SYMBOL(giveup_spe);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000375
376void enable_kernel_spe(void)
377{
378 WARN_ON(preemptible());
379
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100380 msr_check_and_set(MSR_SPE);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100381
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100382 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
383 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100384 __giveup_spe(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100385 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000386}
387EXPORT_SYMBOL(enable_kernel_spe);
388
389void flush_spe_to_thread(struct task_struct *tsk)
390{
391 if (tsk->thread.regs) {
392 preempt_disable();
393 if (tsk->thread.regs->msr & MSR_SPE) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000394 BUG_ON(tsk != current);
yu liu685659e2011-06-14 18:34:25 -0500395 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
Kumar Gala0ee6c152007-08-28 21:15:53 -0500396 giveup_spe(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000397 }
398 preempt_enable();
399 }
400}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000401#endif /* CONFIG_SPE */
402
Anton Blanchardc2085052015-10-29 11:44:08 +1100403static unsigned long msr_all_available;
404
405static int __init init_msr_all_available(void)
406{
407#ifdef CONFIG_PPC_FPU
408 msr_all_available |= MSR_FP;
409#endif
410#ifdef CONFIG_ALTIVEC
411 if (cpu_has_feature(CPU_FTR_ALTIVEC))
412 msr_all_available |= MSR_VEC;
413#endif
414#ifdef CONFIG_VSX
415 if (cpu_has_feature(CPU_FTR_VSX))
416 msr_all_available |= MSR_VSX;
417#endif
418#ifdef CONFIG_SPE
419 if (cpu_has_feature(CPU_FTR_SPE))
420 msr_all_available |= MSR_SPE;
421#endif
422
423 return 0;
424}
425early_initcall(init_msr_all_available);
426
427void giveup_all(struct task_struct *tsk)
428{
429 unsigned long usermsr;
430
431 if (!tsk->thread.regs)
432 return;
433
434 usermsr = tsk->thread.regs->msr;
435
436 if ((usermsr & msr_all_available) == 0)
437 return;
438
439 msr_check_and_set(msr_all_available);
440
441#ifdef CONFIG_PPC_FPU
442 if (usermsr & MSR_FP)
443 __giveup_fpu(tsk);
444#endif
445#ifdef CONFIG_ALTIVEC
446 if (usermsr & MSR_VEC)
447 __giveup_altivec(tsk);
448#endif
449#ifdef CONFIG_VSX
450 if (usermsr & MSR_VSX)
451 __giveup_vsx(tsk);
452#endif
453#ifdef CONFIG_SPE
454 if (usermsr & MSR_SPE)
455 __giveup_spe(tsk);
456#endif
457
458 msr_check_and_clear(msr_all_available);
459}
460EXPORT_SYMBOL(giveup_all);
461
Cyril Bur70fe3d92016-02-29 17:53:47 +1100462void restore_math(struct pt_regs *regs)
463{
464 unsigned long msr;
465
466 if (!current->thread.load_fp && !loadvec(current->thread))
467 return;
468
469 msr = regs->msr;
470 msr_check_and_set(msr_all_available);
471
472 /*
473 * Only reload if the bit is not set in the user MSR, the bit BEING set
474 * indicates that the registers are hot
475 */
476 if ((!(msr & MSR_FP)) && restore_fp(current))
477 msr |= MSR_FP | current->thread.fpexc_mode;
478
479 if ((!(msr & MSR_VEC)) && restore_altivec(current))
480 msr |= MSR_VEC;
481
482 if ((msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC) &&
483 restore_vsx(current)) {
484 msr |= MSR_VSX;
485 }
486
487 msr_check_and_clear(msr_all_available);
488
489 regs->msr = msr;
490}
491
Cyril Burde2a20a2016-02-29 17:53:48 +1100492void save_all(struct task_struct *tsk)
493{
494 unsigned long usermsr;
495
496 if (!tsk->thread.regs)
497 return;
498
499 usermsr = tsk->thread.regs->msr;
500
501 if ((usermsr & msr_all_available) == 0)
502 return;
503
504 msr_check_and_set(msr_all_available);
505
Cyril Burbf6a4d52016-02-29 17:53:51 +1100506 /*
507 * Saving the way the register space is in hardware, save_vsx boils
508 * down to a save_fpu() and save_altivec()
509 */
510 if (usermsr & MSR_VSX) {
511 save_vsx(tsk);
512 } else {
513 if (usermsr & MSR_FP)
514 save_fpu(tsk);
Cyril Burde2a20a2016-02-29 17:53:48 +1100515
Cyril Burbf6a4d52016-02-29 17:53:51 +1100516 if (usermsr & MSR_VEC)
517 save_altivec(tsk);
518 }
Cyril Burde2a20a2016-02-29 17:53:48 +1100519
520 if (usermsr & MSR_SPE)
521 __giveup_spe(tsk);
522
523 msr_check_and_clear(msr_all_available);
524}
525
Anton Blanchard579e6332015-10-29 11:44:09 +1100526void flush_all_to_thread(struct task_struct *tsk)
527{
528 if (tsk->thread.regs) {
529 preempt_disable();
530 BUG_ON(tsk != current);
Cyril Burde2a20a2016-02-29 17:53:48 +1100531 save_all(tsk);
Anton Blanchard579e6332015-10-29 11:44:09 +1100532
533#ifdef CONFIG_SPE
534 if (tsk->thread.regs->msr & MSR_SPE)
535 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
536#endif
537
538 preempt_enable();
539 }
540}
541EXPORT_SYMBOL(flush_all_to_thread);
542
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000543#ifdef CONFIG_PPC_ADV_DEBUG_REGS
544void do_send_trap(struct pt_regs *regs, unsigned long address,
545 unsigned long error_code, int signal_code, int breakpt)
546{
547 siginfo_t info;
548
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000549 current->thread.trap_nr = signal_code;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000550 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
551 11, SIGSEGV) == NOTIFY_STOP)
552 return;
553
554 /* Deliver the signal to userspace */
555 info.si_signo = SIGTRAP;
556 info.si_errno = breakpt; /* breakpoint or watchpoint id */
557 info.si_code = signal_code;
558 info.si_addr = (void __user *)address;
559 force_sig_info(SIGTRAP, &info, current);
560}
561#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Michael Neuling9422de32012-12-20 14:06:44 +0000562void do_break (struct pt_regs *regs, unsigned long address,
Luis Machadod6a61bf2008-07-24 02:10:41 +1000563 unsigned long error_code)
564{
565 siginfo_t info;
566
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000567 current->thread.trap_nr = TRAP_HWBKPT;
Luis Machadod6a61bf2008-07-24 02:10:41 +1000568 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
569 11, SIGSEGV) == NOTIFY_STOP)
570 return;
571
Michael Neuling9422de32012-12-20 14:06:44 +0000572 if (debugger_break_match(regs))
Luis Machadod6a61bf2008-07-24 02:10:41 +1000573 return;
574
Michael Neuling9422de32012-12-20 14:06:44 +0000575 /* Clear the breakpoint */
576 hw_breakpoint_disable();
Luis Machadod6a61bf2008-07-24 02:10:41 +1000577
578 /* Deliver the signal to userspace */
579 info.si_signo = SIGTRAP;
580 info.si_errno = 0;
581 info.si_code = TRAP_HWBKPT;
582 info.si_addr = (void __user *)address;
583 force_sig_info(SIGTRAP, &info, current);
584}
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000585#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000586
Michael Neuling9422de32012-12-20 14:06:44 +0000587static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk);
Michael Ellermana2ceff52008-03-28 19:11:48 +1100588
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000589#ifdef CONFIG_PPC_ADV_DEBUG_REGS
590/*
591 * Set the debug registers back to their default "safe" values.
592 */
593static void set_debug_reg_defaults(struct thread_struct *thread)
594{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530595 thread->debug.iac1 = thread->debug.iac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000596#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530597 thread->debug.iac3 = thread->debug.iac4 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000598#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530599 thread->debug.dac1 = thread->debug.dac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000600#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530601 thread->debug.dvc1 = thread->debug.dvc2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000602#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530603 thread->debug.dbcr0 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000604#ifdef CONFIG_BOOKE
605 /*
606 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
607 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530608 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000609 DBCR1_IAC3US | DBCR1_IAC4US;
610 /*
611 * Force Data Address Compare User/Supervisor bits to be User-only
612 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
613 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530614 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000615#else
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530616 thread->debug.dbcr1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000617#endif
618}
619
Scott Woodf5f97212013-11-22 15:52:29 -0600620static void prime_debug_regs(struct debug_reg *debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000621{
Scott Wood6cecf762013-05-13 14:14:53 +0000622 /*
623 * We could have inherited MSR_DE from userspace, since
624 * it doesn't get cleared on exception entry. Make sure
625 * MSR_DE is clear before we enable any debug events.
626 */
627 mtmsr(mfmsr() & ~MSR_DE);
628
Scott Woodf5f97212013-11-22 15:52:29 -0600629 mtspr(SPRN_IAC1, debug->iac1);
630 mtspr(SPRN_IAC2, debug->iac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000631#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Scott Woodf5f97212013-11-22 15:52:29 -0600632 mtspr(SPRN_IAC3, debug->iac3);
633 mtspr(SPRN_IAC4, debug->iac4);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000634#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600635 mtspr(SPRN_DAC1, debug->dac1);
636 mtspr(SPRN_DAC2, debug->dac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000637#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Scott Woodf5f97212013-11-22 15:52:29 -0600638 mtspr(SPRN_DVC1, debug->dvc1);
639 mtspr(SPRN_DVC2, debug->dvc2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000640#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600641 mtspr(SPRN_DBCR0, debug->dbcr0);
642 mtspr(SPRN_DBCR1, debug->dbcr1);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000643#ifdef CONFIG_BOOKE
Scott Woodf5f97212013-11-22 15:52:29 -0600644 mtspr(SPRN_DBCR2, debug->dbcr2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000645#endif
646}
647/*
648 * Unless neither the old or new thread are making use of the
649 * debug registers, set the debug registers from the values
650 * stored in the new thread.
651 */
Scott Woodf5f97212013-11-22 15:52:29 -0600652void switch_booke_debug_regs(struct debug_reg *new_debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000653{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530654 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
Scott Woodf5f97212013-11-22 15:52:29 -0600655 || (new_debug->dbcr0 & DBCR0_IDM))
656 prime_debug_regs(new_debug);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000657}
Bharat Bhushan3743c9b2013-07-04 12:27:44 +0530658EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000659#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
K.Prasade0780b72011-02-10 04:44:35 +0000660#ifndef CONFIG_HAVE_HW_BREAKPOINT
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000661static void set_debug_reg_defaults(struct thread_struct *thread)
662{
Michael Neuling9422de32012-12-20 14:06:44 +0000663 thread->hw_brk.address = 0;
664 thread->hw_brk.type = 0;
Michael Neulingb9818c32013-01-10 14:25:34 +0000665 set_breakpoint(&thread->hw_brk);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000666}
K.Prasade0780b72011-02-10 04:44:35 +0000667#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000668#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
669
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000670#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling9422de32012-12-20 14:06:44 +0000671static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
672{
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000673 mtspr(SPRN_DAC1, dabr);
Dave Kleikamp221c1852010-03-05 10:43:24 +0000674#ifdef CONFIG_PPC_47x
675 isync();
676#endif
Michael Neuling9422de32012-12-20 14:06:44 +0000677 return 0;
678}
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000679#elif defined(CONFIG_PPC_BOOK3S)
Michael Neuling9422de32012-12-20 14:06:44 +0000680static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
681{
Michael Ellermancab0af92005-11-03 15:30:49 +1100682 mtspr(SPRN_DABR, dabr);
Michael Neuling82a9f162013-05-16 20:27:31 +0000683 if (cpu_has_feature(CPU_FTR_DABRX))
684 mtspr(SPRN_DABRX, dabrx);
Michael Ellermancab0af92005-11-03 15:30:49 +1100685 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000686}
Michael Neuling9422de32012-12-20 14:06:44 +0000687#else
688static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
689{
690 return -EINVAL;
691}
692#endif
693
694static inline int set_dabr(struct arch_hw_breakpoint *brk)
695{
696 unsigned long dabr, dabrx;
697
698 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
699 dabrx = ((brk->type >> 3) & 0x7);
700
701 if (ppc_md.set_dabr)
702 return ppc_md.set_dabr(dabr, dabrx);
703
704 return __set_dabr(dabr, dabrx);
705}
706
Michael Neulingbf99de32012-12-20 14:06:45 +0000707static inline int set_dawr(struct arch_hw_breakpoint *brk)
708{
Michael Neuling05d694e2013-01-24 15:02:58 +0000709 unsigned long dawr, dawrx, mrd;
Michael Neulingbf99de32012-12-20 14:06:45 +0000710
711 dawr = brk->address;
712
713 dawrx = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE)) \
714 << (63 - 58); //* read/write bits */
715 dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) \
716 << (63 - 59); //* translate */
717 dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) \
718 >> 3; //* PRIM bits */
Michael Neuling05d694e2013-01-24 15:02:58 +0000719 /* dawr length is stored in field MDR bits 48:53. Matches range in
720 doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
721 0b111111=64DW.
722 brk->len is in bytes.
723 This aligns up to double word size, shifts and does the bias.
724 */
725 mrd = ((brk->len + 7) >> 3) - 1;
726 dawrx |= (mrd & 0x3f) << (63 - 53);
Michael Neulingbf99de32012-12-20 14:06:45 +0000727
728 if (ppc_md.set_dawr)
729 return ppc_md.set_dawr(dawr, dawrx);
730 mtspr(SPRN_DAWR, dawr);
731 mtspr(SPRN_DAWRX, dawrx);
732 return 0;
733}
734
Paul Gortmaker21f58502014-04-29 15:25:17 -0400735void __set_breakpoint(struct arch_hw_breakpoint *brk)
Michael Neuling9422de32012-12-20 14:06:44 +0000736{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500737 memcpy(this_cpu_ptr(&current_brk), brk, sizeof(*brk));
Michael Neuling9422de32012-12-20 14:06:44 +0000738
Michael Neulingbf99de32012-12-20 14:06:45 +0000739 if (cpu_has_feature(CPU_FTR_DAWR))
Paul Gortmaker04c32a52014-04-29 15:25:16 -0400740 set_dawr(brk);
741 else
742 set_dabr(brk);
Michael Neuling9422de32012-12-20 14:06:44 +0000743}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000744
Paul Gortmaker21f58502014-04-29 15:25:17 -0400745void set_breakpoint(struct arch_hw_breakpoint *brk)
746{
747 preempt_disable();
748 __set_breakpoint(brk);
749 preempt_enable();
750}
751
Paul Mackerras06d67d52005-10-10 22:29:05 +1000752#ifdef CONFIG_PPC64
753DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000754#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000755
Michael Neuling9422de32012-12-20 14:06:44 +0000756static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
757 struct arch_hw_breakpoint *b)
758{
759 if (a->address != b->address)
760 return false;
761 if (a->type != b->type)
762 return false;
763 if (a->len != b->len)
764 return false;
765 return true;
766}
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100767
Michael Neulingfb096922013-02-13 16:21:37 +0000768#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100769static void tm_reclaim_thread(struct thread_struct *thr,
770 struct thread_info *ti, uint8_t cause)
771{
772 unsigned long msr_diff = 0;
773
774 /*
775 * If FP/VSX registers have been already saved to the
776 * thread_struct, move them to the transact_fp array.
777 * We clear the TIF_RESTORE_TM bit since after the reclaim
778 * the thread will no longer be transactional.
779 */
780 if (test_ti_thread_flag(ti, TIF_RESTORE_TM)) {
Anshuman Khandual829023d2015-07-06 16:24:10 +0530781 msr_diff = thr->ckpt_regs.msr & ~thr->regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100782 if (msr_diff & MSR_FP)
783 memcpy(&thr->transact_fp, &thr->fp_state,
784 sizeof(struct thread_fp_state));
785 if (msr_diff & MSR_VEC)
786 memcpy(&thr->transact_vr, &thr->vr_state,
787 sizeof(struct thread_vr_state));
788 clear_ti_thread_flag(ti, TIF_RESTORE_TM);
789 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX | MSR_FE0 | MSR_FE1;
790 }
791
Michael Neuling7f821fc2015-11-19 15:44:45 +1100792 /*
793 * Use the current MSR TM suspended bit to track if we have
794 * checkpointed state outstanding.
795 * On signal delivery, we'd normally reclaim the checkpointed
796 * state to obtain stack pointer (see:get_tm_stackpointer()).
797 * This will then directly return to userspace without going
798 * through __switch_to(). However, if the stack frame is bad,
799 * we need to exit this thread which calls __switch_to() which
800 * will again attempt to reclaim the already saved tm state.
801 * Hence we need to check that we've not already reclaimed
802 * this state.
803 * We do this using the current MSR, rather tracking it in
804 * some specific thread_struct bit, as it has the additional
Michael Ellerman027dfac2016-06-01 16:34:37 +1000805 * benefit of checking for a potential TM bad thing exception.
Michael Neuling7f821fc2015-11-19 15:44:45 +1100806 */
807 if (!MSR_TM_SUSPENDED(mfmsr()))
808 return;
809
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100810 tm_reclaim(thr, thr->regs->msr, cause);
811
812 /* Having done the reclaim, we now have the checkpointed
813 * FP/VSX values in the registers. These might be valid
814 * even if we have previously called enable_kernel_fp() or
815 * flush_fp_to_thread(), so update thr->regs->msr to
816 * indicate their current validity.
817 */
818 thr->regs->msr |= msr_diff;
819}
820
821void tm_reclaim_current(uint8_t cause)
822{
823 tm_enable();
824 tm_reclaim_thread(&current->thread, current_thread_info(), cause);
825}
826
Michael Neulingfb096922013-02-13 16:21:37 +0000827static inline void tm_reclaim_task(struct task_struct *tsk)
828{
829 /* We have to work out if we're switching from/to a task that's in the
830 * middle of a transaction.
831 *
832 * In switching we need to maintain a 2nd register state as
833 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
834 * checkpointed (tbegin) state in ckpt_regs and saves the transactional
835 * (current) FPRs into oldtask->thread.transact_fpr[].
836 *
837 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
838 */
839 struct thread_struct *thr = &tsk->thread;
840
841 if (!thr->regs)
842 return;
843
844 if (!MSR_TM_ACTIVE(thr->regs->msr))
845 goto out_and_saveregs;
846
847 /* Stash the original thread MSR, as giveup_fpu et al will
848 * modify it. We hold onto it to see whether the task used
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100849 * FP & vector regs. If the TIF_RESTORE_TM flag is set,
Anshuman Khandual829023d2015-07-06 16:24:10 +0530850 * ckpt_regs.msr is already set.
Michael Neulingfb096922013-02-13 16:21:37 +0000851 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100852 if (!test_ti_thread_flag(task_thread_info(tsk), TIF_RESTORE_TM))
Anshuman Khandual829023d2015-07-06 16:24:10 +0530853 thr->ckpt_regs.msr = thr->regs->msr;
Michael Neulingfb096922013-02-13 16:21:37 +0000854
855 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
856 "ccr=%lx, msr=%lx, trap=%lx)\n",
857 tsk->pid, thr->regs->nip,
858 thr->regs->ccr, thr->regs->msr,
859 thr->regs->trap);
860
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100861 tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
Michael Neulingfb096922013-02-13 16:21:37 +0000862
863 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
864 tsk->pid);
865
866out_and_saveregs:
867 /* Always save the regs here, even if a transaction's not active.
868 * This context-switches a thread's TM info SPRs. We do it here to
869 * be consistent with the restore path (in recheckpoint) which
870 * cannot happen later in _switch().
871 */
872 tm_save_sprs(thr);
873}
874
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100875extern void __tm_recheckpoint(struct thread_struct *thread,
876 unsigned long orig_msr);
877
878void tm_recheckpoint(struct thread_struct *thread,
879 unsigned long orig_msr)
880{
881 unsigned long flags;
882
883 /* We really can't be interrupted here as the TEXASR registers can't
884 * change and later in the trecheckpoint code, we have a userspace R1.
885 * So let's hard disable over this region.
886 */
887 local_irq_save(flags);
888 hard_irq_disable();
889
890 /* The TM SPRs are restored here, so that TEXASR.FS can be set
891 * before the trecheckpoint and no explosion occurs.
892 */
893 tm_restore_sprs(thread);
894
895 __tm_recheckpoint(thread, orig_msr);
896
897 local_irq_restore(flags);
898}
899
Michael Neulingbc2a9402013-02-13 16:21:40 +0000900static inline void tm_recheckpoint_new_task(struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +0000901{
902 unsigned long msr;
903
904 if (!cpu_has_feature(CPU_FTR_TM))
905 return;
906
907 /* Recheckpoint the registers of the thread we're about to switch to.
908 *
909 * If the task was using FP, we non-lazily reload both the original and
910 * the speculative FP register states. This is because the kernel
911 * doesn't see if/when a TM rollback occurs, so if we take an FP
912 * unavoidable later, we are unable to determine which set of FP regs
913 * need to be restored.
914 */
915 if (!new->thread.regs)
916 return;
917
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100918 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
919 tm_restore_sprs(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +0000920 return;
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100921 }
Anshuman Khandual829023d2015-07-06 16:24:10 +0530922 msr = new->thread.ckpt_regs.msr;
Michael Neulingfb096922013-02-13 16:21:37 +0000923 /* Recheckpoint to restore original checkpointed register state. */
924 TM_DEBUG("*** tm_recheckpoint of pid %d "
925 "(new->msr 0x%lx, new->origmsr 0x%lx)\n",
926 new->pid, new->thread.regs->msr, msr);
927
928 /* This loads the checkpointed FP/VEC state, if used */
929 tm_recheckpoint(&new->thread, msr);
930
931 /* This loads the speculative FP/VEC state, if used */
932 if (msr & MSR_FP) {
933 do_load_up_transact_fpu(&new->thread);
934 new->thread.regs->msr |=
935 (MSR_FP | new->thread.fpexc_mode);
936 }
Michael Neulingf110c0c2013-04-09 16:18:55 +1000937#ifdef CONFIG_ALTIVEC
Michael Neulingfb096922013-02-13 16:21:37 +0000938 if (msr & MSR_VEC) {
939 do_load_up_transact_altivec(&new->thread);
940 new->thread.regs->msr |= MSR_VEC;
941 }
Michael Neulingf110c0c2013-04-09 16:18:55 +1000942#endif
Michael Neulingfb096922013-02-13 16:21:37 +0000943 /* We may as well turn on VSX too since all the state is restored now */
944 if (msr & MSR_VSX)
945 new->thread.regs->msr |= MSR_VSX;
946
947 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
948 "(kernel msr 0x%lx)\n",
949 new->pid, mfmsr());
950}
951
952static inline void __switch_to_tm(struct task_struct *prev)
953{
954 if (cpu_has_feature(CPU_FTR_TM)) {
955 tm_enable();
956 tm_reclaim_task(prev);
957 }
958}
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100959
960/*
961 * This is called if we are on the way out to userspace and the
962 * TIF_RESTORE_TM flag is set. It checks if we need to reload
963 * FP and/or vector state and does so if necessary.
964 * If userspace is inside a transaction (whether active or
965 * suspended) and FP/VMX/VSX instructions have ever been enabled
966 * inside that transaction, then we have to keep them enabled
967 * and keep the FP/VMX/VSX state loaded while ever the transaction
968 * continues. The reason is that if we didn't, and subsequently
969 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
970 * we don't know whether it's the same transaction, and thus we
971 * don't know which of the checkpointed state and the transactional
972 * state to use.
973 */
974void restore_tm_state(struct pt_regs *regs)
975{
976 unsigned long msr_diff;
977
978 clear_thread_flag(TIF_RESTORE_TM);
979 if (!MSR_TM_ACTIVE(regs->msr))
980 return;
981
Anshuman Khandual829023d2015-07-06 16:24:10 +0530982 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100983 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100984
985 restore_math(regs);
986
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100987 regs->msr |= msr_diff;
988}
989
Michael Neulingfb096922013-02-13 16:21:37 +0000990#else
991#define tm_recheckpoint_new_task(new)
992#define __switch_to_tm(prev)
993#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neuling9422de32012-12-20 14:06:44 +0000994
Anton Blanchard152d5232015-10-29 11:43:55 +1100995static inline void save_sprs(struct thread_struct *t)
996{
997#ifdef CONFIG_ALTIVEC
Oliver O'Halloran01d7c2a22016-03-08 09:08:47 +1100998 if (cpu_has_feature(CPU_FTR_ALTIVEC))
Anton Blanchard152d5232015-10-29 11:43:55 +1100999 t->vrsave = mfspr(SPRN_VRSAVE);
1000#endif
1001#ifdef CONFIG_PPC_BOOK3S_64
1002 if (cpu_has_feature(CPU_FTR_DSCR))
1003 t->dscr = mfspr(SPRN_DSCR);
1004
1005 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1006 t->bescr = mfspr(SPRN_BESCR);
1007 t->ebbhr = mfspr(SPRN_EBBHR);
1008 t->ebbrr = mfspr(SPRN_EBBRR);
1009
1010 t->fscr = mfspr(SPRN_FSCR);
1011
1012 /*
1013 * Note that the TAR is not available for use in the kernel.
1014 * (To provide this, the TAR should be backed up/restored on
1015 * exception entry/exit instead, and be in pt_regs. FIXME,
1016 * this should be in pt_regs anyway (for debug).)
1017 */
1018 t->tar = mfspr(SPRN_TAR);
1019 }
1020#endif
1021}
1022
1023static inline void restore_sprs(struct thread_struct *old_thread,
1024 struct thread_struct *new_thread)
1025{
1026#ifdef CONFIG_ALTIVEC
1027 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
1028 old_thread->vrsave != new_thread->vrsave)
1029 mtspr(SPRN_VRSAVE, new_thread->vrsave);
1030#endif
1031#ifdef CONFIG_PPC_BOOK3S_64
1032 if (cpu_has_feature(CPU_FTR_DSCR)) {
1033 u64 dscr = get_paca()->dscr_default;
1034 u64 fscr = old_thread->fscr & ~FSCR_DSCR;
1035
1036 if (new_thread->dscr_inherit) {
1037 dscr = new_thread->dscr;
1038 fscr |= FSCR_DSCR;
1039 }
1040
1041 if (old_thread->dscr != dscr)
1042 mtspr(SPRN_DSCR, dscr);
1043
1044 if (old_thread->fscr != fscr)
1045 mtspr(SPRN_FSCR, fscr);
1046 }
1047
1048 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1049 if (old_thread->bescr != new_thread->bescr)
1050 mtspr(SPRN_BESCR, new_thread->bescr);
1051 if (old_thread->ebbhr != new_thread->ebbhr)
1052 mtspr(SPRN_EBBHR, new_thread->ebbhr);
1053 if (old_thread->ebbrr != new_thread->ebbrr)
1054 mtspr(SPRN_EBBRR, new_thread->ebbrr);
1055
1056 if (old_thread->tar != new_thread->tar)
1057 mtspr(SPRN_TAR, new_thread->tar);
1058 }
1059#endif
1060}
1061
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001062struct task_struct *__switch_to(struct task_struct *prev,
1063 struct task_struct *new)
1064{
1065 struct thread_struct *new_thread, *old_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001066 struct task_struct *last;
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001067#ifdef CONFIG_PPC_BOOK3S_64
1068 struct ppc64_tlb_batch *batch;
1069#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001070
Anton Blanchard152d5232015-10-29 11:43:55 +11001071 new_thread = &new->thread;
1072 old_thread = &current->thread;
1073
Michael Neuling7ba5fef2013-10-02 17:15:14 +10001074 WARN_ON(!irqs_disabled());
1075
Paul Mackerras06d67d52005-10-10 22:29:05 +10001076#ifdef CONFIG_PPC64
1077 /*
1078 * Collect processor utilization data per process
1079 */
1080 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Christoph Lameter69111ba2014-10-21 15:23:25 -05001081 struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001082 long unsigned start_tb, current_tb;
1083 start_tb = old_thread->start_tb;
1084 cu->current_tb = current_tb = mfspr(SPRN_PURR);
1085 old_thread->accum_tb += (current_tb - start_tb);
1086 new_thread->start_tb = current_tb;
1087 }
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001088#endif /* CONFIG_PPC64 */
1089
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001090#ifdef CONFIG_PPC_STD_MMU_64
Christoph Lameter69111ba2014-10-21 15:23:25 -05001091 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001092 if (batch->active) {
1093 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1094 if (batch->index)
1095 __flush_tlb_pending(batch);
1096 batch->active = 0;
1097 }
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001098#endif /* CONFIG_PPC_STD_MMU_64 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001099
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001100#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1101 switch_booke_debug_regs(&new->thread.debug);
1102#else
1103/*
1104 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1105 * schedule DABR
1106 */
1107#ifndef CONFIG_HAVE_HW_BREAKPOINT
1108 if (unlikely(!hw_brk_match(this_cpu_ptr(&current_brk), &new->thread.hw_brk)))
1109 __set_breakpoint(&new->thread.hw_brk);
1110#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1111#endif
1112
1113 /*
1114 * We need to save SPRs before treclaim/trecheckpoint as these will
1115 * change a number of them.
1116 */
1117 save_sprs(&prev->thread);
1118
1119 __switch_to_tm(prev);
1120
1121 /* Save FPU, Altivec, VSX and SPE state */
1122 giveup_all(prev);
1123
Anton Blanchard44387e92008-03-17 15:27:09 +11001124 /*
1125 * We can't take a PMU exception inside _switch() since there is a
1126 * window where the kernel stack SLB and the kernel stack are out
1127 * of sync. Hard disable here.
1128 */
1129 hard_irq_disable();
Michael Neulingbc2a9402013-02-13 16:21:40 +00001130
1131 tm_recheckpoint_new_task(new);
1132
Anton Blanchard20dbe672015-12-10 20:44:39 +11001133 /*
1134 * Call restore_sprs() before calling _switch(). If we move it after
1135 * _switch() then we miss out on calling it for new tasks. The reason
1136 * for this is we manually create a stack frame for new tasks that
1137 * directly returns through ret_from_fork() or
1138 * ret_from_kernel_thread(). See copy_thread() for details.
1139 */
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001140 restore_sprs(old_thread, new_thread);
1141
Anton Blanchard20dbe672015-12-10 20:44:39 +11001142 last = _switch(old_thread, new_thread);
1143
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001144#ifdef CONFIG_PPC_STD_MMU_64
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001145 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1146 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001147 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001148 batch->active = 1;
1149 }
Cyril Bur70fe3d92016-02-29 17:53:47 +11001150
1151 if (current_thread_info()->task->thread.regs)
1152 restore_math(current_thread_info()->task->thread.regs);
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001153#endif /* CONFIG_PPC_STD_MMU_64 */
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001154
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001155 return last;
1156}
1157
Paul Mackerras06d67d52005-10-10 22:29:05 +10001158static int instructions_to_print = 16;
1159
Paul Mackerras06d67d52005-10-10 22:29:05 +10001160static void show_instructions(struct pt_regs *regs)
1161{
1162 int i;
1163 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
1164 sizeof(int));
1165
1166 printk("Instruction dump:");
1167
1168 for (i = 0; i < instructions_to_print; i++) {
1169 int instr;
1170
1171 if (!(i % 8))
1172 printk("\n");
1173
Scott Wood0de2d822007-09-28 04:38:55 +10001174#if !defined(CONFIG_BOOKE)
1175 /* If executing with the IMMU off, adjust pc rather
1176 * than print XXXXXXXX.
1177 */
1178 if (!(regs->msr & MSR_IR))
1179 pc = (unsigned long)phys_to_virt(pc);
1180#endif
1181
Anton Blanchard00ae36d2006-10-13 12:17:16 +10001182 if (!__kernel_text_address(pc) ||
Anton Blanchard7b051f62014-10-13 20:27:15 +11001183 probe_kernel_address((unsigned int __user *)pc, instr)) {
Ira Snyder40c8cef2012-01-06 12:34:07 +00001184 printk(KERN_CONT "XXXXXXXX ");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001185 } else {
1186 if (regs->nip == pc)
Ira Snyder40c8cef2012-01-06 12:34:07 +00001187 printk(KERN_CONT "<%08x> ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001188 else
Ira Snyder40c8cef2012-01-06 12:34:07 +00001189 printk(KERN_CONT "%08x ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001190 }
1191
1192 pc += sizeof(int);
1193 }
1194
1195 printk("\n");
1196}
1197
Michael Neuling801c0b22015-11-20 15:15:32 +11001198struct regbit {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001199 unsigned long bit;
1200 const char *name;
Michael Neuling801c0b22015-11-20 15:15:32 +11001201};
1202
1203static struct regbit msr_bits[] = {
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001204#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
1205 {MSR_SF, "SF"},
1206 {MSR_HV, "HV"},
1207#endif
1208 {MSR_VEC, "VEC"},
1209 {MSR_VSX, "VSX"},
1210#ifdef CONFIG_BOOKE
1211 {MSR_CE, "CE"},
1212#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001213 {MSR_EE, "EE"},
1214 {MSR_PR, "PR"},
1215 {MSR_FP, "FP"},
1216 {MSR_ME, "ME"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001217#ifdef CONFIG_BOOKE
Kumar Gala1b983262008-11-19 04:39:53 +00001218 {MSR_DE, "DE"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001219#else
1220 {MSR_SE, "SE"},
1221 {MSR_BE, "BE"},
1222#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001223 {MSR_IR, "IR"},
1224 {MSR_DR, "DR"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001225 {MSR_PMM, "PMM"},
1226#ifndef CONFIG_BOOKE
1227 {MSR_RI, "RI"},
1228 {MSR_LE, "LE"},
1229#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001230 {0, NULL}
1231};
1232
Michael Neuling801c0b22015-11-20 15:15:32 +11001233static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
Paul Mackerras06d67d52005-10-10 22:29:05 +10001234{
Michael Neuling801c0b22015-11-20 15:15:32 +11001235 const char *s = "";
Paul Mackerras06d67d52005-10-10 22:29:05 +10001236
Paul Mackerras06d67d52005-10-10 22:29:05 +10001237 for (; bits->bit; ++bits)
1238 if (val & bits->bit) {
Michael Neuling801c0b22015-11-20 15:15:32 +11001239 printk("%s%s", s, bits->name);
1240 s = sep;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001241 }
Michael Neuling801c0b22015-11-20 15:15:32 +11001242}
1243
1244#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1245static struct regbit msr_tm_bits[] = {
1246 {MSR_TS_T, "T"},
1247 {MSR_TS_S, "S"},
1248 {MSR_TM, "E"},
1249 {0, NULL}
1250};
1251
1252static void print_tm_bits(unsigned long val)
1253{
1254/*
1255 * This only prints something if at least one of the TM bit is set.
1256 * Inside the TM[], the output means:
1257 * E: Enabled (bit 32)
1258 * S: Suspended (bit 33)
1259 * T: Transactional (bit 34)
1260 */
1261 if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
1262 printk(",TM[");
1263 print_bits(val, msr_tm_bits, "");
1264 printk("]");
1265 }
1266}
1267#else
1268static void print_tm_bits(unsigned long val) {}
1269#endif
1270
1271static void print_msr_bits(unsigned long val)
1272{
1273 printk("<");
1274 print_bits(val, msr_bits, ",");
1275 print_tm_bits(val);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001276 printk(">");
1277}
1278
1279#ifdef CONFIG_PPC64
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001280#define REG "%016lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001281#define REGS_PER_LINE 4
1282#define LAST_VOLATILE 13
1283#else
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001284#define REG "%08lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001285#define REGS_PER_LINE 8
1286#define LAST_VOLATILE 12
1287#endif
1288
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001289void show_regs(struct pt_regs * regs)
1290{
1291 int i, trap;
1292
Tejun Heoa43cb952013-04-30 15:27:17 -07001293 show_regs_print_info(KERN_DEFAULT);
1294
Paul Mackerras06d67d52005-10-10 22:29:05 +10001295 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
1296 regs->nip, regs->link, regs->ctr);
1297 printk("REGS: %p TRAP: %04lx %s (%s)\n",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001298 regs, regs->trap, print_tainted(), init_utsname()->release);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001299 printk("MSR: "REG" ", regs->msr);
Michael Neuling801c0b22015-11-20 15:15:32 +11001300 print_msr_bits(regs->msr);
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001301 printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001302 trap = TRAP(regs);
Michael Neuling5115a022011-07-14 19:25:12 +00001303 if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001304 printk("CFAR: "REG" ", regs->orig_gpr3);
Anton Blanchardc5400642013-11-15 15:41:19 +11001305 if (trap == 0x200 || trap == 0x300 || trap == 0x600)
Kumar Galaba28c9a2011-10-06 02:53:38 +00001306#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001307 printk("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
Kumar Gala14170782007-07-26 00:46:15 -05001308#else
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001309 printk("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1310#endif
1311#ifdef CONFIG_PPC64
1312 printk("SOFTE: %ld ", regs->softe);
1313#endif
1314#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchard6d888d12013-11-18 13:19:17 +11001315 if (MSR_TM_ACTIVE(regs->msr))
1316 printk("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
Kumar Gala14170782007-07-26 00:46:15 -05001317#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001318
1319 for (i = 0; i < 32; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001320 if ((i % REGS_PER_LINE) == 0)
Kumar Galaa2367192009-06-18 22:29:55 +00001321 printk("\nGPR%02d: ", i);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001322 printk(REG " ", regs->gpr[i]);
1323 if (i == LAST_VOLATILE && !FULL_REGS(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001324 break;
1325 }
1326 printk("\n");
1327#ifdef CONFIG_KALLSYMS
1328 /*
1329 * Lookup NIP late so we have the best change of getting the
1330 * above info out without failing
1331 */
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001332 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1333 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001334#endif
1335 show_stack(current, (unsigned long *) regs->gpr[1]);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001336 if (!user_mode(regs))
1337 show_instructions(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001338}
1339
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001340void flush_thread(void)
1341{
K.Prasade0780b72011-02-10 04:44:35 +00001342#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad5aae8a52010-06-15 11:35:19 +05301343 flush_ptrace_hw_breakpoint(current);
K.Prasade0780b72011-02-10 04:44:35 +00001344#else /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001345 set_debug_reg_defaults(&current->thread);
K.Prasade0780b72011-02-10 04:44:35 +00001346#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001347}
1348
1349void
1350release_thread(struct task_struct *t)
1351{
1352}
1353
1354/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001355 * this gets called so that we can store coprocessor state into memory and
1356 * copy the current task into the new thread.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001357 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001358int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001359{
Anton Blanchard579e6332015-10-29 11:44:09 +11001360 flush_all_to_thread(src);
Michael Neuling621b5062014-03-03 14:21:40 +11001361 /*
1362 * Flush TM state out so we can copy it. __switch_to_tm() does this
1363 * flush but it removes the checkpointed state from the current CPU and
1364 * transitions the CPU out of TM mode. Hence we need to call
1365 * tm_recheckpoint_new_task() (on the same task) to restore the
1366 * checkpointed state back and the TM mode.
1367 */
1368 __switch_to_tm(src);
1369 tm_recheckpoint_new_task(src);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001370
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001371 *dst = *src;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001372
1373 clear_task_ebb(dst);
1374
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001375 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001376}
1377
Michael Ellermancec15482014-07-10 12:29:21 +10001378static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1379{
1380#ifdef CONFIG_PPC_STD_MMU_64
1381 unsigned long sp_vsid;
1382 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1383
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001384 if (radix_enabled())
1385 return;
1386
Michael Ellermancec15482014-07-10 12:29:21 +10001387 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1388 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1389 << SLB_VSID_SHIFT_1T;
1390 else
1391 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1392 << SLB_VSID_SHIFT;
1393 sp_vsid |= SLB_VSID_KERNEL | llp;
1394 p->thread.ksp_vsid = sp_vsid;
1395#endif
1396}
1397
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001398/*
1399 * Copy a thread..
1400 */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001401
Alex Dowad6eca8932015-03-13 20:14:46 +02001402/*
1403 * Copy architecture-specific thread state
1404 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -07001405int copy_thread(unsigned long clone_flags, unsigned long usp,
Alex Dowad6eca8932015-03-13 20:14:46 +02001406 unsigned long kthread_arg, struct task_struct *p)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001407{
1408 struct pt_regs *childregs, *kregs;
1409 extern void ret_from_fork(void);
Al Viro58254e12012-09-12 18:32:42 -04001410 extern void ret_from_kernel_thread(void);
1411 void (*f)(void);
Al Viro0cec6fd2006-01-12 01:06:02 -08001412 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Michael Ellerman5d31a962016-03-24 22:04:04 +11001413 struct thread_info *ti = task_thread_info(p);
1414
1415 klp_init_thread_info(ti);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001416
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001417 /* Copy registers */
1418 sp -= sizeof(struct pt_regs);
1419 childregs = (struct pt_regs *) sp;
Al Viroab758192012-10-21 22:33:39 -04001420 if (unlikely(p->flags & PF_KTHREAD)) {
Alex Dowad6eca8932015-03-13 20:14:46 +02001421 /* kernel thread */
Al Viro58254e12012-09-12 18:32:42 -04001422 memset(childregs, 0, sizeof(struct pt_regs));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001423 childregs->gpr[1] = sp + sizeof(struct pt_regs);
Anton Blanchard7cedd602014-02-04 16:08:51 +11001424 /* function */
1425 if (usp)
1426 childregs->gpr[14] = ppc_function_entry((void *)usp);
Al Viro58254e12012-09-12 18:32:42 -04001427#ifdef CONFIG_PPC64
Al Virob5e2fc12006-01-12 01:06:01 -08001428 clear_tsk_thread_flag(p, TIF_32BIT);
Al Viro138d1ce2012-10-11 08:41:43 -04001429 childregs->softe = 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001430#endif
Alex Dowad6eca8932015-03-13 20:14:46 +02001431 childregs->gpr[15] = kthread_arg;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001432 p->thread.regs = NULL; /* no user register state */
Al Viro138d1ce2012-10-11 08:41:43 -04001433 ti->flags |= _TIF_RESTOREALL;
Al Viro58254e12012-09-12 18:32:42 -04001434 f = ret_from_kernel_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001435 } else {
Alex Dowad6eca8932015-03-13 20:14:46 +02001436 /* user thread */
Al Viroafa86fc2012-10-22 22:51:14 -04001437 struct pt_regs *regs = current_pt_regs();
Al Viro58254e12012-09-12 18:32:42 -04001438 CHECK_FULL_REGS(regs);
1439 *childregs = *regs;
Al Viroea516b12012-10-21 22:28:43 -04001440 if (usp)
1441 childregs->gpr[1] = usp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001442 p->thread.regs = childregs;
Al Viro58254e12012-09-12 18:32:42 -04001443 childregs->gpr[3] = 0; /* Result from fork() */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001444 if (clone_flags & CLONE_SETTLS) {
1445#ifdef CONFIG_PPC64
Denis Kirjanov9904b002010-07-29 22:04:39 +00001446 if (!is_32bit_task())
Paul Mackerras06d67d52005-10-10 22:29:05 +10001447 childregs->gpr[13] = childregs->gpr[6];
1448 else
1449#endif
1450 childregs->gpr[2] = childregs->gpr[6];
1451 }
Al Viro58254e12012-09-12 18:32:42 -04001452
1453 f = ret_from_fork;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001454 }
Cyril Burd272f662016-02-29 17:53:46 +11001455 childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001456 sp -= STACK_FRAME_OVERHEAD;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001457
1458 /*
1459 * The way this works is that at some point in the future
1460 * some task will call _switch to switch to the new task.
1461 * That will pop off the stack frame created below and start
1462 * the new task running at ret_from_fork. The new task will
1463 * do some house keeping and then return from the fork or clone
1464 * system call, using the stack frame created above.
1465 */
Li Zhongaf945cf2013-05-06 22:44:41 +00001466 ((unsigned long *)sp)[0] = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001467 sp -= sizeof(struct pt_regs);
1468 kregs = (struct pt_regs *) sp;
1469 sp -= STACK_FRAME_OVERHEAD;
1470 p->thread.ksp = sp;
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001471#ifdef CONFIG_PPC32
Kumar Gala85218822008-04-28 16:21:22 +10001472 p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
1473 _ALIGN_UP(sizeof(struct thread_info), 16);
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001474#endif
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001475#ifdef CONFIG_HAVE_HW_BREAKPOINT
1476 p->thread.ptrace_bps[0] = NULL;
1477#endif
1478
Paul Mackerras18461962013-09-10 20:21:10 +10001479 p->thread.fp_save_area = NULL;
1480#ifdef CONFIG_ALTIVEC
1481 p->thread.vr_save_area = NULL;
1482#endif
1483
Michael Ellermancec15482014-07-10 12:29:21 +10001484 setup_ksp_vsid(p, sp);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001485
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001486#ifdef CONFIG_PPC64
1487 if (cpu_has_feature(CPU_FTR_DSCR)) {
Anton Blanchard1021cb22012-09-03 16:49:47 +00001488 p->thread.dscr_inherit = current->thread.dscr_inherit;
Anton Blancharddb1231dc2015-12-09 20:11:47 +11001489 p->thread.dscr = mfspr(SPRN_DSCR);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001490 }
Haren Myneni92779242012-12-06 21:49:56 +00001491 if (cpu_has_feature(CPU_FTR_HAS_PPR))
1492 p->thread.ppr = INIT_PPR;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001493#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +11001494 kregs->nip = ppc_function_entry(f);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001495 return 0;
1496}
1497
1498/*
1499 * Set up a thread for executing a new program
1500 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001501void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001502{
Michael Ellerman90eac722005-10-21 16:01:33 +10001503#ifdef CONFIG_PPC64
1504 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
1505#endif
1506
Paul Mackerras06d67d52005-10-10 22:29:05 +10001507 /*
1508 * If we exec out of a kernel thread then thread.regs will not be
1509 * set. Do it now.
1510 */
1511 if (!current->thread.regs) {
Al Viro0cec6fd2006-01-12 01:06:02 -08001512 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1513 current->thread.regs = regs - 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001514 }
1515
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001516 memset(regs->gpr, 0, sizeof(regs->gpr));
1517 regs->ctr = 0;
1518 regs->link = 0;
1519 regs->xer = 0;
1520 regs->ccr = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001521 regs->gpr[1] = sp;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001522
Roland McGrath474f8192007-09-24 16:52:44 -07001523 /*
1524 * We have just cleared all the nonvolatile GPRs, so make
1525 * FULL_REGS(regs) return true. This is necessary to allow
1526 * ptrace to examine the thread immediately after exec.
1527 */
1528 regs->trap &= ~1UL;
1529
Paul Mackerras06d67d52005-10-10 22:29:05 +10001530#ifdef CONFIG_PPC32
1531 regs->mq = 0;
1532 regs->nip = start;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001533 regs->msr = MSR_USER;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001534#else
Denis Kirjanov9904b002010-07-29 22:04:39 +00001535 if (!is_32bit_task()) {
Rusty Russell94af3ab2013-11-20 22:15:02 +11001536 unsigned long entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001537
Rusty Russell94af3ab2013-11-20 22:15:02 +11001538 if (is_elf2_task()) {
1539 /* Look ma, no function descriptors! */
1540 entry = start;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001541
Rusty Russell94af3ab2013-11-20 22:15:02 +11001542 /*
1543 * Ulrich says:
1544 * The latest iteration of the ABI requires that when
1545 * calling a function (at its global entry point),
1546 * the caller must ensure r12 holds the entry point
1547 * address (so that the function can quickly
1548 * establish addressability).
1549 */
1550 regs->gpr[12] = start;
1551 /* Make sure that's restored on entry to userspace. */
1552 set_thread_flag(TIF_RESTOREALL);
1553 } else {
1554 unsigned long toc;
1555
1556 /* start is a relocated pointer to the function
1557 * descriptor for the elf _start routine. The first
1558 * entry in the function descriptor is the entry
1559 * address of _start and the second entry is the TOC
1560 * value we need to use.
1561 */
1562 __get_user(entry, (unsigned long __user *)start);
1563 __get_user(toc, (unsigned long __user *)start+1);
1564
1565 /* Check whether the e_entry function descriptor entries
1566 * need to be relocated before we can use them.
1567 */
1568 if (load_addr != 0) {
1569 entry += load_addr;
1570 toc += load_addr;
1571 }
1572 regs->gpr[2] = toc;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001573 }
1574 regs->nip = entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001575 regs->msr = MSR_USER64;
Stephen Rothwelld4bf9a72005-10-13 13:40:54 +10001576 } else {
1577 regs->nip = start;
1578 regs->gpr[2] = 0;
1579 regs->msr = MSR_USER32;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001580 }
1581#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001582#ifdef CONFIG_VSX
1583 current->thread.used_vsr = 0;
1584#endif
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001585 memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
Paul Mackerras18461962013-09-10 20:21:10 +10001586 current->thread.fp_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001587#ifdef CONFIG_ALTIVEC
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001588 memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1589 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
Paul Mackerras18461962013-09-10 20:21:10 +10001590 current->thread.vr_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001591 current->thread.vrsave = 0;
1592 current->thread.used_vr = 0;
1593#endif /* CONFIG_ALTIVEC */
1594#ifdef CONFIG_SPE
1595 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1596 current->thread.acc = 0;
1597 current->thread.spefscr = 0;
1598 current->thread.used_spe = 0;
1599#endif /* CONFIG_SPE */
Michael Neulingbc2a9402013-02-13 16:21:40 +00001600#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1601 if (cpu_has_feature(CPU_FTR_TM))
1602 regs->msr |= MSR_TM;
1603 current->thread.tm_tfhar = 0;
1604 current->thread.tm_texasr = 0;
1605 current->thread.tm_tfiar = 0;
1606#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001607}
Anton Blancharde1802b02014-08-20 08:00:02 +10001608EXPORT_SYMBOL(start_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001609
1610#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1611 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1612
1613int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1614{
1615 struct pt_regs *regs = tsk->thread.regs;
1616
1617 /* This is a bit hairy. If we are an SPE enabled processor
1618 * (have embedded fp) we store the IEEE exception enable flags in
1619 * fpexc_mode. fpexc_mode is also used for setting FP exception
1620 * mode (asyn, precise, disabled) for 'Classic' FP. */
1621 if (val & PR_FP_EXC_SW_ENABLE) {
1622#ifdef CONFIG_SPE
Kumar Gala5e14d212007-09-13 01:44:20 -05001623 if (cpu_has_feature(CPU_FTR_SPE)) {
Joseph Myers640e9222013-12-10 23:07:45 +00001624 /*
1625 * When the sticky exception bits are set
1626 * directly by userspace, it must call prctl
1627 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1628 * in the existing prctl settings) or
1629 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1630 * the bits being set). <fenv.h> functions
1631 * saving and restoring the whole
1632 * floating-point environment need to do so
1633 * anyway to restore the prctl settings from
1634 * the saved environment.
1635 */
1636 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001637 tsk->thread.fpexc_mode = val &
1638 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1639 return 0;
1640 } else {
1641 return -EINVAL;
1642 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001643#else
1644 return -EINVAL;
1645#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001646 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001647
1648 /* on a CONFIG_SPE this does not hurt us. The bits that
1649 * __pack_fe01 use do not overlap with bits used for
1650 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1651 * on CONFIG_SPE implementations are reserved so writing to
1652 * them does not change anything */
1653 if (val > PR_FP_EXC_PRECISE)
1654 return -EINVAL;
1655 tsk->thread.fpexc_mode = __pack_fe01(val);
1656 if (regs != NULL && (regs->msr & MSR_FP) != 0)
1657 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1658 | tsk->thread.fpexc_mode;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001659 return 0;
1660}
1661
1662int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1663{
1664 unsigned int val;
1665
1666 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
1667#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00001668 if (cpu_has_feature(CPU_FTR_SPE)) {
1669 /*
1670 * When the sticky exception bits are set
1671 * directly by userspace, it must call prctl
1672 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1673 * in the existing prctl settings) or
1674 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1675 * the bits being set). <fenv.h> functions
1676 * saving and restoring the whole
1677 * floating-point environment need to do so
1678 * anyway to restore the prctl settings from
1679 * the saved environment.
1680 */
1681 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001682 val = tsk->thread.fpexc_mode;
Joseph Myers640e9222013-12-10 23:07:45 +00001683 } else
Kumar Gala5e14d212007-09-13 01:44:20 -05001684 return -EINVAL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001685#else
1686 return -EINVAL;
1687#endif
1688 else
1689 val = __unpack_fe01(tsk->thread.fpexc_mode);
1690 return put_user(val, (unsigned int __user *) adr);
1691}
1692
Paul Mackerrasfab5db92006-06-07 16:14:40 +10001693int set_endian(struct task_struct *tsk, unsigned int val)
1694{
1695 struct pt_regs *regs = tsk->thread.regs;
1696
1697 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1698 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1699 return -EINVAL;
1700
1701 if (regs == NULL)
1702 return -EINVAL;
1703
1704 if (val == PR_ENDIAN_BIG)
1705 regs->msr &= ~MSR_LE;
1706 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1707 regs->msr |= MSR_LE;
1708 else
1709 return -EINVAL;
1710
1711 return 0;
1712}
1713
1714int get_endian(struct task_struct *tsk, unsigned long adr)
1715{
1716 struct pt_regs *regs = tsk->thread.regs;
1717 unsigned int val;
1718
1719 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1720 !cpu_has_feature(CPU_FTR_REAL_LE))
1721 return -EINVAL;
1722
1723 if (regs == NULL)
1724 return -EINVAL;
1725
1726 if (regs->msr & MSR_LE) {
1727 if (cpu_has_feature(CPU_FTR_REAL_LE))
1728 val = PR_ENDIAN_LITTLE;
1729 else
1730 val = PR_ENDIAN_PPC_LITTLE;
1731 } else
1732 val = PR_ENDIAN_BIG;
1733
1734 return put_user(val, (unsigned int __user *)adr);
1735}
1736
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001737int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
1738{
1739 tsk->thread.align_ctl = val;
1740 return 0;
1741}
1742
1743int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
1744{
1745 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
1746}
1747
Paul Mackerrasbb72c482007-02-19 11:42:42 +11001748static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
1749 unsigned long nbytes)
1750{
1751 unsigned long stack_page;
1752 unsigned long cpu = task_cpu(p);
1753
1754 /*
1755 * Avoid crashing if the stack has overflowed and corrupted
1756 * task_cpu(p), which is in the thread_info struct.
1757 */
1758 if (cpu < NR_CPUS && cpu_possible(cpu)) {
1759 stack_page = (unsigned long) hardirq_ctx[cpu];
1760 if (sp >= stack_page + sizeof(struct thread_struct)
1761 && sp <= stack_page + THREAD_SIZE - nbytes)
1762 return 1;
1763
1764 stack_page = (unsigned long) softirq_ctx[cpu];
1765 if (sp >= stack_page + sizeof(struct thread_struct)
1766 && sp <= stack_page + THREAD_SIZE - nbytes)
1767 return 1;
1768 }
1769 return 0;
1770}
1771
Anton Blanchard2f251942006-03-27 11:46:18 +11001772int validate_sp(unsigned long sp, struct task_struct *p,
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001773 unsigned long nbytes)
1774{
Al Viro0cec6fd2006-01-12 01:06:02 -08001775 unsigned long stack_page = (unsigned long)task_stack_page(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001776
1777 if (sp >= stack_page + sizeof(struct thread_struct)
1778 && sp <= stack_page + THREAD_SIZE - nbytes)
1779 return 1;
1780
Paul Mackerrasbb72c482007-02-19 11:42:42 +11001781 return valid_irq_stack(sp, p, nbytes);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001782}
1783
Anton Blanchard2f251942006-03-27 11:46:18 +11001784EXPORT_SYMBOL(validate_sp);
1785
Paul Mackerras06d67d52005-10-10 22:29:05 +10001786unsigned long get_wchan(struct task_struct *p)
1787{
1788 unsigned long ip, sp;
1789 int count = 0;
1790
1791 if (!p || p == current || p->state == TASK_RUNNING)
1792 return 0;
1793
1794 sp = p->thread.ksp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001795 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001796 return 0;
1797
1798 do {
1799 sp = *(unsigned long *)sp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001800 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001801 return 0;
1802 if (count > 0) {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001803 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10001804 if (!in_sched_functions(ip))
1805 return ip;
1806 }
1807 } while (count++ < 16);
1808 return 0;
1809}
Paul Mackerras06d67d52005-10-10 22:29:05 +10001810
Johannes Bergc4d04be2008-11-20 03:24:07 +00001811static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001812
1813void show_stack(struct task_struct *tsk, unsigned long *stack)
1814{
Paul Mackerras06d67d52005-10-10 22:29:05 +10001815 unsigned long sp, ip, lr, newsp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001816 int count = 0;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001817 int firstframe = 1;
Steven Rostedt6794c782009-02-09 21:10:27 -08001818#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1819 int curr_frame = current->curr_ret_stack;
1820 extern void return_to_handler(void);
Steven Rostedt9135c3c2009-09-15 08:20:15 -07001821 unsigned long rth = (unsigned long)return_to_handler;
Steven Rostedt6794c782009-02-09 21:10:27 -08001822#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001823
1824 sp = (unsigned long) stack;
1825 if (tsk == NULL)
1826 tsk = current;
1827 if (sp == 0) {
1828 if (tsk == current)
Anton Blanchardacf620e2014-10-13 19:41:39 +11001829 sp = current_stack_pointer();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001830 else
1831 sp = tsk->thread.ksp;
1832 }
1833
Paul Mackerras06d67d52005-10-10 22:29:05 +10001834 lr = 0;
1835 printk("Call Trace:\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001836 do {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001837 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001838 return;
1839
1840 stack = (unsigned long *) sp;
1841 newsp = stack[0];
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001842 ip = stack[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10001843 if (!firstframe || ip != lr) {
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001844 printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
Steven Rostedt6794c782009-02-09 21:10:27 -08001845#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Anton Blanchard7d56c652014-09-17 17:07:03 +10001846 if ((ip == rth) && curr_frame >= 0) {
Steven Rostedt6794c782009-02-09 21:10:27 -08001847 printk(" (%pS)",
1848 (void *)current->ret_stack[curr_frame].ret);
1849 curr_frame--;
1850 }
1851#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001852 if (firstframe)
1853 printk(" (unreliable)");
1854 printk("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001855 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001856 firstframe = 0;
1857
1858 /*
1859 * See if this is an exception frame.
1860 * We look for the "regshere" marker in the current frame.
1861 */
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001862 if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
1863 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001864 struct pt_regs *regs = (struct pt_regs *)
1865 (sp + STACK_FRAME_OVERHEAD);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001866 lr = regs->link;
Paul Mackerras9be9be22014-06-12 16:53:08 +10001867 printk("--- interrupt: %lx at %pS\n LR = %pS\n",
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001868 regs->trap, (void *)regs->nip, (void *)lr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001869 firstframe = 1;
1870 }
1871
1872 sp = newsp;
1873 } while (count++ < kstack_depth_to_print);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001874}
Paul Mackerras06d67d52005-10-10 22:29:05 +10001875
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001876#ifdef CONFIG_PPC64
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001877/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10001878void notrace __ppc64_runlatch_on(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001879{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001880 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001881 unsigned long ctrl;
1882
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001883 ctrl = mfspr(SPRN_CTRLF);
1884 ctrl |= CTRL_RUNLATCH;
1885 mtspr(SPRN_CTRLT, ctrl);
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001886
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10001887 ti->local_flags |= _TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001888}
1889
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001890/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10001891void notrace __ppc64_runlatch_off(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001892{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001893 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001894 unsigned long ctrl;
1895
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10001896 ti->local_flags &= ~_TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001897
Anton Blanchard4138d652010-08-06 03:28:19 +00001898 ctrl = mfspr(SPRN_CTRLF);
1899 ctrl &= ~CTRL_RUNLATCH;
1900 mtspr(SPRN_CTRLT, ctrl);
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001901}
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001902#endif /* CONFIG_PPC64 */
Benjamin Herrenschmidtf6a61682008-04-18 16:56:17 +10001903
Anton Blanchardd8390882009-02-22 01:50:03 +00001904unsigned long arch_align_stack(unsigned long sp)
1905{
1906 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
1907 sp -= get_random_int() & ~PAGE_MASK;
1908 return sp & ~0xf;
1909}
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001910
1911static inline unsigned long brk_rnd(void)
1912{
1913 unsigned long rnd = 0;
1914
1915 /* 8MB for 32bit, 1GB for 64bit */
1916 if (is_32bit_task())
Daniel Cashman5ef11c32016-02-26 15:19:37 -08001917 rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT)));
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001918 else
Daniel Cashman5ef11c32016-02-26 15:19:37 -08001919 rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT)));
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001920
1921 return rnd << PAGE_SHIFT;
1922}
1923
1924unsigned long arch_randomize_brk(struct mm_struct *mm)
1925{
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001926 unsigned long base = mm->brk;
1927 unsigned long ret;
1928
Kumar Galace7a35c2009-10-16 07:05:17 +00001929#ifdef CONFIG_PPC_STD_MMU_64
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001930 /*
1931 * If we are using 1TB segments and we are allowed to randomise
1932 * the heap, we can put it above 1TB so it is backed by a 1TB
1933 * segment. Otherwise the heap will be in the bottom 1TB
1934 * which always uses 256MB segments and this may result in a
Aneesh Kumar K.Vcaca2852016-04-29 23:26:07 +10001935 * performance penalty. We don't need to worry about radix. For
1936 * radix, mmu_highuser_ssize remains unchanged from 256MB.
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001937 */
1938 if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
1939 base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
1940#endif
1941
1942 ret = PAGE_ALIGN(base + brk_rnd());
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001943
1944 if (ret < mm->brk)
1945 return mm->brk;
1946
1947 return ret;
1948}
Anton Blanchard501cb162009-02-22 01:50:07 +00001949