signal.c 9.7 KB

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