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