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>
28 #include <asm/ptrace.h>
29 #include <asm/ia32_unistd.h>
30 #include <asm/user32.h>
31 #include <asm/sigcontext32.h>
32 #include <asm/proto.h>
37 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
39 #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
40 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
41 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
44 asmlinkage
int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
);
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
)))
54 /* If you change siginfo_t structure, please make sure that
55 this code is fixed accordingly.
56 It should never copy any pad contained in the structure
57 to avoid security leaks, but must copy the generic
58 3 ints plus the relevant union member. */
59 err
= __put_user(from
->si_signo
, &to
->si_signo
);
60 err
|= __put_user(from
->si_errno
, &to
->si_errno
);
61 err
|= __put_user((short)from
->si_code
, &to
->si_code
);
63 if (from
->si_code
< 0) {
64 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
65 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
66 err
|= __put_user(ptr_to_compat(from
->si_ptr
), &to
->si_ptr
);
69 * First 32bits of unions are always present:
70 * si_pid === si_band === si_tid === si_addr(LS half)
72 err
|= __put_user(from
->_sifields
._pad
[0],
73 &to
->_sifields
._pad
[0]);
74 switch (from
->si_code
>> 16) {
75 case __SI_FAULT
>> 16:
78 err
|= __put_user(from
->si_utime
, &to
->si_utime
);
79 err
|= __put_user(from
->si_stime
, &to
->si_stime
);
80 err
|= __put_user(from
->si_status
, &to
->si_status
);
84 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
87 err
|= __put_user(from
->si_fd
, &to
->si_fd
);
89 case __SI_TIMER
>> 16:
90 err
|= __put_user(from
->si_overrun
, &to
->si_overrun
);
91 err
|= __put_user(ptr_to_compat(from
->si_ptr
),
94 /* This is not generated by the kernel as of now. */
96 case __SI_MESGQ
>> 16:
97 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
98 err
|= __put_user(from
->si_int
, &to
->si_int
);
105 int copy_siginfo_from_user32(siginfo_t
*to
, compat_siginfo_t __user
*from
)
110 if (!access_ok(VERIFY_READ
, from
, sizeof(compat_siginfo_t
)))
113 err
= __get_user(to
->si_signo
, &from
->si_signo
);
114 err
|= __get_user(to
->si_errno
, &from
->si_errno
);
115 err
|= __get_user(to
->si_code
, &from
->si_code
);
117 err
|= __get_user(to
->si_pid
, &from
->si_pid
);
118 err
|= __get_user(to
->si_uid
, &from
->si_uid
);
119 err
|= __get_user(ptr32
, &from
->si_ptr
);
120 to
->si_ptr
= compat_ptr(ptr32
);
125 asmlinkage
long sys32_sigsuspend(int history0
, int history1
, old_sigset_t mask
)
128 spin_lock_irq(¤t
->sighand
->siglock
);
129 current
->saved_sigmask
= current
->blocked
;
130 siginitset(¤t
->blocked
, mask
);
132 spin_unlock_irq(¤t
->sighand
->siglock
);
134 current
->state
= TASK_INTERRUPTIBLE
;
136 set_restore_sigmask();
137 return -ERESTARTNOHAND
;
140 asmlinkage
long sys32_sigaltstack(const stack_ia32_t __user
*uss_ptr
,
141 stack_ia32_t __user
*uoss_ptr
,
142 struct pt_regs
*regs
)
151 memset(&uss
, 0, sizeof(stack_t
));
152 if (!access_ok(VERIFY_READ
, uss_ptr
, sizeof(stack_ia32_t
)) ||
153 __get_user(ptr
, &uss_ptr
->ss_sp
) ||
154 __get_user(uss
.ss_flags
, &uss_ptr
->ss_flags
) ||
155 __get_user(uss
.ss_size
, &uss_ptr
->ss_size
))
157 uss
.ss_sp
= compat_ptr(ptr
);
161 ret
= do_sigaltstack(uss_ptr
? &uss
: NULL
, &uoss
, regs
->sp
);
163 if (ret
>= 0 && uoss_ptr
) {
164 if (!access_ok(VERIFY_WRITE
, uoss_ptr
, sizeof(stack_ia32_t
)) ||
165 __put_user(ptr_to_compat(uoss
.ss_sp
), &uoss_ptr
->ss_sp
) ||
166 __put_user(uoss
.ss_flags
, &uoss_ptr
->ss_flags
) ||
167 __put_user(uoss
.ss_size
, &uoss_ptr
->ss_size
))
174 * Do a signal return; undo the signal stack.
181 struct sigcontext_ia32 sc
;
182 struct _fpstate_ia32 fpstate_unused
; /* look at kernel/sigframe.h */
183 unsigned int extramask
[_COMPAT_NSIG_WORDS
-1];
185 /* fp state follows here */
194 compat_siginfo_t info
;
195 struct ucontext_ia32 uc
;
197 /* fp state follows here */
201 err |= __get_user(regs->x, &sc->x); \
204 #define COPY_SEG_CPL3(seg) { \
205 unsigned short tmp; \
206 err |= __get_user(tmp, &sc->seg); \
207 regs->seg = tmp | 3; \
210 #define RELOAD_SEG(seg) { \
211 unsigned int cur, pre; \
212 err |= __get_user(pre, &sc->seg); \
213 savesegment(seg, cur); \
216 loadsegment(seg, pre); \
219 static int ia32_restore_sigcontext(struct pt_regs
*regs
,
220 struct sigcontext_ia32 __user
*sc
,
223 unsigned int tmpflags
, gs
, oldgs
, err
= 0;
227 /* Always make any pending restarted system calls return -EINTR */
228 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
231 printk(KERN_DEBUG
"SIG restore_sigcontext: "
232 "sc=%p err(%x) eip(%x) cs(%x) flg(%x)\n",
233 sc
, sc
->err
, sc
->ip
, sc
->cs
, sc
->flags
);
237 * Reload fs and gs if they have changed in the signal
238 * handler. This does not handle long fs/gs base changes in
239 * the handler, but does not clobber them at least in the
242 err
|= __get_user(gs
, &sc
->gs
);
244 savesegment(gs
, oldgs
);
252 COPY(di
); COPY(si
); COPY(bp
); COPY(sp
); COPY(bx
);
253 COPY(dx
); COPY(cx
); COPY(ip
);
254 /* Don't touch extended registers */
259 err
|= __get_user(tmpflags
, &sc
->flags
);
260 regs
->flags
= (regs
->flags
& ~FIX_EFLAGS
) | (tmpflags
& FIX_EFLAGS
);
261 /* disable syscall checks */
264 err
|= __get_user(tmp
, &sc
->fpstate
);
265 buf
= compat_ptr(tmp
);
266 err
|= restore_i387_xstate_ia32(buf
);
268 err
|= __get_user(*pax
, &sc
->ax
);
272 asmlinkage
long sys32_sigreturn(struct pt_regs
*regs
)
274 struct sigframe __user
*frame
= (struct sigframe __user
*)(regs
->sp
-8);
278 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
280 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
281 || (_COMPAT_NSIG_WORDS
> 1
282 && __copy_from_user((((char *) &set
.sig
) + 4),
284 sizeof(frame
->extramask
))))
287 sigdelsetmask(&set
, ~_BLOCKABLE
);
288 spin_lock_irq(¤t
->sighand
->siglock
);
289 current
->blocked
= set
;
291 spin_unlock_irq(¤t
->sighand
->siglock
);
293 if (ia32_restore_sigcontext(regs
, &frame
->sc
, &ax
))
298 signal_fault(regs
, frame
, "32bit sigreturn");
302 asmlinkage
long sys32_rt_sigreturn(struct pt_regs
*regs
)
304 struct rt_sigframe __user
*frame
;
307 struct pt_regs tregs
;
309 frame
= (struct rt_sigframe __user
*)(regs
->sp
- 4);
311 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
313 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
316 sigdelsetmask(&set
, ~_BLOCKABLE
);
317 spin_lock_irq(¤t
->sighand
->siglock
);
318 current
->blocked
= set
;
320 spin_unlock_irq(¤t
->sighand
->siglock
);
322 if (ia32_restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &ax
))
326 if (sys32_sigaltstack(&frame
->uc
.uc_stack
, NULL
, &tregs
) == -EFAULT
)
332 signal_fault(regs
, frame
, "32bit rt sigreturn");
337 * Set up a signal frame.
340 static int ia32_setup_sigcontext(struct sigcontext_ia32 __user
*sc
,
341 void __user
*fpstate
,
342 struct pt_regs
*regs
, unsigned int mask
)
346 savesegment(gs
, tmp
);
347 err
|= __put_user(tmp
, (unsigned int __user
*)&sc
->gs
);
348 savesegment(fs
, tmp
);
349 err
|= __put_user(tmp
, (unsigned int __user
*)&sc
->fs
);
350 savesegment(ds
, tmp
);
351 err
|= __put_user(tmp
, (unsigned int __user
*)&sc
->ds
);
352 savesegment(es
, tmp
);
353 err
|= __put_user(tmp
, (unsigned int __user
*)&sc
->es
);
355 err
|= __put_user(regs
->di
, &sc
->di
);
356 err
|= __put_user(regs
->si
, &sc
->si
);
357 err
|= __put_user(regs
->bp
, &sc
->bp
);
358 err
|= __put_user(regs
->sp
, &sc
->sp
);
359 err
|= __put_user(regs
->bx
, &sc
->bx
);
360 err
|= __put_user(regs
->dx
, &sc
->dx
);
361 err
|= __put_user(regs
->cx
, &sc
->cx
);
362 err
|= __put_user(regs
->ax
, &sc
->ax
);
363 err
|= __put_user(current
->thread
.trap_no
, &sc
->trapno
);
364 err
|= __put_user(current
->thread
.error_code
, &sc
->err
);
365 err
|= __put_user(regs
->ip
, &sc
->ip
);
366 err
|= __put_user(regs
->cs
, (unsigned int __user
*)&sc
->cs
);
367 err
|= __put_user(regs
->flags
, &sc
->flags
);
368 err
|= __put_user(regs
->sp
, &sc
->sp_at_signal
);
369 err
|= __put_user(regs
->ss
, (unsigned int __user
*)&sc
->ss
);
371 err
|= __put_user(ptr_to_compat(fpstate
), &sc
->fpstate
);
373 /* non-iBCS2 extensions.. */
374 err
|= __put_user(mask
, &sc
->oldmask
);
375 err
|= __put_user(current
->thread
.cr2
, &sc
->cr2
);
381 * Determine which stack to use..
383 static void __user
*get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
,
389 /* Default to using normal stack */
392 /* This is the X/Open sanctioned signal stack switching. */
393 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
394 if (sas_ss_flags(sp
) == 0)
395 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
398 /* This is the legacy signal stack switching. */
399 else if ((regs
->ss
& 0xffff) != __USER32_DS
&&
400 !(ka
->sa
.sa_flags
& SA_RESTORER
) &&
402 sp
= (unsigned long) ka
->sa
.sa_restorer
;
405 sp
= sp
- sig_xstate_ia32_size
;
406 *fpstate
= (struct _fpstate_ia32
*) sp
;
407 if (save_i387_xstate_ia32(*fpstate
) < 0)
408 return (void __user
*) -1L;
412 /* Align the stack pointer according to the i386 ABI,
413 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
414 sp
= ((sp
+ 4) & -16ul) - 4;
415 return (void __user
*) sp
;
418 int ia32_setup_frame(int sig
, struct k_sigaction
*ka
,
419 compat_sigset_t
*set
, struct pt_regs
*regs
)
421 struct sigframe __user
*frame
;
422 void __user
*restorer
;
424 void __user
*fpstate
= NULL
;
426 /* copy_to_user optimizes that into a single 8 byte store */
427 static const struct {
431 } __attribute__((packed
)) code
= {
432 0xb858, /* popl %eax ; movl $...,%eax */
434 0x80cd, /* int $0x80 */
437 frame
= get_sigframe(ka
, regs
, sizeof(*frame
), &fpstate
);
439 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
442 if (__put_user(sig
, &frame
->sig
))
445 if (ia32_setup_sigcontext(&frame
->sc
, fpstate
, regs
, set
->sig
[0]))
448 if (_COMPAT_NSIG_WORDS
> 1) {
449 if (__copy_to_user(frame
->extramask
, &set
->sig
[1],
450 sizeof(frame
->extramask
)))
454 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
455 restorer
= ka
->sa
.sa_restorer
;
457 /* Return stub is in 32bit vsyscall page */
458 if (current
->mm
->context
.vdso
)
459 restorer
= VDSO32_SYMBOL(current
->mm
->context
.vdso
,
462 restorer
= &frame
->retcode
;
464 err
|= __put_user(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 err
|= __put_user(*((u64
*)&code
), (u64
*)frame
->retcode
);
474 /* Set up registers for signal handler */
475 regs
->sp
= (unsigned long) frame
;
476 regs
->ip
= (unsigned long) ka
->sa
.sa_handler
;
478 /* Make -mregparm=3 work */
483 loadsegment(ds
, __USER32_DS
);
484 loadsegment(es
, __USER32_DS
);
486 regs
->cs
= __USER32_CS
;
487 regs
->ss
= __USER32_DS
;
490 printk(KERN_DEBUG
"SIG deliver (%s:%d): sp=%p pc=%lx ra=%u\n",
491 current
->comm
, current
->pid
, frame
, regs
->ip
, frame
->pretcode
);
497 int ia32_setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
498 compat_sigset_t
*set
, struct pt_regs
*regs
)
500 struct rt_sigframe __user
*frame
;
501 void __user
*restorer
;
503 void __user
*fpstate
= NULL
;
505 /* __copy_to_user optimizes that into a single 8 byte store */
506 static const struct {
511 } __attribute__((packed
)) code
= {
513 __NR_ia32_rt_sigreturn
,
518 frame
= get_sigframe(ka
, regs
, sizeof(*frame
), &fpstate
);
520 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
523 err
|= __put_user(sig
, &frame
->sig
);
524 err
|= __put_user(ptr_to_compat(&frame
->info
), &frame
->pinfo
);
525 err
|= __put_user(ptr_to_compat(&frame
->uc
), &frame
->puc
);
526 err
|= copy_siginfo_to_user32(&frame
->info
, info
);
530 /* Create the ucontext. */
532 err
|= __put_user(UC_FP_XSTATE
, &frame
->uc
.uc_flags
);
534 err
|= __put_user(0, &frame
->uc
.uc_flags
);
535 err
|= __put_user(0, &frame
->uc
.uc_link
);
536 err
|= __put_user(current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
537 err
|= __put_user(sas_ss_flags(regs
->sp
),
538 &frame
->uc
.uc_stack
.ss_flags
);
539 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
540 err
|= ia32_setup_sigcontext(&frame
->uc
.uc_mcontext
, fpstate
,
542 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
546 if (ka
->sa
.sa_flags
& SA_RESTORER
)
547 restorer
= ka
->sa
.sa_restorer
;
549 restorer
= VDSO32_SYMBOL(current
->mm
->context
.vdso
,
551 err
|= __put_user(ptr_to_compat(restorer
), &frame
->pretcode
);
554 * Not actually used anymore, but left because some gdb
557 err
|= __put_user(*((u64
*)&code
), (u64
*)frame
->retcode
);
561 /* Set up registers for signal handler */
562 regs
->sp
= (unsigned long) frame
;
563 regs
->ip
= (unsigned long) ka
->sa
.sa_handler
;
565 /* Make -mregparm=3 work */
567 regs
->dx
= (unsigned long) &frame
->info
;
568 regs
->cx
= (unsigned long) &frame
->uc
;
570 /* Make -mregparm=3 work */
572 regs
->dx
= (unsigned long) &frame
->info
;
573 regs
->cx
= (unsigned long) &frame
->uc
;
575 loadsegment(ds
, __USER32_DS
);
576 loadsegment(es
, __USER32_DS
);
578 regs
->cs
= __USER32_CS
;
579 regs
->ss
= __USER32_DS
;
582 printk(KERN_DEBUG
"SIG deliver (%s:%d): sp=%p pc=%lx ra=%u\n",
583 current
->comm
, current
->pid
, frame
, regs
->ip
, frame
->pretcode
);