process.c 33 KB

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