Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *  Copyright (C) 1991, 1992  Linus Torvalds
  4 *  Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
  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-2002   x86-64 support by Andi Kleen
  9 */
 10
 11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 12
 13#include <linux/sched.h>
 14#include <linux/sched/task_stack.h>
 15#include <linux/mm.h>
 16#include <linux/smp.h>
 17#include <linux/kernel.h>
 18#include <linux/kstrtox.h>
 19#include <linux/errno.h>
 20#include <linux/wait.h>
 
 21#include <linux/unistd.h>
 22#include <linux/stddef.h>
 23#include <linux/personality.h>
 24#include <linux/uaccess.h>
 25#include <linux/user-return-notifier.h>
 26#include <linux/uprobes.h>
 27#include <linux/context_tracking.h>
 28#include <linux/entry-common.h>
 29#include <linux/syscalls.h>
 30
 31#include <asm/processor.h>
 32#include <asm/ucontext.h>
 33#include <asm/fpu/signal.h>
 34#include <asm/fpu/xstate.h>
 35#include <asm/vdso.h>
 36#include <asm/mce.h>
 37#include <asm/sighandling.h>
 38#include <asm/vm86.h>
 
 
 
 
 
 39
 40#include <asm/syscall.h>
 41#include <asm/sigframe.h>
 42#include <asm/signal.h>
 43
 44static inline int is_ia32_compat_frame(struct ksignal *ksig)
 45{
 46	return IS_ENABLED(CONFIG_IA32_EMULATION) &&
 47		ksig->ka.sa.sa_flags & SA_IA32_ABI;
 48}
 49
 50static inline int is_ia32_frame(struct ksignal *ksig)
 51{
 52	return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
 53}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54
 55static inline int is_x32_frame(struct ksignal *ksig)
 56{
 57	return IS_ENABLED(CONFIG_X86_X32_ABI) &&
 58		ksig->ka.sa.sa_flags & SA_X32_ABI;
 59}
 60
 61/*
 62 * Set up a signal frame.
 63 */
 64
 65/* x86 ABI requires 16-byte alignment */
 66#define FRAME_ALIGNMENT	16UL
 67
 68#define MAX_FRAME_PADDING	(FRAME_ALIGNMENT - 1)
 69
 70/*
 71 * Determine which stack to use..
 72 */
 73void __user *
 74get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 75	     void __user **fpstate)
 76{
 77	struct k_sigaction *ka = &ksig->ka;
 78	int ia32_frame = is_ia32_frame(ksig);
 79	/* Default to using normal stack */
 80	bool nested_altstack = on_sig_stack(regs->sp);
 81	bool entering_altstack = false;
 82	unsigned long math_size = 0;
 83	unsigned long sp = regs->sp;
 84	unsigned long buf_fx = 0;
 
 85
 86	/* redzone */
 87	if (!ia32_frame)
 88		sp -= 128;
 89
 90	/* This is the X/Open sanctioned signal stack switching.  */
 91	if (ka->sa.sa_flags & SA_ONSTACK) {
 92		/*
 93		 * This checks nested_altstack via sas_ss_flags(). Sensible
 94		 * programs use SS_AUTODISARM, which disables that check, and
 95		 * programs that don't use SS_AUTODISARM get compatible.
 96		 */
 97		if (sas_ss_flags(sp) == 0) {
 98			sp = current->sas_ss_sp + current->sas_ss_size;
 99			entering_altstack = true;
 
100		}
101	} else if (ia32_frame &&
102		   !nested_altstack &&
103		   regs->ss != __USER_DS &&
104		   !(ka->sa.sa_flags & SA_RESTORER) &&
105		   ka->sa.sa_restorer) {
106		/* This is the legacy signal stack switching. */
107		sp = (unsigned long) ka->sa.sa_restorer;
108		entering_altstack = true;
109	}
110
111	sp = fpu__alloc_mathframe(sp, ia32_frame, &buf_fx, &math_size);
112	*fpstate = (void __user *)sp;
113
114	sp -= frame_size;
 
115
116	if (ia32_frame)
117		/*
118		 * Align the stack pointer according to the i386 ABI,
119		 * i.e. so that on function entry ((sp + 4) & 15) == 0.
120		 */
121		sp = ((sp + 4) & -FRAME_ALIGNMENT) - 4;
122	else
123		sp = round_down(sp, FRAME_ALIGNMENT) - 8;
124
125	/*
126	 * If we are on the alternate signal stack and would overflow it, don't.
127	 * Return an always-bogus address instead so we will die with SIGSEGV.
128	 */
129	if (unlikely((nested_altstack || entering_altstack) &&
130		     !__on_sig_stack(sp))) {
131
132		if (show_unhandled_signals && printk_ratelimit())
133			pr_info("%s[%d] overflowed sigaltstack\n",
134				current->comm, task_pid_nr(current));
135
136		return (void __user *)-1L;
137	}
138
139	/* save i387 and extended state */
140	if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size))
 
