Linux 2.6.19.7
[linux-2.6/suspend2-2.6.19.git] / arch / arm26 / kernel / signal.c
blob6a8ef8da6dab07000467c98fbd7ea95a2617dd10
1 /*
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/sched.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/signal.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/personality.h>
23 #include <linux/tty.h>
24 #include <linux/binfmts.h>
25 #include <linux/elf.h>
27 #include <asm/pgalloc.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
32 #include "ptrace.h"
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 * For ARM syscalls, we encode the syscall number into the instruction.
39 #define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
40 #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
42 static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
45 * atomically swap in the new signal mask, and wait for a signal.
47 asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
49 sigset_t saveset;
51 mask &= _BLOCKABLE;
52 spin_lock_irq(&current->sighand->siglock);
53 saveset = current->blocked;
54 siginitset(&current->blocked, mask);
55 recalc_sigpending();
56 spin_unlock_irq(&current->sighand->siglock);
57 regs->ARM_r0 = -EINTR;
59 while (1) {
60 current->state = TASK_INTERRUPTIBLE;
61 schedule();
62 if (do_signal(&saveset, regs, 0))
63 return regs->ARM_r0;
67 asmlinkage int
68 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs *regs)
70 sigset_t saveset, newset;
72 /* XXX: Don't preclude handling different sized sigset_t's. */
73 if (sigsetsize != sizeof(sigset_t))
74 return -EINVAL;
76 if (copy_from_user(&newset, unewset, sizeof(newset)))
77 return -EFAULT;
78 sigdelsetmask(&newset, ~_BLOCKABLE);
80 spin_lock_irq(&current->sighand->siglock);
81 saveset = current->blocked;
82 current->blocked = newset;
83 recalc_sigpending();
84 spin_unlock_irq(&current->sighand->siglock);
85 regs->ARM_r0 = -EINTR;
87 while (1) {
88 current->state = TASK_INTERRUPTIBLE;
89 schedule();
90 if (do_signal(&saveset, regs, 0))
91 return regs->ARM_r0;
95 asmlinkage int
96 sys_sigaction(int sig, const struct old_sigaction *act,
97 struct old_sigaction *oact)
99 struct k_sigaction new_ka, old_ka;
100 int ret;
102 if (act) {
103 old_sigset_t mask;
104 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
105 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
106 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
107 return -EFAULT;
108 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
109 __get_user(mask, &act->sa_mask);
110 siginitset(&new_ka.sa.sa_mask, mask);
113 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
115 if (!ret && oact) {
116 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
117 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
118 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
119 return -EFAULT;
120 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
121 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
124 return ret;
128 * Do a signal return; undo the signal stack.
130 struct sigframe
132 struct sigcontext sc;
133 unsigned long extramask[_NSIG_WORDS-1];
134 unsigned long retcode;
137 struct rt_sigframe
139 struct siginfo *pinfo;
140 void *puc;
141 struct siginfo info;
142 struct ucontext uc;
143 unsigned long retcode;
146 static int
147 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
149 int err = 0;
151 __get_user_error(regs->ARM_r0, &sc->arm_r0, err);
152 __get_user_error(regs->ARM_r1, &sc->arm_r1, err);
153 __get_user_error(regs->ARM_r2, &sc->arm_r2, err);
154 __get_user_error(regs->ARM_r3, &sc->arm_r3, err);
155 __get_user_error(regs->ARM_r4, &sc->arm_r4, err);
156 __get_user_error(regs->ARM_r5, &sc->arm_r5, err);
157 __get_user_error(regs->ARM_r6, &sc->arm_r6, err);
158 __get_user_error(regs->ARM_r7, &sc->arm_r7, err);
159 __get_user_error(regs->ARM_r8, &sc->arm_r8, err);
160 __get_user_error(regs->ARM_r9, &sc->arm_r9, err);
161 __get_user_error(regs->ARM_r10, &sc->arm_r10, err);
162 __get_user_error(regs->ARM_fp, &sc->arm_fp, err);
163 __get_user_error(regs->ARM_ip, &sc->arm_ip, err);
164 __get_user_error(regs->ARM_sp, &sc->arm_sp, err);
165 __get_user_error(regs->ARM_lr, &sc->arm_lr, err);
166 __get_user_error(regs->ARM_pc, &sc->arm_pc, err);
168 err |= !valid_user_regs(regs);
170 return err;
173 asmlinkage int sys_sigreturn(struct pt_regs *regs)
175 struct sigframe *frame;
176 sigset_t set;
179 * Since we stacked the signal on a 64-bit boundary,
180 * then 'sp' should be word aligned here. If it's
181 * not, then the user is trying to mess with us.
183 if (regs->ARM_sp & 7)
184 goto badframe;
186 frame = (struct sigframe *)regs->ARM_sp;
188 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
189 goto badframe;
190 if (__get_user(set.sig[0], &frame->sc.oldmask)
191 || (_NSIG_WORDS > 1
192 && __copy_from_user(&set.sig[1], &frame->extramask,
193 sizeof(frame->extramask))))
194 goto badframe;
196 sigdelsetmask(&set, ~_BLOCKABLE);
197 spin_lock_irq(&current->sighand->siglock);
198 current->blocked = set;
199 recalc_sigpending();
200 spin_unlock_irq(&current->sighand->siglock);
202 if (restore_sigcontext(regs, &frame->sc))
203 goto badframe;
205 /* Send SIGTRAP if we're single-stepping */
206 if (current->ptrace & PT_SINGLESTEP) {
207 ptrace_cancel_bpt(current);
208 send_sig(SIGTRAP, current, 1);
211 return regs->ARM_r0;
213 badframe:
214 force_sig(SIGSEGV, current);
215 return 0;
218 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
220 struct rt_sigframe *frame;
221 sigset_t set;
224 * Since we stacked the signal on a 64-bit boundary,
225 * then 'sp' should be word aligned here. If it's
226 * not, then the user is trying to mess with us.
228 if (regs->ARM_sp & 7)
229 goto badframe;
231 frame = (struct rt_sigframe *)regs->ARM_sp;
233 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
234 goto badframe;
235 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
236 goto badframe;
238 sigdelsetmask(&set, ~_BLOCKABLE);
239 spin_lock_irq(&current->sighand->siglock);
240 current->blocked = set;
241 recalc_sigpending();
242 spin_unlock_irq(&current->sighand->siglock);
244 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
245 goto badframe;
247 /* Send SIGTRAP if we're single-stepping */
248 if (current->ptrace & PT_SINGLESTEP) {
249 ptrace_cancel_bpt(current);
250 send_sig(SIGTRAP, current, 1);
253 return regs->ARM_r0;
255 badframe:
256 force_sig(SIGSEGV, current);
257 return 0;
260 static int
261 setup_sigcontext(struct sigcontext *sc, /*struct _fpstate *fpstate,*/
262 struct pt_regs *regs, unsigned long mask)
264 int err = 0;
266 __put_user_error(regs->ARM_r0, &sc->arm_r0, err);
267 __put_user_error(regs->ARM_r1, &sc->arm_r1, err);
268 __put_user_error(regs->ARM_r2, &sc->arm_r2, err);
269 __put_user_error(regs->ARM_r3, &sc->arm_r3, err);
270 __put_user_error(regs->ARM_r4, &sc->arm_r4, err);
271 __put_user_error(regs->ARM_r5, &sc->arm_r5, err);
272 __put_user_error(regs->ARM_r6, &sc->arm_r6, err);
273 __put_user_error(regs->ARM_r7, &sc->arm_r7, err);
274 __put_user_error(regs->ARM_r8, &sc->arm_r8, err);
275 __put_user_error(regs->ARM_r9, &sc->arm_r9, err);
276 __put_user_error(regs->ARM_r10, &sc->arm_r10, err);
277 __put_user_error(regs->ARM_fp, &sc->arm_fp, err);
278 __put_user_error(regs->ARM_ip, &sc->arm_ip, err);
279 __put_user_error(regs->ARM_sp, &sc->arm_sp, err);
280 __put_user_error(regs->ARM_lr, &sc->arm_lr, err);
281 __put_user_error(regs->ARM_pc, &sc->arm_pc, err);
283 __put_user_error(current->thread.trap_no, &sc->trap_no, err);
284 __put_user_error(current->thread.error_code, &sc->error_code, err);
285 __put_user_error(current->thread.address, &sc->fault_address, err);
286 __put_user_error(mask, &sc->oldmask, err);
288 return err;
291 static inline void *
292 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
294 unsigned long sp = regs->ARM_sp;
297 * This is the X/Open sanctioned signal stack switching.
299 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
300 sp = current->sas_ss_sp + current->sas_ss_size;
303 * ATPCS B01 mandates 8-byte alignment
305 return (void *)((sp - framesize) & ~7);
308 static int
309 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
310 unsigned long *rc, void *frame, int usig)
312 unsigned long handler = (unsigned long)ka->sa.sa_handler;
313 unsigned long retcode;
315 if (ka->sa.sa_flags & SA_RESTORER) {
316 retcode = (unsigned long)ka->sa.sa_restorer;
317 } else {
319 if (__put_user((ka->sa.sa_flags & SA_SIGINFO)?SWI_SYS_RT_SIGRETURN:SWI_SYS_SIGRETURN, rc))
320 return 1;
322 retcode = ((unsigned long)rc);
325 regs->ARM_r0 = usig;
326 regs->ARM_sp = (unsigned long)frame;
327 regs->ARM_lr = retcode;
328 regs->ARM_pc = handler & ~3;
330 return 0;
333 static int
334 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
336 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
337 int err = 0;
339 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
340 return 1;
342 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
344 if (_NSIG_WORDS > 1) {
345 err |= __copy_to_user(frame->extramask, &set->sig[1],
346 sizeof(frame->extramask));
349 if (err == 0)
350 err = setup_return(regs, ka, &frame->retcode, frame, usig);
352 return err;
355 static int
356 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
357 sigset_t *set, struct pt_regs *regs)
359 struct rt_sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
360 int err = 0;
362 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
363 return 1;
365 __put_user_error(&frame->info, &frame->pinfo, err);
366 __put_user_error(&frame->uc, &frame->puc, err);
367 err |= copy_siginfo_to_user(&frame->info, info);
369 /* Clear all the bits of the ucontext we don't use. */
370 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
372 err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
373 regs, set->sig[0]);
374 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
376 if (err == 0)
377 err = setup_return(regs, ka, &frame->retcode, frame, usig);
379 if (err == 0) {
381 * For realtime signals we must also set the second and third
382 * arguments for the signal handler.
383 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
385 regs->ARM_r1 = (unsigned long)frame->pinfo;
386 regs->ARM_r2 = (unsigned long)frame->puc;
389 return err;
392 static inline void restart_syscall(struct pt_regs *regs)
394 regs->ARM_r0 = regs->ARM_ORIG_r0;
395 regs->ARM_pc -= 4;
399 * OK, we're invoking a handler
401 static void
402 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
403 struct pt_regs * regs, int syscall)
405 struct thread_info *thread = current_thread_info();
406 struct task_struct *tsk = current;
407 struct k_sigaction *ka = &tsk->sighand->action[sig-1];
408 int usig = sig;
409 int ret;
412 * If we were from a system call, check for system call restarting...
414 if (syscall) {
415 switch (regs->ARM_r0) {
416 case -ERESTART_RESTARTBLOCK:
417 current_thread_info()->restart_block.fn =
418 do_no_restart_syscall;
419 case -ERESTARTNOHAND:
420 regs->ARM_r0 = -EINTR;
421 break;
422 case -ERESTARTSYS:
423 if (!(ka->sa.sa_flags & SA_RESTART)) {
424 regs->ARM_r0 = -EINTR;
425 break;
427 /* fallthrough */
428 case -ERESTARTNOINTR:
429 restart_syscall(regs);
434 * translate the signal
436 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
437 usig = thread->exec_domain->signal_invmap[usig];
440 * Set up the stack frame
442 if (ka->sa.sa_flags & SA_SIGINFO)
443 ret = setup_rt_frame(usig, ka, info, oldset, regs);
444 else
445 ret = setup_frame(usig, ka, oldset, regs);
448 * Check that the resulting registers are actually sane.
450 ret |= !valid_user_regs(regs);
452 if (ret == 0) {
453 if (ka->sa.sa_flags & SA_ONESHOT)
454 ka->sa.sa_handler = SIG_DFL;
456 spin_lock_irq(&tsk->sighand->siglock);
457 sigorsets(&tsk->blocked, &tsk->blocked,
458 &ka->sa.sa_mask);
459 if (!(ka->sa.sa_flags & SA_NODEFER))
460 sigaddset(&tsk->blocked, sig);
461 recalc_sigpending();
462 spin_unlock_irq(&tsk->sighand->siglock);
463 return;
466 force_sigsegv(sig, tsk);
470 * Note that 'init' is a special process: it doesn't get signals it doesn't
471 * want to handle. Thus you cannot kill init even with a SIGKILL even by
472 * mistake.
474 * Note that we go through the signals twice: once to check the signals that
475 * the kernel can handle, and then we build all the user-level signal handling
476 * stack-frames in one go after that.
478 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
480 siginfo_t info;
481 int signr;
482 struct k_sigaction ka;
485 * We want the common case to go fast, which
486 * is why we may in certain cases get here from
487 * kernel mode. Just return without doing anything
488 * if so.
490 if (!user_mode(regs))
491 return 0;
493 if (current->ptrace & PT_SINGLESTEP)
494 ptrace_cancel_bpt(current);
496 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
497 if (signr > 0) {
498 handle_signal(signr, &info, oldset, regs, syscall);
499 if (current->ptrace & PT_SINGLESTEP)
500 ptrace_set_bpt(current);
501 return 1;
505 * No signal to deliver to the process - restart the syscall.
507 if (syscall) {
508 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
509 u32 *usp;
511 regs->ARM_sp -= 12;
512 usp = (u32 *)regs->ARM_sp;
514 put_user(regs->ARM_pc, &usp[0]);
515 /* swi __NR_restart_syscall */
516 put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
517 /* ldr pc, [sp], #12 */
518 // FIXME!!! is #12 correct there?
519 put_user(0xe49df00c, &usp[2]);
521 regs->ARM_pc = regs->ARM_sp + 4;
523 if (regs->ARM_r0 == -ERESTARTNOHAND ||
524 regs->ARM_r0 == -ERESTARTSYS ||
525 regs->ARM_r0 == -ERESTARTNOINTR) {
526 restart_syscall(regs);
529 if (current->ptrace & PT_SINGLESTEP)
530 ptrace_set_bpt(current);
531 return 0;
534 asmlinkage void
535 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
537 if (thread_flags & _TIF_SIGPENDING)
538 do_signal(&current->blocked, regs, syscall);