]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/powerpc/kernel/process.c
powerpc: Add the ability to save Altivec without giving it up
[mirror_ubuntu-zesty-kernel.git] / arch / powerpc / kernel / process.c
CommitLineData
14cf11af 1/*
14cf11af
PM
2 * 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
14cf11af
PM
17#include <linux/errno.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/smp.h>
14cf11af
PM
22#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>
14cf11af
PM
28#include <linux/prctl.h>
29#include <linux/init_task.h>
4b16f8e2 30#include <linux/export.h>
14cf11af
PM
31#include <linux/kallsyms.h>
32#include <linux/mqueue.h>
33#include <linux/hardirq.h>
06d67d54 34#include <linux/utsname.h>
6794c782 35#include <linux/ftrace.h>
79741dd3 36#include <linux/kernel_stat.h>
d839088c
AB
37#include <linux/personality.h>
38#include <linux/random.h>
5aae8a53 39#include <linux/hw_breakpoint.h>
7b051f66 40#include <linux/uaccess.h>
14cf11af
PM
41
42#include <asm/pgtable.h>
14cf11af
PM
43#include <asm/io.h>
44#include <asm/processor.h>
45#include <asm/mmu.h>
46#include <asm/prom.h>
76032de8 47#include <asm/machdep.h>
c6622f63 48#include <asm/time.h>
ae3a197e 49#include <asm/runlatch.h>
a7f31841 50#include <asm/syscalls.h>
ae3a197e 51#include <asm/switch_to.h>
fb09692e 52#include <asm/tm.h>
ae3a197e 53#include <asm/debug.h>
06d67d54
PM
54#ifdef CONFIG_PPC64
55#include <asm/firmware.h>
06d67d54 56#endif
7cedd601 57#include <asm/code-patching.h>
d6a61bfc
LM
58#include <linux/kprobes.h>
59#include <linux/kdebug.h>
14cf11af 60
8b3c34cf
MN
61/* 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
14cf11af
PM
68extern unsigned long _get_SP(void);
69
d31626f7 70#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
b86fd2bd 71static void check_if_tm_restore_required(struct task_struct *tsk)
d31626f7
PM
72{
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)) {
829023df 82 tsk->thread.ckpt_regs.msr = tsk->thread.regs->msr;
d31626f7
PM
83 set_thread_flag(TIF_RESTORE_TM);
84 }
d31626f7 85}
d31626f7 86#else
b86fd2bd 87static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
d31626f7
PM
88#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
89
3eb5d588
AB
90bool strict_msr_control;
91EXPORT_SYMBOL(strict_msr_control);
92
93static int __init enable_strict_msr_control(char *str)
94{
95 strict_msr_control = true;
96 pr_info("Enabling strict facility control\n");
97
98 return 0;
99}
100early_param("ppc_strict_facility_enable", enable_strict_msr_control);
101
102void msr_check_and_set(unsigned long bits)
98da581e 103{
a0e72cf1
AB
104 unsigned long oldmsr = mfmsr();
105 unsigned long newmsr;
98da581e 106
a0e72cf1 107 newmsr = oldmsr | bits;
98da581e 108
98da581e 109#ifdef CONFIG_VSX
a0e72cf1 110 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
98da581e
AB
111 newmsr |= MSR_VSX;
112#endif
a0e72cf1 113
98da581e
AB
114 if (oldmsr != newmsr)
115 mtmsr_isync(newmsr);
a0e72cf1 116}
98da581e 117
3eb5d588 118void __msr_check_and_clear(unsigned long bits)
a0e72cf1
AB
119{
120 unsigned long oldmsr = mfmsr();
121 unsigned long newmsr;
122
123 newmsr = oldmsr & ~bits;
124
125#ifdef CONFIG_VSX
126 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
127 newmsr &= ~MSR_VSX;
128#endif
129
130 if (oldmsr != newmsr)
131 mtmsr_isync(newmsr);
132}
3eb5d588 133EXPORT_SYMBOL(__msr_check_and_clear);
a0e72cf1
AB
134
135#ifdef CONFIG_PPC_FPU
8792468d
CB
136void __giveup_fpu(struct task_struct *tsk)
137{
138 save_fpu(tsk);
139 tsk->thread.regs->msr &= ~MSR_FP;
140#ifdef CONFIG_VSX
141 if (cpu_has_feature(CPU_FTR_VSX))
142 tsk->thread.regs->msr &= ~MSR_VSX;
143#endif
144}
145
a0e72cf1
AB
146void giveup_fpu(struct task_struct *tsk)
147{
148 check_if_tm_restore_required(tsk);
149
150 msr_check_and_set(MSR_FP);
98da581e 151 __giveup_fpu(tsk);
a0e72cf1 152 msr_check_and_clear(MSR_FP);
98da581e
AB
153}
154EXPORT_SYMBOL(giveup_fpu);
155
14cf11af
PM
156/*
157 * Make sure the floating-point register state in the
158 * the thread_struct is up to date for task tsk.
159 */
160void flush_fp_to_thread(struct task_struct *tsk)
161{
162 if (tsk->thread.regs) {
163 /*
164 * We need to disable preemption here because if we didn't,
165 * another process could get scheduled after the regs->msr
166 * test but before we have finished saving the FP registers
167 * to the thread_struct. That process could take over the
168 * FPU, and then when we get scheduled again we would store
169 * bogus values for the remaining FP registers.
170 */
171 preempt_disable();
172 if (tsk->thread.regs->msr & MSR_FP) {
14cf11af
PM
173 /*
174 * This should only ever be called for current or
175 * for a stopped child process. Since we save away
af1bbc3d 176 * the FP register state on context switch,
14cf11af
PM
177 * there is something wrong if a stopped child appears
178 * to still have its FP state in the CPU registers.
179 */
180 BUG_ON(tsk != current);
b86fd2bd 181 giveup_fpu(tsk);
14cf11af
PM
182 }
183 preempt_enable();
184 }
185}
de56a948 186EXPORT_SYMBOL_GPL(flush_fp_to_thread);
14cf11af
PM
187
188void enable_kernel_fp(void)
189{
190 WARN_ON(preemptible());
191
a0e72cf1 192 msr_check_and_set(MSR_FP);
611b0e5c 193
d64d02ce
AB
194 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
195 check_if_tm_restore_required(current);
a0e72cf1 196 __giveup_fpu(current);
d64d02ce 197 }
14cf11af
PM
198}
199EXPORT_SYMBOL(enable_kernel_fp);
70fe3d98
CB
200
201static int restore_fp(struct task_struct *tsk) {
202 if (tsk->thread.load_fp) {
203 load_fp_state(&current->thread.fp_state);
204 current->thread.load_fp++;
205 return 1;
206 }
207 return 0;
208}
209#else
210static int restore_fp(struct task_struct *tsk) { return 0; }
d1e1cf2e 211#endif /* CONFIG_PPC_FPU */
14cf11af 212
14cf11af 213#ifdef CONFIG_ALTIVEC
70fe3d98
CB
214#define loadvec(thr) ((thr).load_vec)
215
6f515d84
CB
216static void __giveup_altivec(struct task_struct *tsk)
217{
218 save_altivec(tsk);
219 tsk->thread.regs->msr &= ~MSR_VEC;
220#ifdef CONFIG_VSX
221 if (cpu_has_feature(CPU_FTR_VSX))
222 tsk->thread.regs->msr &= ~MSR_VSX;
223#endif
224}
225
98da581e
AB
226void giveup_altivec(struct task_struct *tsk)
227{
98da581e
AB
228 check_if_tm_restore_required(tsk);
229
a0e72cf1 230 msr_check_and_set(MSR_VEC);
98da581e 231 __giveup_altivec(tsk);
a0e72cf1 232 msr_check_and_clear(MSR_VEC);
98da581e
AB
233}
234EXPORT_SYMBOL(giveup_altivec);
235
14cf11af
PM
236void enable_kernel_altivec(void)
237{
238 WARN_ON(preemptible());
239
a0e72cf1 240 msr_check_and_set(MSR_VEC);
611b0e5c 241
d64d02ce
AB
242 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
243 check_if_tm_restore_required(current);
a0e72cf1 244 __giveup_altivec(current);
d64d02ce 245 }
14cf11af
PM
246}
247EXPORT_SYMBOL(enable_kernel_altivec);
248
249/*
250 * Make sure the VMX/Altivec register state in the
251 * the thread_struct is up to date for task tsk.
252 */
253void flush_altivec_to_thread(struct task_struct *tsk)
254{
255 if (tsk->thread.regs) {
256 preempt_disable();
257 if (tsk->thread.regs->msr & MSR_VEC) {
14cf11af 258 BUG_ON(tsk != current);
b86fd2bd 259 giveup_altivec(tsk);
14cf11af
PM
260 }
261 preempt_enable();
262 }
263}
de56a948 264EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
70fe3d98
CB
265
266static int restore_altivec(struct task_struct *tsk)
267{
268 if (cpu_has_feature(CPU_FTR_ALTIVEC) && tsk->thread.load_vec) {
269 load_vr_state(&tsk->thread.vr_state);
270 tsk->thread.used_vr = 1;
271 tsk->thread.load_vec++;
272
273 return 1;
274 }
275 return 0;
276}
277#else
278#define loadvec(thr) 0
279static inline int restore_altivec(struct task_struct *tsk) { return 0; }
14cf11af
PM
280#endif /* CONFIG_ALTIVEC */
281
ce48b210 282#ifdef CONFIG_VSX
a7d623d4
AB
283void giveup_vsx(struct task_struct *tsk)
284{
a7d623d4
AB
285 check_if_tm_restore_required(tsk);
286
a0e72cf1 287 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
a7d623d4
AB
288 if (tsk->thread.regs->msr & MSR_FP)
289 __giveup_fpu(tsk);
290 if (tsk->thread.regs->msr & MSR_VEC)
291 __giveup_altivec(tsk);
292 __giveup_vsx(tsk);
a0e72cf1 293 msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
a7d623d4
AB
294}
295EXPORT_SYMBOL(giveup_vsx);
296
ce48b210
MN
297void enable_kernel_vsx(void)
298{
299 WARN_ON(preemptible());
300
a0e72cf1 301 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
611b0e5c 302
a0e72cf1 303 if (current->thread.regs && (current->thread.regs->msr & MSR_VSX)) {
d64d02ce 304 check_if_tm_restore_required(current);
a0e72cf1
AB
305 if (current->thread.regs->msr & MSR_FP)
306 __giveup_fpu(current);
307 if (current->thread.regs->msr & MSR_VEC)
308 __giveup_altivec(current);
309 __giveup_vsx(current);
611b0e5c 310 }
ce48b210
MN
311}
312EXPORT_SYMBOL(enable_kernel_vsx);
ce48b210
MN
313
314void flush_vsx_to_thread(struct task_struct *tsk)
315{
316 if (tsk->thread.regs) {
317 preempt_disable();
318 if (tsk->thread.regs->msr & MSR_VSX) {
ce48b210 319 BUG_ON(tsk != current);
ce48b210
MN
320 giveup_vsx(tsk);
321 }
322 preempt_enable();
323 }
324}
de56a948 325EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
70fe3d98
CB
326
327static int restore_vsx(struct task_struct *tsk)
328{
329 if (cpu_has_feature(CPU_FTR_VSX)) {
330 tsk->thread.used_vsr = 1;
331 return 1;
332 }
333
334 return 0;
335}
336#else
337static inline int restore_vsx(struct task_struct *tsk) { return 0; }
ce48b210
MN
338#endif /* CONFIG_VSX */
339
14cf11af 340#ifdef CONFIG_SPE
98da581e
AB
341void giveup_spe(struct task_struct *tsk)
342{
98da581e
AB
343 check_if_tm_restore_required(tsk);
344
a0e72cf1 345 msr_check_and_set(MSR_SPE);
98da581e 346 __giveup_spe(tsk);
a0e72cf1 347 msr_check_and_clear(MSR_SPE);
98da581e
AB
348}
349EXPORT_SYMBOL(giveup_spe);
14cf11af
PM
350
351void enable_kernel_spe(void)
352{
353 WARN_ON(preemptible());
354
a0e72cf1 355 msr_check_and_set(MSR_SPE);
611b0e5c 356
d64d02ce
AB
357 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
358 check_if_tm_restore_required(current);
a0e72cf1 359 __giveup_spe(current);
d64d02ce 360 }
14cf11af
PM
361}
362EXPORT_SYMBOL(enable_kernel_spe);
363
364void flush_spe_to_thread(struct task_struct *tsk)
365{
366 if (tsk->thread.regs) {
367 preempt_disable();
368 if (tsk->thread.regs->msr & MSR_SPE) {
14cf11af 369 BUG_ON(tsk != current);
685659ee 370 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
0ee6c15e 371 giveup_spe(tsk);
14cf11af
PM
372 }
373 preempt_enable();
374 }
375}
14cf11af
PM
376#endif /* CONFIG_SPE */
377
c2085059
AB
378static unsigned long msr_all_available;
379
380static int __init init_msr_all_available(void)
381{
382#ifdef CONFIG_PPC_FPU
383 msr_all_available |= MSR_FP;
384#endif
385#ifdef CONFIG_ALTIVEC
386 if (cpu_has_feature(CPU_FTR_ALTIVEC))
387 msr_all_available |= MSR_VEC;
388#endif
389#ifdef CONFIG_VSX
390 if (cpu_has_feature(CPU_FTR_VSX))
391 msr_all_available |= MSR_VSX;
392#endif
393#ifdef CONFIG_SPE
394 if (cpu_has_feature(CPU_FTR_SPE))
395 msr_all_available |= MSR_SPE;
396#endif
397
398 return 0;
399}
400early_initcall(init_msr_all_available);
401
402void giveup_all(struct task_struct *tsk)
403{
404 unsigned long usermsr;
405
406 if (!tsk->thread.regs)
407 return;
408
409 usermsr = tsk->thread.regs->msr;
410
411 if ((usermsr & msr_all_available) == 0)
412 return;
413
414 msr_check_and_set(msr_all_available);
415
416#ifdef CONFIG_PPC_FPU
417 if (usermsr & MSR_FP)
418 __giveup_fpu(tsk);
419#endif
420#ifdef CONFIG_ALTIVEC
421 if (usermsr & MSR_VEC)
422 __giveup_altivec(tsk);
423#endif
424#ifdef CONFIG_VSX
425 if (usermsr & MSR_VSX)
426 __giveup_vsx(tsk);
427#endif
428#ifdef CONFIG_SPE
429 if (usermsr & MSR_SPE)
430 __giveup_spe(tsk);
431#endif
432
433 msr_check_and_clear(msr_all_available);
434}
435EXPORT_SYMBOL(giveup_all);
436
70fe3d98
CB
437void restore_math(struct pt_regs *regs)
438{
439 unsigned long msr;
440
441 if (!current->thread.load_fp && !loadvec(current->thread))
442 return;
443
444 msr = regs->msr;
445 msr_check_and_set(msr_all_available);
446
447 /*
448 * Only reload if the bit is not set in the user MSR, the bit BEING set
449 * indicates that the registers are hot
450 */
451 if ((!(msr & MSR_FP)) && restore_fp(current))
452 msr |= MSR_FP | current->thread.fpexc_mode;
453
454 if ((!(msr & MSR_VEC)) && restore_altivec(current))
455 msr |= MSR_VEC;
456
457 if ((msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC) &&
458 restore_vsx(current)) {
459 msr |= MSR_VSX;
460 }
461
462 msr_check_and_clear(msr_all_available);
463
464 regs->msr = msr;
465}
466
de2a20aa
CB
467void save_all(struct task_struct *tsk)
468{
469 unsigned long usermsr;
470
471 if (!tsk->thread.regs)
472 return;
473
474 usermsr = tsk->thread.regs->msr;
475
476 if ((usermsr & msr_all_available) == 0)
477 return;
478
479 msr_check_and_set(msr_all_available);
480
481 if (usermsr & MSR_FP)
8792468d 482 save_fpu(tsk);
de2a20aa
CB
483
484 if (usermsr & MSR_VEC)
6f515d84 485 save_altivec(tsk);
de2a20aa
CB
486
487 if (usermsr & MSR_VSX)
488 __giveup_vsx(tsk);
489
490 if (usermsr & MSR_SPE)
491 __giveup_spe(tsk);
492
493 msr_check_and_clear(msr_all_available);
494}
495
579e633e
AB
496void flush_all_to_thread(struct task_struct *tsk)
497{
498 if (tsk->thread.regs) {
499 preempt_disable();
500 BUG_ON(tsk != current);
de2a20aa 501 save_all(tsk);
579e633e
AB
502
503#ifdef CONFIG_SPE
504 if (tsk->thread.regs->msr & MSR_SPE)
505 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
506#endif
507
508 preempt_enable();
509 }
510}
511EXPORT_SYMBOL(flush_all_to_thread);
512
3bffb652
DK
513#ifdef CONFIG_PPC_ADV_DEBUG_REGS
514void do_send_trap(struct pt_regs *regs, unsigned long address,
515 unsigned long error_code, int signal_code, int breakpt)
516{
517 siginfo_t info;
518
41ab5266 519 current->thread.trap_nr = signal_code;
3bffb652
DK
520 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
521 11, SIGSEGV) == NOTIFY_STOP)
522 return;
523
524 /* Deliver the signal to userspace */
525 info.si_signo = SIGTRAP;
526 info.si_errno = breakpt; /* breakpoint or watchpoint id */
527 info.si_code = signal_code;
528 info.si_addr = (void __user *)address;
529 force_sig_info(SIGTRAP, &info, current);
530}
531#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
9422de3e 532void do_break (struct pt_regs *regs, unsigned long address,
d6a61bfc
LM
533 unsigned long error_code)
534{
535 siginfo_t info;
536
41ab5266 537 current->thread.trap_nr = TRAP_HWBKPT;
d6a61bfc
LM
538 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
539 11, SIGSEGV) == NOTIFY_STOP)
540 return;
541
9422de3e 542 if (debugger_break_match(regs))
d6a61bfc
LM
543 return;
544
9422de3e
MN
545 /* Clear the breakpoint */
546 hw_breakpoint_disable();
d6a61bfc
LM
547
548 /* Deliver the signal to userspace */
549 info.si_signo = SIGTRAP;
550 info.si_errno = 0;
551 info.si_code = TRAP_HWBKPT;
552 info.si_addr = (void __user *)address;
553 force_sig_info(SIGTRAP, &info, current);
554}
3bffb652 555#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
d6a61bfc 556
9422de3e 557static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk);
a2ceff5e 558
3bffb652
DK
559#ifdef CONFIG_PPC_ADV_DEBUG_REGS
560/*
561 * Set the debug registers back to their default "safe" values.
562 */
563static void set_debug_reg_defaults(struct thread_struct *thread)
564{
51ae8d4a 565 thread->debug.iac1 = thread->debug.iac2 = 0;
3bffb652 566#if CONFIG_PPC_ADV_DEBUG_IACS > 2
51ae8d4a 567 thread->debug.iac3 = thread->debug.iac4 = 0;
3bffb652 568#endif
51ae8d4a 569 thread->debug.dac1 = thread->debug.dac2 = 0;
3bffb652 570#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
51ae8d4a 571 thread->debug.dvc1 = thread->debug.dvc2 = 0;
3bffb652 572#endif
51ae8d4a 573 thread->debug.dbcr0 = 0;
3bffb652
DK
574#ifdef CONFIG_BOOKE
575 /*
576 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
577 */
51ae8d4a 578 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
3bffb652
DK
579 DBCR1_IAC3US | DBCR1_IAC4US;
580 /*
581 * Force Data Address Compare User/Supervisor bits to be User-only
582 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
583 */
51ae8d4a 584 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
3bffb652 585#else
51ae8d4a 586 thread->debug.dbcr1 = 0;
3bffb652
DK
587#endif
588}
589
f5f97210 590static void prime_debug_regs(struct debug_reg *debug)
3bffb652 591{
6cecf76b
SW
592 /*
593 * We could have inherited MSR_DE from userspace, since
594 * it doesn't get cleared on exception entry. Make sure
595 * MSR_DE is clear before we enable any debug events.
596 */
597 mtmsr(mfmsr() & ~MSR_DE);
598
f5f97210
SW
599 mtspr(SPRN_IAC1, debug->iac1);
600 mtspr(SPRN_IAC2, debug->iac2);
3bffb652 601#if CONFIG_PPC_ADV_DEBUG_IACS > 2
f5f97210
SW
602 mtspr(SPRN_IAC3, debug->iac3);
603 mtspr(SPRN_IAC4, debug->iac4);
3bffb652 604#endif
f5f97210
SW
605 mtspr(SPRN_DAC1, debug->dac1);
606 mtspr(SPRN_DAC2, debug->dac2);
3bffb652 607#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
f5f97210
SW
608 mtspr(SPRN_DVC1, debug->dvc1);
609 mtspr(SPRN_DVC2, debug->dvc2);
3bffb652 610#endif
f5f97210
SW
611 mtspr(SPRN_DBCR0, debug->dbcr0);
612 mtspr(SPRN_DBCR1, debug->dbcr1);
3bffb652 613#ifdef CONFIG_BOOKE
f5f97210 614 mtspr(SPRN_DBCR2, debug->dbcr2);
3bffb652
DK
615#endif
616}
617/*
618 * Unless neither the old or new thread are making use of the
619 * debug registers, set the debug registers from the values
620 * stored in the new thread.
621 */
f5f97210 622void switch_booke_debug_regs(struct debug_reg *new_debug)
3bffb652 623{
51ae8d4a 624 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
f5f97210
SW
625 || (new_debug->dbcr0 & DBCR0_IDM))
626 prime_debug_regs(new_debug);
3bffb652 627}
3743c9b8 628EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
3bffb652 629#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
e0780b72 630#ifndef CONFIG_HAVE_HW_BREAKPOINT
3bffb652
DK
631static void set_debug_reg_defaults(struct thread_struct *thread)
632{
9422de3e
MN
633 thread->hw_brk.address = 0;
634 thread->hw_brk.type = 0;
b9818c33 635 set_breakpoint(&thread->hw_brk);
3bffb652 636}
e0780b72 637#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
3bffb652
DK
638#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
639
172ae2e7 640#ifdef CONFIG_PPC_ADV_DEBUG_REGS
9422de3e
MN
641static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
642{
d6a61bfc 643 mtspr(SPRN_DAC1, dabr);
221c185d
DK
644#ifdef CONFIG_PPC_47x
645 isync();
646#endif
9422de3e
MN
647 return 0;
648}
c6c9eace 649#elif defined(CONFIG_PPC_BOOK3S)
9422de3e
MN
650static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
651{
c6c9eace 652 mtspr(SPRN_DABR, dabr);
82a9f16a
MN
653 if (cpu_has_feature(CPU_FTR_DABRX))
654 mtspr(SPRN_DABRX, dabrx);
cab0af98 655 return 0;
14cf11af 656}
9422de3e
MN
657#else
658static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
659{
660 return -EINVAL;
661}
662#endif
663
664static inline int set_dabr(struct arch_hw_breakpoint *brk)
665{
666 unsigned long dabr, dabrx;
667
668 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
669 dabrx = ((brk->type >> 3) & 0x7);
670
671 if (ppc_md.set_dabr)
672 return ppc_md.set_dabr(dabr, dabrx);
673
674 return __set_dabr(dabr, dabrx);
675}
676
bf99de36
MN
677static inline int set_dawr(struct arch_hw_breakpoint *brk)
678{
05d694ea 679 unsigned long dawr, dawrx, mrd;
bf99de36
MN
680
681 dawr = brk->address;
682
683 dawrx = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE)) \
684 << (63 - 58); //* read/write bits */
685 dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) \
686 << (63 - 59); //* translate */
687 dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) \
688 >> 3; //* PRIM bits */
05d694ea
MN
689 /* dawr length is stored in field MDR bits 48:53. Matches range in
690 doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
691 0b111111=64DW.
692 brk->len is in bytes.
693 This aligns up to double word size, shifts and does the bias.
694 */
695 mrd = ((brk->len + 7) >> 3) - 1;
696 dawrx |= (mrd & 0x3f) << (63 - 53);
bf99de36
MN
697
698 if (ppc_md.set_dawr)
699 return ppc_md.set_dawr(dawr, dawrx);
700 mtspr(SPRN_DAWR, dawr);
701 mtspr(SPRN_DAWRX, dawrx);
702 return 0;
703}
704
21f58507 705void __set_breakpoint(struct arch_hw_breakpoint *brk)
9422de3e 706{
69111bac 707 memcpy(this_cpu_ptr(&current_brk), brk, sizeof(*brk));
9422de3e 708
bf99de36 709 if (cpu_has_feature(CPU_FTR_DAWR))
04c32a51
PG
710 set_dawr(brk);
711 else
712 set_dabr(brk);
9422de3e 713}
14cf11af 714
21f58507
PG
715void set_breakpoint(struct arch_hw_breakpoint *brk)
716{
717 preempt_disable();
718 __set_breakpoint(brk);
719 preempt_enable();
720}
721
06d67d54
PM
722#ifdef CONFIG_PPC64
723DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
06d67d54 724#endif
14cf11af 725
9422de3e
MN
726static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
727 struct arch_hw_breakpoint *b)
728{
729 if (a->address != b->address)
730 return false;
731 if (a->type != b->type)
732 return false;
733 if (a->len != b->len)
734 return false;
735 return true;
736}
d31626f7 737
fb09692e 738#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
d31626f7
PM
739static void tm_reclaim_thread(struct thread_struct *thr,
740 struct thread_info *ti, uint8_t cause)
741{
742 unsigned long msr_diff = 0;
743
744 /*
745 * If FP/VSX registers have been already saved to the
746 * thread_struct, move them to the transact_fp array.
747 * We clear the TIF_RESTORE_TM bit since after the reclaim
748 * the thread will no longer be transactional.
749 */
750 if (test_ti_thread_flag(ti, TIF_RESTORE_TM)) {
829023df 751 msr_diff = thr->ckpt_regs.msr & ~thr->regs->msr;
d31626f7
PM
752 if (msr_diff & MSR_FP)
753 memcpy(&thr->transact_fp, &thr->fp_state,
754 sizeof(struct thread_fp_state));
755 if (msr_diff & MSR_VEC)
756 memcpy(&thr->transact_vr, &thr->vr_state,
757 sizeof(struct thread_vr_state));
758 clear_ti_thread_flag(ti, TIF_RESTORE_TM);
759 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX | MSR_FE0 | MSR_FE1;
760 }
761
7f821fc9
MN
762 /*
763 * Use the current MSR TM suspended bit to track if we have
764 * checkpointed state outstanding.
765 * On signal delivery, we'd normally reclaim the checkpointed
766 * state to obtain stack pointer (see:get_tm_stackpointer()).
767 * This will then directly return to userspace without going
768 * through __switch_to(). However, if the stack frame is bad,
769 * we need to exit this thread which calls __switch_to() which
770 * will again attempt to reclaim the already saved tm state.
771 * Hence we need to check that we've not already reclaimed
772 * this state.
773 * We do this using the current MSR, rather tracking it in
774 * some specific thread_struct bit, as it has the additional
775 * benifit of checking for a potential TM bad thing exception.
776 */
777 if (!MSR_TM_SUSPENDED(mfmsr()))
778 return;
779
d31626f7
PM
780 tm_reclaim(thr, thr->regs->msr, cause);
781
782 /* Having done the reclaim, we now have the checkpointed
783 * FP/VSX values in the registers. These might be valid
784 * even if we have previously called enable_kernel_fp() or
785 * flush_fp_to_thread(), so update thr->regs->msr to
786 * indicate their current validity.
787 */
788 thr->regs->msr |= msr_diff;
789}
790
791void tm_reclaim_current(uint8_t cause)
792{
793 tm_enable();
794 tm_reclaim_thread(&current->thread, current_thread_info(), cause);
795}
796
fb09692e
MN
797static inline void tm_reclaim_task(struct task_struct *tsk)
798{
799 /* We have to work out if we're switching from/to a task that's in the
800 * middle of a transaction.
801 *
802 * In switching we need to maintain a 2nd register state as
803 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
804 * checkpointed (tbegin) state in ckpt_regs and saves the transactional
805 * (current) FPRs into oldtask->thread.transact_fpr[].
806 *
807 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
808 */
809 struct thread_struct *thr = &tsk->thread;
810
811 if (!thr->regs)
812 return;
813
814 if (!MSR_TM_ACTIVE(thr->regs->msr))
815 goto out_and_saveregs;
816
817 /* Stash the original thread MSR, as giveup_fpu et al will
818 * modify it. We hold onto it to see whether the task used
d31626f7 819 * FP & vector regs. If the TIF_RESTORE_TM flag is set,
829023df 820 * ckpt_regs.msr is already set.
fb09692e 821 */
d31626f7 822 if (!test_ti_thread_flag(task_thread_info(tsk), TIF_RESTORE_TM))
829023df 823 thr->ckpt_regs.msr = thr->regs->msr;
fb09692e
MN
824
825 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
826 "ccr=%lx, msr=%lx, trap=%lx)\n",
827 tsk->pid, thr->regs->nip,
828 thr->regs->ccr, thr->regs->msr,
829 thr->regs->trap);
830
d31626f7 831 tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
fb09692e
MN
832
833 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
834 tsk->pid);
835
836out_and_saveregs:
837 /* Always save the regs here, even if a transaction's not active.
838 * This context-switches a thread's TM info SPRs. We do it here to
839 * be consistent with the restore path (in recheckpoint) which
840 * cannot happen later in _switch().
841 */
842 tm_save_sprs(thr);
843}
844
e6b8fd02
MN
845extern void __tm_recheckpoint(struct thread_struct *thread,
846 unsigned long orig_msr);
847
848void tm_recheckpoint(struct thread_struct *thread,
849 unsigned long orig_msr)
850{
851 unsigned long flags;
852
853 /* We really can't be interrupted here as the TEXASR registers can't
854 * change and later in the trecheckpoint code, we have a userspace R1.
855 * So let's hard disable over this region.
856 */
857 local_irq_save(flags);
858 hard_irq_disable();
859
860 /* The TM SPRs are restored here, so that TEXASR.FS can be set
861 * before the trecheckpoint and no explosion occurs.
862 */
863 tm_restore_sprs(thread);
864
865 __tm_recheckpoint(thread, orig_msr);
866
867 local_irq_restore(flags);
868}
869
bc2a9408 870static inline void tm_recheckpoint_new_task(struct task_struct *new)
fb09692e
MN
871{
872 unsigned long msr;
873
874 if (!cpu_has_feature(CPU_FTR_TM))
875 return;
876
877 /* Recheckpoint the registers of the thread we're about to switch to.
878 *
879 * If the task was using FP, we non-lazily reload both the original and
880 * the speculative FP register states. This is because the kernel
881 * doesn't see if/when a TM rollback occurs, so if we take an FP
882 * unavoidable later, we are unable to determine which set of FP regs
883 * need to be restored.
884 */
885 if (!new->thread.regs)
886 return;
887
e6b8fd02
MN
888 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
889 tm_restore_sprs(&new->thread);
fb09692e 890 return;
e6b8fd02 891 }
829023df 892 msr = new->thread.ckpt_regs.msr;
fb09692e
MN
893 /* Recheckpoint to restore original checkpointed register state. */
894 TM_DEBUG("*** tm_recheckpoint of pid %d "
895 "(new->msr 0x%lx, new->origmsr 0x%lx)\n",
896 new->pid, new->thread.regs->msr, msr);
897
898 /* This loads the checkpointed FP/VEC state, if used */
899 tm_recheckpoint(&new->thread, msr);
900
901 /* This loads the speculative FP/VEC state, if used */
902 if (msr & MSR_FP) {
903 do_load_up_transact_fpu(&new->thread);
904 new->thread.regs->msr |=
905 (MSR_FP | new->thread.fpexc_mode);
906 }
f110c0c1 907#ifdef CONFIG_ALTIVEC
fb09692e
MN
908 if (msr & MSR_VEC) {
909 do_load_up_transact_altivec(&new->thread);
910 new->thread.regs->msr |= MSR_VEC;
911 }
f110c0c1 912#endif
fb09692e
MN
913 /* We may as well turn on VSX too since all the state is restored now */
914 if (msr & MSR_VSX)
915 new->thread.regs->msr |= MSR_VSX;
916
917 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
918 "(kernel msr 0x%lx)\n",
919 new->pid, mfmsr());
920}
921
922static inline void __switch_to_tm(struct task_struct *prev)
923{
924 if (cpu_has_feature(CPU_FTR_TM)) {
925 tm_enable();
926 tm_reclaim_task(prev);
927 }
928}
d31626f7
PM
929
930/*
931 * This is called if we are on the way out to userspace and the
932 * TIF_RESTORE_TM flag is set. It checks if we need to reload
933 * FP and/or vector state and does so if necessary.
934 * If userspace is inside a transaction (whether active or
935 * suspended) and FP/VMX/VSX instructions have ever been enabled
936 * inside that transaction, then we have to keep them enabled
937 * and keep the FP/VMX/VSX state loaded while ever the transaction
938 * continues. The reason is that if we didn't, and subsequently
939 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
940 * we don't know whether it's the same transaction, and thus we
941 * don't know which of the checkpointed state and the transactional
942 * state to use.
943 */
944void restore_tm_state(struct pt_regs *regs)
945{
946 unsigned long msr_diff;
947
948 clear_thread_flag(TIF_RESTORE_TM);
949 if (!MSR_TM_ACTIVE(regs->msr))
950 return;
951
829023df 952 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
d31626f7 953 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
70fe3d98
CB
954
955 restore_math(regs);
956
d31626f7
PM
957 regs->msr |= msr_diff;
958}
959
fb09692e
MN
960#else
961#define tm_recheckpoint_new_task(new)
962#define __switch_to_tm(prev)
963#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
9422de3e 964
152d523e
AB
965static inline void save_sprs(struct thread_struct *t)
966{
967#ifdef CONFIG_ALTIVEC
968 if (cpu_has_feature(cpu_has_feature(CPU_FTR_ALTIVEC)))
969 t->vrsave = mfspr(SPRN_VRSAVE);
970#endif
971#ifdef CONFIG_PPC_BOOK3S_64
972 if (cpu_has_feature(CPU_FTR_DSCR))
973 t->dscr = mfspr(SPRN_DSCR);
974
975 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
976 t->bescr = mfspr(SPRN_BESCR);
977 t->ebbhr = mfspr(SPRN_EBBHR);
978 t->ebbrr = mfspr(SPRN_EBBRR);
979
980 t->fscr = mfspr(SPRN_FSCR);
981
982 /*
983 * Note that the TAR is not available for use in the kernel.
984 * (To provide this, the TAR should be backed up/restored on
985 * exception entry/exit instead, and be in pt_regs. FIXME,
986 * this should be in pt_regs anyway (for debug).)
987 */
988 t->tar = mfspr(SPRN_TAR);
989 }
990#endif
991}
992
993static inline void restore_sprs(struct thread_struct *old_thread,
994 struct thread_struct *new_thread)
995{
996#ifdef CONFIG_ALTIVEC
997 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
998 old_thread->vrsave != new_thread->vrsave)
999 mtspr(SPRN_VRSAVE, new_thread->vrsave);
1000#endif
1001#ifdef CONFIG_PPC_BOOK3S_64
1002 if (cpu_has_feature(CPU_FTR_DSCR)) {
1003 u64 dscr = get_paca()->dscr_default;
1004 u64 fscr = old_thread->fscr & ~FSCR_DSCR;
1005
1006 if (new_thread->dscr_inherit) {
1007 dscr = new_thread->dscr;
1008 fscr |= FSCR_DSCR;
1009 }
1010
1011 if (old_thread->dscr != dscr)
1012 mtspr(SPRN_DSCR, dscr);
1013
1014 if (old_thread->fscr != fscr)
1015 mtspr(SPRN_FSCR, fscr);
1016 }
1017
1018 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1019 if (old_thread->bescr != new_thread->bescr)
1020 mtspr(SPRN_BESCR, new_thread->bescr);
1021 if (old_thread->ebbhr != new_thread->ebbhr)
1022 mtspr(SPRN_EBBHR, new_thread->ebbhr);
1023 if (old_thread->ebbrr != new_thread->ebbrr)
1024 mtspr(SPRN_EBBRR, new_thread->ebbrr);
1025
1026 if (old_thread->tar != new_thread->tar)
1027 mtspr(SPRN_TAR, new_thread->tar);
1028 }
1029#endif
1030}
1031
14cf11af
PM
1032struct task_struct *__switch_to(struct task_struct *prev,
1033 struct task_struct *new)
1034{
1035 struct thread_struct *new_thread, *old_thread;
14cf11af 1036 struct task_struct *last;
d6bf29b4
PZ
1037#ifdef CONFIG_PPC_BOOK3S_64
1038 struct ppc64_tlb_batch *batch;
1039#endif
14cf11af 1040
152d523e
AB
1041 new_thread = &new->thread;
1042 old_thread = &current->thread;
1043
7ba5fef7
MN
1044 WARN_ON(!irqs_disabled());
1045
06d67d54
PM
1046#ifdef CONFIG_PPC64
1047 /*
1048 * Collect processor utilization data per process
1049 */
1050 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
69111bac 1051 struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
06d67d54
PM
1052 long unsigned start_tb, current_tb;
1053 start_tb = old_thread->start_tb;
1054 cu->current_tb = current_tb = mfspr(SPRN_PURR);
1055 old_thread->accum_tb += (current_tb - start_tb);
1056 new_thread->start_tb = current_tb;
1057 }
d6bf29b4
PZ
1058#endif /* CONFIG_PPC64 */
1059
1060#ifdef CONFIG_PPC_BOOK3S_64
69111bac 1061 batch = this_cpu_ptr(&ppc64_tlb_batch);
d6bf29b4
PZ
1062 if (batch->active) {
1063 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1064 if (batch->index)
1065 __flush_tlb_pending(batch);
1066 batch->active = 0;
1067 }
1068#endif /* CONFIG_PPC_BOOK3S_64 */
06d67d54 1069
f3d885cc
AB
1070#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1071 switch_booke_debug_regs(&new->thread.debug);
1072#else
1073/*
1074 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1075 * schedule DABR
1076 */
1077#ifndef CONFIG_HAVE_HW_BREAKPOINT
1078 if (unlikely(!hw_brk_match(this_cpu_ptr(&current_brk), &new->thread.hw_brk)))
1079 __set_breakpoint(&new->thread.hw_brk);
1080#endif /* CONFIG_HAVE_HW_BREAKPOINT */
1081#endif
1082
1083 /*
1084 * We need to save SPRs before treclaim/trecheckpoint as these will
1085 * change a number of them.
1086 */
1087 save_sprs(&prev->thread);
1088
1089 __switch_to_tm(prev);
1090
1091 /* Save FPU, Altivec, VSX and SPE state */
1092 giveup_all(prev);
1093
44387e9f
AB
1094 /*
1095 * We can't take a PMU exception inside _switch() since there is a
1096 * window where the kernel stack SLB and the kernel stack are out
1097 * of sync. Hard disable here.
1098 */
1099 hard_irq_disable();
bc2a9408
MN
1100
1101 tm_recheckpoint_new_task(new);
1102
20dbe670
AB
1103 /*
1104 * Call restore_sprs() before calling _switch(). If we move it after
1105 * _switch() then we miss out on calling it for new tasks. The reason
1106 * for this is we manually create a stack frame for new tasks that
1107 * directly returns through ret_from_fork() or
1108 * ret_from_kernel_thread(). See copy_thread() for details.
1109 */
f3d885cc
AB
1110 restore_sprs(old_thread, new_thread);
1111
20dbe670
AB
1112 last = _switch(old_thread, new_thread);
1113
d6bf29b4
PZ
1114#ifdef CONFIG_PPC_BOOK3S_64
1115 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1116 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
69111bac 1117 batch = this_cpu_ptr(&ppc64_tlb_batch);
d6bf29b4
PZ
1118 batch->active = 1;
1119 }
70fe3d98
CB
1120
1121 if (current_thread_info()->task->thread.regs)
1122 restore_math(current_thread_info()->task->thread.regs);
1123
d6bf29b4
PZ
1124#endif /* CONFIG_PPC_BOOK3S_64 */
1125
14cf11af
PM
1126 return last;
1127}
1128
06d67d54
PM
1129static int instructions_to_print = 16;
1130
06d67d54
PM
1131static void show_instructions(struct pt_regs *regs)
1132{
1133 int i;
1134 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
1135 sizeof(int));
1136
1137 printk("Instruction dump:");
1138
1139 for (i = 0; i < instructions_to_print; i++) {
1140 int instr;
1141
1142 if (!(i % 8))
1143 printk("\n");
1144
0de2d820
SW
1145#if !defined(CONFIG_BOOKE)
1146 /* If executing with the IMMU off, adjust pc rather
1147 * than print XXXXXXXX.
1148 */
1149 if (!(regs->msr & MSR_IR))
1150 pc = (unsigned long)phys_to_virt(pc);
1151#endif
1152
00ae36de 1153 if (!__kernel_text_address(pc) ||
7b051f66 1154 probe_kernel_address((unsigned int __user *)pc, instr)) {
40c8cefa 1155 printk(KERN_CONT "XXXXXXXX ");
06d67d54
PM
1156 } else {
1157 if (regs->nip == pc)
40c8cefa 1158 printk(KERN_CONT "<%08x> ", instr);
06d67d54 1159 else
40c8cefa 1160 printk(KERN_CONT "%08x ", instr);
06d67d54
PM
1161 }
1162
1163 pc += sizeof(int);
1164 }
1165
1166 printk("\n");
1167}
1168
801c0b2c 1169struct regbit {
06d67d54
PM
1170 unsigned long bit;
1171 const char *name;
801c0b2c
MN
1172};
1173
1174static struct regbit msr_bits[] = {
3bfd0c9c
AB
1175#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
1176 {MSR_SF, "SF"},
1177 {MSR_HV, "HV"},
1178#endif
1179 {MSR_VEC, "VEC"},
1180 {MSR_VSX, "VSX"},
1181#ifdef CONFIG_BOOKE
1182 {MSR_CE, "CE"},
1183#endif
06d67d54
PM
1184 {MSR_EE, "EE"},
1185 {MSR_PR, "PR"},
1186 {MSR_FP, "FP"},
1187 {MSR_ME, "ME"},
3bfd0c9c 1188#ifdef CONFIG_BOOKE
1b98326b 1189 {MSR_DE, "DE"},
3bfd0c9c
AB
1190#else
1191 {MSR_SE, "SE"},
1192 {MSR_BE, "BE"},
1193#endif
06d67d54
PM
1194 {MSR_IR, "IR"},
1195 {MSR_DR, "DR"},
3bfd0c9c
AB
1196 {MSR_PMM, "PMM"},
1197#ifndef CONFIG_BOOKE
1198 {MSR_RI, "RI"},
1199 {MSR_LE, "LE"},
1200#endif
06d67d54
PM
1201 {0, NULL}
1202};
1203
801c0b2c 1204static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
06d67d54 1205{
801c0b2c 1206 const char *s = "";
06d67d54 1207
06d67d54
PM
1208 for (; bits->bit; ++bits)
1209 if (val & bits->bit) {
801c0b2c
MN
1210 printk("%s%s", s, bits->name);
1211 s = sep;
06d67d54 1212 }
801c0b2c
MN
1213}
1214
1215#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1216static struct regbit msr_tm_bits[] = {
1217 {MSR_TS_T, "T"},
1218 {MSR_TS_S, "S"},
1219 {MSR_TM, "E"},
1220 {0, NULL}
1221};
1222
1223static void print_tm_bits(unsigned long val)
1224{
1225/*
1226 * This only prints something if at least one of the TM bit is set.
1227 * Inside the TM[], the output means:
1228 * E: Enabled (bit 32)
1229 * S: Suspended (bit 33)
1230 * T: Transactional (bit 34)
1231 */
1232 if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
1233 printk(",TM[");
1234 print_bits(val, msr_tm_bits, "");
1235 printk("]");
1236 }
1237}
1238#else
1239static void print_tm_bits(unsigned long val) {}
1240#endif
1241
1242static void print_msr_bits(unsigned long val)
1243{
1244 printk("<");
1245 print_bits(val, msr_bits, ",");
1246 print_tm_bits(val);
06d67d54
PM
1247 printk(">");
1248}
1249
1250#ifdef CONFIG_PPC64
f6f7dde3 1251#define REG "%016lx"
06d67d54
PM
1252#define REGS_PER_LINE 4
1253#define LAST_VOLATILE 13
1254#else
f6f7dde3 1255#define REG "%08lx"
06d67d54
PM
1256#define REGS_PER_LINE 8
1257#define LAST_VOLATILE 12
1258#endif
1259
14cf11af
PM
1260void show_regs(struct pt_regs * regs)
1261{
1262 int i, trap;
1263
a43cb95d
TH
1264 show_regs_print_info(KERN_DEFAULT);
1265
06d67d54
PM
1266 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
1267 regs->nip, regs->link, regs->ctr);
1268 printk("REGS: %p TRAP: %04lx %s (%s)\n",
96b644bd 1269 regs, regs->trap, print_tainted(), init_utsname()->release);
06d67d54 1270 printk("MSR: "REG" ", regs->msr);
801c0b2c 1271 print_msr_bits(regs->msr);
f6f7dde3 1272 printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
14cf11af 1273 trap = TRAP(regs);
5115a026 1274 if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
9db8bcfd 1275 printk("CFAR: "REG" ", regs->orig_gpr3);
c5400649 1276 if (trap == 0x200 || trap == 0x300 || trap == 0x600)
ba28c9aa 1277#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
9db8bcfd 1278 printk("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
14170789 1279#else
9db8bcfd
AB
1280 printk("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1281#endif
1282#ifdef CONFIG_PPC64
1283 printk("SOFTE: %ld ", regs->softe);
1284#endif
1285#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
6d888d1a
AB
1286 if (MSR_TM_ACTIVE(regs->msr))
1287 printk("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
14170789 1288#endif
14cf11af
PM
1289
1290 for (i = 0; i < 32; i++) {
06d67d54 1291 if ((i % REGS_PER_LINE) == 0)
a2367194 1292 printk("\nGPR%02d: ", i);
06d67d54
PM
1293 printk(REG " ", regs->gpr[i]);
1294 if (i == LAST_VOLATILE && !FULL_REGS(regs))
14cf11af
PM
1295 break;
1296 }
1297 printk("\n");
1298#ifdef CONFIG_KALLSYMS
1299 /*
1300 * Lookup NIP late so we have the best change of getting the
1301 * above info out without failing
1302 */
058c78f4
BH
1303 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1304 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
afc07701 1305#endif
14cf11af 1306 show_stack(current, (unsigned long *) regs->gpr[1]);
06d67d54
PM
1307 if (!user_mode(regs))
1308 show_instructions(regs);
14cf11af
PM
1309}
1310
1311void exit_thread(void)
1312{
14cf11af
PM
1313}
1314
1315void flush_thread(void)
1316{
e0780b72 1317#ifdef CONFIG_HAVE_HW_BREAKPOINT
5aae8a53 1318 flush_ptrace_hw_breakpoint(current);
e0780b72 1319#else /* CONFIG_HAVE_HW_BREAKPOINT */
3bffb652 1320 set_debug_reg_defaults(&current->thread);
e0780b72 1321#endif /* CONFIG_HAVE_HW_BREAKPOINT */
14cf11af
PM
1322}
1323
1324void
1325release_thread(struct task_struct *t)
1326{
1327}
1328
1329/*
55ccf3fe
SS
1330 * this gets called so that we can store coprocessor state into memory and
1331 * copy the current task into the new thread.
14cf11af 1332 */
55ccf3fe 1333int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
14cf11af 1334{
579e633e 1335 flush_all_to_thread(src);
621b5060
MN
1336 /*
1337 * Flush TM state out so we can copy it. __switch_to_tm() does this
1338 * flush but it removes the checkpointed state from the current CPU and
1339 * transitions the CPU out of TM mode. Hence we need to call
1340 * tm_recheckpoint_new_task() (on the same task) to restore the
1341 * checkpointed state back and the TM mode.
1342 */
1343 __switch_to_tm(src);
1344 tm_recheckpoint_new_task(src);
330a1eb7 1345
55ccf3fe 1346 *dst = *src;
330a1eb7
ME
1347
1348 clear_task_ebb(dst);
1349
55ccf3fe 1350 return 0;
14cf11af
PM
1351}
1352
cec15488
ME
1353static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1354{
1355#ifdef CONFIG_PPC_STD_MMU_64
1356 unsigned long sp_vsid;
1357 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1358
1359 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1360 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1361 << SLB_VSID_SHIFT_1T;
1362 else
1363 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1364 << SLB_VSID_SHIFT;
1365 sp_vsid |= SLB_VSID_KERNEL | llp;
1366 p->thread.ksp_vsid = sp_vsid;
1367#endif
1368}
1369
14cf11af
PM
1370/*
1371 * Copy a thread..
1372 */
efcac658 1373
6eca8933
AD
1374/*
1375 * Copy architecture-specific thread state
1376 */
6f2c55b8 1377int copy_thread(unsigned long clone_flags, unsigned long usp,
6eca8933 1378 unsigned long kthread_arg, struct task_struct *p)
14cf11af
PM
1379{
1380 struct pt_regs *childregs, *kregs;
1381 extern void ret_from_fork(void);
58254e10
AV
1382 extern void ret_from_kernel_thread(void);
1383 void (*f)(void);
0cec6fd1 1384 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
14cf11af 1385
14cf11af
PM
1386 /* Copy registers */
1387 sp -= sizeof(struct pt_regs);
1388 childregs = (struct pt_regs *) sp;
ab75819d 1389 if (unlikely(p->flags & PF_KTHREAD)) {
6eca8933 1390 /* kernel thread */
138d1ce8 1391 struct thread_info *ti = (void *)task_stack_page(p);
58254e10 1392 memset(childregs, 0, sizeof(struct pt_regs));
14cf11af 1393 childregs->gpr[1] = sp + sizeof(struct pt_regs);
7cedd601
AB
1394 /* function */
1395 if (usp)
1396 childregs->gpr[14] = ppc_function_entry((void *)usp);
58254e10 1397#ifdef CONFIG_PPC64
b5e2fc1c 1398 clear_tsk_thread_flag(p, TIF_32BIT);
138d1ce8 1399 childregs->softe = 1;
06d67d54 1400#endif
6eca8933 1401 childregs->gpr[15] = kthread_arg;
14cf11af 1402 p->thread.regs = NULL; /* no user register state */
138d1ce8 1403 ti->flags |= _TIF_RESTOREALL;
58254e10 1404 f = ret_from_kernel_thread;
14cf11af 1405 } else {
6eca8933 1406 /* user thread */
afa86fc4 1407 struct pt_regs *regs = current_pt_regs();
58254e10
AV
1408 CHECK_FULL_REGS(regs);
1409 *childregs = *regs;
ea516b11
AV
1410 if (usp)
1411 childregs->gpr[1] = usp;
14cf11af 1412 p->thread.regs = childregs;
58254e10 1413 childregs->gpr[3] = 0; /* Result from fork() */
06d67d54
PM
1414 if (clone_flags & CLONE_SETTLS) {
1415#ifdef CONFIG_PPC64
9904b005 1416 if (!is_32bit_task())
06d67d54
PM
1417 childregs->gpr[13] = childregs->gpr[6];
1418 else
1419#endif
1420 childregs->gpr[2] = childregs->gpr[6];
1421 }
58254e10
AV
1422
1423 f = ret_from_fork;
14cf11af 1424 }
d272f667 1425 childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
14cf11af 1426 sp -= STACK_FRAME_OVERHEAD;
14cf11af
PM
1427
1428 /*
1429 * The way this works is that at some point in the future
1430 * some task will call _switch to switch to the new task.
1431 * That will pop off the stack frame created below and start
1432 * the new task running at ret_from_fork. The new task will
1433 * do some house keeping and then return from the fork or clone
1434 * system call, using the stack frame created above.
1435 */
af945cf4 1436 ((unsigned long *)sp)[0] = 0;
14cf11af
PM
1437 sp -= sizeof(struct pt_regs);
1438 kregs = (struct pt_regs *) sp;
1439 sp -= STACK_FRAME_OVERHEAD;
1440 p->thread.ksp = sp;
cbc9565e 1441#ifdef CONFIG_PPC32
85218827
KG
1442 p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
1443 _ALIGN_UP(sizeof(struct thread_info), 16);
cbc9565e 1444#endif
28d170ab
ON
1445#ifdef CONFIG_HAVE_HW_BREAKPOINT
1446 p->thread.ptrace_bps[0] = NULL;
1447#endif
1448
18461960
PM
1449 p->thread.fp_save_area = NULL;
1450#ifdef CONFIG_ALTIVEC
1451 p->thread.vr_save_area = NULL;
1452#endif
1453
cec15488
ME
1454 setup_ksp_vsid(p, sp);
1455
efcac658
AK
1456#ifdef CONFIG_PPC64
1457 if (cpu_has_feature(CPU_FTR_DSCR)) {
1021cb26 1458 p->thread.dscr_inherit = current->thread.dscr_inherit;
db1231dc 1459 p->thread.dscr = mfspr(SPRN_DSCR);
efcac658 1460 }
92779245
HM
1461 if (cpu_has_feature(CPU_FTR_HAS_PPR))
1462 p->thread.ppr = INIT_PPR;
efcac658 1463#endif
7cedd601 1464 kregs->nip = ppc_function_entry(f);
14cf11af
PM
1465 return 0;
1466}
1467
1468/*
1469 * Set up a thread for executing a new program
1470 */
06d67d54 1471void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
14cf11af 1472{
90eac727
ME
1473#ifdef CONFIG_PPC64
1474 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
1475#endif
1476
06d67d54
PM
1477 /*
1478 * If we exec out of a kernel thread then thread.regs will not be
1479 * set. Do it now.
1480 */
1481 if (!current->thread.regs) {
0cec6fd1
AV
1482 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1483 current->thread.regs = regs - 1;
06d67d54
PM
1484 }
1485
14cf11af
PM
1486 memset(regs->gpr, 0, sizeof(regs->gpr));
1487 regs->ctr = 0;
1488 regs->link = 0;
1489 regs->xer = 0;
1490 regs->ccr = 0;
14cf11af 1491 regs->gpr[1] = sp;
06d67d54 1492
474f8196
RM
1493 /*
1494 * We have just cleared all the nonvolatile GPRs, so make
1495 * FULL_REGS(regs) return true. This is necessary to allow
1496 * ptrace to examine the thread immediately after exec.
1497 */
1498 regs->trap &= ~1UL;
1499
06d67d54
PM
1500#ifdef CONFIG_PPC32
1501 regs->mq = 0;
1502 regs->nip = start;
14cf11af 1503 regs->msr = MSR_USER;
06d67d54 1504#else
9904b005 1505 if (!is_32bit_task()) {
94af3abf 1506 unsigned long entry;
06d67d54 1507
94af3abf
RR
1508 if (is_elf2_task()) {
1509 /* Look ma, no function descriptors! */
1510 entry = start;
06d67d54 1511
94af3abf
RR
1512 /*
1513 * Ulrich says:
1514 * The latest iteration of the ABI requires that when
1515 * calling a function (at its global entry point),
1516 * the caller must ensure r12 holds the entry point
1517 * address (so that the function can quickly
1518 * establish addressability).
1519 */
1520 regs->gpr[12] = start;
1521 /* Make sure that's restored on entry to userspace. */
1522 set_thread_flag(TIF_RESTOREALL);
1523 } else {
1524 unsigned long toc;
1525
1526 /* start is a relocated pointer to the function
1527 * descriptor for the elf _start routine. The first
1528 * entry in the function descriptor is the entry
1529 * address of _start and the second entry is the TOC
1530 * value we need to use.
1531 */
1532 __get_user(entry, (unsigned long __user *)start);
1533 __get_user(toc, (unsigned long __user *)start+1);
1534
1535 /* Check whether the e_entry function descriptor entries
1536 * need to be relocated before we can use them.
1537 */
1538 if (load_addr != 0) {
1539 entry += load_addr;
1540 toc += load_addr;
1541 }
1542 regs->gpr[2] = toc;
06d67d54
PM
1543 }
1544 regs->nip = entry;
06d67d54 1545 regs->msr = MSR_USER64;
d4bf9a78
SR
1546 } else {
1547 regs->nip = start;
1548 regs->gpr[2] = 0;
1549 regs->msr = MSR_USER32;
06d67d54
PM
1550 }
1551#endif
ce48b210
MN
1552#ifdef CONFIG_VSX
1553 current->thread.used_vsr = 0;
1554#endif
de79f7b9 1555 memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
18461960 1556 current->thread.fp_save_area = NULL;
14cf11af 1557#ifdef CONFIG_ALTIVEC
de79f7b9
PM
1558 memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
1559 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
18461960 1560 current->thread.vr_save_area = NULL;
14cf11af
PM
1561 current->thread.vrsave = 0;
1562 current->thread.used_vr = 0;
1563#endif /* CONFIG_ALTIVEC */
1564#ifdef CONFIG_SPE
1565 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1566 current->thread.acc = 0;
1567 current->thread.spefscr = 0;
1568 current->thread.used_spe = 0;
1569#endif /* CONFIG_SPE */
bc2a9408
MN
1570#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1571 if (cpu_has_feature(CPU_FTR_TM))
1572 regs->msr |= MSR_TM;
1573 current->thread.tm_tfhar = 0;
1574 current->thread.tm_texasr = 0;
1575 current->thread.tm_tfiar = 0;
1576#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
14cf11af 1577}
e1802b06 1578EXPORT_SYMBOL(start_thread);
14cf11af
PM
1579
1580#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1581 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1582
1583int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1584{
1585 struct pt_regs *regs = tsk->thread.regs;
1586
1587 /* This is a bit hairy. If we are an SPE enabled processor
1588 * (have embedded fp) we store the IEEE exception enable flags in
1589 * fpexc_mode. fpexc_mode is also used for setting FP exception
1590 * mode (asyn, precise, disabled) for 'Classic' FP. */
1591 if (val & PR_FP_EXC_SW_ENABLE) {
1592#ifdef CONFIG_SPE
5e14d21e 1593 if (cpu_has_feature(CPU_FTR_SPE)) {
640e9225
JM
1594 /*
1595 * When the sticky exception bits are set
1596 * directly by userspace, it must call prctl
1597 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1598 * in the existing prctl settings) or
1599 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1600 * the bits being set). <fenv.h> functions
1601 * saving and restoring the whole
1602 * floating-point environment need to do so
1603 * anyway to restore the prctl settings from
1604 * the saved environment.
1605 */
1606 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
5e14d21e
KG
1607 tsk->thread.fpexc_mode = val &
1608 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1609 return 0;
1610 } else {
1611 return -EINVAL;
1612 }
14cf11af
PM
1613#else
1614 return -EINVAL;
1615#endif
14cf11af 1616 }
06d67d54
PM
1617
1618 /* on a CONFIG_SPE this does not hurt us. The bits that
1619 * __pack_fe01 use do not overlap with bits used for
1620 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1621 * on CONFIG_SPE implementations are reserved so writing to
1622 * them does not change anything */
1623 if (val > PR_FP_EXC_PRECISE)
1624 return -EINVAL;
1625 tsk->thread.fpexc_mode = __pack_fe01(val);
1626 if (regs != NULL && (regs->msr & MSR_FP) != 0)
1627 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1628 | tsk->thread.fpexc_mode;
14cf11af
PM
1629 return 0;
1630}
1631
1632int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1633{
1634 unsigned int val;
1635
1636 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
1637#ifdef CONFIG_SPE
640e9225
JM
1638 if (cpu_has_feature(CPU_FTR_SPE)) {
1639 /*
1640 * When the sticky exception bits are set
1641 * directly by userspace, it must call prctl
1642 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1643 * in the existing prctl settings) or
1644 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1645 * the bits being set). <fenv.h> functions
1646 * saving and restoring the whole
1647 * floating-point environment need to do so
1648 * anyway to restore the prctl settings from
1649 * the saved environment.
1650 */
1651 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
5e14d21e 1652 val = tsk->thread.fpexc_mode;
640e9225 1653 } else
5e14d21e 1654 return -EINVAL;
14cf11af
PM
1655#else
1656 return -EINVAL;
1657#endif
1658 else
1659 val = __unpack_fe01(tsk->thread.fpexc_mode);
1660 return put_user(val, (unsigned int __user *) adr);
1661}
1662
fab5db97
PM
1663int set_endian(struct task_struct *tsk, unsigned int val)
1664{
1665 struct pt_regs *regs = tsk->thread.regs;
1666
1667 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1668 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1669 return -EINVAL;
1670
1671 if (regs == NULL)
1672 return -EINVAL;
1673
1674 if (val == PR_ENDIAN_BIG)
1675 regs->msr &= ~MSR_LE;
1676 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1677 regs->msr |= MSR_LE;
1678 else
1679 return -EINVAL;
1680
1681 return 0;
1682}
1683
1684int get_endian(struct task_struct *tsk, unsigned long adr)
1685{
1686 struct pt_regs *regs = tsk->thread.regs;
1687 unsigned int val;
1688
1689 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1690 !cpu_has_feature(CPU_FTR_REAL_LE))
1691 return -EINVAL;
1692
1693 if (regs == NULL)
1694 return -EINVAL;
1695
1696 if (regs->msr & MSR_LE) {
1697 if (cpu_has_feature(CPU_FTR_REAL_LE))
1698 val = PR_ENDIAN_LITTLE;
1699 else
1700 val = PR_ENDIAN_PPC_LITTLE;
1701 } else
1702 val = PR_ENDIAN_BIG;
1703
1704 return put_user(val, (unsigned int __user *)adr);
1705}
1706
e9370ae1
PM
1707int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
1708{
1709 tsk->thread.align_ctl = val;
1710 return 0;
1711}
1712
1713int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
1714{
1715 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
1716}
1717
bb72c481
PM
1718static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
1719 unsigned long nbytes)
1720{
1721 unsigned long stack_page;
1722 unsigned long cpu = task_cpu(p);
1723
1724 /*
1725 * Avoid crashing if the stack has overflowed and corrupted
1726 * task_cpu(p), which is in the thread_info struct.
1727 */
1728 if (cpu < NR_CPUS && cpu_possible(cpu)) {
1729 stack_page = (unsigned long) hardirq_ctx[cpu];
1730 if (sp >= stack_page + sizeof(struct thread_struct)
1731 && sp <= stack_page + THREAD_SIZE - nbytes)
1732 return 1;
1733
1734 stack_page = (unsigned long) softirq_ctx[cpu];
1735 if (sp >= stack_page + sizeof(struct thread_struct)
1736 && sp <= stack_page + THREAD_SIZE - nbytes)
1737 return 1;
1738 }
1739 return 0;
1740}
1741
2f25194d 1742int validate_sp(unsigned long sp, struct task_struct *p,
14cf11af
PM
1743 unsigned long nbytes)
1744{
0cec6fd1 1745 unsigned long stack_page = (unsigned long)task_stack_page(p);
14cf11af
PM
1746
1747 if (sp >= stack_page + sizeof(struct thread_struct)
1748 && sp <= stack_page + THREAD_SIZE - nbytes)
1749 return 1;
1750
bb72c481 1751 return valid_irq_stack(sp, p, nbytes);
14cf11af
PM
1752}
1753
2f25194d
AB
1754EXPORT_SYMBOL(validate_sp);
1755
14cf11af
PM
1756unsigned long get_wchan(struct task_struct *p)
1757{
1758 unsigned long ip, sp;
1759 int count = 0;
1760
1761 if (!p || p == current || p->state == TASK_RUNNING)
1762 return 0;
1763
1764 sp = p->thread.ksp;
ec2b36b9 1765 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
14cf11af
PM
1766 return 0;
1767
1768 do {
1769 sp = *(unsigned long *)sp;
ec2b36b9 1770 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
14cf11af
PM
1771 return 0;
1772 if (count > 0) {
ec2b36b9 1773 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
14cf11af
PM
1774 if (!in_sched_functions(ip))
1775 return ip;
1776 }
1777 } while (count++ < 16);
1778 return 0;
1779}
06d67d54 1780
c4d04be1 1781static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
06d67d54
PM
1782
1783void show_stack(struct task_struct *tsk, unsigned long *stack)
1784{
1785 unsigned long sp, ip, lr, newsp;
1786 int count = 0;
1787 int firstframe = 1;
6794c782
SR
1788#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1789 int curr_frame = current->curr_ret_stack;
1790 extern void return_to_handler(void);
9135c3cc 1791 unsigned long rth = (unsigned long)return_to_handler;
6794c782 1792#endif
06d67d54
PM
1793
1794 sp = (unsigned long) stack;
1795 if (tsk == NULL)
1796 tsk = current;
1797 if (sp == 0) {
1798 if (tsk == current)
acf620ec 1799 sp = current_stack_pointer();
06d67d54
PM
1800 else
1801 sp = tsk->thread.ksp;
1802 }
1803
1804 lr = 0;
1805 printk("Call Trace:\n");
1806 do {
ec2b36b9 1807 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
06d67d54
PM
1808 return;
1809
1810 stack = (unsigned long *) sp;
1811 newsp = stack[0];
ec2b36b9 1812 ip = stack[STACK_FRAME_LR_SAVE];
06d67d54 1813 if (!firstframe || ip != lr) {
058c78f4 1814 printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
6794c782 1815#ifdef CONFIG_FUNCTION_GRAPH_TRACER
7d56c65a 1816 if ((ip == rth) && curr_frame >= 0) {
6794c782
SR
1817 printk(" (%pS)",
1818 (void *)current->ret_stack[curr_frame].ret);
1819 curr_frame--;
1820 }
1821#endif
06d67d54
PM
1822 if (firstframe)
1823 printk(" (unreliable)");
1824 printk("\n");
1825 }
1826 firstframe = 0;
1827
1828 /*
1829 * See if this is an exception frame.
1830 * We look for the "regshere" marker in the current frame.
1831 */
ec2b36b9
BH
1832 if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
1833 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
06d67d54
PM
1834 struct pt_regs *regs = (struct pt_regs *)
1835 (sp + STACK_FRAME_OVERHEAD);
06d67d54 1836 lr = regs->link;
9be9be2e 1837 printk("--- interrupt: %lx at %pS\n LR = %pS\n",
058c78f4 1838 regs->trap, (void *)regs->nip, (void *)lr);
06d67d54
PM
1839 firstframe = 1;
1840 }
1841
1842 sp = newsp;
1843 } while (count++ < kstack_depth_to_print);
1844}
1845
cb2c9b27 1846#ifdef CONFIG_PPC64
fe1952fc 1847/* Called with hard IRQs off */
0e37739b 1848void notrace __ppc64_runlatch_on(void)
cb2c9b27 1849{
fe1952fc 1850 struct thread_info *ti = current_thread_info();
cb2c9b27
AB
1851 unsigned long ctrl;
1852
fe1952fc
BH
1853 ctrl = mfspr(SPRN_CTRLF);
1854 ctrl |= CTRL_RUNLATCH;
1855 mtspr(SPRN_CTRLT, ctrl);
cb2c9b27 1856
fae2e0fb 1857 ti->local_flags |= _TLF_RUNLATCH;
cb2c9b27
AB
1858}
1859
fe1952fc 1860/* Called with hard IRQs off */
0e37739b 1861void notrace __ppc64_runlatch_off(void)
cb2c9b27 1862{
fe1952fc 1863 struct thread_info *ti = current_thread_info();
cb2c9b27
AB
1864 unsigned long ctrl;
1865
fae2e0fb 1866 ti->local_flags &= ~_TLF_RUNLATCH;
cb2c9b27 1867
4138d653
AB
1868 ctrl = mfspr(SPRN_CTRLF);
1869 ctrl &= ~CTRL_RUNLATCH;
1870 mtspr(SPRN_CTRLT, ctrl);
cb2c9b27 1871}
fe1952fc 1872#endif /* CONFIG_PPC64 */
f6a61680 1873
d839088c
AB
1874unsigned long arch_align_stack(unsigned long sp)
1875{
1876 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
1877 sp -= get_random_int() & ~PAGE_MASK;
1878 return sp & ~0xf;
1879}
912f9ee2
AB
1880
1881static inline unsigned long brk_rnd(void)
1882{
1883 unsigned long rnd = 0;
1884
1885 /* 8MB for 32bit, 1GB for 64bit */
1886 if (is_32bit_task())
1887 rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
1888 else
1889 rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
1890
1891 return rnd << PAGE_SHIFT;
1892}
1893
1894unsigned long arch_randomize_brk(struct mm_struct *mm)
1895{
8bbde7a7
AB
1896 unsigned long base = mm->brk;
1897 unsigned long ret;
1898
ce7a35c7 1899#ifdef CONFIG_PPC_STD_MMU_64
8bbde7a7
AB
1900 /*
1901 * If we are using 1TB segments and we are allowed to randomise
1902 * the heap, we can put it above 1TB so it is backed by a 1TB
1903 * segment. Otherwise the heap will be in the bottom 1TB
1904 * which always uses 256MB segments and this may result in a
1905 * performance penalty.
1906 */
1907 if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
1908 base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
1909#endif
1910
1911 ret = PAGE_ALIGN(base + brk_rnd());
912f9ee2
AB
1912
1913 if (ret < mm->brk)
1914 return mm->brk;
1915
1916 return ret;
1917}
501cb16d 1918