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>
19 #include <linux/tracehook.h>
21 #include <asm/uaccess.h>
22 #include <asm/ucontext.h>
23 #include <asm/syscalls.h>
25 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
28 return do_sigaltstack(uss, uoss, regs->sp);
35 unsigned long retcode;
39 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
43 #define COPY(x) err |= __get_user(regs->x, &sc->x)
64 * Don't allow anyone to pretend they're running in supervisor
65 * mode or something...
67 err |= !valid_user_regs(regs);
73 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
75 struct rt_sigframe __user *frame;
78 /* Always make any pending restarted system calls return -EINTR */
79 current_thread_info()->restart_block.fn = do_no_restart_syscall;
81 frame = (struct rt_sigframe __user *)regs->sp;
82 pr_debug("SIG return: frame = %p\n", frame);
84 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
87 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
90 set_current_blocked(&set);
92 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
95 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
98 pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
99 regs->pc, regs->lr, regs->sp);
104 force_sig(SIGSEGV, current);
109 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
113 #define COPY(x) err |= __put_user(regs->x, &sc->x)
136 static inline void __user *
137 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
139 unsigned long sp = regs->sp;
141 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
142 sp = current->sas_ss_sp + current->sas_ss_size;
144 return (void __user *)((sp - framesize) & ~3);
148 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
149 sigset_t *set, struct pt_regs *regs)
151 struct rt_sigframe __user *frame;
154 frame = get_sigframe(ka, regs, sizeof(*frame));
156 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
160 * Set up the return code:
162 * mov r8, __NR_rt_sigreturn
165 * Note: This will blow up since we're using a non-executable
166 * stack. Better use SA_RESTORER.
168 #if __NR_rt_sigreturn > 127
169 # error __NR_rt_sigreturn must be < 127 to fit in a short mov
171 err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
174 err |= copy_siginfo_to_user(&frame->info, info);
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));
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;
198 printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
199 current->comm, current->pid);
200 regs->lr = (unsigned long) &frame->retcode;
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);
207 regs->pc = (unsigned long) ka->sa.sa_handler;
213 static inline void setup_syscall_restart(struct pt_regs *regs)
215 if (regs->r12 == -ERESTART_RESTARTBLOCK)
216 regs->r8 = __NR_restart_syscall;
218 regs->r12 = regs->r12_orig;
223 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
224 struct pt_regs *regs, int syscall)
229 * Set up the stack frame
231 ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs);
234 * Check that the resulting registers are sane
236 ret |= !valid_user_regs(regs);
239 * Block the signal if we were successful.
242 force_sigsegv(sig, current);
244 signal_delivered(sig, info, ka, regs, 0);
248 * Note that 'init' is a special process: it doesn't get signals it
249 * doesn't want to handle. Thus you cannot kill init even with a
250 * SIGKILL even by mistake.
252 static void do_signal(struct pt_regs *regs, int syscall)
256 struct k_sigaction ka;
259 * We want the common case to go fast, which is why we may in
260 * certain cases get here from kernel mode. Just return
261 * without doing anything if so.
263 if (!user_mode(regs))
266 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
269 case -ERESTART_RESTARTBLOCK:
270 case -ERESTARTNOHAND:
277 if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
282 case -ERESTARTNOINTR:
283 setup_syscall_restart(regs);
288 /* No signal to deliver -- put the saved sigmask back */
289 restore_saved_sigmask();
293 handle_signal(signr, &ka, &info, regs, syscall);
296 asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
300 if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
303 if (ti->flags & _TIF_SIGPENDING)
304 do_signal(regs, syscall);
306 if (ti->flags & _TIF_NOTIFY_RESUME) {
307 clear_thread_flag(TIF_NOTIFY_RESUME);
308 tracehook_notify_resume(regs);