signal.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. /*
  55. * Enforce that sigcontext is like pt_regs, and doesn't mess
  56. * up our stack alignment rules.
  57. */
  58. BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
  59. BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
  60. for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
  61. err |= __get_user(regs->regs[i], &sc->gregs[i]);
  62. regs->faultnum = INT_SWINT_1_SIGRETURN;
  63. err |= __get_user(*pr0, &sc->gregs[0]);
  64. return err;
  65. }
  66. /* sigreturn() returns long since it restores r0 in the interrupted code. */
  67. long _sys_rt_sigreturn(struct pt_regs *regs)
  68. {
  69. struct rt_sigframe __user *frame =
  70. (struct rt_sigframe __user *)(regs->sp);
  71. sigset_t set;
  72. long r0;
  73. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  74. goto badframe;
  75. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  76. goto badframe;
  77. sigdelsetmask(&set, ~_BLOCKABLE);
  78. spin_lock_irq(&current->sighand->siglock);
  79. current->blocked = set;
  80. recalc_sigpending();
  81. spin_unlock_irq(&current->sighand->siglock);
  82. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
  83. goto badframe;
  84. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
  85. goto badframe;
  86. return r0;
  87. badframe:
  88. force_sig(SIGSEGV, current);
  89. return 0;
  90. }
  91. /*
  92. * Set up a signal frame.
  93. */
  94. int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
  95. {
  96. int i, err = 0;
  97. for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
  98. err |= __put_user(regs->regs[i], &sc->gregs[i]);
  99. return err;
  100. }
  101. /*
  102. * Determine which stack to use..
  103. */
  104. static inline void __user *get_sigframe(struct k_sigaction *ka,
  105. struct pt_regs *regs,
  106. size_t frame_size)
  107. {
  108. unsigned long sp;
  109. /* Default to using normal stack */
  110. sp = regs->sp;
  111. /*
  112. * If we are on the alternate signal stack and would overflow
  113. * it, don't. Return an always-bogus address instead so we
  114. * will die with SIGSEGV.
  115. */
  116. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
  117. return (void __user __force *)-1UL;
  118. /* This is the X/Open sanctioned signal stack switching. */
  119. if (ka->sa.sa_flags & SA_ONSTACK) {
  120. if (sas_ss_flags(sp) == 0)
  121. sp = current->sas_ss_sp + current->sas_ss_size;
  122. }
  123. sp -= frame_size;
  124. /*
  125. * Align the stack pointer according to the TILE ABI,
  126. * i.e. so that on function entry (sp & 15) == 0.
  127. */
  128. sp &= -16UL;
  129. return (void __user *) sp;
  130. }
  131. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  132. sigset_t *set, struct pt_regs *regs)
  133. {
  134. unsigned long restorer;
  135. struct rt_sigframe __user *frame;
  136. int err = 0;
  137. int usig;
  138. frame = get_sigframe(ka, regs, sizeof(*frame));
  139. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  140. goto give_sigsegv;
  141. usig = current_thread_info()->exec_domain
  142. && current_thread_info()->exec_domain->signal_invmap
  143. && sig < 32
  144. ? current_thread_info()->exec_domain->signal_invmap[sig]
  145. : sig;
  146. /* Always write at least the signal number for the stack backtracer. */
  147. if (ka->sa.sa_flags & SA_SIGINFO) {
  148. /* At sigreturn time, restore the callee-save registers too. */
  149. err |= copy_siginfo_to_user(&frame->info, info);
  150. regs->flags |= PT_FLAGS_RESTORE_REGS;
  151. } else {
  152. err |= __put_user(info->si_signo, &frame->info.si_signo);
  153. }
  154. /* Create the ucontext. */
  155. err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
  156. err |= __put_user(0, &frame->uc.uc_flags);
  157. err |= __put_user(NULL, &frame->uc.uc_link);
  158. err |= __put_user((void __user *)(current->sas_ss_sp),
  159. &frame->uc.uc_stack.ss_sp);
  160. err |= __put_user(sas_ss_flags(regs->sp),
  161. &frame->uc.uc_stack.ss_flags);
  162. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  163. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
  164. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  165. if (err)
  166. goto give_sigsegv;
  167. restorer = VDSO_BASE;
  168. if (ka->sa.sa_flags & SA_RESTORER)
  169. restorer = (unsigned long) ka->sa.sa_restorer;
  170. /*
  171. * Set up registers for signal handler.
  172. * Registers that we don't modify keep the value they had from
  173. * user-space at the time we took the signal.
  174. * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
  175. * since some things rely on this (e.g. glibc's debug/segfault.c).
  176. */
  177. regs->pc = (unsigned long) ka->sa.sa_handler;
  178. regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
  179. regs->sp = (unsigned long) frame;
  180. regs->lr = restorer;
  181. regs->regs[0] = (unsigned long) usig;
  182. regs->regs[1] = (unsigned long) &frame->info;
  183. regs->regs[2] = (unsigned long) &frame->uc;
  184. regs->flags |= PT_FLAGS_CALLER_SAVES;
  185. /*
  186. * Notify any tracer that was single-stepping it.
  187. * The tracer may want to single-step inside the
  188. * handler too.
  189. */
  190. if (test_thread_flag(TIF_SINGLESTEP))
  191. ptrace_notify(SIGTRAP);
  192. return 0;
  193. give_sigsegv:
  194. force_sigsegv(sig, current);
  195. return -EFAULT;
  196. }
  197. /*
  198. * OK, we're invoking a handler
  199. */
  200. static int handle_signal(unsigned long sig, siginfo_t *info,
  201. struct k_sigaction *ka, sigset_t *oldset,
  202. struct pt_regs *regs)
  203. {
  204. int ret;
  205. /* Are we from a system call? */
  206. if (regs->faultnum == INT_SWINT_1) {
  207. /* If so, check system call restarting.. */
  208. switch (regs->regs[0]) {
  209. case -ERESTART_RESTARTBLOCK:
  210. case -ERESTARTNOHAND:
  211. regs->regs[0] = -EINTR;
  212. break;
  213. case -ERESTARTSYS:
  214. if (!(ka->sa.sa_flags & SA_RESTART)) {
  215. regs->regs[0] = -EINTR;
  216. break;
  217. }
  218. /* fallthrough */
  219. case -ERESTARTNOINTR:
  220. /* Reload caller-saves to restore r0..r5 and r10. */
  221. regs->flags |= PT_FLAGS_CALLER_SAVES;
  222. regs->regs[0] = regs->orig_r0;
  223. regs->pc -= 8;
  224. }
  225. }
  226. /* Set up the stack frame */
  227. #ifdef CONFIG_COMPAT
  228. if (is_compat_task())
  229. ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
  230. else
  231. #endif
  232. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  233. if (ret == 0) {
  234. /* This code is only called from system calls or from
  235. * the work_pending path in the return-to-user code, and
  236. * either way we can re-enable interrupts unconditionally.
  237. */
  238. spin_lock_irq(&current->sighand->siglock);
  239. sigorsets(&current->blocked,
  240. &current->blocked, &ka->sa.sa_mask);
  241. if (!(ka->sa.sa_flags & SA_NODEFER))
  242. sigaddset(&current->blocked, sig);
  243. recalc_sigpending();
  244. spin_unlock_irq(&current->sighand->siglock);
  245. }
  246. return ret;
  247. }
  248. /*
  249. * Note that 'init' is a special process: it doesn't get signals it doesn't
  250. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  251. * mistake.
  252. */
  253. void do_signal(struct pt_regs *regs)
  254. {
  255. siginfo_t info;
  256. int signr;
  257. struct k_sigaction ka;
  258. sigset_t *oldset;
  259. /*
  260. * i386 will check if we're coming from kernel mode and bail out
  261. * here. In my experience this just turns weird crashes into
  262. * weird spin-hangs. But if we find a case where this seems
  263. * helpful, we can reinstate the check on "!user_mode(regs)".
  264. */
  265. if (current_thread_info()->status & TS_RESTORE_SIGMASK)
  266. oldset = &current->saved_sigmask;
  267. else
  268. oldset = &current->blocked;
  269. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  270. if (signr > 0) {
  271. /* Whee! Actually deliver the signal. */
  272. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  273. /*
  274. * A signal was successfully delivered; the saved
  275. * sigmask will have been stored in the signal frame,
  276. * and will be restored by sigreturn, so we can simply
  277. * clear the TS_RESTORE_SIGMASK flag.
  278. */
  279. current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
  280. }
  281. return;
  282. }
  283. /* Did we come from a system call? */
  284. if (regs->faultnum == INT_SWINT_1) {
  285. /* Restart the system call - no handlers present */
  286. switch (regs->regs[0]) {
  287. case -ERESTARTNOHAND:
  288. case -ERESTARTSYS:
  289. case -ERESTARTNOINTR:
  290. regs->flags |= PT_FLAGS_CALLER_SAVES;
  291. regs->regs[0] = regs->orig_r0;
  292. regs->pc -= 8;
  293. break;
  294. case -ERESTART_RESTARTBLOCK:
  295. regs->flags |= PT_FLAGS_CALLER_SAVES;
  296. regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
  297. regs->pc -= 8;
  298. break;
  299. }
  300. }
  301. /* If there's no signal to deliver, just put the saved sigmask back. */
  302. if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
  303. current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
  304. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  305. }
  306. }