1 /* 2 * arch/s390/kernel/signal.c 3 * 4 * Copyright (C) IBM Corp. 1999,2006 5 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com) 6 * 7 * Based on Intel version 8 * 9 * Copyright (C) 1991, 1992 Linus Torvalds 10 * 11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 12 */ 13 14 #include <linux/sched.h> 15 #include <linux/mm.h> 16 #include <linux/smp.h> 17 #include <linux/kernel.h> 18 #include <linux/signal.h> 19 #include <linux/errno.h> 20 #include <linux/wait.h> 21 #include <linux/ptrace.h> 22 #include <linux/unistd.h> 23 #include <linux/stddef.h> 24 #include <linux/tty.h> 25 #include <linux/personality.h> 26 #include <linux/binfmts.h> 27 #include <linux/tracehook.h> 28 #include <linux/syscalls.h> 29 #include <linux/compat.h> 30 #include <asm/ucontext.h> 31 #include <asm/uaccess.h> 32 #include <asm/lowcore.h> 33 #include <asm/compat.h> 34 #include "entry.h" 35 36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) 37 38 39 typedef struct 40 { 41 __u8 callee_used_stack[__SIGNAL_FRAMESIZE]; 42 struct sigcontext sc; 43 _sigregs sregs; 44 int signo; 45 __u8 retcode[S390_SYSCALL_SIZE]; 46 } sigframe; 47 48 typedef struct 49 { 50 __u8 callee_used_stack[__SIGNAL_FRAMESIZE]; 51 __u8 retcode[S390_SYSCALL_SIZE]; 52 struct siginfo info; 53 struct ucontext uc; 54 } rt_sigframe; 55 56 /* 57 * Atomically swap in the new signal mask, and wait for a signal. 58 */ 59 SYSCALL_DEFINE3(sigsuspend, int, history0, int, history1, old_sigset_t, mask) 60 { 61 sigset_t blocked; 62 63 current->saved_sigmask = current->blocked; 64 mask &= _BLOCKABLE; 65 siginitset(&blocked, mask); 66 set_current_blocked(&blocked); 67 set_current_state(TASK_INTERRUPTIBLE); 68 schedule(); 69 set_restore_sigmask(); 70 return -ERESTARTNOHAND; 71 } 72 73 SYSCALL_DEFINE3(sigaction, int, sig, const struct old_sigaction __user *, act, 74 struct old_sigaction __user *, oact) 75 { 76 struct k_sigaction new_ka, old_ka; 77 int ret; 78 79 if (act) { 80 old_sigset_t mask; 81 if (!access_ok(VERIFY_READ, act, sizeof(*act)) || 82 __get_user(new_ka.sa.sa_handler, &act->sa_handler) || 83 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) || 84 __get_user(new_ka.sa.sa_flags, &act->sa_flags) || 85 __get_user(mask, &act->sa_mask)) 86 return -EFAULT; 87 siginitset(&new_ka.sa.sa_mask, mask); 88 } 89 90 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); 91 92 if (!ret && oact) { 93 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || 94 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || 95 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) || 96 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) || 97 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask)) 98 return -EFAULT; 99 } 100 101 return ret; 102 } 103 104 SYSCALL_DEFINE2(sigaltstack, const stack_t __user *, uss, 105 stack_t __user *, uoss) 106 { 107 struct pt_regs *regs = task_pt_regs(current); 108 return do_sigaltstack(uss, uoss, regs->gprs[15]); 109 } 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 116 save_access_regs(current->thread.acrs); 117 118 /* Copy a 'clean' PSW mask to the user to avoid leaking 119 information about whether PER is currently on. */ 120 user_sregs.regs.psw.mask = psw_user_bits | 121 (regs->psw.mask & PSW_MASK_USER); 122 user_sregs.regs.psw.addr = regs->psw.addr; 123 memcpy(&user_sregs.regs.gprs, ®s->gprs, sizeof(sregs->regs.gprs)); 124 memcpy(&user_sregs.regs.acrs, current->thread.acrs, 125 sizeof(sregs->regs.acrs)); 126 /* 127 * We have to store the fp registers to current->thread.fp_regs 128 * to merge them with the emulated registers. 129 */ 130 save_fp_regs(¤t->thread.fp_regs); 131 memcpy(&user_sregs.fpregs, ¤t->thread.fp_regs, 132 sizeof(s390_fp_regs)); 133 return __copy_to_user(sregs, &user_sregs, sizeof(_sigregs)); 134 } 135 136 /* Returns positive number on error */ 137 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs) 138 { 139 int err; 140 _sigregs user_sregs; 141 142 /* Alwys make any pending restarted system call return -EINTR */ 143 current_thread_info()->restart_block.fn = do_no_restart_syscall; 144 145 err = __copy_from_user(&user_sregs, sregs, sizeof(_sigregs)); 146 if (err) 147 return err; 148 /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */ 149 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) | 150 (user_sregs.regs.psw.mask & PSW_MASK_USER); 151 /* Check for invalid amode */ 152 if (regs->psw.mask & PSW_MASK_EA) 153 regs->psw.mask |= PSW_MASK_BA; 154 regs->psw.addr = user_sregs.regs.psw.addr; 155 memcpy(®s->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs)); 156 memcpy(¤t->thread.acrs, &user_sregs.regs.acrs, 157 sizeof(sregs->regs.acrs)); 158 restore_access_regs(current->thread.acrs); 159 160 memcpy(¤t->thread.fp_regs, &user_sregs.fpregs, 161 sizeof(s390_fp_regs)); 162 current->thread.fp_regs.fpc &= FPC_VALID_MASK; 163 164 restore_fp_regs(¤t->thread.fp_regs); 165 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */ 166 return 0; 167 } 168 169 SYSCALL_DEFINE0(sigreturn) 170 { 171 struct pt_regs *regs = task_pt_regs(current); 172 sigframe __user *frame = (sigframe __user *)regs->gprs[15]; 173 sigset_t set; 174 175 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 176 goto badframe; 177 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE)) 178 goto badframe; 179 sigdelsetmask(&set, ~_BLOCKABLE); 180 set_current_blocked(&set); 181 if (restore_sigregs(regs, &frame->sregs)) 182 goto badframe; 183 return regs->gprs[2]; 184 badframe: 185 force_sig(SIGSEGV, current); 186 return 0; 187 } 188 189 SYSCALL_DEFINE0(rt_sigreturn) 190 { 191 struct pt_regs *regs = task_pt_regs(current); 192 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15]; 193 sigset_t set; 194 195 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 196 goto badframe; 197 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set))) 198 goto badframe; 199 sigdelsetmask(&set, ~_BLOCKABLE); 200 set_current_blocked(&set); 201 if (restore_sigregs(regs, &frame->uc.uc_mcontext)) 202 goto badframe; 203 if (do_sigaltstack(&frame->uc.uc_stack, NULL, 204 regs->gprs[15]) == -EFAULT) 205 goto badframe; 206 return regs->gprs[2]; 207 badframe: 208 force_sig(SIGSEGV, current); 209 return 0; 210 } 211 212 /* 213 * Set up a signal frame. 214 */ 215 216 217 /* 218 * Determine which stack to use.. 219 */ 220 static inline void __user * 221 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 222 { 223 unsigned long sp; 224 225 /* Default to using normal stack */ 226 sp = regs->gprs[15]; 227 228 /* Overflow on alternate signal stack gives SIGSEGV. */ 229 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL)) 230 return (void __user *) -1UL; 231 232 /* This is the X/Open sanctioned signal stack switching. */ 233 if (ka->sa.sa_flags & SA_ONSTACK) { 234 if (! sas_ss_flags(sp)) 235 sp = current->sas_ss_sp + current->sas_ss_size; 236 } 237 238 /* This is the legacy signal stack switching. */ 239 else if (!user_mode(regs) && 240 !(ka->sa.sa_flags & SA_RESTORER) && 241 ka->sa.sa_restorer) { 242 sp = (unsigned long) ka->sa.sa_restorer; 243 } 244 245 return (void __user *)((sp - frame_size) & -8ul); 246 } 247 248 static inline int map_signal(int sig) 249 { 250 if (current_thread_info()->exec_domain 251 && current_thread_info()->exec_domain->signal_invmap 252 && sig < 32) 253 return current_thread_info()->exec_domain->signal_invmap[sig]; 254 else 255 return sig; 256 } 257 258 static int setup_frame(int sig, struct k_sigaction *ka, 259 sigset_t *set, struct pt_regs * regs) 260 { 261 sigframe __user *frame; 262 263 frame = get_sigframe(ka, regs, sizeof(sigframe)); 264 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe))) 265 goto give_sigsegv; 266 267 if (frame == (void __user *) -1UL) 268 goto give_sigsegv; 269 270 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE)) 271 goto give_sigsegv; 272 273 if (save_sigregs(regs, &frame->sregs)) 274 goto give_sigsegv; 275 if (__put_user(&frame->sregs, &frame->sc.sregs)) 276 goto give_sigsegv; 277 278 /* Set up to return from userspace. If provided, use a stub 279 already in userspace. */ 280 if (ka->sa.sa_flags & SA_RESTORER) { 281 regs->gprs[14] = (unsigned long) 282 ka->sa.sa_restorer | PSW_ADDR_AMODE; 283 } else { 284 regs->gprs[14] = (unsigned long) 285 frame->retcode | PSW_ADDR_AMODE; 286 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, 287 (u16 __user *)(frame->retcode))) 288 goto give_sigsegv; 289 } 290 291 /* Set up backchain. */ 292 if (__put_user(regs->gprs[15], (addr_t __user *) frame)) 293 goto give_sigsegv; 294 295 /* Set up registers for signal handler */ 296 regs->gprs[15] = (unsigned long) frame; 297 regs->psw.mask |= PSW_MASK_EA | PSW_MASK_BA; /* 64 bit amode */ 298 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE; 299 300 regs->gprs[2] = map_signal(sig); 301 regs->gprs[3] = (unsigned long) &frame->sc; 302 303 /* We forgot to include these in the sigcontext. 304 To avoid breaking binary compatibility, they are passed as args. */ 305 regs->gprs[4] = current->thread.trap_no; 306 regs->gprs[5] = current->thread.prot_addr; 307 regs->gprs[6] = task_thread_info(current)->last_break; 308 309 /* Place signal number on stack to allow backtrace from handler. */ 310 if (__put_user(regs->gprs[2], (int __user *) &frame->signo)) 311 goto give_sigsegv; 312 return 0; 313 314 give_sigsegv: 315 force_sigsegv(sig, current); 316 return -EFAULT; 317 } 318 319 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 320 sigset_t *set, struct pt_regs * regs) 321 { 322 int err = 0; 323 rt_sigframe __user *frame; 324 325 frame = get_sigframe(ka, regs, sizeof(rt_sigframe)); 326 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe))) 327 goto give_sigsegv; 328 329 if (frame == (void __user *) -1UL) 330 goto give_sigsegv; 331 332 if (copy_siginfo_to_user(&frame->info, info)) 333 goto give_sigsegv; 334 335 /* Create the ucontext. */ 336 err |= __put_user(0, &frame->uc.uc_flags); 337 err |= __put_user(NULL, &frame->uc.uc_link); 338 err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); 339 err |= __put_user(sas_ss_flags(regs->gprs[15]), 340 &frame->uc.uc_stack.ss_flags); 341 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); 342 err |= save_sigregs(regs, &frame->uc.uc_mcontext); 343 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 344 if (err) 345 goto give_sigsegv; 346 347 /* Set up to return from userspace. If provided, use a stub 348 already in userspace. */ 349 if (ka->sa.sa_flags & SA_RESTORER) { 350 regs->gprs[14] = (unsigned long) 351 ka->sa.sa_restorer | PSW_ADDR_AMODE; 352 } else { 353 regs->gprs[14] = (unsigned long) 354 frame->retcode | PSW_ADDR_AMODE; 355 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, 356 (u16 __user *)(frame->retcode))) 357 goto give_sigsegv; 358 } 359 360 /* Set up backchain. */ 361 if (__put_user(regs->gprs[15], (addr_t __user *) frame)) 362 goto give_sigsegv; 363 364 /* Set up registers for signal handler */ 365 regs->gprs[15] = (unsigned long) frame; 366 regs->psw.mask |= PSW_MASK_EA | PSW_MASK_BA; /* 64 bit amode */ 367 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE; 368 369 regs->gprs[2] = map_signal(sig); 370 regs->gprs[3] = (unsigned long) &frame->info; 371 regs->gprs[4] = (unsigned long) &frame->uc; 372 regs->gprs[5] = task_thread_info(current)->last_break; 373 return 0; 374 375 give_sigsegv: 376 force_sigsegv(sig, current); 377 return -EFAULT; 378 } 379 380 static int handle_signal(unsigned long sig, struct k_sigaction *ka, 381 siginfo_t *info, sigset_t *oldset, 382 struct pt_regs *regs) 383 { 384 sigset_t blocked; 385 int ret; 386 387 /* Set up the stack frame */ 388 if (ka->sa.sa_flags & SA_SIGINFO) 389 ret = setup_rt_frame(sig, ka, info, oldset, regs); 390 else 391 ret = setup_frame(sig, ka, oldset, regs); 392 if (ret) 393 return ret; 394 sigorsets(&blocked, ¤t->blocked, &ka->sa.sa_mask); 395 if (!(ka->sa.sa_flags & SA_NODEFER)) 396 sigaddset(&blocked, sig); 397 set_current_blocked(&blocked); 398 return 0; 399 } 400 401 /* 402 * Note that 'init' is a special process: it doesn't get signals it doesn't 403 * want to handle. Thus you cannot kill init even with a SIGKILL even by 404 * mistake. 405 * 406 * Note that we go through the signals twice: once to check the signals that 407 * the kernel can handle, and then we build all the user-level signal handling 408 * stack-frames in one go after that. 409 */ 410 void do_signal(struct pt_regs *regs) 411 { 412 siginfo_t info; 413 int signr; 414 struct k_sigaction ka; 415 sigset_t *oldset; 416 417 /* 418 * We want the common case to go fast, which 419 * is why we may in certain cases get here from 420 * kernel mode. Just return without doing anything 421 * if so. 422 */ 423 if (!user_mode(regs)) 424 return; 425 426 if (test_thread_flag(TIF_RESTORE_SIGMASK)) 427 oldset = ¤t->saved_sigmask; 428 else 429 oldset = ¤t->blocked; 430 431 /* 432 * Get signal to deliver. When running under ptrace, at this point 433 * the debugger may change all our registers, including the system 434 * call information. 435 */ 436 current_thread_info()->system_call = 437 test_thread_flag(TIF_SYSCALL) ? regs->svc_code : 0; 438 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 439 440 if (signr > 0) { 441 /* Whee! Actually deliver the signal. */ 442 if (current_thread_info()->system_call) { 443 regs->svc_code = current_thread_info()->system_call; 444 /* Check for system call restarting. */ 445 switch (regs->gprs[2]) { 446 case -ERESTART_RESTARTBLOCK: 447 case -ERESTARTNOHAND: 448 regs->gprs[2] = -EINTR; 449 break; 450 case -ERESTARTSYS: 451 if (!(ka.sa.sa_flags & SA_RESTART)) { 452 regs->gprs[2] = -EINTR; 453 break; 454 } 455 /* fallthrough */ 456 case -ERESTARTNOINTR: 457 regs->gprs[2] = regs->orig_gpr2; 458 regs->psw.addr = 459 __rewind_psw(regs->psw, 460 regs->svc_code >> 16); 461 break; 462 } 463 /* No longer in a system call */ 464 clear_thread_flag(TIF_SYSCALL); 465 } 466 467 if ((is_compat_task() ? 468 handle_signal32(signr, &ka, &info, oldset, regs) : 469 handle_signal(signr, &ka, &info, oldset, regs)) == 0) { 470 /* 471 * A signal was successfully delivered; the saved 472 * sigmask will have been stored in the signal frame, 473 * and will be restored by sigreturn, so we can simply 474 * clear the TIF_RESTORE_SIGMASK flag. 475 */ 476 if (test_thread_flag(TIF_RESTORE_SIGMASK)) 477 clear_thread_flag(TIF_RESTORE_SIGMASK); 478 479 /* 480 * Let tracing know that we've done the handler setup. 481 */ 482 tracehook_signal_handler(signr, &info, &ka, regs, 483 test_thread_flag(TIF_SINGLE_STEP)); 484 } 485 return; 486 } 487 488 /* No handlers present - check for system call restart */ 489 if (current_thread_info()->system_call) { 490 regs->svc_code = current_thread_info()->system_call; 491 switch (regs->gprs[2]) { 492 case -ERESTART_RESTARTBLOCK: 493 /* Restart with sys_restart_syscall */ 494 regs->svc_code = __NR_restart_syscall; 495 /* fallthrough */ 496 case -ERESTARTNOHAND: 497 case -ERESTARTSYS: 498 case -ERESTARTNOINTR: 499 /* Restart system call with magic TIF bit. */ 500 regs->gprs[2] = regs->orig_gpr2; 501 set_thread_flag(TIF_SYSCALL); 502 break; 503 default: 504 clear_thread_flag(TIF_SYSCALL); 505 break; 506 } 507 } 508 509 /* 510 * If there's no signal to deliver, we just put the saved sigmask back. 511 */ 512 if (test_thread_flag(TIF_RESTORE_SIGMASK)) { 513 clear_thread_flag(TIF_RESTORE_SIGMASK); 514 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); 515 } 516 } 517 518 void do_notify_resume(struct pt_regs *regs) 519 { 520 clear_thread_flag(TIF_NOTIFY_RESUME); 521 tracehook_notify_resume(regs); 522 if (current->replacement_session_keyring) 523 key_replace_session_keyring(); 524 } 525