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/ptrace.h>
13 #include <linux/personality.h>
15 #include <asm/cacheflush.h>
16 #include <asm/ucontext.h>
17 #include <asm/uaccess.h>
18 #include <asm/unistd.h>
23 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
26 * For ARM syscalls, we encode the syscall number into the instruction.
28 #define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
29 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
32 * With EABI, the syscall number has to be loaded into r7.
34 #define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
35 #define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
38 * For Thumb syscalls, we pass the syscall number via r7. We therefore
39 * need two 16-bit instructions.
41 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
42 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
44 const unsigned long sigreturn_codes[7] = {
45 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
46 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
49 static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
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, struct pt_regs *regs)
59 spin_lock_irq(¤t->sighand->siglock);
60 saveset = current->blocked;
61 siginitset(¤t->blocked, mask);
63 spin_unlock_irq(¤t->sighand->siglock);
64 regs->ARM_r0 = -EINTR;
67 current->state = TASK_INTERRUPTIBLE;
69 if (do_signal(&saveset, regs, 0))
75 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
77 sigset_t saveset, newset;
79 /* XXX: Don't preclude handling different sized sigset_t's. */
80 if (sigsetsize != sizeof(sigset_t))
83 if (copy_from_user(&newset, unewset, sizeof(newset)))
85 sigdelsetmask(&newset, ~_BLOCKABLE);
87 spin_lock_irq(¤t->sighand->siglock);
88 saveset = current->blocked;
89 current->blocked = newset;
91 spin_unlock_irq(¤t->sighand->siglock);
92 regs->ARM_r0 = -EINTR;
95 current->state = TASK_INTERRUPTIBLE;
97 if (do_signal(&saveset, regs, 0))
103 sys_sigaction(int sig, const struct old_sigaction __user *act,
104 struct old_sigaction __user *oact)
106 struct k_sigaction new_ka, old_ka;
111 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
112 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
113 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
115 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
116 __get_user(mask, &act->sa_mask);
117 siginitset(&new_ka.sa.sa_mask, mask);
120 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
123 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
124 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
125 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
127 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
128 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
135 static int preserve_crunch_context(struct crunch_sigframe *frame)
137 char kbuf[sizeof(*frame) + 8];
138 struct crunch_sigframe *kframe;
140 /* the crunch context must be 64 bit aligned */
141 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
142 kframe->magic = CRUNCH_MAGIC;
143 kframe->size = CRUNCH_STORAGE_SIZE;
144 crunch_task_copy(current_thread_info(), &kframe->storage);
145 return __copy_to_user(frame, kframe, sizeof(*frame));
148 static int restore_crunch_context(struct crunch_sigframe *frame)
150 char kbuf[sizeof(*frame) + 8];
151 struct crunch_sigframe *kframe;
153 /* the crunch context must be 64 bit aligned */
154 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
155 if (__copy_from_user(kframe, frame, sizeof(*frame)))
157 if (kframe->magic != CRUNCH_MAGIC ||
158 kframe->size != CRUNCH_STORAGE_SIZE)
160 crunch_task_restore(current_thread_info(), &kframe->storage);
167 static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
169 char kbuf[sizeof(*frame) + 8];
170 struct iwmmxt_sigframe *kframe;
172 /* the iWMMXt context must be 64 bit aligned */
173 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
174 kframe->magic = IWMMXT_MAGIC;
175 kframe->size = IWMMXT_STORAGE_SIZE;
176 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
177 return __copy_to_user(frame, kframe, sizeof(*frame));
180 static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
182 char kbuf[sizeof(*frame) + 8];
183 struct iwmmxt_sigframe *kframe;
185 /* the iWMMXt context must be 64 bit aligned */
186 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
187 if (__copy_from_user(kframe, frame, sizeof(*frame)))
189 if (kframe->magic != IWMMXT_MAGIC ||
190 kframe->size != IWMMXT_STORAGE_SIZE)
192 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
199 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
203 unsigned long retcode[2];
211 static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
213 struct aux_sigframe __user *aux;
217 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
219 sigdelsetmask(&set, ~_BLOCKABLE);
220 spin_lock_irq(¤t->sighand->siglock);
221 current->blocked = set;
223 spin_unlock_irq(¤t->sighand->siglock);
226 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
227 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
228 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
229 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
230 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
231 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
232 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
233 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
234 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
235 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
236 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
237 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
238 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
239 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
240 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
241 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
242 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
244 err |= !valid_user_regs(regs);
246 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
249 err |= restore_crunch_context(&aux->crunch);
252 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
253 err |= restore_iwmmxt_context(&aux->iwmmxt);
257 // err |= vfp_restore_state(&sf->aux.vfp);
263 asmlinkage int sys_sigreturn(struct pt_regs *regs)
265 struct sigframe __user *frame;
267 /* Always make any pending restarted system calls return -EINTR */
268 current_thread_info()->restart_block.fn = do_no_restart_syscall;
271 * Since we stacked the signal on a 64-bit boundary,
272 * then 'sp' should be word aligned here. If it's
273 * not, then the user is trying to mess with us.
275 if (regs->ARM_sp & 7)
278 frame = (struct sigframe __user *)regs->ARM_sp;
280 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
283 if (restore_sigframe(regs, frame))
286 /* Send SIGTRAP if we're single-stepping */
287 if (current->ptrace & PT_SINGLESTEP) {
288 ptrace_cancel_bpt(current);
289 send_sig(SIGTRAP, current, 1);
295 force_sig(SIGSEGV, current);
299 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
301 struct rt_sigframe __user *frame;
303 /* Always make any pending restarted system calls return -EINTR */
304 current_thread_info()->restart_block.fn = do_no_restart_syscall;
307 * Since we stacked the signal on a 64-bit boundary,
308 * then 'sp' should be word aligned here. If it's
309 * not, then the user is trying to mess with us.
311 if (regs->ARM_sp & 7)
314 frame = (struct rt_sigframe __user *)regs->ARM_sp;
316 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
319 if (restore_sigframe(regs, &frame->sig))
322 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
325 /* Send SIGTRAP if we're single-stepping */
326 if (current->ptrace & PT_SINGLESTEP) {
327 ptrace_cancel_bpt(current);
328 send_sig(SIGTRAP, current, 1);
334 force_sig(SIGSEGV, current);
339 setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
341 struct aux_sigframe __user *aux;
344 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
345 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
346 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
347 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
348 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
349 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
350 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
351 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
352 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
353 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
354 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
355 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
356 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
357 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
358 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
359 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
360 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
362 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
363 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
364 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
365 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
367 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
369 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
372 err |= preserve_crunch_context(&aux->crunch);
375 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
376 err |= preserve_iwmmxt_context(&aux->iwmmxt);
380 // err |= vfp_save_state(&sf->aux.vfp);
382 __put_user_error(0, &aux->end_magic, err);
387 static inline void __user *
388 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
390 unsigned long sp = regs->ARM_sp;
394 * This is the X/Open sanctioned signal stack switching.
396 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
397 sp = current->sas_ss_sp + current->sas_ss_size;
400 * ATPCS B01 mandates 8-byte alignment
402 frame = (void __user *)((sp - framesize) & ~7);
405 * Check that we can actually write to the signal frame.
407 if (!access_ok(VERIFY_WRITE, frame, framesize))
414 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
415 unsigned long __user *rc, void __user *frame, int usig)
417 unsigned long handler = (unsigned long)ka->sa.sa_handler;
418 unsigned long retcode;
420 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
423 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
425 if (ka->sa.sa_flags & SA_THIRTYTWO)
426 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
428 #ifdef CONFIG_ARM_THUMB
429 if (elf_hwcap & HWCAP_THUMB) {
431 * The LSB of the handler determines if we're going to
432 * be using THUMB or ARM mode for this signal handler.
443 if (ka->sa.sa_flags & SA_RESTORER) {
444 retcode = (unsigned long)ka->sa.sa_restorer;
446 unsigned int idx = thumb << 1;
448 if (ka->sa.sa_flags & SA_SIGINFO)
451 if (__put_user(sigreturn_codes[idx], rc) ||
452 __put_user(sigreturn_codes[idx+1], rc+1))
455 if (cpsr & MODE32_BIT) {
457 * 32-bit code can use the new high-page
458 * signal return code support.
460 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
463 * Ensure that the instruction cache sees
464 * the return code written onto the stack.
466 flush_icache_range((unsigned long)rc,
467 (unsigned long)(rc + 2));
469 retcode = ((unsigned long)rc) + thumb;
474 regs->ARM_sp = (unsigned long)frame;
475 regs->ARM_lr = retcode;
476 regs->ARM_pc = handler;
477 regs->ARM_cpsr = cpsr;
483 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
485 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
492 * Set uc.uc_flags to a value which sc.trap_no would never have.
494 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
496 err |= setup_sigframe(frame, regs, set);
498 err = setup_return(regs, ka, frame->retcode, frame, usig);
504 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
505 sigset_t *set, struct pt_regs *regs)
507 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
514 err |= copy_siginfo_to_user(&frame->info, info);
516 __put_user_error(0, &frame->sig.uc.uc_flags, err);
517 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
519 memset(&stack, 0, sizeof(stack));
520 stack.ss_sp = (void __user *)current->sas_ss_sp;
521 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
522 stack.ss_size = current->sas_ss_size;
523 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
525 err |= setup_sigframe(&frame->sig, regs, set);
527 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
531 * For realtime signals we must also set the second and third
532 * arguments for the signal handler.
533 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
535 regs->ARM_r1 = (unsigned long)&frame->info;
536 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
542 static inline void restart_syscall(struct pt_regs *regs)
544 regs->ARM_r0 = regs->ARM_ORIG_r0;
545 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
549 * OK, we're invoking a handler
552 handle_signal(unsigned long sig, struct k_sigaction *ka,
553 siginfo_t *info, sigset_t *oldset,
554 struct pt_regs * regs, int syscall)
556 struct thread_info *thread = current_thread_info();
557 struct task_struct *tsk = current;
562 * If we were from a system call, check for system call restarting...
565 switch (regs->ARM_r0) {
566 case -ERESTART_RESTARTBLOCK:
567 case -ERESTARTNOHAND:
568 regs->ARM_r0 = -EINTR;
571 if (!(ka->sa.sa_flags & SA_RESTART)) {
572 regs->ARM_r0 = -EINTR;
576 case -ERESTARTNOINTR:
577 restart_syscall(regs);
582 * translate the signal
584 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
585 usig = thread->exec_domain->signal_invmap[usig];
588 * Set up the stack frame
590 if (ka->sa.sa_flags & SA_SIGINFO)
591 ret = setup_rt_frame(usig, ka, info, oldset, regs);
593 ret = setup_frame(usig, ka, oldset, regs);
596 * Check that the resulting registers are actually sane.
598 ret |= !valid_user_regs(regs);
601 force_sigsegv(sig, tsk);
606 * Block the signal if we were successful.
608 spin_lock_irq(&tsk->sighand->siglock);
609 sigorsets(&tsk->blocked, &tsk->blocked,
611 if (!(ka->sa.sa_flags & SA_NODEFER))
612 sigaddset(&tsk->blocked, sig);
614 spin_unlock_irq(&tsk->sighand->siglock);
619 * Note that 'init' is a special process: it doesn't get signals it doesn't
620 * want to handle. Thus you cannot kill init even with a SIGKILL even by
623 * Note that we go through the signals twice: once to check the signals that
624 * the kernel can handle, and then we build all the user-level signal handling
625 * stack-frames in one go after that.
627 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
629 struct k_sigaction ka;
634 * We want the common case to go fast, which
635 * is why we may in certain cases get here from
636 * kernel mode. Just return without doing anything
639 if (!user_mode(regs))
645 if (current->ptrace & PT_SINGLESTEP)
646 ptrace_cancel_bpt(current);
648 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
650 handle_signal(signr, &ka, &info, oldset, regs, syscall);
651 if (current->ptrace & PT_SINGLESTEP)
652 ptrace_set_bpt(current);
658 * No signal to deliver to the process - restart the syscall.
661 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
662 if (thumb_mode(regs)) {
663 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
666 #if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
667 regs->ARM_r7 = __NR_restart_syscall;
671 u32 swival = __NR_restart_syscall;
674 usp = (u32 __user *)regs->ARM_sp;
677 * Either we supports OABI only, or we have
678 * EABI with the OABI compat layer enabled.
679 * In the later case we don't know if user
680 * space is EABI or not, and if not we must
681 * not clobber r7. Always using the OABI
682 * syscall solves that issue and works for
685 swival = swival - __NR_SYSCALL_BASE + __NR_OABI_SYSCALL_BASE;
687 put_user(regs->ARM_pc, &usp[0]);
688 /* swi __NR_restart_syscall */
689 put_user(0xef000000 | swival, &usp[1]);
690 /* ldr pc, [sp], #12 */
691 put_user(0xe49df00c, &usp[2]);
693 flush_icache_range((unsigned long)usp,
694 (unsigned long)(usp + 3));
696 regs->ARM_pc = regs->ARM_sp + 4;
700 if (regs->ARM_r0 == -ERESTARTNOHAND ||
701 regs->ARM_r0 == -ERESTARTSYS ||
702 regs->ARM_r0 == -ERESTARTNOINTR) {
703 restart_syscall(regs);
706 if (current->ptrace & PT_SINGLESTEP)
707 ptrace_set_bpt(current);
712 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
714 if (thread_flags & _TIF_SIGPENDING)
715 do_signal(¤t->blocked, regs, syscall);