Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  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
  9 */
 10
 11#include <linux/sched.h>
 
 12#include <linux/mm.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>
 24#include <asm/fpu/internal.h>
 25#include <asm/fpu/signal.h>
 26#include <asm/ptrace.h>
 27#include <asm/ia32_unistd.h>
 28#include <asm/user32.h>
 29#include <uapi/asm/sigcontext.h>
 30#include <asm/proto.h>
 31#include <asm/vdso.h>
 32#include <asm/sigframe.h>
 33#include <asm/sighandling.h>
 34#include <asm/sys_ia32.h>
 35#include <asm/smap.h>
 36
 37/*
 38 * Do a signal return; undo the signal stack.
 39 */
 40#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
 48#define COPY(x)			{		\
 49	get_user_ex(regs->x, &sc->x);		\
 50}
 51
 52#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)
 61
 62#define RELOAD_SEG(seg)		{		\
 63	unsigned int pre = GET_SEG(seg);	\
 64	unsigned int cur = get_user_seg(seg);	\
 65	pre |= 3;				\
 66	if (pre != cur)				\
 67		set_user_seg(seg, pre);		\
 68}
 69
 70static int ia32_restore_sigcontext(struct pt_regs *regs,
 71				   struct sigcontext_32 __user *sc)
 72{
 73	unsigned int tmpflags, err = 0;
 74	void __user *buf;
 75	u32 tmp;
 76
 77	/* Always make any pending restarted system calls return -EINTR */
 78	current->restart_block.fn = do_no_restart_syscall;
 79
 80	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		 */
 87		RELOAD_SEG(gs);
 88		RELOAD_SEG(fs);
 89		RELOAD_SEG(ds);
 90		RELOAD_SEG(es);
 91
 92		COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
 93		COPY(dx); COPY(cx); COPY(ip); COPY(ax);
 94		/* Don't touch extended registers */
 95
 96		COPY_SEG_CPL3(cs);
 97		COPY_SEG_CPL3(ss);
 98
 99		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;
103
104		get_user_ex(tmp, &sc->fpstate);
105		buf = compat_ptr(tmp);
106	} get_user_catch(err);
107
108	err |= fpu__restore_sig(buf, 1);
109
110	force_iret();
111
112	return err;
113}
114
115asmlinkage long sys32_sigreturn(void)
116{
117	struct pt_regs *regs = current_pt_regs();
118	struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
119	sigset_t set;
120
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
125		&& __copy_from_user((((char *) &set.sig) + 4),
126				    &frame->extramask,
127				    sizeof(frame->extramask))))
128		goto badframe;
129
130	set_current_blocked(&set);
131
132	if (ia32_restore_sigcontext(regs, &frame->sc))
133		goto badframe;
134	return regs->ax;
135
136badframe:
137	signal_fault(regs, frame, "32bit sigreturn");
138	return 0;
139}
140
141asmlinkage long sys32_rt_sigreturn(void)
142{
143	struct pt_regs *regs = current_pt_regs();
144	struct rt_sigframe_ia32 __user *frame;
145	sigset_t set;
146
147	frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
148
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
154	set_current_blocked(&set);
155
156	if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
157		goto badframe;
158
159	if (compat_restore_altstack(&frame->uc.uc_stack))
160		goto badframe;
161
162	return regs->ax;
163
164badframe:
165	signal_fault(regs, frame, "32bit rt sigreturn");
166	return 0;
167}
168
169/*
170 * Set up a signal frame.
171 */
172
173static int ia32_setup_sigcontext(struct sigcontext_32 __user *sc,
174				 void __user *fpstate,
175				 struct pt_regs *regs, unsigned int mask)
176{
177	int err = 0;
178
179	put_user_try {
180		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);
184
185		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);
193		put_user_ex(current->thread.trap_nr, &sc->trapno);
194		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);
200
201		put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
202
203		/* non-iBCS2 extensions.. */
204		put_user_ex(mask, &sc->oldmask);
205		put_user_ex(current->thread.cr2, &sc->cr2);
206	} put_user_catch(err);
207
208	return err;
209}
210
211/*
212 * Determine which stack to use..
213 */
214static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
215				 size_t frame_size,
216				 void __user **fpstate)
217{
218	struct fpu *fpu = &current->thread.fpu;
219	unsigned long sp;
220
221	/* Default to using normal stack */
222	sp = regs->sp;
223
224	/* This is the X/Open sanctioned signal stack switching.  */
225	if (ksig->ka.sa.sa_flags & SA_ONSTACK)
226		sp = sigsp(sp, ksig);
227	/* This is the legacy signal stack switching. */
228	else if ((regs->ss & 0xffff) != __USER32_DS &&
229		!(ksig->ka.sa.sa_flags & SA_RESTORER) &&
230		 ksig->ka.sa.sa_restorer)
231		sp = (unsigned long) ksig->ka.sa.sa_restorer;
232
233	if (fpu->fpstate_active) {
234		unsigned long fx_aligned, math_size;
235
236		sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
237		*fpstate = (struct _fpstate_32 __user *) sp;
238		if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
239				    math_size) < 0)
240			return (void __user *) -1L;
241	}
242
243	sp -= frame_size;
244	/* Align the stack pointer according to the i386 ABI,
245	 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
246	sp = ((sp + 4) & -16ul) - 4;
247	return (void __user *) sp;
248}
249
250int ia32_setup_frame(int sig, struct ksignal *ksig,
251		     compat_sigset_t *set, struct pt_regs *regs)
252{
253	struct sigframe_ia32 __user *frame;
254	void __user *restorer;
255	int err = 0;
256	void __user *fpstate = NULL;
257
258	/* copy_to_user optimizes that into a single 8 byte store */
259	static const struct {
260		u16 poplmovl;
261		u32 val;
262		u16 int80;
263	} __attribute__((packed)) code = {
264		0xb858,		 /* popl %eax ; movl $...,%eax */
265		__NR_ia32_sigreturn,
266		0x80cd,		/* int $0x80 */
267	};
268
269	frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
270
271	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
272		return -EFAULT;
273
274	if (__put_user(sig, &frame->sig))
275		return -EFAULT;
276
277	if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
278		return -EFAULT;
279
280	if (_COMPAT_NSIG_WORDS > 1) {
281		if (__copy_to_user(frame->extramask, &set->sig[1],
282				   sizeof(frame->extramask)))
283			return -EFAULT;
284	}
285
286	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
287		restorer = ksig->ka.sa.sa_restorer;
288	} else {
289		/* Return stub is in 32bit vsyscall page */
290		if (current->mm->context.vdso)
291			restorer = current->mm->context.vdso +
292				vdso_image_32.sym___kernel_sigreturn;
293		else
294			restorer = &frame->retcode;
295	}
296
297	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		 */
304		put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
305	} put_user_catch(err);
306
307	if (err)
308		return -EFAULT;
309
310	/* Set up registers for signal handler */
311	regs->sp = (unsigned long) frame;
312	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
313
314	/* Make -mregparm=3 work */
315	regs->ax = sig;
316	regs->dx = 0;
317	regs->cx = 0;
318
319	loadsegment(ds, __USER32_DS);
320	loadsegment(es, __USER32_DS);
321
322	regs->cs = __USER32_CS;
323	regs->ss = __USER32_DS;
324
325	return 0;
326}
327
328int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
329			compat_sigset_t *set, struct pt_regs *regs)
330{
331	struct rt_sigframe_ia32 __user *frame;
332	void __user *restorer;
333	int err = 0;
334	void __user *fpstate = NULL;
335
336	/* __copy_to_user optimizes that into a single 8 byte store */
337	static const struct {
338		u8 movl;
339		u32 val;
340		u16 int80;
341		u8  pad;
342	} __attribute__((packed)) code = {
343		0xb8,
344		__NR_ia32_rt_sigreturn,
345		0x80cd,
346		0,
347	};
348
349	frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
350
351	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
352		return -EFAULT;
353
354	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);
358
359		/* Create the ucontext.  */
360		if (cpu_has_xsave)
361			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);
365		compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
366
367		if (ksig->ka.sa.sa_flags & SA_RESTORER)
368			restorer = ksig->ka.sa.sa_restorer;
369		else
370			restorer = current->mm->context.vdso +
371				vdso_image_32.sym___kernel_rt_sigreturn;
372		put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
373
374		/*
375		 * Not actually used anymore, but left because some gdb
376		 * versions need it.
377		 */
378		put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
379	} put_user_catch(err);
380
381	err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
382	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
386	if (err)
387		return -EFAULT;
388
389	/* Set up registers for signal handler */
390	regs->sp = (unsigned long) frame;
391	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
392
393	/* Make -mregparm=3 work */
394	regs->ax = sig;
395	regs->dx = (unsigned long) &frame->info;
396	regs->cx = (unsigned long) &frame->uc;
397
398	loadsegment(ds, __USER32_DS);
399	loadsegment(es, __USER32_DS);
400
401	regs->cs = __USER32_CS;
402	regs->ss = __USER32_DS;
403
404	return 0;
405}
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  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
 10 */
 11
 12#include <linux/sched.h>
 13#include <linux/sched/task_stack.h>
 14#include <linux/mm.h>
 15#include <linux/smp.h>
 16#include <linux/kernel.h>
 17#include <linux/errno.h>
 18#include <linux/wait.h>
 19#include <linux/unistd.h>
 20#include <linux/stddef.h>
 21#include <linux/personality.h>
 22#include <linux/compat.h>
 23#include <linux/binfmts.h>
 24#include <asm/ucontext.h>
 25#include <linux/uaccess.h>
 26#include <asm/fpu/internal.h>
 27#include <asm/fpu/signal.h>
 28#include <asm/ptrace.h>
 29#include <asm/ia32_unistd.h>
 30#include <asm/user32.h>
 31#include <uapi/asm/sigcontext.h>
 32#include <asm/proto.h>
 33#include <asm/vdso.h>
 34#include <asm/sigframe.h>
 35#include <asm/sighandling.h>
 
 36#include <asm/smap.h>
 37
 38/*
 39 * Do a signal return; undo the signal stack.
 40 */
 41#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
 49#define COPY(x)			{		\
 50	get_user_ex(regs->x, &sc->x);		\
 51}
 52
 53#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)
 62
 63#define RELOAD_SEG(seg)		{		\
 64	unsigned int pre = GET_SEG(seg);	\
 65	unsigned int cur = get_user_seg(seg);	\
 66	pre |= 3;				\
 67	if (pre != cur)				\
 68		set_user_seg(seg, pre);		\
 69}
 70
 71static int ia32_restore_sigcontext(struct pt_regs *regs,
 72				   struct sigcontext_32 __user *sc)
 73{
 74	unsigned int tmpflags, err = 0;
 75	void __user *buf;
 76	u32 tmp;
 77
 78	/* Always make any pending restarted system calls return -EINTR */
 79	current->restart_block.fn = do_no_restart_syscall;
 80
 81	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		 */
 88		RELOAD_SEG(gs);
 89		RELOAD_SEG(fs);
 90		RELOAD_SEG(ds);
 91		RELOAD_SEG(es);
 92
 93		COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
 94		COPY(dx); COPY(cx); COPY(ip); COPY(ax);
 95		/* Don't touch extended registers */
 96
 97		COPY_SEG_CPL3(cs);
 98		COPY_SEG_CPL3(ss);
 99
100		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;
104
105		get_user_ex(tmp, &sc->fpstate);
106		buf = compat_ptr(tmp);
107	} get_user_catch(err);
108
109	err |= fpu__restore_sig(buf, 1);
110
111	force_iret();
112
113	return err;
114}
115
116asmlinkage long sys32_sigreturn(void)
117{
118	struct pt_regs *regs = current_pt_regs();
119	struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
120	sigset_t set;
121
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
126		&& __copy_from_user((((char *) &set.sig) + 4),
127				    &frame->extramask,
128				    sizeof(frame->extramask))))
129		goto badframe;
130
131	set_current_blocked(&set);
132
133	if (ia32_restore_sigcontext(regs, &frame->sc))
134		goto badframe;
135	return regs->ax;
136
137badframe:
138	signal_fault(regs, frame, "32bit sigreturn");
139	return 0;
140}
141
142asmlinkage long sys32_rt_sigreturn(void)
143{
144	struct pt_regs *regs = current_pt_regs();
145	struct rt_sigframe_ia32 __user *frame;
146	sigset_t set;
147
148	frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
149
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
155	set_current_blocked(&set);
156
157	if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
158		goto badframe;
159
160	if (compat_restore_altstack(&frame->uc.uc_stack))
161		goto badframe;
162
163	return regs->ax;
164
165badframe:
166	signal_fault(regs, frame, "32bit rt sigreturn");
167	return 0;
168}
169
170/*
171 * Set up a signal frame.
172 */
173
174static int ia32_setup_sigcontext(struct sigcontext_32 __user *sc,
175				 void __user *fpstate,
176				 struct pt_regs *regs, unsigned int mask)
177{
178	int err = 0;
179
180	put_user_try {
181		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);
185
186		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);
194		put_user_ex(current->thread.trap_nr, &sc->trapno);
195		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);
201
202		put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
203
204		/* non-iBCS2 extensions.. */
205		put_user_ex(mask, &sc->oldmask);
206		put_user_ex(current->thread.cr2, &sc->cr2);
207	} put_user_catch(err);
208
209	return err;
210}
211
212/*
213 * Determine which stack to use..
214 */
215static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
216				 size_t frame_size,
217				 void __user **fpstate)
218{
219	struct fpu *fpu = &current->thread.fpu;
220	unsigned long sp;
221
222	/* Default to using normal stack */
223	sp = regs->sp;
224
225	/* This is the X/Open sanctioned signal stack switching.  */
226	if (ksig->ka.sa.sa_flags & SA_ONSTACK)
227		sp = sigsp(sp, ksig);
228	/* This is the legacy signal stack switching. */
229	else if (regs->ss != __USER32_DS &&
230		!(ksig->ka.sa.sa_flags & SA_RESTORER) &&
231		 ksig->ka.sa.sa_restorer)
232		sp = (unsigned long) ksig->ka.sa.sa_restorer;
233
234	if (fpu->initialized) {
235		unsigned long fx_aligned, math_size;
236
237		sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
238		*fpstate = (struct _fpstate_32 __user *) sp;
239		if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
240				    math_size) < 0)
241			return (void __user *) -1L;
242	}
243
244	sp -= frame_size;
245	/* Align the stack pointer according to the i386 ABI,
246	 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
247	sp = ((sp + 4) & -16ul) - 4;
248	return (void __user *) sp;
249}
250
251int ia32_setup_frame(int sig, struct ksignal *ksig,
252		     compat_sigset_t *set, struct pt_regs *regs)
253{
254	struct sigframe_ia32 __user *frame;
255	void __user *restorer;
256	int err = 0;
257	void __user *fpstate = NULL;
258
259	/* copy_to_user optimizes that into a single 8 byte store */
260	static const struct {
261		u16 poplmovl;
262		u32 val;
263		u16 int80;
264	} __attribute__((packed)) code = {
265		0xb858,		 /* popl %eax ; movl $...,%eax */
266		__NR_ia32_sigreturn,
267		0x80cd,		/* int $0x80 */
268	};
269
270	frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
271
272	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
273		return -EFAULT;
274
275	if (__put_user(sig, &frame->sig))
276		return -EFAULT;
277
278	if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
279		return -EFAULT;
280
281	if (_COMPAT_NSIG_WORDS > 1) {
282		if (__copy_to_user(frame->extramask, &set->sig[1],
283				   sizeof(frame->extramask)))
284			return -EFAULT;
285	}
286
287	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
288		restorer = ksig->ka.sa.sa_restorer;
289	} else {
290		/* Return stub is in 32bit vsyscall page */
291		if (current->mm->context.vdso)
292			restorer = current->mm->context.vdso +
293				vdso_image_32.sym___kernel_sigreturn;
294		else
295			restorer = &frame->retcode;
296	}
297
298	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		 */
305		put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
306	} put_user_catch(err);
307
308	if (err)
309		return -EFAULT;
310
311	/* Set up registers for signal handler */
312	regs->sp = (unsigned long) frame;
313	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
314
315	/* Make -mregparm=3 work */
316	regs->ax = sig;
317	regs->dx = 0;
318	regs->cx = 0;
319
320	loadsegment(ds, __USER32_DS);
321	loadsegment(es, __USER32_DS);
322
323	regs->cs = __USER32_CS;
324	regs->ss = __USER32_DS;
325
326	return 0;
327}
328
329int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
330			compat_sigset_t *set, struct pt_regs *regs)
331{
332	struct rt_sigframe_ia32 __user *frame;
333	void __user *restorer;
334	int err = 0;
335	void __user *fpstate = NULL;
336
337	/* __copy_to_user optimizes that into a single 8 byte store */
338	static const struct {
339		u8 movl;
340		u32 val;
341		u16 int80;
342		u8  pad;
343	} __attribute__((packed)) code = {
344		0xb8,
345		__NR_ia32_rt_sigreturn,
346		0x80cd,
347		0,
348	};
349
350	frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
351
352	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
353		return -EFAULT;
354
355	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);
359
360		/* Create the ucontext.  */
361		if (boot_cpu_has(X86_FEATURE_XSAVE))
362			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);
366		compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
367
368		if (ksig->ka.sa.sa_flags & SA_RESTORER)
369			restorer = ksig->ka.sa.sa_restorer;
370		else
371			restorer = current->mm->context.vdso +
372				vdso_image_32.sym___kernel_rt_sigreturn;
373		put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
374
375		/*
376		 * Not actually used anymore, but left because some gdb
377		 * versions need it.
378		 */
379		put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
380	} put_user_catch(err);
381
382	err |= __copy_siginfo_to_user32(&frame->info, &ksig->info, false);
383	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
387	if (err)
388		return -EFAULT;
389
390	/* Set up registers for signal handler */
391	regs->sp = (unsigned long) frame;
392	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
393
394	/* Make -mregparm=3 work */
395	regs->ax = sig;
396	regs->dx = (unsigned long) &frame->info;
397	regs->cx = (unsigned long) &frame->uc;
398
399	loadsegment(ds, __USER32_DS);
400	loadsegment(es, __USER32_DS);
401
402	regs->cs = __USER32_CS;
403	regs->ss = __USER32_DS;
404
405	return 0;
406}