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