1 /* 2 * Copyright (C) 1991, 1992 Linus Torvalds 3 * 4 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 5 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes 6 */ 7 8 #include <linux/sched.h> 9 #include <linux/mm.h> 10 #include <linux/smp.h> 11 #include <linux/kernel.h> 12 #include <linux/signal.h> 13 #include <linux/errno.h> 14 #include <linux/wait.h> 15 #include <linux/unistd.h> 16 #include <linux/stddef.h> 17 #include <linux/personality.h> 18 #include <linux/suspend.h> 19 #include <linux/ptrace.h> 20 #include <linux/elf.h> 21 #include <linux/binfmts.h> 22 #include <asm/processor.h> 23 #include <asm/ucontext.h> 24 #include <asm/uaccess.h> 25 #include <asm/i387.h> 26 #include "sigframe_32.h" 27 28 #define DEBUG_SIG 0 29 30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) 31 32 /* 33 * Atomically swap in the new signal mask, and wait for a signal. 34 */ 35 asmlinkage int 36 sys_sigsuspend(int history0, int history1, old_sigset_t mask) 37 { 38 mask &= _BLOCKABLE; 39 spin_lock_irq(¤t->sighand->siglock); 40 current->saved_sigmask = current->blocked; 41 siginitset(¤t->blocked, mask); 42 recalc_sigpending(); 43 spin_unlock_irq(¤t->sighand->siglock); 44 45 current->state = TASK_INTERRUPTIBLE; 46 schedule(); 47 set_thread_flag(TIF_RESTORE_SIGMASK); 48 return -ERESTARTNOHAND; 49 } 50 51 asmlinkage int 52 sys_sigaction(int sig, const struct old_sigaction __user *act, 53 struct old_sigaction __user *oact) 54 { 55 struct k_sigaction new_ka, old_ka; 56 int ret; 57 58 if (act) { 59 old_sigset_t mask; 60 if (!access_ok(VERIFY_READ, act, sizeof(*act)) || 61 __get_user(new_ka.sa.sa_handler, &act->sa_handler) || 62 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer)) 63 return -EFAULT; 64 __get_user(new_ka.sa.sa_flags, &act->sa_flags); 65 __get_user(mask, &act->sa_mask); 66 siginitset(&new_ka.sa.sa_mask, mask); 67 } 68 69 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); 70 71 if (!ret && oact) { 72 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || 73 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || 74 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer)) 75 return -EFAULT; 76 __put_user(old_ka.sa.sa_flags, &oact->sa_flags); 77 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask); 78 } 79 80 return ret; 81 } 82 83 asmlinkage int 84 sys_sigaltstack(unsigned long ebx) 85 { 86 /* This is needed to make gcc realize it doesn't own the "struct pt_regs" */ 87 struct pt_regs *regs = (struct pt_regs *)&ebx; 88 const stack_t __user *uss = (const stack_t __user *)ebx; 89 stack_t __user *uoss = (stack_t __user *)regs->ecx; 90 91 return do_sigaltstack(uss, uoss, regs->esp); 92 } 93 94 95 /* 96 * Do a signal return; undo the signal stack. 97 */ 98 99 static int 100 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *peax) 101 { 102 unsigned int err = 0; 103 104 /* Always make any pending restarted system calls return -EINTR */ 105 current_thread_info()->restart_block.fn = do_no_restart_syscall; 106 107 #define COPY(x) err |= __get_user(regs->x, &sc->x) 108 109 #define COPY_SEG(seg) \ 110 { unsigned short tmp; \ 111 err |= __get_user(tmp, &sc->seg); \ 112 regs->x##seg = tmp; } 113 114 #define COPY_SEG_STRICT(seg) \ 115 { unsigned short tmp; \ 116 err |= __get_user(tmp, &sc->seg); \ 117 regs->x##seg = tmp|3; } 118 119 #define GET_SEG(seg) \ 120 { unsigned short tmp; \ 121 err |= __get_user(tmp, &sc->seg); \ 122 loadsegment(seg,tmp); } 123 124 #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_RF | \ 125 X86_EFLAGS_OF | X86_EFLAGS_DF | \ 126 X86_EFLAGS_TF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \ 127 X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF) 128 129 GET_SEG(gs); 130 COPY_SEG(fs); 131 COPY_SEG(es); 132 COPY_SEG(ds); 133 COPY(edi); 134 COPY(esi); 135 COPY(ebp); 136 COPY(esp); 137 COPY(ebx); 138 COPY(edx); 139 COPY(ecx); 140 COPY(eip); 141 COPY_SEG_STRICT(cs); 142 COPY_SEG_STRICT(ss); 143 144 { 145 unsigned int tmpflags; 146 err |= __get_user(tmpflags, &sc->eflags); 147 regs->eflags = (regs->eflags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS); 148 regs->orig_eax = -1; /* disable syscall checks */ 149 } 150 151 { 152 struct _fpstate __user * buf; 153 err |= __get_user(buf, &sc->fpstate); 154 if (buf) { 155 if (!access_ok(VERIFY_READ, buf, sizeof(*buf))) 156 goto badframe; 157 err |= restore_i387(buf); 158 } else { 159 struct task_struct *me = current; 160 if (used_math()) { 161 clear_fpu(me); 162 clear_used_math(); 163 } 164 } 165 } 166 167 err |= __get_user(*peax, &sc->eax); 168 return err; 169 170 badframe: 171 return 1; 172 } 173 174 asmlinkage int sys_sigreturn(unsigned long __unused) 175 { 176 struct pt_regs *regs = (struct pt_regs *) &__unused; 177 struct sigframe __user *frame = (struct sigframe __user *)(regs->esp - 8); 178 sigset_t set; 179 int eax; 180 181 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 182 goto badframe; 183 if (__get_user(set.sig[0], &frame->sc.oldmask) 184 || (_NSIG_WORDS > 1 185 && __copy_from_user(&set.sig[1], &frame->extramask, 186 sizeof(frame->extramask)))) 187 goto badframe; 188 189 sigdelsetmask(&set, ~_BLOCKABLE); 190 spin_lock_irq(¤t->sighand->siglock); 191 current->blocked = set; 192 recalc_sigpending(); 193 spin_unlock_irq(¤t->sighand->siglock); 194 195 if (restore_sigcontext(regs, &frame->sc, &eax)) 196 goto badframe; 197 return eax; 198 199 badframe: 200 if (show_unhandled_signals && printk_ratelimit()) 201 printk("%s%s[%d] bad frame in sigreturn frame:%p eip:%lx" 202 " esp:%lx oeax:%lx\n", 203 current->pid > 1 ? KERN_INFO : KERN_EMERG, 204 current->comm, current->pid, frame, regs->eip, 205 regs->esp, regs->orig_eax); 206 207 force_sig(SIGSEGV, current); 208 return 0; 209 } 210 211 asmlinkage int sys_rt_sigreturn(unsigned long __unused) 212 { 213 struct pt_regs *regs = (struct pt_regs *) &__unused; 214 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->esp - 4); 215 sigset_t set; 216 int eax; 217 218 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 219 goto badframe; 220 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 221 goto badframe; 222 223 sigdelsetmask(&set, ~_BLOCKABLE); 224 spin_lock_irq(¤t->sighand->siglock); 225 current->blocked = set; 226 recalc_sigpending(); 227 spin_unlock_irq(¤t->sighand->siglock); 228 229 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax)) 230 goto badframe; 231 232 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->esp) == -EFAULT) 233 goto badframe; 234 235 return eax; 236 237 badframe: 238 force_sig(SIGSEGV, current); 239 return 0; 240 } 241 242 /* 243 * Set up a signal frame. 244 */ 245 246 static int 247 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate, 248 struct pt_regs *regs, unsigned long mask) 249 { 250 int tmp, err = 0; 251 252 err |= __put_user(regs->xfs, (unsigned int __user *)&sc->fs); 253 savesegment(gs, tmp); 254 err |= __put_user(tmp, (unsigned int __user *)&sc->gs); 255 256 err |= __put_user(regs->xes, (unsigned int __user *)&sc->es); 257 err |= __put_user(regs->xds, (unsigned int __user *)&sc->ds); 258 err |= __put_user(regs->edi, &sc->edi); 259 err |= __put_user(regs->esi, &sc->esi); 260 err |= __put_user(regs->ebp, &sc->ebp); 261 err |= __put_user(regs->esp, &sc->esp); 262 err |= __put_user(regs->ebx, &sc->ebx); 263 err |= __put_user(regs->edx, &sc->edx); 264 err |= __put_user(regs->ecx, &sc->ecx); 265 err |= __put_user(regs->eax, &sc->eax); 266 err |= __put_user(current->thread.trap_no, &sc->trapno); 267 err |= __put_user(current->thread.error_code, &sc->err); 268 err |= __put_user(regs->eip, &sc->eip); 269 err |= __put_user(regs->xcs, (unsigned int __user *)&sc->cs); 270 err |= __put_user(regs->eflags, &sc->eflags); 271 err |= __put_user(regs->esp, &sc->esp_at_signal); 272 err |= __put_user(regs->xss, (unsigned int __user *)&sc->ss); 273 274 tmp = save_i387(fpstate); 275 if (tmp < 0) 276 err = 1; 277 else 278 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate); 279 280 /* non-iBCS2 extensions.. */ 281 err |= __put_user(mask, &sc->oldmask); 282 err |= __put_user(current->thread.cr2, &sc->cr2); 283 284 return err; 285 } 286 287 /* 288 * Determine which stack to use.. 289 */ 290 static inline void __user * 291 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 292 { 293 unsigned long esp; 294 295 /* Default to using normal stack */ 296 esp = regs->esp; 297 298 /* This is the X/Open sanctioned signal stack switching. */ 299 if (ka->sa.sa_flags & SA_ONSTACK) { 300 if (sas_ss_flags(esp) == 0) 301 esp = current->sas_ss_sp + current->sas_ss_size; 302 } 303 304 /* This is the legacy signal stack switching. */ 305 else if ((regs->xss & 0xffff) != __USER_DS && 306 !(ka->sa.sa_flags & SA_RESTORER) && 307 ka->sa.sa_restorer) { 308 esp = (unsigned long) ka->sa.sa_restorer; 309 } 310 311 esp -= frame_size; 312 /* Align the stack pointer according to the i386 ABI, 313 * i.e. so that on function entry ((sp + 4) & 15) == 0. */ 314 esp = ((esp + 4) & -16ul) - 4; 315 return (void __user *) esp; 316 } 317 318 /* These symbols are defined with the addresses in the vsyscall page. 319 See vsyscall-sigreturn.S. */ 320 extern void __user __kernel_sigreturn; 321 extern void __user __kernel_rt_sigreturn; 322 323 static int setup_frame(int sig, struct k_sigaction *ka, 324 sigset_t *set, struct pt_regs * regs) 325 { 326 void __user *restorer; 327 struct sigframe __user *frame; 328 int err = 0; 329 int usig; 330 331 frame = get_sigframe(ka, regs, sizeof(*frame)); 332 333 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 334 goto give_sigsegv; 335 336 usig = current_thread_info()->exec_domain 337 && current_thread_info()->exec_domain->signal_invmap 338 && sig < 32 339 ? current_thread_info()->exec_domain->signal_invmap[sig] 340 : sig; 341 342 err = __put_user(usig, &frame->sig); 343 if (err) 344 goto give_sigsegv; 345 346 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]); 347 if (err) 348 goto give_sigsegv; 349 350 if (_NSIG_WORDS > 1) { 351 err = __copy_to_user(&frame->extramask, &set->sig[1], 352 sizeof(frame->extramask)); 353 if (err) 354 goto give_sigsegv; 355 } 356 357 if (current->binfmt->hasvdso) 358 restorer = (void *)VDSO_SYM(&__kernel_sigreturn); 359 else 360 restorer = (void *)&frame->retcode; 361 if (ka->sa.sa_flags & SA_RESTORER) 362 restorer = ka->sa.sa_restorer; 363 364 /* Set up to return from userspace. */ 365 err |= __put_user(restorer, &frame->pretcode); 366 367 /* 368 * This is popl %eax ; movl $,%eax ; int $0x80 369 * 370 * WE DO NOT USE IT ANY MORE! It's only left here for historical 371 * reasons and because gdb uses it as a signature to notice 372 * signal handler stack frames. 373 */ 374 err |= __put_user(0xb858, (short __user *)(frame->retcode+0)); 375 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2)); 376 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6)); 377 378 if (err) 379 goto give_sigsegv; 380 381 /* Set up registers for signal handler */ 382 regs->esp = (unsigned long) frame; 383 regs->eip = (unsigned long) ka->sa.sa_handler; 384 regs->eax = (unsigned long) sig; 385 regs->edx = (unsigned long) 0; 386 regs->ecx = (unsigned long) 0; 387 388 set_fs(USER_DS); 389 regs->xds = __USER_DS; 390 regs->xes = __USER_DS; 391 regs->xss = __USER_DS; 392 regs->xcs = __USER_CS; 393 394 /* 395 * Clear TF when entering the signal handler, but 396 * notify any tracer that was single-stepping it. 397 * The tracer may want to single-step inside the 398 * handler too. 399 */ 400 regs->eflags &= ~TF_MASK; 401 if (test_thread_flag(TIF_SINGLESTEP)) 402 ptrace_notify(SIGTRAP); 403 404 #if DEBUG_SIG 405 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n", 406 current->comm, current->pid, frame, regs->eip, frame->pretcode); 407 #endif 408 409 return 0; 410 411 give_sigsegv: 412 force_sigsegv(sig, current); 413 return -EFAULT; 414 } 415 416 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 417 sigset_t *set, struct pt_regs * regs) 418 { 419 void __user *restorer; 420 struct rt_sigframe __user *frame; 421 int err = 0; 422 int usig; 423 424 frame = get_sigframe(ka, regs, sizeof(*frame)); 425 426 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 427 goto give_sigsegv; 428 429 usig = current_thread_info()->exec_domain 430 && current_thread_info()->exec_domain->signal_invmap 431 && sig < 32 432 ? current_thread_info()->exec_domain->signal_invmap[sig] 433 : sig; 434 435 err |= __put_user(usig, &frame->sig); 436 err |= __put_user(&frame->info, &frame->pinfo); 437 err |= __put_user(&frame->uc, &frame->puc); 438 err |= copy_siginfo_to_user(&frame->info, info); 439 if (err) 440 goto give_sigsegv; 441 442 /* Create the ucontext. */ 443 err |= __put_user(0, &frame->uc.uc_flags); 444 err |= __put_user(0, &frame->uc.uc_link); 445 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); 446 err |= __put_user(sas_ss_flags(regs->esp), 447 &frame->uc.uc_stack.ss_flags); 448 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); 449 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate, 450 regs, set->sig[0]); 451 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 452 if (err) 453 goto give_sigsegv; 454 455 /* Set up to return from userspace. */ 456 restorer = (void *)VDSO_SYM(&__kernel_rt_sigreturn); 457 if (ka->sa.sa_flags & SA_RESTORER) 458 restorer = ka->sa.sa_restorer; 459 err |= __put_user(restorer, &frame->pretcode); 460 461 /* 462 * This is movl $,%eax ; int $0x80 463 * 464 * WE DO NOT USE IT ANY MORE! It's only left here for historical 465 * reasons and because gdb uses it as a signature to notice 466 * signal handler stack frames. 467 */ 468 err |= __put_user(0xb8, (char __user *)(frame->retcode+0)); 469 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1)); 470 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5)); 471 472 if (err) 473 goto give_sigsegv; 474 475 /* Set up registers for signal handler */ 476 regs->esp = (unsigned long) frame; 477 regs->eip = (unsigned long) ka->sa.sa_handler; 478 regs->eax = (unsigned long) usig; 479 regs->edx = (unsigned long) &frame->info; 480 regs->ecx = (unsigned long) &frame->uc; 481 482 set_fs(USER_DS); 483 regs->xds = __USER_DS; 484 regs->xes = __USER_DS; 485 regs->xss = __USER_DS; 486 regs->xcs = __USER_CS; 487 488 /* 489 * Clear TF when entering the signal handler, but 490 * notify any tracer that was single-stepping it. 491 * The tracer may want to single-step inside the 492 * handler too. 493 */ 494 regs->eflags &= ~TF_MASK; 495 if (test_thread_flag(TIF_SINGLESTEP)) 496 ptrace_notify(SIGTRAP); 497 498 #if DEBUG_SIG 499 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n", 500 current->comm, current->pid, frame, regs->eip, frame->pretcode); 501 #endif 502 503 return 0; 504 505 give_sigsegv: 506 force_sigsegv(sig, current); 507 return -EFAULT; 508 } 509 510 /* 511 * OK, we're invoking a handler 512 */ 513 514 static int 515 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, 516 sigset_t *oldset, struct pt_regs * regs) 517 { 518 int ret; 519 520 /* Are we from a system call? */ 521 if (regs->orig_eax >= 0) { 522 /* If so, check system call restarting.. */ 523 switch (regs->eax) { 524 case -ERESTART_RESTARTBLOCK: 525 case -ERESTARTNOHAND: 526 regs->eax = -EINTR; 527 break; 528 529 case -ERESTARTSYS: 530 if (!(ka->sa.sa_flags & SA_RESTART)) { 531 regs->eax = -EINTR; 532 break; 533 } 534 /* fallthrough */ 535 case -ERESTARTNOINTR: 536 regs->eax = regs->orig_eax; 537 regs->eip -= 2; 538 } 539 } 540 541 /* 542 * If TF is set due to a debugger (PT_DTRACE), clear the TF flag so 543 * that register information in the sigcontext is correct. 544 */ 545 if (unlikely(regs->eflags & TF_MASK) 546 && likely(current->ptrace & PT_DTRACE)) { 547 current->ptrace &= ~PT_DTRACE; 548 regs->eflags &= ~TF_MASK; 549 } 550 551 /* Set up the stack frame */ 552 if (ka->sa.sa_flags & SA_SIGINFO) 553 ret = setup_rt_frame(sig, ka, info, oldset, regs); 554 else 555 ret = setup_frame(sig, ka, oldset, regs); 556 557 if (ret == 0) { 558 spin_lock_irq(¤t->sighand->siglock); 559 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask); 560 if (!(ka->sa.sa_flags & SA_NODEFER)) 561 sigaddset(¤t->blocked,sig); 562 recalc_sigpending(); 563 spin_unlock_irq(¤t->sighand->siglock); 564 } 565 566 return ret; 567 } 568 569 /* 570 * Note that 'init' is a special process: it doesn't get signals it doesn't 571 * want to handle. Thus you cannot kill init even with a SIGKILL even by 572 * mistake. 573 */ 574 static void fastcall do_signal(struct pt_regs *regs) 575 { 576 siginfo_t info; 577 int signr; 578 struct k_sigaction ka; 579 sigset_t *oldset; 580 581 /* 582 * We want the common case to go fast, which 583 * is why we may in certain cases get here from 584 * kernel mode. Just return without doing anything 585 * if so. vm86 regs switched out by assembly code 586 * before reaching here, so testing against kernel 587 * CS suffices. 588 */ 589 if (!user_mode(regs)) 590 return; 591 592 if (test_thread_flag(TIF_RESTORE_SIGMASK)) 593 oldset = ¤t->saved_sigmask; 594 else 595 oldset = ¤t->blocked; 596 597 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 598 if (signr > 0) { 599 /* Reenable any watchpoints before delivering the 600 * signal to user space. The processor register will 601 * have been cleared if the watchpoint triggered 602 * inside the kernel. 603 */ 604 if (unlikely(current->thread.debugreg[7])) 605 set_debugreg(current->thread.debugreg[7], 7); 606 607 /* Whee! Actually deliver the signal. */ 608 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) { 609 /* a signal was successfully delivered; the saved 610 * sigmask will have been stored in the signal frame, 611 * and will be restored by sigreturn, so we can simply 612 * clear the TIF_RESTORE_SIGMASK flag */ 613 if (test_thread_flag(TIF_RESTORE_SIGMASK)) 614 clear_thread_flag(TIF_RESTORE_SIGMASK); 615 } 616 617 return; 618 } 619 620 /* Did we come from a system call? */ 621 if (regs->orig_eax >= 0) { 622 /* Restart the system call - no handlers present */ 623 switch (regs->eax) { 624 case -ERESTARTNOHAND: 625 case -ERESTARTSYS: 626 case -ERESTARTNOINTR: 627 regs->eax = regs->orig_eax; 628 regs->eip -= 2; 629 break; 630 631 case -ERESTART_RESTARTBLOCK: 632 regs->eax = __NR_restart_syscall; 633 regs->eip -= 2; 634 break; 635 } 636 } 637 638 /* if there's no signal to deliver, we just put the saved sigmask 639 * back */ 640 if (test_thread_flag(TIF_RESTORE_SIGMASK)) { 641 clear_thread_flag(TIF_RESTORE_SIGMASK); 642 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); 643 } 644 } 645 646 /* 647 * notification of userspace execution resumption 648 * - triggered by the TIF_WORK_MASK flags 649 */ 650 __attribute__((regparm(3))) 651 void do_notify_resume(struct pt_regs *regs, void *_unused, 652 __u32 thread_info_flags) 653 { 654 /* Pending single-step? */ 655 if (thread_info_flags & _TIF_SINGLESTEP) { 656 regs->eflags |= TF_MASK; 657 clear_thread_flag(TIF_SINGLESTEP); 658 } 659 660 /* deal with pending signal delivery */ 661 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK)) 662 do_signal(regs); 663 664 clear_thread_flag(TIF_IRET); 665 } 666