process.c 48 KB

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