141		return (void __user *)-1L;
142
143	return (void __user *)sp;
144}
145
146/*
147 * There are four different struct types for signal frame: sigframe_ia32,
148 * rt_sigframe_ia32, rt_sigframe_x32, and rt_sigframe. Use the worst case
149 * -- the largest size. It means the size for 64-bit apps is a bit more
150 * than needed, but this keeps the code simple.
151 */
152#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
153# define MAX_FRAME_SIGINFO_UCTXT_SIZE	sizeof(struct sigframe_ia32)
154#else
155# define MAX_FRAME_SIGINFO_UCTXT_SIZE	sizeof(struct rt_sigframe)
156#endif
 
 
 
 
 
 
 
 
 
 
 
157
158/*
159 * The FP state frame contains an XSAVE buffer which must be 64-byte aligned.
160 * If a signal frame starts at an unaligned address, extra space is required.
161 * This is the max alignment padding, conservatively.
162 */
163#define MAX_XSAVE_PADDING	63UL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
165/*
166 * The frame data is composed of the following areas and laid out as:
167 *
168 * -------------------------
169 * | alignment padding     |
170 * -------------------------
171 * | (f)xsave frame        |
172 * -------------------------
173 * | fsave header          |
174 * -------------------------
175 * | alignment padding     |
176 * -------------------------
177 * | siginfo + ucontext    |
178 * -------------------------
179 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
181/* max_frame_size tells userspace the worst case signal stack size. */
182static unsigned long __ro_after_init max_frame_size;
183static unsigned int __ro_after_init fpu_default_state_size;
 
 
 
 
 
 
 
 
 
184
185void __init init_sigframe_size(void)
186{
187	fpu_default_state_size = fpu__get_fpstate_size();
 
 
 
188
189	max_frame_size = MAX_FRAME_SIGINFO_UCTXT_SIZE + MAX_FRAME_PADDING;
 
 
 
 
190
191	max_frame_size += fpu_default_state_size + MAX_XSAVE_PADDING;
192
193	/* Userspace expects an aligned size. */
194	max_frame_size = round_up(max_frame_size, FRAME_ALIGNMENT);
195
196	pr_info("max sigframe size: %lu\n", max_frame_size);
 
 
 
 
 
 
 
197}
198
199unsigned long get_sigframe_size(void)
 
 
 
