process.c 36 KB

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