process.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. sp_vsid <<= SLB_VSID_SHIFT;
  496. sp_vsid |= SLB_VSID_KERNEL;
  497. if (cpu_has_feature(CPU_FTR_16M_PAGE))
  498. sp_vsid |= SLB_VSID_L;
  499. p->thread.ksp_vsid = sp_vsid;
  500. }
  501. /*
  502. * The PPC64 ABI makes use of a TOC to contain function
  503. * pointers. The function (ret_from_except) is actually a pointer
  504. * to the TOC entry. The first entry is a pointer to the actual
  505. * function.
  506. */
  507. kregs->nip = *((unsigned long *)ret_from_fork);
  508. #else
  509. kregs->nip = (unsigned long)ret_from_fork;
  510. p->thread.last_syscall = -1;
  511. #endif
  512. return 0;
  513. }
  514. /*
  515. * Set up a thread for executing a new program
  516. */
  517. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  518. {
  519. #ifdef CONFIG_PPC64
  520. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  521. #endif
  522. set_fs(USER_DS);
  523. /*
  524. * If we exec out of a kernel thread then thread.regs will not be
  525. * set. Do it now.
  526. */
  527. if (!current->thread.regs) {
  528. unsigned long childregs = (unsigned long)current->thread_info +
  529. THREAD_SIZE;
  530. childregs -= sizeof(struct pt_regs);
  531. current->thread.regs = (struct pt_regs *)childregs;
  532. }
  533. memset(regs->gpr, 0, sizeof(regs->gpr));
  534. regs->ctr = 0;
  535. regs->link = 0;
  536. regs->xer = 0;
  537. regs->ccr = 0;
  538. regs->gpr[1] = sp;
  539. #ifdef CONFIG_PPC32
  540. regs->mq = 0;
  541. regs->nip = start;
  542. regs->msr = MSR_USER;
  543. #else
  544. if (!test_thread_flag(TIF_32BIT)) {
  545. unsigned long entry, toc;
  546. /* start is a relocated pointer to the function descriptor for
  547. * the elf _start routine. The first entry in the function
  548. * descriptor is the entry address of _start and the second
  549. * entry is the TOC value we need to use.
  550. */
  551. __get_user(entry, (unsigned long __user *)start);
  552. __get_user(toc, (unsigned long __user *)start+1);
  553. /* Check whether the e_entry function descriptor entries
  554. * need to be relocated before we can use them.
  555. */
  556. if (load_addr != 0) {
  557. entry += load_addr;
  558. toc += load_addr;
  559. }
  560. regs->nip = entry;
  561. regs->gpr[2] = toc;
  562. regs->msr = MSR_USER64;
  563. } else {
  564. regs->nip = start;
  565. regs->gpr[2] = 0;
  566. regs->msr = MSR_USER32;
  567. }
  568. #endif
  569. #ifndef CONFIG_SMP
  570. if (last_task_used_math == current)
  571. last_task_used_math = NULL;
  572. #ifdef CONFIG_ALTIVEC
  573. if (last_task_used_altivec == current)
  574. last_task_used_altivec = NULL;
  575. #endif
  576. #ifdef CONFIG_SPE
  577. if (last_task_used_spe == current)
  578. last_task_used_spe = NULL;
  579. #endif
  580. #endif /* CONFIG_SMP */
  581. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  582. current->thread.fpscr.val = 0;
  583. #ifdef CONFIG_ALTIVEC
  584. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  585. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  586. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  587. current->thread.vrsave = 0;
  588. current->thread.used_vr = 0;
  589. #endif /* CONFIG_ALTIVEC */
  590. #ifdef CONFIG_SPE
  591. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  592. current->thread.acc = 0;
  593. current->thread.spefscr = 0;
  594. current->thread.used_spe = 0;
  595. #endif /* CONFIG_SPE */
  596. }
  597. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  598. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  599. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  600. {
  601. struct pt_regs *regs = tsk->thread.regs;
  602. /* This is a bit hairy. If we are an SPE enabled processor
  603. * (have embedded fp) we store the IEEE exception enable flags in
  604. * fpexc_mode. fpexc_mode is also used for setting FP exception
  605. * mode (asyn, precise, disabled) for 'Classic' FP. */
  606. if (val & PR_FP_EXC_SW_ENABLE) {
  607. #ifdef CONFIG_SPE
  608. tsk->thread.fpexc_mode = val &
  609. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  610. return 0;
  611. #else
  612. return -EINVAL;
  613. #endif
  614. }
  615. /* on a CONFIG_SPE this does not hurt us. The bits that
  616. * __pack_fe01 use do not overlap with bits used for
  617. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  618. * on CONFIG_SPE implementations are reserved so writing to
  619. * them does not change anything */
  620. if (val > PR_FP_EXC_PRECISE)
  621. return -EINVAL;
  622. tsk->thread.fpexc_mode = __pack_fe01(val);
  623. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  624. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  625. | tsk->thread.fpexc_mode;
  626. return 0;
  627. }
  628. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  629. {
  630. unsigned int val;
  631. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  632. #ifdef CONFIG_SPE
  633. val = tsk->thread.fpexc_mode;
  634. #else
  635. return -EINVAL;
  636. #endif
  637. else
  638. val = __unpack_fe01(tsk->thread.fpexc_mode);
  639. return put_user(val, (unsigned int __user *) adr);
  640. }
  641. #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
  642. int sys_clone(unsigned long clone_flags, unsigned long usp,
  643. int __user *parent_tidp, void __user *child_threadptr,
  644. int __user *child_tidp, int p6,
  645. struct pt_regs *regs)
  646. {
  647. CHECK_FULL_REGS(regs);
  648. if (usp == 0)
  649. usp = regs->gpr[1]; /* stack pointer for child */
  650. #ifdef CONFIG_PPC64
  651. if (test_thread_flag(TIF_32BIT)) {
  652. parent_tidp = TRUNC_PTR(parent_tidp);
  653. child_tidp = TRUNC_PTR(child_tidp);
  654. }
  655. #endif
  656. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  657. }
  658. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  659. unsigned long p4, unsigned long p5, unsigned long p6,
  660. struct pt_regs *regs)
  661. {
  662. CHECK_FULL_REGS(regs);
  663. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  664. }
  665. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  666. unsigned long p4, unsigned long p5, unsigned long p6,
  667. struct pt_regs *regs)
  668. {
  669. CHECK_FULL_REGS(regs);
  670. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  671. regs, 0, NULL, NULL);
  672. }
  673. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  674. unsigned long a3, unsigned long a4, unsigned long a5,
  675. struct pt_regs *regs)
  676. {
  677. int error;
  678. char *filename;
  679. filename = getname((char __user *) a0);
  680. error = PTR_ERR(filename);
  681. if (IS_ERR(filename))
  682. goto out;
  683. flush_fp_to_thread(current);
  684. flush_altivec_to_thread(current);
  685. flush_spe_to_thread(current);
  686. error = do_execve(filename, (char __user * __user *) a1,
  687. (char __user * __user *) a2, regs);
  688. if (error == 0) {
  689. task_lock(current);
  690. current->ptrace &= ~PT_DTRACE;
  691. task_unlock(current);
  692. }
  693. putname(filename);
  694. out:
  695. return error;
  696. }
  697. static int validate_sp(unsigned long sp, struct task_struct *p,
  698. unsigned long nbytes)
  699. {
  700. unsigned long stack_page = (unsigned long)p->thread_info;
  701. if (sp >= stack_page + sizeof(struct thread_struct)
  702. && sp <= stack_page + THREAD_SIZE - nbytes)
  703. return 1;
  704. #ifdef CONFIG_IRQSTACKS
  705. stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
  706. if (sp >= stack_page + sizeof(struct thread_struct)
  707. && sp <= stack_page + THREAD_SIZE - nbytes)
  708. return 1;
  709. stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
  710. if (sp >= stack_page + sizeof(struct thread_struct)
  711. && sp <= stack_page + THREAD_SIZE - nbytes)
  712. return 1;
  713. #endif
  714. return 0;
  715. }
  716. #ifdef CONFIG_PPC64
  717. #define MIN_STACK_FRAME 112 /* same as STACK_FRAME_OVERHEAD, in fact */
  718. #define FRAME_LR_SAVE 2
  719. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD + 288)
  720. #define REGS_MARKER 0x7265677368657265ul
  721. #define FRAME_MARKER 12
  722. #else
  723. #define MIN_STACK_FRAME 16
  724. #define FRAME_LR_SAVE 1
  725. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
  726. #define REGS_MARKER 0x72656773ul
  727. #define FRAME_MARKER 2
  728. #endif
  729. unsigned long get_wchan(struct task_struct *p)
  730. {
  731. unsigned long ip, sp;
  732. int count = 0;
  733. if (!p || p == current || p->state == TASK_RUNNING)
  734. return 0;
  735. sp = p->thread.ksp;
  736. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  737. return 0;
  738. do {
  739. sp = *(unsigned long *)sp;
  740. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  741. return 0;
  742. if (count > 0) {
  743. ip = ((unsigned long *)sp)[FRAME_LR_SAVE];
  744. if (!in_sched_functions(ip))
  745. return ip;
  746. }
  747. } while (count++ < 16);
  748. return 0;
  749. }
  750. EXPORT_SYMBOL(get_wchan);
  751. static int kstack_depth_to_print = 64;
  752. void show_stack(struct task_struct *tsk, unsigned long *stack)
  753. {
  754. unsigned long sp, ip, lr, newsp;
  755. int count = 0;
  756. int firstframe = 1;
  757. sp = (unsigned long) stack;
  758. if (tsk == NULL)
  759. tsk = current;
  760. if (sp == 0) {
  761. if (tsk == current)
  762. asm("mr %0,1" : "=r" (sp));
  763. else
  764. sp = tsk->thread.ksp;
  765. }
  766. lr = 0;
  767. printk("Call Trace:\n");
  768. do {
  769. if (!validate_sp(sp, tsk, MIN_STACK_FRAME))
  770. return;
  771. stack = (unsigned long *) sp;
  772. newsp = stack[0];
  773. ip = stack[FRAME_LR_SAVE];
  774. if (!firstframe || ip != lr) {
  775. printk("["REG"] ["REG"] ", sp, ip);
  776. print_symbol("%s", ip);
  777. if (firstframe)
  778. printk(" (unreliable)");
  779. printk("\n");
  780. }
  781. firstframe = 0;
  782. /*
  783. * See if this is an exception frame.
  784. * We look for the "regshere" marker in the current frame.
  785. */
  786. if (validate_sp(sp, tsk, INT_FRAME_SIZE)
  787. && stack[FRAME_MARKER] == REGS_MARKER) {
  788. struct pt_regs *regs = (struct pt_regs *)
  789. (sp + STACK_FRAME_OVERHEAD);
  790. printk("--- Exception: %lx", regs->trap);
  791. print_symbol(" at %s\n", regs->nip);
  792. lr = regs->link;
  793. print_symbol(" LR = %s\n", lr);
  794. firstframe = 1;
  795. }
  796. sp = newsp;
  797. } while (count++ < kstack_depth_to_print);
  798. }
  799. void dump_stack(void)
  800. {
  801. show_stack(current, NULL);
  802. }
  803. EXPORT_SYMBOL(dump_stack);