signal.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #ifdef CONFIG_FPU
  36. static long restore_fp_state(struct pt_regs *regs,
  37. union __riscv_fp_state *sc_fpregs)
  38. {
  39. long err;
  40. struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
  41. size_t i;
  42. err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
  43. if (unlikely(err))
  44. return err;
  45. fstate_restore(current, regs);
  46. /* We support no other extension state at this time. */
  47. for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
  48. u32 value;
  49. err = __get_user(value, &sc_fpregs->q.reserved[i]);
  50. if (unlikely(err))
  51. break;
  52. if (value != 0)
  53. return -EINVAL;
  54. }
  55. return err;
  56. }
  57. static long save_fp_state(struct pt_regs *regs,
  58. union __riscv_fp_state *sc_fpregs)
  59. {
  60. long err;
  61. struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
  62. size_t i;
  63. fstate_save(current, regs);
  64. err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
  65. if (unlikely(err))
  66. return err;
  67. /* We support no other extension state at this time. */
  68. for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
  69. err = __put_user(0, &sc_fpregs->q.reserved[i]);
  70. if (unlikely(err))
  71. break;
  72. }
  73. return err;
  74. }
  75. #else
  76. #define save_fp_state(task, regs) (0)
  77. #define restore_fp_state(task, regs) (0)
  78. #endif
  79. static long restore_sigcontext(struct pt_regs *regs,
  80. struct sigcontext __user *sc)
  81. {
  82. long err;
  83. /* sc_regs is structured the same as the start of pt_regs */
  84. err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
  85. /* Restore the floating-point state. */
  86. if (has_fpu)
  87. err |= restore_fp_state(regs, &sc->sc_fpregs);
  88. return err;
  89. }
  90. SYSCALL_DEFINE0(rt_sigreturn)
  91. {
  92. struct pt_regs *regs = current_pt_regs();
  93. struct rt_sigframe __user *frame;
  94. struct task_struct *task;
  95. sigset_t set;
  96. /* Always make any pending restarted system calls return -EINTR */
  97. current->restart_block.fn = do_no_restart_syscall;
  98. frame = (struct rt_sigframe __user *)regs->sp;
  99. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  100. goto badframe;
  101. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  102. goto badframe;
  103. set_current_blocked(&set);
  104. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  105. goto badframe;
  106. if (restore_altstack(&frame->uc.uc_stack))
  107. goto badframe;
  108. return regs->a0;
  109. badframe:
  110. task = current;
  111. if (show_unhandled_signals) {
  112. pr_info_ratelimited(
  113. "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
  114. task->comm, task_pid_nr(task), __func__,
  115. frame, (void *)regs->sepc, (void *)regs->sp);
  116. }
  117. force_sig(SIGSEGV, task);
  118. return 0;
  119. }
  120. static long setup_sigcontext(struct rt_sigframe __user *frame,
  121. struct pt_regs *regs)
  122. {
  123. struct sigcontext __user *sc = &frame->uc.uc_mcontext;
  124. long err;
  125. /* sc_regs is structured the same as the start of pt_regs */
  126. err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
  127. /* Save the floating-point state. */
  128. if (has_fpu)
  129. err |= save_fp_state(regs, &sc->sc_fpregs);
  130. return err;
  131. }
  132. static inline void __user *get_sigframe(struct ksignal *ksig,
  133. struct pt_regs *regs, size_t framesize)
  134. {
  135. unsigned long sp;
  136. /* Default to using normal stack */
  137. sp = regs->sp;
  138. /*
  139. * If we are on the alternate signal stack and would overflow it, don't.
  140. * Return an always-bogus address instead so we will die with SIGSEGV.
  141. */
  142. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  143. return (void __user __force *)(-1UL);
  144. /* This is the X/Open sanctioned signal stack switching. */
  145. sp = sigsp(sp, ksig) - framesize;
  146. /* Align the stack frame. */
  147. sp &= ~0xfUL;
  148. return (void __user *)sp;
  149. }
  150. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  151. struct pt_regs *regs)
  152. {
  153. struct rt_sigframe __user *frame;
  154. long err = 0;
  155. frame = get_sigframe(ksig, regs, sizeof(*frame));
  156. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  157. return -EFAULT;
  158. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  159. /* Create the ucontext. */
  160. err |= __put_user(0, &frame->uc.uc_flags);
  161. err |= __put_user(NULL, &frame->uc.uc_link);
  162. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  163. err |= setup_sigcontext(frame, regs);
  164. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  165. if (err)
  166. return -EFAULT;
  167. /* Set up to return from userspace. */
  168. regs->ra = (unsigned long)VDSO_SYMBOL(
  169. current->mm->context.vdso, rt_sigreturn);
  170. /*
  171. * Set up registers for signal handler.
  172. * Registers that we don't modify keep the value they had from
  173. * user-space at the time we took the signal.
  174. * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
  175. * since some things rely on this (e.g. glibc's debug/segfault.c).
  176. */
  177. regs->sepc = (unsigned long)ksig->ka.sa.sa_handler;
  178. regs->sp = (unsigned long)frame;
  179. regs->a0 = ksig->sig; /* a0: signal number */
  180. regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
  181. regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
  182. #if DEBUG_SIG
  183. pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
  184. current->comm, task_pid_nr(current), ksig->sig,
  185. (void *)regs->sepc, (void *)regs->ra, frame);
  186. #endif
  187. return 0;
  188. }
  189. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  190. {
  191. sigset_t *oldset = sigmask_to_save();
  192. int ret;
  193. /* Are we from a system call? */
  194. if (regs->scause == EXC_SYSCALL) {
  195. /* If so, check system call restarting.. */
  196. switch (regs->a0) {
  197. case -ERESTART_RESTARTBLOCK:
  198. case -ERESTARTNOHAND:
  199. regs->a0 = -EINTR;
  200. break;
  201. case -ERESTARTSYS:
  202. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  203. regs->a0 = -EINTR;
  204. break;
  205. }
  206. /* fallthrough */
  207. case -ERESTARTNOINTR:
  208. regs->a0 = regs->orig_a0;
  209. regs->sepc -= 0x4;
  210. break;
  211. }
  212. }
  213. /* Set up the stack frame */
  214. ret = setup_rt_frame(ksig, oldset, regs);
  215. signal_setup_done(ret, ksig, 0);
  216. }
  217. static void do_signal(struct pt_regs *regs)
  218. {
  219. struct ksignal ksig;
  220. if (get_signal(&ksig)) {
  221. /* Actually deliver the signal */
  222. handle_signal(&ksig, regs);
  223. return;
  224. }
  225. /* Did we come from a system call? */
  226. if (regs->scause == EXC_SYSCALL) {
  227. /* Restart the system call - no handlers present */
  228. switch (regs->a0) {
  229. case -ERESTARTNOHAND:
  230. case -ERESTARTSYS:
  231. case -ERESTARTNOINTR:
  232. regs->a0 = regs->orig_a0;
  233. regs->sepc -= 0x4;
  234. break;
  235. case -ERESTART_RESTARTBLOCK:
  236. regs->a0 = regs->orig_a0;
  237. regs->a7 = __NR_restart_syscall;
  238. regs->sepc -= 0x4;
  239. break;
  240. }
  241. }
  242. /*
  243. * If there is no signal to deliver, we just put the saved
  244. * sigmask back.
  245. */
  246. restore_saved_sigmask();
  247. }
  248. /*
  249. * notification of userspace execution resumption
  250. * - triggered by the _TIF_WORK_MASK flags
  251. */
  252. asmlinkage void do_notify_resume(struct pt_regs *regs,
  253. unsigned long thread_info_flags)
  254. {
  255. /* Handle pending signal delivery */
  256. if (thread_info_flags & _TIF_SIGPENDING)
  257. do_signal(regs);
  258. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  259. clear_thread_flag(TIF_NOTIFY_RESUME);
  260. tracehook_notify_resume(regs);
  261. }
  262. }