signal.c 7.0 KB

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