process.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  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. #ifdef CONFIG_PPC64
  729. DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
  730. #endif
  731. static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
  732. struct arch_hw_breakpoint *b)
  733. {
  734. if (a->address != b->address)
  735. return false;
  736. if (a->type != b->type)
  737. return false;
  738. if (a->len != b->len)
  739. return false;
  740. return true;
  741. }
  742. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  743. static inline bool tm_enabled(struct task_struct *tsk)
  744. {
  745. return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM);
  746. }
  747. static void tm_reclaim_thread(struct thread_struct *thr,
  748. struct thread_info *ti, uint8_t cause)
  749. {
  750. /*
  751. * Use the current MSR TM suspended bit to track if we have
  752. * checkpointed state outstanding.
  753. * On signal delivery, we'd normally reclaim the checkpointed
  754. * state to obtain stack pointer (see:get_tm_stackpointer()).
  755. * This will then directly return to userspace without going
  756. * through __switch_to(). However, if the stack frame is bad,
  757. * we need to exit this thread which calls __switch_to() which
  758. * will again attempt to reclaim the already saved tm state.
  759. * Hence we need to check that we've not already reclaimed
  760. * this state.
  761. * We do this using the current MSR, rather tracking it in
  762. * some specific thread_struct bit, as it has the additional
  763. * benefit of checking for a potential TM bad thing exception.
  764. */
  765. if (!MSR_TM_SUSPENDED(mfmsr()))
  766. return;
  767. giveup_all(container_of(thr, struct task_struct, thread));
  768. tm_reclaim(thr, cause);
  769. /*
  770. * If we are in a transaction and FP is off then we can't have
  771. * used FP inside that transaction. Hence the checkpointed
  772. * state is the same as the live state. We need to copy the
  773. * live state to the checkpointed state so that when the
  774. * transaction is restored, the checkpointed state is correct
  775. * and the aborted transaction sees the correct state. We use
  776. * ckpt_regs.msr here as that's what tm_reclaim will use to
  777. * determine if it's going to write the checkpointed state or
  778. * not. So either this will write the checkpointed registers,
  779. * or reclaim will. Similarly for VMX.
  780. */
  781. if ((thr->ckpt_regs.msr & MSR_FP) == 0)
  782. memcpy(&thr->ckfp_state, &thr->fp_state,
  783. sizeof(struct thread_fp_state));
  784. if ((thr->ckpt_regs.msr & MSR_VEC) == 0)
  785. memcpy(&thr->ckvr_state, &thr->vr_state,
  786. sizeof(struct thread_vr_state));
  787. }
  788. void tm_reclaim_current(uint8_t cause)
  789. {
  790. tm_enable();
  791. tm_reclaim_thread(&current->thread, current_thread_info(), cause);
  792. }
  793. static inline void tm_reclaim_task(struct task_struct *tsk)
  794. {
  795. /* We have to work out if we're switching from/to a task that's in the
  796. * middle of a transaction.
  797. *
  798. * In switching we need to maintain a 2nd register state as
  799. * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
  800. * checkpointed (tbegin) state in ckpt_regs, ckfp_state and
  801. * ckvr_state
  802. *
  803. * We also context switch (save) TFHAR/TEXASR/TFIAR in here.
  804. */
  805. struct thread_struct *thr = &tsk->thread;
  806. if (!thr->regs)
  807. return;
  808. if (!MSR_TM_ACTIVE(thr->regs->msr))
  809. goto out_and_saveregs;
  810. WARN_ON(tm_suspend_disabled);
  811. TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
  812. "ccr=%lx, msr=%lx, trap=%lx)\n",
  813. tsk->pid, thr->regs->nip,
  814. thr->regs->ccr, thr->regs->msr,
  815. thr->regs->trap);
  816. tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
  817. TM_DEBUG("--- tm_reclaim on pid %d complete\n",
  818. tsk->pid);
  819. out_and_saveregs:
  820. /* Always save the regs here, even if a transaction's not active.
  821. * This context-switches a thread's TM info SPRs. We do it here to
  822. * be consistent with the restore path (in recheckpoint) which
  823. * cannot happen later in _switch().
  824. */
  825. tm_save_sprs(thr);
  826. }
  827. extern void __tm_recheckpoint(struct thread_struct *thread);
  828. void tm_recheckpoint(struct thread_struct *thread)
  829. {
  830. unsigned long flags;
  831. if (!(thread->regs->msr & MSR_TM))
  832. return;
  833. /* We really can't be interrupted here as the TEXASR registers can't
  834. * change and later in the trecheckpoint code, we have a userspace R1.
  835. * So let's hard disable over this region.
  836. */
  837. local_irq_save(flags);
  838. hard_irq_disable();
  839. /* The TM SPRs are restored here, so that TEXASR.FS can be set
  840. * before the trecheckpoint and no explosion occurs.
  841. */
  842. tm_restore_sprs(thread);
  843. __tm_recheckpoint(thread);
  844. local_irq_restore(flags);
  845. }
  846. static inline void tm_recheckpoint_new_task(struct task_struct *new)
  847. {
  848. if (!cpu_has_feature(CPU_FTR_TM))
  849. return;
  850. /* Recheckpoint the registers of the thread we're about to switch to.
  851. *
  852. * If the task was using FP, we non-lazily reload both the original and
  853. * the speculative FP register states. This is because the kernel
  854. * doesn't see if/when a TM rollback occurs, so if we take an FP
  855. * unavailable later, we are unable to determine which set of FP regs
  856. * need to be restored.
  857. */
  858. if (!tm_enabled(new))
  859. return;
  860. if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
  861. tm_restore_sprs(&new->thread);
  862. return;
  863. }
  864. /* Recheckpoint to restore original checkpointed register state. */
  865. TM_DEBUG("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n",
  866. new->pid, new->thread.regs->msr);
  867. tm_recheckpoint(&new->thread);
  868. /*
  869. * The checkpointed state has been restored but the live state has
  870. * not, ensure all the math functionality is turned off to trigger
  871. * restore_math() to reload.
  872. */
  873. new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX);
  874. TM_DEBUG("*** tm_recheckpoint of pid %d complete "
  875. "(kernel msr 0x%lx)\n",
  876. new->pid, mfmsr());
  877. }
  878. static inline void __switch_to_tm(struct task_struct *prev,
  879. struct task_struct *new)
  880. {
  881. if (cpu_has_feature(CPU_FTR_TM)) {
  882. if (tm_enabled(prev) || tm_enabled(new))
  883. tm_enable();
  884. if (tm_enabled(prev)) {
  885. prev->thread.load_tm++;
  886. tm_reclaim_task(prev);
  887. if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0)
  888. prev->thread.regs->msr &= ~MSR_TM;
  889. }
  890. tm_recheckpoint_new_task(new);
  891. }
  892. }
  893. /*
  894. * This is called if we are on the way out to userspace and the
  895. * TIF_RESTORE_TM flag is set. It checks if we need to reload
  896. * FP and/or vector state and does so if necessary.
  897. * If userspace is inside a transaction (whether active or
  898. * suspended) and FP/VMX/VSX instructions have ever been enabled
  899. * inside that transaction, then we have to keep them enabled
  900. * and keep the FP/VMX/VSX state loaded while ever the transaction
  901. * continues. The reason is that if we didn't, and subsequently
  902. * got a FP/VMX/VSX unavailable interrupt inside a transaction,
  903. * we don't know whether it's the same transaction, and thus we
  904. * don't know which of the checkpointed state and the transactional
  905. * state to use.
  906. */
  907. void restore_tm_state(struct pt_regs *regs)
  908. {
  909. unsigned long msr_diff;
  910. /*
  911. * This is the only moment we should clear TIF_RESTORE_TM as
  912. * it is here that ckpt_regs.msr and pt_regs.msr become the same
  913. * again, anything else could lead to an incorrect ckpt_msr being
  914. * saved and therefore incorrect signal contexts.
  915. */
  916. clear_thread_flag(TIF_RESTORE_TM);
  917. if (!MSR_TM_ACTIVE(regs->msr))
  918. return;
  919. msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
  920. msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
  921. /* Ensure that restore_math() will restore */
  922. if (msr_diff & MSR_FP)
  923. current->thread.load_fp = 1;
  924. #ifdef CONFIG_ALTIVEC
  925. if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC)
  926. current->thread.load_vec = 1;
  927. #endif
  928. restore_math(regs);
  929. regs->msr |= msr_diff;
  930. }
  931. #else
  932. #define tm_recheckpoint_new_task(new)
  933. #define __switch_to_tm(prev, new)
  934. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  935. static inline void save_sprs(struct thread_struct *t)
  936. {
  937. #ifdef CONFIG_ALTIVEC
  938. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  939. t->vrsave = mfspr(SPRN_VRSAVE);
  940. #endif
  941. #ifdef CONFIG_PPC_BOOK3S_64
  942. if (cpu_has_feature(CPU_FTR_DSCR))
  943. t->dscr = mfspr(SPRN_DSCR);
  944. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  945. t->bescr = mfspr(SPRN_BESCR);
  946. t->ebbhr = mfspr(SPRN_EBBHR);
  947. t->ebbrr = mfspr(SPRN_EBBRR);
  948. t->fscr = mfspr(SPRN_FSCR);
  949. /*
  950. * Note that the TAR is not available for use in the kernel.
  951. * (To provide this, the TAR should be backed up/restored on
  952. * exception entry/exit instead, and be in pt_regs. FIXME,
  953. * this should be in pt_regs anyway (for debug).)
  954. */
  955. t->tar = mfspr(SPRN_TAR);
  956. }
  957. #endif
  958. thread_pkey_regs_save(t);
  959. }
  960. static inline void restore_sprs(struct thread_struct *old_thread,
  961. struct thread_struct *new_thread)
  962. {
  963. #ifdef CONFIG_ALTIVEC
  964. if (cpu_has_feature(CPU_FTR_ALTIVEC) &&
  965. old_thread->vrsave != new_thread->vrsave)
  966. mtspr(SPRN_VRSAVE, new_thread->vrsave);
  967. #endif
  968. #ifdef CONFIG_PPC_BOOK3S_64
  969. if (cpu_has_feature(CPU_FTR_DSCR)) {
  970. u64 dscr = get_paca()->dscr_default;
  971. if (new_thread->dscr_inherit)
  972. dscr = new_thread->dscr;
  973. if (old_thread->dscr != dscr)
  974. mtspr(SPRN_DSCR, dscr);
  975. }
  976. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  977. if (old_thread->bescr != new_thread->bescr)
  978. mtspr(SPRN_BESCR, new_thread->bescr);
  979. if (old_thread->ebbhr != new_thread->ebbhr)
  980. mtspr(SPRN_EBBHR, new_thread->ebbhr);
  981. if (old_thread->ebbrr != new_thread->ebbrr)
  982. mtspr(SPRN_EBBRR, new_thread->ebbrr);
  983. if (old_thread->fscr != new_thread->fscr)
  984. mtspr(SPRN_FSCR, new_thread->fscr);
  985. if (old_thread->tar != new_thread->tar)
  986. mtspr(SPRN_TAR, new_thread->tar);
  987. }
  988. if (cpu_has_feature(CPU_FTR_ARCH_300) &&
  989. old_thread->tidr != new_thread->tidr)
  990. mtspr(SPRN_TIDR, new_thread->tidr);
  991. #endif
  992. thread_pkey_regs_restore(new_thread, old_thread);
  993. }
  994. #ifdef CONFIG_PPC_BOOK3S_64
  995. #define CP_SIZE 128
  996. static const u8 dummy_copy_buffer[CP_SIZE] __attribute__((aligned(CP_SIZE)));
  997. #endif
  998. struct task_struct *__switch_to(struct task_struct *prev,
  999. struct task_struct *new)
  1000. {
  1001. struct thread_struct *new_thread, *old_thread;
  1002. struct task_struct *last;
  1003. #ifdef CONFIG_PPC_BOOK3S_64
  1004. struct ppc64_tlb_batch *batch;
  1005. #endif
  1006. new_thread = &new->thread;
  1007. old_thread = &current->thread;
  1008. WARN_ON(!irqs_disabled());
  1009. #ifdef CONFIG_PPC64
  1010. /*
  1011. * Collect processor utilization data per process
  1012. */
  1013. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  1014. struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
  1015. long unsigned start_tb, current_tb;
  1016. start_tb = old_thread->start_tb;
  1017. cu->current_tb = current_tb = mfspr(SPRN_PURR);
  1018. old_thread->accum_tb += (current_tb - start_tb);
  1019. new_thread->start_tb = current_tb;
  1020. }
  1021. #endif /* CONFIG_PPC64 */
  1022. #ifdef CONFIG_PPC_BOOK3S_64
  1023. batch = this_cpu_ptr(&ppc64_tlb_batch);
  1024. if (batch->active) {
  1025. current_thread_info()->local_flags |= _TLF_LAZY_MMU;
  1026. if (batch->index)
  1027. __flush_tlb_pending(batch);
  1028. batch->active = 0;
  1029. }
  1030. #endif /* CONFIG_PPC_BOOK3S_64 */
  1031. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1032. switch_booke_debug_regs(&new->thread.debug);
  1033. #else
  1034. /*
  1035. * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
  1036. * schedule DABR
  1037. */
  1038. #ifndef CONFIG_HAVE_HW_BREAKPOINT
  1039. if (unlikely(!hw_brk_match(this_cpu_ptr(&current_brk), &new->thread.hw_brk)))
  1040. __set_breakpoint(&new->thread.hw_brk);
  1041. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  1042. #endif
  1043. /*
  1044. * We need to save SPRs before treclaim/trecheckpoint as these will
  1045. * change a number of them.
  1046. */
  1047. save_sprs(&prev->thread);
  1048. /* Save FPU, Altivec, VSX and SPE state */
  1049. giveup_all(prev);
  1050. __switch_to_tm(prev, new);
  1051. if (!radix_enabled()) {
  1052. /*
  1053. * We can't take a PMU exception inside _switch() since there
  1054. * is a window where the kernel stack SLB and the kernel stack
  1055. * are out of sync. Hard disable here.
  1056. */
  1057. hard_irq_disable();
  1058. }
  1059. /*
  1060. * Call restore_sprs() before calling _switch(). If we move it after
  1061. * _switch() then we miss out on calling it for new tasks. The reason
  1062. * for this is we manually create a stack frame for new tasks that
  1063. * directly returns through ret_from_fork() or
  1064. * ret_from_kernel_thread(). See copy_thread() for details.
  1065. */
  1066. restore_sprs(old_thread, new_thread);
  1067. last = _switch(old_thread, new_thread);
  1068. #ifdef CONFIG_PPC_BOOK3S_64
  1069. if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
  1070. current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
  1071. batch = this_cpu_ptr(&ppc64_tlb_batch);
  1072. batch->active = 1;
  1073. }
  1074. if (current_thread_info()->task->thread.regs) {
  1075. restore_math(current_thread_info()->task->thread.regs);
  1076. /*
  1077. * The copy-paste buffer can only store into foreign real
  1078. * addresses, so unprivileged processes can not see the
  1079. * data or use it in any way unless they have foreign real
  1080. * mappings. If the new process has the foreign real address
  1081. * mappings, we must issue a cp_abort to clear any state and
  1082. * prevent snooping, corruption or a covert channel.
  1083. *
  1084. * DD1 allows paste into normal system memory so we do an
  1085. * unpaired copy, rather than cp_abort, to clear the buffer,
  1086. * since cp_abort is quite expensive.
  1087. */
  1088. if (current_thread_info()->task->thread.used_vas) {
  1089. asm volatile(PPC_CP_ABORT);
  1090. } else if (cpu_has_feature(CPU_FTR_POWER9_DD1)) {
  1091. asm volatile(PPC_COPY(%0, %1)
  1092. : : "r"(dummy_copy_buffer), "r"(0));
  1093. }
  1094. }
  1095. #endif /* CONFIG_PPC_BOOK3S_64 */
  1096. return last;
  1097. }
  1098. static int instructions_to_print = 16;
  1099. static void show_instructions(struct pt_regs *regs)
  1100. {
  1101. int i;
  1102. unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
  1103. sizeof(int));
  1104. printk("Instruction dump:");
  1105. for (i = 0; i < instructions_to_print; i++) {
  1106. int instr;
  1107. if (!(i % 8))
  1108. pr_cont("\n");
  1109. #if !defined(CONFIG_BOOKE)
  1110. /* If executing with the IMMU off, adjust pc rather
  1111. * than print XXXXXXXX.
  1112. */
  1113. if (!(regs->msr & MSR_IR))
  1114. pc = (unsigned long)phys_to_virt(pc);
  1115. #endif
  1116. if (!__kernel_text_address(pc) ||
  1117. probe_kernel_address((unsigned int __user *)pc, instr)) {
  1118. pr_cont("XXXXXXXX ");
  1119. } else {
  1120. if (regs->nip == pc)
  1121. pr_cont("<%08x> ", instr);
  1122. else
  1123. pr_cont("%08x ", instr);
  1124. }
  1125. pc += sizeof(int);
  1126. }
  1127. pr_cont("\n");
  1128. }
  1129. struct regbit {
  1130. unsigned long bit;
  1131. const char *name;
  1132. };
  1133. static struct regbit msr_bits[] = {
  1134. #if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
  1135. {MSR_SF, "SF"},
  1136. {MSR_HV, "HV"},
  1137. #endif
  1138. {MSR_VEC, "VEC"},
  1139. {MSR_VSX, "VSX"},
  1140. #ifdef CONFIG_BOOKE
  1141. {MSR_CE, "CE"},
  1142. #endif
  1143. {MSR_EE, "EE"},
  1144. {MSR_PR, "PR"},
  1145. {MSR_FP, "FP"},
  1146. {MSR_ME, "ME"},
  1147. #ifdef CONFIG_BOOKE
  1148. {MSR_DE, "DE"},
  1149. #else
  1150. {MSR_SE, "SE"},
  1151. {MSR_BE, "BE"},
  1152. #endif
  1153. {MSR_IR, "IR"},
  1154. {MSR_DR, "DR"},
  1155. {MSR_PMM, "PMM"},
  1156. #ifndef CONFIG_BOOKE
  1157. {MSR_RI, "RI"},
  1158. {MSR_LE, "LE"},
  1159. #endif
  1160. {0, NULL}
  1161. };
  1162. static void print_bits(unsigned long val, struct regbit *bits, const char *sep)
  1163. {
  1164. const char *s = "";
  1165. for (; bits->bit; ++bits)
  1166. if (val & bits->bit) {
  1167. pr_cont("%s%s", s, bits->name);
  1168. s = sep;
  1169. }
  1170. }
  1171. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1172. static struct regbit msr_tm_bits[] = {
  1173. {MSR_TS_T, "T"},
  1174. {MSR_TS_S, "S"},
  1175. {MSR_TM, "E"},
  1176. {0, NULL}
  1177. };
  1178. static void print_tm_bits(unsigned long val)
  1179. {
  1180. /*
  1181. * This only prints something if at least one of the TM bit is set.
  1182. * Inside the TM[], the output means:
  1183. * E: Enabled (bit 32)
  1184. * S: Suspended (bit 33)
  1185. * T: Transactional (bit 34)
  1186. */
  1187. if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) {
  1188. pr_cont(",TM[");
  1189. print_bits(val, msr_tm_bits, "");
  1190. pr_cont("]");
  1191. }
  1192. }
  1193. #else
  1194. static void print_tm_bits(unsigned long val) {}
  1195. #endif
  1196. static void print_msr_bits(unsigned long val)
  1197. {
  1198. pr_cont("<");
  1199. print_bits(val, msr_bits, ",");
  1200. print_tm_bits(val);
  1201. pr_cont(">");
  1202. }
  1203. #ifdef CONFIG_PPC64
  1204. #define REG "%016lx"
  1205. #define REGS_PER_LINE 4
  1206. #define LAST_VOLATILE 13
  1207. #else
  1208. #define REG "%08lx"
  1209. #define REGS_PER_LINE 8
  1210. #define LAST_VOLATILE 12
  1211. #endif
  1212. void show_regs(struct pt_regs * regs)
  1213. {
  1214. int i, trap;
  1215. show_regs_print_info(KERN_DEFAULT);
  1216. printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
  1217. regs->nip, regs->link, regs->ctr);
  1218. printk("REGS: %px TRAP: %04lx %s (%s)\n",
  1219. regs, regs->trap, print_tainted(), init_utsname()->release);
  1220. printk("MSR: "REG" ", regs->msr);
  1221. print_msr_bits(regs->msr);
  1222. pr_cont(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
  1223. trap = TRAP(regs);
  1224. if ((TRAP(regs) != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
  1225. pr_cont("CFAR: "REG" ", regs->orig_gpr3);
  1226. if (trap == 0x200 || trap == 0x300 || trap == 0x600)
  1227. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  1228. pr_cont("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
  1229. #else
  1230. pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
  1231. #endif
  1232. #ifdef CONFIG_PPC64
  1233. pr_cont("SOFTE: %ld ", regs->softe);
  1234. #endif
  1235. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1236. if (MSR_TM_ACTIVE(regs->msr))
  1237. pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
  1238. #endif
  1239. for (i = 0; i < 32; i++) {
  1240. if ((i % REGS_PER_LINE) == 0)
  1241. pr_cont("\nGPR%02d: ", i);
  1242. pr_cont(REG " ", regs->gpr[i]);
  1243. if (i == LAST_VOLATILE && !FULL_REGS(regs))
  1244. break;
  1245. }
  1246. pr_cont("\n");
  1247. #ifdef CONFIG_KALLSYMS
  1248. /*
  1249. * Lookup NIP late so we have the best change of getting the
  1250. * above info out without failing
  1251. */
  1252. printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
  1253. printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
  1254. #endif
  1255. show_stack(current, (unsigned long *) regs->gpr[1]);
  1256. if (!user_mode(regs))
  1257. show_instructions(regs);
  1258. }
  1259. void flush_thread(void)
  1260. {
  1261. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1262. flush_ptrace_hw_breakpoint(current);
  1263. #else /* CONFIG_HAVE_HW_BREAKPOINT */
  1264. set_debug_reg_defaults(&current->thread);
  1265. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  1266. }
  1267. int set_thread_uses_vas(void)
  1268. {
  1269. #ifdef CONFIG_PPC_BOOK3S_64
  1270. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  1271. return -EINVAL;
  1272. current->thread.used_vas = 1;
  1273. /*
  1274. * Even a process that has no foreign real address mapping can use
  1275. * an unpaired COPY instruction (to no real effect). Issue CP_ABORT
  1276. * to clear any pending COPY and prevent a covert channel.
  1277. *
  1278. * __switch_to() will issue CP_ABORT on future context switches.
  1279. */
  1280. asm volatile(PPC_CP_ABORT);
  1281. #endif /* CONFIG_PPC_BOOK3S_64 */
  1282. return 0;
  1283. }
  1284. #ifdef CONFIG_PPC64
  1285. static DEFINE_SPINLOCK(vas_thread_id_lock);
  1286. static DEFINE_IDA(vas_thread_ida);
  1287. /*
  1288. * We need to assign a unique thread id to each thread in a process.
  1289. *
  1290. * This thread id, referred to as TIDR, and separate from the Linux's tgid,
  1291. * is intended to be used to direct an ASB_Notify from the hardware to the
  1292. * thread, when a suitable event occurs in the system.
  1293. *
  1294. * One such event is a "paste" instruction in the context of Fast Thread
  1295. * Wakeup (aka Core-to-core wake up in the Virtual Accelerator Switchboard
  1296. * (VAS) in POWER9.
  1297. *
  1298. * To get a unique TIDR per process we could simply reuse task_pid_nr() but
  1299. * the problem is that task_pid_nr() is not yet available copy_thread() is
  1300. * called. Fixing that would require changing more intrusive arch-neutral
  1301. * code in code path in copy_process()?.
  1302. *
  1303. * Further, to assign unique TIDRs within each process, we need an atomic
  1304. * field (or an IDR) in task_struct, which again intrudes into the arch-
  1305. * neutral code. So try to assign globally unique TIDRs for now.
  1306. *
  1307. * NOTE: TIDR 0 indicates that the thread does not need a TIDR value.
  1308. * For now, only threads that expect to be notified by the VAS
  1309. * hardware need a TIDR value and we assign values > 0 for those.
  1310. */
  1311. #define MAX_THREAD_CONTEXT ((1 << 16) - 1)
  1312. static int assign_thread_tidr(void)
  1313. {
  1314. int index;
  1315. int err;
  1316. unsigned long flags;
  1317. again:
  1318. if (!ida_pre_get(&vas_thread_ida, GFP_KERNEL))
  1319. return -ENOMEM;
  1320. spin_lock_irqsave(&vas_thread_id_lock, flags);
  1321. err = ida_get_new_above(&vas_thread_ida, 1, &index);
  1322. spin_unlock_irqrestore(&vas_thread_id_lock, flags);
  1323. if (err == -EAGAIN)
  1324. goto again;
  1325. else if (err)
  1326. return err;
  1327. if (index > MAX_THREAD_CONTEXT) {
  1328. spin_lock_irqsave(&vas_thread_id_lock, flags);
  1329. ida_remove(&vas_thread_ida, index);
  1330. spin_unlock_irqrestore(&vas_thread_id_lock, flags);
  1331. return -ENOMEM;
  1332. }
  1333. return index;
  1334. }
  1335. static void free_thread_tidr(int id)
  1336. {
  1337. unsigned long flags;
  1338. spin_lock_irqsave(&vas_thread_id_lock, flags);
  1339. ida_remove(&vas_thread_ida, id);
  1340. spin_unlock_irqrestore(&vas_thread_id_lock, flags);
  1341. }
  1342. /*
  1343. * Clear any TIDR value assigned to this thread.
  1344. */
  1345. void clear_thread_tidr(struct task_struct *t)
  1346. {
  1347. if (!t->thread.tidr)
  1348. return;
  1349. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  1350. WARN_ON_ONCE(1);
  1351. return;
  1352. }
  1353. mtspr(SPRN_TIDR, 0);
  1354. free_thread_tidr(t->thread.tidr);
  1355. t->thread.tidr = 0;
  1356. }
  1357. void arch_release_task_struct(struct task_struct *t)
  1358. {
  1359. clear_thread_tidr(t);
  1360. }
  1361. /*
  1362. * Assign a unique TIDR (thread id) for task @t and set it in the thread
  1363. * structure. For now, we only support setting TIDR for 'current' task.
  1364. */
  1365. int set_thread_tidr(struct task_struct *t)
  1366. {
  1367. int rc;
  1368. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  1369. return -EINVAL;
  1370. if (t != current)
  1371. return -EINVAL;
  1372. if (t->thread.tidr)
  1373. return 0;
  1374. rc = assign_thread_tidr();
  1375. if (rc < 0)
  1376. return rc;
  1377. t->thread.tidr = rc;
  1378. mtspr(SPRN_TIDR, t->thread.tidr);
  1379. return 0;
  1380. }
  1381. EXPORT_SYMBOL_GPL(set_thread_tidr);
  1382. #endif /* CONFIG_PPC64 */
  1383. void
  1384. release_thread(struct task_struct *t)
  1385. {
  1386. }
  1387. /*
  1388. * this gets called so that we can store coprocessor state into memory and
  1389. * copy the current task into the new thread.
  1390. */
  1391. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  1392. {
  1393. flush_all_to_thread(src);
  1394. /*
  1395. * Flush TM state out so we can copy it. __switch_to_tm() does this
  1396. * flush but it removes the checkpointed state from the current CPU and
  1397. * transitions the CPU out of TM mode. Hence we need to call
  1398. * tm_recheckpoint_new_task() (on the same task) to restore the
  1399. * checkpointed state back and the TM mode.
  1400. *
  1401. * Can't pass dst because it isn't ready. Doesn't matter, passing
  1402. * dst is only important for __switch_to()
  1403. */
  1404. __switch_to_tm(src, src);
  1405. *dst = *src;
  1406. clear_task_ebb(dst);
  1407. return 0;
  1408. }
  1409. static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
  1410. {
  1411. #ifdef CONFIG_PPC_BOOK3S_64
  1412. unsigned long sp_vsid;
  1413. unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
  1414. if (radix_enabled())
  1415. return;
  1416. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1417. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
  1418. << SLB_VSID_SHIFT_1T;
  1419. else
  1420. sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
  1421. << SLB_VSID_SHIFT;
  1422. sp_vsid |= SLB_VSID_KERNEL | llp;
  1423. p->thread.ksp_vsid = sp_vsid;
  1424. #endif
  1425. }
  1426. /*
  1427. * Copy a thread..
  1428. */
  1429. /*
  1430. * Copy architecture-specific thread state
  1431. */
  1432. int copy_thread(unsigned long clone_flags, unsigned long usp,
  1433. unsigned long kthread_arg, struct task_struct *p)
  1434. {
  1435. struct pt_regs *childregs, *kregs;
  1436. extern void ret_from_fork(void);
  1437. extern void ret_from_kernel_thread(void);
  1438. void (*f)(void);
  1439. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  1440. struct thread_info *ti = task_thread_info(p);
  1441. klp_init_thread_info(ti);
  1442. /* Copy registers */
  1443. sp -= sizeof(struct pt_regs);
  1444. childregs = (struct pt_regs *) sp;
  1445. if (unlikely(p->flags & PF_KTHREAD)) {
  1446. /* kernel thread */
  1447. memset(childregs, 0, sizeof(struct pt_regs));
  1448. childregs->gpr[1] = sp + sizeof(struct pt_regs);
  1449. /* function */
  1450. if (usp)
  1451. childregs->gpr[14] = ppc_function_entry((void *)usp);
  1452. #ifdef CONFIG_PPC64
  1453. clear_tsk_thread_flag(p, TIF_32BIT);
  1454. childregs->softe = IRQS_ENABLED;
  1455. #endif
  1456. childregs->gpr[15] = kthread_arg;
  1457. p->thread.regs = NULL; /* no user register state */
  1458. ti->flags |= _TIF_RESTOREALL;
  1459. f = ret_from_kernel_thread;
  1460. } else {
  1461. /* user thread */
  1462. struct pt_regs *regs = current_pt_regs();
  1463. CHECK_FULL_REGS(regs);
  1464. *childregs = *regs;
  1465. if (usp)
  1466. childregs->gpr[1] = usp;
  1467. p->thread.regs = childregs;
  1468. childregs->gpr[3] = 0; /* Result from fork() */
  1469. if (clone_flags & CLONE_SETTLS) {
  1470. #ifdef CONFIG_PPC64
  1471. if (!is_32bit_task())
  1472. childregs->gpr[13] = childregs->gpr[6];
  1473. else
  1474. #endif
  1475. childregs->gpr[2] = childregs->gpr[6];
  1476. }
  1477. f = ret_from_fork;
  1478. }
  1479. childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
  1480. sp -= STACK_FRAME_OVERHEAD;
  1481. /*
  1482. * The way this works is that at some point in the future
  1483. * some task will call _switch to switch to the new task.
  1484. * That will pop off the stack frame created below and start
  1485. * the new task running at ret_from_fork. The new task will
  1486. * do some house keeping and then return from the fork or clone
  1487. * system call, using the stack frame created above.
  1488. */
  1489. ((unsigned long *)sp)[0] = 0;
  1490. sp -= sizeof(struct pt_regs);
  1491. kregs = (struct pt_regs *) sp;
  1492. sp -= STACK_FRAME_OVERHEAD;
  1493. p->thread.ksp = sp;
  1494. #ifdef CONFIG_PPC32
  1495. p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
  1496. _ALIGN_UP(sizeof(struct thread_info), 16);
  1497. #endif
  1498. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1499. p->thread.ptrace_bps[0] = NULL;
  1500. #endif
  1501. p->thread.fp_save_area = NULL;
  1502. #ifdef CONFIG_ALTIVEC
  1503. p->thread.vr_save_area = NULL;
  1504. #endif
  1505. setup_ksp_vsid(p, sp);
  1506. #ifdef CONFIG_PPC64
  1507. if (cpu_has_feature(CPU_FTR_DSCR)) {
  1508. p->thread.dscr_inherit = current->thread.dscr_inherit;
  1509. p->thread.dscr = mfspr(SPRN_DSCR);
  1510. }
  1511. if (cpu_has_feature(CPU_FTR_HAS_PPR))
  1512. p->thread.ppr = INIT_PPR;
  1513. p->thread.tidr = 0;
  1514. #endif
  1515. kregs->nip = ppc_function_entry(f);
  1516. return 0;
  1517. }
  1518. /*
  1519. * Set up a thread for executing a new program
  1520. */
  1521. void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
  1522. {
  1523. #ifdef CONFIG_PPC64
  1524. unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
  1525. #endif
  1526. /*
  1527. * If we exec out of a kernel thread then thread.regs will not be
  1528. * set. Do it now.
  1529. */
  1530. if (!current->thread.regs) {
  1531. struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
  1532. current->thread.regs = regs - 1;
  1533. }
  1534. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1535. /*
  1536. * Clear any transactional state, we're exec()ing. The cause is
  1537. * not important as there will never be a recheckpoint so it's not
  1538. * user visible.
  1539. */
  1540. if (MSR_TM_SUSPENDED(mfmsr()))
  1541. tm_reclaim_current(0);
  1542. #endif
  1543. memset(regs->gpr, 0, sizeof(regs->gpr));
  1544. regs->ctr = 0;
  1545. regs->link = 0;
  1546. regs->xer = 0;
  1547. regs->ccr = 0;
  1548. regs->gpr[1] = sp;
  1549. /*
  1550. * We have just cleared all the nonvolatile GPRs, so make
  1551. * FULL_REGS(regs) return true. This is necessary to allow
  1552. * ptrace to examine the thread immediately after exec.
  1553. */
  1554. regs->trap &= ~1UL;
  1555. #ifdef CONFIG_PPC32
  1556. regs->mq = 0;
  1557. regs->nip = start;
  1558. regs->msr = MSR_USER;
  1559. #else
  1560. if (!is_32bit_task()) {
  1561. unsigned long entry;
  1562. if (is_elf2_task()) {
  1563. /* Look ma, no function descriptors! */
  1564. entry = start;
  1565. /*
  1566. * Ulrich says:
  1567. * The latest iteration of the ABI requires that when
  1568. * calling a function (at its global entry point),
  1569. * the caller must ensure r12 holds the entry point
  1570. * address (so that the function can quickly
  1571. * establish addressability).
  1572. */
  1573. regs->gpr[12] = start;
  1574. /* Make sure that's restored on entry to userspace. */
  1575. set_thread_flag(TIF_RESTOREALL);
  1576. } else {
  1577. unsigned long toc;
  1578. /* start is a relocated pointer to the function
  1579. * descriptor for the elf _start routine. The first
  1580. * entry in the function descriptor is the entry
  1581. * address of _start and the second entry is the TOC
  1582. * value we need to use.
  1583. */
  1584. __get_user(entry, (unsigned long __user *)start);
  1585. __get_user(toc, (unsigned long __user *)start+1);
  1586. /* Check whether the e_entry function descriptor entries
  1587. * need to be relocated before we can use them.
  1588. */
  1589. if (load_addr != 0) {
  1590. entry += load_addr;
  1591. toc += load_addr;
  1592. }
  1593. regs->gpr[2] = toc;
  1594. }
  1595. regs->nip = entry;
  1596. regs->msr = MSR_USER64;
  1597. } else {
  1598. regs->nip = start;
  1599. regs->gpr[2] = 0;
  1600. regs->msr = MSR_USER32;
  1601. }
  1602. #endif
  1603. #ifdef CONFIG_VSX
  1604. current->thread.used_vsr = 0;
  1605. #endif
  1606. current->thread.load_fp = 0;
  1607. memset(&current->thread.fp_state, 0, sizeof(current->thread.fp_state));
  1608. current->thread.fp_save_area = NULL;
  1609. #ifdef CONFIG_ALTIVEC
  1610. memset(&current->thread.vr_state, 0, sizeof(current->thread.vr_state));
  1611. current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
  1612. current->thread.vr_save_area = NULL;
  1613. current->thread.vrsave = 0;
  1614. current->thread.used_vr = 0;
  1615. current->thread.load_vec = 0;
  1616. #endif /* CONFIG_ALTIVEC */
  1617. #ifdef CONFIG_SPE
  1618. memset(current->thread.evr, 0, sizeof(current->thread.evr));
  1619. current->thread.acc = 0;
  1620. current->thread.spefscr = 0;
  1621. current->thread.used_spe = 0;
  1622. #endif /* CONFIG_SPE */
  1623. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1624. current->thread.tm_tfhar = 0;
  1625. current->thread.tm_texasr = 0;
  1626. current->thread.tm_tfiar = 0;
  1627. current->thread.load_tm = 0;
  1628. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  1629. thread_pkey_regs_init(&current->thread);
  1630. }
  1631. EXPORT_SYMBOL(start_thread);
  1632. #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
  1633. | PR_FP_EXC_RES | PR_FP_EXC_INV)
  1634. int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
  1635. {
  1636. struct pt_regs *regs = tsk->thread.regs;
  1637. /* This is a bit hairy. If we are an SPE enabled processor
  1638. * (have embedded fp) we store the IEEE exception enable flags in
  1639. * fpexc_mode. fpexc_mode is also used for setting FP exception
  1640. * mode (asyn, precise, disabled) for 'Classic' FP. */
  1641. if (val & PR_FP_EXC_SW_ENABLE) {
  1642. #ifdef CONFIG_SPE
  1643. if (cpu_has_feature(CPU_FTR_SPE)) {
  1644. /*
  1645. * When the sticky exception bits are set
  1646. * directly by userspace, it must call prctl
  1647. * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
  1648. * in the existing prctl settings) or
  1649. * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
  1650. * the bits being set). <fenv.h> functions
  1651. * saving and restoring the whole
  1652. * floating-point environment need to do so
  1653. * anyway to restore the prctl settings from
  1654. * the saved environment.
  1655. */
  1656. tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
  1657. tsk->thread.fpexc_mode = val &
  1658. (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
  1659. return 0;
  1660. } else {
  1661. return -EINVAL;
  1662. }
  1663. #else
  1664. return -EINVAL;
  1665. #endif
  1666. }
  1667. /* on a CONFIG_SPE this does not hurt us. The bits that
  1668. * __pack_fe01 use do not overlap with bits used for
  1669. * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
  1670. * on CONFIG_SPE implementations are reserved so writing to
  1671. * them does not change anything */
  1672. if (val > PR_FP_EXC_PRECISE)
  1673. return -EINVAL;
  1674. tsk->thread.fpexc_mode = __pack_fe01(val);
  1675. if (regs != NULL && (regs->msr & MSR_FP) != 0)
  1676. regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
  1677. | tsk->thread.fpexc_mode;
  1678. return 0;
  1679. }
  1680. int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
  1681. {
  1682. unsigned int val;
  1683. if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
  1684. #ifdef CONFIG_SPE
  1685. if (cpu_has_feature(CPU_FTR_SPE)) {
  1686. /*
  1687. * When the sticky exception bits are set
  1688. * directly by userspace, it must call prctl
  1689. * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
  1690. * in the existing prctl settings) or
  1691. * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
  1692. * the bits being set). <fenv.h> functions
  1693. * saving and restoring the whole
  1694. * floating-point environment need to do so
  1695. * anyway to restore the prctl settings from
  1696. * the saved environment.
  1697. */
  1698. tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
  1699. val = tsk->thread.fpexc_mode;
  1700. } else
  1701. return -EINVAL;
  1702. #else
  1703. return -EINVAL;
  1704. #endif
  1705. else
  1706. val = __unpack_fe01(tsk->thread.fpexc_mode);
  1707. return put_user(val, (unsigned int __user *) adr);
  1708. }
  1709. int set_endian(struct task_struct *tsk, unsigned int val)
  1710. {
  1711. struct pt_regs *regs = tsk->thread.regs;
  1712. if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
  1713. (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
  1714. return -EINVAL;
  1715. if (regs == NULL)
  1716. return -EINVAL;
  1717. if (val == PR_ENDIAN_BIG)
  1718. regs->msr &= ~MSR_LE;
  1719. else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
  1720. regs->msr |= MSR_LE;
  1721. else
  1722. return -EINVAL;
  1723. return 0;
  1724. }
  1725. int get_endian(struct task_struct *tsk, unsigned long adr)
  1726. {
  1727. struct pt_regs *regs = tsk->thread.regs;
  1728. unsigned int val;
  1729. if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
  1730. !cpu_has_feature(CPU_FTR_REAL_LE))
  1731. return -EINVAL;
  1732. if (regs == NULL)
  1733. return -EINVAL;
  1734. if (regs->msr & MSR_LE) {
  1735. if (cpu_has_feature(CPU_FTR_REAL_LE))
  1736. val = PR_ENDIAN_LITTLE;
  1737. else
  1738. val = PR_ENDIAN_PPC_LITTLE;
  1739. } else
  1740. val = PR_ENDIAN_BIG;
  1741. return put_user(val, (unsigned int __user *)adr);
  1742. }
  1743. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  1744. {
  1745. tsk->thread.align_ctl = val;
  1746. return 0;
  1747. }
  1748. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  1749. {
  1750. return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  1751. }
  1752. static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
  1753. unsigned long nbytes)
  1754. {
  1755. unsigned long stack_page;
  1756. unsigned long cpu = task_cpu(p);
  1757. /*
  1758. * Avoid crashing if the stack has overflowed and corrupted
  1759. * task_cpu(p), which is in the thread_info struct.
  1760. */
  1761. if (cpu < NR_CPUS && cpu_possible(cpu)) {
  1762. stack_page = (unsigned long) hardirq_ctx[cpu];
  1763. if (sp >= stack_page + sizeof(struct thread_struct)
  1764. && sp <= stack_page + THREAD_SIZE - nbytes)
  1765. return 1;
  1766. stack_page = (unsigned long) softirq_ctx[cpu];
  1767. if (sp >= stack_page + sizeof(struct thread_struct)
  1768. && sp <= stack_page + THREAD_SIZE - nbytes)
  1769. return 1;
  1770. }
  1771. return 0;
  1772. }
  1773. int validate_sp(unsigned long sp, struct task_struct *p,
  1774. unsigned long nbytes)
  1775. {
  1776. unsigned long stack_page = (unsigned long)task_stack_page(p);
  1777. if (sp >= stack_page + sizeof(struct thread_struct)
  1778. && sp <= stack_page + THREAD_SIZE - nbytes)
  1779. return 1;
  1780. return valid_irq_stack(sp, p, nbytes);
  1781. }
  1782. EXPORT_SYMBOL(validate_sp);
  1783. unsigned long get_wchan(struct task_struct *p)
  1784. {
  1785. unsigned long ip, sp;
  1786. int count = 0;
  1787. if (!p || p == current || p->state == TASK_RUNNING)
  1788. return 0;
  1789. sp = p->thread.ksp;
  1790. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
  1791. return 0;
  1792. do {
  1793. sp = *(unsigned long *)sp;
  1794. if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD) ||
  1795. p->state == TASK_RUNNING)
  1796. return 0;
  1797. if (count > 0) {
  1798. ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
  1799. if (!in_sched_functions(ip))
  1800. return ip;
  1801. }
  1802. } while (count++ < 16);
  1803. return 0;
  1804. }
  1805. static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
  1806. void show_stack(struct task_struct *tsk, unsigned long *stack)
  1807. {
  1808. unsigned long sp, ip, lr, newsp;
  1809. int count = 0;
  1810. int firstframe = 1;
  1811. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1812. int curr_frame = current->curr_ret_stack;
  1813. extern void return_to_handler(void);
  1814. unsigned long rth = (unsigned long)return_to_handler;
  1815. #endif
  1816. sp = (unsigned long) stack;
  1817. if (tsk == NULL)
  1818. tsk = current;
  1819. if (sp == 0) {
  1820. if (tsk == current)
  1821. sp = current_stack_pointer();
  1822. else
  1823. sp = tsk->thread.ksp;
  1824. }
  1825. lr = 0;
  1826. printk("Call Trace:\n");
  1827. do {
  1828. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  1829. return;
  1830. stack = (unsigned long *) sp;
  1831. newsp = stack[0];
  1832. ip = stack[STACK_FRAME_LR_SAVE];
  1833. if (!firstframe || ip != lr) {
  1834. printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
  1835. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1836. if ((ip == rth) && curr_frame >= 0) {
  1837. pr_cont(" (%pS)",
  1838. (void *)current->ret_stack[curr_frame].ret);
  1839. curr_frame--;
  1840. }
  1841. #endif
  1842. if (firstframe)
  1843. pr_cont(" (unreliable)");
  1844. pr_cont("\n");
  1845. }
  1846. firstframe = 0;
  1847. /*
  1848. * See if this is an exception frame.
  1849. * We look for the "regshere" marker in the current frame.
  1850. */
  1851. if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
  1852. && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  1853. struct pt_regs *regs = (struct pt_regs *)
  1854. (sp + STACK_FRAME_OVERHEAD);
  1855. lr = regs->link;
  1856. printk("--- interrupt: %lx at %pS\n LR = %pS\n",
  1857. regs->trap, (void *)regs->nip, (void *)lr);
  1858. firstframe = 1;
  1859. }
  1860. sp = newsp;
  1861. } while (count++ < kstack_depth_to_print);
  1862. }
  1863. #ifdef CONFIG_PPC64
  1864. /* Called with hard IRQs off */
  1865. void notrace __ppc64_runlatch_on(void)
  1866. {
  1867. struct thread_info *ti = current_thread_info();
  1868. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  1869. /*
  1870. * Least significant bit (RUN) is the only writable bit of
  1871. * the CTRL register, so we can avoid mfspr. 2.06 is not the
  1872. * earliest ISA where this is the case, but it's convenient.
  1873. */
  1874. mtspr(SPRN_CTRLT, CTRL_RUNLATCH);
  1875. } else {
  1876. unsigned long ctrl;
  1877. /*
  1878. * Some architectures (e.g., Cell) have writable fields other
  1879. * than RUN, so do the read-modify-write.
  1880. */
  1881. ctrl = mfspr(SPRN_CTRLF);
  1882. ctrl |= CTRL_RUNLATCH;
  1883. mtspr(SPRN_CTRLT, ctrl);
  1884. }
  1885. ti->local_flags |= _TLF_RUNLATCH;
  1886. }
  1887. /* Called with hard IRQs off */
  1888. void notrace __ppc64_runlatch_off(void)
  1889. {
  1890. struct thread_info *ti = current_thread_info();
  1891. ti->local_flags &= ~_TLF_RUNLATCH;
  1892. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  1893. mtspr(SPRN_CTRLT, 0);
  1894. } else {
  1895. unsigned long ctrl;
  1896. ctrl = mfspr(SPRN_CTRLF);
  1897. ctrl &= ~CTRL_RUNLATCH;
  1898. mtspr(SPRN_CTRLT, ctrl);
  1899. }
  1900. }
  1901. #endif /* CONFIG_PPC64 */
  1902. unsigned long arch_align_stack(unsigned long sp)
  1903. {
  1904. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  1905. sp -= get_random_int() & ~PAGE_MASK;
  1906. return sp & ~0xf;
  1907. }
  1908. static inline unsigned long brk_rnd(void)
  1909. {
  1910. unsigned long rnd = 0;
  1911. /* 8MB for 32bit, 1GB for 64bit */
  1912. if (is_32bit_task())
  1913. rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT)));
  1914. else
  1915. rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT)));
  1916. return rnd << PAGE_SHIFT;
  1917. }
  1918. unsigned long arch_randomize_brk(struct mm_struct *mm)
  1919. {
  1920. unsigned long base = mm->brk;
  1921. unsigned long ret;
  1922. #ifdef CONFIG_PPC_BOOK3S_64
  1923. /*
  1924. * If we are using 1TB segments and we are allowed to randomise
  1925. * the heap, we can put it above 1TB so it is backed by a 1TB
  1926. * segment. Otherwise the heap will be in the bottom 1TB
  1927. * which always uses 256MB segments and this may result in a
  1928. * performance penalty. We don't need to worry about radix. For
  1929. * radix, mmu_highuser_ssize remains unchanged from 256MB.
  1930. */
  1931. if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
  1932. base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
  1933. #endif
  1934. ret = PAGE_ALIGN(base + brk_rnd());
  1935. if (ret < mm->brk)
  1936. return mm->brk;
  1937. return ret;
  1938. }