2 * linux/arch/arm26/kernel/signal.c
4 * Copyright (C) 1995-2002 Russell King
5 * Copyright (C) 2003 Ian Molton (ARM26)
7 * FIXME!!! This is probably very broken (13/05/2003)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/config.h>
14 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/wait.h>
22 #include <linux/ptrace.h>
23 #include <linux/personality.h>
24 #include <linux/tty.h>
25 #include <linux/binfmts.h>
26 #include <linux/elf.h>
28 #include <asm/pgalloc.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
31 #include <asm/unistd.h>
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 * For ARM syscalls, we encode the syscall number into the instruction.
40 #define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
41 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
43 static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
46 * atomically swap in the new signal mask, and wait for a signal.
48 asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
53 spin_lock_irq(¤t->sighand->siglock);
54 saveset = current->blocked;
55 siginitset(¤t->blocked, mask);
57 spin_unlock_irq(¤t->sighand->siglock);
58 regs->ARM_r0 = -EINTR;
61 current->state = TASK_INTERRUPTIBLE;
63 if (do_signal(&saveset, regs, 0))
69 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs *regs)
71 sigset_t saveset, newset;
73 /* XXX: Don't preclude handling different sized sigset_t's. */
74 if (sigsetsize != sizeof(sigset_t))
77 if (copy_from_user(&newset, unewset, sizeof(newset)))
79 sigdelsetmask(&newset, ~_BLOCKABLE);
81 spin_lock_irq(¤t->sighand->siglock);
82 saveset = current->blocked;
83 current->blocked = newset;
85 spin_unlock_irq(¤t->sighand->siglock);
86 regs->ARM_r0 = -EINTR;
89 current->state = TASK_INTERRUPTIBLE;
91 if (do_signal(&saveset, regs, 0))
97 sys_sigaction(int sig, const struct old_sigaction *act,
98 struct old_sigaction *oact)
100 struct k_sigaction new_ka, old_ka;
105 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
106 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
107 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
109 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
110 __get_user(mask, &act->sa_mask);
111 siginitset(&new_ka.sa.sa_mask, mask);
114 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
117 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
118 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
119 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
121 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
122 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
129 * Do a signal return; undo the signal stack.
133 struct sigcontext sc;
134 unsigned long extramask[_NSIG_WORDS-1];
135 unsigned long retcode;
140 struct siginfo *pinfo;
144 unsigned long retcode;
148 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
152 __get_user_error(regs->ARM_r0, &sc->arm_r0, err);
153 __get_user_error(regs->ARM_r1, &sc->arm_r1, err);
154 __get_user_error(regs->ARM_r2, &sc->arm_r2, err);
155 __get_user_error(regs->ARM_r3, &sc->arm_r3, err);
156 __get_user_error(regs->ARM_r4, &sc->arm_r4, err);
157 __get_user_error(regs->ARM_r5, &sc->arm_r5, err);
158 __get_user_error(regs->ARM_r6, &sc->arm_r6, err);
159 __get_user_error(regs->ARM_r7, &sc->arm_r7, err);
160 __get_user_error(regs->ARM_r8, &sc->arm_r8, err);
161 __get_user_error(regs->ARM_r9, &sc->arm_r9, err);
162 __get_user_error(regs->ARM_r10, &sc->arm_r10, err);
163 __get_user_error(regs->ARM_fp, &sc->arm_fp, err);
164 __get_user_error(regs->ARM_ip, &sc->arm_ip, err);
165 __get_user_error(regs->ARM_sp, &sc->arm_sp, err);
166 __get_user_error(regs->ARM_lr, &sc->arm_lr, err);
167 __get_user_error(regs->ARM_pc, &sc->arm_pc, err);
169 err |= !valid_user_regs(regs);
174 asmlinkage int sys_sigreturn(struct pt_regs *regs)
176 struct sigframe *frame;
180 * Since we stacked the signal on a 64-bit boundary,
181 * then 'sp' should be word aligned here. If it's
182 * not, then the user is trying to mess with us.
184 if (regs->ARM_sp & 7)
187 frame = (struct sigframe *)regs->ARM_sp;
189 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
191 if (__get_user(set.sig[0], &frame->sc.oldmask)
193 && __copy_from_user(&set.sig[1], &frame->extramask,
194 sizeof(frame->extramask))))
197 sigdelsetmask(&set, ~_BLOCKABLE);
198 spin_lock_irq(¤t->sighand->siglock);
199 current->blocked = set;
201 spin_unlock_irq(¤t->sighand->siglock);
203 if (restore_sigcontext(regs, &frame->sc))
206 /* Send SIGTRAP if we're single-stepping */
207 if (current->ptrace & PT_SINGLESTEP) {
208 ptrace_cancel_bpt(current);
209 send_sig(SIGTRAP, current, 1);
215 force_sig(SIGSEGV, current);
219 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
221 struct rt_sigframe *frame;
225 * Since we stacked the signal on a 64-bit boundary,
226 * then 'sp' should be word aligned here. If it's
227 * not, then the user is trying to mess with us.
229 if (regs->ARM_sp & 7)
232 frame = (struct rt_sigframe *)regs->ARM_sp;
234 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
236 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
239 sigdelsetmask(&set, ~_BLOCKABLE);
240 spin_lock_irq(¤t->sighand->siglock);
241 current->blocked = set;
243 spin_unlock_irq(¤t->sighand->siglock);
245 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
248 /* Send SIGTRAP if we're single-stepping */
249 if (current->ptrace & PT_SINGLESTEP) {
250 ptrace_cancel_bpt(current);
251 send_sig(SIGTRAP, current, 1);
257 force_sig(SIGSEGV, current);
262 setup_sigcontext(struct sigcontext *sc, /*struct _fpstate *fpstate,*/
263 struct pt_regs *regs, unsigned long mask)
267 __put_user_error(regs->ARM_r0, &sc->arm_r0, err);
268 __put_user_error(regs->ARM_r1, &sc->arm_r1, err);
269 __put_user_error(regs->ARM_r2, &sc->arm_r2, err);
270 __put_user_error(regs->ARM_r3, &sc->arm_r3, err);
271 __put_user_error(regs->ARM_r4, &sc->arm_r4, err);
272 __put_user_error(regs->ARM_r5, &sc->arm_r5, err);
273 __put_user_error(regs->ARM_r6, &sc->arm_r6, err);
274 __put_user_error(regs->ARM_r7, &sc->arm_r7, err);
275 __put_user_error(regs->ARM_r8, &sc->arm_r8, err);
276 __put_user_error(regs->ARM_r9, &sc->arm_r9, err);
277 __put_user_error(regs->ARM_r10, &sc->arm_r10, err);
278 __put_user_error(regs->ARM_fp, &sc->arm_fp, err);
279 __put_user_error(regs->ARM_ip, &sc->arm_ip, err);
280 __put_user_error(regs->ARM_sp, &sc->arm_sp, err);
281 __put_user_error(regs->ARM_lr, &sc->arm_lr, err);
282 __put_user_error(regs->ARM_pc, &sc->arm_pc, err);
284 __put_user_error(current->thread.trap_no, &sc->trap_no, err);
285 __put_user_error(current->thread.error_code, &sc->error_code, err);
286 __put_user_error(current->thread.address, &sc->fault_address, err);
287 __put_user_error(mask, &sc->oldmask, err);
293 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
295 unsigned long sp = regs->ARM_sp;
298 * This is the X/Open sanctioned signal stack switching.
300 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
301 sp = current->sas_ss_sp + current->sas_ss_size;
304 * ATPCS B01 mandates 8-byte alignment
306 return (void *)((sp - framesize) & ~7);
310 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
311 unsigned long *rc, void *frame, int usig)
313 unsigned long handler = (unsigned long)ka->sa.sa_handler;
314 unsigned long retcode;
316 if (ka->sa.sa_flags & SA_RESTORER) {
317 retcode = (unsigned long)ka->sa.sa_restorer;
320 if (__put_user((ka->sa.sa_flags & SA_SIGINFO)?SWI_SYS_RT_SIGRETURN:SWI_SYS_SIGRETURN, rc))
323 retcode = ((unsigned long)rc);
327 regs->ARM_sp = (unsigned long)frame;
328 regs->ARM_lr = retcode;
329 regs->ARM_pc = handler & ~3;
335 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
337 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
340 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
343 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
345 if (_NSIG_WORDS > 1) {
346 err |= __copy_to_user(frame->extramask, &set->sig[1],
347 sizeof(frame->extramask));
351 err = setup_return(regs, ka, &frame->retcode, frame, usig);
357 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
358 sigset_t *set, struct pt_regs *regs)
360 struct rt_sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
363 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
366 __put_user_error(&frame->info, &frame->pinfo, err);
367 __put_user_error(&frame->uc, &frame->puc, err);
368 err |= copy_siginfo_to_user(&frame->info, info);
370 /* Clear all the bits of the ucontext we don't use. */
371 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
373 err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
375 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
378 err = setup_return(regs, ka, &frame->retcode, frame, usig);
382 * For realtime signals we must also set the second and third
383 * arguments for the signal handler.
384 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
386 regs->ARM_r1 = (unsigned long)frame->pinfo;
387 regs->ARM_r2 = (unsigned long)frame->puc;
393 static inline void restart_syscall(struct pt_regs *regs)
395 regs->ARM_r0 = regs->ARM_ORIG_r0;
400 * OK, we're invoking a handler
403 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
404 struct pt_regs * regs, int syscall)
406 struct thread_info *thread = current_thread_info();
407 struct task_struct *tsk = current;
408 struct k_sigaction *ka = &tsk->sighand->action[sig-1];
413 * If we were from a system call, check for system call restarting...
416 switch (regs->ARM_r0) {
417 case -ERESTART_RESTARTBLOCK:
418 current_thread_info()->restart_block.fn =
419 do_no_restart_syscall;
420 case -ERESTARTNOHAND:
421 regs->ARM_r0 = -EINTR;
424 if (!(ka->sa.sa_flags & SA_RESTART)) {
425 regs->ARM_r0 = -EINTR;
429 case -ERESTARTNOINTR:
430 restart_syscall(regs);
435 * translate the signal
437 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
438 usig = thread->exec_domain->signal_invmap[usig];
441 * Set up the stack frame
443 if (ka->sa.sa_flags & SA_SIGINFO)
444 ret = setup_rt_frame(usig, ka, info, oldset, regs);
446 ret = setup_frame(usig, ka, oldset, regs);
449 * Check that the resulting registers are actually sane.
451 ret |= !valid_user_regs(regs);
454 if (ka->sa.sa_flags & SA_ONESHOT)
455 ka->sa.sa_handler = SIG_DFL;
457 spin_lock_irq(&tsk->sighand->siglock);
458 sigorsets(&tsk->blocked, &tsk->blocked,
460 if (!(ka->sa.sa_flags & SA_NODEFER))
461 sigaddset(&tsk->blocked, sig);
463 spin_unlock_irq(&tsk->sighand->siglock);
467 force_sigsegv(sig, tsk);
471 * Note that 'init' is a special process: it doesn't get signals it doesn't
472 * want to handle. Thus you cannot kill init even with a SIGKILL even by
475 * Note that we go through the signals twice: once to check the signals that
476 * the kernel can handle, and then we build all the user-level signal handling
477 * stack-frames in one go after that.
479 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
483 struct k_sigaction ka;
486 * We want the common case to go fast, which
487 * is why we may in certain cases get here from
488 * kernel mode. Just return without doing anything
491 if (!user_mode(regs))
494 if (current->ptrace & PT_SINGLESTEP)
495 ptrace_cancel_bpt(current);
497 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
499 handle_signal(signr, &info, oldset, regs, syscall);
500 if (current->ptrace & PT_SINGLESTEP)
501 ptrace_set_bpt(current);
506 * No signal to deliver to the process - restart the syscall.
509 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
513 usp = (u32 *)regs->ARM_sp;
515 put_user(regs->ARM_pc, &usp[0]);
516 /* swi __NR_restart_syscall */
517 put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
518 /* ldr pc, [sp], #12 */
519 // FIXME!!! is #12 correct there?
520 put_user(0xe49df00c, &usp[2]);
522 regs->ARM_pc = regs->ARM_sp + 4;
524 if (regs->ARM_r0 == -ERESTARTNOHAND ||
525 regs->ARM_r0 == -ERESTARTSYS ||
526 regs->ARM_r0 == -ERESTARTNOINTR) {
527 restart_syscall(regs);
530 if (current->ptrace & PT_SINGLESTEP)
531 ptrace_set_bpt(current);
536 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
538 if (thread_flags & _TIF_SIGPENDING)
539 do_signal(¤t->blocked, regs, syscall);