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/version.h>
39 #include <asm/pgtable.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
43 #include <asm/processor.h>
45 #include <asm/mmu_context.h>
47 #include <asm/ppcdebug.h>
48 #include <asm/machdep.h>
49 #include <asm/iSeries/HvCallHpt.h>
50 #include <asm/hardirq.h>
51 #include <asm/cputable.h>
52 #include <asm/sections.h>
53 #include <asm/tlbflush.h>
56 struct task_struct *last_task_used_math = NULL;
57 struct task_struct *last_task_used_altivec = NULL;
60 struct mm_struct ioremap_mm = {
62 .mm_users = ATOMIC_INIT(2),
63 .mm_count = ATOMIC_INIT(1),
64 .cpu_vm_mask = CPU_MASK_ALL,
65 .page_table_lock = SPIN_LOCK_UNLOCKED,
69 * Make sure the floating-point register state in the
70 * the thread_struct is up to date for task tsk.
72 void flush_fp_to_thread(struct task_struct *tsk)
74 if (tsk->thread.regs) {
76 * We need to disable preemption here because if we didn't,
77 * another process could get scheduled after the regs->msr
78 * test but before we have finished saving the FP registers
79 * to the thread_struct. That process could take over the
80 * FPU, and then when we get scheduled again we would store
81 * bogus values for the remaining FP registers.
84 if (tsk->thread.regs->msr & MSR_FP) {
87 * This should only ever be called for current or
88 * for a stopped child process. Since we save away
89 * the FP register state on context switch on SMP,
90 * there is something wrong if a stopped child appears
91 * to still have its FP state in the CPU registers.
93 BUG_ON(tsk != current);
101 void enable_kernel_fp(void)
103 WARN_ON(preemptible());
106 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
109 giveup_fpu(NULL); /* just enables FP for kernel */
111 giveup_fpu(last_task_used_math);
112 #endif /* CONFIG_SMP */
114 EXPORT_SYMBOL(enable_kernel_fp);
116 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
118 if (!tsk->thread.regs)
120 flush_fp_to_thread(current);
122 memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
127 #ifdef CONFIG_ALTIVEC
129 void enable_kernel_altivec(void)
131 WARN_ON(preemptible());
134 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
135 giveup_altivec(current);
137 giveup_altivec(NULL); /* just enables FP for kernel */
139 giveup_altivec(last_task_used_altivec);
140 #endif /* CONFIG_SMP */
142 EXPORT_SYMBOL(enable_kernel_altivec);
145 * Make sure the VMX/Altivec register state in the
146 * the thread_struct is up to date for task tsk.
148 void flush_altivec_to_thread(struct task_struct *tsk)
150 #ifdef CONFIG_ALTIVEC
151 if (tsk->thread.regs) {
153 if (tsk->thread.regs->msr & MSR_VEC) {
155 BUG_ON(tsk != current);
157 giveup_altivec(current);
164 int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
166 flush_altivec_to_thread(current);
167 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
171 #endif /* CONFIG_ALTIVEC */
173 struct task_struct *__switch_to(struct task_struct *prev,
174 struct task_struct *new)
176 struct thread_struct *new_thread, *old_thread;
178 struct task_struct *last;
181 /* avoid complexity of lazy save/restore of fpu
182 * by just saving it every time we switch out if
183 * this task used the fpu during the last quantum.
185 * If it tries to use the fpu again, it'll trap and
186 * reload its fp regs. So we don't have to do a restore
187 * every switch, just a save.
190 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
192 #ifdef CONFIG_ALTIVEC
193 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
194 giveup_altivec(prev);
195 #endif /* CONFIG_ALTIVEC */
196 #endif /* CONFIG_SMP */
198 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
199 /* Avoid the trap. On smp this this never happens since
200 * we don't set last_task_used_altivec -- Cort
202 if (new->thread.regs && last_task_used_altivec == new)
203 new->thread.regs->msr |= MSR_VEC;
204 #endif /* CONFIG_ALTIVEC */
208 new_thread = &new->thread;
209 old_thread = ¤t->thread;
211 local_irq_save(flags);
212 last = _switch(old_thread, new_thread);
214 local_irq_restore(flags);
219 void show_regs(struct pt_regs * regs)
224 printk("NIP: %016lX XER: %016lX LR: %016lX\n",
225 regs->nip, regs->xer, regs->link);
226 printk("REGS: %p TRAP: %04lx %s (%s)\n",
227 regs, regs->trap, print_tainted(), UTS_RELEASE);
228 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
229 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
230 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
231 regs->msr&MSR_IR ? 1 : 0,
232 regs->msr&MSR_DR ? 1 : 0);
234 if (trap == 0x300 || trap == 0x380 || trap == 0x600)
235 printk("DAR: %016lx, DSISR: %016lx\n", regs->dar, regs->dsisr);
236 printk("TASK: %p[%d] '%s' THREAD: %p",
237 current, current->pid, current->comm, current->thread_info);
240 printk(" CPU: %d", smp_processor_id());
241 #endif /* CONFIG_SMP */
243 for (i = 0; i < 32; i++) {
245 printk("\n" KERN_INFO "GPR%02d: ", i);
248 printk("%016lX ", regs->gpr[i]);
249 if (i == 13 && !FULL_REGS(regs))
254 * Lookup NIP late so we have the best change of getting the
255 * above info out without failing
257 printk("NIP [%016lx] ", regs->nip);
258 print_symbol("%s\n", regs->nip);
259 printk("LR [%016lx] ", regs->link);
260 print_symbol("%s\n", regs->link);
261 show_stack(current, (unsigned long *)regs->gpr[1]);
264 void exit_thread(void)
267 if (last_task_used_math == current)
268 last_task_used_math = NULL;
269 #ifdef CONFIG_ALTIVEC
270 if (last_task_used_altivec == current)
271 last_task_used_altivec = NULL;
272 #endif /* CONFIG_ALTIVEC */
273 #endif /* CONFIG_SMP */
276 void flush_thread(void)
278 struct thread_info *t = current_thread_info();
280 if (t->flags & _TIF_ABI_PENDING)
281 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
284 if (last_task_used_math == current)
285 last_task_used_math = NULL;
286 #ifdef CONFIG_ALTIVEC
287 if (last_task_used_altivec == current)
288 last_task_used_altivec = NULL;
289 #endif /* CONFIG_ALTIVEC */
290 #endif /* CONFIG_SMP */
294 release_thread(struct task_struct *t)
300 * This gets called before we allocate a new thread and copy
301 * the current task into it.
303 void prepare_to_copy(struct task_struct *tsk)
305 flush_fp_to_thread(current);
306 flush_altivec_to_thread(current);
313 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
314 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
316 struct pt_regs *childregs, *kregs;
317 extern void ret_from_fork(void);
318 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
320 p->set_child_tid = p->clear_child_tid = NULL;
323 sp -= sizeof(struct pt_regs);
324 childregs = (struct pt_regs *) sp;
326 if ((childregs->msr & MSR_PR) == 0) {
327 /* for kernel thread, set stackptr in new task */
328 childregs->gpr[1] = sp + sizeof(struct pt_regs);
329 p->thread.regs = NULL; /* no user register state */
330 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
331 #ifdef CONFIG_PPC_ISERIES
332 set_ti_thread_flag(p->thread_info, TIF_RUN_LIGHT);
335 childregs->gpr[1] = usp;
336 p->thread.regs = childregs;
337 if (clone_flags & CLONE_SETTLS) {
338 if (test_thread_flag(TIF_32BIT))
339 childregs->gpr[2] = childregs->gpr[6];
341 childregs->gpr[13] = childregs->gpr[6];
344 childregs->gpr[3] = 0; /* Result from fork() */
345 sp -= STACK_FRAME_OVERHEAD;
348 * The way this works is that at some point in the future
349 * some task will call _switch to switch to the new task.
350 * That will pop off the stack frame created below and start
351 * the new task running at ret_from_fork. The new task will
352 * do some house keeping and then return from the fork or clone
353 * system call, using the stack frame created above.
355 sp -= sizeof(struct pt_regs);
356 kregs = (struct pt_regs *) sp;
357 sp -= STACK_FRAME_OVERHEAD;
361 * The PPC64 ABI makes use of a TOC to contain function
362 * pointers. The function (ret_from_except) is actually a pointer
363 * to the TOC entry. The first entry is a pointer to the actual
366 kregs->nip = *((unsigned long *)ret_from_fork);
372 * Set up a thread for executing a new program
374 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
376 unsigned long entry, toc, load_addr = regs->gpr[2];
378 /* fdptr is a relocated pointer to the function descriptor for
379 * the elf _start routine. The first entry in the function
380 * descriptor is the entry address of _start and the second
381 * entry is the TOC value we need to use.
384 __get_user(entry, (unsigned long __user *)fdptr);
385 __get_user(toc, (unsigned long __user *)fdptr+1);
387 /* Check whether the e_entry function descriptor entries
388 * need to be relocated before we can use them.
390 if ( load_addr != 0 ) {
398 regs->msr = MSR_USER64;
400 if (last_task_used_math == current)
401 last_task_used_math = 0;
402 #endif /* CONFIG_SMP */
403 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
404 current->thread.fpscr = 0;
405 #ifdef CONFIG_ALTIVEC
407 if (last_task_used_altivec == current)
408 last_task_used_altivec = 0;
409 #endif /* CONFIG_SMP */
410 memset(current->thread.vr, 0, sizeof(current->thread.vr));
411 current->thread.vscr.u[0] = 0;
412 current->thread.vscr.u[1] = 0;
413 current->thread.vscr.u[2] = 0;
414 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
415 current->thread.vrsave = 0;
416 current->thread.used_vr = 0;
417 #endif /* CONFIG_ALTIVEC */
420 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
422 struct pt_regs *regs = tsk->thread.regs;
424 if (val > PR_FP_EXC_PRECISE)
426 tsk->thread.fpexc_mode = __pack_fe01(val);
427 if (regs != NULL && (regs->msr & MSR_FP) != 0)
428 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
429 | tsk->thread.fpexc_mode;
433 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
437 val = __unpack_fe01(tsk->thread.fpexc_mode);
438 return put_user(val, (unsigned int __user *) adr);
441 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
442 unsigned long p4, unsigned long p5, unsigned long p6,
443 struct pt_regs *regs)
445 unsigned long parent_tidptr = 0;
446 unsigned long child_tidptr = 0;
449 p2 = regs->gpr[1]; /* stack pointer for child */
451 if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
452 CLONE_CHILD_CLEARTID)) {
455 if (test_thread_flag(TIF_32BIT)) {
456 parent_tidptr &= 0xffffffff;
457 child_tidptr &= 0xffffffff;
461 return do_fork(clone_flags & ~CLONE_IDLETASK, p2, regs, 0,
462 (int __user *)parent_tidptr, (int __user *)child_tidptr);
465 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
466 unsigned long p4, unsigned long p5, unsigned long p6,
467 struct pt_regs *regs)
469 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
472 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
473 unsigned long p4, unsigned long p5, unsigned long p6,
474 struct pt_regs *regs)
476 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
480 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
481 unsigned long a3, unsigned long a4, unsigned long a5,
482 struct pt_regs *regs)
487 filename = getname((char __user *) a0);
488 error = PTR_ERR(filename);
489 if (IS_ERR(filename))
491 flush_fp_to_thread(current);
492 flush_altivec_to_thread(current);
493 error = do_execve(filename, (char __user * __user *) a1,
494 (char __user * __user *) a2, regs);
497 current->ptrace &= ~PT_DTRACE;
504 static int kstack_depth_to_print = 64;
506 static int validate_sp(unsigned long sp, struct task_struct *p,
507 unsigned long nbytes)
509 unsigned long stack_page = (unsigned long)p->thread_info;
511 if (sp >= stack_page + sizeof(struct thread_struct)
512 && sp <= stack_page + THREAD_SIZE - nbytes)
515 #ifdef CONFIG_IRQSTACKS
516 stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
517 if (sp >= stack_page + sizeof(struct thread_struct)
518 && sp <= stack_page + THREAD_SIZE - nbytes)
521 stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
522 if (sp >= stack_page + sizeof(struct thread_struct)
523 && sp <= stack_page + THREAD_SIZE - nbytes)
530 unsigned long get_wchan(struct task_struct *p)
532 unsigned long ip, sp;
535 if (!p || p == current || p->state == TASK_RUNNING)
539 if (!validate_sp(sp, p, 112))
543 sp = *(unsigned long *)sp;
544 if (!validate_sp(sp, p, 112))
547 ip = *(unsigned long *)(sp + 16);
548 if (!in_sched_functions(ip))
551 } while (count++ < 16);
555 void show_stack(struct task_struct *p, unsigned long *_sp)
557 unsigned long ip, newsp, lr;
559 unsigned long sp = (unsigned long)_sp;
572 printk("Call Trace:\n");
574 if (!validate_sp(sp, p, 112))
577 _sp = (unsigned long *) sp;
580 if (!firstframe || ip != lr) {
581 printk("[%016lx] [%016lx] ", sp, ip);
582 print_symbol("%s", ip);
584 printk(" (unreliable)");
590 * See if this is an exception frame.
591 * We look for the "regshere" marker in the current frame.
593 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
594 && _sp[12] == 0x7265677368657265ul) {
595 struct pt_regs *regs = (struct pt_regs *)
596 (sp + STACK_FRAME_OVERHEAD);
597 printk("--- Exception: %lx", regs->trap);
598 print_symbol(" at %s\n", regs->nip);
600 print_symbol(" LR = %s\n", lr);
605 } while (count++ < kstack_depth_to_print);
608 void dump_stack(void)
610 show_stack(current, (unsigned long *)__get_SP());
612 EXPORT_SYMBOL(dump_stack);