signal.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation, version 2.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/sched.h>
  16. #include <linux/mm.h>
  17. #include <linux/smp.h>
  18. #include <linux/smp_lock.h>
  19. #include <linux/kernel.h>
  20. #include <linux/signal.h>
  21. #include <linux/errno.h>
  22. #include <linux/wait.h>
  23. #include <linux/unistd.h>
  24. #include <linux/stddef.h>
  25. #include <linux/personality.h>
  26. #include <linux/suspend.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/elf.h>
  29. #include <linux/compat.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/uaccess.h>
  32. #include <asm/processor.h>
  33. #include <asm/ucontext.h>
  34. #include <asm/sigframe.h>
  35. #include <asm/syscalls.h>
  36. #include <arch/interrupts.h>
  37. #define DEBUG_SIG 0
  38. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  39. long _sys_sigaltstack(const stack_t __user *uss,
  40. stack_t __user *uoss, struct pt_regs *regs)
  41. {
  42. return do_sigaltstack(uss, uoss, regs->sp);
  43. }
  44. /*
  45. * Do a signal return; undo the signal stack.
  46. */
  47. int restore_sigcontext(struct pt_regs *regs,
  48. struct sigcontext __user *sc, long *pr0)
  49. {
  50. int err = 0;
  51. int i;
  52. /* Always make any pending restarted system calls return -EINTR */
  53. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  54. for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
  55. err |= __get_user(((long *)regs)[i],
  56. &((long __user *)(&sc->regs))[i]);
  57. regs->faultnum = INT_SWINT_1_SIGRETURN;
  58. err |= __get_user(*pr0, &sc->regs.regs[0]);
  59. return err;
  60. }
  61. /* sigreturn() returns long since it restores r0 in the interrupted code. */
  62. long _sys_rt_sigreturn(struct pt_regs *regs)
  63. {
  64. struct rt_sigframe __user *frame =
  65. (struct rt_sigframe __user *)(regs->sp);
  66. sigset_t set;
  67. long r0;
  68. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  69. goto badframe;
  70. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  71. goto badframe;
  72. sigdelsetmask(&set, ~_BLOCKABLE);
  73. spin_lock_irq(&current->sighand->siglock);
  74. current->blocked = set;
  75. recalc_sigpending();
  76. spin_unlock_irq(&current->sighand->siglock);
  77. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
  78. goto badframe;
  79. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
  80. goto badframe;
  81. return r0;
  82. badframe:
  83. force_sig(SIGSEGV, current);
  84. return 0;
  85. }
  86. /*
  87. * Set up a signal frame.
  88. */
  89. int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
  90. {
  91. int i, err = 0;
  92. for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
  93. err |= __put_user(((long *)regs)[i],
  94. &((long __user *)(&sc->regs))[i]);
  95. return err;
  96. }
  97. /*
  98. * Determine which stack to use..
  99. */
  100. static inline void __user *get_sigframe(struct k_sigaction *ka,
  101. struct pt_regs *regs,
  102. size_t frame_size)
  103. {
  104. unsigned long sp;
  105. /* Default to using normal stack */
  106. sp = regs->sp;
  107. /*
  108. * If we are on the alternate signal stack and would overflow
  109. * it, don't. Return an always-bogus address instead so we
  110. * will die with SIGSEGV.
  111. */
  112. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
  113. return (void __user __force *)-1UL;
  114. /* This is the X/Open sanctioned signal stack switching. */
  115. if (ka->sa.sa_flags & SA_ONSTACK) {
  116. if (sas_ss_flags(sp) == 0)
  117. sp = current->sas_ss_sp + current->sas_ss_size;
  118. }
  119. sp -= frame_size;
  120. /*
  121. * Align the stack pointer according to the TILE ABI,
  122. * i.e. so that on function entry (sp & 15) == 0.
  123. */
  124. sp &= -16UL;
  125. return (void __user *) sp;
  126. }
  127. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  128. sigset_t *set, struct pt_regs *regs)
  129. {
  130. unsigned long restorer;
  131. struct rt_sigframe __user *frame;
  132. int err = 0;
  133. int usig;
  134. frame = get_sigframe(ka, regs, sizeof(*frame));
  135. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  136. goto give_sigsegv;
  137. usig = current_thread_info()->exec_domain
  138. && current_thread_info()->exec_domain->signal_invmap
  139. && sig < 32
  140. ? current_thread_info()->exec_domain->signal_invmap[sig]
  141. : sig;
  142. /* Always write at least the signal number for the stack backtracer. */
  143. if (ka->sa.sa_flags & SA_SIGINFO) {
  144. /* At sigreturn time, restore the callee-save registers too. */
  145. err |= copy_siginfo_to_user(&frame->info, info);
  146. regs->flags |= PT_FLAGS_RESTORE_REGS;
  147. } else {
  148. err |= __put_user(info->si_signo, &frame->info.si_signo);
  149. }
  150. /* Create the ucontext. */
  151. err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
  152. err |= __put_user(0, &frame->uc.uc_flags);
  153. err |= __put_user(NULL, &frame->uc.uc_link);
  154. err |= __put_user((void __user *)(current->sas_ss_sp),
  155. &frame->uc.uc_stack.ss_sp);
  156. err |= __put_user(sas_ss_flags(regs->sp),
  157. &frame->uc.uc_stack.ss_flags);
  158. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  159. err |= 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. restorer = VDSO_BASE;
  164. if (ka->sa.sa_flags & SA_RESTORER)
  165. restorer = (unsigned long) ka->sa.sa_restorer;
  166. /*
  167. * Set up registers for signal handler.
  168. * Registers that we don't modify keep the value they had from
  169. * user-space at the time we took the signal.
  170. */
  171. regs->pc = (unsigned long) ka->sa.sa_handler;
  172. regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
  173. regs->sp = (unsigned long) frame;
  174. regs->lr = restorer;
  175. regs->regs[0] = (unsigned long) usig;
  176. if (ka->sa.sa_flags & SA_SIGINFO) {
  177. /* Need extra arguments, so mark to restore caller-saves. */
  178. regs->regs[1] = (unsigned long) &frame->info;
  179. regs->regs[2] = (unsigned long) &frame->uc;
  180. regs->flags |= PT_FLAGS_CALLER_SAVES;
  181. }
  182. /*
  183. * Notify any tracer that was single-stepping it.
  184. * The tracer may want to single-step inside the
  185. * handler too.
  186. */
  187. if (test_thread_flag(TIF_SINGLESTEP))
  188. ptrace_notify(SIGTRAP);
  189. return 0;
  190. give_sigsegv:
  191. force_sigsegv(sig, current);
  192. return -EFAULT;
  193. }
  194. /*
  195. * OK, we're invoking a handler
  196. */
  197. static int handle_signal(unsigned long sig, siginfo_t *info,
  198. struct k_sigaction *ka, sigset_t *oldset,
  199. struct pt_regs *regs)
  200. {
  201. int ret;
  202. /* Are we from a system call? */
  203. if (regs->faultnum == INT_SWINT_1) {
  204. /* If so, check system call restarting.. */
  205. switch (regs->regs[0]) {
  206. case -ERESTART_RESTARTBLOCK:
  207. case -ERESTARTNOHAND:
  208. regs->regs[0] = -EINTR;
  209. break;
  210. case -ERESTARTSYS:
  211. if (!(ka->sa.sa_flags & SA_RESTART)) {
  212. regs->regs[0] = -EINTR;
  213. break;
  214. }
  215. /* fallthrough */
  216. case -ERESTARTNOINTR:
  217. /* Reload caller-saves to restore r0..r5 and r10. */
  218. regs->flags |= PT_FLAGS_CALLER_SAVES;
  219. regs->regs[0] = regs->orig_r0;
  220. regs->pc -= 8;
  221. }
  222. }
  223. /* Set up the stack frame */
  224. #ifdef CONFIG_COMPAT
  225. if (is_compat_task())
  226. ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
  227. else
  228. #endif
  229. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  230. if (ret == 0) {
  231. /* This code is only called from system calls or from
  232. * the work_pending path in the return-to-user code, and
  233. * either way we can re-enable interrupts unconditionally.
  234. */
  235. spin_lock_irq(&current->sighand->siglock);
  236. sigorsets(&current->blocked,
  237. &current->blocked, &ka->sa.sa_mask);
  238. if (!(ka->sa.sa_flags & SA_NODEFER))
  239. sigaddset(&current->blocked, sig);
  240. recalc_sigpending();
  241. spin_unlock_irq(&current->sighand->siglock);
  242. }
  243. return ret;
  244. }
  245. /*
  246. * Note that 'init' is a special process: it doesn't get signals it doesn't
  247. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  248. * mistake.
  249. */
  250. void do_signal(struct pt_regs *regs)
  251. {
  252. siginfo_t info;
  253. int signr;
  254. struct k_sigaction ka;
  255. sigset_t *oldset;
  256. /*
  257. * i386 will check if we're coming from kernel mode and bail out
  258. * here. In my experience this just turns weird crashes into
  259. * weird spin-hangs. But if we find a case where this seems
  260. * helpful, we can reinstate the check on "!user_mode(regs)".
  261. */
  262. if (current_thread_info()->status & TS_RESTORE_SIGMASK)
  263. oldset = &current->saved_sigmask;
  264. else
  265. oldset = &current->blocked;
  266. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  267. if (signr > 0) {
  268. /* Whee! Actually deliver the signal. */
  269. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  270. /*
  271. * A signal was successfully delivered; the saved
  272. * sigmask will have been stored in the signal frame,
  273. * and will be restored by sigreturn, so we can simply
  274. * clear the TS_RESTORE_SIGMASK flag.
  275. */
  276. current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
  277. }
  278. return;
  279. }
  280. /* Did we come from a system call? */
  281. if (regs->faultnum == INT_SWINT_1) {
  282. /* Restart the system call - no handlers present */
  283. switch (regs->regs[0]) {
  284. case -ERESTARTNOHAND:
  285. case -ERESTARTSYS:
  286. case -ERESTARTNOINTR:
  287. regs->flags |= PT_FLAGS_CALLER_SAVES;
  288. regs->regs[0] = regs->orig_r0;
  289. regs->pc -= 8;
  290. break;
  291. case -ERESTART_RESTARTBLOCK:
  292. regs->flags |= PT_FLAGS_CALLER_SAVES;
  293. regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
  294. regs->pc -= 8;
  295. break;
  296. }
  297. }
  298. /* If there's no signal to deliver, just put the saved sigmask back. */
  299. if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
  300. current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
  301. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  302. }
  303. }