Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 1999, 2006 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/sched.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/signal.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/wait.h> |
| 19 | #include <linux/ptrace.h> |
| 20 | #include <linux/unistd.h> |
| 21 | #include <linux/stddef.h> |
| 22 | #include <linux/tty.h> |
| 23 | #include <linux/personality.h> |
| 24 | #include <linux/binfmts.h> |
Martin Schwidefsky | 753c4dd | 2008-10-10 21:33:20 +0200 | [diff] [blame] | 25 | #include <linux/tracehook.h> |
Heiko Carstens | 2668945 | 2009-01-14 14:14:36 +0100 | [diff] [blame] | 26 | #include <linux/syscalls.h> |
Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 27 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/ucontext.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/lowcore.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 31 | #include <asm/switch_to.h> |
Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 32 | #include "entry.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 34 | /* |
| 35 | * Layout of an old-style signal-frame: |
| 36 | * ----------------------------------------- |
| 37 | * | save area (_SIGNAL_FRAMESIZE) | |
| 38 | * ----------------------------------------- |
| 39 | * | struct sigcontext | |
| 40 | * | oldmask | |
| 41 | * | _sigregs * | |
| 42 | * ----------------------------------------- |
| 43 | * | _sigregs with | |
| 44 | * | _s390_regs_common | |
| 45 | * | _s390_fp_regs | |
| 46 | * ----------------------------------------- |
| 47 | * | int signo | |
| 48 | * ----------------------------------------- |
| 49 | * | _sigregs_ext with | |
| 50 | * | gprs_high 64 byte (opt) | |
| 51 | * | vxrs_low 128 byte (opt) | |
| 52 | * | vxrs_high 256 byte (opt) | |
| 53 | * | reserved 128 byte (opt) | |
| 54 | * ----------------------------------------- |
| 55 | * | __u16 svc_insn | |
| 56 | * ----------------------------------------- |
| 57 | * The svc_insn entry with the sigreturn system call opcode does not |
| 58 | * have a fixed position and moves if gprs_high or vxrs exist. |
| 59 | * Future extensions will be added to _sigregs_ext. |
| 60 | */ |
| 61 | struct sigframe |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | __u8 callee_used_stack[__SIGNAL_FRAMESIZE]; |
| 64 | struct sigcontext sc; |
| 65 | _sigregs sregs; |
| 66 | int signo; |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 67 | _sigregs_ext sregs_ext; |
| 68 | __u16 svc_insn; /* Offset of svc_insn is NOT fixed! */ |
| 69 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 71 | /* |
| 72 | * Layout of an rt signal-frame: |
| 73 | * ----------------------------------------- |
| 74 | * | save area (_SIGNAL_FRAMESIZE) | |
| 75 | * ----------------------------------------- |
| 76 | * | svc __NR_rt_sigreturn 2 byte | |
| 77 | * ----------------------------------------- |
| 78 | * | struct siginfo | |
| 79 | * ----------------------------------------- |
| 80 | * | struct ucontext_extended with | |
| 81 | * | unsigned long uc_flags | |
| 82 | * | struct ucontext *uc_link | |
| 83 | * | stack_t uc_stack | |
| 84 | * | _sigregs uc_mcontext with | |
| 85 | * | _s390_regs_common | |
| 86 | * | _s390_fp_regs | |
| 87 | * | sigset_t uc_sigmask | |
| 88 | * | _sigregs_ext uc_mcontext_ext | |
| 89 | * | gprs_high 64 byte (opt) | |
| 90 | * | vxrs_low 128 byte (opt) | |
| 91 | * | vxrs_high 256 byte (opt)| |
| 92 | * | reserved 128 byte (opt) | |
| 93 | * ----------------------------------------- |
| 94 | * Future extensions will be added to _sigregs_ext. |
| 95 | */ |
| 96 | struct rt_sigframe |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
| 98 | __u8 callee_used_stack[__SIGNAL_FRAMESIZE]; |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 99 | __u16 svc_insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | struct siginfo info; |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 101 | struct ucontext_extended uc; |
| 102 | }; |
| 103 | |
| 104 | /* Store registers needed to create the signal frame */ |
| 105 | static void store_sigregs(void) |
| 106 | { |
| 107 | save_access_regs(current->thread.acrs); |
| 108 | save_fp_ctl(¤t->thread.fp_regs.fpc); |
| 109 | #ifdef CONFIG_64BIT |
| 110 | if (current->thread.vxrs) { |
| 111 | int i; |
| 112 | |
| 113 | save_vx_regs(current->thread.vxrs); |
| 114 | for (i = 0; i < __NUM_FPRS; i++) |
| 115 | current->thread.fp_regs.fprs[i] = |
| 116 | *(freg_t *)(current->thread.vxrs + i); |
| 117 | } else |
| 118 | #endif |
| 119 | save_fp_regs(current->thread.fp_regs.fprs); |
| 120 | } |
| 121 | |
| 122 | /* Load registers after signal return */ |
| 123 | static void load_sigregs(void) |
| 124 | { |
| 125 | restore_access_regs(current->thread.acrs); |
| 126 | /* restore_fp_ctl is done in restore_sigregs */ |
| 127 | #ifdef CONFIG_64BIT |
| 128 | if (current->thread.vxrs) { |
| 129 | int i; |
| 130 | |
| 131 | for (i = 0; i < __NUM_FPRS; i++) |
| 132 | *(freg_t *)(current->thread.vxrs + i) = |
| 133 | current->thread.fp_regs.fprs[i]; |
| 134 | restore_vx_regs(current->thread.vxrs); |
| 135 | } else |
| 136 | #endif |
| 137 | restore_fp_regs(current->thread.fp_regs.fprs); |
| 138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* Returns non-zero on fault. */ |
| 141 | static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs) |
| 142 | { |
Gerald Schaefer | 6837a8c | 2006-09-20 15:59:39 +0200 | [diff] [blame] | 143 | _sigregs user_sregs; |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* Copy a 'clean' PSW mask to the user to avoid leaking |
| 146 | information about whether PER is currently on. */ |
Martin Schwidefsky | e258d71 | 2013-09-24 09:14:56 +0200 | [diff] [blame] | 147 | user_sregs.regs.psw.mask = PSW_USER_BITS | |
Heiko Carstens | 5ebf250 | 2013-10-16 09:58:01 +0200 | [diff] [blame] | 148 | (regs->psw.mask & (PSW_MASK_USER | PSW_MASK_RI)); |
Martin Schwidefsky | b05e370 | 2006-10-04 20:01:58 +0200 | [diff] [blame] | 149 | user_sregs.regs.psw.addr = regs->psw.addr; |
| 150 | memcpy(&user_sregs.regs.gprs, ®s->gprs, sizeof(sregs->regs.gprs)); |
Gerald Schaefer | 6837a8c | 2006-09-20 15:59:39 +0200 | [diff] [blame] | 151 | memcpy(&user_sregs.regs.acrs, current->thread.acrs, |
Martin Schwidefsky | 4725c86 | 2013-10-15 16:08:34 +0200 | [diff] [blame] | 152 | sizeof(user_sregs.regs.acrs)); |
Gerald Schaefer | 6837a8c | 2006-09-20 15:59:39 +0200 | [diff] [blame] | 153 | memcpy(&user_sregs.fpregs, ¤t->thread.fp_regs, |
Martin Schwidefsky | 4725c86 | 2013-10-15 16:08:34 +0200 | [diff] [blame] | 154 | sizeof(user_sregs.fpregs)); |
Heiko Carstens | f8544ec | 2013-10-14 13:34:28 +0200 | [diff] [blame] | 155 | if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs))) |
| 156 | return -EFAULT; |
| 157 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs) |
| 161 | { |
Gerald Schaefer | 6837a8c | 2006-09-20 15:59:39 +0200 | [diff] [blame] | 162 | _sigregs user_sregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | /* Alwys make any pending restarted system call return -EINTR */ |
| 165 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 166 | |
Martin Schwidefsky | 4725c86 | 2013-10-15 16:08:34 +0200 | [diff] [blame] | 167 | if (__copy_from_user(&user_sregs, sregs, sizeof(user_sregs))) |
Heiko Carstens | f8544ec | 2013-10-14 13:34:28 +0200 | [diff] [blame] | 168 | return -EFAULT; |
Martin Schwidefsky | 4725c86 | 2013-10-15 16:08:34 +0200 | [diff] [blame] | 169 | |
Heiko Carstens | 5ebf250 | 2013-10-16 09:58:01 +0200 | [diff] [blame] | 170 | if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW_MASK_RI)) |
| 171 | return -EINVAL; |
| 172 | |
Martin Schwidefsky | 4725c86 | 2013-10-15 16:08:34 +0200 | [diff] [blame] | 173 | /* Loading the floating-point-control word can fail. Do that first. */ |
| 174 | if (restore_fp_ctl(&user_sregs.fpregs.fpc)) |
| 175 | return -EINVAL; |
| 176 | |
Martin Schwidefsky | e258d71 | 2013-09-24 09:14:56 +0200 | [diff] [blame] | 177 | /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */ |
Hendrik Brueckner | aa7e04b | 2013-11-19 12:26:09 +0100 | [diff] [blame] | 178 | regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) | |
Heiko Carstens | 5ebf250 | 2013-10-16 09:58:01 +0200 | [diff] [blame] | 179 | (user_sregs.regs.psw.mask & (PSW_MASK_USER | PSW_MASK_RI)); |
Martin Schwidefsky | fa968ee | 2012-11-07 10:44:08 +0100 | [diff] [blame] | 180 | /* Check for invalid user address space control. */ |
Martin Schwidefsky | e258d71 | 2013-09-24 09:14:56 +0200 | [diff] [blame] | 181 | if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME) |
| 182 | regs->psw.mask = PSW_ASC_PRIMARY | |
Martin Schwidefsky | fa968ee | 2012-11-07 10:44:08 +0100 | [diff] [blame] | 183 | (regs->psw.mask & ~PSW_MASK_ASC); |
Martin Schwidefsky | d4e81b3 | 2011-10-30 15:16:51 +0100 | [diff] [blame] | 184 | /* Check for invalid amode */ |
| 185 | if (regs->psw.mask & PSW_MASK_EA) |
| 186 | regs->psw.mask |= PSW_MASK_BA; |
| 187 | regs->psw.addr = user_sregs.regs.psw.addr; |
Martin Schwidefsky | b05e370 | 2006-10-04 20:01:58 +0200 | [diff] [blame] | 188 | memcpy(®s->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs)); |
Gerald Schaefer | 6837a8c | 2006-09-20 15:59:39 +0200 | [diff] [blame] | 189 | memcpy(¤t->thread.acrs, &user_sregs.regs.acrs, |
Martin Schwidefsky | 4725c86 | 2013-10-15 16:08:34 +0200 | [diff] [blame] | 190 | sizeof(current->thread.acrs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Gerald Schaefer | 6837a8c | 2006-09-20 15:59:39 +0200 | [diff] [blame] | 192 | memcpy(¤t->thread.fp_regs, &user_sregs.fpregs, |
Martin Schwidefsky | 4725c86 | 2013-10-15 16:08:34 +0200 | [diff] [blame] | 193 | sizeof(current->thread.fp_regs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Martin Schwidefsky | d3a73ac | 2014-04-15 12:55:07 +0200 | [diff] [blame] | 195 | clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 199 | /* Returns non-zero on fault. */ |
| 200 | static int save_sigregs_ext(struct pt_regs *regs, |
| 201 | _sigregs_ext __user *sregs_ext) |
| 202 | { |
| 203 | #ifdef CONFIG_64BIT |
| 204 | __u64 vxrs[__NUM_VXRS_LOW]; |
| 205 | int i; |
| 206 | |
| 207 | /* Save vector registers to signal stack */ |
| 208 | if (current->thread.vxrs) { |
| 209 | for (i = 0; i < __NUM_VXRS_LOW; i++) |
| 210 | vxrs[i] = *((__u64 *)(current->thread.vxrs + i) + 1); |
| 211 | if (__copy_to_user(&sregs_ext->vxrs_low, vxrs, |
| 212 | sizeof(sregs_ext->vxrs_low)) || |
| 213 | __copy_to_user(&sregs_ext->vxrs_high, |
| 214 | current->thread.vxrs + __NUM_VXRS_LOW, |
| 215 | sizeof(sregs_ext->vxrs_high))) |
| 216 | return -EFAULT; |
| 217 | } |
| 218 | #endif |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int restore_sigregs_ext(struct pt_regs *regs, |
| 223 | _sigregs_ext __user *sregs_ext) |
| 224 | { |
| 225 | #ifdef CONFIG_64BIT |
| 226 | __u64 vxrs[__NUM_VXRS_LOW]; |
| 227 | int i; |
| 228 | |
| 229 | /* Restore vector registers from signal stack */ |
| 230 | if (current->thread.vxrs) { |
| 231 | if (__copy_from_user(vxrs, &sregs_ext->vxrs_low, |
| 232 | sizeof(sregs_ext->vxrs_low)) || |
| 233 | __copy_from_user(current->thread.vxrs + __NUM_VXRS_LOW, |
| 234 | &sregs_ext->vxrs_high, |
| 235 | sizeof(sregs_ext->vxrs_high))) |
| 236 | return -EFAULT; |
| 237 | for (i = 0; i < __NUM_VXRS_LOW; i++) |
| 238 | *((__u64 *)(current->thread.vxrs + i) + 1) = vxrs[i]; |
| 239 | } |
| 240 | #endif |
| 241 | return 0; |
| 242 | } |
| 243 | |
Heiko Carstens | 2668945 | 2009-01-14 14:14:36 +0100 | [diff] [blame] | 244 | SYSCALL_DEFINE0(sigreturn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Martin Schwidefsky | 03ff9a2 | 2007-04-27 16:01:40 +0200 | [diff] [blame] | 246 | struct pt_regs *regs = task_pt_regs(current); |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 247 | struct sigframe __user *frame = |
| 248 | (struct sigframe __user *) regs->gprs[15]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | sigset_t set; |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE)) |
| 252 | goto badframe; |
Heiko Carstens | 391c62f | 2011-08-03 16:44:26 +0200 | [diff] [blame] | 253 | set_current_blocked(&set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | if (restore_sigregs(regs, &frame->sregs)) |
| 255 | goto badframe; |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 256 | if (restore_sigregs_ext(regs, &frame->sregs_ext)) |
| 257 | goto badframe; |
| 258 | load_sigregs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return regs->gprs[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | badframe: |
| 261 | force_sig(SIGSEGV, current); |
| 262 | return 0; |
| 263 | } |
| 264 | |
Heiko Carstens | 2668945 | 2009-01-14 14:14:36 +0100 | [diff] [blame] | 265 | SYSCALL_DEFINE0(rt_sigreturn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Martin Schwidefsky | 03ff9a2 | 2007-04-27 16:01:40 +0200 | [diff] [blame] | 267 | struct pt_regs *regs = task_pt_regs(current); |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 268 | struct rt_sigframe __user *frame = |
| 269 | (struct rt_sigframe __user *)regs->gprs[15]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | sigset_t set; |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set))) |
| 273 | goto badframe; |
Heiko Carstens | 391c62f | 2011-08-03 16:44:26 +0200 | [diff] [blame] | 274 | set_current_blocked(&set); |
Al Viro | e214125 | 2012-12-23 03:36:41 -0500 | [diff] [blame] | 275 | if (restore_altstack(&frame->uc.uc_stack)) |
Cedric Le Goater | 4e3df37 | 2006-01-06 00:19:10 -0800 | [diff] [blame] | 276 | goto badframe; |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 277 | if (restore_sigregs(regs, &frame->uc.uc_mcontext)) |
| 278 | goto badframe; |
| 279 | if (restore_sigregs_ext(regs, &frame->uc.uc_mcontext_ext)) |
| 280 | goto badframe; |
| 281 | load_sigregs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return regs->gprs[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | badframe: |
| 284 | force_sig(SIGSEGV, current); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | * Determine which stack to use.. |
| 290 | */ |
| 291 | static inline void __user * |
| 292 | get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) |
| 293 | { |
| 294 | unsigned long sp; |
| 295 | |
| 296 | /* Default to using normal stack */ |
| 297 | sp = regs->gprs[15]; |
| 298 | |
Heiko Carstens | de55343 | 2008-04-17 07:45:57 +0200 | [diff] [blame] | 299 | /* Overflow on alternate signal stack gives SIGSEGV. */ |
| 300 | if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL)) |
| 301 | return (void __user *) -1UL; |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | /* This is the X/Open sanctioned signal stack switching. */ |
| 304 | if (ka->sa.sa_flags & SA_ONSTACK) { |
| 305 | if (! sas_ss_flags(sp)) |
| 306 | sp = current->sas_ss_sp + current->sas_ss_size; |
| 307 | } |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return (void __user *)((sp - frame_size) & -8ul); |
| 310 | } |
| 311 | |
| 312 | static inline int map_signal(int sig) |
| 313 | { |
| 314 | if (current_thread_info()->exec_domain |
| 315 | && current_thread_info()->exec_domain->signal_invmap |
| 316 | && sig < 32) |
| 317 | return current_thread_info()->exec_domain->signal_invmap[sig]; |
| 318 | else |
| 319 | return sig; |
| 320 | } |
| 321 | |
Heiko Carstens | 54dfe5d | 2006-02-01 03:06:38 -0800 | [diff] [blame] | 322 | static int setup_frame(int sig, struct k_sigaction *ka, |
| 323 | sigset_t *set, struct pt_regs * regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 325 | struct sigframe __user *frame; |
| 326 | struct sigcontext sc; |
| 327 | unsigned long restorer; |
| 328 | size_t frame_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 330 | /* |
| 331 | * gprs_high are only present for a 31-bit task running on |
| 332 | * a 64-bit kernel (see compat_signal.c) but the space for |
| 333 | * gprs_high need to be allocated if vector registers are |
| 334 | * included in the signal frame on a 31-bit system. |
| 335 | */ |
| 336 | frame_size = sizeof(*frame) - sizeof(frame->sregs_ext); |
| 337 | if (MACHINE_HAS_VX) |
| 338 | frame_size += sizeof(frame->sregs_ext); |
| 339 | frame = get_sigframe(ka, regs, frame_size); |
Heiko Carstens | de55343 | 2008-04-17 07:45:57 +0200 | [diff] [blame] | 340 | if (frame == (void __user *) -1UL) |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 341 | return -EFAULT; |
Heiko Carstens | de55343 | 2008-04-17 07:45:57 +0200 | [diff] [blame] | 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | /* Set up backchain. */ |
| 344 | if (__put_user(regs->gprs[15], (addr_t __user *) frame)) |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 345 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 347 | /* Create struct sigcontext on the signal stack */ |
| 348 | memcpy(&sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE); |
| 349 | sc.sregs = (_sigregs __user __force *) &frame->sregs; |
| 350 | if (__copy_to_user(&frame->sc, &sc, sizeof(frame->sc))) |
| 351 | return -EFAULT; |
| 352 | |
| 353 | /* Store registers needed to create the signal frame */ |
| 354 | store_sigregs(); |
| 355 | |
| 356 | /* Create _sigregs on the signal stack */ |
| 357 | if (save_sigregs(regs, &frame->sregs)) |
| 358 | return -EFAULT; |
| 359 | |
| 360 | /* Place signal number on stack to allow backtrace from handler. */ |
| 361 | if (__put_user(regs->gprs[2], (int __user *) &frame->signo)) |
| 362 | return -EFAULT; |
| 363 | |
| 364 | /* Create _sigregs_ext on the signal stack */ |
| 365 | if (save_sigregs_ext(regs, &frame->sregs_ext)) |
| 366 | return -EFAULT; |
| 367 | |
| 368 | /* Set up to return from userspace. If provided, use a stub |
| 369 | already in userspace. */ |
| 370 | if (ka->sa.sa_flags & SA_RESTORER) { |
| 371 | restorer = (unsigned long) ka->sa.sa_restorer | PSW_ADDR_AMODE; |
| 372 | } else { |
| 373 | /* Signal frame without vector registers are short ! */ |
| 374 | __u16 __user *svc = (void *) frame + frame_size - 2; |
| 375 | if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc)) |
| 376 | return -EFAULT; |
| 377 | restorer = (unsigned long) svc | PSW_ADDR_AMODE; |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* Set up registers for signal handler */ |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 381 | regs->gprs[14] = restorer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | regs->gprs[15] = (unsigned long) frame; |
Martin Schwidefsky | fa968ee | 2012-11-07 10:44:08 +0100 | [diff] [blame] | 383 | /* Force default amode and default user address space control. */ |
| 384 | regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA | |
Martin Schwidefsky | e258d71 | 2013-09-24 09:14:56 +0200 | [diff] [blame] | 385 | (PSW_USER_BITS & PSW_MASK_ASC) | |
Martin Schwidefsky | fa968ee | 2012-11-07 10:44:08 +0100 | [diff] [blame] | 386 | (regs->psw.mask & ~PSW_MASK_ASC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE; |
| 388 | |
| 389 | regs->gprs[2] = map_signal(sig); |
| 390 | regs->gprs[3] = (unsigned long) &frame->sc; |
| 391 | |
| 392 | /* We forgot to include these in the sigcontext. |
| 393 | To avoid breaking binary compatibility, they are passed as args. */ |
Martin Schwidefsky | aa33c8c | 2011-12-27 11:27:18 +0100 | [diff] [blame] | 394 | if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || |
| 395 | sig == SIGTRAP || sig == SIGFPE) { |
| 396 | /* set extra registers only for synchronous signals */ |
| 397 | regs->gprs[4] = regs->int_code & 127; |
| 398 | regs->gprs[5] = regs->int_parm_long; |
| 399 | regs->gprs[6] = task_thread_info(current)->last_break; |
| 400 | } |
Heiko Carstens | 54dfe5d | 2006-02-01 03:06:38 -0800 | [diff] [blame] | 401 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 404 | static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, |
| 405 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 407 | struct rt_sigframe __user *frame; |
| 408 | unsigned long uc_flags, restorer; |
| 409 | size_t frame_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 411 | frame_size = sizeof(struct rt_sigframe) - sizeof(_sigregs_ext); |
| 412 | /* |
| 413 | * gprs_high are only present for a 31-bit task running on |
| 414 | * a 64-bit kernel (see compat_signal.c) but the space for |
| 415 | * gprs_high need to be allocated if vector registers are |
| 416 | * included in the signal frame on a 31-bit system. |
| 417 | */ |
| 418 | uc_flags = 0; |
| 419 | #ifdef CONFIG_64BIT |
| 420 | if (MACHINE_HAS_VX) { |
| 421 | frame_size += sizeof(_sigregs_ext); |
| 422 | if (current->thread.vxrs) |
| 423 | uc_flags |= UC_VXRS; |
| 424 | } |
| 425 | #endif |
| 426 | frame = get_sigframe(&ksig->ka, regs, frame_size); |
Heiko Carstens | de55343 | 2008-04-17 07:45:57 +0200 | [diff] [blame] | 427 | if (frame == (void __user *) -1UL) |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 428 | return -EFAULT; |
Heiko Carstens | de55343 | 2008-04-17 07:45:57 +0200 | [diff] [blame] | 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* Set up backchain. */ |
| 431 | if (__put_user(regs->gprs[15], (addr_t __user *) frame)) |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 432 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 434 | /* Set up to return from userspace. If provided, use a stub |
| 435 | already in userspace. */ |
| 436 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
| 437 | restorer = (unsigned long) |
| 438 | ksig->ka.sa.sa_restorer | PSW_ADDR_AMODE; |
| 439 | } else { |
| 440 | __u16 __user *svc = &frame->svc_insn; |
| 441 | if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc)) |
| 442 | return -EFAULT; |
| 443 | restorer = (unsigned long) svc | PSW_ADDR_AMODE; |
| 444 | } |
| 445 | |
| 446 | /* Create siginfo on the signal stack */ |
| 447 | if (copy_siginfo_to_user(&frame->info, &ksig->info)) |
| 448 | return -EFAULT; |
| 449 | |
| 450 | /* Store registers needed to create the signal frame */ |
| 451 | store_sigregs(); |
| 452 | |
| 453 | /* Create ucontext on the signal stack. */ |
| 454 | if (__put_user(uc_flags, &frame->uc.uc_flags) || |
| 455 | __put_user(NULL, &frame->uc.uc_link) || |
| 456 | __save_altstack(&frame->uc.uc_stack, regs->gprs[15]) || |
| 457 | save_sigregs(regs, &frame->uc.uc_mcontext) || |
| 458 | __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)) || |
| 459 | save_sigregs_ext(regs, &frame->uc.uc_mcontext_ext)) |
| 460 | return -EFAULT; |
| 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | /* Set up registers for signal handler */ |
Martin Schwidefsky | 8070361 | 2014-10-06 17:53:53 +0200 | [diff] [blame] | 463 | regs->gprs[14] = restorer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | regs->gprs[15] = (unsigned long) frame; |
Martin Schwidefsky | fa968ee | 2012-11-07 10:44:08 +0100 | [diff] [blame] | 465 | /* Force default amode and default user address space control. */ |
| 466 | regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA | |
Martin Schwidefsky | e258d71 | 2013-09-24 09:14:56 +0200 | [diff] [blame] | 467 | (PSW_USER_BITS & PSW_MASK_ASC) | |
Martin Schwidefsky | fa968ee | 2012-11-07 10:44:08 +0100 | [diff] [blame] | 468 | (regs->psw.mask & ~PSW_MASK_ASC); |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 469 | regs->psw.addr = (unsigned long) ksig->ka.sa.sa_handler | PSW_ADDR_AMODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 471 | regs->gprs[2] = map_signal(ksig->sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | regs->gprs[3] = (unsigned long) &frame->info; |
| 473 | regs->gprs[4] = (unsigned long) &frame->uc; |
Martin Schwidefsky | 86f2552 | 2010-05-17 10:00:05 +0200 | [diff] [blame] | 474 | regs->gprs[5] = task_thread_info(current)->last_break; |
Heiko Carstens | 54dfe5d | 2006-02-01 03:06:38 -0800 | [diff] [blame] | 475 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 478 | static void handle_signal(struct ksignal *ksig, sigset_t *oldset, |
| 479 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
Heiko Carstens | 54dfe5d | 2006-02-01 03:06:38 -0800 | [diff] [blame] | 481 | int ret; |
| 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | /* Set up the stack frame */ |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 484 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) |
| 485 | ret = setup_rt_frame(ksig, oldset, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | else |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 487 | ret = setup_frame(ksig->sig, &ksig->ka, oldset, regs); |
| 488 | |
| 489 | signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 494 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 495 | * mistake. |
| 496 | * |
| 497 | * Note that we go through the signals twice: once to check the signals that |
| 498 | * the kernel can handle, and then we build all the user-level signal handling |
| 499 | * stack-frames in one go after that. |
| 500 | */ |
Heiko Carstens | 54dfe5d | 2006-02-01 03:06:38 -0800 | [diff] [blame] | 501 | void do_signal(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 503 | struct ksignal ksig; |
Al Viro | b7f9a11 | 2012-05-02 09:59:21 -0400 | [diff] [blame] | 504 | sigset_t *oldset = sigmask_to_save(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 506 | /* |
| 507 | * Get signal to deliver. When running under ptrace, at this point |
| 508 | * the debugger may change all our registers, including the system |
| 509 | * call information. |
| 510 | */ |
Martin Schwidefsky | b6ef5bb | 2011-10-30 15:16:49 +0100 | [diff] [blame] | 511 | current_thread_info()->system_call = |
Martin Schwidefsky | d3a73ac | 2014-04-15 12:55:07 +0200 | [diff] [blame] | 512 | test_pt_regs_flag(regs, PIF_SYSCALL) ? regs->int_code : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 514 | if (get_signal(&ksig)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | /* Whee! Actually deliver the signal. */ |
Martin Schwidefsky | b6ef5bb | 2011-10-30 15:16:49 +0100 | [diff] [blame] | 516 | if (current_thread_info()->system_call) { |
Martin Schwidefsky | aa33c8c | 2011-12-27 11:27:18 +0100 | [diff] [blame] | 517 | regs->int_code = current_thread_info()->system_call; |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 518 | /* Check for system call restarting. */ |
| 519 | switch (regs->gprs[2]) { |
| 520 | case -ERESTART_RESTARTBLOCK: |
| 521 | case -ERESTARTNOHAND: |
| 522 | regs->gprs[2] = -EINTR; |
| 523 | break; |
| 524 | case -ERESTARTSYS: |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 525 | if (!(ksig.ka.sa.sa_flags & SA_RESTART)) { |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 526 | regs->gprs[2] = -EINTR; |
| 527 | break; |
| 528 | } |
| 529 | /* fallthrough */ |
| 530 | case -ERESTARTNOINTR: |
| 531 | regs->gprs[2] = regs->orig_gpr2; |
Martin Schwidefsky | ccf45ca | 2011-10-30 15:16:48 +0100 | [diff] [blame] | 532 | regs->psw.addr = |
| 533 | __rewind_psw(regs->psw, |
Martin Schwidefsky | aa33c8c | 2011-12-27 11:27:18 +0100 | [diff] [blame] | 534 | regs->int_code >> 16); |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 535 | break; |
| 536 | } |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 537 | } |
Martin Schwidefsky | d9ae677 | 2011-12-01 13:32:15 +0100 | [diff] [blame] | 538 | /* No longer in a system call */ |
Martin Schwidefsky | d3a73ac | 2014-04-15 12:55:07 +0200 | [diff] [blame] | 539 | clear_pt_regs_flag(regs, PIF_SYSCALL); |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 540 | |
Al Viro | a610d6e | 2012-05-21 23:42:15 -0400 | [diff] [blame] | 541 | if (is_compat_task()) |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 542 | handle_signal32(&ksig, oldset, regs); |
Al Viro | a610d6e | 2012-05-21 23:42:15 -0400 | [diff] [blame] | 543 | else |
Richard Weinberger | 067bf2d | 2014-07-13 22:21:03 +0200 | [diff] [blame] | 544 | handle_signal(&ksig, oldset, regs); |
Heiko Carstens | 54dfe5d | 2006-02-01 03:06:38 -0800 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 548 | /* No handlers present - check for system call restart */ |
Martin Schwidefsky | d3a73ac | 2014-04-15 12:55:07 +0200 | [diff] [blame] | 549 | clear_pt_regs_flag(regs, PIF_SYSCALL); |
Martin Schwidefsky | b6ef5bb | 2011-10-30 15:16:49 +0100 | [diff] [blame] | 550 | if (current_thread_info()->system_call) { |
Martin Schwidefsky | aa33c8c | 2011-12-27 11:27:18 +0100 | [diff] [blame] | 551 | regs->int_code = current_thread_info()->system_call; |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 552 | switch (regs->gprs[2]) { |
| 553 | case -ERESTART_RESTARTBLOCK: |
| 554 | /* Restart with sys_restart_syscall */ |
Martin Schwidefsky | aa33c8c | 2011-12-27 11:27:18 +0100 | [diff] [blame] | 555 | regs->int_code = __NR_restart_syscall; |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 556 | /* fallthrough */ |
| 557 | case -ERESTARTNOHAND: |
| 558 | case -ERESTARTSYS: |
| 559 | case -ERESTARTNOINTR: |
| 560 | /* Restart system call with magic TIF bit. */ |
| 561 | regs->gprs[2] = regs->orig_gpr2; |
Martin Schwidefsky | d3a73ac | 2014-04-15 12:55:07 +0200 | [diff] [blame] | 562 | set_pt_regs_flag(regs, PIF_SYSCALL); |
Martin Schwidefsky | 39efd4e | 2012-11-21 16:36:27 +0100 | [diff] [blame] | 563 | if (test_thread_flag(TIF_SINGLE_STEP)) |
Martin Schwidefsky | d3a73ac | 2014-04-15 12:55:07 +0200 | [diff] [blame] | 564 | clear_pt_regs_flag(regs, PIF_PER_TRAP); |
Martin Schwidefsky | b6ef5bb | 2011-10-30 15:16:49 +0100 | [diff] [blame] | 565 | break; |
Martin Schwidefsky | 20b40a7 | 2011-10-30 15:16:47 +0100 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Heiko Carstens | 54dfe5d | 2006-02-01 03:06:38 -0800 | [diff] [blame] | 569 | /* |
| 570 | * If there's no signal to deliver, we just put the saved sigmask back. |
| 571 | */ |
Al Viro | 51a7b44 | 2012-05-21 23:33:55 -0400 | [diff] [blame] | 572 | restore_saved_sigmask(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | } |
Martin Schwidefsky | 753c4dd | 2008-10-10 21:33:20 +0200 | [diff] [blame] | 574 | |
| 575 | void do_notify_resume(struct pt_regs *regs) |
| 576 | { |
| 577 | clear_thread_flag(TIF_NOTIFY_RESUME); |
| 578 | tracehook_notify_resume(regs); |
| 579 | } |