signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. * Copyright (C) 1994 - 2000 Ralf Baechle
  8. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  9. */
  10. #include <linux/cache.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/personality.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/ptrace.h>
  21. #include <linux/unistd.h>
  22. #include <linux/compiler.h>
  23. #include <asm/abi.h>
  24. #include <asm/asm.h>
  25. #include <linux/bitops.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/fpu.h>
  28. #include <asm/sim.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/ucontext.h>
  31. #include <asm/cpu-features.h>
  32. #include <asm/war.h>
  33. #include "signal-common.h"
  34. #define DEBUG_SIG 0
  35. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  36. /*
  37. * Atomically swap in the new signal mask, and wait for a signal.
  38. */
  39. #ifdef CONFIG_TRAD_SIGNALS
  40. save_static_function(sys_sigsuspend);
  41. __attribute_used__ noinline static int
  42. _sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
  43. {
  44. sigset_t newset;
  45. sigset_t __user *uset;
  46. uset = (sigset_t __user *) regs.regs[4];
  47. if (copy_from_user(&newset, uset, sizeof(sigset_t)))
  48. return -EFAULT;
  49. sigdelsetmask(&newset, ~_BLOCKABLE);
  50. spin_lock_irq(&current->sighand->siglock);
  51. current->saved_sigmask = current->blocked;
  52. current->blocked = newset;
  53. recalc_sigpending();
  54. spin_unlock_irq(&current->sighand->siglock);
  55. current->state = TASK_INTERRUPTIBLE;
  56. schedule();
  57. set_thread_flag(TIF_RESTORE_SIGMASK);
  58. return -ERESTARTNOHAND;
  59. }
  60. #endif
  61. save_static_function(sys_rt_sigsuspend);
  62. __attribute_used__ noinline static int
  63. _sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
  64. {
  65. sigset_t newset;
  66. sigset_t __user *unewset;
  67. size_t sigsetsize;
  68. /* XXX Don't preclude handling different sized sigset_t's. */
  69. sigsetsize = regs.regs[5];
  70. if (sigsetsize != sizeof(sigset_t))
  71. return -EINVAL;
  72. unewset = (sigset_t __user *) regs.regs[4];
  73. if (copy_from_user(&newset, unewset, sizeof(newset)))
  74. return -EFAULT;
  75. sigdelsetmask(&newset, ~_BLOCKABLE);
  76. spin_lock_irq(&current->sighand->siglock);
  77. current->saved_sigmask = current->blocked;
  78. current->blocked = newset;
  79. recalc_sigpending();
  80. spin_unlock_irq(&current->sighand->siglock);
  81. current->state = TASK_INTERRUPTIBLE;
  82. schedule();
  83. set_thread_flag(TIF_RESTORE_SIGMASK);
  84. return -ERESTARTNOHAND;
  85. }
  86. #ifdef CONFIG_TRAD_SIGNALS
  87. asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
  88. struct sigaction __user *oact)
  89. {
  90. struct k_sigaction new_ka, old_ka;
  91. int ret;
  92. int err = 0;
  93. if (act) {
  94. old_sigset_t mask;
  95. if (!access_ok(VERIFY_READ, act, sizeof(*act)))
  96. return -EFAULT;
  97. err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
  98. err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  99. err |= __get_user(mask, &act->sa_mask.sig[0]);
  100. if (err)
  101. return -EFAULT;
  102. siginitset(&new_ka.sa.sa_mask, mask);
  103. }
  104. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  105. if (!ret && oact) {
  106. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
  107. return -EFAULT;
  108. err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  109. err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
  110. err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
  111. err |= __put_user(0, &oact->sa_mask.sig[1]);
  112. err |= __put_user(0, &oact->sa_mask.sig[2]);
  113. err |= __put_user(0, &oact->sa_mask.sig[3]);
  114. if (err)
  115. return -EFAULT;
  116. }
  117. return ret;
  118. }
  119. #endif
  120. asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
  121. {
  122. const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
  123. stack_t __user *uoss = (stack_t __user *) regs.regs[5];
  124. unsigned long usp = regs.regs[29];
  125. return do_sigaltstack(uss, uoss, usp);
  126. }
  127. /*
  128. * Horribly complicated - with the bloody RM9000 workarounds enabled
  129. * the signal trampolines is moving to the end of the structure so we can
  130. * increase the alignment without breaking software compatibility.
  131. */
  132. #ifdef CONFIG_TRAD_SIGNALS
  133. struct sigframe {
  134. u32 sf_ass[4]; /* argument save space for o32 */
  135. #if ICACHE_REFILLS_WORKAROUND_WAR
  136. u32 sf_pad[2];
  137. #else
  138. u32 sf_code[2]; /* signal trampoline */
  139. #endif
  140. struct sigcontext sf_sc;
  141. sigset_t sf_mask;
  142. #if ICACHE_REFILLS_WORKAROUND_WAR
  143. u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
  144. #endif
  145. };
  146. #endif
  147. struct rt_sigframe {
  148. u32 rs_ass[4]; /* argument save space for o32 */
  149. #if ICACHE_REFILLS_WORKAROUND_WAR
  150. u32 rs_pad[2];
  151. #else
  152. u32 rs_code[2]; /* signal trampoline */
  153. #endif
  154. struct siginfo rs_info;
  155. struct ucontext rs_uc;
  156. #if ICACHE_REFILLS_WORKAROUND_WAR
  157. u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
  158. #endif
  159. };
  160. #ifdef CONFIG_TRAD_SIGNALS
  161. save_static_function(sys_sigreturn);
  162. __attribute_used__ noinline static void
  163. _sys_sigreturn(nabi_no_regargs struct pt_regs regs)
  164. {
  165. struct sigframe __user *frame;
  166. sigset_t blocked;
  167. frame = (struct sigframe __user *) regs.regs[29];
  168. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  169. goto badframe;
  170. if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
  171. goto badframe;
  172. sigdelsetmask(&blocked, ~_BLOCKABLE);
  173. spin_lock_irq(&current->sighand->siglock);
  174. current->blocked = blocked;
  175. recalc_sigpending();
  176. spin_unlock_irq(&current->sighand->siglock);
  177. if (restore_sigcontext(&regs, &frame->sf_sc))
  178. goto badframe;
  179. /*
  180. * Don't let your children do this ...
  181. */
  182. __asm__ __volatile__(
  183. "move\t$29, %0\n\t"
  184. "j\tsyscall_exit"
  185. :/* no outputs */
  186. :"r" (&regs));
  187. /* Unreached */
  188. badframe:
  189. force_sig(SIGSEGV, current);
  190. }
  191. #endif /* CONFIG_TRAD_SIGNALS */
  192. save_static_function(sys_rt_sigreturn);
  193. __attribute_used__ noinline static void
  194. _sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
  195. {
  196. struct rt_sigframe __user *frame;
  197. sigset_t set;
  198. stack_t st;
  199. frame = (struct rt_sigframe __user *) regs.regs[29];
  200. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  201. goto badframe;
  202. if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
  203. goto badframe;
  204. sigdelsetmask(&set, ~_BLOCKABLE);
  205. spin_lock_irq(&current->sighand->siglock);
  206. current->blocked = set;
  207. recalc_sigpending();
  208. spin_unlock_irq(&current->sighand->siglock);
  209. if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
  210. goto badframe;
  211. if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
  212. goto badframe;
  213. /* It is more difficult to avoid calling this function than to
  214. call it and ignore errors. */
  215. do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
  216. /*
  217. * Don't let your children do this ...
  218. */
  219. __asm__ __volatile__(
  220. "move\t$29, %0\n\t"
  221. "j\tsyscall_exit"
  222. :/* no outputs */
  223. :"r" (&regs));
  224. /* Unreached */
  225. badframe:
  226. force_sig(SIGSEGV, current);
  227. }
  228. #ifdef CONFIG_TRAD_SIGNALS
  229. int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
  230. int signr, sigset_t *set)
  231. {
  232. struct sigframe __user *frame;
  233. int err = 0;
  234. frame = get_sigframe(ka, regs, sizeof(*frame));
  235. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  236. goto give_sigsegv;
  237. install_sigtramp(frame->sf_code, __NR_sigreturn);
  238. err |= setup_sigcontext(regs, &frame->sf_sc);
  239. err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
  240. if (err)
  241. goto give_sigsegv;
  242. /*
  243. * Arguments to signal handler:
  244. *
  245. * a0 = signal number
  246. * a1 = 0 (should be cause)
  247. * a2 = pointer to struct sigcontext
  248. *
  249. * $25 and c0_epc point to the signal handler, $29 points to the
  250. * struct sigframe.
  251. */
  252. regs->regs[ 4] = signr;
  253. regs->regs[ 5] = 0;
  254. regs->regs[ 6] = (unsigned long) &frame->sf_sc;
  255. regs->regs[29] = (unsigned long) frame;
  256. regs->regs[31] = (unsigned long) frame->sf_code;
  257. regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
  258. #if DEBUG_SIG
  259. printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
  260. current->comm, current->pid,
  261. frame, regs->cp0_epc, frame->regs[31]);
  262. #endif
  263. return 0;
  264. give_sigsegv:
  265. force_sigsegv(signr, current);
  266. return -EFAULT;
  267. }
  268. #endif
  269. int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
  270. int signr, sigset_t *set, siginfo_t *info)
  271. {
  272. struct rt_sigframe __user *frame;
  273. int err = 0;
  274. frame = get_sigframe(ka, regs, sizeof(*frame));
  275. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  276. goto give_sigsegv;
  277. install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
  278. /* Create siginfo. */
  279. err |= copy_siginfo_to_user(&frame->rs_info, info);
  280. /* Create the ucontext. */
  281. err |= __put_user(0, &frame->rs_uc.uc_flags);
  282. err |= __put_user(NULL, &frame->rs_uc.uc_link);
  283. err |= __put_user((void __user *)current->sas_ss_sp,
  284. &frame->rs_uc.uc_stack.ss_sp);
  285. err |= __put_user(sas_ss_flags(regs->regs[29]),
  286. &frame->rs_uc.uc_stack.ss_flags);
  287. err |= __put_user(current->sas_ss_size,
  288. &frame->rs_uc.uc_stack.ss_size);
  289. err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
  290. err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
  291. if (err)
  292. goto give_sigsegv;
  293. /*
  294. * Arguments to signal handler:
  295. *
  296. * a0 = signal number
  297. * a1 = 0 (should be cause)
  298. * a2 = pointer to ucontext
  299. *
  300. * $25 and c0_epc point to the signal handler, $29 points to
  301. * the struct rt_sigframe.
  302. */
  303. regs->regs[ 4] = signr;
  304. regs->regs[ 5] = (unsigned long) &frame->rs_info;
  305. regs->regs[ 6] = (unsigned long) &frame->rs_uc;
  306. regs->regs[29] = (unsigned long) frame;
  307. regs->regs[31] = (unsigned long) frame->rs_code;
  308. regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
  309. #if DEBUG_SIG
  310. printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
  311. current->comm, current->pid,
  312. frame, regs->cp0_epc, regs->regs[31]);
  313. #endif
  314. return 0;
  315. give_sigsegv:
  316. force_sigsegv(signr, current);
  317. return -EFAULT;
  318. }
  319. static inline int handle_signal(unsigned long sig, siginfo_t *info,
  320. struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
  321. {
  322. int ret;
  323. switch(regs->regs[0]) {
  324. case ERESTART_RESTARTBLOCK:
  325. case ERESTARTNOHAND:
  326. regs->regs[2] = EINTR;
  327. break;
  328. case ERESTARTSYS:
  329. if (!(ka->sa.sa_flags & SA_RESTART)) {
  330. regs->regs[2] = EINTR;
  331. break;
  332. }
  333. /* fallthrough */
  334. case ERESTARTNOINTR: /* Userland will reload $v0. */
  335. regs->regs[7] = regs->regs[26];
  336. regs->cp0_epc -= 8;
  337. }
  338. regs->regs[0] = 0; /* Don't deal with this again. */
  339. if (sig_uses_siginfo(ka))
  340. ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
  341. else
  342. ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
  343. spin_lock_irq(&current->sighand->siglock);
  344. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  345. if (!(ka->sa.sa_flags & SA_NODEFER))
  346. sigaddset(&current->blocked,sig);
  347. recalc_sigpending();
  348. spin_unlock_irq(&current->sighand->siglock);
  349. return ret;
  350. }
  351. void do_signal(struct pt_regs *regs)
  352. {
  353. struct k_sigaction ka;
  354. sigset_t *oldset;
  355. siginfo_t info;
  356. int signr;
  357. /*
  358. * We want the common case to go fast, which is why we may in certain
  359. * cases get here from kernel mode. Just return without doing anything
  360. * if so.
  361. */
  362. if (!user_mode(regs))
  363. return;
  364. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  365. oldset = &current->saved_sigmask;
  366. else
  367. oldset = &current->blocked;
  368. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  369. if (signr > 0) {
  370. /* Whee! Actually deliver the signal. */
  371. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  372. /*
  373. * A signal was successfully delivered; the saved
  374. * sigmask will have been stored in the signal frame,
  375. * and will be restored by sigreturn, so we can simply
  376. * clear the TIF_RESTORE_SIGMASK flag.
  377. */
  378. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  379. clear_thread_flag(TIF_RESTORE_SIGMASK);
  380. }
  381. return;
  382. }
  383. /*
  384. * Who's code doesn't conform to the restartable syscall convention
  385. * dies here!!! The li instruction, a single machine instruction,
  386. * must directly be followed by the syscall instruction.
  387. */
  388. if (regs->regs[0]) {
  389. if (regs->regs[2] == ERESTARTNOHAND ||
  390. regs->regs[2] == ERESTARTSYS ||
  391. regs->regs[2] == ERESTARTNOINTR) {
  392. regs->regs[7] = regs->regs[26];
  393. regs->cp0_epc -= 8;
  394. }
  395. if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
  396. regs->regs[2] = __NR_restart_syscall;
  397. regs->regs[7] = regs->regs[26];
  398. regs->cp0_epc -= 4;
  399. }
  400. regs->regs[0] = 0; /* Don't deal with this again. */
  401. }
  402. /*
  403. * If there's no signal to deliver, we just put the saved sigmask
  404. * back
  405. */
  406. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  407. clear_thread_flag(TIF_RESTORE_SIGMASK);
  408. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  409. }
  410. }
  411. /*
  412. * notification of userspace execution resumption
  413. * - triggered by the TIF_WORK_MASK flags
  414. */
  415. asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
  416. __u32 thread_info_flags)
  417. {
  418. /* deal with pending signal delivery */
  419. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
  420. current->thread.abi->do_signal(regs);
  421. }