process.c 33 KB

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