signal.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 1991,1992 Linus Torvalds
  3. * Copyright (C) 2005-2012 Imagination Technologies Ltd.
  4. *
  5. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  6. *
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/smp.h>
  11. #include <linux/kernel.h>
  12. #include <linux/signal.h>
  13. #include <linux/errno.h>
  14. #include <linux/wait.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/unistd.h>
  17. #include <linux/stddef.h>
  18. #include <linux/personality.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/tracehook.h>
  21. #include <asm/ucontext.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/switch.h>
  24. #include <asm/syscall.h>
  25. #include <asm/syscalls.h>
  26. #define REG_FLAGS ctx.SaveMask
  27. #define REG_RETVAL ctx.DX[0].U0
  28. #define REG_SYSCALL ctx.DX[0].U1
  29. #define REG_SP ctx.AX[0].U0
  30. #define REG_ARG1 ctx.DX[3].U1
  31. #define REG_ARG2 ctx.DX[3].U0
  32. #define REG_ARG3 ctx.DX[2].U1
  33. #define REG_PC ctx.CurrPC
  34. #define REG_RTP ctx.DX[4].U1
  35. struct rt_sigframe {
  36. struct siginfo info;
  37. struct ucontext uc;
  38. unsigned long retcode[2];
  39. };
  40. static int restore_sigcontext(struct pt_regs *regs,
  41. struct sigcontext __user *sc)
  42. {
  43. int err;
  44. /* Always make any pending restarted system calls return -EINTR */
  45. current->restart_block.fn = do_no_restart_syscall;
  46. err = metag_gp_regs_copyin(regs, 0, sizeof(struct user_gp_regs), NULL,
  47. &sc->regs);
  48. if (!err)
  49. err = metag_cb_regs_copyin(regs, 0,
  50. sizeof(struct user_cb_regs), NULL,
  51. &sc->cb);
  52. if (!err)
  53. err = metag_rp_state_copyin(regs, 0,
  54. sizeof(struct user_rp_state), NULL,
  55. &sc->rp);
  56. /* This is a user-mode context. */
  57. regs->REG_FLAGS |= TBICTX_PRIV_BIT;
  58. return err;
  59. }
  60. long sys_rt_sigreturn(void)
  61. {
  62. /* NOTE - Meta stack goes UPWARDS - so we wind the stack back */
  63. struct pt_regs *regs = current_pt_regs();
  64. struct rt_sigframe __user *frame;
  65. sigset_t set;
  66. frame = (__force struct rt_sigframe __user *)(regs->REG_SP -
  67. sizeof(*frame));
  68. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  69. goto badframe;
  70. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  71. goto badframe;
  72. set_current_blocked(&set);
  73. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  74. goto badframe;
  75. if (restore_altstack(&frame->uc.uc_stack))
  76. goto badframe;
  77. return regs->REG_RETVAL;
  78. badframe:
  79. force_sig(SIGSEGV, current);
  80. return 0;
  81. }
  82. static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  83. unsigned long mask)
  84. {
  85. int err;
  86. err = metag_gp_regs_copyout(regs, 0, sizeof(struct user_gp_regs), NULL,
  87. &sc->regs);
  88. if (!err)
  89. err = metag_cb_regs_copyout(regs, 0,
  90. sizeof(struct user_cb_regs), NULL,
  91. &sc->cb);
  92. if (!err)
  93. err = metag_rp_state_copyout(regs, 0,
  94. sizeof(struct user_rp_state), NULL,
  95. &sc->rp);
  96. /* OK, clear that cbuf flag in the old context, or our stored
  97. * catch buffer will be restored when we go to call the signal
  98. * handler. Also clear out the CBRP RA/RD pipe bit incase
  99. * that is pending as well!
  100. * Note that as we have already stored this context, these
  101. * flags will get restored on sigreturn to their original
  102. * state.
  103. */
  104. regs->REG_FLAGS &= ~(TBICTX_XCBF_BIT | TBICTX_CBUF_BIT |
  105. TBICTX_CBRP_BIT);
  106. /* Clear out the LSM_STEP bits in case we are in the middle of
  107. * and MSET/MGET.
  108. */
  109. regs->ctx.Flags &= ~TXSTATUS_LSM_STEP_BITS;
  110. err |= __put_user(mask, &sc->oldmask);
  111. return err;
  112. }
  113. /*
  114. * Determine which stack to use..
  115. */
  116. static void __user *get_sigframe(struct ksignal *ksig, unsigned long sp)
  117. {
  118. sp = sigsp(sp, ksig);
  119. sp = (sp + 7) & ~7; /* 8byte align stack */
  120. return (void __user *)sp;
  121. }
  122. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  123. struct pt_regs *regs)
  124. {
  125. struct rt_sigframe __user *frame;
  126. int err;
  127. unsigned long code;
  128. frame = get_sigframe(ksig, regs->REG_SP);
  129. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  130. return -EFAULT;
  131. err = copy_siginfo_to_user(&frame->info, &ksig->info);
  132. /* Create the ucontext. */
  133. err |= __put_user(0, &frame->uc.uc_flags);
  134. err |= __put_user(0, (unsigned long __user *)&frame->uc.uc_link);
  135. err |= __save_altstack(&frame->uc.uc_stack, regs->REG_SP);
  136. err |= setup_sigcontext(&frame->uc.uc_mcontext,
  137. regs, set->sig[0]);
  138. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  139. if (err)
  140. return -EFAULT;
  141. /* Set up to return from userspace. */
  142. /* MOV D1Re0 (D1.0), #__NR_rt_sigreturn */
  143. code = 0x03000004 | (__NR_rt_sigreturn << 3);
  144. err |= __put_user(code, (unsigned long __user *)(&frame->retcode[0]));
  145. /* SWITCH #__METAG_SW_SYS */
  146. code = __METAG_SW_ENCODING(SYS);
  147. err |= __put_user(code, (unsigned long __user *)(&frame->retcode[1]));
  148. if (err)
  149. return -EFAULT;
  150. /* Set up registers for signal handler */
  151. regs->REG_RTP = (unsigned long) frame->retcode;
  152. regs->REG_SP = (unsigned long) frame + sizeof(*frame);
  153. regs->REG_ARG1 = ksig->sig;
  154. regs->REG_ARG2 = (unsigned long) &frame->info;
  155. regs->REG_ARG3 = (unsigned long) &frame->uc;
  156. regs->REG_PC = (unsigned long) ksig->ka.sa.sa_handler;
  157. pr_debug("SIG deliver (%s:%d): sp=%p pc=%08x pr=%08x\n",
  158. current->comm, current->pid, frame, regs->REG_PC,
  159. regs->REG_RTP);
  160. /* Now pass size of 'new code' into sigtramp so we can do a more
  161. * effective cache flush - directed rather than 'full flush'.
  162. */
  163. flush_cache_sigtramp(regs->REG_RTP, sizeof(frame->retcode));
  164. return 0;
  165. }
  166. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  167. {
  168. sigset_t *oldset = sigmask_to_save();
  169. int ret;
  170. /* Set up the stack frame */
  171. ret = setup_rt_frame(ksig, oldset, regs);
  172. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  173. }
  174. /*
  175. * Notes for Meta.
  176. * We have moved from the old 2.4.9 SH way of using syscall_nr (in the stored
  177. * context) to passing in the syscall flag on the stack.
  178. * This is because having syscall_nr in our context does not fit with TBX, and
  179. * corrupted the stack.
  180. */
  181. static int do_signal(struct pt_regs *regs, int syscall)
  182. {
  183. unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
  184. int restart = 0;
  185. struct ksignal ksig;
  186. /*
  187. * By the end of rt_sigreturn the context describes the point that the
  188. * signal was taken (which may happen to be just before a syscall if
  189. * it's already been restarted). This should *never* be mistaken for a
  190. * system call in need of restarting.
  191. */
  192. if (syscall == __NR_rt_sigreturn)
  193. syscall = -1;
  194. /* Did we come from a system call? */
  195. if (syscall >= 0) {
  196. continue_addr = regs->REG_PC;
  197. restart_addr = continue_addr - 4;
  198. retval = regs->REG_RETVAL;
  199. /*
  200. * Prepare for system call restart. We do this here so that a
  201. * debugger will see the already changed PC.
  202. */
  203. switch (retval) {
  204. case -ERESTART_RESTARTBLOCK:
  205. restart = -2;
  206. case -ERESTARTNOHAND:
  207. case -ERESTARTSYS:
  208. case -ERESTARTNOINTR:
  209. ++restart;
  210. regs->REG_PC = restart_addr;
  211. break;
  212. }
  213. }
  214. /*
  215. * Get the signal to deliver. When running under ptrace, at this point
  216. * the debugger may change all our registers ...
  217. */
  218. get_signal(&ksig);
  219. /*
  220. * Depending on the signal settings we may need to revert the decision
  221. * to restart the system call. But skip this if a debugger has chosen to
  222. * restart at a different PC.
  223. */
  224. if (regs->REG_PC != restart_addr)
  225. restart = 0;
  226. if (ksig.sig > 0) {
  227. if (unlikely(restart)) {
  228. if (retval == -ERESTARTNOHAND
  229. || retval == -ERESTART_RESTARTBLOCK
  230. || (retval == -ERESTARTSYS
  231. && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
  232. regs->REG_RETVAL = -EINTR;
  233. regs->REG_PC = continue_addr;
  234. }
  235. }
  236. /* Whee! Actually deliver the signal. */
  237. handle_signal(&ksig, regs);
  238. return 0;
  239. }
  240. /* Handlerless -ERESTART_RESTARTBLOCK re-enters via restart_syscall */
  241. if (unlikely(restart < 0))
  242. regs->REG_SYSCALL = __NR_restart_syscall;
  243. /*
  244. * If there's no signal to deliver, we just put the saved sigmask back.
  245. */
  246. restore_saved_sigmask();
  247. return restart;
  248. }
  249. int do_work_pending(struct pt_regs *regs, unsigned int thread_flags,
  250. int syscall)
  251. {
  252. do {
  253. if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  254. schedule();
  255. } else {
  256. if (unlikely(!user_mode(regs)))
  257. return 0;
  258. local_irq_enable();
  259. if (thread_flags & _TIF_SIGPENDING) {
  260. int restart = do_signal(regs, syscall);
  261. if (unlikely(restart)) {
  262. /*
  263. * Restart without handlers.
  264. * Deal with it without leaving
  265. * the kernel space.
  266. */
  267. return restart;
  268. }
  269. syscall = -1;
  270. } else {
  271. clear_thread_flag(TIF_NOTIFY_RESUME);
  272. tracehook_notify_resume(regs);
  273. }
  274. }
  275. local_irq_disable();
  276. thread_flags = current_thread_info()->flags;
  277. } while (thread_flags & _TIF_WORK_MASK);
  278. return 0;
  279. }