Linux 4.8-rc8
[linux/fpc-iii.git] / arch / avr32 / kernel / signal.c
blob8f1c63b9b9839bd8de2562c55b72013b2cfc99cf
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/tracehook.h>
20 #include <asm/uaccess.h>
21 #include <asm/ucontext.h>
22 #include <asm/syscalls.h>
24 struct rt_sigframe
26 struct siginfo info;
27 struct ucontext uc;
28 unsigned long retcode;
31 static int
32 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
34 int err = 0;
36 #define COPY(x) err |= __get_user(regs->x, &sc->x)
37 COPY(sr);
38 COPY(pc);
39 COPY(lr);
40 COPY(sp);
41 COPY(r12);
42 COPY(r11);
43 COPY(r10);
44 COPY(r9);
45 COPY(r8);
46 COPY(r7);
47 COPY(r6);
48 COPY(r5);
49 COPY(r4);
50 COPY(r3);
51 COPY(r2);
52 COPY(r1);
53 COPY(r0);
54 #undef COPY
57 * Don't allow anyone to pretend they're running in supervisor
58 * mode or something...
60 err |= !valid_user_regs(regs);
62 return err;
66 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
68 struct rt_sigframe __user *frame;
69 sigset_t set;
71 /* Always make any pending restarted system calls return -EINTR */
72 current->restart_block.fn = do_no_restart_syscall;
74 frame = (struct rt_sigframe __user *)regs->sp;
75 pr_debug("SIG return: frame = %p\n", frame);
77 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
78 goto badframe;
80 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
81 goto badframe;
83 set_current_blocked(&set);
85 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
86 goto badframe;
88 if (restore_altstack(&frame->uc.uc_stack))
89 goto badframe;
91 pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
92 regs->pc, regs->lr, regs->sp);
94 return regs->r12;
96 badframe:
97 force_sig(SIGSEGV, current);
98 return 0;
101 static int
102 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
104 int err = 0;
106 #define COPY(x) err |= __put_user(regs->x, &sc->x)
107 COPY(sr);
108 COPY(pc);
109 COPY(lr);
110 COPY(sp);
111 COPY(r12);
112 COPY(r11);
113 COPY(r10);
114 COPY(r9);
115 COPY(r8);
116 COPY(r7);
117 COPY(r6);
118 COPY(r5);
119 COPY(r4);
120 COPY(r3);
121 COPY(r2);
122 COPY(r1);
123 COPY(r0);
124 #undef COPY
126 return err;
129 static inline void __user *
130 get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
132 unsigned long sp = sigsp(regs->sp, ksig);
134 return (void __user *)((sp - framesize) & ~3);
137 static int
138 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
140 struct rt_sigframe __user *frame;
141 int err = 0;
143 frame = get_sigframe(ksig, regs, sizeof(*frame));
144 err = -EFAULT;
145 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
146 goto out;
149 * Set up the return code:
151 * mov r8, __NR_rt_sigreturn
152 * scall
154 * Note: This will blow up since we're using a non-executable
155 * stack. Better use SA_RESTORER.
157 #if __NR_rt_sigreturn > 127
158 # error __NR_rt_sigreturn must be < 127 to fit in a short mov
159 #endif
160 err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
161 &frame->retcode);
163 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
165 /* Set up the ucontext */
166 err |= __put_user(0, &frame->uc.uc_flags);
167 err |= __put_user(NULL, &frame->uc.uc_link);
168 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
169 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
170 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
172 if (err)
173 goto out;
175 regs->r12 = ksig->sig;
176 regs->r11 = (unsigned long) &frame->info;
177 regs->r10 = (unsigned long) &frame->uc;
178 regs->sp = (unsigned long) frame;
179 if (ksig->ka.sa.sa_flags & SA_RESTORER)
180 regs->lr = (unsigned long)ksig->ka.sa.sa_restorer;
181 else {
182 printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
183 current->comm, current->pid);
184 regs->lr = (unsigned long) &frame->retcode;
187 pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
188 current->comm, current->pid, ksig->sig, regs->sp,
189 regs->pc, ksig->ka.sa.sa_handler, regs->lr);
191 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
193 out:
194 return err;
197 static inline void setup_syscall_restart(struct pt_regs *regs)
199 if (regs->r12 == -ERESTART_RESTARTBLOCK)
200 regs->r8 = __NR_restart_syscall;
201 else
202 regs->r12 = regs->r12_orig;
203 regs->pc -= 2;
206 static inline void
207 handle_signal(struct ksignal *ksig, struct pt_regs *regs, int syscall)
209 int ret;
212 * Set up the stack frame
214 ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
217 * Check that the resulting registers are sane
219 ret |= !valid_user_regs(regs);
222 * Block the signal if we were successful.
224 signal_setup_done(ret, ksig, 0);
228 * Note that 'init' is a special process: it doesn't get signals it
229 * doesn't want to handle. Thus you cannot kill init even with a
230 * SIGKILL even by mistake.
232 static void do_signal(struct pt_regs *regs, int syscall)
234 struct ksignal ksig;
237 * We want the common case to go fast, which is why we may in
238 * certain cases get here from kernel mode. Just return
239 * without doing anything if so.
241 if (!user_mode(regs))
242 return;
244 get_signal(&ksig);
245 if (syscall) {
246 switch (regs->r12) {
247 case -ERESTART_RESTARTBLOCK:
248 case -ERESTARTNOHAND:
249 if (ksig.sig > 0) {
250 regs->r12 = -EINTR;
251 break;
253 /* fall through */
254 case -ERESTARTSYS:
255 if (ksig.sig > 0 && !(ksig.ka.sa.sa_flags & SA_RESTART)) {
256 regs->r12 = -EINTR;
257 break;
259 /* fall through */
260 case -ERESTARTNOINTR:
261 setup_syscall_restart(regs);
265 if (!ksig.sig) {
266 /* No signal to deliver -- put the saved sigmask back */
267 restore_saved_sigmask();
268 return;
271 handle_signal(&ksig, regs, syscall);
274 asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
276 int syscall = 0;
278 if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
279 syscall = 1;
281 if (ti->flags & _TIF_SIGPENDING)
282 do_signal(regs, syscall);
284 if (ti->flags & _TIF_NOTIFY_RESUME) {
285 clear_thread_flag(TIF_NOTIFY_RESUME);
286 tracehook_notify_resume(regs);