signal.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. COPY(dummy_acc1h);
  106. COPY(dummy_acc1l);
  107. #else
  108. #error unknown isa configuration
  109. #endif
  110. COPY(psw);
  111. COPY(bpc);
  112. COPY(bbpsw);
  113. COPY(bbpc);
  114. COPY(spu);
  115. COPY(fp);
  116. COPY(lr);
  117. COPY(spi);
  118. #undef COPY
  119. regs->syscall_nr = -1; /* disable syscall checks */
  120. err |= __get_user(*r0_p, &sc->sc_r0);
  121. return err;
  122. }
  123. asmlinkage int
  124. sys_rt_sigreturn(unsigned long r0, unsigned long r1,
  125. unsigned long r2, unsigned long r3, unsigned long r4,
  126. unsigned long r5, unsigned long r6, struct pt_regs *regs)
  127. {
  128. struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
  129. sigset_t set;
  130. int result;
  131. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  132. goto badframe;
  133. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  134. goto badframe;
  135. sigdelsetmask(&set, ~_BLOCKABLE);
  136. spin_lock_irq(&current->sighand->siglock);
  137. current->blocked = set;
  138. recalc_sigpending();
  139. spin_unlock_irq(&current->sighand->siglock);
  140. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
  141. goto badframe;
  142. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->spu) == -EFAULT)
  143. goto badframe;
  144. return result;
  145. badframe:
  146. force_sig(SIGSEGV, current);
  147. return 0;
  148. }
  149. /*
  150. * Set up a signal frame.
  151. */
  152. static int
  153. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  154. unsigned long mask)
  155. {
  156. int err = 0;
  157. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  158. COPY(r4);
  159. COPY(r5);
  160. COPY(r6);
  161. COPY(pt_regs);
  162. COPY(r0);
  163. COPY(r1);
  164. COPY(r2);
  165. COPY(r3);
  166. COPY(r7);
  167. COPY(r8);
  168. COPY(r9);
  169. COPY(r10);
  170. COPY(r11);
  171. COPY(r12);
  172. #if defined(CONFIG_ISA_M32R2) && defined(CONFIG_ISA_DSP_LEVEL2)
  173. COPY(acc0h);
  174. COPY(acc0l);
  175. COPY(acc1h);
  176. COPY(acc1l);
  177. #elif defined(CONFIG_ISA_M32R2) || defined(CONFIG_ISA_M32R)
  178. COPY(acch);
  179. COPY(accl);
  180. COPY(dummy_acc1h);
  181. COPY(dummy_acc1l);
  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. spin_lock_irq(&current->sighand->siglock);
  294. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  295. if (!(ka->sa.sa_flags & SA_NODEFER))
  296. sigaddset(&current->blocked,sig);
  297. recalc_sigpending();
  298. spin_unlock_irq(&current->sighand->siglock);
  299. }
  300. /*
  301. * Note that 'init' is a special process: it doesn't get signals it doesn't
  302. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  303. * mistake.
  304. */
  305. int do_signal(struct pt_regs *regs, sigset_t *oldset)
  306. {
  307. siginfo_t info;
  308. int signr;
  309. struct k_sigaction ka;
  310. unsigned short inst;
  311. /*
  312. * We want the common case to go fast, which
  313. * is why we may in certain cases get here from
  314. * kernel mode. Just return without doing anything
  315. * if so.
  316. */
  317. if (!user_mode(regs))
  318. return 1;
  319. if (try_to_freeze())
  320. goto no_signal;
  321. if (!oldset)
  322. oldset = &current->blocked;
  323. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  324. if (signr > 0) {
  325. /* Reenable any watchpoints before delivering the
  326. * signal to user space. The processor register will
  327. * have been cleared if the watchpoint triggered
  328. * inside the kernel.
  329. */
  330. /* Whee! Actually deliver the signal. */
  331. handle_signal(signr, &ka, &info, oldset, regs);
  332. return 1;
  333. }
  334. no_signal:
  335. /* Did we come from a system call? */
  336. if (regs->syscall_nr >= 0) {
  337. /* Restart the system call - no handlers present */
  338. if (regs->r0 == -ERESTARTNOHAND ||
  339. regs->r0 == -ERESTARTSYS ||
  340. regs->r0 == -ERESTARTNOINTR) {
  341. regs->r0 = regs->orig_r0;
  342. inst = *(unsigned short *)(regs->bpc - 2);
  343. if ((inst & 0xfff0) == 0x10f0) /* trap ? */
  344. regs->bpc -= 2;
  345. else
  346. regs->bpc -= 4;
  347. }
  348. if (regs->r0 == -ERESTART_RESTARTBLOCK){
  349. regs->r0 = regs->orig_r0;
  350. regs->r7 = __NR_restart_syscall;
  351. inst = *(unsigned short *)(regs->bpc - 2);
  352. if ((inst & 0xfff0) == 0x10f0) /* trap ? */
  353. regs->bpc -= 2;
  354. else
  355. regs->bpc -= 4;
  356. }
  357. }
  358. return 0;
  359. }
  360. /*
  361. * notification of userspace execution resumption
  362. * - triggered by current->work.notify_resume
  363. */
  364. void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
  365. __u32 thread_info_flags)
  366. {
  367. /* Pending single-step? */
  368. if (thread_info_flags & _TIF_SINGLESTEP)
  369. clear_thread_flag(TIF_SINGLESTEP);
  370. /* deal with pending signal delivery */
  371. if (thread_info_flags & _TIF_SIGPENDING)
  372. do_signal(regs,oldset);
  373. clear_thread_flag(TIF_IRET);
  374. }