- pre3:
[davej-history.git] / arch / sh / kernel / signal.c
blobfe2f1b31958fe198437f8695e737e8a1889db457
1 /* $Id: signal.c,v 1.21 2000/03/11 14:06:21 gniibe Exp $
3 * linux/arch/sh/kernel/signal.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
9 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
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/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/stddef.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
28 #define DEBUG_SIG 0
30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32 asmlinkage int sys_wait4(pid_t pid, unsigned long *stat_addr,
33 int options, unsigned long *ru);
34 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
36 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
38 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
39 return -EFAULT;
40 if (from->si_code < 0)
41 return __copy_to_user(to, from, sizeof(siginfo_t));
42 else {
43 int err;
45 /* If you change siginfo_t structure, please be sure
46 this code is fixed accordingly.
47 It should never copy any pad contained in the structure
48 to avoid security leaks, but must copy the generic
49 3 ints plus the relevant union member. */
50 err = __put_user(from->si_signo, &to->si_signo);
51 err |= __put_user(from->si_errno, &to->si_errno);
52 err |= __put_user((short)from->si_code, &to->si_code);
53 /* First 32bits of unions are always present. */
54 err |= __put_user(from->si_pid, &to->si_pid);
55 switch (from->si_code >> 16) {
56 case __SI_FAULT >> 16:
57 break;
58 case __SI_CHLD >> 16:
59 err |= __put_user(from->si_utime, &to->si_utime);
60 err |= __put_user(from->si_stime, &to->si_stime);
61 err |= __put_user(from->si_status, &to->si_status);
62 default:
63 err |= __put_user(from->si_uid, &to->si_uid);
64 break;
65 /* case __SI_RT: This is not generated by the kernel as of now. */
67 return err;
72 * Atomically swap in the new signal mask, and wait for a signal.
74 asmlinkage int
75 sys_sigsuspend(old_sigset_t mask,
76 unsigned long r5, unsigned long r6, unsigned long r7,
77 struct pt_regs regs)
79 sigset_t saveset;
81 mask &= _BLOCKABLE;
82 spin_lock_irq(&current->sigmask_lock);
83 saveset = current->blocked;
84 siginitset(&current->blocked, mask);
85 recalc_sigpending(current);
86 spin_unlock_irq(&current->sigmask_lock);
88 regs.regs[0] = -EINTR;
89 while (1) {
90 current->state = TASK_INTERRUPTIBLE;
91 schedule();
92 if (do_signal(&regs, &saveset))
93 return -EINTR;
97 asmlinkage int
98 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize,
99 unsigned long r6, unsigned long r7,
100 struct pt_regs regs)
102 sigset_t saveset, newset;
104 /* XXX: Don't preclude handling different sized sigset_t's. */
105 if (sigsetsize != sizeof(sigset_t))
106 return -EINVAL;
108 if (copy_from_user(&newset, unewset, sizeof(newset)))
109 return -EFAULT;
110 sigdelsetmask(&newset, ~_BLOCKABLE);
111 spin_lock_irq(&current->sigmask_lock);
112 saveset = current->blocked;
113 current->blocked = newset;
114 recalc_sigpending(current);
115 spin_unlock_irq(&current->sigmask_lock);
117 regs.regs[0] = -EINTR;
118 while (1) {
119 current->state = TASK_INTERRUPTIBLE;
120 schedule();
121 if (do_signal(&regs, &saveset))
122 return -EINTR;
126 asmlinkage int
127 sys_sigaction(int sig, const struct old_sigaction *act,
128 struct old_sigaction *oact)
130 struct k_sigaction new_ka, old_ka;
131 int ret;
133 if (act) {
134 old_sigset_t mask;
135 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
136 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
137 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
138 return -EFAULT;
139 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
140 __get_user(mask, &act->sa_mask);
141 siginitset(&new_ka.sa.sa_mask, mask);
144 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
146 if (!ret && oact) {
147 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
148 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
149 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
150 return -EFAULT;
151 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
152 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
155 return ret;
158 asmlinkage int
159 sys_sigaltstack(const stack_t *uss, stack_t *uoss,
160 unsigned long r6, unsigned long r7,
161 struct pt_regs regs)
163 return do_sigaltstack(uss, uoss, regs.regs[15]);
168 * Do a signal return; undo the signal stack.
171 struct sigframe
173 struct sigcontext sc;
174 unsigned long extramask[_NSIG_WORDS-1];
175 char retcode[4];
178 struct rt_sigframe
180 struct siginfo *pinfo;
181 void *puc;
182 struct siginfo info;
183 struct ucontext uc;
184 char retcode[4];
187 #if defined(__SH4__)
188 static inline int restore_sigcontext_fpu(struct sigcontext *sc)
190 struct task_struct *tsk = current;
192 tsk->used_math = 1;
193 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
194 sizeof(long)*(16*2+2));
197 static inline int save_sigcontext_fpu(struct sigcontext *sc)
199 struct task_struct *tsk = current;
200 unsigned long flags;
201 int val;
203 if (!tsk->used_math) {
204 val = 0;
205 __copy_to_user(&sc->sc_ownedfp, &val, sizeof(int));
206 return 0;
209 val = 1;
210 __copy_to_user(&sc->sc_ownedfp, &val, sizeof(int));
212 /* This will cause a "finit" to be triggered by the next
213 attempted FPU operation by the 'current' process.
215 tsk->used_math = 0;
217 save_and_cli(flags);
218 unlazy_fpu(tsk);
219 restore_flags(flags);
221 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
222 sizeof(long)*(16*2+2));
224 #endif
226 static int
227 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, int *r0_p)
229 unsigned int err = 0;
231 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
232 COPY(regs[1]);
233 COPY(regs[2]); COPY(regs[3]);
234 COPY(regs[4]); COPY(regs[5]);
235 COPY(regs[6]); COPY(regs[7]);
236 COPY(regs[8]); COPY(regs[9]);
237 COPY(regs[10]); COPY(regs[11]);
238 COPY(regs[12]); COPY(regs[13]);
239 COPY(regs[14]); COPY(regs[15]);
240 COPY(gbr); COPY(mach);
241 COPY(macl); COPY(pr);
242 COPY(sr); COPY(pc);
243 #undef COPY
245 #if defined(__SH4__)
247 int owned_fp;
248 struct task_struct *tsk = current;
250 regs->sr |= SR_FD; /* Release FPU */
251 clear_fpu(tsk);
252 current->used_math = 0;
253 __get_user (owned_fp, &sc->sc_ownedfp);
254 if (owned_fp)
255 err |= restore_sigcontext_fpu(sc);
257 #endif
259 regs->syscall_nr = -1; /* disable syscall checks */
260 err |= __get_user(*r0_p, &sc->sc_regs[0]);
261 return err;
264 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
265 unsigned long r6, unsigned long r7,
266 struct pt_regs regs)
268 struct sigframe *frame = (struct sigframe *)regs.regs[15];
269 sigset_t set;
270 int r0;
272 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
273 goto badframe;
275 if (__get_user(set.sig[0], &frame->sc.oldmask)
276 || (_NSIG_WORDS > 1
277 && __copy_from_user(&set.sig[1], &frame->extramask,
278 sizeof(frame->extramask))))
279 goto badframe;
281 sigdelsetmask(&set, ~_BLOCKABLE);
283 spin_lock_irq(&current->sigmask_lock);
284 current->blocked = set;
285 recalc_sigpending(current);
286 spin_unlock_irq(&current->sigmask_lock);
288 if (restore_sigcontext(&regs, &frame->sc, &r0))
289 goto badframe;
290 return r0;
292 badframe:
293 force_sig(SIGSEGV, current);
294 return 0;
297 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
298 unsigned long r6, unsigned long r7,
299 struct pt_regs regs)
301 struct rt_sigframe *frame = (struct rt_sigframe *)regs.regs[15];
302 sigset_t set;
303 stack_t st;
304 int r0;
306 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
307 goto badframe;
309 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
310 goto badframe;
312 sigdelsetmask(&set, ~_BLOCKABLE);
313 spin_lock_irq(&current->sigmask_lock);
314 current->blocked = set;
315 recalc_sigpending(current);
316 spin_unlock_irq(&current->sigmask_lock);
318 if (restore_sigcontext(&regs, &frame->uc.uc_mcontext, &r0))
319 goto badframe;
321 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
322 goto badframe;
323 /* It is more difficult to avoid calling this function than to
324 call it and ignore errors. */
325 do_sigaltstack(&st, NULL, regs.regs[15]);
327 return r0;
329 badframe:
330 force_sig(SIGSEGV, current);
331 return 0;
335 * Set up a signal frame.
338 static int
339 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
340 unsigned long mask)
342 int err = 0;
344 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
345 COPY(regs[0]); COPY(regs[1]);
346 COPY(regs[2]); COPY(regs[3]);
347 COPY(regs[4]); COPY(regs[5]);
348 COPY(regs[6]); COPY(regs[7]);
349 COPY(regs[8]); COPY(regs[9]);
350 COPY(regs[10]); COPY(regs[11]);
351 COPY(regs[12]); COPY(regs[13]);
352 COPY(regs[14]); COPY(regs[15]);
353 COPY(gbr); COPY(mach);
354 COPY(macl); COPY(pr);
355 COPY(sr); COPY(pc);
356 #undef COPY
358 #if defined(__SH4__)
359 err |= save_sigcontext_fpu(sc);
360 #endif
362 /* non-iBCS2 extensions.. */
363 err |= __put_user(mask, &sc->oldmask);
365 return err;
369 * Determine which stack to use..
371 static inline void *
372 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
374 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp))
375 sp = current->sas_ss_sp + current->sas_ss_size;
377 return (void *)((sp - frame_size) & -8ul);
380 static void setup_frame(int sig, struct k_sigaction *ka,
381 sigset_t *set, struct pt_regs *regs)
383 struct sigframe *frame;
384 int err = 0;
385 int signal;
387 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
389 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
390 goto give_sigsegv;
392 signal = current->exec_domain
393 && current->exec_domain->signal_invmap
394 && sig < 32
395 ? current->exec_domain->signal_invmap[sig]
396 : sig;
398 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
400 if (_NSIG_WORDS > 1) {
401 err |= __copy_to_user(frame->extramask, &set->sig[1],
402 sizeof(frame->extramask));
405 /* Set up to return from userspace. If provided, use a stub
406 already in userspace. */
407 if (ka->sa.sa_flags & SA_RESTORER) {
408 regs->pr = (unsigned long) ka->sa.sa_restorer;
409 } else {
410 /* This is : mov #__NR_sigreturn,r3 ; trapa #0x10 */
411 #ifdef __LITTLE_ENDIAN__
412 unsigned long code = 0xc310e300 | (__NR_sigreturn);
413 #else
414 unsigned long code = 0xe300c310 | (__NR_sigreturn << 16);
415 #endif
417 regs->pr = (unsigned long) frame->retcode;
418 err |= __put_user(code, (long *)(frame->retcode+0));
421 if (err)
422 goto give_sigsegv;
424 /* Set up registers for signal handler */
425 regs->regs[15] = (unsigned long) frame;
426 regs->regs[4] = signal; /* Arg for signal handler */
427 regs->pc = (unsigned long) ka->sa.sa_handler;
429 set_fs(USER_DS);
431 #if DEBUG_SIG
432 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
433 current->comm, current->pid, frame, regs->pc, regs->pr);
434 #endif
436 flush_icache_range(regs->pr, regs->pr+4);
437 return;
439 give_sigsegv:
440 if (sig == SIGSEGV)
441 ka->sa.sa_handler = SIG_DFL;
442 force_sig(SIGSEGV, current);
445 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
446 sigset_t *set, struct pt_regs *regs)
448 struct rt_sigframe *frame;
449 int err = 0;
450 int signal;
452 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
454 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
455 goto give_sigsegv;
457 signal = current->exec_domain
458 && current->exec_domain->signal_invmap
459 && sig < 32
460 ? current->exec_domain->signal_invmap[sig]
461 : sig;
463 err |= __put_user(&frame->info, &frame->pinfo);
464 err |= __put_user(&frame->uc, &frame->puc);
465 err |= copy_siginfo_to_user(&frame->info, info);
467 /* Create the ucontext. */
468 err |= __put_user(0, &frame->uc.uc_flags);
469 err |= __put_user(0, &frame->uc.uc_link);
470 err |= __put_user((void *)current->sas_ss_sp,
471 &frame->uc.uc_stack.ss_sp);
472 err |= __put_user(sas_ss_flags(regs->regs[15]),
473 &frame->uc.uc_stack.ss_flags);
474 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
475 err |= setup_sigcontext(&frame->uc.uc_mcontext,
476 regs, set->sig[0]);
477 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
479 /* Set up to return from userspace. If provided, use a stub
480 already in userspace. */
481 if (ka->sa.sa_flags & SA_RESTORER) {
482 regs->pr = (unsigned long) ka->sa.sa_restorer;
483 } else {
484 /* This is : mov #__NR_rt_sigreturn,r3 ; trapa #0x10 */
485 #ifdef __LITTLE_ENDIAN__
486 unsigned long code = 0xc310e300 | (__NR_rt_sigreturn);
487 #else
488 unsigned long code = 0xe300c310 | (__NR_rt_sigreturn << 16);
489 #endif
491 regs->pr = (unsigned long) frame->retcode;
492 err |= __put_user(code, (long *)(frame->retcode+0));
495 if (err)
496 goto give_sigsegv;
498 /* Set up registers for signal handler */
499 regs->regs[15] = (unsigned long) frame;
500 regs->regs[4] = signal; /* Arg for signal handler */
501 regs->pc = (unsigned long) ka->sa.sa_handler;
503 set_fs(USER_DS);
505 #if DEBUG_SIG
506 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
507 current->comm, current->pid, frame, regs->pc, regs->pr);
508 #endif
510 flush_icache_range(regs->pr, regs->pr+4);
511 return;
513 give_sigsegv:
514 if (sig == SIGSEGV)
515 ka->sa.sa_handler = SIG_DFL;
516 force_sig(SIGSEGV, current);
520 * OK, we're invoking a handler
523 static void
524 handle_signal(unsigned long sig, struct k_sigaction *ka,
525 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
527 /* Are we from a system call? */
528 if (regs->syscall_nr >= 0) {
529 /* If so, check system call restarting.. */
530 switch (regs->regs[0]) {
531 case -ERESTARTNOHAND:
532 regs->regs[0] = -EINTR;
533 break;
535 case -ERESTARTSYS:
536 if (!(ka->sa.sa_flags & SA_RESTART)) {
537 regs->regs[0] = -EINTR;
538 break;
540 /* fallthrough */
541 case -ERESTARTNOINTR:
542 regs->regs[0] = regs->syscall_nr;
543 regs->pc -= 2;
547 /* Set up the stack frame */
548 if (ka->sa.sa_flags & SA_SIGINFO)
549 setup_rt_frame(sig, ka, info, oldset, regs);
550 else
551 setup_frame(sig, ka, oldset, regs);
553 if (ka->sa.sa_flags & SA_ONESHOT)
554 ka->sa.sa_handler = SIG_DFL;
556 if (!(ka->sa.sa_flags & SA_NODEFER)) {
557 spin_lock_irq(&current->sigmask_lock);
558 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
559 sigaddset(&current->blocked,sig);
560 recalc_sigpending(current);
561 spin_unlock_irq(&current->sigmask_lock);
566 * Note that 'init' is a special process: it doesn't get signals it doesn't
567 * want to handle. Thus you cannot kill init even with a SIGKILL even by
568 * mistake.
570 * Note that we go through the signals twice: once to check the signals that
571 * the kernel can handle, and then we build all the user-level signal handling
572 * stack-frames in one go after that.
574 int do_signal(struct pt_regs *regs, sigset_t *oldset)
576 siginfo_t info;
577 struct k_sigaction *ka;
580 * We want the common case to go fast, which
581 * is why we may in certain cases get here from
582 * kernel mode. Just return without doing anything
583 * if so.
585 if (!user_mode(regs))
586 return 1;
588 if (!oldset)
589 oldset = &current->blocked;
591 for (;;) {
592 unsigned long signr;
594 spin_lock_irq(&current->sigmask_lock);
595 signr = dequeue_signal(&current->blocked, &info);
596 spin_unlock_irq(&current->sigmask_lock);
598 if (!signr)
599 break;
601 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
602 /* Let the debugger run. */
603 current->exit_code = signr;
604 current->state = TASK_STOPPED;
605 notify_parent(current, SIGCHLD);
606 schedule();
608 /* We're back. Did the debugger cancel the sig? */
609 if (!(signr = current->exit_code))
610 continue;
611 current->exit_code = 0;
613 /* The debugger continued. Ignore SIGSTOP. */
614 if (signr == SIGSTOP)
615 continue;
617 /* Update the siginfo structure. Is this good? */
618 if (signr != info.si_signo) {
619 info.si_signo = signr;
620 info.si_errno = 0;
621 info.si_code = SI_USER;
622 info.si_pid = current->p_pptr->pid;
623 info.si_uid = current->p_pptr->uid;
626 /* If the (new) signal is now blocked, requeue it. */
627 if (sigismember(&current->blocked, signr)) {
628 send_sig_info(signr, &info, current);
629 continue;
633 ka = &current->sig->action[signr-1];
634 if (ka->sa.sa_handler == SIG_IGN) {
635 if (signr != SIGCHLD)
636 continue;
637 /* Check for SIGCHLD: it's special. */
638 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
639 /* nothing */;
640 continue;
643 if (ka->sa.sa_handler == SIG_DFL) {
644 int exit_code = signr;
646 /* Init gets no signals it doesn't want. */
647 if (current->pid == 1)
648 continue;
650 switch (signr) {
651 case SIGCONT: case SIGCHLD: case SIGWINCH:
652 continue;
654 case SIGTSTP: case SIGTTIN: case SIGTTOU:
655 if (is_orphaned_pgrp(current->pgrp))
656 continue;
657 /* FALLTHRU */
659 case SIGSTOP:
660 current->state = TASK_STOPPED;
661 current->exit_code = signr;
662 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
663 notify_parent(current, SIGCHLD);
664 schedule();
665 continue;
667 case SIGQUIT: case SIGILL: case SIGTRAP:
668 case SIGABRT: case SIGFPE: case SIGSEGV:
669 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
670 if (do_coredump(signr, regs))
671 exit_code |= 0x80;
672 /* FALLTHRU */
674 default:
675 sigaddset(&current->pending.signal, signr);
676 recalc_sigpending(current);
677 current->flags |= PF_SIGNALED;
678 do_exit(exit_code);
679 /* NOTREACHED */
683 /* Whee! Actually deliver the signal. */
684 handle_signal(signr, ka, &info, oldset, regs);
685 return 1;
688 /* Did we come from a system call? */
689 if (regs->syscall_nr >= 0) {
690 /* Restart the system call - no handlers present */
691 if (regs->regs[0] == -ERESTARTNOHAND ||
692 regs->regs[0] == -ERESTARTSYS ||
693 regs->regs[0] == -ERESTARTNOINTR) {
694 regs->regs[0] = regs->syscall_nr;
695 regs->pc -= 2;
698 return 0;