signal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * linux/arch/alpha/kernel/signal.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. *
  6. * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/kernel.h>
  10. #include <linux/signal.h>
  11. #include <linux/errno.h>
  12. #include <linux/wait.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/unistd.h>
  15. #include <linux/mm.h>
  16. #include <linux/smp.h>
  17. #include <linux/stddef.h>
  18. #include <linux/tty.h>
  19. #include <linux/binfmts.h>
  20. #include <linux/bitops.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/tracehook.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/sigcontext.h>
  25. #include <asm/ucontext.h>
  26. #include "proto.h"
  27. #define DEBUG_SIG 0
  28. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  29. asmlinkage void ret_from_sys_call(void);
  30. /*
  31. * The OSF/1 sigprocmask calling sequence is different from the
  32. * C sigprocmask() sequence..
  33. */
  34. SYSCALL_DEFINE2(osf_sigprocmask, int, how, unsigned long, newmask)
  35. {
  36. sigset_t oldmask;
  37. sigset_t mask;
  38. unsigned long res;
  39. siginitset(&mask, newmask & _BLOCKABLE);
  40. res = sigprocmask(how, &mask, &oldmask);
  41. if (!res) {
  42. force_successful_syscall_return();
  43. res = oldmask.sig[0];
  44. }
  45. return res;
  46. }
  47. SYSCALL_DEFINE3(osf_sigaction, int, sig,
  48. const struct osf_sigaction __user *, act,
  49. struct osf_sigaction __user *, oact)
  50. {
  51. struct k_sigaction new_ka, old_ka;
  52. int ret;
  53. if (act) {
  54. old_sigset_t mask;
  55. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  56. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  57. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  58. __get_user(mask, &act->sa_mask))
  59. return -EFAULT;
  60. siginitset(&new_ka.sa.sa_mask, mask);
  61. new_ka.ka_restorer = NULL;
  62. }
  63. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  64. if (!ret && oact) {
  65. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  66. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  67. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  68. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  69. return -EFAULT;
  70. }
  71. return ret;
  72. }
  73. SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act,
  74. struct sigaction __user *, oact,
  75. size_t, sigsetsize, void __user *, restorer)
  76. {
  77. struct k_sigaction new_ka, old_ka;
  78. int ret;
  79. /* XXX: Don't preclude handling different sized sigset_t's. */
  80. if (sigsetsize != sizeof(sigset_t))
  81. return -EINVAL;
  82. if (act) {
  83. new_ka.ka_restorer = restorer;
  84. if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
  85. return -EFAULT;
  86. }
  87. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  88. if (!ret && oact) {
  89. if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
  90. return -EFAULT;
  91. }
  92. return ret;
  93. }
  94. /*
  95. * Do a signal return; undo the signal stack.
  96. */
  97. #if _NSIG_WORDS > 1
  98. # error "Non SA_SIGINFO frame needs rearranging"
  99. #endif
  100. struct sigframe
  101. {
  102. struct sigcontext sc;
  103. unsigned int retcode[3];
  104. };
  105. struct rt_sigframe
  106. {
  107. struct siginfo info;
  108. struct ucontext uc;
  109. unsigned int retcode[3];
  110. };
  111. /* If this changes, userland unwinders that Know Things about our signal
  112. frame will break. Do not undertake lightly. It also implies an ABI
  113. change wrt the size of siginfo_t, which may cause some pain. */
  114. extern char compile_time_assert
  115. [offsetof(struct rt_sigframe, uc.uc_mcontext) == 176 ? 1 : -1];
  116. #define INSN_MOV_R30_R16 0x47fe0410
  117. #define INSN_LDI_R0 0x201f0000
  118. #define INSN_CALLSYS 0x00000083
  119. static long
  120. restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
  121. {
  122. unsigned long usp;
  123. struct switch_stack *sw = (struct switch_stack *)regs - 1;
  124. long i, err = __get_user(regs->pc, &sc->sc_pc);
  125. current->restart_block.fn = do_no_restart_syscall;
  126. sw->r26 = (unsigned long) ret_from_sys_call;
  127. err |= __get_user(regs->r0, sc->sc_regs+0);
  128. err |= __get_user(regs->r1, sc->sc_regs+1);
  129. err |= __get_user(regs->r2, sc->sc_regs+2);
  130. err |= __get_user(regs->r3, sc->sc_regs+3);
  131. err |= __get_user(regs->r4, sc->sc_regs+4);
  132. err |= __get_user(regs->r5, sc->sc_regs+5);
  133. err |= __get_user(regs->r6, sc->sc_regs+6);
  134. err |= __get_user(regs->r7, sc->sc_regs+7);
  135. err |= __get_user(regs->r8, sc->sc_regs+8);
  136. err |= __get_user(sw->r9, sc->sc_regs+9);
  137. err |= __get_user(sw->r10, sc->sc_regs+10);
  138. err |= __get_user(sw->r11, sc->sc_regs+11);
  139. err |= __get_user(sw->r12, sc->sc_regs+12);
  140. err |= __get_user(sw->r13, sc->sc_regs+13);
  141. err |= __get_user(sw->r14, sc->sc_regs+14);
  142. err |= __get_user(sw->r15, sc->sc_regs+15);
  143. err |= __get_user(regs->r16, sc->sc_regs+16);
  144. err |= __get_user(regs->r17, sc->sc_regs+17);
  145. err |= __get_user(regs->r18, sc->sc_regs+18);
  146. err |= __get_user(regs->r19, sc->sc_regs+19);
  147. err |= __get_user(regs->r20, sc->sc_regs+20);
  148. err |= __get_user(regs->r21, sc->sc_regs+21);
  149. err |= __get_user(regs->r22, sc->sc_regs+22);
  150. err |= __get_user(regs->r23, sc->sc_regs+23);
  151. err |= __get_user(regs->r24, sc->sc_regs+24);
  152. err |= __get_user(regs->r25, sc->sc_regs+25);
  153. err |= __get_user(regs->r26, sc->sc_regs+26);
  154. err |= __get_user(regs->r27, sc->sc_regs+27);
  155. err |= __get_user(regs->r28, sc->sc_regs+28);
  156. err |= __get_user(regs->gp, sc->sc_regs+29);
  157. err |= __get_user(usp, sc->sc_regs+30);
  158. wrusp(usp);
  159. for (i = 0; i < 31; i++)
  160. err |= __get_user(sw->fp[i], sc->sc_fpregs+i);
  161. err |= __get_user(sw->fp[31], &sc->sc_fpcr);
  162. return err;
  163. }
  164. /* Note that this syscall is also used by setcontext(3) to install
  165. a given sigcontext. This because it's impossible to set *all*
  166. registers and transfer control from userland. */
  167. asmlinkage void
  168. do_sigreturn(struct sigcontext __user *sc)
  169. {
  170. struct pt_regs *regs = current_pt_regs();
  171. sigset_t set;
  172. /* Verify that it's a good sigcontext before using it */
  173. if (!access_ok(VERIFY_READ, sc, sizeof(*sc)))
  174. goto give_sigsegv;
  175. if (__get_user(set.sig[0], &sc->sc_mask))
  176. goto give_sigsegv;
  177. set_current_blocked(&set);
  178. if (restore_sigcontext(sc, regs))
  179. goto give_sigsegv;
  180. /* Send SIGTRAP if we're single-stepping: */
  181. if (ptrace_cancel_bpt (current)) {
  182. siginfo_t info;
  183. info.si_signo = SIGTRAP;
  184. info.si_errno = 0;
  185. info.si_code = TRAP_BRKPT;
  186. info.si_addr = (void __user *) regs->pc;
  187. info.si_trapno = 0;
  188. send_sig_info(SIGTRAP, &info, current);
  189. }
  190. return;
  191. give_sigsegv:
  192. force_sig(SIGSEGV, current);
  193. }
  194. asmlinkage void
  195. do_rt_sigreturn(struct rt_sigframe __user *frame)
  196. {
  197. struct pt_regs *regs = current_pt_regs();
  198. sigset_t set;
  199. /* Verify that it's a good ucontext_t before using it */
  200. if (!access_ok(VERIFY_READ, &frame->uc, sizeof(frame->uc)))
  201. goto give_sigsegv;
  202. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  203. goto give_sigsegv;
  204. set_current_blocked(&set);
  205. if (restore_sigcontext(&frame->uc.uc_mcontext, regs))
  206. goto give_sigsegv;
  207. /* Send SIGTRAP if we're single-stepping: */
  208. if (ptrace_cancel_bpt (current)) {
  209. siginfo_t info;
  210. info.si_signo = SIGTRAP;
  211. info.si_errno = 0;
  212. info.si_code = TRAP_BRKPT;
  213. info.si_addr = (void __user *) regs->pc;
  214. info.si_trapno = 0;
  215. send_sig_info(SIGTRAP, &info, current);
  216. }
  217. return;
  218. give_sigsegv:
  219. force_sig(SIGSEGV, current);
  220. }
  221. /*
  222. * Set up a signal frame.
  223. */
  224. static inline void __user *
  225. get_sigframe(struct ksignal *ksig, unsigned long sp, size_t frame_size)
  226. {
  227. return (void __user *)((sigsp(sp, ksig) - frame_size) & -32ul);
  228. }
  229. static long
  230. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  231. unsigned long mask, unsigned long sp)
  232. {
  233. struct switch_stack *sw = (struct switch_stack *)regs - 1;
  234. long i, err = 0;
  235. err |= __put_user(on_sig_stack((unsigned long)sc), &sc->sc_onstack);
  236. err |= __put_user(mask, &sc->sc_mask);
  237. err |= __put_user(regs->pc, &sc->sc_pc);
  238. err |= __put_user(8, &sc->sc_ps);
  239. err |= __put_user(regs->r0 , sc->sc_regs+0);
  240. err |= __put_user(regs->r1 , sc->sc_regs+1);
  241. err |= __put_user(regs->r2 , sc->sc_regs+2);
  242. err |= __put_user(regs->r3 , sc->sc_regs+3);
  243. err |= __put_user(regs->r4 , sc->sc_regs+4);
  244. err |= __put_user(regs->r5 , sc->sc_regs+5);
  245. err |= __put_user(regs->r6 , sc->sc_regs+6);
  246. err |= __put_user(regs->r7 , sc->sc_regs+7);
  247. err |= __put_user(regs->r8 , sc->sc_regs+8);
  248. err |= __put_user(sw->r9 , sc->sc_regs+9);
  249. err |= __put_user(sw->r10 , sc->sc_regs+10);
  250. err |= __put_user(sw->r11 , sc->sc_regs+11);
  251. err |= __put_user(sw->r12 , sc->sc_regs+12);
  252. err |= __put_user(sw->r13 , sc->sc_regs+13);
  253. err |= __put_user(sw->r14 , sc->sc_regs+14);
  254. err |= __put_user(sw->r15 , sc->sc_regs+15);
  255. err |= __put_user(regs->r16, sc->sc_regs+16);
  256. err |= __put_user(regs->r17, sc->sc_regs+17);
  257. err |= __put_user(regs->r18, sc->sc_regs+18);
  258. err |= __put_user(regs->r19, sc->sc_regs+19);
  259. err |= __put_user(regs->r20, sc->sc_regs+20);
  260. err |= __put_user(regs->r21, sc->sc_regs+21);
  261. err |= __put_user(regs->r22, sc->sc_regs+22);
  262. err |= __put_user(regs->r23, sc->sc_regs+23);
  263. err |= __put_user(regs->r24, sc->sc_regs+24);
  264. err |= __put_user(regs->r25, sc->sc_regs+25);
  265. err |= __put_user(regs->r26, sc->sc_regs+26);
  266. err |= __put_user(regs->r27, sc->sc_regs+27);
  267. err |= __put_user(regs->r28, sc->sc_regs+28);
  268. err |= __put_user(regs->gp , sc->sc_regs+29);
  269. err |= __put_user(sp, sc->sc_regs+30);
  270. err |= __put_user(0, sc->sc_regs+31);
  271. for (i = 0; i < 31; i++)
  272. err |= __put_user(sw->fp[i], sc->sc_fpregs+i);
  273. err |= __put_user(0, sc->sc_fpregs+31);
  274. err |= __put_user(sw->fp[31], &sc->sc_fpcr);
  275. err |= __put_user(regs->trap_a0, &sc->sc_traparg_a0);
  276. err |= __put_user(regs->trap_a1, &sc->sc_traparg_a1);
  277. err |= __put_user(regs->trap_a2, &sc->sc_traparg_a2);
  278. return err;
  279. }
  280. static int
  281. setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  282. {
  283. unsigned long oldsp, r26, err = 0;
  284. struct sigframe __user *frame;
  285. oldsp = rdusp();
  286. frame = get_sigframe(ksig, oldsp, sizeof(*frame));
  287. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  288. return -EFAULT;
  289. err |= setup_sigcontext(&frame->sc, regs, set->sig[0], oldsp);
  290. if (err)
  291. return -EFAULT;
  292. /* Set up to return from userspace. If provided, use a stub
  293. already in userspace. */
  294. r26 = (unsigned long) ksig->ka.ka_restorer;
  295. if (!r26) {
  296. err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
  297. err |= __put_user(INSN_LDI_R0+__NR_sigreturn, frame->retcode+1);
  298. err |= __put_user(INSN_CALLSYS, frame->retcode+2);
  299. imb();
  300. r26 = (unsigned long) frame->retcode;
  301. }
  302. /* Check that everything was written properly. */
  303. if (err)
  304. return err;
  305. /* "Return" to the handler */
  306. regs->r26 = r26;
  307. regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  308. regs->r16 = ksig->sig; /* a0: signal number */
  309. regs->r17 = 0; /* a1: exception code */
  310. regs->r18 = (unsigned long) &frame->sc; /* a2: sigcontext pointer */
  311. wrusp((unsigned long) frame);
  312. #if DEBUG_SIG
  313. printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
  314. current->comm, current->pid, frame, regs->pc, regs->r26);
  315. #endif
  316. return 0;
  317. }
  318. static int
  319. setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  320. {
  321. unsigned long oldsp, r26, err = 0;
  322. struct rt_sigframe __user *frame;
  323. oldsp = rdusp();
  324. frame = get_sigframe(ksig, oldsp, sizeof(*frame));
  325. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  326. return -EFAULT;
  327. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  328. /* Create the ucontext. */
  329. err |= __put_user(0, &frame->uc.uc_flags);
  330. err |= __put_user(0, &frame->uc.uc_link);
  331. err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask);
  332. err |= __save_altstack(&frame->uc.uc_stack, oldsp);
  333. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs,
  334. set->sig[0], oldsp);
  335. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  336. if (err)
  337. return -EFAULT;
  338. /* Set up to return from userspace. If provided, use a stub
  339. already in userspace. */
  340. r26 = (unsigned long) ksig->ka.ka_restorer;
  341. if (!r26) {
  342. err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
  343. err |= __put_user(INSN_LDI_R0+__NR_rt_sigreturn,
  344. frame->retcode+1);
  345. err |= __put_user(INSN_CALLSYS, frame->retcode+2);
  346. imb();
  347. r26 = (unsigned long) frame->retcode;
  348. }
  349. if (err)
  350. return -EFAULT;
  351. /* "Return" to the handler */
  352. regs->r26 = r26;
  353. regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  354. regs->r16 = ksig->sig; /* a0: signal number */
  355. regs->r17 = (unsigned long) &frame->info; /* a1: siginfo pointer */
  356. regs->r18 = (unsigned long) &frame->uc; /* a2: ucontext pointer */
  357. wrusp((unsigned long) frame);
  358. #if DEBUG_SIG
  359. printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
  360. current->comm, current->pid, frame, regs->pc, regs->r26);
  361. #endif
  362. return 0;
  363. }
  364. /*
  365. * OK, we're invoking a handler.
  366. */
  367. static inline void
  368. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  369. {
  370. sigset_t *oldset = sigmask_to_save();
  371. int ret;
  372. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  373. ret = setup_rt_frame(ksig, oldset, regs);
  374. else
  375. ret = setup_frame(ksig, oldset, regs);
  376. signal_setup_done(ret, ksig, 0);
  377. }
  378. static inline void
  379. syscall_restart(unsigned long r0, unsigned long r19,
  380. struct pt_regs *regs, struct k_sigaction *ka)
  381. {
  382. switch (regs->r0) {
  383. case ERESTARTSYS:
  384. if (!(ka->sa.sa_flags & SA_RESTART)) {
  385. case ERESTARTNOHAND:
  386. regs->r0 = EINTR;
  387. break;
  388. }
  389. /* fallthrough */
  390. case ERESTARTNOINTR:
  391. regs->r0 = r0; /* reset v0 and a3 and replay syscall */
  392. regs->r19 = r19;
  393. regs->pc -= 4;
  394. break;
  395. case ERESTART_RESTARTBLOCK:
  396. regs->r0 = EINTR;
  397. break;
  398. }
  399. }
  400. /*
  401. * Note that 'init' is a special process: it doesn't get signals it doesn't
  402. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  403. * mistake.
  404. *
  405. * Note that we go through the signals twice: once to check the signals that
  406. * the kernel can handle, and then we build all the user-level signal handling
  407. * stack-frames in one go after that.
  408. *
  409. * "r0" and "r19" are the registers we need to restore for system call
  410. * restart. "r0" is also used as an indicator whether we can restart at
  411. * all (if we get here from anything but a syscall return, it will be 0)
  412. */
  413. static void
  414. do_signal(struct pt_regs *regs, unsigned long r0, unsigned long r19)
  415. {
  416. unsigned long single_stepping = ptrace_cancel_bpt(current);
  417. struct ksignal ksig;
  418. /* This lets the debugger run, ... */
  419. if (get_signal(&ksig)) {
  420. /* ... so re-check the single stepping. */
  421. single_stepping |= ptrace_cancel_bpt(current);
  422. /* Whee! Actually deliver the signal. */
  423. if (r0)
  424. syscall_restart(r0, r19, regs, &ksig.ka);
  425. handle_signal(&ksig, regs);
  426. } else {
  427. single_stepping |= ptrace_cancel_bpt(current);
  428. if (r0) {
  429. switch (regs->r0) {
  430. case ERESTARTNOHAND:
  431. case ERESTARTSYS:
  432. case ERESTARTNOINTR:
  433. /* Reset v0 and a3 and replay syscall. */
  434. regs->r0 = r0;
  435. regs->r19 = r19;
  436. regs->pc -= 4;
  437. break;
  438. case ERESTART_RESTARTBLOCK:
  439. /* Set v0 to the restart_syscall and replay */
  440. regs->r0 = __NR_restart_syscall;
  441. regs->pc -= 4;
  442. break;
  443. }
  444. }
  445. restore_saved_sigmask();
  446. }
  447. if (single_stepping)
  448. ptrace_set_bpt(current); /* re-set breakpoint */
  449. }
  450. void
  451. do_work_pending(struct pt_regs *regs, unsigned long thread_flags,
  452. unsigned long r0, unsigned long r19)
  453. {
  454. do {
  455. if (thread_flags & _TIF_NEED_RESCHED) {
  456. schedule();
  457. } else {
  458. local_irq_enable();
  459. if (thread_flags & _TIF_SIGPENDING) {
  460. do_signal(regs, r0, r19);
  461. r0 = 0;
  462. } else {
  463. clear_thread_flag(TIF_NOTIFY_RESUME);
  464. tracehook_notify_resume(regs);
  465. }
  466. }
  467. local_irq_disable();
  468. thread_flags = current_thread_info()->flags;
  469. } while (thread_flags & _TIF_WORK_MASK);
  470. }