process.c 25 KB

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