process.c 26 KB

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