process.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. putname(filename);
  766. out:
  767. return error;
  768. }
  769. #ifdef CONFIG_IRQSTACKS
  770. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  771. unsigned long nbytes)
  772. {
  773. unsigned long stack_page;
  774. unsigned long cpu = task_cpu(p);
  775. /*
  776. * Avoid crashing if the stack has overflowed and corrupted
  777. * task_cpu(p), which is in the thread_info struct.
  778. */
  779. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  780. stack_page = (unsigned long) hardirq_ctx[cpu];
  781. if (sp >= stack_page + sizeof(struct thread_struct)
  782. && sp <= stack_page + THREAD_SIZE - nbytes)
  783. return 1;
  784. stack_page = (unsigned long) softirq_ctx[cpu];
  785. if (sp >= stack_page + sizeof(struct thread_struct)
  786. && sp <= stack_page + THREAD_SIZE - nbytes)
  787. return 1;
  788. }
  789. return 0;
  790. }
  791. #else
  792. #define valid_irq_stack(sp, p, nb) 0
  793. #endif /* CONFIG_IRQSTACKS */
  794. int validate_sp(unsigned long sp, struct task_struct *p,
  795. unsigned long nbytes)
  796. {
  797. unsigned long stack_page = (unsigned long)task_stack_page(p);
  798. if (sp >= stack_page + sizeof(struct thread_struct)
  799. && sp <= stack_page + THREAD_SIZE - nbytes)
  800. return 1;
  801. return valid_irq_stack(sp, p, nbytes);
  802. }
  803. #ifdef CONFIG_PPC64
  804. #define MIN_STACK_FRAME 112 /* same as STACK_FRAME_OVERHEAD, in fact */
  805. #define FRAME_LR_SAVE 2
  806. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD + 288)
  807. #define REGS_MARKER 0x7265677368657265ul
  808. #define FRAME_MARKER 12
  809. #else
  810. #define MIN_STACK_FRAME 16
  811. #define FRAME_LR_SAVE 1
  812. #define INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
  813. #define REGS_MARKER 0x72656773ul
  814. #define FRAME_MARKER 2
  815. #endif
  816. EXPORT_SYMBOL(validate_sp);
  817. unsigned long get_wchan(struct task_struct *p)
  818. {
  819. unsigned long ip, sp;
  820. int count = 0;
  821. if (!p || p == current || p->state == TASK_RUNNING)
  822. return 0;
  823. sp = p->thread.ksp;
  824. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  825. return 0;
  826. do {
  827. sp = *(unsigned long *)sp;
  828. if (!validate_sp(sp, p, MIN_STACK_FRAME))
  829. return 0;
  830. if (count > 0) {
  831. ip = ((unsigned long *)sp)[FRAME_LR_SAVE];
  832. if (!in_sched_functions(ip))
  833. return ip;
  834. }
  835. } while (count++ < 16);
  836. return 0;
  837. }
  838. static int kstack_depth_to_print = 64;
  839. void show_stack(struct task_struct *tsk, unsigned long *stack)
  840. {
  841. unsigned long sp, ip, lr, newsp;
  842. int count = 0;
  843. int firstframe = 1;
  844. sp = (unsigned long) stack;
  845. if (tsk == NULL)
  846. tsk = current;
  847. if (sp == 0) {
  848. if (tsk == current)
  849. asm("mr %0,1" : "=r" (sp));
  850. else
  851. sp = tsk->thread.ksp;
  852. }
  853. lr = 0;
  854. printk("Call Trace:\n");
  855. do {
  856. if (!validate_sp(sp, tsk, MIN_STACK_FRAME))
  857. return;
  858. stack = (unsigned long *) sp;
  859. newsp = stack[0];
  860. ip = stack[FRAME_LR_SAVE];
  861. if (!firstframe || ip != lr) {
  862. printk("["REG"] ["REG"] ", sp, ip);
  863. print_symbol("%s", ip);
  864. if (firstframe)
  865. printk(" (unreliable)");
  866. printk("\n");
  867. }
  868. firstframe = 0;
  869. /*
  870. * See if this is an exception frame.
  871. * We look for the "regshere" marker in the current frame.
  872. */
  873. if (validate_sp(sp, tsk, INT_FRAME_SIZE)
  874. && stack[FRAME_MARKER] == REGS_MARKER) {
  875. struct pt_regs *regs = (struct pt_regs *)
  876. (sp + STACK_FRAME_OVERHEAD);
  877. printk("--- Exception: %lx", regs->trap);
  878. print_symbol(" at %s\n", regs->nip);
  879. lr = regs->link;
  880. print_symbol(" LR = %s\n", lr);
  881. firstframe = 1;
  882. }
  883. sp = newsp;
  884. } while (count++ < kstack_depth_to_print);
  885. }
  886. void dump_stack(void)
  887. {
  888. show_stack(current, NULL);
  889. }
  890. EXPORT_SYMBOL(dump_stack);
  891. #ifdef CONFIG_PPC64
  892. void ppc64_runlatch_on(void)
  893. {
  894. unsigned long ctrl;
  895. if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
  896. HMT_medium();
  897. ctrl = mfspr(SPRN_CTRLF);
  898. ctrl |= CTRL_RUNLATCH;
  899. mtspr(SPRN_CTRLT, ctrl);
  900. set_thread_flag(TIF_RUNLATCH);
  901. }
  902. }
  903. void ppc64_runlatch_off(void)
  904. {
  905. unsigned long ctrl;
  906. if (cpu_has_feature(CPU_FTR_CTRL) && test_thread_flag(TIF_RUNLATCH)) {
  907. HMT_medium();
  908. clear_thread_flag(TIF_RUNLATCH);
  909. ctrl = mfspr(SPRN_CTRLF);
  910. ctrl &= ~CTRL_RUNLATCH;
  911. mtspr(SPRN_CTRLT, ctrl);
  912. }
  913. }
  914. #endif