process.c 62 KB

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