4 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2008-2009 PetaLogix
6 * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au>
7 * Copyright (C) 2001 NEC Corporation
8 * Copyright (C) 2001 Miles Bader <miles@gnu.org>
9 * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima
10 * Copyright (C) 1991,1992 Linus Torvalds
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 * This file was was derived from the sh version, arch/sh/kernel/signal.c
16 * This file is subject to the terms and conditions of the GNU General
17 * Public License. See the file COPYING in the main directory of this
18 * archive for more details.
21 #include <linux/sched.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/kernel.h>
26 #include <linux/signal.h>
27 #include <linux/errno.h>
28 #include <linux/wait.h>
29 #include <linux/ptrace.h>
30 #include <linux/unistd.h>
31 #include <linux/stddef.h>
32 #include <linux/personality.h>
33 #include <linux/percpu.h>
34 #include <linux/linkage.h>
35 #include <asm/entry.h>
36 #include <asm/ucontext.h>
37 #include <linux/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/pgalloc.h>
40 #include <linux/syscalls.h>
41 #include <asm/cacheflush.h>
42 #include <asm/syscalls.h>
44 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
46 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_sycall);
49 * Atomically swap in the new signal mask, and wait for a signal.
52 sys_sigsuspend(old_sigset_t mask, struct pt_regs *regs)
57 spin_lock_irq(¤t->sighand->siglock);
58 saveset = current->blocked;
59 siginitset(¤t->blocked, mask);
61 spin_unlock_irq(¤t->sighand->siglock);
65 current->state = TASK_INTERRUPTIBLE;
67 if (do_signal(regs, &saveset, 1))
73 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize,
76 sigset_t saveset, newset;
78 /* XXX: Don't preclude handling different sized sigset_t's. */
79 if (sigsetsize != sizeof(sigset_t))
82 if (copy_from_user(&newset, unewset, sizeof(newset)))
84 sigdelsetmask(&newset, ~_BLOCKABLE);
85 spin_lock_irq(¤t->sighand->siglock);
86 saveset = current->blocked;
87 current->blocked = newset;
89 spin_unlock_irq(¤t->sighand->siglock);
93 current->state = TASK_INTERRUPTIBLE;
95 if (do_signal(regs, &saveset, 1))
101 sys_sigaction(int sig, const struct old_sigaction *act,
102 struct old_sigaction *oact)
104 struct k_sigaction new_ka, old_ka;
109 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
110 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
111 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
113 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
114 __get_user(mask, &act->sa_mask);
115 siginitset(&new_ka.sa.sa_mask, mask);
118 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
121 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
122 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
123 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
125 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
126 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
133 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
134 struct pt_regs *regs)
136 return do_sigaltstack(uss, uoss, regs->r1);
140 * Do a signal return; undo the signal stack.
144 struct sigcontext sc;
145 unsigned long extramask[_NSIG_WORDS-1];
146 unsigned long tramp[2]; /* signal trampoline */
152 unsigned long tramp[2]; /* signal trampoline */
156 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, int *rval_p)
158 unsigned int err = 0;
160 #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
163 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
164 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
165 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
166 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
167 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
168 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
169 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
170 COPY(r30); COPY(r31);
171 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
179 asmlinkage int sys_sigreturn(struct pt_regs *regs)
181 struct sigframe *frame =
182 (struct sigframe *)(regs->r1 + STATE_SAVE_ARG_SPACE);
187 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
190 if (__get_user(set.sig[0], &frame->sc.oldmask)
192 && __copy_from_user(&set.sig[1], &frame->extramask,
193 sizeof(frame->extramask))))
196 sigdelsetmask(&set, ~_BLOCKABLE);
198 spin_lock_irq(¤t->sighand->siglock);
199 current->blocked = set;
201 spin_unlock_irq(¤t->sighand->siglock);
203 if (restore_sigcontext(regs, &frame->sc, &rval))
208 force_sig(SIGSEGV, current);
212 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
214 struct rt_sigframe *frame =
215 (struct rt_sigframe *)(regs->r1 + STATE_SAVE_ARG_SPACE);
221 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
224 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
227 sigdelsetmask(&set, ~_BLOCKABLE);
228 spin_lock_irq(¤t->sighand->siglock);
229 current->blocked = set;
231 spin_unlock_irq(¤t->sighand->siglock);
233 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
236 if (__copy_from_user((void *)&st, &frame->uc.uc_stack, sizeof(st)))
238 /* It is more difficult to avoid calling this function than to
239 call it and ignore errors. */
240 do_sigaltstack(&st, NULL, regs->r1);
245 force_sig(SIGSEGV, current);
250 * Set up a signal frame.
254 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
259 #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
262 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
263 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
264 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
265 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
266 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
267 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
268 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
269 COPY(r30); COPY(r31);
270 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
273 err |= __put_user(mask, &sc->oldmask);
279 * Determine which stack to use..
282 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
284 /* Default to using normal stack */
285 unsigned long sp = regs->r1;
287 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
288 sp = current->sas_ss_sp + current->sas_ss_size;
290 return (void *)((sp - frame_size) & -8UL);
293 static void setup_frame(int sig, struct k_sigaction *ka,
294 sigset_t *set, struct pt_regs *regs)
296 struct sigframe *frame;
300 frame = get_sigframe(ka, regs, sizeof(*frame));
302 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
305 signal = current_thread_info()->exec_domain
306 && current_thread_info()->exec_domain->signal_invmap
308 ? current_thread_info()->exec_domain->signal_invmap[sig]
311 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
313 if (_NSIG_WORDS > 1) {
314 err |= __copy_to_user(frame->extramask, &set->sig[1],
315 sizeof(frame->extramask));
318 /* Set up to return from userspace. If provided, use a stub
319 already in userspace. */
320 /* minus 8 is offset to cater for "rtsd r15,8" offset */
321 if (ka->sa.sa_flags & SA_RESTORER) {
322 regs->r15 = ((unsigned long)ka->sa.sa_restorer)-8;
324 /* Note, these encodings are _big endian_! */
326 /* addi r12, r0, __NR_sigreturn */
327 err |= __put_user(0x31800000 | __NR_sigreturn ,
330 err |= __put_user(0xb9cc0008, frame->tramp + 1);
332 /* Return from sighandler will jump to the tramp.
333 Negative 8 offset because return is rtsd r15, 8 */
334 regs->r15 = ((unsigned long)frame->tramp)-8;
336 __invalidate_cache_sigtramp((unsigned long)frame->tramp);
342 /* Set up registers for signal handler */
343 regs->r1 = (unsigned long) frame - STATE_SAVE_ARG_SPACE;
345 /* Signal handler args: */
346 regs->r5 = signal; /* Arg 0: signum */
347 regs->r6 = (unsigned long) &frame->sc; /* arg 1: sigcontext */
349 /* Offset of 4 to handle microblaze rtid r14, 0 */
350 regs->pc = (unsigned long)ka->sa.sa_handler;
355 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
356 current->comm, current->pid, frame, regs->pc);
363 ka->sa.sa_handler = SIG_DFL;
364 force_sig(SIGSEGV, current);
367 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
368 sigset_t *set, struct pt_regs *regs)
370 struct rt_sigframe *frame;
374 frame = get_sigframe(ka, regs, sizeof(*frame));
376 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
379 signal = current_thread_info()->exec_domain
380 && current_thread_info()->exec_domain->signal_invmap
382 ? current_thread_info()->exec_domain->signal_invmap[sig]
385 err |= copy_siginfo_to_user(&frame->info, info);
387 /* Create the ucontext. */
388 err |= __put_user(0, &frame->uc.uc_flags);
389 err |= __put_user(0, &frame->uc.uc_link);
390 err |= __put_user((void *)current->sas_ss_sp,
391 &frame->uc.uc_stack.ss_sp);
392 err |= __put_user(sas_ss_flags(regs->r1),
393 &frame->uc.uc_stack.ss_flags);
394 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
395 err |= setup_sigcontext(&frame->uc.uc_mcontext,
397 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
399 /* Set up to return from userspace. If provided, use a stub
400 already in userspace. */
401 /* minus 8 is offset to cater for "rtsd r15,8" */
402 if (ka->sa.sa_flags & SA_RESTORER) {
403 regs->r15 = ((unsigned long)ka->sa.sa_restorer)-8;
405 /* addi r12, r0, __NR_sigreturn */
406 err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
409 err |= __put_user(0xb9cc0008, frame->tramp + 1);
411 /* Return from sighandler will jump to the tramp.
412 Negative 8 offset because return is rtsd r15, 8 */
413 regs->r15 = ((unsigned long)frame->tramp)-8;
415 __invalidate_cache_sigtramp((unsigned long)frame->tramp);
421 /* Set up registers for signal handler */
422 regs->r1 = (unsigned long) frame - STATE_SAVE_ARG_SPACE;
424 /* Signal handler args: */
425 regs->r5 = signal; /* arg 0: signum */
426 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
427 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
428 /* Offset to handle microblaze rtid r14, 0 */
429 regs->pc = (unsigned long)ka->sa.sa_handler;
434 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
435 current->comm, current->pid, frame, regs->pc);
442 ka->sa.sa_handler = SIG_DFL;
443 force_sig(SIGSEGV, current);
446 /* Handle restarting system calls */
448 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
451 case -ERESTART_RESTARTBLOCK:
452 case -ERESTARTNOHAND:
458 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
463 case -ERESTARTNOINTR:
465 /* offset of 4 bytes to re-execute trap (brki) instruction */
472 * OK, we're invoking a handler
476 handle_signal(unsigned long sig, struct k_sigaction *ka,
477 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
479 /* Set up the stack frame */
480 if (ka->sa.sa_flags & SA_SIGINFO)
481 setup_rt_frame(sig, ka, info, oldset, regs);
483 setup_frame(sig, ka, oldset, regs);
485 if (ka->sa.sa_flags & SA_ONESHOT)
486 ka->sa.sa_handler = SIG_DFL;
488 if (!(ka->sa.sa_flags & SA_NODEFER)) {
489 spin_lock_irq(¤t->sighand->siglock);
490 sigorsets(¤t->blocked,
491 ¤t->blocked, &ka->sa.sa_mask);
492 sigaddset(¤t->blocked, sig);
494 spin_unlock_irq(¤t->sighand->siglock);
499 * Note that 'init' is a special process: it doesn't get signals it doesn't
500 * want to handle. Thus you cannot kill init even with a SIGKILL even by
503 * Note that we go through the signals twice: once to check the signals that
504 * the kernel can handle, and then we build all the user-level signal handling
505 * stack-frames in one go after that.
507 int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_syscall)
511 struct k_sigaction ka;
513 printk(KERN_INFO "do signal: %p %p %d\n", regs, oldset, in_syscall);
514 printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
515 regs->r12, current_thread_info()->flags);
518 * We want the common case to go fast, which
519 * is why we may in certain cases get here from
520 * kernel mode. Just return without doing anything
523 if (kernel_mode(regs))
527 oldset = ¤t->blocked;
529 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
531 /* Whee! Actually deliver the signal. */
533 handle_restart(regs, &ka, 1);
534 handle_signal(signr, &ka, &info, oldset, regs);
539 handle_restart(regs, NULL, 0);
541 /* Did we come from a system call? */