signal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* MN10300 Signal handling
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/kernel.h>
  15. #include <linux/signal.h>
  16. #include <linux/errno.h>
  17. #include <linux/wait.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/unistd.h>
  20. #include <linux/stddef.h>
  21. #include <linux/tty.h>
  22. #include <linux/personality.h>
  23. #include <linux/suspend.h>
  24. #include <linux/tracehook.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/ucontext.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/fpu.h>
  29. #include "sigframe.h"
  30. #define DEBUG_SIG 0
  31. /*
  32. * do a signal return; undo the signal stack.
  33. */
  34. static int restore_sigcontext(struct pt_regs *regs,
  35. struct sigcontext __user *sc, long *_d0)
  36. {
  37. unsigned int err = 0;
  38. /* Always make any pending restarted system calls return -EINTR */
  39. current->restart_block.fn = do_no_restart_syscall;
  40. if (is_using_fpu(current))
  41. fpu_kill_state(current);
  42. #define COPY(x) err |= __get_user(regs->x, &sc->x)
  43. COPY(d1); COPY(d2); COPY(d3);
  44. COPY(a0); COPY(a1); COPY(a2); COPY(a3);
  45. COPY(e0); COPY(e1); COPY(e2); COPY(e3);
  46. COPY(e4); COPY(e5); COPY(e6); COPY(e7);
  47. COPY(lar); COPY(lir);
  48. COPY(mdr); COPY(mdrq);
  49. COPY(mcvf); COPY(mcrl); COPY(mcrh);
  50. COPY(sp); COPY(pc);
  51. #undef COPY
  52. {
  53. unsigned int tmpflags;
  54. #ifndef CONFIG_MN10300_USING_JTAG
  55. #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
  56. EPSW_T | EPSW_nAR)
  57. #else
  58. #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
  59. EPSW_nAR)
  60. #endif
  61. err |= __get_user(tmpflags, &sc->epsw);
  62. regs->epsw = (regs->epsw & ~USER_EPSW) |
  63. (tmpflags & USER_EPSW);
  64. regs->orig_d0 = -1; /* disable syscall checks */
  65. }
  66. {
  67. struct fpucontext *buf;
  68. err |= __get_user(buf, &sc->fpucontext);
  69. if (buf) {
  70. if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
  71. goto badframe;
  72. err |= fpu_restore_sigcontext(buf);
  73. }
  74. }
  75. err |= __get_user(*_d0, &sc->d0);
  76. return err;
  77. badframe:
  78. return 1;
  79. }
  80. /*
  81. * standard signal return syscall
  82. */
  83. asmlinkage long sys_sigreturn(void)
  84. {
  85. struct sigframe __user *frame;
  86. sigset_t set;
  87. long d0;
  88. frame = (struct sigframe __user *) current_frame()->sp;
  89. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  90. goto badframe;
  91. if (__get_user(set.sig[0], &frame->sc.oldmask))
  92. goto badframe;
  93. if (_NSIG_WORDS > 1 &&
  94. __copy_from_user(&set.sig[1], &frame->extramask,
  95. sizeof(frame->extramask)))
  96. goto badframe;
  97. set_current_blocked(&set);
  98. if (restore_sigcontext(current_frame(), &frame->sc, &d0))
  99. goto badframe;
  100. return d0;
  101. badframe:
  102. force_sig(SIGSEGV, current);
  103. return 0;
  104. }
  105. /*
  106. * realtime signal return syscall
  107. */
  108. asmlinkage long sys_rt_sigreturn(void)
  109. {
  110. struct rt_sigframe __user *frame;
  111. sigset_t set;
  112. long d0;
  113. frame = (struct rt_sigframe __user *) current_frame()->sp;
  114. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  115. goto badframe;
  116. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  117. goto badframe;
  118. set_current_blocked(&set);
  119. if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
  120. goto badframe;
  121. if (restore_altstack(&frame->uc.uc_stack))
  122. goto badframe;
  123. return d0;
  124. badframe:
  125. force_sig(SIGSEGV, current);
  126. return 0;
  127. }
  128. /*
  129. * store the userspace context into a signal frame
  130. */
  131. static int setup_sigcontext(struct sigcontext __user *sc,
  132. struct fpucontext *fpuctx,
  133. struct pt_regs *regs,
  134. unsigned long mask)
  135. {
  136. int tmp, err = 0;
  137. #define COPY(x) err |= __put_user(regs->x, &sc->x)
  138. COPY(d0); COPY(d1); COPY(d2); COPY(d3);
  139. COPY(a0); COPY(a1); COPY(a2); COPY(a3);
  140. COPY(e0); COPY(e1); COPY(e2); COPY(e3);
  141. COPY(e4); COPY(e5); COPY(e6); COPY(e7);
  142. COPY(lar); COPY(lir);
  143. COPY(mdr); COPY(mdrq);
  144. COPY(mcvf); COPY(mcrl); COPY(mcrh);
  145. COPY(sp); COPY(epsw); COPY(pc);
  146. #undef COPY
  147. tmp = fpu_setup_sigcontext(fpuctx);
  148. if (tmp < 0)
  149. err = 1;
  150. else
  151. err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
  152. /* non-iBCS2 extensions.. */
  153. err |= __put_user(mask, &sc->oldmask);
  154. return err;
  155. }
  156. /*
  157. * determine which stack to use..
  158. */
  159. static inline void __user *get_sigframe(struct ksignal *ksig,
  160. struct pt_regs *regs,
  161. size_t frame_size)
  162. {
  163. unsigned long sp = sigsp(regs->sp, ksig);
  164. return (void __user *) ((sp - frame_size) & ~7UL);
  165. }
  166. /*
  167. * set up a normal signal frame
  168. */
  169. static int setup_frame(struct ksignal *ksig, sigset_t *set,
  170. struct pt_regs *regs)
  171. {
  172. struct sigframe __user *frame;
  173. int sig = ksig->sig;
  174. frame = get_sigframe(ksig, regs, sizeof(*frame));
  175. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  176. return -EFAULT;
  177. if (__put_user(sig, &frame->sig) < 0 ||
  178. __put_user(&frame->sc, &frame->psc) < 0)
  179. return -EFAULT;
  180. if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
  181. return -EFAULT;
  182. if (_NSIG_WORDS > 1) {
  183. if (__copy_to_user(frame->extramask, &set->sig[1],
  184. sizeof(frame->extramask)))
  185. return -EFAULT;
  186. }
  187. /* set up to return from userspace. If provided, use a stub already in
  188. * userspace */
  189. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  190. if (__put_user(ksig->ka.sa.sa_restorer, &frame->pretcode))
  191. return -EFAULT;
  192. } else {
  193. if (__put_user((void (*)(void))frame->retcode,
  194. &frame->pretcode))
  195. return -EFAULT;
  196. /* this is mov $,d0; syscall 0 */
  197. if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
  198. __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
  199. __put_user(0x00, (char *)(frame->retcode + 2)) ||
  200. __put_user(0xf0, (char *)(frame->retcode + 3)) ||
  201. __put_user(0xe0, (char *)(frame->retcode + 4)))
  202. return -EFAULT;
  203. flush_icache_range((unsigned long) frame->retcode,
  204. (unsigned long) frame->retcode + 5);
  205. }
  206. /* set up registers for signal handler */
  207. regs->sp = (unsigned long) frame;
  208. regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  209. regs->d0 = sig;
  210. regs->d1 = (unsigned long) &frame->sc;
  211. #if DEBUG_SIG
  212. printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  213. sig, current->comm, current->pid, frame, regs->pc,
  214. frame->pretcode);
  215. #endif
  216. return 0;
  217. }
  218. /*
  219. * set up a realtime signal frame
  220. */
  221. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  222. struct pt_regs *regs)
  223. {
  224. struct rt_sigframe __user *frame;
  225. int sig = ksig->sig;
  226. frame = get_sigframe(ksig, regs, sizeof(*frame));
  227. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  228. return -EFAULT;
  229. if (__put_user(sig, &frame->sig) ||
  230. __put_user(&frame->info, &frame->pinfo) ||
  231. __put_user(&frame->uc, &frame->puc) ||
  232. copy_siginfo_to_user(&frame->info, &ksig->info))
  233. return -EFAULT;
  234. /* create the ucontext. */
  235. if (__put_user(0, &frame->uc.uc_flags) ||
  236. __put_user(0, &frame->uc.uc_link) ||
  237. __save_altstack(&frame->uc.uc_stack, regs->sp) ||
  238. setup_sigcontext(&frame->uc.uc_mcontext,
  239. &frame->fpuctx, regs, set->sig[0]) ||
  240. __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
  241. return -EFAULT;
  242. /* set up to return from userspace. If provided, use a stub already in
  243. * userspace */
  244. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  245. if (__put_user(ksig->ka.sa.sa_restorer, &frame->pretcode))
  246. return -EFAULT;
  247. } else {
  248. if (__put_user((void(*)(void))frame->retcode,
  249. &frame->pretcode) ||
  250. /* This is mov $,d0; syscall 0 */
  251. __put_user(0x2c, (char *)(frame->retcode + 0)) ||
  252. __put_user(__NR_rt_sigreturn,
  253. (char *)(frame->retcode + 1)) ||
  254. __put_user(0x00, (char *)(frame->retcode + 2)) ||
  255. __put_user(0xf0, (char *)(frame->retcode + 3)) ||
  256. __put_user(0xe0, (char *)(frame->retcode + 4)))
  257. return -EFAULT;
  258. flush_icache_range((u_long) frame->retcode,
  259. (u_long) frame->retcode + 5);
  260. }
  261. /* Set up registers for signal handler */
  262. regs->sp = (unsigned long) frame;
  263. regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  264. regs->d0 = sig;
  265. regs->d1 = (long) &frame->info;
  266. #if DEBUG_SIG
  267. printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  268. sig, current->comm, current->pid, frame, regs->pc,
  269. frame->pretcode);
  270. #endif
  271. return 0;
  272. }
  273. static inline void stepback(struct pt_regs *regs)
  274. {
  275. regs->pc -= 2;
  276. regs->orig_d0 = -1;
  277. }
  278. /*
  279. * handle the actual delivery of a signal to userspace
  280. */
  281. static int handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  282. {
  283. sigset_t *oldset = sigmask_to_save();
  284. int ret;
  285. /* Are we from a system call? */
  286. if (regs->orig_d0 >= 0) {
  287. /* If so, check system call restarting.. */
  288. switch (regs->d0) {
  289. case -ERESTART_RESTARTBLOCK:
  290. case -ERESTARTNOHAND:
  291. regs->d0 = -EINTR;
  292. break;
  293. case -ERESTARTSYS:
  294. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  295. regs->d0 = -EINTR;
  296. break;
  297. }
  298. /* fallthrough */
  299. case -ERESTARTNOINTR:
  300. regs->d0 = regs->orig_d0;
  301. stepback(regs);
  302. }
  303. }
  304. /* Set up the stack frame */
  305. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  306. ret = setup_rt_frame(ksig, oldset, regs);
  307. else
  308. ret = setup_frame(ksig, oldset, regs);
  309. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  310. return 0;
  311. }
  312. /*
  313. * handle a potential signal
  314. */
  315. static void do_signal(struct pt_regs *regs)
  316. {
  317. struct ksignal ksig;
  318. if (get_signal(&ksig)) {
  319. handle_signal(&ksig, regs);
  320. return;
  321. }
  322. /* did we come from a system call? */
  323. if (regs->orig_d0 >= 0) {
  324. /* restart the system call - no handlers present */
  325. switch (regs->d0) {
  326. case -ERESTARTNOHAND:
  327. case -ERESTARTSYS:
  328. case -ERESTARTNOINTR:
  329. regs->d0 = regs->orig_d0;
  330. stepback(regs);
  331. break;
  332. case -ERESTART_RESTARTBLOCK:
  333. regs->d0 = __NR_restart_syscall;
  334. stepback(regs);
  335. break;
  336. }
  337. }
  338. /* if there's no signal to deliver, we just put the saved sigmask
  339. * back */
  340. restore_saved_sigmask();
  341. }
  342. /*
  343. * notification of userspace execution resumption
  344. * - triggered by current->work.notify_resume
  345. */
  346. asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
  347. {
  348. /* Pending single-step? */
  349. if (thread_info_flags & _TIF_SINGLESTEP) {
  350. #ifndef CONFIG_MN10300_USING_JTAG
  351. regs->epsw |= EPSW_T;
  352. clear_thread_flag(TIF_SINGLESTEP);
  353. #else
  354. BUG(); /* no h/w single-step if using JTAG unit */
  355. #endif
  356. }
  357. /* deal with pending signal delivery */
  358. if (thread_info_flags & _TIF_SIGPENDING)
  359. do_signal(regs);
  360. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  361. clear_thread_flag(TIF_NOTIFY_RESUME);
  362. tracehook_notify_resume(current_frame());
  363. }
  364. }