process.c 52 KB

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