process.c 56 KB

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