signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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/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/asm.h>
  24. #include <linux/bitops.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/fpu.h>
  27. #include <asm/sim.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/ucontext.h>
  30. #include <asm/cpu-features.h>
  31. #include "signal-common.h"
  32. #define DEBUG_SIG 0
  33. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  34. static int do_signal(sigset_t *oldset, struct pt_regs *regs);
  35. /*
  36. * Atomically swap in the new signal mask, and wait for a signal.
  37. */
  38. #ifdef CONFIG_TRAD_SIGNALS
  39. save_static_function(sys_sigsuspend);
  40. __attribute_used__ noinline static int
  41. _sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
  42. {
  43. sigset_t *uset, saveset, newset;
  44. uset = (sigset_t *) regs.regs[4];
  45. if (copy_from_user(&newset, uset, sizeof(sigset_t)))
  46. return -EFAULT;
  47. sigdelsetmask(&newset, ~_BLOCKABLE);
  48. spin_lock_irq(&current->sighand->siglock);
  49. saveset = current->blocked;
  50. current->blocked = newset;
  51. recalc_sigpending();
  52. spin_unlock_irq(&current->sighand->siglock);
  53. regs.regs[2] = EINTR;
  54. regs.regs[7] = 1;
  55. while (1) {
  56. current->state = TASK_INTERRUPTIBLE;
  57. schedule();
  58. if (do_signal(&saveset, &regs))
  59. return -EINTR;
  60. }
  61. }
  62. #endif
  63. save_static_function(sys_rt_sigsuspend);
  64. __attribute_used__ noinline static int
  65. _sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
  66. {
  67. sigset_t *unewset, saveset, newset;
  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 *) 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. saveset = current->blocked;
  79. current->blocked = newset;
  80. recalc_sigpending();
  81. spin_unlock_irq(&current->sighand->siglock);
  82. regs.regs[2] = EINTR;
  83. regs.regs[7] = 1;
  84. while (1) {
  85. current->state = TASK_INTERRUPTIBLE;
  86. schedule();
  87. if (do_signal(&saveset, &regs))
  88. return -EINTR;
  89. }
  90. }
  91. #ifdef CONFIG_TRAD_SIGNALS
  92. asmlinkage int sys_sigaction(int sig, const struct sigaction *act,
  93. struct sigaction *oact)
  94. {
  95. struct k_sigaction new_ka, old_ka;
  96. int ret;
  97. int err = 0;
  98. if (act) {
  99. old_sigset_t mask;
  100. if (!access_ok(VERIFY_READ, act, sizeof(*act)))
  101. return -EFAULT;
  102. err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
  103. err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  104. err |= __get_user(mask, &act->sa_mask.sig[0]);
  105. if (err)
  106. return -EFAULT;
  107. siginitset(&new_ka.sa.sa_mask, mask);
  108. }
  109. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  110. if (!ret && oact) {
  111. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
  112. return -EFAULT;
  113. err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  114. err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
  115. err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
  116. err |= __put_user(0, &oact->sa_mask.sig[1]);
  117. err |= __put_user(0, &oact->sa_mask.sig[2]);
  118. err |= __put_user(0, &oact->sa_mask.sig[3]);
  119. if (err)
  120. return -EFAULT;
  121. }
  122. return ret;
  123. }
  124. #endif
  125. asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
  126. {
  127. const stack_t *uss = (const stack_t *) regs.regs[4];
  128. stack_t *uoss = (stack_t *) regs.regs[5];
  129. unsigned long usp = regs.regs[29];
  130. return do_sigaltstack(uss, uoss, usp);
  131. }
  132. #if PLAT_TRAMPOLINE_STUFF_LINE
  133. #define __tramp __attribute__((aligned(PLAT_TRAMPOLINE_STUFF_LINE)))
  134. #else
  135. #define __tramp
  136. #endif
  137. #ifdef CONFIG_TRAD_SIGNALS
  138. struct sigframe {
  139. u32 sf_ass[4]; /* argument save space for o32 */
  140. u32 sf_code[2] __tramp; /* signal trampoline */
  141. struct sigcontext sf_sc __tramp;
  142. sigset_t sf_mask;
  143. };
  144. #endif
  145. struct rt_sigframe {
  146. u32 rs_ass[4]; /* argument save space for o32 */
  147. u32 rs_code[2] __tramp; /* signal trampoline */
  148. struct siginfo rs_info __tramp;
  149. struct ucontext rs_uc;
  150. };
  151. #ifdef CONFIG_TRAD_SIGNALS
  152. save_static_function(sys_sigreturn);
  153. __attribute_used__ noinline static void
  154. _sys_sigreturn(nabi_no_regargs struct pt_regs regs)
  155. {
  156. struct sigframe *frame;
  157. sigset_t blocked;
  158. frame = (struct sigframe *) regs.regs[29];
  159. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  160. goto badframe;
  161. if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
  162. goto badframe;
  163. sigdelsetmask(&blocked, ~_BLOCKABLE);
  164. spin_lock_irq(&current->sighand->siglock);
  165. current->blocked = blocked;
  166. recalc_sigpending();
  167. spin_unlock_irq(&current->sighand->siglock);
  168. if (restore_sigcontext(&regs, &frame->sf_sc))
  169. goto badframe;
  170. /*
  171. * Don't let your children do this ...
  172. */
  173. if (current_thread_info()->flags & TIF_SYSCALL_TRACE)
  174. do_syscall_trace(&regs, 1);
  175. __asm__ __volatile__(
  176. "move\t$29, %0\n\t"
  177. "j\tsyscall_exit"
  178. :/* no outputs */
  179. :"r" (&regs));
  180. /* Unreached */
  181. badframe:
  182. force_sig(SIGSEGV, current);
  183. }
  184. #endif
  185. save_static_function(sys_rt_sigreturn);
  186. __attribute_used__ noinline static void
  187. _sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
  188. {
  189. struct rt_sigframe *frame;
  190. sigset_t set;
  191. stack_t st;
  192. frame = (struct rt_sigframe *) regs.regs[29];
  193. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  194. goto badframe;
  195. if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
  196. goto badframe;
  197. sigdelsetmask(&set, ~_BLOCKABLE);
  198. spin_lock_irq(&current->sighand->siglock);
  199. current->blocked = set;
  200. recalc_sigpending();
  201. spin_unlock_irq(&current->sighand->siglock);
  202. if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
  203. goto badframe;
  204. if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
  205. goto badframe;
  206. /* It is more difficult to avoid calling this function than to
  207. call it and ignore errors. */
  208. do_sigaltstack(&st, NULL, regs.regs[29]);
  209. /*
  210. * Don't let your children do this ...
  211. */
  212. __asm__ __volatile__(
  213. "move\t$29, %0\n\t"
  214. "j\tsyscall_exit"
  215. :/* no outputs */
  216. :"r" (&regs));
  217. /* Unreached */
  218. badframe:
  219. force_sig(SIGSEGV, current);
  220. }
  221. #ifdef CONFIG_TRAD_SIGNALS
  222. static void inline setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
  223. int signr, sigset_t *set)
  224. {
  225. struct sigframe *frame;
  226. int err = 0;
  227. frame = get_sigframe(ka, regs, sizeof(*frame));
  228. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  229. goto give_sigsegv;
  230. /*
  231. * Set up the return code ...
  232. *
  233. * li v0, __NR_sigreturn
  234. * syscall
  235. */
  236. if (PLAT_TRAMPOLINE_STUFF_LINE)
  237. __clear_user(frame->sf_code, PLAT_TRAMPOLINE_STUFF_LINE);
  238. err |= __put_user(0x24020000 + __NR_sigreturn, frame->sf_code + 0);
  239. err |= __put_user(0x0000000c , frame->sf_code + 1);
  240. flush_cache_sigtramp((unsigned long) frame->sf_code);
  241. err |= setup_sigcontext(regs, &frame->sf_sc);
  242. err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
  243. if (err)
  244. goto give_sigsegv;
  245. /*
  246. * Arguments to signal handler:
  247. *
  248. * a0 = signal number
  249. * a1 = 0 (should be cause)
  250. * a2 = pointer to struct sigcontext
  251. *
  252. * $25 and c0_epc point to the signal handler, $29 points to the
  253. * struct sigframe.
  254. */
  255. regs->regs[ 4] = signr;
  256. regs->regs[ 5] = 0;
  257. regs->regs[ 6] = (unsigned long) &frame->sf_sc;
  258. regs->regs[29] = (unsigned long) frame;
  259. regs->regs[31] = (unsigned long) frame->sf_code;
  260. regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
  261. #if DEBUG_SIG
  262. printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
  263. current->comm, current->pid,
  264. frame, regs->cp0_epc, frame->regs[31]);
  265. #endif
  266. return;
  267. give_sigsegv:
  268. force_sigsegv(signr, current);
  269. }
  270. #endif
  271. static void inline setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
  272. int signr, sigset_t *set, siginfo_t *info)
  273. {
  274. struct rt_sigframe *frame;
  275. int err = 0;
  276. frame = get_sigframe(ka, regs, sizeof(*frame));
  277. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  278. goto give_sigsegv;
  279. /*
  280. * Set up the return code ...
  281. *
  282. * li v0, __NR_rt_sigreturn
  283. * syscall
  284. */
  285. if (PLAT_TRAMPOLINE_STUFF_LINE)
  286. __clear_user(frame->rs_code, PLAT_TRAMPOLINE_STUFF_LINE);
  287. err |= __put_user(0x24020000 + __NR_rt_sigreturn, frame->rs_code + 0);
  288. err |= __put_user(0x0000000c , frame->rs_code + 1);
  289. flush_cache_sigtramp((unsigned long) frame->rs_code);
  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;
  327. give_sigsegv:
  328. force_sigsegv(signr, current);
  329. }
  330. extern void setup_rt_frame_n32(struct k_sigaction * ka,
  331. struct pt_regs *regs, int signr, sigset_t *set, siginfo_t *info);
  332. static inline void handle_signal(unsigned long sig, siginfo_t *info,
  333. struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
  334. {
  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. #ifdef CONFIG_TRAD_SIGNALS
  352. if (ka->sa.sa_flags & SA_SIGINFO) {
  353. #else
  354. if (1) {
  355. #endif
  356. #ifdef CONFIG_MIPS32_N32
  357. if ((current->thread.mflags & MF_ABI_MASK) == MF_N32)
  358. setup_rt_frame_n32 (ka, regs, sig, oldset, info);
  359. else
  360. #endif
  361. setup_rt_frame(ka, regs, sig, oldset, info);
  362. }
  363. #ifdef CONFIG_TRAD_SIGNALS
  364. else
  365. setup_frame(ka, regs, sig, oldset);
  366. #endif
  367. spin_lock_irq(&current->sighand->siglock);
  368. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  369. if (!(ka->sa.sa_flags & SA_NODEFER))
  370. sigaddset(&current->blocked,sig);
  371. recalc_sigpending();
  372. spin_unlock_irq(&current->sighand->siglock);
  373. }
  374. extern int do_signal32(sigset_t *oldset, struct pt_regs *regs);
  375. extern int do_irix_signal(sigset_t *oldset, struct pt_regs *regs);
  376. static int do_signal(sigset_t *oldset, struct pt_regs *regs)
  377. {
  378. struct k_sigaction ka;
  379. siginfo_t info;
  380. int signr;
  381. #ifdef CONFIG_BINFMT_ELF32
  382. if ((current->thread.mflags & MF_ABI_MASK) == MF_O32) {
  383. return do_signal32(oldset, regs);
  384. }
  385. #endif
  386. /*
  387. * We want the common case to go fast, which is why we may in certain
  388. * cases get here from kernel mode. Just return without doing anything
  389. * if so.
  390. */
  391. if (!user_mode(regs))
  392. return 1;
  393. if (try_to_freeze())
  394. goto no_signal;
  395. if (!oldset)
  396. oldset = &current->blocked;
  397. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  398. if (signr > 0) {
  399. handle_signal(signr, &info, &ka, oldset, regs);
  400. return 1;
  401. }
  402. no_signal:
  403. /*
  404. * Who's code doesn't conform to the restartable syscall convention
  405. * dies here!!! The li instruction, a single machine instruction,
  406. * must directly be followed by the syscall instruction.
  407. */
  408. if (regs->regs[0]) {
  409. if (regs->regs[2] == ERESTARTNOHAND ||
  410. regs->regs[2] == ERESTARTSYS ||
  411. regs->regs[2] == ERESTARTNOINTR) {
  412. regs->regs[7] = regs->regs[26];
  413. regs->cp0_epc -= 8;
  414. }
  415. if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
  416. regs->regs[2] = __NR_restart_syscall;
  417. regs->regs[7] = regs->regs[26];
  418. regs->cp0_epc -= 4;
  419. }
  420. }
  421. return 0;
  422. }
  423. /*
  424. * notification of userspace execution resumption
  425. * - triggered by current->work.notify_resume
  426. */
  427. asmlinkage void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
  428. __u32 thread_info_flags)
  429. {
  430. /* deal with pending signal delivery */
  431. if (thread_info_flags & _TIF_SIGPENDING) {
  432. #ifdef CONFIG_BINFMT_ELF32
  433. if (likely((current->thread.mflags & MF_ABI_MASK) == MF_O32)) {
  434. do_signal32(oldset, regs);
  435. return;
  436. }
  437. #endif
  438. #ifdef CONFIG_BINFMT_IRIX
  439. if (unlikely(current->personality != PER_LINUX)) {
  440. do_irix_signal(oldset, regs);
  441. return;
  442. }
  443. #endif
  444. do_signal(oldset, regs);
  445. }
  446. }