Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / x86 / kernel / signal_32.c
blob3bcff818f9a7617a75c834bf71aa475398c63fd0
1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
4 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
5 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
6 */
8 #include <linux/sched.h>
9 #include <linux/mm.h>
10 #include <linux/smp.h>
11 #include <linux/kernel.h>
12 #include <linux/signal.h>
13 #include <linux/errno.h>
14 #include <linux/wait.h>
15 #include <linux/unistd.h>
16 #include <linux/stddef.h>
17 #include <linux/personality.h>
18 #include <linux/suspend.h>
19 #include <linux/ptrace.h>
20 #include <linux/elf.h>
21 #include <linux/binfmts.h>
22 #include <asm/processor.h>
23 #include <asm/ucontext.h>
24 #include <asm/uaccess.h>
25 #include <asm/i387.h>
26 #include <asm/vdso.h>
27 #include "sigframe_32.h"
29 #define DEBUG_SIG 0
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
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 mask &= _BLOCKABLE;
40 spin_lock_irq(&current->sighand->siglock);
41 current->saved_sigmask = current->blocked;
42 siginitset(&current->blocked, mask);
43 recalc_sigpending();
44 spin_unlock_irq(&current->sighand->siglock);
46 current->state = TASK_INTERRUPTIBLE;
47 schedule();
48 set_thread_flag(TIF_RESTORE_SIGMASK);
49 return -ERESTARTNOHAND;
52 asmlinkage int
53 sys_sigaction(int sig, const struct old_sigaction __user *act,
54 struct old_sigaction __user *oact)
56 struct k_sigaction new_ka, old_ka;
57 int ret;
59 if (act) {
60 old_sigset_t mask;
61 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
62 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
63 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
64 return -EFAULT;
65 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
66 __get_user(mask, &act->sa_mask);
67 siginitset(&new_ka.sa.sa_mask, mask);
70 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
72 if (!ret && oact) {
73 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
74 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
75 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
76 return -EFAULT;
77 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
78 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
81 return ret;
84 asmlinkage int
85 sys_sigaltstack(unsigned long bx)
87 /* This is needed to make gcc realize it doesn't own the "struct pt_regs" */
88 struct pt_regs *regs = (struct pt_regs *)&bx;
89 const stack_t __user *uss = (const stack_t __user *)bx;
90 stack_t __user *uoss = (stack_t __user *)regs->cx;
92 return do_sigaltstack(uss, uoss, regs->sp);
97 * Do a signal return; undo the signal stack.
100 static int
101 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *peax)
103 unsigned int err = 0;
105 /* Always make any pending restarted system calls return -EINTR */
106 current_thread_info()->restart_block.fn = do_no_restart_syscall;
108 #define COPY(x) err |= __get_user(regs->x, &sc->x)
110 #define COPY_SEG(seg) \
111 { unsigned short tmp; \
112 err |= __get_user(tmp, &sc->seg); \
113 regs->seg = tmp; }
115 #define COPY_SEG_STRICT(seg) \
116 { unsigned short tmp; \
117 err |= __get_user(tmp, &sc->seg); \
118 regs->seg = tmp|3; }
120 #define GET_SEG(seg) \
121 { unsigned short tmp; \
122 err |= __get_user(tmp, &sc->seg); \
123 loadsegment(seg,tmp); }
125 #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_RF | \
126 X86_EFLAGS_OF | X86_EFLAGS_DF | \
127 X86_EFLAGS_TF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \
128 X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF)
130 GET_SEG(gs);
131 COPY_SEG(fs);
132 COPY_SEG(es);
133 COPY_SEG(ds);
134 COPY(di);
135 COPY(si);
136 COPY(bp);
137 COPY(sp);
138 COPY(bx);
139 COPY(dx);
140 COPY(cx);
141 COPY(ip);
142 COPY_SEG_STRICT(cs);
143 COPY_SEG_STRICT(ss);
146 unsigned int tmpflags;
147 err |= __get_user(tmpflags, &sc->flags);
148 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
149 regs->orig_ax = -1; /* disable syscall checks */
153 struct _fpstate __user * buf;
154 err |= __get_user(buf, &sc->fpstate);
155 if (buf) {
156 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
157 goto badframe;
158 err |= restore_i387(buf);
159 } else {
160 struct task_struct *me = current;
161 if (used_math()) {
162 clear_fpu(me);
163 clear_used_math();
168 err |= __get_user(*peax, &sc->ax);
169 return err;
171 badframe:
172 return 1;
175 asmlinkage int sys_sigreturn(unsigned long __unused)
177 struct pt_regs *regs = (struct pt_regs *) &__unused;
178 struct sigframe __user *frame = (struct sigframe __user *)(regs->sp - 8);
179 sigset_t set;
180 int ax;
182 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
183 goto badframe;
184 if (__get_user(set.sig[0], &frame->sc.oldmask)
185 || (_NSIG_WORDS > 1
186 && __copy_from_user(&set.sig[1], &frame->extramask,
187 sizeof(frame->extramask))))
188 goto badframe;
190 sigdelsetmask(&set, ~_BLOCKABLE);
191 spin_lock_irq(&current->sighand->siglock);
192 current->blocked = set;
193 recalc_sigpending();
194 spin_unlock_irq(&current->sighand->siglock);
196 if (restore_sigcontext(regs, &frame->sc, &ax))
197 goto badframe;
198 return ax;
200 badframe:
201 if (show_unhandled_signals && printk_ratelimit()) {
202 printk("%s%s[%d] bad frame in sigreturn frame:%p ip:%lx"
203 " sp:%lx oeax:%lx",
204 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
205 current->comm, task_pid_nr(current), frame, regs->ip,
206 regs->sp, regs->orig_ax);
207 print_vma_addr(" in ", regs->ip);
208 printk("\n");
211 force_sig(SIGSEGV, current);
212 return 0;
215 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
217 struct pt_regs *regs = (struct pt_regs *) &__unused;
218 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->sp - 4);
219 sigset_t set;
220 int ax;
222 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
223 goto badframe;
224 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
225 goto badframe;
227 sigdelsetmask(&set, ~_BLOCKABLE);
228 spin_lock_irq(&current->sighand->siglock);
229 current->blocked = set;
230 recalc_sigpending();
231 spin_unlock_irq(&current->sighand->siglock);
233 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
234 goto badframe;
236 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
237 goto badframe;
239 return ax;
241 badframe:
242 force_sig(SIGSEGV, current);
243 return 0;
247 * Set up a signal frame.
250 static int
251 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
252 struct pt_regs *regs, unsigned long mask)
254 int tmp, err = 0;
256 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
257 savesegment(gs, tmp);
258 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
260 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
261 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
262 err |= __put_user(regs->di, &sc->di);
263 err |= __put_user(regs->si, &sc->si);
264 err |= __put_user(regs->bp, &sc->bp);
265 err |= __put_user(regs->sp, &sc->sp);
266 err |= __put_user(regs->bx, &sc->bx);
267 err |= __put_user(regs->dx, &sc->dx);
268 err |= __put_user(regs->cx, &sc->cx);
269 err |= __put_user(regs->ax, &sc->ax);
270 err |= __put_user(current->thread.trap_no, &sc->trapno);
271 err |= __put_user(current->thread.error_code, &sc->err);
272 err |= __put_user(regs->ip, &sc->ip);
273 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
274 err |= __put_user(regs->flags, &sc->flags);
275 err |= __put_user(regs->sp, &sc->sp_at_signal);
276 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
278 tmp = save_i387(fpstate);
279 if (tmp < 0)
280 err = 1;
281 else
282 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
284 /* non-iBCS2 extensions.. */
285 err |= __put_user(mask, &sc->oldmask);
286 err |= __put_user(current->thread.cr2, &sc->cr2);
288 return err;
292 * Determine which stack to use..
294 static inline void __user *
295 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
297 unsigned long sp;
299 /* Default to using normal stack */
300 sp = regs->sp;
303 * If we are on the alternate signal stack and would overflow it, don't.
304 * Return an always-bogus address instead so we will die with SIGSEGV.
306 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
307 return (void __user *) -1L;
309 /* This is the X/Open sanctioned signal stack switching. */
310 if (ka->sa.sa_flags & SA_ONSTACK) {
311 if (sas_ss_flags(sp) == 0)
312 sp = current->sas_ss_sp + current->sas_ss_size;
315 /* This is the legacy signal stack switching. */
316 else if ((regs->ss & 0xffff) != __USER_DS &&
317 !(ka->sa.sa_flags & SA_RESTORER) &&
318 ka->sa.sa_restorer) {
319 sp = (unsigned long) ka->sa.sa_restorer;
322 sp -= frame_size;
323 /* Align the stack pointer according to the i386 ABI,
324 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
325 sp = ((sp + 4) & -16ul) - 4;
326 return (void __user *) sp;
329 /* These symbols are defined with the addresses in the vsyscall page.
330 See vsyscall-sigreturn.S. */
331 extern void __user __kernel_sigreturn;
332 extern void __user __kernel_rt_sigreturn;
334 static int setup_frame(int sig, struct k_sigaction *ka,
335 sigset_t *set, struct pt_regs * regs)
337 void __user *restorer;
338 struct sigframe __user *frame;
339 int err = 0;
340 int usig;
342 frame = get_sigframe(ka, regs, sizeof(*frame));
344 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
345 goto give_sigsegv;
347 usig = current_thread_info()->exec_domain
348 && current_thread_info()->exec_domain->signal_invmap
349 && sig < 32
350 ? current_thread_info()->exec_domain->signal_invmap[sig]
351 : sig;
353 err = __put_user(usig, &frame->sig);
354 if (err)
355 goto give_sigsegv;
357 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
358 if (err)
359 goto give_sigsegv;
361 if (_NSIG_WORDS > 1) {
362 err = __copy_to_user(&frame->extramask, &set->sig[1],
363 sizeof(frame->extramask));
364 if (err)
365 goto give_sigsegv;
368 if (current->binfmt->hasvdso)
369 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
370 else
371 restorer = &frame->retcode;
372 if (ka->sa.sa_flags & SA_RESTORER)
373 restorer = ka->sa.sa_restorer;
375 /* Set up to return from userspace. */
376 err |= __put_user(restorer, &frame->pretcode);
379 * This is popl %eax ; movl $,%eax ; int $0x80
381 * WE DO NOT USE IT ANY MORE! It's only left here for historical
382 * reasons and because gdb uses it as a signature to notice
383 * signal handler stack frames.
385 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
386 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
387 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
389 if (err)
390 goto give_sigsegv;
392 /* Set up registers for signal handler */
393 regs->sp = (unsigned long) frame;
394 regs->ip = (unsigned long) ka->sa.sa_handler;
395 regs->ax = (unsigned long) sig;
396 regs->dx = (unsigned long) 0;
397 regs->cx = (unsigned long) 0;
399 regs->ds = __USER_DS;
400 regs->es = __USER_DS;
401 regs->ss = __USER_DS;
402 regs->cs = __USER_CS;
405 * Clear TF when entering the signal handler, but
406 * notify any tracer that was single-stepping it.
407 * The tracer may want to single-step inside the
408 * handler too.
410 <<<<<<< HEAD:arch/x86/kernel/signal_32.c
411 regs->flags &= ~TF_MASK;
412 =======
413 regs->flags &= ~(TF_MASK | X86_EFLAGS_DF);
414 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/signal_32.c
415 if (test_thread_flag(TIF_SINGLESTEP))
416 ptrace_notify(SIGTRAP);
418 #if DEBUG_SIG
419 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
420 current->comm, current->pid, frame, regs->ip, frame->pretcode);
421 #endif
423 return 0;
425 give_sigsegv:
426 force_sigsegv(sig, current);
427 return -EFAULT;
430 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
431 sigset_t *set, struct pt_regs * regs)
433 void __user *restorer;
434 struct rt_sigframe __user *frame;
435 int err = 0;
436 int usig;
438 frame = get_sigframe(ka, regs, sizeof(*frame));
440 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
441 goto give_sigsegv;
443 usig = current_thread_info()->exec_domain
444 && current_thread_info()->exec_domain->signal_invmap
445 && sig < 32
446 ? current_thread_info()->exec_domain->signal_invmap[sig]
447 : sig;
449 err |= __put_user(usig, &frame->sig);
450 err |= __put_user(&frame->info, &frame->pinfo);
451 err |= __put_user(&frame->uc, &frame->puc);
452 err |= copy_siginfo_to_user(&frame->info, info);
453 if (err)
454 goto give_sigsegv;
456 /* Create the ucontext. */
457 err |= __put_user(0, &frame->uc.uc_flags);
458 err |= __put_user(0, &frame->uc.uc_link);
459 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
460 err |= __put_user(sas_ss_flags(regs->sp),
461 &frame->uc.uc_stack.ss_flags);
462 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
463 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
464 regs, set->sig[0]);
465 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
466 if (err)
467 goto give_sigsegv;
469 /* Set up to return from userspace. */
470 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
471 if (ka->sa.sa_flags & SA_RESTORER)
472 restorer = ka->sa.sa_restorer;
473 err |= __put_user(restorer, &frame->pretcode);
476 * This is movl $,%ax ; int $0x80
478 * WE DO NOT USE IT ANY MORE! It's only left here for historical
479 * reasons and because gdb uses it as a signature to notice
480 * signal handler stack frames.
482 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
483 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
484 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
486 if (err)
487 goto give_sigsegv;
489 /* Set up registers for signal handler */
490 regs->sp = (unsigned long) frame;
491 regs->ip = (unsigned long) ka->sa.sa_handler;
492 regs->ax = (unsigned long) usig;
493 regs->dx = (unsigned long) &frame->info;
494 regs->cx = (unsigned long) &frame->uc;
496 regs->ds = __USER_DS;
497 regs->es = __USER_DS;
498 regs->ss = __USER_DS;
499 regs->cs = __USER_CS;
502 * Clear TF when entering the signal handler, but
503 * notify any tracer that was single-stepping it.
504 * The tracer may want to single-step inside the
505 * handler too.
507 <<<<<<< HEAD:arch/x86/kernel/signal_32.c
508 regs->flags &= ~TF_MASK;
509 =======
510 regs->flags &= ~(TF_MASK | X86_EFLAGS_DF);
511 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/signal_32.c
512 if (test_thread_flag(TIF_SINGLESTEP))
513 ptrace_notify(SIGTRAP);
515 #if DEBUG_SIG
516 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
517 current->comm, current->pid, frame, regs->ip, frame->pretcode);
518 #endif
520 return 0;
522 give_sigsegv:
523 force_sigsegv(sig, current);
524 return -EFAULT;
528 * OK, we're invoking a handler
531 static int
532 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
533 sigset_t *oldset, struct pt_regs * regs)
535 int ret;
537 /* Are we from a system call? */
538 if (regs->orig_ax >= 0) {
539 /* If so, check system call restarting.. */
540 switch (regs->ax) {
541 case -ERESTART_RESTARTBLOCK:
542 case -ERESTARTNOHAND:
543 regs->ax = -EINTR;
544 break;
546 case -ERESTARTSYS:
547 if (!(ka->sa.sa_flags & SA_RESTART)) {
548 regs->ax = -EINTR;
549 break;
551 /* fallthrough */
552 case -ERESTARTNOINTR:
553 regs->ax = regs->orig_ax;
554 regs->ip -= 2;
559 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
560 * flag so that register information in the sigcontext is correct.
562 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
563 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
564 regs->flags &= ~X86_EFLAGS_TF;
566 /* Set up the stack frame */
567 if (ka->sa.sa_flags & SA_SIGINFO)
568 ret = setup_rt_frame(sig, ka, info, oldset, regs);
569 else
570 ret = setup_frame(sig, ka, oldset, regs);
572 if (ret == 0) {
573 spin_lock_irq(&current->sighand->siglock);
574 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
575 if (!(ka->sa.sa_flags & SA_NODEFER))
576 sigaddset(&current->blocked,sig);
577 recalc_sigpending();
578 spin_unlock_irq(&current->sighand->siglock);
581 return ret;
585 * Note that 'init' is a special process: it doesn't get signals it doesn't
586 * want to handle. Thus you cannot kill init even with a SIGKILL even by
587 * mistake.
589 static void do_signal(struct pt_regs *regs)
591 siginfo_t info;
592 int signr;
593 struct k_sigaction ka;
594 sigset_t *oldset;
597 * We want the common case to go fast, which
598 * is why we may in certain cases get here from
599 * kernel mode. Just return without doing anything
600 * if so. vm86 regs switched out by assembly code
601 * before reaching here, so testing against kernel
602 * CS suffices.
604 if (!user_mode(regs))
605 return;
607 if (test_thread_flag(TIF_RESTORE_SIGMASK))
608 oldset = &current->saved_sigmask;
609 else
610 oldset = &current->blocked;
612 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
613 if (signr > 0) {
614 /* Re-enable any watchpoints before delivering the
615 * signal to user space. The processor register will
616 * have been cleared if the watchpoint triggered
617 * inside the kernel.
619 if (unlikely(current->thread.debugreg7))
620 set_debugreg(current->thread.debugreg7, 7);
622 /* Whee! Actually deliver the signal. */
623 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
624 /* a signal was successfully delivered; the saved
625 * sigmask will have been stored in the signal frame,
626 * and will be restored by sigreturn, so we can simply
627 * clear the TIF_RESTORE_SIGMASK flag */
628 if (test_thread_flag(TIF_RESTORE_SIGMASK))
629 clear_thread_flag(TIF_RESTORE_SIGMASK);
632 return;
635 /* Did we come from a system call? */
636 if (regs->orig_ax >= 0) {
637 /* Restart the system call - no handlers present */
638 switch (regs->ax) {
639 case -ERESTARTNOHAND:
640 case -ERESTARTSYS:
641 case -ERESTARTNOINTR:
642 regs->ax = regs->orig_ax;
643 regs->ip -= 2;
644 break;
646 case -ERESTART_RESTARTBLOCK:
647 regs->ax = __NR_restart_syscall;
648 regs->ip -= 2;
649 break;
653 /* if there's no signal to deliver, we just put the saved sigmask
654 * back */
655 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
656 clear_thread_flag(TIF_RESTORE_SIGMASK);
657 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
662 * notification of userspace execution resumption
663 * - triggered by the TIF_WORK_MASK flags
665 __attribute__((regparm(3)))
666 void do_notify_resume(struct pt_regs *regs, void *_unused,
667 __u32 thread_info_flags)
669 /* Pending single-step? */
670 if (thread_info_flags & _TIF_SINGLESTEP) {
671 regs->flags |= TF_MASK;
672 clear_thread_flag(TIF_SINGLESTEP);
675 /* deal with pending signal delivery */
676 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
677 do_signal(regs);
679 if (thread_info_flags & _TIF_HRTICK_RESCHED)
680 hrtick_resched();
682 clear_thread_flag(TIF_IRET);