signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright IBM Corp. 1999, 2006
  3. * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  4. *
  5. * Based on Intel version
  6. *
  7. * Copyright (C) 1991, 1992 Linus Torvalds
  8. *
  9. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/kernel.h>
  15. #include <linux/signal.h>
  16. #include <linux/errno.h>
  17. #include <linux/wait.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/unistd.h>
  20. #include <linux/stddef.h>
  21. #include <linux/tty.h>
  22. #include <linux/personality.h>
  23. #include <linux/binfmts.h>
  24. #include <linux/tracehook.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/compat.h>
  27. #include <asm/ucontext.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/lowcore.h>
  30. #include <asm/switch_to.h>
  31. #include "entry.h"
  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. SYSCALL_DEFINE3(sigaction, int, sig, const struct old_sigaction __user *, act,
  48. struct old_sigaction __user *, oact)
  49. {
  50. struct k_sigaction new_ka, old_ka;
  51. int ret;
  52. if (act) {
  53. old_sigset_t mask;
  54. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  55. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  56. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
  57. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  58. __get_user(mask, &act->sa_mask))
  59. return -EFAULT;
  60. siginitset(&new_ka.sa.sa_mask, mask);
  61. }
  62. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  63. if (!ret && oact) {
  64. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  65. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  66. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
  67. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  68. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  69. return -EFAULT;
  70. }
  71. return ret;
  72. }
  73. /* Returns non-zero on fault. */
  74. static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  75. {
  76. _sigregs user_sregs;
  77. save_access_regs(current->thread.acrs);
  78. /* Copy a 'clean' PSW mask to the user to avoid leaking
  79. information about whether PER is currently on. */
  80. user_sregs.regs.psw.mask = psw_user_bits |
  81. (regs->psw.mask & PSW_MASK_USER);
  82. user_sregs.regs.psw.addr = regs->psw.addr;
  83. memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
  84. memcpy(&user_sregs.regs.acrs, current->thread.acrs,
  85. sizeof(sregs->regs.acrs));
  86. /*
  87. * We have to store the fp registers to current->thread.fp_regs
  88. * to merge them with the emulated registers.
  89. */
  90. save_fp_regs(&current->thread.fp_regs);
  91. memcpy(&user_sregs.fpregs, &current->thread.fp_regs,
  92. sizeof(s390_fp_regs));
  93. return __copy_to_user(sregs, &user_sregs, sizeof(_sigregs));
  94. }
  95. /* Returns positive number on error */
  96. static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  97. {
  98. int err;
  99. _sigregs user_sregs;
  100. /* Alwys make any pending restarted system call return -EINTR */
  101. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  102. err = __copy_from_user(&user_sregs, sregs, sizeof(_sigregs));
  103. if (err)
  104. return err;
  105. /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */
  106. regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
  107. (user_sregs.regs.psw.mask & PSW_MASK_USER);
  108. /* Check for invalid user address space control. */
  109. if ((regs->psw.mask & PSW_MASK_ASC) >= (psw_kernel_bits & PSW_MASK_ASC))
  110. regs->psw.mask = (psw_user_bits & PSW_MASK_ASC) |
  111. (regs->psw.mask & ~PSW_MASK_ASC);
  112. /* Check for invalid amode */
  113. if (regs->psw.mask & PSW_MASK_EA)
  114. regs->psw.mask |= PSW_MASK_BA;
  115. regs->psw.addr = user_sregs.regs.psw.addr;
  116. memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
  117. memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
  118. sizeof(sregs->regs.acrs));
  119. restore_access_regs(current->thread.acrs);
  120. memcpy(&current->thread.fp_regs, &user_sregs.fpregs,
  121. sizeof(s390_fp_regs));
  122. current->thread.fp_regs.fpc &= FPC_VALID_MASK;
  123. restore_fp_regs(&current->thread.fp_regs);
  124. clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */
  125. return 0;
  126. }
  127. SYSCALL_DEFINE0(sigreturn)
  128. {
  129. struct pt_regs *regs = task_pt_regs(current);
  130. sigframe __user *frame = (sigframe __user *)regs->gprs[15];
  131. sigset_t set;
  132. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  133. goto badframe;
  134. if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
  135. goto badframe;
  136. set_current_blocked(&set);
  137. if (restore_sigregs(regs, &frame->sregs))
  138. goto badframe;
  139. return regs->gprs[2];
  140. badframe:
  141. force_sig(SIGSEGV, current);
  142. return 0;
  143. }
  144. SYSCALL_DEFINE0(rt_sigreturn)
  145. {
  146. struct pt_regs *regs = task_pt_regs(current);
  147. rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
  148. sigset_t set;
  149. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  150. goto badframe;
  151. if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
  152. goto badframe;
  153. set_current_blocked(&set);
  154. if (restore_sigregs(regs, &frame->uc.uc_mcontext))
  155. goto badframe;
  156. if (restore_altstack(&frame->uc.uc_stack))
  157. goto badframe;
  158. return regs->gprs[2];
  159. badframe:
  160. force_sig(SIGSEGV, current);
  161. return 0;
  162. }
  163. /*
  164. * Set up a signal frame.
  165. */
  166. /*
  167. * Determine which stack to use..
  168. */
  169. static inline void __user *
  170. get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
  171. {
  172. unsigned long sp;
  173. /* Default to using normal stack */
  174. sp = regs->gprs[15];
  175. /* Overflow on alternate signal stack gives SIGSEGV. */
  176. if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
  177. return (void __user *) -1UL;
  178. /* This is the X/Open sanctioned signal stack switching. */
  179. if (ka->sa.sa_flags & SA_ONSTACK) {
  180. if (! sas_ss_flags(sp))
  181. sp = current->sas_ss_sp + current->sas_ss_size;
  182. }
  183. return (void __user *)((sp - frame_size) & -8ul);
  184. }
  185. static inline int map_signal(int sig)
  186. {
  187. if (current_thread_info()->exec_domain
  188. && current_thread_info()->exec_domain->signal_invmap
  189. && sig < 32)
  190. return current_thread_info()->exec_domain->signal_invmap[sig];
  191. else
  192. return sig;
  193. }
  194. static int setup_frame(int sig, struct k_sigaction *ka,
  195. sigset_t *set, struct pt_regs * regs)
  196. {
  197. sigframe __user *frame;
  198. frame = get_sigframe(ka, regs, sizeof(sigframe));
  199. if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
  200. goto give_sigsegv;
  201. if (frame == (void __user *) -1UL)
  202. goto give_sigsegv;
  203. if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
  204. goto give_sigsegv;
  205. if (save_sigregs(regs, &frame->sregs))
  206. goto give_sigsegv;
  207. if (__put_user(&frame->sregs, &frame->sc.sregs))
  208. goto give_sigsegv;
  209. /* Set up to return from userspace. If provided, use a stub
  210. already in userspace. */
  211. if (ka->sa.sa_flags & SA_RESTORER) {
  212. regs->gprs[14] = (unsigned long)
  213. ka->sa.sa_restorer | PSW_ADDR_AMODE;
  214. } else {
  215. regs->gprs[14] = (unsigned long)
  216. frame->retcode | PSW_ADDR_AMODE;
  217. if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
  218. (u16 __user *)(frame->retcode)))
  219. goto give_sigsegv;
  220. }
  221. /* Set up backchain. */
  222. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  223. goto give_sigsegv;
  224. /* Set up registers for signal handler */
  225. regs->gprs[15] = (unsigned long) frame;
  226. /* Force default amode and default user address space control. */
  227. regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
  228. (psw_user_bits & PSW_MASK_ASC) |
  229. (regs->psw.mask & ~PSW_MASK_ASC);
  230. regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
  231. regs->gprs[2] = map_signal(sig);
  232. regs->gprs[3] = (unsigned long) &frame->sc;
  233. /* We forgot to include these in the sigcontext.
  234. To avoid breaking binary compatibility, they are passed as args. */
  235. if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
  236. sig == SIGTRAP || sig == SIGFPE) {
  237. /* set extra registers only for synchronous signals */
  238. regs->gprs[4] = regs->int_code & 127;
  239. regs->gprs[5] = regs->int_parm_long;
  240. regs->gprs[6] = task_thread_info(current)->last_break;
  241. }
  242. /* Place signal number on stack to allow backtrace from handler. */
  243. if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
  244. goto give_sigsegv;
  245. return 0;
  246. give_sigsegv:
  247. force_sigsegv(sig, current);
  248. return -EFAULT;
  249. }
  250. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  251. sigset_t *set, struct pt_regs * regs)
  252. {
  253. int err = 0;
  254. rt_sigframe __user *frame;
  255. frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
  256. if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
  257. goto give_sigsegv;
  258. if (frame == (void __user *) -1UL)
  259. goto give_sigsegv;
  260. if (copy_siginfo_to_user(&frame->info, info))
  261. goto give_sigsegv;
  262. /* Create the ucontext. */
  263. err |= __put_user(0, &frame->uc.uc_flags);
  264. err |= __put_user(NULL, &frame->uc.uc_link);
  265. err |= __save_altstack(&frame->uc.uc_stack, regs->gprs[15]);
  266. err |= save_sigregs(regs, &frame->uc.uc_mcontext);
  267. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  268. if (err)
  269. goto give_sigsegv;
  270. /* Set up to return from userspace. If provided, use a stub
  271. already in userspace. */
  272. if (ka->sa.sa_flags & SA_RESTORER) {
  273. regs->gprs[14] = (unsigned long)
  274. ka->sa.sa_restorer | PSW_ADDR_AMODE;
  275. } else {
  276. regs->gprs[14] = (unsigned long)
  277. frame->retcode | PSW_ADDR_AMODE;
  278. if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
  279. (u16 __user *)(frame->retcode)))
  280. goto give_sigsegv;
  281. }
  282. /* Set up backchain. */
  283. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  284. goto give_sigsegv;
  285. /* Set up registers for signal handler */
  286. regs->gprs[15] = (unsigned long) frame;
  287. /* Force default amode and default user address space control. */
  288. regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
  289. (psw_user_bits & PSW_MASK_ASC) |
  290. (regs->psw.mask & ~PSW_MASK_ASC);
  291. regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
  292. regs->gprs[2] = map_signal(sig);
  293. regs->gprs[3] = (unsigned long) &frame->info;
  294. regs->gprs[4] = (unsigned long) &frame->uc;
  295. regs->gprs[5] = task_thread_info(current)->last_break;
  296. return 0;
  297. give_sigsegv:
  298. force_sigsegv(sig, current);
  299. return -EFAULT;
  300. }
  301. static void handle_signal(unsigned long sig, struct k_sigaction *ka,
  302. siginfo_t *info, sigset_t *oldset,
  303. struct pt_regs *regs)
  304. {
  305. int ret;
  306. /* Set up the stack frame */
  307. if (ka->sa.sa_flags & SA_SIGINFO)
  308. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  309. else
  310. ret = setup_frame(sig, ka, oldset, regs);
  311. if (ret)
  312. return;
  313. signal_delivered(sig, info, ka, regs,
  314. test_thread_flag(TIF_SINGLE_STEP));
  315. }
  316. /*
  317. * Note that 'init' is a special process: it doesn't get signals it doesn't
  318. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  319. * mistake.
  320. *
  321. * Note that we go through the signals twice: once to check the signals that
  322. * the kernel can handle, and then we build all the user-level signal handling
  323. * stack-frames in one go after that.
  324. */
  325. void do_signal(struct pt_regs *regs)
  326. {
  327. siginfo_t info;
  328. int signr;
  329. struct k_sigaction ka;
  330. sigset_t *oldset = sigmask_to_save();
  331. /*
  332. * Get signal to deliver. When running under ptrace, at this point
  333. * the debugger may change all our registers, including the system
  334. * call information.
  335. */
  336. current_thread_info()->system_call =
  337. test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
  338. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  339. if (signr > 0) {
  340. /* Whee! Actually deliver the signal. */
  341. if (current_thread_info()->system_call) {
  342. regs->int_code = current_thread_info()->system_call;
  343. /* Check for system call restarting. */
  344. switch (regs->gprs[2]) {
  345. case -ERESTART_RESTARTBLOCK:
  346. case -ERESTARTNOHAND:
  347. regs->gprs[2] = -EINTR;
  348. break;
  349. case -ERESTARTSYS:
  350. if (!(ka.sa.sa_flags & SA_RESTART)) {
  351. regs->gprs[2] = -EINTR;
  352. break;
  353. }
  354. /* fallthrough */
  355. case -ERESTARTNOINTR:
  356. regs->gprs[2] = regs->orig_gpr2;
  357. regs->psw.addr =
  358. __rewind_psw(regs->psw,
  359. regs->int_code >> 16);
  360. break;
  361. }
  362. }
  363. /* No longer in a system call */
  364. clear_thread_flag(TIF_SYSCALL);
  365. if (is_compat_task())
  366. handle_signal32(signr, &ka, &info, oldset, regs);
  367. else
  368. handle_signal(signr, &ka, &info, oldset, regs);
  369. return;
  370. }
  371. /* No handlers present - check for system call restart */
  372. clear_thread_flag(TIF_SYSCALL);
  373. if (current_thread_info()->system_call) {
  374. regs->int_code = current_thread_info()->system_call;
  375. switch (regs->gprs[2]) {
  376. case -ERESTART_RESTARTBLOCK:
  377. /* Restart with sys_restart_syscall */
  378. regs->int_code = __NR_restart_syscall;
  379. /* fallthrough */
  380. case -ERESTARTNOHAND:
  381. case -ERESTARTSYS:
  382. case -ERESTARTNOINTR:
  383. /* Restart system call with magic TIF bit. */
  384. regs->gprs[2] = regs->orig_gpr2;
  385. set_thread_flag(TIF_SYSCALL);
  386. if (test_thread_flag(TIF_SINGLE_STEP))
  387. set_thread_flag(TIF_PER_TRAP);
  388. break;
  389. }
  390. }
  391. /*
  392. * If there's no signal to deliver, we just put the saved sigmask back.
  393. */
  394. restore_saved_sigmask();
  395. }
  396. void do_notify_resume(struct pt_regs *regs)
  397. {
  398. clear_thread_flag(TIF_NOTIFY_RESUME);
  399. tracehook_notify_resume(regs);
  400. }