2 * linux/arch/x86_64/ia32/ia32_signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
8 * 2000-12-* x86-64 compatibility mode signal handling by Andi Kleen
11 #include <linux/sched.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 <linux/personality.h>
23 #include <linux/compat.h>
24 #include <linux/binfmts.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
29 #include <asm/ptrace.h>
30 #include <asm/ia32_unistd.h>
31 #include <asm/user32.h>
32 #include <asm/sigcontext32.h>
33 #include <asm/fpu32.h>
34 #include <asm/proto.h>
35 #include <asm/vsyscall32.h>
39 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
42 void signal_fault(struct pt_regs *regs, void __user *frame, char *where);
44 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
47 if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
50 /* If you change siginfo_t structure, please make sure that
51 this code is fixed accordingly.
52 It should never copy any pad contained in the structure
53 to avoid security leaks, but must copy the generic
54 3 ints plus the relevant union member. */
55 err = __put_user(from->si_signo, &to->si_signo);
56 err |= __put_user(from->si_errno, &to->si_errno);
57 err |= __put_user((short)from->si_code, &to->si_code);
59 if (from->si_code < 0) {
60 err |= __put_user(from->si_pid, &to->si_pid);
61 err |= __put_user(from->si_uid, &to->si_uid);
62 err |= __put_user(ptr_to_compat(from->si_ptr), &to->si_ptr);
64 /* First 32bits of unions are always present:
65 * si_pid === si_band === si_tid === si_addr(LS half) */
66 err |= __put_user(from->_sifields._pad[0], &to->_sifields._pad[0]);
67 switch (from->si_code >> 16) {
68 case __SI_FAULT >> 16:
71 err |= __put_user(from->si_utime, &to->si_utime);
72 err |= __put_user(from->si_stime, &to->si_stime);
73 err |= __put_user(from->si_status, &to->si_status);
77 err |= __put_user(from->si_uid, &to->si_uid);
80 err |= __put_user(from->si_fd, &to->si_fd);
82 case __SI_TIMER >> 16:
83 err |= __put_user(from->si_overrun, &to->si_overrun);
84 err |= __put_user(ptr_to_compat(from->si_ptr),
87 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
88 case __SI_MESGQ >> 16:
89 err |= __put_user(from->si_uid, &to->si_uid);
90 err |= __put_user(from->si_int, &to->si_int);
97 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
101 if (!access_ok (VERIFY_READ, from, sizeof(compat_siginfo_t)))
104 err = __get_user(to->si_signo, &from->si_signo);
105 err |= __get_user(to->si_errno, &from->si_errno);
106 err |= __get_user(to->si_code, &from->si_code);
108 err |= __get_user(to->si_pid, &from->si_pid);
109 err |= __get_user(to->si_uid, &from->si_uid);
110 err |= __get_user(ptr32, &from->si_ptr);
111 to->si_ptr = compat_ptr(ptr32);
117 sys32_sigsuspend(int history0, int history1, old_sigset_t mask)
120 spin_lock_irq(¤t->sighand->siglock);
121 current->saved_sigmask = current->blocked;
122 siginitset(¤t->blocked, mask);
124 spin_unlock_irq(¤t->sighand->siglock);
126 current->state = TASK_INTERRUPTIBLE;
128 set_thread_flag(TIF_RESTORE_SIGMASK);
129 return -ERESTARTNOHAND;
133 sys32_sigaltstack(const stack_ia32_t __user *uss_ptr,
134 stack_ia32_t __user *uoss_ptr,
135 struct pt_regs *regs)
142 memset(&uss,0,sizeof(stack_t));
143 if (!access_ok(VERIFY_READ,uss_ptr,sizeof(stack_ia32_t)) ||
144 __get_user(ptr, &uss_ptr->ss_sp) ||
145 __get_user(uss.ss_flags, &uss_ptr->ss_flags) ||
146 __get_user(uss.ss_size, &uss_ptr->ss_size))
148 uss.ss_sp = compat_ptr(ptr);
152 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss, regs->rsp);
154 if (ret >= 0 && uoss_ptr) {
155 if (!access_ok(VERIFY_WRITE,uoss_ptr,sizeof(stack_ia32_t)) ||
156 __put_user(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp) ||
157 __put_user(uoss.ss_flags, &uoss_ptr->ss_flags) ||
158 __put_user(uoss.ss_size, &uoss_ptr->ss_size))
165 * Do a signal return; undo the signal stack.
172 struct sigcontext_ia32 sc;
173 struct _fpstate_ia32 fpstate;
174 unsigned int extramask[_COMPAT_NSIG_WORDS-1];
184 compat_siginfo_t info;
185 struct ucontext_ia32 uc;
186 struct _fpstate_ia32 fpstate;
191 ia32_restore_sigcontext(struct pt_regs *regs, struct sigcontext_ia32 __user *sc, unsigned int *peax)
193 unsigned int err = 0;
195 /* Always make any pending restarted system calls return -EINTR */
196 current_thread_info()->restart_block.fn = do_no_restart_syscall;
199 printk("SIG restore_sigcontext: sc=%p err(%x) eip(%x) cs(%x) flg(%x)\n",
200 sc, sc->err, sc->eip, sc->cs, sc->eflags);
204 err |= __get_user(reg, &sc->e ##x); \
205 regs->r ## x = reg; \
208 #define RELOAD_SEG(seg,mask) \
209 { unsigned int cur; \
210 unsigned short pre; \
211 err |= __get_user(pre, &sc->seg); \
212 asm volatile("movl %%" #seg ",%0" : "=r" (cur)); \
214 if (pre != cur) loadsegment(seg,pre); }
216 /* Reload fs and gs if they have changed in the signal handler.
217 This does not handle long fs/gs base changes in the handler, but
218 does not clobber them at least in the normal case. */
222 err |= __get_user(gs, &sc->gs);
224 asm("movl %%gs,%0" : "=r" (oldgs));
232 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
233 COPY(dx); COPY(cx); COPY(ip);
234 /* Don't touch extended registers */
236 err |= __get_user(regs->cs, &sc->cs);
238 err |= __get_user(regs->ss, &sc->ss);
242 unsigned int tmpflags;
243 err |= __get_user(tmpflags, &sc->eflags);
244 regs->eflags = (regs->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
245 regs->orig_rax = -1; /* disable syscall checks */
250 struct _fpstate_ia32 __user * buf;
251 err |= __get_user(tmp, &sc->fpstate);
252 buf = compat_ptr(tmp);
254 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
256 err |= restore_i387_ia32(current, buf, 0);
258 struct task_struct *me = current;
268 err |= __get_user(tmp, &sc->eax);
277 asmlinkage long sys32_sigreturn(struct pt_regs *regs)
279 struct sigframe __user *frame = (struct sigframe __user *)(regs->rsp-8);
283 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
285 if (__get_user(set.sig[0], &frame->sc.oldmask)
286 || (_COMPAT_NSIG_WORDS > 1
287 && __copy_from_user((((char *) &set.sig) + 4), &frame->extramask,
288 sizeof(frame->extramask))))
291 sigdelsetmask(&set, ~_BLOCKABLE);
292 spin_lock_irq(¤t->sighand->siglock);
293 current->blocked = set;
295 spin_unlock_irq(¤t->sighand->siglock);
297 if (ia32_restore_sigcontext(regs, &frame->sc, &eax))
302 signal_fault(regs, frame, "32bit sigreturn");
306 asmlinkage long sys32_rt_sigreturn(struct pt_regs *regs)
308 struct rt_sigframe __user *frame;
311 struct pt_regs tregs;
313 frame = (struct rt_sigframe __user *)(regs->rsp - 4);
315 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
317 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
320 sigdelsetmask(&set, ~_BLOCKABLE);
321 spin_lock_irq(¤t->sighand->siglock);
322 current->blocked = set;
324 spin_unlock_irq(¤t->sighand->siglock);
326 if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
330 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
336 signal_fault(regs,frame,"32bit rt sigreturn");
341 * Set up a signal frame.
345 ia32_setup_sigcontext(struct sigcontext_ia32 __user *sc, struct _fpstate_ia32 __user *fpstate,
346 struct pt_regs *regs, unsigned int mask)
351 __asm__("movl %%gs,%0" : "=r"(tmp): "0"(tmp));
352 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
353 __asm__("movl %%fs,%0" : "=r"(tmp): "0"(tmp));
354 err |= __put_user(tmp, (unsigned int __user *)&sc->fs);
355 __asm__("movl %%ds,%0" : "=r"(tmp): "0"(tmp));
356 err |= __put_user(tmp, (unsigned int __user *)&sc->ds);
357 __asm__("movl %%es,%0" : "=r"(tmp): "0"(tmp));
358 err |= __put_user(tmp, (unsigned int __user *)&sc->es);
360 err |= __put_user((u32)regs->rdi, &sc->edi);
361 err |= __put_user((u32)regs->rsi, &sc->esi);
362 err |= __put_user((u32)regs->rbp, &sc->ebp);
363 err |= __put_user((u32)regs->rsp, &sc->esp);
364 err |= __put_user((u32)regs->rbx, &sc->ebx);
365 err |= __put_user((u32)regs->rdx, &sc->edx);
366 err |= __put_user((u32)regs->rcx, &sc->ecx);
367 err |= __put_user((u32)regs->rax, &sc->eax);
368 err |= __put_user((u32)regs->cs, &sc->cs);
369 err |= __put_user((u32)regs->ss, &sc->ss);
370 err |= __put_user(current->thread.trap_no, &sc->trapno);
371 err |= __put_user(current->thread.error_code, &sc->err);
372 err |= __put_user((u32)regs->rip, &sc->eip);
373 err |= __put_user((u32)regs->eflags, &sc->eflags);
374 err |= __put_user((u32)regs->rsp, &sc->esp_at_signal);
376 tmp = save_i387_ia32(current, fpstate, regs, 0);
382 err |= __put_user(ptr_to_compat(tmp ? fpstate : NULL),
386 /* non-iBCS2 extensions.. */
387 err |= __put_user(mask, &sc->oldmask);
388 err |= __put_user(current->thread.cr2, &sc->cr2);
394 * Determine which stack to use..
397 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
401 /* Default to using normal stack */
404 /* This is the X/Open sanctioned signal stack switching. */
405 if (ka->sa.sa_flags & SA_ONSTACK) {
406 if (sas_ss_flags(rsp) == 0)
407 rsp = current->sas_ss_sp + current->sas_ss_size;
410 /* This is the legacy signal stack switching. */
411 else if ((regs->ss & 0xffff) != __USER_DS &&
412 !(ka->sa.sa_flags & SA_RESTORER) &&
413 ka->sa.sa_restorer) {
414 rsp = (unsigned long) ka->sa.sa_restorer;
418 /* Align the stack pointer according to the i386 ABI,
419 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
420 rsp = ((rsp + 4) & -16ul) - 4;
421 return (void __user *) rsp;
424 int ia32_setup_frame(int sig, struct k_sigaction *ka,
425 compat_sigset_t *set, struct pt_regs * regs)
427 struct sigframe __user *frame;
430 frame = get_sigframe(ka, regs, sizeof(*frame));
432 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
435 err |= __put_user(sig, &frame->sig);
439 err |= ia32_setup_sigcontext(&frame->sc, &frame->fpstate, regs,
444 if (_COMPAT_NSIG_WORDS > 1) {
445 err |= __copy_to_user(frame->extramask, &set->sig[1],
446 sizeof(frame->extramask));
451 /* Return stub is in 32bit vsyscall page */
453 void __user *restorer;
454 if (current->binfmt->hasvdso)
455 restorer = VSYSCALL32_SIGRETURN;
457 restorer = (void *)&frame->retcode;
458 if (ka->sa.sa_flags & SA_RESTORER)
459 restorer = ka->sa.sa_restorer;
460 err |= __put_user(ptr_to_compat(restorer), &frame->pretcode);
462 /* These are actually not used anymore, but left because some
463 gdb versions depend on them as a marker. */
465 /* copy_to_user optimizes that into a single 8 byte store */
466 static const struct {
471 } __attribute__((packed)) code = {
472 0xb858, /* popl %eax ; movl $...,%eax */
474 0x80cd, /* int $0x80 */
477 err |= __copy_to_user(frame->retcode, &code, 8);
482 /* Set up registers for signal handler */
483 regs->rsp = (unsigned long) frame;
484 regs->rip = (unsigned long) ka->sa.sa_handler;
486 /* Make -mregparm=3 work */
491 asm volatile("movl %0,%%ds" :: "r" (__USER32_DS));
492 asm volatile("movl %0,%%es" :: "r" (__USER32_DS));
494 regs->cs = __USER32_CS;
495 regs->ss = __USER32_DS;
498 regs->eflags &= ~TF_MASK;
499 if (test_thread_flag(TIF_SINGLESTEP))
500 ptrace_notify(SIGTRAP);
503 printk("SIG deliver (%s:%d): sp=%p pc=%lx ra=%u\n",
504 current->comm, current->pid, frame, regs->rip, frame->pretcode);
510 force_sigsegv(sig, current);
514 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
515 compat_sigset_t *set, struct pt_regs * regs)
517 struct rt_sigframe __user *frame;
520 frame = get_sigframe(ka, regs, sizeof(*frame));
522 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
526 struct exec_domain *ed = current_thread_info()->exec_domain;
527 err |= __put_user((ed
530 ? ed->signal_invmap[sig]
534 err |= __put_user(ptr_to_compat(&frame->info), &frame->pinfo);
535 err |= __put_user(ptr_to_compat(&frame->uc), &frame->puc);
536 err |= copy_siginfo_to_user32(&frame->info, info);
540 /* Create the ucontext. */
541 err |= __put_user(0, &frame->uc.uc_flags);
542 err |= __put_user(0, &frame->uc.uc_link);
543 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
544 err |= __put_user(sas_ss_flags(regs->rsp),
545 &frame->uc.uc_stack.ss_flags);
546 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
547 err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
549 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
555 void __user *restorer = VSYSCALL32_RTSIGRETURN;
556 if (ka->sa.sa_flags & SA_RESTORER)
557 restorer = ka->sa.sa_restorer;
558 err |= __put_user(ptr_to_compat(restorer), &frame->pretcode);
561 /* This is movl $,%eax ; int $0x80 */
562 /* Not actually used anymore, but left because some gdb versions
565 /* __copy_to_user optimizes that into a single 8 byte store */
566 static const struct {
572 } __attribute__((packed)) code = {
574 __NR_ia32_rt_sigreturn,
578 err |= __copy_to_user(frame->retcode, &code, 8);
583 /* Set up registers for signal handler */
584 regs->rsp = (unsigned long) frame;
585 regs->rip = (unsigned long) ka->sa.sa_handler;
587 /* Make -mregparm=3 work */
589 regs->rdx = (unsigned long) &frame->info;
590 regs->rcx = (unsigned long) &frame->uc;
592 /* Make -mregparm=3 work */
594 regs->rdx = (unsigned long) &frame->info;
595 regs->rcx = (unsigned long) &frame->uc;
597 asm volatile("movl %0,%%ds" :: "r" (__USER32_DS));
598 asm volatile("movl %0,%%es" :: "r" (__USER32_DS));
600 regs->cs = __USER32_CS;
601 regs->ss = __USER32_DS;
604 regs->eflags &= ~TF_MASK;
605 if (test_thread_flag(TIF_SINGLESTEP))
606 ptrace_notify(SIGTRAP);
609 printk("SIG deliver (%s:%d): sp=%p pc=%lx ra=%u\n",
610 current->comm, current->pid, frame, regs->rip, frame->pretcode);
616 force_sigsegv(sig, current);