process.c 26 KB

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