signal.c 9.9 KB

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