xen/blkback: simplify address translations
[zen-stable.git] / arch / avr32 / kernel / signal.c
blob64f886fac2efef6ea05bd1c0703cfdf4ccb8d7d4
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>
19 #include <linux/tracehook.h>
21 #include <asm/uaccess.h>
22 #include <asm/ucontext.h>
23 #include <asm/syscalls.h>
25 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
27 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
28 struct pt_regs *regs)
30 return do_sigaltstack(uss, uoss, regs->sp);
33 struct rt_sigframe
35 struct siginfo info;
36 struct ucontext uc;
37 unsigned long retcode;
40 static int
41 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
43 int err = 0;
45 #define COPY(x) err |= __get_user(regs->x, &sc->x)
46 COPY(sr);
47 COPY(pc);
48 COPY(lr);
49 COPY(sp);
50 COPY(r12);
51 COPY(r11);
52 COPY(r10);
53 COPY(r9);
54 COPY(r8);
55 COPY(r7);
56 COPY(r6);
57 COPY(r5);
58 COPY(r4);
59 COPY(r3);
60 COPY(r2);
61 COPY(r1);
62 COPY(r0);
63 #undef COPY
66 * Don't allow anyone to pretend they're running in supervisor
67 * mode or something...
69 err |= !valid_user_regs(regs);
71 return err;
75 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
77 struct rt_sigframe __user *frame;
78 sigset_t set;
80 frame = (struct rt_sigframe __user *)regs->sp;
81 pr_debug("SIG return: frame = %p\n", frame);
83 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
84 goto badframe;
86 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
87 goto badframe;
89 sigdelsetmask(&set, ~_BLOCKABLE);
90 spin_lock_irq(&current->sighand->siglock);
91 current->blocked = set;
92 recalc_sigpending();
93 spin_unlock_irq(&current->sighand->siglock);
95 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
96 goto badframe;
98 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
99 goto badframe;
101 pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
102 regs->pc, regs->lr, regs->sp);
104 return regs->r12;
106 badframe:
107 force_sig(SIGSEGV, current);
108 return 0;
111 static int
112 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
114 int err = 0;
116 #define COPY(x) err |= __put_user(regs->x, &sc->x)
117 COPY(sr);
118 COPY(pc);
119 COPY(lr);
120 COPY(sp);
121 COPY(r12);
122 COPY(r11);
123 COPY(r10);
124 COPY(r9);
125 COPY(r8);
126 COPY(r7);
127 COPY(r6);
128 COPY(r5);
129 COPY(r4);
130 COPY(r3);
131 COPY(r2);
132 COPY(r1);
133 COPY(r0);
134 #undef COPY
136 return err;
139 static inline void __user *
140 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
142 unsigned long sp = regs->sp;
144 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
145 sp = current->sas_ss_sp + current->sas_ss_size;
147 return (void __user *)((sp - framesize) & ~3);
150 static int
151 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
152 sigset_t *set, struct pt_regs *regs)
154 struct rt_sigframe __user *frame;
155 int err = 0;
157 frame = get_sigframe(ka, regs, sizeof(*frame));
158 err = -EFAULT;
159 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
160 goto out;
163 * Set up the return code:
165 * mov r8, __NR_rt_sigreturn
166 * scall
168 * Note: This will blow up since we're using a non-executable
169 * stack. Better use SA_RESTORER.
171 #if __NR_rt_sigreturn > 127
172 # error __NR_rt_sigreturn must be < 127 to fit in a short mov
173 #endif
174 err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
175 &frame->retcode);
177 err |= copy_siginfo_to_user(&frame->info, info);
179 /* Set up the ucontext */
180 err |= __put_user(0, &frame->uc.uc_flags);
181 err |= __put_user(NULL, &frame->uc.uc_link);
182 err |= __put_user((void __user *)current->sas_ss_sp,
183 &frame->uc.uc_stack.ss_sp);
184 err |= __put_user(sas_ss_flags(regs->sp),
185 &frame->uc.uc_stack.ss_flags);
186 err |= __put_user(current->sas_ss_size,
187 &frame->uc.uc_stack.ss_size);
188 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
189 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
191 if (err)
192 goto out;
194 regs->r12 = sig;
195 regs->r11 = (unsigned long) &frame->info;
196 regs->r10 = (unsigned long) &frame->uc;
197 regs->sp = (unsigned long) frame;
198 if (ka->sa.sa_flags & SA_RESTORER)
199 regs->lr = (unsigned long)ka->sa.sa_restorer;
200 else {
201 printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
202 current->comm, current->pid);
203 regs->lr = (unsigned long) &frame->retcode;
206 pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
207 current->comm, current->pid, sig, regs->sp,
208 regs->pc, ka->sa.sa_handler, regs->lr);
210 regs->pc = (unsigned long) ka->sa.sa_handler;
212 out:
213 return err;
216 static inline void setup_syscall_restart(struct pt_regs *regs)
218 if (regs->r12 == -ERESTART_RESTARTBLOCK)
219 regs->r8 = __NR_restart_syscall;
220 else
221 regs->r12 = regs->r12_orig;
222 regs->pc -= 2;
225 static inline void
226 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
227 sigset_t *oldset, struct pt_regs *regs, int syscall)
229 int ret;
232 * Set up the stack frame
234 ret = setup_rt_frame(sig, ka, info, oldset, regs);
237 * Check that the resulting registers are sane
239 ret |= !valid_user_regs(regs);
242 * Block the signal if we were unsuccessful.
244 if (ret != 0 || !(ka->sa.sa_flags & SA_NODEFER)) {
245 spin_lock_irq(&current->sighand->siglock);
246 sigorsets(&current->blocked, &current->blocked,
247 &ka->sa.sa_mask);
248 sigaddset(&current->blocked, sig);
249 recalc_sigpending();
250 spin_unlock_irq(&current->sighand->siglock);
253 if (ret == 0)
254 return;
256 force_sigsegv(sig, current);
260 * Note that 'init' is a special process: it doesn't get signals it
261 * doesn't want to handle. Thus you cannot kill init even with a
262 * SIGKILL even by mistake.
264 int do_signal(struct pt_regs *regs, sigset_t *oldset, int syscall)
266 siginfo_t info;
267 int signr;
268 struct k_sigaction ka;
271 * We want the common case to go fast, which is why we may in
272 * certain cases get here from kernel mode. Just return
273 * without doing anything if so.
275 if (!user_mode(regs))
276 return 0;
278 if (test_thread_flag(TIF_RESTORE_SIGMASK))
279 oldset = &current->saved_sigmask;
280 else if (!oldset)
281 oldset = &current->blocked;
283 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
284 if (syscall) {
285 switch (regs->r12) {
286 case -ERESTART_RESTARTBLOCK:
287 case -ERESTARTNOHAND:
288 if (signr > 0) {
289 regs->r12 = -EINTR;
290 break;
292 /* fall through */
293 case -ERESTARTSYS:
294 if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
295 regs->r12 = -EINTR;
296 break;
298 /* fall through */
299 case -ERESTARTNOINTR:
300 setup_syscall_restart(regs);
304 if (signr == 0) {
305 /* No signal to deliver -- put the saved sigmask back */
306 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
307 clear_thread_flag(TIF_RESTORE_SIGMASK);
308 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
310 return 0;
313 handle_signal(signr, &ka, &info, oldset, regs, syscall);
314 return 1;
317 asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
319 int syscall = 0;
321 if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
322 syscall = 1;
324 if (ti->flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
325 do_signal(regs, &current->blocked, syscall);
327 if (ti->flags & _TIF_NOTIFY_RESUME) {
328 clear_thread_flag(TIF_NOTIFY_RESUME);
329 tracehook_notify_resume(regs);
330 if (current->replacement_session_keyring)
331 key_replace_session_keyring();