2 * linux/arch/arm/kernel/signal.c
4 * Copyright (C) 1995-2009 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/errno.h>
11 #include <linux/signal.h>
12 #include <linux/personality.h>
13 #include <linux/freezer.h>
14 #include <linux/uaccess.h>
15 #include <linux/tracehook.h>
18 #include <asm/cacheflush.h>
19 #include <asm/ucontext.h>
20 #include <asm/unistd.h>
25 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
28 * For ARM syscalls, we encode the syscall number into the instruction.
30 #define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
31 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
34 * With EABI, the syscall number has to be loaded into r7.
36 #define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
37 #define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
40 * For Thumb syscalls, we pass the syscall number via r7. We therefore
41 * need two 16-bit instructions.
43 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
44 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
46 const unsigned long sigreturn_codes[7] = {
47 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
48 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
52 * atomically swap in the new signal mask, and wait for a signal.
54 asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask)
57 siginitset(&blocked, mask);
58 return sigsuspend(&blocked);
62 sys_sigaction(int sig, const struct old_sigaction __user *act,
63 struct old_sigaction __user *oact)
65 struct k_sigaction new_ka, old_ka;
70 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
71 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
72 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
73 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
74 __get_user(mask, &act->sa_mask))
76 siginitset(&new_ka.sa.sa_mask, mask);
79 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
82 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
83 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
84 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
85 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
86 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
94 static int preserve_crunch_context(struct crunch_sigframe __user *frame)
96 char kbuf[sizeof(*frame) + 8];
97 struct crunch_sigframe *kframe;
99 /* the crunch context must be 64 bit aligned */
100 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
101 kframe->magic = CRUNCH_MAGIC;
102 kframe->size = CRUNCH_STORAGE_SIZE;
103 crunch_task_copy(current_thread_info(), &kframe->storage);
104 return __copy_to_user(frame, kframe, sizeof(*frame));
107 static int restore_crunch_context(struct crunch_sigframe __user *frame)
109 char kbuf[sizeof(*frame) + 8];
110 struct crunch_sigframe *kframe;
112 /* the crunch context must be 64 bit aligned */
113 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
114 if (__copy_from_user(kframe, frame, sizeof(*frame)))
116 if (kframe->magic != CRUNCH_MAGIC ||
117 kframe->size != CRUNCH_STORAGE_SIZE)
119 crunch_task_restore(current_thread_info(), &kframe->storage);
126 static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
128 char kbuf[sizeof(*frame) + 8];
129 struct iwmmxt_sigframe *kframe;
131 /* the iWMMXt context must be 64 bit aligned */
132 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
133 kframe->magic = IWMMXT_MAGIC;
134 kframe->size = IWMMXT_STORAGE_SIZE;
135 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
136 return __copy_to_user(frame, kframe, sizeof(*frame));
139 static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
141 char kbuf[sizeof(*frame) + 8];
142 struct iwmmxt_sigframe *kframe;
144 /* the iWMMXt context must be 64 bit aligned */
145 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
146 if (__copy_from_user(kframe, frame, sizeof(*frame)))
148 if (kframe->magic != IWMMXT_MAGIC ||
149 kframe->size != IWMMXT_STORAGE_SIZE)
151 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
159 static int preserve_vfp_context(struct vfp_sigframe __user *frame)
161 const unsigned long magic = VFP_MAGIC;
162 const unsigned long size = VFP_STORAGE_SIZE;
165 __put_user_error(magic, &frame->magic, err);
166 __put_user_error(size, &frame->size, err);
171 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
174 static int restore_vfp_context(struct vfp_sigframe __user *frame)
180 __get_user_error(magic, &frame->magic, err);
181 __get_user_error(size, &frame->size, err);
185 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
188 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
194 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
198 unsigned long retcode[2];
206 static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
208 struct aux_sigframe __user *aux;
212 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
214 sigdelsetmask(&set, ~_BLOCKABLE);
215 set_current_blocked(&set);
218 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
219 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
220 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
221 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
222 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
223 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
224 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
225 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
226 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
227 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
228 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
229 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
230 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
231 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
232 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
233 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
234 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
236 err |= !valid_user_regs(regs);
238 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
241 err |= restore_crunch_context(&aux->crunch);
244 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
245 err |= restore_iwmmxt_context(&aux->iwmmxt);
249 err |= restore_vfp_context(&aux->vfp);
255 asmlinkage int sys_sigreturn(struct pt_regs *regs)
257 struct sigframe __user *frame;
259 /* Always make any pending restarted system calls return -EINTR */
260 current_thread_info()->restart_block.fn = do_no_restart_syscall;
263 * Since we stacked the signal on a 64-bit boundary,
264 * then 'sp' should be word aligned here. If it's
265 * not, then the user is trying to mess with us.
267 if (regs->ARM_sp & 7)
270 frame = (struct sigframe __user *)regs->ARM_sp;
272 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
275 if (restore_sigframe(regs, frame))
281 force_sig(SIGSEGV, current);
285 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
287 struct rt_sigframe __user *frame;
289 /* Always make any pending restarted system calls return -EINTR */
290 current_thread_info()->restart_block.fn = do_no_restart_syscall;
293 * Since we stacked the signal on a 64-bit boundary,
294 * then 'sp' should be word aligned here. If it's
295 * not, then the user is trying to mess with us.
297 if (regs->ARM_sp & 7)
300 frame = (struct rt_sigframe __user *)regs->ARM_sp;
302 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
305 if (restore_sigframe(regs, &frame->sig))
308 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
314 force_sig(SIGSEGV, current);
319 setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
321 struct aux_sigframe __user *aux;
324 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
325 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
326 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
327 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
328 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
329 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
330 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
331 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
332 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
333 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
334 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
335 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
336 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
337 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
338 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
339 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
340 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
342 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
343 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
344 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
345 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
347 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
349 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
352 err |= preserve_crunch_context(&aux->crunch);
355 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
356 err |= preserve_iwmmxt_context(&aux->iwmmxt);
360 err |= preserve_vfp_context(&aux->vfp);
362 __put_user_error(0, &aux->end_magic, err);
367 static inline void __user *
368 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
370 unsigned long sp = regs->ARM_sp;
374 * This is the X/Open sanctioned signal stack switching.
376 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
377 sp = current->sas_ss_sp + current->sas_ss_size;
380 * ATPCS B01 mandates 8-byte alignment
382 frame = (void __user *)((sp - framesize) & ~7);
385 * Check that we can actually write to the signal frame.
387 if (!access_ok(VERIFY_WRITE, frame, framesize))
394 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
395 unsigned long __user *rc, void __user *frame, int usig)
397 unsigned long handler = (unsigned long)ka->sa.sa_handler;
398 unsigned long retcode;
400 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
402 cpsr |= PSR_ENDSTATE;
405 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
407 if (ka->sa.sa_flags & SA_THIRTYTWO)
408 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
410 #ifdef CONFIG_ARM_THUMB
411 if (elf_hwcap & HWCAP_THUMB) {
413 * The LSB of the handler determines if we're going to
414 * be using THUMB or ARM mode for this signal handler.
420 #if __LINUX_ARM_ARCH__ >= 7
421 /* clear the If-Then Thumb-2 execution state */
422 cpsr &= ~PSR_IT_MASK;
429 if (ka->sa.sa_flags & SA_RESTORER) {
430 retcode = (unsigned long)ka->sa.sa_restorer;
432 unsigned int idx = thumb << 1;
434 if (ka->sa.sa_flags & SA_SIGINFO)
437 if (__put_user(sigreturn_codes[idx], rc) ||
438 __put_user(sigreturn_codes[idx+1], rc+1))
441 if (cpsr & MODE32_BIT) {
443 * 32-bit code can use the new high-page
444 * signal return code support.
446 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
449 * Ensure that the instruction cache sees
450 * the return code written onto the stack.
452 flush_icache_range((unsigned long)rc,
453 (unsigned long)(rc + 2));
455 retcode = ((unsigned long)rc) + thumb;
460 regs->ARM_sp = (unsigned long)frame;
461 regs->ARM_lr = retcode;
462 regs->ARM_pc = handler;
463 regs->ARM_cpsr = cpsr;
469 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
471 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
478 * Set uc.uc_flags to a value which sc.trap_no would never have.
480 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
482 err |= setup_sigframe(frame, regs, set);
484 err = setup_return(regs, ka, frame->retcode, frame, usig);
490 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
491 sigset_t *set, struct pt_regs *regs)
493 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
500 err |= copy_siginfo_to_user(&frame->info, info);
502 __put_user_error(0, &frame->sig.uc.uc_flags, err);
503 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
505 memset(&stack, 0, sizeof(stack));
506 stack.ss_sp = (void __user *)current->sas_ss_sp;
507 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
508 stack.ss_size = current->sas_ss_size;
509 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
511 err |= setup_sigframe(&frame->sig, regs, set);
513 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
517 * For realtime signals we must also set the second and third
518 * arguments for the signal handler.
519 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
521 regs->ARM_r1 = (unsigned long)&frame->info;
522 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
529 * OK, we're invoking a handler
532 handle_signal(unsigned long sig, struct k_sigaction *ka,
533 siginfo_t *info, sigset_t *oldset,
534 struct pt_regs * regs)
536 struct thread_info *thread = current_thread_info();
537 struct task_struct *tsk = current;
542 * translate the signal
544 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
545 usig = thread->exec_domain->signal_invmap[usig];
548 * Set up the stack frame
550 if (ka->sa.sa_flags & SA_SIGINFO)
551 ret = setup_rt_frame(usig, ka, info, oldset, regs);
553 ret = setup_frame(usig, ka, oldset, regs);
556 * Check that the resulting registers are actually sane.
558 ret |= !valid_user_regs(regs);
561 force_sigsegv(sig, tsk);
566 * Block the signal if we were successful.
568 block_sigmask(ka, sig);
570 tracehook_signal_handler(sig, info, ka, regs, 0);
576 * Note that 'init' is a special process: it doesn't get signals it doesn't
577 * want to handle. Thus you cannot kill init even with a SIGKILL even by
580 * Note that we go through the signals twice: once to check the signals that
581 * the kernel can handle, and then we build all the user-level signal handling
582 * stack-frames in one go after that.
584 static void do_signal(struct pt_regs *regs, int syscall)
586 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
587 struct k_sigaction ka;
592 * If we were from a system call, check for system call restarting...
595 continue_addr = regs->ARM_pc;
596 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
597 retval = regs->ARM_r0;
600 * Prepare for system call restart. We do this here so that a
601 * debugger will see the already changed PSW.
604 case -ERESTARTNOHAND:
606 case -ERESTARTNOINTR:
607 case -ERESTART_RESTARTBLOCK:
608 regs->ARM_r0 = regs->ARM_ORIG_r0;
609 regs->ARM_pc = restart_addr;
615 * Get the signal to deliver. When running under ptrace, at this
616 * point the debugger may change all our registers ...
618 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
623 * Depending on the signal settings we may need to revert the
624 * decision to restart the system call. But skip this if a
625 * debugger has chosen to restart at a different PC.
627 if (regs->ARM_pc == restart_addr) {
628 if (retval == -ERESTARTNOHAND ||
629 retval == -ERESTART_RESTARTBLOCK
630 || (retval == -ERESTARTSYS
631 && !(ka.sa.sa_flags & SA_RESTART))) {
632 regs->ARM_r0 = -EINTR;
633 regs->ARM_pc = continue_addr;
635 clear_thread_flag(TIF_SYSCALL_RESTARTSYS);
638 if (test_thread_flag(TIF_RESTORE_SIGMASK))
639 oldset = ¤t->saved_sigmask;
641 oldset = ¤t->blocked;
642 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
644 * A signal was successfully delivered; the saved
645 * sigmask will have been stored in the signal frame,
646 * and will be restored by sigreturn, so we can simply
647 * clear the TIF_RESTORE_SIGMASK flag.
649 if (test_thread_flag(TIF_RESTORE_SIGMASK))
650 clear_thread_flag(TIF_RESTORE_SIGMASK);
657 * Handle restarting a different system call. As above,
658 * if a debugger has chosen to restart at a different PC,
659 * ignore the restart.
661 if (retval == -ERESTART_RESTARTBLOCK
662 && regs->ARM_pc == restart_addr)
663 set_thread_flag(TIF_SYSCALL_RESTARTSYS);
666 restore_saved_sigmask();
670 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
672 if (thread_flags & _TIF_SIGPENDING)
673 do_signal(regs, syscall);
675 if (thread_flags & _TIF_NOTIFY_RESUME) {
676 clear_thread_flag(TIF_NOTIFY_RESUME);
677 tracehook_notify_resume(regs);