signal.c 7.4 KB

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