process.c 36 KB

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