fix to allow usb modules to compile
[linux-2.4.21-pre4.git] / arch / ppc / kernel / process.c
1 /*
2  * BK Id: SCCS/s.process.c 1.48 01/29/03 07:46:20 trini
3  */
4 /*
5  *  linux/arch/ppc/kernel/process.c
6  *
7  *  Derived from "arch/i386/kernel/process.c"
8  *    Copyright (C) 1995  Linus Torvalds
9  *
10  *  Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
11  *  Paul Mackerras (paulus@cs.anu.edu.au)
12  *
13  *  PowerPC version 
14  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
15  *
16  *  This program is free software; you can redistribute it and/or
17  *  modify it under the terms of the GNU General Public License
18  *  as published by the Free Software Foundation; either version
19  *  2 of the License, or (at your option) any later version.
20  *
21  */
22
23 #include <linux/config.h>
24 #include <linux/errno.h>
25 #include <linux/sched.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/smp.h>
29 #include <linux/smp_lock.h>
30 #include <linux/stddef.h>
31 #include <linux/unistd.h>
32 #include <linux/ptrace.h>
33 #include <linux/slab.h>
34 #include <linux/user.h>
35 #include <linux/elf.h>
36 #include <linux/init.h>
37 #include <linux/prctl.h>
38
39 #include <asm/pgtable.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42 #include <asm/io.h>
43 #include <asm/processor.h>
44 #include <asm/mmu.h>
45 #include <asm/prom.h>
46 #ifdef CONFIG_PPC_ISERIES
47 #include <asm/iSeries/Paca.h>
48 #endif
49
50 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs);
51 extern unsigned long _get_SP(void);
52
53 struct task_struct *last_task_used_math = NULL;
54 struct task_struct *last_task_used_altivec = NULL;
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;
58 struct mm_struct init_mm = INIT_MM(init_mm);
59 /* this is 16-byte aligned because it has a stack in it */
60 union task_union __attribute((aligned(16))) init_task_union = {
61         INIT_TASK(init_task_union.task)
62 };
63 /* only used to get secondary processor up */
64 struct task_struct *current_set[NR_CPUS] = {&init_task, };
65
66 #undef SHOW_TASK_SWITCHES
67 #undef CHECK_STACK
68
69 #if defined(CHECK_STACK)
70 unsigned long
71 kernel_stack_top(struct task_struct *tsk)
72 {
73         return ((unsigned long)tsk) + sizeof(union task_union);
74 }
75
76 unsigned long
77 task_top(struct task_struct *tsk)
78 {
79         return ((unsigned long)tsk) + sizeof(struct task_struct);
80 }
81
82 /* check to make sure the kernel stack is healthy */
83 int check_stack(struct task_struct *tsk)
84 {
85         unsigned long stack_top = kernel_stack_top(tsk);
86         unsigned long tsk_top = task_top(tsk);
87         int ret = 0;
88
89 #if 0   
90         /* check thread magic */
91         if ( tsk->thread.magic != THREAD_MAGIC )
92         {
93                 ret |= 1;
94                 printk("thread.magic bad: %08x\n", tsk->thread.magic);
95         }
96 #endif
97
98         if ( !tsk )
99                 printk("check_stack(): tsk bad tsk %p\n",tsk);
100         
101         /* check if stored ksp is bad */
102         if ( (tsk->thread.ksp > stack_top) || (tsk->thread.ksp < tsk_top) )
103         {
104                 printk("stack out of bounds: %s/%d\n"
105                        " tsk_top %08lx ksp %08lx stack_top %08lx\n",
106                        tsk->comm,tsk->pid,
107                        tsk_top, tsk->thread.ksp, stack_top);
108                 ret |= 2;
109         }
110         
111         /* check if stack ptr RIGHT NOW is bad */
112         if ( (tsk == current) && ((_get_SP() > stack_top ) || (_get_SP() < tsk_top)) )
113         {
114                 printk("current stack ptr out of bounds: %s/%d\n"
115                        " tsk_top %08lx sp %08lx stack_top %08lx\n",
116                        current->comm,current->pid,
117                        tsk_top, _get_SP(), stack_top);
118                 ret |= 4;
119         }
120
121 #if 0   
122         /* check amount of free stack */
123         for ( i = (unsigned long *)task_top(tsk) ; i < kernel_stack_top(tsk) ; i++ )
124         {
125                 if ( !i )
126                         printk("check_stack(): i = %p\n", i);
127                 if ( *i != 0 )
128                 {
129                         /* only notify if it's less than 900 bytes */
130                         if ( (i - (unsigned long *)task_top(tsk))  < 900 )
131                                 printk("%d bytes free on stack\n",
132                                        i - task_top(tsk));
133                         break;
134                 }
135         }
136 #endif
137
138         if (ret)
139         {
140                 panic("bad kernel stack");
141         }
142         return(ret);
143 }
144 #endif /* defined(CHECK_STACK) */
145
146 #ifdef CONFIG_ALTIVEC
147 int
148 dump_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
149 {
150         if (regs->msr & MSR_VEC)
151                 giveup_altivec(current);
152         memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
153         return 1;
154 }
155
156 void 
157 enable_kernel_altivec(void)
158 {
159 #ifdef CONFIG_SMP
160         if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
161                 giveup_altivec(current);
162         else
163                 giveup_altivec(NULL);   /* just enable AltiVec for kernel - force */
164 #else
165         giveup_altivec(last_task_used_altivec);
166 #endif /* __SMP __ */
167 }
168 #endif /* CONFIG_ALTIVEC */
169
170 void
171 enable_kernel_fp(void)
172 {
173 #ifdef CONFIG_SMP
174         if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
175                 giveup_fpu(current);
176         else
177                 giveup_fpu(NULL);       /* just enables FP for kernel */
178 #else
179         giveup_fpu(last_task_used_math);
180 #endif /* CONFIG_SMP */
181 }
182
183 int
184 dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
185 {
186         if (regs->msr & MSR_FP)
187                 giveup_fpu(current);
188         memcpy(fpregs, &current->thread.fpr[0], sizeof(*fpregs));
189         return 1;
190 }
191
192 void
193 _switch_to(struct task_struct *prev, struct task_struct *new,
194           struct task_struct **last)
195 {
196         struct thread_struct *new_thread, *old_thread;
197         unsigned long s;
198         
199         __save_flags(s);
200         __cli();
201 #if CHECK_STACK
202         check_stack(prev);
203         check_stack(new);
204 #endif
205
206 #ifdef CONFIG_SMP
207         /* avoid complexity of lazy save/restore of fpu
208          * by just saving it every time we switch out if
209          * this task used the fpu during the last quantum.
210          * 
211          * If it tries to use the fpu again, it'll trap and
212          * reload its fp regs.  So we don't have to do a restore
213          * every switch, just a save.
214          *  -- Cort
215          */
216         if ( prev->thread.regs && (prev->thread.regs->msr & MSR_FP) )
217                 giveup_fpu(prev);
218 #ifdef CONFIG_ALTIVEC   
219         /*
220          * If the previous thread used altivec in the last quantum
221          * (thus changing altivec regs) then save them.
222          * We used to check the VRSAVE register but not all apps
223          * set it, so we don't rely on it now (and in fact we need
224          * to save & restore VSCR even if VRSAVE == 0).  -- paulus
225          *
226          * On SMP we always save/restore altivec regs just to avoid the
227          * complexity of changing processors.
228          *  -- Cort
229          */
230         if ((prev->thread.regs && (prev->thread.regs->msr & MSR_VEC)))
231                 giveup_altivec(prev);
232 #endif /* CONFIG_ALTIVEC */     
233 #endif /* CONFIG_SMP */
234
235         current_set[smp_processor_id()] = new;
236
237         /* Avoid the trap.  On smp this this never happens since
238          * we don't set last_task_used_altivec -- Cort
239          */
240         if (new->thread.regs && last_task_used_altivec == new)
241                 new->thread.regs->msr |= MSR_VEC;
242         new_thread = &new->thread;
243         old_thread = &current->thread;
244         *last = _switch(old_thread, new_thread);
245         __restore_flags(s);
246 }
247
248 void show_regs(struct pt_regs * regs)
249 {
250         int i;
251
252         printk("NIP: %08lX XER: %08lX LR: %08lX SP: %08lX REGS: %p TRAP: %04lx    %s\n",
253                regs->nip, regs->xer, regs->link, regs->gpr[1], regs,regs->trap, print_tainted());
254         printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
255                regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
256                regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
257                regs->msr&MSR_IR ? 1 : 0,
258                regs->msr&MSR_DR ? 1 : 0);
259 #ifdef CONFIG_4xx
260         /*
261          * TRAP 0x800 is the hijacked FPU unavailable exception vector
262          * on 40x used to implement the heavyweight data access
263          * functionality.  It is an emulated value (like all trap
264          * vectors) on 440.
265          */
266         if (regs->trap == 0x300 || regs->trap == 0x600 || regs->trap == 0x800)
267                 printk("DEAR: %08lX, ESR: %08lX\n", regs->dar, regs->dsisr);
268 #else
269         if (regs->trap == 0x300 || regs->trap == 0x600)
270                 printk("DAR: %08lX, DSISR: %08lX\n", regs->dar, regs->dsisr);
271 #endif
272         printk("TASK = %p[%d] '%s' ",
273                current, current->pid, current->comm);
274         printk("Last syscall: %ld ", current->thread.last_syscall);
275         printk("\nlast math %p last altivec %p", last_task_used_math,
276                last_task_used_altivec);
277
278 #if defined(CONFIG_4xx) && defined(DCRN_PLB0_BEAR)
279         printk("\nPLB0: bear= 0x%8.8x acr=   0x%8.8x besr=  0x%8.8x\n",
280             mfdcr(DCRN_PLB0_BEAR), mfdcr(DCRN_PLB0_ACR),
281             mfdcr(DCRN_PLB0_BESR));
282 #endif
283 #if defined(CONFIG_4xx) && defined(DCRN_POB0_BEAR)
284         printk("PLB0 to OPB: bear= 0x%8.8x besr0= 0x%8.8x besr1= 0x%8.8x\n",
285             mfdcr(DCRN_POB0_BEAR), mfdcr(DCRN_POB0_BESR0),
286             mfdcr(DCRN_POB0_BESR1));
287 #endif
288         
289 #ifdef CONFIG_SMP
290         printk(" CPU: %d", current->processor);
291 #endif /* CONFIG_SMP */
292         
293         printk("\n");
294         for (i = 0;  i < 32;  i++)
295         {
296                 long r;
297                 if ((i % 8) == 0)
298                 {
299                         printk("GPR%02d: ", i);
300                 }
301
302                 if ( __get_user(r, &(regs->gpr[i])) )
303                     goto out;
304                 printk("%08lX ", r);
305                 if ((i % 8) == 7)
306                 {
307                         printk("\n");
308                 }
309         }
310 out:
311         print_backtrace((unsigned long *)regs->gpr[1]);
312 }
313
314 void exit_thread(void)
315 {
316         if (last_task_used_math == current)
317                 last_task_used_math = NULL;
318         if (last_task_used_altivec == current)
319                 last_task_used_altivec = NULL;
320 }
321
322 void flush_thread(void)
323 {
324         if (last_task_used_math == current)
325                 last_task_used_math = NULL;
326         if (last_task_used_altivec == current)
327                 last_task_used_altivec = NULL;
328 }
329
330 void
331 release_thread(struct task_struct *t)
332 {
333 }
334
335 /*
336  * Copy a thread..
337  */
338 int
339 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
340             unsigned long unused,
341             struct task_struct *p, struct pt_regs *regs)
342 {
343         struct pt_regs *childregs, *kregs;
344         extern void ret_from_fork(void);
345         unsigned long sp = (unsigned long)p + sizeof(union task_union);
346         unsigned long childframe;
347
348         /* Copy registers */
349         sp -= sizeof(struct pt_regs);
350         childregs = (struct pt_regs *) sp;
351         *childregs = *regs;
352         if ((childregs->msr & MSR_PR) == 0) {
353                 /* for kernel thread, set `current' and stackptr in new task */
354                 childregs->gpr[1] = sp + sizeof(struct pt_regs);
355                 childregs->gpr[2] = (unsigned long) p;
356                 p->thread.regs = NULL;  /* no user register state */
357         } else
358                 p->thread.regs = childregs;
359         childregs->gpr[3] = 0;  /* Result from fork() */
360         sp -= STACK_FRAME_OVERHEAD;
361         childframe = sp;
362
363         /*
364          * The way this works is that at some point in the future
365          * some task will call _switch to switch to the new task.
366          * That will pop off the stack frame created below and start
367          * the new task running at ret_from_fork.  The new task will
368          * do some house keeping and then return from the fork or clone
369          * system call, using the stack frame created above.
370          */
371         sp -= sizeof(struct pt_regs);
372         kregs = (struct pt_regs *) sp;
373         sp -= STACK_FRAME_OVERHEAD;
374         p->thread.ksp = sp;
375         kregs->nip = (unsigned long)ret_from_fork;
376 #ifdef CONFIG_PPC_ISERIES
377         kregs->softEnable = ((struct Paca *)mfspr(SPRG1))->xProcEnabled;
378 #endif  
379
380         /*
381          * copy fpu info - assume lazy fpu switch now always
382          *  -- Cort
383          */
384         if (regs->msr & MSR_FP) {
385                 giveup_fpu(current);
386                 childregs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
387         }
388         memcpy(&p->thread.fpr, &current->thread.fpr, sizeof(p->thread.fpr));
389         p->thread.fpscr = current->thread.fpscr;
390
391 #ifdef CONFIG_ALTIVEC
392         /*
393          * copy altiVec info - assume lazy altiVec switch
394          * - kumar
395          */
396         if (regs->msr & MSR_VEC)
397                 giveup_altivec(current);
398         memcpy(&p->thread.vr, &current->thread.vr, sizeof(p->thread.vr));
399         p->thread.vscr = current->thread.vscr;
400         childregs->msr &= ~MSR_VEC;
401 #endif /* CONFIG_ALTIVEC */
402
403         p->thread.last_syscall = -1;
404
405         return 0;
406 }
407
408 /*
409  * Set up a thread for executing a new program
410  */
411 void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
412 {
413         set_fs(USER_DS);
414         memset(regs->gpr, 0, sizeof(regs->gpr));
415         regs->ctr = 0;
416         regs->link = 0;
417         regs->xer = 0;
418         regs->ccr = 0;
419         regs->nip = nip;
420         regs->gpr[1] = sp;
421         regs->msr = MSR_USER;
422         if (last_task_used_math == current)
423                 last_task_used_math = 0;
424         if (last_task_used_altivec == current)
425                 last_task_used_altivec = 0;
426         memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
427         current->thread.fpscr = 0;
428 #ifdef CONFIG_ALTIVEC
429         memset(current->thread.vr, 0, sizeof(current->thread.vr));
430         memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
431         current->thread.vrsave = 0;
432 #endif /* CONFIG_ALTIVEC */
433 }
434
435 /*
436  * Support for the PR_GET/SET_FPEXC prctl() calls.
437  */
438 static inline unsigned int __unpack_fe01(unsigned int msr_bits)
439 {
440         return ((msr_bits & MSR_FE0) >> 10) | ((msr_bits & MSR_FE1) >> 8);
441 }
442
443 static inline unsigned int __pack_fe01(unsigned int fpmode)
444 {
445         return ((fpmode << 10) & MSR_FE0) | ((fpmode << 8) & MSR_FE1);
446 }
447
448 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
449 {
450         struct pt_regs *regs = tsk->thread.regs;
451
452         if (val > PR_FP_EXC_PRECISE)
453                 return -EINVAL;
454         tsk->thread.fpexc_mode = __pack_fe01(val);
455         if (regs != NULL && (regs->msr & MSR_FP) != 0)
456                 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
457                         | tsk->thread.fpexc_mode;
458         return 0;
459 }
460
461 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
462 {
463         unsigned int val;
464
465         val = __unpack_fe01(tsk->thread.fpexc_mode);
466         return put_user(val, (unsigned int *) adr);
467 }
468
469 int sys_clone(int p1, int p2, int p3, int p4, int p5, int p6,
470               struct pt_regs *regs)
471 {
472         return do_fork(p1, regs->gpr[1], regs, 0);
473 }
474
475 int sys_fork(int p1, int p2, int p3, int p4, int p5, int p6,
476              struct pt_regs *regs)
477 {
478         return do_fork(SIGCHLD, regs->gpr[1], regs, 0);
479 }
480
481 int sys_vfork(int p1, int p2, int p3, int p4, int p5, int p6,
482               struct pt_regs *regs)
483 {
484         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0);
485 }
486
487 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
488                unsigned long a3, unsigned long a4, unsigned long a5,
489                struct pt_regs *regs)
490 {
491         int error;
492         char * filename;
493         
494         filename = getname((char *) a0);
495         error = PTR_ERR(filename);
496         if (IS_ERR(filename))
497                 goto out;
498         if (regs->msr & MSR_FP)
499                 giveup_fpu(current);
500 #ifdef CONFIG_ALTIVEC
501         if (regs->msr & MSR_VEC)
502                 giveup_altivec(current);
503 #endif /* CONFIG_ALTIVEC */ 
504         error = do_execve(filename, (char **) a1, (char **) a2, regs);
505         if (error == 0)
506                 current->ptrace &= ~PT_DTRACE;
507         putname(filename);
508 out:
509         return error;
510 }
511
512 void
513 print_backtrace(unsigned long *sp)
514 {
515         int cnt = 0;
516         unsigned long i;
517
518         printk("Call backtrace: ");
519         while (sp) {
520                 if (__get_user( i, &sp[1] ))
521                         break;
522                 if (cnt++ % 7 == 0)
523                         printk("\n");
524                 printk("%08lX ", i);
525                 if (cnt > 32) break;
526                 if (__get_user(sp, (unsigned long **)sp))
527                         break;
528         }
529         printk("\n");
530 }
531
532 void show_trace_task(struct task_struct *tsk)
533 {
534         unsigned long stack_top = (unsigned long) tsk + THREAD_SIZE;
535         unsigned long sp, prev_sp;
536         int count = 0;
537
538         if (tsk == NULL)
539                 return;
540         sp = (unsigned long) &tsk->thread.ksp;
541         do {
542                 prev_sp = sp;
543                 sp = *(unsigned long *)sp;
544                 if (sp <= prev_sp || sp >= stack_top || (sp & 3) != 0)
545                         break;
546                 if (count > 0)
547                         printk("[%08lx] ", *(unsigned long *)(sp + 4));
548         } while (++count < 16);
549         if (count > 1)
550                 printk("\n");
551 }
552
553 #if 0
554 /*
555  * Low level print for debugging - Cort
556  */
557 int __init ll_printk(const char *fmt, ...)
558 {
559         va_list args;
560         char buf[256];
561         int i;
562
563         va_start(args, fmt);
564         i=vsprintf(buf,fmt,args);
565         ll_puts(buf);
566         va_end(args);
567         return i;
568 }
569
570 int lines = 24, cols = 80;
571 int orig_x = 0, orig_y = 0;
572
573 void puthex(unsigned long val)
574 {
575         unsigned char buf[10];
576         int i;
577         for (i = 7;  i >= 0;  i--)
578         {
579                 buf[i] = "0123456789ABCDEF"[val & 0x0F];
580                 val >>= 4;
581         }
582         buf[8] = '\0';
583         prom_print(buf);
584 }
585
586 void __init ll_puts(const char *s)
587 {
588         int x,y;
589         char *vidmem = (char *)/*(_ISA_MEM_BASE + 0xB8000) */0xD00B8000;
590         char c;
591         extern int mem_init_done;
592
593         if ( mem_init_done ) /* assume this means we can printk */
594         {
595                 printk(s);
596                 return;
597         }
598
599 #if 0   
600         if ( have_of )
601         {
602                 prom_print(s);
603                 return;
604         }
605 #endif
606
607         /*
608          * can't ll_puts on chrp without openfirmware yet.
609          * vidmem just needs to be setup for it.
610          * -- Cort
611          */
612         if ( _machine != _MACH_prep )
613                 return;
614         x = orig_x;
615         y = orig_y;
616
617         while ( ( c = *s++ ) != '\0' ) {
618                 if ( c == '\n' ) {
619                         x = 0;
620                         if ( ++y >= lines ) {
621                                 /*scroll();*/
622                                 /*y--;*/
623                                 y = 0;
624                         }
625                 } else {
626                         vidmem [ ( x + cols * y ) * 2 ] = c; 
627                         if ( ++x >= cols ) {
628                                 x = 0;
629                                 if ( ++y >= lines ) {
630                                         /*scroll();*/
631                                         /*y--;*/
632                                         y = 0;
633                                 }
634                         }
635                 }
636         }
637
638         orig_x = x;
639         orig_y = y;
640 }
641 #endif
642
643 /*
644  * These bracket the sleeping functions..
645  */
646 extern void scheduling_functions_start_here(void);
647 extern void scheduling_functions_end_here(void);
648 #define first_sched    ((unsigned long) scheduling_functions_start_here)
649 #define last_sched     ((unsigned long) scheduling_functions_end_here)
650
651 unsigned long get_wchan(struct task_struct *p)
652 {
653         unsigned long ip, sp;
654         unsigned long stack_page = (unsigned long) p;
655         int count = 0;
656         if (!p || p == current || p->state == TASK_RUNNING)
657                 return 0;
658         sp = p->thread.ksp;
659         do {
660                 sp = *(unsigned long *)sp;
661                 if (sp < stack_page || sp >= stack_page + 8188)
662                         return 0;
663                 if (count > 0) {
664                         ip = *(unsigned long *)(sp + 4);
665                         if (ip < first_sched || ip >= last_sched)
666                                 return ip;
667                 }
668         } while (count++ < 16);
669         return 0;
670 }