signal.c 10 KB

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