blob: 2a32e5e8e9c963f041fe7334bda569c1c2966591 [file] [log] [blame]
Jeff Dike1d3468a2006-07-10 04:45:13 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "linux/stddef.h"
7#include "linux/sys.h"
8#include "linux/sched.h"
9#include "linux/wait.h"
10#include "linux/kernel.h"
11#include "linux/smp_lock.h"
12#include "linux/module.h"
13#include "linux/slab.h"
14#include "linux/tty.h"
15#include "linux/binfmts.h"
16#include "linux/ptrace.h"
17#include "asm/signal.h"
18#include "asm/uaccess.h"
19#include "asm/unistd.h"
20#include "user_util.h"
21#include "asm/ucontext.h"
22#include "kern_util.h"
23#include "signal_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "kern.h"
25#include "frame_kern.h"
26#include "sigcontext.h"
27#include "mode.h"
28
29EXPORT_SYMBOL(block_signals);
30EXPORT_SYMBOL(unblock_signals);
31
32#define _S(nr) (1<<((nr)-1))
33
34#define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
35
36/*
37 * OK, we're invoking a handler
Jeff Dike1d3468a2006-07-10 04:45:13 -070038 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int handle_signal(struct pt_regs *regs, unsigned long signr,
40 struct k_sigaction *ka, siginfo_t *info,
41 sigset_t *oldset)
42{
43 unsigned long sp;
44 int err;
45
46 /* Always make any pending restarted system calls return -EINTR */
47 current_thread_info()->restart_block.fn = do_no_restart_syscall;
48
49 /* Did we come from a system call? */
50 if(PT_REGS_SYSCALL_NR(regs) >= 0){
51 /* If so, check system call restarting.. */
52 switch(PT_REGS_SYSCALL_RET(regs)){
53 case -ERESTART_RESTARTBLOCK:
54 case -ERESTARTNOHAND:
55 PT_REGS_SYSCALL_RET(regs) = -EINTR;
56 break;
57
58 case -ERESTARTSYS:
59 if (!(ka->sa.sa_flags & SA_RESTART)) {
60 PT_REGS_SYSCALL_RET(regs) = -EINTR;
61 break;
62 }
63 /* fallthrough */
64 case -ERESTARTNOINTR:
65 PT_REGS_RESTART_SYSCALL(regs);
66 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
67 break;
68 }
69 }
70
71 sp = PT_REGS_SP(regs);
72 if((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
73 sp = current->sas_ss_sp + current->sas_ss_size;
74
75#ifdef CONFIG_ARCH_HAS_SC_SIGNALS
76 if(!(ka->sa.sa_flags & SA_SIGINFO))
77 err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
78 else
79#endif
80 err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
81
82 if(err){
83 spin_lock_irq(&current->sighand->siglock);
84 current->blocked = *oldset;
85 recalc_sigpending();
86 spin_unlock_irq(&current->sighand->siglock);
87 force_sigsegv(signr, current);
Steven Rostedt69be8f12005-08-29 11:44:09 -040088 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 spin_lock_irq(&current->sighand->siglock);
Jeff Dike1d3468a2006-07-10 04:45:13 -070090 sigorsets(&current->blocked, &current->blocked,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 &ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -040092 if(!(ka->sa.sa_flags & SA_NODEFER))
93 sigaddset(&current->blocked, signr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 recalc_sigpending();
95 spin_unlock_irq(&current->sighand->siglock);
96 }
97
98 return err;
99}
100
Jeff Dike2fc10622006-01-18 17:44:02 -0800101static int kern_do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct k_sigaction ka_copy;
104 siginfo_t info;
Jeff Dike2fc10622006-01-18 17:44:02 -0800105 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int sig, handled_sig = 0;
107
Jeff Dike2fc10622006-01-18 17:44:02 -0800108 if (test_thread_flag(TIF_RESTORE_SIGMASK))
109 oldset = &current->saved_sigmask;
110 else
111 oldset = &current->blocked;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 while((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0){
114 handled_sig = 1;
115 /* Whee! Actually deliver the signal. */
Jeff Dike2fc10622006-01-18 17:44:02 -0800116 if(!handle_signal(regs, sig, &ka_copy, &info, oldset)){
117 /* a signal was successfully delivered; the saved
118 * sigmask will have been stored in the signal frame,
119 * and will be restored by sigreturn, so we can simply
120 * clear the TIF_RESTORE_SIGMASK flag */
121 if (test_thread_flag(TIF_RESTORE_SIGMASK))
122 clear_thread_flag(TIF_RESTORE_SIGMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 break;
Jeff Dike2fc10622006-01-18 17:44:02 -0800124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
127 /* Did we come from a system call? */
128 if(!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)){
129 /* Restart the system call - no handlers present */
Jeff Dike2fc10622006-01-18 17:44:02 -0800130 switch(PT_REGS_SYSCALL_RET(regs)){
131 case -ERESTARTNOHAND:
132 case -ERESTARTSYS:
133 case -ERESTARTNOINTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
135 PT_REGS_RESTART_SYSCALL(regs);
Jeff Dike2fc10622006-01-18 17:44:02 -0800136 break;
137 case -ERESTART_RESTARTBLOCK:
Jeff Dike1d3468a2006-07-10 04:45:13 -0700138 PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 PT_REGS_RESTART_SYSCALL(regs);
Jeff Dike2fc10622006-01-18 17:44:02 -0800140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142 }
143
144 /* This closes a way to execute a system call on the host. If
145 * you set a breakpoint on a system call instruction and singlestep
146 * from it, the tracing thread used to PTRACE_SINGLESTEP the process
147 * rather than PTRACE_SYSCALL it, allowing the system call to execute
Jeff Dike1d3468a2006-07-10 04:45:13 -0700148 * on the host. The tracing thread will check this flag and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * PTRACE_SYSCALL if necessary.
150 */
151 if(current->ptrace & PT_DTRACE)
152 current->thread.singlestep_syscall =
153 is_syscall(PT_REGS_IP(&current->thread.regs));
Jeff Dike2fc10622006-01-18 17:44:02 -0800154
155 /* if there's no signal to deliver, we just put the saved sigmask
156 * back */
157 if (!handled_sig && test_thread_flag(TIF_RESTORE_SIGMASK)) {
158 clear_thread_flag(TIF_RESTORE_SIGMASK);
159 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return(handled_sig);
162}
163
164int do_signal(void)
165{
Jeff Dike2fc10622006-01-18 17:44:02 -0800166 return(kern_do_signal(&current->thread.regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/*
170 * Atomically swap in the new signal mask, and wait for a signal.
171 */
172long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
173{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 mask &= _BLOCKABLE;
175 spin_lock_irq(&current->sighand->siglock);
Jeff Dike2fc10622006-01-18 17:44:02 -0800176 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 siginitset(&current->blocked, mask);
178 recalc_sigpending();
179 spin_unlock_irq(&current->sighand->siglock);
180
Jeff Dike2fc10622006-01-18 17:44:02 -0800181 current->state = TASK_INTERRUPTIBLE;
182 schedule();
183 set_thread_flag(TIF_RESTORE_SIGMASK);
184 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
188{
189 return(do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs)));
190}