signal.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/signal.h>
  3. #include <linux/uaccess.h>
  4. #include <linux/syscalls.h>
  5. #include <linux/resume_user_mode.h>
  6. #include <asm/traps.h>
  7. #include <asm/ucontext.h>
  8. #include <asm/vdso.h>
  9. #include <abi/regdef.h>
  10. #ifdef CONFIG_CPU_HAS_FPU
  11. #include <abi/fpu.h>
  12. static int restore_fpu_state(struct sigcontext __user *sc)
  13. {
  14. int err = 0;
  15. struct user_fp user_fp;
  16. err = __copy_from_user(&user_fp, &sc->sc_user_fp, sizeof(user_fp));
  17. restore_from_user_fp(&user_fp);
  18. return err;
  19. }
  20. static int save_fpu_state(struct sigcontext __user *sc)
  21. {
  22. struct user_fp user_fp;
  23. save_to_user_fp(&user_fp);
  24. return __copy_to_user(&sc->sc_user_fp, &user_fp, sizeof(user_fp));
  25. }
  26. #else
  27. #define restore_fpu_state(sigcontext) (0)
  28. #define save_fpu_state(sigcontext) (0)
  29. #endif
  30. struct rt_sigframe {
  31. /*
  32. * pad[3] is compatible with the same struct defined in
  33. * gcc/libgcc/config/csky/linux-unwind.h
  34. */
  35. int pad[3];
  36. struct siginfo info;
  37. struct ucontext uc;
  38. };
  39. static long restore_sigcontext(struct pt_regs *regs,
  40. struct sigcontext __user *sc)
  41. {
  42. int err = 0;
  43. unsigned long sr = regs->sr;
  44. /* sc_pt_regs is structured the same as the start of pt_regs */
  45. err |= __copy_from_user(regs, &sc->sc_pt_regs, sizeof(struct pt_regs));
  46. /* BIT(0) of regs->sr is Condition Code/Carry bit */
  47. regs->sr = (sr & ~1) | (regs->sr & 1);
  48. /* Restore the floating-point state. */
  49. err |= restore_fpu_state(sc);
  50. return err;
  51. }
  52. SYSCALL_DEFINE0(rt_sigreturn)
  53. {
  54. struct pt_regs *regs = current_pt_regs();
  55. struct rt_sigframe __user *frame;
  56. sigset_t set;
  57. /* Always make any pending restarted system calls return -EINTR */
  58. current->restart_block.fn = do_no_restart_syscall;
  59. frame = (struct rt_sigframe __user *)regs->usp;
  60. if (!access_ok(frame, sizeof(*frame)))
  61. goto badframe;
  62. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  63. goto badframe;
  64. set_current_blocked(&set);
  65. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  66. goto badframe;
  67. if (restore_altstack(&frame->uc.uc_stack))
  68. goto badframe;
  69. return regs->a0;
  70. badframe:
  71. force_sig(SIGSEGV);
  72. return 0;
  73. }
  74. static int setup_sigcontext(struct rt_sigframe __user *frame,
  75. struct pt_regs *regs)
  76. {
  77. struct sigcontext __user *sc = &frame->uc.uc_mcontext;
  78. int err = 0;
  79. err |= __copy_to_user(&sc->sc_pt_regs, regs, sizeof(struct pt_regs));
  80. err |= save_fpu_state(sc);
  81. return err;
  82. }
  83. static inline void __user *get_sigframe(struct ksignal *ksig,
  84. struct pt_regs *regs, size_t framesize)
  85. {
  86. unsigned long sp;
  87. /* Default to using normal stack */
  88. sp = regs->usp;
  89. /*
  90. * If we are on the alternate signal stack and would overflow it, don't.
  91. * Return an always-bogus address instead so we will die with SIGSEGV.
  92. */
  93. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  94. return (void __user __force *)(-1UL);
  95. /* This is the X/Open sanctioned signal stack switching. */
  96. sp = sigsp(sp, ksig) - framesize;
  97. /* Align the stack frame. */
  98. sp &= -8UL;
  99. return (void __user *)sp;
  100. }
  101. static int
  102. setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  103. {
  104. struct rt_sigframe __user *frame;
  105. int err = 0;
  106. frame = get_sigframe(ksig, regs, sizeof(*frame));
  107. if (!access_ok(frame, sizeof(*frame)))
  108. return -EFAULT;
  109. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  110. /* Create the ucontext. */
  111. err |= __put_user(0, &frame->uc.uc_flags);
  112. err |= __put_user(NULL, &frame->uc.uc_link);
  113. err |= __save_altstack(&frame->uc.uc_stack, regs->usp);
  114. err |= setup_sigcontext(frame, regs);
  115. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  116. if (err)
  117. return -EFAULT;
  118. /* Set up to return from userspace. */
  119. regs->lr = (unsigned long)VDSO_SYMBOL(
  120. current->mm->context.vdso, rt_sigreturn);
  121. /*
  122. * Set up registers for signal handler.
  123. * Registers that we don't modify keep the value they had from
  124. * user-space at the time we took the signal.
  125. * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
  126. * since some things rely on this (e.g. glibc's debug/segfault.c).
  127. */
  128. regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
  129. regs->usp = (unsigned long)frame;
  130. regs->a0 = ksig->sig; /* a0: signal number */
  131. regs->a1 = (unsigned long)(&(frame->info)); /* a1: siginfo pointer */
  132. regs->a2 = (unsigned long)(&(frame->uc)); /* a2: ucontext pointer */
  133. return 0;
  134. }
  135. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  136. {
  137. sigset_t *oldset = sigmask_to_save();
  138. int ret;
  139. rseq_signal_deliver(ksig, regs);
  140. /* Are we from a system call? */
  141. if (in_syscall(regs)) {
  142. /* Avoid additional syscall restarting via ret_from_exception */
  143. forget_syscall(regs);
  144. /* If so, check system call restarting.. */
  145. switch (regs->a0) {
  146. case -ERESTART_RESTARTBLOCK:
  147. case -ERESTARTNOHAND:
  148. regs->a0 = -EINTR;
  149. break;
  150. case -ERESTARTSYS:
  151. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  152. regs->a0 = -EINTR;
  153. break;
  154. }
  155. fallthrough;
  156. case -ERESTARTNOINTR:
  157. regs->a0 = regs->orig_a0;
  158. regs->pc -= TRAP0_SIZE;
  159. break;
  160. }
  161. }
  162. /* Set up the stack frame */
  163. ret = setup_rt_frame(ksig, oldset, regs);
  164. signal_setup_done(ret, ksig, 0);
  165. }
  166. static void do_signal(struct pt_regs *regs)
  167. {
  168. struct ksignal ksig;
  169. if (get_signal(&ksig)) {
  170. /* Actually deliver the signal */
  171. handle_signal(&ksig, regs);
  172. return;
  173. }
  174. /* Did we come from a system call? */
  175. if (in_syscall(regs)) {
  176. /* Avoid additional syscall restarting via ret_from_exception */
  177. forget_syscall(regs);
  178. /* Restart the system call - no handlers present */
  179. switch (regs->a0) {
  180. case -ERESTARTNOHAND:
  181. case -ERESTARTSYS:
  182. case -ERESTARTNOINTR:
  183. regs->a0 = regs->orig_a0;
  184. regs->pc -= TRAP0_SIZE;
  185. break;
  186. case -ERESTART_RESTARTBLOCK:
  187. regs->a0 = regs->orig_a0;
  188. regs_syscallid(regs) = __NR_restart_syscall;
  189. regs->pc -= TRAP0_SIZE;
  190. break;
  191. }
  192. }
  193. /*
  194. * If there is no signal to deliver, we just put the saved
  195. * sigmask back.
  196. */
  197. restore_saved_sigmask();
  198. }
  199. /*
  200. * notification of userspace execution resumption
  201. * - triggered by the _TIF_WORK_MASK flags
  202. */
  203. asmlinkage void do_notify_resume(struct pt_regs *regs,
  204. unsigned long thread_info_flags)
  205. {
  206. if (thread_info_flags & _TIF_UPROBE)
  207. uprobe_notify_resume(regs);
  208. /* Handle pending signal delivery */
  209. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
  210. do_signal(regs);
  211. if (thread_info_flags & _TIF_NOTIFY_RESUME)
  212. resume_user_mode_work(regs);
  213. }