signal.c 9.1 KB

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