process.c 55 KB

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