]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/kernel/process.c
powerpc: Get merged kernel to compile and run on 32-bit SMP powermac.
[karo-tx-linux.git] / arch / powerpc / kernel / process.c
1 /*
2  *  arch/ppc/kernel/process.c
3  *
4  *  Derived from "arch/i386/kernel/process.c"
5  *    Copyright (C) 1995  Linus Torvalds
6  *
7  *  Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
8  *  Paul Mackerras (paulus@cs.anu.edu.au)
9  *
10  *  PowerPC version
11  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
12  *
13  *  This program is free software; you can redistribute it and/or
14  *  modify it under the terms of the GNU General Public License
15  *  as published by the Free Software Foundation; either version
16  *  2 of the License, or (at your option) any later version.
17  */
18
19 #include <linux/config.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/user.h>
31 #include <linux/elf.h>
32 #include <linux/init.h>
33 #include <linux/prctl.h>
34 #include <linux/init_task.h>
35 #include <linux/module.h>
36 #include <linux/kallsyms.h>
37 #include <linux/mqueue.h>
38 #include <linux/hardirq.h>
39
40 #include <asm/pgtable.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
43 #include <asm/io.h>
44 #include <asm/processor.h>
45 #include <asm/mmu.h>
46 #include <asm/prom.h>
47
48 extern unsigned long _get_SP(void);
49
50 #ifndef CONFIG_SMP
51 struct task_struct *last_task_used_math = NULL;
52 struct task_struct *last_task_used_altivec = NULL;
53 struct task_struct *last_task_used_spe = NULL;
54 #endif
55
56 static struct fs_struct init_fs = INIT_FS;
57 static struct files_struct init_files = INIT_FILES;
58 static struct signal_struct init_signals = INIT_SIGNALS(init_signals);
59 static struct sighand_struct init_sighand = INIT_SIGHAND(init_sighand);
60 struct mm_struct init_mm = INIT_MM(init_mm);
61 EXPORT_SYMBOL(init_mm);
62
63 /* this is 8kB-aligned so we can get to the thread_info struct
64    at the base of it from the stack pointer with 1 integer instruction. */
65 union thread_union init_thread_union
66         __attribute__((__section__(".data.init_task"))) =
67 { INIT_THREAD_INFO(init_task) };
68
69 /* initial task structure */
70 struct task_struct init_task = INIT_TASK(init_task);
71 EXPORT_SYMBOL(init_task);
72
73 /* only used to get secondary processor up */
74 struct task_struct *current_set[NR_CPUS] = {&init_task, };
75
76 /*
77  * Make sure the floating-point register state in the
78  * the thread_struct is up to date for task tsk.
79  */
80 void flush_fp_to_thread(struct task_struct *tsk)
81 {
82         if (tsk->thread.regs) {
83                 /*
84                  * We need to disable preemption here because if we didn't,
85                  * another process could get scheduled after the regs->msr
86                  * test but before we have finished saving the FP registers
87                  * to the thread_struct.  That process could take over the
88                  * FPU, and then when we get scheduled again we would store
89                  * bogus values for the remaining FP registers.
90                  */
91                 preempt_disable();
92                 if (tsk->thread.regs->msr & MSR_FP) {
93 #ifdef CONFIG_SMP
94                         /*
95                          * This should only ever be called for current or
96                          * for a stopped child process.  Since we save away
97                          * the FP register state on context switch on SMP,
98                          * there is something wrong if a stopped child appears
99                          * to still have its FP state in the CPU registers.
100                          */
101                         BUG_ON(tsk != current);
102 #endif
103                         giveup_fpu(current);
104                 }
105                 preempt_enable();
106         }
107 }
108
109 void enable_kernel_fp(void)
110 {
111         WARN_ON(preemptible());
112
113 #ifdef CONFIG_SMP
114         if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
115                 giveup_fpu(current);
116         else
117                 giveup_fpu(NULL);       /* just enables FP for kernel */
118 #else
119         giveup_fpu(last_task_used_math);
120 #endif /* CONFIG_SMP */
121 }
122 EXPORT_SYMBOL(enable_kernel_fp);
123
124 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
125 {
126         if (!tsk->thread.regs)
127                 return 0;
128         flush_fp_to_thread(current);
129
130         memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
131
132         return 1;
133 }
134
135 #ifdef CONFIG_ALTIVEC
136 void enable_kernel_altivec(void)
137 {
138         WARN_ON(preemptible());
139
140 #ifdef CONFIG_SMP
141         if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
142                 giveup_altivec(current);
143         else
144                 giveup_altivec(NULL);   /* just enable AltiVec for kernel - force */
145 #else
146         giveup_altivec(last_task_used_altivec);
147 #endif /* CONFIG_SMP */
148 }
149 EXPORT_SYMBOL(enable_kernel_altivec);
150
151 /*
152  * Make sure the VMX/Altivec register state in the
153  * the thread_struct is up to date for task tsk.
154  */
155 void flush_altivec_to_thread(struct task_struct *tsk)
156 {
157         if (tsk->thread.regs) {
158                 preempt_disable();
159                 if (tsk->thread.regs->msr & MSR_VEC) {
160 #ifdef CONFIG_SMP
161                         BUG_ON(tsk != current);
162 #endif
163                         giveup_altivec(current);
164                 }
165                 preempt_enable();
166         }
167 }
168
169 int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
170 {
171         flush_altivec_to_thread(current);
172         memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
173         return 1;
174 }
175 #endif /* CONFIG_ALTIVEC */
176
177 #ifdef CONFIG_SPE
178
179 void enable_kernel_spe(void)
180 {
181         WARN_ON(preemptible());
182
183 #ifdef CONFIG_SMP
184         if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
185                 giveup_spe(current);
186         else
187                 giveup_spe(NULL);       /* just enable SPE for kernel - force */
188 #else
189         giveup_spe(last_task_used_spe);
190 #endif /* __SMP __ */
191 }
192 EXPORT_SYMBOL(enable_kernel_spe);
193
194 void flush_spe_to_thread(struct task_struct *tsk)
195 {
196         if (tsk->thread.regs) {
197                 preempt_disable();
198                 if (tsk->thread.regs->msr & MSR_SPE) {
199 #ifdef CONFIG_SMP
200                         BUG_ON(tsk != current);
201 #endif
202                         giveup_spe(current);
203                 }
204                 preempt_enable();
205         }
206 }
207
208 int dump_spe(struct pt_regs *regs, elf_vrregset_t *evrregs)
209 {
210         flush_spe_to_thread(current);
211         /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
212         memcpy(evrregs, &current->thread.evr[0], sizeof(u32) * 35);
213         return 1;
214 }
215 #endif /* CONFIG_SPE */
216
217 static void set_dabr_spr(unsigned long val)
218 {
219         mtspr(SPRN_DABR, val);
220 }
221
222 int set_dabr(unsigned long dabr)
223 {
224         int ret = 0;
225
226 #ifdef CONFIG_PPC64
227         if (firmware_has_feature(FW_FEATURE_XDABR)) {
228                 /* We want to catch accesses from kernel and userspace */
229                 unsigned long flags = H_DABRX_KERNEL|H_DABRX_USER;
230                 ret = plpar_set_xdabr(dabr, flags);
231         } else if (firmware_has_feature(FW_FEATURE_DABR)) {
232                 ret = plpar_set_dabr(dabr);
233         } else
234 #endif
235                 set_dabr_spr(dabr);
236
237         return ret;
238 }
239
240 static DEFINE_PER_CPU(unsigned long, current_dabr);
241
242 struct task_struct *__switch_to(struct task_struct *prev,
243         struct task_struct *new)
244 {
245         struct thread_struct *new_thread, *old_thread;
246         unsigned long flags;
247         struct task_struct *last;
248
249 #ifdef CONFIG_SMP
250         /* avoid complexity of lazy save/restore of fpu
251          * by just saving it every time we switch out if
252          * this task used the fpu during the last quantum.
253          *
254          * If it tries to use the fpu again, it'll trap and
255          * reload its fp regs.  So we don't have to do a restore
256          * every switch, just a save.
257          *  -- Cort
258          */
259         if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
260                 giveup_fpu(prev);
261 #ifdef CONFIG_ALTIVEC
262         /*
263          * If the previous thread used altivec in the last quantum
264          * (thus changing altivec regs) then save them.
265          * We used to check the VRSAVE register but not all apps
266          * set it, so we don't rely on it now (and in fact we need
267          * to save & restore VSCR even if VRSAVE == 0).  -- paulus
268          *
269          * On SMP we always save/restore altivec regs just to avoid the
270          * complexity of changing processors.
271          *  -- Cort
272          */
273         if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
274                 giveup_altivec(prev);
275 #endif /* CONFIG_ALTIVEC */
276 #ifdef CONFIG_SPE
277         /*
278          * If the previous thread used spe in the last quantum
279          * (thus changing spe regs) then save them.
280          *
281          * On SMP we always save/restore spe regs just to avoid the
282          * complexity of changing processors.
283          */
284         if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
285                 giveup_spe(prev);
286 #endif /* CONFIG_SPE */
287
288 #else  /* CONFIG_SMP */
289 #ifdef CONFIG_ALTIVEC
290         /* Avoid the trap.  On smp this this never happens since
291          * we don't set last_task_used_altivec -- Cort
292          */
293         if (new->thread.regs && last_task_used_altivec == new)
294                 new->thread.regs->msr |= MSR_VEC;
295 #endif /* CONFIG_ALTIVEC */
296 #ifdef CONFIG_SPE
297         /* Avoid the trap.  On smp this this never happens since
298          * we don't set last_task_used_spe
299          */
300         if (new->thread.regs && last_task_used_spe == new)
301                 new->thread.regs->msr |= MSR_SPE;
302 #endif /* CONFIG_SPE */
303
304 #endif /* CONFIG_SMP */
305
306 #ifdef CONFIG_PPC64     /* for now */
307         if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr)) {
308                 set_dabr(new->thread.dabr);
309                 __get_cpu_var(current_dabr) = new->thread.dabr;
310         }
311 #endif
312
313         new_thread = &new->thread;
314         old_thread = &current->thread;
315         local_irq_save(flags);
316         last = _switch(old_thread, new_thread);
317
318         local_irq_restore(flags);
319
320         return last;
321 }
322
323 void show_regs(struct pt_regs * regs)
324 {
325         int i, trap;
326
327         printk("NIP: %08lX LR: %08lX SP: %08lX REGS: %p TRAP: %04lx    %s\n",
328                regs->nip, regs->link, regs->gpr[1], regs, regs->trap,
329                print_tainted());
330         printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
331                regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
332                regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
333                regs->msr&MSR_IR ? 1 : 0,
334                regs->msr&MSR_DR ? 1 : 0);
335         trap = TRAP(regs);
336         if (trap == 0x300 || trap == 0x600)
337                 printk("DAR: %08lX, DSISR: %08lX\n", regs->dar, regs->dsisr);
338         printk("TASK = %p[%d] '%s' THREAD: %p\n",
339                current, current->pid, current->comm, current->thread_info);
340         printk("Last syscall: %ld ", current->thread.last_syscall);
341
342 #ifdef CONFIG_SMP
343         printk(" CPU: %d", smp_processor_id());
344 #endif /* CONFIG_SMP */
345
346         for (i = 0;  i < 32;  i++) {
347                 long r;
348                 if ((i % 8) == 0)
349                         printk("\n" KERN_INFO "GPR%02d: ", i);
350                 if (__get_user(r, &regs->gpr[i]))
351                         break;
352                 printk("%08lX ", r);
353                 if (i == 12 && !FULL_REGS(regs))
354                         break;
355         }
356         printk("\n");
357 #ifdef CONFIG_KALLSYMS
358         /*
359          * Lookup NIP late so we have the best change of getting the
360          * above info out without failing
361          */
362         printk("NIP [%08lx] ", regs->nip);
363         print_symbol("%s\n", regs->nip);
364         printk("LR [%08lx] ", regs->link);
365         print_symbol("%s\n", regs->link);
366 #endif
367         show_stack(current, (unsigned long *) regs->gpr[1]);
368 }
369
370 void exit_thread(void)
371 {
372 #ifndef CONFIG_SMP
373         if (last_task_used_math == current)
374                 last_task_used_math = NULL;
375 #ifdef CONFIG_ALTIVEC
376         if (last_task_used_altivec == current)
377                 last_task_used_altivec = NULL;
378 #endif /* CONFIG_ALTIVEC */
379 #ifdef CONFIG_SPE
380         if (last_task_used_spe == current)
381                 last_task_used_spe = NULL;
382 #endif
383 #endif /* CONFIG_SMP */
384 }
385
386 void flush_thread(void)
387 {
388 #ifndef CONFIG_SMP
389         if (last_task_used_math == current)
390                 last_task_used_math = NULL;
391 #ifdef CONFIG_ALTIVEC
392         if (last_task_used_altivec == current)
393                 last_task_used_altivec = NULL;
394 #endif /* CONFIG_ALTIVEC */
395 #ifdef CONFIG_SPE
396         if (last_task_used_spe == current)
397                 last_task_used_spe = NULL;
398 #endif
399 #endif /* CONFIG_SMP */
400
401 #ifdef CONFIG_PPC64     /* for now */
402         if (current->thread.dabr) {
403                 current->thread.dabr = 0;
404                 set_dabr(0);
405         }
406 #endif
407 }
408
409 void
410 release_thread(struct task_struct *t)
411 {
412 }
413
414 /*
415  * This gets called before we allocate a new thread and copy
416  * the current task into it.
417  */
418 void prepare_to_copy(struct task_struct *tsk)
419 {
420         flush_fp_to_thread(current);
421         flush_altivec_to_thread(current);
422         flush_spe_to_thread(current);
423 }
424
425 /*
426  * Copy a thread..
427  */
428 int
429 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
430             unsigned long unused,
431             struct task_struct *p, struct pt_regs *regs)
432 {
433         struct pt_regs *childregs, *kregs;
434         extern void ret_from_fork(void);
435         unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
436         unsigned long childframe;
437
438         CHECK_FULL_REGS(regs);
439         /* Copy registers */
440         sp -= sizeof(struct pt_regs);
441         childregs = (struct pt_regs *) sp;
442         *childregs = *regs;
443         if ((childregs->msr & MSR_PR) == 0) {
444                 /* for kernel thread, set `current' and stackptr in new task */
445                 childregs->gpr[1] = sp + sizeof(struct pt_regs);
446                 childregs->gpr[2] = (unsigned long) p;
447                 p->thread.regs = NULL;  /* no user register state */
448         } else {
449                 childregs->gpr[1] = usp;
450                 p->thread.regs = childregs;
451                 if (clone_flags & CLONE_SETTLS)
452                         childregs->gpr[2] = childregs->gpr[6];
453         }
454         childregs->gpr[3] = 0;  /* Result from fork() */
455         sp -= STACK_FRAME_OVERHEAD;
456         childframe = sp;
457
458         /*
459          * The way this works is that at some point in the future
460          * some task will call _switch to switch to the new task.
461          * That will pop off the stack frame created below and start
462          * the new task running at ret_from_fork.  The new task will
463          * do some house keeping and then return from the fork or clone
464          * system call, using the stack frame created above.
465          */
466         sp -= sizeof(struct pt_regs);
467         kregs = (struct pt_regs *) sp;
468         sp -= STACK_FRAME_OVERHEAD;
469         p->thread.ksp = sp;
470         kregs->nip = (unsigned long)ret_from_fork;
471
472         p->thread.last_syscall = -1;
473
474         return 0;
475 }
476
477 /*
478  * Set up a thread for executing a new program
479  */
480 void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
481 {
482         set_fs(USER_DS);
483         memset(regs->gpr, 0, sizeof(regs->gpr));
484         regs->ctr = 0;
485         regs->link = 0;
486         regs->xer = 0;
487         regs->ccr = 0;
488         regs->mq = 0;
489         regs->nip = nip;
490         regs->gpr[1] = sp;
491         regs->msr = MSR_USER;
492 #ifndef CONFIG_SMP
493         if (last_task_used_math == current)
494                 last_task_used_math = NULL;
495 #ifdef CONFIG_ALTIVEC
496         if (last_task_used_altivec == current)
497                 last_task_used_altivec = NULL;
498 #endif
499 #ifdef CONFIG_SPE
500         if (last_task_used_spe == current)
501                 last_task_used_spe = NULL;
502 #endif
503 #endif /* CONFIG_SMP */
504         memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
505         current->thread.fpscr = 0;
506 #ifdef CONFIG_ALTIVEC
507         memset(current->thread.vr, 0, sizeof(current->thread.vr));
508         memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
509         current->thread.vrsave = 0;
510         current->thread.used_vr = 0;
511 #endif /* CONFIG_ALTIVEC */
512 #ifdef CONFIG_SPE
513         memset(current->thread.evr, 0, sizeof(current->thread.evr));
514         current->thread.acc = 0;
515         current->thread.spefscr = 0;
516         current->thread.used_spe = 0;
517 #endif /* CONFIG_SPE */
518 }
519
520 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
521                 | PR_FP_EXC_RES | PR_FP_EXC_INV)
522
523 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
524 {
525         struct pt_regs *regs = tsk->thread.regs;
526
527         /* This is a bit hairy.  If we are an SPE enabled  processor
528          * (have embedded fp) we store the IEEE exception enable flags in
529          * fpexc_mode.  fpexc_mode is also used for setting FP exception
530          * mode (asyn, precise, disabled) for 'Classic' FP. */
531         if (val & PR_FP_EXC_SW_ENABLE) {
532 #ifdef CONFIG_SPE
533                 tsk->thread.fpexc_mode = val &
534                         (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
535 #else
536                 return -EINVAL;
537 #endif
538         } else {
539                 /* on a CONFIG_SPE this does not hurt us.  The bits that
540                  * __pack_fe01 use do not overlap with bits used for
541                  * PR_FP_EXC_SW_ENABLE.  Additionally, the MSR[FE0,FE1] bits
542                  * on CONFIG_SPE implementations are reserved so writing to
543                  * them does not change anything */
544                 if (val > PR_FP_EXC_PRECISE)
545                         return -EINVAL;
546                 tsk->thread.fpexc_mode = __pack_fe01(val);
547                 if (regs != NULL && (regs->msr & MSR_FP) != 0)
548                         regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
549                                 | tsk->thread.fpexc_mode;
550         }
551         return 0;
552 }
553
554 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
555 {
556         unsigned int val;
557
558         if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
559 #ifdef CONFIG_SPE
560                 val = tsk->thread.fpexc_mode;
561 #else
562                 return -EINVAL;
563 #endif
564         else
565                 val = __unpack_fe01(tsk->thread.fpexc_mode);
566         return put_user(val, (unsigned int __user *) adr);
567 }
568
569 int sys_clone(unsigned long clone_flags, unsigned long usp,
570               int __user *parent_tidp, void __user *child_threadptr,
571               int __user *child_tidp, int p6,
572               struct pt_regs *regs)
573 {
574         CHECK_FULL_REGS(regs);
575         if (usp == 0)
576                 usp = regs->gpr[1];     /* stack pointer for child */
577         return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
578 }
579
580 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
581              unsigned long p4, unsigned long p5, unsigned long p6,
582              struct pt_regs *regs)
583 {
584         CHECK_FULL_REGS(regs);
585         return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
586 }
587
588 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
589               unsigned long p4, unsigned long p5, unsigned long p6,
590               struct pt_regs *regs)
591 {
592         CHECK_FULL_REGS(regs);
593         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
594                         regs, 0, NULL, NULL);
595 }
596
597 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
598                unsigned long a3, unsigned long a4, unsigned long a5,
599                struct pt_regs *regs)
600 {
601         int error;
602         char * filename;
603
604         filename = getname((char __user *) a0);
605         error = PTR_ERR(filename);
606         if (IS_ERR(filename))
607                 goto out;
608         flush_fp_to_thread(current);
609         flush_altivec_to_thread(current);
610         flush_spe_to_thread(current);
611         error = do_execve(filename, (char __user * __user *) a1,
612                           (char __user * __user *) a2, regs);
613         if (error == 0) {
614                 task_lock(current);
615                 current->ptrace &= ~PT_DTRACE;
616                 task_unlock(current);
617         }
618         putname(filename);
619 out:
620         return error;
621 }
622
623 static int validate_sp(unsigned long sp, struct task_struct *p,
624                        unsigned long nbytes)
625 {
626         unsigned long stack_page = (unsigned long)p->thread_info;
627
628         if (sp >= stack_page + sizeof(struct thread_struct)
629             && sp <= stack_page + THREAD_SIZE - nbytes)
630                 return 1;
631
632 #ifdef CONFIG_IRQSTACKS
633         stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
634         if (sp >= stack_page + sizeof(struct thread_struct)
635             && sp <= stack_page + THREAD_SIZE - nbytes)
636                 return 1;
637
638         stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
639         if (sp >= stack_page + sizeof(struct thread_struct)
640             && sp <= stack_page + THREAD_SIZE - nbytes)
641                 return 1;
642 #endif
643
644         return 0;
645 }
646
647 void dump_stack(void)
648 {
649         show_stack(current, NULL);
650 }
651
652 EXPORT_SYMBOL(dump_stack);
653
654 void show_stack(struct task_struct *tsk, unsigned long *stack)
655 {
656         unsigned long sp, stack_top, prev_sp, ret;
657         int count = 0;
658         unsigned long next_exc = 0;
659         struct pt_regs *regs;
660         extern char ret_from_except, ret_from_except_full, ret_from_syscall;
661
662         sp = (unsigned long) stack;
663         if (tsk == NULL)
664                 tsk = current;
665         if (sp == 0) {
666                 if (tsk == current)
667                         asm("mr %0,1" : "=r" (sp));
668                 else
669                         sp = tsk->thread.ksp;
670         }
671
672         prev_sp = (unsigned long) (tsk->thread_info + 1);
673         stack_top = (unsigned long) tsk->thread_info + THREAD_SIZE;
674         while (count < 16 && sp > prev_sp && sp < stack_top && (sp & 3) == 0) {
675                 if (count == 0) {
676                         printk("Call trace:");
677 #ifdef CONFIG_KALLSYMS
678                         printk("\n");
679 #endif
680                 } else {
681                         if (next_exc) {
682                                 ret = next_exc;
683                                 next_exc = 0;
684                         } else
685                                 ret = *(unsigned long *)(sp + 4);
686                         printk(" [%08lx] ", ret);
687 #ifdef CONFIG_KALLSYMS
688                         print_symbol("%s", ret);
689                         printk("\n");
690 #endif
691                         if (ret == (unsigned long) &ret_from_except
692                             || ret == (unsigned long) &ret_from_except_full
693                             || ret == (unsigned long) &ret_from_syscall) {
694                                 /* sp + 16 points to an exception frame */
695                                 regs = (struct pt_regs *) (sp + 16);
696                                 if (sp + 16 + sizeof(*regs) <= stack_top)
697                                         next_exc = regs->nip;
698                         }
699                 }
700                 ++count;
701                 sp = *(unsigned long *)sp;
702         }
703 #ifndef CONFIG_KALLSYMS
704         if (count > 0)
705                 printk("\n");
706 #endif
707 }
708
709 unsigned long get_wchan(struct task_struct *p)
710 {
711         unsigned long ip, sp;
712         int count = 0;
713
714         if (!p || p == current || p->state == TASK_RUNNING)
715                 return 0;
716
717         sp = p->thread.ksp;
718         if (!validate_sp(sp, p, 16))
719                 return 0;
720
721         do {
722                 sp = *(unsigned long *)sp;
723                 if (!validate_sp(sp, p, 16))
724                         return 0;
725                 if (count > 0) {
726                         ip = *(unsigned long *)(sp + 4);
727                         if (!in_sched_functions(ip))
728                                 return ip;
729                 }
730         } while (count++ < 16);
731         return 0;
732 }
733 EXPORT_SYMBOL(get_wchan);