signal.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Signal handling
  3. *
  4. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2008-2009 PetaLogix
  6. * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au>
  7. * Copyright (C) 2001 NEC Corporation
  8. * Copyright (C) 2001 Miles Bader <miles@gnu.org>
  9. * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima
  10. * Copyright (C) 1991,1992 Linus Torvalds
  11. *
  12. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  13. *
  14. * This file was was derived from the sh version, arch/sh/kernel/signal.c
  15. *
  16. * This file is subject to the terms and conditions of the GNU General
  17. * Public License. See the file COPYING in the main directory of this
  18. * archive for more details.
  19. */
  20. #include <linux/sched.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/kernel.h>
  24. #include <linux/signal.h>
  25. #include <linux/errno.h>
  26. #include <linux/wait.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/unistd.h>
  29. #include <linux/stddef.h>
  30. #include <linux/personality.h>
  31. #include <linux/percpu.h>
  32. #include <linux/linkage.h>
  33. #include <linux/tracehook.h>
  34. #include <asm/entry.h>
  35. #include <asm/ucontext.h>
  36. #include <linux/uaccess.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/pgalloc.h>
  39. #include <linux/syscalls.h>
  40. #include <asm/cacheflush.h>
  41. #include <asm/syscalls.h>
  42. /*
  43. * Do a signal return; undo the signal stack.
  44. */
  45. struct sigframe {
  46. struct sigcontext sc;
  47. unsigned long extramask[_NSIG_WORDS-1];
  48. unsigned long tramp[2]; /* signal trampoline */
  49. };
  50. struct rt_sigframe {
  51. struct siginfo info;
  52. struct ucontext uc;
  53. unsigned long tramp[2]; /* signal trampoline */
  54. };
  55. static int restore_sigcontext(struct pt_regs *regs,
  56. struct sigcontext __user *sc, int *rval_p)
  57. {
  58. unsigned int err = 0;
  59. #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
  60. COPY(r0);
  61. COPY(r1);
  62. COPY(r2); COPY(r3); COPY(r4); COPY(r5);
  63. COPY(r6); COPY(r7); COPY(r8); COPY(r9);
  64. COPY(r10); COPY(r11); COPY(r12); COPY(r13);
  65. COPY(r14); COPY(r15); COPY(r16); COPY(r17);
  66. COPY(r18); COPY(r19); COPY(r20); COPY(r21);
  67. COPY(r22); COPY(r23); COPY(r24); COPY(r25);
  68. COPY(r26); COPY(r27); COPY(r28); COPY(r29);
  69. COPY(r30); COPY(r31);
  70. COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
  71. #undef COPY
  72. *rval_p = regs->r3;
  73. return err;
  74. }
  75. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  76. {
  77. struct rt_sigframe __user *frame =
  78. (struct rt_sigframe __user *)(regs->r1);
  79. sigset_t set;
  80. int rval;
  81. /* Always make any pending restarted system calls return -EINTR */
  82. current->restart_block.fn = do_no_restart_syscall;
  83. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  84. goto badframe;
  85. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  86. goto badframe;
  87. set_current_blocked(&set);
  88. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
  89. goto badframe;
  90. if (restore_altstack(&frame->uc.uc_stack))
  91. goto badframe;
  92. return rval;
  93. badframe:
  94. force_sig(SIGSEGV, current);
  95. return 0;
  96. }
  97. /*
  98. * Set up a signal frame.
  99. */
  100. static int
  101. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  102. unsigned long mask)
  103. {
  104. int err = 0;
  105. #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
  106. COPY(r0);
  107. COPY(r1);
  108. COPY(r2); COPY(r3); COPY(r4); COPY(r5);
  109. COPY(r6); COPY(r7); COPY(r8); COPY(r9);
  110. COPY(r10); COPY(r11); COPY(r12); COPY(r13);
  111. COPY(r14); COPY(r15); COPY(r16); COPY(r17);
  112. COPY(r18); COPY(r19); COPY(r20); COPY(r21);
  113. COPY(r22); COPY(r23); COPY(r24); COPY(r25);
  114. COPY(r26); COPY(r27); COPY(r28); COPY(r29);
  115. COPY(r30); COPY(r31);
  116. COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
  117. #undef COPY
  118. err |= __put_user(mask, &sc->oldmask);
  119. return err;
  120. }
  121. /*
  122. * Determine which stack to use..
  123. */
  124. static inline void __user *
  125. get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size)
  126. {
  127. /* Default to using normal stack */
  128. unsigned long sp = sigsp(regs->r1, ksig);
  129. return (void __user *)((sp - frame_size) & -8UL);
  130. }
  131. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  132. struct pt_regs *regs)
  133. {
  134. struct rt_sigframe __user *frame;
  135. int err = 0, sig = ksig->sig;
  136. unsigned long address = 0;
  137. #ifdef CONFIG_MMU
  138. pmd_t *pmdp;
  139. pte_t *ptep;
  140. #endif
  141. frame = get_sigframe(ksig, regs, sizeof(*frame));
  142. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  143. return -EFAULT;
  144. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  145. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  146. /* Create the ucontext. */
  147. err |= __put_user(0, &frame->uc.uc_flags);
  148. err |= __put_user(NULL, &frame->uc.uc_link);
  149. err |= __save_altstack(&frame->uc.uc_stack, regs->r1);
  150. err |= setup_sigcontext(&frame->uc.uc_mcontext,
  151. regs, set->sig[0]);
  152. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  153. /* Set up to return from userspace. If provided, use a stub
  154. already in userspace. */
  155. /* minus 8 is offset to cater for "rtsd r15,8" */
  156. /* addi r12, r0, __NR_sigreturn */
  157. err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
  158. frame->tramp + 0);
  159. /* brki r14, 0x8 */
  160. err |= __put_user(0xb9cc0008, frame->tramp + 1);
  161. /* Return from sighandler will jump to the tramp.
  162. Negative 8 offset because return is rtsd r15, 8 */
  163. regs->r15 = ((unsigned long)frame->tramp)-8;
  164. address = ((unsigned long)frame->tramp);
  165. #ifdef CONFIG_MMU
  166. pmdp = pmd_offset(pud_offset(
  167. pgd_offset(current->mm, address),
  168. address), address);
  169. preempt_disable();
  170. ptep = pte_offset_map(pmdp, address);
  171. if (pte_present(*ptep)) {
  172. address = (unsigned long) page_address(pte_page(*ptep));
  173. /* MS: I need add offset in page */
  174. address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
  175. /* MS address is virtual */
  176. address = __virt_to_phys(address);
  177. invalidate_icache_range(address, address + 8);
  178. flush_dcache_range(address, address + 8);
  179. }
  180. pte_unmap(ptep);
  181. preempt_enable();
  182. #else
  183. flush_icache_range(address, address + 8);
  184. flush_dcache_range(address, address + 8);
  185. #endif
  186. if (err)
  187. return -EFAULT;
  188. /* Set up registers for signal handler */
  189. regs->r1 = (unsigned long) frame;
  190. /* Signal handler args: */
  191. regs->r5 = sig; /* arg 0: signum */
  192. regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
  193. regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
  194. /* Offset to handle microblaze rtid r14, 0 */
  195. regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
  196. #ifdef DEBUG_SIG
  197. pr_info("SIG deliver (%s:%d): sp=%p pc=%08lx\n",
  198. current->comm, current->pid, frame, regs->pc);
  199. #endif
  200. return 0;
  201. }
  202. /* Handle restarting system calls */
  203. static inline void
  204. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  205. {
  206. switch (regs->r3) {
  207. case -ERESTART_RESTARTBLOCK:
  208. case -ERESTARTNOHAND:
  209. if (!has_handler)
  210. goto do_restart;
  211. regs->r3 = -EINTR;
  212. break;
  213. case -ERESTARTSYS:
  214. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  215. regs->r3 = -EINTR;
  216. break;
  217. }
  218. /* fallthrough */
  219. case -ERESTARTNOINTR:
  220. do_restart:
  221. /* offset of 4 bytes to re-execute trap (brki) instruction */
  222. regs->pc -= 4;
  223. break;
  224. }
  225. }
  226. /*
  227. * OK, we're invoking a handler
  228. */
  229. static void
  230. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  231. {
  232. sigset_t *oldset = sigmask_to_save();
  233. int ret;
  234. /* Set up the stack frame */
  235. ret = setup_rt_frame(ksig, oldset, regs);
  236. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  237. }
  238. /*
  239. * Note that 'init' is a special process: it doesn't get signals it doesn't
  240. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  241. * mistake.
  242. *
  243. * Note that we go through the signals twice: once to check the signals that
  244. * the kernel can handle, and then we build all the user-level signal handling
  245. * stack-frames in one go after that.
  246. */
  247. static void do_signal(struct pt_regs *regs, int in_syscall)
  248. {
  249. struct ksignal ksig;
  250. #ifdef DEBUG_SIG
  251. pr_info("do signal: %p %d\n", regs, in_syscall);
  252. pr_info("do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
  253. regs->r12, current_thread_info()->flags);
  254. #endif
  255. if (get_signal(&ksig)) {
  256. /* Whee! Actually deliver the signal. */
  257. if (in_syscall)
  258. handle_restart(regs, &ksig.ka, 1);
  259. handle_signal(&ksig, regs);
  260. return;
  261. }
  262. if (in_syscall)
  263. handle_restart(regs, NULL, 0);
  264. /*
  265. * If there's no signal to deliver, we just put the saved sigmask
  266. * back.
  267. */
  268. restore_saved_sigmask();
  269. }
  270. asmlinkage void do_notify_resume(struct pt_regs *regs, int in_syscall)
  271. {
  272. if (test_thread_flag(TIF_SIGPENDING))
  273. do_signal(regs, in_syscall);
  274. if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
  275. tracehook_notify_resume(regs);
  276. }