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,
68 void enable_kernel_fp(void)
71 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
74 giveup_fpu(NULL); /* just enables FP for kernel */
76 giveup_fpu(last_task_used_math);
77 #endif /* CONFIG_SMP */
79 EXPORT_SYMBOL(enable_kernel_fp);
81 int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
83 struct pt_regs *regs = tsk->thread.regs;
87 if (tsk == current && (regs->msr & MSR_FP))
90 memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
97 void enable_kernel_altivec(void)
100 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
101 giveup_altivec(current);
103 giveup_altivec(NULL); /* just enables FP for kernel */
105 giveup_altivec(last_task_used_altivec);
106 #endif /* CONFIG_SMP */
108 EXPORT_SYMBOL(enable_kernel_altivec);
110 int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
112 if (regs->msr & MSR_VEC)
113 giveup_altivec(current);
114 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
118 #endif /* CONFIG_ALTIVEC */
120 struct task_struct *__switch_to(struct task_struct *prev,
121 struct task_struct *new)
123 struct thread_struct *new_thread, *old_thread;
125 struct task_struct *last;
128 /* avoid complexity of lazy save/restore of fpu
129 * by just saving it every time we switch out if
130 * this task used the fpu during the last quantum.
132 * If it tries to use the fpu again, it'll trap and
133 * reload its fp regs. So we don't have to do a restore
134 * every switch, just a save.
137 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
139 #ifdef CONFIG_ALTIVEC
140 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
141 giveup_altivec(prev);
142 #endif /* CONFIG_ALTIVEC */
143 #endif /* CONFIG_SMP */
145 #if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
146 /* Avoid the trap. On smp this this never happens since
147 * we don't set last_task_used_altivec -- Cort
149 if (new->thread.regs && last_task_used_altivec == new)
150 new->thread.regs->msr |= MSR_VEC;
151 #endif /* CONFIG_ALTIVEC */
155 new_thread = &new->thread;
156 old_thread = ¤t->thread;
158 local_irq_save(flags);
159 last = _switch(old_thread, new_thread);
161 local_irq_restore(flags);
166 void show_regs(struct pt_regs * regs)
170 printk("NIP: %016lX XER: %016lX LR: %016lX\n",
171 regs->nip, regs->xer, regs->link);
172 printk("REGS: %p TRAP: %04lx %s (%s)\n",
173 regs, regs->trap, print_tainted(), UTS_RELEASE);
174 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
175 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
176 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
177 regs->msr&MSR_IR ? 1 : 0,
178 regs->msr&MSR_DR ? 1 : 0);
179 if (regs->trap == 0x300 || regs->trap == 0x380 || regs->trap == 0x600)
180 printk("DAR: %016lx, DSISR: %016lx\n", regs->dar, regs->dsisr);
181 printk("TASK: %p[%d] '%s' THREAD: %p",
182 current, current->pid, current->comm, current->thread_info);
185 printk(" CPU: %d", smp_processor_id());
186 #endif /* CONFIG_SMP */
188 for (i = 0; i < 32; i++) {
190 printk("\n" KERN_INFO "GPR%02d: ", i);
193 printk("%016lX ", regs->gpr[i]);
197 * Lookup NIP late so we have the best change of getting the
198 * above info out without failing
200 printk("NIP [%016lx] ", regs->nip);
201 print_symbol("%s\n", regs->nip);
202 printk("LR [%016lx] ", regs->link);
203 print_symbol("%s\n", regs->link);
204 show_stack(current, (unsigned long *)regs->gpr[1]);
207 void exit_thread(void)
210 if (last_task_used_math == current)
211 last_task_used_math = NULL;
212 #ifdef CONFIG_ALTIVEC
213 if (last_task_used_altivec == current)
214 last_task_used_altivec = NULL;
215 #endif /* CONFIG_ALTIVEC */
216 #endif /* CONFIG_SMP */
219 void flush_thread(void)
221 struct thread_info *t = current_thread_info();
223 if (t->flags & _TIF_ABI_PENDING)
224 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
227 if (last_task_used_math == current)
228 last_task_used_math = NULL;
229 #ifdef CONFIG_ALTIVEC
230 if (last_task_used_altivec == current)
231 last_task_used_altivec = NULL;
232 #endif /* CONFIG_ALTIVEC */
233 #endif /* CONFIG_SMP */
237 release_thread(struct task_struct *t)
243 * This gets called before we allocate a new thread and copy
244 * the current task into it.
246 void prepare_to_copy(struct task_struct *tsk)
248 struct pt_regs *regs = tsk->thread.regs;
252 if (regs->msr & MSR_FP)
254 #ifdef CONFIG_ALTIVEC
255 if (regs->msr & MSR_VEC)
256 giveup_altivec(current);
257 #endif /* CONFIG_ALTIVEC */
264 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
265 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
267 struct pt_regs *childregs, *kregs;
268 extern void ret_from_fork(void);
269 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
271 p->set_child_tid = p->clear_child_tid = NULL;
274 sp -= sizeof(struct pt_regs);
275 childregs = (struct pt_regs *) sp;
277 if ((childregs->msr & MSR_PR) == 0) {
278 /* for kernel thread, set stackptr in new task */
279 childregs->gpr[1] = sp + sizeof(struct pt_regs);
280 p->thread.regs = NULL; /* no user register state */
281 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
282 #ifdef CONFIG_PPC_ISERIES
283 set_ti_thread_flag(p->thread_info, TIF_RUN_LIGHT);
286 childregs->gpr[1] = usp;
287 p->thread.regs = childregs;
288 if (clone_flags & CLONE_SETTLS) {
289 if (test_thread_flag(TIF_32BIT))
290 childregs->gpr[2] = childregs->gpr[6];
292 childregs->gpr[13] = childregs->gpr[6];
295 childregs->gpr[3] = 0; /* Result from fork() */
296 sp -= STACK_FRAME_OVERHEAD;
299 * The way this works is that at some point in the future
300 * some task will call _switch to switch to the new task.
301 * That will pop off the stack frame created below and start
302 * the new task running at ret_from_fork. The new task will
303 * do some house keeping and then return from the fork or clone
304 * system call, using the stack frame created above.
306 sp -= sizeof(struct pt_regs);
307 kregs = (struct pt_regs *) sp;
308 sp -= STACK_FRAME_OVERHEAD;
312 * The PPC64 ABI makes use of a TOC to contain function
313 * pointers. The function (ret_from_except) is actually a pointer
314 * to the TOC entry. The first entry is a pointer to the actual
317 kregs->nip = *((unsigned long *)ret_from_fork);
323 * Set up a thread for executing a new program
325 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
327 unsigned long entry, toc, load_addr = regs->gpr[2];
329 /* fdptr is a relocated pointer to the function descriptor for
330 * the elf _start routine. The first entry in the function
331 * descriptor is the entry address of _start and the second
332 * entry is the TOC value we need to use.
335 __get_user(entry, (unsigned long *)fdptr);
336 __get_user(toc, (unsigned long *)fdptr+1);
338 /* Check whether the e_entry function descriptor entries
339 * need to be relocated before we can use them.
341 if ( load_addr != 0 ) {
349 regs->msr = MSR_USER64;
351 if (last_task_used_math == current)
352 last_task_used_math = 0;
353 #endif /* CONFIG_SMP */
354 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
355 current->thread.fpscr = 0;
356 #ifdef CONFIG_ALTIVEC
358 if (last_task_used_altivec == current)
359 last_task_used_altivec = 0;
360 #endif /* CONFIG_SMP */
361 memset(current->thread.vr, 0, sizeof(current->thread.vr));
362 current->thread.vscr.u[0] = 0;
363 current->thread.vscr.u[1] = 0;
364 current->thread.vscr.u[2] = 0;
365 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
366 current->thread.vrsave = 0;
367 current->thread.used_vr = 0;
368 #endif /* CONFIG_ALTIVEC */
371 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
373 struct pt_regs *regs = tsk->thread.regs;
375 if (val > PR_FP_EXC_PRECISE)
377 tsk->thread.fpexc_mode = __pack_fe01(val);
378 if (regs != NULL && (regs->msr & MSR_FP) != 0)
379 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
380 | tsk->thread.fpexc_mode;
384 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
388 val = __unpack_fe01(tsk->thread.fpexc_mode);
389 return put_user(val, (unsigned int *) adr);
392 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
393 unsigned long p4, unsigned long p5, unsigned long p6,
394 struct pt_regs *regs)
396 unsigned long parent_tidptr = 0;
397 unsigned long child_tidptr = 0;
400 p2 = regs->gpr[1]; /* stack pointer for child */
402 if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
403 CLONE_CHILD_CLEARTID)) {
406 if (test_thread_flag(TIF_32BIT)) {
407 parent_tidptr &= 0xffffffff;
408 child_tidptr &= 0xffffffff;
412 return do_fork(clone_flags & ~CLONE_IDLETASK, p2, regs, 0,
413 (int *)parent_tidptr, (int *)child_tidptr);
416 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
417 unsigned long p4, unsigned long p5, unsigned long p6,
418 struct pt_regs *regs)
420 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
423 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
424 unsigned long p4, unsigned long p5, unsigned long p6,
425 struct pt_regs *regs)
427 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
431 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
432 unsigned long a3, unsigned long a4, unsigned long a5,
433 struct pt_regs *regs)
438 filename = getname((char *) a0);
439 error = PTR_ERR(filename);
440 if (IS_ERR(filename))
442 if (regs->msr & MSR_FP)
444 #ifdef CONFIG_ALTIVEC
445 if (regs->msr & MSR_VEC)
446 giveup_altivec(current);
447 #endif /* CONFIG_ALTIVEC */
448 error = do_execve(filename, (char **) a1, (char **) a2, regs);
451 current->ptrace &= ~PT_DTRACE;
458 static int kstack_depth_to_print = 64;
460 static inline int validate_sp(unsigned long sp, struct task_struct *p)
462 unsigned long stack_page = (unsigned long)p->thread_info;
464 if (sp < stack_page + sizeof(struct thread_struct))
466 if (sp >= stack_page + THREAD_SIZE)
473 * These bracket the sleeping functions..
475 #define first_sched (*(unsigned long *)scheduling_functions_start_here)
476 #define last_sched (*(unsigned long *)scheduling_functions_end_here)
478 unsigned long get_wchan(struct task_struct *p)
480 unsigned long ip, sp;
483 if (!p || p == current || p->state == TASK_RUNNING)
487 if (!validate_sp(sp, p))
491 sp = *(unsigned long *)sp;
492 if (!validate_sp(sp, p))
495 ip = *(unsigned long *)(sp + 16);
496 if (ip < first_sched || ip >= last_sched)
499 } while (count++ < 16);
503 void show_stack(struct task_struct *p, unsigned long *_sp)
507 unsigned long sp = (unsigned long)_sp;
518 if (!validate_sp(sp, p))
521 printk("Call Trace:\n");
523 sp = *(unsigned long *)sp;
524 if (!validate_sp(sp, p))
526 ip = *(unsigned long *)(sp + 16);
527 printk("[%016lx] ", ip);
528 print_symbol("%s\n", ip);
529 } while (count++ < kstack_depth_to_print);
532 void dump_stack(void)
534 show_stack(current, (unsigned long *)__get_SP());
536 EXPORT_SYMBOL(dump_stack);