signal.c 4.8 KB
Newer Older
J
Jeff Dike 已提交
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6 7
 * Licensed under the GPL
 */

#include "linux/module.h"
#include "linux/ptrace.h"
J
Jeff Dike 已提交
8 9
#include "linux/sched.h"
#include "asm/siginfo.h"
L
Linus Torvalds 已提交
10 11 12
#include "asm/signal.h"
#include "asm/unistd.h"
#include "frame_kern.h"
J
Jeff Dike 已提交
13
#include "kern_util.h"
L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23 24
#include "sigcontext.h"

EXPORT_SYMBOL(block_signals);
EXPORT_SYMBOL(unblock_signals);

#define _S(nr) (1<<((nr)-1))

#define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))

/*
 * OK, we're invoking a handler
J
Jeff Dike 已提交
25
 */
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35 36
static int handle_signal(struct pt_regs *regs, unsigned long signr,
			 struct k_sigaction *ka, siginfo_t *info,
			 sigset_t *oldset)
{
	unsigned long sp;
	int err;

	/* Always make any pending restarted system calls return -EINTR */
	current_thread_info()->restart_block.fn = do_no_restart_syscall;

	/* Did we come from a system call? */
J
Jeff Dike 已提交
37
	if (PT_REGS_SYSCALL_NR(regs) >= 0) {
L
Linus Torvalds 已提交
38
		/* If so, check system call restarting.. */
J
Jeff Dike 已提交
39
		switch(PT_REGS_SYSCALL_RET(regs)) {
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
		case -ERESTART_RESTARTBLOCK:
		case -ERESTARTNOHAND:
			PT_REGS_SYSCALL_RET(regs) = -EINTR;
			break;

		case -ERESTARTSYS:
			if (!(ka->sa.sa_flags & SA_RESTART)) {
				PT_REGS_SYSCALL_RET(regs) = -EINTR;
				break;
			}
		/* fallthrough */
		case -ERESTARTNOINTR:
			PT_REGS_RESTART_SYSCALL(regs);
			PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
			break;
		}
	}

	sp = PT_REGS_SP(regs);
J
Jeff Dike 已提交
59
	if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
L
Linus Torvalds 已提交
60 61 62
		sp = current->sas_ss_sp + current->sas_ss_size;

#ifdef CONFIG_ARCH_HAS_SC_SIGNALS
J
Jeff Dike 已提交
63
	if (!(ka->sa.sa_flags & SA_SIGINFO))
L
Linus Torvalds 已提交
64 65 66 67 68
		err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
	else
#endif
		err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);

J
Jeff Dike 已提交
69
	if (err) {
L
Linus Torvalds 已提交
70 71 72 73 74
		spin_lock_irq(&current->sighand->siglock);
		current->blocked = *oldset;
		recalc_sigpending();
		spin_unlock_irq(&current->sighand->siglock);
		force_sigsegv(signr, current);
75
	} else {
L
Linus Torvalds 已提交
76
		spin_lock_irq(&current->sighand->siglock);
J
Jeff Dike 已提交
77
		sigorsets(&current->blocked, &current->blocked,
L
Linus Torvalds 已提交
78
			  &ka->sa.sa_mask);
J
Jeff Dike 已提交
79
		if (!(ka->sa.sa_flags & SA_NODEFER))
80
			sigaddset(&current->blocked, signr);
L
Linus Torvalds 已提交
81 82 83 84 85 86 87
		recalc_sigpending();
		spin_unlock_irq(&current->sighand->siglock);
	}

	return err;
}

