1 /* signal.c: FRV specific bits of signal handling
3 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/m68k/kernel/signal.c
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/personality.h>
24 #include <linux/suspend.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/cacheflush.h>
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33 struct fdpic_func_descriptor {
38 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
41 * Atomically swap in the new signal mask, and wait for a signal.
43 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
48 spin_lock_irq(¤t->sighand->siglock);
49 saveset = current->blocked;
50 siginitset(¤t->blocked, mask);
52 spin_unlock_irq(¤t->sighand->siglock);
54 __frame->gr8 = -EINTR;
56 current->state = TASK_INTERRUPTIBLE;
58 if (do_signal(__frame, &saveset))
59 /* return the signal number as the return value of this function
60 * - this is an utterly evil hack. syscalls should not invoke do_signal()
61 * as entry.S sets regs->gr8 to the return value of the system call
62 * - we can't just use sigpending() as we'd have to discard SIG_IGN signals
63 * and call waitpid() if SIGCHLD needed discarding
64 * - this only works on the i386 because it passes arguments to the signal
65 * handler on the stack, and the return value in EAX is effectively
72 asmlinkage int sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
74 sigset_t saveset, newset;
76 /* XXX: Don't preclude handling different sized sigset_t's. */
77 if (sigsetsize != sizeof(sigset_t))
80 if (copy_from_user(&newset, unewset, sizeof(newset)))
82 sigdelsetmask(&newset, ~_BLOCKABLE);
84 spin_lock_irq(¤t->sighand->siglock);
85 saveset = current->blocked;
86 current->blocked = newset;
88 spin_unlock_irq(¤t->sighand->siglock);
90 __frame->gr8 = -EINTR;
92 current->state = TASK_INTERRUPTIBLE;
94 if (do_signal(__frame, &saveset))
95 /* return the signal number as the return value of this function
96 * - this is an utterly evil hack. syscalls should not invoke do_signal()
97 * as entry.S sets regs->gr8 to the return value of the system call
98 * - we can't just use sigpending() as we'd have to discard SIG_IGN signals
99 * and call waitpid() if SIGCHLD needed discarding
100 * - this only works on the i386 because it passes arguments to the signal
101 * handler on the stack, and the return value in EAX is effectively
108 asmlinkage int sys_sigaction(int sig,
109 const struct old_sigaction __user *act,
110 struct old_sigaction __user *oact)
112 struct k_sigaction new_ka, old_ka;
117 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
118 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
119 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
121 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
122 __get_user(mask, &act->sa_mask);
123 siginitset(&new_ka.sa.sa_mask, mask);
126 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
129 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
130 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
131 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
133 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
134 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
141 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
143 return do_sigaltstack(uss, uoss, __frame->sp);
148 * Do a signal return; undo the signal stack.
153 void (*pretcode)(void);
155 struct sigcontext sc;
156 unsigned long extramask[_NSIG_WORDS-1];
162 void (*pretcode)(void);
164 struct siginfo *pinfo;
171 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
173 struct user_context *user = current->thread.user;
174 unsigned long tbr, psr;
178 if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
183 restore_user_regs(user);
185 user->i.syscallno = -1; /* disable syscall checks */
187 *_gr8 = user->i.gr[8];
194 asmlinkage int sys_sigreturn(void)
196 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
200 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
202 if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
205 if (_NSIG_WORDS > 1 &&
206 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
209 sigdelsetmask(&set, ~_BLOCKABLE);
210 spin_lock_irq(¤t->sighand->siglock);
211 current->blocked = set;
213 spin_unlock_irq(¤t->sighand->siglock);
215 if (restore_sigcontext(&frame->sc, &gr8))
220 force_sig(SIGSEGV, current);
224 asmlinkage int sys_rt_sigreturn(void)
226 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
230 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
232 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
235 sigdelsetmask(&set, ~_BLOCKABLE);
236 spin_lock_irq(¤t->sighand->siglock);
237 current->blocked = set;
239 spin_unlock_irq(¤t->sighand->siglock);
241 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
244 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
250 force_sig(SIGSEGV, current);
255 * Set up a signal frame
257 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
259 save_user_regs(current->thread.user);
261 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
264 /* non-iBCS2 extensions.. */
265 if (__put_user(mask, &sc->sc_oldmask) < 0)
274 /*****************************************************************************/
276 * Determine which stack to use..
278 static inline void __user *get_sigframe(struct k_sigaction *ka,
279 struct pt_regs *regs,
284 /* Default to using normal stack */
287 /* This is the X/Open sanctioned signal stack switching. */
288 if (ka->sa.sa_flags & SA_ONSTACK) {
289 if (! on_sig_stack(sp))
290 sp = current->sas_ss_sp + current->sas_ss_size;
293 return (void __user *) ((sp - frame_size) & ~7UL);
294 } /* end get_sigframe() */
296 /*****************************************************************************/
300 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
301 struct pt_regs *regs)
303 struct sigframe __user *frame;
306 frame = get_sigframe(ka, regs, sizeof(*frame));
308 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
313 __current_thread_info->exec_domain &&
314 __current_thread_info->exec_domain->signal_invmap)
315 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
317 if (__put_user(rsig, &frame->sig) < 0)
320 if (setup_sigcontext(&frame->sc, set->sig[0]))
323 if (_NSIG_WORDS > 1) {
324 if (__copy_to_user(frame->extramask, &set->sig[1],
325 sizeof(frame->extramask)))
329 /* Set up to return from userspace. If provided, use a stub
330 * already in userspace. */
331 if (ka->sa.sa_flags & SA_RESTORER) {
332 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
336 /* Set up the following code on the stack:
337 * setlos #__NR_sigreturn,gr7
340 if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
341 __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
342 __put_user(0xc0700000, &frame->retcode[1]))
345 flush_icache_range((unsigned long) frame->retcode,
346 (unsigned long) (frame->retcode + 2));
349 /* set up registers for signal handler */
350 regs->sp = (unsigned long) frame;
351 regs->lr = (unsigned long) &frame->retcode;
354 if (get_personality & FDPIC_FUNCPTRS) {
355 struct fdpic_func_descriptor __user *funcptr =
356 (struct fdpic_func_descriptor *) ka->sa.sa_handler;
357 __get_user(regs->pc, &funcptr->text);
358 __get_user(regs->gr15, &funcptr->GOT);
360 regs->pc = (unsigned long) ka->sa.sa_handler;
366 /* the tracer may want to single-step inside the handler */
367 if (test_thread_flag(TIF_SINGLESTEP))
368 ptrace_notify(SIGTRAP);
371 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
372 sig, current->comm, current->pid, frame, regs->pc,
379 force_sig(SIGSEGV, current);
382 } /* end setup_frame() */
384 /*****************************************************************************/
388 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
389 sigset_t *set, struct pt_regs * regs)
391 struct rt_sigframe __user *frame;
394 frame = get_sigframe(ka, regs, sizeof(*frame));
396 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
401 __current_thread_info->exec_domain &&
402 __current_thread_info->exec_domain->signal_invmap)
403 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
405 if (__put_user(rsig, &frame->sig) ||
406 __put_user(&frame->info, &frame->pinfo) ||
407 __put_user(&frame->uc, &frame->puc))
410 if (copy_siginfo_to_user(&frame->info, info))
413 /* Create the ucontext. */
414 if (__put_user(0, &frame->uc.uc_flags) ||
415 __put_user(0, &frame->uc.uc_link) ||
416 __put_user((void*)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
417 __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
418 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
421 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
424 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
427 /* Set up to return from userspace. If provided, use a stub
428 * already in userspace. */
429 if (ka->sa.sa_flags & SA_RESTORER) {
430 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
434 /* Set up the following code on the stack:
435 * setlos #__NR_sigreturn,gr7
438 if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
439 __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
440 __put_user(0xc0700000, &frame->retcode[1]))
443 flush_icache_range((unsigned long) frame->retcode,
444 (unsigned long) (frame->retcode + 2));
447 /* Set up registers for signal handler */
448 regs->sp = (unsigned long) frame;
449 regs->lr = (unsigned long) &frame->retcode;
451 regs->gr9 = (unsigned long) &frame->info;
453 if (get_personality & FDPIC_FUNCPTRS) {
454 struct fdpic_func_descriptor *funcptr =
455 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
456 __get_user(regs->pc, &funcptr->text);
457 __get_user(regs->gr15, &funcptr->GOT);
459 regs->pc = (unsigned long) ka->sa.sa_handler;
465 /* the tracer may want to single-step inside the handler */
466 if (test_thread_flag(TIF_SINGLESTEP))
467 ptrace_notify(SIGTRAP);
470 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
471 sig, current->comm, current->pid, frame, regs->pc,
478 force_sig(SIGSEGV, current);
481 } /* end setup_rt_frame() */
483 /*****************************************************************************/
485 * OK, we're invoking a handler
487 static int handle_signal(unsigned long sig, siginfo_t *info,
488 struct k_sigaction *ka, sigset_t *oldset,
489 struct pt_regs *regs)
493 /* Are we from a system call? */
494 if (in_syscall(regs)) {
495 /* If so, check system call restarting.. */
497 case -ERESTART_RESTARTBLOCK:
498 case -ERESTARTNOHAND:
503 if (!(ka->sa.sa_flags & SA_RESTART)) {
509 case -ERESTARTNOINTR:
510 regs->gr8 = regs->orig_gr8;
515 /* Set up the stack frame */
516 if (ka->sa.sa_flags & SA_SIGINFO)
517 ret = setup_rt_frame(sig, ka, info, oldset, regs);
519 ret = setup_frame(sig, ka, oldset, regs);
522 spin_lock_irq(¤t->sighand->siglock);
523 sigorsets(¤t->blocked, ¤t->blocked,
525 if (!(ka->sa.sa_flags & SA_NODEFER))
526 sigaddset(¤t->blocked, sig);
528 spin_unlock_irq(¤t->sighand->siglock);
533 } /* end handle_signal() */
535 /*****************************************************************************/
537 * Note that 'init' is a special process: it doesn't get signals it doesn't
538 * want to handle. Thus you cannot kill init even with a SIGKILL even by
541 int do_signal(struct pt_regs *regs, sigset_t *oldset)
543 struct k_sigaction ka;
548 * We want the common case to go fast, which
549 * is why we may in certain cases get here from
550 * kernel mode. Just return without doing anything
553 if (!user_mode(regs))
560 oldset = ¤t->blocked;
562 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
564 return handle_signal(signr, &info, &ka, oldset, regs);
567 /* Did we come from a system call? */
568 if (regs->syscallno >= 0) {
569 /* Restart the system call - no handlers present */
570 if (regs->gr8 == -ERESTARTNOHAND ||
571 regs->gr8 == -ERESTARTSYS ||
572 regs->gr8 == -ERESTARTNOINTR) {
573 regs->gr8 = regs->orig_gr8;
577 if (regs->gr8 == -ERESTART_RESTARTBLOCK){
578 regs->gr8 = __NR_restart_syscall;
585 } /* end do_signal() */
587 /*****************************************************************************/
589 * notification of userspace execution resumption
590 * - triggered by current->work.notify_resume
592 asmlinkage void do_notify_resume(__u32 thread_info_flags)
594 /* pending single-step? */
595 if (thread_info_flags & _TIF_SINGLESTEP)
596 clear_thread_flag(TIF_SINGLESTEP);
598 /* deal with pending signal delivery */
599 if (thread_info_flags & _TIF_SIGPENDING)
600 do_signal(__frame, NULL);
602 } /* end do_notify_resume() */