signal_64.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerPC version
  4. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  5. *
  6. * Derived from "arch/i386/kernel/signal.c"
  7. * Copyright (C) 1991, 1992 Linus Torvalds
  8. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  9. */
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/kernel.h>
  14. #include <linux/signal.h>
  15. #include <linux/errno.h>
  16. #include <linux/wait.h>
  17. #include <linux/unistd.h>
  18. #include <linux/stddef.h>
  19. #include <linux/elf.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/ratelimit.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/pagemap.h>
  24. #include <asm/sigcontext.h>
  25. #include <asm/ucontext.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/unistd.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/syscalls.h>
  30. #include <asm/vdso.h>
  31. #include <asm/switch_to.h>
  32. #include <asm/tm.h>
  33. #include <asm/asm-prototypes.h>
  34. #include "signal.h"
  35. #define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
  36. #define FP_REGS_SIZE sizeof(elf_fpregset_t)
  37. #define TRAMP_TRACEBACK 4
  38. #define TRAMP_SIZE 7
  39. /*
  40. * When we have signals to deliver, we set up on the user stack,
  41. * going down from the original stack pointer:
  42. * 1) a rt_sigframe struct which contains the ucontext
  43. * 2) a gap of __SIGNAL_FRAMESIZE bytes which acts as a dummy caller
  44. * frame for the signal handler.
  45. */
  46. struct rt_sigframe {
  47. /* sys_rt_sigreturn requires the ucontext be the first field */
  48. struct ucontext uc;
  49. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  50. struct ucontext uc_transact;
  51. #endif
  52. unsigned long _unused[2];
  53. unsigned int tramp[TRAMP_SIZE];
  54. struct siginfo __user *pinfo;
  55. void __user *puc;
  56. struct siginfo info;
  57. /* New 64 bit little-endian ABI allows redzone of 512 bytes below sp */
  58. char abigap[USER_REDZONE_SIZE];
  59. } __attribute__ ((aligned (16)));
  60. unsigned long get_min_sigframe_size_64(void)
  61. {
  62. return sizeof(struct rt_sigframe) + __SIGNAL_FRAMESIZE;
  63. }
  64. /*
  65. * This computes a quad word aligned pointer inside the vmx_reserve array
  66. * element. For historical reasons sigcontext might not be quad word aligned,
  67. * but the location we write the VMX regs to must be. See the comment in
  68. * sigcontext for more detail.
  69. */
  70. #ifdef CONFIG_ALTIVEC
  71. static elf_vrreg_t __user *sigcontext_vmx_regs(struct sigcontext __user *sc)
  72. {
  73. return (elf_vrreg_t __user *) (((unsigned long)sc->vmx_reserve + 15) & ~0xful);
  74. }
  75. #endif
  76. static void prepare_setup_sigcontext(struct task_struct *tsk)
  77. {
  78. #ifdef CONFIG_ALTIVEC
  79. /* save altivec registers */
  80. if (tsk->thread.used_vr)
  81. flush_altivec_to_thread(tsk);
  82. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  83. tsk->thread.vrsave = mfspr(SPRN_VRSAVE);
  84. #endif /* CONFIG_ALTIVEC */
  85. flush_fp_to_thread(tsk);
  86. #ifdef CONFIG_VSX
  87. if (tsk->thread.used_vsr)
  88. flush_vsx_to_thread(tsk);
  89. #endif /* CONFIG_VSX */
  90. }
  91. /*
  92. * Set up the sigcontext for the signal frame.
  93. */
  94. #define unsafe_setup_sigcontext(sc, tsk, signr, set, handler, ctx_has_vsx_region, label)\
  95. do { \
  96. if (__unsafe_setup_sigcontext(sc, tsk, signr, set, handler, ctx_has_vsx_region))\
  97. goto label; \
  98. } while (0)
  99. static long notrace __unsafe_setup_sigcontext(struct sigcontext __user *sc,
  100. struct task_struct *tsk, int signr, sigset_t *set,
  101. unsigned long handler, int ctx_has_vsx_region)
  102. {
  103. /* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
  104. * process never used altivec yet (MSR_VEC is zero in pt_regs of
  105. * the context). This is very important because we must ensure we
  106. * don't lose the VRSAVE content that may have been set prior to
  107. * the process doing its first vector operation
  108. * Userland shall check AT_HWCAP to know whether it can rely on the
  109. * v_regs pointer or not
  110. */
  111. #ifdef CONFIG_ALTIVEC
  112. elf_vrreg_t __user *v_regs = sigcontext_vmx_regs(sc);
  113. #endif
  114. struct pt_regs *regs = tsk->thread.regs;
  115. unsigned long msr = regs->msr;
  116. /* Force usr to always see softe as 1 (interrupts enabled) */
  117. unsigned long softe = 0x1;
  118. BUG_ON(tsk != current);
  119. #ifdef CONFIG_ALTIVEC
  120. unsafe_put_user(v_regs, &sc->v_regs, efault_out);
  121. /* save altivec registers */
  122. if (tsk->thread.used_vr) {
  123. /* Copy 33 vec registers (vr0..31 and vscr) to the stack */
  124. unsafe_copy_to_user(v_regs, &tsk->thread.vr_state,
  125. 33 * sizeof(vector128), efault_out);
  126. /* set MSR_VEC in the MSR value in the frame to indicate that sc->v_reg)
  127. * contains valid data.
  128. */
  129. msr |= MSR_VEC;
  130. }
  131. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  132. * use altivec.
  133. */
  134. unsafe_put_user(tsk->thread.vrsave, (u32 __user *)&v_regs[33], efault_out);
  135. #else /* CONFIG_ALTIVEC */
  136. unsafe_put_user(0, &sc->v_regs, efault_out);
  137. #endif /* CONFIG_ALTIVEC */
  138. /* copy fpr regs and fpscr */
  139. unsafe_copy_fpr_to_user(&sc->fp_regs, tsk, efault_out);
  140. /*
  141. * Clear the MSR VSX bit to indicate there is no valid state attached
  142. * to this context, except in the specific case below where we set it.
  143. */
  144. msr &= ~MSR_VSX;
  145. #ifdef CONFIG_VSX
  146. /*
  147. * Copy VSX low doubleword to local buffer for formatting,
  148. * then out to userspace. Update v_regs to point after the
  149. * VMX data.
  150. */
  151. if (tsk->thread.used_vsr && ctx_has_vsx_region) {
  152. v_regs += ELF_NVRREG;
  153. unsafe_copy_vsx_to_user(v_regs, tsk, efault_out);
  154. /* set MSR_VSX in the MSR value in the frame to
  155. * indicate that sc->vs_reg) contains valid data.
  156. */
  157. msr |= MSR_VSX;
  158. }
  159. #endif /* CONFIG_VSX */
  160. unsafe_put_user(&sc->gp_regs, &sc->regs, efault_out);
  161. unsafe_copy_to_user(&sc->gp_regs, regs, GP_REGS_SIZE, efault_out);
  162. unsafe_put_user(msr, &sc->gp_regs[PT_MSR], efault_out);
  163. unsafe_put_user(softe, &sc->gp_regs[PT_SOFTE], efault_out);
  164. unsafe_put_user(signr, &sc->signal, efault_out);
  165. unsafe_put_user(handler, &sc->handler, efault_out);
  166. if (set != NULL)
  167. unsafe_put_user(set->sig[0], &sc->oldmask, efault_out);
  168. return 0;
  169. efault_out:
  170. return -EFAULT;
  171. }
  172. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  173. /*
  174. * As above, but Transactional Memory is in use, so deliver sigcontexts
  175. * containing checkpointed and transactional register states.
  176. *
  177. * To do this, we treclaim (done before entering here) to gather both sets of
  178. * registers and set up the 'normal' sigcontext registers with rolled-back
  179. * register values such that a simple signal handler sees a correct
  180. * checkpointed register state. If interested, a TM-aware sighandler can
  181. * examine the transactional registers in the 2nd sigcontext to determine the
  182. * real origin of the signal.
  183. */
  184. static long setup_tm_sigcontexts(struct sigcontext __user *sc,
  185. struct sigcontext __user *tm_sc,
  186. struct task_struct *tsk,
  187. int signr, sigset_t *set, unsigned long handler,
  188. unsigned long msr)
  189. {
  190. /* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
  191. * process never used altivec yet (MSR_VEC is zero in pt_regs of
  192. * the context). This is very important because we must ensure we
  193. * don't lose the VRSAVE content that may have been set prior to
  194. * the process doing its first vector operation
  195. * Userland shall check AT_HWCAP to know wether it can rely on the
  196. * v_regs pointer or not.
  197. */
  198. #ifdef CONFIG_ALTIVEC
  199. elf_vrreg_t __user *v_regs = sigcontext_vmx_regs(sc);
  200. elf_vrreg_t __user *tm_v_regs = sigcontext_vmx_regs(tm_sc);
  201. #endif
  202. struct pt_regs *regs = tsk->thread.regs;
  203. long err = 0;
  204. BUG_ON(tsk != current);
  205. BUG_ON(!MSR_TM_ACTIVE(msr));
  206. WARN_ON(tm_suspend_disabled);
  207. /* Restore checkpointed FP, VEC, and VSX bits from ckpt_regs as
  208. * it contains the correct FP, VEC, VSX state after we treclaimed
  209. * the transaction and giveup_all() was called on reclaiming.
  210. */
  211. msr |= tsk->thread.ckpt_regs.msr & (MSR_FP | MSR_VEC | MSR_VSX);
  212. #ifdef CONFIG_ALTIVEC
  213. err |= __put_user(v_regs, &sc->v_regs);
  214. err |= __put_user(tm_v_regs, &tm_sc->v_regs);
  215. /* save altivec registers */
  216. if (tsk->thread.used_vr) {
  217. /* Copy 33 vec registers (vr0..31 and vscr) to the stack */
  218. err |= __copy_to_user(v_regs, &tsk->thread.ckvr_state,
  219. 33 * sizeof(vector128));
  220. /* If VEC was enabled there are transactional VRs valid too,
  221. * else they're a copy of the checkpointed VRs.
  222. */
  223. if (msr & MSR_VEC)
  224. err |= __copy_to_user(tm_v_regs,
  225. &tsk->thread.vr_state,
  226. 33 * sizeof(vector128));
  227. else
  228. err |= __copy_to_user(tm_v_regs,
  229. &tsk->thread.ckvr_state,
  230. 33 * sizeof(vector128));
  231. /* set MSR_VEC in the MSR value in the frame to indicate
  232. * that sc->v_reg contains valid data.
  233. */
  234. msr |= MSR_VEC;
  235. }
  236. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  237. * use altivec.
  238. */
  239. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  240. tsk->thread.ckvrsave = mfspr(SPRN_VRSAVE);
  241. err |= __put_user(tsk->thread.ckvrsave, (u32 __user *)&v_regs[33]);
  242. if (msr & MSR_VEC)
  243. err |= __put_user(tsk->thread.vrsave,
  244. (u32 __user *)&tm_v_regs[33]);
  245. else
  246. err |= __put_user(tsk->thread.ckvrsave,
  247. (u32 __user *)&tm_v_regs[33]);
  248. #else /* CONFIG_ALTIVEC */
  249. err |= __put_user(0, &sc->v_regs);
  250. err |= __put_user(0, &tm_sc->v_regs);
  251. #endif /* CONFIG_ALTIVEC */
  252. /* copy fpr regs and fpscr */
  253. err |= copy_ckfpr_to_user(&sc->fp_regs, tsk);
  254. if (msr & MSR_FP)
  255. err |= copy_fpr_to_user(&tm_sc->fp_regs, tsk);
  256. else
  257. err |= copy_ckfpr_to_user(&tm_sc->fp_regs, tsk);
  258. #ifdef CONFIG_VSX
  259. /*
  260. * Copy VSX low doubleword to local buffer for formatting,
  261. * then out to userspace. Update v_regs to point after the
  262. * VMX data.
  263. */
  264. if (tsk->thread.used_vsr) {
  265. v_regs += ELF_NVRREG;
  266. tm_v_regs += ELF_NVRREG;
  267. err |= copy_ckvsx_to_user(v_regs, tsk);
  268. if (msr & MSR_VSX)
  269. err |= copy_vsx_to_user(tm_v_regs, tsk);
  270. else
  271. err |= copy_ckvsx_to_user(tm_v_regs, tsk);
  272. /* set MSR_VSX in the MSR value in the frame to
  273. * indicate that sc->vs_reg) contains valid data.
  274. */
  275. msr |= MSR_VSX;
  276. }
  277. #endif /* CONFIG_VSX */
  278. err |= __put_user(&sc->gp_regs, &sc->regs);
  279. err |= __put_user(&tm_sc->gp_regs, &tm_sc->regs);
  280. err |= __copy_to_user(&tm_sc->gp_regs, regs, GP_REGS_SIZE);
  281. err |= __copy_to_user(&sc->gp_regs,
  282. &tsk->thread.ckpt_regs, GP_REGS_SIZE);
  283. err |= __put_user(msr, &tm_sc->gp_regs[PT_MSR]);
  284. err |= __put_user(msr, &sc->gp_regs[PT_MSR]);
  285. err |= __put_user(signr, &sc->signal);
  286. err |= __put_user(handler, &sc->handler);
  287. if (set != NULL)
  288. err |= __put_user(set->sig[0], &sc->oldmask);
  289. return err;
  290. }
  291. #endif
  292. /*
  293. * Restore the sigcontext from the signal frame.
  294. */
  295. #define unsafe_restore_sigcontext(tsk, set, sig, sc, label) do { \
  296. if (__unsafe_restore_sigcontext(tsk, set, sig, sc)) \
  297. goto label; \
  298. } while (0)
  299. static long notrace __unsafe_restore_sigcontext(struct task_struct *tsk, sigset_t *set,
  300. int sig, struct sigcontext __user *sc)
  301. {
  302. #ifdef CONFIG_ALTIVEC
  303. elf_vrreg_t __user *v_regs;
  304. #endif
  305. unsigned long save_r13 = 0;
  306. unsigned long msr;
  307. struct pt_regs *regs = tsk->thread.regs;
  308. #ifdef CONFIG_VSX
  309. int i;
  310. #endif
  311. BUG_ON(tsk != current);
  312. /* If this is not a signal return, we preserve the TLS in r13 */
  313. if (!sig)
  314. save_r13 = regs->gpr[13];
  315. /* copy the GPRs */
  316. unsafe_copy_from_user(regs->gpr, sc->gp_regs, sizeof(regs->gpr), efault_out);
  317. unsafe_get_user(regs->nip, &sc->gp_regs[PT_NIP], efault_out);
  318. /* get MSR separately, transfer the LE bit if doing signal return */
  319. unsafe_get_user(msr, &sc->gp_regs[PT_MSR], efault_out);
  320. if (sig)
  321. regs_set_return_msr(regs, (regs->msr & ~MSR_LE) | (msr & MSR_LE));
  322. unsafe_get_user(regs->orig_gpr3, &sc->gp_regs[PT_ORIG_R3], efault_out);
  323. unsafe_get_user(regs->ctr, &sc->gp_regs[PT_CTR], efault_out);
  324. unsafe_get_user(regs->link, &sc->gp_regs[PT_LNK], efault_out);
  325. unsafe_get_user(regs->xer, &sc->gp_regs[PT_XER], efault_out);
  326. unsafe_get_user(regs->ccr, &sc->gp_regs[PT_CCR], efault_out);
  327. /* Don't allow userspace to set SOFTE */
  328. set_trap_norestart(regs);
  329. unsafe_get_user(regs->dar, &sc->gp_regs[PT_DAR], efault_out);
  330. unsafe_get_user(regs->dsisr, &sc->gp_regs[PT_DSISR], efault_out);
  331. unsafe_get_user(regs->result, &sc->gp_regs[PT_RESULT], efault_out);
  332. if (!sig)
  333. regs->gpr[13] = save_r13;
  334. if (set != NULL)
  335. unsafe_get_user(set->sig[0], &sc->oldmask, efault_out);
  336. /*
  337. * Force reload of FP/VEC/VSX so userspace sees any changes.
  338. * Clear these bits from the user process' MSR before copying into the
  339. * thread struct. If we are rescheduled or preempted and another task
  340. * uses FP/VEC/VSX, and this process has the MSR bits set, then the
  341. * context switch code will save the current CPU state into the
  342. * thread_struct - possibly overwriting the data we are updating here.
  343. */
  344. regs_set_return_msr(regs, regs->msr & ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX));
  345. #ifdef CONFIG_ALTIVEC
  346. unsafe_get_user(v_regs, &sc->v_regs, efault_out);
  347. if (v_regs && !access_ok(v_regs, 34 * sizeof(vector128)))
  348. return -EFAULT;
  349. /* Copy 33 vec registers (vr0..31 and vscr) from the stack */
  350. if (v_regs != NULL && (msr & MSR_VEC) != 0) {
  351. unsafe_copy_from_user(&tsk->thread.vr_state, v_regs,
  352. 33 * sizeof(vector128), efault_out);
  353. tsk->thread.used_vr = true;
  354. } else if (tsk->thread.used_vr) {
  355. memset(&tsk->thread.vr_state, 0, 33 * sizeof(vector128));
  356. }
  357. /* Always get VRSAVE back */
  358. if (v_regs != NULL)
  359. unsafe_get_user(tsk->thread.vrsave, (u32 __user *)&v_regs[33], efault_out);
  360. else
  361. tsk->thread.vrsave = 0;
  362. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  363. mtspr(SPRN_VRSAVE, tsk->thread.vrsave);
  364. #endif /* CONFIG_ALTIVEC */
  365. /* restore floating point */
  366. unsafe_copy_fpr_from_user(tsk, &sc->fp_regs, efault_out);
  367. #ifdef CONFIG_VSX
  368. /*
  369. * Get additional VSX data. Update v_regs to point after the
  370. * VMX data. Copy VSX low doubleword from userspace to local
  371. * buffer for formatting, then into the taskstruct.
  372. */
  373. v_regs += ELF_NVRREG;
  374. if ((msr & MSR_VSX) != 0) {
  375. unsafe_copy_vsx_from_user(tsk, v_regs, efault_out);
  376. tsk->thread.used_vsr = true;
  377. } else {
  378. for (i = 0; i < 32 ; i++)
  379. tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  380. }
  381. #endif
  382. return 0;
  383. efault_out:
  384. return -EFAULT;
  385. }
  386. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  387. /*
  388. * Restore the two sigcontexts from the frame of a transactional processes.
  389. */
  390. static long restore_tm_sigcontexts(struct task_struct *tsk,
  391. struct sigcontext __user *sc,
  392. struct sigcontext __user *tm_sc)
  393. {
  394. #ifdef CONFIG_ALTIVEC
  395. elf_vrreg_t __user *v_regs, *tm_v_regs;
  396. #endif
  397. unsigned long err = 0;
  398. unsigned long msr;
  399. struct pt_regs *regs = tsk->thread.regs;
  400. #ifdef CONFIG_VSX
  401. int i;
  402. #endif
  403. BUG_ON(tsk != current);
  404. if (tm_suspend_disabled)
  405. return -EINVAL;
  406. /* copy the GPRs */
  407. err |= __copy_from_user(regs->gpr, tm_sc->gp_regs, sizeof(regs->gpr));
  408. err |= __copy_from_user(&tsk->thread.ckpt_regs, sc->gp_regs,
  409. sizeof(regs->gpr));
  410. /*
  411. * TFHAR is restored from the checkpointed 'wound-back' ucontext's NIP.
  412. * TEXASR was set by the signal delivery reclaim, as was TFIAR.
  413. * Users doing anything abhorrent like thread-switching w/ signals for
  414. * TM-Suspended code will have to back TEXASR/TFIAR up themselves.
  415. * For the case of getting a signal and simply returning from it,
  416. * we don't need to re-copy them here.
  417. */
  418. err |= __get_user(regs->nip, &tm_sc->gp_regs[PT_NIP]);
  419. err |= __get_user(tsk->thread.tm_tfhar, &sc->gp_regs[PT_NIP]);
  420. /* get MSR separately, transfer the LE bit if doing signal return */
  421. err |= __get_user(msr, &sc->gp_regs[PT_MSR]);
  422. /* Don't allow reserved mode. */
  423. if (MSR_TM_RESV(msr))
  424. return -EINVAL;
  425. /* pull in MSR LE from user context */
  426. regs_set_return_msr(regs, (regs->msr & ~MSR_LE) | (msr & MSR_LE));
  427. /* The following non-GPR non-FPR non-VR state is also checkpointed: */
  428. err |= __get_user(regs->ctr, &tm_sc->gp_regs[PT_CTR]);
  429. err |= __get_user(regs->link, &tm_sc->gp_regs[PT_LNK]);
  430. err |= __get_user(regs->xer, &tm_sc->gp_regs[PT_XER]);
  431. err |= __get_user(regs->ccr, &tm_sc->gp_regs[PT_CCR]);
  432. err |= __get_user(tsk->thread.ckpt_regs.ctr,
  433. &sc->gp_regs[PT_CTR]);
  434. err |= __get_user(tsk->thread.ckpt_regs.link,
  435. &sc->gp_regs[PT_LNK]);
  436. err |= __get_user(tsk->thread.ckpt_regs.xer,
  437. &sc->gp_regs[PT_XER]);
  438. err |= __get_user(tsk->thread.ckpt_regs.ccr,
  439. &sc->gp_regs[PT_CCR]);
  440. /* Don't allow userspace to set SOFTE */
  441. set_trap_norestart(regs);
  442. /* These regs are not checkpointed; they can go in 'regs'. */
  443. err |= __get_user(regs->dar, &sc->gp_regs[PT_DAR]);
  444. err |= __get_user(regs->dsisr, &sc->gp_regs[PT_DSISR]);
  445. err |= __get_user(regs->result, &sc->gp_regs[PT_RESULT]);
  446. /*
  447. * Force reload of FP/VEC.
  448. * This has to be done before copying stuff into tsk->thread.fpr/vr
  449. * for the reasons explained in the previous comment.
  450. */
  451. regs_set_return_msr(regs, regs->msr & ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX));
  452. #ifdef CONFIG_ALTIVEC
  453. err |= __get_user(v_regs, &sc->v_regs);
  454. err |= __get_user(tm_v_regs, &tm_sc->v_regs);
  455. if (err)
  456. return err;
  457. if (v_regs && !access_ok(v_regs, 34 * sizeof(vector128)))
  458. return -EFAULT;
  459. if (tm_v_regs && !access_ok(tm_v_regs, 34 * sizeof(vector128)))
  460. return -EFAULT;
  461. /* Copy 33 vec registers (vr0..31 and vscr) from the stack */
  462. if (v_regs != NULL && tm_v_regs != NULL && (msr & MSR_VEC) != 0) {
  463. err |= __copy_from_user(&tsk->thread.ckvr_state, v_regs,
  464. 33 * sizeof(vector128));
  465. err |= __copy_from_user(&tsk->thread.vr_state, tm_v_regs,
  466. 33 * sizeof(vector128));
  467. current->thread.used_vr = true;
  468. }
  469. else if (tsk->thread.used_vr) {
  470. memset(&tsk->thread.vr_state, 0, 33 * sizeof(vector128));
  471. memset(&tsk->thread.ckvr_state, 0, 33 * sizeof(vector128));
  472. }
  473. /* Always get VRSAVE back */
  474. if (v_regs != NULL && tm_v_regs != NULL) {
  475. err |= __get_user(tsk->thread.ckvrsave,
  476. (u32 __user *)&v_regs[33]);
  477. err |= __get_user(tsk->thread.vrsave,
  478. (u32 __user *)&tm_v_regs[33]);
  479. }
  480. else {
  481. tsk->thread.vrsave = 0;
  482. tsk->thread.ckvrsave = 0;
  483. }
  484. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  485. mtspr(SPRN_VRSAVE, tsk->thread.vrsave);
  486. #endif /* CONFIG_ALTIVEC */
  487. /* restore floating point */
  488. err |= copy_fpr_from_user(tsk, &tm_sc->fp_regs);
  489. err |= copy_ckfpr_from_user(tsk, &sc->fp_regs);
  490. #ifdef CONFIG_VSX
  491. /*
  492. * Get additional VSX data. Update v_regs to point after the
  493. * VMX data. Copy VSX low doubleword from userspace to local
  494. * buffer for formatting, then into the taskstruct.
  495. */
  496. if (v_regs && ((msr & MSR_VSX) != 0)) {
  497. v_regs += ELF_NVRREG;
  498. tm_v_regs += ELF_NVRREG;
  499. err |= copy_vsx_from_user(tsk, tm_v_regs);
  500. err |= copy_ckvsx_from_user(tsk, v_regs);
  501. tsk->thread.used_vsr = true;
  502. } else {
  503. for (i = 0; i < 32 ; i++) {
  504. tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  505. tsk->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  506. }
  507. }
  508. #endif
  509. tm_enable();
  510. /* Make sure the transaction is marked as failed */
  511. tsk->thread.tm_texasr |= TEXASR_FS;
  512. /*
  513. * Disabling preemption, since it is unsafe to be preempted
  514. * with MSR[TS] set without recheckpointing.
  515. */
  516. preempt_disable();
  517. /* pull in MSR TS bits from user context */
  518. regs_set_return_msr(regs, regs->msr | (msr & MSR_TS_MASK));
  519. /*
  520. * Ensure that TM is enabled in regs->msr before we leave the signal
  521. * handler. It could be the case that (a) user disabled the TM bit
  522. * through the manipulation of the MSR bits in uc_mcontext or (b) the
  523. * TM bit was disabled because a sufficient number of context switches
  524. * happened whilst in the signal handler and load_tm overflowed,
  525. * disabling the TM bit. In either case we can end up with an illegal
  526. * TM state leading to a TM Bad Thing when we return to userspace.
  527. *
  528. * CAUTION:
  529. * After regs->MSR[TS] being updated, make sure that get_user(),
  530. * put_user() or similar functions are *not* called. These
  531. * functions can generate page faults which will cause the process
  532. * to be de-scheduled with MSR[TS] set but without calling
  533. * tm_recheckpoint(). This can cause a bug.
  534. */
  535. regs_set_return_msr(regs, regs->msr | MSR_TM);
  536. /* This loads the checkpointed FP/VEC state, if used */
  537. tm_recheckpoint(&tsk->thread);
  538. msr_check_and_set(msr & (MSR_FP | MSR_VEC));
  539. if (msr & MSR_FP) {
  540. load_fp_state(&tsk->thread.fp_state);
  541. regs_set_return_msr(regs, regs->msr | (MSR_FP | tsk->thread.fpexc_mode));
  542. }
  543. if (msr & MSR_VEC) {
  544. load_vr_state(&tsk->thread.vr_state);
  545. regs_set_return_msr(regs, regs->msr | MSR_VEC);
  546. }
  547. preempt_enable();
  548. return err;
  549. }
  550. #else /* !CONFIG_PPC_TRANSACTIONAL_MEM */
  551. static long restore_tm_sigcontexts(struct task_struct *tsk, struct sigcontext __user *sc,
  552. struct sigcontext __user *tm_sc)
  553. {
  554. return -EINVAL;
  555. }
  556. #endif
  557. /*
  558. * Setup the trampoline code on the stack
  559. */
  560. static long setup_trampoline(unsigned int syscall, unsigned int __user *tramp)
  561. {
  562. int i;
  563. long err = 0;
  564. /* Call the handler and pop the dummy stackframe*/
  565. err |= __put_user(PPC_RAW_BCTRL(), &tramp[0]);
  566. err |= __put_user(PPC_RAW_ADDI(_R1, _R1, __SIGNAL_FRAMESIZE), &tramp[1]);
  567. err |= __put_user(PPC_RAW_LI(_R0, syscall), &tramp[2]);
  568. err |= __put_user(PPC_RAW_SC(), &tramp[3]);
  569. /* Minimal traceback info */
  570. for (i=TRAMP_TRACEBACK; i < TRAMP_SIZE ;i++)
  571. err |= __put_user(0, &tramp[i]);
  572. if (!err)
  573. flush_icache_range((unsigned long) &tramp[0],
  574. (unsigned long) &tramp[TRAMP_SIZE]);
  575. return err;
  576. }
  577. /*
  578. * Userspace code may pass a ucontext which doesn't include VSX added
  579. * at the end. We need to check for this case.
  580. */
  581. #define UCONTEXTSIZEWITHOUTVSX \
  582. (sizeof(struct ucontext) - 32*sizeof(long))
  583. /*
  584. * Handle {get,set,swap}_context operations
  585. */
  586. SYSCALL_DEFINE3(swapcontext, struct ucontext __user *, old_ctx,
  587. struct ucontext __user *, new_ctx, long, ctx_size)
  588. {
  589. sigset_t set;
  590. unsigned long new_msr = 0;
  591. int ctx_has_vsx_region = 0;
  592. if (new_ctx &&
  593. get_user(new_msr, &new_ctx->uc_mcontext.gp_regs[PT_MSR]))
  594. return -EFAULT;
  595. /*
  596. * Check that the context is not smaller than the original
  597. * size (with VMX but without VSX)
  598. */
  599. if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
  600. return -EINVAL;
  601. /*
  602. * If the new context state sets the MSR VSX bits but
  603. * it doesn't provide VSX state.
  604. */
  605. if ((ctx_size < sizeof(struct ucontext)) &&
  606. (new_msr & MSR_VSX))
  607. return -EINVAL;
  608. /* Does the context have enough room to store VSX data? */
  609. if (ctx_size >= sizeof(struct ucontext))
  610. ctx_has_vsx_region = 1;
  611. if (old_ctx != NULL) {
  612. prepare_setup_sigcontext(current);
  613. if (!user_write_access_begin(old_ctx, ctx_size))
  614. return -EFAULT;
  615. unsafe_setup_sigcontext(&old_ctx->uc_mcontext, current, 0, NULL,
  616. 0, ctx_has_vsx_region, efault_out);
  617. unsafe_copy_to_user(&old_ctx->uc_sigmask, &current->blocked,
  618. sizeof(sigset_t), efault_out);
  619. user_write_access_end();
  620. }
  621. if (new_ctx == NULL)
  622. return 0;
  623. if (!access_ok(new_ctx, ctx_size) ||
  624. fault_in_readable((char __user *)new_ctx, ctx_size))
  625. return -EFAULT;
  626. /*
  627. * If we get a fault copying the context into the kernel's
  628. * image of the user's registers, we can't just return -EFAULT
  629. * because the user's registers will be corrupted. For instance
  630. * the NIP value may have been updated but not some of the
  631. * other registers. Given that we have done the access_ok
  632. * and successfully read the first and last bytes of the region
  633. * above, this should only happen in an out-of-memory situation
  634. * or if another thread unmaps the region containing the context.
  635. * We kill the task with a SIGSEGV in this situation.
  636. */
  637. if (__get_user_sigset(&set, &new_ctx->uc_sigmask)) {
  638. force_exit_sig(SIGSEGV);
  639. return -EFAULT;
  640. }
  641. set_current_blocked(&set);
  642. if (!user_read_access_begin(new_ctx, ctx_size))
  643. return -EFAULT;
  644. if (__unsafe_restore_sigcontext(current, NULL, 0, &new_ctx->uc_mcontext)) {
  645. user_read_access_end();
  646. force_exit_sig(SIGSEGV);
  647. return -EFAULT;
  648. }
  649. user_read_access_end();
  650. /* This returns like rt_sigreturn */
  651. set_thread_flag(TIF_RESTOREALL);
  652. return 0;
  653. efault_out:
  654. user_write_access_end();
  655. return -EFAULT;
  656. }
  657. /*
  658. * Do a signal return; undo the signal stack.
  659. */
  660. SYSCALL_DEFINE0(rt_sigreturn)
  661. {
  662. struct pt_regs *regs = current_pt_regs();
  663. struct ucontext __user *uc = (struct ucontext __user *)regs->gpr[1];
  664. sigset_t set;
  665. unsigned long msr;
  666. /* Always make any pending restarted system calls return -EINTR */
  667. current->restart_block.fn = do_no_restart_syscall;
  668. if (!access_ok(uc, sizeof(*uc)))
  669. goto badframe;
  670. if (__get_user_sigset(&set, &uc->uc_sigmask))
  671. goto badframe;
  672. set_current_blocked(&set);
  673. if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM)) {
  674. /*
  675. * If there is a transactional state then throw it away.
  676. * The purpose of a sigreturn is to destroy all traces of the
  677. * signal frame, this includes any transactional state created
  678. * within in. We only check for suspended as we can never be
  679. * active in the kernel, we are active, there is nothing better to
  680. * do than go ahead and Bad Thing later.
  681. * The cause is not important as there will never be a
  682. * recheckpoint so it's not user visible.
  683. */
  684. if (MSR_TM_SUSPENDED(mfmsr()))
  685. tm_reclaim_current(0);
  686. /*
  687. * Disable MSR[TS] bit also, so, if there is an exception in the
  688. * code below (as a page fault in copy_ckvsx_to_user()), it does
  689. * not recheckpoint this task if there was a context switch inside
  690. * the exception.
  691. *
  692. * A major page fault can indirectly call schedule(). A reschedule
  693. * process in the middle of an exception can have a side effect
  694. * (Changing the CPU MSR[TS] state), since schedule() is called
  695. * with the CPU MSR[TS] disable and returns with MSR[TS]=Suspended
  696. * (switch_to() calls tm_recheckpoint() for the 'new' process). In
  697. * this case, the process continues to be the same in the CPU, but
  698. * the CPU state just changed.
  699. *
  700. * This can cause a TM Bad Thing, since the MSR in the stack will
  701. * have the MSR[TS]=0, and this is what will be used to RFID.
  702. *
  703. * Clearing MSR[TS] state here will avoid a recheckpoint if there
  704. * is any process reschedule in kernel space. The MSR[TS] state
  705. * does not need to be saved also, since it will be replaced with
  706. * the MSR[TS] that came from user context later, at
  707. * restore_tm_sigcontexts.
  708. */
  709. regs_set_return_msr(regs, regs->msr & ~MSR_TS_MASK);
  710. if (__get_user(msr, &uc->uc_mcontext.gp_regs[PT_MSR]))
  711. goto badframe;
  712. }
  713. if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) && MSR_TM_ACTIVE(msr)) {
  714. /* We recheckpoint on return. */
  715. struct ucontext __user *uc_transact;
  716. /* Trying to start TM on non TM system */
  717. if (!cpu_has_feature(CPU_FTR_TM))
  718. goto badframe;
  719. if (__get_user(uc_transact, &uc->uc_link))
  720. goto badframe;
  721. if (restore_tm_sigcontexts(current, &uc->uc_mcontext,
  722. &uc_transact->uc_mcontext))
  723. goto badframe;
  724. } else {
  725. /*
  726. * Fall through, for non-TM restore
  727. *
  728. * Unset MSR[TS] on the thread regs since MSR from user
  729. * context does not have MSR active, and recheckpoint was
  730. * not called since restore_tm_sigcontexts() was not called
  731. * also.
  732. *
  733. * If not unsetting it, the code can RFID to userspace with
  734. * MSR[TS] set, but without CPU in the proper state,
  735. * causing a TM bad thing.
  736. */
  737. regs_set_return_msr(current->thread.regs,
  738. current->thread.regs->msr & ~MSR_TS_MASK);
  739. if (!user_read_access_begin(&uc->uc_mcontext, sizeof(uc->uc_mcontext)))
  740. goto badframe;
  741. unsafe_restore_sigcontext(current, NULL, 1, &uc->uc_mcontext,
  742. badframe_block);
  743. user_read_access_end();
  744. }
  745. if (restore_altstack(&uc->uc_stack))
  746. goto badframe;
  747. set_thread_flag(TIF_RESTOREALL);
  748. return 0;
  749. badframe_block:
  750. user_read_access_end();
  751. badframe:
  752. signal_fault(current, regs, "rt_sigreturn", uc);
  753. force_sig(SIGSEGV);
  754. return 0;
  755. }
  756. int handle_rt_signal64(struct ksignal *ksig, sigset_t *set,
  757. struct task_struct *tsk)
  758. {
  759. struct rt_sigframe __user *frame;
  760. unsigned long newsp = 0;
  761. long err = 0;
  762. struct pt_regs *regs = tsk->thread.regs;
  763. /* Save the thread's msr before get_tm_stackpointer() changes it */
  764. unsigned long msr = regs->msr;
  765. frame = get_sigframe(ksig, tsk, sizeof(*frame), 0);
  766. /*
  767. * This only applies when calling unsafe_setup_sigcontext() and must be
  768. * called before opening the uaccess window.
  769. */
  770. if (!MSR_TM_ACTIVE(msr))
  771. prepare_setup_sigcontext(tsk);
  772. if (!user_write_access_begin(frame, sizeof(*frame)))
  773. goto badframe;
  774. unsafe_put_user(&frame->info, &frame->pinfo, badframe_block);
  775. unsafe_put_user(&frame->uc, &frame->puc, badframe_block);
  776. /* Create the ucontext. */
  777. unsafe_put_user(0, &frame->uc.uc_flags, badframe_block);
  778. unsafe_save_altstack(&frame->uc.uc_stack, regs->gpr[1], badframe_block);
  779. if (MSR_TM_ACTIVE(msr)) {
  780. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  781. /* The ucontext_t passed to userland points to the second
  782. * ucontext_t (for transactional state) with its uc_link ptr.
  783. */
  784. unsafe_put_user(&frame->uc_transact, &frame->uc.uc_link, badframe_block);
  785. user_write_access_end();
  786. err |= setup_tm_sigcontexts(&frame->uc.uc_mcontext,
  787. &frame->uc_transact.uc_mcontext,
  788. tsk, ksig->sig, NULL,
  789. (unsigned long)ksig->ka.sa.sa_handler,
  790. msr);
  791. if (!user_write_access_begin(&frame->uc.uc_sigmask,
  792. sizeof(frame->uc.uc_sigmask)))
  793. goto badframe;
  794. #endif
  795. } else {
  796. unsafe_put_user(0, &frame->uc.uc_link, badframe_block);
  797. unsafe_setup_sigcontext(&frame->uc.uc_mcontext, tsk, ksig->sig,
  798. NULL, (unsigned long)ksig->ka.sa.sa_handler,
  799. 1, badframe_block);
  800. }
  801. unsafe_copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set), badframe_block);
  802. user_write_access_end();
  803. /* Save the siginfo outside of the unsafe block. */
  804. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  805. goto badframe;
  806. /* Make sure signal handler doesn't get spurious FP exceptions */
  807. tsk->thread.fp_state.fpscr = 0;
  808. /* Set up to return from userspace. */
  809. if (tsk->mm->context.vdso) {
  810. regs_set_return_ip(regs, VDSO64_SYMBOL(tsk->mm->context.vdso, sigtramp_rt64));
  811. } else {
  812. err |= setup_trampoline(__NR_rt_sigreturn, &frame->tramp[0]);
  813. if (err)
  814. goto badframe;
  815. regs_set_return_ip(regs, (unsigned long) &frame->tramp[0]);
  816. }
  817. /* Allocate a dummy caller frame for the signal handler. */
  818. newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
  819. err |= put_user(regs->gpr[1], (unsigned long __user *)newsp);
  820. /* Set up "regs" so we "return" to the signal handler. */
  821. if (is_elf2_task()) {
  822. regs->ctr = (unsigned long) ksig->ka.sa.sa_handler;
  823. regs->gpr[12] = regs->ctr;
  824. } else {
  825. /* Handler is *really* a pointer to the function descriptor for
  826. * the signal routine. The first entry in the function
  827. * descriptor is the entry address of signal and the second
  828. * entry is the TOC value we need to use.
  829. */
  830. struct func_desc __user *ptr =
  831. (struct func_desc __user *)ksig->ka.sa.sa_handler;
  832. err |= get_user(regs->ctr, &ptr->addr);
  833. err |= get_user(regs->gpr[2], &ptr->toc);
  834. }
  835. /* enter the signal handler in native-endian mode */
  836. regs_set_return_msr(regs, (regs->msr & ~MSR_LE) | (MSR_KERNEL & MSR_LE));
  837. regs->gpr[1] = newsp;
  838. regs->gpr[3] = ksig->sig;
  839. regs->result = 0;
  840. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  841. regs->gpr[4] = (unsigned long)&frame->info;
  842. regs->gpr[5] = (unsigned long)&frame->uc;
  843. regs->gpr[6] = (unsigned long) frame;
  844. } else {
  845. regs->gpr[4] = (unsigned long)&frame->uc.uc_mcontext;
  846. }
  847. if (err)
  848. goto badframe;
  849. return 0;
  850. badframe_block:
  851. user_write_access_end();
  852. badframe:
  853. signal_fault(current, regs, "handle_rt_signal64", frame);
  854. return 1;
  855. }