m68k: use set_current_blocked() and block_sigmask()
[linux-2.6/libata-dev.git] / arch / m68k / kernel / signal.c
blobd8f6960e8fd9ecb98eedfe64a549665302fa4924
1 /*
2 * linux/arch/m68k/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
8 * for more details.
9 */
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
28 * signal handlers!
31 #include <linux/sched.h>
32 #include <linux/mm.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/personality.h>
43 #include <linux/tty.h>
44 #include <linux/binfmts.h>
45 #include <linux/module.h>
47 #include <asm/setup.h>
48 #include <asm/uaccess.h>
49 #include <asm/pgtable.h>
50 #include <asm/traps.h>
51 #include <asm/ucontext.h>
53 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
55 #ifdef CONFIG_MMU
58 * Handle the slight differences in classic 68k and ColdFire trap frames.
60 #ifdef CONFIG_COLDFIRE
61 #define FORMAT 4
62 #define FMT4SIZE 0
63 #else
64 #define FORMAT 0
65 #define FMT4SIZE sizeof(((struct frame *)0)->un.fmt4)
66 #endif
68 static const int frame_size_change[16] = {
69 [1] = -1, /* sizeof(((struct frame *)0)->un.fmt1), */
70 [2] = sizeof(((struct frame *)0)->un.fmt2),
71 [3] = sizeof(((struct frame *)0)->un.fmt3),
72 [4] = FMT4SIZE,
73 [5] = -1, /* sizeof(((struct frame *)0)->un.fmt5), */
74 [6] = -1, /* sizeof(((struct frame *)0)->un.fmt6), */
75 [7] = sizeof(((struct frame *)0)->un.fmt7),
76 [8] = -1, /* sizeof(((struct frame *)0)->un.fmt8), */
77 [9] = sizeof(((struct frame *)0)->un.fmt9),
78 [10] = sizeof(((struct frame *)0)->un.fmta),
79 [11] = sizeof(((struct frame *)0)->un.fmtb),
80 [12] = -1, /* sizeof(((struct frame *)0)->un.fmtc), */
81 [13] = -1, /* sizeof(((struct frame *)0)->un.fmtd), */
82 [14] = -1, /* sizeof(((struct frame *)0)->un.fmte), */
83 [15] = -1, /* sizeof(((struct frame *)0)->un.fmtf), */
86 static inline int frame_extra_sizes(int f)
88 return frame_size_change[f];
91 int handle_kernel_fault(struct pt_regs *regs)
93 const struct exception_table_entry *fixup;
94 struct pt_regs *tregs;
96 /* Are we prepared to handle this kernel fault? */
97 fixup = search_exception_tables(regs->pc);
98 if (!fixup)
99 return 0;
101 /* Create a new four word stack frame, discarding the old one. */
102 regs->stkadj = frame_extra_sizes(regs->format);
103 tregs = (struct pt_regs *)((long)regs + regs->stkadj);
104 tregs->vector = regs->vector;
105 tregs->format = FORMAT;
106 tregs->pc = fixup->fixup;
107 tregs->sr = regs->sr;
109 return 1;
112 void ptrace_signal_deliver(struct pt_regs *regs, void *cookie)
114 if (regs->orig_d0 < 0)
115 return;
116 switch (regs->d0) {
117 case -ERESTARTNOHAND:
118 case -ERESTARTSYS:
119 case -ERESTARTNOINTR:
120 regs->d0 = regs->orig_d0;
121 regs->orig_d0 = -1;
122 regs->pc -= 2;
123 break;
127 static inline void push_cache (unsigned long vaddr)
130 * Using the old cache_push_v() was really a big waste.
132 * What we are trying to do is to flush 8 bytes to ram.
133 * Flushing 2 cache lines of 16 bytes is much cheaper than
134 * flushing 1 or 2 pages, as previously done in
135 * cache_push_v().
136 * Jes
138 if (CPU_IS_040) {
139 unsigned long temp;
141 __asm__ __volatile__ (".chip 68040\n\t"
142 "nop\n\t"
143 "ptestr (%1)\n\t"
144 "movec %%mmusr,%0\n\t"
145 ".chip 68k"
146 : "=r" (temp)
147 : "a" (vaddr));
149 temp &= PAGE_MASK;
150 temp |= vaddr & ~PAGE_MASK;
152 __asm__ __volatile__ (".chip 68040\n\t"
153 "nop\n\t"
154 "cpushl %%bc,(%0)\n\t"
155 ".chip 68k"
156 : : "a" (temp));
158 else if (CPU_IS_060) {
159 unsigned long temp;
160 __asm__ __volatile__ (".chip 68060\n\t"
161 "plpar (%0)\n\t"
162 ".chip 68k"
163 : "=a" (temp)
164 : "0" (vaddr));
165 __asm__ __volatile__ (".chip 68060\n\t"
166 "cpushl %%bc,(%0)\n\t"
167 ".chip 68k"
168 : : "a" (temp));
169 } else if (!CPU_IS_COLDFIRE) {
171 * 68030/68020 have no writeback cache;
172 * still need to clear icache.
173 * Note that vaddr is guaranteed to be long word aligned.
175 unsigned long temp;
176 asm volatile ("movec %%cacr,%0" : "=r" (temp));
177 temp += 4;
178 asm volatile ("movec %0,%%caar\n\t"
179 "movec %1,%%cacr"
180 : : "r" (vaddr), "r" (temp));
181 asm volatile ("movec %0,%%caar\n\t"
182 "movec %1,%%cacr"
183 : : "r" (vaddr + 4), "r" (temp));
187 static inline void adjustformat(struct pt_regs *regs)
191 static inline void save_a5_state(struct sigcontext *sc, struct pt_regs *regs)
195 #else /* CONFIG_MMU */
197 void ret_from_user_signal(void);
198 void ret_from_user_rt_signal(void);
200 static inline int frame_extra_sizes(int f)
202 /* No frame size adjustments required on non-MMU CPUs */
203 return 0;
206 static inline void adjustformat(struct pt_regs *regs)
208 ((struct switch_stack *)regs - 1)->a5 = current->mm->start_data;
210 * set format byte to make stack appear modulo 4, which it will
211 * be when doing the rte
213 regs->format = 0x4;
216 static inline void save_a5_state(struct sigcontext *sc, struct pt_regs *regs)
218 sc->sc_a5 = ((struct switch_stack *)regs - 1)->a5;
221 static inline void push_cache(unsigned long vaddr)
225 #endif /* CONFIG_MMU */
228 * Atomically swap in the new signal mask, and wait for a signal.
230 asmlinkage int
231 sys_sigsuspend(int unused0, int unused1, old_sigset_t mask)
233 sigset_t blocked;
234 siginitset(&blocked, mask);
235 return sigsuspend(&blocked);
238 asmlinkage int
239 sys_sigaction(int sig, const struct old_sigaction __user *act,
240 struct old_sigaction __user *oact)
242 struct k_sigaction new_ka, old_ka;
243 int ret;
245 if (act) {
246 old_sigset_t mask;
247 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
248 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
249 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
250 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
251 __get_user(mask, &act->sa_mask))
252 return -EFAULT;
253 siginitset(&new_ka.sa.sa_mask, mask);
256 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
258 if (!ret && oact) {
259 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
260 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
261 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
262 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
263 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
264 return -EFAULT;
267 return ret;
270 asmlinkage int
271 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
273 return do_sigaltstack(uss, uoss, rdusp());
278 * Do a signal return; undo the signal stack.
280 * Keep the return code on the stack quadword aligned!
281 * That makes the cache flush below easier.
284 struct sigframe
286 char __user *pretcode;
287 int sig;
288 int code;
289 struct sigcontext __user *psc;
290 char retcode[8];
291 unsigned long extramask[_NSIG_WORDS-1];
292 struct sigcontext sc;
295 struct rt_sigframe
297 char __user *pretcode;
298 int sig;
299 struct siginfo __user *pinfo;
300 void __user *puc;
301 char retcode[8];
302 struct siginfo info;
303 struct ucontext uc;
306 #define FPCONTEXT_SIZE 216
307 #define uc_fpstate uc_filler[0]
308 #define uc_formatvec uc_filler[FPCONTEXT_SIZE/4]
309 #define uc_extra uc_filler[FPCONTEXT_SIZE/4+1]
311 #ifdef CONFIG_FPU
313 static unsigned char fpu_version; /* version number of fpu, set by setup_frame */
315 static inline int restore_fpu_state(struct sigcontext *sc)
317 int err = 1;
319 if (FPU_IS_EMU) {
320 /* restore registers */
321 memcpy(current->thread.fpcntl, sc->sc_fpcntl, 12);
322 memcpy(current->thread.fp, sc->sc_fpregs, 24);
323 return 0;
326 if (CPU_IS_060 ? sc->sc_fpstate[2] : sc->sc_fpstate[0]) {
327 /* Verify the frame format. */
328 if (!(CPU_IS_060 || CPU_IS_COLDFIRE) &&
329 (sc->sc_fpstate[0] != fpu_version))
330 goto out;
331 if (CPU_IS_020_OR_030) {
332 if (m68k_fputype & FPU_68881 &&
333 !(sc->sc_fpstate[1] == 0x18 || sc->sc_fpstate[1] == 0xb4))
334 goto out;
335 if (m68k_fputype & FPU_68882 &&
336 !(sc->sc_fpstate[1] == 0x38 || sc->sc_fpstate[1] == 0xd4))
337 goto out;
338 } else if (CPU_IS_040) {
339 if (!(sc->sc_fpstate[1] == 0x00 ||
340 sc->sc_fpstate[1] == 0x28 ||
341 sc->sc_fpstate[1] == 0x60))
342 goto out;
343 } else if (CPU_IS_060) {
344 if (!(sc->sc_fpstate[3] == 0x00 ||
345 sc->sc_fpstate[3] == 0x60 ||
346 sc->sc_fpstate[3] == 0xe0))
347 goto out;
348 } else if (CPU_IS_COLDFIRE) {
349 if (!(sc->sc_fpstate[0] == 0x00 ||
350 sc->sc_fpstate[0] == 0x05 ||
351 sc->sc_fpstate[0] == 0xe5))
352 goto out;
353 } else
354 goto out;
356 if (CPU_IS_COLDFIRE) {
357 __asm__ volatile ("fmovemd %0,%%fp0-%%fp1\n\t"
358 "fmovel %1,%%fpcr\n\t"
359 "fmovel %2,%%fpsr\n\t"
360 "fmovel %3,%%fpiar"
361 : /* no outputs */
362 : "m" (sc->sc_fpregs[0]),
363 "m" (sc->sc_fpcntl[0]),
364 "m" (sc->sc_fpcntl[1]),
365 "m" (sc->sc_fpcntl[2]));
366 } else {
367 __asm__ volatile (".chip 68k/68881\n\t"
368 "fmovemx %0,%%fp0-%%fp1\n\t"
369 "fmoveml %1,%%fpcr/%%fpsr/%%fpiar\n\t"
370 ".chip 68k"
371 : /* no outputs */
372 : "m" (*sc->sc_fpregs),
373 "m" (*sc->sc_fpcntl));
377 if (CPU_IS_COLDFIRE) {
378 __asm__ volatile ("frestore %0" : : "m" (*sc->sc_fpstate));
379 } else {
380 __asm__ volatile (".chip 68k/68881\n\t"
381 "frestore %0\n\t"
382 ".chip 68k"
383 : : "m" (*sc->sc_fpstate));
385 err = 0;
387 out:
388 return err;
391 static inline int rt_restore_fpu_state(struct ucontext __user *uc)
393 unsigned char fpstate[FPCONTEXT_SIZE];
394 int context_size = CPU_IS_060 ? 8 : (CPU_IS_COLDFIRE ? 12 : 0);
395 fpregset_t fpregs;
396 int err = 1;
398 if (FPU_IS_EMU) {
399 /* restore fpu control register */
400 if (__copy_from_user(current->thread.fpcntl,
401 uc->uc_mcontext.fpregs.f_fpcntl, 12))
402 goto out;
403 /* restore all other fpu register */
404 if (__copy_from_user(current->thread.fp,
405 uc->uc_mcontext.fpregs.f_fpregs, 96))
406 goto out;
407 return 0;
410 if (__get_user(*(long *)fpstate, (long __user *)&uc->uc_fpstate))
411 goto out;
412 if (CPU_IS_060 ? fpstate[2] : fpstate[0]) {
413 if (!(CPU_IS_060 || CPU_IS_COLDFIRE))
414 context_size = fpstate[1];
415 /* Verify the frame format. */
416 if (!(CPU_IS_060 || CPU_IS_COLDFIRE) &&
417 (fpstate[0] != fpu_version))
418 goto out;
419 if (CPU_IS_020_OR_030) {
420 if (m68k_fputype & FPU_68881 &&
421 !(context_size == 0x18 || context_size == 0xb4))
422 goto out;
423 if (m68k_fputype & FPU_68882 &&
424 !(context_size == 0x38 || context_size == 0xd4))
425 goto out;
426 } else if (CPU_IS_040) {
427 if (!(context_size == 0x00 ||
428 context_size == 0x28 ||
429 context_size == 0x60))
430 goto out;
431 } else if (CPU_IS_060) {
432 if (!(fpstate[3] == 0x00 ||
433 fpstate[3] == 0x60 ||
434 fpstate[3] == 0xe0))
435 goto out;
436 } else if (CPU_IS_COLDFIRE) {
437 if (!(fpstate[3] == 0x00 ||
438 fpstate[3] == 0x05 ||
439 fpstate[3] == 0xe5))
440 goto out;
441 } else
442 goto out;
443 if (__copy_from_user(&fpregs, &uc->uc_mcontext.fpregs,
444 sizeof(fpregs)))
445 goto out;
447 if (CPU_IS_COLDFIRE) {
448 __asm__ volatile ("fmovemd %0,%%fp0-%%fp7\n\t"
449 "fmovel %1,%%fpcr\n\t"
450 "fmovel %2,%%fpsr\n\t"
451 "fmovel %3,%%fpiar"
452 : /* no outputs */
453 : "m" (fpregs.f_fpregs[0]),
454 "m" (fpregs.f_fpcntl[0]),
455 "m" (fpregs.f_fpcntl[1]),
456 "m" (fpregs.f_fpcntl[2]));
457 } else {
458 __asm__ volatile (".chip 68k/68881\n\t"
459 "fmovemx %0,%%fp0-%%fp7\n\t"
460 "fmoveml %1,%%fpcr/%%fpsr/%%fpiar\n\t"
461 ".chip 68k"
462 : /* no outputs */
463 : "m" (*fpregs.f_fpregs),
464 "m" (*fpregs.f_fpcntl));
467 if (context_size &&
468 __copy_from_user(fpstate + 4, (long __user *)&uc->uc_fpstate + 1,
469 context_size))
470 goto out;
472 if (CPU_IS_COLDFIRE) {
473 __asm__ volatile ("frestore %0" : : "m" (*fpstate));
474 } else {
475 __asm__ volatile (".chip 68k/68881\n\t"
476 "frestore %0\n\t"
477 ".chip 68k"
478 : : "m" (*fpstate));
480 err = 0;
482 out:
483 return err;
487 * Set up a signal frame.
489 static inline void save_fpu_state(struct sigcontext *sc, struct pt_regs *regs)
491 if (FPU_IS_EMU) {
492 /* save registers */
493 memcpy(sc->sc_fpcntl, current->thread.fpcntl, 12);
494 memcpy(sc->sc_fpregs, current->thread.fp, 24);
495 return;
498 if (CPU_IS_COLDFIRE) {
499 __asm__ volatile ("fsave %0"
500 : : "m" (*sc->sc_fpstate) : "memory");
501 } else {
502 __asm__ volatile (".chip 68k/68881\n\t"
503 "fsave %0\n\t"
504 ".chip 68k"
505 : : "m" (*sc->sc_fpstate) : "memory");
508 if (CPU_IS_060 ? sc->sc_fpstate[2] : sc->sc_fpstate[0]) {
509 fpu_version = sc->sc_fpstate[0];
510 if (CPU_IS_020_OR_030 &&
511 regs->vector >= (VEC_FPBRUC * 4) &&
512 regs->vector <= (VEC_FPNAN * 4)) {
513 /* Clear pending exception in 68882 idle frame */
514 if (*(unsigned short *) sc->sc_fpstate == 0x1f38)
515 sc->sc_fpstate[0x38] |= 1 << 3;
518 if (CPU_IS_COLDFIRE) {
519 __asm__ volatile ("fmovemd %%fp0-%%fp1,%0\n\t"
520 "fmovel %%fpcr,%1\n\t"
521 "fmovel %%fpsr,%2\n\t"
522 "fmovel %%fpiar,%3"
523 : "=m" (sc->sc_fpregs[0]),
524 "=m" (sc->sc_fpcntl[0]),
525 "=m" (sc->sc_fpcntl[1]),
526 "=m" (sc->sc_fpcntl[2])
527 : /* no inputs */
528 : "memory");
529 } else {
530 __asm__ volatile (".chip 68k/68881\n\t"
531 "fmovemx %%fp0-%%fp1,%0\n\t"
532 "fmoveml %%fpcr/%%fpsr/%%fpiar,%1\n\t"
533 ".chip 68k"
534 : "=m" (*sc->sc_fpregs),
535 "=m" (*sc->sc_fpcntl)
536 : /* no inputs */
537 : "memory");
542 static inline int rt_save_fpu_state(struct ucontext __user *uc, struct pt_regs *regs)
544 unsigned char fpstate[FPCONTEXT_SIZE];
545 int context_size = CPU_IS_060 ? 8 : (CPU_IS_COLDFIRE ? 12 : 0);
546 int err = 0;
548 if (FPU_IS_EMU) {
549 /* save fpu control register */
550 err |= copy_to_user(uc->uc_mcontext.fpregs.f_fpcntl,
551 current->thread.fpcntl, 12);
552 /* save all other fpu register */
553 err |= copy_to_user(uc->uc_mcontext.fpregs.f_fpregs,
554 current->thread.fp, 96);
555 return err;
558 if (CPU_IS_COLDFIRE) {
559 __asm__ volatile ("fsave %0" : : "m" (*fpstate) : "memory");
560 } else {
561 __asm__ volatile (".chip 68k/68881\n\t"
562 "fsave %0\n\t"
563 ".chip 68k"
564 : : "m" (*fpstate) : "memory");
567 err |= __put_user(*(long *)fpstate, (long __user *)&uc->uc_fpstate);
568 if (CPU_IS_060 ? fpstate[2] : fpstate[0]) {
569 fpregset_t fpregs;
570 if (!(CPU_IS_060 || CPU_IS_COLDFIRE))
571 context_size = fpstate[1];
572 fpu_version = fpstate[0];
573 if (CPU_IS_020_OR_030 &&
574 regs->vector >= (VEC_FPBRUC * 4) &&
575 regs->vector <= (VEC_FPNAN * 4)) {
576 /* Clear pending exception in 68882 idle frame */
577 if (*(unsigned short *) fpstate == 0x1f38)
578 fpstate[0x38] |= 1 << 3;
580 if (CPU_IS_COLDFIRE) {
581 __asm__ volatile ("fmovemd %%fp0-%%fp7,%0\n\t"
582 "fmovel %%fpcr,%1\n\t"
583 "fmovel %%fpsr,%2\n\t"
584 "fmovel %%fpiar,%3"
585 : "=m" (fpregs.f_fpregs[0]),
586 "=m" (fpregs.f_fpcntl[0]),
587 "=m" (fpregs.f_fpcntl[1]),
588 "=m" (fpregs.f_fpcntl[2])
589 : /* no inputs */
590 : "memory");
591 } else {
592 __asm__ volatile (".chip 68k/68881\n\t"
593 "fmovemx %%fp0-%%fp7,%0\n\t"
594 "fmoveml %%fpcr/%%fpsr/%%fpiar,%1\n\t"
595 ".chip 68k"
596 : "=m" (*fpregs.f_fpregs),
597 "=m" (*fpregs.f_fpcntl)
598 : /* no inputs */
599 : "memory");
601 err |= copy_to_user(&uc->uc_mcontext.fpregs, &fpregs,
602 sizeof(fpregs));
604 if (context_size)
605 err |= copy_to_user((long __user *)&uc->uc_fpstate + 1, fpstate + 4,
606 context_size);
607 return err;
610 #else /* CONFIG_FPU */
613 * For the case with no FPU configured these all do nothing.
615 static inline int restore_fpu_state(struct sigcontext *sc)
617 return 0;
620 static inline int rt_restore_fpu_state(struct ucontext __user *uc)
622 return 0;
625 static inline void save_fpu_state(struct sigcontext *sc, struct pt_regs *regs)
629 static inline int rt_save_fpu_state(struct ucontext __user *uc, struct pt_regs *regs)
631 return 0;
634 #endif /* CONFIG_FPU */
636 static int mangle_kernel_stack(struct pt_regs *regs, int formatvec,
637 void __user *fp)
639 int fsize = frame_extra_sizes(formatvec >> 12);
640 if (fsize < 0) {
642 * user process trying to return with weird frame format
644 #ifdef DEBUG
645 printk("user process returning with weird frame format\n");
646 #endif
647 return 1;
649 if (!fsize) {
650 regs->format = formatvec >> 12;
651 regs->vector = formatvec & 0xfff;
652 } else {
653 struct switch_stack *sw = (struct switch_stack *)regs - 1;
654 unsigned long buf[fsize / 2]; /* yes, twice as much */
656 /* that'll make sure that expansion won't crap over data */
657 if (copy_from_user(buf + fsize / 4, fp, fsize))
658 return 1;
660 /* point of no return */
661 regs->format = formatvec >> 12;
662 regs->vector = formatvec & 0xfff;
663 #define frame_offset (sizeof(struct pt_regs)+sizeof(struct switch_stack))
664 __asm__ __volatile__ (
665 #ifdef CONFIG_COLDFIRE
666 " movel %0,%/sp\n\t"
667 " bra ret_from_signal\n"
668 #else
669 " movel %0,%/a0\n\t"
670 " subl %1,%/a0\n\t" /* make room on stack */
671 " movel %/a0,%/sp\n\t" /* set stack pointer */
672 /* move switch_stack and pt_regs */
673 "1: movel %0@+,%/a0@+\n\t"
674 " dbra %2,1b\n\t"
675 " lea %/sp@(%c3),%/a0\n\t" /* add offset of fmt */
676 " lsrl #2,%1\n\t"
677 " subql #1,%1\n\t"
678 /* copy to the gap we'd made */
679 "2: movel %4@+,%/a0@+\n\t"
680 " dbra %1,2b\n\t"
681 " bral ret_from_signal\n"
682 #endif
683 : /* no outputs, it doesn't ever return */
684 : "a" (sw), "d" (fsize), "d" (frame_offset/4-1),
685 "n" (frame_offset), "a" (buf + fsize/4)
686 : "a0");
687 #undef frame_offset
689 return 0;
692 static inline int
693 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *usc, void __user *fp)
695 int formatvec;
696 struct sigcontext context;
697 int err = 0;
699 /* Always make any pending restarted system calls return -EINTR */
700 current_thread_info()->restart_block.fn = do_no_restart_syscall;
702 /* get previous context */
703 if (copy_from_user(&context, usc, sizeof(context)))
704 goto badframe;
706 /* restore passed registers */
707 regs->d0 = context.sc_d0;
708 regs->d1 = context.sc_d1;
709 regs->a0 = context.sc_a0;
710 regs->a1 = context.sc_a1;
711 regs->sr = (regs->sr & 0xff00) | (context.sc_sr & 0xff);
712 regs->pc = context.sc_pc;
713 regs->orig_d0 = -1; /* disable syscall checks */
714 wrusp(context.sc_usp);
715 formatvec = context.sc_formatvec;
717 err = restore_fpu_state(&context);
719 if (err || mangle_kernel_stack(regs, formatvec, fp))
720 goto badframe;
722 return 0;
724 badframe:
725 return 1;
728 static inline int
729 rt_restore_ucontext(struct pt_regs *regs, struct switch_stack *sw,
730 struct ucontext __user *uc)
732 int temp;
733 greg_t __user *gregs = uc->uc_mcontext.gregs;
734 unsigned long usp;
735 int err;
737 /* Always make any pending restarted system calls return -EINTR */
738 current_thread_info()->restart_block.fn = do_no_restart_syscall;
740 err = __get_user(temp, &uc->uc_mcontext.version);
741 if (temp != MCONTEXT_VERSION)
742 goto badframe;
743 /* restore passed registers */
744 err |= __get_user(regs->d0, &gregs[0]);
745 err |= __get_user(regs->d1, &gregs[1]);
746 err |= __get_user(regs->d2, &gregs[2]);
747 err |= __get_user(regs->d3, &gregs[3]);
748 err |= __get_user(regs->d4, &gregs[4]);
749 err |= __get_user(regs->d5, &gregs[5]);
750 err |= __get_user(sw->d6, &gregs[6]);
751 err |= __get_user(sw->d7, &gregs[7]);
752 err |= __get_user(regs->a0, &gregs[8]);
753 err |= __get_user(regs->a1, &gregs[9]);
754 err |= __get_user(regs->a2, &gregs[10]);
755 err |= __get_user(sw->a3, &gregs[11]);
756 err |= __get_user(sw->a4, &gregs[12]);
757 err |= __get_user(sw->a5, &gregs[13]);
758 err |= __get_user(sw->a6, &gregs[14]);
759 err |= __get_user(usp, &gregs[15]);
760 wrusp(usp);
761 err |= __get_user(regs->pc, &gregs[16]);
762 err |= __get_user(temp, &gregs[17]);
763 regs->sr = (regs->sr & 0xff00) | (temp & 0xff);
764 regs->orig_d0 = -1; /* disable syscall checks */
765 err |= __get_user(temp, &uc->uc_formatvec);
767 err |= rt_restore_fpu_state(uc);
769 if (err || do_sigaltstack(&uc->uc_stack, NULL, usp) == -EFAULT)
770 goto badframe;
772 if (mangle_kernel_stack(regs, temp, &uc->uc_extra))
773 goto badframe;
775 return 0;
777 badframe:
778 return 1;
781 asmlinkage int do_sigreturn(unsigned long __unused)
783 struct switch_stack *sw = (struct switch_stack *) &__unused;
784 struct pt_regs *regs = (struct pt_regs *) (sw + 1);
785 unsigned long usp = rdusp();
786 struct sigframe __user *frame = (struct sigframe __user *)(usp - 4);
787 sigset_t set;
789 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
790 goto badframe;
791 if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
792 (_NSIG_WORDS > 1 &&
793 __copy_from_user(&set.sig[1], &frame->extramask,
794 sizeof(frame->extramask))))
795 goto badframe;
797 sigdelsetmask(&set, ~_BLOCKABLE);
798 set_current_blocked(&set);
800 if (restore_sigcontext(regs, &frame->sc, frame + 1))
801 goto badframe;
802 return regs->d0;
804 badframe:
805 force_sig(SIGSEGV, current);
806 return 0;
809 asmlinkage int do_rt_sigreturn(unsigned long __unused)
811 struct switch_stack *sw = (struct switch_stack *) &__unused;
812 struct pt_regs *regs = (struct pt_regs *) (sw + 1);
813 unsigned long usp = rdusp();
814 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(usp - 4);
815 sigset_t set;
817 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
818 goto badframe;
819 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
820 goto badframe;
822 sigdelsetmask(&set, ~_BLOCKABLE);
823 set_current_blocked(&set);
825 if (rt_restore_ucontext(regs, sw, &frame->uc))
826 goto badframe;
827 return regs->d0;
829 badframe:
830 force_sig(SIGSEGV, current);
831 return 0;
834 static void setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
835 unsigned long mask)
837 sc->sc_mask = mask;
838 sc->sc_usp = rdusp();
839 sc->sc_d0 = regs->d0;
840 sc->sc_d1 = regs->d1;
841 sc->sc_a0 = regs->a0;
842 sc->sc_a1 = regs->a1;
843 sc->sc_sr = regs->sr;
844 sc->sc_pc = regs->pc;
845 sc->sc_formatvec = regs->format << 12 | regs->vector;
846 save_a5_state(sc, regs);
847 save_fpu_state(sc, regs);
850 static inline int rt_setup_ucontext(struct ucontext __user *uc, struct pt_regs *regs)
852 struct switch_stack *sw = (struct switch_stack *)regs - 1;
853 greg_t __user *gregs = uc->uc_mcontext.gregs;
854 int err = 0;
856 err |= __put_user(MCONTEXT_VERSION, &uc->uc_mcontext.version);
857 err |= __put_user(regs->d0, &gregs[0]);
858 err |= __put_user(regs->d1, &gregs[1]);
859 err |= __put_user(regs->d2, &gregs[2]);
860 err |= __put_user(regs->d3, &gregs[3]);
861 err |= __put_user(regs->d4, &gregs[4]);
862 err |= __put_user(regs->d5, &gregs[5]);
863 err |= __put_user(sw->d6, &gregs[6]);
864 err |= __put_user(sw->d7, &gregs[7]);
865 err |= __put_user(regs->a0, &gregs[8]);
866 err |= __put_user(regs->a1, &gregs[9]);
867 err |= __put_user(regs->a2, &gregs[10]);
868 err |= __put_user(sw->a3, &gregs[11]);
869 err |= __put_user(sw->a4, &gregs[12]);
870 err |= __put_user(sw->a5, &gregs[13]);
871 err |= __put_user(sw->a6, &gregs[14]);
872 err |= __put_user(rdusp(), &gregs[15]);
873 err |= __put_user(regs->pc, &gregs[16]);
874 err |= __put_user(regs->sr, &gregs[17]);
875 err |= __put_user((regs->format << 12) | regs->vector, &uc->uc_formatvec);
876 err |= rt_save_fpu_state(uc, regs);
877 return err;
880 static inline void __user *
881 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
883 unsigned long usp;
885 /* Default to using normal stack. */
886 usp = rdusp();
888 /* This is the X/Open sanctioned signal stack switching. */
889 if (ka->sa.sa_flags & SA_ONSTACK) {
890 if (!sas_ss_flags(usp))
891 usp = current->sas_ss_sp + current->sas_ss_size;
893 return (void __user *)((usp - frame_size) & -8UL);
896 static int setup_frame (int sig, struct k_sigaction *ka,
897 sigset_t *set, struct pt_regs *regs)
899 struct sigframe __user *frame;
900 int fsize = frame_extra_sizes(regs->format);
901 struct sigcontext context;
902 int err = 0;
904 if (fsize < 0) {
905 #ifdef DEBUG
906 printk ("setup_frame: Unknown frame format %#x\n",
907 regs->format);
908 #endif
909 goto give_sigsegv;
912 frame = get_sigframe(ka, regs, sizeof(*frame) + fsize);
914 if (fsize)
915 err |= copy_to_user (frame + 1, regs + 1, fsize);
917 err |= __put_user((current_thread_info()->exec_domain
918 && current_thread_info()->exec_domain->signal_invmap
919 && sig < 32
920 ? current_thread_info()->exec_domain->signal_invmap[sig]
921 : sig),
922 &frame->sig);
924 err |= __put_user(regs->vector, &frame->code);
925 err |= __put_user(&frame->sc, &frame->psc);
927 if (_NSIG_WORDS > 1)
928 err |= copy_to_user(frame->extramask, &set->sig[1],
929 sizeof(frame->extramask));
931 setup_sigcontext(&context, regs, set->sig[0]);
932 err |= copy_to_user (&frame->sc, &context, sizeof(context));
934 /* Set up to return from userspace. */
935 #ifdef CONFIG_MMU
936 err |= __put_user(frame->retcode, &frame->pretcode);
937 /* moveq #,d0; trap #0 */
938 err |= __put_user(0x70004e40 + (__NR_sigreturn << 16),
939 (long __user *)(frame->retcode));
940 #else
941 err |= __put_user((void *) ret_from_user_signal, &frame->pretcode);
942 #endif
944 if (err)
945 goto give_sigsegv;
947 push_cache ((unsigned long) &frame->retcode);
950 * Set up registers for signal handler. All the state we are about
951 * to destroy is successfully copied to sigframe.
953 wrusp ((unsigned long) frame);
954 regs->pc = (unsigned long) ka->sa.sa_handler;
955 adjustformat(regs);
958 * This is subtle; if we build more than one sigframe, all but the
959 * first one will see frame format 0 and have fsize == 0, so we won't
960 * screw stkadj.
962 if (fsize)
963 regs->stkadj = fsize;
965 /* Prepare to skip over the extra stuff in the exception frame. */
966 if (regs->stkadj) {
967 struct pt_regs *tregs =
968 (struct pt_regs *)((ulong)regs + regs->stkadj);
969 #ifdef DEBUG
970 printk("Performing stackadjust=%04x\n", regs->stkadj);
971 #endif
972 /* This must be copied with decreasing addresses to
973 handle overlaps. */
974 tregs->vector = 0;
975 tregs->format = 0;
976 tregs->pc = regs->pc;
977 tregs->sr = regs->sr;
979 return 0;
981 give_sigsegv:
982 force_sigsegv(sig, current);
983 return err;
986 static int setup_rt_frame (int sig, struct k_sigaction *ka, siginfo_t *info,
987 sigset_t *set, struct pt_regs *regs)
989 struct rt_sigframe __user *frame;
990 int fsize = frame_extra_sizes(regs->format);
991 int err = 0;
993 if (fsize < 0) {
994 #ifdef DEBUG
995 printk ("setup_frame: Unknown frame format %#x\n",
996 regs->format);
997 #endif
998 goto give_sigsegv;
1001 frame = get_sigframe(ka, regs, sizeof(*frame));
1003 if (fsize)
1004 err |= copy_to_user (&frame->uc.uc_extra, regs + 1, fsize);
1006 err |= __put_user((current_thread_info()->exec_domain
1007 && current_thread_info()->exec_domain->signal_invmap
1008 && sig < 32
1009 ? current_thread_info()->exec_domain->signal_invmap[sig]
1010 : sig),
1011 &frame->sig);
1012 err |= __put_user(&frame->info, &frame->pinfo);
1013 err |= __put_user(&frame->uc, &frame->puc);
1014 err |= copy_siginfo_to_user(&frame->info, info);
1016 /* Create the ucontext. */
1017 err |= __put_user(0, &frame->uc.uc_flags);
1018 err |= __put_user(NULL, &frame->uc.uc_link);
1019 err |= __put_user((void __user *)current->sas_ss_sp,
1020 &frame->uc.uc_stack.ss_sp);
1021 err |= __put_user(sas_ss_flags(rdusp()),
1022 &frame->uc.uc_stack.ss_flags);
1023 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
1024 err |= rt_setup_ucontext(&frame->uc, regs);
1025 err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
1027 /* Set up to return from userspace. */
1028 #ifdef CONFIG_MMU
1029 err |= __put_user(frame->retcode, &frame->pretcode);
1030 #ifdef __mcoldfire__
1031 /* movel #__NR_rt_sigreturn,d0; trap #0 */
1032 err |= __put_user(0x203c0000, (long __user *)(frame->retcode + 0));
1033 err |= __put_user(0x00004e40 + (__NR_rt_sigreturn << 16),
1034 (long __user *)(frame->retcode + 4));
1035 #else
1036 /* moveq #,d0; notb d0; trap #0 */
1037 err |= __put_user(0x70004600 + ((__NR_rt_sigreturn ^ 0xff) << 16),
1038 (long __user *)(frame->retcode + 0));
1039 err |= __put_user(0x4e40, (short __user *)(frame->retcode + 4));
1040 #endif
1041 #else
1042 err |= __put_user((void *) ret_from_user_rt_signal, &frame->pretcode);
1043 #endif /* CONFIG_MMU */
1045 if (err)
1046 goto give_sigsegv;
1048 push_cache ((unsigned long) &frame->retcode);
1051 * Set up registers for signal handler. All the state we are about
1052 * to destroy is successfully copied to sigframe.
1054 wrusp ((unsigned long) frame);
1055 regs->pc = (unsigned long) ka->sa.sa_handler;
1056 adjustformat(regs);
1059 * This is subtle; if we build more than one sigframe, all but the
1060 * first one will see frame format 0 and have fsize == 0, so we won't
1061 * screw stkadj.
1063 if (fsize)
1064 regs->stkadj = fsize;
1066 /* Prepare to skip over the extra stuff in the exception frame. */
1067 if (regs->stkadj) {
1068 struct pt_regs *tregs =
1069 (struct pt_regs *)((ulong)regs + regs->stkadj);
1070 #ifdef DEBUG
1071 printk("Performing stackadjust=%04x\n", regs->stkadj);
1072 #endif
1073 /* This must be copied with decreasing addresses to
1074 handle overlaps. */
1075 tregs->vector = 0;
1076 tregs->format = 0;
1077 tregs->pc = regs->pc;
1078 tregs->sr = regs->sr;
1080 return 0;
1082 give_sigsegv:
1083 force_sigsegv(sig, current);
1084 return err;
1087 static inline void
1088 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
1090 switch (regs->d0) {
1091 case -ERESTARTNOHAND:
1092 if (!has_handler)
1093 goto do_restart;
1094 regs->d0 = -EINTR;
1095 break;
1097 case -ERESTART_RESTARTBLOCK:
1098 if (!has_handler) {
1099 regs->d0 = __NR_restart_syscall;
1100 regs->pc -= 2;
1101 break;
1103 regs->d0 = -EINTR;
1104 break;
1106 case -ERESTARTSYS:
1107 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
1108 regs->d0 = -EINTR;
1109 break;
1111 /* fallthrough */
1112 case -ERESTARTNOINTR:
1113 do_restart:
1114 regs->d0 = regs->orig_d0;
1115 regs->pc -= 2;
1116 break;
1121 * OK, we're invoking a handler
1123 static void
1124 handle_signal(int sig, struct k_sigaction *ka, siginfo_t *info,
1125 sigset_t *oldset, struct pt_regs *regs)
1127 int err;
1128 /* are we from a system call? */
1129 if (regs->orig_d0 >= 0)
1130 /* If so, check system call restarting.. */
1131 handle_restart(regs, ka, 1);
1133 /* set up the stack frame */
1134 if (ka->sa.sa_flags & SA_SIGINFO)
1135 err = setup_rt_frame(sig, ka, info, oldset, regs);
1136 else
1137 err = setup_frame(sig, ka, oldset, regs);
1139 if (err)
1140 return;
1142 block_sigmask(ka, sig);
1144 if (test_thread_flag(TIF_DELAYED_TRACE)) {
1145 regs->sr &= ~0x8000;
1146 send_sig(SIGTRAP, current, 1);
1149 clear_thread_flag(TIF_RESTORE_SIGMASK);
1153 * Note that 'init' is a special process: it doesn't get signals it doesn't
1154 * want to handle. Thus you cannot kill init even with a SIGKILL even by
1155 * mistake.
1157 asmlinkage void do_signal(struct pt_regs *regs)
1159 siginfo_t info;
1160 struct k_sigaction ka;
1161 int signr;
1162 sigset_t *oldset;
1164 current->thread.esp0 = (unsigned long) regs;
1166 if (test_thread_flag(TIF_RESTORE_SIGMASK))
1167 oldset = &current->saved_sigmask;
1168 else
1169 oldset = &current->blocked;
1171 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
1172 if (signr > 0) {
1173 /* Whee! Actually deliver the signal. */
1174 handle_signal(signr, &ka, &info, oldset, regs);
1175 return;
1178 /* Did we come from a system call? */
1179 if (regs->orig_d0 >= 0)
1180 /* Restart the system call - no handlers present */
1181 handle_restart(regs, NULL, 0);
1183 /* If there's no signal to deliver, we just restore the saved mask. */
1184 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
1185 clear_thread_flag(TIF_RESTORE_SIGMASK);
1186 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);