process.c 44 KB

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