2 * linux/arch/ppc64/kernel/process.c
4 * Derived from "arch/i386/kernel/process.c"
5 * Copyright (C) 1995 Linus Torvalds
7 * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
8 * Paul Mackerras (paulus@cs.anu.edu.au)
11 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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.
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>
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/version.h>
40 #include <asm/pgtable.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
44 #include <asm/processor.h>
46 #include <asm/mmu_context.h>
48 #include <asm/ppcdebug.h>
49 #include <asm/machdep.h>
50 #include <asm/iSeries/HvCallHpt.h>
51 #include <asm/cputable.h>
52 #include <asm/sections.h>
53 #include <asm/tlbflush.h>
56 struct task_struct *last_task_used_math = NULL;
57 struct task_struct *last_task_used_altivec = NULL;
60 struct mm_struct ioremap_mm = {
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,
69 * Make sure the floating-point register state in the
70 * the thread_struct is up to date for task tsk.
72 void flush_fp_to_thread(struct task_struct *tsk)
74 if (tsk->thread.regs) {
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.
84 if (tsk->thread.regs->msr & MSR_FP) {
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.
93 BUG_ON(tsk != current);
101 void enable_kernel_fp(void)
103 WARN_ON(preemptible());
106 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
109 giveup_fpu(NULL); /* just enables FP for kernel */
111 giveup_fpu(last_task_used_math);
112 #endif /* CONFIG_SMP */
114 EXPORT_SYMBOL(enable_kernel_fp);
116 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
118 if (!tsk->thread.regs)
120 flush_fp_to_thread(current);
122 memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
127 #ifdef CONFIG_ALTIVEC
129 void enable_kernel_altivec(void)
131 WARN_ON(preemptible());
134 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
135 giveup_altivec(current);
137 giveup_altivec(NULL); /* just enables FP for kernel */
139 giveup_altivec(last_task_used_altivec);
140 #endif /* CONFIG_SMP */
142 EXPORT_SYMBOL(enable_kernel_altivec);
145 * Make sure the VMX/Altivec register state in the
146 * the thread_struct is up to date for task tsk.
148 void flush_altivec_to_thread(struct task_struct *tsk)
150 if (tsk->thread.regs) {
152 if (tsk->thread.regs->msr & MSR_VEC) {
154 BUG_ON(tsk != current);
156 giveup_altivec(current);
162 int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
164 flush_altivec_to_thread(current);
165 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
169 #endif /* CONFIG_ALTIVEC */
171 struct task_struct *__switch_to(struct task_struct *prev,
172 struct task_struct *new)
174 struct thread_struct *new_thread, *old_thread;
176 struct task_struct *last;
179 /* avoid complexity of lazy save/restore of fpu
180 * by just saving it every time we switch out if
181 * this task used the fpu during the last quantum.
183 * If it tries to use the fpu again, it'll trap and
184 * reload its fp regs. So we don't have to do a restore
185 * every switch, just a save.
188 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
190 #ifdef CONFIG_ALTIVEC
191 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
192 giveup_altivec(prev);
193 #endif /* CONFIG_ALTIVEC */
194 #endif /* CONFIG_SMP */
196 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
197 /* Avoid the trap. On smp this this never happens since
198 * we don't set last_task_used_altivec -- Cort
200 if (new->thread.regs && last_task_used_altivec == new)
201 new->thread.regs->msr |= MSR_VEC;
202 #endif /* CONFIG_ALTIVEC */
206 new_thread = &new->thread;
207 old_thread = ¤t->thread;
209 local_irq_save(flags);
210 last = _switch(old_thread, new_thread);
212 local_irq_restore(flags);
217 void show_regs(struct pt_regs * regs)
222 printk("NIP: %016lX XER: %016lX LR: %016lX\n",
223 regs->nip, regs->xer, regs->link);
224 printk("REGS: %p TRAP: %04lx %s (%s)\n",
225 regs, regs->trap, print_tainted(), UTS_RELEASE);
226 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
227 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
228 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
229 regs->msr&MSR_IR ? 1 : 0,
230 regs->msr&MSR_DR ? 1 : 0);
232 if (trap == 0x300 || trap == 0x380 || trap == 0x600)
233 printk("DAR: %016lx, DSISR: %016lx\n", regs->dar, regs->dsisr);
234 printk("TASK: %p[%d] '%s' THREAD: %p",
235 current, current->pid, current->comm, current->thread_info);
238 printk(" CPU: %d", smp_processor_id());
239 #endif /* CONFIG_SMP */
241 for (i = 0; i < 32; i++) {
243 printk("\n" KERN_INFO "GPR%02d: ", i);
246 printk("%016lX ", regs->gpr[i]);
247 if (i == 13 && !FULL_REGS(regs))
252 * Lookup NIP late so we have the best change of getting the
253 * above info out without failing
255 printk("NIP [%016lx] ", regs->nip);
256 print_symbol("%s\n", regs->nip);
257 printk("LR [%016lx] ", regs->link);
258 print_symbol("%s\n", regs->link);
259 show_stack(current, (unsigned long *)regs->gpr[1]);
261 EXPORT_SYMBOL_GPL(show_regs);
263 void exit_thread(void)
266 if (last_task_used_math == current)
267 last_task_used_math = NULL;
268 #ifdef CONFIG_ALTIVEC
269 if (last_task_used_altivec == current)
270 last_task_used_altivec = NULL;
271 #endif /* CONFIG_ALTIVEC */
272 #endif /* CONFIG_SMP */
275 void flush_thread(void)
277 struct thread_info *t = current_thread_info();
279 if (t->flags & _TIF_ABI_PENDING)
280 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
283 if (last_task_used_math == current)
284 last_task_used_math = NULL;
285 #ifdef CONFIG_ALTIVEC
286 if (last_task_used_altivec == current)
287 last_task_used_altivec = NULL;
288 #endif /* CONFIG_ALTIVEC */
289 #endif /* CONFIG_SMP */
293 release_thread(struct task_struct *t)
299 * This gets called before we allocate a new thread and copy
300 * the current task into it.
302 void prepare_to_copy(struct task_struct *tsk)
304 flush_fp_to_thread(current);
305 flush_altivec_to_thread(current);
312 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
313 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
315 struct pt_regs *childregs, *kregs;
316 extern void ret_from_fork(void);
317 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
320 sp -= sizeof(struct pt_regs);
321 childregs = (struct pt_regs *) sp;
323 if ((childregs->msr & MSR_PR) == 0) {
324 /* for kernel thread, set stackptr in new task */
325 childregs->gpr[1] = sp + sizeof(struct pt_regs);
326 p->thread.regs = NULL; /* no user register state */
327 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
328 #ifdef CONFIG_PPC_ISERIES
329 set_ti_thread_flag(p->thread_info, TIF_RUN_LIGHT);
332 childregs->gpr[1] = usp;
333 p->thread.regs = childregs;
334 if (clone_flags & CLONE_SETTLS) {
335 if (test_thread_flag(TIF_32BIT))
336 childregs->gpr[2] = childregs->gpr[6];
338 childregs->gpr[13] = childregs->gpr[6];
341 childregs->gpr[3] = 0; /* Result from fork() */
342 sp -= STACK_FRAME_OVERHEAD;
345 * The way this works is that at some point in the future
346 * some task will call _switch to switch to the new task.
347 * That will pop off the stack frame created below and start
348 * the new task running at ret_from_fork. The new task will
349 * do some house keeping and then return from the fork or clone
350 * system call, using the stack frame created above.
352 sp -= sizeof(struct pt_regs);
353 kregs = (struct pt_regs *) sp;
354 sp -= STACK_FRAME_OVERHEAD;
356 if (cur_cpu_spec->cpu_features & CPU_FTR_SLB) {
357 unsigned long sp_vsid = get_kernel_vsid(sp);
359 sp_vsid <<= SLB_VSID_SHIFT;
360 sp_vsid |= SLB_VSID_KERNEL;
361 if (cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE)
362 sp_vsid |= SLB_VSID_L;
364 p->thread.ksp_vsid = sp_vsid;
368 * The PPC64 ABI makes use of a TOC to contain function
369 * pointers. The function (ret_from_except) is actually a pointer
370 * to the TOC entry. The first entry is a pointer to the actual
373 kregs->nip = *((unsigned long *)ret_from_fork);
379 * Set up a thread for executing a new program
381 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
383 unsigned long entry, toc, load_addr = regs->gpr[2];
385 /* fdptr is a relocated pointer to the function descriptor for
386 * the elf _start routine. The first entry in the function
387 * descriptor is the entry address of _start and the second
388 * entry is the TOC value we need to use.
391 __get_user(entry, (unsigned long __user *)fdptr);
392 __get_user(toc, (unsigned long __user *)fdptr+1);
394 /* Check whether the e_entry function descriptor entries
395 * need to be relocated before we can use them.
397 if (load_addr != 0) {
403 * If we exec out of a kernel thread then thread.regs will not be
406 if (!current->thread.regs) {
407 unsigned long childregs = (unsigned long)current->thread_info +
409 childregs -= sizeof(struct pt_regs);
410 current->thread.regs = (struct pt_regs *)childregs;
416 regs->msr = MSR_USER64;
418 if (last_task_used_math == current)
419 last_task_used_math = 0;
420 #endif /* CONFIG_SMP */
421 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
422 current->thread.fpscr = 0;
423 #ifdef CONFIG_ALTIVEC
425 if (last_task_used_altivec == current)
426 last_task_used_altivec = 0;
427 #endif /* CONFIG_SMP */
428 memset(current->thread.vr, 0, sizeof(current->thread.vr));
429 current->thread.vscr.u[0] = 0;
430 current->thread.vscr.u[1] = 0;
431 current->thread.vscr.u[2] = 0;
432 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
433 current->thread.vrsave = 0;
434 current->thread.used_vr = 0;
435 #endif /* CONFIG_ALTIVEC */
438 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
440 struct pt_regs *regs = tsk->thread.regs;
442 if (val > PR_FP_EXC_PRECISE)
444 tsk->thread.fpexc_mode = __pack_fe01(val);
445 if (regs != NULL && (regs->msr & MSR_FP) != 0)
446 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
447 | tsk->thread.fpexc_mode;
451 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
455 val = __unpack_fe01(tsk->thread.fpexc_mode);
456 return put_user(val, (unsigned int __user *) adr);
459 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
460 unsigned long p4, unsigned long p5, unsigned long p6,
461 struct pt_regs *regs)
463 unsigned long parent_tidptr = 0;
464 unsigned long child_tidptr = 0;
467 p2 = regs->gpr[1]; /* stack pointer for child */
469 if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
470 CLONE_CHILD_CLEARTID)) {
473 if (test_thread_flag(TIF_32BIT)) {
474 parent_tidptr &= 0xffffffff;
475 child_tidptr &= 0xffffffff;
479 return do_fork(clone_flags, p2, regs, 0,
480 (int __user *)parent_tidptr, (int __user *)child_tidptr);
483 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
484 unsigned long p4, unsigned long p5, unsigned long p6,
485 struct pt_regs *regs)
487 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
490 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
491 unsigned long p4, unsigned long p5, unsigned long p6,
492 struct pt_regs *regs)
494 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
498 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
499 unsigned long a3, unsigned long a4, unsigned long a5,
500 struct pt_regs *regs)
505 filename = getname((char __user *) a0);
506 error = PTR_ERR(filename);
507 if (IS_ERR(filename))
509 flush_fp_to_thread(current);
510 flush_altivec_to_thread(current);
511 error = do_execve(filename, (char __user * __user *) a1,
512 (char __user * __user *) a2, regs);
516 current->ptrace &= ~PT_DTRACE;
517 task_unlock(current);
525 static int kstack_depth_to_print = 64;
527 static int validate_sp(unsigned long sp, struct task_struct *p,
528 unsigned long nbytes)
530 unsigned long stack_page = (unsigned long)p->thread_info;
532 if (sp >= stack_page + sizeof(struct thread_struct)
533 && sp <= stack_page + THREAD_SIZE - nbytes)
536 #ifdef CONFIG_IRQSTACKS
537 stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
538 if (sp >= stack_page + sizeof(struct thread_struct)
539 && sp <= stack_page + THREAD_SIZE - nbytes)
542 stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
543 if (sp >= stack_page + sizeof(struct thread_struct)
544 && sp <= stack_page + THREAD_SIZE - nbytes)
551 unsigned long get_wchan(struct task_struct *p)
553 unsigned long ip, sp;
556 if (!p || p == current || p->state == TASK_RUNNING)
560 if (!validate_sp(sp, p, 112))
564 sp = *(unsigned long *)sp;
565 if (!validate_sp(sp, p, 112))
568 ip = *(unsigned long *)(sp + 16);
569 if (!in_sched_functions(ip))
572 } while (count++ < 16);
576 void show_stack(struct task_struct *p, unsigned long *_sp)
578 unsigned long ip, newsp, lr;
580 unsigned long sp = (unsigned long)_sp;
593 printk("Call Trace:\n");
595 if (!validate_sp(sp, p, 112))
598 _sp = (unsigned long *) sp;
601 if (!firstframe || ip != lr) {
602 printk("[%016lx] [%016lx] ", sp, ip);
603 print_symbol("%s", ip);
605 printk(" (unreliable)");
611 * See if this is an exception frame.
612 * We look for the "regshere" marker in the current frame.
614 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
615 && _sp[12] == 0x7265677368657265ul) {
616 struct pt_regs *regs = (struct pt_regs *)
617 (sp + STACK_FRAME_OVERHEAD);
618 printk("--- Exception: %lx", regs->trap);
619 print_symbol(" at %s\n", regs->nip);
621 print_symbol(" LR = %s\n", lr);
626 } while (count++ < kstack_depth_to_print);
629 void dump_stack(void)
631 show_stack(current, (unsigned long *)__get_SP());
633 EXPORT_SYMBOL(dump_stack);