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