signal.c 12 KB

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