1/* 2 * arch/s390/kernel/signal.c 3 * 4 * S390 version 5 * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation 6 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com) 7 * 8 * Based on Intel version 9 * 10 * Copyright (C) 1991, 1992 Linus Torvalds 11 * 12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 13 */ 14 15#include <linux/config.h> 16#include <linux/sched.h> 17#include <linux/mm.h> 18#include <linux/smp.h> 19#include <linux/smp_lock.h> 20#include <linux/kernel.h> 21#include <linux/signal.h> 22#include <linux/errno.h> 23#include <linux/wait.h> 24#include <linux/ptrace.h> 25#include <linux/unistd.h> 26#include <linux/stddef.h> 27#include <linux/personality.h> 28#include <asm/ucontext.h> 29#include <asm/uaccess.h> 30 31#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) 32 33 34typedef struct 35{ 36 __u8 callee_used_stack[__SIGNAL_FRAMESIZE]; 37 struct sigcontext sc; 38 _sigregs sregs; 39 __u8 retcode[S390_SYSCALL_SIZE]; 40} sigframe; 41 42typedef struct 43{ 44 __u8 callee_used_stack[__SIGNAL_FRAMESIZE]; 45 __u8 retcode[S390_SYSCALL_SIZE]; 46 struct siginfo info; 47 struct ucontext uc; 48} rt_sigframe; 49 50asmlinkage int FASTCALL(do_signal(struct pt_regs *regs, sigset_t *oldset)); 51 52int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from) 53{ 54 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t))) 55 return -EFAULT; 56 if (from->si_code < 0) 57 return __copy_to_user(to, from, sizeof(siginfo_t)); 58 else { 59 int err; 60 61 /* If you change siginfo_t structure, please be sure 62 this code is fixed accordingly. 63 It should never copy any pad contained in the structure 64 to avoid security leaks, but must copy the generic 65 3 ints plus the relevant union member. */ 66 err = __put_user(from->si_signo, &to->si_signo); 67 err |= __put_user(from->si_errno, &to->si_errno); 68 err |= __put_user((short)from->si_code, &to->si_code); 69 /* First 32bits of unions are always present. */ 70 err |= __put_user(from->si_pid, &to->si_pid); 71 switch (from->si_code >> 16) { 72 case __SI_FAULT >> 16: 73 break; 74 case __SI_CHLD >> 16: 75 err |= __put_user(from->si_utime, &to->si_utime); 76 err |= __put_user(from->si_stime, &to->si_stime); 77 err |= __put_user(from->si_status, &to->si_status); 78 default: 79 err |= __put_user(from->si_uid, &to->si_uid); 80 break; 81 /* case __SI_RT: This is not generated by the kernel as of now. */ 82 } 83 return err; 84 } 85} 86 87/* 88 * Atomically swap in the new signal mask, and wait for a signal. 89 */ 90asmlinkage int 91sys_sigsuspend(struct pt_regs * regs,int history0, int history1, old_sigset_t mask) 92{ 93 sigset_t saveset; 94 95 mask &= _BLOCKABLE; 96 spin_lock_irq(¤t->sigmask_lock); 97 saveset = current->blocked; 98 siginitset(¤t->blocked, mask); 99 recalc_sigpending(current); 100 spin_unlock_irq(¤t->sigmask_lock); 101 regs->gprs[2] = -EINTR; 102 103 while (1) { 104 set_current_state(TASK_INTERRUPTIBLE); 105 schedule(); 106 if (do_signal(regs, &saveset)) 107 return -EINTR; 108 } 109} 110 111asmlinkage int 112sys_rt_sigsuspend(struct pt_regs * regs,sigset_t *unewset, size_t sigsetsize) 113{ 114 sigset_t saveset, newset; 115 116 if (sigsetsize != sizeof(sigset_t)) 117 return -EINVAL; 118 119 if (copy_from_user(&newset, unewset, sizeof(newset))) 120 return -EFAULT; 121 sigdelsetmask(&newset, ~_BLOCKABLE); 122 123 spin_lock_irq(¤t->sigmask_lock); 124 saveset = current->blocked; 125 current->blocked = newset; 126 recalc_sigpending(current); 127 spin_unlock_irq(¤t->sigmask_lock); 128 regs->gprs[2] = -EINTR; 129 130 while (1) { 131 set_current_state(TASK_INTERRUPTIBLE); 132 schedule(); 133 if (do_signal(regs, &saveset)) 134 return -EINTR; 135 } 136} 137 138asmlinkage int 139sys_sigaction(int sig, const struct old_sigaction *act, 140 struct old_sigaction *oact) 141{ 142 struct k_sigaction new_ka, old_ka; 143 int ret; 144 145 if (act) { 146 old_sigset_t mask; 147 if (verify_area(VERIFY_READ, act, sizeof(*act)) || 148 __get_user(new_ka.sa.sa_handler, &act->sa_handler) || 149 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer)) 150 return -EFAULT; 151 __get_user(new_ka.sa.sa_flags, &act->sa_flags); 152 __get_user(mask, &act->sa_mask); 153 siginitset(&new_ka.sa.sa_mask, mask); 154 } 155 156 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); 157 158 if (!ret && oact) { 159 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) || 160 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || 161 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer)) 162 return -EFAULT; 163 __put_user(old_ka.sa.sa_flags, &oact->sa_flags); 164 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask); 165 } 166 167 return ret; 168} 169 170asmlinkage int 171sys_sigaltstack(const stack_t *uss, stack_t *uoss, struct pt_regs *regs) 172{ 173 return do_sigaltstack(uss, uoss, regs->gprs[15]); 174} 175 176 177 178 179static int save_sigregs(struct pt_regs *regs,_sigregs *sregs) 180{ 181 int err; 182 s390_fp_regs fpregs; 183 184 err = __copy_to_user(&sregs->regs,regs,sizeof(_s390_regs_common)); 185 if(!err) 186 { 187 save_fp_regs(&fpregs); 188 err=__copy_to_user(&sregs->fpregs,&fpregs,sizeof(fpregs)); 189 } 190 return(err); 191 192} 193 194static int restore_sigregs(struct pt_regs *regs,_sigregs *sregs) 195{ 196 int err; 197 s390_fp_regs fpregs; 198 psw_t saved_psw=regs->psw; 199 err=__copy_from_user(regs,&sregs->regs,sizeof(_s390_regs_common)); 200 if(!err) 201 { 202 regs->trap = -1; /* disable syscall checks */ 203 regs->psw.mask=(saved_psw.mask&~PSW_MASK_DEBUGCHANGE)| 204 (regs->psw.mask&PSW_MASK_DEBUGCHANGE); 205 regs->psw.addr=(saved_psw.addr&~PSW_ADDR_DEBUGCHANGE)| 206 (regs->psw.addr&PSW_ADDR_DEBUGCHANGE); 207 err=__copy_from_user(&fpregs,&sregs->fpregs,sizeof(fpregs)); 208 if(!err) 209 restore_fp_regs(&fpregs); 210 } 211 return(err); 212} 213 214asmlinkage long sys_sigreturn(struct pt_regs *regs) 215{ 216 sigframe *frame = (sigframe *)regs->gprs[15]; 217 sigset_t set; 218 219 if (verify_area(VERIFY_READ, frame, sizeof(*frame))) 220 goto badframe; 221 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE)) 222 goto badframe; 223 224 sigdelsetmask(&set, ~_BLOCKABLE); 225 spin_lock_irq(¤t->sigmask_lock); 226 current->blocked = set; 227 recalc_sigpending(current); 228 spin_unlock_irq(¤t->sigmask_lock); 229 230 if (restore_sigregs(regs, &frame->sregs)) 231 goto badframe; 232 233 return regs->gprs[2]; 234 235badframe: 236 force_sig(SIGSEGV, current); 237 return 0; 238} 239 240asmlinkage long sys_rt_sigreturn(struct pt_regs *regs) 241{ 242 rt_sigframe *frame = (rt_sigframe *)regs->gprs[15]; 243 sigset_t set; 244 245 if (verify_area(VERIFY_READ, frame, sizeof(*frame))) 246 goto badframe; 247 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set))) 248 goto badframe; 249 250 sigdelsetmask(&set, ~_BLOCKABLE); 251 spin_lock_irq(¤t->sigmask_lock); 252 current->blocked = set; 253 recalc_sigpending(current); 254 spin_unlock_irq(¤t->sigmask_lock); 255 256 if (restore_sigregs(regs, &frame->uc.uc_mcontext)) 257 goto badframe; 258 259 /* It is more difficult to avoid calling this function than to 260 call it and ignore errors. */ 261 do_sigaltstack(&frame->uc.uc_stack, NULL, regs->gprs[15]); 262 return regs->gprs[2]; 263 264badframe: 265 force_sig(SIGSEGV, current); 266 return 0; 267} 268 269/* 270 * Set up a signal frame. 271 */ 272 273 274/* 275 * Determine which stack to use.. 276 */ 277static inline void * 278get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 279{ 280 unsigned long sp; 281 282 /* Default to using normal stack */ 283 sp = regs->gprs[15]; 284 285 /* This is the X/Open sanctioned signal stack switching. */ 286 if (ka->sa.sa_flags & SA_ONSTACK) { 287 if (! on_sig_stack(sp)) 288 sp = current->sas_ss_sp + current->sas_ss_size; 289 } 290 291 /* This is the legacy signal stack switching. */ 292 else if (!user_mode(regs) && 293 !(ka->sa.sa_flags & SA_RESTORER) && 294 ka->sa.sa_restorer) { 295 sp = (unsigned long) ka->sa.sa_restorer; 296 } 297 298 return (void *)((sp - frame_size) & -8ul); 299} 300 301static inline int map_signal(int sig) 302{ 303 if (current->exec_domain 304 && current->exec_domain->signal_invmap 305 && sig < 32) 306 return current->exec_domain->signal_invmap[sig]; 307 else 308 return sig; 309} 310 311static void setup_frame(int sig, struct k_sigaction *ka, 312 sigset_t *set, struct pt_regs * regs) 313{ 314 sigframe *frame = get_sigframe(ka, regs, sizeof(sigframe)); 315 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe))) 316 goto give_sigsegv; 317 318 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE)) 319 goto give_sigsegv; 320 321 if (save_sigregs(regs, &frame->sregs)) 322 goto give_sigsegv; 323 if (__put_user(&frame->sregs, &frame->sc.sregs)) 324 goto give_sigsegv; 325 326 /* Set up to return from userspace. If provided, use a stub 327 already in userspace. */ 328 if (ka->sa.sa_flags & SA_RESTORER) { 329 regs->gprs[14] = FIX_PSW(ka->sa.sa_restorer); 330 } else { 331 regs->gprs[14] = FIX_PSW(frame->retcode); 332 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, 333 (u16 *)(frame->retcode))) 334 goto give_sigsegv; 335 } 336 337 /* Set up backchain. */ 338 if (__put_user(regs->gprs[15], (addr_t *) frame)) 339 goto give_sigsegv; 340 341 /* Set up registers for signal handler */ 342 regs->gprs[15] = (addr_t)frame; 343 regs->psw.addr = FIX_PSW(ka->sa.sa_handler); 344 regs->psw.mask = _USER_PSW_MASK; 345 346 regs->gprs[2] = map_signal(sig); 347 regs->gprs[3] = (addr_t)&frame->sc; 348 349 /* We forgot to include these in the sigcontext. 350 To avoid breaking binary compatibility, they are passed as args. */ 351 regs->gprs[4] = current->thread.trap_no; 352 regs->gprs[5] = current->thread.prot_addr; 353 return; 354 355give_sigsegv: 356 if (sig == SIGSEGV) 357 ka->sa.sa_handler = SIG_DFL; 358 force_sig(SIGSEGV, current); 359} 360 361static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 362 sigset_t *set, struct pt_regs * regs) 363{ 364 int err = 0; 365 rt_sigframe *frame = get_sigframe(ka, regs, sizeof(rt_sigframe)); 366 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe))) 367 goto give_sigsegv; 368 369 if (copy_siginfo_to_user(&frame->info, info)) 370 goto give_sigsegv; 371 372 /* Create the ucontext. */ 373 err |= __put_user(0, &frame->uc.uc_flags); 374 err |= __put_user(0, &frame->uc.uc_link); 375 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); 376 err |= __put_user(sas_ss_flags(regs->gprs[15]), 377 &frame->uc.uc_stack.ss_flags); 378 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); 379 err |= save_sigregs(regs, &frame->uc.uc_mcontext); 380 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 381 if (err) 382 goto give_sigsegv; 383 384 /* Set up to return from userspace. If provided, use a stub 385 already in userspace. */ 386 if (ka->sa.sa_flags & SA_RESTORER) { 387 regs->gprs[14] = FIX_PSW(ka->sa.sa_restorer); 388 } else { 389 regs->gprs[14] = FIX_PSW(frame->retcode); 390 err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, 391 (u16 *)(frame->retcode)); 392 } 393 394 /* Set up backchain. */ 395 if (__put_user(regs->gprs[15], (addr_t *) frame)) 396 goto give_sigsegv; 397 398 /* Set up registers for signal handler */ 399 regs->gprs[15] = (addr_t)frame; 400 regs->psw.addr = FIX_PSW(ka->sa.sa_handler); 401 regs->psw.mask = _USER_PSW_MASK; 402 403 regs->gprs[2] = map_signal(sig); 404 regs->gprs[3] = (addr_t)&frame->info; 405 regs->gprs[4] = (addr_t)&frame->uc; 406 return; 407 408give_sigsegv: 409 if (sig == SIGSEGV) 410 ka->sa.sa_handler = SIG_DFL; 411 force_sig(SIGSEGV, current); 412} 413 414/* 415 * OK, we're invoking a handler 416 */ 417 418static void 419handle_signal(unsigned long sig, struct k_sigaction *ka, 420 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs) 421{ 422 /* Are we from a system call? */ 423 if (regs->trap == __LC_SVC_OLD_PSW) { 424 /* If so, check system call restarting.. */ 425 switch (regs->gprs[2]) { 426 case -ERESTARTNOHAND: 427 regs->gprs[2] = -EINTR; 428 break; 429 430 case -ERESTARTSYS: 431 if (!(ka->sa.sa_flags & SA_RESTART)) { 432 regs->gprs[2] = -EINTR; 433 break; 434 } 435 /* fallthrough */ 436 case -ERESTARTNOINTR: 437 regs->gprs[2] = regs->orig_gpr2; 438 regs->psw.addr -= 2; 439 } 440 } 441 442 /* Set up the stack frame */ 443 if (ka->sa.sa_flags & SA_SIGINFO) 444 setup_rt_frame(sig, ka, info, oldset, regs); 445 else 446 setup_frame(sig, ka, oldset, regs); 447 448 if (ka->sa.sa_flags & SA_ONESHOT) 449 ka->sa.sa_handler = SIG_DFL; 450 451 if (!(ka->sa.sa_flags & SA_NODEFER)) { 452 spin_lock_irq(¤t->sigmask_lock); 453 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask); 454 sigaddset(¤t->blocked,sig); 455 recalc_sigpending(current); 456 spin_unlock_irq(¤t->sigmask_lock); 457 } 458} 459 460/* 461 * Note that 'init' is a special process: it doesn't get signals it doesn't 462 * want to handle. Thus you cannot kill init even with a SIGKILL even by 463 * mistake. 464 * 465 * Note that we go through the signals twice: once to check the signals that 466 * the kernel can handle, and then we build all the user-level signal handling 467 * stack-frames in one go after that. 468 */ 469int do_signal(struct pt_regs *regs, sigset_t *oldset) 470{ 471 siginfo_t info; 472 struct k_sigaction *ka; 473 474 /* 475 * We want the common case to go fast, which 476 * is why we may in certain cases get here from 477 * kernel mode. Just return without doing anything 478 * if so. 479 */ 480 if (!user_mode(regs)) 481 return 1; 482 483 if (!oldset) 484 oldset = ¤t->blocked; 485 486 for (;;) { 487 unsigned long signr; 488 489 spin_lock_irq(¤t->sigmask_lock); 490 signr = dequeue_signal(¤t->blocked, &info); 491 spin_unlock_irq(¤t->sigmask_lock); 492 493 if (!signr) 494 break; 495 496 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) { 497 /* Let the debugger run. */ 498 current->exit_code = signr; 499 set_current_state(TASK_STOPPED); 500 notify_parent(current, SIGCHLD); 501 schedule(); 502 503 /* We're back. Did the debugger cancel the sig? */ 504 if (!(signr = current->exit_code)) 505 continue; 506 current->exit_code = 0; 507 508 /* The debugger continued. Ignore SIGSTOP. */ 509 if (signr == SIGSTOP) 510 continue; 511 512 /* Update the siginfo structure. Is this good? */ 513 if (signr != info.si_signo) { 514 info.si_signo = signr; 515 info.si_errno = 0; 516 info.si_code = SI_USER; 517 info.si_pid = current->p_pptr->pid; 518 info.si_uid = current->p_pptr->uid; 519 } 520 521 /* If the (new) signal is now blocked, requeue it. */ 522 if (sigismember(¤t->blocked, signr)) { 523 send_sig_info(signr, &info, current); 524 continue; 525 } 526 } 527 528 ka = ¤t->sig->action[signr-1]; 529 if (ka->sa.sa_handler == SIG_IGN) { 530 if (signr != SIGCHLD) 531 continue; 532 /* Check for SIGCHLD: it's special. */ 533 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0) 534 /* nothing */; 535 continue; 536 } 537 538 if (ka->sa.sa_handler == SIG_DFL) { 539 int exit_code = signr; 540 541 /* Init gets no signals it doesn't want. */ 542 if (current->pid == 1) 543 continue; 544 545 switch (signr) { 546 case SIGCONT: case SIGCHLD: case SIGWINCH: 547 continue; 548 549 case SIGTSTP: case SIGTTIN: case SIGTTOU: 550 if (is_orphaned_pgrp(current->pgrp)) 551 continue; 552 /* FALLTHRU */ 553 554 case SIGSTOP: { 555 struct signal_struct *sig; 556 set_current_state(TASK_STOPPED); 557 current->exit_code = signr; 558 sig = current->p_pptr->sig; 559 if (sig && !(sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP)) 560 notify_parent(current, SIGCHLD); 561 schedule(); 562 continue; 563 } 564 565 case SIGQUIT: case SIGILL: case SIGTRAP: 566 case SIGABRT: case SIGFPE: case SIGSEGV: 567 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ: 568 if (do_coredump(signr, regs)) 569 exit_code |= 0x80; 570 /* FALLTHRU */ 571 572 default: 573 sig_exit(signr, exit_code, &info); 574 /* NOTREACHED */ 575 } 576 } 577 578 /* Whee! Actually deliver the signal. */ 579 handle_signal(signr, ka, &info, oldset, regs); 580 return 1; 581 } 582 583 /* Did we come from a system call? */ 584 if ( regs->trap == __LC_SVC_OLD_PSW /* System Call! */ ) { 585 /* Restart the system call - no handlers present */ 586 if (regs->gprs[2] == -ERESTARTNOHAND || 587 regs->gprs[2] == -ERESTARTSYS || 588 regs->gprs[2] == -ERESTARTNOINTR) { 589 regs->gprs[2] = regs->orig_gpr2; 590 regs->psw.addr -= 2; 591 } 592 } 593 return 0; 594} 595