signal_64.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. /* New 64 bit little-endian ABI allows redzone of 512 bytes below sp */
  61. char abigap[USER_REDZONE_SIZE];
  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. /* Remove TM bits from thread's MSR. The MSR in the sigcontext
  178. * just indicates to userland that we were doing a transaction, but we
  179. * don't want to return in transactional state. This also ensures
  180. * that flush_fp_to_thread won't set TIF_RESTORE_TM again.
  181. */
  182. regs->msr &= ~MSR_TS_MASK;
  183. flush_fp_to_thread(current);
  184. #ifdef CONFIG_ALTIVEC
  185. err |= __put_user(v_regs, &sc->v_regs);
  186. err |= __put_user(tm_v_regs, &tm_sc->v_regs);
  187. /* save altivec registers */
  188. if (current->thread.used_vr) {
  189. flush_altivec_to_thread(current);
  190. /* Copy 33 vec registers (vr0..31 and vscr) to the stack */
  191. err |= __copy_to_user(v_regs, &current->thread.vr_state,
  192. 33 * sizeof(vector128));
  193. /* If VEC was enabled there are transactional VRs valid too,
  194. * else they're a copy of the checkpointed VRs.
  195. */
  196. if (msr & MSR_VEC)
  197. err |= __copy_to_user(tm_v_regs,
  198. &current->thread.transact_vr,
  199. 33 * sizeof(vector128));
  200. else
  201. err |= __copy_to_user(tm_v_regs,
  202. &current->thread.vr_state,
  203. 33 * sizeof(vector128));
  204. /* set MSR_VEC in the MSR value in the frame to indicate
  205. * that sc->v_reg contains valid data.
  206. */
  207. msr |= MSR_VEC;
  208. }
  209. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  210. * use altivec.
  211. */
  212. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  213. current->thread.vrsave = mfspr(SPRN_VRSAVE);
  214. err |= __put_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
  215. if (msr & MSR_VEC)
  216. err |= __put_user(current->thread.transact_vrsave,
  217. (u32 __user *)&tm_v_regs[33]);
  218. else
  219. err |= __put_user(current->thread.vrsave,
  220. (u32 __user *)&tm_v_regs[33]);
  221. #else /* CONFIG_ALTIVEC */
  222. err |= __put_user(0, &sc->v_regs);
  223. err |= __put_user(0, &tm_sc->v_regs);
  224. #endif /* CONFIG_ALTIVEC */
  225. /* copy fpr regs and fpscr */
  226. err |= copy_fpr_to_user(&sc->fp_regs, current);
  227. if (msr & MSR_FP)
  228. err |= copy_transact_fpr_to_user(&tm_sc->fp_regs, current);
  229. else
  230. err |= copy_fpr_to_user(&tm_sc->fp_regs, current);
  231. #ifdef CONFIG_VSX
  232. /*
  233. * Copy VSX low doubleword to local buffer for formatting,
  234. * then out to userspace. Update v_regs to point after the
  235. * VMX data.
  236. */
  237. if (current->thread.used_vsr) {
  238. __giveup_vsx(current);
  239. v_regs += ELF_NVRREG;
  240. tm_v_regs += ELF_NVRREG;
  241. err |= copy_vsx_to_user(v_regs, current);
  242. if (msr & MSR_VSX)
  243. err |= copy_transact_vsx_to_user(tm_v_regs, current);
  244. else
  245. err |= copy_vsx_to_user(tm_v_regs, current);
  246. /* set MSR_VSX in the MSR value in the frame to
  247. * indicate that sc->vs_reg) contains valid data.
  248. */
  249. msr |= MSR_VSX;
  250. }
  251. #endif /* CONFIG_VSX */
  252. err |= __put_user(&sc->gp_regs, &sc->regs);
  253. err |= __put_user(&tm_sc->gp_regs, &tm_sc->regs);
  254. WARN_ON(!FULL_REGS(regs));
  255. err |= __copy_to_user(&tm_sc->gp_regs, regs, GP_REGS_SIZE);
  256. err |= __copy_to_user(&sc->gp_regs,
  257. &current->thread.ckpt_regs, GP_REGS_SIZE);
  258. err |= __put_user(msr, &tm_sc->gp_regs[PT_MSR]);
  259. err |= __put_user(msr, &sc->gp_regs[PT_MSR]);
  260. err |= __put_user(signr, &sc->signal);
  261. err |= __put_user(handler, &sc->handler);
  262. if (set != NULL)
  263. err |= __put_user(set->sig[0], &sc->oldmask);
  264. return err;
  265. }
  266. #endif
  267. /*
  268. * Restore the sigcontext from the signal frame.
  269. */
  270. static long restore_sigcontext(struct pt_regs *regs, sigset_t *set, int sig,
  271. struct sigcontext __user *sc)
  272. {
  273. #ifdef CONFIG_ALTIVEC
  274. elf_vrreg_t __user *v_regs;
  275. #endif
  276. unsigned long err = 0;
  277. unsigned long save_r13 = 0;
  278. unsigned long msr;
  279. #ifdef CONFIG_VSX
  280. int i;
  281. #endif
  282. /* If this is not a signal return, we preserve the TLS in r13 */
  283. if (!sig)
  284. save_r13 = regs->gpr[13];
  285. /* copy the GPRs */
  286. err |= __copy_from_user(regs->gpr, sc->gp_regs, sizeof(regs->gpr));
  287. err |= __get_user(regs->nip, &sc->gp_regs[PT_NIP]);
  288. /* get MSR separately, transfer the LE bit if doing signal return */
  289. err |= __get_user(msr, &sc->gp_regs[PT_MSR]);
  290. if (sig)
  291. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  292. err |= __get_user(regs->orig_gpr3, &sc->gp_regs[PT_ORIG_R3]);
  293. err |= __get_user(regs->ctr, &sc->gp_regs[PT_CTR]);
  294. err |= __get_user(regs->link, &sc->gp_regs[PT_LNK]);
  295. err |= __get_user(regs->xer, &sc->gp_regs[PT_XER]);
  296. err |= __get_user(regs->ccr, &sc->gp_regs[PT_CCR]);
  297. /* skip SOFTE */
  298. regs->trap = 0;
  299. err |= __get_user(regs->dar, &sc->gp_regs[PT_DAR]);
  300. err |= __get_user(regs->dsisr, &sc->gp_regs[PT_DSISR]);
  301. err |= __get_user(regs->result, &sc->gp_regs[PT_RESULT]);
  302. if (!sig)
  303. regs->gpr[13] = save_r13;
  304. if (set != NULL)
  305. err |= __get_user(set->sig[0], &sc->oldmask);
  306. /*
  307. * Do this before updating the thread state in
  308. * current->thread.fpr/vr. That way, if we get preempted
  309. * and another task grabs the FPU/Altivec, it won't be
  310. * tempted to save the current CPU state into the thread_struct
  311. * and corrupt what we are writing there.
  312. */
  313. discard_lazy_cpu_state();
  314. /*
  315. * Force reload of FP/VEC.
  316. * This has to be done before copying stuff into current->thread.fpr/vr
  317. * for the reasons explained in the previous comment.
  318. */
  319. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX);
  320. #ifdef CONFIG_ALTIVEC
  321. err |= __get_user(v_regs, &sc->v_regs);
  322. if (err)
  323. return err;
  324. if (v_regs && !access_ok(VERIFY_READ, v_regs, 34 * sizeof(vector128)))
  325. return -EFAULT;
  326. /* Copy 33 vec registers (vr0..31 and vscr) from the stack */
  327. if (v_regs != NULL && (msr & MSR_VEC) != 0)
  328. err |= __copy_from_user(&current->thread.vr_state, v_regs,
  329. 33 * sizeof(vector128));
  330. else if (current->thread.used_vr)
  331. memset(&current->thread.vr_state, 0, 33 * sizeof(vector128));
  332. /* Always get VRSAVE back */
  333. if (v_regs != NULL)
  334. err |= __get_user(current->thread.vrsave, (u32 __user *)&v_regs[33]);
  335. else
  336. current->thread.vrsave = 0;
  337. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  338. mtspr(SPRN_VRSAVE, current->thread.vrsave);
  339. #endif /* CONFIG_ALTIVEC */
  340. /* restore floating point */
  341. err |= copy_fpr_from_user(current, &sc->fp_regs);
  342. #ifdef CONFIG_VSX
  343. /*
  344. * Get additional VSX data. Update v_regs to point after the
  345. * VMX data. Copy VSX low doubleword from userspace to local
  346. * buffer for formatting, then into the taskstruct.
  347. */
  348. v_regs += ELF_NVRREG;
  349. if ((msr & MSR_VSX) != 0)
  350. err |= copy_vsx_from_user(current, v_regs);
  351. else
  352. for (i = 0; i < 32 ; i++)
  353. current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  354. #endif
  355. return err;
  356. }
  357. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  358. /*
  359. * Restore the two sigcontexts from the frame of a transactional processes.
  360. */
  361. static long restore_tm_sigcontexts(struct pt_regs *regs,
  362. struct sigcontext __user *sc,
  363. struct sigcontext __user *tm_sc)
  364. {
  365. #ifdef CONFIG_ALTIVEC
  366. elf_vrreg_t __user *v_regs, *tm_v_regs;
  367. #endif
  368. unsigned long err = 0;
  369. unsigned long msr;
  370. #ifdef CONFIG_VSX
  371. int i;
  372. #endif
  373. /* copy the GPRs */
  374. err |= __copy_from_user(regs->gpr, tm_sc->gp_regs, sizeof(regs->gpr));
  375. err |= __copy_from_user(&current->thread.ckpt_regs, sc->gp_regs,
  376. sizeof(regs->gpr));
  377. /*
  378. * TFHAR is restored from the checkpointed 'wound-back' ucontext's NIP.
  379. * TEXASR was set by the signal delivery reclaim, as was TFIAR.
  380. * Users doing anything abhorrent like thread-switching w/ signals for
  381. * TM-Suspended code will have to back TEXASR/TFIAR up themselves.
  382. * For the case of getting a signal and simply returning from it,
  383. * we don't need to re-copy them here.
  384. */
  385. err |= __get_user(regs->nip, &tm_sc->gp_regs[PT_NIP]);
  386. err |= __get_user(current->thread.tm_tfhar, &sc->gp_regs[PT_NIP]);
  387. /* get MSR separately, transfer the LE bit if doing signal return */
  388. err |= __get_user(msr, &sc->gp_regs[PT_MSR]);
  389. /* pull in MSR TM from user context */
  390. regs->msr = (regs->msr & ~MSR_TS_MASK) | (msr & MSR_TS_MASK);
  391. /* pull in MSR LE from user context */
  392. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  393. /* The following non-GPR non-FPR non-VR state is also checkpointed: */
  394. err |= __get_user(regs->ctr, &tm_sc->gp_regs[PT_CTR]);
  395. err |= __get_user(regs->link, &tm_sc->gp_regs[PT_LNK]);
  396. err |= __get_user(regs->xer, &tm_sc->gp_regs[PT_XER]);
  397. err |= __get_user(regs->ccr, &tm_sc->gp_regs[PT_CCR]);
  398. err |= __get_user(current->thread.ckpt_regs.ctr,
  399. &sc->gp_regs[PT_CTR]);
  400. err |= __get_user(current->thread.ckpt_regs.link,
  401. &sc->gp_regs[PT_LNK]);
  402. err |= __get_user(current->thread.ckpt_regs.xer,
  403. &sc->gp_regs[PT_XER]);
  404. err |= __get_user(current->thread.ckpt_regs.ccr,
  405. &sc->gp_regs[PT_CCR]);
  406. /* These regs are not checkpointed; they can go in 'regs'. */
  407. err |= __get_user(regs->trap, &sc->gp_regs[PT_TRAP]);
  408. err |= __get_user(regs->dar, &sc->gp_regs[PT_DAR]);
  409. err |= __get_user(regs->dsisr, &sc->gp_regs[PT_DSISR]);
  410. err |= __get_user(regs->result, &sc->gp_regs[PT_RESULT]);
  411. /*
  412. * Do this before updating the thread state in
  413. * current->thread.fpr/vr. That way, if we get preempted
  414. * and another task grabs the FPU/Altivec, it won't be
  415. * tempted to save the current CPU state into the thread_struct
  416. * and corrupt what we are writing there.
  417. */
  418. discard_lazy_cpu_state();
  419. /*
  420. * Force reload of FP/VEC.
  421. * This has to be done before copying stuff into current->thread.fpr/vr
  422. * for the reasons explained in the previous comment.
  423. */
  424. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX);
  425. #ifdef CONFIG_ALTIVEC
  426. err |= __get_user(v_regs, &sc->v_regs);
  427. err |= __get_user(tm_v_regs, &tm_sc->v_regs);
  428. if (err)
  429. return err;
  430. if (v_regs && !access_ok(VERIFY_READ, v_regs, 34 * sizeof(vector128)))
  431. return -EFAULT;
  432. if (tm_v_regs && !access_ok(VERIFY_READ,
  433. tm_v_regs, 34 * sizeof(vector128)))
  434. return -EFAULT;
  435. /* Copy 33 vec registers (vr0..31 and vscr) from the stack */
  436. if (v_regs != NULL && tm_v_regs != NULL && (msr & MSR_VEC) != 0) {
  437. err |= __copy_from_user(&current->thread.vr_state, v_regs,
  438. 33 * sizeof(vector128));
  439. err |= __copy_from_user(&current->thread.transact_vr, tm_v_regs,
  440. 33 * sizeof(vector128));
  441. }
  442. else if (current->thread.used_vr) {
  443. memset(&current->thread.vr_state, 0, 33 * sizeof(vector128));
  444. memset(&current->thread.transact_vr, 0, 33 * sizeof(vector128));
  445. }
  446. /* Always get VRSAVE back */
  447. if (v_regs != NULL && tm_v_regs != NULL) {
  448. err |= __get_user(current->thread.vrsave,
  449. (u32 __user *)&v_regs[33]);
  450. err |= __get_user(current->thread.transact_vrsave,
  451. (u32 __user *)&tm_v_regs[33]);
  452. }
  453. else {
  454. current->thread.vrsave = 0;
  455. current->thread.transact_vrsave = 0;
  456. }
  457. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  458. mtspr(SPRN_VRSAVE, current->thread.vrsave);
  459. #endif /* CONFIG_ALTIVEC */
  460. /* restore floating point */
  461. err |= copy_fpr_from_user(current, &sc->fp_regs);
  462. err |= copy_transact_fpr_from_user(current, &tm_sc->fp_regs);
  463. #ifdef CONFIG_VSX
  464. /*
  465. * Get additional VSX data. Update v_regs to point after the
  466. * VMX data. Copy VSX low doubleword from userspace to local
  467. * buffer for formatting, then into the taskstruct.
  468. */
  469. if (v_regs && ((msr & MSR_VSX) != 0)) {
  470. v_regs += ELF_NVRREG;
  471. tm_v_regs += ELF_NVRREG;
  472. err |= copy_vsx_from_user(current, v_regs);
  473. err |= copy_transact_vsx_from_user(current, tm_v_regs);
  474. } else {
  475. for (i = 0; i < 32 ; i++) {
  476. current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  477. current->thread.transact_fp.fpr[i][TS_VSRLOWOFFSET] = 0;
  478. }
  479. }
  480. #endif
  481. tm_enable();
  482. /* Make sure the transaction is marked as failed */
  483. current->thread.tm_texasr |= TEXASR_FS;
  484. /* This loads the checkpointed FP/VEC state, if used */
  485. tm_recheckpoint(&current->thread, msr);
  486. /* This loads the speculative FP/VEC state, if used */
  487. if (msr & MSR_FP) {
  488. do_load_up_transact_fpu(&current->thread);
  489. regs->msr |= (MSR_FP | current->thread.fpexc_mode);
  490. }
  491. #ifdef CONFIG_ALTIVEC
  492. if (msr & MSR_VEC) {
  493. do_load_up_transact_altivec(&current->thread);
  494. regs->msr |= MSR_VEC;
  495. }
  496. #endif
  497. return err;
  498. }
  499. #endif
  500. /*
  501. * Setup the trampoline code on the stack
  502. */
  503. static long setup_trampoline(unsigned int syscall, unsigned int __user *tramp)
  504. {
  505. int i;
  506. long err = 0;
  507. /* addi r1, r1, __SIGNAL_FRAMESIZE # Pop the dummy stackframe */
  508. err |= __put_user(0x38210000UL | (__SIGNAL_FRAMESIZE & 0xffff), &tramp[0]);
  509. /* li r0, __NR_[rt_]sigreturn| */
  510. err |= __put_user(0x38000000UL | (syscall & 0xffff), &tramp[1]);
  511. /* sc */
  512. err |= __put_user(0x44000002UL, &tramp[2]);
  513. /* Minimal traceback info */
  514. for (i=TRAMP_TRACEBACK; i < TRAMP_SIZE ;i++)
  515. err |= __put_user(0, &tramp[i]);
  516. if (!err)
  517. flush_icache_range((unsigned long) &tramp[0],
  518. (unsigned long) &tramp[TRAMP_SIZE]);
  519. return err;
  520. }
  521. /*
  522. * Userspace code may pass a ucontext which doesn't include VSX added
  523. * at the end. We need to check for this case.
  524. */
  525. #define UCONTEXTSIZEWITHOUTVSX \
  526. (sizeof(struct ucontext) - 32*sizeof(long))
  527. /*
  528. * Handle {get,set,swap}_context operations
  529. */
  530. int sys_swapcontext(struct ucontext __user *old_ctx,
  531. struct ucontext __user *new_ctx,
  532. long ctx_size, long r6, long r7, long r8, struct pt_regs *regs)
  533. {
  534. unsigned char tmp;
  535. sigset_t set;
  536. unsigned long new_msr = 0;
  537. int ctx_has_vsx_region = 0;
  538. if (new_ctx &&
  539. get_user(new_msr, &new_ctx->uc_mcontext.gp_regs[PT_MSR]))
  540. return -EFAULT;
  541. /*
  542. * Check that the context is not smaller than the original
  543. * size (with VMX but without VSX)
  544. */
  545. if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
  546. return -EINVAL;
  547. /*
  548. * If the new context state sets the MSR VSX bits but
  549. * it doesn't provide VSX state.
  550. */
  551. if ((ctx_size < sizeof(struct ucontext)) &&
  552. (new_msr & MSR_VSX))
  553. return -EINVAL;
  554. /* Does the context have enough room to store VSX data? */
  555. if (ctx_size >= sizeof(struct ucontext))
  556. ctx_has_vsx_region = 1;
  557. if (old_ctx != NULL) {
  558. if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
  559. || setup_sigcontext(&old_ctx->uc_mcontext, regs, 0, NULL, 0,
  560. ctx_has_vsx_region)
  561. || __copy_to_user(&old_ctx->uc_sigmask,
  562. &current->blocked, sizeof(sigset_t)))
  563. return -EFAULT;
  564. }
  565. if (new_ctx == NULL)
  566. return 0;
  567. if (!access_ok(VERIFY_READ, new_ctx, ctx_size)
  568. || __get_user(tmp, (u8 __user *) new_ctx)
  569. || __get_user(tmp, (u8 __user *) new_ctx + ctx_size - 1))
  570. return -EFAULT;
  571. /*
  572. * If we get a fault copying the context into the kernel's
  573. * image of the user's registers, we can't just return -EFAULT
  574. * because the user's registers will be corrupted. For instance
  575. * the NIP value may have been updated but not some of the
  576. * other registers. Given that we have done the access_ok
  577. * and successfully read the first and last bytes of the region
  578. * above, this should only happen in an out-of-memory situation
  579. * or if another thread unmaps the region containing the context.
  580. * We kill the task with a SIGSEGV in this situation.
  581. */
  582. if (__copy_from_user(&set, &new_ctx->uc_sigmask, sizeof(set)))
  583. do_exit(SIGSEGV);
  584. set_current_blocked(&set);
  585. if (restore_sigcontext(regs, NULL, 0, &new_ctx->uc_mcontext))
  586. do_exit(SIGSEGV);
  587. /* This returns like rt_sigreturn */
  588. set_thread_flag(TIF_RESTOREALL);
  589. return 0;
  590. }
  591. /*
  592. * Do a signal return; undo the signal stack.
  593. */
  594. int sys_rt_sigreturn(unsigned long r3, unsigned long r4, unsigned long r5,
  595. unsigned long r6, unsigned long r7, unsigned long r8,
  596. struct pt_regs *regs)
  597. {
  598. struct ucontext __user *uc = (struct ucontext __user *)regs->gpr[1];
  599. sigset_t set;
  600. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  601. unsigned long msr;
  602. #endif
  603. /* Always make any pending restarted system calls return -EINTR */
  604. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  605. if (!access_ok(VERIFY_READ, uc, sizeof(*uc)))
  606. goto badframe;
  607. if (__copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
  608. goto badframe;
  609. set_current_blocked(&set);
  610. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  611. if (__get_user(msr, &uc->uc_mcontext.gp_regs[PT_MSR]))
  612. goto badframe;
  613. if (MSR_TM_ACTIVE(msr)) {
  614. /* We recheckpoint on return. */
  615. struct ucontext __user *uc_transact;
  616. if (__get_user(uc_transact, &uc->uc_link))
  617. goto badframe;
  618. if (restore_tm_sigcontexts(regs, &uc->uc_mcontext,
  619. &uc_transact->uc_mcontext))
  620. goto badframe;
  621. }
  622. else
  623. /* Fall through, for non-TM restore */
  624. #endif
  625. if (restore_sigcontext(regs, NULL, 1, &uc->uc_mcontext))
  626. goto badframe;
  627. if (restore_altstack(&uc->uc_stack))
  628. goto badframe;
  629. set_thread_flag(TIF_RESTOREALL);
  630. return 0;
  631. badframe:
  632. #if DEBUG_SIG
  633. printk("badframe in sys_rt_sigreturn, regs=%p uc=%p &uc->uc_mcontext=%p\n",
  634. regs, uc, &uc->uc_mcontext);
  635. #endif
  636. if (show_unhandled_signals)
  637. printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
  638. current->comm, current->pid, "rt_sigreturn",
  639. (long)uc, regs->nip, regs->link);
  640. force_sig(SIGSEGV, current);
  641. return 0;
  642. }
  643. int handle_rt_signal64(int signr, struct k_sigaction *ka, siginfo_t *info,
  644. sigset_t *set, struct pt_regs *regs)
  645. {
  646. struct rt_sigframe __user *frame;
  647. unsigned long newsp = 0;
  648. long err = 0;
  649. frame = get_sigframe(ka, get_tm_stackpointer(regs), sizeof(*frame), 0);
  650. if (unlikely(frame == NULL))
  651. goto badframe;
  652. err |= __put_user(&frame->info, &frame->pinfo);
  653. err |= __put_user(&frame->uc, &frame->puc);
  654. err |= copy_siginfo_to_user(&frame->info, info);
  655. if (err)
  656. goto badframe;
  657. /* Create the ucontext. */
  658. err |= __put_user(0, &frame->uc.uc_flags);
  659. err |= __save_altstack(&frame->uc.uc_stack, regs->gpr[1]);
  660. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  661. if (MSR_TM_ACTIVE(regs->msr)) {
  662. /* The ucontext_t passed to userland points to the second
  663. * ucontext_t (for transactional state) with its uc_link ptr.
  664. */
  665. err |= __put_user(&frame->uc_transact, &frame->uc.uc_link);
  666. err |= setup_tm_sigcontexts(&frame->uc.uc_mcontext,
  667. &frame->uc_transact.uc_mcontext,
  668. regs, signr,
  669. NULL,
  670. (unsigned long)ka->sa.sa_handler);
  671. } else
  672. #endif
  673. {
  674. err |= __put_user(0, &frame->uc.uc_link);
  675. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, signr,
  676. NULL, (unsigned long)ka->sa.sa_handler,
  677. 1);
  678. }
  679. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  680. if (err)
  681. goto badframe;
  682. /* Make sure signal handler doesn't get spurious FP exceptions */
  683. current->thread.fp_state.fpscr = 0;
  684. /* Set up to return from userspace. */
  685. if (vdso64_rt_sigtramp && current->mm->context.vdso_base) {
  686. regs->link = current->mm->context.vdso_base + vdso64_rt_sigtramp;
  687. } else {
  688. err |= setup_trampoline(__NR_rt_sigreturn, &frame->tramp[0]);
  689. if (err)
  690. goto badframe;
  691. regs->link = (unsigned long) &frame->tramp[0];
  692. }
  693. /* Allocate a dummy caller frame for the signal handler. */
  694. newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
  695. err |= put_user(regs->gpr[1], (unsigned long __user *)newsp);
  696. /* Set up "regs" so we "return" to the signal handler. */
  697. if (is_elf2_task()) {
  698. regs->nip = (unsigned long) ka->sa.sa_handler;
  699. regs->gpr[12] = regs->nip;
  700. } else {
  701. /* Handler is *really* a pointer to the function descriptor for
  702. * the signal routine. The first entry in the function
  703. * descriptor is the entry address of signal and the second
  704. * entry is the TOC value we need to use.
  705. */
  706. func_descr_t __user *funct_desc_ptr =
  707. (func_descr_t __user *) ka->sa.sa_handler;
  708. err |= get_user(regs->nip, &funct_desc_ptr->entry);
  709. err |= get_user(regs->gpr[2], &funct_desc_ptr->toc);
  710. }
  711. /* enter the signal handler in native-endian mode */
  712. regs->msr &= ~MSR_LE;
  713. regs->msr |= (MSR_KERNEL & MSR_LE);
  714. regs->gpr[1] = newsp;
  715. regs->gpr[3] = signr;
  716. regs->result = 0;
  717. if (ka->sa.sa_flags & SA_SIGINFO) {
  718. err |= get_user(regs->gpr[4], (unsigned long __user *)&frame->pinfo);
  719. err |= get_user(regs->gpr[5], (unsigned long __user *)&frame->puc);
  720. regs->gpr[6] = (unsigned long) frame;
  721. } else {
  722. regs->gpr[4] = (unsigned long)&frame->uc.uc_mcontext;
  723. }
  724. if (err)
  725. goto badframe;
  726. return 1;
  727. badframe:
  728. #if DEBUG_SIG
  729. printk("badframe in setup_rt_frame, regs=%p frame=%p newsp=%lx\n",
  730. regs, frame, newsp);
  731. #endif
  732. if (show_unhandled_signals)
  733. printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
  734. current->comm, current->pid, "setup_rt_frame",
  735. (long)frame, regs->nip, regs->link);
  736. force_sigsegv(signr, current);
  737. return 0;
  738. }