signal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. /*
  33. * Layout of an old-style signal-frame:
  34. * -----------------------------------------
  35. * | save area (_SIGNAL_FRAMESIZE) |
  36. * -----------------------------------------
  37. * | struct sigcontext |
  38. * | oldmask |
  39. * | _sigregs * |
  40. * -----------------------------------------
  41. * | _sigregs with |
  42. * | _s390_regs_common |
  43. * | _s390_fp_regs |
  44. * -----------------------------------------
  45. * | int signo |
  46. * -----------------------------------------
  47. * | _sigregs_ext with |
  48. * | gprs_high 64 byte (opt) |
  49. * | vxrs_low 128 byte (opt) |
  50. * | vxrs_high 256 byte (opt) |
  51. * | reserved 128 byte (opt) |
  52. * -----------------------------------------
  53. * | __u16 svc_insn |
  54. * -----------------------------------------
  55. * The svc_insn entry with the sigreturn system call opcode does not
  56. * have a fixed position and moves if gprs_high or vxrs exist.
  57. * Future extensions will be added to _sigregs_ext.
  58. */
  59. struct sigframe
  60. {
  61. __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
  62. struct sigcontext sc;
  63. _sigregs sregs;
  64. int signo;
  65. _sigregs_ext sregs_ext;
  66. __u16 svc_insn; /* Offset of svc_insn is NOT fixed! */
  67. };
  68. /*
  69. * Layout of an rt signal-frame:
  70. * -----------------------------------------
  71. * | save area (_SIGNAL_FRAMESIZE) |
  72. * -----------------------------------------
  73. * | svc __NR_rt_sigreturn 2 byte |
  74. * -----------------------------------------
  75. * | struct siginfo |
  76. * -----------------------------------------
  77. * | struct ucontext_extended with |
  78. * | unsigned long uc_flags |
  79. * | struct ucontext *uc_link |
  80. * | stack_t uc_stack |
  81. * | _sigregs uc_mcontext with |
  82. * | _s390_regs_common |
  83. * | _s390_fp_regs |
  84. * | sigset_t uc_sigmask |
  85. * | _sigregs_ext uc_mcontext_ext |
  86. * | gprs_high 64 byte (opt) |
  87. * | vxrs_low 128 byte (opt) |
  88. * | vxrs_high 256 byte (opt)|
  89. * | reserved 128 byte (opt) |
  90. * -----------------------------------------
  91. * Future extensions will be added to _sigregs_ext.
  92. */
  93. struct rt_sigframe
  94. {
  95. __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
  96. __u16 svc_insn;
  97. struct siginfo info;
  98. struct ucontext_extended uc;
  99. };
  100. /* Store registers needed to create the signal frame */
  101. static void store_sigregs(void)
  102. {
  103. save_access_regs(current->thread.acrs);
  104. save_fpu_regs();
  105. }
  106. /* Load registers after signal return */
  107. static void load_sigregs(void)
  108. {
  109. restore_access_regs(current->thread.acrs);
  110. }
  111. /* Returns non-zero on fault. */
  112. static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  113. {
  114. _sigregs user_sregs;
  115. /* Copy a 'clean' PSW mask to the user to avoid leaking
  116. information about whether PER is currently on. */
  117. user_sregs.regs.psw.mask = PSW_USER_BITS |
  118. (regs->psw.mask & (PSW_MASK_USER | PSW_MASK_RI));
  119. user_sregs.regs.psw.addr = regs->psw.addr;
  120. memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
  121. memcpy(&user_sregs.regs.acrs, current->thread.acrs,
  122. sizeof(user_sregs.regs.acrs));
  123. fpregs_store(&user_sregs.fpregs, &current->thread.fpu);
  124. if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs)))
  125. return -EFAULT;
  126. return 0;
  127. }
  128. static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
  129. {
  130. _sigregs user_sregs;
  131. /* Alwys make any pending restarted system call return -EINTR */
  132. current->restart_block.fn = do_no_restart_syscall;
  133. if (__copy_from_user(&user_sregs, sregs, sizeof(user_sregs)))
  134. return -EFAULT;
  135. if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW_MASK_RI))
  136. return -EINVAL;
  137. /* Test the floating-point-control word. */
  138. if (test_fp_ctl(user_sregs.fpregs.fpc))
  139. return -EINVAL;
  140. /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
  141. regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
  142. (user_sregs.regs.psw.mask & (PSW_MASK_USER | PSW_MASK_RI));
  143. /* Check for invalid user address space control. */
  144. if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
  145. regs->psw.mask = PSW_ASC_PRIMARY |
  146. (regs->psw.mask & ~PSW_MASK_ASC);
  147. /* Check for invalid amode */
  148. if (regs->psw.mask & PSW_MASK_EA)
  149. regs->psw.mask |= PSW_MASK_BA;
  150. regs->psw.addr = user_sregs.regs.psw.addr;
  151. memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
  152. memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
  153. sizeof(current->thread.acrs));
  154. fpregs_load(&user_sregs.fpregs, &current->thread.fpu);
  155. clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
  156. return 0;
  157. }
  158. /* Returns non-zero on fault. */
  159. static int save_sigregs_ext(struct pt_regs *regs,
  160. _sigregs_ext __user *sregs_ext)
  161. {
  162. __u64 vxrs[__NUM_VXRS_LOW];
  163. int i;
  164. /* Save vector registers to signal stack */
  165. if (MACHINE_HAS_VX) {
  166. for (i = 0; i < __NUM_VXRS_LOW; i++)
  167. vxrs[i] = *((__u64 *)(current->thread.fpu.vxrs + i) + 1);
  168. if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
  169. sizeof(sregs_ext->vxrs_low)) ||
  170. __copy_to_user(&sregs_ext->vxrs_high,
  171. current->thread.fpu.vxrs + __NUM_VXRS_LOW,
  172. sizeof(sregs_ext->vxrs_high)))
  173. return -EFAULT;
  174. }
  175. return 0;
  176. }
  177. static int restore_sigregs_ext(struct pt_regs *regs,
  178. _sigregs_ext __user *sregs_ext)
  179. {
  180. __u64 vxrs[__NUM_VXRS_LOW];
  181. int i;
  182. /* Restore vector registers from signal stack */
  183. if (MACHINE_HAS_VX) {
  184. if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
  185. sizeof(sregs_ext->vxrs_low)) ||
  186. __copy_from_user(current->thread.fpu.vxrs + __NUM_VXRS_LOW,
  187. &sregs_ext->vxrs_high,
  188. sizeof(sregs_ext->vxrs_high)))
  189. return -EFAULT;
  190. for (i = 0; i < __NUM_VXRS_LOW; i++)
  191. *((__u64 *)(current->thread.fpu.vxrs + i) + 1) = vxrs[i];
  192. }
  193. return 0;
  194. }
  195. SYSCALL_DEFINE0(sigreturn)
  196. {
  197. struct pt_regs *regs = task_pt_regs(current);
  198. struct sigframe __user *frame =
  199. (struct sigframe __user *) regs->gprs[15];
  200. sigset_t set;
  201. if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
  202. goto badframe;
  203. set_current_blocked(&set);
  204. save_fpu_regs();
  205. if (restore_sigregs(regs, &frame->sregs))
  206. goto badframe;
  207. if (restore_sigregs_ext(regs, &frame->sregs_ext))
  208. goto badframe;
  209. load_sigregs();
  210. return regs->gprs[2];
  211. badframe:
  212. force_sig(SIGSEGV, current);
  213. return 0;
  214. }
  215. SYSCALL_DEFINE0(rt_sigreturn)
  216. {
  217. struct pt_regs *regs = task_pt_regs(current);
  218. struct rt_sigframe __user *frame =
  219. (struct rt_sigframe __user *)regs->gprs[15];
  220. sigset_t set;
  221. if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
  222. goto badframe;
  223. set_current_blocked(&set);
  224. if (restore_altstack(&frame->uc.uc_stack))
  225. goto badframe;
  226. save_fpu_regs();
  227. if (restore_sigregs(regs, &frame->uc.uc_mcontext))
  228. goto badframe;
  229. if (restore_sigregs_ext(regs, &frame->uc.uc_mcontext_ext))
  230. goto badframe;
  231. load_sigregs();
  232. return regs->gprs[2];
  233. badframe:
  234. force_sig(SIGSEGV, current);
  235. return 0;
  236. }
  237. /*
  238. * Determine which stack to use..
  239. */
  240. static inline void __user *
  241. get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
  242. {
  243. unsigned long sp;
  244. /* Default to using normal stack */
  245. sp = regs->gprs[15];
  246. /* Overflow on alternate signal stack gives SIGSEGV. */
  247. if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
  248. return (void __user *) -1UL;
  249. /* This is the X/Open sanctioned signal stack switching. */
  250. if (ka->sa.sa_flags & SA_ONSTACK) {
  251. if (! sas_ss_flags(sp))
  252. sp = current->sas_ss_sp + current->sas_ss_size;
  253. }
  254. return (void __user *)((sp - frame_size) & -8ul);
  255. }
  256. static int setup_frame(int sig, struct k_sigaction *ka,
  257. sigset_t *set, struct pt_regs * regs)
  258. {
  259. struct sigframe __user *frame;
  260. struct sigcontext sc;
  261. unsigned long restorer;
  262. size_t frame_size;
  263. /*
  264. * gprs_high are only present for a 31-bit task running on
  265. * a 64-bit kernel (see compat_signal.c) but the space for
  266. * gprs_high need to be allocated if vector registers are
  267. * included in the signal frame on a 31-bit system.
  268. */
  269. frame_size = sizeof(*frame) - sizeof(frame->sregs_ext);
  270. if (MACHINE_HAS_VX)
  271. frame_size += sizeof(frame->sregs_ext);
  272. frame = get_sigframe(ka, regs, frame_size);
  273. if (frame == (void __user *) -1UL)
  274. return -EFAULT;
  275. /* Set up backchain. */
  276. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  277. return -EFAULT;
  278. /* Create struct sigcontext on the signal stack */
  279. memcpy(&sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE);
  280. sc.sregs = (_sigregs __user __force *) &frame->sregs;
  281. if (__copy_to_user(&frame->sc, &sc, sizeof(frame->sc)))
  282. return -EFAULT;
  283. /* Store registers needed to create the signal frame */
  284. store_sigregs();
  285. /* Create _sigregs on the signal stack */
  286. if (save_sigregs(regs, &frame->sregs))
  287. return -EFAULT;
  288. /* Place signal number on stack to allow backtrace from handler. */
  289. if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
  290. return -EFAULT;
  291. /* Create _sigregs_ext on the signal stack */
  292. if (save_sigregs_ext(regs, &frame->sregs_ext))
  293. return -EFAULT;
  294. /* Set up to return from userspace. If provided, use a stub
  295. already in userspace. */
  296. if (ka->sa.sa_flags & SA_RESTORER) {
  297. restorer = (unsigned long) ka->sa.sa_restorer;
  298. } else {
  299. /* Signal frame without vector registers are short ! */
  300. __u16 __user *svc = (void __user *) frame + frame_size - 2;
  301. if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc))
  302. return -EFAULT;
  303. restorer = (unsigned long) svc;
  304. }
  305. /* Set up registers for signal handler */
  306. regs->gprs[14] = restorer;
  307. regs->gprs[15] = (unsigned long) frame;
  308. /* Force default amode and default user address space control. */
  309. regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
  310. (PSW_USER_BITS & PSW_MASK_ASC) |
  311. (regs->psw.mask & ~PSW_MASK_ASC);
  312. regs->psw.addr = (unsigned long) ka->sa.sa_handler;
  313. regs->gprs[2] = sig;
  314. regs->gprs[3] = (unsigned long) &frame->sc;
  315. /* We forgot to include these in the sigcontext.
  316. To avoid breaking binary compatibility, they are passed as args. */
  317. if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
  318. sig == SIGTRAP || sig == SIGFPE) {
  319. /* set extra registers only for synchronous signals */
  320. regs->gprs[4] = regs->int_code & 127;
  321. regs->gprs[5] = regs->int_parm_long;
  322. regs->gprs[6] = task_thread_info(current)->last_break;
  323. }
  324. return 0;
  325. }
  326. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  327. struct pt_regs *regs)
  328. {
  329. struct rt_sigframe __user *frame;
  330. unsigned long uc_flags, restorer;
  331. size_t frame_size;
  332. frame_size = sizeof(struct rt_sigframe) - sizeof(_sigregs_ext);
  333. /*
  334. * gprs_high are only present for a 31-bit task running on
  335. * a 64-bit kernel (see compat_signal.c) but the space for
  336. * gprs_high need to be allocated if vector registers are
  337. * included in the signal frame on a 31-bit system.
  338. */
  339. uc_flags = 0;
  340. if (MACHINE_HAS_VX) {
  341. frame_size += sizeof(_sigregs_ext);
  342. uc_flags |= UC_VXRS;
  343. }
  344. frame = get_sigframe(&ksig->ka, regs, frame_size);
  345. if (frame == (void __user *) -1UL)
  346. return -EFAULT;
  347. /* Set up backchain. */
  348. if (__put_user(regs->gprs[15], (addr_t __user *) frame))
  349. return -EFAULT;
  350. /* Set up to return from userspace. If provided, use a stub
  351. already in userspace. */
  352. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  353. restorer = (unsigned long) ksig->ka.sa.sa_restorer;
  354. } else {
  355. __u16 __user *svc = &frame->svc_insn;
  356. if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc))
  357. return -EFAULT;
  358. restorer = (unsigned long) svc;
  359. }
  360. /* Create siginfo on the signal stack */
  361. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  362. return -EFAULT;
  363. /* Store registers needed to create the signal frame */
  364. store_sigregs();
  365. /* Create ucontext on the signal stack. */
  366. if (__put_user(uc_flags, &frame->uc.uc_flags) ||
  367. __put_user(NULL, &frame->uc.uc_link) ||
  368. __save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
  369. save_sigregs(regs, &frame->uc.uc_mcontext) ||
  370. __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)) ||
  371. save_sigregs_ext(regs, &frame->uc.uc_mcontext_ext))
  372. return -EFAULT;
  373. /* Set up registers for signal handler */
  374. regs->gprs[14] = restorer;
  375. regs->gprs[15] = (unsigned long) frame;
  376. /* Force default amode and default user address space control. */
  377. regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
  378. (PSW_USER_BITS & PSW_MASK_ASC) |
  379. (regs->psw.mask & ~PSW_MASK_ASC);
  380. regs->psw.addr = (unsigned long) ksig->ka.sa.sa_handler;
  381. regs->gprs[2] = ksig->sig;
  382. regs->gprs[3] = (unsigned long) &frame->info;
  383. regs->gprs[4] = (unsigned long) &frame->uc;
  384. regs->gprs[5] = task_thread_info(current)->last_break;
  385. return 0;
  386. }
  387. static void handle_signal(struct ksignal *ksig, sigset_t *oldset,
  388. struct pt_regs *regs)
  389. {
  390. int ret;
  391. /* Set up the stack frame */
  392. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  393. ret = setup_rt_frame(ksig, oldset, regs);
  394. else
  395. ret = setup_frame(ksig->sig, &ksig->ka, oldset, regs);
  396. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));
  397. }
  398. /*
  399. * Note that 'init' is a special process: it doesn't get signals it doesn't
  400. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  401. * mistake.
  402. *
  403. * Note that we go through the signals twice: once to check the signals that
  404. * the kernel can handle, and then we build all the user-level signal handling
  405. * stack-frames in one go after that.
  406. */
  407. void do_signal(struct pt_regs *regs)
  408. {
  409. struct ksignal ksig;
  410. sigset_t *oldset = sigmask_to_save();
  411. /*
  412. * Get signal to deliver. When running under ptrace, at this point
  413. * the debugger may change all our registers, including the system
  414. * call information.
  415. */
  416. current_thread_info()->system_call =
  417. test_pt_regs_flag(regs, PIF_SYSCALL) ? regs->int_code : 0;
  418. if (get_signal(&ksig)) {
  419. /* Whee! Actually deliver the signal. */
  420. if (current_thread_info()->system_call) {
  421. regs->int_code = current_thread_info()->system_call;
  422. /* Check for system call restarting. */
  423. switch (regs->gprs[2]) {
  424. case -ERESTART_RESTARTBLOCK:
  425. case -ERESTARTNOHAND:
  426. regs->gprs[2] = -EINTR;
  427. break;
  428. case -ERESTARTSYS:
  429. if (!(ksig.ka.sa.sa_flags & SA_RESTART)) {
  430. regs->gprs[2] = -EINTR;
  431. break;
  432. }
  433. /* fallthrough */
  434. case -ERESTARTNOINTR:
  435. regs->gprs[2] = regs->orig_gpr2;
  436. regs->psw.addr =
  437. __rewind_psw(regs->psw,
  438. regs->int_code >> 16);
  439. break;
  440. }
  441. }
  442. /* No longer in a system call */
  443. clear_pt_regs_flag(regs, PIF_SYSCALL);
  444. if (is_compat_task())
  445. handle_signal32(&ksig, oldset, regs);
  446. else
  447. handle_signal(&ksig, oldset, regs);
  448. return;
  449. }
  450. /* No handlers present - check for system call restart */
  451. clear_pt_regs_flag(regs, PIF_SYSCALL);
  452. if (current_thread_info()->system_call) {
  453. regs->int_code = current_thread_info()->system_call;
  454. switch (regs->gprs[2]) {
  455. case -ERESTART_RESTARTBLOCK:
  456. /* Restart with sys_restart_syscall */
  457. regs->int_code = __NR_restart_syscall;
  458. /* fallthrough */
  459. case -ERESTARTNOHAND:
  460. case -ERESTARTSYS:
  461. case -ERESTARTNOINTR:
  462. /* Restart system call with magic TIF bit. */
  463. regs->gprs[2] = regs->orig_gpr2;
  464. set_pt_regs_flag(regs, PIF_SYSCALL);
  465. if (test_thread_flag(TIF_SINGLE_STEP))
  466. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  467. break;
  468. }
  469. }
  470. /*
  471. * If there's no signal to deliver, we just put the saved sigmask back.
  472. */
  473. restore_saved_sigmask();
  474. }
  475. void do_notify_resume(struct pt_regs *regs)
  476. {
  477. clear_thread_flag(TIF_NOTIFY_RESUME);
  478. tracehook_notify_resume(regs);
  479. }