signal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #include <linux/errno.h>
  4. #include <linux/signal.h>
  5. #include <linux/ptrace.h>
  6. #include <linux/personality.h>
  7. #include <linux/freezer.h>
  8. #include <linux/tracehook.h>
  9. #include <linux/uaccess.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/ucontext.h>
  12. #include <asm/unistd.h>
  13. #include <asm/ptrace.h>
  14. #include <asm/vdso.h>
  15. struct rt_sigframe {
  16. struct siginfo info;
  17. struct ucontext uc;
  18. };
  19. static int restore_sigframe(struct pt_regs *regs,
  20. struct rt_sigframe __user * sf)
  21. {
  22. sigset_t set;
  23. int err;
  24. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  25. if (err == 0) {
  26. set_current_blocked(&set);
  27. }
  28. __get_user_error(regs->uregs[0], &sf->uc.uc_mcontext.nds32_r0, err);
  29. __get_user_error(regs->uregs[1], &sf->uc.uc_mcontext.nds32_r1, err);
  30. __get_user_error(regs->uregs[2], &sf->uc.uc_mcontext.nds32_r2, err);
  31. __get_user_error(regs->uregs[3], &sf->uc.uc_mcontext.nds32_r3, err);
  32. __get_user_error(regs->uregs[4], &sf->uc.uc_mcontext.nds32_r4, err);
  33. __get_user_error(regs->uregs[5], &sf->uc.uc_mcontext.nds32_r5, err);
  34. __get_user_error(regs->uregs[6], &sf->uc.uc_mcontext.nds32_r6, err);
  35. __get_user_error(regs->uregs[7], &sf->uc.uc_mcontext.nds32_r7, err);
  36. __get_user_error(regs->uregs[8], &sf->uc.uc_mcontext.nds32_r8, err);
  37. __get_user_error(regs->uregs[9], &sf->uc.uc_mcontext.nds32_r9, err);
  38. __get_user_error(regs->uregs[10], &sf->uc.uc_mcontext.nds32_r10, err);
  39. __get_user_error(regs->uregs[11], &sf->uc.uc_mcontext.nds32_r11, err);
  40. __get_user_error(regs->uregs[12], &sf->uc.uc_mcontext.nds32_r12, err);
  41. __get_user_error(regs->uregs[13], &sf->uc.uc_mcontext.nds32_r13, err);
  42. __get_user_error(regs->uregs[14], &sf->uc.uc_mcontext.nds32_r14, err);
  43. __get_user_error(regs->uregs[15], &sf->uc.uc_mcontext.nds32_r15, err);
  44. __get_user_error(regs->uregs[16], &sf->uc.uc_mcontext.nds32_r16, err);
  45. __get_user_error(regs->uregs[17], &sf->uc.uc_mcontext.nds32_r17, err);
  46. __get_user_error(regs->uregs[18], &sf->uc.uc_mcontext.nds32_r18, err);
  47. __get_user_error(regs->uregs[19], &sf->uc.uc_mcontext.nds32_r19, err);
  48. __get_user_error(regs->uregs[20], &sf->uc.uc_mcontext.nds32_r20, err);
  49. __get_user_error(regs->uregs[21], &sf->uc.uc_mcontext.nds32_r21, err);
  50. __get_user_error(regs->uregs[22], &sf->uc.uc_mcontext.nds32_r22, err);
  51. __get_user_error(regs->uregs[23], &sf->uc.uc_mcontext.nds32_r23, err);
  52. __get_user_error(regs->uregs[24], &sf->uc.uc_mcontext.nds32_r24, err);
  53. __get_user_error(regs->uregs[25], &sf->uc.uc_mcontext.nds32_r25, err);
  54. __get_user_error(regs->fp, &sf->uc.uc_mcontext.nds32_fp, err);
  55. __get_user_error(regs->gp, &sf->uc.uc_mcontext.nds32_gp, err);
  56. __get_user_error(regs->lp, &sf->uc.uc_mcontext.nds32_lp, err);
  57. __get_user_error(regs->sp, &sf->uc.uc_mcontext.nds32_sp, err);
  58. __get_user_error(regs->ipc, &sf->uc.uc_mcontext.nds32_ipc, err);
  59. #if defined(CONFIG_HWZOL)
  60. __get_user_error(regs->lc, &sf->uc.uc_mcontext.zol.nds32_lc, err);
  61. __get_user_error(regs->le, &sf->uc.uc_mcontext.zol.nds32_le, err);
  62. __get_user_error(regs->lb, &sf->uc.uc_mcontext.zol.nds32_lb, err);
  63. #endif
  64. /*
  65. * Avoid sys_rt_sigreturn() restarting.
  66. */
  67. forget_syscall(regs);
  68. return err;
  69. }
  70. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  71. {
  72. struct rt_sigframe __user *frame;
  73. /* Always make any pending restarted system calls return -EINTR */
  74. current->restart_block.fn = do_no_restart_syscall;
  75. /*
  76. * Since we stacked the signal on a 64-bit boundary,
  77. * then 'sp' should be two-word aligned here. If it's
  78. * not, then the user is trying to mess with us.
  79. */
  80. if (regs->sp & 7)
  81. goto badframe;
  82. frame = (struct rt_sigframe __user *)regs->sp;
  83. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  84. goto badframe;
  85. if (restore_sigframe(regs, frame))
  86. goto badframe;
  87. if (restore_altstack(&frame->uc.uc_stack))
  88. goto badframe;
  89. return regs->uregs[0];
  90. badframe:
  91. force_sig(SIGSEGV, current);
  92. return 0;
  93. }
  94. static int
  95. setup_sigframe(struct rt_sigframe __user * sf, struct pt_regs *regs,
  96. sigset_t * set)
  97. {
  98. int err = 0;
  99. __put_user_error(regs->uregs[0], &sf->uc.uc_mcontext.nds32_r0, err);
  100. __put_user_error(regs->uregs[1], &sf->uc.uc_mcontext.nds32_r1, err);
  101. __put_user_error(regs->uregs[2], &sf->uc.uc_mcontext.nds32_r2, err);
  102. __put_user_error(regs->uregs[3], &sf->uc.uc_mcontext.nds32_r3, err);
  103. __put_user_error(regs->uregs[4], &sf->uc.uc_mcontext.nds32_r4, err);
  104. __put_user_error(regs->uregs[5], &sf->uc.uc_mcontext.nds32_r5, err);
  105. __put_user_error(regs->uregs[6], &sf->uc.uc_mcontext.nds32_r6, err);
  106. __put_user_error(regs->uregs[7], &sf->uc.uc_mcontext.nds32_r7, err);
  107. __put_user_error(regs->uregs[8], &sf->uc.uc_mcontext.nds32_r8, err);
  108. __put_user_error(regs->uregs[9], &sf->uc.uc_mcontext.nds32_r9, err);
  109. __put_user_error(regs->uregs[10], &sf->uc.uc_mcontext.nds32_r10, err);
  110. __put_user_error(regs->uregs[11], &sf->uc.uc_mcontext.nds32_r11, err);
  111. __put_user_error(regs->uregs[12], &sf->uc.uc_mcontext.nds32_r12, err);
  112. __put_user_error(regs->uregs[13], &sf->uc.uc_mcontext.nds32_r13, err);
  113. __put_user_error(regs->uregs[14], &sf->uc.uc_mcontext.nds32_r14, err);
  114. __put_user_error(regs->uregs[15], &sf->uc.uc_mcontext.nds32_r15, err);
  115. __put_user_error(regs->uregs[16], &sf->uc.uc_mcontext.nds32_r16, err);
  116. __put_user_error(regs->uregs[17], &sf->uc.uc_mcontext.nds32_r17, err);
  117. __put_user_error(regs->uregs[18], &sf->uc.uc_mcontext.nds32_r18, err);
  118. __put_user_error(regs->uregs[19], &sf->uc.uc_mcontext.nds32_r19, err);
  119. __put_user_error(regs->uregs[20], &sf->uc.uc_mcontext.nds32_r20, err);
  120. __put_user_error(regs->uregs[21], &sf->uc.uc_mcontext.nds32_r21, err);
  121. __put_user_error(regs->uregs[22], &sf->uc.uc_mcontext.nds32_r22, err);
  122. __put_user_error(regs->uregs[23], &sf->uc.uc_mcontext.nds32_r23, err);
  123. __put_user_error(regs->uregs[24], &sf->uc.uc_mcontext.nds32_r24, err);
  124. __put_user_error(regs->uregs[25], &sf->uc.uc_mcontext.nds32_r25, err);
  125. __put_user_error(regs->fp, &sf->uc.uc_mcontext.nds32_fp, err);
  126. __put_user_error(regs->gp, &sf->uc.uc_mcontext.nds32_gp, err);
  127. __put_user_error(regs->lp, &sf->uc.uc_mcontext.nds32_lp, err);
  128. __put_user_error(regs->sp, &sf->uc.uc_mcontext.nds32_sp, err);
  129. __put_user_error(regs->ipc, &sf->uc.uc_mcontext.nds32_ipc, err);
  130. #if defined(CONFIG_HWZOL)
  131. __put_user_error(regs->lc, &sf->uc.uc_mcontext.zol.nds32_lc, err);
  132. __put_user_error(regs->le, &sf->uc.uc_mcontext.zol.nds32_le, err);
  133. __put_user_error(regs->lb, &sf->uc.uc_mcontext.zol.nds32_lb, err);
  134. #endif
  135. __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no,
  136. err);
  137. __put_user_error(current->thread.error_code,
  138. &sf->uc.uc_mcontext.error_code, err);
  139. __put_user_error(current->thread.address,
  140. &sf->uc.uc_mcontext.fault_address, err);
  141. __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
  142. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
  143. return err;
  144. }
  145. static inline void __user *get_sigframe(struct ksignal *ksig,
  146. struct pt_regs *regs, int framesize)
  147. {
  148. unsigned long sp;
  149. /* Default to using normal stack */
  150. sp = regs->sp;
  151. /*
  152. * If we are on the alternate signal stack and would overflow it, don't.
  153. * Return an always-bogus address instead so we will die with SIGSEGV.
  154. */
  155. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  156. return (void __user __force *)(-1UL);
  157. /* This is the X/Open sanctioned signal stack switching. */
  158. sp = (sigsp(sp, ksig) - framesize);
  159. /*
  160. * nds32 mandates 8-byte alignment
  161. */
  162. sp &= ~0x7UL;
  163. return (void __user *)sp;
  164. }
  165. static int
  166. setup_return(struct pt_regs *regs, struct ksignal *ksig, void __user * frame)
  167. {
  168. unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
  169. unsigned long retcode;
  170. retcode = VDSO_SYMBOL(current->mm->context.vdso, rt_sigtramp);
  171. regs->uregs[0] = ksig->sig;
  172. regs->sp = (unsigned long)frame;
  173. regs->lp = retcode;
  174. regs->ipc = handler;
  175. return 0;
  176. }
  177. static int
  178. setup_rt_frame(struct ksignal *ksig, sigset_t * set, struct pt_regs *regs)
  179. {
  180. struct rt_sigframe __user *frame =
  181. get_sigframe(ksig, regs, sizeof(*frame));
  182. int err = 0;
  183. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  184. return -EFAULT;
  185. __put_user_error(0, &frame->uc.uc_flags, err);
  186. __put_user_error(NULL, &frame->uc.uc_link, err);
  187. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  188. err |= setup_sigframe(frame, regs, set);
  189. if (err == 0) {
  190. setup_return(regs, ksig, frame);
  191. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  192. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  193. regs->uregs[1] = (unsigned long)&frame->info;
  194. regs->uregs[2] = (unsigned long)&frame->uc;
  195. }
  196. }
  197. return err;
  198. }
  199. /*
  200. * OK, we're invoking a handler
  201. */
  202. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  203. {
  204. int ret;
  205. sigset_t *oldset = sigmask_to_save();
  206. if (in_syscall(regs)) {
  207. /* Avoid additional syscall restarting via ret_slow_syscall. */
  208. forget_syscall(regs);
  209. switch (regs->uregs[0]) {
  210. case -ERESTART_RESTARTBLOCK:
  211. case -ERESTARTNOHAND:
  212. regs->uregs[0] = -EINTR;
  213. break;
  214. case -ERESTARTSYS:
  215. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  216. regs->uregs[0] = -EINTR;
  217. break;
  218. }
  219. case -ERESTARTNOINTR:
  220. regs->uregs[0] = regs->orig_r0;
  221. regs->ipc -= 4;
  222. break;
  223. }
  224. }
  225. /*
  226. * Set up the stack frame
  227. */
  228. ret = setup_rt_frame(ksig, oldset, regs);
  229. signal_setup_done(ret, ksig, 0);
  230. }
  231. /*
  232. * Note that 'init' is a special process: it doesn't get signals it doesn't
  233. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  234. * mistake.
  235. *
  236. * Note that we go through the signals twice: once to check the signals that
  237. * the kernel can handle, and then we build all the user-level signal handling
  238. * stack-frames in one go after that.
  239. */
  240. static void do_signal(struct pt_regs *regs)
  241. {
  242. struct ksignal ksig;
  243. if (get_signal(&ksig)) {
  244. handle_signal(&ksig, regs);
  245. return;
  246. }
  247. /*
  248. * If we were from a system call, check for system call restarting...
  249. */
  250. if (in_syscall(regs)) {
  251. /* Restart the system call - no handlers present */
  252. /* Avoid additional syscall restarting via ret_slow_syscall. */
  253. forget_syscall(regs);
  254. switch (regs->uregs[0]) {
  255. case -ERESTART_RESTARTBLOCK:
  256. regs->uregs[15] = __NR_restart_syscall;
  257. case -ERESTARTNOHAND:
  258. case -ERESTARTSYS:
  259. case -ERESTARTNOINTR:
  260. regs->uregs[0] = regs->orig_r0;
  261. regs->ipc -= 0x4;
  262. break;
  263. }
  264. }
  265. restore_saved_sigmask();
  266. }
  267. asmlinkage void
  268. do_notify_resume(struct pt_regs *regs, unsigned int thread_flags)
  269. {
  270. if (thread_flags & _TIF_SIGPENDING)
  271. do_signal(regs);
  272. if (thread_flags & _TIF_NOTIFY_RESUME) {
  273. clear_thread_flag(TIF_NOTIFY_RESUME);
  274. tracehook_notify_resume(regs);
  275. }
  276. }