process.c 27 KB

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