blob: ef64219548d53d8cd8038fb9d98592ad47cd0278 [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
Paul Mackerras14cf11a2005-09-26 16:04:21 +100091/*
92 * Make sure the floating-point register state in the
93 * the thread_struct is up to date for task tsk.
94 */
95void flush_fp_to_thread(struct task_struct *tsk)
96{
97 if (tsk->thread.regs) {
98 /*
99 * We need to disable preemption here because if we didn't,
100 * another process could get scheduled after the regs->msr
101 * test but before we have finished saving the FP registers
102 * to the thread_struct. That process could take over the
103 * FPU, and then when we get scheduled again we would store
104 * bogus values for the remaining FP registers.
105 */
106 preempt_disable();
107 if (tsk->thread.regs->msr & MSR_FP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000108 /*
109 * This should only ever be called for current or
110 * for a stopped child process. Since we save away
Anton Blanchardaf1bbc32015-10-29 11:43:57 +1100111 * the FP register state on context switch,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000112 * there is something wrong if a stopped child appears
113 * to still have its FP state in the CPU registers.
114 */
115 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100116 check_if_tm_restore_required(tsk);
117 giveup_fpu(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000118 }
119 preempt_enable();
120 }
121}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000122EXPORT_SYMBOL_GPL(flush_fp_to_thread);
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100123#endif /* CONFIG_PPC_FPU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000124
125void enable_kernel_fp(void)
126{
127 WARN_ON(preemptible());
128
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100129 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
130 check_if_tm_restore_required(current);
131 giveup_fpu(current);
132 } else {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000133 giveup_fpu(NULL); /* just enables FP for kernel */
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100134 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000135}
136EXPORT_SYMBOL(enable_kernel_fp);
137
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000138#ifdef CONFIG_ALTIVEC
139void enable_kernel_altivec(void)
140{
141 WARN_ON(preemptible());
142
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100143 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
144 check_if_tm_restore_required(current);
145 giveup_altivec(current);
146 } else {
Anton Blanchard35000872012-04-15 20:56:45 +0000147 giveup_altivec_notask();
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100148 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000149}
150EXPORT_SYMBOL(enable_kernel_altivec);
151
152/*
153 * Make sure the VMX/Altivec register state in the
154 * the thread_struct is up to date for task tsk.
155 */
156void flush_altivec_to_thread(struct task_struct *tsk)
157{
158 if (tsk->thread.regs) {
159 preempt_disable();
160 if (tsk->thread.regs->msr & MSR_VEC) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000161 BUG_ON(tsk != current);
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100162 check_if_tm_restore_required(tsk);
163 giveup_altivec(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000164 }
165 preempt_enable();
166 }
167}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000168EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000169#endif /* CONFIG_ALTIVEC */
170
Michael Neulingce48b212008-06-25 14:07:18 +1000171#ifdef CONFIG_VSX
Michael Neulingce48b212008-06-25 14:07:18 +1000172void enable_kernel_vsx(void)
173{
174 WARN_ON(preemptible());
175
Michael Neulingce48b212008-06-25 14:07:18 +1000176 if (current->thread.regs && (current->thread.regs->msr & MSR_VSX))
177 giveup_vsx(current);
178 else
179 giveup_vsx(NULL); /* just enable vsx for kernel - force */
Michael Neulingce48b212008-06-25 14:07:18 +1000180}
181EXPORT_SYMBOL(enable_kernel_vsx);
Michael Neulingce48b212008-06-25 14:07:18 +1000182
Michael Neuling7c292172008-07-11 16:29:12 +1000183void giveup_vsx(struct task_struct *tsk)
184{
Anton Blanchardb86fd2b2015-10-29 11:43:58 +1100185 check_if_tm_restore_required(tsk);
186 giveup_fpu(tsk);
187 giveup_altivec(tsk);
Michael Neuling7c292172008-07-11 16:29:12 +1000188 __giveup_vsx(tsk);
189}
Anton Blancharde1802b02014-08-20 08:00:02 +1000190EXPORT_SYMBOL(giveup_vsx);
Michael Neuling7c292172008-07-11 16:29:12 +1000191
Michael Neulingce48b212008-06-25 14:07:18 +1000192void flush_vsx_to_thread(struct task_struct *tsk)
193{
194 if (tsk->thread.regs) {
195 preempt_disable();
196 if (tsk->thread.regs->msr & MSR_VSX) {
Michael Neulingce48b212008-06-25 14:07:18 +1000197 BUG_ON(tsk != current);
Michael Neulingce48b212008-06-25 14:07:18 +1000198 giveup_vsx(tsk);
199 }
200 preempt_enable();
201 }
202}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000203EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
Michael Neulingce48b212008-06-25 14:07:18 +1000204#endif /* CONFIG_VSX */
205
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000206#ifdef CONFIG_SPE
207
208void enable_kernel_spe(void)
209{
210 WARN_ON(preemptible());
211
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000212 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
213 giveup_spe(current);
214 else
215 giveup_spe(NULL); /* just enable SPE for kernel - force */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000216}
217EXPORT_SYMBOL(enable_kernel_spe);
218
219void flush_spe_to_thread(struct task_struct *tsk)
220{
221 if (tsk->thread.regs) {
222 preempt_disable();
223 if (tsk->thread.regs->msr & MSR_SPE) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000224 BUG_ON(tsk != current);
yu liu685659e2011-06-14 18:34:25 -0500225 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
Kumar Gala0ee6c152007-08-28 21:15:53 -0500226 giveup_spe(tsk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000227 }
228 preempt_enable();
229 }
230}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000231#endif /* CONFIG_SPE */
232
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000233#ifdef CONFIG_PPC_ADV_DEBUG_REGS
234void do_send_trap(struct pt_regs *regs, unsigned long address,
235 unsigned long error_code, int signal_code, int breakpt)
236{
237 siginfo_t info;
238
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000239 current->thread.trap_nr = signal_code;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000240 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
241 11, SIGSEGV) == NOTIFY_STOP)
242 return;
243
244 /* Deliver the signal to userspace */
245 info.si_signo = SIGTRAP;
246 info.si_errno = breakpt; /* breakpoint or watchpoint id */
247 info.si_code = signal_code;
248 info.si_addr = (void __user *)address;
249 force_sig_info(SIGTRAP, &info, current);
250}
251#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
Michael Neuling9422de32012-12-20 14:06:44 +0000252void do_break (struct pt_regs *regs, unsigned long address,
Luis Machadod6a61bf2008-07-24 02:10:41 +1000253 unsigned long error_code)
254{
255 siginfo_t info;
256
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000257 current->thread.trap_nr = TRAP_HWBKPT;
Luis Machadod6a61bf2008-07-24 02:10:41 +1000258 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
259 11, SIGSEGV) == NOTIFY_STOP)
260 return;
261
Michael Neuling9422de32012-12-20 14:06:44 +0000262 if (debugger_break_match(regs))
Luis Machadod6a61bf2008-07-24 02:10:41 +1000263 return;
264
Michael Neuling9422de32012-12-20 14:06:44 +0000265 /* Clear the breakpoint */
266 hw_breakpoint_disable();
Luis Machadod6a61bf2008-07-24 02:10:41 +1000267
268 /* Deliver the signal to userspace */
269 info.si_signo = SIGTRAP;
270 info.si_errno = 0;
271 info.si_code = TRAP_HWBKPT;
272 info.si_addr = (void __user *)address;
273 force_sig_info(SIGTRAP, &info, current);
274}
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000275#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000276
Michael Neuling9422de32012-12-20 14:06:44 +0000277static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk);
Michael Ellermana2ceff52008-03-28 19:11:48 +1100278
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000279#ifdef CONFIG_PPC_ADV_DEBUG_REGS
280/*
281 * Set the debug registers back to their default "safe" values.
282 */
283static void set_debug_reg_defaults(struct thread_struct *thread)
284{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530285 thread->debug.iac1 = thread->debug.iac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000286#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530287 thread->debug.iac3 = thread->debug.iac4 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000288#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530289 thread->debug.dac1 = thread->debug.dac2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000290#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530291 thread->debug.dvc1 = thread->debug.dvc2 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000292#endif
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530293 thread->debug.dbcr0 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000294#ifdef CONFIG_BOOKE
295 /*
296 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
297 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530298 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000299 DBCR1_IAC3US | DBCR1_IAC4US;
300 /*
301 * Force Data Address Compare User/Supervisor bits to be User-only
302 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
303 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530304 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000305#else
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530306 thread->debug.dbcr1 = 0;
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000307#endif
308}
309
Scott Woodf5f97212013-11-22 15:52:29 -0600310static void prime_debug_regs(struct debug_reg *debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000311{
Scott Wood6cecf762013-05-13 14:14:53 +0000312 /*
313 * We could have inherited MSR_DE from userspace, since
314 * it doesn't get cleared on exception entry. Make sure
315 * MSR_DE is clear before we enable any debug events.
316 */
317 mtmsr(mfmsr() & ~MSR_DE);
318
Scott Woodf5f97212013-11-22 15:52:29 -0600319 mtspr(SPRN_IAC1, debug->iac1);
320 mtspr(SPRN_IAC2, debug->iac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000321#if CONFIG_PPC_ADV_DEBUG_IACS > 2
Scott Woodf5f97212013-11-22 15:52:29 -0600322 mtspr(SPRN_IAC3, debug->iac3);
323 mtspr(SPRN_IAC4, debug->iac4);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000324#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600325 mtspr(SPRN_DAC1, debug->dac1);
326 mtspr(SPRN_DAC2, debug->dac2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000327#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
Scott Woodf5f97212013-11-22 15:52:29 -0600328 mtspr(SPRN_DVC1, debug->dvc1);
329 mtspr(SPRN_DVC2, debug->dvc2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000330#endif
Scott Woodf5f97212013-11-22 15:52:29 -0600331 mtspr(SPRN_DBCR0, debug->dbcr0);
332 mtspr(SPRN_DBCR1, debug->dbcr1);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000333#ifdef CONFIG_BOOKE
Scott Woodf5f97212013-11-22 15:52:29 -0600334 mtspr(SPRN_DBCR2, debug->dbcr2);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000335#endif
336}
337/*
338 * Unless neither the old or new thread are making use of the
339 * debug registers, set the debug registers from the values
340 * stored in the new thread.
341 */
Scott Woodf5f97212013-11-22 15:52:29 -0600342void switch_booke_debug_regs(struct debug_reg *new_debug)
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000343{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530344 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
Scott Woodf5f97212013-11-22 15:52:29 -0600345 || (new_debug->dbcr0 & DBCR0_IDM))
346 prime_debug_regs(new_debug);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000347}
Bharat Bhushan3743c9b2013-07-04 12:27:44 +0530348EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000349#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
K.Prasade0780b72011-02-10 04:44:35 +0000350#ifndef CONFIG_HAVE_HW_BREAKPOINT
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000351static void set_debug_reg_defaults(struct thread_struct *thread)
352{
Michael Neuling9422de32012-12-20 14:06:44 +0000353 thread->hw_brk.address = 0;
354 thread->hw_brk.type = 0;
Michael Neulingb9818c32013-01-10 14:25:34 +0000355 set_breakpoint(&thread->hw_brk);
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000356}
K.Prasade0780b72011-02-10 04:44:35 +0000357#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000358#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
359
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000360#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Michael Neuling9422de32012-12-20 14:06:44 +0000361static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
362{
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000363 mtspr(SPRN_DAC1, dabr);
Dave Kleikamp221c1852010-03-05 10:43:24 +0000364#ifdef CONFIG_PPC_47x
365 isync();
366#endif
Michael Neuling9422de32012-12-20 14:06:44 +0000367 return 0;
368}
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000369#elif defined(CONFIG_PPC_BOOK3S)
Michael Neuling9422de32012-12-20 14:06:44 +0000370static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
371{
Michael Ellermancab0af92005-11-03 15:30:49 +1100372 mtspr(SPRN_DABR, dabr);
Michael Neuling82a9f162013-05-16 20:27:31 +0000373 if (cpu_has_feature(CPU_FTR_DABRX))
374 mtspr(SPRN_DABRX, dabrx);
Michael Ellermancab0af92005-11-03 15:30:49 +1100375 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000376}
Michael Neuling9422de32012-12-20 14:06:44 +0000377#else
378static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
379{
380 return -EINVAL;
381}
382#endif
383
384static inline int set_dabr(struct arch_hw_breakpoint *brk)
385{
386 unsigned long dabr, dabrx;
387
388 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
389 dabrx = ((brk->type >> 3) & 0x7);
390
391 if (ppc_md.set_dabr)
392 return ppc_md.set_dabr(dabr, dabrx);
393
394 return __set_dabr(dabr, dabrx);
395}
396
Michael Neulingbf99de32012-12-20 14:06:45 +0000397static inline int set_dawr(struct arch_hw_breakpoint *brk)
398{
Michael Neuling05d694e2013-01-24 15:02:58 +0000399 unsigned long dawr, dawrx, mrd;
Michael Neulingbf99de32012-12-20 14:06:45 +0000400
401 dawr = brk->address;
402
403 dawrx = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE)) \
404 << (63 - 58); //* read/write bits */
405 dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) \
406 << (63 - 59); //* translate */
407 dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) \
408 >> 3; //* PRIM bits */
Michael Neuling05d694e2013-01-24 15:02:58 +0000409 /* dawr length is stored in field MDR bits 48:53. Matches range in
410 doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
411 0b111111=64DW.
412 brk->len is in bytes.
413 This aligns up to double word size, shifts and does the bias.
414 */
415 mrd = ((brk->len + 7) >> 3) - 1;
416 dawrx |= (mrd & 0x3f) << (63 - 53);
Michael Neulingbf99de32012-12-20 14:06:45 +0000417
418 if (ppc_md.set_dawr)
419 return ppc_md.set_dawr(dawr, dawrx);
420 mtspr(SPRN_DAWR, dawr);
421 mtspr(SPRN_DAWRX, dawrx);
422 return 0;
423}
424
Paul Gortmaker21f58502014-04-29 15:25:17 -0400425void __set_breakpoint(struct arch_hw_breakpoint *brk)
Michael Neuling9422de32012-12-20 14:06:44 +0000426{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500427 memcpy(this_cpu_ptr(&current_brk), brk, sizeof(*brk));
Michael Neuling9422de32012-12-20 14:06:44 +0000428
Michael Neulingbf99de32012-12-20 14:06:45 +0000429 if (cpu_has_feature(CPU_FTR_DAWR))
Paul Gortmaker04c32a52014-04-29 15:25:16 -0400430 set_dawr(brk);
431 else
432 set_dabr(brk);
Michael Neuling9422de32012-12-20 14:06:44 +0000433}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000434
Paul Gortmaker21f58502014-04-29 15:25:17 -0400435void set_breakpoint(struct arch_hw_breakpoint *brk)
436{
437 preempt_disable();
438 __set_breakpoint(brk);
439 preempt_enable();
440}
441
Paul Mackerras06d67d52005-10-10 22:29:05 +1000442#ifdef CONFIG_PPC64
443DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000444#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000445
Michael Neuling9422de32012-12-20 14:06:44 +0000446static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
447 struct arch_hw_breakpoint *b)
448{
449 if (a->address != b->address)
450 return false;
451 if (a->type != b->type)
452 return false;
453 if (a->len != b->len)
454 return false;
455 return true;
456}
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100457
Michael Neulingfb096922013-02-13 16:21:37 +0000458#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100459static void tm_reclaim_thread(struct thread_struct *thr,
460 struct thread_info *ti, uint8_t cause)
461{
462 unsigned long msr_diff = 0;
463
464 /*
465 * If FP/VSX registers have been already saved to the
466 * thread_struct, move them to the transact_fp array.
467 * We clear the TIF_RESTORE_TM bit since after the reclaim
468 * the thread will no longer be transactional.
469 */
470 if (test_ti_thread_flag(ti, TIF_RESTORE_TM)) {
Anshuman Khandual829023d2015-07-06 16:24:10 +0530471 msr_diff = thr->ckpt_regs.msr & ~thr->regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100472 if (msr_diff & MSR_FP)
473 memcpy(&thr->transact_fp, &thr->fp_state,
474 sizeof(struct thread_fp_state));
475 if (msr_diff & MSR_VEC)
476 memcpy(&thr->transact_vr, &thr->vr_state,
477 sizeof(struct thread_vr_state));
478 clear_ti_thread_flag(ti, TIF_RESTORE_TM);
479 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX | MSR_FE0 | MSR_FE1;
480 }
481
482 tm_reclaim(thr, thr->regs->msr, cause);
483
484 /* Having done the reclaim, we now have the checkpointed
485 * FP/VSX values in the registers. These might be valid
486 * even if we have previously called enable_kernel_fp() or
487 * flush_fp_to_thread(), so update thr->regs->msr to
488 * indicate their current validity.
489 */
490 thr->regs->msr |= msr_diff;
491}
492
493void tm_reclaim_current(uint8_t cause)
494{
495 tm_enable();
496 tm_reclaim_thread(&current->thread, current_thread_info(), cause);
497}
498
Michael Neulingfb096922013-02-13 16:21:37 +0000499static inline void tm_reclaim_task(struct task_struct *tsk)
500{
501 /* We have to work out if we're switching from/to a task that's in the
502 * middle of a transaction.
503 *
504 * In switching we need to maintain a 2nd register state as
505 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
506 * checkpointed (tbegin) state in ckpt_regs and saves the transactional
507 * (current) FPRs into oldtask->thread.transact_fpr[].
508 *
509 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
510 */
511 struct thread_struct *thr = &tsk->thread;
512
513 if (!thr->regs)
514 return;
515
516 if (!MSR_TM_ACTIVE(thr->regs->msr))
517 goto out_and_saveregs;
518
519 /* Stash the original thread MSR, as giveup_fpu et al will
520 * modify it. We hold onto it to see whether the task used
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100521 * FP & vector regs. If the TIF_RESTORE_TM flag is set,
Anshuman Khandual829023d2015-07-06 16:24:10 +0530522 * ckpt_regs.msr is already set.
Michael Neulingfb096922013-02-13 16:21:37 +0000523 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100524 if (!test_ti_thread_flag(task_thread_info(tsk), TIF_RESTORE_TM))
Anshuman Khandual829023d2015-07-06 16:24:10 +0530525 thr->ckpt_regs.msr = thr->regs->msr;
Michael Neulingfb096922013-02-13 16:21:37 +0000526
527 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
528 "ccr=%lx, msr=%lx, trap=%lx)\n",
529 tsk->pid, thr->regs->nip,
530 thr->regs->ccr, thr->regs->msr,
531 thr->regs->trap);
532
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100533 tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
Michael Neulingfb096922013-02-13 16:21:37 +0000534
535 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
536 tsk->pid);
537
538out_and_saveregs:
539 /* Always save the regs here, even if a transaction's not active.
540 * This context-switches a thread's TM info SPRs. We do it here to
541 * be consistent with the restore path (in recheckpoint) which
542 * cannot happen later in _switch().
543 */
544 tm_save_sprs(thr);
545}
546
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100547extern void __tm_recheckpoint(struct thread_struct *thread,
548 unsigned long orig_msr);
549
550void tm_recheckpoint(struct thread_struct *thread,
551 unsigned long orig_msr)
552{
553 unsigned long flags;
554
555 /* We really can't be interrupted here as the TEXASR registers can't
556 * change and later in the trecheckpoint code, we have a userspace R1.
557 * So let's hard disable over this region.
558 */
559 local_irq_save(flags);
560 hard_irq_disable();
561
562 /* The TM SPRs are restored here, so that TEXASR.FS can be set
563 * before the trecheckpoint and no explosion occurs.
564 */
565 tm_restore_sprs(thread);
566
567 __tm_recheckpoint(thread, orig_msr);
568
569 local_irq_restore(flags);
570}
571
Michael Neulingbc2a9402013-02-13 16:21:40 +0000572static inline void tm_recheckpoint_new_task(struct task_struct *new)
Michael Neulingfb096922013-02-13 16:21:37 +0000573{
574 unsigned long msr;
575
576 if (!cpu_has_feature(CPU_FTR_TM))
577 return;
578
579 /* Recheckpoint the registers of the thread we're about to switch to.
580 *
581 * If the task was using FP, we non-lazily reload both the original and
582 * the speculative FP register states. This is because the kernel
583 * doesn't see if/when a TM rollback occurs, so if we take an FP
584 * unavoidable later, we are unable to determine which set of FP regs
585 * need to be restored.
586 */
587 if (!new->thread.regs)
588 return;
589
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100590 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
591 tm_restore_sprs(&new->thread);
Michael Neulingfb096922013-02-13 16:21:37 +0000592 return;
Michael Neulinge6b8fd02014-04-04 20:19:48 +1100593 }
Anshuman Khandual829023d2015-07-06 16:24:10 +0530594 msr = new->thread.ckpt_regs.msr;
Michael Neulingfb096922013-02-13 16:21:37 +0000595 /* Recheckpoint to restore original checkpointed register state. */
596 TM_DEBUG("*** tm_recheckpoint of pid %d "
597 "(new->msr 0x%lx, new->origmsr 0x%lx)\n",
598 new->pid, new->thread.regs->msr, msr);
599
600 /* This loads the checkpointed FP/VEC state, if used */
601 tm_recheckpoint(&new->thread, msr);
602
603 /* This loads the speculative FP/VEC state, if used */
604 if (msr & MSR_FP) {
605 do_load_up_transact_fpu(&new->thread);
606 new->thread.regs->msr |=
607 (MSR_FP | new->thread.fpexc_mode);
608 }
Michael Neulingf110c0c2013-04-09 16:18:55 +1000609#ifdef CONFIG_ALTIVEC
Michael Neulingfb096922013-02-13 16:21:37 +0000610 if (msr & MSR_VEC) {
611 do_load_up_transact_altivec(&new->thread);
612 new->thread.regs->msr |= MSR_VEC;
613 }
Michael Neulingf110c0c2013-04-09 16:18:55 +1000614#endif
Michael Neulingfb096922013-02-13 16:21:37 +0000615 /* We may as well turn on VSX too since all the state is restored now */
616 if (msr & MSR_VSX)
617 new->thread.regs->msr |= MSR_VSX;
618
619 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
620 "(kernel msr 0x%lx)\n",
621 new->pid, mfmsr());
622}
623
624static inline void __switch_to_tm(struct task_struct *prev)
625{
626 if (cpu_has_feature(CPU_FTR_TM)) {
627 tm_enable();
628 tm_reclaim_task(prev);
629 }
630}
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100631
632/*
633 * This is called if we are on the way out to userspace and the
634 * TIF_RESTORE_TM flag is set. It checks if we need to reload
635 * FP and/or vector state and does so if necessary.
636 * If userspace is inside a transaction (whether active or
637 * suspended) and FP/VMX/VSX instructions have ever been enabled
638 * inside that transaction, then we have to keep them enabled
639 * and keep the FP/VMX/VSX state loaded while ever the transaction
640 * continues. The reason is that if we didn't, and subsequently
641 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
642 * we don't know whether it's the same transaction, and thus we
643 * don't know which of the checkpointed state and the transactional
644 * state to use.
645 */
646void restore_tm_state(struct pt_regs *regs)
647{
648 unsigned long msr_diff;
649
650 clear_thread_flag(TIF_RESTORE_TM);
651 if (!MSR_TM_ACTIVE(regs->msr))
652 return;
653
Anshuman Khandual829023d2015-07-06 16:24:10 +0530654 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
Paul Mackerrasd31626f2014-01-13 15:56:29 +1100655 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
656 if (msr_diff & MSR_FP) {
657 fp_enable();
658 load_fp_state(&current->thread.fp_state);
659 regs->msr |= current->thread.fpexc_mode;
660 }
661 if (msr_diff & MSR_VEC) {
662 vec_enable();
663 load_vr_state(&current->thread.vr_state);
664 }
665 regs->msr |= msr_diff;
666}
667
Michael Neulingfb096922013-02-13 16:21:37 +0000668#else
669#define tm_recheckpoint_new_task(new)
670#define __switch_to_tm(prev)
671#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Michael Neuling9422de32012-12-20 14:06:44 +0000672
Anton Blanchard152d5232015-10-29 11:43:55 +1100673static inline void save_sprs(struct thread_struct *t)
674{
675#ifdef CONFIG_ALTIVEC
676 if (cpu_has_feature(cpu_has_feature(CPU_FTR_ALTIVEC)))
677 t->vrsave = mfspr(SPRN_VRSAVE);
678#endif
679#ifdef CONFIG_PPC_BOOK3S_64
680 if (cpu_has_feature(CPU_FTR_DSCR))
681 t->dscr = mfspr(SPRN_DSCR);
682
683 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
684 t->bescr = mfspr(SPRN_BESCR);
685 t->ebbhr = mfspr(SPRN_EBBHR);
686 t->ebbrr = mfspr(SPRN_EBBRR);
687
688 t->fscr = mfspr(SPRN_FSCR);
689
690 /*
691 * Note that the TAR is not available for use in the kernel.
692 * (To provide this, the TAR should be backed up/restored on
693 * exception entry/exit instead, and be in pt_regs. FIXME,
694 * this should be in pt_regs anyway (for debug).)
695 */
696 t->tar = mfspr(SPRN_TAR);
697 }
698#endif
699}
700
701static inline void restore_sprs(struct thread_struct *old_thread,
702 struct thread_struct *new_thread)
703{
704#ifdef CONFIG_ALTIVEC
705 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
706 old_thread->vrsave != new_thread->vrsave)
707 mtspr(SPRN_VRSAVE, new_thread->vrsave);
708#endif
709#ifdef CONFIG_PPC_BOOK3S_64
710 if (cpu_has_feature(CPU_FTR_DSCR)) {
711 u64 dscr = get_paca()->dscr_default;
712 u64 fscr = old_thread->fscr & ~FSCR_DSCR;
713
714 if (new_thread->dscr_inherit) {
715 dscr = new_thread->dscr;
716 fscr |= FSCR_DSCR;
717 }
718
719 if (old_thread->dscr != dscr)
720 mtspr(SPRN_DSCR, dscr);
721
722 if (old_thread->fscr != fscr)
723 mtspr(SPRN_FSCR, fscr);
724 }
725
726 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
727 if (old_thread->bescr != new_thread->bescr)
728 mtspr(SPRN_BESCR, new_thread->bescr);
729 if (old_thread->ebbhr != new_thread->ebbhr)
730 mtspr(SPRN_EBBHR, new_thread->ebbhr);
731 if (old_thread->ebbrr != new_thread->ebbrr)
732 mtspr(SPRN_EBBRR, new_thread->ebbrr);
733
734 if (old_thread->tar != new_thread->tar)
735 mtspr(SPRN_TAR, new_thread->tar);
736 }
737#endif
738}
739
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000740struct task_struct *__switch_to(struct task_struct *prev,
741 struct task_struct *new)
742{
743 struct thread_struct *new_thread, *old_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000744 struct task_struct *last;
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700745#ifdef CONFIG_PPC_BOOK3S_64
746 struct ppc64_tlb_batch *batch;
747#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000748
Anton Blanchard152d5232015-10-29 11:43:55 +1100749 new_thread = &new->thread;
750 old_thread = &current->thread;
751
Michael Neuling7ba5fef2013-10-02 17:15:14 +1000752 WARN_ON(!irqs_disabled());
753
Anton Blanchard152d5232015-10-29 11:43:55 +1100754 /*
755 * We need to save SPRs before treclaim/trecheckpoint as these will
756 * change a number of them.
Michael Neulingc2d52642013-08-09 17:29:30 +1000757 */
Anton Blanchard152d5232015-10-29 11:43:55 +1100758 save_sprs(&prev->thread);
Michael Neulingc2d52642013-08-09 17:29:30 +1000759
Michael Neulingbc2a9402013-02-13 16:21:40 +0000760 __switch_to_tm(prev);
761
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000762 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
763 giveup_fpu(prev);
764#ifdef CONFIG_ALTIVEC
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000765 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
766 giveup_altivec(prev);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000767#endif /* CONFIG_ALTIVEC */
Michael Neulingce48b212008-06-25 14:07:18 +1000768#ifdef CONFIG_VSX
769 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
Michael Neuling7c292172008-07-11 16:29:12 +1000770 /* VMX and FPU registers are already save here */
771 __giveup_vsx(prev);
Michael Neulingce48b212008-06-25 14:07:18 +1000772#endif /* CONFIG_VSX */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000773#ifdef CONFIG_SPE
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000774 if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
775 giveup_spe(prev);
Paul Mackerrasc0c0d992005-10-01 13:49:08 +1000776#endif /* CONFIG_SPE */
777
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000778#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Scott Woodf5f97212013-11-22 15:52:29 -0600779 switch_booke_debug_regs(&new->thread.debug);
Benjamin Herrenschmidtc6c9eac2009-09-08 14:16:58 +0000780#else
K.Prasad5aae8a52010-06-15 11:35:19 +0530781/*
782 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
783 * schedule DABR
784 */
785#ifndef CONFIG_HAVE_HW_BREAKPOINT
Christoph Lameter69111ba2014-10-21 15:23:25 -0500786 if (unlikely(!hw_brk_match(this_cpu_ptr(&current_brk), &new->thread.hw_brk)))
Paul Gortmaker21f58502014-04-29 15:25:17 -0400787 __set_breakpoint(&new->thread.hw_brk);
K.Prasad5aae8a52010-06-15 11:35:19 +0530788#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Luis Machadod6a61bf2008-07-24 02:10:41 +1000789#endif
790
Paul Mackerras06d67d52005-10-10 22:29:05 +1000791#ifdef CONFIG_PPC64
792 /*
793 * Collect processor utilization data per process
794 */
795 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Christoph Lameter69111ba2014-10-21 15:23:25 -0500796 struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000797 long unsigned start_tb, current_tb;
798 start_tb = old_thread->start_tb;
799 cu->current_tb = current_tb = mfspr(SPRN_PURR);
800 old_thread->accum_tb += (current_tb - start_tb);
801 new_thread->start_tb = current_tb;
802 }
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700803#endif /* CONFIG_PPC64 */
804
805#ifdef CONFIG_PPC_BOOK3S_64
Christoph Lameter69111ba2014-10-21 15:23:25 -0500806 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700807 if (batch->active) {
808 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
809 if (batch->index)
810 __flush_tlb_pending(batch);
811 batch->active = 0;
812 }
813#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerras06d67d52005-10-10 22:29:05 +1000814
Anton Blanchard44387e92008-03-17 15:27:09 +1100815 /*
816 * We can't take a PMU exception inside _switch() since there is a
817 * window where the kernel stack SLB and the kernel stack are out
818 * of sync. Hard disable here.
819 */
820 hard_irq_disable();
Michael Neulingbc2a9402013-02-13 16:21:40 +0000821
822 tm_recheckpoint_new_task(new);
823
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000824 last = _switch(old_thread, new_thread);
825
Anton Blanchard152d5232015-10-29 11:43:55 +1100826 /* Need to recalculate these after calling _switch() */
827 old_thread = &last->thread;
828 new_thread = &current->thread;
829
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700830#ifdef CONFIG_PPC_BOOK3S_64
831 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
832 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
Christoph Lameter69111ba2014-10-21 15:23:25 -0500833 batch = this_cpu_ptr(&ppc64_tlb_batch);
Peter Zijlstrad6bf29b2011-05-24 17:11:48 -0700834 batch->active = 1;
835 }
836#endif /* CONFIG_PPC_BOOK3S_64 */
837
Anton Blanchard152d5232015-10-29 11:43:55 +1100838 restore_sprs(old_thread, new_thread);
839
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000840 return last;
841}
842
Paul Mackerras06d67d52005-10-10 22:29:05 +1000843static int instructions_to_print = 16;
844
Paul Mackerras06d67d52005-10-10 22:29:05 +1000845static void show_instructions(struct pt_regs *regs)
846{
847 int i;
848 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
849 sizeof(int));
850
851 printk("Instruction dump:");
852
853 for (i = 0; i < instructions_to_print; i++) {
854 int instr;
855
856 if (!(i % 8))
857 printk("\n");
858
Scott Wood0de2d822007-09-28 04:38:55 +1000859#if !defined(CONFIG_BOOKE)
860 /* If executing with the IMMU off, adjust pc rather
861 * than print XXXXXXXX.
862 */
863 if (!(regs->msr & MSR_IR))
864 pc = (unsigned long)phys_to_virt(pc);
865#endif
866
Anton Blanchard00ae36d2006-10-13 12:17:16 +1000867 if (!__kernel_text_address(pc) ||
Anton Blanchard7b051f62014-10-13 20:27:15 +1100868 probe_kernel_address((unsigned int __user *)pc, instr)) {
Ira Snyder40c8cef2012-01-06 12:34:07 +0000869 printk(KERN_CONT "XXXXXXXX ");
Paul Mackerras06d67d52005-10-10 22:29:05 +1000870 } else {
871 if (regs->nip == pc)
Ira Snyder40c8cef2012-01-06 12:34:07 +0000872 printk(KERN_CONT "<%08x> ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000873 else
Ira Snyder40c8cef2012-01-06 12:34:07 +0000874 printk(KERN_CONT "%08x ", instr);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000875 }
876
877 pc += sizeof(int);
878 }
879
880 printk("\n");
881}
882
883static struct regbit {
884 unsigned long bit;
885 const char *name;
886} msr_bits[] = {
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +0000887#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
888 {MSR_SF, "SF"},
889 {MSR_HV, "HV"},
890#endif
891 {MSR_VEC, "VEC"},
892 {MSR_VSX, "VSX"},
893#ifdef CONFIG_BOOKE
894 {MSR_CE, "CE"},
895#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +1000896 {MSR_EE, "EE"},
897 {MSR_PR, "PR"},
898 {MSR_FP, "FP"},
899 {MSR_ME, "ME"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +0000900#ifdef CONFIG_BOOKE
Kumar Gala1b983262008-11-19 04:39:53 +0000901 {MSR_DE, "DE"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +0000902#else
903 {MSR_SE, "SE"},
904 {MSR_BE, "BE"},
905#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +1000906 {MSR_IR, "IR"},
907 {MSR_DR, "DR"},
Anton Blanchard3bfd0c9c2011-11-24 19:35:57 +0000908 {MSR_PMM, "PMM"},
909#ifndef CONFIG_BOOKE
910 {MSR_RI, "RI"},
911 {MSR_LE, "LE"},
912#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +1000913 {0, NULL}
914};
915
916static void printbits(unsigned long val, struct regbit *bits)
917{
918 const char *sep = "";
919
920 printk("<");
921 for (; bits->bit; ++bits)
922 if (val & bits->bit) {
923 printk("%s%s", sep, bits->name);
924 sep = ",";
925 }
926 printk(">");
927}
928
929#ifdef CONFIG_PPC64
anton@samba.orgf6f7dde2007-03-20 20:38:19 -0500930#define REG "%016lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +1000931#define REGS_PER_LINE 4
932#define LAST_VOLATILE 13
933#else
anton@samba.orgf6f7dde2007-03-20 20:38:19 -0500934#define REG "%08lx"
Paul Mackerras06d67d52005-10-10 22:29:05 +1000935#define REGS_PER_LINE 8
936#define LAST_VOLATILE 12
937#endif
938
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000939void show_regs(struct pt_regs * regs)
940{
941 int i, trap;
942
Tejun Heoa43cb952013-04-30 15:27:17 -0700943 show_regs_print_info(KERN_DEFAULT);
944
Paul Mackerras06d67d52005-10-10 22:29:05 +1000945 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
946 regs->nip, regs->link, regs->ctr);
947 printk("REGS: %p TRAP: %04lx %s (%s)\n",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700948 regs, regs->trap, print_tainted(), init_utsname()->release);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000949 printk("MSR: "REG" ", regs->msr);
950 printbits(regs->msr, msr_bits);
anton@samba.orgf6f7dde2007-03-20 20:38:19 -0500951 printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000952 trap = TRAP(regs);
Michael Neuling5115a022011-07-14 19:25:12 +0000953 if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
Anton Blanchard9db8bcf2013-11-15 15:48:38 +1100954 printk("CFAR: "REG" ", regs->orig_gpr3);
Anton Blanchardc5400642013-11-15 15:41:19 +1100955 if (trap == 0x200 || trap == 0x300 || trap == 0x600)
Kumar Galaba28c9a2011-10-06 02:53:38 +0000956#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
Anton Blanchard9db8bcf2013-11-15 15:48:38 +1100957 printk("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
Kumar Gala14170782007-07-26 00:46:15 -0500958#else
Anton Blanchard9db8bcf2013-11-15 15:48:38 +1100959 printk("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
960#endif
961#ifdef CONFIG_PPC64
962 printk("SOFTE: %ld ", regs->softe);
963#endif
964#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Anton Blanchard6d888d12013-11-18 13:19:17 +1100965 if (MSR_TM_ACTIVE(regs->msr))
966 printk("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
Kumar Gala14170782007-07-26 00:46:15 -0500967#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000968
969 for (i = 0; i < 32; i++) {
Paul Mackerras06d67d52005-10-10 22:29:05 +1000970 if ((i % REGS_PER_LINE) == 0)
Kumar Galaa2367192009-06-18 22:29:55 +0000971 printk("\nGPR%02d: ", i);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000972 printk(REG " ", regs->gpr[i]);
973 if (i == LAST_VOLATILE && !FULL_REGS(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000974 break;
975 }
976 printk("\n");
977#ifdef CONFIG_KALLSYMS
978 /*
979 * Lookup NIP late so we have the best change of getting the
980 * above info out without failing
981 */
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +1000982 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
983 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000984#endif
985 show_stack(current, (unsigned long *) regs->gpr[1]);
Paul Mackerras06d67d52005-10-10 22:29:05 +1000986 if (!user_mode(regs))
987 show_instructions(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000988}
989
990void exit_thread(void)
991{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000992}
993
994void flush_thread(void)
995{
K.Prasade0780b72011-02-10 04:44:35 +0000996#ifdef CONFIG_HAVE_HW_BREAKPOINT
K.Prasad5aae8a52010-06-15 11:35:19 +0530997 flush_ptrace_hw_breakpoint(current);
K.Prasade0780b72011-02-10 04:44:35 +0000998#else /* CONFIG_HAVE_HW_BREAKPOINT */
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000999 set_debug_reg_defaults(&current->thread);
K.Prasade0780b72011-02-10 04:44:35 +00001000#endif /* CONFIG_HAVE_HW_BREAKPOINT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001001}
1002
1003void
1004release_thread(struct task_struct *t)
1005{
1006}
1007
1008/*
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001009 * this gets called so that we can store coprocessor state into memory and
1010 * copy the current task into the new thread.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001011 */
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001012int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001013{
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001014 flush_fp_to_thread(src);
1015 flush_altivec_to_thread(src);
1016 flush_vsx_to_thread(src);
1017 flush_spe_to_thread(src);
Michael Neuling621b5062014-03-03 14:21:40 +11001018 /*
1019 * Flush TM state out so we can copy it. __switch_to_tm() does this
1020 * flush but it removes the checkpointed state from the current CPU and
1021 * transitions the CPU out of TM mode. Hence we need to call
1022 * tm_recheckpoint_new_task() (on the same task) to restore the
1023 * checkpointed state back and the TM mode.
1024 */
1025 __switch_to_tm(src);
1026 tm_recheckpoint_new_task(src);
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001027
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001028 *dst = *src;
Michael Ellerman330a1eb2013-06-28 18:15:16 +10001029
1030 clear_task_ebb(dst);
1031
Suresh Siddha55ccf3f2012-05-16 15:03:51 -07001032 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001033}
1034
Michael Ellermancec15482014-07-10 12:29:21 +10001035static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1036{
1037#ifdef CONFIG_PPC_STD_MMU_64
1038 unsigned long sp_vsid;
1039 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1040
1041 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1042 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1043 << SLB_VSID_SHIFT_1T;
1044 else
1045 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1046 << SLB_VSID_SHIFT;
1047 sp_vsid |= SLB_VSID_KERNEL | llp;
1048 p->thread.ksp_vsid = sp_vsid;
1049#endif
1050}
1051
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001052/*
1053 * Copy a thread..
1054 */
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001055
Alex Dowad6eca8932015-03-13 20:14:46 +02001056/*
1057 * Copy architecture-specific thread state
1058 */
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -07001059int copy_thread(unsigned long clone_flags, unsigned long usp,
Alex Dowad6eca8932015-03-13 20:14:46 +02001060 unsigned long kthread_arg, struct task_struct *p)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001061{
1062 struct pt_regs *childregs, *kregs;
1063 extern void ret_from_fork(void);
Al Viro58254e12012-09-12 18:32:42 -04001064 extern void ret_from_kernel_thread(void);
1065 void (*f)(void);
Al Viro0cec6fd2006-01-12 01:06:02 -08001066 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001067
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001068 /* Copy registers */
1069 sp -= sizeof(struct pt_regs);
1070 childregs = (struct pt_regs *) sp;
Al Viroab758192012-10-21 22:33:39 -04001071 if (unlikely(p->flags & PF_KTHREAD)) {
Alex Dowad6eca8932015-03-13 20:14:46 +02001072 /* kernel thread */
Al Viro138d1ce2012-10-11 08:41:43 -04001073 struct thread_info *ti = (void *)task_stack_page(p);
Al Viro58254e12012-09-12 18:32:42 -04001074 memset(childregs, 0, sizeof(struct pt_regs));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001075 childregs->gpr[1] = sp + sizeof(struct pt_regs);
Anton Blanchard7cedd602014-02-04 16:08:51 +11001076 /* function */
1077 if (usp)
1078 childregs->gpr[14] = ppc_function_entry((void *)usp);
Al Viro58254e12012-09-12 18:32:42 -04001079#ifdef CONFIG_PPC64
Al Virob5e2fc12006-01-12 01:06:01 -08001080 clear_tsk_thread_flag(p, TIF_32BIT);
Al Viro138d1ce2012-10-11 08:41:43 -04001081 childregs->softe = 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001082#endif
Alex Dowad6eca8932015-03-13 20:14:46 +02001083 childregs->gpr[15] = kthread_arg;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001084 p->thread.regs = NULL; /* no user register state */
Al Viro138d1ce2012-10-11 08:41:43 -04001085 ti->flags |= _TIF_RESTOREALL;
Al Viro58254e12012-09-12 18:32:42 -04001086 f = ret_from_kernel_thread;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001087 } else {
Alex Dowad6eca8932015-03-13 20:14:46 +02001088 /* user thread */
Al Viroafa86fc2012-10-22 22:51:14 -04001089 struct pt_regs *regs = current_pt_regs();
Al Viro58254e12012-09-12 18:32:42 -04001090 CHECK_FULL_REGS(regs);
1091 *childregs = *regs;
Al Viroea516b12012-10-21 22:28:43 -04001092 if (usp)
1093 childregs->gpr[1] = usp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001094 p->thread.regs = childregs;
Al Viro58254e12012-09-12 18:32:42 -04001095 childregs->gpr[3] = 0; /* Result from fork() */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001096 if (clone_flags & CLONE_SETTLS) {
1097#ifdef CONFIG_PPC64
Denis Kirjanov9904b002010-07-29 22:04:39 +00001098 if (!is_32bit_task())
Paul Mackerras06d67d52005-10-10 22:29:05 +10001099 childregs->gpr[13] = childregs->gpr[6];
1100 else
1101#endif
1102 childregs->gpr[2] = childregs->gpr[6];
1103 }
Al Viro58254e12012-09-12 18:32:42 -04001104
1105 f = ret_from_fork;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001106 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001107 sp -= STACK_FRAME_OVERHEAD;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001108
1109 /*
1110 * The way this works is that at some point in the future
1111 * some task will call _switch to switch to the new task.
1112 * That will pop off the stack frame created below and start
1113 * the new task running at ret_from_fork. The new task will
1114 * do some house keeping and then return from the fork or clone
1115 * system call, using the stack frame created above.
1116 */
Li Zhongaf945cf2013-05-06 22:44:41 +00001117 ((unsigned long *)sp)[0] = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001118 sp -= sizeof(struct pt_regs);
1119 kregs = (struct pt_regs *) sp;
1120 sp -= STACK_FRAME_OVERHEAD;
1121 p->thread.ksp = sp;
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001122#ifdef CONFIG_PPC32
Kumar Gala85218822008-04-28 16:21:22 +10001123 p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
1124 _ALIGN_UP(sizeof(struct thread_info), 16);
Benjamin Herrenschmidtcbc95652013-09-24 15:17:21 +10001125#endif
Oleg Nesterov28d170ab2013-04-21 06:47:59 +00001126#ifdef CONFIG_HAVE_HW_BREAKPOINT
1127 p->thread.ptrace_bps[0] = NULL;
1128#endif
1129
Paul Mackerras18461962013-09-10 20:21:10 +10001130 p->thread.fp_save_area = NULL;
1131#ifdef CONFIG_ALTIVEC
1132 p->thread.vr_save_area = NULL;
1133#endif
1134
Michael Ellermancec15482014-07-10 12:29:21 +10001135 setup_ksp_vsid(p, sp);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001136
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001137#ifdef CONFIG_PPC64
1138 if (cpu_has_feature(CPU_FTR_DSCR)) {
Anton Blanchard1021cb22012-09-03 16:49:47 +00001139 p->thread.dscr_inherit = current->thread.dscr_inherit;
1140 p->thread.dscr = current->thread.dscr;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001141 }
Haren Myneni92779242012-12-06 21:49:56 +00001142 if (cpu_has_feature(CPU_FTR_HAS_PPR))
1143 p->thread.ppr = INIT_PPR;
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001144#endif
Anton Blanchard7cedd602014-02-04 16:08:51 +11001145 kregs->nip = ppc_function_entry(f);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001146 return 0;
1147}
1148
1149/*
1150 * Set up a thread for executing a new program
1151 */
Paul Mackerras06d67d52005-10-10 22:29:05 +10001152void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001153{
Michael Ellerman90eac722005-10-21 16:01:33 +10001154#ifdef CONFIG_PPC64
1155 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
1156#endif
1157
Paul Mackerras06d67d52005-10-10 22:29:05 +10001158 /*
1159 * If we exec out of a kernel thread then thread.regs will not be
1160 * set. Do it now.
1161 */
1162 if (!current->thread.regs) {
Al Viro0cec6fd2006-01-12 01:06:02 -08001163 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1164 current->thread.regs = regs - 1;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001165 }
1166
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001167 memset(regs->gpr, 0, sizeof(regs->gpr));
1168 regs->ctr = 0;
1169 regs->link = 0;
1170 regs->xer = 0;
1171 regs->ccr = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001172 regs->gpr[1] = sp;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001173
Roland McGrath474f8192007-09-24 16:52:44 -07001174 /*
1175 * We have just cleared all the nonvolatile GPRs, so make
1176 * FULL_REGS(regs) return true. This is necessary to allow
1177 * ptrace to examine the thread immediately after exec.
1178 */
1179 regs->trap &= ~1UL;
1180
Paul Mackerras06d67d52005-10-10 22:29:05 +10001181#ifdef CONFIG_PPC32
1182 regs->mq = 0;
1183 regs->nip = start;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001184 regs->msr = MSR_USER;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001185#else
Denis Kirjanov9904b002010-07-29 22:04:39 +00001186 if (!is_32bit_task()) {
Rusty Russell94af3ab2013-11-20 22:15:02 +11001187 unsigned long entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001188
Rusty Russell94af3ab2013-11-20 22:15:02 +11001189 if (is_elf2_task()) {
1190 /* Look ma, no function descriptors! */
1191 entry = start;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001192
Rusty Russell94af3ab2013-11-20 22:15:02 +11001193 /*
1194 * Ulrich says:
1195 * The latest iteration of the ABI requires that when
1196 * calling a function (at its global entry point),
1197 * the caller must ensure r12 holds the entry point
1198 * address (so that the function can quickly
1199 * establish addressability).
1200 */
1201 regs->gpr[12] = start;
1202 /* Make sure that's restored on entry to userspace. */
1203 set_thread_flag(TIF_RESTOREALL);
1204 } else {
1205 unsigned long toc;
1206
1207 /* start is a relocated pointer to the function
1208 * descriptor for the elf _start routine. The first
1209 * entry in the function descriptor is the entry
1210 * address of _start and the second entry is the TOC
1211 * value we need to use.
1212 */
1213 __get_user(entry, (unsigned long __user *)start);
1214 __get_user(toc, (unsigned long __user *)start+1);
1215
1216 /* Check whether the e_entry function descriptor entries
1217 * need to be relocated before we can use them.
1218 */
1219 if (load_addr != 0) {
1220 entry += load_addr;
1221 toc += load_addr;
1222 }
1223 regs->gpr[2] = toc;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001224 }
1225 regs->nip = entry;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001226 regs->msr = MSR_USER64;
Stephen Rothwelld4bf9a72005-10-13 13:40:54 +10001227 } else {
1228 regs->nip = start;
1229 regs->gpr[2] = 0;
1230 regs->msr = MSR_USER32;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001231 }
1232#endif
Michael Neulingce48b212008-06-25 14:07:18 +10001233#ifdef CONFIG_VSX
1234 current->thread.used_vsr = 0;
1235#endif
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001236 memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
Paul Mackerras18461962013-09-10 20:21:10 +10001237 current->thread.fp_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001238#ifdef CONFIG_ALTIVEC
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001239 memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1240 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
Paul Mackerras18461962013-09-10 20:21:10 +10001241 current->thread.vr_save_area = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001242 current->thread.vrsave = 0;
1243 current->thread.used_vr = 0;
1244#endif /* CONFIG_ALTIVEC */
1245#ifdef CONFIG_SPE
1246 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1247 current->thread.acc = 0;
1248 current->thread.spefscr = 0;
1249 current->thread.used_spe = 0;
1250#endif /* CONFIG_SPE */
Michael Neulingbc2a9402013-02-13 16:21:40 +00001251#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1252 if (cpu_has_feature(CPU_FTR_TM))
1253 regs->msr |= MSR_TM;
1254 current->thread.tm_tfhar = 0;
1255 current->thread.tm_texasr = 0;
1256 current->thread.tm_tfiar = 0;
1257#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001258}
Anton Blancharde1802b02014-08-20 08:00:02 +10001259EXPORT_SYMBOL(start_thread);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001260
1261#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1262 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1263
1264int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1265{
1266 struct pt_regs *regs = tsk->thread.regs;
1267
1268 /* This is a bit hairy. If we are an SPE enabled processor
1269 * (have embedded fp) we store the IEEE exception enable flags in
1270 * fpexc_mode. fpexc_mode is also used for setting FP exception
1271 * mode (asyn, precise, disabled) for 'Classic' FP. */
1272 if (val & PR_FP_EXC_SW_ENABLE) {
1273#ifdef CONFIG_SPE
Kumar Gala5e14d212007-09-13 01:44:20 -05001274 if (cpu_has_feature(CPU_FTR_SPE)) {
Joseph Myers640e9222013-12-10 23:07:45 +00001275 /*
1276 * When the sticky exception bits are set
1277 * directly by userspace, it must call prctl
1278 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1279 * in the existing prctl settings) or
1280 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1281 * the bits being set). <fenv.h> functions
1282 * saving and restoring the whole
1283 * floating-point environment need to do so
1284 * anyway to restore the prctl settings from
1285 * the saved environment.
1286 */
1287 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001288 tsk->thread.fpexc_mode = val &
1289 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1290 return 0;
1291 } else {
1292 return -EINVAL;
1293 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001294#else
1295 return -EINVAL;
1296#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001297 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001298
1299 /* on a CONFIG_SPE this does not hurt us. The bits that
1300 * __pack_fe01 use do not overlap with bits used for
1301 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1302 * on CONFIG_SPE implementations are reserved so writing to
1303 * them does not change anything */
1304 if (val > PR_FP_EXC_PRECISE)
1305 return -EINVAL;
1306 tsk->thread.fpexc_mode = __pack_fe01(val);
1307 if (regs != NULL && (regs->msr & MSR_FP) != 0)
1308 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1309 | tsk->thread.fpexc_mode;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001310 return 0;
1311}
1312
1313int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1314{
1315 unsigned int val;
1316
1317 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
1318#ifdef CONFIG_SPE
Joseph Myers640e9222013-12-10 23:07:45 +00001319 if (cpu_has_feature(CPU_FTR_SPE)) {
1320 /*
1321 * When the sticky exception bits are set
1322 * directly by userspace, it must call prctl
1323 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1324 * in the existing prctl settings) or
1325 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1326 * the bits being set). <fenv.h> functions
1327 * saving and restoring the whole
1328 * floating-point environment need to do so
1329 * anyway to restore the prctl settings from
1330 * the saved environment.
1331 */
1332 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
Kumar Gala5e14d212007-09-13 01:44:20 -05001333 val = tsk->thread.fpexc_mode;
Joseph Myers640e9222013-12-10 23:07:45 +00001334 } else
Kumar Gala5e14d212007-09-13 01:44:20 -05001335 return -EINVAL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001336#else
1337 return -EINVAL;
1338#endif
1339 else
1340 val = __unpack_fe01(tsk->thread.fpexc_mode);
1341 return put_user(val, (unsigned int __user *) adr);
1342}
1343
Paul Mackerrasfab5db92006-06-07 16:14:40 +10001344int set_endian(struct task_struct *tsk, unsigned int val)
1345{
1346 struct pt_regs *regs = tsk->thread.regs;
1347
1348 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1349 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1350 return -EINVAL;
1351
1352 if (regs == NULL)
1353 return -EINVAL;
1354
1355 if (val == PR_ENDIAN_BIG)
1356 regs->msr &= ~MSR_LE;
1357 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1358 regs->msr |= MSR_LE;
1359 else
1360 return -EINVAL;
1361
1362 return 0;
1363}
1364
1365int get_endian(struct task_struct *tsk, unsigned long adr)
1366{
1367 struct pt_regs *regs = tsk->thread.regs;
1368 unsigned int val;
1369
1370 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1371 !cpu_has_feature(CPU_FTR_REAL_LE))
1372 return -EINVAL;
1373
1374 if (regs == NULL)
1375 return -EINVAL;
1376
1377 if (regs->msr & MSR_LE) {
1378 if (cpu_has_feature(CPU_FTR_REAL_LE))
1379 val = PR_ENDIAN_LITTLE;
1380 else
1381 val = PR_ENDIAN_PPC_LITTLE;
1382 } else
1383 val = PR_ENDIAN_BIG;
1384
1385 return put_user(val, (unsigned int __user *)adr);
1386}
1387
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001388int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
1389{
1390 tsk->thread.align_ctl = val;
1391 return 0;
1392}
1393
1394int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
1395{
1396 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
1397}
1398
Paul Mackerrasbb72c482007-02-19 11:42:42 +11001399static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
1400 unsigned long nbytes)
1401{
1402 unsigned long stack_page;
1403 unsigned long cpu = task_cpu(p);
1404
1405 /*
1406 * Avoid crashing if the stack has overflowed and corrupted
1407 * task_cpu(p), which is in the thread_info struct.
1408 */
1409 if (cpu < NR_CPUS && cpu_possible(cpu)) {
1410 stack_page = (unsigned long) hardirq_ctx[cpu];
1411 if (sp >= stack_page + sizeof(struct thread_struct)
1412 && sp <= stack_page + THREAD_SIZE - nbytes)
1413 return 1;
1414
1415 stack_page = (unsigned long) softirq_ctx[cpu];
1416 if (sp >= stack_page + sizeof(struct thread_struct)
1417 && sp <= stack_page + THREAD_SIZE - nbytes)
1418 return 1;
1419 }
1420 return 0;
1421}
1422
Anton Blanchard2f251942006-03-27 11:46:18 +11001423int validate_sp(unsigned long sp, struct task_struct *p,
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001424 unsigned long nbytes)
1425{
Al Viro0cec6fd2006-01-12 01:06:02 -08001426 unsigned long stack_page = (unsigned long)task_stack_page(p);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001427
1428 if (sp >= stack_page + sizeof(struct thread_struct)
1429 && sp <= stack_page + THREAD_SIZE - nbytes)
1430 return 1;
1431
Paul Mackerrasbb72c482007-02-19 11:42:42 +11001432 return valid_irq_stack(sp, p, nbytes);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001433}
1434
Anton Blanchard2f251942006-03-27 11:46:18 +11001435EXPORT_SYMBOL(validate_sp);
1436
Paul Mackerras06d67d52005-10-10 22:29:05 +10001437unsigned long get_wchan(struct task_struct *p)
1438{
1439 unsigned long ip, sp;
1440 int count = 0;
1441
1442 if (!p || p == current || p->state == TASK_RUNNING)
1443 return 0;
1444
1445 sp = p->thread.ksp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001446 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001447 return 0;
1448
1449 do {
1450 sp = *(unsigned long *)sp;
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001451 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001452 return 0;
1453 if (count > 0) {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001454 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10001455 if (!in_sched_functions(ip))
1456 return ip;
1457 }
1458 } while (count++ < 16);
1459 return 0;
1460}
Paul Mackerras06d67d52005-10-10 22:29:05 +10001461
Johannes Bergc4d04be2008-11-20 03:24:07 +00001462static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001463
1464void show_stack(struct task_struct *tsk, unsigned long *stack)
1465{
Paul Mackerras06d67d52005-10-10 22:29:05 +10001466 unsigned long sp, ip, lr, newsp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001467 int count = 0;
Paul Mackerras06d67d52005-10-10 22:29:05 +10001468 int firstframe = 1;
Steven Rostedt6794c782009-02-09 21:10:27 -08001469#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1470 int curr_frame = current->curr_ret_stack;
1471 extern void return_to_handler(void);
Steven Rostedt9135c3c2009-09-15 08:20:15 -07001472 unsigned long rth = (unsigned long)return_to_handler;
Steven Rostedt6794c782009-02-09 21:10:27 -08001473#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001474
1475 sp = (unsigned long) stack;
1476 if (tsk == NULL)
1477 tsk = current;
1478 if (sp == 0) {
1479 if (tsk == current)
Anton Blanchardacf620e2014-10-13 19:41:39 +11001480 sp = current_stack_pointer();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001481 else
1482 sp = tsk->thread.ksp;
1483 }
1484
Paul Mackerras06d67d52005-10-10 22:29:05 +10001485 lr = 0;
1486 printk("Call Trace:\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001487 do {
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001488 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
Paul Mackerras06d67d52005-10-10 22:29:05 +10001489 return;
1490
1491 stack = (unsigned long *) sp;
1492 newsp = stack[0];
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001493 ip = stack[STACK_FRAME_LR_SAVE];
Paul Mackerras06d67d52005-10-10 22:29:05 +10001494 if (!firstframe || ip != lr) {
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001495 printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
Steven Rostedt6794c782009-02-09 21:10:27 -08001496#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Anton Blanchard7d56c652014-09-17 17:07:03 +10001497 if ((ip == rth) && curr_frame >= 0) {
Steven Rostedt6794c782009-02-09 21:10:27 -08001498 printk(" (%pS)",
1499 (void *)current->ret_stack[curr_frame].ret);
1500 curr_frame--;
1501 }
1502#endif
Paul Mackerras06d67d52005-10-10 22:29:05 +10001503 if (firstframe)
1504 printk(" (unreliable)");
1505 printk("\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001506 }
Paul Mackerras06d67d52005-10-10 22:29:05 +10001507 firstframe = 0;
1508
1509 /*
1510 * See if this is an exception frame.
1511 * We look for the "regshere" marker in the current frame.
1512 */
Benjamin Herrenschmidtec2b36b2008-04-17 14:34:59 +10001513 if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
1514 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
Paul Mackerras06d67d52005-10-10 22:29:05 +10001515 struct pt_regs *regs = (struct pt_regs *)
1516 (sp + STACK_FRAME_OVERHEAD);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001517 lr = regs->link;
Paul Mackerras9be9be22014-06-12 16:53:08 +10001518 printk("--- interrupt: %lx at %pS\n LR = %pS\n",
Benjamin Herrenschmidt058c78f2008-07-07 13:44:31 +10001519 regs->trap, (void *)regs->nip, (void *)lr);
Paul Mackerras06d67d52005-10-10 22:29:05 +10001520 firstframe = 1;
1521 }
1522
1523 sp = newsp;
1524 } while (count++ < kstack_depth_to_print);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001525}
Paul Mackerras06d67d52005-10-10 22:29:05 +10001526
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001527#ifdef CONFIG_PPC64
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001528/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10001529void notrace __ppc64_runlatch_on(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001530{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001531 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001532 unsigned long ctrl;
1533
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001534 ctrl = mfspr(SPRN_CTRLF);
1535 ctrl |= CTRL_RUNLATCH;
1536 mtspr(SPRN_CTRLT, ctrl);
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001537
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10001538 ti->local_flags |= _TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001539}
1540
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001541/* Called with hard IRQs off */
Michael Ellerman0e377392013-06-13 21:04:56 +10001542void notrace __ppc64_runlatch_off(void)
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001543{
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001544 struct thread_info *ti = current_thread_info();
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001545 unsigned long ctrl;
1546
Benjamin Herrenschmidtfae2e0f2012-04-11 10:42:15 +10001547 ti->local_flags &= ~_TLF_RUNLATCH;
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001548
Anton Blanchard4138d652010-08-06 03:28:19 +00001549 ctrl = mfspr(SPRN_CTRLF);
1550 ctrl &= ~CTRL_RUNLATCH;
1551 mtspr(SPRN_CTRLT, ctrl);
Anton Blanchardcb2c9b22006-02-13 14:48:35 +11001552}
Benjamin Herrenschmidtfe1952f2012-03-01 12:45:27 +11001553#endif /* CONFIG_PPC64 */
Benjamin Herrenschmidtf6a61682008-04-18 16:56:17 +10001554
Anton Blanchardd8390882009-02-22 01:50:03 +00001555unsigned long arch_align_stack(unsigned long sp)
1556{
1557 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
1558 sp -= get_random_int() & ~PAGE_MASK;
1559 return sp & ~0xf;
1560}
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001561
1562static inline unsigned long brk_rnd(void)
1563{
1564 unsigned long rnd = 0;
1565
1566 /* 8MB for 32bit, 1GB for 64bit */
1567 if (is_32bit_task())
1568 rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
1569 else
1570 rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
1571
1572 return rnd << PAGE_SHIFT;
1573}
1574
1575unsigned long arch_randomize_brk(struct mm_struct *mm)
1576{
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001577 unsigned long base = mm->brk;
1578 unsigned long ret;
1579
Kumar Galace7a35c2009-10-16 07:05:17 +00001580#ifdef CONFIG_PPC_STD_MMU_64
Anton Blanchard8bbde7a2009-09-21 16:52:35 +00001581 /*
1582 * If we are using 1TB segments and we are allowed to randomise
1583 * the heap, we can put it above 1TB so it is backed by a 1TB
1584 * segment. Otherwise the heap will be in the bottom 1TB
1585 * which always uses 256MB segments and this may result in a
1586 * performance penalty.
1587 */
1588 if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
1589 base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
1590#endif
1591
1592 ret = PAGE_ALIGN(base + brk_rnd());
Anton Blanchard912f9ee2009-02-22 01:50:04 +00001593
1594 if (ret < mm->brk)
1595 return mm->brk;
1596
1597 return ret;
1598}
Anton Blanchard501cb162009-02-22 01:50:07 +00001599