process.c 22 KB

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