signal.c 9.0 KB

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