200{
201	return max_frame_size;
 
 
 
 
 
 
202}
203
204static int
205setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
206{
207	/* Perform fixup for the pre-signal frame. */
208	rseq_signal_deliver(ksig, regs);
 
209
210	/* Set up the stack frame */
211	if (is_ia32_frame(ksig)) {
212		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
213			return ia32_setup_rt_frame(ksig, regs);
214		else
215			return ia32_setup_frame(ksig, regs);
216	} else if (is_x32_frame(ksig)) {
217		return x32_setup_rt_frame(ksig, regs);
218	} else {
219		return x64_setup_rt_frame(ksig, regs);
220	}
221}
222
223static void
224handle_signal(struct ksignal *ksig, struct pt_regs *regs)
225{
226	bool stepping, failed;
227	struct fpu *fpu = &current->thread.fpu;
228
229	if (v8086_mode(regs))
230		save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
231
232	/* Are we from a system call? */
233	if (syscall_get_nr(current, regs) != -1) {
234		/* If so, check system call restarting.. */
235		switch (syscall_get_error(current, regs)) {
236		case -ERESTART_RESTARTBLOCK:
237		case -ERESTARTNOHAND:
238			regs->ax = -EINTR;
239			break;
240
241		case -ERESTARTSYS:
242			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
243				regs->ax = -EINTR;
244				break;
245			}
246			fallthrough;
247		case -ERESTARTNOINTR:
248			regs->ax = regs->orig_ax;
249			regs->ip -= 2;
250			break;
251		}
252	}
253
254	/*
255	 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
256	 * so that register information in the sigcontext is correct and
257	 * then notify the tracer before entering the signal handler.
258	 */
259	stepping = test_thread_flag(TIF_SINGLESTEP);
260	if (stepping)
261		user_disable_single_step(current);
262
263	failed = (setup_rt_frame(ksig, regs) < 0);
264	if (!failed) {
265		/*
266		 * Clear the direction flag as per the ABI for function entry.
267		 *
268		 * Clear RF when entering the signal handler, because
269		 * it might disable possible debug exception from the
270		 * signal handler.
271		 *
272		 * Clear TF for the case when it wasn't set by debugger to
273		 * avoid the recursive send_sigtrap() in SIGTRAP handler.
 
 
274		 */
275		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
276		/*
277		 * Ensure the signal handler starts with the new fpu state.
278		 */
279		fpu__clear_user_states(fpu);
280	}
281	signal_setup_done(failed, ksig, stepping);
282}
283
284static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
285{
286#ifdef CONFIG_IA32_EMULATION
287	if (current->restart_block.arch_data & TS_COMPAT)
288		return __NR_ia32_restart_syscall;
289#endif
290#ifdef CONFIG_X86_X32_ABI
291	return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
292#else
293	return __NR_restart_syscall;
294#endif
295}
296
297/*
298 * Note that 'init' is a special process: it doesn't get signals it doesn't
299 * want to handle. Thus you cannot kill init even with a SIGKILL even by
300 * mistake.
301 */
302void arch_do_signal_or_restart(struct pt_regs *regs)
303{
304	struct ksignal ksig;
305
306	if (get_signal(&ksig)) {
307		/* Whee! Actually deliver the signal.  */
308		handle_signal(&ksig, regs);
309		return;
310	}
311
312	/* Did we come from a system call? */
313	if (syscall_get_nr(current, regs) != -1) {
314		/* Restart the system call - no handlers present */
315		switch (syscall_get_error(current, regs)) {
316		case -ERESTARTNOHAND:
317		case -ERESTARTSYS:
318		case -ERESTARTNOINTR:
319			regs->ax = regs->orig_ax;
320			regs->ip -= 2;
321			break;
322
323		case -ERESTART_RESTARTBLOCK:
324			regs->ax = get_nr_restart_syscall(regs);
325			regs->ip -= 2;
326			break;
327		}
328	}
329
330	/*
331	 * If there's no signal to deliver, we just put the saved sigmask
332	 * back.
333	 */
334	restore_saved_sigmask();
335}
336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
338{
339	struct task_struct *me = current;
340
341	if (show_unhandled_signals && printk_ratelimit()) {
342		printk("%s"
343		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
344		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
345		       me->comm, me->pid, where, frame,
346		       regs->ip, regs->sp, regs->orig_ax);
347		print_vma_addr(KERN_CONT " in ", regs->ip);
348		pr_cont("\n");
349	}
350
351	force_sig(SIGSEGV);
352}
353
354#ifdef CONFIG_DYNAMIC_SIGFRAME
355#ifdef CONFIG_STRICT_SIGALTSTACK_SIZE
356static bool strict_sigaltstack_size __ro_after_init = true;
357#else
358static bool strict_sigaltstack_size __ro_after_init = false;
359#endif
360
361static int __init strict_sas_size(char *arg)
362{
363	return kstrtobool(arg, &strict_sigaltstack_size);
364}
365__setup("strict_sas_size", strict_sas_size);
 
