2 * arch/ppc/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.
20 #include <linux/config.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/ptrace.h>
30 #include <linux/slab.h>
31 #include <linux/user.h>
32 #include <linux/elf.h>
33 #include <linux/init.h>
34 #include <linux/prctl.h>
35 #include <linux/init_task.h>
36 #include <linux/module.h>
37 #include <linux/kallsyms.h>
38 #include <linux/mqueue.h>
39 #include <linux/hardirq.h>
41 #include <asm/pgtable.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
45 #include <asm/processor.h>
49 extern unsigned long _get_SP(void);
51 struct task_struct *last_task_used_math = NULL;
52 struct task_struct *last_task_used_altivec = NULL;
53 struct task_struct *last_task_used_spe = NULL;
55 static struct fs_struct init_fs = INIT_FS;
56 static struct files_struct init_files = INIT_FILES;
57 static struct signal_struct init_signals = INIT_SIGNALS(init_signals);
58 static struct sighand_struct init_sighand = INIT_SIGHAND(init_sighand);
59 struct mm_struct init_mm = INIT_MM(init_mm);
60 EXPORT_SYMBOL(init_mm);
62 /* this is 8kB-aligned so we can get to the thread_info struct
63 at the base of it from the stack pointer with 1 integer instruction. */
64 union thread_union init_thread_union
65 __attribute__((__section__(".data.init_task"))) =
66 { INIT_THREAD_INFO(init_task) };
68 /* initial task structure */
69 struct task_struct init_task = INIT_TASK(init_task);
70 EXPORT_SYMBOL(init_task);
72 /* only used to get secondary processor up */
73 struct task_struct *current_set[NR_CPUS] = {&init_task, };
75 #undef SHOW_TASK_SWITCHES
78 #if defined(CHECK_STACK)
80 kernel_stack_top(struct task_struct *tsk)
82 return ((unsigned long)tsk) + sizeof(union task_union);
86 task_top(struct task_struct *tsk)
88 return ((unsigned long)tsk) + sizeof(struct thread_info);
91 /* check to make sure the kernel stack is healthy */
92 int check_stack(struct task_struct *tsk)
94 unsigned long stack_top = kernel_stack_top(tsk);
95 unsigned long tsk_top = task_top(tsk);
99 /* check thread magic */
100 if ( tsk->thread.magic != THREAD_MAGIC )
103 printk("thread.magic bad: %08x\n", tsk->thread.magic);
108 printk("check_stack(): tsk bad tsk %p\n",tsk);
110 /* check if stored ksp is bad */
111 if ( (tsk->thread.ksp > stack_top) || (tsk->thread.ksp < tsk_top) )
113 printk("stack out of bounds: %s/%d\n"
114 " tsk_top %08lx ksp %08lx stack_top %08lx\n",
116 tsk_top, tsk->thread.ksp, stack_top);
120 /* check if stack ptr RIGHT NOW is bad */
121 if ( (tsk == current) && ((_get_SP() > stack_top ) || (_get_SP() < tsk_top)) )
123 printk("current stack ptr out of bounds: %s/%d\n"
124 " tsk_top %08lx sp %08lx stack_top %08lx\n",
125 current->comm,current->pid,
126 tsk_top, _get_SP(), stack_top);
131 /* check amount of free stack */
132 for ( i = (unsigned long *)task_top(tsk) ; i < kernel_stack_top(tsk) ; i++ )
135 printk("check_stack(): i = %p\n", i);
138 /* only notify if it's less than 900 bytes */
139 if ( (i - (unsigned long *)task_top(tsk)) < 900 )
140 printk("%d bytes free on stack\n",
149 panic("bad kernel stack");
153 #endif /* defined(CHECK_STACK) */
156 * Make sure the floating-point register state in the
157 * the thread_struct is up to date for task tsk.
159 void flush_fp_to_thread(struct task_struct *tsk)
161 if (tsk->thread.regs) {
163 * We need to disable preemption here because if we didn't,
164 * another process could get scheduled after the regs->msr
165 * test but before we have finished saving the FP registers
166 * to the thread_struct. That process could take over the
167 * FPU, and then when we get scheduled again we would store
168 * bogus values for the remaining FP registers.
171 if (tsk->thread.regs->msr & MSR_FP) {
174 * This should only ever be called for current or
175 * for a stopped child process. Since we save away
176 * the FP register state on context switch on SMP,
177 * there is something wrong if a stopped child appears
178 * to still have its FP state in the CPU registers.
180 BUG_ON(tsk != current);
188 void enable_kernel_fp(void)
190 WARN_ON(preemptible());
193 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
196 giveup_fpu(NULL); /* just enables FP for kernel */
198 giveup_fpu(last_task_used_math);
199 #endif /* CONFIG_SMP */
201 EXPORT_SYMBOL(enable_kernel_fp);
203 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
206 if (tsk->thread.regs && (tsk->thread.regs->msr & MSR_FP))
209 memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
213 #ifdef CONFIG_ALTIVEC
214 void enable_kernel_altivec(void)
216 WARN_ON(preemptible());
219 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
220 giveup_altivec(current);
222 giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
224 giveup_altivec(last_task_used_altivec);
225 #endif /* __SMP __ */
227 EXPORT_SYMBOL(enable_kernel_altivec);
230 * Make sure the VMX/Altivec register state in the
231 * the thread_struct is up to date for task tsk.
233 void flush_altivec_to_thread(struct task_struct *tsk)
235 if (tsk->thread.regs) {
237 if (tsk->thread.regs->msr & MSR_VEC) {
239 BUG_ON(tsk != current);
241 giveup_altivec(current);
247 int dump_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
249 if (regs->msr & MSR_VEC)
250 giveup_altivec(current);
251 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
254 #endif /* CONFIG_ALTIVEC */
258 enable_kernel_spe(void)
260 WARN_ON(preemptible());
263 if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
266 giveup_spe(NULL); /* just enable SPE for kernel - force */
268 giveup_spe(last_task_used_spe);
269 #endif /* __SMP __ */
271 EXPORT_SYMBOL(enable_kernel_spe);
273 void flush_spe_to_thread(struct task_struct *tsk)
275 if (tsk->thread.regs) {
277 if (tsk->thread.regs->msr & MSR_SPE) {
279 BUG_ON(tsk != current);
287 int dump_spe(struct pt_regs *regs, elf_vrregset_t *evrregs)
289 if (regs->msr & MSR_SPE)
291 /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
292 memcpy(evrregs, ¤t->thread.evr[0], sizeof(u32) * 35);
295 #endif /* CONFIG_SPE */
297 struct task_struct *__switch_to(struct task_struct *prev,
298 struct task_struct *new)
300 struct thread_struct *new_thread, *old_thread;
302 struct task_struct *last;
311 /* avoid complexity of lazy save/restore of fpu
312 * by just saving it every time we switch out if
313 * this task used the fpu during the last quantum.
315 * If it tries to use the fpu again, it'll trap and
316 * reload its fp regs. So we don't have to do a restore
317 * every switch, just a save.
320 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
322 #ifdef CONFIG_ALTIVEC
324 * If the previous thread used altivec in the last quantum
325 * (thus changing altivec regs) then save them.
326 * We used to check the VRSAVE register but not all apps
327 * set it, so we don't rely on it now (and in fact we need
328 * to save & restore VSCR even if VRSAVE == 0). -- paulus
330 * On SMP we always save/restore altivec regs just to avoid the
331 * complexity of changing processors.
334 if ((prev->thread.regs && (prev->thread.regs->msr & MSR_VEC)))
335 giveup_altivec(prev);
336 #endif /* CONFIG_ALTIVEC */
339 * If the previous thread used spe in the last quantum
340 * (thus changing spe regs) then save them.
342 * On SMP we always save/restore spe regs just to avoid the
343 * complexity of changing processors.
345 if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
347 #endif /* CONFIG_SPE */
348 #endif /* CONFIG_SMP */
350 #ifdef CONFIG_ALTIVEC
351 /* Avoid the trap. On smp this this never happens since
352 * we don't set last_task_used_altivec -- Cort
354 if (new->thread.regs && last_task_used_altivec == new)
355 new->thread.regs->msr |= MSR_VEC;
358 /* Avoid the trap. On smp this this never happens since
359 * we don't set last_task_used_spe
361 if (new->thread.regs && last_task_used_spe == new)
362 new->thread.regs->msr |= MSR_SPE;
363 #endif /* CONFIG_SPE */
364 new_thread = &new->thread;
365 old_thread = ¤t->thread;
366 last = _switch(old_thread, new_thread);
367 local_irq_restore(s);
371 void show_regs(struct pt_regs * regs)
375 printk("NIP: %08lX LR: %08lX SP: %08lX REGS: %p TRAP: %04lx %s\n",
376 regs->nip, regs->link, regs->gpr[1], regs, regs->trap,
378 printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
379 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
380 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
381 regs->msr&MSR_IR ? 1 : 0,
382 regs->msr&MSR_DR ? 1 : 0);
384 if (trap == 0x300 || trap == 0x600)
385 printk("DAR: %08lX, DSISR: %08lX\n", regs->dar, regs->dsisr);
386 printk("TASK = %p[%d] '%s' THREAD: %p\n",
387 current, current->pid, current->comm, current->thread_info);
388 printk("Last syscall: %ld ", current->thread.last_syscall);
391 printk(" CPU: %d", smp_processor_id());
392 #endif /* CONFIG_SMP */
394 for (i = 0; i < 32; i++) {
397 printk("\n" KERN_INFO "GPR%02d: ", i);
398 if (__get_user(r, ®s->gpr[i]))
401 if (i == 12 && !FULL_REGS(regs))
405 #ifdef CONFIG_KALLSYMS
407 * Lookup NIP late so we have the best change of getting the
408 * above info out without failing
410 printk("NIP [%08lx] ", regs->nip);
411 print_symbol("%s\n", regs->nip);
412 printk("LR [%08lx] ", regs->link);
413 print_symbol("%s\n", regs->link);
415 show_stack(current, (unsigned long *) regs->gpr[1]);
418 void exit_thread(void)
420 if (last_task_used_math == current)
421 last_task_used_math = NULL;
422 if (last_task_used_altivec == current)
423 last_task_used_altivec = NULL;
425 if (last_task_used_spe == current)
426 last_task_used_spe = NULL;
430 void flush_thread(void)
432 if (last_task_used_math == current)
433 last_task_used_math = NULL;
434 if (last_task_used_altivec == current)
435 last_task_used_altivec = NULL;
437 if (last_task_used_spe == current)
438 last_task_used_spe = NULL;
443 release_thread(struct task_struct *t)
448 * This gets called before we allocate a new thread and copy
449 * the current task into it.
451 void prepare_to_copy(struct task_struct *tsk)
453 struct pt_regs *regs = tsk->thread.regs;
458 if (regs->msr & MSR_FP)
460 #ifdef CONFIG_ALTIVEC
461 if (regs->msr & MSR_VEC)
462 giveup_altivec(current);
463 #endif /* CONFIG_ALTIVEC */
465 if (regs->msr & MSR_SPE)
467 #endif /* CONFIG_SPE */
475 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
476 unsigned long unused,
477 struct task_struct *p, struct pt_regs *regs)
479 struct pt_regs *childregs, *kregs;
480 extern void ret_from_fork(void);
481 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
482 unsigned long childframe;
484 CHECK_FULL_REGS(regs);
486 sp -= sizeof(struct pt_regs);
487 childregs = (struct pt_regs *) sp;
489 if ((childregs->msr & MSR_PR) == 0) {
490 /* for kernel thread, set `current' and stackptr in new task */
491 childregs->gpr[1] = sp + sizeof(struct pt_regs);
492 childregs->gpr[2] = (unsigned long) p;
493 p->thread.regs = NULL; /* no user register state */
495 childregs->gpr[1] = usp;
496 p->thread.regs = childregs;
497 if (clone_flags & CLONE_SETTLS)
498 childregs->gpr[2] = childregs->gpr[6];
500 childregs->gpr[3] = 0; /* Result from fork() */
501 sp -= STACK_FRAME_OVERHEAD;
505 * The way this works is that at some point in the future
506 * some task will call _switch to switch to the new task.
507 * That will pop off the stack frame created below and start
508 * the new task running at ret_from_fork. The new task will
509 * do some house keeping and then return from the fork or clone
510 * system call, using the stack frame created above.
512 sp -= sizeof(struct pt_regs);
513 kregs = (struct pt_regs *) sp;
514 sp -= STACK_FRAME_OVERHEAD;
516 kregs->nip = (unsigned long)ret_from_fork;
518 p->thread.last_syscall = -1;
524 * Set up a thread for executing a new program
526 void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
529 memset(regs->gpr, 0, sizeof(regs->gpr));
537 regs->msr = MSR_USER;
538 if (last_task_used_math == current)
539 last_task_used_math = NULL;
540 if (last_task_used_altivec == current)
541 last_task_used_altivec = NULL;
543 if (last_task_used_spe == current)
544 last_task_used_spe = NULL;
546 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
547 current->thread.fpscr.val = 0;
548 #ifdef CONFIG_ALTIVEC
549 memset(current->thread.vr, 0, sizeof(current->thread.vr));
550 memset(¤t->thread.vscr, 0, sizeof(current->thread.vscr));
551 current->thread.vrsave = 0;
552 current->thread.used_vr = 0;
553 #endif /* CONFIG_ALTIVEC */
555 memset(current->thread.evr, 0, sizeof(current->thread.evr));
556 current->thread.acc = 0;
557 current->thread.spefscr = 0;
558 current->thread.used_spe = 0;
559 #endif /* CONFIG_SPE */
562 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
563 | PR_FP_EXC_RES | PR_FP_EXC_INV)
565 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
567 struct pt_regs *regs = tsk->thread.regs;
569 /* This is a bit hairy. If we are an SPE enabled processor
570 * (have embedded fp) we store the IEEE exception enable flags in
571 * fpexc_mode. fpexc_mode is also used for setting FP exception
572 * mode (asyn, precise, disabled) for 'Classic' FP. */
573 if (val & PR_FP_EXC_SW_ENABLE) {
575 tsk->thread.fpexc_mode = val &
576 (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
581 /* on a CONFIG_SPE this does not hurt us. The bits that
582 * __pack_fe01 use do not overlap with bits used for
583 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
584 * on CONFIG_SPE implementations are reserved so writing to
585 * them does not change anything */
586 if (val > PR_FP_EXC_PRECISE)
588 tsk->thread.fpexc_mode = __pack_fe01(val);
589 if (regs != NULL && (regs->msr & MSR_FP) != 0)
590 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
591 | tsk->thread.fpexc_mode;
596 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
600 if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
602 val = tsk->thread.fpexc_mode;
607 val = __unpack_fe01(tsk->thread.fpexc_mode);
608 return put_user(val, (unsigned int __user *) adr);
611 int sys_clone(unsigned long clone_flags, unsigned long usp,
612 int __user *parent_tidp, void __user *child_threadptr,
613 int __user *child_tidp, int p6,
614 struct pt_regs *regs)
616 CHECK_FULL_REGS(regs);
618 usp = regs->gpr[1]; /* stack pointer for child */
619 return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
622 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
623 unsigned long p4, unsigned long p5, unsigned long p6,
624 struct pt_regs *regs)
626 CHECK_FULL_REGS(regs);
627 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
630 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
631 unsigned long p4, unsigned long p5, unsigned long p6,
632 struct pt_regs *regs)
634 CHECK_FULL_REGS(regs);
635 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
636 regs, 0, NULL, NULL);
639 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
640 unsigned long a3, unsigned long a4, unsigned long a5,
641 struct pt_regs *regs)
646 filename = getname((char __user *) a0);
647 error = PTR_ERR(filename);
648 if (IS_ERR(filename))
651 if (regs->msr & MSR_FP)
653 #ifdef CONFIG_ALTIVEC
654 if (regs->msr & MSR_VEC)
655 giveup_altivec(current);
656 #endif /* CONFIG_ALTIVEC */
658 if (regs->msr & MSR_SPE)
660 #endif /* CONFIG_SPE */
662 error = do_execve(filename, (char __user *__user *) a1,
663 (char __user *__user *) a2, regs);
666 current->ptrace &= ~PT_DTRACE;
667 task_unlock(current);
674 void dump_stack(void)
676 show_stack(current, NULL);
679 EXPORT_SYMBOL(dump_stack);
681 void show_stack(struct task_struct *tsk, unsigned long *stack)
683 unsigned long sp, stack_top, prev_sp, ret;
685 unsigned long next_exc = 0;
686 struct pt_regs *regs;
687 extern char ret_from_except, ret_from_except_full, ret_from_syscall;
689 sp = (unsigned long) stack;
694 asm("mr %0,1" : "=r" (sp));
696 sp = tsk->thread.ksp;
699 prev_sp = (unsigned long) (tsk->thread_info + 1);
700 stack_top = (unsigned long) tsk->thread_info + THREAD_SIZE;
701 while (count < 16 && sp > prev_sp && sp < stack_top && (sp & 3) == 0) {
703 printk("Call trace:");
704 #ifdef CONFIG_KALLSYMS
712 ret = *(unsigned long *)(sp + 4);
713 printk(" [%08lx] ", ret);
714 #ifdef CONFIG_KALLSYMS
715 print_symbol("%s", ret);
718 if (ret == (unsigned long) &ret_from_except
719 || ret == (unsigned long) &ret_from_except_full
720 || ret == (unsigned long) &ret_from_syscall) {
721 /* sp + 16 points to an exception frame */
722 regs = (struct pt_regs *) (sp + 16);
723 if (sp + 16 + sizeof(*regs) <= stack_top)
724 next_exc = regs->nip;
728 sp = *(unsigned long *)sp;
730 #ifndef CONFIG_KALLSYMS
738 * Low level print for debugging - Cort
740 int __init ll_printk(const char *fmt, ...)
747 i=vsprintf(buf,fmt,args);
753 int lines = 24, cols = 80;
754 int orig_x = 0, orig_y = 0;
756 void puthex(unsigned long val)
758 unsigned char buf[10];
760 for (i = 7; i >= 0; i--)
762 buf[i] = "0123456789ABCDEF"[val & 0x0F];
769 void __init ll_puts(const char *s)
772 char *vidmem = (char *)/*(_ISA_MEM_BASE + 0xB8000) */0xD00B8000;
774 extern int mem_init_done;
776 if ( mem_init_done ) /* assume this means we can printk */
791 * can't ll_puts on chrp without openfirmware yet.
792 * vidmem just needs to be setup for it.
795 if ( _machine != _MACH_prep )
800 while ( ( c = *s++ ) != '\0' ) {
803 if ( ++y >= lines ) {
809 vidmem [ ( x + cols * y ) * 2 ] = c;
812 if ( ++y >= lines ) {
826 unsigned long get_wchan(struct task_struct *p)
828 unsigned long ip, sp;
829 unsigned long stack_page = (unsigned long) p->thread_info;
831 if (!p || p == current || p->state == TASK_RUNNING)
835 sp = *(unsigned long *)sp;
836 if (sp < stack_page || sp >= stack_page + 8188)
839 ip = *(unsigned long *)(sp + 4);
840 if (!in_sched_functions(ip))
843 } while (count++ < 16);