process.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  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 <linux/uaccess.h>
  40. #include <linux/elf-randomize.h>
  41. #include <asm/pgtable.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 <asm/code-patching.h>
  57. #include <asm/exec.h>
  58. #include <asm/livepatch.h>
  59. #include <asm/cpu_has_feature.h>
  60. #include <asm/asm-prototypes.h>
  61. #include <linux/kprobes.h>
  62. #include <linux/kdebug.h>
  63. /* Transactional Memory debug */
  64. #ifdef TM_DEBUG_SW
  65. #define TM_DEBUG(x...) printk(KERN_INFO x)
  66. #else
  67. #define TM_DEBUG(x...) do { } while(0)
  68. #endif
  69. extern unsigned long _get_SP(void);
  70. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  71. static void check_if_tm_restore_required(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.ckpt_regs.msr = tsk->thread.regs->msr;
  83. set_thread_flag(TIF_RESTORE_TM);
  84. }
  85. }
  86. static inline bool msr_tm_active(unsigned long msr)
  87. {
  88. return MSR_TM_ACTIVE(msr);
  89. }
  90. #else
  91. static inline bool msr_tm_active(unsigned long msr) { return false; }
  92. static inline void check_if_tm_restore_required(struct task_struct *tsk) { }
  93. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  94. bool strict_msr_control;
  95. EXPORT_SYMBOL(strict_msr_control);
  96. static int __init enable_strict_msr_control(char *str)
  97. {
  98. strict_msr_control = true;
  99. pr_info("Enabling strict facility control\n");
  100. return 0;
  101. }
  102. early_param("ppc_strict_facility_enable", enable_strict_msr_control);
  103. unsigned long msr_check_and_set(unsigned long bits)
  104. {
  105. unsigned long oldmsr = mfmsr();
  106. unsigned long newmsr;
  107. newmsr = oldmsr | bits;
  108. #ifdef CONFIG_VSX
  109. if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
  110. newmsr |= MSR_VSX;
  111. #endif
  112. if (oldmsr != newmsr)
  113. mtmsr_isync(newmsr);
  114. return newmsr;
  115. }
  116. void __msr_check_and_clear(unsigned long bits)
  117. {
  118. unsigned long oldmsr = mfmsr();
  119. unsigned long newmsr;
  120. newmsr = oldmsr & ~bits;
  121. #ifdef CONFIG_VSX
  122. if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP))
  123. newmsr &= ~MSR_VSX;
  124. #endif
  125. if (oldmsr != newmsr)
  126. mtmsr_isync(newmsr);
  127. }
  128. EXPORT_SYMBOL(__msr_check_and_clear);
  129. #ifdef CONFIG_PPC_FPU
  130. void __giveup_fpu(struct task_struct *tsk)
  131. {
  132. unsigned long msr;
  133. save_fpu(tsk);
  134. msr = tsk->thread.regs->msr;
  135. msr &= ~MSR_FP;
  136. #ifdef CONFIG_VSX
  137. if (cpu_has_feature(CPU_FTR_VSX))
  138. msr &= ~MSR_VSX;
  139. #endif
  140. tsk->thread.regs->msr = msr;
  141. }
  142. void giveup_fpu(struct task_struct *tsk)
  143. {
  144. check_if_tm_restore_required(tsk);
  145. msr_check_and_set(MSR_FP);
  146. __giveup_fpu(tsk);
  147. msr_check_and_clear(MSR_FP);
  148. }
  149. EXPORT_SYMBOL(giveup_fpu);
  150. /*
  151. * Make sure the floating-point register state in the
  152. * the thread_struct is up to date for task tsk.
  153. */
  154. void flush_fp_to_thread(struct task_struct *tsk)
  155. {
  156. if (tsk->thread.regs) {
  157. /*
  158. * We need to disable preemption here because if we didn't,
  159. * another process could get scheduled after the regs->msr
  160. * test but before we have finished saving the FP registers
  161. * to the thread_struct. That process could take over the
  162. * FPU, and then when we get scheduled again we would store
  163. * bogus values for the remaining FP registers.
  164. */
  165. preempt_disable();
  166. if (tsk->thread.regs->msr & MSR_FP) {
  167. /*
  168. * This should only ever be called for current or
  169. * for a stopped child process. Since we save away
  170. * the FP register state on context switch,
  171. * there is something wrong if a stopped child appears
  172. * to still have its FP state in the CPU registers.
  173. */
  174. BUG_ON(tsk != current);
  175. giveup_fpu(tsk);
  176. }
  177. preempt_enable();
  178. }
  179. }
  180. EXPORT_SYMBOL_GPL(flush_fp_to_thread);
  181. void enable_kernel_fp(void)
  182. {
  183. unsigned long cpumsr;
  184. WARN_ON(preemptible());
  185. cpumsr = msr_check_and_set(MSR_FP);
  186. if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) {
  187. check_if_tm_restore_required(current);
  188. /*
  189. * If a thread has already been reclaimed then the
  190. * checkpointed registers are on the CPU but have definitely
  191. * been saved by the reclaim code. Don't need to and *cannot*
  192. * giveup as this would save to the 'live' structure not the
  193. * checkpointed structure.
  194. */
  195. if(!msr_tm_active(cpumsr) && msr_tm_active(current->thread.regs->msr))
  196. return;
  197. __giveup_fpu(current);
  198. }
  199. }
  200. EXPORT_SYMBOL(enable_kernel_fp);
  201. static int restore_fp(struct task_struct *tsk) {
  202. if (tsk->thread.load_fp || msr_tm_active(tsk->thread.regs->msr)) {
  203. load_fp_state(&current->thread.fp_state);
  204. current->thread.load_fp++;
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. #else
  210. static int restore_fp(struct task_struct *tsk) { return 0; }
  211. #endif /* CONFIG_PPC_FPU */
  212. #ifdef CONFIG_ALTIVEC
  213. #define loadvec(thr) ((thr).load_vec)
  214. static void __giveup_altivec(struct task_struct *tsk)
  215. {
  216. unsigned long msr;
  217. save_altivec(tsk);
  218. msr = tsk->thread.regs->msr;
  219. msr &= ~MSR_VEC;
  220. #ifdef CONFIG_VSX
  221. if (cpu_has_feature(CPU_FTR_VSX))
  222. msr &= ~MSR_VSX;
  223. #endif
  224. tsk->thread.regs->msr = msr;
  225. }
  226. void giveup_altivec(struct task_struct *tsk)
  227. {
  228. check_if_tm_restore_required(tsk);
  229. msr_check_and_set(MSR_VEC);
  230. __giveup_altivec(tsk);
  231. msr_check_and_clear(MSR_VEC);
  232. }
  233. EXPORT_SYMBOL(giveup_altivec);
  234. void enable_kernel_altivec(void)
  235. {
  236. unsigned long cpumsr;
  237. WARN_ON(preemptible());
  238. cpumsr = msr_check_and_set(MSR_VEC);
  239. if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) {
  240. check_if_tm_restore_required(current);
  241. /*
  242. * If a thread has already been reclaimed then the
  243. * checkpointed registers are on the CPU but have definitely
  244. * been saved by the reclaim code. Don't need to and *cannot*
  245. * giveup as this would save to the 'live' structure not the
  246. * checkpointed structure.
  247. */
  248. if(!msr_tm_active(cpumsr) && msr_tm_active(current->thread.regs->msr))
  249. return;
  250. __giveup_altivec(current);
  251. }
  252. }
  253. EXPORT_SYMBOL(enable_kernel_altivec);
  254. /*
  255. * Make sure the VMX/Altivec register state in the
  256. * the thread_struct is up to date for task tsk.
  257. */
  258. void flush_altivec_to_thread(struct task_struct *tsk)
  259. {
  260. if (tsk->thread.regs) {
  261. preempt_disable();
  262. if (tsk->thread.regs->msr & MSR_VEC) {
  263. BUG_ON(tsk != current);
  264. giveup_altivec(tsk);
  265. }
  266. preempt_enable();
  267. }
  268. }
  269. EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
  270. static int restore_altivec(struct task_struct *tsk)
  271. {
  272. if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
  273. (tsk->thread.load_vec || msr_tm_active(tsk->thread.regs->msr))) {
  274. load_vr_state(&tsk->thread.vr_state);
  275. tsk->thread.used_vr = 1;
  276. tsk->thread.load_vec++;
  277. return 1;
  278. }
  279. return 0;
  280. }
  281. #else
  282. #define loadvec(thr) 0
  283. static inline int restore_altivec(struct task_struct *tsk) { return 0; }
  284. #endif /* CONFIG_ALTIVEC */
  285. #ifdef CONFIG_VSX
  286. static void __giveup_vsx(struct task_struct *tsk)
  287. {
  288. if (tsk->thread.regs->msr & MSR_FP)
  289. __giveup_fpu(tsk);
  290. if (tsk->thread.regs->msr & MSR_VEC)
  291. __giveup_altivec(tsk);
  292. tsk->thread.regs->msr &= ~MSR_VSX;
  293. }
  294. static void giveup_vsx(struct task_struct *tsk)
  295. {
  296. check_if_tm_restore_required(tsk);
  297. msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
  298. __giveup_vsx(tsk);
  299. msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
  300. }
  301. static void save_vsx(struct task_struct *tsk)
  302. {
  303. if (tsk->thread.regs->msr & MSR_FP)
  304. save_fpu(tsk);
  305. if (tsk->thread.regs->msr & MSR_VEC)
  306. save_altivec(tsk);
  307. }
  308. void enable_kernel_vsx(void)
  309. {
  310. unsigned long cpumsr;
  311. WARN_ON(preemptible());
  312. cpumsr = msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
  313. if (current->thread.regs &&
  314. (current->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP))) {
  315. check_if_tm_restore_required(current);
  316. /*
  317. * If a thread has already been reclaimed then the
  318. * checkpointed registers are on the CPU but have definitely
  319. * been saved by the reclaim code. Don't need to and *cannot*
  320. * giveup as this would save to the 'live' structure not the
  321. * checkpointed structure.
  322. */
  323. if(!msr_tm_active(cpumsr) && msr_tm_active(current->thread.regs->msr))
  324. return;
  325. if (current->thread.regs->msr & MSR_FP)
  326. __giveup_fpu(current);
  327. if (current->thread.regs->msr & MSR_VEC)
  328. __giveup_altivec(current);
  329. __giveup_vsx(current);
  330. }
  331. }
  332. EXPORT_SYMBOL(enable_kernel_vsx);
  333. void flush_vsx_to_thread(struct task_struct *tsk)
  334. {
  335. if (tsk->thread.regs) {
  336. preempt_disable();
  337. if (tsk->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP)) {
  338. BUG_ON(tsk != current);
  339. giveup_vsx(tsk);
  340. }
  341. preempt_enable();
  342. }
  343. }
  344. EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
  345. static int restore_vsx(struct task_struct *tsk)
  346. {
  347. if (cpu_has_feature(CPU_FTR_VSX)) {
  348. tsk->thread.used_vsr = 1;
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. #else
  354. static inline int restore_vsx(struct task_struct *tsk) { return 0; }
  355. static inline void save_vsx(struct task_struct *tsk) { }
  356. #endif /* CONFIG_VSX */
  357. #ifdef CONFIG_SPE
  358. void giveup_spe(struct task_struct *tsk)
  359. {
  360. check_if_tm_restore_required(tsk);
  361. msr_check_and_set(MSR_SPE);
  362. __giveup_spe(tsk);
  363. msr_check_and_clear(MSR_SPE);
  364. }
  365. EXPORT_SYMBOL(giveup_spe);
  366. void enable_kernel_spe(void)
  367. {
  368. WARN_ON(preemptible());
  369. msr_check_and_set(MSR_SPE);
  370. if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) {
  371. check_if_tm_restore_required(current);
  372. __giveup_spe(current);
  373. }
  374. }
  375. EXPORT_SYMBOL(enable_kernel_spe);
  376. void flush_spe_to_thread(struct task_struct *tsk)
  377. {
  378. if (tsk->thread.regs) {
  379. preempt_disable();
  380. if (tsk->thread.regs->msr & MSR_SPE) {
  381. BUG_ON(tsk != current);
  382. tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
  383. giveup_spe(tsk);
  384. }
  385. preempt_enable();
  386. }
  387. }
  388. #endif /* CONFIG_SPE */
  389. static unsigned long msr_all_available;
  390. static int __init init_msr_all_available(void)
  391. {
  392. #ifdef CONFIG_PPC_FPU
  393. msr_all_available |= MSR_FP;
  394. #endif
  395. #ifdef CONFIG_ALTIVEC
  396. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  397. msr_all_available |= MSR_VEC;
  398. #endif
  399. #ifdef CONFIG_VSX
  400. if (cpu_has_feature(CPU_FTR_VSX))
  401. msr_all_available |= MSR_VSX;
  402. #endif
  403. #ifdef CONFIG_SPE
  404. if (cpu_has_feature(CPU_FTR_SPE))
  405. msr_all_available |= MSR_SPE;
  406. #endif
  407. return 0;
  408. }
  409. early_initcall(init_msr_all_available);
  410. void giveup_all(struct task_struct *tsk)
  411. {
  412. unsigned long usermsr;
  413. if (!tsk->thread.regs)
  414. return;
  415. usermsr = tsk->thread.regs->msr;
  416. if ((usermsr & msr_all_available) == 0)
  417. return;
  418. msr_check_and_set(msr_all_available);
  419. check_if_tm_restore_required(tsk);
  420. #ifdef CONFIG_PPC_FPU
  421. if (usermsr & MSR_FP)
  422. __giveup_fpu(tsk);
  423. #endif
  424. #ifdef CONFIG_ALTIVEC
  425. if (usermsr & MSR_VEC)
  426. __giveup_altivec(tsk);
  427. #endif
  428. #ifdef CONFIG_VSX
  429. if (usermsr & MSR_VSX)
  430. __giveup_vsx(tsk);
  431. #endif
  432. #ifdef CONFIG_SPE
  433. if (usermsr & MSR_SPE)
  434. __giveup_spe(tsk);
  435. #endif
  436. msr_check_and_clear(msr_all_available);
  437. }
  438. EXPORT_SYMBOL(giveup_all);
  439. void restore_math(struct pt_regs *regs)
  440. {
  441. unsigned long msr;
  442. if (!msr_tm_active(regs->msr) &&
  443. !current->thread.load_fp && !loadvec(current->thread))
  444. return;
  445. msr = regs->msr;
  446. msr_check_and_set(msr_all_available);
  447. /*
  448. * Only reload if the bit is not set in the user MSR, the bit BEING set
  449. * indicates that the registers are hot
  450. */
  451. if ((!(msr & MSR_FP)) && restore_fp(current))
  452. msr |= MSR_FP | current->thread.fpexc_mode;
  453. if ((!(msr & MSR_VEC)) && restore_altivec(current))
  454. msr |= MSR_VEC;
  455. if ((msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC) &&
  456. restore_vsx(current)) {
  457. msr |= MSR_VSX;
  458. }
  459. msr_check_and_clear(msr_all_available);
  460. regs->msr = msr;
  461. }
  462. void save_all(struct task_struct *tsk)
  463. {
  464. unsigned long usermsr;
  465. if (!tsk->thread.regs)
  466. return;
  467. usermsr = tsk->thread.regs->msr;
  468. if ((usermsr & msr_all_available) == 0)
  469. return;
  470. msr_check_and_set(msr_all_available);
  471. /*
  472. * Saving the way the register space is in hardware, save_vsx boils
  473. * down to a save_fpu() and save_altivec()
  474. */
  475. if (usermsr & MSR_VSX) {
  476. save_vsx(tsk);
  477. } else {
  478. if (usermsr & MSR_FP)
  479. save_fpu(tsk);
  480. if (usermsr & MSR_VEC)
  481. save_altivec(tsk);
  482. }
  483. if (usermsr & MSR_SPE)
  484. __giveup_spe(tsk);
  485. msr_check_and_clear(msr_all_available);
  486. }
  487. void flush_all_to_thread(struct task_struct *tsk)
  488. {
  489. if (tsk->thread.regs) {
  490. preempt_disable();
  491. BUG_ON(tsk != current);
  492. save_all(tsk);
  493. #ifdef CONFIG_SPE
  494. if (tsk->thread.regs->msr & MSR_SPE)
  495. tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
  496. #endif
  497. preempt_enable();
  498. }
  499. }
  500. EXPORT_SYMBOL(flush_all_to_thread);
  501. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  502. void do_send_trap(struct pt_regs *regs, unsigned long address,
  503. unsigned long error_code, int signal_code, int breakpt)
  504. {
  505. siginfo_t info;
  506. current->thread.trap_nr = signal_code;
  507. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  508. 11, SIGSEGV) == NOTIFY_STOP)
  509. return;
  510. /* Deliver the signal to userspace */
  511. info.si_signo = SIGTRAP;
  512. info.si_errno = breakpt; /* breakpoint or watchpoint id */
  513. info.si_code = signal_code;
  514. info.si_addr = (void __user *)address;
  515. force_sig_info(SIGTRAP, &info, current);
  516. }
  517. #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
  518. void do_break (struct pt_regs *regs, unsigned long address,
  519. unsigned long error_code)
  520. {
  521. siginfo_t info;
  522. current->thread.trap_nr = TRAP_HWBKPT;
  523. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  524. 11, SIGSEGV) == NOTIFY_STOP)
  525. return;
  526. if (debugger_break_match(regs))
  527. return;
  528. /* Clear the breakpoint */
  529. hw_breakpoint_disable();
  530. /* Deliver the signal to userspace */
  531. info.si_signo = SIGTRAP;
  532. info.si_errno = 0;
  533. info.si_code = TRAP_HWBKPT;
  534. info.si_addr = (void __user *)address;
  535. force_sig_info(SIGTRAP, &info, current);
  536. }
  537. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  538. static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk);
  539. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  540. /*
  541. * Set the debug registers back to their default "safe" values.
  542. */
  543. static void set_debug_reg_defaults(struct thread_struct *thread)
  544. {
  545. thread->debug.iac1 = thread->debug.iac2 = 0;
  546. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  547. thread->debug.iac3 = thread->debug.iac4 = 0;
  548. #endif
  549. thread->debug.dac1 = thread->debug.dac2 = 0;
  550. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  551. thread->debug.dvc1 = thread->debug.dvc2 = 0;
  552. #endif
  553. thread->debug.dbcr0 = 0;
  554. #ifdef CONFIG_BOOKE
  555. /*
  556. * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
  557. */
  558. thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
  559. DBCR1_IAC3US | DBCR1_IAC4US;
  560. /*
  561. * Force Data Address Compare User/Supervisor bits to be User-only
  562. * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
  563. */
  564. thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
  565. #else
  566. thread->debug.dbcr1 = 0;
  567. #endif
  568. }
  569. static void prime_debug_regs(struct debug_reg *debug)
  570. {
  571. /*
  572. * We could have inherited MSR_DE from userspace, since
  573. * it doesn't get cleared on exception entry. Make sure
  574. * MSR_DE is clear before we enable any debug events.
  575. */
  576. mtmsr(mfmsr() & ~MSR_DE);
  577. mtspr(SPRN_IAC1, debug->iac1);
  578. mtspr(SPRN_IAC2, debug->iac2);
  579. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  580. mtspr(SPRN_IAC3, debug->iac3);
  581. mtspr(SPRN_IAC4, debug->iac4);
  582. #endif
  583. mtspr(SPRN_DAC1, debug->dac1);
  584. mtspr(SPRN_DAC2, debug->dac2);
  585. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  586. mtspr(SPRN_DVC1, debug->dvc1);
  587. mtspr(SPRN_DVC2, debug->dvc2);
  588. #endif
  589. mtspr(SPRN_DBCR0, debug->dbcr0);
  590. mtspr(SPRN_DBCR1, debug->dbcr1);
  591. #ifdef CONFIG_BOOKE
  592. mtspr(SPRN_DBCR2, debug->dbcr2);
  593. #endif
  594. }
  595. /*
  596. * Unless neither the old or new thread are making use of the
  597. * debug registers, set the debug registers from the values
  598. * stored in the new thread.
  599. */
  600. void switch_booke_debug_regs(struct debug_reg *new_debug)
  601. {
  602. if ((current->thread.debug.dbcr0 & DBCR0_IDM)
  603. || (new_debug->dbcr0 & DBCR0_IDM))
  604. prime_debug_regs(new_debug);
  605. }
  606. EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
  607. #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
  608. #ifndef CONFIG_HAVE_HW_BREAKPOINT
  609. static void set_debug_reg_defaults(struct thread_struct *thread)
  610. {
  611. thread->hw_brk.address = 0;
  612. thread->hw_brk.type = 0;
  613. set_breakpoint(&thread->hw_brk);
  614. }
  615. #endif /* !CONFIG_HAVE_HW_BREAKPOINT */
  616. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  617. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  618. static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
  619. {
  620. mtspr(SPRN_DAC1, dabr);
  621. #ifdef CONFIG_PPC_47x
  622. isync();
  623. #endif
  624. return 0;
  625. }
  626. #elif defined(CONFIG_PPC_BOOK3S)
  627. static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
  628. {
  629. mtspr(SPRN_DABR, dabr);
  630. if (cpu_has_feature(CPU_FTR_DABRX))
  631. mtspr(SPRN_DABRX, dabrx);
  632. return 0;
  633. }
  634. #else
  635. static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
  636. {
  637. return -EINVAL;
  638. }
  639. #endif
  640. static inline int set_dabr(struct arch_hw_breakpoint *brk)
  641. {
  642. unsigned long dabr, dabrx;
  643. dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
  644. dabrx = ((brk->type >> 3) & 0x7);
  645. if (ppc_md.set_dabr)
  646. return ppc_md.set_dabr(dabr, dabrx);
  647. return __set_dabr(dabr, dabrx);
  648. }
  649. static inline int set_dawr(struct arch_hw_breakpoint *brk)
  650. {
  651. unsigned long dawr, dawrx, mrd;
  652. dawr = brk->address;
  653. dawrx = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE)) \
  654. << (63 - 58); //* read/write bits */
  655. dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) \
  656. << (63 - 59); //* translate */
  657. dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) \
  658. >> 3; //* PRIM bits */
  659. /* dawr length is stored in field MDR bits 48:53. Matches range in
  660. doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
  661. 0b111111=64DW.
  662. brk->len is in bytes.
  663. This aligns up to double word size, shifts and does the bias.
  664. */
  665. mrd = ((brk->len + 7) >> 3) - 1;
  666. dawrx |= (mrd & 0x3f) << (63 - 53);
  667. if (ppc_md.set_dawr)
  668. return ppc_md.set_dawr(dawr, dawrx);
  669. mtspr(SPRN_DAWR, dawr);
  670. mtspr(SPRN_DAWRX, dawrx);
  671. return 0;
  672. }
  673. void __set_breakpoint(struct arch_hw_breakpoint *brk)
  674. {
  675. memcpy(this_cpu_ptr(&current_brk), brk, sizeof(*brk));
  676. if (cpu_has_feature(CPU_FTR_DAWR))
  677. set_dawr(brk);
  678. else
  679. set_dabr(brk);
  680. }
  681. void set_breakpoint(struct arch_hw_breakpoint *brk)
  682. {
  683. preempt_disable();
  684. __set_breakpoint(brk);
  685. preempt_enable();
  686. }
  687. #ifdef CONFIG_PPC64
  688. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  689. #endif
  690. static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
  691. struct arch_hw_breakpoint *b)
  692. {
  693. if (a->address != b->address)
  694. return false;
  695. if (a->type != b->type)
  696. return false;
  697. if (a->len != b->len)
  698. return false;
  699. return true;
  700. }
  701. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  702. static inline bool tm_enabled(struct task_struct *tsk)
  703. {
  704. return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM);
  705. }
  706. static void tm_reclaim_thread(struct thread_struct *thr,
  707. struct thread_info *ti, uint8_t cause)
  708. {
  709. /*
  710. * Use the current MSR TM suspended bit to track if we have
  711. * checkpointed state outstanding.
  712. * On signal delivery, we'd normally reclaim the checkpointed
  713. * state to obtain stack pointer (see:get_tm_stackpointer()).
  714. * This will then directly return to userspace without going
  715. * through __switch_to(). However, if the stack frame is bad,
  716. * we need to exit this thread which calls __switch_to() which
  717. * will again attempt to reclaim the already saved tm state.
  718. * Hence we need to check that we've not already reclaimed
  719. * this state.
  720. * We do this using the current MSR, rather tracking it in
  721. * some specific thread_struct bit, as it has the additional
  722. * benefit of checking for a potential TM bad thing exception.
  723. */
  724. if (!MSR_TM_SUSPENDED(mfmsr()))
  725. return;
  726. /*
  727. * If we are in a transaction and FP is off then we can't have
  728. * used FP inside that transaction. Hence the checkpointed
  729. * state is the same as the live state. We need to copy the
  730. * live state to the checkpointed state so that when the
  731. * transaction is restored, the checkpointed state is correct
  732. * and the aborted transaction sees the correct state. We use
  733. * ckpt_regs.msr here as that's what tm_reclaim will use to
  734. * determine if it's going to write the checkpointed state or
  735. * not. So either this will write the checkpointed registers,
  736. * or reclaim will. Similarly for VMX.
  737. */
  738. if ((thr->ckpt_regs.msr & MSR_FP) == 0)
  739. memcpy(&thr->ckfp_state, &thr->fp_state,
  740. sizeof(struct thread_fp_state));
  741. if ((thr->ckpt_regs.msr & MSR_VEC) == 0)
  742. memcpy(&thr->ckvr_state, &thr->vr_state,
  743. sizeof(struct thread_vr_state));
  744. giveup_all(container_of(thr, struct task_struct, thread));
  745. tm_reclaim(thr, thr->ckpt_regs.msr, cause);
  746. }
  747. void tm_reclaim_current(uint8_t cause)
  748. {
  749. tm_enable();
  750. tm_reclaim_thread(&current->thread, current_thread_info(), cause);
  751. }
  752. static inline void tm_reclaim_task(struct task_struct *tsk)
  753. {
  754. /* We have to work out if we're switching from/to a task that's in the
  755. * middle of a transaction.
  756. *
  757. * In switching we need to maintain a 2nd register state as
  758. * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
  759. * checkpointed (tbegin) state in ckpt_regs, ckfp_state and
  760. * ckvr_state
  761. *
  762. * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
  763. */
  764. struct thread_struct *thr = &tsk->thread;
  765. if (!thr->regs)
  766. return;
  767. if (!MSR_TM_ACTIVE(thr->regs->msr))
  768. goto out_and_saveregs;
  769. TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
  770. "ccr=%lx, msr=%lx, trap=%lx)\n",
  771. tsk->pid, thr->regs->nip,
  772. thr->regs->ccr, thr->regs->msr,
  773. thr->regs->trap);
  774. tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
  775. TM_DEBUG("--- tm_reclaim on pid %d complete\n",
  776. tsk->pid);
  777. out_and_saveregs:
  778. /* Always save the regs here, even if a transaction's not active.
  779. * This context-switches a thread's TM info SPRs. We do it here to
  780. * be consistent with the restore path (in recheckpoint) which
  781. * cannot happen later in _switch().
  782. */
  783. tm_save_sprs(thr);
  784. }
  785. extern void __tm_recheckpoint(struct thread_struct *thread,
  786. unsigned long orig_msr);
  787. void tm_recheckpoint(struct thread_struct *thread,
  788. unsigned long orig_msr)
  789. {
  790. unsigned long flags;
  791. if (!(thread->regs->msr & MSR_TM))
  792. return;
  793. /* We really can't be interrupted here as the TEXASR registers can't
  794. * change and later in the trecheckpoint code, we have a userspace R1.
  795. * So let's hard disable over this region.
  796. */
  797. local_irq_save(flags);
  798. hard_irq_disable();
  799. /* The TM SPRs are restored here, so that TEXASR.FS can be set
  800. * before the trecheckpoint and no explosion occurs.
  801. */
  802. tm_restore_sprs(thread);
  803. __tm_recheckpoint(thread, orig_msr);
  804. local_irq_restore(flags);
  805. }
  806. static inline void tm_recheckpoint_new_task(struct task_struct *new)
  807. {
  808. unsigned long msr;
  809. if (!cpu_has_feature(CPU_FTR_TM))
  810. return;
  811. /* Recheckpoint the registers of the thread we're about to switch to.
  812. *
  813. * If the task was using FP, we non-lazily reload both the original and
  814. * the speculative FP register states. This is because the kernel
  815. * doesn't see if/when a TM rollback occurs, so if we take an FP
  816. * unavailable later, we are unable to determine which set of FP regs
  817. * need to be restored.
  818. */
  819. if (!tm_enabled(new))
  820. return;
  821. if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
  822. tm_restore_sprs(&new->thread);
  823. return;
  824. }
  825. msr = new->thread.ckpt_regs.msr;
  826. /* Recheckpoint to restore original checkpointed register state. */
  827. TM_DEBUG("*** tm_recheckpoint of pid %d "
  828. "(new->msr 0x%lx, new->origmsr 0x%lx)\n",
  829. new->pid, new->thread.regs->msr, msr);
  830. tm_recheckpoint(&new->thread, msr);
  831. /*
  832. * The checkpointed state has been restored but the live state has
  833. * not, ensure all the math functionality is turned off to trigger
  834. * restore_math() to reload.
  835. */
  836. new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX);
  837. TM_DEBUG("*** tm_recheckpoint of pid %d complete "
  838. "(kernel msr 0x%lx)\n",
  839. new->pid, mfmsr());
  840. }
  841. static inline void __switch_to_tm(struct task_struct *prev,
  842. struct task_struct *new)
  843. {
  844. if (cpu_has_feature(CPU_FTR_TM)) {
  845. if (tm_enabled(prev) || tm_enabled(new))
  846. tm_enable();
  847. if (tm_enabled(prev)) {
  848. prev->thread.load_tm++;
  849. tm_reclaim_task(prev);
  850. if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0)
  851. prev->thread.regs->msr &= ~MSR_TM;
  852. }
  853. tm_recheckpoint_new_task(new);
  854. }
  855. }
  856. /*
  857. * This is called if we are on the way out to userspace and the
  858. * TIF_RESTORE_TM flag is set. It checks if we need to reload
  859. * FP and/or vector state and does so if necessary.
  860. * If userspace is inside a transaction (whether active or
  861. * suspended) and FP/VMX/VSX instructions have ever been enabled
  862. * inside that transaction, then we have to keep them enabled
  863. * and keep the FP/VMX/VSX state loaded while ever the transaction
  864. * continues. The reason is that if we didn't, and subsequently
  865. * got a FP/VMX/VSX unavailable interrupt inside a transaction,
  866. * we don't know whether it's the same transaction, and thus we
  867. * don't know which of the checkpointed state and the transactional
  868. * state to use.
  869. */
  870. void restore_tm_state(struct pt_regs *regs)
  871. {
  872. unsigned long msr_diff;
  873. /*
  874. * This is the only moment we should clear TIF_RESTORE_TM as
  875. * it is here that ckpt_regs.msr and pt_regs.msr become the same
  876. * again, anything else could lead to an incorrect ckpt_msr being
  877. * saved and therefore incorrect signal contexts.
  878. */
  879. clear_thread_flag(TIF_RESTORE_TM);
  880. if (!MSR_TM_ACTIVE(regs->msr))
  881. return;
  882. msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
  883. msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
  884. /* Ensure that restore_math() will restore */
  885. if (msr_diff & MSR_FP)
  886. current->thread.load_fp = 1;
  887. #ifdef CONFIG_ALTIVEC
  888. if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC)
  889. current->thread.load_vec = 1;
  890. #endif
  891. restore_math(regs);
  892. regs->msr |= msr_diff;
  893. }
  894. #else
  895. #define tm_recheckpoint_new_task(new)
  896. #define __switch_to_tm(prev, new)
  897. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  898. static inline void save_sprs(struct thread_struct *t)
  899. {
  900. #ifdef CONFIG_ALTIVEC
  901. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  902. t->vrsave = mfspr(SPRN_VRSAVE);
  903. #endif
  904. #ifdef CONFIG_PPC_BOOK3S_64
  905. if (cpu_has_feature(CPU_FTR_DSCR))
  906. t->dscr = mfspr(SPRN_DSCR);
  907. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  908. t->bescr = mfspr(SPRN_BESCR);
  909. t->ebbhr = mfspr(SPRN_EBBHR);
  910. t->ebbrr = mfspr(SPRN_EBBRR);
  911. t->fscr = mfspr(SPRN_FSCR);
  912. /*
  913. * Note that the TAR is not available for use in the kernel.
  914. * (To provide this, the TAR should be backed up/restored on
  915. * exception entry/exit instead, and be in pt_regs. FIXME,
  916. * this should be in pt_regs anyway (for debug).)
  917. */
  918. t->tar = mfspr(SPRN_TAR);
  919. }
  920. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  921. /* Conditionally save Load Monitor registers, if enabled */
  922. if (t->fscr & FSCR_LM) {
  923. t->lmrr = mfspr(SPRN_LMRR);
  924. t->lmser = mfspr(SPRN_LMSER);
  925. }
  926. }
  927. #endif
  928. }
  929. static inline void restore_sprs(struct thread_struct *old_thread,
  930. struct thread_struct *new_thread)
  931. {
  932. #ifdef CONFIG_ALTIVEC
  933. if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
  934. old_thread->vrsave != new_thread->vrsave)
  935. mtspr(SPRN_VRSAVE, new_thread->vrsave);
  936. #endif
  937. #ifdef CONFIG_PPC_BOOK3S_64
  938. if (cpu_has_feature(CPU_FTR_DSCR)) {
  939. u64 dscr = get_paca()->dscr_default;
  940. if (new_thread->dscr_inherit)
  941. dscr = new_thread->dscr;
  942. if (old_thread->dscr != dscr)
  943. mtspr(SPRN_DSCR, dscr);
  944. }
  945. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  946. if (old_thread->bescr != new_thread->bescr)
  947. mtspr(SPRN_BESCR, new_thread->bescr);
  948. if (old_thread->ebbhr != new_thread->ebbhr)
  949. mtspr(SPRN_EBBHR, new_thread->ebbhr);
  950. if (old_thread->ebbrr != new_thread->ebbrr)
  951. mtspr(SPRN_EBBRR, new_thread->ebbrr);
  952. if (old_thread->fscr != new_thread->fscr)
  953. mtspr(SPRN_FSCR, new_thread->fscr);
  954. if (old_thread->tar != new_thread->tar)
  955. mtspr(SPRN_TAR, new_thread->tar);
  956. }
  957. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  958. /* Conditionally restore Load Monitor registers, if enabled */
  959. if (new_thread->fscr & FSCR_LM) {
  960. if (old_thread->lmrr != new_thread->lmrr)
  961. mtspr(SPRN_LMRR, new_thread->lmrr);
  962. if (old_thread->lmser != new_thread->lmser)
  963. mtspr(SPRN_LMSER, new_thread->lmser);
  964. }
  965. }
  966. #endif
  967. }
  968. struct task_struct *__switch_to(struct task_struct *prev,
  969. struct task_struct *new)
  970. {
  971. struct thread_struct *new_thread, *old_thread;
  972. struct task_struct *last;
  973. #ifdef CONFIG_PPC_BOOK3S_64
  974. struct ppc64_tlb_batch *batch;
  975. #endif
  976. new_thread = &new->thread;
  977. old_thread = &current->thread;
  978. WARN_ON(!irqs_disabled());
  979. #ifdef CONFIG_PPC64
  980. /*
  981. * Collect processor utilization data per process
  982. */
  983. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  984. struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
  985. long unsigned start_tb, current_tb;
  986. start_tb = old_thread->start_tb;
  987. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  988. old_thread->accum_tb += (current_tb - start_tb);
  989. new_thread->start_tb = current_tb;
  990. }
  991. #endif /* CONFIG_PPC64 */
  992. #ifdef CONFIG_PPC_STD_MMU_64
  993. batch = this_cpu_ptr(&ppc64_tlb_batch);
  994. if (batch->active) {
  995. current_thread_info()->local_flags |= _TLF_LAZY_MMU;
  996. if (batch->index)
  997. __flush_tlb_pending(batch);
  998. batch->active = 0;
  999. }
  1000. #endif /* CONFIG_PPC_STD_MMU_64 */
  1001. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1002. switch_booke_debug_regs(&new->thread.debug);
  1003. #else
  1004. /*
  1005. * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
  1006. * schedule DABR
  1007. */
  1008. #ifndef CONFIG_HAVE_HW_BREAKPOINT
  1009. if (unlikely(!hw_brk_match(this_cpu_ptr(&current_brk), &new->thread.hw_brk)))
  1010. __set_breakpoint(&new->thread.hw_brk);
  1011. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  1012. #endif
  1013. /*
  1014. * We need to save SPRs before treclaim/trecheckpoint as these will
  1015. * change a number of them.
  1016. */
  1017. save_sprs(&prev->thread);
  1018. /* Save FPU, Altivec, VSX and SPE state */
  1019. giveup_all(prev);
  1020. __switch_to_tm(prev, new);
  1021. /*
  1022. * We can't take a PMU exception inside _switch() since there is a
  1023. * window where the kernel stack SLB and the kernel stack are out
  1024. * of sync. Hard disable here.
  1025. */
  1026. hard_irq_disable();
  1027. /*
  1028. * Call restore_sprs() before calling _switch(). If we move it after
  1029. * _switch() then we miss out on calling it for new tasks. The reason
  1030. * for this is we manually create a stack frame for new tasks that
  1031. * directly returns through ret_from_fork() or
  1032. * ret_from_kernel_thread(). See copy_thread() for details.
  1033. */
  1034. restore_sprs(old_thread, new_thread);
  1035. last = _switch(old_thread, new_thread);
  1036. #ifdef CONFIG_PPC_STD_MMU_64
  1037. if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
  1038. current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
  1039. batch = this_cpu_ptr(&ppc64_tlb_batch);
  1040. batch->active = 1;
  1041. }
  1042. if (current_thread_info()->task->thread.regs)
  1043. restore_math(current_thread_info()->task->thread.regs);
  1044. #endif /* CONFIG_PPC_STD_MMU_64 */
  1045. return last;
  1046. }
  1047. static int instructions_to_print = 16;
  1048. static void show_instructions(struct pt_regs *regs)
  1049. {
  1050. int i;
  1051. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  1052. sizeof(int));
  1053. printk("Instruction dump:");
  1054. for (i = 0; i < instructions_to_print; i++) {
  1055. int instr;
  1056. if (!(i % 8))
  1057. pr_cont("\n");
  1058. #if !defined(CONFIG_BOOKE)
  1059. /* If executing with the IMMU off, adjust pc rather
  1060. * than print XXXXXXXX.
  1061. */
  1062. if (!(regs->msr & MSR_IR))
  1063. pc = (unsigned long)phys_to_virt(pc);
  1064. #endif
  1065. if (!__kernel_text_address(pc) ||
  1066. probe_kernel_address((unsigned int __user *)pc, instr)) {
  1067. pr_cont("XXXXXXXX ");
  1068. } else {
  1069. if (regs->nip == pc)
  1070. pr_cont("<%08x> ", instr);
  1071. else
  1072. pr_cont("%08x ", instr);
  1073. }
  1074. pc += sizeof(int);
  1075. }
  1076. pr_cont("\n");
  1077. }
  1078. struct regbit {
  1079. unsigned long bit;
  1080. const char *name;
  1081. };
  1082. static struct regbit msr_bits[] = {
  1083. #if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
  1084. {MSR_SF, "SF"},
  1085. {MSR_HV, "HV"},
  1086. #endif
  1087. {MSR_VEC, "VEC"},
  1088. {MSR_VSX, "VSX"},
  1089. #ifdef CONFIG_BOOKE
  1090. {MSR_CE, "CE"},
  1091. #endif
  1092. {MSR_EE, "EE"},
  1093. {MSR_PR, "PR"},
  1094. {MSR_FP, "FP"},
  1095. {MSR_ME, "ME"},
  1096. #ifdef CONFIG_BOOKE
  1097. {MSR_DE, "DE"},
  1098. #else
  1099. {MSR_SE, "SE"},
  1100. {MSR_BE, "BE"},
  1101. #endif
  1102. {MSR_IR, "IR"},
  1103. {MSR_DR, "DR"},
  1104. {MSR_PMM, "PMM"},
  1105. #ifndef CONFIG_BOOKE
  1106. {MSR_RI, "RI"},
  1107. {MSR_LE, "LE"},
  1108. #endif
  1109. {0, NULL}
  1110. };
  1111. static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
  1112. {
  1113. const char *s = "";
  1114. for (; bits->bit; ++bits)
  1115. if (val & bits->bit) {
  1116. pr_cont("%s%s", s, bits->name);
  1117. s = sep;
  1118. }
  1119. }
  1120. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1121. static struct regbit msr_tm_bits[] = {
  1122. {MSR_TS_T, "T"},
  1123. {MSR_TS_S, "S"},
  1124. {MSR_TM, "E"},
  1125. {0, NULL}
  1126. };
  1127. static void print_tm_bits(unsigned long val)
  1128. {
  1129. /*
  1130. * This only prints something if at least one of the TM bit is set.
  1131. * Inside the TM[], the output means:
  1132. * E: Enabled (bit 32)
  1133. * S: Suspended (bit 33)
  1134. * T: Transactional (bit 34)
  1135. */
  1136. if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
  1137. pr_cont(",TM[");
  1138. print_bits(val, msr_tm_bits, "");
  1139. pr_cont("]");
  1140. }
  1141. }
  1142. #else
  1143. static void print_tm_bits(unsigned long val) {}
  1144. #endif
  1145. static void print_msr_bits(unsigned long val)
  1146. {
  1147. pr_cont("<");
  1148. print_bits(val, msr_bits, ",");
  1149. print_tm_bits(val);
  1150. pr_cont(">");
  1151. }
  1152. #ifdef CONFIG_PPC64
  1153. #define REG "%016lx"
  1154. #define REGS_PER_LINE 4
  1155. #define LAST_VOLATILE 13
  1156. #else
  1157. #define REG "%08lx"
  1158. #define REGS_PER_LINE 8
  1159. #define LAST_VOLATILE 12
  1160. #endif
  1161. void show_regs(struct pt_regs * regs)
  1162. {
  1163. int i, trap;
  1164. show_regs_print_info(KERN_DEFAULT);
  1165. printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
  1166. regs->nip, regs->link, regs->ctr);
  1167. printk("REGS: %p TRAP: %04lx %s (%s)\n",
  1168. regs, regs->trap, print_tainted(), init_utsname()->release);
  1169. printk("MSR: "REG" ", regs->msr);
  1170. print_msr_bits(regs->msr);
  1171. printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
  1172. trap = TRAP(regs);
  1173. if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
  1174. pr_cont("CFAR: "REG" ", regs->orig_gpr3);
  1175. if (trap == 0x200 || trap == 0x300 || trap == 0x600)
  1176. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  1177. pr_cont("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
  1178. #else
  1179. pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
  1180. #endif
  1181. #ifdef CONFIG_PPC64
  1182. pr_cont("SOFTE: %ld ", regs->softe);
  1183. #endif
  1184. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1185. if (MSR_TM_ACTIVE(regs->msr))
  1186. pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
  1187. #endif
  1188. for (i = 0; i < 32; i++) {
  1189. if ((i % REGS_PER_LINE) == 0)
  1190. pr_cont("\nGPR%02d: ", i);
  1191. pr_cont(REG " ", regs->gpr[i]);
  1192. if (i == LAST_VOLATILE && !FULL_REGS(regs))
  1193. break;
  1194. }
  1195. pr_cont("\n");
  1196. #ifdef CONFIG_KALLSYMS
  1197. /*
  1198. * Lookup NIP late so we have the best change of getting the
  1199. * above info out without failing
  1200. */
  1201. printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
  1202. printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
  1203. #endif
  1204. show_stack(current, (unsigned long *) regs->gpr[1]);
  1205. if (!user_mode(regs))
  1206. show_instructions(regs);
  1207. }
  1208. void flush_thread(void)
  1209. {
  1210. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1211. flush_ptrace_hw_breakpoint(current);
  1212. #else /* CONFIG_HAVE_HW_BREAKPOINT */
  1213. set_debug_reg_defaults(&current->thread);
  1214. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  1215. }
  1216. void
  1217. release_thread(struct task_struct *t)
  1218. {
  1219. }
  1220. /*
  1221. * this gets called so that we can store coprocessor state into memory and
  1222. * copy the current task into the new thread.
  1223. */
  1224. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  1225. {
  1226. flush_all_to_thread(src);
  1227. /*
  1228. * Flush TM state out so we can copy it. __switch_to_tm() does this
  1229. * flush but it removes the checkpointed state from the current CPU and
  1230. * transitions the CPU out of TM mode. Hence we need to call
  1231. * tm_recheckpoint_new_task() (on the same task) to restore the
  1232. * checkpointed state back and the TM mode.
  1233. *
  1234. * Can't pass dst because it isn't ready. Doesn't matter, passing
  1235. * dst is only important for __switch_to()
  1236. */
  1237. __switch_to_tm(src, src);
  1238. *dst = *src;
  1239. clear_task_ebb(dst);
  1240. return 0;
  1241. }
  1242. static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
  1243. {
  1244. #ifdef CONFIG_PPC_STD_MMU_64
  1245. unsigned long sp_vsid;
  1246. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  1247. if (radix_enabled())
  1248. return;
  1249. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1250. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
  1251. << SLB_VSID_SHIFT_1T;
  1252. else
  1253. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
  1254. << SLB_VSID_SHIFT;
  1255. sp_vsid |= SLB_VSID_KERNEL | llp;
  1256. p->thread.ksp_vsid = sp_vsid;
  1257. #endif
  1258. }
  1259. /*
  1260. * Copy a thread..
  1261. */
  1262. /*
  1263. * Copy architecture-specific thread state
  1264. */
  1265. int copy_thread(unsigned long clone_flags, unsigned long usp,
  1266. unsigned long kthread_arg, struct task_struct *p)
  1267. {
  1268. struct pt_regs *childregs, *kregs;
  1269. extern void ret_from_fork(void);
  1270. extern void ret_from_kernel_thread(void);
  1271. void (*f)(void);
  1272. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  1273. struct thread_info *ti = task_thread_info(p);
  1274. klp_init_thread_info(ti);
  1275. /* Copy registers */
  1276. sp -= sizeof(struct pt_regs);
  1277. childregs = (struct pt_regs *) sp;
  1278. if (unlikely(p->flags & PF_KTHREAD)) {
  1279. /* kernel thread */
  1280. memset(childregs, 0, sizeof(struct pt_regs));
  1281. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  1282. /* function */
  1283. if (usp)
  1284. childregs->gpr[14] = ppc_function_entry((void *)usp);
  1285. #ifdef CONFIG_PPC64
  1286. clear_tsk_thread_flag(p, TIF_32BIT);
  1287. childregs->softe = 1;
  1288. #endif
  1289. childregs->gpr[15] = kthread_arg;
  1290. p->thread.regs = NULL; /* no user register state */
  1291. ti->flags |= _TIF_RESTOREALL;
  1292. f = ret_from_kernel_thread;
  1293. } else {
  1294. /* user thread */
  1295. struct pt_regs *regs = current_pt_regs();
  1296. CHECK_FULL_REGS(regs);
  1297. *childregs = *regs;
  1298. if (usp)
  1299. childregs->gpr[1] = usp;
  1300. p->thread.regs = childregs;
  1301. childregs->gpr[3] = 0; /* Result from fork() */
  1302. if (clone_flags & CLONE_SETTLS) {
  1303. #ifdef CONFIG_PPC64
  1304. if (!is_32bit_task())
  1305. childregs->gpr[13] = childregs->gpr[6];
  1306. else
  1307. #endif
  1308. childregs->gpr[2] = childregs->gpr[6];
  1309. }
  1310. f = ret_from_fork;
  1311. }
  1312. childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
  1313. sp -= STACK_FRAME_OVERHEAD;
  1314. /*
  1315. * The way this works is that at some point in the future
  1316. * some task will call _switch to switch to the new task.
  1317. * That will pop off the stack frame created below and start
  1318. * the new task running at ret_from_fork. The new task will
  1319. * do some house keeping and then return from the fork or clone
  1320. * system call, using the stack frame created above.
  1321. */
  1322. ((unsigned long *)sp)[0] = 0;
  1323. sp -= sizeof(struct pt_regs);
  1324. kregs = (struct pt_regs *) sp;
  1325. sp -= STACK_FRAME_OVERHEAD;
  1326. p->thread.ksp = sp;
  1327. #ifdef CONFIG_PPC32
  1328. p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
  1329. _ALIGN_UP(sizeof(struct thread_info), 16);
  1330. #endif
  1331. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1332. p->thread.ptrace_bps[0] = NULL;
  1333. #endif
  1334. p->thread.fp_save_area = NULL;
  1335. #ifdef CONFIG_ALTIVEC
  1336. p->thread.vr_save_area = NULL;
  1337. #endif
  1338. setup_ksp_vsid(p, sp);
  1339. #ifdef CONFIG_PPC64
  1340. if (cpu_has_feature(CPU_FTR_DSCR)) {
  1341. p->thread.dscr_inherit = current->thread.dscr_inherit;
  1342. p->thread.dscr = mfspr(SPRN_DSCR);
  1343. }
  1344. if (cpu_has_feature(CPU_FTR_HAS_PPR))
  1345. p->thread.ppr = INIT_PPR;
  1346. #endif
  1347. kregs->nip = ppc_function_entry(f);
  1348. return 0;
  1349. }
  1350. /*
  1351. * Set up a thread for executing a new program
  1352. */
  1353. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  1354. {
  1355. #ifdef CONFIG_PPC64
  1356. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  1357. #endif
  1358. /*
  1359. * If we exec out of a kernel thread then thread.regs will not be
  1360. * set. Do it now.
  1361. */
  1362. if (!current->thread.regs) {
  1363. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  1364. current->thread.regs = regs - 1;
  1365. }
  1366. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1367. /*
  1368. * Clear any transactional state, we're exec()ing. The cause is
  1369. * not important as there will never be a recheckpoint so it's not
  1370. * user visible.
  1371. */
  1372. if (MSR_TM_SUSPENDED(mfmsr()))
  1373. tm_reclaim_current(0);
  1374. #endif
  1375. memset(regs->gpr, 0, sizeof(regs->gpr));
  1376. regs->ctr = 0;
  1377. regs->link = 0;
  1378. regs->xer = 0;
  1379. regs->ccr = 0;
  1380. regs->gpr[1] = sp;
  1381. /*
  1382. * We have just cleared all the nonvolatile GPRs, so make
  1383. * FULL_REGS(regs) return true. This is necessary to allow
  1384. * ptrace to examine the thread immediately after exec.
  1385. */
  1386. regs->trap &= ~1UL;
  1387. #ifdef CONFIG_PPC32
  1388. regs->mq = 0;
  1389. regs->nip = start;
  1390. regs->msr = MSR_USER;
  1391. #else
  1392. if (!is_32bit_task()) {
  1393. unsigned long entry;
  1394. if (is_elf2_task()) {
  1395. /* Look ma, no function descriptors! */
  1396. entry = start;
  1397. /*
  1398. * Ulrich says:
  1399. * The latest iteration of the ABI requires that when
  1400. * calling a function (at its global entry point),
  1401. * the caller must ensure r12 holds the entry point
  1402. * address (so that the function can quickly
  1403. * establish addressability).
  1404. */
  1405. regs->gpr[12] = start;
  1406. /* Make sure that's restored on entry to userspace. */
  1407. set_thread_flag(TIF_RESTOREALL);
  1408. } else {
  1409. unsigned long toc;
  1410. /* start is a relocated pointer to the function
  1411. * descriptor for the elf _start routine. The first
  1412. * entry in the function descriptor is the entry
  1413. * address of _start and the second entry is the TOC
  1414. * value we need to use.
  1415. */
  1416. __get_user(entry, (unsigned long __user *)start);
  1417. __get_user(toc, (unsigned long __user *)start+1);
  1418. /* Check whether the e_entry function descriptor entries
  1419. * need to be relocated before we can use them.
  1420. */
  1421. if (load_addr != 0) {
  1422. entry += load_addr;
  1423. toc += load_addr;
  1424. }
  1425. regs->gpr[2] = toc;
  1426. }
  1427. regs->nip = entry;
  1428. regs->msr = MSR_USER64;
  1429. } else {
  1430. regs->nip = start;
  1431. regs->gpr[2] = 0;
  1432. regs->msr = MSR_USER32;
  1433. }
  1434. #endif
  1435. #ifdef CONFIG_VSX
  1436. current->thread.used_vsr = 0;
  1437. #endif
  1438. current->thread.load_fp = 0;
  1439. memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
  1440. current->thread.fp_save_area = NULL;
  1441. #ifdef CONFIG_ALTIVEC
  1442. memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
  1443. current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
  1444. current->thread.vr_save_area = NULL;
  1445. current->thread.vrsave = 0;
  1446. current->thread.used_vr = 0;
  1447. current->thread.load_vec = 0;
  1448. #endif /* CONFIG_ALTIVEC */
  1449. #ifdef CONFIG_SPE
  1450. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  1451. current->thread.acc = 0;
  1452. current->thread.spefscr = 0;
  1453. current->thread.used_spe = 0;
  1454. #endif /* CONFIG_SPE */
  1455. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1456. current->thread.tm_tfhar = 0;
  1457. current->thread.tm_texasr = 0;
  1458. current->thread.tm_tfiar = 0;
  1459. current->thread.load_tm = 0;
  1460. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  1461. }
  1462. EXPORT_SYMBOL(start_thread);
  1463. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  1464. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  1465. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  1466. {
  1467. struct pt_regs *regs = tsk->thread.regs;
  1468. /* This is a bit hairy. If we are an SPE enabled processor
  1469. * (have embedded fp) we store the IEEE exception enable flags in
  1470. * fpexc_mode. fpexc_mode is also used for setting FP exception
  1471. * mode (asyn, precise, disabled) for 'Classic' FP. */
  1472. if (val & PR_FP_EXC_SW_ENABLE) {
  1473. #ifdef CONFIG_SPE
  1474. if (cpu_has_feature(CPU_FTR_SPE)) {
  1475. /*
  1476. * When the sticky exception bits are set
  1477. * directly by userspace, it must call prctl
  1478. * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
  1479. * in the existing prctl settings) or
  1480. * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
  1481. * the bits being set). <fenv.h> functions
  1482. * saving and restoring the whole
  1483. * floating-point environment need to do so
  1484. * anyway to restore the prctl settings from
  1485. * the saved environment.
  1486. */
  1487. tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
  1488. tsk->thread.fpexc_mode = val &
  1489. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  1490. return 0;
  1491. } else {
  1492. return -EINVAL;
  1493. }
  1494. #else
  1495. return -EINVAL;
  1496. #endif
  1497. }
  1498. /* on a CONFIG_SPE this does not hurt us. The bits that
  1499. * __pack_fe01 use do not overlap with bits used for
  1500. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  1501. * on CONFIG_SPE implementations are reserved so writing to
  1502. * them does not change anything */
  1503. if (val > PR_FP_EXC_PRECISE)
  1504. return -EINVAL;
  1505. tsk->thread.fpexc_mode = __pack_fe01(val);
  1506. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  1507. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  1508. | tsk->thread.fpexc_mode;
  1509. return 0;
  1510. }
  1511. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  1512. {
  1513. unsigned int val;
  1514. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  1515. #ifdef CONFIG_SPE
  1516. if (cpu_has_feature(CPU_FTR_SPE)) {
  1517. /*
  1518. * When the sticky exception bits are set
  1519. * directly by userspace, it must call prctl
  1520. * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
  1521. * in the existing prctl settings) or
  1522. * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
  1523. * the bits being set). <fenv.h> functions
  1524. * saving and restoring the whole
  1525. * floating-point environment need to do so
  1526. * anyway to restore the prctl settings from
  1527. * the saved environment.
  1528. */
  1529. tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
  1530. val = tsk->thread.fpexc_mode;
  1531. } else
  1532. return -EINVAL;
  1533. #else
  1534. return -EINVAL;
  1535. #endif
  1536. else
  1537. val = __unpack_fe01(tsk->thread.fpexc_mode);
  1538. return put_user(val, (unsigned int __user *) adr);
  1539. }
  1540. int set_endian(struct task_struct *tsk, unsigned int val)
  1541. {
  1542. struct pt_regs *regs = tsk->thread.regs;
  1543. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  1544. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  1545. return -EINVAL;
  1546. if (regs == NULL)
  1547. return -EINVAL;
  1548. if (val == PR_ENDIAN_BIG)
  1549. regs->msr &= ~MSR_LE;
  1550. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  1551. regs->msr |= MSR_LE;
  1552. else
  1553. return -EINVAL;
  1554. return 0;
  1555. }
  1556. int get_endian(struct task_struct *tsk, unsigned long adr)
  1557. {
  1558. struct pt_regs *regs = tsk->thread.regs;
  1559. unsigned int val;
  1560. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  1561. !cpu_has_feature(CPU_FTR_REAL_LE))
  1562. return -EINVAL;
  1563. if (regs == NULL)
  1564. return -EINVAL;
  1565. if (regs->msr & MSR_LE) {
  1566. if (cpu_has_feature(CPU_FTR_REAL_LE))
  1567. val = PR_ENDIAN_LITTLE;
  1568. else
  1569. val = PR_ENDIAN_PPC_LITTLE;
  1570. } else
  1571. val = PR_ENDIAN_BIG;
  1572. return put_user(val, (unsigned int __user *)adr);
  1573. }
  1574. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  1575. {
  1576. tsk->thread.align_ctl = val;
  1577. return 0;
  1578. }
  1579. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  1580. {
  1581. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  1582. }
  1583. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  1584. unsigned long nbytes)
  1585. {
  1586. unsigned long stack_page;
  1587. unsigned long cpu = task_cpu(p);
  1588. /*
  1589. * Avoid crashing if the stack has overflowed and corrupted
  1590. * task_cpu(p), which is in the thread_info struct.
  1591. */
  1592. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  1593. stack_page = (unsigned long) hardirq_ctx[cpu];
  1594. if (sp >= stack_page + sizeof(struct thread_struct)
  1595. && sp <= stack_page + THREAD_SIZE - nbytes)
  1596. return 1;
  1597. stack_page = (unsigned long) softirq_ctx[cpu];
  1598. if (sp >= stack_page + sizeof(struct thread_struct)
  1599. && sp <= stack_page + THREAD_SIZE - nbytes)
  1600. return 1;
  1601. }
  1602. return 0;
  1603. }
  1604. int validate_sp(unsigned long sp, struct task_struct *p,
  1605. unsigned long nbytes)
  1606. {
  1607. unsigned long stack_page = (unsigned long)task_stack_page(p);
  1608. if (sp >= stack_page + sizeof(struct thread_struct)
  1609. && sp <= stack_page + THREAD_SIZE - nbytes)
  1610. return 1;
  1611. return valid_irq_stack(sp, p, nbytes);
  1612. }
  1613. EXPORT_SYMBOL(validate_sp);
  1614. unsigned long get_wchan(struct task_struct *p)
  1615. {
  1616. unsigned long ip, sp;
  1617. int count = 0;
  1618. if (!p || p == current || p->state == TASK_RUNNING)
  1619. return 0;
  1620. sp = p->thread.ksp;
  1621. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  1622. return 0;
  1623. do {
  1624. sp = *(unsigned long *)sp;
  1625. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  1626. return 0;
  1627. if (count > 0) {
  1628. ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
  1629. if (!in_sched_functions(ip))
  1630. return ip;
  1631. }
  1632. } while (count++ < 16);
  1633. return 0;
  1634. }
  1635. static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
  1636. void show_stack(struct task_struct *tsk, unsigned long *stack)
  1637. {
  1638. unsigned long sp, ip, lr, newsp;
  1639. int count = 0;
  1640. int firstframe = 1;
  1641. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1642. int curr_frame = current->curr_ret_stack;
  1643. extern void return_to_handler(void);
  1644. unsigned long rth = (unsigned long)return_to_handler;
  1645. #endif
  1646. sp = (unsigned long) stack;
  1647. if (tsk == NULL)
  1648. tsk = current;
  1649. if (sp == 0) {
  1650. if (tsk == current)
  1651. sp = current_stack_pointer();
  1652. else
  1653. sp = tsk->thread.ksp;
  1654. }
  1655. lr = 0;
  1656. printk("Call Trace:\n");
  1657. do {
  1658. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  1659. return;
  1660. stack = (unsigned long *) sp;
  1661. newsp = stack[0];
  1662. ip = stack[STACK_FRAME_LR_SAVE];
  1663. if (!firstframe || ip != lr) {
  1664. printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
  1665. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1666. if ((ip == rth) && curr_frame >= 0) {
  1667. pr_cont(" (%pS)",
  1668. (void *)current->ret_stack[curr_frame].ret);
  1669. curr_frame--;
  1670. }
  1671. #endif
  1672. if (firstframe)
  1673. pr_cont(" (unreliable)");
  1674. pr_cont("\n");
  1675. }
  1676. firstframe = 0;
  1677. /*
  1678. * See if this is an exception frame.
  1679. * We look for the "regshere" marker in the current frame.
  1680. */
  1681. if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
  1682. && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  1683. struct pt_regs *regs = (struct pt_regs *)
  1684. (sp + STACK_FRAME_OVERHEAD);
  1685. lr = regs->link;
  1686. printk("--- interrupt: %lx at %pS\n LR = %pS\n",
  1687. regs->trap, (void *)regs->nip, (void *)lr);
  1688. firstframe = 1;
  1689. }
  1690. sp = newsp;
  1691. } while (count++ < kstack_depth_to_print);
  1692. }
  1693. #ifdef CONFIG_PPC64
  1694. /* Called with hard IRQs off */
  1695. void notrace __ppc64_runlatch_on(void)
  1696. {
  1697. struct thread_info *ti = current_thread_info();
  1698. unsigned long ctrl;
  1699. ctrl = mfspr(SPRN_CTRLF);
  1700. ctrl |= CTRL_RUNLATCH;
  1701. mtspr(SPRN_CTRLT, ctrl);
  1702. ti->local_flags |= _TLF_RUNLATCH;
  1703. }
  1704. /* Called with hard IRQs off */
  1705. void notrace __ppc64_runlatch_off(void)
  1706. {
  1707. struct thread_info *ti = current_thread_info();
  1708. unsigned long ctrl;
  1709. ti->local_flags &= ~_TLF_RUNLATCH;
  1710. ctrl = mfspr(SPRN_CTRLF);
  1711. ctrl &= ~CTRL_RUNLATCH;
  1712. mtspr(SPRN_CTRLT, ctrl);
  1713. }
  1714. #endif /* CONFIG_PPC64 */
  1715. unsigned long arch_align_stack(unsigned long sp)
  1716. {
  1717. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  1718. sp -= get_random_int() & ~PAGE_MASK;
  1719. return sp & ~0xf;
  1720. }
  1721. static inline unsigned long brk_rnd(void)
  1722. {
  1723. unsigned long rnd = 0;
  1724. /* 8MB for 32bit, 1GB for 64bit */
  1725. if (is_32bit_task())
  1726. rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT)));
  1727. else
  1728. rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT)));
  1729. return rnd << PAGE_SHIFT;
  1730. }
  1731. unsigned long arch_randomize_brk(struct mm_struct *mm)
  1732. {
  1733. unsigned long base = mm->brk;
  1734. unsigned long ret;
  1735. #ifdef CONFIG_PPC_STD_MMU_64
  1736. /*
  1737. * If we are using 1TB segments and we are allowed to randomise
  1738. * the heap, we can put it above 1TB so it is backed by a 1TB
  1739. * segment. Otherwise the heap will be in the bottom 1TB
  1740. * which always uses 256MB segments and this may result in a
  1741. * performance penalty. We don't need to worry about radix. For
  1742. * radix, mmu_highuser_ssize remains unchanged from 256MB.
  1743. */
  1744. if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
  1745. base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
  1746. #endif
  1747. ret = PAGE_ALIGN(base + brk_rnd());
  1748. if (ret < mm->brk)
  1749. return mm->brk;
  1750. return ret;
  1751. }