signal.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_thread_info()->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 k_sigaction *ka, struct pt_regs *regs,
  116. size_t frame_size)
  117. {
  118. unsigned long usp;
  119. /* Default to using normal stack. */
  120. usp = rdusp();
  121. /* This is the X/Open sanctioned signal stack switching. */
  122. if (ka->sa.sa_flags & SA_ONSTACK) {
  123. if (!on_sig_stack(usp))
  124. usp = current->sas_ss_sp + current->sas_ss_size;
  125. }
  126. return (void *)((usp - frame_size) & -8UL);
  127. }
  128. static int
  129. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
  130. sigset_t * set, struct pt_regs *regs)
  131. {
  132. struct rt_sigframe *frame;
  133. int err = 0;
  134. frame = get_sigframe(ka, regs, sizeof(*frame));
  135. err |= __put_user((current_thread_info()->exec_domain
  136. && current_thread_info()->exec_domain->signal_invmap
  137. && sig < 32
  138. ? current_thread_info()->exec_domain->
  139. signal_invmap[sig] : sig), &frame->sig);
  140. err |= __put_user(&frame->info, &frame->pinfo);
  141. err |= __put_user(&frame->uc, &frame->puc);
  142. err |= copy_siginfo_to_user(&frame->info, info);
  143. /* Create the ucontext. */
  144. err |= __put_user(0, &frame->uc.uc_flags);
  145. err |= __put_user(0, &frame->uc.uc_link);
  146. err |= __save_altstack(&frame->uc.uc_stack, rdusp());
  147. err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
  148. err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  149. if (err)
  150. return -EFAULT;
  151. /* Set up registers for signal handler */
  152. if (current->personality & FDPIC_FUNCPTRS) {
  153. struct fdpic_func_descriptor __user *funcptr =
  154. (struct fdpic_func_descriptor *) ka->sa.sa_handler;
  155. u32 pc, p3;
  156. err |= __get_user(pc, &funcptr->text);
  157. err |= __get_user(p3, &funcptr->GOT);
  158. if (err)
  159. return -EFAULT;
  160. regs->pc = pc;
  161. regs->p3 = p3;
  162. } else
  163. regs->pc = (unsigned long)ka->sa.sa_handler;
  164. wrusp((unsigned long)frame);
  165. regs->rets = SIGRETURN_STUB;
  166. regs->r0 = frame->sig;
  167. regs->r1 = (unsigned long)(&frame->info);
  168. regs->r2 = (unsigned long)(&frame->uc);
  169. return 0;
  170. }
  171. static inline void
  172. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  173. {
  174. switch (regs->r0) {
  175. case -ERESTARTNOHAND:
  176. if (!has_handler)
  177. goto do_restart;
  178. regs->r0 = -EINTR;
  179. break;
  180. case -ERESTARTSYS:
  181. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  182. regs->r0 = -EINTR;
  183. break;
  184. }
  185. /* fallthrough */
  186. case -ERESTARTNOINTR:
  187. do_restart:
  188. regs->p0 = regs->orig_p0;
  189. regs->r0 = regs->orig_r0;
  190. regs->pc -= 2;
  191. break;
  192. case -ERESTART_RESTARTBLOCK:
  193. regs->p0 = __NR_restart_syscall;
  194. regs->pc -= 2;
  195. break;
  196. }
  197. }
  198. /*
  199. * OK, we're invoking a handler
  200. */
  201. static void
  202. handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
  203. struct pt_regs *regs)
  204. {
  205. /* are we from a system call? to see pt_regs->orig_p0 */
  206. if (regs->orig_p0 >= 0)
  207. /* If so, check system call restarting.. */
  208. handle_restart(regs, ka, 1);
  209. /* set up the stack frame */
  210. if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
  211. force_sigsegv(sig, current);
  212. else
  213. signal_delivered(sig, info, ka, regs,
  214. test_thread_flag(TIF_SINGLESTEP));
  215. }
  216. /*
  217. * Note that 'init' is a special process: it doesn't get signals it doesn't
  218. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  219. * mistake.
  220. *
  221. * Note that we go through the signals twice: once to check the signals
  222. * that the kernel can handle, and then we build all the user-level signal
  223. * handling stack-frames in one go after that.
  224. */
  225. asmlinkage void do_signal(struct pt_regs *regs)
  226. {
  227. siginfo_t info;
  228. int signr;
  229. struct k_sigaction ka;
  230. current->thread.esp0 = (unsigned long)regs;
  231. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  232. if (signr > 0) {
  233. /* Whee! Actually deliver the signal. */
  234. handle_signal(signr, &info, &ka, regs);
  235. return;
  236. }
  237. /* Did we come from a system call? */
  238. if (regs->orig_p0 >= 0)
  239. /* Restart the system call - no handlers present */
  240. handle_restart(regs, NULL, 0);
  241. /* if there's no signal to deliver, we just put the saved sigmask
  242. * back */
  243. restore_saved_sigmask();
  244. }
  245. /*
  246. * notification of userspace execution resumption
  247. */
  248. asmlinkage void do_notify_resume(struct pt_regs *regs)
  249. {
  250. if (test_thread_flag(TIF_SIGPENDING))
  251. do_signal(regs);
  252. if (test_thread_flag(TIF_NOTIFY_RESUME)) {
  253. clear_thread_flag(TIF_NOTIFY_RESUME);
  254. tracehook_notify_resume(regs);
  255. }
  256. }