process.c 27 KB

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