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 {
39 * Atomically swap in the new signal mask, and wait for a signal.
41 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
44 spin_lock_irq(¤t->sighand->siglock);
45 current->saved_sigmask = current->blocked;
46 siginitset(¤t->blocked, mask);
48 spin_unlock_irq(¤t->sighand->siglock);
50 current->state = TASK_INTERRUPTIBLE;
52 set_thread_flag(TIF_RESTORE_SIGMASK);
53 return -ERESTARTNOHAND;
56 asmlinkage int sys_sigaction(int sig,
57 const struct old_sigaction __user *act,
58 struct old_sigaction __user *oact)
60 struct k_sigaction new_ka, old_ka;
65 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
66 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
67 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
69 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
70 __get_user(mask, &act->sa_mask);
71 siginitset(&new_ka.sa.sa_mask, mask);
74 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
77 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
78 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
79 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
81 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
82 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
89 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
91 return do_sigaltstack(uss, uoss, __frame->sp);
96 * Do a signal return; undo the signal stack.
101 void (*pretcode)(void);
103 struct sigcontext sc;
104 unsigned long extramask[_NSIG_WORDS-1];
110 void (*pretcode)(void);
112 struct siginfo *pinfo;
119 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
121 struct user_context *user = current->thread.user;
122 unsigned long tbr, psr;
126 if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
131 restore_user_regs(user);
133 user->i.syscallno = -1; /* disable syscall checks */
135 *_gr8 = user->i.gr[8];
142 asmlinkage int sys_sigreturn(void)
144 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
148 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
150 if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
153 if (_NSIG_WORDS > 1 &&
154 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
157 sigdelsetmask(&set, ~_BLOCKABLE);
158 spin_lock_irq(¤t->sighand->siglock);
159 current->blocked = set;
161 spin_unlock_irq(¤t->sighand->siglock);
163 if (restore_sigcontext(&frame->sc, &gr8))
168 force_sig(SIGSEGV, current);
172 asmlinkage int sys_rt_sigreturn(void)
174 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
178 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
180 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
183 sigdelsetmask(&set, ~_BLOCKABLE);
184 spin_lock_irq(¤t->sighand->siglock);
185 current->blocked = set;
187 spin_unlock_irq(¤t->sighand->siglock);
189 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
192 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
198 force_sig(SIGSEGV, current);
203 * Set up a signal frame
205 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
207 save_user_regs(current->thread.user);
209 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
212 /* non-iBCS2 extensions.. */
213 if (__put_user(mask, &sc->sc_oldmask) < 0)
222 /*****************************************************************************/
224 * Determine which stack to use..
226 static inline void __user *get_sigframe(struct k_sigaction *ka,
231 /* Default to using normal stack */
234 /* This is the X/Open sanctioned signal stack switching. */
235 if (ka->sa.sa_flags & SA_ONSTACK) {
236 if (! on_sig_stack(sp))
237 sp = current->sas_ss_sp + current->sas_ss_size;
240 return (void __user *) ((sp - frame_size) & ~7UL);
242 } /* end get_sigframe() */
244 /*****************************************************************************/
248 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
250 struct sigframe __user *frame;
253 frame = get_sigframe(ka, sizeof(*frame));
255 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
260 __current_thread_info->exec_domain &&
261 __current_thread_info->exec_domain->signal_invmap)
262 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
264 if (__put_user(rsig, &frame->sig) < 0)
267 if (setup_sigcontext(&frame->sc, set->sig[0]))
270 if (_NSIG_WORDS > 1) {
271 if (__copy_to_user(frame->extramask, &set->sig[1],
272 sizeof(frame->extramask)))
276 /* Set up to return from userspace. If provided, use a stub
277 * already in userspace. */
278 if (ka->sa.sa_flags & SA_RESTORER) {
279 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
283 /* Set up the following code on the stack:
284 * setlos #__NR_sigreturn,gr7
287 if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
288 __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
289 __put_user(0xc0700000, &frame->retcode[1]))
292 flush_icache_range((unsigned long) frame->retcode,
293 (unsigned long) (frame->retcode + 2));
296 /* set up registers for signal handler */
297 __frame->sp = (unsigned long) frame;
298 __frame->lr = (unsigned long) &frame->retcode;
301 if (get_personality & FDPIC_FUNCPTRS) {
302 struct fdpic_func_descriptor __user *funcptr =
303 (struct fdpic_func_descriptor *) ka->sa.sa_handler;
304 __get_user(__frame->pc, &funcptr->text);
305 __get_user(__frame->gr15, &funcptr->GOT);
307 __frame->pc = (unsigned long) ka->sa.sa_handler;
313 /* the tracer may want to single-step inside the handler */
314 if (test_thread_flag(TIF_SINGLESTEP))
315 ptrace_notify(SIGTRAP);
318 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
319 sig, current->comm, current->pid, frame, __frame->pc,
326 force_sig(SIGSEGV, current);
329 } /* end setup_frame() */
331 /*****************************************************************************/
335 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
338 struct rt_sigframe __user *frame;
341 frame = get_sigframe(ka, sizeof(*frame));
343 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
348 __current_thread_info->exec_domain &&
349 __current_thread_info->exec_domain->signal_invmap)
350 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
352 if (__put_user(rsig, &frame->sig) ||
353 __put_user(&frame->info, &frame->pinfo) ||
354 __put_user(&frame->uc, &frame->puc))
357 if (copy_siginfo_to_user(&frame->info, info))
360 /* Create the ucontext. */
361 if (__put_user(0, &frame->uc.uc_flags) ||
362 __put_user(0, &frame->uc.uc_link) ||
363 __put_user((void*)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
364 __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
365 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
368 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
371 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
374 /* Set up to return from userspace. If provided, use a stub
375 * already in userspace. */
376 if (ka->sa.sa_flags & SA_RESTORER) {
377 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
381 /* Set up the following code on the stack:
382 * setlos #__NR_sigreturn,gr7
385 if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
386 __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
387 __put_user(0xc0700000, &frame->retcode[1]))
390 flush_icache_range((unsigned long) frame->retcode,
391 (unsigned long) (frame->retcode + 2));
394 /* Set up registers for signal handler */
395 __frame->sp = (unsigned long) frame;
396 __frame->lr = (unsigned long) &frame->retcode;
398 __frame->gr9 = (unsigned long) &frame->info;
400 if (get_personality & FDPIC_FUNCPTRS) {
401 struct fdpic_func_descriptor *funcptr =
402 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
403 __get_user(__frame->pc, &funcptr->text);
404 __get_user(__frame->gr15, &funcptr->GOT);
406 __frame->pc = (unsigned long) ka->sa.sa_handler;
412 /* the tracer may want to single-step inside the handler */
413 if (test_thread_flag(TIF_SINGLESTEP))
414 ptrace_notify(SIGTRAP);
417 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
418 sig, current->comm, current->pid, frame, __frame->pc,
425 force_sig(SIGSEGV, current);
428 } /* end setup_rt_frame() */
430 /*****************************************************************************/
432 * OK, we're invoking a handler
434 static int handle_signal(unsigned long sig, siginfo_t *info,
435 struct k_sigaction *ka, sigset_t *oldset)
439 /* Are we from a system call? */
440 if (in_syscall(__frame)) {
441 /* If so, check system call restarting.. */
442 switch (__frame->gr8) {
443 case -ERESTART_RESTARTBLOCK:
444 case -ERESTARTNOHAND:
445 __frame->gr8 = -EINTR;
449 if (!(ka->sa.sa_flags & SA_RESTART)) {
450 __frame->gr8 = -EINTR;
455 case -ERESTARTNOINTR:
456 __frame->gr8 = __frame->orig_gr8;
461 /* Set up the stack frame */
462 if (ka->sa.sa_flags & SA_SIGINFO)
463 ret = setup_rt_frame(sig, ka, info, oldset);
465 ret = setup_frame(sig, ka, oldset);
468 spin_lock_irq(¤t->sighand->siglock);
469 sigorsets(¤t->blocked, ¤t->blocked,
471 if (!(ka->sa.sa_flags & SA_NODEFER))
472 sigaddset(¤t->blocked, sig);
474 spin_unlock_irq(¤t->sighand->siglock);
479 } /* end handle_signal() */
481 /*****************************************************************************/
483 * Note that 'init' is a special process: it doesn't get signals it doesn't
484 * want to handle. Thus you cannot kill init even with a SIGKILL even by
487 static void do_signal(void)
489 struct k_sigaction ka;
495 * We want the common case to go fast, which
496 * is why we may in certain cases get here from
497 * kernel mode. Just return without doing anything
500 if (!user_mode(__frame))
506 if (test_thread_flag(TIF_RESTORE_SIGMASK))
507 oldset = ¤t->saved_sigmask;
509 oldset = ¤t->blocked;
511 signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
513 if (handle_signal(signr, &info, &ka, oldset) == 0) {
514 /* a signal was successfully delivered; the saved
515 * sigmask will have been stored in the signal frame,
516 * and will be restored by sigreturn, so we can simply
517 * clear the TIF_RESTORE_SIGMASK flag */
518 if (test_thread_flag(TIF_RESTORE_SIGMASK))
519 clear_thread_flag(TIF_RESTORE_SIGMASK);
526 /* Did we come from a system call? */
527 if (__frame->syscallno >= 0) {
528 /* Restart the system call - no handlers present */
529 switch (__frame->gr8) {
530 case -ERESTARTNOHAND:
532 case -ERESTARTNOINTR:
533 __frame->gr8 = __frame->orig_gr8;
537 case -ERESTART_RESTARTBLOCK:
538 __frame->gr8 = __NR_restart_syscall;
544 /* if there's no signal to deliver, we just put the saved sigmask
546 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
547 clear_thread_flag(TIF_RESTORE_SIGMASK);
548 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
551 } /* end do_signal() */
553 /*****************************************************************************/
555 * notification of userspace execution resumption
556 * - triggered by the TIF_WORK_MASK flags
558 asmlinkage void do_notify_resume(__u32 thread_info_flags)
560 /* pending single-step? */
561 if (thread_info_flags & _TIF_SINGLESTEP)
562 clear_thread_flag(TIF_SINGLESTEP);
564 /* deal with pending signal delivery */
565 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
568 } /* end do_notify_resume() */