x86: rename the struct pt_regs members for 32/64-bit consistency
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / signal_32.c
blob40fd3515ccf1b774c8471839b3d403ced5637a7e
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->e ## 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->eflags);
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->eax);
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\n",
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);
208 force_sig(SIGSEGV, current);
209 return 0;
212 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
214 struct pt_regs *regs = (struct pt_regs *) &__unused;
215 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->sp - 4);
216 sigset_t set;
217 int ax;
219 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
220 goto badframe;
221 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
222 goto badframe;
224 sigdelsetmask(&set, ~_BLOCKABLE);
225 spin_lock_irq(&current->sighand->siglock);
226 current->blocked = set;
227 recalc_sigpending();
228 spin_unlock_irq(&current->sighand->siglock);
230 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
231 goto badframe;
233 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
234 goto badframe;
236 return ax;
238 badframe:
239 force_sig(SIGSEGV, current);
240 return 0;
244 * Set up a signal frame.
247 static int
248 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
249 struct pt_regs *regs, unsigned long mask)
251 int tmp, err = 0;
253 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
254 savesegment(gs, tmp);
255 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
257 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
258 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
259 err |= __put_user(regs->di, &sc->edi);
260 err |= __put_user(regs->si, &sc->esi);
261 err |= __put_user(regs->bp, &sc->ebp);
262 err |= __put_user(regs->sp, &sc->esp);
263 err |= __put_user(regs->bx, &sc->ebx);
264 err |= __put_user(regs->dx, &sc->edx);
265 err |= __put_user(regs->cx, &sc->ecx);
266 err |= __put_user(regs->ax, &sc->eax);
267 err |= __put_user(current->thread.trap_no, &sc->trapno);
268 err |= __put_user(current->thread.error_code, &sc->err);
269 err |= __put_user(regs->ip, &sc->eip);
270 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
271 err |= __put_user(regs->flags, &sc->eflags);
272 err |= __put_user(regs->sp, &sc->esp_at_signal);
273 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
275 tmp = save_i387(fpstate);
276 if (tmp < 0)
277 err = 1;
278 else
279 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
281 /* non-iBCS2 extensions.. */
282 err |= __put_user(mask, &sc->oldmask);
283 err |= __put_user(current->thread.cr2, &sc->cr2);
285 return err;
289 * Determine which stack to use..
291 static inline void __user *
292 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
294 unsigned long sp;
296 /* Default to using normal stack */
297 sp = regs->sp;
300 * If we are on the alternate signal stack and would overflow it, don't.
301 * Return an always-bogus address instead so we will die with SIGSEGV.
303 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
304 return (void __user *) -1L;
306 /* This is the X/Open sanctioned signal stack switching. */
307 if (ka->sa.sa_flags & SA_ONSTACK) {
308 if (sas_ss_flags(sp) == 0)
309 sp = current->sas_ss_sp + current->sas_ss_size;
312 /* This is the legacy signal stack switching. */
313 else if ((regs->ss & 0xffff) != __USER_DS &&
314 !(ka->sa.sa_flags & SA_RESTORER) &&
315 ka->sa.sa_restorer) {
316 sp = (unsigned long) ka->sa.sa_restorer;
319 sp -= frame_size;
320 /* Align the stack pointer according to the i386 ABI,
321 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
322 sp = ((sp + 4) & -16ul) - 4;
323 return (void __user *) sp;
326 /* These symbols are defined with the addresses in the vsyscall page.
327 See vsyscall-sigreturn.S. */
328 extern void __user __kernel_sigreturn;
329 extern void __user __kernel_rt_sigreturn;
331 static int setup_frame(int sig, struct k_sigaction *ka,
332 sigset_t *set, struct pt_regs * regs)
334 void __user *restorer;
335 struct sigframe __user *frame;
336 int err = 0;
337 int usig;
339 frame = get_sigframe(ka, regs, sizeof(*frame));
341 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
342 goto give_sigsegv;
344 usig = current_thread_info()->exec_domain
345 && current_thread_info()->exec_domain->signal_invmap
346 && sig < 32
347 ? current_thread_info()->exec_domain->signal_invmap[sig]
348 : sig;
350 err = __put_user(usig, &frame->sig);
351 if (err)
352 goto give_sigsegv;
354 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
355 if (err)
356 goto give_sigsegv;
358 if (_NSIG_WORDS > 1) {
359 err = __copy_to_user(&frame->extramask, &set->sig[1],
360 sizeof(frame->extramask));
361 if (err)
362 goto give_sigsegv;
365 if (current->binfmt->hasvdso)
366 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
367 else
368 restorer = (void *)&frame->retcode;
369 if (ka->sa.sa_flags & SA_RESTORER)
370 restorer = ka->sa.sa_restorer;
372 /* Set up to return from userspace. */
373 err |= __put_user(restorer, &frame->pretcode);
376 * This is popl %eax ; movl $,%eax ; int $0x80
378 * WE DO NOT USE IT ANY MORE! It's only left here for historical
379 * reasons and because gdb uses it as a signature to notice
380 * signal handler stack frames.
382 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
383 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
384 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
386 if (err)
387 goto give_sigsegv;
389 /* Set up registers for signal handler */
390 regs->sp = (unsigned long) frame;
391 regs->ip = (unsigned long) ka->sa.sa_handler;
392 regs->ax = (unsigned long) sig;
393 regs->dx = (unsigned long) 0;
394 regs->cx = (unsigned long) 0;
396 regs->ds = __USER_DS;
397 regs->es = __USER_DS;
398 regs->ss = __USER_DS;
399 regs->cs = __USER_CS;
402 * Clear TF when entering the signal handler, but
403 * notify any tracer that was single-stepping it.
404 * The tracer may want to single-step inside the
405 * handler too.
407 regs->flags &= ~TF_MASK;
408 if (test_thread_flag(TIF_SINGLESTEP))
409 ptrace_notify(SIGTRAP);
411 #if DEBUG_SIG
412 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
413 current->comm, current->pid, frame, regs->ip, frame->pretcode);
414 #endif
416 return 0;
418 give_sigsegv:
419 force_sigsegv(sig, current);
420 return -EFAULT;
423 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
424 sigset_t *set, struct pt_regs * regs)
426 void __user *restorer;
427 struct rt_sigframe __user *frame;
428 int err = 0;
429 int usig;
431 frame = get_sigframe(ka, regs, sizeof(*frame));
433 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
434 goto give_sigsegv;
436 usig = current_thread_info()->exec_domain
437 && current_thread_info()->exec_domain->signal_invmap
438 && sig < 32
439 ? current_thread_info()->exec_domain->signal_invmap[sig]
440 : sig;
442 err |= __put_user(usig, &frame->sig);
443 err |= __put_user(&frame->info, &frame->pinfo);
444 err |= __put_user(&frame->uc, &frame->puc);
445 err |= copy_siginfo_to_user(&frame->info, info);
446 if (err)
447 goto give_sigsegv;
449 /* Create the ucontext. */
450 err |= __put_user(0, &frame->uc.uc_flags);
451 err |= __put_user(0, &frame->uc.uc_link);
452 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
453 err |= __put_user(sas_ss_flags(regs->sp),
454 &frame->uc.uc_stack.ss_flags);
455 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
456 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
457 regs, set->sig[0]);
458 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
459 if (err)
460 goto give_sigsegv;
462 /* Set up to return from userspace. */
463 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
464 if (ka->sa.sa_flags & SA_RESTORER)
465 restorer = ka->sa.sa_restorer;
466 err |= __put_user(restorer, &frame->pretcode);
469 * This is movl $,%ax ; int $0x80
471 * WE DO NOT USE IT ANY MORE! It's only left here for historical
472 * reasons and because gdb uses it as a signature to notice
473 * signal handler stack frames.
475 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
476 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
477 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
479 if (err)
480 goto give_sigsegv;
482 /* Set up registers for signal handler */
483 regs->sp = (unsigned long) frame;
484 regs->ip = (unsigned long) ka->sa.sa_handler;
485 regs->ax = (unsigned long) usig;
486 regs->dx = (unsigned long) &frame->info;
487 regs->cx = (unsigned long) &frame->uc;
489 regs->ds = __USER_DS;
490 regs->es = __USER_DS;
491 regs->ss = __USER_DS;
492 regs->cs = __USER_CS;
495 * Clear TF when entering the signal handler, but
496 * notify any tracer that was single-stepping it.
497 * The tracer may want to single-step inside the
498 * handler too.
500 regs->flags &= ~TF_MASK;
501 if (test_thread_flag(TIF_SINGLESTEP))
502 ptrace_notify(SIGTRAP);
504 #if DEBUG_SIG
505 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
506 current->comm, current->pid, frame, regs->ip, frame->pretcode);
507 #endif
509 return 0;
511 give_sigsegv:
512 force_sigsegv(sig, current);
513 return -EFAULT;
517 * OK, we're invoking a handler
520 static int
521 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
522 sigset_t *oldset, struct pt_regs * regs)
524 int ret;
526 /* Are we from a system call? */
527 if (regs->orig_ax >= 0) {
528 /* If so, check system call restarting.. */
529 switch (regs->ax) {
530 case -ERESTART_RESTARTBLOCK:
531 case -ERESTARTNOHAND:
532 regs->ax = -EINTR;
533 break;
535 case -ERESTARTSYS:
536 if (!(ka->sa.sa_flags & SA_RESTART)) {
537 regs->ax = -EINTR;
538 break;
540 /* fallthrough */
541 case -ERESTARTNOINTR:
542 regs->ax = regs->orig_ax;
543 regs->ip -= 2;
548 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
549 * flag so that register information in the sigcontext is correct.
551 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
552 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
553 regs->flags &= ~X86_EFLAGS_TF;
555 /* Set up the stack frame */
556 if (ka->sa.sa_flags & SA_SIGINFO)
557 ret = setup_rt_frame(sig, ka, info, oldset, regs);
558 else
559 ret = setup_frame(sig, ka, oldset, regs);
561 if (ret == 0) {
562 spin_lock_irq(&current->sighand->siglock);
563 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
564 if (!(ka->sa.sa_flags & SA_NODEFER))
565 sigaddset(&current->blocked,sig);
566 recalc_sigpending();
567 spin_unlock_irq(&current->sighand->siglock);
570 return ret;
574 * Note that 'init' is a special process: it doesn't get signals it doesn't
575 * want to handle. Thus you cannot kill init even with a SIGKILL even by
576 * mistake.
578 static void fastcall do_signal(struct pt_regs *regs)
580 siginfo_t info;
581 int signr;
582 struct k_sigaction ka;
583 sigset_t *oldset;
586 * We want the common case to go fast, which
587 * is why we may in certain cases get here from
588 * kernel mode. Just return without doing anything
589 * if so. vm86 regs switched out by assembly code
590 * before reaching here, so testing against kernel
591 * CS suffices.
593 if (!user_mode(regs))
594 return;
596 if (test_thread_flag(TIF_RESTORE_SIGMASK))
597 oldset = &current->saved_sigmask;
598 else
599 oldset = &current->blocked;
601 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
602 if (signr > 0) {
603 /* Re-enable any watchpoints before delivering the
604 * signal to user space. The processor register will
605 * have been cleared if the watchpoint triggered
606 * inside the kernel.
608 if (unlikely(current->thread.debugreg[7]))
609 set_debugreg(current->thread.debugreg[7], 7);
611 /* Whee! Actually deliver the signal. */
612 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
613 /* a signal was successfully delivered; the saved
614 * sigmask will have been stored in the signal frame,
615 * and will be restored by sigreturn, so we can simply
616 * clear the TIF_RESTORE_SIGMASK flag */
617 if (test_thread_flag(TIF_RESTORE_SIGMASK))
618 clear_thread_flag(TIF_RESTORE_SIGMASK);
621 return;
624 /* Did we come from a system call? */
625 if (regs->orig_ax >= 0) {
626 /* Restart the system call - no handlers present */
627 switch (regs->ax) {
628 case -ERESTARTNOHAND:
629 case -ERESTARTSYS:
630 case -ERESTARTNOINTR:
631 regs->ax = regs->orig_ax;
632 regs->ip -= 2;
633 break;
635 case -ERESTART_RESTARTBLOCK:
636 regs->ax = __NR_restart_syscall;
637 regs->ip -= 2;
638 break;
642 /* if there's no signal to deliver, we just put the saved sigmask
643 * back */
644 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
645 clear_thread_flag(TIF_RESTORE_SIGMASK);
646 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
651 * notification of userspace execution resumption
652 * - triggered by the TIF_WORK_MASK flags
654 __attribute__((regparm(3)))
655 void do_notify_resume(struct pt_regs *regs, void *_unused,
656 __u32 thread_info_flags)
658 /* Pending single-step? */
659 if (thread_info_flags & _TIF_SINGLESTEP) {
660 regs->flags |= TF_MASK;
661 clear_thread_flag(TIF_SINGLESTEP);
664 /* deal with pending signal delivery */
665 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
666 do_signal(regs);
668 if (thread_info_flags & _TIF_HRTICK_RESCHED)
669 hrtick_resched();
671 clear_thread_flag(TIF_IRET);