88
static int kern_do_signal(struct pt_regs *regs)
L
Linus Torvalds 已提交
89 90 91
{
	struct k_sigaction ka_copy;
	siginfo_t info;
92
	sigset_t *oldset;
L
Linus Torvalds 已提交
93 94
	int sig, handled_sig = 0;

95 96 97 98 99
	if (test_thread_flag(TIF_RESTORE_SIGMASK))
		oldset = &current->saved_sigmask;
	else
		oldset = &current->blocked;

J
Jeff Dike 已提交
100
	while ((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0) {
L
Linus Torvalds 已提交
101 102
		handled_sig = 1;
		/* Whee!  Actually deliver the signal.  */
J
Jeff Dike 已提交
103 104 105
		if (!handle_signal(regs, sig, &ka_copy, &info, oldset)) {
			/*
			 * a signal was successfully delivered; the saved
106 107
			 * sigmask will have been stored in the signal frame,
			 * and will be restored by sigreturn, so we can simply
J
Jeff Dike 已提交
108 109
			 * clear the TIF_RESTORE_SIGMASK flag
			 */
110 111
			if (test_thread_flag(TIF_RESTORE_SIGMASK))
				clear_thread_flag(TIF_RESTORE_SIGMASK);
L
Linus Torvalds 已提交
112
			break;
113
		}
L
Linus Torvalds 已提交
114 115 116
	}

	/* Did we come from a system call? */
J
Jeff Dike 已提交
117
	if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
L
Linus Torvalds 已提交
118
		/* Restart the system call - no handlers present */
J
Jeff Dike 已提交
119
		switch(PT_REGS_SYSCALL_RET(regs)) {
120 121 122
		case -ERESTARTNOHAND:
		case -ERESTARTSYS:
		case -ERESTARTNOINTR:
L
Linus Torvalds 已提交
123 124
			PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
			PT_REGS_RESTART_SYSCALL(regs);
125 126
			break;
		case -ERESTART_RESTARTBLOCK:
J
Jeff Dike 已提交
127
			PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
L
Linus Torvalds 已提交
128
			PT_REGS_RESTART_SYSCALL(regs);
129
			break;
J
Jeff Dike 已提交
130
		}
L
Linus Torvalds 已提交
131 132
	}

J
Jeff Dike 已提交
133 134
	/*
	 * This closes a way to execute a system call on the host.  If
L
Linus Torvalds 已提交
135 136 137
	 * you set a breakpoint on a system call instruction and singlestep
	 * from it, the tracing thread used to PTRACE_SINGLESTEP the process
	 * rather than PTRACE_SYSCALL it, allowing the system call to execute
J
Jeff Dike 已提交
138
	 * on the host.  The tracing thread will check this flag and
L
Linus Torvalds 已提交
139 140
	 * PTRACE_SYSCALL if necessary.
	 */
J
Jeff Dike 已提交
141
	if (current->ptrace & PT_DTRACE)
L
Linus Torvalds 已提交
142 143
		current->thread.singlestep_syscall =
			is_syscall(PT_REGS_IP(&current->thread.regs));
144

J
Jeff Dike 已提交
145 146 147 148
	/*
	 * if there's no signal to deliver, we just put the saved sigmask
	 * back
	 */
149 150 151 152
	if (!handled_sig && test_thread_flag(TIF_RESTORE_SIGMASK)) {
		clear_thread_flag(TIF_RESTORE_SIGMASK);
		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
	}
153
	return handled_sig;
L
Linus Torvalds 已提交
154 155 156 157
}

int do_signal(void)
{
158
	return kern_do_signal(&current->thread.regs);
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167
}

/*
 * Atomically swap in the new signal mask, and wait for a signal.
 */
long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
{
	mask &= _BLOCKABLE;
	spin_lock_irq(&current->sighand->siglock);
168
	current->saved_sigmask = current->blocked;
L
Linus Torvalds 已提交
169 170 171 172
	siginitset(&current->blocked, mask);
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);

173 174 175 176
	current->state = TASK_INTERRUPTIBLE;
	schedule();
	set_thread_flag(TIF_RESTORE_SIGMASK);
	return -ERESTARTNOHAND;
L
Linus Torvalds 已提交
177 178 179 180
}

long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
{
181
	return do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs));
L
Linus Torvalds 已提交
182
}