blob: 0cb627662ded11ab23a096238e65a77037c9e3b0 [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
Kevin Hao037f0ee2013-07-14 17:02:05 +080090#ifdef CONFIG_PPC_FPU
Anton Blanchard98da5812015-10-29 11:44:01 +110091void giveup_fpu(struct task_struct *tsk)
92{
93 u64 oldmsr = mfmsr();
94 u64 newmsr;
95
96 check_if_tm_restore_required(tsk);
97
98 newmsr = oldmsr | MSR_FP;
99#ifdef CONFIG_VSX
100 if (cpu_has_feature(CPU_FTR_VSX))
101 newmsr |= MSR_VSX;
102#endif
103 if (oldmsr != newmsr)
104 mtmsr_isync(newmsr);
105
106 __giveup_fpu(tsk);
107}
108EXPORT_SYMBOL(giveup_fpu);
109
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000110/*
111 * Make sure the floating-point register state in the
112 * the thread_struct is up to date for task tsk.
113 */
114void flush_fp_to_thread(struct task_struct *tsk)
115{
116 if (tsk->thread.regs) {
117 /*
118 * We need to disable preemption here because if we didn't,
119 * another process could get scheduled after the regs->msr
120 * test but before we have finished saving the FP registers
121 * to the thread_struct. That process could take over the
122 * FPU, and then when we get scheduled again we would store
123 * bogus values for the remaining FP registers.
124 */
125 preempt_disable();
126 if (tsk->thread.regs->msr & MSR_FP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000127 /*
128 * This should only ever be called for current or
129 * for a stopped child process. Since we save away
Anton Blanchardaf1bbc32015-10-29 11:43:57 +1100130 * the FP register state on context switch,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000131 * there is something wrong if a stopped child appears
132 * to still have its FP state in the CPU registers.
133 */
134 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100135 giveup_fpu(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000136 }
137 preempt_enable();
138 }
139}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000140EXPORT_SYMBOL_GPL(flush_fp_to_thread);
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100141#endif /* CONFIG_PPC_FPU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000142
143void enable_kernel_fp(void)
144{
145 WARN_ON(preemptible());
146
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100147 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100148 giveup_fpu(current);
149 } else {
Anton Blanchard611b0e52015-10-29 11:43:59 +1100150 u64 oldmsr = mfmsr();
151
152 if (!(oldmsr & MSR_FP))
153 mtmsr_isync(oldmsr | MSR_FP);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100154 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000155}
156EXPORT_SYMBOL(enable_kernel_fp);
157
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000158#ifdef CONFIG_ALTIVEC
Anton Blanchard98da5812015-10-29 11:44:01 +1100159void giveup_altivec(struct task_struct *tsk)
160{
161 u64 oldmsr = mfmsr();
162 u64 newmsr;
163
164 check_if_tm_restore_required(tsk);
165
166 newmsr = oldmsr | MSR_VEC;
167 if (oldmsr != newmsr)
168 mtmsr_isync(newmsr);
169
170 __giveup_altivec(tsk);
171}
172EXPORT_SYMBOL(giveup_altivec);
173
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000174void enable_kernel_altivec(void)
175{
176 WARN_ON(preemptible());
177
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100178 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100179 giveup_altivec(current);
180 } else {
Anton Blanchard611b0e52015-10-29 11:43:59 +1100181 u64 oldmsr = mfmsr();
182
183 if (!(oldmsr & MSR_VEC))
184 mtmsr_isync(oldmsr | MSR_VEC);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100185 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000186}
187EXPORT_SYMBOL(enable_kernel_altivec);
188
189/*
190 * Make sure the VMX/Altivec register state in the
191 * the thread_struct is up to date for task tsk.
192 */
193void flush_altivec_to_thread(struct task_struct *tsk)
194{
195 if (tsk->thread.regs) {
196 preempt_disable();
197 if (tsk->thread.regs->msr & MSR_VEC) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000198 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100199 giveup_altivec(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000200 }
201 preempt_enable();
202 }
203}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000204EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000205#endif /* CONFIG_ALTIVEC */
206
Michael Neulingce48b212008-06-25 14:07:18 +1000207#ifdef CONFIG_VSX
Anton Blancharda7d623d2015-10-29 11:44:02 +1100208void giveup_vsx(struct task_struct *tsk)
209{
210 u64 oldmsr = mfmsr();
211 u64 newmsr;
212
213 check_if_tm_restore_required(tsk);
214
215 newmsr = oldmsr | (MSR_FP|MSR_VEC|MSR_VSX);
216 if (oldmsr != newmsr)
217 mtmsr_isync(newmsr);
218
219 if (tsk->thread.regs->msr & MSR_FP)
220 __giveup_fpu(tsk);
221 if (tsk->thread.regs->msr & MSR_VEC)
222 __giveup_altivec(tsk);
223 __giveup_vsx(tsk);
224}
225EXPORT_SYMBOL(giveup_vsx);
226
Michael Neulingce48b212008-06-25 14:07:18 +1000227void enable_kernel_vsx(void)
228{
229 WARN_ON(preemptible());
230
Anton Blanchard611b0e52015-10-29 11:43:59 +1100231 if (current->thread.regs && (current->thread.regs->msr & MSR_VSX)) {
Michael Neulingce48b212008-06-25 14:07:18 +1000232 giveup_vsx(current);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100233 } else {
234 u64 oldmsr = mfmsr();
235
236 if (!(oldmsr & MSR_VSX))
237 mtmsr_isync(oldmsr | MSR_VSX);
238 }
Michael Neulingce48b212008-06-25 14:07:18 +1000239}
240EXPORT_SYMBOL(enable_kernel_vsx);
Michael Neulingce48b212008-06-25 14:07:18 +1000241
242void flush_vsx_to_thread(struct task_struct *tsk)
243{
244 if (tsk->thread.regs) {
245 preempt_disable();
246 if (tsk->thread.regs->msr & MSR_VSX) {
Michael Neulingce48b212008-06-25 14:07:18 +1000247 BUG_ON(tsk != current);
Michael Neulingce48b212008-06-25 14:07:18 +1000248 giveup_vsx(tsk);
249 }
250 preempt_enable();
251 }
252}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000253EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
Michael Neulingce48b212008-06-25 14:07:18 +1000254#endif /* CONFIG_VSX */
255
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000256#ifdef CONFIG_SPE
Anton Blanchard98da5812015-10-29 11:44:01 +1100257void giveup_spe(struct task_struct *tsk)
258{
259 u64 oldmsr = mfmsr();
260 u64 newmsr;
261
262 check_if_tm_restore_required(tsk);
263
264 newmsr = oldmsr | MSR_SPE;
265 if (oldmsr != newmsr)
266 mtmsr_isync(newmsr);
267
268 __giveup_spe(tsk);
269}
270EXPORT_SYMBOL(giveup_spe);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000271
272void enable_kernel_spe(void)
273{
274 WARN_ON(preemptible());
275
Anton Blanchard611b0e52015-10-29 11:43:59 +1100276 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000277 giveup_spe(current);
Anton Blanchard611b0e52015-10-29 11:43:59 +1100278 } else {
279 u64 oldmsr = mfmsr();
280
281 if (!(oldmsr & MSR_SPE))
282 mtmsr_isync(oldmsr | MSR_SPE);
283 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000284}
285EXPORT_SYMBOL(enable_kernel_spe);
286
287void flush_spe_to_thread(struct task_struct *tsk)
288{
289 if (tsk->thread.regs) {
290 preempt_disable();
291 if (tsk->thread.regs->msr & MSR_SPE) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000292 BUG_ON(tsk != current);
yu liu685659e2011-06-14 18:34:25 -0500293 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
Kumar Gala0ee6c152007-08-28 21:15:53 -0500294 giveup_spe(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000295 }
296 preempt_enable();
297 }
298}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000299#endif /* CONFIG_SPE */
300
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000301#ifdef CONFIG_PPC_ADV_DEBUG_REGS
302void do_send_trap(struct pt_regs *regs, unsigned long address,
303 unsigned long error_code, int signal_code, int breakpt)
304{
305 siginfo_t info;
306
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000307 current->thread.trap_nr = signal_code;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000308 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
309 11, SIGSEGV) == NOTIFY_STOP)
310 return;
311
312 /* Deliver the signal to userspace */
313 info.si_signo = SIGTRAP;
314 info.si_errno = breakpt; /* breakpoint or watchpoint id */
315 info.si_code = signal_code;
316 info.si_addr = (void __user *)address;
317 force_sig_info(SIGTRAP, &info, current);
318}
319#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Michael Neuling9422de32012-12-20 14:06:44 +0000320void do_break (struct pt_regs *regs, unsigned long address,
Luis Machadod6a61bf2008-07-24 02:10:41 +1000321 unsigned long error_code)
322{
323 siginfo_t info;
324
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000325 current->thread.trap_nr = TRAP_HWBKPT;
Luis Machadod6a61bf2008-07-24 02:10:41 +1000326 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
327 11, SIGSEGV) == NOTIFY_STOP)
328 return;
329
Michael Neuling9422de32012-12-20 14:06:44 +0000330 if (debugger_break_match(regs))
Luis Machadod6a61bf2008-07-24 02:10:41 +1000331 return;
332
Michael Neuling9422de32012-12-20 14:06:44 +0000333 /* Clear the breakpoint */
334 hw_breakpoint_disable();
Luis Machadod6a61bf2008-07-24 02:10:41 +1000335
336 /* Deliver the signal to userspace */
337 info.si_signo = SIGTRAP;
338 info.si_errno = 0;
339 info.si_code = TRAP_HWBKPT;
340 info.si_addr = (void __user *)address;
341 force_sig_info(SIGTRAP, &info, current);
342}
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000343#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000344
Michael Neuling9422de32012-12-20 14:06:44 +0000345static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk);
Michael Ellermana2ceff52008-03-28 19:11:48 +1100346
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000347#ifdef CONFIG_PPC_ADV_DEBUG_REGS
348/*
349 * Set the debug registers back to their default "safe" values.
350 */
351static void set_debug_reg_defaults(struct thread_struct *thread)
352{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530353 thread->debug.iac1 = thread->debug.iac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000354#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530355 thread->debug.iac3 = thread->debug.iac4 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000356#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530357 thread->debug.dac1 = thread->debug.dac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000358#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530359 thread->debug.dvc1 = thread->debug.dvc2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000360#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530361 thread->debug.dbcr0 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000362#ifdef CONFIG_BOOKE
363 /*
364 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
365 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530366 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000367 DBCR1_IAC3US | DBCR1_IAC4US;
368 /*
369 * Force Data Address Compare User/Supervisor bits to be User-only
370 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
371 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530372 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000373#else
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530374 thread->debug.dbcr1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000375#endif
376}
377
Scott Woodf5f97212013-11-22 15:52:29 -0600378static void prime_debug_regs(struct debug_reg *debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000379{
Scott Wood6cecf762013-05-13 14:14:53 +0000380 /*
381 * We could have inherited MSR_DE from userspace, since
382 * it doesn't get cleared on exception entry. Make sure
383 * MSR_DE is clear before we enable any debug events.
384 */
385 mtmsr(mfmsr() & ~MSR_DE);
386
Scott Woodf5f97212013-11-22 15:52:29 -0600387 mtspr(SPRN_IAC1, debug->iac1);
388 mtspr(SPRN_IAC2, debug->iac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000389#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Scott Woodf5f97212013-11-22 15:52:29 -0600390 mtspr(SPRN_IAC3, debug->iac3);
391 mtspr(SPRN_IAC4, debug->iac4);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000392#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600393 mtspr(SPRN_DAC1, debug->dac1);
394 mtspr(SPRN_DAC2, debug->dac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000395#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Scott Woodf5f97212013-11-22 15:52:29 -0600396 mtspr(SPRN_DVC1, debug->dvc1);
397 mtspr(SPRN_DVC2, debug->dvc2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000398#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600399 mtspr(SPRN_DBCR0, debug->dbcr0);
400 mtspr(SPRN_DBCR1, debug->dbcr1);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000401#ifdef CONFIG_BOOKE
Scott Woodf5f97212013-11-22 15:52:29 -0600402 mtspr(SPRN_DBCR2, debug->dbcr2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000403#endif
404}
405/*
406 * Unless neither the old or new thread are making use of the
407 * debug registers, set the debug registers from the values
408 * stored in the new thread.
409 */
Scott Woodf5f97212013-11-22 15:52:29 -0600410void switch_booke_debug_regs(struct debug_reg *new_debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000411{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530412 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
Scott Woodf5f97212013-11-22 15:52:29 -0600413 || (new_debug->dbcr0 & DBCR0_IDM))
414 prime_debug_regs(new_debug);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000415}
Bharat Bhushan3743c9b2013-07-04 12:27:44 +0530416EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000417#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
K.Prasade0780b72011-02-10 04:44:35 +0000418#ifndef CONFIG_HAVE_HW_BREAKPOINT
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000419static void set_debug_reg_defaults(struct thread_struct *thread)
420{
Michael Neuling9422de32012-12-20 14:06:44 +0000421 thread->hw_brk.address = 0;
422 thread->hw_brk.type = 0;
Michael Neulingb9818c32013-01-10 14:25:34 +0000423 set_breakpoint(&thread->hw_brk);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000424}
K.Prasade0780b72011-02-10 04:44:35 +0000425#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000426#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
427
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000428#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling9422de32012-12-20 14:06:44 +0000429static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
430{
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000431 mtspr(SPRN_DAC1, dabr);
Dave Kleikamp221c1852010-03-05 10:43:24 +0000432#ifdef CONFIG_PPC_47x
433 isync();
434#endif
Michael Neuling9422de32012-12-20 14:06:44 +0000435 return 0;
436}
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000437#elif defined(CONFIG_PPC_BOOK3S)
Michael Neuling9422de32012-12-20 14:06:44 +0000438static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
439{
Michael Ellermancab0af92005-11-03 15:30:49 +1100440 mtspr(SPRN_DABR, dabr);
Michael Neuling82a9f162013-05-16 20:27:31 +0000441 if (cpu_has_feature(CPU_FTR_DABRX))
442 mtspr(SPRN_DABRX, dabrx);
Michael Ellermancab0af92005-11-03 15:30:49 +1100443 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000444}
Michael Neuling9422de32012-12-20 14:06:44 +0000445#else
446static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
447{
448 return -EINVAL;
449}
450#endif
451
452static inline int set_dabr(struct arch_hw_breakpoint *brk)
453{
454 unsigned long dabr, dabrx;
455
456 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
457 dabrx = ((brk->type >> 3) & 0x7);
458
459 if (ppc_md.set_dabr)
460 return ppc_md.set_dabr(dabr, dabrx);
461
462 return __set_dabr(dabr, dabrx);
463}
464
Michael Neulingbf99de32012-12-20 14:06:45 +0000465static inline int set_dawr(struct arch_hw_breakpoint *brk)
466{
Michael Neuling05d694e2013-01-24 15:02:58 +0000467 unsigned long dawr, dawrx, mrd;
Michael Neulingbf99de32012-12-20 14:06:45 +0000468
469 dawr = brk->address;
470
471 dawrx = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE)) \
472 << (63 - 58); //* read/write bits */
473 dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) \
474 << (63 - 59); //* translate */
475 dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) \
476 >> 3; //* PRIM bits */
Michael Neuling05d694e2013-01-24 15:02:58 +0000477 /* dawr length is stored in field MDR bits 48:53. Matches range in
478 doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
479 0b111111=64DW.
480 brk->len is in bytes.
481 This aligns up to double word size, shifts and does the bias.
482 */
483 mrd = ((brk->len + 7) >> 3) - 1;
484 dawrx |= (mrd & 0x3f) << (63 - 53);
Michael Neulingbf99de32012-12-20 14:06:45 +0000485
486 if (ppc_md.set_dawr)
487 return ppc_md.set_dawr(dawr, dawrx);
488 mtspr(SPRN_DAWR, dawr);
489 mtspr(SPRN_DAWRX, dawrx);
490 return 0;
491}
492
Paul Gortmaker21f58502014-04-29 15:25:17 -0400493void __set_breakpoint(struct arch_hw_breakpoint *brk)
Michael Neuling9422de32012-12-20 14:06:44 +0000494{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500495 memcpy(this_cpu_ptr(&current_brk), brk, sizeof(*brk));
Michael Neuling9422de32012-12-20 14:06:44 +0000496
Michael Neulingbf99de32012-12-20 14:06:45 +0000497 if (cpu_has_feature(CPU_FTR_DAWR))
Paul Gortmaker04c32a52014-04-29 15:25:16 -0400498 set_dawr(brk);
499 else
500 set_dabr(brk);
Michael Neuling9422de32012-12-20 14:06:44 +0000501}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000502
Paul Gortmaker21f58502014-04-29 15:25:17 -0400503void set_breakpoint(struct arch_hw_breakpoint *brk)
504{
505 preempt_disable();
506 __set_breakpoint(brk);
507 preempt_enable();
508}
509
Paul Mackerras06d67d52005-10-10 22:29:05 +1000510#ifdef CONFIG_PPC64
511DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000512#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000513
Michael Neuling9422de32012-12-20 14:06:44 +0000514static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
515 struct arch_hw_breakpoint *b)
516{
517 if (a->address != b->address)
518 return false;
519 if (a->type != b->type)
520 return false;
521 if (a->len != b->len)
522 return false;
523 return true;
524}
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100525
Michael Neulingfb096922013-02-13 16:21:37 +0000526#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100527static void tm_reclaim_thread(struct thread_struct *thr,
528 struct thread_info *ti, uint8_t cause)
529{
530 unsigned long msr_diff = 0;
531
532 /*
533 * If FP/VSX registers have been already saved to the
534 * thread_struct, move them to the transact_fp array.
535 * We clear the TIF_RESTORE_TM bit since after the reclaim
536 * the thread will no longer be transactional.
537 */
538 if (test_ti_thread_flag(ti, TIF_RESTORE_TM)) {
Anshuman Khandual829023d2015-07-06 16:24:10 +0530539 msr_diff = thr->ckpt_regs.msr & ~thr->regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100540 if (msr_diff & MSR_FP)
541 memcpy(&thr->transact_fp, &thr->fp_state,
542 sizeof(struct thread_fp_state));
543 if (msr_diff & MSR_VEC)
544 memcpy(&thr->transact_vr, &thr->vr_state,
545 sizeof(struct thread_vr_state));
546 clear_ti_thread_flag(ti, TIF_RESTORE_TM);
547 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX | MSR_FE0 | MSR_FE1;
548 }
549
550 tm_reclaim(thr, thr->regs->msr, cause);
551
552 /* Having done the reclaim, we now have the checkpointed
553 * FP/VSX values in the registers. These might be valid
554 * even if we have previously called enable_kernel_fp() or
555 * flush_fp_to_thread(), so update thr->regs->msr to
556 * indicate their current validity.
557 */
558 thr->regs->msr |= msr_diff;
559}
560
561void tm_reclaim_current(uint8_t cause)
562{
563 tm_enable();
564 tm_reclaim_thread(&current->thread, current_thread_info(), cause);
565}
566
Michael Neulingfb096922013-02-13 16:21:37 +0000567static inline void tm_reclaim_task(struct task_struct *tsk)
568{
569 /* We have to work out if we're switching from/to a task that's in the
570 * middle of a transaction.
571 *
572 * In switching we need to maintain a 2nd register state as
573 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
574 * checkpointed (tbegin) state in ckpt_regs and saves the transactional
575 * (current) FPRs into oldtask->thread.transact_fpr[].
576 *
577 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
578 */
579 struct thread_struct *thr = &tsk->thread;
580
581 if (!thr->regs)
582 return;
583
584 if (!MSR_TM_ACTIVE(thr->regs->msr))
585 goto out_and_saveregs;
586
587 /* Stash the original thread MSR, as giveup_fpu et al will
588 * modify it. We hold onto it to see whether the task used
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100589 * FP & vector regs. If the TIF_RESTORE_TM flag is set,
Anshuman Khandual829023d2015-07-06 16:24:10 +0530590 * ckpt_regs.msr is already set.
Michael Neulingfb096922013-02-13 16:21:37 +0000591 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100592 if (!test_ti_thread_flag(task_thread_info(tsk), TIF_RESTORE_TM))
Anshuman Khandual829023d2015-07-06 16:24:10 +0530593 thr->ckpt_regs.msr = thr->regs->msr;
Michael Neulingfb096922013-02-13 16:21:37 +0000594
595 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
596 "ccr=%lx, msr=%lx, trap=%lx)\n",
597 tsk->pid, thr->regs->nip,
598 thr->regs->ccr, thr->regs->msr,
599 thr->regs->trap);
600
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100601 tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
Michael Neulingfb096922013-02-13 16:21:37 +0000602
603 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
604 tsk->pid);
605
606out_and_saveregs:
607 /* Always save the regs here, even if a transaction's not active.
608 * This context-switches a thread's TM info SPRs. We do it here to
609 * be consistent with the restore path (in recheckpoint) which
610 * cannot happen later in _switch().
611 */
612 tm_save_sprs(thr);
613}
614
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100615extern void __tm_recheckpoint(struct thread_struct *thread,
616 unsigned long orig_msr);
617
618void tm_recheckpoint(struct thread_struct *thread,
619 unsigned long orig_msr)
620{
621 unsigned long flags;
622
623 /* We really can't be interrupted here as the TEXASR registers can't
624 * change and later in the trecheckpoint code, we have a userspace R1.
625 * So let's hard disable over this region.
626 */
627 local_irq_save(flags);
628 hard_irq_disable();
629
630 /* The TM SPRs are restored here, so that TEXASR.FS can be set
631 * before the trecheckpoint and no explosion occurs.
632 */
633 tm_restore_sprs(thread);
634
635 __tm_recheckpoint(thread, orig_msr);
636
637 local_irq_restore(flags);
638}
639
Michael Neulingbc2a9402013-02-13 16:21:40 +0000640static inline void tm_recheckpoint_new_task(struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +0000641{
642 unsigned long msr;
643
644 if (!cpu_has_feature(CPU_FTR_TM))
645 return;
646
647 /* Recheckpoint the registers of the thread we're about to switch to.
648 *
649 * If the task was using FP, we non-lazily reload both the original and
650 * the speculative FP register states. This is because the kernel
651 * doesn't see if/when a TM rollback occurs, so if we take an FP
652 * unavoidable later, we are unable to determine which set of FP regs
653 * need to be restored.
654 */
655 if (!new->thread.regs)
656 return;
657
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100658 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
659 tm_restore_sprs(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +0000660 return;
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100661 }
Anshuman Khandual829023d2015-07-06 16:24:10 +0530662 msr = new->thread.ckpt_regs.msr;
Michael Neulingfb096922013-02-13 16:21:37 +0000663 /* Recheckpoint to restore original checkpointed register state. */
664 TM_DEBUG("*** tm_recheckpoint of pid %d "
665 "(new->msr 0x%lx, new->origmsr 0x%lx)\n",
666 new->pid, new->thread.regs->msr, msr);
667
668 /* This loads the checkpointed FP/VEC state, if used */
669 tm_recheckpoint(&new->thread, msr);
670
671 /* This loads the speculative FP/VEC state, if used */
672 if (msr & MSR_FP) {
673 do_load_up_transact_fpu(&new->thread);
674 new->thread.regs->msr |=
675 (MSR_FP | new->thread.fpexc_mode);
676 }
Michael Neulingf110c0c2013-04-09 16:18:55 +1000677#ifdef CONFIG_ALTIVEC
Michael Neulingfb096922013-02-13 16:21:37 +0000678 if (msr & MSR_VEC) {
679 do_load_up_transact_altivec(&new->thread);
680 new->thread.regs->msr |= MSR_VEC;
681 }
Michael Neulingf110c0c2013-04-09 16:18:55 +1000682#endif
Michael Neulingfb096922013-02-13 16:21:37 +0000683 /* We may as well turn on VSX too since all the state is restored now */
684 if (msr & MSR_VSX)
685 new->thread.regs->msr |= MSR_VSX;
686
687 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
688 "(kernel msr 0x%lx)\n",
689 new->pid, mfmsr());
690}
691
692static inline void __switch_to_tm(struct task_struct *prev)
693{
694 if (cpu_has_feature(CPU_FTR_TM)) {
695 tm_enable();
696 tm_reclaim_task(prev);
697 }
698}
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100699
700/*
701 * This is called if we are on the way out to userspace and the
702 * TIF_RESTORE_TM flag is set. It checks if we need to reload
703 * FP and/or vector state and does so if necessary.
704 * If userspace is inside a transaction (whether active or
705 * suspended) and FP/VMX/VSX instructions have ever been enabled
706 * inside that transaction, then we have to keep them enabled
707 * and keep the FP/VMX/VSX state loaded while ever the transaction
708 * continues. The reason is that if we didn't, and subsequently
709 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
710 * we don't know whether it's the same transaction, and thus we
711 * don't know which of the checkpointed state and the transactional
712 * state to use.
713 */
714void restore_tm_state(struct pt_regs *regs)
715{
716 unsigned long msr_diff;
717
718 clear_thread_flag(TIF_RESTORE_TM);
719 if (!MSR_TM_ACTIVE(regs->msr))
720 return;
721
Anshuman Khandual829023d2015-07-06 16:24:10 +0530722 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100723 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
724 if (msr_diff & MSR_FP) {
725 fp_enable();
726 load_fp_state(&current->thread.fp_state);
727 regs->msr |= current->thread.fpexc_mode;
728 }
729 if (msr_diff & MSR_VEC) {
730 vec_enable();
731 load_vr_state(&current->thread.vr_state);
732 }
733 regs->msr |= msr_diff;
734}
735
Michael Neulingfb096922013-02-13 16:21:37 +0000736#else
737#define tm_recheckpoint_new_task(new)
738#define __switch_to_tm(prev)
739#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neuling9422de32012-12-20 14:06:44 +0000740
Anton Blanchard152d5232015-10-29 11:43:55 +1100741static inline void save_sprs(struct thread_struct *t)
742{
743#ifdef CONFIG_ALTIVEC
744 if (cpu_has_feature(cpu_has_feature(CPU_FTR_ALTIVEC)))
745 t->vrsave = mfspr(SPRN_VRSAVE);
746#endif
747#ifdef CONFIG_PPC_BOOK3S_64
748 if (cpu_has_feature(CPU_FTR_DSCR))
749 t->dscr = mfspr(SPRN_DSCR);
750
751 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
752 t->bescr = mfspr(SPRN_BESCR);
753 t->ebbhr = mfspr(SPRN_EBBHR);
754 t->ebbrr = mfspr(SPRN_EBBRR);
755
756 t->fscr = mfspr(SPRN_FSCR);
757
758 /*
759 * Note that the TAR is not available for use in the kernel.
760 * (To provide this, the TAR should be backed up/restored on
761 * exception entry/exit instead, and be in pt_regs. FIXME,
762 * this should be in pt_regs anyway (for debug).)
763 */
764 t->tar = mfspr(SPRN_TAR);
765 }
766#endif
767}
768
769static inline void restore_sprs(struct thread_struct *old_thread,
770 struct thread_struct *new_thread)
771{
772#ifdef CONFIG_ALTIVEC
773 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
774 old_thread->vrsave != new_thread->vrsave)
775 mtspr(SPRN_VRSAVE, new_thread->vrsave);
776#endif
777#ifdef CONFIG_PPC_BOOK3S_64
778 if (cpu_has_feature(CPU_FTR_DSCR)) {
779 u64 dscr = get_paca()->dscr_default;
780 u64 fscr = old_thread->fscr & ~FSCR_DSCR;
781
782 if (new_thread->dscr_inherit) {
783 dscr = new_thread->dscr;
784 fscr |= FSCR_DSCR;
785 }
786
787 if (old_thread->dscr != dscr)
788 mtspr(SPRN_DSCR, dscr);
789
790 if (old_thread->fscr != fscr)
791 mtspr(SPRN_FSCR, fscr);
792 }
793
794 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
795 if (old_thread->bescr != new_thread->bescr)
796 mtspr(SPRN_BESCR, new_thread->bescr);
797 if (old_thread->ebbhr != new_thread->ebbhr)
798 mtspr(SPRN_EBBHR, new_thread->ebbhr);
799 if (old_thread->ebbrr != new_thread->ebbrr)
800 mtspr(SPRN_EBBRR, new_thread->ebbrr);
801
802 if (old_thread->tar != new_thread->tar)
803 mtspr(SPRN_TAR, new_thread->tar);
804 }
805#endif
806}
807
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000808struct task_struct *__switch_to(struct task_struct *prev,
809 struct task_struct *new)
810{
811 struct thread_struct *new_thread, *old_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000812 struct task_struct *last;
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700813#ifdef CONFIG_PPC_BOOK3S_64
814 struct ppc64_tlb_batch *batch;
815#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000816
Anton Blanchard152d5232015-10-29 11:43:55 +1100817 new_thread = &new->thread;
818 old_thread = &current->thread;
819
Michael Neuling7ba5fef2013-10-02 17:15:14 +1000820 WARN_ON(!irqs_disabled());
821
Anton Blanchard152d5232015-10-29 11:43:55 +1100822 /*
823 * We need to save SPRs before treclaim/trecheckpoint as these will
824 * change a number of them.
Michael Neulingc2d52642013-08-09 17:29:30 +1000825 */
Anton Blanchard152d5232015-10-29 11:43:55 +1100826 save_sprs(&prev->thread);
Michael Neulingc2d52642013-08-09 17:29:30 +1000827
Michael Neulingbc2a9402013-02-13 16:21:40 +0000828 __switch_to_tm(prev);
829
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000830 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
831 giveup_fpu(prev);
832#ifdef CONFIG_ALTIVEC
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000833 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
834 giveup_altivec(prev);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000835#endif /* CONFIG_ALTIVEC */
Michael Neulingce48b212008-06-25 14:07:18 +1000836#ifdef CONFIG_VSX
837 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
Michael Neuling7c292172008-07-11 16:29:12 +1000838 /* VMX and FPU registers are already save here */
839 __giveup_vsx(prev);
Michael Neulingce48b212008-06-25 14:07:18 +1000840#endif /* CONFIG_VSX */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000841#ifdef CONFIG_SPE
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000842 if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
843 giveup_spe(prev);
Paul Mackerrasc0c0d992005-10-01 13:49:08 +1000844#endif /* CONFIG_SPE */
845
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000846#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Scott Woodf5f97212013-11-22 15:52:29 -0600847 switch_booke_debug_regs(&new->thread.debug);
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000848#else
K.Prasad5aae8a52010-06-15 11:35:19 +0530849/*
850 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
851 * schedule DABR
852 */
853#ifndef CONFIG_HAVE_HW_BREAKPOINT
Christoph Lameter69111ba2014-10-21 15:23:25 -0500854 if (unlikely(!hw_brk_match(this_cpu_ptr(&current_brk), &new->thread.hw_brk)))
Paul Gortmaker21f58502014-04-29 15:25:17 -0400855 __set_breakpoint(&new->thread.hw_brk);
K.Prasad5aae8a52010-06-15 11:35:19 +0530856#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000857#endif
858
Paul Mackerras06d67d52005-10-10 22:29:05 +1000859#ifdef CONFIG_PPC64
860 /*
861 * Collect processor utilization data per process
862 */
863 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Christoph Lameter69111ba2014-10-21 15:23:25 -0500864 struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000865 long unsigned start_tb, current_tb;
866 start_tb = old_thread->start_tb;
867 cu->current_tb = current_tb = mfspr(SPRN_PURR);
868 old_thread->accum_tb += (current_tb - start_tb);
869 new_thread->start_tb = current_tb;
870 }
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700871#endif /* CONFIG_PPC64 */
872
873#ifdef CONFIG_PPC_BOOK3S_64
Christoph Lameter69111ba2014-10-21 15:23:25 -0500874 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700875 if (batch->active) {
876 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
877 if (batch->index)
878 __flush_tlb_pending(batch);
879 batch->active = 0;
880 }
881#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerras06d67d52005-10-10 22:29:05 +1000882
Anton Blanchard44387e92008-03-17 15:27:09 +1100883 /*
884 * We can't take a PMU exception inside _switch() since there is a
885 * window where the kernel stack SLB and the kernel stack are out
886 * of sync. Hard disable here.
887 */
888 hard_irq_disable();
Michael Neulingbc2a9402013-02-13 16:21:40 +0000889
890 tm_recheckpoint_new_task(new);
891
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000892 last = _switch(old_thread, new_thread);
893
Anton Blanchard152d5232015-10-29 11:43:55 +1100894 /* Need to recalculate these after calling _switch() */
895 old_thread = &last->thread;
896 new_thread = &current->thread;
897
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700898#ifdef CONFIG_PPC_BOOK3S_64
899 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
900 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
Christoph Lameter69111ba2014-10-21 15:23:25 -0500901 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700902 batch->active = 1;
903 }
904#endif /* CONFIG_PPC_BOOK3S_64 */
905
Anton Blanchard152d5232015-10-29 11:43:55 +1100906 restore_sprs(old_thread, new_thread);
907
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000908 return last;
909}
910
Paul Mackerras06d67d52005-10-10 22:29:05 +1000911static int instructions_to_print = 16;
912
Paul Mackerras06d67d52005-10-10 22:29:05 +1000913static void show_instructions(struct pt_regs *regs)
914{
915 int i;
916 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
917 sizeof(int));
918
919 printk("Instruction dump:");
920
921 for (i = 0; i < instructions_to_print; i++) {
922 int instr;
923
924 if (!(i % 8))
925 printk("\n");
926
Scott Wood0de2d822007-09-28 04:38:55 +1000927#if !defined(CONFIG_BOOKE)
928 /* If executing with the IMMU off, adjust pc rather
929 * than print XXXXXXXX.
930 */
931 if (!(regs->msr & MSR_IR))
932 pc = (unsigned long)phys_to_virt(pc);
933#endif
934
Anton Blanchard00ae36d2006-10-13 12:17:16 +1000935 if (!__kernel_text_address(pc) ||
Anton Blanchard7b051f62014-10-13 20:27:15 +1100936 probe_kernel_address((unsigned int __user *)pc, instr)) {
Ira Snyder40c8cef2012-01-06 12:34:07 +0000937 printk(KERN_CONT "XXXXXXXX ");
Paul Mackerras06d67d52005-10-10 22:29:05 +1000938 } else {
939 if (regs->nip == pc)
Ira Snyder40c8cef2012-01-06 12:34:07 +0000940 printk(KERN_CONT "<%08x> ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000941 else
Ira Snyder40c8cef2012-01-06 12:34:07 +0000942 printk(KERN_CONT "%08x ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000943 }
944
945 pc += sizeof(int);
946 }
947
948 printk("\n");
949}
950
951static struct regbit {
952 unsigned long bit;
953 const char *name;
954} msr_bits[] = {
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +0000955#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
956 {MSR_SF, "SF"},
957 {MSR_HV, "HV"},
958#endif
959 {MSR_VEC, "VEC"},
960 {MSR_VSX, "VSX"},
961#ifdef CONFIG_BOOKE
962 {MSR_CE, "CE"},
963#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +1000964 {MSR_EE, "EE"},
965 {MSR_PR, "PR"},
966 {MSR_FP, "FP"},
967 {MSR_ME, "ME"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +0000968#ifdef CONFIG_BOOKE
Kumar Gala1b983262008-11-19 04:39:53 +0000969 {MSR_DE, "DE"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +0000970#else
971 {MSR_SE, "SE"},
972 {MSR_BE, "BE"},
973#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +1000974 {MSR_IR, "IR"},
975 {MSR_DR, "DR"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +0000976 {MSR_PMM, "PMM"},
977#ifndef CONFIG_BOOKE
978 {MSR_RI, "RI"},
979 {MSR_LE, "LE"},
980#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +1000981 {0, NULL}
982};
983
984static void printbits(unsigned long val, struct regbit *bits)
985{
986 const char *sep = "";
987
988 printk("<");
989 for (; bits->bit; ++bits)
990 if (val & bits->bit) {
991 printk("%s%s", sep, bits->name);
992 sep = ",";
993 }
994 printk(">");
995}
996
997#ifdef CONFIG_PPC64
anton@samba.orgf6f7dde2007-03-20 20:38:19 -0500998#define REG "%016lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +1000999#define REGS_PER_LINE 4
1000#define LAST_VOLATILE 13
1001#else
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001002#define REG "%08lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +10001003#define REGS_PER_LINE 8
1004#define LAST_VOLATILE 12
1005#endif
1006
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001007void show_regs(struct pt_regs * regs)
1008{
1009 int i, trap;
1010
Tejun Heoa43cb952013-04-30 15:27:17 -07001011 show_regs_print_info(KERN_DEFAULT);
1012
Paul Mackerras06d67d52005-10-10 22:29:05 +10001013 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
1014 regs->nip, regs->link, regs->ctr);
1015 printk("REGS: %p TRAP: %04lx %s (%s)\n",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001016 regs, regs->trap, print_tainted(), init_utsname()->release);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001017 printk("MSR: "REG" ", regs->msr);
1018 printbits(regs->msr, msr_bits);
anton@samba.orgf6f7dde2007-03-20 20:38:19 -05001019 printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001020 trap = TRAP(regs);
Michael Neuling5115a022011-07-14 19:25:12 +00001021 if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001022 printk("CFAR: "REG" ", regs->orig_gpr3);
Anton Blanchardc5400642013-11-15 15:41:19 +11001023 if (trap == 0x200 || trap == 0x300 || trap == 0x600)
Kumar Galaba28c9a2011-10-06 02:53:38 +00001024#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001025 printk("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
Kumar Gala14170782007-07-26 00:46:15 -05001026#else
Anton Blanchard9db8bcf2013-11-15 15:48:38 +11001027 printk("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1028#endif
1029#ifdef CONFIG_PPC64
1030 printk("SOFTE: %ld ", regs->softe);
1031#endif
1032#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchard6d888d12013-11-18 13:19:17 +11001033 if (MSR_TM_ACTIVE(regs->msr))
1034 printk("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
Kumar Gala14170782007-07-26 00:46:15 -05001035#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001036
1037 for (i = 0; i < 32; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001038 if ((i % REGS_PER_LINE) == 0)
Kumar Galaa2367192009-06-18 22:29:55 +00001039 printk("\nGPR%02d: ", i);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001040 printk(REG " ", regs->gpr[i]);
1041 if (i == LAST_VOLATILE && !FULL_REGS(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001042 break;
1043 }
1044 printk("\n");
1045#ifdef CONFIG_KALLSYMS
1046 /*
1047 * Lookup NIP late so we have the best change of getting the
1048 * above info out without failing
1049 */
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001050 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1051 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001052#endif
1053 show_stack(current, (unsigned long *) regs->gpr[1]);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001054 if (!user_mode(regs))
1055 show_instructions(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001056}
1057
1058void exit_thread(void)
1059{
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001060}
1061
1062void flush_thread(void)
1063{
K.Prasade0780b72011-02-10 04:44:35 +00001064#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad5aae8a52010-06-15 11:35:19 +05301065 flush_ptrace_hw_breakpoint(current);
K.Prasade0780b72011-02-10 04:44:35 +00001066#else /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001067 set_debug_reg_defaults(&current->thread);
K.Prasade0780b72011-02-10 04:44:35 +00001068#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001069}
1070
1071void
1072release_thread(struct task_struct *t)
1073{
1074}
1075
1076/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001077 * this gets called so that we can store coprocessor state into memory and
1078 * copy the current task into the new thread.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001079 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001080int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001081{
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001082 flush_fp_to_thread(src);
1083 flush_altivec_to_thread(src);
1084 flush_vsx_to_thread(src);
1085 flush_spe_to_thread(src);
Michael Neuling621b5062014-03-03 14:21:40 +11001086 /*
1087 * Flush TM state out so we can copy it. __switch_to_tm() does this
1088 * flush but it removes the checkpointed state from the current CPU and
1089 * transitions the CPU out of TM mode. Hence we need to call
1090 * tm_recheckpoint_new_task() (on the same task) to restore the
1091 * checkpointed state back and the TM mode.
1092 */
1093 __switch_to_tm(src);
1094 tm_recheckpoint_new_task(src);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001095
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001096 *dst = *src;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001097
1098 clear_task_ebb(dst);
1099
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001100 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001101}
1102
Michael Ellermancec15482014-07-10 12:29:21 +10001103static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1104{
1105#ifdef CONFIG_PPC_STD_MMU_64
1106 unsigned long sp_vsid;
1107 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1108
1109 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1110 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1111 << SLB_VSID_SHIFT_1T;
1112 else
1113 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1114 << SLB_VSID_SHIFT;
1115 sp_vsid |= SLB_VSID_KERNEL | llp;
1116 p->thread.ksp_vsid = sp_vsid;
1117#endif
1118}
1119
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001120/*
1121 * Copy a thread..
1122 */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001123
Alex Dowad6eca8932015-03-13 20:14:46 +02001124/*
1125 * Copy architecture-specific thread state
1126 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -07001127int copy_thread(unsigned long clone_flags, unsigned long usp,
Alex Dowad6eca8932015-03-13 20:14:46 +02001128 unsigned long kthread_arg, struct task_struct *p)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001129{
1130 struct pt_regs *childregs, *kregs;
1131 extern void ret_from_fork(void);
Al Viro58254e12012-09-12 18:32:42 -04001132 extern void ret_from_kernel_thread(void);
1133 void (*f)(void);
Al Viro0cec6fd2006-01-12 01:06:02 -08001134 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001135
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001136 /* Copy registers */
1137 sp -= sizeof(struct pt_regs);
1138 childregs = (struct pt_regs *) sp;
Al Viroab758192012-10-21 22:33:39 -04001139 if (unlikely(p->flags & PF_KTHREAD)) {
Alex Dowad6eca8932015-03-13 20:14:46 +02001140 /* kernel thread */
Al Viro138d1ce2012-10-11 08:41:43 -04001141 struct thread_info *ti = (void *)task_stack_page(p);
Al Viro58254e12012-09-12 18:32:42 -04001142 memset(childregs, 0, sizeof(struct pt_regs));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001143 childregs->gpr[1] = sp + sizeof(struct pt_regs);
Anton Blanchard7cedd602014-02-04 16:08:51 +11001144 /* function */
1145 if (usp)
1146 childregs->gpr[14] = ppc_function_entry((void *)usp);
Al Viro58254e12012-09-12 18:32:42 -04001147#ifdef CONFIG_PPC64
Al Virob5e2fc12006-01-12 01:06:01 -08001148 clear_tsk_thread_flag(p, TIF_32BIT);
Al Viro138d1ce2012-10-11 08:41:43 -04001149 childregs->softe = 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001150#endif
Alex Dowad6eca8932015-03-13 20:14:46 +02001151 childregs->gpr[15] = kthread_arg;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001152 p->thread.regs = NULL; /* no user register state */
Al Viro138d1ce2012-10-11 08:41:43 -04001153 ti->flags |= _TIF_RESTOREALL;
Al Viro58254e12012-09-12 18:32:42 -04001154 f = ret_from_kernel_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001155 } else {
Alex Dowad6eca8932015-03-13 20:14:46 +02001156 /* user thread */
Al Viroafa86fc2012-10-22 22:51:14 -04001157 struct pt_regs *regs = current_pt_regs();
Al Viro58254e12012-09-12 18:32:42 -04001158 CHECK_FULL_REGS(regs);
1159 *childregs = *regs;
Al Viroea516b12012-10-21 22:28:43 -04001160 if (usp)
1161 childregs->gpr[1] = usp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001162 p->thread.regs = childregs;
Al Viro58254e12012-09-12 18:32:42 -04001163 childregs->gpr[3] = 0; /* Result from fork() */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001164 if (clone_flags & CLONE_SETTLS) {
1165#ifdef CONFIG_PPC64
Denis Kirjanov9904b002010-07-29 22:04:39 +00001166 if (!is_32bit_task())
Paul Mackerras06d67d52005-10-10 22:29:05 +10001167 childregs->gpr[13] = childregs->gpr[6];
1168 else
1169#endif
1170 childregs->gpr[2] = childregs->gpr[6];
1171 }
Al Viro58254e12012-09-12 18:32:42 -04001172
1173 f = ret_from_fork;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001174 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001175 sp -= STACK_FRAME_OVERHEAD;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001176
1177 /*
1178 * The way this works is that at some point in the future
1179 * some task will call _switch to switch to the new task.
1180 * That will pop off the stack frame created below and start
1181 * the new task running at ret_from_fork. The new task will
1182 * do some house keeping and then return from the fork or clone
1183 * system call, using the stack frame created above.
1184 */
Li Zhongaf945cf2013-05-06 22:44:41 +00001185 ((unsigned long *)sp)[0] = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001186 sp -= sizeof(struct pt_regs);
1187 kregs = (struct pt_regs *) sp;
1188 sp -= STACK_FRAME_OVERHEAD;
1189 p->thread.ksp = sp;
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001190#ifdef CONFIG_PPC32
Kumar Gala85218822008-04-28 16:21:22 +10001191 p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
1192 _ALIGN_UP(sizeof(struct thread_info), 16);
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001193#endif
Oleg Nesterov28d170a2013-04-21 06:47:59 +00001194#ifdef CONFIG_HAVE_HW_BREAKPOINT
1195 p->thread.ptrace_bps[0] = NULL;
1196#endif
1197
Paul Mackerras18461962013-09-10 20:21:10 +10001198 p->thread.fp_save_area = NULL;
1199#ifdef CONFIG_ALTIVEC
1200 p->thread.vr_save_area = NULL;
1201#endif
1202
Michael Ellermancec15482014-07-10 12:29:21 +10001203 setup_ksp_vsid(p, sp);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001204
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001205#ifdef CONFIG_PPC64
1206 if (cpu_has_feature(CPU_FTR_DSCR)) {
Anton Blanchard1021cb22012-09-03 16:49:47 +00001207 p->thread.dscr_inherit = current->thread.dscr_inherit;
1208 p->thread.dscr = current->thread.dscr;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001209 }
Haren Myneni92779242012-12-06 21:49:56 +00001210 if (cpu_has_feature(CPU_FTR_HAS_PPR))
1211 p->thread.ppr = INIT_PPR;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001212#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +11001213 kregs->nip = ppc_function_entry(f);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001214 return 0;
1215}
1216
1217/*
1218 * Set up a thread for executing a new program
1219 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001220void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001221{
Michael Ellerman90eac722005-10-21 16:01:33 +10001222#ifdef CONFIG_PPC64
1223 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
1224#endif
1225
Paul Mackerras06d67d52005-10-10 22:29:05 +10001226 /*
1227 * If we exec out of a kernel thread then thread.regs will not be
1228 * set. Do it now.
1229 */
1230 if (!current->thread.regs) {
Al Viro0cec6fd2006-01-12 01:06:02 -08001231 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1232 current->thread.regs = regs - 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001233 }
1234
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001235 memset(regs->gpr, 0, sizeof(regs->gpr));
1236 regs->ctr = 0;
1237 regs->link = 0;
1238 regs->xer = 0;
1239 regs->ccr = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001240 regs->gpr[1] = sp;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001241
Roland McGrath474f8192007-09-24 16:52:44 -07001242 /*
1243 * We have just cleared all the nonvolatile GPRs, so make
1244 * FULL_REGS(regs) return true. This is necessary to allow
1245 * ptrace to examine the thread immediately after exec.
1246 */
1247 regs->trap &= ~1UL;
1248
Paul Mackerras06d67d52005-10-10 22:29:05 +10001249#ifdef CONFIG_PPC32
1250 regs->mq = 0;
1251 regs->nip = start;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001252 regs->msr = MSR_USER;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001253#else
Denis Kirjanov9904b002010-07-29 22:04:39 +00001254 if (!is_32bit_task()) {
Rusty Russell94af3ab2013-11-20 22:15:02 +11001255 unsigned long entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001256
Rusty Russell94af3ab2013-11-20 22:15:02 +11001257 if (is_elf2_task()) {
1258 /* Look ma, no function descriptors! */
1259 entry = start;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001260
Rusty Russell94af3ab2013-11-20 22:15:02 +11001261 /*
1262 * Ulrich says:
1263 * The latest iteration of the ABI requires that when
1264 * calling a function (at its global entry point),
1265 * the caller must ensure r12 holds the entry point
1266 * address (so that the function can quickly
1267 * establish addressability).
1268 */
1269 regs->gpr[12] = start;
1270 /* Make sure that's restored on entry to userspace. */
1271 set_thread_flag(TIF_RESTOREALL);
1272 } else {
1273 unsigned long toc;
1274
1275 /* start is a relocated pointer to the function
1276 * descriptor for the elf _start routine. The first
1277 * entry in the function descriptor is the entry
1278 * address of _start and the second entry is the TOC
1279 * value we need to use.
1280 */
1281 __get_user(entry, (unsigned long __user *)start);
1282 __get_user(toc, (unsigned long __user *)start+1);
1283
1284 /* Check whether the e_entry function descriptor entries
1285 * need to be relocated before we can use them.
1286 */
1287 if (load_addr != 0) {
1288 entry += load_addr;
1289 toc += load_addr;
1290 }
1291 regs->gpr[2] = toc;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001292 }
1293 regs->nip = entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001294 regs->msr = MSR_USER64;
Stephen Rothwelld4bf9a72005-10-13 13:40:54 +10001295 } else {
1296 regs->nip = start;
1297 regs->gpr[2] = 0;
1298 regs->msr = MSR_USER32;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001299 }
1300#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001301#ifdef CONFIG_VSX
1302 current->thread.used_vsr = 0;
1303#endif
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001304 memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
Paul Mackerras18461962013-09-10 20:21:10 +10001305 current->thread.fp_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001306#ifdef CONFIG_ALTIVEC
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001307 memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1308 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
Paul Mackerras18461962013-09-10 20:21:10 +10001309 current->thread.vr_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001310 current->thread.vrsave = 0;
1311 current->thread.used_vr = 0;
1312#endif /* CONFIG_ALTIVEC */
1313#ifdef CONFIG_SPE
1314 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1315 current->thread.acc = 0;
1316 current->thread.spefscr = 0;
1317 current->thread.used_spe = 0;
1318#endif /* CONFIG_SPE */
Michael Neulingbc2a9402013-02-13 16:21:40 +00001319#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1320 if (cpu_has_feature(CPU_FTR_TM))
1321 regs->msr |= MSR_TM;
1322 current->thread.tm_tfhar = 0;
1323 current->thread.tm_texasr = 0;
1324 current->thread.tm_tfiar = 0;
1325#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001326}
Anton Blancharde1802b02014-08-20 08:00:02 +10001327EXPORT_SYMBOL(start_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001328
1329#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1330 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1331
1332int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1333{
1334 struct pt_regs *regs = tsk->thread.regs;
1335
1336 /* This is a bit hairy. If we are an SPE enabled processor
1337 * (have embedded fp) we store the IEEE exception enable flags in
1338 * fpexc_mode. fpexc_mode is also used for setting FP exception
1339 * mode (asyn, precise, disabled) for 'Classic' FP. */
1340 if (val & PR_FP_EXC_SW_ENABLE) {
1341#ifdef CONFIG_SPE
Kumar Gala5e14d212007-09-13 01:44:20 -05001342 if (cpu_has_feature(CPU_FTR_SPE)) {
Joseph Myers640e9222013-12-10 23:07:45 +00001343 /*
1344 * When the sticky exception bits are set
1345 * directly by userspace, it must call prctl
1346 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1347 * in the existing prctl settings) or
1348 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1349 * the bits being set). <fenv.h> functions
1350 * saving and restoring the whole
1351 * floating-point environment need to do so
1352 * anyway to restore the prctl settings from
1353 * the saved environment.
1354 */
1355 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001356 tsk->thread.fpexc_mode = val &
1357 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1358 return 0;
1359 } else {
1360 return -EINVAL;
1361 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001362#else
1363 return -EINVAL;
1364#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001365 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001366
1367 /* on a CONFIG_SPE this does not hurt us. The bits that
1368 * __pack_fe01 use do not overlap with bits used for
1369 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1370 * on CONFIG_SPE implementations are reserved so writing to
1371 * them does not change anything */
1372 if (val > PR_FP_EXC_PRECISE)
1373 return -EINVAL;
1374 tsk->thread.fpexc_mode = __pack_fe01(val);
1375 if (regs != NULL && (regs->msr & MSR_FP) != 0)
1376 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1377 | tsk->thread.fpexc_mode;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001378 return 0;
1379}
1380
1381int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1382{
1383 unsigned int val;
1384
1385 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
1386#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00001387 if (cpu_has_feature(CPU_FTR_SPE)) {
1388 /*
1389 * When the sticky exception bits are set
1390 * directly by userspace, it must call prctl
1391 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1392 * in the existing prctl settings) or
1393 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1394 * the bits being set). <fenv.h> functions
1395 * saving and restoring the whole
1396 * floating-point environment need to do so
1397 * anyway to restore the prctl settings from
1398 * the saved environment.
1399 */
1400 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001401 val = tsk->thread.fpexc_mode;
Joseph Myers640e9222013-12-10 23:07:45 +00001402 } else
Kumar Gala5e14d212007-09-13 01:44:20 -05001403 return -EINVAL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001404#else
1405 return -EINVAL;
1406#endif
1407 else
1408 val = __unpack_fe01(tsk->thread.fpexc_mode);
1409 return put_user(val, (unsigned int __user *) adr);
1410}
1411
Paul Mackerrasfab5db92006-06-07 16:14:40 +10001412int set_endian(struct task_struct *tsk, unsigned int val)
1413{
1414 struct pt_regs *regs = tsk->thread.regs;
1415
1416 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1417 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1418 return -EINVAL;
1419
1420 if (regs == NULL)
1421 return -EINVAL;
1422
1423 if (val == PR_ENDIAN_BIG)
1424 regs->msr &= ~MSR_LE;
1425 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1426 regs->msr |= MSR_LE;
1427 else
1428 return -EINVAL;
1429
1430 return 0;
1431}
1432
1433int get_endian(struct task_struct *tsk, unsigned long adr)
1434{
1435 struct pt_regs *regs = tsk->thread.regs;
1436 unsigned int val;
1437
1438 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1439 !cpu_has_feature(CPU_FTR_REAL_LE))
1440 return -EINVAL;
1441
1442 if (regs == NULL)
1443 return -EINVAL;
1444
1445 if (regs->msr & MSR_LE) {
1446 if (cpu_has_feature(CPU_FTR_REAL_LE))
1447 val = PR_ENDIAN_LITTLE;
1448 else
1449 val = PR_ENDIAN_PPC_LITTLE;
1450 } else
1451 val = PR_ENDIAN_BIG;
1452
1453 return put_user(val, (unsigned int __user *)adr);
1454}
1455
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001456int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
1457{
1458 tsk->thread.align_ctl = val;
1459 return 0;
1460}
1461
1462int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
1463{
1464 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
1465}
1466
Paul Mackerrasbb72c482007-02-19 11:42:42 +11001467static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
1468 unsigned long nbytes)
1469{
1470 unsigned long stack_page;
1471 unsigned long cpu = task_cpu(p);
1472
1473 /*
1474 * Avoid crashing if the stack has overflowed and corrupted
1475 * task_cpu(p), which is in the thread_info struct.
1476 */
1477 if (cpu < NR_CPUS && cpu_possible(cpu)) {
1478 stack_page = (unsigned long) hardirq_ctx[cpu];
1479 if (sp >= stack_page + sizeof(struct thread_struct)
1480 && sp <= stack_page + THREAD_SIZE - nbytes)
1481 return 1;
1482
1483 stack_page = (unsigned long) softirq_ctx[cpu];
1484 if (sp >= stack_page + sizeof(struct thread_struct)
1485 && sp <= stack_page + THREAD_SIZE - nbytes)
1486 return 1;
1487 }
1488 return 0;
1489}
1490
Anton Blanchard2f251942006-03-27 11:46:18 +11001491int validate_sp(unsigned long sp, struct task_struct *p,
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001492 unsigned long nbytes)
1493{
Al Viro0cec6fd2006-01-12 01:06:02 -08001494 unsigned long stack_page = (unsigned long)task_stack_page(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001495
1496 if (sp >= stack_page + sizeof(struct thread_struct)
1497 && sp <= stack_page + THREAD_SIZE - nbytes)
1498 return 1;
1499
Paul Mackerrasbb72c482007-02-19 11:42:42 +11001500 return valid_irq_stack(sp, p, nbytes);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001501}
1502
Anton Blanchard2f251942006-03-27 11:46:18 +11001503EXPORT_SYMBOL(validate_sp);
1504
Paul Mackerras06d67d52005-10-10 22:29:05 +10001505unsigned long get_wchan(struct task_struct *p)
1506{
1507 unsigned long ip, sp;
1508 int count = 0;
1509
1510 if (!p || p == current || p->state == TASK_RUNNING)
1511 return 0;
1512
1513 sp = p->thread.ksp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001514 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001515 return 0;
1516
1517 do {
1518 sp = *(unsigned long *)sp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001519 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001520 return 0;
1521 if (count > 0) {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001522 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10001523 if (!in_sched_functions(ip))
1524 return ip;
1525 }
1526 } while (count++ < 16);
1527 return 0;
1528}
Paul Mackerras06d67d52005-10-10 22:29:05 +10001529
Johannes Bergc4d04be2008-11-20 03:24:07 +00001530static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001531
1532void show_stack(struct task_struct *tsk, unsigned long *stack)
1533{
Paul Mackerras06d67d52005-10-10 22:29:05 +10001534 unsigned long sp, ip, lr, newsp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001535 int count = 0;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001536 int firstframe = 1;
Steven Rostedt6794c782009-02-09 21:10:27 -08001537#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1538 int curr_frame = current->curr_ret_stack;
1539 extern void return_to_handler(void);
Steven Rostedt9135c3c2009-09-15 08:20:15 -07001540 unsigned long rth = (unsigned long)return_to_handler;
Steven Rostedt6794c782009-02-09 21:10:27 -08001541#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001542
1543 sp = (unsigned long) stack;
1544 if (tsk == NULL)
1545 tsk = current;
1546 if (sp == 0) {
1547 if (tsk == current)
Anton Blanchardacf620e2014-10-13 19:41:39 +11001548 sp = current_stack_pointer();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001549 else
1550 sp = tsk->thread.ksp;
1551 }
1552
Paul Mackerras06d67d52005-10-10 22:29:05 +10001553 lr = 0;
1554 printk("Call Trace:\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001555 do {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001556 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001557 return;
1558
1559 stack = (unsigned long *) sp;
1560 newsp = stack[0];
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001561 ip = stack[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10001562 if (!firstframe || ip != lr) {
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001563 printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
Steven Rostedt6794c782009-02-09 21:10:27 -08001564#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Anton Blanchard7d56c652014-09-17 17:07:03 +10001565 if ((ip == rth) && curr_frame >= 0) {
Steven Rostedt6794c782009-02-09 21:10:27 -08001566 printk(" (%pS)",
1567 (void *)current->ret_stack[curr_frame].ret);
1568 curr_frame--;
1569 }
1570#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001571 if (firstframe)
1572 printk(" (unreliable)");
1573 printk("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001574 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001575 firstframe = 0;
1576
1577 /*
1578 * See if this is an exception frame.
1579 * We look for the "regshere" marker in the current frame.
1580 */
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001581 if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
1582 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001583 struct pt_regs *regs = (struct pt_regs *)
1584 (sp + STACK_FRAME_OVERHEAD);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001585 lr = regs->link;
Paul Mackerras9be9be22014-06-12 16:53:08 +10001586 printk("--- interrupt: %lx at %pS\n LR = %pS\n",
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001587 regs->trap, (void *)regs->nip, (void *)lr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001588 firstframe = 1;
1589 }
1590
1591 sp = newsp;
1592 } while (count++ < kstack_depth_to_print);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001593}
Paul Mackerras06d67d52005-10-10 22:29:05 +10001594
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001595#ifdef CONFIG_PPC64
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001596/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10001597void notrace __ppc64_runlatch_on(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001598{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001599 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001600 unsigned long ctrl;
1601
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001602 ctrl = mfspr(SPRN_CTRLF);
1603 ctrl |= CTRL_RUNLATCH;
1604 mtspr(SPRN_CTRLT, ctrl);
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001605
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10001606 ti->local_flags |= _TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001607}
1608
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001609/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10001610void notrace __ppc64_runlatch_off(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001611{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001612 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001613 unsigned long ctrl;
1614
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10001615 ti->local_flags &= ~_TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001616
Anton Blanchard4138d652010-08-06 03:28:19 +00001617 ctrl = mfspr(SPRN_CTRLF);
1618 ctrl &= ~CTRL_RUNLATCH;
1619 mtspr(SPRN_CTRLT, ctrl);
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001620}
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001621#endif /* CONFIG_PPC64 */
Benjamin Herrenschmidtf6a61682008-04-18 16:56:17 +10001622
Anton Blanchardd8390882009-02-22 01:50:03 +00001623unsigned long arch_align_stack(unsigned long sp)
1624{
1625 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
1626 sp -= get_random_int() & ~PAGE_MASK;
1627 return sp & ~0xf;
1628}
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001629
1630static inline unsigned long brk_rnd(void)
1631{
1632 unsigned long rnd = 0;
1633
1634 /* 8MB for 32bit, 1GB for 64bit */
1635 if (is_32bit_task())
1636 rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
1637 else
1638 rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
1639
1640 return rnd << PAGE_SHIFT;
1641}
1642
1643unsigned long arch_randomize_brk(struct mm_struct *mm)
1644{
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001645 unsigned long base = mm->brk;
1646 unsigned long ret;
1647
Kumar Galace7a35c2009-10-16 07:05:17 +00001648#ifdef CONFIG_PPC_STD_MMU_64
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001649 /*
1650 * If we are using 1TB segments and we are allowed to randomise
1651 * the heap, we can put it above 1TB so it is backed by a 1TB
1652 * segment. Otherwise the heap will be in the bottom 1TB
1653 * which always uses 256MB segments and this may result in a
1654 * performance penalty.
1655 */
1656 if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
1657 base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
1658#endif
1659
1660 ret = PAGE_ALIGN(base + brk_rnd());
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001661
1662 if (ret < mm->brk)
1663 return mm->brk;
1664
1665 return ret;
1666}
Anton Blanchard501cb162009-02-22 01:50:07 +00001667