process.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. int set_dabr(unsigned long dabr)
  218. {
  219. #ifdef CONFIG_PPC_MERGE /* XXX for now */
  220. if (ppc_md.set_dabr)
  221. return ppc_md.set_dabr(dabr);
  222. #endif
  223. /* XXX should we have a CPU_FTR_HAS_DABR ? */
  224. #if defined(CONFIG_PPC64) || defined(CONFIG_6xx)
  225. mtspr(SPRN_DABR, dabr);
  226. #endif
  227. return 0;
  228. }
  229. #ifdef CONFIG_PPC64
  230. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  231. #endif
  232. static DEFINE_PER_CPU(unsigned long, current_dabr);
  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_SPE
  267. /*
  268. * If the previous thread used spe in the last quantum
  269. * (thus changing spe regs) then save them.
  270. *
  271. * On SMP we always save/restore spe regs just to avoid the
  272. * complexity of changing processors.
  273. */
  274. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
  275. giveup_spe(prev);
  276. #endif /* CONFIG_SPE */
  277. #else /* CONFIG_SMP */
  278. #ifdef CONFIG_ALTIVEC
  279. /* Avoid the trap. On smp this this never happens since
  280. * we don't set last_task_used_altivec -- Cort
  281. */
  282. if (new->thread.regs && last_task_used_altivec == new)
  283. new->thread.regs->msr |= MSR_VEC;
  284. #endif /* CONFIG_ALTIVEC */
  285. #ifdef CONFIG_SPE
  286. /* Avoid the trap. On smp this this never happens since
  287. * we don't set last_task_used_spe
  288. */
  289. if (new->thread.regs && last_task_used_spe == new)
  290. new->thread.regs->msr |= MSR_SPE;
  291. #endif /* CONFIG_SPE */
  292. #endif /* CONFIG_SMP */
  293. if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr)) {
  294. set_dabr(new->thread.dabr);
  295. __get_cpu_var(current_dabr) = new->thread.dabr;
  296. }
  297. new_thread = &new->thread;
  298. old_thread = &current->thread;
  299. #ifdef CONFIG_PPC64
  300. /*
  301. * Collect processor utilization data per process
  302. */
  303. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  304. struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
  305. long unsigned start_tb, current_tb;
  306. start_tb = old_thread->start_tb;
  307. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  308. old_thread->accum_tb += (current_tb - start_tb);
  309. new_thread->start_tb = current_tb;
  310. }
  311. #endif
  312. local_irq_save(flags);
  313. account_system_vtime(current);
  314. account_process_vtime(current);
  315. calculate_steal_time();
  316. last = _switch(old_thread, new_thread);
  317. local_irq_restore(flags);
  318. return last;
  319. }
  320. static int instructions_to_print = 16;
  321. static void show_instructions(struct pt_regs *regs)
  322. {
  323. int i;
  324. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  325. sizeof(int));
  326. printk("Instruction dump:");
  327. for (i = 0; i < instructions_to_print; i++) {
  328. int instr;
  329. if (!(i % 8))
  330. printk("\n");
  331. #if !defined(CONFIG_BOOKE)
  332. /* If executing with the IMMU off, adjust pc rather
  333. * than print XXXXXXXX.
  334. */
  335. if (!(regs->msr & MSR_IR))
  336. pc = (unsigned long)phys_to_virt(pc);
  337. #endif
  338. /* We use __get_user here *only* to avoid an OOPS on a
  339. * bad address because the pc *should* only be a
  340. * kernel address.
  341. */
  342. if (!__kernel_text_address(pc) ||
  343. __get_user(instr, (unsigned int __user *)pc)) {
  344. printk("XXXXXXXX ");
  345. } else {
  346. if (regs->nip == pc)
  347. printk("<%08x> ", instr);
  348. else
  349. printk("%08x ", instr);
  350. }
  351. pc += sizeof(int);
  352. }
  353. printk("\n");
  354. }
  355. static struct regbit {
  356. unsigned long bit;
  357. const char *name;
  358. } msr_bits[] = {
  359. {MSR_EE, "EE"},
  360. {MSR_PR, "PR"},
  361. {MSR_FP, "FP"},
  362. {MSR_ME, "ME"},
  363. {MSR_IR, "IR"},
  364. {MSR_DR, "DR"},
  365. {0, NULL}
  366. };
  367. static void printbits(unsigned long val, struct regbit *bits)
  368. {
  369. const char *sep = "";
  370. printk("<");
  371. for (; bits->bit; ++bits)
  372. if (val & bits->bit) {
  373. printk("%s%s", sep, bits->name);
  374. sep = ",";
  375. }
  376. printk(">");
  377. }
  378. #ifdef CONFIG_PPC64
  379. #define REG "%016lx"
  380. #define REGS_PER_LINE 4
  381. #define LAST_VOLATILE 13
  382. #else
  383. #define REG "%08lx"
  384. #define REGS_PER_LINE 8
  385. #define LAST_VOLATILE 12
  386. #endif
  387. void show_regs(struct pt_regs * regs)
  388. {
  389. int i, trap;
  390. printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
  391. regs->nip, regs->link, regs->ctr);
  392. printk("REGS: %p TRAP: %04lx %s (%s)\n",
  393. regs, regs->trap, print_tainted(), init_utsname()->release);
  394. printk("MSR: "REG" ", regs->msr);
  395. printbits(regs->msr, msr_bits);
  396. printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
  397. trap = TRAP(regs);
  398. if (trap == 0x300 || trap == 0x600)
  399. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  400. printk("DEAR: "REG", ESR: "REG"\n", regs->dar, regs->dsisr);
  401. #else
  402. printk("DAR: "REG", DSISR: "REG"\n", regs->dar, regs->dsisr);
  403. #endif
  404. printk("TASK = %p[%d] '%s' THREAD: %p",
  405. current, task_pid_nr(current), current->comm, task_thread_info(current));
  406. #ifdef CONFIG_SMP
  407. printk(" CPU: %d", smp_processor_id());
  408. #endif /* CONFIG_SMP */
  409. for (i = 0; i < 32; i++) {
  410. if ((i % REGS_PER_LINE) == 0)
  411. printk("\n" KERN_INFO "GPR%02d: ", i);
  412. printk(REG " ", regs->gpr[i]);
  413. if (i == LAST_VOLATILE && !FULL_REGS(regs))
  414. break;
  415. }
  416. printk("\n");
  417. #ifdef CONFIG_KALLSYMS
  418. /*
  419. * Lookup NIP late so we have the best change of getting the
  420. * above info out without failing
  421. */
  422. printk("NIP ["REG"] ", regs->nip);
  423. print_symbol("%s\n", regs->nip);
  424. printk("LR ["REG"] ", regs->link);
  425. print_symbol("%s\n", regs->link);
  426. #endif
  427. show_stack(current, (unsigned long *) regs->gpr[1]);
  428. if (!user_mode(regs))
  429. show_instructions(regs);
  430. }
  431. void exit_thread(void)
  432. {
  433. discard_lazy_cpu_state();
  434. }
  435. void flush_thread(void)
  436. {
  437. #ifdef CONFIG_PPC64
  438. struct thread_info *t = current_thread_info();
  439. if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
  440. clear_ti_thread_flag(t, TIF_ABI_PENDING);
  441. if (test_ti_thread_flag(t, TIF_32BIT))
  442. clear_ti_thread_flag(t, TIF_32BIT);
  443. else
  444. set_ti_thread_flag(t, TIF_32BIT);
  445. }
  446. #endif
  447. discard_lazy_cpu_state();
  448. if (current->thread.dabr) {
  449. current->thread.dabr = 0;
  450. set_dabr(0);
  451. }
  452. }
  453. void
  454. release_thread(struct task_struct *t)
  455. {
  456. }
  457. /*
  458. * This gets called before we allocate a new thread and copy
  459. * the current task into it.
  460. */
  461. void prepare_to_copy(struct task_struct *tsk)
  462. {
  463. flush_fp_to_thread(current);
  464. flush_altivec_to_thread(current);
  465. flush_spe_to_thread(current);
  466. }
  467. /*
  468. * Copy a thread..
  469. */
  470. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  471. unsigned long unused, struct task_struct *p,
  472. struct pt_regs *regs)
  473. {
  474. struct pt_regs *childregs, *kregs;
  475. extern void ret_from_fork(void);
  476. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  477. CHECK_FULL_REGS(regs);
  478. /* Copy registers */
  479. sp -= sizeof(struct pt_regs);
  480. childregs = (struct pt_regs *) sp;
  481. *childregs = *regs;
  482. if ((childregs->msr & MSR_PR) == 0) {
  483. /* for kernel thread, set `current' and stackptr in new task */
  484. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  485. #ifdef CONFIG_PPC32
  486. childregs->gpr[2] = (unsigned long) p;
  487. #else
  488. clear_tsk_thread_flag(p, TIF_32BIT);
  489. #endif
  490. p->thread.regs = NULL; /* no user register state */
  491. } else {
  492. childregs->gpr[1] = usp;
  493. p->thread.regs = childregs;
  494. if (clone_flags & CLONE_SETTLS) {
  495. #ifdef CONFIG_PPC64
  496. if (!test_thread_flag(TIF_32BIT))
  497. childregs->gpr[13] = childregs->gpr[6];
  498. else
  499. #endif
  500. childregs->gpr[2] = childregs->gpr[6];
  501. }
  502. }
  503. childregs->gpr[3] = 0; /* Result from fork() */
  504. sp -= STACK_FRAME_OVERHEAD;
  505. /*
  506. * The way this works is that at some point in the future
  507. * some task will call _switch to switch to the new task.
  508. * That will pop off the stack frame created below and start
  509. * the new task running at ret_from_fork. The new task will
  510. * do some house keeping and then return from the fork or clone
  511. * system call, using the stack frame created above.
  512. */
  513. sp -= sizeof(struct pt_regs);
  514. kregs = (struct pt_regs *) sp;
  515. sp -= STACK_FRAME_OVERHEAD;
  516. p->thread.ksp = sp;
  517. #ifdef CONFIG_PPC64
  518. if (cpu_has_feature(CPU_FTR_SLB)) {
  519. unsigned long sp_vsid;
  520. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  521. if (cpu_has_feature(CPU_FTR_1T_SEGMENT))
  522. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
  523. << SLB_VSID_SHIFT_1T;
  524. else
  525. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
  526. << SLB_VSID_SHIFT;
  527. sp_vsid |= SLB_VSID_KERNEL | llp;
  528. p->thread.ksp_vsid = sp_vsid;
  529. }
  530. /*
  531. * The PPC64 ABI makes use of a TOC to contain function
  532. * pointers. The function (ret_from_except) is actually a pointer
  533. * to the TOC entry. The first entry is a pointer to the actual
  534. * function.
  535. */
  536. kregs->nip = *((unsigned long *)ret_from_fork);
  537. #else
  538. kregs->nip = (unsigned long)ret_from_fork;
  539. #endif
  540. return 0;
  541. }
  542. /*
  543. * Set up a thread for executing a new program
  544. */
  545. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  546. {
  547. #ifdef CONFIG_PPC64
  548. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  549. #endif
  550. set_fs(USER_DS);
  551. /*
  552. * If we exec out of a kernel thread then thread.regs will not be
  553. * set. Do it now.
  554. */
  555. if (!current->thread.regs) {
  556. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  557. current->thread.regs = regs - 1;
  558. }
  559. memset(regs->gpr, 0, sizeof(regs->gpr));
  560. regs->ctr = 0;
  561. regs->link = 0;
  562. regs->xer = 0;
  563. regs->ccr = 0;
  564. regs->gpr[1] = sp;
  565. /*
  566. * We have just cleared all the nonvolatile GPRs, so make
  567. * FULL_REGS(regs) return true. This is necessary to allow
  568. * ptrace to examine the thread immediately after exec.
  569. */
  570. regs->trap &= ~1UL;
  571. #ifdef CONFIG_PPC32
  572. regs->mq = 0;
  573. regs->nip = start;
  574. regs->msr = MSR_USER;
  575. #else
  576. if (!test_thread_flag(TIF_32BIT)) {
  577. unsigned long entry, toc;
  578. /* start is a relocated pointer to the function descriptor for
  579. * the elf _start routine. The first entry in the function
  580. * descriptor is the entry address of _start and the second
  581. * entry is the TOC value we need to use.
  582. */
  583. __get_user(entry, (unsigned long __user *)start);
  584. __get_user(toc, (unsigned long __user *)start+1);
  585. /* Check whether the e_entry function descriptor entries
  586. * need to be relocated before we can use them.
  587. */
  588. if (load_addr != 0) {
  589. entry += load_addr;
  590. toc += load_addr;
  591. }
  592. regs->nip = entry;
  593. regs->gpr[2] = toc;
  594. regs->msr = MSR_USER64;
  595. } else {
  596. regs->nip = start;
  597. regs->gpr[2] = 0;
  598. regs->msr = MSR_USER32;
  599. }
  600. #endif
  601. discard_lazy_cpu_state();
  602. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  603. current->thread.fpscr.val = 0;
  604. #ifdef CONFIG_ALTIVEC
  605. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  606. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  607. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  608. current->thread.vrsave = 0;
  609. current->thread.used_vr = 0;
  610. #endif /* CONFIG_ALTIVEC */
  611. #ifdef CONFIG_SPE
  612. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  613. current->thread.acc = 0;
  614. current->thread.spefscr = 0;
  615. current->thread.used_spe = 0;
  616. #endif /* CONFIG_SPE */
  617. }
  618. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  619. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  620. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  621. {
  622. struct pt_regs *regs = tsk->thread.regs;
  623. /* This is a bit hairy. If we are an SPE enabled processor
  624. * (have embedded fp) we store the IEEE exception enable flags in
  625. * fpexc_mode. fpexc_mode is also used for setting FP exception
  626. * mode (asyn, precise, disabled) for 'Classic' FP. */
  627. if (val & PR_FP_EXC_SW_ENABLE) {
  628. #ifdef CONFIG_SPE
  629. if (cpu_has_feature(CPU_FTR_SPE)) {
  630. tsk->thread.fpexc_mode = val &
  631. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  632. return 0;
  633. } else {
  634. return -EINVAL;
  635. }
  636. #else
  637. return -EINVAL;
  638. #endif
  639. }
  640. /* on a CONFIG_SPE this does not hurt us. The bits that
  641. * __pack_fe01 use do not overlap with bits used for
  642. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  643. * on CONFIG_SPE implementations are reserved so writing to
  644. * them does not change anything */
  645. if (val > PR_FP_EXC_PRECISE)
  646. return -EINVAL;
  647. tsk->thread.fpexc_mode = __pack_fe01(val);
  648. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  649. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  650. | tsk->thread.fpexc_mode;
  651. return 0;
  652. }
  653. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  654. {
  655. unsigned int val;
  656. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  657. #ifdef CONFIG_SPE
  658. if (cpu_has_feature(CPU_FTR_SPE))
  659. val = tsk->thread.fpexc_mode;
  660. else
  661. return -EINVAL;
  662. #else
  663. return -EINVAL;
  664. #endif
  665. else
  666. val = __unpack_fe01(tsk->thread.fpexc_mode);
  667. return put_user(val, (unsigned int __user *) adr);
  668. }
  669. int set_endian(struct task_struct *tsk, unsigned int val)
  670. {
  671. struct pt_regs *regs = tsk->thread.regs;
  672. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  673. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  674. return -EINVAL;
  675. if (regs == NULL)
  676. return -EINVAL;
  677. if (val == PR_ENDIAN_BIG)
  678. regs->msr &= ~MSR_LE;
  679. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  680. regs->msr |= MSR_LE;
  681. else
  682. return -EINVAL;
  683. return 0;
  684. }
  685. int get_endian(struct task_struct *tsk, unsigned long adr)
  686. {
  687. struct pt_regs *regs = tsk->thread.regs;
  688. unsigned int val;
  689. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  690. !cpu_has_feature(CPU_FTR_REAL_LE))
  691. return -EINVAL;
  692. if (regs == NULL)
  693. return -EINVAL;
  694. if (regs->msr & MSR_LE) {
  695. if (cpu_has_feature(CPU_FTR_REAL_LE))
  696. val = PR_ENDIAN_LITTLE;
  697. else
  698. val = PR_ENDIAN_PPC_LITTLE;
  699. } else
  700. val = PR_ENDIAN_BIG;
  701. return put_user(val, (unsigned int __user *)adr);
  702. }
  703. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  704. {
  705. tsk->thread.align_ctl = val;
  706. return 0;
  707. }
  708. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  709. {
  710. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  711. }
  712. #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
  713. int sys_clone(unsigned long clone_flags, unsigned long usp,
  714. int __user *parent_tidp, void __user *child_threadptr,
  715. int __user *child_tidp, int p6,
  716. struct pt_regs *regs)
  717. {
  718. CHECK_FULL_REGS(regs);
  719. if (usp == 0)
  720. usp = regs->gpr[1]; /* stack pointer for child */
  721. #ifdef CONFIG_PPC64
  722. if (test_thread_flag(TIF_32BIT)) {
  723. parent_tidp = TRUNC_PTR(parent_tidp);
  724. child_tidp = TRUNC_PTR(child_tidp);
  725. }
  726. #endif
  727. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  728. }
  729. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  730. unsigned long p4, unsigned long p5, unsigned long p6,
  731. struct pt_regs *regs)
  732. {
  733. CHECK_FULL_REGS(regs);
  734. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  735. }
  736. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  737. unsigned long p4, unsigned long p5, unsigned long p6,
  738. struct pt_regs *regs)
  739. {
  740. CHECK_FULL_REGS(regs);
  741. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  742. regs, 0, NULL, NULL);
  743. }
  744. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  745. unsigned long a3, unsigned long a4, unsigned long a5,
  746. struct pt_regs *regs)
  747. {
  748. int error;
  749. char *filename;
  750. filename = getname((char __user *) a0);
  751. error = PTR_ERR(filename);
  752. if (IS_ERR(filename))
  753. goto out;
  754. flush_fp_to_thread(current);
  755. flush_altivec_to_thread(current);
  756. flush_spe_to_thread(current);
  757. error = do_execve(filename, (char __user * __user *) a1,
  758. (char __user * __user *) a2, regs);
  759. if (error == 0) {
  760. task_lock(current);
  761. current->ptrace &= ~PT_DTRACE;
  762. task_unlock(current);
  763. }
  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. #ifdef CONFIG_PPC64
  803. #define MIN_STACK_FRAME 112 /* same as STACK_FRAME_OVERHEAD, in fact */
  804. #define FRAME_LR_SAVE 2
  805. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD + 288)
  806. #define REGS_MARKER 0x7265677368657265ul
  807. #define FRAME_MARKER 12
  808. #else
  809. #define MIN_STACK_FRAME 16
  810. #define FRAME_LR_SAVE 1
  811. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
  812. #define REGS_MARKER 0x72656773ul
  813. #define FRAME_MARKER 2
  814. #endif
  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, MIN_STACK_FRAME))
  824. return 0;
  825. do {
  826. sp = *(unsigned long *)sp;
  827. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  828. return 0;
  829. if (count > 0) {
  830. ip = ((unsigned long *)sp)[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, MIN_STACK_FRAME))
  856. return;
  857. stack = (unsigned long *) sp;
  858. newsp = stack[0];
  859. ip = stack[FRAME_LR_SAVE];
  860. if (!firstframe || ip != lr) {
  861. printk("["REG"] ["REG"] ", sp, ip);
  862. print_symbol("%s", ip);
  863. if (firstframe)
  864. printk(" (unreliable)");
  865. printk("\n");
  866. }
  867. firstframe = 0;
  868. /*
  869. * See if this is an exception frame.
  870. * We look for the "regshere" marker in the current frame.
  871. */
  872. if (validate_sp(sp, tsk, INT_FRAME_SIZE)
  873. && stack[FRAME_MARKER] == REGS_MARKER) {
  874. struct pt_regs *regs = (struct pt_regs *)
  875. (sp + STACK_FRAME_OVERHEAD);
  876. printk("--- Exception: %lx", regs->trap);
  877. print_symbol(" at %s\n", regs->nip);
  878. lr = regs->link;
  879. print_symbol(" LR = %s\n", lr);
  880. firstframe = 1;
  881. }
  882. sp = newsp;
  883. } while (count++ < kstack_depth_to_print);
  884. }
  885. void dump_stack(void)
  886. {
  887. show_stack(current, NULL);
  888. }
  889. EXPORT_SYMBOL(dump_stack);
  890. #ifdef CONFIG_PPC64
  891. void ppc64_runlatch_on(void)
  892. {
  893. unsigned long ctrl;
  894. if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
  895. HMT_medium();
  896. ctrl = mfspr(SPRN_CTRLF);
  897. ctrl |= CTRL_RUNLATCH;
  898. mtspr(SPRN_CTRLT, ctrl);
  899. set_thread_flag(TIF_RUNLATCH);
  900. }
  901. }
  902. void ppc64_runlatch_off(void)
  903. {
  904. unsigned long ctrl;
  905. if (cpu_has_feature(CPU_FTR_CTRL) && test_thread_flag(TIF_RUNLATCH)) {
  906. HMT_medium();
  907. clear_thread_flag(TIF_RUNLATCH);
  908. ctrl = mfspr(SPRN_CTRLF);
  909. ctrl &= ~CTRL_RUNLATCH;
  910. mtspr(SPRN_CTRLT, ctrl);
  911. }
  912. }
  913. #endif