]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - arch/avr32/kernel/signal.c
KEYS: Extend TIF_NOTIFY_RESUME to (almost) all architectures [try #6]
[linux-2.6.git] / arch / avr32 / kernel / signal.c
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * Based on linux/arch/sh/kernel/signal.c
5  *  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
6  *  Copyright (C) 1991, 1992  Linus Torvalds
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/unistd.h>
18 #include <linux/freezer.h>
19
20 #include <asm/uaccess.h>
21 #include <asm/ucontext.h>
22 #include <asm/syscalls.h>
23
24 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
25
26 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
27                                struct pt_regs *regs)
28 {
29         return do_sigaltstack(uss, uoss, regs->sp);
30 }
31
32 struct rt_sigframe
33 {
34         struct siginfo info;
35         struct ucontext uc;
36         unsigned long retcode;
37 };
38
39 static int
40 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
41 {
42         int err = 0;
43
44 #define COPY(x)         err |= __get_user(regs->x, &sc->x)
45         COPY(sr);
46         COPY(pc);
47         COPY(lr);
48         COPY(sp);
49         COPY(r12);
50         COPY(r11);
51         COPY(r10);
52         COPY(r9);
53         COPY(r8);
54         COPY(r7);
55         COPY(r6);
56         COPY(r5);
57         COPY(r4);
58         COPY(r3);
59         COPY(r2);
60         COPY(r1);
61         COPY(r0);
62 #undef  COPY
63
64         /*
65          * Don't allow anyone to pretend they're running in supervisor
66          * mode or something...
67          */
68         err |= !valid_user_regs(regs);
69
70         return err;
71 }
72
73
74 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
75 {
76         struct rt_sigframe __user *frame;
77         sigset_t set;
78
79         frame = (struct rt_sigframe __user *)regs->sp;
80         pr_debug("SIG return: frame = %p\n", frame);
81
82         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
83                 goto badframe;
84
85         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
86                 goto badframe;
87
88         sigdelsetmask(&set, ~_BLOCKABLE);
89         spin_lock_irq(&current->sighand->siglock);
90         current->blocked = set;
91         recalc_sigpending();
92         spin_unlock_irq(&current->sighand->siglock);
93
94         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
95                 goto badframe;
96
97         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
98                 goto badframe;
99
100         pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
101                  regs->pc, regs->lr, regs->sp);
102
103         return regs->r12;
104
105 badframe:
106         force_sig(SIGSEGV, current);
107         return 0;
108 }
109
110 static int
111 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
112 {
113         int err = 0;
114
115 #define COPY(x)         err |= __put_user(regs->x, &sc->x)
116         COPY(sr);
117         COPY(pc);
118         COPY(lr);
119         COPY(sp);
120         COPY(r12);
121         COPY(r11);
122         COPY(r10);
123         COPY(r9);
124         COPY(r8);
125         COPY(r7);
126         COPY(r6);
127         COPY(r5);
128         COPY(r4);
129         COPY(r3);
130         COPY(r2);
131         COPY(r1);
132         COPY(r0);
133 #undef  COPY
134
135         return err;
136 }
137
138 static inline void __user *
139 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
140 {
141         unsigned long sp = regs->sp;
142
143         if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
144                 sp = current->sas_ss_sp + current->sas_ss_size;
145
146         return (void __user *)((sp - framesize) & ~3);
147 }
148
149 static int
150 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
151                sigset_t *set, struct pt_regs *regs)
152 {
153         struct rt_sigframe __user *frame;
154         int err = 0;
155
156         frame = get_sigframe(ka, regs, sizeof(*frame));
157         err = -EFAULT;
158         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
159                 goto out;
160
161         /*
162          * Set up the return code:
163          *
164          *      mov     r8, __NR_rt_sigreturn
165          *      scall
166          *
167          * Note: This will blow up since we're using a non-executable
168          * stack. Better use SA_RESTORER.
169          */
170 #if __NR_rt_sigreturn > 127
171 # error __NR_rt_sigreturn must be < 127 to fit in a short mov
172 #endif
173         err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
174                          &frame->retcode);
175
176         err |= copy_siginfo_to_user(&frame->info, info);
177
178         /* Set up the ucontext */
179         err |= __put_user(0, &frame->uc.uc_flags);
180         err |= __put_user(NULL, &frame->uc.uc_link);
181         err |= __put_user((void __user *)current->sas_ss_sp,
182                           &frame->uc.uc_stack.ss_sp);
183         err |= __put_user(sas_ss_flags(regs->sp),
184                           &frame->uc.uc_stack.ss_flags);
185         err |= __put_user(current->sas_ss_size,
186                           &frame->uc.uc_stack.ss_size);
187         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
188         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
189
190         if (err)
191                 goto out;
192
193         regs->r12 = sig;
194         regs->r11 = (unsigned long) &frame->info;
195         regs->r10 = (unsigned long) &frame->uc;
196         regs->sp = (unsigned long) frame;
197         if (ka->sa.sa_flags & SA_RESTORER)
198                 regs->lr = (unsigned long)ka->sa.sa_restorer;
199         else {
200                 printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
201                        current->comm, current->pid);
202                 regs->lr = (unsigned long) &frame->retcode;
203         }
204
205         pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
206                  current->comm, current->pid, sig, regs->sp,
207                  regs->pc, ka->sa.sa_handler, regs->lr);
208
209         regs->pc = (unsigned long) ka->sa.sa_handler;
210
211 out:
212         return err;
213 }
214
215 static inline void setup_syscall_restart(struct pt_regs *regs)
216 {
217         if (regs->r12 == -ERESTART_RESTARTBLOCK)
218                 regs->r8 = __NR_restart_syscall;
219         else
220                 regs->r12 = regs->r12_orig;
221         regs->pc -= 2;
222 }
223
224 static inline void
225 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
226               sigset_t *oldset, struct pt_regs *regs, int syscall)
227 {
228         int ret;
229
230         /*
231          * Set up the stack frame
232          */
233         ret = setup_rt_frame(sig, ka, info, oldset, regs);
234
235         /*
236          * Check that the resulting registers are sane
237          */
238         ret |= !valid_user_regs(regs);
239
240         /*
241          * Block the signal if we were unsuccessful.
242          */
243         if (ret != 0 || !(ka->sa.sa_flags & SA_NODEFER)) {
244                 spin_lock_irq(&current->sighand->siglock);
245                 sigorsets(&current->blocked, &current->blocked,
246                           &ka->sa.sa_mask);
247                 sigaddset(&current->blocked, sig);
248                 recalc_sigpending();
249                 spin_unlock_irq(&current->sighand->siglock);
250         }
251
252         if (ret == 0)
253                 return;
254
255         force_sigsegv(sig, current);
256 }
257
258 /*
259  * Note that 'init' is a special process: it doesn't get signals it
260  * doesn't want to handle. Thus you cannot kill init even with a
261  * SIGKILL even by mistake.
262  */
263 int do_signal(struct pt_regs *regs, sigset_t *oldset, int syscall)
264 {
265         siginfo_t info;
266         int signr;
267         struct k_sigaction ka;
268
269         /*
270          * We want the common case to go fast, which is why we may in
271          * certain cases get here from kernel mode. Just return
272          * without doing anything if so.
273          */
274         if (!user_mode(regs))
275                 return 0;
276
277         if (test_thread_flag(TIF_RESTORE_SIGMASK))
278                 oldset = &current->saved_sigmask;
279         else if (!oldset)
280                 oldset = &current->blocked;
281
282         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
283         if (syscall) {
284                 switch (regs->r12) {
285                 case -ERESTART_RESTARTBLOCK:
286                 case -ERESTARTNOHAND:
287                         if (signr > 0) {
288                                 regs->r12 = -EINTR;
289                                 break;
290                         }
291                         /* fall through */
292                 case -ERESTARTSYS:
293                         if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
294                                 regs->r12 = -EINTR;
295                                 break;
296                         }
297                         /* fall through */
298                 case -ERESTARTNOINTR:
299                         setup_syscall_restart(regs);
300                 }
301         }
302
303         if (signr == 0) {
304                 /* No signal to deliver -- put the saved sigmask back */
305                 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
306                         clear_thread_flag(TIF_RESTORE_SIGMASK);
307                         sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
308                 }
309                 return 0;
310         }
311
312         handle_signal(signr, &ka, &info, oldset, regs, syscall);
313         return 1;
314 }
315
316 asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
317 {
318         int syscall = 0;
319
320         if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
321                 syscall = 1;
322
323         if (ti->flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
324                 do_signal(regs, &current->blocked, syscall);
325
326         if (ti->flags & _TIF_NOTIFY_RESUME) {
327                 clear_thread_flag(TIF_NOTIFY_RESUME);
328                 tracehook_notify_resume(regs);
329         }
330 }