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>
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>
57 struct task_struct *last_task_used_math = NULL;
58 struct task_struct *last_task_used_altivec = NULL;
61 struct mm_struct ioremap_mm = {
63 .mm_users = ATOMIC_INIT(2),
64 .mm_count = ATOMIC_INIT(1),
65 .cpu_vm_mask = CPU_MASK_ALL,
66 .page_table_lock = SPIN_LOCK_UNLOCKED,
70 * Make sure the floating-point register state in the
71 * the thread_struct is up to date for task tsk.
73 void flush_fp_to_thread(struct task_struct *tsk)
75 if (tsk->thread.regs) {
77 * We need to disable preemption here because if we didn't,
78 * another process could get scheduled after the regs->msr
79 * test but before we have finished saving the FP registers
80 * to the thread_struct. That process could take over the
81 * FPU, and then when we get scheduled again we would store
82 * bogus values for the remaining FP registers.
85 if (tsk->thread.regs->msr & MSR_FP) {
88 * This should only ever be called for current or
89 * for a stopped child process. Since we save away
90 * the FP register state on context switch on SMP,
91 * there is something wrong if a stopped child appears
92 * to still have its FP state in the CPU registers.
94 BUG_ON(tsk != current);
102 void enable_kernel_fp(void)
104 WARN_ON(preemptible());
107 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
110 giveup_fpu(NULL); /* just enables FP for kernel */
112 giveup_fpu(last_task_used_math);
113 #endif /* CONFIG_SMP */
115 EXPORT_SYMBOL(enable_kernel_fp);
117 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
119 if (!tsk->thread.regs)
121 flush_fp_to_thread(current);
123 memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
128 #ifdef CONFIG_ALTIVEC
130 void enable_kernel_altivec(void)
132 WARN_ON(preemptible());
135 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
136 giveup_altivec(current);
138 giveup_altivec(NULL); /* just enables FP for kernel */
140 giveup_altivec(last_task_used_altivec);
141 #endif /* CONFIG_SMP */
143 EXPORT_SYMBOL(enable_kernel_altivec);
146 * Make sure the VMX/Altivec register state in the
147 * the thread_struct is up to date for task tsk.
149 void flush_altivec_to_thread(struct task_struct *tsk)
151 if (tsk->thread.regs) {
153 if (tsk->thread.regs->msr & MSR_VEC) {
155 BUG_ON(tsk != current);
157 giveup_altivec(current);
163 int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
165 flush_altivec_to_thread(current);
166 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
170 #endif /* CONFIG_ALTIVEC */
172 DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
174 struct task_struct *__switch_to(struct task_struct *prev,
175 struct task_struct *new)
177 struct thread_struct *new_thread, *old_thread;
179 struct task_struct *last;
182 /* avoid complexity of lazy save/restore of fpu
183 * by just saving it every time we switch out if
184 * this task used the fpu during the last quantum.
186 * If it tries to use the fpu again, it'll trap and
187 * reload its fp regs. So we don't have to do a restore
188 * every switch, just a save.
191 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
193 #ifdef CONFIG_ALTIVEC
194 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
195 giveup_altivec(prev);
196 #endif /* CONFIG_ALTIVEC */
197 #endif /* CONFIG_SMP */
199 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
200 /* Avoid the trap. On smp this this never happens since
201 * we don't set last_task_used_altivec -- Cort
203 if (new->thread.regs && last_task_used_altivec == new)
204 new->thread.regs->msr |= MSR_VEC;
205 #endif /* CONFIG_ALTIVEC */
209 new_thread = &new->thread;
210 old_thread = ¤t->thread;
212 /* Collect purr utilization data per process and per processor wise */
213 /* purr is nothing but processor time base */
215 #if defined(CONFIG_PPC_PSERIES)
216 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
217 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
218 long unsigned start_tb, current_tb;
219 start_tb = old_thread->start_tb;
220 cu->current_tb = current_tb = mfspr(SPRN_PURR);
221 old_thread->accum_tb += (current_tb - start_tb);
222 new_thread->start_tb = current_tb;
227 local_irq_save(flags);
228 last = _switch(old_thread, new_thread);
230 local_irq_restore(flags);
235 static int instructions_to_print = 16;
237 static void show_instructions(struct pt_regs *regs)
240 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
243 printk("Instruction dump:");
245 for (i = 0; i < instructions_to_print; i++) {
251 if (((REGION_ID(pc) != KERNEL_REGION_ID) &&
252 (REGION_ID(pc) != VMALLOC_REGION_ID)) ||
253 __get_user(instr, (unsigned int *)pc)) {
257 printk("<%08x> ", instr);
259 printk("%08x ", instr);
268 void show_regs(struct pt_regs * regs)
273 printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
274 regs->nip, (unsigned int)regs->xer, regs->link, regs->ctr);
275 printk("REGS: %p TRAP: %04lx %s (%s)\n",
276 regs, regs->trap, print_tainted(), system_utsname.release);
277 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
278 "IR/DR: %01x%01x CR: %08X\n",
279 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
280 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
281 regs->msr&MSR_IR ? 1 : 0,
282 regs->msr&MSR_DR ? 1 : 0,
283 (unsigned int)regs->ccr);
285 printk("DAR: %016lx DSISR: %016lx\n", regs->dar, regs->dsisr);
286 printk("TASK: %p[%d] '%s' THREAD: %p",
287 current, current->pid, current->comm, current->thread_info);
290 printk(" CPU: %d", smp_processor_id());
291 #endif /* CONFIG_SMP */
293 for (i = 0; i < 32; i++) {
295 printk("\n" KERN_INFO "GPR%02d: ", i);
298 printk("%016lX ", regs->gpr[i]);
299 if (i == 13 && !FULL_REGS(regs))
304 * Lookup NIP late so we have the best change of getting the
305 * above info out without failing
307 printk("NIP [%016lx] ", regs->nip);
308 print_symbol("%s\n", regs->nip);
309 printk("LR [%016lx] ", regs->link);
310 print_symbol("%s\n", regs->link);
311 show_stack(current, (unsigned long *)regs->gpr[1]);
312 if (!user_mode(regs))
313 show_instructions(regs);
316 void exit_thread(void)
319 if (last_task_used_math == current)
320 last_task_used_math = NULL;
321 #ifdef CONFIG_ALTIVEC
322 if (last_task_used_altivec == current)
323 last_task_used_altivec = NULL;
324 #endif /* CONFIG_ALTIVEC */
325 #endif /* CONFIG_SMP */
328 void flush_thread(void)
330 struct thread_info *t = current_thread_info();
332 if (t->flags & _TIF_ABI_PENDING)
333 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
336 if (last_task_used_math == current)
337 last_task_used_math = NULL;
338 #ifdef CONFIG_ALTIVEC
339 if (last_task_used_altivec == current)
340 last_task_used_altivec = NULL;
341 #endif /* CONFIG_ALTIVEC */
342 #endif /* CONFIG_SMP */
346 release_thread(struct task_struct *t)
352 * This gets called before we allocate a new thread and copy
353 * the current task into it.
355 void prepare_to_copy(struct task_struct *tsk)
357 flush_fp_to_thread(current);
358 flush_altivec_to_thread(current);
365 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
366 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
368 struct pt_regs *childregs, *kregs;
369 extern void ret_from_fork(void);
370 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
373 sp -= sizeof(struct pt_regs);
374 childregs = (struct pt_regs *) sp;
376 if ((childregs->msr & MSR_PR) == 0) {
377 /* for kernel thread, set stackptr in new task */
378 childregs->gpr[1] = sp + sizeof(struct pt_regs);
379 p->thread.regs = NULL; /* no user register state */
380 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
382 childregs->gpr[1] = usp;
383 p->thread.regs = childregs;
384 if (clone_flags & CLONE_SETTLS) {
385 if (test_thread_flag(TIF_32BIT))
386 childregs->gpr[2] = childregs->gpr[6];
388 childregs->gpr[13] = childregs->gpr[6];
391 childregs->gpr[3] = 0; /* Result from fork() */
392 sp -= STACK_FRAME_OVERHEAD;
395 * The way this works is that at some point in the future
396 * some task will call _switch to switch to the new task.
397 * That will pop off the stack frame created below and start
398 * the new task running at ret_from_fork. The new task will
399 * do some house keeping and then return from the fork or clone
400 * system call, using the stack frame created above.
402 sp -= sizeof(struct pt_regs);
403 kregs = (struct pt_regs *) sp;
404 sp -= STACK_FRAME_OVERHEAD;
406 if (cpu_has_feature(CPU_FTR_SLB)) {
407 unsigned long sp_vsid = get_kernel_vsid(sp);
409 sp_vsid <<= SLB_VSID_SHIFT;
410 sp_vsid |= SLB_VSID_KERNEL;
411 if (cpu_has_feature(CPU_FTR_16M_PAGE))
412 sp_vsid |= SLB_VSID_L;
414 p->thread.ksp_vsid = sp_vsid;
418 * The PPC64 ABI makes use of a TOC to contain function
419 * pointers. The function (ret_from_except) is actually a pointer
420 * to the TOC entry. The first entry is a pointer to the actual
423 kregs->nip = *((unsigned long *)ret_from_fork);
429 * Set up a thread for executing a new program
431 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
433 unsigned long entry, toc, load_addr = regs->gpr[2];
435 /* fdptr is a relocated pointer to the function descriptor for
436 * the elf _start routine. The first entry in the function
437 * descriptor is the entry address of _start and the second
438 * entry is the TOC value we need to use.
441 __get_user(entry, (unsigned long __user *)fdptr);
442 __get_user(toc, (unsigned long __user *)fdptr+1);
444 /* Check whether the e_entry function descriptor entries
445 * need to be relocated before we can use them.
447 if (load_addr != 0) {
453 * If we exec out of a kernel thread then thread.regs will not be
456 if (!current->thread.regs) {
457 unsigned long childregs = (unsigned long)current->thread_info +
459 childregs -= sizeof(struct pt_regs);
460 current->thread.regs = (struct pt_regs *)childregs;
466 regs->msr = MSR_USER64;
468 if (last_task_used_math == current)
469 last_task_used_math = 0;
470 #endif /* CONFIG_SMP */
471 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
472 current->thread.fpscr = 0;
473 #ifdef CONFIG_ALTIVEC
475 if (last_task_used_altivec == current)
476 last_task_used_altivec = 0;
477 #endif /* CONFIG_SMP */
478 memset(current->thread.vr, 0, sizeof(current->thread.vr));
479 current->thread.vscr.u[0] = 0;
480 current->thread.vscr.u[1] = 0;
481 current->thread.vscr.u[2] = 0;
482 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
483 current->thread.vrsave = 0;
484 current->thread.used_vr = 0;
485 #endif /* CONFIG_ALTIVEC */
487 EXPORT_SYMBOL(start_thread);
489 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
491 struct pt_regs *regs = tsk->thread.regs;
493 if (val > PR_FP_EXC_PRECISE)
495 tsk->thread.fpexc_mode = __pack_fe01(val);
496 if (regs != NULL && (regs->msr & MSR_FP) != 0)
497 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
498 | tsk->thread.fpexc_mode;
502 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
506 val = __unpack_fe01(tsk->thread.fpexc_mode);
507 return put_user(val, (unsigned int __user *) adr);
510 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
511 unsigned long p4, unsigned long p5, unsigned long p6,
512 struct pt_regs *regs)
514 unsigned long parent_tidptr = 0;
515 unsigned long child_tidptr = 0;
518 p2 = regs->gpr[1]; /* stack pointer for child */
520 if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
521 CLONE_CHILD_CLEARTID)) {
524 if (test_thread_flag(TIF_32BIT)) {
525 parent_tidptr &= 0xffffffff;
526 child_tidptr &= 0xffffffff;
530 return do_fork(clone_flags, p2, regs, 0,
531 (int __user *)parent_tidptr, (int __user *)child_tidptr);
534 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
535 unsigned long p4, unsigned long p5, unsigned long p6,
536 struct pt_regs *regs)
538 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
541 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
542 unsigned long p4, unsigned long p5, unsigned long p6,
543 struct pt_regs *regs)
545 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
549 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
550 unsigned long a3, unsigned long a4, unsigned long a5,
551 struct pt_regs *regs)
556 filename = getname((char __user *) a0);
557 error = PTR_ERR(filename);
558 if (IS_ERR(filename))
560 flush_fp_to_thread(current);
561 flush_altivec_to_thread(current);
562 error = do_execve(filename, (char __user * __user *) a1,
563 (char __user * __user *) a2, regs);
567 current->ptrace &= ~PT_DTRACE;
568 task_unlock(current);
576 static int kstack_depth_to_print = 64;
578 static int validate_sp(unsigned long sp, struct task_struct *p,
579 unsigned long nbytes)
581 unsigned long stack_page = (unsigned long)p->thread_info;
583 if (sp >= stack_page + sizeof(struct thread_struct)
584 && sp <= stack_page + THREAD_SIZE - nbytes)
587 #ifdef CONFIG_IRQSTACKS
588 stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
589 if (sp >= stack_page + sizeof(struct thread_struct)
590 && sp <= stack_page + THREAD_SIZE - nbytes)
593 stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
594 if (sp >= stack_page + sizeof(struct thread_struct)
595 && sp <= stack_page + THREAD_SIZE - nbytes)
602 unsigned long get_wchan(struct task_struct *p)
604 unsigned long ip, sp;
607 if (!p || p == current || p->state == TASK_RUNNING)
611 if (!validate_sp(sp, p, 112))
615 sp = *(unsigned long *)sp;
616 if (!validate_sp(sp, p, 112))
619 ip = *(unsigned long *)(sp + 16);
620 if (!in_sched_functions(ip))
623 } while (count++ < 16);
626 EXPORT_SYMBOL(get_wchan);
628 void show_stack(struct task_struct *p, unsigned long *_sp)
630 unsigned long ip, newsp, lr;
632 unsigned long sp = (unsigned long)_sp;
645 printk("Call Trace:\n");
647 if (!validate_sp(sp, p, 112))
650 _sp = (unsigned long *) sp;
653 if (!firstframe || ip != lr) {
654 printk("[%016lx] [%016lx] ", sp, ip);
655 print_symbol("%s", ip);
657 printk(" (unreliable)");
663 * See if this is an exception frame.
664 * We look for the "regshere" marker in the current frame.
666 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
667 && _sp[12] == 0x7265677368657265ul) {
668 struct pt_regs *regs = (struct pt_regs *)
669 (sp + STACK_FRAME_OVERHEAD);
670 printk("--- Exception: %lx", regs->trap);
671 print_symbol(" at %s\n", regs->nip);
673 print_symbol(" LR = %s\n", lr);
678 } while (count++ < kstack_depth_to_print);
681 void dump_stack(void)
683 show_stack(current, (unsigned long *)__get_SP());
685 EXPORT_SYMBOL(dump_stack);