signal.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Signal support for Hexagon processor
  3. *
  4. * Copyright (c) 2010-2012, 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. struct rt_sigframe {
  32. unsigned long tramp[2];
  33. struct siginfo info;
  34. struct ucontext uc;
  35. };
  36. static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
  37. size_t frame_size)
  38. {
  39. unsigned long sp = sigsp(regs->r29, ksig);
  40. return (void __user *)((sp - frame_size) & ~(sizeof(long long) - 1));
  41. }
  42. static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  43. {
  44. unsigned long tmp;
  45. int err = 0;
  46. err |= copy_to_user(&sc->sc_regs.r0, &regs->r00,
  47. 32*sizeof(unsigned long));
  48. err |= __put_user(regs->sa0, &sc->sc_regs.sa0);
  49. err |= __put_user(regs->lc0, &sc->sc_regs.lc0);
  50. err |= __put_user(regs->sa1, &sc->sc_regs.sa1);
  51. err |= __put_user(regs->lc1, &sc->sc_regs.lc1);
  52. err |= __put_user(regs->m0, &sc->sc_regs.m0);
  53. err |= __put_user(regs->m1, &sc->sc_regs.m1);
  54. err |= __put_user(regs->usr, &sc->sc_regs.usr);
  55. err |= __put_user(regs->preds, &sc->sc_regs.p3_0);
  56. err |= __put_user(regs->gp, &sc->sc_regs.gp);
  57. err |= __put_user(regs->ugp, &sc->sc_regs.ugp);
  58. #if CONFIG_HEXAGON_ARCH_VERSION >= 4
  59. err |= __put_user(regs->cs0, &sc->sc_regs.cs0);
  60. err |= __put_user(regs->cs1, &sc->sc_regs.cs1);
  61. #endif
  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. #if CONFIG_HEXAGON_ARCH_VERSION >= 4
  85. err |= __get_user(regs->cs0, &sc->sc_regs.cs0);
  86. err |= __get_user(regs->cs1, &sc->sc_regs.cs1);
  87. #endif
  88. err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp);
  89. return err;
  90. }
  91. /*
  92. * Setup signal stack frame with siginfo structure
  93. */
  94. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  95. struct pt_regs *regs)
  96. {
  97. int err = 0;
  98. struct rt_sigframe __user *frame;
  99. struct hexagon_vdso *vdso = current->mm->context.vdso;
  100. frame = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
  101. if (!access_ok(VERIFY_WRITE, frame, sizeof(struct rt_sigframe)))
  102. return -EFAULT;
  103. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  104. return -EFAULT;
  105. /* The on-stack signal trampoline is no longer executed;
  106. * however, the libgcc signal frame unwinding code checks for
  107. * the presence of these two numeric magic values.
  108. */
  109. err |= __put_user(0x7800d166, &frame->tramp[0]);
  110. err |= __put_user(0x5400c004, &frame->tramp[1]);
  111. err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
  112. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  113. err |= __save_altstack(&frame->uc.uc_stack, user_stack_pointer(regs));
  114. if (err)
  115. return -EFAULT;
  116. /* Load r0/r1 pair with signumber/siginfo pointer... */
  117. regs->r0100 = ((unsigned long long)((unsigned long)&frame->info) << 32)
  118. | (unsigned long long)ksig->sig;
  119. regs->r02 = (unsigned long) &frame->uc;
  120. regs->r31 = (unsigned long) vdso->rt_signal_trampoline;
  121. pt_psp(regs) = (unsigned long) frame;
  122. pt_set_elr(regs, (unsigned long)ksig->ka.sa.sa_handler);
  123. return 0;
  124. }
  125. /*
  126. * Setup invocation of signal handler
  127. */
  128. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  129. {
  130. int ret;
  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 (!(ksig->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. /* If there was an error on setup, no signal was delivered. */
  162. ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
  163. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  164. }
  165. /*
  166. * Called from return-from-event code.
  167. */
  168. void do_signal(struct pt_regs *regs)
  169. {
  170. struct ksignal ksig;
  171. if (!user_mode(regs))
  172. return;
  173. if (get_signal(&ksig)) {
  174. handle_signal(&ksig, regs);
  175. return;
  176. }
  177. /*
  178. * No (more) signals; if we came from a system call, handle the restart.
  179. */
  180. if (regs->syscall_nr >= 0) {
  181. switch (regs->r00) {
  182. case -ERESTARTNOHAND:
  183. case -ERESTARTSYS:
  184. case -ERESTARTNOINTR:
  185. regs->r06 = regs->syscall_nr;
  186. break;
  187. case -ERESTART_RESTARTBLOCK:
  188. regs->r06 = __NR_restart_syscall;
  189. break;
  190. default:
  191. goto no_restart;
  192. }
  193. pt_set_elr(regs, pt_elr(regs) - 4);
  194. regs->r00 = regs->restart_r0;
  195. }
  196. no_restart:
  197. /* If there's no signal to deliver, put the saved sigmask back */
  198. restore_saved_sigmask();
  199. }
  200. /*
  201. * Architecture-specific wrappers for signal-related system calls
  202. */
  203. asmlinkage int sys_rt_sigreturn(void)
  204. {
  205. struct pt_regs *regs = current_pt_regs();
  206. struct rt_sigframe __user *frame;
  207. sigset_t blocked;
  208. /* Always make any pending restarted system calls return -EINTR */
  209. current->restart_block.fn = do_no_restart_syscall;
  210. frame = (struct rt_sigframe __user *)pt_psp(regs);
  211. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  212. goto badframe;
  213. if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked)))
  214. goto badframe;
  215. set_current_blocked(&blocked);
  216. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  217. goto badframe;
  218. /* Restore the user's stack as well */
  219. pt_psp(regs) = regs->r29;
  220. regs->syscall_nr = -1;
  221. if (restore_altstack(&frame->uc.uc_stack))
  222. goto badframe;
  223. return regs->r00;
  224. badframe:
  225. force_sig(SIGSEGV, current);
  226. return 0;
  227. }