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 <linux/tracehook.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 __sigrestore_t pretcode;
103 struct sigcontext sc;
104 unsigned long extramask[_NSIG_WORDS-1];
110 __sigrestore_t pretcode;
112 struct siginfo __user *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;
124 /* Always make any pending restarted system calls return -EINTR */
125 current_thread_info()->restart_block.fn = do_no_restart_syscall;
129 if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
134 restore_user_regs(user);
136 user->i.syscallno = -1; /* disable syscall checks */
138 *_gr8 = user->i.gr[8];
145 asmlinkage int sys_sigreturn(void)
147 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
151 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
153 if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
156 if (_NSIG_WORDS > 1 &&
157 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
160 sigdelsetmask(&set, ~_BLOCKABLE);
161 spin_lock_irq(¤t->sighand->siglock);
162 current->blocked = set;
164 spin_unlock_irq(¤t->sighand->siglock);
166 if (restore_sigcontext(&frame->sc, &gr8))
171 force_sig(SIGSEGV, current);
175 asmlinkage int sys_rt_sigreturn(void)
177 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
181 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
183 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
186 sigdelsetmask(&set, ~_BLOCKABLE);
187 spin_lock_irq(¤t->sighand->siglock);
188 current->blocked = set;
190 spin_unlock_irq(¤t->sighand->siglock);
192 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
195 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
201 force_sig(SIGSEGV, current);
206 * Set up a signal frame
208 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
210 save_user_regs(current->thread.user);
212 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
215 /* non-iBCS2 extensions.. */
216 if (__put_user(mask, &sc->sc_oldmask) < 0)
225 /*****************************************************************************/
227 * Determine which stack to use..
229 static inline void __user *get_sigframe(struct k_sigaction *ka,
234 /* Default to using normal stack */
237 /* This is the X/Open sanctioned signal stack switching. */
238 if (ka->sa.sa_flags & SA_ONSTACK) {
239 if (! sas_ss_flags(sp))
240 sp = current->sas_ss_sp + current->sas_ss_size;
243 return (void __user *) ((sp - frame_size) & ~7UL);
245 } /* end get_sigframe() */
247 /*****************************************************************************/
251 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
253 struct sigframe __user *frame;
256 frame = get_sigframe(ka, sizeof(*frame));
258 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
263 __current_thread_info->exec_domain &&
264 __current_thread_info->exec_domain->signal_invmap)
265 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
267 if (__put_user(rsig, &frame->sig) < 0)
270 if (setup_sigcontext(&frame->sc, set->sig[0]))
273 if (_NSIG_WORDS > 1) {
274 if (__copy_to_user(frame->extramask, &set->sig[1],
275 sizeof(frame->extramask)))
279 /* Set up to return from userspace. If provided, use a stub
280 * already in userspace. */
281 if (ka->sa.sa_flags & SA_RESTORER) {
282 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
286 /* Set up the following code on the stack:
287 * setlos #__NR_sigreturn,gr7
290 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
291 __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
292 __put_user(0xc0700000, &frame->retcode[1]))
295 flush_icache_range((unsigned long) frame->retcode,
296 (unsigned long) (frame->retcode + 2));
299 /* set up registers for signal handler */
300 __frame->sp = (unsigned long) frame;
301 __frame->lr = (unsigned long) &frame->retcode;
304 if (current->personality & FDPIC_FUNCPTRS) {
305 struct fdpic_func_descriptor __user *funcptr =
306 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
307 __get_user(__frame->pc, &funcptr->text);
308 __get_user(__frame->gr15, &funcptr->GOT);
310 __frame->pc = (unsigned long) ka->sa.sa_handler;
316 /* the tracer may want to single-step inside the handler */
317 if (test_thread_flag(TIF_SINGLESTEP))
318 ptrace_notify(SIGTRAP);
321 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
322 sig, current->comm, current->pid, frame, __frame->pc,
329 force_sig(SIGSEGV, current);
332 } /* end setup_frame() */
334 /*****************************************************************************/
338 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
341 struct rt_sigframe __user *frame;
344 frame = get_sigframe(ka, sizeof(*frame));
346 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
351 __current_thread_info->exec_domain &&
352 __current_thread_info->exec_domain->signal_invmap)
353 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
355 if (__put_user(rsig, &frame->sig) ||
356 __put_user(&frame->info, &frame->pinfo) ||
357 __put_user(&frame->uc, &frame->puc))
360 if (copy_siginfo_to_user(&frame->info, info))
363 /* Create the ucontext. */
364 if (__put_user(0, &frame->uc.uc_flags) ||
365 __put_user(NULL, &frame->uc.uc_link) ||
366 __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
367 __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
368 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
371 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
374 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
377 /* Set up to return from userspace. If provided, use a stub
378 * already in userspace. */
379 if (ka->sa.sa_flags & SA_RESTORER) {
380 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
384 /* Set up the following code on the stack:
385 * setlos #__NR_sigreturn,gr7
388 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
389 __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
390 __put_user(0xc0700000, &frame->retcode[1]))
393 flush_icache_range((unsigned long) frame->retcode,
394 (unsigned long) (frame->retcode + 2));
397 /* Set up registers for signal handler */
398 __frame->sp = (unsigned long) frame;
399 __frame->lr = (unsigned long) &frame->retcode;
401 __frame->gr9 = (unsigned long) &frame->info;
403 if (current->personality & FDPIC_FUNCPTRS) {
404 struct fdpic_func_descriptor __user *funcptr =
405 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
406 __get_user(__frame->pc, &funcptr->text);
407 __get_user(__frame->gr15, &funcptr->GOT);
409 __frame->pc = (unsigned long) ka->sa.sa_handler;
415 /* the tracer may want to single-step inside the handler */
416 if (test_thread_flag(TIF_SINGLESTEP))
417 ptrace_notify(SIGTRAP);
420 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
421 sig, current->comm, current->pid, frame, __frame->pc,
428 force_sig(SIGSEGV, current);
431 } /* end setup_rt_frame() */
433 /*****************************************************************************/
435 * OK, we're invoking a handler
437 static int handle_signal(unsigned long sig, siginfo_t *info,
438 struct k_sigaction *ka, sigset_t *oldset)
442 /* Are we from a system call? */
443 if (in_syscall(__frame)) {
444 /* If so, check system call restarting.. */
445 switch (__frame->gr8) {
446 case -ERESTART_RESTARTBLOCK:
447 case -ERESTARTNOHAND:
448 __frame->gr8 = -EINTR;
452 if (!(ka->sa.sa_flags & SA_RESTART)) {
453 __frame->gr8 = -EINTR;
458 case -ERESTARTNOINTR:
459 __frame->gr8 = __frame->orig_gr8;
464 /* Set up the stack frame */
465 if (ka->sa.sa_flags & SA_SIGINFO)
466 ret = setup_rt_frame(sig, ka, info, oldset);
468 ret = setup_frame(sig, ka, oldset);
471 spin_lock_irq(¤t->sighand->siglock);
472 sigorsets(¤t->blocked, ¤t->blocked,
474 if (!(ka->sa.sa_flags & SA_NODEFER))
475 sigaddset(¤t->blocked, sig);
477 spin_unlock_irq(¤t->sighand->siglock);
482 } /* end handle_signal() */
484 /*****************************************************************************/
486 * Note that 'init' is a special process: it doesn't get signals it doesn't
487 * want to handle. Thus you cannot kill init even with a SIGKILL even by
490 static void do_signal(void)
492 struct k_sigaction ka;
498 * We want the common case to go fast, which
499 * is why we may in certain cases get here from
500 * kernel mode. Just return without doing anything
503 if (!user_mode(__frame))
509 if (test_thread_flag(TIF_RESTORE_SIGMASK))
510 oldset = ¤t->saved_sigmask;
512 oldset = ¤t->blocked;
514 signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
516 if (handle_signal(signr, &info, &ka, oldset) == 0) {
517 /* a signal was successfully delivered; the saved
518 * sigmask will have been stored in the signal frame,
519 * and will be restored by sigreturn, so we can simply
520 * clear the TIF_RESTORE_SIGMASK flag */
521 if (test_thread_flag(TIF_RESTORE_SIGMASK))
522 clear_thread_flag(TIF_RESTORE_SIGMASK);
524 tracehook_signal_handler(signr, &info, &ka, __frame,
525 test_thread_flag(TIF_SINGLESTEP));
532 /* Did we come from a system call? */
533 if (__frame->syscallno != -1) {
534 /* Restart the system call - no handlers present */
535 switch (__frame->gr8) {
536 case -ERESTARTNOHAND:
538 case -ERESTARTNOINTR:
539 __frame->gr8 = __frame->orig_gr8;
543 case -ERESTART_RESTARTBLOCK:
544 __frame->gr8 = __NR_restart_syscall;
550 /* if there's no signal to deliver, we just put the saved sigmask
552 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
553 clear_thread_flag(TIF_RESTORE_SIGMASK);
554 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
557 } /* end do_signal() */
559 /*****************************************************************************/
561 * notification of userspace execution resumption
562 * - triggered by the TIF_WORK_MASK flags
564 asmlinkage void do_notify_resume(__u32 thread_info_flags)
566 /* pending single-step? */
567 if (thread_info_flags & _TIF_SINGLESTEP)
568 clear_thread_flag(TIF_SINGLESTEP);
570 /* deal with pending signal delivery */
571 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
574 /* deal with notification on about to resume userspace execution */
575 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
576 clear_thread_flag(TIF_NOTIFY_RESUME);
577 tracehook_notify_resume(__frame);
578 if (current->replacement_session_keyring)
579 key_replace_session_keyring();
582 } /* end do_notify_resume() */