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