process.c 43 KB

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