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/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/personality.h>
23 #include <linux/freezer.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/cacheflush.h>
30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32 struct fdpic_func_descriptor {
38 * Atomically swap in the new signal mask, and wait for a signal.
40 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
43 spin_lock_irq(¤t->sighand->siglock);
44 current->saved_sigmask = current->blocked;
45 siginitset(¤t->blocked, mask);
47 spin_unlock_irq(¤t->sighand->siglock);
49 current->state = TASK_INTERRUPTIBLE;
51 set_thread_flag(TIF_RESTORE_SIGMASK);
52 return -ERESTARTNOHAND;
55 asmlinkage int sys_sigaction(int sig,
56 const struct old_sigaction __user *act,
57 struct old_sigaction __user *oact)
59 struct k_sigaction new_ka, old_ka;
64 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
65 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
66 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
68 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
69 __get_user(mask, &act->sa_mask);
70 siginitset(&new_ka.sa.sa_mask, mask);
73 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
76 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
77 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
78 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
80 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
81 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
88 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
90 return do_sigaltstack(uss, uoss, __frame->sp);
95 * Do a signal return; undo the signal stack.
100 __sigrestore_t pretcode;
102 struct sigcontext sc;
103 unsigned long extramask[_NSIG_WORDS-1];
109 __sigrestore_t pretcode;
111 struct siginfo __user *pinfo;
118 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
120 struct user_context *user = current->thread.user;
121 unsigned long tbr, psr;
125 if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
130 restore_user_regs(user);
132 user->i.syscallno = -1; /* disable syscall checks */
134 *_gr8 = user->i.gr[8];
141 asmlinkage int sys_sigreturn(void)
143 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
147 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
149 if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
152 if (_NSIG_WORDS > 1 &&
153 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
156 sigdelsetmask(&set, ~_BLOCKABLE);
157 spin_lock_irq(¤t->sighand->siglock);
158 current->blocked = set;
160 spin_unlock_irq(¤t->sighand->siglock);
162 if (restore_sigcontext(&frame->sc, &gr8))
167 force_sig(SIGSEGV, current);
171 asmlinkage int sys_rt_sigreturn(void)
173 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
177 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
179 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
182 sigdelsetmask(&set, ~_BLOCKABLE);
183 spin_lock_irq(¤t->sighand->siglock);
184 current->blocked = set;
186 spin_unlock_irq(¤t->sighand->siglock);
188 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
191 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
197 force_sig(SIGSEGV, current);
202 * Set up a signal frame
204 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
206 save_user_regs(current->thread.user);
208 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
211 /* non-iBCS2 extensions.. */
212 if (__put_user(mask, &sc->sc_oldmask) < 0)
221 /*****************************************************************************/
223 * Determine which stack to use..
225 static inline void __user *get_sigframe(struct k_sigaction *ka,
230 /* Default to using normal stack */
233 /* This is the X/Open sanctioned signal stack switching. */
234 if (ka->sa.sa_flags & SA_ONSTACK) {
235 if (! sas_ss_flags(sp))
236 sp = current->sas_ss_sp + current->sas_ss_size;
239 return (void __user *) ((sp - frame_size) & ~7UL);
241 } /* end get_sigframe() */
243 /*****************************************************************************/
247 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
249 struct sigframe __user *frame;
252 frame = get_sigframe(ka, sizeof(*frame));
254 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
259 __current_thread_info->exec_domain &&
260 __current_thread_info->exec_domain->signal_invmap)
261 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
263 if (__put_user(rsig, &frame->sig) < 0)
266 if (setup_sigcontext(&frame->sc, set->sig[0]))
269 if (_NSIG_WORDS > 1) {
270 if (__copy_to_user(frame->extramask, &set->sig[1],
271 sizeof(frame->extramask)))
275 /* Set up to return from userspace. If provided, use a stub
276 * already in userspace. */
277 if (ka->sa.sa_flags & SA_RESTORER) {
278 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
282 /* Set up the following code on the stack:
283 * setlos #__NR_sigreturn,gr7
286 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
287 __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
288 __put_user(0xc0700000, &frame->retcode[1]))
291 flush_icache_range((unsigned long) frame->retcode,
292 (unsigned long) (frame->retcode + 2));
295 /* set up registers for signal handler */
296 __frame->sp = (unsigned long) frame;
297 __frame->lr = (unsigned long) &frame->retcode;
300 if (current->personality & FDPIC_FUNCPTRS) {
301 struct fdpic_func_descriptor __user *funcptr =
302 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
303 __get_user(__frame->pc, &funcptr->text);
304 __get_user(__frame->gr15, &funcptr->GOT);
306 __frame->pc = (unsigned long) ka->sa.sa_handler;
312 /* the tracer may want to single-step inside the handler */
313 if (test_thread_flag(TIF_SINGLESTEP))
314 ptrace_notify(SIGTRAP);
317 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
318 sig, current->comm, current->pid, frame, __frame->pc,
325 force_sig(SIGSEGV, current);
328 } /* end setup_frame() */
330 /*****************************************************************************/
334 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
337 struct rt_sigframe __user *frame;
340 frame = get_sigframe(ka, sizeof(*frame));
342 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
347 __current_thread_info->exec_domain &&
348 __current_thread_info->exec_domain->signal_invmap)
349 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
351 if (__put_user(rsig, &frame->sig) ||
352 __put_user(&frame->info, &frame->pinfo) ||
353 __put_user(&frame->uc, &frame->puc))
356 if (copy_siginfo_to_user(&frame->info, info))
359 /* Create the ucontext. */
360 if (__put_user(0, &frame->uc.uc_flags) ||
361 __put_user(NULL, &frame->uc.uc_link) ||
362 __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
363 __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
364 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
367 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
370 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
373 /* Set up to return from userspace. If provided, use a stub
374 * already in userspace. */
375 if (ka->sa.sa_flags & SA_RESTORER) {
376 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
380 /* Set up the following code on the stack:
381 * setlos #__NR_sigreturn,gr7
384 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
385 __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
386 __put_user(0xc0700000, &frame->retcode[1]))
389 flush_icache_range((unsigned long) frame->retcode,
390 (unsigned long) (frame->retcode + 2));
393 /* Set up registers for signal handler */
394 __frame->sp = (unsigned long) frame;
395 __frame->lr = (unsigned long) &frame->retcode;
397 __frame->gr9 = (unsigned long) &frame->info;
399 if (current->personality & FDPIC_FUNCPTRS) {
400 struct fdpic_func_descriptor __user *funcptr =
401 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
402 __get_user(__frame->pc, &funcptr->text);
403 __get_user(__frame->gr15, &funcptr->GOT);
405 __frame->pc = (unsigned long) ka->sa.sa_handler;
411 /* the tracer may want to single-step inside the handler */
412 if (test_thread_flag(TIF_SINGLESTEP))
413 ptrace_notify(SIGTRAP);
416 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
417 sig, current->comm, current->pid, frame, __frame->pc,
424 force_sig(SIGSEGV, current);
427 } /* end setup_rt_frame() */
429 /*****************************************************************************/
431 * OK, we're invoking a handler
433 static int handle_signal(unsigned long sig, siginfo_t *info,
434 struct k_sigaction *ka, sigset_t *oldset)
438 /* Are we from a system call? */
439 if (in_syscall(__frame)) {
440 /* If so, check system call restarting.. */
441 switch (__frame->gr8) {
442 case -ERESTART_RESTARTBLOCK:
443 case -ERESTARTNOHAND:
444 __frame->gr8 = -EINTR;
448 if (!(ka->sa.sa_flags & SA_RESTART)) {
449 __frame->gr8 = -EINTR;
454 case -ERESTARTNOINTR:
455 __frame->gr8 = __frame->orig_gr8;
460 /* Set up the stack frame */
461 if (ka->sa.sa_flags & SA_SIGINFO)
462 ret = setup_rt_frame(sig, ka, info, oldset);
464 ret = setup_frame(sig, ka, oldset);
467 spin_lock_irq(¤t->sighand->siglock);
468 sigorsets(¤t->blocked, ¤t->blocked,
470 if (!(ka->sa.sa_flags & SA_NODEFER))
471 sigaddset(¤t->blocked, sig);
473 spin_unlock_irq(¤t->sighand->siglock);
478 } /* end handle_signal() */
480 /*****************************************************************************/
482 * Note that 'init' is a special process: it doesn't get signals it doesn't
483 * want to handle. Thus you cannot kill init even with a SIGKILL even by
486 static void do_signal(void)
488 struct k_sigaction ka;
494 * We want the common case to go fast, which
495 * is why we may in certain cases get here from
496 * kernel mode. Just return without doing anything
499 if (!user_mode(__frame))
505 if (test_thread_flag(TIF_RESTORE_SIGMASK))
506 oldset = ¤t->saved_sigmask;
508 oldset = ¤t->blocked;
510 signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
512 if (handle_signal(signr, &info, &ka, oldset) == 0) {
513 /* a signal was successfully delivered; the saved
514 * sigmask will have been stored in the signal frame,
515 * and will be restored by sigreturn, so we can simply
516 * clear the TIF_RESTORE_SIGMASK flag */
517 if (test_thread_flag(TIF_RESTORE_SIGMASK))
518 clear_thread_flag(TIF_RESTORE_SIGMASK);
525 /* Did we come from a system call? */
526 if (__frame->syscallno >= 0) {
527 /* Restart the system call - no handlers present */
528 switch (__frame->gr8) {
529 case -ERESTARTNOHAND:
531 case -ERESTARTNOINTR:
532 __frame->gr8 = __frame->orig_gr8;
536 case -ERESTART_RESTARTBLOCK:
537 __frame->gr8 = __NR_restart_syscall;
543 /* if there's no signal to deliver, we just put the saved sigmask
545 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
546 clear_thread_flag(TIF_RESTORE_SIGMASK);
547 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
550 } /* end do_signal() */
552 /*****************************************************************************/
554 * notification of userspace execution resumption
555 * - triggered by the TIF_WORK_MASK flags
557 asmlinkage void do_notify_resume(__u32 thread_info_flags)
559 /* pending single-step? */
560 if (thread_info_flags & _TIF_SINGLESTEP)
561 clear_thread_flag(TIF_SINGLESTEP);
563 /* deal with pending signal delivery */
564 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
567 } /* end do_notify_resume() */