signal.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * Based on linux/arch/sh/kernel/signal.c
  5. * Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/errno.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/unistd.h>
  17. #include <linux/freezer.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/ucontext.h>
  20. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  21. asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  22. struct pt_regs *regs)
  23. {
  24. return do_sigaltstack(uss, uoss, regs->sp);
  25. }
  26. struct rt_sigframe
  27. {
  28. struct siginfo info;
  29. struct ucontext uc;
  30. unsigned long retcode;
  31. };
  32. static int
  33. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  34. {
  35. int err = 0;
  36. #define COPY(x) err |= __get_user(regs->x, &sc->x)
  37. COPY(sr);
  38. COPY(pc);
  39. COPY(lr);
  40. COPY(sp);
  41. COPY(r12);
  42. COPY(r11);
  43. COPY(r10);
  44. COPY(r9);
  45. COPY(r8);
  46. COPY(r7);
  47. COPY(r6);
  48. COPY(r5);
  49. COPY(r4);
  50. COPY(r3);
  51. COPY(r2);
  52. COPY(r1);
  53. COPY(r0);
  54. #undef COPY
  55. /*
  56. * Don't allow anyone to pretend they're running in supervisor
  57. * mode or something...
  58. */
  59. err |= !valid_user_regs(regs);
  60. return err;
  61. }
  62. asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
  63. {
  64. struct rt_sigframe __user *frame;
  65. sigset_t set;
  66. frame = (struct rt_sigframe __user *)regs->sp;
  67. pr_debug("SIG return: frame = %p\n", frame);
  68. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  69. goto badframe;
  70. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  71. goto badframe;
  72. sigdelsetmask(&set, ~_BLOCKABLE);
  73. spin_lock_irq(&current->sighand->siglock);
  74. current->blocked = set;
  75. recalc_sigpending();
  76. spin_unlock_irq(&current->sighand->siglock);
  77. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  78. goto badframe;
  79. pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
  80. regs->pc, regs->lr, regs->sp);
  81. return regs->r12;
  82. badframe:
  83. force_sig(SIGSEGV, current);
  84. return 0;
  85. }
  86. static int
  87. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
  88. {
  89. int err = 0;
  90. #define COPY(x) err |= __put_user(regs->x, &sc->x)
  91. COPY(sr);
  92. COPY(pc);
  93. COPY(lr);
  94. COPY(sp);
  95. COPY(r12);
  96. COPY(r11);
  97. COPY(r10);
  98. COPY(r9);
  99. COPY(r8);
  100. COPY(r7);
  101. COPY(r6);
  102. COPY(r5);
  103. COPY(r4);
  104. COPY(r3);
  105. COPY(r2);
  106. COPY(r1);
  107. COPY(r0);
  108. #undef COPY
  109. return err;
  110. }
  111. static inline void __user *
  112. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
  113. {
  114. unsigned long sp = regs->sp;
  115. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
  116. sp = current->sas_ss_sp + current->sas_ss_size;
  117. return (void __user *)((sp - framesize) & ~3);
  118. }
  119. static int
  120. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  121. sigset_t *set, struct pt_regs *regs)
  122. {
  123. struct rt_sigframe __user *frame;
  124. int err = 0;
  125. frame = get_sigframe(ka, regs, sizeof(*frame));
  126. err = -EFAULT;
  127. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  128. goto out;
  129. /*
  130. * Set up the return code:
  131. *
  132. * mov r8, __NR_rt_sigreturn
  133. * scall
  134. *
  135. * Note: This will blow up since we're using a non-executable
  136. * stack. Better use SA_RESTORER.
  137. */
  138. #if __NR_rt_sigreturn > 127
  139. # error __NR_rt_sigreturn must be < 127 to fit in a short mov
  140. #endif
  141. err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
  142. &frame->retcode);
  143. err |= copy_siginfo_to_user(&frame->info, info);
  144. /* Set up the ucontext */
  145. err |= __put_user(0, &frame->uc.uc_flags);
  146. err |= __put_user(NULL, &frame->uc.uc_link);
  147. err |= __put_user((void __user *)current->sas_ss_sp,
  148. &frame->uc.uc_stack.ss_sp);
  149. err |= __put_user(sas_ss_flags(regs->sp),
  150. &frame->uc.uc_stack.ss_flags);
  151. err |= __put_user(current->sas_ss_size,
  152. &frame->uc.uc_stack.ss_size);
  153. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
  154. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  155. if (err)
  156. goto out;
  157. regs->r12 = sig;
  158. regs->r11 = (unsigned long) &frame->info;
  159. regs->r10 = (unsigned long) &frame->uc;
  160. regs->sp = (unsigned long) frame;
  161. if (ka->sa.sa_flags & SA_RESTORER)
  162. regs->lr = (unsigned long)ka->sa.sa_restorer;
  163. else {
  164. printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
  165. current->comm, current->pid);
  166. regs->lr = (unsigned long) &frame->retcode;
  167. }
  168. pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
  169. current->comm, current->pid, sig, regs->sp,
  170. regs->pc, ka->sa.sa_handler, regs->lr);
  171. regs->pc = (unsigned long) ka->sa.sa_handler;
  172. out:
  173. return err;
  174. }
  175. static inline void restart_syscall(struct pt_regs *regs)
  176. {
  177. if (regs->r12 == -ERESTART_RESTARTBLOCK)
  178. regs->r8 = __NR_restart_syscall;
  179. else
  180. regs->r12 = regs->r12_orig;
  181. regs->pc -= 2;
  182. }
  183. static inline void
  184. handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
  185. sigset_t *oldset, struct pt_regs *regs, int syscall)
  186. {
  187. int ret;
  188. /*
  189. * Set up the stack frame
  190. */
  191. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  192. /*
  193. * Check that the resulting registers are sane
  194. */
  195. ret |= !valid_user_regs(regs);
  196. /*
  197. * Block the signal if we were unsuccessful.
  198. */
  199. if (ret != 0 || !(ka->sa.sa_flags & SA_NODEFER)) {
  200. spin_lock_irq(&current->sighand->siglock);
  201. sigorsets(&current->blocked, &current->blocked,
  202. &ka->sa.sa_mask);
  203. sigaddset(&current->blocked, sig);
  204. recalc_sigpending();
  205. spin_unlock_irq(&current->sighand->siglock);
  206. }
  207. if (ret == 0)
  208. return;
  209. force_sigsegv(sig, current);
  210. }
  211. /*
  212. * Note that 'init' is a special process: it doesn't get signals it
  213. * doesn't want to handle. Thus you cannot kill init even with a
  214. * SIGKILL even by mistake.
  215. */
  216. int do_signal(struct pt_regs *regs, sigset_t *oldset, int syscall)
  217. {
  218. siginfo_t info;
  219. int signr;
  220. struct k_sigaction ka;
  221. /*
  222. * We want the common case to go fast, which is why we may in
  223. * certain cases get here from kernel mode. Just return
  224. * without doing anything if so.
  225. */
  226. if (!user_mode(regs))
  227. return 0;
  228. if (try_to_freeze()) {
  229. signr = 0;
  230. if (!signal_pending(current))
  231. goto no_signal;
  232. }
  233. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  234. oldset = &current->saved_sigmask;
  235. else if (!oldset)
  236. oldset = &current->blocked;
  237. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  238. no_signal:
  239. if (syscall) {
  240. switch (regs->r12) {
  241. case -ERESTART_RESTARTBLOCK:
  242. case -ERESTARTNOHAND:
  243. if (signr > 0) {
  244. regs->r12 = -EINTR;
  245. break;
  246. }
  247. /* fall through */
  248. case -ERESTARTSYS:
  249. if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
  250. regs->r12 = -EINTR;
  251. break;
  252. }
  253. /* fall through */
  254. case -ERESTARTNOINTR:
  255. restart_syscall(regs);
  256. }
  257. }
  258. if (signr == 0) {
  259. /* No signal to deliver -- put the saved sigmask back */
  260. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  261. clear_thread_flag(TIF_RESTORE_SIGMASK);
  262. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  263. }
  264. return 0;
  265. }
  266. handle_signal(signr, &ka, &info, oldset, regs, syscall);
  267. return 1;
  268. }
  269. asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
  270. {
  271. int syscall = 0;
  272. if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
  273. syscall = 1;
  274. if (ti->flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
  275. do_signal(regs, &current->blocked, syscall);
  276. }