signal.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_thread_info()->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 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 void handle_signal(int sig,
  195. siginfo_t *info, struct k_sigaction *ka,
  196. struct pt_regs *regs, int syscall)
  197. {
  198. /* Are we from a system call? */
  199. if (syscall) {
  200. /* If so, check system call restarting.. */
  201. switch (regs->a4) {
  202. case -ERESTART_RESTARTBLOCK:
  203. case -ERESTARTNOHAND:
  204. regs->a4 = -EINTR;
  205. break;
  206. case -ERESTARTSYS:
  207. if (!(ka->sa.sa_flags & SA_RESTART)) {
  208. regs->a4 = -EINTR;
  209. break;
  210. }
  211. /* fallthrough */
  212. case -ERESTARTNOINTR:
  213. regs->a4 = regs->orig_a4;
  214. regs->pc -= 4;
  215. }
  216. }
  217. /* Set up the stack frame */
  218. if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
  219. return;
  220. signal_delivered(sig, info, ka, regs, 0);
  221. }
  222. /*
  223. * handle a potential signal
  224. */
  225. static void do_signal(struct pt_regs *regs, int syscall)
  226. {
  227. struct k_sigaction ka;
  228. siginfo_t info;
  229. int signr;
  230. /* we want the common case to go fast, which is why we may in certain
  231. * cases get here from kernel mode */
  232. if (!user_mode(regs))
  233. return;
  234. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  235. if (signr > 0) {
  236. handle_signal(signr, &info, &ka, regs, syscall);
  237. return;
  238. }
  239. /* did we come from a system call? */
  240. if (syscall) {
  241. /* restart the system call - no handlers present */
  242. switch (regs->a4) {
  243. case -ERESTARTNOHAND:
  244. case -ERESTARTSYS:
  245. case -ERESTARTNOINTR:
  246. regs->a4 = regs->orig_a4;
  247. regs->pc -= 4;
  248. break;
  249. case -ERESTART_RESTARTBLOCK:
  250. regs->a4 = regs->orig_a4;
  251. regs->b0 = __NR_restart_syscall;
  252. regs->pc -= 4;
  253. break;
  254. }
  255. }
  256. /* if there's no signal to deliver, we just put the saved sigmask
  257. * back */
  258. restore_saved_sigmask();
  259. }
  260. /*
  261. * notification of userspace execution resumption
  262. * - triggered by current->work.notify_resume
  263. */
  264. asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags,
  265. int syscall)
  266. {
  267. /* deal with pending signal delivery */
  268. if (thread_info_flags & (1 << TIF_SIGPENDING))
  269. do_signal(regs, syscall);
  270. if (thread_info_flags & (1 << TIF_NOTIFY_RESUME)) {
  271. clear_thread_flag(TIF_NOTIFY_RESUME);
  272. tracehook_notify_resume(regs);
  273. }
  274. }