signal.c 6.0 KB

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