signal.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * linux/arch/h8300/kernel/signal.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. */
  10. /*
  11. * uClinux H8/300 support by Yoshinori Sato <ysato@users.sourceforge.jp>
  12. * and David McCullough <davidm@snapgear.com>
  13. *
  14. * Based on
  15. * Linux/m68k by Hamish Macdonald
  16. */
  17. /*
  18. * ++roman (07/09/96): implemented signal stacks (specially for tosemu on
  19. * Atari :-) Current limitation: Only one sigstack can be active at one time.
  20. * If a second signal with SA_ONSTACK set arrives while working on a sigstack,
  21. * SA_ONSTACK is ignored. This behaviour avoids lots of trouble with nested
  22. * signal handlers!
  23. */
  24. #include <linux/sched.h>
  25. #include <linux/mm.h>
  26. #include <linux/kernel.h>
  27. #include <linux/signal.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/errno.h>
  30. #include <linux/wait.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/unistd.h>
  33. #include <linux/stddef.h>
  34. #include <linux/highuid.h>
  35. #include <linux/personality.h>
  36. #include <linux/tty.h>
  37. #include <linux/binfmts.h>
  38. #include <linux/tracehook.h>
  39. #include <asm/setup.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/traps.h>
  43. #include <asm/ucontext.h>
  44. /*
  45. * Do a signal return; undo the signal stack.
  46. *
  47. * Keep the return code on the stack quadword aligned!
  48. * That makes the cache flush below easier.
  49. */
  50. struct rt_sigframe {
  51. long dummy_er0;
  52. long dummy_vector;
  53. #if defined(CONFIG_CPU_H8S)
  54. short dummy_exr;
  55. #endif
  56. long dummy_pc;
  57. char *pretcode;
  58. struct siginfo *pinfo;
  59. void *puc;
  60. unsigned char retcode[8];
  61. struct siginfo info;
  62. struct ucontext uc;
  63. int sig;
  64. } __packed __aligned(2);
  65. static inline int
  66. restore_sigcontext(struct sigcontext *usc, int *pd0)
  67. {
  68. struct pt_regs *regs = current_pt_regs();
  69. int err = 0;
  70. unsigned int ccr;
  71. unsigned int usp;
  72. unsigned int er0;
  73. /* Always make any pending restarted system calls return -EINTR */
  74. current->restart_block.fn = do_no_restart_syscall;
  75. /* restore passed registers */
  76. #define COPY(r) do { err |= get_user(regs->r, &usc->sc_##r); } while (0)
  77. COPY(er1);
  78. COPY(er2);
  79. COPY(er3);
  80. COPY(er5);
  81. COPY(pc);
  82. ccr = regs->ccr & 0x10;
  83. COPY(ccr);
  84. #undef COPY
  85. regs->ccr &= 0xef;
  86. regs->ccr |= ccr;
  87. regs->orig_er0 = -1; /* disable syscall checks */
  88. err |= __get_user(usp, &usc->sc_usp);
  89. wrusp(usp);
  90. err |= __get_user(er0, &usc->sc_er0);
  91. *pd0 = er0;
  92. return err;
  93. }
  94. asmlinkage int sys_rt_sigreturn(void)
  95. {
  96. unsigned long usp = rdusp();
  97. struct rt_sigframe *frame = (struct rt_sigframe *)(usp - 4);
  98. sigset_t set;
  99. int er0;
  100. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  101. goto badframe;
  102. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  103. goto badframe;
  104. set_current_blocked(&set);
  105. if (restore_sigcontext(&frame->uc.uc_mcontext, &er0))
  106. goto badframe;
  107. if (restore_altstack(&frame->uc.uc_stack))
  108. goto badframe;
  109. return er0;
  110. badframe:
  111. force_sig(SIGSEGV, current);
  112. return 0;
  113. }
  114. static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  115. unsigned long mask)
  116. {
  117. int err = 0;
  118. err |= __put_user(regs->er0, &sc->sc_er0);
  119. err |= __put_user(regs->er1, &sc->sc_er1);
  120. err |= __put_user(regs->er2, &sc->sc_er2);
  121. err |= __put_user(regs->er3, &sc->sc_er3);
  122. err |= __put_user(regs->er4, &sc->sc_er4);
  123. err |= __put_user(regs->er5, &sc->sc_er5);
  124. err |= __put_user(regs->er6, &sc->sc_er6);
  125. err |= __put_user(rdusp(), &sc->sc_usp);
  126. err |= __put_user(regs->pc, &sc->sc_pc);
  127. err |= __put_user(regs->ccr, &sc->sc_ccr);
  128. err |= __put_user(mask, &sc->sc_mask);
  129. return err;
  130. }
  131. static inline void __user *
  132. get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size)
  133. {
  134. return (void __user *)((sigsp(rdusp(), ksig) - frame_size) & -8UL);
  135. }
  136. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  137. struct pt_regs *regs)
  138. {
  139. struct rt_sigframe *frame;
  140. int err = 0;
  141. unsigned char *ret;
  142. frame = get_sigframe(ksig, regs, sizeof(*frame));
  143. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  144. return -EFAULT;
  145. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  146. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  147. /* Create the ucontext. */
  148. err |= __put_user(0, &frame->uc.uc_flags);
  149. err |= __put_user(0, &frame->uc.uc_link);
  150. err |= __save_altstack(&frame->uc.uc_stack, rdusp());
  151. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  152. err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  153. if (err)
  154. return -EFAULT;
  155. /* Set up to return from userspace. */
  156. ret = frame->retcode;
  157. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  158. ret = (unsigned char *)(ksig->ka.sa.sa_restorer);
  159. else {
  160. /* sub.l er0,er0; mov.b #__NR_rt_sigreturn,r0l; trapa #0 */
  161. err |= __put_user(0x1a80f800 + (__NR_rt_sigreturn & 0xff),
  162. (unsigned long *)(frame->retcode + 0));
  163. err |= __put_user(0x5700,
  164. (unsigned short *)(frame->retcode + 4));
  165. }
  166. err |= __put_user(ret, &frame->pretcode);
  167. if (err)
  168. return -EFAULT;
  169. /* Set up registers for signal handler */
  170. wrusp((unsigned long) frame);
  171. regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  172. regs->er0 = ksig->sig;
  173. regs->er1 = (unsigned long)&(frame->info);
  174. regs->er2 = (unsigned long)&frame->uc;
  175. regs->er5 = current->mm->start_data; /* GOT base */
  176. return 0;
  177. }
  178. static void
  179. handle_restart(struct pt_regs *regs, struct k_sigaction *ka)
  180. {
  181. switch (regs->er0) {
  182. case -ERESTARTNOHAND:
  183. if (!ka)
  184. goto do_restart;
  185. regs->er0 = -EINTR;
  186. break;
  187. case -ERESTART_RESTARTBLOCK:
  188. if (!ka) {
  189. regs->er0 = __NR_restart_syscall;
  190. regs->pc -= 2;
  191. } else
  192. regs->er0 = -EINTR;
  193. break;
  194. case -ERESTARTSYS:
  195. if (!(ka->sa.sa_flags & SA_RESTART)) {
  196. regs->er0 = -EINTR;
  197. break;
  198. }
  199. /* fallthrough */
  200. case -ERESTARTNOINTR:
  201. do_restart:
  202. regs->er0 = regs->orig_er0;
  203. regs->pc -= 2;
  204. break;
  205. }
  206. }
  207. /*
  208. * OK, we're invoking a handler
  209. */
  210. static void
  211. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  212. {
  213. sigset_t *oldset = sigmask_to_save();
  214. int ret;
  215. /* are we from a system call? */
  216. if (regs->orig_er0 >= 0)
  217. handle_restart(regs, &ksig->ka);
  218. ret = setup_rt_frame(ksig, oldset, regs);
  219. signal_setup_done(ret, ksig, 0);
  220. }
  221. /*
  222. * Note that 'init' is a special process: it doesn't get signals it doesn't
  223. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  224. * mistake.
  225. */
  226. static void do_signal(struct pt_regs *regs)
  227. {
  228. struct ksignal ksig;
  229. current->thread.esp0 = (unsigned long) regs;
  230. if (get_signal(&ksig)) {
  231. /* Whee! Actually deliver the signal. */
  232. handle_signal(&ksig, regs);
  233. return;
  234. }
  235. /* Did we come from a system call? */
  236. if (regs->orig_er0 >= 0)
  237. handle_restart(regs, NULL);
  238. /* If there's no signal to deliver, we just restore the saved mask. */
  239. restore_saved_sigmask();
  240. }
  241. asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
  242. {
  243. if (thread_info_flags & _TIF_SIGPENDING)
  244. do_signal(regs);
  245. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  246. clear_thread_flag(TIF_NOTIFY_RESUME);
  247. tracehook_notify_resume(regs);
  248. }
  249. }