signal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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/smp_lock.h>
  17. #include <linux/kernel.h>
  18. #include <linux/signal.h>
  19. #include <linux/errno.h>
  20. #include <linux/wait.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/unistd.h>
  23. #include <linux/stddef.h>
  24. #include <linux/tty.h>
  25. #include <linux/personality.h>
  26. #include <linux/binfmts.h>
  27. #include <asm/ucontext.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/lowcore.h>
  30. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  31. typedef struct
  32. {
  33. __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
  34. struct sigcontext sc;
  35. _sigregs sregs;
  36. int signo;
  37. __u8 retcode[S390_SYSCALL_SIZE];
  38. } sigframe;
  39. typedef struct
  40. {
  41. __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
  42. __u8 retcode[S390_SYSCALL_SIZE];
  43. struct siginfo info;
  44. struct ucontext uc;
  45. } rt_sigframe;
  46. /*
  47. * Atomically swap in the new signal mask, and wait for a signal.
  48. */
  49. asmlinkage int
  50. sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  51. {
  52. mask &= _BLOCKABLE;
  53. spin_lock_irq(&current->sighand->siglock);
  54. current->saved_sigmask = current->blocked;
  55. siginitset(&current->blocked, mask);
  56. recalc_sigpending();
  57. spin_unlock_irq(&current->sighand->siglock);
  58. current->state = TASK_INTERRUPTIBLE;
  59. schedule();
  60. set_thread_flag(TIF_RESTORE_SIGMASK);
  61. return -ERESTARTNOHAND;
  62. }
  63. asmlinkage long
  64. sys_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. return -EFAULT;
  75. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  76. __get_user(mask, &act->sa_mask);
  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. return -EFAULT;
  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. }
  88. return ret;
  89. }
  90. asmlinkage long
  91. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  92. struct pt_regs *regs)
  93. {
  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. unsigned long old_mask = regs->psw.mask;
  100. int err;
  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. regs->psw.mask = PSW_MASK_MERGE(PSW_USER_BITS, regs->psw.mask);
  105. err = __copy_to_user(&sregs->regs.psw, &regs->psw,
  106. sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
  107. regs->psw.mask = old_mask;
  108. if (err != 0)
  109. return err;
  110. err = __copy_to_user(&sregs->regs.acrs, current->thread.acrs,
  111. sizeof(sregs->regs.acrs));
  112. if (err != 0)
  113. return err;
  114. /*
  115. * We have to store the fp registers to current->thread.fp_regs
  116. * to merge them with the emulated registers.
  117. */
  118. save_fp_regs(&current->thread.fp_regs);
  119. return __copy_to_user(&sregs->fpregs, &current->thread.fp_regs,
  120. sizeof(s390_fp_regs));
  121. }
  122. /* Returns positive number on error */
  123. static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  124. {
  125. unsigned long old_mask = regs->psw.mask;
  126. int err;
  127. /* Alwys make any pending restarted system call return -EINTR */
  128. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  129. err = __copy_from_user(&regs->psw, &sregs->regs.psw,
  130. sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
  131. regs->psw.mask = PSW_MASK_MERGE(old_mask, regs->psw.mask);
  132. regs->psw.addr |= PSW_ADDR_AMODE;
  133. if (err)
  134. return err;
  135. err = __copy_from_user(&current->thread.acrs, &sregs->regs.acrs,
  136. sizeof(sregs->regs.acrs));
  137. if (err)
  138. return err;
  139. restore_access_regs(current->thread.acrs);
  140. err = __copy_from_user(&current->thread.fp_regs, &sregs->fpregs,
  141. sizeof(s390_fp_regs));
  142. current->thread.fp_regs.fpc &= FPC_VALID_MASK;
  143. if (err)
  144. return err;
  145. restore_fp_regs(&current->thread.fp_regs);
  146. regs->trap = -1; /* disable syscall checks */
  147. return 0;
  148. }
  149. asmlinkage long sys_sigreturn(struct pt_regs *regs)
  150. {
  151. sigframe __user *frame = (sigframe __user *)regs->gprs[15];
  152. sigset_t set;
  153. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  154. goto badframe;
  155. if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
  156. goto badframe;
  157. sigdelsetmask(&set, ~_BLOCKABLE);
  158. spin_lock_irq(&current->sighand->siglock);
  159. current->blocked = set;
  160. recalc_sigpending();
  161. spin_unlock_irq(&current->sighand->siglock);
  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. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  170. {
  171. rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
  172. sigset_t set;
  173. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  174. goto badframe;
  175. if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
  176. goto badframe;
  177. sigdelsetmask(&set, ~_BLOCKABLE);
  178. spin_lock_irq(&current->sighand->siglock);
  179. current->blocked = set;
  180. recalc_sigpending();
  181. spin_unlock_irq(&current->sighand->siglock);
  182. if (restore_sigregs(regs, &frame->uc.uc_mcontext))
  183. goto badframe;
  184. if (do_sigaltstack(&frame->uc.uc_stack, NULL,
  185. regs->gprs[15]) == -EFAULT)
  186. goto badframe;
  187. return regs->gprs[2];
  188. badframe:
  189. force_sig(SIGSEGV, current);
  190. return 0;
  191. }
  192. /*
  193. * Set up a signal frame.
  194. */
  195. /*
  196. * Determine which stack to use..
  197. */
  198. static inline void __user *
  199. get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
  200. {
  201. unsigned long sp;
  202. /* Default to using normal stack */
  203. sp = regs->gprs[15];
  204. /* This is the X/Open sanctioned signal stack switching. */
  205. if (ka->sa.sa_flags & SA_ONSTACK) {
  206. if (! sas_ss_flags(sp))
  207. sp = current->sas_ss_sp + current->sas_ss_size;
  208. }
  209. /* This is the legacy signal stack switching. */
  210. else if (!user_mode(regs) &&
  211. !(ka->sa.sa_flags & SA_RESTORER) &&
  212. ka->sa.sa_restorer) {
  213. sp = (unsigned long) ka->sa.sa_restorer;
  214. }
  215. return (void __user *)((sp - frame_size) & -8ul);
  216. }
  217. static inline int map_signal(int sig)
  218. {
  219. if (current_thread_info()->exec_domain
  220. && current_thread_info()->exec_domain->signal_invmap
  221. && sig < 32)
  222. return current_thread_info()->exec_domain->signal_invmap[sig];
  223. else
  224. return sig;
  225. }
  226. static int setup_frame(int sig, struct k_sigaction *ka,
  227. sigset_t *set, struct pt_regs * regs)
  228. {
  229. sigframe __user *frame;
  230. frame = get_sigframe(ka, regs, sizeof(sigframe));
  231. if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
  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. /* Place signal number on stack to allow backtrace from handler. */
  264. if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
  265. goto give_sigsegv;
  266. return 0;
  267. give_sigsegv:
  268. force_sigsegv(sig, current);
  269. return -EFAULT;
  270. }
  271. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  272. sigset_t *set, struct pt_regs * regs)
  273. {
  274. int err = 0;
  275. rt_sigframe __user *frame;
  276. frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
  277. if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
  278. goto give_sigsegv;
  279. if (copy_siginfo_to_user(&frame->info, info))
  280. goto give_sigsegv;
  281. /* Create the ucontext. */
  282. err |= __put_user(0, &frame->uc.uc_flags);
  283. err |= __put_user(NULL, &frame->uc.uc_link);
  284. err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  285. err |= __put_user(sas_ss_flags(regs->gprs[15]),
  286. &frame->uc.uc_stack.ss_flags);
  287. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  288. err |= save_sigregs(regs, &frame->uc.uc_mcontext);
  289. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  290. if (err)
  291. goto give_sigsegv;
  292. /* Set up to return from userspace. If provided, use a stub
  293. already in userspace. */
  294. if (ka->sa.sa_flags & SA_RESTORER) {
  295. regs->gprs[14] = (unsigned long)
  296. ka->sa.sa_restorer | PSW_ADDR_AMODE;
  297. } else {
  298. regs->gprs[14] = (unsigned long)
  299. frame->retcode | PSW_ADDR_AMODE;
  300. if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
  301. (u16 __user *)(frame->retcode)))
  302. goto give_sigsegv;
  303. }
  304. /* Set up backchain. */
  305. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  306. goto give_sigsegv;
  307. /* Set up registers for signal handler */
  308. regs->gprs[15] = (unsigned long) frame;
  309. regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
  310. regs->gprs[2] = map_signal(sig);
  311. regs->gprs[3] = (unsigned long) &frame->info;
  312. regs->gprs[4] = (unsigned long) &frame->uc;
  313. return 0;
  314. give_sigsegv:
  315. force_sigsegv(sig, current);
  316. return -EFAULT;
  317. }
  318. /*
  319. * OK, we're invoking a handler
  320. */
  321. static int
  322. handle_signal(unsigned long sig, struct k_sigaction *ka,
  323. siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
  324. {
  325. int ret;
  326. /* Set up the stack frame */
  327. if (ka->sa.sa_flags & SA_SIGINFO)
  328. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  329. else
  330. ret = setup_frame(sig, ka, oldset, regs);
  331. if (ret == 0) {
  332. spin_lock_irq(&current->sighand->siglock);
  333. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  334. if (!(ka->sa.sa_flags & SA_NODEFER))
  335. sigaddset(&current->blocked,sig);
  336. recalc_sigpending();
  337. spin_unlock_irq(&current->sighand->siglock);
  338. }
  339. return ret;
  340. }
  341. /*
  342. * Note that 'init' is a special process: it doesn't get signals it doesn't
  343. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  344. * mistake.
  345. *
  346. * Note that we go through the signals twice: once to check the signals that
  347. * the kernel can handle, and then we build all the user-level signal handling
  348. * stack-frames in one go after that.
  349. */
  350. void do_signal(struct pt_regs *regs)
  351. {
  352. unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
  353. siginfo_t info;
  354. int signr;
  355. struct k_sigaction ka;
  356. sigset_t *oldset;
  357. /*
  358. * We want the common case to go fast, which
  359. * is why we may in certain cases get here from
  360. * kernel mode. Just return without doing anything
  361. * if so.
  362. */
  363. if (!user_mode(regs))
  364. return;
  365. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  366. oldset = &current->saved_sigmask;
  367. else
  368. oldset = &current->blocked;
  369. /* Are we from a system call? */
  370. if (regs->trap == __LC_SVC_OLD_PSW) {
  371. continue_addr = regs->psw.addr;
  372. restart_addr = continue_addr - regs->ilc;
  373. retval = regs->gprs[2];
  374. /* Prepare for system call restart. We do this here so that a
  375. debugger will see the already changed PSW. */
  376. switch (retval) {
  377. case -ERESTARTNOHAND:
  378. case -ERESTARTSYS:
  379. case -ERESTARTNOINTR:
  380. regs->gprs[2] = regs->orig_gpr2;
  381. regs->psw.addr = restart_addr;
  382. break;
  383. case -ERESTART_RESTARTBLOCK:
  384. regs->gprs[2] = -EINTR;
  385. }
  386. }
  387. /* Get signal to deliver. When running under ptrace, at this point
  388. the debugger may change all our registers ... */
  389. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  390. /* Depending on the signal settings we may need to revert the
  391. decision to restart the system call. */
  392. if (signr > 0 && regs->psw.addr == restart_addr) {
  393. if (retval == -ERESTARTNOHAND
  394. || (retval == -ERESTARTSYS
  395. && !(current->sighand->action[signr-1].sa.sa_flags
  396. & SA_RESTART))) {
  397. regs->gprs[2] = -EINTR;
  398. regs->psw.addr = continue_addr;
  399. }
  400. }
  401. if (signr > 0) {
  402. /* Whee! Actually deliver the signal. */
  403. #ifdef CONFIG_COMPAT
  404. if (test_thread_flag(TIF_31BIT)) {
  405. extern int handle_signal32(unsigned long sig,
  406. struct k_sigaction *ka,
  407. siginfo_t *info,
  408. sigset_t *oldset,
  409. struct pt_regs *regs);
  410. if (handle_signal32(
  411. signr, &ka, &info, oldset, regs) == 0) {
  412. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  413. clear_thread_flag(TIF_RESTORE_SIGMASK);
  414. }
  415. return;
  416. }
  417. #endif
  418. if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
  419. /*
  420. * A signal was successfully delivered; the saved
  421. * sigmask will have been stored in the signal frame,
  422. * and will be restored by sigreturn, so we can simply
  423. * clear the TIF_RESTORE_SIGMASK flag.
  424. */
  425. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  426. clear_thread_flag(TIF_RESTORE_SIGMASK);
  427. }
  428. return;
  429. }
  430. /*
  431. * If there's no signal to deliver, we just put the saved sigmask back.
  432. */
  433. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  434. clear_thread_flag(TIF_RESTORE_SIGMASK);
  435. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  436. }
  437. /* Restart a different system call. */
  438. if (retval == -ERESTART_RESTARTBLOCK
  439. && regs->psw.addr == continue_addr) {
  440. regs->gprs[2] = __NR_restart_syscall;
  441. set_thread_flag(TIF_RESTART_SVC);
  442. }
  443. }