process.c 26 KB

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