signal.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Signal support for Hexagon processor
  3. *
  4. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <linux/linkage.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/tracehook.h>
  23. #include <asm/registers.h>
  24. #include <asm/thread_info.h>
  25. #include <asm/unistd.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/ucontext.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/signal.h>
  30. #include <asm/vdso.h>
  31. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  32. struct rt_sigframe {
  33. unsigned long tramp[2];
  34. struct siginfo info;
  35. struct ucontext uc;
  36. };
  37. static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
  38. size_t frame_size)
  39. {
  40. unsigned long sp = regs->r29;
  41. /* Switch to signal stack if appropriate */
  42. if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
  43. sp = current->sas_ss_sp + current->sas_ss_size;
  44. return (void __user *)((sp - frame_size) & ~(sizeof(long long) - 1));
  45. }
  46. static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  47. {
  48. unsigned long tmp;
  49. int err = 0;
  50. err |= copy_to_user(&sc->sc_regs.r0, &regs->r00,
  51. 32*sizeof(unsigned long));
  52. err |= __put_user(regs->sa0, &sc->sc_regs.sa0);
  53. err |= __put_user(regs->lc0, &sc->sc_regs.lc0);
  54. err |= __put_user(regs->sa1, &sc->sc_regs.sa1);
  55. err |= __put_user(regs->lc1, &sc->sc_regs.lc1);
  56. err |= __put_user(regs->m0, &sc->sc_regs.m0);
  57. err |= __put_user(regs->m1, &sc->sc_regs.m1);
  58. err |= __put_user(regs->usr, &sc->sc_regs.usr);
  59. err |= __put_user(regs->preds, &sc->sc_regs.p3_0);
  60. err |= __put_user(regs->gp, &sc->sc_regs.gp);
  61. err |= __put_user(regs->ugp, &sc->sc_regs.ugp);
  62. tmp = pt_elr(regs); err |= __put_user(tmp, &sc->sc_regs.pc);
  63. tmp = pt_cause(regs); err |= __put_user(tmp, &sc->sc_regs.cause);
  64. tmp = pt_badva(regs); err |= __put_user(tmp, &sc->sc_regs.badva);
  65. return err;
  66. }
  67. static int restore_sigcontext(struct pt_regs *regs,
  68. struct sigcontext __user *sc)
  69. {
  70. unsigned long tmp;
  71. int err = 0;
  72. err |= copy_from_user(&regs->r00, &sc->sc_regs.r0,
  73. 32 * sizeof(unsigned long));
  74. err |= __get_user(regs->sa0, &sc->sc_regs.sa0);
  75. err |= __get_user(regs->lc0, &sc->sc_regs.lc0);
  76. err |= __get_user(regs->sa1, &sc->sc_regs.sa1);
  77. err |= __get_user(regs->lc1, &sc->sc_regs.lc1);
  78. err |= __get_user(regs->m0, &sc->sc_regs.m0);
  79. err |= __get_user(regs->m1, &sc->sc_regs.m1);
  80. err |= __get_user(regs->usr, &sc->sc_regs.usr);
  81. err |= __get_user(regs->preds, &sc->sc_regs.p3_0);
  82. err |= __get_user(regs->gp, &sc->sc_regs.gp);
  83. err |= __get_user(regs->ugp, &sc->sc_regs.ugp);
  84. err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp);
  85. return err;
  86. }
  87. /*
  88. * Setup signal stack frame with siginfo structure
  89. */
  90. static int setup_rt_frame(int signr, struct k_sigaction *ka, siginfo_t *info,
  91. sigset_t *set, struct pt_regs *regs)
  92. {
  93. int err = 0;
  94. struct rt_sigframe __user *frame;
  95. struct hexagon_vdso *vdso = current->mm->context.vdso;
  96. frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe));
  97. if (!access_ok(VERIFY_WRITE, frame, sizeof(struct rt_sigframe)))
  98. goto sigsegv;
  99. if (copy_siginfo_to_user(&frame->info, info))
  100. goto sigsegv;
  101. /* The on-stack signal trampoline is no longer executed;
  102. * however, the libgcc signal frame unwinding code checks for
  103. * the presence of these two numeric magic values.
  104. */
  105. err |= __put_user(0x7800d166, &frame->tramp[0]);
  106. err |= __put_user(0x5400c004, &frame->tramp[1]);
  107. err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
  108. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  109. if (err)
  110. goto sigsegv;
  111. /* Load r0/r1 pair with signumber/siginfo pointer... */
  112. regs->r0100 = ((unsigned long long)((unsigned long)&frame->info) << 32)
  113. | (unsigned long long)signr;
  114. regs->r02 = (unsigned long) &frame->uc;
  115. regs->r31 = (unsigned long) vdso->rt_signal_trampoline;
  116. pt_psp(regs) = (unsigned long) frame;
  117. pt_set_elr(regs, (unsigned long)ka->sa.sa_handler);
  118. return 0;
  119. sigsegv:
  120. force_sigsegv(signr, current);
  121. return -EFAULT;
  122. }
  123. /*
  124. * Setup invocation of signal handler
  125. */
  126. static int handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
  127. sigset_t *oldset, struct pt_regs *regs)
  128. {
  129. int rc;
  130. /*
  131. * If we're handling a signal that aborted a system call,
  132. * set up the error return value before adding the signal
  133. * frame to the stack.
  134. */
  135. if (regs->syscall_nr >= 0) {
  136. switch (regs->r00) {
  137. case -ERESTART_RESTARTBLOCK:
  138. case -ERESTARTNOHAND:
  139. regs->r00 = -EINTR;
  140. break;
  141. case -ERESTARTSYS:
  142. if (!(ka->sa.sa_flags & SA_RESTART)) {
  143. regs->r00 = -EINTR;
  144. break;
  145. }
  146. /* Fall through */
  147. case -ERESTARTNOINTR:
  148. regs->r06 = regs->syscall_nr;
  149. pt_set_elr(regs, pt_elr(regs) - 4);
  150. regs->r00 = regs->restart_r0;
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. /*
  157. * Set up the stack frame; not doing the SA_SIGINFO thing. We
  158. * only set up the rt_frame flavor.
  159. */
  160. rc = setup_rt_frame(sig, ka, info, oldset, regs);
  161. /* If there was an error on setup, no signal was delivered. */
  162. if (rc)
  163. return rc;
  164. block_sigmask(ka, sig);
  165. return 0;
  166. }
  167. /*
  168. * Called from return-from-event code.
  169. */
  170. static void do_signal(struct pt_regs *regs)
  171. {
  172. struct k_sigaction sigact;
  173. siginfo_t info;
  174. int signo;
  175. if (!user_mode(regs))
  176. return;
  177. if (try_to_freeze())
  178. goto no_signal;
  179. signo = get_signal_to_deliver(&info, &sigact, regs, NULL);
  180. if (signo > 0) {
  181. sigset_t *oldset;
  182. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  183. oldset = &current->saved_sigmask;
  184. else
  185. oldset = &current->blocked;
  186. if (handle_signal(signo, &info, &sigact, oldset, regs) == 0) {
  187. /*
  188. * Successful delivery case. The saved sigmask is
  189. * stored in the signal frame, and will be restored
  190. * by sigreturn. We can clear the TIF flag.
  191. */
  192. clear_thread_flag(TIF_RESTORE_SIGMASK);
  193. tracehook_signal_handler(signo, &info, &sigact, regs,
  194. test_thread_flag(TIF_SINGLESTEP));
  195. }
  196. return;
  197. }
  198. no_signal:
  199. /*
  200. * If we came from a system call, handle the restart.
  201. */
  202. if (regs->syscall_nr >= 0) {
  203. switch (regs->r00) {
  204. case -ERESTARTNOHAND:
  205. case -ERESTARTSYS:
  206. case -ERESTARTNOINTR:
  207. regs->r06 = regs->syscall_nr;
  208. break;
  209. case -ERESTART_RESTARTBLOCK:
  210. regs->r06 = __NR_restart_syscall;
  211. break;
  212. default:
  213. goto no_restart;
  214. }
  215. pt_set_elr(regs, pt_elr(regs) - 4);
  216. regs->r00 = regs->restart_r0;
  217. }
  218. no_restart:
  219. /* If there's no signal to deliver, put the saved sigmask back */
  220. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  221. clear_thread_flag(TIF_RESTORE_SIGMASK);
  222. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  223. }
  224. }
  225. void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
  226. {
  227. if (thread_info_flags & _TIF_SIGPENDING)
  228. do_signal(regs);
  229. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  230. clear_thread_flag(TIF_NOTIFY_RESUME);
  231. if (current->replacement_session_keyring)
  232. key_replace_session_keyring();
  233. }
  234. }
  235. /*
  236. * Architecture-specific wrappers for signal-related system calls
  237. */
  238. asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  239. {
  240. struct pt_regs *regs = current_thread_info()->regs;
  241. return do_sigaltstack(uss, uoss, regs->r29);
  242. }
  243. asmlinkage int sys_rt_sigreturn(void)
  244. {
  245. struct pt_regs *regs = current_thread_info()->regs;
  246. struct rt_sigframe __user *frame;
  247. sigset_t blocked;
  248. frame = (struct rt_sigframe __user *)pt_psp(regs);
  249. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  250. goto badframe;
  251. if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked)))
  252. goto badframe;
  253. sigdelsetmask(&blocked, ~_BLOCKABLE);
  254. set_current_blocked(&blocked);
  255. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  256. goto badframe;
  257. /* Restore the user's stack as well */
  258. pt_psp(regs) = regs->r29;
  259. /*
  260. * Leave a trace in the stack frame that this was a sigreturn.
  261. * If the system call is to replay, we've already restored the
  262. * number in the GPR slot and it will be regenerated on the
  263. * new system call trap entry. Note that if restore_sigcontext()
  264. * did something other than a bulk copy of the pt_regs struct,
  265. * we could avoid this assignment by simply not overwriting
  266. * regs->syscall_nr.
  267. */
  268. regs->syscall_nr = __NR_rt_sigreturn;
  269. /*
  270. * If we were meticulous, we'd only call this if we knew that
  271. * we were actually going to use an alternate stack, and we'd
  272. * consider any error to be fatal. What we do here, in common
  273. * with many other architectures, is call it blindly and only
  274. * consider the -EFAULT return case to be proof of a problem.
  275. */
  276. if (do_sigaltstack(&frame->uc.uc_stack, NULL, pt_psp(regs)) == -EFAULT)
  277. goto badframe;
  278. return 0;
  279. badframe:
  280. force_sig(SIGSEGV, current);
  281. return 0;
  282. }