process.c 50 KB

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