process.c 59 KB

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