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/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/ptrace.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/personality.h>
22 #include <linux/compat.h>
23 #include <linux/binfmts.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
27 #include <asm/ptrace.h>
28 #include <asm/ia32_unistd.h>
29 #include <asm/user32.h>
30 #include <asm/sigcontext32.h>
31 #include <asm/proto.h>
33 #include <asm/sigframe.h>
34 #include <asm/sys_ia32.h>
38 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40 #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
41 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
42 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
45 void signal_fault(struct pt_regs *regs, void __user *frame, char *where);
47 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
51 if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
55 /* If you change siginfo_t structure, please make sure that
56 this code is fixed accordingly.
57 It should never copy any pad contained in the structure
58 to avoid security leaks, but must copy the generic
59 3 ints plus the relevant union member. */
60 put_user_ex(from->si_signo, &to->si_signo);
61 put_user_ex(from->si_errno, &to->si_errno);
62 put_user_ex((short)from->si_code, &to->si_code);
64 if (from->si_code < 0) {
65 put_user_ex(from->si_pid, &to->si_pid);
66 put_user_ex(from->si_uid, &to->si_uid);
67 put_user_ex(ptr_to_compat(from->si_ptr), &to->si_ptr);
70 * First 32bits of unions are always present:
71 * si_pid === si_band === si_tid === si_addr(LS half)
73 put_user_ex(from->_sifields._pad[0],
74 &to->_sifields._pad[0]);
75 switch (from->si_code >> 16) {
76 case __SI_FAULT >> 16:
79 put_user_ex(from->si_utime, &to->si_utime);
80 put_user_ex(from->si_stime, &to->si_stime);
81 put_user_ex(from->si_status, &to->si_status);
85 put_user_ex(from->si_uid, &to->si_uid);
88 put_user_ex(from->si_fd, &to->si_fd);
90 case __SI_TIMER >> 16:
91 put_user_ex(from->si_overrun, &to->si_overrun);
92 put_user_ex(ptr_to_compat(from->si_ptr),
95 /* This is not generated by the kernel as of now. */
97 case __SI_MESGQ >> 16:
98 put_user_ex(from->si_uid, &to->si_uid);
99 put_user_ex(from->si_int, &to->si_int);
103 } put_user_catch(err);
108 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
113 if (!access_ok(VERIFY_READ, from, sizeof(compat_siginfo_t)))
117 get_user_ex(to->si_signo, &from->si_signo);
118 get_user_ex(to->si_errno, &from->si_errno);
119 get_user_ex(to->si_code, &from->si_code);
121 get_user_ex(to->si_pid, &from->si_pid);
122 get_user_ex(to->si_uid, &from->si_uid);
123 get_user_ex(ptr32, &from->si_ptr);
124 to->si_ptr = compat_ptr(ptr32);
125 } get_user_catch(err);
130 asmlinkage long sys32_sigsuspend(int history0, int history1, old_sigset_t mask)
133 spin_lock_irq(¤t->sighand->siglock);
134 current->saved_sigmask = current->blocked;
135 siginitset(¤t->blocked, mask);
137 spin_unlock_irq(¤t->sighand->siglock);
139 current->state = TASK_INTERRUPTIBLE;
141 set_restore_sigmask();
142 return -ERESTARTNOHAND;
145 asmlinkage long sys32_sigaltstack(const stack_ia32_t __user *uss_ptr,
146 stack_ia32_t __user *uoss_ptr,
147 struct pt_regs *regs)
156 memset(&uss, 0, sizeof(stack_t));
157 if (!access_ok(VERIFY_READ, uss_ptr, sizeof(stack_ia32_t)))
161 get_user_ex(ptr, &uss_ptr->ss_sp);
162 get_user_ex(uss.ss_flags, &uss_ptr->ss_flags);
163 get_user_ex(uss.ss_size, &uss_ptr->ss_size);
164 } get_user_catch(err);
168 uss.ss_sp = compat_ptr(ptr);
172 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss, regs->sp);
174 if (ret >= 0 && uoss_ptr) {
175 if (!access_ok(VERIFY_WRITE, uoss_ptr, sizeof(stack_ia32_t)))
179 put_user_ex(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp);
180 put_user_ex(uoss.ss_flags, &uoss_ptr->ss_flags);
181 put_user_ex(uoss.ss_size, &uoss_ptr->ss_size);
182 } put_user_catch(err);
191 * Do a signal return; undo the signal stack.
194 get_user_ex(regs->x, &sc->x); \
197 #define COPY_SEG_CPL3(seg) { \
198 unsigned short tmp; \
199 get_user_ex(tmp, &sc->seg); \
200 regs->seg = tmp | 3; \
203 #define RELOAD_SEG(seg) { \
204 unsigned int cur, pre; \
205 get_user_ex(pre, &sc->seg); \
206 savesegment(seg, cur); \
209 loadsegment(seg, pre); \
212 static int ia32_restore_sigcontext(struct pt_regs *regs,
213 struct sigcontext_ia32 __user *sc,
216 unsigned int tmpflags, gs, oldgs, err = 0;
220 /* Always make any pending restarted system calls return -EINTR */
221 current_thread_info()->restart_block.fn = do_no_restart_syscall;
224 printk(KERN_DEBUG "SIG restore_sigcontext: "
225 "sc=%p err(%x) eip(%x) cs(%x) flg(%x)\n",
226 sc, sc->err, sc->ip, sc->cs, sc->flags);
231 * Reload fs and gs if they have changed in the signal
232 * handler. This does not handle long fs/gs base changes in
233 * the handler, but does not clobber them at least in the
236 get_user_ex(gs, &sc->gs);
238 savesegment(gs, oldgs);
246 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
247 COPY(dx); COPY(cx); COPY(ip);
248 /* Don't touch extended registers */
253 get_user_ex(tmpflags, &sc->flags);
254 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
255 /* disable syscall checks */
258 get_user_ex(tmp, &sc->fpstate);
259 buf = compat_ptr(tmp);
260 err |= restore_i387_xstate_ia32(buf);
262 get_user_ex(*pax, &sc->ax);
263 } get_user_catch(err);
268 asmlinkage long sys32_sigreturn(struct pt_regs *regs)
270 struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
274 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
276 if (__get_user(set.sig[0], &frame->sc.oldmask)
277 || (_COMPAT_NSIG_WORDS > 1
278 && __copy_from_user((((char *) &set.sig) + 4),
280 sizeof(frame->extramask))))
283 sigdelsetmask(&set, ~_BLOCKABLE);
284 spin_lock_irq(¤t->sighand->siglock);
285 current->blocked = set;
287 spin_unlock_irq(¤t->sighand->siglock);
289 if (ia32_restore_sigcontext(regs, &frame->sc, &ax))
294 signal_fault(regs, frame, "32bit sigreturn");
298 asmlinkage long sys32_rt_sigreturn(struct pt_regs *regs)
300 struct rt_sigframe_ia32 __user *frame;
303 struct pt_regs tregs;
305 frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
307 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
309 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
312 sigdelsetmask(&set, ~_BLOCKABLE);
313 spin_lock_irq(¤t->sighand->siglock);
314 current->blocked = set;
316 spin_unlock_irq(¤t->sighand->siglock);
318 if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
322 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
328 signal_fault(regs, frame, "32bit rt sigreturn");
333 * Set up a signal frame.
336 static int ia32_setup_sigcontext(struct sigcontext_ia32 __user *sc,
337 void __user *fpstate,
338 struct pt_regs *regs, unsigned int mask)
343 savesegment(gs, tmp);
344 put_user_ex(tmp, (unsigned int __user *)&sc->gs);
345 savesegment(fs, tmp);
346 put_user_ex(tmp, (unsigned int __user *)&sc->fs);
347 savesegment(ds, tmp);
348 put_user_ex(tmp, (unsigned int __user *)&sc->ds);
349 savesegment(es, tmp);
350 put_user_ex(tmp, (unsigned int __user *)&sc->es);
352 put_user_ex(regs->di, &sc->di);
353 put_user_ex(regs->si, &sc->si);
354 put_user_ex(regs->bp, &sc->bp);
355 put_user_ex(regs->sp, &sc->sp);
356 put_user_ex(regs->bx, &sc->bx);
357 put_user_ex(regs->dx, &sc->dx);
358 put_user_ex(regs->cx, &sc->cx);
359 put_user_ex(regs->ax, &sc->ax);
360 put_user_ex(current->thread.trap_no, &sc->trapno);
361 put_user_ex(current->thread.error_code, &sc->err);
362 put_user_ex(regs->ip, &sc->ip);
363 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
364 put_user_ex(regs->flags, &sc->flags);
365 put_user_ex(regs->sp, &sc->sp_at_signal);
366 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
368 put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
370 /* non-iBCS2 extensions.. */
371 put_user_ex(mask, &sc->oldmask);
372 put_user_ex(current->thread.cr2, &sc->cr2);
373 } put_user_catch(err);
379 * Determine which stack to use..
381 static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
387 /* Default to using normal stack */
390 /* This is the X/Open sanctioned signal stack switching. */
391 if (ka->sa.sa_flags & SA_ONSTACK) {
392 if (sas_ss_flags(sp) == 0)
393 sp = current->sas_ss_sp + current->sas_ss_size;
396 /* This is the legacy signal stack switching. */
397 else if ((regs->ss & 0xffff) != __USER32_DS &&
398 !(ka->sa.sa_flags & SA_RESTORER) &&
400 sp = (unsigned long) ka->sa.sa_restorer;
403 sp = sp - sig_xstate_ia32_size;
404 *fpstate = (struct _fpstate_ia32 *) sp;
405 if (save_i387_xstate_ia32(*fpstate) < 0)
406 return (void __user *) -1L;
410 /* Align the stack pointer according to the i386 ABI,
411 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
412 sp = ((sp + 4) & -16ul) - 4;
413 return (void __user *) sp;
416 int ia32_setup_frame(int sig, struct k_sigaction *ka,
417 compat_sigset_t *set, struct pt_regs *regs)
419 struct sigframe_ia32 __user *frame;
420 void __user *restorer;
422 void __user *fpstate = NULL;
424 /* copy_to_user optimizes that into a single 8 byte store */
425 static const struct {
429 } __attribute__((packed)) code = {
430 0xb858, /* popl %eax ; movl $...,%eax */
432 0x80cd, /* int $0x80 */
435 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
437 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
440 if (__put_user(sig, &frame->sig))
443 if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
446 if (_COMPAT_NSIG_WORDS > 1) {
447 if (__copy_to_user(frame->extramask, &set->sig[1],
448 sizeof(frame->extramask)))
452 if (ka->sa.sa_flags & SA_RESTORER) {
453 restorer = ka->sa.sa_restorer;
455 /* Return stub is in 32bit vsyscall page */
456 if (current->mm->context.vdso)
457 restorer = VDSO32_SYMBOL(current->mm->context.vdso,
460 restorer = &frame->retcode;
464 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
467 * These are actually not used anymore, but left because some
468 * gdb versions depend on them as a marker.
470 put_user_ex(*((u64 *)&code), (u64 *)frame->retcode);
471 } put_user_catch(err);
476 /* Set up registers for signal handler */
477 regs->sp = (unsigned long) frame;
478 regs->ip = (unsigned long) ka->sa.sa_handler;
480 /* Make -mregparm=3 work */
485 loadsegment(ds, __USER32_DS);
486 loadsegment(es, __USER32_DS);
488 regs->cs = __USER32_CS;
489 regs->ss = __USER32_DS;
492 printk(KERN_DEBUG "SIG deliver (%s:%d): sp=%p pc=%lx ra=%u\n",
493 current->comm, current->pid, frame, regs->ip, frame->pretcode);
499 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
500 compat_sigset_t *set, struct pt_regs *regs)
502 struct rt_sigframe_ia32 __user *frame;
503 void __user *restorer;
505 void __user *fpstate = NULL;
507 /* __copy_to_user optimizes that into a single 8 byte store */
508 static const struct {
513 } __attribute__((packed)) code = {
515 __NR_ia32_rt_sigreturn,
520 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
522 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
526 put_user_ex(sig, &frame->sig);
527 put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
528 put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
529 err |= copy_siginfo_to_user32(&frame->info, info);
531 /* Create the ucontext. */
533 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
535 put_user_ex(0, &frame->uc.uc_flags);
536 put_user_ex(0, &frame->uc.uc_link);
537 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
538 put_user_ex(sas_ss_flags(regs->sp),
539 &frame->uc.uc_stack.ss_flags);
540 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
541 err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
543 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
545 if (ka->sa.sa_flags & SA_RESTORER)
546 restorer = ka->sa.sa_restorer;
548 restorer = VDSO32_SYMBOL(current->mm->context.vdso,
550 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
553 * Not actually used anymore, but left because some gdb
556 put_user_ex(*((u64 *)&code), (u64 *)frame->retcode);
557 } put_user_catch(err);
562 /* Set up registers for signal handler */
563 regs->sp = (unsigned long) frame;
564 regs->ip = (unsigned long) ka->sa.sa_handler;
566 /* Make -mregparm=3 work */
568 regs->dx = (unsigned long) &frame->info;
569 regs->cx = (unsigned long) &frame->uc;
571 loadsegment(ds, __USER32_DS);
572 loadsegment(es, __USER32_DS);
574 regs->cs = __USER32_CS;
575 regs->ss = __USER32_DS;
578 printk(KERN_DEBUG "SIG deliver (%s:%d): sp=%p pc=%lx ra=%u\n",
579 current->comm, current->pid, frame, regs->ip, frame->pretcode);