signal.c 9.5 KB

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