signal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. /* Returns non-zero on fault. */
  48. static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  49. {
  50. _sigregs user_sregs;
  51. save_access_regs(current->thread.acrs);
  52. /* Copy a 'clean' PSW mask to the user to avoid leaking
  53. information about whether PER is currently on. */
  54. user_sregs.regs.psw.mask = PSW_USER_BITS |
  55. (regs->psw.mask & (PSW_MASK_USER | PSW_MASK_RI));
  56. user_sregs.regs.psw.addr = regs->psw.addr;
  57. memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
  58. memcpy(&user_sregs.regs.acrs, current->thread.acrs,
  59. sizeof(user_sregs.regs.acrs));
  60. /*
  61. * We have to store the fp registers to current->thread.fp_regs
  62. * to merge them with the emulated registers.
  63. */
  64. save_fp_ctl(&current->thread.fp_regs.fpc);
  65. save_fp_regs(current->thread.fp_regs.fprs);
  66. memcpy(&user_sregs.fpregs, &current->thread.fp_regs,
  67. sizeof(user_sregs.fpregs));
  68. if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs)))
  69. return -EFAULT;
  70. return 0;
  71. }
  72. static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  73. {
  74. _sigregs user_sregs;
  75. /* Alwys make any pending restarted system call return -EINTR */
  76. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  77. if (__copy_from_user(&user_sregs, sregs, sizeof(user_sregs)))
  78. return -EFAULT;
  79. if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW_MASK_RI))
  80. return -EINVAL;
  81. /* Loading the floating-point-control word can fail. Do that first. */
  82. if (restore_fp_ctl(&user_sregs.fpregs.fpc))
  83. return -EINVAL;
  84. /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
  85. regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
  86. (user_sregs.regs.psw.mask & (PSW_MASK_USER | PSW_MASK_RI));
  87. /* Check for invalid user address space control. */
  88. if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
  89. regs->psw.mask = PSW_ASC_PRIMARY |
  90. (regs->psw.mask & ~PSW_MASK_ASC);
  91. /* Check for invalid amode */
  92. if (regs->psw.mask & PSW_MASK_EA)
  93. regs->psw.mask |= PSW_MASK_BA;
  94. regs->psw.addr = user_sregs.regs.psw.addr;
  95. memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
  96. memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
  97. sizeof(current->thread.acrs));
  98. restore_access_regs(current->thread.acrs);
  99. memcpy(&current->thread.fp_regs, &user_sregs.fpregs,
  100. sizeof(current->thread.fp_regs));
  101. restore_fp_regs(current->thread.fp_regs.fprs);
  102. clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
  103. return 0;
  104. }
  105. SYSCALL_DEFINE0(sigreturn)
  106. {
  107. struct pt_regs *regs = task_pt_regs(current);
  108. sigframe __user *frame = (sigframe __user *)regs->gprs[15];
  109. sigset_t set;
  110. if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
  111. goto badframe;
  112. set_current_blocked(&set);
  113. if (restore_sigregs(regs, &frame->sregs))
  114. goto badframe;
  115. return regs->gprs[2];
  116. badframe:
  117. force_sig(SIGSEGV, current);
  118. return 0;
  119. }
  120. SYSCALL_DEFINE0(rt_sigreturn)
  121. {
  122. struct pt_regs *regs = task_pt_regs(current);
  123. rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
  124. sigset_t set;
  125. if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
  126. goto badframe;
  127. set_current_blocked(&set);
  128. if (restore_sigregs(regs, &frame->uc.uc_mcontext))
  129. goto badframe;
  130. if (restore_altstack(&frame->uc.uc_stack))
  131. goto badframe;
  132. return regs->gprs[2];
  133. badframe:
  134. force_sig(SIGSEGV, current);
  135. return 0;
  136. }
  137. /*
  138. * Set up a signal frame.
  139. */
  140. /*
  141. * Determine which stack to use..
  142. */
  143. static inline void __user *
  144. get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
  145. {
  146. unsigned long sp;
  147. /* Default to using normal stack */
  148. sp = regs->gprs[15];
  149. /* Overflow on alternate signal stack gives SIGSEGV. */
  150. if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
  151. return (void __user *) -1UL;
  152. /* This is the X/Open sanctioned signal stack switching. */
  153. if (ka->sa.sa_flags & SA_ONSTACK) {
  154. if (! sas_ss_flags(sp))
  155. sp = current->sas_ss_sp + current->sas_ss_size;
  156. }
  157. return (void __user *)((sp - frame_size) & -8ul);
  158. }
  159. static inline int map_signal(int sig)
  160. {
  161. if (current_thread_info()->exec_domain
  162. && current_thread_info()->exec_domain->signal_invmap
  163. && sig < 32)
  164. return current_thread_info()->exec_domain->signal_invmap[sig];
  165. else
  166. return sig;
  167. }
  168. static int setup_frame(int sig, struct k_sigaction *ka,
  169. sigset_t *set, struct pt_regs * regs)
  170. {
  171. sigframe __user *frame;
  172. frame = get_sigframe(ka, regs, sizeof(sigframe));
  173. if (frame == (void __user *) -1UL)
  174. return -EFAULT;
  175. if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
  176. return -EFAULT;
  177. if (save_sigregs(regs, &frame->sregs))
  178. return -EFAULT;
  179. if (__put_user(&frame->sregs, &frame->sc.sregs))
  180. return -EFAULT;
  181. /* Set up to return from userspace. If provided, use a stub
  182. already in userspace. */
  183. if (ka->sa.sa_flags & SA_RESTORER) {
  184. regs->gprs[14] = (unsigned long)
  185. ka->sa.sa_restorer | PSW_ADDR_AMODE;
  186. } else {
  187. regs->gprs[14] = (unsigned long)
  188. frame->retcode | PSW_ADDR_AMODE;
  189. if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
  190. (u16 __user *)(frame->retcode)))
  191. return -EFAULT;
  192. }
  193. /* Set up backchain. */
  194. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  195. return -EFAULT;
  196. /* Set up registers for signal handler */
  197. regs->gprs[15] = (unsigned long) frame;
  198. /* Force default amode and default user address space control. */
  199. regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
  200. (PSW_USER_BITS & PSW_MASK_ASC) |
  201. (regs->psw.mask & ~PSW_MASK_ASC);
  202. regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
  203. regs->gprs[2] = map_signal(sig);
  204. regs->gprs[3] = (unsigned long) &frame->sc;
  205. /* We forgot to include these in the sigcontext.
  206. To avoid breaking binary compatibility, they are passed as args. */
  207. if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
  208. sig == SIGTRAP || sig == SIGFPE) {
  209. /* set extra registers only for synchronous signals */
  210. regs->gprs[4] = regs->int_code & 127;
  211. regs->gprs[5] = regs->int_parm_long;
  212. regs->gprs[6] = task_thread_info(current)->last_break;
  213. }
  214. /* Place signal number on stack to allow backtrace from handler. */
  215. if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
  216. return -EFAULT;
  217. return 0;
  218. }
  219. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  220. struct pt_regs *regs)
  221. {
  222. int err = 0;
  223. rt_sigframe __user *frame;
  224. frame = get_sigframe(&ksig->ka, regs, sizeof(rt_sigframe));
  225. if (frame == (void __user *) -1UL)
  226. return -EFAULT;
  227. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  228. return -EFAULT;
  229. /* Create the ucontext. */
  230. err |= __put_user(0, &frame->uc.uc_flags);
  231. err |= __put_user(NULL, &frame->uc.uc_link);
  232. err |= __save_altstack(&frame->uc.uc_stack, regs->gprs[15]);
  233. err |= save_sigregs(regs, &frame->uc.uc_mcontext);
  234. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  235. if (err)
  236. return -EFAULT;
  237. /* Set up to return from userspace. If provided, use a stub
  238. already in userspace. */
  239. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  240. regs->gprs[14] = (unsigned long)
  241. ksig->ka.sa.sa_restorer | PSW_ADDR_AMODE;
  242. } else {
  243. regs->gprs[14] = (unsigned long)
  244. frame->retcode | PSW_ADDR_AMODE;
  245. if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
  246. (u16 __user *)(frame->retcode)))
  247. return -EFAULT;
  248. }
  249. /* Set up backchain. */
  250. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  251. return -EFAULT;
  252. /* Set up registers for signal handler */
  253. regs->gprs[15] = (unsigned long) frame;
  254. /* Force default amode and default user address space control. */
  255. regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
  256. (PSW_USER_BITS & PSW_MASK_ASC) |
  257. (regs->psw.mask & ~PSW_MASK_ASC);
  258. regs->psw.addr = (unsigned long) ksig->ka.sa.sa_handler | PSW_ADDR_AMODE;
  259. regs->gprs[2] = map_signal(ksig->sig);
  260. regs->gprs[3] = (unsigned long) &frame->info;
  261. regs->gprs[4] = (unsigned long) &frame->uc;
  262. regs->gprs[5] = task_thread_info(current)->last_break;
  263. return 0;
  264. }
  265. static void handle_signal(struct ksignal *ksig, sigset_t *oldset,
  266. struct pt_regs *regs)
  267. {
  268. int ret;
  269. /* Set up the stack frame */
  270. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  271. ret = setup_rt_frame(ksig, oldset, regs);
  272. else
  273. ret = setup_frame(ksig->sig, &ksig->ka, oldset, regs);
  274. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));
  275. }
  276. /*
  277. * Note that 'init' is a special process: it doesn't get signals it doesn't
  278. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  279. * mistake.
  280. *
  281. * Note that we go through the signals twice: once to check the signals that
  282. * the kernel can handle, and then we build all the user-level signal handling
  283. * stack-frames in one go after that.
  284. */
  285. void do_signal(struct pt_regs *regs)
  286. {
  287. struct ksignal ksig;
  288. sigset_t *oldset = sigmask_to_save();
  289. /*
  290. * Get signal to deliver. When running under ptrace, at this point
  291. * the debugger may change all our registers, including the system
  292. * call information.
  293. */
  294. current_thread_info()->system_call =
  295. test_pt_regs_flag(regs, PIF_SYSCALL) ? regs->int_code : 0;
  296. if (get_signal(&ksig)) {
  297. /* Whee! Actually deliver the signal. */
  298. if (current_thread_info()->system_call) {
  299. regs->int_code = current_thread_info()->system_call;
  300. /* Check for system call restarting. */
  301. switch (regs->gprs[2]) {
  302. case -ERESTART_RESTARTBLOCK:
  303. case -ERESTARTNOHAND:
  304. regs->gprs[2] = -EINTR;
  305. break;
  306. case -ERESTARTSYS:
  307. if (!(ksig.ka.sa.sa_flags & SA_RESTART)) {
  308. regs->gprs[2] = -EINTR;
  309. break;
  310. }
  311. /* fallthrough */
  312. case -ERESTARTNOINTR:
  313. regs->gprs[2] = regs->orig_gpr2;
  314. regs->psw.addr =
  315. __rewind_psw(regs->psw,
  316. regs->int_code >> 16);
  317. break;
  318. }
  319. }
  320. /* No longer in a system call */
  321. clear_pt_regs_flag(regs, PIF_SYSCALL);
  322. if (is_compat_task())
  323. handle_signal32(&ksig, oldset, regs);
  324. else
  325. handle_signal(&ksig, oldset, regs);
  326. return;
  327. }
  328. /* No handlers present - check for system call restart */
  329. clear_pt_regs_flag(regs, PIF_SYSCALL);
  330. if (current_thread_info()->system_call) {
  331. regs->int_code = current_thread_info()->system_call;
  332. switch (regs->gprs[2]) {
  333. case -ERESTART_RESTARTBLOCK:
  334. /* Restart with sys_restart_syscall */
  335. regs->int_code = __NR_restart_syscall;
  336. /* fallthrough */
  337. case -ERESTARTNOHAND:
  338. case -ERESTARTSYS:
  339. case -ERESTARTNOINTR:
  340. /* Restart system call with magic TIF bit. */
  341. regs->gprs[2] = regs->orig_gpr2;
  342. set_pt_regs_flag(regs, PIF_SYSCALL);
  343. if (test_thread_flag(TIF_SINGLE_STEP))
  344. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  345. break;
  346. }
  347. }
  348. /*
  349. * If there's no signal to deliver, we just put the saved sigmask back.
  350. */
  351. restore_saved_sigmask();
  352. }
  353. void do_notify_resume(struct pt_regs *regs)
  354. {
  355. clear_thread_flag(TIF_NOTIFY_RESUME);
  356. tracehook_notify_resume(regs);
  357. }