2 * linux/arch/arm26/kernel/signal.c
4 * Copyright (C) 1995-2002 Russell King
5 * Copyright (C) 2003 Ian Molton (ARM26)
7 * FIXME!!! This is probably very broken (13/05/2003)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/signal.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/personality.h>
23 #include <linux/tty.h>
24 #include <linux/binfmts.h>
25 #include <linux/elf.h>
27 #include <asm/pgalloc.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 * For ARM syscalls, we encode the syscall number into the instruction.
39 #define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
40 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
42 static int do_signal(sigset_t
*oldset
, struct pt_regs
* regs
, int syscall
);
45 * atomically swap in the new signal mask, and wait for a signal.
47 asmlinkage
int sys_sigsuspend(int restart
, unsigned long oldmask
, old_sigset_t mask
, struct pt_regs
*regs
)
52 spin_lock_irq(¤t
->sighand
->siglock
);
53 saveset
= current
->blocked
;
54 siginitset(¤t
->blocked
, mask
);
56 spin_unlock_irq(¤t
->sighand
->siglock
);
57 regs
->ARM_r0
= -EINTR
;
60 current
->state
= TASK_INTERRUPTIBLE
;
62 if (do_signal(&saveset
, regs
, 0))
68 sys_rt_sigsuspend(sigset_t
*unewset
, size_t sigsetsize
, struct pt_regs
*regs
)
70 sigset_t saveset
, newset
;
72 /* XXX: Don't preclude handling different sized sigset_t's. */
73 if (sigsetsize
!= sizeof(sigset_t
))
76 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
78 sigdelsetmask(&newset
, ~_BLOCKABLE
);
80 spin_lock_irq(¤t
->sighand
->siglock
);
81 saveset
= current
->blocked
;
82 current
->blocked
= newset
;
84 spin_unlock_irq(¤t
->sighand
->siglock
);
85 regs
->ARM_r0
= -EINTR
;
88 current
->state
= TASK_INTERRUPTIBLE
;
90 if (do_signal(&saveset
, regs
, 0))
96 sys_sigaction(int sig
, const struct old_sigaction
*act
,
97 struct old_sigaction
*oact
)
99 struct k_sigaction new_ka
, old_ka
;
104 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
105 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
106 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
108 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
109 __get_user(mask
, &act
->sa_mask
);
110 siginitset(&new_ka
.sa
.sa_mask
, mask
);
113 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
116 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
117 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
118 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
120 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
121 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
128 * Do a signal return; undo the signal stack.
132 struct sigcontext sc
;
133 unsigned long extramask
[_NSIG_WORDS
-1];
134 unsigned long retcode
;
139 struct siginfo
*pinfo
;
143 unsigned long retcode
;
147 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*sc
)
151 __get_user_error(regs
->ARM_r0
, &sc
->arm_r0
, err
);
152 __get_user_error(regs
->ARM_r1
, &sc
->arm_r1
, err
);
153 __get_user_error(regs
->ARM_r2
, &sc
->arm_r2
, err
);
154 __get_user_error(regs
->ARM_r3
, &sc
->arm_r3
, err
);
155 __get_user_error(regs
->ARM_r4
, &sc
->arm_r4
, err
);
156 __get_user_error(regs
->ARM_r5
, &sc
->arm_r5
, err
);
157 __get_user_error(regs
->ARM_r6
, &sc
->arm_r6
, err
);
158 __get_user_error(regs
->ARM_r7
, &sc
->arm_r7
, err
);
159 __get_user_error(regs
->ARM_r8
, &sc
->arm_r8
, err
);
160 __get_user_error(regs
->ARM_r9
, &sc
->arm_r9
, err
);
161 __get_user_error(regs
->ARM_r10
, &sc
->arm_r10
, err
);
162 __get_user_error(regs
->ARM_fp
, &sc
->arm_fp
, err
);
163 __get_user_error(regs
->ARM_ip
, &sc
->arm_ip
, err
);
164 __get_user_error(regs
->ARM_sp
, &sc
->arm_sp
, err
);
165 __get_user_error(regs
->ARM_lr
, &sc
->arm_lr
, err
);
166 __get_user_error(regs
->ARM_pc
, &sc
->arm_pc
, err
);
168 err
|= !valid_user_regs(regs
);
173 asmlinkage
int sys_sigreturn(struct pt_regs
*regs
)
175 struct sigframe
*frame
;
179 * Since we stacked the signal on a 64-bit boundary,
180 * then 'sp' should be word aligned here. If it's
181 * not, then the user is trying to mess with us.
183 if (regs
->ARM_sp
& 7)
186 frame
= (struct sigframe
*)regs
->ARM_sp
;
188 if (!access_ok(VERIFY_READ
, frame
, sizeof (*frame
)))
190 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
192 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
193 sizeof(frame
->extramask
))))
196 sigdelsetmask(&set
, ~_BLOCKABLE
);
197 spin_lock_irq(¤t
->sighand
->siglock
);
198 current
->blocked
= set
;
200 spin_unlock_irq(¤t
->sighand
->siglock
);
202 if (restore_sigcontext(regs
, &frame
->sc
))
205 /* Send SIGTRAP if we're single-stepping */
206 if (current
->ptrace
& PT_SINGLESTEP
) {
207 ptrace_cancel_bpt(current
);
208 send_sig(SIGTRAP
, current
, 1);
214 force_sig(SIGSEGV
, current
);
218 asmlinkage
int sys_rt_sigreturn(struct pt_regs
*regs
)
220 struct rt_sigframe
*frame
;
224 * Since we stacked the signal on a 64-bit boundary,
225 * then 'sp' should be word aligned here. If it's
226 * not, then the user is trying to mess with us.
228 if (regs
->ARM_sp
& 7)
231 frame
= (struct rt_sigframe
*)regs
->ARM_sp
;
233 if (!access_ok(VERIFY_READ
, frame
, sizeof (*frame
)))
235 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
238 sigdelsetmask(&set
, ~_BLOCKABLE
);
239 spin_lock_irq(¤t
->sighand
->siglock
);
240 current
->blocked
= set
;
242 spin_unlock_irq(¤t
->sighand
->siglock
);
244 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
247 /* Send SIGTRAP if we're single-stepping */
248 if (current
->ptrace
& PT_SINGLESTEP
) {
249 ptrace_cancel_bpt(current
);
250 send_sig(SIGTRAP
, current
, 1);
256 force_sig(SIGSEGV
, current
);
261 setup_sigcontext(struct sigcontext
*sc
, /*struct _fpstate *fpstate,*/
262 struct pt_regs
*regs
, unsigned long mask
)
266 __put_user_error(regs
->ARM_r0
, &sc
->arm_r0
, err
);
267 __put_user_error(regs
->ARM_r1
, &sc
->arm_r1
, err
);
268 __put_user_error(regs
->ARM_r2
, &sc
->arm_r2
, err
);
269 __put_user_error(regs
->ARM_r3
, &sc
->arm_r3
, err
);
270 __put_user_error(regs
->ARM_r4
, &sc
->arm_r4
, err
);
271 __put_user_error(regs
->ARM_r5
, &sc
->arm_r5
, err
);
272 __put_user_error(regs
->ARM_r6
, &sc
->arm_r6
, err
);
273 __put_user_error(regs
->ARM_r7
, &sc
->arm_r7
, err
);
274 __put_user_error(regs
->ARM_r8
, &sc
->arm_r8
, err
);
275 __put_user_error(regs
->ARM_r9
, &sc
->arm_r9
, err
);
276 __put_user_error(regs
->ARM_r10
, &sc
->arm_r10
, err
);
277 __put_user_error(regs
->ARM_fp
, &sc
->arm_fp
, err
);
278 __put_user_error(regs
->ARM_ip
, &sc
->arm_ip
, err
);
279 __put_user_error(regs
->ARM_sp
, &sc
->arm_sp
, err
);
280 __put_user_error(regs
->ARM_lr
, &sc
->arm_lr
, err
);
281 __put_user_error(regs
->ARM_pc
, &sc
->arm_pc
, err
);
283 __put_user_error(current
->thread
.trap_no
, &sc
->trap_no
, err
);
284 __put_user_error(current
->thread
.error_code
, &sc
->error_code
, err
);
285 __put_user_error(current
->thread
.address
, &sc
->fault_address
, err
);
286 __put_user_error(mask
, &sc
->oldmask
, err
);
292 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
, int framesize
)
294 unsigned long sp
= regs
->ARM_sp
;
297 * This is the X/Open sanctioned signal stack switching.
299 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && !sas_ss_flags(sp
))
300 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
303 * ATPCS B01 mandates 8-byte alignment
305 return (void *)((sp
- framesize
) & ~7);
309 setup_return(struct pt_regs
*regs
, struct k_sigaction
*ka
,
310 unsigned long *rc
, void *frame
, int usig
)
312 unsigned long handler
= (unsigned long)ka
->sa
.sa_handler
;
313 unsigned long retcode
;
315 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
316 retcode
= (unsigned long)ka
->sa
.sa_restorer
;
319 if (__put_user((ka
->sa
.sa_flags
& SA_SIGINFO
)?SWI_SYS_RT_SIGRETURN
:SWI_SYS_SIGRETURN
, rc
))
322 retcode
= ((unsigned long)rc
);
326 regs
->ARM_sp
= (unsigned long)frame
;
327 regs
->ARM_lr
= retcode
;
328 regs
->ARM_pc
= handler
& ~3;
334 setup_frame(int usig
, struct k_sigaction
*ka
, sigset_t
*set
, struct pt_regs
*regs
)
336 struct sigframe
*frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
339 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
342 err
|= setup_sigcontext(&frame
->sc
, /*&frame->fpstate,*/ regs
, set
->sig
[0]);
344 if (_NSIG_WORDS
> 1) {
345 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
346 sizeof(frame
->extramask
));
350 err
= setup_return(regs
, ka
, &frame
->retcode
, frame
, usig
);
356 setup_rt_frame(int usig
, struct k_sigaction
*ka
, siginfo_t
*info
,
357 sigset_t
*set
, struct pt_regs
*regs
)
359 struct rt_sigframe
*frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
362 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
365 __put_user_error(&frame
->info
, &frame
->pinfo
, err
);
366 __put_user_error(&frame
->uc
, &frame
->puc
, err
);
367 err
|= copy_siginfo_to_user(&frame
->info
, info
);
369 /* Clear all the bits of the ucontext we don't use. */
370 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
372 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, /*&frame->fpstate,*/
374 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
377 err
= setup_return(regs
, ka
, &frame
->retcode
, frame
, usig
);
381 * For realtime signals we must also set the second and third
382 * arguments for the signal handler.
383 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
385 regs
->ARM_r1
= (unsigned long)frame
->pinfo
;
386 regs
->ARM_r2
= (unsigned long)frame
->puc
;
392 static inline void restart_syscall(struct pt_regs
*regs
)
394 regs
->ARM_r0
= regs
->ARM_ORIG_r0
;
399 * OK, we're invoking a handler
402 handle_signal(unsigned long sig
, siginfo_t
*info
, sigset_t
*oldset
,
403 struct pt_regs
* regs
, int syscall
)
405 struct thread_info
*thread
= current_thread_info();
406 struct task_struct
*tsk
= current
;
407 struct k_sigaction
*ka
= &tsk
->sighand
->action
[sig
-1];
412 * If we were from a system call, check for system call restarting...
415 switch (regs
->ARM_r0
) {
416 case -ERESTART_RESTARTBLOCK
:
417 current_thread_info()->restart_block
.fn
=
418 do_no_restart_syscall
;
419 case -ERESTARTNOHAND
:
420 regs
->ARM_r0
= -EINTR
;
423 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
424 regs
->ARM_r0
= -EINTR
;
428 case -ERESTARTNOINTR
:
429 restart_syscall(regs
);
434 * translate the signal
436 if (usig
< 32 && thread
->exec_domain
&& thread
->exec_domain
->signal_invmap
)
437 usig
= thread
->exec_domain
->signal_invmap
[usig
];
440 * Set up the stack frame
442 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
443 ret
= setup_rt_frame(usig
, ka
, info
, oldset
, regs
);
445 ret
= setup_frame(usig
, ka
, oldset
, regs
);
448 * Check that the resulting registers are actually sane.
450 ret
|= !valid_user_regs(regs
);
453 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
454 ka
->sa
.sa_handler
= SIG_DFL
;
456 spin_lock_irq(&tsk
->sighand
->siglock
);
457 sigorsets(&tsk
->blocked
, &tsk
->blocked
,
459 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
460 sigaddset(&tsk
->blocked
, sig
);
462 spin_unlock_irq(&tsk
->sighand
->siglock
);
466 force_sigsegv(sig
, tsk
);
470 * Note that 'init' is a special process: it doesn't get signals it doesn't
471 * want to handle. Thus you cannot kill init even with a SIGKILL even by
474 * Note that we go through the signals twice: once to check the signals that
475 * the kernel can handle, and then we build all the user-level signal handling
476 * stack-frames in one go after that.
478 static int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
, int syscall
)
482 struct k_sigaction ka
;
485 * We want the common case to go fast, which
486 * is why we may in certain cases get here from
487 * kernel mode. Just return without doing anything
490 if (!user_mode(regs
))
493 if (current
->ptrace
& PT_SINGLESTEP
)
494 ptrace_cancel_bpt(current
);
496 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
498 handle_signal(signr
, &info
, oldset
, regs
, syscall
);
499 if (current
->ptrace
& PT_SINGLESTEP
)
500 ptrace_set_bpt(current
);
505 * No signal to deliver to the process - restart the syscall.
508 if (regs
->ARM_r0
== -ERESTART_RESTARTBLOCK
) {
512 usp
= (u32
*)regs
->ARM_sp
;
514 put_user(regs
->ARM_pc
, &usp
[0]);
515 /* swi __NR_restart_syscall */
516 put_user(0xef000000 | __NR_restart_syscall
, &usp
[1]);
517 /* ldr pc, [sp], #12 */
518 // FIXME!!! is #12 correct there?
519 put_user(0xe49df00c, &usp
[2]);
521 regs
->ARM_pc
= regs
->ARM_sp
+ 4;
523 if (regs
->ARM_r0
== -ERESTARTNOHAND
||
524 regs
->ARM_r0
== -ERESTARTSYS
||
525 regs
->ARM_r0
== -ERESTARTNOINTR
) {
526 restart_syscall(regs
);
529 if (current
->ptrace
& PT_SINGLESTEP
)
530 ptrace_set_bpt(current
);
535 do_notify_resume(struct pt_regs
*regs
, unsigned int thread_flags
, int syscall
)
537 if (thread_flags
& _TIF_SIGPENDING
)
538 do_signal(¤t
->blocked
, regs
, syscall
);