Import 2.3.99pre6-6
[davej-history.git] / arch / sh / kernel / signal.c
blob8751d45f547c95059eb7d816e7cbbac4f90cef76
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);
37 * Atomically swap in the new signal mask, and wait for a signal.
39 asmlinkage int
40 sys_sigsuspend(old_sigset_t mask,
41 unsigned long r5, unsigned long r6, unsigned long r7,
42 struct pt_regs regs)
44 sigset_t saveset;
46 mask &= _BLOCKABLE;
47 spin_lock_irq(&current->sigmask_lock);
48 saveset = current->blocked;
49 siginitset(&current->blocked, mask);
50 recalc_sigpending(current);
51 spin_unlock_irq(&current->sigmask_lock);
53 regs.regs[0] = -EINTR;
54 while (1) {
55 current->state = TASK_INTERRUPTIBLE;
56 schedule();
57 if (do_signal(&regs, &saveset))
58 return -EINTR;
62 asmlinkage int
63 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize,
64 unsigned long r6, unsigned long r7,
65 struct pt_regs regs)
67 sigset_t saveset, newset;
69 /* XXX: Don't preclude handling different sized sigset_t's. */
70 if (sigsetsize != sizeof(sigset_t))
71 return -EINVAL;
73 if (copy_from_user(&newset, unewset, sizeof(newset)))
74 return -EFAULT;
75 sigdelsetmask(&newset, ~_BLOCKABLE);
76 spin_lock_irq(&current->sigmask_lock);
77 saveset = current->blocked;
78 current->blocked = newset;
79 recalc_sigpending(current);
80 spin_unlock_irq(&current->sigmask_lock);
82 regs.regs[0] = -EINTR;
83 while (1) {
84 current->state = TASK_INTERRUPTIBLE;
85 schedule();
86 if (do_signal(&regs, &saveset))
87 return -EINTR;
91 asmlinkage int
92 sys_sigaction(int sig, const struct old_sigaction *act,
93 struct old_sigaction *oact)
95 struct k_sigaction new_ka, old_ka;
96 int ret;
98 if (act) {
99 old_sigset_t mask;
100 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
101 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
102 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
103 return -EFAULT;
104 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
105 __get_user(mask, &act->sa_mask);
106 siginitset(&new_ka.sa.sa_mask, mask);
109 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
111 if (!ret && oact) {
112 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
113 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
114 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
115 return -EFAULT;
116 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
117 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
120 return ret;
123 asmlinkage int
124 sys_sigaltstack(const stack_t *uss, stack_t *uoss,
125 unsigned long r6, unsigned long r7,
126 struct pt_regs regs)
128 return do_sigaltstack(uss, uoss, regs.regs[15]);
133 * Do a signal return; undo the signal stack.
136 struct sigframe
138 struct sigcontext sc;
139 unsigned long extramask[_NSIG_WORDS-1];
140 char retcode[4];
143 struct rt_sigframe
145 struct siginfo *pinfo;
146 void *puc;
147 struct siginfo info;
148 struct ucontext uc;
149 char retcode[4];
152 #if defined(__SH4__)
153 static inline int restore_sigcontext_fpu(struct sigcontext *sc)
155 struct task_struct *tsk = current;
157 tsk->used_math = 1;
158 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
159 sizeof(long)*(16*2+2));
162 static inline int save_sigcontext_fpu(struct sigcontext *sc)
164 struct task_struct *tsk = current;
165 unsigned long flags;
167 if (!tsk->used_math) {
168 sc->sc_ownedfp = 0;
169 return 0;
172 sc->sc_ownedfp = 1;
174 /* This will cause a "finit" to be triggered by the next
175 attempted FPU operation by the 'current' process.
177 tsk->used_math = 0;
179 save_and_cli(flags);
180 unlazy_fpu(tsk);
181 restore_flags(flags);
183 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
184 sizeof(long)*(16*2+2));
186 #endif
188 static int
189 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, int *r0_p)
191 unsigned int err = 0;
193 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
194 COPY(regs[1]);
195 COPY(regs[2]); COPY(regs[3]);
196 COPY(regs[4]); COPY(regs[5]);
197 COPY(regs[6]); COPY(regs[7]);
198 COPY(regs[8]); COPY(regs[9]);
199 COPY(regs[10]); COPY(regs[11]);
200 COPY(regs[12]); COPY(regs[13]);
201 COPY(regs[14]); COPY(regs[15]);
202 COPY(gbr); COPY(mach);
203 COPY(macl); COPY(pr);
204 COPY(sr); COPY(pc);
205 #undef COPY
207 #if defined(__SH4__)
209 int owned_fp;
210 struct task_struct *tsk = current;
212 regs->sr |= SR_FD; /* Release FPU */
213 clear_fpu(tsk);
214 current->used_math = 0;
215 __get_user (owned_fp, &sc->sc_ownedfp);
216 if (owned_fp)
217 err |= restore_sigcontext_fpu(sc);
219 #endif
221 regs->syscall_nr = -1; /* disable syscall checks */
222 err |= __get_user(*r0_p, &sc->sc_regs[0]);
223 return err;
226 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
227 unsigned long r6, unsigned long r7,
228 struct pt_regs regs)
230 struct sigframe *frame = (struct sigframe *)regs.regs[15];
231 sigset_t set;
232 int r0;
234 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
235 goto badframe;
237 if (__get_user(set.sig[0], &frame->sc.oldmask)
238 || (_NSIG_WORDS > 1
239 && __copy_from_user(&set.sig[1], &frame->extramask,
240 sizeof(frame->extramask))))
241 goto badframe;
243 sigdelsetmask(&set, ~_BLOCKABLE);
245 spin_lock_irq(&current->sigmask_lock);
246 current->blocked = set;
247 recalc_sigpending(current);
248 spin_unlock_irq(&current->sigmask_lock);
250 if (restore_sigcontext(&regs, &frame->sc, &r0))
251 goto badframe;
252 return r0;
254 badframe:
255 force_sig(SIGSEGV, current);
256 return 0;
259 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
260 unsigned long r6, unsigned long r7,
261 struct pt_regs regs)
263 struct rt_sigframe *frame = (struct rt_sigframe *)regs.regs[15];
264 sigset_t set;
265 stack_t st;
266 int r0;
268 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
269 goto badframe;
271 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
272 goto badframe;
274 sigdelsetmask(&set, ~_BLOCKABLE);
275 spin_lock_irq(&current->sigmask_lock);
276 current->blocked = set;
277 recalc_sigpending(current);
278 spin_unlock_irq(&current->sigmask_lock);
280 if (restore_sigcontext(&regs, &frame->uc.uc_mcontext, &r0))
281 goto badframe;
283 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
284 goto badframe;
285 /* It is more difficult to avoid calling this function than to
286 call it and ignore errors. */
287 do_sigaltstack(&st, NULL, regs.regs[15]);
289 return r0;
291 badframe:
292 force_sig(SIGSEGV, current);
293 return 0;
297 * Set up a signal frame.
300 static int
301 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
302 unsigned long mask)
304 int err = 0;
306 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
307 COPY(regs[0]); COPY(regs[1]);
308 COPY(regs[2]); COPY(regs[3]);
309 COPY(regs[4]); COPY(regs[5]);
310 COPY(regs[6]); COPY(regs[7]);
311 COPY(regs[8]); COPY(regs[9]);
312 COPY(regs[10]); COPY(regs[11]);
313 COPY(regs[12]); COPY(regs[13]);
314 COPY(regs[14]); COPY(regs[15]);
315 COPY(gbr); COPY(mach);
316 COPY(macl); COPY(pr);
317 COPY(sr); COPY(pc);
318 #undef COPY
320 #if defined(__SH4__)
321 err |= save_sigcontext_fpu(sc);
322 #endif
324 /* non-iBCS2 extensions.. */
325 err |= __put_user(mask, &sc->oldmask);
327 return err;
331 * Determine which stack to use..
333 static inline void *
334 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
336 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp))
337 sp = current->sas_ss_sp + current->sas_ss_size;
339 return (void *)((sp - frame_size) & -8ul);
342 static void setup_frame(int sig, struct k_sigaction *ka,
343 sigset_t *set, struct pt_regs *regs)
345 struct sigframe *frame;
346 int err = 0;
347 int signal;
349 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
351 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
352 goto give_sigsegv;
354 signal = current->exec_domain
355 && current->exec_domain->signal_invmap
356 && sig < 32
357 ? current->exec_domain->signal_invmap[sig]
358 : sig;
360 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
362 if (_NSIG_WORDS > 1) {
363 err |= __copy_to_user(frame->extramask, &set->sig[1],
364 sizeof(frame->extramask));
367 /* Set up to return from userspace. If provided, use a stub
368 already in userspace. */
369 if (ka->sa.sa_flags & SA_RESTORER) {
370 regs->pr = (unsigned long) ka->sa.sa_restorer;
371 } else {
372 /* This is : mov #__NR_sigreturn,r3 ; trapa #0x10 */
373 #ifdef __LITTLE_ENDIAN__
374 unsigned long code = 0xc310e300 | (__NR_sigreturn);
375 #else
376 unsigned long code = 0xe300c310 | (__NR_sigreturn << 16);
377 #endif
379 regs->pr = (unsigned long) frame->retcode;
380 err |= __put_user(code, (long *)(frame->retcode+0));
383 if (err)
384 goto give_sigsegv;
386 /* Set up registers for signal handler */
387 regs->regs[15] = (unsigned long) frame;
388 regs->regs[4] = signal; /* Arg for signal handler */
389 regs->pc = (unsigned long) ka->sa.sa_handler;
391 set_fs(USER_DS);
393 #if DEBUG_SIG
394 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
395 current->comm, current->pid, frame, regs->pc, regs->pr);
396 #endif
398 flush_icache_range(regs->pr, regs->pr+4);
399 return;
401 give_sigsegv:
402 if (sig == SIGSEGV)
403 ka->sa.sa_handler = SIG_DFL;
404 force_sig(SIGSEGV, current);
407 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
408 sigset_t *set, struct pt_regs *regs)
410 struct rt_sigframe *frame;
411 int err = 0;
412 int signal;
414 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
416 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
417 goto give_sigsegv;
419 signal = current->exec_domain
420 && current->exec_domain->signal_invmap
421 && sig < 32
422 ? current->exec_domain->signal_invmap[sig]
423 : sig;
425 err |= __put_user(&frame->info, &frame->pinfo);
426 err |= __put_user(&frame->uc, &frame->puc);
427 err |= __copy_to_user(&frame->info, info, sizeof(*info));
429 /* Create the ucontext. */
430 err |= __put_user(0, &frame->uc.uc_flags);
431 err |= __put_user(0, &frame->uc.uc_link);
432 err |= __put_user((void *)current->sas_ss_sp,
433 &frame->uc.uc_stack.ss_sp);
434 err |= __put_user(sas_ss_flags(regs->regs[15]),
435 &frame->uc.uc_stack.ss_flags);
436 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
437 err |= setup_sigcontext(&frame->uc.uc_mcontext,
438 regs, set->sig[0]);
439 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
441 /* Set up to return from userspace. If provided, use a stub
442 already in userspace. */
443 if (ka->sa.sa_flags & SA_RESTORER) {
444 regs->pr = (unsigned long) ka->sa.sa_restorer;
445 } else {
446 /* This is : mov #__NR_rt_sigreturn,r3 ; trapa #0x10 */
447 #ifdef __LITTLE_ENDIAN__
448 unsigned long code = 0xc310e300 | (__NR_rt_sigreturn);
449 #else
450 unsigned long code = 0xe300c310 | (__NR_rt_sigreturn << 16);
451 #endif
453 regs->pr = (unsigned long) frame->retcode;
454 err |= __put_user(code, (long *)(frame->retcode+0));
457 if (err)
458 goto give_sigsegv;
460 /* Set up registers for signal handler */
461 regs->regs[15] = (unsigned long) frame;
462 regs->regs[4] = signal; /* Arg for signal handler */
463 regs->pc = (unsigned long) ka->sa.sa_handler;
465 set_fs(USER_DS);
467 #if DEBUG_SIG
468 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
469 current->comm, current->pid, frame, regs->pc, regs->pr);
470 #endif
472 flush_icache_range(regs->pr, regs->pr+4);
473 return;
475 give_sigsegv:
476 if (sig == SIGSEGV)
477 ka->sa.sa_handler = SIG_DFL;
478 force_sig(SIGSEGV, current);
482 * OK, we're invoking a handler
485 static void
486 handle_signal(unsigned long sig, struct k_sigaction *ka,
487 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
489 /* Are we from a system call? */
490 if (regs->syscall_nr >= 0) {
491 /* If so, check system call restarting.. */
492 switch (regs->regs[0]) {
493 case -ERESTARTNOHAND:
494 regs->regs[0] = -EINTR;
495 break;
497 case -ERESTARTSYS:
498 if (!(ka->sa.sa_flags & SA_RESTART)) {
499 regs->regs[0] = -EINTR;
500 break;
502 /* fallthrough */
503 case -ERESTARTNOINTR:
504 regs->regs[0] = regs->syscall_nr;
505 regs->pc -= 2;
509 /* Set up the stack frame */
510 if (ka->sa.sa_flags & SA_SIGINFO)
511 setup_rt_frame(sig, ka, info, oldset, regs);
512 else
513 setup_frame(sig, ka, oldset, regs);
515 if (ka->sa.sa_flags & SA_ONESHOT)
516 ka->sa.sa_handler = SIG_DFL;
518 if (!(ka->sa.sa_flags & SA_NODEFER)) {
519 spin_lock_irq(&current->sigmask_lock);
520 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
521 sigaddset(&current->blocked,sig);
522 recalc_sigpending(current);
523 spin_unlock_irq(&current->sigmask_lock);
528 * Note that 'init' is a special process: it doesn't get signals it doesn't
529 * want to handle. Thus you cannot kill init even with a SIGKILL even by
530 * mistake.
532 * Note that we go through the signals twice: once to check the signals that
533 * the kernel can handle, and then we build all the user-level signal handling
534 * stack-frames in one go after that.
536 int do_signal(struct pt_regs *regs, sigset_t *oldset)
538 siginfo_t info;
539 struct k_sigaction *ka;
542 * We want the common case to go fast, which
543 * is why we may in certain cases get here from
544 * kernel mode. Just return without doing anything
545 * if so.
547 if (!user_mode(regs))
548 return 1;
550 if (!oldset)
551 oldset = &current->blocked;
553 for (;;) {
554 unsigned long signr;
556 spin_lock_irq(&current->sigmask_lock);
557 signr = dequeue_signal(&current->blocked, &info);
558 spin_unlock_irq(&current->sigmask_lock);
560 if (!signr)
561 break;
563 if ((current->flags & PF_PTRACED) && signr != SIGKILL) {
564 /* Let the debugger run. */
565 current->exit_code = signr;
566 current->state = TASK_STOPPED;
567 notify_parent(current, SIGCHLD);
568 schedule();
570 /* We're back. Did the debugger cancel the sig? */
571 if (!(signr = current->exit_code))
572 continue;
573 current->exit_code = 0;
575 /* The debugger continued. Ignore SIGSTOP. */
576 if (signr == SIGSTOP)
577 continue;
579 /* Update the siginfo structure. Is this good? */
580 if (signr != info.si_signo) {
581 info.si_signo = signr;
582 info.si_errno = 0;
583 info.si_code = SI_USER;
584 info.si_pid = current->p_pptr->pid;
585 info.si_uid = current->p_pptr->uid;
588 /* If the (new) signal is now blocked, requeue it. */
589 if (sigismember(&current->blocked, signr)) {
590 send_sig_info(signr, &info, current);
591 continue;
595 ka = &current->sig->action[signr-1];
596 if (ka->sa.sa_handler == SIG_IGN) {
597 if (signr != SIGCHLD)
598 continue;
599 /* Check for SIGCHLD: it's special. */
600 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
601 /* nothing */;
602 continue;
605 if (ka->sa.sa_handler == SIG_DFL) {
606 int exit_code = signr;
608 /* Init gets no signals it doesn't want. */
609 if (current->pid == 1)
610 continue;
612 switch (signr) {
613 case SIGCONT: case SIGCHLD: case SIGWINCH:
614 continue;
616 case SIGTSTP: case SIGTTIN: case SIGTTOU:
617 if (is_orphaned_pgrp(current->pgrp))
618 continue;
619 /* FALLTHRU */
621 case SIGSTOP:
622 current->state = TASK_STOPPED;
623 current->exit_code = signr;
624 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
625 notify_parent(current, SIGCHLD);
626 schedule();
627 continue;
629 case SIGQUIT: case SIGILL: case SIGTRAP:
630 case SIGABRT: case SIGFPE: case SIGSEGV:
631 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
632 if (do_coredump(signr, regs))
633 exit_code |= 0x80;
634 /* FALLTHRU */
636 default:
637 lock_kernel();
638 sigaddset(&current->signal, signr);
639 recalc_sigpending(current);
640 current->flags |= PF_SIGNALED;
641 do_exit(exit_code);
642 /* NOTREACHED */
646 /* Whee! Actually deliver the signal. */
647 handle_signal(signr, ka, &info, oldset, regs);
648 return 1;
651 /* Did we come from a system call? */
652 if (regs->syscall_nr >= 0) {
653 /* Restart the system call - no handlers present */
654 if (regs->regs[0] == -ERESTARTNOHAND ||
655 regs->regs[0] == -ERESTARTSYS ||
656 regs->regs[0] == -ERESTARTNOINTR) {
657 regs->regs[0] = regs->syscall_nr;
658 regs->pc -= 2;
661 return 0;