1 /* 2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) 3 * Licensed under the GPL 4 */ 5 6 #include "linux/config.h" 7 #include "linux/stddef.h" 8 #include "linux/sys.h" 9 #include "linux/sched.h" 10 #include "linux/wait.h" 11 #include "linux/kernel.h" 12 #include "linux/smp_lock.h" 13 #include "linux/module.h" 14 #include "linux/slab.h" 15 #include "linux/tty.h" 16 #include "linux/binfmts.h" 17 #include "linux/ptrace.h" 18 #include "asm/signal.h" 19 #include "asm/uaccess.h" 20 #include "asm/unistd.h" 21 #include "user_util.h" 22 #include "asm/ucontext.h" 23 #include "kern_util.h" 24 #include "signal_kern.h" 25 #include "kern.h" 26 #include "frame_kern.h" 27 #include "sigcontext.h" 28 #include "mode.h" 29 30 EXPORT_SYMBOL(block_signals); 31 EXPORT_SYMBOL(unblock_signals); 32 33 #define _S(nr) (1<<((nr)-1)) 34 35 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP))) 36 37 /* 38 * OK, we're invoking a handler 39 */ 40 static int handle_signal(struct pt_regs *regs, unsigned long signr, 41 struct k_sigaction *ka, siginfo_t *info, 42 sigset_t *oldset) 43 { 44 unsigned long sp; 45 int err; 46 47 /* Always make any pending restarted system calls return -EINTR */ 48 current_thread_info()->restart_block.fn = do_no_restart_syscall; 49 50 /* Did we come from a system call? */ 51 if(PT_REGS_SYSCALL_NR(regs) >= 0){ 52 /* If so, check system call restarting.. */ 53 switch(PT_REGS_SYSCALL_RET(regs)){ 54 case -ERESTART_RESTARTBLOCK: 55 case -ERESTARTNOHAND: 56 PT_REGS_SYSCALL_RET(regs) = -EINTR; 57 break; 58 59 case -ERESTARTSYS: 60 if (!(ka->sa.sa_flags & SA_RESTART)) { 61 PT_REGS_SYSCALL_RET(regs) = -EINTR; 62 break; 63 } 64 /* fallthrough */ 65 case -ERESTARTNOINTR: 66 PT_REGS_RESTART_SYSCALL(regs); 67 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs); 68 break; 69 } 70 } 71 72 sp = PT_REGS_SP(regs); 73 if((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0)) 74 sp = current->sas_ss_sp + current->sas_ss_size; 75 76 #ifdef CONFIG_ARCH_HAS_SC_SIGNALS 77 if(!(ka->sa.sa_flags & SA_SIGINFO)) 78 err = setup_signal_stack_sc(sp, signr, ka, regs, oldset); 79 else 80 #endif 81 err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset); 82 83 if(err){ 84 spin_lock_irq(¤t->sighand->siglock); 85 current->blocked = *oldset; 86 recalc_sigpending(); 87 spin_unlock_irq(¤t->sighand->siglock); 88 force_sigsegv(signr, current); 89 } else { 90 spin_lock_irq(¤t->sighand->siglock); 91 sigorsets(¤t->blocked, ¤t->blocked, 92 &ka->sa.sa_mask); 93 if(!(ka->sa.sa_flags & SA_NODEFER)) 94 sigaddset(¤t->blocked, signr); 95 recalc_sigpending(); 96 spin_unlock_irq(¤t->sighand->siglock); 97 } 98 99 return err; 100 } 101 102 static int kern_do_signal(struct pt_regs *regs) 103 { 104 struct k_sigaction ka_copy; 105 siginfo_t info; 106 sigset_t *oldset; 107 int sig, handled_sig = 0; 108 109 if (test_thread_flag(TIF_RESTORE_SIGMASK)) 110 oldset = ¤t->saved_sigmask; 111 else 112 oldset = ¤t->blocked; 113 114 while((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0){ 115 handled_sig = 1; 116 /* Whee! Actually deliver the signal. */ 117 if(!handle_signal(regs, sig, &ka_copy, &info, oldset)){ 118 /* a signal was successfully delivered; the saved 119 * sigmask will have been stored in the signal frame, 120 * and will be restored by sigreturn, so we can simply 121 * clear the TIF_RESTORE_SIGMASK flag */ 122 if (test_thread_flag(TIF_RESTORE_SIGMASK)) 123 clear_thread_flag(TIF_RESTORE_SIGMASK); 124 break; 125 } 126 } 127 128 /* Did we come from a system call? */ 129 if(!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)){ 130 /* Restart the system call - no handlers present */ 131 switch(PT_REGS_SYSCALL_RET(regs)){ 132 case -ERESTARTNOHAND: 133 case -ERESTARTSYS: 134 case -ERESTARTNOINTR: 135 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs); 136 PT_REGS_RESTART_SYSCALL(regs); 137 break; 138 case -ERESTART_RESTARTBLOCK: 139 PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall; 140 PT_REGS_RESTART_SYSCALL(regs); 141 break; 142 } 143 } 144 145 /* This closes a way to execute a system call on the host. If 146 * you set a breakpoint on a system call instruction and singlestep 147 * from it, the tracing thread used to PTRACE_SINGLESTEP the process 148 * rather than PTRACE_SYSCALL it, allowing the system call to execute 149 * on the host. The tracing thread will check this flag and 150 * PTRACE_SYSCALL if necessary. 151 */ 152 if(current->ptrace & PT_DTRACE) 153 current->thread.singlestep_syscall = 154 is_syscall(PT_REGS_IP(¤t->thread.regs)); 155 156 /* if there's no signal to deliver, we just put the saved sigmask 157 * back */ 158 if (!handled_sig && test_thread_flag(TIF_RESTORE_SIGMASK)) { 159 clear_thread_flag(TIF_RESTORE_SIGMASK); 160 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); 161 } 162 return(handled_sig); 163 } 164 165 int do_signal(void) 166 { 167 return(kern_do_signal(¤t->thread.regs)); 168 } 169 170 /* 171 * Atomically swap in the new signal mask, and wait for a signal. 172 */ 173 long sys_sigsuspend(int history0, int history1, old_sigset_t mask) 174 { 175 mask &= _BLOCKABLE; 176 spin_lock_irq(¤t->sighand->siglock); 177 current->saved_sigmask = current->blocked; 178 siginitset(¤t->blocked, mask); 179 recalc_sigpending(); 180 spin_unlock_irq(¤t->sighand->siglock); 181 182 current->state = TASK_INTERRUPTIBLE; 183 schedule(); 184 set_thread_flag(TIF_RESTORE_SIGMASK); 185 return -ERESTARTNOHAND; 186 } 187 188 long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss) 189 { 190 return(do_sigaltstack(uss, uoss, PT_REGS_SP(¤t->thread.regs))); 191 } 192