2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright 2010 Tilera Corporation. All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation, version 2.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for
16 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/unistd.h>
25 #include <linux/stddef.h>
26 #include <linux/personality.h>
27 #include <linux/suspend.h>
28 #include <linux/ptrace.h>
29 #include <linux/elf.h>
30 #include <linux/compat.h>
31 #include <linux/syscalls.h>
32 #include <linux/uaccess.h>
33 #include <asm/processor.h>
34 #include <asm/ucontext.h>
35 #include <asm/sigframe.h>
36 #include <arch/interrupts.h>
40 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
43 /* Caller before callee in this file; other callee is in assembler */
44 void do_signal(struct pt_regs *regs);
46 long _sys_sigaltstack(const stack_t __user *uss,
47 stack_t __user *uoss, struct pt_regs *regs)
49 return do_sigaltstack(uss, uoss, regs->sp);
54 * Do a signal return; undo the signal stack.
57 int restore_sigcontext(struct pt_regs *regs,
58 struct sigcontext __user *sc, long *pr0)
63 /* Always make any pending restarted system calls return -EINTR */
64 current_thread_info()->restart_block.fn = do_no_restart_syscall;
66 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
67 err |= __get_user(((long *)regs)[i],
68 &((long *)(&sc->regs))[i]);
70 regs->faultnum = INT_SWINT_1_SIGRETURN;
72 err |= __get_user(*pr0, &sc->regs.regs[0]);
76 int _sys_rt_sigreturn(struct pt_regs *regs)
78 struct rt_sigframe __user *frame =
79 (struct rt_sigframe __user *)(regs->sp);
83 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, &r0))
97 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
103 force_sig(SIGSEGV, current);
108 * Set up a signal frame.
111 int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
115 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
116 err |= __put_user(((long *)regs)[i],
117 &((long *)(&sc->regs))[i]);
123 * Determine which stack to use..
125 static inline void __user *get_sigframe(struct k_sigaction *ka,
126 struct pt_regs *regs,
131 /* Default to using normal stack */
135 * If we are on the alternate signal stack and would overflow
136 * it, don't. Return an always-bogus address instead so we
137 * will die with SIGSEGV.
139 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
140 return (void __user *) -1L;
142 /* This is the X/Open sanctioned signal stack switching. */
143 if (ka->sa.sa_flags & SA_ONSTACK) {
144 if (sas_ss_flags(sp) == 0)
145 sp = current->sas_ss_sp + current->sas_ss_size;
150 * Align the stack pointer according to the TILE ABI,
151 * i.e. so that on function entry (sp & 15) == 0.
154 return (void __user *) sp;
157 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
158 sigset_t *set, struct pt_regs *regs)
160 unsigned long restorer;
161 struct rt_sigframe __user *frame;
165 frame = get_sigframe(ka, regs, sizeof(*frame));
167 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
170 usig = current_thread_info()->exec_domain
171 && current_thread_info()->exec_domain->signal_invmap
173 ? current_thread_info()->exec_domain->signal_invmap[sig]
176 /* Always write at least the signal number for the stack backtracer. */
177 if (ka->sa.sa_flags & SA_SIGINFO) {
178 /* At sigreturn time, restore the callee-save registers too. */
179 err |= copy_siginfo_to_user(&frame->info, info);
180 regs->flags |= PT_FLAGS_RESTORE_REGS;
182 err |= __put_user(info->si_signo, &frame->info.si_signo);
185 /* Create the ucontext. */
186 err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
187 err |= __put_user(0, &frame->uc.uc_flags);
188 err |= __put_user(0, &frame->uc.uc_link);
189 err |= __put_user((void *)(current->sas_ss_sp),
190 &frame->uc.uc_stack.ss_sp);
191 err |= __put_user(sas_ss_flags(regs->sp),
192 &frame->uc.uc_stack.ss_flags);
193 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
194 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
195 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
199 restorer = VDSO_BASE;
200 if (ka->sa.sa_flags & SA_RESTORER)
201 restorer = (unsigned long) ka->sa.sa_restorer;
204 * Set up registers for signal handler.
205 * Registers that we don't modify keep the value they had from
206 * user-space at the time we took the signal.
208 regs->pc = (unsigned long) ka->sa.sa_handler;
209 regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
210 regs->sp = (unsigned long) frame;
212 regs->regs[0] = (unsigned long) usig;
214 if (ka->sa.sa_flags & SA_SIGINFO) {
215 /* Need extra arguments, so mark to restore caller-saves. */
216 regs->regs[1] = (unsigned long) &frame->info;
217 regs->regs[2] = (unsigned long) &frame->uc;
218 regs->flags |= PT_FLAGS_CALLER_SAVES;
222 * Notify any tracer that was single-stepping it.
223 * The tracer may want to single-step inside the
226 if (test_thread_flag(TIF_SINGLESTEP))
227 ptrace_notify(SIGTRAP);
232 force_sigsegv(sig, current);
237 * OK, we're invoking a handler
240 static int handle_signal(unsigned long sig, siginfo_t *info,
241 struct k_sigaction *ka, sigset_t *oldset,
242 struct pt_regs *regs)
247 /* Are we from a system call? */
248 if (regs->faultnum == INT_SWINT_1) {
249 /* If so, check system call restarting.. */
250 switch (regs->regs[0]) {
251 case -ERESTART_RESTARTBLOCK:
252 case -ERESTARTNOHAND:
253 regs->regs[0] = -EINTR;
257 if (!(ka->sa.sa_flags & SA_RESTART)) {
258 regs->regs[0] = -EINTR;
262 case -ERESTARTNOINTR:
263 /* Reload caller-saves to restore r0..r5 and r10. */
264 regs->flags |= PT_FLAGS_CALLER_SAVES;
265 regs->regs[0] = regs->orig_r0;
270 /* Set up the stack frame */
272 if (is_compat_task())
273 ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
276 ret = setup_rt_frame(sig, ka, info, oldset, regs);
278 /* This code is only called from system calls or from
279 * the work_pending path in the return-to-user code, and
280 * either way we can re-enable interrupts unconditionally.
282 spin_lock_irq(¤t->sighand->siglock);
283 sigorsets(¤t->blocked,
284 ¤t->blocked, &ka->sa.sa_mask);
285 if (!(ka->sa.sa_flags & SA_NODEFER))
286 sigaddset(¤t->blocked, sig);
288 spin_unlock_irq(¤t->sighand->siglock);
295 * Note that 'init' is a special process: it doesn't get signals it doesn't
296 * want to handle. Thus you cannot kill init even with a SIGKILL even by
299 void do_signal(struct pt_regs *regs)
303 struct k_sigaction ka;
307 * i386 will check if we're coming from kernel mode and bail out
308 * here. In my experience this just turns weird crashes into
309 * weird spin-hangs. But if we find a case where this seems
310 * helpful, we can reinstate the check on "!user_mode(regs)".
313 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
314 oldset = ¤t->saved_sigmask;
316 oldset = ¤t->blocked;
318 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
320 /* Whee! Actually deliver the signal. */
321 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
323 * A signal was successfully delivered; the saved
324 * sigmask will have been stored in the signal frame,
325 * and will be restored by sigreturn, so we can simply
326 * clear the TS_RESTORE_SIGMASK flag.
328 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
334 /* Did we come from a system call? */
335 if (regs->faultnum == INT_SWINT_1) {
336 /* Restart the system call - no handlers present */
337 switch (regs->regs[0]) {
338 case -ERESTARTNOHAND:
340 case -ERESTARTNOINTR:
341 regs->flags |= PT_FLAGS_CALLER_SAVES;
342 regs->regs[0] = regs->orig_r0;
346 case -ERESTART_RESTARTBLOCK:
347 regs->flags |= PT_FLAGS_CALLER_SAVES;
348 regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
354 /* If there's no signal to deliver, just put the saved sigmask back. */
355 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
356 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
357 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);