2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/config.h"
7 #include "linux/stddef.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 "signal_user.h"
27 #include "frame_kern.h"
28 #include "sigcontext.h"
31 EXPORT_SYMBOL(block_signals);
32 EXPORT_SYMBOL(unblock_signals);
34 #define _S(nr) (1<<((nr)-1))
36 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
39 * OK, we're invoking a handler
41 static int handle_signal(struct pt_regs *regs, unsigned long signr,
42 struct k_sigaction *ka, siginfo_t *info,
48 /* Always make any pending restarted system calls return -EINTR */
49 current_thread_info()->restart_block.fn = do_no_restart_syscall;
51 /* Did we come from a system call? */
52 if(PT_REGS_SYSCALL_NR(regs) >= 0){
53 /* If so, check system call restarting.. */
54 switch(PT_REGS_SYSCALL_RET(regs)){
55 case -ERESTART_RESTARTBLOCK:
57 PT_REGS_SYSCALL_RET(regs) = -EINTR;
61 if (!(ka->sa.sa_flags & SA_RESTART)) {
62 PT_REGS_SYSCALL_RET(regs) = -EINTR;
67 PT_REGS_RESTART_SYSCALL(regs);
68 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
73 sp = PT_REGS_SP(regs);
74 if((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
75 sp = current->sas_ss_sp + current->sas_ss_size;
77 if(ka->sa.sa_flags & SA_SIGINFO)
78 err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
80 err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
83 spin_lock_irq(¤t->sighand->siglock);
84 current->blocked = *oldset;
86 spin_unlock_irq(¤t->sighand->siglock);
87 force_sigsegv(signr, current);
89 else if(!(ka->sa.sa_flags & SA_NODEFER)){
90 spin_lock_irq(¤t->sighand->siglock);
91 sigorsets(¤t->blocked, ¤t->blocked,
93 sigaddset(¤t->blocked, signr);
95 spin_unlock_irq(¤t->sighand->siglock);
101 static int kern_do_signal(struct pt_regs *regs, sigset_t *oldset)
103 struct k_sigaction ka_copy;
105 int sig, handled_sig = 0;
107 while((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0){
109 /* Whee! Actually deliver the signal. */
110 if(!handle_signal(regs, sig, &ka_copy, &info, oldset))
114 /* Did we come from a system call? */
115 if(!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)){
116 /* Restart the system call - no handlers present */
117 if(PT_REGS_SYSCALL_RET(regs) == -ERESTARTNOHAND ||
118 PT_REGS_SYSCALL_RET(regs) == -ERESTARTSYS ||
119 PT_REGS_SYSCALL_RET(regs) == -ERESTARTNOINTR){
120 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
121 PT_REGS_RESTART_SYSCALL(regs);
123 else if(PT_REGS_SYSCALL_RET(regs) == -ERESTART_RESTARTBLOCK){
124 PT_REGS_SYSCALL_RET(regs) = __NR_restart_syscall;
125 PT_REGS_RESTART_SYSCALL(regs);
129 /* This closes a way to execute a system call on the host. If
130 * you set a breakpoint on a system call instruction and singlestep
131 * from it, the tracing thread used to PTRACE_SINGLESTEP the process
132 * rather than PTRACE_SYSCALL it, allowing the system call to execute
133 * on the host. The tracing thread will check this flag and
134 * PTRACE_SYSCALL if necessary.
136 if(current->ptrace & PT_DTRACE)
137 current->thread.singlestep_syscall =
138 is_syscall(PT_REGS_IP(¤t->thread.regs));
144 return(kern_do_signal(¤t->thread.regs, ¤t->blocked));
148 * Atomically swap in the new signal mask, and wait for a signal.
150 long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
155 spin_lock_irq(¤t->sighand->siglock);
156 saveset = current->blocked;
157 siginitset(¤t->blocked, mask);
159 spin_unlock_irq(¤t->sighand->siglock);
161 PT_REGS_SYSCALL_RET(¤t->thread.regs) = -EINTR;
163 current->state = TASK_INTERRUPTIBLE;
165 if(kern_do_signal(¤t->thread.regs, &saveset))
170 long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
172 sigset_t saveset, newset;
174 /* XXX: Don't preclude handling different sized sigset_t's. */
175 if (sigsetsize != sizeof(sigset_t))
178 if (copy_from_user(&newset, unewset, sizeof(newset)))
180 sigdelsetmask(&newset, ~_BLOCKABLE);
182 spin_lock_irq(¤t->sighand->siglock);
183 saveset = current->blocked;
184 current->blocked = newset;
186 spin_unlock_irq(¤t->sighand->siglock);
188 PT_REGS_SYSCALL_RET(¤t->thread.regs) = -EINTR;
190 current->state = TASK_INTERRUPTIBLE;
192 if (kern_do_signal(¤t->thread.regs, &saveset))
197 int sys_sigaction(int sig, const struct old_sigaction __user *act,
198 struct old_sigaction __user *oact)
200 struct k_sigaction new_ka, old_ka;
205 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
206 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
207 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
209 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
210 __get_user(mask, &act->sa_mask);
211 siginitset(&new_ka.sa.sa_mask, mask);
214 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
217 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
218 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
219 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
221 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
222 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
228 long sys_sigaltstack(const stack_t *uss, stack_t *uoss)
230 return(do_sigaltstack(uss, uoss, PT_REGS_SP(¤t->thread.regs)));
233 extern int userspace_pid[];
235 static int copy_sc_from_user(struct pt_regs *to, void *from,
236 struct arch_frame_data *arch)
240 ret = CHOOSE_MODE(copy_sc_from_user_tt(UPT_SC(&to->regs), from, arch),
241 copy_sc_from_user_skas(userspace_pid[0],
246 long sys_sigreturn(struct pt_regs regs)
248 void __user *sc = sp_to_sc(PT_REGS_SP(¤t->thread.regs));
249 void __user *mask = sp_to_mask(PT_REGS_SP(¤t->thread.regs));
250 int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
252 spin_lock_irq(¤t->sighand->siglock);
253 copy_from_user(¤t->blocked.sig[0], sc_sigmask(sc),
254 sizeof(current->blocked.sig[0]));
255 copy_from_user(¤t->blocked.sig[1], mask, sig_size);
256 sigdelsetmask(¤t->blocked, ~_BLOCKABLE);
258 spin_unlock_irq(¤t->sighand->siglock);
259 copy_sc_from_user(¤t->thread.regs, sc,
260 &signal_frame_sc.common.arch);
261 return(PT_REGS_SYSCALL_RET(¤t->thread.regs));
264 long sys_rt_sigreturn(struct pt_regs regs)
266 unsigned long sp = PT_REGS_SP(¤t->thread.regs);
267 struct ucontext __user *uc = sp_to_uc(sp);
268 int sig_size = _NSIG_WORDS * sizeof(unsigned long);
270 spin_lock_irq(¤t->sighand->siglock);
271 copy_from_user(¤t->blocked, &uc->uc_sigmask, sig_size);
272 sigdelsetmask(¤t->blocked, ~_BLOCKABLE);
274 spin_unlock_irq(¤t->sighand->siglock);
275 copy_sc_from_user(¤t->thread.regs, &uc->uc_mcontext,
276 &signal_frame_si.common.arch);
277 return(PT_REGS_SYSCALL_RET(¤t->thread.regs));
281 * Overrides for Emacs so that we follow Linus's tabbing style.
282 * Emacs will notice this stuff at the end of the file and automatically
283 * adjust the settings for this buffer only. This must remain at the end
285 * ---------------------------------------------------------------------------
287 * c-file-style: "linux"