signal.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 2004-2010 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2 or later
  5. */
  6. #include <linux/signal.h>
  7. #include <linux/syscalls.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/tty.h>
  10. #include <linux/personality.h>
  11. #include <linux/binfmts.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/tracehook.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/ucontext.h>
  16. #include <asm/fixed_code.h>
  17. #include <asm/syscall.h>
  18. /* Location of the trace bit in SYSCFG. */
  19. #define TRACE_BITS 0x0001
  20. struct fdpic_func_descriptor {
  21. unsigned long text;
  22. unsigned long GOT;
  23. };
  24. struct rt_sigframe {
  25. int sig;
  26. struct siginfo *pinfo;
  27. void *puc;
  28. /* This is no longer needed by the kernel, but unfortunately userspace
  29. * code expects it to be there. */
  30. char retcode[8];
  31. struct siginfo info;
  32. struct ucontext uc;
  33. };
  34. static inline int
  35. rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
  36. {
  37. unsigned long usp = 0;
  38. int err = 0;
  39. /* Always make any pending restarted system calls return -EINTR */
  40. current->restart_block.fn = do_no_restart_syscall;
  41. #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
  42. /* restore passed registers */
  43. RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
  44. RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
  45. RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
  46. RESTORE(p4); RESTORE(p5);
  47. err |= __get_user(usp, &sc->sc_usp);
  48. wrusp(usp);
  49. RESTORE(a0w); RESTORE(a1w);
  50. RESTORE(a0x); RESTORE(a1x);
  51. RESTORE(astat);
  52. RESTORE(rets);
  53. RESTORE(pc);
  54. RESTORE(retx);
  55. RESTORE(fp);
  56. RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
  57. RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
  58. RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
  59. RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
  60. RESTORE(lc0); RESTORE(lc1);
  61. RESTORE(lt0); RESTORE(lt1);
  62. RESTORE(lb0); RESTORE(lb1);
  63. RESTORE(seqstat);
  64. regs->orig_p0 = -1; /* disable syscall checks */
  65. *pr0 = regs->r0;
  66. return err;
  67. }
  68. asmlinkage int sys_rt_sigreturn(void)
  69. {
  70. struct pt_regs *regs = current_pt_regs();
  71. unsigned long usp = rdusp();
  72. struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
  73. sigset_t set;
  74. int r0;
  75. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  76. goto badframe;
  77. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  78. goto badframe;
  79. set_current_blocked(&set);
  80. if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
  81. goto badframe;
  82. if (restore_altstack(&frame->uc.uc_stack))
  83. goto badframe;
  84. return r0;
  85. badframe:
  86. force_sig(SIGSEGV, current);
  87. return 0;
  88. }
  89. static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
  90. {
  91. int err = 0;
  92. #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
  93. SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
  94. SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
  95. SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
  96. SETUP(p4); SETUP(p5);
  97. err |= __put_user(rdusp(), &sc->sc_usp);
  98. SETUP(a0w); SETUP(a1w);
  99. SETUP(a0x); SETUP(a1x);
  100. SETUP(astat);
  101. SETUP(rets);
  102. SETUP(pc);
  103. SETUP(retx);
  104. SETUP(fp);
  105. SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
  106. SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
  107. SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
  108. SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
  109. SETUP(lc0); SETUP(lc1);
  110. SETUP(lt0); SETUP(lt1);
  111. SETUP(lb0); SETUP(lb1);
  112. SETUP(seqstat);
  113. return err;
  114. }
  115. static inline void *get_sigframe(struct ksignal *ksig,
  116. size_t frame_size)
  117. {
  118. unsigned long usp = sigsp(rdusp(), ksig);
  119. return (void *)((usp - frame_size) & -8UL);
  120. }
  121. static int
  122. setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  123. {
  124. struct rt_sigframe *frame;
  125. int err = 0;
  126. frame = get_sigframe(ksig, sizeof(*frame));
  127. err |= __put_user(ksig->sig, &frame->sig);
  128. err |= __put_user(&frame->info, &frame->pinfo);
  129. err |= __put_user(&frame->uc, &frame->puc);
  130. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  131. /* Create the ucontext. */
  132. err |= __put_user(0, &frame->uc.uc_flags);
  133. err |= __put_user(0, &frame->uc.uc_link);
  134. err |= __save_altstack(&frame->uc.uc_stack, rdusp());
  135. err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
  136. err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  137. if (err)
  138. return -EFAULT;
  139. /* Set up registers for signal handler */
  140. if (current->personality & FDPIC_FUNCPTRS) {
  141. struct fdpic_func_descriptor __user *funcptr =
  142. (struct fdpic_func_descriptor *) ksig->ka.sa.sa_handler;
  143. u32 pc, p3;
  144. err |= __get_user(pc, &funcptr->text);
  145. err |= __get_user(p3, &funcptr->GOT);
  146. if (err)
  147. return -EFAULT;
  148. regs->pc = pc;
  149. regs->p3 = p3;
  150. } else
  151. regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
  152. wrusp((unsigned long)frame);
  153. regs->rets = SIGRETURN_STUB;
  154. regs->r0 = frame->sig;
  155. regs->r1 = (unsigned long)(&frame->info);
  156. regs->r2 = (unsigned long)(&frame->uc);
  157. return 0;
  158. }
  159. static inline void
  160. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  161. {
  162. switch (regs->r0) {
  163. case -ERESTARTNOHAND:
  164. if (!has_handler)
  165. goto do_restart;
  166. regs->r0 = -EINTR;
  167. break;
  168. case -ERESTARTSYS:
  169. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  170. regs->r0 = -EINTR;
  171. break;
  172. }
  173. /* fallthrough */
  174. case -ERESTARTNOINTR:
  175. do_restart:
  176. regs->p0 = regs->orig_p0;
  177. regs->r0 = regs->orig_r0;
  178. regs->pc -= 2;
  179. break;
  180. case -ERESTART_RESTARTBLOCK:
  181. regs->p0 = __NR_restart_syscall;
  182. regs->pc -= 2;
  183. break;
  184. }
  185. }
  186. /*
  187. * OK, we're invoking a handler
  188. */
  189. static void
  190. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  191. {
  192. int ret;
  193. /* are we from a system call? to see pt_regs->orig_p0 */
  194. if (regs->orig_p0 >= 0)
  195. /* If so, check system call restarting.. */
  196. handle_restart(regs, &ksig->ka, 1);
  197. /* set up the stack frame */
  198. ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
  199. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  200. }
  201. /*
  202. * Note that 'init' is a special process: it doesn't get signals it doesn't
  203. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  204. * mistake.
  205. *
  206. * Note that we go through the signals twice: once to check the signals
  207. * that the kernel can handle, and then we build all the user-level signal
  208. * handling stack-frames in one go after that.
  209. */
  210. asmlinkage void do_signal(struct pt_regs *regs)
  211. {
  212. struct ksignal ksig;
  213. current->thread.esp0 = (unsigned long)regs;
  214. if (get_signal(&ksig)) {
  215. /* Whee! Actually deliver the signal. */
  216. handle_signal(&ksig, regs);
  217. return;
  218. }
  219. /* Did we come from a system call? */
  220. if (regs->orig_p0 >= 0)
  221. /* Restart the system call - no handlers present */
  222. handle_restart(regs, NULL, 0);
  223. /* if there's no signal to deliver, we just put the saved sigmask
  224. * back */
  225. restore_saved_sigmask();
  226. }
  227. /*
  228. * notification of userspace execution resumption
  229. */
  230. asmlinkage void do_notify_resume(struct pt_regs *regs)
  231. {
  232. if (test_thread_flag(TIF_SIGPENDING))
  233. do_signal(regs);
  234. if (test_thread_flag(TIF_NOTIFY_RESUME)) {
  235. clear_thread_flag(TIF_NOTIFY_RESUME);
  236. tracehook_notify_resume(regs);
  237. }
  238. }