signal.c 3.4 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
 * Licensed under the GPL
 */

J
Jeff Dike 已提交
6 7 8 9 10 11
#include <linux/module.h>
#include <linux/ptrace.h>
#include <linux/sched.h>
#include <asm/siginfo.h>
#include <asm/signal.h>
#include <asm/unistd.h>
12 13
#include <frame_kern.h>
#include <kern_util.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19

EXPORT_SYMBOL(block_signals);
EXPORT_SYMBOL(unblock_signals);

/*
 * OK, we're invoking a handler
J
Jeff Dike 已提交
20
 */
21
static void handle_signal(struct pt_regs *regs, unsigned long signr,
A
Al Viro 已提交
22
			 struct k_sigaction *ka, siginfo_t *info)
L
Linus Torvalds 已提交
23
{
A
Al Viro 已提交
24
	sigset_t *oldset = sigmask_to_save();
25
	int singlestep = 0;
L
Linus Torvalds 已提交
26 27 28
	unsigned long sp;
	int err;

29 30 31
	if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
		singlestep = 1;

L
Linus Torvalds 已提交
32
	/* Did we come from a system call? */
J
Jeff Dike 已提交
33
	if (PT_REGS_SYSCALL_NR(regs) >= 0) {
L
Linus Torvalds 已提交
34
		/* If so, check system call restarting.. */
J
Jeff Dike 已提交
35
		switch (PT_REGS_SYSCALL_RET(regs)) {
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
		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 已提交
55
	if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
L
Linus Torvalds 已提交
56 57 58
		sp = current->sas_ss_sp + current->sas_ss_size;

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

65
	if (err)
L
Linus Torvalds 已提交
66
		force_sigsegv(signr, current);
67
	else
68
		signal_delivered(signr, info, ka, regs, singlestep);
L
Linus Torvalds 已提交
69 70
}

71
static int kern_do_signal(struct pt_regs *regs)
L
Linus Torvalds 已提交
72 73 74 75 76
{
	struct k_sigaction ka_copy;
	siginfo_t info;
	int sig, handled_sig = 0;

J
Jeff Dike 已提交
77
	while ((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0) {
L
Linus Torvalds 已提交
78 79
		handled_sig = 1;
		/* Whee!  Actually deliver the signal.  */
80
		handle_signal(regs, sig, &ka_copy, &info);
L
Linus Torvalds 已提交
81 82 83
	}

	/* Did we come from a system call? */
J
Jeff Dike 已提交
84
	if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
L
Linus Torvalds 已提交
85
		/* Restart the system call - no handlers present */
J
Jeff Dike 已提交
86
		switch (PT_REGS_SYSCALL_RET(regs)) {
87 88 89
		case -ERESTARTNOHAND:
		case -ERESTARTSYS:
		case -ERESTARTNOINTR:
L
Linus Torvalds 已提交
90 91
			PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
			PT_REGS_RESTART_SYSCALL(regs);
92 93
			break;
		case -ERESTART_RESTARTBLOCK:
J
Jeff Dike 已提交
94
			PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
L
Linus Torvalds 已提交
95
			PT_REGS_RESTART_SYSCALL(regs);
96
			break;
J
Jeff Dike 已提交
97
		}
L
Linus Torvalds 已提交
98 99
	}

J
Jeff Dike 已提交
100 101
	/*
	 * This closes a way to execute a system call on the host.  If
L
Linus Torvalds 已提交
102 103 104
	 * 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 已提交
105
	 * on the host.  The tracing thread will check this flag and
L
Linus Torvalds 已提交
106 107
	 * PTRACE_SYSCALL if necessary.
	 */
J
Jeff Dike 已提交
108
	if (current->ptrace & PT_DTRACE)
L
Linus Torvalds 已提交
109 110
		current->thread.singlestep_syscall =
			is_syscall(PT_REGS_IP(&current->thread.regs));
111

J
Jeff Dike 已提交
112 113 114 115
	/*
	 * if there's no signal to deliver, we just put the saved sigmask
	 * back
	 */
A
Al Viro 已提交
116 117
	if (!handled_sig)
		restore_saved_sigmask();
118
	return handled_sig;
L
Linus Torvalds 已提交
119 120 121 122
}

int do_signal(void)
{
123
	return kern_do_signal(&current->thread.regs);
L
Linus Torvalds 已提交
124 125 126 127 128 129 130
}

/*
 * Atomically swap in the new signal mask, and wait for a signal.
 */
long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
{
131 132
	sigset_t blocked;
	siginitset(&blocked, mask);
A
Al Viro 已提交
133
	return sigsuspend(&blocked);
L
Linus Torvalds 已提交
134
}