signal.c 9.5 KB

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