process.c 49 KB

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