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
;
62 * Make sure the floating-point register state in the
63 * the thread_struct is up to date for task tsk.
65 void flush_fp_to_thread(struct task_struct
*tsk
)
67 if (tsk
->thread
.regs
) {
69 * We need to disable preemption here because if we didn't,
70 * another process could get scheduled after the regs->msr
71 * test but before we have finished saving the FP registers
72 * to the thread_struct. That process could take over the
73 * FPU, and then when we get scheduled again we would store
74 * bogus values for the remaining FP registers.
77 if (tsk
->thread
.regs
->msr
& MSR_FP
) {
80 * This should only ever be called for current or
81 * for a stopped child process. Since we save away
82 * the FP register state on context switch on SMP,
83 * there is something wrong if a stopped child appears
84 * to still have its FP state in the CPU registers.
86 BUG_ON(tsk
!= current
);
94 void enable_kernel_fp(void)
96 WARN_ON(preemptible());
99 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_FP
))
102 giveup_fpu(NULL
); /* just enables FP for kernel */
104 giveup_fpu(last_task_used_math
);
105 #endif /* CONFIG_SMP */
107 EXPORT_SYMBOL(enable_kernel_fp
);
109 int dump_task_fpu(struct task_struct
*tsk
, elf_fpregset_t
*fpregs
)
111 if (!tsk
->thread
.regs
)
113 flush_fp_to_thread(current
);
115 memcpy(fpregs
, &tsk
->thread
.fpr
[0], sizeof(*fpregs
));
120 #ifdef CONFIG_ALTIVEC
122 void enable_kernel_altivec(void)
124 WARN_ON(preemptible());
127 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_VEC
))
128 giveup_altivec(current
);
130 giveup_altivec(NULL
); /* just enables FP for kernel */
132 giveup_altivec(last_task_used_altivec
);
133 #endif /* CONFIG_SMP */
135 EXPORT_SYMBOL(enable_kernel_altivec
);
138 * Make sure the VMX/Altivec register state in the
139 * the thread_struct is up to date for task tsk.
141 void flush_altivec_to_thread(struct task_struct
*tsk
)
143 if (tsk
->thread
.regs
) {
145 if (tsk
->thread
.regs
->msr
& MSR_VEC
) {
147 BUG_ON(tsk
!= current
);
149 giveup_altivec(current
);
155 int dump_task_altivec(struct pt_regs
*regs
, elf_vrregset_t
*vrregs
)
157 flush_altivec_to_thread(current
);
158 memcpy(vrregs
, ¤t
->thread
.vr
[0], sizeof(*vrregs
));
162 #endif /* CONFIG_ALTIVEC */
164 DEFINE_PER_CPU(struct cpu_usage
, cpu_usage_array
);
166 struct task_struct
*__switch_to(struct task_struct
*prev
,
167 struct task_struct
*new)
169 struct thread_struct
*new_thread
, *old_thread
;
171 struct task_struct
*last
;
174 /* avoid complexity of lazy save/restore of fpu
175 * by just saving it every time we switch out if
176 * this task used the fpu during the last quantum.
178 * If it tries to use the fpu again, it'll trap and
179 * reload its fp regs. So we don't have to do a restore
180 * every switch, just a save.
183 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_FP
))
185 #ifdef CONFIG_ALTIVEC
186 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_VEC
))
187 giveup_altivec(prev
);
188 #endif /* CONFIG_ALTIVEC */
189 #endif /* CONFIG_SMP */
191 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
192 /* Avoid the trap. On smp this this never happens since
193 * we don't set last_task_used_altivec -- Cort
195 if (new->thread
.regs
&& last_task_used_altivec
== new)
196 new->thread
.regs
->msr
|= MSR_VEC
;
197 #endif /* CONFIG_ALTIVEC */
201 new_thread
= &new->thread
;
202 old_thread
= ¤t
->thread
;
204 /* Collect purr utilization data per process and per processor wise */
205 /* purr is nothing but processor time base */
207 #if defined(CONFIG_PPC_PSERIES)
208 if (cur_cpu_spec
->firmware_features
& FW_FEATURE_SPLPAR
) {
209 struct cpu_usage
*cu
= &__get_cpu_var(cpu_usage_array
);
210 long unsigned start_tb
, current_tb
;
211 start_tb
= old_thread
->start_tb
;
212 cu
->current_tb
= current_tb
= mfspr(SPRN_PURR
);
213 old_thread
->accum_tb
+= (current_tb
- start_tb
);
214 new_thread
->start_tb
= current_tb
;
219 local_irq_save(flags
);
220 last
= _switch(old_thread
, new_thread
);
222 local_irq_restore(flags
);
227 static int instructions_to_print
= 16;
229 static void show_instructions(struct pt_regs
*regs
)
232 unsigned long pc
= regs
->nip
- (instructions_to_print
* 3 / 4 *
235 printk("Instruction dump:");
237 for (i
= 0; i
< instructions_to_print
; i
++) {
243 if (((REGION_ID(pc
) != KERNEL_REGION_ID
) &&
244 (REGION_ID(pc
) != VMALLOC_REGION_ID
)) ||
245 __get_user(instr
, (unsigned int *)pc
)) {
249 printk("<%08x> ", instr
);
251 printk("%08x ", instr
);
260 void show_regs(struct pt_regs
* regs
)
265 printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
266 regs
->nip
, (unsigned int)regs
->xer
, regs
->link
, regs
->ctr
);
267 printk("REGS: %p TRAP: %04lx %s (%s)\n",
268 regs
, regs
->trap
, print_tainted(), system_utsname
.release
);
269 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
270 "IR/DR: %01x%01x CR: %08X\n",
271 regs
->msr
, regs
->msr
&MSR_EE
? 1 : 0, regs
->msr
&MSR_PR
? 1 : 0,
272 regs
->msr
& MSR_FP
? 1 : 0,regs
->msr
&MSR_ME
? 1 : 0,
273 regs
->msr
&MSR_IR
? 1 : 0,
274 regs
->msr
&MSR_DR
? 1 : 0,
275 (unsigned int)regs
->ccr
);
277 printk("DAR: %016lx DSISR: %016lx\n", regs
->dar
, regs
->dsisr
);
278 printk("TASK: %p[%d] '%s' THREAD: %p",
279 current
, current
->pid
, current
->comm
, current
->thread_info
);
282 printk(" CPU: %d", smp_processor_id());
283 #endif /* CONFIG_SMP */
285 for (i
= 0; i
< 32; i
++) {
287 printk("\n" KERN_INFO
"GPR%02d: ", i
);
290 printk("%016lX ", regs
->gpr
[i
]);
291 if (i
== 13 && !FULL_REGS(regs
))
296 * Lookup NIP late so we have the best change of getting the
297 * above info out without failing
299 printk("NIP [%016lx] ", regs
->nip
);
300 print_symbol("%s\n", regs
->nip
);
301 printk("LR [%016lx] ", regs
->link
);
302 print_symbol("%s\n", regs
->link
);
303 show_stack(current
, (unsigned long *)regs
->gpr
[1]);
304 if (!user_mode(regs
))
305 show_instructions(regs
);
308 void exit_thread(void)
311 if (last_task_used_math
== current
)
312 last_task_used_math
= NULL
;
313 #ifdef CONFIG_ALTIVEC
314 if (last_task_used_altivec
== current
)
315 last_task_used_altivec
= NULL
;
316 #endif /* CONFIG_ALTIVEC */
317 #endif /* CONFIG_SMP */
320 void flush_thread(void)
322 struct thread_info
*t
= current_thread_info();
324 if (t
->flags
& _TIF_ABI_PENDING
)
325 t
->flags
^= (_TIF_ABI_PENDING
| _TIF_32BIT
);
328 if (last_task_used_math
== current
)
329 last_task_used_math
= NULL
;
330 #ifdef CONFIG_ALTIVEC
331 if (last_task_used_altivec
== current
)
332 last_task_used_altivec
= NULL
;
333 #endif /* CONFIG_ALTIVEC */
334 #endif /* CONFIG_SMP */
338 release_thread(struct task_struct
*t
)
344 * This gets called before we allocate a new thread and copy
345 * the current task into it.
347 void prepare_to_copy(struct task_struct
*tsk
)
349 flush_fp_to_thread(current
);
350 flush_altivec_to_thread(current
);
357 copy_thread(int nr
, unsigned long clone_flags
, unsigned long usp
,
358 unsigned long unused
, struct task_struct
*p
, struct pt_regs
*regs
)
360 struct pt_regs
*childregs
, *kregs
;
361 extern void ret_from_fork(void);
362 unsigned long sp
= (unsigned long)p
->thread_info
+ THREAD_SIZE
;
365 sp
-= sizeof(struct pt_regs
);
366 childregs
= (struct pt_regs
*) sp
;
368 if ((childregs
->msr
& MSR_PR
) == 0) {
369 /* for kernel thread, set stackptr in new task */
370 childregs
->gpr
[1] = sp
+ sizeof(struct pt_regs
);
371 p
->thread
.regs
= NULL
; /* no user register state */
372 clear_ti_thread_flag(p
->thread_info
, TIF_32BIT
);
374 childregs
->gpr
[1] = usp
;
375 p
->thread
.regs
= childregs
;
376 if (clone_flags
& CLONE_SETTLS
) {
377 if (test_thread_flag(TIF_32BIT
))
378 childregs
->gpr
[2] = childregs
->gpr
[6];
380 childregs
->gpr
[13] = childregs
->gpr
[6];
383 childregs
->gpr
[3] = 0; /* Result from fork() */
384 sp
-= STACK_FRAME_OVERHEAD
;
387 * The way this works is that at some point in the future
388 * some task will call _switch to switch to the new task.
389 * That will pop off the stack frame created below and start
390 * the new task running at ret_from_fork. The new task will
391 * do some house keeping and then return from the fork or clone
392 * system call, using the stack frame created above.
394 sp
-= sizeof(struct pt_regs
);
395 kregs
= (struct pt_regs
*) sp
;
396 sp
-= STACK_FRAME_OVERHEAD
;
398 if (cpu_has_feature(CPU_FTR_SLB
)) {
399 unsigned long sp_vsid
= get_kernel_vsid(sp
);
401 sp_vsid
<<= SLB_VSID_SHIFT
;
402 sp_vsid
|= SLB_VSID_KERNEL
;
403 if (cpu_has_feature(CPU_FTR_16M_PAGE
))
404 sp_vsid
|= SLB_VSID_L
;
406 p
->thread
.ksp_vsid
= sp_vsid
;
410 * The PPC64 ABI makes use of a TOC to contain function
411 * pointers. The function (ret_from_except) is actually a pointer
412 * to the TOC entry. The first entry is a pointer to the actual
415 kregs
->nip
= *((unsigned long *)ret_from_fork
);
421 * Set up a thread for executing a new program
423 void start_thread(struct pt_regs
*regs
, unsigned long fdptr
, unsigned long sp
)
425 unsigned long entry
, toc
, load_addr
= regs
->gpr
[2];
427 /* fdptr is a relocated pointer to the function descriptor for
428 * the elf _start routine. The first entry in the function
429 * descriptor is the entry address of _start and the second
430 * entry is the TOC value we need to use.
433 __get_user(entry
, (unsigned long __user
*)fdptr
);
434 __get_user(toc
, (unsigned long __user
*)fdptr
+1);
436 /* Check whether the e_entry function descriptor entries
437 * need to be relocated before we can use them.
439 if (load_addr
!= 0) {
445 * If we exec out of a kernel thread then thread.regs will not be
448 if (!current
->thread
.regs
) {
449 unsigned long childregs
= (unsigned long)current
->thread_info
+
451 childregs
-= sizeof(struct pt_regs
);
452 current
->thread
.regs
= (struct pt_regs
*)childregs
;
458 regs
->msr
= MSR_USER64
;
460 if (last_task_used_math
== current
)
461 last_task_used_math
= 0;
462 #endif /* CONFIG_SMP */
463 memset(current
->thread
.fpr
, 0, sizeof(current
->thread
.fpr
));
464 current
->thread
.fpscr
= 0;
465 #ifdef CONFIG_ALTIVEC
467 if (last_task_used_altivec
== current
)
468 last_task_used_altivec
= 0;
469 #endif /* CONFIG_SMP */
470 memset(current
->thread
.vr
, 0, sizeof(current
->thread
.vr
));
471 current
->thread
.vscr
.u
[0] = 0;
472 current
->thread
.vscr
.u
[1] = 0;
473 current
->thread
.vscr
.u
[2] = 0;
474 current
->thread
.vscr
.u
[3] = 0x00010000; /* Java mode disabled */
475 current
->thread
.vrsave
= 0;
476 current
->thread
.used_vr
= 0;
477 #endif /* CONFIG_ALTIVEC */
479 EXPORT_SYMBOL(start_thread
);
481 int set_fpexc_mode(struct task_struct
*tsk
, unsigned int val
)
483 struct pt_regs
*regs
= tsk
->thread
.regs
;
485 if (val
> PR_FP_EXC_PRECISE
)
487 tsk
->thread
.fpexc_mode
= __pack_fe01(val
);
488 if (regs
!= NULL
&& (regs
->msr
& MSR_FP
) != 0)
489 regs
->msr
= (regs
->msr
& ~(MSR_FE0
|MSR_FE1
))
490 | tsk
->thread
.fpexc_mode
;
494 int get_fpexc_mode(struct task_struct
*tsk
, unsigned long adr
)
498 val
= __unpack_fe01(tsk
->thread
.fpexc_mode
);
499 return put_user(val
, (unsigned int __user
*) adr
);
502 int sys_clone(unsigned long clone_flags
, unsigned long p2
, unsigned long p3
,
503 unsigned long p4
, unsigned long p5
, unsigned long p6
,
504 struct pt_regs
*regs
)
506 unsigned long parent_tidptr
= 0;
507 unsigned long child_tidptr
= 0;
510 p2
= regs
->gpr
[1]; /* stack pointer for child */
512 if (clone_flags
& (CLONE_PARENT_SETTID
| CLONE_CHILD_SETTID
|
513 CLONE_CHILD_CLEARTID
)) {
516 if (test_thread_flag(TIF_32BIT
)) {
517 parent_tidptr
&= 0xffffffff;
518 child_tidptr
&= 0xffffffff;
522 return do_fork(clone_flags
, p2
, regs
, 0,
523 (int __user
*)parent_tidptr
, (int __user
*)child_tidptr
);
526 int sys_fork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
527 unsigned long p4
, unsigned long p5
, unsigned long p6
,
528 struct pt_regs
*regs
)
530 return do_fork(SIGCHLD
, regs
->gpr
[1], regs
, 0, NULL
, NULL
);
533 int sys_vfork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
534 unsigned long p4
, unsigned long p5
, unsigned long p6
,
535 struct pt_regs
*regs
)
537 return do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, regs
->gpr
[1], regs
, 0,
541 int sys_execve(unsigned long a0
, unsigned long a1
, unsigned long a2
,
542 unsigned long a3
, unsigned long a4
, unsigned long a5
,
543 struct pt_regs
*regs
)
548 filename
= getname((char __user
*) a0
);
549 error
= PTR_ERR(filename
);
550 if (IS_ERR(filename
))
552 flush_fp_to_thread(current
);
553 flush_altivec_to_thread(current
);
554 error
= do_execve(filename
, (char __user
* __user
*) a1
,
555 (char __user
* __user
*) a2
, regs
);
559 current
->ptrace
&= ~PT_DTRACE
;
560 task_unlock(current
);
568 static int kstack_depth_to_print
= 64;
570 static int validate_sp(unsigned long sp
, struct task_struct
*p
,
571 unsigned long nbytes
)
573 unsigned long stack_page
= (unsigned long)p
->thread_info
;
575 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
576 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
579 #ifdef CONFIG_IRQSTACKS
580 stack_page
= (unsigned long) hardirq_ctx
[task_cpu(p
)];
581 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
582 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
585 stack_page
= (unsigned long) softirq_ctx
[task_cpu(p
)];
586 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
587 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
594 unsigned long get_wchan(struct task_struct
*p
)
596 unsigned long ip
, sp
;
599 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
603 if (!validate_sp(sp
, p
, 112))
607 sp
= *(unsigned long *)sp
;
608 if (!validate_sp(sp
, p
, 112))
611 ip
= *(unsigned long *)(sp
+ 16);
612 if (!in_sched_functions(ip
))
615 } while (count
++ < 16);
618 EXPORT_SYMBOL(get_wchan
);
620 void show_stack(struct task_struct
*p
, unsigned long *_sp
)
622 unsigned long ip
, newsp
, lr
;
624 unsigned long sp
= (unsigned long)_sp
;
637 printk("Call Trace:\n");
639 if (!validate_sp(sp
, p
, 112))
642 _sp
= (unsigned long *) sp
;
645 if (!firstframe
|| ip
!= lr
) {
646 printk("[%016lx] [%016lx] ", sp
, ip
);
647 print_symbol("%s", ip
);
649 printk(" (unreliable)");
655 * See if this is an exception frame.
656 * We look for the "regshere" marker in the current frame.
658 if (validate_sp(sp
, p
, sizeof(struct pt_regs
) + 400)
659 && _sp
[12] == 0x7265677368657265ul
) {
660 struct pt_regs
*regs
= (struct pt_regs
*)
661 (sp
+ STACK_FRAME_OVERHEAD
);
662 printk("--- Exception: %lx", regs
->trap
);
663 print_symbol(" at %s\n", regs
->nip
);
665 print_symbol(" LR = %s\n", lr
);
670 } while (count
++ < kstack_depth_to_print
);
673 void dump_stack(void)
675 show_stack(current
, (unsigned long *)__get_SP());
677 EXPORT_SYMBOL(dump_stack
);