process.c 56 KB

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