2 * linux/arch/arm/kernel/signal.c
4 * Copyright (C) 1995-2002 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/errno.h>
12 #include <linux/signal.h>
13 #include <linux/ptrace.h>
14 #include <linux/personality.h>
15 #include <linux/suspend.h>
17 #include <asm/cacheflush.h>
18 #include <asm/ucontext.h>
19 #include <asm/uaccess.h>
20 #include <asm/unistd.h>
24 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
27 * For ARM syscalls, we encode the syscall number into the instruction.
29 #define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
30 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
33 * For Thumb syscalls, we pass the syscall number via r7. We therefore
34 * need two 16-bit instructions.
36 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
37 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
39 static const unsigned long retcodes
[4] = {
40 SWI_SYS_SIGRETURN
, SWI_THUMB_SIGRETURN
,
41 SWI_SYS_RT_SIGRETURN
, SWI_THUMB_RT_SIGRETURN
44 static int do_signal(sigset_t
*oldset
, struct pt_regs
* regs
, int syscall
);
47 * atomically swap in the new signal mask, and wait for a signal.
49 asmlinkage
int sys_sigsuspend(int restart
, unsigned long oldmask
, old_sigset_t mask
, struct pt_regs
*regs
)
54 spin_lock_irq(¤t
->sighand
->siglock
);
55 saveset
= current
->blocked
;
56 siginitset(¤t
->blocked
, mask
);
58 spin_unlock_irq(¤t
->sighand
->siglock
);
59 regs
->ARM_r0
= -EINTR
;
62 current
->state
= TASK_INTERRUPTIBLE
;
64 if (do_signal(&saveset
, regs
, 0))
70 sys_rt_sigsuspend(sigset_t __user
*unewset
, size_t sigsetsize
, struct pt_regs
*regs
)
72 sigset_t saveset
, newset
;
74 /* XXX: Don't preclude handling different sized sigset_t's. */
75 if (sigsetsize
!= sizeof(sigset_t
))
78 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
80 sigdelsetmask(&newset
, ~_BLOCKABLE
);
82 spin_lock_irq(¤t
->sighand
->siglock
);
83 saveset
= current
->blocked
;
84 current
->blocked
= newset
;
86 spin_unlock_irq(¤t
->sighand
->siglock
);
87 regs
->ARM_r0
= -EINTR
;
90 current
->state
= TASK_INTERRUPTIBLE
;
92 if (do_signal(&saveset
, regs
, 0))
98 sys_sigaction(int sig
, const struct old_sigaction __user
*act
,
99 struct old_sigaction __user
*oact
)
101 struct k_sigaction new_ka
, old_ka
;
106 if (verify_area(VERIFY_READ
, act
, sizeof(*act
)) ||
107 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
108 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
110 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
111 __get_user(mask
, &act
->sa_mask
);
112 siginitset(&new_ka
.sa
.sa_mask
, mask
);
115 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
118 if (verify_area(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
119 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
120 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
122 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
123 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
130 * Do a signal return; undo the signal stack.
134 struct sigcontext sc
;
135 unsigned long extramask
[_NSIG_WORDS
-1];
136 unsigned long retcode
;
141 struct siginfo __user
*pinfo
;
145 unsigned long retcode
;
150 /* iwmmxt_area is 0x98 bytes long, preceeded by 8 bytes of signature */
151 #define IWMMXT_STORAGE_SIZE (0x98 + 8)
152 #define IWMMXT_MAGIC0 0x12ef842a
153 #define IWMMXT_MAGIC1 0x1c07ca71
155 static int page_present(struct mm_struct
*mm
, unsigned long addr
, int wr
)
157 pgd_t
*pgd
= pgd_offset(mm
, addr
);
158 if (pgd_present(*pgd
)) {
159 pmd_t
*pmd
= pmd_offset(pgd
, addr
);
160 if (pmd_present(*pmd
)) {
161 pte_t
*pte
= pte_offset_map(pmd
, addr
);
162 return (pte_present(*pte
) && (!wr
|| pte_write(*pte
)));
169 preserve_iwmmxt_context(void *iwmmxt_save_area
)
173 /* the iWMMXt context must be 64 bit aligned */
174 long *iwmmxt_storage
= (long *)(((long)iwmmxt_save_area
+ 4) & ~7);
177 __put_user_error(IWMMXT_MAGIC0
, iwmmxt_storage
+0, err
);
178 __put_user_error(IWMMXT_MAGIC1
, iwmmxt_storage
+1, err
);
180 * iwmmxt_task_copy() doesn't check user permissions.
181 * Let's do a dummy write on the upper boundary to ensure
182 * access to user mem is OK all way up.
184 __put_user_error(0, iwmmxt_storage
+IWMMXT_STORAGE_SIZE
/4-1, err
);
186 /* Let's make sure the user mapping won't disappear under us */
187 struct mm_struct
*mm
= current
->mm
;
188 unsigned long addr
= (unsigned long)iwmmxt_storage
;
189 spin_lock(&mm
->page_table_lock
);
190 if ( !page_present(mm
, addr
, 1) ||
191 !page_present(mm
, addr
+IWMMXT_STORAGE_SIZE
-1, 1) ) {
192 /* our user area has gone before grabbing the lock */
193 spin_unlock(&mm
->page_table_lock
);
196 iwmmxt_task_copy(current_thread_info(), iwmmxt_storage
+2);
197 spin_unlock(&mm
->page_table_lock
);
204 restore_iwmmxt_context(void *iwmmxt_save_area
)
207 long *iwmmxt_storage
, magic0
, magic1
, dummy
;
209 /* the iWMMXt context is 64 bit aligned */
210 iwmmxt_storage
= (long *)(((long)iwmmxt_save_area
+ 4) & ~7);
213 * Validate iWMMXt context signature.
214 * Also, iwmmxt_task_restore() doesn't check user permissions.
215 * Let's do a dummy write on the upper boundary to ensure
216 * access to user mem is OK all way up.
219 __get_user_error(magic0
, iwmmxt_storage
+0, err
);
220 __get_user_error(magic1
, iwmmxt_storage
+1, err
);
221 if (!err
&& magic0
== IWMMXT_MAGIC0
&& magic1
== IWMMXT_MAGIC1
&&
222 !__get_user(dummy
, iwmmxt_storage
+IWMMXT_STORAGE_SIZE
/4-1)) {
223 /* Let's make sure the user mapping won't disappear under us */
224 struct mm_struct
*mm
= current
->mm
;
225 unsigned long addr
= (unsigned long)iwmmxt_storage
;
226 spin_lock(&mm
->page_table_lock
);
227 if ( !page_present(mm
, addr
, 0) ||
228 !page_present(mm
, addr
+IWMMXT_STORAGE_SIZE
-1, 0) ) {
229 /* our user area has gone before grabbing the lock */
230 spin_unlock(&mm
->page_table_lock
);
233 iwmmxt_task_restore(current_thread_info(), iwmmxt_storage
+2);
234 spin_unlock(&mm
->page_table_lock
);
243 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
247 __get_user_error(regs
->ARM_r0
, &sc
->arm_r0
, err
);
248 __get_user_error(regs
->ARM_r1
, &sc
->arm_r1
, err
);
249 __get_user_error(regs
->ARM_r2
, &sc
->arm_r2
, err
);
250 __get_user_error(regs
->ARM_r3
, &sc
->arm_r3
, err
);
251 __get_user_error(regs
->ARM_r4
, &sc
->arm_r4
, err
);
252 __get_user_error(regs
->ARM_r5
, &sc
->arm_r5
, err
);
253 __get_user_error(regs
->ARM_r6
, &sc
->arm_r6
, err
);
254 __get_user_error(regs
->ARM_r7
, &sc
->arm_r7
, err
);
255 __get_user_error(regs
->ARM_r8
, &sc
->arm_r8
, err
);
256 __get_user_error(regs
->ARM_r9
, &sc
->arm_r9
, err
);
257 __get_user_error(regs
->ARM_r10
, &sc
->arm_r10
, err
);
258 __get_user_error(regs
->ARM_fp
, &sc
->arm_fp
, err
);
259 __get_user_error(regs
->ARM_ip
, &sc
->arm_ip
, err
);
260 __get_user_error(regs
->ARM_sp
, &sc
->arm_sp
, err
);
261 __get_user_error(regs
->ARM_lr
, &sc
->arm_lr
, err
);
262 __get_user_error(regs
->ARM_pc
, &sc
->arm_pc
, err
);
263 __get_user_error(regs
->ARM_cpsr
, &sc
->arm_cpsr
, err
);
265 err
|= !valid_user_regs(regs
);
270 asmlinkage
int sys_sigreturn(struct pt_regs
*regs
)
272 struct sigframe __user
*frame
;
275 /* Always make any pending restarted system calls return -EINTR */
276 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
279 * Since we stacked the signal on a 64-bit boundary,
280 * then 'sp' should be word aligned here. If it's
281 * not, then the user is trying to mess with us.
283 if (regs
->ARM_sp
& 7)
286 frame
= (struct sigframe __user
*)regs
->ARM_sp
;
288 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
290 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
292 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
293 sizeof(frame
->extramask
))))
296 sigdelsetmask(&set
, ~_BLOCKABLE
);
297 spin_lock_irq(¤t
->sighand
->siglock
);
298 current
->blocked
= set
;
300 spin_unlock_irq(¤t
->sighand
->siglock
);
302 if (restore_sigcontext(regs
, &frame
->sc
))
306 if (test_thread_flag(TIF_USING_IWMMXT
) && restore_iwmmxt_context(frame
+1))
310 /* Send SIGTRAP if we're single-stepping */
311 if (current
->ptrace
& PT_SINGLESTEP
) {
312 ptrace_cancel_bpt(current
);
313 send_sig(SIGTRAP
, current
, 1);
319 force_sig(SIGSEGV
, current
);
323 asmlinkage
int sys_rt_sigreturn(struct pt_regs
*regs
)
325 struct rt_sigframe __user
*frame
;
328 /* Always make any pending restarted system calls return -EINTR */
329 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
332 * Since we stacked the signal on a 64-bit boundary,
333 * then 'sp' should be word aligned here. If it's
334 * not, then the user is trying to mess with us.
336 if (regs
->ARM_sp
& 7)
339 frame
= (struct rt_sigframe __user
*)regs
->ARM_sp
;
341 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
343 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
346 sigdelsetmask(&set
, ~_BLOCKABLE
);
347 spin_lock_irq(¤t
->sighand
->siglock
);
348 current
->blocked
= set
;
350 spin_unlock_irq(¤t
->sighand
->siglock
);
352 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
355 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, regs
->ARM_sp
) == -EFAULT
)
359 if (test_thread_flag(TIF_USING_IWMMXT
) && restore_iwmmxt_context(frame
+1))
363 /* Send SIGTRAP if we're single-stepping */
364 if (current
->ptrace
& PT_SINGLESTEP
) {
365 ptrace_cancel_bpt(current
);
366 send_sig(SIGTRAP
, current
, 1);
372 force_sig(SIGSEGV
, current
);
377 setup_sigcontext(struct sigcontext __user
*sc
, /*struct _fpstate *fpstate,*/
378 struct pt_regs
*regs
, unsigned long mask
)
382 __put_user_error(regs
->ARM_r0
, &sc
->arm_r0
, err
);
383 __put_user_error(regs
->ARM_r1
, &sc
->arm_r1
, err
);
384 __put_user_error(regs
->ARM_r2
, &sc
->arm_r2
, err
);
385 __put_user_error(regs
->ARM_r3
, &sc
->arm_r3
, err
);
386 __put_user_error(regs
->ARM_r4
, &sc
->arm_r4
, err
);
387 __put_user_error(regs
->ARM_r5
, &sc
->arm_r5
, err
);
388 __put_user_error(regs
->ARM_r6
, &sc
->arm_r6
, err
);
389 __put_user_error(regs
->ARM_r7
, &sc
->arm_r7
, err
);
390 __put_user_error(regs
->ARM_r8
, &sc
->arm_r8
, err
);
391 __put_user_error(regs
->ARM_r9
, &sc
->arm_r9
, err
);
392 __put_user_error(regs
->ARM_r10
, &sc
->arm_r10
, err
);
393 __put_user_error(regs
->ARM_fp
, &sc
->arm_fp
, err
);
394 __put_user_error(regs
->ARM_ip
, &sc
->arm_ip
, err
);
395 __put_user_error(regs
->ARM_sp
, &sc
->arm_sp
, err
);
396 __put_user_error(regs
->ARM_lr
, &sc
->arm_lr
, err
);
397 __put_user_error(regs
->ARM_pc
, &sc
->arm_pc
, err
);
398 __put_user_error(regs
->ARM_cpsr
, &sc
->arm_cpsr
, err
);
400 __put_user_error(current
->thread
.trap_no
, &sc
->trap_no
, err
);
401 __put_user_error(current
->thread
.error_code
, &sc
->error_code
, err
);
402 __put_user_error(current
->thread
.address
, &sc
->fault_address
, err
);
403 __put_user_error(mask
, &sc
->oldmask
, err
);
408 static inline void __user
*
409 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
, int framesize
)
411 unsigned long sp
= regs
->ARM_sp
;
415 if (test_thread_flag(TIF_USING_IWMMXT
))
416 framesize
= (framesize
+ 4 + IWMMXT_STORAGE_SIZE
) & ~7;
420 * This is the X/Open sanctioned signal stack switching.
422 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && !sas_ss_flags(sp
))
423 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
426 * ATPCS B01 mandates 8-byte alignment
428 frame
= (void __user
*)((sp
- framesize
) & ~7);
431 * Check that we can actually write to the signal frame.
433 if (!access_ok(VERIFY_WRITE
, frame
, framesize
))
440 setup_return(struct pt_regs
*regs
, struct k_sigaction
*ka
,
441 unsigned long __user
*rc
, void __user
*frame
, int usig
)
443 unsigned long handler
= (unsigned long)ka
->sa
.sa_handler
;
444 unsigned long retcode
;
446 unsigned long cpsr
= regs
->ARM_cpsr
& ~PSR_f
;
449 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
451 if (ka
->sa
.sa_flags
& SA_THIRTYTWO
)
452 cpsr
= (cpsr
& ~MODE_MASK
) | USR_MODE
;
454 #ifdef CONFIG_ARM_THUMB
455 if (elf_hwcap
& HWCAP_THUMB
) {
457 * The LSB of the handler determines if we're going to
458 * be using THUMB or ARM mode for this signal handler.
469 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
470 retcode
= (unsigned long)ka
->sa
.sa_restorer
;
472 unsigned int idx
= thumb
;
474 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
477 if (__put_user(retcodes
[idx
], rc
))
481 * Ensure that the instruction cache sees
482 * the return code written onto the stack.
484 flush_icache_range((unsigned long)rc
,
485 (unsigned long)(rc
+ 1));
487 retcode
= ((unsigned long)rc
) + thumb
;
491 regs
->ARM_sp
= (unsigned long)frame
;
492 regs
->ARM_lr
= retcode
;
493 regs
->ARM_pc
= handler
;
494 regs
->ARM_cpsr
= cpsr
;
500 setup_frame(int usig
, struct k_sigaction
*ka
, sigset_t
*set
, struct pt_regs
*regs
)
502 struct sigframe __user
*frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
508 err
|= setup_sigcontext(&frame
->sc
, /*&frame->fpstate,*/ regs
, set
->sig
[0]);
510 if (_NSIG_WORDS
> 1) {
511 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
512 sizeof(frame
->extramask
));
516 if (test_thread_flag(TIF_USING_IWMMXT
))
517 err
|= preserve_iwmmxt_context(frame
+1);
521 err
= setup_return(regs
, ka
, &frame
->retcode
, frame
, usig
);
527 setup_rt_frame(int usig
, struct k_sigaction
*ka
, siginfo_t
*info
,
528 sigset_t
*set
, struct pt_regs
*regs
)
530 struct rt_sigframe __user
*frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
537 __put_user_error(&frame
->info
, &frame
->pinfo
, err
);
538 __put_user_error(&frame
->uc
, &frame
->puc
, err
);
539 err
|= copy_siginfo_to_user(&frame
->info
, info
);
541 __put_user_error(0, &frame
->uc
.uc_flags
, err
);
542 __put_user_error(NULL
, &frame
->uc
.uc_link
, err
);
544 memset(&stack
, 0, sizeof(stack
));
545 stack
.ss_sp
= (void __user
*)current
->sas_ss_sp
;
546 stack
.ss_flags
= sas_ss_flags(regs
->ARM_sp
);
547 stack
.ss_size
= current
->sas_ss_size
;
548 err
|= __copy_to_user(&frame
->uc
.uc_stack
, &stack
, sizeof(stack
));
550 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, /*&frame->fpstate,*/
552 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
555 if (test_thread_flag(TIF_USING_IWMMXT
))
556 err
|= preserve_iwmmxt_context(frame
+1);
560 err
= setup_return(regs
, ka
, &frame
->retcode
, frame
, usig
);
564 * For realtime signals we must also set the second and third
565 * arguments for the signal handler.
566 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
568 regs
->ARM_r1
= (unsigned long)&frame
->info
;
569 regs
->ARM_r2
= (unsigned long)&frame
->uc
;
575 static inline void restart_syscall(struct pt_regs
*regs
)
577 regs
->ARM_r0
= regs
->ARM_ORIG_r0
;
578 regs
->ARM_pc
-= thumb_mode(regs
) ? 2 : 4;
582 * OK, we're invoking a handler
585 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
586 siginfo_t
*info
, sigset_t
*oldset
,
587 struct pt_regs
* regs
, int syscall
)
589 struct thread_info
*thread
= current_thread_info();
590 struct task_struct
*tsk
= current
;
595 * If we were from a system call, check for system call restarting...
598 switch (regs
->ARM_r0
) {
599 case -ERESTART_RESTARTBLOCK
:
600 case -ERESTARTNOHAND
:
601 regs
->ARM_r0
= -EINTR
;
604 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
605 regs
->ARM_r0
= -EINTR
;
609 case -ERESTARTNOINTR
:
610 restart_syscall(regs
);
615 * translate the signal
617 if (usig
< 32 && thread
->exec_domain
&& thread
->exec_domain
->signal_invmap
)
618 usig
= thread
->exec_domain
->signal_invmap
[usig
];
621 * Set up the stack frame
623 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
624 ret
= setup_rt_frame(usig
, ka
, info
, oldset
, regs
);
626 ret
= setup_frame(usig
, ka
, oldset
, regs
);
629 * Check that the resulting registers are actually sane.
631 ret
|= !valid_user_regs(regs
);
634 * Block the signal if we were unsuccessful.
636 if (ret
!= 0 || !(ka
->sa
.sa_flags
& SA_NODEFER
)) {
637 spin_lock_irq(&tsk
->sighand
->siglock
);
638 sigorsets(&tsk
->blocked
, &tsk
->blocked
,
640 sigaddset(&tsk
->blocked
, sig
);
642 spin_unlock_irq(&tsk
->sighand
->siglock
);
648 force_sigsegv(sig
, tsk
);
652 * Note that 'init' is a special process: it doesn't get signals it doesn't
653 * want to handle. Thus you cannot kill init even with a SIGKILL even by
656 * Note that we go through the signals twice: once to check the signals that
657 * the kernel can handle, and then we build all the user-level signal handling
658 * stack-frames in one go after that.
660 static int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
, int syscall
)
662 struct k_sigaction ka
;
667 * We want the common case to go fast, which
668 * is why we may in certain cases get here from
669 * kernel mode. Just return without doing anything
672 if (!user_mode(regs
))
675 if (current
->flags
& PF_FREEZE
) {
680 if (current
->ptrace
& PT_SINGLESTEP
)
681 ptrace_cancel_bpt(current
);
683 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
685 handle_signal(signr
, &ka
, &info
, oldset
, regs
, syscall
);
686 if (current
->ptrace
& PT_SINGLESTEP
)
687 ptrace_set_bpt(current
);
693 * No signal to deliver to the process - restart the syscall.
696 if (regs
->ARM_r0
== -ERESTART_RESTARTBLOCK
) {
697 if (thumb_mode(regs
)) {
698 regs
->ARM_r7
= __NR_restart_syscall
;
704 usp
= (u32 __user
*)regs
->ARM_sp
;
706 put_user(regs
->ARM_pc
, &usp
[0]);
707 /* swi __NR_restart_syscall */
708 put_user(0xef000000 | __NR_restart_syscall
, &usp
[1]);
709 /* ldr pc, [sp], #12 */
710 put_user(0xe49df00c, &usp
[2]);
712 flush_icache_range((unsigned long)usp
,
713 (unsigned long)(usp
+ 3));
715 regs
->ARM_pc
= regs
->ARM_sp
+ 4;
718 if (regs
->ARM_r0
== -ERESTARTNOHAND
||
719 regs
->ARM_r0
== -ERESTARTSYS
||
720 regs
->ARM_r0
== -ERESTARTNOINTR
) {
721 restart_syscall(regs
);
724 if (current
->ptrace
& PT_SINGLESTEP
)
725 ptrace_set_bpt(current
);
730 do_notify_resume(struct pt_regs
*regs
, unsigned int thread_flags
, int syscall
)
732 if (thread_flags
& _TIF_SIGPENDING
)
733 do_signal(¤t
->blocked
, regs
, syscall
);