patch-2_6_7-vs1_9_1_12
[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 void enable_kernel_fp(void)
69 {
70 #ifdef CONFIG_SMP
71         if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
72                 giveup_fpu(current);
73         else
74                 giveup_fpu(NULL);       /* just enables FP for kernel */
75 #else
76         giveup_fpu(last_task_used_math);
77 #endif /* CONFIG_SMP */
78 }
79 EXPORT_SYMBOL(enable_kernel_fp);
80
81 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
82 {
83         struct pt_regs *regs = tsk->thread.regs;
84
85         if (!regs)
86                 return 0;
87         if (tsk == current && (regs->msr & MSR_FP))
88                 giveup_fpu(current);
89
90         memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
91
92         return 1;
93 }
94
95 #ifdef CONFIG_ALTIVEC
96
97 void enable_kernel_altivec(void)
98 {
99 #ifdef CONFIG_SMP
100         if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
101                 giveup_altivec(current);
102         else
103                 giveup_altivec(NULL);   /* just enables FP for kernel */
104 #else
105         giveup_altivec(last_task_used_altivec);
106 #endif /* CONFIG_SMP */
107 }
108 EXPORT_SYMBOL(enable_kernel_altivec);
109
110 int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
111 {
112         if (regs->msr & MSR_VEC)
113                 giveup_altivec(current);
114         memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
115         return 1;
116 }
117
118 #endif /* CONFIG_ALTIVEC */
119
120 struct task_struct *__switch_to(struct task_struct *prev,
121                                 struct task_struct *new)
122 {
123         struct thread_struct *new_thread, *old_thread;
124         unsigned long flags;
125         struct task_struct *last;
126
127 #ifdef CONFIG_SMP
128         /* avoid complexity of lazy save/restore of fpu
129          * by just saving it every time we switch out if
130          * this task used the fpu during the last quantum.
131          * 
132          * If it tries to use the fpu again, it'll trap and
133          * reload its fp regs.  So we don't have to do a restore
134          * every switch, just a save.
135          *  -- Cort
136          */
137         if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
138                 giveup_fpu(prev);
139 #ifdef CONFIG_ALTIVEC
140         if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
141                 giveup_altivec(prev);
142 #endif /* CONFIG_ALTIVEC */
143 #endif /* CONFIG_SMP */
144
145 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
146         /* Avoid the trap.  On smp this this never happens since
147          * we don't set last_task_used_altivec -- Cort
148          */
149         if (new->thread.regs && last_task_used_altivec == new)
150                 new->thread.regs->msr |= MSR_VEC;
151 #endif /* CONFIG_ALTIVEC */
152
153         flush_tlb_pending();
154
155         new_thread = &new->thread;
156         old_thread = &current->thread;
157
158         local_irq_save(flags);
159         last = _switch(old_thread, new_thread);
160
161         local_irq_restore(flags);
162
163         return last;
164 }
165
166 void show_regs(struct pt_regs * regs)
167 {
168         int i;
169
170         printk("NIP: %016lX XER: %016lX LR: %016lX\n",
171                regs->nip, regs->xer, regs->link);
172         printk("REGS: %p TRAP: %04lx   %s  (%s)\n",
173                regs, regs->trap, print_tainted(), UTS_RELEASE);
174         printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
175                regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
176                regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
177                regs->msr&MSR_IR ? 1 : 0,
178                regs->msr&MSR_DR ? 1 : 0);
179         if (regs->trap == 0x300 || regs->trap == 0x380 || regs->trap == 0x600)
180                 printk("DAR: %016lx, DSISR: %016lx\n", regs->dar, regs->dsisr);
181         printk("TASK: %p[%d] '%s' THREAD: %p",
182                current, current->pid, current->comm, current->thread_info);
183
184 #ifdef CONFIG_SMP
185         printk(" CPU: %d", smp_processor_id());
186 #endif /* CONFIG_SMP */
187
188         for (i = 0; i < 32; i++) {
189                 if ((i % 4) == 0) {
190                         printk("\n" KERN_INFO "GPR%02d: ", i);
191                 }
192
193                 printk("%016lX ", regs->gpr[i]);
194         }
195         printk("\n");
196         /*
197          * Lookup NIP late so we have the best change of getting the
198          * above info out without failing
199          */
200         printk("NIP [%016lx] ", regs->nip);
201         print_symbol("%s\n", regs->nip);
202         printk("LR [%016lx] ", regs->link);
203         print_symbol("%s\n", regs->link);
204         show_stack(current, (unsigned long *)regs->gpr[1]);
205 }
206
207 void exit_thread(void)
208 {
209 #ifndef CONFIG_SMP
210         if (last_task_used_math == current)
211                 last_task_used_math = NULL;
212 #ifdef CONFIG_ALTIVEC
213         if (last_task_used_altivec == current)
214                 last_task_used_altivec = NULL;
215 #endif /* CONFIG_ALTIVEC */
216 #endif /* CONFIG_SMP */
217 }
218
219 void flush_thread(void)
220 {
221         struct thread_info *t = current_thread_info();
222
223         if (t->flags & _TIF_ABI_PENDING)
224                 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
225
226 #ifndef CONFIG_SMP
227         if (last_task_used_math == current)
228                 last_task_used_math = NULL;
229 #ifdef CONFIG_ALTIVEC
230         if (last_task_used_altivec == current)
231                 last_task_used_altivec = NULL;
232 #endif /* CONFIG_ALTIVEC */
233 #endif /* CONFIG_SMP */
234 }
235
236 void
237 release_thread(struct task_struct *t)
238 {
239 }
240
241
242 /*
243  * This gets called before we allocate a new thread and copy
244  * the current task into it.
245  */
246 void prepare_to_copy(struct task_struct *tsk)
247 {
248         struct pt_regs *regs = tsk->thread.regs;
249
250         if (regs == NULL)
251                 return;
252         if (regs->msr & MSR_FP)
253                 giveup_fpu(current);
254 #ifdef CONFIG_ALTIVEC
255         if (regs->msr & MSR_VEC)
256                 giveup_altivec(current);
257 #endif /* CONFIG_ALTIVEC */
258 }
259
260 /*
261  * Copy a thread..
262  */
263 int
264 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
265             unsigned long unused, struct task_struct *p, struct pt_regs *regs)
266 {
267         struct pt_regs *childregs, *kregs;
268         extern void ret_from_fork(void);
269         unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
270
271         p->set_child_tid = p->clear_child_tid = NULL;
272
273         /* Copy registers */
274         sp -= sizeof(struct pt_regs);
275         childregs = (struct pt_regs *) sp;
276         *childregs = *regs;
277         if ((childregs->msr & MSR_PR) == 0) {
278                 /* for kernel thread, set stackptr in new task */
279                 childregs->gpr[1] = sp + sizeof(struct pt_regs);
280                 p->thread.regs = NULL;  /* no user register state */
281                 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
282 #ifdef CONFIG_PPC_ISERIES
283                 set_ti_thread_flag(p->thread_info, TIF_RUN_LIGHT);
284 #endif
285         } else {
286                 childregs->gpr[1] = usp;
287                 p->thread.regs = childregs;
288                 if (clone_flags & CLONE_SETTLS) {
289                         if (test_thread_flag(TIF_32BIT))
290                                 childregs->gpr[2] = childregs->gpr[6];
291                         else
292                                 childregs->gpr[13] = childregs->gpr[6];
293                 }
294         }
295         childregs->gpr[3] = 0;  /* Result from fork() */
296         sp -= STACK_FRAME_OVERHEAD;
297
298         /*
299          * The way this works is that at some point in the future
300          * some task will call _switch to switch to the new task.
301          * That will pop off the stack frame created below and start
302          * the new task running at ret_from_fork.  The new task will
303          * do some house keeping and then return from the fork or clone
304          * system call, using the stack frame created above.
305          */
306         sp -= sizeof(struct pt_regs);
307         kregs = (struct pt_regs *) sp;
308         sp -= STACK_FRAME_OVERHEAD;
309         p->thread.ksp = sp;
310
311         /*
312          * The PPC64 ABI makes use of a TOC to contain function 
313          * pointers.  The function (ret_from_except) is actually a pointer
314          * to the TOC entry.  The first entry is a pointer to the actual
315          * function.
316          */
317         kregs->nip = *((unsigned long *)ret_from_fork);
318
319         return 0;
320 }
321
322 /*
323  * Set up a thread for executing a new program
324  */
325 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
326 {
327         unsigned long entry, toc, load_addr = regs->gpr[2];
328
329         /* fdptr is a relocated pointer to the function descriptor for
330          * the elf _start routine.  The first entry in the function
331          * descriptor is the entry address of _start and the second
332          * entry is the TOC value we need to use.
333          */
334         set_fs(USER_DS);
335         __get_user(entry, (unsigned long __user *)fdptr);
336         __get_user(toc, (unsigned long __user *)fdptr+1);
337
338         /* Check whether the e_entry function descriptor entries
339          * need to be relocated before we can use them.
340          */
341         if ( load_addr != 0 ) {
342                 entry += load_addr;
343                 toc   += load_addr;
344         }
345
346         regs->nip = entry;
347         regs->gpr[1] = sp;
348         regs->gpr[2] = toc;
349         regs->msr = MSR_USER64;
350 #ifndef CONFIG_SMP
351         if (last_task_used_math == current)
352                 last_task_used_math = 0;
353 #endif /* CONFIG_SMP */
354         memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
355         current->thread.fpscr = 0;
356 #ifdef CONFIG_ALTIVEC
357 #ifndef CONFIG_SMP
358         if (last_task_used_altivec == current)
359                 last_task_used_altivec = 0;
360 #endif /* CONFIG_SMP */
361         memset(current->thread.vr, 0, sizeof(current->thread.vr));
362         current->thread.vscr.u[0] = 0;
363         current->thread.vscr.u[1] = 0;
364         current->thread.vscr.u[2] = 0;
365         current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
366         current->thread.vrsave = 0;
367         current->thread.used_vr = 0;
368 #endif /* CONFIG_ALTIVEC */
369 }
370
371 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
372 {
373         struct pt_regs *regs = tsk->thread.regs;
374
375         if (val > PR_FP_EXC_PRECISE)
376                 return -EINVAL;
377         tsk->thread.fpexc_mode = __pack_fe01(val);
378         if (regs != NULL && (regs->msr & MSR_FP) != 0)
379                 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
380                         | tsk->thread.fpexc_mode;
381         return 0;
382 }
383
384 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
385 {
386         unsigned int val;
387
388         val = __unpack_fe01(tsk->thread.fpexc_mode);
389         return put_user(val, (unsigned int __user *) adr);
390 }
391
392 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
393               unsigned long p4, unsigned long p5, unsigned long p6,
394               struct pt_regs *regs)
395 {
396         unsigned long parent_tidptr = 0;
397         unsigned long child_tidptr = 0;
398
399         if (p2 == 0)
400                 p2 = regs->gpr[1];      /* stack pointer for child */
401
402         if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
403                            CLONE_CHILD_CLEARTID)) {
404                 parent_tidptr = p3;
405                 child_tidptr = p5;
406                 if (test_thread_flag(TIF_32BIT)) {
407                         parent_tidptr &= 0xffffffff;
408                         child_tidptr &= 0xffffffff;
409                 }
410         }
411
412         return do_fork(clone_flags & ~CLONE_IDLETASK, p2, regs, 0,
413                     (int __user *)parent_tidptr, (int __user *)child_tidptr);
414 }
415
416 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
417              unsigned long p4, unsigned long p5, unsigned long p6,
418              struct pt_regs *regs)
419 {
420         return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
421 }
422
423 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
424               unsigned long p4, unsigned long p5, unsigned long p6,
425               struct pt_regs *regs)
426 {
427         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
428                     NULL, NULL);
429 }
430
431 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
432                unsigned long a3, unsigned long a4, unsigned long a5,
433                struct pt_regs *regs)
434 {
435         int error;
436         char * filename;
437         
438         filename = getname((char __user *) a0);
439         error = PTR_ERR(filename);
440         if (IS_ERR(filename))
441                 goto out;
442         if (regs->msr & MSR_FP)
443                 giveup_fpu(current);
444 #ifdef CONFIG_ALTIVEC
445         if (regs->msr & MSR_VEC)
446                 giveup_altivec(current);
447 #endif /* CONFIG_ALTIVEC */
448         error = do_execve(filename, (char __user * __user *) a1,
449                                     (char __user * __user *) a2, regs);
450   
451         if (error == 0)
452                 current->ptrace &= ~PT_DTRACE;
453         putname(filename);
454
455 out:
456         return error;
457 }
458
459 static int kstack_depth_to_print = 64;
460
461 static int validate_sp(unsigned long sp, struct task_struct *p,
462                        unsigned long nbytes)
463 {
464         unsigned long stack_page = (unsigned long)p->thread_info;
465
466         if (sp >= stack_page + sizeof(struct thread_struct)
467             && sp <= stack_page + THREAD_SIZE - nbytes)
468                 return 1;
469
470 #ifdef CONFIG_IRQSTACKS
471         stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
472         if (sp >= stack_page + sizeof(struct thread_struct)
473             && sp <= stack_page + THREAD_SIZE - nbytes)
474                 return 1;
475
476         stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
477         if (sp >= stack_page + sizeof(struct thread_struct)
478             && sp <= stack_page + THREAD_SIZE - nbytes)
479                 return 1;
480 #endif
481
482         return 0;
483 }
484
485 unsigned long get_wchan(struct task_struct *p)
486 {
487         unsigned long ip, sp;
488         int count = 0;
489
490         if (!p || p == current || p->state == TASK_RUNNING)
491                 return 0;
492
493         sp = p->thread.ksp;
494         if (!validate_sp(sp, p, 112))
495                 return 0;
496
497         do {
498                 sp = *(unsigned long *)sp;
499                 if (!validate_sp(sp, p, 112))
500                         return 0;
501                 if (count > 0) {
502                         ip = *(unsigned long *)(sp + 16);
503                         if (!in_sched_functions(ip))
504                                 return ip;
505                 }
506         } while (count++ < 16);
507         return 0;
508 }
509
510 void show_stack(struct task_struct *p, unsigned long *_sp)
511 {
512         unsigned long ip, newsp, lr;
513         int count = 0;
514         unsigned long sp = (unsigned long)_sp;
515         int firstframe = 1;
516
517         if (sp == 0) {
518                 if (p) {
519                         sp = p->thread.ksp;
520                 } else {
521                         sp = __get_SP();
522                         p = current;
523                 }
524         }
525
526         lr = 0;
527         printk("Call Trace:\n");
528         do {
529                 if (!validate_sp(sp, p, 112))
530                         return;
531
532                 _sp = (unsigned long *) sp;
533                 newsp = _sp[0];
534                 ip = _sp[2];
535                 if (!firstframe || ip != lr) {
536                         printk("[%016lx] [%016lx] ", sp, ip);
537                         print_symbol("%s", ip);
538                         if (firstframe)
539                                 printk(" (unreliable)");
540                         printk("\n");
541                 }
542                 firstframe = 0;
543
544                 /*
545                  * See if this is an exception frame.
546                  * We look for the "regshere" marker in the current frame.
547                  */
548                 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
549                     && _sp[12] == 0x7265677368657265ul) {
550                         struct pt_regs *regs = (struct pt_regs *)
551                                 (sp + STACK_FRAME_OVERHEAD);
552                         printk("--- Exception: %lx", regs->trap);
553                         print_symbol(" at %s\n", regs->nip);
554                         lr = regs->link;
555                         print_symbol("    LR = %s\n", lr);
556                         firstframe = 1;
557                 }
558
559                 sp = newsp;
560         } while (count++ < kstack_depth_to_print);
561 }
562
563 void dump_stack(void)
564 {
565         show_stack(current, (unsigned long *)__get_SP());
566 }
567 EXPORT_SYMBOL(dump_stack);