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 _sigregs_ext32 sregs_ext; 40 __u16 svc_insn; /* Offset of svc_insn is NOT fixed! */ 41 } sigframe32; 42 43 typedef struct 44 { 45 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32]; 46 __u16 svc_insn; 47 compat_siginfo_t info; 48 struct ucontext32 uc; 49 } rt_sigframe32; 50 51 int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from) 52 { 53 int err; 54 55 /* If you change siginfo_t structure, please be sure 56 this code is fixed accordingly. 57 It should never copy any pad contained in the structure 58 to avoid security leaks, but must copy the generic 59 3 ints plus the relevant union member. 60 This routine must convert siginfo from 64bit to 32bit as well 61 at the same time. */ 62 err = __put_user(from->si_signo, &to->si_signo); 63 err |= __put_user(from->si_errno, &to->si_errno); 64 err |= __put_user((short)from->si_code, &to->si_code); 65 if (from->si_code < 0) 66 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE); 67 else { 68 switch (from->si_code >> 16) { 69 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */ 70 case __SI_MESGQ >> 16: 71 err |= __put_user(from->si_int, &to->si_int); 72 /* fallthrough */ 73 case __SI_KILL >> 16: 74 err |= __put_user(from->si_pid, &to->si_pid); 75 err |= __put_user(from->si_uid, &to->si_uid); 76 break; 77 case __SI_CHLD >> 16: 78 err |= __put_user(from->si_pid, &to->si_pid); 79 err |= __put_user(from->si_uid, &to->si_uid); 80 err |= __put_user(from->si_utime, &to->si_utime); 81 err |= __put_user(from->si_stime, &to->si_stime); 82 err |= __put_user(from->si_status, &to->si_status); 83 break; 84 case __SI_FAULT >> 16: 85 err |= __put_user((unsigned long) from->si_addr, 86 &to->si_addr); 87 break; 88 case __SI_POLL >> 16: 89 err |= __put_user(from->si_band, &to->si_band); 90 err |= __put_user(from->si_fd, &to->si_fd); 91 break; 92 case __SI_TIMER >> 16: 93 err |= __put_user(from->si_tid, &to->si_tid); 94 err |= __put_user(from->si_overrun, &to->si_overrun); 95 err |= __put_user(from->si_int, &to->si_int); 96 break; 97 default: 98 break; 99 } 100 } 101 return err ? -EFAULT : 0; 102 } 103 104 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from) 105 { 106 int err; 107 u32 tmp; 108 109 err = __get_user(to->si_signo, &from->si_signo); 110 err |= __get_user(to->si_errno, &from->si_errno); 111 err |= __get_user(to->si_code, &from->si_code); 112 113 if (to->si_code < 0) 114 err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE); 115 else { 116 switch (to->si_code >> 16) { 117 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */ 118 case __SI_MESGQ >> 16: 119 err |= __get_user(to->si_int, &from->si_int); 120 /* fallthrough */ 121 case __SI_KILL >> 16: 122 err |= __get_user(to->si_pid, &from->si_pid); 123 err |= __get_user(to->si_uid, &from->si_uid); 124 break; 125 case __SI_CHLD >> 16: 126 err |= __get_user(to->si_pid, &from->si_pid); 127 err |= __get_user(to->si_uid, &from->si_uid); 128 err |= __get_user(to->si_utime, &from->si_utime); 129 err |= __get_user(to->si_stime, &from->si_stime); 130 err |= __get_user(to->si_status, &from->si_status); 131 break; 132 case __SI_FAULT >> 16: 133 err |= __get_user(tmp, &from->si_addr); 134 to->si_addr = (void __force __user *) 135 (u64) (tmp & PSW32_ADDR_INSN); 136 break; 137 case __SI_POLL >> 16: 138 err |= __get_user(to->si_band, &from->si_band); 139 err |= __get_user(to->si_fd, &from->si_fd); 140 break; 141 case __SI_TIMER >> 16: 142 err |= __get_user(to->si_tid, &from->si_tid); 143 err |= __get_user(to->si_overrun, &from->si_overrun); 144 err |= __get_user(to->si_int, &from->si_int); 145 break; 146 default: 147 break; 148 } 149 } 150 return err ? -EFAULT : 0; 151 } 152 153 /* Store registers needed to create the signal frame */ 154 static void store_sigregs(void) 155 { 156 int i; 157 158 save_access_regs(current->thread.acrs); 159 save_fp_ctl(¤t->thread.fp_regs.fpc); 160 if (current->thread.vxrs) { 161 save_vx_regs(current->thread.vxrs); 162 for (i = 0; i < __NUM_FPRS; i++) 163 current->thread.fp_regs.fprs[i] = 164 *(freg_t *)(current->thread.vxrs + i); 165 } else 166 save_fp_regs(current->thread.fp_regs.fprs); 167 } 168 169 /* Load registers after signal return */ 170 static void load_sigregs(void) 171 { 172 int i; 173 174 restore_access_regs(current->thread.acrs); 175 /* restore_fp_ctl is done in restore_sigregs */ 176 if (current->thread.vxrs) { 177 for (i = 0; i < __NUM_FPRS; i++) 178 *(freg_t *)(current->thread.vxrs + i) = 179 current->thread.fp_regs.fprs[i]; 180 restore_vx_regs(current->thread.vxrs); 181 } else 182 restore_fp_regs(current->thread.fp_regs.fprs); 183 } 184 185 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs) 186 { 187 _sigregs32 user_sregs; 188 int i; 189 190 user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32); 191 user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI; 192 user_sregs.regs.psw.mask |= PSW32_USER_BITS; 193 user_sregs.regs.psw.addr = (__u32) regs->psw.addr | 194 (__u32)(regs->psw.mask & PSW_MASK_BA); 195 for (i = 0; i < NUM_GPRS; i++) 196 user_sregs.regs.gprs[i] = (__u32) regs->gprs[i]; 197 memcpy(&user_sregs.regs.acrs, current->thread.acrs, 198 sizeof(user_sregs.regs.acrs)); 199 memcpy(&user_sregs.fpregs, ¤t->thread.fp_regs, 200 sizeof(user_sregs.fpregs)); 201 if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32))) 202 return -EFAULT; 203 return 0; 204 } 205 206 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs) 207 { 208 _sigregs32 user_sregs; 209 int i; 210 211 /* Alwys make any pending restarted system call return -EINTR */ 212 current->restart_block.fn = do_no_restart_syscall; 213 214 if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs))) 215 return -EFAULT; 216 217 if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI)) 218 return -EINVAL; 219 220 /* Loading the floating-point-control word can fail. Do that first. */ 221 if (restore_fp_ctl(&user_sregs.fpregs.fpc)) 222 return -EINVAL; 223 224 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */ 225 regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) | 226 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 | 227 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 | 228 (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE); 229 /* Check for invalid user address space control. */ 230 if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME) 231 regs->psw.mask = PSW_ASC_PRIMARY | 232 (regs->psw.mask & ~PSW_MASK_ASC); 233 regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN); 234 for (i = 0; i < NUM_GPRS; i++) 235 regs->gprs[i] = (__u64) user_sregs.regs.gprs[i]; 236 memcpy(¤t->thread.acrs, &user_sregs.regs.acrs, 237 sizeof(current->thread.acrs)); 238 239 memcpy(¤t->thread.fp_regs, &user_sregs.fpregs, 240 sizeof(current->thread.fp_regs)); 241 242 clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */ 243 return 0; 244 } 245 246 static int save_sigregs_ext32(struct pt_regs *regs, 247 _sigregs_ext32 __user *sregs_ext) 248 { 249 __u32 gprs_high[NUM_GPRS]; 250 __u64 vxrs[__NUM_VXRS_LOW]; 251 int i; 252 253 /* Save high gprs to signal stack */ 254 for (i = 0; i < NUM_GPRS; i++) 255 gprs_high[i] = regs->gprs[i] >> 32; 256 if (__copy_to_user(&sregs_ext->gprs_high, &gprs_high, 257 sizeof(sregs_ext->gprs_high))) 258 return -EFAULT; 259 260 /* Save vector registers to signal stack */ 261 if (current->thread.vxrs) { 262 for (i = 0; i < __NUM_VXRS_LOW; i++) 263 vxrs[i] = *((__u64 *)(current->thread.vxrs + i) + 1); 264 if (__copy_to_user(&sregs_ext->vxrs_low, vxrs, 265 sizeof(sregs_ext->vxrs_low)) || 266 __copy_to_user(&sregs_ext->vxrs_high, 267 current->thread.vxrs + __NUM_VXRS_LOW, 268 sizeof(sregs_ext->vxrs_high))) 269 return -EFAULT; 270 } 271 return 0; 272 } 273 274 static int restore_sigregs_ext32(struct pt_regs *regs, 275 _sigregs_ext32 __user *sregs_ext) 276 { 277 __u32 gprs_high[NUM_GPRS]; 278 __u64 vxrs[__NUM_VXRS_LOW]; 279 int i; 280 281 /* Restore high gprs from signal stack */ 282 if (__copy_from_user(&gprs_high, &sregs_ext->gprs_high, 283 sizeof(&sregs_ext->gprs_high))) 284 return -EFAULT; 285 for (i = 0; i < NUM_GPRS; i++) 286 *(__u32 *)®s->gprs[i] = gprs_high[i]; 287 288 /* Restore vector registers from signal stack */ 289 if (current->thread.vxrs) { 290 if (__copy_from_user(vxrs, &sregs_ext->vxrs_low, 291 sizeof(sregs_ext->vxrs_low)) || 292 __copy_from_user(current->thread.vxrs + __NUM_VXRS_LOW, 293 &sregs_ext->vxrs_high, 294 sizeof(sregs_ext->vxrs_high))) 295 return -EFAULT; 296 for (i = 0; i < __NUM_VXRS_LOW; i++) 297 *((__u64 *)(current->thread.vxrs + i) + 1) = vxrs[i]; 298 } 299 return 0; 300 } 301 302 COMPAT_SYSCALL_DEFINE0(sigreturn) 303 { 304 struct pt_regs *regs = task_pt_regs(current); 305 sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15]; 306 sigset_t set; 307 308 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE32)) 309 goto badframe; 310 set_current_blocked(&set); 311 if (restore_sigregs32(regs, &frame->sregs)) 312 goto badframe; 313 if (restore_sigregs_ext32(regs, &frame->sregs_ext)) 314 goto badframe; 315 load_sigregs(); 316 return regs->gprs[2]; 317 badframe: 318 force_sig(SIGSEGV, current); 319 return 0; 320 } 321 322 COMPAT_SYSCALL_DEFINE0(rt_sigreturn) 323 { 324 struct pt_regs *regs = task_pt_regs(current); 325 rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15]; 326 sigset_t set; 327 328 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 329 goto badframe; 330 set_current_blocked(&set); 331 if (compat_restore_altstack(&frame->uc.uc_stack)) 332 goto badframe; 333 if (restore_sigregs32(regs, &frame->uc.uc_mcontext)) 334 goto badframe; 335 if (restore_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext)) 336 goto badframe; 337 load_sigregs(); 338 return regs->gprs[2]; 339 badframe: 340 force_sig(SIGSEGV, current); 341 return 0; 342 } 343 344 /* 345 * Set up a signal frame. 346 */ 347 348 349 /* 350 * Determine which stack to use.. 351 */ 352 static inline void __user * 353 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 354 { 355 unsigned long sp; 356 357 /* Default to using normal stack */ 358 sp = (unsigned long) A(regs->gprs[15]); 359 360 /* Overflow on alternate signal stack gives SIGSEGV. */ 361 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL)) 362 return (void __user *) -1UL; 363 364 /* This is the X/Open sanctioned signal stack switching. */ 365 if (ka->sa.sa_flags & SA_ONSTACK) { 366 if (! sas_ss_flags(sp)) 367 sp = current->sas_ss_sp + current->sas_ss_size; 368 } 369 370 return (void __user *)((sp - frame_size) & -8ul); 371 } 372 373 static int setup_frame32(struct ksignal *ksig, sigset_t *set, 374 struct pt_regs *regs) 375 { 376 int sig = ksig->sig; 377 sigframe32 __user *frame; 378 struct sigcontext32 sc; 379 unsigned long restorer; 380 size_t frame_size; 381 382 /* 383 * gprs_high are always present for 31-bit compat tasks. 384 * The space for vector registers is only allocated if 385 * the machine supports it 386 */ 387 frame_size = sizeof(*frame) - sizeof(frame->sregs_ext.__reserved); 388 if (!MACHINE_HAS_VX) 389 frame_size -= sizeof(frame->sregs_ext.vxrs_low) + 390 sizeof(frame->sregs_ext.vxrs_high); 391 frame = get_sigframe(&ksig->ka, regs, frame_size); 392 if (frame == (void __user *) -1UL) 393 return -EFAULT; 394 395 /* Set up backchain. */ 396 if (__put_user(regs->gprs[15], (unsigned int __user *) frame)) 397 return -EFAULT; 398 399 /* Create struct sigcontext32 on the signal stack */ 400 memcpy(&sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE32); 401 sc.sregs = (__u32)(unsigned long __force) &frame->sregs; 402 if (__copy_to_user(&frame->sc, &sc, sizeof(frame->sc))) 403 return -EFAULT; 404 405 /* Store registers needed to create the signal frame */ 406 store_sigregs(); 407 408 /* Create _sigregs32 on the signal stack */ 409 if (save_sigregs32(regs, &frame->sregs)) 410 return -EFAULT; 411 412 /* Place signal number on stack to allow backtrace from handler. */ 413 if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo)) 414 return -EFAULT; 415 416 /* Create _sigregs_ext32 on the signal stack */ 417 if (save_sigregs_ext32(regs, &frame->sregs_ext)) 418 return -EFAULT; 419 420 /* Set up to return from userspace. If provided, use a stub 421 already in userspace. */ 422 if (ksig->ka.sa.sa_flags & SA_RESTORER) { 423 restorer = (unsigned long __force) 424 ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE; 425 } else { 426 /* Signal frames without vectors registers are short ! */ 427 __u16 __user *svc = (void __user *) frame + frame_size - 2; 428 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc)) 429 return -EFAULT; 430 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE; 431 } 432 433 /* Set up registers for signal handler */ 434 regs->gprs[14] = restorer; 435 regs->gprs[15] = (__force __u64) frame; 436 /* Force 31 bit amode and default user address space control. */ 437 regs->psw.mask = PSW_MASK_BA | 438 (PSW_USER_BITS & PSW_MASK_ASC) | 439 (regs->psw.mask & ~PSW_MASK_ASC); 440 regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler; 441 442 regs->gprs[2] = sig; 443 regs->gprs[3] = (__force __u64) &frame->sc; 444 445 /* We forgot to include these in the sigcontext. 446 To avoid breaking binary compatibility, they are passed as args. */ 447 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 448 sig == SIGTRAP || sig == SIGFPE) { 449 /* set extra registers only for synchronous signals */ 450 regs->gprs[4] = regs->int_code & 127; 451 regs->gprs[5] = regs->int_parm_long; 452 regs->gprs[6] = task_thread_info(current)->last_break; 453 } 454 455 return 0; 456 } 457 458 static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set, 459 struct pt_regs *regs) 460 { 461 rt_sigframe32 __user *frame; 462 unsigned long restorer; 463 size_t frame_size; 464 u32 uc_flags; 465 466 frame_size = sizeof(*frame) - 467 sizeof(frame->uc.uc_mcontext_ext.__reserved); 468 /* 469 * gprs_high are always present for 31-bit compat tasks. 470 * The space for vector registers is only allocated if 471 * the machine supports it 472 */ 473 uc_flags = UC_GPRS_HIGH; 474 if (MACHINE_HAS_VX) { 475 if (current->thread.vxrs) 476 uc_flags |= UC_VXRS; 477 } else 478 frame_size -= sizeof(frame->uc.uc_mcontext_ext.vxrs_low) + 479 sizeof(frame->uc.uc_mcontext_ext.vxrs_high); 480 frame = get_sigframe(&ksig->ka, regs, frame_size); 481 if (frame == (void __user *) -1UL) 482 return -EFAULT; 483 484 /* Set up backchain. */ 485 if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame)) 486 return -EFAULT; 487 488 /* Set up to return from userspace. If provided, use a stub 489 already in userspace. */ 490 if (ksig->ka.sa.sa_flags & SA_RESTORER) { 491 restorer = (unsigned long __force) 492 ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE; 493 } else { 494 __u16 __user *svc = &frame->svc_insn; 495 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc)) 496 return -EFAULT; 497 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE; 498 } 499 500 /* Create siginfo on the signal stack */ 501 if (copy_siginfo_to_user32(&frame->info, &ksig->info)) 502 return -EFAULT; 503 504 /* Store registers needed to create the signal frame */ 505 store_sigregs(); 506 507 /* Create ucontext on the signal stack. */ 508 if (__put_user(uc_flags, &frame->uc.uc_flags) || 509 __put_user(0, &frame->uc.uc_link) || 510 __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]) || 511 save_sigregs32(regs, &frame->uc.uc_mcontext) || 512 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)) || 513 save_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext)) 514 return -EFAULT; 515 516 /* Set up registers for signal handler */ 517 regs->gprs[14] = restorer; 518 regs->gprs[15] = (__force __u64) frame; 519 /* Force 31 bit amode and default user address space control. */ 520 regs->psw.mask = PSW_MASK_BA | 521 (PSW_USER_BITS & PSW_MASK_ASC) | 522 (regs->psw.mask & ~PSW_MASK_ASC); 523 regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler; 524 525 regs->gprs[2] = ksig->sig; 526 regs->gprs[3] = (__force __u64) &frame->info; 527 regs->gprs[4] = (__force __u64) &frame->uc; 528 regs->gprs[5] = task_thread_info(current)->last_break; 529 return 0; 530 } 531 532 /* 533 * OK, we're invoking a handler 534 */ 535 536 void handle_signal32(struct ksignal *ksig, sigset_t *oldset, 537 struct pt_regs *regs) 538 { 539 int ret; 540 541 /* Set up the stack frame */ 542 if (ksig->ka.sa.sa_flags & SA_SIGINFO) 543 ret = setup_rt_frame32(ksig, oldset, regs); 544 else 545 ret = setup_frame32(ksig, oldset, regs); 546 547 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP)); 548 } 549 550