OSDN Git Service

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