signal.c 7.2 KB

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