process.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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_PPC64) || defined(CONFIG_6xx)
  259. mtspr(SPRN_DABR, dabr);
  260. #endif
  261. #if defined(CONFIG_BOOKE)
  262. mtspr(SPRN_DAC1, dabr);
  263. #endif
  264. return 0;
  265. }
  266. #ifdef CONFIG_PPC64
  267. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  268. #endif
  269. struct task_struct *__switch_to(struct task_struct *prev,
  270. struct task_struct *new)
  271. {
  272. struct thread_struct *new_thread, *old_thread;
  273. unsigned long flags;
  274. struct task_struct *last;
  275. #ifdef CONFIG_SMP
  276. /* avoid complexity of lazy save/restore of fpu
  277. * by just saving it every time we switch out if
  278. * this task used the fpu during the last quantum.
  279. *
  280. * If it tries to use the fpu again, it'll trap and
  281. * reload its fp regs. So we don't have to do a restore
  282. * every switch, just a save.
  283. * -- Cort
  284. */
  285. if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
  286. giveup_fpu(prev);
  287. #ifdef CONFIG_ALTIVEC
  288. /*
  289. * If the previous thread used altivec in the last quantum
  290. * (thus changing altivec regs) then save them.
  291. * We used to check the VRSAVE register but not all apps
  292. * set it, so we don't rely on it now (and in fact we need
  293. * to save & restore VSCR even if VRSAVE == 0). -- paulus
  294. *
  295. * On SMP we always save/restore altivec regs just to avoid the
  296. * complexity of changing processors.
  297. * -- Cort
  298. */
  299. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
  300. giveup_altivec(prev);
  301. #endif /* CONFIG_ALTIVEC */
  302. #ifdef CONFIG_VSX
  303. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
  304. /* VMX and FPU registers are already save here */
  305. __giveup_vsx(prev);
  306. #endif /* CONFIG_VSX */
  307. #ifdef CONFIG_SPE
  308. /*
  309. * If the previous thread used spe in the last quantum
  310. * (thus changing spe regs) then save them.
  311. *
  312. * On SMP we always save/restore spe regs just to avoid the
  313. * complexity of changing processors.
  314. */
  315. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
  316. giveup_spe(prev);
  317. #endif /* CONFIG_SPE */
  318. #else /* CONFIG_SMP */
  319. #ifdef CONFIG_ALTIVEC
  320. /* Avoid the trap. On smp this this never happens since
  321. * we don't set last_task_used_altivec -- Cort
  322. */
  323. if (new->thread.regs && last_task_used_altivec == new)
  324. new->thread.regs->msr |= MSR_VEC;
  325. #endif /* CONFIG_ALTIVEC */
  326. #ifdef CONFIG_VSX
  327. if (new->thread.regs && last_task_used_vsx == new)
  328. new->thread.regs->msr |= MSR_VSX;
  329. #endif /* CONFIG_VSX */
  330. #ifdef CONFIG_SPE
  331. /* Avoid the trap. On smp this this never happens since
  332. * we don't set last_task_used_spe
  333. */
  334. if (new->thread.regs && last_task_used_spe == new)
  335. new->thread.regs->msr |= MSR_SPE;
  336. #endif /* CONFIG_SPE */
  337. #endif /* CONFIG_SMP */
  338. if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr))
  339. set_dabr(new->thread.dabr);
  340. #if defined(CONFIG_BOOKE)
  341. /* If new thread DAC (HW breakpoint) is the same then leave it */
  342. if (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. #ifdef CONFIG_PPC64
  494. struct thread_info *t = current_thread_info();
  495. if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
  496. clear_ti_thread_flag(t, TIF_ABI_PENDING);
  497. if (test_ti_thread_flag(t, TIF_32BIT))
  498. clear_ti_thread_flag(t, TIF_32BIT);
  499. else
  500. set_ti_thread_flag(t, TIF_32BIT);
  501. }
  502. #endif
  503. discard_lazy_cpu_state();
  504. if (current->thread.dabr) {
  505. current->thread.dabr = 0;
  506. set_dabr(0);
  507. #if defined(CONFIG_BOOKE)
  508. current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W);
  509. #endif
  510. }
  511. }
  512. void
  513. release_thread(struct task_struct *t)
  514. {
  515. }
  516. /*
  517. * This gets called before we allocate a new thread and copy
  518. * the current task into it.
  519. */
  520. void prepare_to_copy(struct task_struct *tsk)
  521. {
  522. flush_fp_to_thread(current);
  523. flush_altivec_to_thread(current);
  524. flush_vsx_to_thread(current);
  525. flush_spe_to_thread(current);
  526. }
  527. /*
  528. * Copy a thread..
  529. */
  530. int copy_thread(unsigned long clone_flags, unsigned long usp,
  531. unsigned long unused, struct task_struct *p,
  532. struct pt_regs *regs)
  533. {
  534. struct pt_regs *childregs, *kregs;
  535. extern void ret_from_fork(void);
  536. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  537. CHECK_FULL_REGS(regs);
  538. /* Copy registers */
  539. sp -= sizeof(struct pt_regs);
  540. childregs = (struct pt_regs *) sp;
  541. *childregs = *regs;
  542. if ((childregs->msr & MSR_PR) == 0) {
  543. /* for kernel thread, set `current' and stackptr in new task */
  544. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  545. #ifdef CONFIG_PPC32
  546. childregs->gpr[2] = (unsigned long) p;
  547. #else
  548. clear_tsk_thread_flag(p, TIF_32BIT);
  549. #endif
  550. p->thread.regs = NULL; /* no user register state */
  551. } else {
  552. childregs->gpr[1] = usp;
  553. p->thread.regs = childregs;
  554. if (clone_flags & CLONE_SETTLS) {
  555. #ifdef CONFIG_PPC64
  556. if (!test_thread_flag(TIF_32BIT))
  557. childregs->gpr[13] = childregs->gpr[6];
  558. else
  559. #endif
  560. childregs->gpr[2] = childregs->gpr[6];
  561. }
  562. }
  563. childregs->gpr[3] = 0; /* Result from fork() */
  564. sp -= STACK_FRAME_OVERHEAD;
  565. /*
  566. * The way this works is that at some point in the future
  567. * some task will call _switch to switch to the new task.
  568. * That will pop off the stack frame created below and start
  569. * the new task running at ret_from_fork. The new task will
  570. * do some house keeping and then return from the fork or clone
  571. * system call, using the stack frame created above.
  572. */
  573. sp -= sizeof(struct pt_regs);
  574. kregs = (struct pt_regs *) sp;
  575. sp -= STACK_FRAME_OVERHEAD;
  576. p->thread.ksp = sp;
  577. p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
  578. _ALIGN_UP(sizeof(struct thread_info), 16);
  579. #ifdef CONFIG_PPC_STD_MMU_64
  580. if (cpu_has_feature(CPU_FTR_SLB)) {
  581. unsigned long sp_vsid;
  582. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  583. if (cpu_has_feature(CPU_FTR_1T_SEGMENT))
  584. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
  585. << SLB_VSID_SHIFT_1T;
  586. else
  587. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
  588. << SLB_VSID_SHIFT;
  589. sp_vsid |= SLB_VSID_KERNEL | llp;
  590. p->thread.ksp_vsid = sp_vsid;
  591. }
  592. /*
  593. * The PPC64 ABI makes use of a TOC to contain function
  594. * pointers. The function (ret_from_except) is actually a pointer
  595. * to the TOC entry. The first entry is a pointer to the actual
  596. * function.
  597. */
  598. kregs->nip = *((unsigned long *)ret_from_fork);
  599. #else
  600. kregs->nip = (unsigned long)ret_from_fork;
  601. #endif
  602. return 0;
  603. }
  604. /*
  605. * Set up a thread for executing a new program
  606. */
  607. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  608. {
  609. #ifdef CONFIG_PPC64
  610. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  611. #endif
  612. set_fs(USER_DS);
  613. /*
  614. * If we exec out of a kernel thread then thread.regs will not be
  615. * set. Do it now.
  616. */
  617. if (!current->thread.regs) {
  618. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  619. current->thread.regs = regs - 1;
  620. }
  621. memset(regs->gpr, 0, sizeof(regs->gpr));
  622. regs->ctr = 0;
  623. regs->link = 0;
  624. regs->xer = 0;
  625. regs->ccr = 0;
  626. regs->gpr[1] = sp;
  627. /*
  628. * We have just cleared all the nonvolatile GPRs, so make
  629. * FULL_REGS(regs) return true. This is necessary to allow
  630. * ptrace to examine the thread immediately after exec.
  631. */
  632. regs->trap &= ~1UL;
  633. #ifdef CONFIG_PPC32
  634. regs->mq = 0;
  635. regs->nip = start;
  636. regs->msr = MSR_USER;
  637. #else
  638. if (!test_thread_flag(TIF_32BIT)) {
  639. unsigned long entry, toc;
  640. /* start is a relocated pointer to the function descriptor for
  641. * the elf _start routine. The first entry in the function
  642. * descriptor is the entry address of _start and the second
  643. * entry is the TOC value we need to use.
  644. */
  645. __get_user(entry, (unsigned long __user *)start);
  646. __get_user(toc, (unsigned long __user *)start+1);
  647. /* Check whether the e_entry function descriptor entries
  648. * need to be relocated before we can use them.
  649. */
  650. if (load_addr != 0) {
  651. entry += load_addr;
  652. toc += load_addr;
  653. }
  654. regs->nip = entry;
  655. regs->gpr[2] = toc;
  656. regs->msr = MSR_USER64;
  657. } else {
  658. regs->nip = start;
  659. regs->gpr[2] = 0;
  660. regs->msr = MSR_USER32;
  661. }
  662. #endif
  663. discard_lazy_cpu_state();
  664. #ifdef CONFIG_VSX
  665. current->thread.used_vsr = 0;
  666. #endif
  667. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  668. current->thread.fpscr.val = 0;
  669. #ifdef CONFIG_ALTIVEC
  670. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  671. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  672. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  673. current->thread.vrsave = 0;
  674. current->thread.used_vr = 0;
  675. #endif /* CONFIG_ALTIVEC */
  676. #ifdef CONFIG_SPE
  677. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  678. current->thread.acc = 0;
  679. current->thread.spefscr = 0;
  680. current->thread.used_spe = 0;
  681. #endif /* CONFIG_SPE */
  682. }
  683. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  684. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  685. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  686. {
  687. struct pt_regs *regs = tsk->thread.regs;
  688. /* This is a bit hairy. If we are an SPE enabled processor
  689. * (have embedded fp) we store the IEEE exception enable flags in
  690. * fpexc_mode. fpexc_mode is also used for setting FP exception
  691. * mode (asyn, precise, disabled) for 'Classic' FP. */
  692. if (val & PR_FP_EXC_SW_ENABLE) {
  693. #ifdef CONFIG_SPE
  694. if (cpu_has_feature(CPU_FTR_SPE)) {
  695. tsk->thread.fpexc_mode = val &
  696. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  697. return 0;
  698. } else {
  699. return -EINVAL;
  700. }
  701. #else
  702. return -EINVAL;
  703. #endif
  704. }
  705. /* on a CONFIG_SPE this does not hurt us. The bits that
  706. * __pack_fe01 use do not overlap with bits used for
  707. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  708. * on CONFIG_SPE implementations are reserved so writing to
  709. * them does not change anything */
  710. if (val > PR_FP_EXC_PRECISE)
  711. return -EINVAL;
  712. tsk->thread.fpexc_mode = __pack_fe01(val);
  713. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  714. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  715. | tsk->thread.fpexc_mode;
  716. return 0;
  717. }
  718. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  719. {
  720. unsigned int val;
  721. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  722. #ifdef CONFIG_SPE
  723. if (cpu_has_feature(CPU_FTR_SPE))
  724. val = tsk->thread.fpexc_mode;
  725. else
  726. return -EINVAL;
  727. #else
  728. return -EINVAL;
  729. #endif
  730. else
  731. val = __unpack_fe01(tsk->thread.fpexc_mode);
  732. return put_user(val, (unsigned int __user *) adr);
  733. }
  734. int set_endian(struct task_struct *tsk, unsigned int val)
  735. {
  736. struct pt_regs *regs = tsk->thread.regs;
  737. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  738. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  739. return -EINVAL;
  740. if (regs == NULL)
  741. return -EINVAL;
  742. if (val == PR_ENDIAN_BIG)
  743. regs->msr &= ~MSR_LE;
  744. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  745. regs->msr |= MSR_LE;
  746. else
  747. return -EINVAL;
  748. return 0;
  749. }
  750. int get_endian(struct task_struct *tsk, unsigned long adr)
  751. {
  752. struct pt_regs *regs = tsk->thread.regs;
  753. unsigned int val;
  754. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  755. !cpu_has_feature(CPU_FTR_REAL_LE))
  756. return -EINVAL;
  757. if (regs == NULL)
  758. return -EINVAL;
  759. if (regs->msr & MSR_LE) {
  760. if (cpu_has_feature(CPU_FTR_REAL_LE))
  761. val = PR_ENDIAN_LITTLE;
  762. else
  763. val = PR_ENDIAN_PPC_LITTLE;
  764. } else
  765. val = PR_ENDIAN_BIG;
  766. return put_user(val, (unsigned int __user *)adr);
  767. }
  768. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  769. {
  770. tsk->thread.align_ctl = val;
  771. return 0;
  772. }
  773. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  774. {
  775. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  776. }
  777. #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
  778. int sys_clone(unsigned long clone_flags, unsigned long usp,
  779. int __user *parent_tidp, void __user *child_threadptr,
  780. int __user *child_tidp, int p6,
  781. struct pt_regs *regs)
  782. {
  783. CHECK_FULL_REGS(regs);
  784. if (usp == 0)
  785. usp = regs->gpr[1]; /* stack pointer for child */
  786. #ifdef CONFIG_PPC64
  787. if (test_thread_flag(TIF_32BIT)) {
  788. parent_tidp = TRUNC_PTR(parent_tidp);
  789. child_tidp = TRUNC_PTR(child_tidp);
  790. }
  791. #endif
  792. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  793. }
  794. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  795. unsigned long p4, unsigned long p5, unsigned long p6,
  796. struct pt_regs *regs)
  797. {
  798. CHECK_FULL_REGS(regs);
  799. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  800. }
  801. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  802. unsigned long p4, unsigned long p5, unsigned long p6,
  803. struct pt_regs *regs)
  804. {
  805. CHECK_FULL_REGS(regs);
  806. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  807. regs, 0, NULL, NULL);
  808. }
  809. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  810. unsigned long a3, unsigned long a4, unsigned long a5,
  811. struct pt_regs *regs)
  812. {
  813. int error;
  814. char *filename;
  815. filename = getname((char __user *) a0);
  816. error = PTR_ERR(filename);
  817. if (IS_ERR(filename))
  818. goto out;
  819. flush_fp_to_thread(current);
  820. flush_altivec_to_thread(current);
  821. flush_spe_to_thread(current);
  822. error = do_execve(filename, (char __user * __user *) a1,
  823. (char __user * __user *) a2, regs);
  824. putname(filename);
  825. out:
  826. return error;
  827. }
  828. #ifdef CONFIG_IRQSTACKS
  829. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  830. unsigned long nbytes)
  831. {
  832. unsigned long stack_page;
  833. unsigned long cpu = task_cpu(p);
  834. /*
  835. * Avoid crashing if the stack has overflowed and corrupted
  836. * task_cpu(p), which is in the thread_info struct.
  837. */
  838. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  839. stack_page = (unsigned long) hardirq_ctx[cpu];
  840. if (sp >= stack_page + sizeof(struct thread_struct)
  841. && sp <= stack_page + THREAD_SIZE - nbytes)
  842. return 1;
  843. stack_page = (unsigned long) softirq_ctx[cpu];
  844. if (sp >= stack_page + sizeof(struct thread_struct)
  845. && sp <= stack_page + THREAD_SIZE - nbytes)
  846. return 1;
  847. }
  848. return 0;
  849. }
  850. #else
  851. #define valid_irq_stack(sp, p, nb) 0
  852. #endif /* CONFIG_IRQSTACKS */
  853. int validate_sp(unsigned long sp, struct task_struct *p,
  854. unsigned long nbytes)
  855. {
  856. unsigned long stack_page = (unsigned long)task_stack_page(p);
  857. if (sp >= stack_page + sizeof(struct thread_struct)
  858. && sp <= stack_page + THREAD_SIZE - nbytes)
  859. return 1;
  860. return valid_irq_stack(sp, p, nbytes);
  861. }
  862. EXPORT_SYMBOL(validate_sp);
  863. unsigned long get_wchan(struct task_struct *p)
  864. {
  865. unsigned long ip, sp;
  866. int count = 0;
  867. if (!p || p == current || p->state == TASK_RUNNING)
  868. return 0;
  869. sp = p->thread.ksp;
  870. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  871. return 0;
  872. do {
  873. sp = *(unsigned long *)sp;
  874. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  875. return 0;
  876. if (count > 0) {
  877. ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
  878. if (!in_sched_functions(ip))
  879. return ip;
  880. }
  881. } while (count++ < 16);
  882. return 0;
  883. }
  884. static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
  885. void show_stack(struct task_struct *tsk, unsigned long *stack)
  886. {
  887. unsigned long sp, ip, lr, newsp;
  888. int count = 0;
  889. int firstframe = 1;
  890. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  891. int curr_frame = current->curr_ret_stack;
  892. extern void return_to_handler(void);
  893. unsigned long addr = (unsigned long)return_to_handler;
  894. #ifdef CONFIG_PPC64
  895. addr = *(unsigned long*)addr;
  896. #endif
  897. #endif
  898. sp = (unsigned long) stack;
  899. if (tsk == NULL)
  900. tsk = current;
  901. if (sp == 0) {
  902. if (tsk == current)
  903. asm("mr %0,1" : "=r" (sp));
  904. else
  905. sp = tsk->thread.ksp;
  906. }
  907. lr = 0;
  908. printk("Call Trace:\n");
  909. do {
  910. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  911. return;
  912. stack = (unsigned long *) sp;
  913. newsp = stack[0];
  914. ip = stack[STACK_FRAME_LR_SAVE];
  915. if (!firstframe || ip != lr) {
  916. printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
  917. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  918. if (ip == addr && curr_frame >= 0) {
  919. printk(" (%pS)",
  920. (void *)current->ret_stack[curr_frame].ret);
  921. curr_frame--;
  922. }
  923. #endif
  924. if (firstframe)
  925. printk(" (unreliable)");
  926. printk("\n");
  927. }
  928. firstframe = 0;
  929. /*
  930. * See if this is an exception frame.
  931. * We look for the "regshere" marker in the current frame.
  932. */
  933. if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
  934. && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  935. struct pt_regs *regs = (struct pt_regs *)
  936. (sp + STACK_FRAME_OVERHEAD);
  937. lr = regs->link;
  938. printk("--- Exception: %lx at %pS\n LR = %pS\n",
  939. regs->trap, (void *)regs->nip, (void *)lr);
  940. firstframe = 1;
  941. }
  942. sp = newsp;
  943. } while (count++ < kstack_depth_to_print);
  944. }
  945. void dump_stack(void)
  946. {
  947. show_stack(current, NULL);
  948. }
  949. EXPORT_SYMBOL(dump_stack);
  950. #ifdef CONFIG_PPC64
  951. void ppc64_runlatch_on(void)
  952. {
  953. unsigned long ctrl;
  954. if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
  955. HMT_medium();
  956. ctrl = mfspr(SPRN_CTRLF);
  957. ctrl |= CTRL_RUNLATCH;
  958. mtspr(SPRN_CTRLT, ctrl);
  959. set_thread_flag(TIF_RUNLATCH);
  960. }
  961. }
  962. void ppc64_runlatch_off(void)
  963. {
  964. unsigned long ctrl;
  965. if (cpu_has_feature(CPU_FTR_CTRL) && test_thread_flag(TIF_RUNLATCH)) {
  966. HMT_medium();
  967. clear_thread_flag(TIF_RUNLATCH);
  968. ctrl = mfspr(SPRN_CTRLF);
  969. ctrl &= ~CTRL_RUNLATCH;
  970. mtspr(SPRN_CTRLT, ctrl);
  971. }
  972. }
  973. #endif
  974. #if THREAD_SHIFT < PAGE_SHIFT
  975. static struct kmem_cache *thread_info_cache;
  976. struct thread_info *alloc_thread_info(struct task_struct *tsk)
  977. {
  978. struct thread_info *ti;
  979. ti = kmem_cache_alloc(thread_info_cache, GFP_KERNEL);
  980. if (unlikely(ti == NULL))
  981. return NULL;
  982. #ifdef CONFIG_DEBUG_STACK_USAGE
  983. memset(ti, 0, THREAD_SIZE);
  984. #endif
  985. return ti;
  986. }
  987. void free_thread_info(struct thread_info *ti)
  988. {
  989. kmem_cache_free(thread_info_cache, ti);
  990. }
  991. void thread_info_cache_init(void)
  992. {
  993. thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
  994. THREAD_SIZE, 0, NULL);
  995. BUG_ON(thread_info_cache == NULL);
  996. }
  997. #endif /* THREAD_SHIFT < PAGE_SHIFT */
  998. unsigned long arch_align_stack(unsigned long sp)
  999. {
  1000. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  1001. sp -= get_random_int() & ~PAGE_MASK;
  1002. return sp & ~0xf;
  1003. }
  1004. static inline unsigned long brk_rnd(void)
  1005. {
  1006. unsigned long rnd = 0;
  1007. /* 8MB for 32bit, 1GB for 64bit */
  1008. if (is_32bit_task())
  1009. rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
  1010. else
  1011. rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
  1012. return rnd << PAGE_SHIFT;
  1013. }
  1014. unsigned long arch_randomize_brk(struct mm_struct *mm)
  1015. {
  1016. unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
  1017. if (ret < mm->brk)
  1018. return mm->brk;
  1019. return ret;
  1020. }
  1021. unsigned long randomize_et_dyn(unsigned long base)
  1022. {
  1023. unsigned long ret = PAGE_ALIGN(base + brk_rnd());
  1024. if (ret < base)
  1025. return base;
  1026. return ret;
  1027. }