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/utsname.h>
39 #include <linux/kprobes.h>
41 #include <asm/pgtable.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
45 #include <asm/processor.h>
47 #include <asm/mmu_context.h>
49 #include <asm/ppcdebug.h>
50 #include <asm/machdep.h>
51 #include <asm/iSeries/HvCallHpt.h>
52 #include <asm/cputable.h>
53 #include <asm/firmware.h>
54 #include <asm/sections.h>
55 #include <asm/tlbflush.h>
59 struct task_struct *last_task_used_math = NULL;
60 struct task_struct *last_task_used_altivec = NULL;
64 * Make sure the floating-point register state in the
65 * the thread_struct is up to date for task tsk.
67 void flush_fp_to_thread(struct task_struct *tsk)
69 if (tsk->thread.regs) {
71 * We need to disable preemption here because if we didn't,
72 * another process could get scheduled after the regs->msr
73 * test but before we have finished saving the FP registers
74 * to the thread_struct. That process could take over the
75 * FPU, and then when we get scheduled again we would store
76 * bogus values for the remaining FP registers.
79 if (tsk->thread.regs->msr & MSR_FP) {
82 * This should only ever be called for current or
83 * for a stopped child process. Since we save away
84 * the FP register state on context switch on SMP,
85 * there is something wrong if a stopped child appears
86 * to still have its FP state in the CPU registers.
88 BUG_ON(tsk != current);
96 void enable_kernel_fp(void)
98 WARN_ON(preemptible());
101 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
104 giveup_fpu(NULL); /* just enables FP for kernel */
106 giveup_fpu(last_task_used_math);
107 #endif /* CONFIG_SMP */
109 EXPORT_SYMBOL(enable_kernel_fp);
111 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
113 if (!tsk->thread.regs)
115 flush_fp_to_thread(current);
117 memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
122 #ifdef CONFIG_ALTIVEC
124 void enable_kernel_altivec(void)
126 WARN_ON(preemptible());
129 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
130 giveup_altivec(current);
132 giveup_altivec(NULL); /* just enables FP for kernel */
134 giveup_altivec(last_task_used_altivec);
135 #endif /* CONFIG_SMP */
137 EXPORT_SYMBOL(enable_kernel_altivec);
140 * Make sure the VMX/Altivec register state in the
141 * the thread_struct is up to date for task tsk.
143 void flush_altivec_to_thread(struct task_struct *tsk)
145 if (tsk->thread.regs) {
147 if (tsk->thread.regs->msr & MSR_VEC) {
149 BUG_ON(tsk != current);
151 giveup_altivec(current);
157 int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
159 flush_altivec_to_thread(current);
160 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
164 #endif /* CONFIG_ALTIVEC */
166 DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
168 struct task_struct *__switch_to(struct task_struct *prev,
169 struct task_struct *new)
171 struct thread_struct *new_thread, *old_thread;
173 struct task_struct *last;
176 /* avoid complexity of lazy save/restore of fpu
177 * by just saving it every time we switch out if
178 * this task used the fpu during the last quantum.
180 * If it tries to use the fpu again, it'll trap and
181 * reload its fp regs. So we don't have to do a restore
182 * every switch, just a save.
185 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
187 #ifdef CONFIG_ALTIVEC
188 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
189 giveup_altivec(prev);
190 #endif /* CONFIG_ALTIVEC */
191 #endif /* CONFIG_SMP */
193 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
194 /* Avoid the trap. On smp this this never happens since
195 * we don't set last_task_used_altivec -- Cort
197 if (new->thread.regs && last_task_used_altivec == new)
198 new->thread.regs->msr |= MSR_VEC;
199 #endif /* CONFIG_ALTIVEC */
203 new_thread = &new->thread;
204 old_thread = ¤t->thread;
206 /* Collect purr utilization data per process and per processor
207 * wise purr is nothing but processor time base
209 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
210 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
211 long unsigned start_tb, current_tb;
212 start_tb = old_thread->start_tb;
213 cu->current_tb = current_tb = mfspr(SPRN_PURR);
214 old_thread->accum_tb += (current_tb - start_tb);
215 new_thread->start_tb = current_tb;
218 local_irq_save(flags);
219 last = _switch(old_thread, new_thread);
221 local_irq_restore(flags);
226 static int instructions_to_print = 16;
228 static void show_instructions(struct pt_regs *regs)
231 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
234 printk("Instruction dump:");
236 for (i = 0; i < instructions_to_print; i++) {
242 if (((REGION_ID(pc) != KERNEL_REGION_ID) &&
243 (REGION_ID(pc) != VMALLOC_REGION_ID)) ||
244 __get_user(instr, (unsigned int *)pc)) {
248 printk("<%08x> ", instr);
250 printk("%08x ", instr);
259 void show_regs(struct pt_regs * regs)
264 printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
265 regs->nip, (unsigned int)regs->xer, regs->link, regs->ctr);
266 printk("REGS: %p TRAP: %04lx %s (%s)\n",
267 regs, regs->trap, print_tainted(), system_utsname.release);
268 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
269 "IR/DR: %01x%01x CR: %08X\n",
270 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
271 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
272 regs->msr&MSR_IR ? 1 : 0,
273 regs->msr&MSR_DR ? 1 : 0,
274 (unsigned int)regs->ccr);
276 printk("DAR: %016lx DSISR: %016lx\n", regs->dar, regs->dsisr);
277 printk("TASK: %p[%d] '%s' THREAD: %p",
278 current, current->pid, current->comm, current->thread_info);
281 printk(" CPU: %d", smp_processor_id());
282 #endif /* CONFIG_SMP */
284 for (i = 0; i < 32; i++) {
286 printk("\n" KERN_INFO "GPR%02d: ", i);
289 printk("%016lX ", regs->gpr[i]);
290 if (i == 13 && !FULL_REGS(regs))
295 * Lookup NIP late so we have the best change of getting the
296 * above info out without failing
298 printk("NIP [%016lx] ", regs->nip);
299 print_symbol("%s\n", regs->nip);
300 printk("LR [%016lx] ", regs->link);
301 print_symbol("%s\n", regs->link);
302 show_stack(current, (unsigned long *)regs->gpr[1]);
303 if (!user_mode(regs))
304 show_instructions(regs);
307 void exit_thread(void)
309 kprobe_flush_task(current);
312 if (last_task_used_math == current)
313 last_task_used_math = NULL;
314 #ifdef CONFIG_ALTIVEC
315 if (last_task_used_altivec == current)
316 last_task_used_altivec = NULL;
317 #endif /* CONFIG_ALTIVEC */
318 #endif /* CONFIG_SMP */
321 void flush_thread(void)
323 struct thread_info *t = current_thread_info();
325 kprobe_flush_task(current);
326 if (t->flags & _TIF_ABI_PENDING)
327 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
330 if (last_task_used_math == current)
331 last_task_used_math = NULL;
332 #ifdef CONFIG_ALTIVEC
333 if (last_task_used_altivec == current)
334 last_task_used_altivec = NULL;
335 #endif /* CONFIG_ALTIVEC */
336 #endif /* CONFIG_SMP */
340 release_thread(struct task_struct *t)
346 * This gets called before we allocate a new thread and copy
347 * the current task into it.
349 void prepare_to_copy(struct task_struct *tsk)
351 flush_fp_to_thread(current);
352 flush_altivec_to_thread(current);
359 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
360 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
362 struct pt_regs *childregs, *kregs;
363 extern void ret_from_fork(void);
364 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
367 sp -= sizeof(struct pt_regs);
368 childregs = (struct pt_regs *) sp;
370 if ((childregs->msr & MSR_PR) == 0) {
371 /* for kernel thread, set stackptr in new task */
372 childregs->gpr[1] = sp + sizeof(struct pt_regs);
373 p->thread.regs = NULL; /* no user register state */
374 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
376 childregs->gpr[1] = usp;
377 p->thread.regs = childregs;
378 if (clone_flags & CLONE_SETTLS) {
379 if (test_thread_flag(TIF_32BIT))
380 childregs->gpr[2] = childregs->gpr[6];
382 childregs->gpr[13] = childregs->gpr[6];
385 childregs->gpr[3] = 0; /* Result from fork() */
386 sp -= STACK_FRAME_OVERHEAD;
389 * The way this works is that at some point in the future
390 * some task will call _switch to switch to the new task.
391 * That will pop off the stack frame created below and start
392 * the new task running at ret_from_fork. The new task will
393 * do some house keeping and then return from the fork or clone
394 * system call, using the stack frame created above.
396 sp -= sizeof(struct pt_regs);
397 kregs = (struct pt_regs *) sp;
398 sp -= STACK_FRAME_OVERHEAD;
400 if (cpu_has_feature(CPU_FTR_SLB)) {
401 unsigned long sp_vsid = get_kernel_vsid(sp);
403 sp_vsid <<= SLB_VSID_SHIFT;
404 sp_vsid |= SLB_VSID_KERNEL;
405 if (cpu_has_feature(CPU_FTR_16M_PAGE))
406 sp_vsid |= SLB_VSID_L;
408 p->thread.ksp_vsid = sp_vsid;
412 * The PPC64 ABI makes use of a TOC to contain function
413 * pointers. The function (ret_from_except) is actually a pointer
414 * to the TOC entry. The first entry is a pointer to the actual
417 kregs->nip = *((unsigned long *)ret_from_fork);
423 * Set up a thread for executing a new program
425 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
427 unsigned long entry, toc, load_addr = regs->gpr[2];
429 /* fdptr is a relocated pointer to the function descriptor for
430 * the elf _start routine. The first entry in the function
431 * descriptor is the entry address of _start and the second
432 * entry is the TOC value we need to use.
435 __get_user(entry, (unsigned long __user *)fdptr);
436 __get_user(toc, (unsigned long __user *)fdptr+1);
438 /* Check whether the e_entry function descriptor entries
439 * need to be relocated before we can use them.
441 if (load_addr != 0) {
447 * If we exec out of a kernel thread then thread.regs will not be
450 if (!current->thread.regs) {
451 unsigned long childregs = (unsigned long)current->thread_info +
453 childregs -= sizeof(struct pt_regs);
454 current->thread.regs = (struct pt_regs *)childregs;
460 regs->msr = MSR_USER64;
462 if (last_task_used_math == current)
463 last_task_used_math = 0;
464 #endif /* CONFIG_SMP */
465 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
466 current->thread.fpscr = 0;
467 #ifdef CONFIG_ALTIVEC
469 if (last_task_used_altivec == current)
470 last_task_used_altivec = 0;
471 #endif /* CONFIG_SMP */
472 memset(current->thread.vr, 0, sizeof(current->thread.vr));
473 current->thread.vscr.u[0] = 0;
474 current->thread.vscr.u[1] = 0;
475 current->thread.vscr.u[2] = 0;
476 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
477 current->thread.vrsave = 0;
478 current->thread.used_vr = 0;
479 #endif /* CONFIG_ALTIVEC */
481 EXPORT_SYMBOL(start_thread);
483 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
485 struct pt_regs *regs = tsk->thread.regs;
487 if (val > PR_FP_EXC_PRECISE)
489 tsk->thread.fpexc_mode = __pack_fe01(val);
490 if (regs != NULL && (regs->msr & MSR_FP) != 0)
491 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
492 | tsk->thread.fpexc_mode;
496 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
500 val = __unpack_fe01(tsk->thread.fpexc_mode);
501 return put_user(val, (unsigned int __user *) adr);
504 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
505 unsigned long p4, unsigned long p5, unsigned long p6,
506 struct pt_regs *regs)
508 unsigned long parent_tidptr = 0;
509 unsigned long child_tidptr = 0;
512 p2 = regs->gpr[1]; /* stack pointer for child */
514 if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
515 CLONE_CHILD_CLEARTID)) {
518 if (test_thread_flag(TIF_32BIT)) {
519 parent_tidptr &= 0xffffffff;
520 child_tidptr &= 0xffffffff;
524 return do_fork(clone_flags, p2, regs, 0,
525 (int __user *)parent_tidptr, (int __user *)child_tidptr);
528 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
529 unsigned long p4, unsigned long p5, unsigned long p6,
530 struct pt_regs *regs)
532 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
535 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
536 unsigned long p4, unsigned long p5, unsigned long p6,
537 struct pt_regs *regs)
539 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
543 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
544 unsigned long a3, unsigned long a4, unsigned long a5,
545 struct pt_regs *regs)
550 filename = getname((char __user *) a0);
551 error = PTR_ERR(filename);
552 if (IS_ERR(filename))
554 flush_fp_to_thread(current);
555 flush_altivec_to_thread(current);
556 error = do_execve(filename, (char __user * __user *) a1,
557 (char __user * __user *) a2, regs);
561 current->ptrace &= ~PT_DTRACE;
562 task_unlock(current);
570 static int kstack_depth_to_print = 64;
572 static int validate_sp(unsigned long sp, struct task_struct *p,
573 unsigned long nbytes)
575 unsigned long stack_page = (unsigned long)p->thread_info;
577 if (sp >= stack_page + sizeof(struct thread_struct)
578 && sp <= stack_page + THREAD_SIZE - nbytes)
581 #ifdef CONFIG_IRQSTACKS
582 stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
583 if (sp >= stack_page + sizeof(struct thread_struct)
584 && sp <= stack_page + THREAD_SIZE - nbytes)
587 stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
588 if (sp >= stack_page + sizeof(struct thread_struct)
589 && sp <= stack_page + THREAD_SIZE - nbytes)
596 unsigned long get_wchan(struct task_struct *p)
598 unsigned long ip, sp;
601 if (!p || p == current || p->state == TASK_RUNNING)
605 if (!validate_sp(sp, p, 112))
609 sp = *(unsigned long *)sp;
610 if (!validate_sp(sp, p, 112))
613 ip = *(unsigned long *)(sp + 16);
614 if (!in_sched_functions(ip))
617 } while (count++ < 16);
620 EXPORT_SYMBOL(get_wchan);
622 void show_stack(struct task_struct *p, unsigned long *_sp)
624 unsigned long ip, newsp, lr;
626 unsigned long sp = (unsigned long)_sp;
639 printk("Call Trace:\n");
641 if (!validate_sp(sp, p, 112))
644 _sp = (unsigned long *) sp;
647 if (!firstframe || ip != lr) {
648 printk("[%016lx] [%016lx] ", sp, ip);
649 print_symbol("%s", ip);
651 printk(" (unreliable)");
657 * See if this is an exception frame.
658 * We look for the "regshere" marker in the current frame.
660 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
661 && _sp[12] == 0x7265677368657265ul) {
662 struct pt_regs *regs = (struct pt_regs *)
663 (sp + STACK_FRAME_OVERHEAD);
664 printk("--- Exception: %lx", regs->trap);
665 print_symbol(" at %s\n", regs->nip);
667 print_symbol(" LR = %s\n", lr);
672 } while (count++ < kstack_depth_to_print);
675 void dump_stack(void)
677 show_stack(current, (unsigned long *)__get_SP());
679 EXPORT_SYMBOL(dump_stack);