Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / avr32 / kernel / signal.c
blob5616a00c10ba2f3a9660b63a25e57d021c00da92
1 /*
2 * Copyright (C) 2004-2006 Atmel Corporation
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
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.
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>
20 #include <asm/uaccess.h>
21 #include <asm/ucontext.h>
23 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
25 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
26 struct pt_regs *regs)
28 return do_sigaltstack(uss, uoss, regs->sp);
31 struct rt_sigframe
33 struct siginfo info;
34 struct ucontext uc;
35 unsigned long retcode;
38 static int
39 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
41 int err = 0;
43 #define COPY(x) err |= __get_user(regs->x, &sc->x)
44 COPY(sr);
45 COPY(pc);
46 COPY(lr);
47 COPY(sp);
48 COPY(r12);
49 COPY(r11);
50 COPY(r10);
51 COPY(r9);
52 COPY(r8);
53 COPY(r7);
54 COPY(r6);
55 COPY(r5);
56 COPY(r4);
57 COPY(r3);
58 COPY(r2);
59 COPY(r1);
60 COPY(r0);
61 #undef COPY
64 * Don't allow anyone to pretend they're running in supervisor
65 * mode or something...
67 err |= !valid_user_regs(regs);
69 return err;
73 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
75 struct rt_sigframe __user *frame;
76 sigset_t set;
78 frame = (struct rt_sigframe __user *)regs->sp;
79 pr_debug("SIG return: frame = %p\n", frame);
81 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
82 goto badframe;
84 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
85 goto badframe;
87 sigdelsetmask(&set, ~_BLOCKABLE);
88 spin_lock_irq(&current->sighand->siglock);
89 current->blocked = set;
90 recalc_sigpending();
91 spin_unlock_irq(&current->sighand->siglock);
93 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
94 goto badframe;
96 pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
97 regs->pc, regs->lr, regs->sp);
99 return regs->r12;
101 badframe:
102 force_sig(SIGSEGV, current);
103 return 0;
106 static int
107 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
109 int err = 0;
111 #define COPY(x) err |= __put_user(regs->x, &sc->x)
112 COPY(sr);
113 COPY(pc);
114 COPY(lr);
115 COPY(sp);
116 COPY(r12);
117 COPY(r11);
118 COPY(r10);
119 COPY(r9);
120 COPY(r8);
121 COPY(r7);
122 COPY(r6);
123 COPY(r5);
124 COPY(r4);
125 COPY(r3);
126 COPY(r2);
127 COPY(r1);
128 COPY(r0);
129 #undef COPY
131 return err;
134 static inline void __user *
135 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
137 unsigned long sp = regs->sp;
139 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
140 sp = current->sas_ss_sp + current->sas_ss_size;
142 return (void __user *)((sp - framesize) & ~3);
145 static int
146 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
147 sigset_t *set, struct pt_regs *regs)
149 struct rt_sigframe __user *frame;
150 int err = 0;
152 frame = get_sigframe(ka, regs, sizeof(*frame));
153 err = -EFAULT;
154 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
155 goto out;
158 * Set up the return code:
160 * mov r8, __NR_rt_sigreturn
161 * scall
163 * Note: This will blow up since we're using a non-executable
164 * stack. Better use SA_RESTORER.
166 #if __NR_rt_sigreturn > 127
167 # error __NR_rt_sigreturn must be < 127 to fit in a short mov
168 #endif
169 err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
170 &frame->retcode);
172 err |= copy_siginfo_to_user(&frame->info, info);
174 /* Set up the ucontext */
175 err |= __put_user(0, &frame->uc.uc_flags);
176 err |= __put_user(NULL, &frame->uc.uc_link);
177 err |= __put_user((void __user *)current->sas_ss_sp,
178 &frame->uc.uc_stack.ss_sp);
179 err |= __put_user(sas_ss_flags(regs->sp),
180 &frame->uc.uc_stack.ss_flags);
181 err |= __put_user(current->sas_ss_size,
182 &frame->uc.uc_stack.ss_size);
183 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
184 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
186 if (err)
187 goto out;
189 regs->r12 = sig;
190 regs->r11 = (unsigned long) &frame->info;
191 regs->r10 = (unsigned long) &frame->uc;
192 regs->sp = (unsigned long) frame;
193 if (ka->sa.sa_flags & SA_RESTORER)
194 regs->lr = (unsigned long)ka->sa.sa_restorer;
195 else {
196 printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
197 current->comm, current->pid);
198 regs->lr = (unsigned long) &frame->retcode;
201 pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
202 current->comm, current->pid, sig, regs->sp,
203 regs->pc, ka->sa.sa_handler, regs->lr);
205 regs->pc = (unsigned long) ka->sa.sa_handler;
207 out:
208 return err;
211 static inline void restart_syscall(struct pt_regs *regs)
213 if (regs->r12 == -ERESTART_RESTARTBLOCK)
214 regs->r8 = __NR_restart_syscall;
215 else
216 regs->r12 = regs->r12_orig;
217 regs->pc -= 2;
220 static inline void
221 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
222 sigset_t *oldset, struct pt_regs *regs, int syscall)
224 int ret;
227 * Set up the stack frame
229 ret = setup_rt_frame(sig, ka, info, oldset, regs);
232 * Check that the resulting registers are sane
234 ret |= !valid_user_regs(regs);
237 * Block the signal if we were unsuccessful.
239 if (ret != 0 || !(ka->sa.sa_flags & SA_NODEFER)) {
240 spin_lock_irq(&current->sighand->siglock);
241 sigorsets(&current->blocked, &current->blocked,
242 &ka->sa.sa_mask);
243 sigaddset(&current->blocked, sig);
244 recalc_sigpending();
245 spin_unlock_irq(&current->sighand->siglock);
248 if (ret == 0)
249 return;
251 force_sigsegv(sig, current);
255 * Note that 'init' is a special process: it doesn't get signals it
256 * doesn't want to handle. Thus you cannot kill init even with a
257 * SIGKILL even by mistake.
259 int do_signal(struct pt_regs *regs, sigset_t *oldset, int syscall)
261 siginfo_t info;
262 int signr;
263 struct k_sigaction ka;
266 * We want the common case to go fast, which is why we may in
267 * certain cases get here from kernel mode. Just return
268 * without doing anything if so.
270 if (!user_mode(regs))
271 return 0;
273 if (test_thread_flag(TIF_RESTORE_SIGMASK))
274 oldset = &current->saved_sigmask;
275 else if (!oldset)
276 oldset = &current->blocked;
278 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
279 if (syscall) {
280 switch (regs->r12) {
281 case -ERESTART_RESTARTBLOCK:
282 case -ERESTARTNOHAND:
283 if (signr > 0) {
284 regs->r12 = -EINTR;
285 break;
287 /* fall through */
288 case -ERESTARTSYS:
289 if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
290 regs->r12 = -EINTR;
291 break;
293 /* fall through */
294 case -ERESTARTNOINTR:
295 restart_syscall(regs);
299 if (signr == 0) {
300 /* No signal to deliver -- put the saved sigmask back */
301 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
302 clear_thread_flag(TIF_RESTORE_SIGMASK);
303 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
305 return 0;
308 handle_signal(signr, &ka, &info, oldset, regs, syscall);
309 return 1;
312 asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
314 int syscall = 0;
316 if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
317 syscall = 1;
319 if (ti->flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
320 do_signal(regs, &current->blocked, syscall);