signal.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  35. {
  36. return do_sigaltstack(uss, uoss, rdusp());
  37. }
  38. static inline int
  39. rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
  40. {
  41. unsigned long usp = 0;
  42. int err = 0;
  43. /* Always make any pending restarted system calls return -EINTR */
  44. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  45. #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
  46. /* restore passed registers */
  47. RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
  48. RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
  49. RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
  50. RESTORE(p4); RESTORE(p5);
  51. err |= __get_user(usp, &sc->sc_usp);
  52. wrusp(usp);
  53. RESTORE(a0w); RESTORE(a1w);
  54. RESTORE(a0x); RESTORE(a1x);
  55. RESTORE(astat);
  56. RESTORE(rets);
  57. RESTORE(pc);
  58. RESTORE(retx);
  59. RESTORE(fp);
  60. RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
  61. RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
  62. RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
  63. RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
  64. RESTORE(lc0); RESTORE(lc1);
  65. RESTORE(lt0); RESTORE(lt1);
  66. RESTORE(lb0); RESTORE(lb1);
  67. RESTORE(seqstat);
  68. regs->orig_p0 = -1; /* disable syscall checks */
  69. *pr0 = regs->r0;
  70. return err;
  71. }
  72. asmlinkage int sys_rt_sigreturn(void)
  73. {
  74. struct pt_regs *regs = current_pt_regs();
  75. unsigned long usp = rdusp();
  76. struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
  77. sigset_t set;
  78. int r0;
  79. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  80. goto badframe;
  81. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  82. goto badframe;
  83. set_current_blocked(&set);
  84. if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
  85. goto badframe;
  86. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
  87. goto badframe;
  88. return r0;
  89. badframe:
  90. force_sig(SIGSEGV, current);
  91. return 0;
  92. }
  93. static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
  94. {
  95. int err = 0;
  96. #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
  97. SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
  98. SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
  99. SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
  100. SETUP(p4); SETUP(p5);
  101. err |= __put_user(rdusp(), &sc->sc_usp);
  102. SETUP(a0w); SETUP(a1w);
  103. SETUP(a0x); SETUP(a1x);
  104. SETUP(astat);
  105. SETUP(rets);
  106. SETUP(pc);
  107. SETUP(retx);
  108. SETUP(fp);
  109. SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
  110. SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
  111. SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
  112. SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
  113. SETUP(lc0); SETUP(lc1);
  114. SETUP(lt0); SETUP(lt1);
  115. SETUP(lb0); SETUP(lb1);
  116. SETUP(seqstat);
  117. return err;
  118. }
  119. static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
  120. size_t frame_size)
  121. {
  122. unsigned long usp;
  123. /* Default to using normal stack. */
  124. usp = rdusp();
  125. /* This is the X/Open sanctioned signal stack switching. */
  126. if (ka->sa.sa_flags & SA_ONSTACK) {
  127. if (!on_sig_stack(usp))
  128. usp = current->sas_ss_sp + current->sas_ss_size;
  129. }
  130. return (void *)((usp - frame_size) & -8UL);
  131. }
  132. static int
  133. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
  134. sigset_t * set, struct pt_regs *regs)
  135. {
  136. struct rt_sigframe *frame;
  137. int err = 0;
  138. frame = get_sigframe(ka, regs, sizeof(*frame));
  139. err |= __put_user((current_thread_info()->exec_domain
  140. && current_thread_info()->exec_domain->signal_invmap
  141. && sig < 32
  142. ? current_thread_info()->exec_domain->
  143. signal_invmap[sig] : sig), &frame->sig);
  144. err |= __put_user(&frame->info, &frame->pinfo);
  145. err |= __put_user(&frame->uc, &frame->puc);
  146. err |= copy_siginfo_to_user(&frame->info, info);
  147. /* Create the ucontext. */
  148. err |= __put_user(0, &frame->uc.uc_flags);
  149. err |= __put_user(0, &frame->uc.uc_link);
  150. err |=
  151. __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  152. err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
  153. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  154. err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
  155. err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  156. if (err)
  157. return -EFAULT;
  158. /* Set up registers for signal handler */
  159. if (current->personality & FDPIC_FUNCPTRS) {
  160. struct fdpic_func_descriptor __user *funcptr =
  161. (struct fdpic_func_descriptor *) ka->sa.sa_handler;
  162. u32 pc, p3;
  163. err |= __get_user(pc, &funcptr->text);
  164. err |= __get_user(p3, &funcptr->GOT);
  165. if (err)
  166. return -EFAULT;
  167. regs->pc = pc;
  168. regs->p3 = p3;
  169. } else
  170. regs->pc = (unsigned long)ka->sa.sa_handler;
  171. wrusp((unsigned long)frame);
  172. regs->rets = SIGRETURN_STUB;
  173. regs->r0 = frame->sig;
  174. regs->r1 = (unsigned long)(&frame->info);
  175. regs->r2 = (unsigned long)(&frame->uc);
  176. return 0;
  177. }
  178. static inline void
  179. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  180. {
  181. switch (regs->r0) {
  182. case -ERESTARTNOHAND:
  183. if (!has_handler)
  184. goto do_restart;
  185. regs->r0 = -EINTR;
  186. break;
  187. case -ERESTARTSYS:
  188. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  189. regs->r0 = -EINTR;
  190. break;
  191. }
  192. /* fallthrough */
  193. case -ERESTARTNOINTR:
  194. do_restart:
  195. regs->p0 = regs->orig_p0;
  196. regs->r0 = regs->orig_r0;
  197. regs->pc -= 2;
  198. break;
  199. case -ERESTART_RESTARTBLOCK:
  200. regs->p0 = __NR_restart_syscall;
  201. regs->pc -= 2;
  202. break;
  203. }
  204. }
  205. /*
  206. * OK, we're invoking a handler
  207. */
  208. static void
  209. handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
  210. struct pt_regs *regs)
  211. {
  212. /* are we from a system call? to see pt_regs->orig_p0 */
  213. if (regs->orig_p0 >= 0)
  214. /* If so, check system call restarting.. */
  215. handle_restart(regs, ka, 1);
  216. /* set up the stack frame */
  217. if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
  218. force_sigsegv(sig, current);
  219. else
  220. signal_delivered(sig, info, ka, regs,
  221. test_thread_flag(TIF_SINGLESTEP));
  222. }
  223. /*
  224. * Note that 'init' is a special process: it doesn't get signals it doesn't
  225. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  226. * mistake.
  227. *
  228. * Note that we go through the signals twice: once to check the signals
  229. * that the kernel can handle, and then we build all the user-level signal
  230. * handling stack-frames in one go after that.
  231. */
  232. asmlinkage void do_signal(struct pt_regs *regs)
  233. {
  234. siginfo_t info;
  235. int signr;
  236. struct k_sigaction ka;
  237. current->thread.esp0 = (unsigned long)regs;
  238. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  239. if (signr > 0) {
  240. /* Whee! Actually deliver the signal. */
  241. handle_signal(signr, &info, &ka, regs);
  242. return;
  243. }
  244. /* Did we come from a system call? */
  245. if (regs->orig_p0 >= 0)
  246. /* Restart the system call - no handlers present */
  247. handle_restart(regs, NULL, 0);
  248. /* if there's no signal to deliver, we just put the saved sigmask
  249. * back */
  250. restore_saved_sigmask();
  251. }
  252. /*
  253. * notification of userspace execution resumption
  254. */
  255. asmlinkage void do_notify_resume(struct pt_regs *regs)
  256. {
  257. if (test_thread_flag(TIF_SIGPENDING))
  258. do_signal(regs);
  259. if (test_thread_flag(TIF_NOTIFY_RESUME)) {
  260. clear_thread_flag(TIF_NOTIFY_RESUME);
  261. tracehook_notify_resume(regs);
  262. }
  263. }