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>
16 #include <asm/cacheflush.h>
17 #include <asm/ucontext.h>
18 #include <asm/uaccess.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))
30 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
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);
137 /* iwmmxt_area is 0x98 bytes long, preceeded by 8 bytes of signature */
138 #define IWMMXT_STORAGE_SIZE (0x98 + 8)
139 #define IWMMXT_MAGIC0 0x12ef842a
140 #define IWMMXT_MAGIC1 0x1c07ca71
142 struct iwmmxt_sigframe {
143 unsigned long magic0;
144 unsigned long magic1;
145 unsigned long storage[0x98/4];
148 static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
150 char kbuf[sizeof(*frame) + 8];
151 struct iwmmxt_sigframe *kframe;
153 /* the iWMMXt context must be 64 bit aligned */
154 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
155 kframe->magic0 = IWMMXT_MAGIC0;
156 kframe->magic1 = IWMMXT_MAGIC1;
157 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
158 return __copy_to_user(frame, kframe, sizeof(*frame));
161 static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
163 char kbuf[sizeof(*frame) + 8];
164 struct iwmmxt_sigframe *kframe;
166 /* the iWMMXt context must be 64 bit aligned */
167 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
168 if (__copy_from_user(kframe, frame, sizeof(*frame)))
170 if (kframe->magic0 != IWMMXT_MAGIC0 ||
171 kframe->magic1 != IWMMXT_MAGIC1)
173 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
180 * Auxiliary signal frame. This saves stuff like FP state.
181 * The layout of this structure is not part of the user ABI.
183 struct aux_sigframe {
185 struct iwmmxt_sigframe iwmmxt;
193 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
196 struct sigcontext sc;
197 unsigned long extramask[_NSIG_WORDS-1];
198 unsigned long retcode[2];
199 struct aux_sigframe aux __attribute__((aligned(8)));
203 struct siginfo __user *pinfo;
207 unsigned long retcode[2];
208 struct aux_sigframe aux __attribute__((aligned(8)));
212 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
213 struct aux_sigframe __user *aux)
217 __get_user_error(regs->ARM_r0, &sc->arm_r0, err);
218 __get_user_error(regs->ARM_r1, &sc->arm_r1, err);
219 __get_user_error(regs->ARM_r2, &sc->arm_r2, err);
220 __get_user_error(regs->ARM_r3, &sc->arm_r3, err);
221 __get_user_error(regs->ARM_r4, &sc->arm_r4, err);
222 __get_user_error(regs->ARM_r5, &sc->arm_r5, err);
223 __get_user_error(regs->ARM_r6, &sc->arm_r6, err);
224 __get_user_error(regs->ARM_r7, &sc->arm_r7, err);
225 __get_user_error(regs->ARM_r8, &sc->arm_r8, err);
226 __get_user_error(regs->ARM_r9, &sc->arm_r9, err);
227 __get_user_error(regs->ARM_r10, &sc->arm_r10, err);
228 __get_user_error(regs->ARM_fp, &sc->arm_fp, err);
229 __get_user_error(regs->ARM_ip, &sc->arm_ip, err);
230 __get_user_error(regs->ARM_sp, &sc->arm_sp, err);
231 __get_user_error(regs->ARM_lr, &sc->arm_lr, err);
232 __get_user_error(regs->ARM_pc, &sc->arm_pc, err);
233 __get_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
235 err |= !valid_user_regs(regs);
238 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
239 err |= restore_iwmmxt_context(&aux->iwmmxt);
243 // err |= vfp_restore_state(&aux->vfp);
249 asmlinkage int sys_sigreturn(struct pt_regs *regs)
251 struct sigframe __user *frame;
254 /* Always make any pending restarted system calls return -EINTR */
255 current_thread_info()->restart_block.fn = do_no_restart_syscall;
258 * Since we stacked the signal on a 64-bit boundary,
259 * then 'sp' should be word aligned here. If it's
260 * not, then the user is trying to mess with us.
262 if (regs->ARM_sp & 7)
265 frame = (struct sigframe __user *)regs->ARM_sp;
267 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
269 if (__get_user(set.sig[0], &frame->sc.oldmask)
271 && __copy_from_user(&set.sig[1], &frame->extramask,
272 sizeof(frame->extramask))))
275 sigdelsetmask(&set, ~_BLOCKABLE);
276 spin_lock_irq(¤t->sighand->siglock);
277 current->blocked = set;
279 spin_unlock_irq(¤t->sighand->siglock);
281 if (restore_sigcontext(regs, &frame->sc, &frame->aux))
284 /* Send SIGTRAP if we're single-stepping */
285 if (current->ptrace & PT_SINGLESTEP) {
286 ptrace_cancel_bpt(current);
287 send_sig(SIGTRAP, current, 1);
293 force_sig(SIGSEGV, current);
297 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
299 struct rt_sigframe __user *frame;
302 /* Always make any pending restarted system calls return -EINTR */
303 current_thread_info()->restart_block.fn = do_no_restart_syscall;
306 * Since we stacked the signal on a 64-bit boundary,
307 * then 'sp' should be word aligned here. If it's
308 * not, then the user is trying to mess with us.
310 if (regs->ARM_sp & 7)
313 frame = (struct rt_sigframe __user *)regs->ARM_sp;
315 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
317 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
320 sigdelsetmask(&set, ~_BLOCKABLE);
321 spin_lock_irq(¤t->sighand->siglock);
322 current->blocked = set;
324 spin_unlock_irq(¤t->sighand->siglock);
326 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &frame->aux))
329 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
332 /* Send SIGTRAP if we're single-stepping */
333 if (current->ptrace & PT_SINGLESTEP) {
334 ptrace_cancel_bpt(current);
335 send_sig(SIGTRAP, current, 1);
341 force_sig(SIGSEGV, current);
346 setup_sigcontext(struct sigcontext __user *sc, struct aux_sigframe __user *aux,
347 struct pt_regs *regs, unsigned long mask)
351 __put_user_error(regs->ARM_r0, &sc->arm_r0, err);
352 __put_user_error(regs->ARM_r1, &sc->arm_r1, err);
353 __put_user_error(regs->ARM_r2, &sc->arm_r2, err);
354 __put_user_error(regs->ARM_r3, &sc->arm_r3, err);
355 __put_user_error(regs->ARM_r4, &sc->arm_r4, err);
356 __put_user_error(regs->ARM_r5, &sc->arm_r5, err);
357 __put_user_error(regs->ARM_r6, &sc->arm_r6, err);
358 __put_user_error(regs->ARM_r7, &sc->arm_r7, err);
359 __put_user_error(regs->ARM_r8, &sc->arm_r8, err);
360 __put_user_error(regs->ARM_r9, &sc->arm_r9, err);
361 __put_user_error(regs->ARM_r10, &sc->arm_r10, err);
362 __put_user_error(regs->ARM_fp, &sc->arm_fp, err);
363 __put_user_error(regs->ARM_ip, &sc->arm_ip, err);
364 __put_user_error(regs->ARM_sp, &sc->arm_sp, err);
365 __put_user_error(regs->ARM_lr, &sc->arm_lr, err);
366 __put_user_error(regs->ARM_pc, &sc->arm_pc, err);
367 __put_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
369 __put_user_error(current->thread.trap_no, &sc->trap_no, err);
370 __put_user_error(current->thread.error_code, &sc->error_code, err);
371 __put_user_error(current->thread.address, &sc->fault_address, err);
372 __put_user_error(mask, &sc->oldmask, err);
375 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
376 err |= preserve_iwmmxt_context(&aux->iwmmxt);
380 // err |= vfp_save_state(&aux->vfp);
386 static inline void __user *
387 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
389 unsigned long sp = regs->ARM_sp;
393 * This is the X/Open sanctioned signal stack switching.
395 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
396 sp = current->sas_ss_sp + current->sas_ss_size;
399 * ATPCS B01 mandates 8-byte alignment
401 frame = (void __user *)((sp - framesize) & ~7);
404 * Check that we can actually write to the signal frame.
406 if (!access_ok(VERIFY_WRITE, frame, framesize))
413 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
414 unsigned long __user *rc, void __user *frame, int usig)
416 unsigned long handler = (unsigned long)ka->sa.sa_handler;
417 unsigned long retcode;
419 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
422 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
424 if (ka->sa.sa_flags & SA_THIRTYTWO)
425 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
427 #ifdef CONFIG_ARM_THUMB
428 if (elf_hwcap & HWCAP_THUMB) {
430 * The LSB of the handler determines if we're going to
431 * be using THUMB or ARM mode for this signal handler.
442 if (ka->sa.sa_flags & SA_RESTORER) {
443 retcode = (unsigned long)ka->sa.sa_restorer;
445 unsigned int idx = thumb << 1;
447 if (ka->sa.sa_flags & SA_SIGINFO)
450 if (__put_user(sigreturn_codes[idx], rc) ||
451 __put_user(sigreturn_codes[idx+1], rc+1))
454 if (cpsr & MODE32_BIT) {
456 * 32-bit code can use the new high-page
457 * signal return code support.
459 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
462 * Ensure that the instruction cache sees
463 * the return code written onto the stack.
465 flush_icache_range((unsigned long)rc,
466 (unsigned long)(rc + 2));
468 retcode = ((unsigned long)rc) + thumb;
473 regs->ARM_sp = (unsigned long)frame;
474 regs->ARM_lr = retcode;
475 regs->ARM_pc = handler;
476 regs->ARM_cpsr = cpsr;
482 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
484 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
490 err |= setup_sigcontext(&frame->sc, &frame->aux, regs, set->sig[0]);
492 if (_NSIG_WORDS > 1) {
493 err |= __copy_to_user(frame->extramask, &set->sig[1],
494 sizeof(frame->extramask));
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 __put_user_error(&frame->info, &frame->pinfo, err);
515 __put_user_error(&frame->uc, &frame->puc, err);
516 err |= copy_siginfo_to_user(&frame->info, info);
518 __put_user_error(0, &frame->uc.uc_flags, err);
519 __put_user_error(NULL, &frame->uc.uc_link, err);
521 memset(&stack, 0, sizeof(stack));
522 stack.ss_sp = (void __user *)current->sas_ss_sp;
523 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
524 stack.ss_size = current->sas_ss_size;
525 err |= __copy_to_user(&frame->uc.uc_stack, &stack, sizeof(stack));
527 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->aux,
529 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
532 err = setup_return(regs, ka, frame->retcode, frame, usig);
536 * For realtime signals we must also set the second and third
537 * arguments for the signal handler.
538 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
540 regs->ARM_r1 = (unsigned long)&frame->info;
541 regs->ARM_r2 = (unsigned long)&frame->uc;
547 static inline void restart_syscall(struct pt_regs *regs)
549 regs->ARM_r0 = regs->ARM_ORIG_r0;
550 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
554 * OK, we're invoking a handler
557 handle_signal(unsigned long sig, struct k_sigaction *ka,
558 siginfo_t *info, sigset_t *oldset,
559 struct pt_regs * regs, int syscall)
561 struct thread_info *thread = current_thread_info();
562 struct task_struct *tsk = current;
567 * If we were from a system call, check for system call restarting...
570 switch (regs->ARM_r0) {
571 case -ERESTART_RESTARTBLOCK:
572 case -ERESTARTNOHAND:
573 regs->ARM_r0 = -EINTR;
576 if (!(ka->sa.sa_flags & SA_RESTART)) {
577 regs->ARM_r0 = -EINTR;
581 case -ERESTARTNOINTR:
582 restart_syscall(regs);
587 * translate the signal
589 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
590 usig = thread->exec_domain->signal_invmap[usig];
593 * Set up the stack frame
595 if (ka->sa.sa_flags & SA_SIGINFO)
596 ret = setup_rt_frame(usig, ka, info, oldset, regs);
598 ret = setup_frame(usig, ka, oldset, regs);
601 * Check that the resulting registers are actually sane.
603 ret |= !valid_user_regs(regs);
606 force_sigsegv(sig, tsk);
611 * Block the signal if we were successful.
613 spin_lock_irq(&tsk->sighand->siglock);
614 sigorsets(&tsk->blocked, &tsk->blocked,
616 if (!(ka->sa.sa_flags & SA_NODEFER))
617 sigaddset(&tsk->blocked, sig);
619 spin_unlock_irq(&tsk->sighand->siglock);
624 * Note that 'init' is a special process: it doesn't get signals it doesn't
625 * want to handle. Thus you cannot kill init even with a SIGKILL even by
628 * Note that we go through the signals twice: once to check the signals that
629 * the kernel can handle, and then we build all the user-level signal handling
630 * stack-frames in one go after that.
632 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
634 struct k_sigaction ka;
639 * We want the common case to go fast, which
640 * is why we may in certain cases get here from
641 * kernel mode. Just return without doing anything
644 if (!user_mode(regs))
650 if (current->ptrace & PT_SINGLESTEP)
651 ptrace_cancel_bpt(current);
653 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
655 handle_signal(signr, &ka, &info, oldset, regs, syscall);
656 if (current->ptrace & PT_SINGLESTEP)
657 ptrace_set_bpt(current);
663 * No signal to deliver to the process - restart the syscall.
666 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
667 if (thumb_mode(regs)) {
668 regs->ARM_r7 = __NR_restart_syscall;
674 usp = (u32 __user *)regs->ARM_sp;
676 put_user(regs->ARM_pc, &usp[0]);
677 /* swi __NR_restart_syscall */
678 put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
679 /* ldr pc, [sp], #12 */
680 put_user(0xe49df00c, &usp[2]);
682 flush_icache_range((unsigned long)usp,
683 (unsigned long)(usp + 3));
685 regs->ARM_pc = regs->ARM_sp + 4;
688 if (regs->ARM_r0 == -ERESTARTNOHAND ||
689 regs->ARM_r0 == -ERESTARTSYS ||
690 regs->ARM_r0 == -ERESTARTNOINTR) {
691 restart_syscall(regs);
694 if (current->ptrace & PT_SINGLESTEP)
695 ptrace_set_bpt(current);
700 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
702 if (thread_flags & _TIF_SIGPENDING)
703 do_signal(¤t->blocked, regs, syscall);