process.c 23 KB

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