process.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. #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. #endif /* CONFIG_PPC_STD_MMU_64 */
  593. /*
  594. * The PPC64 ABI makes use of a TOC to contain function
  595. * pointers. The function (ret_from_except) is actually a pointer
  596. * to the TOC entry. The first entry is a pointer to the actual
  597. * function.
  598. */
  599. #ifdef CONFIG_PPC64
  600. kregs->nip = *((unsigned long *)ret_from_fork);
  601. #else
  602. kregs->nip = (unsigned long)ret_from_fork;
  603. #endif
  604. return 0;
  605. }
  606. /*
  607. * Set up a thread for executing a new program
  608. */
  609. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  610. {
  611. #ifdef CONFIG_PPC64
  612. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  613. #endif
  614. set_fs(USER_DS);
  615. /*
  616. * If we exec out of a kernel thread then thread.regs will not be
  617. * set. Do it now.
  618. */
  619. if (!current->thread.regs) {
  620. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  621. current->thread.regs = regs - 1;
  622. }
  623. memset(regs->gpr, 0, sizeof(regs->gpr));
  624. regs->ctr = 0;
  625. regs->link = 0;
  626. regs->xer = 0;
  627. regs->ccr = 0;
  628. regs->gpr[1] = sp;
  629. /*
  630. * We have just cleared all the nonvolatile GPRs, so make
  631. * FULL_REGS(regs) return true. This is necessary to allow
  632. * ptrace to examine the thread immediately after exec.
  633. */
  634. regs->trap &= ~1UL;
  635. #ifdef CONFIG_PPC32
  636. regs->mq = 0;
  637. regs->nip = start;
  638. regs->msr = MSR_USER;
  639. #else
  640. if (!test_thread_flag(TIF_32BIT)) {
  641. unsigned long entry, toc;
  642. /* start is a relocated pointer to the function descriptor for
  643. * the elf _start routine. The first entry in the function
  644. * descriptor is the entry address of _start and the second
  645. * entry is the TOC value we need to use.
  646. */
  647. __get_user(entry, (unsigned long __user *)start);
  648. __get_user(toc, (unsigned long __user *)start+1);
  649. /* Check whether the e_entry function descriptor entries
  650. * need to be relocated before we can use them.
  651. */
  652. if (load_addr != 0) {
  653. entry += load_addr;
  654. toc += load_addr;
  655. }
  656. regs->nip = entry;
  657. regs->gpr[2] = toc;
  658. regs->msr = MSR_USER64;
  659. } else {
  660. regs->nip = start;
  661. regs->gpr[2] = 0;
  662. regs->msr = MSR_USER32;
  663. }
  664. #endif
  665. discard_lazy_cpu_state();
  666. #ifdef CONFIG_VSX
  667. current->thread.used_vsr = 0;
  668. #endif
  669. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  670. current->thread.fpscr.val = 0;
  671. #ifdef CONFIG_ALTIVEC
  672. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  673. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  674. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  675. current->thread.vrsave = 0;
  676. current->thread.used_vr = 0;
  677. #endif /* CONFIG_ALTIVEC */
  678. #ifdef CONFIG_SPE
  679. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  680. current->thread.acc = 0;
  681. current->thread.spefscr = 0;
  682. current->thread.used_spe = 0;
  683. #endif /* CONFIG_SPE */
  684. }
  685. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  686. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  687. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  688. {
  689. struct pt_regs *regs = tsk->thread.regs;
  690. /* This is a bit hairy. If we are an SPE enabled processor
  691. * (have embedded fp) we store the IEEE exception enable flags in
  692. * fpexc_mode. fpexc_mode is also used for setting FP exception
  693. * mode (asyn, precise, disabled) for 'Classic' FP. */
  694. if (val & PR_FP_EXC_SW_ENABLE) {
  695. #ifdef CONFIG_SPE
  696. if (cpu_has_feature(CPU_FTR_SPE)) {
  697. tsk->thread.fpexc_mode = val &
  698. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  699. return 0;
  700. } else {
  701. return -EINVAL;
  702. }
  703. #else
  704. return -EINVAL;
  705. #endif
  706. }
  707. /* on a CONFIG_SPE this does not hurt us. The bits that
  708. * __pack_fe01 use do not overlap with bits used for
  709. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  710. * on CONFIG_SPE implementations are reserved so writing to
  711. * them does not change anything */
  712. if (val > PR_FP_EXC_PRECISE)
  713. return -EINVAL;
  714. tsk->thread.fpexc_mode = __pack_fe01(val);
  715. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  716. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  717. | tsk->thread.fpexc_mode;
  718. return 0;
  719. }
  720. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  721. {
  722. unsigned int val;
  723. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  724. #ifdef CONFIG_SPE
  725. if (cpu_has_feature(CPU_FTR_SPE))
  726. val = tsk->thread.fpexc_mode;
  727. else
  728. return -EINVAL;
  729. #else
  730. return -EINVAL;
  731. #endif
  732. else
  733. val = __unpack_fe01(tsk->thread.fpexc_mode);
  734. return put_user(val, (unsigned int __user *) adr);
  735. }
  736. int set_endian(struct task_struct *tsk, unsigned int val)
  737. {
  738. struct pt_regs *regs = tsk->thread.regs;
  739. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  740. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  741. return -EINVAL;
  742. if (regs == NULL)
  743. return -EINVAL;
  744. if (val == PR_ENDIAN_BIG)
  745. regs->msr &= ~MSR_LE;
  746. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  747. regs->msr |= MSR_LE;
  748. else
  749. return -EINVAL;
  750. return 0;
  751. }
  752. int get_endian(struct task_struct *tsk, unsigned long adr)
  753. {
  754. struct pt_regs *regs = tsk->thread.regs;
  755. unsigned int val;
  756. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  757. !cpu_has_feature(CPU_FTR_REAL_LE))
  758. return -EINVAL;
  759. if (regs == NULL)
  760. return -EINVAL;
  761. if (regs->msr & MSR_LE) {
  762. if (cpu_has_feature(CPU_FTR_REAL_LE))
  763. val = PR_ENDIAN_LITTLE;
  764. else
  765. val = PR_ENDIAN_PPC_LITTLE;
  766. } else
  767. val = PR_ENDIAN_BIG;
  768. return put_user(val, (unsigned int __user *)adr);
  769. }
  770. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  771. {
  772. tsk->thread.align_ctl = val;
  773. return 0;
  774. }
  775. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  776. {
  777. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  778. }
  779. #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
  780. int sys_clone(unsigned long clone_flags, unsigned long usp,
  781. int __user *parent_tidp, void __user *child_threadptr,
  782. int __user *child_tidp, int p6,
  783. struct pt_regs *regs)
  784. {
  785. CHECK_FULL_REGS(regs);
  786. if (usp == 0)
  787. usp = regs->gpr[1]; /* stack pointer for child */
  788. #ifdef CONFIG_PPC64
  789. if (test_thread_flag(TIF_32BIT)) {
  790. parent_tidp = TRUNC_PTR(parent_tidp);
  791. child_tidp = TRUNC_PTR(child_tidp);
  792. }
  793. #endif
  794. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  795. }
  796. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  797. unsigned long p4, unsigned long p5, unsigned long p6,
  798. struct pt_regs *regs)
  799. {
  800. CHECK_FULL_REGS(regs);
  801. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  802. }
  803. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  804. unsigned long p4, unsigned long p5, unsigned long p6,
  805. struct pt_regs *regs)
  806. {
  807. CHECK_FULL_REGS(regs);
  808. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  809. regs, 0, NULL, NULL);
  810. }
  811. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  812. unsigned long a3, unsigned long a4, unsigned long a5,
  813. struct pt_regs *regs)
  814. {
  815. int error;
  816. char *filename;
  817. filename = getname((char __user *) a0);
  818. error = PTR_ERR(filename);
  819. if (IS_ERR(filename))
  820. goto out;
  821. flush_fp_to_thread(current);
  822. flush_altivec_to_thread(current);
  823. flush_spe_to_thread(current);
  824. error = do_execve(filename, (char __user * __user *) a1,
  825. (char __user * __user *) a2, regs);
  826. putname(filename);
  827. out:
  828. return error;
  829. }
  830. #ifdef CONFIG_IRQSTACKS
  831. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  832. unsigned long nbytes)
  833. {
  834. unsigned long stack_page;
  835. unsigned long cpu = task_cpu(p);
  836. /*
  837. * Avoid crashing if the stack has overflowed and corrupted
  838. * task_cpu(p), which is in the thread_info struct.
  839. */
  840. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  841. stack_page = (unsigned long) hardirq_ctx[cpu];
  842. if (sp >= stack_page + sizeof(struct thread_struct)
  843. && sp <= stack_page + THREAD_SIZE - nbytes)
  844. return 1;
  845. stack_page = (unsigned long) softirq_ctx[cpu];
  846. if (sp >= stack_page + sizeof(struct thread_struct)
  847. && sp <= stack_page + THREAD_SIZE - nbytes)
  848. return 1;
  849. }
  850. return 0;
  851. }
  852. #else
  853. #define valid_irq_stack(sp, p, nb) 0
  854. #endif /* CONFIG_IRQSTACKS */
  855. int validate_sp(unsigned long sp, struct task_struct *p,
  856. unsigned long nbytes)
  857. {
  858. unsigned long stack_page = (unsigned long)task_stack_page(p);
  859. if (sp >= stack_page + sizeof(struct thread_struct)
  860. && sp <= stack_page + THREAD_SIZE - nbytes)
  861. return 1;
  862. return valid_irq_stack(sp, p, nbytes);
  863. }
  864. EXPORT_SYMBOL(validate_sp);
  865. unsigned long get_wchan(struct task_struct *p)
  866. {
  867. unsigned long ip, sp;
  868. int count = 0;
  869. if (!p || p == current || p->state == TASK_RUNNING)
  870. return 0;
  871. sp = p->thread.ksp;
  872. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  873. return 0;
  874. do {
  875. sp = *(unsigned long *)sp;
  876. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  877. return 0;
  878. if (count > 0) {
  879. ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
  880. if (!in_sched_functions(ip))
  881. return ip;
  882. }
  883. } while (count++ < 16);
  884. return 0;
  885. }
  886. static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
  887. void show_stack(struct task_struct *tsk, unsigned long *stack)
  888. {
  889. unsigned long sp, ip, lr, newsp;
  890. int count = 0;
  891. int firstframe = 1;
  892. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  893. int curr_frame = current->curr_ret_stack;
  894. extern void return_to_handler(void);
  895. unsigned long rth = (unsigned long)return_to_handler;
  896. unsigned long mrth = -1;
  897. #ifdef CONFIG_PPC64
  898. extern void mod_return_to_handler(void);
  899. rth = *(unsigned long *)rth;
  900. mrth = (unsigned long)mod_return_to_handler;
  901. mrth = *(unsigned long *)mrth;
  902. #endif
  903. #endif
  904. sp = (unsigned long) stack;
  905. if (tsk == NULL)
  906. tsk = current;
  907. if (sp == 0) {
  908. if (tsk == current)
  909. asm("mr %0,1" : "=r" (sp));
  910. else
  911. sp = tsk->thread.ksp;
  912. }
  913. lr = 0;
  914. printk("Call Trace:\n");
  915. do {
  916. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  917. return;
  918. stack = (unsigned long *) sp;
  919. newsp = stack[0];
  920. ip = stack[STACK_FRAME_LR_SAVE];
  921. if (!firstframe || ip != lr) {
  922. printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
  923. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  924. if ((ip == rth || ip == mrth) && curr_frame >= 0) {
  925. printk(" (%pS)",
  926. (void *)current->ret_stack[curr_frame].ret);
  927. curr_frame--;
  928. }
  929. #endif
  930. if (firstframe)
  931. printk(" (unreliable)");
  932. printk("\n");
  933. }
  934. firstframe = 0;
  935. /*
  936. * See if this is an exception frame.
  937. * We look for the "regshere" marker in the current frame.
  938. */
  939. if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
  940. && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  941. struct pt_regs *regs = (struct pt_regs *)
  942. (sp + STACK_FRAME_OVERHEAD);
  943. lr = regs->link;
  944. printk("--- Exception: %lx at %pS\n LR = %pS\n",
  945. regs->trap, (void *)regs->nip, (void *)lr);
  946. firstframe = 1;
  947. }
  948. sp = newsp;
  949. } while (count++ < kstack_depth_to_print);
  950. }
  951. void dump_stack(void)
  952. {
  953. show_stack(current, NULL);
  954. }
  955. EXPORT_SYMBOL(dump_stack);
  956. #ifdef CONFIG_PPC64
  957. void ppc64_runlatch_on(void)
  958. {
  959. unsigned long ctrl;
  960. if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
  961. HMT_medium();
  962. ctrl = mfspr(SPRN_CTRLF);
  963. ctrl |= CTRL_RUNLATCH;
  964. mtspr(SPRN_CTRLT, ctrl);
  965. set_thread_flag(TIF_RUNLATCH);
  966. }
  967. }
  968. void ppc64_runlatch_off(void)
  969. {
  970. unsigned long ctrl;
  971. if (cpu_has_feature(CPU_FTR_CTRL) && test_thread_flag(TIF_RUNLATCH)) {
  972. HMT_medium();
  973. clear_thread_flag(TIF_RUNLATCH);
  974. ctrl = mfspr(SPRN_CTRLF);
  975. ctrl &= ~CTRL_RUNLATCH;
  976. mtspr(SPRN_CTRLT, ctrl);
  977. }
  978. }
  979. #endif
  980. #if THREAD_SHIFT < PAGE_SHIFT
  981. static struct kmem_cache *thread_info_cache;
  982. struct thread_info *alloc_thread_info(struct task_struct *tsk)
  983. {
  984. struct thread_info *ti;
  985. ti = kmem_cache_alloc(thread_info_cache, GFP_KERNEL);
  986. if (unlikely(ti == NULL))
  987. return NULL;
  988. #ifdef CONFIG_DEBUG_STACK_USAGE
  989. memset(ti, 0, THREAD_SIZE);
  990. #endif
  991. return ti;
  992. }
  993. void free_thread_info(struct thread_info *ti)
  994. {
  995. kmem_cache_free(thread_info_cache, ti);
  996. }
  997. void thread_info_cache_init(void)
  998. {
  999. thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
  1000. THREAD_SIZE, 0, NULL);
  1001. BUG_ON(thread_info_cache == NULL);
  1002. }
  1003. #endif /* THREAD_SHIFT < PAGE_SHIFT */
  1004. unsigned long arch_align_stack(unsigned long sp)
  1005. {
  1006. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  1007. sp -= get_random_int() & ~PAGE_MASK;
  1008. return sp & ~0xf;
  1009. }
  1010. static inline unsigned long brk_rnd(void)
  1011. {
  1012. unsigned long rnd = 0;
  1013. /* 8MB for 32bit, 1GB for 64bit */
  1014. if (is_32bit_task())
  1015. rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
  1016. else
  1017. rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
  1018. return rnd << PAGE_SHIFT;
  1019. }
  1020. unsigned long arch_randomize_brk(struct mm_struct *mm)
  1021. {
  1022. unsigned long base = mm->brk;
  1023. unsigned long ret;
  1024. #ifdef CONFIG_PPC_STD_MMU_64
  1025. /*
  1026. * If we are using 1TB segments and we are allowed to randomise
  1027. * the heap, we can put it above 1TB so it is backed by a 1TB
  1028. * segment. Otherwise the heap will be in the bottom 1TB
  1029. * which always uses 256MB segments and this may result in a
  1030. * performance penalty.
  1031. */
  1032. if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
  1033. base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
  1034. #endif
  1035. ret = PAGE_ALIGN(base + brk_rnd());
  1036. if (ret < mm->brk)
  1037. return mm->brk;
  1038. return ret;
  1039. }
  1040. unsigned long randomize_et_dyn(unsigned long base)
  1041. {
  1042. unsigned long ret = PAGE_ALIGN(base + brk_rnd());
  1043. if (ret < base)
  1044. return base;
  1045. return ret;
  1046. }