signal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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/smp_lock.h>
  15. #include <linux/kernel.h>
  16. #include <linux/signal.h>
  17. #include <linux/errno.h>
  18. #include <linux/wait.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/unistd.h>
  21. #include <linux/stddef.h>
  22. #include <linux/tty.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. #include <asm/fpu.h>
  29. #include "sigframe.h"
  30. #define DEBUG_SIG 0
  31. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  32. /*
  33. * atomically swap in the new signal mask, and wait for a signal.
  34. */
  35. asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  36. {
  37. mask &= _BLOCKABLE;
  38. spin_lock_irq(&current->sighand->siglock);
  39. current->saved_sigmask = current->blocked;
  40. siginitset(&current->blocked, mask);
  41. recalc_sigpending();
  42. spin_unlock_irq(&current->sighand->siglock);
  43. current->state = TASK_INTERRUPTIBLE;
  44. schedule();
  45. set_thread_flag(TIF_RESTORE_SIGMASK);
  46. return -ERESTARTNOHAND;
  47. }
  48. /*
  49. * set signal action syscall
  50. */
  51. asmlinkage long sys_sigaction(int sig,
  52. const struct old_sigaction __user *act,
  53. struct old_sigaction __user *oact)
  54. {
  55. struct k_sigaction new_ka, old_ka;
  56. int ret;
  57. if (act) {
  58. old_sigset_t mask;
  59. if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
  60. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  61. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  62. return -EFAULT;
  63. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  64. __get_user(mask, &act->sa_mask);
  65. siginitset(&new_ka.sa.sa_mask, mask);
  66. }
  67. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  68. if (!ret && oact) {
  69. if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
  70. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  71. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  72. return -EFAULT;
  73. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  74. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  75. }
  76. return ret;
  77. }
  78. /*
  79. * set alternate signal stack syscall
  80. */
  81. asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
  82. {
  83. return do_sigaltstack(uss, uoss, __frame->sp);
  84. }
  85. /*
  86. * do a signal return; undo the signal stack.
  87. */
  88. static int restore_sigcontext(struct pt_regs *regs,
  89. struct sigcontext __user *sc, long *_d0)
  90. {
  91. unsigned int err = 0;
  92. if (is_using_fpu(current))
  93. fpu_kill_state(current);
  94. #define COPY(x) err |= __get_user(regs->x, &sc->x)
  95. COPY(d1); COPY(d2); COPY(d3);
  96. COPY(a0); COPY(a1); COPY(a2); COPY(a3);
  97. COPY(e0); COPY(e1); COPY(e2); COPY(e3);
  98. COPY(e4); COPY(e5); COPY(e6); COPY(e7);
  99. COPY(lar); COPY(lir);
  100. COPY(mdr); COPY(mdrq);
  101. COPY(mcvf); COPY(mcrl); COPY(mcrh);
  102. COPY(sp); COPY(pc);
  103. #undef COPY
  104. {
  105. unsigned int tmpflags;
  106. #ifndef CONFIG_MN10300_USING_JTAG
  107. #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
  108. EPSW_T | EPSW_nAR)
  109. #else
  110. #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
  111. EPSW_nAR)
  112. #endif
  113. err |= __get_user(tmpflags, &sc->epsw);
  114. regs->epsw = (regs->epsw & ~USER_EPSW) |
  115. (tmpflags & USER_EPSW);
  116. regs->orig_d0 = -1; /* disable syscall checks */
  117. }
  118. {
  119. struct fpucontext *buf;
  120. err |= __get_user(buf, &sc->fpucontext);
  121. if (buf) {
  122. if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
  123. goto badframe;
  124. err |= fpu_restore_sigcontext(buf);
  125. }
  126. }
  127. err |= __get_user(*_d0, &sc->d0);
  128. return err;
  129. badframe:
  130. return 1;
  131. }
  132. /*
  133. * standard signal return syscall
  134. */
  135. asmlinkage long sys_sigreturn(void)
  136. {
  137. struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
  138. sigset_t set;
  139. long d0;
  140. if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
  141. goto badframe;
  142. if (__get_user(set.sig[0], &frame->sc.oldmask))
  143. goto badframe;
  144. if (_NSIG_WORDS > 1 &&
  145. __copy_from_user(&set.sig[1], &frame->extramask,
  146. sizeof(frame->extramask)))
  147. goto badframe;
  148. sigdelsetmask(&set, ~_BLOCKABLE);
  149. spin_lock_irq(&current->sighand->siglock);
  150. current->blocked = set;
  151. recalc_sigpending();
  152. spin_unlock_irq(&current->sighand->siglock);
  153. if (restore_sigcontext(__frame, &frame->sc, &d0))
  154. goto badframe;
  155. return d0;
  156. badframe:
  157. force_sig(SIGSEGV, current);
  158. return 0;
  159. }
  160. /*
  161. * realtime signal return syscall
  162. */
  163. asmlinkage long sys_rt_sigreturn(void)
  164. {
  165. struct rt_sigframe __user *frame =
  166. (struct rt_sigframe __user *) __frame->sp;
  167. sigset_t set;
  168. unsigned long d0;
  169. if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
  170. goto badframe;
  171. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  172. goto badframe;
  173. sigdelsetmask(&set, ~_BLOCKABLE);
  174. spin_lock_irq(&current->sighand->siglock);
  175. current->blocked = set;
  176. recalc_sigpending();
  177. spin_unlock_irq(&current->sighand->siglock);
  178. if (restore_sigcontext(__frame, &frame->uc.uc_mcontext, &d0))
  179. goto badframe;
  180. if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
  181. goto badframe;
  182. return d0;
  183. badframe:
  184. force_sig(SIGSEGV, current);
  185. return 0;
  186. }
  187. /*
  188. * store the userspace context into a signal frame
  189. */
  190. static int setup_sigcontext(struct sigcontext __user *sc,
  191. struct fpucontext *fpuctx,
  192. struct pt_regs *regs,
  193. unsigned long mask)
  194. {
  195. int tmp, err = 0;
  196. #define COPY(x) err |= __put_user(regs->x, &sc->x)
  197. COPY(d0); COPY(d1); COPY(d2); COPY(d3);
  198. COPY(a0); COPY(a1); COPY(a2); COPY(a3);
  199. COPY(e0); COPY(e1); COPY(e2); COPY(e3);
  200. COPY(e4); COPY(e5); COPY(e6); COPY(e7);
  201. COPY(lar); COPY(lir);
  202. COPY(mdr); COPY(mdrq);
  203. COPY(mcvf); COPY(mcrl); COPY(mcrh);
  204. COPY(sp); COPY(epsw); COPY(pc);
  205. #undef COPY
  206. tmp = fpu_setup_sigcontext(fpuctx);
  207. if (tmp < 0)
  208. err = 1;
  209. else
  210. err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
  211. /* non-iBCS2 extensions.. */
  212. err |= __put_user(mask, &sc->oldmask);
  213. return err;
  214. }
  215. /*
  216. * determine which stack to use..
  217. */
  218. static inline void __user *get_sigframe(struct k_sigaction *ka,
  219. struct pt_regs *regs,
  220. size_t frame_size)
  221. {
  222. unsigned long sp;
  223. /* default to using normal stack */
  224. sp = regs->sp;
  225. /* this is the X/Open sanctioned signal stack switching. */
  226. if (ka->sa.sa_flags & SA_ONSTACK) {
  227. if (!on_sig_stack(sp))
  228. sp = current->sas_ss_sp + current->sas_ss_size;
  229. }
  230. return (void __user *) ((sp - frame_size) & ~7UL);
  231. }
  232. /*
  233. * set up a normal signal frame
  234. */
  235. static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
  236. struct pt_regs *regs)
  237. {
  238. struct sigframe __user *frame;
  239. int rsig;
  240. frame = get_sigframe(ka, regs, sizeof(*frame));
  241. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  242. goto give_sigsegv;
  243. rsig = sig;
  244. if (sig < 32 &&
  245. current_thread_info()->exec_domain &&
  246. current_thread_info()->exec_domain->signal_invmap)
  247. rsig = current_thread_info()->exec_domain->signal_invmap[sig];
  248. if (__put_user(rsig, &frame->sig) < 0 ||
  249. __put_user(&frame->sc, &frame->psc) < 0)
  250. goto give_sigsegv;
  251. if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
  252. goto give_sigsegv;
  253. if (_NSIG_WORDS > 1) {
  254. if (__copy_to_user(frame->extramask, &set->sig[1],
  255. sizeof(frame->extramask)))
  256. goto give_sigsegv;
  257. }
  258. /* set up to return from userspace. If provided, use a stub already in
  259. * userspace */
  260. if (ka->sa.sa_flags & SA_RESTORER) {
  261. if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
  262. goto give_sigsegv;
  263. } else {
  264. if (__put_user((void (*)(void))frame->retcode,
  265. &frame->pretcode))
  266. goto give_sigsegv;
  267. /* this is mov $,d0; syscall 0 */
  268. if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
  269. __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
  270. __put_user(0x00, (char *)(frame->retcode + 2)) ||
  271. __put_user(0xf0, (char *)(frame->retcode + 3)) ||
  272. __put_user(0xe0, (char *)(frame->retcode + 4)))
  273. goto give_sigsegv;
  274. flush_icache_range((unsigned long) frame->retcode,
  275. (unsigned long) frame->retcode + 5);
  276. }
  277. /* set up registers for signal handler */
  278. regs->sp = (unsigned long) frame;
  279. regs->pc = (unsigned long) ka->sa.sa_handler;
  280. regs->d0 = sig;
  281. regs->d1 = (unsigned long) &frame->sc;
  282. set_fs(USER_DS);
  283. /* the tracer may want to single-step inside the handler */
  284. if (test_thread_flag(TIF_SINGLESTEP))
  285. ptrace_notify(SIGTRAP);
  286. #if DEBUG_SIG
  287. printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  288. sig, current->comm, current->pid, frame, regs->pc,
  289. frame->pretcode);
  290. #endif
  291. return 0;
  292. give_sigsegv:
  293. force_sig(SIGSEGV, current);
  294. return -EFAULT;
  295. }
  296. /*
  297. * set up a realtime signal frame
  298. */
  299. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  300. sigset_t *set, struct pt_regs *regs)
  301. {
  302. struct rt_sigframe __user *frame;
  303. int rsig;
  304. frame = get_sigframe(ka, regs, sizeof(*frame));
  305. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  306. goto give_sigsegv;
  307. rsig = sig;
  308. if (sig < 32 &&
  309. current_thread_info()->exec_domain &&
  310. current_thread_info()->exec_domain->signal_invmap)
  311. rsig = current_thread_info()->exec_domain->signal_invmap[sig];
  312. if (__put_user(rsig, &frame->sig) ||
  313. __put_user(&frame->info, &frame->pinfo) ||
  314. __put_user(&frame->uc, &frame->puc) ||
  315. copy_siginfo_to_user(&frame->info, info))
  316. goto give_sigsegv;
  317. /* create the ucontext. */
  318. if (__put_user(0, &frame->uc.uc_flags) ||
  319. __put_user(0, &frame->uc.uc_link) ||
  320. __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
  321. __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
  322. __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
  323. setup_sigcontext(&frame->uc.uc_mcontext,
  324. &frame->fpuctx, regs, set->sig[0]) ||
  325. __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
  326. goto give_sigsegv;
  327. /* set up to return from userspace. If provided, use a stub already in
  328. * userspace */
  329. if (ka->sa.sa_flags & SA_RESTORER) {
  330. if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
  331. goto give_sigsegv;
  332. } else {
  333. if (__put_user((void(*)(void))frame->retcode,
  334. &frame->pretcode) ||
  335. /* This is mov $,d0; syscall 0 */
  336. __put_user(0x2c, (char *)(frame->retcode + 0)) ||
  337. __put_user(__NR_rt_sigreturn,
  338. (char *)(frame->retcode + 1)) ||
  339. __put_user(0x00, (char *)(frame->retcode + 2)) ||
  340. __put_user(0xf0, (char *)(frame->retcode + 3)) ||
  341. __put_user(0xe0, (char *)(frame->retcode + 4)))
  342. goto give_sigsegv;
  343. flush_icache_range((u_long) frame->retcode,
  344. (u_long) frame->retcode + 5);
  345. }
  346. /* Set up registers for signal handler */
  347. regs->sp = (unsigned long) frame;
  348. regs->pc = (unsigned long) ka->sa.sa_handler;
  349. regs->d0 = sig;
  350. regs->d1 = (long) &frame->info;
  351. set_fs(USER_DS);
  352. /* the tracer may want to single-step inside the handler */
  353. if (test_thread_flag(TIF_SINGLESTEP))
  354. ptrace_notify(SIGTRAP);
  355. #if DEBUG_SIG
  356. printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  357. sig, current->comm, current->pid, frame, regs->pc,
  358. frame->pretcode);
  359. #endif
  360. return 0;
  361. give_sigsegv:
  362. force_sig(SIGSEGV, current);
  363. return -EFAULT;
  364. }
  365. /*
  366. * handle the actual delivery of a signal to userspace
  367. */
  368. static int handle_signal(int sig,
  369. siginfo_t *info, struct k_sigaction *ka,
  370. sigset_t *oldset, struct pt_regs *regs)
  371. {
  372. int ret;
  373. /* Are we from a system call? */
  374. if (regs->orig_d0 >= 0) {
  375. /* If so, check system call restarting.. */
  376. switch (regs->d0) {
  377. case -ERESTART_RESTARTBLOCK:
  378. case -ERESTARTNOHAND:
  379. regs->d0 = -EINTR;
  380. break;
  381. case -ERESTARTSYS:
  382. if (!(ka->sa.sa_flags & SA_RESTART)) {
  383. regs->d0 = -EINTR;
  384. break;
  385. }
  386. /* fallthrough */
  387. case -ERESTARTNOINTR:
  388. regs->d0 = regs->orig_d0;
  389. regs->pc -= 2;
  390. }
  391. }
  392. /* Set up the stack frame */
  393. if (ka->sa.sa_flags & SA_SIGINFO)
  394. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  395. else
  396. ret = setup_frame(sig, ka, oldset, regs);
  397. if (ret == 0) {
  398. spin_lock_irq(&current->sighand->siglock);
  399. sigorsets(&current->blocked, &current->blocked,
  400. &ka->sa.sa_mask);
  401. if (!(ka->sa.sa_flags & SA_NODEFER))
  402. sigaddset(&current->blocked, sig);
  403. recalc_sigpending();
  404. spin_unlock_irq(&current->sighand->siglock);
  405. }
  406. return ret;
  407. }
  408. /*
  409. * handle a potential signal
  410. */
  411. static void do_signal(struct pt_regs *regs)
  412. {
  413. struct k_sigaction ka;
  414. siginfo_t info;
  415. sigset_t *oldset;
  416. int signr;
  417. /* we want the common case to go fast, which is why we may in certain
  418. * cases get here from kernel mode */
  419. if (!user_mode(regs))
  420. return;
  421. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  422. oldset = &current->saved_sigmask;
  423. else
  424. oldset = &current->blocked;
  425. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  426. if (signr > 0) {
  427. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  428. /* a signal was successfully delivered; the saved
  429. * sigmask will have been stored in the signal frame,
  430. * and will be restored by sigreturn, so we can simply
  431. * clear the TIF_RESTORE_SIGMASK flag */
  432. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  433. clear_thread_flag(TIF_RESTORE_SIGMASK);
  434. }
  435. return;
  436. }
  437. /* did we come from a system call? */
  438. if (regs->orig_d0 >= 0) {
  439. /* restart the system call - no handlers present */
  440. switch (regs->d0) {
  441. case -ERESTARTNOHAND:
  442. case -ERESTARTSYS:
  443. case -ERESTARTNOINTR:
  444. regs->d0 = regs->orig_d0;
  445. regs->pc -= 2;
  446. break;
  447. case -ERESTART_RESTARTBLOCK:
  448. regs->d0 = __NR_restart_syscall;
  449. regs->pc -= 2;
  450. break;
  451. }
  452. }
  453. /* if there's no signal to deliver, we just put the saved sigmask
  454. * back */
  455. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  456. clear_thread_flag(TIF_RESTORE_SIGMASK);
  457. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  458. }
  459. }
  460. /*
  461. * notification of userspace execution resumption
  462. * - triggered by current->work.notify_resume
  463. */
  464. asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
  465. {
  466. /* Pending single-step? */
  467. if (thread_info_flags & _TIF_SINGLESTEP) {
  468. #ifndef CONFIG_MN10300_USING_JTAG
  469. regs->epsw |= EPSW_T;
  470. clear_thread_flag(TIF_SINGLESTEP);
  471. #else
  472. BUG(); /* no h/w single-step if using JTAG unit */
  473. #endif
  474. }
  475. /* deal with pending signal delivery */
  476. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
  477. do_signal(regs);
  478. }