process.c 52 KB

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