process.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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/module.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/system.h>
  43. #include <asm/io.h>
  44. #include <asm/processor.h>
  45. #include <asm/mmu.h>
  46. #include <asm/prom.h>
  47. #include <asm/machdep.h>
  48. #include <asm/time.h>
  49. #include <asm/syscalls.h>
  50. #ifdef CONFIG_PPC64
  51. #include <asm/firmware.h>
  52. #endif
  53. #include <linux/kprobes.h>
  54. #include <linux/kdebug.h>
  55. extern unsigned long _get_SP(void);
  56. #ifndef CONFIG_SMP
  57. struct task_struct *last_task_used_math = NULL;
  58. struct task_struct *last_task_used_altivec = NULL;
  59. struct task_struct *last_task_used_vsx = NULL;
  60. struct task_struct *last_task_used_spe = NULL;
  61. #endif
  62. /*
  63. * Make sure the floating-point register state in the
  64. * the thread_struct is up to date for task tsk.
  65. */
  66. void flush_fp_to_thread(struct task_struct *tsk)
  67. {
  68. if (tsk->thread.regs) {
  69. /*
  70. * We need to disable preemption here because if we didn't,
  71. * another process could get scheduled after the regs->msr
  72. * test but before we have finished saving the FP registers
  73. * to the thread_struct. That process could take over the
  74. * FPU, and then when we get scheduled again we would store
  75. * bogus values for the remaining FP registers.
  76. */
  77. preempt_disable();
  78. if (tsk->thread.regs->msr & MSR_FP) {
  79. #ifdef CONFIG_SMP
  80. /*
  81. * This should only ever be called for current or
  82. * for a stopped child process. Since we save away
  83. * the FP register state on context switch on SMP,
  84. * there is something wrong if a stopped child appears
  85. * to still have its FP state in the CPU registers.
  86. */
  87. BUG_ON(tsk != current);
  88. #endif
  89. giveup_fpu(tsk);
  90. }
  91. preempt_enable();
  92. }
  93. }
  94. void enable_kernel_fp(void)
  95. {
  96. WARN_ON(preemptible());
  97. #ifdef CONFIG_SMP
  98. if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
  99. giveup_fpu(current);
  100. else
  101. giveup_fpu(NULL); /* just enables FP for kernel */
  102. #else
  103. giveup_fpu(last_task_used_math);
  104. #endif /* CONFIG_SMP */
  105. }
  106. EXPORT_SYMBOL(enable_kernel_fp);
  107. #ifdef CONFIG_ALTIVEC
  108. void enable_kernel_altivec(void)
  109. {
  110. WARN_ON(preemptible());
  111. #ifdef CONFIG_SMP
  112. if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
  113. giveup_altivec(current);
  114. else
  115. giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
  116. #else
  117. giveup_altivec(last_task_used_altivec);
  118. #endif /* CONFIG_SMP */
  119. }
  120. EXPORT_SYMBOL(enable_kernel_altivec);
  121. /*
  122. * Make sure the VMX/Altivec register state in the
  123. * the thread_struct is up to date for task tsk.
  124. */
  125. void flush_altivec_to_thread(struct task_struct *tsk)
  126. {
  127. if (tsk->thread.regs) {
  128. preempt_disable();
  129. if (tsk->thread.regs->msr & MSR_VEC) {
  130. #ifdef CONFIG_SMP
  131. BUG_ON(tsk != current);
  132. #endif
  133. giveup_altivec(tsk);
  134. }
  135. preempt_enable();
  136. }
  137. }
  138. #endif /* CONFIG_ALTIVEC */
  139. #ifdef CONFIG_VSX
  140. #if 0
  141. /* not currently used, but some crazy RAID module might want to later */
  142. void enable_kernel_vsx(void)
  143. {
  144. WARN_ON(preemptible());
  145. #ifdef CONFIG_SMP
  146. if (current->thread.regs && (current->thread.regs->msr & MSR_VSX))
  147. giveup_vsx(current);
  148. else
  149. giveup_vsx(NULL); /* just enable vsx for kernel - force */
  150. #else
  151. giveup_vsx(last_task_used_vsx);
  152. #endif /* CONFIG_SMP */
  153. }
  154. EXPORT_SYMBOL(enable_kernel_vsx);
  155. #endif
  156. void giveup_vsx(struct task_struct *tsk)
  157. {
  158. giveup_fpu(tsk);
  159. giveup_altivec(tsk);
  160. __giveup_vsx(tsk);
  161. }
  162. void flush_vsx_to_thread(struct task_struct *tsk)
  163. {
  164. if (tsk->thread.regs) {
  165. preempt_disable();
  166. if (tsk->thread.regs->msr & MSR_VSX) {
  167. #ifdef CONFIG_SMP
  168. BUG_ON(tsk != current);
  169. #endif
  170. giveup_vsx(tsk);
  171. }
  172. preempt_enable();
  173. }
  174. }
  175. #endif /* CONFIG_VSX */
  176. #ifdef CONFIG_SPE
  177. void enable_kernel_spe(void)
  178. {
  179. WARN_ON(preemptible());
  180. #ifdef CONFIG_SMP
  181. if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
  182. giveup_spe(current);
  183. else
  184. giveup_spe(NULL); /* just enable SPE for kernel - force */
  185. #else
  186. giveup_spe(last_task_used_spe);
  187. #endif /* __SMP __ */
  188. }
  189. EXPORT_SYMBOL(enable_kernel_spe);
  190. void flush_spe_to_thread(struct task_struct *tsk)
  191. {
  192. if (tsk->thread.regs) {
  193. preempt_disable();
  194. if (tsk->thread.regs->msr & MSR_SPE) {
  195. #ifdef CONFIG_SMP
  196. BUG_ON(tsk != current);
  197. #endif
  198. giveup_spe(tsk);
  199. }
  200. preempt_enable();
  201. }
  202. }
  203. #endif /* CONFIG_SPE */
  204. #ifndef CONFIG_SMP
  205. /*
  206. * If we are doing lazy switching of CPU state (FP, altivec or SPE),
  207. * and the current task has some state, discard it.
  208. */
  209. void discard_lazy_cpu_state(void)
  210. {
  211. preempt_disable();
  212. if (last_task_used_math == current)
  213. last_task_used_math = NULL;
  214. #ifdef CONFIG_ALTIVEC
  215. if (last_task_used_altivec == current)
  216. last_task_used_altivec = NULL;
  217. #endif /* CONFIG_ALTIVEC */
  218. #ifdef CONFIG_VSX
  219. if (last_task_used_vsx == current)
  220. last_task_used_vsx = NULL;
  221. #endif /* CONFIG_VSX */
  222. #ifdef CONFIG_SPE
  223. if (last_task_used_spe == current)
  224. last_task_used_spe = NULL;
  225. #endif
  226. preempt_enable();
  227. }
  228. #endif /* CONFIG_SMP */
  229. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  230. void do_send_trap(struct pt_regs *regs, unsigned long address,
  231. unsigned long error_code, int signal_code, int breakpt)
  232. {
  233. siginfo_t info;
  234. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  235. 11, SIGSEGV) == NOTIFY_STOP)
  236. return;
  237. /* Deliver the signal to userspace */
  238. info.si_signo = SIGTRAP;
  239. info.si_errno = breakpt; /* breakpoint or watchpoint id */
  240. info.si_code = signal_code;
  241. info.si_addr = (void __user *)address;
  242. force_sig_info(SIGTRAP, &info, current);
  243. }
  244. #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
  245. void do_dabr(struct pt_regs *regs, unsigned long address,
  246. unsigned long error_code)
  247. {
  248. siginfo_t info;
  249. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  250. 11, SIGSEGV) == NOTIFY_STOP)
  251. return;
  252. if (debugger_dabr_match(regs))
  253. return;
  254. /* Clear the DABR */
  255. set_dabr(0);
  256. /* Deliver the signal to userspace */
  257. info.si_signo = SIGTRAP;
  258. info.si_errno = 0;
  259. info.si_code = TRAP_HWBKPT;
  260. info.si_addr = (void __user *)address;
  261. force_sig_info(SIGTRAP, &info, current);
  262. }
  263. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  264. static DEFINE_PER_CPU(unsigned long, current_dabr);
  265. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  266. /*
  267. * Set the debug registers back to their default "safe" values.
  268. */
  269. static void set_debug_reg_defaults(struct thread_struct *thread)
  270. {
  271. thread->iac1 = thread->iac2 = 0;
  272. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  273. thread->iac3 = thread->iac4 = 0;
  274. #endif
  275. thread->dac1 = thread->dac2 = 0;
  276. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  277. thread->dvc1 = thread->dvc2 = 0;
  278. #endif
  279. thread->dbcr0 = 0;
  280. #ifdef CONFIG_BOOKE
  281. /*
  282. * Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
  283. */
  284. thread->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | \
  285. DBCR1_IAC3US | DBCR1_IAC4US;
  286. /*
  287. * Force Data Address Compare User/Supervisor bits to be User-only
  288. * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
  289. */
  290. thread->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
  291. #else
  292. thread->dbcr1 = 0;
  293. #endif
  294. }
  295. static void prime_debug_regs(struct thread_struct *thread)
  296. {
  297. mtspr(SPRN_IAC1, thread->iac1);
  298. mtspr(SPRN_IAC2, thread->iac2);
  299. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  300. mtspr(SPRN_IAC3, thread->iac3);
  301. mtspr(SPRN_IAC4, thread->iac4);
  302. #endif
  303. mtspr(SPRN_DAC1, thread->dac1);
  304. mtspr(SPRN_DAC2, thread->dac2);
  305. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  306. mtspr(SPRN_DVC1, thread->dvc1);
  307. mtspr(SPRN_DVC2, thread->dvc2);
  308. #endif
  309. mtspr(SPRN_DBCR0, thread->dbcr0);
  310. mtspr(SPRN_DBCR1, thread->dbcr1);
  311. #ifdef CONFIG_BOOKE
  312. mtspr(SPRN_DBCR2, thread->dbcr2);
  313. #endif
  314. }
  315. /*
  316. * Unless neither the old or new thread are making use of the
  317. * debug registers, set the debug registers from the values
  318. * stored in the new thread.
  319. */
  320. static void switch_booke_debug_regs(struct thread_struct *new_thread)
  321. {
  322. if ((current->thread.dbcr0 & DBCR0_IDM)
  323. || (new_thread->dbcr0 & DBCR0_IDM))
  324. prime_debug_regs(new_thread);
  325. }
  326. #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
  327. #ifndef CONFIG_HAVE_HW_BREAKPOINT
  328. static void set_debug_reg_defaults(struct thread_struct *thread)
  329. {
  330. if (thread->dabr) {
  331. thread->dabr = 0;
  332. set_dabr(0);
  333. }
  334. }
  335. #endif /* !CONFIG_HAVE_HW_BREAKPOINT */
  336. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  337. int set_dabr(unsigned long dabr)
  338. {
  339. __get_cpu_var(current_dabr) = dabr;
  340. if (ppc_md.set_dabr)
  341. return ppc_md.set_dabr(dabr);
  342. /* XXX should we have a CPU_FTR_HAS_DABR ? */
  343. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  344. mtspr(SPRN_DAC1, dabr);
  345. #ifdef CONFIG_PPC_47x
  346. isync();
  347. #endif
  348. #elif defined(CONFIG_PPC_BOOK3S)
  349. mtspr(SPRN_DABR, dabr);
  350. #endif
  351. return 0;
  352. }
  353. #ifdef CONFIG_PPC64
  354. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  355. #endif
  356. struct task_struct *__switch_to(struct task_struct *prev,
  357. struct task_struct *new)
  358. {
  359. struct thread_struct *new_thread, *old_thread;
  360. unsigned long flags;
  361. struct task_struct *last;
  362. #ifdef CONFIG_SMP
  363. /* avoid complexity of lazy save/restore of fpu
  364. * by just saving it every time we switch out if
  365. * this task used the fpu during the last quantum.
  366. *
  367. * If it tries to use the fpu again, it'll trap and
  368. * reload its fp regs. So we don't have to do a restore
  369. * every switch, just a save.
  370. * -- Cort
  371. */
  372. if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
  373. giveup_fpu(prev);
  374. #ifdef CONFIG_ALTIVEC
  375. /*
  376. * If the previous thread used altivec in the last quantum
  377. * (thus changing altivec regs) then save them.
  378. * We used to check the VRSAVE register but not all apps
  379. * set it, so we don't rely on it now (and in fact we need
  380. * to save & restore VSCR even if VRSAVE == 0). -- paulus
  381. *
  382. * On SMP we always save/restore altivec regs just to avoid the
  383. * complexity of changing processors.
  384. * -- Cort
  385. */
  386. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
  387. giveup_altivec(prev);
  388. #endif /* CONFIG_ALTIVEC */
  389. #ifdef CONFIG_VSX
  390. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
  391. /* VMX and FPU registers are already save here */
  392. __giveup_vsx(prev);
  393. #endif /* CONFIG_VSX */
  394. #ifdef CONFIG_SPE
  395. /*
  396. * If the previous thread used spe in the last quantum
  397. * (thus changing spe regs) then save them.
  398. *
  399. * On SMP we always save/restore spe regs just to avoid the
  400. * complexity of changing processors.
  401. */
  402. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
  403. giveup_spe(prev);
  404. #endif /* CONFIG_SPE */
  405. #else /* CONFIG_SMP */
  406. #ifdef CONFIG_ALTIVEC
  407. /* Avoid the trap. On smp this this never happens since
  408. * we don't set last_task_used_altivec -- Cort
  409. */
  410. if (new->thread.regs && last_task_used_altivec == new)
  411. new->thread.regs->msr |= MSR_VEC;
  412. #endif /* CONFIG_ALTIVEC */
  413. #ifdef CONFIG_VSX
  414. if (new->thread.regs && last_task_used_vsx == new)
  415. new->thread.regs->msr |= MSR_VSX;
  416. #endif /* CONFIG_VSX */
  417. #ifdef CONFIG_SPE
  418. /* Avoid the trap. On smp this this never happens since
  419. * we don't set last_task_used_spe
  420. */
  421. if (new->thread.regs && last_task_used_spe == new)
  422. new->thread.regs->msr |= MSR_SPE;
  423. #endif /* CONFIG_SPE */
  424. #endif /* CONFIG_SMP */
  425. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  426. switch_booke_debug_regs(&new->thread);
  427. #else
  428. /*
  429. * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
  430. * schedule DABR
  431. */
  432. #ifndef CONFIG_HAVE_HW_BREAKPOINT
  433. if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr))
  434. set_dabr(new->thread.dabr);
  435. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  436. #endif
  437. new_thread = &new->thread;
  438. old_thread = &current->thread;
  439. #if defined(CONFIG_PPC_BOOK3E_64)
  440. /* XXX Current Book3E code doesn't deal with kernel side DBCR0,
  441. * we always hold the user values, so we set it now.
  442. *
  443. * However, we ensure the kernel MSR:DE is appropriately cleared too
  444. * to avoid spurrious single step exceptions in the kernel.
  445. *
  446. * This will have to change to merge with the ppc32 code at some point,
  447. * but I don't like much what ppc32 is doing today so there's some
  448. * thinking needed there
  449. */
  450. if ((new_thread->dbcr0 | old_thread->dbcr0) & DBCR0_IDM) {
  451. u32 dbcr0;
  452. mtmsr(mfmsr() & ~MSR_DE);
  453. isync();
  454. dbcr0 = mfspr(SPRN_DBCR0);
  455. dbcr0 = (dbcr0 & DBCR0_EDM) | new_thread->dbcr0;
  456. mtspr(SPRN_DBCR0, dbcr0);
  457. }
  458. #endif /* CONFIG_PPC64_BOOK3E */
  459. #ifdef CONFIG_PPC64
  460. /*
  461. * Collect processor utilization data per process
  462. */
  463. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  464. struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
  465. long unsigned start_tb, current_tb;
  466. start_tb = old_thread->start_tb;
  467. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  468. old_thread->accum_tb += (current_tb - start_tb);
  469. new_thread->start_tb = current_tb;
  470. }
  471. #endif
  472. local_irq_save(flags);
  473. account_system_vtime(current);
  474. account_process_vtime(current);
  475. /*
  476. * We can't take a PMU exception inside _switch() since there is a
  477. * window where the kernel stack SLB and the kernel stack are out
  478. * of sync. Hard disable here.
  479. */
  480. hard_irq_disable();
  481. last = _switch(old_thread, new_thread);
  482. local_irq_restore(flags);
  483. return last;
  484. }
  485. static int instructions_to_print = 16;
  486. static void show_instructions(struct pt_regs *regs)
  487. {
  488. int i;
  489. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  490. sizeof(int));
  491. printk("Instruction dump:");
  492. for (i = 0; i < instructions_to_print; i++) {
  493. int instr;
  494. if (!(i % 8))
  495. printk("\n");
  496. #if !defined(CONFIG_BOOKE)
  497. /* If executing with the IMMU off, adjust pc rather
  498. * than print XXXXXXXX.
  499. */
  500. if (!(regs->msr & MSR_IR))
  501. pc = (unsigned long)phys_to_virt(pc);
  502. #endif
  503. /* We use __get_user here *only* to avoid an OOPS on a
  504. * bad address because the pc *should* only be a
  505. * kernel address.
  506. */
  507. if (!__kernel_text_address(pc) ||
  508. __get_user(instr, (unsigned int __user *)pc)) {
  509. printk("XXXXXXXX ");
  510. } else {
  511. if (regs->nip == pc)
  512. printk("<%08x> ", instr);
  513. else
  514. printk("%08x ", instr);
  515. }
  516. pc += sizeof(int);
  517. }
  518. printk("\n");
  519. }
  520. static struct regbit {
  521. unsigned long bit;
  522. const char *name;
  523. } msr_bits[] = {
  524. {MSR_EE, "EE"},
  525. {MSR_PR, "PR"},
  526. {MSR_FP, "FP"},
  527. {MSR_VEC, "VEC"},
  528. {MSR_VSX, "VSX"},
  529. {MSR_ME, "ME"},
  530. {MSR_CE, "CE"},
  531. {MSR_DE, "DE"},
  532. {MSR_IR, "IR"},
  533. {MSR_DR, "DR"},
  534. {0, NULL}
  535. };
  536. static void printbits(unsigned long val, struct regbit *bits)
  537. {
  538. const char *sep = "";
  539. printk("<");
  540. for (; bits->bit; ++bits)
  541. if (val & bits->bit) {
  542. printk("%s%s", sep, bits->name);
  543. sep = ",";
  544. }
  545. printk(">");
  546. }
  547. #ifdef CONFIG_PPC64
  548. #define REG "%016lx"
  549. #define REGS_PER_LINE 4
  550. #define LAST_VOLATILE 13
  551. #else
  552. #define REG "%08lx"
  553. #define REGS_PER_LINE 8
  554. #define LAST_VOLATILE 12
  555. #endif
  556. void show_regs(struct pt_regs * regs)
  557. {
  558. int i, trap;
  559. printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
  560. regs->nip, regs->link, regs->ctr);
  561. printk("REGS: %p TRAP: %04lx %s (%s)\n",
  562. regs, regs->trap, print_tainted(), init_utsname()->release);
  563. printk("MSR: "REG" ", regs->msr);
  564. printbits(regs->msr, msr_bits);
  565. printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
  566. trap = TRAP(regs);
  567. if (trap == 0x300 || trap == 0x600)
  568. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  569. printk("DEAR: "REG", ESR: "REG"\n", regs->dar, regs->dsisr);
  570. #else
  571. printk("DAR: "REG", DSISR: %08lx\n", regs->dar, regs->dsisr);
  572. #endif
  573. printk("TASK = %p[%d] '%s' THREAD: %p",
  574. current, task_pid_nr(current), current->comm, task_thread_info(current));
  575. #ifdef CONFIG_SMP
  576. printk(" CPU: %d", raw_smp_processor_id());
  577. #endif /* CONFIG_SMP */
  578. for (i = 0; i < 32; i++) {
  579. if ((i % REGS_PER_LINE) == 0)
  580. printk("\nGPR%02d: ", i);
  581. printk(REG " ", regs->gpr[i]);
  582. if (i == LAST_VOLATILE && !FULL_REGS(regs))
  583. break;
  584. }
  585. printk("\n");
  586. #ifdef CONFIG_KALLSYMS
  587. /*
  588. * Lookup NIP late so we have the best change of getting the
  589. * above info out without failing
  590. */
  591. printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
  592. printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
  593. #endif
  594. show_stack(current, (unsigned long *) regs->gpr[1]);
  595. if (!user_mode(regs))
  596. show_instructions(regs);
  597. }
  598. void exit_thread(void)
  599. {
  600. discard_lazy_cpu_state();
  601. }
  602. void flush_thread(void)
  603. {
  604. discard_lazy_cpu_state();
  605. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  606. flush_ptrace_hw_breakpoint(current);
  607. #else /* CONFIG_HAVE_HW_BREAKPOINT */
  608. set_debug_reg_defaults(&current->thread);
  609. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  610. }
  611. void
  612. release_thread(struct task_struct *t)
  613. {
  614. }
  615. /*
  616. * This gets called before we allocate a new thread and copy
  617. * the current task into it.
  618. */
  619. void prepare_to_copy(struct task_struct *tsk)
  620. {
  621. flush_fp_to_thread(current);
  622. flush_altivec_to_thread(current);
  623. flush_vsx_to_thread(current);
  624. flush_spe_to_thread(current);
  625. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  626. flush_ptrace_hw_breakpoint(tsk);
  627. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  628. }
  629. /*
  630. * Copy a thread..
  631. */
  632. int copy_thread(unsigned long clone_flags, unsigned long usp,
  633. unsigned long unused, struct task_struct *p,
  634. struct pt_regs *regs)
  635. {
  636. struct pt_regs *childregs, *kregs;
  637. extern void ret_from_fork(void);
  638. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  639. CHECK_FULL_REGS(regs);
  640. /* Copy registers */
  641. sp -= sizeof(struct pt_regs);
  642. childregs = (struct pt_regs *) sp;
  643. *childregs = *regs;
  644. if ((childregs->msr & MSR_PR) == 0) {
  645. /* for kernel thread, set `current' and stackptr in new task */
  646. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  647. #ifdef CONFIG_PPC32
  648. childregs->gpr[2] = (unsigned long) p;
  649. #else
  650. clear_tsk_thread_flag(p, TIF_32BIT);
  651. #endif
  652. p->thread.regs = NULL; /* no user register state */
  653. } else {
  654. childregs->gpr[1] = usp;
  655. p->thread.regs = childregs;
  656. if (clone_flags & CLONE_SETTLS) {
  657. #ifdef CONFIG_PPC64
  658. if (!is_32bit_task())
  659. childregs->gpr[13] = childregs->gpr[6];
  660. else
  661. #endif
  662. childregs->gpr[2] = childregs->gpr[6];
  663. }
  664. }
  665. childregs->gpr[3] = 0; /* Result from fork() */
  666. sp -= STACK_FRAME_OVERHEAD;
  667. /*
  668. * The way this works is that at some point in the future
  669. * some task will call _switch to switch to the new task.
  670. * That will pop off the stack frame created below and start
  671. * the new task running at ret_from_fork. The new task will
  672. * do some house keeping and then return from the fork or clone
  673. * system call, using the stack frame created above.
  674. */
  675. sp -= sizeof(struct pt_regs);
  676. kregs = (struct pt_regs *) sp;
  677. sp -= STACK_FRAME_OVERHEAD;
  678. p->thread.ksp = sp;
  679. p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
  680. _ALIGN_UP(sizeof(struct thread_info), 16);
  681. #ifdef CONFIG_PPC_STD_MMU_64
  682. if (cpu_has_feature(CPU_FTR_SLB)) {
  683. unsigned long sp_vsid;
  684. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  685. if (cpu_has_feature(CPU_FTR_1T_SEGMENT))
  686. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
  687. << SLB_VSID_SHIFT_1T;
  688. else
  689. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
  690. << SLB_VSID_SHIFT;
  691. sp_vsid |= SLB_VSID_KERNEL | llp;
  692. p->thread.ksp_vsid = sp_vsid;
  693. }
  694. #endif /* CONFIG_PPC_STD_MMU_64 */
  695. /*
  696. * The PPC64 ABI makes use of a TOC to contain function
  697. * pointers. The function (ret_from_except) is actually a pointer
  698. * to the TOC entry. The first entry is a pointer to the actual
  699. * function.
  700. */
  701. #ifdef CONFIG_PPC64
  702. kregs->nip = *((unsigned long *)ret_from_fork);
  703. #else
  704. kregs->nip = (unsigned long)ret_from_fork;
  705. #endif
  706. return 0;
  707. }
  708. /*
  709. * Set up a thread for executing a new program
  710. */
  711. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  712. {
  713. #ifdef CONFIG_PPC64
  714. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  715. #endif
  716. set_fs(USER_DS);
  717. /*
  718. * If we exec out of a kernel thread then thread.regs will not be
  719. * set. Do it now.
  720. */
  721. if (!current->thread.regs) {
  722. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  723. current->thread.regs = regs - 1;
  724. }
  725. memset(regs->gpr, 0, sizeof(regs->gpr));
  726. regs->ctr = 0;
  727. regs->link = 0;
  728. regs->xer = 0;
  729. regs->ccr = 0;
  730. regs->gpr[1] = sp;
  731. /*
  732. * We have just cleared all the nonvolatile GPRs, so make
  733. * FULL_REGS(regs) return true. This is necessary to allow
  734. * ptrace to examine the thread immediately after exec.
  735. */
  736. regs->trap &= ~1UL;
  737. #ifdef CONFIG_PPC32
  738. regs->mq = 0;
  739. regs->nip = start;
  740. regs->msr = MSR_USER;
  741. #else
  742. if (!is_32bit_task()) {
  743. unsigned long entry, toc;
  744. /* start is a relocated pointer to the function descriptor for
  745. * the elf _start routine. The first entry in the function
  746. * descriptor is the entry address of _start and the second
  747. * entry is the TOC value we need to use.
  748. */
  749. __get_user(entry, (unsigned long __user *)start);
  750. __get_user(toc, (unsigned long __user *)start+1);
  751. /* Check whether the e_entry function descriptor entries
  752. * need to be relocated before we can use them.
  753. */
  754. if (load_addr != 0) {
  755. entry += load_addr;
  756. toc += load_addr;
  757. }
  758. regs->nip = entry;
  759. regs->gpr[2] = toc;
  760. regs->msr = MSR_USER64;
  761. } else {
  762. regs->nip = start;
  763. regs->gpr[2] = 0;
  764. regs->msr = MSR_USER32;
  765. }
  766. #endif
  767. discard_lazy_cpu_state();
  768. #ifdef CONFIG_VSX
  769. current->thread.used_vsr = 0;
  770. #endif
  771. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  772. current->thread.fpscr.val = 0;
  773. #ifdef CONFIG_ALTIVEC
  774. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  775. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  776. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  777. current->thread.vrsave = 0;
  778. current->thread.used_vr = 0;
  779. #endif /* CONFIG_ALTIVEC */
  780. #ifdef CONFIG_SPE
  781. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  782. current->thread.acc = 0;
  783. current->thread.spefscr = 0;
  784. current->thread.used_spe = 0;
  785. #endif /* CONFIG_SPE */
  786. }
  787. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  788. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  789. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  790. {
  791. struct pt_regs *regs = tsk->thread.regs;
  792. /* This is a bit hairy. If we are an SPE enabled processor
  793. * (have embedded fp) we store the IEEE exception enable flags in
  794. * fpexc_mode. fpexc_mode is also used for setting FP exception
  795. * mode (asyn, precise, disabled) for 'Classic' FP. */
  796. if (val & PR_FP_EXC_SW_ENABLE) {
  797. #ifdef CONFIG_SPE
  798. if (cpu_has_feature(CPU_FTR_SPE)) {
  799. tsk->thread.fpexc_mode = val &
  800. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  801. return 0;
  802. } else {
  803. return -EINVAL;
  804. }
  805. #else
  806. return -EINVAL;
  807. #endif
  808. }
  809. /* on a CONFIG_SPE this does not hurt us. The bits that
  810. * __pack_fe01 use do not overlap with bits used for
  811. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  812. * on CONFIG_SPE implementations are reserved so writing to
  813. * them does not change anything */
  814. if (val > PR_FP_EXC_PRECISE)
  815. return -EINVAL;
  816. tsk->thread.fpexc_mode = __pack_fe01(val);
  817. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  818. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  819. | tsk->thread.fpexc_mode;
  820. return 0;
  821. }
  822. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  823. {
  824. unsigned int val;
  825. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  826. #ifdef CONFIG_SPE
  827. if (cpu_has_feature(CPU_FTR_SPE))
  828. val = tsk->thread.fpexc_mode;
  829. else
  830. return -EINVAL;
  831. #else
  832. return -EINVAL;
  833. #endif
  834. else
  835. val = __unpack_fe01(tsk->thread.fpexc_mode);
  836. return put_user(val, (unsigned int __user *) adr);
  837. }
  838. int set_endian(struct task_struct *tsk, unsigned int val)
  839. {
  840. struct pt_regs *regs = tsk->thread.regs;
  841. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  842. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  843. return -EINVAL;
  844. if (regs == NULL)
  845. return -EINVAL;
  846. if (val == PR_ENDIAN_BIG)
  847. regs->msr &= ~MSR_LE;
  848. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  849. regs->msr |= MSR_LE;
  850. else
  851. return -EINVAL;
  852. return 0;
  853. }
  854. int get_endian(struct task_struct *tsk, unsigned long adr)
  855. {
  856. struct pt_regs *regs = tsk->thread.regs;
  857. unsigned int val;
  858. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  859. !cpu_has_feature(CPU_FTR_REAL_LE))
  860. return -EINVAL;
  861. if (regs == NULL)
  862. return -EINVAL;
  863. if (regs->msr & MSR_LE) {
  864. if (cpu_has_feature(CPU_FTR_REAL_LE))
  865. val = PR_ENDIAN_LITTLE;
  866. else
  867. val = PR_ENDIAN_PPC_LITTLE;
  868. } else
  869. val = PR_ENDIAN_BIG;
  870. return put_user(val, (unsigned int __user *)adr);
  871. }
  872. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  873. {
  874. tsk->thread.align_ctl = val;
  875. return 0;
  876. }
  877. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  878. {
  879. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  880. }
  881. #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
  882. int sys_clone(unsigned long clone_flags, unsigned long usp,
  883. int __user *parent_tidp, void __user *child_threadptr,
  884. int __user *child_tidp, int p6,
  885. struct pt_regs *regs)
  886. {
  887. CHECK_FULL_REGS(regs);
  888. if (usp == 0)
  889. usp = regs->gpr[1]; /* stack pointer for child */
  890. #ifdef CONFIG_PPC64
  891. if (is_32bit_task()) {
  892. parent_tidp = TRUNC_PTR(parent_tidp);
  893. child_tidp = TRUNC_PTR(child_tidp);
  894. }
  895. #endif
  896. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  897. }
  898. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  899. unsigned long p4, unsigned long p5, unsigned long p6,
  900. struct pt_regs *regs)
  901. {
  902. CHECK_FULL_REGS(regs);
  903. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  904. }
  905. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  906. unsigned long p4, unsigned long p5, unsigned long p6,
  907. struct pt_regs *regs)
  908. {
  909. CHECK_FULL_REGS(regs);
  910. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  911. regs, 0, NULL, NULL);
  912. }
  913. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  914. unsigned long a3, unsigned long a4, unsigned long a5,
  915. struct pt_regs *regs)
  916. {
  917. int error;
  918. char *filename;
  919. filename = getname((const char __user *) a0);
  920. error = PTR_ERR(filename);
  921. if (IS_ERR(filename))
  922. goto out;
  923. flush_fp_to_thread(current);
  924. flush_altivec_to_thread(current);
  925. flush_spe_to_thread(current);
  926. error = do_execve(filename,
  927. (const char __user *const __user *) a1,
  928. (const char __user *const __user *) a2, regs);
  929. putname(filename);
  930. out:
  931. return error;
  932. }
  933. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  934. unsigned long nbytes)
  935. {
  936. unsigned long stack_page;
  937. unsigned long cpu = task_cpu(p);
  938. /*
  939. * Avoid crashing if the stack has overflowed and corrupted
  940. * task_cpu(p), which is in the thread_info struct.
  941. */
  942. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  943. stack_page = (unsigned long) hardirq_ctx[cpu];
  944. if (sp >= stack_page + sizeof(struct thread_struct)
  945. && sp <= stack_page + THREAD_SIZE - nbytes)
  946. return 1;
  947. stack_page = (unsigned long) softirq_ctx[cpu];
  948. if (sp >= stack_page + sizeof(struct thread_struct)
  949. && sp <= stack_page + THREAD_SIZE - nbytes)
  950. return 1;
  951. }
  952. return 0;
  953. }
  954. int validate_sp(unsigned long sp, struct task_struct *p,
  955. unsigned long nbytes)
  956. {
  957. unsigned long stack_page = (unsigned long)task_stack_page(p);
  958. if (sp >= stack_page + sizeof(struct thread_struct)
  959. && sp <= stack_page + THREAD_SIZE - nbytes)
  960. return 1;
  961. return valid_irq_stack(sp, p, nbytes);
  962. }
  963. EXPORT_SYMBOL(validate_sp);
  964. unsigned long get_wchan(struct task_struct *p)
  965. {
  966. unsigned long ip, sp;
  967. int count = 0;
  968. if (!p || p == current || p->state == TASK_RUNNING)
  969. return 0;
  970. sp = p->thread.ksp;
  971. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  972. return 0;
  973. do {
  974. sp = *(unsigned long *)sp;
  975. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  976. return 0;
  977. if (count > 0) {
  978. ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
  979. if (!in_sched_functions(ip))
  980. return ip;
  981. }
  982. } while (count++ < 16);
  983. return 0;
  984. }
  985. static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
  986. void show_stack(struct task_struct *tsk, unsigned long *stack)
  987. {
  988. unsigned long sp, ip, lr, newsp;
  989. int count = 0;
  990. int firstframe = 1;
  991. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  992. int curr_frame = current->curr_ret_stack;
  993. extern void return_to_handler(void);
  994. unsigned long rth = (unsigned long)return_to_handler;
  995. unsigned long mrth = -1;
  996. #ifdef CONFIG_PPC64
  997. extern void mod_return_to_handler(void);
  998. rth = *(unsigned long *)rth;
  999. mrth = (unsigned long)mod_return_to_handler;
  1000. mrth = *(unsigned long *)mrth;
  1001. #endif
  1002. #endif
  1003. sp = (unsigned long) stack;
  1004. if (tsk == NULL)
  1005. tsk = current;
  1006. if (sp == 0) {
  1007. if (tsk == current)
  1008. asm("mr %0,1" : "=r" (sp));
  1009. else
  1010. sp = tsk->thread.ksp;
  1011. }
  1012. lr = 0;
  1013. printk("Call Trace:\n");
  1014. do {
  1015. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  1016. return;
  1017. stack = (unsigned long *) sp;
  1018. newsp = stack[0];
  1019. ip = stack[STACK_FRAME_LR_SAVE];
  1020. if (!firstframe || ip != lr) {
  1021. printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
  1022. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1023. if ((ip == rth || ip == mrth) && curr_frame >= 0) {
  1024. printk(" (%pS)",
  1025. (void *)current->ret_stack[curr_frame].ret);
  1026. curr_frame--;
  1027. }
  1028. #endif
  1029. if (firstframe)
  1030. printk(" (unreliable)");
  1031. printk("\n");
  1032. }
  1033. firstframe = 0;
  1034. /*
  1035. * See if this is an exception frame.
  1036. * We look for the "regshere" marker in the current frame.
  1037. */
  1038. if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
  1039. && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  1040. struct pt_regs *regs = (struct pt_regs *)
  1041. (sp + STACK_FRAME_OVERHEAD);
  1042. lr = regs->link;
  1043. printk("--- Exception: %lx at %pS\n LR = %pS\n",
  1044. regs->trap, (void *)regs->nip, (void *)lr);
  1045. firstframe = 1;
  1046. }
  1047. sp = newsp;
  1048. } while (count++ < kstack_depth_to_print);
  1049. }
  1050. void dump_stack(void)
  1051. {
  1052. show_stack(current, NULL);
  1053. }
  1054. EXPORT_SYMBOL(dump_stack);
  1055. #ifdef CONFIG_PPC64
  1056. void ppc64_runlatch_on(void)
  1057. {
  1058. unsigned long ctrl;
  1059. if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
  1060. HMT_medium();
  1061. ctrl = mfspr(SPRN_CTRLF);
  1062. ctrl |= CTRL_RUNLATCH;
  1063. mtspr(SPRN_CTRLT, ctrl);
  1064. set_thread_flag(TIF_RUNLATCH);
  1065. }
  1066. }
  1067. void __ppc64_runlatch_off(void)
  1068. {
  1069. unsigned long ctrl;
  1070. HMT_medium();
  1071. clear_thread_flag(TIF_RUNLATCH);
  1072. ctrl = mfspr(SPRN_CTRLF);
  1073. ctrl &= ~CTRL_RUNLATCH;
  1074. mtspr(SPRN_CTRLT, ctrl);
  1075. }
  1076. #endif
  1077. #if THREAD_SHIFT < PAGE_SHIFT
  1078. static struct kmem_cache *thread_info_cache;
  1079. struct thread_info *alloc_thread_info_node(struct task_struct *tsk, int node)
  1080. {
  1081. struct thread_info *ti;
  1082. ti = kmem_cache_alloc_node(thread_info_cache, GFP_KERNEL, node);
  1083. if (unlikely(ti == NULL))
  1084. return NULL;
  1085. #ifdef CONFIG_DEBUG_STACK_USAGE
  1086. memset(ti, 0, THREAD_SIZE);
  1087. #endif
  1088. return ti;
  1089. }
  1090. void free_thread_info(struct thread_info *ti)
  1091. {
  1092. kmem_cache_free(thread_info_cache, ti);
  1093. }
  1094. void thread_info_cache_init(void)
  1095. {
  1096. thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
  1097. THREAD_SIZE, 0, NULL);
  1098. BUG_ON(thread_info_cache == NULL);
  1099. }
  1100. #endif /* THREAD_SHIFT < PAGE_SHIFT */
  1101. unsigned long arch_align_stack(unsigned long sp)
  1102. {
  1103. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  1104. sp -= get_random_int() & ~PAGE_MASK;
  1105. return sp & ~0xf;
  1106. }
  1107. static inline unsigned long brk_rnd(void)
  1108. {
  1109. unsigned long rnd = 0;
  1110. /* 8MB for 32bit, 1GB for 64bit */
  1111. if (is_32bit_task())
  1112. rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
  1113. else
  1114. rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
  1115. return rnd << PAGE_SHIFT;
  1116. }
  1117. unsigned long arch_randomize_brk(struct mm_struct *mm)
  1118. {
  1119. unsigned long base = mm->brk;
  1120. unsigned long ret;
  1121. #ifdef CONFIG_PPC_STD_MMU_64
  1122. /*
  1123. * If we are using 1TB segments and we are allowed to randomise
  1124. * the heap, we can put it above 1TB so it is backed by a 1TB
  1125. * segment. Otherwise the heap will be in the bottom 1TB
  1126. * which always uses 256MB segments and this may result in a
  1127. * performance penalty.
  1128. */
  1129. if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
  1130. base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
  1131. #endif
  1132. ret = PAGE_ALIGN(base + brk_rnd());
  1133. if (ret < mm->brk)
  1134. return mm->brk;
  1135. return ret;
  1136. }
  1137. unsigned long randomize_et_dyn(unsigned long base)
  1138. {
  1139. unsigned long ret = PAGE_ALIGN(base + brk_rnd());
  1140. if (ret < base)
  1141. return base;
  1142. return ret;
  1143. }