signal.c 9.0 KB

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