process.c 36 KB

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