powerpc: Update for VSX core file and ptrace
[linux-2.6-microblaze.git] / arch / powerpc / kernel / process.c
1 /*
2  *  Derived from "arch/i386/kernel/process.c"
3  *    Copyright (C) 1995  Linus Torvalds
4  *
5  *  Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
6  *  Paul Mackerras (paulus@cs.anu.edu.au)
7  *
8  *  PowerPC version
9  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
10  *
11  *  This program is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU General Public License
13  *  as published by the Free Software Foundation; either version
14  *  2 of the License, or (at your option) any later version.
15  */
16
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/smp.h>
22 #include <linux/stddef.h>
23 #include <linux/unistd.h>
24 #include <linux/ptrace.h>
25 #include <linux/slab.h>
26 #include <linux/user.h>
27 #include <linux/elf.h>
28 #include <linux/init.h>
29 #include <linux/prctl.h>
30 #include <linux/init_task.h>
31 #include <linux/module.h>
32 #include <linux/kallsyms.h>
33 #include <linux/mqueue.h>
34 #include <linux/hardirq.h>
35 #include <linux/utsname.h>
36
37 #include <asm/pgtable.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/processor.h>
42 #include <asm/mmu.h>
43 #include <asm/prom.h>
44 #include <asm/machdep.h>
45 #include <asm/time.h>
46 #include <asm/syscalls.h>
47 #ifdef CONFIG_PPC64
48 #include <asm/firmware.h>
49 #endif
50
51 extern unsigned long _get_SP(void);
52
53 #ifndef CONFIG_SMP
54 struct task_struct *last_task_used_math = NULL;
55 struct task_struct *last_task_used_altivec = NULL;
56 struct task_struct *last_task_used_vsx = NULL;
57 struct task_struct *last_task_used_spe = NULL;
58 #endif
59
60 /*
61  * Make sure the floating-point register state in the
62  * the thread_struct is up to date for task tsk.
63  */
64 void flush_fp_to_thread(struct task_struct *tsk)
65 {
66         if (tsk->thread.regs) {
67                 /*
68                  * We need to disable preemption here because if we didn't,
69                  * another process could get scheduled after the regs->msr
70                  * test but before we have finished saving the FP registers
71                  * to the thread_struct.  That process could take over the
72                  * FPU, and then when we get scheduled again we would store
73                  * bogus values for the remaining FP registers.
74                  */
75                 preempt_disable();
76                 if (tsk->thread.regs->msr & MSR_FP) {
77 #ifdef CONFIG_SMP
78                         /*
79                          * This should only ever be called for current or
80                          * for a stopped child process.  Since we save away
81                          * the FP register state on context switch on SMP,
82                          * there is something wrong if a stopped child appears
83                          * to still have its FP state in the CPU registers.
84                          */
85                         BUG_ON(tsk != current);
86 #endif
87                         giveup_fpu(tsk);
88                 }
89                 preempt_enable();
90         }
91 }
92
93 void enable_kernel_fp(void)
94 {
95         WARN_ON(preemptible());
96
97 #ifdef CONFIG_SMP
98         if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
99                 giveup_fpu(current);
100         else
101                 giveup_fpu(NULL);       /* just enables FP for kernel */
102 #else
103         giveup_fpu(last_task_used_math);
104 #endif /* CONFIG_SMP */
105 }
106 EXPORT_SYMBOL(enable_kernel_fp);
107
108 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
109 {
110 #ifdef CONFIG_VSX
111         int i;
112         elf_fpreg_t *reg;
113 #endif
114
115         if (!tsk->thread.regs)
116                 return 0;
117         flush_fp_to_thread(current);
118
119 #ifdef CONFIG_VSX
120         reg = (elf_fpreg_t *)fpregs;
121         for (i = 0; i < ELF_NFPREG - 1; i++, reg++)
122                 *reg = tsk->thread.TS_FPR(i);
123         memcpy(reg, &tsk->thread.fpscr, sizeof(elf_fpreg_t));
124 #else
125         memcpy(fpregs, &tsk->thread.TS_FPR(0), sizeof(*fpregs));
126 #endif
127
128         return 1;
129 }
130
131 #ifdef CONFIG_ALTIVEC
132 void enable_kernel_altivec(void)
133 {
134         WARN_ON(preemptible());
135
136 #ifdef CONFIG_SMP
137         if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
138                 giveup_altivec(current);
139         else
140                 giveup_altivec(NULL);   /* just enable AltiVec for kernel - force */
141 #else
142         giveup_altivec(last_task_used_altivec);
143 #endif /* CONFIG_SMP */
144 }
145 EXPORT_SYMBOL(enable_kernel_altivec);
146
147 /*
148  * Make sure the VMX/Altivec register state in the
149  * the thread_struct is up to date for task tsk.
150  */
151 void flush_altivec_to_thread(struct task_struct *tsk)
152 {
153         if (tsk->thread.regs) {
154                 preempt_disable();
155                 if (tsk->thread.regs->msr & MSR_VEC) {
156 #ifdef CONFIG_SMP
157                         BUG_ON(tsk != current);
158 #endif
159                         giveup_altivec(tsk);
160                 }
161                 preempt_enable();
162         }
163 }
164
165 int dump_task_altivec(struct task_struct *tsk, elf_vrregset_t *vrregs)
166 {
167         /* ELF_NVRREG includes the VSCR and VRSAVE which we need to save
168          * separately, see below */
169         const int nregs = ELF_NVRREG - 2;
170         elf_vrreg_t *reg;
171         u32 *dest;
172
173         if (tsk == current)
174                 flush_altivec_to_thread(tsk);
175
176         reg = (elf_vrreg_t *)vrregs;
177
178         /* copy the 32 vr registers */
179         memcpy(reg, &tsk->thread.vr[0], nregs * sizeof(*reg));
180         reg += nregs;
181
182         /* copy the vscr */
183         memcpy(reg, &tsk->thread.vscr, sizeof(*reg));
184         reg++;
185
186         /* vrsave is stored in the high 32bit slot of the final 128bits */
187         memset(reg, 0, sizeof(*reg));
188         dest = (u32 *)reg;
189         *dest = tsk->thread.vrsave;
190
191         return 1;
192 }
193 #endif /* CONFIG_ALTIVEC */
194
195 #ifdef CONFIG_VSX
196 #if 0
197 /* not currently used, but some crazy RAID module might want to later */
198 void enable_kernel_vsx(void)
199 {
200         WARN_ON(preemptible());
201
202 #ifdef CONFIG_SMP
203         if (current->thread.regs && (current->thread.regs->msr & MSR_VSX))
204                 giveup_vsx(current);
205         else
206                 giveup_vsx(NULL);       /* just enable vsx for kernel - force */
207 #else
208         giveup_vsx(last_task_used_vsx);
209 #endif /* CONFIG_SMP */
210 }
211 EXPORT_SYMBOL(enable_kernel_vsx);
212 #endif
213
214 void flush_vsx_to_thread(struct task_struct *tsk)
215 {
216         if (tsk->thread.regs) {
217                 preempt_disable();
218                 if (tsk->thread.regs->msr & MSR_VSX) {
219 #ifdef CONFIG_SMP
220                         BUG_ON(tsk != current);
221 #endif
222                         giveup_vsx(tsk);
223                 }
224                 preempt_enable();
225         }
226 }
227
228 /*
229  * This dumps the lower half 64bits of the first 32 VSX registers.
230  * This needs to be called with dump_task_fp and dump_task_altivec to
231  * get all the VSX state.
232  */
233 int dump_task_vsx(struct task_struct *tsk, elf_vrreg_t *vrregs)
234 {
235         elf_vrreg_t *reg;
236         double buf[32];
237         int i;
238
239         if (tsk == current)
240                 flush_vsx_to_thread(tsk);
241
242         reg = (elf_vrreg_t *)vrregs;
243
244         for (i = 0; i < 32 ; i++)
245                 buf[i] = current->thread.fpr[i][TS_VSRLOWOFFSET];
246         memcpy(reg, buf, sizeof(buf));
247
248         return 1;
249 }
250 #endif /* CONFIG_VSX */
251
252 #ifdef CONFIG_SPE
253
254 void enable_kernel_spe(void)
255 {
256         WARN_ON(preemptible());
257
258 #ifdef CONFIG_SMP
259         if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
260                 giveup_spe(current);
261         else
262                 giveup_spe(NULL);       /* just enable SPE for kernel - force */
263 #else
264         giveup_spe(last_task_used_spe);
265 #endif /* __SMP __ */
266 }
267 EXPORT_SYMBOL(enable_kernel_spe);
268
269 void flush_spe_to_thread(struct task_struct *tsk)
270 {
271         if (tsk->thread.regs) {
272                 preempt_disable();
273                 if (tsk->thread.regs->msr & MSR_SPE) {
274 #ifdef CONFIG_SMP
275                         BUG_ON(tsk != current);
276 #endif
277                         giveup_spe(tsk);
278                 }
279                 preempt_enable();
280         }
281 }
282
283 int dump_spe(struct pt_regs *regs, elf_vrregset_t *evrregs)
284 {
285         flush_spe_to_thread(current);
286         /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
287         memcpy(evrregs, &current->thread.evr[0], sizeof(u32) * 35);
288         return 1;
289 }
290 #endif /* CONFIG_SPE */
291
292 #ifndef CONFIG_SMP
293 /*
294  * If we are doing lazy switching of CPU state (FP, altivec or SPE),
295  * and the current task has some state, discard it.
296  */
297 void discard_lazy_cpu_state(void)
298 {
299         preempt_disable();
300         if (last_task_used_math == current)
301                 last_task_used_math = NULL;
302 #ifdef CONFIG_ALTIVEC
303         if (last_task_used_altivec == current)
304                 last_task_used_altivec = NULL;
305 #endif /* CONFIG_ALTIVEC */
306 #ifdef CONFIG_VSX
307         if (last_task_used_vsx == current)
308                 last_task_used_vsx = NULL;
309 #endif /* CONFIG_VSX */
310 #ifdef CONFIG_SPE
311         if (last_task_used_spe == current)
312                 last_task_used_spe = NULL;
313 #endif
314         preempt_enable();
315 }
316 #endif /* CONFIG_SMP */
317
318 static DEFINE_PER_CPU(unsigned long, current_dabr);
319
320 int set_dabr(unsigned long dabr)
321 {
322         __get_cpu_var(current_dabr) = dabr;
323
324 #ifdef CONFIG_PPC_MERGE         /* XXX for now */
325         if (ppc_md.set_dabr)
326                 return ppc_md.set_dabr(dabr);
327 #endif
328
329         /* XXX should we have a CPU_FTR_HAS_DABR ? */
330 #if defined(CONFIG_PPC64) || defined(CONFIG_6xx)
331         mtspr(SPRN_DABR, dabr);
332 #endif
333         return 0;
334 }
335
336 #ifdef CONFIG_PPC64
337 DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
338 #endif
339
340 struct task_struct *__switch_to(struct task_struct *prev,
341         struct task_struct *new)
342 {
343         struct thread_struct *new_thread, *old_thread;
344         unsigned long flags;
345         struct task_struct *last;
346
347 #ifdef CONFIG_SMP
348         /* avoid complexity of lazy save/restore of fpu
349          * by just saving it every time we switch out if
350          * this task used the fpu during the last quantum.
351          *
352          * If it tries to use the fpu again, it'll trap and
353          * reload its fp regs.  So we don't have to do a restore
354          * every switch, just a save.
355          *  -- Cort
356          */
357         if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
358                 giveup_fpu(prev);
359 #ifdef CONFIG_ALTIVEC
360         /*
361          * If the previous thread used altivec in the last quantum
362          * (thus changing altivec regs) then save them.
363          * We used to check the VRSAVE register but not all apps
364          * set it, so we don't rely on it now (and in fact we need
365          * to save & restore VSCR even if VRSAVE == 0).  -- paulus
366          *
367          * On SMP we always save/restore altivec regs just to avoid the
368          * complexity of changing processors.
369          *  -- Cort
370          */
371         if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
372                 giveup_altivec(prev);
373 #endif /* CONFIG_ALTIVEC */
374 #ifdef CONFIG_VSX
375         if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
376                 giveup_vsx(prev);
377 #endif /* CONFIG_VSX */
378 #ifdef CONFIG_SPE
379         /*
380          * If the previous thread used spe in the last quantum
381          * (thus changing spe regs) then save them.
382          *
383          * On SMP we always save/restore spe regs just to avoid the
384          * complexity of changing processors.
385          */
386         if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
387                 giveup_spe(prev);
388 #endif /* CONFIG_SPE */
389
390 #else  /* CONFIG_SMP */
391 #ifdef CONFIG_ALTIVEC
392         /* Avoid the trap.  On smp this this never happens since
393          * we don't set last_task_used_altivec -- Cort
394          */
395         if (new->thread.regs && last_task_used_altivec == new)
396                 new->thread.regs->msr |= MSR_VEC;
397 #endif /* CONFIG_ALTIVEC */
398 #ifdef CONFIG_VSX
399         if (new->thread.regs && last_task_used_vsx == new)
400                 new->thread.regs->msr |= MSR_VSX;
401 #endif /* CONFIG_VSX */
402 #ifdef CONFIG_SPE
403         /* Avoid the trap.  On smp this this never happens since
404          * we don't set last_task_used_spe
405          */
406         if (new->thread.regs && last_task_used_spe == new)
407                 new->thread.regs->msr |= MSR_SPE;
408 #endif /* CONFIG_SPE */
409
410 #endif /* CONFIG_SMP */
411
412         if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr))
413                 set_dabr(new->thread.dabr);
414
415         new_thread = &new->thread;
416         old_thread = &current->thread;
417
418 #ifdef CONFIG_PPC64
419         /*
420          * Collect processor utilization data per process
421          */
422         if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
423                 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
424                 long unsigned start_tb, current_tb;
425                 start_tb = old_thread->start_tb;
426                 cu->current_tb = current_tb = mfspr(SPRN_PURR);
427                 old_thread->accum_tb += (current_tb - start_tb);
428                 new_thread->start_tb = current_tb;
429         }
430 #endif
431
432         local_irq_save(flags);
433
434         account_system_vtime(current);
435         account_process_vtime(current);
436         calculate_steal_time();
437
438         /*
439          * We can't take a PMU exception inside _switch() since there is a
440          * window where the kernel stack SLB and the kernel stack are out
441          * of sync. Hard disable here.
442          */
443         hard_irq_disable();
444         last = _switch(old_thread, new_thread);
445
446         local_irq_restore(flags);
447
448         return last;
449 }
450
451 static int instructions_to_print = 16;
452
453 static void show_instructions(struct pt_regs *regs)
454 {
455         int i;
456         unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
457                         sizeof(int));
458
459         printk("Instruction dump:");
460
461         for (i = 0; i < instructions_to_print; i++) {
462                 int instr;
463
464                 if (!(i % 8))
465                         printk("\n");
466
467 #if !defined(CONFIG_BOOKE)
468                 /* If executing with the IMMU off, adjust pc rather
469                  * than print XXXXXXXX.
470                  */
471                 if (!(regs->msr & MSR_IR))
472                         pc = (unsigned long)phys_to_virt(pc);
473 #endif
474
475                 /* We use __get_user here *only* to avoid an OOPS on a
476                  * bad address because the pc *should* only be a
477                  * kernel address.
478                  */
479                 if (!__kernel_text_address(pc) ||
480                      __get_user(instr, (unsigned int __user *)pc)) {
481                         printk("XXXXXXXX ");
482                 } else {
483                         if (regs->nip == pc)
484                                 printk("<%08x> ", instr);
485                         else
486                                 printk("%08x ", instr);
487                 }
488
489                 pc += sizeof(int);
490         }
491
492         printk("\n");
493 }
494
495 static struct regbit {
496         unsigned long bit;
497         const char *name;
498 } msr_bits[] = {
499         {MSR_EE,        "EE"},
500         {MSR_PR,        "PR"},
501         {MSR_FP,        "FP"},
502         {MSR_VEC,       "VEC"},
503         {MSR_VSX,       "VSX"},
504         {MSR_ME,        "ME"},
505         {MSR_IR,        "IR"},
506         {MSR_DR,        "DR"},
507         {0,             NULL}
508 };
509
510 static void printbits(unsigned long val, struct regbit *bits)
511 {
512         const char *sep = "";
513
514         printk("<");
515         for (; bits->bit; ++bits)
516                 if (val & bits->bit) {
517                         printk("%s%s", sep, bits->name);
518                         sep = ",";
519                 }
520         printk(">");
521 }
522
523 #ifdef CONFIG_PPC64
524 #define REG             "%016lx"
525 #define REGS_PER_LINE   4
526 #define LAST_VOLATILE   13
527 #else
528 #define REG             "%08lx"
529 #define REGS_PER_LINE   8
530 #define LAST_VOLATILE   12
531 #endif
532
533 void show_regs(struct pt_regs * regs)
534 {
535         int i, trap;
536
537         printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
538                regs->nip, regs->link, regs->ctr);
539         printk("REGS: %p TRAP: %04lx   %s  (%s)\n",
540                regs, regs->trap, print_tainted(), init_utsname()->release);
541         printk("MSR: "REG" ", regs->msr);
542         printbits(regs->msr, msr_bits);
543         printk("  CR: %08lx  XER: %08lx\n", regs->ccr, regs->xer);
544         trap = TRAP(regs);
545         if (trap == 0x300 || trap == 0x600)
546 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
547                 printk("DEAR: "REG", ESR: "REG"\n", regs->dar, regs->dsisr);
548 #else
549                 printk("DAR: "REG", DSISR: "REG"\n", regs->dar, regs->dsisr);
550 #endif
551         printk("TASK = %p[%d] '%s' THREAD: %p",
552                current, task_pid_nr(current), current->comm, task_thread_info(current));
553
554 #ifdef CONFIG_SMP
555         printk(" CPU: %d", raw_smp_processor_id());
556 #endif /* CONFIG_SMP */
557
558         for (i = 0;  i < 32;  i++) {
559                 if ((i % REGS_PER_LINE) == 0)
560                         printk("\n" KERN_INFO "GPR%02d: ", i);
561                 printk(REG " ", regs->gpr[i]);
562                 if (i == LAST_VOLATILE && !FULL_REGS(regs))
563                         break;
564         }
565         printk("\n");
566 #ifdef CONFIG_KALLSYMS
567         /*
568          * Lookup NIP late so we have the best change of getting the
569          * above info out without failing
570          */
571         printk("NIP ["REG"] ", regs->nip);
572         print_symbol("%s\n", regs->nip);
573         printk("LR ["REG"] ", regs->link);
574         print_symbol("%s\n", regs->link);
575 #endif
576         show_stack(current, (unsigned long *) regs->gpr[1]);
577         if (!user_mode(regs))
578                 show_instructions(regs);
579 }
580
581 void exit_thread(void)
582 {
583         discard_lazy_cpu_state();
584 }
585
586 void flush_thread(void)
587 {
588 #ifdef CONFIG_PPC64
589         struct thread_info *t = current_thread_info();
590
591         if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
592                 clear_ti_thread_flag(t, TIF_ABI_PENDING);
593                 if (test_ti_thread_flag(t, TIF_32BIT))
594                         clear_ti_thread_flag(t, TIF_32BIT);
595                 else
596                         set_ti_thread_flag(t, TIF_32BIT);
597         }
598 #endif
599
600         discard_lazy_cpu_state();
601
602         if (current->thread.dabr) {
603                 current->thread.dabr = 0;
604                 set_dabr(0);
605         }
606 }
607
608 void
609 release_thread(struct task_struct *t)
610 {
611 }
612
613 /*
614  * This gets called before we allocate a new thread and copy
615  * the current task into it.
616  */
617 void prepare_to_copy(struct task_struct *tsk)
618 {
619         flush_fp_to_thread(current);
620         flush_altivec_to_thread(current);
621         flush_vsx_to_thread(current);
622         flush_spe_to_thread(current);
623 }
624
625 /*
626  * Copy a thread..
627  */
628 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
629                 unsigned long unused, struct task_struct *p,
630                 struct pt_regs *regs)
631 {
632         struct pt_regs *childregs, *kregs;
633         extern void ret_from_fork(void);
634         unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
635
636         CHECK_FULL_REGS(regs);
637         /* Copy registers */
638         sp -= sizeof(struct pt_regs);
639         childregs = (struct pt_regs *) sp;
640         *childregs = *regs;
641         if ((childregs->msr & MSR_PR) == 0) {
642                 /* for kernel thread, set `current' and stackptr in new task */
643                 childregs->gpr[1] = sp + sizeof(struct pt_regs);
644 #ifdef CONFIG_PPC32
645                 childregs->gpr[2] = (unsigned long) p;
646 #else
647                 clear_tsk_thread_flag(p, TIF_32BIT);
648 #endif
649                 p->thread.regs = NULL;  /* no user register state */
650         } else {
651                 childregs->gpr[1] = usp;
652                 p->thread.regs = childregs;
653                 if (clone_flags & CLONE_SETTLS) {
654 #ifdef CONFIG_PPC64
655                         if (!test_thread_flag(TIF_32BIT))
656                                 childregs->gpr[13] = childregs->gpr[6];
657                         else
658 #endif
659                                 childregs->gpr[2] = childregs->gpr[6];
660                 }
661         }
662         childregs->gpr[3] = 0;  /* Result from fork() */
663         sp -= STACK_FRAME_OVERHEAD;
664
665         /*
666          * The way this works is that at some point in the future
667          * some task will call _switch to switch to the new task.
668          * That will pop off the stack frame created below and start
669          * the new task running at ret_from_fork.  The new task will
670          * do some house keeping and then return from the fork or clone
671          * system call, using the stack frame created above.
672          */
673         sp -= sizeof(struct pt_regs);
674         kregs = (struct pt_regs *) sp;
675         sp -= STACK_FRAME_OVERHEAD;
676         p->thread.ksp = sp;
677         p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
678                                 _ALIGN_UP(sizeof(struct thread_info), 16);
679
680 #ifdef CONFIG_PPC64
681         if (cpu_has_feature(CPU_FTR_SLB)) {
682                 unsigned long sp_vsid;
683                 unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
684
685                 if (cpu_has_feature(CPU_FTR_1T_SEGMENT))
686                         sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
687                                 << SLB_VSID_SHIFT_1T;
688                 else
689                         sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
690                                 << SLB_VSID_SHIFT;
691                 sp_vsid |= SLB_VSID_KERNEL | llp;
692                 p->thread.ksp_vsid = sp_vsid;
693         }
694
695         /*
696          * The PPC64 ABI makes use of a TOC to contain function 
697          * pointers.  The function (ret_from_except) is actually a pointer
698          * to the TOC entry.  The first entry is a pointer to the actual
699          * function.
700          */
701         kregs->nip = *((unsigned long *)ret_from_fork);
702 #else
703         kregs->nip = (unsigned long)ret_from_fork;
704 #endif
705
706         return 0;
707 }
708
709 /*
710  * Set up a thread for executing a new program
711  */
712 void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
713 {
714 #ifdef CONFIG_PPC64
715         unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
716 #endif
717
718         set_fs(USER_DS);
719
720         /*
721          * If we exec out of a kernel thread then thread.regs will not be
722          * set.  Do it now.
723          */
724         if (!current->thread.regs) {
725                 struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
726                 current->thread.regs = regs - 1;
727         }
728
729         memset(regs->gpr, 0, sizeof(regs->gpr));
730         regs->ctr = 0;
731         regs->link = 0;
732         regs->xer = 0;
733         regs->ccr = 0;
734         regs->gpr[1] = sp;
735
736         /*
737          * We have just cleared all the nonvolatile GPRs, so make
738          * FULL_REGS(regs) return true.  This is necessary to allow
739          * ptrace to examine the thread immediately after exec.
740          */
741         regs->trap &= ~1UL;
742
743 #ifdef CONFIG_PPC32
744         regs->mq = 0;
745         regs->nip = start;
746         regs->msr = MSR_USER;
747 #else
748         if (!test_thread_flag(TIF_32BIT)) {
749                 unsigned long entry, toc;
750
751                 /* start is a relocated pointer to the function descriptor for
752                  * the elf _start routine.  The first entry in the function
753                  * descriptor is the entry address of _start and the second
754                  * entry is the TOC value we need to use.
755                  */
756                 __get_user(entry, (unsigned long __user *)start);
757                 __get_user(toc, (unsigned long __user *)start+1);
758
759                 /* Check whether the e_entry function descriptor entries
760                  * need to be relocated before we can use them.
761                  */
762                 if (load_addr != 0) {
763                         entry += load_addr;
764                         toc   += load_addr;
765                 }
766                 regs->nip = entry;
767                 regs->gpr[2] = toc;
768                 regs->msr = MSR_USER64;
769         } else {
770                 regs->nip = start;
771                 regs->gpr[2] = 0;
772                 regs->msr = MSR_USER32;
773         }
774 #endif
775
776         discard_lazy_cpu_state();
777 #ifdef CONFIG_VSX
778         current->thread.used_vsr = 0;
779 #endif
780         memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
781         current->thread.fpscr.val = 0;
782 #ifdef CONFIG_ALTIVEC
783         memset(current->thread.vr, 0, sizeof(current->thread.vr));
784         memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
785         current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
786         current->thread.vrsave = 0;
787         current->thread.used_vr = 0;
788 #endif /* CONFIG_ALTIVEC */
789 #ifdef CONFIG_SPE
790         memset(current->thread.evr, 0, sizeof(current->thread.evr));
791         current->thread.acc = 0;
792         current->thread.spefscr = 0;
793         current->thread.used_spe = 0;
794 #endif /* CONFIG_SPE */
795 }
796
797 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
798                 | PR_FP_EXC_RES | PR_FP_EXC_INV)
799
800 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
801 {
802         struct pt_regs *regs = tsk->thread.regs;
803
804         /* This is a bit hairy.  If we are an SPE enabled  processor
805          * (have embedded fp) we store the IEEE exception enable flags in
806          * fpexc_mode.  fpexc_mode is also used for setting FP exception
807          * mode (asyn, precise, disabled) for 'Classic' FP. */
808         if (val & PR_FP_EXC_SW_ENABLE) {
809 #ifdef CONFIG_SPE
810                 if (cpu_has_feature(CPU_FTR_SPE)) {
811                         tsk->thread.fpexc_mode = val &
812                                 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
813                         return 0;
814                 } else {
815                         return -EINVAL;
816                 }
817 #else
818                 return -EINVAL;
819 #endif
820         }
821
822         /* on a CONFIG_SPE this does not hurt us.  The bits that
823          * __pack_fe01 use do not overlap with bits used for
824          * PR_FP_EXC_SW_ENABLE.  Additionally, the MSR[FE0,FE1] bits
825          * on CONFIG_SPE implementations are reserved so writing to
826          * them does not change anything */
827         if (val > PR_FP_EXC_PRECISE)
828                 return -EINVAL;
829         tsk->thread.fpexc_mode = __pack_fe01(val);
830         if (regs != NULL && (regs->msr & MSR_FP) != 0)
831                 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
832                         | tsk->thread.fpexc_mode;
833         return 0;
834 }
835
836 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
837 {
838         unsigned int val;
839
840         if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
841 #ifdef CONFIG_SPE
842                 if (cpu_has_feature(CPU_FTR_SPE))
843                         val = tsk->thread.fpexc_mode;
844                 else
845                         return -EINVAL;
846 #else
847                 return -EINVAL;
848 #endif
849         else
850                 val = __unpack_fe01(tsk->thread.fpexc_mode);
851         return put_user(val, (unsigned int __user *) adr);
852 }
853
854 int set_endian(struct task_struct *tsk, unsigned int val)
855 {
856         struct pt_regs *regs = tsk->thread.regs;
857
858         if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
859             (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
860                 return -EINVAL;
861
862         if (regs == NULL)
863                 return -EINVAL;
864
865         if (val == PR_ENDIAN_BIG)
866                 regs->msr &= ~MSR_LE;
867         else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
868                 regs->msr |= MSR_LE;
869         else
870                 return -EINVAL;
871
872         return 0;
873 }
874
875 int get_endian(struct task_struct *tsk, unsigned long adr)
876 {
877         struct pt_regs *regs = tsk->thread.regs;
878         unsigned int val;
879
880         if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
881             !cpu_has_feature(CPU_FTR_REAL_LE))
882                 return -EINVAL;
883
884         if (regs == NULL)
885                 return -EINVAL;
886
887         if (regs->msr & MSR_LE) {
888                 if (cpu_has_feature(CPU_FTR_REAL_LE))
889                         val = PR_ENDIAN_LITTLE;
890                 else
891                         val = PR_ENDIAN_PPC_LITTLE;
892         } else
893                 val = PR_ENDIAN_BIG;
894
895         return put_user(val, (unsigned int __user *)adr);
896 }
897
898 int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
899 {
900         tsk->thread.align_ctl = val;
901         return 0;
902 }
903
904 int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
905 {
906         return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
907 }
908
909 #define TRUNC_PTR(x)    ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
910
911 int sys_clone(unsigned long clone_flags, unsigned long usp,
912               int __user *parent_tidp, void __user *child_threadptr,
913               int __user *child_tidp, int p6,
914               struct pt_regs *regs)
915 {
916         CHECK_FULL_REGS(regs);
917         if (usp == 0)
918                 usp = regs->gpr[1];     /* stack pointer for child */
919 #ifdef CONFIG_PPC64
920         if (test_thread_flag(TIF_32BIT)) {
921                 parent_tidp = TRUNC_PTR(parent_tidp);
922                 child_tidp = TRUNC_PTR(child_tidp);
923         }
924 #endif
925         return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
926 }
927
928 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
929              unsigned long p4, unsigned long p5, unsigned long p6,
930              struct pt_regs *regs)
931 {
932         CHECK_FULL_REGS(regs);
933         return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
934 }
935
936 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
937               unsigned long p4, unsigned long p5, unsigned long p6,
938               struct pt_regs *regs)
939 {
940         CHECK_FULL_REGS(regs);
941         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
942                         regs, 0, NULL, NULL);
943 }
944
945 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
946                unsigned long a3, unsigned long a4, unsigned long a5,
947                struct pt_regs *regs)
948 {
949         int error;
950         char *filename;
951
952         filename = getname((char __user *) a0);
953         error = PTR_ERR(filename);
954         if (IS_ERR(filename))
955                 goto out;
956         flush_fp_to_thread(current);
957         flush_altivec_to_thread(current);
958         flush_spe_to_thread(current);
959         error = do_execve(filename, (char __user * __user *) a1,
960                           (char __user * __user *) a2, regs);
961         putname(filename);
962 out:
963         return error;
964 }
965
966 #ifdef CONFIG_IRQSTACKS
967 static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
968                                   unsigned long nbytes)
969 {
970         unsigned long stack_page;
971         unsigned long cpu = task_cpu(p);
972
973         /*
974          * Avoid crashing if the stack has overflowed and corrupted
975          * task_cpu(p), which is in the thread_info struct.
976          */
977         if (cpu < NR_CPUS && cpu_possible(cpu)) {
978                 stack_page = (unsigned long) hardirq_ctx[cpu];
979                 if (sp >= stack_page + sizeof(struct thread_struct)
980                     && sp <= stack_page + THREAD_SIZE - nbytes)
981                         return 1;
982
983                 stack_page = (unsigned long) softirq_ctx[cpu];
984                 if (sp >= stack_page + sizeof(struct thread_struct)
985                     && sp <= stack_page + THREAD_SIZE - nbytes)
986                         return 1;
987         }
988         return 0;
989 }
990
991 #else
992 #define valid_irq_stack(sp, p, nb)      0
993 #endif /* CONFIG_IRQSTACKS */
994
995 int validate_sp(unsigned long sp, struct task_struct *p,
996                        unsigned long nbytes)
997 {
998         unsigned long stack_page = (unsigned long)task_stack_page(p);
999
1000         if (sp >= stack_page + sizeof(struct thread_struct)
1001             && sp <= stack_page + THREAD_SIZE - nbytes)
1002                 return 1;
1003
1004         return valid_irq_stack(sp, p, nbytes);
1005 }
1006
1007 EXPORT_SYMBOL(validate_sp);
1008
1009 unsigned long get_wchan(struct task_struct *p)
1010 {
1011         unsigned long ip, sp;
1012         int count = 0;
1013
1014         if (!p || p == current || p->state == TASK_RUNNING)
1015                 return 0;
1016
1017         sp = p->thread.ksp;
1018         if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
1019                 return 0;
1020
1021         do {
1022                 sp = *(unsigned long *)sp;
1023                 if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
1024                         return 0;
1025                 if (count > 0) {
1026                         ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
1027                         if (!in_sched_functions(ip))
1028                                 return ip;
1029                 }
1030         } while (count++ < 16);
1031         return 0;
1032 }
1033
1034 static int kstack_depth_to_print = 64;
1035
1036 void show_stack(struct task_struct *tsk, unsigned long *stack)
1037 {
1038         unsigned long sp, ip, lr, newsp;
1039         int count = 0;
1040         int firstframe = 1;
1041
1042         sp = (unsigned long) stack;
1043         if (tsk == NULL)
1044                 tsk = current;
1045         if (sp == 0) {
1046                 if (tsk == current)
1047                         asm("mr %0,1" : "=r" (sp));
1048                 else
1049                         sp = tsk->thread.ksp;
1050         }
1051
1052         lr = 0;
1053         printk("Call Trace:\n");
1054         do {
1055                 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
1056                         return;
1057
1058                 stack = (unsigned long *) sp;
1059                 newsp = stack[0];
1060                 ip = stack[STACK_FRAME_LR_SAVE];
1061                 if (!firstframe || ip != lr) {
1062                         printk("["REG"] ["REG"] ", sp, ip);
1063                         print_symbol("%s", ip);
1064                         if (firstframe)
1065                                 printk(" (unreliable)");
1066                         printk("\n");
1067                 }
1068                 firstframe = 0;
1069
1070                 /*
1071                  * See if this is an exception frame.
1072                  * We look for the "regshere" marker in the current frame.
1073                  */
1074                 if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
1075                     && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
1076                         struct pt_regs *regs = (struct pt_regs *)
1077                                 (sp + STACK_FRAME_OVERHEAD);
1078                         printk("--- Exception: %lx", regs->trap);
1079                         print_symbol(" at %s\n", regs->nip);
1080                         lr = regs->link;
1081                         print_symbol("    LR = %s\n", lr);
1082                         firstframe = 1;
1083                 }
1084
1085                 sp = newsp;
1086         } while (count++ < kstack_depth_to_print);
1087 }
1088
1089 void dump_stack(void)
1090 {
1091         show_stack(current, NULL);
1092 }
1093 EXPORT_SYMBOL(dump_stack);
1094
1095 #ifdef CONFIG_PPC64
1096 void ppc64_runlatch_on(void)
1097 {
1098         unsigned long ctrl;
1099
1100         if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
1101                 HMT_medium();
1102
1103                 ctrl = mfspr(SPRN_CTRLF);
1104                 ctrl |= CTRL_RUNLATCH;
1105                 mtspr(SPRN_CTRLT, ctrl);
1106
1107                 set_thread_flag(TIF_RUNLATCH);
1108         }
1109 }
1110
1111 void ppc64_runlatch_off(void)
1112 {
1113         unsigned long ctrl;
1114
1115         if (cpu_has_feature(CPU_FTR_CTRL) && test_thread_flag(TIF_RUNLATCH)) {
1116                 HMT_medium();
1117
1118                 clear_thread_flag(TIF_RUNLATCH);
1119
1120                 ctrl = mfspr(SPRN_CTRLF);
1121                 ctrl &= ~CTRL_RUNLATCH;
1122                 mtspr(SPRN_CTRLT, ctrl);
1123         }
1124 }
1125 #endif
1126
1127 #if THREAD_SHIFT < PAGE_SHIFT
1128
1129 static struct kmem_cache *thread_info_cache;
1130
1131 struct thread_info *alloc_thread_info(struct task_struct *tsk)
1132 {
1133         struct thread_info *ti;
1134
1135         ti = kmem_cache_alloc(thread_info_cache, GFP_KERNEL);
1136         if (unlikely(ti == NULL))
1137                 return NULL;
1138 #ifdef CONFIG_DEBUG_STACK_USAGE
1139         memset(ti, 0, THREAD_SIZE);
1140 #endif
1141         return ti;
1142 }
1143
1144 void free_thread_info(struct thread_info *ti)
1145 {
1146         kmem_cache_free(thread_info_cache, ti);
1147 }
1148
1149 void thread_info_cache_init(void)
1150 {
1151         thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
1152                                               THREAD_SIZE, 0, NULL);
1153         BUG_ON(thread_info_cache == NULL);
1154 }
1155
1156 #endif /* THREAD_SHIFT < PAGE_SHIFT */