signal.c 9.8 KB

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