process.c 55 KB

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