process.c 29 KB

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