process.c 56 KB

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