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/sections.h>
54 #include <asm/tlbflush.h>
58 struct task_struct
*last_task_used_math
= NULL
;
59 struct task_struct
*last_task_used_altivec
= NULL
;
63 * Make sure the floating-point register state in the
64 * the thread_struct is up to date for task tsk.
66 void flush_fp_to_thread(struct task_struct
*tsk
)
68 if (tsk
->thread
.regs
) {
70 * We need to disable preemption here because if we didn't,
71 * another process could get scheduled after the regs->msr
72 * test but before we have finished saving the FP registers
73 * to the thread_struct. That process could take over the
74 * FPU, and then when we get scheduled again we would store
75 * bogus values for the remaining FP registers.
78 if (tsk
->thread
.regs
->msr
& MSR_FP
) {
81 * This should only ever be called for current or
82 * for a stopped child process. Since we save away
83 * the FP register state on context switch on SMP,
84 * there is something wrong if a stopped child appears
85 * to still have its FP state in the CPU registers.
87 BUG_ON(tsk
!= current
);
95 void enable_kernel_fp(void)
97 WARN_ON(preemptible());
100 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_FP
))
103 giveup_fpu(NULL
); /* just enables FP for kernel */
105 giveup_fpu(last_task_used_math
);
106 #endif /* CONFIG_SMP */
108 EXPORT_SYMBOL(enable_kernel_fp
);
110 int dump_task_fpu(struct task_struct
*tsk
, elf_fpregset_t
*fpregs
)
112 if (!tsk
->thread
.regs
)
114 flush_fp_to_thread(current
);
116 memcpy(fpregs
, &tsk
->thread
.fpr
[0], sizeof(*fpregs
));
121 #ifdef CONFIG_ALTIVEC
123 void enable_kernel_altivec(void)
125 WARN_ON(preemptible());
128 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_VEC
))
129 giveup_altivec(current
);
131 giveup_altivec(NULL
); /* just enables FP for kernel */
133 giveup_altivec(last_task_used_altivec
);
134 #endif /* CONFIG_SMP */
136 EXPORT_SYMBOL(enable_kernel_altivec
);
139 * Make sure the VMX/Altivec register state in the
140 * the thread_struct is up to date for task tsk.
142 void flush_altivec_to_thread(struct task_struct
*tsk
)
144 if (tsk
->thread
.regs
) {
146 if (tsk
->thread
.regs
->msr
& MSR_VEC
) {
148 BUG_ON(tsk
!= current
);
150 giveup_altivec(current
);
156 int dump_task_altivec(struct pt_regs
*regs
, elf_vrregset_t
*vrregs
)
158 flush_altivec_to_thread(current
);
159 memcpy(vrregs
, ¤t
->thread
.vr
[0], sizeof(*vrregs
));
163 #endif /* CONFIG_ALTIVEC */
165 DEFINE_PER_CPU(struct cpu_usage
, cpu_usage_array
);
167 struct task_struct
*__switch_to(struct task_struct
*prev
,
168 struct task_struct
*new)
170 struct thread_struct
*new_thread
, *old_thread
;
172 struct task_struct
*last
;
175 /* avoid complexity of lazy save/restore of fpu
176 * by just saving it every time we switch out if
177 * this task used the fpu during the last quantum.
179 * If it tries to use the fpu again, it'll trap and
180 * reload its fp regs. So we don't have to do a restore
181 * every switch, just a save.
184 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_FP
))
186 #ifdef CONFIG_ALTIVEC
187 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_VEC
))
188 giveup_altivec(prev
);
189 #endif /* CONFIG_ALTIVEC */
190 #endif /* CONFIG_SMP */
192 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
193 /* Avoid the trap. On smp this this never happens since
194 * we don't set last_task_used_altivec -- Cort
196 if (new->thread
.regs
&& last_task_used_altivec
== new)
197 new->thread
.regs
->msr
|= MSR_VEC
;
198 #endif /* CONFIG_ALTIVEC */
202 new_thread
= &new->thread
;
203 old_thread
= ¤t
->thread
;
205 /* Collect purr utilization data per process and per processor wise */
206 /* purr is nothing but processor time base */
208 #if defined(CONFIG_PPC_PSERIES)
209 if (cur_cpu_spec
->firmware_features
& 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
;
220 local_irq_save(flags
);
221 last
= _switch(old_thread
, new_thread
);
223 local_irq_restore(flags
);
228 static int instructions_to_print
= 16;
230 static void show_instructions(struct pt_regs
*regs
)
233 unsigned long pc
= regs
->nip
- (instructions_to_print
* 3 / 4 *
236 printk("Instruction dump:");
238 for (i
= 0; i
< instructions_to_print
; i
++) {
244 if (((REGION_ID(pc
) != KERNEL_REGION_ID
) &&
245 (REGION_ID(pc
) != VMALLOC_REGION_ID
)) ||
246 __get_user(instr
, (unsigned int *)pc
)) {
250 printk("<%08x> ", instr
);
252 printk("%08x ", instr
);
261 void show_regs(struct pt_regs
* regs
)
266 printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
267 regs
->nip
, (unsigned int)regs
->xer
, regs
->link
, regs
->ctr
);
268 printk("REGS: %p TRAP: %04lx %s (%s)\n",
269 regs
, regs
->trap
, print_tainted(), system_utsname
.release
);
270 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
271 "IR/DR: %01x%01x CR: %08X\n",
272 regs
->msr
, regs
->msr
&MSR_EE
? 1 : 0, regs
->msr
&MSR_PR
? 1 : 0,
273 regs
->msr
& MSR_FP
? 1 : 0,regs
->msr
&MSR_ME
? 1 : 0,
274 regs
->msr
&MSR_IR
? 1 : 0,
275 regs
->msr
&MSR_DR
? 1 : 0,
276 (unsigned int)regs
->ccr
);
278 printk("DAR: %016lx DSISR: %016lx\n", regs
->dar
, regs
->dsisr
);
279 printk("TASK: %p[%d] '%s' THREAD: %p",
280 current
, current
->pid
, current
->comm
, current
->thread_info
);
283 printk(" CPU: %d", smp_processor_id());
284 #endif /* CONFIG_SMP */
286 for (i
= 0; i
< 32; i
++) {
288 printk("\n" KERN_INFO
"GPR%02d: ", i
);
291 printk("%016lX ", regs
->gpr
[i
]);
292 if (i
== 13 && !FULL_REGS(regs
))
297 * Lookup NIP late so we have the best change of getting the
298 * above info out without failing
300 printk("NIP [%016lx] ", regs
->nip
);
301 print_symbol("%s\n", regs
->nip
);
302 printk("LR [%016lx] ", regs
->link
);
303 print_symbol("%s\n", regs
->link
);
304 show_stack(current
, (unsigned long *)regs
->gpr
[1]);
305 if (!user_mode(regs
))
306 show_instructions(regs
);
309 void exit_thread(void)
311 kprobe_flush_task(current
);
314 if (last_task_used_math
== current
)
315 last_task_used_math
= NULL
;
316 #ifdef CONFIG_ALTIVEC
317 if (last_task_used_altivec
== current
)
318 last_task_used_altivec
= NULL
;
319 #endif /* CONFIG_ALTIVEC */
320 #endif /* CONFIG_SMP */
323 void flush_thread(void)
325 struct thread_info
*t
= current_thread_info();
327 kprobe_flush_task(current
);
328 if (t
->flags
& _TIF_ABI_PENDING
)
329 t
->flags
^= (_TIF_ABI_PENDING
| _TIF_32BIT
);
332 if (last_task_used_math
== current
)
333 last_task_used_math
= NULL
;
334 #ifdef CONFIG_ALTIVEC
335 if (last_task_used_altivec
== current
)
336 last_task_used_altivec
= NULL
;
337 #endif /* CONFIG_ALTIVEC */
338 #endif /* CONFIG_SMP */
342 release_thread(struct task_struct
*t
)
348 * This gets called before we allocate a new thread and copy
349 * the current task into it.
351 void prepare_to_copy(struct task_struct
*tsk
)
353 flush_fp_to_thread(current
);
354 flush_altivec_to_thread(current
);
361 copy_thread(int nr
, unsigned long clone_flags
, unsigned long usp
,
362 unsigned long unused
, struct task_struct
*p
, struct pt_regs
*regs
)
364 struct pt_regs
*childregs
, *kregs
;
365 extern void ret_from_fork(void);
366 unsigned long sp
= (unsigned long)p
->thread_info
+ THREAD_SIZE
;
369 sp
-= sizeof(struct pt_regs
);
370 childregs
= (struct pt_regs
*) sp
;
372 if ((childregs
->msr
& MSR_PR
) == 0) {
373 /* for kernel thread, set stackptr in new task */
374 childregs
->gpr
[1] = sp
+ sizeof(struct pt_regs
);
375 p
->thread
.regs
= NULL
; /* no user register state */
376 clear_ti_thread_flag(p
->thread_info
, TIF_32BIT
);
378 childregs
->gpr
[1] = usp
;
379 p
->thread
.regs
= childregs
;
380 if (clone_flags
& CLONE_SETTLS
) {
381 if (test_thread_flag(TIF_32BIT
))
382 childregs
->gpr
[2] = childregs
->gpr
[6];
384 childregs
->gpr
[13] = childregs
->gpr
[6];
387 childregs
->gpr
[3] = 0; /* Result from fork() */
388 sp
-= STACK_FRAME_OVERHEAD
;
391 * The way this works is that at some point in the future
392 * some task will call _switch to switch to the new task.
393 * That will pop off the stack frame created below and start
394 * the new task running at ret_from_fork. The new task will
395 * do some house keeping and then return from the fork or clone
396 * system call, using the stack frame created above.
398 sp
-= sizeof(struct pt_regs
);
399 kregs
= (struct pt_regs
*) sp
;
400 sp
-= STACK_FRAME_OVERHEAD
;
402 if (cpu_has_feature(CPU_FTR_SLB
)) {
403 unsigned long sp_vsid
= get_kernel_vsid(sp
);
405 sp_vsid
<<= SLB_VSID_SHIFT
;
406 sp_vsid
|= SLB_VSID_KERNEL
;
407 if (cpu_has_feature(CPU_FTR_16M_PAGE
))
408 sp_vsid
|= SLB_VSID_L
;
410 p
->thread
.ksp_vsid
= sp_vsid
;
414 * The PPC64 ABI makes use of a TOC to contain function
415 * pointers. The function (ret_from_except) is actually a pointer
416 * to the TOC entry. The first entry is a pointer to the actual
419 kregs
->nip
= *((unsigned long *)ret_from_fork
);
425 * Set up a thread for executing a new program
427 void start_thread(struct pt_regs
*regs
, unsigned long fdptr
, unsigned long sp
)
429 unsigned long entry
, toc
, load_addr
= regs
->gpr
[2];
431 /* fdptr is a relocated pointer to the function descriptor for
432 * the elf _start routine. The first entry in the function
433 * descriptor is the entry address of _start and the second
434 * entry is the TOC value we need to use.
437 __get_user(entry
, (unsigned long __user
*)fdptr
);
438 __get_user(toc
, (unsigned long __user
*)fdptr
+1);
440 /* Check whether the e_entry function descriptor entries
441 * need to be relocated before we can use them.
443 if (load_addr
!= 0) {
449 * If we exec out of a kernel thread then thread.regs will not be
452 if (!current
->thread
.regs
) {
453 unsigned long childregs
= (unsigned long)current
->thread_info
+
455 childregs
-= sizeof(struct pt_regs
);
456 current
->thread
.regs
= (struct pt_regs
*)childregs
;
462 regs
->msr
= MSR_USER64
;
464 if (last_task_used_math
== current
)
465 last_task_used_math
= 0;
466 #endif /* CONFIG_SMP */
467 memset(current
->thread
.fpr
, 0, sizeof(current
->thread
.fpr
));
468 current
->thread
.fpscr
= 0;
469 #ifdef CONFIG_ALTIVEC
471 if (last_task_used_altivec
== current
)
472 last_task_used_altivec
= 0;
473 #endif /* CONFIG_SMP */
474 memset(current
->thread
.vr
, 0, sizeof(current
->thread
.vr
));
475 current
->thread
.vscr
.u
[0] = 0;
476 current
->thread
.vscr
.u
[1] = 0;
477 current
->thread
.vscr
.u
[2] = 0;
478 current
->thread
.vscr
.u
[3] = 0x00010000; /* Java mode disabled */
479 current
->thread
.vrsave
= 0;
480 current
->thread
.used_vr
= 0;
481 #endif /* CONFIG_ALTIVEC */
483 EXPORT_SYMBOL(start_thread
);
485 int set_fpexc_mode(struct task_struct
*tsk
, unsigned int val
)
487 struct pt_regs
*regs
= tsk
->thread
.regs
;
489 if (val
> PR_FP_EXC_PRECISE
)
491 tsk
->thread
.fpexc_mode
= __pack_fe01(val
);
492 if (regs
!= NULL
&& (regs
->msr
& MSR_FP
) != 0)
493 regs
->msr
= (regs
->msr
& ~(MSR_FE0
|MSR_FE1
))
494 | tsk
->thread
.fpexc_mode
;
498 int get_fpexc_mode(struct task_struct
*tsk
, unsigned long adr
)
502 val
= __unpack_fe01(tsk
->thread
.fpexc_mode
);
503 return put_user(val
, (unsigned int __user
*) adr
);
506 int sys_clone(unsigned long clone_flags
, unsigned long p2
, unsigned long p3
,
507 unsigned long p4
, unsigned long p5
, unsigned long p6
,
508 struct pt_regs
*regs
)
510 unsigned long parent_tidptr
= 0;
511 unsigned long child_tidptr
= 0;
514 p2
= regs
->gpr
[1]; /* stack pointer for child */
516 if (clone_flags
& (CLONE_PARENT_SETTID
| CLONE_CHILD_SETTID
|
517 CLONE_CHILD_CLEARTID
)) {
520 if (test_thread_flag(TIF_32BIT
)) {
521 parent_tidptr
&= 0xffffffff;
522 child_tidptr
&= 0xffffffff;
526 return do_fork(clone_flags
, p2
, regs
, 0,
527 (int __user
*)parent_tidptr
, (int __user
*)child_tidptr
);
530 int sys_fork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
531 unsigned long p4
, unsigned long p5
, unsigned long p6
,
532 struct pt_regs
*regs
)
534 return do_fork(SIGCHLD
, regs
->gpr
[1], regs
, 0, NULL
, NULL
);
537 int sys_vfork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
538 unsigned long p4
, unsigned long p5
, unsigned long p6
,
539 struct pt_regs
*regs
)
541 return do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, regs
->gpr
[1], regs
, 0,
545 int sys_execve(unsigned long a0
, unsigned long a1
, unsigned long a2
,
546 unsigned long a3
, unsigned long a4
, unsigned long a5
,
547 struct pt_regs
*regs
)
552 filename
= getname((char __user
*) a0
);
553 error
= PTR_ERR(filename
);
554 if (IS_ERR(filename
))
556 flush_fp_to_thread(current
);
557 flush_altivec_to_thread(current
);
558 error
= do_execve(filename
, (char __user
* __user
*) a1
,
559 (char __user
* __user
*) a2
, regs
);
563 current
->ptrace
&= ~PT_DTRACE
;
564 task_unlock(current
);
572 static int kstack_depth_to_print
= 64;
574 static int validate_sp(unsigned long sp
, struct task_struct
*p
,
575 unsigned long nbytes
)
577 unsigned long stack_page
= (unsigned long)p
->thread_info
;
579 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
580 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
583 #ifdef CONFIG_IRQSTACKS
584 stack_page
= (unsigned long) hardirq_ctx
[task_cpu(p
)];
585 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
586 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
589 stack_page
= (unsigned long) softirq_ctx
[task_cpu(p
)];
590 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
591 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
598 unsigned long get_wchan(struct task_struct
*p
)
600 unsigned long ip
, sp
;
603 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
607 if (!validate_sp(sp
, p
, 112))
611 sp
= *(unsigned long *)sp
;
612 if (!validate_sp(sp
, p
, 112))
615 ip
= *(unsigned long *)(sp
+ 16);
616 if (!in_sched_functions(ip
))
619 } while (count
++ < 16);
622 EXPORT_SYMBOL(get_wchan
);
624 void show_stack(struct task_struct
*p
, unsigned long *_sp
)
626 unsigned long ip
, newsp
, lr
;
628 unsigned long sp
= (unsigned long)_sp
;
641 printk("Call Trace:\n");
643 if (!validate_sp(sp
, p
, 112))
646 _sp
= (unsigned long *) sp
;
649 if (!firstframe
|| ip
!= lr
) {
650 printk("[%016lx] [%016lx] ", sp
, ip
);
651 print_symbol("%s", ip
);
653 printk(" (unreliable)");
659 * See if this is an exception frame.
660 * We look for the "regshere" marker in the current frame.
662 if (validate_sp(sp
, p
, sizeof(struct pt_regs
) + 400)
663 && _sp
[12] == 0x7265677368657265ul
) {
664 struct pt_regs
*regs
= (struct pt_regs
*)
665 (sp
+ STACK_FRAME_OVERHEAD
);
666 printk("--- Exception: %lx", regs
->trap
);
667 print_symbol(" at %s\n", regs
->nip
);
669 print_symbol(" LR = %s\n", lr
);
674 } while (count
++ < kstack_depth_to_print
);
677 void dump_stack(void)
679 show_stack(current
, (unsigned long *)__get_SP());
681 EXPORT_SYMBOL(dump_stack
);