signal.c 9.0 KB

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