signal.c 9.7 KB

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