signal.c 15 KB

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