process.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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. static void set_debug_reg_defaults(struct thread_struct *thread)
  328. {
  329. if (thread->dabr) {
  330. thread->dabr = 0;
  331. set_dabr(0);
  332. }
  333. }
  334. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  335. int set_dabr(unsigned long dabr)
  336. {
  337. __get_cpu_var(current_dabr) = dabr;
  338. if (ppc_md.set_dabr)
  339. return ppc_md.set_dabr(dabr);
  340. /* XXX should we have a CPU_FTR_HAS_DABR ? */
  341. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  342. mtspr(SPRN_DAC1, dabr);
  343. #ifdef CONFIG_PPC_47x
  344. isync();
  345. #endif
  346. #elif defined(CONFIG_PPC_BOOK3S)
  347. mtspr(SPRN_DABR, dabr);
  348. #endif
  349. return 0;
  350. }
  351. #ifdef CONFIG_PPC64
  352. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  353. #endif
  354. struct task_struct *__switch_to(struct task_struct *prev,
  355. struct task_struct *new)
  356. {
  357. struct thread_struct *new_thread, *old_thread;
  358. unsigned long flags;
  359. struct task_struct *last;
  360. #ifdef CONFIG_SMP
  361. /* avoid complexity of lazy save/restore of fpu
  362. * by just saving it every time we switch out if
  363. * this task used the fpu during the last quantum.
  364. *
  365. * If it tries to use the fpu again, it'll trap and
  366. * reload its fp regs. So we don't have to do a restore
  367. * every switch, just a save.
  368. * -- Cort
  369. */
  370. if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
  371. giveup_fpu(prev);
  372. #ifdef CONFIG_ALTIVEC
  373. /*
  374. * If the previous thread used altivec in the last quantum
  375. * (thus changing altivec regs) then save them.
  376. * We used to check the VRSAVE register but not all apps
  377. * set it, so we don't rely on it now (and in fact we need
  378. * to save & restore VSCR even if VRSAVE == 0). -- paulus
  379. *
  380. * On SMP we always save/restore altivec regs just to avoid the
  381. * complexity of changing processors.
  382. * -- Cort
  383. */
  384. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
  385. giveup_altivec(prev);
  386. #endif /* CONFIG_ALTIVEC */
  387. #ifdef CONFIG_VSX
  388. if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
  389. /* VMX and FPU registers are already save here */
  390. __giveup_vsx(prev);
  391. #endif /* CONFIG_VSX */
  392. #ifdef CONFIG_SPE
  393. /*
  394. * If the previous thread used spe in the last quantum
  395. * (thus changing spe regs) then save them.
  396. *
  397. * On SMP we always save/restore spe regs just to avoid the
  398. * complexity of changing processors.
  399. */
  400. if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
  401. giveup_spe(prev);
  402. #endif /* CONFIG_SPE */
  403. #else /* CONFIG_SMP */
  404. #ifdef CONFIG_ALTIVEC
  405. /* Avoid the trap. On smp this this never happens since
  406. * we don't set last_task_used_altivec -- Cort
  407. */
  408. if (new->thread.regs && last_task_used_altivec == new)
  409. new->thread.regs->msr |= MSR_VEC;
  410. #endif /* CONFIG_ALTIVEC */
  411. #ifdef CONFIG_VSX
  412. if (new->thread.regs && last_task_used_vsx == new)
  413. new->thread.regs->msr |= MSR_VSX;
  414. #endif /* CONFIG_VSX */
  415. #ifdef CONFIG_SPE
  416. /* Avoid the trap. On smp this this never happens since
  417. * we don't set last_task_used_spe
  418. */
  419. if (new->thread.regs && last_task_used_spe == new)
  420. new->thread.regs->msr |= MSR_SPE;
  421. #endif /* CONFIG_SPE */
  422. #endif /* CONFIG_SMP */
  423. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  424. switch_booke_debug_regs(&new->thread);
  425. #else
  426. /*
  427. * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
  428. * schedule DABR
  429. */
  430. #ifndef CONFIG_HAVE_HW_BREAKPOINT
  431. if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr))
  432. set_dabr(new->thread.dabr);
  433. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  434. #endif
  435. new_thread = &new->thread;
  436. old_thread = &current->thread;
  437. #ifdef CONFIG_PPC64
  438. /*
  439. * Collect processor utilization data per process
  440. */
  441. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  442. struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
  443. long unsigned start_tb, current_tb;
  444. start_tb = old_thread->start_tb;
  445. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  446. old_thread->accum_tb += (current_tb - start_tb);
  447. new_thread->start_tb = current_tb;
  448. }
  449. #endif
  450. local_irq_save(flags);
  451. account_system_vtime(current);
  452. account_process_vtime(current);
  453. calculate_steal_time();
  454. /*
  455. * We can't take a PMU exception inside _switch() since there is a
  456. * window where the kernel stack SLB and the kernel stack are out
  457. * of sync. Hard disable here.
  458. */
  459. hard_irq_disable();
  460. last = _switch(old_thread, new_thread);
  461. local_irq_restore(flags);
  462. return last;
  463. }
  464. static int instructions_to_print = 16;
  465. static void show_instructions(struct pt_regs *regs)
  466. {
  467. int i;
  468. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  469. sizeof(int));
  470. printk("Instruction dump:");
  471. for (i = 0; i < instructions_to_print; i++) {
  472. int instr;
  473. if (!(i % 8))
  474. printk("\n");
  475. #if !defined(CONFIG_BOOKE)
  476. /* If executing with the IMMU off, adjust pc rather
  477. * than print XXXXXXXX.
  478. */
  479. if (!(regs->msr & MSR_IR))
  480. pc = (unsigned long)phys_to_virt(pc);
  481. #endif
  482. /* We use __get_user here *only* to avoid an OOPS on a
  483. * bad address because the pc *should* only be a
  484. * kernel address.
  485. */
  486. if (!__kernel_text_address(pc) ||
  487. __get_user(instr, (unsigned int __user *)pc)) {
  488. printk("XXXXXXXX ");
  489. } else {
  490. if (regs->nip == pc)
  491. printk("<%08x> ", instr);
  492. else
  493. printk("%08x ", instr);
  494. }
  495. pc += sizeof(int);
  496. }
  497. printk("\n");
  498. }
  499. static struct regbit {
  500. unsigned long bit;
  501. const char *name;
  502. } msr_bits[] = {
  503. {MSR_EE, "EE"},
  504. {MSR_PR, "PR"},
  505. {MSR_FP, "FP"},
  506. {MSR_VEC, "VEC"},
  507. {MSR_VSX, "VSX"},
  508. {MSR_ME, "ME"},
  509. {MSR_CE, "CE"},
  510. {MSR_DE, "DE"},
  511. {MSR_IR, "IR"},
  512. {MSR_DR, "DR"},
  513. {0, NULL}
  514. };
  515. static void printbits(unsigned long val, struct regbit *bits)
  516. {
  517. const char *sep = "";
  518. printk("<");
  519. for (; bits->bit; ++bits)
  520. if (val & bits->bit) {
  521. printk("%s%s", sep, bits->name);
  522. sep = ",";
  523. }
  524. printk(">");
  525. }
  526. #ifdef CONFIG_PPC64
  527. #define REG "%016lx"
  528. #define REGS_PER_LINE 4
  529. #define LAST_VOLATILE 13
  530. #else
  531. #define REG "%08lx"
  532. #define REGS_PER_LINE 8
  533. #define LAST_VOLATILE 12
  534. #endif
  535. void show_regs(struct pt_regs * regs)
  536. {
  537. int i, trap;
  538. printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
  539. regs->nip, regs->link, regs->ctr);
  540. printk("REGS: %p TRAP: %04lx %s (%s)\n",
  541. regs, regs->trap, print_tainted(), init_utsname()->release);
  542. printk("MSR: "REG" ", regs->msr);
  543. printbits(regs->msr, msr_bits);
  544. printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
  545. trap = TRAP(regs);
  546. if (trap == 0x300 || trap == 0x600)
  547. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  548. printk("DEAR: "REG", ESR: "REG"\n", regs->dar, regs->dsisr);
  549. #else
  550. printk("DAR: "REG", DSISR: "REG"\n", regs->dar, regs->dsisr);
  551. #endif
  552. printk("TASK = %p[%d] '%s' THREAD: %p",
  553. current, task_pid_nr(current), current->comm, task_thread_info(current));
  554. #ifdef CONFIG_SMP
  555. printk(" CPU: %d", raw_smp_processor_id());
  556. #endif /* CONFIG_SMP */
  557. for (i = 0; i < 32; i++) {
  558. if ((i % REGS_PER_LINE) == 0)
  559. printk("\nGPR%02d: ", i);
  560. printk(REG " ", regs->gpr[i]);
  561. if (i == LAST_VOLATILE && !FULL_REGS(regs))
  562. break;
  563. }
  564. printk("\n");
  565. #ifdef CONFIG_KALLSYMS
  566. /*
  567. * Lookup NIP late so we have the best change of getting the
  568. * above info out without failing
  569. */
  570. printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
  571. printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
  572. #endif
  573. show_stack(current, (unsigned long *) regs->gpr[1]);
  574. if (!user_mode(regs))
  575. show_instructions(regs);
  576. }
  577. void exit_thread(void)
  578. {
  579. discard_lazy_cpu_state();
  580. }
  581. void flush_thread(void)
  582. {
  583. discard_lazy_cpu_state();
  584. #ifdef CONFIG_HAVE_HW_BREAKPOINTS
  585. flush_ptrace_hw_breakpoint(current);
  586. #else /* CONFIG_HAVE_HW_BREAKPOINTS */
  587. set_debug_reg_defaults(&current->thread);
  588. #endif /* CONFIG_HAVE_HW_BREAKPOINTS */
  589. }
  590. void
  591. release_thread(struct task_struct *t)
  592. {
  593. }
  594. /*
  595. * This gets called before we allocate a new thread and copy
  596. * the current task into it.
  597. */
  598. void prepare_to_copy(struct task_struct *tsk)
  599. {
  600. flush_fp_to_thread(current);
  601. flush_altivec_to_thread(current);
  602. flush_vsx_to_thread(current);
  603. flush_spe_to_thread(current);
  604. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  605. flush_ptrace_hw_breakpoint(tsk);
  606. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  607. }
  608. /*
  609. * Copy a thread..
  610. */
  611. int copy_thread(unsigned long clone_flags, unsigned long usp,
  612. unsigned long unused, struct task_struct *p,
  613. struct pt_regs *regs)
  614. {
  615. struct pt_regs *childregs, *kregs;
  616. extern void ret_from_fork(void);
  617. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  618. CHECK_FULL_REGS(regs);
  619. /* Copy registers */
  620. sp -= sizeof(struct pt_regs);
  621. childregs = (struct pt_regs *) sp;
  622. *childregs = *regs;
  623. if ((childregs->msr & MSR_PR) == 0) {
  624. /* for kernel thread, set `current' and stackptr in new task */
  625. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  626. #ifdef CONFIG_PPC32
  627. childregs->gpr[2] = (unsigned long) p;
  628. #else
  629. clear_tsk_thread_flag(p, TIF_32BIT);
  630. #endif
  631. p->thread.regs = NULL; /* no user register state */
  632. } else {
  633. childregs->gpr[1] = usp;
  634. p->thread.regs = childregs;
  635. if (clone_flags & CLONE_SETTLS) {
  636. #ifdef CONFIG_PPC64
  637. if (!test_thread_flag(TIF_32BIT))
  638. childregs->gpr[13] = childregs->gpr[6];
  639. else
  640. #endif
  641. childregs->gpr[2] = childregs->gpr[6];
  642. }
  643. }
  644. childregs->gpr[3] = 0; /* Result from fork() */
  645. sp -= STACK_FRAME_OVERHEAD;
  646. /*
  647. * The way this works is that at some point in the future
  648. * some task will call _switch to switch to the new task.
  649. * That will pop off the stack frame created below and start
  650. * the new task running at ret_from_fork. The new task will
  651. * do some house keeping and then return from the fork or clone
  652. * system call, using the stack frame created above.
  653. */
  654. sp -= sizeof(struct pt_regs);
  655. kregs = (struct pt_regs *) sp;
  656. sp -= STACK_FRAME_OVERHEAD;
  657. p->thread.ksp = sp;
  658. p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
  659. _ALIGN_UP(sizeof(struct thread_info), 16);
  660. #ifdef CONFIG_PPC_STD_MMU_64
  661. if (cpu_has_feature(CPU_FTR_SLB)) {
  662. unsigned long sp_vsid;
  663. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  664. if (cpu_has_feature(CPU_FTR_1T_SEGMENT))
  665. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
  666. << SLB_VSID_SHIFT_1T;
  667. else
  668. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
  669. << SLB_VSID_SHIFT;
  670. sp_vsid |= SLB_VSID_KERNEL | llp;
  671. p->thread.ksp_vsid = sp_vsid;
  672. }
  673. #endif /* CONFIG_PPC_STD_MMU_64 */
  674. /*
  675. * The PPC64 ABI makes use of a TOC to contain function
  676. * pointers. The function (ret_from_except) is actually a pointer
  677. * to the TOC entry. The first entry is a pointer to the actual
  678. * function.
  679. */
  680. #ifdef CONFIG_PPC64
  681. kregs->nip = *((unsigned long *)ret_from_fork);
  682. #else
  683. kregs->nip = (unsigned long)ret_from_fork;
  684. #endif
  685. return 0;
  686. }
  687. /*
  688. * Set up a thread for executing a new program
  689. */
  690. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  691. {
  692. #ifdef CONFIG_PPC64
  693. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  694. #endif
  695. set_fs(USER_DS);
  696. /*
  697. * If we exec out of a kernel thread then thread.regs will not be
  698. * set. Do it now.
  699. */
  700. if (!current->thread.regs) {
  701. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  702. current->thread.regs = regs - 1;
  703. }
  704. memset(regs->gpr, 0, sizeof(regs->gpr));
  705. regs->ctr = 0;
  706. regs->link = 0;
  707. regs->xer = 0;
  708. regs->ccr = 0;
  709. regs->gpr[1] = sp;
  710. /*
  711. * We have just cleared all the nonvolatile GPRs, so make
  712. * FULL_REGS(regs) return true. This is necessary to allow
  713. * ptrace to examine the thread immediately after exec.
  714. */
  715. regs->trap &= ~1UL;
  716. #ifdef CONFIG_PPC32
  717. regs->mq = 0;
  718. regs->nip = start;
  719. regs->msr = MSR_USER;
  720. #else
  721. if (!test_thread_flag(TIF_32BIT)) {
  722. unsigned long entry, toc;
  723. /* start is a relocated pointer to the function descriptor for
  724. * the elf _start routine. The first entry in the function
  725. * descriptor is the entry address of _start and the second
  726. * entry is the TOC value we need to use.
  727. */
  728. __get_user(entry, (unsigned long __user *)start);
  729. __get_user(toc, (unsigned long __user *)start+1);
  730. /* Check whether the e_entry function descriptor entries
  731. * need to be relocated before we can use them.
  732. */
  733. if (load_addr != 0) {
  734. entry += load_addr;
  735. toc += load_addr;
  736. }
  737. regs->nip = entry;
  738. regs->gpr[2] = toc;
  739. regs->msr = MSR_USER64;
  740. } else {
  741. regs->nip = start;
  742. regs->gpr[2] = 0;
  743. regs->msr = MSR_USER32;
  744. }
  745. #endif
  746. discard_lazy_cpu_state();
  747. #ifdef CONFIG_VSX
  748. current->thread.used_vsr = 0;
  749. #endif
  750. memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
  751. current->thread.fpscr.val = 0;
  752. #ifdef CONFIG_ALTIVEC
  753. memset(current->thread.vr, 0, sizeof(current->thread.vr));
  754. memset(&current->thread.vscr, 0, sizeof(current->thread.vscr));
  755. current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
  756. current->thread.vrsave = 0;
  757. current->thread.used_vr = 0;
  758. #endif /* CONFIG_ALTIVEC */
  759. #ifdef CONFIG_SPE
  760. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  761. current->thread.acc = 0;
  762. current->thread.spefscr = 0;
  763. current->thread.used_spe = 0;
  764. #endif /* CONFIG_SPE */
  765. }
  766. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  767. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  768. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  769. {
  770. struct pt_regs *regs = tsk->thread.regs;
  771. /* This is a bit hairy. If we are an SPE enabled processor
  772. * (have embedded fp) we store the IEEE exception enable flags in
  773. * fpexc_mode. fpexc_mode is also used for setting FP exception
  774. * mode (asyn, precise, disabled) for 'Classic' FP. */
  775. if (val & PR_FP_EXC_SW_ENABLE) {
  776. #ifdef CONFIG_SPE
  777. if (cpu_has_feature(CPU_FTR_SPE)) {
  778. tsk->thread.fpexc_mode = val &
  779. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  780. return 0;
  781. } else {
  782. return -EINVAL;
  783. }
  784. #else
  785. return -EINVAL;
  786. #endif
  787. }
  788. /* on a CONFIG_SPE this does not hurt us. The bits that
  789. * __pack_fe01 use do not overlap with bits used for
  790. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  791. * on CONFIG_SPE implementations are reserved so writing to
  792. * them does not change anything */
  793. if (val > PR_FP_EXC_PRECISE)
  794. return -EINVAL;
  795. tsk->thread.fpexc_mode = __pack_fe01(val);
  796. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  797. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  798. | tsk->thread.fpexc_mode;
  799. return 0;
  800. }
  801. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  802. {
  803. unsigned int val;
  804. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  805. #ifdef CONFIG_SPE
  806. if (cpu_has_feature(CPU_FTR_SPE))
  807. val = tsk->thread.fpexc_mode;
  808. else
  809. return -EINVAL;
  810. #else
  811. return -EINVAL;
  812. #endif
  813. else
  814. val = __unpack_fe01(tsk->thread.fpexc_mode);
  815. return put_user(val, (unsigned int __user *) adr);
  816. }
  817. int set_endian(struct task_struct *tsk, unsigned int val)
  818. {
  819. struct pt_regs *regs = tsk->thread.regs;
  820. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  821. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  822. return -EINVAL;
  823. if (regs == NULL)
  824. return -EINVAL;
  825. if (val == PR_ENDIAN_BIG)
  826. regs->msr &= ~MSR_LE;
  827. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  828. regs->msr |= MSR_LE;
  829. else
  830. return -EINVAL;
  831. return 0;
  832. }
  833. int get_endian(struct task_struct *tsk, unsigned long adr)
  834. {
  835. struct pt_regs *regs = tsk->thread.regs;
  836. unsigned int val;
  837. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  838. !cpu_has_feature(CPU_FTR_REAL_LE))
  839. return -EINVAL;
  840. if (regs == NULL)
  841. return -EINVAL;
  842. if (regs->msr & MSR_LE) {
  843. if (cpu_has_feature(CPU_FTR_REAL_LE))
  844. val = PR_ENDIAN_LITTLE;
  845. else
  846. val = PR_ENDIAN_PPC_LITTLE;
  847. } else
  848. val = PR_ENDIAN_BIG;
  849. return put_user(val, (unsigned int __user *)adr);
  850. }
  851. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  852. {
  853. tsk->thread.align_ctl = val;
  854. return 0;
  855. }
  856. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  857. {
  858. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  859. }
  860. #define TRUNC_PTR(x) ((typeof(x))(((unsigned long)(x)) & 0xffffffff))
  861. int sys_clone(unsigned long clone_flags, unsigned long usp,
  862. int __user *parent_tidp, void __user *child_threadptr,
  863. int __user *child_tidp, int p6,
  864. struct pt_regs *regs)
  865. {
  866. CHECK_FULL_REGS(regs);
  867. if (usp == 0)
  868. usp = regs->gpr[1]; /* stack pointer for child */
  869. #ifdef CONFIG_PPC64
  870. if (test_thread_flag(TIF_32BIT)) {
  871. parent_tidp = TRUNC_PTR(parent_tidp);
  872. child_tidp = TRUNC_PTR(child_tidp);
  873. }
  874. #endif
  875. return do_fork(clone_flags, usp, regs, 0, parent_tidp, child_tidp);
  876. }
  877. int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
  878. unsigned long p4, unsigned long p5, unsigned long p6,
  879. struct pt_regs *regs)
  880. {
  881. CHECK_FULL_REGS(regs);
  882. return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
  883. }
  884. int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
  885. unsigned long p4, unsigned long p5, unsigned long p6,
  886. struct pt_regs *regs)
  887. {
  888. CHECK_FULL_REGS(regs);
  889. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1],
  890. regs, 0, NULL, NULL);
  891. }
  892. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  893. unsigned long a3, unsigned long a4, unsigned long a5,
  894. struct pt_regs *regs)
  895. {
  896. int error;
  897. char *filename;
  898. filename = getname((char __user *) a0);
  899. error = PTR_ERR(filename);
  900. if (IS_ERR(filename))
  901. goto out;
  902. flush_fp_to_thread(current);
  903. flush_altivec_to_thread(current);
  904. flush_spe_to_thread(current);
  905. error = do_execve(filename, (char __user * __user *) a1,
  906. (char __user * __user *) a2, regs);
  907. putname(filename);
  908. out:
  909. return error;
  910. }
  911. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  912. unsigned long nbytes)
  913. {
  914. unsigned long stack_page;
  915. unsigned long cpu = task_cpu(p);
  916. /*
  917. * Avoid crashing if the stack has overflowed and corrupted
  918. * task_cpu(p), which is in the thread_info struct.
  919. */
  920. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  921. stack_page = (unsigned long) hardirq_ctx[cpu];
  922. if (sp >= stack_page + sizeof(struct thread_struct)
  923. && sp <= stack_page + THREAD_SIZE - nbytes)
  924. return 1;
  925. stack_page = (unsigned long) softirq_ctx[cpu];
  926. if (sp >= stack_page + sizeof(struct thread_struct)
  927. && sp <= stack_page + THREAD_SIZE - nbytes)
  928. return 1;
  929. }
  930. return 0;
  931. }
  932. int validate_sp(unsigned long sp, struct task_struct *p,
  933. unsigned long nbytes)
  934. {
  935. unsigned long stack_page = (unsigned long)task_stack_page(p);
  936. if (sp >= stack_page + sizeof(struct thread_struct)
  937. && sp <= stack_page + THREAD_SIZE - nbytes)
  938. return 1;
  939. return valid_irq_stack(sp, p, nbytes);
  940. }
  941. EXPORT_SYMBOL(validate_sp);
  942. unsigned long get_wchan(struct task_struct *p)
  943. {
  944. unsigned long ip, sp;
  945. int count = 0;
  946. if (!p || p == current || p->state == TASK_RUNNING)
  947. return 0;
  948. sp = p->thread.ksp;
  949. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  950. return 0;
  951. do {
  952. sp = *(unsigned long *)sp;
  953. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  954. return 0;
  955. if (count > 0) {
  956. ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
  957. if (!in_sched_functions(ip))
  958. return ip;
  959. }
  960. } while (count++ < 16);
  961. return 0;
  962. }
  963. static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
  964. void show_stack(struct task_struct *tsk, unsigned long *stack)
  965. {
  966. unsigned long sp, ip, lr, newsp;
  967. int count = 0;
  968. int firstframe = 1;
  969. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  970. int curr_frame = current->curr_ret_stack;
  971. extern void return_to_handler(void);
  972. unsigned long rth = (unsigned long)return_to_handler;
  973. unsigned long mrth = -1;
  974. #ifdef CONFIG_PPC64
  975. extern void mod_return_to_handler(void);
  976. rth = *(unsigned long *)rth;
  977. mrth = (unsigned long)mod_return_to_handler;
  978. mrth = *(unsigned long *)mrth;
  979. #endif
  980. #endif
  981. sp = (unsigned long) stack;
  982. if (tsk == NULL)
  983. tsk = current;
  984. if (sp == 0) {
  985. if (tsk == current)
  986. asm("mr %0,1" : "=r" (sp));
  987. else
  988. sp = tsk->thread.ksp;
  989. }
  990. lr = 0;
  991. printk("Call Trace:\n");
  992. do {
  993. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  994. return;
  995. stack = (unsigned long *) sp;
  996. newsp = stack[0];
  997. ip = stack[STACK_FRAME_LR_SAVE];
  998. if (!firstframe || ip != lr) {
  999. printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
  1000. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1001. if ((ip == rth || ip == mrth) && curr_frame >= 0) {
  1002. printk(" (%pS)",
  1003. (void *)current->ret_stack[curr_frame].ret);
  1004. curr_frame--;
  1005. }
  1006. #endif
  1007. if (firstframe)
  1008. printk(" (unreliable)");
  1009. printk("\n");
  1010. }
  1011. firstframe = 0;
  1012. /*
  1013. * See if this is an exception frame.
  1014. * We look for the "regshere" marker in the current frame.
  1015. */
  1016. if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
  1017. && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  1018. struct pt_regs *regs = (struct pt_regs *)
  1019. (sp + STACK_FRAME_OVERHEAD);
  1020. lr = regs->link;
  1021. printk("--- Exception: %lx at %pS\n LR = %pS\n",
  1022. regs->trap, (void *)regs->nip, (void *)lr);
  1023. firstframe = 1;
  1024. }
  1025. sp = newsp;
  1026. } while (count++ < kstack_depth_to_print);
  1027. }
  1028. void dump_stack(void)
  1029. {
  1030. show_stack(current, NULL);
  1031. }
  1032. EXPORT_SYMBOL(dump_stack);
  1033. #ifdef CONFIG_PPC64
  1034. void ppc64_runlatch_on(void)
  1035. {
  1036. unsigned long ctrl;
  1037. if (cpu_has_feature(CPU_FTR_CTRL) && !test_thread_flag(TIF_RUNLATCH)) {
  1038. HMT_medium();
  1039. ctrl = mfspr(SPRN_CTRLF);
  1040. ctrl |= CTRL_RUNLATCH;
  1041. mtspr(SPRN_CTRLT, ctrl);
  1042. set_thread_flag(TIF_RUNLATCH);
  1043. }
  1044. }
  1045. void ppc64_runlatch_off(void)
  1046. {
  1047. unsigned long ctrl;
  1048. if (cpu_has_feature(CPU_FTR_CTRL) && test_thread_flag(TIF_RUNLATCH)) {
  1049. HMT_medium();
  1050. clear_thread_flag(TIF_RUNLATCH);
  1051. ctrl = mfspr(SPRN_CTRLF);
  1052. ctrl &= ~CTRL_RUNLATCH;
  1053. mtspr(SPRN_CTRLT, ctrl);
  1054. }
  1055. }
  1056. #endif
  1057. #if THREAD_SHIFT < PAGE_SHIFT
  1058. static struct kmem_cache *thread_info_cache;
  1059. struct thread_info *alloc_thread_info(struct task_struct *tsk)
  1060. {
  1061. struct thread_info *ti;
  1062. ti = kmem_cache_alloc(thread_info_cache, GFP_KERNEL);
  1063. if (unlikely(ti == NULL))
  1064. return NULL;
  1065. #ifdef CONFIG_DEBUG_STACK_USAGE
  1066. memset(ti, 0, THREAD_SIZE);
  1067. #endif
  1068. return ti;
  1069. }
  1070. void free_thread_info(struct thread_info *ti)
  1071. {
  1072. kmem_cache_free(thread_info_cache, ti);
  1073. }
  1074. void thread_info_cache_init(void)
  1075. {
  1076. thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
  1077. THREAD_SIZE, 0, NULL);
  1078. BUG_ON(thread_info_cache == NULL);
  1079. }
  1080. #endif /* THREAD_SHIFT < PAGE_SHIFT */
  1081. unsigned long arch_align_stack(unsigned long sp)
  1082. {
  1083. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  1084. sp -= get_random_int() & ~PAGE_MASK;
  1085. return sp & ~0xf;
  1086. }
  1087. static inline unsigned long brk_rnd(void)
  1088. {
  1089. unsigned long rnd = 0;
  1090. /* 8MB for 32bit, 1GB for 64bit */
  1091. if (is_32bit_task())
  1092. rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
  1093. else
  1094. rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
  1095. return rnd << PAGE_SHIFT;
  1096. }
  1097. unsigned long arch_randomize_brk(struct mm_struct *mm)
  1098. {
  1099. unsigned long base = mm->brk;
  1100. unsigned long ret;
  1101. #ifdef CONFIG_PPC_STD_MMU_64
  1102. /*
  1103. * If we are using 1TB segments and we are allowed to randomise
  1104. * the heap, we can put it above 1TB so it is backed by a 1TB
  1105. * segment. Otherwise the heap will be in the bottom 1TB
  1106. * which always uses 256MB segments and this may result in a
  1107. * performance penalty.
  1108. */
  1109. if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
  1110. base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
  1111. #endif
  1112. ret = PAGE_ALIGN(base + brk_rnd());
  1113. if (ret < mm->brk)
  1114. return mm->brk;
  1115. return ret;
  1116. }
  1117. unsigned long randomize_et_dyn(unsigned long base)
  1118. {
  1119. unsigned long ret = PAGE_ALIGN(base + brk_rnd());
  1120. if (ret < base)
  1121. return base;
  1122. return ret;
  1123. }