signal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. if (current_thread_info()->flags & TIF_SYSCALL_TRACE)
  193. do_syscall_trace(&regs, 1);
  194. __asm__ __volatile__(
  195. "move\t$29, %0\n\t"
  196. "j\tsyscall_exit"
  197. :/* no outputs */
  198. :"r" (&regs));
  199. /* Unreached */
  200. badframe:
  201. force_sig(SIGSEGV, current);
  202. }
  203. #endif /* CONFIG_TRAD_SIGNALS */
  204. save_static_function(sys_rt_sigreturn);
  205. __attribute_used__ noinline static void
  206. _sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
  207. {
  208. struct rt_sigframe *frame;
  209. sigset_t set;
  210. stack_t st;
  211. frame = (struct rt_sigframe *) regs.regs[29];
  212. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  213. goto badframe;
  214. if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
  215. goto badframe;
  216. sigdelsetmask(&set, ~_BLOCKABLE);
  217. spin_lock_irq(&current->sighand->siglock);
  218. current->blocked = set;
  219. recalc_sigpending();
  220. spin_unlock_irq(&current->sighand->siglock);
  221. if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
  222. goto badframe;
  223. if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
  224. goto badframe;
  225. /* It is more difficult to avoid calling this function than to
  226. call it and ignore errors. */
  227. do_sigaltstack(&st, NULL, regs.regs[29]);
  228. /*
  229. * Don't let your children do this ...
  230. */
  231. __asm__ __volatile__(
  232. "move\t$29, %0\n\t"
  233. "j\tsyscall_exit"
  234. :/* no outputs */
  235. :"r" (&regs));
  236. /* Unreached */
  237. badframe:
  238. force_sig(SIGSEGV, current);
  239. }
  240. #ifdef CONFIG_TRAD_SIGNALS
  241. int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
  242. int signr, sigset_t *set)
  243. {
  244. struct sigframe *frame;
  245. int err = 0;
  246. frame = get_sigframe(ka, regs, sizeof(*frame));
  247. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  248. goto give_sigsegv;
  249. install_sigtramp(frame->sf_code, __NR_sigreturn);
  250. err |= setup_sigcontext(regs, &frame->sf_sc);
  251. err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
  252. if (err)
  253. goto give_sigsegv;
  254. /*
  255. * Arguments to signal handler:
  256. *
  257. * a0 = signal number
  258. * a1 = 0 (should be cause)
  259. * a2 = pointer to struct sigcontext
  260. *
  261. * $25 and c0_epc point to the signal handler, $29 points to the
  262. * struct sigframe.
  263. */
  264. regs->regs[ 4] = signr;
  265. regs->regs[ 5] = 0;
  266. regs->regs[ 6] = (unsigned long) &frame->sf_sc;
  267. regs->regs[29] = (unsigned long) frame;
  268. regs->regs[31] = (unsigned long) frame->sf_code;
  269. regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
  270. #if DEBUG_SIG
  271. printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
  272. current->comm, current->pid,
  273. frame, regs->cp0_epc, frame->regs[31]);
  274. #endif
  275. return 1;
  276. give_sigsegv:
  277. force_sigsegv(signr, current);
  278. return 0;
  279. }
  280. #endif
  281. int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
  282. int signr, sigset_t *set, siginfo_t *info)
  283. {
  284. struct rt_sigframe *frame;
  285. int err = 0;
  286. frame = get_sigframe(ka, regs, sizeof(*frame));
  287. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  288. goto give_sigsegv;
  289. install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
  290. /* Create siginfo. */
  291. err |= copy_siginfo_to_user(&frame->rs_info, info);
  292. /* Create the ucontext. */
  293. err |= __put_user(0, &frame->rs_uc.uc_flags);
  294. err |= __put_user(0, &frame->rs_uc.uc_link);
  295. err |= __put_user((void *)current->sas_ss_sp,
  296. &frame->rs_uc.uc_stack.ss_sp);
  297. err |= __put_user(sas_ss_flags(regs->regs[29]),
  298. &frame->rs_uc.uc_stack.ss_flags);
  299. err |= __put_user(current->sas_ss_size,
  300. &frame->rs_uc.uc_stack.ss_size);
  301. err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
  302. err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
  303. if (err)
  304. goto give_sigsegv;
  305. /*
  306. * Arguments to signal handler:
  307. *
  308. * a0 = signal number
  309. * a1 = 0 (should be cause)
  310. * a2 = pointer to ucontext
  311. *
  312. * $25 and c0_epc point to the signal handler, $29 points to
  313. * the struct rt_sigframe.
  314. */
  315. regs->regs[ 4] = signr;
  316. regs->regs[ 5] = (unsigned long) &frame->rs_info;
  317. regs->regs[ 6] = (unsigned long) &frame->rs_uc;
  318. regs->regs[29] = (unsigned long) frame;
  319. regs->regs[31] = (unsigned long) frame->rs_code;
  320. regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
  321. #if DEBUG_SIG
  322. printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
  323. current->comm, current->pid,
  324. frame, regs->cp0_epc, regs->regs[31]);
  325. #endif
  326. return 1;
  327. give_sigsegv:
  328. force_sigsegv(signr, current);
  329. return 0;
  330. }
  331. static inline int handle_signal(unsigned long sig, siginfo_t *info,
  332. struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
  333. {
  334. int ret;
  335. switch(regs->regs[0]) {
  336. case ERESTART_RESTARTBLOCK:
  337. case ERESTARTNOHAND:
  338. regs->regs[2] = EINTR;
  339. break;
  340. case ERESTARTSYS:
  341. if(!(ka->sa.sa_flags & SA_RESTART)) {
  342. regs->regs[2] = EINTR;
  343. break;
  344. }
  345. /* fallthrough */
  346. case ERESTARTNOINTR: /* Userland will reload $v0. */
  347. regs->regs[7] = regs->regs[26];
  348. regs->cp0_epc -= 8;
  349. }
  350. regs->regs[0] = 0; /* Don't deal with this again. */
  351. if (sig_uses_siginfo(ka))
  352. ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
  353. else
  354. ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
  355. spin_lock_irq(&current->sighand->siglock);
  356. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  357. if (!(ka->sa.sa_flags & SA_NODEFER))
  358. sigaddset(&current->blocked,sig);
  359. recalc_sigpending();
  360. spin_unlock_irq(&current->sighand->siglock);
  361. return ret;
  362. }
  363. int do_signal(sigset_t *oldset, struct pt_regs *regs)
  364. {
  365. struct k_sigaction ka;
  366. siginfo_t info;
  367. int signr;
  368. /*
  369. * We want the common case to go fast, which is why we may in certain
  370. * cases get here from kernel mode. Just return without doing anything
  371. * if so.
  372. */
  373. if (!user_mode(regs))
  374. return 1;
  375. if (try_to_freeze())
  376. goto no_signal;
  377. if (!oldset)
  378. oldset = &current->blocked;
  379. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  380. if (signr > 0)
  381. return handle_signal(signr, &info, &ka, oldset, regs);
  382. no_signal:
  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. }
  401. return 0;
  402. }
  403. /*
  404. * notification of userspace execution resumption
  405. * - triggered by current->work.notify_resume
  406. */
  407. asmlinkage void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
  408. __u32 thread_info_flags)
  409. {
  410. /* deal with pending signal delivery */
  411. if (thread_info_flags & _TIF_SIGPENDING) {
  412. current->thread.abi->do_signal(oldset, regs);
  413. }
  414. }