process.c 26 KB

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