2 * Derived from "arch/i386/kernel/process.c"
3 * Copyright (C) 1995 Linus Torvalds
5 * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
6 * Paul Mackerras (paulus@cs.anu.edu.au)
9 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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.
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
21 #include <linux/smp.h>
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>
28 #include <linux/init.h>
29 #include <linux/prctl.h>
30 #include <linux/init_task.h>
31 #include <linux/module.h>
32 #include <linux/kallsyms.h>
33 #include <linux/mqueue.h>
34 #include <linux/hardirq.h>
35 #include <linux/utsname.h>
37 #include <asm/pgtable.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
41 #include <asm/processor.h>
44 #include <asm/machdep.h>
46 #include <asm/syscalls.h>
48 #include <asm/firmware.h>
51 extern unsigned long _get_SP(void);
54 struct task_struct *last_task_used_math = NULL;
55 struct task_struct *last_task_used_altivec = NULL;
56 struct task_struct *last_task_used_vsx = NULL;
57 struct task_struct *last_task_used_spe = NULL;
61 * Make sure the floating-point register state in the
62 * the thread_struct is up to date for task tsk.
64 void flush_fp_to_thread(struct task_struct *tsk)
66 if (tsk->thread.regs) {
68 * We need to disable preemption here because if we didn't,
69 * another process could get scheduled after the regs->msr
70 * test but before we have finished saving the FP registers
71 * to the thread_struct. That process could take over the
72 * FPU, and then when we get scheduled again we would store
73 * bogus values for the remaining FP registers.
76 if (tsk->thread.regs->msr & MSR_FP) {
79 * This should only ever be called for current or
80 * for a stopped child process. Since we save away
81 * the FP register state on context switch on SMP,
82 * there is something wrong if a stopped child appears
83 * to still have its FP state in the CPU registers.
85 BUG_ON(tsk != current);
93 void enable_kernel_fp(void)
95 WARN_ON(preemptible());
98 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
101 giveup_fpu(NULL); /* just enables FP for kernel */
103 giveup_fpu(last_task_used_math);
104 #endif /* CONFIG_SMP */
106 EXPORT_SYMBOL(enable_kernel_fp);
108 #ifdef CONFIG_ALTIVEC
109 void enable_kernel_altivec(void)
111 WARN_ON(preemptible());
114 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
115 giveup_altivec(current);
117 giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
119 giveup_altivec(last_task_used_altivec);
120 #endif /* CONFIG_SMP */
122 EXPORT_SYMBOL(enable_kernel_altivec);
125 * Make sure the VMX/Altivec register state in the
126 * the thread_struct is up to date for task tsk.
128 void flush_altivec_to_thread(struct task_struct *tsk)
130 if (tsk->thread.regs) {
132 if (tsk->thread.regs->msr & MSR_VEC) {
134 BUG_ON(tsk != current);
141 #endif /* CONFIG_ALTIVEC */
145 /* not currently used, but some crazy RAID module might want to later */
146 void enable_kernel_vsx(void)
148 WARN_ON(preemptible());
151 if (current->thread.regs && (current->thread.regs->msr & MSR_VSX))
154 giveup_vsx(NULL); /* just enable vsx for kernel - force */
156 giveup_vsx(last_task_used_vsx);
157 #endif /* CONFIG_SMP */
159 EXPORT_SYMBOL(enable_kernel_vsx);
162 void flush_vsx_to_thread(struct task_struct *tsk)
164 if (tsk->thread.regs) {
166 if (tsk->thread.regs->msr & MSR_VSX) {
168 BUG_ON(tsk != current);
175 #endif /* CONFIG_VSX */
179 void enable_kernel_spe(void)
181 WARN_ON(preemptible());
184 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
187 giveup_spe(NULL); /* just enable SPE for kernel - force */
189 giveup_spe(last_task_used_spe);
190 #endif /* __SMP __ */
192 EXPORT_SYMBOL(enable_kernel_spe);
194 void flush_spe_to_thread(struct task_struct *tsk)
196 if (tsk->thread.regs) {
198 if (tsk->thread.regs->msr & MSR_SPE) {
200 BUG_ON(tsk != current);
207 #endif /* CONFIG_SPE */
211 * If we are doing lazy switching of CPU state (FP, altivec or SPE),
212 * and the current task has some state, discard it.
214 void discard_lazy_cpu_state(void)
217 if (last_task_used_math == current)
218 last_task_used_math = NULL;
219 #ifdef CONFIG_ALTIVEC
220 if (last_task_used_altivec == current)
221 last_task_used_altivec = NULL;
222 #endif /* CONFIG_ALTIVEC */
224 if (last_task_used_vsx == current)
225 last_task_used_vsx = NULL;
226 #endif /* CONFIG_VSX */
228 if (last_task_used_spe == current)
229 last_task_used_spe = NULL;
233 #endif /* CONFIG_SMP */
235 static DEFINE_PER_CPU(unsigned long, current_dabr);
237 int set_dabr(unsigned long dabr)
239 __get_cpu_var(current_dabr) = dabr;
241 #ifdef CONFIG_PPC_MERGE /* XXX for now */
243 return ppc_md.set_dabr(dabr);
246 /* XXX should we have a CPU_FTR_HAS_DABR ? */
247 #if defined(CONFIG_PPC64) || defined(CONFIG_6xx)
248 mtspr(SPRN_DABR, dabr);
254 DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
257 struct task_struct *__switch_to(struct task_struct *prev,
258 struct task_struct *new)
260 struct thread_struct *new_thread, *old_thread;
262 struct task_struct *last;
265 /* avoid complexity of lazy save/restore of fpu
266 * by just saving it every time we switch out if
267 * this task used the fpu during the last quantum.
269 * If it tries to use the fpu again, it'll trap and
270 * reload its fp regs. So we don't have to do a restore
271 * every switch, just a save.
274 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
276 #ifdef CONFIG_ALTIVEC
278 * If the previous thread used altivec in the last quantum
279 * (thus changing altivec regs) then save them.
280 * We used to check the VRSAVE register but not all apps
281 * set it, so we don't rely on it now (and in fact we need
282 * to save & restore VSCR even if VRSAVE == 0). -- paulus
284 * On SMP we always save/restore altivec regs just to avoid the
285 * complexity of changing processors.
288 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
289 giveup_altivec(prev);
290 #endif /* CONFIG_ALTIVEC */
292 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
294 #endif /* CONFIG_VSX */
297 * If the previous thread used spe in the last quantum
298 * (thus changing spe regs) then save them.
300 * On SMP we always save/restore spe regs just to avoid the
301 * complexity of changing processors.
303 if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
305 #endif /* CONFIG_SPE */
307 #else /* CONFIG_SMP */
308 #ifdef CONFIG_ALTIVEC
309 /* Avoid the trap. On smp this this never happens since
310 * we don't set last_task_used_altivec -- Cort
312 if (new->thread.regs && last_task_used_altivec == new)
313 new->thread.regs->msr |= MSR_VEC;
314 #endif /* CONFIG_ALTIVEC */
316 if (new->thread.regs && last_task_used_vsx == new)
317 new->thread.regs->msr |= MSR_VSX;
318 #endif /* CONFIG_VSX */
320 /* Avoid the trap. On smp this this never happens since
321 * we don't set last_task_used_spe
323 if (new->thread.regs && last_task_used_spe == new)
324 new->thread.regs->msr |= MSR_SPE;
325 #endif /* CONFIG_SPE */
327 #endif /* CONFIG_SMP */
329 if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr))
330 set_dabr(new->thread.dabr);
332 new_thread = &new->thread;
333 old_thread = ¤t->thread;
337 * Collect processor utilization data per process
339 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
340 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
341 long unsigned start_tb, current_tb;
342 start_tb = old_thread->start_tb;
343 cu->current_tb = current_tb = mfspr(SPRN_PURR);
344 old_thread->accum_tb += (current_tb - start_tb);
345 new_thread->start_tb = current_tb;
349 local_irq_save(flags);
351 account_system_vtime(current);
352 account_process_vtime(current);
353 calculate_steal_time();
356 * We can't take a PMU exception inside _switch() since there is a
357 * window where the kernel stack SLB and the kernel stack are out
358 * of sync. Hard disable here.
361 last = _switch(old_thread, new_thread);
363 local_irq_restore(flags);
368 static int instructions_to_print = 16;
370 static void show_instructions(struct pt_regs *regs)
373 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
376 printk("Instruction dump:");
378 for (i = 0; i < instructions_to_print; i++) {
384 #if !defined(CONFIG_BOOKE)
385 /* If executing with the IMMU off, adjust pc rather
386 * than print XXXXXXXX.
388 if (!(regs->msr & MSR_IR))
389 pc = (unsigned long)phys_to_virt(pc);
392 /* We use __get_user here *only* to avoid an OOPS on a
393 * bad address because the pc *should* only be a
396 if (!__kernel_text_address(pc) ||
397 __get_user(instr, (unsigned int __user *)pc)) {
401 printk("<%08x> ", instr);
403 printk("%08x ", instr);
412 static struct regbit {
427 static void printbits(unsigned long val, struct regbit *bits)
429 const char *sep = "";
432 for (; bits->bit; ++bits)
433 if (val & bits->bit) {
434 printk("%s%s", sep, bits->name);
442 #define REGS_PER_LINE 4
443 #define LAST_VOLATILE 13
446 #define REGS_PER_LINE 8
447 #define LAST_VOLATILE 12
450 void show_regs(struct pt_regs * regs)
454 printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
455 regs->nip, regs->link, regs->ctr);
456 printk("REGS: %p TRAP: %04lx %s (%s)\n",
457 regs, regs->trap, print_tainted(), init_utsname()->release);
458 printk("MSR: "REG" ", regs->msr);
459 printbits(regs->msr, msr_bits);
460 printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
462 if (trap == 0x300 || trap == 0x600)
463 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
464 printk("DEAR: "REG", ESR: "REG"\n", regs->dar, regs->dsisr);
466 printk("DAR: "REG", DSISR: "REG"\n", regs->dar, regs->dsisr);
468 printk("TASK = %p[%d] '%s' THREAD: %p",
469 current, task_pid_nr(current), current->comm, task_thread_info(current));
472 printk(" CPU: %d", raw_smp_processor_id());
473 #endif /* CONFIG_SMP */
475 for (i = 0; i < 32; i++) {
476 if ((i % REGS_PER_LINE) == 0)
477 printk("\n" KERN_INFO "GPR%02d: ", i);
478 printk(REG " ", regs->gpr[i]);
479 if (i == LAST_VOLATILE && !FULL_REGS(regs))
483 #ifdef CONFIG_KALLSYMS
485 * Lookup NIP late so we have the best change of getting the
486 * above info out without failing
488 printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
489 printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
491 show_stack(current, (unsigned long *) regs->gpr[1]);
492 if (!user_mode(regs))
493 show_instructions(regs);
496 void exit_thread(void)
498 discard_lazy_cpu_state();
501 void flush_thread(void)
504 struct thread_info *t = current_thread_info();
506 if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
507 clear_ti_thread_flag(t, TIF_ABI_PENDING);
508 if (test_ti_thread_flag(t, TIF_32BIT))
509 clear_ti_thread_flag(t, TIF_32BIT);
511 set_ti_thread_flag(t, TIF_32BIT);
515 discard_lazy_cpu_state();
517 if (current->thread.dabr) {
518 current->thread.dabr = 0;
524 release_thread(struct task_struct *t)
529 * This gets called before we allocate a new thread and copy
530 * the current task into it.
532 void prepare_to_copy(struct task_struct *tsk)
534 flush_fp_to_thread(current);
535 flush_altivec_to_thread(current);
536 flush_vsx_to_thread(current);
537 flush_spe_to_thread(current);
543 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
544 unsigned long unused, struct task_struct *p,
545 struct pt_regs *regs)
547 struct pt_regs *childregs, *kregs;
548 extern void ret_from_fork(void);
549 unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
551 CHECK_FULL_REGS(regs);
553 sp -= sizeof(struct pt_regs);
554 childregs = (struct pt_regs *) sp;
556 if ((childregs->msr & MSR_PR) == 0) {
557 /* for kernel thread, set `current' and stackptr in new task */
558 childregs->gpr[1] = sp + sizeof(struct pt_regs);
560 childregs->gpr[2] = (unsigned long) p;
562 clear_tsk_thread_flag(p, TIF_32BIT);
564 p->thread.regs = NULL; /* no user register state */
566 childregs->gpr[1] = usp;
567 p->thread.regs = childregs;
568 if (clone_flags & CLONE_SETTLS) {
570 if (!test_thread_flag(TIF_32BIT))
571 childregs->gpr[13] = childregs->gpr[6];
574 childregs->gpr[2] = childregs->gpr[6];
577 childregs->gpr[3] = 0; /* Result from fork() */
578 sp -= STACK_FRAME_OVERHEAD;
581 * The way this works is that at some point in the future
582 * some task will call _switch to switch to the new task.
583 * That will pop off the stack frame created below and start
584 * the new task running at ret_from_fork. The new task will
585 * do some house keeping and then return from the fork or clone
586 * system call, using the stack frame created above.
588 sp -= sizeof(struct pt_regs);
589 kregs = (struct pt_regs *) sp;
590 sp -= STACK_FRAME_OVERHEAD;
592 p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
593 _ALIGN_UP(sizeof(struct thread_info), 16);
596 if (cpu_has_feature(CPU_FTR_SLB)) {
597 unsigned long sp_vsid;
598 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
600 if (cpu_has_feature(CPU_FTR_1T_SEGMENT))
601 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
602 << SLB_VSID_SHIFT_1T;
604 sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
606 sp_vsid |= SLB_VSID_KERNEL | llp;
607 p->thread.ksp_vsid = sp_vsid;
611 * The PPC64 ABI makes use of a TOC to contain function
612 * pointers. The function (ret_from_except) is actually a pointer
613 * to the TOC entry. The first entry is a pointer to the actual
616 kregs->nip = *((unsigned long *)ret_from_fork);
618 kregs->nip = (unsigned long)ret_from_fork;
625 * Set up a thread for executing a new program
627 void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
630 unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
636 * If we exec out of a kernel thread then thread.regs will not be
639 if (!current->thread.regs) {
640 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
641 current->thread.regs = regs - 1;
644 memset(regs->gpr, 0, sizeof(regs->gpr));
652 * We have just cleared all the nonvolatile GPRs, so make
653 * FULL_REGS(regs) return true. This is necessary to allow
654 * ptrace to examine the thread immediately after exec.
661 regs->msr = MSR_USER;
663 if (!test_thread_flag(TIF_32BIT)) {
664 unsigned long entry, toc;
666 /* start is a relocated pointer to the function descriptor for
667 * the elf _start routine. The first entry in the function
668 * descriptor is the entry address of _start and the second
669 * entry is the TOC value we need to use.
671 __get_user(entry, (unsigned long __user *)start);
672 __get_user(toc, (unsigned long __user *)start+1);
674 /* Check whether the e_entry function descriptor entries
675 * need to be relocated before we can use them.
677 if (load_addr != 0) {
683 regs->msr = MSR_USER64;
687 regs->msr = MSR_USER32;
691 discard_lazy_cpu_state();
693 current->thread.used_vsr = 0;
695 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
696 current->thread.fpscr.val = 0;
697 #ifdef CONFIG_ALTIVEC
698 memset(current->thread.vr, 0, sizeof(current->thread.vr));
699 memset(¤t->thread.vscr, 0, sizeof(current->thread.vscr));
700 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
701 current->thread.vrsave = 0;
702 current->thread.used_vr = 0;
703 #endif /* CONFIG_ALTIVEC */
705 memset(current->thread.evr, 0, sizeof(current->thread.evr));
706 current->thread.acc = 0;
707 current->thread.spefscr = 0;
708 current->thread.used_spe = 0;
709 #endif /* CONFIG_SPE */
712 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
713 | PR_FP_EXC_RES | PR_FP_EXC_INV)
715 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
717 struct pt_regs *regs = tsk->thread.regs;
719 /* This is a bit hairy. If we are an SPE enabled processor
720 * (have embedded fp) we store the IEEE exception enable flags in
721 * fpexc_mode. fpexc_mode is also used for setting FP exception
722 * mode (asyn, precise, disabled) for 'Classic' FP. */
723 if (val & PR_FP_EXC_SW_ENABLE) {
725 if (cpu_has_feature(CPU_FTR_SPE)) {
726 tsk->thread.fpexc_mode = val &
727 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
737 /* on a CONFIG_SPE this does not hurt us. The bits that
738 * __pack_fe01 use do not overlap with bits used for
739 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
740 * on CONFIG_SPE implementations are reserved so writing to
741 * them does not change anything */
742 if (val > PR_FP_EXC_PRECISE)
744 tsk->thread.fpexc_mode = __pack_fe01(val);
745 if (regs != NULL && (regs->msr & MSR_FP) != 0)
746 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
747 | tsk->thread.fpexc_mode;
751 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
755 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
757 if (cpu_has_feature(CPU_FTR_SPE))
758 val = tsk->thread.fpexc_mode;
765 val = __unpack_fe01(tsk->thread.fpexc_mode);
766 return put_user(val, (unsigned int __user *) adr);
769 int set_endian(struct task_struct *tsk, unsigned int val)
771 struct pt_regs *regs = tsk->thread.regs;
773 if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
774 (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
780 if (val == PR_ENDIAN_BIG)
781 regs->msr &= ~MSR_LE;
782 else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
790 int get_endian(struct task_struct *tsk, unsigned long adr)
792 struct pt_regs *regs = tsk->thread.regs;
795 if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
796 !cpu_has_feature(CPU_FTR_REAL_LE))
802 if (regs->msr & MSR_LE) {
803 if (cpu_has_feature(CPU_FTR_REAL_LE))
804 val = PR_ENDIAN_LITTLE;
806 val = PR_ENDIAN_PPC_LITTLE;
810 return put_user(val, (unsigned int __user *)adr);
813 int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
815 tsk->thread.align_ctl = val;
819 int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
821 return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
824 #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
826 int sys_clone(unsigned long clone_flags, unsigned long usp,
827 int __user *parent_tidp, void __user *child_threadptr,
828 int __user *child_tidp, int p6,
829 struct pt_regs *regs)
831 CHECK_FULL_REGS(regs);
833 usp = regs->gpr[1]; /* stack pointer for child */
835 if (test_thread_flag(TIF_32BIT)) {
836 parent_tidp = TRUNC_PTR(parent_tidp);
837 child_tidp = TRUNC_PTR(child_tidp);
840 return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
843 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
844 unsigned long p4, unsigned long p5, unsigned long p6,
845 struct pt_regs *regs)
847 CHECK_FULL_REGS(regs);
848 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
851 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
852 unsigned long p4, unsigned long p5, unsigned long p6,
853 struct pt_regs *regs)
855 CHECK_FULL_REGS(regs);
856 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
857 regs, 0, NULL, NULL);
860 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
861 unsigned long a3, unsigned long a4, unsigned long a5,
862 struct pt_regs *regs)
867 filename = getname((char __user *) a0);
868 error = PTR_ERR(filename);
869 if (IS_ERR(filename))
871 flush_fp_to_thread(current);
872 flush_altivec_to_thread(current);
873 flush_spe_to_thread(current);
874 error = do_execve(filename, (char __user * __user *) a1,
875 (char __user * __user *) a2, regs);
881 #ifdef CONFIG_IRQSTACKS
882 static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
883 unsigned long nbytes)
885 unsigned long stack_page;
886 unsigned long cpu = task_cpu(p);
889 * Avoid crashing if the stack has overflowed and corrupted
890 * task_cpu(p), which is in the thread_info struct.
892 if (cpu < NR_CPUS && cpu_possible(cpu)) {
893 stack_page = (unsigned long) hardirq_ctx[cpu];
894 if (sp >= stack_page + sizeof(struct thread_struct)
895 && sp <= stack_page + THREAD_SIZE - nbytes)
898 stack_page = (unsigned long) softirq_ctx[cpu];
899 if (sp >= stack_page + sizeof(struct thread_struct)
900 && sp <= stack_page + THREAD_SIZE - nbytes)
907 #define valid_irq_stack(sp, p, nb) 0
908 #endif /* CONFIG_IRQSTACKS */
910 int validate_sp(unsigned long sp, struct task_struct *p,
911 unsigned long nbytes)
913 unsigned long stack_page = (unsigned long)task_stack_page(p);
915 if (sp >= stack_page + sizeof(struct thread_struct)
916 && sp <= stack_page + THREAD_SIZE - nbytes)
919 return valid_irq_stack(sp, p, nbytes);
922 EXPORT_SYMBOL(validate_sp);
924 unsigned long get_wchan(struct task_struct *p)
926 unsigned long ip, sp;
929 if (!p || p == current || p->state == TASK_RUNNING)
933 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
937 sp = *(unsigned long *)sp;
938 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
941 ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
942 if (!in_sched_functions(ip))
945 } while (count++ < 16);
949 static int kstack_depth_to_print = 64;
951 void show_stack(struct task_struct *tsk, unsigned long *stack)
953 unsigned long sp, ip, lr, newsp;
957 sp = (unsigned long) stack;
962 asm("mr %0,1" : "=r" (sp));
964 sp = tsk->thread.ksp;
968 printk("Call Trace:\n");
970 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
973 stack = (unsigned long *) sp;
975 ip = stack[STACK_FRAME_LR_SAVE];
976 if (!firstframe || ip != lr) {
977 printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
979 printk(" (unreliable)");
985 * See if this is an exception frame.
986 * We look for the "regshere" marker in the current frame.
988 if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
989 && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
990 struct pt_regs *regs = (struct pt_regs *)
991 (sp + STACK_FRAME_OVERHEAD);
993 printk("--- Exception: %lx at %pS\n LR = %pS\n",
994 regs->trap, (void *)regs->nip, (void *)lr);
999 } while (count++ < kstack_depth_to_print);
1002 void dump_stack(void)
1004 show_stack(current, NULL);
1006 EXPORT_SYMBOL(dump_stack);
1009 void ppc64_runlatch_on(void)
1013 if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
1016 ctrl = mfspr(SPRN_CTRLF);
1017 ctrl |= CTRL_RUNLATCH;
1018 mtspr(SPRN_CTRLT, ctrl);
1020 set_thread_flag(TIF_RUNLATCH);
1024 void ppc64_runlatch_off(void)
1028 if (cpu_has_feature(CPU_FTR_CTRL) && test_thread_flag(TIF_RUNLATCH)) {
1031 clear_thread_flag(TIF_RUNLATCH);
1033 ctrl = mfspr(SPRN_CTRLF);
1034 ctrl &= ~CTRL_RUNLATCH;
1035 mtspr(SPRN_CTRLT, ctrl);
1040 #if THREAD_SHIFT < PAGE_SHIFT
1042 static struct kmem_cache *thread_info_cache;
1044 struct thread_info *alloc_thread_info(struct task_struct *tsk)
1046 struct thread_info *ti;
1048 ti = kmem_cache_alloc(thread_info_cache, GFP_KERNEL);
1049 if (unlikely(ti == NULL))
1051 #ifdef CONFIG_DEBUG_STACK_USAGE
1052 memset(ti, 0, THREAD_SIZE);
1057 void free_thread_info(struct thread_info *ti)
1059 kmem_cache_free(thread_info_cache, ti);
1062 void thread_info_cache_init(void)
1064 thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
1065 THREAD_SIZE, 0, NULL);
1066 BUG_ON(thread_info_cache == NULL);
1069 #endif /* THREAD_SHIFT < PAGE_SHIFT */