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