signal.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /*
  20. * Do a signal return, undo the signal stack.
  21. */
  22. #define RETCODE_SIZE (9 << 2) /* 9 instructions = 36 bytes */
  23. struct rt_sigframe {
  24. struct siginfo __user *pinfo;
  25. void __user *puc;
  26. struct siginfo info;
  27. struct ucontext uc;
  28. unsigned long retcode[RETCODE_SIZE >> 2];
  29. };
  30. static int restore_sigcontext(struct pt_regs *regs,
  31. struct sigcontext __user *sc)
  32. {
  33. int err = 0;
  34. /* The access_ok check was done by caller, so use __get_user here */
  35. #define COPY(x) (err |= __get_user(regs->x, &sc->sc_##x))
  36. COPY(sp); COPY(a4); COPY(b4); COPY(a6); COPY(b6); COPY(a8); COPY(b8);
  37. COPY(a0); COPY(a1); COPY(a2); COPY(a3); COPY(a5); COPY(a7); COPY(a9);
  38. COPY(b0); COPY(b1); COPY(b2); COPY(b3); COPY(b5); COPY(b7); COPY(b9);
  39. COPY(a16); COPY(a17); COPY(a18); COPY(a19);
  40. COPY(a20); COPY(a21); COPY(a22); COPY(a23);
  41. COPY(a24); COPY(a25); COPY(a26); COPY(a27);
  42. COPY(a28); COPY(a29); COPY(a30); COPY(a31);
  43. COPY(b16); COPY(b17); COPY(b18); COPY(b19);
  44. COPY(b20); COPY(b21); COPY(b22); COPY(b23);
  45. COPY(b24); COPY(b25); COPY(b26); COPY(b27);
  46. COPY(b28); COPY(b29); COPY(b30); COPY(b31);
  47. COPY(csr); COPY(pc);
  48. #undef COPY
  49. return err;
  50. }
  51. asmlinkage int do_rt_sigreturn(struct pt_regs *regs)
  52. {
  53. struct rt_sigframe __user *frame;
  54. sigset_t set;
  55. /* Always make any pending restarted system calls return -EINTR */
  56. current->restart_block.fn = do_no_restart_syscall;
  57. /*
  58. * Since we stacked the signal on a dword boundary,
  59. * 'sp' should be dword aligned here. If it's
  60. * not, then the user is trying to mess with us.
  61. */
  62. if (regs->sp & 7)
  63. goto badframe;
  64. frame = (struct rt_sigframe __user *) ((unsigned long) regs->sp + 8);
  65. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  66. goto badframe;
  67. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  68. goto badframe;
  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 ksignal *ksig,
  100. struct pt_regs *regs,
  101. unsigned long framesize)
  102. {
  103. unsigned long sp = sigsp(regs->sp, ksig);
  104. /*
  105. * No matter what happens, 'sp' must be dword
  106. * aligned. Otherwise, nasty things will happen
  107. */
  108. return (void __user *)((sp - framesize) & ~7);
  109. }
  110. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  111. struct pt_regs *regs)
  112. {
  113. struct rt_sigframe __user *frame;
  114. unsigned long __user *retcode;
  115. int err = 0;
  116. frame = get_sigframe(ksig, regs, sizeof(*frame));
  117. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  118. return -EFAULT;
  119. err |= __put_user(&frame->info, &frame->pinfo);
  120. err |= __put_user(&frame->uc, &frame->puc);
  121. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  122. /* Clear all the bits of the ucontext we don't use. */
  123. err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
  124. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  125. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  126. /* Set up to return from userspace */
  127. retcode = (unsigned long __user *) &frame->retcode;
  128. /* The access_ok check was done above, so use __put_user here */
  129. #define COPY(x) (err |= __put_user(x, retcode++))
  130. COPY(0x0000002AUL | (__NR_rt_sigreturn << 7));
  131. /* MVK __NR_rt_sigreturn,B0 */
  132. COPY(0x10000000UL); /* SWE */
  133. COPY(0x00006000UL); /* NOP 4 */
  134. COPY(0x00006000UL); /* NOP 4 */
  135. COPY(0x00006000UL); /* NOP 4 */
  136. COPY(0x00006000UL); /* NOP 4 */
  137. COPY(0x00006000UL); /* NOP 4 */
  138. COPY(0x00006000UL); /* NOP 4 */
  139. COPY(0x00006000UL); /* NOP 4 */
  140. #undef COPY
  141. if (err)
  142. return -EFAULT;
  143. flush_icache_range((unsigned long) &frame->retcode,
  144. (unsigned long) &frame->retcode + RETCODE_SIZE);
  145. retcode = (unsigned long __user *) &frame->retcode;
  146. /* Change user context to branch to signal handler */
  147. regs->sp = (unsigned long) frame - 8;
  148. regs->b3 = (unsigned long) retcode;
  149. regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  150. /* Give the signal number to the handler */
  151. regs->a4 = ksig->sig;
  152. /*
  153. * For realtime signals we must also set the second and third
  154. * arguments for the signal handler.
  155. * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
  156. */
  157. regs->b4 = (unsigned long)&frame->info;
  158. regs->a6 = (unsigned long)&frame->uc;
  159. return 0;
  160. }
  161. static inline void
  162. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  163. {
  164. switch (regs->a4) {
  165. case -ERESTARTNOHAND:
  166. if (!has_handler)
  167. goto do_restart;
  168. regs->a4 = -EINTR;
  169. break;
  170. case -ERESTARTSYS:
  171. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  172. regs->a4 = -EINTR;
  173. break;
  174. }
  175. /* fallthrough */
  176. case -ERESTARTNOINTR:
  177. do_restart:
  178. regs->a4 = regs->orig_a4;
  179. regs->pc -= 4;
  180. break;
  181. }
  182. }
  183. /*
  184. * handle the actual delivery of a signal to userspace
  185. */
  186. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs,
  187. int syscall)
  188. {
  189. int ret;
  190. /* Are we from a system call? */
  191. if (syscall) {
  192. /* If so, check system call restarting.. */
  193. switch (regs->a4) {
  194. case -ERESTART_RESTARTBLOCK:
  195. case -ERESTARTNOHAND:
  196. regs->a4 = -EINTR;
  197. break;
  198. case -ERESTARTSYS:
  199. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  200. regs->a4 = -EINTR;
  201. break;
  202. }
  203. /* fallthrough */
  204. case -ERESTARTNOINTR:
  205. regs->a4 = regs->orig_a4;
  206. regs->pc -= 4;
  207. }
  208. }
  209. /* Set up the stack frame */
  210. ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
  211. signal_setup_done(ret, ksig, 0);
  212. }
  213. /*
  214. * handle a potential signal
  215. */
  216. static void do_signal(struct pt_regs *regs, int syscall)
  217. {
  218. struct ksignal ksig;
  219. /* we want the common case to go fast, which is why we may in certain
  220. * cases get here from kernel mode */
  221. if (!user_mode(regs))
  222. return;
  223. if (get_signal(&ksig)) {
  224. handle_signal(&ksig, regs, syscall);
  225. return;
  226. }
  227. /* did we come from a system call? */
  228. if (syscall) {
  229. /* restart the system call - no handlers present */
  230. switch (regs->a4) {
  231. case -ERESTARTNOHAND:
  232. case -ERESTARTSYS:
  233. case -ERESTARTNOINTR:
  234. regs->a4 = regs->orig_a4;
  235. regs->pc -= 4;
  236. break;
  237. case -ERESTART_RESTARTBLOCK:
  238. regs->a4 = regs->orig_a4;
  239. regs->b0 = __NR_restart_syscall;
  240. regs->pc -= 4;
  241. break;
  242. }
  243. }
  244. /* if there's no signal to deliver, we just put the saved sigmask
  245. * back */
  246. restore_saved_sigmask();
  247. }
  248. /*
  249. * notification of userspace execution resumption
  250. * - triggered by current->work.notify_resume
  251. */
  252. asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags,
  253. int syscall)
  254. {
  255. /* deal with pending signal delivery */
  256. if (thread_info_flags & (1 << TIF_SIGPENDING))
  257. do_signal(regs, syscall);
  258. if (thread_info_flags & (1 << TIF_NOTIFY_RESUME)) {
  259. clear_thread_flag(TIF_NOTIFY_RESUME);
  260. tracehook_notify_resume(regs);
  261. }
  262. }