signal.c 15 KB

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