909e9b8d6612bc4b54f94ffa3e3622d0df3f6eca
[cascardo/linux.git] / 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 {
28         sigset_t *oldset = sigmask_to_save();
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         int sig, handled_sig = 0;
78
79         while ((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0) {
80                 handled_sig = 1;
81                 /* Whee!  Actually deliver the signal.  */
82                 if (!handle_signal(regs, sig, &ka_copy, &info)) {
83                         /*
84                          * a signal was successfully delivered; the saved
85                          * sigmask will have been stored in the signal frame,
86                          * and will be restored by sigreturn, so we can simply
87                          * clear the TIF_RESTORE_SIGMASK flag
88                          */
89                         if (test_thread_flag(TIF_RESTORE_SIGMASK))
90                                 clear_thread_flag(TIF_RESTORE_SIGMASK);
91                         break;
92                 }
93         }
94
95         /* Did we come from a system call? */
96         if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
97                 /* Restart the system call - no handlers present */
98                 switch (PT_REGS_SYSCALL_RET(regs)) {
99                 case -ERESTARTNOHAND:
100                 case -ERESTARTSYS:
101                 case -ERESTARTNOINTR:
102                         PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
103                         PT_REGS_RESTART_SYSCALL(regs);
104                         break;
105                 case -ERESTART_RESTARTBLOCK:
106                         PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
107                         PT_REGS_RESTART_SYSCALL(regs);
108                         break;
109                 }
110         }
111
112         /*
113          * This closes a way to execute a system call on the host.  If
114          * you set a breakpoint on a system call instruction and singlestep
115          * from it, the tracing thread used to PTRACE_SINGLESTEP the process
116          * rather than PTRACE_SYSCALL it, allowing the system call to execute
117          * on the host.  The tracing thread will check this flag and
118          * PTRACE_SYSCALL if necessary.
119          */
120         if (current->ptrace & PT_DTRACE)
121                 current->thread.singlestep_syscall =
122                         is_syscall(PT_REGS_IP(&current->thread.regs));
123
124         /*
125          * if there's no signal to deliver, we just put the saved sigmask
126          * back
127          */
128         if (!handled_sig)
129                 restore_saved_sigmask();
130         return handled_sig;
131 }
132
133 int do_signal(void)
134 {
135         return kern_do_signal(&current->thread.regs);
136 }
137
138 /*
139  * Atomically swap in the new signal mask, and wait for a signal.
140  */
141 long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
142 {
143         sigset_t blocked;
144         siginitset(&blocked, mask);
145         return sigsuspend(&blocked);
146 }
147
148 long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
149 {
150         return do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs));
151 }