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
)
45 current
->saved_sigmask
= current
->blocked
;
48 siginitset(&blocked
, mask
);
49 set_current_blocked(&blocked
);
51 current
->state
= TASK_INTERRUPTIBLE
;
53 set_thread_flag(TIF_RESTORE_SIGMASK
);
54 return -ERESTARTNOHAND
;
57 asmlinkage
int sys_sigaction(int sig
,
58 const struct old_sigaction __user
*act
,
59 struct old_sigaction __user
*oact
)
61 struct k_sigaction new_ka
, old_ka
;
66 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
67 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
68 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
70 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
71 __get_user(mask
, &act
->sa_mask
);
72 siginitset(&new_ka
.sa
.sa_mask
, mask
);
75 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
78 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
79 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
80 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
82 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
83 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
90 int sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
)
92 return do_sigaltstack(uss
, uoss
, __frame
->sp
);
97 * Do a signal return; undo the signal stack.
102 __sigrestore_t pretcode
;
104 struct sigcontext sc
;
105 unsigned long extramask
[_NSIG_WORDS
-1];
111 __sigrestore_t pretcode
;
113 struct siginfo __user
*pinfo
;
120 static int restore_sigcontext(struct sigcontext __user
*sc
, int *_gr8
)
122 struct user_context
*user
= current
->thread
.user
;
123 unsigned long tbr
, psr
;
125 /* Always make any pending restarted system calls return -EINTR */
126 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
130 if (copy_from_user(user
, &sc
->sc_context
, sizeof(sc
->sc_context
)))
135 restore_user_regs(user
);
137 user
->i
.syscallno
= -1; /* disable syscall checks */
139 *_gr8
= user
->i
.gr
[8];
146 asmlinkage
int sys_sigreturn(void)
148 struct sigframe __user
*frame
= (struct sigframe __user
*) __frame
->sp
;
152 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
154 if (__get_user(set
.sig
[0], &frame
->sc
.sc_oldmask
))
157 if (_NSIG_WORDS
> 1 &&
158 __copy_from_user(&set
.sig
[1], &frame
->extramask
, sizeof(frame
->extramask
)))
161 sigdelsetmask(&set
, ~_BLOCKABLE
);
162 set_current_blocked(&set
);
164 if (restore_sigcontext(&frame
->sc
, &gr8
))
169 force_sig(SIGSEGV
, current
);
173 asmlinkage
int sys_rt_sigreturn(void)
175 struct rt_sigframe __user
*frame
= (struct rt_sigframe __user
*) __frame
->sp
;
179 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
181 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
184 sigdelsetmask(&set
, ~_BLOCKABLE
);
185 set_current_blocked(&set
);
187 if (restore_sigcontext(&frame
->uc
.uc_mcontext
, &gr8
))
190 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, __frame
->sp
) == -EFAULT
)
196 force_sig(SIGSEGV
, current
);
201 * Set up a signal frame
203 static int setup_sigcontext(struct sigcontext __user
*sc
, unsigned long mask
)
205 save_user_regs(current
->thread
.user
);
207 if (copy_to_user(&sc
->sc_context
, current
->thread
.user
, sizeof(sc
->sc_context
)) != 0)
210 /* non-iBCS2 extensions.. */
211 if (__put_user(mask
, &sc
->sc_oldmask
) < 0)
220 /*****************************************************************************/
222 * Determine which stack to use..
224 static inline void __user
*get_sigframe(struct k_sigaction
*ka
,
229 /* Default to using normal stack */
232 /* This is the X/Open sanctioned signal stack switching. */
233 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
234 if (! sas_ss_flags(sp
))
235 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
238 return (void __user
*) ((sp
- frame_size
) & ~7UL);
240 } /* end get_sigframe() */
242 /*****************************************************************************/
246 static int setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
)
248 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((__sigrestore_t
)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 the signal handler */
297 if (current
->personality
& FDPIC_FUNCPTRS
) {
298 struct fdpic_func_descriptor __user
*funcptr
=
299 (struct fdpic_func_descriptor __user
*) ka
->sa
.sa_handler
;
300 struct fdpic_func_descriptor desc
;
301 if (copy_from_user(&desc
, funcptr
, sizeof(desc
)))
303 __frame
->pc
= desc
.text
;
304 __frame
->gr15
= desc
.GOT
;
306 __frame
->pc
= (unsigned long) ka
->sa
.sa_handler
;
310 __frame
->sp
= (unsigned long) frame
;
311 __frame
->lr
= (unsigned long) &frame
->retcode
;
314 /* the tracer may want to single-step inside the handler */
315 if (test_thread_flag(TIF_SINGLESTEP
))
316 ptrace_notify(SIGTRAP
);
319 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
320 sig
, current
->comm
, current
->pid
, frame
, __frame
->pc
,
327 force_sigsegv(sig
, current
);
330 } /* end setup_frame() */
332 /*****************************************************************************/
336 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
339 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 if (current
->personality
& FDPIC_FUNCPTRS
) {
399 struct fdpic_func_descriptor __user
*funcptr
=
400 (struct fdpic_func_descriptor __user
*) ka
->sa
.sa_handler
;
401 struct fdpic_func_descriptor desc
;
402 if (copy_from_user(&desc
, funcptr
, sizeof(desc
)))
404 __frame
->pc
= desc
.text
;
405 __frame
->gr15
= desc
.GOT
;
407 __frame
->pc
= (unsigned long) ka
->sa
.sa_handler
;
411 __frame
->sp
= (unsigned long) frame
;
412 __frame
->lr
= (unsigned long) &frame
->retcode
;
414 __frame
->gr9
= (unsigned long) &frame
->info
;
416 /* the tracer may want to single-step inside the handler */
417 if (test_thread_flag(TIF_SINGLESTEP
))
418 ptrace_notify(SIGTRAP
);
421 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
422 sig
, current
->comm
, current
->pid
, frame
, __frame
->pc
,
429 force_sigsegv(sig
, current
);
432 } /* end setup_rt_frame() */
434 /*****************************************************************************/
436 * OK, we're invoking a handler
438 static int handle_signal(unsigned long sig
, siginfo_t
*info
,
439 struct k_sigaction
*ka
, sigset_t
*oldset
)
443 /* Are we from a system call? */
444 if (__frame
->syscallno
!= -1) {
445 /* If so, check system call restarting.. */
446 switch (__frame
->gr8
) {
447 case -ERESTART_RESTARTBLOCK
:
448 case -ERESTARTNOHAND
:
449 __frame
->gr8
= -EINTR
;
453 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
454 __frame
->gr8
= -EINTR
;
459 case -ERESTARTNOINTR
:
460 __frame
->gr8
= __frame
->orig_gr8
;
463 __frame
->syscallno
= -1;
466 /* Set up the stack frame */
467 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
468 ret
= setup_rt_frame(sig
, ka
, info
, oldset
);
470 ret
= setup_frame(sig
, ka
, oldset
);
473 block_sigmask(ka
, sig
);
477 } /* end handle_signal() */
479 /*****************************************************************************/
481 * Note that 'init' is a special process: it doesn't get signals it doesn't
482 * want to handle. Thus you cannot kill init even with a SIGKILL even by
485 static void do_signal(void)
487 struct k_sigaction ka
;
493 * We want the common case to go fast, which
494 * is why we may in certain cases get here from
495 * kernel mode. Just return without doing anything
498 if (!user_mode(__frame
))
504 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
505 oldset
= ¤t
->saved_sigmask
;
507 oldset
= ¤t
->blocked
;
509 signr
= get_signal_to_deliver(&info
, &ka
, __frame
, NULL
);
511 if (handle_signal(signr
, &info
, &ka
, oldset
) == 0) {
512 /* a signal was successfully delivered; the saved
513 * sigmask will have been stored in the signal frame,
514 * and will be restored by sigreturn, so we can simply
515 * clear the TIF_RESTORE_SIGMASK flag */
516 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
517 clear_thread_flag(TIF_RESTORE_SIGMASK
);
519 tracehook_signal_handler(signr
, &info
, &ka
, __frame
,
520 test_thread_flag(TIF_SINGLESTEP
));
527 /* Did we come from a system call? */
528 if (__frame
->syscallno
!= -1) {
529 /* Restart the system call - no handlers present */
530 switch (__frame
->gr8
) {
531 case -ERESTARTNOHAND
:
533 case -ERESTARTNOINTR
:
534 __frame
->gr8
= __frame
->orig_gr8
;
538 case -ERESTART_RESTARTBLOCK
:
539 __frame
->gr7
= __NR_restart_syscall
;
543 __frame
->syscallno
= -1;
546 /* if there's no signal to deliver, we just put the saved sigmask
548 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
549 clear_thread_flag(TIF_RESTORE_SIGMASK
);
550 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
553 } /* end do_signal() */
555 /*****************************************************************************/
557 * notification of userspace execution resumption
558 * - triggered by the TIF_WORK_MASK flags
560 asmlinkage
void do_notify_resume(__u32 thread_info_flags
)
562 /* pending single-step? */
563 if (thread_info_flags
& _TIF_SINGLESTEP
)
564 clear_thread_flag(TIF_SINGLESTEP
);
566 /* deal with pending signal delivery */
567 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
570 /* deal with notification on about to resume userspace execution */
571 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
572 clear_thread_flag(TIF_NOTIFY_RESUME
);
573 tracehook_notify_resume(__frame
);
574 if (current
->replacement_session_keyring
)
575 key_replace_session_keyring();
578 } /* end do_notify_resume() */