kernel.org linux-2.6.10
[linux-2.6.git] / arch / ppc64 / kernel / process.c
1 /*
2  *  linux/arch/ppc64/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/module.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/slab.h>
30 #include <linux/user.h>
31 #include <linux/elf.h>
32 #include <linux/init.h>
33 #include <linux/init_task.h>
34 #include <linux/prctl.h>
35 #include <linux/ptrace.h>
36 #include <linux/kallsyms.h>
37 #include <linux/interrupt.h>
38 #include <linux/version.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/mmu_context.h>
47 #include <asm/prom.h>
48 #include <asm/ppcdebug.h>
49 #include <asm/machdep.h>
50 #include <asm/iSeries/HvCallHpt.h>
51 #include <asm/cputable.h>
52 #include <asm/sections.h>
53 #include <asm/tlbflush.h>
54
55 #ifndef CONFIG_SMP
56 struct task_struct *last_task_used_math = NULL;
57 struct task_struct *last_task_used_altivec = NULL;
58 #endif
59
60 struct mm_struct ioremap_mm = {
61         .pgd            = ioremap_dir,
62         .mm_users       = ATOMIC_INIT(2),
63         .mm_count       = ATOMIC_INIT(1),
64         .cpu_vm_mask    = CPU_MASK_ALL,
65         .page_table_lock = SPIN_LOCK_UNLOCKED,
66 };
67
68 /*
69  * Make sure the floating-point register state in the
70  * the thread_struct is up to date for task tsk.
71  */
72 void flush_fp_to_thread(struct task_struct *tsk)
73 {
74         if (tsk->thread.regs) {
75                 /*
76                  * We need to disable preemption here because if we didn't,
77                  * another process could get scheduled after the regs->msr
78                  * test but before we have finished saving the FP registers
79                  * to the thread_struct.  That process could take over the
80                  * FPU, and then when we get scheduled again we would store
81                  * bogus values for the remaining FP registers.
82                  */
83                 preempt_disable();
84                 if (tsk->thread.regs->msr & MSR_FP) {
85 #ifdef CONFIG_SMP
86                         /*
87                          * This should only ever be called for current or
88                          * for a stopped child process.  Since we save away
89                          * the FP register state on context switch on SMP,
90                          * there is something wrong if a stopped child appears
91                          * to still have its FP state in the CPU registers.
92                          */
93                         BUG_ON(tsk != current);
94 #endif
95                         giveup_fpu(current);
96                 }
97                 preempt_enable();
98         }
99 }
100
101 void enable_kernel_fp(void)
102 {
103         WARN_ON(preemptible());
104
105 #ifdef CONFIG_SMP
106         if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
107                 giveup_fpu(current);
108         else
109                 giveup_fpu(NULL);       /* just enables FP for kernel */
110 #else
111         giveup_fpu(last_task_used_math);
112 #endif /* CONFIG_SMP */
113 }
114 EXPORT_SYMBOL(enable_kernel_fp);
115
116 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
117 {
118         if (!tsk->thread.regs)
119                 return 0;
120         flush_fp_to_thread(current);
121
122         memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
123
124         return 1;
125 }
126
127 #ifdef CONFIG_ALTIVEC
128
129 void enable_kernel_altivec(void)
130 {
131         WARN_ON(preemptible());
132
133 #ifdef CONFIG_SMP
134         if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
135                 giveup_altivec(current);
136         else
137                 giveup_altivec(NULL);   /* just enables FP for kernel */
138 #else
139         giveup_altivec(last_task_used_altivec);
140 #endif /* CONFIG_SMP */
141 }
142 EXPORT_SYMBOL(enable_kernel_altivec);
143
144 /*
145  * Make sure the VMX/Altivec register state in the
146  * the thread_struct is up to date for task tsk.
147  */
148 void flush_altivec_to_thread(struct task_struct *tsk)
149 {
150         if (tsk->thread.regs) {
151                 preempt_disable();
152                 if (tsk->thread.regs->msr & MSR_VEC) {
153 #ifdef CONFIG_SMP
154                         BUG_ON(tsk != current);
155 #endif
156                         giveup_altivec(current);
157                 }
158                 preempt_enable();
159         }
160 }
161
162 int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
163 {
164         flush_altivec_to_thread(current);
165         memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
166         return 1;
167 }
168
169 #endif /* CONFIG_ALTIVEC */
170
171 struct task_struct *__switch_to(struct task_struct *prev,
172                                 struct task_struct *new)
173 {
174         struct thread_struct *new_thread, *old_thread;
175         unsigned long flags;
176         struct task_struct *last;
177
178 #ifdef CONFIG_SMP
179         /* avoid complexity of lazy save/restore of fpu
180          * by just saving it every time we switch out if
181          * this task used the fpu during the last quantum.
182          * 
183          * If it tries to use the fpu again, it'll trap and
184          * reload its fp regs.  So we don't have to do a restore
185          * every switch, just a save.
186          *  -- Cort
187          */
188         if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
189                 giveup_fpu(prev);
190 #ifdef CONFIG_ALTIVEC
191         if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
192                 giveup_altivec(prev);
193 #endif /* CONFIG_ALTIVEC */
194 #endif /* CONFIG_SMP */
195
196 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
197         /* Avoid the trap.  On smp this this never happens since
198          * we don't set last_task_used_altivec -- Cort
199          */
200         if (new->thread.regs && last_task_used_altivec == new)
201                 new->thread.regs->msr |= MSR_VEC;
202 #endif /* CONFIG_ALTIVEC */
203
204         flush_tlb_pending();
205
206         new_thread = &new->thread;
207         old_thread = &current->thread;
208
209         local_irq_save(flags);
210         last = _switch(old_thread, new_thread);
211
212         local_irq_restore(flags);
213
214         return last;
215 }
216
217 void show_regs(struct pt_regs * regs)
218 {
219         int i;
220         unsigned long trap;
221
222         printk("NIP: %016lX XER: %016lX LR: %016lX\n",
223                regs->nip, regs->xer, regs->link);
224         printk("REGS: %p TRAP: %04lx   %s  (%s)\n",
225                regs, regs->trap, print_tainted(), UTS_RELEASE);
226         printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
227                regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
228                regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
229                regs->msr&MSR_IR ? 1 : 0,
230                regs->msr&MSR_DR ? 1 : 0);
231         trap = TRAP(regs);
232         if (trap == 0x300 || trap == 0x380 || trap == 0x600)
233                 printk("DAR: %016lx, DSISR: %016lx\n", regs->dar, regs->dsisr);
234         printk("TASK: %p[%d] '%s' THREAD: %p",
235                current, current->pid, current->comm, current->thread_info);
236
237 #ifdef CONFIG_SMP
238         printk(" CPU: %d", smp_processor_id());
239 #endif /* CONFIG_SMP */
240
241         for (i = 0; i < 32; i++) {
242                 if ((i % 4) == 0) {
243                         printk("\n" KERN_INFO "GPR%02d: ", i);
244                 }
245
246                 printk("%016lX ", regs->gpr[i]);
247                 if (i == 13 && !FULL_REGS(regs))
248                         break;
249         }
250         printk("\n");
251         /*
252          * Lookup NIP late so we have the best change of getting the
253          * above info out without failing
254          */
255         printk("NIP [%016lx] ", regs->nip);
256         print_symbol("%s\n", regs->nip);
257         printk("LR [%016lx] ", regs->link);
258         print_symbol("%s\n", regs->link);
259         show_stack(current, (unsigned long *)regs->gpr[1]);
260 }
261
262 void exit_thread(void)
263 {
264 #ifndef CONFIG_SMP
265         if (last_task_used_math == current)
266                 last_task_used_math = NULL;
267 #ifdef CONFIG_ALTIVEC
268         if (last_task_used_altivec == current)
269                 last_task_used_altivec = NULL;
270 #endif /* CONFIG_ALTIVEC */
271 #endif /* CONFIG_SMP */
272 }
273
274 void flush_thread(void)
275 {
276         struct thread_info *t = current_thread_info();
277
278         if (t->flags & _TIF_ABI_PENDING)
279                 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
280
281 #ifndef CONFIG_SMP
282         if (last_task_used_math == current)
283                 last_task_used_math = NULL;
284 #ifdef CONFIG_ALTIVEC
285         if (last_task_used_altivec == current)
286                 last_task_used_altivec = NULL;
287 #endif /* CONFIG_ALTIVEC */
288 #endif /* CONFIG_SMP */
289 }
290
291 void
292 release_thread(struct task_struct *t)
293 {
294 }
295
296
297 /*
298  * This gets called before we allocate a new thread and copy
299  * the current task into it.
300  */
301 void prepare_to_copy(struct task_struct *tsk)
302 {
303         flush_fp_to_thread(current);
304         flush_altivec_to_thread(current);
305 }
306
307 /*
308  * Copy a thread..
309  */
310 int
311 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
312             unsigned long unused, struct task_struct *p, struct pt_regs *regs)
313 {
314         struct pt_regs *childregs, *kregs;
315         extern void ret_from_fork(void);
316         unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
317
318         /* Copy registers */
319         sp -= sizeof(struct pt_regs);
320         childregs = (struct pt_regs *) sp;
321         *childregs = *regs;
322         if ((childregs->msr & MSR_PR) == 0) {
323                 /* for kernel thread, set stackptr in new task */
324                 childregs->gpr[1] = sp + sizeof(struct pt_regs);
325                 p->thread.regs = NULL;  /* no user register state */
326                 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
327 #ifdef CONFIG_PPC_ISERIES
328                 set_ti_thread_flag(p->thread_info, TIF_RUN_LIGHT);
329 #endif
330         } else {
331                 childregs->gpr[1] = usp;
332                 p->thread.regs = childregs;
333                 if (clone_flags & CLONE_SETTLS) {
334                         if (test_thread_flag(TIF_32BIT))
335                                 childregs->gpr[2] = childregs->gpr[6];
336                         else
337                                 childregs->gpr[13] = childregs->gpr[6];
338                 }
339         }
340         childregs->gpr[3] = 0;  /* Result from fork() */
341         sp -= STACK_FRAME_OVERHEAD;
342
343         /*
344          * The way this works is that at some point in the future
345          * some task will call _switch to switch to the new task.
346          * That will pop off the stack frame created below and start
347          * the new task running at ret_from_fork.  The new task will
348          * do some house keeping and then return from the fork or clone
349          * system call, using the stack frame created above.
350          */
351         sp -= sizeof(struct pt_regs);
352         kregs = (struct pt_regs *) sp;
353         sp -= STACK_FRAME_OVERHEAD;
354         p->thread.ksp = sp;
355         if (cur_cpu_spec->cpu_features & CPU_FTR_SLB) {
356                 unsigned long sp_vsid = get_kernel_vsid(sp);
357
358                 sp_vsid <<= SLB_VSID_SHIFT;
359                 sp_vsid |= SLB_VSID_KERNEL;
360                 if (cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE)
361                         sp_vsid |= SLB_VSID_L;
362
363                 p->thread.ksp_vsid = sp_vsid;
364         }
365
366         /*
367          * The PPC64 ABI makes use of a TOC to contain function 
368          * pointers.  The function (ret_from_except) is actually a pointer
369          * to the TOC entry.  The first entry is a pointer to the actual
370          * function.
371          */
372         kregs->nip = *((unsigned long *)ret_from_fork);
373
374         return 0;
375 }
376
377 /*
378  * Set up a thread for executing a new program
379  */
380 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
381 {
382         unsigned long entry, toc, load_addr = regs->gpr[2];
383
384         /* fdptr is a relocated pointer to the function descriptor for
385          * the elf _start routine.  The first entry in the function
386          * descriptor is the entry address of _start and the second
387          * entry is the TOC value we need to use.
388          */
389         set_fs(USER_DS);
390         __get_user(entry, (unsigned long __user *)fdptr);
391         __get_user(toc, (unsigned long __user *)fdptr+1);
392
393         /* Check whether the e_entry function descriptor entries
394          * need to be relocated before we can use them.
395          */
396         if (load_addr != 0) {
397                 entry += load_addr;
398                 toc   += load_addr;
399         }
400
401         /*
402          * If we exec out of a kernel thread then thread.regs will not be
403          * set. Do it now.
404          */
405         if (!current->thread.regs) {
406                 unsigned long childregs = (unsigned long)current->thread_info +
407                                                 THREAD_SIZE;
408                 childregs -= sizeof(struct pt_regs);
409                 current->thread.regs = (struct pt_regs *)childregs;
410         }
411
412         regs->nip = entry;
413         regs->gpr[1] = sp;
414         regs->gpr[2] = toc;
415         regs->msr = MSR_USER64;
416 #ifndef CONFIG_SMP
417         if (last_task_used_math == current)
418                 last_task_used_math = 0;
419 #endif /* CONFIG_SMP */
420         memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
421         current->thread.fpscr = 0;
422 #ifdef CONFIG_ALTIVEC
423 #ifndef CONFIG_SMP
424         if (last_task_used_altivec == current)
425                 last_task_used_altivec = 0;
426 #endif /* CONFIG_SMP */
427         memset(current->thread.vr, 0, sizeof(current->thread.vr));
428         current->thread.vscr.u[0] = 0;
429         current->thread.vscr.u[1] = 0;
430         current->thread.vscr.u[2] = 0;
431         current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
432         current->thread.vrsave = 0;
433         current->thread.used_vr = 0;
434 #endif /* CONFIG_ALTIVEC */
435 }
436
437 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
438 {
439         struct pt_regs *regs = tsk->thread.regs;
440
441         if (val > PR_FP_EXC_PRECISE)
442                 return -EINVAL;
443         tsk->thread.fpexc_mode = __pack_fe01(val);
444         if (regs != NULL && (regs->msr & MSR_FP) != 0)
445                 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
446                         | tsk->thread.fpexc_mode;
447         return 0;
448 }
449
450 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
451 {
452         unsigned int val;
453
454         val = __unpack_fe01(tsk->thread.fpexc_mode);
455         return put_user(val, (unsigned int __user *) adr);
456 }
457
458 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
459               unsigned long p4, unsigned long p5, unsigned long p6,
460               struct pt_regs *regs)
461 {
462         unsigned long parent_tidptr = 0;
463         unsigned long child_tidptr = 0;
464
465         if (p2 == 0)
466                 p2 = regs->gpr[1];      /* stack pointer for child */
467
468         if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
469                            CLONE_CHILD_CLEARTID)) {
470                 parent_tidptr = p3;
471                 child_tidptr = p5;
472                 if (test_thread_flag(TIF_32BIT)) {
473                         parent_tidptr &= 0xffffffff;
474                         child_tidptr &= 0xffffffff;
475                 }
476         }
477
478         return do_fork(clone_flags, p2, regs, 0,
479                     (int __user *)parent_tidptr, (int __user *)child_tidptr);
480 }
481
482 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
483              unsigned long p4, unsigned long p5, unsigned long p6,
484              struct pt_regs *regs)
485 {
486         return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
487 }
488
489 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
490               unsigned long p4, unsigned long p5, unsigned long p6,
491               struct pt_regs *regs)
492 {
493         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
494                     NULL, NULL);
495 }
496
497 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
498                unsigned long a3, unsigned long a4, unsigned long a5,
499                struct pt_regs *regs)
500 {
501         int error;
502         char * filename;
503         
504         filename = getname((char __user *) a0);
505         error = PTR_ERR(filename);
506         if (IS_ERR(filename))
507                 goto out;
508         flush_fp_to_thread(current);
509         flush_altivec_to_thread(current);
510         error = do_execve(filename, (char __user * __user *) a1,
511                                     (char __user * __user *) a2, regs);
512   
513         if (error == 0) {
514                 task_lock(current);
515                 current->ptrace &= ~PT_DTRACE;
516                 task_unlock(current);
517         }
518         putname(filename);
519
520 out:
521         return error;
522 }
523
524 static int kstack_depth_to_print = 64;
525
526 static int validate_sp(unsigned long sp, struct task_struct *p,
527                        unsigned long nbytes)
528 {
529         unsigned long stack_page = (unsigned long)p->thread_info;
530
531         if (sp >= stack_page + sizeof(struct thread_struct)
532             && sp <= stack_page + THREAD_SIZE - nbytes)
533                 return 1;
534
535 #ifdef CONFIG_IRQSTACKS
536         stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
537         if (sp >= stack_page + sizeof(struct thread_struct)
538             && sp <= stack_page + THREAD_SIZE - nbytes)
539                 return 1;
540
541         stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
542         if (sp >= stack_page + sizeof(struct thread_struct)
543             && sp <= stack_page + THREAD_SIZE - nbytes)
544                 return 1;
545 #endif
546
547         return 0;
548 }
549
550 unsigned long get_wchan(struct task_struct *p)
551 {
552         unsigned long ip, sp;
553         int count = 0;
554
555         if (!p || p == current || p->state == TASK_RUNNING)
556                 return 0;
557
558         sp = p->thread.ksp;
559         if (!validate_sp(sp, p, 112))
560                 return 0;
561
562         do {
563                 sp = *(unsigned long *)sp;
564                 if (!validate_sp(sp, p, 112))
565                         return 0;
566                 if (count > 0) {
567                         ip = *(unsigned long *)(sp + 16);
568                         if (!in_sched_functions(ip))
569                                 return ip;
570                 }
571         } while (count++ < 16);
572         return 0;
573 }
574
575 void show_stack(struct task_struct *p, unsigned long *_sp)
576 {
577         unsigned long ip, newsp, lr;
578         int count = 0;
579         unsigned long sp = (unsigned long)_sp;
580         int firstframe = 1;
581
582         if (sp == 0) {
583                 if (p) {
584                         sp = p->thread.ksp;
585                 } else {
586                         sp = __get_SP();
587                         p = current;
588                 }
589         }
590
591         lr = 0;
592         printk("Call Trace:\n");
593         do {
594                 if (!validate_sp(sp, p, 112))
595                         return;
596
597                 _sp = (unsigned long *) sp;
598                 newsp = _sp[0];
599                 ip = _sp[2];
600                 if (!firstframe || ip != lr) {
601                         printk("[%016lx] [%016lx] ", sp, ip);
602                         print_symbol("%s", ip);
603                         if (firstframe)
604                                 printk(" (unreliable)");
605                         printk("\n");
606                 }
607                 firstframe = 0;
608
609                 /*
610                  * See if this is an exception frame.
611                  * We look for the "regshere" marker in the current frame.
612                  */
613                 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
614                     && _sp[12] == 0x7265677368657265ul) {
615                         struct pt_regs *regs = (struct pt_regs *)
616                                 (sp + STACK_FRAME_OVERHEAD);
617                         printk("--- Exception: %lx", regs->trap);
618                         print_symbol(" at %s\n", regs->nip);
619                         lr = regs->link;
620                         print_symbol("    LR = %s\n", lr);
621                         firstframe = 1;
622                 }
623
624                 sp = newsp;
625         } while (count++ < kstack_depth_to_print);
626 }
627
628 void dump_stack(void)
629 {
630         show_stack(current, (unsigned long *)__get_SP());
631 }
632 EXPORT_SYMBOL(dump_stack);