signal.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4. * Chen Liqin <liqin.chen@sunplusct.com>
  5. * Lennox Wu <lennox.wu@sunplusct.com>
  6. * Copyright (C) 2012 Regents of the University of California
  7. */
  8. #include <linux/compat.h>
  9. #include <linux/signal.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/resume_user_mode.h>
  13. #include <linux/linkage.h>
  14. #include <asm/ucontext.h>
  15. #include <asm/vdso.h>
  16. #include <asm/signal.h>
  17. #include <asm/signal32.h>
  18. #include <asm/switch_to.h>
  19. #include <asm/csr.h>
  20. #include <asm/cacheflush.h>
  21. extern u32 __user_rt_sigreturn[2];
  22. #define DEBUG_SIG 0
  23. struct rt_sigframe {
  24. struct siginfo info;
  25. struct ucontext uc;
  26. #ifndef CONFIG_MMU
  27. u32 sigreturn_code[2];
  28. #endif
  29. };
  30. #ifdef CONFIG_FPU
  31. static long restore_fp_state(struct pt_regs *regs,
  32. union __riscv_fp_state __user *sc_fpregs)
  33. {
  34. long err;
  35. struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
  36. size_t i;
  37. err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
  38. if (unlikely(err))
  39. return err;
  40. fstate_restore(current, regs);
  41. /* We support no other extension state at this time. */
  42. for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
  43. u32 value;
  44. err = __get_user(value, &sc_fpregs->q.reserved[i]);
  45. if (unlikely(err))
  46. break;
  47. if (value != 0)
  48. return -EINVAL;
  49. }
  50. return err;
  51. }
  52. static long save_fp_state(struct pt_regs *regs,
  53. union __riscv_fp_state __user *sc_fpregs)
  54. {
  55. long err;
  56. struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
  57. size_t i;
  58. fstate_save(current, regs);
  59. err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
  60. if (unlikely(err))
  61. return err;
  62. /* We support no other extension state at this time. */
  63. for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
  64. err = __put_user(0, &sc_fpregs->q.reserved[i]);
  65. if (unlikely(err))
  66. break;
  67. }
  68. return err;
  69. }
  70. #else
  71. #define save_fp_state(task, regs) (0)
  72. #define restore_fp_state(task, regs) (0)
  73. #endif
  74. static long restore_sigcontext(struct pt_regs *regs,
  75. struct sigcontext __user *sc)
  76. {
  77. long err;
  78. /* sc_regs is structured the same as the start of pt_regs */
  79. err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
  80. /* Restore the floating-point state. */
  81. if (has_fpu())
  82. err |= restore_fp_state(regs, &sc->sc_fpregs);
  83. return err;
  84. }
  85. SYSCALL_DEFINE0(rt_sigreturn)
  86. {
  87. struct pt_regs *regs = current_pt_regs();
  88. struct rt_sigframe __user *frame;
  89. struct task_struct *task;
  90. sigset_t set;
  91. /* Always make any pending restarted system calls return -EINTR */
  92. current->restart_block.fn = do_no_restart_syscall;
  93. frame = (struct rt_sigframe __user *)regs->sp;
  94. if (!access_ok(frame, sizeof(*frame)))
  95. goto badframe;
  96. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  97. goto badframe;
  98. set_current_blocked(&set);
  99. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  100. goto badframe;
  101. if (restore_altstack(&frame->uc.uc_stack))
  102. goto badframe;
  103. regs->cause = -1UL;
  104. return regs->a0;
  105. badframe:
  106. task = current;
  107. if (show_unhandled_signals) {
  108. pr_info_ratelimited(
  109. "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
  110. task->comm, task_pid_nr(task), __func__,
  111. frame, (void *)regs->epc, (void *)regs->sp);
  112. }
  113. force_sig(SIGSEGV);
  114. return 0;
  115. }
  116. static long setup_sigcontext(struct rt_sigframe __user *frame,
  117. struct pt_regs *regs)
  118. {
  119. struct sigcontext __user *sc = &frame->uc.uc_mcontext;
  120. long err;
  121. /* sc_regs is structured the same as the start of pt_regs */
  122. err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
  123. /* Save the floating-point state. */
  124. if (has_fpu())
  125. err |= save_fp_state(regs, &sc->sc_fpregs);
  126. return err;
  127. }
  128. static inline void __user *get_sigframe(struct ksignal *ksig,
  129. struct pt_regs *regs, size_t framesize)
  130. {
  131. unsigned long sp;
  132. /* Default to using normal stack */
  133. sp = regs->sp;
  134. /*
  135. * If we are on the alternate signal stack and would overflow it, don't.
  136. * Return an always-bogus address instead so we will die with SIGSEGV.
  137. */
  138. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  139. return (void __user __force *)(-1UL);
  140. /* This is the X/Open sanctioned signal stack switching. */
  141. sp = sigsp(sp, ksig) - framesize;
  142. /* Align the stack frame. */
  143. sp &= ~0xfUL;
  144. return (void __user *)sp;
  145. }
  146. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  147. struct pt_regs *regs)
  148. {
  149. struct rt_sigframe __user *frame;
  150. long err = 0;
  151. unsigned long __maybe_unused addr;
  152. frame = get_sigframe(ksig, regs, sizeof(*frame));
  153. if (!access_ok(frame, sizeof(*frame)))
  154. return -EFAULT;
  155. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  156. /* Create the ucontext. */
  157. err |= __put_user(0, &frame->uc.uc_flags);
  158. err |= __put_user(NULL, &frame->uc.uc_link);
  159. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  160. err |= setup_sigcontext(frame, regs);
  161. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  162. if (err)
  163. return -EFAULT;
  164. /* Set up to return from userspace. */
  165. #ifdef CONFIG_MMU
  166. regs->ra = (unsigned long)VDSO_SYMBOL(
  167. current->mm->context.vdso, rt_sigreturn);
  168. #else
  169. /*
  170. * For the nommu case we don't have a VDSO. Instead we push two
  171. * instructions to call the rt_sigreturn syscall onto the user stack.
  172. */
  173. if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
  174. sizeof(frame->sigreturn_code)))
  175. return -EFAULT;
  176. addr = (unsigned long)&frame->sigreturn_code;
  177. /* Make sure the two instructions are pushed to icache. */
  178. flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
  179. regs->ra = addr;
  180. #endif /* CONFIG_MMU */
  181. /*
  182. * Set up registers for signal handler.
  183. * Registers that we don't modify keep the value they had from
  184. * user-space at the time we took the signal.
  185. * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
  186. * since some things rely on this (e.g. glibc's debug/segfault.c).
  187. */
  188. regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
  189. regs->sp = (unsigned long)frame;
  190. regs->a0 = ksig->sig; /* a0: signal number */
  191. regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
  192. regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
  193. #if DEBUG_SIG
  194. pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
  195. current->comm, task_pid_nr(current), ksig->sig,
  196. (void *)regs->epc, (void *)regs->ra, frame);
  197. #endif
  198. return 0;
  199. }
  200. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  201. {
  202. sigset_t *oldset = sigmask_to_save();
  203. int ret;
  204. /* Are we from a system call? */
  205. if (regs->cause == EXC_SYSCALL) {
  206. /* Avoid additional syscall restarting via ret_from_exception */
  207. regs->cause = -1UL;
  208. /* If so, check system call restarting.. */
  209. switch (regs->a0) {
  210. case -ERESTART_RESTARTBLOCK:
  211. case -ERESTARTNOHAND:
  212. regs->a0 = -EINTR;
  213. break;
  214. case -ERESTARTSYS:
  215. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  216. regs->a0 = -EINTR;
  217. break;
  218. }
  219. fallthrough;
  220. case -ERESTARTNOINTR:
  221. regs->a0 = regs->orig_a0;
  222. regs->epc -= 0x4;
  223. break;
  224. }
  225. }
  226. rseq_signal_deliver(ksig, regs);
  227. /* Set up the stack frame */
  228. if (is_compat_task())
  229. ret = compat_setup_rt_frame(ksig, oldset, regs);
  230. else
  231. ret = setup_rt_frame(ksig, oldset, regs);
  232. signal_setup_done(ret, ksig, 0);
  233. }
  234. static void do_signal(struct pt_regs *regs)
  235. {
  236. struct ksignal ksig;
  237. if (get_signal(&ksig)) {
  238. /* Actually deliver the signal */
  239. handle_signal(&ksig, regs);
  240. return;
  241. }
  242. /* Did we come from a system call? */
  243. if (regs->cause == EXC_SYSCALL) {
  244. /* Avoid additional syscall restarting via ret_from_exception */
  245. regs->cause = -1UL;
  246. /* Restart the system call - no handlers present */
  247. switch (regs->a0) {
  248. case -ERESTARTNOHAND:
  249. case -ERESTARTSYS:
  250. case -ERESTARTNOINTR:
  251. regs->a0 = regs->orig_a0;
  252. regs->epc -= 0x4;
  253. break;
  254. case -ERESTART_RESTARTBLOCK:
  255. regs->a0 = regs->orig_a0;
  256. regs->a7 = __NR_restart_syscall;
  257. regs->epc -= 0x4;
  258. break;
  259. }
  260. }
  261. /*
  262. * If there is no signal to deliver, we just put the saved
  263. * sigmask back.
  264. */
  265. restore_saved_sigmask();
  266. }
  267. /*
  268. * Handle any pending work on the resume-to-userspace path, as indicated by
  269. * _TIF_WORK_MASK. Entered from assembly with IRQs off.
  270. */
  271. asmlinkage __visible void do_work_pending(struct pt_regs *regs,
  272. unsigned long thread_info_flags)
  273. {
  274. do {
  275. if (thread_info_flags & _TIF_NEED_RESCHED) {
  276. schedule();
  277. } else {
  278. local_irq_enable();
  279. if (thread_info_flags & _TIF_UPROBE)
  280. uprobe_notify_resume(regs);
  281. /* Handle pending signal delivery */
  282. if (thread_info_flags & (_TIF_SIGPENDING |
  283. _TIF_NOTIFY_SIGNAL))
  284. do_signal(regs);
  285. if (thread_info_flags & _TIF_NOTIFY_RESUME)
  286. resume_user_mode_work(regs);
  287. }
  288. local_irq_disable();
  289. thread_info_flags = read_thread_flags();
  290. } while (thread_info_flags & _TIF_WORK_MASK);
  291. }