process.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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. /*
  563. * Use the current MSR TM suspended bit to track if we have
  564. * checkpointed state outstanding.
  565. * On signal delivery, we'd normally reclaim the checkpointed
  566. * state to obtain stack pointer (see:get_tm_stackpointer()).
  567. * This will then directly return to userspace without going
  568. * through __switch_to(). However, if the stack frame is bad,
  569. * we need to exit this thread which calls __switch_to() which
  570. * will again attempt to reclaim the already saved tm state.
  571. * Hence we need to check that we've not already reclaimed
  572. * this state.
  573. * We do this using the current MSR, rather tracking it in
  574. * some specific thread_struct bit, as it has the additional
  575. * benifit of checking for a potential TM bad thing exception.
  576. */
  577. if (!MSR_TM_SUSPENDED(mfmsr()))
  578. return;
  579. tm_reclaim(thr, thr->regs->msr, cause);
  580. /* Having done the reclaim, we now have the checkpointed
  581. * FP/VSX values in the registers. These might be valid
  582. * even if we have previously called enable_kernel_fp() or
  583. * flush_fp_to_thread(), so update thr->regs->msr to
  584. * indicate their current validity.
  585. */
  586. thr->regs->msr |= msr_diff;
  587. }
  588. void tm_reclaim_current(uint8_t cause)
  589. {
  590. tm_enable();
  591. tm_reclaim_thread(&current->thread, current_thread_info(), cause);
  592. }
  593. static inline void tm_reclaim_task(struct task_struct *tsk)
  594. {
  595. /* We have to work out if we're switching from/to a task that's in the
  596. * middle of a transaction.
  597. *
  598. * In switching we need to maintain a 2nd register state as
  599. * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
  600. * checkpointed (tbegin) state in ckpt_regs and saves the transactional
  601. * (current) FPRs into oldtask->thread.transact_fpr[].
  602. *
  603. * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
  604. */
  605. struct thread_struct *thr = &tsk->thread;
  606. if (!thr->regs)
  607. return;
  608. if (!MSR_TM_ACTIVE(thr->regs->msr))
  609. goto out_and_saveregs;
  610. /* Stash the original thread MSR, as giveup_fpu et al will
  611. * modify it. We hold onto it to see whether the task used
  612. * FP & vector regs. If the TIF_RESTORE_TM flag is set,
  613. * ckpt_regs.msr is already set.
  614. */
  615. if (!test_ti_thread_flag(task_thread_info(tsk), TIF_RESTORE_TM))
  616. thr->ckpt_regs.msr = thr->regs->msr;
  617. TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
  618. "ccr=%lx, msr=%lx, trap=%lx)\n",
  619. tsk->pid, thr->regs->nip,
  620. thr->regs->ccr, thr->regs->msr,
  621. thr->regs->trap);
  622. tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
  623. TM_DEBUG("--- tm_reclaim on pid %d complete\n",
  624. tsk->pid);
  625. out_and_saveregs:
  626. /* Always save the regs here, even if a transaction's not active.
  627. * This context-switches a thread's TM info SPRs. We do it here to
  628. * be consistent with the restore path (in recheckpoint) which
  629. * cannot happen later in _switch().
  630. */
  631. tm_save_sprs(thr);
  632. }
  633. extern void __tm_recheckpoint(struct thread_struct *thread,
  634. unsigned long orig_msr);
  635. void tm_recheckpoint(struct thread_struct *thread,
  636. unsigned long orig_msr)
  637. {
  638. unsigned long flags;
  639. /* We really can't be interrupted here as the TEXASR registers can't
  640. * change and later in the trecheckpoint code, we have a userspace R1.
  641. * So let's hard disable over this region.
  642. */
  643. local_irq_save(flags);
  644. hard_irq_disable();
  645. /* The TM SPRs are restored here, so that TEXASR.FS can be set
  646. * before the trecheckpoint and no explosion occurs.
  647. */
  648. tm_restore_sprs(thread);
  649. __tm_recheckpoint(thread, orig_msr);
  650. local_irq_restore(flags);
  651. }
  652. static inline void tm_recheckpoint_new_task(struct task_struct *new)
  653. {
  654. unsigned long msr;
  655. if (!cpu_has_feature(CPU_FTR_TM))
  656. return;
  657. /* Recheckpoint the registers of the thread we're about to switch to.
  658. *
  659. * If the task was using FP, we non-lazily reload both the original and
  660. * the speculative FP register states. This is because the kernel
  661. * doesn't see if/when a TM rollback occurs, so if we take an FP
  662. * unavoidable later, we are unable to determine which set of FP regs
  663. * need to be restored.
  664. */
  665. if (!new->thread.regs)
  666. return;
  667. if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
  668. tm_restore_sprs(&new->thread);
  669. return;
  670. }
  671. msr = new->thread.ckpt_regs.msr;
  672. /* Recheckpoint to restore original checkpointed register state. */
  673. TM_DEBUG("*** tm_recheckpoint of pid %d "
  674. "(new->msr 0x%lx, new->origmsr 0x%lx)\n",
  675. new->pid, new->thread.regs->msr, msr);
  676. /* This loads the checkpointed FP/VEC state, if used */
  677. tm_recheckpoint(&new->thread, msr);
  678. /* This loads the speculative FP/VEC state, if used */
  679. if (msr & MSR_FP) {
  680. do_load_up_transact_fpu(&new->thread);
  681. new->thread.regs->msr |=
  682. (MSR_FP | new->thread.fpexc_mode);
  683. }
  684. #ifdef CONFIG_ALTIVEC
  685. if (msr & MSR_VEC) {
  686. do_load_up_transact_altivec(&new->thread);
  687. new->thread.regs->msr |= MSR_VEC;
  688. }
  689. #endif
  690. /* We may as well turn on VSX too since all the state is restored now */
  691. if (msr & MSR_VSX)
  692. new->thread.regs->msr |= MSR_VSX;
  693. TM_DEBUG("*** tm_recheckpoint of pid %d complete "
  694. "(kernel msr 0x%lx)\n",
  695. new->pid, mfmsr());
  696. }
  697. static inline void __switch_to_tm(struct task_struct *prev)
  698. {
  699. if (cpu_has_feature(CPU_FTR_TM)) {
  700. tm_enable();
  701. tm_reclaim_task(prev);
  702. }
  703. }
  704. /*
  705. * This is called if we are on the way out to userspace and the
  706. * TIF_RESTORE_TM flag is set. It checks if we need to reload
  707. * FP and/or vector state and does so if necessary.
  708. * If userspace is inside a transaction (whether active or
  709. * suspended) and FP/VMX/VSX instructions have ever been enabled
  710. * inside that transaction, then we have to keep them enabled
  711. * and keep the FP/VMX/VSX state loaded while ever the transaction
  712. * continues. The reason is that if we didn't, and subsequently
  713. * got a FP/VMX/VSX unavailable interrupt inside a transaction,
  714. * we don't know whether it's the same transaction, and thus we
  715. * don't know which of the checkpointed state and the transactional
  716. * state to use.
  717. */
  718. void restore_tm_state(struct pt_regs *regs)
  719. {
  720. unsigned long msr_diff;
  721. clear_thread_flag(TIF_RESTORE_TM);
  722. if (!MSR_TM_ACTIVE(regs->msr))
  723. return;
  724. msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
  725. msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
  726. if (msr_diff & MSR_FP) {
  727. msr_check_and_set(MSR_FP);
  728. load_fp_state(&current->thread.fp_state);
  729. msr_check_and_clear(MSR_FP);
  730. regs->msr |= current->thread.fpexc_mode;
  731. }
  732. if (msr_diff & MSR_VEC) {
  733. msr_check_and_set(MSR_VEC);
  734. load_vr_state(&current->thread.vr_state);
  735. msr_check_and_clear(MSR_VEC);
  736. }
  737. regs->msr |= msr_diff;
  738. }
  739. #else
  740. #define tm_recheckpoint_new_task(new)
  741. #define __switch_to_tm(prev)
  742. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  743. static inline void save_sprs(struct thread_struct *t)
  744. {
  745. #ifdef CONFIG_ALTIVEC
  746. if (cpu_has_feature(cpu_has_feature(CPU_FTR_ALTIVEC)))
  747. t->vrsave = mfspr(SPRN_VRSAVE);
  748. #endif
  749. #ifdef CONFIG_PPC_BOOK3S_64
  750. if (cpu_has_feature(CPU_FTR_DSCR))
  751. t->dscr = mfspr(SPRN_DSCR);
  752. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  753. t->bescr = mfspr(SPRN_BESCR);
  754. t->ebbhr = mfspr(SPRN_EBBHR);
  755. t->ebbrr = mfspr(SPRN_EBBRR);
  756. t->fscr = mfspr(SPRN_FSCR);
  757. /*
  758. * Note that the TAR is not available for use in the kernel.
  759. * (To provide this, the TAR should be backed up/restored on
  760. * exception entry/exit instead, and be in pt_regs. FIXME,
  761. * this should be in pt_regs anyway (for debug).)
  762. */
  763. t->tar = mfspr(SPRN_TAR);
  764. }
  765. #endif
  766. }
  767. static inline void restore_sprs(struct thread_struct *old_thread,
  768. struct thread_struct *new_thread)
  769. {
  770. #ifdef CONFIG_ALTIVEC
  771. if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
  772. old_thread->vrsave != new_thread->vrsave)
  773. mtspr(SPRN_VRSAVE, new_thread->vrsave);
  774. #endif
  775. #ifdef CONFIG_PPC_BOOK3S_64
  776. if (cpu_has_feature(CPU_FTR_DSCR)) {
  777. u64 dscr = get_paca()->dscr_default;
  778. u64 fscr = old_thread->fscr & ~FSCR_DSCR;
  779. if (new_thread->dscr_inherit) {
  780. dscr = new_thread->dscr;
  781. fscr |= FSCR_DSCR;
  782. }
  783. if (old_thread->dscr != dscr)
  784. mtspr(SPRN_DSCR, dscr);
  785. if (old_thread->fscr != fscr)
  786. mtspr(SPRN_FSCR, fscr);
  787. }
  788. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  789. if (old_thread->bescr != new_thread->bescr)
  790. mtspr(SPRN_BESCR, new_thread->bescr);
  791. if (old_thread->ebbhr != new_thread->ebbhr)
  792. mtspr(SPRN_EBBHR, new_thread->ebbhr);
  793. if (old_thread->ebbrr != new_thread->ebbrr)
  794. mtspr(SPRN_EBBRR, new_thread->ebbrr);
  795. if (old_thread->tar != new_thread->tar)
  796. mtspr(SPRN_TAR, new_thread->tar);
  797. }
  798. #endif
  799. }
  800. struct task_struct *__switch_to(struct task_struct *prev,
  801. struct task_struct *new)
  802. {
  803. struct thread_struct *new_thread, *old_thread;
  804. struct task_struct *last;
  805. #ifdef CONFIG_PPC_BOOK3S_64
  806. struct ppc64_tlb_batch *batch;
  807. #endif
  808. new_thread = &new->thread;
  809. old_thread = &current->thread;
  810. WARN_ON(!irqs_disabled());
  811. #ifdef CONFIG_PPC64
  812. /*
  813. * Collect processor utilization data per process
  814. */
  815. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  816. struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
  817. long unsigned start_tb, current_tb;
  818. start_tb = old_thread->start_tb;
  819. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  820. old_thread->accum_tb += (current_tb - start_tb);
  821. new_thread->start_tb = current_tb;
  822. }
  823. #endif /* CONFIG_PPC64 */
  824. #ifdef CONFIG_PPC_BOOK3S_64
  825. batch = this_cpu_ptr(&ppc64_tlb_batch);
  826. if (batch->active) {
  827. current_thread_info()->local_flags |= _TLF_LAZY_MMU;
  828. if (batch->index)
  829. __flush_tlb_pending(batch);
  830. batch->active = 0;
  831. }
  832. #endif /* CONFIG_PPC_BOOK3S_64 */
  833. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  834. switch_booke_debug_regs(&new->thread.debug);
  835. #else
  836. /*
  837. * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
  838. * schedule DABR
  839. */
  840. #ifndef CONFIG_HAVE_HW_BREAKPOINT
  841. if (unlikely(!hw_brk_match(this_cpu_ptr(&current_brk), &new->thread.hw_brk)))
  842. __set_breakpoint(&new->thread.hw_brk);
  843. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  844. #endif
  845. /*
  846. * We need to save SPRs before treclaim/trecheckpoint as these will
  847. * change a number of them.
  848. */
  849. save_sprs(&prev->thread);
  850. __switch_to_tm(prev);
  851. /* Save FPU, Altivec, VSX and SPE state */
  852. giveup_all(prev);
  853. /*
  854. * We can't take a PMU exception inside _switch() since there is a
  855. * window where the kernel stack SLB and the kernel stack are out
  856. * of sync. Hard disable here.
  857. */
  858. hard_irq_disable();
  859. tm_recheckpoint_new_task(new);
  860. /*
  861. * Call restore_sprs() before calling _switch(). If we move it after
  862. * _switch() then we miss out on calling it for new tasks. The reason
  863. * for this is we manually create a stack frame for new tasks that
  864. * directly returns through ret_from_fork() or
  865. * ret_from_kernel_thread(). See copy_thread() for details.
  866. */
  867. restore_sprs(old_thread, new_thread);
  868. last = _switch(old_thread, new_thread);
  869. #ifdef CONFIG_PPC_BOOK3S_64
  870. if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
  871. current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
  872. batch = this_cpu_ptr(&ppc64_tlb_batch);
  873. batch->active = 1;
  874. }
  875. #endif /* CONFIG_PPC_BOOK3S_64 */
  876. return last;
  877. }
  878. static int instructions_to_print = 16;
  879. static void show_instructions(struct pt_regs *regs)
  880. {
  881. int i;
  882. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  883. sizeof(int));
  884. printk("Instruction dump:");
  885. for (i = 0; i < instructions_to_print; i++) {
  886. int instr;
  887. if (!(i % 8))
  888. printk("\n");
  889. #if !defined(CONFIG_BOOKE)
  890. /* If executing with the IMMU off, adjust pc rather
  891. * than print XXXXXXXX.
  892. */
  893. if (!(regs->msr & MSR_IR))
  894. pc = (unsigned long)phys_to_virt(pc);
  895. #endif
  896. if (!__kernel_text_address(pc) ||
  897. probe_kernel_address((unsigned int __user *)pc, instr)) {
  898. printk(KERN_CONT "XXXXXXXX ");
  899. } else {
  900. if (regs->nip == pc)
  901. printk(KERN_CONT "<%08x> ", instr);
  902. else
  903. printk(KERN_CONT "%08x ", instr);
  904. }
  905. pc += sizeof(int);
  906. }
  907. printk("\n");
  908. }
  909. struct regbit {
  910. unsigned long bit;
  911. const char *name;
  912. };
  913. static struct regbit msr_bits[] = {
  914. #if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
  915. {MSR_SF, "SF"},
  916. {MSR_HV, "HV"},
  917. #endif
  918. {MSR_VEC, "VEC"},
  919. {MSR_VSX, "VSX"},
  920. #ifdef CONFIG_BOOKE
  921. {MSR_CE, "CE"},
  922. #endif
  923. {MSR_EE, "EE"},
  924. {MSR_PR, "PR"},
  925. {MSR_FP, "FP"},
  926. {MSR_ME, "ME"},
  927. #ifdef CONFIG_BOOKE
  928. {MSR_DE, "DE"},
  929. #else
  930. {MSR_SE, "SE"},
  931. {MSR_BE, "BE"},
  932. #endif
  933. {MSR_IR, "IR"},
  934. {MSR_DR, "DR"},
  935. {MSR_PMM, "PMM"},
  936. #ifndef CONFIG_BOOKE
  937. {MSR_RI, "RI"},
  938. {MSR_LE, "LE"},
  939. #endif
  940. {0, NULL}
  941. };
  942. static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
  943. {
  944. const char *s = "";
  945. for (; bits->bit; ++bits)
  946. if (val & bits->bit) {
  947. printk("%s%s", s, bits->name);
  948. s = sep;
  949. }
  950. }
  951. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  952. static struct regbit msr_tm_bits[] = {
  953. {MSR_TS_T, "T"},
  954. {MSR_TS_S, "S"},
  955. {MSR_TM, "E"},
  956. {0, NULL}
  957. };
  958. static void print_tm_bits(unsigned long val)
  959. {
  960. /*
  961. * This only prints something if at least one of the TM bit is set.
  962. * Inside the TM[], the output means:
  963. * E: Enabled (bit 32)
  964. * S: Suspended (bit 33)
  965. * T: Transactional (bit 34)
  966. */
  967. if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
  968. printk(",TM[");
  969. print_bits(val, msr_tm_bits, "");
  970. printk("]");
  971. }
  972. }
  973. #else
  974. static void print_tm_bits(unsigned long val) {}
  975. #endif
  976. static void print_msr_bits(unsigned long val)
  977. {
  978. printk("<");
  979. print_bits(val, msr_bits, ",");
  980. print_tm_bits(val);
  981. printk(">");
  982. }
  983. #ifdef CONFIG_PPC64
  984. #define REG "%016lx"
  985. #define REGS_PER_LINE 4
  986. #define LAST_VOLATILE 13
  987. #else
  988. #define REG "%08lx"
  989. #define REGS_PER_LINE 8
  990. #define LAST_VOLATILE 12
  991. #endif
  992. void show_regs(struct pt_regs * regs)
  993. {
  994. int i, trap;
  995. show_regs_print_info(KERN_DEFAULT);
  996. printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
  997. regs->nip, regs->link, regs->ctr);
  998. printk("REGS: %p TRAP: %04lx %s (%s)\n",
  999. regs, regs->trap, print_tainted(), init_utsname()->release);
  1000. printk("MSR: "REG" ", regs->msr);
  1001. print_msr_bits(regs->msr);
  1002. printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
  1003. trap = TRAP(regs);
  1004. if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
  1005. printk("CFAR: "REG" ", regs->orig_gpr3);
  1006. if (trap == 0x200 || trap == 0x300 || trap == 0x600)
  1007. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  1008. printk("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
  1009. #else
  1010. printk("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
  1011. #endif
  1012. #ifdef CONFIG_PPC64
  1013. printk("SOFTE: %ld ", regs->softe);
  1014. #endif
  1015. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1016. if (MSR_TM_ACTIVE(regs->msr))
  1017. printk("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
  1018. #endif
  1019. for (i = 0; i < 32; i++) {
  1020. if ((i % REGS_PER_LINE) == 0)
  1021. printk("\nGPR%02d: ", i);
  1022. printk(REG " ", regs->gpr[i]);
  1023. if (i == LAST_VOLATILE && !FULL_REGS(regs))
  1024. break;
  1025. }
  1026. printk("\n");
  1027. #ifdef CONFIG_KALLSYMS
  1028. /*
  1029. * Lookup NIP late so we have the best change of getting the
  1030. * above info out without failing
  1031. */
  1032. printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
  1033. printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
  1034. #endif
  1035. show_stack(current, (unsigned long *) regs->gpr[1]);
  1036. if (!user_mode(regs))
  1037. show_instructions(regs);
  1038. }
  1039. void exit_thread(void)
  1040. {
  1041. }
  1042. void flush_thread(void)
  1043. {
  1044. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1045. flush_ptrace_hw_breakpoint(current);
  1046. #else /* CONFIG_HAVE_HW_BREAKPOINT */
  1047. set_debug_reg_defaults(&current->thread);
  1048. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  1049. }
  1050. void
  1051. release_thread(struct task_struct *t)
  1052. {
  1053. }
  1054. /*
  1055. * this gets called so that we can store coprocessor state into memory and
  1056. * copy the current task into the new thread.
  1057. */
  1058. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  1059. {
  1060. flush_all_to_thread(src);
  1061. /*
  1062. * Flush TM state out so we can copy it. __switch_to_tm() does this
  1063. * flush but it removes the checkpointed state from the current CPU and
  1064. * transitions the CPU out of TM mode. Hence we need to call
  1065. * tm_recheckpoint_new_task() (on the same task) to restore the
  1066. * checkpointed state back and the TM mode.
  1067. */
  1068. __switch_to_tm(src);
  1069. tm_recheckpoint_new_task(src);
  1070. *dst = *src;
  1071. clear_task_ebb(dst);
  1072. return 0;
  1073. }
  1074. static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
  1075. {
  1076. #ifdef CONFIG_PPC_STD_MMU_64
  1077. unsigned long sp_vsid;
  1078. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  1079. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1080. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
  1081. << SLB_VSID_SHIFT_1T;
  1082. else
  1083. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
  1084. << SLB_VSID_SHIFT;
  1085. sp_vsid |= SLB_VSID_KERNEL | llp;
  1086. p->thread.ksp_vsid = sp_vsid;
  1087. #endif
  1088. }
  1089. /*
  1090. * Copy a thread..
  1091. */
  1092. /*
  1093. * Copy architecture-specific thread state
  1094. */
  1095. int copy_thread(unsigned long clone_flags, unsigned long usp,
  1096. unsigned long kthread_arg, struct task_struct *p)
  1097. {
  1098. struct pt_regs *childregs, *kregs;
  1099. extern void ret_from_fork(void);
  1100. extern void ret_from_kernel_thread(void);
  1101. void (*f)(void);
  1102. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  1103. /* Copy registers */
  1104. sp -= sizeof(struct pt_regs);
  1105. childregs = (struct pt_regs *) sp;
  1106. if (unlikely(p->flags & PF_KTHREAD)) {
  1107. /* kernel thread */
  1108. struct thread_info *ti = (void *)task_stack_page(p);
  1109. memset(childregs, 0, sizeof(struct pt_regs));
  1110. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  1111. /* function */
  1112. if (usp)
  1113. childregs->gpr[14] = ppc_function_entry((void *)usp);
  1114. #ifdef CONFIG_PPC64
  1115. clear_tsk_thread_flag(p, TIF_32BIT);
  1116. childregs->softe = 1;
  1117. #endif
  1118. childregs->gpr[15] = kthread_arg;
  1119. p->thread.regs = NULL; /* no user register state */
  1120. ti->flags |= _TIF_RESTOREALL;
  1121. f = ret_from_kernel_thread;
  1122. } else {
  1123. /* user thread */
  1124. struct pt_regs *regs = current_pt_regs();
  1125. CHECK_FULL_REGS(regs);
  1126. *childregs = *regs;
  1127. if (usp)
  1128. childregs->gpr[1] = usp;
  1129. p->thread.regs = childregs;
  1130. childregs->gpr[3] = 0; /* Result from fork() */
  1131. if (clone_flags & CLONE_SETTLS) {
  1132. #ifdef CONFIG_PPC64
  1133. if (!is_32bit_task())
  1134. childregs->gpr[13] = childregs->gpr[6];
  1135. else
  1136. #endif
  1137. childregs->gpr[2] = childregs->gpr[6];
  1138. }
  1139. f = ret_from_fork;
  1140. }
  1141. sp -= STACK_FRAME_OVERHEAD;
  1142. /*
  1143. * The way this works is that at some point in the future
  1144. * some task will call _switch to switch to the new task.
  1145. * That will pop off the stack frame created below and start
  1146. * the new task running at ret_from_fork. The new task will
  1147. * do some house keeping and then return from the fork or clone
  1148. * system call, using the stack frame created above.
  1149. */
  1150. ((unsigned long *)sp)[0] = 0;
  1151. sp -= sizeof(struct pt_regs);
  1152. kregs = (struct pt_regs *) sp;
  1153. sp -= STACK_FRAME_OVERHEAD;
  1154. p->thread.ksp = sp;
  1155. #ifdef CONFIG_PPC32
  1156. p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
  1157. _ALIGN_UP(sizeof(struct thread_info), 16);
  1158. #endif
  1159. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1160. p->thread.ptrace_bps[0] = NULL;
  1161. #endif
  1162. p->thread.fp_save_area = NULL;
  1163. #ifdef CONFIG_ALTIVEC
  1164. p->thread.vr_save_area = NULL;
  1165. #endif
  1166. setup_ksp_vsid(p, sp);
  1167. #ifdef CONFIG_PPC64
  1168. if (cpu_has_feature(CPU_FTR_DSCR)) {
  1169. p->thread.dscr_inherit = current->thread.dscr_inherit;
  1170. p->thread.dscr = mfspr(SPRN_DSCR);
  1171. }
  1172. if (cpu_has_feature(CPU_FTR_HAS_PPR))
  1173. p->thread.ppr = INIT_PPR;
  1174. #endif
  1175. kregs->nip = ppc_function_entry(f);
  1176. return 0;
  1177. }
  1178. /*
  1179. * Set up a thread for executing a new program
  1180. */
  1181. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  1182. {
  1183. #ifdef CONFIG_PPC64
  1184. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  1185. #endif
  1186. /*
  1187. * If we exec out of a kernel thread then thread.regs will not be
  1188. * set. Do it now.
  1189. */
  1190. if (!current->thread.regs) {
  1191. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  1192. current->thread.regs = regs - 1;
  1193. }
  1194. memset(regs->gpr, 0, sizeof(regs->gpr));
  1195. regs->ctr = 0;
  1196. regs->link = 0;
  1197. regs->xer = 0;
  1198. regs->ccr = 0;
  1199. regs->gpr[1] = sp;
  1200. /*
  1201. * We have just cleared all the nonvolatile GPRs, so make
  1202. * FULL_REGS(regs) return true. This is necessary to allow
  1203. * ptrace to examine the thread immediately after exec.
  1204. */
  1205. regs->trap &= ~1UL;
  1206. #ifdef CONFIG_PPC32
  1207. regs->mq = 0;
  1208. regs->nip = start;
  1209. regs->msr = MSR_USER;
  1210. #else
  1211. if (!is_32bit_task()) {
  1212. unsigned long entry;
  1213. if (is_elf2_task()) {
  1214. /* Look ma, no function descriptors! */
  1215. entry = start;
  1216. /*
  1217. * Ulrich says:
  1218. * The latest iteration of the ABI requires that when
  1219. * calling a function (at its global entry point),
  1220. * the caller must ensure r12 holds the entry point
  1221. * address (so that the function can quickly
  1222. * establish addressability).
  1223. */
  1224. regs->gpr[12] = start;
  1225. /* Make sure that's restored on entry to userspace. */
  1226. set_thread_flag(TIF_RESTOREALL);
  1227. } else {
  1228. unsigned long toc;
  1229. /* start is a relocated pointer to the function
  1230. * descriptor for the elf _start routine. The first
  1231. * entry in the function descriptor is the entry
  1232. * address of _start and the second entry is the TOC
  1233. * value we need to use.
  1234. */
  1235. __get_user(entry, (unsigned long __user *)start);
  1236. __get_user(toc, (unsigned long __user *)start+1);
  1237. /* Check whether the e_entry function descriptor entries
  1238. * need to be relocated before we can use them.
  1239. */
  1240. if (load_addr != 0) {
  1241. entry += load_addr;
  1242. toc += load_addr;
  1243. }
  1244. regs->gpr[2] = toc;
  1245. }
  1246. regs->nip = entry;
  1247. regs->msr = MSR_USER64;
  1248. } else {
  1249. regs->nip = start;
  1250. regs->gpr[2] = 0;
  1251. regs->msr = MSR_USER32;
  1252. }
  1253. #endif
  1254. #ifdef CONFIG_VSX
  1255. current->thread.used_vsr = 0;
  1256. #endif
  1257. memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
  1258. current->thread.fp_save_area = NULL;
  1259. #ifdef CONFIG_ALTIVEC
  1260. memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
  1261. current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
  1262. current->thread.vr_save_area = NULL;
  1263. current->thread.vrsave = 0;
  1264. current->thread.used_vr = 0;
  1265. #endif /* CONFIG_ALTIVEC */
  1266. #ifdef CONFIG_SPE
  1267. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  1268. current->thread.acc = 0;
  1269. current->thread.spefscr = 0;
  1270. current->thread.used_spe = 0;
  1271. #endif /* CONFIG_SPE */
  1272. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1273. if (cpu_has_feature(CPU_FTR_TM))
  1274. regs->msr |= MSR_TM;
  1275. current->thread.tm_tfhar = 0;
  1276. current->thread.tm_texasr = 0;
  1277. current->thread.tm_tfiar = 0;
  1278. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  1279. }
  1280. EXPORT_SYMBOL(start_thread);
  1281. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  1282. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  1283. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  1284. {
  1285. struct pt_regs *regs = tsk->thread.regs;
  1286. /* This is a bit hairy. If we are an SPE enabled processor
  1287. * (have embedded fp) we store the IEEE exception enable flags in
  1288. * fpexc_mode. fpexc_mode is also used for setting FP exception
  1289. * mode (asyn, precise, disabled) for 'Classic' FP. */
  1290. if (val & PR_FP_EXC_SW_ENABLE) {
  1291. #ifdef CONFIG_SPE
  1292. if (cpu_has_feature(CPU_FTR_SPE)) {
  1293. /*
  1294. * When the sticky exception bits are set
  1295. * directly by userspace, it must call prctl
  1296. * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
  1297. * in the existing prctl settings) or
  1298. * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
  1299. * the bits being set). <fenv.h> functions
  1300. * saving and restoring the whole
  1301. * floating-point environment need to do so
  1302. * anyway to restore the prctl settings from
  1303. * the saved environment.
  1304. */
  1305. tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
  1306. tsk->thread.fpexc_mode = val &
  1307. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  1308. return 0;
  1309. } else {
  1310. return -EINVAL;
  1311. }
  1312. #else
  1313. return -EINVAL;
  1314. #endif
  1315. }
  1316. /* on a CONFIG_SPE this does not hurt us. The bits that
  1317. * __pack_fe01 use do not overlap with bits used for
  1318. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  1319. * on CONFIG_SPE implementations are reserved so writing to
  1320. * them does not change anything */
  1321. if (val > PR_FP_EXC_PRECISE)
  1322. return -EINVAL;
  1323. tsk->thread.fpexc_mode = __pack_fe01(val);
  1324. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  1325. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  1326. | tsk->thread.fpexc_mode;
  1327. return 0;
  1328. }
  1329. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  1330. {
  1331. unsigned int val;
  1332. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  1333. #ifdef CONFIG_SPE
  1334. if (cpu_has_feature(CPU_FTR_SPE)) {
  1335. /*
  1336. * When the sticky exception bits are set
  1337. * directly by userspace, it must call prctl
  1338. * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
  1339. * in the existing prctl settings) or
  1340. * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
  1341. * the bits being set). <fenv.h> functions
  1342. * saving and restoring the whole
  1343. * floating-point environment need to do so
  1344. * anyway to restore the prctl settings from
  1345. * the saved environment.
  1346. */
  1347. tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
  1348. val = tsk->thread.fpexc_mode;
  1349. } else
  1350. return -EINVAL;
  1351. #else
  1352. return -EINVAL;
  1353. #endif
  1354. else
  1355. val = __unpack_fe01(tsk->thread.fpexc_mode);
  1356. return put_user(val, (unsigned int __user *) adr);
  1357. }
  1358. int set_endian(struct task_struct *tsk, unsigned int val)
  1359. {
  1360. struct pt_regs *regs = tsk->thread.regs;
  1361. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  1362. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  1363. return -EINVAL;
  1364. if (regs == NULL)
  1365. return -EINVAL;
  1366. if (val == PR_ENDIAN_BIG)
  1367. regs->msr &= ~MSR_LE;
  1368. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  1369. regs->msr |= MSR_LE;
  1370. else
  1371. return -EINVAL;
  1372. return 0;
  1373. }
  1374. int get_endian(struct task_struct *tsk, unsigned long adr)
  1375. {
  1376. struct pt_regs *regs = tsk->thread.regs;
  1377. unsigned int val;
  1378. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  1379. !cpu_has_feature(CPU_FTR_REAL_LE))
  1380. return -EINVAL;
  1381. if (regs == NULL)
  1382. return -EINVAL;
  1383. if (regs->msr & MSR_LE) {
  1384. if (cpu_has_feature(CPU_FTR_REAL_LE))
  1385. val = PR_ENDIAN_LITTLE;
  1386. else
  1387. val = PR_ENDIAN_PPC_LITTLE;
  1388. } else
  1389. val = PR_ENDIAN_BIG;
  1390. return put_user(val, (unsigned int __user *)adr);
  1391. }
  1392. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  1393. {
  1394. tsk->thread.align_ctl = val;
  1395. return 0;
  1396. }
  1397. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  1398. {
  1399. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  1400. }
  1401. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  1402. unsigned long nbytes)
  1403. {
  1404. unsigned long stack_page;
  1405. unsigned long cpu = task_cpu(p);
  1406. /*
  1407. * Avoid crashing if the stack has overflowed and corrupted
  1408. * task_cpu(p), which is in the thread_info struct.
  1409. */
  1410. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  1411. stack_page = (unsigned long) hardirq_ctx[cpu];
  1412. if (sp >= stack_page + sizeof(struct thread_struct)
  1413. && sp <= stack_page + THREAD_SIZE - nbytes)
  1414. return 1;
  1415. stack_page = (unsigned long) softirq_ctx[cpu];
  1416. if (sp >= stack_page + sizeof(struct thread_struct)
  1417. && sp <= stack_page + THREAD_SIZE - nbytes)
  1418. return 1;
  1419. }
  1420. return 0;
  1421. }
  1422. int validate_sp(unsigned long sp, struct task_struct *p,
  1423. unsigned long nbytes)
  1424. {
  1425. unsigned long stack_page = (unsigned long)task_stack_page(p);
  1426. if (sp >= stack_page + sizeof(struct thread_struct)
  1427. && sp <= stack_page + THREAD_SIZE - nbytes)
  1428. return 1;
  1429. return valid_irq_stack(sp, p, nbytes);
  1430. }
  1431. EXPORT_SYMBOL(validate_sp);
  1432. unsigned long get_wchan(struct task_struct *p)
  1433. {
  1434. unsigned long ip, sp;
  1435. int count = 0;
  1436. if (!p || p == current || p->state == TASK_RUNNING)
  1437. return 0;
  1438. sp = p->thread.ksp;
  1439. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  1440. return 0;
  1441. do {
  1442. sp = *(unsigned long *)sp;
  1443. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  1444. return 0;
  1445. if (count > 0) {
  1446. ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
  1447. if (!in_sched_functions(ip))
  1448. return ip;
  1449. }
  1450. } while (count++ < 16);
  1451. return 0;
  1452. }
  1453. static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
  1454. void show_stack(struct task_struct *tsk, unsigned long *stack)
  1455. {
  1456. unsigned long sp, ip, lr, newsp;
  1457. int count = 0;
  1458. int firstframe = 1;
  1459. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1460. int curr_frame = current->curr_ret_stack;
  1461. extern void return_to_handler(void);
  1462. unsigned long rth = (unsigned long)return_to_handler;
  1463. #endif
  1464. sp = (unsigned long) stack;
  1465. if (tsk == NULL)
  1466. tsk = current;
  1467. if (sp == 0) {
  1468. if (tsk == current)
  1469. sp = current_stack_pointer();
  1470. else
  1471. sp = tsk->thread.ksp;
  1472. }
  1473. lr = 0;
  1474. printk("Call Trace:\n");
  1475. do {
  1476. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  1477. return;
  1478. stack = (unsigned long *) sp;
  1479. newsp = stack[0];
  1480. ip = stack[STACK_FRAME_LR_SAVE];
  1481. if (!firstframe || ip != lr) {
  1482. printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
  1483. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1484. if ((ip == rth) && curr_frame >= 0) {
  1485. printk(" (%pS)",
  1486. (void *)current->ret_stack[curr_frame].ret);
  1487. curr_frame--;
  1488. }
  1489. #endif
  1490. if (firstframe)
  1491. printk(" (unreliable)");
  1492. printk("\n");
  1493. }
  1494. firstframe = 0;
  1495. /*
  1496. * See if this is an exception frame.
  1497. * We look for the "regshere" marker in the current frame.
  1498. */
  1499. if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
  1500. && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  1501. struct pt_regs *regs = (struct pt_regs *)
  1502. (sp + STACK_FRAME_OVERHEAD);
  1503. lr = regs->link;
  1504. printk("--- interrupt: %lx at %pS\n LR = %pS\n",
  1505. regs->trap, (void *)regs->nip, (void *)lr);
  1506. firstframe = 1;
  1507. }
  1508. sp = newsp;
  1509. } while (count++ < kstack_depth_to_print);
  1510. }
  1511. #ifdef CONFIG_PPC64
  1512. /* Called with hard IRQs off */
  1513. void notrace __ppc64_runlatch_on(void)
  1514. {
  1515. struct thread_info *ti = current_thread_info();
  1516. unsigned long ctrl;
  1517. ctrl = mfspr(SPRN_CTRLF);
  1518. ctrl |= CTRL_RUNLATCH;
  1519. mtspr(SPRN_CTRLT, ctrl);
  1520. ti->local_flags |= _TLF_RUNLATCH;
  1521. }
  1522. /* Called with hard IRQs off */
  1523. void notrace __ppc64_runlatch_off(void)
  1524. {
  1525. struct thread_info *ti = current_thread_info();
  1526. unsigned long ctrl;
  1527. ti->local_flags &= ~_TLF_RUNLATCH;
  1528. ctrl = mfspr(SPRN_CTRLF);
  1529. ctrl &= ~CTRL_RUNLATCH;
  1530. mtspr(SPRN_CTRLT, ctrl);
  1531. }
  1532. #endif /* CONFIG_PPC64 */
  1533. unsigned long arch_align_stack(unsigned long sp)
  1534. {
  1535. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  1536. sp -= get_random_int() & ~PAGE_MASK;
  1537. return sp & ~0xf;
  1538. }
  1539. static inline unsigned long brk_rnd(void)
  1540. {
  1541. unsigned long rnd = 0;
  1542. /* 8MB for 32bit, 1GB for 64bit */
  1543. if (is_32bit_task())
  1544. rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
  1545. else
  1546. rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
  1547. return rnd << PAGE_SHIFT;
  1548. }
  1549. unsigned long arch_randomize_brk(struct mm_struct *mm)
  1550. {
  1551. unsigned long base = mm->brk;
  1552. unsigned long ret;
  1553. #ifdef CONFIG_PPC_STD_MMU_64
  1554. /*
  1555. * If we are using 1TB segments and we are allowed to randomise
  1556. * the heap, we can put it above 1TB so it is backed by a 1TB
  1557. * segment. Otherwise the heap will be in the bottom 1TB
  1558. * which always uses 256MB segments and this may result in a
  1559. * performance penalty.
  1560. */
  1561. if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
  1562. base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
  1563. #endif
  1564. ret = PAGE_ALIGN(base + brk_rnd());
  1565. if (ret < mm->brk)
  1566. return mm->brk;
  1567. return ret;
  1568. }