process.c 22 KB

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