366
367/*
368 * MINSIGSTKSZ is 2048 and can't be changed despite the fact that AVX512
369 * exceeds that size already. As such programs might never use the
370 * sigaltstack they just continued to work. While always checking against
371 * the real size would be correct, this might be considered a regression.
372 *
373 * Therefore avoid the sanity check, unless enforced by kernel
374 * configuration or command line option.
375 *
376 * When dynamic FPU features are supported, the check is also enforced when
377 * the task has permissions to use dynamic features. Tasks which have no
378 * permission are checked against the size of the non-dynamic feature set
379 * if strict checking is enabled. This avoids forcing all tasks on the
380 * system to allocate large sigaltstacks even if they are never going
381 * to use a dynamic feature. As this is serialized via sighand::siglock
382 * any permission request for a dynamic feature either happened already
383 * or will see the newly install sigaltstack size in the permission checks.
384 */
385bool sigaltstack_size_valid(size_t ss_size)
386{
387	unsigned long fsize = max_frame_size - fpu_default_state_size;
388	u64 mask;
389
390	lockdep_assert_held(&current->sighand->siglock);
 
 
 
391
392	if (!fpu_state_size_dynamic() && !strict_sigaltstack_size)
393		return true;
394
395	fsize += current->group_leader->thread.fpu.perm.__user_state_size;
396	if (likely(ss_size > fsize))
397		return true;
398
399	if (strict_sigaltstack_size)
400		return ss_size > fsize;
401
402	mask = current->group_leader->thread.fpu.perm.__state_perm;
403	if (mask & XFEATURE_MASK_USER_DYNAMIC)
404		return ss_size > fsize;
405
406	return true;
 
 
407}
408#endif /* CONFIG_DYNAMIC_SIGFRAME */
v3.15
 
  1/*
  2 *  Copyright (C) 1991, 1992  Linus Torvalds
  3 *  Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
  4 *
  5 *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
  6 *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
  7 *  2000-2002   x86-64 support by Andi Kleen
  8 */
  9
 10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 11
 12#include <linux/sched.h>
 
 13#include <linux/mm.h>
 14#include <linux/smp.h>
 15#include <linux/kernel.h>
 
 16#include <linux/errno.h>
 17#include <linux/wait.h>
 18#include <linux/tracehook.h>
 19#include <linux/unistd.h>
 20#include <linux/stddef.h>
 21#include <linux/personality.h>
 22#include <linux/uaccess.h>
 23#include <linux/user-return-notifier.h>
 24#include <linux/uprobes.h>
 25#include <linux/context_tracking.h>
 
 
 26
 27#include <asm/processor.h>
 28#include <asm/ucontext.h>
 29#include <asm/i387.h>
 30#include <asm/fpu-internal.h>
 31#include <asm/vdso.h>
 32#include <asm/mce.h>
 33#include <asm/sighandling.h>
 34
 35#ifdef CONFIG_X86_64
 36#include <asm/proto.h>
 37#include <asm/ia32_unistd.h>
 38#include <asm/sys_ia32.h>
 39#endif /* CONFIG_X86_64 */
 40
 41#include <asm/syscall.h>
 42#include <asm/syscalls.h>
 
 43
 44#include <asm/sigframe.h>
 
 
 
 
 45
 46#define COPY(x)			do {			\
 47	get_user_ex(regs->x, &sc->x);			\
 48} while (0)
 49
 50#define GET_SEG(seg)		({			\
 51	unsigned short tmp;				\
 52	get_user_ex(tmp, &sc->seg);			\
 53	tmp;						\
 54})
 55
 56#define COPY_SEG(seg)		do {			\
 57	regs->seg = GET_SEG(seg);			\
 58} while (0)
 59
 60#define COPY_SEG_CPL3(seg)	do {			\
 61	regs->seg = GET_SEG(seg) | 3;			\
 62} while (0)
 63
 64int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
 65		       unsigned long *pax)
 66{
 67	void __user *buf;
 68	unsigned int tmpflags;
 69	unsigned int err = 0;
 70
 71	/* Always make any pending restarted system calls return -EINTR */
 72	current_thread_info()->restart_block.fn = do_no_restart_syscall;
 73
 74	get_user_try {
 75
 76#ifdef CONFIG_X86_32
 77		set_user_gs(regs, GET_SEG(gs));
 78		COPY_SEG(fs);
 79		COPY_SEG(es);
 80		COPY_SEG(ds);
 81#endif /* CONFIG_X86_32 */
 82
 83		COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
 84		COPY(dx); COPY(cx); COPY(ip);
 85
 86#ifdef CONFIG_X86_64
 87		COPY(r8);
 88		COPY(r9);
 89		COPY(r10);
 90		COPY(r11);
 91		COPY(r12);
 92		COPY(r13);
 93		COPY(r14);
 94		COPY(r15);
 95#endif /* CONFIG_X86_64 */
 96
 97#ifdef CONFIG_X86_32
 98		COPY_SEG_CPL3(cs);
 99		COPY_SEG_CPL3(ss);
100#else /* !CONFIG_X86_32 */
101		/* Kernel saves and restores only the CS segment register on signals,
102		 * which is the bare minimum needed to allow mixed 32/64-bit code.
103		 * App's signal handler can save/restore other segments if needed. */
104		COPY_SEG_CPL3(cs);
105#endif /* CONFIG_X86_32 */
106
107		get_user_ex(tmpflags, &sc->flags);
108		regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
109		regs->orig_ax = -1;		/* disable syscall checks */
110
111		get_user_ex(buf, &sc->fpstate);
112
113		get_user_ex(*pax, &sc->ax);
114	} get_user_catch(err);
115
116	err |= restore_xstate_sig(buf, config_enabled(CONFIG_X86_32));
117
118	return err;
119}
120
121int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
122		     struct pt_regs *regs, unsigned long mask)
123{
124	int err = 0;
125
126	put_user_try {
127
128#ifdef CONFIG_X86_32
129		put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
130		put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
131		put_user_ex(regs->es, (unsigned int __user *)&sc->es);
132		put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
133#endif /* CONFIG_X86_32 */
134
135		put_user_ex(regs->di, &sc->di);
136		put_user_ex(regs->si, &sc->si);
137		put_user_ex(regs->bp, &sc->bp);
138		put_user_ex(regs->sp, &sc->sp);
139		put_user_ex(regs->bx, &sc->bx);
140		put_user_ex(regs->dx, &sc->dx);
141		put_user_ex(regs->cx, &sc->cx);
142		put_user_ex(regs->ax, &sc->ax);
143#ifdef CONFIG_X86_64
144		put_user_ex(regs->r8, &sc->r8);
145		put_user_ex(regs->r9, &sc->r9);
146		put_user_ex(regs->r10, &sc->r10);
147		put_user_ex(regs->r11, &sc->r11);
148		put_user_ex(regs->r12, &sc->r12);
149		put_user_ex(regs->r13, &sc->r13);
150		put_user_ex(regs->r14, &sc->r14);
151		put_user_ex(regs->r15, &sc->r15);
152#endif /* CONFIG_X86_64 */
153
154		put_user_ex(current->thread.trap_nr, &sc->trapno);
155		put_user_ex(current->thread.error_code, &sc->err);
156		put_user_ex(regs->ip, &sc->ip);
157#ifdef CONFIG_X86_32
158		put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
159		put_user_ex(regs->flags, &sc->flags);
160		put_user_ex(regs->sp, &sc->sp_at_signal);
161		put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
162#else /* !CONFIG_X86_32 */
163		put_user_ex(regs->flags, &sc->flags);
164		put_user_ex(regs->cs, &sc->cs);
165		put_user_ex(0, &sc->gs);
166		put_user_ex(0, &sc->fs);
167#endif /* CONFIG_X86_32 */
168
169		put_user_ex(fpstate, &sc->fpstate);
170
171		/* non-iBCS2 extensions.. */
172		put_user_ex(mask, &sc->oldmask);
173		put_user_ex(current->thread.cr2, &sc->cr2);
174	} put_user_catch(err);
175
176	return err;
 
 
 
177}
178
179/*
180 * Set up a signal frame.
181 */
182
 
 
 
 
 
