blob: a7e5061187e890100e1d461db707c858d5ae4dea [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>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100041
42#include <asm/pgtable.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100043#include <asm/io.h>
44#include <asm/processor.h>
45#include <asm/mmu.h>
46#include <asm/prom.h>
Michael Ellerman76032de2005-11-07 13:12:03 +110047#include <asm/machdep.h>
Paul Mackerrasc6622f62006-02-24 10:06:59 +110048#include <asm/time.h>
David Howellsae3a1972012-03-28 18:30:02 +010049#include <asm/runlatch.h>
Arnd Bergmanna7f31842006-03-23 00:00:08 +010050#include <asm/syscalls.h>
David Howellsae3a1972012-03-28 18:30:02 +010051#include <asm/switch_to.h>
Michael Neulingfb096922013-02-13 16:21:37 +000052#include <asm/tm.h>
David Howellsae3a1972012-03-28 18:30:02 +010053#include <asm/debug.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100054#ifdef CONFIG_PPC64
55#include <asm/firmware.h>
Paul Mackerras06d67d52005-10-10 22:29:05 +100056#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +110057#include <asm/code-patching.h>
Luis Machadod6a61bf2008-07-24 02:10:41 +100058#include <linux/kprobes.h>
59#include <linux/kdebug.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100060
Michael Neuling8b3c34c2013-02-13 16:21:32 +000061/* Transactional Memory debug */
62#ifdef TM_DEBUG_SW
63#define TM_DEBUG(x...) printk(KERN_INFO x)
64#else
65#define TM_DEBUG(x...) do { } while(0)
66#endif
67
Paul Mackerras14cf11a2005-09-26 16:04:21 +100068extern unsigned long _get_SP(void);
69
Paul Mackerrasd31626f2014-01-13 15:56:29 +110070#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchardb86fd2b2015-10-29 11:43:58 +110071static void check_if_tm_restore_required(struct task_struct *tsk)
Paul Mackerrasd31626f2014-01-13 15:56:29 +110072{
73 /*
74 * If we are saving the current thread's registers, and the
75 * thread is in a transactional state, set the TIF_RESTORE_TM
76 * bit so that we know to restore the registers before
77 * returning to userspace.
78 */
79 if (tsk == current && tsk->thread.regs &&
80 MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
81 !test_thread_flag(TIF_RESTORE_TM)) {
Anshuman Khandual829023d2015-07-06 16:24:10 +053082 tsk->thread.ckpt_regs.msr = tsk->thread.regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +110083 set_thread_flag(TIF_RESTORE_TM);
84 }
Paul Mackerrasd31626f2014-01-13 15:56:29 +110085}
Paul Mackerrasd31626f2014-01-13 15:56:29 +110086#else
Anton Blanchardb86fd2b2015-10-29 11:43:58 +110087static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
Paul Mackerrasd31626f2014-01-13 15:56:29 +110088#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
89
Anton Blanchard3eb5d582015-10-29 11:44:06 +110090bool strict_msr_control;
91EXPORT_SYMBOL(strict_msr_control);
92
93static int __init enable_strict_msr_control(char *str)
94{
95 strict_msr_control = true;
96 pr_info("Enabling strict facility control\n");
97
98 return 0;
99}
100early_param("ppc_strict_facility_enable", enable_strict_msr_control);
101
102void msr_check_and_set(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100103{
104 unsigned long oldmsr = mfmsr();
105 unsigned long newmsr;
106
107 newmsr = oldmsr | bits;
108
109#ifdef CONFIG_VSX
110 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
111 newmsr |= MSR_VSX;
112#endif
113
114 if (oldmsr != newmsr)
115 mtmsr_isync(newmsr);
116}
117
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100118void __msr_check_and_clear(unsigned long bits)
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100119{
120 unsigned long oldmsr = mfmsr();
121 unsigned long newmsr;
122
123 newmsr = oldmsr & ~bits;
124
125#ifdef CONFIG_VSX
126 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
127 newmsr &= ~MSR_VSX;
128#endif
129
130 if (oldmsr != newmsr)
131 mtmsr_isync(newmsr);
132}
Anton Blanchard3eb5d582015-10-29 11:44:06 +1100133EXPORT_SYMBOL(__msr_check_and_clear);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100134
Kevin Hao037f0ee2013-07-14 17:02:05 +0800135#ifdef CONFIG_PPC_FPU
Cyril Bur87924682016-02-29 17:53:49 +1100136void __giveup_fpu(struct task_struct *tsk)
137{
138 save_fpu(tsk);
139 tsk->thread.regs->msr &= ~MSR_FP;
140#ifdef CONFIG_VSX
141 if (cpu_has_feature(CPU_FTR_VSX))
142 tsk->thread.regs->msr &= ~MSR_VSX;
143#endif
144}
145
Anton Blanchard98da5812015-10-29 11:44:01 +1100146void giveup_fpu(struct task_struct *tsk)
147{
Anton Blanchard98da5812015-10-29 11:44:01 +1100148 check_if_tm_restore_required(tsk);
149
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100150 msr_check_and_set(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100151 __giveup_fpu(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100152 msr_check_and_clear(MSR_FP);
Anton Blanchard98da5812015-10-29 11:44:01 +1100153}
154EXPORT_SYMBOL(giveup_fpu);
155
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000156/*
157 * Make sure the floating-point register state in the
158 * the thread_struct is up to date for task tsk.
159 */
160void flush_fp_to_thread(struct task_struct *tsk)
161{
162 if (tsk->thread.regs) {
163 /*
164 * We need to disable preemption here because if we didn't,
165 * another process could get scheduled after the regs->msr
166 * test but before we have finished saving the FP registers
167 * to the thread_struct. That process could take over the
168 * FPU, and then when we get scheduled again we would store
169 * bogus values for the remaining FP registers.
170 */
171 preempt_disable();
172 if (tsk->thread.regs->msr & MSR_FP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000173 /*
174 * This should only ever be called for current or
175 * for a stopped child process. Since we save away
Anton Blanchardaf1bbc32015-10-29 11:43:57 +1100176 * the FP register state on context switch,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000177 * there is something wrong if a stopped child appears
178 * to still have its FP state in the CPU registers.
179 */
180 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100181 giveup_fpu(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000182 }
183 preempt_enable();
184 }
185}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000186EXPORT_SYMBOL_GPL(flush_fp_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000187
188void enable_kernel_fp(void)
189{
190 WARN_ON(preemptible());
191
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100192 msr_check_and_set(MSR_FP);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100193
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100194 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
195 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100196 __giveup_fpu(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100197 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000198}
199EXPORT_SYMBOL(enable_kernel_fp);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100200
201static int restore_fp(struct task_struct *tsk) {
202 if (tsk->thread.load_fp) {
203 load_fp_state(&current->thread.fp_state);
204 current->thread.load_fp++;
205 return 1;
206 }
207 return 0;
208}
209#else
210static int restore_fp(struct task_struct *tsk) { return 0; }
Anton Blanchardd1e1cf22015-10-29 11:44:11 +1100211#endif /* CONFIG_PPC_FPU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000212
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000213#ifdef CONFIG_ALTIVEC
Cyril Bur70fe3d92016-02-29 17:53:47 +1100214#define loadvec(thr) ((thr).load_vec)
215
Anton Blanchard98da5812015-10-29 11:44:01 +1100216void giveup_altivec(struct task_struct *tsk)
217{
Anton Blanchard98da5812015-10-29 11:44:01 +1100218 check_if_tm_restore_required(tsk);
219
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100220 msr_check_and_set(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100221 __giveup_altivec(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100222 msr_check_and_clear(MSR_VEC);
Anton Blanchard98da5812015-10-29 11:44:01 +1100223}
224EXPORT_SYMBOL(giveup_altivec);
225
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000226void enable_kernel_altivec(void)
227{
228 WARN_ON(preemptible());
229
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100230 msr_check_and_set(MSR_VEC);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100231
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100232 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
233 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100234 __giveup_altivec(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100235 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000236}
237EXPORT_SYMBOL(enable_kernel_altivec);
238
239/*
240 * Make sure the VMX/Altivec register state in the
241 * the thread_struct is up to date for task tsk.
242 */
243void flush_altivec_to_thread(struct task_struct *tsk)
244{
245 if (tsk->thread.regs) {
246 preempt_disable();
247 if (tsk->thread.regs->msr & MSR_VEC) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000248 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100249 giveup_altivec(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000250 }
251 preempt_enable();
252 }
253}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000254EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100255
256static int restore_altivec(struct task_struct *tsk)
257{
258 if (cpu_has_feature(CPU_FTR_ALTIVEC) && tsk->thread.load_vec) {
259 load_vr_state(&tsk->thread.vr_state);
260 tsk->thread.used_vr = 1;
261 tsk->thread.load_vec++;
262
263 return 1;
264 }
265 return 0;
266}
267#else
268#define loadvec(thr) 0
269static inline int restore_altivec(struct task_struct *tsk) { return 0; }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000270#endif /* CONFIG_ALTIVEC */
271
Michael Neulingce48b212008-06-25 14:07:18 +1000272#ifdef CONFIG_VSX
Anton Blancharda7d623d2015-10-29 11:44:02 +1100273void giveup_vsx(struct task_struct *tsk)
274{
Anton Blancharda7d623d2015-10-29 11:44:02 +1100275 check_if_tm_restore_required(tsk);
276
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100277 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100278 if (tsk->thread.regs->msr & MSR_FP)
279 __giveup_fpu(tsk);
280 if (tsk->thread.regs->msr & MSR_VEC)
281 __giveup_altivec(tsk);
282 __giveup_vsx(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100283 msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blancharda7d623d2015-10-29 11:44:02 +1100284}
285EXPORT_SYMBOL(giveup_vsx);
286
Michael Neulingce48b212008-06-25 14:07:18 +1000287void enable_kernel_vsx(void)
288{
289 WARN_ON(preemptible());
290
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100291 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100292
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100293 if (current->thread.regs && (current->thread.regs->msr & MSR_VSX)) {
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100294 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100295 if (current->thread.regs->msr & MSR_FP)
296 __giveup_fpu(current);
297 if (current->thread.regs->msr & MSR_VEC)
298 __giveup_altivec(current);
299 __giveup_vsx(current);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100300 }
Michael Neulingce48b212008-06-25 14:07:18 +1000301}
302EXPORT_SYMBOL(enable_kernel_vsx);
Michael Neulingce48b212008-06-25 14:07:18 +1000303
304void flush_vsx_to_thread(struct task_struct *tsk)
305{
306 if (tsk->thread.regs) {
307 preempt_disable();
308 if (tsk->thread.regs->msr & MSR_VSX) {
Michael Neulingce48b212008-06-25 14:07:18 +1000309 BUG_ON(tsk != current);
Michael Neulingce48b212008-06-25 14:07:18 +1000310 giveup_vsx(tsk);
311 }
312 preempt_enable();
313 }
314}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000315EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
Cyril Bur70fe3d92016-02-29 17:53:47 +1100316
317static int restore_vsx(struct task_struct *tsk)
318{
319 if (cpu_has_feature(CPU_FTR_VSX)) {
320 tsk->thread.used_vsr = 1;
321 return 1;
322 }
323
324 return 0;
325}
326#else
327static inline int restore_vsx(struct task_struct *tsk) { return 0; }
Michael Neulingce48b212008-06-25 14:07:18 +1000328#endif /* CONFIG_VSX */
329
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000330#ifdef CONFIG_SPE
Anton Blanchard98da5812015-10-29 11:44:01 +1100331void giveup_spe(struct task_struct *tsk)
332{
Anton Blanchard98da5812015-10-29 11:44:01 +1100333 check_if_tm_restore_required(tsk);
334
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100335 msr_check_and_set(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100336 __giveup_spe(tsk);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100337 msr_check_and_clear(MSR_SPE);
Anton Blanchard98da5812015-10-29 11:44:01 +1100338}
339EXPORT_SYMBOL(giveup_spe);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000340
341void enable_kernel_spe(void)
342{
343 WARN_ON(preemptible());
344
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100345 msr_check_and_set(MSR_SPE);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100346
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100347 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
348 check_if_tm_restore_required(current);
Anton Blancharda0e72cf2015-10-29 11:44:04 +1100349 __giveup_spe(current);
Anton Blanchardd64d02c2015-12-10 20:04:05 +1100350 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000351}
352EXPORT_SYMBOL(enable_kernel_spe);
353
354void flush_spe_to_thread(struct task_struct *tsk)
355{
356 if (tsk->thread.regs) {
357 preempt_disable();
358 if (tsk->thread.regs->msr & MSR_SPE) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000359 BUG_ON(tsk != current);
yu liu685659e2011-06-14 18:34:25 -0500360 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
Kumar Gala0ee6c152007-08-28 21:15:53 -0500361 giveup_spe(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000362 }
363 preempt_enable();
364 }
365}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000366#endif /* CONFIG_SPE */
367
Anton Blanchardc2085052015-10-29 11:44:08 +1100368static unsigned long msr_all_available;
369
370static int __init init_msr_all_available(void)
371{
372#ifdef CONFIG_PPC_FPU
373 msr_all_available |= MSR_FP;
374#endif
375#ifdef CONFIG_ALTIVEC
376 if (cpu_has_feature(CPU_FTR_ALTIVEC))
377 msr_all_available |= MSR_VEC;
378#endif
379#ifdef CONFIG_VSX
380 if (cpu_has_feature(CPU_FTR_VSX))
381 msr_all_available |= MSR_VSX;
382#endif
383#ifdef CONFIG_SPE
384 if (cpu_has_feature(CPU_FTR_SPE))
385 msr_all_available |= MSR_SPE;
386#endif
387
388 return 0;
389}
390early_initcall(init_msr_all_available);
391
392void giveup_all(struct task_struct *tsk)
393{
394 unsigned long usermsr;
395
396 if (!tsk->thread.regs)
397 return;
398
399 usermsr = tsk->thread.regs->msr;
400
401 if ((usermsr & msr_all_available) == 0)
402 return;
403
404 msr_check_and_set(msr_all_available);
405
406#ifdef CONFIG_PPC_FPU
407 if (usermsr & MSR_FP)
408 __giveup_fpu(tsk);
409#endif
410#ifdef CONFIG_ALTIVEC
411 if (usermsr & MSR_VEC)
412 __giveup_altivec(tsk);
413#endif
414#ifdef CONFIG_VSX
415 if (usermsr & MSR_VSX)
416 __giveup_vsx(tsk);
417#endif
418#ifdef CONFIG_SPE
419 if (usermsr & MSR_SPE)
420 __giveup_spe(tsk);
421#endif
422
423 msr_check_and_clear(msr_all_available);
424}
425EXPORT_SYMBOL(giveup_all);
426
Cyril Bur70fe3d92016-02-29 17:53:47 +1100427void restore_math(struct pt_regs *regs)
428{
429 unsigned long msr;
430
431 if (!current->thread.load_fp && !loadvec(current->thread))
432 return;
433
434 msr = regs->msr;
435 msr_check_and_set(msr_all_available);
436
437 /*
438 * Only reload if the bit is not set in the user MSR, the bit BEING set
439 * indicates that the registers are hot
440 */
441 if ((!(msr & MSR_FP)) && restore_fp(current))
442 msr |= MSR_FP | current->thread.fpexc_mode;
443
444 if ((!(msr & MSR_VEC)) && restore_altivec(current))
445 msr |= MSR_VEC;
446
447 if ((msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC) &&
448 restore_vsx(current)) {
449 msr |= MSR_VSX;
450 }
451
452 msr_check_and_clear(msr_all_available);
453
454 regs->msr = msr;
455}
456
Cyril Burde2a20a2016-02-29 17:53:48 +1100457void save_all(struct task_struct *tsk)
458{
459 unsigned long usermsr;
460
461 if (!tsk->thread.regs)
462 return;
463
464 usermsr = tsk->thread.regs->msr;
465
466 if ((usermsr & msr_all_available) == 0)
467 return;
468
469 msr_check_and_set(msr_all_available);
470
471 if (usermsr & MSR_FP)
Cyril Bur87924682016-02-29 17:53:49 +1100472 save_fpu(tsk);
Cyril Burde2a20a2016-02-29 17:53:48 +1100473
474 if (usermsr & MSR_VEC)
475 __giveup_altivec(tsk);
476
477 if (usermsr & MSR_VSX)
478 __giveup_vsx(tsk);
479
480 if (usermsr & MSR_SPE)
481 __giveup_spe(tsk);
482
483 msr_check_and_clear(msr_all_available);
484}
485
Anton Blanchard579e6332015-10-29 11:44:09 +1100486void flush_all_to_thread(struct task_struct *tsk)
487{
488 if (tsk->thread.regs) {
489 preempt_disable();
490 BUG_ON(tsk != current);
Cyril Burde2a20a2016-02-29 17:53:48 +1100491 save_all(tsk);
Anton Blanchard579e6332015-10-29 11:44:09 +1100492
493#ifdef CONFIG_SPE
494 if (tsk->thread.regs->msr & MSR_SPE)
495 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
496#endif
497
498 preempt_enable();
499 }
500}
501EXPORT_SYMBOL(flush_all_to_thread);
502
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000503#ifdef CONFIG_PPC_ADV_DEBUG_REGS
504void do_send_trap(struct pt_regs *regs, unsigned long address,
505 unsigned long error_code, int signal_code, int breakpt)
506{
507 siginfo_t info;
508
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000509 current->thread.trap_nr = signal_code;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000510 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
511 11, SIGSEGV) == NOTIFY_STOP)
512 return;
513
514 /* Deliver the signal to userspace */
515 info.si_signo = SIGTRAP;
516 info.si_errno = breakpt; /* breakpoint or watchpoint id */
517 info.si_code = signal_code;
518 info.si_addr = (void __user *)address;
519 force_sig_info(SIGTRAP, &info, current);
520}
521#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Michael Neuling9422de32012-12-20 14:06:44 +0000522void do_break (struct pt_regs *regs, unsigned long address,
Luis Machadod6a61bf2008-07-24 02:10:41 +1000523 unsigned long error_code)
524{
525 siginfo_t info;
526
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000527 current->thread.trap_nr = TRAP_HWBKPT;
Luis Machadod6a61bf2008-07-24 02:10:41 +1000528 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
529 11, SIGSEGV) == NOTIFY_STOP)
530 return;
531
Michael Neuling9422de32012-12-20 14:06:44 +0000532 if (debugger_break_match(regs))
Luis Machadod6a61bf2008-07-24 02:10:41 +1000533 return;
534
Michael Neuling9422de32012-12-20 14:06:44 +0000535 /* Clear the breakpoint */
536 hw_breakpoint_disable();
Luis Machadod6a61bf2008-07-24 02:10:41 +1000537
538 /* Deliver the signal to userspace */
539 info.si_signo = SIGTRAP;
540 info.si_errno = 0;
541 info.si_code = TRAP_HWBKPT;
542 info.si_addr = (void __user *)address;
543 force_sig_info(SIGTRAP, &info, current);
544}
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000545#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000546
Michael Neuling9422de32012-12-20 14:06:44 +0000547static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk);
Michael Ellermana2ceff52008-03-28 19:11:48 +1100548
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000549#ifdef CONFIG_PPC_ADV_DEBUG_REGS
550/*
551 * Set the debug registers back to their default "safe" values.
552 */
553static void set_debug_reg_defaults(struct thread_struct *thread)
554{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530555 thread->debug.iac1 = thread->debug.iac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000556#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530557 thread->debug.iac3 = thread->debug.iac4 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000558#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530559 thread->debug.dac1 = thread->debug.dac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000560#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530561 thread->debug.dvc1 = thread->debug.dvc2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000562#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530563 thread->debug.dbcr0 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000564#ifdef CONFIG_BOOKE
565 /*
566 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
567 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530568 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000569 DBCR1_IAC3US | DBCR1_IAC4US;
570 /*
571 * Force Data Address Compare User/Supervisor bits to be User-only
572 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
573 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530574 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000575#else
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530576 thread->debug.dbcr1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000577#endif
578}
579
Scott Woodf5f97212013-11-22 15:52:29 -0600580static void prime_debug_regs(struct debug_reg *debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000581{
Scott Wood6cecf762013-05-13 14:14:53 +0000582 /*
583 * We could have inherited MSR_DE from userspace, since
584 * it doesn't get cleared on exception entry. Make sure
585 * MSR_DE is clear before we enable any debug events.
586 */
587 mtmsr(mfmsr() & ~MSR_DE);
588
Scott Woodf5f97212013-11-22 15:52:29 -0600589 mtspr(SPRN_IAC1, debug->iac1);
590 mtspr(SPRN_IAC2, debug->iac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000591#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Scott Woodf5f97212013-11-22 15:52:29 -0600592 mtspr(SPRN_IAC3, debug->iac3);
593 mtspr(SPRN_IAC4, debug->iac4);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000594#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600595 mtspr(SPRN_DAC1, debug->dac1);
596 mtspr(SPRN_DAC2, debug->dac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000597#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Scott Woodf5f97212013-11-22 15:52:29 -0600598 mtspr(SPRN_DVC1, debug->dvc1);
599 mtspr(SPRN_DVC2, debug->dvc2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000600#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600601 mtspr(SPRN_DBCR0, debug->dbcr0);
602 mtspr(SPRN_DBCR1, debug->dbcr1);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000603#ifdef CONFIG_BOOKE
Scott Woodf5f97212013-11-22 15:52:29 -0600604 mtspr(SPRN_DBCR2, debug->dbcr2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000605#endif
606}
607/*
608 * Unless neither the old or new thread are making use of the
609 * debug registers, set the debug registers from the values
610 * stored in the new thread.
611 */
Scott Woodf5f97212013-11-22 15:52:29 -0600612void switch_booke_debug_regs(struct debug_reg *new_debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000613{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530614 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
Scott Woodf5f97212013-11-22 15:52:29 -0600615 || (new_debug->dbcr0 & DBCR0_IDM))
616 prime_debug_regs(new_debug);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000617}
Bharat Bhushan3743c9b2013-07-04 12:27:44 +0530618EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000619#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
K.Prasade0780b72011-02-10 04:44:35 +0000620#ifndef CONFIG_HAVE_HW_BREAKPOINT
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000621static void set_debug_reg_defaults(struct thread_struct *thread)
622{
Michael Neuling9422de32012-12-20 14:06:44 +0000623 thread->hw_brk.address = 0;
624 thread->hw_brk.type = 0;
Michael Neulingb9818c32013-01-10 14:25:34 +0000625 set_breakpoint(&thread->hw_brk);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000626}
K.Prasade0780b72011-02-10 04:44:35 +0000627#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000628#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
629
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000630#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling9422de32012-12-20 14:06:44 +0000631static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
632{
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000633 mtspr(SPRN_DAC1, dabr);
Dave Kleikamp221c1852010-03-05 10:43:24 +0000634#ifdef CONFIG_PPC_47x
635 isync();
636#endif
Michael Neuling9422de32012-12-20 14:06:44 +0000637 return 0;
638}
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000639#elif defined(CONFIG_PPC_BOOK3S)
Michael Neuling9422de32012-12-20 14:06:44 +0000640static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
641{
Michael Ellermancab0af92005-11-03 15:30:49 +1100642 mtspr(SPRN_DABR, dabr);
Michael Neuling82a9f162013-05-16 20:27:31 +0000643 if (cpu_has_feature(CPU_FTR_DABRX))
644 mtspr(SPRN_DABRX, dabrx);
Michael Ellermancab0af92005-11-03 15:30:49 +1100645 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000646}
Michael Neuling9422de32012-12-20 14:06:44 +0000647#else
648static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
649{
650 return -EINVAL;
651}
652#endif
653
654static inline int set_dabr(struct arch_hw_breakpoint *brk)
655{
656 unsigned long dabr, dabrx;
657
658 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
659 dabrx = ((brk->type >> 3) & 0x7);
660
661 if (ppc_md.set_dabr)
662 return ppc_md.set_dabr(dabr, dabrx);
663
664 return __set_dabr(dabr, dabrx);
665}
666
Michael Neulingbf99de32012-12-20 14:06:45 +0000667static inline int set_dawr(struct arch_hw_breakpoint *brk)
668{
Michael Neuling05d694e2013-01-24 15:02:58 +0000669 unsigned long dawr, dawrx, mrd;
Michael Neulingbf99de32012-12-20 14:06:45 +0000670
671 dawr = brk->address;
672
673 dawrx = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE)) \
674 << (63 - 58); //* read/write bits */
675 dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) \
676 << (63 - 59); //* translate */
677 dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) \
678 >> 3; //* PRIM bits */
Michael Neuling05d694e2013-01-24 15:02:58 +0000679 /* dawr length is stored in field MDR bits 48:53. Matches range in
680 doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
681 0b111111=64DW.
682 brk->len is in bytes.
683 This aligns up to double word size, shifts and does the bias.
684 */
685 mrd = ((brk->len + 7) >> 3) - 1;
686 dawrx |= (mrd & 0x3f) << (63 - 53);
Michael Neulingbf99de32012-12-20 14:06:45 +0000687
688 if (ppc_md.set_dawr)
689 return ppc_md.set_dawr(dawr, dawrx);
690 mtspr(SPRN_DAWR, dawr);
691 mtspr(SPRN_DAWRX, dawrx);
692 return 0;
693}
694
Paul Gortmaker21f58502014-04-29 15:25:17 -0400695void __set_breakpoint(struct arch_hw_breakpoint *brk)
Michael Neuling9422de32012-12-20 14:06:44 +0000696{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500697 memcpy(this_cpu_ptr(&current_brk), brk, sizeof(*brk));
Michael Neuling9422de32012-12-20 14:06:44 +0000698
Michael Neulingbf99de32012-12-20 14:06:45 +0000699 if (cpu_has_feature(CPU_FTR_DAWR))
Paul Gortmaker04c32a52014-04-29 15:25:16 -0400700 set_dawr(brk);
701 else
702 set_dabr(brk);
Michael Neuling9422de32012-12-20 14:06:44 +0000703}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000704
Paul Gortmaker21f58502014-04-29 15:25:17 -0400705void set_breakpoint(struct arch_hw_breakpoint *brk)
706{
707 preempt_disable();
708 __set_breakpoint(brk);
709 preempt_enable();
710}
711
Paul Mackerras06d67d52005-10-10 22:29:05 +1000712#ifdef CONFIG_PPC64
713DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000714#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000715
Michael Neuling9422de32012-12-20 14:06:44 +0000716static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
717 struct arch_hw_breakpoint *b)
718{
719 if (a->address != b->address)
720 return false;
721 if (a->type != b->type)
722 return false;
723 if (a->len != b->len)
724 return false;
725 return true;
726}
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100727
Michael Neulingfb096922013-02-13 16:21:37 +0000728#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100729static void tm_reclaim_thread(struct thread_struct *thr,
730 struct thread_info *ti, uint8_t cause)
731{
732 unsigned long msr_diff = 0;
733
734 /*
735 * If FP/VSX registers have been already saved to the
736 * thread_struct, move them to the transact_fp array.
737 * We clear the TIF_RESTORE_TM bit since after the reclaim
738 * the thread will no longer be transactional.
739 */
740 if (test_ti_thread_flag(ti, TIF_RESTORE_TM)) {
Anshuman Khandual829023d2015-07-06 16:24:10 +0530741 msr_diff = thr->ckpt_regs.msr & ~thr->regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100742 if (msr_diff & MSR_FP)
743 memcpy(&thr->transact_fp, &thr->fp_state,
744 sizeof(struct thread_fp_state));
745 if (msr_diff & MSR_VEC)
746 memcpy(&thr->transact_vr, &thr->vr_state,
747 sizeof(struct thread_vr_state));
748 clear_ti_thread_flag(ti, TIF_RESTORE_TM);
749 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX | MSR_FE0 | MSR_FE1;
750 }
751
Michael Neuling7f821fc2015-11-19 15:44:45 +1100752 /*
753 * Use the current MSR TM suspended bit to track if we have
754 * checkpointed state outstanding.
755 * On signal delivery, we'd normally reclaim the checkpointed
756 * state to obtain stack pointer (see:get_tm_stackpointer()).
757 * This will then directly return to userspace without going
758 * through __switch_to(). However, if the stack frame is bad,
759 * we need to exit this thread which calls __switch_to() which
760 * will again attempt to reclaim the already saved tm state.
761 * Hence we need to check that we've not already reclaimed
762 * this state.
763 * We do this using the current MSR, rather tracking it in
764 * some specific thread_struct bit, as it has the additional
765 * benifit of checking for a potential TM bad thing exception.
766 */
767 if (!MSR_TM_SUSPENDED(mfmsr()))
768 return;
769
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100770 tm_reclaim(thr, thr->regs->msr, cause);
771
772 /* Having done the reclaim, we now have the checkpointed
773 * FP/VSX values in the registers. These might be valid
774 * even if we have previously called enable_kernel_fp() or
775 * flush_fp_to_thread(), so update thr->regs->msr to
776 * indicate their current validity.
777 */
778 thr->regs->msr |= msr_diff;
779}
780
781void tm_reclaim_current(uint8_t cause)
782{
783 tm_enable();
784 tm_reclaim_thread(&current->thread, current_thread_info(), cause);
785}
786
Michael Neulingfb096922013-02-13 16:21:37 +0000787static inline void tm_reclaim_task(struct task_struct *tsk)
788{
789 /* We have to work out if we're switching from/to a task that's in the
790 * middle of a transaction.
791 *
792 * In switching we need to maintain a 2nd register state as
793 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
794 * checkpointed (tbegin) state in ckpt_regs and saves the transactional
795 * (current) FPRs into oldtask->thread.transact_fpr[].
796 *
797 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
798 */
799 struct thread_struct *thr = &tsk->thread;
800
801 if (!thr->regs)
802 return;
803
804 if (!MSR_TM_ACTIVE(thr->regs->msr))
805 goto out_and_saveregs;
806
807 /* Stash the original thread MSR, as giveup_fpu et al will
808 * modify it. We hold onto it to see whether the task used
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100809 * FP & vector regs. If the TIF_RESTORE_TM flag is set,
Anshuman Khandual829023d2015-07-06 16:24:10 +0530810 * ckpt_regs.msr is already set.
Michael Neulingfb096922013-02-13 16:21:37 +0000811 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100812 if (!test_ti_thread_flag(task_thread_info(tsk), TIF_RESTORE_TM))
Anshuman Khandual829023d2015-07-06 16:24:10 +0530813 thr->ckpt_regs.msr = thr->regs->msr;
Michael Neulingfb096922013-02-13 16:21:37 +0000814
815 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
816 "ccr=%lx, msr=%lx, trap=%lx)\n",
817 tsk->pid, thr->regs->nip,
818 thr->regs->ccr, thr->regs->msr,
819 thr->regs->trap);
820
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100821 tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
Michael Neulingfb096922013-02-13 16:21:37 +0000822
823 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
824 tsk->pid);
825
826out_and_saveregs:
827 /* Always save the regs here, even if a transaction's not active.
828 * This context-switches a thread's TM info SPRs. We do it here to
829 * be consistent with the restore path (in recheckpoint) which
830 * cannot happen later in _switch().
831 */
832 tm_save_sprs(thr);
833}
834
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100835extern void __tm_recheckpoint(struct thread_struct *thread,
836 unsigned long orig_msr);
837
838void tm_recheckpoint(struct thread_struct *thread,
839 unsigned long orig_msr)
840{
841 unsigned long flags;
842
843 /* We really can't be interrupted here as the TEXASR registers can't
844 * change and later in the trecheckpoint code, we have a userspace R1.
845 * So let's hard disable over this region.
846 */
847 local_irq_save(flags);
848 hard_irq_disable();
849
850 /* The TM SPRs are restored here, so that TEXASR.FS can be set
851 * before the trecheckpoint and no explosion occurs.
852 */
853 tm_restore_sprs(thread);
854
855 __tm_recheckpoint(thread, orig_msr);
856
857 local_irq_restore(flags);
858}
859
Michael Neulingbc2a9402013-02-13 16:21:40 +0000860static inline void tm_recheckpoint_new_task(struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +0000861{
862 unsigned long msr;
863
864 if (!cpu_has_feature(CPU_FTR_TM))
865 return;
866
867 /* Recheckpoint the registers of the thread we're about to switch to.
868 *
869 * If the task was using FP, we non-lazily reload both the original and
870 * the speculative FP register states. This is because the kernel
871 * doesn't see if/when a TM rollback occurs, so if we take an FP
872 * unavoidable later, we are unable to determine which set of FP regs
873 * need to be restored.
874 */
875 if (!new->thread.regs)
876 return;
877
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100878 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
879 tm_restore_sprs(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +0000880 return;
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100881 }
Anshuman Khandual829023d2015-07-06 16:24:10 +0530882 msr = new->thread.ckpt_regs.msr;
Michael Neulingfb096922013-02-13 16:21:37 +0000883 /* Recheckpoint to restore original checkpointed register state. */
884 TM_DEBUG("*** tm_recheckpoint of pid %d "
885 "(new->msr 0x%lx, new->origmsr 0x%lx)\n",
886 new->pid, new->thread.regs->msr, msr);
887
888 /* This loads the checkpointed FP/VEC state, if used */
889 tm_recheckpoint(&new->thread, msr);
890
891 /* This loads the speculative FP/VEC state, if used */
892 if (msr & MSR_FP) {
893 do_load_up_transact_fpu(&new->thread);
894 new->thread.regs->msr |=
895 (MSR_FP | new->thread.fpexc_mode);
896 }
Michael Neulingf110c0c2013-04-09 16:18:55 +1000897#ifdef CONFIG_ALTIVEC
Michael Neulingfb096922013-02-13 16:21:37 +0000898 if (msr & MSR_VEC) {
899 do_load_up_transact_altivec(&new->thread);
900 new->thread.regs->msr |= MSR_VEC;
901 }
Michael Neulingf110c0c2013-04-09 16:18:55 +1000902#endif
Michael Neulingfb096922013-02-13 16:21:37 +0000903 /* We may as well turn on VSX too since all the state is restored now */
904 if (msr & MSR_VSX)
905 new->thread.regs->msr |= MSR_VSX;
906
907 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
908 "(kernel msr 0x%lx)\n",
909 new->pid, mfmsr());
910}
911
912static inline void __switch_to_tm(struct task_struct *prev)
913{
914 if (cpu_has_feature(CPU_FTR_TM)) {
915 tm_enable();
916 tm_reclaim_task(prev);
917 }
918}
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100919
920/*
921 * This is called if we are on the way out to userspace and the
922 * TIF_RESTORE_TM flag is set. It checks if we need to reload
923 * FP and/or vector state and does so if necessary.
924 * If userspace is inside a transaction (whether active or
925 * suspended) and FP/VMX/VSX instructions have ever been enabled
926 * inside that transaction, then we have to keep them enabled
927 * and keep the FP/VMX/VSX state loaded while ever the transaction
928 * continues. The reason is that if we didn't, and subsequently
929 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
930 * we don't know whether it's the same transaction, and thus we
931 * don't know which of the checkpointed state and the transactional
932 * state to use.
933 */
934void restore_tm_state(struct pt_regs *regs)
935{
936 unsigned long msr_diff;
937
938 clear_thread_flag(TIF_RESTORE_TM);
939 if (!MSR_TM_ACTIVE(regs->msr))
940 return;
941
Anshuman Khandual829023d2015-07-06 16:24:10 +0530942 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100943 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
Cyril Bur70fe3d92016-02-29 17:53:47 +1100944
945 restore_math(regs);
946
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100947 regs->msr |= msr_diff;
948}
949
Michael Neulingfb096922013-02-13 16:21:37 +0000950#else
951#define tm_recheckpoint_new_task(new)
952#define __switch_to_tm(prev)
953#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neuling9422de32012-12-20 14:06:44 +0000954
Anton Blanchard152d5232015-10-29 11:43:55 +1100955static inline void save_sprs(struct thread_struct *t)
956{
957#ifdef CONFIG_ALTIVEC
958 if (cpu_has_feature(cpu_has_feature(CPU_FTR_ALTIVEC)))
959 t->vrsave = mfspr(SPRN_VRSAVE);
960#endif
961#ifdef CONFIG_PPC_BOOK3S_64
962 if (cpu_has_feature(CPU_FTR_DSCR))
963 t->dscr = mfspr(SPRN_DSCR);
964
965 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
966 t->bescr = mfspr(SPRN_BESCR);
967 t->ebbhr = mfspr(SPRN_EBBHR);
968 t->ebbrr = mfspr(SPRN_EBBRR);
969
970 t->fscr = mfspr(SPRN_FSCR);
971
972 /*
973 * Note that the TAR is not available for use in the kernel.
974 * (To provide this, the TAR should be backed up/restored on
975 * exception entry/exit instead, and be in pt_regs. FIXME,
976 * this should be in pt_regs anyway (for debug).)
977 */
978 t->tar = mfspr(SPRN_TAR);
979 }
980#endif
981}
982
983static inline void restore_sprs(struct thread_struct *old_thread,
984 struct thread_struct *new_thread)
985{
986#ifdef CONFIG_ALTIVEC
987 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
988 old_thread->vrsave != new_thread->vrsave)
989 mtspr(SPRN_VRSAVE, new_thread->vrsave);
990#endif
991#ifdef CONFIG_PPC_BOOK3S_64
992 if (cpu_has_feature(CPU_FTR_DSCR)) {
993 u64 dscr = get_paca()->dscr_default;
994 u64 fscr = old_thread->fscr & ~FSCR_DSCR;
995
996 if (new_thread->dscr_inherit) {
997 dscr = new_thread->dscr;
998 fscr |= FSCR_DSCR;
999 }
1000
1001 if (old_thread->dscr != dscr)
1002 mtspr(SPRN_DSCR, dscr);
1003
1004 if (old_thread->fscr != fscr)
1005 mtspr(SPRN_FSCR, fscr);
1006 }
1007
1008 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1009 if (old_thread->bescr != new_thread->bescr)
1010 mtspr(SPRN_BESCR, new_thread->bescr);
1011 if (old_thread->ebbhr != new_thread->ebbhr)
1012 mtspr(SPRN_EBBHR, new_thread->ebbhr);
1013 if (old_thread->ebbrr != new_thread->ebbrr)
1014 mtspr(SPRN_EBBRR, new_thread->ebbrr);
1015
1016 if (old_thread->tar != new_thread->tar)
1017 mtspr(SPRN_TAR, new_thread->tar);
1018 }
1019#endif
1020}
1021
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001022struct task_struct *__switch_to(struct task_struct *prev,
1023 struct task_struct *new)
1024{
1025 struct thread_struct *new_thread, *old_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001026 struct task_struct *last;
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001027#ifdef CONFIG_PPC_BOOK3S_64
1028 struct ppc64_tlb_batch *batch;
1029#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001030
Anton Blanchard152d5232015-10-29 11:43:55 +11001031 new_thread = &new->thread;
1032 old_thread = &current->thread;
1033
Michael Neuling7ba5fef2013-10-02 17:15:14 +10001034 WARN_ON(!irqs_disabled());
1035
Paul Mackerras06d67d52005-10-10 22:29:05 +10001036#ifdef CONFIG_PPC64
1037 /*
1038 * Collect processor utilization data per process
1039 */
1040 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Christoph Lameter69111ba2014-10-21 15:23:25 -05001041 struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001042 long unsigned start_tb, current_tb;
1043 start_tb = old_thread->start_tb;
1044 cu->current_tb = current_tb = mfspr(SPRN_PURR);
1045 old_thread->accum_tb += (current_tb - start_tb);
1046 new_thread->start_tb = current_tb;
1047 }
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001048#endif /* CONFIG_PPC64 */
1049
1050#ifdef CONFIG_PPC_BOOK3S_64
Christoph Lameter69111ba2014-10-21 15:23:25 -05001051 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001052 if (batch->active) {
1053 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1054 if (batch->index)
1055 __flush_tlb_pending(batch);
1056 batch->active = 0;
1057 }
1058#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001059
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001060#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1061 switch_booke_debug_regs(&new->thread.debug);
1062#else
1063/*
1064 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1065 * schedule DABR
1066 */
1067#ifndef CONFIG_HAVE_HW_BREAKPOINT
1068 if (unlikely(!hw_brk_match(this_cpu_ptr(&current_brk), &new->thread.hw_brk)))
1069 __set_breakpoint(&new->thread.hw_brk);
1070#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1071#endif
1072
1073 /*
1074 * We need to save SPRs before treclaim/trecheckpoint as these will
1075 * change a number of them.
1076 */
1077 save_sprs(&prev->thread);
1078
1079 __switch_to_tm(prev);
1080
1081 /* Save FPU, Altivec, VSX and SPE state */
1082 giveup_all(prev);
1083
Anton Blanchard44387e92008-03-17 15:27:09 +11001084 /*
1085 * We can't take a PMU exception inside _switch() since there is a
1086 * window where the kernel stack SLB and the kernel stack are out
1087 * of sync. Hard disable here.
1088 */
1089 hard_irq_disable();
Michael Neulingbc2a9402013-02-13 16:21:40 +00001090
1091 tm_recheckpoint_new_task(new);
1092
Anton Blanchard20dbe672015-12-10 20:44:39 +11001093 /*
1094 * Call restore_sprs() before calling _switch(). If we move it after
1095 * _switch() then we miss out on calling it for new tasks. The reason
1096 * for this is we manually create a stack frame for new tasks that
1097 * directly returns through ret_from_fork() or
1098 * ret_from_kernel_thread(). See copy_thread() for details.
1099 */
Anton Blanchardf3d885c2015-10-29 11:44:10 +11001100 restore_sprs(old_thread, new_thread);
1101
Anton Blanchard20dbe672015-12-10 20:44:39 +11001102 last = _switch(old_thread, new_thread);
1103
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001104#ifdef CONFIG_PPC_BOOK3S_64
1105 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1106 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001107 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001108 batch->active = 1;
1109 }
Cyril Bur70fe3d92016-02-29 17:53:47 +11001110
1111 if (current_thread_info()->task->thread.regs)
1112 restore_math(current_thread_info()->task->thread.regs);
1113
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -07001114#endif /* CONFIG_PPC_BOOK3S_64 */
1115
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001116 return last;
1117}
1118
Paul Mackerras06d67d52005-10-10 22:29:05 +10001119static int instructions_to_print = 16;
1120
Paul Mackerras06d67d52005-10-10 22:29:05 +10001121static void show_instructions(struct pt_regs *regs)
1122{
1123 int i;
1124 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
1125 sizeof(int));
1126
1127 printk("Instruction dump:");
1128
1129 for (i = 0; i < instructions_to_print; i++) {
1130 int instr;
1131
1132 if (!(i % 8))
1133 printk("\n");
1134
Scott Wood0de2d822007-09-28 04:38:55 +10001135#if !defined(CONFIG_BOOKE)
1136 /* If executing with the IMMU off, adjust pc rather
1137 * than print XXXXXXXX.
1138 */
1139 if (!(regs->msr & MSR_IR))
1140 pc = (unsigned long)phys_to_virt(pc);
1141#endif
1142
Anton Blanchard00ae36d2006-10-13 12:17:16 +10001143 if (!__kernel_text_address(pc) ||
Anton Blanchard7b051f62014-10-13 20:27:15 +11001144 probe_kernel_address((unsigned int __user *)pc, instr)) {
Ira Snyder40c8cef2012-01-06 12:34:07 +00001145 printk(KERN_CONT "XXXXXXXX ");
Paul Mackerras06d67d52005-10-10 22:29:05 +10001146 } else {
1147 if (regs->nip == pc)
Ira Snyder40c8cef2012-01-06 12:34:07 +00001148 printk(KERN_CONT "<%08x> ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001149 else
Ira Snyder40c8cef2012-01-06 12:34:07 +00001150 printk(KERN_CONT "%08x ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001151 }
1152
1153 pc += sizeof(int);
1154 }
1155
1156 printk("\n");
1157}
1158
Michael Neuling801c0b22015-11-20 15:15:32 +11001159struct regbit {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001160 unsigned long bit;
1161 const char *name;
Michael Neuling801c0b22015-11-20 15:15:32 +11001162};
1163
1164static struct regbit msr_bits[] = {
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001165#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
1166 {MSR_SF, "SF"},
1167 {MSR_HV, "HV"},
1168#endif
1169 {MSR_VEC, "VEC"},
1170 {MSR_VSX, "VSX"},
1171#ifdef CONFIG_BOOKE
1172 {MSR_CE, "CE"},
1173#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001174 {MSR_EE, "EE"},
1175 {MSR_PR, "PR"},
1176 {MSR_FP, "FP"},
1177 {MSR_ME, "ME"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001178#ifdef CONFIG_BOOKE
Kumar Gala1b983262008-11-19 04:39:53 +00001179 {MSR_DE, "DE"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001180#else
1181 {MSR_SE, "SE"},
1182 {MSR_BE, "BE"},
1183#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001184 {MSR_IR, "IR"},
1185 {MSR_DR, "DR"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +00001186 {MSR_PMM, "PMM"},
1187#ifndef CONFIG_BOOKE
1188 {MSR_RI, "RI"},
1189 {MSR_LE, "LE"},
1190#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001191 {0, NULL}
1192};
1193
Michael Neuling801c0b22015-11-20 15:15:32 +11001194static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
Paul Mackerras06d67d52005-10-10 22:29:05 +10001195{
Michael Neuling801c0b22015-11-20 15:15:32 +11001196 const char *s = "";
Paul Mackerras06d67d52005-10-10 22:29:05 +10001197
Paul Mackerras06d67d52005-10-10 22:29:05 +10001198 for (; bits->bit; ++bits)
1199 if (val & bits->bit) {
Michael Neuling801c0b22015-11-20 15:15:32 +11001200 printk("%s%s", s, bits->name);
1201 s = sep;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001202 }
Michael Neuling801c0b22015-11-20 15:15:32 +11001203}
1204
1205#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1206static struct regbit msr_tm_bits[] = {
1207 {MSR_TS_T, "T"},
1208 {MSR_TS_S, "S"},
1209 {MSR_TM, "E"},
1210 {0, NULL}
1211};
1212
1213static void print_tm_bits(unsigned long val)
1214{
1215/*
1216 * This only prints something if at least one of the TM bit is set.
1217 * Inside the TM[], the output means:
1218 * E: Enabled (bit 32)
1219 * S: Suspended (bit 33)
1220 * T: Transactional (bit 34)
1221 */
1222 if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
1223 printk(",TM[");
1224 print_bits(val, msr_tm_bits, "");
1225 printk("]");
1226 }
1227}
1228#else
1229static void print_tm_bits(unsigned long val) {}
1230#endif
1231
1232static void print_msr_bits(unsigned long val)
1233{
1234 printk("<");
1235 print_bits(val, msr_bits, ",");
1236 print_tm_bits(val);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001237 printk(">");
1238}
1239
1240#ifdef CONFIG_PPC64
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001241#define REG "%016lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001242#define REGS_PER_LINE 4
1243#define LAST_VOLATILE 13
1244#else
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001245#define REG "%08lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001246#define REGS_PER_LINE 8
1247#define LAST_VOLATILE 12
1248#endif
1249
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001250void show_regs(struct pt_regs * regs)
1251{
1252 int i, trap;
1253
Tejun Heoa43cb952013-04-30 15:27:17 -07001254 show_regs_print_info(KERN_DEFAULT);
1255
Paul Mackerras06d67d52005-10-10 22:29:05 +10001256 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
1257 regs->nip, regs->link, regs->ctr);
1258 printk("REGS: %p TRAP: %04lx %s (%s)\n",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001259 regs, regs->trap, print_tainted(), init_utsname()->release);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001260 printk("MSR: "REG" ", regs->msr);
Michael Neuling801c0b22015-11-20 15:15:32 +11001261 print_msr_bits(regs->msr);
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001262 printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001263 trap = TRAP(regs);
Michael Neuling5115a022011-07-14 19:25:12 +00001264 if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001265 printk("CFAR: "REG" ", regs->orig_gpr3);
Anton Blanchardc5400642013-11-15 15:41:19 +11001266 if (trap == 0x200 || trap == 0x300 || trap == 0x600)
Kumar Galaba28c9a2011-10-06 02:53:38 +00001267#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001268 printk("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
Kumar Gala14170782007-07-26 00:46:15 -05001269#else
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001270 printk("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1271#endif
1272#ifdef CONFIG_PPC64
1273 printk("SOFTE: %ld ", regs->softe);
1274#endif
1275#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchard6d888d12013-11-18 13:19:17 +11001276 if (MSR_TM_ACTIVE(regs->msr))
1277 printk("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
Kumar Gala14170782007-07-26 00:46:15 -05001278#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001279
1280 for (i = 0; i < 32; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001281 if ((i % REGS_PER_LINE) == 0)
Kumar Galaa2367192009-06-18 22:29:55 +00001282 printk("\nGPR%02d: ", i);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001283 printk(REG " ", regs->gpr[i]);
1284 if (i == LAST_VOLATILE && !FULL_REGS(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001285 break;
1286 }
1287 printk("\n");
1288#ifdef CONFIG_KALLSYMS
1289 /*
1290 * Lookup NIP late so we have the best change of getting the
1291 * above info out without failing
1292 */
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001293 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1294 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001295#endif
1296 show_stack(current, (unsigned long *) regs->gpr[1]);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001297 if (!user_mode(regs))
1298 show_instructions(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001299}
1300
1301void exit_thread(void)
1302{
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001303}
1304
1305void flush_thread(void)
1306{
K.Prasade0780b72011-02-10 04:44:35 +00001307#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad5aae8a52010-06-15 11:35:19 +05301308 flush_ptrace_hw_breakpoint(current);
K.Prasade0780b72011-02-10 04:44:35 +00001309#else /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001310 set_debug_reg_defaults(&current->thread);
K.Prasade0780b72011-02-10 04:44:35 +00001311#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001312}
1313
1314void
1315release_thread(struct task_struct *t)
1316{
1317}
1318
1319/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001320 * this gets called so that we can store coprocessor state into memory and
1321 * copy the current task into the new thread.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001322 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001323int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001324{
Anton Blanchard579e6332015-10-29 11:44:09 +11001325 flush_all_to_thread(src);
Michael Neuling621b5062014-03-03 14:21:40 +11001326 /*
1327 * Flush TM state out so we can copy it. __switch_to_tm() does this
1328 * flush but it removes the checkpointed state from the current CPU and
1329 * transitions the CPU out of TM mode. Hence we need to call
1330 * tm_recheckpoint_new_task() (on the same task) to restore the
1331 * checkpointed state back and the TM mode.
1332 */
1333 __switch_to_tm(src);
1334 tm_recheckpoint_new_task(src);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001335
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001336 *dst = *src;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001337
1338 clear_task_ebb(dst);
1339
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001340 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001341}
1342
Michael Ellermancec15482014-07-10 12:29:21 +10001343static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1344{
1345#ifdef CONFIG_PPC_STD_MMU_64
1346 unsigned long sp_vsid;
1347 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1348
1349 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1350 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1351 << SLB_VSID_SHIFT_1T;
1352 else
1353 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1354 << SLB_VSID_SHIFT;
1355 sp_vsid |= SLB_VSID_KERNEL | llp;
1356 p->thread.ksp_vsid = sp_vsid;
1357#endif
1358}
1359
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001360/*
1361 * Copy a thread..
1362 */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001363
Alex Dowad6eca8932015-03-13 20:14:46 +02001364/*
1365 * Copy architecture-specific thread state
1366 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -07001367int copy_thread(unsigned long clone_flags, unsigned long usp,
Alex Dowad6eca8932015-03-13 20:14:46 +02001368 unsigned long kthread_arg, struct task_struct *p)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001369{
1370 struct pt_regs *childregs, *kregs;
1371 extern void ret_from_fork(void);
Al Viro58254e12012-09-12 18:32:42 -04001372 extern void ret_from_kernel_thread(void);
1373 void (*f)(void);
Al Viro0cec6fd2006-01-12 01:06:02 -08001374 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001375
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001376 /* Copy registers */
1377 sp -= sizeof(struct pt_regs);
1378 childregs = (struct pt_regs *) sp;
Al Viroab758192012-10-21 22:33:39 -04001379 if (unlikely(p->flags & PF_KTHREAD)) {
Alex Dowad6eca8932015-03-13 20:14:46 +02001380 /* kernel thread */
Al Viro138d1ce2012-10-11 08:41:43 -04001381 struct thread_info *ti = (void *)task_stack_page(p);
Al Viro58254e12012-09-12 18:32:42 -04001382 memset(childregs, 0, sizeof(struct pt_regs));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001383 childregs->gpr[1] = sp + sizeof(struct pt_regs);
Anton Blanchard7cedd602014-02-04 16:08:51 +11001384 /* function */
1385 if (usp)
1386 childregs->gpr[14] = ppc_function_entry((void *)usp);
Al Viro58254e12012-09-12 18:32:42 -04001387#ifdef CONFIG_PPC64
Al Virob5e2fc12006-01-12 01:06:01 -08001388 clear_tsk_thread_flag(p, TIF_32BIT);
Al Viro138d1ce2012-10-11 08:41:43 -04001389 childregs->softe = 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001390#endif
Alex Dowad6eca8932015-03-13 20:14:46 +02001391 childregs->gpr[15] = kthread_arg;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001392 p->thread.regs = NULL; /* no user register state */
Al Viro138d1ce2012-10-11 08:41:43 -04001393 ti->flags |= _TIF_RESTOREALL;
Al Viro58254e12012-09-12 18:32:42 -04001394 f = ret_from_kernel_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001395 } else {
Alex Dowad6eca8932015-03-13 20:14:46 +02001396 /* user thread */
Al Viroafa86fc2012-10-22 22:51:14 -04001397 struct pt_regs *regs = current_pt_regs();
Al Viro58254e12012-09-12 18:32:42 -04001398 CHECK_FULL_REGS(regs);
1399 *childregs = *regs;
Al Viroea516b12012-10-21 22:28:43 -04001400 if (usp)
1401 childregs->gpr[1] = usp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001402 p->thread.regs = childregs;
Al Viro58254e12012-09-12 18:32:42 -04001403 childregs->gpr[3] = 0; /* Result from fork() */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001404 if (clone_flags & CLONE_SETTLS) {
1405#ifdef CONFIG_PPC64
Denis Kirjanov9904b002010-07-29 22:04:39 +00001406 if (!is_32bit_task())
Paul Mackerras06d67d52005-10-10 22:29:05 +10001407 childregs->gpr[13] = childregs->gpr[6];
1408 else
1409#endif
1410 childregs->gpr[2] = childregs->gpr[6];
1411 }
Al Viro58254e12012-09-12 18:32:42 -04001412
1413 f = ret_from_fork;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001414 }
Cyril Burd272f662016-02-29 17:53:46 +11001415 childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001416 sp -= STACK_FRAME_OVERHEAD;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001417
1418 /*
1419 * The way this works is that at some point in the future
1420 * some task will call _switch to switch to the new task.
1421 * That will pop off the stack frame created below and start
1422 * the new task running at ret_from_fork. The new task will
1423 * do some house keeping and then return from the fork or clone
1424 * system call, using the stack frame created above.
1425 */
Li Zhongaf945cf2013-05-06 22:44:41 +00001426 ((unsigned long *)sp)[0] = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001427 sp -= sizeof(struct pt_regs);
1428 kregs = (struct pt_regs *) sp;
1429 sp -= STACK_FRAME_OVERHEAD;
1430 p->thread.ksp = sp;
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001431#ifdef CONFIG_PPC32
Kumar Gala85218822008-04-28 16:21:22 +10001432 p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
1433 _ALIGN_UP(sizeof(struct thread_info), 16);
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001434#endif
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001435#ifdef CONFIG_HAVE_HW_BREAKPOINT
1436 p->thread.ptrace_bps[0] = NULL;
1437#endif
1438
Paul Mackerras18461962013-09-10 20:21:10 +10001439 p->thread.fp_save_area = NULL;
1440#ifdef CONFIG_ALTIVEC
1441 p->thread.vr_save_area = NULL;
1442#endif
1443
Michael Ellermancec15482014-07-10 12:29:21 +10001444 setup_ksp_vsid(p, sp);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001445
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001446#ifdef CONFIG_PPC64
1447 if (cpu_has_feature(CPU_FTR_DSCR)) {
Anton Blanchard1021cb22012-09-03 16:49:47 +00001448 p->thread.dscr_inherit = current->thread.dscr_inherit;
Anton Blancharddb1231dc2015-12-09 20:11:47 +11001449 p->thread.dscr = mfspr(SPRN_DSCR);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001450 }
Haren Myneni92779242012-12-06 21:49:56 +00001451 if (cpu_has_feature(CPU_FTR_HAS_PPR))
1452 p->thread.ppr = INIT_PPR;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001453#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +11001454 kregs->nip = ppc_function_entry(f);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001455 return 0;
1456}
1457
1458/*
1459 * Set up a thread for executing a new program
1460 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001461void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001462{
Michael Ellerman90eac722005-10-21 16:01:33 +10001463#ifdef CONFIG_PPC64
1464 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
1465#endif
1466
Paul Mackerras06d67d52005-10-10 22:29:05 +10001467 /*
1468 * If we exec out of a kernel thread then thread.regs will not be
1469 * set. Do it now.
1470 */
1471 if (!current->thread.regs) {
Al Viro0cec6fd2006-01-12 01:06:02 -08001472 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1473 current->thread.regs = regs - 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001474 }
1475
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001476 memset(regs->gpr, 0, sizeof(regs->gpr));
1477 regs->ctr = 0;
1478 regs->link = 0;
1479 regs->xer = 0;
1480 regs->ccr = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001481 regs->gpr[1] = sp;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001482
Roland McGrath474f8192007-09-24 16:52:44 -07001483 /*
1484 * We have just cleared all the nonvolatile GPRs, so make
1485 * FULL_REGS(regs) return true. This is necessary to allow
1486 * ptrace to examine the thread immediately after exec.
1487 */
1488 regs->trap &= ~1UL;
1489
Paul Mackerras06d67d52005-10-10 22:29:05 +10001490#ifdef CONFIG_PPC32
1491 regs->mq = 0;
1492 regs->nip = start;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001493 regs->msr = MSR_USER;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001494#else
Denis Kirjanov9904b002010-07-29 22:04:39 +00001495 if (!is_32bit_task()) {
Rusty Russell94af3ab2013-11-20 22:15:02 +11001496 unsigned long entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001497
Rusty Russell94af3ab2013-11-20 22:15:02 +11001498 if (is_elf2_task()) {
1499 /* Look ma, no function descriptors! */
1500 entry = start;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001501
Rusty Russell94af3ab2013-11-20 22:15:02 +11001502 /*
1503 * Ulrich says:
1504 * The latest iteration of the ABI requires that when
1505 * calling a function (at its global entry point),
1506 * the caller must ensure r12 holds the entry point
1507 * address (so that the function can quickly
1508 * establish addressability).
1509 */
1510 regs->gpr[12] = start;
1511 /* Make sure that's restored on entry to userspace. */
1512 set_thread_flag(TIF_RESTOREALL);
1513 } else {
1514 unsigned long toc;
1515
1516 /* start is a relocated pointer to the function
1517 * descriptor for the elf _start routine. The first
1518 * entry in the function descriptor is the entry
1519 * address of _start and the second entry is the TOC
1520 * value we need to use.
1521 */
1522 __get_user(entry, (unsigned long __user *)start);
1523 __get_user(toc, (unsigned long __user *)start+1);
1524
1525 /* Check whether the e_entry function descriptor entries
1526 * need to be relocated before we can use them.
1527 */
1528 if (load_addr != 0) {
1529 entry += load_addr;
1530 toc += load_addr;
1531 }
1532 regs->gpr[2] = toc;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001533 }
1534 regs->nip = entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001535 regs->msr = MSR_USER64;
Stephen Rothwelld4bf9a72005-10-13 13:40:54 +10001536 } else {
1537 regs->nip = start;
1538 regs->gpr[2] = 0;
1539 regs->msr = MSR_USER32;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001540 }
1541#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001542#ifdef CONFIG_VSX
1543 current->thread.used_vsr = 0;
1544#endif
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001545 memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
Paul Mackerras18461962013-09-10 20:21:10 +10001546 current->thread.fp_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001547#ifdef CONFIG_ALTIVEC
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001548 memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1549 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
Paul Mackerras18461962013-09-10 20:21:10 +10001550 current->thread.vr_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001551 current->thread.vrsave = 0;
1552 current->thread.used_vr = 0;
1553#endif /* CONFIG_ALTIVEC */
1554#ifdef CONFIG_SPE
1555 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1556 current->thread.acc = 0;
1557 current->thread.spefscr = 0;
1558 current->thread.used_spe = 0;
1559#endif /* CONFIG_SPE */
Michael Neulingbc2a9402013-02-13 16:21:40 +00001560#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1561 if (cpu_has_feature(CPU_FTR_TM))
1562 regs->msr |= MSR_TM;
1563 current->thread.tm_tfhar = 0;
1564 current->thread.tm_texasr = 0;
1565 current->thread.tm_tfiar = 0;
1566#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001567}
Anton Blancharde1802b02014-08-20 08:00:02 +10001568EXPORT_SYMBOL(start_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001569
1570#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1571 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1572
1573int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1574{
1575 struct pt_regs *regs = tsk->thread.regs;
1576
1577 /* This is a bit hairy. If we are an SPE enabled processor
1578 * (have embedded fp) we store the IEEE exception enable flags in
1579 * fpexc_mode. fpexc_mode is also used for setting FP exception
1580 * mode (asyn, precise, disabled) for 'Classic' FP. */
1581 if (val & PR_FP_EXC_SW_ENABLE) {
1582#ifdef CONFIG_SPE
Kumar Gala5e14d212007-09-13 01:44:20 -05001583 if (cpu_has_feature(CPU_FTR_SPE)) {
Joseph Myers640e9222013-12-10 23:07:45 +00001584 /*
1585 * When the sticky exception bits are set
1586 * directly by userspace, it must call prctl
1587 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1588 * in the existing prctl settings) or
1589 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1590 * the bits being set). <fenv.h> functions
1591 * saving and restoring the whole
1592 * floating-point environment need to do so
1593 * anyway to restore the prctl settings from
1594 * the saved environment.
1595 */
1596 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001597 tsk->thread.fpexc_mode = val &
1598 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1599 return 0;
1600 } else {
1601 return -EINVAL;
1602 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001603#else
1604 return -EINVAL;
1605#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001606 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001607
1608 /* on a CONFIG_SPE this does not hurt us. The bits that
1609 * __pack_fe01 use do not overlap with bits used for
1610 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1611 * on CONFIG_SPE implementations are reserved so writing to
1612 * them does not change anything */
1613 if (val > PR_FP_EXC_PRECISE)
1614 return -EINVAL;
1615 tsk->thread.fpexc_mode = __pack_fe01(val);
1616 if (regs != NULL && (regs->msr & MSR_FP) != 0)
1617 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1618 | tsk->thread.fpexc_mode;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001619 return 0;
1620}
1621
1622int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1623{
1624 unsigned int val;
1625
1626 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
1627#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00001628 if (cpu_has_feature(CPU_FTR_SPE)) {
1629 /*
1630 * When the sticky exception bits are set
1631 * directly by userspace, it must call prctl
1632 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1633 * in the existing prctl settings) or
1634 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1635 * the bits being set). <fenv.h> functions
1636 * saving and restoring the whole
1637 * floating-point environment need to do so
1638 * anyway to restore the prctl settings from
1639 * the saved environment.
1640 */
1641 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001642 val = tsk->thread.fpexc_mode;
Joseph Myers640e9222013-12-10 23:07:45 +00001643 } else
Kumar Gala5e14d212007-09-13 01:44:20 -05001644 return -EINVAL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001645#else
1646 return -EINVAL;
1647#endif
1648 else
1649 val = __unpack_fe01(tsk->thread.fpexc_mode);
1650 return put_user(val, (unsigned int __user *) adr);
1651}
1652
Paul Mackerrasfab5db92006-06-07 16:14:40 +10001653int set_endian(struct task_struct *tsk, unsigned int val)
1654{
1655 struct pt_regs *regs = tsk->thread.regs;
1656
1657 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1658 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1659 return -EINVAL;
1660
1661 if (regs == NULL)
1662 return -EINVAL;
1663
1664 if (val == PR_ENDIAN_BIG)
1665 regs->msr &= ~MSR_LE;
1666 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1667 regs->msr |= MSR_LE;
1668 else
1669 return -EINVAL;
1670
1671 return 0;
1672}
1673
1674int get_endian(struct task_struct *tsk, unsigned long adr)
1675{
1676 struct pt_regs *regs = tsk->thread.regs;
1677 unsigned int val;
1678
1679 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1680 !cpu_has_feature(CPU_FTR_REAL_LE))
1681 return -EINVAL;
1682
1683 if (regs == NULL)
1684 return -EINVAL;
1685
1686 if (regs->msr & MSR_LE) {
1687 if (cpu_has_feature(CPU_FTR_REAL_LE))
1688 val = PR_ENDIAN_LITTLE;
1689 else
1690 val = PR_ENDIAN_PPC_LITTLE;
1691 } else
1692 val = PR_ENDIAN_BIG;
1693
1694 return put_user(val, (unsigned int __user *)adr);
1695}
1696
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001697int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
1698{
1699 tsk->thread.align_ctl = val;
1700 return 0;
1701}
1702
1703int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
1704{
1705 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
1706}
1707
Paul Mackerrasbb72c482007-02-19 11:42:42 +11001708static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
1709 unsigned long nbytes)
1710{
1711 unsigned long stack_page;
1712 unsigned long cpu = task_cpu(p);
1713
1714 /*
1715 * Avoid crashing if the stack has overflowed and corrupted
1716 * task_cpu(p), which is in the thread_info struct.
1717 */
1718 if (cpu < NR_CPUS && cpu_possible(cpu)) {
1719 stack_page = (unsigned long) hardirq_ctx[cpu];
1720 if (sp >= stack_page + sizeof(struct thread_struct)
1721 && sp <= stack_page + THREAD_SIZE - nbytes)
1722 return 1;
1723
1724 stack_page = (unsigned long) softirq_ctx[cpu];
1725 if (sp >= stack_page + sizeof(struct thread_struct)
1726 && sp <= stack_page + THREAD_SIZE - nbytes)
1727 return 1;
1728 }
1729 return 0;
1730}
1731
Anton Blanchard2f251942006-03-27 11:46:18 +11001732int validate_sp(unsigned long sp, struct task_struct *p,
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001733 unsigned long nbytes)
1734{
Al Viro0cec6fd2006-01-12 01:06:02 -08001735 unsigned long stack_page = (unsigned long)task_stack_page(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001736
1737 if (sp >= stack_page + sizeof(struct thread_struct)
1738 && sp <= stack_page + THREAD_SIZE - nbytes)
1739 return 1;
1740
Paul Mackerrasbb72c482007-02-19 11:42:42 +11001741 return valid_irq_stack(sp, p, nbytes);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001742}
1743
Anton Blanchard2f251942006-03-27 11:46:18 +11001744EXPORT_SYMBOL(validate_sp);
1745
Paul Mackerras06d67d52005-10-10 22:29:05 +10001746unsigned long get_wchan(struct task_struct *p)
1747{
1748 unsigned long ip, sp;
1749 int count = 0;
1750
1751 if (!p || p == current || p->state == TASK_RUNNING)
1752 return 0;
1753
1754 sp = p->thread.ksp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001755 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001756 return 0;
1757
1758 do {
1759 sp = *(unsigned long *)sp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001760 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001761 return 0;
1762 if (count > 0) {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001763 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10001764 if (!in_sched_functions(ip))
1765 return ip;
1766 }
1767 } while (count++ < 16);
1768 return 0;
1769}
Paul Mackerras06d67d52005-10-10 22:29:05 +10001770
Johannes Bergc4d04be2008-11-20 03:24:07 +00001771static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001772
1773void show_stack(struct task_struct *tsk, unsigned long *stack)
1774{
Paul Mackerras06d67d52005-10-10 22:29:05 +10001775 unsigned long sp, ip, lr, newsp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001776 int count = 0;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001777 int firstframe = 1;
Steven Rostedt6794c782009-02-09 21:10:27 -08001778#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1779 int curr_frame = current->curr_ret_stack;
1780 extern void return_to_handler(void);
Steven Rostedt9135c3c2009-09-15 08:20:15 -07001781 unsigned long rth = (unsigned long)return_to_handler;
Steven Rostedt6794c782009-02-09 21:10:27 -08001782#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001783
1784 sp = (unsigned long) stack;
1785 if (tsk == NULL)
1786 tsk = current;
1787 if (sp == 0) {
1788 if (tsk == current)
Anton Blanchardacf620e2014-10-13 19:41:39 +11001789 sp = current_stack_pointer();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001790 else
1791 sp = tsk->thread.ksp;
1792 }
1793
Paul Mackerras06d67d52005-10-10 22:29:05 +10001794 lr = 0;
1795 printk("Call Trace:\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001796 do {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001797 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001798 return;
1799
1800 stack = (unsigned long *) sp;
1801 newsp = stack[0];
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001802 ip = stack[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10001803 if (!firstframe || ip != lr) {
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001804 printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
Steven Rostedt6794c782009-02-09 21:10:27 -08001805#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Anton Blanchard7d56c652014-09-17 17:07:03 +10001806 if ((ip == rth) && curr_frame >= 0) {
Steven Rostedt6794c782009-02-09 21:10:27 -08001807 printk(" (%pS)",
1808 (void *)current->ret_stack[curr_frame].ret);
1809 curr_frame--;
1810 }
1811#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001812 if (firstframe)
1813 printk(" (unreliable)");
1814 printk("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001815 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001816 firstframe = 0;
1817
1818 /*
1819 * See if this is an exception frame.
1820 * We look for the "regshere" marker in the current frame.
1821 */
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001822 if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
1823 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001824 struct pt_regs *regs = (struct pt_regs *)
1825 (sp + STACK_FRAME_OVERHEAD);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001826 lr = regs->link;
Paul Mackerras9be9be22014-06-12 16:53:08 +10001827 printk("--- interrupt: %lx at %pS\n LR = %pS\n",
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001828 regs->trap, (void *)regs->nip, (void *)lr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001829 firstframe = 1;
1830 }
1831
1832 sp = newsp;
1833 } while (count++ < kstack_depth_to_print);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001834}
Paul Mackerras06d67d52005-10-10 22:29:05 +10001835
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001836#ifdef CONFIG_PPC64
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001837/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10001838void notrace __ppc64_runlatch_on(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001839{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001840 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001841 unsigned long ctrl;
1842
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001843 ctrl = mfspr(SPRN_CTRLF);
1844 ctrl |= CTRL_RUNLATCH;
1845 mtspr(SPRN_CTRLT, ctrl);
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001846
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10001847 ti->local_flags |= _TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001848}
1849
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001850/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10001851void notrace __ppc64_runlatch_off(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001852{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001853 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001854 unsigned long ctrl;
1855
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10001856 ti->local_flags &= ~_TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001857
Anton Blanchard4138d652010-08-06 03:28:19 +00001858 ctrl = mfspr(SPRN_CTRLF);
1859 ctrl &= ~CTRL_RUNLATCH;
1860 mtspr(SPRN_CTRLT, ctrl);
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001861}
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001862#endif /* CONFIG_PPC64 */
Benjamin Herrenschmidtf6a61682008-04-18 16:56:17 +10001863
Anton Blanchardd8390882009-02-22 01:50:03 +00001864unsigned long arch_align_stack(unsigned long sp)
1865{
1866 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
1867 sp -= get_random_int() & ~PAGE_MASK;
1868 return sp & ~0xf;
1869}
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001870
1871static inline unsigned long brk_rnd(void)
1872{
1873 unsigned long rnd = 0;
1874
1875 /* 8MB for 32bit, 1GB for 64bit */
1876 if (is_32bit_task())
1877 rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
1878 else
1879 rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
1880
1881 return rnd << PAGE_SHIFT;
1882}
1883
1884unsigned long arch_randomize_brk(struct mm_struct *mm)
1885{
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001886 unsigned long base = mm->brk;
1887 unsigned long ret;
1888
Kumar Galace7a35c2009-10-16 07:05:17 +00001889#ifdef CONFIG_PPC_STD_MMU_64
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001890 /*
1891 * If we are using 1TB segments and we are allowed to randomise
1892 * the heap, we can put it above 1TB so it is backed by a 1TB
1893 * segment. Otherwise the heap will be in the bottom 1TB
1894 * which always uses 256MB segments and this may result in a
1895 * performance penalty.
1896 */
1897 if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
1898 base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
1899#endif
1900
1901 ret = PAGE_ALIGN(base + brk_rnd());
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001902
1903 if (ret < mm->brk)
1904 return mm->brk;
1905
1906 return ret;
1907}
Anton Blanchard501cb162009-02-22 01:50:07 +00001908