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
;
61 struct mm_struct ioremap_mm
= {
63 .mm_users
= ATOMIC_INIT(2),
64 .mm_count
= ATOMIC_INIT(1),
65 .cpu_vm_mask
= CPU_MASK_ALL
,
66 .page_table_lock
= SPIN_LOCK_UNLOCKED
,
70 * Make sure the floating-point register state in the
71 * the thread_struct is up to date for task tsk.
73 void flush_fp_to_thread(struct task_struct
*tsk
)
75 if (tsk
->thread
.regs
) {
77 * We need to disable preemption here because if we didn't,
78 * another process could get scheduled after the regs->msr
79 * test but before we have finished saving the FP registers
80 * to the thread_struct. That process could take over the
81 * FPU, and then when we get scheduled again we would store
82 * bogus values for the remaining FP registers.
85 if (tsk
->thread
.regs
->msr
& MSR_FP
) {
88 * This should only ever be called for current or
89 * for a stopped child process. Since we save away
90 * the FP register state on context switch on SMP,
91 * there is something wrong if a stopped child appears
92 * to still have its FP state in the CPU registers.
94 BUG_ON(tsk
!= current
);
102 void enable_kernel_fp(void)
104 WARN_ON(preemptible());
107 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_FP
))
110 giveup_fpu(NULL
); /* just enables FP for kernel */
112 giveup_fpu(last_task_used_math
);
113 #endif /* CONFIG_SMP */
115 EXPORT_SYMBOL(enable_kernel_fp
);
117 int dump_task_fpu(struct task_struct
*tsk
, elf_fpregset_t
*fpregs
)
119 if (!tsk
->thread
.regs
)
121 flush_fp_to_thread(current
);
123 memcpy(fpregs
, &tsk
->thread
.fpr
[0], sizeof(*fpregs
));
128 #ifdef CONFIG_ALTIVEC
130 void enable_kernel_altivec(void)
132 WARN_ON(preemptible());
135 if (current
->thread
.regs
&& (current
->thread
.regs
->msr
& MSR_VEC
))
136 giveup_altivec(current
);
138 giveup_altivec(NULL
); /* just enables FP for kernel */
140 giveup_altivec(last_task_used_altivec
);
141 #endif /* CONFIG_SMP */
143 EXPORT_SYMBOL(enable_kernel_altivec
);
146 * Make sure the VMX/Altivec register state in the
147 * the thread_struct is up to date for task tsk.
149 void flush_altivec_to_thread(struct task_struct
*tsk
)
151 if (tsk
->thread
.regs
) {
153 if (tsk
->thread
.regs
->msr
& MSR_VEC
) {
155 BUG_ON(tsk
!= current
);
157 giveup_altivec(current
);
163 int dump_task_altivec(struct pt_regs
*regs
, elf_vrregset_t
*vrregs
)
165 flush_altivec_to_thread(current
);
166 memcpy(vrregs
, ¤t
->thread
.vr
[0], sizeof(*vrregs
));
170 #endif /* CONFIG_ALTIVEC */
172 DEFINE_PER_CPU(struct cpu_usage
, cpu_usage_array
);
174 struct task_struct
*__switch_to(struct task_struct
*prev
,
175 struct task_struct
*new)
177 struct thread_struct
*new_thread
, *old_thread
;
179 struct task_struct
*last
;
182 /* avoid complexity of lazy save/restore of fpu
183 * by just saving it every time we switch out if
184 * this task used the fpu during the last quantum.
186 * If it tries to use the fpu again, it'll trap and
187 * reload its fp regs. So we don't have to do a restore
188 * every switch, just a save.
191 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_FP
))
193 #ifdef CONFIG_ALTIVEC
194 if (prev
->thread
.regs
&& (prev
->thread
.regs
->msr
& MSR_VEC
))
195 giveup_altivec(prev
);
196 #endif /* CONFIG_ALTIVEC */
197 #endif /* CONFIG_SMP */
199 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
200 /* Avoid the trap. On smp this this never happens since
201 * we don't set last_task_used_altivec -- Cort
203 if (new->thread
.regs
&& last_task_used_altivec
== new)
204 new->thread
.regs
->msr
|= MSR_VEC
;
205 #endif /* CONFIG_ALTIVEC */
209 new_thread
= &new->thread
;
210 old_thread
= ¤t
->thread
;
212 /* Collect purr utilization data per process and per processor wise */
213 /* purr is nothing but processor time base */
215 #if defined(CONFIG_PPC_PSERIES)
216 if (cur_cpu_spec
->firmware_features
& FW_FEATURE_SPLPAR
) {
217 struct cpu_usage
*cu
= &__get_cpu_var(cpu_usage_array
);
218 long unsigned start_tb
, current_tb
;
219 start_tb
= old_thread
->start_tb
;
220 cu
->current_tb
= current_tb
= mfspr(SPRN_PURR
);
221 old_thread
->accum_tb
+= (current_tb
- start_tb
);
222 new_thread
->start_tb
= current_tb
;
227 local_irq_save(flags
);
228 last
= _switch(old_thread
, new_thread
);
230 local_irq_restore(flags
);
235 static int instructions_to_print
= 16;
237 static void show_instructions(struct pt_regs
*regs
)
240 unsigned long pc
= regs
->nip
- (instructions_to_print
* 3 / 4 *
243 printk("Instruction dump:");
245 for (i
= 0; i
< instructions_to_print
; i
++) {
251 if (((REGION_ID(pc
) != KERNEL_REGION_ID
) &&
252 (REGION_ID(pc
) != VMALLOC_REGION_ID
)) ||
253 __get_user(instr
, (unsigned int *)pc
)) {
257 printk("<%08x> ", instr
);
259 printk("%08x ", instr
);
268 void show_regs(struct pt_regs
* regs
)
273 printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
274 regs
->nip
, (unsigned int)regs
->xer
, regs
->link
, regs
->ctr
);
275 printk("REGS: %p TRAP: %04lx %s (%s)\n",
276 regs
, regs
->trap
, print_tainted(), system_utsname
.release
);
277 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
278 "IR/DR: %01x%01x CR: %08X\n",
279 regs
->msr
, regs
->msr
&MSR_EE
? 1 : 0, regs
->msr
&MSR_PR
? 1 : 0,
280 regs
->msr
& MSR_FP
? 1 : 0,regs
->msr
&MSR_ME
? 1 : 0,
281 regs
->msr
&MSR_IR
? 1 : 0,
282 regs
->msr
&MSR_DR
? 1 : 0,
283 (unsigned int)regs
->ccr
);
285 printk("DAR: %016lx DSISR: %016lx\n", regs
->dar
, regs
->dsisr
);
286 printk("TASK: %p[%d] '%s' THREAD: %p",
287 current
, current
->pid
, current
->comm
, current
->thread_info
);
290 printk(" CPU: %d", smp_processor_id());
291 #endif /* CONFIG_SMP */
293 for (i
= 0; i
< 32; i
++) {
295 printk("\n" KERN_INFO
"GPR%02d: ", i
);
298 printk("%016lX ", regs
->gpr
[i
]);
299 if (i
== 13 && !FULL_REGS(regs
))
304 * Lookup NIP late so we have the best change of getting the
305 * above info out without failing
307 printk("NIP [%016lx] ", regs
->nip
);
308 print_symbol("%s\n", regs
->nip
);
309 printk("LR [%016lx] ", regs
->link
);
310 print_symbol("%s\n", regs
->link
);
311 show_stack(current
, (unsigned long *)regs
->gpr
[1]);
312 if (!user_mode(regs
))
313 show_instructions(regs
);
316 void exit_thread(void)
319 if (last_task_used_math
== current
)
320 last_task_used_math
= NULL
;
321 #ifdef CONFIG_ALTIVEC
322 if (last_task_used_altivec
== current
)
323 last_task_used_altivec
= NULL
;
324 #endif /* CONFIG_ALTIVEC */
325 #endif /* CONFIG_SMP */
328 void flush_thread(void)
330 struct thread_info
*t
= current_thread_info();
332 if (t
->flags
& _TIF_ABI_PENDING
)
333 t
->flags
^= (_TIF_ABI_PENDING
| _TIF_32BIT
);
336 if (last_task_used_math
== current
)
337 last_task_used_math
= NULL
;
338 #ifdef CONFIG_ALTIVEC
339 if (last_task_used_altivec
== current
)
340 last_task_used_altivec
= NULL
;
341 #endif /* CONFIG_ALTIVEC */
342 #endif /* CONFIG_SMP */
346 release_thread(struct task_struct
*t
)
352 * This gets called before we allocate a new thread and copy
353 * the current task into it.
355 void prepare_to_copy(struct task_struct
*tsk
)
357 flush_fp_to_thread(current
);
358 flush_altivec_to_thread(current
);
365 copy_thread(int nr
, unsigned long clone_flags
, unsigned long usp
,
366 unsigned long unused
, struct task_struct
*p
, struct pt_regs
*regs
)
368 struct pt_regs
*childregs
, *kregs
;
369 extern void ret_from_fork(void);
370 unsigned long sp
= (unsigned long)p
->thread_info
+ THREAD_SIZE
;
373 sp
-= sizeof(struct pt_regs
);
374 childregs
= (struct pt_regs
*) sp
;
376 if ((childregs
->msr
& MSR_PR
) == 0) {
377 /* for kernel thread, set stackptr in new task */
378 childregs
->gpr
[1] = sp
+ sizeof(struct pt_regs
);
379 p
->thread
.regs
= NULL
; /* no user register state */
380 clear_ti_thread_flag(p
->thread_info
, TIF_32BIT
);
381 #ifdef CONFIG_PPC_ISERIES
382 set_ti_thread_flag(p
->thread_info
, TIF_RUN_LIGHT
);
385 childregs
->gpr
[1] = usp
;
386 p
->thread
.regs
= childregs
;
387 if (clone_flags
& CLONE_SETTLS
) {
388 if (test_thread_flag(TIF_32BIT
))
389 childregs
->gpr
[2] = childregs
->gpr
[6];
391 childregs
->gpr
[13] = childregs
->gpr
[6];
394 childregs
->gpr
[3] = 0; /* Result from fork() */
395 sp
-= STACK_FRAME_OVERHEAD
;
398 * The way this works is that at some point in the future
399 * some task will call _switch to switch to the new task.
400 * That will pop off the stack frame created below and start
401 * the new task running at ret_from_fork. The new task will
402 * do some house keeping and then return from the fork or clone
403 * system call, using the stack frame created above.
405 sp
-= sizeof(struct pt_regs
);
406 kregs
= (struct pt_regs
*) sp
;
407 sp
-= STACK_FRAME_OVERHEAD
;
409 if (cpu_has_feature(CPU_FTR_SLB
)) {
410 unsigned long sp_vsid
= get_kernel_vsid(sp
);
412 sp_vsid
<<= SLB_VSID_SHIFT
;
413 sp_vsid
|= SLB_VSID_KERNEL
;
414 if (cpu_has_feature(CPU_FTR_16M_PAGE
))
415 sp_vsid
|= SLB_VSID_L
;
417 p
->thread
.ksp_vsid
= sp_vsid
;
421 * The PPC64 ABI makes use of a TOC to contain function
422 * pointers. The function (ret_from_except) is actually a pointer
423 * to the TOC entry. The first entry is a pointer to the actual
426 kregs
->nip
= *((unsigned long *)ret_from_fork
);
432 * Set up a thread for executing a new program
434 void start_thread(struct pt_regs
*regs
, unsigned long fdptr
, unsigned long sp
)
436 unsigned long entry
, toc
, load_addr
= regs
->gpr
[2];
438 /* fdptr is a relocated pointer to the function descriptor for
439 * the elf _start routine. The first entry in the function
440 * descriptor is the entry address of _start and the second
441 * entry is the TOC value we need to use.
444 __get_user(entry
, (unsigned long __user
*)fdptr
);
445 __get_user(toc
, (unsigned long __user
*)fdptr
+1);
447 /* Check whether the e_entry function descriptor entries
448 * need to be relocated before we can use them.
450 if (load_addr
!= 0) {
456 * If we exec out of a kernel thread then thread.regs will not be
459 if (!current
->thread
.regs
) {
460 unsigned long childregs
= (unsigned long)current
->thread_info
+
462 childregs
-= sizeof(struct pt_regs
);
463 current
->thread
.regs
= (struct pt_regs
*)childregs
;
469 regs
->msr
= MSR_USER64
;
471 if (last_task_used_math
== current
)
472 last_task_used_math
= 0;
473 #endif /* CONFIG_SMP */
474 memset(current
->thread
.fpr
, 0, sizeof(current
->thread
.fpr
));
475 current
->thread
.fpscr
= 0;
476 #ifdef CONFIG_ALTIVEC
478 if (last_task_used_altivec
== current
)
479 last_task_used_altivec
= 0;
480 #endif /* CONFIG_SMP */
481 memset(current
->thread
.vr
, 0, sizeof(current
->thread
.vr
));
482 current
->thread
.vscr
.u
[0] = 0;
483 current
->thread
.vscr
.u
[1] = 0;
484 current
->thread
.vscr
.u
[2] = 0;
485 current
->thread
.vscr
.u
[3] = 0x00010000; /* Java mode disabled */
486 current
->thread
.vrsave
= 0;
487 current
->thread
.used_vr
= 0;
488 #endif /* CONFIG_ALTIVEC */
490 EXPORT_SYMBOL(start_thread
);
492 int set_fpexc_mode(struct task_struct
*tsk
, unsigned int val
)
494 struct pt_regs
*regs
= tsk
->thread
.regs
;
496 if (val
> PR_FP_EXC_PRECISE
)
498 tsk
->thread
.fpexc_mode
= __pack_fe01(val
);
499 if (regs
!= NULL
&& (regs
->msr
& MSR_FP
) != 0)
500 regs
->msr
= (regs
->msr
& ~(MSR_FE0
|MSR_FE1
))
501 | tsk
->thread
.fpexc_mode
;
505 int get_fpexc_mode(struct task_struct
*tsk
, unsigned long adr
)
509 val
= __unpack_fe01(tsk
->thread
.fpexc_mode
);
510 return put_user(val
, (unsigned int __user
*) adr
);
513 int sys_clone(unsigned long clone_flags
, unsigned long p2
, unsigned long p3
,
514 unsigned long p4
, unsigned long p5
, unsigned long p6
,
515 struct pt_regs
*regs
)
517 unsigned long parent_tidptr
= 0;
518 unsigned long child_tidptr
= 0;
521 p2
= regs
->gpr
[1]; /* stack pointer for child */
523 if (clone_flags
& (CLONE_PARENT_SETTID
| CLONE_CHILD_SETTID
|
524 CLONE_CHILD_CLEARTID
)) {
527 if (test_thread_flag(TIF_32BIT
)) {
528 parent_tidptr
&= 0xffffffff;
529 child_tidptr
&= 0xffffffff;
533 return do_fork(clone_flags
, p2
, regs
, 0,
534 (int __user
*)parent_tidptr
, (int __user
*)child_tidptr
);
537 int sys_fork(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(SIGCHLD
, regs
->gpr
[1], regs
, 0, NULL
, NULL
);
544 int sys_vfork(unsigned long p1
, unsigned long p2
, unsigned long p3
,
545 unsigned long p4
, unsigned long p5
, unsigned long p6
,
546 struct pt_regs
*regs
)
548 return do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, regs
->gpr
[1], regs
, 0,
552 int sys_execve(unsigned long a0
, unsigned long a1
, unsigned long a2
,
553 unsigned long a3
, unsigned long a4
, unsigned long a5
,
554 struct pt_regs
*regs
)
559 filename
= getname((char __user
*) a0
);
560 error
= PTR_ERR(filename
);
561 if (IS_ERR(filename
))
563 flush_fp_to_thread(current
);
564 flush_altivec_to_thread(current
);
565 error
= do_execve(filename
, (char __user
* __user
*) a1
,
566 (char __user
* __user
*) a2
, regs
);
570 current
->ptrace
&= ~PT_DTRACE
;
571 task_unlock(current
);
579 static int kstack_depth_to_print
= 64;
581 static int validate_sp(unsigned long sp
, struct task_struct
*p
,
582 unsigned long nbytes
)
584 unsigned long stack_page
= (unsigned long)p
->thread_info
;
586 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
587 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
590 #ifdef CONFIG_IRQSTACKS
591 stack_page
= (unsigned long) hardirq_ctx
[task_cpu(p
)];
592 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
593 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
596 stack_page
= (unsigned long) softirq_ctx
[task_cpu(p
)];
597 if (sp
>= stack_page
+ sizeof(struct thread_struct
)
598 && sp
<= stack_page
+ THREAD_SIZE
- nbytes
)
605 unsigned long get_wchan(struct task_struct
*p
)
607 unsigned long ip
, sp
;
610 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
614 if (!validate_sp(sp
, p
, 112))
618 sp
= *(unsigned long *)sp
;
619 if (!validate_sp(sp
, p
, 112))
622 ip
= *(unsigned long *)(sp
+ 16);
623 if (!in_sched_functions(ip
))
626 } while (count
++ < 16);
629 EXPORT_SYMBOL(get_wchan
);
631 void show_stack(struct task_struct
*p
, unsigned long *_sp
)
633 unsigned long ip
, newsp
, lr
;
635 unsigned long sp
= (unsigned long)_sp
;
648 printk("Call Trace:\n");
650 if (!validate_sp(sp
, p
, 112))
653 _sp
= (unsigned long *) sp
;
656 if (!firstframe
|| ip
!= lr
) {
657 printk("[%016lx] [%016lx] ", sp
, ip
);
658 print_symbol("%s", ip
);
660 printk(" (unreliable)");
666 * See if this is an exception frame.
667 * We look for the "regshere" marker in the current frame.
669 if (validate_sp(sp
, p
, sizeof(struct pt_regs
) + 400)
670 && _sp
[12] == 0x7265677368657265ul
) {
671 struct pt_regs
*regs
= (struct pt_regs
*)
672 (sp
+ STACK_FRAME_OVERHEAD
);
673 printk("--- Exception: %lx", regs
->trap
);
674 print_symbol(" at %s\n", regs
->nip
);
676 print_symbol(" LR = %s\n", lr
);
681 } while (count
++ < kstack_depth_to_print
);
684 void dump_stack(void)
686 show_stack(current
, (unsigned long *)__get_SP());
688 EXPORT_SYMBOL(dump_stack
);