signal.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/freezer.h>
  23. #include <linux/tracehook.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/ucontext.h>
  26. #include <asm/uaccess.h>
  27. #define DEBUG_SIG 0
  28. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  29. asmlinkage int
  30. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  31. unsigned long r2, unsigned long r3, unsigned long r4,
  32. unsigned long r5, unsigned long r6, struct pt_regs *regs)
  33. {
  34. return do_sigaltstack(uss, uoss, regs->spu);
  35. }
  36. /*
  37. * Do a signal return; undo the signal stack.
  38. */
  39. struct rt_sigframe
  40. {
  41. int sig;
  42. struct siginfo __user *pinfo;
  43. void __user *puc;
  44. struct siginfo info;
  45. struct ucontext uc;
  46. // struct _fpstate fpstate;
  47. };
  48. static int
  49. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
  50. int *r0_p)
  51. {
  52. unsigned int err = 0;
  53. /* Always make any pending restarted system calls return -EINTR */
  54. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  55. #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
  56. COPY(r4);
  57. COPY(r5);
  58. COPY(r6);
  59. COPY(pt_regs);
  60. /* COPY(r0); Skip r0 */
  61. COPY(r1);
  62. COPY(r2);
  63. COPY(r3);
  64. COPY(r7);
  65. COPY(r8);
  66. COPY(r9);
  67. COPY(r10);
  68. COPY(r11);
  69. COPY(r12);
  70. COPY(acc0h);
  71. COPY(acc0l);
  72. COPY(acc1h); /* ISA_DSP_LEVEL2 only */
  73. COPY(acc1l); /* ISA_DSP_LEVEL2 only */
  74. COPY(psw);
  75. COPY(bpc);
  76. COPY(bbpsw);
  77. COPY(bbpc);
  78. COPY(spu);
  79. COPY(fp);
  80. COPY(lr);
  81. COPY(spi);
  82. #undef COPY
  83. regs->syscall_nr = -1; /* disable syscall checks */
  84. err |= __get_user(*r0_p, &sc->sc_r0);
  85. return err;
  86. }
  87. asmlinkage int
  88. sys_rt_sigreturn(unsigned long r0, unsigned long r1,
  89. unsigned long r2, unsigned long r3, unsigned long r4,
  90. unsigned long r5, unsigned long r6, struct pt_regs *regs)
  91. {
  92. struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
  93. sigset_t set;
  94. int result;
  95. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  96. goto badframe;
  97. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  98. goto badframe;
  99. sigdelsetmask(&set, ~_BLOCKABLE);
  100. spin_lock_irq(&current->sighand->siglock);
  101. current->blocked = set;
  102. recalc_sigpending();
  103. spin_unlock_irq(&current->sighand->siglock);
  104. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
  105. goto badframe;
  106. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->spu) == -EFAULT)
  107. goto badframe;
  108. return result;
  109. badframe:
  110. force_sig(SIGSEGV, current);
  111. return 0;
  112. }
  113. /*
  114. * Set up a signal frame.
  115. */
  116. static int
  117. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  118. unsigned long mask)
  119. {
  120. int err = 0;
  121. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  122. COPY(r4);
  123. COPY(r5);
  124. COPY(r6);
  125. COPY(pt_regs);
  126. COPY(r0);
  127. COPY(r1);
  128. COPY(r2);
  129. COPY(r3);
  130. COPY(r7);
  131. COPY(r8);
  132. COPY(r9);
  133. COPY(r10);
  134. COPY(r11);
  135. COPY(r12);
  136. COPY(acc0h);
  137. COPY(acc0l);
  138. COPY(acc1h); /* ISA_DSP_LEVEL2 only */
  139. COPY(acc1l); /* ISA_DSP_LEVEL2 only */
  140. COPY(psw);
  141. COPY(bpc);
  142. COPY(bbpsw);
  143. COPY(bbpc);
  144. COPY(spu);
  145. COPY(fp);
  146. COPY(lr);
  147. COPY(spi);
  148. #undef COPY
  149. err |= __put_user(mask, &sc->oldmask);
  150. return err;
  151. }
  152. /*
  153. * Determine which stack to use..
  154. */
  155. static inline void __user *
  156. get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
  157. {
  158. /* This is the X/Open sanctioned signal stack switching. */
  159. if (ka->sa.sa_flags & SA_ONSTACK) {
  160. if (sas_ss_flags(sp) == 0)
  161. sp = current->sas_ss_sp + current->sas_ss_size;
  162. }
  163. return (void __user *)((sp - frame_size) & -8ul);
  164. }
  165. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  166. sigset_t *set, struct pt_regs *regs)
  167. {
  168. struct rt_sigframe __user *frame;
  169. int err = 0;
  170. int signal;
  171. frame = get_sigframe(ka, regs->spu, sizeof(*frame));
  172. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  173. goto give_sigsegv;
  174. signal = current_thread_info()->exec_domain
  175. && current_thread_info()->exec_domain->signal_invmap
  176. && sig < 32
  177. ? current_thread_info()->exec_domain->signal_invmap[sig]
  178. : sig;
  179. err |= __put_user(signal, &frame->sig);
  180. if (err)
  181. goto give_sigsegv;
  182. err |= __put_user(&frame->info, &frame->pinfo);
  183. err |= __put_user(&frame->uc, &frame->puc);
  184. err |= copy_siginfo_to_user(&frame->info, info);
  185. if (err)
  186. goto give_sigsegv;
  187. /* Create the ucontext. */
  188. err |= __put_user(0, &frame->uc.uc_flags);
  189. err |= __put_user(0, &frame->uc.uc_link);
  190. err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  191. err |= __put_user(sas_ss_flags(regs->spu),
  192. &frame->uc.uc_stack.ss_flags);
  193. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  194. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  195. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  196. if (err)
  197. goto give_sigsegv;
  198. /* Set up to return from userspace. */
  199. regs->lr = (unsigned long)ka->sa.sa_restorer;
  200. /* Set up registers for signal handler */
  201. regs->spu = (unsigned long)frame;
  202. regs->r0 = signal; /* Arg for signal handler */
  203. regs->r1 = (unsigned long)&frame->info;
  204. regs->r2 = (unsigned long)&frame->uc;
  205. regs->bpc = (unsigned long)ka->sa.sa_handler;
  206. set_fs(USER_DS);
  207. #if DEBUG_SIG
  208. printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
  209. current->comm, current->pid, frame, regs->pc);
  210. #endif
  211. return 0;
  212. give_sigsegv:
  213. force_sigsegv(sig, current);
  214. return -EFAULT;
  215. }
  216. static int prev_insn(struct pt_regs *regs)
  217. {
  218. u16 inst;
  219. if (get_user(inst, (u16 __user *)(regs->bpc - 2)))
  220. return -EFAULT;
  221. if ((inst & 0xfff0) == 0x10f0) /* trap ? */
  222. regs->bpc -= 2;
  223. else
  224. regs->bpc -= 4;
  225. regs->syscall_nr = -1;
  226. return 0;
  227. }
  228. /*
  229. * OK, we're invoking a handler
  230. */
  231. static int
  232. handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
  233. sigset_t *oldset, struct pt_regs *regs)
  234. {
  235. /* Are we from a system call? */
  236. if (regs->syscall_nr >= 0) {
  237. /* If so, check system call restarting.. */
  238. switch (regs->r0) {
  239. case -ERESTART_RESTARTBLOCK:
  240. case -ERESTARTNOHAND:
  241. regs->r0 = -EINTR;
  242. break;
  243. case -ERESTARTSYS:
  244. if (!(ka->sa.sa_flags & SA_RESTART)) {
  245. regs->r0 = -EINTR;
  246. break;
  247. }
  248. /* fallthrough */
  249. case -ERESTARTNOINTR:
  250. regs->r0 = regs->orig_r0;
  251. if (prev_insn(regs) < 0)
  252. return -EFAULT;
  253. }
  254. }
  255. /* Set up the stack frame */
  256. if (setup_rt_frame(sig, ka, info, oldset, regs))
  257. return -EFAULT;
  258. spin_lock_irq(&current->sighand->siglock);
  259. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  260. if (!(ka->sa.sa_flags & SA_NODEFER))
  261. sigaddset(&current->blocked,sig);
  262. recalc_sigpending();
  263. spin_unlock_irq(&current->sighand->siglock);
  264. return 0;
  265. }
  266. /*
  267. * Note that 'init' is a special process: it doesn't get signals it doesn't
  268. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  269. * mistake.
  270. */
  271. static void do_signal(struct pt_regs *regs)
  272. {
  273. siginfo_t info;
  274. int signr;
  275. struct k_sigaction ka;
  276. sigset_t *oldset;
  277. /*
  278. * We want the common case to go fast, which
  279. * is why we may in certain cases get here from
  280. * kernel mode. Just return without doing anything
  281. * if so.
  282. */
  283. if (!user_mode(regs))
  284. return;
  285. if (try_to_freeze())
  286. goto no_signal;
  287. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  288. oldset = &current->saved_sigmask;
  289. else
  290. oldset = &current->blocked;
  291. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  292. if (signr > 0) {
  293. /* Re-enable any watchpoints before delivering the
  294. * signal to user space. The processor register will
  295. * have been cleared if the watchpoint triggered
  296. * inside the kernel.
  297. */
  298. /* Whee! Actually deliver the signal. */
  299. if (handle_signal(signr, &ka, &info, oldset, regs) == 0)
  300. clear_thread_flag(TIF_RESTORE_SIGMASK);
  301. return;
  302. }
  303. no_signal:
  304. /* Did we come from a system call? */
  305. if (regs->syscall_nr >= 0) {
  306. /* Restart the system call - no handlers present */
  307. if (regs->r0 == -ERESTARTNOHAND ||
  308. regs->r0 == -ERESTARTSYS ||
  309. regs->r0 == -ERESTARTNOINTR) {
  310. regs->r0 = regs->orig_r0;
  311. prev_insn(regs);
  312. } else if (regs->r0 == -ERESTART_RESTARTBLOCK){
  313. regs->r0 = regs->orig_r0;
  314. regs->r7 = __NR_restart_syscall;
  315. prev_insn(regs);
  316. }
  317. }
  318. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  319. clear_thread_flag(TIF_RESTORE_SIGMASK);
  320. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  321. }
  322. }
  323. /*
  324. * notification of userspace execution resumption
  325. * - triggered by current->work.notify_resume
  326. */
  327. void do_notify_resume(struct pt_regs *regs, __u32 thread_info_flags)
  328. {
  329. /* Pending single-step? */
  330. if (thread_info_flags & _TIF_SINGLESTEP)
  331. clear_thread_flag(TIF_SINGLESTEP);
  332. /* deal with pending signal delivery */
  333. if (thread_info_flags & _TIF_SIGPENDING)
  334. do_signal(regs);
  335. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  336. clear_thread_flag(TIF_NOTIFY_RESUME);
  337. tracehook_notify_resume(regs);
  338. if (current->replacement_session_keyring)
  339. key_replace_session_keyring();
  340. }
  341. clear_thread_flag(TIF_IRET);
  342. }