blob: af673ec23a2d91e50cb1863d8d16a845923a59dd [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>
Sami Tolvanen00198a62019-10-08 15:40:47 -070024#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/ucontext.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020027#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020028#include <asm/fpu/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/ptrace.h>
30#include <asm/ia32_unistd.h>
31#include <asm/user32.h>
Ingo Molnardecb4c42015-09-05 09:32:43 +020032#include <uapi/asm/sigcontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/proto.h>
Roland McGrathaf65d642008-01-30 13:30:43 +010034#include <asm/vdso.h>
Hiroshi Shimamotod98f9d82008-12-17 18:52:45 -080035#include <asm/sigframe.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080036#include <asm/sighandling.h>
H. Peter Anvin49b8c6952012-09-21 17:18:44 -070037#include <asm/smap.h>
Hiroshi Shimamotod98f9d82008-12-17 18:52:45 -080038
Al Viro978727c2020-02-15 12:23:36 -050039static inline void reload_segments(struct sigcontext_32 *sc)
40{
41 unsigned int cur;
42
43 savesegment(gs, cur);
44 if ((sc->gs | 0x03) != cur)
45 load_gs_index(sc->gs | 0x03);
46 savesegment(fs, cur);
47 if ((sc->fs | 0x03) != cur)
48 loadsegment(fs, sc->fs | 0x03);
49 savesegment(ds, cur);
50 if ((sc->ds | 0x03) != cur)
51 loadsegment(ds, sc->ds | 0x03);
52 savesegment(es, cur);
53 if ((sc->es | 0x03) != cur)
54 loadsegment(es, sc->es | 0x03);
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Do a signal return; undo the signal stack.
59 */
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010060static int ia32_restore_sigcontext(struct pt_regs *regs,
Al Viro978727c2020-02-15 12:23:36 -050061 struct sigcontext_32 __user *usc)
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010062{
Al Viro978727c2020-02-15 12:23:36 -050063 struct sigcontext_32 sc;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010064
65 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080066 current->restart_block.fn = do_no_restart_syscall;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +010067
Al Viro978727c2020-02-15 12:23:36 -050068 if (unlikely(copy_from_user(&sc, usc, sizeof(sc))))
69 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Al Viro978727c2020-02-15 12:23:36 -050071 /* Get only the ia32 registers. */
72 regs->bx = sc.bx;
73 regs->cx = sc.cx;
74 regs->dx = sc.dx;
75 regs->si = sc.si;
76 regs->di = sc.di;
77 regs->bp = sc.bp;
78 regs->ax = sc.ax;
79 regs->sp = sc.sp;
80 regs->ip = sc.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Al Viro978727c2020-02-15 12:23:36 -050082 /* Get CS/SS and force CPL3 */
83 regs->cs = sc.cs | 0x03;
84 regs->ss = sc.ss | 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Al Viro978727c2020-02-15 12:23:36 -050086 regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
87 /* disable syscall checks */
88 regs->orig_ax = -1;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -080089
Peter Zijlstra67a05142019-02-25 12:56:35 +010090 /*
91 * Reload fs and gs if they have changed in the signal
92 * handler. This does not handle long fs/gs base changes in
93 * the handler, but does not clobber them at least in the
94 * normal case.
95 */
Al Viro978727c2020-02-15 12:23:36 -050096 reload_segments(&sc);
97 return fpu__restore_sig(compat_ptr(sc.fpstate), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Sami Tolvanen00198a62019-10-08 15:40:47 -0700100COMPAT_SYSCALL_DEFINE0(sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Al Viro3fe26fa2012-11-12 14:32:42 -0500102 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800103 struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Linus Torvalds96d4f262019-01-03 18:57:57 -0800106 if (!access_ok(frame, sizeof(*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 goto badframe;
108 if (__get_user(set.sig[0], &frame->sc.oldmask)
Al Viro71c33132020-02-15 11:43:18 -0500109 || __get_user(((__u32 *)&set)[1], &frame->extramask[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 goto badframe;
111
Oleg Nesterov905f29e2011-07-10 21:27:24 +0200112 set_current_blocked(&set);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100113
Brian Gerst6a3713f2015-04-04 08:58:23 -0400114 if (ia32_restore_sigcontext(regs, &frame->sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400116 return regs->ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118badframe:
119 signal_fault(regs, frame, "32bit sigreturn");
120 return 0;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Sami Tolvanen00198a62019-10-08 15:40:47 -0700123COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Al Viro3fe26fa2012-11-12 14:32:42 -0500125 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800126 struct rt_sigframe_ia32 __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800129 frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds96d4f262019-01-03 18:57:57 -0800131 if (!access_ok(frame, sizeof(*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 goto badframe;
Al Viro71c33132020-02-15 11:43:18 -0500133 if (__get_user(set.sig[0], (__u64 __user *)&frame->uc.uc_sigmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto badframe;
135
Oleg Nesterov905f29e2011-07-10 21:27:24 +0200136 set_current_blocked(&set);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100137
Brian Gerst6a3713f2015-04-04 08:58:23 -0400138 if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto badframe;
140
Al Viro90268432012-12-14 14:47:53 -0500141 if (compat_restore_altstack(&frame->uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto badframe;
143
Brian Gerst6a3713f2015-04-04 08:58:23 -0400144 return regs->ax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146badframe:
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100147 signal_fault(regs, frame, "32bit rt sigreturn");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/*
152 * Set up a signal frame.
153 */
154
Al Viro978727c2020-02-15 12:23:36 -0500155#define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
156
Ingo Molnar8fcb3462015-09-05 09:32:41 +0200157static int ia32_setup_sigcontext(struct sigcontext_32 __user *sc,
Suresh Siddhaab513702008-07-29 10:29:22 -0700158 void __user *fpstate,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100159 struct pt_regs *regs, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Al Virod2d27282020-02-15 17:41:04 -0500161 if (!user_access_begin(sc, sizeof(struct sigcontext_32)))
162 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Al Virod2d27282020-02-15 17:41:04 -0500164 unsafe_put_user(get_user_seg(gs), (unsigned int __user *)&sc->gs, Efault);
165 unsafe_put_user(get_user_seg(fs), (unsigned int __user *)&sc->fs, Efault);
166 unsafe_put_user(get_user_seg(ds), (unsigned int __user *)&sc->ds, Efault);
167 unsafe_put_user(get_user_seg(es), (unsigned int __user *)&sc->es, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Al Virod2d27282020-02-15 17:41:04 -0500169 unsafe_put_user(regs->di, &sc->di, Efault);
170 unsafe_put_user(regs->si, &sc->si, Efault);
171 unsafe_put_user(regs->bp, &sc->bp, Efault);
172 unsafe_put_user(regs->sp, &sc->sp, Efault);
173 unsafe_put_user(regs->bx, &sc->bx, Efault);
174 unsafe_put_user(regs->dx, &sc->dx, Efault);
175 unsafe_put_user(regs->cx, &sc->cx, Efault);
176 unsafe_put_user(regs->ax, &sc->ax, Efault);
177 unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
178 unsafe_put_user(current->thread.error_code, &sc->err, Efault);
179 unsafe_put_user(regs->ip, &sc->ip, Efault);
180 unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
181 unsafe_put_user(regs->flags, &sc->flags, Efault);
182 unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
183 unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Al Virod2d27282020-02-15 17:41:04 -0500185 unsafe_put_user(ptr_to_compat(fpstate), &sc->fpstate, Efault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Al Virod2d27282020-02-15 17:41:04 -0500187 /* non-iBCS2 extensions.. */
188 unsafe_put_user(mask, &sc->oldmask, Efault);
189 unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
190 user_access_end();
191 return 0;
192Efault:
193 user_access_end();
194 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
198 * Determine which stack to use..
199 */
Al Viro235b8022012-11-09 23:51:47 -0500200static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700201 size_t frame_size,
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200202 void __user **fpstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200204 unsigned long sp, fx_aligned, math_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Default to using normal stack */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100207 sp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* This is the X/Open sanctioned signal stack switching. */
Al Viro235b8022012-11-09 23:51:47 -0500210 if (ksig->ka.sa.sa_flags & SA_ONSTACK)
211 sp = sigsp(sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* This is the legacy signal stack switching. */
Andy Lutomirski99504812017-07-28 06:00:32 -0700213 else if (regs->ss != __USER32_DS &&
Al Viro235b8022012-11-09 23:51:47 -0500214 !(ksig->ka.sa.sa_flags & SA_RESTORER) &&
215 ksig->ka.sa.sa_restorer)
216 sp = (unsigned long) ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200218 sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
219 *fpstate = (struct _fpstate_32 __user *) sp;
220 if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
221 math_size) < 0)
222 return (void __user *) -1L;
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700223
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100224 sp -= frame_size;
Markus F.X.J. Oberhumerd347f372005-10-09 18:54:23 +0200225 /* Align the stack pointer according to the i386 ABI,
226 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100227 sp = ((sp + 4) & -16ul) - 4;
228 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Al Viro235b8022012-11-09 23:51:47 -0500231int ia32_setup_frame(int sig, struct ksignal *ksig,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100232 compat_sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800234 struct sigframe_ia32 __user *frame;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100235 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700237 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100239 /* copy_to_user optimizes that into a single 8 byte store */
240 static const struct {
241 u16 poplmovl;
242 u32 val;
243 u16 int80;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100244 } __attribute__((packed)) code = {
245 0xb858, /* popl %eax ; movl $...,%eax */
246 __NR_ia32_sigreturn,
247 0x80cd, /* int $0x80 */
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100248 };
249
Al Viro235b8022012-11-09 23:51:47 -0500250 frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvalds96d4f262019-01-03 18:57:57 -0800252 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700253 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700255 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700256 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700258 if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700259 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Al Viro71c33132020-02-15 11:43:18 -0500261 if (__put_user(set->sig[1], &frame->extramask[0]))
262 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Al Viro235b8022012-11-09 23:51:47 -0500264 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
265 restorer = ksig->ka.sa.sa_restorer;
Roland McGrathaf65d642008-01-30 13:30:43 +0100266 } else {
267 /* Return stub is in 32bit vsyscall page */
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700268 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700269 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700270 vdso_image_32.sym___kernel_sigreturn;
Roland McGrathaf65d642008-01-30 13:30:43 +0100271 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100272 restorer = &frame->retcode;
Roland McGrathaf65d642008-01-30 13:30:43 +0100273 }
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100274
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800275 put_user_try {
276 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
277
278 /*
279 * These are actually not used anymore, but left because some
280 * gdb versions depend on them as a marker.
281 */
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200282 put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800283 } put_user_catch(err);
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700286 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100289 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500290 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Andi Kleen536e3ee2006-09-26 10:52:41 +0200292 /* Make -mregparm=3 work */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100293 regs->ax = sig;
294 regs->dx = 0;
295 regs->cx = 0;
Andi Kleen536e3ee2006-09-26 10:52:41 +0200296
Jeremy Fitzhardingeb6edbb12008-08-19 13:04:19 -0700297 loadsegment(ds, __USER32_DS);
298 loadsegment(es, __USER32_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100300 regs->cs = __USER32_CS;
301 regs->ss = __USER32_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Andi Kleen1d001df2006-09-26 10:52:26 +0200303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Al Viro235b8022012-11-09 23:51:47 -0500306int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100307 compat_sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Hiroshi Shimamoto3b0d29e2008-12-17 18:51:46 -0800309 struct rt_sigframe_ia32 __user *frame;
Roland McGrathaf65d642008-01-30 13:30:43 +0100310 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700312 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100314 /* __copy_to_user optimizes that into a single 8 byte store */
315 static const struct {
316 u8 movl;
317 u32 val;
318 u16 int80;
Hiroshi Shimamoto9cc3c492008-11-11 19:11:39 -0800319 u8 pad;
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100320 } __attribute__((packed)) code = {
321 0xb8,
322 __NR_ia32_rt_sigreturn,
323 0x80cd,
324 0,
325 };
326
Al Viro235b8022012-11-09 23:51:47 -0500327 frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds96d4f262019-01-03 18:57:57 -0800329 if (!access_ok(frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700330 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800332 put_user_try {
333 put_user_ex(sig, &frame->sig);
334 put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
335 put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800337 /* Create the ucontext. */
Marco Elverff661352019-07-11 20:53:56 -0700338 if (static_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800339 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
340 else
341 put_user_ex(0, &frame->uc.uc_flags);
342 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100343 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Al Viro235b8022012-11-09 23:51:47 -0500345 if (ksig->ka.sa.sa_flags & SA_RESTORER)
346 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800347 else
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700348 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700349 vdso_image_32.sym___kernel_rt_sigreturn;
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800350 put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800352 /*
353 * Not actually used anymore, but left because some gdb
354 * versions need it.
355 */
Mathias Krause0ff8fef2012-09-02 23:31:42 +0200356 put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
Hiroshi Shimamoto3b4b7572009-01-23 15:50:38 -0800357 } put_user_catch(err);
358
Dmitry Safonov68463512016-09-05 16:33:08 +0300359 err |= __copy_siginfo_to_user32(&frame->info, &ksig->info, false);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700360 err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
361 regs, set->sig[0]);
Al Viro71c33132020-02-15 11:43:18 -0500362 err |= __put_user(*(__u64 *)set, (__u64 __user *)&frame->uc.uc_sigmask);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700365 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* Set up registers for signal handler */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100368 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500369 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Albert Cahalana7aacdf2006-10-29 22:26:17 -0500371 /* Make -mregparm=3 work */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100372 regs->ax = sig;
373 regs->dx = (unsigned long) &frame->info;
374 regs->cx = (unsigned long) &frame->uc;
Albert Cahalana7aacdf2006-10-29 22:26:17 -0500375
Jeremy Fitzhardingeb6edbb12008-08-19 13:04:19 -0700376 loadsegment(ds, __USER32_DS);
377 loadsegment(es, __USER32_DS);
Thomas Gleixner99b9cdf2008-01-30 13:30:07 +0100378
379 regs->cs = __USER32_CS;
380 regs->ss = __USER32_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Andi Kleen1d001df2006-09-26 10:52:26 +0200382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}