process.c 41 KB

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