signal.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. stack_t st;
  129. int result;
  130. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  131. goto badframe;
  132. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  133. goto badframe;
  134. sigdelsetmask(&set, ~_BLOCKABLE);
  135. spin_lock_irq(&current->sighand->siglock);
  136. current->blocked = set;
  137. recalc_sigpending();
  138. spin_unlock_irq(&current->sighand->siglock);
  139. if (restore_sigcontext(&regs, &frame->uc.uc_mcontext, &result))
  140. goto badframe;
  141. if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
  142. goto badframe;
  143. /* It is more difficult to avoid calling this function than to
  144. call it and ignore errors. */
  145. do_sigaltstack(&st, NULL, regs.spu);
  146. return result;
  147. badframe:
  148. force_sig(SIGSEGV, current);
  149. return 0;
  150. }
  151. /*
  152. * Set up a signal frame.
  153. */
  154. static int
  155. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  156. unsigned long mask)
  157. {
  158. int err = 0;
  159. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  160. COPY(r4);
  161. COPY(r5);
  162. COPY(r6);
  163. COPY(pt_regs);
  164. COPY(r0);
  165. COPY(r1);
  166. COPY(r2);
  167. COPY(r3);
  168. COPY(r7);
  169. COPY(r8);
  170. COPY(r9);
  171. COPY(r10);
  172. COPY(r11);
  173. COPY(r12);
  174. #if defined(CONFIG_ISA_M32R2) && defined(CONFIG_ISA_DSP_LEVEL2)
  175. COPY(acc0h);
  176. COPY(acc0l);
  177. COPY(acc1h);
  178. COPY(acc1l);
  179. #elif defined(CONFIG_ISA_M32R2) || defined(CONFIG_ISA_M32R)
  180. COPY(acch);
  181. COPY(accl);
  182. #else
  183. #error unknown isa configuration
  184. #endif
  185. COPY(psw);
  186. COPY(bpc);
  187. COPY(bbpsw);
  188. COPY(bbpc);
  189. COPY(spu);
  190. COPY(fp);
  191. COPY(lr);
  192. COPY(spi);
  193. #undef COPY
  194. err |= __put_user(mask, &sc->oldmask);
  195. return err;
  196. }
  197. /*
  198. * Determine which stack to use..
  199. */
  200. static inline void __user *
  201. get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
  202. {
  203. /* This is the X/Open sanctioned signal stack switching. */
  204. if (ka->sa.sa_flags & SA_ONSTACK) {
  205. if (sas_ss_flags(sp) == 0)
  206. sp = current->sas_ss_sp + current->sas_ss_size;
  207. }
  208. return (void __user *)((sp - frame_size) & -8ul);
  209. }
  210. static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  211. sigset_t *set, struct pt_regs *regs)
  212. {
  213. struct rt_sigframe __user *frame;
  214. int err = 0;
  215. int signal;
  216. frame = get_sigframe(ka, regs->spu, sizeof(*frame));
  217. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  218. goto give_sigsegv;
  219. signal = current_thread_info()->exec_domain
  220. && current_thread_info()->exec_domain->signal_invmap
  221. && sig < 32
  222. ? current_thread_info()->exec_domain->signal_invmap[sig]
  223. : sig;
  224. err |= __put_user(signal, &frame->sig);
  225. if (err)
  226. goto give_sigsegv;
  227. err |= __put_user(&frame->info, &frame->pinfo);
  228. err |= __put_user(&frame->uc, &frame->puc);
  229. err |= copy_siginfo_to_user(&frame->info, info);
  230. if (err)
  231. goto give_sigsegv;
  232. /* Create the ucontext. */
  233. err |= __put_user(0, &frame->uc.uc_flags);
  234. err |= __put_user(0, &frame->uc.uc_link);
  235. err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  236. err |= __put_user(sas_ss_flags(regs->spu),
  237. &frame->uc.uc_stack.ss_flags);
  238. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  239. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  240. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  241. if (err)
  242. goto give_sigsegv;
  243. /* Set up to return from userspace. */
  244. regs->lr = (unsigned long)ka->sa.sa_restorer;
  245. /* Set up registers for signal handler */
  246. regs->spu = (unsigned long)frame;
  247. regs->r0 = signal; /* Arg for signal handler */
  248. regs->r1 = (unsigned long)&frame->info;
  249. regs->r2 = (unsigned long)&frame->uc;
  250. regs->bpc = (unsigned long)ka->sa.sa_handler;
  251. set_fs(USER_DS);
  252. #if DEBUG_SIG
  253. printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
  254. current->comm, current->pid, frame, regs->pc);
  255. #endif
  256. return;
  257. give_sigsegv:
  258. force_sigsegv(sig, current);
  259. }
  260. /*
  261. * OK, we're invoking a handler
  262. */
  263. static void
  264. handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
  265. sigset_t *oldset, struct pt_regs *regs)
  266. {
  267. unsigned short inst;
  268. /* Are we from a system call? */
  269. if (regs->syscall_nr >= 0) {
  270. /* If so, check system call restarting.. */
  271. switch (regs->r0) {
  272. case -ERESTART_RESTARTBLOCK:
  273. case -ERESTARTNOHAND:
  274. regs->r0 = -EINTR;
  275. break;
  276. case -ERESTARTSYS:
  277. if (!(ka->sa.sa_flags & SA_RESTART)) {
  278. regs->r0 = -EINTR;
  279. break;
  280. }
  281. /* fallthrough */
  282. case -ERESTARTNOINTR:
  283. regs->r0 = regs->orig_r0;
  284. inst = *(unsigned short *)(regs->bpc - 2);
  285. if ((inst & 0xfff0) == 0x10f0) /* trap ? */
  286. regs->bpc -= 2;
  287. else
  288. regs->bpc -= 4;
  289. }
  290. }
  291. /* Set up the stack frame */
  292. setup_rt_frame(sig, ka, info, oldset, regs);
  293. if (!(ka->sa.sa_flags & SA_NODEFER)) {
  294. spin_lock_irq(&current->sighand->siglock);
  295. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  296. sigaddset(&current->blocked,sig);
  297. recalc_sigpending();
  298. spin_unlock_irq(&current->sighand->siglock);
  299. }
  300. }
  301. /*
  302. * Note that 'init' is a special process: it doesn't get signals it doesn't
  303. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  304. * mistake.
  305. */
  306. int do_signal(struct pt_regs *regs, sigset_t *oldset)
  307. {
  308. siginfo_t info;
  309. int signr;
  310. struct k_sigaction ka;
  311. unsigned short inst;
  312. /*
  313. * We want the common case to go fast, which
  314. * is why we may in certain cases get here from
  315. * kernel mode. Just return without doing anything
  316. * if so.
  317. */
  318. if (!user_mode(regs))
  319. return 1;
  320. if (current->flags & PF_FREEZE) {
  321. refrigerator(0);
  322. goto no_signal;
  323. }
  324. if (!oldset)
  325. oldset = &current->blocked;
  326. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  327. if (signr > 0) {
  328. /* Reenable any watchpoints before delivering the
  329. * signal to user space. The processor register will
  330. * have been cleared if the watchpoint triggered
  331. * inside the kernel.
  332. */
  333. /* Whee! Actually deliver the signal. */
  334. handle_signal(signr, &ka, &info, oldset, regs);
  335. return 1;
  336. }
  337. no_signal:
  338. /* Did we come from a system call? */
  339. if (regs->syscall_nr >= 0) {
  340. /* Restart the system call - no handlers present */
  341. if (regs->r0 == -ERESTARTNOHAND ||
  342. regs->r0 == -ERESTARTSYS ||
  343. regs->r0 == -ERESTARTNOINTR) {
  344. regs->r0 = regs->orig_r0;
  345. inst = *(unsigned short *)(regs->bpc - 2);
  346. if ((inst & 0xfff0) == 0x10f0) /* trap ? */
  347. regs->bpc -= 2;
  348. else
  349. regs->bpc -= 4;
  350. }
  351. if (regs->r0 == -ERESTART_RESTARTBLOCK){
  352. regs->r0 = regs->orig_r0;
  353. regs->r7 = __NR_restart_syscall;
  354. inst = *(unsigned short *)(regs->bpc - 2);
  355. if ((inst & 0xfff0) == 0x10f0) /* trap ? */
  356. regs->bpc -= 2;
  357. else
  358. regs->bpc -= 4;
  359. }
  360. }
  361. return 0;
  362. }
  363. /*
  364. * notification of userspace execution resumption
  365. * - triggered by current->work.notify_resume
  366. */
  367. void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
  368. __u32 thread_info_flags)
  369. {
  370. /* Pending single-step? */
  371. if (thread_info_flags & _TIF_SINGLESTEP)
  372. clear_thread_flag(TIF_SINGLESTEP);
  373. /* deal with pending signal delivery */
  374. if (thread_info_flags & _TIF_SIGPENDING)
  375. do_signal(regs,oldset);
  376. clear_thread_flag(TIF_IRET);
  377. }