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 * VMX/Altivec port from ppc32 (c) IBM 2003
14 * Denis Joseph Barrow (dj@de.ibm.com,barrow_dj@yahoo.com)
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
22 #include <linux/config.h>
23 #include <linux/errno.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
27 #include <linux/smp.h>
28 #include <linux/smp_lock.h>
29 #include <linux/stddef.h>
30 #include <linux/unistd.h>
31 #include <linux/ptrace.h>
32 #include <linux/slab.h>
33 #include <linux/user.h>
34 #include <linux/elf.h>
35 #include <linux/init.h>
37 #include <asm/pgtable.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
41 #include <asm/processor.h>
43 #include <asm/mmu_context.h>
45 #include <asm/ppcdebug.h>
46 #include <asm/machdep.h>
47 #include <asm/iSeries/HvCallHpt.h>
48 #include <asm/cputable.h>
50 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs);
53 struct task_struct *last_task_used_math = NULL;
54 struct task_struct *last_task_used_altivec = NULL;
55 #endif /* CONFIG_SMP */
56 static struct fs_struct init_fs = INIT_FS;
57 static struct files_struct init_files = INIT_FILES;
58 static struct signal_struct init_signals = INIT_SIGNALS;
59 struct mm_struct init_mm = INIT_MM(init_mm);
61 struct mm_struct ioremap_mm = { pgd : ioremap_dir
62 ,page_table_lock : SPIN_LOCK_UNLOCKED };
64 /* this is 16-byte aligned because it has a stack in it */
65 union task_union __attribute((aligned(16))) init_task_union = {
66 INIT_TASK(init_task_union.task)
70 struct current_set_struct current_set[NR_CPUS] = {{&init_task, 0}, };
74 unsigned long sysmap_size = 0;
76 extern char __toc_start;
78 #undef SHOW_TASK_SWITCHES
81 enable_kernel_fp(void)
84 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
87 giveup_fpu(NULL); /* just enables FP for kernel */
89 giveup_fpu(last_task_used_math);
90 #endif /* CONFIG_SMP */
94 dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
96 if (regs->msr & MSR_FP)
98 memcpy(fpregs, ¤t->thread.fpr[0], sizeof(*fpregs));
102 #ifdef CONFIG_ALTIVEC
104 dump_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
106 if (regs->msr & MSR_VEC)
107 giveup_altivec(current);
108 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
114 enable_kernel_altivec(void)
117 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
118 giveup_altivec(current);
120 giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
122 giveup_altivec(last_task_used_altivec);
123 #endif /* __SMP __ */
125 #endif /* CONFIG_ALTIVEC */
129 _switch_to(struct task_struct *prev, struct task_struct *new,
130 struct task_struct **last)
132 struct thread_struct *new_thread, *old_thread;
138 #ifdef SHOW_TASK_SWITCHES
139 printk("%s/%d -> %s/%d NIP %08lx cpu %d root %x/%x\n",
140 prev->comm,prev->pid,
141 new->comm,new->pid,new->thread.regs->nip,new->processor,
142 new->fs->root,prev->fs->root);
145 /* avoid complexity of lazy save/restore of fpu
146 * by just saving it every time we switch out if
147 * this task used the fpu during the last quantum.
149 * If it tries to use the fpu again, it'll trap and
150 * reload its fp regs. So we don't have to do a restore
151 * every switch, just a save.
154 if ( prev->thread.regs && (prev->thread.regs->msr & MSR_FP) )
156 #ifdef CONFIG_ALTIVEC
158 * If the previous thread used altivec in the last quantum
159 * (thus changing altivec regs) then save them.
160 * We used to check the VRSAVE register but not all apps
161 * set it, so we don't rely on it now (and in fact we need
162 * to save & restore VSCR even if VRSAVE == 0). -- paulus
164 * On SMP we always save/restore altivec regs just to avoid the
165 * complexity of changing processors.
168 if ((prev->thread.regs && (prev->thread.regs->msr & MSR_VEC)))
169 giveup_altivec(prev);
170 #endif /* CONFIG_ALTIVEC */
171 /* prev->last_processor = prev->processor; */
172 current_set[smp_processor_id()].task = new;
173 #endif /* CONFIG_SMP */
174 new_thread = &new->thread;
175 old_thread = ¤t->thread;
176 *last = _switch(old_thread, new_thread);
180 void show_regs(struct pt_regs * regs)
184 printk("NIP: %016lX XER: %016lX LR: %016lX REGS: %p TRAP: %04lx %s\n",
185 regs->nip, regs->xer, regs->link, regs,regs->trap, print_tainted());
186 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
187 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
188 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
189 regs->msr&MSR_IR ? 1 : 0,
190 regs->msr&MSR_DR ? 1 : 0);
191 printk("TASK = %p[%d] '%s' ",
192 current, current->pid, current->comm);
193 printk("Last syscall: %ld ", current->thread.last_syscall);
195 printk("\nlast math %p last altivec %p", last_task_used_math,
196 last_task_used_altivec);
200 /* printk(" CPU: %d last CPU: %d", current->processor,current->last_processor); */
201 #endif /* CONFIG_SMP */
204 for (i = 0; i < 32; i++)
209 printk("GPR%02d: ", i);
212 if ( __get_user(r, &(regs->gpr[i])) )
215 printk("%016lX ", r);
223 void exit_thread(void)
226 if (last_task_used_math == current)
227 last_task_used_math = NULL;
228 if (last_task_used_altivec == current)
229 last_task_used_altivec = NULL;
233 void flush_thread(void)
236 if (last_task_used_math == current)
237 last_task_used_math = NULL;
238 if (last_task_used_altivec == current)
239 last_task_used_altivec = NULL;
244 release_thread(struct task_struct *t)
252 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
253 unsigned long unused,
254 struct task_struct * p, struct pt_regs * regs)
257 struct pt_regs * childregs, *kregs;
258 extern void ret_from_fork(void);
261 childregs = ((struct pt_regs *)
262 ((unsigned long)p + sizeof(union task_union)
263 - STACK_FRAME_OVERHEAD)) - 2;
265 childregs->gpr[3] = 0; /* Result from fork() */
266 p->thread.regs = childregs;
267 p->thread.ksp = (unsigned long) childregs - STACK_FRAME_OVERHEAD;
268 p->thread.ksp -= sizeof(struct pt_regs ) + STACK_FRAME_OVERHEAD;
269 kregs = (struct pt_regs *)(p->thread.ksp + STACK_FRAME_OVERHEAD);
270 /* The PPC64 compiler makes use of a TOC to contain function
271 * pointers. The function (ret_from_except) is actually a pointer
272 * to the TOC entry. The first entry is a pointer to the actual
275 kregs->nip = *((unsigned long *)ret_from_fork);
276 asm volatile("mfmsr %0" : "=r" (msr):);
278 kregs->gpr[1] = (unsigned long)childregs - STACK_FRAME_OVERHEAD;
279 kregs->gpr[2] = (((unsigned long)&__toc_start) + 0x8000);
281 if (usp >= (unsigned long) regs) {
282 /* Stack is in kernel space - must adjust */
283 childregs->gpr[1] = (unsigned long)(childregs + 1);
284 *((unsigned long *) childregs->gpr[1]) = 0;
286 /* Provided stack is in user space */
287 childregs->gpr[1] = usp;
289 p->thread.last_syscall = -1;
292 * copy fpu info - assume lazy fpu switch now always
295 if (regs->msr & MSR_FP) {
297 childregs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
299 memcpy(&p->thread.fpr, ¤t->thread.fpr, sizeof(p->thread.fpr));
300 p->thread.fpscr = current->thread.fpscr;
301 p->thread.fpexc_mode = current->thread.fpexc_mode;
303 #ifdef CONFIG_ALTIVEC
305 * copy altiVec info - assume lazy altiVec switch
308 if (regs->msr & MSR_VEC)
309 giveup_altivec(current);
310 memcpy(&p->thread.vr, ¤t->thread.vr, sizeof(p->thread.vr));
311 p->thread.vscr = current->thread.vscr;
312 childregs->msr &= ~MSR_VEC;
313 #endif /* CONFIG_ALTIVEC */
319 * Set up a thread for executing a new program
321 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
323 unsigned long entry, toc, load_addr = regs->gpr[2];
325 /* fdptr is a relocated pointer to the function descriptor for
326 * the elf _start routine. The first entry in the function
327 * descriptor is the entry address of _start and the second
328 * entry is the TOC value we need to use.
331 __get_user(entry, (unsigned long *)fdptr);
332 __get_user(toc, (unsigned long *)fdptr+1);
334 /* Check whether the e_entry function descriptor entries
335 * need to be relocated before we can use them.
337 if ( load_addr != 0 ) {
345 regs->msr = MSR_USER64;
347 if (last_task_used_math == current)
348 last_task_used_math = 0;
349 if (last_task_used_altivec == current)
350 last_task_used_altivec = 0;
351 #endif /* CONFIG_SMP */
352 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
353 current->thread.fpscr = 0;
354 #ifdef CONFIG_ALTIVEC
355 memset(¤t->thread.vr[0], 0,offsetof(struct thread_struct,vrsave[2])-
356 offsetof(struct thread_struct,vr[0]));
357 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
358 #endif /* CONFIG_ALTIVEC */
361 # define PR_FP_EXC_DISABLED 0 /* FP exceptions disabled */
362 # define PR_FP_EXC_NONRECOV 1 /* async non-recoverable exc. mode */
363 # define PR_FP_EXC_ASYNC 2 /* async recoverable exception mode */
364 # define PR_FP_EXC_PRECISE 3 /* precise exception mode */
366 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
368 struct pt_regs *regs = tsk->thread.regs;
370 if (val > PR_FP_EXC_PRECISE)
372 tsk->thread.fpexc_mode = __pack_fe01(val);
373 if (regs != NULL && (regs->msr & MSR_FP) != 0)
374 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
375 | tsk->thread.fpexc_mode;
379 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
383 val = __unpack_fe01(tsk->thread.fpexc_mode);
384 return put_user(val, (unsigned int *) adr);
387 int sys_clone(int p1, int p2, int p3, int p4, int p5, int p6,
388 struct pt_regs *regs)
390 return do_fork(p1, regs->gpr[1], regs, 0);
393 int sys_fork(int p1, int p2, int p3, int p4, int p5, int p6,
394 struct pt_regs *regs)
396 return do_fork(SIGCHLD, regs->gpr[1], regs, 0);
399 int sys_vfork(int p1, int p2, int p3, int p4, int p5, int p6,
400 struct pt_regs *regs)
402 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0);
405 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
406 unsigned long a3, unsigned long a4, unsigned long a5,
407 struct pt_regs *regs)
412 filename = getname((char *) a0);
413 error = PTR_ERR(filename);
414 if (IS_ERR(filename))
416 if (regs->msr & MSR_FP)
418 #ifdef CONFIG_ALTIVEC
419 if (regs->msr & MSR_VEC)
420 giveup_altivec(current);
421 #endif /* CONFIG_ALTIVEC */
422 error = do_execve(filename, (char **) a1, (char **) a2, regs);
425 current->ptrace &= ~PT_DTRACE;
432 struct task_struct * alloc_task_struct(void)
434 struct task_struct * new_task_ptr;
436 new_task_ptr = ((struct task_struct *)
437 __get_free_pages(GFP_KERNEL, get_order(THREAD_SIZE)));
442 void free_task_struct(struct task_struct * task_ptr)
444 free_pages((unsigned long)(task_ptr), get_order(THREAD_SIZE));
447 void initialize_paca_hardware_interrupt_stack(void)
449 extern struct systemcfg *systemcfg;
453 unsigned long end_of_stack =0;
455 for (i=1; i < systemcfg->processorCount; i++) {
456 /* Carve out storage for the hardware interrupt stack */
457 stack = __get_free_pages(GFP_KERNEL, get_order(8*PAGE_SIZE));
460 printk("ERROR, cannot find space for hardware stack.\n");
461 panic(" no hardware stack ");
465 /* Store the stack value in the PACA for the processor */
466 paca[i].xHrdIntStack = stack + (8*PAGE_SIZE) - STACK_FRAME_OVERHEAD;
467 paca[i].xHrdIntCount = 0;
472 * __get_free_pages() might give us a page > KERNBASE+256M which
473 * is mapped with large ptes so we can't set up the guard page.
475 if (cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE)
478 for (i=0; i < systemcfg->processorCount; i++) {
479 /* set page at the top of stack to be protected - prevent overflow */
480 end_of_stack = paca[i].xHrdIntStack - (8*PAGE_SIZE - STACK_FRAME_OVERHEAD);
481 ppc_md.hpte_updateboltedpp(PP_RXRX,end_of_stack);
485 extern char _stext[], _etext[];
487 char * ppc_find_proc_name( unsigned * p, char * buf, unsigned buflen )
489 unsigned long tb_flags;
490 unsigned short name_len;
491 unsigned long tb_start, code_start, code_ptr, code_offset;
493 strcpy( buf, "Unknown" );
494 code_ptr = (unsigned long)p;
496 if ( ( (unsigned long)p >= (unsigned long)_stext ) && ( (unsigned long)p <= (unsigned long)_etext ) ) {
497 while ( (unsigned long)p <= (unsigned long)_etext ) {
499 tb_start = (unsigned long)p;
500 ++p; /* Point to traceback flags */
501 tb_flags = *((unsigned long *)p);
502 p += 2; /* Skip over traceback flags */
503 if ( tb_flags & TB_NAME_PRESENT ) {
504 if ( tb_flags & TB_PARMINFO )
505 ++p; /* skip over parminfo data */
506 if ( tb_flags & TB_HAS_TBOFF ) {
507 code_len = *p; /* get code length */
508 code_start = tb_start - code_len;
509 code_offset = code_ptr - code_start + 1;
510 if ( code_offset > 0x100000 )
512 ++p; /* skip over code size */
514 name_len = *((unsigned short *)p);
515 if ( name_len > (buflen-20) )
516 name_len = buflen-20;
517 memcpy( buf, ((char *)p)+2, name_len );
520 sprintf( buf+name_len, "+0x%lx", code_offset-1 );
531 print_backtrace(unsigned long *sp)
537 printk("Call backtrace: \n");
539 if (__get_user(i, &sp[2]))
541 printk("%016lX ", i);
542 printk("%s\n", ppc_find_proc_name((unsigned *)i, name_buf, 256));
544 if (__get_user(sp, (unsigned long **)sp))
551 * These bracket the sleeping functions..
553 extern void scheduling_functions_start_here(void);
554 extern void scheduling_functions_end_here(void);
555 #define first_sched (*(unsigned long *)scheduling_functions_start_here)
556 #define last_sched (*(unsigned long *)scheduling_functions_end_here)
558 unsigned long get_wchan(struct task_struct *p)
560 unsigned long ip, sp;
561 unsigned long stack_page = (unsigned long)p;
563 if (!p || p == current || p->state == TASK_RUNNING)
567 sp = *(unsigned long *)sp;
568 if (sp < (stack_page + (2 * PAGE_SIZE)) ||
569 sp >= (stack_page + THREAD_SIZE))
572 ip = *(unsigned long *)(sp + 16);
574 * XXX we mask the upper 32 bits until procps
577 if (ip < first_sched || ip >= last_sched)
580 } while (count++ < 16);
584 void show_trace_task(struct task_struct *p)
586 unsigned long ip, sp;
587 unsigned long stack_page = (unsigned long)p;
593 printk("Call Trace: ");
596 sp = *(unsigned long *)sp;
597 if (sp < (stack_page + (2 * PAGE_SIZE)) ||
598 sp >= (stack_page + THREAD_SIZE))
601 ip = *(unsigned long *)(sp + 16);
602 printk("[%016lx] ", ip);
604 } while (count++ < 16);