2 * linux/arch/m68knommu/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
12 * Linux/m68k support by Hamish Macdonald
14 * 68060 fixes by Jesper Skov
16 * 1997-12-01 Modified for POSIX.1b signals by Andreas Schwab
18 * mathemu support by Roman Zippel
19 * (Note: fpstate in the signal context is completely ignored for the emulator
20 * and the internal floating point format is put on stack)
24 * ++roman (07/09/96): implemented signal stacks (specially for tosemu on
25 * Atari :-) Current limitation: Only one sigstack can be active at one time.
26 * If a second signal with SA_ONSTACK set arrives while working on a sigstack,
27 * SA_ONSTACK is ignored. This behaviour avoids lots of trouble with nested
31 #include <linux/sched.h>
33 #include <linux/kernel.h>
34 #include <linux/signal.h>
35 #include <linux/syscalls.h>
36 #include <linux/errno.h>
37 #include <linux/wait.h>
38 #include <linux/ptrace.h>
39 #include <linux/unistd.h>
40 #include <linux/stddef.h>
41 #include <linux/highuid.h>
42 #include <linux/tty.h>
43 #include <linux/personality.h>
44 #include <linux/binfmts.h>
46 #include <asm/setup.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
49 #include <asm/traps.h>
50 #include <asm/ucontext.h>
52 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
54 asmlinkage int do_signal(sigset_t *oldset, struct pt_regs *regs);
57 * Atomically swap in the new signal mask, and wait for a signal.
59 asmlinkage int do_sigsuspend(struct pt_regs *regs)
61 old_sigset_t mask = regs->d3;
65 spin_lock_irq(¤t->sighand->siglock);
66 saveset = current->blocked;
67 siginitset(¤t->blocked, mask);
69 spin_unlock_irq(¤t->sighand->siglock);
73 current->state = TASK_INTERRUPTIBLE;
75 if (do_signal(&saveset, regs))
81 do_rt_sigsuspend(struct pt_regs *regs)
83 sigset_t *unewset = (sigset_t *)regs->d1;
84 size_t sigsetsize = (size_t)regs->d2;
85 sigset_t saveset, newset;
87 /* XXX: Don't preclude handling different sized sigset_t's. */
88 if (sigsetsize != sizeof(sigset_t))
91 if (copy_from_user(&newset, unewset, sizeof(newset)))
93 sigdelsetmask(&newset, ~_BLOCKABLE);
95 spin_lock_irq(¤t->sighand->siglock);
96 saveset = current->blocked;
97 current->blocked = newset;
99 spin_unlock_irq(¤t->sighand->siglock);
103 current->state = TASK_INTERRUPTIBLE;
105 if (do_signal(&saveset, regs))
111 sys_sigaction(int sig, const struct old_sigaction *act,
112 struct old_sigaction *oact)
114 struct k_sigaction new_ka, old_ka;
119 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
120 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
121 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
123 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
124 __get_user(mask, &act->sa_mask);
125 siginitset(&new_ka.sa.sa_mask, mask);
128 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
131 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
132 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
133 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
135 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
136 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
143 sys_sigaltstack(const stack_t *uss, stack_t *uoss)
145 return do_sigaltstack(uss, uoss, rdusp());
150 * Do a signal return; undo the signal stack.
152 * Keep the return code on the stack quadword aligned!
153 * That makes the cache flush below easier.
161 struct sigcontext *psc;
163 unsigned long extramask[_NSIG_WORDS-1];
164 struct sigcontext sc;
171 struct siginfo *pinfo;
180 static unsigned char fpu_version = 0; /* version number of fpu, set by setup_frame */
182 static inline int restore_fpu_state(struct sigcontext *sc)
187 /* restore registers */
188 memcpy(current->thread.fpcntl, sc->sc_fpcntl, 12);
189 memcpy(current->thread.fp, sc->sc_fpregs, 24);
193 if (sc->sc_fpstate[0]) {
194 /* Verify the frame format. */
195 if (sc->sc_fpstate[0] != fpu_version)
198 __asm__ volatile (".chip 68k/68881\n\t"
199 "fmovemx %0,%/fp0-%/fp1\n\t"
200 "fmoveml %1,%/fpcr/%/fpsr/%/fpiar\n\t"
203 : "m" (*sc->sc_fpregs), "m" (*sc->sc_fpcntl));
205 __asm__ volatile (".chip 68k/68881\n\t"
207 ".chip 68k" : : "m" (*sc->sc_fpstate));
214 #define FPCONTEXT_SIZE 216
215 #define uc_fpstate uc_filler[0]
216 #define uc_formatvec uc_filler[FPCONTEXT_SIZE/4]
217 #define uc_extra uc_filler[FPCONTEXT_SIZE/4+1]
219 static inline int rt_restore_fpu_state(struct ucontext *uc)
221 unsigned char fpstate[FPCONTEXT_SIZE];
222 int context_size = 0;
227 /* restore fpu control register */
228 if (__copy_from_user(current->thread.fpcntl,
229 &uc->uc_mcontext.fpregs.f_pcr, 12))
231 /* restore all other fpu register */
232 if (__copy_from_user(current->thread.fp,
233 uc->uc_mcontext.fpregs.f_fpregs, 96))
238 if (__get_user(*(long *)fpstate, (long *)&uc->uc_fpstate))
241 context_size = fpstate[1];
243 /* Verify the frame format. */
244 if (fpstate[0] != fpu_version)
246 if (__copy_from_user(&fpregs, &uc->uc_mcontext.fpregs,
249 __asm__ volatile (".chip 68k/68881\n\t"
250 "fmovemx %0,%/fp0-%/fp7\n\t"
251 "fmoveml %1,%/fpcr/%/fpsr/%/fpiar\n\t"
254 : "m" (*fpregs.f_fpregs),
258 __copy_from_user(fpstate + 4, (long *)&uc->uc_fpstate + 1,
261 __asm__ volatile (".chip 68k/68881\n\t"
263 ".chip 68k" : : "m" (*fpstate));
273 restore_sigcontext(struct pt_regs *regs, struct sigcontext *usc, void *fp,
277 struct sigcontext context;
280 /* get previous context */
281 if (copy_from_user(&context, usc, sizeof(context)))
284 /* restore passed registers */
285 regs->d1 = context.sc_d1;
286 regs->a0 = context.sc_a0;
287 regs->a1 = context.sc_a1;
288 ((struct switch_stack *)regs - 1)->a5 = context.sc_a5;
289 regs->sr = (regs->sr & 0xff00) | (context.sc_sr & 0xff);
290 regs->pc = context.sc_pc;
291 regs->orig_d0 = -1; /* disable syscall checks */
292 wrusp(context.sc_usp);
293 formatvec = context.sc_formatvec;
294 regs->format = formatvec >> 12;
295 regs->vector = formatvec & 0xfff;
298 err = restore_fpu_state(&context);
301 *pd0 = context.sc_d0;
309 rt_restore_ucontext(struct pt_regs *regs, struct switch_stack *sw,
310 struct ucontext *uc, int *pd0)
313 greg_t *gregs = uc->uc_mcontext.gregs;
317 err = __get_user(temp, &uc->uc_mcontext.version);
318 if (temp != MCONTEXT_VERSION)
320 /* restore passed registers */
321 err |= __get_user(regs->d0, &gregs[0]);
322 err |= __get_user(regs->d1, &gregs[1]);
323 err |= __get_user(regs->d2, &gregs[2]);
324 err |= __get_user(regs->d3, &gregs[3]);
325 err |= __get_user(regs->d4, &gregs[4]);
326 err |= __get_user(regs->d5, &gregs[5]);
327 err |= __get_user(sw->d6, &gregs[6]);
328 err |= __get_user(sw->d7, &gregs[7]);
329 err |= __get_user(regs->a0, &gregs[8]);
330 err |= __get_user(regs->a1, &gregs[9]);
331 err |= __get_user(regs->a2, &gregs[10]);
332 err |= __get_user(sw->a3, &gregs[11]);
333 err |= __get_user(sw->a4, &gregs[12]);
334 err |= __get_user(sw->a5, &gregs[13]);
335 err |= __get_user(sw->a6, &gregs[14]);
336 err |= __get_user(usp, &gregs[15]);
338 err |= __get_user(regs->pc, &gregs[16]);
339 err |= __get_user(temp, &gregs[17]);
340 regs->sr = (regs->sr & 0xff00) | (temp & 0xff);
341 regs->orig_d0 = -1; /* disable syscall checks */
342 regs->format = temp >> 12;
343 regs->vector = temp & 0xfff;
345 if (do_sigaltstack(&uc->uc_stack, NULL, usp) == -EFAULT)
355 asmlinkage int do_sigreturn(unsigned long __unused)
357 struct switch_stack *sw = (struct switch_stack *) &__unused;
358 struct pt_regs *regs = (struct pt_regs *) (sw + 1);
359 unsigned long usp = rdusp();
360 struct sigframe *frame = (struct sigframe *)(usp - 4);
364 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
366 if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
368 __copy_from_user(&set.sig[1], &frame->extramask,
369 sizeof(frame->extramask))))
372 sigdelsetmask(&set, ~_BLOCKABLE);
373 spin_lock_irq(¤t->sighand->siglock);
374 current->blocked = set;
376 spin_unlock_irq(¤t->sighand->siglock);
378 if (restore_sigcontext(regs, &frame->sc, frame + 1, &d0))
383 force_sig(SIGSEGV, current);
387 asmlinkage int do_rt_sigreturn(unsigned long __unused)
389 struct switch_stack *sw = (struct switch_stack *) &__unused;
390 struct pt_regs *regs = (struct pt_regs *) (sw + 1);
391 unsigned long usp = rdusp();
392 struct rt_sigframe *frame = (struct rt_sigframe *)(usp - 4);
396 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
398 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
401 sigdelsetmask(&set, ~_BLOCKABLE);
402 spin_lock_irq(¤t->sighand->siglock);
403 current->blocked = set;
405 spin_unlock_irq(¤t->sighand->siglock);
407 if (rt_restore_ucontext(regs, sw, &frame->uc, &d0))
412 force_sig(SIGSEGV, current);
418 * Set up a signal frame.
421 static inline void save_fpu_state(struct sigcontext *sc, struct pt_regs *regs)
425 memcpy(sc->sc_fpcntl, current->thread.fpcntl, 12);
426 memcpy(sc->sc_fpregs, current->thread.fp, 24);
430 __asm__ volatile (".chip 68k/68881\n\t"
433 : : "m" (*sc->sc_fpstate) : "memory");
435 if (sc->sc_fpstate[0]) {
436 fpu_version = sc->sc_fpstate[0];
437 __asm__ volatile (".chip 68k/68881\n\t"
438 "fmovemx %/fp0-%/fp1,%0\n\t"
439 "fmoveml %/fpcr/%/fpsr/%/fpiar,%1\n\t"
442 : "m" (*sc->sc_fpregs),
448 static inline int rt_save_fpu_state(struct ucontext *uc, struct pt_regs *regs)
450 unsigned char fpstate[FPCONTEXT_SIZE];
451 int context_size = 0;
455 /* save fpu control register */
456 err |= copy_to_user(&uc->uc_mcontext.fpregs.f_pcr,
457 current->thread.fpcntl, 12);
458 /* save all other fpu register */
459 err |= copy_to_user(uc->uc_mcontext.fpregs.f_fpregs,
460 current->thread.fp, 96);
464 __asm__ volatile (".chip 68k/68881\n\t"
467 : : "m" (*fpstate) : "memory");
469 err |= __put_user(*(long *)fpstate, (long *)&uc->uc_fpstate);
472 context_size = fpstate[1];
473 fpu_version = fpstate[0];
474 __asm__ volatile (".chip 68k/68881\n\t"
475 "fmovemx %/fp0-%/fp7,%0\n\t"
476 "fmoveml %/fpcr/%/fpsr/%/fpiar,%1\n\t"
479 : "m" (*fpregs.f_fpregs),
482 err |= copy_to_user(&uc->uc_mcontext.fpregs, &fpregs,
486 err |= copy_to_user((long *)&uc->uc_fpstate + 1, fpstate + 4,
493 static void setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
497 sc->sc_usp = rdusp();
498 sc->sc_d0 = regs->d0;
499 sc->sc_d1 = regs->d1;
500 sc->sc_a0 = regs->a0;
501 sc->sc_a1 = regs->a1;
502 sc->sc_a5 = ((struct switch_stack *)regs - 1)->a5;
503 sc->sc_sr = regs->sr;
504 sc->sc_pc = regs->pc;
505 sc->sc_formatvec = regs->format << 12 | regs->vector;
507 save_fpu_state(sc, regs);
511 static inline int rt_setup_ucontext(struct ucontext *uc, struct pt_regs *regs)
513 struct switch_stack *sw = (struct switch_stack *)regs - 1;
514 greg_t *gregs = uc->uc_mcontext.gregs;
517 err |= __put_user(MCONTEXT_VERSION, &uc->uc_mcontext.version);
518 err |= __put_user(regs->d0, &gregs[0]);
519 err |= __put_user(regs->d1, &gregs[1]);
520 err |= __put_user(regs->d2, &gregs[2]);
521 err |= __put_user(regs->d3, &gregs[3]);
522 err |= __put_user(regs->d4, &gregs[4]);
523 err |= __put_user(regs->d5, &gregs[5]);
524 err |= __put_user(sw->d6, &gregs[6]);
525 err |= __put_user(sw->d7, &gregs[7]);
526 err |= __put_user(regs->a0, &gregs[8]);
527 err |= __put_user(regs->a1, &gregs[9]);
528 err |= __put_user(regs->a2, &gregs[10]);
529 err |= __put_user(sw->a3, &gregs[11]);
530 err |= __put_user(sw->a4, &gregs[12]);
531 err |= __put_user(sw->a5, &gregs[13]);
532 err |= __put_user(sw->a6, &gregs[14]);
533 err |= __put_user(rdusp(), &gregs[15]);
534 err |= __put_user(regs->pc, &gregs[16]);
535 err |= __put_user(regs->sr, &gregs[17]);
537 err |= rt_save_fpu_state(uc, regs);
542 static inline void push_cache (unsigned long vaddr)
547 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
551 /* Default to using normal stack. */
554 /* This is the X/Open sanctioned signal stack switching. */
555 if (ka->sa.sa_flags & SA_ONSTACK) {
556 if (!sas_ss_flags(usp))
557 usp = current->sas_ss_sp + current->sas_ss_size;
559 return (void *)((usp - frame_size) & -8UL);
562 static void setup_frame (int sig, struct k_sigaction *ka,
563 sigset_t *set, struct pt_regs *regs)
565 struct sigframe *frame;
566 struct sigcontext context;
569 frame = get_sigframe(ka, regs, sizeof(*frame));
571 err |= __put_user((current_thread_info()->exec_domain
572 && current_thread_info()->exec_domain->signal_invmap
574 ? current_thread_info()->exec_domain->signal_invmap[sig]
578 err |= __put_user(regs->vector, &frame->code);
579 err |= __put_user(&frame->sc, &frame->psc);
582 err |= copy_to_user(frame->extramask, &set->sig[1],
583 sizeof(frame->extramask));
585 setup_sigcontext(&context, regs, set->sig[0]);
586 err |= copy_to_user (&frame->sc, &context, sizeof(context));
588 /* Set up to return from userspace. */
589 err |= __put_user(frame->retcode, &frame->pretcode);
590 /* moveq #,d0; trap #0 */
591 err |= __put_user(0x70004e40 + (__NR_sigreturn << 16),
592 (long *)(frame->retcode));
597 push_cache ((unsigned long) &frame->retcode);
599 /* Set up registers for signal handler */
600 wrusp ((unsigned long) frame);
601 regs->pc = (unsigned long) ka->sa.sa_handler;
602 ((struct switch_stack *)regs - 1)->a5 = current->mm->start_data;
603 regs->format = 0x4; /*set format byte to make stack appear modulo 4
604 which it will be when doing the rte */
607 /* Prepare to skip over the extra stuff in the exception frame. */
609 struct pt_regs *tregs =
610 (struct pt_regs *)((ulong)regs + regs->stkadj);
612 printk(KERN_DEBUG "Performing stackadjust=%04x\n", regs->stkadj);
614 /* This must be copied with decreasing addresses to
618 tregs->pc = regs->pc;
619 tregs->sr = regs->sr;
624 force_sigsegv(sig, current);
628 static void setup_rt_frame (int sig, struct k_sigaction *ka, siginfo_t *info,
629 sigset_t *set, struct pt_regs *regs)
631 struct rt_sigframe *frame;
634 frame = get_sigframe(ka, regs, sizeof(*frame));
636 err |= __put_user((current_thread_info()->exec_domain
637 && current_thread_info()->exec_domain->signal_invmap
639 ? current_thread_info()->exec_domain->signal_invmap[sig]
642 err |= __put_user(&frame->info, &frame->pinfo);
643 err |= __put_user(&frame->uc, &frame->puc);
644 err |= copy_siginfo_to_user(&frame->info, info);
646 /* Create the ucontext. */
647 err |= __put_user(0, &frame->uc.uc_flags);
648 err |= __put_user(0, &frame->uc.uc_link);
649 err |= __put_user((void *)current->sas_ss_sp,
650 &frame->uc.uc_stack.ss_sp);
651 err |= __put_user(sas_ss_flags(rdusp()),
652 &frame->uc.uc_stack.ss_flags);
653 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
654 err |= rt_setup_ucontext(&frame->uc, regs);
655 err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
657 /* Set up to return from userspace. */
658 err |= __put_user(frame->retcode, &frame->pretcode);
659 /* moveq #,d0; notb d0; trap #0 */
660 err |= __put_user(0x70004600 + ((__NR_rt_sigreturn ^ 0xff) << 16),
661 (long *)(frame->retcode + 0));
662 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
667 push_cache ((unsigned long) &frame->retcode);
669 /* Set up registers for signal handler */
670 wrusp ((unsigned long) frame);
671 regs->pc = (unsigned long) ka->sa.sa_handler;
672 ((struct switch_stack *)regs - 1)->a5 = current->mm->start_data;
673 regs->format = 0x4; /*set format byte to make stack appear modulo 4
674 which it will be when doing the rte */
677 /* Prepare to skip over the extra stuff in the exception frame. */
679 struct pt_regs *tregs =
680 (struct pt_regs *)((ulong)regs + regs->stkadj);
682 printk(KERN_DEBUG "Performing stackadjust=%04x\n", regs->stkadj);
684 /* This must be copied with decreasing addresses to
688 tregs->pc = regs->pc;
689 tregs->sr = regs->sr;
694 force_sigsegv(sig, current);
699 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
702 case -ERESTARTNOHAND:
709 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
714 case -ERESTARTNOINTR:
716 regs->d0 = regs->orig_d0;
723 * OK, we're invoking a handler
726 handle_signal(int sig, struct k_sigaction *ka, siginfo_t *info,
727 sigset_t *oldset, struct pt_regs *regs)
729 /* are we from a system call? */
730 if (regs->orig_d0 >= 0)
731 /* If so, check system call restarting.. */
732 handle_restart(regs, ka, 1);
734 /* set up the stack frame */
735 if (ka->sa.sa_flags & SA_SIGINFO)
736 setup_rt_frame(sig, ka, info, oldset, regs);
738 setup_frame(sig, ka, oldset, regs);
740 if (ka->sa.sa_flags & SA_ONESHOT)
741 ka->sa.sa_handler = SIG_DFL;
743 spin_lock_irq(¤t->sighand->siglock);
744 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
745 if (!(ka->sa.sa_flags & SA_NODEFER))
746 sigaddset(¤t->blocked,sig);
748 spin_unlock_irq(¤t->sighand->siglock);
752 * Note that 'init' is a special process: it doesn't get signals it doesn't
753 * want to handle. Thus you cannot kill init even with a SIGKILL even by
756 asmlinkage int do_signal(sigset_t *oldset, struct pt_regs *regs)
758 struct k_sigaction ka;
763 * We want the common case to go fast, which
764 * is why we may in certain cases get here from
765 * kernel mode. Just return without doing anything
768 if (!user_mode(regs))
772 oldset = ¤t->blocked;
774 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
776 /* Whee! Actually deliver the signal. */
777 handle_signal(signr, &ka, &info, oldset, regs);
781 /* Did we come from a system call? */
782 if (regs->orig_d0 >= 0) {
783 /* Restart the system call - no handlers present */
784 handle_restart(regs, NULL, 0);