[ARM] Re-use sigframe within rt_sigframe
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / kernel / signal.c
blob652bb1a33c06dfa42f97457cd548b75e939683c9
1 /*
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.
9 */
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>
21 #include "ptrace.h"
22 #include "signal.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)
57 sigset_t saveset;
59 mask &= _BLOCKABLE;
60 spin_lock_irq(&current->sighand->siglock);
61 saveset = current->blocked;
62 siginitset(&current->blocked, mask);
63 recalc_sigpending();
64 spin_unlock_irq(&current->sighand->siglock);
65 regs->ARM_r0 = -EINTR;
67 while (1) {
68 current->state = TASK_INTERRUPTIBLE;
69 schedule();
70 if (do_signal(&saveset, regs, 0))
71 return regs->ARM_r0;
75 asmlinkage int
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))
82 return -EINVAL;
84 if (copy_from_user(&newset, unewset, sizeof(newset)))
85 return -EFAULT;
86 sigdelsetmask(&newset, ~_BLOCKABLE);
88 spin_lock_irq(&current->sighand->siglock);
89 saveset = current->blocked;
90 current->blocked = newset;
91 recalc_sigpending();
92 spin_unlock_irq(&current->sighand->siglock);
93 regs->ARM_r0 = -EINTR;
95 while (1) {
96 current->state = TASK_INTERRUPTIBLE;
97 schedule();
98 if (do_signal(&saveset, regs, 0))
99 return regs->ARM_r0;
103 asmlinkage int
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;
108 int ret;
110 if (act) {
111 old_sigset_t mask;
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))
115 return -EFAULT;
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);
123 if (!ret && oact) {
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))
127 return -EFAULT;
128 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
129 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
132 return ret;
135 #ifdef CONFIG_IWMMXT
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)))
169 return -1;
170 if (kframe->magic0 != IWMMXT_MAGIC0 ||
171 kframe->magic1 != IWMMXT_MAGIC1)
172 return -1;
173 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
174 return 0;
177 #endif
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 {
184 #ifdef CONFIG_IWMMXT
185 struct iwmmxt_sigframe iwmmxt;
186 #endif
187 #ifdef CONFIG_VFP
188 union vfp_state vfp;
189 #endif
193 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
195 struct sigframe {
196 struct ucontext uc;
197 unsigned long retcode[2];
198 struct aux_sigframe aux __attribute__((aligned(8)));
201 struct rt_sigframe {
202 struct siginfo info;
203 struct sigframe sig;
206 static int
207 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
208 struct aux_sigframe __user *aux)
210 int err = 0;
212 __get_user_error(regs->ARM_r0, &sc->arm_r0, err);
213 __get_user_error(regs->ARM_r1, &sc->arm_r1, err);
214 __get_user_error(regs->ARM_r2, &sc->arm_r2, err);
215 __get_user_error(regs->ARM_r3, &sc->arm_r3, err);
216 __get_user_error(regs->ARM_r4, &sc->arm_r4, err);
217 __get_user_error(regs->ARM_r5, &sc->arm_r5, err);
218 __get_user_error(regs->ARM_r6, &sc->arm_r6, err);
219 __get_user_error(regs->ARM_r7, &sc->arm_r7, err);
220 __get_user_error(regs->ARM_r8, &sc->arm_r8, err);
221 __get_user_error(regs->ARM_r9, &sc->arm_r9, err);
222 __get_user_error(regs->ARM_r10, &sc->arm_r10, err);
223 __get_user_error(regs->ARM_fp, &sc->arm_fp, err);
224 __get_user_error(regs->ARM_ip, &sc->arm_ip, err);
225 __get_user_error(regs->ARM_sp, &sc->arm_sp, err);
226 __get_user_error(regs->ARM_lr, &sc->arm_lr, err);
227 __get_user_error(regs->ARM_pc, &sc->arm_pc, err);
228 __get_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
230 err |= !valid_user_regs(regs);
232 #ifdef CONFIG_IWMMXT
233 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
234 err |= restore_iwmmxt_context(&aux->iwmmxt);
235 #endif
236 #ifdef CONFIG_VFP
237 // if (err == 0)
238 // err |= vfp_restore_state(&aux->vfp);
239 #endif
241 return err;
244 asmlinkage int sys_sigreturn(struct pt_regs *regs)
246 struct sigframe __user *frame;
247 sigset_t set;
249 /* Always make any pending restarted system calls return -EINTR */
250 current_thread_info()->restart_block.fn = do_no_restart_syscall;
253 * Since we stacked the signal on a 64-bit boundary,
254 * then 'sp' should be word aligned here. If it's
255 * not, then the user is trying to mess with us.
257 if (regs->ARM_sp & 7)
258 goto badframe;
260 frame = (struct sigframe __user *)regs->ARM_sp;
262 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
263 goto badframe;
264 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
265 goto badframe;
267 sigdelsetmask(&set, ~_BLOCKABLE);
268 spin_lock_irq(&current->sighand->siglock);
269 current->blocked = set;
270 recalc_sigpending();
271 spin_unlock_irq(&current->sighand->siglock);
273 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &frame->aux))
274 goto badframe;
276 /* Send SIGTRAP if we're single-stepping */
277 if (current->ptrace & PT_SINGLESTEP) {
278 ptrace_cancel_bpt(current);
279 send_sig(SIGTRAP, current, 1);
282 return regs->ARM_r0;
284 badframe:
285 force_sig(SIGSEGV, current);
286 return 0;
289 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
291 struct rt_sigframe __user *frame;
292 sigset_t set;
294 /* Always make any pending restarted system calls return -EINTR */
295 current_thread_info()->restart_block.fn = do_no_restart_syscall;
298 * Since we stacked the signal on a 64-bit boundary,
299 * then 'sp' should be word aligned here. If it's
300 * not, then the user is trying to mess with us.
302 if (regs->ARM_sp & 7)
303 goto badframe;
305 frame = (struct rt_sigframe __user *)regs->ARM_sp;
307 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
308 goto badframe;
309 if (__copy_from_user(&set, &frame->sig.uc.uc_sigmask, sizeof(set)))
310 goto badframe;
312 sigdelsetmask(&set, ~_BLOCKABLE);
313 spin_lock_irq(&current->sighand->siglock);
314 current->blocked = set;
315 recalc_sigpending();
316 spin_unlock_irq(&current->sighand->siglock);
318 if (restore_sigcontext(regs, &frame->sig.uc.uc_mcontext, &frame->sig.aux))
319 goto badframe;
321 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
322 goto badframe;
324 /* Send SIGTRAP if we're single-stepping */
325 if (current->ptrace & PT_SINGLESTEP) {
326 ptrace_cancel_bpt(current);
327 send_sig(SIGTRAP, current, 1);
330 return regs->ARM_r0;
332 badframe:
333 force_sig(SIGSEGV, current);
334 return 0;
337 static int
338 setup_sigcontext(struct sigcontext __user *sc, struct aux_sigframe __user *aux,
339 struct pt_regs *regs, unsigned long mask)
341 int err = 0;
343 __put_user_error(regs->ARM_r0, &sc->arm_r0, err);
344 __put_user_error(regs->ARM_r1, &sc->arm_r1, err);
345 __put_user_error(regs->ARM_r2, &sc->arm_r2, err);
346 __put_user_error(regs->ARM_r3, &sc->arm_r3, err);
347 __put_user_error(regs->ARM_r4, &sc->arm_r4, err);
348 __put_user_error(regs->ARM_r5, &sc->arm_r5, err);
349 __put_user_error(regs->ARM_r6, &sc->arm_r6, err);
350 __put_user_error(regs->ARM_r7, &sc->arm_r7, err);
351 __put_user_error(regs->ARM_r8, &sc->arm_r8, err);
352 __put_user_error(regs->ARM_r9, &sc->arm_r9, err);
353 __put_user_error(regs->ARM_r10, &sc->arm_r10, err);
354 __put_user_error(regs->ARM_fp, &sc->arm_fp, err);
355 __put_user_error(regs->ARM_ip, &sc->arm_ip, err);
356 __put_user_error(regs->ARM_sp, &sc->arm_sp, err);
357 __put_user_error(regs->ARM_lr, &sc->arm_lr, err);
358 __put_user_error(regs->ARM_pc, &sc->arm_pc, err);
359 __put_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
361 __put_user_error(current->thread.trap_no, &sc->trap_no, err);
362 __put_user_error(current->thread.error_code, &sc->error_code, err);
363 __put_user_error(current->thread.address, &sc->fault_address, err);
364 __put_user_error(mask, &sc->oldmask, err);
366 #ifdef CONFIG_IWMMXT
367 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
368 err |= preserve_iwmmxt_context(&aux->iwmmxt);
369 #endif
370 #ifdef CONFIG_VFP
371 // if (err == 0)
372 // err |= vfp_save_state(&aux->vfp);
373 #endif
375 return err;
378 static inline void __user *
379 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
381 unsigned long sp = regs->ARM_sp;
382 void __user *frame;
385 * This is the X/Open sanctioned signal stack switching.
387 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
388 sp = current->sas_ss_sp + current->sas_ss_size;
391 * ATPCS B01 mandates 8-byte alignment
393 frame = (void __user *)((sp - framesize) & ~7);
396 * Check that we can actually write to the signal frame.
398 if (!access_ok(VERIFY_WRITE, frame, framesize))
399 frame = NULL;
401 return frame;
404 static int
405 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
406 unsigned long __user *rc, void __user *frame, int usig)
408 unsigned long handler = (unsigned long)ka->sa.sa_handler;
409 unsigned long retcode;
410 int thumb = 0;
411 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
414 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
416 if (ka->sa.sa_flags & SA_THIRTYTWO)
417 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
419 #ifdef CONFIG_ARM_THUMB
420 if (elf_hwcap & HWCAP_THUMB) {
422 * The LSB of the handler determines if we're going to
423 * be using THUMB or ARM mode for this signal handler.
425 thumb = handler & 1;
427 if (thumb)
428 cpsr |= PSR_T_BIT;
429 else
430 cpsr &= ~PSR_T_BIT;
432 #endif
434 if (ka->sa.sa_flags & SA_RESTORER) {
435 retcode = (unsigned long)ka->sa.sa_restorer;
436 } else {
437 unsigned int idx = thumb << 1;
439 if (ka->sa.sa_flags & SA_SIGINFO)
440 idx += 3;
442 if (__put_user(sigreturn_codes[idx], rc) ||
443 __put_user(sigreturn_codes[idx+1], rc+1))
444 return 1;
446 if (cpsr & MODE32_BIT) {
448 * 32-bit code can use the new high-page
449 * signal return code support.
451 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
452 } else {
454 * Ensure that the instruction cache sees
455 * the return code written onto the stack.
457 flush_icache_range((unsigned long)rc,
458 (unsigned long)(rc + 2));
460 retcode = ((unsigned long)rc) + thumb;
464 regs->ARM_r0 = usig;
465 regs->ARM_sp = (unsigned long)frame;
466 regs->ARM_lr = retcode;
467 regs->ARM_pc = handler;
468 regs->ARM_cpsr = cpsr;
470 return 0;
473 static int
474 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
476 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
477 int err = 0;
479 if (!frame)
480 return 1;
482 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->aux, regs, set->sig[0]);
483 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
485 if (err == 0)
486 err = setup_return(regs, ka, frame->retcode, frame, usig);
488 return err;
491 static int
492 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
493 sigset_t *set, struct pt_regs *regs)
495 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
496 stack_t stack;
497 int err = 0;
499 if (!frame)
500 return 1;
502 err |= copy_siginfo_to_user(&frame->info, info);
504 __put_user_error(0, &frame->sig.uc.uc_flags, err);
505 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
507 memset(&stack, 0, sizeof(stack));
508 stack.ss_sp = (void __user *)current->sas_ss_sp;
509 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
510 stack.ss_size = current->sas_ss_size;
511 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
513 err |= setup_sigcontext(&frame->sig.uc.uc_mcontext, &frame->sig.aux,
514 regs, set->sig[0]);
515 err |= __copy_to_user(&frame->sig.uc.uc_sigmask, set, sizeof(*set));
517 if (err == 0)
518 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
520 if (err == 0) {
522 * For realtime signals we must also set the second and third
523 * arguments for the signal handler.
524 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
526 regs->ARM_r1 = (unsigned long)&frame->info;
527 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
530 return err;
533 static inline void restart_syscall(struct pt_regs *regs)
535 regs->ARM_r0 = regs->ARM_ORIG_r0;
536 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
540 * OK, we're invoking a handler
542 static void
543 handle_signal(unsigned long sig, struct k_sigaction *ka,
544 siginfo_t *info, sigset_t *oldset,
545 struct pt_regs * regs, int syscall)
547 struct thread_info *thread = current_thread_info();
548 struct task_struct *tsk = current;
549 int usig = sig;
550 int ret;
553 * If we were from a system call, check for system call restarting...
555 if (syscall) {
556 switch (regs->ARM_r0) {
557 case -ERESTART_RESTARTBLOCK:
558 case -ERESTARTNOHAND:
559 regs->ARM_r0 = -EINTR;
560 break;
561 case -ERESTARTSYS:
562 if (!(ka->sa.sa_flags & SA_RESTART)) {
563 regs->ARM_r0 = -EINTR;
564 break;
566 /* fallthrough */
567 case -ERESTARTNOINTR:
568 restart_syscall(regs);
573 * translate the signal
575 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
576 usig = thread->exec_domain->signal_invmap[usig];
579 * Set up the stack frame
581 if (ka->sa.sa_flags & SA_SIGINFO)
582 ret = setup_rt_frame(usig, ka, info, oldset, regs);
583 else
584 ret = setup_frame(usig, ka, oldset, regs);
587 * Check that the resulting registers are actually sane.
589 ret |= !valid_user_regs(regs);
591 if (ret != 0) {
592 force_sigsegv(sig, tsk);
593 return;
597 * Block the signal if we were successful.
599 spin_lock_irq(&tsk->sighand->siglock);
600 sigorsets(&tsk->blocked, &tsk->blocked,
601 &ka->sa.sa_mask);
602 if (!(ka->sa.sa_flags & SA_NODEFER))
603 sigaddset(&tsk->blocked, sig);
604 recalc_sigpending();
605 spin_unlock_irq(&tsk->sighand->siglock);
610 * Note that 'init' is a special process: it doesn't get signals it doesn't
611 * want to handle. Thus you cannot kill init even with a SIGKILL even by
612 * mistake.
614 * Note that we go through the signals twice: once to check the signals that
615 * the kernel can handle, and then we build all the user-level signal handling
616 * stack-frames in one go after that.
618 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
620 struct k_sigaction ka;
621 siginfo_t info;
622 int signr;
625 * We want the common case to go fast, which
626 * is why we may in certain cases get here from
627 * kernel mode. Just return without doing anything
628 * if so.
630 if (!user_mode(regs))
631 return 0;
633 if (try_to_freeze())
634 goto no_signal;
636 if (current->ptrace & PT_SINGLESTEP)
637 ptrace_cancel_bpt(current);
639 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
640 if (signr > 0) {
641 handle_signal(signr, &ka, &info, oldset, regs, syscall);
642 if (current->ptrace & PT_SINGLESTEP)
643 ptrace_set_bpt(current);
644 return 1;
647 no_signal:
649 * No signal to deliver to the process - restart the syscall.
651 if (syscall) {
652 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
653 if (thumb_mode(regs)) {
654 regs->ARM_r7 = __NR_restart_syscall;
655 regs->ARM_pc -= 2;
656 } else {
657 u32 __user *usp;
659 regs->ARM_sp -= 12;
660 usp = (u32 __user *)regs->ARM_sp;
662 put_user(regs->ARM_pc, &usp[0]);
663 /* swi __NR_restart_syscall */
664 put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
665 /* ldr pc, [sp], #12 */
666 put_user(0xe49df00c, &usp[2]);
668 flush_icache_range((unsigned long)usp,
669 (unsigned long)(usp + 3));
671 regs->ARM_pc = regs->ARM_sp + 4;
674 if (regs->ARM_r0 == -ERESTARTNOHAND ||
675 regs->ARM_r0 == -ERESTARTSYS ||
676 regs->ARM_r0 == -ERESTARTNOINTR) {
677 restart_syscall(regs);
680 if (current->ptrace & PT_SINGLESTEP)
681 ptrace_set_bpt(current);
682 return 0;
685 asmlinkage void
686 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
688 if (thread_flags & _TIF_SIGPENDING)
689 do_signal(&current->blocked, regs, syscall);