2 * linux/arch/arm/kernel/signal.c
4 * Copyright (C) 1995, 1996 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/config.h>
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/signal.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/stddef.h>
21 #include <linux/unistd.h>
22 #include <linux/tty.h>
24 #include <asm/pgalloc.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32 #define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
33 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
35 asmlinkage
int sys_wait4(pid_t pid
, unsigned long * stat_addr
,
36 int options
, unsigned long *ru
);
37 asmlinkage
int do_signal(sigset_t
*oldset
, struct pt_regs
* regs
, int syscall
);
39 int copy_siginfo_to_user(siginfo_t
*to
, siginfo_t
*from
)
43 if (!access_ok (VERIFY_WRITE
, to
, sizeof(siginfo_t
)))
46 if (from
->si_code
< 0)
47 return __copy_to_user(to
, from
, sizeof(siginfo_t
));
49 /* If you change siginfo_t structure, please be sure
50 this code is fixed accordingly.
51 It should never copy any pad contained in the structure
52 to avoid security leaks, but must copy the generic
53 3 ints plus the relevant union member. */
54 err
= __put_user(from
->si_signo
, &to
->si_signo
);
55 err
|= __put_user(from
->si_errno
, &to
->si_errno
);
56 err
|= __put_user((short)from
->si_code
, &to
->si_code
);
57 /* First 32bits of unions are always present. */
58 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
59 switch (from
->si_code
>> 16) {
60 case __SI_FAULT
>> 16:
63 err
|= __put_user(from
->si_utime
, &to
->si_utime
);
64 err
|= __put_user(from
->si_stime
, &to
->si_stime
);
65 err
|= __put_user(from
->si_status
, &to
->si_status
);
67 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
69 /* case __SI_RT: This is not generated by the kernel as of now. */
76 * atomically swap in the new signal mask, and wait for a signal.
78 asmlinkage
int sys_sigsuspend(int restart
, unsigned long oldmask
, old_sigset_t mask
, struct pt_regs
*regs
)
83 spin_lock_irq(¤t
->sigmask_lock
);
84 saveset
= current
->blocked
;
85 siginitset(¤t
->blocked
, mask
);
86 recalc_sigpending(current
);
87 spin_unlock_irq(¤t
->sigmask_lock
);
88 regs
->ARM_r0
= -EINTR
;
91 current
->state
= TASK_INTERRUPTIBLE
;
93 if (do_signal(&saveset
, regs
, 0))
99 sys_rt_sigsuspend(sigset_t
*unewset
, size_t sigsetsize
, struct pt_regs
*regs
)
101 sigset_t saveset
, newset
;
103 /* XXX: Don't preclude handling different sized sigset_t's. */
104 if (sigsetsize
!= sizeof(sigset_t
))
107 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
109 sigdelsetmask(&newset
, ~_BLOCKABLE
);
111 spin_lock_irq(¤t
->sigmask_lock
);
112 saveset
= current
->blocked
;
113 current
->blocked
= newset
;
114 recalc_sigpending(current
);
115 spin_unlock_irq(¤t
->sigmask_lock
);
116 regs
->ARM_r0
= -EINTR
;
119 current
->state
= TASK_INTERRUPTIBLE
;
121 if (do_signal(&saveset
, regs
, 0))
127 sys_sigaction(int sig
, const struct old_sigaction
*act
,
128 struct old_sigaction
*oact
)
130 struct k_sigaction new_ka
, old_ka
;
135 if (verify_area(VERIFY_READ
, act
, sizeof(*act
)) ||
136 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
137 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
139 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
140 __get_user(mask
, &act
->sa_mask
);
141 siginitset(&new_ka
.sa
.sa_mask
, mask
);
144 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
147 if (verify_area(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
148 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
149 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
151 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
152 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
159 * Do a signal return; undo the signal stack.
163 struct sigcontext sc
;
164 unsigned long extramask
[_NSIG_WORDS
-1];
165 unsigned long retcode
;
170 struct siginfo
*pinfo
;
174 unsigned long retcode
;
178 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*sc
)
182 __get_user_error(regs
->ARM_r0
, &sc
->arm_r0
, err
);
183 __get_user_error(regs
->ARM_r1
, &sc
->arm_r1
, err
);
184 __get_user_error(regs
->ARM_r2
, &sc
->arm_r2
, err
);
185 __get_user_error(regs
->ARM_r3
, &sc
->arm_r3
, err
);
186 __get_user_error(regs
->ARM_r4
, &sc
->arm_r4
, err
);
187 __get_user_error(regs
->ARM_r5
, &sc
->arm_r5
, err
);
188 __get_user_error(regs
->ARM_r6
, &sc
->arm_r6
, err
);
189 __get_user_error(regs
->ARM_r7
, &sc
->arm_r7
, err
);
190 __get_user_error(regs
->ARM_r8
, &sc
->arm_r8
, err
);
191 __get_user_error(regs
->ARM_r9
, &sc
->arm_r9
, err
);
192 __get_user_error(regs
->ARM_r10
, &sc
->arm_r10
, err
);
193 __get_user_error(regs
->ARM_fp
, &sc
->arm_fp
, err
);
194 __get_user_error(regs
->ARM_ip
, &sc
->arm_ip
, err
);
195 __get_user_error(regs
->ARM_sp
, &sc
->arm_sp
, err
);
196 __get_user_error(regs
->ARM_lr
, &sc
->arm_lr
, err
);
197 __get_user_error(regs
->ARM_pc
, &sc
->arm_pc
, err
);
199 __get_user_error(regs
->ARM_cpsr
, &sc
->arm_cpsr
, err
);
202 err
|= !valid_user_regs(regs
);
207 asmlinkage
int sys_sigreturn(struct pt_regs
*regs
)
209 struct sigframe
*frame
;
213 * Since we stacked the signal on a word boundary,
214 * then 'sp' should be word aligned here. If it's
215 * not, then the user is trying to mess with us.
217 if (regs
->ARM_sp
& 3)
220 frame
= (struct sigframe
*)regs
->ARM_sp
;
222 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
224 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
226 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
227 sizeof(frame
->extramask
))))
230 sigdelsetmask(&set
, ~_BLOCKABLE
);
231 spin_lock_irq(¤t
->sigmask_lock
);
232 current
->blocked
= set
;
233 recalc_sigpending(current
);
234 spin_unlock_irq(¤t
->sigmask_lock
);
236 if (restore_sigcontext(regs
, &frame
->sc
))
239 /* Send SIGTRAP if we're single-stepping */
240 if (ptrace_cancel_bpt(current
))
241 send_sig(SIGTRAP
, current
, 1);
246 force_sig(SIGSEGV
, current
);
250 asmlinkage
int sys_rt_sigreturn(struct pt_regs
*regs
)
252 struct rt_sigframe
*frame
;
256 * Since we stacked the signal on a word boundary,
257 * then 'sp' should be word aligned here. If it's
258 * not, then the user is trying to mess with us.
260 if (regs
->ARM_sp
& 3)
263 frame
= (struct rt_sigframe
*)regs
->ARM_sp
;
265 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
267 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
270 sigdelsetmask(&set
, ~_BLOCKABLE
);
271 spin_lock_irq(¤t
->sigmask_lock
);
272 current
->blocked
= set
;
273 recalc_sigpending(current
);
274 spin_unlock_irq(¤t
->sigmask_lock
);
276 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
279 /* Send SIGTRAP if we're single-stepping */
280 if (ptrace_cancel_bpt(current
))
281 send_sig(SIGTRAP
, current
, 1);
286 force_sig(SIGSEGV
, current
);
291 setup_sigcontext(struct sigcontext
*sc
, /*struct _fpstate *fpstate,*/
292 struct pt_regs
*regs
, unsigned long mask
)
296 __put_user_error(regs
->ARM_r0
, &sc
->arm_r0
, err
);
297 __put_user_error(regs
->ARM_r1
, &sc
->arm_r1
, err
);
298 __put_user_error(regs
->ARM_r2
, &sc
->arm_r2
, err
);
299 __put_user_error(regs
->ARM_r3
, &sc
->arm_r3
, err
);
300 __put_user_error(regs
->ARM_r4
, &sc
->arm_r4
, err
);
301 __put_user_error(regs
->ARM_r5
, &sc
->arm_r5
, err
);
302 __put_user_error(regs
->ARM_r6
, &sc
->arm_r6
, err
);
303 __put_user_error(regs
->ARM_r7
, &sc
->arm_r7
, err
);
304 __put_user_error(regs
->ARM_r8
, &sc
->arm_r8
, err
);
305 __put_user_error(regs
->ARM_r9
, &sc
->arm_r9
, err
);
306 __put_user_error(regs
->ARM_r10
, &sc
->arm_r10
, err
);
307 __put_user_error(regs
->ARM_fp
, &sc
->arm_fp
, err
);
308 __put_user_error(regs
->ARM_ip
, &sc
->arm_ip
, err
);
309 __put_user_error(regs
->ARM_sp
, &sc
->arm_sp
, err
);
310 __put_user_error(regs
->ARM_lr
, &sc
->arm_lr
, err
);
311 __put_user_error(regs
->ARM_pc
, &sc
->arm_pc
, err
);
313 __put_user_error(regs
->ARM_cpsr
, &sc
->arm_cpsr
, err
);
316 __put_user_error(current
->thread
.trap_no
, &sc
->trap_no
, err
);
317 __put_user_error(current
->thread
.error_code
, &sc
->error_code
, err
);
318 __put_user_error(current
->thread
.address
, &sc
->fault_address
, err
);
319 __put_user_error(mask
, &sc
->oldmask
, err
);
324 static inline void *get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
325 unsigned long framesize
)
327 unsigned long sp
= regs
->ARM_sp
;
330 * This is the X/Open sanctioned signal stack switching.
332 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && ! on_sig_stack(sp
))
333 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
336 * No matter what happens, 'sp' must be word
337 * aligned otherwise nasty things could happen
341 return (void *)(sp
- framesize
);
344 static void setup_frame(int sig
, struct k_sigaction
*ka
,
345 sigset_t
*set
, struct pt_regs
*regs
)
347 struct sigframe
*frame
;
348 unsigned long retcode
;
351 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
353 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
356 err
|= setup_sigcontext(&frame
->sc
, /*&frame->fpstate,*/ regs
, set
->sig
[0]);
358 if (_NSIG_WORDS
> 1) {
359 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
360 sizeof(frame
->extramask
));
363 /* Set up to return from userspace. If provided, use a stub
364 already in userspace. */
365 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
366 retcode
= (unsigned long)ka
->sa
.sa_restorer
;
368 retcode
= (unsigned long)&frame
->retcode
;
369 err
|= __put_user(SWI_SYS_SIGRETURN
, &frame
->retcode
);
370 flush_icache_range(retcode
, retcode
+ 4);
376 if (current
->exec_domain
&& current
->exec_domain
->signal_invmap
&& sig
< 32)
377 regs
->ARM_r0
= current
->exec_domain
->signal_invmap
[sig
];
380 regs
->ARM_sp
= (unsigned long)frame
;
381 regs
->ARM_lr
= retcode
;
382 regs
->ARM_pc
= (unsigned long)ka
->sa
.sa_handler
;
383 #if defined(CONFIG_CPU_32)
384 /* Maybe we need to deliver a 32-bit signal to a 26-bit task. */
385 if (ka
->sa
.sa_flags
& SA_THIRTYTWO
)
386 regs
->ARM_cpsr
= USR_MODE
;
388 if (valid_user_regs(regs
))
393 ka
->sa
.sa_handler
= SIG_DFL
;
394 force_sig(SIGSEGV
, current
);
397 static void setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
398 sigset_t
*set
, struct pt_regs
*regs
)
400 struct rt_sigframe
*frame
;
401 unsigned long retcode
;
404 frame
= get_sigframe(ka
, regs
, sizeof(struct rt_sigframe
));
406 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
409 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
410 err
|= __put_user(&frame
->uc
, &frame
->puc
);
411 err
|= copy_siginfo_to_user(&frame
->info
, info
);
413 /* Clear all the bits of the ucontext we don't use. */
414 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
416 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, /*&frame->fpstate,*/
418 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
420 /* Set up to return from userspace. If provided, use a stub
421 already in userspace. */
422 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
423 retcode
= (unsigned long)ka
->sa
.sa_restorer
;
425 retcode
= (unsigned long)&frame
->retcode
;
426 err
|= __put_user(SWI_SYS_RT_SIGRETURN
, &frame
->retcode
);
427 flush_icache_range(retcode
, retcode
+ 4);
433 if (current
->exec_domain
&& current
->exec_domain
->signal_invmap
&& sig
< 32)
434 regs
->ARM_r0
= current
->exec_domain
->signal_invmap
[sig
];
437 regs
->ARM_sp
= (unsigned long)frame
;
438 regs
->ARM_lr
= retcode
;
439 regs
->ARM_pc
= (unsigned long)ka
->sa
.sa_handler
;
440 #if defined(CONFIG_CPU_32)
441 /* Maybe we need to deliver a 32-bit signal to a 26-bit task. */
442 if (ka
->sa
.sa_flags
& SA_THIRTYTWO
)
443 regs
->ARM_cpsr
= USR_MODE
;
445 if (valid_user_regs(regs
))
450 ka
->sa
.sa_handler
= SIG_DFL
;
451 force_sig(SIGSEGV
, current
);
455 * OK, we're invoking a handler
458 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
459 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
* regs
)
461 /* Set up the stack frame */
462 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
463 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
465 setup_frame(sig
, ka
, oldset
, regs
);
467 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
468 ka
->sa
.sa_handler
= SIG_DFL
;
470 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
471 spin_lock_irq(¤t
->sigmask_lock
);
472 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
473 sigaddset(¤t
->blocked
,sig
);
474 recalc_sigpending(current
);
475 spin_unlock_irq(¤t
->sigmask_lock
);
480 * Note that 'init' is a special process: it doesn't get signals it doesn't
481 * want to handle. Thus you cannot kill init even with a SIGKILL even by
484 * Note that we go through the signals twice: once to check the signals that
485 * the kernel can handle, and then we build all the user-level signal handling
486 * stack-frames in one go after that.
488 asmlinkage
int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
, int syscall
)
490 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(regs
))
504 oldset
= ¤t
->blocked
;
506 single_stepping
= ptrace_cancel_bpt(current
);
511 spin_lock_irq (¤t
->sigmask_lock
);
512 signr
= dequeue_signal(¤t
->blocked
, &info
);
513 spin_unlock_irq (¤t
->sigmask_lock
);
518 if ((current
->ptrace
& PT_PTRACED
) && signr
!= SIGKILL
) {
519 /* Let the debugger run. */
520 current
->exit_code
= signr
;
521 current
->state
= TASK_STOPPED
;
522 notify_parent(current
, SIGCHLD
);
524 single_stepping
|= ptrace_cancel_bpt(current
);
526 /* We're back. Did the debugger cancel the sig? */
527 if (!(signr
= current
->exit_code
))
529 current
->exit_code
= 0;
531 /* The debugger continued. Ignore SIGSTOP. */
532 if (signr
== SIGSTOP
)
535 /* Update the siginfo structure. Is this good? */
536 if (signr
!= info
.si_signo
) {
537 info
.si_signo
= signr
;
539 info
.si_code
= SI_USER
;
540 info
.si_pid
= current
->p_pptr
->pid
;
541 info
.si_uid
= current
->p_pptr
->uid
;
544 /* If the (new) signal is now blocked, requeue it. */
545 if (sigismember(¤t
->blocked
, signr
)) {
546 send_sig_info(signr
, &info
, current
);
551 ka
= ¤t
->sig
->action
[signr
-1];
552 if (ka
->sa
.sa_handler
== SIG_IGN
) {
553 if (signr
!= SIGCHLD
)
555 /* Check for SIGCHLD: it's special. */
556 while (sys_wait4(-1, NULL
, WNOHANG
, NULL
) > 0)
561 if (ka
->sa
.sa_handler
== SIG_DFL
) {
562 int exit_code
= signr
;
564 /* Init gets no signals it doesn't want. */
565 if (current
->pid
== 1)
569 case SIGCONT
: case SIGCHLD
: case SIGWINCH
:
572 case SIGTSTP
: case SIGTTIN
: case SIGTTOU
:
573 if (is_orphaned_pgrp(current
->pgrp
))
578 current
->state
= TASK_STOPPED
;
579 current
->exit_code
= signr
;
580 if (!(current
->p_pptr
->sig
->action
[SIGCHLD
-1].sa
.sa_flags
& SA_NOCLDSTOP
))
581 notify_parent(current
, SIGCHLD
);
585 case SIGQUIT
: case SIGILL
: case SIGTRAP
:
586 case SIGABRT
: case SIGFPE
: case SIGSEGV
:
587 case SIGBUS
: case SIGSYS
: case SIGXCPU
: case SIGXFSZ
:
588 if (do_coredump(signr
, regs
))
593 sigaddset(¤t
->pending
.signal
, signr
);
594 recalc_sigpending(current
);
595 current
->flags
|= PF_SIGNALED
;
601 /* Are we from a system call? */
603 switch (regs
->ARM_r0
) {
604 case -ERESTARTNOHAND
:
605 regs
->ARM_r0
= -EINTR
;
609 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
610 regs
->ARM_r0
= -EINTR
;
614 case -ERESTARTNOINTR
:
615 regs
->ARM_r0
= regs
->ARM_ORIG_r0
;
619 /* Whee! Actually deliver the signal. */
620 handle_signal(signr
, ka
, &info
, oldset
, regs
);
622 ptrace_set_bpt(current
);
627 (regs
->ARM_r0
== -ERESTARTNOHAND
||
628 regs
->ARM_r0
== -ERESTARTSYS
||
629 regs
->ARM_r0
== -ERESTARTNOINTR
)) {
630 regs
->ARM_r0
= regs
->ARM_ORIG_r0
;
634 ptrace_set_bpt(current
);