blob: cb13c0564ea7b07eb08617f07cacab589e7b6dd1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/ia32/ia32_signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/unistd.h>
18#include <linux/stddef.h>
19#include <linux/personality.h>
20#include <linux/compat.h>
Andi Kleen9fbbd4d2007-02-13 13:26:26 +010021#include <linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/ucontext.h>
23#include <asm/uaccess.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020024#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020025#include <asm/fpu/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/ptrace.h>
27#include <asm/ia32_unistd.h>
28#include <asm/user32.h>
Ingo Molnardecb4c42015-09-05 09:32:43 +020029#include <uapi/asm/sigcontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/proto.h>
Roland McGrathaf65d642008-01-30 13:30:43 +010031#include <asm/vdso.h>
Hiroshi Shimamotod98f9d82008-12-17 18:52:45 -080032#include <asm/sigframe.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080033#include <asm/sighandling.h>
Jaswinder Singh Rajput2f06de02008-12-27 21:37:10 +053034#include <asm/sys_ia32.h>
H. Peter Anvin49b8c692012-09-21 17:18:44 -070035#include <asm/smap.h>
Hiroshi Shimamotod98f9d82008-12-17 18:52:45 -080036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Do a signal return; undo the signal stack.
39 */
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080040#define loadsegment_gs(v) load_gs_index(v)
41#define loadsegment_fs(v) loadsegment(fs, v)
42#define loadsegment_ds(v) loadsegment(ds, v)
43#define loadsegment_es(v) loadsegment(es, v)
44
45#define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
46#define set_user_seg(seg, v) loadsegment_##seg(v)
47
Hiroshi Shimamotob78a5b52008-11-17 15:44:50 -080048#define COPY(x) { \
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080049 get_user_ex(regs->x, &sc->x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Hiroshi Shimamoto8801ead2009-02-20 19:00:13 -080052#define GET_SEG(seg) ({ \
53 unsigned short tmp; \
54 get_user_ex(tmp, &sc->seg); \
55 tmp; \
56})
57
58#define COPY_SEG_CPL3(seg) do { \
59 regs->seg = GET_SEG(seg) | 3; \
60} while (0)
Hiroshi Shimamotod71a68d2008-11-17 15:47:06 -080061
Hiroshi Shimamoto8c6e5ce2008-11-17 15:47:48 -080062#define RELOAD_SEG(seg) { \
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080063 unsigned int pre = GET_SEG(seg); \
64 unsigned int cur = get_user_seg(seg); \
Hiroshi Shimamoto8c6e5ce2008-11-17 15:47:48 -080065 pre |= 3; \
66 if (pre != cur) \
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080067 set_user_seg(seg, pre); \
Hiroshi Shimamoto8c6e5ce2008-11-17 15:47:48 -080068}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010070static int ia32_restore_sigcontext(struct pt_regs *regs,
Ingo Molnar8fcb3462015-09-05 09:32:41 +020071 struct sigcontext_32 __user *sc)
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010072{
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080073 unsigned int tmpflags, err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -070074 void __user *buf;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010075 u32 tmp;
76
77 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080078 current->restart_block.fn = do_no_restart_syscall;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010079
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080080 get_user_try {
81 /*
82 * Reload fs and gs if they have changed in the signal
83 * handler. This does not handle long fs/gs base changes in
84 * the handler, but does not clobber them at least in the
85 * normal case.
86 */
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080087 RELOAD_SEG(gs);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080088 RELOAD_SEG(fs);
89 RELOAD_SEG(ds);
90 RELOAD_SEG(es);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080092 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -040093 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080094 /* Don't touch extended registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080096 COPY_SEG_CPL3(cs);
97 COPY_SEG_CPL3(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080099 get_user_ex(tmpflags, &sc->flags);
100 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
101 /* disable syscall checks */
102 regs->orig_ax = -1;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100103
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800104 get_user_ex(tmp, &sc->fpstate);
105 buf = compat_ptr(tmp);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800106 } get_user_catch(err);
107
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200108 err |= fpu__restore_sig(buf, 1);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700109
Brian Gerst1daeaa32015-03-21 18:54:21 -0400110 force_iret();
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Al Viro3fe26fa2012-11-12 14:32:42 -0500115asmlinkage long sys32_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Al Viro3fe26fa2012-11-12 14:32:42 -0500117 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800118 struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
122 goto badframe;
123 if (__get_user(set.sig[0], &frame->sc.oldmask)
124 || (_COMPAT_NSIG_WORDS > 1
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100125 && __copy_from_user((((char *) &set.sig) + 4),
126 &frame->extramask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 sizeof(frame->extramask))))
128 goto badframe;
129
Oleg Nesterov905f29e2011-07-10 21:27:24 +0200130 set_current_blocked(&set);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100131
Brian Gerst6a3713f2015-04-04 08:58:23 -0400132 if (ia32_restore_sigcontext(regs, &frame->sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400134 return regs->ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136badframe:
137 signal_fault(regs, frame, "32bit sigreturn");
138 return 0;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Al Viro3fe26fa2012-11-12 14:32:42 -0500141asmlinkage long sys32_rt_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Al Viro3fe26fa2012-11-12 14:32:42 -0500143 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800144 struct rt_sigframe_ia32 __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800147 frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
150 goto badframe;
151 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
152 goto badframe;
153
Oleg Nesterov905f29e2011-07-10 21:27:24 +0200154 set_current_blocked(&set);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100155
Brian Gerst6a3713f2015-04-04 08:58:23 -0400156 if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 goto badframe;
158
Al Viro90268432012-12-14 14:47:53 -0500159 if (compat_restore_altstack(&frame->uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto badframe;
161
Brian Gerst6a3713f2015-04-04 08:58:23 -0400162 return regs->ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164badframe:
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100165 signal_fault(regs, frame, "32bit rt sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/*
170 * Set up a signal frame.
171 */
172
Ingo Molnar8fcb3462015-09-05 09:32:41 +0200173static int ia32_setup_sigcontext(struct sigcontext_32 __user *sc,
Suresh Siddhaab513702008-07-29 10:29:22 -0700174 void __user *fpstate,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100175 struct pt_regs *regs, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -0800177 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800179 put_user_try {
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -0800180 put_user_ex(get_user_seg(gs), (unsigned int __user *)&sc->gs);
181 put_user_ex(get_user_seg(fs), (unsigned int __user *)&sc->fs);
182 put_user_ex(get_user_seg(ds), (unsigned int __user *)&sc->ds);
183 put_user_ex(get_user_seg(es), (unsigned int __user *)&sc->es);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800185 put_user_ex(regs->di, &sc->di);
186 put_user_ex(regs->si, &sc->si);
187 put_user_ex(regs->bp, &sc->bp);
188 put_user_ex(regs->sp, &sc->sp);
189 put_user_ex(regs->bx, &sc->bx);
190 put_user_ex(regs->dx, &sc->dx);
191 put_user_ex(regs->cx, &sc->cx);
192 put_user_ex(regs->ax, &sc->ax);
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530193 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800194 put_user_ex(current->thread.error_code, &sc->err);
195 put_user_ex(regs->ip, &sc->ip);
196 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
197 put_user_ex(regs->flags, &sc->flags);
198 put_user_ex(regs->sp, &sc->sp_at_signal);
199 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800201 put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800203 /* non-iBCS2 extensions.. */
204 put_user_ex(mask, &sc->oldmask);
205 put_user_ex(current->thread.cr2, &sc->cr2);
206 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 return err;
209}
210
211/*
212 * Determine which stack to use..
213 */
Al Viro235b8022012-11-09 23:51:47 -0500214static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700215 size_t frame_size,
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200216 void __user **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200218 struct fpu *fpu = &current->thread.fpu;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100219 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100222 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* This is the X/Open sanctioned signal stack switching. */
Al Viro235b8022012-11-09 23:51:47 -0500225 if (ksig->ka.sa.sa_flags & SA_ONSTACK)
226 sp = sigsp(sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* This is the legacy signal stack switching. */
Hiroshi Shimamoto8bee3f02008-12-16 14:04:43 -0800228 else if ((regs->ss & 0xffff) != __USER32_DS &&
Al Viro235b8022012-11-09 23:51:47 -0500229 !(ksig->ka.sa.sa_flags & SA_RESTORER) &&
230 ksig->ka.sa.sa_restorer)
231 sp = (unsigned long) ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200233 if (fpu->fpstate_active) {
Suresh Siddha72a671c2012-07-24 16:05:29 -0700234 unsigned long fx_aligned, math_size;
235
Ingo Molnar82c0e452015-04-29 21:09:18 +0200236 sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
Ingo Molnar86e9fc32015-09-05 09:32:36 +0200237 *fpstate = (struct _fpstate_32 __user *) sp;
Ingo Molnarc8e14042015-04-28 11:35:20 +0200238 if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
Suresh Siddha72a671c2012-07-24 16:05:29 -0700239 math_size) < 0)
Hiroshi Shimamoto99ea1b932008-11-05 18:32:54 -0800240 return (void __user *) -1L;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700241 }
242
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100243 sp -= frame_size;
Markus F.X.J. Oberhumerd347f372005-10-09 18:54:23 +0200244 /* Align the stack pointer according to the i386 ABI,
245 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100246 sp = ((sp + 4) & -16ul) - 4;
247 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Al Viro235b8022012-11-09 23:51:47 -0500250int ia32_setup_frame(int sig, struct ksignal *ksig,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100251 compat_sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800253 struct sigframe_ia32 __user *frame;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100254 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700256 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100258 /* copy_to_user optimizes that into a single 8 byte store */
259 static const struct {
260 u16 poplmovl;
261 u32 val;
262 u16 int80;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100263 } __attribute__((packed)) code = {
264 0xb858, /* popl %eax ; movl $...,%eax */
265 __NR_ia32_sigreturn,
266 0x80cd, /* int $0x80 */
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100267 };
268
Al Viro235b8022012-11-09 23:51:47 -0500269 frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700272 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700274 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700275 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700277 if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700278 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (_COMPAT_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700281 if (__copy_to_user(frame->extramask, &set->sig[1],
282 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700283 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Al Viro235b8022012-11-09 23:51:47 -0500286 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
287 restorer = ksig->ka.sa.sa_restorer;
Roland McGrathaf65d642008-01-30 13:30:43 +0100288 } else {
289 /* Return stub is in 32bit vsyscall page */
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700290 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700291 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700292 vdso_image_32.sym___kernel_sigreturn;
Roland McGrathaf65d642008-01-30 13:30:43 +0100293 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100294 restorer = &frame->retcode;
Roland McGrathaf65d642008-01-30 13:30:43 +0100295 }
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100296
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800297 put_user_try {
298 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
299
300 /*
301 * These are actually not used anymore, but left because some
302 * gdb versions depend on them as a marker.
303 */
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200304 put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800305 } put_user_catch(err);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700308 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100311 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500312 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Andi Kleen536e3ee2006-09-26 10:52:41 +0200314 /* Make -mregparm=3 work */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100315 regs->ax = sig;
316 regs->dx = 0;
317 regs->cx = 0;
Andi Kleen536e3ee2006-09-26 10:52:41 +0200318
Jeremy Fitzhardingeb6edbb12008-08-19 13:04:19 -0700319 loadsegment(ds, __USER32_DS);
320 loadsegment(es, __USER32_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100322 regs->cs = __USER32_CS;
323 regs->ss = __USER32_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Andi Kleen1d001df2006-09-26 10:52:26 +0200325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Al Viro235b8022012-11-09 23:51:47 -0500328int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100329 compat_sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800331 struct rt_sigframe_ia32 __user *frame;
Roland McGrathaf65d642008-01-30 13:30:43 +0100332 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700334 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100336 /* __copy_to_user optimizes that into a single 8 byte store */
337 static const struct {
338 u8 movl;
339 u32 val;
340 u16 int80;
Hiroshi Shimamoto9cc3c492008-11-11 19:11:39 -0800341 u8 pad;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100342 } __attribute__((packed)) code = {
343 0xb8,
344 __NR_ia32_rt_sigreturn,
345 0x80cd,
346 0,
347 };
348
Al Viro235b8022012-11-09 23:51:47 -0500349 frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700352 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800354 put_user_try {
355 put_user_ex(sig, &frame->sig);
356 put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
357 put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800359 /* Create the ucontext. */
Borislav Petkovd366bf72016-04-04 22:25:02 +0200360 if (boot_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800361 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
362 else
363 put_user_ex(0, &frame->uc.uc_flags);
364 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100365 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Al Viro235b8022012-11-09 23:51:47 -0500367 if (ksig->ka.sa.sa_flags & SA_RESTORER)
368 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800369 else
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700370 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700371 vdso_image_32.sym___kernel_rt_sigreturn;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800372 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800374 /*
375 * Not actually used anymore, but left because some gdb
376 * versions need it.
377 */
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200378 put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800379 } put_user_catch(err);
380
Dmitry Safonov68463512016-09-05 16:33:08 +0300381 err |= __copy_siginfo_to_user32(&frame->info, &ksig->info, false);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700382 err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
383 regs, set->sig[0]);
384 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700387 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100390 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500391 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Albert Cahalana7aacdf2006-10-29 22:26:17 -0500393 /* Make -mregparm=3 work */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100394 regs->ax = sig;
395 regs->dx = (unsigned long) &frame->info;
396 regs->cx = (unsigned long) &frame->uc;
Albert Cahalana7aacdf2006-10-29 22:26:17 -0500397
Jeremy Fitzhardingeb6edbb12008-08-19 13:04:19 -0700398 loadsegment(ds, __USER32_DS);
399 loadsegment(es, __USER32_DS);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100400
401 regs->cs = __USER32_CS;
402 regs->ss = __USER32_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Andi Kleen1d001df2006-09-26 10:52:26 +0200404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}