xref: /openbmc/linux/arch/x86/kernel/signal.c (revision b34e08d5)
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 		       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 
121 int 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  */
186 static 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 
200 static inline void __user *
201 get_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
252 static 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 
262 static 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 
274 static 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 
337 static 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 */
402 static 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 
466 static 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
536 asmlinkage 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 
558 badframe:
559 	signal_fault(regs, frame, "sigreturn");
560 
561 	return 0;
562 }
563 #endif /* CONFIG_X86_32 */
564 
565 asmlinkage 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 
588 badframe:
589 	signal_fault(regs, frame, "rt_sigreturn");
590 	return 0;
591 }
592 
593 /*
594  * OK, we're invoking a handler:
595  */
596 static 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 
607 static int
608 setup_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 
627 static void
628 handle_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  */
692 static 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
732 do_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 
759 void 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
777 asmlinkage 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 
801 badframe:
802 	signal_fault(regs, frame, "x32 rt_sigreturn");
803 	return 0;
804 }
805 #endif
806