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/errno.h>
11 #include <linux/signal.h>
12 #include <linux/personality.h>
13 #include <linux/freezer.h>
14 #include <linux/uaccess.h>
17 #include <asm/cacheflush.h>
18 #include <asm/ucontext.h>
19 #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)|(__NR_OABI_SYSCALL_BASE))
30 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
33 * With EABI, the syscall number has to be loaded into r7.
35 #define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
36 #define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
39 * For Thumb syscalls, we pass the syscall number via r7. We therefore
40 * need two 16-bit instructions.
42 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
43 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
45 const unsigned long sigreturn_codes[7] = {
46 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
47 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
50 static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
53 * atomically swap in the new signal mask, and wait for a signal.
55 asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
60 spin_lock_irq(¤t->sighand->siglock);
61 saveset = current->blocked;
62 siginitset(¤t->blocked, mask);
64 spin_unlock_irq(¤t->sighand->siglock);
65 regs->ARM_r0 = -EINTR;
68 current->state = TASK_INTERRUPTIBLE;
70 if (do_signal(&saveset, regs, 0))
76 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
78 sigset_t saveset, newset;
80 /* XXX: Don't preclude handling different sized sigset_t's. */
81 if (sigsetsize != sizeof(sigset_t))
84 if (copy_from_user(&newset, unewset, sizeof(newset)))
86 sigdelsetmask(&newset, ~_BLOCKABLE);
88 spin_lock_irq(¤t->sighand->siglock);
89 saveset = current->blocked;
90 current->blocked = newset;
92 spin_unlock_irq(¤t->sighand->siglock);
93 regs->ARM_r0 = -EINTR;
96 current->state = TASK_INTERRUPTIBLE;
98 if (do_signal(&saveset, regs, 0))
104 sys_sigaction(int sig, const struct old_sigaction __user *act,
105 struct old_sigaction __user *oact)
107 struct k_sigaction new_ka, old_ka;
112 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
113 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
114 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
116 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
117 __get_user(mask, &act->sa_mask);
118 siginitset(&new_ka.sa.sa_mask, mask);
121 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
124 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
125 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
126 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
128 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
129 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
136 static int preserve_crunch_context(struct crunch_sigframe *frame)
138 char kbuf[sizeof(*frame) + 8];
139 struct crunch_sigframe *kframe;
141 /* the crunch context must be 64 bit aligned */
142 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
143 kframe->magic = CRUNCH_MAGIC;
144 kframe->size = CRUNCH_STORAGE_SIZE;
145 crunch_task_copy(current_thread_info(), &kframe->storage);
146 return __copy_to_user(frame, kframe, sizeof(*frame));
149 static int restore_crunch_context(struct crunch_sigframe *frame)
151 char kbuf[sizeof(*frame) + 8];
152 struct crunch_sigframe *kframe;
154 /* the crunch context must be 64 bit aligned */
155 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
156 if (__copy_from_user(kframe, frame, sizeof(*frame)))
158 if (kframe->magic != CRUNCH_MAGIC ||
159 kframe->size != CRUNCH_STORAGE_SIZE)
161 crunch_task_restore(current_thread_info(), &kframe->storage);
168 static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
170 char kbuf[sizeof(*frame) + 8];
171 struct iwmmxt_sigframe *kframe;
173 /* the iWMMXt context must be 64 bit aligned */
174 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
175 kframe->magic = IWMMXT_MAGIC;
176 kframe->size = IWMMXT_STORAGE_SIZE;
177 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
178 return __copy_to_user(frame, kframe, sizeof(*frame));
181 static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
183 char kbuf[sizeof(*frame) + 8];
184 struct iwmmxt_sigframe *kframe;
186 /* the iWMMXt context must be 64 bit aligned */
187 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
188 if (__copy_from_user(kframe, frame, sizeof(*frame)))
190 if (kframe->magic != IWMMXT_MAGIC ||
191 kframe->size != IWMMXT_STORAGE_SIZE)
193 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
200 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
204 unsigned long retcode[2];
212 static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
214 struct aux_sigframe __user *aux;
218 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
220 sigdelsetmask(&set, ~_BLOCKABLE);
221 spin_lock_irq(¤t->sighand->siglock);
222 current->blocked = set;
224 spin_unlock_irq(¤t->sighand->siglock);
227 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
228 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
229 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
230 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
231 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
232 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
233 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
234 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
235 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
236 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
237 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
238 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
239 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
240 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
241 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
242 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
243 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
245 err |= !valid_user_regs(regs);
247 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
250 err |= restore_crunch_context(&aux->crunch);
253 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
254 err |= restore_iwmmxt_context(&aux->iwmmxt);
258 // err |= vfp_restore_state(&sf->aux.vfp);
264 asmlinkage int sys_sigreturn(struct pt_regs *regs)
266 struct sigframe __user *frame;
268 /* Always make any pending restarted system calls return -EINTR */
269 current_thread_info()->restart_block.fn = do_no_restart_syscall;
272 * Since we stacked the signal on a 64-bit boundary,
273 * then 'sp' should be word aligned here. If it's
274 * not, then the user is trying to mess with us.
276 if (regs->ARM_sp & 7)
279 frame = (struct sigframe __user *)regs->ARM_sp;
281 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
284 if (restore_sigframe(regs, frame))
287 single_step_trap(current);
292 force_sig(SIGSEGV, current);
296 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
298 struct rt_sigframe __user *frame;
300 /* Always make any pending restarted system calls return -EINTR */
301 current_thread_info()->restart_block.fn = do_no_restart_syscall;
304 * Since we stacked the signal on a 64-bit boundary,
305 * then 'sp' should be word aligned here. If it's
306 * not, then the user is trying to mess with us.
308 if (regs->ARM_sp & 7)
311 frame = (struct rt_sigframe __user *)regs->ARM_sp;
313 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
316 if (restore_sigframe(regs, &frame->sig))
319 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
322 single_step_trap(current);
327 force_sig(SIGSEGV, current);
332 setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
334 struct aux_sigframe __user *aux;
337 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
338 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
339 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
340 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
341 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
342 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
343 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
344 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
345 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
346 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
347 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
348 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
349 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
350 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
351 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
352 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
353 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
355 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
356 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
357 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
358 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
360 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
362 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
365 err |= preserve_crunch_context(&aux->crunch);
368 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
369 err |= preserve_iwmmxt_context(&aux->iwmmxt);
373 // err |= vfp_save_state(&sf->aux.vfp);
375 __put_user_error(0, &aux->end_magic, err);
380 static inline void __user *
381 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
383 unsigned long sp = regs->ARM_sp;
387 * This is the X/Open sanctioned signal stack switching.
389 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
390 sp = current->sas_ss_sp + current->sas_ss_size;
393 * ATPCS B01 mandates 8-byte alignment
395 frame = (void __user *)((sp - framesize) & ~7);
398 * Check that we can actually write to the signal frame.
400 if (!access_ok(VERIFY_WRITE, frame, framesize))
407 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
408 unsigned long __user *rc, void __user *frame, int usig)
410 unsigned long handler = (unsigned long)ka->sa.sa_handler;
411 unsigned long retcode;
413 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
416 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
418 if (ka->sa.sa_flags & SA_THIRTYTWO)
419 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
421 #ifdef CONFIG_ARM_THUMB
422 if (elf_hwcap & HWCAP_THUMB) {
424 * The LSB of the handler determines if we're going to
425 * be using THUMB or ARM mode for this signal handler.
431 #if __LINUX_ARM_ARCH__ >= 7
432 /* clear the If-Then Thumb-2 execution state */
433 cpsr &= ~PSR_IT_MASK;
440 if (ka->sa.sa_flags & SA_RESTORER) {
441 retcode = (unsigned long)ka->sa.sa_restorer;
443 unsigned int idx = thumb << 1;
445 if (ka->sa.sa_flags & SA_SIGINFO)
448 if (__put_user(sigreturn_codes[idx], rc) ||
449 __put_user(sigreturn_codes[idx+1], rc+1))
452 if (cpsr & MODE32_BIT) {
454 * 32-bit code can use the new high-page
455 * signal return code support.
457 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
460 * Ensure that the instruction cache sees
461 * the return code written onto the stack.
463 flush_icache_range((unsigned long)rc,
464 (unsigned long)(rc + 2));
466 retcode = ((unsigned long)rc) + thumb;
471 regs->ARM_sp = (unsigned long)frame;
472 regs->ARM_lr = retcode;
473 regs->ARM_pc = handler;
474 regs->ARM_cpsr = cpsr;
480 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
482 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
489 * Set uc.uc_flags to a value which sc.trap_no would never have.
491 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
493 err |= setup_sigframe(frame, regs, set);
495 err = setup_return(regs, ka, frame->retcode, frame, usig);
501 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
502 sigset_t *set, struct pt_regs *regs)
504 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
511 err |= copy_siginfo_to_user(&frame->info, info);
513 __put_user_error(0, &frame->sig.uc.uc_flags, err);
514 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
516 memset(&stack, 0, sizeof(stack));
517 stack.ss_sp = (void __user *)current->sas_ss_sp;
518 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
519 stack.ss_size = current->sas_ss_size;
520 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
522 err |= setup_sigframe(&frame->sig, regs, set);
524 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
528 * For realtime signals we must also set the second and third
529 * arguments for the signal handler.
530 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
532 regs->ARM_r1 = (unsigned long)&frame->info;
533 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
539 static inline void setup_syscall_restart(struct pt_regs *regs)
541 regs->ARM_r0 = regs->ARM_ORIG_r0;
542 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
546 * OK, we're invoking a handler
549 handle_signal(unsigned long sig, struct k_sigaction *ka,
550 siginfo_t *info, sigset_t *oldset,
551 struct pt_regs * regs, int syscall)
553 struct thread_info *thread = current_thread_info();
554 struct task_struct *tsk = current;
559 * If we were from a system call, check for system call restarting...
562 switch (regs->ARM_r0) {
563 case -ERESTART_RESTARTBLOCK:
564 case -ERESTARTNOHAND:
565 regs->ARM_r0 = -EINTR;
568 if (!(ka->sa.sa_flags & SA_RESTART)) {
569 regs->ARM_r0 = -EINTR;
573 case -ERESTARTNOINTR:
574 setup_syscall_restart(regs);
579 * translate the signal
581 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
582 usig = thread->exec_domain->signal_invmap[usig];
585 * Set up the stack frame
587 if (ka->sa.sa_flags & SA_SIGINFO)
588 ret = setup_rt_frame(usig, ka, info, oldset, regs);
590 ret = setup_frame(usig, ka, oldset, regs);
593 * Check that the resulting registers are actually sane.
595 ret |= !valid_user_regs(regs);
598 force_sigsegv(sig, tsk);
603 * Block the signal if we were successful.
605 spin_lock_irq(&tsk->sighand->siglock);
606 sigorsets(&tsk->blocked, &tsk->blocked,
608 if (!(ka->sa.sa_flags & SA_NODEFER))
609 sigaddset(&tsk->blocked, sig);
611 spin_unlock_irq(&tsk->sighand->siglock);
616 * Note that 'init' is a special process: it doesn't get signals it doesn't
617 * want to handle. Thus you cannot kill init even with a SIGKILL even by
620 * Note that we go through the signals twice: once to check the signals that
621 * the kernel can handle, and then we build all the user-level signal handling
622 * stack-frames in one go after that.
624 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
626 struct k_sigaction ka;
631 * We want the common case to go fast, which
632 * is why we may in certain cases get here from
633 * kernel mode. Just return without doing anything
636 if (!user_mode(regs))
642 single_step_clear(current);
644 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
646 handle_signal(signr, &ka, &info, oldset, regs, syscall);
647 single_step_set(current);
653 * No signal to deliver to the process - restart the syscall.
656 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
657 if (thumb_mode(regs)) {
658 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
661 #if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
662 regs->ARM_r7 = __NR_restart_syscall;
666 u32 swival = __NR_restart_syscall;
669 usp = (u32 __user *)regs->ARM_sp;
672 * Either we supports OABI only, or we have
673 * EABI with the OABI compat layer enabled.
674 * In the later case we don't know if user
675 * space is EABI or not, and if not we must
676 * not clobber r7. Always using the OABI
677 * syscall solves that issue and works for
680 swival = swival - __NR_SYSCALL_BASE + __NR_OABI_SYSCALL_BASE;
682 put_user(regs->ARM_pc, &usp[0]);
683 /* swi __NR_restart_syscall */
684 put_user(0xef000000 | swival, &usp[1]);
685 /* ldr pc, [sp], #12 */
686 put_user(0xe49df00c, &usp[2]);
688 flush_icache_range((unsigned long)usp,
689 (unsigned long)(usp + 3));
691 regs->ARM_pc = regs->ARM_sp + 4;
695 if (regs->ARM_r0 == -ERESTARTNOHAND ||
696 regs->ARM_r0 == -ERESTARTSYS ||
697 regs->ARM_r0 == -ERESTARTNOINTR) {
698 setup_syscall_restart(regs);
701 single_step_set(current);
706 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
708 if (thread_flags & _TIF_SIGPENDING)
709 do_signal(¤t->blocked, regs, syscall);