blob: 7b0e0e81c16196d7244819316a2b0cf725f1e62e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "kern.h"
26#include "frame_kern.h"
27#include "sigcontext.h"
28#include "mode.h"
29
30EXPORT_SYMBOL(block_signals);
31EXPORT_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 */
40static 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(&current->sighand->siglock);
85 current->blocked = *oldset;
86 recalc_sigpending();
87 spin_unlock_irq(&current->sighand->siglock);
88 force_sigsegv(signr, current);
Steven Rostedt69be8f12005-08-29 11:44:09 -040089 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_lock_irq(&current->sighand->siglock);
91 sigorsets(&current->blocked, &current->blocked,
92 &ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -040093 if(!(ka->sa.sa_flags & SA_NODEFER))
94 sigaddset(&current->blocked, signr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 recalc_sigpending();
96 spin_unlock_irq(&current->sighand->siglock);
97 }
98
99 return err;
100}
101
102static int kern_do_signal(struct pt_regs *regs, sigset_t *oldset)
103{
104 struct k_sigaction ka_copy;
105 siginfo_t info;
106 int sig, handled_sig = 0;
107
108 while((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0){
109 handled_sig = 1;
110 /* Whee! Actually deliver the signal. */
111 if(!handle_signal(regs, sig, &ka_copy, &info, oldset))
112 break;
113 }
114
115 /* Did we come from a system call? */
116 if(!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)){
117 /* Restart the system call - no handlers present */
118 if(PT_REGS_SYSCALL_RET(regs) == -ERESTARTNOHAND ||
119 PT_REGS_SYSCALL_RET(regs) == -ERESTARTSYS ||
120 PT_REGS_SYSCALL_RET(regs) == -ERESTARTNOINTR){
121 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
122 PT_REGS_RESTART_SYSCALL(regs);
123 }
124 else if(PT_REGS_SYSCALL_RET(regs) == -ERESTART_RESTARTBLOCK){
125 PT_REGS_SYSCALL_RET(regs) = __NR_restart_syscall;
126 PT_REGS_RESTART_SYSCALL(regs);
127 }
128 }
129
130 /* This closes a way to execute a system call on the host. If
131 * you set a breakpoint on a system call instruction and singlestep
132 * from it, the tracing thread used to PTRACE_SINGLESTEP the process
133 * rather than PTRACE_SYSCALL it, allowing the system call to execute
134 * on the host. The tracing thread will check this flag and
135 * PTRACE_SYSCALL if necessary.
136 */
137 if(current->ptrace & PT_DTRACE)
138 current->thread.singlestep_syscall =
139 is_syscall(PT_REGS_IP(&current->thread.regs));
140 return(handled_sig);
141}
142
143int do_signal(void)
144{
145 return(kern_do_signal(&current->thread.regs, &current->blocked));
146}
147
148/*
149 * Atomically swap in the new signal mask, and wait for a signal.
150 */
151long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
152{
153 sigset_t saveset;
154
155 mask &= _BLOCKABLE;
156 spin_lock_irq(&current->sighand->siglock);
157 saveset = current->blocked;
158 siginitset(&current->blocked, mask);
159 recalc_sigpending();
160 spin_unlock_irq(&current->sighand->siglock);
161
162 PT_REGS_SYSCALL_RET(&current->thread.regs) = -EINTR;
163 while (1) {
164 current->state = TASK_INTERRUPTIBLE;
165 schedule();
166 if(kern_do_signal(&current->thread.regs, &saveset))
167 return(-EINTR);
168 }
169}
170
171long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
172{
173 sigset_t saveset, newset;
174
175 /* XXX: Don't preclude handling different sized sigset_t's. */
176 if (sigsetsize != sizeof(sigset_t))
177 return -EINVAL;
178
179 if (copy_from_user(&newset, unewset, sizeof(newset)))
180 return -EFAULT;
181 sigdelsetmask(&newset, ~_BLOCKABLE);
182
183 spin_lock_irq(&current->sighand->siglock);
184 saveset = current->blocked;
185 current->blocked = newset;
186 recalc_sigpending();
187 spin_unlock_irq(&current->sighand->siglock);
188
189 PT_REGS_SYSCALL_RET(&current->thread.regs) = -EINTR;
190 while (1) {
191 current->state = TASK_INTERRUPTIBLE;
192 schedule();
193 if (kern_do_signal(&current->thread.regs, &saveset))
194 return(-EINTR);
195 }
196}
197
198long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
199{
200 return(do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs)));
201}
202
203/*
204 * Overrides for Emacs so that we follow Linus's tabbing style.
205 * Emacs will notice this stuff at the end of the file and automatically
206 * adjust the settings for this buffer only. This must remain at the end
207 * of the file.
208 * ---------------------------------------------------------------------------
209 * Local variables:
210 * c-file-style: "linux"
211 * End:
212 */