signal.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * linux/arch/m32r/kernel/signal.c
  3. *
  4. * Copyright (c) 2003 Hitoshi Yamamoto
  5. *
  6. * Taken from i386 version.
  7. * Copyright (C) 1991, 1992 Linus Torvalds
  8. *
  9. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  10. * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/kernel.h>
  16. #include <linux/signal.h>
  17. #include <linux/errno.h>
  18. #include <linux/wait.h>
  19. #include <linux/unistd.h>
  20. #include <linux/stddef.h>
  21. #include <linux/personality.h>
  22. #include <linux/tracehook.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/ucontext.h>
  25. #include <asm/uaccess.h>
  26. #define DEBUG_SIG 0
  27. /*
  28. * Do a signal return; undo the signal stack.
  29. */
  30. struct rt_sigframe
  31. {
  32. int sig;
  33. struct siginfo __user *pinfo;
  34. void __user *puc;
  35. struct siginfo info;
  36. struct ucontext uc;
  37. // struct _fpstate fpstate;
  38. };
  39. static int
  40. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
  41. int *r0_p)
  42. {
  43. unsigned int err = 0;
  44. /* Always make any pending restarted system calls return -EINTR */
  45. current->restart_block.fn = do_no_restart_syscall;
  46. #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
  47. COPY(r4);
  48. COPY(r5);
  49. COPY(r6);
  50. COPY(pt_regs);
  51. /* COPY(r0); Skip r0 */
  52. COPY(r1);
  53. COPY(r2);
  54. COPY(r3);
  55. COPY(r7);
  56. COPY(r8);
  57. COPY(r9);
  58. COPY(r10);
  59. COPY(r11);
  60. COPY(r12);
  61. COPY(acc0h);
  62. COPY(acc0l);
  63. COPY(acc1h); /* ISA_DSP_LEVEL2 only */
  64. COPY(acc1l); /* ISA_DSP_LEVEL2 only */
  65. COPY(psw);
  66. COPY(bpc);
  67. COPY(bbpsw);
  68. COPY(bbpc);
  69. COPY(spu);
  70. COPY(fp);
  71. COPY(lr);
  72. COPY(spi);
  73. #undef COPY
  74. regs->syscall_nr = -1; /* disable syscall checks */
  75. err |= __get_user(*r0_p, &sc->sc_r0);
  76. return err;
  77. }
  78. asmlinkage int
  79. sys_rt_sigreturn(unsigned long r0, unsigned long r1,
  80. unsigned long r2, unsigned long r3, unsigned long r4,
  81. unsigned long r5, unsigned long r6, struct pt_regs *regs)
  82. {
  83. struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
  84. sigset_t set;
  85. int result;
  86. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  87. goto badframe;
  88. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  89. goto badframe;
  90. set_current_blocked(&set);
  91. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
  92. goto badframe;
  93. if (restore_altstack(&frame->uc.uc_stack))
  94. goto badframe;
  95. return result;
  96. badframe:
  97. force_sig(SIGSEGV, current);
  98. return 0;
  99. }
  100. /*
  101. * Set up a signal frame.
  102. */
  103. static int
  104. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  105. unsigned long mask)
  106. {
  107. int err = 0;
  108. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  109. COPY(r4);
  110. COPY(r5);
  111. COPY(r6);
  112. COPY(pt_regs);
  113. COPY(r0);
  114. COPY(r1);
  115. COPY(r2);
  116. COPY(r3);
  117. COPY(r7);
  118. COPY(r8);
  119. COPY(r9);
  120. COPY(r10);
  121. COPY(r11);
  122. COPY(r12);
  123. COPY(acc0h);
  124. COPY(acc0l);
  125. COPY(acc1h); /* ISA_DSP_LEVEL2 only */
  126. COPY(acc1l); /* ISA_DSP_LEVEL2 only */
  127. COPY(psw);
  128. COPY(bpc);
  129. COPY(bbpsw);
  130. COPY(bbpc);
  131. COPY(spu);
  132. COPY(fp);
  133. COPY(lr);
  134. COPY(spi);
  135. #undef COPY
  136. err |= __put_user(mask, &sc->oldmask);
  137. return err;
  138. }
  139. /*
  140. * Determine which stack to use..
  141. */
  142. static inline void __user *
  143. get_sigframe(struct ksignal *ksig, unsigned long sp, size_t frame_size)
  144. {
  145. return (void __user *)((sigsp(sp, ksig) - frame_size) & -8ul);
  146. }
  147. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  148. struct pt_regs *regs)
  149. {
  150. struct rt_sigframe __user *frame;
  151. int err = 0;
  152. int sig = ksig->sig;
  153. frame = get_sigframe(ksig, regs->spu, sizeof(*frame));
  154. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  155. return -EFAULT;
  156. err |= __put_user(sig, &frame->sig);
  157. if (err)
  158. return -EFAULT;
  159. err |= __put_user(&frame->info, &frame->pinfo);
  160. err |= __put_user(&frame->uc, &frame->puc);
  161. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  162. if (err)
  163. return -EFAULT;
  164. /* Create the ucontext. */
  165. err |= __put_user(0, &frame->uc.uc_flags);
  166. err |= __put_user(0, &frame->uc.uc_link);
  167. err |= __save_altstack(&frame->uc.uc_stack, regs->spu);
  168. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  169. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  170. if (err)
  171. return -EFAULT;
  172. /* Set up to return from userspace. */
  173. regs->lr = (unsigned long)ksig->ka.sa.sa_restorer;
  174. /* Set up registers for signal handler */
  175. regs->spu = (unsigned long)frame;
  176. regs->r0 = sig; /* Arg for signal handler */
  177. regs->r1 = (unsigned long)&frame->info;
  178. regs->r2 = (unsigned long)&frame->uc;
  179. regs->bpc = (unsigned long)ksig->ka.sa.sa_handler;
  180. #if DEBUG_SIG
  181. printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
  182. current->comm, current->pid, frame, regs->pc);
  183. #endif
  184. return 0;
  185. }
  186. static int prev_insn(struct pt_regs *regs)
  187. {
  188. u16 inst;
  189. if (get_user(inst, (u16 __user *)(regs->bpc - 2)))
  190. return -EFAULT;
  191. if ((inst & 0xfff0) == 0x10f0) /* trap ? */
  192. regs->bpc -= 2;
  193. else
  194. regs->bpc -= 4;
  195. regs->syscall_nr = -1;
  196. return 0;
  197. }
  198. /*
  199. * OK, we're invoking a handler
  200. */
  201. static void
  202. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  203. {
  204. int ret;
  205. /* Are we from a system call? */
  206. if (regs->syscall_nr >= 0) {
  207. /* If so, check system call restarting.. */
  208. switch (regs->r0) {
  209. case -ERESTART_RESTARTBLOCK:
  210. case -ERESTARTNOHAND:
  211. regs->r0 = -EINTR;
  212. break;
  213. case -ERESTARTSYS:
  214. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  215. regs->r0 = -EINTR;
  216. break;
  217. }
  218. /* fallthrough */
  219. case -ERESTARTNOINTR:
  220. regs->r0 = regs->orig_r0;
  221. if (prev_insn(regs) < 0)
  222. return;
  223. }
  224. }
  225. /* Set up the stack frame */
  226. ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
  227. signal_setup_done(ret, ksig, 0);
  228. }
  229. /*
  230. * Note that 'init' is a special process: it doesn't get signals it doesn't
  231. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  232. * mistake.
  233. */
  234. static void do_signal(struct pt_regs *regs)
  235. {
  236. struct ksignal ksig;
  237. /*
  238. * We want the common case to go fast, which
  239. * is why we may in certain cases get here from
  240. * kernel mode. Just return without doing anything
  241. * if so.
  242. */
  243. if (!user_mode(regs))
  244. return;
  245. if (get_signal(&ksig)) {
  246. /* Re-enable any watchpoints before delivering the
  247. * signal to user space. The processor register will
  248. * have been cleared if the watchpoint triggered
  249. * inside the kernel.
  250. */
  251. /* Whee! Actually deliver the signal. */
  252. handle_signal(&ksig, regs);
  253. return;
  254. }
  255. /* Did we come from a system call? */
  256. if (regs->syscall_nr >= 0) {
  257. /* Restart the system call - no handlers present */
  258. if (regs->r0 == -ERESTARTNOHAND ||
  259. regs->r0 == -ERESTARTSYS ||
  260. regs->r0 == -ERESTARTNOINTR) {
  261. regs->r0 = regs->orig_r0;
  262. prev_insn(regs);
  263. } else if (regs->r0 == -ERESTART_RESTARTBLOCK){
  264. regs->r0 = regs->orig_r0;
  265. regs->r7 = __NR_restart_syscall;
  266. prev_insn(regs);
  267. }
  268. }
  269. restore_saved_sigmask();
  270. }
  271. /*
  272. * notification of userspace execution resumption
  273. * - triggered by current->work.notify_resume
  274. */
  275. void do_notify_resume(struct pt_regs *regs, __u32 thread_info_flags)
  276. {
  277. /* Pending single-step? */
  278. if (thread_info_flags & _TIF_SINGLESTEP)
  279. clear_thread_flag(TIF_SINGLESTEP);
  280. /* deal with pending signal delivery */
  281. if (thread_info_flags & _TIF_SIGPENDING)
  282. do_signal(regs);
  283. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  284. clear_thread_flag(TIF_NOTIFY_RESUME);
  285. tracehook_notify_resume(regs);
  286. }
  287. }