process.c 51 KB

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