process.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. * arch/ppc/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. #include <linux/config.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/stddef.h>
  26. #include <linux/unistd.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/user.h>
  30. #include <linux/elf.h>
  31. #include <linux/init.h>
  32. #include <linux/prctl.h>
  33. #include <linux/init_task.h>
  34. #include <linux/module.h>
  35. #include <linux/kallsyms.h>
  36. #include <linux/mqueue.h>
  37. #include <linux/hardirq.h>
  38. #include <linux/utsname.h>
  39. #include <linux/kprobes.h>
  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/prom.h>
  47. #ifdef CONFIG_PPC64
  48. #include <asm/firmware.h>
  49. #include <asm/time.h>
  50. #include <asm/machdep.h>
  51. #endif
  52. extern unsigned long _get_SP(void);
  53. #ifndef CONFIG_SMP
  54. struct task_struct *last_task_used_math = NULL;
  55. struct task_struct *last_task_used_altivec = NULL;
  56. struct task_struct *last_task_used_spe = NULL;
  57. #endif
  58. /*
  59. * Make sure the floating-point register state in the
  60. * the thread_struct is up to date for task tsk.
  61. */
  62. void flush_fp_to_thread(struct task_struct *tsk)
  63. {
  64. if (tsk->thread.regs) {
  65. /*
  66. * We need to disable preemption here because if we didn't,
  67. * another process could get scheduled after the regs->msr
  68. * test but before we have finished saving the FP registers
  69. * to the thread_struct. That process could take over the
  70. * FPU, and then when we get scheduled again we would store
  71. * bogus values for the remaining FP registers.
  72. */
  73. preempt_disable();
  74. if (tsk->thread.regs->msr & MSR_FP) {
  75. #ifdef CONFIG_SMP
  76. /*
  77. * This should only ever be called for current or
  78. * for a stopped child process. Since we save away
  79. * the FP register state on context switch on SMP,
  80. * there is something wrong if a stopped child appears
  81. * to still have its FP state in the CPU registers.
  82. */
  83. BUG_ON(tsk != current);
  84. #endif
  85. giveup_fpu(current);
  86. }
  87. preempt_enable();
  88. }
  89. }
  90. void enable_kernel_fp(void)
  91. {
  92. WARN_ON(preemptible());
  93. #ifdef CONFIG_SMP
  94. if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
  95. giveup_fpu(current);
  96. else
  97. giveup_fpu(NULL); /* just enables FP for kernel */
  98. #else
  99. giveup_fpu(last_task_used_math);
  100. #endif /* CONFIG_SMP */
  101. }
  102. EXPORT_SYMBOL(enable_kernel_fp);
  103. int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
  104. {
  105. if (!tsk->thread.regs)
  106. return 0;
  107. flush_fp_to_thread(current);
  108. memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
  109. return 1;
  110. }
  111. #ifdef CONFIG_ALTIVEC
  112. void enable_kernel_altivec(void)
  113. {
  114. WARN_ON(preemptible());
  115. #ifdef CONFIG_SMP
  116. if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
  117. giveup_altivec(current);
  118. else
  119. giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
  120. #else
  121. giveup_altivec(last_task_used_altivec);
  122. #endif /* CONFIG_SMP */
  123. }
  124. EXPORT_SYMBOL(enable_kernel_altivec);
  125. /*
  126. * Make sure the VMX/Altivec register state in the
  127. * the thread_struct is up to date for task tsk.
  128. */
  129. void flush_altivec_to_thread(struct task_struct *tsk)
  130. {
  131. if (tsk->thread.regs) {
  132. preempt_disable();
  133. if (tsk->thread.regs->msr & MSR_VEC) {
  134. #ifdef CONFIG_SMP
  135. BUG_ON(tsk != current);
  136. #endif
  137. giveup_altivec(current);
  138. }
  139. preempt_enable();
  140. }
  141. }
  142. int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
  143. {
  144. flush_altivec_to_thread(current);
  145. memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
  146. return 1;
  147. }
  148. #endif /* CONFIG_ALTIVEC */
  149. #ifdef CONFIG_SPE
  150. void enable_kernel_spe(void)
  151. {
  152. WARN_ON(preemptible());
  153. #ifdef CONFIG_SMP
  154. if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
  155. giveup_spe(current);
  156. else
  157. giveup_spe(NULL); /* just enable SPE for kernel - force */
  158. #else
  159. giveup_spe(last_task_used_spe);
  160. #endif /* __SMP __ */
  161. }
  162. EXPORT_SYMBOL(enable_kernel_spe);
  163. void flush_spe_to_thread(struct task_struct *tsk)
  164. {
  165. if (tsk->thread.regs) {
  166. preempt_disable();
  167. if (tsk->thread.regs->msr & MSR_SPE) {
  168. #ifdef CONFIG_SMP
  169. BUG_ON(tsk != current);
  170. #endif
  171. giveup_spe(current);
  172. }
  173. preempt_enable();
  174. }
  175. }
  176. int dump_spe(struct pt_regs *regs, elf_vrregset_t *evrregs)
  177. {
  178. flush_spe_to_thread(current);
  179. /* We copy u32 evr[32] + u64 acc + u32 spefscr -> 35 */
  180. memcpy(evrregs, &current->thread.evr[0], sizeof(u32) * 35);
  181. return 1;
  182. }
  183. #endif /* CONFIG_SPE */
  184. int set_dabr(unsigned long dabr)
  185. {
  186. #ifdef CONFIG_PPC64
  187. if (ppc_md.set_dabr)
  188. return ppc_md.set_dabr(dabr);
  189. #endif
  190. mtspr(SPRN_DABR, dabr);
  191. return 0;
  192. }
  193. #ifdef CONFIG_PPC64
  194. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  195. static DEFINE_PER_CPU(unsigned long, current_dabr);
  196. #endif
  197. struct task_struct *__switch_to(struct task_struct *prev,
  198. struct task_struct *new)
  199. {
  200. struct thread_struct *new_thread, *old_thread;
  201. unsigned long flags;
  202. struct task_struct *last;
  203. #ifdef CONFIG_SMP
  204. /* avoid complexity of lazy save/restore of fpu
  205. * by just saving it every time we switch out if
  206. * this task used the fpu during the last quantum.
  207. *
  208. * If it tries to use the fpu again, it'll trap and
  209. * reload its fp regs. So we don't have to do a restore
  210. * every switch, just a save.
  211. * -- Cort
  212. */
  213. if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
  214. giveup_fpu(prev);
  215. #ifdef CONFIG_ALTIVEC
  216. /*
  217. * If the previous thread used altivec in the last quantum
  218. * (thus changing altivec regs) then save them.
  219. * We used to check the VRSAVE register but not all apps
  220. * set it, so we don't rely on it now (and in fact we need
  221. * to save & restore VSCR even if VRSAVE == 0). -- paulus
  222. *
  223. * On SMP we always save/restore altivec regs just to avoid the
  224. * complexity of changing processors.
  225. * -- Cort
  226. */
  227. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
  228. giveup_altivec(prev);
  229. #endif /* CONFIG_ALTIVEC */
  230. #ifdef CONFIG_SPE
  231. /*
  232. * If the previous thread used spe in the last quantum
  233. * (thus changing spe regs) then save them.
  234. *
  235. * On SMP we always save/restore spe regs just to avoid the
  236. * complexity of changing processors.
  237. */
  238. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
  239. giveup_spe(prev);
  240. #endif /* CONFIG_SPE */
  241. #else /* CONFIG_SMP */
  242. #ifdef CONFIG_ALTIVEC
  243. /* Avoid the trap. On smp this this never happens since
  244. * we don't set last_task_used_altivec -- Cort
  245. */
  246. if (new->thread.regs && last_task_used_altivec == new)
  247. new->thread.regs->msr |= MSR_VEC;
  248. #endif /* CONFIG_ALTIVEC */
  249. #ifdef CONFIG_SPE
  250. /* Avoid the trap. On smp this this never happens since
  251. * we don't set last_task_used_spe
  252. */
  253. if (new->thread.regs && last_task_used_spe == new)
  254. new->thread.regs->msr |= MSR_SPE;
  255. #endif /* CONFIG_SPE */
  256. #endif /* CONFIG_SMP */
  257. #ifdef CONFIG_PPC64 /* for now */
  258. if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr)) {
  259. set_dabr(new->thread.dabr);
  260. __get_cpu_var(current_dabr) = new->thread.dabr;
  261. }
  262. flush_tlb_pending();
  263. #endif
  264. new_thread = &new->thread;
  265. old_thread = &current->thread;
  266. #ifdef CONFIG_PPC64
  267. /*
  268. * Collect processor utilization data per process
  269. */
  270. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  271. struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
  272. long unsigned start_tb, current_tb;
  273. start_tb = old_thread->start_tb;
  274. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  275. old_thread->accum_tb += (current_tb - start_tb);
  276. new_thread->start_tb = current_tb;
  277. }
  278. #endif
  279. local_irq_save(flags);
  280. last = _switch(old_thread, new_thread);
  281. local_irq_restore(flags);
  282. return last;
  283. }
  284. static int instructions_to_print = 16;
  285. #ifdef CONFIG_PPC64
  286. #define BAD_PC(pc) ((REGION_ID(pc) != KERNEL_REGION_ID) && \
  287. (REGION_ID(pc) != VMALLOC_REGION_ID))
  288. #else
  289. #define BAD_PC(pc) ((pc) < KERNELBASE)
  290. #endif
  291. static void show_instructions(struct pt_regs *regs)
  292. {
  293. int i;
  294. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  295. sizeof(int));
  296. printk("Instruction dump:");
  297. for (i = 0; i < instructions_to_print; i++) {
  298. int instr;
  299. if (!(i % 8))
  300. printk("\n");
  301. if (BAD_PC(pc) || __get_user(instr, (unsigned int *)pc)) {
  302. printk("XXXXXXXX ");
  303. } else {
  304. if (regs->nip == pc)
  305. printk("<%08x> ", instr);
  306. else
  307. printk("%08x ", instr);
  308. }
  309. pc += sizeof(int);
  310. }
  311. printk("\n");
  312. }
  313. static struct regbit {
  314. unsigned long bit;
  315. const char *name;
  316. } msr_bits[] = {
  317. {MSR_EE, "EE"},
  318. {MSR_PR, "PR"},
  319. {MSR_FP, "FP"},
  320. {MSR_ME, "ME"},
  321. {MSR_IR, "IR"},
  322. {MSR_DR, "DR"},
  323. {0, NULL}
  324. };
  325. static void printbits(unsigned long val, struct regbit *bits)
  326. {
  327. const char *sep = "";
  328. printk("<");
  329. for (; bits->bit; ++bits)
  330. if (val & bits->bit) {
  331. printk("%s%s", sep, bits->name);
  332. sep = ",";
  333. }
  334. printk(">");
  335. }
  336. #ifdef CONFIG_PPC64
  337. #define REG "%016lX"
  338. #define REGS_PER_LINE 4
  339. #define LAST_VOLATILE 13
  340. #else
  341. #define REG "%08lX"
  342. #define REGS_PER_LINE 8
  343. #define LAST_VOLATILE 12
  344. #endif
  345. void show_regs(struct pt_regs * regs)
  346. {
  347. int i, trap;
  348. printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
  349. regs->nip, regs->link, regs->ctr);
  350. printk("REGS: %p TRAP: %04lx %s (%s)\n",
  351. regs, regs->trap, print_tainted(), system_utsname.release);
  352. printk("MSR: "REG" ", regs->msr);
  353. printbits(regs->msr, msr_bits);
  354. printk(" CR: %08lX XER: %08lX\n", regs->ccr, regs->xer);
  355. trap = TRAP(regs);
  356. if (trap == 0x300 || trap == 0x600)
  357. printk("DAR: "REG", DSISR: "REG"\n", regs->dar, regs->dsisr);
  358. printk("TASK = %p[%d] '%s' THREAD: %p",
  359. current, current->pid, current->comm, current->thread_info);
  360. #ifdef CONFIG_SMP
  361. printk(" CPU: %d", smp_processor_id());
  362. #endif /* CONFIG_SMP */
  363. for (i = 0; i < 32; i++) {
  364. if ((i % REGS_PER_LINE) == 0)
  365. printk("\n" KERN_INFO "GPR%02d: ", i);
  366. printk(REG " ", regs->gpr[i]);
  367. if (i == LAST_VOLATILE && !FULL_REGS(regs))
  368. break;
  369. }
  370. printk("\n");
  371. #ifdef CONFIG_KALLSYMS
  372. /*
  373. * Lookup NIP late so we have the best change of getting the
  374. * above info out without failing
  375. */
  376. printk("NIP ["REG"] ", regs->nip);
  377. print_symbol("%s\n", regs->nip);
  378. printk("LR ["REG"] ", regs->link);
  379. print_symbol("%s\n", regs->link);
  380. #endif
  381. show_stack(current, (unsigned long *) regs->gpr[1]);
  382. if (!user_mode(regs))
  383. show_instructions(regs);
  384. }
  385. void exit_thread(void)
  386. {
  387. kprobe_flush_task(current);
  388. #ifndef CONFIG_SMP
  389. if (last_task_used_math == current)
  390. last_task_used_math = NULL;
  391. #ifdef CONFIG_ALTIVEC
  392. if (last_task_used_altivec == current)
  393. last_task_used_altivec = NULL;
  394. #endif /* CONFIG_ALTIVEC */
  395. #ifdef CONFIG_SPE
  396. if (last_task_used_spe == current)
  397. last_task_used_spe = NULL;
  398. #endif
  399. #endif /* CONFIG_SMP */
  400. }
  401. void flush_thread(void)
  402. {
  403. #ifdef CONFIG_PPC64
  404. struct thread_info *t = current_thread_info();
  405. if (t->flags & _TIF_ABI_PENDING)
  406. t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
  407. #endif
  408. kprobe_flush_task(current);
  409. #ifndef CONFIG_SMP
  410. if (last_task_used_math == current)
  411. last_task_used_math = NULL;
  412. #ifdef CONFIG_ALTIVEC
  413. if (last_task_used_altivec == current)
  414. last_task_used_altivec = NULL;
  415. #endif /* CONFIG_ALTIVEC */
  416. #ifdef CONFIG_SPE
  417. if (last_task_used_spe == current)
  418. last_task_used_spe = NULL;
  419. #endif
  420. #endif /* CONFIG_SMP */
  421. #ifdef CONFIG_PPC64 /* for now */
  422. if (current->thread.dabr) {
  423. current->thread.dabr = 0;
  424. set_dabr(0);
  425. }
  426. #endif
  427. }
  428. void
  429. release_thread(struct task_struct *t)
  430. {
  431. }
  432. /*
  433. * This gets called before we allocate a new thread and copy
  434. * the current task into it.
  435. */
  436. void prepare_to_copy(struct task_struct *tsk)
  437. {
  438. flush_fp_to_thread(current);
  439. flush_altivec_to_thread(current);
  440. flush_spe_to_thread(current);
  441. }
  442. /*
  443. * Copy a thread..
  444. */
  445. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  446. unsigned long unused, struct task_struct *p,
  447. struct pt_regs *regs)
  448. {
  449. struct pt_regs *childregs, *kregs;
  450. extern void ret_from_fork(void);
  451. unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
  452. CHECK_FULL_REGS(regs);
  453. /* Copy registers */
  454. sp -= sizeof(struct pt_regs);
  455. childregs = (struct pt_regs *) sp;
  456. *childregs = *regs;
  457. if ((childregs->msr & MSR_PR) == 0) {
  458. /* for kernel thread, set `current' and stackptr in new task */
  459. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  460. #ifdef CONFIG_PPC32
  461. childregs->gpr[2] = (unsigned long) p;
  462. #else
  463. clear_ti_thread_flag(p->thread_info, TIF_32BIT);
  464. #endif
  465. p->thread.regs = NULL; /* no user register state */
  466. } else {
  467. childregs->gpr[1] = usp;
  468. p->thread.regs = childregs;
  469. if (clone_flags & CLONE_SETTLS) {
  470. #ifdef CONFIG_PPC64
  471. if (!test_thread_flag(TIF_32BIT))
  472. childregs->gpr[13] = childregs->gpr[6];
  473. else
  474. #endif
  475. childregs->gpr[2] = childregs->gpr[6];
  476. }
  477. }
  478. childregs->gpr[3] = 0; /* Result from fork() */
  479. sp -= STACK_FRAME_OVERHEAD;
  480. /*
  481. * The way this works is that at some point in the future
  482. * some task will call _switch to switch to the new task.
  483. * That will pop off the stack frame created below and start
  484. * the new task running at ret_from_fork. The new task will
  485. * do some house keeping and then return from the fork or clone
  486. * system call, using the stack frame created above.
  487. */
  488. sp -= sizeof(struct pt_regs);
  489. kregs = (struct pt_regs *) sp;
  490. sp -= STACK_FRAME_OVERHEAD;
  491. p->thread.ksp = sp;
  492. #ifdef CONFIG_PPC64
  493. if (cpu_has_feature(CPU_FTR_SLB)) {
  494. unsigned long sp_vsid = get_kernel_vsid(sp);
  495. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  496. sp_vsid <<= SLB_VSID_SHIFT;
  497. sp_vsid |= SLB_VSID_KERNEL | llp;
  498. p->thread.ksp_vsid = sp_vsid;
  499. }
  500. /*
  501. * The PPC64 ABI makes use of a TOC to contain function
  502. * pointers. The function (ret_from_except) is actually a pointer
  503. * to the TOC entry. The first entry is a pointer to the actual
  504. * function.
  505. */
  506. kregs->nip = *((unsigned long *)ret_from_fork);
  507. #else
  508. kregs->nip = (unsigned long)ret_from_fork;
  509. p->thread.last_syscall = -1;
  510. #endif
  511. return 0;
  512. }
  513. /*
  514. * Set up a thread for executing a new program
  515. */
  516. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  517. {
  518. #ifdef CONFIG_PPC64
  519. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  520. #endif
  521. set_fs(USER_DS);
  522. /*
  523. * If we exec out of a kernel thread then thread.regs will not be
  524. * set. Do it now.
  525. */
  526. if (!current->thread.regs) {
  527. unsigned long childregs = (unsigned long)current->thread_info +
  528. THREAD_SIZE;
  529. childregs -= sizeof(struct pt_regs);
  530. current->thread.regs = (struct pt_regs *)childregs;
  531. }
  532. memset(regs->gpr, 0, sizeof(regs->gpr));
  533. regs->ctr = 0;
  534. regs->link = 0;
  535. regs->xer = 0;
  536. regs->ccr = 0;
  537. regs->gpr[1] = sp;
  538. #ifdef CONFIG_PPC32
  539. regs->mq = 0;
  540. regs->nip = start;
  541. regs->msr = MSR_USER;
  542. #else
  543. if (!test_thread_flag(TIF_32BIT)) {
  544. unsigned long entry, toc;
  545. /* start is a relocated pointer to the function descriptor for
  546. * the elf _start routine. The first entry in the function
  547. * descriptor is the entry address of _start and the second
  548. * entry is the TOC value we need to use.
  549. */
  550. __get_user(entry, (unsigned long __user *)start);
  551. __get_user(toc, (unsigned long __user *)start+1);
  552. /* Check whether the e_entry function descriptor entries
  553. * need to be relocated before we can use them.
  554. */
  555. if (load_addr != 0) {
  556. entry += load_addr;
  557. toc += load_addr;
  558. }
  559. regs->nip = entry;
  560. regs->gpr[2] = toc;
  561. regs->msr = MSR_USER64;
  562. } else {
  563. regs->nip = start;
  564. regs->gpr[2] = 0;
  565. regs->msr = MSR_USER32;
  566. }
  567. #endif
  568. #ifndef CONFIG_SMP
  569. if (last_task_used_math == current)
  570. last_task_used_math = NULL;
  571. #ifdef CONFIG_ALTIVEC
  572. if (last_task_used_altivec == current)
  573. last_task_used_altivec = NULL;
  574. #endif
  575. #ifdef CONFIG_SPE
  576. if (last_task_used_spe == current)
  577. last_task_used_spe = NULL;
  578. #endif
  579. #endif /* CONFIG_SMP */
  580. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  581. current->thread.fpscr.val = 0;
  582. #ifdef CONFIG_ALTIVEC
  583. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  584. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  585. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  586. current->thread.vrsave = 0;
  587. current->thread.used_vr = 0;
  588. #endif /* CONFIG_ALTIVEC */
  589. #ifdef CONFIG_SPE
  590. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  591. current->thread.acc = 0;
  592. current->thread.spefscr = 0;
  593. current->thread.used_spe = 0;
  594. #endif /* CONFIG_SPE */
  595. }
  596. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  597. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  598. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  599. {
  600. struct pt_regs *regs = tsk->thread.regs;
  601. /* This is a bit hairy. If we are an SPE enabled processor
  602. * (have embedded fp) we store the IEEE exception enable flags in
  603. * fpexc_mode. fpexc_mode is also used for setting FP exception
  604. * mode (asyn, precise, disabled) for 'Classic' FP. */
  605. if (val & PR_FP_EXC_SW_ENABLE) {
  606. #ifdef CONFIG_SPE
  607. tsk->thread.fpexc_mode = val &
  608. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  609. return 0;
  610. #else
  611. return -EINVAL;
  612. #endif
  613. }
  614. /* on a CONFIG_SPE this does not hurt us. The bits that
  615. * __pack_fe01 use do not overlap with bits used for
  616. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  617. * on CONFIG_SPE implementations are reserved so writing to
  618. * them does not change anything */
  619. if (val > PR_FP_EXC_PRECISE)
  620. return -EINVAL;
  621. tsk->thread.fpexc_mode = __pack_fe01(val);
  622. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  623. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  624. | tsk->thread.fpexc_mode;
  625. return 0;
  626. }
  627. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  628. {
  629. unsigned int val;
  630. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  631. #ifdef CONFIG_SPE
  632. val = tsk->thread.fpexc_mode;
  633. #else
  634. return -EINVAL;
  635. #endif
  636. else
  637. val = __unpack_fe01(tsk->thread.fpexc_mode);
  638. return put_user(val, (unsigned int __user *) adr);
  639. }
  640. #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
  641. int sys_clone(unsigned long clone_flags, unsigned long usp,
  642. int __user *parent_tidp, void __user *child_threadptr,
  643. int __user *child_tidp, int p6,
  644. struct pt_regs *regs)
  645. {
  646. CHECK_FULL_REGS(regs);
  647. if (usp == 0)
  648. usp = regs->gpr[1]; /* stack pointer for child */
  649. #ifdef CONFIG_PPC64
  650. if (test_thread_flag(TIF_32BIT)) {
  651. parent_tidp = TRUNC_PTR(parent_tidp);
  652. child_tidp = TRUNC_PTR(child_tidp);
  653. }
  654. #endif
  655. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  656. }
  657. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  658. unsigned long p4, unsigned long p5, unsigned long p6,
  659. struct pt_regs *regs)
  660. {
  661. CHECK_FULL_REGS(regs);
  662. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  663. }
  664. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  665. unsigned long p4, unsigned long p5, unsigned long p6,
  666. struct pt_regs *regs)
  667. {
  668. CHECK_FULL_REGS(regs);
  669. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  670. regs, 0, NULL, NULL);
  671. }
  672. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  673. unsigned long a3, unsigned long a4, unsigned long a5,
  674. struct pt_regs *regs)
  675. {
  676. int error;
  677. char *filename;
  678. filename = getname((char __user *) a0);
  679. error = PTR_ERR(filename);
  680. if (IS_ERR(filename))
  681. goto out;
  682. flush_fp_to_thread(current);
  683. flush_altivec_to_thread(current);
  684. flush_spe_to_thread(current);
  685. error = do_execve(filename, (char __user * __user *) a1,
  686. (char __user * __user *) a2, regs);
  687. if (error == 0) {
  688. task_lock(current);
  689. current->ptrace &= ~PT_DTRACE;
  690. task_unlock(current);
  691. }
  692. putname(filename);
  693. out:
  694. return error;
  695. }
  696. static int validate_sp(unsigned long sp, struct task_struct *p,
  697. unsigned long nbytes)
  698. {
  699. unsigned long stack_page = (unsigned long)p->thread_info;
  700. if (sp >= stack_page + sizeof(struct thread_struct)
  701. && sp <= stack_page + THREAD_SIZE - nbytes)
  702. return 1;
  703. #ifdef CONFIG_IRQSTACKS
  704. stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
  705. if (sp >= stack_page + sizeof(struct thread_struct)
  706. && sp <= stack_page + THREAD_SIZE - nbytes)
  707. return 1;
  708. stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
  709. if (sp >= stack_page + sizeof(struct thread_struct)
  710. && sp <= stack_page + THREAD_SIZE - nbytes)
  711. return 1;
  712. #endif
  713. return 0;
  714. }
  715. #ifdef CONFIG_PPC64
  716. #define MIN_STACK_FRAME 112 /* same as STACK_FRAME_OVERHEAD, in fact */
  717. #define FRAME_LR_SAVE 2
  718. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD + 288)
  719. #define REGS_MARKER 0x7265677368657265ul
  720. #define FRAME_MARKER 12
  721. #else
  722. #define MIN_STACK_FRAME 16
  723. #define FRAME_LR_SAVE 1
  724. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
  725. #define REGS_MARKER 0x72656773ul
  726. #define FRAME_MARKER 2
  727. #endif
  728. unsigned long get_wchan(struct task_struct *p)
  729. {
  730. unsigned long ip, sp;
  731. int count = 0;
  732. if (!p || p == current || p->state == TASK_RUNNING)
  733. return 0;
  734. sp = p->thread.ksp;
  735. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  736. return 0;
  737. do {
  738. sp = *(unsigned long *)sp;
  739. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  740. return 0;
  741. if (count > 0) {
  742. ip = ((unsigned long *)sp)[FRAME_LR_SAVE];
  743. if (!in_sched_functions(ip))
  744. return ip;
  745. }
  746. } while (count++ < 16);
  747. return 0;
  748. }
  749. EXPORT_SYMBOL(get_wchan);
  750. static int kstack_depth_to_print = 64;
  751. void show_stack(struct task_struct *tsk, unsigned long *stack)
  752. {
  753. unsigned long sp, ip, lr, newsp;
  754. int count = 0;
  755. int firstframe = 1;
  756. sp = (unsigned long) stack;
  757. if (tsk == NULL)
  758. tsk = current;
  759. if (sp == 0) {
  760. if (tsk == current)
  761. asm("mr %0,1" : "=r" (sp));
  762. else
  763. sp = tsk->thread.ksp;
  764. }
  765. lr = 0;
  766. printk("Call Trace:\n");
  767. do {
  768. if (!validate_sp(sp, tsk, MIN_STACK_FRAME))
  769. return;
  770. stack = (unsigned long *) sp;
  771. newsp = stack[0];
  772. ip = stack[FRAME_LR_SAVE];
  773. if (!firstframe || ip != lr) {
  774. printk("["REG"] ["REG"] ", sp, ip);
  775. print_symbol("%s", ip);
  776. if (firstframe)
  777. printk(" (unreliable)");
  778. printk("\n");
  779. }
  780. firstframe = 0;
  781. /*
  782. * See if this is an exception frame.
  783. * We look for the "regshere" marker in the current frame.
  784. */
  785. if (validate_sp(sp, tsk, INT_FRAME_SIZE)
  786. && stack[FRAME_MARKER] == REGS_MARKER) {
  787. struct pt_regs *regs = (struct pt_regs *)
  788. (sp + STACK_FRAME_OVERHEAD);
  789. printk("--- Exception: %lx", regs->trap);
  790. print_symbol(" at %s\n", regs->nip);
  791. lr = regs->link;
  792. print_symbol(" LR = %s\n", lr);
  793. firstframe = 1;
  794. }
  795. sp = newsp;
  796. } while (count++ < kstack_depth_to_print);
  797. }
  798. void dump_stack(void)
  799. {
  800. show_stack(current, NULL);
  801. }
  802. EXPORT_SYMBOL(dump_stack);