signal.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  3. * Chen Liqin <liqin.chen@sunplusct.com>
  4. * Lennox Wu <lennox.wu@sunplusct.com>
  5. * Copyright (C) 2012 Regents of the University of California
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see the file COPYING, or write
  19. * to the Free Software Foundation, Inc.,
  20. */
  21. #include <linux/signal.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/tracehook.h>
  25. #include <linux/linkage.h>
  26. #include <asm/ucontext.h>
  27. #include <asm/vdso.h>
  28. #include <asm/switch_to.h>
  29. #include <asm/csr.h>
  30. #define DEBUG_SIG 0
  31. struct rt_sigframe {
  32. struct siginfo info;
  33. struct ucontext uc;
  34. };
  35. static long restore_d_state(struct pt_regs *regs,
  36. struct __riscv_d_ext_state __user *state)
  37. {
  38. long err;
  39. err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
  40. if (likely(!err))
  41. fstate_restore(current, regs);
  42. return err;
  43. }
  44. static long save_d_state(struct pt_regs *regs,
  45. struct __riscv_d_ext_state __user *state)
  46. {
  47. fstate_save(current, regs);
  48. return __copy_to_user(state, &current->thread.fstate, sizeof(*state));
  49. }
  50. static long restore_sigcontext(struct pt_regs *regs,
  51. struct sigcontext __user *sc)
  52. {
  53. long err;
  54. size_t i;
  55. /* sc_regs is structured the same as the start of pt_regs */
  56. err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
  57. if (unlikely(err))
  58. return err;
  59. /* Restore the floating-point state. */
  60. err = restore_d_state(regs, &sc->sc_fpregs.d);
  61. if (unlikely(err))
  62. return err;
  63. /* We support no other extension state at this time. */
  64. for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) {
  65. u32 value;
  66. err = __get_user(value, &sc->sc_fpregs.q.reserved[i]);
  67. if (unlikely(err))
  68. break;
  69. if (value != 0)
  70. return -EINVAL;
  71. }
  72. return err;
  73. }
  74. SYSCALL_DEFINE0(rt_sigreturn)
  75. {
  76. struct pt_regs *regs = current_pt_regs();
  77. struct rt_sigframe __user *frame;
  78. struct task_struct *task;
  79. sigset_t set;
  80. /* Always make any pending restarted system calls return -EINTR */
  81. current->restart_block.fn = do_no_restart_syscall;
  82. frame = (struct rt_sigframe __user *)regs->sp;
  83. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  84. goto badframe;
  85. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  86. goto badframe;
  87. set_current_blocked(&set);
  88. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  89. goto badframe;
  90. if (restore_altstack(&frame->uc.uc_stack))
  91. goto badframe;
  92. return regs->a0;
  93. badframe:
  94. task = current;
  95. if (show_unhandled_signals) {
  96. pr_info_ratelimited(
  97. "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
  98. task->comm, task_pid_nr(task), __func__,
  99. frame, (void *)regs->sepc, (void *)regs->sp);
  100. }
  101. force_sig(SIGSEGV, task);
  102. return 0;
  103. }
  104. static long setup_sigcontext(struct rt_sigframe __user *frame,
  105. struct pt_regs *regs)
  106. {
  107. struct sigcontext __user *sc = &frame->uc.uc_mcontext;
  108. long err;
  109. size_t i;
  110. /* sc_regs is structured the same as the start of pt_regs */
  111. err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
  112. /* Save the floating-point state. */
  113. err |= save_d_state(regs, &sc->sc_fpregs.d);
  114. /* We support no other extension state at this time. */
  115. for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++)
  116. err |= __put_user(0, &sc->sc_fpregs.q.reserved[i]);
  117. return err;
  118. }
  119. static inline void __user *get_sigframe(struct ksignal *ksig,
  120. struct pt_regs *regs, size_t framesize)
  121. {
  122. unsigned long sp;
  123. /* Default to using normal stack */
  124. sp = regs->sp;
  125. /*
  126. * If we are on the alternate signal stack and would overflow it, don't.
  127. * Return an always-bogus address instead so we will die with SIGSEGV.
  128. */
  129. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  130. return (void __user __force *)(-1UL);
  131. /* This is the X/Open sanctioned signal stack switching. */
  132. sp = sigsp(sp, ksig) - framesize;
  133. /* Align the stack frame. */
  134. sp &= ~0xfUL;
  135. return (void __user *)sp;
  136. }
  137. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  138. struct pt_regs *regs)
  139. {
  140. struct rt_sigframe __user *frame;
  141. long err = 0;
  142. frame = get_sigframe(ksig, regs, sizeof(*frame));
  143. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  144. return -EFAULT;
  145. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  146. /* Create the ucontext. */
  147. err |= __put_user(0, &frame->uc.uc_flags);
  148. err |= __put_user(NULL, &frame->uc.uc_link);
  149. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  150. err |= setup_sigcontext(frame, regs);
  151. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  152. if (err)
  153. return -EFAULT;
  154. /* Set up to return from userspace. */
  155. regs->ra = (unsigned long)VDSO_SYMBOL(
  156. current->mm->context.vdso, rt_sigreturn);
  157. /*
  158. * Set up registers for signal handler.
  159. * Registers that we don't modify keep the value they had from
  160. * user-space at the time we took the signal.
  161. * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
  162. * since some things rely on this (e.g. glibc's debug/segfault.c).
  163. */
  164. regs->sepc = (unsigned long)ksig->ka.sa.sa_handler;
  165. regs->sp = (unsigned long)frame;
  166. regs->a0 = ksig->sig; /* a0: signal number */
  167. regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
  168. regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
  169. #if DEBUG_SIG
  170. pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
  171. current->comm, task_pid_nr(current), ksig->sig,
  172. (void *)regs->sepc, (void *)regs->ra, frame);
  173. #endif
  174. return 0;
  175. }
  176. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  177. {
  178. sigset_t *oldset = sigmask_to_save();
  179. int ret;
  180. /* Are we from a system call? */
  181. if (regs->scause == EXC_SYSCALL) {
  182. /* If so, check system call restarting.. */
  183. switch (regs->a0) {
  184. case -ERESTART_RESTARTBLOCK:
  185. case -ERESTARTNOHAND:
  186. regs->a0 = -EINTR;
  187. break;
  188. case -ERESTARTSYS:
  189. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  190. regs->a0 = -EINTR;
  191. break;
  192. }
  193. /* fallthrough */
  194. case -ERESTARTNOINTR:
  195. regs->a0 = regs->orig_a0;
  196. regs->sepc -= 0x4;
  197. break;
  198. }
  199. }
  200. /* Set up the stack frame */
  201. ret = setup_rt_frame(ksig, oldset, regs);
  202. signal_setup_done(ret, ksig, 0);
  203. }
  204. static void do_signal(struct pt_regs *regs)
  205. {
  206. struct ksignal ksig;
  207. if (get_signal(&ksig)) {
  208. /* Actually deliver the signal */
  209. handle_signal(&ksig, regs);
  210. return;
  211. }
  212. /* Did we come from a system call? */
  213. if (regs->scause == EXC_SYSCALL) {
  214. /* Restart the system call - no handlers present */
  215. switch (regs->a0) {
  216. case -ERESTARTNOHAND:
  217. case -ERESTARTSYS:
  218. case -ERESTARTNOINTR:
  219. regs->a0 = regs->orig_a0;
  220. regs->sepc -= 0x4;
  221. break;
  222. case -ERESTART_RESTARTBLOCK:
  223. regs->a0 = regs->orig_a0;
  224. regs->a7 = __NR_restart_syscall;
  225. regs->sepc -= 0x4;
  226. break;
  227. }
  228. }
  229. /*
  230. * If there is no signal to deliver, we just put the saved
  231. * sigmask back.
  232. */
  233. restore_saved_sigmask();
  234. }
  235. /*
  236. * notification of userspace execution resumption
  237. * - triggered by the _TIF_WORK_MASK flags
  238. */
  239. asmlinkage void do_notify_resume(struct pt_regs *regs,
  240. unsigned long thread_info_flags)
  241. {
  242. /* Handle pending signal delivery */
  243. if (thread_info_flags & _TIF_SIGPENDING)
  244. do_signal(regs);
  245. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  246. clear_thread_flag(TIF_NOTIFY_RESUME);
  247. tracehook_notify_resume(regs);
  248. }
  249. }