183/*
184 * Determine which stack to use..
185 */
186static unsigned long align_sigframe(unsigned long sp)
187{
188#ifdef CONFIG_X86_32
189	/*
190	 * Align the stack pointer according to the i386 ABI,
191	 * i.e. so that on function entry ((sp + 4) & 15) == 0.
192	 */
193	sp = ((sp + 4) & -16ul) - 4;
194#else /* !CONFIG_X86_32 */
195	sp = round_down(sp, 16) - 8;
196#endif
197	return sp;
198}
199
200static inline void __user *
201get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
202	     void __user **fpstate)
203{
 
 
204	/* Default to using normal stack */
 
 
205	unsigned long math_size = 0;
206	unsigned long sp = regs->sp;
207	unsigned long buf_fx = 0;
208	int onsigstack = on_sig_stack(sp);
209
210	/* redzone */
211	if (config_enabled(CONFIG_X86_64))
212		sp -= 128;
213
214	if (!onsigstack) {
215		/* This is the X/Open sanctioned signal stack switching.  */
216		if (ka->sa.sa_flags & SA_ONSTACK) {
217			if (current->sas_ss_size)
218				sp = current->sas_ss_sp + current->sas_ss_size;
219		} else if (config_enabled(CONFIG_X86_32) &&
220			   (regs->ss & 0xffff) != __USER_DS &&
221			   !(ka->sa.sa_flags & SA_RESTORER) &&
222			   ka->sa.sa_restorer) {
223				/* This is the legacy signal stack switching. */
224				sp = (unsigned long) ka->sa.sa_restorer;
225		}
 
 
 
 
 
 
 
 
226	}
227
228	if (used_math()) {
229		sp = alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
230				     &buf_fx, &math_size);
231		*fpstate = (void __user *)sp;
232	}
233
234	sp = align_sigframe(sp - frame_size);
 
 
 
 
 
 
 
235
236	/*
237	 * If we are on the alternate signal stack and would overflow it, don't.
238	 * Return an always-bogus address instead so we will die with SIGSEGV.
239	 */
240	if (onsigstack && !likely(on_sig_stack(sp)))
 
 
 
 
 
 
241		return (void __user *)-1L;
 
