signal.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright 2004-2009 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/freezer.h>
  13. #include <linux/uaccess.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/ucontext.h>
  16. #include <asm/fixed_code.h>
  17. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  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. #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
  44. /* restore passed registers */
  45. RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
  46. RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
  47. RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
  48. RESTORE(p4); RESTORE(p5);
  49. err |= __get_user(usp, &sc->sc_usp);
  50. wrusp(usp);
  51. RESTORE(a0w); RESTORE(a1w);
  52. RESTORE(a0x); RESTORE(a1x);
  53. RESTORE(astat);
  54. RESTORE(rets);
  55. RESTORE(pc);
  56. RESTORE(retx);
  57. RESTORE(fp);
  58. RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
  59. RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
  60. RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
  61. RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
  62. RESTORE(lc0); RESTORE(lc1);
  63. RESTORE(lt0); RESTORE(lt1);
  64. RESTORE(lb0); RESTORE(lb1);
  65. RESTORE(seqstat);
  66. regs->orig_p0 = -1; /* disable syscall checks */
  67. *pr0 = regs->r0;
  68. return err;
  69. }
  70. asmlinkage int do_rt_sigreturn(unsigned long __unused)
  71. {
  72. struct pt_regs *regs = (struct pt_regs *)__unused;
  73. unsigned long usp = rdusp();
  74. struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
  75. sigset_t set;
  76. int r0;
  77. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  78. goto badframe;
  79. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  80. goto badframe;
  81. sigdelsetmask(&set, ~_BLOCKABLE);
  82. spin_lock_irq(&current->sighand->siglock);
  83. current->blocked = set;
  84. recalc_sigpending();
  85. spin_unlock_irq(&current->sighand->siglock);
  86. if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
  87. goto badframe;
  88. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
  89. goto badframe;
  90. return r0;
  91. badframe:
  92. force_sig(SIGSEGV, current);
  93. return 0;
  94. }
  95. static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
  96. {
  97. int err = 0;
  98. #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
  99. SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
  100. SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
  101. SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
  102. SETUP(p4); SETUP(p5);
  103. err |= __put_user(rdusp(), &sc->sc_usp);
  104. SETUP(a0w); SETUP(a1w);
  105. SETUP(a0x); SETUP(a1x);
  106. SETUP(astat);
  107. SETUP(rets);
  108. SETUP(pc);
  109. SETUP(retx);
  110. SETUP(fp);
  111. SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
  112. SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
  113. SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
  114. SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
  115. SETUP(lc0); SETUP(lc1);
  116. SETUP(lt0); SETUP(lt1);
  117. SETUP(lb0); SETUP(lb1);
  118. SETUP(seqstat);
  119. return err;
  120. }
  121. static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
  122. size_t frame_size)
  123. {
  124. unsigned long usp;
  125. /* Default to using normal stack. */
  126. usp = rdusp();
  127. /* This is the X/Open sanctioned signal stack switching. */
  128. if (ka->sa.sa_flags & SA_ONSTACK) {
  129. if (!on_sig_stack(usp))
  130. usp = current->sas_ss_sp + current->sas_ss_size;
  131. }
  132. return (void *)((usp - frame_size) & -8UL);
  133. }
  134. static int
  135. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
  136. sigset_t * set, struct pt_regs *regs)
  137. {
  138. struct rt_sigframe *frame;
  139. int err = 0;
  140. frame = get_sigframe(ka, regs, sizeof(*frame));
  141. err |= __put_user((current_thread_info()->exec_domain
  142. && current_thread_info()->exec_domain->signal_invmap
  143. && sig < 32
  144. ? current_thread_info()->exec_domain->
  145. signal_invmap[sig] : sig), &frame->sig);
  146. err |= __put_user(&frame->info, &frame->pinfo);
  147. err |= __put_user(&frame->uc, &frame->puc);
  148. err |= copy_siginfo_to_user(&frame->info, info);
  149. /* Create the ucontext. */
  150. err |= __put_user(0, &frame->uc.uc_flags);
  151. err |= __put_user(0, &frame->uc.uc_link);
  152. err |=
  153. __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  154. err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
  155. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  156. err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
  157. err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  158. if (err)
  159. goto give_sigsegv;
  160. /* Set up registers for signal handler */
  161. wrusp((unsigned long)frame);
  162. if (current->personality & FDPIC_FUNCPTRS) {
  163. struct fdpic_func_descriptor __user *funcptr =
  164. (struct fdpic_func_descriptor *) ka->sa.sa_handler;
  165. __get_user(regs->pc, &funcptr->text);
  166. __get_user(regs->p3, &funcptr->GOT);
  167. } else
  168. regs->pc = (unsigned long)ka->sa.sa_handler;
  169. regs->rets = SIGRETURN_STUB;
  170. regs->r0 = frame->sig;
  171. regs->r1 = (unsigned long)(&frame->info);
  172. regs->r2 = (unsigned long)(&frame->uc);
  173. /*
  174. * Clear the trace flag when entering the signal handler, but
  175. * notify any tracer that was single-stepping it. The tracer
  176. * may want to single-step inside the handler too.
  177. */
  178. if (regs->syscfg & TRACE_BITS) {
  179. regs->syscfg &= ~TRACE_BITS;
  180. ptrace_notify(SIGTRAP);
  181. }
  182. return 0;
  183. give_sigsegv:
  184. if (sig == SIGSEGV)
  185. ka->sa.sa_handler = SIG_DFL;
  186. force_sig(SIGSEGV, current);
  187. return -EFAULT;
  188. }
  189. static inline void
  190. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  191. {
  192. switch (regs->r0) {
  193. case -ERESTARTNOHAND:
  194. if (!has_handler)
  195. goto do_restart;
  196. regs->r0 = -EINTR;
  197. break;
  198. case -ERESTARTSYS:
  199. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  200. regs->r0 = -EINTR;
  201. break;
  202. }
  203. /* fallthrough */
  204. case -ERESTARTNOINTR:
  205. do_restart:
  206. regs->p0 = regs->orig_p0;
  207. regs->r0 = regs->orig_r0;
  208. regs->pc -= 2;
  209. break;
  210. }
  211. }
  212. /*
  213. * OK, we're invoking a handler
  214. */
  215. static int
  216. handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
  217. sigset_t *oldset, struct pt_regs *regs)
  218. {
  219. int ret;
  220. /* are we from a system call? to see pt_regs->orig_p0 */
  221. if (regs->orig_p0 >= 0)
  222. /* If so, check system call restarting.. */
  223. handle_restart(regs, ka, 1);
  224. /* set up the stack frame */
  225. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  226. if (ret == 0) {
  227. spin_lock_irq(&current->sighand->siglock);
  228. sigorsets(&current->blocked, &current->blocked,
  229. &ka->sa.sa_mask);
  230. if (!(ka->sa.sa_flags & SA_NODEFER))
  231. sigaddset(&current->blocked, sig);
  232. recalc_sigpending();
  233. spin_unlock_irq(&current->sighand->siglock);
  234. }
  235. return ret;
  236. }
  237. /*
  238. * Note that 'init' is a special process: it doesn't get signals it doesn't
  239. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  240. * mistake.
  241. *
  242. * Note that we go through the signals twice: once to check the signals
  243. * that the kernel can handle, and then we build all the user-level signal
  244. * handling stack-frames in one go after that.
  245. */
  246. asmlinkage void do_signal(struct pt_regs *regs)
  247. {
  248. siginfo_t info;
  249. int signr;
  250. struct k_sigaction ka;
  251. sigset_t *oldset;
  252. current->thread.esp0 = (unsigned long)regs;
  253. if (try_to_freeze())
  254. goto no_signal;
  255. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  256. oldset = &current->saved_sigmask;
  257. else
  258. oldset = &current->blocked;
  259. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  260. if (signr > 0) {
  261. /* Whee! Actually deliver the signal. */
  262. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  263. /* a signal was successfully delivered; the saved
  264. * sigmask will have been stored in the signal frame,
  265. * and will be restored by sigreturn, so we can simply
  266. * clear the TIF_RESTORE_SIGMASK flag */
  267. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  268. clear_thread_flag(TIF_RESTORE_SIGMASK);
  269. }
  270. return;
  271. }
  272. no_signal:
  273. /* Did we come from a system call? */
  274. if (regs->orig_p0 >= 0)
  275. /* Restart the system call - no handlers present */
  276. handle_restart(regs, NULL, 0);
  277. /* if there's no signal to deliver, we just put the saved sigmask
  278. * back */
  279. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  280. clear_thread_flag(TIF_RESTORE_SIGMASK);
  281. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  282. }
  283. }