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/config.h>
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/smp.h>
  17. #include <linux/smp_lock.h>
  18. #include <linux/kernel.h>
  19. #include <linux/signal.h>
  20. #include <linux/errno.h>
  21. #include <linux/wait.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/unistd.h>
  24. #include <linux/stddef.h>
  25. #include <linux/tty.h>
  26. #include <linux/personality.h>
  27. #include <linux/binfmts.h>
  28. #include <asm/ucontext.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/lowcore.h>
  31. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  32. typedef struct
  33. {
  34. __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
  35. struct sigcontext sc;
  36. _sigregs sregs;
  37. int signo;
  38. __u8 retcode[S390_SYSCALL_SIZE];
  39. } sigframe;
  40. typedef struct
  41. {
  42. __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
  43. __u8 retcode[S390_SYSCALL_SIZE];
  44. struct siginfo info;
  45. struct ucontext uc;
  46. } rt_sigframe;
  47. /*
  48. * Atomically swap in the new signal mask, and wait for a signal.
  49. */
  50. asmlinkage int
  51. sys_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. asmlinkage long
  65. sys_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. return -EFAULT;
  76. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  77. __get_user(mask, &act->sa_mask);
  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. return -EFAULT;
  86. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  87. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  88. }
  89. return ret;
  90. }
  91. asmlinkage long
  92. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  93. struct pt_regs *regs)
  94. {
  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. unsigned long old_mask = regs->psw.mask;
  101. int err;
  102. save_access_regs(current->thread.acrs);
  103. /* Copy a 'clean' PSW mask to the user to avoid leaking
  104. information about whether PER is currently on. */
  105. regs->psw.mask = PSW_MASK_MERGE(PSW_USER_BITS, regs->psw.mask);
  106. err = __copy_to_user(&sregs->regs.psw, &regs->psw,
  107. sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
  108. regs->psw.mask = old_mask;
  109. if (err != 0)
  110. return err;
  111. err = __copy_to_user(&sregs->regs.acrs, current->thread.acrs,
  112. sizeof(sregs->regs.acrs));
  113. if (err != 0)
  114. return err;
  115. /*
  116. * We have to store the fp registers to current->thread.fp_regs
  117. * to merge them with the emulated registers.
  118. */
  119. save_fp_regs(&current->thread.fp_regs);
  120. return __copy_to_user(&sregs->fpregs, &current->thread.fp_regs,
  121. sizeof(s390_fp_regs));
  122. }
  123. /* Returns positive number on error */
  124. static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  125. {
  126. unsigned long old_mask = regs->psw.mask;
  127. int err;
  128. /* Alwys make any pending restarted system call return -EINTR */
  129. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  130. err = __copy_from_user(&regs->psw, &sregs->regs.psw,
  131. sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
  132. regs->psw.mask = PSW_MASK_MERGE(old_mask, regs->psw.mask);
  133. regs->psw.addr |= PSW_ADDR_AMODE;
  134. if (err)
  135. return err;
  136. err = __copy_from_user(&current->thread.acrs, &sregs->regs.acrs,
  137. sizeof(sregs->regs.acrs));
  138. if (err)
  139. return err;
  140. restore_access_regs(current->thread.acrs);
  141. err = __copy_from_user(&current->thread.fp_regs, &sregs->fpregs,
  142. sizeof(s390_fp_regs));
  143. current->thread.fp_regs.fpc &= FPC_VALID_MASK;
  144. if (err)
  145. return err;
  146. restore_fp_regs(&current->thread.fp_regs);
  147. regs->trap = -1; /* disable syscall checks */
  148. return 0;
  149. }
  150. asmlinkage long sys_sigreturn(struct pt_regs *regs)
  151. {
  152. sigframe __user *frame = (sigframe __user *)regs->gprs[15];
  153. sigset_t set;
  154. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  155. goto badframe;
  156. if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
  157. goto badframe;
  158. sigdelsetmask(&set, ~_BLOCKABLE);
  159. spin_lock_irq(&current->sighand->siglock);
  160. current->blocked = set;
  161. recalc_sigpending();
  162. spin_unlock_irq(&current->sighand->siglock);
  163. if (restore_sigregs(regs, &frame->sregs))
  164. goto badframe;
  165. return regs->gprs[2];
  166. badframe:
  167. force_sig(SIGSEGV, current);
  168. return 0;
  169. }
  170. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  171. {
  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. spin_lock_irq(&current->sighand->siglock);
  180. current->blocked = set;
  181. recalc_sigpending();
  182. spin_unlock_irq(&current->sighand->siglock);
  183. if (restore_sigregs(regs, &frame->uc.uc_mcontext))
  184. goto badframe;
  185. if (do_sigaltstack(&frame->uc.uc_stack, NULL,
  186. regs->gprs[15]) == -EFAULT)
  187. goto badframe;
  188. return regs->gprs[2];
  189. badframe:
  190. force_sig(SIGSEGV, current);
  191. return 0;
  192. }
  193. /*
  194. * Set up a signal frame.
  195. */
  196. /*
  197. * Determine which stack to use..
  198. */
  199. static inline void __user *
  200. get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
  201. {
  202. unsigned long sp;
  203. /* Default to using normal stack */
  204. sp = regs->gprs[15];
  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 (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
  235. goto give_sigsegv;
  236. if (save_sigregs(regs, &frame->sregs))
  237. goto give_sigsegv;
  238. if (__put_user(&frame->sregs, &frame->sc.sregs))
  239. goto give_sigsegv;
  240. /* Set up to return from userspace. If provided, use a stub
  241. already in userspace. */
  242. if (ka->sa.sa_flags & SA_RESTORER) {
  243. regs->gprs[14] = (unsigned long)
  244. ka->sa.sa_restorer | PSW_ADDR_AMODE;
  245. } else {
  246. regs->gprs[14] = (unsigned long)
  247. frame->retcode | PSW_ADDR_AMODE;
  248. if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
  249. (u16 __user *)(frame->retcode)))
  250. goto give_sigsegv;
  251. }
  252. /* Set up backchain. */
  253. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  254. goto give_sigsegv;
  255. /* Set up registers for signal handler */
  256. regs->gprs[15] = (unsigned long) frame;
  257. regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
  258. regs->gprs[2] = map_signal(sig);
  259. regs->gprs[3] = (unsigned long) &frame->sc;
  260. /* We forgot to include these in the sigcontext.
  261. To avoid breaking binary compatibility, they are passed as args. */
  262. regs->gprs[4] = current->thread.trap_no;
  263. regs->gprs[5] = current->thread.prot_addr;
  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 (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. err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
  302. (u16 __user *)(frame->retcode));
  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. }