signal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  43. asmlinkage long
  44. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  45. struct pt_regs *regs)
  46. {
  47. return do_sigaltstack(uss, uoss, regs->r1);
  48. }
  49. /*
  50. * Do a signal return; undo the signal stack.
  51. */
  52. struct sigframe {
  53. struct sigcontext sc;
  54. unsigned long extramask[_NSIG_WORDS-1];
  55. unsigned long tramp[2]; /* signal trampoline */
  56. };
  57. struct rt_sigframe {
  58. struct siginfo info;
  59. struct ucontext uc;
  60. unsigned long tramp[2]; /* signal trampoline */
  61. };
  62. static int restore_sigcontext(struct pt_regs *regs,
  63. struct sigcontext __user *sc, int *rval_p)
  64. {
  65. unsigned int err = 0;
  66. #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
  67. COPY(r0);
  68. COPY(r1);
  69. COPY(r2); COPY(r3); COPY(r4); COPY(r5);
  70. COPY(r6); COPY(r7); COPY(r8); COPY(r9);
  71. COPY(r10); COPY(r11); COPY(r12); COPY(r13);
  72. COPY(r14); COPY(r15); COPY(r16); COPY(r17);
  73. COPY(r18); COPY(r19); COPY(r20); COPY(r21);
  74. COPY(r22); COPY(r23); COPY(r24); COPY(r25);
  75. COPY(r26); COPY(r27); COPY(r28); COPY(r29);
  76. COPY(r30); COPY(r31);
  77. COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
  78. #undef COPY
  79. *rval_p = regs->r3;
  80. return err;
  81. }
  82. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  83. {
  84. struct rt_sigframe __user *frame =
  85. (struct rt_sigframe __user *)(regs->r1);
  86. sigset_t set;
  87. int rval;
  88. /* Always make any pending restarted system calls return -EINTR */
  89. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  90. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  91. goto badframe;
  92. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  93. goto badframe;
  94. sigdelsetmask(&set, ~_BLOCKABLE);
  95. set_current_blocked(&set);
  96. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
  97. goto badframe;
  98. /* It is more difficult to avoid calling this function than to
  99. call it and ignore errors. */
  100. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->r1))
  101. goto badframe;
  102. return rval;
  103. badframe:
  104. force_sig(SIGSEGV, current);
  105. return 0;
  106. }
  107. /*
  108. * Set up a signal frame.
  109. */
  110. static int
  111. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  112. unsigned long mask)
  113. {
  114. int err = 0;
  115. #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
  116. COPY(r0);
  117. COPY(r1);
  118. COPY(r2); COPY(r3); COPY(r4); COPY(r5);
  119. COPY(r6); COPY(r7); COPY(r8); COPY(r9);
  120. COPY(r10); COPY(r11); COPY(r12); COPY(r13);
  121. COPY(r14); COPY(r15); COPY(r16); COPY(r17);
  122. COPY(r18); COPY(r19); COPY(r20); COPY(r21);
  123. COPY(r22); COPY(r23); COPY(r24); COPY(r25);
  124. COPY(r26); COPY(r27); COPY(r28); COPY(r29);
  125. COPY(r30); COPY(r31);
  126. COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
  127. #undef COPY
  128. err |= __put_user(mask, &sc->oldmask);
  129. return err;
  130. }
  131. /*
  132. * Determine which stack to use..
  133. */
  134. static inline void __user *
  135. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
  136. {
  137. /* Default to using normal stack */
  138. unsigned long sp = regs->r1;
  139. if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
  140. sp = current->sas_ss_sp + current->sas_ss_size;
  141. return (void __user *)((sp - frame_size) & -8UL);
  142. }
  143. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  144. sigset_t *set, struct pt_regs *regs)
  145. {
  146. struct rt_sigframe __user *frame;
  147. int err = 0;
  148. int signal;
  149. unsigned long address = 0;
  150. #ifdef CONFIG_MMU
  151. pmd_t *pmdp;
  152. pte_t *ptep;
  153. #endif
  154. frame = get_sigframe(ka, regs, sizeof(*frame));
  155. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  156. goto give_sigsegv;
  157. signal = current_thread_info()->exec_domain
  158. && current_thread_info()->exec_domain->signal_invmap
  159. && sig < 32
  160. ? current_thread_info()->exec_domain->signal_invmap[sig]
  161. : sig;
  162. if (info)
  163. err |= copy_siginfo_to_user(&frame->info, info);
  164. /* Create the ucontext. */
  165. err |= __put_user(0, &frame->uc.uc_flags);
  166. err |= __put_user(NULL, &frame->uc.uc_link);
  167. err |= __put_user((void __user *)current->sas_ss_sp,
  168. &frame->uc.uc_stack.ss_sp);
  169. err |= __put_user(sas_ss_flags(regs->r1),
  170. &frame->uc.uc_stack.ss_flags);
  171. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  172. err |= setup_sigcontext(&frame->uc.uc_mcontext,
  173. regs, set->sig[0]);
  174. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  175. /* Set up to return from userspace. If provided, use a stub
  176. already in userspace. */
  177. /* minus 8 is offset to cater for "rtsd r15,8" */
  178. /* addi r12, r0, __NR_sigreturn */
  179. err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
  180. frame->tramp + 0);
  181. /* brki r14, 0x8 */
  182. err |= __put_user(0xb9cc0008, frame->tramp + 1);
  183. /* Return from sighandler will jump to the tramp.
  184. Negative 8 offset because return is rtsd r15, 8 */
  185. regs->r15 = ((unsigned long)frame->tramp)-8;
  186. address = ((unsigned long)frame->tramp);
  187. #ifdef CONFIG_MMU
  188. pmdp = pmd_offset(pud_offset(
  189. pgd_offset(current->mm, address),
  190. address), address);
  191. preempt_disable();
  192. ptep = pte_offset_map(pmdp, address);
  193. if (pte_present(*ptep)) {
  194. address = (unsigned long) page_address(pte_page(*ptep));
  195. /* MS: I need add offset in page */
  196. address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
  197. /* MS address is virtual */
  198. address = virt_to_phys(address);
  199. invalidate_icache_range(address, address + 8);
  200. flush_dcache_range(address, address + 8);
  201. }
  202. pte_unmap(ptep);
  203. preempt_enable();
  204. #else
  205. flush_icache_range(address, address + 8);
  206. flush_dcache_range(address, address + 8);
  207. #endif
  208. if (err)
  209. goto give_sigsegv;
  210. /* Set up registers for signal handler */
  211. regs->r1 = (unsigned long) frame;
  212. /* Signal handler args: */
  213. regs->r5 = signal; /* arg 0: signum */
  214. regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
  215. regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
  216. /* Offset to handle microblaze rtid r14, 0 */
  217. regs->pc = (unsigned long)ka->sa.sa_handler;
  218. set_fs(USER_DS);
  219. /* the tracer may want to single-step inside the handler */
  220. if (test_thread_flag(TIF_SINGLESTEP))
  221. ptrace_notify(SIGTRAP);
  222. #ifdef DEBUG_SIG
  223. printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
  224. current->comm, current->pid, frame, regs->pc);
  225. #endif
  226. return 0;
  227. give_sigsegv:
  228. force_sigsegv(sig, current);
  229. return -EFAULT;
  230. }
  231. /* Handle restarting system calls */
  232. static inline void
  233. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  234. {
  235. switch (regs->r3) {
  236. case -ERESTART_RESTARTBLOCK:
  237. case -ERESTARTNOHAND:
  238. if (!has_handler)
  239. goto do_restart;
  240. regs->r3 = -EINTR;
  241. break;
  242. case -ERESTARTSYS:
  243. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  244. regs->r3 = -EINTR;
  245. break;
  246. }
  247. /* fallthrough */
  248. case -ERESTARTNOINTR:
  249. do_restart:
  250. /* offset of 4 bytes to re-execute trap (brki) instruction */
  251. #ifndef CONFIG_MMU
  252. regs->pc -= 4;
  253. #else
  254. /* offset of 8 bytes required = 4 for rtbd
  255. offset, plus 4 for size of
  256. "brki r14,8"
  257. instruction. */
  258. regs->pc -= 8;
  259. #endif
  260. break;
  261. }
  262. }
  263. /*
  264. * OK, we're invoking a handler
  265. */
  266. static int
  267. handle_signal(unsigned long sig, struct k_sigaction *ka,
  268. siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
  269. {
  270. int ret;
  271. /* Set up the stack frame */
  272. if (ka->sa.sa_flags & SA_SIGINFO)
  273. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  274. else
  275. ret = setup_rt_frame(sig, ka, NULL, oldset, regs);
  276. if (ret)
  277. return ret;
  278. block_sigmask(ka, sig);
  279. return 0;
  280. }
  281. /*
  282. * Note that 'init' is a special process: it doesn't get signals it doesn't
  283. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  284. * mistake.
  285. *
  286. * Note that we go through the signals twice: once to check the signals that
  287. * the kernel can handle, and then we build all the user-level signal handling
  288. * stack-frames in one go after that.
  289. */
  290. static void do_signal(struct pt_regs *regs, int in_syscall)
  291. {
  292. siginfo_t info;
  293. int signr;
  294. struct k_sigaction ka;
  295. sigset_t *oldset;
  296. #ifdef DEBUG_SIG
  297. printk(KERN_INFO "do signal: %p %d\n", regs, in_syscall);
  298. printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
  299. regs->r12, current_thread_info()->flags);
  300. #endif
  301. if (current_thread_info()->status & TS_RESTORE_SIGMASK)
  302. oldset = &current->saved_sigmask;
  303. else
  304. oldset = &current->blocked;
  305. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  306. if (signr > 0) {
  307. /* Whee! Actually deliver the signal. */
  308. if (in_syscall)
  309. handle_restart(regs, &ka, 1);
  310. if (!handle_signal(signr, &ka, &info, oldset, regs)) {
  311. /*
  312. * A signal was successfully delivered; the saved
  313. * sigmask will have been stored in the signal frame,
  314. * and will be restored by sigreturn, so we can simply
  315. * clear the TS_RESTORE_SIGMASK flag.
  316. */
  317. current_thread_info()->status &=
  318. ~TS_RESTORE_SIGMASK;
  319. }
  320. return;
  321. }
  322. if (in_syscall)
  323. handle_restart(regs, NULL, 0);
  324. /*
  325. * If there's no signal to deliver, we just put the saved sigmask
  326. * back.
  327. */
  328. restore_saved_sigmask();
  329. }
  330. void do_notify_resume(struct pt_regs *regs, int in_syscall)
  331. {
  332. /*
  333. * We want the common case to go fast, which
  334. * is why we may in certain cases get here from
  335. * kernel mode. Just return without doing anything
  336. * if so.
  337. */
  338. if (kernel_mode(regs))
  339. return;
  340. if (test_thread_flag(TIF_SIGPENDING))
  341. do_signal(regs, in_syscall);
  342. if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
  343. tracehook_notify_resume(regs);
  344. }