]> Pileus Git - ~andy/linux/blob - arch/um/kernel/signal.c
0dfcef92ec912e114fcec46962ebdbc1c63bc147
[~andy/linux] / arch / um / kernel / signal.c
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5
6 #include <linux/module.h>
7 #include <linux/ptrace.h>
8 #include <linux/sched.h>
9 #include <asm/siginfo.h>
10 #include <asm/signal.h>
11 #include <asm/unistd.h>
12 #include "frame_kern.h"
13 #include "kern_util.h"
14
15 EXPORT_SYMBOL(block_signals);
16 EXPORT_SYMBOL(unblock_signals);
17
18 #define _S(nr) (1<<((nr)-1))
19
20 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
21
22 /*
23  * OK, we're invoking a handler
24  */
25 static int handle_signal(struct pt_regs *regs, unsigned long signr,
26                          struct k_sigaction *ka, siginfo_t *info,
27                          sigset_t *oldset)
28 {
29         unsigned long sp;
30         int err;
31
32         /* Did we come from a system call? */
33         if (PT_REGS_SYSCALL_NR(regs) >= 0) {
34                 /* If so, check system call restarting.. */
35                 switch (PT_REGS_SYSCALL_RET(regs)) {
36                 case -ERESTART_RESTARTBLOCK:
37                 case -ERESTARTNOHAND:
38                         PT_REGS_SYSCALL_RET(regs) = -EINTR;
39                         break;
40
41                 case -ERESTARTSYS:
42                         if (!(ka->sa.sa_flags & SA_RESTART)) {
43                                 PT_REGS_SYSCALL_RET(regs) = -EINTR;
44                                 break;
45                         }
46                 /* fallthrough */
47                 case -ERESTARTNOINTR:
48                         PT_REGS_RESTART_SYSCALL(regs);
49                         PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
50                         break;
51                 }
52         }
53
54         sp = PT_REGS_SP(regs);
55         if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
56                 sp = current->sas_ss_sp + current->sas_ss_size;
57
58 #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
59         if (!(ka->sa.sa_flags & SA_SIGINFO))
60                 err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
61         else
62 #endif
63                 err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
64
65         if (err)
66                 force_sigsegv(signr, current);
67         else
68                 block_sigmask(ka, signr);
69
70         return err;
71 }
72
73 static int kern_do_signal(struct pt_regs *regs)
74 {
75         struct k_sigaction ka_copy;
76         siginfo_t info;
77         sigset_t *oldset;
78         int sig, handled_sig = 0;
79
80         if (test_thread_flag(TIF_RESTORE_SIGMASK))
81                 oldset = &current->saved_sigmask;
82         else
83                 oldset = &current->blocked;
84
85         while ((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0) {
86                 handled_sig = 1;
87                 /* Whee!  Actually deliver the signal.  */
88                 if (!handle_signal(regs, sig, &ka_copy, &info, oldset)) {
89                         /*
90                          * a signal was successfully delivered; the saved
91                          * sigmask will have been stored in the signal frame,
92                          * and will be restored by sigreturn, so we can simply
93                          * clear the TIF_RESTORE_SIGMASK flag
94                          */
95                         if (test_thread_flag(TIF_RESTORE_SIGMASK))
96                                 clear_thread_flag(TIF_RESTORE_SIGMASK);
97                         break;
98                 }
99         }
100
101         /* Did we come from a system call? */
102         if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
103                 /* Restart the system call - no handlers present */
104                 switch (PT_REGS_SYSCALL_RET(regs)) {
105                 case -ERESTARTNOHAND:
106                 case -ERESTARTSYS:
107                 case -ERESTARTNOINTR:
108                         PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
109                         PT_REGS_RESTART_SYSCALL(regs);
110                         break;
111                 case -ERESTART_RESTARTBLOCK:
112                         PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
113                         PT_REGS_RESTART_SYSCALL(regs);
114                         break;
115                 }
116         }
117
118         /*
119          * This closes a way to execute a system call on the host.  If
120          * you set a breakpoint on a system call instruction and singlestep
121          * from it, the tracing thread used to PTRACE_SINGLESTEP the process
122          * rather than PTRACE_SYSCALL it, allowing the system call to execute
123          * on the host.  The tracing thread will check this flag and
124          * PTRACE_SYSCALL if necessary.
125          */
126         if (current->ptrace & PT_DTRACE)
127                 current->thread.singlestep_syscall =
128                         is_syscall(PT_REGS_IP(&current->thread.regs));
129
130         /*
131          * if there's no signal to deliver, we just put the saved sigmask
132          * back
133          */
134         if (!handled_sig && test_thread_flag(TIF_RESTORE_SIGMASK)) {
135                 clear_thread_flag(TIF_RESTORE_SIGMASK);
136                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
137         }
138         return handled_sig;
139 }
140
141 int do_signal(void)
142 {
143         return kern_do_signal(&current->thread.regs);
144 }
145
146 /*
147  * Atomically swap in the new signal mask, and wait for a signal.
148  */
149 long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
150 {
151         sigset_t blocked;
152
153         mask &= _BLOCKABLE;
154         siginitset(&blocked, mask);
155         set_current_blocked(&blocked);
156
157         current->state = TASK_INTERRUPTIBLE;
158         schedule();
159         set_thread_flag(TIF_RESTORE_SIGMASK);
160         return -ERESTARTNOHAND;
161 }
162
163 long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
164 {
165         return do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs));
166 }