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/errno.h>
16 #include <linux/wait.h>
17 #include <linux/unistd.h>
18 #include <linux/stddef.h>
19 #include <linux/personality.h>
20 #include <linux/compat.h>
21 #include <linux/binfmts.h>
22 #include <asm/ucontext.h>
23 #include <asm/uaccess.h>
25 #include <asm/fpu-internal.h>
26 #include <asm/ptrace.h>
27 #include <asm/ia32_unistd.h>
28 #include <asm/user32.h>
29 #include <asm/sigcontext32.h>
30 #include <asm/proto.h>
32 #include <asm/sigframe.h>
33 #include <asm/sighandling.h>
34 #include <asm/sys_ia32.h>
37 #define FIX_EFLAGS __FIX_EFLAGS
39 int copy_siginfo_to_user32(compat_siginfo_t __user
*to
, siginfo_t
*from
)
42 bool ia32
= test_thread_flag(TIF_IA32
);
44 if (!access_ok(VERIFY_WRITE
, to
, sizeof(compat_siginfo_t
)))
48 /* If you change siginfo_t structure, please make sure that
49 this code is fixed accordingly.
50 It should never copy any pad contained in the structure
51 to avoid security leaks, but must copy the generic
52 3 ints plus the relevant union member. */
53 put_user_ex(from
->si_signo
, &to
->si_signo
);
54 put_user_ex(from
->si_errno
, &to
->si_errno
);
55 put_user_ex((short)from
->si_code
, &to
->si_code
);
57 if (from
->si_code
< 0) {
58 put_user_ex(from
->si_pid
, &to
->si_pid
);
59 put_user_ex(from
->si_uid
, &to
->si_uid
);
60 put_user_ex(ptr_to_compat(from
->si_ptr
), &to
->si_ptr
);
63 * First 32bits of unions are always present:
64 * si_pid === si_band === si_tid === si_addr(LS half)
66 put_user_ex(from
->_sifields
._pad
[0],
67 &to
->_sifields
._pad
[0]);
68 switch (from
->si_code
>> 16) {
69 case __SI_FAULT
>> 16:
72 put_user_ex(from
->si_syscall
, &to
->si_syscall
);
73 put_user_ex(from
->si_arch
, &to
->si_arch
);
77 put_user_ex(from
->si_utime
, &to
->si_utime
);
78 put_user_ex(from
->si_stime
, &to
->si_stime
);
80 put_user_ex(from
->si_utime
, &to
->_sifields
._sigchld_x32
._utime
);
81 put_user_ex(from
->si_stime
, &to
->_sifields
._sigchld_x32
._stime
);
83 put_user_ex(from
->si_status
, &to
->si_status
);
87 put_user_ex(from
->si_uid
, &to
->si_uid
);
90 put_user_ex(from
->si_fd
, &to
->si_fd
);
92 case __SI_TIMER
>> 16:
93 put_user_ex(from
->si_overrun
, &to
->si_overrun
);
94 put_user_ex(ptr_to_compat(from
->si_ptr
),
97 /* This is not generated by the kernel as of now. */
99 case __SI_MESGQ
>> 16:
100 put_user_ex(from
->si_uid
, &to
->si_uid
);
101 put_user_ex(from
->si_int
, &to
->si_int
);
105 } put_user_catch(err
);
110 int copy_siginfo_from_user32(siginfo_t
*to
, compat_siginfo_t __user
*from
)
115 if (!access_ok(VERIFY_READ
, from
, sizeof(compat_siginfo_t
)))
119 get_user_ex(to
->si_signo
, &from
->si_signo
);
120 get_user_ex(to
->si_errno
, &from
->si_errno
);
121 get_user_ex(to
->si_code
, &from
->si_code
);
123 get_user_ex(to
->si_pid
, &from
->si_pid
);
124 get_user_ex(to
->si_uid
, &from
->si_uid
);
125 get_user_ex(ptr32
, &from
->si_ptr
);
126 to
->si_ptr
= compat_ptr(ptr32
);
127 } get_user_catch(err
);
133 * Do a signal return; undo the signal stack.
135 #define loadsegment_gs(v) load_gs_index(v)
136 #define loadsegment_fs(v) loadsegment(fs, v)
137 #define loadsegment_ds(v) loadsegment(ds, v)
138 #define loadsegment_es(v) loadsegment(es, v)
140 #define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
141 #define set_user_seg(seg, v) loadsegment_##seg(v)
144 get_user_ex(regs->x, &sc->x); \
147 #define GET_SEG(seg) ({ \
148 unsigned short tmp; \
149 get_user_ex(tmp, &sc->seg); \
153 #define COPY_SEG_CPL3(seg) do { \
154 regs->seg = GET_SEG(seg) | 3; \
157 #define RELOAD_SEG(seg) { \
158 unsigned int pre = GET_SEG(seg); \
159 unsigned int cur = get_user_seg(seg); \
162 set_user_seg(seg, pre); \
165 static int ia32_restore_sigcontext(struct pt_regs
*regs
,
166 struct sigcontext_ia32 __user
*sc
,
169 unsigned int tmpflags
, err
= 0;
173 /* Always make any pending restarted system calls return -EINTR */
174 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
178 * Reload fs and gs if they have changed in the signal
179 * handler. This does not handle long fs/gs base changes in
180 * the handler, but does not clobber them at least in the
188 COPY(di
); COPY(si
); COPY(bp
); COPY(sp
); COPY(bx
);
189 COPY(dx
); COPY(cx
); COPY(ip
);
190 /* Don't touch extended registers */
195 get_user_ex(tmpflags
, &sc
->flags
);
196 regs
->flags
= (regs
->flags
& ~FIX_EFLAGS
) | (tmpflags
& FIX_EFLAGS
);
197 /* disable syscall checks */
200 get_user_ex(tmp
, &sc
->fpstate
);
201 buf
= compat_ptr(tmp
);
203 get_user_ex(*pax
, &sc
->ax
);
204 } get_user_catch(err
);
206 err
|= restore_xstate_sig(buf
, 1);
211 asmlinkage
long sys32_sigreturn(void)
213 struct pt_regs
*regs
= current_pt_regs();
214 struct sigframe_ia32 __user
*frame
= (struct sigframe_ia32 __user
*)(regs
->sp
-8);
218 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
220 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
221 || (_COMPAT_NSIG_WORDS
> 1
222 && __copy_from_user((((char *) &set
.sig
) + 4),
224 sizeof(frame
->extramask
))))
227 set_current_blocked(&set
);
229 if (ia32_restore_sigcontext(regs
, &frame
->sc
, &ax
))
234 signal_fault(regs
, frame
, "32bit sigreturn");
238 asmlinkage
long sys32_rt_sigreturn(void)
240 struct pt_regs
*regs
= current_pt_regs();
241 struct rt_sigframe_ia32 __user
*frame
;
245 frame
= (struct rt_sigframe_ia32 __user
*)(regs
->sp
- 4);
247 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
249 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
252 set_current_blocked(&set
);
254 if (ia32_restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &ax
))
257 if (compat_restore_altstack(&frame
->uc
.uc_stack
))
263 signal_fault(regs
, frame
, "32bit rt sigreturn");
268 * Set up a signal frame.
271 static int ia32_setup_sigcontext(struct sigcontext_ia32 __user
*sc
,
272 void __user
*fpstate
,
273 struct pt_regs
*regs
, unsigned int mask
)
278 put_user_ex(get_user_seg(gs
), (unsigned int __user
*)&sc
->gs
);
279 put_user_ex(get_user_seg(fs
), (unsigned int __user
*)&sc
->fs
);
280 put_user_ex(get_user_seg(ds
), (unsigned int __user
*)&sc
->ds
);
281 put_user_ex(get_user_seg(es
), (unsigned int __user
*)&sc
->es
);
283 put_user_ex(regs
->di
, &sc
->di
);
284 put_user_ex(regs
->si
, &sc
->si
);
285 put_user_ex(regs
->bp
, &sc
->bp
);
286 put_user_ex(regs
->sp
, &sc
->sp
);
287 put_user_ex(regs
->bx
, &sc
->bx
);
288 put_user_ex(regs
->dx
, &sc
->dx
);
289 put_user_ex(regs
->cx
, &sc
->cx
);
290 put_user_ex(regs
->ax
, &sc
->ax
);
291 put_user_ex(current
->thread
.trap_nr
, &sc
->trapno
);
292 put_user_ex(current
->thread
.error_code
, &sc
->err
);
293 put_user_ex(regs
->ip
, &sc
->ip
);
294 put_user_ex(regs
->cs
, (unsigned int __user
*)&sc
->cs
);
295 put_user_ex(regs
->flags
, &sc
->flags
);
296 put_user_ex(regs
->sp
, &sc
->sp_at_signal
);
297 put_user_ex(regs
->ss
, (unsigned int __user
*)&sc
->ss
);
299 put_user_ex(ptr_to_compat(fpstate
), &sc
->fpstate
);
301 /* non-iBCS2 extensions.. */
302 put_user_ex(mask
, &sc
->oldmask
);
303 put_user_ex(current
->thread
.cr2
, &sc
->cr2
);
304 } put_user_catch(err
);
310 * Determine which stack to use..
312 static void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
,
314 void __user
**fpstate
)
318 /* Default to using normal stack */
321 /* This is the X/Open sanctioned signal stack switching. */
322 if (ksig
->ka
.sa
.sa_flags
& SA_ONSTACK
)
323 sp
= sigsp(sp
, ksig
);
324 /* This is the legacy signal stack switching. */
325 else if ((regs
->ss
& 0xffff) != __USER32_DS
&&
326 !(ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) &&
327 ksig
->ka
.sa
.sa_restorer
)
328 sp
= (unsigned long) ksig
->ka
.sa
.sa_restorer
;
331 unsigned long fx_aligned
, math_size
;
333 sp
= alloc_mathframe(sp
, 1, &fx_aligned
, &math_size
);
334 *fpstate
= (struct _fpstate_ia32 __user
*) sp
;
335 if (save_xstate_sig(*fpstate
, (void __user
*)fx_aligned
,
337 return (void __user
*) -1L;
341 /* Align the stack pointer according to the i386 ABI,
342 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
343 sp
= ((sp
+ 4) & -16ul) - 4;
344 return (void __user
*) sp
;
347 int ia32_setup_frame(int sig
, struct ksignal
*ksig
,
348 compat_sigset_t
*set
, struct pt_regs
*regs
)
350 struct sigframe_ia32 __user
*frame
;
351 void __user
*restorer
;
353 void __user
*fpstate
= NULL
;
355 /* copy_to_user optimizes that into a single 8 byte store */
356 static const struct {
360 } __attribute__((packed
)) code
= {
361 0xb858, /* popl %eax ; movl $...,%eax */
363 0x80cd, /* int $0x80 */
366 frame
= get_sigframe(ksig
, regs
, sizeof(*frame
), &fpstate
);
368 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
371 if (__put_user(sig
, &frame
->sig
))
374 if (ia32_setup_sigcontext(&frame
->sc
, fpstate
, regs
, set
->sig
[0]))
377 if (_COMPAT_NSIG_WORDS
> 1) {
378 if (__copy_to_user(frame
->extramask
, &set
->sig
[1],
379 sizeof(frame
->extramask
)))
383 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
384 restorer
= ksig
->ka
.sa
.sa_restorer
;
386 /* Return stub is in 32bit vsyscall page */
387 if (current
->mm
->context
.vdso
)
388 restorer
= VDSO32_SYMBOL(current
->mm
->context
.vdso
,
391 restorer
= &frame
->retcode
;
395 put_user_ex(ptr_to_compat(restorer
), &frame
->pretcode
);
398 * These are actually not used anymore, but left because some
399 * gdb versions depend on them as a marker.
401 put_user_ex(*((u64
*)&code
), (u64 __user
*)frame
->retcode
);
402 } put_user_catch(err
);
407 /* Set up registers for signal handler */
408 regs
->sp
= (unsigned long) frame
;
409 regs
->ip
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
411 /* Make -mregparm=3 work */
416 loadsegment(ds
, __USER32_DS
);
417 loadsegment(es
, __USER32_DS
);
419 regs
->cs
= __USER32_CS
;
420 regs
->ss
= __USER32_DS
;
425 int ia32_setup_rt_frame(int sig
, struct ksignal
*ksig
,
426 compat_sigset_t
*set
, struct pt_regs
*regs
)
428 struct rt_sigframe_ia32 __user
*frame
;
429 void __user
*restorer
;
431 void __user
*fpstate
= NULL
;
433 /* __copy_to_user optimizes that into a single 8 byte store */
434 static const struct {
439 } __attribute__((packed
)) code
= {
441 __NR_ia32_rt_sigreturn
,
446 frame
= get_sigframe(ksig
, regs
, sizeof(*frame
), &fpstate
);
448 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
452 put_user_ex(sig
, &frame
->sig
);
453 put_user_ex(ptr_to_compat(&frame
->info
), &frame
->pinfo
);
454 put_user_ex(ptr_to_compat(&frame
->uc
), &frame
->puc
);
456 /* Create the ucontext. */
458 put_user_ex(UC_FP_XSTATE
, &frame
->uc
.uc_flags
);
460 put_user_ex(0, &frame
->uc
.uc_flags
);
461 put_user_ex(0, &frame
->uc
.uc_link
);
462 err
|= __compat_save_altstack(&frame
->uc
.uc_stack
, regs
->sp
);
464 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
)
465 restorer
= ksig
->ka
.sa
.sa_restorer
;
467 restorer
= VDSO32_SYMBOL(current
->mm
->context
.vdso
,
469 put_user_ex(ptr_to_compat(restorer
), &frame
->pretcode
);
472 * Not actually used anymore, but left because some gdb
475 put_user_ex(*((u64
*)&code
), (u64 __user
*)frame
->retcode
);
476 } put_user_catch(err
);
478 err
|= copy_siginfo_to_user32(&frame
->info
, &ksig
->info
);
479 err
|= ia32_setup_sigcontext(&frame
->uc
.uc_mcontext
, fpstate
,
481 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
486 /* Set up registers for signal handler */
487 regs
->sp
= (unsigned long) frame
;
488 regs
->ip
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
490 /* Make -mregparm=3 work */
492 regs
->dx
= (unsigned long) &frame
->info
;
493 regs
->cx
= (unsigned long) &frame
->uc
;
495 loadsegment(ds
, __USER32_DS
);
496 loadsegment(es
, __USER32_DS
);
498 regs
->cs
= __USER32_CS
;
499 regs
->ss
= __USER32_DS
;