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>
57 #include <asm/plpar_wrappers.h>
60 struct task_struct
*last_task_used_math
= NULL
;
61 struct task_struct
*last_task_used_altivec
= NULL
;
65 * Make sure the floating-point register state in the
66 * the thread_struct is up to date for task tsk.
68 void flush_fp_to_thread(struct task_struct
*tsk
)
70 if (tsk
->thread
.regs
) {
72 * We need to disable preemption here because if we didn't,
73 * another process could get scheduled after the regs->msr
74 * test but before we have finished saving the FP registers
75 * to the thread_struct. That process could take over the
76 * FPU, and then when we get scheduled again we would store
77 * bogus values for the remaining FP registers.
80 if (tsk
->thread
.regs
->msr
& MSR_FP
) {
83 * This should only ever be called for current or
84 * for a stopped child process. Since we save away
85 * the FP register state on context switch on SMP,
86 * there is something wrong if a stopped child appears
87 * to still have its FP state in the CPU registers.
89 BUG_ON(tsk
!= current
);
97 void enable_kernel_fp(void)
99 WARN_ON(preemptible());
102 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_FP
))
105 giveup_fpu(NULL
); /* just enables FP for kernel */
107 giveup_fpu(last_task_used_math
);
108 #endif /* CONFIG_SMP */
110 EXPORT_SYMBOL(enable_kernel_fp
);
112 int dump_task_fpu(struct task_struct
*tsk
, elf_fpregset_t
*fpregs
)
114 if (!tsk
->thread
.regs
)
116 flush_fp_to_thread(current
);
118 memcpy(fpregs
, &tsk
->thread
.fpr
[0], sizeof(*fpregs
));
123 #ifdef CONFIG_ALTIVEC
125 void enable_kernel_altivec(void)
127 WARN_ON(preemptible());
130 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_VEC
))
131 giveup_altivec(current
);
133 giveup_altivec(NULL
); /* just enables FP for kernel */
135 giveup_altivec(last_task_used_altivec
);
136 #endif /* CONFIG_SMP */
138 EXPORT_SYMBOL(enable_kernel_altivec
);
141 * Make sure the VMX/Altivec register state in the
142 * the thread_struct is up to date for task tsk.
144 void flush_altivec_to_thread(struct task_struct
*tsk
)
146 if (tsk
->thread
.regs
) {
148 if (tsk
->thread
.regs
->msr
& MSR_VEC
) {
150 BUG_ON(tsk
!= current
);
152 giveup_altivec(current
);
158 int dump_task_altivec(struct pt_regs
*regs
, elf_vrregset_t
*vrregs
)
160 flush_altivec_to_thread(current
);
161 memcpy(vrregs
, ¤t
->thread
.vr
[0], sizeof(*vrregs
));
165 #endif /* CONFIG_ALTIVEC */
167 static void set_dabr_spr(unsigned long val
)
169 mtspr(SPRN_DABR
, val
);
172 int set_dabr(unsigned long dabr
)
176 if (firmware_has_feature(FW_FEATURE_XDABR
)) {
177 /* We want to catch accesses from kernel and userspace */
178 unsigned long flags
= H_DABRX_KERNEL
|H_DABRX_USER
;
179 ret
= plpar_set_xdabr(dabr
, flags
);
180 } else if (firmware_has_feature(FW_FEATURE_DABR
)) {
181 ret
= plpar_set_dabr(dabr
);
189 DEFINE_PER_CPU(struct cpu_usage
, cpu_usage_array
);
190 static DEFINE_PER_CPU(unsigned long, current_dabr
);
192 struct task_struct
*__switch_to(struct task_struct
*prev
,
193 struct task_struct
*new)
195 struct thread_struct
*new_thread
, *old_thread
;
197 struct task_struct
*last
;
200 /* avoid complexity of lazy save/restore of fpu
201 * by just saving it every time we switch out if
202 * this task used the fpu during the last quantum.
204 * If it tries to use the fpu again, it'll trap and
205 * reload its fp regs. So we don't have to do a restore
206 * every switch, just a save.
209 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_FP
))
211 #ifdef CONFIG_ALTIVEC
212 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_VEC
))
213 giveup_altivec(prev
);
214 #endif /* CONFIG_ALTIVEC */
215 #endif /* CONFIG_SMP */
217 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
218 /* Avoid the trap. On smp this this never happens since
219 * we don't set last_task_used_altivec -- Cort
221 if (new->thread
.regs
&& last_task_used_altivec
== new)
222 new->thread
.regs
->msr
|= MSR_VEC
;
223 #endif /* CONFIG_ALTIVEC */
225 if (unlikely(__get_cpu_var(current_dabr
) != new->thread
.dabr
)) {
226 set_dabr(new->thread
.dabr
);
227 __get_cpu_var(current_dabr
) = new->thread
.dabr
;
232 new_thread
= &new->thread
;
233 old_thread
= ¤t
->thread
;
235 /* Collect purr utilization data per process and per processor
236 * wise purr is nothing but processor time base
238 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
239 struct cpu_usage
*cu
= &__get_cpu_var(cpu_usage_array
);
240 long unsigned start_tb
, current_tb
;
241 start_tb
= old_thread
->start_tb
;
242 cu
->current_tb
= current_tb
= mfspr(SPRN_PURR
);
243 old_thread
->accum_tb
+= (current_tb
- start_tb
);
244 new_thread
->start_tb
= current_tb
;
247 local_irq_save(flags
);
248 last
= _switch(old_thread
, new_thread
);
250 local_irq_restore(flags
);
255 static int instructions_to_print
= 16;
257 static void show_instructions(struct pt_regs
*regs
)
260 unsigned long pc
= regs
->nip
- (instructions_to_print
* 3 / 4 *
263 printk("Instruction dump:");
265 for (i
= 0; i
< instructions_to_print
; i
++) {
271 if (((REGION_ID(pc
) != KERNEL_REGION_ID
) &&
272 (REGION_ID(pc
) != VMALLOC_REGION_ID
)) ||
273 __get_user(instr
, (unsigned int *)pc
)) {
277 printk("<%08x> ", instr
);
279 printk("%08x ", instr
);
288 void show_regs(struct pt_regs
* regs
)
293 printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
294 regs
->nip
, (unsigned int)regs
->xer
, regs
->link
, regs
->ctr
);
295 printk("REGS: %p TRAP: %04lx %s (%s)\n",
296 regs
, regs
->trap
, print_tainted(), system_utsname
.release
);
297 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
298 "IR/DR: %01x%01x CR: %08X\n",
299 regs
->msr
, regs
->msr
&MSR_EE
? 1 : 0, regs
->msr
&MSR_PR
? 1 : 0,
300 regs
->msr
& MSR_FP
? 1 : 0,regs
->msr
&MSR_ME
? 1 : 0,
301 regs
->msr
&MSR_IR
? 1 : 0,
302 regs
->msr
&MSR_DR
? 1 : 0,
303 (unsigned int)regs
->ccr
);
305 printk("DAR: %016lx DSISR: %016lx\n", regs
->dar
, regs
->dsisr
);
306 printk("TASK: %p[%d] '%s' THREAD: %p",
307 current
, current
->pid
, current
->comm
, current
->thread_info
);
310 printk(" CPU: %d", smp_processor_id());
311 #endif /* CONFIG_SMP */
313 for (i
= 0; i
< 32; i
++) {
315 printk("\n" KERN_INFO
"GPR%02d: ", i
);
318 printk("%016lX ", regs
->gpr
[i
]);
319 if (i
== 13 && !FULL_REGS(regs
))
324 * Lookup NIP late so we have the best change of getting the
325 * above info out without failing
327 printk("NIP [%016lx] ", regs
->nip
);
328 print_symbol("%s\n", regs
->nip
);
329 printk("LR [%016lx] ", regs
->link
);
330 print_symbol("%s\n", regs
->link
);
331 show_stack(current
, (unsigned long *)regs
->gpr
[1]);
332 if (!user_mode(regs
))
333 show_instructions(regs
);
336 void exit_thread(void)
338 kprobe_flush_task(current
);
341 if (last_task_used_math
== current
)
342 last_task_used_math
= NULL
;
343 #ifdef CONFIG_ALTIVEC
344 if (last_task_used_altivec
== current
)
345 last_task_used_altivec
= NULL
;
346 #endif /* CONFIG_ALTIVEC */
347 #endif /* CONFIG_SMP */
350 void flush_thread(void)
352 struct thread_info
*t
= current_thread_info();
354 kprobe_flush_task(current
);
355 if (t
->flags
& _TIF_ABI_PENDING
)
356 t
->flags
^= (_TIF_ABI_PENDING
| _TIF_32BIT
);
359 if (last_task_used_math
== current
)
360 last_task_used_math
= NULL
;
361 #ifdef CONFIG_ALTIVEC
362 if (last_task_used_altivec
== current
)
363 last_task_used_altivec
= NULL
;
364 #endif /* CONFIG_ALTIVEC */
365 #endif /* CONFIG_SMP */
367 if (current
->thread
.dabr
) {
368 current
->thread
.dabr
= 0;
374 release_thread(struct task_struct
*t
)
380 * This gets called before we allocate a new thread and copy
381 * the current task into it.
383 void prepare_to_copy(struct task_struct
*tsk
)
385 flush_fp_to_thread(current
);
386 flush_altivec_to_thread(current
);
393 copy_thread(int nr
, unsigned long clone_flags
, unsigned long usp
,
394 unsigned long unused
, struct task_struct
*p
, struct pt_regs
*regs
)
396 struct pt_regs
*childregs
, *kregs
;
397 extern void ret_from_fork(void);
398 unsigned long sp
= (unsigned long)p
->thread_info
+ THREAD_SIZE
;
401 sp
-= sizeof(struct pt_regs
);
402 childregs
= (struct pt_regs
*) sp
;
404 if ((childregs
->msr
& MSR_PR
) == 0) {
405 /* for kernel thread, set stackptr in new task */
406 childregs
->gpr
[1] = sp
+ sizeof(struct pt_regs
);
407 p
->thread
.regs
= NULL
; /* no user register state */
408 clear_ti_thread_flag(p
->thread_info
, TIF_32BIT
);
410 childregs
->gpr
[1] = usp
;
411 p
->thread
.regs
= childregs
;
412 if (clone_flags
& CLONE_SETTLS
) {
413 if (test_thread_flag(TIF_32BIT
))
414 childregs
->gpr
[2] = childregs
->gpr
[6];
416 childregs
->gpr
[13] = childregs
->gpr
[6];
419 childregs
->gpr
[3] = 0; /* Result from fork() */
420 sp
-= STACK_FRAME_OVERHEAD
;
423 * The way this works is that at some point in the future
424 * some task will call _switch to switch to the new task.
425 * That will pop off the stack frame created below and start
426 * the new task running at ret_from_fork. The new task will
427 * do some house keeping and then return from the fork or clone
428 * system call, using the stack frame created above.
430 sp
-= sizeof(struct pt_regs
);
431 kregs
= (struct pt_regs
*) sp
;
432 sp
-= STACK_FRAME_OVERHEAD
;
434 if (cpu_has_feature(CPU_FTR_SLB
)) {
435 unsigned long sp_vsid
= get_kernel_vsid(sp
);
437 sp_vsid
<<= SLB_VSID_SHIFT
;
438 sp_vsid
|= SLB_VSID_KERNEL
;
439 if (cpu_has_feature(CPU_FTR_16M_PAGE
))
440 sp_vsid
|= SLB_VSID_L
;
442 p
->thread
.ksp_vsid
= sp_vsid
;
446 * The PPC64 ABI makes use of a TOC to contain function
447 * pointers. The function (ret_from_except) is actually a pointer
448 * to the TOC entry. The first entry is a pointer to the actual
451 kregs
->nip
= *((unsigned long *)ret_from_fork
);
457 * Set up a thread for executing a new program
459 void start_thread(struct pt_regs
*regs
, unsigned long fdptr
, unsigned long sp
)
461 unsigned long entry
, toc
, load_addr
= regs
->gpr
[2];
463 /* fdptr is a relocated pointer to the function descriptor for
464 * the elf _start routine. The first entry in the function
465 * descriptor is the entry address of _start and the second
466 * entry is the TOC value we need to use.
469 __get_user(entry
, (unsigned long __user
*)fdptr
);
470 __get_user(toc
, (unsigned long __user
*)fdptr
+1);
472 /* Check whether the e_entry function descriptor entries
473 * need to be relocated before we can use them.
475 if (load_addr
!= 0) {
481 * If we exec out of a kernel thread then thread.regs will not be
484 if (!current
->thread
.regs
) {
485 unsigned long childregs
= (unsigned long)current
->thread_info
+
487 childregs
-= sizeof(struct pt_regs
);
488 current
->thread
.regs
= (struct pt_regs
*)childregs
;
494 regs
->msr
= MSR_USER64
;
496 if (last_task_used_math
== current
)
497 last_task_used_math
= 0;
498 #endif /* CONFIG_SMP */
499 memset(current
->thread
.fpr
, 0, sizeof(current
->thread
.fpr
));
500 current
->thread
.fpscr
= 0;
501 #ifdef CONFIG_ALTIVEC
503 if (last_task_used_altivec
== current
)
504 last_task_used_altivec
= 0;
505 #endif /* CONFIG_SMP */
506 memset(current
->thread
.vr
, 0, sizeof(current
->thread
.vr
));
507 current
->thread
.vscr
.u
[0] = 0;
508 current
->thread
.vscr
.u
[1] = 0;
509 current
->thread
.vscr
.u
[2] = 0;
510 current
->thread
.vscr
.u
[3] = 0x00010000; /* Java mode disabled */
511 current
->thread
.vrsave
= 0;
512 current
->thread
.used_vr
= 0;
513 #endif /* CONFIG_ALTIVEC */
515 EXPORT_SYMBOL(start_thread
);
517 int set_fpexc_mode(struct task_struct
*tsk
, unsigned int val
)
519 struct pt_regs
*regs
= tsk
->thread
.regs
;
521 if (val
> PR_FP_EXC_PRECISE
)
523 tsk
->thread
.fpexc_mode
= __pack_fe01(val
);
524 if (regs
!= NULL
&& (regs
->msr
& MSR_FP
) != 0)
525 regs
->msr
= (regs
->msr
& ~(MSR_FE0
|MSR_FE1
))
526 | tsk
->thread
.fpexc_mode
;
530 int get_fpexc_mode(struct task_struct
*tsk
, unsigned long adr
)
534 val
= __unpack_fe01(tsk
->thread
.fpexc_mode
);
535 return put_user(val
, (unsigned int __user
*) adr
);
538 int sys_clone(unsigned long clone_flags
, unsigned long p2
, unsigned long p3
,
539 unsigned long p4
, unsigned long p5
, unsigned long p6
,
540 struct pt_regs
*regs
)
542 unsigned long parent_tidptr
= 0;
543 unsigned long child_tidptr
= 0;
546 p2
= regs
->gpr
[1]; /* stack pointer for child */
548 if (clone_flags
& (CLONE_PARENT_SETTID
| CLONE_CHILD_SETTID
|
549 CLONE_CHILD_CLEARTID
)) {
552 if (test_thread_flag(TIF_32BIT
)) {
553 parent_tidptr
&= 0xffffffff;
554 child_tidptr
&= 0xffffffff;
558 return do_fork(clone_flags
, p2
, regs
, 0,
559 (int __user
*)parent_tidptr
, (int __user
*)child_tidptr
);
562 int sys_fork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
563 unsigned long p4
, unsigned long p5
, unsigned long p6
,
564 struct pt_regs
*regs
)
566 return do_fork(SIGCHLD
, regs
->gpr
[1], regs
, 0, NULL
, NULL
);
569 int sys_vfork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
570 unsigned long p4
, unsigned long p5
, unsigned long p6
,
571 struct pt_regs
*regs
)
573 return do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, regs
->gpr
[1], regs
, 0,
577 int sys_execve(unsigned long a0
, unsigned long a1
, unsigned long a2
,
578 unsigned long a3
, unsigned long a4
, unsigned long a5
,
579 struct pt_regs
*regs
)
584 filename
= getname((char __user
*) a0
);
585 error
= PTR_ERR(filename
);
586 if (IS_ERR(filename
))
588 flush_fp_to_thread(current
);
589 flush_altivec_to_thread(current
);
590 error
= do_execve(filename
, (char __user
* __user
*) a1
,
591 (char __user
* __user
*) a2
, regs
);
595 current
->ptrace
&= ~PT_DTRACE
;
596 task_unlock(current
);
604 static int kstack_depth_to_print
= 64;
606 static int validate_sp(unsigned long sp
, struct task_struct
*p
,
607 unsigned long nbytes
)
609 unsigned long stack_page
= (unsigned long)p
->thread_info
;
611 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
612 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
615 #ifdef CONFIG_IRQSTACKS
616 stack_page
= (unsigned long) hardirq_ctx
[task_cpu(p
)];
617 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
618 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
621 stack_page
= (unsigned long) softirq_ctx
[task_cpu(p
)];
622 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
623 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
630 unsigned long get_wchan(struct task_struct
*p
)
632 unsigned long ip
, sp
;
635 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
639 if (!validate_sp(sp
, p
, 112))
643 sp
= *(unsigned long *)sp
;
644 if (!validate_sp(sp
, p
, 112))
647 ip
= *(unsigned long *)(sp
+ 16);
648 if (!in_sched_functions(ip
))
651 } while (count
++ < 16);
654 EXPORT_SYMBOL(get_wchan
);
656 void show_stack(struct task_struct
*p
, unsigned long *_sp
)
658 unsigned long ip
, newsp
, lr
;
660 unsigned long sp
= (unsigned long)_sp
;
673 printk("Call Trace:\n");
675 if (!validate_sp(sp
, p
, 112))
678 _sp
= (unsigned long *) sp
;
681 if (!firstframe
|| ip
!= lr
) {
682 printk("[%016lx] [%016lx] ", sp
, ip
);
683 print_symbol("%s", ip
);
685 printk(" (unreliable)");
691 * See if this is an exception frame.
692 * We look for the "regshere" marker in the current frame.
694 if (validate_sp(sp
, p
, sizeof(struct pt_regs
) + 400)
695 && _sp
[12] == 0x7265677368657265ul
) {
696 struct pt_regs
*regs
= (struct pt_regs
*)
697 (sp
+ STACK_FRAME_OVERHEAD
);
698 printk("--- Exception: %lx", regs
->trap
);
699 print_symbol(" at %s\n", regs
->nip
);
701 print_symbol(" LR = %s\n", lr
);
706 } while (count
++ < kstack_depth_to_print
);
709 void dump_stack(void)
711 show_stack(current
, (unsigned long *)__get_SP());
713 EXPORT_SYMBOL(dump_stack
);