signal.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Port on Texas Instruments TMS320C6x architecture
  3. *
  4. * Copyright (C) 2004, 2006, 2009, 2010, 2011 Texas Instruments Incorporated
  5. * Author: Aurelien Jacquiot (aurelien.jacquiot@jaluna.com)
  6. *
  7. * Updated for 2.6.34: Mark Salter <msalter@redhat.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/tracehook.h>
  17. #include <asm/ucontext.h>
  18. #include <asm/cacheflush.h>
  19. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  20. /*
  21. * Do a signal return, undo the signal stack.
  22. */
  23. #define RETCODE_SIZE (9 << 2) /* 9 instructions = 36 bytes */
  24. struct rt_sigframe {
  25. struct siginfo __user *pinfo;
  26. void __user *puc;
  27. struct siginfo info;
  28. struct ucontext uc;
  29. unsigned long retcode[RETCODE_SIZE >> 2];
  30. };
  31. static int restore_sigcontext(struct pt_regs *regs,
  32. struct sigcontext __user *sc)
  33. {
  34. int err = 0;
  35. /* The access_ok check was done by caller, so use __get_user here */
  36. #define COPY(x) (err |= __get_user(regs->x, &sc->sc_##x))
  37. COPY(sp); COPY(a4); COPY(b4); COPY(a6); COPY(b6); COPY(a8); COPY(b8);
  38. COPY(a0); COPY(a1); COPY(a2); COPY(a3); COPY(a5); COPY(a7); COPY(a9);
  39. COPY(b0); COPY(b1); COPY(b2); COPY(b3); COPY(b5); COPY(b7); COPY(b9);
  40. COPY(a16); COPY(a17); COPY(a18); COPY(a19);
  41. COPY(a20); COPY(a21); COPY(a22); COPY(a23);
  42. COPY(a24); COPY(a25); COPY(a26); COPY(a27);
  43. COPY(a28); COPY(a29); COPY(a30); COPY(a31);
  44. COPY(b16); COPY(b17); COPY(b18); COPY(b19);
  45. COPY(b20); COPY(b21); COPY(b22); COPY(b23);
  46. COPY(b24); COPY(b25); COPY(b26); COPY(b27);
  47. COPY(b28); COPY(b29); COPY(b30); COPY(b31);
  48. COPY(csr); COPY(pc);
  49. #undef COPY
  50. return err;
  51. }
  52. asmlinkage int do_rt_sigreturn(struct pt_regs *regs)
  53. {
  54. struct rt_sigframe __user *frame;
  55. sigset_t set;
  56. /* Always make any pending restarted system calls return -EINTR */
  57. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  58. /*
  59. * Since we stacked the signal on a dword boundary,
  60. * 'sp' should be dword aligned here. If it's
  61. * not, then the user is trying to mess with us.
  62. */
  63. if (regs->sp & 7)
  64. goto badframe;
  65. frame = (struct rt_sigframe __user *) ((unsigned long) regs->sp + 8);
  66. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  67. goto badframe;
  68. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  69. goto badframe;
  70. sigdelsetmask(&set, ~_BLOCKABLE);
  71. set_current_blocked(&set);
  72. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  73. goto badframe;
  74. return regs->a4;
  75. badframe:
  76. force_sig(SIGSEGV, current);
  77. return 0;
  78. }
  79. static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  80. unsigned long mask)
  81. {
  82. int err = 0;
  83. err |= __put_user(mask, &sc->sc_mask);
  84. /* The access_ok check was done by caller, so use __put_user here */
  85. #define COPY(x) (err |= __put_user(regs->x, &sc->sc_##x))
  86. COPY(sp); COPY(a4); COPY(b4); COPY(a6); COPY(b6); COPY(a8); COPY(b8);
  87. COPY(a0); COPY(a1); COPY(a2); COPY(a3); COPY(a5); COPY(a7); COPY(a9);
  88. COPY(b0); COPY(b1); COPY(b2); COPY(b3); COPY(b5); COPY(b7); COPY(b9);
  89. COPY(a16); COPY(a17); COPY(a18); COPY(a19);
  90. COPY(a20); COPY(a21); COPY(a22); COPY(a23);
  91. COPY(a24); COPY(a25); COPY(a26); COPY(a27);
  92. COPY(a28); COPY(a29); COPY(a30); COPY(a31);
  93. COPY(b16); COPY(b17); COPY(b18); COPY(b19);
  94. COPY(b20); COPY(b21); COPY(b22); COPY(b23);
  95. COPY(b24); COPY(b25); COPY(b26); COPY(b27);
  96. COPY(b28); COPY(b29); COPY(b30); COPY(b31);
  97. COPY(csr); COPY(pc);
  98. #undef COPY
  99. return err;
  100. }
  101. static inline void __user *get_sigframe(struct k_sigaction *ka,
  102. struct pt_regs *regs,
  103. unsigned long framesize)
  104. {
  105. unsigned long sp = regs->sp;
  106. /*
  107. * This is the X/Open sanctioned signal stack switching.
  108. */
  109. if ((ka->sa.sa_flags & SA_ONSTACK) && sas_ss_flags(sp) == 0)
  110. sp = current->sas_ss_sp + current->sas_ss_size;
  111. /*
  112. * No matter what happens, 'sp' must be dword
  113. * aligned. Otherwise, nasty things will happen
  114. */
  115. return (void __user *)((sp - framesize) & ~7);
  116. }
  117. static int setup_rt_frame(int signr, struct k_sigaction *ka, siginfo_t *info,
  118. sigset_t *set, struct pt_regs *regs)
  119. {
  120. struct rt_sigframe __user *frame;
  121. unsigned long __user *retcode;
  122. int err = 0;
  123. frame = get_sigframe(ka, regs, sizeof(*frame));
  124. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  125. goto segv_and_exit;
  126. err |= __put_user(&frame->info, &frame->pinfo);
  127. err |= __put_user(&frame->uc, &frame->puc);
  128. err |= copy_siginfo_to_user(&frame->info, info);
  129. /* Clear all the bits of the ucontext we don't use. */
  130. err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
  131. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  132. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  133. /* Set up to return from userspace */
  134. retcode = (unsigned long __user *) &frame->retcode;
  135. /* The access_ok check was done above, so use __put_user here */
  136. #define COPY(x) (err |= __put_user(x, retcode++))
  137. COPY(0x0000002AUL | (__NR_rt_sigreturn << 7));
  138. /* MVK __NR_rt_sigreturn,B0 */
  139. COPY(0x10000000UL); /* SWE */
  140. COPY(0x00006000UL); /* NOP 4 */
  141. COPY(0x00006000UL); /* NOP 4 */
  142. COPY(0x00006000UL); /* NOP 4 */
  143. COPY(0x00006000UL); /* NOP 4 */
  144. COPY(0x00006000UL); /* NOP 4 */
  145. COPY(0x00006000UL); /* NOP 4 */
  146. COPY(0x00006000UL); /* NOP 4 */
  147. #undef COPY
  148. if (err)
  149. goto segv_and_exit;
  150. flush_icache_range((unsigned long) &frame->retcode,
  151. (unsigned long) &frame->retcode + RETCODE_SIZE);
  152. retcode = (unsigned long __user *) &frame->retcode;
  153. /* Change user context to branch to signal handler */
  154. regs->sp = (unsigned long) frame - 8;
  155. regs->b3 = (unsigned long) retcode;
  156. regs->pc = (unsigned long) ka->sa.sa_handler;
  157. /* Give the signal number to the handler */
  158. regs->a4 = signr;
  159. /*
  160. * For realtime signals we must also set the second and third
  161. * arguments for the signal handler.
  162. * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
  163. */
  164. regs->b4 = (unsigned long)&frame->info;
  165. regs->a6 = (unsigned long)&frame->uc;
  166. return 0;
  167. segv_and_exit:
  168. force_sigsegv(signr, current);
  169. return -EFAULT;
  170. }
  171. static inline void
  172. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  173. {
  174. switch (regs->a4) {
  175. case -ERESTARTNOHAND:
  176. if (!has_handler)
  177. goto do_restart;
  178. regs->a4 = -EINTR;
  179. break;
  180. case -ERESTARTSYS:
  181. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  182. regs->a4 = -EINTR;
  183. break;
  184. }
  185. /* fallthrough */
  186. case -ERESTARTNOINTR:
  187. do_restart:
  188. regs->a4 = regs->orig_a4;
  189. regs->pc -= 4;
  190. break;
  191. }
  192. }
  193. /*
  194. * handle the actual delivery of a signal to userspace
  195. */
  196. static int handle_signal(int sig,
  197. siginfo_t *info, struct k_sigaction *ka,
  198. sigset_t *oldset, struct pt_regs *regs,
  199. int syscall)
  200. {
  201. int ret;
  202. /* Are we from a system call? */
  203. if (syscall) {
  204. /* If so, check system call restarting.. */
  205. switch (regs->a4) {
  206. case -ERESTART_RESTARTBLOCK:
  207. case -ERESTARTNOHAND:
  208. regs->a4 = -EINTR;
  209. break;
  210. case -ERESTARTSYS:
  211. if (!(ka->sa.sa_flags & SA_RESTART)) {
  212. regs->a4 = -EINTR;
  213. break;
  214. }
  215. /* fallthrough */
  216. case -ERESTARTNOINTR:
  217. regs->a4 = regs->orig_a4;
  218. regs->pc -= 4;
  219. }
  220. }
  221. /* Set up the stack frame */
  222. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  223. if (ret == 0)
  224. block_sigmask(ka, sig);
  225. return ret;
  226. }
  227. /*
  228. * handle a potential signal
  229. */
  230. static void do_signal(struct pt_regs *regs, int syscall)
  231. {
  232. struct k_sigaction ka;
  233. siginfo_t info;
  234. sigset_t *oldset;
  235. int signr;
  236. /* we want the common case to go fast, which is why we may in certain
  237. * cases get here from kernel mode */
  238. if (!user_mode(regs))
  239. return;
  240. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  241. oldset = &current->saved_sigmask;
  242. else
  243. oldset = &current->blocked;
  244. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  245. if (signr > 0) {
  246. if (handle_signal(signr, &info, &ka, oldset,
  247. regs, syscall) == 0) {
  248. /* a signal was successfully delivered; the saved
  249. * sigmask will have been stored in the signal frame,
  250. * and will be restored by sigreturn, so we can simply
  251. * clear the TIF_RESTORE_SIGMASK flag */
  252. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  253. clear_thread_flag(TIF_RESTORE_SIGMASK);
  254. tracehook_signal_handler(signr, &info, &ka, regs, 0);
  255. }
  256. return;
  257. }
  258. /* did we come from a system call? */
  259. if (syscall) {
  260. /* restart the system call - no handlers present */
  261. switch (regs->a4) {
  262. case -ERESTARTNOHAND:
  263. case -ERESTARTSYS:
  264. case -ERESTARTNOINTR:
  265. regs->a4 = regs->orig_a4;
  266. regs->pc -= 4;
  267. break;
  268. case -ERESTART_RESTARTBLOCK:
  269. regs->a4 = regs->orig_a4;
  270. regs->b0 = __NR_restart_syscall;
  271. regs->pc -= 4;
  272. break;
  273. }
  274. }
  275. /* if there's no signal to deliver, we just put the saved sigmask
  276. * back */
  277. restore_saved_sigmask();
  278. }
  279. /*
  280. * notification of userspace execution resumption
  281. * - triggered by current->work.notify_resume
  282. */
  283. asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags,
  284. int syscall)
  285. {
  286. /* deal with pending signal delivery */
  287. if (thread_info_flags & ((1 << TIF_SIGPENDING) |
  288. (1 << TIF_RESTORE_SIGMASK)))
  289. do_signal(regs, syscall);
  290. if (thread_info_flags & (1 << TIF_NOTIFY_RESUME)) {
  291. clear_thread_flag(TIF_NOTIFY_RESUME);
  292. tracehook_notify_resume(regs);
  293. }
  294. }