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.
19 #include <linux/config.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/user.h>
31 #include <linux/elf.h>
32 #include <linux/init.h>
33 #include <linux/prctl.h>
34 #include <linux/init_task.h>
35 #include <linux/module.h>
36 #include <linux/kallsyms.h>
37 #include <linux/mqueue.h>
38 #include <linux/hardirq.h>
40 #include <asm/pgtable.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
44 #include <asm/processor.h>
48 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
;
56 static struct fs_struct init_fs
= INIT_FS
;
57 static struct files_struct init_files
= INIT_FILES
;
58 static struct signal_struct init_signals
= INIT_SIGNALS(init_signals
);
59 static struct sighand_struct init_sighand
= INIT_SIGHAND(init_sighand
);
60 struct mm_struct init_mm
= INIT_MM(init_mm
);
61 EXPORT_SYMBOL(init_mm
);
63 /* this is 8kB-aligned so we can get to the thread_info struct
64 at the base of it from the stack pointer with 1 integer instruction. */
65 union thread_union init_thread_union
66 __attribute__((__section__(".data.init_task"))) =
67 { INIT_THREAD_INFO(init_task
) };
69 /* initial task structure */
70 struct task_struct init_task
= INIT_TASK(init_task
);
71 EXPORT_SYMBOL(init_task
);
73 /* only used to get secondary processor up */
74 struct task_struct
*current_set
[NR_CPUS
] = {&init_task
, };
77 * Make sure the floating-point register state in the
78 * the thread_struct is up to date for task tsk.
80 void flush_fp_to_thread(struct task_struct
*tsk
)
82 if (tsk
->thread
.regs
) {
84 * We need to disable preemption here because if we didn't,
85 * another process could get scheduled after the regs->msr
86 * test but before we have finished saving the FP registers
87 * to the thread_struct. That process could take over the
88 * FPU, and then when we get scheduled again we would store
89 * bogus values for the remaining FP registers.
92 if (tsk
->thread
.regs
->msr
& MSR_FP
) {
95 * This should only ever be called for current or
96 * for a stopped child process. Since we save away
97 * the FP register state on context switch on SMP,
98 * there is something wrong if a stopped child appears
99 * to still have its FP state in the CPU registers.
101 BUG_ON(tsk
!= current
);
109 void enable_kernel_fp(void)
111 WARN_ON(preemptible());
114 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_FP
))
117 giveup_fpu(NULL
); /* just enables FP for kernel */
119 giveup_fpu(last_task_used_math
);
120 #endif /* CONFIG_SMP */
122 EXPORT_SYMBOL(enable_kernel_fp
);
124 int dump_task_fpu(struct task_struct
*tsk
, elf_fpregset_t
*fpregs
)
126 if (!tsk
->thread
.regs
)
128 flush_fp_to_thread(current
);
130 memcpy(fpregs
, &tsk
->thread
.fpr
[0], sizeof(*fpregs
));
135 #ifdef CONFIG_ALTIVEC
136 void enable_kernel_altivec(void)
138 WARN_ON(preemptible());
141 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_VEC
))
142 giveup_altivec(current
);
144 giveup_altivec(NULL
); /* just enable AltiVec for kernel - force */
146 giveup_altivec(last_task_used_altivec
);
147 #endif /* CONFIG_SMP */
149 EXPORT_SYMBOL(enable_kernel_altivec
);
152 * Make sure the VMX/Altivec register state in the
153 * the thread_struct is up to date for task tsk.
155 void flush_altivec_to_thread(struct task_struct
*tsk
)
157 if (tsk
->thread
.regs
) {
159 if (tsk
->thread
.regs
->msr
& MSR_VEC
) {
161 BUG_ON(tsk
!= current
);
163 giveup_altivec(current
);
169 int dump_task_altivec(struct pt_regs
*regs
, elf_vrregset_t
*vrregs
)
171 flush_altivec_to_thread(current
);
172 memcpy(vrregs
, ¤t
->thread
.vr
[0], sizeof(*vrregs
));
175 #endif /* CONFIG_ALTIVEC */
179 void enable_kernel_spe(void)
181 WARN_ON(preemptible());
184 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_SPE
))
187 giveup_spe(NULL
); /* just enable SPE for kernel - force */
189 giveup_spe(last_task_used_spe
);
190 #endif /* __SMP __ */
192 EXPORT_SYMBOL(enable_kernel_spe
);
194 void flush_spe_to_thread(struct task_struct
*tsk
)
196 if (tsk
->thread
.regs
) {
198 if (tsk
->thread
.regs
->msr
& MSR_SPE
) {
200 BUG_ON(tsk
!= current
);
208 int dump_spe(struct pt_regs
*regs
, elf_vrregset_t
*evrregs
)
210 flush_spe_to_thread(current
);
211 /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
212 memcpy(evrregs
, ¤t
->thread
.evr
[0], sizeof(u32
) * 35);
215 #endif /* CONFIG_SPE */
217 static void set_dabr_spr(unsigned long val
)
219 mtspr(SPRN_DABR
, val
);
222 int set_dabr(unsigned long dabr
)
227 if (firmware_has_feature(FW_FEATURE_XDABR
)) {
228 /* We want to catch accesses from kernel and userspace */
229 unsigned long flags
= H_DABRX_KERNEL
|H_DABRX_USER
;
230 ret
= plpar_set_xdabr(dabr
, flags
);
231 } else if (firmware_has_feature(FW_FEATURE_DABR
)) {
232 ret
= plpar_set_dabr(dabr
);
240 static DEFINE_PER_CPU(unsigned long, current_dabr
);
242 struct task_struct
*__switch_to(struct task_struct
*prev
,
243 struct task_struct
*new)
245 struct thread_struct
*new_thread
, *old_thread
;
247 struct task_struct
*last
;
250 /* avoid complexity of lazy save/restore of fpu
251 * by just saving it every time we switch out if
252 * this task used the fpu during the last quantum.
254 * If it tries to use the fpu again, it'll trap and
255 * reload its fp regs. So we don't have to do a restore
256 * every switch, just a save.
259 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_FP
))
261 #ifdef CONFIG_ALTIVEC
263 * If the previous thread used altivec in the last quantum
264 * (thus changing altivec regs) then save them.
265 * We used to check the VRSAVE register but not all apps
266 * set it, so we don't rely on it now (and in fact we need
267 * to save & restore VSCR even if VRSAVE == 0). -- paulus
269 * On SMP we always save/restore altivec regs just to avoid the
270 * complexity of changing processors.
273 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_VEC
))
274 giveup_altivec(prev
);
275 /* Avoid the trap. On smp this this never happens since
276 * we don't set last_task_used_altivec -- Cort
278 if (new->thread
.regs
&& last_task_used_altivec
== new)
279 new->thread
.regs
->msr
|= MSR_VEC
;
280 #endif /* CONFIG_ALTIVEC */
283 * If the previous thread used spe in the last quantum
284 * (thus changing spe regs) then save them.
286 * On SMP we always save/restore spe regs just to avoid the
287 * complexity of changing processors.
289 if ((prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_SPE
)))
291 /* Avoid the trap. On smp this this never happens since
292 * we don't set last_task_used_spe
294 if (new->thread
.regs
&& last_task_used_spe
== new)
295 new->thread
.regs
->msr
|= MSR_SPE
;
296 #endif /* CONFIG_SPE */
297 #endif /* CONFIG_SMP */
299 #ifdef CONFIG_PPC64 /* for now */
300 if (unlikely(__get_cpu_var(current_dabr
) != new->thread
.dabr
)) {
301 set_dabr(new->thread
.dabr
);
302 __get_cpu_var(current_dabr
) = new->thread
.dabr
;
306 new_thread
= &new->thread
;
307 old_thread
= ¤t
->thread
;
308 local_irq_save(flags
);
309 last
= _switch(old_thread
, new_thread
);
311 local_irq_restore(flags
);
316 void show_regs(struct pt_regs
* regs
)
320 printk("NIP: %08lX LR: %08lX SP: %08lX REGS: %p TRAP: %04lx %s\n",
321 regs
->nip
, regs
->link
, regs
->gpr
[1], regs
, regs
->trap
,
323 printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
324 regs
->msr
, regs
->msr
&MSR_EE
? 1 : 0, regs
->msr
&MSR_PR
? 1 : 0,
325 regs
->msr
& MSR_FP
? 1 : 0,regs
->msr
&MSR_ME
? 1 : 0,
326 regs
->msr
&MSR_IR
? 1 : 0,
327 regs
->msr
&MSR_DR
? 1 : 0);
329 if (trap
== 0x300 || trap
== 0x600)
330 printk("DAR: %08lX, DSISR: %08lX\n", regs
->dar
, regs
->dsisr
);
331 printk("TASK = %p[%d] '%s' THREAD: %p\n",
332 current
, current
->pid
, current
->comm
, current
->thread_info
);
333 printk("Last syscall: %ld ", current
->thread
.last_syscall
);
336 printk(" CPU: %d", smp_processor_id());
337 #endif /* CONFIG_SMP */
339 for (i
= 0; i
< 32; i
++) {
342 printk("\n" KERN_INFO
"GPR%02d: ", i
);
343 if (__get_user(r
, ®s
->gpr
[i
]))
346 if (i
== 12 && !FULL_REGS(regs
))
350 #ifdef CONFIG_KALLSYMS
352 * Lookup NIP late so we have the best change of getting the
353 * above info out without failing
355 printk("NIP [%08lx] ", regs
->nip
);
356 print_symbol("%s\n", regs
->nip
);
357 printk("LR [%08lx] ", regs
->link
);
358 print_symbol("%s\n", regs
->link
);
360 show_stack(current
, (unsigned long *) regs
->gpr
[1]);
363 void exit_thread(void)
366 if (last_task_used_math
== current
)
367 last_task_used_math
= NULL
;
368 #ifdef CONFIG_ALTIVEC
369 if (last_task_used_altivec
== current
)
370 last_task_used_altivec
= NULL
;
371 #endif /* CONFIG_ALTIVEC */
373 if (last_task_used_spe
== current
)
374 last_task_used_spe
= NULL
;
376 #endif /* CONFIG_SMP */
379 void flush_thread(void)
382 if (last_task_used_math
== current
)
383 last_task_used_math
= NULL
;
384 #ifdef CONFIG_ALTIVEC
385 if (last_task_used_altivec
== current
)
386 last_task_used_altivec
= NULL
;
387 #endif /* CONFIG_ALTIVEC */
389 if (last_task_used_spe
== current
)
390 last_task_used_spe
= NULL
;
392 #endif /* CONFIG_SMP */
394 #ifdef CONFIG_PPC64 /* for now */
395 if (current
->thread
.dabr
) {
396 current
->thread
.dabr
= 0;
403 release_thread(struct task_struct
*t
)
408 * This gets called before we allocate a new thread and copy
409 * the current task into it.
411 void prepare_to_copy(struct task_struct
*tsk
)
413 flush_fp_to_thread(current
);
414 flush_altivec_to_thread(current
);
415 flush_spe_to_thread(current
);
422 copy_thread(int nr
, unsigned long clone_flags
, unsigned long usp
,
423 unsigned long unused
,
424 struct task_struct
*p
, struct pt_regs
*regs
)
426 struct pt_regs
*childregs
, *kregs
;
427 extern void ret_from_fork(void);
428 unsigned long sp
= (unsigned long)p
->thread_info
+ THREAD_SIZE
;
429 unsigned long childframe
;
431 CHECK_FULL_REGS(regs
);
433 sp
-= sizeof(struct pt_regs
);
434 childregs
= (struct pt_regs
*) sp
;
436 if ((childregs
->msr
& MSR_PR
) == 0) {
437 /* for kernel thread, set `current' and stackptr in new task */
438 childregs
->gpr
[1] = sp
+ sizeof(struct pt_regs
);
439 childregs
->gpr
[2] = (unsigned long) p
;
440 p
->thread
.regs
= NULL
; /* no user register state */
442 childregs
->gpr
[1] = usp
;
443 p
->thread
.regs
= childregs
;
444 if (clone_flags
& CLONE_SETTLS
)
445 childregs
->gpr
[2] = childregs
->gpr
[6];
447 childregs
->gpr
[3] = 0; /* Result from fork() */
448 sp
-= STACK_FRAME_OVERHEAD
;
452 * The way this works is that at some point in the future
453 * some task will call _switch to switch to the new task.
454 * That will pop off the stack frame created below and start
455 * the new task running at ret_from_fork. The new task will
456 * do some house keeping and then return from the fork or clone
457 * system call, using the stack frame created above.
459 sp
-= sizeof(struct pt_regs
);
460 kregs
= (struct pt_regs
*) sp
;
461 sp
-= STACK_FRAME_OVERHEAD
;
463 kregs
->nip
= (unsigned long)ret_from_fork
;
465 p
->thread
.last_syscall
= -1;
471 * Set up a thread for executing a new program
473 void start_thread(struct pt_regs
*regs
, unsigned long nip
, unsigned long sp
)
476 memset(regs
->gpr
, 0, sizeof(regs
->gpr
));
484 regs
->msr
= MSR_USER
;
486 if (last_task_used_math
== current
)
487 last_task_used_math
= NULL
;
488 #ifdef CONFIG_ALTIVEC
489 if (last_task_used_altivec
== current
)
490 last_task_used_altivec
= NULL
;
493 if (last_task_used_spe
== current
)
494 last_task_used_spe
= NULL
;
496 #endif /* CONFIG_SMP */
497 memset(current
->thread
.fpr
, 0, sizeof(current
->thread
.fpr
));
498 current
->thread
.fpscr
= 0;
499 #ifdef CONFIG_ALTIVEC
500 memset(current
->thread
.vr
, 0, sizeof(current
->thread
.vr
));
501 memset(¤t
->thread
.vscr
, 0, sizeof(current
->thread
.vscr
));
502 current
->thread
.vrsave
= 0;
503 current
->thread
.used_vr
= 0;
504 #endif /* CONFIG_ALTIVEC */
506 memset(current
->thread
.evr
, 0, sizeof(current
->thread
.evr
));
507 current
->thread
.acc
= 0;
508 current
->thread
.spefscr
= 0;
509 current
->thread
.used_spe
= 0;
510 #endif /* CONFIG_SPE */
513 #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
514 | PR_FP_EXC_RES | PR_FP_EXC_INV)
516 int set_fpexc_mode(struct task_struct
*tsk
, unsigned int val
)
518 struct pt_regs
*regs
= tsk
->thread
.regs
;
520 /* This is a bit hairy. If we are an SPE enabled processor
521 * (have embedded fp) we store the IEEE exception enable flags in
522 * fpexc_mode. fpexc_mode is also used for setting FP exception
523 * mode (asyn, precise, disabled) for 'Classic' FP. */
524 if (val
& PR_FP_EXC_SW_ENABLE
) {
526 tsk
->thread
.fpexc_mode
= val
&
527 (PR_FP_EXC_SW_ENABLE
| PR_FP_ALL_EXCEPT
);
532 /* on a CONFIG_SPE this does not hurt us. The bits that
533 * __pack_fe01 use do not overlap with bits used for
534 * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
535 * on CONFIG_SPE implementations are reserved so writing to
536 * them does not change anything */
537 if (val
> PR_FP_EXC_PRECISE
)
539 tsk
->thread
.fpexc_mode
= __pack_fe01(val
);
540 if (regs
!= NULL
&& (regs
->msr
& MSR_FP
) != 0)
541 regs
->msr
= (regs
->msr
& ~(MSR_FE0
|MSR_FE1
))
542 | tsk
->thread
.fpexc_mode
;
547 int get_fpexc_mode(struct task_struct
*tsk
, unsigned long adr
)
551 if (tsk
->thread
.fpexc_mode
& PR_FP_EXC_SW_ENABLE
)
553 val
= tsk
->thread
.fpexc_mode
;
558 val
= __unpack_fe01(tsk
->thread
.fpexc_mode
);
559 return put_user(val
, (unsigned int __user
*) adr
);
562 int sys_clone(unsigned long clone_flags
, unsigned long usp
,
563 int __user
*parent_tidp
, void __user
*child_threadptr
,
564 int __user
*child_tidp
, int p6
,
565 struct pt_regs
*regs
)
567 CHECK_FULL_REGS(regs
);
569 usp
= regs
->gpr
[1]; /* stack pointer for child */
570 return do_fork(clone_flags
, usp
, regs
, 0, parent_tidp
, child_tidp
);
573 int sys_fork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
574 unsigned long p4
, unsigned long p5
, unsigned long p6
,
575 struct pt_regs
*regs
)
577 CHECK_FULL_REGS(regs
);
578 return do_fork(SIGCHLD
, regs
->gpr
[1], regs
, 0, NULL
, NULL
);
581 int sys_vfork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
582 unsigned long p4
, unsigned long p5
, unsigned long p6
,
583 struct pt_regs
*regs
)
585 CHECK_FULL_REGS(regs
);
586 return do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, regs
->gpr
[1],
587 regs
, 0, NULL
, NULL
);
590 int sys_execve(unsigned long a0
, unsigned long a1
, unsigned long a2
,
591 unsigned long a3
, unsigned long a4
, unsigned long a5
,
592 struct pt_regs
*regs
)
597 filename
= getname((char __user
*) a0
);
598 error
= PTR_ERR(filename
);
599 if (IS_ERR(filename
))
601 flush_fp_to_thread(current
);
602 flush_altivec_to_thread(current
);
603 flush_spe_to_thread(current
);
604 error
= do_execve(filename
, (char __user
* __user
*) a1
,
605 (char __user
* __user
*) a2
, regs
);
608 current
->ptrace
&= ~PT_DTRACE
;
609 task_unlock(current
);
616 static int validate_sp(unsigned long sp
, struct task_struct
*p
,
617 unsigned long nbytes
)
619 unsigned long stack_page
= (unsigned long)p
->thread_info
;
621 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
622 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
625 #ifdef CONFIG_IRQSTACKS
626 stack_page
= (unsigned long) hardirq_ctx
[task_cpu(p
)];
627 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
628 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
631 stack_page
= (unsigned long) softirq_ctx
[task_cpu(p
)];
632 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
633 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
640 void dump_stack(void)
642 show_stack(current
, NULL
);
645 EXPORT_SYMBOL(dump_stack
);
647 void show_stack(struct task_struct
*tsk
, unsigned long *stack
)
649 unsigned long sp
, stack_top
, prev_sp
, ret
;
651 unsigned long next_exc
= 0;
652 struct pt_regs
*regs
;
653 extern char ret_from_except
, ret_from_except_full
, ret_from_syscall
;
655 sp
= (unsigned long) stack
;
660 asm("mr %0,1" : "=r" (sp
));
662 sp
= tsk
->thread
.ksp
;
665 prev_sp
= (unsigned long) (tsk
->thread_info
+ 1);
666 stack_top
= (unsigned long) tsk
->thread_info
+ THREAD_SIZE
;
667 while (count
< 16 && sp
> prev_sp
&& sp
< stack_top
&& (sp
& 3) == 0) {
669 printk("Call trace:");
670 #ifdef CONFIG_KALLSYMS
678 ret
= *(unsigned long *)(sp
+ 4);
679 printk(" [%08lx] ", ret
);
680 #ifdef CONFIG_KALLSYMS
681 print_symbol("%s", ret
);
684 if (ret
== (unsigned long) &ret_from_except
685 || ret
== (unsigned long) &ret_from_except_full
686 || ret
== (unsigned long) &ret_from_syscall
) {
687 /* sp + 16 points to an exception frame */
688 regs
= (struct pt_regs
*) (sp
+ 16);
689 if (sp
+ 16 + sizeof(*regs
) <= stack_top
)
690 next_exc
= regs
->nip
;
694 sp
= *(unsigned long *)sp
;
696 #ifndef CONFIG_KALLSYMS
702 unsigned long get_wchan(struct task_struct
*p
)
704 unsigned long ip
, sp
;
707 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
711 if (!validate_sp(sp
, p
, 16))
715 sp
= *(unsigned long *)sp
;
716 if (!validate_sp(sp
, p
, 16))
719 ip
= *(unsigned long *)(sp
+ 4);
720 if (!in_sched_functions(ip
))
723 } while (count
++ < 16);
726 EXPORT_SYMBOL(get_wchan
);