signal.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/sched.h>
  4. #include <linux/mm.h>
  5. #include <linux/kernel.h>
  6. #include <linux/signal.h>
  7. #include <linux/syscalls.h>
  8. #include <linux/errno.h>
  9. #include <linux/wait.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/unistd.h>
  12. #include <linux/stddef.h>
  13. #include <linux/highuid.h>
  14. #include <linux/personality.h>
  15. #include <linux/tty.h>
  16. #include <linux/binfmts.h>
  17. #include <linux/tracehook.h>
  18. #include <linux/freezer.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/setup.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/traps.h>
  23. #include <asm/ucontext.h>
  24. #include <asm/vdso.h>
  25. #include <abi/regdef.h>
  26. #ifdef CONFIG_CPU_HAS_FPU
  27. #include <abi/fpu.h>
  28. static int restore_fpu_state(struct sigcontext *sc)
  29. {
  30. int err = 0;
  31. struct user_fp user_fp;
  32. err = copy_from_user(&user_fp, &sc->sc_user_fp, sizeof(user_fp));
  33. restore_from_user_fp(&user_fp);
  34. return err;
  35. }
  36. static int save_fpu_state(struct sigcontext *sc)
  37. {
  38. struct user_fp user_fp;
  39. save_to_user_fp(&user_fp);
  40. return copy_to_user(&sc->sc_user_fp, &user_fp, sizeof(user_fp));
  41. }
  42. #else
  43. static inline int restore_fpu_state(struct sigcontext *sc) { return 0; }
  44. static inline int save_fpu_state(struct sigcontext *sc) { return 0; }
  45. #endif
  46. struct rt_sigframe {
  47. int sig;
  48. struct siginfo *pinfo;
  49. void *puc;
  50. struct siginfo info;
  51. struct ucontext uc;
  52. };
  53. static int
  54. restore_sigframe(struct pt_regs *regs,
  55. struct sigcontext *sc, int *pr2)
  56. {
  57. int err = 0;
  58. /* Always make any pending restarted system calls return -EINTR */
  59. current_thread_info()->task->restart_block.fn = do_no_restart_syscall;
  60. err |= copy_from_user(regs, &sc->sc_pt_regs, sizeof(struct pt_regs));
  61. err |= restore_fpu_state(sc);
  62. *pr2 = regs->a0;
  63. return err;
  64. }
  65. asmlinkage int
  66. do_rt_sigreturn(void)
  67. {
  68. sigset_t set;
  69. int a0;
  70. struct pt_regs *regs = current_pt_regs();
  71. struct rt_sigframe *frame = (struct rt_sigframe *)(regs->usp);
  72. if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
  73. goto badframe;
  74. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  75. goto badframe;
  76. sigdelsetmask(&set, (sigmask(SIGKILL) | sigmask(SIGSTOP)));
  77. spin_lock_irq(&current->sighand->siglock);
  78. current->blocked = set;
  79. recalc_sigpending();
  80. spin_unlock_irq(&current->sighand->siglock);
  81. if (restore_sigframe(regs, &frame->uc.uc_mcontext, &a0))
  82. goto badframe;
  83. return a0;
  84. badframe:
  85. force_sig(SIGSEGV, current);
  86. return 0;
  87. }
  88. static int setup_sigframe(struct sigcontext *sc, struct pt_regs *regs)
  89. {
  90. int err = 0;
  91. err |= copy_to_user(&sc->sc_pt_regs, regs, sizeof(struct pt_regs));
  92. err |= save_fpu_state(sc);
  93. return err;
  94. }
  95. static inline void *
  96. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
  97. {
  98. unsigned long usp;
  99. /* Default to using normal stack. */
  100. usp = regs->usp;
  101. /* This is the X/Open sanctioned signal stack switching. */
  102. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(usp)) {
  103. if (!on_sig_stack(usp))
  104. usp = current->sas_ss_sp + current->sas_ss_size;
  105. }
  106. return (void *)((usp - frame_size) & -8UL);
  107. }
  108. static int
  109. setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  110. {
  111. struct rt_sigframe *frame;
  112. int err = 0;
  113. struct csky_vdso *vdso = current->mm->context.vdso;
  114. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
  115. if (!frame)
  116. return 1;
  117. err |= __put_user(ksig->sig, &frame->sig);
  118. err |= __put_user(&frame->info, &frame->pinfo);
  119. err |= __put_user(&frame->uc, &frame->puc);
  120. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  121. /* Create the ucontext. */
  122. err |= __put_user(0, &frame->uc.uc_flags);
  123. err |= __put_user(0, &frame->uc.uc_link);
  124. err |= __put_user((void *)current->sas_ss_sp,
  125. &frame->uc.uc_stack.ss_sp);
  126. err |= __put_user(sas_ss_flags(regs->usp),
  127. &frame->uc.uc_stack.ss_flags);
  128. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  129. err |= setup_sigframe(&frame->uc.uc_mcontext, regs);
  130. err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  131. if (err)
  132. goto give_sigsegv;
  133. /* Set up registers for signal handler */
  134. regs->usp = (unsigned long)frame;
  135. regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
  136. regs->lr = (unsigned long)vdso->rt_signal_retcode;
  137. adjust_stack:
  138. regs->a0 = ksig->sig; /* first arg is signo */
  139. regs->a1 = (unsigned long)(&(frame->info));
  140. regs->a2 = (unsigned long)(&(frame->uc));
  141. return err;
  142. give_sigsegv:
  143. if (ksig->sig == SIGSEGV)
  144. ksig->ka.sa.sa_handler = SIG_DFL;
  145. force_sig(SIGSEGV, current);
  146. goto adjust_stack;
  147. }
  148. /*
  149. * OK, we're invoking a handler
  150. */
  151. static int
  152. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  153. {
  154. int ret;
  155. sigset_t *oldset = sigmask_to_save();
  156. /*
  157. * set up the stack frame, regardless of SA_SIGINFO,
  158. * and pass info anyway.
  159. */
  160. ret = setup_rt_frame(ksig, oldset, regs);
  161. if (ret != 0) {
  162. force_sigsegv(ksig->sig, current);
  163. return ret;
  164. }
  165. /* Block the signal if we were successful. */
  166. spin_lock_irq(&current->sighand->siglock);
  167. sigorsets(&current->blocked, &current->blocked, &ksig->ka.sa.sa_mask);
  168. if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
  169. sigaddset(&current->blocked, ksig->sig);
  170. recalc_sigpending();
  171. spin_unlock_irq(&current->sighand->siglock);
  172. return 0;
  173. }
  174. /*
  175. * Note that 'init' is a special process: it doesn't get signals it doesn't
  176. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  177. * mistake.
  178. *
  179. * Note that we go through the signals twice: once to check the signals
  180. * that the kernel can handle, and then we build all the user-level signal
  181. * handling stack-frames in one go after that.
  182. */
  183. static void do_signal(struct pt_regs *regs, int syscall)
  184. {
  185. unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
  186. struct ksignal ksig;
  187. /*
  188. * We want the common case to go fast, which
  189. * is why we may in certain cases get here from
  190. * kernel mode. Just return without doing anything
  191. * if so.
  192. */
  193. if (!user_mode(regs))
  194. return;
  195. current->thread.esp0 = (unsigned long)regs;
  196. /*
  197. * If we were from a system call, check for system call restarting...
  198. */
  199. if (syscall) {
  200. continue_addr = regs->pc;
  201. #if defined(__CSKYABIV2__)
  202. restart_addr = continue_addr - 4;
  203. #else
  204. restart_addr = continue_addr - 2;
  205. #endif
  206. retval = regs->a0;
  207. /*
  208. * Prepare for system call restart. We do this here so that a
  209. * debugger will see the already changed.
  210. */
  211. switch (retval) {
  212. case -ERESTARTNOHAND:
  213. case -ERESTARTSYS:
  214. case -ERESTARTNOINTR:
  215. regs->a0 = regs->orig_a0;
  216. regs->pc = restart_addr;
  217. break;
  218. case -ERESTART_RESTARTBLOCK:
  219. regs->a0 = -EINTR;
  220. break;
  221. }
  222. }
  223. if (try_to_freeze())
  224. goto no_signal;
  225. /*
  226. * Get the signal to deliver. When running under ptrace, at this
  227. * point the debugger may change all our registers ...
  228. */
  229. if (get_signal(&ksig)) {
  230. /*
  231. * Depending on the signal settings we may need to revert the
  232. * decision to restart the system call. But skip this if a
  233. * debugger has chosen to restart at a different PC.
  234. */
  235. if (regs->pc == restart_addr) {
  236. if (retval == -ERESTARTNOHAND ||
  237. (retval == -ERESTARTSYS &&
  238. !(ksig.ka.sa.sa_flags & SA_RESTART))) {
  239. regs->a0 = -EINTR;
  240. regs->pc = continue_addr;
  241. }
  242. }
  243. /* Whee! Actually deliver the signal. */
  244. if (handle_signal(&ksig, regs) == 0) {
  245. /*
  246. * A signal was successfully delivered; the saved
  247. * sigmask will have been stored in the signal frame,
  248. * and will be restored by sigreturn, so we can simply
  249. * clear the TIF_RESTORE_SIGMASK flag.
  250. */
  251. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  252. clear_thread_flag(TIF_RESTORE_SIGMASK);
  253. }
  254. return;
  255. }
  256. no_signal:
  257. if (syscall) {
  258. /*
  259. * Handle restarting a different system call. As above,
  260. * if a debugger has chosen to restart at a different PC,
  261. * ignore the restart.
  262. */
  263. if (retval == -ERESTART_RESTARTBLOCK
  264. && regs->pc == continue_addr) {
  265. #if defined(__CSKYABIV2__)
  266. regs->regs[3] = __NR_restart_syscall;
  267. regs->pc -= 4;
  268. #else
  269. regs->regs[9] = __NR_restart_syscall;
  270. regs->pc -= 2;
  271. #endif
  272. }
  273. /*
  274. * If there's no signal to deliver, we just put the saved
  275. * sigmask back.
  276. */
  277. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  278. clear_thread_flag(TIF_RESTORE_SIGMASK);
  279. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  280. }
  281. }
  282. }
  283. asmlinkage void
  284. do_notify_resume(unsigned int thread_flags, struct pt_regs *regs, int syscall)
  285. {
  286. if (thread_flags & _TIF_SIGPENDING)
  287. do_signal(regs, syscall);
  288. if (thread_flags & _TIF_NOTIFY_RESUME) {
  289. clear_thread_flag(TIF_NOTIFY_RESUME);
  290. tracehook_notify_resume(regs);
  291. }
  292. }