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 int copy_siginfo_to_user32(compat_siginfo_t __user
*to
, const siginfo_t
*from
)
40 bool ia32
= test_thread_flag(TIF_IA32
);
42 if (!access_ok(VERIFY_WRITE
, to
, sizeof(compat_siginfo_t
)))
46 /* If you change siginfo_t structure, please make sure that
47 this code is fixed accordingly.
48 It should never copy any pad contained in the structure
49 to avoid security leaks, but must copy the generic
50 3 ints plus the relevant union member. */
51 put_user_ex(from
->si_signo
, &to
->si_signo
);
52 put_user_ex(from
->si_errno
, &to
->si_errno
);
53 put_user_ex((short)from
->si_code
, &to
->si_code
);
55 if (from
->si_code
< 0) {
56 put_user_ex(from
->si_pid
, &to
->si_pid
);
57 put_user_ex(from
->si_uid
, &to
->si_uid
);
58 put_user_ex(ptr_to_compat(from
->si_ptr
), &to
->si_ptr
);
61 * First 32bits of unions are always present:
62 * si_pid === si_band === si_tid === si_addr(LS half)
64 put_user_ex(from
->_sifields
._pad
[0],
65 &to
->_sifields
._pad
[0]);
66 switch (from
->si_code
>> 16) {
67 case __SI_FAULT
>> 16:
70 put_user_ex(from
->si_syscall
, &to
->si_syscall
);
71 put_user_ex(from
->si_arch
, &to
->si_arch
);
75 put_user_ex(from
->si_utime
, &to
->si_utime
);
76 put_user_ex(from
->si_stime
, &to
->si_stime
);
78 put_user_ex(from
->si_utime
, &to
->_sifields
._sigchld_x32
._utime
);
79 put_user_ex(from
->si_stime
, &to
->_sifields
._sigchld_x32
._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
);
131 * Do a signal return; undo the signal stack.
133 #define loadsegment_gs(v) load_gs_index(v)
134 #define loadsegment_fs(v) loadsegment(fs, v)
135 #define loadsegment_ds(v) loadsegment(ds, v)
136 #define loadsegment_es(v) loadsegment(es, v)
138 #define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
139 #define set_user_seg(seg, v) loadsegment_##seg(v)
142 get_user_ex(regs->x, &sc->x); \
145 #define GET_SEG(seg) ({ \
146 unsigned short tmp; \
147 get_user_ex(tmp, &sc->seg); \
151 #define COPY_SEG_CPL3(seg) do { \
152 regs->seg = GET_SEG(seg) | 3; \
155 #define RELOAD_SEG(seg) { \
156 unsigned int pre = GET_SEG(seg); \
157 unsigned int cur = get_user_seg(seg); \
160 set_user_seg(seg, pre); \
163 static int ia32_restore_sigcontext(struct pt_regs
*regs
,
164 struct sigcontext_ia32 __user
*sc
)
166 unsigned int tmpflags
, err
= 0;
170 /* Always make any pending restarted system calls return -EINTR */
171 current
->restart_block
.fn
= do_no_restart_syscall
;
175 * Reload fs and gs if they have changed in the signal
176 * handler. This does not handle long fs/gs base changes in
177 * the handler, but does not clobber them at least in the
185 COPY(di
); COPY(si
); COPY(bp
); COPY(sp
); COPY(bx
);
186 COPY(dx
); COPY(cx
); COPY(ip
); COPY(ax
);
187 /* Don't touch extended registers */
192 get_user_ex(tmpflags
, &sc
->flags
);
193 regs
->flags
= (regs
->flags
& ~FIX_EFLAGS
) | (tmpflags
& FIX_EFLAGS
);
194 /* disable syscall checks */
197 get_user_ex(tmp
, &sc
->fpstate
);
198 buf
= compat_ptr(tmp
);
199 } get_user_catch(err
);
201 err
|= restore_xstate_sig(buf
, 1);
208 asmlinkage
long sys32_sigreturn(void)
210 struct pt_regs
*regs
= current_pt_regs();
211 struct sigframe_ia32 __user
*frame
= (struct sigframe_ia32 __user
*)(regs
->sp
-8);
214 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
216 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
217 || (_COMPAT_NSIG_WORDS
> 1
218 && __copy_from_user((((char *) &set
.sig
) + 4),
220 sizeof(frame
->extramask
))))
223 set_current_blocked(&set
);
225 if (ia32_restore_sigcontext(regs
, &frame
->sc
))
230 signal_fault(regs
, frame
, "32bit sigreturn");
234 asmlinkage
long sys32_rt_sigreturn(void)
236 struct pt_regs
*regs
= current_pt_regs();
237 struct rt_sigframe_ia32 __user
*frame
;
240 frame
= (struct rt_sigframe_ia32 __user
*)(regs
->sp
- 4);
242 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
244 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
247 set_current_blocked(&set
);
249 if (ia32_restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
252 if (compat_restore_altstack(&frame
->uc
.uc_stack
))
258 signal_fault(regs
, frame
, "32bit rt sigreturn");
263 * Set up a signal frame.
266 static int ia32_setup_sigcontext(struct sigcontext_ia32 __user
*sc
,
267 void __user
*fpstate
,
268 struct pt_regs
*regs
, unsigned int mask
)
273 put_user_ex(get_user_seg(gs
), (unsigned int __user
*)&sc
->gs
);
274 put_user_ex(get_user_seg(fs
), (unsigned int __user
*)&sc
->fs
);
275 put_user_ex(get_user_seg(ds
), (unsigned int __user
*)&sc
->ds
);
276 put_user_ex(get_user_seg(es
), (unsigned int __user
*)&sc
->es
);
278 put_user_ex(regs
->di
, &sc
->di
);
279 put_user_ex(regs
->si
, &sc
->si
);
280 put_user_ex(regs
->bp
, &sc
->bp
);
281 put_user_ex(regs
->sp
, &sc
->sp
);
282 put_user_ex(regs
->bx
, &sc
->bx
);
283 put_user_ex(regs
->dx
, &sc
->dx
);
284 put_user_ex(regs
->cx
, &sc
->cx
);
285 put_user_ex(regs
->ax
, &sc
->ax
);
286 put_user_ex(current
->thread
.trap_nr
, &sc
->trapno
);
287 put_user_ex(current
->thread
.error_code
, &sc
->err
);
288 put_user_ex(regs
->ip
, &sc
->ip
);
289 put_user_ex(regs
->cs
, (unsigned int __user
*)&sc
->cs
);
290 put_user_ex(regs
->flags
, &sc
->flags
);
291 put_user_ex(regs
->sp
, &sc
->sp_at_signal
);
292 put_user_ex(regs
->ss
, (unsigned int __user
*)&sc
->ss
);
294 put_user_ex(ptr_to_compat(fpstate
), &sc
->fpstate
);
296 /* non-iBCS2 extensions.. */
297 put_user_ex(mask
, &sc
->oldmask
);
298 put_user_ex(current
->thread
.cr2
, &sc
->cr2
);
299 } put_user_catch(err
);
305 * Determine which stack to use..
307 static void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
,
309 void __user
**fpstate
)
313 /* Default to using normal stack */
316 /* This is the X/Open sanctioned signal stack switching. */
317 if (ksig
->ka
.sa
.sa_flags
& SA_ONSTACK
)
318 sp
= sigsp(sp
, ksig
);
319 /* This is the legacy signal stack switching. */
320 else if ((regs
->ss
& 0xffff) != __USER32_DS
&&
321 !(ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) &&
322 ksig
->ka
.sa
.sa_restorer
)
323 sp
= (unsigned long) ksig
->ka
.sa
.sa_restorer
;
326 unsigned long fx_aligned
, math_size
;
328 sp
= alloc_mathframe(sp
, 1, &fx_aligned
, &math_size
);
329 *fpstate
= (struct _fpstate_ia32 __user
*) sp
;
330 if (save_xstate_sig(*fpstate
, (void __user
*)fx_aligned
,
332 return (void __user
*) -1L;
336 /* Align the stack pointer according to the i386 ABI,
337 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
338 sp
= ((sp
+ 4) & -16ul) - 4;
339 return (void __user
*) sp
;
342 int ia32_setup_frame(int sig
, struct ksignal
*ksig
,
343 compat_sigset_t
*set
, struct pt_regs
*regs
)
345 struct sigframe_ia32 __user
*frame
;
346 void __user
*restorer
;
348 void __user
*fpstate
= NULL
;
350 /* copy_to_user optimizes that into a single 8 byte store */
351 static const struct {
355 } __attribute__((packed
)) code
= {
356 0xb858, /* popl %eax ; movl $...,%eax */
358 0x80cd, /* int $0x80 */
361 frame
= get_sigframe(ksig
, regs
, sizeof(*frame
), &fpstate
);
363 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
366 if (__put_user(sig
, &frame
->sig
))
369 if (ia32_setup_sigcontext(&frame
->sc
, fpstate
, regs
, set
->sig
[0]))
372 if (_COMPAT_NSIG_WORDS
> 1) {
373 if (__copy_to_user(frame
->extramask
, &set
->sig
[1],
374 sizeof(frame
->extramask
)))
378 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
379 restorer
= ksig
->ka
.sa
.sa_restorer
;
381 /* Return stub is in 32bit vsyscall page */
382 if (current
->mm
->context
.vdso
)
383 restorer
= current
->mm
->context
.vdso
+
384 selected_vdso32
->sym___kernel_sigreturn
;
386 restorer
= &frame
->retcode
;
390 put_user_ex(ptr_to_compat(restorer
), &frame
->pretcode
);
393 * These are actually not used anymore, but left because some
394 * gdb versions depend on them as a marker.
396 put_user_ex(*((u64
*)&code
), (u64 __user
*)frame
->retcode
);
397 } put_user_catch(err
);
402 /* Set up registers for signal handler */
403 regs
->sp
= (unsigned long) frame
;
404 regs
->ip
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
406 /* Make -mregparm=3 work */
411 loadsegment(ds
, __USER32_DS
);
412 loadsegment(es
, __USER32_DS
);
414 regs
->cs
= __USER32_CS
;
415 regs
->ss
= __USER32_DS
;
420 int ia32_setup_rt_frame(int sig
, struct ksignal
*ksig
,
421 compat_sigset_t
*set
, struct pt_regs
*regs
)
423 struct rt_sigframe_ia32 __user
*frame
;
424 void __user
*restorer
;
426 void __user
*fpstate
= NULL
;
428 /* __copy_to_user optimizes that into a single 8 byte store */
429 static const struct {
434 } __attribute__((packed
)) code
= {
436 __NR_ia32_rt_sigreturn
,
441 frame
= get_sigframe(ksig
, regs
, sizeof(*frame
), &fpstate
);
443 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
447 put_user_ex(sig
, &frame
->sig
);
448 put_user_ex(ptr_to_compat(&frame
->info
), &frame
->pinfo
);
449 put_user_ex(ptr_to_compat(&frame
->uc
), &frame
->puc
);
451 /* Create the ucontext. */
453 put_user_ex(UC_FP_XSTATE
, &frame
->uc
.uc_flags
);
455 put_user_ex(0, &frame
->uc
.uc_flags
);
456 put_user_ex(0, &frame
->uc
.uc_link
);
457 compat_save_altstack_ex(&frame
->uc
.uc_stack
, regs
->sp
);
459 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
)
460 restorer
= ksig
->ka
.sa
.sa_restorer
;
462 restorer
= current
->mm
->context
.vdso
+
463 selected_vdso32
->sym___kernel_rt_sigreturn
;
464 put_user_ex(ptr_to_compat(restorer
), &frame
->pretcode
);
467 * Not actually used anymore, but left because some gdb
470 put_user_ex(*((u64
*)&code
), (u64 __user
*)frame
->retcode
);
471 } put_user_catch(err
);
473 err
|= copy_siginfo_to_user32(&frame
->info
, &ksig
->info
);
474 err
|= ia32_setup_sigcontext(&frame
->uc
.uc_mcontext
, fpstate
,
476 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
481 /* Set up registers for signal handler */
482 regs
->sp
= (unsigned long) frame
;
483 regs
->ip
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
485 /* Make -mregparm=3 work */
487 regs
->dx
= (unsigned long) &frame
->info
;
488 regs
->cx
= (unsigned long) &frame
->uc
;
490 loadsegment(ds
, __USER32_DS
);
491 loadsegment(es
, __USER32_DS
);
493 regs
->cs
= __USER32_CS
;
494 regs
->ss
= __USER32_DS
;