process.c 26 KB

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