signal.c 9.8 KB

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