signal.c 9.5 KB

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