signal.c 12 KB

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