242
243	/* save i387 and extended state */
244	if (used_math() &&
245	    save_xstate_sig(*fpstate, (void __user *)buf_fx, math_size) < 0)
246		return (void __user *)-1L;
247
248	return (void __user *)sp;
249}
250
251#ifdef CONFIG_X86_32
252static const struct {
253	u16 poplmovl;
254	u32 val;
255	u16 int80;
256} __attribute__((packed)) retcode = {
257	0xb858,		/* popl %eax; movl $..., %eax */
258	__NR_sigreturn,
259	0x80cd,		/* int $0x80 */
260};
261
262static const struct {
263	u8  movl;
264	u32 val;
265	u16 int80;
266	u8  pad;
267} __attribute__((packed)) rt_retcode = {
268	0xb8,		/* movl $..., %eax */
269	__NR_rt_sigreturn,
270	0x80cd,		/* int $0x80 */
271	0
272};
273
274static int
275__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
276	      struct pt_regs *regs)
277{
278	struct sigframe __user *frame;
279	void __user *restorer;
280	int err = 0;
281	void __user *fpstate = NULL;
282
283	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
284
285	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
286		return -EFAULT;
287
288	if (__put_user(sig, &frame->sig))
289		return -EFAULT;
290
291	if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
292		return -EFAULT;
293
294	if (_NSIG_WORDS > 1) {
295		if (__copy_to_user(&frame->extramask, &set->sig[1],
296				   sizeof(frame->extramask)))
297			return -EFAULT;
298	}
299
300	if (current->mm->context.vdso)
301		restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
302	else
303		restorer = &frame->retcode;
304	if (ksig->ka.sa.sa_flags & SA_RESTORER)
305		restorer = ksig->ka.sa.sa_restorer;
306
307	/* Set up to return from userspace.  */
308	err |= __put_user(restorer, &frame->pretcode);
309
310	/*
311	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
312	 *
313	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
314	 * reasons and because gdb uses it as a signature to notice
315	 * signal handler stack frames.
316	 */
317	err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
318
319	if (err)
320		return -EFAULT;
321
322	/* Set up registers for signal handler */
323	regs->sp = (unsigned long)frame;
324	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
325	regs->ax = (unsigned long)sig;
326	regs->dx = 0;
327	regs->cx = 0;
328
329	regs->ds = __USER_DS;
330	regs->es = __USER_DS;
331	regs->ss = __USER_DS;
332	regs->cs = __USER_CS;
333
334	return 0;
335}
336
337static int __setup_rt_frame(int sig, struct ksignal *ksig,
338			    sigset_t *set, struct pt_regs *regs)
339{
340	struct rt_sigframe __user *frame;
341	void __user *restorer;
342	int err = 0;
343	void __user *fpstate = NULL;
344
345	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
346
347	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
348		return -EFAULT;
349
350	put_user_try {
351		put_user_ex(sig, &frame->sig);
352		put_user_ex(&frame->info, &frame->pinfo);
353		put_user_ex(&frame->uc, &frame->puc);
354
355		/* Create the ucontext.  */
356		if (cpu_has_xsave)
357			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
358		else
359			put_user_ex(0, &frame->uc.uc_flags);
360		put_user_ex(0, &frame->uc.uc_link);
361		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
362
363		/* Set up to return from userspace.  */
364		restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
365		if (ksig->ka.sa.sa_flags & SA_RESTORER)
366			restorer = ksig->ka.sa.sa_restorer;
367		put_user_ex(restorer, &frame->pretcode);
368
369		/*
370		 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
371		 *
372		 * WE DO NOT USE IT ANY MORE! It's only left here for historical
373		 * reasons and because gdb uses it as a signature to notice
374		 * signal handler stack frames.
375		 */
376		put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
377	} put_user_catch(err);
378	
379	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
380	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
381				regs, set->sig[0]);
382	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
383
384	if (err)
385		return -EFAULT;
386
387	/* Set up registers for signal handler */
388	regs->sp = (unsigned long)frame;
389	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
390	regs->ax = (unsigned long)sig;
391	regs->dx = (unsigned long)&frame->info;
392	regs->cx = (unsigned long)&frame->uc;
393
394	regs->ds = __USER_DS;
395	regs->es = __USER_DS;
396	regs->ss = __USER_DS;
397	regs->cs = __USER_CS;
398
399	return 0;
400}
401#else /* !CONFIG_X86_32 */
402static int __setup_rt_frame(int sig, struct ksignal *ksig,
403			    sigset_t *set, struct pt_regs *regs)
404{
405	struct rt_sigframe __user *frame;
406	void __user *fp = NULL;
407	int err = 0;
408
409	frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
410
411	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
412		return -EFAULT;
413
414	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
415		if (copy_siginfo_to_user(&frame->info, &ksig->info))
416			return -EFAULT;
417	}
418
419	put_user_try {
420		/* Create the ucontext.  */
421		if (cpu_has_xsave)
422			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
423		else
424			put_user_ex(0, &frame->uc.uc_flags);
425		put_user_ex(0, &frame->uc.uc_link);
426		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
427
428		/* Set up to return from userspace.  If provided, use a stub
429		   already in userspace.  */
430		/* x86-64 should always use SA_RESTORER. */
431		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
432			put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
433		} else {
434			/* could use a vstub here */
435			err |= -EFAULT;
436		}
437	} put_user_catch(err);
438
439	err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
440	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
441
442	if (err)
443		return -EFAULT;
444
445	/* Set up registers for signal handler */
446	regs->di = sig;
447	/* In case the signal handler was declared without prototypes */
448	regs->ax = 0;
449
450	/* This also works for non SA_SIGINFO handlers because they expect the
451	   next argument after the signal number on the stack. */
452	regs->si = (unsigned long)&frame->info;
453	regs->dx = (unsigned long)&frame->uc;
454	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
455
456	regs->sp = (unsigned long)frame;
457
458	/* Set up the CS register to run signal handlers in 64-bit mode,
459	   even if the handler happens to be interrupting 32-bit code. */
460	regs->cs = __USER_CS;
461
462	return 0;
463}
464#endif /* CONFIG_X86_32 */
465
466static int x32_setup_rt_frame(struct ksignal *ksig,
467			      compat_sigset_t *set,
468			      struct pt_regs *regs)
469{
470#ifdef CONFIG_X86_X32_ABI
471	struct rt_sigframe_x32 __user *frame;
472	void __user *restorer;
473	int err = 0;
474	void __user *fpstate = NULL;
475
476	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
477
478	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
479		return -EFAULT;
480
481	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
482		if (copy_siginfo_to_user32(&frame->info, &ksig->info))
483			return -EFAULT;
484	}
485
486	put_user_try {
487		/* Create the ucontext.  */
488		if (cpu_has_xsave)
489			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
490		else
491			put_user_ex(0, &frame->uc.uc_flags);
492		put_user_ex(0, &frame->uc.uc_link);
493		compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
494		put_user_ex(0, &frame->uc.uc__pad0);
495
496		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
497			restorer = ksig->ka.sa.sa_restorer;
498		} else {
499			/* could use a vstub here */
500			restorer = NULL;
501			err |= -EFAULT;
502		}
503		put_user_ex(restorer, &frame->pretcode);
504	} put_user_catch(err);
505
506	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
507				regs, set->sig[0]);
508	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
509
510	if (err)
511		return -EFAULT;
512
513	/* Set up registers for signal handler */
514	regs->sp = (unsigned long) frame;
515	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
516
517	/* We use the x32 calling convention here... */
518	regs->di = ksig->sig;
519	regs->si = (unsigned long) &frame->info;
520	regs->dx = (unsigned long) &frame->uc;
521
522	loadsegment(ds, __USER_DS);
523	loadsegment(es, __USER_DS);
524
525	regs->cs = __USER_CS;
526	regs->ss = __USER_DS;
527#endif	/* CONFIG_X86_X32_ABI */
528
529	return 0;
530}
531
532/*
533 * Do a signal return; undo the signal stack.
 
 
 
 
 
 
 
 
 
 
 
 
534 */
535#ifdef CONFIG_X86_32
536asmlinkage unsigned long sys_sigreturn(void)
537{
538	struct pt_regs *regs = current_pt_regs();
539	struct sigframe __user *frame;
540	unsigned long ax;
541	sigset_t set;
542
543	frame = (struct sigframe __user *)(regs->sp - 8);
544
545	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
546		goto badframe;
547	if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
548		&& __copy_from_user(&set.sig[1], &frame->extramask,
549				    sizeof(frame->extramask))))
550		goto badframe;
551
552	set_current_blocked(&set);
553
554	if (restore_sigcontext(regs, &frame->sc, &ax))
555		goto badframe;
556	return ax;
557
558badframe:
559	signal_fault(regs, frame, "sigreturn");
560
561	return 0;
562}
563#endif /* CONFIG_X86_32 */
564
565asmlinkage long sys_rt_sigreturn(void)
566{
567	struct pt_regs *regs = current_pt_regs();
568	struct rt_sigframe __user *frame;
569	unsigned long ax;
570	sigset_t set;
571
572	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
573	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
574		goto badframe;
575	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
576		goto badframe;
577
578	set_current_blocked(&set);
579
580	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
581		goto badframe;
582
583	if (restore_altstack(&frame->uc.uc_stack))
584		goto badframe;
585
586	return ax;
587
588badframe:
589	signal_fault(regs, frame, "rt_sigreturn");
590	return 0;
591}
592
593/*
594 * OK, we're invoking a handler:
595 */
596static int signr_convert(int sig)
597{
598#ifdef CONFIG_X86_32
599	struct thread_info *info = current_thread_info();
600
601	if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
602		return info->exec_domain->signal_invmap[sig];
603#endif /* CONFIG_X86_32 */
604	return sig;
605}
606
607static int
608setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
609{
610	int usig = signr_convert(ksig->sig);
611	sigset_t *set = sigmask_to_save();
612	compat_sigset_t *cset = (compat_sigset_t *) set;
613
614	/* Set up the stack frame */
615	if (is_ia32_frame()) {
616		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
617			return ia32_setup_rt_frame(usig, ksig, cset, regs);
618		else
619			return ia32_setup_frame(usig, ksig, cset, regs);
620	} else if (is_x32_frame()) {
621		return x32_setup_rt_frame(ksig, cset, regs);
622	} else {
623		return __setup_rt_frame(ksig->sig, ksig, set, regs);
624	}
625}
626
627static void
628handle_signal(struct ksignal *ksig, struct pt_regs *regs)
629{
630	bool failed;
 
 
 
 
 
631	/* Are we from a system call? */
632	if (syscall_get_nr(current, regs) >= 0) {
633		/* If so, check system call restarting.. */
634		switch (syscall_get_error(current, regs)) {
635		case -ERESTART_RESTARTBLOCK:
636		case -ERESTARTNOHAND:
637			regs->ax = -EINTR;
638			break;
639
640		case -ERESTARTSYS:
641			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
642				regs->ax = -EINTR;
643				break;
644			}
645		/* fallthrough */
646		case -ERESTARTNOINTR:
647			regs->ax = regs->orig_ax;
648			regs->ip -= 2;
649			break;
650		}
651	}
652
653	/*
654	 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
655	 * flag so that register information in the sigcontext is correct.
 
656	 */
657	if (unlikely(regs->flags & X86_EFLAGS_TF) &&
658	    likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
659		regs->flags &= ~X86_EFLAGS_TF;
660
661	failed = (setup_rt_frame(ksig, regs) < 0);
662	if (!failed) {
663		/*
664		 * Clear the direction flag as per the ABI for function entry.
665		 *
666		 * Clear RF when entering the signal handler, because
667		 * it might disable possible debug exception from the
668		 * signal handler.
669		 *
670		 * Clear TF when entering the signal handler, but
671		 * notify any tracer that was single-stepping it.
672		 * The tracer may want to single-step inside the
673		 * handler too.
674		 */
675		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
 
 
 
 
676	}
677	signal_setup_done(failed, ksig, test_thread_flag(TIF_SINGLESTEP));
678}
679
680#ifdef CONFIG_X86_32
681#define NR_restart_syscall	__NR_restart_syscall
682#else /* !CONFIG_X86_32 */
683#define NR_restart_syscall	\
684	test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
685#endif /* CONFIG_X86_32 */
 
 
 
 
 
 
686
687/*
688 * Note that 'init' is a special process: it doesn't get signals it doesn't
689 * want to handle. Thus you cannot kill init even with a SIGKILL even by
690 * mistake.
691 */
692static void do_signal(struct pt_regs *regs)
693{
694	struct ksignal ksig;
695
696	if (get_signal(&ksig)) {
697		/* Whee! Actually deliver the signal.  */
698		handle_signal(&ksig, regs);
699		return;
700	}
701
702	/* Did we come from a system call? */
703	if (syscall_get_nr(current, regs) >= 0) {
704		/* Restart the system call - no handlers present */
705		switch (syscall_get_error(current, regs)) {
706		case -ERESTARTNOHAND:
707		case -ERESTARTSYS:
708		case -ERESTARTNOINTR:
709			regs->ax = regs->orig_ax;
710			regs->ip -= 2;
711			break;
712
713		case -ERESTART_RESTARTBLOCK:
714			regs->ax = NR_restart_syscall;
715			regs->ip -= 2;
716			break;
717		}
718	}
719
720	/*
721	 * If there's no signal to deliver, we just put the saved sigmask
722	 * back.
723	 */
724	restore_saved_sigmask();
725}
726
727/*
728 * notification of userspace execution resumption
729 * - triggered by the TIF_WORK_MASK flags
730 */
731__visible void
732do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
733{
734	user_exit();
735
736#ifdef CONFIG_X86_MCE
737	/* notify userspace of pending MCEs */
738	if (thread_info_flags & _TIF_MCE_NOTIFY)
739		mce_notify_process();
740#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
741
742	if (thread_info_flags & _TIF_UPROBE)
743		uprobe_notify_resume(regs);
744
745	/* deal with pending signal delivery */
746	if (thread_info_flags & _TIF_SIGPENDING)
747		do_signal(regs);
748
749	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
750		clear_thread_flag(TIF_NOTIFY_RESUME);
751		tracehook_notify_resume(regs);
752	}
753	if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
754		fire_user_return_notifiers();
755
756	user_enter();
757}
758
759void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
760{
761	struct task_struct *me = current;
762
763	if (show_unhandled_signals && printk_ratelimit()) {
764		printk("%s"
765		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
766		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
767		       me->comm, me->pid, where, frame,
768		       regs->ip, regs->sp, regs->orig_ax);
769		print_vma_addr(" in ", regs->ip);
770		pr_cont("\n");
771	}
772
773	force_sig(SIGSEGV, me);
774}
775
776#ifdef CONFIG_X86_X32_ABI
777asmlinkage long sys32_x32_rt_sigreturn(void)
 
 
 
 
 
 
778{
779	struct pt_regs *regs = current_pt_regs();
780	struct rt_sigframe_x32 __user *frame;
781	sigset_t set;
782	unsigned long ax;
783
784	frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
785
786	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
787		goto badframe;
788	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
789		goto badframe;
790
791	set_current_blocked(&set);
 
792
793	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
794		goto badframe;
 
795
796	if (compat_restore_altstack(&frame->uc.uc_stack))
797		goto badframe;
798
799	return ax;
 
 
800
801badframe:
802	signal_fault(regs, frame, "x32 rt_sigreturn");
803	return 0;
804}
805#endif