xref: /openbmc/linux/arch/x86/kernel/signal.c (revision 03cb0503)
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 
64 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
65 {
66 	void __user *buf;
67 	unsigned int tmpflags;
68 	unsigned int err = 0;
69 
70 	/* Always make any pending restarted system calls return -EINTR */
71 	current->restart_block.fn = do_no_restart_syscall;
72 
73 	get_user_try {
74 
75 #ifdef CONFIG_X86_32
76 		set_user_gs(regs, GET_SEG(gs));
77 		COPY_SEG(fs);
78 		COPY_SEG(es);
79 		COPY_SEG(ds);
80 #endif /* CONFIG_X86_32 */
81 
82 		COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
83 		COPY(dx); COPY(cx); COPY(ip); COPY(ax);
84 
85 #ifdef CONFIG_X86_64
86 		COPY(r8);
87 		COPY(r9);
88 		COPY(r10);
89 		COPY(r11);
90 		COPY(r12);
91 		COPY(r13);
92 		COPY(r14);
93 		COPY(r15);
94 #endif /* CONFIG_X86_64 */
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 		regs->orig_ax = -1;		/* disable syscall checks */
102 
103 		get_user_ex(buf, &sc->fpstate);
104 	} get_user_catch(err);
105 
106 	err |= restore_xstate_sig(buf, config_enabled(CONFIG_X86_32));
107 
108 	force_iret();
109 
110 	return err;
111 }
112 
113 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
114 		     struct pt_regs *regs, unsigned long mask)
115 {
116 	int err = 0;
117 
118 	put_user_try {
119 
120 #ifdef CONFIG_X86_32
121 		put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
122 		put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
123 		put_user_ex(regs->es, (unsigned int __user *)&sc->es);
124 		put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
125 #endif /* CONFIG_X86_32 */
126 
127 		put_user_ex(regs->di, &sc->di);
128 		put_user_ex(regs->si, &sc->si);
129 		put_user_ex(regs->bp, &sc->bp);
130 		put_user_ex(regs->sp, &sc->sp);
131 		put_user_ex(regs->bx, &sc->bx);
132 		put_user_ex(regs->dx, &sc->dx);
133 		put_user_ex(regs->cx, &sc->cx);
134 		put_user_ex(regs->ax, &sc->ax);
135 #ifdef CONFIG_X86_64
136 		put_user_ex(regs->r8, &sc->r8);
137 		put_user_ex(regs->r9, &sc->r9);
138 		put_user_ex(regs->r10, &sc->r10);
139 		put_user_ex(regs->r11, &sc->r11);
140 		put_user_ex(regs->r12, &sc->r12);
141 		put_user_ex(regs->r13, &sc->r13);
142 		put_user_ex(regs->r14, &sc->r14);
143 		put_user_ex(regs->r15, &sc->r15);
144 #endif /* CONFIG_X86_64 */
145 
146 		put_user_ex(current->thread.trap_nr, &sc->trapno);
147 		put_user_ex(current->thread.error_code, &sc->err);
148 		put_user_ex(regs->ip, &sc->ip);
149 #ifdef CONFIG_X86_32
150 		put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
151 		put_user_ex(regs->flags, &sc->flags);
152 		put_user_ex(regs->sp, &sc->sp_at_signal);
153 		put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
154 #else /* !CONFIG_X86_32 */
155 		put_user_ex(regs->flags, &sc->flags);
156 		put_user_ex(regs->cs, &sc->cs);
157 		put_user_ex(0, &sc->__pad2);
158 		put_user_ex(0, &sc->__pad1);
159 		put_user_ex(regs->ss, &sc->ss);
160 #endif /* CONFIG_X86_32 */
161 
162 		put_user_ex(fpstate, &sc->fpstate);
163 
164 		/* non-iBCS2 extensions.. */
165 		put_user_ex(mask, &sc->oldmask);
166 		put_user_ex(current->thread.cr2, &sc->cr2);
167 	} put_user_catch(err);
168 
169 	return err;
170 }
171 
172 /*
173  * Set up a signal frame.
174  */
175 
176 /*
177  * Determine which stack to use..
178  */
179 static unsigned long align_sigframe(unsigned long sp)
180 {
181 #ifdef CONFIG_X86_32
182 	/*
183 	 * Align the stack pointer according to the i386 ABI,
184 	 * i.e. so that on function entry ((sp + 4) & 15) == 0.
185 	 */
186 	sp = ((sp + 4) & -16ul) - 4;
187 #else /* !CONFIG_X86_32 */
188 	sp = round_down(sp, 16) - 8;
189 #endif
190 	return sp;
191 }
192 
193 static inline void __user *
194 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
195 	     void __user **fpstate)
196 {
197 	/* Default to using normal stack */
198 	unsigned long math_size = 0;
199 	unsigned long sp = regs->sp;
200 	unsigned long buf_fx = 0;
201 	int onsigstack = on_sig_stack(sp);
202 
203 	/* redzone */
204 	if (config_enabled(CONFIG_X86_64))
205 		sp -= 128;
206 
207 	if (!onsigstack) {
208 		/* This is the X/Open sanctioned signal stack switching.  */
209 		if (ka->sa.sa_flags & SA_ONSTACK) {
210 			if (current->sas_ss_size)
211 				sp = current->sas_ss_sp + current->sas_ss_size;
212 		} else if (config_enabled(CONFIG_X86_32) &&
213 			   (regs->ss & 0xffff) != __USER_DS &&
214 			   !(ka->sa.sa_flags & SA_RESTORER) &&
215 			   ka->sa.sa_restorer) {
216 				/* This is the legacy signal stack switching. */
217 				sp = (unsigned long) ka->sa.sa_restorer;
218 		}
219 	}
220 
221 	if (used_math()) {
222 		sp = alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
223 				     &buf_fx, &math_size);
224 		*fpstate = (void __user *)sp;
225 	}
226 
227 	sp = align_sigframe(sp - frame_size);
228 
229 	/*
230 	 * If we are on the alternate signal stack and would overflow it, don't.
231 	 * Return an always-bogus address instead so we will die with SIGSEGV.
232 	 */
233 	if (onsigstack && !likely(on_sig_stack(sp)))
234 		return (void __user *)-1L;
235 
236 	/* save i387 and extended state */
237 	if (used_math() &&
238 	    save_xstate_sig(*fpstate, (void __user *)buf_fx, math_size) < 0)
239 		return (void __user *)-1L;
240 
241 	return (void __user *)sp;
242 }
243 
244 #ifdef CONFIG_X86_32
245 static const struct {
246 	u16 poplmovl;
247 	u32 val;
248 	u16 int80;
249 } __attribute__((packed)) retcode = {
250 	0xb858,		/* popl %eax; movl $..., %eax */
251 	__NR_sigreturn,
252 	0x80cd,		/* int $0x80 */
253 };
254 
255 static const struct {
256 	u8  movl;
257 	u32 val;
258 	u16 int80;
259 	u8  pad;
260 } __attribute__((packed)) rt_retcode = {
261 	0xb8,		/* movl $..., %eax */
262 	__NR_rt_sigreturn,
263 	0x80cd,		/* int $0x80 */
264 	0
265 };
266 
267 static int
268 __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
269 	      struct pt_regs *regs)
270 {
271 	struct sigframe __user *frame;
272 	void __user *restorer;
273 	int err = 0;
274 	void __user *fpstate = NULL;
275 
276 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
277 
278 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
279 		return -EFAULT;
280 
281 	if (__put_user(sig, &frame->sig))
282 		return -EFAULT;
283 
284 	if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
285 		return -EFAULT;
286 
287 	if (_NSIG_WORDS > 1) {
288 		if (__copy_to_user(&frame->extramask, &set->sig[1],
289 				   sizeof(frame->extramask)))
290 			return -EFAULT;
291 	}
292 
293 	if (current->mm->context.vdso)
294 		restorer = current->mm->context.vdso +
295 			selected_vdso32->sym___kernel_sigreturn;
296 	else
297 		restorer = &frame->retcode;
298 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
299 		restorer = ksig->ka.sa.sa_restorer;
300 
301 	/* Set up to return from userspace.  */
302 	err |= __put_user(restorer, &frame->pretcode);
303 
304 	/*
305 	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
306 	 *
307 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
308 	 * reasons and because gdb uses it as a signature to notice
309 	 * signal handler stack frames.
310 	 */
311 	err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
312 
313 	if (err)
314 		return -EFAULT;
315 
316 	/* Set up registers for signal handler */
317 	regs->sp = (unsigned long)frame;
318 	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
319 	regs->ax = (unsigned long)sig;
320 	regs->dx = 0;
321 	regs->cx = 0;
322 
323 	regs->ds = __USER_DS;
324 	regs->es = __USER_DS;
325 	regs->ss = __USER_DS;
326 	regs->cs = __USER_CS;
327 
328 	return 0;
329 }
330 
331 static int __setup_rt_frame(int sig, struct ksignal *ksig,
332 			    sigset_t *set, struct pt_regs *regs)
333 {
334 	struct rt_sigframe __user *frame;
335 	void __user *restorer;
336 	int err = 0;
337 	void __user *fpstate = NULL;
338 
339 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
340 
341 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
342 		return -EFAULT;
343 
344 	put_user_try {
345 		put_user_ex(sig, &frame->sig);
346 		put_user_ex(&frame->info, &frame->pinfo);
347 		put_user_ex(&frame->uc, &frame->puc);
348 
349 		/* Create the ucontext.  */
350 		if (cpu_has_xsave)
351 			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
352 		else
353 			put_user_ex(0, &frame->uc.uc_flags);
354 		put_user_ex(0, &frame->uc.uc_link);
355 		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
356 
357 		/* Set up to return from userspace.  */
358 		restorer = current->mm->context.vdso +
359 			selected_vdso32->sym___kernel_rt_sigreturn;
360 		if (ksig->ka.sa.sa_flags & SA_RESTORER)
361 			restorer = ksig->ka.sa.sa_restorer;
362 		put_user_ex(restorer, &frame->pretcode);
363 
364 		/*
365 		 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
366 		 *
367 		 * WE DO NOT USE IT ANY MORE! It's only left here for historical
368 		 * reasons and because gdb uses it as a signature to notice
369 		 * signal handler stack frames.
370 		 */
371 		put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
372 	} put_user_catch(err);
373 
374 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
375 	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
376 				regs, set->sig[0]);
377 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
378 
379 	if (err)
380 		return -EFAULT;
381 
382 	/* Set up registers for signal handler */
383 	regs->sp = (unsigned long)frame;
384 	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
385 	regs->ax = (unsigned long)sig;
386 	regs->dx = (unsigned long)&frame->info;
387 	regs->cx = (unsigned long)&frame->uc;
388 
389 	regs->ds = __USER_DS;
390 	regs->es = __USER_DS;
391 	regs->ss = __USER_DS;
392 	regs->cs = __USER_CS;
393 
394 	return 0;
395 }
396 #else /* !CONFIG_X86_32 */
397 static int __setup_rt_frame(int sig, struct ksignal *ksig,
398 			    sigset_t *set, struct pt_regs *regs)
399 {
400 	struct rt_sigframe __user *frame;
401 	void __user *fp = NULL;
402 	int err = 0;
403 
404 	frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
405 
406 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
407 		return -EFAULT;
408 
409 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
410 		if (copy_siginfo_to_user(&frame->info, &ksig->info))
411 			return -EFAULT;
412 	}
413 
414 	put_user_try {
415 		/* Create the ucontext.  */
416 		if (cpu_has_xsave)
417 			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
418 		else
419 			put_user_ex(0, &frame->uc.uc_flags);
420 		put_user_ex(0, &frame->uc.uc_link);
421 		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
422 
423 		/* Set up to return from userspace.  If provided, use a stub
424 		   already in userspace.  */
425 		/* x86-64 should always use SA_RESTORER. */
426 		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
427 			put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
428 		} else {
429 			/* could use a vstub here */
430 			err |= -EFAULT;
431 		}
432 	} put_user_catch(err);
433 
434 	err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
435 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
436 
437 	if (err)
438 		return -EFAULT;
439 
440 	/* Set up registers for signal handler */
441 	regs->di = sig;
442 	/* In case the signal handler was declared without prototypes */
443 	regs->ax = 0;
444 
445 	/* This also works for non SA_SIGINFO handlers because they expect the
446 	   next argument after the signal number on the stack. */
447 	regs->si = (unsigned long)&frame->info;
448 	regs->dx = (unsigned long)&frame->uc;
449 	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
450 
451 	regs->sp = (unsigned long)frame;
452 
453 	/*
454 	 * Set up the CS and SS registers to run signal handlers in
455 	 * 64-bit mode, even if the handler happens to be interrupting
456 	 * 32-bit or 16-bit code.
457 	 *
458 	 * SS is subtle.  In 64-bit mode, we don't need any particular
459 	 * SS descriptor, but we do need SS to be valid.  It's possible
460 	 * that the old SS is entirely bogus -- this can happen if the
461 	 * signal we're trying to deliver is #GP or #SS caused by a bad
462 	 * SS value.
463 	 */
464 	regs->cs = __USER_CS;
465 	regs->ss = __USER_DS;
466 
467 	return 0;
468 }
469 #endif /* CONFIG_X86_32 */
470 
471 static int x32_setup_rt_frame(struct ksignal *ksig,
472 			      compat_sigset_t *set,
473 			      struct pt_regs *regs)
474 {
475 #ifdef CONFIG_X86_X32_ABI
476 	struct rt_sigframe_x32 __user *frame;
477 	void __user *restorer;
478 	int err = 0;
479 	void __user *fpstate = NULL;
480 
481 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
482 
483 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
484 		return -EFAULT;
485 
486 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
487 		if (copy_siginfo_to_user32(&frame->info, &ksig->info))
488 			return -EFAULT;
489 	}
490 
491 	put_user_try {
492 		/* Create the ucontext.  */
493 		if (cpu_has_xsave)
494 			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
495 		else
496 			put_user_ex(0, &frame->uc.uc_flags);
497 		put_user_ex(0, &frame->uc.uc_link);
498 		compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
499 		put_user_ex(0, &frame->uc.uc__pad0);
500 
501 		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
502 			restorer = ksig->ka.sa.sa_restorer;
503 		} else {
504 			/* could use a vstub here */
505 			restorer = NULL;
506 			err |= -EFAULT;
507 		}
508 		put_user_ex(restorer, &frame->pretcode);
509 	} put_user_catch(err);
510 
511 	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
512 				regs, set->sig[0]);
513 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
514 
515 	if (err)
516 		return -EFAULT;
517 
518 	/* Set up registers for signal handler */
519 	regs->sp = (unsigned long) frame;
520 	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
521 
522 	/* We use the x32 calling convention here... */
523 	regs->di = ksig->sig;
524 	regs->si = (unsigned long) &frame->info;
525 	regs->dx = (unsigned long) &frame->uc;
526 
527 	loadsegment(ds, __USER_DS);
528 	loadsegment(es, __USER_DS);
529 
530 	regs->cs = __USER_CS;
531 	regs->ss = __USER_DS;
532 #endif	/* CONFIG_X86_X32_ABI */
533 
534 	return 0;
535 }
536 
537 /*
538  * Do a signal return; undo the signal stack.
539  */
540 #ifdef CONFIG_X86_32
541 asmlinkage unsigned long sys_sigreturn(void)
542 {
543 	struct pt_regs *regs = current_pt_regs();
544 	struct sigframe __user *frame;
545 	sigset_t set;
546 
547 	frame = (struct sigframe __user *)(regs->sp - 8);
548 
549 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
550 		goto badframe;
551 	if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
552 		&& __copy_from_user(&set.sig[1], &frame->extramask,
553 				    sizeof(frame->extramask))))
554 		goto badframe;
555 
556 	set_current_blocked(&set);
557 
558 	if (restore_sigcontext(regs, &frame->sc))
559 		goto badframe;
560 	return regs->ax;
561 
562 badframe:
563 	signal_fault(regs, frame, "sigreturn");
564 
565 	return 0;
566 }
567 #endif /* CONFIG_X86_32 */
568 
569 asmlinkage long sys_rt_sigreturn(void)
570 {
571 	struct pt_regs *regs = current_pt_regs();
572 	struct rt_sigframe __user *frame;
573 	sigset_t set;
574 
575 	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
576 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
577 		goto badframe;
578 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
579 		goto badframe;
580 
581 	set_current_blocked(&set);
582 
583 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
584 		goto badframe;
585 
586 	if (restore_altstack(&frame->uc.uc_stack))
587 		goto badframe;
588 
589 	return regs->ax;
590 
591 badframe:
592 	signal_fault(regs, frame, "rt_sigreturn");
593 	return 0;
594 }
595 
596 static int
597 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
598 {
599 	int usig = ksig->sig;
600 	sigset_t *set = sigmask_to_save();
601 	compat_sigset_t *cset = (compat_sigset_t *) set;
602 
603 	/* Set up the stack frame */
604 	if (is_ia32_frame()) {
605 		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
606 			return ia32_setup_rt_frame(usig, ksig, cset, regs);
607 		else
608 			return ia32_setup_frame(usig, ksig, cset, regs);
609 	} else if (is_x32_frame()) {
610 		return x32_setup_rt_frame(ksig, cset, regs);
611 	} else {
612 		return __setup_rt_frame(ksig->sig, ksig, set, regs);
613 	}
614 }
615 
616 static void
617 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
618 {
619 	bool stepping, failed;
620 
621 	/* Are we from a system call? */
622 	if (syscall_get_nr(current, regs) >= 0) {
623 		/* If so, check system call restarting.. */
624 		switch (syscall_get_error(current, regs)) {
625 		case -ERESTART_RESTARTBLOCK:
626 		case -ERESTARTNOHAND:
627 			regs->ax = -EINTR;
628 			break;
629 
630 		case -ERESTARTSYS:
631 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
632 				regs->ax = -EINTR;
633 				break;
634 			}
635 		/* fallthrough */
636 		case -ERESTARTNOINTR:
637 			regs->ax = regs->orig_ax;
638 			regs->ip -= 2;
639 			break;
640 		}
641 	}
642 
643 	/*
644 	 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
645 	 * so that register information in the sigcontext is correct and
646 	 * then notify the tracer before entering the signal handler.
647 	 */
648 	stepping = test_thread_flag(TIF_SINGLESTEP);
649 	if (stepping)
650 		user_disable_single_step(current);
651 
652 	failed = (setup_rt_frame(ksig, regs) < 0);
653 	if (!failed) {
654 		/*
655 		 * Clear the direction flag as per the ABI for function entry.
656 		 *
657 		 * Clear RF when entering the signal handler, because
658 		 * it might disable possible debug exception from the
659 		 * signal handler.
660 		 *
661 		 * Clear TF for the case when it wasn't set by debugger to
662 		 * avoid the recursive send_sigtrap() in SIGTRAP handler.
663 		 */
664 		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
665 		/*
666 		 * Ensure the signal handler starts with the new fpu state.
667 		 */
668 		if (used_math())
669 			fpu_reset_state(current);
670 	}
671 	signal_setup_done(failed, ksig, stepping);
672 }
673 
674 #ifdef CONFIG_X86_32
675 #define NR_restart_syscall	__NR_restart_syscall
676 #else /* !CONFIG_X86_32 */
677 #define NR_restart_syscall	\
678 	test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
679 #endif /* CONFIG_X86_32 */
680 
681 /*
682  * Note that 'init' is a special process: it doesn't get signals it doesn't
683  * want to handle. Thus you cannot kill init even with a SIGKILL even by
684  * mistake.
685  */
686 static void do_signal(struct pt_regs *regs)
687 {
688 	struct ksignal ksig;
689 
690 	if (get_signal(&ksig)) {
691 		/* Whee! Actually deliver the signal.  */
692 		handle_signal(&ksig, regs);
693 		return;
694 	}
695 
696 	/* Did we come from a system call? */
697 	if (syscall_get_nr(current, regs) >= 0) {
698 		/* Restart the system call - no handlers present */
699 		switch (syscall_get_error(current, regs)) {
700 		case -ERESTARTNOHAND:
701 		case -ERESTARTSYS:
702 		case -ERESTARTNOINTR:
703 			regs->ax = regs->orig_ax;
704 			regs->ip -= 2;
705 			break;
706 
707 		case -ERESTART_RESTARTBLOCK:
708 			regs->ax = NR_restart_syscall;
709 			regs->ip -= 2;
710 			break;
711 		}
712 	}
713 
714 	/*
715 	 * If there's no signal to deliver, we just put the saved sigmask
716 	 * back.
717 	 */
718 	restore_saved_sigmask();
719 }
720 
721 /*
722  * notification of userspace execution resumption
723  * - triggered by the TIF_WORK_MASK flags
724  */
725 __visible void
726 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
727 {
728 	user_exit();
729 
730 	if (thread_info_flags & _TIF_UPROBE)
731 		uprobe_notify_resume(regs);
732 
733 	/* deal with pending signal delivery */
734 	if (thread_info_flags & _TIF_SIGPENDING)
735 		do_signal(regs);
736 
737 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
738 		clear_thread_flag(TIF_NOTIFY_RESUME);
739 		tracehook_notify_resume(regs);
740 	}
741 	if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
742 		fire_user_return_notifiers();
743 
744 	user_enter();
745 }
746 
747 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
748 {
749 	struct task_struct *me = current;
750 
751 	if (show_unhandled_signals && printk_ratelimit()) {
752 		printk("%s"
753 		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
754 		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
755 		       me->comm, me->pid, where, frame,
756 		       regs->ip, regs->sp, regs->orig_ax);
757 		print_vma_addr(" in ", regs->ip);
758 		pr_cont("\n");
759 	}
760 
761 	force_sig(SIGSEGV, me);
762 }
763 
764 #ifdef CONFIG_X86_X32_ABI
765 asmlinkage long sys32_x32_rt_sigreturn(void)
766 {
767 	struct pt_regs *regs = current_pt_regs();
768 	struct rt_sigframe_x32 __user *frame;
769 	sigset_t set;
770 
771 	frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
772 
773 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
774 		goto badframe;
775 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
776 		goto badframe;
777 
778 	set_current_blocked(&set);
779 
780 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
781 		goto badframe;
782 
783 	if (compat_restore_altstack(&frame->uc.uc_stack))
784 		goto badframe;
785 
786 	return regs->ax;
787 
788 badframe:
789 	signal_fault(regs, frame, "x32 rt_sigreturn");
790 	return 0;
791 }
792 #endif
793