blob: 86b1341cba9ac5c6b32a3dd941091d59f8ebb56c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/x86_64/ia32/ia32_signal.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
9 * 2000-12-* x86-64 compatibility mode signal handling by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010013#include <linux/sched/task_stack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
15#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/unistd.h>
20#include <linux/stddef.h>
21#include <linux/personality.h>
22#include <linux/compat.h>
Andi Kleen9fbbd4d2007-02-13 13:26:26 +010023#include <linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/ucontext.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020026#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020027#include <asm/fpu/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/ptrace.h>
29#include <asm/ia32_unistd.h>
30#include <asm/user32.h>
Ingo Molnardecb4c42015-09-05 09:32:43 +020031#include <uapi/asm/sigcontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/proto.h>
Roland McGrathaf65d642008-01-30 13:30:43 +010033#include <asm/vdso.h>
Hiroshi Shimamotod98f9d82008-12-17 18:52:45 -080034#include <asm/sigframe.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080035#include <asm/sighandling.h>
H. Peter Anvin49b8c692012-09-21 17:18:44 -070036#include <asm/smap.h>
Hiroshi Shimamotod98f9d82008-12-17 18:52:45 -080037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * Do a signal return; undo the signal stack.
40 */
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080041#define loadsegment_gs(v) load_gs_index(v)
42#define loadsegment_fs(v) loadsegment(fs, v)
43#define loadsegment_ds(v) loadsegment(ds, v)
44#define loadsegment_es(v) loadsegment(es, v)
45
46#define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
47#define set_user_seg(seg, v) loadsegment_##seg(v)
48
Hiroshi Shimamotob78a5b52008-11-17 15:44:50 -080049#define COPY(x) { \
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080050 get_user_ex(regs->x, &sc->x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Hiroshi Shimamoto8801ead2009-02-20 19:00:13 -080053#define GET_SEG(seg) ({ \
54 unsigned short tmp; \
55 get_user_ex(tmp, &sc->seg); \
56 tmp; \
57})
58
59#define COPY_SEG_CPL3(seg) do { \
60 regs->seg = GET_SEG(seg) | 3; \
61} while (0)
Hiroshi Shimamotod71a68d2008-11-17 15:47:06 -080062
Hiroshi Shimamoto8c6e5ce2008-11-17 15:47:48 -080063#define RELOAD_SEG(seg) { \
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080064 unsigned int pre = GET_SEG(seg); \
65 unsigned int cur = get_user_seg(seg); \
Hiroshi Shimamoto8c6e5ce2008-11-17 15:47:48 -080066 pre |= 3; \
67 if (pre != cur) \
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080068 set_user_seg(seg, pre); \
Hiroshi Shimamoto8c6e5ce2008-11-17 15:47:48 -080069}
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010071static int ia32_restore_sigcontext(struct pt_regs *regs,
Ingo Molnar8fcb3462015-09-05 09:32:41 +020072 struct sigcontext_32 __user *sc)
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010073{
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080074 unsigned int tmpflags, err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -070075 void __user *buf;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010076 u32 tmp;
77
78 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080079 current->restart_block.fn = do_no_restart_syscall;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010080
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080081 get_user_try {
82 /*
83 * Reload fs and gs if they have changed in the signal
84 * handler. This does not handle long fs/gs base changes in
85 * the handler, but does not clobber them at least in the
86 * normal case.
87 */
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080088 RELOAD_SEG(gs);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080089 RELOAD_SEG(fs);
90 RELOAD_SEG(ds);
91 RELOAD_SEG(es);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080093 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -040094 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080095 /* Don't touch extended registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080097 COPY_SEG_CPL3(cs);
98 COPY_SEG_CPL3(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800100 get_user_ex(tmpflags, &sc->flags);
101 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
102 /* disable syscall checks */
103 regs->orig_ax = -1;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100104
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800105 get_user_ex(tmp, &sc->fpstate);
106 buf = compat_ptr(tmp);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800107 } get_user_catch(err);
108
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200109 err |= fpu__restore_sig(buf, 1);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700110
Brian Gerst1daeaa32015-03-21 18:54:21 -0400111 force_iret();
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Al Viro3fe26fa2012-11-12 14:32:42 -0500116asmlinkage long sys32_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Al Viro3fe26fa2012-11-12 14:32:42 -0500118 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800119 struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
123 goto badframe;
124 if (__get_user(set.sig[0], &frame->sc.oldmask)
125 || (_COMPAT_NSIG_WORDS > 1
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100126 && __copy_from_user((((char *) &set.sig) + 4),
127 &frame->extramask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 sizeof(frame->extramask))))
129 goto badframe;
130
Oleg Nesterov905f29e2011-07-10 21:27:24 +0200131 set_current_blocked(&set);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100132
Brian Gerst6a3713f2015-04-04 08:58:23 -0400133 if (ia32_restore_sigcontext(regs, &frame->sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400135 return regs->ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137badframe:
138 signal_fault(regs, frame, "32bit sigreturn");
139 return 0;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100140}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Al Viro3fe26fa2012-11-12 14:32:42 -0500142asmlinkage long sys32_rt_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Al Viro3fe26fa2012-11-12 14:32:42 -0500144 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800145 struct rt_sigframe_ia32 __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800148 frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
151 goto badframe;
152 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
153 goto badframe;
154
Oleg Nesterov905f29e2011-07-10 21:27:24 +0200155 set_current_blocked(&set);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100156
Brian Gerst6a3713f2015-04-04 08:58:23 -0400157 if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 goto badframe;
159
Al Viro90268432012-12-14 14:47:53 -0500160 if (compat_restore_altstack(&frame->uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 goto badframe;
162
Brian Gerst6a3713f2015-04-04 08:58:23 -0400163 return regs->ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165badframe:
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100166 signal_fault(regs, frame, "32bit rt sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/*
171 * Set up a signal frame.
172 */
173
Ingo Molnar8fcb3462015-09-05 09:32:41 +0200174static int ia32_setup_sigcontext(struct sigcontext_32 __user *sc,
Suresh Siddhaab513702008-07-29 10:29:22 -0700175 void __user *fpstate,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100176 struct pt_regs *regs, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -0800178 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800180 put_user_try {
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -0800181 put_user_ex(get_user_seg(gs), (unsigned int __user *)&sc->gs);
182 put_user_ex(get_user_seg(fs), (unsigned int __user *)&sc->fs);
183 put_user_ex(get_user_seg(ds), (unsigned int __user *)&sc->ds);
184 put_user_ex(get_user_seg(es), (unsigned int __user *)&sc->es);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800186 put_user_ex(regs->di, &sc->di);
187 put_user_ex(regs->si, &sc->si);
188 put_user_ex(regs->bp, &sc->bp);
189 put_user_ex(regs->sp, &sc->sp);
190 put_user_ex(regs->bx, &sc->bx);
191 put_user_ex(regs->dx, &sc->dx);
192 put_user_ex(regs->cx, &sc->cx);
193 put_user_ex(regs->ax, &sc->ax);
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530194 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800195 put_user_ex(current->thread.error_code, &sc->err);
196 put_user_ex(regs->ip, &sc->ip);
197 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
198 put_user_ex(regs->flags, &sc->flags);
199 put_user_ex(regs->sp, &sc->sp_at_signal);
200 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800202 put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800204 /* non-iBCS2 extensions.. */
205 put_user_ex(mask, &sc->oldmask);
206 put_user_ex(current->thread.cr2, &sc->cr2);
207 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 return err;
210}
211
212/*
213 * Determine which stack to use..
214 */
Al Viro235b8022012-11-09 23:51:47 -0500215static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700216 size_t frame_size,
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200217 void __user **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200219 struct fpu *fpu = &current->thread.fpu;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100220 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100223 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* This is the X/Open sanctioned signal stack switching. */
Al Viro235b8022012-11-09 23:51:47 -0500226 if (ksig->ka.sa.sa_flags & SA_ONSTACK)
227 sp = sigsp(sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* This is the legacy signal stack switching. */
Andy Lutomirski99504812017-07-28 06:00:32 -0700229 else if (regs->ss != __USER32_DS &&
Al Viro235b8022012-11-09 23:51:47 -0500230 !(ksig->ka.sa.sa_flags & SA_RESTORER) &&
231 ksig->ka.sa.sa_restorer)
232 sp = (unsigned long) ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Ingo Molnare4a81bf2017-09-26 09:43:36 +0200234 if (fpu->initialized) {
Suresh Siddha72a671c2012-07-24 16:05:29 -0700235 unsigned long fx_aligned, math_size;
236
Ingo Molnar82c0e452015-04-29 21:09:18 +0200237 sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
Ingo Molnar86e9fc32015-09-05 09:32:36 +0200238 *fpstate = (struct _fpstate_32 __user *) sp;
Ingo Molnarc8e14042015-04-28 11:35:20 +0200239 if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
Suresh Siddha72a671c2012-07-24 16:05:29 -0700240 math_size) < 0)
Hiroshi Shimamoto99ea1b932008-11-05 18:32:54 -0800241 return (void __user *) -1L;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700242 }
243
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100244 sp -= frame_size;
Markus F.X.J. Oberhumerd347f372005-10-09 18:54:23 +0200245 /* Align the stack pointer according to the i386 ABI,
246 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100247 sp = ((sp + 4) & -16ul) - 4;
248 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Al Viro235b8022012-11-09 23:51:47 -0500251int ia32_setup_frame(int sig, struct ksignal *ksig,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100252 compat_sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800254 struct sigframe_ia32 __user *frame;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100255 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700257 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100259 /* copy_to_user optimizes that into a single 8 byte store */
260 static const struct {
261 u16 poplmovl;
262 u32 val;
263 u16 int80;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100264 } __attribute__((packed)) code = {
265 0xb858, /* popl %eax ; movl $...,%eax */
266 __NR_ia32_sigreturn,
267 0x80cd, /* int $0x80 */
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100268 };
269
Al Viro235b8022012-11-09 23:51:47 -0500270 frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700273 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700275 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700276 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700278 if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700279 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (_COMPAT_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700282 if (__copy_to_user(frame->extramask, &set->sig[1],
283 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700284 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Al Viro235b8022012-11-09 23:51:47 -0500287 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
288 restorer = ksig->ka.sa.sa_restorer;
Roland McGrathaf65d642008-01-30 13:30:43 +0100289 } else {
290 /* Return stub is in 32bit vsyscall page */
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700291 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700292 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700293 vdso_image_32.sym___kernel_sigreturn;
Roland McGrathaf65d642008-01-30 13:30:43 +0100294 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100295 restorer = &frame->retcode;
Roland McGrathaf65d642008-01-30 13:30:43 +0100296 }
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100297
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800298 put_user_try {
299 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
300
301 /*
302 * These are actually not used anymore, but left because some
303 * gdb versions depend on them as a marker.
304 */
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200305 put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800306 } put_user_catch(err);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700309 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100312 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500313 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Andi Kleen536e3ee2006-09-26 10:52:41 +0200315 /* Make -mregparm=3 work */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100316 regs->ax = sig;
317 regs->dx = 0;
318 regs->cx = 0;
Andi Kleen536e3ee2006-09-26 10:52:41 +0200319
Jeremy Fitzhardingeb6edbb12008-08-19 13:04:19 -0700320 loadsegment(ds, __USER32_DS);
321 loadsegment(es, __USER32_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100323 regs->cs = __USER32_CS;
324 regs->ss = __USER32_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Andi Kleen1d001df2006-09-26 10:52:26 +0200326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Al Viro235b8022012-11-09 23:51:47 -0500329int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100330 compat_sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800332 struct rt_sigframe_ia32 __user *frame;
Roland McGrathaf65d642008-01-30 13:30:43 +0100333 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700335 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100337 /* __copy_to_user optimizes that into a single 8 byte store */
338 static const struct {
339 u8 movl;
340 u32 val;
341 u16 int80;
Hiroshi Shimamoto9cc3c492008-11-11 19:11:39 -0800342 u8 pad;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100343 } __attribute__((packed)) code = {
344 0xb8,
345 __NR_ia32_rt_sigreturn,
346 0x80cd,
347 0,
348 };
349
Al Viro235b8022012-11-09 23:51:47 -0500350 frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700353 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800355 put_user_try {
356 put_user_ex(sig, &frame->sig);
357 put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
358 put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800360 /* Create the ucontext. */
Borislav Petkovd366bf72016-04-04 22:25:02 +0200361 if (boot_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800362 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
363 else
364 put_user_ex(0, &frame->uc.uc_flags);
365 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100366 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Al Viro235b8022012-11-09 23:51:47 -0500368 if (ksig->ka.sa.sa_flags & SA_RESTORER)
369 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800370 else
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700371 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700372 vdso_image_32.sym___kernel_rt_sigreturn;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800373 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800375 /*
376 * Not actually used anymore, but left because some gdb
377 * versions need it.
378 */
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200379 put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800380 } put_user_catch(err);
381
Dmitry Safonov68463512016-09-05 16:33:08 +0300382 err |= __copy_siginfo_to_user32(&frame->info, &ksig->info, false);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700383 err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
384 regs, set->sig[0]);
385 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700388 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100391 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500392 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Albert Cahalana7aacdf2006-10-29 22:26:17 -0500394 /* Make -mregparm=3 work */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100395 regs->ax = sig;
396 regs->dx = (unsigned long) &frame->info;
397 regs->cx = (unsigned long) &frame->uc;
Albert Cahalana7aacdf2006-10-29 22:26:17 -0500398
Jeremy Fitzhardingeb6edbb12008-08-19 13:04:19 -0700399 loadsegment(ds, __USER32_DS);
400 loadsegment(es, __USER32_DS);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100401
402 regs->cs = __USER32_CS;
403 regs->ss = __USER32_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Andi Kleen1d001df2006-09-26 10:52:26 +0200405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}