signal.c 8.9 KB

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