process.c 27 KB

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