signal.c 7.4 KB

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