signal.c 9.2 KB

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