vserver 1.9.5.x5
[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/utsname.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 static int instructions_to_print = 16;
218
219 static void show_instructions(struct pt_regs *regs)
220 {
221         int i;
222         unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
223                         sizeof(int));
224
225         printk("Instruction dump:");
226
227         for (i = 0; i < instructions_to_print; i++) {
228                 int instr;
229
230                 if (!(i % 8))
231                         printk("\n");
232
233                 if (((REGION_ID(pc) != KERNEL_REGION_ID) &&
234                      (REGION_ID(pc) != VMALLOC_REGION_ID)) ||
235                      __get_user(instr, (unsigned int *)pc)) {
236                         printk("XXXXXXXX ");
237                 } else {
238                         if (regs->nip == pc)
239                                 printk("<%08x> ", instr);
240                         else
241                                 printk("%08x ", instr);
242                 }
243
244                 pc += sizeof(int);
245         }
246
247         printk("\n");
248 }
249
250 void show_regs(struct pt_regs * regs)
251 {
252         int i;
253         unsigned long trap;
254
255         printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
256                regs->nip, (unsigned int)regs->xer, regs->link, regs->ctr);
257         printk("REGS: %p TRAP: %04lx   %s  (%s)\n",
258                regs, regs->trap, print_tainted(), system_utsname.release);
259         printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
260                "IR/DR: %01x%01x CR: %08X\n",
261                regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
262                regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
263                regs->msr&MSR_IR ? 1 : 0,
264                regs->msr&MSR_DR ? 1 : 0,
265                (unsigned int)regs->ccr);
266         trap = TRAP(regs);
267         printk("DAR: %016lx DSISR: %016lx\n", regs->dar, regs->dsisr);
268         printk("TASK: %p[%d] '%s' THREAD: %p",
269                current, current->pid, current->comm, current->thread_info);
270
271 #ifdef CONFIG_SMP
272         printk(" CPU: %d", smp_processor_id());
273 #endif /* CONFIG_SMP */
274
275         for (i = 0; i < 32; i++) {
276                 if ((i % 4) == 0) {
277                         printk("\n" KERN_INFO "GPR%02d: ", i);
278                 }
279
280                 printk("%016lX ", regs->gpr[i]);
281                 if (i == 13 && !FULL_REGS(regs))
282                         break;
283         }
284         printk("\n");
285         /*
286          * Lookup NIP late so we have the best change of getting the
287          * above info out without failing
288          */
289         printk("NIP [%016lx] ", regs->nip);
290         print_symbol("%s\n", regs->nip);
291         printk("LR [%016lx] ", regs->link);
292         print_symbol("%s\n", regs->link);
293         show_stack(current, (unsigned long *)regs->gpr[1]);
294         if (!user_mode(regs))
295                 show_instructions(regs);
296 }
297
298 void exit_thread(void)
299 {
300 #ifndef CONFIG_SMP
301         if (last_task_used_math == current)
302                 last_task_used_math = NULL;
303 #ifdef CONFIG_ALTIVEC
304         if (last_task_used_altivec == current)
305                 last_task_used_altivec = NULL;
306 #endif /* CONFIG_ALTIVEC */
307 #endif /* CONFIG_SMP */
308 }
309
310 void flush_thread(void)
311 {
312         struct thread_info *t = current_thread_info();
313
314         if (t->flags & _TIF_ABI_PENDING)
315                 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
316
317 #ifndef CONFIG_SMP
318         if (last_task_used_math == current)
319                 last_task_used_math = NULL;
320 #ifdef CONFIG_ALTIVEC
321         if (last_task_used_altivec == current)
322                 last_task_used_altivec = NULL;
323 #endif /* CONFIG_ALTIVEC */
324 #endif /* CONFIG_SMP */
325 }
326
327 void
328 release_thread(struct task_struct *t)
329 {
330 }
331
332
333 /*
334  * This gets called before we allocate a new thread and copy
335  * the current task into it.
336  */
337 void prepare_to_copy(struct task_struct *tsk)
338 {
339         flush_fp_to_thread(current);
340         flush_altivec_to_thread(current);
341 }
342
343 /*
344  * Copy a thread..
345  */
346 int
347 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
348             unsigned long unused, struct task_struct *p, struct pt_regs *regs)
349 {
350         struct pt_regs *childregs, *kregs;
351         extern void ret_from_fork(void);
352         unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
353
354         /* Copy registers */
355         sp -= sizeof(struct pt_regs);
356         childregs = (struct pt_regs *) sp;
357         *childregs = *regs;
358         if ((childregs->msr & MSR_PR) == 0) {
359                 /* for kernel thread, set stackptr in new task */
360                 childregs->gpr[1] = sp + sizeof(struct pt_regs);
361                 p->thread.regs = NULL;  /* no user register state */
362                 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
363 #ifdef CONFIG_PPC_ISERIES
364                 set_ti_thread_flag(p->thread_info, TIF_RUN_LIGHT);
365 #endif
366         } else {
367                 childregs->gpr[1] = usp;
368                 p->thread.regs = childregs;
369                 if (clone_flags & CLONE_SETTLS) {
370                         if (test_thread_flag(TIF_32BIT))
371                                 childregs->gpr[2] = childregs->gpr[6];
372                         else
373                                 childregs->gpr[13] = childregs->gpr[6];
374                 }
375         }
376         childregs->gpr[3] = 0;  /* Result from fork() */
377         sp -= STACK_FRAME_OVERHEAD;
378
379         /*
380          * The way this works is that at some point in the future
381          * some task will call _switch to switch to the new task.
382          * That will pop off the stack frame created below and start
383          * the new task running at ret_from_fork.  The new task will
384          * do some house keeping and then return from the fork or clone
385          * system call, using the stack frame created above.
386          */
387         sp -= sizeof(struct pt_regs);
388         kregs = (struct pt_regs *) sp;
389         sp -= STACK_FRAME_OVERHEAD;
390         p->thread.ksp = sp;
391         if (cur_cpu_spec->cpu_features & CPU_FTR_SLB) {
392                 unsigned long sp_vsid = get_kernel_vsid(sp);
393
394                 sp_vsid <<= SLB_VSID_SHIFT;
395                 sp_vsid |= SLB_VSID_KERNEL;
396                 if (cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE)
397                         sp_vsid |= SLB_VSID_L;
398
399                 p->thread.ksp_vsid = sp_vsid;
400         }
401
402         /*
403          * The PPC64 ABI makes use of a TOC to contain function 
404          * pointers.  The function (ret_from_except) is actually a pointer
405          * to the TOC entry.  The first entry is a pointer to the actual
406          * function.
407          */
408         kregs->nip = *((unsigned long *)ret_from_fork);
409
410         return 0;
411 }
412
413 /*
414  * Set up a thread for executing a new program
415  */
416 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
417 {
418         unsigned long entry, toc, load_addr = regs->gpr[2];
419
420         /* fdptr is a relocated pointer to the function descriptor for
421          * the elf _start routine.  The first entry in the function
422          * descriptor is the entry address of _start and the second
423          * entry is the TOC value we need to use.
424          */
425         set_fs(USER_DS);
426         __get_user(entry, (unsigned long __user *)fdptr);
427         __get_user(toc, (unsigned long __user *)fdptr+1);
428
429         /* Check whether the e_entry function descriptor entries
430          * need to be relocated before we can use them.
431          */
432         if (load_addr != 0) {
433                 entry += load_addr;
434                 toc   += load_addr;
435         }
436
437         /*
438          * If we exec out of a kernel thread then thread.regs will not be
439          * set. Do it now.
440          */
441         if (!current->thread.regs) {
442                 unsigned long childregs = (unsigned long)current->thread_info +
443                                                 THREAD_SIZE;
444                 childregs -= sizeof(struct pt_regs);
445                 current->thread.regs = (struct pt_regs *)childregs;
446         }
447
448         regs->nip = entry;
449         regs->gpr[1] = sp;
450         regs->gpr[2] = toc;
451         regs->msr = MSR_USER64;
452 #ifndef CONFIG_SMP
453         if (last_task_used_math == current)
454                 last_task_used_math = 0;
455 #endif /* CONFIG_SMP */
456         memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
457         current->thread.fpscr = 0;
458 #ifdef CONFIG_ALTIVEC
459 #ifndef CONFIG_SMP
460         if (last_task_used_altivec == current)
461                 last_task_used_altivec = 0;
462 #endif /* CONFIG_SMP */
463         memset(current->thread.vr, 0, sizeof(current->thread.vr));
464         current->thread.vscr.u[0] = 0;
465         current->thread.vscr.u[1] = 0;
466         current->thread.vscr.u[2] = 0;
467         current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
468         current->thread.vrsave = 0;
469         current->thread.used_vr = 0;
470 #endif /* CONFIG_ALTIVEC */
471 }
472
473 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
474 {
475         struct pt_regs *regs = tsk->thread.regs;
476
477         if (val > PR_FP_EXC_PRECISE)
478                 return -EINVAL;
479         tsk->thread.fpexc_mode = __pack_fe01(val);
480         if (regs != NULL && (regs->msr & MSR_FP) != 0)
481                 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
482                         | tsk->thread.fpexc_mode;
483         return 0;
484 }
485
486 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
487 {
488         unsigned int val;
489
490         val = __unpack_fe01(tsk->thread.fpexc_mode);
491         return put_user(val, (unsigned int __user *) adr);
492 }
493
494 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
495               unsigned long p4, unsigned long p5, unsigned long p6,
496               struct pt_regs *regs)
497 {
498         unsigned long parent_tidptr = 0;
499         unsigned long child_tidptr = 0;
500
501         if (p2 == 0)
502                 p2 = regs->gpr[1];      /* stack pointer for child */
503
504         if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
505                            CLONE_CHILD_CLEARTID)) {
506                 parent_tidptr = p3;
507                 child_tidptr = p5;
508                 if (test_thread_flag(TIF_32BIT)) {
509                         parent_tidptr &= 0xffffffff;
510                         child_tidptr &= 0xffffffff;
511                 }
512         }
513
514         return do_fork(clone_flags, p2, regs, 0,
515                     (int __user *)parent_tidptr, (int __user *)child_tidptr);
516 }
517
518 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
519              unsigned long p4, unsigned long p5, unsigned long p6,
520              struct pt_regs *regs)
521 {
522         return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
523 }
524
525 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
526               unsigned long p4, unsigned long p5, unsigned long p6,
527               struct pt_regs *regs)
528 {
529         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
530                     NULL, NULL);
531 }
532
533 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
534                unsigned long a3, unsigned long a4, unsigned long a5,
535                struct pt_regs *regs)
536 {
537         int error;
538         char * filename;
539         
540         filename = getname((char __user *) a0);
541         error = PTR_ERR(filename);
542         if (IS_ERR(filename))
543                 goto out;
544         flush_fp_to_thread(current);
545         flush_altivec_to_thread(current);
546         error = do_execve(filename, (char __user * __user *) a1,
547                                     (char __user * __user *) a2, regs);
548   
549         if (error == 0) {
550                 task_lock(current);
551                 current->ptrace &= ~PT_DTRACE;
552                 task_unlock(current);
553         }
554         putname(filename);
555
556 out:
557         return error;
558 }
559
560 static int kstack_depth_to_print = 64;
561
562 static int validate_sp(unsigned long sp, struct task_struct *p,
563                        unsigned long nbytes)
564 {
565         unsigned long stack_page = (unsigned long)p->thread_info;
566
567         if (sp >= stack_page + sizeof(struct thread_struct)
568             && sp <= stack_page + THREAD_SIZE - nbytes)
569                 return 1;
570
571 #ifdef CONFIG_IRQSTACKS
572         stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
573         if (sp >= stack_page + sizeof(struct thread_struct)
574             && sp <= stack_page + THREAD_SIZE - nbytes)
575                 return 1;
576
577         stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
578         if (sp >= stack_page + sizeof(struct thread_struct)
579             && sp <= stack_page + THREAD_SIZE - nbytes)
580                 return 1;
581 #endif
582
583         return 0;
584 }
585
586 unsigned long get_wchan(struct task_struct *p)
587 {
588         unsigned long ip, sp;
589         int count = 0;
590
591         if (!p || p == current || p->state == TASK_RUNNING)
592                 return 0;
593
594         sp = p->thread.ksp;
595         if (!validate_sp(sp, p, 112))
596                 return 0;
597
598         do {
599                 sp = *(unsigned long *)sp;
600                 if (!validate_sp(sp, p, 112))
601                         return 0;
602                 if (count > 0) {
603                         ip = *(unsigned long *)(sp + 16);
604                         if (!in_sched_functions(ip))
605                                 return ip;
606                 }
607         } while (count++ < 16);
608         return 0;
609 }
610
611 void show_stack(struct task_struct *p, unsigned long *_sp)
612 {
613         unsigned long ip, newsp, lr;
614         int count = 0;
615         unsigned long sp = (unsigned long)_sp;
616         int firstframe = 1;
617
618         if (sp == 0) {
619                 if (p) {
620                         sp = p->thread.ksp;
621                 } else {
622                         sp = __get_SP();
623                         p = current;
624                 }
625         }
626
627         lr = 0;
628         printk("Call Trace:\n");
629         do {
630                 if (!validate_sp(sp, p, 112))
631                         return;
632
633                 _sp = (unsigned long *) sp;
634                 newsp = _sp[0];
635                 ip = _sp[2];
636                 if (!firstframe || ip != lr) {
637                         printk("[%016lx] [%016lx] ", sp, ip);
638                         print_symbol("%s", ip);
639                         if (firstframe)
640                                 printk(" (unreliable)");
641                         printk("\n");
642                 }
643                 firstframe = 0;
644
645                 /*
646                  * See if this is an exception frame.
647                  * We look for the "regshere" marker in the current frame.
648                  */
649                 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
650                     && _sp[12] == 0x7265677368657265ul) {
651                         struct pt_regs *regs = (struct pt_regs *)
652                                 (sp + STACK_FRAME_OVERHEAD);
653                         printk("--- Exception: %lx", regs->trap);
654                         print_symbol(" at %s\n", regs->nip);
655                         lr = regs->link;
656                         print_symbol("    LR = %s\n", lr);
657                         firstframe = 1;
658                 }
659
660                 sp = newsp;
661         } while (count++ < kstack_depth_to_print);
662 }
663
664 void dump_stack(void)
665 {
666         show_stack(current, (unsigned long *)__get_SP());
667 }
668 EXPORT_SYMBOL(dump_stack);