signal_64.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Derived from "arch/i386/kernel/signal.c"
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/smp.h>
  17. #include <linux/kernel.h>
  18. #include <linux/signal.h>
  19. #include <linux/errno.h>
  20. #include <linux/wait.h>
  21. #include <linux/unistd.h>
  22. #include <linux/stddef.h>
  23. #include <linux/elf.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/ratelimit.h>
  26. #include <asm/sigcontext.h>
  27. #include <asm/ucontext.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/unistd.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/syscalls.h>
  33. #include <asm/vdso.h>
  34. #include <asm/switch_to.h>
  35. #include <asm/tm.h>
  36. #include "signal.h"
  37. #define DEBUG_SIG 0
  38. #define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
  39. #define FP_REGS_SIZE sizeof(elf_fpregset_t)
  40. #define TRAMP_TRACEBACK 3
  41. #define TRAMP_SIZE 6
  42. /*
  43. * When we have signals to deliver, we set up on the user stack,
  44. * going down from the original stack pointer:
  45. * 1) a rt_sigframe struct which contains the ucontext
  46. * 2) a gap of __SIGNAL_FRAMESIZE bytes which acts as a dummy caller
  47. * frame for the signal handler.
  48. */
  49. struct rt_sigframe {
  50. /* sys_rt_sigreturn requires the ucontext be the first field */
  51. struct ucontext uc;
  52. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  53. struct ucontext uc_transact;
  54. #endif
  55. unsigned long _unused[2];
  56. unsigned int tramp[TRAMP_SIZE];
  57. struct siginfo __user *pinfo;
  58. void __user *puc;
  59. struct siginfo info;
  60. /* 64 bit ABI allows for 288 bytes below sp before decrementing it. */
  61. char abigap[288];
  62. } __attribute__ ((aligned (16)));
  63. static const char fmt32[] = KERN_INFO \
  64. "%s[%d]: bad frame in %s: %08lx nip %08lx lr %08lx\n";
  65. static const char fmt64[] = KERN_INFO \
  66. "%s[%d]: bad frame in %s: %016lx nip %016lx lr %016lx\n";
  67. /*
  68. * Set up the sigcontext for the signal frame.
  69. */
  70. static long setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  71. int signr, sigset_t *set, unsigned long handler,
  72. int ctx_has_vsx_region)
  73. {
  74. /* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
  75. * process never used altivec yet (MSR_VEC is zero in pt_regs of
  76. * the context). This is very important because we must ensure we
  77. * don't lose the VRSAVE content that may have been set prior to
  78. * the process doing its first vector operation
  79. * Userland shall check AT_HWCAP to know whether it can rely on the
  80. * v_regs pointer or not
  81. */
  82. #ifdef CONFIG_ALTIVEC
  83. elf_vrreg_t __user *v_regs = (elf_vrreg_t __user *)(((unsigned long)sc->vmx_reserve + 15) & ~0xful);
  84. #endif
  85. unsigned long msr = regs->msr;
  86. long err = 0;
  87. #ifdef CONFIG_ALTIVEC
  88. err |= __put_user(v_regs, &sc->v_regs);
  89. /* save altivec registers */
  90. if (current->thread.used_vr) {
  91. flush_altivec_to_thread(current);
  92. /* Copy 33 vec registers (vr0..31 and vscr) to the stack */
  93. err |= __copy_to_user(v_regs, &current->thread.vr_state,
  94. 33 * sizeof(vector128));
  95. /* set MSR_VEC in the MSR value in the frame to indicate that sc->v_reg)
  96. * contains valid data.
  97. */
  98. msr |= MSR_VEC;
  99. }
  100. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  101. * use altivec.
  102. */
  103. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  104. current->thread.vrsave = mfspr(SPRN_VRSAVE);
  105. err |= __put_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
  106. #else /* CONFIG_ALTIVEC */
  107. err |= __put_user(0, &sc->v_regs);
  108. #endif /* CONFIG_ALTIVEC */
  109. flush_fp_to_thread(current);
  110. /* copy fpr regs and fpscr */
  111. err |= copy_fpr_to_user(&sc->fp_regs, current);
  112. /*
  113. * Clear the MSR VSX bit to indicate there is no valid state attached
  114. * to this context, except in the specific case below where we set it.
  115. */
  116. msr &= ~MSR_VSX;
  117. #ifdef CONFIG_VSX
  118. /*
  119. * Copy VSX low doubleword to local buffer for formatting,
  120. * then out to userspace. Update v_regs to point after the
  121. * VMX data.
  122. */
  123. if (current->thread.used_vsr && ctx_has_vsx_region) {
  124. __giveup_vsx(current);
  125. v_regs += ELF_NVRREG;
  126. err |= copy_vsx_to_user(v_regs, current);
  127. /* set MSR_VSX in the MSR value in the frame to
  128. * indicate that sc->vs_reg) contains valid data.
  129. */
  130. msr |= MSR_VSX;
  131. }
  132. #endif /* CONFIG_VSX */
  133. err |= __put_user(&sc->gp_regs, &sc->regs);
  134. WARN_ON(!FULL_REGS(regs));
  135. err |= __copy_to_user(&sc->gp_regs, regs, GP_REGS_SIZE);
  136. err |= __put_user(msr, &sc->gp_regs[PT_MSR]);
  137. err |= __put_user(signr, &sc->signal);
  138. err |= __put_user(handler, &sc->handler);
  139. if (set != NULL)
  140. err |= __put_user(set->sig[0], &sc->oldmask);
  141. return err;
  142. }
  143. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  144. /*
  145. * As above, but Transactional Memory is in use, so deliver sigcontexts
  146. * containing checkpointed and transactional register states.
  147. *
  148. * To do this, we treclaim (done before entering here) to gather both sets of
  149. * registers and set up the 'normal' sigcontext registers with rolled-back
  150. * register values such that a simple signal handler sees a correct
  151. * checkpointed register state. If interested, a TM-aware sighandler can
  152. * examine the transactional registers in the 2nd sigcontext to determine the
  153. * real origin of the signal.
  154. */
  155. static long setup_tm_sigcontexts(struct sigcontext __user *sc,
  156. struct sigcontext __user *tm_sc,
  157. struct pt_regs *regs,
  158. int signr, sigset_t *set, unsigned long handler)
  159. {
  160. /* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
  161. * process never used altivec yet (MSR_VEC is zero in pt_regs of
  162. * the context). This is very important because we must ensure we
  163. * don't lose the VRSAVE content that may have been set prior to
  164. * the process doing its first vector operation
  165. * Userland shall check AT_HWCAP to know wether it can rely on the
  166. * v_regs pointer or not.
  167. */
  168. #ifdef CONFIG_ALTIVEC
  169. elf_vrreg_t __user *v_regs = (elf_vrreg_t __user *)
  170. (((unsigned long)sc->vmx_reserve + 15) & ~0xful);
  171. elf_vrreg_t __user *tm_v_regs = (elf_vrreg_t __user *)
  172. (((unsigned long)tm_sc->vmx_reserve + 15) & ~0xful);
  173. #endif
  174. unsigned long msr = regs->msr;
  175. long err = 0;
  176. BUG_ON(!MSR_TM_ACTIVE(regs->msr));
  177. flush_fp_to_thread(current);
  178. #ifdef CONFIG_ALTIVEC
  179. err |= __put_user(v_regs, &sc->v_regs);
  180. err |= __put_user(tm_v_regs, &tm_sc->v_regs);
  181. /* save altivec registers */
  182. if (current->thread.used_vr) {
  183. flush_altivec_to_thread(current);
  184. /* Copy 33 vec registers (vr0..31 and vscr) to the stack */
  185. err |= __copy_to_user(v_regs, &current->thread.vr_state,
  186. 33 * sizeof(vector128));
  187. /* If VEC was enabled there are transactional VRs valid too,
  188. * else they're a copy of the checkpointed VRs.
  189. */
  190. if (msr & MSR_VEC)
  191. err |= __copy_to_user(tm_v_regs,
  192. &current->thread.transact_vr,
  193. 33 * sizeof(vector128));
  194. else
  195. err |= __copy_to_user(tm_v_regs,
  196. &current->thread.vr_state,
  197. 33 * sizeof(vector128));
  198. /* set MSR_VEC in the MSR value in the frame to indicate
  199. * that sc->v_reg contains valid data.
  200. */
  201. msr |= MSR_VEC;
  202. }
  203. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  204. * use altivec.
  205. */
  206. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  207. current->thread.vrsave = mfspr(SPRN_VRSAVE);
  208. err |= __put_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
  209. if (msr & MSR_VEC)
  210. err |= __put_user(current->thread.transact_vrsave,
  211. (u32 __user *)&tm_v_regs[33]);
  212. else
  213. err |= __put_user(current->thread.vrsave,
  214. (u32 __user *)&tm_v_regs[33]);
  215. #else /* CONFIG_ALTIVEC */
  216. err |= __put_user(0, &sc->v_regs);
  217. err |= __put_user(0, &tm_sc->v_regs);
  218. #endif /* CONFIG_ALTIVEC */
  219. /* copy fpr regs and fpscr */
  220. err |= copy_fpr_to_user(&sc->fp_regs, current);
  221. if (msr & MSR_FP)
  222. err |= copy_transact_fpr_to_user(&tm_sc->fp_regs, current);
  223. else
  224. err |= copy_fpr_to_user(&tm_sc->fp_regs, current);
  225. #ifdef CONFIG_VSX
  226. /*
  227. * Copy VSX low doubleword to local buffer for formatting,
  228. * then out to userspace. Update v_regs to point after the
  229. * VMX data.
  230. */
  231. if (current->thread.used_vsr) {
  232. __giveup_vsx(current);
  233. v_regs += ELF_NVRREG;
  234. tm_v_regs += ELF_NVRREG;
  235. err |= copy_vsx_to_user(v_regs, current);
  236. if (msr & MSR_VSX)
  237. err |= copy_transact_vsx_to_user(tm_v_regs, current);
  238. else
  239. err |= copy_vsx_to_user(tm_v_regs, current);
  240. /* set MSR_VSX in the MSR value in the frame to
  241. * indicate that sc->vs_reg) contains valid data.
  242. */
  243. msr |= MSR_VSX;
  244. }
  245. #endif /* CONFIG_VSX */
  246. err |= __put_user(&sc->gp_regs, &sc->regs);
  247. err |= __put_user(&tm_sc->gp_regs, &tm_sc->regs);
  248. WARN_ON(!FULL_REGS(regs));
  249. err |= __copy_to_user(&tm_sc->gp_regs, regs, GP_REGS_SIZE);
  250. err |= __copy_to_user(&sc->gp_regs,
  251. &current->thread.ckpt_regs, GP_REGS_SIZE);
  252. err |= __put_user(msr, &tm_sc->gp_regs[PT_MSR]);
  253. err |= __put_user(msr, &sc->gp_regs[PT_MSR]);
  254. err |= __put_user(signr, &sc->signal);
  255. err |= __put_user(handler, &sc->handler);
  256. if (set != NULL)
  257. err |= __put_user(set->sig[0], &sc->oldmask);
  258. return err;
  259. }
  260. #endif
  261. /*
  262. * Restore the sigcontext from the signal frame.
  263. */
  264. static long restore_sigcontext(struct pt_regs *regs, sigset_t *set, int sig,
  265. struct sigcontext __user *sc)
  266. {
  267. #ifdef CONFIG_ALTIVEC
  268. elf_vrreg_t __user *v_regs;
  269. #endif
  270. unsigned long err = 0;
  271. unsigned long save_r13 = 0;
  272. unsigned long msr;
  273. #ifdef CONFIG_VSX
  274. int i;
  275. #endif
  276. /* If this is not a signal return, we preserve the TLS in r13 */
  277. if (!sig)
  278. save_r13 = regs->gpr[13];
  279. /* copy the GPRs */
  280. err |= __copy_from_user(regs->gpr, sc->gp_regs, sizeof(regs->gpr));
  281. err |= __get_user(regs->nip, &sc->gp_regs[PT_NIP]);
  282. /* get MSR separately, transfer the LE bit if doing signal return */
  283. err |= __get_user(msr, &sc->gp_regs[PT_MSR]);
  284. if (sig)
  285. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  286. err |= __get_user(regs->orig_gpr3, &sc->gp_regs[PT_ORIG_R3]);
  287. err |= __get_user(regs->ctr, &sc->gp_regs[PT_CTR]);
  288. err |= __get_user(regs->link, &sc->gp_regs[PT_LNK]);
  289. err |= __get_user(regs->xer, &sc->gp_regs[PT_XER]);
  290. err |= __get_user(regs->ccr, &sc->gp_regs[PT_CCR]);
  291. /* skip SOFTE */
  292. regs->trap = 0;
  293. err |= __get_user(regs->dar, &sc->gp_regs[PT_DAR]);
  294. err |= __get_user(regs->dsisr, &sc->gp_regs[PT_DSISR]);
  295. err |= __get_user(regs->result, &sc->gp_regs[PT_RESULT]);
  296. if (!sig)
  297. regs->gpr[13] = save_r13;
  298. if (set != NULL)
  299. err |= __get_user(set->sig[0], &sc->oldmask);
  300. /*
  301. * Do this before updating the thread state in
  302. * current->thread.fpr/vr. That way, if we get preempted
  303. * and another task grabs the FPU/Altivec, it won't be
  304. * tempted to save the current CPU state into the thread_struct
  305. * and corrupt what we are writing there.
  306. */
  307. discard_lazy_cpu_state();
  308. /*
  309. * Force reload of FP/VEC.
  310. * This has to be done before copying stuff into current->thread.fpr/vr
  311. * for the reasons explained in the previous comment.
  312. */
  313. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX);
  314. #ifdef CONFIG_ALTIVEC
  315. err |= __get_user(v_regs, &sc->v_regs);
  316. if (err)
  317. return err;
  318. if (v_regs && !access_ok(VERIFY_READ, v_regs, 34 * sizeof(vector128)))
  319. return -EFAULT;
  320. /* Copy 33 vec registers (vr0..31 and vscr) from the stack */
  321. if (v_regs != NULL && (msr & MSR_VEC) != 0)
  322. err |= __copy_from_user(&current->thread.vr_state, v_regs,
  323. 33 * sizeof(vector128));
  324. else if (current->thread.used_vr)
  325. memset(&current->thread.vr_state, 0, 33 * sizeof(vector128));
  326. /* Always get VRSAVE back */
  327. if (v_regs != NULL)
  328. err |= __get_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
  329. else
  330. current->thread.vrsave = 0;
  331. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  332. mtspr(SPRN_VRSAVE, current->thread.vrsave);
  333. #endif /* CONFIG_ALTIVEC */
  334. /* restore floating point */
  335. err |= copy_fpr_from_user(current, &sc->fp_regs);
  336. #ifdef CONFIG_VSX
  337. /*
  338. * Get additional VSX data. Update v_regs to point after the
  339. * VMX data. Copy VSX low doubleword from userspace to local
  340. * buffer for formatting, then into the taskstruct.
  341. */
  342. v_regs += ELF_NVRREG;
  343. if ((msr & MSR_VSX) != 0)
  344. err |= copy_vsx_from_user(current, v_regs);
  345. else
  346. for (i = 0; i < 32 ; i++)
  347. current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  348. #endif
  349. return err;
  350. }
  351. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  352. /*
  353. * Restore the two sigcontexts from the frame of a transactional processes.
  354. */
  355. static long restore_tm_sigcontexts(struct pt_regs *regs,
  356. struct sigcontext __user *sc,
  357. struct sigcontext __user *tm_sc)
  358. {
  359. #ifdef CONFIG_ALTIVEC
  360. elf_vrreg_t __user *v_regs, *tm_v_regs;
  361. #endif
  362. unsigned long err = 0;
  363. unsigned long msr;
  364. #ifdef CONFIG_VSX
  365. int i;
  366. #endif
  367. /* copy the GPRs */
  368. err |= __copy_from_user(regs->gpr, tm_sc->gp_regs, sizeof(regs->gpr));
  369. err |= __copy_from_user(&current->thread.ckpt_regs, sc->gp_regs,
  370. sizeof(regs->gpr));
  371. /*
  372. * TFHAR is restored from the checkpointed 'wound-back' ucontext's NIP.
  373. * TEXASR was set by the signal delivery reclaim, as was TFIAR.
  374. * Users doing anything abhorrent like thread-switching w/ signals for
  375. * TM-Suspended code will have to back TEXASR/TFIAR up themselves.
  376. * For the case of getting a signal and simply returning from it,
  377. * we don't need to re-copy them here.
  378. */
  379. err |= __get_user(regs->nip, &tm_sc->gp_regs[PT_NIP]);
  380. err |= __get_user(current->thread.tm_tfhar, &sc->gp_regs[PT_NIP]);
  381. /* get MSR separately, transfer the LE bit if doing signal return */
  382. err |= __get_user(msr, &sc->gp_regs[PT_MSR]);
  383. /* pull in MSR TM from user context */
  384. regs->msr = (regs->msr & ~MSR_TS_MASK) | (msr & MSR_TS_MASK);
  385. /* pull in MSR LE from user context */
  386. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  387. /* The following non-GPR non-FPR non-VR state is also checkpointed: */
  388. err |= __get_user(regs->ctr, &tm_sc->gp_regs[PT_CTR]);
  389. err |= __get_user(regs->link, &tm_sc->gp_regs[PT_LNK]);
  390. err |= __get_user(regs->xer, &tm_sc->gp_regs[PT_XER]);
  391. err |= __get_user(regs->ccr, &tm_sc->gp_regs[PT_CCR]);
  392. err |= __get_user(current->thread.ckpt_regs.ctr,
  393. &sc->gp_regs[PT_CTR]);
  394. err |= __get_user(current->thread.ckpt_regs.link,
  395. &sc->gp_regs[PT_LNK]);
  396. err |= __get_user(current->thread.ckpt_regs.xer,
  397. &sc->gp_regs[PT_XER]);
  398. err |= __get_user(current->thread.ckpt_regs.ccr,
  399. &sc->gp_regs[PT_CCR]);
  400. /* These regs are not checkpointed; they can go in 'regs'. */
  401. err |= __get_user(regs->trap, &sc->gp_regs[PT_TRAP]);
  402. err |= __get_user(regs->dar, &sc->gp_regs[PT_DAR]);
  403. err |= __get_user(regs->dsisr, &sc->gp_regs[PT_DSISR]);
  404. err |= __get_user(regs->result, &sc->gp_regs[PT_RESULT]);
  405. /*
  406. * Do this before updating the thread state in
  407. * current->thread.fpr/vr. That way, if we get preempted
  408. * and another task grabs the FPU/Altivec, it won't be
  409. * tempted to save the current CPU state into the thread_struct
  410. * and corrupt what we are writing there.
  411. */
  412. discard_lazy_cpu_state();
  413. /*
  414. * Force reload of FP/VEC.
  415. * This has to be done before copying stuff into current->thread.fpr/vr
  416. * for the reasons explained in the previous comment.
  417. */
  418. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX);
  419. #ifdef CONFIG_ALTIVEC
  420. err |= __get_user(v_regs, &sc->v_regs);
  421. err |= __get_user(tm_v_regs, &tm_sc->v_regs);
  422. if (err)
  423. return err;
  424. if (v_regs && !access_ok(VERIFY_READ, v_regs, 34 * sizeof(vector128)))
  425. return -EFAULT;
  426. if (tm_v_regs && !access_ok(VERIFY_READ,
  427. tm_v_regs, 34 * sizeof(vector128)))
  428. return -EFAULT;
  429. /* Copy 33 vec registers (vr0..31 and vscr) from the stack */
  430. if (v_regs != NULL && tm_v_regs != NULL && (msr & MSR_VEC) != 0) {
  431. err |= __copy_from_user(&current->thread.vr_state, v_regs,
  432. 33 * sizeof(vector128));
  433. err |= __copy_from_user(&current->thread.transact_vr, tm_v_regs,
  434. 33 * sizeof(vector128));
  435. }
  436. else if (current->thread.used_vr) {
  437. memset(&current->thread.vr_state, 0, 33 * sizeof(vector128));
  438. memset(&current->thread.transact_vr, 0, 33 * sizeof(vector128));
  439. }
  440. /* Always get VRSAVE back */
  441. if (v_regs != NULL && tm_v_regs != NULL) {
  442. err |= __get_user(current->thread.vrsave,
  443. (u32 __user *)&v_regs[33]);
  444. err |= __get_user(current->thread.transact_vrsave,
  445. (u32 __user *)&tm_v_regs[33]);
  446. }
  447. else {
  448. current->thread.vrsave = 0;
  449. current->thread.transact_vrsave = 0;
  450. }
  451. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  452. mtspr(SPRN_VRSAVE, current->thread.vrsave);
  453. #endif /* CONFIG_ALTIVEC */
  454. /* restore floating point */
  455. err |= copy_fpr_from_user(current, &sc->fp_regs);
  456. err |= copy_transact_fpr_from_user(current, &tm_sc->fp_regs);
  457. #ifdef CONFIG_VSX
  458. /*
  459. * Get additional VSX data. Update v_regs to point after the
  460. * VMX data. Copy VSX low doubleword from userspace to local
  461. * buffer for formatting, then into the taskstruct.
  462. */
  463. if (v_regs && ((msr & MSR_VSX) != 0)) {
  464. v_regs += ELF_NVRREG;
  465. tm_v_regs += ELF_NVRREG;
  466. err |= copy_vsx_from_user(current, v_regs);
  467. err |= copy_transact_vsx_from_user(current, tm_v_regs);
  468. } else {
  469. for (i = 0; i < 32 ; i++) {
  470. current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  471. current->thread.transact_fp.fpr[i][TS_VSRLOWOFFSET] = 0;
  472. }
  473. }
  474. #endif
  475. tm_enable();
  476. /* This loads the checkpointed FP/VEC state, if used */
  477. tm_recheckpoint(&current->thread, msr);
  478. /* This loads the speculative FP/VEC state, if used */
  479. if (msr & MSR_FP) {
  480. do_load_up_transact_fpu(&current->thread);
  481. regs->msr |= (MSR_FP | current->thread.fpexc_mode);
  482. }
  483. #ifdef CONFIG_ALTIVEC
  484. if (msr & MSR_VEC) {
  485. do_load_up_transact_altivec(&current->thread);
  486. regs->msr |= MSR_VEC;
  487. }
  488. #endif
  489. return err;
  490. }
  491. #endif
  492. /*
  493. * Setup the trampoline code on the stack
  494. */
  495. static long setup_trampoline(unsigned int syscall, unsigned int __user *tramp)
  496. {
  497. int i;
  498. long err = 0;
  499. /* addi r1, r1, __SIGNAL_FRAMESIZE # Pop the dummy stackframe */
  500. err |= __put_user(0x38210000UL | (__SIGNAL_FRAMESIZE & 0xffff), &tramp[0]);
  501. /* li r0, __NR_[rt_]sigreturn| */
  502. err |= __put_user(0x38000000UL | (syscall & 0xffff), &tramp[1]);
  503. /* sc */
  504. err |= __put_user(0x44000002UL, &tramp[2]);
  505. /* Minimal traceback info */
  506. for (i=TRAMP_TRACEBACK; i < TRAMP_SIZE ;i++)
  507. err |= __put_user(0, &tramp[i]);
  508. if (!err)
  509. flush_icache_range((unsigned long) &tramp[0],
  510. (unsigned long) &tramp[TRAMP_SIZE]);
  511. return err;
  512. }
  513. /*
  514. * Userspace code may pass a ucontext which doesn't include VSX added
  515. * at the end. We need to check for this case.
  516. */
  517. #define UCONTEXTSIZEWITHOUTVSX \
  518. (sizeof(struct ucontext) - 32*sizeof(long))
  519. /*
  520. * Handle {get,set,swap}_context operations
  521. */
  522. int sys_swapcontext(struct ucontext __user *old_ctx,
  523. struct ucontext __user *new_ctx,
  524. long ctx_size, long r6, long r7, long r8, struct pt_regs *regs)
  525. {
  526. unsigned char tmp;
  527. sigset_t set;
  528. unsigned long new_msr = 0;
  529. int ctx_has_vsx_region = 0;
  530. if (new_ctx &&
  531. get_user(new_msr, &new_ctx->uc_mcontext.gp_regs[PT_MSR]))
  532. return -EFAULT;
  533. /*
  534. * Check that the context is not smaller than the original
  535. * size (with VMX but without VSX)
  536. */
  537. if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
  538. return -EINVAL;
  539. /*
  540. * If the new context state sets the MSR VSX bits but
  541. * it doesn't provide VSX state.
  542. */
  543. if ((ctx_size < sizeof(struct ucontext)) &&
  544. (new_msr & MSR_VSX))
  545. return -EINVAL;
  546. /* Does the context have enough room to store VSX data? */
  547. if (ctx_size >= sizeof(struct ucontext))
  548. ctx_has_vsx_region = 1;
  549. if (old_ctx != NULL) {
  550. if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
  551. || setup_sigcontext(&old_ctx->uc_mcontext, regs, 0, NULL, 0,
  552. ctx_has_vsx_region)
  553. || __copy_to_user(&old_ctx->uc_sigmask,
  554. &current->blocked, sizeof(sigset_t)))
  555. return -EFAULT;
  556. }
  557. if (new_ctx == NULL)
  558. return 0;
  559. if (!access_ok(VERIFY_READ, new_ctx, ctx_size)
  560. || __get_user(tmp, (u8 __user *) new_ctx)
  561. || __get_user(tmp, (u8 __user *) new_ctx + ctx_size - 1))
  562. return -EFAULT;
  563. /*
  564. * If we get a fault copying the context into the kernel's
  565. * image of the user's registers, we can't just return -EFAULT
  566. * because the user's registers will be corrupted. For instance
  567. * the NIP value may have been updated but not some of the
  568. * other registers. Given that we have done the access_ok
  569. * and successfully read the first and last bytes of the region
  570. * above, this should only happen in an out-of-memory situation
  571. * or if another thread unmaps the region containing the context.
  572. * We kill the task with a SIGSEGV in this situation.
  573. */
  574. if (__copy_from_user(&set, &new_ctx->uc_sigmask, sizeof(set)))
  575. do_exit(SIGSEGV);
  576. set_current_blocked(&set);
  577. if (restore_sigcontext(regs, NULL, 0, &new_ctx->uc_mcontext))
  578. do_exit(SIGSEGV);
  579. /* This returns like rt_sigreturn */
  580. set_thread_flag(TIF_RESTOREALL);
  581. return 0;
  582. }
  583. /*
  584. * Do a signal return; undo the signal stack.
  585. */
  586. int sys_rt_sigreturn(unsigned long r3, unsigned long r4, unsigned long r5,
  587. unsigned long r6, unsigned long r7, unsigned long r8,
  588. struct pt_regs *regs)
  589. {
  590. struct ucontext __user *uc = (struct ucontext __user *)regs->gpr[1];
  591. sigset_t set;
  592. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  593. unsigned long msr;
  594. #endif
  595. /* Always make any pending restarted system calls return -EINTR */
  596. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  597. if (!access_ok(VERIFY_READ, uc, sizeof(*uc)))
  598. goto badframe;
  599. if (__copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
  600. goto badframe;
  601. set_current_blocked(&set);
  602. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  603. if (__get_user(msr, &uc->uc_mcontext.gp_regs[PT_MSR]))
  604. goto badframe;
  605. if (MSR_TM_ACTIVE(msr)) {
  606. /* We recheckpoint on return. */
  607. struct ucontext __user *uc_transact;
  608. if (__get_user(uc_transact, &uc->uc_link))
  609. goto badframe;
  610. if (restore_tm_sigcontexts(regs, &uc->uc_mcontext,
  611. &uc_transact->uc_mcontext))
  612. goto badframe;
  613. }
  614. else
  615. /* Fall through, for non-TM restore */
  616. #endif
  617. if (restore_sigcontext(regs, NULL, 1, &uc->uc_mcontext))
  618. goto badframe;
  619. if (restore_altstack(&uc->uc_stack))
  620. goto badframe;
  621. set_thread_flag(TIF_RESTOREALL);
  622. return 0;
  623. badframe:
  624. #if DEBUG_SIG
  625. printk("badframe in sys_rt_sigreturn, regs=%p uc=%p &uc->uc_mcontext=%p\n",
  626. regs, uc, &uc->uc_mcontext);
  627. #endif
  628. if (show_unhandled_signals)
  629. printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
  630. current->comm, current->pid, "rt_sigreturn",
  631. (long)uc, regs->nip, regs->link);
  632. force_sig(SIGSEGV, current);
  633. return 0;
  634. }
  635. int handle_rt_signal64(int signr, struct k_sigaction *ka, siginfo_t *info,
  636. sigset_t *set, struct pt_regs *regs)
  637. {
  638. struct rt_sigframe __user *frame;
  639. unsigned long newsp = 0;
  640. long err = 0;
  641. frame = get_sigframe(ka, get_tm_stackpointer(regs), sizeof(*frame), 0);
  642. if (unlikely(frame == NULL))
  643. goto badframe;
  644. err |= __put_user(&frame->info, &frame->pinfo);
  645. err |= __put_user(&frame->uc, &frame->puc);
  646. err |= copy_siginfo_to_user(&frame->info, info);
  647. if (err)
  648. goto badframe;
  649. /* Create the ucontext. */
  650. err |= __put_user(0, &frame->uc.uc_flags);
  651. err |= __save_altstack(&frame->uc.uc_stack, regs->gpr[1]);
  652. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  653. if (MSR_TM_ACTIVE(regs->msr)) {
  654. /* The ucontext_t passed to userland points to the second
  655. * ucontext_t (for transactional state) with its uc_link ptr.
  656. */
  657. err |= __put_user(&frame->uc_transact, &frame->uc.uc_link);
  658. err |= setup_tm_sigcontexts(&frame->uc.uc_mcontext,
  659. &frame->uc_transact.uc_mcontext,
  660. regs, signr,
  661. NULL,
  662. (unsigned long)ka->sa.sa_handler);
  663. } else
  664. #endif
  665. {
  666. err |= __put_user(0, &frame->uc.uc_link);
  667. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, signr,
  668. NULL, (unsigned long)ka->sa.sa_handler,
  669. 1);
  670. }
  671. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  672. if (err)
  673. goto badframe;
  674. /* Make sure signal handler doesn't get spurious FP exceptions */
  675. current->thread.fp_state.fpscr = 0;
  676. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  677. /* Remove TM bits from thread's MSR. The MSR in the sigcontext
  678. * just indicates to userland that we were doing a transaction, but we
  679. * don't want to return in transactional state:
  680. */
  681. regs->msr &= ~MSR_TS_MASK;
  682. #endif
  683. /* Set up to return from userspace. */
  684. if (vdso64_rt_sigtramp && current->mm->context.vdso_base) {
  685. regs->link = current->mm->context.vdso_base + vdso64_rt_sigtramp;
  686. } else {
  687. err |= setup_trampoline(__NR_rt_sigreturn, &frame->tramp[0]);
  688. if (err)
  689. goto badframe;
  690. regs->link = (unsigned long) &frame->tramp[0];
  691. }
  692. /* Allocate a dummy caller frame for the signal handler. */
  693. newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
  694. err |= put_user(regs->gpr[1], (unsigned long __user *)newsp);
  695. /* Set up "regs" so we "return" to the signal handler. */
  696. if (is_elf2_task()) {
  697. regs->nip = (unsigned long) ka->sa.sa_handler;
  698. regs->gpr[12] = regs->nip;
  699. } else {
  700. /* Handler is *really* a pointer to the function descriptor for
  701. * the signal routine. The first entry in the function
  702. * descriptor is the entry address of signal and the second
  703. * entry is the TOC value we need to use.
  704. */
  705. func_descr_t __user *funct_desc_ptr =
  706. (func_descr_t __user *) ka->sa.sa_handler;
  707. err |= get_user(regs->nip, &funct_desc_ptr->entry);
  708. err |= get_user(regs->gpr[2], &funct_desc_ptr->toc);
  709. }
  710. /* enter the signal handler in native-endian mode */
  711. regs->msr &= ~MSR_LE;
  712. regs->msr |= (MSR_KERNEL & MSR_LE);
  713. regs->gpr[1] = newsp;
  714. regs->gpr[3] = signr;
  715. regs->result = 0;
  716. if (ka->sa.sa_flags & SA_SIGINFO) {
  717. err |= get_user(regs->gpr[4], (unsigned long __user *)&frame->pinfo);
  718. err |= get_user(regs->gpr[5], (unsigned long __user *)&frame->puc);
  719. regs->gpr[6] = (unsigned long) frame;
  720. } else {
  721. regs->gpr[4] = (unsigned long)&frame->uc.uc_mcontext;
  722. }
  723. if (err)
  724. goto badframe;
  725. return 1;
  726. badframe:
  727. #if DEBUG_SIG
  728. printk("badframe in setup_rt_frame, regs=%p frame=%p newsp=%lx\n",
  729. regs, frame, newsp);
  730. #endif
  731. if (show_unhandled_signals)
  732. printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
  733. current->comm, current->pid, "setup_rt_frame",
  734. (long)frame, regs->nip, regs->link);
  735. force_sigsegv(signr, current);
  736. return 0;
  737. }