signal.c 13 KB

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