process.c 43 KB

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