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