signal.c 9.4 KB

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