1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Derived from "arch/i386/kernel/process.c"
4 * Copyright (C) 1995 Linus Torvalds
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/sched/debug.h>
16 #include <linux/sched/task.h>
17 #include <linux/sched/task_stack.h>
18 #include <linux/kernel.h>
20 #include <linux/smp.h>
21 #include <linux/stddef.h>
22 #include <linux/unistd.h>
23 #include <linux/ptrace.h>
24 #include <linux/slab.h>
25 #include <linux/user.h>
26 #include <linux/elf.h>
27 #include <linux/prctl.h>
28 #include <linux/init_task.h>
29 #include <linux/export.h>
30 #include <linux/kallsyms.h>
31 #include <linux/mqueue.h>
32 #include <linux/hardirq.h>
33 #include <linux/utsname.h>
34 #include <linux/ftrace.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/personality.h>
37 #include <linux/random.h>
38 #include <linux/hw_breakpoint.h>
39 #include <linux/uaccess.h>
40 #include <linux/elf-randomize.h>
41 #include <linux/pkeys.h>
42 #include <linux/seq_buf.h>
44 #include <asm/interrupt.h>
46 #include <asm/processor.h>
49 #include <asm/machdep.h>
51 #include <asm/runlatch.h>
52 #include <asm/syscalls.h>
53 #include <asm/switch_to.h>
55 #include <asm/debug.h>
57 #include <asm/firmware.h>
58 #include <asm/hw_irq.h>
60 #include <asm/code-patching.h>
62 #include <asm/livepatch.h>
63 #include <asm/cpu_has_feature.h>
64 #include <asm/asm-prototypes.h>
65 #include <asm/stacktrace.h>
66 #include <asm/hw_breakpoint.h>
68 #include <linux/kprobes.h>
69 #include <linux/kdebug.h>
71 /* Transactional Memory debug */
73 #define TM_DEBUG(x...) printk(KERN_INFO x)
75 #define TM_DEBUG(x...) do { } while(0)
78 extern unsigned long _get_SP(void);
80 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
82 * Are we running in "Suspend disabled" mode? If so we have to block any
83 * sigreturn that would get us into suspended state, and we also warn in some
84 * other paths that we should never reach with suspend disabled.
86 bool tm_suspend_disabled __ro_after_init = false;
88 static void check_if_tm_restore_required(struct task_struct *tsk)
91 * If we are saving the current thread's registers, and the
92 * thread is in a transactional state, set the TIF_RESTORE_TM
93 * bit so that we know to restore the registers before
94 * returning to userspace.
96 if (tsk == current && tsk->thread.regs &&
97 MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
98 !test_thread_flag(TIF_RESTORE_TM)) {
99 tsk->thread.ckpt_regs.msr = tsk->thread.regs->msr;
100 set_thread_flag(TIF_RESTORE_TM);
105 static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
106 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
108 bool strict_msr_control;
109 EXPORT_SYMBOL(strict_msr_control);
111 static int __init enable_strict_msr_control(char *str)
113 strict_msr_control = true;
114 pr_info("Enabling strict facility control\n");
118 early_param("ppc_strict_facility_enable", enable_strict_msr_control);
120 /* notrace because it's called by restore_math */
121 unsigned long notrace msr_check_and_set(unsigned long bits)
123 unsigned long oldmsr = mfmsr();
124 unsigned long newmsr;
126 newmsr = oldmsr | bits;
128 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
131 if (oldmsr != newmsr)
136 EXPORT_SYMBOL_GPL(msr_check_and_set);
138 /* notrace because it's called by restore_math */
139 void notrace __msr_check_and_clear(unsigned long bits)
141 unsigned long oldmsr = mfmsr();
142 unsigned long newmsr;
144 newmsr = oldmsr & ~bits;
146 if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
149 if (oldmsr != newmsr)
152 EXPORT_SYMBOL(__msr_check_and_clear);
154 #ifdef CONFIG_PPC_FPU
155 static void __giveup_fpu(struct task_struct *tsk)
160 msr = tsk->thread.regs->msr;
161 msr &= ~(MSR_FP|MSR_FE0|MSR_FE1);
162 if (cpu_has_feature(CPU_FTR_VSX))
164 tsk->thread.regs->msr = msr;
167 void giveup_fpu(struct task_struct *tsk)
169 check_if_tm_restore_required(tsk);
171 msr_check_and_set(MSR_FP);
173 msr_check_and_clear(MSR_FP);
175 EXPORT_SYMBOL(giveup_fpu);
178 * Make sure the floating-point register state in the
179 * the thread_struct is up to date for task tsk.
181 void flush_fp_to_thread(struct task_struct *tsk)
183 if (tsk->thread.regs) {
185 * We need to disable preemption here because if we didn't,
186 * another process could get scheduled after the regs->msr
187 * test but before we have finished saving the FP registers
188 * to the thread_struct. That process could take over the
189 * FPU, and then when we get scheduled again we would store
190 * bogus values for the remaining FP registers.
193 if (tsk->thread.regs->msr & MSR_FP) {
195 * This should only ever be called for current or
196 * for a stopped child process. Since we save away
197 * the FP register state on context switch,
198 * there is something wrong if a stopped child appears
199 * to still have its FP state in the CPU registers.
201 BUG_ON(tsk != current);
207 EXPORT_SYMBOL_GPL(flush_fp_to_thread);
209 void enable_kernel_fp(void)
211 unsigned long cpumsr;
213 WARN_ON(preemptible());
215 cpumsr = msr_check_and_set(MSR_FP);
217 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
218 check_if_tm_restore_required(current);
220 * If a thread has already been reclaimed then the
221 * checkpointed registers are on the CPU but have definitely
222 * been saved by the reclaim code. Don't need to and *cannot*
223 * giveup as this would save to the 'live' structure not the
224 * checkpointed structure.
226 if (!MSR_TM_ACTIVE(cpumsr) &&
227 MSR_TM_ACTIVE(current->thread.regs->msr))
229 __giveup_fpu(current);
232 EXPORT_SYMBOL(enable_kernel_fp);
234 static inline void __giveup_fpu(struct task_struct *tsk) { }
235 #endif /* CONFIG_PPC_FPU */
237 #ifdef CONFIG_ALTIVEC
238 static void __giveup_altivec(struct task_struct *tsk)
243 msr = tsk->thread.regs->msr;
245 if (cpu_has_feature(CPU_FTR_VSX))
247 tsk->thread.regs->msr = msr;
250 void giveup_altivec(struct task_struct *tsk)
252 check_if_tm_restore_required(tsk);
254 msr_check_and_set(MSR_VEC);
255 __giveup_altivec(tsk);
256 msr_check_and_clear(MSR_VEC);
258 EXPORT_SYMBOL(giveup_altivec);
260 void enable_kernel_altivec(void)
262 unsigned long cpumsr;
264 WARN_ON(preemptible());
266 cpumsr = msr_check_and_set(MSR_VEC);
268 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
269 check_if_tm_restore_required(current);
271 * If a thread has already been reclaimed then the
272 * checkpointed registers are on the CPU but have definitely
273 * been saved by the reclaim code. Don't need to and *cannot*
274 * giveup as this would save to the 'live' structure not the
275 * checkpointed structure.
277 if (!MSR_TM_ACTIVE(cpumsr) &&
278 MSR_TM_ACTIVE(current->thread.regs->msr))
280 __giveup_altivec(current);
283 EXPORT_SYMBOL(enable_kernel_altivec);
286 * Make sure the VMX/Altivec register state in the
287 * the thread_struct is up to date for task tsk.
289 void flush_altivec_to_thread(struct task_struct *tsk)
291 if (tsk->thread.regs) {
293 if (tsk->thread.regs->msr & MSR_VEC) {
294 BUG_ON(tsk != current);
300 EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
301 #endif /* CONFIG_ALTIVEC */
304 static void __giveup_vsx(struct task_struct *tsk)
306 unsigned long msr = tsk->thread.regs->msr;
309 * We should never be ssetting MSR_VSX without also setting
312 WARN_ON((msr & MSR_VSX) && !((msr & MSR_FP) && (msr & MSR_VEC)));
314 /* __giveup_fpu will clear MSR_VSX */
318 __giveup_altivec(tsk);
321 static void giveup_vsx(struct task_struct *tsk)
323 check_if_tm_restore_required(tsk);
325 msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
327 msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
330 void enable_kernel_vsx(void)
332 unsigned long cpumsr;
334 WARN_ON(preemptible());
336 cpumsr = msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
338 if (current->thread.regs &&
339 (current->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP))) {
340 check_if_tm_restore_required(current);
342 * If a thread has already been reclaimed then the
343 * checkpointed registers are on the CPU but have definitely
344 * been saved by the reclaim code. Don't need to and *cannot*
345 * giveup as this would save to the 'live' structure not the
346 * checkpointed structure.
348 if (!MSR_TM_ACTIVE(cpumsr) &&
349 MSR_TM_ACTIVE(current->thread.regs->msr))
351 __giveup_vsx(current);
354 EXPORT_SYMBOL(enable_kernel_vsx);
356 void flush_vsx_to_thread(struct task_struct *tsk)
358 if (tsk->thread.regs) {
360 if (tsk->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP)) {
361 BUG_ON(tsk != current);
367 EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
368 #endif /* CONFIG_VSX */
371 void giveup_spe(struct task_struct *tsk)
373 check_if_tm_restore_required(tsk);
375 msr_check_and_set(MSR_SPE);
377 msr_check_and_clear(MSR_SPE);
379 EXPORT_SYMBOL(giveup_spe);
381 void enable_kernel_spe(void)
383 WARN_ON(preemptible());
385 msr_check_and_set(MSR_SPE);
387 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
388 check_if_tm_restore_required(current);
389 __giveup_spe(current);
392 EXPORT_SYMBOL(enable_kernel_spe);
394 void flush_spe_to_thread(struct task_struct *tsk)
396 if (tsk->thread.regs) {
398 if (tsk->thread.regs->msr & MSR_SPE) {
399 BUG_ON(tsk != current);
400 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
406 #endif /* CONFIG_SPE */
408 static unsigned long msr_all_available;
410 static int __init init_msr_all_available(void)
412 if (IS_ENABLED(CONFIG_PPC_FPU))
413 msr_all_available |= MSR_FP;
414 if (cpu_has_feature(CPU_FTR_ALTIVEC))
415 msr_all_available |= MSR_VEC;
416 if (cpu_has_feature(CPU_FTR_VSX))
417 msr_all_available |= MSR_VSX;
418 if (cpu_has_feature(CPU_FTR_SPE))
419 msr_all_available |= MSR_SPE;
423 early_initcall(init_msr_all_available);
425 void giveup_all(struct task_struct *tsk)
427 unsigned long usermsr;
429 if (!tsk->thread.regs)
432 check_if_tm_restore_required(tsk);
434 usermsr = tsk->thread.regs->msr;
436 if ((usermsr & msr_all_available) == 0)
439 msr_check_and_set(msr_all_available);
441 WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
443 if (usermsr & MSR_FP)
445 if (usermsr & MSR_VEC)
446 __giveup_altivec(tsk);
447 if (usermsr & MSR_SPE)
450 msr_check_and_clear(msr_all_available);
452 EXPORT_SYMBOL(giveup_all);
454 #ifdef CONFIG_PPC_BOOK3S_64
455 #ifdef CONFIG_PPC_FPU
456 static bool should_restore_fp(void)
458 if (current->thread.load_fp) {
459 current->thread.load_fp++;
465 static void do_restore_fp(void)
467 load_fp_state(¤t->thread.fp_state);
470 static bool should_restore_fp(void) { return false; }
471 static void do_restore_fp(void) { }
472 #endif /* CONFIG_PPC_FPU */
474 #ifdef CONFIG_ALTIVEC
475 static bool should_restore_altivec(void)
477 if (cpu_has_feature(CPU_FTR_ALTIVEC) && (current->thread.load_vec)) {
478 current->thread.load_vec++;
484 static void do_restore_altivec(void)
486 load_vr_state(¤t->thread.vr_state);
487 current->thread.used_vr = 1;
490 static bool should_restore_altivec(void) { return false; }
491 static void do_restore_altivec(void) { }
492 #endif /* CONFIG_ALTIVEC */
494 static bool should_restore_vsx(void)
496 if (cpu_has_feature(CPU_FTR_VSX))
501 static void do_restore_vsx(void)
503 current->thread.used_vsr = 1;
506 static void do_restore_vsx(void) { }
507 #endif /* CONFIG_VSX */
510 * The exception exit path calls restore_math() with interrupts hard disabled
511 * but the soft irq state not "reconciled". ftrace code that calls
512 * local_irq_save/restore causes warnings.
514 * Rather than complicate the exit path, just don't trace restore_math. This
515 * could be done by having ftrace entry code check for this un-reconciled
516 * condition where MSR[EE]=0 and PACA_IRQ_HARD_DIS is not set, and
517 * temporarily fix it up for the duration of the ftrace call.
519 void notrace restore_math(struct pt_regs *regs)
522 unsigned long new_msr = 0;
527 * new_msr tracks the facilities that are to be restored. Only reload
528 * if the bit is not set in the user MSR (if it is set, the registers
529 * are live for the user thread).
531 if ((!(msr & MSR_FP)) && should_restore_fp())
534 if ((!(msr & MSR_VEC)) && should_restore_altivec())
537 if ((!(msr & MSR_VSX)) && should_restore_vsx()) {
538 if (((msr | new_msr) & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC))
543 unsigned long fpexc_mode = 0;
545 msr_check_and_set(new_msr);
547 if (new_msr & MSR_FP) {
550 // This also covers VSX, because VSX implies FP
551 fpexc_mode = current->thread.fpexc_mode;
554 if (new_msr & MSR_VEC)
555 do_restore_altivec();
557 if (new_msr & MSR_VSX)
560 msr_check_and_clear(new_msr);
562 regs->msr |= new_msr | fpexc_mode;
565 #endif /* CONFIG_PPC_BOOK3S_64 */
567 static void save_all(struct task_struct *tsk)
569 unsigned long usermsr;
571 if (!tsk->thread.regs)
574 usermsr = tsk->thread.regs->msr;
576 if ((usermsr & msr_all_available) == 0)
579 msr_check_and_set(msr_all_available);
581 WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC)));
583 if (usermsr & MSR_FP)
586 if (usermsr & MSR_VEC)
589 if (usermsr & MSR_SPE)
592 msr_check_and_clear(msr_all_available);
595 void flush_all_to_thread(struct task_struct *tsk)
597 if (tsk->thread.regs) {
599 BUG_ON(tsk != current);
601 if (tsk->thread.regs->msr & MSR_SPE)
602 tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
609 EXPORT_SYMBOL(flush_all_to_thread);
611 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
612 void do_send_trap(struct pt_regs *regs, unsigned long address,
613 unsigned long error_code, int breakpt)
615 current->thread.trap_nr = TRAP_HWBKPT;
616 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
617 11, SIGSEGV) == NOTIFY_STOP)
620 /* Deliver the signal to userspace */
621 force_sig_ptrace_errno_trap(breakpt, /* breakpoint or watchpoint id */
622 (void __user *)address);
624 #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
626 static void do_break_handler(struct pt_regs *regs)
628 struct arch_hw_breakpoint null_brk = {0};
629 struct arch_hw_breakpoint *info;
630 struct ppc_inst instr = ppc_inst(0);
637 * If underneath hw supports only one watchpoint, we know it
638 * caused exception. 8xx also falls into this category.
640 if (nr_wp_slots() == 1) {
641 __set_breakpoint(0, &null_brk);
642 current->thread.hw_brk[0] = null_brk;
643 current->thread.hw_brk[0].flags |= HW_BRK_FLAG_DISABLED;
647 /* Otherwise findout which DAWR caused exception and disable it. */
648 wp_get_instr_detail(regs, &instr, &type, &size, &ea);
650 for (i = 0; i < nr_wp_slots(); i++) {
651 info = ¤t->thread.hw_brk[i];
655 if (wp_check_constraints(regs, instr, ea, type, size, info)) {
656 __set_breakpoint(i, &null_brk);
657 current->thread.hw_brk[i] = null_brk;
658 current->thread.hw_brk[i].flags |= HW_BRK_FLAG_DISABLED;
663 DEFINE_INTERRUPT_HANDLER(do_break)
665 current->thread.trap_nr = TRAP_HWBKPT;
666 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, regs->dsisr,
667 11, SIGSEGV) == NOTIFY_STOP)
670 if (debugger_break_match(regs))
674 * We reach here only when watchpoint exception is generated by ptrace
675 * event (or hw is buggy!). Now if CONFIG_HAVE_HW_BREAKPOINT is set,
676 * watchpoint is already handled by hw_breakpoint_handler() so we don't
677 * have to do anything. But when CONFIG_HAVE_HW_BREAKPOINT is not set,
678 * we need to manually handle the watchpoint here.
680 if (!IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT))
681 do_break_handler(regs);
683 /* Deliver the signal to userspace */
684 force_sig_fault(SIGTRAP, TRAP_HWBKPT, (void __user *)regs->dar);
686 #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
688 static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk[HBP_NUM_MAX]);
690 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
692 * Set the debug registers back to their default "safe" values.
694 static void set_debug_reg_defaults(struct thread_struct *thread)
696 thread->debug.iac1 = thread->debug.iac2 = 0;
697 #if CONFIG_PPC_ADV_DEBUG_IACS > 2
698 thread->debug.iac3 = thread->debug.iac4 = 0;
700 thread->debug.dac1 = thread->debug.dac2 = 0;
701 #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
702 thread->debug.dvc1 = thread->debug.dvc2 = 0;
704 thread->debug.dbcr0 = 0;
707 * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
709 thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
710 DBCR1_IAC3US | DBCR1_IAC4US;
712 * Force Data Address Compare User/Supervisor bits to be User-only
713 * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
715 thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
717 thread->debug.dbcr1 = 0;
721 static void prime_debug_regs(struct debug_reg *debug)
724 * We could have inherited MSR_DE from userspace, since
725 * it doesn't get cleared on exception entry. Make sure
726 * MSR_DE is clear before we enable any debug events.
728 mtmsr(mfmsr() & ~MSR_DE);
730 mtspr(SPRN_IAC1, debug->iac1);
731 mtspr(SPRN_IAC2, debug->iac2);
732 #if CONFIG_PPC_ADV_DEBUG_IACS > 2
733 mtspr(SPRN_IAC3, debug->iac3);
734 mtspr(SPRN_IAC4, debug->iac4);
736 mtspr(SPRN_DAC1, debug->dac1);
737 mtspr(SPRN_DAC2, debug->dac2);
738 #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
739 mtspr(SPRN_DVC1, debug->dvc1);
740 mtspr(SPRN_DVC2, debug->dvc2);
742 mtspr(SPRN_DBCR0, debug->dbcr0);
743 mtspr(SPRN_DBCR1, debug->dbcr1);
745 mtspr(SPRN_DBCR2, debug->dbcr2);
749 * Unless neither the old or new thread are making use of the
750 * debug registers, set the debug registers from the values
751 * stored in the new thread.
753 void switch_booke_debug_regs(struct debug_reg *new_debug)
755 if ((current->thread.debug.dbcr0 & DBCR0_IDM)
756 || (new_debug->dbcr0 & DBCR0_IDM))
757 prime_debug_regs(new_debug);
759 EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
760 #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
761 #ifndef CONFIG_HAVE_HW_BREAKPOINT
762 static void set_breakpoint(int i, struct arch_hw_breakpoint *brk)
765 __set_breakpoint(i, brk);
769 static void set_debug_reg_defaults(struct thread_struct *thread)
772 struct arch_hw_breakpoint null_brk = {0};
774 for (i = 0; i < nr_wp_slots(); i++) {
775 thread->hw_brk[i] = null_brk;
776 if (ppc_breakpoint_available())
777 set_breakpoint(i, &thread->hw_brk[i]);
781 static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
782 struct arch_hw_breakpoint *b)
784 if (a->address != b->address)
786 if (a->type != b->type)
788 if (a->len != b->len)
790 /* no need to check hw_len. it's calculated from address and len */
794 static void switch_hw_breakpoint(struct task_struct *new)
798 for (i = 0; i < nr_wp_slots(); i++) {
799 if (likely(hw_brk_match(this_cpu_ptr(¤t_brk[i]),
800 &new->thread.hw_brk[i])))
803 __set_breakpoint(i, &new->thread.hw_brk[i]);
806 #endif /* !CONFIG_HAVE_HW_BREAKPOINT */
807 #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
809 static inline int set_dabr(struct arch_hw_breakpoint *brk)
811 unsigned long dabr, dabrx;
813 dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
814 dabrx = ((brk->type >> 3) & 0x7);
817 return ppc_md.set_dabr(dabr, dabrx);
819 if (IS_ENABLED(CONFIG_PPC_ADV_DEBUG_REGS)) {
820 mtspr(SPRN_DAC1, dabr);
821 if (IS_ENABLED(CONFIG_PPC_47x))
824 } else if (IS_ENABLED(CONFIG_PPC_BOOK3S)) {
825 mtspr(SPRN_DABR, dabr);
826 if (cpu_has_feature(CPU_FTR_DABRX))
827 mtspr(SPRN_DABRX, dabrx);
834 static inline int set_breakpoint_8xx(struct arch_hw_breakpoint *brk)
836 unsigned long lctrl1 = LCTRL1_CTE_GT | LCTRL1_CTF_LT | LCTRL1_CRWE_RW |
838 unsigned long lctrl2 = LCTRL2_LW0EN | LCTRL2_LW0LADC | LCTRL2_SLW0EN;
839 unsigned long start_addr = ALIGN_DOWN(brk->address, HW_BREAKPOINT_SIZE);
840 unsigned long end_addr = ALIGN(brk->address + brk->len, HW_BREAKPOINT_SIZE);
843 lctrl2 |= LCTRL2_LW0LA_F;
844 else if (end_addr == 0)
845 lctrl2 |= LCTRL2_LW0LA_E;
847 lctrl2 |= LCTRL2_LW0LA_EandF;
849 mtspr(SPRN_LCTRL2, 0);
851 if ((brk->type & HW_BRK_TYPE_RDWR) == 0)
854 if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_READ)
855 lctrl1 |= LCTRL1_CRWE_RO | LCTRL1_CRWF_RO;
856 if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_WRITE)
857 lctrl1 |= LCTRL1_CRWE_WO | LCTRL1_CRWF_WO;
859 mtspr(SPRN_CMPE, start_addr - 1);
860 mtspr(SPRN_CMPF, end_addr);
861 mtspr(SPRN_LCTRL1, lctrl1);
862 mtspr(SPRN_LCTRL2, lctrl2);
867 void __set_breakpoint(int nr, struct arch_hw_breakpoint *brk)
869 memcpy(this_cpu_ptr(¤t_brk[nr]), brk, sizeof(*brk));
874 else if (IS_ENABLED(CONFIG_PPC_8xx))
875 set_breakpoint_8xx(brk);
876 else if (!cpu_has_feature(CPU_FTR_ARCH_207S))
880 // Shouldn't happen due to higher level checks
884 /* Check if we have DAWR or DABR hardware */
885 bool ppc_breakpoint_available(void)
888 return true; /* POWER8 DAWR or POWER9 forced DAWR */
889 if (cpu_has_feature(CPU_FTR_ARCH_207S))
890 return false; /* POWER9 with DAWR disabled */
891 /* DABR: Everything but POWER8 and POWER9 */
894 EXPORT_SYMBOL_GPL(ppc_breakpoint_available);
896 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
898 static inline bool tm_enabled(struct task_struct *tsk)
900 return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM);
903 static void tm_reclaim_thread(struct thread_struct *thr, uint8_t cause)
906 * Use the current MSR TM suspended bit to track if we have
907 * checkpointed state outstanding.
908 * On signal delivery, we'd normally reclaim the checkpointed
909 * state to obtain stack pointer (see:get_tm_stackpointer()).
910 * This will then directly return to userspace without going
911 * through __switch_to(). However, if the stack frame is bad,
912 * we need to exit this thread which calls __switch_to() which
913 * will again attempt to reclaim the already saved tm state.
914 * Hence we need to check that we've not already reclaimed
916 * We do this using the current MSR, rather tracking it in
917 * some specific thread_struct bit, as it has the additional
918 * benefit of checking for a potential TM bad thing exception.
920 if (!MSR_TM_SUSPENDED(mfmsr()))
923 giveup_all(container_of(thr, struct task_struct, thread));
925 tm_reclaim(thr, cause);
928 * If we are in a transaction and FP is off then we can't have
929 * used FP inside that transaction. Hence the checkpointed
930 * state is the same as the live state. We need to copy the
931 * live state to the checkpointed state so that when the
932 * transaction is restored, the checkpointed state is correct
933 * and the aborted transaction sees the correct state. We use
934 * ckpt_regs.msr here as that's what tm_reclaim will use to
935 * determine if it's going to write the checkpointed state or
936 * not. So either this will write the checkpointed registers,
937 * or reclaim will. Similarly for VMX.
939 if ((thr->ckpt_regs.msr & MSR_FP) == 0)
940 memcpy(&thr->ckfp_state, &thr->fp_state,
941 sizeof(struct thread_fp_state));
942 if ((thr->ckpt_regs.msr & MSR_VEC) == 0)
943 memcpy(&thr->ckvr_state, &thr->vr_state,
944 sizeof(struct thread_vr_state));
947 void tm_reclaim_current(uint8_t cause)
950 tm_reclaim_thread(¤t->thread, cause);
953 static inline void tm_reclaim_task(struct task_struct *tsk)
955 /* We have to work out if we're switching from/to a task that's in the
956 * middle of a transaction.
958 * In switching we need to maintain a 2nd register state as
959 * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
960 * checkpointed (tbegin) state in ckpt_regs, ckfp_state and
963 * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
965 struct thread_struct *thr = &tsk->thread;
970 if (!MSR_TM_ACTIVE(thr->regs->msr))
971 goto out_and_saveregs;
973 WARN_ON(tm_suspend_disabled);
975 TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
976 "ccr=%lx, msr=%lx, trap=%lx)\n",
977 tsk->pid, thr->regs->nip,
978 thr->regs->ccr, thr->regs->msr,
981 tm_reclaim_thread(thr, TM_CAUSE_RESCHED);
983 TM_DEBUG("--- tm_reclaim on pid %d complete\n",
987 /* Always save the regs here, even if a transaction's not active.
988 * This context-switches a thread's TM info SPRs. We do it here to
989 * be consistent with the restore path (in recheckpoint) which
990 * cannot happen later in _switch().
995 extern void __tm_recheckpoint(struct thread_struct *thread);
997 void tm_recheckpoint(struct thread_struct *thread)
1001 if (!(thread->regs->msr & MSR_TM))
1004 /* We really can't be interrupted here as the TEXASR registers can't
1005 * change and later in the trecheckpoint code, we have a userspace R1.
1006 * So let's hard disable over this region.
1008 local_irq_save(flags);
1011 /* The TM SPRs are restored here, so that TEXASR.FS can be set
1012 * before the trecheckpoint and no explosion occurs.
1014 tm_restore_sprs(thread);
1016 __tm_recheckpoint(thread);
1018 local_irq_restore(flags);
1021 static inline void tm_recheckpoint_new_task(struct task_struct *new)
1023 if (!cpu_has_feature(CPU_FTR_TM))
1026 /* Recheckpoint the registers of the thread we're about to switch to.
1028 * If the task was using FP, we non-lazily reload both the original and
1029 * the speculative FP register states. This is because the kernel
1030 * doesn't see if/when a TM rollback occurs, so if we take an FP
1031 * unavailable later, we are unable to determine which set of FP regs
1032 * need to be restored.
1034 if (!tm_enabled(new))
1037 if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
1038 tm_restore_sprs(&new->thread);
1041 /* Recheckpoint to restore original checkpointed register state. */
1042 TM_DEBUG("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n",
1043 new->pid, new->thread.regs->msr);
1045 tm_recheckpoint(&new->thread);
1048 * The checkpointed state has been restored but the live state has
1049 * not, ensure all the math functionality is turned off to trigger
1050 * restore_math() to reload.
1052 new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX);
1054 TM_DEBUG("*** tm_recheckpoint of pid %d complete "
1055 "(kernel msr 0x%lx)\n",
1059 static inline void __switch_to_tm(struct task_struct *prev,
1060 struct task_struct *new)
1062 if (cpu_has_feature(CPU_FTR_TM)) {
1063 if (tm_enabled(prev) || tm_enabled(new))
1066 if (tm_enabled(prev)) {
1067 prev->thread.load_tm++;
1068 tm_reclaim_task(prev);
1069 if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0)
1070 prev->thread.regs->msr &= ~MSR_TM;
1073 tm_recheckpoint_new_task(new);
1078 * This is called if we are on the way out to userspace and the
1079 * TIF_RESTORE_TM flag is set. It checks if we need to reload
1080 * FP and/or vector state and does so if necessary.
1081 * If userspace is inside a transaction (whether active or
1082 * suspended) and FP/VMX/VSX instructions have ever been enabled
1083 * inside that transaction, then we have to keep them enabled
1084 * and keep the FP/VMX/VSX state loaded while ever the transaction
1085 * continues. The reason is that if we didn't, and subsequently
1086 * got a FP/VMX/VSX unavailable interrupt inside a transaction,
1087 * we don't know whether it's the same transaction, and thus we
1088 * don't know which of the checkpointed state and the transactional
1091 void restore_tm_state(struct pt_regs *regs)
1093 unsigned long msr_diff;
1096 * This is the only moment we should clear TIF_RESTORE_TM as
1097 * it is here that ckpt_regs.msr and pt_regs.msr become the same
1098 * again, anything else could lead to an incorrect ckpt_msr being
1099 * saved and therefore incorrect signal contexts.
1101 clear_thread_flag(TIF_RESTORE_TM);
1102 if (!MSR_TM_ACTIVE(regs->msr))
1105 msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
1106 msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
1108 /* Ensure that restore_math() will restore */
1109 if (msr_diff & MSR_FP)
1110 current->thread.load_fp = 1;
1111 #ifdef CONFIG_ALTIVEC
1112 if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC)
1113 current->thread.load_vec = 1;
1117 regs->msr |= msr_diff;
1120 #else /* !CONFIG_PPC_TRANSACTIONAL_MEM */
1121 #define tm_recheckpoint_new_task(new)
1122 #define __switch_to_tm(prev, new)
1123 void tm_reclaim_current(uint8_t cause) {}
1124 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1126 static inline void save_sprs(struct thread_struct *t)
1128 #ifdef CONFIG_ALTIVEC
1129 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1130 t->vrsave = mfspr(SPRN_VRSAVE);
1132 #ifdef CONFIG_PPC_BOOK3S_64
1133 if (cpu_has_feature(CPU_FTR_DSCR))
1134 t->dscr = mfspr(SPRN_DSCR);
1136 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1137 t->bescr = mfspr(SPRN_BESCR);
1138 t->ebbhr = mfspr(SPRN_EBBHR);
1139 t->ebbrr = mfspr(SPRN_EBBRR);
1141 t->fscr = mfspr(SPRN_FSCR);
1144 * Note that the TAR is not available for use in the kernel.
1145 * (To provide this, the TAR should be backed up/restored on
1146 * exception entry/exit instead, and be in pt_regs. FIXME,
1147 * this should be in pt_regs anyway (for debug).)
1149 t->tar = mfspr(SPRN_TAR);
1154 static inline void restore_sprs(struct thread_struct *old_thread,
1155 struct thread_struct *new_thread)
1157 #ifdef CONFIG_ALTIVEC
1158 if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
1159 old_thread->vrsave != new_thread->vrsave)
1160 mtspr(SPRN_VRSAVE, new_thread->vrsave);
1162 #ifdef CONFIG_PPC_BOOK3S_64
1163 if (cpu_has_feature(CPU_FTR_DSCR)) {
1164 u64 dscr = get_paca()->dscr_default;
1165 if (new_thread->dscr_inherit)
1166 dscr = new_thread->dscr;
1168 if (old_thread->dscr != dscr)
1169 mtspr(SPRN_DSCR, dscr);
1172 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
1173 if (old_thread->bescr != new_thread->bescr)
1174 mtspr(SPRN_BESCR, new_thread->bescr);
1175 if (old_thread->ebbhr != new_thread->ebbhr)
1176 mtspr(SPRN_EBBHR, new_thread->ebbhr);
1177 if (old_thread->ebbrr != new_thread->ebbrr)
1178 mtspr(SPRN_EBBRR, new_thread->ebbrr);
1180 if (old_thread->fscr != new_thread->fscr)
1181 mtspr(SPRN_FSCR, new_thread->fscr);
1183 if (old_thread->tar != new_thread->tar)
1184 mtspr(SPRN_TAR, new_thread->tar);
1187 if (cpu_has_feature(CPU_FTR_P9_TIDR) &&
1188 old_thread->tidr != new_thread->tidr)
1189 mtspr(SPRN_TIDR, new_thread->tidr);
1194 struct task_struct *__switch_to(struct task_struct *prev,
1195 struct task_struct *new)
1197 struct thread_struct *new_thread, *old_thread;
1198 struct task_struct *last;
1199 #ifdef CONFIG_PPC_BOOK3S_64
1200 struct ppc64_tlb_batch *batch;
1203 new_thread = &new->thread;
1204 old_thread = ¤t->thread;
1206 WARN_ON(!irqs_disabled());
1208 #ifdef CONFIG_PPC_BOOK3S_64
1209 batch = this_cpu_ptr(&ppc64_tlb_batch);
1210 if (batch->active) {
1211 current_thread_info()->local_flags |= _TLF_LAZY_MMU;
1213 __flush_tlb_pending(batch);
1216 #endif /* CONFIG_PPC_BOOK3S_64 */
1218 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1219 switch_booke_debug_regs(&new->thread.debug);
1222 * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
1225 #ifndef CONFIG_HAVE_HW_BREAKPOINT
1226 switch_hw_breakpoint(new);
1227 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1231 * We need to save SPRs before treclaim/trecheckpoint as these will
1232 * change a number of them.
1234 save_sprs(&prev->thread);
1236 /* Save FPU, Altivec, VSX and SPE state */
1239 __switch_to_tm(prev, new);
1241 if (!radix_enabled()) {
1243 * We can't take a PMU exception inside _switch() since there
1244 * is a window where the kernel stack SLB and the kernel stack
1245 * are out of sync. Hard disable here.
1251 * Call restore_sprs() before calling _switch(). If we move it after
1252 * _switch() then we miss out on calling it for new tasks. The reason
1253 * for this is we manually create a stack frame for new tasks that
1254 * directly returns through ret_from_fork() or
1255 * ret_from_kernel_thread(). See copy_thread() for details.
1257 restore_sprs(old_thread, new_thread);
1260 kuap_assert_locked();
1262 last = _switch(old_thread, new_thread);
1264 #ifdef CONFIG_PPC_BOOK3S_64
1265 if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
1266 current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
1267 batch = this_cpu_ptr(&ppc64_tlb_batch);
1271 if (current->thread.regs) {
1272 restore_math(current->thread.regs);
1275 * On POWER9 the copy-paste buffer can only paste into
1276 * foreign real addresses, so unprivileged processes can not
1277 * see the data or use it in any way unless they have
1278 * foreign real mappings. If the new process has the foreign
1279 * real address mappings, we must issue a cp_abort to clear
1280 * any state and prevent snooping, corruption or a covert
1281 * channel. ISA v3.1 supports paste into local memory.
1284 (cpu_has_feature(CPU_FTR_ARCH_31) ||
1285 atomic_read(¤t->mm->context.vas_windows)))
1286 asm volatile(PPC_CP_ABORT);
1288 #endif /* CONFIG_PPC_BOOK3S_64 */
1293 #define NR_INSN_TO_PRINT 16
1295 static void show_instructions(struct pt_regs *regs)
1298 unsigned long nip = regs->nip;
1299 unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
1301 printk("Instruction dump:");
1304 * If we were executing with the MMU off for instructions, adjust pc
1305 * rather than printing XXXXXXXX.
1307 if (!IS_ENABLED(CONFIG_BOOKE) && !(regs->msr & MSR_IR)) {
1308 pc = (unsigned long)phys_to_virt(pc);
1309 nip = (unsigned long)phys_to_virt(regs->nip);
1312 for (i = 0; i < NR_INSN_TO_PRINT; i++) {
1318 if (!__kernel_text_address(pc) ||
1319 get_kernel_nofault(instr, (const void *)pc)) {
1320 pr_cont("XXXXXXXX ");
1323 pr_cont("<%08x> ", instr);
1325 pr_cont("%08x ", instr);
1334 void show_user_instructions(struct pt_regs *regs)
1337 int n = NR_INSN_TO_PRINT;
1339 char buf[96]; /* enough for 8 times 9 + 2 chars */
1341 pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
1343 seq_buf_init(&s, buf, sizeof(buf));
1350 for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) {
1353 if (copy_from_user_nofault(&instr, (void __user *)pc,
1355 seq_buf_printf(&s, "XXXXXXXX ");
1358 seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr);
1361 if (!seq_buf_has_overflowed(&s))
1362 pr_info("%s[%d]: code: %s\n", current->comm,
1363 current->pid, s.buffer);
1372 static struct regbit msr_bits[] = {
1373 #if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
1395 #ifndef CONFIG_BOOKE
1402 static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
1406 for (; bits->bit; ++bits)
1407 if (val & bits->bit) {
1408 pr_cont("%s%s", s, bits->name);
1413 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1414 static struct regbit msr_tm_bits[] = {
1421 static void print_tm_bits(unsigned long val)
1424 * This only prints something if at least one of the TM bit is set.
1425 * Inside the TM[], the output means:
1426 * E: Enabled (bit 32)
1427 * S: Suspended (bit 33)
1428 * T: Transactional (bit 34)
1430 if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
1432 print_bits(val, msr_tm_bits, "");
1437 static void print_tm_bits(unsigned long val) {}
1440 static void print_msr_bits(unsigned long val)
1443 print_bits(val, msr_bits, ",");
1449 #define REG "%016lx"
1450 #define REGS_PER_LINE 4
1453 #define REGS_PER_LINE 8
1456 static void __show_regs(struct pt_regs *regs)
1460 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
1461 regs->nip, regs->link, regs->ctr);
1462 printk("REGS: %px TRAP: %04lx %s (%s)\n",
1463 regs, regs->trap, print_tainted(), init_utsname()->release);
1464 printk("MSR: "REG" ", regs->msr);
1465 print_msr_bits(regs->msr);
1466 pr_cont(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
1468 if (!trap_is_syscall(regs) && cpu_has_feature(CPU_FTR_CFAR))
1469 pr_cont("CFAR: "REG" ", regs->orig_gpr3);
1470 if (trap == INTERRUPT_MACHINE_CHECK ||
1471 trap == INTERRUPT_DATA_STORAGE ||
1472 trap == INTERRUPT_ALIGNMENT) {
1473 if (IS_ENABLED(CONFIG_4xx) || IS_ENABLED(CONFIG_BOOKE))
1474 pr_cont("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
1476 pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
1480 pr_cont("IRQMASK: %lx ", regs->softe);
1482 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1483 if (MSR_TM_ACTIVE(regs->msr))
1484 pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
1487 for (i = 0; i < 32; i++) {
1488 if ((i % REGS_PER_LINE) == 0)
1489 pr_cont("\nGPR%02d: ", i);
1490 pr_cont(REG " ", regs->gpr[i]);
1494 * Lookup NIP late so we have the best change of getting the
1495 * above info out without failing
1497 if (IS_ENABLED(CONFIG_KALLSYMS)) {
1498 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
1499 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
1503 void show_regs(struct pt_regs *regs)
1505 show_regs_print_info(KERN_DEFAULT);
1507 show_stack(current, (unsigned long *) regs->gpr[1], KERN_DEFAULT);
1508 if (!user_mode(regs))
1509 show_instructions(regs);
1512 void flush_thread(void)
1514 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1515 flush_ptrace_hw_breakpoint(current);
1516 #else /* CONFIG_HAVE_HW_BREAKPOINT */
1517 set_debug_reg_defaults(¤t->thread);
1518 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1521 void arch_setup_new_exec(void)
1524 #ifdef CONFIG_PPC_BOOK3S_64
1525 if (!radix_enabled())
1526 hash__setup_new_exec();
1529 * If we exec out of a kernel thread then thread.regs will not be
1532 if (!current->thread.regs) {
1533 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
1534 current->thread.regs = regs - 1;
1537 #ifdef CONFIG_PPC_MEM_KEYS
1538 current->thread.regs->amr = default_amr;
1539 current->thread.regs->iamr = default_iamr;
1545 * Assign a TIDR (thread ID) for task @t and set it in the thread
1546 * structure. For now, we only support setting TIDR for 'current' task.
1548 * Since the TID value is a truncated form of it PID, it is possible
1549 * (but unlikely) for 2 threads to have the same TID. In the unlikely event
1550 * that 2 threads share the same TID and are waiting, one of the following
1551 * cases will happen:
1553 * 1. The correct thread is running, the wrong thread is not
1554 * In this situation, the correct thread is woken and proceeds to pass it's
1557 * 2. Neither threads are running
1558 * In this situation, neither thread will be woken. When scheduled, the waiting
1559 * threads will execute either a wait, which will return immediately, followed
1560 * by a condition check, which will pass for the correct thread and fail
1561 * for the wrong thread, or they will execute the condition check immediately.
1563 * 3. The wrong thread is running, the correct thread is not
1564 * The wrong thread will be woken, but will fail it's condition check and
1565 * re-execute wait. The correct thread, when scheduled, will execute either
1566 * it's condition check (which will pass), or wait, which returns immediately
1567 * when called the first time after the thread is scheduled, followed by it's
1568 * condition check (which will pass).
1570 * 4. Both threads are running
1571 * Both threads will be woken. The wrong thread will fail it's condition check
1572 * and execute another wait, while the correct thread will pass it's condition
1575 * @t: the task to set the thread ID for
1577 int set_thread_tidr(struct task_struct *t)
1579 if (!cpu_has_feature(CPU_FTR_P9_TIDR))
1588 t->thread.tidr = (u16)task_pid_nr(t);
1589 mtspr(SPRN_TIDR, t->thread.tidr);
1593 EXPORT_SYMBOL_GPL(set_thread_tidr);
1595 #endif /* CONFIG_PPC64 */
1598 release_thread(struct task_struct *t)
1603 * this gets called so that we can store coprocessor state into memory and
1604 * copy the current task into the new thread.
1606 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
1608 flush_all_to_thread(src);
1610 * Flush TM state out so we can copy it. __switch_to_tm() does this
1611 * flush but it removes the checkpointed state from the current CPU and
1612 * transitions the CPU out of TM mode. Hence we need to call
1613 * tm_recheckpoint_new_task() (on the same task) to restore the
1614 * checkpointed state back and the TM mode.
1616 * Can't pass dst because it isn't ready. Doesn't matter, passing
1617 * dst is only important for __switch_to()
1619 __switch_to_tm(src, src);
1623 clear_task_ebb(dst);
1628 static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
1630 #ifdef CONFIG_PPC_BOOK3S_64
1631 unsigned long sp_vsid;
1632 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
1634 if (radix_enabled())
1637 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1638 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
1639 << SLB_VSID_SHIFT_1T;
1641 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
1643 sp_vsid |= SLB_VSID_KERNEL | llp;
1644 p->thread.ksp_vsid = sp_vsid;
1653 * Copy architecture-specific thread state
1655 int copy_thread(unsigned long clone_flags, unsigned long usp,
1656 unsigned long kthread_arg, struct task_struct *p,
1659 struct pt_regs *childregs, *kregs;
1660 extern void ret_from_fork(void);
1661 extern void ret_from_fork_scv(void);
1662 extern void ret_from_kernel_thread(void);
1664 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
1665 struct thread_info *ti = task_thread_info(p);
1666 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1670 klp_init_thread_info(p);
1672 /* Copy registers */
1673 sp -= sizeof(struct pt_regs);
1674 childregs = (struct pt_regs *) sp;
1675 if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) {
1677 memset(childregs, 0, sizeof(struct pt_regs));
1678 childregs->gpr[1] = sp + sizeof(struct pt_regs);
1681 childregs->gpr[14] = ppc_function_entry((void *)usp);
1683 clear_tsk_thread_flag(p, TIF_32BIT);
1684 childregs->softe = IRQS_ENABLED;
1686 childregs->gpr[15] = kthread_arg;
1687 p->thread.regs = NULL; /* no user register state */
1688 ti->flags |= _TIF_RESTOREALL;
1689 f = ret_from_kernel_thread;
1692 struct pt_regs *regs = current_pt_regs();
1695 childregs->gpr[1] = usp;
1696 p->thread.regs = childregs;
1697 /* 64s sets this in ret_from_fork */
1698 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64))
1699 childregs->gpr[3] = 0; /* Result from fork() */
1700 if (clone_flags & CLONE_SETTLS) {
1701 if (!is_32bit_task())
1702 childregs->gpr[13] = tls;
1704 childregs->gpr[2] = tls;
1707 if (trap_is_scv(regs))
1708 f = ret_from_fork_scv;
1712 childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
1713 sp -= STACK_FRAME_OVERHEAD;
1716 * The way this works is that at some point in the future
1717 * some task will call _switch to switch to the new task.
1718 * That will pop off the stack frame created below and start
1719 * the new task running at ret_from_fork. The new task will
1720 * do some house keeping and then return from the fork or clone
1721 * system call, using the stack frame created above.
1723 ((unsigned long *)sp)[0] = 0;
1724 sp -= sizeof(struct pt_regs);
1725 kregs = (struct pt_regs *) sp;
1726 sp -= STACK_FRAME_OVERHEAD;
1728 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1729 for (i = 0; i < nr_wp_slots(); i++)
1730 p->thread.ptrace_bps[i] = NULL;
1733 #ifdef CONFIG_PPC_FPU_REGS
1734 p->thread.fp_save_area = NULL;
1736 #ifdef CONFIG_ALTIVEC
1737 p->thread.vr_save_area = NULL;
1740 setup_ksp_vsid(p, sp);
1743 if (cpu_has_feature(CPU_FTR_DSCR)) {
1744 p->thread.dscr_inherit = current->thread.dscr_inherit;
1745 p->thread.dscr = mfspr(SPRN_DSCR);
1747 if (cpu_has_feature(CPU_FTR_HAS_PPR))
1748 childregs->ppr = DEFAULT_PPR;
1753 * Run with the current AMR value of the kernel
1755 #ifdef CONFIG_PPC_PKEY
1756 if (mmu_has_feature(MMU_FTR_BOOK3S_KUAP))
1757 kregs->amr = AMR_KUAP_BLOCKED;
1759 if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP))
1760 kregs->iamr = AMR_KUEP_BLOCKED;
1762 kregs->nip = ppc_function_entry(f);
1766 void preload_new_slb_context(unsigned long start, unsigned long sp);
1769 * Set up a thread for executing a new program
1771 void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
1774 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
1776 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled())
1777 preload_new_slb_context(start, sp);
1780 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1782 * Clear any transactional state, we're exec()ing. The cause is
1783 * not important as there will never be a recheckpoint so it's not
1786 if (MSR_TM_SUSPENDED(mfmsr()))
1787 tm_reclaim_current(0);
1790 memset(regs->gpr, 0, sizeof(regs->gpr));
1800 regs->msr = MSR_USER;
1802 if (!is_32bit_task()) {
1803 unsigned long entry;
1805 if (is_elf2_task()) {
1806 /* Look ma, no function descriptors! */
1811 * The latest iteration of the ABI requires that when
1812 * calling a function (at its global entry point),
1813 * the caller must ensure r12 holds the entry point
1814 * address (so that the function can quickly
1815 * establish addressability).
1817 regs->gpr[12] = start;
1818 /* Make sure that's restored on entry to userspace. */
1819 set_thread_flag(TIF_RESTOREALL);
1823 /* start is a relocated pointer to the function
1824 * descriptor for the elf _start routine. The first
1825 * entry in the function descriptor is the entry
1826 * address of _start and the second entry is the TOC
1827 * value we need to use.
1829 __get_user(entry, (unsigned long __user *)start);
1830 __get_user(toc, (unsigned long __user *)start+1);
1832 /* Check whether the e_entry function descriptor entries
1833 * need to be relocated before we can use them.
1835 if (load_addr != 0) {
1842 regs->msr = MSR_USER64;
1846 regs->msr = MSR_USER32;
1850 current->thread.used_vsr = 0;
1852 current->thread.load_slb = 0;
1853 current->thread.load_fp = 0;
1854 #ifdef CONFIG_PPC_FPU_REGS
1855 memset(¤t->thread.fp_state, 0, sizeof(current->thread.fp_state));
1856 current->thread.fp_save_area = NULL;
1858 #ifdef CONFIG_ALTIVEC
1859 memset(¤t->thread.vr_state, 0, sizeof(current->thread.vr_state));
1860 current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
1861 current->thread.vr_save_area = NULL;
1862 current->thread.vrsave = 0;
1863 current->thread.used_vr = 0;
1864 current->thread.load_vec = 0;
1865 #endif /* CONFIG_ALTIVEC */
1867 memset(current->thread.evr, 0, sizeof(current->thread.evr));
1868 current->thread.acc = 0;
1869 current->thread.spefscr = 0;
1870 current->thread.used_spe = 0;
1871 #endif /* CONFIG_SPE */
1872 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1873 current->thread.tm_tfhar = 0;
1874 current->thread.tm_texasr = 0;
1875 current->thread.tm_tfiar = 0;
1876 current->thread.load_tm = 0;
1877 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1880 EXPORT_SYMBOL(start_thread);
1882 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
1883 | PR_FP_EXC_RES | PR_FP_EXC_INV)
1885 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
1887 struct pt_regs *regs = tsk->thread.regs;
1889 /* This is a bit hairy. If we are an SPE enabled processor
1890 * (have embedded fp) we store the IEEE exception enable flags in
1891 * fpexc_mode. fpexc_mode is also used for setting FP exception
1892 * mode (asyn, precise, disabled) for 'Classic' FP. */
1893 if (val & PR_FP_EXC_SW_ENABLE) {
1894 if (cpu_has_feature(CPU_FTR_SPE)) {
1896 * When the sticky exception bits are set
1897 * directly by userspace, it must call prctl
1898 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1899 * in the existing prctl settings) or
1900 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1901 * the bits being set). <fenv.h> functions
1902 * saving and restoring the whole
1903 * floating-point environment need to do so
1904 * anyway to restore the prctl settings from
1905 * the saved environment.
1908 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
1909 tsk->thread.fpexc_mode = val &
1910 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
1918 /* on a CONFIG_SPE this does not hurt us. The bits that
1919 * __pack_fe01 use do not overlap with bits used for
1920 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
1921 * on CONFIG_SPE implementations are reserved so writing to
1922 * them does not change anything */
1923 if (val > PR_FP_EXC_PRECISE)
1925 tsk->thread.fpexc_mode = __pack_fe01(val);
1926 if (regs != NULL && (regs->msr & MSR_FP) != 0)
1927 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
1928 | tsk->thread.fpexc_mode;
1932 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
1934 unsigned int val = 0;
1936 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE) {
1937 if (cpu_has_feature(CPU_FTR_SPE)) {
1939 * When the sticky exception bits are set
1940 * directly by userspace, it must call prctl
1941 * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
1942 * in the existing prctl settings) or
1943 * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
1944 * the bits being set). <fenv.h> functions
1945 * saving and restoring the whole
1946 * floating-point environment need to do so
1947 * anyway to restore the prctl settings from
1948 * the saved environment.
1951 tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
1952 val = tsk->thread.fpexc_mode;
1957 val = __unpack_fe01(tsk->thread.fpexc_mode);
1959 return put_user(val, (unsigned int __user *) adr);
1962 int set_endian(struct task_struct *tsk, unsigned int val)
1964 struct pt_regs *regs = tsk->thread.regs;
1966 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
1967 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
1973 if (val == PR_ENDIAN_BIG)
1974 regs->msr &= ~MSR_LE;
1975 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
1976 regs->msr |= MSR_LE;
1983 int get_endian(struct task_struct *tsk, unsigned long adr)
1985 struct pt_regs *regs = tsk->thread.regs;
1988 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
1989 !cpu_has_feature(CPU_FTR_REAL_LE))
1995 if (regs->msr & MSR_LE) {
1996 if (cpu_has_feature(CPU_FTR_REAL_LE))
1997 val = PR_ENDIAN_LITTLE;
1999 val = PR_ENDIAN_PPC_LITTLE;
2001 val = PR_ENDIAN_BIG;
2003 return put_user(val, (unsigned int __user *)adr);
2006 int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
2008 tsk->thread.align_ctl = val;
2012 int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
2014 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
2017 static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
2018 unsigned long nbytes)
2020 unsigned long stack_page;
2021 unsigned long cpu = task_cpu(p);
2023 stack_page = (unsigned long)hardirq_ctx[cpu];
2024 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2027 stack_page = (unsigned long)softirq_ctx[cpu];
2028 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2034 static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p,
2035 unsigned long nbytes)
2038 unsigned long stack_page;
2039 unsigned long cpu = task_cpu(p);
2044 stack_page = (unsigned long)paca_ptrs[cpu]->emergency_sp - THREAD_SIZE;
2045 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2048 # ifdef CONFIG_PPC_BOOK3S_64
2049 stack_page = (unsigned long)paca_ptrs[cpu]->nmi_emergency_sp - THREAD_SIZE;
2050 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2053 stack_page = (unsigned long)paca_ptrs[cpu]->mc_emergency_sp - THREAD_SIZE;
2054 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2063 int validate_sp(unsigned long sp, struct task_struct *p,
2064 unsigned long nbytes)
2066 unsigned long stack_page = (unsigned long)task_stack_page(p);
2068 if (sp < THREAD_SIZE)
2071 if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
2074 if (valid_irq_stack(sp, p, nbytes))
2077 return valid_emergency_stack(sp, p, nbytes);
2080 EXPORT_SYMBOL(validate_sp);
2082 static unsigned long __get_wchan(struct task_struct *p)
2084 unsigned long ip, sp;
2087 if (!p || p == current || p->state == TASK_RUNNING)
2091 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
2095 sp = *(unsigned long *)sp;
2096 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD) ||
2097 p->state == TASK_RUNNING)
2100 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
2101 if (!in_sched_functions(ip))
2104 } while (count++ < 16);
2108 unsigned long get_wchan(struct task_struct *p)
2112 if (!try_get_task_stack(p))
2115 ret = __get_wchan(p);
2122 static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
2124 void show_stack(struct task_struct *tsk, unsigned long *stack,
2127 unsigned long sp, ip, lr, newsp;
2130 unsigned long ret_addr;
2136 if (!try_get_task_stack(tsk))
2139 sp = (unsigned long) stack;
2142 sp = current_stack_frame();
2144 sp = tsk->thread.ksp;
2148 printk("%sCall Trace:\n", loglvl);
2150 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
2153 stack = (unsigned long *) sp;
2155 ip = stack[STACK_FRAME_LR_SAVE];
2156 if (!firstframe || ip != lr) {
2157 printk("%s["REG"] ["REG"] %pS",
2158 loglvl, sp, ip, (void *)ip);
2159 ret_addr = ftrace_graph_ret_addr(current,
2160 &ftrace_idx, ip, stack);
2162 pr_cont(" (%pS)", (void *)ret_addr);
2164 pr_cont(" (unreliable)");
2170 * See if this is an exception frame.
2171 * We look for the "regshere" marker in the current frame.
2173 if (validate_sp(sp, tsk, STACK_FRAME_WITH_PT_REGS)
2174 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
2175 struct pt_regs *regs = (struct pt_regs *)
2176 (sp + STACK_FRAME_OVERHEAD);
2179 printk("%s--- interrupt: %lx at %pS\n",
2180 loglvl, regs->trap, (void *)regs->nip);
2182 printk("%s--- interrupt: %lx\n",
2183 loglvl, regs->trap);
2189 } while (count++ < kstack_depth_to_print);
2191 put_task_stack(tsk);
2195 /* Called with hard IRQs off */
2196 void notrace __ppc64_runlatch_on(void)
2198 struct thread_info *ti = current_thread_info();
2200 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2202 * Least significant bit (RUN) is the only writable bit of
2203 * the CTRL register, so we can avoid mfspr. 2.06 is not the
2204 * earliest ISA where this is the case, but it's convenient.
2206 mtspr(SPRN_CTRLT, CTRL_RUNLATCH);
2211 * Some architectures (e.g., Cell) have writable fields other
2212 * than RUN, so do the read-modify-write.
2214 ctrl = mfspr(SPRN_CTRLF);
2215 ctrl |= CTRL_RUNLATCH;
2216 mtspr(SPRN_CTRLT, ctrl);
2219 ti->local_flags |= _TLF_RUNLATCH;
2222 /* Called with hard IRQs off */
2223 void notrace __ppc64_runlatch_off(void)
2225 struct thread_info *ti = current_thread_info();
2227 ti->local_flags &= ~_TLF_RUNLATCH;
2229 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
2230 mtspr(SPRN_CTRLT, 0);
2234 ctrl = mfspr(SPRN_CTRLF);
2235 ctrl &= ~CTRL_RUNLATCH;
2236 mtspr(SPRN_CTRLT, ctrl);
2239 #endif /* CONFIG_PPC64 */
2241 unsigned long arch_align_stack(unsigned long sp)
2243 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
2244 sp -= get_random_int() & ~PAGE_MASK;
2248 static inline unsigned long brk_rnd(void)
2250 unsigned long rnd = 0;
2252 /* 8MB for 32bit, 1GB for 64bit */
2253 if (is_32bit_task())
2254 rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT)));
2256 rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT)));
2258 return rnd << PAGE_SHIFT;
2261 unsigned long arch_randomize_brk(struct mm_struct *mm)
2263 unsigned long base = mm->brk;
2266 #ifdef CONFIG_PPC_BOOK3S_64
2268 * If we are using 1TB segments and we are allowed to randomise
2269 * the heap, we can put it above 1TB so it is backed by a 1TB
2270 * segment. Otherwise the heap will be in the bottom 1TB
2271 * which always uses 256MB segments and this may result in a
2272 * performance penalty. We don't need to worry about radix. For
2273 * radix, mmu_highuser_ssize remains unchanged from 256MB.
2275 if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
2276 base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
2279 ret = PAGE_ALIGN(base + brk_rnd());