process.c 44 KB

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