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>
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>
22 #include <asm/syscalls.h>
24 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
26 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
29 return do_sigaltstack(uss, uoss, regs->sp);
36 unsigned long retcode;
40 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
44 #define COPY(x) err |= __get_user(regs->x, &sc->x)
65 * Don't allow anyone to pretend they're running in supervisor
66 * mode or something...
68 err |= !valid_user_regs(regs);
74 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
76 struct rt_sigframe __user *frame;
79 frame = (struct rt_sigframe __user *)regs->sp;
80 pr_debug("SIG return: frame = %p\n", frame);
82 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
85 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
88 sigdelsetmask(&set, ~_BLOCKABLE);
89 spin_lock_irq(¤t->sighand->siglock);
90 current->blocked = set;
92 spin_unlock_irq(¤t->sighand->siglock);
94 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
97 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
100 pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
101 regs->pc, regs->lr, regs->sp);
106 force_sig(SIGSEGV, current);
111 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
115 #define COPY(x) err |= __put_user(regs->x, &sc->x)
138 static inline void __user *
139 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
141 unsigned long sp = regs->sp;
143 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
144 sp = current->sas_ss_sp + current->sas_ss_size;
146 return (void __user *)((sp - framesize) & ~3);
150 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
151 sigset_t *set, struct pt_regs *regs)
153 struct rt_sigframe __user *frame;
156 frame = get_sigframe(ka, regs, sizeof(*frame));
158 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
162 * Set up the return code:
164 * mov r8, __NR_rt_sigreturn
167 * Note: This will blow up since we're using a non-executable
168 * stack. Better use SA_RESTORER.
170 #if __NR_rt_sigreturn > 127
171 # error __NR_rt_sigreturn must be < 127 to fit in a short mov
173 err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
176 err |= copy_siginfo_to_user(&frame->info, info);
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));
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;
200 printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
201 current->comm, current->pid);
202 regs->lr = (unsigned long) &frame->retcode;
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);
209 regs->pc = (unsigned long) ka->sa.sa_handler;
215 static inline void setup_syscall_restart(struct pt_regs *regs)
217 if (regs->r12 == -ERESTART_RESTARTBLOCK)
218 regs->r8 = __NR_restart_syscall;
220 regs->r12 = regs->r12_orig;
225 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
226 sigset_t *oldset, struct pt_regs *regs, int syscall)
231 * Set up the stack frame
233 ret = setup_rt_frame(sig, ka, info, oldset, regs);
236 * Check that the resulting registers are sane
238 ret |= !valid_user_regs(regs);
241 * Block the signal if we were unsuccessful.
243 if (ret != 0 || !(ka->sa.sa_flags & SA_NODEFER)) {
244 spin_lock_irq(¤t->sighand->siglock);
245 sigorsets(¤t->blocked, ¤t->blocked,
247 sigaddset(¤t->blocked, sig);
249 spin_unlock_irq(¤t->sighand->siglock);
255 force_sigsegv(sig, current);
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.
263 int do_signal(struct pt_regs *regs, sigset_t *oldset, int syscall)
267 struct k_sigaction ka;
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.
274 if (!user_mode(regs))
277 if (test_thread_flag(TIF_RESTORE_SIGMASK))
278 oldset = ¤t->saved_sigmask;
280 oldset = ¤t->blocked;
282 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
285 case -ERESTART_RESTARTBLOCK:
286 case -ERESTARTNOHAND:
293 if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
298 case -ERESTARTNOINTR:
299 setup_syscall_restart(regs);
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, ¤t->saved_sigmask, NULL);
312 handle_signal(signr, &ka, &info, oldset, regs, syscall);
316 asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
320 if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
323 if (ti->flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
324 do_signal(regs, ¤t->blocked, syscall);