at v2.6.12 685 lines 18 kB view raw
1/* 2 * linux/arch/ppc64/kernel/process.c 3 * 4 * Derived from "arch/i386/kernel/process.c" 5 * Copyright (C) 1995 Linus Torvalds 6 * 7 * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and 8 * Paul Mackerras (paulus@cs.anu.edu.au) 9 * 10 * PowerPC version 11 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 12 * 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. 17 */ 18 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> 24#include <linux/mm.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> 39 40#include <asm/pgtable.h> 41#include <asm/uaccess.h> 42#include <asm/system.h> 43#include <asm/io.h> 44#include <asm/processor.h> 45#include <asm/mmu.h> 46#include <asm/mmu_context.h> 47#include <asm/prom.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> 54#include <asm/time.h> 55 56#ifndef CONFIG_SMP 57struct task_struct *last_task_used_math = NULL; 58struct task_struct *last_task_used_altivec = NULL; 59#endif 60 61struct mm_struct ioremap_mm = { 62 .pgd = ioremap_dir, 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, 67}; 68 69/* 70 * Make sure the floating-point register state in the 71 * the thread_struct is up to date for task tsk. 72 */ 73void flush_fp_to_thread(struct task_struct *tsk) 74{ 75 if (tsk->thread.regs) { 76 /* 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. 83 */ 84 preempt_disable(); 85 if (tsk->thread.regs->msr & MSR_FP) { 86#ifdef CONFIG_SMP 87 /* 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. 93 */ 94 BUG_ON(tsk != current); 95#endif 96 giveup_fpu(current); 97 } 98 preempt_enable(); 99 } 100} 101 102void enable_kernel_fp(void) 103{ 104 WARN_ON(preemptible()); 105 106#ifdef CONFIG_SMP 107 if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) 108 giveup_fpu(current); 109 else 110 giveup_fpu(NULL); /* just enables FP for kernel */ 111#else 112 giveup_fpu(last_task_used_math); 113#endif /* CONFIG_SMP */ 114} 115EXPORT_SYMBOL(enable_kernel_fp); 116 117int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs) 118{ 119 if (!tsk->thread.regs) 120 return 0; 121 flush_fp_to_thread(current); 122 123 memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs)); 124 125 return 1; 126} 127 128#ifdef CONFIG_ALTIVEC 129 130void enable_kernel_altivec(void) 131{ 132 WARN_ON(preemptible()); 133 134#ifdef CONFIG_SMP 135 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) 136 giveup_altivec(current); 137 else 138 giveup_altivec(NULL); /* just enables FP for kernel */ 139#else 140 giveup_altivec(last_task_used_altivec); 141#endif /* CONFIG_SMP */ 142} 143EXPORT_SYMBOL(enable_kernel_altivec); 144 145/* 146 * Make sure the VMX/Altivec register state in the 147 * the thread_struct is up to date for task tsk. 148 */ 149void flush_altivec_to_thread(struct task_struct *tsk) 150{ 151 if (tsk->thread.regs) { 152 preempt_disable(); 153 if (tsk->thread.regs->msr & MSR_VEC) { 154#ifdef CONFIG_SMP 155 BUG_ON(tsk != current); 156#endif 157 giveup_altivec(current); 158 } 159 preempt_enable(); 160 } 161} 162 163int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs) 164{ 165 flush_altivec_to_thread(current); 166 memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs)); 167 return 1; 168} 169 170#endif /* CONFIG_ALTIVEC */ 171 172DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array); 173 174struct task_struct *__switch_to(struct task_struct *prev, 175 struct task_struct *new) 176{ 177 struct thread_struct *new_thread, *old_thread; 178 unsigned long flags; 179 struct task_struct *last; 180 181#ifdef CONFIG_SMP 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. 185 * 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. 189 * -- Cort 190 */ 191 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP)) 192 giveup_fpu(prev); 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 */ 198 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 202 */ 203 if (new->thread.regs && last_task_used_altivec == new) 204 new->thread.regs->msr |= MSR_VEC; 205#endif /* CONFIG_ALTIVEC */ 206 207 flush_tlb_pending(); 208 209 new_thread = &new->thread; 210 old_thread = &current->thread; 211 212/* Collect purr utilization data per process and per processor wise */ 213/* purr is nothing but processor time base */ 214 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; 223 } 224#endif 225 226 227 local_irq_save(flags); 228 last = _switch(old_thread, new_thread); 229 230 local_irq_restore(flags); 231 232 return last; 233} 234 235static int instructions_to_print = 16; 236 237static void show_instructions(struct pt_regs *regs) 238{ 239 int i; 240 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 * 241 sizeof(int)); 242 243 printk("Instruction dump:"); 244 245 for (i = 0; i < instructions_to_print; i++) { 246 int instr; 247 248 if (!(i % 8)) 249 printk("\n"); 250 251 if (((REGION_ID(pc) != KERNEL_REGION_ID) && 252 (REGION_ID(pc) != VMALLOC_REGION_ID)) || 253 __get_user(instr, (unsigned int *)pc)) { 254 printk("XXXXXXXX "); 255 } else { 256 if (regs->nip == pc) 257 printk("<%08x> ", instr); 258 else 259 printk("%08x ", instr); 260 } 261 262 pc += sizeof(int); 263 } 264 265 printk("\n"); 266} 267 268void show_regs(struct pt_regs * regs) 269{ 270 int i; 271 unsigned long trap; 272 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); 284 trap = TRAP(regs); 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); 288 289#ifdef CONFIG_SMP 290 printk(" CPU: %d", smp_processor_id()); 291#endif /* CONFIG_SMP */ 292 293 for (i = 0; i < 32; i++) { 294 if ((i % 4) == 0) { 295 printk("\n" KERN_INFO "GPR%02d: ", i); 296 } 297 298 printk("%016lX ", regs->gpr[i]); 299 if (i == 13 && !FULL_REGS(regs)) 300 break; 301 } 302 printk("\n"); 303 /* 304 * Lookup NIP late so we have the best change of getting the 305 * above info out without failing 306 */ 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); 314} 315 316void exit_thread(void) 317{ 318#ifndef CONFIG_SMP 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 */ 326} 327 328void flush_thread(void) 329{ 330 struct thread_info *t = current_thread_info(); 331 332 if (t->flags & _TIF_ABI_PENDING) 333 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT); 334 335#ifndef CONFIG_SMP 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 */ 343} 344 345void 346release_thread(struct task_struct *t) 347{ 348} 349 350 351/* 352 * This gets called before we allocate a new thread and copy 353 * the current task into it. 354 */ 355void prepare_to_copy(struct task_struct *tsk) 356{ 357 flush_fp_to_thread(current); 358 flush_altivec_to_thread(current); 359} 360 361/* 362 * Copy a thread.. 363 */ 364int 365copy_thread(int nr, unsigned long clone_flags, unsigned long usp, 366 unsigned long unused, struct task_struct *p, struct pt_regs *regs) 367{ 368 struct pt_regs *childregs, *kregs; 369 extern void ret_from_fork(void); 370 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE; 371 372 /* Copy registers */ 373 sp -= sizeof(struct pt_regs); 374 childregs = (struct pt_regs *) sp; 375 *childregs = *regs; 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 } else { 382 childregs->gpr[1] = usp; 383 p->thread.regs = childregs; 384 if (clone_flags & CLONE_SETTLS) { 385 if (test_thread_flag(TIF_32BIT)) 386 childregs->gpr[2] = childregs->gpr[6]; 387 else 388 childregs->gpr[13] = childregs->gpr[6]; 389 } 390 } 391 childregs->gpr[3] = 0; /* Result from fork() */ 392 sp -= STACK_FRAME_OVERHEAD; 393 394 /* 395 * The way this works is that at some point in the future 396 * some task will call _switch to switch to the new task. 397 * That will pop off the stack frame created below and start 398 * the new task running at ret_from_fork. The new task will 399 * do some house keeping and then return from the fork or clone 400 * system call, using the stack frame created above. 401 */ 402 sp -= sizeof(struct pt_regs); 403 kregs = (struct pt_regs *) sp; 404 sp -= STACK_FRAME_OVERHEAD; 405 p->thread.ksp = sp; 406 if (cpu_has_feature(CPU_FTR_SLB)) { 407 unsigned long sp_vsid = get_kernel_vsid(sp); 408 409 sp_vsid <<= SLB_VSID_SHIFT; 410 sp_vsid |= SLB_VSID_KERNEL; 411 if (cpu_has_feature(CPU_FTR_16M_PAGE)) 412 sp_vsid |= SLB_VSID_L; 413 414 p->thread.ksp_vsid = sp_vsid; 415 } 416 417 /* 418 * The PPC64 ABI makes use of a TOC to contain function 419 * pointers. The function (ret_from_except) is actually a pointer 420 * to the TOC entry. The first entry is a pointer to the actual 421 * function. 422 */ 423 kregs->nip = *((unsigned long *)ret_from_fork); 424 425 return 0; 426} 427 428/* 429 * Set up a thread for executing a new program 430 */ 431void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp) 432{ 433 unsigned long entry, toc, load_addr = regs->gpr[2]; 434 435 /* fdptr is a relocated pointer to the function descriptor for 436 * the elf _start routine. The first entry in the function 437 * descriptor is the entry address of _start and the second 438 * entry is the TOC value we need to use. 439 */ 440 set_fs(USER_DS); 441 __get_user(entry, (unsigned long __user *)fdptr); 442 __get_user(toc, (unsigned long __user *)fdptr+1); 443 444 /* Check whether the e_entry function descriptor entries 445 * need to be relocated before we can use them. 446 */ 447 if (load_addr != 0) { 448 entry += load_addr; 449 toc += load_addr; 450 } 451 452 /* 453 * If we exec out of a kernel thread then thread.regs will not be 454 * set. Do it now. 455 */ 456 if (!current->thread.regs) { 457 unsigned long childregs = (unsigned long)current->thread_info + 458 THREAD_SIZE; 459 childregs -= sizeof(struct pt_regs); 460 current->thread.regs = (struct pt_regs *)childregs; 461 } 462 463 regs->nip = entry; 464 regs->gpr[1] = sp; 465 regs->gpr[2] = toc; 466 regs->msr = MSR_USER64; 467#ifndef CONFIG_SMP 468 if (last_task_used_math == current) 469 last_task_used_math = 0; 470#endif /* CONFIG_SMP */ 471 memset(current->thread.fpr, 0, sizeof(current->thread.fpr)); 472 current->thread.fpscr = 0; 473#ifdef CONFIG_ALTIVEC 474#ifndef CONFIG_SMP 475 if (last_task_used_altivec == current) 476 last_task_used_altivec = 0; 477#endif /* CONFIG_SMP */ 478 memset(current->thread.vr, 0, sizeof(current->thread.vr)); 479 current->thread.vscr.u[0] = 0; 480 current->thread.vscr.u[1] = 0; 481 current->thread.vscr.u[2] = 0; 482 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */ 483 current->thread.vrsave = 0; 484 current->thread.used_vr = 0; 485#endif /* CONFIG_ALTIVEC */ 486} 487EXPORT_SYMBOL(start_thread); 488 489int set_fpexc_mode(struct task_struct *tsk, unsigned int val) 490{ 491 struct pt_regs *regs = tsk->thread.regs; 492 493 if (val > PR_FP_EXC_PRECISE) 494 return -EINVAL; 495 tsk->thread.fpexc_mode = __pack_fe01(val); 496 if (regs != NULL && (regs->msr & MSR_FP) != 0) 497 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1)) 498 | tsk->thread.fpexc_mode; 499 return 0; 500} 501 502int get_fpexc_mode(struct task_struct *tsk, unsigned long adr) 503{ 504 unsigned int val; 505 506 val = __unpack_fe01(tsk->thread.fpexc_mode); 507 return put_user(val, (unsigned int __user *) adr); 508} 509 510int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3, 511 unsigned long p4, unsigned long p5, unsigned long p6, 512 struct pt_regs *regs) 513{ 514 unsigned long parent_tidptr = 0; 515 unsigned long child_tidptr = 0; 516 517 if (p2 == 0) 518 p2 = regs->gpr[1]; /* stack pointer for child */ 519 520 if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | 521 CLONE_CHILD_CLEARTID)) { 522 parent_tidptr = p3; 523 child_tidptr = p5; 524 if (test_thread_flag(TIF_32BIT)) { 525 parent_tidptr &= 0xffffffff; 526 child_tidptr &= 0xffffffff; 527 } 528 } 529 530 return do_fork(clone_flags, p2, regs, 0, 531 (int __user *)parent_tidptr, (int __user *)child_tidptr); 532} 533 534int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3, 535 unsigned long p4, unsigned long p5, unsigned long p6, 536 struct pt_regs *regs) 537{ 538 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL); 539} 540 541int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3, 542 unsigned long p4, unsigned long p5, unsigned long p6, 543 struct pt_regs *regs) 544{ 545 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0, 546 NULL, NULL); 547} 548 549int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2, 550 unsigned long a3, unsigned long a4, unsigned long a5, 551 struct pt_regs *regs) 552{ 553 int error; 554 char * filename; 555 556 filename = getname((char __user *) a0); 557 error = PTR_ERR(filename); 558 if (IS_ERR(filename)) 559 goto out; 560 flush_fp_to_thread(current); 561 flush_altivec_to_thread(current); 562 error = do_execve(filename, (char __user * __user *) a1, 563 (char __user * __user *) a2, regs); 564 565 if (error == 0) { 566 task_lock(current); 567 current->ptrace &= ~PT_DTRACE; 568 task_unlock(current); 569 } 570 putname(filename); 571 572out: 573 return error; 574} 575 576static int kstack_depth_to_print = 64; 577 578static int validate_sp(unsigned long sp, struct task_struct *p, 579 unsigned long nbytes) 580{ 581 unsigned long stack_page = (unsigned long)p->thread_info; 582 583 if (sp >= stack_page + sizeof(struct thread_struct) 584 && sp <= stack_page + THREAD_SIZE - nbytes) 585 return 1; 586 587#ifdef CONFIG_IRQSTACKS 588 stack_page = (unsigned long) hardirq_ctx[task_cpu(p)]; 589 if (sp >= stack_page + sizeof(struct thread_struct) 590 && sp <= stack_page + THREAD_SIZE - nbytes) 591 return 1; 592 593 stack_page = (unsigned long) softirq_ctx[task_cpu(p)]; 594 if (sp >= stack_page + sizeof(struct thread_struct) 595 && sp <= stack_page + THREAD_SIZE - nbytes) 596 return 1; 597#endif 598 599 return 0; 600} 601 602unsigned long get_wchan(struct task_struct *p) 603{ 604 unsigned long ip, sp; 605 int count = 0; 606 607 if (!p || p == current || p->state == TASK_RUNNING) 608 return 0; 609 610 sp = p->thread.ksp; 611 if (!validate_sp(sp, p, 112)) 612 return 0; 613 614 do { 615 sp = *(unsigned long *)sp; 616 if (!validate_sp(sp, p, 112)) 617 return 0; 618 if (count > 0) { 619 ip = *(unsigned long *)(sp + 16); 620 if (!in_sched_functions(ip)) 621 return ip; 622 } 623 } while (count++ < 16); 624 return 0; 625} 626EXPORT_SYMBOL(get_wchan); 627 628void show_stack(struct task_struct *p, unsigned long *_sp) 629{ 630 unsigned long ip, newsp, lr; 631 int count = 0; 632 unsigned long sp = (unsigned long)_sp; 633 int firstframe = 1; 634 635 if (sp == 0) { 636 if (p) { 637 sp = p->thread.ksp; 638 } else { 639 sp = __get_SP(); 640 p = current; 641 } 642 } 643 644 lr = 0; 645 printk("Call Trace:\n"); 646 do { 647 if (!validate_sp(sp, p, 112)) 648 return; 649 650 _sp = (unsigned long *) sp; 651 newsp = _sp[0]; 652 ip = _sp[2]; 653 if (!firstframe || ip != lr) { 654 printk("[%016lx] [%016lx] ", sp, ip); 655 print_symbol("%s", ip); 656 if (firstframe) 657 printk(" (unreliable)"); 658 printk("\n"); 659 } 660 firstframe = 0; 661 662 /* 663 * See if this is an exception frame. 664 * We look for the "regshere" marker in the current frame. 665 */ 666 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400) 667 && _sp[12] == 0x7265677368657265ul) { 668 struct pt_regs *regs = (struct pt_regs *) 669 (sp + STACK_FRAME_OVERHEAD); 670 printk("--- Exception: %lx", regs->trap); 671 print_symbol(" at %s\n", regs->nip); 672 lr = regs->link; 673 print_symbol(" LR = %s\n", lr); 674 firstframe = 1; 675 } 676 677 sp = newsp; 678 } while (count++ < kstack_depth_to_print); 679} 680 681void dump_stack(void) 682{ 683 show_stack(current, (unsigned long *)__get_SP()); 684} 685EXPORT_SYMBOL(dump_stack);