1 /* 2 * Copyright IBM Corp. 2000, 2006 3 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com) 4 * Gerhard Tonn (ton@de.ibm.com) 5 * 6 * Copyright (C) 1991, 1992 Linus Torvalds 7 * 8 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 9 */ 10 11 #include <linux/compat.h> 12 #include <linux/sched.h> 13 #include <linux/mm.h> 14 #include <linux/smp.h> 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> 25 #include <asm/ucontext.h> 26 #include <asm/uaccess.h> 27 #include <asm/lowcore.h> 28 #include <asm/switch_to.h> 29 #include "compat_linux.h" 30 #include "compat_ptrace.h" 31 #include "entry.h" 32 33 typedef struct 34 { 35 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32]; 36 struct sigcontext32 sc; 37 _sigregs32 sregs; 38 int signo; 39 __u32 gprs_high[NUM_GPRS]; 40 __u8 retcode[S390_SYSCALL_SIZE]; 41 } sigframe32; 42 43 typedef struct 44 { 45 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32]; 46 __u8 retcode[S390_SYSCALL_SIZE]; 47 compat_siginfo_t info; 48 struct ucontext32 uc; 49 __u32 gprs_high[NUM_GPRS]; 50 } rt_sigframe32; 51 52 int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from) 53 { 54 int err; 55 56 /* If you change siginfo_t structure, please be sure 57 this code is fixed accordingly. 58 It should never copy any pad contained in the structure 59 to avoid security leaks, but must copy the generic 60 3 ints plus the relevant union member. 61 This routine must convert siginfo from 64bit to 32bit as well 62 at the same time. */ 63 err = __put_user(from->si_signo, &to->si_signo); 64 err |= __put_user(from->si_errno, &to->si_errno); 65 err |= __put_user((short)from->si_code, &to->si_code); 66 if (from->si_code < 0) 67 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE); 68 else { 69 switch (from->si_code >> 16) { 70 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */ 71 case __SI_MESGQ >> 16: 72 err |= __put_user(from->si_int, &to->si_int); 73 /* fallthrough */ 74 case __SI_KILL >> 16: 75 err |= __put_user(from->si_pid, &to->si_pid); 76 err |= __put_user(from->si_uid, &to->si_uid); 77 break; 78 case __SI_CHLD >> 16: 79 err |= __put_user(from->si_pid, &to->si_pid); 80 err |= __put_user(from->si_uid, &to->si_uid); 81 err |= __put_user(from->si_utime, &to->si_utime); 82 err |= __put_user(from->si_stime, &to->si_stime); 83 err |= __put_user(from->si_status, &to->si_status); 84 break; 85 case __SI_FAULT >> 16: 86 err |= __put_user((unsigned long) from->si_addr, 87 &to->si_addr); 88 break; 89 case __SI_POLL >> 16: 90 err |= __put_user(from->si_band, &to->si_band); 91 err |= __put_user(from->si_fd, &to->si_fd); 92 break; 93 case __SI_TIMER >> 16: 94 err |= __put_user(from->si_tid, &to->si_tid); 95 err |= __put_user(from->si_overrun, &to->si_overrun); 96 err |= __put_user(from->si_int, &to->si_int); 97 break; 98 default: 99 break; 100 } 101 } 102 return err ? -EFAULT : 0; 103 } 104 105 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from) 106 { 107 int err; 108 u32 tmp; 109 110 err = __get_user(to->si_signo, &from->si_signo); 111 err |= __get_user(to->si_errno, &from->si_errno); 112 err |= __get_user(to->si_code, &from->si_code); 113 114 if (to->si_code < 0) 115 err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE); 116 else { 117 switch (to->si_code >> 16) { 118 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */ 119 case __SI_MESGQ >> 16: 120 err |= __get_user(to->si_int, &from->si_int); 121 /* fallthrough */ 122 case __SI_KILL >> 16: 123 err |= __get_user(to->si_pid, &from->si_pid); 124 err |= __get_user(to->si_uid, &from->si_uid); 125 break; 126 case __SI_CHLD >> 16: 127 err |= __get_user(to->si_pid, &from->si_pid); 128 err |= __get_user(to->si_uid, &from->si_uid); 129 err |= __get_user(to->si_utime, &from->si_utime); 130 err |= __get_user(to->si_stime, &from->si_stime); 131 err |= __get_user(to->si_status, &from->si_status); 132 break; 133 case __SI_FAULT >> 16: 134 err |= __get_user(tmp, &from->si_addr); 135 to->si_addr = (void __force __user *) 136 (u64) (tmp & PSW32_ADDR_INSN); 137 break; 138 case __SI_POLL >> 16: 139 err |= __get_user(to->si_band, &from->si_band); 140 err |= __get_user(to->si_fd, &from->si_fd); 141 break; 142 case __SI_TIMER >> 16: 143 err |= __get_user(to->si_tid, &from->si_tid); 144 err |= __get_user(to->si_overrun, &from->si_overrun); 145 err |= __get_user(to->si_int, &from->si_int); 146 break; 147 default: 148 break; 149 } 150 } 151 return err ? -EFAULT : 0; 152 } 153 154 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs) 155 { 156 _sigregs32 user_sregs; 157 int i; 158 159 user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32); 160 user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI; 161 user_sregs.regs.psw.mask |= PSW32_USER_BITS; 162 user_sregs.regs.psw.addr = (__u32) regs->psw.addr | 163 (__u32)(regs->psw.mask & PSW_MASK_BA); 164 for (i = 0; i < NUM_GPRS; i++) 165 user_sregs.regs.gprs[i] = (__u32) regs->gprs[i]; 166 save_access_regs(current->thread.acrs); 167 memcpy(&user_sregs.regs.acrs, current->thread.acrs, 168 sizeof(user_sregs.regs.acrs)); 169 save_fp_ctl(¤t->thread.fp_regs.fpc); 170 save_fp_regs(current->thread.fp_regs.fprs); 171 memcpy(&user_sregs.fpregs, ¤t->thread.fp_regs, 172 sizeof(user_sregs.fpregs)); 173 if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32))) 174 return -EFAULT; 175 return 0; 176 } 177 178 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs) 179 { 180 _sigregs32 user_sregs; 181 int i; 182 183 /* Alwys make any pending restarted system call return -EINTR */ 184 current_thread_info()->restart_block.fn = do_no_restart_syscall; 185 186 if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs))) 187 return -EFAULT; 188 189 if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI)) 190 return -EINVAL; 191 192 /* Loading the floating-point-control word can fail. Do that first. */ 193 if (restore_fp_ctl(&user_sregs.fpregs.fpc)) 194 return -EINVAL; 195 196 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */ 197 regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) | 198 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 | 199 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 | 200 (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE); 201 /* Check for invalid user address space control. */ 202 if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME) 203 regs->psw.mask = PSW_ASC_PRIMARY | 204 (regs->psw.mask & ~PSW_MASK_ASC); 205 regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN); 206 for (i = 0; i < NUM_GPRS; i++) 207 regs->gprs[i] = (__u64) user_sregs.regs.gprs[i]; 208 memcpy(¤t->thread.acrs, &user_sregs.regs.acrs, 209 sizeof(current->thread.acrs)); 210 restore_access_regs(current->thread.acrs); 211 212 memcpy(¤t->thread.fp_regs, &user_sregs.fpregs, 213 sizeof(current->thread.fp_regs)); 214 215 restore_fp_regs(current->thread.fp_regs.fprs); 216 clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */ 217 return 0; 218 } 219 220 static int save_sigregs_gprs_high(struct pt_regs *regs, __u32 __user *uregs) 221 { 222 __u32 gprs_high[NUM_GPRS]; 223 int i; 224 225 for (i = 0; i < NUM_GPRS; i++) 226 gprs_high[i] = regs->gprs[i] >> 32; 227 if (__copy_to_user(uregs, &gprs_high, sizeof(gprs_high))) 228 return -EFAULT; 229 return 0; 230 } 231 232 static int restore_sigregs_gprs_high(struct pt_regs *regs, __u32 __user *uregs) 233 { 234 __u32 gprs_high[NUM_GPRS]; 235 int i; 236 237 if (__copy_from_user(&gprs_high, uregs, sizeof(gprs_high))) 238 return -EFAULT; 239 for (i = 0; i < NUM_GPRS; i++) 240 *(__u32 *)®s->gprs[i] = gprs_high[i]; 241 return 0; 242 } 243 244 COMPAT_SYSCALL_DEFINE0(sigreturn) 245 { 246 struct pt_regs *regs = task_pt_regs(current); 247 sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15]; 248 sigset_t set; 249 250 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE32)) 251 goto badframe; 252 set_current_blocked(&set); 253 if (restore_sigregs32(regs, &frame->sregs)) 254 goto badframe; 255 if (restore_sigregs_gprs_high(regs, frame->gprs_high)) 256 goto badframe; 257 return regs->gprs[2]; 258 badframe: 259 force_sig(SIGSEGV, current); 260 return 0; 261 } 262 263 COMPAT_SYSCALL_DEFINE0(rt_sigreturn) 264 { 265 struct pt_regs *regs = task_pt_regs(current); 266 rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15]; 267 sigset_t set; 268 269 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 270 goto badframe; 271 set_current_blocked(&set); 272 if (restore_sigregs32(regs, &frame->uc.uc_mcontext)) 273 goto badframe; 274 if (restore_sigregs_gprs_high(regs, frame->gprs_high)) 275 goto badframe; 276 if (compat_restore_altstack(&frame->uc.uc_stack)) 277 goto badframe; 278 return regs->gprs[2]; 279 badframe: 280 force_sig(SIGSEGV, current); 281 return 0; 282 } 283 284 /* 285 * Set up a signal frame. 286 */ 287 288 289 /* 290 * Determine which stack to use.. 291 */ 292 static inline void __user * 293 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 294 { 295 unsigned long sp; 296 297 /* Default to using normal stack */ 298 sp = (unsigned long) A(regs->gprs[15]); 299 300 /* Overflow on alternate signal stack gives SIGSEGV. */ 301 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL)) 302 return (void __user *) -1UL; 303 304 /* This is the X/Open sanctioned signal stack switching. */ 305 if (ka->sa.sa_flags & SA_ONSTACK) { 306 if (! sas_ss_flags(sp)) 307 sp = current->sas_ss_sp + current->sas_ss_size; 308 } 309 310 return (void __user *)((sp - frame_size) & -8ul); 311 } 312 313 static inline int map_signal(int sig) 314 { 315 if (current_thread_info()->exec_domain 316 && current_thread_info()->exec_domain->signal_invmap 317 && sig < 32) 318 return current_thread_info()->exec_domain->signal_invmap[sig]; 319 else 320 return sig; 321 } 322 323 static int setup_frame32(struct ksignal *ksig, sigset_t *set, 324 struct pt_regs *regs) 325 { 326 int sig = ksig->sig; 327 sigframe32 __user *frame = get_sigframe(&ksig->ka, regs, sizeof(sigframe32)); 328 329 if (frame == (void __user *) -1UL) 330 return -EFAULT; 331 332 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE32)) 333 return -EFAULT; 334 335 if (save_sigregs32(regs, &frame->sregs)) 336 return -EFAULT; 337 if (save_sigregs_gprs_high(regs, frame->gprs_high)) 338 return -EFAULT; 339 if (__put_user((unsigned long) &frame->sregs, &frame->sc.sregs)) 340 return -EFAULT; 341 342 /* Set up to return from userspace. If provided, use a stub 343 already in userspace. */ 344 if (ksig->ka.sa.sa_flags & SA_RESTORER) { 345 regs->gprs[14] = (__u64 __force) ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE; 346 } else { 347 regs->gprs[14] = (__u64 __force) frame->retcode | PSW32_ADDR_AMODE; 348 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, 349 (u16 __force __user *)(frame->retcode))) 350 return -EFAULT; 351 } 352 353 /* Set up backchain. */ 354 if (__put_user(regs->gprs[15], (unsigned int __user *) frame)) 355 return -EFAULT; 356 357 /* Set up registers for signal handler */ 358 regs->gprs[15] = (__force __u64) frame; 359 /* Force 31 bit amode and default user address space control. */ 360 regs->psw.mask = PSW_MASK_BA | 361 (PSW_USER_BITS & PSW_MASK_ASC) | 362 (regs->psw.mask & ~PSW_MASK_ASC); 363 regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler; 364 365 regs->gprs[2] = map_signal(sig); 366 regs->gprs[3] = (__force __u64) &frame->sc; 367 368 /* We forgot to include these in the sigcontext. 369 To avoid breaking binary compatibility, they are passed as args. */ 370 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 371 sig == SIGTRAP || sig == SIGFPE) { 372 /* set extra registers only for synchronous signals */ 373 regs->gprs[4] = regs->int_code & 127; 374 regs->gprs[5] = regs->int_parm_long; 375 regs->gprs[6] = task_thread_info(current)->last_break; 376 } 377 378 /* Place signal number on stack to allow backtrace from handler. */ 379 if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo)) 380 return -EFAULT; 381 return 0; 382 } 383 384 static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set, 385 struct pt_regs *regs) 386 { 387 int err = 0; 388 rt_sigframe32 __user *frame = get_sigframe(&ksig->ka, regs, sizeof(rt_sigframe32)); 389 390 if (frame == (void __user *) -1UL) 391 return -EFAULT; 392 393 if (copy_siginfo_to_user32(&frame->info, &ksig->info)) 394 return -EFAULT; 395 396 /* Create the ucontext. */ 397 err |= __put_user(UC_EXTENDED, &frame->uc.uc_flags); 398 err |= __put_user(0, &frame->uc.uc_link); 399 err |= __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]); 400 err |= save_sigregs32(regs, &frame->uc.uc_mcontext); 401 err |= save_sigregs_gprs_high(regs, frame->gprs_high); 402 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 403 if (err) 404 return -EFAULT; 405 406 /* Set up to return from userspace. If provided, use a stub 407 already in userspace. */ 408 if (ksig->ka.sa.sa_flags & SA_RESTORER) { 409 regs->gprs[14] = (__u64 __force) ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE; 410 } else { 411 regs->gprs[14] = (__u64 __force) frame->retcode | PSW32_ADDR_AMODE; 412 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, 413 (u16 __force __user *)(frame->retcode))) 414 return -EFAULT; 415 } 416 417 /* Set up backchain. */ 418 if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame)) 419 return -EFAULT; 420 421 /* Set up registers for signal handler */ 422 regs->gprs[15] = (__force __u64) frame; 423 /* Force 31 bit amode and default user address space control. */ 424 regs->psw.mask = PSW_MASK_BA | 425 (PSW_USER_BITS & PSW_MASK_ASC) | 426 (regs->psw.mask & ~PSW_MASK_ASC); 427 regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler; 428 429 regs->gprs[2] = map_signal(ksig->sig); 430 regs->gprs[3] = (__force __u64) &frame->info; 431 regs->gprs[4] = (__force __u64) &frame->uc; 432 regs->gprs[5] = task_thread_info(current)->last_break; 433 return 0; 434 } 435 436 /* 437 * OK, we're invoking a handler 438 */ 439 440 void handle_signal32(struct ksignal *ksig, sigset_t *oldset, 441 struct pt_regs *regs) 442 { 443 int ret; 444 445 /* Set up the stack frame */ 446 if (ksig->ka.sa.sa_flags & SA_SIGINFO) 447 ret = setup_rt_frame32(ksig, oldset, regs); 448 else 449 ret = setup_frame32(ksig, oldset, regs); 450 451 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP)); 452 } 453 454