signal.c 9.4 KB

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