blob: 789284d19b5589b46bc247dd73689287e1dd2149 [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>
Jaswinder Singh Rajput2f06de02008-12-27 21:37:10 +053036#include <asm/sys_ia32.h>
H. Peter Anvin49b8c692012-09-21 17:18:44 -070037#include <asm/smap.h>
Hiroshi Shimamotod98f9d82008-12-17 18:52:45 -080038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * Do a signal return; undo the signal stack.
41 */
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080042#define loadsegment_gs(v) load_gs_index(v)
43#define loadsegment_fs(v) loadsegment(fs, v)
44#define loadsegment_ds(v) loadsegment(ds, v)
45#define loadsegment_es(v) loadsegment(es, v)
46
47#define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
48#define set_user_seg(seg, v) loadsegment_##seg(v)
49
Hiroshi Shimamotob78a5b52008-11-17 15:44:50 -080050#define COPY(x) { \
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080051 get_user_ex(regs->x, &sc->x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Hiroshi Shimamoto8801ead2009-02-20 19:00:13 -080054#define GET_SEG(seg) ({ \
55 unsigned short tmp; \
56 get_user_ex(tmp, &sc->seg); \
57 tmp; \
58})
59
60#define COPY_SEG_CPL3(seg) do { \
61 regs->seg = GET_SEG(seg) | 3; \
62} while (0)
Hiroshi Shimamotod71a68d2008-11-17 15:47:06 -080063
Hiroshi Shimamoto8c6e5ce2008-11-17 15:47:48 -080064#define RELOAD_SEG(seg) { \
Peter Zijlstra06c239a2019-02-25 12:56:35 +010065 unsigned int pre = (seg) | 3; \
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -080066 unsigned int cur = get_user_seg(seg); \
Hiroshi Shimamoto8c6e5ce2008-11-17 15:47:48 -080067 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;
Peter Zijlstra06c239a2019-02-25 12:56:35 +010075 u16 gs, fs, es, ds;
Suresh Siddhaab513702008-07-29 10:29:22 -070076 void __user *buf;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010077 u32 tmp;
78
79 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080080 current->restart_block.fn = do_no_restart_syscall;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010081
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080082 get_user_try {
Peter Zijlstra06c239a2019-02-25 12:56:35 +010083 gs = GET_SEG(gs);
84 fs = GET_SEG(fs);
85 ds = GET_SEG(ds);
86 es = GET_SEG(es);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080088 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -040089 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080090 /* Don't touch extended registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080092 COPY_SEG_CPL3(cs);
93 COPY_SEG_CPL3(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080095 get_user_ex(tmpflags, &sc->flags);
96 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
97 /* disable syscall checks */
98 regs->orig_ax = -1;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010099
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800100 get_user_ex(tmp, &sc->fpstate);
101 buf = compat_ptr(tmp);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800102 } get_user_catch(err);
103
Peter Zijlstra06c239a2019-02-25 12:56:35 +0100104 /*
105 * Reload fs and gs if they have changed in the signal
106 * handler. This does not handle long fs/gs base changes in
107 * the handler, but does not clobber them at least in the
108 * normal case.
109 */
110 RELOAD_SEG(gs);
111 RELOAD_SEG(fs);
112 RELOAD_SEG(ds);
113 RELOAD_SEG(es);
114
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200115 err |= fpu__restore_sig(buf, 1);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700116
Brian Gerst1daeaa32015-03-21 18:54:21 -0400117 force_iret();
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Al Viro3fe26fa2012-11-12 14:32:42 -0500122asmlinkage long sys32_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Al Viro3fe26fa2012-11-12 14:32:42 -0500124 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800125 struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
129 goto badframe;
130 if (__get_user(set.sig[0], &frame->sc.oldmask)
131 || (_COMPAT_NSIG_WORDS > 1
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100132 && __copy_from_user((((char *) &set.sig) + 4),
133 &frame->extramask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 sizeof(frame->extramask))))
135 goto badframe;
136
Oleg Nesterov905f29e2011-07-10 21:27:24 +0200137 set_current_blocked(&set);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100138
Brian Gerst6a3713f2015-04-04 08:58:23 -0400139 if (ia32_restore_sigcontext(regs, &frame->sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400141 return regs->ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143badframe:
144 signal_fault(regs, frame, "32bit sigreturn");
145 return 0;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Al Viro3fe26fa2012-11-12 14:32:42 -0500148asmlinkage long sys32_rt_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Al Viro3fe26fa2012-11-12 14:32:42 -0500150 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800151 struct rt_sigframe_ia32 __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800154 frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
157 goto badframe;
158 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
159 goto badframe;
160
Oleg Nesterov905f29e2011-07-10 21:27:24 +0200161 set_current_blocked(&set);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100162
Brian Gerst6a3713f2015-04-04 08:58:23 -0400163 if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto badframe;
165
Al Viro90268432012-12-14 14:47:53 -0500166 if (compat_restore_altstack(&frame->uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto badframe;
168
Brian Gerst6a3713f2015-04-04 08:58:23 -0400169 return regs->ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171badframe:
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100172 signal_fault(regs, frame, "32bit rt sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/*
177 * Set up a signal frame.
178 */
179
Ingo Molnar8fcb3462015-09-05 09:32:41 +0200180static int ia32_setup_sigcontext(struct sigcontext_32 __user *sc,
Suresh Siddhaab513702008-07-29 10:29:22 -0700181 void __user *fpstate,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100182 struct pt_regs *regs, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -0800184 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800186 put_user_try {
Hiroshi Shimamotoa967bb32009-02-20 19:00:54 -0800187 put_user_ex(get_user_seg(gs), (unsigned int __user *)&sc->gs);
188 put_user_ex(get_user_seg(fs), (unsigned int __user *)&sc->fs);
189 put_user_ex(get_user_seg(ds), (unsigned int __user *)&sc->ds);
190 put_user_ex(get_user_seg(es), (unsigned int __user *)&sc->es);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800192 put_user_ex(regs->di, &sc->di);
193 put_user_ex(regs->si, &sc->si);
194 put_user_ex(regs->bp, &sc->bp);
195 put_user_ex(regs->sp, &sc->sp);
196 put_user_ex(regs->bx, &sc->bx);
197 put_user_ex(regs->dx, &sc->dx);
198 put_user_ex(regs->cx, &sc->cx);
199 put_user_ex(regs->ax, &sc->ax);
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530200 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800201 put_user_ex(current->thread.error_code, &sc->err);
202 put_user_ex(regs->ip, &sc->ip);
203 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
204 put_user_ex(regs->flags, &sc->flags);
205 put_user_ex(regs->sp, &sc->sp_at_signal);
206 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800208 put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800210 /* non-iBCS2 extensions.. */
211 put_user_ex(mask, &sc->oldmask);
212 put_user_ex(current->thread.cr2, &sc->cr2);
213 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 return err;
216}
217
218/*
219 * Determine which stack to use..
220 */
Al Viro235b8022012-11-09 23:51:47 -0500221static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700222 size_t frame_size,
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200223 void __user **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200225 struct fpu *fpu = &current->thread.fpu;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100226 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100229 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* This is the X/Open sanctioned signal stack switching. */
Al Viro235b8022012-11-09 23:51:47 -0500232 if (ksig->ka.sa.sa_flags & SA_ONSTACK)
233 sp = sigsp(sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* This is the legacy signal stack switching. */
Andy Lutomirski99504812017-07-28 06:00:32 -0700235 else if (regs->ss != __USER32_DS &&
Al Viro235b8022012-11-09 23:51:47 -0500236 !(ksig->ka.sa.sa_flags & SA_RESTORER) &&
237 ksig->ka.sa.sa_restorer)
238 sp = (unsigned long) ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Ingo Molnare4a81bf2017-09-26 09:43:36 +0200240 if (fpu->initialized) {
Suresh Siddha72a671c2012-07-24 16:05:29 -0700241 unsigned long fx_aligned, math_size;
242
Ingo Molnar82c0e452015-04-29 21:09:18 +0200243 sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
Ingo Molnar86e9fc32015-09-05 09:32:36 +0200244 *fpstate = (struct _fpstate_32 __user *) sp;
Ingo Molnarc8e14042015-04-28 11:35:20 +0200245 if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
Suresh Siddha72a671c2012-07-24 16:05:29 -0700246 math_size) < 0)
Hiroshi Shimamoto99ea1b932008-11-05 18:32:54 -0800247 return (void __user *) -1L;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700248 }
249
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100250 sp -= frame_size;
Markus F.X.J. Oberhumerd347f372005-10-09 18:54:23 +0200251 /* Align the stack pointer according to the i386 ABI,
252 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100253 sp = ((sp + 4) & -16ul) - 4;
254 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Al Viro235b8022012-11-09 23:51:47 -0500257int ia32_setup_frame(int sig, struct ksignal *ksig,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100258 compat_sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800260 struct sigframe_ia32 __user *frame;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100261 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700263 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100265 /* copy_to_user optimizes that into a single 8 byte store */
266 static const struct {
267 u16 poplmovl;
268 u32 val;
269 u16 int80;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100270 } __attribute__((packed)) code = {
271 0xb858, /* popl %eax ; movl $...,%eax */
272 __NR_ia32_sigreturn,
273 0x80cd, /* int $0x80 */
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100274 };
275
Al Viro235b8022012-11-09 23:51:47 -0500276 frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700279 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700281 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700282 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700284 if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700285 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (_COMPAT_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700288 if (__copy_to_user(frame->extramask, &set->sig[1],
289 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700290 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Al Viro235b8022012-11-09 23:51:47 -0500293 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
294 restorer = ksig->ka.sa.sa_restorer;
Roland McGrathaf65d642008-01-30 13:30:43 +0100295 } else {
296 /* Return stub is in 32bit vsyscall page */
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700297 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700298 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700299 vdso_image_32.sym___kernel_sigreturn;
Roland McGrathaf65d642008-01-30 13:30:43 +0100300 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100301 restorer = &frame->retcode;
Roland McGrathaf65d642008-01-30 13:30:43 +0100302 }
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100303
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800304 put_user_try {
305 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
306
307 /*
308 * These are actually not used anymore, but left because some
309 * gdb versions depend on them as a marker.
310 */
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200311 put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800312 } put_user_catch(err);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700315 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100318 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500319 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Andi Kleen536e3ee2006-09-26 10:52:41 +0200321 /* Make -mregparm=3 work */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100322 regs->ax = sig;
323 regs->dx = 0;
324 regs->cx = 0;
Andi Kleen536e3ee2006-09-26 10:52:41 +0200325
Jeremy Fitzhardingeb6edbb12008-08-19 13:04:19 -0700326 loadsegment(ds, __USER32_DS);
327 loadsegment(es, __USER32_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100329 regs->cs = __USER32_CS;
330 regs->ss = __USER32_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Andi Kleen1d001df2006-09-26 10:52:26 +0200332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Al Viro235b8022012-11-09 23:51:47 -0500335int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100336 compat_sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800338 struct rt_sigframe_ia32 __user *frame;
Roland McGrathaf65d642008-01-30 13:30:43 +0100339 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700341 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100343 /* __copy_to_user optimizes that into a single 8 byte store */
344 static const struct {
345 u8 movl;
346 u32 val;
347 u16 int80;
Hiroshi Shimamoto9cc3c492008-11-11 19:11:39 -0800348 u8 pad;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100349 } __attribute__((packed)) code = {
350 0xb8,
351 __NR_ia32_rt_sigreturn,
352 0x80cd,
353 0,
354 };
355
Al Viro235b8022012-11-09 23:51:47 -0500356 frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700359 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800361 put_user_try {
362 put_user_ex(sig, &frame->sig);
363 put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
364 put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800366 /* Create the ucontext. */
Borislav Petkovd366bf72016-04-04 22:25:02 +0200367 if (boot_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800368 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
369 else
370 put_user_ex(0, &frame->uc.uc_flags);
371 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100372 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Al Viro235b8022012-11-09 23:51:47 -0500374 if (ksig->ka.sa.sa_flags & SA_RESTORER)
375 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800376 else
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700377 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700378 vdso_image_32.sym___kernel_rt_sigreturn;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800379 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800381 /*
382 * Not actually used anymore, but left because some gdb
383 * versions need it.
384 */
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200385 put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800386 } put_user_catch(err);
387
Dmitry Safonov68463512016-09-05 16:33:08 +0300388 err |= __copy_siginfo_to_user32(&frame->info, &ksig->info, false);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700389 err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
390 regs, set->sig[0]);
391 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700394 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100397 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500398 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Albert Cahalana7aacdf2006-10-29 22:26:17 -0500400 /* Make -mregparm=3 work */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100401 regs->ax = sig;
402 regs->dx = (unsigned long) &frame->info;
403 regs->cx = (unsigned long) &frame->uc;
Albert Cahalana7aacdf2006-10-29 22:26:17 -0500404
Jeremy Fitzhardingeb6edbb12008-08-19 13:04:19 -0700405 loadsegment(ds, __USER32_DS);
406 loadsegment(es, __USER32_DS);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100407
408 regs->cs = __USER32_CS;
409 regs->ss = __USER32_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Andi Kleen1d001df2006-09-26 10:52:26 +0200411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}