process.c 22 KB

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