Import 2.1.118
[davej-history.git] / arch / i386 / kernel / signal.c
blob231356d909cd1caeb3d5ec32ca4f13e7e3804212
1 /*
2 * linux/arch/i386/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 */
9 #include <linux/config.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <asm/ucontext.h>
23 #include <asm/uaccess.h>
25 #define DEBUG_SIG 0
27 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
29 asmlinkage int sys_wait4(pid_t pid, unsigned long *stat_addr,
30 int options, unsigned long *ru);
31 asmlinkage int do_signal(sigset_t *oldset, struct pt_regs *regs);
34 * Atomically swap in the new signal mask, and wait for a signal.
36 asmlinkage int
37 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
39 struct pt_regs * regs = (struct pt_regs *) &history0;
40 sigset_t saveset;
42 mask &= _BLOCKABLE;
43 spin_lock_irq(&current->sigmask_lock);
44 saveset = current->blocked;
45 siginitset(&current->blocked, mask);
46 recalc_sigpending(current);
47 spin_unlock_irq(&current->sigmask_lock);
49 regs->eax = -EINTR;
50 while (1) {
51 current->state = TASK_INTERRUPTIBLE;
52 schedule();
53 if (do_signal(&saveset, regs))
54 return -EINTR;
58 asmlinkage int
59 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize)
61 struct pt_regs * regs = (struct pt_regs *) &unewset;
62 sigset_t saveset, newset;
64 /* XXX: Don't preclude handling different sized sigset_t's. */
65 if (sigsetsize != sizeof(sigset_t))
66 return -EINVAL;
68 if (copy_from_user(&newset, unewset, sizeof(newset)))
69 return -EFAULT;
70 sigdelsetmask(&newset, ~_BLOCKABLE);
72 spin_lock_irq(&current->sigmask_lock);
73 saveset = current->blocked;
74 current->blocked = newset;
75 recalc_sigpending(current);
76 spin_unlock_irq(&current->sigmask_lock);
78 regs->eax = -EINTR;
79 while (1) {
80 current->state = TASK_INTERRUPTIBLE;
81 schedule();
82 if (do_signal(&saveset, regs))
83 return -EINTR;
87 asmlinkage int
88 sys_sigaction(int sig, const struct old_sigaction *act,
89 struct old_sigaction *oact)
91 struct k_sigaction new_ka, old_ka;
92 int ret;
94 if (act) {
95 old_sigset_t mask;
96 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
97 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
98 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
99 return -EFAULT;
100 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
101 __get_user(mask, &act->sa_mask);
102 siginitset(&new_ka.sa.sa_mask, mask);
105 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
107 if (!ret && oact) {
108 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
109 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
110 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
111 return -EFAULT;
112 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
113 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
116 return ret;
119 asmlinkage int
120 sys_sigaltstack(const stack_t *uss, stack_t *uoss)
122 struct pt_regs *regs = (struct pt_regs *) &uss;
123 return do_sigaltstack(uss, uoss, regs->esp);
128 * Do a signal return; undo the signal stack.
131 struct sigframe
133 char *pretcode;
134 int sig;
135 struct sigcontext sc;
136 struct _fpstate fpstate;
137 unsigned long extramask[_NSIG_WORDS-1];
138 char retcode[8];
141 struct rt_sigframe
143 char *pretcode;
144 int sig;
145 struct siginfo *pinfo;
146 void *puc;
147 struct siginfo info;
148 struct ucontext uc;
149 struct _fpstate fpstate;
150 char retcode[8];
154 static inline int restore_i387_hard(struct _fpstate *buf)
156 if (current->flags & PF_USEDFPU) {
157 current->flags &= ~PF_USEDFPU;
158 stts();
160 return __copy_from_user(&current->tss.i387.hard, buf, sizeof(*buf));
163 static inline int restore_i387(struct _fpstate *buf)
165 int err;
166 #ifndef CONFIG_MATH_EMULATION
167 err = restore_i387_hard(buf);
168 #else
169 if (boot_cpu_data.hard_math)
170 err = restore_i387_hard(buf);
171 else
172 err = restore_i387_soft(&current->tss.i387.soft, buf);
173 #endif
174 current->used_math = 1;
175 return err;
178 static int
179 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, int *peax)
181 unsigned int err = 0;
183 #define COPY(x) err |= __get_user(regs->x, &sc->x)
185 #define COPY_SEG(seg) \
186 { unsigned short tmp; \
187 err |= __get_user(tmp, &sc->seg); \
188 regs->x##seg = tmp; }
190 #define COPY_SEG_STRICT(seg) \
191 { unsigned short tmp; \
192 err |= __get_user(tmp, &sc->seg); \
193 regs->x##seg = tmp|3; }
195 #define GET_SEG(seg) \
196 { unsigned short tmp; \
197 err |= __get_user(tmp, &sc->seg); \
198 loadsegment(seg,tmp); }
200 GET_SEG(gs);
201 GET_SEG(fs);
202 COPY_SEG(es);
203 COPY_SEG(ds);
204 COPY(edi);
205 COPY(esi);
206 COPY(ebp);
207 COPY(esp);
208 COPY(ebx);
209 COPY(edx);
210 COPY(ecx);
211 COPY(eip);
212 COPY_SEG_STRICT(cs);
213 COPY_SEG_STRICT(ss);
216 unsigned int tmpflags;
217 err |= __get_user(tmpflags, &sc->eflags);
218 regs->eflags = (regs->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
219 regs->orig_eax = -1; /* disable syscall checks */
223 struct _fpstate * buf;
224 err |= __get_user(buf, &sc->fpstate);
225 if (buf) {
226 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
227 goto badframe;
228 err |= restore_i387(buf);
232 err |= __get_user(*peax, &sc->eax);
233 return err;
235 badframe:
236 return 1;
239 asmlinkage int sys_sigreturn(unsigned long __unused)
241 struct pt_regs *regs = (struct pt_regs *) &__unused;
242 struct sigframe *frame = (struct sigframe *)(regs->esp - 8);
243 sigset_t set;
244 int eax;
246 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
247 goto badframe;
248 if (__get_user(set.sig[0], &frame->sc.oldmask)
249 || (_NSIG_WORDS > 1
250 && __copy_from_user(&set.sig[1], &frame->extramask,
251 sizeof(frame->extramask))))
252 goto badframe;
254 sigdelsetmask(&set, ~_BLOCKABLE);
255 spin_lock_irq(&current->sigmask_lock);
256 current->blocked = set;
257 recalc_sigpending(current);
258 spin_unlock_irq(&current->sigmask_lock);
260 if (restore_sigcontext(regs, &frame->sc, &eax))
261 goto badframe;
262 return eax;
264 badframe:
265 force_sig(SIGSEGV, current);
266 return 0;
269 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
271 struct pt_regs *regs = (struct pt_regs *) &__unused;
272 struct rt_sigframe *frame = (struct rt_sigframe *)(regs->esp - 4);
273 sigset_t set;
274 stack_t st;
275 int eax;
277 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
278 goto badframe;
279 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
280 goto badframe;
282 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->uc.uc_mcontext, &eax))
289 goto badframe;
291 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
292 goto badframe;
293 /* It is more difficult to avoid calling this function than to
294 call it and ignore errors. */
295 do_sigaltstack(&st, NULL, regs->esp);
297 return eax;
299 badframe:
300 force_sig(SIGSEGV, current);
301 return 0;
305 * Set up a signal frame.
308 static inline int save_i387_hard(struct _fpstate * buf)
310 if (current->flags & PF_USEDFPU) {
311 current->flags &= ~PF_USEDFPU;
312 __asm__ __volatile__("fnsave %0":"=m"(current->tss.i387.hard));
313 stts();
315 asm volatile("fwait");
316 current->tss.i387.hard.status = current->tss.i387.hard.swd;
317 if (__copy_to_user(buf, &current->tss.i387.hard, sizeof(*buf)))
318 return -1;
319 return 1;
322 static int save_i387(struct _fpstate *buf)
324 if (!current->used_math)
325 return 0;
327 /* This will cause a "finit" to be triggered by the next
328 attempted FPU operation by the 'current' process.
330 current->used_math = 0;
332 #ifndef CONFIG_MATH_EMULATION
333 return save_i387_hard(buf);
334 #else
335 return boot_cpu_data.hard_math ? save_i387_hard(buf)
336 : save_i387_soft(&current->tss.i387.soft, buf);
337 #endif
340 static int
341 setup_sigcontext(struct sigcontext *sc, struct _fpstate *fpstate,
342 struct pt_regs *regs, unsigned long mask)
344 int tmp, err = 0;
346 tmp = 0;
347 __asm__("movl %%gs,%w0" : "=r"(tmp): "0"(tmp));
348 err |= __put_user(tmp, (unsigned int *)&sc->gs);
349 __asm__("movl %%fs,%w0" : "=r"(tmp): "0"(tmp));
350 err |= __put_user(tmp, (unsigned int *)&sc->fs);
352 err |= __put_user(regs->xes, (unsigned int *)&sc->es);
353 err |= __put_user(regs->xds, (unsigned int *)&sc->ds);
354 err |= __put_user(regs->edi, &sc->edi);
355 err |= __put_user(regs->esi, &sc->esi);
356 err |= __put_user(regs->ebp, &sc->ebp);
357 err |= __put_user(regs->esp, &sc->esp);
358 err |= __put_user(regs->ebx, &sc->ebx);
359 err |= __put_user(regs->edx, &sc->edx);
360 err |= __put_user(regs->ecx, &sc->ecx);
361 err |= __put_user(regs->eax, &sc->eax);
362 err |= __put_user(current->tss.trap_no, &sc->trapno);
363 err |= __put_user(current->tss.error_code, &sc->err);
364 err |= __put_user(regs->eip, &sc->eip);
365 err |= __put_user(regs->xcs, (unsigned int *)&sc->cs);
366 err |= __put_user(regs->eflags, &sc->eflags);
367 err |= __put_user(regs->esp, &sc->esp_at_signal);
368 err |= __put_user(regs->xss, (unsigned int *)&sc->ss);
370 tmp = save_i387(fpstate);
371 if (tmp < 0)
372 err = 1;
373 else
374 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
376 /* non-iBCS2 extensions.. */
377 err |= __put_user(mask, &sc->oldmask);
378 err |= __put_user(current->tss.cr2, &sc->cr2);
380 return err;
384 * Determine which stack to use..
386 static inline void *
387 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
389 unsigned long esp;
391 /* Default to using normal stack */
392 esp = regs->esp;
394 /* This is the X/Open sanctioned signal stack switching. */
395 if (ka->sa.sa_flags & SA_ONSTACK) {
396 if (! on_sig_stack(esp))
397 esp = current->sas_ss_sp + current->sas_ss_size;
400 /* This is the legacy signal stack switching. */
401 else if ((regs->xss & 0xffff) != __USER_DS &&
402 !(ka->sa.sa_flags & SA_RESTORER) &&
403 ka->sa.sa_restorer) {
404 esp = (unsigned long) ka->sa.sa_restorer;
407 return (void *)((esp - frame_size) & -8ul);
410 static void setup_frame(int sig, struct k_sigaction *ka,
411 sigset_t *set, struct pt_regs * regs)
413 struct sigframe *frame;
414 int err = 0;
416 frame = get_sigframe(ka, regs, sizeof(*frame));
418 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
419 goto give_sigsegv;
421 err |= __put_user((current->exec_domain
422 && current->exec_domain->signal_invmap
423 && sig < 32
424 ? current->exec_domain->signal_invmap[sig]
425 : sig),
426 &frame->sig);
428 err |= setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
430 if (_NSIG_WORDS > 1) {
431 err |= __copy_to_user(frame->extramask, &set->sig[1],
432 sizeof(frame->extramask));
435 /* Set up to return from userspace. If provided, use a stub
436 already in userspace. */
437 if (ka->sa.sa_flags & SA_RESTORER) {
438 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
439 } else {
440 err |= __put_user(frame->retcode, &frame->pretcode);
441 /* This is popl %eax ; movl $,%eax ; int $0x80 */
442 err |= __put_user(0xb858, (short *)(frame->retcode+0));
443 err |= __put_user(__NR_sigreturn, (int *)(frame->retcode+2));
444 err |= __put_user(0x80cd, (short *)(frame->retcode+6));
447 if (err)
448 goto give_sigsegv;
450 /* Set up registers for signal handler */
451 regs->esp = (unsigned long) frame;
452 regs->eip = (unsigned long) ka->sa.sa_handler;
454 set_fs(USER_DS);
455 regs->xds = __USER_DS;
456 regs->xes = __USER_DS;
457 regs->xss = __USER_DS;
458 regs->xcs = __USER_CS;
459 regs->eflags &= ~TF_MASK;
461 #if DEBUG_SIG
462 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
463 current->comm, current->pid, frame, regs->eip, frame->pretcode);
464 #endif
466 return;
468 give_sigsegv:
469 if (sig == SIGSEGV)
470 ka->sa.sa_handler = SIG_DFL;
471 force_sig(SIGSEGV, current);
474 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
475 sigset_t *set, struct pt_regs * regs)
477 struct rt_sigframe *frame;
478 int err = 0;
480 frame = get_sigframe(ka, regs, sizeof(*frame));
482 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
483 goto give_sigsegv;
485 err |= __put_user((current->exec_domain
486 && current->exec_domain->signal_invmap
487 && sig < 32
488 ? current->exec_domain->signal_invmap[sig]
489 : sig),
490 &frame->sig);
491 err |= __put_user(&frame->info, &frame->pinfo);
492 err |= __put_user(&frame->uc, &frame->puc);
493 err |= __copy_to_user(&frame->info, info, sizeof(*info));
495 /* Create the ucontext. */
496 err |= __put_user(0, &frame->uc.uc_flags);
497 err |= __put_user(0, &frame->uc.uc_link);
498 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
499 err |= __put_user(sas_ss_flags(regs->esp),
500 &frame->uc.uc_stack.ss_flags);
501 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
502 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
503 regs, set->sig[0]);
504 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
506 /* Set up to return from userspace. If provided, use a stub
507 already in userspace. */
508 if (ka->sa.sa_flags & SA_RESTORER) {
509 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
510 } else {
511 err |= __put_user(frame->retcode, &frame->pretcode);
512 /* This is movl $,%eax ; int $0x80 */
513 err |= __put_user(0xb8, (char *)(frame->retcode+0));
514 err |= __put_user(__NR_rt_sigreturn, (int *)(frame->retcode+1));
515 err |= __put_user(0x80cd, (short *)(frame->retcode+5));
518 if (err)
519 goto give_sigsegv;
521 /* Set up registers for signal handler */
522 regs->esp = (unsigned long) frame;
523 regs->eip = (unsigned long) ka->sa.sa_handler;
525 set_fs(USER_DS);
526 regs->xds = __USER_DS;
527 regs->xes = __USER_DS;
528 regs->xss = __USER_DS;
529 regs->xcs = __USER_CS;
530 regs->eflags &= ~TF_MASK;
532 #if DEBUG_SIG
533 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
534 current->comm, current->pid, frame, regs->eip, frame->pretcode);
535 #endif
537 return;
539 give_sigsegv:
540 if (sig == SIGSEGV)
541 ka->sa.sa_handler = SIG_DFL;
542 force_sig(SIGSEGV, current);
546 * OK, we're invoking a handler
549 static void
550 handle_signal(unsigned long sig, struct k_sigaction *ka,
551 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
553 /* Are we from a system call? */
554 if (regs->orig_eax >= 0) {
555 /* If so, check system call restarting.. */
556 switch (regs->eax) {
557 case -ERESTARTNOHAND:
558 regs->eax = -EINTR;
559 break;
561 case -ERESTARTSYS:
562 if (!(ka->sa.sa_flags & SA_RESTART)) {
563 regs->eax = -EINTR;
564 break;
566 /* fallthrough */
567 case -ERESTARTNOINTR:
568 regs->eax = regs->orig_eax;
569 regs->eip -= 2;
573 /* Set up the stack frame */
574 if (ka->sa.sa_flags & SA_SIGINFO)
575 setup_rt_frame(sig, ka, info, oldset, regs);
576 else
577 setup_frame(sig, ka, oldset, regs);
579 if (ka->sa.sa_flags & SA_ONESHOT)
580 ka->sa.sa_handler = SIG_DFL;
582 if (!(ka->sa.sa_flags & SA_NODEFER)) {
583 spin_lock_irq(&current->sigmask_lock);
584 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
585 sigaddset(&current->blocked,sig);
586 recalc_sigpending(current);
587 spin_unlock_irq(&current->sigmask_lock);
592 * Note that 'init' is a special process: it doesn't get signals it doesn't
593 * want to handle. Thus you cannot kill init even with a SIGKILL even by
594 * mistake.
596 * Note that we go through the signals twice: once to check the signals that
597 * the kernel can handle, and then we build all the user-level signal handling
598 * stack-frames in one go after that.
600 asmlinkage int do_signal(sigset_t *oldset, struct pt_regs *regs)
602 siginfo_t info;
603 struct k_sigaction *ka;
606 * We want the common case to go fast, which
607 * is why we may in certain cases get here from
608 * kernel mode. Just return without doing anything
609 * if so.
611 if ((regs->xcs & 3) != 3)
612 return 1;
614 if (!oldset)
615 oldset = &current->blocked;
617 for (;;) {
618 unsigned long signr;
620 spin_lock_irq(&current->sigmask_lock);
621 signr = dequeue_signal(&current->blocked, &info);
622 spin_unlock_irq(&current->sigmask_lock);
624 if (!signr)
625 break;
627 if ((current->flags & PF_PTRACED) && signr != SIGKILL) {
628 /* Let the debugger run. */
629 current->exit_code = signr;
630 current->state = TASK_STOPPED;
631 notify_parent(current, SIGCHLD);
632 schedule();
634 /* We're back. Did the debugger cancel the sig? */
635 if (!(signr = current->exit_code))
636 continue;
637 current->exit_code = 0;
639 /* The debugger continued. Ignore SIGSTOP. */
640 if (signr == SIGSTOP)
641 continue;
643 /* Update the siginfo structure. Is this good? */
644 if (signr != info.si_signo) {
645 info.si_signo = signr;
646 info.si_errno = 0;
647 info.si_code = SI_USER;
648 info.si_pid = current->p_pptr->pid;
649 info.si_uid = current->p_pptr->uid;
652 /* If the (new) signal is now blocked, requeue it. */
653 if (sigismember(&current->blocked, signr)) {
654 send_sig_info(signr, &info, current);
655 continue;
659 ka = &current->sig->action[signr-1];
660 if (ka->sa.sa_handler == SIG_IGN) {
661 if (signr != SIGCHLD)
662 continue;
663 /* Check for SIGCHLD: it's special. */
664 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
665 /* nothing */;
666 continue;
669 if (ka->sa.sa_handler == SIG_DFL) {
670 int exit_code = signr;
672 /* Init gets no signals it doesn't want. */
673 if (current->pid == 1)
674 continue;
676 switch (signr) {
677 case SIGCONT: case SIGCHLD: case SIGWINCH:
678 continue;
680 case SIGTSTP: case SIGTTIN: case SIGTTOU:
681 if (is_orphaned_pgrp(current->pgrp))
682 continue;
683 /* FALLTHRU */
685 case SIGSTOP:
686 current->state = TASK_STOPPED;
687 current->exit_code = signr;
688 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
689 notify_parent(current, SIGCHLD);
690 schedule();
691 continue;
693 case SIGQUIT: case SIGILL: case SIGTRAP:
694 case SIGABRT: case SIGFPE: case SIGSEGV:
695 lock_kernel();
696 if (current->binfmt
697 && current->binfmt->core_dump
698 && current->binfmt->core_dump(signr, regs))
699 exit_code |= 0x80;
700 unlock_kernel();
701 /* FALLTHRU */
703 default:
704 lock_kernel();
705 sigaddset(&current->signal, signr);
706 current->flags |= PF_SIGNALED;
707 do_exit(exit_code);
708 /* NOTREACHED */
712 /* Whee! Actually deliver the signal. */
713 handle_signal(signr, ka, &info, oldset, regs);
714 return 1;
717 /* Did we come from a system call? */
718 if (regs->orig_eax >= 0) {
719 /* Restart the system call - no handlers present */
720 if (regs->eax == -ERESTARTNOHAND ||
721 regs->eax == -ERESTARTSYS ||
722 regs->eax == -ERESTARTNOINTR) {
723 regs->eax = regs->orig_eax;
724 regs->eip -= 2;
727 return 0;