signal.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_thread_info()->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 k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
  126. {
  127. /* Default to using normal stack */
  128. unsigned long sp = regs->r1;
  129. if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
  130. sp = current->sas_ss_sp + current->sas_ss_size;
  131. return (void __user *)((sp - frame_size) & -8UL);
  132. }
  133. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  134. sigset_t *set, struct pt_regs *regs)
  135. {
  136. struct rt_sigframe __user *frame;
  137. int err = 0;
  138. int signal;
  139. unsigned long address = 0;
  140. #ifdef CONFIG_MMU
  141. pmd_t *pmdp;
  142. pte_t *ptep;
  143. #endif
  144. frame = get_sigframe(ka, regs, sizeof(*frame));
  145. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  146. goto give_sigsegv;
  147. signal = current_thread_info()->exec_domain
  148. && current_thread_info()->exec_domain->signal_invmap
  149. && sig < 32
  150. ? current_thread_info()->exec_domain->signal_invmap[sig]
  151. : sig;
  152. if (info)
  153. err |= copy_siginfo_to_user(&frame->info, info);
  154. /* Create the ucontext. */
  155. err |= __put_user(0, &frame->uc.uc_flags);
  156. err |= __put_user(NULL, &frame->uc.uc_link);
  157. err |= __save_altstack(&frame->uc.uc_stack, regs->r1);
  158. err |= setup_sigcontext(&frame->uc.uc_mcontext,
  159. regs, set->sig[0]);
  160. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  161. /* Set up to return from userspace. If provided, use a stub
  162. already in userspace. */
  163. /* minus 8 is offset to cater for "rtsd r15,8" */
  164. /* addi r12, r0, __NR_sigreturn */
  165. err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
  166. frame->tramp + 0);
  167. /* brki r14, 0x8 */
  168. err |= __put_user(0xb9cc0008, frame->tramp + 1);
  169. /* Return from sighandler will jump to the tramp.
  170. Negative 8 offset because return is rtsd r15, 8 */
  171. regs->r15 = ((unsigned long)frame->tramp)-8;
  172. address = ((unsigned long)frame->tramp);
  173. #ifdef CONFIG_MMU
  174. pmdp = pmd_offset(pud_offset(
  175. pgd_offset(current->mm, address),
  176. address), address);
  177. preempt_disable();
  178. ptep = pte_offset_map(pmdp, address);
  179. if (pte_present(*ptep)) {
  180. address = (unsigned long) page_address(pte_page(*ptep));
  181. /* MS: I need add offset in page */
  182. address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
  183. /* MS address is virtual */
  184. address = virt_to_phys(address);
  185. invalidate_icache_range(address, address + 8);
  186. flush_dcache_range(address, address + 8);
  187. }
  188. pte_unmap(ptep);
  189. preempt_enable();
  190. #else
  191. flush_icache_range(address, address + 8);
  192. flush_dcache_range(address, address + 8);
  193. #endif
  194. if (err)
  195. goto give_sigsegv;
  196. /* Set up registers for signal handler */
  197. regs->r1 = (unsigned long) frame;
  198. /* Signal handler args: */
  199. regs->r5 = signal; /* arg 0: signum */
  200. regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
  201. regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
  202. /* Offset to handle microblaze rtid r14, 0 */
  203. regs->pc = (unsigned long)ka->sa.sa_handler;
  204. set_fs(USER_DS);
  205. #ifdef DEBUG_SIG
  206. pr_info("SIG deliver (%s:%d): sp=%p pc=%08lx\n",
  207. current->comm, current->pid, frame, regs->pc);
  208. #endif
  209. return 0;
  210. give_sigsegv:
  211. force_sigsegv(sig, current);
  212. return -EFAULT;
  213. }
  214. /* Handle restarting system calls */
  215. static inline void
  216. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  217. {
  218. switch (regs->r3) {
  219. case -ERESTART_RESTARTBLOCK:
  220. case -ERESTARTNOHAND:
  221. if (!has_handler)
  222. goto do_restart;
  223. regs->r3 = -EINTR;
  224. break;
  225. case -ERESTARTSYS:
  226. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  227. regs->r3 = -EINTR;
  228. break;
  229. }
  230. /* fallthrough */
  231. case -ERESTARTNOINTR:
  232. do_restart:
  233. /* offset of 4 bytes to re-execute trap (brki) instruction */
  234. regs->pc -= 4;
  235. break;
  236. }
  237. }
  238. /*
  239. * OK, we're invoking a handler
  240. */
  241. static void
  242. handle_signal(unsigned long sig, struct k_sigaction *ka,
  243. siginfo_t *info, struct pt_regs *regs)
  244. {
  245. sigset_t *oldset = sigmask_to_save();
  246. int ret;
  247. /* Set up the stack frame */
  248. if (ka->sa.sa_flags & SA_SIGINFO)
  249. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  250. else
  251. ret = setup_rt_frame(sig, ka, NULL, oldset, regs);
  252. if (ret)
  253. return;
  254. signal_delivered(sig, info, ka, regs,
  255. test_thread_flag(TIF_SINGLESTEP));
  256. }
  257. /*
  258. * Note that 'init' is a special process: it doesn't get signals it doesn't
  259. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  260. * mistake.
  261. *
  262. * Note that we go through the signals twice: once to check the signals that
  263. * the kernel can handle, and then we build all the user-level signal handling
  264. * stack-frames in one go after that.
  265. */
  266. static void do_signal(struct pt_regs *regs, int in_syscall)
  267. {
  268. siginfo_t info;
  269. int signr;
  270. struct k_sigaction ka;
  271. #ifdef DEBUG_SIG
  272. pr_info("do signal: %p %d\n", regs, in_syscall);
  273. pr_info("do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
  274. regs->r12, current_thread_info()->flags);
  275. #endif
  276. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  277. if (signr > 0) {
  278. /* Whee! Actually deliver the signal. */
  279. if (in_syscall)
  280. handle_restart(regs, &ka, 1);
  281. handle_signal(signr, &ka, &info, regs);
  282. return;
  283. }
  284. if (in_syscall)
  285. handle_restart(regs, NULL, 0);
  286. /*
  287. * If there's no signal to deliver, we just put the saved sigmask
  288. * back.
  289. */
  290. restore_saved_sigmask();
  291. }
  292. asmlinkage void do_notify_resume(struct pt_regs *regs, int in_syscall)
  293. {
  294. if (test_thread_flag(TIF_SIGPENDING))
  295. do_signal(regs, in_syscall);
  296. if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
  297. tracehook_notify_resume(regs);
  298. }