signal.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * File: arch/blackfin/kernel/signal.c
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description:
  8. *
  9. * Modified:
  10. * Copyright 2004-2006 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <linux/signal.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/tty.h>
  33. #include <linux/personality.h>
  34. #include <linux/binfmts.h>
  35. #include <linux/freezer.h>
  36. #include <linux/uaccess.h>
  37. #include <asm/cacheflush.h>
  38. #include <asm/ucontext.h>
  39. #include <asm/fixed_code.h>
  40. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  41. struct fdpic_func_descriptor {
  42. unsigned long text;
  43. unsigned long GOT;
  44. };
  45. struct rt_sigframe {
  46. int sig;
  47. struct siginfo *pinfo;
  48. void *puc;
  49. /* This is no longer needed by the kernel, but unfortunately userspace
  50. * code expects it to be there. */
  51. char retcode[8];
  52. struct siginfo info;
  53. struct ucontext uc;
  54. };
  55. asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  56. {
  57. return do_sigaltstack(uss, uoss, rdusp());
  58. }
  59. static inline int
  60. rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
  61. {
  62. unsigned long usp = 0;
  63. int err = 0;
  64. #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
  65. /* restore passed registers */
  66. RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
  67. RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
  68. RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
  69. RESTORE(p4); RESTORE(p5);
  70. err |= __get_user(usp, &sc->sc_usp);
  71. wrusp(usp);
  72. RESTORE(a0w); RESTORE(a1w);
  73. RESTORE(a0x); RESTORE(a1x);
  74. RESTORE(astat);
  75. RESTORE(rets);
  76. RESTORE(pc);
  77. RESTORE(retx);
  78. RESTORE(fp);
  79. RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
  80. RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
  81. RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
  82. RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
  83. RESTORE(lc0); RESTORE(lc1);
  84. RESTORE(lt0); RESTORE(lt1);
  85. RESTORE(lb0); RESTORE(lb1);
  86. RESTORE(seqstat);
  87. regs->orig_p0 = -1; /* disable syscall checks */
  88. *pr0 = regs->r0;
  89. return err;
  90. }
  91. asmlinkage int do_rt_sigreturn(unsigned long __unused)
  92. {
  93. struct pt_regs *regs = (struct pt_regs *)__unused;
  94. unsigned long usp = rdusp();
  95. struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
  96. sigset_t set;
  97. int r0;
  98. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  99. goto badframe;
  100. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  101. goto badframe;
  102. sigdelsetmask(&set, ~_BLOCKABLE);
  103. spin_lock_irq(&current->sighand->siglock);
  104. current->blocked = set;
  105. recalc_sigpending();
  106. spin_unlock_irq(&current->sighand->siglock);
  107. if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
  108. goto badframe;
  109. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
  110. goto badframe;
  111. return r0;
  112. badframe:
  113. force_sig(SIGSEGV, current);
  114. return 0;
  115. }
  116. static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
  117. {
  118. int err = 0;
  119. #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
  120. SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
  121. SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
  122. SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
  123. SETUP(p4); SETUP(p5);
  124. err |= __put_user(rdusp(), &sc->sc_usp);
  125. SETUP(a0w); SETUP(a1w);
  126. SETUP(a0x); SETUP(a1x);
  127. SETUP(astat);
  128. SETUP(rets);
  129. SETUP(pc);
  130. SETUP(retx);
  131. SETUP(fp);
  132. SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
  133. SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
  134. SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
  135. SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
  136. SETUP(lc0); SETUP(lc1);
  137. SETUP(lt0); SETUP(lt1);
  138. SETUP(lb0); SETUP(lb1);
  139. SETUP(seqstat);
  140. return err;
  141. }
  142. static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
  143. size_t frame_size)
  144. {
  145. unsigned long usp;
  146. /* Default to using normal stack. */
  147. usp = rdusp();
  148. /* This is the X/Open sanctioned signal stack switching. */
  149. if (ka->sa.sa_flags & SA_ONSTACK) {
  150. if (!on_sig_stack(usp))
  151. usp = current->sas_ss_sp + current->sas_ss_size;
  152. }
  153. return (void *)((usp - frame_size) & -8UL);
  154. }
  155. static int
  156. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
  157. sigset_t * set, struct pt_regs *regs)
  158. {
  159. struct rt_sigframe *frame;
  160. int err = 0;
  161. frame = get_sigframe(ka, regs, sizeof(*frame));
  162. err |= __put_user((current_thread_info()->exec_domain
  163. && current_thread_info()->exec_domain->signal_invmap
  164. && sig < 32
  165. ? current_thread_info()->exec_domain->
  166. signal_invmap[sig] : sig), &frame->sig);
  167. err |= __put_user(&frame->info, &frame->pinfo);
  168. err |= __put_user(&frame->uc, &frame->puc);
  169. err |= copy_siginfo_to_user(&frame->info, info);
  170. /* Create the ucontext. */
  171. err |= __put_user(0, &frame->uc.uc_flags);
  172. err |= __put_user(0, &frame->uc.uc_link);
  173. err |=
  174. __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  175. err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
  176. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  177. err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
  178. err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  179. if (err)
  180. goto give_sigsegv;
  181. /* Set up registers for signal handler */
  182. wrusp((unsigned long)frame);
  183. if (get_personality & FDPIC_FUNCPTRS) {
  184. struct fdpic_func_descriptor __user *funcptr =
  185. (struct fdpic_func_descriptor *) ka->sa.sa_handler;
  186. __get_user(regs->pc, &funcptr->text);
  187. __get_user(regs->p3, &funcptr->GOT);
  188. } else
  189. regs->pc = (unsigned long)ka->sa.sa_handler;
  190. regs->rets = SIGRETURN_STUB;
  191. regs->r0 = frame->sig;
  192. regs->r1 = (unsigned long)(&frame->info);
  193. regs->r2 = (unsigned long)(&frame->uc);
  194. return 0;
  195. give_sigsegv:
  196. if (sig == SIGSEGV)
  197. ka->sa.sa_handler = SIG_DFL;
  198. force_sig(SIGSEGV, current);
  199. return -EFAULT;
  200. }
  201. static inline void
  202. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  203. {
  204. switch (regs->r0) {
  205. case -ERESTARTNOHAND:
  206. if (!has_handler)
  207. goto do_restart;
  208. regs->r0 = -EINTR;
  209. break;
  210. case -ERESTARTSYS:
  211. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  212. regs->r0 = -EINTR;
  213. break;
  214. }
  215. /* fallthrough */
  216. case -ERESTARTNOINTR:
  217. do_restart:
  218. regs->p0 = regs->orig_p0;
  219. regs->r0 = regs->orig_r0;
  220. regs->pc -= 2;
  221. break;
  222. }
  223. }
  224. /*
  225. * OK, we're invoking a handler
  226. */
  227. static int
  228. handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
  229. sigset_t *oldset, struct pt_regs *regs)
  230. {
  231. int ret;
  232. /* are we from a system call? to see pt_regs->orig_p0 */
  233. if (regs->orig_p0 >= 0)
  234. /* If so, check system call restarting.. */
  235. handle_restart(regs, ka, 1);
  236. /* set up the stack frame */
  237. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  238. if (ret == 0) {
  239. spin_lock_irq(&current->sighand->siglock);
  240. sigorsets(&current->blocked, &current->blocked,
  241. &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. * Note that we go through the signals twice: once to check the signals
  255. * that the kernel can handle, and then we build all the user-level signal
  256. * handling stack-frames in one go after that.
  257. */
  258. asmlinkage void do_signal(struct pt_regs *regs)
  259. {
  260. siginfo_t info;
  261. int signr;
  262. struct k_sigaction ka;
  263. sigset_t *oldset;
  264. current->thread.esp0 = (unsigned long)regs;
  265. if (try_to_freeze())
  266. goto no_signal;
  267. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  268. oldset = &current->saved_sigmask;
  269. else
  270. oldset = &current->blocked;
  271. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  272. if (signr > 0) {
  273. /* Whee! Actually deliver the signal. */
  274. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  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 TIF_RESTORE_SIGMASK flag */
  279. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  280. clear_thread_flag(TIF_RESTORE_SIGMASK);
  281. }
  282. return;
  283. }
  284. no_signal:
  285. /* Did we come from a system call? */
  286. if (regs->orig_p0 >= 0)
  287. /* Restart the system call - no handlers present */
  288. handle_restart(regs, NULL, 0);
  289. /* if there's no signal to deliver, we just put the saved sigmask
  290. * back */
  291. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  292. clear_thread_flag(TIF_RESTORE_SIGMASK);
  293. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  294. }
  295. }