process.c 56 KB

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