signal.c 13 KB

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