4db17100eb67930fab70cd953a16505af7ad18ee
[linux-2.6.git] / 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 #ifdef CONFIG_ALTIVEC
156 int
157 dump_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
158 {
159         if (regs->msr & MSR_VEC)
160                 giveup_altivec(current);
161         memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
162         return 1;
163 }
164
165 void
166 enable_kernel_altivec(void)
167 {
168         WARN_ON(preemptible());
169
170 #ifdef CONFIG_SMP
171         if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
172                 giveup_altivec(current);
173         else
174                 giveup_altivec(NULL);   /* just enable AltiVec for kernel - force */
175 #else
176         giveup_altivec(last_task_used_altivec);
177 #endif /* __SMP __ */
178 }
179 EXPORT_SYMBOL(enable_kernel_altivec);
180 #endif /* CONFIG_ALTIVEC */
181
182 #ifdef CONFIG_SPE
183 int
184 dump_spe(struct pt_regs *regs, elf_vrregset_t *evrregs)
185 {
186         if (regs->msr & MSR_SPE)
187                 giveup_spe(current);
188         /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
189         memcpy(evrregs, &current->thread.evr[0], sizeof(u32) * 35);
190         return 1;
191 }
192
193 void
194 enable_kernel_spe(void)
195 {
196         WARN_ON(preemptible());
197
198 #ifdef CONFIG_SMP
199         if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
200                 giveup_spe(current);
201         else
202                 giveup_spe(NULL);       /* just enable SPE for kernel - force */
203 #else
204         giveup_spe(last_task_used_spe);
205 #endif /* __SMP __ */
206 }
207 EXPORT_SYMBOL(enable_kernel_spe);
208 #endif /* CONFIG_SPE */
209
210 void
211 enable_kernel_fp(void)
212 {
213         WARN_ON(preemptible());
214
215 #ifdef CONFIG_SMP
216         if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
217                 giveup_fpu(current);
218         else
219                 giveup_fpu(NULL);       /* just enables FP for kernel */
220 #else
221         giveup_fpu(last_task_used_math);
222 #endif /* CONFIG_SMP */
223 }
224 EXPORT_SYMBOL(enable_kernel_fp);
225
226 int
227 dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
228 {
229         preempt_disable();
230         if (tsk->thread.regs && (tsk->thread.regs->msr & MSR_FP))
231                 giveup_fpu(tsk);
232         preempt_enable();
233         memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
234         return 1;
235 }
236
237 struct task_struct *__switch_to(struct task_struct *prev,
238         struct task_struct *new)
239 {
240         struct thread_struct *new_thread, *old_thread;
241         unsigned long s;
242         struct task_struct *last;
243
244         local_irq_save(s);
245 #ifdef CHECK_STACK
246         check_stack(prev);
247         check_stack(new);
248 #endif
249
250 #ifdef CONFIG_SMP
251         /* avoid complexity of lazy save/restore of fpu
252          * by just saving it every time we switch out if
253          * this task used the fpu during the last quantum.
254          *
255          * If it tries to use the fpu again, it'll trap and
256          * reload its fp regs.  So we don't have to do a restore
257          * every switch, just a save.
258          *  -- Cort
259          */
260         if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
261                 giveup_fpu(prev);
262 #ifdef CONFIG_ALTIVEC
263         /*
264          * If the previous thread used altivec in the last quantum
265          * (thus changing altivec regs) then save them.
266          * We used to check the VRSAVE register but not all apps
267          * set it, so we don't rely on it now (and in fact we need
268          * to save & restore VSCR even if VRSAVE == 0).  -- paulus
269          *
270          * On SMP we always save/restore altivec regs just to avoid the
271          * complexity of changing processors.
272          *  -- Cort
273          */
274         if ((prev->thread.regs && (prev->thread.regs->msr & MSR_VEC)))
275                 giveup_altivec(prev);
276 #endif /* CONFIG_ALTIVEC */
277 #ifdef CONFIG_SPE
278         /*
279          * If the previous thread used spe in the last quantum
280          * (thus changing spe regs) then save them.
281          *
282          * On SMP we always save/restore spe regs just to avoid the
283          * complexity of changing processors.
284          */
285         if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
286                 giveup_spe(prev);
287 #endif /* CONFIG_SPE */
288 #endif /* CONFIG_SMP */
289
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 #ifdef CONFIG_SPE
296         /* Avoid the trap.  On smp this this never happens since
297          * we don't set last_task_used_spe
298          */
299         if (new->thread.regs && last_task_used_spe == new)
300                 new->thread.regs->msr |= MSR_SPE;
301 #endif /* CONFIG_SPE */
302         new_thread = &new->thread;
303         old_thread = &current->thread;
304         last = _switch(old_thread, new_thread);
305         local_irq_restore(s);
306         return last;
307 }
308
309 void show_regs(struct pt_regs * regs)
310 {
311         int i, trap;
312
313         printk("NIP: %08lX LR: %08lX SP: %08lX REGS: %p TRAP: %04lx    %s\n",
314                regs->nip, regs->link, regs->gpr[1], regs, regs->trap,
315                print_tainted());
316         printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
317                regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
318                regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
319                regs->msr&MSR_IR ? 1 : 0,
320                regs->msr&MSR_DR ? 1 : 0);
321         trap = TRAP(regs);
322         if (trap == 0x300 || trap == 0x600)
323                 printk("DAR: %08lX, DSISR: %08lX\n", regs->dar, regs->dsisr);
324         printk("TASK = %p[%d] '%s' THREAD: %p\n",
325                current, current->pid, current->comm, current->thread_info);
326         printk("Last syscall: %ld ", current->thread.last_syscall);
327
328 #if defined(CONFIG_4xx) && defined(DCRN_PLB0_BEAR)
329         printk("\nPLB0: bear= 0x%8.8x acr=   0x%8.8x besr=  0x%8.8x\n",
330             mfdcr(DCRN_PLB0_BEAR), mfdcr(DCRN_PLB0_ACR),
331             mfdcr(DCRN_PLB0_BESR));
332 #endif
333 #if defined(CONFIG_4xx) && defined(DCRN_POB0_BEAR)
334         printk("PLB0 to OPB: bear= 0x%8.8x besr0= 0x%8.8x besr1= 0x%8.8x\n",
335             mfdcr(DCRN_POB0_BEAR), mfdcr(DCRN_POB0_BESR0),
336             mfdcr(DCRN_POB0_BESR1));
337 #endif
338
339 #ifdef CONFIG_SMP
340         printk(" CPU: %d", smp_processor_id());
341 #endif /* CONFIG_SMP */
342
343         for (i = 0;  i < 32;  i++) {
344                 long r;
345                 if ((i % 8) == 0)
346                         printk("\n" KERN_INFO "GPR%02d: ", i);
347                 if (__get_user(r, &regs->gpr[i]))
348                         break;
349                 printk("%08lX ", r);
350                 if (i == 12 && !FULL_REGS(regs))
351                         break;
352         }
353         printk("\n");
354 #ifdef CONFIG_KALLSYMS
355         /*
356          * Lookup NIP late so we have the best change of getting the
357          * above info out without failing
358          */
359         printk("NIP [%08lx] ", regs->nip);
360         print_symbol("%s\n", regs->nip);
361         printk("LR [%08lx] ", regs->link);
362         print_symbol("%s\n", regs->link);
363 #endif
364         show_stack(current, (unsigned long *) regs->gpr[1]);
365 }
366
367 void exit_thread(void)
368 {
369         if (last_task_used_math == current)
370                 last_task_used_math = NULL;
371         if (last_task_used_altivec == current)
372                 last_task_used_altivec = NULL;
373 #ifdef CONFIG_SPE
374         if (last_task_used_spe == current)
375                 last_task_used_spe = NULL;
376 #endif
377 }
378
379 void flush_thread(void)
380 {
381         if (last_task_used_math == current)
382                 last_task_used_math = NULL;
383         if (last_task_used_altivec == current)
384                 last_task_used_altivec = NULL;
385 #ifdef CONFIG_SPE
386         if (last_task_used_spe == current)
387                 last_task_used_spe = NULL;
388 #endif
389 }
390
391 void
392 release_thread(struct task_struct *t)
393 {
394 }
395
396 /*
397  * This gets called before we allocate a new thread and copy
398  * the current task into it.
399  */
400 void prepare_to_copy(struct task_struct *tsk)
401 {
402         struct pt_regs *regs = tsk->thread.regs;
403
404         if (regs == NULL)
405                 return;
406         preempt_disable();
407         if (regs->msr & MSR_FP)
408                 giveup_fpu(current);
409 #ifdef CONFIG_ALTIVEC
410         if (regs->msr & MSR_VEC)
411                 giveup_altivec(current);
412 #endif /* CONFIG_ALTIVEC */
413 #ifdef CONFIG_SPE
414         if (regs->msr & MSR_SPE)
415                 giveup_spe(current);
416 #endif /* CONFIG_SPE */
417         preempt_enable();
418 }
419
420 /*
421  * Copy a thread..
422  */
423 int
424 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
425             unsigned long unused,
426             struct task_struct *p, struct pt_regs *regs)
427 {
428         struct pt_regs *childregs, *kregs;
429         extern void ret_from_fork(void);
430         unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
431         unsigned long childframe;
432
433         CHECK_FULL_REGS(regs);
434         /* Copy registers */
435         sp -= sizeof(struct pt_regs);
436         childregs = (struct pt_regs *) sp;
437         *childregs = *regs;
438         if ((childregs->msr & MSR_PR) == 0) {
439                 /* for kernel thread, set `current' and stackptr in new task */
440                 childregs->gpr[1] = sp + sizeof(struct pt_regs);
441                 childregs->gpr[2] = (unsigned long) p;
442                 p->thread.regs = NULL;  /* no user register state */
443         } else {
444                 childregs->gpr[1] = usp;
445                 p->thread.regs = childregs;
446                 if (clone_flags & CLONE_SETTLS)
447                         childregs->gpr[2] = childregs->gpr[6];
448         }
449         childregs->gpr[3] = 0;  /* Result from fork() */
450         sp -= STACK_FRAME_OVERHEAD;
451         childframe = sp;
452
453         /*
454          * The way this works is that at some point in the future
455          * some task will call _switch to switch to the new task.
456          * That will pop off the stack frame created below and start
457          * the new task running at ret_from_fork.  The new task will
458          * do some house keeping and then return from the fork or clone
459          * system call, using the stack frame created above.
460          */
461         sp -= sizeof(struct pt_regs);
462         kregs = (struct pt_regs *) sp;
463         sp -= STACK_FRAME_OVERHEAD;
464         p->thread.ksp = sp;
465         kregs->nip = (unsigned long)ret_from_fork;
466
467         p->thread.last_syscall = -1;
468
469         return 0;
470 }
471
472 /*
473  * Set up a thread for executing a new program
474  */
475 void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
476 {
477         set_fs(USER_DS);
478         memset(regs->gpr, 0, sizeof(regs->gpr));
479         regs->ctr = 0;
480         regs->link = 0;
481         regs->xer = 0;
482         regs->ccr = 0;
483         regs->mq = 0;
484         regs->nip = nip;
485         regs->gpr[1] = sp;
486         regs->msr = MSR_USER;
487         if (last_task_used_math == current)
488                 last_task_used_math = NULL;
489         if (last_task_used_altivec == current)
490                 last_task_used_altivec = NULL;
491 #ifdef CONFIG_SPE
492         if (last_task_used_spe == current)
493                 last_task_used_spe = NULL;
494 #endif
495         memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
496         current->thread.fpscr = 0;
497 #ifdef CONFIG_ALTIVEC
498         memset(current->thread.vr, 0, sizeof(current->thread.vr));
499         memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
500         current->thread.vrsave = 0;
501         current->thread.used_vr = 0;
502 #endif /* CONFIG_ALTIVEC */
503 #ifdef CONFIG_SPE
504         memset(current->thread.evr, 0, sizeof(current->thread.evr));
505         current->thread.acc = 0;
506         current->thread.spefscr = 0;
507         current->thread.used_spe = 0;
508 #endif /* CONFIG_SPE */
509 }
510
511 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
512                 | PR_FP_EXC_RES | PR_FP_EXC_INV)
513
514 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
515 {
516         struct pt_regs *regs = tsk->thread.regs;
517
518         /* This is a bit hairy.  If we are an SPE enabled  processor
519          * (have embedded fp) we store the IEEE exception enable flags in
520          * fpexc_mode.  fpexc_mode is also used for setting FP exception
521          * mode (asyn, precise, disabled) for 'Classic' FP. */
522         if (val & PR_FP_EXC_SW_ENABLE) {
523 #ifdef CONFIG_SPE
524                 tsk->thread.fpexc_mode = val &
525                         (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
526 #else
527                 return -EINVAL;
528 #endif
529         } else {
530                 /* on a CONFIG_SPE this does not hurt us.  The bits that
531                  * __pack_fe01 use do not overlap with bits used for
532                  * PR_FP_EXC_SW_ENABLE.  Additionally, the MSR[FE0,FE1] bits
533                  * on CONFIG_SPE implementations are reserved so writing to
534                  * them does not change anything */
535                 if (val > PR_FP_EXC_PRECISE)
536                         return -EINVAL;
537                 tsk->thread.fpexc_mode = __pack_fe01(val);
538                 if (regs != NULL && (regs->msr & MSR_FP) != 0)
539                         regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
540                                 | tsk->thread.fpexc_mode;
541         }
542         return 0;
543 }
544
545 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
546 {
547         unsigned int val;
548
549         if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
550 #ifdef CONFIG_SPE
551                 val = tsk->thread.fpexc_mode;
552 #else
553                 return -EINVAL;
554 #endif
555         else
556                 val = __unpack_fe01(tsk->thread.fpexc_mode);
557         return put_user(val, (unsigned int __user *) adr);
558 }
559
560 int sys_clone(unsigned long clone_flags, unsigned long usp,
561               int __user *parent_tidp, void __user *child_threadptr,
562               int __user *child_tidp, int p6,
563               struct pt_regs *regs)
564 {
565         CHECK_FULL_REGS(regs);
566         if (usp == 0)
567                 usp = regs->gpr[1];     /* stack pointer for child */
568         return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
569 }
570
571 int sys_fork(int p1, int p2, int p3, int p4, int p5, int p6,
572              struct pt_regs *regs)
573 {
574         CHECK_FULL_REGS(regs);
575         return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
576 }
577
578 int sys_vfork(int p1, int p2, int p3, int p4, int p5, int p6,
579               struct pt_regs *regs)
580 {
581         CHECK_FULL_REGS(regs);
582         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
583                         regs, 0, NULL, NULL);
584 }
585
586 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
587                unsigned long a3, unsigned long a4, unsigned long a5,
588                struct pt_regs *regs)
589 {
590         int error;
591         char * filename;
592
593         filename = getname((char __user *) a0);
594         error = PTR_ERR(filename);
595         if (IS_ERR(filename))
596                 goto out;
597         preempt_disable();
598         if (regs->msr & MSR_FP)
599                 giveup_fpu(current);
600 #ifdef CONFIG_ALTIVEC
601         if (regs->msr & MSR_VEC)
602                 giveup_altivec(current);
603 #endif /* CONFIG_ALTIVEC */
604 #ifdef CONFIG_SPE
605         if (regs->msr & MSR_SPE)
606                 giveup_spe(current);
607 #endif /* CONFIG_SPE */
608         preempt_enable();
609         error = do_execve(filename, (char __user *__user *) a1,
610                           (char __user *__user *) a2, regs);
611         if (error == 0) {
612                 task_lock(current);
613                 current->ptrace &= ~PT_DTRACE;
614                 task_unlock(current);
615         }
616         putname(filename);
617 out:
618         return error;
619 }
620
621 void dump_stack(void)
622 {
623         show_stack(current, NULL);
624 }
625
626 EXPORT_SYMBOL(dump_stack);
627
628 void show_stack(struct task_struct *tsk, unsigned long *stack)
629 {
630         unsigned long sp, stack_top, prev_sp, ret;
631         int count = 0;
632         unsigned long next_exc = 0;
633         struct pt_regs *regs;
634         extern char ret_from_except, ret_from_except_full, ret_from_syscall;
635
636         sp = (unsigned long) stack;
637         if (tsk == NULL)
638                 tsk = current;
639         if (sp == 0) {
640                 if (tsk == current)
641                         asm("mr %0,1" : "=r" (sp));
642                 else
643                         sp = tsk->thread.ksp;
644         }
645
646         prev_sp = (unsigned long) (tsk->thread_info + 1);
647         stack_top = (unsigned long) tsk->thread_info + THREAD_SIZE;
648         while (count < 16 && sp > prev_sp && sp < stack_top && (sp & 3) == 0) {
649                 if (count == 0) {
650                         printk("Call trace:");
651 #ifdef CONFIG_KALLSYMS
652                         printk("\n");
653 #endif
654                 } else {
655                         if (next_exc) {
656                                 ret = next_exc;
657                                 next_exc = 0;
658                         } else
659                                 ret = *(unsigned long *)(sp + 4);
660                         printk(" [%08lx] ", ret);
661 #ifdef CONFIG_KALLSYMS
662                         print_symbol("%s", ret);
663                         printk("\n");
664 #endif
665                         if (ret == (unsigned long) &ret_from_except
666                             || ret == (unsigned long) &ret_from_except_full
667                             || ret == (unsigned long) &ret_from_syscall) {
668                                 /* sp + 16 points to an exception frame */
669                                 regs = (struct pt_regs *) (sp + 16);
670                                 if (sp + 16 + sizeof(*regs) <= stack_top)
671                                         next_exc = regs->nip;
672                         }
673                 }
674                 ++count;
675                 sp = *(unsigned long *)sp;
676         }
677 #ifndef CONFIG_KALLSYMS
678         if (count > 0)
679                 printk("\n");
680 #endif
681 }
682
683 #if 0
684 /*
685  * Low level print for debugging - Cort
686  */
687 int __init ll_printk(const char *fmt, ...)
688 {
689         va_list args;
690         char buf[256];
691         int i;
692
693         va_start(args, fmt);
694         i=vsprintf(buf,fmt,args);
695         ll_puts(buf);
696         va_end(args);
697         return i;
698 }
699
700 int lines = 24, cols = 80;
701 int orig_x = 0, orig_y = 0;
702
703 void puthex(unsigned long val)
704 {
705         unsigned char buf[10];
706         int i;
707         for (i = 7;  i >= 0;  i--)
708         {
709                 buf[i] = "0123456789ABCDEF"[val & 0x0F];
710                 val >>= 4;
711         }
712         buf[8] = '\0';
713         prom_print(buf);
714 }
715
716 void __init ll_puts(const char *s)
717 {
718         int x,y;
719         char *vidmem = (char *)/*(_ISA_MEM_BASE + 0xB8000) */0xD00B8000;
720         char c;
721         extern int mem_init_done;
722
723         if ( mem_init_done ) /* assume this means we can printk */
724         {
725                 printk(s);
726                 return;
727         }
728
729 #if 0
730         if ( have_of )
731         {
732                 prom_print(s);
733                 return;
734         }
735 #endif
736
737         /*
738          * can't ll_puts on chrp without openfirmware yet.
739          * vidmem just needs to be setup for it.
740          * -- Cort
741          */
742         if ( _machine != _MACH_prep )
743                 return;
744         x = orig_x;
745         y = orig_y;
746
747         while ( ( c = *s++ ) != '\0' ) {
748                 if ( c == '\n' ) {
749                         x = 0;
750                         if ( ++y >= lines ) {
751                                 /*scroll();*/
752                                 /*y--;*/
753                                 y = 0;
754                         }
755                 } else {
756                         vidmem [ ( x + cols * y ) * 2 ] = c;
757                         if ( ++x >= cols ) {
758                                 x = 0;
759                                 if ( ++y >= lines ) {
760                                         /*scroll();*/
761                                         /*y--;*/
762                                         y = 0;
763                                 }
764                         }
765                 }
766         }
767
768         orig_x = x;
769         orig_y = y;
770 }
771 #endif
772
773 unsigned long get_wchan(struct task_struct *p)
774 {
775         unsigned long ip, sp;
776         unsigned long stack_page = (unsigned long) p->thread_info;
777         int count = 0;
778         if (!p || p == current || p->state == TASK_RUNNING)
779                 return 0;
780         sp = p->thread.ksp;
781         do {
782                 sp = *(unsigned long *)sp;
783                 if (sp < stack_page || sp >= stack_page + 8188)
784                         return 0;
785                 if (count > 0) {
786                         ip = *(unsigned long *)(sp + 4);
787                         if (!in_sched_functions(ip))
788                                 return ip;
789                 }
790         } while (count++ < 16);
791         return 0;
792 }