signal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * arch/s390/kernel/signal.c
  3. *
  4. * Copyright (C) IBM Corp. 1999,2006
  5. * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  6. *
  7. * Based on Intel version
  8. *
  9. * Copyright (C) 1991, 1992 Linus Torvalds
  10. *
  11. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  12. */
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.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/stddef.h>
  23. #include <linux/tty.h>
  24. #include <linux/personality.h>
  25. #include <linux/binfmts.h>
  26. #include <linux/tracehook.h>
  27. #include <linux/syscalls.h>
  28. #include <asm/ucontext.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/lowcore.h>
  31. #include "entry.h"
  32. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  33. typedef struct
  34. {
  35. __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
  36. struct sigcontext sc;
  37. _sigregs sregs;
  38. int signo;
  39. __u8 retcode[S390_SYSCALL_SIZE];
  40. } sigframe;
  41. typedef struct
  42. {
  43. __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
  44. __u8 retcode[S390_SYSCALL_SIZE];
  45. struct siginfo info;
  46. struct ucontext uc;
  47. } rt_sigframe;
  48. /*
  49. * Atomically swap in the new signal mask, and wait for a signal.
  50. */
  51. SYSCALL_DEFINE3(sigsuspend, int, history0, int, history1, old_sigset_t, mask)
  52. {
  53. mask &= _BLOCKABLE;
  54. spin_lock_irq(&current->sighand->siglock);
  55. current->saved_sigmask = current->blocked;
  56. siginitset(&current->blocked, mask);
  57. recalc_sigpending();
  58. spin_unlock_irq(&current->sighand->siglock);
  59. current->state = TASK_INTERRUPTIBLE;
  60. schedule();
  61. set_thread_flag(TIF_RESTORE_SIGMASK);
  62. return -ERESTARTNOHAND;
  63. }
  64. SYSCALL_DEFINE3(sigaction, int, sig, const struct old_sigaction __user *, act,
  65. struct old_sigaction __user *, oact)
  66. {
  67. struct k_sigaction new_ka, old_ka;
  68. int ret;
  69. if (act) {
  70. old_sigset_t mask;
  71. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  72. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  73. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
  74. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  75. __get_user(mask, &act->sa_mask))
  76. return -EFAULT;
  77. siginitset(&new_ka.sa.sa_mask, mask);
  78. }
  79. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  80. if (!ret && oact) {
  81. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  82. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  83. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
  84. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  85. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  86. return -EFAULT;
  87. }
  88. return ret;
  89. }
  90. SYSCALL_DEFINE2(sigaltstack, const stack_t __user *, uss,
  91. stack_t __user *, uoss)
  92. {
  93. struct pt_regs *regs = task_pt_regs(current);
  94. return do_sigaltstack(uss, uoss, regs->gprs[15]);
  95. }
  96. /* Returns non-zero on fault. */
  97. static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  98. {
  99. _sigregs user_sregs;
  100. save_access_regs(current->thread.acrs);
  101. /* Copy a 'clean' PSW mask to the user to avoid leaking
  102. information about whether PER is currently on. */
  103. user_sregs.regs.psw.mask = PSW_MASK_MERGE(psw_user_bits, regs->psw.mask);
  104. user_sregs.regs.psw.addr = regs->psw.addr;
  105. memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
  106. memcpy(&user_sregs.regs.acrs, current->thread.acrs,
  107. sizeof(sregs->regs.acrs));
  108. /*
  109. * We have to store the fp registers to current->thread.fp_regs
  110. * to merge them with the emulated registers.
  111. */
  112. save_fp_regs(&current->thread.fp_regs);
  113. memcpy(&user_sregs.fpregs, &current->thread.fp_regs,
  114. sizeof(s390_fp_regs));
  115. return __copy_to_user(sregs, &user_sregs, sizeof(_sigregs));
  116. }
  117. /* Returns positive number on error */
  118. static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  119. {
  120. int err;
  121. _sigregs user_sregs;
  122. /* Alwys make any pending restarted system call return -EINTR */
  123. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  124. err = __copy_from_user(&user_sregs, sregs, sizeof(_sigregs));
  125. if (err)
  126. return err;
  127. regs->psw.mask = PSW_MASK_MERGE(regs->psw.mask,
  128. user_sregs.regs.psw.mask);
  129. regs->psw.addr = PSW_ADDR_AMODE | user_sregs.regs.psw.addr;
  130. memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
  131. memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
  132. sizeof(sregs->regs.acrs));
  133. restore_access_regs(current->thread.acrs);
  134. memcpy(&current->thread.fp_regs, &user_sregs.fpregs,
  135. sizeof(s390_fp_regs));
  136. current->thread.fp_regs.fpc &= FPC_VALID_MASK;
  137. restore_fp_regs(&current->thread.fp_regs);
  138. regs->svcnr = 0; /* disable syscall checks */
  139. return 0;
  140. }
  141. SYSCALL_DEFINE0(sigreturn)
  142. {
  143. struct pt_regs *regs = task_pt_regs(current);
  144. sigframe __user *frame = (sigframe __user *)regs->gprs[15];
  145. sigset_t set;
  146. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  147. goto badframe;
  148. if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
  149. goto badframe;
  150. sigdelsetmask(&set, ~_BLOCKABLE);
  151. spin_lock_irq(&current->sighand->siglock);
  152. current->blocked = set;
  153. recalc_sigpending();
  154. spin_unlock_irq(&current->sighand->siglock);
  155. if (restore_sigregs(regs, &frame->sregs))
  156. goto badframe;
  157. return regs->gprs[2];
  158. badframe:
  159. force_sig(SIGSEGV, current);
  160. return 0;
  161. }
  162. SYSCALL_DEFINE0(rt_sigreturn)
  163. {
  164. struct pt_regs *regs = task_pt_regs(current);
  165. rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
  166. sigset_t set;
  167. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  168. goto badframe;
  169. if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
  170. goto badframe;
  171. sigdelsetmask(&set, ~_BLOCKABLE);
  172. spin_lock_irq(&current->sighand->siglock);
  173. current->blocked = set;
  174. recalc_sigpending();
  175. spin_unlock_irq(&current->sighand->siglock);
  176. if (restore_sigregs(regs, &frame->uc.uc_mcontext))
  177. goto badframe;
  178. if (do_sigaltstack(&frame->uc.uc_stack, NULL,
  179. regs->gprs[15]) == -EFAULT)
  180. goto badframe;
  181. return regs->gprs[2];
  182. badframe:
  183. force_sig(SIGSEGV, current);
  184. return 0;
  185. }
  186. /*
  187. * Set up a signal frame.
  188. */
  189. /*
  190. * Determine which stack to use..
  191. */
  192. static inline void __user *
  193. get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
  194. {
  195. unsigned long sp;
  196. /* Default to using normal stack */
  197. sp = regs->gprs[15];
  198. /* Overflow on alternate signal stack gives SIGSEGV. */
  199. if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
  200. return (void __user *) -1UL;
  201. /* This is the X/Open sanctioned signal stack switching. */
  202. if (ka->sa.sa_flags & SA_ONSTACK) {
  203. if (! sas_ss_flags(sp))
  204. sp = current->sas_ss_sp + current->sas_ss_size;
  205. }
  206. /* This is the legacy signal stack switching. */
  207. else if (!user_mode(regs) &&
  208. !(ka->sa.sa_flags & SA_RESTORER) &&
  209. ka->sa.sa_restorer) {
  210. sp = (unsigned long) ka->sa.sa_restorer;
  211. }
  212. return (void __user *)((sp - frame_size) & -8ul);
  213. }
  214. static inline int map_signal(int sig)
  215. {
  216. if (current_thread_info()->exec_domain
  217. && current_thread_info()->exec_domain->signal_invmap
  218. && sig < 32)
  219. return current_thread_info()->exec_domain->signal_invmap[sig];
  220. else
  221. return sig;
  222. }
  223. static int setup_frame(int sig, struct k_sigaction *ka,
  224. sigset_t *set, struct pt_regs * regs)
  225. {
  226. sigframe __user *frame;
  227. frame = get_sigframe(ka, regs, sizeof(sigframe));
  228. if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
  229. goto give_sigsegv;
  230. if (frame == (void __user *) -1UL)
  231. goto give_sigsegv;
  232. if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
  233. goto give_sigsegv;
  234. if (save_sigregs(regs, &frame->sregs))
  235. goto give_sigsegv;
  236. if (__put_user(&frame->sregs, &frame->sc.sregs))
  237. goto give_sigsegv;
  238. /* Set up to return from userspace. If provided, use a stub
  239. already in userspace. */
  240. if (ka->sa.sa_flags & SA_RESTORER) {
  241. regs->gprs[14] = (unsigned long)
  242. ka->sa.sa_restorer | PSW_ADDR_AMODE;
  243. } else {
  244. regs->gprs[14] = (unsigned long)
  245. frame->retcode | PSW_ADDR_AMODE;
  246. if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
  247. (u16 __user *)(frame->retcode)))
  248. goto give_sigsegv;
  249. }
  250. /* Set up backchain. */
  251. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  252. goto give_sigsegv;
  253. /* Set up registers for signal handler */
  254. regs->gprs[15] = (unsigned long) frame;
  255. regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
  256. regs->gprs[2] = map_signal(sig);
  257. regs->gprs[3] = (unsigned long) &frame->sc;
  258. /* We forgot to include these in the sigcontext.
  259. To avoid breaking binary compatibility, they are passed as args. */
  260. regs->gprs[4] = current->thread.trap_no;
  261. regs->gprs[5] = current->thread.prot_addr;
  262. /* Place signal number on stack to allow backtrace from handler. */
  263. if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
  264. goto give_sigsegv;
  265. return 0;
  266. give_sigsegv:
  267. force_sigsegv(sig, current);
  268. return -EFAULT;
  269. }
  270. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  271. sigset_t *set, struct pt_regs * regs)
  272. {
  273. int err = 0;
  274. rt_sigframe __user *frame;
  275. frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
  276. if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
  277. goto give_sigsegv;
  278. if (frame == (void __user *) -1UL)
  279. goto give_sigsegv;
  280. if (copy_siginfo_to_user(&frame->info, info))
  281. goto give_sigsegv;
  282. /* Create the ucontext. */
  283. err |= __put_user(0, &frame->uc.uc_flags);
  284. err |= __put_user(NULL, &frame->uc.uc_link);
  285. err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  286. err |= __put_user(sas_ss_flags(regs->gprs[15]),
  287. &frame->uc.uc_stack.ss_flags);
  288. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  289. err |= save_sigregs(regs, &frame->uc.uc_mcontext);
  290. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  291. if (err)
  292. goto give_sigsegv;
  293. /* Set up to return from userspace. If provided, use a stub
  294. already in userspace. */
  295. if (ka->sa.sa_flags & SA_RESTORER) {
  296. regs->gprs[14] = (unsigned long)
  297. ka->sa.sa_restorer | PSW_ADDR_AMODE;
  298. } else {
  299. regs->gprs[14] = (unsigned long)
  300. frame->retcode | PSW_ADDR_AMODE;
  301. if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
  302. (u16 __user *)(frame->retcode)))
  303. goto give_sigsegv;
  304. }
  305. /* Set up backchain. */
  306. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  307. goto give_sigsegv;
  308. /* Set up registers for signal handler */
  309. regs->gprs[15] = (unsigned long) frame;
  310. regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
  311. regs->gprs[2] = map_signal(sig);
  312. regs->gprs[3] = (unsigned long) &frame->info;
  313. regs->gprs[4] = (unsigned long) &frame->uc;
  314. return 0;
  315. give_sigsegv:
  316. force_sigsegv(sig, current);
  317. return -EFAULT;
  318. }
  319. /*
  320. * OK, we're invoking a handler
  321. */
  322. static int
  323. handle_signal(unsigned long sig, struct k_sigaction *ka,
  324. siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
  325. {
  326. int ret;
  327. /* Set up the stack frame */
  328. if (ka->sa.sa_flags & SA_SIGINFO)
  329. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  330. else
  331. ret = setup_frame(sig, ka, oldset, regs);
  332. if (ret == 0) {
  333. spin_lock_irq(&current->sighand->siglock);
  334. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  335. if (!(ka->sa.sa_flags & SA_NODEFER))
  336. sigaddset(&current->blocked,sig);
  337. recalc_sigpending();
  338. spin_unlock_irq(&current->sighand->siglock);
  339. }
  340. return ret;
  341. }
  342. /*
  343. * Note that 'init' is a special process: it doesn't get signals it doesn't
  344. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  345. * mistake.
  346. *
  347. * Note that we go through the signals twice: once to check the signals that
  348. * the kernel can handle, and then we build all the user-level signal handling
  349. * stack-frames in one go after that.
  350. */
  351. void do_signal(struct pt_regs *regs)
  352. {
  353. unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
  354. siginfo_t info;
  355. int signr;
  356. struct k_sigaction ka;
  357. sigset_t *oldset;
  358. /*
  359. * We want the common case to go fast, which
  360. * is why we may in certain cases get here from
  361. * kernel mode. Just return without doing anything
  362. * if so.
  363. */
  364. if (!user_mode(regs))
  365. return;
  366. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  367. oldset = &current->saved_sigmask;
  368. else
  369. oldset = &current->blocked;
  370. /* Are we from a system call? */
  371. if (regs->svcnr) {
  372. continue_addr = regs->psw.addr;
  373. restart_addr = continue_addr - regs->ilc;
  374. retval = regs->gprs[2];
  375. /* Prepare for system call restart. We do this here so that a
  376. debugger will see the already changed PSW. */
  377. switch (retval) {
  378. case -ERESTARTNOHAND:
  379. case -ERESTARTSYS:
  380. case -ERESTARTNOINTR:
  381. regs->gprs[2] = regs->orig_gpr2;
  382. regs->psw.addr = restart_addr;
  383. break;
  384. case -ERESTART_RESTARTBLOCK:
  385. regs->gprs[2] = -EINTR;
  386. }
  387. regs->svcnr = 0; /* Don't deal with this again. */
  388. }
  389. /* Get signal to deliver. When running under ptrace, at this point
  390. the debugger may change all our registers ... */
  391. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  392. /* Depending on the signal settings we may need to revert the
  393. decision to restart the system call. */
  394. if (signr > 0 && regs->psw.addr == restart_addr) {
  395. if (retval == -ERESTARTNOHAND
  396. || (retval == -ERESTARTSYS
  397. && !(current->sighand->action[signr-1].sa.sa_flags
  398. & SA_RESTART))) {
  399. regs->gprs[2] = -EINTR;
  400. regs->psw.addr = continue_addr;
  401. }
  402. }
  403. if (signr > 0) {
  404. /* Whee! Actually deliver the signal. */
  405. int ret;
  406. #ifdef CONFIG_COMPAT
  407. if (test_thread_flag(TIF_31BIT)) {
  408. ret = handle_signal32(signr, &ka, &info, oldset, regs);
  409. }
  410. else
  411. #endif
  412. ret = handle_signal(signr, &ka, &info, oldset, regs);
  413. if (!ret) {
  414. /*
  415. * A signal was successfully delivered; the saved
  416. * sigmask will have been stored in the signal frame,
  417. * and will be restored by sigreturn, so we can simply
  418. * clear the TIF_RESTORE_SIGMASK flag.
  419. */
  420. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  421. clear_thread_flag(TIF_RESTORE_SIGMASK);
  422. /*
  423. * If we would have taken a single-step trap
  424. * for a normal instruction, act like we took
  425. * one for the handler setup.
  426. */
  427. if (current->thread.per_info.single_step)
  428. set_thread_flag(TIF_SINGLE_STEP);
  429. /*
  430. * Let tracing know that we've done the handler setup.
  431. */
  432. tracehook_signal_handler(signr, &info, &ka, regs,
  433. test_thread_flag(TIF_SINGLE_STEP));
  434. }
  435. return;
  436. }
  437. /*
  438. * If there's no signal to deliver, we just put the saved sigmask back.
  439. */
  440. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  441. clear_thread_flag(TIF_RESTORE_SIGMASK);
  442. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  443. }
  444. /* Restart a different system call. */
  445. if (retval == -ERESTART_RESTARTBLOCK
  446. && regs->psw.addr == continue_addr) {
  447. regs->gprs[2] = __NR_restart_syscall;
  448. set_thread_flag(TIF_RESTART_SVC);
  449. }
  450. }
  451. void do_notify_resume(struct pt_regs *regs)
  452. {
  453. clear_thread_flag(TIF_NOTIFY_RESUME);
  454. tracehook_notify_resume(regs);
  455. }