process.c 55 KB

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