signal.c 9.5 KB

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