Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / arch / ppc64 / kernel / process.c
blobda4fbd31a909f6d3c6cbdd1450ad5a249afcd710
1 /*
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)
10 * PowerPC version
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/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/slab.h>
29 #include <linux/user.h>
30 #include <linux/elf.h>
31 #include <linux/init.h>
32 #include <linux/init_task.h>
33 #include <linux/prctl.h>
35 #include <asm/pgtable.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
38 #include <asm/io.h>
39 #include <asm/processor.h>
40 #include <asm/mmu.h>
41 #include <asm/mmu_context.h>
42 #include <asm/prom.h>
43 #include <asm/ppcdebug.h>
44 #include <asm/machdep.h>
45 #include <asm/iSeries/HvCallHpt.h>
46 #include <asm/hardirq.h>
48 struct task_struct *last_task_used_math = NULL;
50 struct mm_struct ioremap_mm = { pgd : ioremap_dir
51 ,page_table_lock : SPIN_LOCK_UNLOCKED };
53 char *sysmap = NULL;
54 unsigned long sysmap_size = 0;
56 void
57 enable_kernel_fp(void)
59 #ifdef CONFIG_SMP
60 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
61 giveup_fpu(current);
62 else
63 giveup_fpu(NULL); /* just enables FP for kernel */
64 #else
65 giveup_fpu(last_task_used_math);
66 #endif /* CONFIG_SMP */
69 int
70 dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
73 * XXX temporary workaround until threaded coredumps for ppc64
74 * are implemented - Anton
76 if (!regs)
77 return 0;
78 if (regs->msr & MSR_FP)
79 giveup_fpu(current);
80 memcpy(fpregs, &current->thread.fpr[0], sizeof(*fpregs));
81 return 1;
84 void
85 __switch_to(struct task_struct *prev, struct task_struct *new)
87 struct thread_struct *new_thread, *old_thread;
88 unsigned long flags;
90 #ifdef CONFIG_SMP
91 /* avoid complexity of lazy save/restore of fpu
92 * by just saving it every time we switch out if
93 * this task used the fpu during the last quantum.
95 * If it tries to use the fpu again, it'll trap and
96 * reload its fp regs. So we don't have to do a restore
97 * every switch, just a save.
98 * -- Cort
100 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
101 giveup_fpu(prev);
102 #endif /* CONFIG_SMP */
104 new_thread = &new->thread;
105 old_thread = &current->thread;
107 local_irq_save(flags);
108 _switch(old_thread, new_thread);
109 local_irq_restore(flags);
112 static void show_tsk_stack(struct task_struct *p, unsigned long sp);
113 static char *ppc_find_proc_name(unsigned *p, char *buf, unsigned buflen);
115 void show_regs(struct pt_regs * regs)
117 int i;
118 char name_buf[256];
120 printk("NIP: %016lX XER: %016lX LR: %016lX\n",
121 regs->nip, regs->xer, regs->link);
122 printk("REGS: %p TRAP: %04lx %s\n",
123 regs, regs->trap, print_tainted());
124 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
125 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
126 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
127 regs->msr&MSR_IR ? 1 : 0,
128 regs->msr&MSR_DR ? 1 : 0);
129 if (regs->trap == 0x300 || regs->trap == 0x380 || regs->trap == 0x600)
130 printk("DAR: %016lx, DSISR: %016lx\n", regs->dar, regs->dsisr);
131 printk("TASK = %p[%d] '%s' ",
132 current, current->pid, current->comm);
134 #ifdef CONFIG_SMP
135 printk(" CPU: %d", smp_processor_id());
136 #endif /* CONFIG_SMP */
138 for (i = 0; i < 32; i++) {
139 long r;
140 if ((i % 4) == 0) {
141 printk("\n" KERN_INFO "GPR%02d: ", i);
143 if (__get_user(r, &(regs->gpr[i])))
144 return;
145 printk("%016lX ", r);
147 printk("\n");
149 * Lookup NIP late so we have the best change of getting the
150 * above info out without failing
152 printk("NIP [%016lx] ", regs->nip);
153 printk("%s\n", ppc_find_proc_name((unsigned *)regs->nip,
154 name_buf, 256));
155 show_tsk_stack(current, regs->gpr[1]);
158 void exit_thread(void)
160 if (last_task_used_math == current)
161 last_task_used_math = NULL;
164 void flush_thread(void)
166 if (last_task_used_math == current)
167 last_task_used_math = NULL;
170 void
171 release_thread(struct task_struct *t)
176 * Copy a thread..
179 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
180 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
182 struct pt_regs *childregs, *kregs;
183 extern void ret_from_fork(void);
184 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
186 p->set_child_tid = p->clear_child_tid = NULL;
188 /* Copy registers */
189 sp -= sizeof(struct pt_regs);
190 childregs = (struct pt_regs *) sp;
191 *childregs = *regs;
192 if ((childregs->msr & MSR_PR) == 0) {
193 /* for kernel thread, set stackptr in new task */
194 childregs->gpr[1] = sp + sizeof(struct pt_regs);
195 p->thread.regs = NULL; /* no user register state */
196 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
197 #ifdef CONFIG_PPC_ISERIES
198 set_ti_thread_flag(p->thread_info, TIF_RUN_LIGHT);
199 #endif
200 } else {
201 childregs->gpr[1] = usp;
202 p->thread.regs = childregs;
204 childregs->gpr[3] = 0; /* Result from fork() */
205 sp -= STACK_FRAME_OVERHEAD;
208 * The way this works is that at some point in the future
209 * some task will call _switch to switch to the new task.
210 * That will pop off the stack frame created below and start
211 * the new task running at ret_from_fork. The new task will
212 * do some house keeping and then return from the fork or clone
213 * system call, using the stack frame created above.
215 sp -= sizeof(struct pt_regs);
216 kregs = (struct pt_regs *) sp;
217 sp -= STACK_FRAME_OVERHEAD;
218 p->thread.ksp = sp;
221 * The PPC64 ABI makes use of a TOC to contain function
222 * pointers. The function (ret_from_except) is actually a pointer
223 * to the TOC entry. The first entry is a pointer to the actual
224 * function.
226 kregs->nip = *((unsigned long *)ret_from_fork);
228 return 0;
232 * Set up a thread for executing a new program
234 void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
236 /* NIP is *really* a pointer to the function descriptor for
237 * the elf _start routine. The first entry in the function
238 * descriptor is the entry address of _start and the second
239 * entry is the TOC value we need to use.
241 unsigned long *entry = (unsigned long *)nip;
242 unsigned long *toc = entry + 1;
244 set_fs(USER_DS);
245 memset(regs->gpr, 0, sizeof(regs->gpr));
246 memset(&regs->ctr, 0, 4 * sizeof(regs->ctr));
247 __get_user(regs->nip, entry);
248 regs->gpr[1] = sp;
249 __get_user(regs->gpr[2], toc);
250 regs->msr = MSR_USER64;
251 if (last_task_used_math == current)
252 last_task_used_math = 0;
253 current->thread.fpscr = 0;
256 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
258 struct pt_regs *regs = tsk->thread.regs;
260 if (val > PR_FP_EXC_PRECISE)
261 return -EINVAL;
262 tsk->thread.fpexc_mode = __pack_fe01(val);
263 if (regs != NULL && (regs->msr & MSR_FP) != 0)
264 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
265 | tsk->thread.fpexc_mode;
266 return 0;
269 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
271 unsigned int val;
273 val = __unpack_fe01(tsk->thread.fpexc_mode);
274 return put_user(val, (unsigned int *) adr);
277 int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
278 unsigned long p4, unsigned long p5, unsigned long p6,
279 struct pt_regs *regs)
281 struct task_struct *p;
282 unsigned long parent_tidptr = 0;
283 unsigned long child_tidptr = 0;
285 if (p2 == 0)
286 p2 = regs->gpr[1]; /* stack pointer for child */
288 if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
289 CLONE_CHILD_CLEARTID)) {
290 parent_tidptr = p3;
291 child_tidptr = p4;
292 if (test_thread_flag(TIF_32BIT)) {
293 parent_tidptr &= 0xffffffff;
294 child_tidptr &= 0xffffffff;
298 if (regs->msr & MSR_FP)
299 giveup_fpu(current);
301 p = do_fork(clone_flags & ~CLONE_IDLETASK, p2, regs, 0,
302 (int *)parent_tidptr, (int *)child_tidptr);
303 return IS_ERR(p) ? PTR_ERR(p) : p->pid;
306 int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
307 unsigned long p4, unsigned long p5, unsigned long p6,
308 struct pt_regs *regs)
310 struct task_struct *p;
312 if (regs->msr & MSR_FP)
313 giveup_fpu(current);
315 p = do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
316 return IS_ERR(p) ? PTR_ERR(p) : p->pid;
319 int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
320 unsigned long p4, unsigned long p5, unsigned long p6,
321 struct pt_regs *regs)
323 struct task_struct *p;
325 if (regs->msr & MSR_FP)
326 giveup_fpu(current);
328 p = do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
329 NULL, NULL);
330 return IS_ERR(p) ? PTR_ERR(p) : p->pid;
333 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
334 unsigned long a3, unsigned long a4, unsigned long a5,
335 struct pt_regs *regs)
337 int error;
338 char * filename;
340 filename = getname((char *) a0);
341 error = PTR_ERR(filename);
342 if (IS_ERR(filename))
343 goto out;
344 if (regs->msr & MSR_FP)
345 giveup_fpu(current);
347 error = do_execve(filename, (char **) a1, (char **) a2, regs);
349 if (error == 0)
350 current->ptrace &= ~PT_DTRACE;
351 putname(filename);
353 out:
354 return error;
357 void initialize_paca_hardware_interrupt_stack(void)
359 int i;
360 unsigned long stack;
361 unsigned long end_of_stack =0;
363 for (i=1; i < NR_CPUS; i++) {
364 if (!cpu_possible(i))
365 continue;
366 /* Carve out storage for the hardware interrupt stack */
367 stack = __get_free_pages(GFP_ATOMIC, get_order(8*PAGE_SIZE));
369 if ( !stack ) {
370 printk("ERROR, cannot find space for hardware stack.\n");
371 panic(" no hardware stack ");
375 /* Store the stack value in the PACA for the processor */
376 paca[i].xHrdIntStack = stack + (8*PAGE_SIZE) - STACK_FRAME_OVERHEAD;
377 paca[i].xHrdIntCount = 0;
382 * __get_free_pages() might give us a page > KERNBASE+256M which
383 * is mapped with large ptes so we can't set up the guard page.
385 if (cpu_has_largepage())
386 return;
388 for (i=0; i < NR_CPUS; i++) {
389 if (!cpu_possible(i))
390 continue;
391 /* set page at the top of stack to be protected - prevent overflow */
392 end_of_stack = paca[i].xHrdIntStack - (8*PAGE_SIZE - STACK_FRAME_OVERHEAD);
393 ppc_md.hpte_updateboltedpp(PP_RXRX,end_of_stack);
397 extern char _stext[], _etext[], __init_begin[], __init_end[];
399 static char *ppc_find_proc_name(unsigned *p, char *buf, unsigned buflen)
401 unsigned long tb_flags;
402 unsigned short name_len;
403 unsigned long tb_start, code_start, code_ptr, code_offset;
404 unsigned int code_len;
405 unsigned long end;
407 strcpy(buf, "Unknown");
408 code_ptr = (unsigned long)p;
409 code_offset = 0;
411 /* handle functions in text and init sections */
412 if (((unsigned long)p >= (unsigned long)_stext) &&
413 ((unsigned long)p < (unsigned long)_etext))
414 end = (unsigned long)_etext;
415 else if (((unsigned long)p >= (unsigned long)__init_begin) &&
416 ((unsigned long)p < (unsigned long)__init_end))
417 end = (unsigned long)__init_end;
418 else
419 return buf;
421 while ((unsigned long)p < end) {
422 if (*p == 0) {
423 tb_start = (unsigned long)p;
424 ++p; /* Point to traceback flags */
425 tb_flags = *((unsigned long *)p);
426 p += 2; /* Skip over traceback flags */
427 if (tb_flags & TB_NAME_PRESENT) {
428 if (tb_flags & TB_PARMINFO)
429 ++p; /* skip over parminfo data */
430 if (tb_flags & TB_HAS_TBOFF) {
431 code_len = *p; /* get code length */
432 code_start = tb_start - code_len;
433 code_offset = code_ptr - code_start + 1;
434 if (code_offset > 0x100000)
435 break;
436 ++p; /* skip over code size */
438 name_len = *((unsigned short *)p);
439 if (name_len > (buflen-20))
440 name_len = buflen-20;
441 memcpy(buf, ((char *)p)+2, name_len);
442 buf[name_len] = 0;
443 if (code_offset)
444 sprintf(buf+name_len, "+0x%lx",
445 code_offset-1);
447 break;
449 ++p;
452 return buf;
456 * These bracket the sleeping functions..
458 extern void scheduling_functions_start_here(void);
459 extern void scheduling_functions_end_here(void);
460 #define first_sched (*(unsigned long *)scheduling_functions_start_here)
461 #define last_sched (*(unsigned long *)scheduling_functions_end_here)
463 unsigned long get_wchan(struct task_struct *p)
465 unsigned long ip, sp;
466 unsigned long stack_page = (unsigned long)p->thread_info;
467 int count = 0;
468 if (!p || p == current || p->state == TASK_RUNNING)
469 return 0;
470 sp = p->thread.ksp;
471 do {
472 sp = *(unsigned long *)sp;
473 if (sp < (stack_page + sizeof(struct thread_struct)) ||
474 sp >= (stack_page + THREAD_SIZE))
475 return 0;
476 if (count > 0) {
477 ip = *(unsigned long *)(sp + 16);
479 * XXX we mask the upper 32 bits until procps
480 * gets fixed.
482 if (ip < first_sched || ip >= last_sched)
483 return (ip & 0xFFFFFFFF);
485 } while (count++ < 16);
486 return 0;
489 static void show_tsk_stack(struct task_struct *p, unsigned long sp)
491 unsigned long ip;
492 unsigned long stack_page = (unsigned long)p->thread_info;
493 int count = 0;
494 char name_buf[256];
496 if (!p)
497 return;
499 printk("Call Trace:\n");
500 do {
501 if (__get_user(sp, (unsigned long *)sp))
502 break;
503 if (sp < (stack_page + sizeof(struct thread_struct)) ||
504 sp >= (stack_page + THREAD_SIZE))
505 break;
506 if (__get_user(ip, (unsigned long *)(sp + 16)))
507 break;
508 printk("[%016lx] ", ip);
509 printk("%s\n", ppc_find_proc_name((unsigned *)ip,
510 name_buf, 256));
511 } while (count++ < 32);
514 extern unsigned long *_get_SP(void);
516 void dump_stack(void)
518 show_tsk_stack(current, (unsigned long)_get_SP());
521 void show_trace_task(struct task_struct *tsk)
523 show_tsk_stack(tsk, tsk->thread.ksp);